use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class CertificateStore_getUsableCertsTest method testGetUsableCerts_inValidUserCert_noCertsRetrieved.
public void testGetUsableCerts_inValidUserCert_noCertsRetrieved() throws Exception {
final X509CertificateEx userCert = TestUtils.getInternalCert("user1");
final X509CertificateEx domainCert = TestUtils.getInternalCert("gm2552");
CertificateStore store = new CertificateStoreAdapter() {
protected Collection<X509Certificate> filterUsable(Collection<X509Certificate> certs) {
if (certs.iterator().next().getSubjectDN().getName().contains("user1"))
return null;
else
return certs;
}
public Collection<X509Certificate> getCertificates(String subjectName) {
if (subjectName.contains("user1@domain.com"))
return Arrays.asList((X509Certificate) userCert);
else
return Arrays.asList((X509Certificate) domainCert);
}
};
boolean exceptionOccured = false;
try {
store.getCertificates(new InternetAddress("user1@domain.com"));
} catch (NHINDException e) {
assertEquals(e.getError(), AgentError.AllCertsInResolverInvalid);
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class TrustChainValidator_resolveIssuersTest method testResolveIssuers_AIAExists_validateResolved.
public void testResolveIssuers_AIAExists_validateResolved() throws Exception {
final TrustChainValidatorWrapper validator = new TrustChainValidatorWrapper() {
@Override
protected Collection<X509Certificate> downloadCertsFromAIA(String url) throws NHINDException {
try {
retrievedURL = url;
return Arrays.asList(TestUtils.loadCertificate("CernerDirect Cert Professional Community CA.der"));
} catch (Exception e) {
throw new NHINDException(e);
}
}
};
final Collection<X509Certificate> resolvedIssuers = new ArrayList<X509Certificate>();
final Collection<X509Certificate> anchors = new ArrayList<X509Certificate>();
final TrustChainValidatorWrapper spyValidator = spy(validator);
spyValidator.resolveIssuers(TestUtils.loadCertificate("demo.sandboxcernerdirect.com.der"), resolvedIssuers, 0, anchors);
assertEquals(1, resolvedIssuers.size());
assertEquals(TestUtils.loadCertificate("CernerDirect Cert Professional Community CA.der"), resolvedIssuers.iterator().next());
verify(spyValidator, times(2)).downloadCertsFromAIA((String) any());
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class TrustChainValidator_resolveIssuersTest method testResolveIssuers_AIAExists_resolveToRoot_validateResolved.
public void testResolveIssuers_AIAExists_resolveToRoot_validateResolved() throws Exception {
final TrustChainValidatorWrapper validator = new TrustChainValidatorWrapper() {
protected Collection<X509Certificate> downloadCertsFromAIA(String url) throws NHINDException {
try {
if (url.contains("sandbox"))
return Arrays.asList(TestUtils.loadCertificate("CernerDirect Cert Professional Community CA.der"));
else
return Arrays.asList(TestUtils.loadCertificate("CernerRoot.der"));
} catch (Exception e) {
throw new NHINDException(e);
}
}
};
final Collection<X509Certificate> resolvedIssuers = new ArrayList<X509Certificate>();
final Collection<X509Certificate> anchors = new ArrayList<X509Certificate>();
final TrustChainValidatorWrapper spyValidator = spy(validator);
spyValidator.resolveIssuers(TestUtils.loadCertificate("demo.sandboxcernerdirect.com.der"), resolvedIssuers, 0, anchors);
assertEquals(2, resolvedIssuers.size());
Iterator<X509Certificate> iter = resolvedIssuers.iterator();
assertEquals(TestUtils.loadCertificate("CernerDirect Cert Professional Community CA.der"), iter.next());
assertEquals(TestUtils.loadCertificate("CernerRoot.der"), iter.next());
verify(spyValidator, times(2)).downloadCertsFromAIA((String) any());
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class MDNFactory_createTest method getNotificationFieldsAsHeaders.
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMultipart mm) {
InternetHeaders retVal = null;
if (mm == null)
throw new IllegalArgumentException("Multipart can not be null");
try {
if (mm.getCount() < 2)
throw new IllegalArgumentException("Multipart can not be null");
// the second part should be the notification
BodyPart part = mm.getBodyPart(1);
if (part.getContent() instanceof DispositionNotification) {
return ((DispositionNotification) part.getContent()).getNotifications();
}
// parse fields
retVal = new InternetHeaders();
String[] fields = Notification.getPartContentBodyAsString(part).split("\r\n");
for (String field : fields) {
int idx = field.indexOf(":");
if (idx > -1) {
String name = field.substring(0, idx);
String value = field.substring(idx + 1).trim();
retVal.setHeader(name, value);
}
}
} catch (Exception e) {
throw new NHINDException("Failed to parse notification fields.", e);
}
return retVal;
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class TrustChainValidator_IntermidiateCert_Test method certFromData.
private X509Certificate certFromData(byte[] data) {
X509Certificate retVal = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
// lets try this a as a PKCS12 data stream first
try {
KeyStore localKeyStore = KeyStore.getInstance("PKCS12", CryptoExtensions.getJCEProviderName());
localKeyStore.load(bais, "".toCharArray());
Enumeration<String> aliases = localKeyStore.aliases();
// we are really expecting only one alias
if (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
X509Certificate cert = (X509Certificate) localKeyStore.getCertificate(alias);
// check if there is private key
Key key = localKeyStore.getKey(alias, "".toCharArray());
if (key != null && key instanceof PrivateKey) {
retVal = X509CertificateEx.fromX509Certificate(cert, (PrivateKey) key);
} else
retVal = cert;
}
} catch (Exception e) {
// must not be a PKCS12 stream, go on to next step
}
if (retVal == null) {
//try X509 certificate factory next
bais.reset();
bais = new ByteArrayInputStream(data);
retVal = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(bais);
}
bais.close();
} catch (Exception e) {
throw new NHINDException("Data cannot be converted to a valid X.509 Certificate", e);
}
return retVal;
}
Aggregations