View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import org.apache.geronimo.util.asn1.ASN1Encodable;
22  import org.apache.geronimo.util.asn1.ASN1Sequence;
23  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
24  import org.apache.geronimo.util.asn1.DERBitString;
25  import org.apache.geronimo.util.asn1.DERInteger;
26  import org.apache.geronimo.util.asn1.DERObject;
27  import org.apache.geronimo.util.asn1.DERTaggedObject;
28  import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
29  
30  /**
31   * The TBSCertificate object.
32   * <pre>
33   * TBSCertificate ::= SEQUENCE {
34   *      version          [ 0 ]  Version DEFAULT v1(0),
35   *      serialNumber            CertificateSerialNumber,
36   *      signature               AlgorithmIdentifier,
37   *      issuer                  Name,
38   *      validity                Validity,
39   *      subject                 Name,
40   *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
41   *      issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
42   *      subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
43   *      extensions        [ 3 ] Extensions OPTIONAL
44   *      }
45   * </pre>
46   * <p>
47   * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
48   * will parse them, but you really shouldn't be creating new ones.
49   */
50  public class TBSCertificateStructure
51      extends ASN1Encodable
52      implements 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  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("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, false);
137                 break;
138             case 2:
139                 subjectUniqueId = DERBitString.getInstance(extra, false);
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 toASN1Object()
208     {
209         return seq;
210     }
211 }