Search in sources :

Example 81 with CertificateEncodingException

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

the class X509CertificatePair method getEncoded.

/**
 * Return the DER encoded form of the certificate pair.
 *
 * @return The encoded form of the certificate pair.
 * @throws CerticateEncodingException If an encoding exception occurs.
 */
public byte[] getEncoded() throws CertificateEncodingException {
    try {
        if (encoded == null) {
            DerOutputStream tmp = new DerOutputStream();
            emit(tmp);
            encoded = tmp.toByteArray();
        }
    } catch (IOException ex) {
        throw new CertificateEncodingException(ex.toString());
    }
    return encoded;
}
Also used : DerOutputStream(sun.security.util.DerOutputStream) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException)

Example 82 with CertificateEncodingException

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

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

use of java.security.cert.CertificateEncodingException in project keystore-explorer by kaikramer.

the class DProperties method populateCertificateNode.

private void populateCertificateNode(DefaultMutableTreeNode certificateNode, X509Certificate certificate) throws CryptoException {
    try {
        String version = MessageFormat.format(res.getString("DProperties.properties.Version"), "" + certificate.getVersion());
        certificateNode.add(new DefaultMutableTreeNode(version));
        String subject = MessageFormat.format(res.getString("DProperties.properties.Subject"), X500NameUtils.x500PrincipalToX500Name(certificate.getSubjectX500Principal()));
        certificateNode.add(new DefaultMutableTreeNode(subject));
        String issuer = MessageFormat.format(res.getString("DProperties.properties.Issuer"), X500NameUtils.x500PrincipalToX500Name(certificate.getIssuerX500Principal()));
        certificateNode.add(new DefaultMutableTreeNode(issuer));
        String serialNumber = MessageFormat.format(res.getString("DProperties.properties.SerialNumber"), new BigInteger(certificate.getSerialNumber().toByteArray()).toString(16).toUpperCase());
        certificateNode.add(new DefaultMutableTreeNode(serialNumber));
        Date validFromDate = certificate.getNotBefore();
        String validFrom = MessageFormat.format(res.getString("DProperties.properties.ValidFrom"), StringUtils.formatDate(validFromDate));
        certificateNode.add(new DefaultMutableTreeNode(validFrom));
        Date validUntilDate = certificate.getNotAfter();
        String validUntil = MessageFormat.format(res.getString("DProperties.properties.ValidUntil"), StringUtils.formatDate(validUntilDate));
        certificateNode.add(new DefaultMutableTreeNode(validUntil));
        createPublicKeyNodes(certificateNode, certificate);
        String signatureAlgorithm = MessageFormat.format(res.getString("DProperties.properties.SignatureAlgorithm"), X509CertUtil.getCertificateSignatureAlgorithm(certificate));
        certificateNode.add(new DefaultMutableTreeNode(signatureAlgorithm));
        byte[] cert = certificate.getEncoded();
        String md5 = MessageFormat.format(res.getString("DProperties.properties.Md5Fingerprint"), DigestUtil.getFriendlyMessageDigest(cert, DigestType.MD5));
        certificateNode.add(new DefaultMutableTreeNode(md5));
        String sha1 = MessageFormat.format(res.getString("DProperties.properties.Sha1Fingerprint"), DigestUtil.getFriendlyMessageDigest(cert, DigestType.SHA1));
        certificateNode.add(new DefaultMutableTreeNode(sha1));
    } catch (CertificateEncodingException ex) {
        throw new CryptoException(res.getString("DProperties.NoGetProperties.exception.message"), ex);
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) BigInteger(java.math.BigInteger) CertificateEncodingException(java.security.cert.CertificateEncodingException) CryptoException(org.kse.crypto.CryptoException) Date(java.util.Date)

Example 84 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project keystore-explorer by kaikramer.

the class DViewCertificate method populateDetails.

private void populateDetails() {
    X509Certificate cert = getSelectedCertificate();
    if (cert == null) {
        jdnSubject.setEnabled(false);
        jdnIssuer.setEnabled(false);
        jbViewPublicKeyDetails.setEnabled(false);
        jcfFingerprint.setEnabled(false);
        jbExtensions.setEnabled(false);
        jbPem.setEnabled(false);
        jbAsn1.setEnabled(false);
        jtfVersion.setText("");
        jdnSubject.setDistinguishedName(null);
        jdnIssuer.setDistinguishedName(null);
        jtfSerialNumber.setText("");
        jtfValidFrom.setText("");
        jtfValidUntil.setText("");
        jtfPublicKey.setText("");
        jtfSignatureAlgorithm.setText("");
        jcfFingerprint.setEncodedCertificate(null);
    } else {
        jdnSubject.setEnabled(true);
        jdnIssuer.setEnabled(true);
        jbViewPublicKeyDetails.setEnabled(true);
        jbExtensions.setEnabled(true);
        jbPem.setEnabled(true);
        jbAsn1.setEnabled(true);
        try {
            Date currentDate = new Date();
            Date startDate = cert.getNotBefore();
            Date endDate = cert.getNotAfter();
            boolean notYetValid = currentDate.before(startDate);
            boolean noLongerValid = currentDate.after(endDate);
            jtfVersion.setText(Integer.toString(cert.getVersion()));
            jtfVersion.setCaretPosition(0);
            jdnSubject.setDistinguishedName(X500NameUtils.x500PrincipalToX500Name(cert.getSubjectX500Principal()));
            jdnIssuer.setDistinguishedName(X500NameUtils.x500PrincipalToX500Name(cert.getIssuerX500Principal()));
            jtfSerialNumber.setText("0x" + new BigInteger(1, cert.getSerialNumber().toByteArray()).toString(16).toUpperCase());
            jtfSerialNumber.setCaretPosition(0);
            jtfValidFrom.setText(StringUtils.formatDate(startDate));
            if (notYetValid) {
                jtfValidFrom.setText(MessageFormat.format(res.getString("DViewCertificate.jtfValidFrom.notyetvalid.text"), jtfValidFrom.getText()));
                jtfValidFrom.setForeground(Color.red);
            } else {
                jtfValidFrom.setForeground(jtfVersion.getForeground());
            }
            jtfValidFrom.setCaretPosition(0);
            jtfValidUntil.setText(StringUtils.formatDate(endDate));
            if (noLongerValid) {
                jtfValidUntil.setText(MessageFormat.format(res.getString("DViewCertificate.jtfValidUntil.expired.text"), jtfValidUntil.getText()));
                jtfValidUntil.setForeground(Color.red);
            } else {
                jtfValidUntil.setForeground(jtfVersion.getForeground());
            }
            jtfValidUntil.setCaretPosition(0);
            KeyInfo keyInfo = KeyPairUtil.getKeyInfo(cert.getPublicKey());
            jtfPublicKey.setText(keyInfo.getAlgorithm());
            Integer keySize = keyInfo.getSize();
            if (keySize != null) {
                jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCertificate.jtfPublicKey.text"), jtfPublicKey.getText(), "" + keySize));
            } else {
                jtfPublicKey.setText(MessageFormat.format(res.getString("DViewCertificate.jtfPublicKey.text"), jtfPublicKey.getText(), "?"));
            }
            jtfPublicKey.setCaretPosition(0);
            jtfSignatureAlgorithm.setText(X509CertUtil.getCertificateSignatureAlgorithm(cert));
            jtfSignatureAlgorithm.setCaretPosition(0);
            byte[] encodedCertificate;
            try {
                encodedCertificate = cert.getEncoded();
            } catch (CertificateEncodingException ex) {
                throw new CryptoException(res.getString("DViewCertificate.NoGetEncodedCert.exception.message"), ex);
            }
            jcfFingerprint.setEncodedCertificate(encodedCertificate);
            jcfFingerprint.setFingerprintAlg(ApplicationSettings.getInstance().getCertificateFingerprintType());
            Set<?> critExts = cert.getCriticalExtensionOIDs();
            Set<?> nonCritExts = cert.getNonCriticalExtensionOIDs();
            if (critExts != null && critExts.size() != 0 || nonCritExts != null && nonCritExts.size() != 0) {
                jbExtensions.setEnabled(true);
            } else {
                jbExtensions.setEnabled(false);
            }
        } catch (CryptoException ex) {
            DError dError = new DError(this, ex);
            dError.setLocationRelativeTo(this);
            dError.setVisible(true);
            dispose();
        }
    }
}
Also used : BigInteger(java.math.BigInteger) KeyInfo(org.kse.crypto.KeyInfo) BigInteger(java.math.BigInteger) CertificateEncodingException(java.security.cert.CertificateEncodingException) CryptoException(org.kse.crypto.CryptoException) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) DError(org.kse.gui.error.DError)

