@@ -109,59 +109,53 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
109109 pass = args [1 ].isNil () ? null : args [1 ].toString ().toCharArray ();
110110 }
111111
112- final byte [] input = StringHelper . readX509PEM (context , data );
113- KeyPair key = null ;
112+ final RubyString str = readInitArg (context , data );
113+ Object key = null ;
114114 // d2i_PrivateKey_bio
115115 try {
116- key =org .jruby .ext .openssl .impl .PKey .readPrivateKey (input );
117- } catch (IOException ioe ) {
118- // ignore
119- } catch (GeneralSecurityException gse ) {
120- // ignore
121- }
116+ key = readPrivateKey (str , pass );
117+ } catch (IOException e ) { /* ignore */ }
122118 // PEM_read_bio_PrivateKey
123- if (key == null ) {
124- try {
125- key = PEMInputOutput .readPrivateKey (new InputStreamReader (new ByteArrayInputStream (input )), pass );
126- } catch (IOException ioe ) {
127- // ignore
128- }
129- }
130119 if (key != null ) {
131- final String alg = getAlgorithm (key );
120+ final KeyPair keyPair = (KeyPair ) key ;
121+ final String alg = getAlgorithm (keyPair );
132122 if ( "RSA" .equals (alg ) ) {
133123 return new PKeyRSA (runtime , _PKey (runtime ).getClass ("RSA" ),
134- (RSAPrivateCrtKey ) key .getPrivate (), (RSAPublicKey ) key .getPublic ()
124+ (RSAPrivateCrtKey ) keyPair .getPrivate (), (RSAPublicKey ) keyPair .getPublic ()
135125 );
136126 }
137127 if ( "DSA" .equals (alg ) ) {
138128 return new PKeyDSA (runtime , _PKey (runtime ).getClass ("DSA" ),
139- (DSAPrivateKey ) key .getPrivate (), (DSAPublicKey ) key .getPublic ()
129+ (DSAPrivateKey ) keyPair .getPrivate (), (DSAPublicKey ) keyPair .getPublic ()
140130 );
141131 }
142132 if ( "ECDSA" .equals (alg ) ) {
143133 return new PKeyEC (runtime , _PKey (runtime ).getClass ("EC" ),
144- (PrivateKey ) key .getPrivate (), (PublicKey ) key .getPublic ()
134+ (PrivateKey ) keyPair .getPrivate (), (PublicKey ) keyPair .getPublic ()
145135 );
146136 }
147137 }
148138
149139 PublicKey pubKey = null ;
140+ try {
141+ pubKey = PEMInputOutput .readRSAPublicKey (new StringReader (str .toString ()), null );
142+ return new PKeyRSA (runtime , (RSAPublicKey ) pubKey );
143+ } catch (IOException e ) { /* ignore */ }
144+ try {
145+ pubKey = PEMInputOutput .readDSAPublicKey (new StringReader (str .toString ()), null );
146+ return new PKeyDSA (runtime , (DSAPublicKey ) pubKey );
147+ } catch (IOException e ) { /* ignore */ }
148+
149+ final byte [] input = StringHelper .readX509PEM (context , str );
150150 // d2i_PUBKEY_bio
151151 try {
152152 pubKey = org .jruby .ext .openssl .impl .PKey .readPublicKey (input );
153- } catch (IOException ioe ) {
154- // ignore
155- } catch (GeneralSecurityException gse ) {
156- // ignore
157- }
153+ } catch (IOException |GeneralSecurityException e ) { /* ignore */ }
158154 // PEM_read_bio_PUBKEY
159155 if (pubKey == null ) {
160156 try {
161- pubKey = PEMInputOutput .readPubKey (new InputStreamReader (new ByteArrayInputStream (input )));
162- } catch (IOException ioe ) {
163- // ignore
164- }
157+ pubKey = PEMInputOutput .readPubKey (new StringReader (str .toString ()));
158+ } catch (IOException e ) { /* ignore */ }
165159 }
166160
167161 if (pubKey != null ) {
0 commit comments