Skip to content

Commit 10ce7f4

Browse files
Add functions to lock/unlock the APDU source to a specific media
1 parent dedfb45 commit 10ce7f4

File tree

4 files changed

+53
-12
lines changed

4 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
@@ -243,6 +243,7 @@ typedef struct io_seph_s {
243243
unsigned short apdu_length; // total length to be received
244244
unsigned short io_flags; // flags to be set when calling io_exchange
245245
io_apdu_media_t apdu_media;
246+
io_apdu_media_t apdu_media_lock;
246247

247248
unsigned int ms;
248249

@@ -282,11 +283,29 @@ extern io_seph_app_t G_io_app;
282283
*/
283284
void io_task(void);
284285
/**
285-
* IO task initializez
286+
* IO task initialize
286287
*/
287288
void io_start(void);
288289
#endif // HAVE_IO_TASK
289290

291+
/**
292+
* Lock apdu source to a media
293+
* After this function is called, all subsequent apdus
294+
* that come from any other media will be refused
295+
*/
296+
void io_apdu_media_lock(io_apdu_media_t media);
297+
/**
298+
* Unlock apdu source.
299+
* After this function is called, apdus any media will be processed
300+
*/
301+
void io_apdu_media_unlock(void);
302+
/**
303+
* Check if an Apdu media shall be accepted.
304+
* Return true if source media is not locked or if the media corresponds to the current lock
305+
* Return false if media doesn't correspond to the current lock
306+
*/
307+
bool io_apdu_is_media_accepted(io_apdu_media_t media);
308+
290309
void io_seproxyhal_setup_ticker(unsigned int interval_ms);
291310
void io_seproxyhal_power_off(bool criticalBattery);
292311
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
@@ -511,7 +511,8 @@ static void hci_evt_cmd_complete(const uint8_t *buffer, uint16_t length)
511511
if (ledger_ble_data.transfer_mode_enable) {
512512
if ((ledger_protocol_data.rx_apdu_length)
513513
&& (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE)) {
514-
if (G_io_app.apdu_state == APDU_IDLE) {
514+
if (G_io_app.apdu_state == APDU_IDLE
515+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
515516
copy_apdu_to_app(false);
516517
}
517518
else {
@@ -886,7 +887,8 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
886887
LOG_BLE("Transfer failed 0x%04x\n", U2BE(ledger_ble_data.resp, 0));
887888
G_io_app.transfer_mode = 0;
888889
check_transfer_mode(G_io_app.transfer_mode);
889-
if (G_io_app.apdu_state == APDU_IDLE) {
890+
if (G_io_app.apdu_state == APDU_IDLE
891+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
890892
copy_apdu_to_app(true);
891893
}
892894
else {
@@ -907,7 +909,8 @@ static void attribute_modified(const uint8_t *buffer, uint16_t length)
907909
}
908910
else {
909911
// Nominal case for apdu reception
910-
if (G_io_app.apdu_state == APDU_IDLE) {
912+
if (G_io_app.apdu_state == APDU_IDLE
913+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
911914
copy_apdu_to_app(true);
912915
}
913916
else {
@@ -954,7 +957,7 @@ static void write_permit_request(const uint8_t *buffer, uint16_t length)
954957
data_length,
955958
&buffer[3]);
956959
if (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) {
957-
if (G_io_app.apdu_state == APDU_IDLE) {
960+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_BLE)) {
958961
copy_apdu_to_app(true);
959962
}
960963
else {

lib_stusb_impl/usbd_impl.c

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

10371037
#ifndef HAVE_USB_HIDKBD
10381038
// avoid troubles when an apdu has not been replied yet
1039-
if (G_io_app.apdu_state == APDU_IDLE) {
1039+
if (G_io_app.apdu_state == APDU_IDLE
1040+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_USB_HID)) {
10401041
// add to the hid transport
10411042
switch (io_usb_hid_receive(io_usb_send_apdu_data,
10421043
buffer,
@@ -1124,7 +1125,8 @@ uint8_t USBD_WEBUSB_DataOut(USBD_HandleTypeDef *pdev,
11241125
USBD_LL_PrepareReceive(pdev, WEBUSB_EPOUT_ADDR, WEBUSB_EPOUT_SIZE);
11251126

11261127
// avoid troubles when an apdu has not been replied yet
1127-
if (G_io_app.apdu_state == APDU_IDLE) {
1128+
if (G_io_app.apdu_state == APDU_IDLE
1129+
&& io_apdu_is_media_accepted(IO_APDU_MEDIA_USB_WEBUSB)) {
11281130
// add to the hid transport
11291131
switch (io_usb_hid_receive(io_usb_send_apdu_data_ep0x83,
11301132
buffer,

src/os_io_seproxyhal.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ void io_usb_send_apdu_data_ep0x83(unsigned char *buffer, unsigned short length)
231231

232232
void io_seproxyhal_handle_capdu_event(void)
233233
{
234-
if (G_io_app.apdu_state == APDU_IDLE) {
234+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_RAW)) {
235235
size_t max = MIN(sizeof(G_io_apdu_buffer) - 3, sizeof(G_io_seproxyhal_spi_buffer) - 3);
236236
size_t size = U2BE(G_io_seproxyhal_spi_buffer, 1);
237237

@@ -254,7 +254,7 @@ void io_seproxyhal_handle_capdu_event(void)
254254
#ifdef HAVE_NFC
255255
void io_seproxyhal_handle_nfc_recv_event(void)
256256
{
257-
if (G_io_app.apdu_state == APDU_IDLE) {
257+
if (G_io_app.apdu_state == APDU_IDLE && io_apdu_is_media_accepted(IO_APDU_MEDIA_NFC)) {
258258
size_t max = MIN(sizeof(G_io_apdu_buffer), sizeof(G_io_seproxyhal_spi_buffer) - 3);
259259
size_t size = U2BE(G_io_seproxyhal_spi_buffer, 1);
260260

@@ -457,9 +457,10 @@ void io_seproxyhal_init(void)
457457
G_io_app.plane_mode = plane;
458458
#endif // HAVE_BLE
459459

460-
G_io_app.apdu_state = APDU_IDLE;
461-
G_io_app.apdu_length = 0;
462-
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
460+
G_io_app.apdu_state = APDU_IDLE;
461+
G_io_app.apdu_length = 0;
462+
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
463+
G_io_app.apdu_media_lock = IO_APDU_MEDIA_NONE;
463464

464465
G_io_app.ms = 0;
465466

@@ -482,6 +483,22 @@ void io_seproxyhal_init(void)
482483
#endif // !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
483484
}
484485

486+
void io_apdu_media_lock(io_apdu_media_t media)
487+
{
488+
G_io_app.apdu_media_lock = media;
489+
}
490+
void io_apdu_media_unlock(void)
491+
{
492+
G_io_app.apdu_media_lock = IO_APDU_MEDIA_NONE;
493+
}
494+
bool io_apdu_is_media_accepted(io_apdu_media_t media)
495+
{
496+
if (G_io_app.apdu_media_lock == IO_APDU_MEDIA_NONE) {
497+
return true;
498+
}
499+
return (G_io_app.apdu_media_lock == media);
500+
}
501+
485502
#ifdef HAVE_PIEZO_SOUND
486503
void io_seproxyhal_play_tune(tune_index_e tune_index)
487504
{

0 commit comments

Comments
 (0)