1package org.bouncycastle.asn1.x509;
2
3import java.math.BigInteger;
4import java.util.Enumeration;
5
6import org.bouncycastle.asn1.ASN1EncodableVector;
7import org.bouncycastle.asn1.ASN1Integer;
8import org.bouncycastle.asn1.ASN1Object;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.ASN1TaggedObject;
12import org.bouncycastle.asn1.DERSequence;
13
14/**
15 * @deprecated use org.bouncycastle.asn1.pkcs.RSAPublicKey
16 */
17public 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