Search in sources :

Example 41 with PolicyExpression

use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.

the class JavaSerializedObjectLexiconPolicyParser_serializeTest method testSerialize_simpleExpression_validateExpression.

public void testSerialize_simpleExpression_validateExpression() throws Exception {
    final JavaSerializedObjectLexiconPolicyParser parser = new JavaSerializedObjectLexiconPolicyParser();
    // build the expression
    final PolicyValue<Boolean> op1 = PolicyValueFactory.getInstance(true);
    final LiteralPolicyExpression<Boolean> expr = LiteralPolicyExpressionFactory.getInstance(op1);
    final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
    operands.add(expr);
    final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.LOGICAL_NOT, operands);
    // serialize
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    parser.serialize(oper, outStream);
    assertTrue(outStream.size() > 0);
    String serialzied = new String(outStream.toByteArray());
    System.out.println(serialzied);
    // deserialize
    final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    final PolicyExpression deserExpression = parser.parse(inStream);
    assertNotNull(deserExpression);
    assertEquals(PolicyExpressionType.OPERATION, deserExpression.getExpressionType());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Vector(java.util.Vector) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Example 42 with PolicyExpression

use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.

the class WSSmtpAgentConfig method addPolicyToMap.

public void addPolicyToMap(Map<String, Collection<PolicyExpression>> policyMap, String domainName, CertPolicyGroupReltn policyReltn) {
    // check to see if the domain is in the map
    Collection<PolicyExpression> policyExpressionCollection = policyMap.get(domainName);
    if (policyExpressionCollection == null) {
        policyExpressionCollection = new ArrayList<PolicyExpression>();
        policyMap.put(domainName, policyExpressionCollection);
    }
    final CertPolicy policy = policyReltn.getCertPolicy();
    final PolicyLexicon lexicon;
    if (policy.getLexicon().equals(org.nhind.config.PolicyLexicon.JAVA_SER))
        lexicon = PolicyLexicon.JAVA_SER;
    else if (policy.getLexicon().equals(org.nhind.config.PolicyLexicon.SIMPLE_TEXT_V1))
        lexicon = PolicyLexicon.SIMPLE_TEXT_V1;
    else
        lexicon = PolicyLexicon.XML;
    final InputStream inStr = new ByteArrayInputStream(policy.getPolicyData());
    try {
        // grab a parser and compile this policy
        final PolicyLexiconParser parser = PolicyLexiconParserFactory.getInstance(lexicon);
        policyExpressionCollection.add(parser.parse(inStr));
    } catch (PolicyParseException ex) {
        throw new SmtpAgentException(SmtpAgentError.InvalidConfigurationFormat, "Failed parse policy into policy expression: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(inStr);
    }
}
Also used : PolicyLexicon(org.nhindirect.policy.PolicyLexicon) SmtpAgentException(org.nhindirect.gateway.smtp.SmtpAgentException) CertPolicy(org.nhind.config.CertPolicy) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PolicyLexiconParser(org.nhindirect.policy.PolicyLexiconParser) PolicyExpression(org.nhindirect.policy.PolicyExpression) PolicyParseException(org.nhindirect.policy.PolicyParseException)

Example 43 with PolicyExpression

use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.

the class TrustModel_isCertPolicyCompliantTest method testIsCertPolicyCompliant_noPolicyExpression_assertTrue.

public void testIsCertPolicyCompliant_noPolicyExpression_assertTrue() throws Exception {
    final TrustModel model = new TrustModel();
    final PolicyResolver resolver = mock(PolicyResolver.class);
    when(resolver.getIncomingPolicy((InternetAddress) any())).thenReturn(new ArrayList<PolicyExpression>());
    model.setTrustPolicyResolver(resolver);
    final X509Certificate cert = mock(X509Certificate.class);
    assertTrue(model.isCertPolicyCompliant(new InternetAddress("me@test.com"), cert));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) PolicyExpression(org.nhindirect.policy.PolicyExpression) X509Certificate(java.security.cert.X509Certificate)

Example 44 with PolicyExpression

use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.

the class TrustModel_isCertPolicyCompliantTest method testIsCertPolicyCompliant_policyCompliant_assertTrue.

public void testIsCertPolicyCompliant_policyCompliant_assertTrue() throws Exception {
    final TrustModel model = new TrustModel();
    final PolicyFilter filter = mock(PolicyFilter.class);
    when(filter.isCompliant((X509Certificate) any(), (PolicyExpression) any())).thenReturn(true);
    final PolicyResolver resolver = mock(PolicyResolver.class);
    final PolicyExpression expression = mock(PolicyExpression.class);
    when(resolver.getIncomingPolicy((InternetAddress) any())).thenReturn(Arrays.asList(expression));
    model.setTrustPolicyResolver(resolver);
    model.setPolicyFilter(filter);
    final X509Certificate cert = mock(X509Certificate.class);
    assertTrue(model.isCertPolicyCompliant(new InternetAddress("me@test.com"), cert));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) PolicyFilter(org.nhindirect.policy.PolicyFilter) PolicyResolver(org.nhindirect.stagent.policy.PolicyResolver) PolicyExpression(org.nhindirect.policy.PolicyExpression) X509Certificate(java.security.cert.X509Certificate)

Example 45 with PolicyExpression

use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.

the class DefaultNHINDAgent method filterCertificatesByPolicy.

protected Collection<X509Certificate> filterCertificatesByPolicy(InternetAddress sender, PolicyResolver resolver, Collection<X509Certificate> certsToFilter, boolean incoming) {
    if (certsToFilter == null || certsToFilter.isEmpty())
        return certsToFilter;
    final Collection<X509Certificate> filteredCerts;
    // apply the policy if it exists
    if (resolver != null) {
        filteredCerts = new ArrayList<X509Certificate>();
        // get the incoming policy based on the sender
        final Collection<PolicyExpression> expressions = (incoming) ? resolver.getIncomingPolicy(sender) : resolver.getOutgoingPolicy(sender);
        // loop through filters and certs
        for (X509Certificate cert : certsToFilter) {
            boolean filterCert = false;
            for (PolicyExpression expression : expressions) {
                try {
                    // check for compliance
                    if (!policyFilter.isCompliant(cert, expression)) {
                        filterCert = true;
                        break;
                    }
                } catch (PolicyRequiredException requiredException) {
                    filterCert = true;
                    break;
                } catch (PolicyProcessException processException) {
                    throw new AgentException(AgentError.InvalidPolicy, processException);
                }
            }
            if (!filterCert)
                filteredCerts.add(cert);
        }
    } else
        filteredCerts = certsToFilter;
    return filteredCerts;
}
Also used : PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) PolicyExpression(org.nhindirect.policy.PolicyExpression) X509Certificate(java.security.cert.X509Certificate) PolicyProcessException(org.nhindirect.policy.PolicyProcessException)

Aggregations

PolicyExpression (org.nhindirect.policy.PolicyExpression)54 X509Certificate (java.security.cert.X509Certificate)21 InternetAddress (javax.mail.internet.InternetAddress)18 LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)16 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)16 PolicyResolver (org.nhindirect.stagent.policy.PolicyResolver)12 InputStream (java.io.InputStream)11 PolicyFilter (org.nhindirect.policy.PolicyFilter)10 Vector (java.util.Vector)9 Collection (java.util.Collection)8 HashMap (java.util.HashMap)8 Opcode (org.nhindirect.policy.Opcode)7 KeyUsageExtensionField (org.nhindirect.policy.x509.KeyUsageExtensionField)7 CertificateResolver (org.nhindirect.stagent.cert.CertificateResolver)7 TrustAnchorResolver (org.nhindirect.stagent.trust.TrustAnchorResolver)7 File (java.io.File)6 StackMachineCompiler (org.nhindirect.policy.impl.machine.StackMachineCompiler)6 ExtendedKeyUsageExtensionField (org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5