blob: 8fa2b93edd1a9eacb935385e4a08b382920b4571 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package org.spongycastle.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.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.spongycastle.asn1.pkcs.PrivateKeyInfo;
import org.spongycastle.asn1.x509.AlgorithmIdentifier;
import org.spongycastle.asn1.x509.SubjectPublicKeyInfo;
import org.spongycastle.asn1.x9.X9ObjectIdentifiers;
import org.spongycastle.jcajce.DefaultJcaJceHelper;
import org.spongycastle.jcajce.JcaJceHelper;
import org.spongycastle.jcajce.NamedJcaJceHelper;
import org.spongycastle.jcajce.ProviderJcaJceHelper;
import org.spongycastle.openssl.PEMException;
import org.spongycastle.openssl.PEMKeyPair;
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);
return this;
}
public JcaPEMKeyConverter setProvider(String providerName)
{
this.helper = new NamedJcaJceHelper(providerName);
return this;
}
public KeyPair getKeyPair(PEMKeyPair keyPair)
throws PEMException
{
try
{
KeyFactory keyFactory = getKeyFactory(keyPair.getPrivateKeyInfo().getPrivateKeyAlgorithm());
return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(keyPair.getPublicKeyInfo().getEncoded())),
keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded())));
}
catch (Exception e)
{
throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
}
}
public PublicKey getPublicKey(SubjectPublicKeyInfo publicKeyInfo)
throws PEMException
{
try
{
KeyFactory keyFactory = getKeyFactory(publicKeyInfo.getAlgorithm());
return keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyInfo.getEncoded()));
}
catch (Exception e)
{
throw new PEMException("unable to convert key pair: " + e.getMessage(), e);
}
}
public PrivateKey getPrivateKey(PrivateKeyInfo privateKeyInfo)
throws PEMException
{
try
{
KeyFactory keyFactory = getKeyFactory(privateKeyInfo.getPrivateKeyAlgorithm());
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()));
}
catch (Exception e)
{
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);
}
}
|