Search in sources :

Example 16 with ApduConnectionException

use of es.gob.jmulticard.apdu.connection.ApduConnectionException in project jmulticard by ctt-gob-es.

the class Dnie method sign.

/**
 * {@inheritDoc}
 */
@Override
public byte[] sign(final byte[] data, final String signAlgorithm, final PrivateKeyReference privateKeyReference) throws CryptoCardException, PinException {
    final byte[] ret = signInternal(data, signAlgorithm, privateKeyReference);
    // el PIN y rehacer los canales CWA
    try {
        this.rawConnection.reset();
        setConnection(this.rawConnection);
    } catch (final ApduConnectionException e) {
        throw new CryptoCardException(// $NON-NLS-1$
        "Error en el establecimiento del canal inicial previo al seguro de PIN: " + e, // $NON-NLS-1$
        e);
    }
    return ret;
}
Also used : CryptoCardException(es.gob.jmulticard.card.CryptoCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 17 with ApduConnectionException

use of es.gob.jmulticard.apdu.connection.ApduConnectionException in project jmulticard by ctt-gob-es.

the class SmartcardIoConnection method open.

/**
 * {@inheritDoc}
 */
@Override
public void open() throws ApduConnectionException {
    // Desactivamos las respuestas automaticas para evitar los problemas con el canal seguro
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t0GetResponse", "false");
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t1GetResponse", "false");
    if (isExclusiveUse() && isOpen()) {
        throw new ApduConnectionOpenedInExclusiveModeException();
    }
    final List<CardTerminal> terminales;
    try {
        terminales = TerminalFactory.getDefault().terminals().list();
    } catch (final Exception e) {
        throw new NoReadersFoundException(// $NON-NLS-1$
        "No se han podido listar los lectores del sistema: " + e, // $NON-NLS-1$
        e);
    }
    try {
        if (terminales.size() < 1) {
            throw new NoReadersFoundException();
        }
        if (this.terminalNumber == -1) {
            final long[] cadsWithCard = getTerminals(true);
            if (cadsWithCard.length > 0) {
                this.terminalNumber = (int) cadsWithCard[0];
            } else {
                throw new ApduConnectionException(// $NON-NLS-1$
                "En el sistema no hay ningun terminal con tarjeta insertada");
            }
        }
        if (terminales.size() <= this.terminalNumber) {
            throw new ApduConnectionException(// $NON-NLS-1$
            "No se detecto el lector de tarjetas numero " + Integer.toString(this.terminalNumber));
        }
        this.card = terminales.get(this.terminalNumber).connect(this.protocol.toString());
    } catch (final javax.smartcardio.CardNotPresentException e) {
        throw new CardNotPresentException(e);
    } catch (final CardException e) {
        throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
        "No se ha podido abrir la conexion con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    if (this.exclusive) {
        try {
            this.card.beginExclusive();
        } catch (final CardException e) {
            throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
            "No se ha podido abrir la conexion exclusiva con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
            e);
        }
    }
    this.canal = this.card.getBasicChannel();
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 18 with ApduConnectionException

use of es.gob.jmulticard.apdu.connection.ApduConnectionException in project jmulticard by ctt-gob-es.

the class Cwa14890OneV1Connection method transmit.

/**
 * {@inheritDoc}
 */
@Override
public ResponseApdu transmit(final CommandApdu command) throws ApduConnectionException {
    final CommandApdu protectedApdu;
    try {
        this.ssc = increment(this.ssc);
        protectedApdu = this.apduEncrypter.protectAPDU(command, this.kenc, this.kmac, this.ssc, this.cryptoHelper);
    } catch (final IOException e) {
        throw new SecureChannelException(// $NON-NLS-1$
        "Error en la encriptacion de la APDU para su envio por el canal seguro: " + e, // $NON-NLS-1$
        e);
    }
    final ResponseApdu responseApdu = this.subConnection.transmit(protectedApdu);
    if (INVALID_CRYPTO_CHECKSUM.equals(responseApdu.getStatusWord())) {
        throw new InvalidCryptographicChecksum();
    }
    // Desencriptamos la respuesta
    try {
        this.ssc = increment(this.ssc);
        final ResponseApdu decipherApdu = this.apduEncrypter.decryptResponseApdu(responseApdu, this.kenc, this.ssc, this.kmac, this.cryptoHelper);
        // a enviar el comando indicando la longitud correcta
        if (decipherApdu.getStatusWord().getMsb() == MSB_INCORRECT_LE) {
            command.setLe(decipherApdu.getStatusWord().getLsb());
            return transmit(command);
        } else if (decipherApdu.getStatusWord().getMsb() == MSB_INCORRECT_LE_PACE) {
            command.setLe(command.getLe().intValue() - 1);
            return transmit(command);
        }
        return decipherApdu;
    } catch (final Exception e) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "Error en la desencriptacion de la APDU de respuesta recibida por el canal seguro: " + e, // $NON-NLS-1$
        e);
    }
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) IOException(java.io.IOException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 19 with ApduConnectionException

use of es.gob.jmulticard.apdu.connection.ApduConnectionException in project jmulticard by ctt-gob-es.

the class Dnie method signOperation.

/**
 * Realiza la operaci&oacute;n de firma.
 * @param data Datos que se desean firmar.
 * @param signAlgorithm Algoritmo de firma (por ejemplo, <code>SHA512withRSA</code>, <code>SHA1withRSA</code>, etc.).
 * @param privateKeyReference Referencia a la clave privada para la firma.
 * @return Firma de los datos.
 * @throws CryptoCardException Cuando se produce un error durante la operaci&oacute;n de firma.
 * @throws PinException Si el PIN proporcionado en la <i>PasswordCallback</i>
 *                      es incorrecto y no estaba habilitado el reintento autom&aacute;tico.
 * @throws es.gob.jmulticard.card.AuthenticationModeLockedException Cuando el DNIe est&aacute; bloqueado.
 */
protected byte[] signOperation(final byte[] data, final String signAlgorithm, final PrivateKeyReference privateKeyReference) throws CryptoCardException, PinException {
    openSecureChannelIfNotAlreadyOpened();
    ResponseApdu res;
    try {
        CommandApdu apdu = new MseSetComputationApduCommand((byte) 0x00, ((DniePrivateKeyReference) privateKeyReference).getKeyPath().getLastFilePath(), null);
        res = getConnection().transmit(apdu);
        if (!res.isOk()) {
            throw new DnieCardException(// $NON-NLS-1$
            "Error en el establecimiento de las clave de firma con respuesta: " + res.getStatusWord(), // $NON-NLS-1$
            res.getStatusWord());
        }
        final byte[] digestInfo;
        try {
            digestInfo = DigestInfo.encode(signAlgorithm, data, this.cryptoHelper);
        } catch (final IOException e) {
            // $NON-NLS-1$
            throw new DnieCardException("Error en el calculo de la huella para firmar: " + e, e);
        }
        apdu = new PsoSignHashApduCommand((byte) 0x00, digestInfo);
        res = getConnection().transmit(apdu);
        if (!res.isOk()) {
            throw new DnieCardException(// $NON-NLS-1$
            "Error durante la operacion de firma con respuesta: " + res.getStatusWord(), res.getStatusWord());
        }
    } catch (final LostChannelException e) {
        try {
            getConnection().close();
            if (getConnection() instanceof Cwa14890Connection) {
                setConnection(((Cwa14890Connection) getConnection()).getSubConnection());
            }
        } catch (final Exception ex) {
            // $NON-NLS-1$
            throw new DnieCardException("No se pudo recuperar el canal seguro para firmar: " + ex, ex);
        }
        return signOperation(data, signAlgorithm, privateKeyReference);
    } catch (final ApduConnectionException e) {
        // $NON-NLS-1$
        throw new DnieCardException("Error en la transmision de comandos a la tarjeta: " + e, e);
    }
    return res.getData();
}
Also used : LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) MseSetComputationApduCommand(es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) PsoSignHashApduCommand(es.gob.jmulticard.apdu.iso7816eight.PsoSignHashApduCommand) Cwa14890Connection(es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) IOException(java.io.IOException) 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)

