1313import java .math .BigInteger ;
1414import java .security .AlgorithmParameters ;
1515import java .security .GeneralSecurityException ;
16+ import java .security .InvalidAlgorithmParameterException ;
1617import java .security .InvalidKeyException ;
1718import java .security .KeyFactory ;
1819import java .security .KeyPair ;
@@ -438,8 +439,11 @@ public PKeyEC generate_key(final ThreadContext context) {
438439 this .publicKey = (ECPublicKey ) pair .getPublic ();
439440 this .privateKey = pair .getPrivate ();
440441 }
442+ catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException ex ) {
443+ throw PKey .newPKeyError (context .runtime , ex .getMessage ());
444+ }
441445 catch (GeneralSecurityException ex ) {
442- throw newECError (context .runtime , ex .toString ());
446+ throw ( RaiseException ) newECError (context .runtime , ex .toString ()). initCause ( ex );
443447 }
444448 return this ;
445449 }
@@ -567,9 +571,15 @@ private Group getGroup(boolean required) {
567571 * @return OpenSSL::PKey::EC::Group
568572 */
569573 @ JRubyMethod
570- public IRubyObject group () {
571- final Group group = getGroup (false );
572- return group == null ? getRuntime ().getNil () : group ;
574+ public IRubyObject group (ThreadContext context ) {
575+ Group group = this .group ;
576+ if (group != null ) return group ;
577+
578+ if (publicKey == null && publicKey == null ) {
579+ return context .nil ; // PKey::EC.new
580+ }
581+ group = getGroup (false );
582+ return group == null ? context .nil : group ;
573583 }
574584
575585 @ JRubyMethod (name = "group=" )
@@ -679,7 +689,7 @@ public RubyString to_der() {
679689 return public_to_der (runtime );
680690 }
681691 if (privateKey == null ) {
682- throw new IllegalStateException ( "private key as well as public key are null " );
692+ throw PKey . newPKeyError ( runtime , "can't export - no public key set " );
683693 }
684694
685695 try {
@@ -841,6 +851,11 @@ static void createGroup(final Ruby runtime, final RubyClass EC, final RubyClass
841851 Group .defineAnnotatedMethods (Group .class );
842852 }
843853
854+ static RaiseException newError (final Ruby runtime , final String message ) {
855+ final RubyClass Error = _EC (runtime ).getClass ("Group" ).getClass ("Error" );
856+ return Utils .newError (runtime , Error , message );
857+ }
858+
844859 private transient ECParameterSpec paramSpec ;
845860
846861 private PointConversion conversionForm = PointConversion .UNCOMPRESSED ;
@@ -854,7 +869,8 @@ public Group(Ruby runtime, RubyClass type) {
854869
855870 Group (Ruby runtime , PKeyEC key ) {
856871 this (runtime , _EC (runtime ).getClass ("Group" ));
857- setCurveName (runtime , key .getCurveName ());
872+ final String curveName = key .getCurveName ();
873+ if (curveName != null ) setCurveName (runtime , curveName );
858874 }
859875
860876 @ JRubyMethod (rest = true , visibility = Visibility .PRIVATE )
@@ -876,7 +892,9 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
876892
877893 private String getCurveName () {
878894 if (curveName == null ) {
879- assert impl_curve_name != null ;
895+ if (impl_curve_name == null ) {
896+ throw newError (getRuntime (), "no curve name" );
897+ }
880898 return impl_curve_name .asJavaString ();
881899 }
882900 return curveName ;
0 commit comments