View Javadoc

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