1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.jce.provider;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ObjectInputStream;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ObjectOutputStream;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.math.BigInteger;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.interfaces.RSAPrivateKey;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.spec.RSAPrivateKeySpec;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Enumeration;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Encodable;
124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.DERNull;
144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
154c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.x509.AlgorithmIdentifier;
164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.crypto.params.RSAKeyParameters;
174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
194c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class JCERSAPrivateKey
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements RSAPrivateKey, PKCS12BagAttributeCarrier
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
24c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static final long serialVersionUID = 5110188922551353628L;
25c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
26c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static BigInteger ZERO = BigInteger.valueOf(0);
27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    protected BigInteger modulus;
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    protected BigInteger privateExponent;
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
314c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    protected JCERSAPrivateKey()
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    JCERSAPrivateKey(
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        RSAKeyParameters key)
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.modulus = key.getModulus();
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.privateExponent = key.getExponent();
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    JCERSAPrivateKey(
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        RSAPrivateKeySpec spec)
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.modulus = spec.getModulus();
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.privateExponent = spec.getPrivateExponent();
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    JCERSAPrivateKey(
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        RSAPrivateKey key)
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.modulus = key.getModulus();
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.privateExponent = key.getPrivateExponent();
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getModulus()
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return modulus;
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getPrivateExponent()
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return privateExponent;
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getAlgorithm()
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return "RSA";
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getFormat()
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return "PKCS#8";
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getEncoded()
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
804c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPrivateKey(getModulus(), ZERO, getPrivateExponent(), ZERO, ZERO, ZERO, ZERO, ZERO));
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public boolean equals(Object o)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(o instanceof RSAPrivateKey))
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (o == this)
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return true;
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        RSAPrivateKey key = (RSAPrivateKey)o;
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getModulus().equals(key.getModulus())
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            && getPrivateExponent().equals(key.getPrivateExponent());
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getModulus().hashCode() ^ getPrivateExponent().hashCode();
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public void setBagAttribute(
1074c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1ObjectIdentifier oid,
1084c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Encodable        attribute)
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        attrCarrier.setBagAttribute(oid, attribute);
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Encodable getBagAttribute(
11470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        ASN1ObjectIdentifier oid)
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return attrCarrier.getBagAttribute(oid);
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Enumeration getBagAttributeKeys()
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
121c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return attrCarrier.getBagAttributeKeys();
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private void readObject(
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ObjectInputStream   in)
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException, ClassNotFoundException
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.modulus = (BigInteger)in.readObject();
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        attrCarrier.readObject(in);
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.privateExponent = (BigInteger)in.readObject();
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private void writeObject(
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ObjectOutputStream  out)
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        out.writeObject(modulus);
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
142c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        attrCarrier.writeObject(out);
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        out.writeObject(privateExponent);
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
147