Search in sources :

Example 1 with VerifyApduCommand

use of es.gob.jmulticard.apdu.dnie.VerifyApduCommand 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)

Example 2 with VerifyApduCommand

use of es.gob.jmulticard.apdu.dnie.VerifyApduCommand in project jmulticard by ctt-gob-es.

the class TestApduEncrypter method testPinEncryptionDes.

/**
 * Prueba de cifrado 3DES de APDU de verificación de PIN.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
public void testPinEncryptionDes() throws Exception {
    final CommandApdu verifyCommandApdu = new VerifyApduCommand((byte) 0x00, // $NON-NLS-1$
    new CachePasswordCallback("CRYPTOKI".toCharArray()));
    final ApduEncrypter apduEncrypterDes = new ApduEncrypterDes();
    final byte[] res = apduEncrypterDes.protectAPDU(verifyCommandApdu, KENC, KMAC, SSC_PIN, new JseCryptoHelper()).getBytes();
    Assert.assertEquals(// $NON-NLS-1$
    "0c20000019871101ce1ab937c332f3faee43336d4311ef338e046908df4e", HexUtils.hexify(res, false).toLowerCase());
}
Also used : ApduEncrypterDes(es.gob.jmulticard.apdu.connection.ApduEncrypterDes) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ApduEncrypter(es.gob.jmulticard.apdu.connection.ApduEncrypter) JseCryptoHelper(es.gob.jmulticard.JseCryptoHelper) VerifyApduCommand(es.gob.jmulticard.apdu.dnie.VerifyApduCommand) Test(org.junit.Test)

Example 3 with VerifyApduCommand

use of es.gob.jmulticard.apdu.dnie.VerifyApduCommand in project jmulticard by ctt-gob-es.

the class AndroidNfcConnection method transmit.

@Override
public ResponseApdu transmit(final CommandApdu command) throws ApduConnectionException {
    if (this.mIsoDep == null) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "No se puede transmitir sobre una conexion NFC cerrada");
    }
    if (command == null) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "No se puede transmitir una APDU nula");
    }
    if (!this.mIsoDep.isConnected()) {
        try {
            this.mIsoDep.connect();
        } catch (final IOException e) {
            throw new ApduConnectionException(// $NON-NLS-1$
            "Se ha producido un problema al intentar establecer la conexion por NFC: " + e, // $NON-NLS-1$
            e);
        }
    }
    final byte[] commandBytes;
    if (command instanceof VerifyApduCommand) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final byte[] bcomm = command.getBytes();
        final byte[] bdata = command.getData();
        baos.write(bcomm, 0, bcomm.length - 2);
        try {
            baos.write(new byte[] { (byte) bdata.length });
            baos.write(bdata);
        } catch (final IOException e) {
            throw new ApduConnectionException(// $NON-NLS-1$
            "Error preparando la APDU para su envio", e);
        }
        commandBytes = baos.toByteArray();
    } else {
        commandBytes = command.getBytes();
    }
    // Liberamos la conexion para transmitir
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
        NFCWatchdogRefresher.stopHoldingConnection();
    }
    final byte[] bResp;
    try {
        bResp = this.mIsoDep.transceive(commandBytes);
    } catch (final IOException e) {
        // Evitamos que salga el PIN en la traza de excepcion
        throw new ApduConnectionException(// $NON-NLS-1$
        "Error tratando de transmitir la APDU" + (// $NON-NLS-1$
        (command instanceof VerifyApduCommand) ? // $NON-NLS-1$
        " de verificacion de PIN" : (" " + HexUtils.hexify(command.getBytes(), true))) + // $NON-NLS-1$
        " via NFC", e);
    } finally {
        // Retenemos la conexion hasta nuestro siguiente envio
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
            NFCWatchdogRefresher.holdConnection(this.mIsoDep);
        }
    }
    if (bResp.length < 2) {
        throw new ApduConnectionException(// $NON-NLS-1$
        "No se ha recibido respuesta al envio del comando");
    }
    final ResponseApdu response = new ResponseApdu(bResp);
    if (response.getStatusWord().getMsb() == 97) {
        if (response.getData().length > 0) {
            final byte[] data = response.getData();
            final byte[] additionalData = this.transmit(new GetResponseApduCommand((byte) 0, response.getStatusWord().getLsb())).getBytes();
            final byte[] fullResponse = new byte[data.length + additionalData.length];
            System.arraycopy(data, 0, fullResponse, 0, data.length);
            System.arraycopy(additionalData, 0, fullResponse, data.length, additionalData.length);
            return new ResponseApdu(fullResponse);
        }
        return this.transmit(new GetResponseApduCommand((byte) 0, response.getStatusWord().getLsb()));
    }
    if (response.getStatusWord().getMsb() == 108 && command.getCla() == 0) {
        command.setLe(response.getStatusWord().getLsb());
        return this.transmit(command);
    }
    return response;
}
Also used : ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) GetResponseApduCommand(es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) VerifyApduCommand(es.gob.jmulticard.apdu.dnie.VerifyApduCommand)

Aggregations

VerifyApduCommand (es.gob.jmulticard.apdu.dnie.VerifyApduCommand)3 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)2 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)2 JseCryptoHelper (es.gob.jmulticard.JseCryptoHelper)1 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)1 ApduEncrypter (es.gob.jmulticard.apdu.connection.ApduEncrypter)1 ApduEncrypterDes (es.gob.jmulticard.apdu.connection.ApduEncrypterDes)1 GetResponseApduCommand (es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand)1 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)1 BadPinException (es.gob.jmulticard.card.BadPinException)1 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Test (org.junit.Test)1