Search in sources :

Example 26 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class Dnie3 method getCom.

@Override
public Com getCom() throws IOException {
    try {
        final Com com = new Com();
        com.setDerValue(selectFileByLocationAndRead(FILE_COM_LOCATION));
        return com;
    } catch (final es.gob.jmulticard.card.iso7816four.FileNotFoundException e) {
        // $NON-NLS-1$
        throw (IOException) new FileNotFoundException("COM no encontrado").initCause(e);
    } catch (final Iso7816FourCardException | TlvException | Asn1Exception e) {
        // $NON-NLS-1$
        throw new CryptoCardException("Error leyendo el 'Common Data' (COM)", e);
    }
}
Also used : Com(es.gob.jmulticard.asn1.icao.Com) TlvException(es.gob.jmulticard.asn1.TlvException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) FileNotFoundException(java.io.FileNotFoundException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) CryptoCardException(es.gob.jmulticard.card.CryptoCardException)

Example 27 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class TuiR5 method preloadCertificates.

private void preloadCertificates() throws IOException, Iso7816FourCardException {
    selectMasterFile();
    final Cdf cdf = new Cdf();
    try {
        cdf.setDerValue(selectFileByLocationAndRead(CDF_LOCATION));
    } catch (final Asn1Exception | TlvException e) {
        // $NON-NLS-1$
        throw new IOException("Error en la lectura del CDF", e);
    }
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        try {
            CERTIFICATES_BY_ALIAS.put(cdf.getCertificateAlias(i), CertificateUtils.generateCertificate(selectFileByLocationAndRead(new Location(cdf.getCertificatePath(i)))));
        } catch (final CertificateException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new IOException("Error en la lectura del certificado " + i + " del dispositivo", e);
        }
    }
}
Also used : TlvException(es.gob.jmulticard.asn1.TlvException) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) Location(es.gob.jmulticard.card.Location)

Example 28 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class SmartCafePkcs15Applet method preloadCertificates.

private void preloadCertificates() throws FileNotFoundException, Iso7816FourCardException, IOException, Asn1Exception, TlvException {
    selectMasterFile();
    // Seleccionamos el ODF, no nos devuelve FCI ni nada
    selectFileById(ODF_PATH);
    // Leemos el ODF
    final byte[] odfBytes = readBinaryComplete(162);
    final Odf odf = new Odf();
    odf.setDerValue(odfBytes);
    // Sacamos del ODF la ruta del CDF
    final Path cdfPath = odf.getCdfPath();
    // Leemos el CDF
    final Cdf cdf = new Cdf();
    try {
        selectMasterFile();
        final byte[] cdfBytes = selectFileByIdAndRead(cdfPath.getPathBytes());
        cdf.setDerValue(cdfBytes);
    } catch (final Exception e) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "No se ha podido cargar el CDF de la tarjeta", // $NON-NLS-1$
        e);
    }
    if (cdf.getCertificateCount() < 1) {
        // $NON-NLS-1$
        LOGGER.warning("La tarjeta no contiene ningun certificado");
    }
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        try {
            int fileLength = -1;
            Location certLocation = new Location(cdf.getCertificatePath(i));
            while (certLocation != null) {
                final byte[] id = certLocation.getFile();
                try {
                    fileLength = selectFileById(id);
                } catch (final FileNotFoundException e) {
                    LOGGER.warning(// $NON-NLS-1$//$NON-NLS-2$
                    "El CDF indicaba un certificado en la ruta '" + certLocation + "', pero un elemento de esta no existe, se ignorara: " + e);
                }
                certLocation = certLocation.getChild();
            }
            final byte[] certBytes;
            if (fileLength <= 0) {
                // A veces hay punteros que apuntan a localizaciones vacias
                LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
                "El certificado " + i + " del dispositivo esta vacio");
                continue;
            }
            certBytes = readBinaryComplete(fileLength);
            CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), CertificateUtils.generateCertificate(certBytes));
        } catch (final Exception e) {
            // Puede darse el caso de que el puntero apunte a algo que no es un certificado
            LOGGER.severe(// $NON-NLS-1$ //$NON-NLS-2$
            "Error en la lectura del certificado " + i + " del dispositivo: " + e);
            continue;
        }
    }
}
Also used : Odf(es.gob.jmulticard.asn1.der.pkcs15.Odf) Path(es.gob.jmulticard.asn1.der.pkcs15.Path) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) BadPinException(es.gob.jmulticard.card.BadPinException) IOException(java.io.IOException) PinException(es.gob.jmulticard.card.PinException) TlvException(es.gob.jmulticard.asn1.TlvException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) Location(es.gob.jmulticard.card.Location)

Example 29 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jmulticard by ctt-gob-es.

the class CeresSc method preload.

