Skip to content

Commit 382a829

Browse files
committed
[refactor] PKey read-er methods and return types
1 parent 585fb89 commit 382a829

File tree

3 files changed

+27
-42
lines changed

3 files changed

+27
-42
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,12 @@ protected static boolean ttySTDIN(final ThreadContext context) {
431431
catch (RaiseException ex) { return false; }
432432
}
433433

434-
static Object readPrivateKey(final String str, final char[] passwd)
434+
static KeyPair readPrivateKey(final String str, final char[] passwd)
435435
throws PEMInputOutput.PasswordRequiredException, IOException {
436436
return PEMInputOutput.readPrivateKey(new StringReader(str), passwd);
437437
}
438438

439-
static Object readPrivateKey(final RubyString str, final char[] passwd)
439+
static KeyPair readPrivateKey(final RubyString str, final char[] passwd)
440440
throws PEMInputOutput.PasswordRequiredException, IOException {
441441
return readPrivateKey(str.toString(), passwd);
442442
}

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@
8888
public class PKey {
8989

9090
public static KeyPair readPrivateKey(final byte[] input, final String type)
91+
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
92+
return readPrivateKey((ASN1Sequence) new ASN1InputStream(input).readObject(), type);
93+
}
94+
95+
public static KeyPair readPrivateKey(final ASN1Sequence seq, final String type)
9196
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
9297
KeySpec pubSpec; KeySpec privSpec;
93-
ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
9498
if ( type.equals("RSA") ) {
9599
ASN1Integer mod = (ASN1Integer) seq.getObjectAt(1);
96100
ASN1Integer pubExp = (ASN1Integer) seq.getObjectAt(2);
@@ -114,7 +118,7 @@ else if ( type.equals("DSA") ) {
114118
pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(), g.getValue());
115119
}
116120
else if ( type.equals("EC") ) {
117-
return readECPrivateKey(input);
121+
return readECPrivateKey(SecurityHelper.getKeyFactory("EC"), seq);
118122
}
119123
else {
120124
throw new IllegalStateException("unsupported type: " + type);
@@ -123,29 +127,6 @@ else if ( type.equals("EC") ) {
123127
return new KeyPair(fact.generatePublic(pubSpec), fact.generatePrivate(privSpec));
124128
}
125129

126-
// d2i_PrivateKey_bio
127-
public static KeyPair readPrivateKey(byte[] input) throws IOException,
128-
NoSuchAlgorithmException, InvalidKeySpecException {
129-
KeyPair key = null;
130-
try {
131-
key = readRSAPrivateKey(input);
132-
}
133-
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
134-
catch (InvalidKeySpecException e) {
135-
// ignore
136-
}
137-
if (key == null) {
138-
try {
139-
key = readDSAPrivateKey(input);
140-
}
141-
catch (NoSuchAlgorithmException e) { throw e; /* should not happen */ }
142-
catch (InvalidKeySpecException e) {
143-
// ignore
144-
}
145-
}
146-
return key;
147-
}
148-
149130
// d2i_PUBKEY_bio
150131
public static PublicKey readPublicKey(byte[] input) throws IOException,
151132
NoSuchAlgorithmException, InvalidKeySpecException {
@@ -281,22 +262,26 @@ public static KeyPair readECPrivateKey(final byte[] input)
281262
return readECPrivateKey(SecurityHelper.getKeyFactory("EC"), input);
282263
}
283264

284-
public static KeyPair readECPrivateKey(final KeyFactory ecFactory, final byte[] input)
265+
public static KeyPair readECPrivateKey(final KeyFactory keyFactory, final byte[] input)
266+
throws IOException, InvalidKeySpecException {
267+
return readECPrivateKey(keyFactory, (ASN1Sequence) ASN1Primitive.fromByteArray(input));
268+
}
269+
270+
public static KeyPair readECPrivateKey(final KeyFactory keyFactory, final ASN1Sequence input)
285271
throws IOException, InvalidKeySpecException {
286272
try {
287-
org.bouncycastle.asn1.sec.ECPrivateKey pKey = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(ASN1Primitive.fromByteArray(input));
273+
org.bouncycastle.asn1.sec.ECPrivateKey pKey = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(input);
288274
AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, pKey.getParametersObject().toASN1Primitive());
289275
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey.toASN1Primitive());
290276
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pKey.getPublicKey().getBytes());
291277
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privInfo.getEncoded());
292278
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubInfo.getEncoded());
293-
//KeyFactory fact = KeyFactory.getInstance("EC", provider);
294279

295-
ECPrivateKey privateKey = (ECPrivateKey) ecFactory.generatePrivate(privSpec);
280+
ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(privSpec);
296281
if ( algId.getParameters() instanceof ASN1ObjectIdentifier ) {
297282
privateKey = ECPrivateKeyWithName.wrap(privateKey, (ASN1ObjectIdentifier) algId.getParameters());
298283
}
299-
return new KeyPair(ecFactory.generatePublic(pubSpec), privateKey);
284+
return new KeyPair(keyFactory.generatePublic(pubSpec), privateKey);
300285
}
301286
catch (ClassCastException ex) {
302287
throw new IOException("wrong ASN.1 object found in stream", ex);

src/main/java/org/jruby/ext/openssl/x509store/PEMInputOutput.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,23 +1486,23 @@ private static CMSSignedData readPKCS7(BufferedReader in, char[] p, String endMa
14861486

14871487
public static KeyFactory getKeyFactory(final AlgorithmIdentifier algId)
14881488
throws NoSuchAlgorithmException {
1489+
return SecurityHelper.getKeyFactory(getPrivateKeyType(algId));
1490+
}
14891491

1492+
private static String getPrivateKeyType(final AlgorithmIdentifier algId) {
14901493
final ASN1ObjectIdentifier algIdentifier = algId.getAlgorithm();
14911494

1492-
String algorithm = null;
1493-
if ( X9ObjectIdentifiers.id_ecPublicKey.equals(algIdentifier) ) {
1494-
algorithm = "EC";
1495+
if (X9ObjectIdentifiers.id_ecPublicKey.equals(algIdentifier)) {
1496+
return "EC";
14951497
}
1496-
else if ( PKCSObjectIdentifiers.rsaEncryption.equals(algIdentifier) ) {
1497-
algorithm = "RSA";
1498+
if (PKCSObjectIdentifiers.rsaEncryption.equals(algIdentifier)) {
1499+
return "RSA";
14981500
}
1499-
else if ( X9ObjectIdentifiers.id_dsa.equals(algIdentifier) ) {
1500-
algorithm = "DSA";
1501+
if (X9ObjectIdentifiers.id_dsa.equals(algIdentifier)) {
1502+
return "DSA";
15011503
}
15021504

1503-
if ( algorithm == null ) algorithm = algIdentifier.getId();
1504-
1505-
return SecurityHelper.getKeyFactory(algorithm);
1505+
return algIdentifier.getId();
15061506
}
15071507

15081508
private static CertificateFactory getX509CertificateFactory() {

0 commit comments

Comments
 (0)