Example usage for java.security.cert X509Certificate getSubjectX500Principal

List of usage examples for java.security.cert X509Certificate getSubjectX500Principal

Introduction

In this page you can find the example usage for java.security.cert X509Certificate getSubjectX500Principal.

Prototype

public X500Principal getSubjectX500Principal() 

Source Link

Document

Returns the subject (subject distinguished name) value from the certificate as an X500Principal .

Usage

From source file:test.unit.be.fedict.eid.applet.service.signer.ODFSignatureVerifierTest.java

@Test
public void testGetSigners() throws Exception {
    // setup/*w  w  w  .  j av  a 2  s . co m*/
    URL odfUrl = ODFSignatureVerifierTest.class.getResource("/hello-world-signed.odt");
    assertNotNull(odfUrl);

    // operate
    List<X509Certificate> result = ODFSignatureVerifier.getSigners(odfUrl);

    // verify
    assertNotNull(result);
    assertEquals(1, result.size());
    X509Certificate signer = result.get(0);
    LOG.debug("signer: " + signer.getSubjectX500Principal());
}

From source file:test.unit.be.fedict.eid.applet.service.signer.ODFSignatureVerifierTest.java

@Test
public void testGetSigners2() throws Exception {
    // setup// ww  w  . j ava2 s  . c  o  m
    URL odfUrl = ODFSignatureVerifierTest.class.getResource("/hello-world-signed-twice.odt");
    assertNotNull(odfUrl);

    // operate
    List<X509Certificate> result = ODFSignatureVerifier.getSigners(odfUrl);

    // verify
    assertNotNull(result);
    assertEquals(2, result.size());
    for (X509Certificate signer : result) {
        LOG.debug("signer: " + signer.getSubjectX500Principal());
    }
}

From source file:be.fedict.eid.applet.service.signer.KeyInfoKeySelector.java

@SuppressWarnings("unchecked")
@Override//from  w ww .j  av  a  2s . c o  m
public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {
    LOG.debug("select key");
    if (null == keyInfo) {
        throw new KeySelectorException("no ds:KeyInfo present");
    }
    List<XMLStructure> keyInfoContent = keyInfo.getContent();
    this.certificate = null;
    for (XMLStructure keyInfoStructure : keyInfoContent) {
        if (false == (keyInfoStructure instanceof X509Data)) {
            continue;
        }
        X509Data x509Data = (X509Data) keyInfoStructure;
        List<Object> x509DataList = x509Data.getContent();
        for (Object x509DataObject : x509DataList) {
            if (false == (x509DataObject instanceof X509Certificate)) {
                continue;
            }
            X509Certificate certificate = (X509Certificate) x509DataObject;
            LOG.debug("certificate: " + certificate.getSubjectX500Principal());
            if (null == this.certificate) {
                /*
                 * The first certificate is presumably the signer.
                 */
                this.certificate = certificate;
                LOG.debug("signer certificate: " + certificate.getSubjectX500Principal());
            }
        }
        if (null != this.certificate) {
            return this;
        }
    }
    throw new KeySelectorException("No key found!");
}

From source file:edu.vt.middleware.crypt.x509.DNUtilsTest.java

/**
 * @param  certFile  File containing X.509 certificate data.
 * @param  attribute  Attribute to fetch.
 * @param  expectedValue  Expected attribute value.
 *
 * @throws  Exception  On test failure.//from  w w w .ja  v  a2s .  c  om
 */
@Test(groups = { "functest", "x509" }, dataProvider = "singlevaluedata")
public void testGetAttributeValue(final File certFile, final AttributeType attribute,
        final String expectedValue) throws Exception {
    logger.info("Testing getting attribute value of subject DN of " + certFile);

    final X509Certificate cert = (X509Certificate) CryptReader.readCertificate(certFile);
    final String actualValue = DNUtils.getAttributeValue(cert.getSubjectX500Principal(), attribute);
    AssertJUnit.assertEquals(expectedValue, actualValue);
}

From source file:org.eclipse.smarthome.io.net.http.internal.ExtensibleTrustManagerImplTest.java

private void mockSubjectForCertificate(X509Certificate certificate, String principal) {
    when(certificate.getSubjectX500Principal()).thenReturn(new X500Principal(principal));

}

From source file:edu.vt.middleware.crypt.x509.DNUtilsTest.java

/**
 * @param  certFile  File containing X.509 certificate data.
 * @param  attribute  Attribute to fetch.
 * @param  expectedValues  Expected attribute values.
 *
 * @throws  Exception  On test failure./*w  ww. j av a 2 s  . c om*/
 */
@Test(groups = { "functest", "x509" }, dataProvider = "multivaluesdata")
public void testGetAttributeValues(final File certFile, final AttributeType attribute,
        final String[] expectedValues) throws Exception {
    logger.info("Testing getting attribute values of subject DN of " + certFile);

    final X509Certificate cert = (X509Certificate) CryptReader.readCertificate(certFile);
    final String[] actualValues = DNUtils.getAttributeValues(cert.getSubjectX500Principal(), attribute);
    AssertJUnit.assertEquals(Arrays.asList(expectedValues), Arrays.asList(actualValues));
}

From source file:test.integ.be.fedict.trust.util.TestUtils.java

