KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > pkcs > PrivateKeyInfo


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.pkcs;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.util.Enumeration JavaDoc;
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.ASN1OctetString;
29 import org.apache.geronimo.util.asn1.ASN1Sequence;
30 import org.apache.geronimo.util.asn1.ASN1Set;
31 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
32 import org.apache.geronimo.util.asn1.DERInteger;
33 import org.apache.geronimo.util.asn1.DERObject;
34 import org.apache.geronimo.util.asn1.DEROctetString;
35 import org.apache.geronimo.util.asn1.DERSequence;
36 import org.apache.geronimo.util.asn1.DERTaggedObject;
37 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
38
39 public class PrivateKeyInfo
40     extends ASN1Encodable
41 {
42     private DERObject privKey;
43     private AlgorithmIdentifier algId;
44     private ASN1Set attributes;
45
46     public static PrivateKeyInfo getInstance(
47         ASN1TaggedObject obj,
48         boolean explicit)
49     {
50         return getInstance(ASN1Sequence.getInstance(obj, explicit));
51     }
52
53     public static PrivateKeyInfo getInstance(
54         Object JavaDoc obj)
55     {
56         if (obj instanceof PrivateKeyInfo)
57         {
58             return (PrivateKeyInfo)obj;
59         }
60         else if (obj instanceof ASN1Sequence)
61         {
62             return new PrivateKeyInfo((ASN1Sequence)obj);
63         }
64
65         throw new IllegalArgumentException JavaDoc("unknown object in factory");
66     }
67
68     public PrivateKeyInfo(
69         AlgorithmIdentifier algId,
70         DERObject privateKey)
71     {
72         this.privKey = privateKey;
73         this.algId = algId;
74     }
75
76     public PrivateKeyInfo(
77         ASN1Sequence seq)
78     {
79         Enumeration JavaDoc e = seq.getObjects();
80
81         BigInteger JavaDoc version = ((DERInteger)e.nextElement()).getValue();
82         if (version.intValue() != 0)
83         {
84             throw new IllegalArgumentException JavaDoc("wrong version for private key info");
85         }
86
87         algId = new AlgorithmIdentifier((ASN1Sequence)e.nextElement());
88
89         try
90         {
91             ByteArrayInputStream JavaDoc bIn = new ByteArrayInputStream JavaDoc(((ASN1OctetString)e.nextElement()).getOctets());
92             ASN1InputStream aIn = new ASN1InputStream(bIn);
93
94             privKey = aIn.readObject();
95         }
96         catch (IOException JavaDoc ex)
97         {
98             throw new IllegalArgumentException JavaDoc("Error recoverying private key from sequence");
99         }
100
101         if (e.hasMoreElements())
102         {
103            attributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false);
104         }
105     }
106
107     public AlgorithmIdentifier getAlgorithmId()
108     {
109         return algId;
110     }
111
112     public DERObject getPrivateKey()
113     {
114         return privKey;
115     }
116
117     public ASN1Set getAttributes()
118     {
119         return attributes;
120     }
121
122     /**
123      * write out an RSA private key with it's asscociated information
124      * as described in PKCS8.
125      * <pre>
126      * PrivateKeyInfo ::= SEQUENCE {
127      * version Version,
128      * privateKeyAlgorithm AlgorithmIdentifier {{PrivateKeyAlgorithms}},
129      * privateKey PrivateKey,
130      * attributes [0] IMPLICIT Attributes OPTIONAL
131      * }
132      * Version ::= INTEGER {v1(0)} (v1,...)
133      *
134      * PrivateKey ::= OCTET STRING
135      *
136      * Attributes ::= SET OF Attribute
137      * </pre>
138      */

139     public DERObject toASN1Object()
140     {
141         ASN1EncodableVector v = new ASN1EncodableVector();
142
143         v.add(new DERInteger(0));
144         v.add(algId);
145         v.add(new DEROctetString(privKey));
146
147         if (attributes != null)
148         {
149             v.add(new DERTaggedObject(false, 0, attributes));
150         }
151
152         return new DERSequence(v);
153     }
154 }
155
Popular Tags