summaryrefslogtreecommitdiffstats
path: root/bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2016-01-27 20:40:41 +0000
committerSergio Giro <sgiro@google.com>2016-01-28 15:30:59 +0000
commit80261dd2d1824bb3862e90e77a5412d56ad88b1f (patch)
treed89e670054247d0a050ac1b0d9a7918cbe3498d6 /bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java
parent9be78fe4c709f1e585b5ed7e99b21084045b7fba (diff)
downloadandroid_external_bouncycastle-80261dd2d1824bb3862e90e77a5412d56ad88b1f.tar.gz
android_external_bouncycastle-80261dd2d1824bb3862e90e77a5412d56ad88b1f.tar.bz2
android_external_bouncycastle-80261dd2d1824bb3862e90e77a5412d56ad88b1f.zip
bouncycastle: Android tree with upstream code for version 1.50
Android tree as of c0d8909a6c6a4ac075a9dee7ac1fe6baff34acc0 Change-Id: I8d381554d6edec32aae8ff5bab5d5314f0954440
Diffstat (limited to 'bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java')
-rw-r--r--bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java58
1 files changed, 34 insertions, 24 deletions
diff --git a/bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java b/bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java
index 4d55aa3..d332ca1 100644
--- a/bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java
+++ b/bcpkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java
@@ -2,13 +2,20 @@ package org.bouncycastle.openssl.jcajce;
import java.security.KeyFactory;
import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
+import java.util.HashMap;
+import java.util.Map;
+import org.bouncycastle.asn1.ASN1ObjectIdentifier;
+import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
+import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.jcajce.DefaultJcaJceHelper;
@@ -22,6 +29,15 @@ public class JcaPEMKeyConverter
{
private JcaJceHelper helper = new DefaultJcaJceHelper();
+ private static final Map algorithms = new HashMap();
+
+ static
+ {
+ algorithms.put(X9ObjectIdentifiers.id_ecPublicKey, "ECDSA");
+ algorithms.put(PKCSObjectIdentifiers.rsaEncryption, "RSA");
+ algorithms.put(X9ObjectIdentifiers.id_dsa, "DSA");
+ }
+
public JcaPEMKeyConverter setProvider(Provider provider)
{
this.helper = new ProviderJcaJceHelper(provider);
@@ -41,14 +57,7 @@ public class JcaPEMKeyConverter
{
try
{
- String algorithm = keyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm().getAlgorithm().getId();
-
- if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
- {
- algorithm = "ECDSA";
- }
-
- KeyFactory keyFactory = helper.createKeyFactory(algorithm);
+ KeyFactory keyFactory = getKeyFactory(keyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm());
return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(keyPair.getPublicKeyInfo().getEncoded())),
keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded())));
@@ -64,14 +73,7 @@ public class JcaPEMKeyConverter
{
try
{
- String algorithm = publicKeyInfo.getAlgorithm().getAlgorithm().getId();
-
- if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
- {
- algorithm = "ECDSA";
- }
-
- KeyFactory keyFactory = helper.createKeyFactory(algorithm);
+ KeyFactory keyFactory = getKeyFactory(publicKeyInfo.getAlgorithm());
return keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyInfo.getEncoded()));
}
@@ -86,14 +88,7 @@ public class JcaPEMKeyConverter
{
try
{
- String algorithm = privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm().getId();
-
- if (X9ObjectIdentifiers.id_ecPublicKey.getId().equals(algorithm))
- {
- algorithm = "ECDSA";
- }
-
- KeyFactory keyFactory = helper.createKeyFactory(algorithm);
+ KeyFactory keyFactory = getKeyFactory(privateKeyInfo.getPrivateKeyAlgorithm());
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()));
}
@@ -102,4 +97,19 @@ public class JcaPEMKeyConverter
throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
}
}
+
+ private KeyFactory getKeyFactory(AlgorithmIdentifier algId)
+ throws NoSuchAlgorithmException, NoSuchProviderException
+ {
+ ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
+
+ String algName = (String)algorithms.get(algorithm);
+
+ if (algName == null)
+ {
+ algName = algorithm.getId();
+ }
+
+ return helper.createKeyFactory(algName);
+ }
}