001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1.pkcs;
017:
018: import java.math.BigInteger;
019: import java.util.Enumeration;
020:
021: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
022: import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
023: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
024: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
025: import org.apache.geronimo.crypto.asn1.DERInteger;
026: import org.apache.geronimo.crypto.asn1.DERObject;
027: import org.apache.geronimo.crypto.asn1.DERSequence;
028:
029: public class RSAPrivateKeyStructure extends ASN1Encodable {
030: private int version;
031: private BigInteger modulus;
032: private BigInteger publicExponent;
033: private BigInteger privateExponent;
034: private BigInteger prime1;
035: private BigInteger prime2;
036: private BigInteger exponent1;
037: private BigInteger exponent2;
038: private BigInteger coefficient;
039: private ASN1Sequence otherPrimeInfos = null;
040:
041: public static RSAPrivateKeyStructure getInstance(
042: ASN1TaggedObject obj, boolean explicit) {
043: return getInstance(ASN1Sequence.getInstance(obj, explicit));
044: }
045:
046: public static RSAPrivateKeyStructure getInstance(Object obj) {
047: if (obj instanceof RSAPrivateKeyStructure) {
048: return (RSAPrivateKeyStructure) obj;
049: } else if (obj instanceof ASN1Sequence) {
050: return new RSAPrivateKeyStructure((ASN1Sequence) obj);
051: }
052:
053: throw new IllegalArgumentException("unknown object in factory");
054: }
055:
056: public RSAPrivateKeyStructure(BigInteger modulus,
057: BigInteger publicExponent, BigInteger privateExponent,
058: BigInteger prime1, BigInteger prime2, BigInteger exponent1,
059: BigInteger exponent2, BigInteger coefficient) {
060: this .version = 0;
061: this .modulus = modulus;
062: this .publicExponent = publicExponent;
063: this .privateExponent = privateExponent;
064: this .prime1 = prime1;
065: this .prime2 = prime2;
066: this .exponent1 = exponent1;
067: this .exponent2 = exponent2;
068: this .coefficient = coefficient;
069: }
070:
071: public RSAPrivateKeyStructure(ASN1Sequence seq) {
072: Enumeration e = seq.getObjects();
073:
074: BigInteger v = ((DERInteger) e.nextElement()).getValue();
075: if (v.intValue() != 0 && v.intValue() != 1) {
076: throw new IllegalArgumentException(
077: "wrong version for RSA private key");
078: }
079:
080: version = v.intValue();
081: modulus = ((DERInteger) e.nextElement()).getValue();
082: publicExponent = ((DERInteger) e.nextElement()).getValue();
083: privateExponent = ((DERInteger) e.nextElement()).getValue();
084: prime1 = ((DERInteger) e.nextElement()).getValue();
085: prime2 = ((DERInteger) e.nextElement()).getValue();
086: exponent1 = ((DERInteger) e.nextElement()).getValue();
087: exponent2 = ((DERInteger) e.nextElement()).getValue();
088: coefficient = ((DERInteger) e.nextElement()).getValue();
089:
090: if (e.hasMoreElements()) {
091: otherPrimeInfos = (ASN1Sequence) e.nextElement();
092: }
093: }
094:
095: public int getVersion() {
096: return version;
097: }
098:
099: public BigInteger getModulus() {
100: return modulus;
101: }
102:
103: public BigInteger getPublicExponent() {
104: return publicExponent;
105: }
106:
107: public BigInteger getPrivateExponent() {
108: return privateExponent;
109: }
110:
111: public BigInteger getPrime1() {
112: return prime1;
113: }
114:
115: public BigInteger getPrime2() {
116: return prime2;
117: }
118:
119: public BigInteger getExponent1() {
120: return exponent1;
121: }
122:
123: public BigInteger getExponent2() {
124: return exponent2;
125: }
126:
127: public BigInteger getCoefficient() {
128: return coefficient;
129: }
130:
131: /**
132: * This outputs the key in PKCS1v2 format.
133: * <pre>
134: * RSAPrivateKey ::= SEQUENCE {
135: * version Version,
136: * modulus INTEGER, -- n
137: * publicExponent INTEGER, -- e
138: * privateExponent INTEGER, -- d
139: * prime1 INTEGER, -- p
140: * prime2 INTEGER, -- q
141: * exponent1 INTEGER, -- d mod (p-1)
142: * exponent2 INTEGER, -- d mod (q-1)
143: * coefficient INTEGER, -- (inverse of q) mod p
144: * otherPrimeInfos OtherPrimeInfos OPTIONAL
145: * }
146: *
147: * Version ::= INTEGER { two-prime(0), multi(1) }
148: * (CONSTRAINED BY {-- version must be multi if otherPrimeInfos present --})
149: * </pre>
150: * <p>
151: * This routine is written to output PKCS1 version 2.1, private keys.
152: */
153: public DERObject toASN1Object() {
154: ASN1EncodableVector v = new ASN1EncodableVector();
155:
156: v.add(new DERInteger(version)); // version
157: v.add(new DERInteger(getModulus()));
158: v.add(new DERInteger(getPublicExponent()));
159: v.add(new DERInteger(getPrivateExponent()));
160: v.add(new DERInteger(getPrime1()));
161: v.add(new DERInteger(getPrime2()));
162: v.add(new DERInteger(getExponent1()));
163: v.add(new DERInteger(getExponent2()));
164: v.add(new DERInteger(getCoefficient()));
165:
166: if (otherPrimeInfos != null) {
167: v.add(otherPrimeInfos);
168: }
169:
170: return new DERSequence(v);
171: }
172: }
|