Search in sources :

Example 16 with CommandApdu

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

the class TestApduEncrypter method testPartialEncryptionAes.

/**
 * Prueba de cifrado AES del cuerpo de una APDU.
 * @throws Exception En cualquier error.
 */
@Test
public void testPartialEncryptionAes() throws Exception {
    this.paddingLength = 16;
    final CommandApdu apdu = new CommandApdu((byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, new byte[] { (byte) 0x4d, (byte) 0x61, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x2e, (byte) 0x46, (byte) 0x69, (byte) 0x6c, (byte) 0x65 }, null);
    Assert.assertEquals(// $NON-NLS-1$
    "00a404000b4d61737465722e46696c65", HexUtils.hexify(apdu.getBytes(), false).toLowerCase());
    final byte[] paddedData = addPadding7816(apdu.getData(), this.paddingLength);
    Assert.assertEquals(// $NON-NLS-1$
    "4d61737465722e46696c658000000000", HexUtils.hexify(paddedData, false).toLowerCase());
    final byte[] encryptedApdu = encryptData(paddedData, KENC2, SSC2, new JseCryptoHelper());
    Assert.assertEquals(// $NON-NLS-1$
    "f5124ee2f53962e86e66a6d234827f0f", HexUtils.hexify(encryptedApdu, false).toLowerCase());
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu) JseCryptoHelper(es.gob.jmulticard.JseCryptoHelper) Test(org.junit.Test)

Example 17 with CommandApdu

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

the class TestApduEncrypter method testEncryptionDes.

/**
 * Prueba de cifrado 3DES de APDU.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
public void testEncryptionDes() throws Exception {
    final ApduEncrypter apduEncrypterDes = new ApduEncrypterDes();
    final CipheredApdu a = apduEncrypterDes.protectAPDU(new CommandApdu((byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, new byte[] { (byte) 0x4d, (byte) 0x61, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x2e, (byte) 0x46, (byte) 0x69, (byte) 0x6c, (byte) 0x65 }, null), KENC, KMAC, SSC_SIMPLE, new JseCryptoHelper());
    Assert.assertEquals(// $NON-NLS-1$
    "0ca40400198711013e9ac315a8e855dd3722f291078ac2bd8e04b6f56963", HexUtils.hexify(a.getBytes(), false).toLowerCase());
}
Also used : ApduEncrypterDes(es.gob.jmulticard.apdu.connection.ApduEncrypterDes) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ApduEncrypter(es.gob.jmulticard.apdu.connection.ApduEncrypter) CipheredApdu(es.gob.jmulticard.apdu.connection.CipheredApdu) JseCryptoHelper(es.gob.jmulticard.JseCryptoHelper) Test(org.junit.Test)

Example 18 with CommandApdu

use of es.gob.jmulticard.apdu.CommandApdu 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 CommandApdu

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

the class Dnie method signOperation.

/**
 * Realiza la operació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 CommandApdu

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

the class Ceres method getPinRetriesLeft.

private int getPinRetriesLeft() throws PinException {
    final CommandApdu verifyCommandApdu = new RetriesLeftApduCommand();
    final ResponseApdu verifyResponse;
    try {
        verifyResponse = getConnection().transmit(verifyCommandApdu);
    } catch (final ApduConnectionException e) {
        throw new PinException(// $NON-NLS-1$
        "Error obteniendo el PIN del CallbackHandler: " + e, // $NON-NLS-1$
        e);
    }
    return verifyResponse.getStatusWord().getLsb() - (byte) 0xC0;
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) BadPinException(es.gob.jmulticard.card.BadPinException) PinException(es.gob.jmulticard.card.PinException) RetriesLeftApduCommand(es.gob.jmulticard.apdu.dnie.RetriesLeftApduCommand) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Aggregations

CommandApdu (es.gob.jmulticard.apdu.CommandApdu)29 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)20 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)15 BadPinException (es.gob.jmulticard.card.BadPinException)8 PinException (es.gob.jmulticard.card.PinException)8 IOException (java.io.IOException)8 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)7 CertificateException (java.security.cert.CertificateException)6 Test (org.junit.Test)6 StatusWord (es.gob.jmulticard.apdu.StatusWord)5 JseCryptoHelper (es.gob.jmulticard.JseCryptoHelper)4 SecureChannelException (es.gob.jmulticard.apdu.connection.cwa14890.SecureChannelException)4 TlvException (es.gob.jmulticard.asn1.TlvException)4 AuthenticationModeLockedException (es.gob.jmulticard.card.AuthenticationModeLockedException)4 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)4 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)3 Cwa14890Connection (es.gob.jmulticard.apdu.connection.cwa14890.Cwa14890Connection)3 MseSetComputationApduCommand (es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand)3 Asn1Exception (es.gob.jmulticard.asn1.Asn1Exception)3 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)3