KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > cmp > PKIHeader


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.cmp;
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.DERGeneralizedTime;
30 import org.bouncycastle.asn1.DERInteger;
31 import org.bouncycastle.asn1.DERObject;
32 import org.bouncycastle.asn1.DEROctetString;
33 import org.bouncycastle.asn1.DERSequence;
34 import org.bouncycastle.asn1.DERTaggedObject;
35 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
36 import org.bouncycastle.asn1.x509.GeneralName;
37
38 /**
39  * ASN.1 structure DER En/DeCoder.
40  *
41  * <pre>
42  * PKIHeader ::= SEQUENCE {
43  * pvno INTEGER { ietf-version2 (1) },
44  * sender GeneralName, -- identifies the sender
45  * recipient GeneralName, -- identifies the intended recipient
46  * messageTime [0] GeneralizedTime OPTIONAL, -- time of production of this message
47  * protectionAlg [1] AlgorithmIdentifier OPTIONAL, -- algorithm used for calculation of protection bits
48  * senderKID [2] KeyIdentifier OPTIONAL, -- (OCTET STRING)
49  * recipKID [3] KeyIdentifier OPTIONAL, -- (OCTET STRING) to identify specific keys used for protection
50  * transactionID [4] OCTET STRING OPTIONAL, -- identifies the transaction; i.e., this will be the same in corresponding request, response and confirmation messages
51  * senderNonce [5] OCTET STRING OPTIONAL,
52  * recipNonce [6] OCTET STRING OPTIONAL, -- nonces used to provide replay protection, senderNonce is inserted by the creator of this message; recipNonce is a nonce previously inserted in a related message by the intended recipient of this message
53  * freeText [7] PKIFreeText OPTIONAL, -- this may be used to indicate context-specific instructions (this field is intended for human consumption)
54  * generalInfo [8] SEQUENCE SIZE (1..MAX) OF
55  * InfoTypeAndValue OPTIONAL -- this may be used to convey context-specific information (this field not primarily intended for human consumption)
56  * }
57  *
58  * </pre>
59  */

