KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.bouncycastle.asn1.ASN1EncodableVector;
25 import org.bouncycastle.asn1.ASN1Sequence;
26 import org.bouncycastle.asn1.ASN1TaggedObject;
27 import org.bouncycastle.asn1.DERBitString;
28 import org.bouncycastle.asn1.DEREncodable;
29 import org.bouncycastle.asn1.DERInteger;
30 import org.bouncycastle.asn1.DERObject;
31 import org.bouncycastle.asn1.DERSequence;
32 import org.bouncycastle.asn1.DERTaggedObject;
33 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
34 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
35 import org.bouncycastle.asn1.x509.X509Extensions;
36 import org.bouncycastle.asn1.x509.X509Name;
37
38 /**
39  * ASN.1 structure DER En/DeCoder.
40  *
41  * <pre>
42  * CertTemplate ::= SEQUENCE {
43  * version [0] Version OPTIONAL,
44  * serialNumber [1] INTEGER OPTIONAL,
45  * signingAlg [2] AlgorithmIdentifier OPTIONAL,
46  * issuer [3] Name OPTIONAL,
47  * validity [4] OptionalValidity OPTIONAL,
48  * subject [5] Name OPTIONAL,
49  * publicKey [6] SubjectPublicKeyInfo OPTIONAL,
50  * issuerUID [7] UniqueIdentifier OPTIONAL,
51  * subjectUID [8] UniqueIdentifier OPTIONAL,
52  * extensions [9] Extensions OPTIONAL }
53  *
54  * </pre>
55  */

