Example usage for org.bouncycastle.asn1.x509 Certificate getEncoded

List of usage examples for org.bouncycastle.asn1.x509 Certificate getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 Certificate getEncoded.

Prototype

public byte[] getEncoded(String encoding) throws IOException 

Source Link

Document

Return either the default for "BER" or a DER encoding if "DER" is specified.

Usage

From source file:net.wstech2.me.httpsclient.CertificateValidatorUtils.java

License:Apache License

private static Boolean isTrustedCertificate(Certificate cert, String fullCommonName, String friendlyCommonName)
        throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
    Certificate repositoryCertificate = getCertificateFromJarOrRecordStore(fullCommonName, friendlyCommonName);
    if (repositoryCertificate == null) {
        HttpsConnectionUtils.logDebug("A file for [[" + fullCommonName + "/" + friendlyCommonName
                + "]] was not located as a resource in the local repository.");
        return Boolean.FALSE;
    }//from w w w  .  j  a  v  a2  s. c o m
    boolean certificatesMatch = Arrays.constantTimeAreEqual(cert.getEncoded("DER"),
            repositoryCertificate.getEncoded("DER"));
    if (certificatesMatch) {
        HttpsConnectionUtils.logDebug("A file for [[" + fullCommonName + "/" + friendlyCommonName
                + "]] was located as a resource in the local repository and "
                + "the certificate will be considered as TRUSTED.");

    } else {
        HttpsConnectionUtils.logDebug("A file for [[" + fullCommonName + "/" + friendlyCommonName
                + "]] was located as a resource in the local repository, but it DOES NOT"
                + "MATCH the certificate sent by the client. It will NOT be considered as TRUESTED.");
        HttpsConnectionUtils.logDebug("Base 64 for[[" + fullCommonName + "/" + friendlyCommonName
                + "]] sent by the client: \n" + Base64.toBase64String(cert.getEncoded("DER")));
        HttpsConnectionUtils.logDebug(
                "Base 64 for[[" + fullCommonName + "/" + friendlyCommonName + "]] from the local repository:\n"
                        + Base64.toBase64String(repositoryCertificate.getEncoded("DER")));
    }
    return certificatesMatch ? Boolean.TRUE : Boolean.FALSE;
}

From source file:org.jitsi.impl.neomedia.transform.dtls.DtlsControlImpl.java

License:LGPL

/**
 * Computes the fingerprint of a specific certificate using a specific
 * hash function./*from w ww . j  av  a 2 s . c  o  m*/
 *
 * @param certificate the certificate the fingerprint of which is to be
 * computed
 * @param hashFunction the hash function to be used in order to compute the
 * fingerprint of the specified <tt>certificate</tt> 
 * @return the fingerprint of the specified <tt>certificate</tt> computed
 * using the specified <tt>hashFunction</tt>
 */
private static final String computeFingerprint(org.bouncycastle.asn1.x509.Certificate certificate,
        String hashFunction) {
    try {
        AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder()
                .find(hashFunction.toUpperCase());
        Digest digest = BcDefaultDigestProvider.INSTANCE.get(digAlgId);
        byte[] in = certificate.getEncoded(ASN1Encoding.DER);
        byte[] out = new byte[digest.getDigestSize()];

        digest.update(in, 0, in.length);
        digest.doFinal(out, 0);

        return toHex(out);
    } catch (Throwable t) {
        if (t instanceof ThreadDeath) {
            throw (ThreadDeath) t;
        } else {
            logger.error("Failed to generate certificate fingerprint!", t);
            if (t instanceof RuntimeException)
                throw (RuntimeException) t;
            else
                throw new RuntimeException(t);
        }
    }
}