Skip to content

Commit f10ea4f

Browse files
committed
unify and DRY out PKEy's to_pem impl
1 parent 1580de0 commit f10ea4f

File tree

4 files changed

+84
-51
lines changed

4 files changed

+84
-51
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
import org.jruby.ext.openssl.x509store.PEMInputOutput;
6464
import static org.jruby.ext.openssl.OpenSSL.*;
65+
import org.jruby.ext.openssl.impl.CipherSpec;
6566
import static org.jruby.ext.openssl.impl.PKey.readPrivateKey;
6667
import static org.jruby.ext.openssl.impl.PKey.readPublicKey;
6768

@@ -186,8 +187,14 @@ public IRubyObject initialize(ThreadContext context) {
186187

187188
public String getAlgorithm() { return "NONE"; }
188189

189-
// NetscapeSPKI uses it.
190-
public abstract IRubyObject to_der();
190+
public abstract RubyString to_der() ;
191+
192+
public abstract RubyString to_pem(final IRubyObject[] args) ;
193+
194+
@Deprecated
195+
public RubyString export(final IRubyObject[] args) {
196+
return to_pem(args);
197+
}
191198

192199
@JRubyMethod(name = "sign")
193200
public IRubyObject sign(IRubyObject digest, IRubyObject data) {
@@ -328,6 +335,14 @@ protected static void addSplittedAndFormatted(StringBuilder result, BigInteger v
328335
result.append("\n");
329336
}
330337

338+
protected static CipherSpec cipherSpec(final IRubyObject cipher) {
339+
if ( cipher != null && ! cipher.isNil() ) {
340+
final Cipher c = (Cipher) cipher;
341+
return new CipherSpec(c.getCipherInstance(), c.getName(), c.getKeyLength() * 8);
342+
}
343+
return null;
344+
}
345+
331346
protected static char[] password(final IRubyObject pass) {
332347
if ( pass != null && ! pass.isNil() ) {
333348
return pass.toString().toCharArray();

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.crypto.spec.DHPublicKeySpec;
4545

4646
import org.jruby.Ruby;
47+
import org.jruby.RubyBoolean;
4748
import org.jruby.RubyClass;
4849
import org.jruby.RubyHash;
4950
import org.jruby.RubyModule;
@@ -61,6 +62,7 @@
6162

6263
import static org.jruby.ext.openssl.PKey._PKey;
6364
import static org.jruby.ext.openssl.OpenSSL.bcExceptionMessage;
65+
import org.jruby.ext.openssl.impl.CipherSpec;
6466

6567
/**
6668
* OpenSSL::PKey::DH implementation.
@@ -270,20 +272,29 @@ public static byte[] computeKey(BigInteger y, BigInteger x, BigInteger p) {
270272
}
271273

272274
@JRubyMethod(name = "public?")
273-
public IRubyObject public_p() {
275+
public RubyBoolean public_p() {
274276
return getRuntime().newBoolean(dh_y != null);
275277
}
276278

277279
@JRubyMethod(name = "private?")
278-
public IRubyObject private_p() {
280+
public RubyBoolean private_p() {
279281
// FIXME! need to figure out what it means in MRI/OSSL code to
280282
// claim a DH is private if an engine is present -- doesn't really
281283
// map to Java implementation.
282284
return getRuntime().newBoolean(dh_x != null /* || haveEngine */);
283285
}
284286

285-
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export")
286-
public RubyString to_pem() {
287+
@Override
288+
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
289+
public RubyString to_pem(final IRubyObject[] args) {
290+
//Arity.checkArgumentCount(getRuntime(), args, 0, 2);
291+
292+
//CipherSpec spec = null; char[] passwd = null;
293+
//if ( args.length > 0 ) {
294+
// spec = cipherSpec( args[0] );
295+
// if ( args.length > 1 ) passwd = password(args[1]);
296+
//}
297+
287298
BigInteger p, g;
288299
synchronized(this) {
289300
p = this.dh_p;
@@ -299,11 +310,11 @@ public RubyString to_pem() {
299310
catch (IOException e) { // shouldn't happen (string/buffer io only)
300311
throw getRuntime().newIOErrorFromException(e);
301312
}
302-
return getRuntime().newString(writer.toString());
313+
return RubyString.newString(getRuntime(), writer.getBuffer());
303314
}
304315

305316
@Override
306-
@JRubyMethod
317+
@JRubyMethod(name = "to_der")
307318
public RubyString to_der() {
308319
BigInteger p, g;
309320
synchronized (this) {

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.security.spec.InvalidKeySpecException;
4747

4848
import org.jruby.Ruby;
49+
import org.jruby.RubyBoolean;
4950
import org.jruby.RubyClass;
5051
import org.jruby.RubyFixnum;
5152
import org.jruby.RubyModule;
@@ -265,18 +266,18 @@ else if ( key instanceof DSAPublicKey ) {
265266
return this;
266267
}
267268

268-
@JRubyMethod(name="public?")
269-
public IRubyObject public_p() {
269+
@JRubyMethod(name = "public?")
270+
public RubyBoolean public_p() {
270271
return pubKey != null ? getRuntime().getTrue() : getRuntime().getFalse();
271272
}
272273

273-
@JRubyMethod(name="private?")
274-
public IRubyObject private_p() {
274+
@JRubyMethod(name = "private?")
275+
public RubyBoolean private_p() {
275276
return privKey != null ? getRuntime().getTrue() : getRuntime().getFalse();
276277
}
277278

278279
@Override
279-
@JRubyMethod
280+
@JRubyMethod(name = "to_der")
280281
public RubyString to_der() {
281282
final byte[] bytes;
282283
try {
@@ -316,28 +317,31 @@ public PKeyDSA public_key() {
316317
return new PKeyDSA(getRuntime(), this.pubKey);
317318
}
318319

319-
@JRubyMethod(name = { "export", "to_pem", "to_s" }, rest = true)
320-
public IRubyObject export(IRubyObject[] args) {
320+
@Override
321+
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
322+
public RubyString to_pem(final IRubyObject[] args) {
321323
Arity.checkArgumentCount(getRuntime(), args, 0, 2);
324+
322325
CipherSpec spec = null; char[] passwd = null;
323-
if (args.length > 0 && !args[0].isNil()) {
324-
final Cipher c = (Cipher) args[0];
325-
spec = new CipherSpec(c.getCipherInstance(), c.getName(), c.getKeyLength() * 8);
326-
if (args.length > 1 && !args[1].isNil()) {
327-
passwd = args[1].toString().toCharArray();
328-
}
326+
if ( args.length > 0 ) {
327+
spec = cipherSpec( args[0] );
328+
if ( args.length > 1 ) passwd = password(args[1]);
329329
}
330+
330331
try {
331332
final StringWriter writer = new StringWriter();
332-
if (privKey != null) {
333+
if ( privKey != null ) {
333334
PEMInputOutput.writeDSAPrivateKey(writer, privKey, spec, passwd);
334-
} else {
335+
}
336+
else {
335337
PEMInputOutput.writeDSAPublicKey(writer, pubKey);
336338
}
337-
return getRuntime().newString(writer.toString());
338-
} catch (NoClassDefFoundError ncdfe) {
339+
return RubyString.newString(getRuntime(), writer.getBuffer());
340+
}
341+
catch (NoClassDefFoundError ncdfe) {
339342
throw newDSAError(getRuntime(), bcExceptionMessage(ncdfe));
340-
} catch (IOException ioe) {
343+
}
344+
catch (IOException ioe) {
341345
throw newDSAError(getRuntime(), ioe.getMessage());
342346
}
343347
}

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.jruby.Ruby;
5454
import org.jruby.RubyClass;
5555
import org.jruby.RubyBignum;
56+
import org.jruby.RubyBoolean;
5657
import org.jruby.RubyFixnum;
5758
import org.jruby.RubyHash;
5859
import org.jruby.RubyModule;
@@ -71,6 +72,7 @@
7172
import org.jruby.ext.openssl.x509store.PEMInputOutput;
7273
import static org.jruby.ext.openssl.OpenSSL.*;
7374
import static org.jruby.ext.openssl.PKey._PKey;
75+
import static org.jruby.ext.openssl.PKey.cipherSpec;
7476
import static org.jruby.ext.openssl.impl.PKey.readRSAPrivateKey;
7577
import static org.jruby.ext.openssl.impl.PKey.readRSAPublicKey;
7678
import static org.jruby.ext.openssl.impl.PKey.toDerRSAKey;
@@ -192,8 +194,7 @@ private static void rsaGenerate(PKeyRSA rsa, int keysize, BigInteger exp) throws
192194
}
193195

194196
@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
195-
public IRubyObject initialize(final ThreadContext context,
196-
final IRubyObject[] args, final Block block) {
197+
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
197198
final Ruby runtime = context.runtime;
198199

199200
if ( Arity.checkArgumentCount(runtime, args, 0, 2) == 0 ) {
@@ -305,18 +306,18 @@ else if ( key instanceof RSAPublicKey ) {
305306
return this;
306307
}
307308

308-
@JRubyMethod(name="public?")
309-
public IRubyObject public_p() {
309+
@JRubyMethod(name = "public?")
310+
public RubyBoolean public_p() {
310311
return pubKey != null ? getRuntime().getTrue() : getRuntime().getFalse();
311312
}
312313

313-
@JRubyMethod(name="private?")
314-
public IRubyObject private_p() {
314+
@JRubyMethod(name = "private?")
315+
public RubyBoolean private_p() {
315316
return privKey != null ? getRuntime().getTrue() : getRuntime().getFalse();
316317
}
317318

318319
@Override
319-
@JRubyMethod
320+
@JRubyMethod(name = "to_der")
320321
public RubyString to_der() {
321322
final byte[] bytes;
322323
try {
@@ -364,7 +365,7 @@ public IRubyObject params(final ThreadContext context) {
364365
}
365366

366367
@JRubyMethod
367-
public IRubyObject to_text() {
368+
public RubyString to_text() {
368369
StringBuilder result = new StringBuilder();
369370
if (privKey != null) {
370371
int len = privKey.getModulus().bitLength();
@@ -393,29 +394,31 @@ public IRubyObject to_text() {
393394
return getRuntime().newString(result.toString());
394395
}
395396

396-
@JRubyMethod(name = { "export", "to_pem", "to_s" }, rest = true)
397-
public IRubyObject export(IRubyObject[] args) {
398-
StringWriter w = new StringWriter();
397+
@Override
398+
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
399+
public RubyString to_pem(final IRubyObject[] args) {
399400
Arity.checkArgumentCount(getRuntime(), args, 0, 2);
401+
400402
CipherSpec spec = null; char[] passwd = null;
401-
if (args.length > 0 && !args[0].isNil()) {
402-
final Cipher c = (Cipher) args[0];
403-
spec = new CipherSpec(c.getCipherInstance(), c.getName(), c.getKeyLength() * 8);
404-
if (args.length > 1 && !args[1].isNil()) {
405-
passwd = args[1].toString().toCharArray();
406-
}
403+
if ( args.length > 0 ) {
404+
spec = cipherSpec( args[0] );
405+
if ( args.length > 1 ) passwd = password(args[1]);
407406
}
407+
408408
try {
409-
if (privKey != null) {
410-
PEMInputOutput.writeRSAPrivateKey(w, privKey, spec, passwd);
411-
} else {
412-
PEMInputOutput.writeRSAPublicKey(w, pubKey);
409+
final StringWriter writer = new StringWriter();
410+
if ( privKey != null ) {
411+
PEMInputOutput.writeRSAPrivateKey(writer, privKey, spec, passwd);
412+
}
413+
else {
414+
PEMInputOutput.writeRSAPublicKey(writer, pubKey);
413415
}
414-
w.close();
415-
return getRuntime().newString(w.toString());
416-
} catch (NoClassDefFoundError ncdfe) {
416+
return RubyString.newString(getRuntime(), writer.getBuffer());
417+
}
418+
catch (NoClassDefFoundError ncdfe) {
417419
throw newRSAError(getRuntime(), bcExceptionMessage(ncdfe));
418-
} catch (IOException ioe) {
420+
}
421+
catch (IOException ioe) {
419422
throw newRSAError(getRuntime(), ioe.getMessage());
420423
}
421424
}

0 commit comments

Comments
 (0)