001: package org.bouncycastle.asn1.pkcs;
002:
003: import java.math.BigInteger;
004: import java.util.Enumeration;
005:
006: import org.bouncycastle.asn1.ASN1Encodable;
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.ASN1TaggedObject;
010: import org.bouncycastle.asn1.DERInteger;
011: import org.bouncycastle.asn1.DERObject;
012: import org.bouncycastle.asn1.DERSequence;
013:
014: public class RSAPrivateKeyStructure extends ASN1Encodable {
015: private int version;
016: private BigInteger modulus;
017: private BigInteger publicExponent;
018: private BigInteger privateExponent;
019: private BigInteger prime1;
020: private BigInteger prime2;
021: private BigInteger exponent1;
022: private BigInteger exponent2;
023: private BigInteger coefficient;
024: private ASN1Sequence otherPrimeInfos = null;
025:
026: public static RSAPrivateKeyStructure getInstance(
027: ASN1TaggedObject obj, boolean explicit) {
028: return getInstance(ASN1Sequence.getInstance(obj, explicit));
029: }
030:
031: public static RSAPrivateKeyStructure getInstance(Object obj) {
032: if (obj instanceof RSAPrivateKeyStructure) {
033: return (RSAPrivateKeyStructure) obj;
034: } else if (obj instanceof ASN1Sequence) {
035: return new RSAPrivateKeyStructure((ASN1Sequence) obj);
036: }
037:
038: throw new IllegalArgumentException("unknown object in factory");
039: }
040:
041: public RSAPrivateKeyStructure(BigInteger modulus,
042: BigInteger publicExponent, BigInteger privateExponent,
043: BigInteger prime1, BigInteger prime2, BigInteger exponent1,
044: BigInteger exponent2, BigInteger coefficient) {
045: this .version = 0;
046: this .modulus = modulus;
047: this .publicExponent = publicExponent;
048: this .privateExponent = privateExponent;
049: this .prime1 = prime1;
050: this .prime2 = prime2;
051: this .exponent1 = exponent1;
052: this .exponent2 = exponent2;
053: this .coefficient = coefficient;
054: }
055:
056: public RSAPrivateKeyStructure(ASN1Sequence seq) {
057: Enumeration e = seq.getObjects();
058:
059: BigInteger v = ((DERInteger) e.nextElement()).getValue();
060: if (v.intValue() != 0 && v.intValue() != 1) {
061: throw new IllegalArgumentException(
062: "wrong version for RSA private key");
063: }
064:
065: version = v.intValue();
066: modulus = ((DERInteger) e.nextElement()).getValue();
067: publicExponent = ((DERInteger) e.nextElement()).getValue();
068: privateExponent = ((DERInteger) e.nextElement()).getValue();
069: prime1 = ((DERInteger) e.nextElement()).getValue();
070: prime2 = ((DERInteger) e.nextElement()).getValue();
071: exponent1 = ((DERInteger) e.nextElement()).getValue();
072: exponent2 = ((DERInteger) e.nextElement()).getValue();
073: coefficient = ((DERInteger) e.nextElement()).getValue();
074:
075: if (e.hasMoreElements()) {
076: otherPrimeInfos = (ASN1Sequence) e.nextElement();
077: }
078: }
079:
080: public int getVersion() {
081: return version;
082: }
083:
084: public BigInteger getModulus() {
085: return modulus;
086: }
087:
088: public BigInteger getPublicExponent() {
089: return publicExponent;
090: }
091:
092: public BigInteger getPrivateExponent() {
093: return privateExponent;
094: }
095:
096: public BigInteger getPrime1() {
097: return prime1;
098: }
099:
100: public BigInteger getPrime2() {
101: return prime2;
102: }
103:
104: public BigInteger getExponent1() {
105: return exponent1;
106: }
107:
108: public BigInteger getExponent2() {
109: return exponent2;
110: }
111:
112: public BigInteger getCoefficient() {
113: return coefficient;
114: }
115:
116: /**
117: * This outputs the key in PKCS1v2 format.
118: * <pre>
119: * RSAPrivateKey ::= SEQUENCE {
120: * version Version,
121: * modulus INTEGER, -- n
122: * publicExponent INTEGER, -- e
123: * privateExponent INTEGER, -- d
124: * prime1 INTEGER, -- p
125: * prime2 INTEGER, -- q
126: * exponent1 INTEGER, -- d mod (p-1)
127: * exponent2 INTEGER, -- d mod (q-1)
128: * coefficient INTEGER, -- (inverse of q) mod p
129: * otherPrimeInfos OtherPrimeInfos OPTIONAL
130: * }
131: *
132: * Version ::= INTEGER { two-prime(0), multi(1) }
133: * (CONSTRAINED BY {-- version must be multi if otherPrimeInfos present --})
134: * </pre>
135: * <p>
136: * This routine is written to output PKCS1 version 2.1, private keys.
137: */
138: public DERObject toASN1Object() {
139: ASN1EncodableVector v = new ASN1EncodableVector();
140:
141: v.add(new DERInteger(version)); // version
142: v.add(new DERInteger(getModulus()));
143: v.add(new DERInteger(getPublicExponent()));
144: v.add(new DERInteger(getPrivateExponent()));
145: v.add(new DERInteger(getPrime1()));
146: v.add(new DERInteger(getPrime2()));
147: v.add(new DERInteger(getExponent1()));
148: v.add(new DERInteger(getExponent2()));
149: v.add(new DERInteger(getCoefficient()));
150:
151: if (otherPrimeInfos != null) {
152: v.add(otherPrimeInfos);
153: }
154:
155: return new DERSequence(v);
156: }
157: }
|