KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 import java.io.ByteArrayOutputStream JavaDoc;
26
27 import org.bouncycastle.asn1.*;
28 import org.bouncycastle.asn1.x509.*;
29
30 /**
31  * ASN.1 structure DER En/DeCoder.
32  *
33  * <pre>
34  * PKIMessage ::= SEQUENCE {
35  * header PKIMessage,
36  * body PKIBody,
37  * protection [0] PKIProtection OPTIONAL, -- (BIT STRING)
38  * extraCerts [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL
39  * }
40  *
41  * </pre>
42  */

43 public class PKIMessage implements DEREncodable
44 {
45     PKIHeader header;
46     PKIBody body;
47     DERBitString protection;
48     Vector JavaDoc extraCerts = new Vector JavaDoc();
49     byte protectedBytes[];
50
51     public static PKIMessage getInstance( ASN1TaggedObject obj, boolean explicit )
52     {
53         return getInstance(ASN1Sequence.getInstance(obj, explicit));
54     }
55
56     public static PKIMessage getInstance( Object JavaDoc obj )
57     {
58         if (obj instanceof PKIMessage)
59         {
60             return (PKIMessage)obj;
61         }
62         else if (obj instanceof ASN1Sequence)
63         {
64             return new PKIMessage((ASN1Sequence)obj);
65         }
66
67         throw new IllegalArgumentException JavaDoc("unknown object in factory");
68     }
69   
70     public PKIMessage( ASN1Sequence seq )
71     {
72       Enumeration JavaDoc e = seq.getObjects();
73       
74 /*
75       header = PKIHeader.getInstance( e.nextElement() );
76       body = PKIBody.getInstance( (ASN1TaggedObject)e.nextElement() );
77 */

78
79       DEREncodable derHeader = (DEREncodable)e.nextElement();
80       DEREncodable derBody = (DEREncodable)e.nextElement();
81       
82       try
83       {
84         //store protected part in unmodified form...
85
ASN1EncodableVector v = new ASN1EncodableVector();
86         v.add( derHeader );
87         v.add( derBody );
88         
89         ByteArrayOutputStream JavaDoc bao = new ByteArrayOutputStream JavaDoc();
90         DEROutputStream out = new DEROutputStream( bao );
91         out.writeObject( new DERSequence(v) );
92       
93         protectedBytes = bao.toByteArray();
94       }
95       catch( Exception JavaDoc ex ) {}
96          
97       header = PKIHeader.getInstance( derHeader );
98       body = PKIBody.getInstance( (ASN1TaggedObject)derBody );
99       
100       while (e.hasMoreElements())
101       {
102         ASN1TaggedObject tagObj = (ASN1TaggedObject)e.nextElement();
103
104         switch (tagObj.getTagNo())
105         {
106           case 0: protection = DERBitString.getInstance( tagObj.getObject() ); break;
107           case 1:
108             ASN1Sequence s = (ASN1Sequence)tagObj.getObject();
109             for( int i=0; i<s.size(); i++ )
110               extraCerts.addElement( X509CertificateStructure.getInstance(s.getObjectAt(i)) );
111             break;
112         }
113       }
114     }
115
116     public PKIMessage( PKIHeader header, PKIBody body )
117     {
118         this.header = header;
119         this.body = body;
120     }
121
122     public PKIHeader getHeader()
123     {
124         return header;
125     }
126
127     public PKIBody getBody()
128     {
129         return body;
130     }
131     
132     public void setProtection( DERBitString protection )
133     {
134       this.protection = protection;
135     }
136
137     public DERBitString getProtection()
138     {
139       return protection;
140     }
141
142     public void addExtraCert( X509CertificateStructure extraCert )
143     {
144       this.extraCerts.addElement(extraCert);
145     }
146
147     public X509CertificateStructure getExtraCert(int nr)
148     {
149       if (extraCerts.size() > nr)
150         return (X509CertificateStructure)extraCerts.elementAt(nr);
151
152       return null;
153     }
154
155     public DERObject getDERObject()
156     {
157       ASN1EncodableVector v = new ASN1EncodableVector();
158
159       v.add( header );
160       v.add( body );
161       
162       if( protection != null )
163         v.add( new DERTaggedObject( true, 0, protection ) );
164
165       if( extraCerts.size() > 0 )
166       {
167         ASN1EncodableVector giv = new ASN1EncodableVector();
168   
169         for (int i=0;i<extraCerts.size();i++)
170           giv.add((X509CertificateStructure)extraCerts.elementAt(i));
171   
172         v.add( new DERTaggedObject( true, 1, new DERSequence(giv) ) );
173       }
174       
175       return new DERSequence(v);
176     }
177     
178     public byte[] getProtectedBytes()
179     {
180       if( protectedBytes != null )
181         return protectedBytes;
182
183       try
184       {
185         ByteArrayOutputStream JavaDoc bao = new ByteArrayOutputStream JavaDoc();
186         DEROutputStream out = new DEROutputStream( bao );
187         out.writeObject( getProtectedPart() );
188         return bao.toByteArray();
189       }
190       catch(Exception JavaDoc ex){}
191       
192       return null;
193     }
194     
195     public ProtectedPart getProtectedPart()
196     {
197       return new ProtectedPart( header, body );
198     }
199
200     public String JavaDoc toString()
201     {
202       String JavaDoc s = "PKIMessage: ( header: " + this.getHeader() + ", body: " + this.getBody() + ", ";
203
204       if( this.getProtection() != null )
205         s += "protection: "+ this.getProtection() + ", ";
206       
207       if( extraCerts.size() > 0 )
208       {
209         s += "extraCerts: (";
210         for (int i=0;i<extraCerts.size();i++)
211           s += extraCerts.elementAt(i) + ", ";
212         s += ")";
213       }
214
215       s += ")";
216       
217       return s;
218     }
219 }
220
Popular Tags