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