Search in sources :

Example 56 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_Key_EmptyKey_Failure.

public void testSign_SHA1withRSA_Key_EmptyKey_Failure() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(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 key is empty");
    } catch (InvalidKeyException expected) {
    }
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Example 57 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_SHA1withDSA_Key_Success.

public void testSign_SHA1withDSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(DSA_priv, DSA_P, DSA_Q, DSA_G);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initSign(privKey);
    sig.update(Vector2Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 58 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class SignatureTest method testSign_NONEwithRSA_Key_Success.

public void testSign_NONEwithRSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("NONEwithRSA");
    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, NONEwithRSA_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 : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 59 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class KeyManagerFactoryTest method test_X509KeyManager_alias.

private void test_X509KeyManager_alias(X509KeyManager km, String alias, String keyType, boolean many, boolean empty) throws Exception {
    if (empty || (!many && (keyType == null || keyType.isEmpty()))) {
        assertNull(keyType, alias);
        assertNull(keyType, km.getCertificateChain(alias));
        assertNull(keyType, km.getPrivateKey(alias));
        return;
    }
    assertNotNull(keyType, alias);
    X509Certificate[] certificateChain = km.getCertificateChain(alias);
    PrivateKey privateKey = km.getPrivateKey(alias);
    String keyAlgName;
    String sigAlgName;
    if (keyType == null) {
        keyAlgName = privateKey.getAlgorithm();
        sigAlgName = keyAlgName;
    } else {
        // potentially handle EC_EC or EC_RSA
        keyAlgName = TestKeyStore.keyAlgorithm(keyType);
        sigAlgName = TestKeyStore.signatureAlgorithm(keyType);
        X509Certificate certificate = certificateChain[0];
        assertEquals(keyType, keyAlgName, certificate.getPublicKey().getAlgorithm());
        assertEquals(keyType, keyAlgName, privateKey.getAlgorithm());
        // skip this for EC which could return EC_RSA case instead of EC_EC
        if (!keyType.equals("EC")) {
            String expectedSigAlgName = sigAlgName.toUpperCase();
            String actualSigAlgName = certificate.getSigAlgName().toUpperCase();
            String expected = actualSigAlgName + " contains " + expectedSigAlgName;
            assertTrue(expected, actualSigAlgName.contains(expectedSigAlgName));
        }
    }
    PrivateKeyEntry privateKeyEntry = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName);
    if (!"EC".equals(keyAlgName)) {
        assertEquals(keyType, Arrays.<Certificate>asList(privateKeyEntry.getCertificateChain()), Arrays.<Certificate>asList(certificateChain));
        assertEquals(keyType, privateKeyEntry.getPrivateKey(), privateKey);
    }
}
Also used : PrivateKey(java.security.PrivateKey) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) X509Certificate(java.security.cert.X509Certificate)

Example 60 with PrivateKey

use of java.security.PrivateKey in project robovm by robovm.

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually decrypting with private keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] encrypted = new byte[RSA_Vector1_Encrypt_Private.length];
    final int encryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, encryptLen);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    final int decryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, decryptLen);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Aggregations

PrivateKey (java.security.PrivateKey)517 X509Certificate (java.security.cert.X509Certificate)217 KeyFactory (java.security.KeyFactory)169 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)144 Certificate (java.security.cert.Certificate)127 PublicKey (java.security.PublicKey)120 ByteArrayInputStream (java.io.ByteArrayInputStream)118 KeyStore (java.security.KeyStore)93 CertificateFactory (java.security.cert.CertificateFactory)92 IOException (java.io.IOException)81 Key (java.security.Key)74 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)70 Entry (java.security.KeyStore.Entry)60 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)60 KeyPair (java.security.KeyPair)59 SecretKey (javax.crypto.SecretKey)48 InvalidKeyException (java.security.InvalidKeyException)47 KeyStoreException (java.security.KeyStoreException)46 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46