Search in sources :

Example 1 with KeyUsageExtensionField

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

the class StackMachineCompiler_compileTest method testCompile_simpleBinaryLiteralAndExpressionOperation_assertEntriesAndEvaluation.

public void testCompile_simpleBinaryLiteralAndExpressionOperation_assertEntriesAndEvaluation() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("AlAnderson@hospitalA.direct.visionshareinc.com.der");
    // build the expression
    final Integer keyUsage = KeyUsage.digitalSignature | KeyUsage.keyEncipherment | KeyUsage.nonRepudiation;
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(keyUsage);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final KeyUsageExtensionField expr2 = new KeyUsageExtensionField(true);
    final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
    operands.add(expr1);
    operands.add(expr2);
    final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.EQUALS, operands);
    final StackMachineCompiler compiler = new StackMachineCompiler();
    final Vector<Opcode> entries = compiler.compile(cert, oper);
    assertEquals(3, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(keyUsage, ((StackMachineEntry) entries.get(1)).getValue().getPolicyValue());
    assertEquals(PolicyOperator.EQUALS, ((StackMachineEntry) entries.get(2)).getOperator());
    // execute the compiled expression in the stack machine
    final StackMachine machine = new StackMachine();
    final Boolean evalVal = machine.evaluate(entries);
    assertTrue(evalVal);
}
Also used : Opcode(org.nhindirect.policy.Opcode) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachineCompiler(org.nhindirect.policy.impl.machine.StackMachineCompiler) X509Certificate(java.security.cert.X509Certificate) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachine(org.nhindirect.policy.impl.machine.StackMachine) StackMachineEntry(org.nhindirect.policy.impl.machine.StackMachineEntry) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 2 with KeyUsageExtensionField

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

the class StackMachineCompiler_compileTest method testCompile_multipleEmbeddedOperations_keyUsage_assertEntriesAndEvaluation.

public void testCompile_multipleEmbeddedOperations_keyUsage_assertEntriesAndEvaluation() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("AlAnderson@hospitalA.direct.visionshareinc.com.der");
    // build the expression
    final Integer keyUsage = KeyUsage.nonRepudiation;
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(keyUsage);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final KeyUsageExtensionField expr2 = new KeyUsageExtensionField(true);
    final Vector<PolicyExpression> operands1 = new Vector<PolicyExpression>();
    operands1.add(expr1);
    operands1.add(expr2);
    final OperationPolicyExpression oper1 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.BITWISE_AND, operands1);
    // build outer expression embedding the first operation as a parameter
    final PolicyValue<Integer> op3 = PolicyValueFactory.getInstance(0);
    final LiteralPolicyExpression<Integer> expr3 = LiteralPolicyExpressionFactory.getInstance(op3);
    final Vector<PolicyExpression> operands2 = new Vector<PolicyExpression>();
    operands2.add(oper1);
    operands2.add(expr3);
    final OperationPolicyExpression oper2 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.GREATER, operands2);
    final StackMachineCompiler compiler = new StackMachineCompiler();
    final Vector<Opcode> entries = compiler.compile(cert, oper2);
    assertEquals(5, entries.size());
    assertEquals(op1, ((StackMachineEntry) entries.get(0)).getValue());
    assertEquals(expr2.getPolicyValue().getPolicyValue(), ((StackMachineEntry) entries.get(1)).getValue().getPolicyValue());
    assertEquals(PolicyOperator.BITWISE_AND, ((StackMachineEntry) entries.get(2)).getOperator());
    assertEquals(op3, ((StackMachineEntry) entries.get(3)).getValue());
    assertEquals(PolicyOperator.GREATER, ((StackMachineEntry) entries.get(4)).getOperator());
    // execute the compiled expression in the stack machine
    final StackMachine machine = new StackMachine();
    final Boolean evalVal = machine.evaluate(entries);
    assertTrue(evalVal);
}
Also used : Opcode(org.nhindirect.policy.Opcode) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachineCompiler(org.nhindirect.policy.impl.machine.StackMachineCompiler) X509Certificate(java.security.cert.X509Certificate) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) StackMachine(org.nhindirect.policy.impl.machine.StackMachine) StackMachineEntry(org.nhindirect.policy.impl.machine.StackMachineEntry) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 3 with KeyUsageExtensionField

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

the class JavaSerializedObjectLexiconPolicyParser_serializeTest method testSerialize_complexExpression_validateExpression.

