Search in sources :

Example 11 with CommandApdu

use of es.gob.jmulticard.apdu.CommandApdu 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ño del fichero seleccionado.
 * @throws ApduConnectionException Si hay problemas en el envío de la APDU.
 * @throws Iso7816FourCardException Si falla la selecció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 12 with CommandApdu

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

the class TuiR5 method selectMasterFile.

@Override
protected void selectMasterFile() throws ApduConnectionException {
    final CommandApdu selectMf = new CommandApdu(CLA, (byte) 0xA4, (byte) 0x08, (byte) 0x0C, new byte[] { (byte) 0x50, (byte) 0x00, (byte) 0x50, (byte) 0x01 }, null);
    sendArbitraryApdu(selectMf);
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu)

Example 13 with CommandApdu

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

the class TestApdus method testMseSetComputation.

/**
 * Prueba de la APDU ISO 7816-4 de gestión de entorno de seguridad para cómputo de
 * firma electrónica.
 */
@SuppressWarnings("static-method")
@Test
public void testMseSetComputation() {
    final CommandApdu mseSetComputation = new MseSetComputationApduCommand((byte) 0x01, new byte[] { (byte) 0x00 }, new byte[] { (byte) 0x02 });
    System.out.println(HexUtils.hexify(mseSetComputation.getBytes(), true));
}
Also used : MseSetComputationApduCommand(es.gob.jmulticard.apdu.iso7816four.MseSetComputationApduCommand) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) Test(org.junit.Test)

Example 14 with CommandApdu

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

the class TestApdus method testSelectDfByName.

/**
 * Prueba de APDU de selección de DF por nombre.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
public void testSelectDfByName() throws Exception {
    final byte[] dfName = new byte[] { (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x63, (byte) 0x50, (byte) 0x4B, (byte) 0x43, (byte) 0x53, (byte) 0x2D, (byte) 0x31, (byte) 0x35 };
    final CommandApdu selectDfByName = new SelectDfByNameApduCommand((byte) 0x00, dfName);
    System.out.println(new String(dfName, StandardCharsets.UTF_8));
    System.out.println(HexUtils.hexify(selectDfByName.getBytes(), true));
}
Also used : SelectDfByNameApduCommand(es.gob.jmulticard.apdu.iso7816four.SelectDfByNameApduCommand) CommandApdu(es.gob.jmulticard.apdu.CommandApdu) Test(org.junit.Test)

Example 15 with CommandApdu

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

the class SecureMessaging method wrap.

/**
 * Transforma un Comando APDU en claro a Comando APDU protegido.
 * @param capdu APDU en claro.
 * @return CommandApdu APDU protegida.
 * @throws SecureMessagingException En cualquier error.
 */
public CommandApdu wrap(final CommandApdu capdu) throws SecureMessagingException {
    byte lc = 0;
    DO97 do97 = null;
    DO87 do87 = null;
    incrementAtIndex(this.ssc);
    // Enmascara el byte de la clase y hace un padding del comando de cabecera
    final byte[] header = new byte[4];
    // Los primeros 4 bytes de la cabecera son los del Comando APDU
    System.arraycopy(capdu.getBytes(), 0, header, 0, 4);
    // Marca la mensajeria segura con el CLA-Byte
    header[0] = (byte) (header[0] | (byte) 0x0C);
    // Construye el DO87 (parametros de comando)
    if (getAPDUStructure(capdu) == 3 || getAPDUStructure(capdu) == 4) {
        do87 = buildDO87(capdu.getData().clone());
        lc += do87.getEncoded().length;
    }
    // Construye el DO97 (payload de respuesta esperado)
    if (getAPDUStructure(capdu) == 2 || getAPDUStructure(capdu) == 4) {
        do97 = buildDO97(capdu.getLe().intValue());
        lc += do97.getEncoded().length;
    }
    // Construye el DO8E (checksum (MAC))
    final DO8E do8E = buildDO8E(header, do87, do97);
    lc += do8E.getEncoded().length;
    // Construye y devuelve la APDU protegida
    final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        bOut.write(header);
        bOut.write(lc);
        if (do87 != null) {
            bOut.write(do87.getEncoded());
        }
        if (do97 != null) {
            bOut.write(do97.getEncoded());
        }
        bOut.write(do8E.getEncoded());
        bOut.write(0);
    } catch (final IOException e) {
        throw new SecureMessagingException(e);
    }
    return new CommandApdu(bOut.toByteArray());
}
Also used : CommandApdu(es.gob.jmulticard.apdu.CommandApdu) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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