Skip to content

Commit 921dce5

Browse files
committed
added setAlgorithmMapping method with test on EC public key OID.
1 parent 00d40a5 commit 921dce5

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

pkix/src/main/java/org/bouncycastle/openssl/jcajce/JcaPEMKeyConverter.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@ public class JcaPEMKeyConverter
2929
{
3030
private JcaJceHelper helper = new DefaultJcaJceHelper();
3131

32-
private static final Map algorithms = new HashMap();
32+
private final Map algorithms = new HashMap();
33+
34+
private static final Map baseMappings = new HashMap();
3335

3436
static
3537
{
36-
algorithms.put(X9ObjectIdentifiers.id_ecPublicKey, "ECDSA");
37-
algorithms.put(PKCSObjectIdentifiers.rsaEncryption, "RSA");
38-
algorithms.put(X9ObjectIdentifiers.id_dsa, "DSA");
38+
baseMappings.put(X9ObjectIdentifiers.id_ecPublicKey, "ECDSA");
39+
baseMappings.put(PKCSObjectIdentifiers.rsaEncryption, "RSA");
40+
baseMappings.put(X9ObjectIdentifiers.id_dsa, "DSA");
3941
}
4042

43+
public JcaPEMKeyConverter()
44+
{
45+
this.algorithms.putAll(baseMappings);
46+
}
47+
4148
public JcaPEMKeyConverter setProvider(Provider provider)
4249
{
4350
this.helper = new ProviderJcaJceHelper(provider);
@@ -52,6 +59,27 @@ public JcaPEMKeyConverter setProvider(String providerName)
5259
return this;
5360
}
5461

62+
/**
63+
* Set the algorithm mapping for a particular OID to the given algorithm name.
64+
*
65+
* @param algOid object identifier used to identify the public/private key
66+
* @param algorithmName algorithm name we want to map to in the provider.
67+
* @return the current builder instance.
68+
*/
69+
public JcaPEMKeyConverter setAlgorithmMapping(ASN1ObjectIdentifier algOid, String algorithmName)
70+
{
71+
this.algorithms.put(algOid, algorithmName);
72+
73+
return this;
74+
}
75+
76+
/**
77+
* Convert a PEMKeyPair into a KeyPair, returning the converted result.
78+
*
79+
* @param keyPair the PEMKeyPair to be converted.
80+
* @return the result of the conversion
81+
* @throws PEMException if an exception is thrown attempting to do the conversion.
82+
*/
5583
public KeyPair getKeyPair(PEMKeyPair keyPair)
5684
throws PEMException
5785
{

pkix/src/test/java/org/bouncycastle/openssl/test/ParserTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
3535
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
3636
import org.bouncycastle.asn1.x9.X9ECParameters;
37+
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
3738
import org.bouncycastle.cert.X509CertificateHolder;
3839
import org.bouncycastle.jcajce.interfaces.EdDSAPrivateKey;
3940
import org.bouncycastle.jcajce.interfaces.EdDSAPublicKey;
@@ -164,6 +165,21 @@ public void performTest()
164165
fail("wrong algorithm name on private");
165166
}
166167

168+
//
169+
// Check for algorithm replacement
170+
//
171+
pair = new JcaPEMKeyConverter().setProvider("BC").setAlgorithmMapping(X9ObjectIdentifiers.id_ecPublicKey, "EC").getKeyPair(pemPair);
172+
173+
if (!pair.getPublic().getAlgorithm().equals("EC"))
174+
{
175+
fail("wrong algorithm name on public got: " + pair.getPublic().getAlgorithm());
176+
}
177+
178+
if (!pair.getPrivate().getAlgorithm().equals("EC"))
179+
{
180+
fail("wrong algorithm name on private");
181+
}
182+
167183
//
168184
// ECKey -- explicit parameters
169185
//

0 commit comments

Comments
 (0)