JDKDSAPrivateKey.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.jce.provider;
2
3import java.io.IOException;
4import java.io.ObjectInputStream;
5import java.io.ObjectOutputStream;
6import java.math.BigInteger;
7import java.security.interfaces.DSAParams;
8import java.security.interfaces.DSAPrivateKey;
9import java.security.spec.DSAParameterSpec;
10import java.security.spec.DSAPrivateKeySpec;
11import java.util.Enumeration;
12
13import org.bouncycastle.asn1.ASN1Encodable;
14import org.bouncycastle.asn1.ASN1Encoding;
15import org.bouncycastle.asn1.ASN1Integer;
16import org.bouncycastle.asn1.ASN1ObjectIdentifier;
17import org.bouncycastle.asn1.ASN1Sequence;
18import org.bouncycastle.asn1.DERInteger;
19import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
20import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
21import org.bouncycastle.asn1.x509.DSAParameter;
22import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
23import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
24import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
25import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
26
27public class JDKDSAPrivateKey
28    implements DSAPrivateKey, PKCS12BagAttributeCarrier
29{
30    private static final long serialVersionUID = -4677259546958385734L;
31
32    BigInteger          x;
33    DSAParams           dsaSpec;
34
35    private PKCS12BagAttributeCarrierImpl   attrCarrier = new PKCS12BagAttributeCarrierImpl();
36
37    protected JDKDSAPrivateKey()
38    {
39    }
40
41    JDKDSAPrivateKey(
42        DSAPrivateKey    key)
43    {
44        this.x = key.getX();
45        this.dsaSpec = key.getParams();
46    }
47
48    JDKDSAPrivateKey(
49        DSAPrivateKeySpec    spec)
50    {
51        this.x = spec.getX();
52        this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());
53    }
54
55    JDKDSAPrivateKey(
56        PrivateKeyInfo  info)
57        throws IOException
58    {
59        DSAParameter    params = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
60        DERInteger      derX = ASN1Integer.getInstance(info.parsePrivateKey());
61
62        this.x = derX.getValue();
63        this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
64    }
65
66    JDKDSAPrivateKey(
67        DSAPrivateKeyParameters  params)
68    {
69        this.x = params.getX();
70        this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
71    }
72
73    public String getAlgorithm()
74    {
75        return "DSA";
76    }
77
78    /**
79     * return the encoding format we produce in getEncoded().
80     *
81     * @return the string "PKCS#8"
82     */
83    public String getFormat()
84    {
85        return "PKCS#8";
86    }
87
88    /**
89     * Return a PKCS8 representation of the key. The sequence returned
90     * represents a full PrivateKeyInfo object.
91     *
92     * @return a PKCS8 representation of the key.
93     */
94    public byte[] getEncoded()
95    {
96        try
97        {
98            PrivateKeyInfo          info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG())), new DERInteger(getX()));
99
100            return info.getEncoded(ASN1Encoding.DER);
101        }
102        catch (IOException e)
103        {
104            return null;
105        }
106    }
107
108    public DSAParams getParams()
109    {
110        return dsaSpec;
111    }
112
113    public BigInteger getX()
114    {
115        return x;
116    }
117
118    public boolean equals(
119        Object o)
120    {
121        if (!(o instanceof DSAPrivateKey))
122        {
123            return false;
124        }
125
126        DSAPrivateKey other = (DSAPrivateKey)o;
127
128        return this.getX().equals(other.getX())
129            && this.getParams().getG().equals(other.getParams().getG())
130            && this.getParams().getP().equals(other.getParams().getP())
131            && this.getParams().getQ().equals(other.getParams().getQ());
132    }
133
134    public int hashCode()
135    {
136        return this.getX().hashCode() ^ this.getParams().getG().hashCode()
137                ^ this.getParams().getP().hashCode() ^ this.getParams().getQ().hashCode();
138    }
139
140    public void setBagAttribute(
141        ASN1ObjectIdentifier oid,
142        ASN1Encodable attribute)
143    {
144        attrCarrier.setBagAttribute(oid, attribute);
145    }
146
147    public ASN1Encodable getBagAttribute(
148        ASN1ObjectIdentifier oid)
149    {
150        return attrCarrier.getBagAttribute(oid);
151    }
152
153    public Enumeration getBagAttributeKeys()
154    {
155        return attrCarrier.getBagAttributeKeys();
156    }
157
158    private void readObject(
159        ObjectInputStream in)
160        throws IOException, ClassNotFoundException
161    {
162        this.x = (BigInteger)in.readObject();
163        this.dsaSpec = new DSAParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), (BigInteger)in.readObject());
164        this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
165
166        attrCarrier.readObject(in);
167    }
168
169    private void writeObject(
170        ObjectOutputStream out)
171        throws IOException
172    {
173        out.writeObject(x);
174        out.writeObject(dsaSpec.getP());
175        out.writeObject(dsaSpec.getQ());
176        out.writeObject(dsaSpec.getG());
177
178        attrCarrier.writeObject(out);
179    }
180}
181