@@ -463,9 +463,7 @@ DateTime getNotAfter() {
463
463
464
464
@ JRubyMethod
465
465
public IRubyObject public_key (final ThreadContext context ) {
466
- if ( this .public_key == null ) {
467
- initializePublicKey (context );
468
- }
466
+ if ( public_key == null ) initializePublicKey ();
469
467
return public_key .callMethod (context , "public_key" );
470
468
}
471
469
@@ -480,40 +478,42 @@ public IRubyObject set_public_key(IRubyObject public_key) {
480
478
return this .public_key = (PKey ) public_key ;
481
479
}
482
480
483
- private PublicKey getPublicKey () { return cert == null ? null : cert .getPublicKey (); }
481
+ private PublicKey getPublicKey () {
482
+ if ( public_key == null ) initializePublicKey ();
483
+ return public_key .getPublicKey ();
484
+ }
484
485
485
- private void initializePublicKey (final ThreadContext context ) throws RaiseException {
486
- final boolean changed = this . changed ;
486
+ private void initializePublicKey () throws RaiseException {
487
+ final Ruby runtime = getRuntime () ;
487
488
488
- RubyModule OpenSSL = context .runtime .getModule ("OpenSSL" );
489
- RubyModule PKey = (RubyModule ) OpenSSL .getConstant ("PKey" );
489
+ final boolean changed = this .changed ;
490
490
491
- if ( getPublicKey () == null ) {
492
- throw newCertificateError (context . runtime , "no certificate" );
491
+ if ( cert == null ) {
492
+ throw newCertificateError (runtime , "no certificate" );
493
493
}
494
494
495
- final String algorithm = getPublicKey ().getAlgorithm ();
496
- final byte [] public_key = getPublicKey ().getEncoded ();
495
+ final PublicKey publicKey = cert .getPublicKey ();
496
+
497
+ final String algorithm = publicKey .getAlgorithm ();
497
498
498
499
if ( "RSA" .equalsIgnoreCase (algorithm ) ) {
499
- if ( public_key == null ) {
500
- throw new IllegalStateException ("no public key encoded data" );
501
- }
502
- RubyString encoded = RubyString .newString (context .runtime , public_key );
503
- set_public_key ( PKey .getConstant ("RSA" ).callMethod (context , "new" , encoded ) );
500
+ //if ( public_key == null ) {
501
+ // throw new IllegalStateException("no public key encoded data");
502
+ //}
503
+ set_public_key ( PKeyRSA .newInstance (runtime , publicKey ) );
504
504
}
505
505
else if ( "DSA" .equalsIgnoreCase (algorithm ) ) {
506
- if ( public_key == null ) {
507
- throw new IllegalStateException ("no public key encoded data" );
508
- }
509
- RubyString encoded = RubyString .newString (context .runtime , public_key );
510
- set_public_key ( PKey .getConstant ("DSA" ).callMethod (context , "new" , encoded ) );
506
+ //if ( public_key == null ) {
507
+ // throw new IllegalStateException("no public key encoded data");
508
+ //}
509
+ set_public_key ( PKeyDSA .newInstance (runtime , publicKey ) );
511
510
}
512
511
else {
513
512
String message = "unsupported algorithm" ;
514
513
if ( algorithm != null ) message += " '" + algorithm + "'" ;
515
- throw newCertificateError (context . runtime , message );
514
+ throw newCertificateError (runtime , message );
516
515
}
516
+
517
517
this .changed = changed ;
518
518
}
519
519
@@ -571,9 +571,7 @@ private org.bouncycastle.x509.X509V3CertificateGenerator getCertificateBuilder()
571
571
572
572
generator .setNotBefore ( not_before .getJavaDate () );
573
573
generator .setNotAfter ( not_after .getJavaDate () );
574
-
575
- if ( public_key == null ) initializePublicKey (getRuntime ().getCurrentContext ());
576
- generator .setPublicKey ( public_key .getPublicKey () );
574
+ generator .setPublicKey ( getPublicKey () );
577
575
578
576
return generator ;
579
577
}
0 commit comments