Skip to content

Commit fba4d67

Browse files
committed
Merge remote-tracking branch 'origin/master' into wip/merge-tusb
2 parents 0d0b663 + 2f87d7d commit fba4d67

File tree

9 files changed

+66
-14
lines changed

9 files changed

+66
-14
lines changed

applets/oath/oath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int oath_install(const uint8_t reset) {
4141
static int oath_select(const CAPDU *capdu, RAPDU *rapdu) {
4242
if (P2 != 0x00) EXCEPT(SW_WRONG_P1P2);
4343

44-
memcpy(RDATA, (uint8_t[]){OATH_TAG_VERSION, 3, 0x06, 0x00, 0x00, OATH_TAG_NAME, HANDLE_LEN}, 7);
44+
memcpy(RDATA, ((uint8_t[]){OATH_TAG_VERSION, 3, 0x06, 0x00, 0x00, OATH_TAG_NAME, HANDLE_LEN}), 7);
4545
if (read_attr(OATH_FILE, ATTR_HANDLE, RDATA + 7, HANDLE_LEN) < 0) return -1;
4646
LL = 7 + HANDLE_LEN;
4747

applets/openpgp/openpgp.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ static const uint8_t extended_length_info[] = {0x02, 0x02, HI(APDU_BUFFER_SIZE),
7878
0x02, 0x02, HI(APDU_BUFFER_SIZE), LO(APDU_BUFFER_SIZE)};
7979

8080
static const uint8_t extended_capabilities[] = {
81-
0x34, // Support key import, pw1 status change, and algorithm attributes changes
81+
0x74, // Support get challenge, key import, pw1 status change, and algorithm attributes changes
8282
0x00, // No SM algorithm
83-
0x00,
84-
0x00, // No challenge support
83+
HI(APDU_BUFFER_SIZE),
84+
LO(APDU_BUFFER_SIZE), // Challenge size
8585
HI(MAX_CERT_LENGTH),
8686
LO(MAX_CERT_LENGTH), // Cert length
8787
HI(MAX_DO_LENGTH),
@@ -1201,6 +1201,14 @@ static int openpgp_activate(const CAPDU *capdu, RAPDU *rapdu) {
12011201
return openpgp_install(1);
12021202
}
12031203

1204+
static int openpgp_get_challenge(const CAPDU *capdu, RAPDU *rapdu) {
1205+
if (P1 != 0x00 || P2 != 0x00) EXCEPT(SW_WRONG_P1P2);
1206+
if (LE > APDU_BUFFER_SIZE) EXCEPT(SW_WRONG_LENGTH);
1207+
random_buffer(RDATA, LE);
1208+
LL = LE;
1209+
return 0;
1210+
}
1211+
12041212
int openpgp_process_apdu(const CAPDU *capdu, RAPDU *rapdu) {
12051213
LL = 0;
12061214
SW = SW_NO_ERROR;
@@ -1287,6 +1295,9 @@ int openpgp_process_apdu(const CAPDU *capdu, RAPDU *rapdu) {
12871295
ret = openpgp_sign_or_auth(capdu, rapdu, false);
12881296
stop_blinking();
12891297
break;
1298+
case OPENPGP_INS_GET_CHALLENGE:
1299+
ret = openpgp_get_challenge(capdu, rapdu);
1300+
break;
12901301
case OPENPGP_INS_TERMINATE:
12911302
ret = openpgp_terminate(capdu, rapdu);
12921303
break;

include/openpgp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define OPENPGP_INS_GENERATE_ASYMMETRIC_KEY_PAIR 0x47
1919
#define OPENPGP_INS_TERMINATE 0xE6
2020
#define OPENPGP_INS_ACTIVATE 0x44
21+
#define OPENPGP_INS_GET_CHALLENGE 0x84
2122

2223
#define TAG_AID 0x4F
2324
#define TAG_LOGIN 0x5E

interfaces/USB/class/kbdhid/kbdhid.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <kbdhid.h>
1010
#include <usb_descriptors.h>
1111

12+
#define EJECT_KEY 0x03
13+
1214
static enum {
1315
KBDHID_Idle,
1416
KBDHID_Typing,
@@ -85,15 +87,22 @@ static void KBDHID_TypeKeySeq(void) {
8587
DBG_MSG("Key typing ended\n");
8688
state = KBDHID_Idle;
8789
} else if (tud_hid_n_ready(HID_ITF_KBD)) {
88-
// Emulate key down
89-
keycode[0] = ascii2keycode(key_sequence[key_seq_position]);
90-
if (keycode[0] & 0x80) { // Check for shift flag
91-
modifier = 0x02; // Shift key
92-
keycode[0] &= 0x7F; // Clear shift flag
90+
uint8_t report_id = 1;
91+
if (key_sequence[key_seq_position] == EJECT_KEY) {
92+
report_id = 2;
93+
keycode[0] = 0;
94+
modifier = 0xB8;
9395
} else {
94-
modifier = 0; // No modifier key
96+
// Emulate key down
97+
keycode[0] = ascii2keycode(key_sequence[key_seq_position]);
98+
if (keycode[0] & 0x80) { // Check for shift flag
99+
modifier = 0x02; // Shift key
100+
keycode[0] &= 0x7F; // Clear shift flag
101+
} else {
102+
modifier = 0; // No modifier key
103+
}
95104
}
96-
tud_hid_n_keyboard_report(HID_ITF_KBD, 0, modifier, keycode);
105+
tud_hid_n_keyboard_report(HID_ITF_KBD, report_id, modifier, keycode);
97106

98107
state = KBDHID_KeyDown;
99108
}
@@ -104,7 +113,13 @@ static void KBDHID_TypeKeySeq(void) {
104113
// Emulate key release
105114
modifier = 0;
106115
keycode[0] = 0;
107-
tud_hid_n_keyboard_report(HID_ITF_KBD, 0, modifier, keycode);
116+
if (key_sequence[key_seq_position] == EJECT_KEY) {
117+
// Emulate the key release
118+
tud_hid_n_keyboard_report(HID_ITF_KBD, 2, modifier, keycode);
119+
} else {
120+
// Emulate the key release
121+
tud_hid_n_keyboard_report(HID_ITF_KBD, 1, modifier, keycode);
122+
}
108123

109124
key_seq_position++;
110125
state = KBDHID_KeyUp;
@@ -113,6 +128,13 @@ static void KBDHID_TypeKeySeq(void) {
113128
}
114129
}
115130

131+
void KBDHID_Eject() {
132+
key_sequence[0] = EJECT_KEY;
133+
key_sequence[1] = 0;
134+
key_seq_position = 0;
135+
state = KBDHID_Typing;
136+
}
137+
116138
void kbd_hid_init(void) {
117139
state = KBDHID_Idle;
118140

interfaces/USB/class/kbdhid/kbdhid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
void kbd_hid_init();
88
void kbd_hid_loop();
9+
void KBDHID_Eject(void);
910

1011
void kbd_hid_report_complete_cb(uint8_t const* report, uint8_t len);
1112
uint16_t kbd_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);

interfaces/USB/device/usb_descriptors.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ uint8_t const desc_ctaphid_report[] = {
4444
};
4545

4646
uint8_t const desc_kbdhid_report[] = {
47-
TUD_HID_REPORT_DESC_KEYBOARD()
47+
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ) ,
48+
TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(2) ) ,
4849
};
4950

5051
// Invoked when received GET HID REPORT DESCRIPTOR

src/apdu.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <oath.h>
1111
#include <openpgp.h>
1212
#include <piv.h>
13+
#include <kbdhid.h>
1314

1415
enum APPLET {
1516
APPLET_NULL,
@@ -151,6 +152,13 @@ int apdu_output(RAPDU_CHAINING *ex, RAPDU *sh) {
151152
}
152153

153154
void process_apdu(CAPDU *capdu, RAPDU *rapdu) {
155+
if (CLA == 0xFF && INS == 0xEE && P1 == 0xFF && P2 == 0xEE) {
156+
// A special APDU to trigger Eject
157+
KBDHID_Eject();
158+
LL = 0;
159+
SW = SW_NO_ERROR;
160+
return;
161+
}
154162
static enum PIV_STATE piv_state;
155163
if (current_applet == APPLET_PIV) {
156164
// Offload some APDU chaining commands of PIV applet,

src/device.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void device_loop(uint8_t has_touch) {
3232
ctap_hid_loop(0);
3333
webusb_loop();
3434
if (has_touch && // hardware features the touch pad
35-
!device_is_blinking() // applets are not waiting for touch
35+
!device_is_blinking() && // applets are not waiting for touch
36+
device_get_tick() > 2000 // ignore touch for the first 2 seconds
3637
)
3738
kbd_hid_loop();
3839
}

test-via-pcsc/openpgp_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func TestOpenPGPApplet(t *testing.T) {
107107
So(res, ShouldResemble, []byte{2, 2, 0x05, 0x3C, 2, 2, 0x05, 0x3C}) // 1340 bytes
108108
})
109109

110+
Convey("Get challenge", func(ctx C) {
111+
res, code, err := app.Send([]byte{0x00, 0x84, 0x00, 0x00, 0x00, 0x05, 0x3C})
112+
So(err, ShouldBeNil)
113+
So(code, ShouldEqual, 0x9000)
114+
So(len(res), ShouldEqual, 0x53C) // 1340 bytes
115+
})
116+
110117
Convey("Admin PIN retry times", func(ctx C) {
111118
_, code, err := app.Send([]byte{0x00, 0x20, 0x00, 0x83})
112119
So(err, ShouldBeNil)

0 commit comments

Comments
 (0)