Skip to content

Commit

Permalink
Merge pull request #20 from bettse/t_1
Browse files Browse the repository at this point in the history
T=1
  • Loading branch information
bettse authored Jul 27, 2024
2 parents a19b63d + 29f36f5 commit 3361e70
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 31 deletions.
82 changes: 59 additions & 23 deletions ccid.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,44 @@ void seader_ccid_GetSlotStatus(SeaderUartBridge* seader_uart, uint8_t slot) {
void seader_ccid_SetParameters(Seader* seader, uint8_t slot, uint8_t* atr, size_t atr_len) {
SeaderWorker* seader_worker = seader->worker;
SeaderUartBridge* seader_uart = seader_worker->uart;
UNUSED(slot);
UNUSED(atr);
UNUSED(atr_len);
uint8_t T1 = 1;
FURI_LOG_D(TAG, "seader_ccid_SetParameters(%d)", slot);

uint8_t payloadLen = 0;
if(seader_uart->T == 0) {
payloadLen = 5;
} else if(atr[4] == 0xB1 && seader_uart->T == 1) {
payloadLen = 7;
}
memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
seader_uart->tx_buf[0] = SYNC;
seader_uart->tx_buf[1] = CTRL;
seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters;
seader_uart->tx_buf[2 + 1] = 0;
seader_uart->tx_buf[2 + 5] = sam_slot;
seader_uart->tx_buf[2 + 6] = getSequence(sam_slot);
seader_uart->tx_buf[2 + 7] = T1;
seader_uart->tx_buf[2 + 1] = payloadLen;
seader_uart->tx_buf[2 + 5] = slot;
seader_uart->tx_buf[2 + 6] = getSequence(slot);
seader_uart->tx_buf[2 + 7] = seader_uart->T;
seader_uart->tx_buf[2 + 8] = 0;
seader_uart->tx_buf[2 + 9] = 0;

seader_uart->tx_len = seader_add_lrc(seader_uart->tx_buf, 2 + 10);
if(seader_uart->T == 0) {
// I'm leaving this here for completeness, but it was actually causing ICC_MUTE on the first apdu.
seader_uart->tx_buf[2 + 10] = 0x96; //atr[2]; //bmFindexDindex
seader_uart->tx_buf[2 + 11] = 0x00; //bmTCCKST1
seader_uart->tx_buf[2 + 12] = 0x00; //bGuardTimeT0
seader_uart->tx_buf[2 + 13] = 0x0a; //bWaitingIntegerT0
seader_uart->tx_buf[2 + 14] = 0x00; //bClockStop
} else if(seader_uart->T == 1) {
seader_uart->tx_buf[2 + 10] = atr[2]; //bmFindexDindex
seader_uart->tx_buf[2 + 11] = 0x10; //bmTCCKST1
seader_uart->tx_buf[2 + 12] = 0xfe; //bGuardTimeT1
seader_uart->tx_buf[2 + 13] = atr[6]; //bWaitingIntegerT1
seader_uart->tx_buf[2 + 14] = atr[8]; //bClockStop
seader_uart->tx_buf[2 + 15] = atr[5]; //bIFSC
seader_uart->tx_buf[2 + 16] = 0x00; //bNadValue
}

seader_uart->tx_len = seader_add_lrc(seader_uart->tx_buf, 2 + 10 + payloadLen);
furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
}

Expand Down Expand Up @@ -121,14 +142,16 @@ void seader_ccid_XfrBlockToSlot(
seader_uart->tx_buf[2 + 8] = 0;
seader_uart->tx_buf[2 + 9] = 0;

memcpy(seader_uart->tx_buf + 2 + 10, data, len);
seader_uart->tx_len = seader_add_lrc(seader_uart->tx_buf, 2 + 10 + len);
uint8_t header_len = 2 + 10;
memcpy(seader_uart->tx_buf + header_len, data, len);
seader_uart->tx_len = header_len + len;
seader_uart->tx_len = seader_add_lrc(seader_uart->tx_buf, seader_uart->tx_len);

char display[SEADER_UART_RX_BUF_SIZE * 2 + 1] = {0};
for(uint8_t i = 0; i < seader_uart->tx_len; i++) {
snprintf(display + (i * 2), sizeof(display), "%02x", seader_uart->tx_buf[i]);
}
FURI_LOG_D(TAG, "seader_ccid_XfrBlock %d bytes: %s", seader_uart->tx_len, display);
FURI_LOG_D(TAG, "seader_ccid_XfrBlockToSlot(%d) %d: %s", slot, seader_uart->tx_len, display);

furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
}
Expand Down Expand Up @@ -319,10 +342,25 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) {
return message.consumed;
}

