Search in sources :

Example 26 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class DefaultDirectRecipientInformation_getDecryptedContentTest method createSMIMEEnv.

protected SMIMEEnveloped createSMIMEEnv() throws Exception {
    // get the cert
    encCert = (X509CertificateEx) TestUtils.getInternalCert("user1");
    // create an encrypted message
    final MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    final SMIMECryptographerImpl encryptor = new SMIMECryptographerImpl();
    return new SMIMEEnveloped(encryptor.encrypt(entity, Arrays.asList((X509Certificate) encCert)));
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 27 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SplitDirectRecipientInformationFactory_createInstanceTest method createSMIMEEnv.

protected SMIMEEnveloped createSMIMEEnv() throws Exception {
    // get the cert
    final X509Certificate cert = TestUtils.getExternalCert("user1");
    // create an encrypted message
    final MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    final SMIMECryptographerImpl encryptor = new SMIMECryptographerImpl();
    return new SMIMEEnveloped(encryptor.encrypt(entity, Arrays.asList(cert)));
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) X509Certificate(java.security.cert.X509Certificate)

Example 28 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertDispatched.

public void testCreateNotification_AssertDispatched() throws Exception {
    Notification noti = new Notification(NotificationType.Dispatched);
    ArrayList<MimeEntity> entities = (ArrayList<MimeEntity>) noti.getParts();
    MimeEntity entity = entities.get(0);
    assertEquals("Your message was successfully processed.", entity.getContent().toString());
    entity = entities.get(1);
    assertTrue(entity.getContentType().startsWith("message/disposition-notification"));
    DispositionNotification notification = (DispositionNotification) entity.getContent();
    assertEquals(notification.getNotifications().getHeader("disposition", ","), "automatic-action/MDN-sent-automatically;dispatched");
}
Also used : DispositionNotification(com.sun.mail.dsn.DispositionNotification) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 29 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SigTest method testCreateVerifySig.

public void testCreateVerifySig() throws Exception {
    X509CertificateEx internalCert = TestUtils.getInternalCert("user1");
    X509Certificate caCert = TestUtils.getExternalCert("cacert");
    String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
    MimeMessage entity = EntitySerializer.Default.deserialize(testMessage);
    Message message = new Message(entity);
    MimeEntity entityToSig = message.extractEntityForSignature(true);
    // Serialize message out as ASCII encoded...
    byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entityToSig);
    MimeBodyPart partToSign = null;
    try {
        partToSign = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
    } catch (Exception e) {
    }
    SMIMESignedGenerator gen = new SMIMESignedGenerator();
    ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
    SMIMECapabilityVector caps = new SMIMECapabilityVector();
    caps.addCapability(SMIMECapability.dES_EDE3_CBC);
    caps.addCapability(SMIMECapability.rC2_CBC, 128);
    caps.addCapability(SMIMECapability.dES_CBC);
    caps.addCapability(new DERObjectIdentifier("1.2.840.113549.1.7.1"));
    caps.addCapability(PKCSObjectIdentifiers.x509Certificate);
    signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
    List certList = new ArrayList();
    gen.addSigner(internalCert.getPrivateKey(), internalCert, SMIMESignedGenerator.DIGEST_SHA1, new AttributeTable(signedAttrs), null);
    //SMIMESignedGenerator.DIGEST_SHA1, null, null);
    certList.add(internalCert);
    MimeMultipart retVal = null;
    CertStore certsAndcrls = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), CryptoExtensions.getJCEProviderName());
    gen.addCertificatesAndCRLs(certsAndcrls);
    _certStores.add(certsAndcrls);
    _signers.add(new Signer(internalCert.getPrivateKey(), internalCert, SMIMESignedGenerator.DIGEST_SHA1, new AttributeTable(signedAttrs), null));
    retVal = generate(partToSign, CryptoExtensions.getJCEProviderName());
    for (int i = 0; i < 10; ++i) {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        retVal.writeTo(oStream);
        oStream.flush();
        byte[] serialzedBytes = oStream.toByteArray();
        //System.out.println(new String(serialzedBytes, "ASCII") + "\r\n\r\n\r\n\r\n\r\n");
        ByteArrayDataSource dataSource = new ByteArrayDataSource(serialzedBytes, retVal.getContentType());
        MimeMultipart verifyMM = new MimeMultipart(dataSource);
        CMSSignedData signed = null;
        //CMSSignedData signeddata = new CMSSignedData(new CMSProcessableBodyPartInbound(verifyMM.getBodyPart(0)), verifyMM.getBodyPart(1).getInputStream());			
        CMSSignedData signeddata = new CMSSignedData(new CMSProcessableBodyPartInbound(partToSign), verifyMM.getBodyPart(1).getInputStream());
        int verified = 0;
        CertStore certs = signeddata.getCertificatesAndCRLs("Collection", CryptoExtensions.getJCEProviderName());
        SignerInformationStore signers = signeddata.getSignerInfos();
        Collection c = signers.getSigners();
        Iterator it = c.iterator();
        while (it.hasNext()) {
            SignerInformation signer = (SignerInformation) it.next();
            Collection certCollection = certs.getCertificates(signer.getSID());
            Attribute dig = signer.getSignedAttributes().get(CMSAttributes.messageDigest);
            DERObject hashObj = dig.getAttrValues().getObjectAt(0).getDERObject();
            byte[] signedHash = ((ASN1OctetString) hashObj).getOctets();
            System.out.print("value of signedHash: \r\n\tvalue: ");
            for (byte bt : signedHash) {
                System.out.print(bt + " ");
            }
            System.out.println();
            Iterator certIt = certCollection.iterator();
            try {
                assertTrue(signer.verify(internalCert, CryptoExtensions.getJCEProviderName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            byte[] bytes = signer.getContentDigest();
            /*
	    		  X509Certificate cert = (X509Certificate)certIt.next();
	    		  
    		      if (signer.verify(cert.getPublicKey()))
    		      {
    		          verified++;
    		      }
	    		  */
            verified++;
        }
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) CMSProcessableBodyPartInbound(org.bouncycastle.mail.smime.CMSProcessableBodyPartInbound) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Attribute(org.bouncycastle.asn1.cms.Attribute) SMIMECapabilitiesAttribute(org.bouncycastle.asn1.smime.SMIMECapabilitiesAttribute) ArrayList(java.util.ArrayList) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) SMIMESignedGenerator(org.bouncycastle.mail.smime.SMIMESignedGenerator) SignerInformation(org.bouncycastle.cms.SignerInformation) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) CollectionCertStoreParameters(java.security.cert.CollectionCertStoreParameters) DERObject(org.bouncycastle.asn1.DERObject) MimeMessage(javax.mail.internet.MimeMessage) SMIMECapabilityVector(org.bouncycastle.asn1.smime.SMIMECapabilityVector) MimeMultipart(javax.mail.internet.MimeMultipart) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) Iterator(java.util.Iterator) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) List(java.util.List) ArrayList(java.util.ArrayList) SMIMECapabilitiesAttribute(org.bouncycastle.asn1.smime.SMIMECapabilitiesAttribute) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DERObjectIdentifier(org.bouncycastle.asn1.DERObjectIdentifier) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) MessagingException(javax.mail.MessagingException) CertStoreException(java.security.cert.CertStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CMSException(org.bouncycastle.cms.CMSException) IOException(java.io.IOException) SMIMEException(org.bouncycastle.mail.smime.SMIMEException) NoSuchProviderException(java.security.NoSuchProviderException) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) Collection(java.util.Collection) MimeBodyPart(javax.mail.internet.MimeBodyPart) CertStore(java.security.cert.CertStore)

Example 30 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class Notification method genMMRep.

/*
	 * Generates the multipart MIME representation of this object
	 */
private void genMMRep() {
    mmRep = new MimeMultipart();
    Collection<MimeEntity> parts = getParts();
    try {
        for (MimeEntity part : parts) mmRep.addBodyPart(part);
    } catch (MessagingException e) {
    /* no-op */
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) MimeEntity(org.nhindirect.stagent.mail.MimeEntity)

Aggregations

MimeEntity (org.nhindirect.stagent.mail.MimeEntity)34 X509Certificate (java.security.cert.X509Certificate)20 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)18 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 MessagingException (javax.mail.MessagingException)9 IOException (java.io.IOException)8 MimeMultipart (javax.mail.internet.MimeMultipart)8 SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Message (org.nhindirect.stagent.mail.Message)6 MimeException (org.nhindirect.stagent.mail.MimeException)6 ArrayList (java.util.ArrayList)5 InternetHeaders (javax.mail.internet.InternetHeaders)5 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)5 NHINDException (org.nhindirect.stagent.NHINDException)5 SignatureValidationException (org.nhindirect.stagent.SignatureValidationException)5 KeyStore (java.security.KeyStore)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3