Skip to content

Commit 8e81ebb

Browse files
committed
[refactor] make sure curveName is set when using PKey.read
1 parent 890d3ab commit 8e81ebb

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
132132
}
133133
if ( "EC".equals(alg) ) {
134134
return new PKeyEC(runtime, _PKey(runtime).getClass("EC"),
135-
(PrivateKey) keyPair.getPrivate(), (PublicKey) keyPair.getPublic()
135+
keyPair.getPrivate(), keyPair.getPublic()
136136
);
137137
}
138138
}

src/main/java/org/jruby/ext/openssl/PKeyEC.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.security.PublicKey;
2222

2323
import java.security.SecureRandom;
24+
import java.security.SignatureException;
2425
import java.security.interfaces.ECPrivateKey;
2526
import java.security.interfaces.ECPublicKey;
2627
import java.security.spec.ECGenParameterSpec;
@@ -69,11 +70,11 @@
6970
import org.jruby.ext.openssl.impl.CipherSpec;
7071
import static org.jruby.ext.openssl.OpenSSL.debug;
7172
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
72-
import static org.jruby.ext.openssl.PKey._PKey;
7373
import org.jruby.ext.openssl.impl.ECPrivateKeyWithName;
7474
import static org.jruby.ext.openssl.impl.PKey.readECPrivateKey;
7575
import org.jruby.ext.openssl.util.ByteArrayOutputStream;
7676
import org.jruby.ext.openssl.x509store.PEMInputOutput;
77+
import org.jruby.util.ByteList;
7778

7879
/**
7980
* OpenSSL::PKey::EC implementation.
@@ -200,8 +201,12 @@ public PKeyEC(Ruby runtime, RubyClass type) {
200201

201202
PKeyEC(Ruby runtime, RubyClass type, PrivateKey privKey, PublicKey pubKey) {
202203
super(runtime, type);
203-
this.privateKey = privKey;
204204
this.publicKey = (ECPublicKey) pubKey;
205+
if (privKey instanceof ECPrivateKey) {
206+
setPrivateKey((ECPrivateKey) privKey);
207+
} else {
208+
this.privateKey = privKey;
209+
}
205210
}
206211

207212
private transient Group group;
@@ -213,9 +218,10 @@ public PKeyEC(Ruby runtime, RubyClass type) {
213218

214219
private String getCurveName() { return curveName; }
215220

216-
// private ECNamedCurveParameterSpec getParameterSpec() {
217-
// return ECNamedCurveTable.getParameterSpec( getCurveName() );
218-
// }
221+
private ECNamedCurveParameterSpec getParameterSpec() {
222+
assert curveName != null;
223+
return ECNamedCurveTable.getParameterSpec(getCurveName());
224+
}
219225

220226
@Override
221227
public PublicKey getPublicKey() { return publicKey; }
@@ -342,12 +348,10 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
342348
throw newECError(runtime, "Neither PUB key nor PRIV key: (invalid key type " + privKey.getClass().getName() + ")");
343349
}
344350
this.publicKey = (ECPublicKey) pubKey;
345-
this.privateKey = (ECPrivateKey) privKey;
346-
unwrapPrivateKeyWithName();
351+
setPrivateKey((ECPrivateKey) privKey);
347352
}
348353
else if ( key instanceof ECPrivateKey ) {
349-
this.privateKey = (ECPrivateKey) key;
350-
unwrapPrivateKeyWithName();
354+
setPrivateKey((ECPrivateKey) key);
351355
}
352356
else if ( key instanceof ECPublicKey ) {
353357
this.publicKey = (ECPublicKey) key; this.privateKey = null;
@@ -359,11 +363,15 @@ else if ( key instanceof ECPublicKey ) {
359363
if ( publicKey != null ) {
360364
publicKey.getParams().getCurve();
361365
}
362-
// TODO set curveName ?!?!?!?!?!?!?!
363366

364367
return this;
365368
}
366369

370+
void setPrivateKey(final ECPrivateKey key) {
371+
this.privateKey = key;
372+
unwrapPrivateKeyWithName();
373+
}
374+
367375
private void unwrapPrivateKeyWithName() {
368376
final ECPrivateKey privKey = (ECPrivateKey) this.privateKey;
369377
if ( privKey instanceof ECPrivateKeyWithName ) {
@@ -402,7 +410,7 @@ public PKeyEC generate_key(final ThreadContext context) {
402410
@JRubyMethod(name = "dsa_sign_asn1")
403411
public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject data) {
404412
try {
405-
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec(getCurveName());
413+
ECNamedCurveParameterSpec params = getParameterSpec();
406414
ASN1ObjectIdentifier oid = getCurveOID(getCurveName());
407415
ECNamedDomainParameters domainParams = new ECNamedDomainParameters(oid,
408416
params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed()
@@ -442,10 +450,10 @@ public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject
442450
return StringHelper.newString(context.runtime, bytes.buffer(), bytes.size());
443451
}
444452
catch (IOException ex) {
445-
throw newECError(context.runtime, ex.toString());
453+
throw newECError(context.runtime, ex.getMessage());
446454
}
447455
catch (RuntimeException ex) {
448-
throw newECError(context.runtime, ex.toString());
456+
throw (RaiseException) newECError(context.runtime, ex.toString()).initCause(ex);
449457
}
450458
}
451459

0 commit comments

Comments
 (0)