@@ -202,6 +202,7 @@ public PKeyEC(Ruby runtime, RubyClass type) {
202
202
setPrivateKey ((ECPrivateKey ) privKey );
203
203
} else {
204
204
this .privateKey = privKey ;
205
+ setCurveNameFromPublicKeyIfNeeded ();
205
206
}
206
207
}
207
208
@@ -353,14 +354,18 @@ else if ( key instanceof ECPublicKey ) {
353
354
throw newECError (runtime , "Neither PUB key nor PRIV key: " );
354
355
}
355
356
356
- if ( curveName == null && publicKey != null ) {
357
- final String oid = getCurveNameObjectIdFromKey (context , publicKey );
357
+ setCurveNameFromPublicKeyIfNeeded ();
358
+
359
+ return this ;
360
+ }
361
+
362
+ private void setCurveNameFromPublicKeyIfNeeded () {
363
+ if (curveName == null && publicKey != null ) {
364
+ final String oid = getCurveNameObjectIdFromKey (getRuntime (), publicKey );
358
365
if (isCurveName (oid )) {
359
366
this .curveName = getCurveName (new ASN1ObjectIdentifier (oid ));
360
367
}
361
368
}
362
-
363
- return this ;
364
369
}
365
370
366
371
void setPrivateKey (final ECPrivateKey key ) {
@@ -376,17 +381,17 @@ private void unwrapPrivateKeyWithName() {
376
381
}
377
382
}
378
383
379
- private static String getCurveNameObjectIdFromKey (final ThreadContext context , final ECPublicKey key ) {
384
+ private static String getCurveNameObjectIdFromKey (final Ruby runtime , final ECPublicKey key ) {
380
385
try {
381
386
AlgorithmParameters algParams = AlgorithmParameters .getInstance ("EC" );
382
387
algParams .init (key .getParams ());
383
388
return algParams .getParameterSpec (ECGenParameterSpec .class ).getName ();
384
389
}
385
390
catch (NoSuchAlgorithmException |InvalidParameterSpecException ex ) {
386
- throw newECError (context . runtime , ex .getMessage ());
391
+ throw newECError (runtime , ex .getMessage ());
387
392
}
388
393
catch (Exception ex ) {
389
- throw (RaiseException ) newECError (context . runtime , ex .toString ()).initCause (ex );
394
+ throw (RaiseException ) newECError (runtime , ex .toString ()).initCause (ex );
390
395
}
391
396
}
392
397
@@ -535,10 +540,7 @@ public IRubyObject oid() {
535
540
536
541
private Group getGroup (boolean required ) {
537
542
if (group == null ) {
538
- if (publicKey != null ) {
539
- return group = new Group (getRuntime (), this );
540
- }
541
- if (required ) throw new IllegalStateException ("no group (without public key)" );
543
+ return group = new Group (getRuntime (), this );
542
544
}
543
545
return group ;
544
546
}
@@ -748,7 +750,6 @@ public Group(Ruby runtime, RubyClass type) {
748
750
Group (Ruby runtime , PKeyEC key ) {
749
751
this (runtime , _EC (runtime ).getClass ("Group" ));
750
752
this .key = key ;
751
- this .paramSpec = key .publicKey .getParams ();
752
753
}
753
754
754
755
@ JRubyMethod (rest = true , visibility = Visibility .PRIVATE )
@@ -759,8 +760,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
759
760
IRubyObject arg = args [0 ];
760
761
761
762
if ( arg instanceof Group ) {
762
- IRubyObject curve_name = ((Group ) arg ).curve_name (context );
763
- this .curve_name = curve_name .isNil () ? null : (RubyString ) curve_name ;
763
+ this .curve_name = ((Group ) arg ).implCurveName (runtime );
764
764
return this ;
765
765
}
766
766
@@ -771,6 +771,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
771
771
772
772
private String getCurveName () {
773
773
if (key != null ) return key .getCurveName ();
774
+ assert curve_name != null ;
774
775
return curve_name .toString ();
775
776
}
776
777
@@ -792,6 +793,7 @@ public IRubyObject curve_name(final ThreadContext context) {
792
793
793
794
private RubyString implCurveName (final Ruby runtime ) {
794
795
if (curve_name == null ) {
796
+ assert key != null ;
795
797
String prefix , curveName = key .getCurveName ();
796
798
// BC 1.54: "brainpoolP512t1" 1.55: "brainpoolp512t1"
797
799
if (curveName .startsWith (prefix = "brainpoolp" )) {
@@ -854,7 +856,11 @@ public RubyString to_pem(final ThreadContext context, final IRubyObject[] args)
854
856
855
857
private ECParameterSpec getParamSpec () {
856
858
if (paramSpec == null ) {
857
- paramSpec = PKeyEC .getParamSpec (getCurveName ());
859
+ if (key != null ) {
860
+ return paramSpec = key .getParamSpec ();
861
+ }
862
+ assert curve_name != null ;
863
+ return paramSpec = PKeyEC .getParamSpec (getCurveName ());
858
864
}
859
865
return paramSpec ;
860
866
}
0 commit comments