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

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

Introduction

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

Prototype

public DERBitString getSignature() 

Source Link

Usage

From source file:com.bitbreeds.webrtc.dtls.WebrtcDtlsServer.java

License:Open Source License

public void notifyClientCertificate(org.bouncycastle.crypto.tls.Certificate clientCertificate)
        throws IOException {
    Certificate[] chain = clientCertificate.getCertificateList();
    logger.info("DTLS server received client certificate chain of length " + chain.length);
    for (int i = 0; i != chain.length; i++) {
        Certificate entry = chain[i];
        // TODO Create fingerprint based on certificate signature algorithm digest
        logger.info("fingerprint:SHA-256 {} ( {} )", entry.getSignature().toString(), entry.getSubject());
    }//from   www. j av  a  2 s  .c o m
}

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

License:Apache License

/**
 * /*from  w w  w  . ja  v  a2s .  c  o  m*/
 * Prints common certificate informations like signature, signature
 * algorithm, subject and issuer details, etc.
 * 
 * @param cert
 *            The X509CertificateStructure containing the information that
 *            will be printed.
 * 
 */
public static void printCertificateDetails(org.bouncycastle.asn1.x509.Certificate cert) {

    HttpsConnectionUtils.logDebug(
            "BEGIN CERTIFICATE DUMP FOR:[[" + CertificateValidatorUtils.extractCommonName(cert, true) + "]]");

    HttpsConnectionUtils.logDebug("Certificate Signature:[[" + cert.getSignature().toString() + "]]");

    HttpsConnectionUtils.logDebug(
            "Certificate Signature Algorithm OID:[[" + cert.getSignatureAlgorithm().getAlgorithm() + "]]");

    HttpsConnectionUtils.logDebug("Certificate Subject Info:[[" + cert.getSubject().toString() + "]]");

    HttpsConnectionUtils
            .logDebug("Certificate Subject common name (CN):[[" + extractCommonName(cert, false) + "]]");
    HttpsConnectionUtils
            .logDebug("Certificate Subject short common name (CN):[[" + extractCommonName(cert, true) + "]]");

    HttpsConnectionUtils.logDebug("Certificate Issuer Info:[[" + cert.getIssuer() + "]]");

    HttpsConnectionUtils.logDebug("Certificate Start Date:[[" + cert.getStartDate().getTime() + "]]");

    HttpsConnectionUtils.logDebug("Certificate End Date:[[" + cert.getEndDate().getTime() + "]]");

    HttpsConnectionUtils.logDebug("Certificate ASN.1 Dump:[[" + ASN1Dump.dumpAsString(cert, true) + "]]");

    HttpsConnectionUtils.logDebug(
            "END CERTIFICATE DUMP FOR:[[" + CertificateValidatorUtils.extractCommonName(cert, true) + "]]");
}

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

License:Apache License

/**
 * //  w  w  w . j  a va  2s  .co  m
 * Inspected and display various informations from the Certificate passed as
 * parameter. Keys are presented in HEX values and ASN1 structures dumped
 * using ASN1Dump.dumpAsString.
 * 
 * This method is intended for debug purposes only.
 * 
 * 
 * @param cert
 *            The X509CertificateStructure to be inspected.
 * 
 */