public static X509CRL generateCrl(PrivateKey issuerPrivateKey, X509Certificate issuerCertificate,
        DateTime thisUpdate, DateTime nextUpdate, List<String> deltaCrlUris, boolean deltaCrl,
        List<RevokedCertificate> revokedCertificates, String signatureAlgorithm)
        throws InvalidKeyException, CRLException, IllegalStateException, NoSuchAlgorithmException,
        SignatureException, CertificateParsingException {

    X509V2CRLGenerator crlGenerator = new X509V2CRLGenerator();
    crlGenerator.setThisUpdate(thisUpdate.toDate());
    crlGenerator.setNextUpdate(nextUpdate.toDate());
    crlGenerator.setSignatureAlgorithm(signatureAlgorithm);
    crlGenerator.setIssuerDN(issuerCertificate.getSubjectX500Principal());

    for (RevokedCertificate revokedCertificate : revokedCertificates) {
        crlGenerator.addCRLEntry(revokedCertificate.serialNumber, revokedCertificate.revocationDate.toDate(),
                CRLReason.privilegeWithdrawn);
    }//from www  .j  a  v  a2s . co  m

    crlGenerator.addExtension(X509Extensions.AuthorityKeyIdentifier, false,
            new AuthorityKeyIdentifierStructure(issuerCertificate));
    crlGenerator.addExtension(X509Extensions.CRLNumber, false, new CRLNumber(BigInteger.ONE));

    if (null != deltaCrlUris && !deltaCrlUris.isEmpty()) {
        DistributionPoint[] deltaCrlDps = new DistributionPoint[deltaCrlUris.size()];
        for (int i = 0; i < deltaCrlUris.size(); i++) {
            deltaCrlDps[i] = getDistributionPoint(deltaCrlUris.get(i));
        }
        CRLDistPoint crlDistPoint = new CRLDistPoint(deltaCrlDps);
        crlGenerator.addExtension(X509Extensions.FreshestCRL, false, crlDistPoint);
    }

    if (deltaCrl) {
        crlGenerator.addExtension(X509Extensions.DeltaCRLIndicator, true, new CRLNumber(BigInteger.ONE));
    }

    return crlGenerator.generate(issuerPrivateKey);
}

From source file:org.dataone.proto.trove.jsse.X509CertificateToolset.java

/**
 * Returns the RFC2253 string representation for the certificate's subject This is the standard format used in
 * DataONE./*from w ww  . j av  a2 s.  c om*/
 *
 * @param certificate
 * @return subject DN using RFC2253 format
 */
public String getSubjectDN(X509Certificate certificate) {
    if (certificate == null) {
        return null;
    }
    X500Principal principal = certificate.getSubjectX500Principal();
    String dn = principal.getName(X500Principal.RFC2253);
    //dn = standardizeDN(dn);
    return dn;
}

From source file:edu.vt.middleware.crypt.x509.LDAPv3DNFormatterTest.java

/**
 * @param  certFile  File containing X.509 certificate data.
 * @param  expectedSubjectDn  Expected certficate subject DN.
 * @param  expectedIssuerDn  Expected certficate issuer DN.
 *
 * @throws  Exception  On test failure.//from www. jav  a2  s  .co  m
 */
@Test(groups = { "functest", "x509" }, dataProvider = "certdata")
public void testFormat(final File certFile, final String expectedSubjectDn, final String expectedIssuerDn)
        throws Exception {
    logger.info("Testing formatting subject and issuer DNs of " + certFile);

    final X509Certificate cert = (X509Certificate) CryptReader.readCertificate(certFile);
    final LDAPv3DNFormatter formatter = new LDAPv3DNFormatter();
    AssertJUnit.assertEquals(expectedSubjectDn, formatter.format(cert.getSubjectX500Principal()));
    AssertJUnit.assertEquals(expectedIssuerDn, formatter.format(cert.getIssuerX500Principal()));
}

From source file:test.integ.be.fedict.trust.Foreigner201305Test.java

/**
 * wget --recursive -e robots=off http://certs.eid.belgium.be
 * //from w  w w .  j a  va2 s .  c o  m
 * @throws Exception
 */
@Test
public void testAllCertificateAuthorities() throws Exception {
    File dirFile = new File("/home/fcorneli/certs/certs.eid.belgium.be");
    LOG.debug("directory: " + dirFile.getAbsolutePath());
    File[] certFiles = dirFile.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            LOG.debug(name);
            return name.endsWith("crt");
        }
    });
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    for (File certFile : certFiles) {
        X509Certificate certificate = (X509Certificate) certificateFactory
                .generateCertificate(new FileInputStream(certFile));
        LOG.debug("certificate: " + certificate.getSubjectX500Principal());
        RSAPublicKey rsaPublicKey = (RSAPublicKey) certificate.getPublicKey();
        int modulusSize = rsaPublicKey.getModulus().toByteArray().length;
        LOG.debug("modulus size: " + modulusSize);
        int signatureSize = certificate.getSignature().length;
        LOG.debug("signature size: " + signatureSize);
        assertEquals(modulusSize - 1, signatureSize);
    }
    LOG.debug("total number of CAs: " + certFiles.length);
}