Search in sources :

Example 11 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class MessagaeDecryptor method main.

public static void main(String[] args) {
    try {
        final KeyStore store = KeyStore.getInstance("pkcs12");
        store.load(FileUtils.openInputStream(new File("/users/gm2552/Desktop/ops.p12")), "".toCharArray());
        final String alias = store.aliases().nextElement();
        final PrivateKey entry = (PrivateKey) store.getKey(alias, "".toCharArray());
        final X509Certificate cert = (X509Certificate) store.getCertificate(alias);
        /*
			for (String arg :args)
			{
				if (arg )
			}
			*/
        //String encryptedStuff = FileUtils.readFileToString(new File("users/gm2552/Desktop/cry.eml"));
        InputStream inStream = FileUtils.openInputStream(new File("/users/gm2552/Desktop/cry2.eml"));
        MimeBodyPart part = new MimeBodyPart(inStream);
        final SMIMEEnveloped m = new SMIMEEnveloped(part);
        RecipientId recId = new RecipientId();
        recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
        recId.setSerialNumber(cert.getSerialNumber());
        final RecipientInformationStore recipients = m.getRecipientInfos();
        final DirectRecipientInformation recipient = new SplitDirectRecipientInformationFactory().createInstance(recipients.get(recId), m);
        final byte[] decryptedPayload = recipient.getDecryptedContent(entry);
        System.out.println("Alg OID: " + m.getEncryptionAlgOID());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PrivateKey(java.security.PrivateKey) RecipientId(org.bouncycastle.cms.RecipientId) InputStream(java.io.InputStream) DirectRecipientInformation(org.nhindirect.stagent.cryptography.activekeyops.DirectRecipientInformation) KeyStore(java.security.KeyStore) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) X509Certificate(java.security.cert.X509Certificate) SplitDirectRecipientInformationFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitDirectRecipientInformationFactory) RecipientInformationStore(org.bouncycastle.cms.RecipientInformationStore) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File)

Example 12 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class MessageEncEnvInfoInspector method main.

public static void main(String[] args) {
    if (args.length == 0) {
        //printUsage();
        System.exit(-1);
    }
    String messgefile = null;
    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        // Options
        if (!arg.startsWith("-")) {
            System.err.println("Error: Unexpected argument [" + arg + "]\n");
            //printUsage();
            System.exit(-1);
        } else if (arg.equalsIgnoreCase("-msgFile")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing message file");
                System.exit(-1);
            }
            messgefile = args[++i];
        } else if (arg.equals("-help")) {
            //printUsage();
            System.exit(-1);
        } else {
            System.err.println("Error: Unknown argument " + arg + "\n");
            //printUsage();
            System.exit(-1);
        }
    }
    if (messgefile == null) {
        System.err.println("Error: missing message file\n");
    }
    InputStream inStream = null;
    try {
        inStream = FileUtils.openInputStream(new File(messgefile));
        final SMIMEEnveloped env = new SMIMEEnveloped(new MimeMessage(null, inStream));
        String OID = env.getEncryptionAlgOID();
        System.out.println("Encryption OID: " + OID);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) InputStream(java.io.InputStream) DERBitString(org.bouncycastle.asn1.DERBitString) File(java.io.File) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 13 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class DefaultDirectRecipientInformationFactory_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 14 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped 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 15 with SMIMEEnveloped

use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.

the class SplitDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_configedProvider_assertConfigedProvider.

public void testInstanceTest_configedProvider_assertConfigedProvider() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory("SensitiveProv", "NonSenProv");
    final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
    assertEquals("NonSenProv", recInfo.encProvider);
    assertEquals("SensitiveProv", recInfo.keyEncProvider);
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Aggregations

SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)16 RecipientInformation (org.bouncycastle.cms.RecipientInformation)9 X509Certificate (java.security.cert.X509Certificate)5 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)4 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Key (java.security.Key)2 Certificate (java.security.cert.Certificate)2 RecipientId (org.bouncycastle.cms.RecipientId)2 RecipientInformationStore (org.bouncycastle.cms.RecipientInformationStore)2 DirectRecipientInformation (org.nhindirect.stagent.cryptography.activekeyops.DirectRecipientInformation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1