Skip to content

Commit 17ae2fd

Browse files
committed
simplify esp. avoid the nasty PKeyRSA/DSA #initialize as it's really not necessary
1 parent 629db48 commit 17ae2fd

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/main/java/org/jruby/ext/openssl/PKeyDSA.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ private static void dsaGenerate(PKeyDSA dsa, int keysize) throws RaiseException
155155
}
156156
}
157157

158+
static PKeyDSA newInstance(final Ruby runtime, final PublicKey publicKey) {
159+
//if ( publicKey instanceof DSAPublicKey ) {
160+
return new PKeyDSA(runtime, (DSAPublicKey) publicKey);
161+
//}
162+
}
163+
158164
@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
159165
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
160166
final Ruby runtime = context.runtime;

src/main/java/org/jruby/ext/openssl/PKeyRSA.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ private static void rsaGenerate(PKeyRSA rsa, int keysize, BigInteger exp) throws
192192
}
193193
}
194194

195+
static PKeyRSA newInstance(final Ruby runtime, final PublicKey publicKey) {
196+
//if ( publicKey instanceof RSAPublicKey ) {
197+
return new PKeyRSA(runtime, (RSAPublicKey) publicKey);
198+
//}
199+
}
200+
195201
@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
196202
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
197203
final Ruby runtime = context.runtime;

src/main/java/org/jruby/ext/openssl/X509Cert.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,7 @@ DateTime getNotAfter() {
463463

464464
@JRubyMethod
465465
public IRubyObject public_key(final ThreadContext context) {
466-
if ( this.public_key == null ) {
467-
initializePublicKey(context);
468-
}
466+
if ( public_key == null ) initializePublicKey();
469467
return public_key.callMethod(context, "public_key");
470468
}
471469

@@ -480,40 +478,42 @@ public IRubyObject set_public_key(IRubyObject public_key) {
480478
return this.public_key = (PKey) public_key;
481479
}
482480

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+
}
484485

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();
487488

488-
RubyModule OpenSSL = context.runtime.getModule("OpenSSL");
489-
RubyModule PKey = (RubyModule) OpenSSL.getConstant("PKey");
489+
final boolean changed = this.changed;
490490

491-
if ( getPublicKey() == null ) {
492-
throw newCertificateError(context.runtime, "no certificate");
491+
if ( cert == null ) {
492+
throw newCertificateError(runtime, "no certificate");
493493
}
494494

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();
497498

498499
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) );
504504
}
505505
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) );
511510
}
512511
else {
513512
String message = "unsupported algorithm";
514513
if ( algorithm != null ) message += " '" + algorithm + "'";
515-
throw newCertificateError(context.runtime, message);
514+
throw newCertificateError(runtime, message);
516515
}
516+
517517
this.changed = changed;
518518
}
519519

@@ -571,9 +571,7 @@ private org.bouncycastle.x509.X509V3CertificateGenerator getCertificateBuilder()
571571

572572
generator.setNotBefore( not_before.getJavaDate() );
573573
generator.setNotAfter( not_after.getJavaDate() );
574-
575-
if ( public_key == null ) initializePublicKey(getRuntime().getCurrentContext());
576-
generator.setPublicKey( public_key.getPublicKey() );
574+
generator.setPublicKey( getPublicKey() );
577575

578576
return generator;
579577
}

0 commit comments

Comments
 (0)