Search in sources :

Example 1 with Location

use of es.gob.jmulticard.card.Location in project jmulticard by ctt-gob-es.

the class Iso7816FourCard method selectFileByLocation.

/**
 * Selecciona un fichero (DF o EF).
 * @param location La ruta absoluta donde se encuentra el fichero a leer
 * @return Tamaño del fichero seleccionado
 * @throws ApduConnectionException Si hay problemas en el envío de la APDU
 * @throws Iso7816FourCardException Si falla la selección de fichero
 */
public int selectFileByLocation(final Location location) throws ApduConnectionException, Iso7816FourCardException {
    int fileLength = 0;
    Location loc = location;
    selectMasterFile();
    while (loc != null) {
        final byte[] id = loc.getFile();
        fileLength = selectFileById(id);
        loc = loc.getChild();
    }
    return fileLength;
}
Also used : Location(es.gob.jmulticard.card.Location)

Example 2 with Location

use of es.gob.jmulticard.card.Location in project jmulticard by ctt-gob-es.

the class Dnie method preloadCertificates.

/**
 * Carga el certificado de la CA intermedia y las localizaciones de los
 * certificados de firma y autenticación.
 * @throws ApduConnectionException Si hay problemas en la precarga.
 */
protected void preloadCertificates() throws ApduConnectionException {
    final Cdf cdf = new Cdf();
    try {
        selectMasterFile();
        final byte[] cdfBytes = selectFileByLocationAndRead(CDF_LOCATION);
        cdf.setDerValue(cdfBytes);
    } catch (final Exception e) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "No se ha podido cargar el CDF de la tarjeta: " + e.toString(), // $NON-NLS-1$
        e);
    }
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        final String currentAlias = cdf.getCertificateAlias(i);
        if (CERT_ALIAS_AUTH.equals(currentAlias)) {
            this.authCertPath = new Location(cdf.getCertificatePath(i));
        } else if (CERT_ALIAS_SIGN.equals(currentAlias)) {
            this.signCertPath = new Location(cdf.getCertificatePath(i));
        } else if (CERT_ALIAS_CYPHER.equals(currentAlias)) {
            this.cyphCertPath = new Location(cdf.getCertificatePath(i));
        } else if (CERT_ALIAS_INTERMEDIATE_CA.equals(currentAlias)) {
            try {
                final byte[] intermediateCaCertEncoded = selectFileByLocationAndRead(new Location(cdf.getCertificatePath(i)));
                this.intermediateCaCert = CompressionUtils.getCertificateFromCompressedOrNotData(intermediateCaCertEncoded);
            } catch (final Exception e) {
                LOGGER.warning(// $NON-NLS-1$
                "No se ha podido cargar el certificado de la autoridad intermedia del CNP: " + e);
                this.intermediateCaCert = null;
            }
        } else {
            this.signAliasCertPath = new Location(cdf.getCertificatePath(i));
        }
    }
}
Also used : Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) AccessControlException(java.security.AccessControlException) SecureChannelException(es.gob.jmulticard.apdu.connection.cwa14890.SecureChannelException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) CancelledOperationException(es.gob.jmulticard.CancelledOperationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) Location(es.gob.jmulticard.card.Location)

Example 3 with Location

use of es.gob.jmulticard.card.Location 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: " + e, // $NON-NLS-1$
        e);
    }
    final CertificateFactory cf;
    try {
        // $NON-NLS-1$
        cf = CertificateFactory.getInstance("X.509");
    } catch (final CertificateException e) {
        // $NON-NLS-1$
        throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, 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) {
                    System.out.println(// $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) {
                certBytes = readBinaryComplete(fileLength);
            } else {
                // 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;
            }
            CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(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 : Path(es.gob.jmulticard.asn1.der.pkcs15.Path) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) 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) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Asn1Exception(es.gob.jmulticard.asn1.Asn1Exception) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TlvException(es.gob.jmulticard.asn1.TlvException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) Odf(es.gob.jmulticard.asn1.der.pkcs15.Odf) ByteArrayInputStream(java.io.ByteArrayInputStream) Location(es.gob.jmulticard.card.Location)