public static void dumpCertificateInfo(org.bouncycastle.asn1.x509.Certificate cert) {
    boolean valid = false;
    TBSCertificate tbs = cert.getTBSCertificate();
    RSAEngine engine = new RSAEngine();
    SHA1Digest digest = new SHA1Digest();

    GenericSigner signer = new GenericSigner((engine), digest);
    RSAPublicKey signingKey;
    try {
        signingKey = RSAPublicKey.getInstance(cert.getSubjectPublicKeyInfo().parsePublicKey());

        HttpsConnectionUtils.logDebug("Public Key:[[" + cert.getSubjectPublicKeyInfo().parsePublicKey() + "]]");

        RSAKeyParameters keySpec = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        signer.init(false, keySpec);
        HttpsConnectionUtils.logDebug("TBS DER object:[[" + tbs.getEncoded("DER") + "]]");

        signer.update(tbs.getEncoded(), 0, tbs.getEncoded().length);

        valid = signer.verifySignature(cert.getSignature().getBytes());

        HttpsConnectionUtils.logDebug("signer.verifySignature:[[" + valid + "]]");

        SHA1Digest d2 = new SHA1Digest();
        d2.update(tbs.getEncoded("DER"), 0, tbs.getEncoded("DER").length);
        byte[] hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("tbs.getDEREncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");
        DEROctetString asn1Hash = new DEROctetString(hash);
        HttpsConnectionUtils.logDebug(
                "ASN1 DEROctetString hash:[[" + new String(Hex.encode(asn1Hash.getEncoded("DER"))) + "]]");

        d2 = new SHA1Digest();
        d2.update(cert.getEncoded(), 0, cert.getEncoded().length);
        hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("cert.getEncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");

        byte[] signature = cert.getSignature().getBytes();
        HttpsConnectionUtils
                .logDebug("cert.getSignature().getBytes():[[" + new String(Hex.encode(signature)) + "]]");

        PKCS1Encoding engine2 = new PKCS1Encoding(new RSAEngine());
        engine2.init(false, keySpec);
        byte[] decryptedHash = engine2.processBlock(signature, 0, signature.length);
        HttpsConnectionUtils.logDebug("decryptedHash:[[" + new String(Hex.encode(decryptedHash)) + "]]");

        ASN1Object o = ASN1Primitive.fromByteArray(decryptedHash);
        HttpsConnectionUtils.logDebug(
                "decryptedHash.getDEREncoded():[[" + new String(Hex.encode(o.getEncoded("DER"))) + "]]");

        HttpsConnectionUtils.logDebug(
                "ASN1Dump.dumpAsString(decryptedHash,true):[[" + ASN1Dump.dumpAsString(o, true) + "]]");

        HttpsConnectionUtils.logDebug("engine.getInputBlockSize():[[" + engine2.getInputBlockSize() + "]]");

        HttpsConnectionUtils.logDebug("engine.getOutputBlockSize():[[" + engine2.getOutputBlockSize() + "]]");

        ASN1Sequence asn1SignSeq = (ASN1Sequence) ASN1Sequence.fromByteArray(decryptedHash);
        HttpsConnectionUtils
                .logDebug("Signature ASN1 Sequence:[[" + ASN1Dump.dumpAsString(asn1SignSeq, true) + "]]");

        AlgorithmIdentifier algorithm = AlgorithmIdentifier.getInstance(asn1SignSeq.getObjectAt(0));
        HttpsConnectionUtils.logDebug("AlgorithmIdentifier:[[" + ASN1Dump.dumpAsString(algorithm, true) + "]]");

        DEROctetString signedHash = (DEROctetString) DEROctetString.getInstance(asn1SignSeq.getObjectAt(1));
        HttpsConnectionUtils.logDebug("signedHash:[[" + ASN1Dump.dumpAsString(signedHash, true) + "]]");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

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

License:Apache License

/**
 * Validates the certificate signature (hash).
 * /*w  w  w . j a va 2s.c om*/
 * @param cert
 *            The certificate to be validated.
 * @param issuerCert
 *            the issuer (normally a C.A.) certificate corresponding to the
 *            key used to sign the certificate indicated at the previous
 *            parameter.
 * 
 * @param errors
 *            a list to be filled - if needed - with errors detected during
 *            the validation process. See
 *            {@link CertificateValidationException#setErrors(List)}.
 * 
 * @return True if the certificate signature is valid. False otherwise.
 * @throws IOException
 * @throws InvalidCipherTextException
 * @throws CertException
 * @throws OperatorCreationException
 * 
 */
public static boolean verifySignature(org.bouncycastle.asn1.x509.Certificate cert,
        org.bouncycastle.asn1.x509.Certificate issuerCert, List errors)
        throws OperatorCreationException, CertException, IOException {
    boolean retval = false;
    if (!CertificateValidatorUtils.isAlgIdEqual(cert.getTBSCertificate().getSignature(),
            cert.getSignatureAlgorithm())) {
        throw new CertException("signature invalid - algorithm identifier mismatch");
    }

    ContentVerifierProvider verifierProvider = new BcRSAContentVerifierProviderBuilder(
            new DefaultDigestAlgorithmIdentifierFinder()).build(
                    PublicKeyFactory.createKey(issuerCert.getTBSCertificate().getSubjectPublicKeyInfo()));
    ContentVerifier verifier;
    try {
        verifier = verifierProvider.get((cert.getTBSCertificate().getSignature()));

        OutputStream sOut = verifier.getOutputStream();
        DEROutputStream dOut = new DEROutputStream(sOut);

        dOut.writeObject(cert.getTBSCertificate());

        sOut.close();
    } catch (Exception e) {
        throw new CertException("unable to process signature: " + e.getMessage(), e);
    }

    retval = verifier.verify(cert.getSignature().getBytes());

    if (retval == false) {
        String error = "Invalid certificate signature for [[" + extractCommonName(cert, true)
                + "]] validated against the Signer Certificate [[" + extractCommonName(issuerCert, true)
                + "]].";
        HttpsConnectionUtils.logError(error);
        errors.add(error);
    }
    return retval;
}