Search in sources :

Example 1 with Cdf

use of es.gob.jmulticard.asn1.der.pkcs15.Cdf 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 2 with Cdf

use of es.gob.jmulticard.asn1.der.pkcs15.Cdf 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 3 with Cdf

use of es.gob.jmulticard.asn1.der.pkcs15.Cdf 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 4 with Cdf

use of es.gob.jmulticard.asn1.der.pkcs15.Cdf in project jmulticard by ctt-gob-es.

the class TestCdfCreation method testSampleCdf.

/**
 * Prueba de CDF de ejemplo (extra&iacute;do de una tarjeta GyD de ACCV).
 * @throws Exception En cualquier error.
 */
@Test
public static void testSampleCdf() throws Exception {
    final Cdf cdf = new Cdf();
    Assert.assertNotNull(cdf);
    cdf.setDerValue(SAMPLE_CDF);
    // $NON-NLS-1$
    LOGGER.info("\n" + cdf.toString());
}
Also used : Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) Test(org.junit.Test)

Example 5 with Cdf

use of es.gob.jmulticard.asn1.der.pkcs15.Cdf in project jmulticard by ctt-gob-es.

the class TestCdfCreation method testCdf.

/**
 * Prueba la creaci&oacute;n de CDF con volcados en disco.
 * @throws Exception En caso de cualquier tipo de error
 */
@Test
public static void testCdf() throws Exception {
    byte[] cdfdata;
    for (final String element : CDF_TEST_FILES) {
        cdfdata = getDataFromInputStream(ClassLoader.getSystemResourceAsStream(element));
        // $NON-NLS-1$ //$NON-NLS-2$
        LOGGER.info("\n\nCDF completo (" + Integer.toString(cdfdata.length) + "):");
        LOGGER.info(HexUtils.hexify(cdfdata, true));
        final Cdf cdf = new Cdf();
        Assert.assertNotNull(cdf);
        cdf.setDerValue(cdfdata);
        // $NON-NLS-1$
        LOGGER.info("\n" + cdf.toString());
    }
}
Also used : Cdf(es.gob.jmulticard.asn1.der.pkcs15.Cdf) Test(org.junit.Test)

Aggregations

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