Example usage for java.security.cert CertificateFactory getInstance

List of usage examples for java.security.cert CertificateFactory getInstance

Introduction

In this page you can find the example usage for java.security.cert CertificateFactory getInstance.

Prototype

public static final CertificateFactory getInstance(String type) throws CertificateException 

Source Link

Document

Returns a certificate factory object that implements the specified certificate type.

Usage

From source file:eu.stork.peps.auth.engine.STORKSAMLEngine.java

/**
 * Gets the alias from X.509 Certificate at keystore.
 * /*from   w  ww . j  ava 2  s . co m*/
 * @param keyInfo the key info
 * @param storkOwnKeyStore 
 * @param storkOwnKeyStore 
 * 
 * @return the alias
 */
private String getAlias(final KeyInfo keyInfo, KeyStore storkOwnKeyStore) {

    LOG.debug("Recover alias information");

    String alias = null;
    try {
        final org.opensaml.xml.signature.X509Certificate xmlCert = keyInfo.getX509Datas().get(0)
                .getX509Certificates().get(0);

        // Transform the KeyInfo to X509Certificate.
        CertificateFactory certFact;
        certFact = CertificateFactory.getInstance("X.509");

        final ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(xmlCert.getValue()));

        final X509Certificate cert = (X509Certificate) certFact.generateCertificate(bis);

        final String tokenSerialNumber = cert.getSerialNumber().toString(16);
        final X509Principal tokenIssuerDN = new X509Principal(cert.getIssuerDN().getName());

        String aliasCert;
        X509Certificate certificate;
        boolean find = false;

        for (final Enumeration<String> e = storkOwnKeyStore.aliases(); e.hasMoreElements() && !find;) {
            aliasCert = e.nextElement();
            certificate = (X509Certificate) storkOwnKeyStore.getCertificate(aliasCert);

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

            X509Principal issuerDN = new X509Principal(certificate.getIssuerDN().getName());

            if (serialNum.equalsIgnoreCase(tokenSerialNumber)
                    && X509PrincipalUtil.equals2(issuerDN, tokenIssuerDN)) {
                alias = aliasCert;
                find = true;
            }

        }

    } catch (KeyStoreException e) {
        LOG.error("Procces getAlias from certificate associated into the signing keystore..", e);
    } catch (CertificateException e) {
        LOG.error("Procces getAlias from certificate associated into the signing keystore..", e);
    } catch (RuntimeException e) {
        LOG.error("Procces getAlias from certificate associated into the signing keystore..", e);
    }
    return alias;
}

From source file:eu.stork.peps.auth.engine.STORKSAMLEngine.java

/**
 * Gets the country from X.509 Certificate.
 * /* ww w . j  a v  a  2  s.c o  m*/
 * @param keyInfo the key info
 * 
 * @return the country
 */
private String getCountry(final KeyInfo keyInfo) {
    LOG.debug("Recover country information.");

    String result = "";
    try {
        final org.opensaml.xml.signature.X509Certificate xmlCert = keyInfo.getX509Datas().get(0)
                .getX509Certificates().get(0);

        // Transform the KeyInfo to X509Certificate.
        CertificateFactory certFact;
        certFact = CertificateFactory.getInstance("X.509");

        final ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(xmlCert.getValue()));

        final X509Certificate cert = (X509Certificate) certFact.generateCertificate(bis);

        String distName = cert.getSubjectDN().toString();

        distName = StringUtils.deleteWhitespace(StringUtils.upperCase(distName));

        final String countryCode = "C=";
        final int init = distName.indexOf(countryCode);

        if (init > StringUtils.INDEX_NOT_FOUND) { // Exist country code.
            int end = distName.indexOf(',', init);

            if (end <= StringUtils.INDEX_NOT_FOUND) {
                end = distName.length();
            }

            if (init < end && end > StringUtils.INDEX_NOT_FOUND) {
                result = distName.substring(init + countryCode.length(), end);
                //It must be a two characters value
                if (result.length() > 2) {
                    result = result.substring(0, 2);
                }
            }
        }

    } catch (CertificateException e) {
        LOG.error("Procces getCountry from certificate.");
    }
    return result.trim();
}

