Search in sources :

Example 26 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project robovm by robovm.

the class ECDHKeyAgreementTest method testKnownAnswer.

/**
     * Performs a known-answer test of the shared secret for all permutations of {@code Providers}
     * of: first key pair, second key pair, and the {@code KeyAgreement}. This is to check that
     * the {@code KeyAgreement} instances work with keys of all registered providers.
     */
public void testKnownAnswer() throws Exception {
    for (Provider keyFactoryProvider1 : getKeyFactoryProviders()) {
        ECPrivateKey privateKey1 = getPrivateKey(KAT_PRIVATE_KEY1_PKCS8, keyFactoryProvider1);
        ECPublicKey publicKey1 = getPublicKey(KAT_PUBLIC_KEY1_X509, keyFactoryProvider1);
        for (Provider keyFactoryProvider2 : getKeyFactoryProviders()) {
            ECPrivateKey privateKey2 = getPrivateKey(KAT_PRIVATE_KEY2_PKCS8, keyFactoryProvider2);
            ECPublicKey publicKey2 = getPublicKey(KAT_PUBLIC_KEY2_X509, keyFactoryProvider2);
            for (Provider keyAgreementProvider : getKeyAgreementProviders()) {
                try {
                    testKnownAnswer(publicKey1, privateKey1, publicKey2, privateKey2, keyAgreementProvider);
                } catch (Throwable e) {
                    throw new RuntimeException(getClass().getSimpleName() + ".testKnownAnswer(" + keyFactoryProvider1.getName() + ", " + keyFactoryProvider2.getName() + ", " + keyAgreementProvider.getName() + ")", e);
                }
            }
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Provider(java.security.Provider)

Example 27 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project android_frameworks_base by AOSPA.

the class AndroidKeyPairGeneratorTest method assertKeyPairCorrect.

private void assertKeyPairCorrect(KeyPair pair, String alias, String keyType, int keySize, AlgorithmParameterSpec spec, X500Principal dn, BigInteger serial, Date start, Date end) throws Exception {
    final PublicKey pubKey = pair.getPublic();
    assertNotNull("The PublicKey for the KeyPair should be not null", pubKey);
    assertEquals(keyType, pubKey.getAlgorithm());
    if ("EC".equalsIgnoreCase(keyType)) {
        assertEquals("Curve should be what was specified during initialization", keySize, ((ECPublicKey) pubKey).getParams().getCurve().getField().getFieldSize());
    } else if ("RSA".equalsIgnoreCase(keyType)) {
        RSAPublicKey rsaPubKey = (RSAPublicKey) pubKey;
        assertEquals("Modulus size should be what is specified during initialization", (keySize + 7) & ~7, (rsaPubKey.getModulus().bitLength() + 7) & ~7);
        if (spec != null) {
            RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) spec;
            assertEquals((keySize + 7) & ~7, (params.getKeysize() + 7) & ~7);
            assertEquals(params.getPublicExponent(), rsaPubKey.getPublicExponent());
        }
    }
    final PrivateKey privKey = pair.getPrivate();
    assertNotNull("The PrivateKey for the KeyPair should be not null", privKey);
    assertEquals(keyType, privKey.getAlgorithm());
    if ("EC".equalsIgnoreCase(keyType)) {
        assertTrue("EC private key must be instanceof ECKey: " + privKey.getClass().getName(), privKey instanceof ECKey);
        assertEquals("Private and public key must have the same EC parameters", ((ECKey) pubKey).getParams(), ((ECKey) privKey).getParams());
    } else if ("RSA".equalsIgnoreCase(keyType)) {
        assertTrue("RSA private key must be instance of RSAKey: " + privKey.getClass().getName(), privKey instanceof RSAKey);
        assertEquals("Private and public key must have the same RSA modulus", ((RSAKey) pubKey).getModulus(), ((RSAKey) privKey).getModulus());
    }
    final byte[] userCertBytes = mAndroidKeyStore.get(Credentials.USER_CERTIFICATE + alias);
    assertNotNull("The user certificate should exist for the generated entry", userCertBytes);
    final CertificateFactory cf = CertificateFactory.getInstance("X.509");
    final Certificate userCert = cf.generateCertificate(new ByteArrayInputStream(userCertBytes));
    assertTrue("Certificate should be in X.509 format", userCert instanceof X509Certificate);
    final X509Certificate x509userCert = (X509Certificate) userCert;
    assertEquals("Public key used to sign certificate should have the same algorithm as in KeyPair", pubKey.getAlgorithm(), x509userCert.getPublicKey().getAlgorithm());
    assertEquals("PublicKey used to sign certificate should match one returned in KeyPair", pubKey, AndroidKeyStoreProvider.getAndroidKeyStorePublicKey(Credentials.USER_PRIVATE_KEY + alias, KeyStore.UID_SELF, x509userCert.getPublicKey().getAlgorithm(), x509userCert.getPublicKey().getEncoded()));
    assertEquals("The Subject DN should be the one passed into the params", dn, x509userCert.getSubjectDN());
    assertEquals("The Issuer DN should be the same as the Subject DN", dn, x509userCert.getIssuerDN());
    assertEquals("The Serial should be the one passed into the params", serial, x509userCert.getSerialNumber());
    assertDateEquals("The notBefore date should be the one passed into the params", start, x509userCert.getNotBefore());
    assertDateEquals("The notAfter date should be the one passed into the params", end, x509userCert.getNotAfter());
    // Assert that the cert's signature verifies using the public key from generated KeyPair
    x509userCert.verify(pubKey);
    // Assert that the cert's signature verifies using the public key from the cert itself.
    x509userCert.verify(x509userCert.getPublicKey());
    final byte[] caCerts = mAndroidKeyStore.get(Credentials.CA_CERTIFICATE + alias);
    assertNull("A list of CA certificates should not exist for the generated entry", caCerts);
    ExportResult exportResult = mAndroidKeyStore.exportKey(Credentials.USER_PRIVATE_KEY + alias, KeymasterDefs.KM_KEY_FORMAT_X509, null, null);
    assertEquals(KeyStore.NO_ERROR, exportResult.resultCode);
    final byte[] pubKeyBytes = exportResult.exportData;
    assertNotNull("The keystore should return the public key for the generated key", pubKeyBytes);
    assertTrue("Public key X.509 format should be as expected", Arrays.equals(pubKey.getEncoded(), pubKeyBytes));
}
Also used : RSAKey(java.security.interfaces.RSAKey) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) ECKey(java.security.interfaces.ECKey) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) ExportResult(android.security.keymaster.ExportResult)

