Search in sources :

Example 36 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project coprhd-controller by CoprHD.

the class SSLUtil method getCertificateThumbprint.

/**
 * getCertificateThumbprint
 *
 * @param cert
 */
public String getCertificateThumbprint(Certificate cert) throws InvalidArgument {
    // Compute the SHA-1 hash of the certificate.
    try {
        byte[] encoded;
        try {
            encoded = cert.getEncoded();
        } catch (CertificateEncodingException cee) {
            throw FaultUtil.InvalidArgument("Error reading certificate encoding: " + cee.getMessage(), cee);
        }
        MessageDigest sha1;
        try {
            sha1 = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            throw FaultUtil.InvalidArgument("Could not instantiate SHA-1 hash algorithm", e);
        }
        sha1.update(encoded);
        byte[] hash = sha1.digest();
        if (hash.length != HASH_LENGTH) {
            throw FaultUtil.InvalidArgument("Computed thumbprint is " + hash.length + " bytes long, expected " + HASH_LENGTH);
        }
        StringBuilder thumbprintString = new StringBuilder(hash.length * 3);
        for (int i = 0; i < hash.length; i++) {
            if (i > 0) {
                thumbprintString.append(":");
            }
            String hexByte = Integer.toHexString(0xFF & (int) hash[i]);
            if (hexByte.length() == 1) {
                thumbprintString.append("0");
            }
            thumbprintString.append(hexByte);
        }
        return thumbprintString.toString().toUpperCase();
    } catch (InvalidArgument ia) {
        throw ia;
    } catch (Exception e) {
        throw FaultUtil.InvalidArgument("Exception: " + e);
    }
}
Also used : InvalidArgument(com.vmware.vim.vasa._1_0.InvalidArgument) CertificateEncodingException(java.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 37 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project coprhd-controller by CoprHD.

the class TrustStoreResource method getTrustedCertificates.

/**
 * Get Trusted Certificates
 *
 * @brief List certificates
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.SECURITY_ADMIN, Role.RESTRICTED_SECURITY_ADMIN }, blockProxies = true)
public TrustedCertificates getTrustedCertificates() {
    List<TrustedCertificate> trustedCertsList = new ArrayList<TrustedCertificate>();
    try {
        for (String alias : Collections.list(getKeyStore().aliases())) {
            log.debug("get alias {}", alias);
            if (getKeyStore().isCertificateEntry(alias)) {
                boolean userSupplied = KeystoreEngine.isUserSuppliedCerts(alias);
                Certificate cert = getKeyStore().getCertificate(alias);
                TrustedCertificate tc = new TrustedCertificate(KeyCertificatePairGenerator.getCertificateAsString(cert), userSupplied);
                trustedCertsList.add(tc);
            }
        }
    } catch (KeyStoreException e) {
        log.error(e.getMessage(), e);
        throw new IllegalStateException(e);
    } catch (CertificateEncodingException e) {
        log.error(e.getMessage(), e);
        throw SecurityException.fatals.couldNotParseCertificateToString(e);
    }
    TrustedCertificates certs = new TrustedCertificates();
    certs.setTrustedCertificates(trustedCertsList);
    return certs;
}
Also used : ArrayList(java.util.ArrayList) CertificateEncodingException(java.security.cert.CertificateEncodingException) KeyStoreException(java.security.KeyStoreException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 38 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project android_frameworks_base by crdroidandroid.

the class PackageParser method populateCertificates.

/**
     * Populates the correct packages fields with the given certificates.
     * <p>
     * This is useful when we've already processed the certificates [such as during package
     * installation through an installer session]. We don't re-process the archive and
     * simply populate the correct fields.
     */
public static void populateCertificates(Package pkg, Certificate[][] certificates) throws PackageParserException {
    pkg.mCertificates = null;
    pkg.mSignatures = null;
    pkg.mSigningKeys = null;
    pkg.mCertificates = certificates;
    try {
        pkg.mSignatures = convertToSignatures(certificates);
    } catch (CertificateEncodingException e) {
        // certificates weren't encoded properly; something went wrong
        throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES, "Failed to collect certificates from " + pkg.baseCodePath, e);
    }
    pkg.mSigningKeys = new ArraySet<>(certificates.length);
    for (int i = 0; i < certificates.length; i++) {
        Certificate[] signerCerts = certificates[i];
        Certificate signerCert = signerCerts[0];
        pkg.mSigningKeys.add(signerCert.getPublicKey());
    }
    // add signatures to child packages
    final int childCount = (pkg.childPackages != null) ? pkg.childPackages.size() : 0;
    for (int i = 0; i < childCount; i++) {
        Package childPkg = pkg.childPackages.get(i);
        childPkg.mCertificates = pkg.mCertificates;
        childPkg.mSignatures = pkg.mSignatures;
        childPkg.mSigningKeys = pkg.mSigningKeys;
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) Certificate(java.security.cert.Certificate)

Example 39 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project jdk8u_jdk by JetBrains.

the class X509CertPath method encodePKCS7.

/**
     * Encode the CertPath using PKCS#7 format.
     *
     * @return a byte array containing the binary encoding of the PKCS#7 object
     * @exception CertificateEncodingException if an exception occurs
     */
private byte[] encodePKCS7() throws CertificateEncodingException {
    PKCS7 p7 = new PKCS7(new AlgorithmId[0], new ContentInfo(ContentInfo.DATA_OID, null), certs.toArray(new X509Certificate[certs.size()]), new SignerInfo[0]);
    DerOutputStream derout = new DerOutputStream();
    try {
        p7.encodeSignedData(derout);
    } catch (IOException ioe) {
        throw new CertificateEncodingException(ioe.getMessage());
    }
    return derout.toByteArray();
}
Also used : ContentInfo(sun.security.pkcs.ContentInfo) DerOutputStream(sun.security.util.DerOutputStream) PKCS7(sun.security.pkcs.PKCS7) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 40 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project jdk8u_jdk by JetBrains.

the class SecureKey method getPeerCertificateChain.

/**
     * Return the cert chain presented by the peer in the
     * javax.security.cert format.
     * Note: This method can be used only when using certificate-based
     * cipher suites; using it with non-certificate-based cipher suites,
     * such as Kerberos, will throw an SSLPeerUnverifiedException.
     *
     * @return array of peer X.509 certs, with the peer's own cert
     *  first in the chain, and with the "root" CA last.
     */
@Override
public javax.security.cert.X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
    //
    if ((cipherSuite.keyExchange == K_KRB5) || (cipherSuite.keyExchange == K_KRB5_EXPORT)) {
        throw new SSLPeerUnverifiedException("no certificates expected" + " for Kerberos cipher suites");
    }
    if (peerCerts == null) {
        throw new SSLPeerUnverifiedException("peer not authenticated");
    }
    javax.security.cert.X509Certificate[] certs;
    certs = new javax.security.cert.X509Certificate[peerCerts.length];
    for (int i = 0; i < peerCerts.length; i++) {
        byte[] der = null;
        try {
            der = peerCerts[i].getEncoded();
            certs[i] = javax.security.cert.X509Certificate.getInstance(der);
        } catch (CertificateEncodingException e) {
            throw new SSLPeerUnverifiedException(e.getMessage());
        } catch (javax.security.cert.CertificateException e) {
            throw new SSLPeerUnverifiedException(e.getMessage());
        }
    }
    return certs;
}
Also used : SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

CertificateEncodingException (java.security.cert.CertificateEncodingException)210 X509Certificate (java.security.cert.X509Certificate)94 IOException (java.io.IOException)76 Certificate (java.security.cert.Certificate)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)27 KeyStoreException (java.security.KeyStoreException)19 MessageDigest (java.security.MessageDigest)19 ArrayList (java.util.ArrayList)19 X500Name (org.bouncycastle.asn1.x500.X500Name)16 CertificateException (java.security.cert.CertificateException)14 BigInteger (java.math.BigInteger)11 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)10 Bundle (android.os.Bundle)9 PublicKey (java.security.PublicKey)9 Date (java.util.Date)9 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 File (java.io.File)8 PrivateKey (java.security.PrivateKey)8 DEROctetString (org.bouncycastle.asn1.DEROctetString)8