-
Notifications
You must be signed in to change notification settings - Fork 27
/
piv-chuid.c
656 lines (570 loc) · 13.2 KB
/
piv-chuid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2017, Joyent Inc
* Author: Alex Wilson <[email protected]>
* Copyright 2024 The University of Queensland
* Author: Alex Wilson <[email protected]>
*/
/*
* Documentation references used below:
* [piv]: https://csrc.nist.gov/publications/detail/sp/800-73/4/final
* [yubico-piv]: https://developers.yubico.com/PIV/Introduction/Yubico_extensions.html
* [iso7816]: (you'll need an ISO membership, or try a university library)
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <strings.h>
#include <limits.h>
#include "debug.h"
#include "openssh/config.h"
#include "openssh/ssherr.h"
#include "openssh/sshkey.h"
#include "openssh/sshbuf.h"
#include "openssh/digest.h"
#include "openssh/cipher.h"
#include "openssh/authfd.h"
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "utils.h"
#include "tlv.h"
#include "piv.h"
#include "bunyan.h"
#include "utils.h"
/* Contains structs apdubuf, piv_ecdh_box, and enum piv_box_version */
#include "piv-internal.h"
enum piv_chuid_flags {
PIV_CHUID_HAS_CHUUID = (1<<0),
PIV_CHUID_HAS_BUFLEN = (1<<1),
};
struct piv_chuid {
enum piv_chuid_flags pc_flags;
uint16_t pc_buflen;
struct piv_fascn *pc_fascn;
uint8_t pc_guid[GUID_LEN];
uint8_t pc_chuuid[GUID_LEN];
uint8_t *pc_expiry;
size_t pc_expiry_len;
uint8_t *pc_orgid;
size_t pc_orgid_len;
uint8_t *pc_duns;
size_t pc_duns_len;
CMS_ContentInfo *pc_sig;
char *pc_guidhex;
};
void
piv_chuid_free(struct piv_chuid *chuid)
{
if (chuid == NULL)
return;
piv_fascn_free(chuid->pc_fascn);
free(chuid->pc_expiry);
free(chuid->pc_orgid);
free(chuid->pc_duns);
CMS_ContentInfo_free(chuid->pc_sig);
free(chuid->pc_guidhex);
free(chuid);
}
struct piv_chuid *
piv_chuid_new(void)
{
struct piv_chuid *chuid;
chuid = calloc(1, sizeof (struct piv_chuid));
if (chuid == NULL)
return (NULL);
return (chuid);
}
errf_t *
piv_chuid_clone(const struct piv_chuid *other, struct piv_chuid **out)
{
struct piv_chuid *chuid = NULL;
uint8_t *buf = NULL;
const uint8_t *p;
size_t len;
errf_t *err;
chuid = calloc(1, sizeof (struct piv_chuid));
if (chuid == NULL)
return (NULL);
chuid->pc_flags = other->pc_flags;
chuid->pc_buflen = other->pc_buflen;
bcopy(other->pc_guid, chuid->pc_guid, sizeof (chuid->pc_guid));
bcopy(other->pc_chuuid, chuid->pc_chuuid, sizeof (chuid->pc_chuuid));
if (other->pc_fascn != NULL) {
chuid->pc_fascn = piv_fascn_clone(other->pc_fascn);
if (chuid->pc_fascn == NULL) {
err = ERRF_NOMEM;
goto out;
}
}
if (other->pc_expiry_len > 0) {
chuid->pc_expiry = malloc(other->pc_expiry_len);
if (chuid->pc_expiry == NULL) {
err = ERRF_NOMEM;
goto out;
}
bcopy(other->pc_expiry, chuid->pc_expiry, other->pc_expiry_len);
chuid->pc_expiry_len = other->pc_expiry_len;
}
if (other->pc_orgid_len > 0) {
chuid->pc_orgid = malloc(other->pc_orgid_len);
if (chuid->pc_orgid == NULL) {
err = ERRF_NOMEM;
goto out;
}
bcopy(other->pc_orgid, chuid->pc_orgid, other->pc_orgid_len);
chuid->pc_orgid_len = other->pc_orgid_len;
}
if (other->pc_duns_len > 0) {
chuid->pc_duns = malloc(other->pc_duns_len);
if (chuid->pc_duns == NULL) {
err = ERRF_NOMEM;
goto out;
}
bcopy(other->pc_duns, chuid->pc_duns, other->pc_duns_len);
chuid->pc_duns_len = other->pc_duns_len;
}
if (other->pc_sig != NULL) {
len = i2d_CMS_ContentInfo(other->pc_sig, &buf);
if (len == 0) {
make_sslerrf(err, "i2d_CMS_ContentInfo", "encoding "
"chuid signature");
goto out;
}
p = buf;
chuid->pc_sig = d2i_CMS_ContentInfo(NULL, &p, len);
if (chuid->pc_sig == NULL) {
make_sslerrf(err, "d2i_CMS_ContentInfo",
"parsing issuer signature in CHUID");
goto out;
}
}
*out = chuid;
chuid = NULL;
err = ERRF_OK;
out:
OPENSSL_free(buf);
piv_chuid_free(chuid);
return (err);
}
boolean_t
piv_chuid_is_expired(const struct piv_chuid *pc)
{
return (B_FALSE);
}
void
piv_chuid_set_random_guid(struct piv_chuid *pc)
{
arc4random_buf(pc->pc_guid, sizeof (pc->pc_guid));
free(pc->pc_guidhex);
pc->pc_guidhex = NULL;
}
void
piv_chuid_set_fascn(struct piv_chuid *pc, const struct piv_fascn *v)
{
piv_fascn_free(pc->pc_fascn);
pc->pc_fascn = piv_fascn_clone(v);
}
void
piv_chuid_set_guid(struct piv_chuid *pc, uint8_t *v)
{
bcopy(v, pc->pc_guid, sizeof (pc->pc_guid));
free(pc->pc_guidhex);
pc->pc_guidhex = NULL;
}
const char *
piv_chuid_get_guidhex(const struct piv_chuid *pc)
{
if (pc->pc_guidhex == NULL) {
struct piv_chuid *pcw = (struct piv_chuid *)pc;
pcw->pc_guidhex = buf_to_hex(pc->pc_guid, sizeof (pc->pc_guid),
B_FALSE);
return (pcw->pc_guidhex);
}
return (pc->pc_guidhex);
}
void
piv_chuid_set_chuuid(struct piv_chuid *pc, uint8_t *v)
{
if (v == NULL) {
bzero(pc->pc_chuuid, sizeof (pc->pc_chuuid));
pc->pc_flags &= ~PIV_CHUID_HAS_CHUUID;
} else {
bcopy(v, pc->pc_chuuid, sizeof (pc->pc_chuuid));
pc->pc_flags |= PIV_CHUID_HAS_CHUUID;
}
}
void
piv_chuid_set_expiry(struct piv_chuid *pc, uint8_t *v, size_t len)
{
free(pc->pc_expiry);
pc->pc_expiry = malloc(len);
bcopy(v, pc->pc_expiry, len);
pc->pc_expiry_len = len;
}
void
piv_chuid_set_expiry_rel(struct piv_chuid *pc, uint sec)
{
char buf[9] = {0};
time_t now;
struct tm *tm;
now = time(NULL);
__CPROVER_assume(now > 0);
__CPROVER_assume(((unsigned long)now + (unsigned long)sec) >= now);
__CPROVER_assume(((unsigned long)now + (unsigned long)sec) < LONG_MAX);
VERIFY3U((unsigned long)now + sec, >=, now);
VERIFY3U((unsigned long)now + sec, <, LONG_MAX);
now = (time_t)((unsigned long)now + sec);
tm = gmtime(&now);
snprintf(buf, sizeof (buf), "%04d%02d%02d", tm->tm_year + 1900,
tm->tm_mon + 1, tm->tm_mday);
piv_chuid_set_expiry(pc, (uint8_t *)buf, strlen(buf));
}
static errf_t *
piv_chuid_write_tbs_tlv(const struct piv_chuid *pc, struct tlv_state *tlv)
{
uint8_t *buf = NULL;
size_t len;
errf_t *err;
if (pc->pc_flags & PIV_CHUID_HAS_BUFLEN) {
tlv_push(tlv, 0xEE);
tlv_write_u16(tlv, pc->pc_buflen);
tlv_pop(tlv);
}
if (pc->pc_fascn != NULL) {
err = piv_fascn_encode(pc->pc_fascn, &buf, &len);
if (err != ERRF_OK) {
err = errf("CHUIDEncodeError", err, "Failed to encode "
"FASC-N in CHUID");
goto out;
}
tlv_push(tlv, 0x30);
tlv_write(tlv, buf, len);
tlv_pop(tlv);
free(buf);
buf = NULL;
}
if (pc->pc_orgid != NULL) {
tlv_push(tlv, 0x32);
tlv_write(tlv, pc->pc_orgid, pc->pc_orgid_len);
tlv_pop(tlv);
}
if (pc->pc_duns != NULL) {
tlv_push(tlv, 0x32);
tlv_write(tlv, pc->pc_duns, pc->pc_duns_len);
tlv_pop(tlv);
}
tlv_push(tlv, 0x34);
tlv_write(tlv, pc->pc_guid, sizeof (pc->pc_guid));
tlv_pop(tlv);
if (pc->pc_expiry != NULL) {
tlv_push(tlv, 0x35);
tlv_write(tlv, pc->pc_expiry, pc->pc_expiry_len);
tlv_pop(tlv);
}
if (pc->pc_flags & PIV_CHUID_HAS_CHUUID) {
tlv_push(tlv, 0x36);
tlv_write(tlv, pc->pc_chuuid, sizeof (pc->pc_chuuid));
tlv_pop(tlv);
}
err = ERRF_OK;
out:
free(buf);
return (err);
}
errf_t *
piv_chuid_tbs(const struct piv_chuid *pc, uint8_t **out, size_t *len)
{
struct tlv_state *tlv = NULL;
errf_t *err;
tlv = tlv_init_write();
if (tlv == NULL) {
err = errfno("tlv_init_write", errno, NULL);
goto out;
}
if ((err = piv_chuid_write_tbs_tlv(pc, tlv)))
goto out;
*len = tlv_len(tlv);
*out = malloc(*len);
if (*out == NULL) {
err = ERRF_NOMEM;
goto out;
}
bcopy(tlv_buf(tlv), *out, *len);
err = ERRF_OK;
out:
tlv_free(tlv);
return (err);
}
errf_t *
piv_chuid_encode(const struct piv_chuid *pc, uint8_t **out, size_t *outlen)
{
struct tlv_state *tlv = NULL;
uint8_t *buf = NULL;
size_t len;
errf_t *err;
tlv = tlv_init_write();
if (tlv == NULL) {
err = errfno("tlv_init_write", errno, NULL);
goto out;
}
if ((err = piv_chuid_write_tbs_tlv(pc, tlv)))
goto out;
if (pc->pc_sig != NULL) {
len = i2d_CMS_ContentInfo(pc->pc_sig, &buf);
if (len == 0) {
make_sslerrf(err, "i2d_CMS_ContentInfo", "encoding "
"CHUID signature");
goto out;
}
tlv_push(tlv, 0x3E);
tlv_write(tlv, buf, len);
tlv_pop(tlv);
} else {
/*
* The signature field is compulsory, so write an empty tag.
*/
tlv_push(tlv, 0x3E);
tlv_pop(tlv);
}
len = tlv_len(tlv);
*out = malloc(len);
if (*out == NULL) {
err = ERRF_NOMEM;
goto out;
}
bcopy(tlv_buf(tlv), *out, len);
*outlen = len;
err = ERRF_OK;
out:
tlv_free(tlv);
free(buf);
return (err);
}
errf_t *
piv_chuid_decode(const uint8_t *data, size_t len, struct piv_chuid **out)
{
struct tlv_state *tlv = NULL;
uint tag;
errf_t *err;
struct piv_chuid *chuid = NULL;
uint8_t *d;
size_t dlen;
const uint8_t *p;
chuid = calloc(1, sizeof (struct piv_chuid));
if (chuid == NULL) {
err = errfno("calloc", errno, NULL);
goto out;
}
tlv = tlv_init(data, 0, len);
if (tlv == NULL) {
err = errfno("tlv_init", errno, NULL);
goto out;
}
while (!tlv_at_end(tlv)) {
if ((err = tlv_read_tag(tlv, &tag)))
goto out;
switch (tag) {
case 0xEE: /* Buffer Length */
err = tlv_read_u16(tlv, &chuid->pc_buflen);
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
chuid->pc_flags |= PIV_CHUID_HAS_BUFLEN;
break;
case 0x30: /* FASC-N */
err = piv_fascn_decode(tlv_ptr(tlv), tlv_rem(tlv),
&chuid->pc_fascn);
if (err)
goto out;
tlv_skip(tlv);
break;
case 0x32: /* Organizational Identifier */
err = tlv_read_alloc(tlv, &chuid->pc_orgid,
&chuid->pc_orgid_len);
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
break;
case 0x33: /* DUNS */
err = tlv_read_alloc(tlv, &chuid->pc_duns,
&chuid->pc_duns_len);
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
break;
case 0x34: /* GUID */
err = tlv_read(tlv, chuid->pc_guid,
sizeof (chuid->pc_guid));
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
break;
case 0x35: /* Expiry */
err = tlv_read_alloc(tlv, &chuid->pc_expiry,
&chuid->pc_expiry_len);
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
break;
case 0x36: /* Cardholder UUID */
err = tlv_read(tlv, chuid->pc_chuuid,
sizeof (chuid->pc_chuuid));
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
chuid->pc_flags |= PIV_CHUID_HAS_CHUUID;
break;
case 0x3D: /* Authentication Key Map (Deprecated) */
tlv_skip(tlv);
break;
case 0x3E: /* Issuer Signature */
err = tlv_read_alloc(tlv, &d, &dlen);
if (err)
goto out;
if ((err = tlv_end(tlv)))
goto out;
if (dlen == 0) {
/* Skip an empty signature */
free(d);
break;
}
p = d;
chuid->pc_sig = d2i_CMS_ContentInfo(NULL, &p, len);
free(d);
if (chuid->pc_sig == NULL) {
make_sslerrf(err, "d2i_CMS_ContentInfo",
"parsing issuer signature in CHUID");
goto out;
}
break;
case 0xFE: /* LRC */
tlv_skip(tlv);
break;
default:
err = tagerrf("CHUID", tag);
goto out;
}
}
*out = chuid;
chuid = NULL;
err = ERRF_OK;
out:
if (err != ERRF_OK)
tlv_abort(tlv);
else
tlv_free(tlv);
piv_chuid_free(chuid);
return (err);
}
const struct piv_fascn *
piv_chuid_get_fascn(const struct piv_chuid *c)
{
return (c->pc_fascn);
}
const uint8_t *
piv_chuid_get_guid(const struct piv_chuid *c)
{
return (c->pc_guid);
}
const uint8_t *
piv_chuid_get_chuuid(const struct piv_chuid *c)
{
if (!(c->pc_flags & PIV_CHUID_HAS_CHUUID))
return (NULL);
return (c->pc_chuuid);
}
const uint8_t *
piv_chuid_get_expiry(const struct piv_chuid *c, size_t *plen)
{
*plen = c->pc_expiry_len;
return (c->pc_expiry);
}
CMS_ContentInfo *
piv_chuid_get_signature(struct piv_chuid *c)
{
return (c->pc_sig);
}
boolean_t
piv_chuid_is_signed(const struct piv_chuid *c)
{
return (c->pc_sig != NULL);
}
#if defined(__CPROVER) && __CPROVER_MAIN == __FILE_piv_chuid_c
uint8_t *
genbinstr(size_t minlen, size_t maxlen, size_t *plen)
{
size_t len, i;
uint8_t *buf;
__CPROVER_assume(len >= minlen);
__CPROVER_assume(len <= maxlen);
buf = malloc(len);
__CPROVER_assume(buf != NULL);
*plen = len;
return (buf);
}
void
chuid_write_proof(void)
{
struct piv_chuid *chuid;
uint8_t *data;
size_t len;
errf_t *err;
chuid = piv_chuid_new();
__CPROVER_assume(chuid != NULL);
piv_chuid_set_random_guid(chuid);
piv_chuid_set_expiry_rel(chuid, 3600*24*365);
data = NULL;
len = 0;
err = piv_chuid_encode(chuid, &data, &len);
__CPROVER_assume(err != ERRF_NOMEM);
assert(err == ERRF_OK);
assert(data != NULL);
assert(len > 0);
piv_chuid_free(chuid);
}
void
chuid_read_proof(void)
{
struct piv_chuid *chuid;
uint8_t *data;
size_t len;
errf_t *err;
chuid = NULL;
data = genbinstr(18, 20, &len);
err = piv_chuid_decode(data, len, &chuid);
__CPROVER_assume(err != ERRF_NOMEM);
__CPROVER_assume(err != ERRF_OK);
assert(chuid == NULL);
data = genbinstr(18, 20, &len);
err = piv_chuid_decode(data, len, &chuid);
__CPROVER_assume(err == ERRF_OK);
assert(chuid != NULL);
assert(piv_chuid_get_guid(chuid) != NULL);
piv_chuid_free(chuid);
}
int
main(int argc, char *argv[])
{
__CPROVER_assume(ERRF_NOMEM != NULL);
chuid_write_proof();
chuid_read_proof();
return (0);
}
#endif