| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/* Inspired by Andrew Lutomirski's 'u2f-hidraw-policy.c' */ |
| 3 |
|
| 4 |
#include "fido_id_desc.h" |
| 5 |
|
| 6 |
#define HID_RPTDESC_FIRST_BYTE_LONG_ITEM 0xfeu |
| 7 |
#define HID_RPTDESC_TYPE_GLOBAL 0x1u |
| 8 |
#define HID_RPTDESC_TYPE_LOCAL 0x2u |
| 9 |
#define HID_RPTDESC_TAG_USAGE_PAGE 0x0u |
| 10 |
#define HID_RPTDESC_TAG_USAGE 0x0u |
| 11 |
|
| 12 |
/* |
| 13 |
* HID usage for FIDO CTAP1 ("U2F") and CTAP2 security tokens. |
| 14 |
* https://fidoalliance.org/specs/fido-u2f-v1.0-ps-20141009/fido-u2f-u2f_hid.h-v1.0-ps-20141009.txt |
| 15 |
* https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#usb-discovery |
| 16 |
* https://www.usb.org/sites/default/files/hutrr48.pdf |
| 17 |
*/ |
| 18 |
#define FIDO_FULL_USAGE_CTAPHID 0xf1d00001u |
| 19 |
|
| 20 |
/* |
| 21 |
* Parses a HID report descriptor and identifies FIDO CTAP1 ("U2F")/CTAP2 security tokens based on their |
| 22 |
* declared usage. |
| 23 |
* A positive return value indicates that the report descriptor belongs to a FIDO security token. |
| 24 |
* https://www.usb.org/sites/default/files/documents/hid1_11.pdf (Section 6.2.2) |
| 25 |
*/ |
| 26 |
int is_fido_security_token_desc(const uint8_t *desc, size_t desc_len) { |
| 27 |
uint32_t usage = 0; |
| 28 |
|
| 29 |
for (size_t pos = 0; pos < desc_len; ) { |
| 30 |
uint8_t tag, type, size_code; |
| 31 |
size_t size; |
| 32 |
uint32_t value; |
| 33 |
|
| 34 |
/* Report descriptors consists of short items (1-5 bytes) and long items (3-258 bytes). */ |
| 35 |
if (desc[pos] == HID_RPTDESC_FIRST_BYTE_LONG_ITEM) { |
| 36 |
/* No long items are defined in the spec; skip them. |
| 37 |
* The length of the data in a long item is contained in the byte after the long |
| 38 |
* item tag. The header consists of three bytes: special long item tag, length, |
| 39 |
* actual tag. */ |
| 40 |
if (pos + 1 >= desc_len) |
| 41 |
return -EINVAL; |
| 42 |
pos += desc[pos + 1] + 3; |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
/* The first byte of a short item encodes tag, type and size. */ |
| 47 |
tag = desc[pos] >> 4; /* Bits 7 to 4 */ |
| 48 |
type = (desc[pos] >> 2) & 0x3; /* Bits 3 and 2 */ |
| 49 |
size_code = desc[pos] & 0x3; /* Bits 1 and 0 */ |
| 50 |
/* Size is coded as follows: |
| 51 |
* 0 -> 0 bytes, 1 -> 1 byte, 2 -> 2 bytes, 3 -> 4 bytes |
| 52 |
*/ |
| 53 |
size = size_code < 3 ? size_code : 4; |
| 54 |
/* Consume header byte. */ |
| 55 |
pos++; |
| 56 |
|
| 57 |
/* Extract the item value coded on size bytes. */ |
| 58 |
if (pos + size > desc_len) |
| 59 |
return -EINVAL; |
| 60 |
value = 0; |
| 61 |
for (size_t i = 0; i < size; i++) |
| 62 |
value |= (uint32_t) desc[pos + i] << (8 * i); |
| 63 |
/* Consume value bytes. */ |
| 64 |
pos += size; |
| 65 |
|
| 66 |
if (type == HID_RPTDESC_TYPE_GLOBAL && tag == HID_RPTDESC_TAG_USAGE_PAGE) { |
| 67 |
/* A usage page is a 16 bit value coded on at most 16 bits. */ |
| 68 |
if (size > 2) |
| 69 |
return -EINVAL; |
| 70 |
/* A usage page sets the upper 16 bits of a following usage. */ |
| 71 |
usage = (value & 0x0000ffffu) << 16; |
| 72 |
} |
| 73 |
|
| 74 |
if (type == HID_RPTDESC_TYPE_LOCAL && tag == HID_RPTDESC_TAG_USAGE) { |
| 75 |
/* A usage is a 32 bit value, but is prepended with the current usage page if |
| 76 |
* coded on less than 4 bytes (that is, at most 2 bytes). */ |
| 77 |
if (size == 4) |
| 78 |
usage = value; |
| 79 |
else |
| 80 |
usage = (usage & 0xffff0000u) | (value & 0x0000ffffu); |
| 81 |
if (usage == FIDO_FULL_USAGE_CTAPHID) |
| 82 |
return 1; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |