Search in sources :

Example 46 with RSAPrivateCrtKeySpec

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;
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 47 with RSAPrivateCrtKeySpec

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;
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) Ruby(org.jruby.Ruby) KeyFactory(java.security.KeyFactory)

Example 48 with RSAPrivateCrtKeySpec

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);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey) SneakyThrows(lombok.SneakyThrows)

Example 49 with RSAPrivateCrtKeySpec

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;
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) CertificateFactory(java.security.cert.CertificateFactory) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) KeyFactory(java.security.KeyFactory)

Example 50 with RSAPrivateCrtKeySpec

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);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) BigInteger(java.math.BigInteger) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)51 KeyFactory (java.security.KeyFactory)18 BigInteger (java.math.BigInteger)16 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 PrivateKey (java.security.PrivateKey)11 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)10 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)8 PublicKey (java.security.PublicKey)7 KeySpec (java.security.spec.KeySpec)6 IOException (java.io.IOException)5 KeyPair (java.security.KeyPair)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 GeneralSecurityException (java.security.GeneralSecurityException)4 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)4