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