Example 4 with Location

use of es.gob.jmulticard.card.Location 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 Exception e) {
        // $NON-NLS-1$
        throw new IOException("Error en la lectura del CDF: " + e, e);
    }
    final CertificateFactory cf;
    try {
        // $NON-NLS-1$
        cf = CertificateFactory.getInstance("X.509");
    } catch (final CertificateException e) {
        // $NON-NLS-1$
        throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, e);
    }
    for (int i = 0; i < cdf.getCertificateCount(); i++) {
        try {
            certificatesByAlias.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(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, e);
        }
    }
}
Also used : Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) BadPinException(es.gob.jmulticard.card.BadPinException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) Location(es.gob.jmulticard.card.Location)

Example 5 with Location

use of es.gob.jmulticard.card.Location in project jmulticard by ctt-gob-es.

the class Ceres 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
    Pkcs15Cdf cdf = new CeresCdf();
    try {
        cdf.setDerValue(cdfBytes);
    } catch (final Exception e) {
        // Si ha fallado la inicializacion del CDF tipo CERES probamos con el CDF generico PKCS#15,
        // presente en las nuevas tarjetas FNMT-CERES
        cdf = new Cdf();
        cdf.setDerValue(cdfBytes);
    }
    // Leemos los certificados segun las rutas del CDF
    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());
        X509Certificate cert;
        try {
            cert = CompressionUtils.getCertificateFromCompressedOrNotData(selectFileByLocationAndRead(l));
        } catch (final IOException e) {
            // $NON-NLS-1$
            LOGGER.warning("No se ha encontrado un certificado referenciado, se pasa al siguiente: " + e);
            continue;
        }
        // $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
    Pkcs15PrKdf prkdf = new CeresPrKdf();
    try {
        prkdf.setDerValue(prkdfValue);
    } catch (final Exception e) {
        // Si no carga el estructura PrKDF especifica de CERES probamos con la
        // generica PKCS#15, presente en las ultimas versiones de la tarjeta
        prkdf = new PrKdf();
        prkdf.setDerValue(prkdfValue);
    }
    this.keys = 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.keys.put(alias, Byte.valueOf(prkdf.getKeyReference(i)));
        }
    }
    // Sincronizamos claves y certificados
    hideCertsWithoutKey();
}
Also used : CeresPrKdf(es.gob.jmulticard.card.fnmt.ceres.asn1.CeresPrKdf) Pkcs15Cdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf) Pkcs15Cdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf) Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) CeresCdf(es.gob.jmulticard.card.fnmt.ceres.asn1.CeresCdf) IOException(java.io.IOException) 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) CertificateException(java.security.cert.CertificateException) 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) X509Certificate(java.security.cert.X509Certificate) CeresCdf(es.gob.jmulticard.card.fnmt.ceres.asn1.CeresCdf) CeresPrKdf(es.gob.jmulticard.card.fnmt.ceres.asn1.CeresPrKdf) PrKdf(es.gob.jmulticard.asn1.der.pkcs15.PrKdf) Pkcs15PrKdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15PrKdf) Pkcs15PrKdf(es.gob.jmulticard.asn1.der.pkcs15.Pkcs15PrKdf) Location(es.gob.jmulticard.card.Location)

Aggregations

Location (es.gob.jmulticard.card.Location)6 Cdf (es.gob.jmulticard.asn1.der.pkcs15.Cdf)5 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)4 BadPinException (es.gob.jmulticard.card.BadPinException)4 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)4 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)4 IOException (java.io.IOException)4 CertificateException (java.security.cert.CertificateException)4 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)3 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)3 PinException (es.gob.jmulticard.card.PinException)3 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)2 TlvException (es.gob.jmulticard.asn1.TlvException)2 Pkcs15Cdf (es.gob.jmulticard.asn1.der.pkcs15.Pkcs15Cdf)2 PrKdf (es.gob.jmulticard.asn1.der.pkcs15.PrKdf)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CertificateFactory (java.security.cert.CertificateFactory)2 X509Certificate (java.security.cert.X509Certificate)2