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 java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.util.Enumeration;
24  
25  import org.apache.geronimo.util.asn1.ASN1Encodable;
26  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
27  import org.apache.geronimo.util.asn1.ASN1InputStream;
28  import org.apache.geronimo.util.asn1.ASN1Sequence;
29  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
30  import org.apache.geronimo.util.asn1.DERBitString;
31  import org.apache.geronimo.util.asn1.DEREncodable;
32  import org.apache.geronimo.util.asn1.DERObject;
33  import org.apache.geronimo.util.asn1.DERSequence;
34  
35  /**
36   * The object that contains the public key stored in a certficate.
37   * <p>
38   * The getEncoded() method in the public keys in the JCE produces a DER
39   * encoded one of these.
40   */
41  public class SubjectPublicKeyInfo
42      extends ASN1Encodable
43  {
44      private AlgorithmIdentifier     algId;
45      private DERBitString            keyData;
46  
47      public static SubjectPublicKeyInfo getInstance(
48          ASN1TaggedObject obj,
49          boolean          explicit)
50      {
51          return getInstance(ASN1Sequence.getInstance(obj, explicit));
52      }
53  
54      public static SubjectPublicKeyInfo getInstance(
55          Object  obj)
56      {
57          if (obj instanceof SubjectPublicKeyInfo)
58          {
59              return (SubjectPublicKeyInfo)obj;
60          }
61          else if (obj instanceof ASN1Sequence)
62          {
63              return new SubjectPublicKeyInfo((ASN1Sequence)obj);
64          }
65  
66          throw new IllegalArgumentException("unknown object in factory");
67      }
68  
69      public SubjectPublicKeyInfo(
70          AlgorithmIdentifier algId,
71          DEREncodable        publicKey)
72      {
73          this.keyData = new DERBitString(publicKey);
74          this.algId = algId;
75      }
76  
77      public SubjectPublicKeyInfo(
78          AlgorithmIdentifier algId,
79          byte[]              publicKey)
80      {
81          this.keyData = new DERBitString(publicKey);
82          this.algId = algId;
83      }
84  
85      public SubjectPublicKeyInfo(
86          ASN1Sequence  seq)
87      {
88          Enumeration         e = seq.getObjects();
89  
90          this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
91          this.keyData = (DERBitString)e.nextElement();
92      }
93  
94      public AlgorithmIdentifier getAlgorithmId()
95      {
96          return algId;
97      }
98  
99      /**
100      * for when the public key is an encoded object - if the bitstring
101      * can't be decoded this routine throws an IOException.
102      *
103      * @exception IOException - if the bit string doesn't represent a DER
104      * encoded object.
105      */
106     public DERObject getPublicKey()
107         throws IOException
108     {
109         ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData.getBytes());
110         ASN1InputStream         aIn = new ASN1InputStream(bIn);
111 
112         return aIn.readObject();
113     }
114 
115     /**
116      * for when the public key is raw bits...
117      */
118     public DERBitString getPublicKeyData()
119     {
120         return keyData;
121     }
122 
123     /**
124      * Produce an object suitable for an ASN1OutputStream.
125      * <pre>
126      * SubjectPublicKeyInfo ::= SEQUENCE {
127      *                          algorithm AlgorithmIdentifier,
128      *                          publicKey BIT STRING }
129      * </pre>
130      */
131     public DERObject toASN1Object()
132     {
133         ASN1EncodableVector  v = new ASN1EncodableVector();
134 
135         v.add(algId);
136         v.add(keyData);
137 
138         return new DERSequence(v);
139     }
140 }