Skip to content

Commit cff68a0

Browse files
committed
add a readPrivateKey with a Java String passed directly
1 parent 81e451d commit cff68a0

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,14 @@ protected static boolean ttySTDIN(final ThreadContext context) {
362362
catch (RaiseException ex) { return false; }
363363
}
364364

365+
static Object readPrivateKey(final String str, final char[] passwd)
366+
throws PEMInputOutput.PasswordRequiredException, IOException {
367+
return PEMInputOutput.readPrivateKey(new StringReader(str), passwd);
368+
}
369+
365370
static Object readPrivateKey(final RubyString str, final char[] passwd)
366371
throws PEMInputOutput.PasswordRequiredException, IOException {
367-
return PEMInputOutput.readPrivateKey(new StringReader(str.toString()), passwd);
372+
return readPrivateKey(str.toString(), passwd);
368373
}
369374

370375
protected static RubyString readInitArg(final ThreadContext context, IRubyObject arg) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
178178

179179
final char[] passwd = password(pass);
180180
final RubyString str = readInitArg(context, arg);
181+
final String strJava = str.toString();
181182

182183
Object key = null;
183184
final KeyFactory dsaFactory;
@@ -194,27 +195,27 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
194195
boolean noClassDef = false;
195196
if ( key == null && ! noClassDef ) { // PEM_read_bio_DSAPrivateKey
196197
try {
197-
key = readPrivateKey(str, passwd);
198+
key = readPrivateKey(strJava, passwd);
198199
}
199200
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
200201
catch (PEMInputOutput.PasswordRequiredException retry) {
201202
if ( ttySTDIN(context) ) {
202-
try { key = readPrivateKey(str, passwordPrompt(context)); }
203+
try { key = readPrivateKey(strJava, passwordPrompt(context)); }
203204
catch (Exception e) { debugStackTrace(runtime, e); }
204205
}
205206
}
206207
catch (Exception e) { debugStackTrace(runtime, e); }
207208
}
208209
if ( key == null && ! noClassDef ) { // PEM_read_bio_DSAPublicKey
209210
try {
210-
key = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), passwd);
211+
key = PEMInputOutput.readDSAPublicKey(new StringReader(strJava), passwd);
211212
}
212213
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
213214
catch (Exception e) { debugStackTrace(runtime, e); }
214215
}
215216
if ( key == null && ! noClassDef ) { // PEM_read_bio_DSA_PUBKEY
216217
try {
217-
key = PEMInputOutput.readDSAPubKey(new StringReader(str.toString()));
218+
key = PEMInputOutput.readDSAPubKey(new StringReader(strJava));
218219
}
219220
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
220221
catch (Exception e) { debugStackTrace(runtime, e); }

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
217217

218218
final char[] passwd = password(pass);
219219
final RubyString str = readInitArg(context, arg);
220+
final String strJava = str.toString();
220221

221222
Object key = null;
222223
final KeyFactory rsaFactory;
@@ -233,27 +234,27 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
233234
boolean noClassDef = false;
234235
if ( key == null && ! noClassDef ) { // PEM_read_bio_RSAPrivateKey
235236
try {
236-
key = readPrivateKey(str, passwd);
237+
key = readPrivateKey(strJava, passwd);
237238
}
238239
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
239240
catch (PEMInputOutput.PasswordRequiredException retry) {
240241
if ( ttySTDIN(context) ) {
241-
try { key = readPrivateKey(str, passwordPrompt(context)); }
242+
try { key = readPrivateKey(strJava, passwordPrompt(context)); }
242243
catch (Exception e) { debugStackTrace(runtime, e); }
243244
}
244245
}
245246
catch (Exception e) { debugStackTrace(runtime, e); }
246247
}
247248
if ( key == null && ! noClassDef ) { // PEM_read_bio_RSAPublicKey
248249
try {
249-
key = PEMInputOutput.readRSAPublicKey(new StringReader(str.toString()), passwd);
250+
key = PEMInputOutput.readRSAPublicKey(new StringReader(strJava), passwd);
250251
}
251252
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
252253
catch (Exception e) { debugStackTrace(runtime, e); }
253254
}
254255
if ( key == null && ! noClassDef ) { // PEM_read_bio_RSA_PUBKEY
255256
try {
256-
key = PEMInputOutput.readRSAPubKey(new StringReader(str.toString()));
257+
key = PEMInputOutput.readRSAPubKey(new StringReader(strJava));
257258
}
258259
catch (NoClassDefFoundError e) { noClassDef = true; debugStackTrace(runtime, e); }
259260
catch (Exception e) { debugStackTrace(runtime, e); }

0 commit comments

Comments
 (0)