use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CryptographerTest method testEncryptAndDecryptMimeEntity_sensitiveDataInPKCS11.
private void testEncryptAndDecryptMimeEntity_sensitiveDataInPKCS11(EncryptionAlgorithm encAlg) throws Exception {
OptionsManager.destroyInstance();
System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderName", "SunPKCS11-SafeNeteTokenPro");
System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderClassNames", "sun.security.pkcs11.SunPKCS11;./src/test/resources/pkcs11Config/pkcs11.cfg");
CryptoExtensions.registerJCEProviders();
try {
X509Certificate cert = TestUtils.getExternalCert("user1");
SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
cryptographer.setEncryptionAlgorithm(encAlg);
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");
// open up the pkcs11 store and find the private key
KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, "1Kingpuff".toCharArray());
X509CertificateEx decryptCert = null;
final Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
Certificate pkcs11Cert = ks.getCertificate(alias);
if (pkcs11Cert != null && pkcs11Cert instanceof X509Certificate) {
// check if there is private key
Key key = ks.getKey(alias, null);
if (key != null && key instanceof PrivateKey && CryptoExtensions.certSubjectContainsName((X509Certificate) pkcs11Cert, "user1@cerner.com")) {
decryptCert = X509CertificateEx.fromX509Certificate((X509Certificate) pkcs11Cert, (PrivateKey) key);
break;
}
}
}
MimeEntity decryEntity = cryptographer.decrypt(encEntity, decryptCert);
assertNotNull(decryEntity);
byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
} finally {
System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderName", "Hello");
System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderClassNames", "sun.security.pkcs11.SunPKCS11");
OptionsManager.destroyInstance();
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CryptographerTest method testEncryptAndDecryptMimeEntity.
private void testEncryptAndDecryptMimeEntity(EncryptionAlgorithm encAlg) throws Exception {
X509Certificate cert = TestUtils.getExternalCert("user1");
SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
cryptographer.setEncryptionAlgorithm(encAlg);
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");
MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
assertNotNull(decryEntity);
byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class CryptographerTest method testEncryptAndDecryptMultipartEntity.
private void testEncryptAndDecryptMultipartEntity(EncryptionAlgorithm encAlgo) throws Exception {
X509Certificate cert = TestUtils.getExternalCert("user1");
SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
cryptographer.setEncryptionAlgorithm(encAlgo);
MimeEntity entityText = new MimeEntity();
entityText.setText("Hello world.");
entityText.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
entityText.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
MimeEntity entityXML = new MimeEntity();
entityXML.setText("<Test></Test>");
entityXML.setHeader(MimeStandard.ContentTypeHeader, "text/xml");
MimeMultipart mpEntity = new MimeMultipart();
mpEntity.addBodyPart(entityText);
mpEntity.addBodyPart(entityXML);
MimeEntity encEntity = cryptographer.encrypt(mpEntity, cert);
assertNotNull(encEntity);
X509CertificateEx certex = TestUtils.getInternalCert("user1");
MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
assertNotNull(decryEntity);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
mpEntity.writeTo(oStream);
InternetHeaders hdrs = new InternetHeaders();
hdrs.addHeader(MimeStandard.ContentTypeHeader, mpEntity.getContentType());
MimeEntity orgEntity = new MimeEntity(hdrs, oStream.toByteArray());
byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
byte[] entityBytes = EntitySerializer.Default.serializeToBytes(orgEntity);
System.out.println("Original:\r\n" + new String(entityBytes));
System.out.println("\r\n\r\n\r\nNew:\r\n" + new String(decryEntityBytes));
assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class KeyStoreCertificateStore method add.
/**
* Adds a certificate into the keystore with a given alias name.
* @param cert The certificate to add to the keystore.
* @param alias The alias of the certificate.
*/
public void add(X509Certificate cert, String alias) {
if (certs.contains(cert)) {
LOGGER.warn("Certificate already exists in store. Use update() instead.");
return;
}
try {
certs.add(cert);
if (cert instanceof X509CertificateEx)
ks.setKeyEntry(alias, ((X509CertificateEx) cert).getPrivateKey(), privateKeyPassword == null ? null : privateKeyPassword.toCharArray(), new Certificate[] { cert });
else
ks.setCertificateEntry(alias, cert);
// persist
FileOutputStream outStream = new FileOutputStream(keyStoreFile);
ks.store(outStream, keyStorePassword == null ? null : keyStorePassword.toCharArray());
IOUtils.closeQuietly(outStream);
} catch (Throwable e) {
LOGGER.warn("Error adding certificate to store: " + e.getMessage());
}
}
use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.
the class DefaultNHINDAgent method decryptMessage.
/*
* Decrypts the message content.
*/
protected MimeEntity decryptMessage(IncomingMessage message) {
MimeEntity decryptedEntity = null;
if (this.encryptionEnabled) {
//
for (X509Certificate cert : message.getDomainRecipients().getCertificates()) {
try {
if (cert instanceof X509CertificateEx) {
X509CertificateEx privCert = (X509CertificateEx) cert;
decryptedEntity = this.cryptographer.decrypt(message.getMessage(), privCert);
break;
}
} catch (Exception e) {
LOGGER.info("Could not decrypt with message private cert subject " + cert.getSubjectDN().getName(), e);
}
}
} else {
try {
decryptedEntity = new MimeEntity(message.getMessage().getRawInputStream());
} catch (MessagingException ex) {
throw new AgentException(AgentError.MissingMessage);
}
}
if (decryptedEntity == null) {
throw new AgentException(AgentError.UntrustedMessage);
}
return decryptedEntity;
}
Aggregations