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