KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > TBSCertificateStructure


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22 import com.maverick.crypto.asn1.ASN1Sequence;
23 import com.maverick.crypto.asn1.ASN1TaggedObject;
24 import com.maverick.crypto.asn1.DERBitString;
25 import com.maverick.crypto.asn1.DEREncodable;
26 import com.maverick.crypto.asn1.DERInteger;
27 import com.maverick.crypto.asn1.DERObject;
28 import com.maverick.crypto.asn1.DERTaggedObject;
29 import com.maverick.crypto.asn1.pkcs.PKCSObjectIdentifiers;
30
31 /**
32  * The TBSCertificate object.
33  * <pre>
34  * TBSCertificate ::= SEQUENCE {
35  * version [ 0 ] Version DEFAULT v1(0),
36  * serialNumber CertificateSerialNumber,
37  * signature AlgorithmIdentifier,
38  * issuer Name,
39  * validity Validity,
40  * subject Name,
41  * subjectPublicKeyInfo SubjectPublicKeyInfo,
42  * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
43  * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
44  * extensions [ 3 ] Extensions OPTIONAL
45  * }
46  * </pre>
47  * <p>
48  * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
49  * will parse them, but you really shouldn't be creating new ones.
50  */

51 public class TBSCertificateStructure
52     implements DEREncodable, X509ObjectIdentifiers, PKCSObjectIdentifiers
53 {
54     ASN1Sequence seq;
55
56     DERInteger version;
57     DERInteger serialNumber;
58     AlgorithmIdentifier signature;
59     X509Name issuer;
60     Time startDate, endDate;
61     X509Name subject;
62     SubjectPublicKeyInfo subjectPublicKeyInfo;
63     DERBitString issuerUniqueId;
64     DERBitString subjectUniqueId;
65     X509Extensions extensions;
66
67     public static TBSCertificateStructure getInstance(
68         ASN1TaggedObject obj,
69         boolean explicit)
70     {
71         return getInstance(ASN1Sequence.getInstance(obj, explicit));
72     }
73
74     public static TBSCertificateStructure getInstance(
75         Object JavaDoc obj)
76     {
77         if (obj instanceof TBSCertificateStructure)
78         {
79             return (TBSCertificateStructure)obj;
80         }
81         else if (obj instanceof ASN1Sequence)
82         {
83             return new TBSCertificateStructure((ASN1Sequence)obj);
84         }
85
86         throw new IllegalArgumentException JavaDoc("unknown object in factory");
87     }
88
89     public TBSCertificateStructure(
90         ASN1Sequence seq)
91     {
92         int seqStart = 0;
93
94         this.seq = seq;
95
96         //
97
// some certficates don't include a version number - we assume v1
98
//
99
if (seq.getObjectAt(0) instanceof DERTaggedObject)
100         {
101             version = DERInteger.getInstance(seq.getObjectAt(0));
102         }
103         else
104         {
105             seqStart = -1; // field 0 is missing!
106
version = new DERInteger(0);
107         }
108
109         serialNumber = DERInteger.getInstance(seq.getObjectAt(seqStart + 1));
110
111         signature = AlgorithmIdentifier.getInstance(seq.getObjectAt(seqStart + 2));
112         issuer = X509Name.getInstance(seq.getObjectAt(seqStart + 3));
113
114         //
115
// before and after dates
116
//
117
ASN1Sequence dates = (ASN1Sequence)seq.getObjectAt(seqStart + 4);
118
119         startDate = Time.getInstance(dates.getObjectAt(0));
120         endDate = Time.getInstance(dates.getObjectAt(1));
121
122         subject = X509Name.getInstance(seq.getObjectAt(seqStart + 5));
123
124         //
125
// public key info.
126
//
127
subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(seq.getObjectAt(seqStart + 6));
128
129         for (int extras = seq.size() - (seqStart + 6) - 1; extras > 0; extras--)
130         {
131             DERTaggedObject extra = (DERTaggedObject)seq.getObjectAt(seqStart + 6 + extras);
132
133             switch (extra.getTagNo())
134             {
135             case 1:
136                 issuerUniqueId = DERBitString.getInstance(extra);
137                 break;
138             case 2:
139                 subjectUniqueId = DERBitString.getInstance(extra);
140                 break;
141             case 3:
142                 extensions = X509Extensions.getInstance(extra);
143             }
144         }
145     }
146
147     public int getVersion()
148     {
149         return version.getValue().intValue() + 1;
150     }
151
152     public DERInteger getVersionNumber()
153     {
154         return version;
155     }
156
157     public DERInteger getSerialNumber()
158     {
159         return serialNumber;
160     }
161
162     public AlgorithmIdentifier getSignature()
163     {
164         return signature;
165     }
166
167     public X509Name getIssuer()
168     {
169         return issuer;
170     }
171
172     public Time getStartDate()
173     {
174         return startDate;
175     }
176
177     public Time getEndDate()
178     {
179         return endDate;
180     }
181
182     public X509Name getSubject()
183     {
184         return subject;
185     }
186
187     public SubjectPublicKeyInfo getSubjectPublicKeyInfo()
188     {
189         return subjectPublicKeyInfo;
190     }
191
192     public DERBitString getIssuerUniqueId()
193     {
194         return issuerUniqueId;
195     }
196
197     public DERBitString getSubjectUniqueId()
198     {
199         return subjectUniqueId;
200     }
201
202     public X509Extensions getExtensions()
203     {
204         return extensions;
205     }
206
207     public DERObject getDERObject()
208     {
209         return seq;
210     }
211 }
212
Popular Tags