KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > crmf > CertReqMsg


1 // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
2
//
3
// Author: Maik Stohn
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
// software and associated documentation files (the "Software"), to deal in the Software
7
// without restriction, including without limitation the rights to use, copy, modify, merge,
8
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
// to whom the Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in all copies or
12
// substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19

20 package com.novosec.pkix.asn1.crmf;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.bouncycastle.asn1.ASN1EncodableVector;
26 import org.bouncycastle.asn1.ASN1Sequence;
27 import org.bouncycastle.asn1.ASN1TaggedObject;
28 import org.bouncycastle.asn1.DEREncodable;
29 import org.bouncycastle.asn1.DERObject;
30 import org.bouncycastle.asn1.DERSequence;
31
32 /**
33  * ASN.1 structure DER En/DeCoder.
34  *
35  * <pre>
36  * CertReqMsg ::= SEQUENCE {
37  * certReq CertRequest,
38  * pop ProofOfPossession OPTIONAL, -- content depends upon key type
39  * regInfo SEQUENCE SIZE(1..MAX) OF AttributeTypeAndValue OPTIONAL }
40  *
41  * </pre>
42  */

43 public class CertReqMsg implements DEREncodable
44 {
45     CertRequest certReq;
46     ProofOfPossession pop;
47     Vector JavaDoc regInfos = new Vector JavaDoc();
48
49     public static CertReqMsg getInstance( ASN1TaggedObject obj, boolean explicit )
50     {
51         return getInstance(ASN1Sequence.getInstance(obj, explicit));
52     }
53
54     public static CertReqMsg getInstance( Object JavaDoc obj )
55     {
56         if (obj instanceof CertReqMsg)
57         {
58             return (CertReqMsg)obj;
59         }
60         else if (obj instanceof ASN1Sequence)
61         {
62             return new CertReqMsg((ASN1Sequence)obj);
63         }
64
65         throw new IllegalArgumentException JavaDoc("unknown object in factory");
66     }
67     
68     public CertReqMsg( ASN1Sequence seq )
69     {
70       Enumeration JavaDoc e = seq.getObjects();
71       this.certReq = CertRequest.getInstance(e.nextElement());
72
73       Object JavaDoc obj = null;
74
75       if( e.hasMoreElements() )
76         obj = e.nextElement();
77       
78       if( obj instanceof ASN1TaggedObject )
79       {
80         this.pop = ProofOfPossession.getInstance((ASN1TaggedObject)obj);
81         
82         if( e.hasMoreElements() )
83           obj = e.nextElement();
84       }
85       
86       if( obj instanceof ASN1Sequence )
87       {
88         ASN1Sequence s = (ASN1Sequence)obj;
89         for( int i=0; i<s.size(); i++ )
90           regInfos.addElement( AttributeTypeAndValue.getInstance(s.getObjectAt(i)) );
91       }
92     }
93
94     public CertReqMsg( CertRequest certReq )
95     {
96       this.certReq = certReq;
97     }
98
99     public CertRequest getCertReq()
100     {
101       return certReq;
102     }
103
104     public ProofOfPossession getPop()
105     {
106       return pop;
107     }
108
109     public void setPop( ProofOfPossession pop )
110     {
111       this.pop = pop;
112     }
113
114     public AttributeTypeAndValue getRegInfo(int nr)
115     {
116       if( regInfos.size() > nr )
117         return (AttributeTypeAndValue)regInfos.elementAt(nr);
118         
119       return null;
120     }
121
122     public void addRegInfo(AttributeTypeAndValue regInfo)
123     {
124       regInfos.addElement( regInfo );
125     }
126
127     public DERObject getDERObject()
128     {
129       ASN1EncodableVector v = new ASN1EncodableVector();
130
131       v.add( certReq );
132       
133       if( pop != null )
134         v.add( pop );
135
136       if( regInfos.size() > 0 )
137       {
138         ASN1EncodableVector regiv = new ASN1EncodableVector();
139         for (int i=0;i<regInfos.size();i++)
140           regiv.add( (AttributeTypeAndValue)regInfos.elementAt(i) );
141           
142         v.add( new DERSequence( regiv ) );
143       }
144
145       return new DERSequence(v);
146     }
147
148     public String JavaDoc toString()
149     {
150       String JavaDoc s = "CertReqMsg: (certReq = " + this.getCertReq() + ", ";
151       
152       if( this.getPop() != null )
153         s += "pop: " + this.getPop() + ", ";
154       
155       if( regInfos.size() > 0 )
156       {
157         s += "regInfo : (";
158         
159         for (int i=0;i<regInfos.size();i++)
160           s += (AttributeTypeAndValue)regInfos.elementAt(i);
161           
162         s += ")";
163       }
164
165       s += ")";
166       
167       return s;
168     }
169 }
170
Popular Tags