Search in sources :

Example 1 with PasswordException

use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.

the class DynamicBasicAuthorizationHeader method setup.

@Override
public void setup(String target, AdaptrisMessage msg, ResourceTargetMatcher auth) throws CoreException {
    try {
        String username = Args.notBlank(msg.resolve(getUsername()), "username");
        String password = Args.notBlank(msg.resolve(getPassword()), "password");
        String encoded = new Base64ByteTranslator().translate(String.format("%s:%s", username, Password.decode(password)).getBytes("UTF-8"));
        authHeader = String.format("Basic %s", encoded);
    } catch (UnsupportedEncodingException | IllegalArgumentException | PasswordException e) {
        throw ExceptionHelper.wrapCoreException(e);
    }
}
Also used : Base64ByteTranslator(com.adaptris.util.text.Base64ByteTranslator) PasswordException(com.adaptris.security.exc.PasswordException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with PasswordException

use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.

the class AesCrypto method decode.

@Override
@SuppressWarnings({ "lgtm [java/weak-cryptographic-algorithm]" })
public String decode(String encrypted, String charset) throws PasswordException {
    String encryptedString = encrypted;
    String result;
    if (encrypted.startsWith(PORTABLE_PASSWORD)) {
        encryptedString = encrypted.substring(PORTABLE_PASSWORD.length());
    }
    try {
        Input input = new Input(encryptedString);
        input.read();
        SecretKey sessionKey = new SecretKeySpec(input.getSessionKey(), ALG);
        Cipher cipher = Cipher.getInstance(CIPHER);
        if (input.getSessionVector() != null) {
            IvParameterSpec spec = new IvParameterSpec(input.getSessionVector());
            cipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, sessionKey);
        }
        byte[] decrypted = cipher.doFinal(input.getEncryptedData());
        result = unseed(decrypted, charset);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) PasswordException(com.adaptris.security.exc.PasswordException)

Example 3 with PasswordException

use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.

the class AesGcmCrypto method encode.

@Override
public String encode(String plainText, String charset) throws PasswordException {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        keyGenerator.init(KEY_LENGTH);
        SecretKey secretKey = keyGenerator.generateKey();
        Cipher cipher = Cipher.getInstance(CIPHER);
        byte[] iv = new byte[IV_LENGTH];
        SECURE_RAND.nextBytes(iv);
        GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec);
        byte[] bytes = plainText.getBytes(getEncodingToUse(charset));
        byte[] key = secretKey.getEncoded();
        byte[] cipherText = new byte[iv.length + key.length + cipher.getOutputSize(bytes.length)];
        int i = 0;
        for (byte v : iv) {
            cipherText[i++] = v;
        }
        for (byte k : key) {
            cipherText[i++] = k;
        }
        cipher.doFinal(bytes, 0, bytes.length, cipherText, i);
        secretKey.getEncoded();
        return PORTABLE_PASSWORD_2 + base64.translate(cipherText);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) KeyGenerator(javax.crypto.KeyGenerator) PasswordException(com.adaptris.security.exc.PasswordException)

Example 4 with PasswordException

use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.

the class AesGcmCrypto method decode.

@Override
public String decode(String b64CipherText, String charset) throws PasswordException {
    if (b64CipherText.startsWith(PORTABLE_PASSWORD_2)) {
        b64CipherText = b64CipherText.substring(PORTABLE_PASSWORD_2.length());
    }
    try {
        byte[] cipherText = base64.translate(b64CipherText);
        byte[] iv = Arrays.copyOfRange(cipherText, 0, IV_LENGTH);
        byte[] key = Arrays.copyOfRange(cipherText, IV_LENGTH, IV_LENGTH + KEY_SIZE);
        SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER);
        GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
        int offset = iv.length + key.length;
        byte[] plaintext = cipher.doFinal(cipherText, offset, cipherText.length - offset);
        return new String(plaintext, getEncodingToUse(charset));
    } catch (Exception e) {
        throw new PasswordException(e);
    }
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) PasswordException(com.adaptris.security.exc.PasswordException)

Example 5 with PasswordException

use of com.adaptris.security.exc.PasswordException in project interlok by adaptris.

the class MicrosoftCrypto method encode.

public String encode(String plainText, String charset) throws PasswordException {
    byte[] encryptedBody = new byte[0];
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, getCertificate());
        encryptedBody = cipher.doFinal(plainText.getBytes(getEncodingToUse(charset)));
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return MSCAPI_STYLE + base64.translate(encryptedBody);
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) Cipher(javax.crypto.Cipher) PasswordException(com.adaptris.security.exc.PasswordException)

Aggregations

PasswordException (com.adaptris.security.exc.PasswordException)15 Cipher (javax.crypto.Cipher)8 IOException (java.io.IOException)6 SecretKey (javax.crypto.SecretKey)6 Properties (java.util.Properties)3 FileTransferClient (com.adaptris.filetransfer.FileTransferClient)2 InputStream (java.io.InputStream)2 KeyGenerator (javax.crypto.KeyGenerator)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)2 PBEKeySpec (javax.crypto.spec.PBEKeySpec)2 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 Test (org.junit.Test)2 CoreException (com.adaptris.core.CoreException)1 SecurityServiceFactory (com.adaptris.security.SecurityServiceFactory)1 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)1 Alias (com.adaptris.security.keystore.Alias)1 ConfiguredKeystore (com.adaptris.security.keystore.ConfiguredKeystore)1 Base64ByteTranslator (com.adaptris.util.text.Base64ByteTranslator)1