Search in sources :

Example 56 with KeyStoreException

use of java.security.KeyStoreException in project OpenAM by OpenRock.

the class STSCryptoProviderBase method loadKeystore.

private KeyStore loadKeystore() throws TokenCreationException {
    InputStream inputStream;
    try {
        inputStream = getKeystoreInputStream();
    } catch (FileNotFoundException e) {
        throw new TokenCreationException(ResourceException.BAD_REQUEST, "Could not find keystore file at location " + keystoreLocation + " neither on the filesystem, nor on the classpath.");
    }
    KeyStore keyStore;
    try {
        keyStore = KeyStore.getInstance(keystoreType);
    } catch (KeyStoreException e) {
        throw new TokenCreationException(ResourceException.INTERNAL_ERROR, "Could not get JKS keystore: " + e.getMessage(), e);
    }
    try {
        keyStore.load(inputStream, new String(keystorePassword, AMSTSConstants.UTF_8_CHARSET_ID).toCharArray());
        return keyStore;
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        throw new TokenCreationException(ResourceException.CONFLICT, "Could not load keystore at location " + keystoreLocation + ": " + e.getMessage(), e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TokenCreationException(org.forgerock.openam.sts.TokenCreationException) KeyStore(java.security.KeyStore)

Example 57 with KeyStoreException

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

the class SSLContextManager method getFingerPrint.

public String getFingerPrint(Certificate cert) throws KeyStoreException {
    if (!(cert instanceof X509Certificate)) {
        return null;
    }
    StringBuffer buff = new StringBuffer();
    X509Certificate x509 = (X509Certificate) cert;
    try {
        String fingerprint = Encoding.hashMD5(cert.getEncoded());
        for (int i = 0; i < fingerprint.length(); i += 2) {
            buff.append(fingerprint.substring(i, i + 1)).append(":");
        }
        buff.deleteCharAt(buff.length() - 1);
    } catch (CertificateEncodingException e) {
        throw new KeyStoreException(e.getMessage());
    }
    String dn = x509.getSubjectDN().getName();
    log.info("Fingerprint is " + buff.toString().toUpperCase());
    return buff.toString().toUpperCase() + " " + dn;
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate)

Example 58 with KeyStoreException

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

the class SSLContextManager method initMSCAPI.

public int initMSCAPI() throws KeyStoreException, NoSuchProviderException, IOException, NoSuchAlgorithmException, CertificateException {
    try {
        if (!isProviderAvailable("msks")) {
            return -1;
        }
        Provider mscapi = (Provider) Class.forName("se.assembla.jce.provider.ms.MSProvider").newInstance();
        Security.addProvider(mscapi);
        // init the key store
        KeyStore ks = KeyStore.getInstance("msks", "assembla");
        ks.load(null, null);
        return addKeyStore(ks, "Microsoft CAPI Store", null);
    } catch (Exception e) {
        log.error("Error instantiating the MSCAPI provider: " + e.getMessage(), e);
        return -1;
    }
}
Also used : KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) Provider(java.security.Provider)

Example 59 with KeyStoreException

use of java.security.KeyStoreException in project OpenAM by OpenRock.

the class AMKeyProvider method getPublicKey.

/**
     * Return java.security.PublicKey for the specified keyAlias.
     * @param keyAlias Key alias name
     * @return PublicKey which matches the keyAlias, return null if the PublicKey could not be found.
     */
public java.security.PublicKey getPublicKey(String keyAlias) {
    if (keyAlias == null || keyAlias.length() == 0) {
        return null;
    }
    java.security.PublicKey pkey = null;
    try {
        X509Certificate cert = (X509Certificate) ks.getCertificate(keyAlias);
        if (cert == null) {
            logger.error("Unable to retrieve certificate with alias '" + keyAlias + "' from keystore " + "'" + this.keystoreFile + "'");
            return null;
        }
        pkey = cert.getPublicKey();
    } catch (KeyStoreException e) {
        logger.error("Unable to get public key:" + keyAlias, e);
    }
    return pkey;
}
Also used : PublicKey(java.security.PublicKey) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate)

Example 60 with KeyStoreException

use of java.security.KeyStoreException in project OpenAM by OpenRock.

the class AMKeyProvider method getPrivateKey.

/**
     * Return the {@link java.security.PrivateKey} for the specified certAlias and encrypted private key password.
     * @param certAlias Certificate alias name
     * @param encryptedKeyPass The encrypted key password to use when getting the private certificate
     * @return PrivateKey which matches the certAlias, return null if the private key could not be found.
     */
public PrivateKey getPrivateKey(String certAlias, String encryptedKeyPass) {
    PrivateKey key = null;
    String keyPass = decodePassword(encryptedKeyPass);
    if (keyPass != null) {
        try {
            key = (PrivateKey) ks.getKey(certAlias, keyPass.toCharArray());
        } catch (KeyStoreException e) {
            logger.error(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            logger.error(e.getMessage());
        } catch (UnrecoverableKeyException e) {
            logger.error(e.getMessage());
        }
    } else {
        logger.error("AMKeyProvider.getPrivateKey: " + "null key password returned from decryption for certificate alias:" + certAlias + " The password maybe incorrect.");
    }
    return key;
}
Also used : PrivateKey(java.security.PrivateKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

KeyStoreException (java.security.KeyStoreException)381 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)211 IOException (java.io.IOException)179 CertificateException (java.security.cert.CertificateException)148 KeyStore (java.security.KeyStore)141 X509Certificate (java.security.cert.X509Certificate)112 UnrecoverableKeyException (java.security.UnrecoverableKeyException)95 Certificate (java.security.cert.Certificate)73 KeyManagementException (java.security.KeyManagementException)69 CertificateFactory (java.security.cert.CertificateFactory)39 SSLContext (javax.net.ssl.SSLContext)38 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)38 InputStream (java.io.InputStream)37 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)37 PrivateKey (java.security.PrivateKey)35 ByteArrayInputStream (java.io.ByteArrayInputStream)33 InvalidKeyException (java.security.InvalidKeyException)33 FileNotFoundException (java.io.FileNotFoundException)32 TrustManager (javax.net.ssl.TrustManager)30 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)28