Java Code Examples for java.security.KeyStore.PrivateKeyEntry#getCertificateChain()

The following examples show how to use java.security.KeyStore.PrivateKeyEntry#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: KSPrivateKeyEntry.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * The default constructor for KSPrivateKeyEntry.
 * 
 * @param alias
 *            the given alias
 * @param privateKeyEntry
 *            the keystore private key entry
 */
public KSPrivateKeyEntry(final String alias, final PrivateKeyEntry privateKeyEntry) {
	this.alias = alias;
	certificate = new CertificateToken((X509Certificate) privateKeyEntry.getCertificate());
	final List<CertificateToken> x509CertificateList = new ArrayList<>();
	final Certificate[] simpleCertificateChain = privateKeyEntry.getCertificateChain();
	for (final Certificate currentCertificate : simpleCertificateChain) {
		x509CertificateList.add(new CertificateToken((X509Certificate) currentCertificate));
	}
	final CertificateToken[] certificateChain_ = new CertificateToken[x509CertificateList.size()];
	certificateChain = x509CertificateList.toArray(certificateChain_);
	privateKey = privateKeyEntry.getPrivateKey();
}
 
Example 2
Source File: X509KeyManagerImpl.java    From openjsse with GNU General Public License v2.0 4 votes vote down vote up
@Override
public X509Certificate[] getCertificateChain(String alias) {
    PrivateKeyEntry entry = getEntry(alias);
    return entry == null ? null :
            (X509Certificate[])entry.getCertificateChain();
}
 
Example 3
Source File: X509KeyManagerImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public X509Certificate[] getCertificateChain(String alias) {
    PrivateKeyEntry entry = getEntry(alias);
    return entry == null ? null :
            (X509Certificate[])entry.getCertificateChain();
}
 
Example 4
Source File: AddPrivateKey.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void test(Provider p, PrivateKeyEntry entry) throws Exception {
    PrivateKey key = entry.getPrivateKey();
    X509Certificate[] chain = (X509Certificate[])entry.getCertificateChain();
    PublicKey publicKey = chain[0].getPublicKey();
    System.out.println(toString(key));
    sign(p, key, publicKey);

    KeyStore ks = KeyStore.getInstance("PKCS11", p);
    ks.load(null, null);
    if (ks.size() != 0) {
        throw new Exception("KeyStore not empty");
    }
    List<String> aliases;

    // test 1: add entry
    ks.setKeyEntry(ALIAS1, key, null, chain);
    aliases = aliases(ks);
    if (aliases.size() != 1) {
        throw new Exception("size not 1: " + aliases);
    }
    if (aliases.get(0).equals(ALIAS1) == false) {
        throw new Exception("alias mismatch: " + aliases);
    }

    PrivateKey key2 = (PrivateKey)ks.getKey(ALIAS1, null);
    System.out.println(toString(key2));
    X509Certificate[] chain2 =
            (X509Certificate[]) ks.getCertificateChain(ALIAS1);
    if (Arrays.equals(chain, chain2) == false) {
        throw new Exception("chain mismatch");
    }
    sign(p, key2, publicKey);

    ks.deleteEntry(ALIAS1);
    if (ks.size() != 0) {
        throw new Exception("KeyStore not empty");
    }

    // test 2: translate to session object, then add entry
    KeyFactory kf = KeyFactory.getInstance(key.getAlgorithm(), p);
    PrivateKey key3 = (PrivateKey)kf.translateKey(key);
    System.out.println(toString(key3));
    sign(p, key3, publicKey);

    ks.setKeyEntry(ALIAS2, key3, null, chain);
    aliases = aliases(ks);
    if (aliases.size() != 1) {
        throw new Exception("size not 1");
    }
    if (aliases.get(0).equals(ALIAS2) == false) {
        throw new Exception("alias mismatch: " + aliases);
    }

    PrivateKey key4 = (PrivateKey)ks.getKey(ALIAS2, null);
    System.out.println(toString(key4));
    X509Certificate[] chain4 = (X509Certificate[])
            ks.getCertificateChain(ALIAS2);
    if (Arrays.equals(chain, chain4) == false) {
        throw new Exception("chain mismatch");
    }
    sign(p, key4, publicKey);

    // test 3: change alias
    ks.setKeyEntry(ALIAS3, key3, null, chain);
    aliases = aliases(ks);
    if (aliases.size() != 1) {
        throw new Exception("size not 1");
    }
    if (aliases.get(0).equals(ALIAS3) == false) {
        throw new Exception("alias mismatch: " + aliases);
    }

    PrivateKey key5 = (PrivateKey)ks.getKey(ALIAS3, null);
    System.out.println(toString(key5));
    X509Certificate[] chain5 = (X509Certificate[])
            ks.getCertificateChain(ALIAS3);
    if (Arrays.equals(chain, chain5) == false) {
        throw new Exception("chain mismatch");
    }
    sign(p, key5, publicKey);

    ks.deleteEntry(ALIAS3);
    if (ks.size() != 0) {
        throw new Exception("KeyStore not empty");
    }

    System.out.println("OK");
}