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;
}
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;
}
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;
}
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());
}
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());
}
Aggregations