Example usage for java.security KeyStore toString

List of usage examples for java.security KeyStore toString

Introduction

In this page you can find the example usage for java.security KeyStore toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:eu.eidas.auth.engine.SAMLEngineUtils.java

/**
 *
 * @param keystore// w w w . ja  v a2 s . c  o  m
 * @param serialNumber
 * @param issuer
 * @return a certificate/alias pair from the keystore, having the given issuer and serialNumber
 * @throws KeyStoreException
 * @throws SAMLEngineException
 */
public static CertificateAliasPair getCertificatePair(KeyStore keystore, String serialNumber, String issuer)
        throws KeyStoreException, SAMLEngineException {
    String alias = null;
    String aliasCert;
    X509Certificate certificate;
    boolean find = false;
    LOG.debug("cherche dans " + keystore.toString() + " numSerie=" + serialNumber + " issuer=" + issuer);
    for (final Enumeration<String> e = keystore.aliases(); e.hasMoreElements() && !find;) {
        aliasCert = e.nextElement();
        certificate = (X509Certificate) keystore.getCertificate(aliasCert);

        final String serialNum = certificate.getSerialNumber().toString(16);

        Principal p = certificate.getIssuerDN();
        String name = p.getName();

        X500Name issuerDN = new X500Name(name);
        X500Name issuerDNConf = new X500Name(issuer);

        if (serialNum.equalsIgnoreCase(serialNumber)
                && X500PrincipalUtil.principalEquals(issuerDN, issuerDNConf)) {
            alias = aliasCert;
            find = true;
        } else {
            LOG.debug("pas pareil numSerie=" + serialNum + " ou issuer=" + name);
        }
    }
    if (!find) {
        throw new SAMLEngineException(
                "Certificate " + issuer + "/" + serialNumber + " cannot be found in keystore ");
    }
    certificate = (X509Certificate) keystore.getCertificate(alias);
    return new CertificateAliasPair(certificate, alias);

}