Search in sources :

Example 31 with InternetAddress

use of javax.mail.internet.InternetAddress in project nhin-d by DirectProject.

the class DefaultNHINDAgent_bindAddressesOutgoingMessageTest method testBindAddresses_singlePrivateCerts_policyFiltered_assertNoBoundPrivateCerts.

public void testBindAddresses_singlePrivateCerts_policyFiltered_assertNoBoundPrivateCerts() throws Exception {
    final X509Certificate cert = mock(X509Certificate.class);
    final CertificateResolver publicCertResolver = mock(CertificateResolver.class);
    when(publicCertResolver.getCertificates((InternetAddress) any())).thenReturn(new ArrayList<X509Certificate>());
    final CertificateResolver privateCertResolver = mock(CertificateResolver.class);
    when(privateCertResolver.getCertificates((InternetAddress) any())).thenReturn(Arrays.asList(cert));
    final DefaultNHINDAgent agent = new DefaultNHINDAgent("Cerner.com", privateCertResolver, publicCertResolver, mock(TrustAnchorResolver.class)) {

        @Override
        protected Collection<X509Certificate> filterCertificatesByPolicy(InternetAddress sender, PolicyResolver resolver, Collection<X509Certificate> certsToFilter, boolean incoming) {
            return Collections.emptyList();
        }
    };
    final OutgoingMessage message = buildOutgoingMessage("MultipartMimeMessage.txt");
    message.setAgent(agent);
    agent.bindAddresses(message);
    assertEquals(0, message.getSender().getCertificates().size());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) TrustAnchorResolver(org.nhindirect.stagent.trust.TrustAnchorResolver) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) Collection(java.util.Collection) CertificateResolver(org.nhindirect.stagent.cert.CertificateResolver) X509Certificate(java.security.cert.X509Certificate)

Example 32 with InternetAddress

use of javax.mail.internet.InternetAddress in project nhin-d by DirectProject.

the class DefaultNHINDAgent_filterCertificateByPolicyTest method testFilterCertificateByPolicy_outgoingPolicyCompliant_assertNoCertsFiltered.

public void testFilterCertificateByPolicy_outgoingPolicyCompliant_assertNoCertsFiltered() throws Exception {
    final PolicyFilter filter = mock(PolicyFilter.class);
    when(filter.isCompliant((X509Certificate) any(), (PolicyExpression) any())).thenReturn(true);
    final PolicyExpression expression = mock(PolicyExpression.class);
    final X509Certificate cert = mock(X509Certificate.class);
    final Collection<X509Certificate> certs = Arrays.asList(cert);
    final PolicyResolver resolver = mock(PolicyResolver.class);
    when(resolver.getOutgoingPolicy((InternetAddress) any())).thenReturn(Arrays.asList(expression));
    final DefaultNHINDAgent agent = new DefaultNHINDAgent("", mock(CertificateResolver.class), mock(CertificateResolver.class), mock(TrustAnchorResolver.class));
    agent.setPolicyFilter(filter);
    Collection<X509Certificate> filteredCerts = agent.filterCertificatesByPolicy(new InternetAddress("me@you.com"), resolver, certs, false);
    assertEquals(1, filteredCerts.size());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) TrustAnchorResolver(org.nhindirect.stagent.trust.TrustAnchorResolver) PolicyFilter(org.nhindirect.policy.PolicyFilter) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) PolicyExpression(org.nhindirect.policy.PolicyExpression) CertificateResolver(org.nhindirect.stagent.cert.CertificateResolver) X509Certificate(java.security.cert.X509Certificate)

Example 33 with InternetAddress

use of javax.mail.internet.InternetAddress in project nhin-d by DirectProject.

the class DefaultNHINDAgent_filterCertificateByPolicyTest method testFilterCertificateByPolicy_notCompliant_assertNoCertsFiltered.

public void testFilterCertificateByPolicy_notCompliant_assertNoCertsFiltered() throws Exception {
    final PolicyFilter filter = mock(PolicyFilter.class);
    when(filter.isCompliant((X509Certificate) any(), (PolicyExpression) any())).thenReturn(false);
    final PolicyExpression expression = mock(PolicyExpression.class);
    final X509Certificate cert = mock(X509Certificate.class);
    final Collection<X509Certificate> certs = Arrays.asList(cert);
    final PolicyResolver resolver = mock(PolicyResolver.class);
    when(resolver.getIncomingPolicy((InternetAddress) any())).thenReturn(Arrays.asList(expression));
    final DefaultNHINDAgent agent = new DefaultNHINDAgent("", mock(CertificateResolver.class), mock(CertificateResolver.class), mock(TrustAnchorResolver.class));
    agent.setPolicyFilter(filter);
    Collection<X509Certificate> filteredCerts = agent.filterCertificatesByPolicy(new InternetAddress("me@you.com"), resolver, certs, true);
    assertEquals(0, filteredCerts.size());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) TrustAnchorResolver(org.nhindirect.stagent.trust.TrustAnchorResolver) PolicyFilter(org.nhindirect.policy.PolicyFilter) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) PolicyExpression(org.nhindirect.policy.PolicyExpression) CertificateResolver(org.nhindirect.stagent.cert.CertificateResolver) X509Certificate(java.security.cert.X509Certificate)

