Search in sources :

Example 76 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project xipki by xipki.

the class DfltConcurrentContentSigner method setCertificateChain.

@Override
public void setCertificateChain(X509Certificate[] certificateChain) {
    if (CollectionUtil.isEmpty(certificateChain)) {
        this.certificateChain = null;
        this.bcCertificateChain = null;
        return;
    }
    this.certificateChain = certificateChain;
    setPublicKey(certificateChain[0].getPublicKey());
    final int n = certificateChain.length;
    this.bcCertificateChain = new X509CertificateHolder[n];
    for (int i = 0; i < n; i++) {
        X509Certificate cert = this.certificateChain[i];
        try {
            this.bcCertificateChain[i] = new X509CertificateHolder(cert.getEncoded());
        } catch (CertificateEncodingException | IOException ex) {
            throw new IllegalArgumentException(String.format("%s occurred while parsing certificate at index %d: %s", ex.getClass().getName(), i, ex.getMessage()), ex);
        }
    }
}
Also used : X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 77 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project android_packages_apps_Settings by DirtyUnicorns.

the class CertInstallerHelper method installCertificate.

/**
 * Extract certificate from the given file, and install it to keystore
 * @param name certificate name
 * @param certFile .p12 file which includes certificates
 * @param password password to extract the .p12 file
 */
public void installCertificate(VpnProfile profile, String certFile, String password) {
    // extract private keys, certificates from the provided file
    extractCertificate(certFile, password);
    // install certificate to the keystore
    int flags = KeyStore.FLAG_ENCRYPTED;
    try {
        if (mUserKey != null) {
            Log.v(TAG, "has private key");
            String key = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert;
            byte[] value = mUserKey.getEncoded();
            if (!mKeyStore.importKey(key, value, mUid, flags)) {
                Log.e(TAG, "Failed to install " + key + " as user " + mUid);
                return;
            }
            Log.v(TAG, "install " + key + " as user " + mUid + " is successful");
        }
        if (mUserCert != null) {
            String certName = Credentials.USER_CERTIFICATE + profile.ipsecUserCert;
            byte[] certData = Credentials.convertToPem(mUserCert);
            if (!mKeyStore.put(certName, certData, mUid, flags)) {
                Log.e(TAG, "Failed to install " + certName + " as user " + mUid);
                return;
            }
            Log.v(TAG, "install " + certName + " as user" + mUid + " is successful.");
        }
        if (!mCaCerts.isEmpty()) {
            String caListName = Credentials.CA_CERTIFICATE + profile.ipsecCaCert;
            X509Certificate[] caCerts = mCaCerts.toArray(new X509Certificate[mCaCerts.size()]);
            byte[] caListData = Credentials.convertToPem(caCerts);
            if (!mKeyStore.put(caListName, caListData, mUid, flags)) {
                Log.e(TAG, "Failed to install " + caListName + " as user " + mUid);
                return;
            }
            Log.v(TAG, " install " + caListName + " as user " + mUid + " is successful");
        }
    } catch (CertificateEncodingException e) {
        Log.e(TAG, "Exception while convert certificates to pem " + e);
        throw new AssertionError(e);
    } catch (IOException e) {
        Log.e(TAG, "IOException while convert to pem: " + e);
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) DEROctetString(com.android.org.bouncycastle.asn1.DEROctetString) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 78 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project Bytecoder by mirkosertic.

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

use of java.security.cert.CertificateEncodingException in project Bytecoder by mirkosertic.

the class X509CertPath method encodePKIPATH.

/**
 * Encode the CertPath using PKIPATH format.
 *
 * @return a byte array containing the binary encoding of the PkiPath object
 * @exception CertificateEncodingException if an exception occurs
 */
private byte[] encodePKIPATH() throws CertificateEncodingException {
    ListIterator<X509Certificate> li = certs.listIterator(certs.size());
    try {
        DerOutputStream bytes = new DerOutputStream();
        // according to PkiPath format
        while (li.hasPrevious()) {
            X509Certificate cert = li.previous();
            // check for duplicate cert
            if (certs.lastIndexOf(cert) != certs.indexOf(cert)) {
                throw new CertificateEncodingException("Duplicate Certificate");
            }
            // get encoded certificates
            byte[] encoded = cert.getEncoded();
            bytes.write(encoded);
        }
        // Wrap the data in a SEQUENCE
        DerOutputStream derout = new DerOutputStream();
        derout.write(DerValue.tag_SequenceOf, bytes);
        return derout.toByteArray();
    } catch (IOException ioe) {
        throw new CertificateEncodingException("IOException encoding " + "PkiPath data: " + ioe, ioe);
    }
}
Also used : DerOutputStream(sun.security.util.DerOutputStream) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Example 80 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project Bytecoder by mirkosertic.

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)

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