Search in sources :

Example 6 with CertificateHandler

use of com.adaptris.security.certificate.CertificateHandler 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 7 with CertificateHandler

use of com.adaptris.security.certificate.CertificateHandler 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 8 with CertificateHandler

use of com.adaptris.security.certificate.CertificateHandler in project interlok by adaptris.

the class StdSecurityService method verify.

/**
 * @see SecurityService#verify(byte[], Alias, Alias)
 */
public Output verify(byte[] payload, Alias receiver, Alias sender) throws AdaptrisSecurityException {
    StdOutput target = null;
    PrivateKey pk = null;
    CertificateHandler them = null;
    if (alg == null) {
        throw new VerifyException("Decrypt / Verify requires an " + "EncryptionAlgorithm object");
    }
    pk = getPrivateKey(receiver.getAlias(), receiver.getAliasPassword());
    them = createCertificateHandler(getCertificate(sender.getAlias()));
    target = decrypt(payload, pk);
    if (!verify(target, them)) {
        throw new VerifyException("Payload signature could not be verified");
    }
    return target;
}
Also used : PrivateKey(java.security.PrivateKey) VerifyException(com.adaptris.security.exc.VerifyException) CertificateHandler(com.adaptris.security.certificate.CertificateHandler)

Example 9 with CertificateHandler

use of com.adaptris.security.certificate.CertificateHandler in project interlok by adaptris.

the class TestCertificateGeneration method testCertificateGeneration.

@Test
public void testCertificateGeneration() throws Exception {
    String commonName = String.valueOf(random.nextInt(1000));
    CertificateBuilder builder = Config.getInstance().getBuilder(commonName);
    Certificate selfCert = builder.createSelfSignedCertificate();
    CertificateHandler ch = CertificateHandlerFactory.getInstance().generateHandler(selfCert);
    String signatureAlgorithm = ch.getSignatureAlgorithm();
    String property = cfg.getProperty(Config.CERTIFICATE_SIGALG);
    // AlgorithmID algorithmID = SecurityUtil.getAlgorithmID(signatureAlgorithm);
    // AlgorithmID configID = SecurityUtil.getAlgorithmID(property);
    assertEquals("Signature Algorithm", signatureAlgorithm, property);
    assertEquals("Key Algorithm", cfg.getProperty(Config.CERTIFICATE_KEYALG), ch.getKeyAlgorithm());
}
Also used : CertificateBuilder(com.adaptris.security.certificate.CertificateBuilder) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 10 with CertificateHandler

use of com.adaptris.security.certificate.CertificateHandler in project interlok by adaptris.

the class TestCertificateHandler method testGoodCertificateRevocation.

@Test
public void testGoodCertificateRevocation() throws Exception {
    InputStream input = new FileInputStream(config.getProperties().getProperty(Config.CERTHANDLER_GOOD));
    CertificateHandler handler = CertificateHandlerFactory.getInstance().generateHandler(input);
    input.close();
    assertEquals("Revocation", handler.isRevoked(), false);
    assertNotNull(handler.getLastRevocationCheck());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateHandler(com.adaptris.security.certificate.CertificateHandler) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

CertificateHandler (com.adaptris.security.certificate.CertificateHandler)10 Test (org.junit.Test)7 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 CertException (com.adaptris.security.exc.CertException)3 PrivateKey (java.security.PrivateKey)3 EncryptException (com.adaptris.security.exc.EncryptException)2 VerifyException (com.adaptris.security.exc.VerifyException)2 UnknownHostException (java.net.UnknownHostException)2 Calendar (java.util.Calendar)2 CertificateBuilder (com.adaptris.security.certificate.CertificateBuilder)1 AdaptrisSecurityException (com.adaptris.security.exc.AdaptrisSecurityException)1 DecryptException (com.adaptris.security.exc.DecryptException)1 KeystoreException (com.adaptris.security.exc.KeystoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Signature (java.security.Signature)1 Certificate (java.security.cert.Certificate)1