SignaturePolicyInfo { SignaturePolicyIdentifier toSignaturePolicyIdentifier() { String algId = DigestAlgorithms.getAllowedDigest(this.policyDigestAlgorithm); if (algId == null || algId.length() == 0) { throw new IllegalArgumentException("Invalid policy hash algorithm"); } SignaturePolicyIdentifier signaturePolicyIdentifier = null; SigPolicyQualifierInfo spqi = null; if (this.policyUri != null && this.policyUri.length() > 0) { spqi = new SigPolicyQualifierInfo(PKCSObjectIdentifiers.id_spq_ets_uri, new DERIA5String(this.policyUri)); } signaturePolicyIdentifier = new SignaturePolicyIdentifier(new SignaturePolicyId(DERObjectIdentifier.getInstance(new DERObjectIdentifier(this.policyIdentifier.replace("urn:oid:", ""))), new OtherHashAlgAndValue(new AlgorithmIdentifier(new ASN1ObjectIdentifier(algId)), new DEROctetString(this.policyHash)), SignUtils.createSigPolicyQualifiers(spqi))); return signaturePolicyIdentifier; } SignaturePolicyInfo(String policyIdentifier, byte[] policyHash, String policyDigestAlgorithm, String policyUri);  SignaturePolicyInfo(String policyIdentifier, String policyHashBase64, String policyDigestAlgorithm, String policyUri); String getPolicyIdentifier(); byte[] getPolicyHash(); String getPolicyDigestAlgorithm(); String getPolicyUri();  }
@Test public void toSignaturePolicyIdentifierTest() { SignaturePolicyIdentifier actual = new SignaturePolicyInfo(POLICY_IDENTIFIER, POLICY_HASH, POLICY_DIGEST_ALGORITHM, POLICY_URI).toSignaturePolicyIdentifier(); DERIA5String deria5String = new DERIA5String(POLICY_URI); SigPolicyQualifierInfo sigPolicyQualifierInfo = new SigPolicyQualifierInfo( PKCSObjectIdentifiers.id_spq_ets_uri, deria5String); DEROctetString derOctetString = new DEROctetString(POLICY_HASH); String algId = DigestAlgorithms.getAllowedDigest(POLICY_DIGEST_ALGORITHM); ASN1ObjectIdentifier asn1ObjectIdentifier = new ASN1ObjectIdentifier(algId); AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier( asn1ObjectIdentifier); OtherHashAlgAndValue otherHashAlgAndValue = new OtherHashAlgAndValue(algorithmIdentifier, derOctetString); DERObjectIdentifier derObjectIdentifier = new DERObjectIdentifier(POLICY_IDENTIFIER); ASN1ObjectIdentifier derObjectIdentifierInstance = DERObjectIdentifier.getInstance(derObjectIdentifier); SignaturePolicyId signaturePolicyId = new SignaturePolicyId(derObjectIdentifierInstance, otherHashAlgAndValue, SignUtils.createSigPolicyQualifiers(sigPolicyQualifierInfo)); SignaturePolicyIdentifier expected = new SignaturePolicyIdentifier(signaturePolicyId); Assert.assertEquals(expected.toASN1Primitive(), actual.toASN1Primitive()); }
@Test public void toSignaturePolicyIdentifierUnexpectedAlgorithmTest() { junitExpectedException.expect(IllegalArgumentException.class); junitExpectedException.expectMessage("Invalid policy hash algorithm"); new SignaturePolicyInfo(POLICY_IDENTIFIER, POLICY_HASH, "SHA-12345", POLICY_URI).toSignaturePolicyIdentifier(); }