private void preload() throws ApduConnectionException, Iso7816FourCardException, IOException, CertificateException, Asn1Exception, TlvException {
    // Nos vamos al raiz antes de nada
    selectMasterFile();
    // Leemos el CDF
    final byte[] cdfBytes = selectFileByLocationAndRead(CDF_LOCATION);
    // Cargamos el CDF
    final Pkcs15Cdf cdf = new Cdf();
    cdf.setDerValue(cdfBytes);
    this.certs = new LinkedHashMap<>(cdf.getCertificateCount());
    this.aliasByCertAndKeyId = new LinkedHashMap<>(cdf.getCertificateCount());
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        final Location l = new Location(// $NON-NLS-1$ //$NON-NLS-2$
        cdf.getCertificatePath(i).replace("\\", "").trim());
        final X509Certificate cert = CompressionUtils.getCertificateFromCompressedOrNotData(selectFileByLocationAndRead(l));
        // $NON-NLS-1$
        final String alias = i + " " + cert.getSerialNumber();
        this.aliasByCertAndKeyId.put(HexUtils.hexify(cdf.getCertificateId(i), false), alias);
        this.certs.put(alias, cert);
    }
    // Leemos el PrKDF
    final byte[] prkdfValue = selectFileByLocationAndRead(PRKDF_LOCATION);
    // Establecemos el valor del PrKDF
    PrKdf prkdf;
    try {
        prkdf = new PrKdf();
        prkdf.setDerValue(prkdfValue);
    } catch (final Exception e) {
        LOGGER.warning(// $NON-NLS-1$
        "Detectado posible PrKDF con CommonPrivateKeyAttributes vacio, se prueba con estructura alternativa: " + e);
        prkdf = new PrKdfCeres();
        prkdf.setDerValue(prkdfValue);
    }
    this.keyReferences = new LinkedHashMap<>();
    for (int i = 0; i < prkdf.getKeyCount(); i++) {
        final String alias = this.aliasByCertAndKeyId.get(HexUtils.hexify(prkdf.getKeyId(i), false));
        if (alias != null) {
            this.keyReferences.put(alias, new DniePrivateKeyReference(this, prkdf.getKeyIdentifier(i), new Location(prkdf.getKeyPath(i)), prkdf.getKeyName(i), prkdf.getKeyReference(i), ((RSAPublicKey) this.certs.get(alias).getPublicKey()).getModulus().bitLength()));
        }
    }
    // Sincronizamos claves y certificados
    hideCertsWithoutKey();
}
Also used : PrKdfCeres(es.gob.jmulticard.asn1.custom.fnmt.ceressc.PrKdfCeres) Pkcs15Cdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf) Pkcs15Cdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) PrKdf(es.gob.jmulticard.asn1.der.pkcs15.PrKdf) DniePrivateKeyReference(es.gob.jmulticard.card.dnie.DniePrivateKeyReference) X509Certificate(java.security.cert.X509Certificate) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) DnieCardException(es.gob.jmulticard.card.dnie.DnieCardException) PinException(es.gob.jmulticard.card.PinException) TlvException(es.gob.jmulticard.asn1.TlvException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) Location(es.gob.jmulticard.card.Location)

Example 30 with ASN1Exception

use of com.github.zhenwei.core.asn1.ASN1Exception in project jdk8u_jdk by JetBrains.

the class EncKrbPrivPart method init.

/**
     * Initializes an EncKrbPrivPart object.
     * @param encoding a single DER-encoded value.
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
     * @exception IOException if an I/O error occurs while reading encoded data.
     */
private void init(DerValue encoding) throws Asn1Exception, IOException {
    DerValue der, subDer;
    if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x1C) || (encoding.isApplication() != true) || (encoding.isConstructed() != true)) {
        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    }
    der = encoding.getData().getDerValue();
    if (der.getTag() != DerValue.tag_Sequence) {
        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    }
    subDer = der.getData().getDerValue();
    if ((subDer.getTag() & (byte) 0x1F) == (byte) 0x00) {
        userData = subDer.getData().getOctetString();
    } else {
        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    }
    timestamp = KerberosTime.parse(der.getData(), (byte) 0x01, true);
    if ((der.getData().peekByte() & 0x1F) == 0x02) {
        subDer = der.getData().getDerValue();
        usec = new Integer(subDer.getData().getBigInteger().intValue());
    } else {
        usec = null;
    }
    if ((der.getData().peekByte() & 0x1F) == 0x03) {
        subDer = der.getData().getDerValue();
        seqNumber = new Integer(subDer.getData().getBigInteger().intValue());
    } else {
        seqNumber = null;
    }
    sAddress = HostAddress.parse(der.getData(), (byte) 0x04, false);
    if (der.getData().available() > 0) {
        rAddress = HostAddress.parse(der.getData(), (byte) 0x05, true);
    }
    if (der.getData().available() > 0) {
        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    }
}
Also used : BigInteger(java.math.BigInteger) Asn1Exception(sun.security.krb5.Asn1Exception)

Aggregations

IOException (java.io.IOException)18 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)16 ASN1Exception (com.unboundid.asn1.ASN1Exception)12 TlvException (es.gob.jmulticard.asn1.TlvException)12 Asn1Exception (sun.security.krb5.Asn1Exception)11 ASN1Element (com.unboundid.asn1.ASN1Element)7 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)7 NotNull (com.unboundid.util.NotNull)7 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)7 CertificateException (java.security.cert.CertificateException)7 X509Certificate (java.security.cert.X509Certificate)7 Asn1Exception (org.kse.utilities.asn1.Asn1Exception)7 ASN1Exception (org.wildfly.security.asn1.ASN1Exception)6 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)5 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)5 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)5 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)4 DecoderObject (es.gob.jmulticard.asn1.DecoderObject)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 DerValue (sun.security.util.DerValue)4