KickJava   Java API By Example, From Geeks To Geeks.

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


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.DERObject;
30 import org.bouncycastle.asn1.DERSequence;
31 import org.bouncycastle.asn1.DERTaggedObject;
32 import org.bouncycastle.asn1.x509.X509CertificateStructure;
33
34 /**
35  * ASN.1 structure DER En/DeCoder.
36  *
37  * <pre>
38  * CertRepMessage ::= SEQUENCE {
39  * caPubs [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL, (X509CertificateStructure)
40  * response SEQUENCE OF CertResponse
41  * }
42  *
43  * </pre>
44  */

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