56 public class CertTemplate implements DEREncodable
57 {
58     // name is not a choice type --> tag it implicit...no but it should be explicit?
59
public static final boolean bNameIsExplicit = true;
60
61     private DERInteger version = null;
62     private DERInteger serialNumber = null;
63     private AlgorithmIdentifier signingAlg = null;
64     private X509Name issuer = null;
65     private OptionalValidity validity = null;
66     private X509Name subject = null;
67     private SubjectPublicKeyInfo publicKey = null;
68     private DERBitString issuerUID = null;
69     private DERBitString subjectUID = null;
70     private X509Extensions extensions = null;
71
72     public static CertTemplate getInstance( ASN1TaggedObject obj, boolean explicit )
73     {
74         return getInstance(ASN1Sequence.getInstance(obj, explicit));
75     }
76
77     public static CertTemplate getInstance( Object JavaDoc obj )
78     {
79         if (obj == null) {
80             return new CertTemplate();
81         }
82         else if (obj instanceof CertTemplate) {
83             return (CertTemplate)obj;
84         }
85         else if (obj instanceof ASN1Sequence) {
86             return new CertTemplate((ASN1Sequence)obj);
87         }
88         else {
89             throw new IllegalArgumentException JavaDoc("unknown object in factory");
90         }
91     }
92
93     public CertTemplate( ASN1Sequence seq )
94     {
95       Enumeration JavaDoc e = (seq == null ? null : seq.getObjects());
96       while (e != null && e.hasMoreElements())
97       {
98         DERTaggedObject obj = (DERTaggedObject)e.nextElement();
99         int tagno = (obj == null ? -1 : obj.getTagNo());
100         switch( tagno )
101         {
102           case 0: this.version = DERInteger.getInstance( obj, false ); break;
103           case 1: this.serialNumber = DERInteger.getInstance( obj, false ); break;
104           case 2: this.signingAlg = AlgorithmIdentifier.getInstance( obj, false ); break;
105           case 3: this.issuer = X509Name.getInstance( obj, bNameIsExplicit ); break;
106           case 4: this.validity = OptionalValidity.getInstance( obj, false ); break;
107           case 5: this.subject = X509Name.getInstance( obj, bNameIsExplicit ); break;
108           case 6: this.publicKey = SubjectPublicKeyInfo.getInstance( obj, false ); break;
109           case 7: this.issuerUID = DERBitString.getInstance( obj, false ); break;
110           case 8: this.subjectUID = DERBitString.getInstance( obj, false ); break;
111           case 9: this.extensions = X509Extensions.getInstance( obj, false ); break;
112           default : throw new IllegalArgumentException JavaDoc("invalid asn1 sequence");
113         }
114       }
115     }
116
117     public CertTemplate()
118     {
119     }
120
121     public DERInteger getVersion()
122     {
123       return version;
124     }
125
126     public void setVersion(DERInteger version)
127     {
128       this.version = version;
129     }
130
131     public DERInteger getSerialNumber()
132     {
133       return serialNumber;
134     }
135
136     public void setSerialNumber(DERInteger serialNumber)
137     {
138       this.serialNumber = serialNumber;
139     }
140
141     public AlgorithmIdentifier getSigningAlg()
142     {
143       return signingAlg;
144     }
145
146     public void setSigningAlg(AlgorithmIdentifier signingAlg)
147     {
148       this.signingAlg = signingAlg;
149     }
150
151     public X509Name getIssuer()
152     {
153       return issuer;
154     }
155
156     public void setIssuer(X509Name issuer)
157     {
158       this.issuer = issuer;
159     }
160
161     public OptionalValidity getValidity()
162     {
163       return validity;
164     }
165
166     public void setValidity(OptionalValidity validity)
167     {
168       this.validity = validity;
169     }
170
171     public X509Name getSubject()
172     {
173       return subject;
174     }
175
176     public void setSubject(X509Name subject)
177     {
178       this.subject = subject;
179     }
180
181     public SubjectPublicKeyInfo getPublicKey()
182     {
183       return publicKey;
184     }
185
186     public void setPublicKey(SubjectPublicKeyInfo publicKey)
187     {
188       this.publicKey = publicKey;
189     }
190
191     public DERBitString getIssuerUID()
192     {
193       return issuerUID;
194     }
195
196     public void setIssuerUID(DERBitString issuerUID)
197     {
198       this.issuerUID = issuerUID;
199     }
200
201     public DERBitString getSubjectUID()
202     {
203       return subjectUID;
204     }
205
206     public void setSubjectUID(DERBitString subjectUID)
207     {
208       this.subjectUID = subjectUID;
209     }
210
211     public X509Extensions getExtensions()
212     {
213       return extensions;
214     }
215
216     public void setExtensions(X509Extensions extensions)
217     {
218       this.extensions = extensions;
219     }
220
221     public DERObject getDERObject()
222     {
223         ASN1EncodableVector v = new ASN1EncodableVector();
224
225         if( version != null )
226           v.add( new DERTaggedObject( false, 0, version ) );
227         if( serialNumber != null )
228           v.add( new DERTaggedObject( false, 1, serialNumber ) );
229         if( signingAlg != null )
230           v.add( new DERTaggedObject( false, 2, signingAlg ) );
231         if( issuer != null )
232           v.add( new DERTaggedObject( bNameIsExplicit, 3, issuer) );
233         if( validity != null )
234           v.add( new DERTaggedObject( false, 4, validity ) );
235         if( subject != null )
236           v.add( new DERTaggedObject( bNameIsExplicit, 5, subject ) );
237         if( publicKey != null )
238           v.add( new DERTaggedObject( false, 6, publicKey ) );
239         if( issuerUID != null )
240           v.add( new DERTaggedObject( false, 7, issuerUID ) );
241         if( subjectUID != null )
242           v.add( new DERTaggedObject( false, 8, subjectUID ) );
243         if( extensions != null )
244           v.add( new DERTaggedObject( false, 9, extensions ) );
245
246         return new DERSequence(v);
247     }
248
249     public String JavaDoc toString() {
250         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(this.getClass().getName());
251         sb.append(" (");
252
253         if( this.getVersion() != null )
254             sb.append("version: " + this.getVersion() + ", ");
255
256         if( this.getSerialNumber() != null )
257             sb.append("serialNumber: " + this.getSerialNumber() + ", ");
258
259         if( this.getSigningAlg() != null )
260             sb.append("signingAlg: " + this.getSigningAlg() + ", ");
261
262         if( this.getIssuer() != null )
263             sb.append("issuer: " + this.getIssuer() + ", ");
264
265         if( this.getValidity() != null )
266             sb.append("validity: " + this.getValidity() + ", ");
267
268         if( this.getSubject() != null )
269             sb.append("subject: " + this.getSubject() + ", ");
270
271         if( this.getPublicKey() != null )
272             sb.append("publicKey: " + this.getPublicKey() + ", ");
273
274         if( this.getIssuerUID() != null )
275             sb.append("issuerUID: " + this.getIssuerUID() + ", ");
276
277         if( this.getSubjectUID() != null )
278             sb.append("subjectUID: " + this.getSubjectUID() + ", ");
279
280         if( this.getExtensions() != null )
281             sb.append("extensions: " + this.getExtensions() + ", ");
282
283         sb.append("hashCode: " + Integer.toHexString(this.hashCode()) + ")");
284         return sb.toString();
285     }
286 }
287
Popular Tags