001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DEREncodable;
005: import org.bouncycastle.asn1.DERInteger;
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
008: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
009: import org.bouncycastle.asn1.x509.DSAParameter;
010: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
011: import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
012: import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
013:
014: import java.io.IOException;
015: import java.io.ObjectInputStream;
016: import java.io.ObjectOutputStream;
017: import java.math.BigInteger;
018: import java.security.interfaces.DSAParams;
019: import java.security.interfaces.DSAPrivateKey;
020: import java.security.spec.DSAParameterSpec;
021: import java.security.spec.DSAPrivateKeySpec;
022: import java.util.Enumeration;
023:
024: public class JDKDSAPrivateKey implements DSAPrivateKey,
025: PKCS12BagAttributeCarrier {
026: private static final long serialVersionUID = -4677259546958385734L;
027:
028: BigInteger x;
029: DSAParams dsaSpec;
030:
031: private PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
032:
033: protected JDKDSAPrivateKey() {
034: }
035:
036: JDKDSAPrivateKey(DSAPrivateKey key) {
037: this .x = key.getX();
038: this .dsaSpec = key.getParams();
039: }
040:
041: JDKDSAPrivateKey(DSAPrivateKeySpec spec) {
042: this .x = spec.getX();
043: this .dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(),
044: spec.getG());
045: }
046:
047: JDKDSAPrivateKey(PrivateKeyInfo info) {
048: DSAParameter params = new DSAParameter((ASN1Sequence) info
049: .getAlgorithmId().getParameters());
050: DERInteger derX = (DERInteger) info.getPrivateKey();
051:
052: this .x = derX.getValue();
053: this .dsaSpec = new DSAParameterSpec(params.getP(), params
054: .getQ(), params.getG());
055: }
056:
057: JDKDSAPrivateKey(DSAPrivateKeyParameters params) {
058: this .x = params.getX();
059: this .dsaSpec = new DSAParameterSpec(params.getParameters()
060: .getP(), params.getParameters().getQ(), params
061: .getParameters().getG());
062: }
063:
064: public String getAlgorithm() {
065: return "DSA";
066: }
067:
068: /**
069: * return the encoding format we produce in getEncoded().
070: *
071: * @return the string "PKCS#8"
072: */
073: public String getFormat() {
074: return "PKCS#8";
075: }
076:
077: /**
078: * Return a PKCS8 representation of the key. The sequence returned
079: * represents a full PrivateKeyInfo object.
080: *
081: * @return a PKCS8 representation of the key.
082: */
083: public byte[] getEncoded() {
084: PrivateKeyInfo info = new PrivateKeyInfo(
085: new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
086: new DSAParameter(dsaSpec.getP(),
087: dsaSpec.getQ(), dsaSpec.getG())
088: .getDERObject()),
089: new DERInteger(getX()));
090:
091: return info.getDEREncoded();
092: }
093:
094: public DSAParams getParams() {
095: return dsaSpec;
096: }
097:
098: public BigInteger getX() {
099: return x;
100: }
101:
102: public boolean equals(Object o) {
103: if (!(o instanceof DSAPrivateKey)) {
104: return false;
105: }
106:
107: DSAPrivateKey other = (DSAPrivateKey) o;
108:
109: return this .getX().equals(other.getX())
110: && this .getParams().getG().equals(
111: other.getParams().getG())
112: && this .getParams().getP().equals(
113: other.getParams().getP())
114: && this .getParams().getQ().equals(
115: other.getParams().getQ());
116: }
117:
118: public int hashCode() {
119: return this .getX().hashCode()
120: ^ this .getParams().getG().hashCode()
121: ^ this .getParams().getP().hashCode()
122: ^ this .getParams().getQ().hashCode();
123: }
124:
125: public void setBagAttribute(DERObjectIdentifier oid,
126: DEREncodable attribute) {
127: attrCarrier.setBagAttribute(oid, attribute);
128: }
129:
130: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
131: return attrCarrier.getBagAttribute(oid);
132: }
133:
134: public Enumeration getBagAttributeKeys() {
135: return attrCarrier.getBagAttributeKeys();
136: }
137:
138: private void readObject(ObjectInputStream in) throws IOException,
139: ClassNotFoundException {
140: this .x = (BigInteger) in.readObject();
141: this .dsaSpec = new DSAParameterSpec((BigInteger) in
142: .readObject(), (BigInteger) in.readObject(),
143: (BigInteger) in.readObject());
144: this .attrCarrier = new PKCS12BagAttributeCarrierImpl();
145:
146: attrCarrier.readObject(in);
147: }
148:
149: private void writeObject(ObjectOutputStream out) throws IOException {
150: out.writeObject(x);
151: out.writeObject(dsaSpec.getP());
152: out.writeObject(dsaSpec.getQ());
153: out.writeObject(dsaSpec.getG());
154:
155: attrCarrier.writeObject(out);
156: }
157: }
|