Example 20 with ApduConnectionException

use of es.gob.jmulticard.apdu.connection.ApduConnectionException in project jmulticard by ctt-gob-es.

the class Dnie method verifyPin.

@Override
public void verifyPin(final PasswordCallback psc) throws ApduConnectionException, PinException {
    if (psc == null) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "No se puede verificar el titular con un PasswordCallback nulo");
    }
    VerifyApduCommand verifyCommandApdu = new VerifyApduCommand((byte) 0x00, psc);
    final ResponseApdu verifyResponse = getConnection().transmit(verifyCommandApdu);
    verifyCommandApdu = null;
    // a pedir si es necesario
    if (!verifyResponse.isOk()) {
        if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1) {
            // Cliente @firma, que derivaria en DNI bloqueado
            if (!PIN_AUTO_RETRY || psc.getClass().getName().endsWith("CachePasswordCallback")) {
                // $NON-NLS-1$
                throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0);
            }
            // Si hay reintento automatico volvemos a pedir el PIN con la misma CallBack
            verifyPin(getInternalPasswordCallback());
        } else if (verifyResponse.getStatusWord().getMsb() == (byte) 0x69 && verifyResponse.getStatusWord().getLsb() == (byte) 0x83) {
            throw new AuthenticationModeLockedException();
        } else if (verifyResponse.getStatusWord().getMsb() == (byte) 0x00 && verifyResponse.getStatusWord().getLsb() == (byte) 0x00) {
            // $NON-NLS-1$
            throw new ApduConnectionException("Se ha perdido el canal NFC");
        } else {
            throw new ApduConnectionException(new Iso7816FourCardException(// $NON-NLS-1$ //$NON-NLS-2$
            "Error en la verificacion de PIN (" + verifyResponse.getStatusWord() + ")", verifyResponse.getStatusWord()));
        }
    }
}
Also used : AuthenticationModeLockedException(es.gob.jmulticard.card.AuthenticationModeLockedException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) BadPinException(es.gob.jmulticard.card.BadPinException) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) VerifyApduCommand(es.gob.jmulticard.apdu.dnie.VerifyApduCommand) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Aggregations

ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)38 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)21 IOException (java.io.IOException)16 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)14 PinException (es.gob.jmulticard.card.PinException)14 BadPinException (es.gob.jmulticard.card.BadPinException)13 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)13 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)12 CertificateException (java.security.cert.CertificateException)10 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)9 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)7 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)7 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)7 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)6 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)6 ApduConnection (es.gob.jmulticard.apdu.connection.ApduConnection)5 Cwa14890Connection (es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection)5 TlvException (es.gob.jmulticard.asn1.TlvException)5 SecureChannelException (es.gob.jmulticard.apdu.connection.cwa14890.SecureChannelException)4 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)4