Search in sources :

Example 1 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project intellij-community by JetBrains.

the class Asn1Object method read.

private static PrivateKey read(String fileName) throws IOException {
    KeyFactory factory;
    try {
        factory = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("JCE error: " + e.getMessage());
    }
    List<String> lines = FileUtilRt.loadLines(fileName, "UTF-8");
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        if (line.contains(P1_BEGIN_MARKER)) {
            List<String> strings = lines.subList(i + 1, lines.size());
            byte[] keyBytes = readKeyMaterial(P1_END_MARKER, strings);
            RSAPrivateCrtKeySpec keySpec = getRSAKeySpec(keyBytes);
            try {
                return factory.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                throw new IOException("Invalid PKCS#1 PEM file: " + e.getMessage());
            }
        }
        if (line.contains(P8_BEGIN_MARKER)) {
            List<String> strings = lines.subList(i + 1, lines.size());
            byte[] keyBytes = readKeyMaterial(P8_END_MARKER, strings);
            EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            try {
                return factory.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                throw new IOException("Invalid PKCS#8 PEM file: " + e.getMessage());
            }
        }
    }
    throw new IOException("Invalid PEM file: no begin marker");
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 2 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success.

public void testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, RSA_2048_privateExponent, null, null, null, null, null);
    // The RI fails on this key which is totally unreasonable.
    final PrivateKey privKey;
    try {
        privKey = kf.generatePrivate(keySpec);
    } catch (NullPointerException e) {
        if (StandardNames.IS_RI) {
            return;
        } else {
            fail("Private key should be created");
            return;
        }
    }
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(privKey);
    sig.update(Vector1Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, SHA1withRSA_Vector1Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector1Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 3 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure.

public void testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, null, RSA_2048_primeP, RSA_2048_primeQ, null, null, null);
    // Failing on this key early is okay.
    final PrivateKey privKey;
    try {
        privKey = kf.generatePrivate(keySpec);
    } catch (NullPointerException e) {
        return;
    } catch (InvalidKeySpecException e) {
        return;
    }
    Signature sig = Signature.getInstance("SHA1withRSA");
    try {
        sig.initSign(privKey);
        fail("Should throw error when private exponent is not available");
    } catch (InvalidKeyException expected) {
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Example 4 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.

the class RSAPrivateCrtKeySpecTest method testGetPrimeExponentP.

/**
     * Test for <code>getPrimeExponentP()</code> method<br>
     * Assertion: returns prime exponent P
     */
public final void testGetPrimeExponentP() {
    RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE);
    assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeExponentP()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 5 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.

the class RSAPrivateCrtKeySpecTest method testGetCrtCoefficient.

/**
     * Test for <code>getCrtCoefficient()</code> method<br>
     * Assertion: returns crt coefficient
     */
public final void testGetCrtCoefficient() {
    RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L));
    assertTrue(BigInteger.valueOf(5L).equals(ks.getCrtCoefficient()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Aggregations

RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)48 KeyFactory (java.security.KeyFactory)16 BigInteger (java.math.BigInteger)14 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 PrivateKey (java.security.PrivateKey)11 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 PublicKey (java.security.PublicKey)7 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)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