Search in sources :

Example 61 with InvalidKeyException

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

the class OpenSSLSocketImpl method setCertificate.

private void setCertificate(String alias) throws CertificateEncodingException, SSLException {
    if (alias == null) {
        return;
    }
    PrivateKey privateKey = sslParameters.getKeyManager().getPrivateKey(alias);
    if (privateKey == null) {
        return;
    }
    X509Certificate[] certificates = sslParameters.getKeyManager().getCertificateChain(alias);
    if (certificates == null) {
        return;
    }
    // Note that OpenSSL says to use SSL_use_certificate before SSL_use_PrivateKey.
    byte[][] certificateBytes = NativeCrypto.encodeCertificates(certificates);
    NativeCrypto.SSL_use_certificate(sslNativePointer, certificateBytes);
    try {
        final OpenSSLKey key = OpenSSLKey.fromPrivateKey(privateKey);
        NativeCrypto.SSL_use_PrivateKey(sslNativePointer, key.getPkeyContext());
    } catch (InvalidKeyException e) {
        throw new SSLException(e);
    }
    // checks the last installed private key and certificate,
    // so need to do this once per loop iteration
    NativeCrypto.SSL_check_private_key(sslNativePointer);
}
Also used : PrivateKey(java.security.PrivateKey) InvalidKeyException(java.security.InvalidKeyException) SSLException(javax.net.ssl.SSLException) X509Certificate(java.security.cert.X509Certificate)

Example 62 with InvalidKeyException

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

the class OpenSSLCipher method engineInitInternal.

private void engineInitInternal(int opmode, Key key, byte[] iv, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
        encrypting = true;
    } else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
        encrypting = false;
    } else {
        throw new InvalidParameterException("Unsupported opmode " + opmode);
    }
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Only SecretKey is supported");
    }
    final byte[] encodedKey = key.getEncoded();
    if (encodedKey == null) {
        throw new InvalidKeyException("key.getEncoded() == null");
    }
    checkSupportedKeySize(encodedKey.length);
    final long cipherType = NativeCrypto.EVP_get_cipherbyname(getCipherName(encodedKey.length, mode));
    if (cipherType == 0) {
        throw new InvalidAlgorithmParameterException("Cannot find name for key length = " + (encodedKey.length * 8) + " and mode = " + mode);
    }
    final int ivLength = NativeCrypto.EVP_CIPHER_iv_length(cipherType);
    if (iv == null && ivLength != 0) {
        iv = new byte[ivLength];
        if (encrypting) {
            if (random == null) {
                random = new SecureRandom();
            }
            random.nextBytes(iv);
        }
    } else if (iv != null && iv.length != ivLength) {
        throw new InvalidAlgorithmParameterException("expected IV length of " + ivLength);
    }
    this.iv = iv;
    if (supportsVariableSizeKey()) {
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, null, null, encrypting);
        NativeCrypto.EVP_CIPHER_CTX_set_key_length(cipherCtx.getContext(), encodedKey.length);
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), 0, encodedKey, iv, encrypting);
    } else {
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, encodedKey, iv, encrypting);
    }
    // OpenSSL only supports PKCS5 Padding.
    NativeCrypto.EVP_CIPHER_CTX_set_padding(cipherCtx.getContext(), padding == Padding.PKCS5PADDING);
    modeBlockSize = NativeCrypto.EVP_CIPHER_CTX_block_size(cipherCtx.getContext());
    calledUpdate = false;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException)

Example 63 with InvalidKeyException

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

the class OpenSSLECKeyFactory method engineTranslateKey.

@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    if ((key instanceof OpenSSLECPublicKey) || (key instanceof OpenSSLECPrivateKey)) {
        return key;
    } else if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey) key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        try {
            return engineGeneratePublic(new ECPublicKeySpec(w, params));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof ECPrivateKey) {
        ECPrivateKey ecKey = (ECPrivateKey) key;
        BigInteger s = ecKey.getS();
        ECParameterSpec params = ecKey.getParams();
        try {
            return engineGeneratePrivate(new ECPrivateKeySpec(s, params));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePublic(new X509EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Key must be EC public or private key; was " + key.getClass().getName());
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeyException(java.security.InvalidKeyException) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 64 with InvalidKeyException

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

the class OpenSSLEngine method getSecretKeyById.

public SecretKey getSecretKeyById(String id, String algorithm) throws InvalidKeyException {
    if (id == null) {
        throw new NullPointerException("id == null");
    }
    final long keyRef = NativeCrypto.ENGINE_load_private_key(ctx, id);
    if (keyRef == 0) {
        return null;
    }
    OpenSSLKey pkey = new OpenSSLKey(keyRef, this, id);
    try {
        return pkey.getSecretKey(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new InvalidKeyException(e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 65 with InvalidKeyException

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

the class EncryptedPrivateKeyInfoTest method testGetKeySpecKey01.

public final void testGetKeySpecKey01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            try {
                // check that method under test throws NPE
                epki.getKeySpec((Key) null);
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            } catch (InvalidKeyException e) {
                fail(getName() + "Unexpected exception: " + e);
            }
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33