Example 28 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project XobotOS by xamarin.

the class Signature method engineInitVerify.

protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    CipherParameters param;
    if (publicKey instanceof ECPublicKey) {
        param = ECUtil.generatePublicKeyParameter(publicKey);
    } else {
        try {
            byte[] bytes = publicKey.getEncoded();
            publicKey = JDKKeyFactory.createPublicKeyFromDERStream(bytes);
            if (publicKey instanceof ECPublicKey) {
                param = ECUtil.generatePublicKeyParameter(publicKey);
            } else {
                throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
            }
        } catch (Exception e) {
            throw new InvalidKeyException("can't recognise key type in ECDSA based signer");
        }
    }
    digest.reset();
    signer.init(false, param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPublicKey(java.security.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException)

Example 29 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project android_frameworks_base by DirtyUnicorns.

the class AndroidKeyStoreProvider method getAndroidKeyStorePublicKey.

@NonNull
public static AndroidKeyStorePublicKey getAndroidKeyStorePublicKey(@NonNull String alias, int uid, @NonNull @KeyProperties.KeyAlgorithmEnum String keyAlgorithm, @NonNull byte[] x509EncodedForm) {
    PublicKey publicKey;
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(x509EncodedForm));
    } catch (NoSuchAlgorithmException e) {
        throw new ProviderException("Failed to obtain " + keyAlgorithm + " KeyFactory", e);
    } catch (InvalidKeySpecException e) {
        throw new ProviderException("Invalid X.509 encoding of public key", e);
    }
    if (KeyProperties.KEY_ALGORITHM_EC.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreECPublicKey(alias, uid, (ECPublicKey) publicKey);
    } else if (KeyProperties.KEY_ALGORITHM_RSA.equalsIgnoreCase(keyAlgorithm)) {
        return new AndroidKeyStoreRSAPublicKey(alias, uid, (RSAPublicKey) publicKey);
    } else {
        throw new ProviderException("Unsupported Android Keystore public key algorithm: " + keyAlgorithm);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ProviderException(java.security.ProviderException) NoSuchProviderException(java.security.NoSuchProviderException) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) NonNull(android.annotation.NonNull)

Example 30 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project jdk8u_jdk by JetBrains.

the class ClientHandshaker method serverKeyExchange.

private void serverKeyExchange(ECDH_ServerKeyExchange mesg) throws IOException {
    if (debug != null && Debug.isOn("handshake")) {
        mesg.print(System.out);
    }
    ECPublicKey key = mesg.getPublicKey();
    ecdh = new ECDHCrypt(key.getParams(), sslContext.getSecureRandom());
    ephemeralServerKey = key;
    // check constraints of EC PublicKey
    if (!algorithmConstraints.permits(EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), ephemeralServerKey)) {
        throw new SSLHandshakeException("ECDH ServerKeyExchange " + "does not comply to algorithm constraints");
    }
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey)

Aggregations

ECPublicKey (java.security.interfaces.ECPublicKey)35 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 PublicKey (java.security.PublicKey)13 KeyFactory (java.security.KeyFactory)11 KeyPairGenerator (java.security.KeyPairGenerator)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 ECPrivateKey (java.security.interfaces.ECPrivateKey)8 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)8 PrivateKey (java.security.PrivateKey)7 X509Certificate (java.security.cert.X509Certificate)7 ECParameterSpec (java.security.spec.ECParameterSpec)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 NonNull (android.annotation.NonNull)5 ExportResult (android.security.keymaster.ExportResult)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 KeyPair (java.security.KeyPair)5 NoSuchProviderException (java.security.NoSuchProviderException)5 ProviderException (java.security.ProviderException)5 Certificate (java.security.cert.Certificate)5 CertificateFactory (java.security.cert.CertificateFactory)5