Search in sources :

Example 56 with ASN1InputStream

use of com.android.org.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.

the class PrincipalUtil method getSubjectX509Principal.

/**
 * return the subject of the given cert as an X509PrincipalObject.
 */
public static X509Principal getSubjectX509Principal(X509Certificate cert) throws CertificateEncodingException {
    try {
        ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getTBSCertificate());
        ASN1InputStream aIn = new ASN1InputStream(bIn);
        TBSCertificateStructure tbsCert = new TBSCertificateStructure((ASN1Sequence) aIn.readObject());
        return new X509Principal(tbsCert.getSubject());
    } catch (IOException e) {
        throw new CertificateEncodingException(e.toString());
    }
}
Also used : ASN1InputStream(org.gudy.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TBSCertificateStructure(org.gudy.bouncycastle.asn1.x509.TBSCertificateStructure) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException)

Example 57 with ASN1InputStream

use of com.android.org.bouncycastle.asn1.ASN1InputStream in project jmulticard by ctt-gob-es.

the class DO8E method fromByteArray.

void fromByteArray(final byte[] encodedData) throws SecureMessagingException {
    try (final ASN1InputStream asn1in = new ASN1InputStream(encodedData)) {
        this.to = (DERTaggedObject) asn1in.readObject();
    } catch (final IOException e) {
        throw new SecureMessagingException(e);
    }
    final DEROctetString ocs = (DEROctetString) this.to.getObject();
    this.data = ocs.getOctets();
}
Also used : ASN1InputStream(org.spongycastle.asn1.ASN1InputStream) IOException(java.io.IOException) DEROctetString(org.spongycastle.asn1.DEROctetString)

Example 58 with ASN1InputStream

use of com.android.org.bouncycastle.asn1.ASN1InputStream in project jmulticard by ctt-gob-es.

the class SecureMessaging method unwrap.

/**
 * Obtiene la APDU de respuesta en claro a partir de una APDU protegida.
 * @param responseApduEncrypted APDU protegida.
 * @return APDU en claro.
 * @throws SecureMessagingException En cualquier error.
 */
public ResponseApdu unwrap(final ResponseApdu responseApduEncrypted) throws SecureMessagingException {
    DO87 do87 = null;
    DO99 do99 = null;
    DO8E do8E = null;
    incrementAtIndex(this.ssc);
    int pointer = 0;
    final byte[] rapduBytes = responseApduEncrypted.getData();
    final byte[] subArray = new byte[rapduBytes.length];
    while (pointer < rapduBytes.length) {
        System.arraycopy(rapduBytes, pointer, subArray, 0, rapduBytes.length - pointer);
        final byte[] encodedBytes;
        try (final ASN1InputStream asn1sp = new ASN1InputStream(subArray)) {
            encodedBytes = asn1sp.readObject().getEncoded();
        } catch (final IOException e) {
            throw new SecureMessagingException(e);
        }
        try (final ASN1InputStream asn1in = new ASN1InputStream(encodedBytes)) {
            switch(encodedBytes[0]) {
                case (byte) 0x87:
                    do87 = new DO87();
                    do87.fromByteArray(asn1in.readObject().getEncoded());
                    break;
                case (byte) 0x99:
                    do99 = new DO99();
                    do99.fromByteArray(asn1in.readObject().getEncoded());
                    break;
                case (byte) 0x8E:
                    do8E = new DO8E();
                    do8E.fromByteArray(asn1in.readObject().getEncoded());
                    break;
                default:
                    break;
            }
        } catch (final IOException e) {
            throw new SecureMessagingException(e);
        }
        pointer += encodedBytes.length;
    }
    if (do99 == null || do8E == null) {
        // DO99 es obligatorio //$NON-NLS-1$
        throw new SecureMessagingException("Error en SecureMessaging: DO99 o DO8E no encontrados");
    }
    // Construct K (SSC||DO87||DO99)
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try {
        if (do87 != null) {
            bout.write(do87.getEncoded());
        }
        bout.write(do99.getEncoded());
    } catch (final IOException e) {
        throw new SecureMessagingException(e);
    }
    this.crypto.init(this.kmac, this.ssc);
    final byte[] cc = this.crypto.getMAC(bout.toByteArray());
    final byte[] do8eData = do8E.getData();
    if (!java.util.Arrays.equals(cc, do8eData)) {
        throw new SecureMessagingException(// $NON-NLS-1$
        "Checksum incorrecto\n CC Calculado: " + HexUtils.hexify(cc, false) + // $NON-NLS-1$
        "\nCC en DO8E: " + HexUtils.hexify(do8eData, false));
    }
    // Desencriptar DO87
    final byte[] unwrappedAPDUBytes;
    if (do87 != null) {
        this.crypto.init(this.kenc, this.ssc);
        final byte[] do87Data = do87.getData();
        final byte[] data;
        try {
            data = this.crypto.decrypt(do87Data);
        } catch (final AmCryptoException e) {
            throw new SecureMessagingException(e);
        }
        // Construir la respuesta APDU desencriptada
        unwrappedAPDUBytes = new byte[data.length + 2];
        System.arraycopy(data, 0, unwrappedAPDUBytes, 0, data.length);
        final byte[] do99Data = do99.getData();
        System.arraycopy(do99Data, 0, unwrappedAPDUBytes, data.length, do99Data.length);
    } else {
        unwrappedAPDUBytes = do99.getData().clone();
    }
    return new ResponseApdu(unwrappedAPDUBytes);
}
Also used : ASN1InputStream(org.spongycastle.asn1.ASN1InputStream) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AmCryptoException(es.gob.jmulticard.de.tsenger.androsmex.crypto.AmCryptoException)

