Search in sources :

Example 11 with PasswordException

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

the class PbeCrypto method decode.

@Override
@SuppressWarnings({ "lgtm [java/weak-cryptographic-algorithm]" })
public String decode(String encrypted, String charset) throws PasswordException {
    String encryptedString = encrypted;
    String result = null;
    if (encrypted.startsWith(NON_PORTABLE_PASSWORD)) {
        encryptedString = encrypted.substring(NON_PORTABLE_PASSWORD.length());
    }
    try {
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(SALT, ITERATIONS);
        PBEKeySpec pbeKeySpec = new PBEKeySpec(hostname.toCharArray());
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
        Cipher pbeCipher = Cipher.getInstance(ALGORITHM);
        pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
        byte[] decrypted = pbeCipher.doFinal(base64.translate(encryptedString));
        result = unseed(decrypted, charset);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PasswordException(com.adaptris.security.exc.PasswordException) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) PasswordException(com.adaptris.security.exc.PasswordException)

Example 12 with PasswordException

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

the class LegacyPrivateKeyPasswordProvider method retrievePrivateKeyPassword.

/**
 * Return the private key password as a char[] array.
 *
 * @return the private key password sourced from 'security.properties' and decoded using {@link com.adaptris.security.password.Password#decode(String)}
 */
@Override
public char[] retrievePrivateKeyPassword() throws PasswordException {
    if (pkPassword == null) {
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(PKEY_PW_FILE)) {
            Properties p = new Properties();
            p.load(is);
            pkPassword = Password.decode(p.getProperty(PKEY_PW_KEY)).toCharArray();
        } catch (IOException e) {
            throw new PasswordException(e);
        }
    }
    return pkPassword;
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 13 with PasswordException

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

the class HttpLegacyPrivateKeyPasswordProvider method retrievePrivateKeyPassword.

/**
 * Return the private key password as a char[] array.
 *
 * @return the private key sourced from 'adp-http.properties' and decoded using {@link com.adaptris.security.password.Password#decode(String)}
 */
@Override
public char[] retrievePrivateKeyPassword() throws PasswordException {
    if (pkPassword == null) {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(Https.CONFIG_PROPERTY_FILE);
        try {
            if (is != null) {
                Properties p = new Properties();
                p.load(is);
                pkPassword = Password.decode(p.getProperty(Https.CONFIG_PRIVATE_KEY_PW)).toCharArray();
            }
        } catch (IOException e) {
            throw new PasswordException(e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return pkPassword;
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 14 with PasswordException

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

the class JdbcConnection method validateConnection.

/**
 * <p>
 * Validate the underlying connection.
 * </p>
 *
 * @throws SQLException if we could not validate the connection.
 */
private void validateConnection() throws SQLException {
    boolean connectionNeedsRefresh = false;
    connectionNeedsRefresh = (sqlConnection == null);
    if (connectionNeedsRefresh) {
        try {
            Properties p = connectionProperties();
            sqlConnection = new ProxyNonClosingSqlConnection(DriverManager.getConnection(getConnectUrl(), p));
        } catch (PasswordException e) {
            sqlConnection = null;
            log.error("Couldn't decode password for database");
            throw new SQLException(e);
        }
    }
    try {
        // If the driver throws an AbstractMethodError because it's too old
        // We're running in J11 which means it must have been compiled against
        // J8 or higher... How the hell is it getting away with that?
        JdbcUtil.testConnection(sqlConnection, alwaysValidateConnection());
    } catch (SQLException e) {
        sqlConnection = null;
        throw e;
    }
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) SQLException(java.sql.SQLException) Properties(java.util.Properties)

Example 15 with PasswordException

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

the class StandardSftpConnectionTest method testConnectOnly_Composite_Fails.

@Test
public void testConnectOnly_Composite_Fails() throws Exception {
    Assume.assumeTrue(areTestsEnabled());
    StandardSftpConnection conn = createConnection();
    SftpAuthenticationWrapper auth = new SftpAuthenticationWrapper(new SftpKeyAuthentication(PROPERTIES.getProperty(CFG_PRIVATE_KEY_FILE), "PW:abde"), new SftpKeyAuthentication("/some/path/that/does/not/exist", PROPERTIES.getProperty(CFG_PRIVATE_KEY_PW)));
    conn.setAuthentication(auth);
    try {
        start(conn);
        FileTransferClient c = conn.connect(getDestinationString());
        fail();
    } catch (IOException | PasswordException expected) {
    } finally {
        stop(conn);
    }
}
Also used : PasswordException(com.adaptris.security.exc.PasswordException) FileTransferClient(com.adaptris.filetransfer.FileTransferClient) IOException(java.io.IOException) Test(org.junit.Test)

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