Search in sources :

Example 1 with InvalidCardException

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

the class DnieFactory method getDnie.

/**
 * Obtiene la clase de DNIe apropiada (según su ATR).
 * @param conn Conexión con el lector de tarjetas.
 * @param pwc <i>PasswordCallback</i> para la obtenci&oacute;n del PIN.
 * @param cryptoHelper Clase de apoyo para operaciones criptogr&aacute;ficas.
 * @param ch Gestor de <i>callbacks</i> para la obtenci&oacute;n de datos adicionales por parte
 *           del titular del DNIe.
 * @param loadCertsAndKeys Si se indica <code>true</code>, se cargan las referencias a
 *                         las claves privadas y a los certificados, mientras que si se
 *                         indica <code>false</code>, no se cargan, permitiendo la
 *                         instanciaci&oacute;n de un DNIe sin capacidades de firma o
 *                         autenticaci&oacute;n con certificados.
 * @return Clase de DNIe apropiada (seg&uacute;n su ATR).
 * @throws InvalidCardException Si se ha detectado al menos una tarjeta, pero no es un DNIe.
 * @throws BurnedDnieCardException Si se ha detectado un DNIe con su memoria vol&aacute;til borrada.
 * @throws ApduConnectionException Si no se puede conectar con el lector de tarjetas.
 */
public static Dnie getDnie(final ApduConnection conn, final PasswordCallback pwc, final CryptoHelper cryptoHelper, final CallbackHandler ch, final boolean loadCertsAndKeys) throws InvalidCardException, BurnedDnieCardException, ApduConnectionException {
    if (conn == null) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "La conexion no puede ser nula");
    }
    byte[] responseAtr = new byte[] {};
    Atr actualAtr;
    InvalidCardException invalidCardException = null;
    CardNotPresentException cardNotPresentException = null;
    final long[] terminals = conn.getTerminals(false);
    if (terminals.length < 1) {
        throw new NoReadersFoundException();
    }
    for (final long terminal : terminals) {
        conn.setTerminal((int) terminal);
        try {
            responseAtr = conn.reset();
        } catch (final CardNotPresentException e) {
            cardNotPresentException = e;
            continue;
        }
        actualAtr = new Atr(responseAtr, ATR_MASK);
        final byte[] actualAtrBytes = actualAtr.getBytes();
        if (ATR_NFC.equals(actualAtr)) {
            try {
                // $NON-NLS-1$
                LOGGER.info("Detectado DNIe 3.0 por NFC");
                return new DnieNFC(conn, pwc, cryptoHelper, ch, loadCertsAndKeys);
            } catch (final PaceException e) {
                throw new ApduConnectionException(// $NON-NLS-1$
                "No se ha podido abrir el canal PACE: " + e);
            }
        } else if (ATR.equals(actualAtr)) {
            if (actualAtrBytes[15] == 0x04) {
                // $NON-NLS-1$
                LOGGER.info("Detectado DNIe 3.0");
                return new Dnie3(conn, pwc, cryptoHelper, ch, loadCertsAndKeys);
            }
            // $NON-NLS-1$
            LOGGER.info("Detectado DNIe 2.0");
            return new Dnie(conn, pwc, cryptoHelper, ch, loadCertsAndKeys);
        } else if (ATR_TIF.equals(actualAtr)) {
            // $NON-NLS-1$
            LOGGER.info("Detectada tarjeta TIF");
            // (no se aplica el parametro 'loadCertsAndKeys')
            return new Tif(conn, pwc, cryptoHelper, ch);
        } else {
            // en 90-00
            if (actualAtrBytes[actualAtrBytes.length - 1] == (byte) 0x81 && actualAtrBytes[actualAtrBytes.length - 2] == (byte) 0x65) {
                throw new BurnedDnieCardException(actualAtr);
            }
            // $NON-NLS-1$
            invalidCardException = new InvalidCardException("DNIe", ATR, responseAtr);
            continue;
        }
    }
    if (invalidCardException != null) {
        throw invalidCardException;
    }
    if (cardNotPresentException != null) {
        throw cardNotPresentException;
    }
    // $NON-NLS-1$
    throw new ApduConnectionException("No se ha podido conectar con ningun lector de tarjetas");
}
Also used : InvalidCardException(es.gob.jmulticard.card.InvalidCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) PaceException(es.gob.jmulticard.card.pace.PaceException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) Atr(es.gob.jmulticard.card.Atr) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException)

Example 2 with InvalidCardException

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

the class Ceres method checkAtr.

