| 1 |
/* |
| 2 |
* Copyright (c) 2020-2026 Yubico AB. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style |
| 4 |
* license that can be found in the LICENSE file. |
| 5 |
* SPDX-License-Identifier: BSD-2-Clause |
| 6 |
*/ |
| 7 |
|
| 8 |
#undef NDEBUG |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "mutator_aux.h" |
| 17 |
#include "wiredata_fido2.h" |
| 18 |
#include "dummy.h" |
| 19 |
|
| 20 |
#include "../openbsd-compat/openbsd-compat.h" |
| 21 |
|
| 22 |
#define PACK_ARRAY_LEN 6 |
| 23 |
|
| 24 |
enum { |
| 25 |
OPT_NO_PIN = 1, |
| 26 |
OPT_PUAT = 2, |
| 27 |
|
| 28 |
OPT_EDGE, |
| 29 |
OPT_MASK = (((OPT_EDGE - 1) << 1) - 1), |
| 30 |
}; |
| 31 |
|
| 32 |
/* Parameter set defining a FIDO2 "large blob" operation. */ |
| 33 |
struct param { |
| 34 |
char pin[MAXSTR]; |
| 35 |
uint8_t opt; |
| 36 |
int seed; |
| 37 |
struct blob key; |
| 38 |
struct blob get_wiredata; |
| 39 |
struct blob set_wiredata; |
| 40 |
}; |
| 41 |
|
| 42 |
/* |
| 43 |
* Collection of HID reports from an authenticator issued with a FIDO2 |
| 44 |
* 'authenticatorLargeBlobs' 'get' command. |
| 45 |
*/ |
| 46 |
static const uint8_t dummy_get_wiredata[] = { |
| 47 |
WIREDATA_CTAP_INIT, |
| 48 |
WIREDATA_CTAP_CBOR_INFO, |
| 49 |
WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY |
| 50 |
}; |
| 51 |
|
| 52 |
/* |
| 53 |
* Collection of HID reports from an authenticator issued with a FIDO2 |
| 54 |
* 'authenticatorLargeBlobs' 'set' command. |
| 55 |
*/ |
| 56 |
static const uint8_t dummy_set_wiredata[] = { |
| 57 |
WIREDATA_CTAP_INIT, |
| 58 |
WIREDATA_CTAP_CBOR_INFO, |
| 59 |
WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY, |
| 60 |
WIREDATA_CTAP_CBOR_AUTHKEY, |
| 61 |
WIREDATA_CTAP_CBOR_PINTOKEN, |
| 62 |
WIREDATA_CTAP_CBOR_STATUS |
| 63 |
}; |
| 64 |
|
| 65 |
/* |
| 66 |
* XXX this needs to match the encrypted blob embedded in |
| 67 |
* WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY. |
| 68 |
*/ |
| 69 |
static const uint8_t dummy_key[] = { |
| 70 |
0xa9, 0x1b, 0xc4, 0xdd, 0xfc, 0x9a, 0x93, 0x79, |
| 71 |
0x75, 0xba, 0xf7, 0x7f, 0x4d, 0x57, 0xfc, 0xa6, |
| 72 |
0xe1, 0xf8, 0x06, 0x43, 0x23, 0x99, 0x51, 0x32, |
| 73 |
0xce, 0x6e, 0x19, 0x84, 0x50, 0x13, 0x2d, 0x7b |
| 74 |
}; |
| 75 |
|
| 76 |
struct param * |
| 77 |
unpack(const uint8_t *ptr, size_t len) |
| 78 |
{ |
| 79 |
cbor_item_t *item = NULL, **v; |
| 80 |
struct cbor_load_result cbor; |
| 81 |
struct param *p; |
| 82 |
int ok = -1; |
| 83 |
|
| 84 |
if ((p = calloc(1, sizeof(*p))) == NULL || |
| 85 |
(item = cbor_load(ptr, len, &cbor)) == NULL || |
| 86 |
cbor.read != len || |
| 87 |
cbor_isa_array(item) == false || |
| 88 |
cbor_array_is_definite(item) == false || |
| 89 |
cbor_array_size(item) != PACK_ARRAY_LEN || |
| 90 |
(v = cbor_array_handle(item)) == NULL) |
| 91 |
goto fail; |
| 92 |
|
| 93 |
if (unpack_int(v[0], &p->seed) < 0 || |
| 94 |
unpack_string(v[1], p->pin) < 0 || |
| 95 |
unpack_blob(v[2], &p->key) < 0 || |
| 96 |
unpack_blob(v[3], &p->get_wiredata) < 0 || |
| 97 |
unpack_blob(v[4], &p->set_wiredata) < 0 || |
| 98 |
unpack_byte(v[5], &p->opt) < 0) |
| 99 |
goto fail; |
| 100 |
|
| 101 |
ok = 0; |
| 102 |
fail: |
| 103 |
if (ok < 0) { |
| 104 |
free(p); |
| 105 |
p = NULL; |
| 106 |
} |
| 107 |
|
| 108 |
if (item) |
| 109 |
cbor_decref(&item); |
| 110 |
|
| 111 |
return p; |
| 112 |
} |
| 113 |
|
| 114 |
size_t |
| 115 |
pack(uint8_t *ptr, size_t len, const struct param *p) |
| 116 |
{ |
| 117 |
cbor_item_t *argv[PACK_ARRAY_LEN], *array = NULL; |
| 118 |
size_t cbor_alloc_len, cbor_len = 0; |
| 119 |
unsigned char *cbor = NULL; |
| 120 |
|
| 121 |
memset(argv, 0, sizeof(argv)); |
| 122 |
|
| 123 |
if ((array = cbor_new_definite_array(PACK_ARRAY_LEN)) == NULL || |
| 124 |
(argv[0] = pack_int(p->seed)) == NULL || |
| 125 |
(argv[1] = pack_string(p->pin)) == NULL || |
| 126 |
(argv[2] = pack_blob(&p->key)) == NULL || |
| 127 |
(argv[3] = pack_blob(&p->get_wiredata)) == NULL || |
| 128 |
(argv[4] = pack_blob(&p->set_wiredata)) == NULL || |
| 129 |
(argv[5] = pack_byte(p->opt)) == NULL) |
| 130 |
goto fail; |
| 131 |
|
| 132 |
for (size_t i = 0; i < PACK_ARRAY_LEN; i++) |
| 133 |
if (cbor_array_push(array, argv[i]) == false) |
| 134 |
goto fail; |
| 135 |
|
| 136 |
if ((cbor_len = cbor_serialize_alloc(array, &cbor, |
| 137 |
&cbor_alloc_len)) == 0 || cbor_len > len) { |
| 138 |
cbor_len = 0; |
| 139 |
goto fail; |
| 140 |
} |
| 141 |
|
| 142 |
memcpy(ptr, cbor, cbor_len); |
| 143 |
fail: |
| 144 |
for (size_t i = 0; i < PACK_ARRAY_LEN; i++) |
| 145 |
if (argv[i]) |
| 146 |
cbor_decref(&argv[i]); |
| 147 |
|
| 148 |
if (array) |
| 149 |
cbor_decref(&array); |
| 150 |
|
| 151 |
free(cbor); |
| 152 |
|
| 153 |
return cbor_len; |
| 154 |
} |
| 155 |
|
| 156 |
size_t |
| 157 |
pack_dummy(uint8_t *ptr, size_t len) |
| 158 |
{ |
| 159 |
struct param dummy; |
| 160 |
uint8_t blob[MAXCORPUS]; |
| 161 |
size_t blob_len; |
| 162 |
|
| 163 |
memset(&dummy, 0, sizeof(dummy)); |
| 164 |
|
| 165 |
strlcpy(dummy.pin, dummy_pin, sizeof(dummy.pin)); |
| 166 |
|
| 167 |
dummy.get_wiredata.len = sizeof(dummy_get_wiredata); |
| 168 |
dummy.set_wiredata.len = sizeof(dummy_set_wiredata); |
| 169 |
dummy.key.len = sizeof(dummy_key); |
| 170 |
|
| 171 |
memcpy(&dummy.get_wiredata.body, &dummy_get_wiredata, |
| 172 |
dummy.get_wiredata.len); |
| 173 |
memcpy(&dummy.set_wiredata.body, &dummy_set_wiredata, |
| 174 |
dummy.set_wiredata.len); |
| 175 |
memcpy(&dummy.key.body, &dummy_key, dummy.key.len); |
| 176 |
|
| 177 |
assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0); |
| 178 |
|
| 179 |
if (blob_len > len) { |
| 180 |
memcpy(ptr, blob, len); |
| 181 |
return len; |
| 182 |
} |
| 183 |
|
| 184 |
memcpy(ptr, blob, blob_len); |
| 185 |
|
| 186 |
return blob_len; |
| 187 |
} |
| 188 |
|
| 189 |
static const char * |
| 190 |
maybe_pin(const struct param *p) |
| 191 |
{ |
| 192 |
return p->opt & OPT_NO_PIN ? NULL : p->pin; |
| 193 |
} |
| 194 |
|
| 195 |
static fido_dev_t * |
| 196 |
prepare_dev(const struct blob *wire_data, const struct param *p) |
| 197 |
{ |
| 198 |
fido_dev_t *dev; |
| 199 |
|
| 200 |
set_wire_data(wire_data->body, wire_data->len); |
| 201 |
|
| 202 |
if ((dev = open_dev(0)) == NULL) |
| 203 |
return NULL; |
| 204 |
|
| 205 |
if (p->opt & OPT_PUAT) |
| 206 |
fido_dev_get_puat(dev, FIDO_PUAT_LARGEBLOB, NULL, maybe_pin(p)); |
| 207 |
|
| 208 |
return dev; |
| 209 |
} |
| 210 |
|
| 211 |
static void |
| 212 |
get_blob(const struct param *p, int array) |
| 213 |
{ |
| 214 |
fido_dev_t *dev; |
| 215 |
u_char *ptr = NULL; |
| 216 |
size_t len = 0; |
| 217 |
|
| 218 |
if ((dev = prepare_dev(&p->get_wiredata, p)) == NULL) |
| 219 |
return; |
| 220 |
|
| 221 |
if (array) |
| 222 |
fido_dev_largeblob_get_array(dev, &ptr, &len); |
| 223 |
else |
| 224 |
fido_dev_largeblob_get(dev, p->key.body, p->key.len, &ptr, &len); |
| 225 |
consume(ptr, len); |
| 226 |
free(ptr); |
| 227 |
|
| 228 |
fido_dev_close(dev); |
| 229 |
fido_dev_free(&dev); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
static void |
| 234 |
set_blob(const struct param *p, int op) |
| 235 |
{ |
| 236 |
fido_dev_t *dev; |
| 237 |
const char *pin = maybe_pin(p); |
| 238 |
|
| 239 |
if ((dev = prepare_dev(&p->set_wiredata, p)) == NULL) |
| 240 |
return; |
| 241 |
|
| 242 |
switch (op) { |
| 243 |
case 0: |
| 244 |
fido_dev_largeblob_remove(dev, p->key.body, p->key.len, pin); |
| 245 |
break; |
| 246 |
case 1: |
| 247 |
/* XXX reuse p->get_wiredata as the blob to be set */ |
| 248 |
fido_dev_largeblob_set(dev, p->key.body, p->key.len, |
| 249 |
p->get_wiredata.body, p->get_wiredata.len, pin); |
| 250 |
break; |
| 251 |
case 2: |
| 252 |
/* XXX reuse p->get_wiredata as the body of the cbor array */ |
| 253 |
fido_dev_largeblob_set_array(dev, p->get_wiredata.body, |
| 254 |
p->get_wiredata.len, pin); |
| 255 |
} |
| 256 |
|
| 257 |
fido_dev_close(dev); |
| 258 |
fido_dev_free(&dev); |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
test(const struct param *p) |
| 263 |
{ |
| 264 |
prng_init((unsigned int)p->seed); |
| 265 |
fuzz_clock_reset(); |
| 266 |
fido_init(FIDO_DEBUG); |
| 267 |
fido_set_log_handler(consume_str); |
| 268 |
|
| 269 |
get_blob(p, 0); |
| 270 |
get_blob(p, 1); |
| 271 |
set_blob(p, 0); |
| 272 |
set_blob(p, 1); |
| 273 |
set_blob(p, 2); |
| 274 |
} |
| 275 |
|
| 276 |
void |
| 277 |
mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN |
| 278 |
{ |
| 279 |
if (flags & MUTATE_SEED) |
| 280 |
p->seed = (int)seed; |
| 281 |
|
| 282 |
if (flags & MUTATE_PARAM) { |
| 283 |
mutate_blob(&p->key); |
| 284 |
mutate_string(p->pin); |
| 285 |
mutate_byte(&p->opt); |
| 286 |
|
| 287 |
p->opt &= OPT_MASK; |
| 288 |
} |
| 289 |
|
| 290 |
if (flags & MUTATE_WIREDATA) { |
| 291 |
mutate_blob(&p->get_wiredata); |
| 292 |
mutate_blob(&p->set_wiredata); |
| 293 |
} |
| 294 |
} |