Search in sources :

Example 26 with DestroyFailedException

use of javax.security.auth.DestroyFailedException in project cxf by apache.

the class AbstractXmlEncInHandler method decryptPayload.

protected byte[] decryptPayload(Element root, byte[] secretKeyBytes, String symEncAlgo) throws WSSecurityException {
    SecretKey key = KeyUtils.prepareSecretKey(symEncAlgo, secretKeyBytes);
    try {
        XMLCipher xmlCipher = EncryptionUtils.initXMLCipher(symEncAlgo, XMLCipher.DECRYPT_MODE, key);
        byte[] decryptedContent = xmlCipher.decryptToByteArray(root);
        // Clean the private key from memory now that we're finished with it
        try {
            key.destroy();
        } catch (DestroyFailedException ex) {
        // ignore
        }
        return decryptedContent;
    } catch (XMLEncryptionException ex) {
        throw new WSSecurityException(WSSecurityException.ErrorCode.UNSUPPORTED_ALGORITHM, ex);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) DestroyFailedException(javax.security.auth.DestroyFailedException) XMLCipher(org.apache.xml.security.encryption.XMLCipher) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 27 with DestroyFailedException

use of javax.security.auth.DestroyFailedException in project cxf by apache.

the class AbstractXmlEncInHandler method decryptSymmetricKey.

// TODO: Support symmetric keys if requested
protected byte[] decryptSymmetricKey(String base64EncodedKey, X509Certificate cert, Crypto crypto, String keyEncAlgo, String digestAlgo, Message message) throws WSSecurityException {
    CallbackHandler callback = RSSecurityUtils.getCallbackHandler(message, this.getClass());
    PrivateKey key = null;
    try {
        key = crypto.getPrivateKey(cert, callback);
    } catch (Exception ex) {
        throwFault("Encrypted key can not be decrypted", ex);
    }
    Cipher cipher = EncryptionUtils.initCipherWithKey(keyEncAlgo, digestAlgo, Cipher.DECRYPT_MODE, key);
    try {
        byte[] encryptedBytes = Base64Utility.decode(base64EncodedKey);
        byte[] decryptedKey = cipher.doFinal(encryptedBytes);
        // Clean the private key from memory now that we're finished with it
        try {
            key.destroy();
        } catch (DestroyFailedException ex) {
        // ignore
        }
        return decryptedKey;
    } catch (Base64Exception ex) {
        throwFault("Base64 decoding has failed", ex);
    } catch (Exception ex) {
        throwFault("Encrypted key can not be decrypted", ex);
    }
    return null;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) DestroyFailedException(javax.security.auth.DestroyFailedException) PrivateKey(java.security.PrivateKey) Base64Exception(org.apache.cxf.common.util.Base64Exception) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Cipher(javax.crypto.Cipher) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) DestroyFailedException(javax.security.auth.DestroyFailedException) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException) Base64Exception(org.apache.cxf.common.util.Base64Exception)

Example 28 with DestroyFailedException

use of javax.security.auth.DestroyFailedException in project cxf by apache.

the class CryptoUtils method wrapSecretKey.

public static byte[] wrapSecretKey(byte[] keyBytes, String keyAlgo, Key wrapperKey, KeyProperties wrapperKeyProps) throws SecurityException {
    SecretKeySpec secretKey = new SecretKeySpec(keyBytes, convertJCECipherToSecretKeyName(keyAlgo));
    byte[] encryptedKey = wrapSecretKey(secretKey, wrapperKey, wrapperKeyProps);
    // Here we're finished with the SecretKey we created, so we can destroy it
    try {
        secretKey.destroy();
    } catch (DestroyFailedException e) {
    // ignore
    }
    return encryptedKey;
}
Also used : DestroyFailedException(javax.security.auth.DestroyFailedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec)

Example 29 with DestroyFailedException

use of javax.security.auth.DestroyFailedException in project cxf by apache.

the class HmacUtils method computeHmac.

public static byte[] computeHmac(byte[] key, Mac hmac, String data) {
    SecretKeySpec secretKey = new SecretKeySpec(key, hmac.getAlgorithm());
    byte[] digest = computeHmac(secretKey, hmac, data);
    // Here we're finished with the SecretKey we created, so we can destroy it
    try {
        secretKey.destroy();
    } catch (DestroyFailedException e) {
    // ignore
    }
    return digest;
}
Also used : DestroyFailedException(javax.security.auth.DestroyFailedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec)

Example 30 with DestroyFailedException

use of javax.security.auth.DestroyFailedException in project cxf by apache.

the class HmacUtils method computeHmac.

public static byte[] computeHmac(byte[] key, String macAlgoJavaName, AlgorithmParameterSpec spec, String data) {
    Mac mac = getMac(macAlgoJavaName);
    SecretKeySpec secretKey = new SecretKeySpec(key, mac.getAlgorithm());
    byte[] digest = computeHmac(secretKey, mac, spec, data);
    // Here we're finished with the SecretKey we created, so we can destroy it
    try {
        secretKey.destroy();
    } catch (DestroyFailedException e) {
        LOG.log(Level.FINE, "Error destroying key: {}", e.getMessage());
    }
    return digest;
}
Also used : DestroyFailedException(javax.security.auth.DestroyFailedException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Aggregations

DestroyFailedException (javax.security.auth.DestroyFailedException)30 SecretKey (javax.crypto.SecretKey)9 PrivateKey (java.security.PrivateKey)5 X509Certificate (java.security.cert.X509Certificate)4 CallbackHandler (javax.security.auth.callback.CallbackHandler)4 Crypto (org.apache.wss4j.common.crypto.Crypto)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 KerberosTicket (javax.security.auth.kerberos.KerberosTicket)3 CryptoType (org.apache.wss4j.common.crypto.CryptoType)3 WSPasswordCallback (org.apache.wss4j.common.ext.WSPasswordCallback)3 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 XMLCipher (org.apache.xml.security.encryption.XMLCipher)2 XMLEncryptionException (org.apache.xml.security.encryption.XMLEncryptionException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 java.io (java.io)1 Signature (java.security.Signature)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 Cipher (javax.crypto.Cipher)1