Search in sources :

Example 1 with SubjectAttributeField

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

the class SimpleTextV1LexiconPolicyParser method buildTBSField.

/**
	 * Builds a certificate reference expression that is an {@link TBSField}.
	 * @param token The token used to build the field.
	 * @return An {@link TBSField} object that represents the token.  Returns null if the token does not represent an {@link TBSField}.
	 * @throws PolicyParseException
	 */
protected PolicyExpression buildTBSField(String token) throws PolicyParseException {
    TBSField<?> retVal = null;
    final TBSFieldName fieldName = TBSFieldName.fromToken(token);
    if (fieldName != null) {
        try {
            final Class<? extends TBSField<?>> fieldRefClass = fieldName.getReferenceClass(token);
            if (fieldRefClass == null)
                throw new PolicyParseException("TBSField with token name " + token + " has not been implemented yet.");
            if (fieldRefClass.equals(IssuerAttributeField.class) || fieldRefClass.equals(SubjectAttributeField.class)) {
                boolean required = token.endsWith("+");
                final String rdnLookupToken = (required) ? token.substring(0, token.length() - 1) : token;
                final RDNAttributeIdentifier identifier = RDNAttributeIdentifier.fromName(rdnLookupToken);
                retVal = fieldRefClass.equals(IssuerAttributeField.class) ? new IssuerAttributeField(required, identifier) : new SubjectAttributeField(required, identifier);
            } else {
                retVal = fieldRefClass.newInstance();
            }
        } catch (PolicyParseException ex) {
            throw ex;
        }///CLOVER:OFF
         catch (Exception e) {
            throw new PolicyParseException("Error building TBSField", e);
        }
    ///CLOVER:ON
    }
    return retVal;
}
Also used : IssuerAttributeField(org.nhindirect.policy.x509.IssuerAttributeField) RDNAttributeIdentifier(org.nhindirect.policy.x509.RDNAttributeIdentifier) TBSFieldName(org.nhindirect.policy.x509.TBSFieldName) SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField) PolicyParseException(org.nhindirect.policy.PolicyParseException) PolicyGrammarException(org.nhindirect.policy.PolicyGrammarException) IOException(java.io.IOException) PolicyParseException(org.nhindirect.policy.PolicyParseException)

Example 2 with SubjectAttributeField

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

the class SubjectAttributeField_injectReferenceValueTest method testInjectRefereneValue_distinguishedName_assertValue.

public void testInjectRefereneValue_distinguishedName_assertValue() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("altNameOnly.der");
    final SubjectAttributeField field = new SubjectAttributeField(true, RDNAttributeIdentifier.DISTINGUISHED_NAME);
    field.injectReferenceValue(cert);
    final Collection<String> values = field.getPolicyValue().getPolicyValue();
    assertEquals(1, values.size());
    Iterator<String> str = values.iterator();
    assertEquals("O=Cerner,L=Kansas City,ST=MO,C=US,CN=altNameOnly", str.next());
}
Also used : SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField) X509Certificate(java.security.cert.X509Certificate)

Example 3 with SubjectAttributeField

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

the class SubjectAttributeField_injectReferenceValueTest method testInjectRefereneValue_rdnAttributeDoesNotExist_notRequired_assertValueCollection.

public void testInjectRefereneValue_rdnAttributeDoesNotExist_notRequired_assertValueCollection() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("altNameOnly.der");
    final SubjectAttributeField field = new SubjectAttributeField(false, RDNAttributeIdentifier.INITIALS);
    field.injectReferenceValue(cert);
    final Collection<String> values = field.getPolicyValue().getPolicyValue();
    assertEquals(0, values.size());
}
Also used : SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField) X509Certificate(java.security.cert.X509Certificate)

Example 4 with SubjectAttributeField

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

the class SimpleTextV1LexiconPolicyParser_buildExpressionTest method testBuildExpression_tbsFieldName_rdnAttribute_validatePolicyExpression.

public void testBuildExpression_tbsFieldName_rdnAttribute_validatePolicyExpression() throws Exception {
    final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
    final InputStream stream = FileUtils.openInputStream(new File("./src/test/resources/policies/literalWithSpaces.txt"));
    final Vector<SimpleTextV1LexiconPolicyParser.TokenTypeAssociation> tokens = parser.parseToTokens(stream);
    // now build expressions
    PolicyExpression expression = parser.buildExpression(tokens.iterator());
    // check that the expression is an equals
    assertNotNull(expression);
    assertEquals(PolicyExpressionType.OPERATION, expression.getExpressionType());
    OperationPolicyExpression operationExpression = (OperationPolicyExpression) expression;
    assertEquals(PolicyOperator.EQUALS, operationExpression.getPolicyOperator());
    // break down the sub operation parameters... should be a cert reference and a literal
    expression = operationExpression.getOperands().get(0);
    assertEquals(PolicyExpressionType.REFERENCE, expression.getExpressionType());
    assertTrue(expression instanceof SubjectAttributeField);
    expression = operationExpression.getOperands().get(1);
    assertEquals(PolicyExpressionType.LITERAL, expression.getExpressionType());
    assertEquals("United States", ((LiteralPolicyExpression<?>) expression).getPolicyValue().getPolicyValue());
}
Also used : LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) InputStream(java.io.InputStream) SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) File(java.io.File) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression)

Example 5 with SubjectAttributeField

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

the class SubjectAttributeField_injectReferenceValueTest method testInjectRefereneValue_noInjection_getPolicyValue_assertException.

public void testInjectRefereneValue_noInjection_getPolicyValue_assertException() throws Exception {
    final SubjectAttributeField field = new SubjectAttributeField(true, RDNAttributeIdentifier.COMMON_NAME);
    boolean exceptionOccured = false;
    try {
        field.getPolicyValue();
    } catch (IllegalStateException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : SubjectAttributeField(org.nhindirect.policy.x509.SubjectAttributeField)

Aggregations

SubjectAttributeField (org.nhindirect.policy.x509.SubjectAttributeField)7 X509Certificate (java.security.cert.X509Certificate)4 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)1 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)1 PolicyExpression (org.nhindirect.policy.PolicyExpression)1 PolicyGrammarException (org.nhindirect.policy.PolicyGrammarException)1 PolicyParseException (org.nhindirect.policy.PolicyParseException)1 PolicyRequiredException (org.nhindirect.policy.PolicyRequiredException)1 IssuerAttributeField (org.nhindirect.policy.x509.IssuerAttributeField)1 RDNAttributeIdentifier (org.nhindirect.policy.x509.RDNAttributeIdentifier)1 TBSFieldName (org.nhindirect.policy.x509.TBSFieldName)1