Java 8 X509Certificate - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 X509Certificate class.

This code example shows how to use the following methods:getIssuerDN, getIssuerX500Principal, getSubjectDN, getSubjectX500Principal
     */ 
    public void invoke(X509Certificate cert, GSIConstants.CertificateType certType) throws CertPathValidatorException {
        if (!requireSigningPolicyCheck(certType)) {
            return; 
        } 
        X500Principal caPrincipal = cert.getIssuerX500Principal();
        SigningPolicy policy;
        try { 
            policy = this.policyStore.getSigningPolicy(caPrincipal);
        } catch (CertStoreException e) {
            throw new CertPathValidatorException(e);
        } 
 
        if (policy == null) {
            throw new CertPathValidatorException("No signing policy for " + cert.getIssuerDN());
        } 
 
        boolean valid = policy.isValidSubject(cert.getSubjectX500Principal());
 
        if (!valid) {

7
CodeRank
This code example shows how to use the following methods:getSubjectX500Principal
        + "PmMdpbttL0O7mUboBC8CAwEAAaM1MDMwMQYDVR0RBCowKIIVbG9jYWxob3N0Lmxv\n" 
        + "Y2FsZG9tYWlugglsb2NhbGhvc3SHBH8AAAEwDQYJKoZIhvcNAQEFBQADQQD0ntfL\n" 
        + "DCzOCv9Ma6Lv5o5jcYWVxvBSTsnt22hsJpWD1K7iY9lbkLwl0ivn73pG2evsAn9G\n" 
        + "X8YKH52fnHsCrhSD\n" 
        + "-----END CERTIFICATE-----"); 
    assertEquals(new X500Principal("CN=localhost"), certificate.getSubjectX500Principal());
 
    FakeSSLSession session = new FakeSSLSession(certificate);
    assertTrue(verifier.verify("localhost", session));
    assertTrue(verifier.verify("localhost.localdomain", session));
    assertFalse(verifier.verify("local.host", session));
 
    assertTrue(verifier.verify("127.0.0.1", session));
    assertFalse(verifier.verify("127.0.0.2", session));
  } 
 
  @Test public void wildcardsCannotMatchIpAddresses() throws Exception { 
    // openssl req -x509 -nodes -days 36500 -subj '/CN=*.0.0.1' -newkey rsa:512 -out cert.pem 
    SSLSession session = session(""
        + "-----BEGIN CERTIFICATE-----\n" 
7
CodeRank
This code example shows how to use the following methods:getIssuerX500Principal, getSerialNumber
     * 
     * @param certificate certificate providing the issue and serial number and subject key identifier. 
     */ 
    public JcaSignerId(X509Certificate certificate)
    { 
        super(convertPrincipal(certificate.getIssuerX500Principal()), certificate.getSerialNumber(), CMSUtils.getSubjectKeyId(certificate));
    } 
 
    /** 
     * Construct a signer identifier based on the provided issuer and serial number.. 
     * 
     * @param issuer the issuer to use. 
     * @param serialNumber  the serial number to use. 
     */ 
    public JcaSignerId(X500Principal issuer, BigInteger serialNumber)
    { 
        super(convertPrincipal(issuer), serialNumber);
    } 
 
    /** 
7
CodeRank
This code example shows how to use the following methods:getPublicKey, getNotAfter, getNotBefore, getSerialNumber, getSigAlgName
This code example shows how to use the following methods:getIssuerX500Principal, getSerialNumber
     * 
     * @param certificate certificate providing the issue and serial number and subject key identifier. 
     */ 
    public JcaX509CertificateHolderSelector(X509Certificate certificate)
    { 
        super(convertPrincipal(certificate.getIssuerX500Principal()), certificate.getSerialNumber(), getSubjectKeyId(certificate));
    } 
 
    /** 
     * Construct a signer identifier based on the provided issuer and serial number.. 
     * 
     * @param issuer the issuer to use. 
     * @param serialNumber  the serial number to use. 
     */ 
    public JcaX509CertificateHolderSelector(X500Principal issuer, BigInteger serialNumber)
    { 
        super(convertPrincipal(issuer), serialNumber);
    } 
 
    /** 
7
CodeRank
This code example shows how to use the following methods:getIssuerDN, getNotAfter, getNotBefore, getSerialNumber, getSigAlgName
                    CertificateFactory.getInstance("X509");
            X509Certificate crt = (X509Certificate)
                    cf.generateCertificate(fr);
            logger.info("Read certificate:");
            logger.info("\tCertificate for: " +
                    crt.getSubjectDN());
            logger.info("\tCertificate issued by: " +
                    crt.getIssuerDN());
            logger.info("\tCertificate is valid from " +
                    crt.getNotBefore() + " to " + crt.getNotAfter());
            logger.info("\tCertificate SN# " +
                    crt.getSerialNumber());
            logger.info("\tGenerated with " +
                    crt.getSigAlgName());
 
            return crt;
        } catch (Exception e) {
            e.printStackTrace();
        } 
 
6
CodeRank
This code example shows how to use the following methods:getPublicKey, getSigAlgName, getSignature, getTBSCertificate
    private void doTest( 
        String      certName)
        throws Exception 
    { 
        X509Certificate  cert = loadCert(certName);
        byte[]           tbs = cert.getTBSCertificate();
        Signature        sig = Signature.getInstance(cert.getSigAlgName(), "BC");
         
        sig.initVerify(cert.getPublicKey());
         
        sig.update(tbs);
         
        assertFalse(sig.verify(cert.getSignature()));
    } 
 
    private X509Certificate loadCert(
        String certName)
        throws Exception 
    { 
        CertificateFactory rd = CertificateFactory.getInstance("X.509", "BC");
7
CodeRank
This code example shows how to use the following methods:getEncoded, getIssuerDN, getIssuerX500Principal, getSerialNumber
             *  
             * XXX: not correct according to RFC 4514. 
             */ 
            // TODO: check if issuerName is different on getTBSCertificate 
            // issuerName = PrincipalUtil.getIssuerX509Principal(certificate).getName().replace(",", ", "); 
            issuerName = certificate.getIssuerDN().getName().replace(",", ", ");
        } else { 
            issuerName = certificate.getIssuerX500Principal().toString();
        } 
        issuerSerial.setX509IssuerName(issuerName);
        issuerSerial.setX509SerialNumber(certificate.getSerialNumber());
 
        byte[] encodedCertificate;
        try { 
            encodedCertificate = certificate.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new RuntimeException("certificate encoding error: "
                    + e.getMessage(), e);
        } 
        DigestAlgAndValueType certDigest = certId.addNewCertDigest(); 
6
CodeRank
This code example shows how to use the following methods:getIssuerDN, getNotAfter, getNotBefore, getSigAlgName, getSubjectDN
defaultTrustManager.checkServerTrusted(certificates, authType);
	} 
 
	private void logCertificate(final X509Certificate cert) {
		log.trace("  Subject DN: {}", cert.getSubjectDN());
		log.trace("  Signature algorithm name: {}", cert.getSigAlgName());
		log.trace("  Valid from: {}", cert.getNotBefore());
		log.trace("  Valid until: {}", cert.getNotAfter());
		log.trace("  Issuer DN: {}", cert.getIssuerDN());
	} 
 
	/** 
	 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() 
	 */ 
	@Override 
	public X509Certificate[] getAcceptedIssuers() {
		return this.defaultTrustManager.getAcceptedIssuers();
	} 
} 
6
CodeRank
This code example shows how to use the following methods:getType, getIssuerDN, getNotAfter, getNotBefore, getSubjectDN
	} 
 
	public void list(String alias, Certificate cert) {
		if (cert instanceof X509Certificate) {
			X509Certificate x509 = (X509Certificate) cert;
			System.out.println(" " + alias + " (" + x509.getType() + ")\n" +
					"  valid from " + x509.getNotBefore() + " to " + x509.getNotAfter() + "\n" +
					"  for        " + x509.getSubjectDN().getName() + "\n" +
					"  issued by  " + x509.getIssuerDN().getName());
		} else { 
			System.out.println(" " + alias + " (" + cert.getType() + ")");
		} 
	} 
 
	public void print(Certificate c) {
		System.out.println(c.getType() + " " + c);
	} 
 
	abstract void run(File keystore, Map args) throws Exception;
} 
7
CodeRank

Related Java 8 X509Certificate Questions & Answers:

See Code Examples for Java 8 X509Certificate Methods: