Search in sources :

Example 6 with SignedEntity

use of org.nhindirect.stagent.cryptography.SignedEntity in project nhin-d by DirectProject.

the class CryptographerTest method testSignMimeEntity_SHA256Digest_forceStrongDigest_assertValidation.

public void testSignMimeEntity_SHA256Digest_forceStrongDigest_assertValidation() throws Exception {
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setDigestAlgorithm(DigestAlgorithm.SHA256);
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    SignedEntity signedEnt = cryptographer.sign(entity, certex);
    assertNotNull(signedEnt);
    byte[] signedEntityBytes = EntitySerializer.Default.serializeToBytes(signedEnt.getContent());
    byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
    assertTrue(Arrays.equals(signedEntityBytes, entityBytes));
    assertNotNull(signedEnt.getSignature());
    X509Certificate cert = TestUtils.getExternalCert("user1");
    cryptographer.checkSignature(signedEnt, cert, new ArrayList<X509Certificate>());
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) X509Certificate(java.security.cert.X509Certificate)

Example 7 with SignedEntity

use of org.nhindirect.stagent.cryptography.SignedEntity in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptAndSignMimeEntity.

public void testEncryptAndSignMimeEntity() throws Exception {
    X509Certificate cert = TestUtils.getInternalCACert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity encEntity = cryptographer.encrypt(entity, cert);
    assertNotNull(encEntity);
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SignedEntity signedEnt = cryptographer.sign(entity, certex);
    assertNotNull(signedEnt);
    cryptographer.checkSignature(signedEnt, cert, new ArrayList<X509Certificate>());
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) X509Certificate(java.security.cert.X509Certificate)

Aggregations

SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)7 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)7 X509Certificate (java.security.cert.X509Certificate)5 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)5 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)5 IOException (java.io.IOException)2 Header (javax.mail.Header)2 MessagingException (javax.mail.MessagingException)2 InternetHeaders (javax.mail.internet.InternetHeaders)2 MimeMessage (javax.mail.internet.MimeMessage)2 Message (org.nhindirect.stagent.mail.Message)2 MimeException (org.nhindirect.stagent.mail.MimeException)2 WrappedMessage (org.nhindirect.stagent.mail.WrappedMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ContentType (javax.mail.internet.ContentType)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1 CMSSignedData (org.bouncycastle.cms.CMSSignedData)1 PolicyParseException (org.nhindirect.policy.PolicyParseException)1 PolicyProcessException (org.nhindirect.policy.PolicyProcessException)1