Skip to content

Commit 2e2b423

Browse files
committed
[fix] be explicit when reading DSA from DER
1 parent aa51ade commit 2e2b423

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

  • src/main/java/org/jruby/ext/openssl/impl

src/main/java/org/jruby/ext/openssl/impl/PKey.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@
6161
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
6262
import org.bouncycastle.asn1.ASN1Primitive;
6363
import org.bouncycastle.asn1.ASN1Sequence;
64+
import org.bouncycastle.asn1.DERBitString;
65+
import org.bouncycastle.asn1.DERInteger;
6466
import org.bouncycastle.asn1.DERNull;
6567
import org.bouncycastle.asn1.DERSequence;
6668
import org.bouncycastle.asn1.DLSequence;
6769
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
6870
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
6971
import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
7072
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
73+
import org.bouncycastle.asn1.x509.DSAParameter;
7174
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
7275
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
7376
import org.bouncycastle.jce.ECNamedCurveTable;
@@ -328,7 +331,15 @@ public static byte[] toDerRSAKey(RSAPublicKey pubKey, RSAPrivateCrtKey privKey)
328331

329332
public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) throws IOException {
330333
if ( pubKey != null && privKey == null ) {
331-
return pubKey.getEncoded();
334+
// pubKey.getEncoded() :
335+
final DSAParams params = pubKey.getParams();
336+
if (params == null) {
337+
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa),
338+
new ASN1Integer(pubKey.getY())).getEncoded(ASN1Encoding.DER);
339+
}
340+
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
341+
new DSAParameter(params.getP(), params.getQ(), params.getG())), new ASN1Integer(pubKey.getY())
342+
).getEncoded(ASN1Encoding.DER);
332343
}
333344
if ( privKey != null && pubKey != null ) {
334345
ASN1EncodableVector vec = new ASN1EncodableVector();
@@ -339,12 +350,15 @@ public static byte[] toDerDSAKey(DSAPublicKey pubKey, DSAPrivateKey privKey) thr
339350
vec.add(new ASN1Integer(params.getG()));
340351
vec.add(new ASN1Integer(pubKey.getY()));
341352
vec.add(new ASN1Integer(privKey.getX()));
342-
return new DLSequence(vec).getEncoded();
353+
return new DERSequence(vec).toASN1Primitive().getEncoded(ASN1Encoding.DER);
343354
}
344355
if ( privKey == null ) {
345356
throw new IllegalArgumentException("private key as well as public key are null");
346357
}
347-
return privKey.getEncoded();
358+
final DSAParams params = privKey.getParams();
359+
return new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
360+
new DSAParameter(params.getP(), params.getQ(), params.getG())), new ASN1Integer(privKey.getX())
361+
).getEncoded(ASN1Encoding.DER);
348362
}
349363

350364
public static byte[] toDerDHKey(BigInteger p, BigInteger g) throws IOException {

0 commit comments

Comments
 (0)