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.pkcs.PKCSObjectIdentifiers;
28  
29  /**
30   * an X509Certificate structure.
31   * <pre>
32   *  Certificate ::= SEQUENCE {
33   *      tbsCertificate          TBSCertificate,
34   *      signatureAlgorithm      AlgorithmIdentifier,
35   *      signature               BIT STRING
36   *  }
37   * </pre>
38   */
39  public class X509CertificateStructure
40      extends ASN1Encodable
41      implements X509ObjectIdentifiers, PKCSObjectIdentifiers
42  {
43      ASN1Sequence  seq;
44      TBSCertificateStructure tbsCert;
45      AlgorithmIdentifier     sigAlgId;
46      DERBitString            sig;
47  
48      public static X509CertificateStructure getInstance(
49          ASN1TaggedObject obj,
50          boolean          explicit)
51      {
52          return getInstance(ASN1Sequence.getInstance(obj, explicit));
53      }
54  
55      public static X509CertificateStructure getInstance(
56          Object  obj)
57      {
58          if (obj instanceof X509CertificateStructure)
59          {
60              return (X509CertificateStructure)obj;
61          }
62          else if (obj instanceof ASN1Sequence)
63          {
64              return new X509CertificateStructure((ASN1Sequence)obj);
65          }
66  
67          throw new IllegalArgumentException("unknown object in factory");
68      }
69  
70      public X509CertificateStructure(
71          ASN1Sequence  seq)
72      {
73          this.seq = seq;
74  
75          //
76          // correct x509 certficate
77          //
78          if (seq.size() == 3)
79          {
80              tbsCert = TBSCertificateStructure.getInstance(seq.getObjectAt(0));
81              sigAlgId = AlgorithmIdentifier.getInstance(seq.getObjectAt(1));
82  
83              sig = (DERBitString)seq.getObjectAt(2);
84          }
85          else
86          {
87              throw new IllegalArgumentException("sequence wrong size for a certificate");
88          }
89      }
90  
91      public TBSCertificateStructure getTBSCertificate()
92      {
93          return tbsCert;
94      }
95  
96      public int getVersion()
97      {
98          return tbsCert.getVersion();
99      }
100 
101     public DERInteger getSerialNumber()
102     {
103         return tbsCert.getSerialNumber();
104     }
105 
106     public X509Name getIssuer()
107     {
108         return tbsCert.getIssuer();
109     }
110 
111     public Time getStartDate()
112     {
113         return tbsCert.getStartDate();
114     }
115 
116     public Time getEndDate()
117     {
118         return tbsCert.getEndDate();
119     }
120 
121     public X509Name getSubject()
122     {
123         return tbsCert.getSubject();
124     }
125 
126     public SubjectPublicKeyInfo getSubjectPublicKeyInfo()
127     {
128         return tbsCert.getSubjectPublicKeyInfo();
129     }
130 
131     public AlgorithmIdentifier getSignatureAlgorithm()
132     {
133         return sigAlgId;
134     }
135 
136     public DERBitString getSignature()
137     {
138         return sig;
139     }
140 
141     public DERObject toASN1Object()
142     {
143         return seq;
144     }
145 }