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.DERNull;
007: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
008: import org.bouncycastle.asn1.x509.DSAParameter;
009: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
010: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
011: import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
012:
013: import java.io.IOException;
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.math.BigInteger;
017: import java.security.interfaces.DSAParams;
018: import java.security.interfaces.DSAPublicKey;
019: import java.security.spec.DSAParameterSpec;
020: import java.security.spec.DSAPublicKeySpec;
021:
022: public class JDKDSAPublicKey implements DSAPublicKey {
023: private static final long serialVersionUID = 1752452449903495175L;
024:
025: private BigInteger y;
026: private DSAParams dsaSpec;
027:
028: JDKDSAPublicKey(DSAPublicKeySpec spec) {
029: this .y = spec.getY();
030: this .dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(),
031: spec.getG());
032: }
033:
034: JDKDSAPublicKey(DSAPublicKey key) {
035: this .y = key.getY();
036: this .dsaSpec = key.getParams();
037: }
038:
039: JDKDSAPublicKey(DSAPublicKeyParameters params) {
040: this .y = params.getY();
041: this .dsaSpec = new DSAParameterSpec(params.getParameters()
042: .getP(), params.getParameters().getQ(), params
043: .getParameters().getG());
044: }
045:
046: JDKDSAPublicKey(BigInteger y, DSAParameterSpec dsaSpec) {
047: this .y = y;
048: this .dsaSpec = dsaSpec;
049: }
050:
051: JDKDSAPublicKey(SubjectPublicKeyInfo info) {
052:
053: DERInteger derY;
054:
055: try {
056: derY = (DERInteger) info.getPublicKey();
057: } catch (IOException e) {
058: throw new IllegalArgumentException(
059: "invalid info structure in DSA public key");
060: }
061:
062: this .y = derY.getValue();
063:
064: if (isNotNull(info.getAlgorithmId().getParameters())) {
065: DSAParameter params = new DSAParameter((ASN1Sequence) info
066: .getAlgorithmId().getParameters());
067:
068: this .dsaSpec = new DSAParameterSpec(params.getP(), params
069: .getQ(), params.getG());
070: }
071: }
072:
073: private boolean isNotNull(DEREncodable parameters) {
074: return parameters != null
075: && !DERNull.INSTANCE.equals(parameters);
076: }
077:
078: public String getAlgorithm() {
079: return "DSA";
080: }
081:
082: public String getFormat() {
083: return "X.509";
084: }
085:
086: public byte[] getEncoded() {
087: if (dsaSpec == null) {
088: return new SubjectPublicKeyInfo(new AlgorithmIdentifier(
089: X9ObjectIdentifiers.id_dsa), new DERInteger(y))
090: .getDEREncoded();
091: }
092:
093: return new SubjectPublicKeyInfo(new AlgorithmIdentifier(
094: X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec
095: .getP(), dsaSpec.getQ(), dsaSpec.getG())
096: .getDERObject()), new DERInteger(y))
097: .getDEREncoded();
098: }
099:
100: public DSAParams getParams() {
101: return dsaSpec;
102: }
103:
104: public BigInteger getY() {
105: return y;
106: }
107:
108: public String toString() {
109: StringBuffer buf = new StringBuffer();
110: String nl = System.getProperty("line.separator");
111:
112: buf.append("DSA Public Key").append(nl);
113: buf.append(" y: ").append(this .getY().toString(16))
114: .append(nl);
115:
116: return buf.toString();
117: }
118:
119: public int hashCode() {
120: return this .getY().hashCode()
121: ^ this .getParams().getG().hashCode()
122: ^ this .getParams().getP().hashCode()
123: ^ this .getParams().getQ().hashCode();
124: }
125:
126: public boolean equals(Object o) {
127: if (!(o instanceof DSAPublicKey)) {
128: return false;
129: }
130:
131: DSAPublicKey other = (DSAPublicKey) o;
132:
133: return this .getY().equals(other.getY())
134: && this .getParams().getG().equals(
135: other.getParams().getG())
136: && this .getParams().getP().equals(
137: other.getParams().getP())
138: && this .getParams().getQ().equals(
139: other.getParams().getQ());
140: }
141:
142: private void readObject(ObjectInputStream in) throws IOException,
143: ClassNotFoundException {
144: this .y = (BigInteger) in.readObject();
145: this .dsaSpec = new DSAParameterSpec((BigInteger) in
146: .readObject(), (BigInteger) in.readObject(),
147: (BigInteger) in.readObject());
148: }
149:
150: private void writeObject(ObjectOutputStream out) throws IOException {
151: out.writeObject(y);
152: out.writeObject(dsaSpec.getP());
153: out.writeObject(dsaSpec.getQ());
154: out.writeObject(dsaSpec.getG());
155: }
156: }
|