Example 34 with InternetAddress

use of javax.mail.internet.InternetAddress in project nhin-d by DirectProject.

the class DefaultNHINDAgent_filterCertificateByPolicyTest method testFilterCertificateByPolicy_noOutgoingExpressions_assertNoCertsFiltered.

public void testFilterCertificateByPolicy_noOutgoingExpressions_assertNoCertsFiltered() throws Exception {
    final X509Certificate cert = mock(X509Certificate.class);
    final Collection<X509Certificate> certs = Arrays.asList(cert);
    final PolicyResolver resolver = mock(PolicyResolver.class);
    when(resolver.getOutgoingPolicy((InternetAddress) any())).thenReturn(new ArrayList<PolicyExpression>());
    final DefaultNHINDAgent agent = new DefaultNHINDAgent("", mock(CertificateResolver.class), mock(CertificateResolver.class), mock(TrustAnchorResolver.class));
    Collection<X509Certificate> filteredCerts = agent.filterCertificatesByPolicy(new InternetAddress("me@you.com"), resolver, certs, false);
    assertEquals(1, filteredCerts.size());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) TrustAnchorResolver(org.nhindirect.stagent.trust.TrustAnchorResolver) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) PolicyExpression(org.nhindirect.policy.PolicyExpression) CertificateResolver(org.nhindirect.stagent.cert.CertificateResolver) X509Certificate(java.security.cert.X509Certificate)

Example 35 with InternetAddress

use of javax.mail.internet.InternetAddress in project nhin-d by DirectProject.

the class DNSCertDumper method main.

public static void main(String[] args) {
    if (args.length == 0) {
        printUsage();
        System.exit(-1);
    }
    //String emailAddress = "Richard_Campbell@direct.healthvault-stage.com";
    //String emailAddress = "ca.direct.healthvault-stage.com";
    //String emailAddress = "ca.direct.healthvault-stage.com";
    //"beau@direct3.h1sp.com";
    String emailAddress = "";
    String[] servers = null;
    String outFile = null;
    // Check parameters
    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        // Options
        if (!arg.startsWith("-")) {
            System.err.println("Error: Unexpected argument [" + arg + "]\n");
            printUsage();
            System.exit(-1);
        } else if (arg.equalsIgnoreCase("-add")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing email address");
                System.exit(-1);
            }
            emailAddress = args[++i];
        } else if (arg.equals("-server")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing DNS server list");
                System.exit(-1);
            }
            servers = args[++i].split(",");
        } else if (arg.equals("-out")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing output file.");
                System.exit(-1);
            }
            outFile = args[++i];
        } else if (arg.equals("-help")) {
            printUsage();
            System.exit(-1);
        } else {
            System.err.println("Error: Unknown argument " + arg + "\n");
            printUsage();
            System.exit(-1);
        }
    }
    if (emailAddress == null || emailAddress.isEmpty()) {
        System.err.println("You must provide an email address.");
        printUsage();
    } else {
        DNSCertificateStore dnsStore = (servers != null) ? new DNSCertificateStore(Arrays.asList(servers)) : new DNSCertificateStore();
        try {
            Collection<X509Certificate> certs = dnsStore.getCertificates(new InternetAddress(emailAddress));
            if (certs == null || certs.size() == 0) {
                System.out.println("No certs found");
            } else {
                int idx = 1;
                for (X509Certificate cert : certs) {
                    String certFileName = "";
                    String certFileHold = (outFile == null || outFile.isEmpty()) ? emailAddress + ".der" : outFile;
                    if (certs.size() > 1) {
                        int index = certFileHold.lastIndexOf(".");
                        if (index < 0)
                            certFileHold += "(" + idx + ")";
                        else {
                            certFileName = certFileHold.substring(0, index - 1) + "(" + idx + ")" + certFileHold.substring(index);
                        }
                    } else
                        certFileName = certFileHold;
                    File certFile = new File(certFileName);
                    if (certFile.exists())
                        certFile.delete();
                    System.out.println("Writing cert file: " + certFile.getAbsolutePath());
                    FileUtils.writeByteArrayToFile(certFile, cert.getEncoded());
                    ++idx;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.exit(0);
}
Also used : DNSCertificateStore(org.nhindirect.stagent.cert.impl.DNSCertificateStore) InternetAddress(javax.mail.internet.InternetAddress) File(java.io.File) X509Certificate(java.security.cert.X509Certificate)

Aggregations

InternetAddress (javax.mail.internet.InternetAddress)255 MimeMessage (javax.mail.internet.MimeMessage)106 MessagingException (javax.mail.MessagingException)69 Session (javax.mail.Session)49 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)42 Address (javax.mail.Address)41 Message (javax.mail.Message)40 Date (java.util.Date)38 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)36 AddressException (javax.mail.internet.AddressException)34 X509Certificate (java.security.cert.X509Certificate)32 MimeBodyPart (javax.mail.internet.MimeBodyPart)30 Test (org.junit.Test)29 IOException (java.io.IOException)26 MimeMultipart (javax.mail.internet.MimeMultipart)26 PolicyExpression (org.nhindirect.policy.PolicyExpression)18 HashMap (java.util.HashMap)17 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)17 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)17