if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock) {
if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_Parameters) {
FURI_LOG_D(TAG, "Got Parameters");
if(seader_uart->T == 1) {
seader_t_1_set_IFSD(seader);
} else {
seader_worker_send_version(seader);
if(seader_worker->callback) {
seader_worker->callback(SeaderWorkerEventSamPresent, seader_worker->context);
}
}
} else if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock) {
if(hasSAM) {
if(message.bSlot == sam_slot) {
seader_worker_process_sam_message(seader, message.payload, message.dwLength);
if(seader_uart->T == 0) {
seader_worker_process_sam_message(
seader, message.payload, message.dwLength);
} else if(seader_uart->T == 1) {
seader_recv_t1(seader, &message);
}
} else {
FURI_LOG_D(TAG, "Discarding message on non-sam slot");
}
Expand All @@ -331,20 +369,18 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) {
FURI_LOG_I(TAG, "SAM ATR!");
hasSAM = true;
sam_slot = message.bSlot;
seader_worker_send_version(seader);
if(seader_worker->callback) {
seader_worker->callback(
SeaderWorkerEventSamPresent, seader_worker->context);
if(seader_uart->T == 0) {
seader_ccid_GetParameters(seader_uart);
} else if(seader_uart->T == 1) {
seader_ccid_SetParameters(
seader, sam_slot, message.payload, message.dwLength);
}
} else if(memcmp(SAM_ATR2, message.payload, sizeof(SAM_ATR2)) == 0) {
FURI_LOG_I(TAG, "SAM ATR2!");
hasSAM = true;
sam_slot = message.bSlot;
seader_worker_send_version(seader);
if(seader_worker->callback) {
seader_worker->callback(
SeaderWorkerEventSamPresent, seader_worker->context);
}
// I don't have an ATR2 to test with
seader_ccid_GetParameters(seader_uart);
} else {
FURI_LOG_W(TAG, "Unknown ATR");
if(seader_worker->callback) {
Expand All @@ -353,7 +389,7 @@ size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) {
}
}
} else {
FURI_LOG_W(TAG, "Unhandled CCID message type %d", message.bMessageType);
FURI_LOG_W(TAG, "Unhandled CCID message type %02x", message.bMessageType);
}
}

Expand Down
35 changes: 27 additions & 8 deletions sam_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#define TAG "SAMAPI"

#define APDU_HEADER_LEN 5
#define ASN1_PREFIX 6
#define ASN1_DEBUG true
#define SEADER_ICLASS_SR_SIO_BASE_BLOCK 10
Expand Down Expand Up @@ -137,19 +136,24 @@ void seader_picopass_state_machine(Seader* seader, uint8_t* buffer, size_t len)
bit_buffer_free(rx_buffer);
}

uint8_t APDU_HEADER_LEN = 5;
bool seader_send_apdu(
Seader* seader,
uint8_t CLA,
uint8_t INS,
uint8_t P1,
uint8_t P2,
uint8_t* payload,
uint8_t length) {
uint8_t payloadLen) {
SeaderWorker* seader_worker = seader->worker;
SeaderUartBridge* seader_uart = seader_worker->uart;

if(APDU_HEADER_LEN + length > SEADER_UART_RX_BUF_SIZE) {
FURI_LOG_E(TAG, "Cannot send message, too long: %d", APDU_HEADER_LEN + length);
if(seader_uart->T == 1) {
APDU_HEADER_LEN = 7;
}

if(APDU_HEADER_LEN + payloadLen > SEADER_UART_RX_BUF_SIZE) {
FURI_LOG_E(TAG, "Cannot send message, too long: %d", APDU_HEADER_LEN + payloadLen);
return false;
}

Expand All @@ -158,16 +162,30 @@ bool seader_send_apdu(
apdu[1] = INS;
apdu[2] = P1;
apdu[3] = P2;
apdu[4] = length;
memcpy(apdu + APDU_HEADER_LEN, payload, length);

if(seader_uart->T == 1) {
apdu[4] = 0x00;
apdu[5] = 0x00;
apdu[6] = payloadLen;
} else {
apdu[4] = payloadLen;
}

memcpy(apdu + APDU_HEADER_LEN, payload, payloadLen);
uint8_t length = APDU_HEADER_LEN + payloadLen;

memset(display, 0, sizeof(display));
for(uint8_t i = 0; i < APDU_HEADER_LEN + length; i++) {
for(uint8_t i = 0; i < length; i++) {
snprintf(display + (i * 2), sizeof(display), "%02x", apdu[i]);
}
FURI_LOG_D(TAG, "seader_send_apdu %s", display);

seader_ccid_XfrBlock(seader_uart, apdu, APDU_HEADER_LEN + length);
if(seader_uart->T == 1) {
seader_send_t1(seader_uart, apdu, length);
} else {
seader_ccid_XfrBlock(seader_uart, apdu, length);
}

return true;
}

Expand Down Expand Up @@ -629,6 +647,7 @@ void seader_iso15693_transmit(
uint8_t* buffer,
size_t len) {
SeaderWorker* seader_worker = seader->worker;

BitBuffer* tx_buffer = bit_buffer_alloc(len);
BitBuffer* rx_buffer = bit_buffer_alloc(SEADER_POLLER_MAX_BUFFER_SIZE);

Expand Down
3 changes: 3 additions & 0 deletions seader_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ struct SeaderUartBridge {
uint8_t rx_buf[SEADER_UART_RX_BUF_SIZE];
uint8_t tx_buf[SEADER_UART_RX_BUF_SIZE];
size_t tx_len;

// T=0 or T=1
uint8_t T;
};

typedef struct SeaderUartBridge SeaderUartBridge;
1 change: 1 addition & 0 deletions seader_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "ccid.h"
#include "uart.h"
#include "lrc.h"
#include "t_1.h"
#include "seader_worker.h"
#include "seader_credential.h"

Expand Down
Loading

0 comments on commit 3361e70

Please sign in to comment.