Search in sources :

Example 6 with PolicyLexiconParser

use of org.nhindirect.policy.PolicyLexiconParser 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 7 with PolicyLexiconParser

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

the class ValidatePanel method validateCert.

private void validateCert() {
    reportText.setText("");
    final File certFile = certFileField.getFile();
    final File policyFile = policyFileField.getFile();
    if (!certFile.exists()) {
        JOptionPane.showMessageDialog(this, "Certificate file does not exist or cannot be found.", "Invalid Cert File", JOptionPane.ERROR_MESSAGE);
        return;
    }
    InputStream policyInput = null;
    if (!feedMode) {
        if (!policyFile.exists()) {
            JOptionPane.showMessageDialog(this, "Policy file does not exist or cannot be found.", "Invalid Policy File", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            // load the policy as an input stream
            policyInput = FileUtils.openInputStream(policyFile);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not load policy from file: " + e.getMessage(), "Invalid Policy File", JOptionPane.ERROR_MESSAGE);
            return;
        }
    } else {
        try {
            final int length = feed.getLength();
            policyInput = IOUtils.toInputStream(feed.getText(0, length));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not load policy: " + e.getMessage(), "Invalid Policy", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    // load the certificate
    X509Certificate cert = null;
    try {
        cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(FileUtils.openInputStream(certFile));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Could not load certificate from file: " + e.getMessage(), "Invalid Cert File", JOptionPane.ERROR_MESSAGE);
        return;
    }
    final DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d yyyy HH:mm:ss", Locale.getDefault());
    final StringBuilder reportTextBuilder = new StringBuilder("Validation run at " + dateFormat.format(Calendar.getInstance(Locale.getDefault()).getTime()) + "\r\n\r\n");
    try {
        final PolicyLexiconParser parser = (feedMode) ? PolicyLexiconParserFactory.getInstance(feedLexicon) : PolicyLexiconParserFactory.getInstance(PolicyLexicon.XML);
        final PolicyExpression policyExpression = parser.parse(policyInput);
        final org.nhindirect.policy.Compiler compiler = new StackMachineCompiler();
        compiler.setReportModeEnabled(true);
        final PolicyFilter filter = PolicyFilterFactory.getInstance(compiler);
        if (filter.isCompliant(cert, policyExpression) && compiler.getCompilationReport().isEmpty())
            reportTextBuilder.append("Certificate is compliant with the provided policy.");
        else {
            reportTextBuilder.append("Certificate is NOT compliant with the provided policy.\r\n\r\n");
            final Collection<String> report = compiler.getCompilationReport();
            if (!report.isEmpty()) {
                for (String reportEntry : report) reportTextBuilder.append(reportEntry + "\r\n");
            }
        }
    } catch (PolicyRequiredException e) {
        reportTextBuilder.append("Validation Successful\r\nCertificate is missing a required field\r\n\t" + e.getMessage());
    } catch (PolicyGrammarException e) {
        reportTextBuilder.append("Validation Failed\r\nError compiling policy\r\n\t" + e.getMessage());
    } catch (Exception e) {
        final ByteArrayOutputStream str = new ByteArrayOutputStream();
        final PrintStream printStr = new PrintStream(str);
        e.printStackTrace();
        e.printStackTrace(printStr);
        final String stackTrace = new String(str.toByteArray());
        reportTextBuilder.append("Validation Failed\r\nError compiling or proccessing policy\r\n\t" + e.getMessage() + "\r\n" + stackTrace);
    } finally {
        reportText.setText(reportTextBuilder.toString());
        IOUtils.closeQuietly(policyInput);
    }
}
Also used : PrintStream(java.io.PrintStream) PolicyGrammarException(org.nhindirect.policy.PolicyGrammarException) PolicyFilter(org.nhindirect.policy.PolicyFilter) InputStream(java.io.InputStream) PolicyExpression(org.nhindirect.policy.PolicyExpression) StackMachineCompiler(org.nhindirect.policy.impl.machine.StackMachineCompiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolicyGrammarException(org.nhindirect.policy.PolicyGrammarException) PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) X509Certificate(java.security.cert.X509Certificate) PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) PolicyLexiconParser(org.nhindirect.policy.PolicyLexiconParser) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with PolicyLexiconParser

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

the class DefaultPolicyFilter method isCompliant.

/**
	 * {@inheritDoc}
	 */
@Override
public boolean isCompliant(X509Certificate cert, InputStream policyStream, PolicyLexicon lexicon) throws PolicyProcessException {
    final PolicyLexiconParser parser = PolicyLexiconParserFactory.getInstance(lexicon);
    final PolicyExpression expression = parser.parse(policyStream);
    return isCompliant(cert, expression);
}
Also used : PolicyLexiconParser(org.nhindirect.policy.PolicyLexiconParser) PolicyExpression(org.nhindirect.policy.PolicyExpression)

Aggregations

PolicyLexiconParser (org.nhindirect.policy.PolicyLexiconParser)8 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 PolicyParseException (org.nhindirect.policy.PolicyParseException)5 PolicyExpression (org.nhindirect.policy.PolicyExpression)4 PolicyLexicon (org.nhindirect.policy.PolicyLexicon)4 IOException (java.io.IOException)3 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 CertificateException (java.security.cert.CertificateException)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 ServiceException (org.nhindirect.common.rest.exceptions.ServiceException)2 SmtpAgentException (org.nhindirect.gateway.smtp.SmtpAgentException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1