@@ -109,59 +109,53 @@ public static IRubyObject read(final ThreadContext context, IRubyObject recv, IR
109
109
pass = args [1 ].isNil () ? null : args [1 ].toString ().toCharArray ();
110
110
}
111
111
112
- final byte [] input = StringHelper . readX509PEM (context , data );
113
- KeyPair key = null ;
112
+ final RubyString str = readInitArg (context , data );
113
+ Object key = null ;
114
114
// d2i_PrivateKey_bio
115
115
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 */ }
122
118
// 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
- }
130
119
if (key != null ) {
131
- final String alg = getAlgorithm (key );
120
+ final KeyPair keyPair = (KeyPair ) key ;
121
+ final String alg = getAlgorithm (keyPair );
132
122
if ( "RSA" .equals (alg ) ) {
133
123
return new PKeyRSA (runtime , _PKey (runtime ).getClass ("RSA" ),
134
- (RSAPrivateCrtKey ) key .getPrivate (), (RSAPublicKey ) key .getPublic ()
124
+ (RSAPrivateCrtKey ) keyPair .getPrivate (), (RSAPublicKey ) keyPair .getPublic ()
135
125
);
136
126
}
137
127
if ( "DSA" .equals (alg ) ) {
138
128
return new PKeyDSA (runtime , _PKey (runtime ).getClass ("DSA" ),
139
- (DSAPrivateKey ) key .getPrivate (), (DSAPublicKey ) key .getPublic ()
129
+ (DSAPrivateKey ) keyPair .getPrivate (), (DSAPublicKey ) keyPair .getPublic ()
140
130
);
141
131
}
142
132
if ( "ECDSA" .equals (alg ) ) {
143
133
return new PKeyEC (runtime , _PKey (runtime ).getClass ("EC" ),
144
- (PrivateKey ) key .getPrivate (), (PublicKey ) key .getPublic ()
134
+ (PrivateKey ) keyPair .getPrivate (), (PublicKey ) keyPair .getPublic ()
145
135
);
146
136
}
147
137
}
148
138
149
139
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 );
150
150
// d2i_PUBKEY_bio
151
151
try {
152
152
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 */ }
158
154
// PEM_read_bio_PUBKEY
159
155
if (pubKey == null ) {
160
156
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 */ }
165
159
}
166
160
167
161
if (pubKey != null ) {
0 commit comments