Search in sources :

Example 11 with ResponseApdu

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

the class Ceres method loadData.

private void loadData(final int keyBitSize, final byte[] digestInfo) throws CryptoCardException {
    final byte[] paddedData;
    try {
        paddedData = CryptoHelper.addPkcs1PaddingForPrivateKeyOperation(digestInfo, keyBitSize);
    } catch (final Exception e1) {
        throw new CryptoCardException(// $NON-NLS-1$
        "Error realizando el relleno PKCS#1 de los datos a firmar: " + e1, // $NON-NLS-1$
        e1);
    }
    ResponseApdu res;
    // Si la clave es de 1024 la carga se puede hacer en una unica APDU
    if (keyBitSize < 2048) {
        try {
            res = sendArbitraryApdu(new LoadDataApduCommand(paddedData));
        } catch (final Exception e) {
            throw new CryptoCardException(// $NON-NLS-1$
            "Error enviando los datos a firmar a la tarjeta: " + e, // $NON-NLS-1$
            e);
        }
        if (!res.isOk()) {
            throw new CryptoCardException(// $NON-NLS-1$
            "No se han podido enviar los datos a firmar a la tarjeta. Respuesta: " + HexUtils.hexify(res.getBytes(), true));
        }
    } else // Pero si es de 2048 hacen falta dos APDU, envolviendo la APDU de carga de datos
    if (keyBitSize == 2048) {
        final byte[] envelopedLoadDataApdu = new byte[] { (byte) 0x90, (byte) 0x58, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00 };
        // La primera APDU carga 0xFF octetos (254)
        byte[] data = new byte[255];
        System.arraycopy(envelopedLoadDataApdu, 0, data, 0, envelopedLoadDataApdu.length);
        System.arraycopy(paddedData, 0, data, envelopedLoadDataApdu.length, 255 - envelopedLoadDataApdu.length);
        try {
            res = sendArbitraryApdu(new EnvelopeDataApduCommand(data));
        } catch (final Exception e) {
            throw new CryptoCardException(// $NON-NLS-1$
            "Error en el primer envio a la tarjeta de los datos a firmar: " + e, // $NON-NLS-1$
            e);
        }
        if (!res.isOk()) {
            throw new CryptoCardException(// $NON-NLS-1$
            "No se han podido enviar (primera tanda) los datos a firmar a la tarjeta. Respuesta: " + HexUtils.hexify(res.getBytes(), true));
        }
        // La segunda APDU es de 0x08 octetos (8)
        data = new byte[8];
        System.arraycopy(paddedData, 255 - envelopedLoadDataApdu.length, data, 0, 8);
        try {
            res = sendArbitraryApdu(new EnvelopeDataApduCommand(data));
        } catch (final Exception e) {
            throw new CryptoCardException(// $NON-NLS-1$
            "Error en el segundo envio a la tarjeta de los datos a firmar: " + e, // $NON-NLS-1$
            e);
        }
        if (!res.isOk()) {
            throw new CryptoCardException(// $NON-NLS-1$
            "No se han podido enviar (segunda tanda) los datos a firmar a la tarjeta. Respuesta: " + HexUtils.hexify(res.getBytes(), true));
        }
    } else {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Solo se soportan claves de 2048 o menos bits");
    }
}
Also used : EnvelopeDataApduCommand(es.gob.jmulticard.apdu.iso7816eight.EnvelopeDataApduCommand) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) 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) LoadDataApduCommand(es.gob.jmulticard.apdu.ceres.LoadDataApduCommand)

Example 12 with ResponseApdu

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

the class Ceres method verifyPin.

@Override
public void verifyPin(final PasswordCallback pinPc) throws ApduConnectionException, PinException {
    if (pinPc == null) {
        // $NON-NLS-1$
        throw new PinException("No se ha establecido un PasswordCallback");
    }
    final CommandApdu chv = new CeresVerifyApduCommand(CLA, pinPc);
    final ResponseApdu verifyResponse = sendArbitraryApdu(chv);
    if (!verifyResponse.isOk()) {
        if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1 || verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW2) {
            if (AUTO_RETRY) {
                this.passwordCallback = null;
                verifyPin(getInternalPasswordCallback());
                return;
            }
            throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0);
        } else if (new StatusWord((byte) 0x69, (byte) 0x83).equals(verifyResponse.getStatusWord())) {
            throw new AuthenticationModeLockedException();
        }
        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) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) CeresVerifyApduCommand(es.gob.jmulticard.apdu.ceres.CeresVerifyApduCommand) BadPinException(es.gob.jmulticard.card.BadPinException) StatusWord(es.gob.jmulticard.apdu.StatusWord) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 13 with ResponseApdu

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

