Search in sources :

Example 1 with EncryptException

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

the class StdSecurityService method encrypt.

/**
 * @see SecurityService#encrypt(byte[], Alias, Alias)
 */
public Output encrypt(byte[] payload, Alias sender, Alias receiver) throws AdaptrisSecurityException {
    PrivateKey us = null;
    Output output = null;
    if (alg == null) {
        throw new EncryptException("Encryption requires an " + "EncryptionAlgorithm object");
    }
    us = getPrivateKey(sender.getAlias(), sender.getAliasPassword());
    CertificateHandler them = createCertificateHandler(getCertificate(receiver.getAlias()));
    output = encrypt(payload, us, them);
    return output;
}
Also used : PrivateKey(java.security.PrivateKey) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) EncryptException(com.adaptris.security.exc.EncryptException)

Example 2 with EncryptException

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

the class StdSecurityService method sign.

/**
 * @see SecurityService#sign(byte[], Alias, Output)
 */
public Output sign(byte[] payload, Alias us, Output output) throws AdaptrisSecurityException {
    PrivateKey pk = null;
    StdOutput target = null;
    CertificateHandler ch = null;
    try {
        target = output == null ? new StdOutput(Output.PLAIN) : (StdOutput) output;
        target.setType(target.getType() | Output.SIGNED);
    } catch (ClassCastException e) {
        if (output != null)
            throw new EncryptException("Class " + output.getClass() + " not recognised", e);
        else
            throw new EncryptException("Output null, therefore not recognised", e);
    }
    pk = getPrivateKey(us.getAlias(), us.getAliasPassword());
    ch = createCertificateHandler(getCertificate(us.getAlias()));
    try {
        Signature sig = getSignatureInstance(ch);
        sig.initSign(pk, SecurityUtil.getSecureRandom());
        sig.update(payload);
        target.setSignature(sig.sign());
        target.setDecryptedData(payload);
    } catch (Exception e) {
        throw new EncryptException(e);
    }
    return target;
}
Also used : PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) EncryptException(com.adaptris.security.exc.EncryptException) KeystoreException(com.adaptris.security.exc.KeystoreException) CertException(com.adaptris.security.exc.CertException) VerifyException(com.adaptris.security.exc.VerifyException) EncryptException(com.adaptris.security.exc.EncryptException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DecryptException(com.adaptris.security.exc.DecryptException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 3 with EncryptException

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

the class StdOutput method formatBase64.

/**
 * Return the encrypted message ready for immediate writing to file.
 *
 * @return the bytes ready for writing.
 * @throws AdaptrisSecurityException if an error occurs
 */
private byte[] formatBase64() throws EncryptException {
    DataOutputStream out = null;
    ByteArrayOutputStream byteStream = null;
    byte[] returnBytes = null;
    try {
        byteStream = new ByteArrayOutputStream();
        out = new DataOutputStream(byteStream);
        write(out, getSessionVector());
        write(out, getSessionKey());
        write(out, getEncryptedData(false) == null ? getDecryptedData(false) : getEncryptedData(false));
        write(out, getSignature());
        returnBytes = Base64.encodeBase64(byteStream.toByteArray());
    } catch (Exception e) {
        throw new EncryptException(e);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (byteStream != null) {
                byteStream.close();
            }
        } catch (Exception ignored) {
            ;
        }
    }
    return returnBytes;
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VerifyException(com.adaptris.security.exc.VerifyException) IOException(java.io.IOException) EncryptException(com.adaptris.security.exc.EncryptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) EncryptException(com.adaptris.security.exc.EncryptException)

Example 4 with EncryptException

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

the class StdSecurityService method encrypt.

private Output encrypt(byte[] payload, PrivateKey pk, CertificateHandler ch) throws AdaptrisSecurityException {
    StdOutput output = new StdOutput(Output.ENCRYPTED);
    try {
        KeyGenerator kg = KeyGenerator.getInstance(getCipherName(alg.getAlgorithm()));
        kg.init(alg.getKeyLength(), SecurityUtil.getSecureRandom());
        SecretKey sessionKey = kg.generateKey();
        Cipher dataCipher = Cipher.getInstance(alg.getAlgorithm());
        /*,
          Constants.SECURITY_PROVIDER);*/
        dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey);
        byte[] encryptedBody = dataCipher.doFinal(payload);
        Cipher keyCipher = Cipher.getInstance(ch.getKeyAlgorithm());
        /*,
          Constants.SECURITY_PROVIDER);*/
        keyCipher.init(Cipher.ENCRYPT_MODE, ch.getPublicKey(), SecurityUtil.getSecureRandom());
        byte[] encryptedSessionKey = keyCipher.doFinal(sessionKey.getEncoded());
        output.setSessionKey(encryptedSessionKey);
        output.setSessionVector(dataCipher.getIV());
        output.setEncryptedData(encryptedBody);
    } catch (Exception e) {
        throw new EncryptException(e);
    }
    return output;
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) KeystoreException(com.adaptris.security.exc.KeystoreException) CertException(com.adaptris.security.exc.CertException) VerifyException(com.adaptris.security.exc.VerifyException) EncryptException(com.adaptris.security.exc.EncryptException) AdaptrisSecurityException(com.adaptris.security.exc.AdaptrisSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DecryptException(com.adaptris.security.exc.DecryptException) NoSuchProviderException(java.security.NoSuchProviderException) EncryptException(com.adaptris.security.exc.EncryptException)

Aggregations

EncryptException (com.adaptris.security.exc.EncryptException)4 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)3 VerifyException (com.adaptris.security.exc.VerifyException)3 CertificateHandler (com.adaptris.security.certificate.CertificateHandler)2 CertException (com.adaptris.security.exc.CertException)2 DecryptException (com.adaptris.security.exc.DecryptException)2 KeystoreException (com.adaptris.security.exc.KeystoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 PrivateKey (java.security.PrivateKey)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Signature (java.security.Signature)1 Cipher (javax.crypto.Cipher)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1