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.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 RSAPublicKeyStructure
     15     extends ASN1Encodable
     16 {
     17     private BigInteger  modulus;
     18     private BigInteger  publicExponent;
     19 
     20     public static RSAPublicKeyStructure getInstance(
     21         ASN1TaggedObject obj,
     22         boolean          explicit)
     23     {
     24         return getInstance(ASN1Sequence.getInstance(obj, explicit));
     25     }
     26 
     27     public static RSAPublicKeyStructure getInstance(
     28         Object obj)
     29     {
     30         if(obj == null || obj instanceof RSAPublicKeyStructure)
     31         {
     32             return (RSAPublicKeyStructure)obj;
     33         }
     34 
     35         if(obj instanceof ASN1Sequence)
     36         {
     37             return new RSAPublicKeyStructure((ASN1Sequence)obj);
     38         }
     39 
     40         throw new IllegalArgumentException("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
     41     }
     42 
     43     public RSAPublicKeyStructure(
     44         BigInteger  modulus,
     45         BigInteger  publicExponent)
     46     {
     47         this.modulus = modulus;
     48         this.publicExponent = publicExponent;
     49     }
     50 
     51     public RSAPublicKeyStructure(
     52         ASN1Sequence  seq)
     53     {
     54         if (seq.size() != 2)
     55         {
     56             throw new IllegalArgumentException("Bad sequence size: "
     57                     + seq.size());
     58         }
     59 
     60         Enumeration e = seq.getObjects();
     61 
     62         modulus = DERInteger.getInstance(e.nextElement()).getPositiveValue();
     63         publicExponent = DERInteger.getInstance(e.nextElement()).getPositiveValue();
     64     }
     65 
     66     public BigInteger getModulus()
     67     {
     68         return modulus;
     69     }
     70 
     71     public BigInteger getPublicExponent()
     72     {
     73         return publicExponent;
     74     }
     75 
     76     /**
     77      * This outputs the key in PKCS1v2 format.
     78      * <pre>
     79      *      RSAPublicKey ::= SEQUENCE {
     80      *                          modulus INTEGER, -- n
     81      *                          publicExponent INTEGER, -- e
     82      *                      }
     83      * </pre>
     84      * <p>
     85      */
     86     public DERObject toASN1Object()
     87     {
     88         ASN1EncodableVector  v = new ASN1EncodableVector();
     89 
     90         v.add(new DERInteger(getModulus()));
     91         v.add(new DERInteger(getPublicExponent()));
     92 
     93         return new DERSequence(v);
     94     }
     95 }
     96