use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class TrustChainValidator method getIntermediateCertsByAIA.
/**
* Retrieves intermediate certificate using the AIA extension.
* @param certificate The certificate to search for AIA extensions.
* @return Returns a collection of intermediate certs using the AIA extension. If the AIA extension does not exists
* or the certificate cannot be downloaded from the URL, then an empty list is returned.
*/
protected Collection<X509Certificate> getIntermediateCertsByAIA(X509Certificate certificate) {
final Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
// check to see if there are extensions
final AuthorityInfoAccessExtentionField aiaField = new AuthorityInfoAccessExtentionField(false);
try {
// we can get all names from the AuthorityInfoAccessExtentionField objects
aiaField.injectReferenceValue(certificate);
final Collection<String> urlPairs = aiaField.getPolicyValue().getPolicyValue();
// look through all of the values (if they exist) for caIssuers
for (String urlPair : urlPairs) {
if (urlPair.startsWith(CA_ISSUER_CHECK_STRING)) {
// the url pair is in the format of caIssuer:URL... need to break it
// apart to get the url
final String url = urlPair.substring(CA_ISSUER_CHECK_STRING.length());
// now pull the certificate from the URL
try {
final Collection<X509Certificate> intermCerts = downloadCertsFromAIA(url);
retVal.addAll(intermCerts);
} catch (NHINDException e) {
LOGGER.warn("Intermediate cert cannot be resolved from AIA extension.", e);
}
}
}
}///CLOVER:OFF
catch (PolicyProcessException e) {
LOGGER.warn("Intermediate cert cannot be resolved from AIA extension.", e);
}
return retVal;
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class Notification method getNotificationFieldsAsHeaders.
/**
* Parses the notification part fields of the MimeMultipart body of a MDN message. The multipart is expected to conform to the MDN specification
* as described in RFC3798.
* @return The notification part fields as a set of Internet headers.
*/
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);
try {
Object contecntObj = part.getContent();
if (dsnClass != null && dsnClass.getCanonicalName().equals(contecntObj.getClass().getCanonicalName())) {
retVal = (InternetHeaders) getHeaders.invoke(contecntObj);
return retVal;
}
} catch (Exception e) {
/* no-op */
}
if (!part.getContentType().equalsIgnoreCase(MDNStandard.MediaType.DispositionNotification))
throw new IllegalArgumentException("Notification part content type is not " + MDNStandard.MediaType.DispositionNotification);
// parse fields
retVal = new InternetHeaders();
String[] fields = 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 (MessagingException 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 ConfigServiceRESTCertificateStore method certFromData.
private X509Certificate certFromData(byte[] data) {
X509Certificate retVal = null;
try {
// first check for wrapped data
final CertContainer container = CertUtils.toCertContainer(data);
if (container.getWrappedKeyData() != null) {
// make sure we have a KeyStoreManager configured
if (this.mgr == null) {
throw new NHINDException(AgentError.Unexpected, "Resolved certifiate has wrapped data, but resolver has not been configured to unwrap it.");
}
// create a new wrapped certificate object
retVal = WrappedOnDemandX509CertificateEx.fromX509Certificate(mgr, container.getCert(), container.getWrappedKeyData());
return retVal;
}
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;
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class ConfigServiceCertificateStore method lookupFromConfigStore.
private Collection<X509Certificate> lookupFromConfigStore(String subjectName) {
String domain;
org.nhind.config.Certificate[] certificates;
try {
certificates = proxy.getCertificatesForOwner(subjectName, null);
} catch (Exception e) {
throw new NHINDException("WebService error getting certificates by subject: " + e.getMessage(), e);
}
if (certificates == null || certificates.length == 0) {
// try again with the domain name
int index;
if ((index = subjectName.indexOf("@")) > -1)
domain = subjectName.substring(index + 1);
else
domain = subjectName;
try {
certificates = proxy.getCertificatesForOwner(domain, null);
} catch (Exception e) {
throw new NHINDException("WebService error getting certificates by domain: " + e.getMessage(), e);
}
}
if (certificates == null || certificates.length == 0)
return Collections.emptyList();
Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
for (org.nhind.config.Certificate cert : certificates) {
X509Certificate storeCert = CertStoreUtils.certFromData(mgr, cert.getData());
retVal.add(storeCert);
if (localStoreDelegate != null) {
if (localStoreDelegate.contains(storeCert))
localStoreDelegate.update(storeCert);
else
localStoreDelegate.add(storeCert);
}
}
// add to JCS and cache
try {
if (cache != null)
cache.put(subjectName, retVal);
} catch (CacheException e) {
/*
* TODO: handle exception
*/
}
return retVal;
}
use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.
the class ConfigServiceRESTCertificateStore method lookupFromConfigStore.
private Collection<X509Certificate> lookupFromConfigStore(String subjectName) {
String domain;
Collection<org.nhindirect.config.model.Certificate> certificates;
try {
certificates = certService.getCertificatesByOwner(subjectName);
} catch (Exception e) {
throw new NHINDException("WebService error getting certificates by subject: " + e.getMessage(), e);
}
if (certificates == null || certificates.isEmpty()) {
// try again with the domain name
int index;
if ((index = subjectName.indexOf("@")) > -1)
domain = subjectName.substring(index + 1);
else
domain = subjectName;
try {
certificates = certService.getCertificatesByOwner(domain);
} catch (Exception e) {
throw new NHINDException("WebService error getting certificates by domain: " + e.getMessage(), e);
}
}
if (certificates == null || certificates.isEmpty())
return Collections.emptyList();
Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
for (org.nhindirect.config.model.Certificate cert : certificates) {
X509Certificate storeCert = CertStoreUtils.certFromData(mgr, cert.getData());
retVal.add(storeCert);
}
// add to JCS and cache
try {
if (cache != null)
cache.put(subjectName, retVal);
} catch (CacheException e) {
/*
* TODO: handle exception
*/
}
return retVal;
}
Aggregations