Search in sources :

Example 41 with CertificateEncodingException

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

the class CertAndKeyGen method getSelfCertificate.

// Like above, plus a CertificateExtensions argument, which can be null.
public X509Certificate getSelfCertificate(X500Name myname, Date firstDate, long validity, CertificateExtensions ext) throws CertificateException, InvalidKeyException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException {
    X509CertImpl cert;
    Date lastDate;
    try {
        lastDate = new Date();
        lastDate.setTime(firstDate.getTime() + validity * 1000);
        CertificateValidity interval = new CertificateValidity(firstDate, lastDate);
        X509CertInfo info = new X509CertInfo();
        // Add all mandatory attributes
        info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
        info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new java.util.Random().nextInt() & 0x7fffffff));
        AlgorithmId algID = AlgorithmId.get(sigAlg);
        info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
        info.set(X509CertInfo.SUBJECT, myname);
        info.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
        info.set(X509CertInfo.VALIDITY, interval);
        info.set(X509CertInfo.ISSUER, myname);
        if (ext != null)
            info.set(X509CertInfo.EXTENSIONS, ext);
        cert = new X509CertImpl(info);
        cert.sign(privateKey, this.sigAlg);
        return (X509Certificate) cert;
    } catch (IOException e) {
        throw new CertificateEncodingException("getSelfCert: " + e.getMessage());
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate)

Example 42 with CertificateEncodingException

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

the class BlacklistedCertsConverter method getCertificateFingerPrint.

/**
     * Gets the requested finger print of the certificate.
     */
private static String getCertificateFingerPrint(String mdAlg, X509Certificate cert) {
    String fingerPrint = "";
    try {
        byte[] encCertInfo = cert.getEncoded();
        MessageDigest md = MessageDigest.getInstance(mdAlg);
        byte[] digest = md.digest(encCertInfo);
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            byte2hex(digest[i], buf);
        }
        fingerPrint = buf.toString();
    } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
    // ignored
    }
    return fingerPrint;
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 43 with CertificateEncodingException

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

the class SslCertificate method saveState.

/**
     * Saves the certificate state to a bundle
     * @param certificate The SSL certificate to store
     * @return A bundle with the certificate stored in it or null if fails
     */
public static Bundle saveState(SslCertificate certificate) {
    if (certificate == null) {
        return null;
    }
    Bundle bundle = new Bundle();
    bundle.putString(ISSUED_TO, certificate.getIssuedTo().getDName());
    bundle.putString(ISSUED_BY, certificate.getIssuedBy().getDName());
    bundle.putString(VALID_NOT_BEFORE, certificate.getValidNotBefore());
    bundle.putString(VALID_NOT_AFTER, certificate.getValidNotAfter());
    X509Certificate x509Certificate = certificate.mX509Certificate;
    if (x509Certificate != null) {
        try {
            bundle.putByteArray(X509_CERTIFICATE, x509Certificate.getEncoded());
        } catch (CertificateEncodingException ignored) {
        }
    }
    return bundle;
}
Also used : Bundle(android.os.Bundle) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate)

Example 44 with CertificateEncodingException

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

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 45 with CertificateEncodingException

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

the class SslCertificate method getDigest.

/**
     * Convenience for UI presentation, not intended as public API.
     */
private static String getDigest(X509Certificate x509Certificate, String algorithm) {
    if (x509Certificate == null) {
        return "";
    }
    try {
        byte[] bytes = x509Certificate.getEncoded();
        MessageDigest md = MessageDigest.getInstance(algorithm);
        byte[] digest = md.digest(bytes);
        return fingerprint(digest);
    } catch (CertificateEncodingException ignored) {
        return "";
    } catch (NoSuchAlgorithmException ignored) {
        return "";
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

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