use of java.security.spec.RSAPrivateCrtKeySpec in project qpid-broker-j by apache.
the class SSLUtil method readPrivateKey.
public static PrivateKey readPrivateKey(final byte[] content, final String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException {
PrivateKey key;
try {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(content);
KeyFactory kf = KeyFactory.getInstance(algorithm);
key = kf.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
// not in PCKS#8 format - try parsing as PKCS#1
RSAPrivateCrtKeySpec keySpec = getRSAKeySpec(content);
KeyFactory kf = KeyFactory.getInstance(algorithm);
try {
key = kf.generatePrivate(keySpec);
} catch (InvalidKeySpecException e2) {
throw new InvalidKeySpecException("Cannot parse the provided key as either PKCS#1 or PCKS#8 format");
}
}
return key;
}
use of java.security.spec.RSAPrivateCrtKeySpec in project jruby-openssl by jruby.
the class PKeyRSA method generatePrivateKeyIfParams.
private void generatePrivateKeyIfParams(final ThreadContext context) {
final Ruby runtime = context.runtime;
if (privateKey != null)
throw newRSAError(runtime, "illegal modification");
// Don't access the rsa_n and rsa_e fields directly. They may have
// already been consumed and cleared by generatePublicKeyIfParams.
BigInteger _rsa_n = getModulus();
BigInteger _rsa_e = getPublicExponent();
if (_rsa_n != null && _rsa_e != null && rsa_p != null && rsa_q != null && rsa_d != null && rsa_dmp1 != null && rsa_dmq1 != null && rsa_iqmp != null) {
final KeyFactory rsaFactory;
try {
rsaFactory = SecurityHelper.getKeyFactory("RSA");
} catch (NoSuchAlgorithmException e) {
throw runtime.newLoadError("unsupported key algorithm (RSA)");
}
try {
privateKey = (RSAPrivateCrtKey) rsaFactory.generatePrivate(new RSAPrivateCrtKeySpec(_rsa_n, _rsa_e, rsa_d, rsa_p, rsa_q, rsa_dmp1, rsa_dmq1, rsa_iqmp));
} catch (InvalidKeySpecException e) {
throw newRSAError(runtime, "invalid parameters");
}
rsa_n = null;
rsa_e = null;
rsa_d = null;
rsa_p = null;
rsa_q = null;
rsa_dmp1 = null;
rsa_dmq1 = null;
rsa_iqmp = null;
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project faf-java-server by FAForever.
the class RsaHelper method readPkcs1.
/**
* Reads a specified PKCS#1 formatted key (without any headers or footers). Having the key in PKCS#8 format would be
* easier as bouncy castle provides a one-liner to read it but since the original FAF server had its key in PKCS#1,
* this method allows to just use the same key string instead of having to convert it.
*/
@SneakyThrows
RSAPrivateCrtKey readPkcs1(String content) {
ASN1Sequence seq = ASN1Sequence.getInstance(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8)));
Assert.notNull(seq, "RSA private key has not been specified properly. Value is '" + content + "'.");
Assert.isTrue(seq.size() == 9, "Invalid RSA Private Key ASN1 sequence.");
RSAPrivateKey key = RSAPrivateKey.getInstance(seq);
RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient());
return (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate(privSpec);
}
use of java.security.spec.RSAPrivateCrtKeySpec in project fabric8 by jboss-fuse.
the class WebClients method createKeyStore.
public static KeyStore createKeyStore(String clientCertData, File clientCertFile, String clientKeyData, File clientKeyFile, String clientKeyAlgo, char[] clientKeyPassword) throws Exception {
try (InputStream certInputStream = getInputStreamFromDataOrFile(clientCertData, clientCertFile)) {
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream);
InputStream keyInputStream = getInputStreamFromDataOrFile(clientKeyData, clientKeyFile);
PEMReader reader = new PEMReader(keyInputStream);
RSAPrivateCrtKeySpec keySpec = new PKCS1EncodedKeySpec(reader.getDerBytes()).getKeySpec();
KeyFactory kf = KeyFactory.getInstance(clientKeyAlgo);
RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null, clientKeyPassword);
String alias = cert.getSubjectX500Principal().getName();
keyStore.setKeyEntry(alias, privKey, clientKeyPassword, new Certificate[] { cert });
return keyStore;
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project thingsboard by thingsboard.
the class CertPemCredentials method decodeRSAPrivatePKCS1.
static RSAPrivateCrtKeySpec decodeRSAPrivatePKCS1(byte[] encoded) {
ByteBuffer input = ByteBuffer.wrap(encoded);
if (der(input, 0x30) != input.remaining())
throw new IllegalArgumentException("Excess data");
if (!BigInteger.ZERO.equals(derint(input)))
throw new IllegalArgumentException("Unsupported version");
BigInteger n = derint(input);
BigInteger e = derint(input);
BigInteger d = derint(input);
BigInteger p = derint(input);
BigInteger q = derint(input);
BigInteger ep = derint(input);
BigInteger eq = derint(input);
BigInteger c = derint(input);
return new RSAPrivateCrtKeySpec(n, e, d, p, q, ep, eq, c);
}
Aggregations