Skip to content

Commit af90253

Browse files
Add functions to lock/unlock the APDU source to a specific media
1 parent 7cf5318 commit af90253

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

include/os_io_seproxyhal.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ typedef struct io_seph_s {
246246
unsigned short apdu_length; // total length to be received
247247
unsigned short io_flags; // flags to be set when calling io_exchange
248248
io_apdu_media_t apdu_media;
249+
io_apdu_media_t apdu_media_lock;
249250

250251
unsigned int ms;
251252

@@ -285,11 +286,29 @@ extern io_seph_app_t G_io_app;
285286
*/
286287
void io_task(void);
287288
/**
288-
* IO task initializez
289+
* IO task initialize
289290
*/
290291
void io_start(void);
291292
#endif // HAVE_IO_TASK
292293

294+
/**
295+
* Lock apdu source to a media
296+
* After this function is called, all subsequent apdus
297+
* that come from any other media will be refused
298+
*/
299+
void io_apdu_media_lock(io_apdu_media_t media);
300+
/**
301+
* Unlock apdu source.
302+
* After this function is called, apdus any media will be processed
303+
*/
304+
void io_apdu_media_unlock(void);
305+
/**
306+
* Check if an Apdu media shall be accepted.
307+
* Return true if source media is not locked or if the media corresponds to the current lock
308+
* Return false if media doesn't correspond to the current lock
309+
*/
310+
bool io_apdu_is_media_accepted(io_apdu_media_t media);
311+
293312
void io_seproxyhal_setup_ticker(unsigned int interval_ms);
294313
void io_seproxyhal_power_off(bool criticalBattery);
295314
void io_seproxyhal_se_reset(void);

lib_blewbxx_impl/src/ledger_ble.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ static void hci_evt_cmd_complete(const uint8_t *buffer, uint16_t length)
517517
if (ledger_ble_data.transfer_mode_enable) {
518518
if ((ledger_protocol_data.rx_apdu_length)
519519
&& (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE)) {
520-
if (G_io_app.apdu_state == APDU_IDLE) {
520+
if (G_io_app.apdu_state == APDU_IDLE
521+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
521522
copy_apdu_to_app(false);
522523
}
523524
else {
@@ -892,7 +893,8 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
892893
LOG_BLE("Transfer failed 0x%04x\n", U2BE(ledger_ble_data.resp, 0));
893894
G_io_app.transfer_mode = 0;
894895
check_transfer_mode(G_io_app.transfer_mode);
895-
if (G_io_app.apdu_state == APDU_IDLE) {
896+
if (G_io_app.apdu_state == APDU_IDLE
897+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
896898
copy_apdu_to_app(true);
897899
}
898900
else {
@@ -914,7 +916,8 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
914916
}
915917
else {
916918
// Nominal case for apdu reception
917-
if (G_io_app.apdu_state == APDU_IDLE) {
919+
if (G_io_app.apdu_state == APDU_IDLE
920+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
918921
copy_apdu_to_app(true);
919922
}
920923
else {
@@ -961,7 +964,7 @@ static void write_permit_request(const uint8_t *buffer, uint16_t length)
961964
data_length,
962965
&buffer[3]);
963966
if (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) {
964-
if (G_io_app.apdu_state == APDU_IDLE) {
967+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
965968
copy_apdu_to_app(true);
966969
}
967970
else {

lib_stusb_impl/usbd_impl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,8 @@ uint8_t USBD_HID_DataOut_impl(USBD_HandleTypeDef *pdev,
10741074

10751075
#ifndef HAVE_USB_HIDKBD
10761076
// avoid troubles when an apdu has not been replied yet
1077-
if (G_io_app.apdu_state == APDU_IDLE) {
1077+
if (G_io_app.apdu_state == APDU_IDLE
1078+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_USB_HID)) {
10781079
// add to the hid transport
10791080
switch (io_usb_hid_receive(io_usb_send_apdu_data,
10801081
buffer,
@@ -1162,7 +1163,8 @@ uint8_t USBD_WEBUSB_DataOut(USBD_HandleTypeDef *pdev,
11621163
USBD_LL_PrepareReceive(pdev, WEBUSB_EPOUT_ADDR, WEBUSB_EPOUT_SIZE);
11631164

11641165
// avoid troubles when an apdu has not been replied yet
1165-
if (G_io_app.apdu_state == APDU_IDLE) {
1166+
if (G_io_app.apdu_state == APDU_IDLE
1167+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_USB_WEBUSB)) {
11661168
// add to the hid transport
11671169
switch (io_usb_hid_receive(io_usb_send_apdu_data_ep0x83,
11681170
buffer,

src/os_io_nfc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void io_nfc_recv_event(void)
6767

6868
// Full apdu is received, copy it to global apdu buffer
6969
if (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) {
70-
if (G_io_app.apdu_state == APDU_IDLE) {
70+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_NFC)) {
7171
memcpy(ledger_protocol_data.rx_dst_buffer,
7272
ledger_protocol_data.rx_apdu_buffer,
7373
ledger_protocol_data.rx_apdu_length);

src/os_io_seproxyhal.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void io_usb_send_apdu_data_ep0x83(unsigned char *buffer, unsigned short length)
228228

229229
void io_seproxyhal_handle_capdu_event(void)
230230
{
231-
if (G_io_app.apdu_state == APDU_IDLE) {
231+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_RAW)) {
232232
size_t max = MIN(sizeof(G_io_apdu_buffer) - 3, sizeof(G_io_seproxyhal_spi_buffer) - 3);
233233
size_t size = U2BE(G_io_seproxyhal_spi_buffer, 1);
234234

@@ -446,9 +446,10 @@ void io_seproxyhal_init(void)
446446
G_io_app.plane_mode = plane;
447447
#endif // HAVE_BLE
448448

449-
G_io_app.apdu_state = APDU_IDLE;
450-
G_io_app.apdu_length = 0;
451-
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
449+
G_io_app.apdu_state = APDU_IDLE;
450+
G_io_app.apdu_length = 0;
451+
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
452+
G_io_app.apdu_media_lock = IO_APDU_MEDIA_NONE;
452453

453454
G_io_app.ms = 0;
454455

@@ -475,6 +476,22 @@ void io_seproxyhal_init(void)
475476
#endif // !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
476477
}
477478

479+
void io_apdu_media_lock(io_apdu_media_t media)
480+
{
481+
G_io_app.apdu_media_lock = media;
482+
}
483+
void io_apdu_media_unlock(void)
484+
{
485+
G_io_app.apdu_media_lock = IO_APDU_MEDIA_NONE;
486+
}
487+
bool io_apdu_is_media_accepted(io_apdu_media_t media)
488+
{
489+
if (G_io_app.apdu_media_lock == IO_APDU_MEDIA_NONE) {
490+
return true;
491+
}
492+
return (G_io_app.apdu_media_lock == media);
493+
}
494+
478495
#ifdef HAVE_PIEZO_SOUND
479496
void io_seproxyhal_play_tune(tune_index_e tune_index)
480497
{

0 commit comments

Comments
 (0)