Search in sources :

Example 11 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project oxAuth by GluuFederation.

the class JweDecrypterImpl method decryptCipherText.

@Override
public String decryptCipherText(String encodedCipherText, byte[] contentMasterKey, byte[] initializationVector, byte[] authenticationTag, byte[] additionalAuthenticatedData) throws InvalidJweException {
    if (getBlockEncryptionAlgorithm() == null) {
        throw new InvalidJweException("The block encryption algorithm is null");
    }
    if (contentMasterKey == null) {
        throw new InvalidJweException("The content master key (CMK) is null");
    }
    if (initializationVector == null) {
        throw new InvalidJweException("The initialization vector is null");
    }
    if (authenticationTag == null) {
        throw new InvalidJweException("The authentication tag is null");
    }
    if (additionalAuthenticatedData == null) {
        throw new InvalidJweException("The additional authentication data is null");
    }
    try {
        if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128GCM || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256GCM) {
            final int MAC_SIZE_BITS = 128;
            byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
            KeyParameter key = new KeyParameter(contentMasterKey);
            AEADParameters aeadParameters = new AEADParameters(key, MAC_SIZE_BITS, initializationVector, additionalAuthenticatedData);
            SecretKeySpec sks = new SecretKeySpec(contentMasterKey, "AES");
            BlockCipher blockCipher = new AESEngine();
            CipherParameters params = new KeyParameter(sks.getEncoded());
            blockCipher.init(false, params);
            GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
            aGCMBlockCipher.init(false, aeadParameters);
            byte[] input = new byte[cipherText.length + authenticationTag.length];
            System.arraycopy(cipherText, 0, input, 0, cipherText.length);
            System.arraycopy(authenticationTag, 0, input, cipherText.length, authenticationTag.length);
            int len = aGCMBlockCipher.getOutputSize(input.length);
            byte[] out = new byte[len];
            int outOff = aGCMBlockCipher.processBytes(input, 0, input.length, out, 0);
            aGCMBlockCipher.doFinal(out, outOff);
            String plaintext = new String(out, Charset.forName(Util.UTF8_STRING_ENCODING));
            return plaintext;
        } else if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128CBC_PLUS_HS256 || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256CBC_PLUS_HS512) {
            byte[] cipherText = Base64Util.base64urldecode(encodedCipherText);
            byte[] cek = KeyDerivationFunction.generateCek(contentMasterKey, getBlockEncryptionAlgorithm());
            Cipher cipher = Cipher.getInstance(getBlockEncryptionAlgorithm().getAlgorithm());
            IvParameterSpec ivParameter = new IvParameterSpec(initializationVector);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(cek, "AES"), ivParameter);
            byte[] decodedPlainTextBytes = cipher.doFinal(cipherText);
            String decodedPlainText = new String(decodedPlainTextBytes, Charset.forName(Util.UTF8_STRING_ENCODING));
            // Integrity check
            String securedInputValue = new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING)) + "." + encodedCipherText;
            byte[] cik = KeyDerivationFunction.generateCik(contentMasterKey, getBlockEncryptionAlgorithm());
            SecretKey secretKey = new SecretKeySpec(cik, getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
            Mac mac = Mac.getInstance(getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm());
            mac.init(secretKey);
            byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING));
            if (!Arrays.equals(integrityValue, authenticationTag)) {
                throw new InvalidJweException("The authentication tag is not valid");
            }
            return decodedPlainText;
        } else {
            throw new InvalidJweException("The block encryption algorithm is not supported");
        }
    } catch (InvalidCipherTextException e) {
        throw new InvalidJweException(e);
    } catch (NoSuchPaddingException e) {
        throw new InvalidJweException(e);
    } catch (BadPaddingException e) {
        throw new InvalidJweException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new InvalidJweException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new InvalidJweException(e);
    } catch (IllegalBlockSizeException e) {
        throw new InvalidJweException(e);
    } catch (UnsupportedEncodingException e) {
        throw new InvalidJweException(e);
    } catch (NoSuchProviderException e) {
        throw new InvalidJweException(e);
    } catch (InvalidKeyException e) {
        throw new InvalidJweException(e);
    } catch (InvalidParameterException e) {
        throw new InvalidJweException(e);
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) InvalidParameterException(org.xdi.oxauth.model.exception.InvalidParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidJweException(org.xdi.oxauth.model.exception.InvalidJweException) AESEngine(org.bouncycastle.crypto.engines.AESEngine) BlockCipher(org.bouncycastle.crypto.BlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CipherParameters(org.bouncycastle.crypto.CipherParameters) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) IvParameterSpec(javax.crypto.spec.IvParameterSpec) BlockCipher(org.bouncycastle.crypto.BlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Example 12 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project gocd by gocd.

the class TfsMaterialConfigUpdateTest method shouldErrorOutIfEncryptionFails.

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {
        new TfsMaterialConfig(mockGoCipher, new UrlArgument("http://10.4.4.101:8080/tfs/Sample"), "loser", "CORPORATE", "password", "walk_this_path");
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}
Also used : UrlArgument(com.thoughtworks.go.util.command.UrlArgument) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) GoCipher(com.thoughtworks.go.security.GoCipher) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) Test(org.junit.Test)

Example 13 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project gocd by gocd.

the class TfsMaterialTest method shouldErrorOutIfEncryptionFails.

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {
        new TfsMaterial(mockGoCipher, new UrlArgument("/foo"), "username", DOMAIN, "password", "");
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}
Also used : UrlArgument(com.thoughtworks.go.util.command.UrlArgument) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) GoCipher(com.thoughtworks.go.security.GoCipher) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) Test(org.junit.Test)

Example 14 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project gocd by gocd.

the class TfsMaterialTest method shouldErrorOutIfDecryptionFails.

@Test
public void shouldErrorOutIfDecryptionFails() throws InvalidCipherTextException {
    GoCipher mockGoCipher = mock(GoCipher.class);
    String fakeCipherText = "fake cipher text";
    when(mockGoCipher.decrypt(fakeCipherText)).thenThrow(new InvalidCipherTextException("exception"));
    TfsMaterial material = new TfsMaterial(mockGoCipher, new UrlArgument("/foo"), "username", DOMAIN, "password", "");
    ReflectionUtil.setField(material, "encryptedPassword", fakeCipherText);
    try {
        material.getPassword();
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Could not decrypt the password to get the real password"));
    }
}
Also used : UrlArgument(com.thoughtworks.go.util.command.UrlArgument) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) GoCipher(com.thoughtworks.go.security.GoCipher) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) Test(org.junit.Test)

Example 15 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project gocd by gocd.

the class SvnMaterialTest method shouldErrorOutIfEncryptionFails.

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {
        new SvnMaterial("/foo", "username", "password", false, mockGoCipher);
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) GoCipher(com.thoughtworks.go.security.GoCipher) SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)19 GoCipher (com.thoughtworks.go.security.GoCipher)6 Test (org.junit.Test)6 BlockCipher (org.bouncycastle.crypto.BlockCipher)5 UrlArgument (com.thoughtworks.go.util.command.UrlArgument)4 BadPaddingException (javax.crypto.BadPaddingException)4 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 CipherParameters (org.bouncycastle.crypto.CipherParameters)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)3 InvalidJweException (org.xdi.oxauth.model.exception.InvalidJweException)3 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 DataLengthException (org.bouncycastle.crypto.DataLengthException)2 AESEngine (org.bouncycastle.crypto.engines.AESEngine)2 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)2