21
21
import java .security .PublicKey ;
22
22
23
23
import java .security .SecureRandom ;
24
+ import java .security .SignatureException ;
24
25
import java .security .interfaces .ECPrivateKey ;
25
26
import java .security .interfaces .ECPublicKey ;
26
27
import java .security .spec .ECGenParameterSpec ;
69
70
import org .jruby .ext .openssl .impl .CipherSpec ;
70
71
import static org .jruby .ext .openssl .OpenSSL .debug ;
71
72
import static org .jruby .ext .openssl .OpenSSL .debugStackTrace ;
72
- import static org .jruby .ext .openssl .PKey ._PKey ;
73
73
import org .jruby .ext .openssl .impl .ECPrivateKeyWithName ;
74
74
import static org .jruby .ext .openssl .impl .PKey .readECPrivateKey ;
75
75
import org .jruby .ext .openssl .util .ByteArrayOutputStream ;
76
76
import org .jruby .ext .openssl .x509store .PEMInputOutput ;
77
+ import org .jruby .util .ByteList ;
77
78
78
79
/**
79
80
* OpenSSL::PKey::EC implementation.
@@ -200,8 +201,12 @@ public PKeyEC(Ruby runtime, RubyClass type) {
200
201
201
202
PKeyEC (Ruby runtime , RubyClass type , PrivateKey privKey , PublicKey pubKey ) {
202
203
super (runtime , type );
203
- this .privateKey = privKey ;
204
204
this .publicKey = (ECPublicKey ) pubKey ;
205
+ if (privKey instanceof ECPrivateKey ) {
206
+ setPrivateKey ((ECPrivateKey ) privKey );
207
+ } else {
208
+ this .privateKey = privKey ;
209
+ }
205
210
}
206
211
207
212
private transient Group group ;
@@ -213,9 +218,10 @@ public PKeyEC(Ruby runtime, RubyClass type) {
213
218
214
219
private String getCurveName () { return curveName ; }
215
220
216
- // private ECNamedCurveParameterSpec getParameterSpec() {
217
- // return ECNamedCurveTable.getParameterSpec( getCurveName() );
218
- // }
221
+ private ECNamedCurveParameterSpec getParameterSpec () {
222
+ assert curveName != null ;
223
+ return ECNamedCurveTable .getParameterSpec (getCurveName ());
224
+ }
219
225
220
226
@ Override
221
227
public PublicKey getPublicKey () { return publicKey ; }
@@ -342,12 +348,10 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
342
348
throw newECError (runtime , "Neither PUB key nor PRIV key: (invalid key type " + privKey .getClass ().getName () + ")" );
343
349
}
344
350
this .publicKey = (ECPublicKey ) pubKey ;
345
- this .privateKey = (ECPrivateKey ) privKey ;
346
- unwrapPrivateKeyWithName ();
351
+ setPrivateKey ((ECPrivateKey ) privKey );
347
352
}
348
353
else if ( key instanceof ECPrivateKey ) {
349
- this .privateKey = (ECPrivateKey ) key ;
350
- unwrapPrivateKeyWithName ();
354
+ setPrivateKey ((ECPrivateKey ) key );
351
355
}
352
356
else if ( key instanceof ECPublicKey ) {
353
357
this .publicKey = (ECPublicKey ) key ; this .privateKey = null ;
@@ -359,11 +363,15 @@ else if ( key instanceof ECPublicKey ) {
359
363
if ( publicKey != null ) {
360
364
publicKey .getParams ().getCurve ();
361
365
}
362
- // TODO set curveName ?!?!?!?!?!?!?!
363
366
364
367
return this ;
365
368
}
366
369
370
+ void setPrivateKey (final ECPrivateKey key ) {
371
+ this .privateKey = key ;
372
+ unwrapPrivateKeyWithName ();
373
+ }
374
+
367
375
private void unwrapPrivateKeyWithName () {
368
376
final ECPrivateKey privKey = (ECPrivateKey ) this .privateKey ;
369
377
if ( privKey instanceof ECPrivateKeyWithName ) {
@@ -402,7 +410,7 @@ public PKeyEC generate_key(final ThreadContext context) {
402
410
@ JRubyMethod (name = "dsa_sign_asn1" )
403
411
public IRubyObject dsa_sign_asn1 (final ThreadContext context , final IRubyObject data ) {
404
412
try {
405
- ECNamedCurveParameterSpec params = ECNamedCurveTable . getParameterSpec (getCurveName () );
413
+ ECNamedCurveParameterSpec params = getParameterSpec ();
406
414
ASN1ObjectIdentifier oid = getCurveOID (getCurveName ());
407
415
ECNamedDomainParameters domainParams = new ECNamedDomainParameters (oid ,
408
416
params .getCurve (), params .getG (), params .getN (), params .getH (), params .getSeed ()
@@ -442,10 +450,10 @@ public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject
442
450
return StringHelper .newString (context .runtime , bytes .buffer (), bytes .size ());
443
451
}
444
452
catch (IOException ex ) {
445
- throw newECError (context .runtime , ex .toString ());
453
+ throw newECError (context .runtime , ex .getMessage ());
446
454
}
447
455
catch (RuntimeException ex ) {
448
- throw newECError (context .runtime , ex .toString ());
456
+ throw ( RaiseException ) newECError (context .runtime , ex .toString ()). initCause ( ex );
449
457
}
450
458
}
451
459
0 commit comments