Search in sources :

Example 16 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class TrustChainValidator_downloadCertFromAIATest method testDownloadCertFromAIA_certNotAtURL_assertException.

@SuppressWarnings("deprecation")
public void testDownloadCertFromAIA_certNotAtURL_assertException() throws Exception {
    final TrustChainValidator validator = new TrustChainValidator();
    final File fl = new File("src/test/resources/certs/bob.derdd");
    boolean exceptionOccurred = false;
    try {
        validator.downloadCertFromAIA(filePrefix + fl.getAbsolutePath());
    } catch (NHINDException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
}
Also used : File(java.io.File) NHINDException(org.nhindirect.stagent.NHINDException)

Example 17 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class TrustChainValidator_downloadCertsFromAIATest method testDownloadCertsFromAIA_certNotAtURL_assertException.

public void testDownloadCertsFromAIA_certNotAtURL_assertException() throws Exception {
    final TrustChainValidator validator = new TrustChainValidator();
    final File fl = new File("src/test/resources/certs/bob.derdd");
    boolean exceptionOccurred = false;
    try {
        validator.downloadCertsFromAIA(filePrefix + fl.getAbsolutePath());
    } catch (NHINDException e) {
        exceptionOccurred = true;
    }
    assertTrue(exceptionOccurred);
}
Also used : File(java.io.File) NHINDException(org.nhindirect.stagent.NHINDException)

Example 18 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class TrustChainValidator_getIntermediateCertsByAIATest method testGetIntermediateCertsByAIA_AIAExists_validateResolved.

public void testGetIntermediateCertsByAIA_AIAExists_validateResolved() throws Exception {
    final TrustChainValidatorWrapper validator = new TrustChainValidatorWrapper() {

        protected Collection<X509Certificate> downloadCertsFromAIA(String url) throws NHINDException {
            try {
                retrievedURL = url;
                return Arrays.asList(TestUtils.loadCertificate("bob.der"));
            } catch (Exception e) {
                throw new NHINDException(e);
            }
        }
    };
    final TrustChainValidatorWrapper spyValidator = spy(validator);
    Collection<X509Certificate> downloadedCerts = spyValidator.getIntermediateCertsByAIA(TestUtils.loadCertificate("demo.sandboxcernerdirect.com.der"));
    assertEquals("http://sandboxcernerdirect.com/professional/public/subordinate.der", spyValidator.retrievedURL);
    assertEquals(1, downloadedCerts.size());
    assertEquals(TestUtils.loadCertificate("bob.der"), downloadedCerts.iterator().next());
    verify(spyValidator, times(1)).downloadCertsFromAIA((String) any());
}
Also used : NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) NHINDException(org.nhindirect.stagent.NHINDException)

Example 19 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class DefaultSmtpAgent method processMessage.

/**
	 * Processes an message from an SMTP stack.  The bridge component between the SMTP stack and the SMTP agent is responsible for
	 * extracting the message, the recipient list, and the sender.  In some cases, the routing headers may have different information than
	 * what is populated in the SMTP MAIL FROM and RCTP TO headers.  In these cases, the SMTP headers should be favored over the routing
	 * headers in the message and passed as the recipient collection and sender to this method.
	 * @param message The message in the SMTP envelope.
	 * @param recipients The recipients of the message.  The RCTP TO headers should be used over the message routing headers.
	 * @param sender The send of the message. The MAIL FROM header should be used over the From: routing header in the message.
	 */
public MessageProcessResult processMessage(MimeMessage message, NHINDAddressCollection recipients, NHINDAddress sender) {
    GatewayState.getInstance().lockForProcessing();
    try {
        LOGGER.trace("Entering processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
        MessageProcessResult retVal = null;
        verifyInitialized();
        preProcessMessage(message, sender);
        Collection<NHINDAddress> originalRecipList = new ArrayList<NHINDAddress>(recipients);
        DefaultMessageEnvelope envelopeToProcess = null;
        try {
            envelopeToProcess = new DefaultMessageEnvelope(new Message(message), recipients, sender);
            envelopeToProcess.setAgent(agent);
            // should always result in either a non null object or an exception
            MessageEnvelope processEvn = processEnvelope(envelopeToProcess);
            retVal = new MessageProcessResult(processEvn, null);
            if (retVal.getProcessedMessage() != null)
                postProcessMessage(retVal);
        } catch (SmtpAgentException e) {
            // rethrow
            LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
            throw e;
        } catch (Exception e) {
            // audit the message rejection
            if (envelopeToProcess != null) {
                Collection<AuditContext> contexts = createContextCollectionFromMessage(envelopeToProcess, Arrays.asList(AuditEvents.DEFAULT_HEADER_CONTEXT));
                if (e instanceof NHINDException) {
                    NHINDException exception = (NHINDException) e;
                    if (exception.getError() != null) {
                        contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_MESSAGE_REASON_CONTEXT, exception.getError().toString()));
                        if (exception.getError() != null && exception.getError() instanceof AgentException && ((AgentException) exception.getError()).getError() == AgentError.NoTrustedRecipients) {
                            StringBuilder rejectedRecips = new StringBuilder();
                            int cnt = 0;
                            for (NHINDAddress address : originalRecipList) {
                                rejectedRecips.append(address.getAddress());
                                if (++cnt < originalRecipList.size())
                                    rejectedRecips.append(", ");
                            }
                            contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_RECIPIENTS_CONTEXT, rejectedRecips.toString()));
                        }
                    }
                }
                auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.REJECTED_MESSAGE_NAME, AuditEvents.EVENT_TYPE), contexts);
            }
            LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
            throw new SmtpAgentException(SmtpAgentError.Unknown, e);
        }
        LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
        return retVal;
    } finally {
        GatewayState.getInstance().unlockFromProcessing();
    }
}
Also used : DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) OutgoingMessage(org.nhindirect.stagent.OutgoingMessage) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) AgentException(org.nhindirect.stagent.AgentException) ArrayList(java.util.ArrayList) NHINDException(org.nhindirect.stagent.NHINDException) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) MessageEnvelope(org.nhindirect.stagent.MessageEnvelope) MessagingException(javax.mail.MessagingException) AgentException(org.nhindirect.stagent.AgentException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) Collection(java.util.Collection) AuditEvent(org.nhindirect.common.audit.AuditEvent)

Example 20 with NHINDException

use of org.nhindirect.stagent.NHINDException in project nhin-d by DirectProject.

the class ConfigServiceCertificateStore 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());
        }
        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;
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) NHINDException(org.nhindirect.stagent.NHINDException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) CertContainer(org.nhindirect.config.model.utils.CertUtils.CertContainer) Key(java.security.Key) PrivateKey(java.security.PrivateKey) CacheException(org.apache.jcs.access.exception.CacheException) NHINDException(org.nhindirect.stagent.NHINDException)

Aggregations

NHINDException (org.nhindirect.stagent.NHINDException)45 X509Certificate (java.security.cert.X509Certificate)30 ArrayList (java.util.ArrayList)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 Key (java.security.Key)10 PrivateKey (java.security.PrivateKey)10 KeyStore (java.security.KeyStore)9 CacheException (org.apache.jcs.access.exception.CacheException)7 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)7 MessagingException (javax.mail.MessagingException)6 Collection (java.util.Collection)4 UnknownHostException (java.net.UnknownHostException)3 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)3 Certificate (java.security.cert.Certificate)3 InternetHeaders (javax.mail.internet.InternetHeaders)3 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 MutableKeyStoreProtectionManager (org.nhindirect.common.crypto.MutableKeyStoreProtectionManager)3 File (java.io.File)2