use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.
the class SMIMECryptographerImpl method decrypt.
/**
* Decrypts an entity with the provided certificates' private key.
* @param encryptedEntity The entity that will be decrypted.
* @param decryptingCertificate The certificates whose private keys will be used to decrypt the message.
* @return A MimeEntity containing the decrypted part.
*/
public MimeEntity decrypt(MimeEntity encryptedEntity, Collection<X509CertificateEx> decryptingCertificates) {
if (decryptingCertificates == null || decryptingCertificates.size() == 0) {
throw new IllegalArgumentException();
}
MimeEntity retEntity = null;
try {
if (LOGGER.isDebugEnabled()) {
final byte[] encryptedContent = encryptedEntity.getContentAsBytes();
writePreDecrypt(encryptedContent);
}
final SMIMEEnveloped m = new SMIMEEnveloped(encryptedEntity);
if (!this.isAllowedEncryptionAlgorithm(m.getEncryptionAlgOID()))
throw new NHINDException(MimeError.DisallowedEncryptionAlgorithm, "The encryption algorithm " + m.getEncryptionAlgOID() + " is not allowed");
for (X509CertificateEx decryptCert : decryptingCertificates) {
final RecipientId recId = generateRecipientSelector(decryptCert);
final RecipientInformationStore recipients = m.getRecipientInfos();
final DirectRecipientInformation recipient = decFactory.createInstance(recipients.get(recId), m);
if (recipient == null)
continue;
final byte[] decryptedPayload = recipient.getDecryptedContent(decryptCert.getPrivateKey());
if (LOGGER.isDebugEnabled()) {
writePostDecrypt(decryptedPayload);
}
final ByteArrayInputStream inStream = new ByteArrayInputStream(decryptedPayload);
retEntity = new MimeEntity(inStream);
break;
}
} catch (MessagingException e) {
throw new MimeException(MimeError.InvalidMimeEntity, e);
} catch (Exception e) {
throw new MimeException(MimeError.Unexpected, e);
}
if (retEntity == null) {
throw new NHINDException(MimeError.Unexpected, "None of the the provided decryption certs were found in message's RecipientsInfo set.");
}
return retEntity;
}
use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.
the class DefaultDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_emptyProvider_assertDefaultProvider.
public void testInstanceTest_emptyProvider_assertDefaultProvider() throws Exception {
final SMIMEEnveloped env = createSMIMEEnv();
final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
final DefaultDirectRecipientInformationFactory factory = new DefaultDirectRecipientInformationFactory();
final DefaultDirectRecipientInformation recInfo = (DefaultDirectRecipientInformation) factory.createInstance(recipient, env);
assertEquals(CryptoExtensions.getJCEProviderName(), recInfo.encProvider);
}
use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.
the class DefaultDirectRecipientInformationFactory_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 DefaultDirectRecipientInformationFactory factory = new DefaultDirectRecipientInformationFactory("Hello");
final DefaultDirectRecipientInformation recInfo = (DefaultDirectRecipientInformation) factory.createInstance(recipient, env);
assertEquals("Hello", recInfo.encProvider);
}
use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.
the class DefaultDirectRecipientInformation_getDecryptedContentTest method testDecryptedContent_defaultConfig_assertDecrypted.
public void testDecryptedContent_defaultConfig_assertDecrypted() throws Exception {
final SMIMEEnveloped env = createSMIMEEnv();
final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory();
final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
// this won't work unless the data is successfully decrypted
assertNotNull(recInfo.getDecryptedContent(encCert.getPrivateKey()));
}
use of org.bouncycastle.mail.smime.SMIMEEnveloped in project nhin-d by DirectProject.
the class SplitDirectRecipientInformationFactory_createInstanceTest method testInstanceTest_emptyProvider_assertDefaultProvider.
public void testInstanceTest_emptyProvider_assertDefaultProvider() throws Exception {
final SMIMEEnveloped env = createSMIMEEnv();
final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory();
final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
assertEquals(CryptoExtensions.getJCEProviderName(), recInfo.encProvider);
assertEquals(CryptoExtensions.getJCESensitiveProviderName(), recInfo.keyEncProvider);
}
Aggregations