60 public class PKIHeader implements DEREncodable
61 {
62     DERInteger pvno;
63     GeneralName sender;
64     GeneralName recipient;
65     DERGeneralizedTime messageTime;
66     AlgorithmIdentifier protectionAlg;
67     DEROctetString senderKID;
68     DEROctetString recipKID;
69     DEROctetString transactionID;
70     DEROctetString senderNonce;
71     DEROctetString recipNonce;
72     PKIFreeText freeText;
73     Vector JavaDoc generalInfos = new Vector JavaDoc();
74
75     public static PKIHeader getInstance( ASN1TaggedObject obj, boolean explicit )
76     {
77         return getInstance(ASN1Sequence.getInstance(obj, explicit));
78     }
79
80     public static PKIHeader getInstance( Object JavaDoc obj )
81     {
82         if (obj instanceof PKIHeader)
83         {
84             return (PKIHeader)obj;
85         }
86         else if (obj instanceof ASN1Sequence)
87         {
88             return new PKIHeader((ASN1Sequence)obj);
89         }
90
91         throw new IllegalArgumentException JavaDoc("unknown object in factory");
92     }
93     
94     public PKIHeader( ASN1Sequence seq )
95     {
96       Enumeration JavaDoc e = seq.getObjects();
97
98       pvno = DERInteger.getInstance(e.nextElement());
99       sender = GeneralName.getInstance(e.nextElement());
100       recipient = GeneralName.getInstance(e.nextElement());
101       
102       while (e.hasMoreElements())
103       {
104         ASN1TaggedObject tagObj = (ASN1TaggedObject)e.nextElement();
105
106         switch (tagObj.getTagNo())
107         {
108           case 0: messageTime = DERGeneralizedTime.getInstance(tagObj.getObject()); break;
109           case 1: protectionAlg = AlgorithmIdentifier.getInstance(tagObj.getObject()); break;
110           case 2: senderKID = (DEROctetString)DEROctetString.getInstance(tagObj); break;
111           case 3: recipKID = (DEROctetString)DEROctetString.getInstance(tagObj); break;
112           case 4: transactionID = (DEROctetString)DEROctetString.getInstance(tagObj); break;
113           case 5: senderNonce = (DEROctetString)DEROctetString.getInstance(tagObj); break;
114           case 6: recipNonce = (DEROctetString)DEROctetString.getInstance(tagObj); break;
115           case 7: freeText = PKIFreeText.getInstance(tagObj.getObject()); break;
116           case 8:
117             ASN1Sequence s = (ASN1Sequence)tagObj.getObject();
118             for( int i=0; i<s.size(); i++ )
119               generalInfos.addElement( InfoTypeAndValue.getInstance(s.getObjectAt(i)) );
120             break;
121         }
122       }
123     }
124
125     public PKIHeader( DERInteger pvno, GeneralName sender, GeneralName recipient )
126     {
127         this.pvno = pvno;
128         this.sender = sender;
129         this.recipient = recipient;
130     }
131
132     public DERInteger getPvno()
133     {
134         return pvno;
135     }
136
137     public GeneralName getSender()
138     {
139         return sender;
140     }
141
142     public GeneralName getRecipient()
143     {
144         return recipient;
145     }
146     
147     public void setMessageTime( DERGeneralizedTime messageTime )
148     {
149       this.messageTime = messageTime;
150     }
151
152     public DERGeneralizedTime getMessageTime()
153     {
154       return messageTime;
155     }
156
157     public void setProtectionAlg( AlgorithmIdentifier protectionAlg )
158     {
159       this.protectionAlg = protectionAlg;
160     }
161
162     public AlgorithmIdentifier getProtectionAlg()
163     {
164       return protectionAlg;
165     }
166
167     public void setSenderKID( DEROctetString senderKID )
168     {
169       this.senderKID = senderKID;
170     }
171
172     public DEROctetString getSenderKID()
173     {
174       return senderKID;
175     }
176
177     public void setRecipKID( DEROctetString recipKID )
178     {
179       this.recipKID = recipKID;
180     }
181
182     public DEROctetString getRecipKID()
183     {
184       return recipKID;
185     }
186
187     public void setTransactionID( DEROctetString transactionID )
188     {
189       this.transactionID = transactionID;
190     }
191
192     public DEROctetString getTransactionID()
193     {
194       return transactionID;
195     }
196
197     public void setSenderNonce( DEROctetString senderNonce )
198     {
199       this.senderNonce = senderNonce;
200     }
201
202     public DEROctetString getSenderNonce()
203     {
204       return senderNonce;
205     }
206
207     public void setRecipNonce( DEROctetString recipNonce )
208     {
209       this.recipNonce = recipNonce;
210     }
211
212     public DEROctetString getRecipNonce()
213     {
214       return recipNonce;
215     }
216
217     public void setFreeText( PKIFreeText freeText )
218     {
219       this.freeText = freeText;
220     }
221
222     public PKIFreeText getFreeText()
223     {
224       return freeText;
225     }
226
227     public void addGeneralInfo( InfoTypeAndValue generalInfo )
228     {
229       this.generalInfos.addElement(generalInfo);
230     }
231
232     public InfoTypeAndValue getGeneralInfo(int nr)
233     {
234       if (generalInfos.size() > nr)
235         return (InfoTypeAndValue)generalInfos.elementAt(nr);
236
237       return null;
238     }
239
240     public DERObject getDERObject()
241     {
242       ASN1EncodableVector v = new ASN1EncodableVector();
243
244       v.add( pvno );
245       v.add( sender );
246       v.add( recipient );
247       
248       if( messageTime != null )
249         v.add( new DERTaggedObject( true, 0, messageTime ) );
250
251       if( protectionAlg != null )
252         v.add( new DERTaggedObject( true, 1, protectionAlg ) );
253
254       if( senderKID != null )
255         v.add( new DERTaggedObject( true, 2, senderKID ) );
256
257       if( recipKID != null )
258         v.add( new DERTaggedObject( true, 3, recipKID ) );
259
260       if( transactionID != null )
261         v.add( new DERTaggedObject( true, 4, transactionID ) );
262
263       if( senderNonce != null )
264         v.add( new DERTaggedObject( true, 5, senderNonce ) );
265
266       if( recipNonce != null )
267         v.add( new DERTaggedObject( true, 6, recipNonce ) );
268
269       if( freeText != null )
270         v.add( new DERTaggedObject( true, 7, freeText ) );
271
272       if( generalInfos.size() > 0 )
273       {
274         ASN1EncodableVector giv = new ASN1EncodableVector();
275   
276         for (int i=0;i<generalInfos.size();i++)
277           giv.add((InfoTypeAndValue)generalInfos.elementAt(i));
278   
279         v.add( new DERTaggedObject( true, 8, new DERSequence(giv) ) );
280       }
281       
282       return new DERSequence(v);
283     }
284
285     public String JavaDoc toString()
286     {
287       String JavaDoc s = "PKIHeader: ( pvno: " + pvno + ", sender: " + sender + ", recipient: " + recipient + ", ";
288
289       if( messageTime != null )
290         s += "messageTime: " + messageTime + ", ";
291
292       if( protectionAlg != null )
293         s += "protectionAlg: " + protectionAlg + ", ";
294
295       if( senderKID != null )
296         s += "senderKID: " + senderKID + ", ";
297
298       if( recipKID != null )
299         s += "recipKID: " + recipKID + ", ";
300
301       if( transactionID != null )
302         s += "transactionID: " + transactionID + ", ";
303
304       if( senderNonce != null )
305         s += "senderNonce: " + senderNonce + ", ";
306
307       if( recipNonce != null )
308         s += "recipNonce: " + recipNonce + ", ";
309       
310       if( freeText != null )
311         s += "freeText: " + freeText + ", ";
312
313       if( generalInfos.size() > 0 )
314       {
315         s += "generalInfo: (";
316         for (int i=0;i<generalInfos.size();i++)
317           s += generalInfos.elementAt(i) + ", ";
318         s += ")";
319       }
320       
321       return s;
322     }
323 }
324
Popular Tags