From d0f11afd02e9843e98ca1b074db63b1299bf71c9 Mon Sep 17 00:00:00 2001 From: kares Date: Thu, 4 Apr 2024 08:00:49 +0200 Subject: [PATCH] [refactor] PKey read-er methods and return types --- src/main/java/org/jruby/ext/openssl/PKey.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jruby/ext/openssl/PKey.java b/src/main/java/org/jruby/ext/openssl/PKey.java index ff42bdef..5191dac4 100644 --- a/src/main/java/org/jruby/ext/openssl/PKey.java +++ b/src/main/java/org/jruby/ext/openssl/PKey.java @@ -27,10 +27,8 @@ ***** END LICENSE BLOCK *****/ package org.jruby.ext.openssl; -import java.io.ByteArrayInputStream; import java.io.Console; import java.io.IOException; -import java.io.InputStreamReader; import java.io.StringReader; import java.math.BigInteger; import java.security.*; @@ -57,11 +55,12 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.Visibility; +import org.jruby.util.ByteList; +import org.jruby.ext.openssl.impl.CipherSpec; import org.jruby.ext.openssl.x509store.PEMInputOutput; + import static org.jruby.ext.openssl.OpenSSL.*; -import org.jruby.ext.openssl.impl.CipherSpec; -import org.jruby.util.ByteList; /** * @author Ola Bini @@ -111,14 +110,16 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR } final RubyString str = readInitArg(context, data); - Object key = null; + KeyPair keyPair; // d2i_PrivateKey_bio try { - key = readPrivateKey(str, pass); - } catch (IOException e) { /* ignore */ } + keyPair = readPrivateKey(str, pass); + } catch (IOException e) { + debugStackTrace(runtime, "PKey readPrivateKey", e); /* ignore */ + keyPair = null; + } // PEM_read_bio_PrivateKey - if (key != null) { - final KeyPair keyPair = (KeyPair) key; + if (keyPair != null) { final String alg = getAlgorithm(keyPair); if ( "RSA".equals(alg) ) { return new PKeyRSA(runtime, _PKey(runtime).getClass("RSA"), @@ -141,17 +142,23 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR try { pubKey = PEMInputOutput.readRSAPublicKey(new StringReader(str.toString()), null); return new PKeyRSA(runtime, (RSAPublicKey) pubKey); - } catch (IOException e) { /* ignore */ } + } catch (IOException e) { + debugStackTrace(runtime, "PKey readRSAPublicKey", e); /* ignore */ + } try { pubKey = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), null); return new PKeyDSA(runtime, (DSAPublicKey) pubKey); - } catch (IOException e) { /* ignore */ } + } catch (IOException e) { + debugStackTrace(runtime, "PKey readDSAPublicKey", e); /* ignore */ + } final byte[] input = StringHelper.readX509PEM(context, str); // d2i_PUBKEY_bio try { pubKey = org.jruby.ext.openssl.impl.PKey.readPublicKey(input); - } catch (IOException|GeneralSecurityException e) { /* ignore */ } + } catch (IOException|GeneralSecurityException e) { + debugStackTrace(runtime, "PKey readPublicKey", e); /* ignore */ + } // PEM_read_bio_PUBKEY if (pubKey == null) { try {