Skip to content

Commit

Permalink
CCID: Support PC To Reader Transfer Block data (flipperdevices#3126)
Browse files Browse the repository at this point in the history
* CCID: Support PC To Reader Transfer Block data
* Format sources

Co-authored-by: あく <[email protected]>
  • Loading branch information
kidbomb and skotopes authored Oct 9, 2023
1 parent 65a56cd commit 4308a5e
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 133 deletions.
18 changes: 14 additions & 4 deletions applications/debug/ccid_test/ccid_test_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,25 @@ void icc_power_on_callback(uint8_t* atrBuffer, uint32_t* atrlen, void* context)
iso7816_answer_to_reset(atrBuffer, atrlen);
}

void xfr_datablock_callback(uint8_t* dataBlock, uint32_t* dataBlockLen, void* context) {
//dataBlock points to the buffer
//dataBlockLen tells reader how nany bytes should be read
void xfr_datablock_callback(
const uint8_t* dataBlock,
uint32_t dataBlockLen,
uint8_t* responseDataBlock,
uint32_t* responseDataBlockLen,
void* context) {
UNUSED(context);

struct ISO7816_Command_APDU commandAPDU;
iso7816_read_command_apdu(&commandAPDU, dataBlock, dataBlockLen);

struct ISO7816_Response_APDU responseAPDU;
//class not supported
responseAPDU.SW1 = 0x6E;
responseAPDU.SW2 = 0x00;

iso7816_write_response_apdu(&responseAPDU, dataBlock, dataBlockLen);
iso7816_write_response_apdu(&responseAPDU, responseDataBlock, responseDataBlockLen);
}

static const CcidCallbacks ccid_cb = {
Expand All @@ -66,7 +76,7 @@ static void ccid_test_app_render_callback(Canvas* canvas, void* ctx) {
canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
}

static void ccid_test_app__input_callback(InputEvent* input_event, void* ctx) {
static void ccid_test_app_input_callback(InputEvent* input_event, void* ctx) {
FuriMessageQueue* event_queue = ctx;

CcidTestAppEvent event;
Expand Down Expand Up @@ -94,7 +104,7 @@ CcidTestApp* ccid_test_app_alloc() {
//message queue
app->event_queue = furi_message_queue_alloc(8, sizeof(CcidTestAppEvent));
furi_check(app->event_queue);
view_port_input_callback_set(app->view_port, ccid_test_app__input_callback, app->event_queue);
view_port_input_callback_set(app->view_port, ccid_test_app_input_callback, app->event_queue);

return app;
}
Expand Down
7 changes: 4 additions & 3 deletions applications/debug/ccid_test/iso7816_t0_apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
#include <furi.h>
#include "iso7816_t0_apdu.h"

void iso7816_answer_to_reset(uint8_t* atrBuffer, uint32_t* atrlen) {
void iso7816_answer_to_reset(uint8_t* dataBuffer, uint32_t* atrlen) {
//minimum valid ATR: https://smartcard-atr.apdu.fr/parse?ATR=3B+00
uint8_t AtrBuffer[2] = {
0x3B, //TS (direct convention)
0x00 // T0 (Y(1): b0000, K: 0 (historical bytes))
};
*atrlen = 2;

memcpy(atrBuffer, AtrBuffer, sizeof(uint8_t) * (*atrlen));
memcpy(dataBuffer, AtrBuffer, sizeof(uint8_t) * (*atrlen));
}

void iso7816_read_command_apdu(
struct ISO7816_Command_APDU* command,
const uint8_t* dataBuffer,
uint32_t dataLen) {
furi_assert(dataLen <= 4);
UNUSED(dataLen);
command->CLA = dataBuffer[0];
command->INS = dataBuffer[1];
command->P1 = dataBuffer[2];
command->P2 = dataBuffer[3];
command->Lc = dataBuffer[4];
}

void iso7816_write_response_apdu(
Expand Down
8 changes: 4 additions & 4 deletions applications/debug/ccid_test/iso7816_t0_apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
struct ISO7816_Command_APDU {
//header
uint8_t CLA;
uint32_t INS;
uint8_t INS;
uint8_t P1;
uint8_t P2;

//body
uint8_t Nc;
uint8_t Ne;
uint8_t Lc;
uint8_t Le;
} __attribute__((packed));

struct ISO7816_Response_APDU {
uint8_t SW1;
uint32_t SW2;
uint8_t SW2;
} __attribute__((packed));

void iso7816_answer_to_reset(uint8_t* atrBuffer, uint32_t* atrlen);
Expand Down
Loading

0 comments on commit 4308a5e

Please sign in to comment.