| 1 |
/* |
| 2 |
* Copyright (c) 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 |
#include "../openbsd-compat/openbsd-compat.h" |
| 9 |
|
| 10 |
#include <fido.h> |
| 11 |
|
| 12 |
/* openssl sha256 -binary </dev/null | xxd -i */ |
| 13 |
static const unsigned char CLIENT_DATA_HASH[] = { |
| 14 |
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, |
| 15 |
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 16 |
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 17 |
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 18 |
}; |
| 19 |
|
| 20 |
#define RPID "relying party" |
| 21 |
#define USER_NAME "Archimedes" |
| 22 |
|
| 23 |
static const unsigned char USER_ID[] = { |
| 24 |
0x33, 0x2e, 0x31, 0x34, 0x31, 0x35, 0x39, 0x32, |
| 25 |
0x36, 0x35, 0x33, 0x35, 0x38, 0x39, 0x37, 0x39, |
| 26 |
0x33, 0x32, 0x33, 0x38, 0x34, 0x36, 0x32, 0x36, |
| 27 |
0x34, 0x33, 0x33 |
| 28 |
}; |
| 29 |
|
| 30 |
static const unsigned char LARGE_BLOB[] = { |
| 31 |
0x49, 0x6e, 0x20, 0x41, 0x6e, 0x63, 0x69, 0x65, |
| 32 |
0x6e, 0x74, 0x20, 0x47, 0x72, 0x65, 0x65, 0x6b, |
| 33 |
0x20, 0x6f, 0x6e, 0x65, 0x20, 0x63, 0x6f, 0x75, |
| 34 |
0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6d, 0x61, |
| 35 |
0x74, 0x68, 0x65, 0x6d, 0x61, 0x74, 0x69, 0x63, |
| 36 |
0x69, 0x61, 0x6e, 0x2c, 0x20, 0x70, 0x68, 0x79, |
| 37 |
0x73, 0x69, 0x63, 0x69, 0x73, 0x74, 0x2c, 0x20, |
| 38 |
0x65, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, |
| 39 |
0x20, 0x61, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x6f, |
| 40 |
0x6d, 0x65, 0x72, 0x2c, 0x20, 0x61, 0x6e, 0x64, |
| 41 |
0x20, 0x69, 0x6e, 0x76, 0x65, 0x6e, 0x74, 0x6f, |
| 42 |
0x72, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68, 0x65, |
| 43 |
0x20, 0x73, 0x61, 0x6d, 0x65, 0x20, 0x74, 0x69, |
| 44 |
0x6d, 0x65, 0x0a |
| 45 |
}; |
| 46 |
|
| 47 |
static int |
| 48 |
prepare_cred(fido_cred_t **cred_out) |
| 49 |
{ |
| 50 |
int r; |
| 51 |
fido_cred_t *cred = NULL; |
| 52 |
|
| 53 |
if ((cred = fido_cred_new()) == NULL) |
| 54 |
errx(1, "fido_cred_new"); |
| 55 |
|
| 56 |
if ((r = fido_cred_set_clientdata_hash(cred, CLIENT_DATA_HASH, |
| 57 |
sizeof(CLIENT_DATA_HASH))) != FIDO_OK) { |
| 58 |
warnx("fido_cred_set_clientdata_hash: %s", fido_strerr(r)); |
| 59 |
goto exit; |
| 60 |
} |
| 61 |
|
| 62 |
if ((r = fido_cred_set_type(cred, COSE_ES256)) != FIDO_OK) { |
| 63 |
warnx("fido_cred_set_type: %s", fido_strerr(r)); |
| 64 |
goto exit; |
| 65 |
} |
| 66 |
|
| 67 |
if ((r = fido_cred_set_rp(cred, RPID, NULL)) != FIDO_OK) { |
| 68 |
warnx("fido_cred_set_rp: %s", fido_strerr(r)); |
| 69 |
goto exit; |
| 70 |
} |
| 71 |
|
| 72 |
if ((r = fido_cred_set_user(cred, USER_ID, sizeof(USER_ID), USER_NAME, |
| 73 |
NULL, NULL)) != FIDO_OK) { |
| 74 |
warnx("fido_cred_set_user: %s", fido_strerr(r)); |
| 75 |
goto exit; |
| 76 |
} |
| 77 |
|
| 78 |
if ((r = fido_cred_set_rk(cred, FIDO_OPT_TRUE)) != FIDO_OK) { |
| 79 |
warnx("fido_cred_set_rk: %s", fido_strerr(r)); |
| 80 |
goto exit; |
| 81 |
} |
| 82 |
|
| 83 |
if ((r = fido_cred_set_extensions(cred, |
| 84 |
FIDO_EXT_LARGEBLOB_KEY)) != FIDO_OK) { |
| 85 |
warnx("fido_cred_set_extensions: %s", fido_strerr(r)); |
| 86 |
goto exit; |
| 87 |
} |
| 88 |
|
| 89 |
exit: |
| 90 |
if (r) |
| 91 |
fido_cred_free(&cred); |
| 92 |
else |
| 93 |
*cred_out = cred; |
| 94 |
return r; |
| 95 |
} |
| 96 |
|
| 97 |
static int |
| 98 |
run_make_cred(fido_dev_t *dev, const char *pin) |
| 99 |
{ |
| 100 |
int r; |
| 101 |
fido_cred_t *cred = NULL; |
| 102 |
|
| 103 |
warnx("Create PUAT"); |
| 104 |
if ((r = fido_dev_get_puat(dev, |
| 105 |
FIDO_PUAT_MAKECRED | FIDO_PUAT_LARGEBLOB, RPID, pin)) != FIDO_OK) { |
| 106 |
warnx("could not get puat: %s", fido_strerr(r)); |
| 107 |
return r; |
| 108 |
} |
| 109 |
|
| 110 |
warnx("Make credential"); |
| 111 |
if ((r = prepare_cred(&cred)) != FIDO_OK) |
| 112 |
return r; |
| 113 |
if ((r = fido_dev_make_cred(dev, cred, NULL)) != FIDO_OK) { |
| 114 |
warnx("could not make cred: %s", fido_strerr(r)); |
| 115 |
goto exit; |
| 116 |
} |
| 117 |
|
| 118 |
warnx("Add large blob (%zu bytes)", sizeof(LARGE_BLOB)); |
| 119 |
if ((r = fido_dev_largeblob_set(dev, fido_cred_largeblob_key_ptr(cred), |
| 120 |
fido_cred_largeblob_key_len(cred), LARGE_BLOB, sizeof(LARGE_BLOB), |
| 121 |
NULL)) != FIDO_OK) |
| 122 |
warnx("fido_dev_largeblob_set: %s", fido_strerr(r)); |
| 123 |
|
| 124 |
exit: |
| 125 |
fido_cred_free(&cred); |
| 126 |
return r; |
| 127 |
} |
| 128 |
|
| 129 |
static int |
| 130 |
prepare_assert(fido_assert_t **assert_out) |
| 131 |
{ |
| 132 |
int r; |
| 133 |
fido_assert_t *assert; |
| 134 |
|
| 135 |
if ((assert = fido_assert_new()) == NULL) |
| 136 |
errx(1, "fido_assert_new"); |
| 137 |
|
| 138 |
if ((r = fido_assert_set_clientdata_hash(assert, CLIENT_DATA_HASH, |
| 139 |
sizeof(CLIENT_DATA_HASH))) != FIDO_OK) { |
| 140 |
warnx("fido_assert_set_clientdata_hash: %s", fido_strerr(r)); |
| 141 |
goto fail; |
| 142 |
} |
| 143 |
|
| 144 |
if ((r = fido_assert_set_up(assert, FIDO_OPT_FALSE)) != FIDO_OK) { |
| 145 |
warnx("fido_assert_set_up: %s", fido_strerr(r)); |
| 146 |
goto fail; |
| 147 |
} |
| 148 |
|
| 149 |
if ((r = fido_assert_set_rp(assert, RPID)) != FIDO_OK) { |
| 150 |
warnx("fido_assert_set_rp: %s", fido_strerr(r)); |
| 151 |
goto fail; |
| 152 |
} |
| 153 |
|
| 154 |
if ((r = fido_assert_set_extensions(assert, |
| 155 |
FIDO_EXT_LARGEBLOB_KEY)) != FIDO_OK) { |
| 156 |
warnx("fido_assert_set_extensions: %s", fido_strerr(r)); |
| 157 |
goto fail; |
| 158 |
} |
| 159 |
|
| 160 |
*assert_out = assert; |
| 161 |
return 0; |
| 162 |
fail: |
| 163 |
fido_assert_free(&assert); |
| 164 |
return r; |
| 165 |
} |
| 166 |
|
| 167 |
static int |
| 168 |
run_get_assertion(fido_dev_t *dev, const char *pin) |
| 169 |
{ |
| 170 |
fido_assert_t *assert; |
| 171 |
unsigned char *blob_ptr = NULL; |
| 172 |
size_t n_assert, blob_len = 0; |
| 173 |
int r; |
| 174 |
|
| 175 |
warnx("Create PUAT"); |
| 176 |
if ((r = fido_dev_get_puat(dev, |
| 177 |
FIDO_PUAT_GETASSERT | FIDO_PUAT_LARGEBLOB, RPID, |
| 178 |
pin)) != FIDO_OK) { |
| 179 |
warnx("could not get puat: %s", fido_strerr(r)); |
| 180 |
return r; |
| 181 |
} |
| 182 |
|
| 183 |
warnx("Get Assert"); |
| 184 |
if ((r = prepare_assert(&assert)) != FIDO_OK) |
| 185 |
goto exit; |
| 186 |
if ((r = fido_dev_get_assert(dev, assert, NULL)) != FIDO_OK) { |
| 187 |
warnx("fido_dev_get_assert: %s", fido_strerr(r)); |
| 188 |
goto exit; |
| 189 |
} |
| 190 |
|
| 191 |
if ((n_assert = fido_assert_count(assert)) != 1) { |
| 192 |
warnx("fido_assert_count -> %zu", n_assert); |
| 193 |
if (n_assert == 0) |
| 194 |
goto exit; |
| 195 |
} |
| 196 |
|
| 197 |
warnx("Extract large blob"); |
| 198 |
if ((r = fido_dev_largeblob_get(dev, |
| 199 |
fido_assert_largeblob_key_ptr(assert, 0), |
| 200 |
fido_assert_largeblob_key_len(assert, 0), |
| 201 |
&blob_ptr, &blob_len)) != FIDO_OK) { |
| 202 |
warnx("fido_dev_largeblob_get: %s", fido_strerr(r)); |
| 203 |
goto exit; |
| 204 |
} |
| 205 |
|
| 206 |
warnx("Remove large blob (%zu bytes)", blob_len); |
| 207 |
if ((r = fido_dev_largeblob_remove(dev, |
| 208 |
fido_assert_largeblob_key_ptr(assert, 0), |
| 209 |
fido_assert_largeblob_key_len(assert, 0), |
| 210 |
NULL)) != FIDO_OK) |
| 211 |
warnx("fido_dev_largeblob_remove: %s", fido_strerr(r)); |
| 212 |
|
| 213 |
r = 0; |
| 214 |
exit: |
| 215 |
free(blob_ptr); |
| 216 |
fido_assert_free(&assert); |
| 217 |
return r; |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
usage(void) |
| 222 |
{ |
| 223 |
fprintf(stderr, "usage: largeblob [-P pin] <device>\n"); |
| 224 |
exit(1); |
| 225 |
} |
| 226 |
|
| 227 |
int |
| 228 |
main(int argc, char **argv) |
| 229 |
{ |
| 230 |
fido_dev_t *dev = NULL; |
| 231 |
int r, ch; |
| 232 |
const char *path, *pin = NULL; |
| 233 |
|
| 234 |
while ((ch = getopt(argc, argv, "P:")) != -1) { |
| 235 |
switch (ch) { |
| 236 |
case 'P': |
| 237 |
pin = optarg; |
| 238 |
break; |
| 239 |
default: |
| 240 |
usage(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if (argc - optind < 1) |
| 245 |
usage(); |
| 246 |
path = argv[optind]; |
| 247 |
|
| 248 |
fido_init(0); |
| 249 |
|
| 250 |
if ((dev = fido_dev_new()) == NULL) |
| 251 |
errx(1, "fido_dev_new"); |
| 252 |
|
| 253 |
if ((r = fido_dev_open(dev, path)) != FIDO_OK) { |
| 254 |
warnx("could not open %s: %s", path, fido_strerr(r)); |
| 255 |
goto quit; |
| 256 |
} |
| 257 |
|
| 258 |
warnx("Make credential + large blob"); |
| 259 |
if ((r = run_make_cred(dev, pin)) != FIDO_OK) |
| 260 |
goto quit; |
| 261 |
|
| 262 |
warnx("Get assertion + large blob"); |
| 263 |
r = run_get_assertion(dev, pin); |
| 264 |
|
| 265 |
quit: |
| 266 |
fido_dev_close(dev); |
| 267 |
fido_dev_free(&dev); |
| 268 |
return r ? 1 : 0; |
| 269 |
} |