Java Code Examples for javax.net.ssl.X509KeyManager#getCertificateChain()

The following examples show how to use javax.net.ssl.X509KeyManager#getCertificateChain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenSSLContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public X509Certificate[] getCertificateChain(String alias) {
    X509Certificate[] chain = null;
    X509KeyManager x509KeyManager = certificate.getCertificateKeyManager();
    if (x509KeyManager != null) {
        if (alias == null) {
            alias = "tomcat";
        }
        chain = x509KeyManager.getCertificateChain(alias);
        if (chain == null) {
            alias = findAlias(x509KeyManager, certificate);
            chain = x509KeyManager.getCertificateChain(alias);
        }
    }

    return chain;
}
 
Example 2
Source File: FileTrustStoreSslSocketFactory.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
public X509Certificate[] getCertificateChain(final String alias) {
    for (final X509KeyManager keyManager : keyManagers) {
        final X509Certificate[] chain = keyManager.getCertificateChain(alias);
        if (chain != null && chain.length > 0) {
            return chain;
        }
    }
    return null;
}
 
Example 3
Source File: SSLKeyManager.java    From PADListener with GNU General Public License v2.0 5 votes vote down vote up
public synchronized X509Certificate[] getCertificateChain(String alias) {
    String[] parts = alias.split(SEP, 2);
    String description = parts[0];
    alias = parts[1];
    X509KeyManager km = (X509KeyManager) _managers.get(description);
    return km.getCertificateChain(alias);
}
 
Example 4
Source File: CompositeX509KeyManager.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the first non-null certificate chain associated with the given alias, or {@code null}
 * if the alias can't be found.
 */
@Override
public @Nullable X509Certificate[] getCertificateChain(String alias){
	for (List<X509KeyManager> keyManagers : keyManagers.values()) {
		for (X509KeyManager x509KeyManager : keyManagers) {
			X509Certificate[] chain = x509KeyManager.getCertificateChain(alias);
			if (chain != null && chain.length > 0) {
				return chain;
			}
		}
	}
	return null;
}
 
Example 5
Source File: OpenSSLContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void addCertificate(SSLHostConfigCertificate certificate) throws Exception {
    // Load Server key and certificate
    if (certificate.getCertificateFile() != null) {
        // Set certificate
        SSLContext.setCertificate(ctx,
                SSLHostConfig.adjustRelativePath(certificate.getCertificateFile()),
                SSLHostConfig.adjustRelativePath(certificate.getCertificateKeyFile()),
                certificate.getCertificateKeyPassword(), getCertificateIndex(certificate));
        // Set certificate chain file
        SSLContext.setCertificateChainFile(ctx,
                SSLHostConfig.adjustRelativePath(certificate.getCertificateChainFile()), false);
        // Set revocation
        SSLContext.setCARevocation(ctx,
                SSLHostConfig.adjustRelativePath(
                        sslHostConfig.getCertificateRevocationListFile()),
                SSLHostConfig.adjustRelativePath(
                        sslHostConfig.getCertificateRevocationListPath()));
    } else {
        String alias = certificate.getCertificateKeyAlias();
        X509KeyManager x509KeyManager = certificate.getCertificateKeyManager();
        if (alias == null) {
            alias = "tomcat";
        }
        X509Certificate[] chain = x509KeyManager.getCertificateChain(alias);
        if (chain == null) {
            alias = findAlias(x509KeyManager, certificate);
            chain = x509KeyManager.getCertificateChain(alias);
        }
        PrivateKey key = x509KeyManager.getPrivateKey(alias);
        StringBuilder sb = new StringBuilder(BEGIN_KEY);
        String encoded = BASE64_ENCODER.encodeToString(key.getEncoded());
        if (encoded.endsWith("\n")) {
            encoded = encoded.substring(0, encoded.length() - 1);
        }
        sb.append(encoded);
        sb.append(END_KEY);
        SSLContext.setCertificateRaw(ctx, chain[0].getEncoded(),
                sb.toString().getBytes(StandardCharsets.US_ASCII),
                getCertificateIndex(certificate));
        for (int i = 1; i < chain.length; i++) {
            SSLContext.addChainCertificateRaw(ctx, chain[i].getEncoded());
        }
    }
}