From source file:org.apache.juddi.webconsole.hub.UddiHub.java

/**
 * Converts a UDDI Signature to a readable representation of the signing
 * certificate subject name//from www . ja v a 2  s.  c  om
 *
 * @param sig
 * @return human readable signature
 */
public static String SignatureToReadable(SignatureType sig) {
    StringBuilder sb = new StringBuilder();
    // X509Certificate signingcert = null;
    //sb.append("Signature Id: ").append(sig.getKeyInfo().getId());
    for (int i = 0; i < sig.getKeyInfo().getContent().size(); i++) {
        //sb.append("Signature #").append((i + 1)).append(": ");
        JAXBElement get = (JAXBElement) sig.getKeyInfo().getContent().get(i);

        if (get.getValue() instanceof org.w3._2000._09.xmldsig_.X509DataType) {
            X509DataType xd = (X509DataType) get.getValue();
            for (int k = 0; k < xd.getX509IssuerSerialOrX509SKIOrX509SubjectName().size(); k++) {
                if (xd.getX509IssuerSerialOrX509SKIOrX509SubjectName().get(k) instanceof JAXBElement) {
                    JAXBElement element = (JAXBElement) xd.getX509IssuerSerialOrX509SKIOrX509SubjectName()
                            .get(k);
                    if (element.getValue() instanceof byte[]) {
                        try {
                            CertificateFactory cf = CertificateFactory.getInstance("X.509");
                            InputStream is = new ByteArrayInputStream((byte[]) element.getValue());
                            X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
                            is.close();
                            sb.append(cert.getSubjectDN().getName());
                        } catch (Exception ex) {
                        }
                    } else if (element.getValue() instanceof String) {
                        // sb.append((String) element.getValue());
                    }
                }
            }
        }
    }
    return sb.toString();
}

From source file:org.apache.juddi.webconsole.hub.UddiHub.java

/**
 * converts a UDDI Signature Type element into a base64 string
 * containing the raw data for the signing certificate, if present
 *
 * @param sig/*  w w w .  j  a va  2 s.  com*/
 * @return x509 cert
 */
public String SignatureToBase64(SignatureType sig) {
    if (sig == null) {
        return "Error, the signature was nullavailable";
    }
    for (int i = 0; i < sig.getKeyInfo().getContent().size(); i++) {
        JAXBElement get = (JAXBElement) sig.getKeyInfo().getContent().get(i);

        if (get.getValue() instanceof org.w3._2000._09.xmldsig_.X509DataType) {
            X509DataType xd = (X509DataType) get.getValue();
            for (int k = 0; k < xd.getX509IssuerSerialOrX509SKIOrX509SubjectName().size(); k++) {
                if (xd.getX509IssuerSerialOrX509SKIOrX509SubjectName().get(k) instanceof JAXBElement) {
                    JAXBElement element = (JAXBElement) xd.getX509IssuerSerialOrX509SKIOrX509SubjectName()
                            .get(k);
                    if (element.getValue() instanceof byte[]) {
                        try {
                            CertificateFactory cf = CertificateFactory.getInstance("X.509");
                            InputStream is = new ByteArrayInputStream((byte[]) element.getValue());
                            X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
                            is.close();
                            //this is the most supportable way to do this
                            return org.apache.commons.codec.binary.Base64.encodeBase64String(cert.getEncoded());
                            //BASE64Encoder encoder = new BASE64Encoder();
                            //return encoder.encodeBuffer(cert.getEncoded());

                        } catch (Exception ex) {
                            return HandleException(ex);
                        }
                    } else if (element.getValue() instanceof String) {
                    }
                }
            }
        }
    }
    return ResourceLoader.GetResource(session, "errors.nocertavaiable");
}

From source file:com.vmware.identity.idm.client.TenantManagementTest.java

private X509Certificate getCertificate(String certAlias) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf
            .generateCertificate(TenantManagementTest.class.getResourceAsStream(certAlias));

    return cert;/*from   ww w .  ja v  a 2 s  . c o m*/
}