Search in sources :

Example 96 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project service-proxy by membrane.

the class TrustManagerWrapper method adjustChain.

private void adjustChain(X509Certificate[] chain) {
    for (int i = 0; i < chain.length; i++) {
        final X509Certificate x509 = chain[i];
        chain[i] = new X509Certificate() {

            public boolean hasUnsupportedCriticalExtension() {
                return x509.hasUnsupportedCriticalExtension();
            }

            public Set<String> getCriticalExtensionOIDs() {
                return x509.getCriticalExtensionOIDs();
            }

            @Override
            public boolean equals(Object other) {
                return x509.equals(other);
            }

            @Override
            public int hashCode() {
                return x509.hashCode();
            }

            public Set<String> getNonCriticalExtensionOIDs() {
                return x509.getNonCriticalExtensionOIDs();
            }

            @Override
            public byte[] getEncoded() throws CertificateEncodingException {
                return x509.getEncoded();
            }

            @Override
            public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
                x509.verify(key);
            }

            public byte[] getExtensionValue(String oid) {
                return x509.getExtensionValue(oid);
            }

            @Override
            public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
                x509.verify(key, sigProvider);
            }

            @Override
            public int getVersion() {
                return x509.getVersion();
            }

            @Override
            public BigInteger getSerialNumber() {
                return x509.getSerialNumber();
            }

            @Override
            public String toString() {
                return x509.toString();
            }

            @Override
            public PublicKey getPublicKey() {
                return x509.getPublicKey();
            }

            @Override
            public Principal getIssuerDN() {
                return x509.getIssuerDN();
            }

            @Override
            public X500Principal getIssuerX500Principal() {
                return x509.getIssuerX500Principal();
            }

            @Override
            public Principal getSubjectDN() {
                return x509.getSubjectDN();
            }

            @Override
            public X500Principal getSubjectX500Principal() {
                return x509.getSubjectX500Principal();
            }

            @Override
            public Date getNotBefore() {
                return x509.getNotBefore();
            }

            @Override
            public Date getNotAfter() {
                return x509.getNotAfter();
            }

            @Override
            public byte[] getTBSCertificate() throws CertificateEncodingException {
                return x509.getTBSCertificate();
            }

            @Override
            public byte[] getSignature() {
                return x509.getSignature();
            }

            @Override
            public String getSigAlgName() {
                return x509.getSigAlgName();
            }

            @Override
            public String getSigAlgOID() {
                return x509.getSigAlgOID();
            }

            @Override
            public byte[] getSigAlgParams() {
                return x509.getSigAlgParams();
            }

            @Override
            public boolean[] getIssuerUniqueID() {
                return x509.getIssuerUniqueID();
            }

            @Override
            public boolean[] getSubjectUniqueID() {
                return x509.getSubjectUniqueID();
            }

            @Override
            public boolean[] getKeyUsage() {
                return x509.getKeyUsage();
            }

            @Override
            public List<String> getExtendedKeyUsage() throws CertificateParsingException {
                return x509.getExtendedKeyUsage();
            }

            @Override
            public int getBasicConstraints() {
                return x509.getBasicConstraints();
            }

            @Override
            public Collection<List<?>> getSubjectAlternativeNames() throws CertificateParsingException {
                return x509.getSubjectAlternativeNames();
            }

            @Override
            public Collection<List<?>> getIssuerAlternativeNames() throws CertificateParsingException {
                return x509.getIssuerAlternativeNames();
            }

            @Override
            public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
                if (ignoreTimestampCheckFailure)
                    return;
                x509.checkValidity(date);
            }

            @Override
            public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
                if (ignoreTimestampCheckFailure)
                    return;
                x509.checkValidity();
            }
        };
    }
}
Also used : CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) Set(java.util.Set) CertificateParsingException(java.security.cert.CertificateParsingException) CertificateExpiredException(java.security.cert.CertificateExpiredException) PublicKey(java.security.PublicKey) CertificateEncodingException(java.security.cert.CertificateEncodingException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) BigInteger(java.math.BigInteger) X500Principal(javax.security.auth.x500.X500Principal) Collection(java.util.Collection) List(java.util.List) NoSuchProviderException(java.security.NoSuchProviderException) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Example 97 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project signer by demoiselle.

the class SigningCertificate method getValue.

@Override
public Attribute getValue() {
    try {
        X509Certificate cert = (X509Certificate) certificates[0];
        Digest digest = DigestFactory.getInstance().factoryDefault();
        digest.setAlgorithm(DigestAlgorithmEnum.SHA_1);
        byte[] hash = digest.digest(cert.getEncoded());
        X500Name dirName = new X500Name(cert.getSubjectDN().getName());
        GeneralName name = new GeneralName(dirName);
        GeneralNames issuer = new GeneralNames(name);
        ASN1Integer serial = new ASN1Integer(cert.getSerialNumber());
        IssuerSerial issuerSerial = new IssuerSerial(issuer, serial);
        ESSCertID essCertId = new ESSCertID(hash, issuerSerial);
        return new Attribute(new ASN1ObjectIdentifier(identifier), new DERSet(new DERSequence(new ASN1Encodable[] { new DERSequence(essCertId), new DERSequence(DERNull.INSTANCE) })));
    } catch (CertificateEncodingException ex) {
        throw new SignerException(ex.getMessage());
    }
}
Also used : IssuerSerial(org.bouncycastle.asn1.x509.IssuerSerial) Digest(org.demoiselle.signer.cryptography.Digest) SignedAttribute(org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.SignedAttribute) Attribute(org.bouncycastle.asn1.cms.Attribute) CertificateEncodingException(java.security.cert.CertificateEncodingException) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DERSet(org.bouncycastle.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) DERSequence(org.bouncycastle.asn1.DERSequence) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) ESSCertID(org.bouncycastle.asn1.ess.ESSCertID) GeneralName(org.bouncycastle.asn1.x509.GeneralName) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 98 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project signer by demoiselle.