Example 59 with ASN1InputStream

use of com.android.org.bouncycastle.asn1.ASN1InputStream in project signer by demoiselle.

the class CertificateHelper method createSubjectKeyIdentifier.

private static SubjectKeyIdentifier createSubjectKeyIdentifier(Key key) throws IOException {
    ByteArrayInputStream bIn = new ByteArrayInputStream(key.getEncoded());
    ASN1InputStream is = null;
    try {
        is = new ASN1InputStream(bIn);
        ASN1Sequence seq = (ASN1Sequence) is.readObject();
        SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(seq);
        return new BcX509ExtensionUtils().createSubjectKeyIdentifier(info);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) ByteArrayInputStream(java.io.ByteArrayInputStream) BcX509ExtensionUtils(org.bouncycastle.cert.bc.BcX509ExtensionUtils) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)

Example 60 with ASN1InputStream

use of com.android.org.bouncycastle.asn1.ASN1InputStream in project oxAuth by GluuFederation.

the class CRLCertificateVerifier method getExtensionValue.

/**
 * @param certificate
 *            the certificate from which we need the ExtensionValue
 * @param oid
 *            the Object Identifier value for the extension.
 * @return the extension value as an ASN1Primitive object
 * @throws IOException
 */
private static ASN1Primitive getExtensionValue(X509Certificate certificate, String oid) throws IOException {
    byte[] bytes = certificate.getExtensionValue(oid);
    if (bytes == null) {
        return null;
    }
    ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(bytes));
    ASN1OctetString octs = (ASN1OctetString) aIn.readObject();
    aIn = new ASN1InputStream(new ByteArrayInputStream(octs.getOctets()));
    return aIn.readObject();
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)108 IOException (java.io.IOException)90 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)36 ByteArrayInputStream (java.io.ByteArrayInputStream)35 X509Certificate (java.security.cert.X509Certificate)25 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)25 BigInteger (java.math.BigInteger)21 DEROctetString (org.bouncycastle.asn1.DEROctetString)21 ASN1InputStream (com.android.org.bouncycastle.asn1.ASN1InputStream)20 CertificateException (java.security.cert.CertificateException)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 CertificateParsingException (java.security.cert.CertificateParsingException)18 Enumeration (java.util.Enumeration)17 CertificateEncodingException (java.security.cert.CertificateEncodingException)16 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)16 InvalidKeyException (java.security.InvalidKeyException)14 CRLException (java.security.cert.CRLException)14 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)14 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)12 NoSuchProviderException (java.security.NoSuchProviderException)11