X509CertificateSubjectRdnSelector implements CertSelector { public boolean match(Certificate cert) { if (!(cert instanceof X509Certificate)) { return false; } if (rdnArray == null) { return true; } try { X500Name x500name = getX500SubjectName((X509Certificate) cert); boolean ok = true; for (RDN rdn : rdnArray) { AttributeTypeAndValue expectedTypeAndValue = rdn.getFirst(); ok = ok && contains(x500name, expectedTypeAndValue); } return ok; } catch (CertificateEncodingException e) { return false; } } X509CertificateSubjectRdnSelector(String rdnString);  X509CertificateSubjectRdnSelector(RDN[] rdnArray); boolean match(Certificate cert); Object clone();  }
@Test public void testX509CertificateMatches_Ok() throws Exception { X509CertificateSubjectRdnSelector certSelector = new X509CertificateSubjectRdnSelector("E=publications@guardtime.com"); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testX509CertificateMatchesUsingLongRdnType_Ok() throws Exception { X509CertificateSubjectRdnSelector certSelector = new X509CertificateSubjectRdnSelector("EMAILADDRESS=publications@guardtime.com"); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testX509CertificateMatchesUsingDifferentRdnValues_Ok() throws Exception { X509CertificateSubjectRdnSelector certSelector = new X509CertificateSubjectRdnSelector("EMAILADDRESS=publications@guardtime.com,L=Tallinn, C=EE, C=EE"); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testX509CertificateDoesNotMatch_Ok() throws Exception { X509CertificateSubjectRdnSelector certSelector = new X509CertificateSubjectRdnSelector("E=publications2@guardtime.com"); Assert.assertFalse(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testX509CertificateDoesNotMatchUsingMultipleRdnValues_Ok() throws Exception { X509CertificateSubjectRdnSelector certSelector = new X509CertificateSubjectRdnSelector("E=publications@guardtime.com,E=publications2@guardtime.com"); Assert.assertFalse(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testMultipleDifferentRdnWithSameOid() throws Exception { X509CertificateSubjectRdnSelector certSelector = new TestX509CertificateSubjectRdnSelector( "E=publications@guardtime.com", new AttributeTypeAndValue[] {EMAIL_INVALID, EMAIL}); Assert.assertFalse(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testMultipleSameRdnWithSameOid() throws Exception { X509CertificateSubjectRdnSelector certSelector = new TestX509CertificateSubjectRdnSelector( "E=publications@guardtime.com", new AttributeTypeAndValue[] {EMAIL, EMAIL}); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testMultiValuedRdnInCertificate() throws Exception { X509CertificateSubjectRdnSelector certSelector = new TestX509CertificateSubjectRdnSelector( "E=publications@guardtime.com", new AttributeTypeAndValue[] {EMAIL, EMAIL}, true); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testMultiValuedRdnContainingInvalidEmail() throws Exception { X509CertificateSubjectRdnSelector certSelector = new TestX509CertificateSubjectRdnSelector( "E=publications@guardtime.com", new AttributeTypeAndValue[] {EMAIL_INVALID, EMAIL}, true); Assert.assertFalse(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }
@Test public void testDifferentAsn1Encodings() throws Exception { X509CertificateSubjectRdnSelector certSelector = new TestX509CertificateSubjectRdnSelector("E=publications@guardtime.com", new AttributeTypeAndValue[]{new AttributeTypeAndValue(BCStyle.EmailAddress, new DERPrintableString("publications@guardtime.com"))}, true); Assert.assertTrue(certSelector.match(X509CertUtil.toCert(TestUtil.loadBytes("server.crt")))); }