public void testSerialize_complexExpression_validateExpression() throws Exception {
    final JavaSerializedObjectLexiconPolicyParser parser = new JavaSerializedObjectLexiconPolicyParser();
    // build the expression
    final Integer keyUsage = KeyUsage.keyEncipherment;
    final PolicyValue<Integer> op1 = PolicyValueFactory.getInstance(keyUsage);
    final LiteralPolicyExpression<Integer> expr1 = LiteralPolicyExpressionFactory.getInstance(op1);
    final KeyUsageExtensionField expr2 = new KeyUsageExtensionField(true);
    final Vector<PolicyExpression> operands1 = new Vector<PolicyExpression>();
    operands1.add(expr1);
    operands1.add(expr2);
    final OperationPolicyExpression oper1 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.BITWISE_AND, operands1);
    // build outer expression embedding the first operation as a parameter
    final PolicyValue<Integer> op3 = PolicyValueFactory.getInstance(0);
    final LiteralPolicyExpression<Integer> expr3 = LiteralPolicyExpressionFactory.getInstance(op3);
    final Vector<PolicyExpression> operands2 = new Vector<PolicyExpression>();
    operands2.add(oper1);
    operands2.add(expr3);
    final OperationPolicyExpression oper2 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.GREATER, operands2);
    // build a separate expression for extended key usage
    final ExtendedKeyUsageExtensionField expr4 = new ExtendedKeyUsageExtensionField(true);
    final PolicyValue<String> op5 = PolicyValueFactory.getInstance(ExtendedKeyUsageIdentifier.ID_KP_EMAIL_PROTECTION.getId());
    final LiteralPolicyExpression<String> expr5 = LiteralPolicyExpressionFactory.getInstance(op5);
    final Vector<PolicyExpression> operands3 = new Vector<PolicyExpression>();
    operands3.add(expr4);
    operands3.add(expr5);
    final OperationPolicyExpression oper3 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.CONTAINS, operands3);
    // build an and operator and make sure the cert has all policies met
    final Vector<PolicyExpression> operands4 = new Vector<PolicyExpression>();
    operands4.add(oper2);
    operands4.add(oper3);
    final OperationPolicyExpression oper4 = OperationPolicyExpressionFactory.getInstance(PolicyOperator.LOGICAL_AND, operands4);
    // serialize
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    parser.serialize(oper4, 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 : ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) LiteralPolicyExpression(org.nhindirect.policy.LiteralPolicyExpression) PolicyExpression(org.nhindirect.policy.PolicyExpression) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OperationPolicyExpression(org.nhindirect.policy.OperationPolicyExpression) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) ExtendedKeyUsageExtensionField(org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField) Vector(java.util.Vector)

Example 4 with KeyUsageExtensionField

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

the class StackMachine_getCompilationReportTest method testGetCompilationReportTest_mulipleCompilations_reportModeOn_assertCorrectReportSize.

public void testGetCompilationReportTest_mulipleCompilations_reportModeOn_assertCorrectReportSize() throws Exception {
    final X509Certificate cert1 = TestUtils.loadCertificate("umesh.der");
    final X509Certificate cert2 = TestUtils.loadCertificate("AlAnderson@hospitalA.direct.visionshareinc.com.der");
    KeyUsageExtensionField keyExtendExp = new KeyUsageExtensionField(true);
    final StackMachineCompiler machine = new StackMachineCompiler();
    machine.setReportModeEnabled(true);
    assertTrue(machine.isReportModeEnabled());
    machine.compile(cert1, keyExtendExp);
    assertEquals(1, machine.getCompilationReport().size());
    // do it again with a cert that has all required fields
    keyExtendExp = new KeyUsageExtensionField(true);
    machine.compile(cert2, keyExtendExp);
    assertTrue(machine.getCompilationReport().isEmpty());
    // do one more time with the first use case and make sure there is an entry in the report
    keyExtendExp = new KeyUsageExtensionField(true);
    machine.compile(cert1, keyExtendExp);
    assertEquals(1, machine.getCompilationReport().size());
}
Also used : KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) X509Certificate(java.security.cert.X509Certificate)

Example 5 with KeyUsageExtensionField

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

the class StackMachine_getCompilationReportTest method testGetCompilationReportTest_missingRequiredField_reportModeOn_assertNonEmptyCollection.

public void testGetCompilationReportTest_missingRequiredField_reportModeOn_assertNonEmptyCollection() throws Exception {
    final X509Certificate cert = TestUtils.loadCertificate("umesh.der");
    final KeyUsageExtensionField keyExtendExp = new KeyUsageExtensionField(true);
    final StackMachineCompiler machine = new StackMachineCompiler();
    machine.setReportModeEnabled(true);
    assertTrue(machine.isReportModeEnabled());
    machine.compile(cert, keyExtendExp);
    assertEquals(1, machine.getCompilationReport().size());
}
Also used : KeyUsageExtensionField(org.nhindirect.policy.x509.KeyUsageExtensionField) X509Certificate(java.security.cert.X509Certificate)

Aggregations

KeyUsageExtensionField (org.nhindirect.policy.x509.KeyUsageExtensionField)15 X509Certificate (java.security.cert.X509Certificate)10 LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)7 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)7 PolicyExpression (org.nhindirect.policy.PolicyExpression)7 Vector (java.util.Vector)6 ExtendedKeyUsageExtensionField (org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Opcode (org.nhindirect.policy.Opcode)3 PolicyRequiredException (org.nhindirect.policy.PolicyRequiredException)3 StackMachine (org.nhindirect.policy.impl.machine.StackMachine)3 StackMachineCompiler (org.nhindirect.policy.impl.machine.StackMachineCompiler)3 StackMachineEntry (org.nhindirect.policy.impl.machine.StackMachineEntry)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 XMLLexiconPolicyParser (org.nhindirect.policy.impl.XMLLexiconPolicyParser)2 File (java.io.File)1 InputStream (java.io.InputStream)1