KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import com.maverick.crypto.asn1.ASN1EncodableVector;
27 import com.maverick.crypto.asn1.ASN1Sequence;
28 import com.maverick.crypto.asn1.ASN1TaggedObject;
29 import com.maverick.crypto.asn1.DERBitString;
30 import com.maverick.crypto.asn1.DEREncodable;
31 import com.maverick.crypto.asn1.DERInputStream;
32 import com.maverick.crypto.asn1.DERObject;
33 import com.maverick.crypto.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     implements DEREncodable
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 JavaDoc 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 JavaDoc("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 JavaDoc 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 JavaDoc
108     {
109         ByteArrayInputStream JavaDoc bIn = new ByteArrayInputStream JavaDoc(keyData.getBytes());
110         DERInputStream dIn = new DERInputStream(bIn);
111
112         return dIn.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 getDERObject()
132     {
133         ASN1EncodableVector v = new ASN1EncodableVector();
134
135         v.add(algId);
136         v.add(keyData);
137
138         return new DERSequence(v);
139     }
140 }
141
Popular Tags