KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import org.apache.geronimo.util.asn1.ASN1Encodable;
24 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25 import org.apache.geronimo.util.asn1.ASN1Sequence;
26 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
27 import org.apache.geronimo.util.asn1.DERInteger;
28 import org.apache.geronimo.util.asn1.DERObject;
29 import org.apache.geronimo.util.asn1.DERSequence;
30
31 public class RSAPrivateKeyStructure
32     extends ASN1Encodable
33 {
34     private int version;
35     private BigInteger JavaDoc modulus;
36     private BigInteger JavaDoc publicExponent;
37     private BigInteger JavaDoc privateExponent;
38     private BigInteger JavaDoc prime1;
39     private BigInteger JavaDoc prime2;
40     private BigInteger JavaDoc exponent1;
41     private BigInteger JavaDoc exponent2;
42     private BigInteger JavaDoc coefficient;
43     private ASN1Sequence otherPrimeInfos = null;
44
45     public static RSAPrivateKeyStructure getInstance(
46         ASN1TaggedObject obj,
47         boolean explicit)
48     {
49         return getInstance(ASN1Sequence.getInstance(obj, explicit));
50     }
51
52     public static RSAPrivateKeyStructure getInstance(
53         Object JavaDoc obj)
54     {
55         if (obj instanceof RSAPrivateKeyStructure)
56         {
57             return (RSAPrivateKeyStructure)obj;
58         }
59         else if (obj instanceof ASN1Sequence)
60         {
61             return new RSAPrivateKeyStructure((ASN1Sequence)obj);
62         }
63
64         throw new IllegalArgumentException JavaDoc("unknown object in factory");
65     }
66
67     public RSAPrivateKeyStructure(
68         BigInteger JavaDoc modulus,
69         BigInteger JavaDoc publicExponent,
70         BigInteger JavaDoc privateExponent,
71         BigInteger JavaDoc prime1,
72         BigInteger JavaDoc prime2,
73         BigInteger JavaDoc exponent1,
74         BigInteger JavaDoc exponent2,
75         BigInteger JavaDoc coefficient)
76     {
77         this.version = 0;
78         this.modulus = modulus;
79         this.publicExponent = publicExponent;
80         this.privateExponent = privateExponent;
81         this.prime1 = prime1;
82         this.prime2 = prime2;
83         this.exponent1 = exponent1;
84         this.exponent2 = exponent2;
85         this.coefficient = coefficient;
86     }
87
88     public RSAPrivateKeyStructure(
89         ASN1Sequence seq)
90     {
91         Enumeration JavaDoc e = seq.getObjects();
92
93         BigInteger JavaDoc v = ((DERInteger)e.nextElement()).getValue();
94         if (v.intValue() != 0 && v.intValue() != 1)
95         {
96             throw new IllegalArgumentException JavaDoc("wrong version for RSA private key");
97         }
98
99         version = v.intValue();
100         modulus = ((DERInteger)e.nextElement()).getValue();
101         publicExponent = ((DERInteger)e.nextElement()).getValue();
102         privateExponent = ((DERInteger)e.nextElement()).getValue();
103         prime1 = ((DERInteger)e.nextElement()).getValue();
104         prime2 = ((DERInteger)e.nextElement()).getValue();
105         exponent1 = ((DERInteger)e.nextElement()).getValue();
106         exponent2 = ((DERInteger)e.nextElement()).getValue();
107         coefficient = ((DERInteger)e.nextElement()).getValue();
108
109         if (e.hasMoreElements())
110         {
111             otherPrimeInfos = (ASN1Sequence)e.nextElement();
112         }
113     }
114
115     public int getVersion()
116     {
117         return version;
118     }
119
120     public BigInteger JavaDoc getModulus()
121     {
122         return modulus;
123     }
124
125     public BigInteger JavaDoc getPublicExponent()
126     {
127         return publicExponent;
128     }
129
130     public BigInteger JavaDoc getPrivateExponent()
131     {
132         return privateExponent;
133     }
134
135     public BigInteger JavaDoc getPrime1()
136     {
137         return prime1;
138     }
139
140     public BigInteger JavaDoc getPrime2()
141     {
142         return prime2;
143     }
144
145     public BigInteger JavaDoc getExponent1()
146     {
147         return exponent1;
148     }
149
150     public BigInteger JavaDoc getExponent2()
151     {
152         return exponent2;
153     }
154
155     public BigInteger JavaDoc getCoefficient()
156     {
157         return coefficient;
158     }
159
160     /**
161      * This outputs the key in PKCS1v2 format.
162      * <pre>
163      * RSAPrivateKey ::= SEQUENCE {
164      * version Version,
165      * modulus INTEGER, -- n
166      * publicExponent INTEGER, -- e
167      * privateExponent INTEGER, -- d
168      * prime1 INTEGER, -- p
169      * prime2 INTEGER, -- q
170      * exponent1 INTEGER, -- d mod (p-1)
171      * exponent2 INTEGER, -- d mod (q-1)
172      * coefficient INTEGER, -- (inverse of q) mod p
173      * otherPrimeInfos OtherPrimeInfos OPTIONAL
174      * }
175      *
176      * Version ::= INTEGER { two-prime(0), multi(1) }
177      * (CONSTRAINED BY {-- version must be multi if otherPrimeInfos present --})
178      * </pre>
179      * <p>
180      * This routine is written to output PKCS1 version 2.1, private keys.
181      */

182     public DERObject toASN1Object()
183     {
184         ASN1EncodableVector v = new ASN1EncodableVector();
185
186         v.add(new DERInteger(version)); // version
187
v.add(new DERInteger(getModulus()));
188         v.add(new DERInteger(getPublicExponent()));
189         v.add(new DERInteger(getPrivateExponent()));
190         v.add(new DERInteger(getPrime1()));
191         v.add(new DERInteger(getPrime2()));
192         v.add(new DERInteger(getExponent1()));
193         v.add(new DERInteger(getExponent2()));
194         v.add(new DERInteger(getCoefficient()));
195
196         if (otherPrimeInfos != null)
197         {
198             v.add(otherPrimeInfos);
199         }
200
201         return new DERSequence(v);
202     }
203 }
204
Popular Tags