Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import java.math.BigInteger;
      4 import java.util.Enumeration;
      5 
      6 import org.bouncycastle.asn1.ASN1EncodableVector;
      7 import org.bouncycastle.asn1.ASN1Integer;
      8 import org.bouncycastle.asn1.ASN1Object;
      9 import org.bouncycastle.asn1.ASN1Primitive;
     10 import org.bouncycastle.asn1.ASN1Sequence;
     11 import org.bouncycastle.asn1.ASN1TaggedObject;
     12 import org.bouncycastle.asn1.DERSequence;
     13 
     14 /**
     15  * @deprecated use org.bouncycastle.asn1.pkcs.RSAPublicKey
     16  */
     17 public class RSAPublicKeyStructure
     18     extends ASN1Object
     19 {
     20     private BigInteger  modulus;
     21     private BigInteger  publicExponent;
     22 
     23     public static RSAPublicKeyStructure getInstance(
     24         ASN1TaggedObject obj,
     25         boolean          explicit)
     26     {
     27         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     28     }
     29 
     30     public static RSAPublicKeyStructure getInstance(
     31         Object obj)
     32     {
     33         if(obj == null || obj instanceof RSAPublicKeyStructure)
     34         {
     35             return (RSAPublicKeyStructure)obj;
     36         }
     37 
     38         if(obj instanceof ASN1Sequence)
     39         {
     40             return new RSAPublicKeyStructure((ASN1Sequence)obj);
     41         }
     42 
     43         throw new IllegalArgumentException("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
     44     }
     45 
     46     public RSAPublicKeyStructure(
     47         BigInteger  modulus,
     48         BigInteger  publicExponent)
     49     {
     50         this.modulus = modulus;
     51         this.publicExponent = publicExponent;
     52     }
     53 
     54     public RSAPublicKeyStructure(
     55         ASN1Sequence  seq)
     56     {
     57         if (seq.size() != 2)
     58         {
     59             throw new IllegalArgumentException("Bad sequence size: "
     60                     + seq.size());
     61         }
     62 
     63         Enumeration e = seq.getObjects();
     64 
     65         modulus = ASN1Integer.getInstance(e.nextElement()).getPositiveValue();
     66         publicExponent = ASN1Integer.getInstance(e.nextElement()).getPositiveValue();
     67     }
     68 
     69     public BigInteger getModulus()
     70     {
     71         return modulus;
     72     }
     73 
     74     public BigInteger getPublicExponent()
     75     {
     76         return publicExponent;
     77     }
     78 
     79     /**
     80      * This outputs the key in PKCS1v2 format.
     81      * <pre>
     82      *      RSAPublicKey ::= SEQUENCE {
     83      *                          modulus INTEGER, -- n
     84      *                          publicExponent INTEGER, -- e
     85      *                      }
     86      * </pre>
     87      * <p>
     88      */
     89     public ASN1Primitive toASN1Primitive()
     90     {
     91         ASN1EncodableVector  v = new ASN1EncodableVector();
     92 
     93         v.add(new ASN1Integer(getModulus()));
     94         v.add(new ASN1Integer(getPublicExponent()));
     95 
     96         return new DERSequence(v);
     97     }
     98 }
     99