Search in sources :

Example 11 with NHINDException

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);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Collection(java.util.Collection) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate)

Example 12 with NHINDException

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());
}
Also used : TrustChainValidatorWrapper(org.nhindirect.stagent.trust.TrustChainValidator_getIntermediateCertsByAIATest.TrustChainValidatorWrapper) ArrayList(java.util.ArrayList) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) NHINDException(org.nhindirect.stagent.NHINDException)

Example 13 with NHINDException

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());
}
Also used : TrustChainValidatorWrapper(org.nhindirect.stagent.trust.TrustChainValidator_getIntermediateCertsByAIATest.TrustChainValidatorWrapper) ArrayList(java.util.ArrayList) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) NHINDException(org.nhindirect.stagent.NHINDException)

Example 14 with NHINDException

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;
}
Also used : BodyPart(javax.mail.BodyPart) DispositionNotification(com.sun.mail.dsn.DispositionNotification) InternetHeaders(javax.mail.internet.InternetHeaders) NHINDException(org.nhindirect.stagent.NHINDException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 15 with NHINDException

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;
}
Also used : PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyStore(java.security.KeyStore) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) 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