Search in sources :

Example 41 with InvalidKeyException

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

the class RelaxedX509TrustManager method getTunnelSSLSocketFactory.

// ZAP: added new ServerSocketFaktory with support of dynamic SSL certificates
public SSLSocketFactory getTunnelSSLSocketFactory(String hostname) {
    //	KeyStore ks;
    try {
        SSLContext ctx = SSLContext.getInstance(SSL);
        // Normally "SunX509", "IbmX509"...
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        SslCertificateService scs = CachedSslCertifificateServiceImpl.getService();
        KeyStore ks = scs.createCertForHost(hostname);
        kmf.init(ks, SslCertificateService.PASSPHRASE);
        java.security.SecureRandom x = new java.security.SecureRandom();
        x.setSeed(System.currentTimeMillis());
        ctx.init(kmf.getKeyManagers(), null, x);
        SSLSocketFactory tunnelSSLFactory = createDecoratedServerSslSocketFactory(ctx.getSocketFactory());
        return tunnelSSLFactory;
    } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException | InvalidKeyException | NoSuchProviderException | SignatureException | IOException e) {
        // friendly way?
        throw new RuntimeException(e);
    }
}
Also used : SslCertificateService(org.parosproxy.paros.security.SslCertificateService) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) NoSuchProviderException(java.security.NoSuchProviderException)

Example 42 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class Cipher method init.

/**
     * Initializes this cipher instance with the public key from the specified
     * certificate and a source of randomness.
     * <p>
     * The cipher will be initialized for the specified operation (one of:
     * encryption, decryption, key wrapping or key unwrapping) depending on
     * {@code opmode}.
     * <p>
     * It the type of the certificate is X.509 and the certificate has a <i>key
     * usage</i> extension field marked as critical, the specified {@code
     * opmode} has the be enabled for this key, otherwise an {@code
     * InvalidKeyException} is thrown.
     * <p>
     * If this cipher instance needs any algorithm parameters that the key in
     * the certificate can not provide, the underlying implementation of this
     * cipher is supposed to generate the required parameters (using its
     * provider or random values). Random values are generated using {@code
     * random}.
     * <p>
     * When a cipher instance is initialized by a call to any of the {@code
     * init} methods, the state of the instance is overridden, means it is
     * equivalent to creating a new instance and calling it {@code init} method.
     *
     * @param opmode
     *            the operation this cipher instance should be initialized for
     *            (one of: {@code ENCRYPT_MODE}, {@code DECRYPT_MODE}, {@code
     *            WRAP_MODE} or {@code UNWRAP_MODE}).
     * @param certificate
     *            the certificate.
     * @param random
     *            the source of randomness to be used.
     * @throws InvalidKeyException
     *             if the public key in the certificate can not be used to
     *             initialize this cipher instance.
     */
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException {
    checkMode(opmode);
    if (certificate instanceof X509Certificate) {
        Set<String> ce = ((X509Certificate) certificate).getCriticalExtensionOIDs();
        boolean critical = false;
        if (ce != null && !ce.isEmpty()) {
            for (String oid : ce) {
                if (oid.equals("2.5.29.15")) {
                    // KeyUsage OID = 2.5.29.15
                    critical = true;
                    break;
                }
            }
            if (critical) {
                boolean[] keyUsage = ((X509Certificate) certificate).getKeyUsage();
                //                          decipherOnly     (8) }
                if (keyUsage != null) {
                    if (opmode == ENCRYPT_MODE && !keyUsage[3]) {
                        throw new InvalidKeyException("The public key in the certificate " + "cannot be used for ENCRYPT_MODE");
                    } else if (opmode == WRAP_MODE && !keyUsage[2]) {
                        throw new InvalidKeyException("The public key in the certificate " + "cannot be used for WRAP_MODE");
                    }
                }
            }
        }
    }
    //        FIXME InvalidKeyException
    //        if keysize exceeds the maximum allowable keysize
    //        (jurisdiction policy files)
    spiImpl.engineInit(opmode, certificate.getPublicKey(), random);
    mode = opmode;
}
Also used : InvalidKeyException(java.security.InvalidKeyException) X509Certificate(java.security.cert.X509Certificate)

Example 43 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class EncryptedPrivateKeyInfo method getKeySpec.

/**
     * Returns the {@code PKCS8EncodedKeySpec} object extracted from the
     * encrypted data.
     *
     * @param decryptKey
     *            the key to decrypt the encrypted data with.
     * @return the extracted {@code PKCS8EncodedKeySpec}.
     * @throws NoSuchAlgorithmException
     *             if no usable cipher can be found to decrypt the encrypted
     *             data.
     * @throws InvalidKeyException
     *             if {@code decryptKey} is not usable to decrypt the encrypted
     *             data.
     * @throws NullPointerException
     *             if {@code decryptKey} is {@code null}.
     */
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException {
    if (decryptKey == null) {
        throw new NullPointerException("decryptKey == null");
    }
    try {
        Cipher cipher = Cipher.getInstance(algName);
        if (algParameters == null) {
            cipher.init(Cipher.DECRYPT_MODE, decryptKey);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters);
        }
        byte[] decryptedData = cipher.doFinal(encryptedData);
        try {
            ASN1PrivateKeyInfo.verify(decryptedData);
        } catch (IOException e1) {
            throw invalidKey();
        }
        return new PKCS8EncodedKeySpec(decryptedData);
    } catch (NoSuchPaddingException e) {
        throw new NoSuchAlgorithmException(e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        throw new NoSuchAlgorithmException(e.getMessage());
    } catch (IllegalStateException e) {
        throw new InvalidKeyException(e.getMessage());
    } catch (IllegalBlockSizeException e) {
        throw new InvalidKeyException(e.getMessage());
    } catch (BadPaddingException e) {
        throw new InvalidKeyException(e.getMessage());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 44 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class SealedObject method getObject.

/**
     * Returns the wrapped object, decrypting it using the specified key.
     *
     * @param key
     *            the key to decrypt the data with.
     * @return the encapsulated object.
     * @throws IOException
     *             if deserialization fails.
     * @throws ClassNotFoundException
     *             if deserialization fails.
     * @throws NoSuchAlgorithmException
     *             if the algorithm to decrypt the data is not available.
     * @throws InvalidKeyException
     *             if the specified key cannot be used to decrypt the data.
     */
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    try {
        Cipher cipher = Cipher.getInstance(sealAlg);
        if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
            AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
            params.init(encodedParams);
            cipher.init(Cipher.DECRYPT_MODE, key, params);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key);
        }
        byte[] serialized = cipher.doFinal(encryptedContent);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
        return ois.readObject();
    } catch (NoSuchPaddingException e) {
        // with existing padding
        throw new NoSuchAlgorithmException(e.toString());
    } catch (InvalidAlgorithmParameterException e) {
        // with correct algorithm parameters
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalBlockSizeException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (BadPaddingException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalStateException e) {
        // should never be thrown because cipher is initialized
        throw new NoSuchAlgorithmException(e.toString());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameters(java.security.AlgorithmParameters) ObjectInputStream(java.io.ObjectInputStream)

Example 45 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class WrapCipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
            // try next spec
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

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