2121import java .security .PublicKey ;
2222
2323import java .security .SecureRandom ;
24+ import java .security .SignatureException ;
2425import java .security .interfaces .ECPrivateKey ;
2526import java .security .interfaces .ECPublicKey ;
2627import java .security .spec .ECGenParameterSpec ;
6970import org .jruby .ext .openssl .impl .CipherSpec ;
7071import static org .jruby .ext .openssl .OpenSSL .debug ;
7172import static org .jruby .ext .openssl .OpenSSL .debugStackTrace ;
72- import static org .jruby .ext .openssl .PKey ._PKey ;
7373import org .jruby .ext .openssl .impl .ECPrivateKeyWithName ;
7474import static org .jruby .ext .openssl .impl .PKey .readECPrivateKey ;
7575import org .jruby .ext .openssl .util .ByteArrayOutputStream ;
7676import 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