the class SmartCafePkcs15Applet 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(psc);
    final ResponseApdu verifyResponse = getConnection().transmit(verifyCommandApdu);
    verifyCommandApdu = null;
    if (!verifyResponse.isOk()) {
        if (verifyResponse.getStatusWord().getMsb() == ERROR_PIN_SW1) {
            throw new BadPinException(verifyResponse.getStatusWord().getLsb() - (byte) 0xC0);
        } else if (verifyResponse.getStatusWord().getMsb() == (byte) 0x69 && verifyResponse.getStatusWord().getLsb() == (byte) 0x83) {
            throw new AuthenticationModeLockedException();
        } 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.gide.VerifyApduCommand) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 14 with ResponseApdu

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

the class SmartCafePkcs15Applet method selectFileById.

/**
 * Selecciona un fichero (DF o EF).
 * @param id Identificador del fichero a seleccionar.
 * @return Tama&ntilde;o del fichero seleccionado.
 * @throws ApduConnectionException Si hay problemas en el env&iacute;o de la APDU.
 * @throws Iso7816FourCardException Si falla la selecci&oacute;n de fichero.
 */
@Override
public int selectFileById(final byte[] id) throws ApduConnectionException, Iso7816FourCardException {
    final CommandApdu selectCommand = new SelectFileByIdApduCommand(getCla(), id);
    final ResponseApdu res = getConnection().transmit(selectCommand);
    if (HexUtils.arrayEquals(res.getBytes(), new byte[] { (byte) 0x6a, (byte) 0x82 })) {
        throw new FileNotFoundException(id);
    }
    final SelectFileApduResponse response = new SelectFileApduResponse(res);
    if (response.isOk()) {
        return HexUtils.getUnsignedInt(new byte[] { response.getData()[4], response.getData()[5] }, // Offset
        0);
    }
    final StatusWord sw = response.getStatusWord();
    if (sw.equals(new StatusWord((byte) 0x6A, (byte) 0x82))) {
        throw new FileNotFoundException(id);
    }
    throw new Iso7816FourCardException(sw, selectCommand);
}
Also used : SelectFileByIdApduCommand(es.gob.jmulticard.apdu.iso7816four.SelectFileByIdApduCommand) SelectFileApduResponse(es.gob.jmulticard.apdu.iso7816four.SelectFileApduResponse) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) StatusWord(es.gob.jmulticard.apdu.StatusWord) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu)

Example 15 with ResponseApdu

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

the class TuiR5 method getPrivateKey.

@Override
public PrivateKeyReference getPrivateKey(final String alias) throws CryptoCardException {
    if (alias == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("El alias no puede ser nulo");
    }
    if (!certificatesByAlias.containsKey(alias)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        LOGGER.warning("La tarjeta no contiene el alias '" + alias + "', se devolvera null");
        return null;
    }
    final String[] aliases = getAliases();
    byte index = (byte) 0xff;
    for (int i = 0; i < aliases.length; i++) {
        if (alias.equals(aliases[i])) {
            index = (byte) i;
            break;
        }
    }
    if (index == (byte) 0xff) {
        // $NON-NLS-1$
        throw new IllegalStateException("La tarjeta no contiene el alias: " + alias);
    }
    final MseSetSignatureKeyApduCommand mseSet = new MseSetSignatureKeyApduCommand(CLA, MseSetSignatureKeyApduCommand.CryptographicMechanism.RSASSA_PKCS1v1_5_SHA1, index);
    final ResponseApdu res;
    try {
        res = sendArbitraryApdu(mseSet);
    } catch (final Exception e) {
        // $NON-NLS-1$
        throw new CryptoCardException("Error enviando la APDU de establecimiento de clave privada para firma: " + e, e);
    }
    if (res.isOk()) {
        return new TuiPrivateKeyReference(index);
    }
    throw new CryptoCardException(// $NON-NLS-1$
    "No se ha podido recuperar la referencia a la clave privada: " + HexUtils.hexify(res.getBytes(), true));
}
Also used : MseSetSignatureKeyApduCommand(es.gob.jmulticard.apdu.gemalto.MseSetSignatureKeyApduCommand) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) 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)

Aggregations

ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)32 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)23 CommandApdu (es.gob.jmulticard.apdu.CommandApdu)20 BadPinException (es.gob.jmulticard.card.BadPinException)13 IOException (java.io.IOException)12 PinException (es.gob.jmulticard.card.PinException)10 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)10 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)7 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)7 CertificateException (java.security.cert.CertificateException)7 SecureChannelException (es.gob.jmulticard.apdu.connection.cwa14890.SecureChannelException)6 StatusWord (es.gob.jmulticard.apdu.StatusWord)5 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)4 TlvException (es.gob.jmulticard.asn1.TlvException)4 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)4 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)4 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)3 Cwa14890Connection (es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection)3 PsoSignHashApduCommand (es.gob.jmulticard.apdu.iso7816eight.PsoSignHashApduCommand)3 GetResponseApduCommand (es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand)3