KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > SubjectPublicKeyInfo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 import org.apache.geronimo.util.asn1.ASN1Encodable;
25 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
26 import org.apache.geronimo.util.asn1.ASN1InputStream;
27 import org.apache.geronimo.util.asn1.ASN1Sequence;
28 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
29 import org.apache.geronimo.util.asn1.DERBitString;
30 import org.apache.geronimo.util.asn1.DEREncodable;
31 import org.apache.geronimo.util.asn1.DERObject;
32 import org.apache.geronimo.util.asn1.DERSequence;
33
34 /**
35  * The object that contains the public key stored in a certficate.
36  * <p>
37  * The getEncoded() method in the public keys in the JCE produces a DER
38  * encoded one of these.
39  */

40 public class SubjectPublicKeyInfo
41     extends ASN1Encodable
42 {
43     private AlgorithmIdentifier algId;
44     private DERBitString keyData;
45
46     public static SubjectPublicKeyInfo getInstance(
47         ASN1TaggedObject obj,
48         boolean explicit)
49     {
50         return getInstance(ASN1Sequence.getInstance(obj, explicit));
51     }
52
53     public static SubjectPublicKeyInfo getInstance(
54         Object JavaDoc obj)
55     {
56         if (obj instanceof SubjectPublicKeyInfo)
57         {
58             return (SubjectPublicKeyInfo)obj;
59         }
60         else if (obj instanceof ASN1Sequence)
61         {
62             return new SubjectPublicKeyInfo((ASN1Sequence)obj);
63         }
64
65         throw new IllegalArgumentException JavaDoc("unknown object in factory");
66     }
67
68     public SubjectPublicKeyInfo(
69         AlgorithmIdentifier algId,
70         DEREncodable publicKey)
71     {
72         this.keyData = new DERBitString(publicKey);
73         this.algId = algId;
74     }
75
76     public SubjectPublicKeyInfo(
77         AlgorithmIdentifier algId,
78         byte[] publicKey)
79     {
80         this.keyData = new DERBitString(publicKey);
81         this.algId = algId;
82     }
83
84     public SubjectPublicKeyInfo(
85         ASN1Sequence seq)
86     {
87         Enumeration JavaDoc e = seq.getObjects();
88
89         this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
90         this.keyData = (DERBitString)e.nextElement();
91     }
92
93     public AlgorithmIdentifier getAlgorithmId()
94     {
95         return algId;
96     }
97
98     /**
99      * for when the public key is an encoded object - if the bitstring
100      * can't be decoded this routine throws an IOException.
101      *
102      * @exception IOException - if the bit string doesn't represent a DER
103      * encoded object.
104      */

105     public DERObject getPublicKey()
106         throws IOException JavaDoc
107     {
108         ByteArrayInputStream JavaDoc bIn = new ByteArrayInputStream JavaDoc(keyData.getBytes());
109         ASN1InputStream aIn = new ASN1InputStream(bIn);
110
111         return aIn.readObject();
112     }
113
114     /**
115      * for when the public key is raw bits...
116      */

117     public DERBitString getPublicKeyData()
118     {
119         return keyData;
120     }
121
122     /**
123      * Produce an object suitable for an ASN1OutputStream.
124      * <pre>
125      * SubjectPublicKeyInfo ::= SEQUENCE {
126      * algorithm AlgorithmIdentifier,
127      * publicKey BIT STRING }
128      * </pre>
129      */

130     public DERObject toASN1Object()
131     {
132         ASN1EncodableVector v = new ASN1EncodableVector();
133
134         v.add(algId);
135         v.add(keyData);
136
137         return new DERSequence(v);
138     }
139 }
140
Popular Tags