KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeyRecRepContent ::= SEQUENCE {
39  * status PKIStatusInfo,
40  * newSigCert [0] Certificate OPTIONAL, (X509CertificateStructure)
41  * caCerts [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL, (X509CertificateStructure)
42  * keyPairHist [2] SEQUENCE SIZE (1..MAX) OF CertifiedKeyPair OPTIONAL
43  * }
44  *
45  * </pre>
46  */

47 public class KeyRecRepContent implements DEREncodable
48 {
49   PKIStatusInfo status;
50   X509CertificateStructure newSigCert;
51   Vector JavaDoc caCerts = new Vector JavaDoc();
52   Vector JavaDoc keyPairHists = new Vector JavaDoc();
53
54   public static KeyRecRepContent getInstance(ASN1TaggedObject obj, boolean explicit)
55   {
56     return getInstance(ASN1Sequence.getInstance(obj, explicit));
57   }
58
59   public static KeyRecRepContent getInstance(Object JavaDoc obj)
60   {
61     if (obj instanceof KeyRecRepContent)
62     {
63       return (KeyRecRepContent) obj;
64     }
65     else if (obj instanceof ASN1Sequence)
66     {
67       return new KeyRecRepContent((ASN1Sequence) obj);
68     }
69
70     throw new IllegalArgumentException JavaDoc("unknown object in factory");
71   }
72
73   public KeyRecRepContent(ASN1Sequence seq)
74   {
75     Enumeration JavaDoc e = seq.getObjects();
76
77     status = PKIStatusInfo.getInstance(e.nextElement());
78
79     while (e.hasMoreElements())
80     {
81       ASN1TaggedObject tagObj = (ASN1TaggedObject) e.nextElement();
82
83       switch (tagObj.getTagNo())
84       {
85         case 0 :
86           newSigCert = X509CertificateStructure.getInstance(tagObj.getObject());
87           break;
88         case 1 :
89           {
90             ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
91             for (int i = 0; i < s.size(); i++)
92               caCerts.addElement(X509CertificateStructure.getInstance(s.getObjectAt(i)));
93           }
94           break;
95         case 2 :
96           {
97             ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
98             for (int i = 0; i < s.size(); i++)
99               keyPairHists.addElement(CertifiedKeyPair.getInstance(s.getObjectAt(i)));
100           }
101           break;
102       }
103     }
104   }
105
106   public KeyRecRepContent(PKIStatusInfo status)
107   {
108     this.status = status;
109   }
110
111   public PKIStatusInfo getStatus()
112   {
113     return status;
114   }
115
116   public X509CertificateStructure getNewSigCert()
117   {
118     return newSigCert;
119   }
120
121   public void setNewSigCert(X509CertificateStructure newSigCert)
122   {
123     this.newSigCert = newSigCert;
124   }
125
126   public void addCaCerts(X509CertificateStructure caCert)
127   {
128     caCerts.addElement(caCert);
129   }
130
131   public X509CertificateStructure getCaCerts(int nr)
132   {
133     if (nr < caCerts.size())
134       return (X509CertificateStructure) caCerts.elementAt(nr);
135
136     return null;
137   }
138
139   public void addKeyPairHist(CertifiedKeyPair keyPairHist)
140   {
141     keyPairHists.addElement(keyPairHist);
142   }
143
144   public CertifiedKeyPair getKeyPairHist(int nr)
145   {
146     if (nr < keyPairHists.size())
147       return (CertifiedKeyPair) keyPairHists.elementAt(nr);
148
149     return null;
150   }
151
152   public DERObject getDERObject()
153   {
154     ASN1EncodableVector v = new ASN1EncodableVector();
155
156     v.add(status);
157
158     if (newSigCert != null)
159       v.add(new DERTaggedObject(true, 0, newSigCert));
160
161     if (caCerts.size() > 0)
162     {
163       ASN1EncodableVector cacv = new ASN1EncodableVector();
164
165       for (int i = 0; i < caCerts.size(); i++)
166         cacv.add((X509CertificateStructure) caCerts.elementAt(i));
167
168       v.add(new DERTaggedObject(true, 1, new DERSequence(cacv)));
169     }
170
171     if (keyPairHists.size() > 0)
172     {
173       ASN1EncodableVector keyphv = new ASN1EncodableVector();
174
175       for (int i = 0; i < keyPairHists.size(); i++)
176         keyphv.add((CertifiedKeyPair) keyPairHists.elementAt(i));
177
178       v.add(new DERTaggedObject(true, 2, new DERSequence(keyphv)));
179     }
180
181     return new DERSequence(v);
182   }
183
184   public String JavaDoc toString()
185   {
186     String JavaDoc s = "CertifiedKeyPair: ( status: " + this.getStatus() + ", ";
187
188     if( this.getNewSigCert() != null )
189       s += "newSigCert: "+ this.getNewSigCert() + ", ";
190
191     if( caCerts.size() > 0 )
192     {
193       s += "caCerts: (";
194       
195       for( int i=0; i<caCerts.size(); i++ )
196         s += (X509CertificateStructure)caCerts.elementAt(i);
197         
198       s += "), ";
199     }
200     
201     if( keyPairHists.size() > 0 )
202     {
203       s += "keyPairHist: (";
204       
205       for( int i=0; i<caCerts.size(); i++ )
206         s += (CertifiedKeyPair)keyPairHists.elementAt(i);
207         
208       s += ")";
209     }
210
211     s += ")";
212     
213     return s;
214   }
215 }
216
Popular Tags