the class CertValues method getValue.

@Override
public Attribute getValue() throws SignerException {
    List<org.bouncycastle.asn1.x509.Certificate> certificateValues = new ArrayList<org.bouncycastle.asn1.x509.Certificate>();
    try {
        int chainSize = certificates.length - 1;
        for (int i = 0; i < chainSize; i++) {
            X509Certificate cert = (X509Certificate) certificates[i];
            byte[] data = cert.getEncoded();
            certificateValues.add(org.bouncycastle.asn1.x509.Certificate.getInstance(data));
        }
        org.bouncycastle.asn1.x509.Certificate[] certValuesArray = new org.bouncycastle.asn1.x509.Certificate[certificateValues.size()];
        return new Attribute(new ASN1ObjectIdentifier(identifier), new DERSet(new DERSequence(certificateValues.toArray(certValuesArray))));
    } catch (CertificateEncodingException e) {
        throw new SignerException(e.getMessage());
    }
}
Also used : UnsignedAttribute(org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.UnsignedAttribute) Attribute(org.bouncycastle.asn1.cms.Attribute) ArrayList(java.util.ArrayList) CertificateEncodingException(java.security.cert.CertificateEncodingException) DERSet(org.bouncycastle.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) DERSequence(org.bouncycastle.asn1.DERSequence) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 99 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project signer by demoiselle.

the class CertificateRefs method getValue.

@Override
public Attribute getValue() throws SignerException {
    try {
        int chainSize = certificates.length - 1;
        OtherCertID[] arrayOtherCertID = new OtherCertID[chainSize];
        for (int i = 1; i <= chainSize; i++) {
            X509Certificate issuerCert = null;
            X509Certificate cert = (X509Certificate) certificates[i];
            if (i < chainSize) {
                issuerCert = (X509Certificate) certificates[i + 1];
            } else {
                // raiz
                issuerCert = (X509Certificate) certificates[i];
            }
            Digest digest = DigestFactory.getInstance().factoryDefault();
            digest.setAlgorithm(DigestAlgorithmEnum.SHA_256);
            byte[] certHash = digest.digest(cert.getEncoded());
            X500Name dirName = new X500Name(issuerCert.getSubjectX500Principal().getName());
            GeneralName name = new GeneralName(dirName);
            GeneralNames issuer = new GeneralNames(name);
            ASN1Integer serialNumber = new ASN1Integer(cert.getSerialNumber());
            IssuerSerial issuerSerial = new IssuerSerial(issuer, serialNumber);
            AlgorithmIdentifier algId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
            OtherCertID otherCertID = new OtherCertID(algId, certHash, issuerSerial);
            arrayOtherCertID[i - 1] = otherCertID;
        }
        return new Attribute(new ASN1ObjectIdentifier(identifier), new DERSet(new ASN1Encodable[] { new DERSequence(arrayOtherCertID) }));
    } catch (CertificateEncodingException e) {
        throw new SignerException(e.getMessage());
    }
}
Also used : IssuerSerial(org.bouncycastle.asn1.x509.IssuerSerial) Digest(org.demoiselle.signer.cryptography.Digest) UnsignedAttribute(org.demoiselle.signer.policy.impl.cades.pkcs7.attribute.UnsignedAttribute) Attribute(org.bouncycastle.asn1.cms.Attribute) CertificateEncodingException(java.security.cert.CertificateEncodingException) X500Name(org.bouncycastle.asn1.x500.X500Name) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) DERSet(org.bouncycastle.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) OtherCertID(org.bouncycastle.asn1.ess.OtherCertID) DERSequence(org.bouncycastle.asn1.DERSequence) GeneralNames(org.bouncycastle.asn1.x509.GeneralNames) GeneralName(org.bouncycastle.asn1.x509.GeneralName) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) SignerException(org.demoiselle.signer.policy.impl.cades.SignerException) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Example 100 with CertificateEncodingException

use of java.security.cert.CertificateEncodingException in project apollo-android by apollographql.

the class ResponseHeaderRecord method writeCertList.

private void writeCertList(BufferedSink sink, List<Certificate> certificates) throws IOException {
    try {
        sink.writeDecimalLong(certificates.size()).writeByte('\n');
        for (int i = 0, size = certificates.size(); i < size; i++) {
            byte[] bytes = certificates.get(i).getEncoded();
            String line = ByteString.of(bytes).base64();
            sink.writeUtf8(line).writeByte('\n');
        }
    } catch (CertificateEncodingException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : CertificateEncodingException(java.security.cert.CertificateEncodingException) ByteString(okio.ByteString) IOException(java.io.IOException)

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