use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class CertificateStore method getUsableCerts.
protected Collection<X509Certificate> getUsableCerts(InternetAddress address) {
Collection<X509Certificate> retVal;
if (address == null) {
throw new IllegalArgumentException();
}
// may need to do some parsing of the address because the some email clients may send real name information along with the address
int index = 0;
String theAddress = address.getAddress();
if ((index = theAddress.indexOf("<")) > -1 && theAddress.endsWith(">")) {
theAddress = theAddress.substring(index + 1);
theAddress = theAddress.substring(0, theAddress.length() - 1);
}
// search for "+" extension on the email address
if (theAddress.indexOf("+") > -1 && theAddress.indexOf("@") > -1) {
int startIndex = theAddress.indexOf("+");
int endIndex = theAddress.indexOf("@");
theAddress = theAddress.substring(0, startIndex) + theAddress.substring(endIndex);
}
Collection<X509Certificate> certs = getCertificates("EMAILADDRESS=" + theAddress);
if (certs == null || certs.size() == 0) {
if ((index = theAddress.indexOf("@")) > -1) {
theAddress = theAddress.substring(index + 1);
certs = getCertificates("EMAILADDRESS=" + theAddress);
} else
return null;
}
if (certs == null || certs.size() == 0)
return null;
retVal = filterUsable(certs);
if (retVal == null)
throw new NHINDException(AgentError.AllCertsInResolverInvalid);
return retVal;
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class CRLRevocationManager method getObject.
private static DERObject getObject(String oid, byte[] ext) throws AnnotatedException {
ASN1InputStream aIn = null;
try {
aIn = new ASN1InputStream(ext);
ASN1OctetString octs = (ASN1OctetString) aIn.readObject();
IOUtils.closeQuietly(aIn);
aIn = new ASN1InputStream(octs.getOctets());
return aIn.readObject();
} catch (Exception e) {
throw new NHINDException("exception processing extension " + oid, e);
} finally {
IOUtils.closeQuietly(aIn);
}
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class CertificateStore_getUsableCertsTest method testGetUsableCerts_allCertsInvalid_assertNoCerts.
public void testGetUsableCerts_allCertsInvalid_assertNoCerts() 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) {
return null;
}
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 CryptographerTest method testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt.
public void testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt() throws Exception {
X509Certificate cert = TestUtils.getExternalCert("user1");
SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
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("altnameonly");
boolean exceptionOccured = false;
try {
cryptographer.decrypt(encEntity, certex);
} catch (NHINDException e) {
if (e.getError().equals(MimeError.Unexpected))
;
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class TrustChainValidator_crlCheckTest 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