Example 85 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project Pix-Art-Messenger by kriztan.

the class CryptoHelper method extractCertificateInformation.

public static Bundle extractCertificateInformation(X509Certificate certificate) {
    Bundle information = new Bundle();
    try {
        JcaX509CertificateHolder holder = new JcaX509CertificateHolder(certificate);
        X500Name subject = holder.getSubject();
        try {
            information.putString("subject_cn", subject.getRDNs(BCStyle.CN)[0].getFirst().getValue().toString());
        } catch (Exception e) {
        // ignored
        }
        try {
            information.putString("subject_o", subject.getRDNs(BCStyle.O)[0].getFirst().getValue().toString());
        } catch (Exception e) {
        // ignored
        }
        X500Name issuer = holder.getIssuer();
        try {
            information.putString("issuer_cn", issuer.getRDNs(BCStyle.CN)[0].getFirst().getValue().toString());
        } catch (Exception e) {
        // ignored
        }
        try {
            information.putString("issuer_o", issuer.getRDNs(BCStyle.O)[0].getFirst().getValue().toString());
        } catch (Exception e) {
        // ignored
        }
        try {
            information.putString("sha1", getFingerprintCert(certificate.getEncoded()));
        } catch (Exception e) {
        }
        return information;
    } catch (CertificateEncodingException e) {
        return information;
    }
}
Also used : Bundle(android.os.Bundle) CertificateEncodingException(java.security.cert.CertificateEncodingException) X500Name(org.bouncycastle.asn1.x500.X500Name) JcaX509CertificateHolder(org.bouncycastle.cert.jcajce.JcaX509CertificateHolder) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) CertificateParsingException(java.security.cert.CertificateParsingException) MalformedURLException(java.net.MalformedURLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

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