private static void checkAtr(final byte[] atrBytes) throws InvalidCardException {
    Atr tmpAtr = new Atr(atrBytes, ATR_MASK_TC);
    if (ATR_TC.equals(tmpAtr)) {
        if (atrBytes[15] >= (byte) 0x04 && atrBytes[16] >= (byte) 0x30) {
            throw new InvalidCardException(// $NON-NLS-1$
            "Encontrada CERES en version " + HexUtils.hexify(new byte[] { atrBytes[15] }, false) + "." + // $NON-NLS-1$
            HexUtils.hexify(new byte[] { atrBytes[16] }, false) + // $NON-NLS-1$
            ", pero las versiones iguales o superiores a la 04.30 no estan soportadas por este controlador");
        }
        return;
    }
    tmpAtr = new Atr(atrBytes, ATR_MASK_ST);
    if (ATR_ST.equals(tmpAtr)) {
        return;
    }
    tmpAtr = new Atr(atrBytes, ATR_MASK_SLE_FN19);
    if (ATR_SLE_FN19.equals(tmpAtr)) {
        return;
    }
    tmpAtr = new Atr(atrBytes, ATR_MASK_SLE_FN20);
    if (ATR_SLE_FN20.equals(tmpAtr)) {
        return;
    }
    // $NON-NLS-1$
    throw new InvalidCardException("CERES", ATR_TC, atrBytes);
}
Also used : Atr(es.gob.jmulticard.card.Atr) InvalidCardException(es.gob.jmulticard.card.InvalidCardException)

Example 3 with InvalidCardException

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

the class CardOS method connect.

/**
 * Conecta con el lector del sistema que tenga una CardOS insertada.
 * @param conn Conexi&oacute;n hacia la tarjeta.
 * @throws IOException Cuando hay errores de entrada / salida.
 */
private void connect(final ApduConnection conn) throws IOException {
    if (conn == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("La conexion no puede ser nula");
    }
    // Siemens CardOS son T=1
    conn.setProtocol(ApduConnectionProtocol.T1);
    final long[] terminals = conn.getTerminals(false);
    if (terminals.length < 1) {
        throw new NoReadersFoundException();
    }
    byte[] responseAtr;
    Atr actualAtr;
    InvalidCardException invalidCardException = null;
    CardNotPresentException cardNotPresentException = null;
    ApduConnectionException apduConnectionException = null;
    for (final long terminal : terminals) {
        conn.setTerminal((int) terminal);
        try {
            responseAtr = conn.reset();
        } catch (final CardNotPresentException e) {
            cardNotPresentException = e;
            continue;
        } catch (final ApduConnectionException e) {
            apduConnectionException = e;
            continue;
        }
        actualAtr = new Atr(responseAtr, ATR_MASK);
        if (!ATR.equals(actualAtr)) {
            // La tarjeta encontrada no es una CardOS
            invalidCardException = new InvalidCardException(getCardName(), ATR, responseAtr);
            continue;
        }
        return;
    }
    if (invalidCardException != null) {
        throw invalidCardException;
    }
    if (cardNotPresentException != null) {
        throw cardNotPresentException;
    }
    if (apduConnectionException != null) {
        throw apduConnectionException;
    }
    // $NON-NLS-1$
    throw new ApduConnectionException("No se ha podido conectar con ningun lector de tarjetas");
}
Also used : CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) Atr(es.gob.jmulticard.card.Atr) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 4 with InvalidCardException

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

the class TuiR5 method connect.

/**
 * Conecta con el lector del sistema que tenga una TUI insertada.
 * @param conn Conexi&oacute;n hacia la TUI
 * @throws IOException Cuando hay errores de entrada / salida.
 */
private void connect(final ApduConnection conn) throws IOException {
    if (conn == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("La conexion no puede ser nula");
    }
    final long[] terminals = conn.getTerminals(false);
    if (terminals.length < 1) {
        throw new NoReadersFoundException();
    }
    byte[] responseAtr;
    Atr actualAtr;
    InvalidCardException invalidCardException = null;
    CardNotPresentException cardNotPresentException = null;
    for (final long terminal : terminals) {
        conn.setTerminal((int) terminal);
        try {
            responseAtr = conn.reset();
        } catch (final CardNotPresentException e) {
            cardNotPresentException = e;
            continue;
        }
        actualAtr = new Atr(responseAtr, ATR_MASK);
        if (!ATR.equals(actualAtr)) {
            // La tarjeta encontrada no es una TUI
            invalidCardException = new InvalidCardException(getCardName(), ATR, responseAtr);
            continue;
        }
        return;
    }
    if (invalidCardException != null) {
        throw invalidCardException;
    }
    if (cardNotPresentException != null) {
        throw cardNotPresentException;
    }
    // $NON-NLS-1$
    throw new ApduConnectionException("No se ha podido conectar con ningun lector de tarjetas");
}
Also used : CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) Atr(es.gob.jmulticard.card.Atr) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Aggregations

Atr (es.gob.jmulticard.card.Atr)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)3 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)3 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)3 PaceException (es.gob.jmulticard.card.pace.PaceException)1