-
Notifications
You must be signed in to change notification settings - Fork 27
/
piv.c
7169 lines (6322 loc) · 171 KB
/
piv.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <alex.wilson@joyent.com>
*/
/*
* 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 "debug.h"
#if defined(__APPLE__)
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <wintypes.h>
#include <winscard.h>
#endif
#include <sys/mman.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <zlib.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 <openssl/evp.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"
#define PIV_MAX_CERT_LEN 16384
const uint8_t AID_PIV[] = {
0xA0, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00
};
boolean_t piv_full_apdu_debug = B_FALSE;
struct apdu {
enum iso_class a_cls;
enum iso_ins a_ins;
uint8_t a_p1;
uint8_t a_p2;
uint8_t a_le;
struct apdubuf a_cmd;
uint16_t a_sw;
struct apdubuf a_reply;
};
/* Tags used in the GENERAL AUTHENTICATE command. */
enum gen_auth_tag {
GA_TAG_WITNESS = 0x80,
GA_TAG_CHALLENGE = 0x81,
GA_TAG_RESPONSE = 0x82,
GA_TAG_EXP = 0x85,
};
struct piv_slot {
/*
* Links to the next member of the slot list hanging off a token at
* ->pt_slots. There's only one slots list per token.
*/
struct piv_slot *ps_next;
enum piv_slotid ps_slot;
enum piv_alg ps_alg;
X509 *ps_x509;
const char *ps_subj;
const char *ps_issuer;
const char *ps_cert_ser_hex;
struct sshkey *ps_pubkey;
enum piv_slot_auth ps_auth;
boolean_t ps_got_metadata;
};
struct piv_token {
struct piv_ctx *pt_ctx;
/* piv_ctx list of all tokens created. */
struct piv_token *pt_lib_prev;
struct piv_token *pt_lib_next;
/*
* Next in the enumeration. If this token was returned by piv_find()
* then this will always be NULL. piv_enumerate() can return a linked
* list of piv_tokens though.
*/
struct piv_token *pt_next;
/* PCSC parameters */
const char *pt_rdrname;
SCARDHANDLE pt_cardhdl;
DWORD pt_proto;
SCARD_IO_REQUEST pt_sendpci;
/*
* Are we in a transaction right now?
*/
boolean_t pt_intxn;
/*
* Do we need to reset at the end of this txn?
*/
boolean_t pt_reset;
/*
* Do we have an auth'd PIN that we should try to reset at the end of
* this txn?
*/
enum piv_pin pt_used_pin;
/*
* Our GUID. This can either be the GUID from our CHUID file, or if
* we don't have one, we can synthesise this from other data.
*/
uint8_t pt_guid[GUID_LEN];
/* We use this to cache a cstring hex version of pt_guid */
char *pt_guidhex;
/* Contents of the CHUID file. */
struct piv_chuid *pt_chuid;
/*
* Array of supported algorithms, if we got any in the answer to
* SELECT. Lots of PIV cards don't supply these.
*/
enum piv_alg pt_algs[PIV_RTS_MAX_ALGS];
size_t pt_alg_count;
/*
* If we are using any of the retired key history slots (82-95), then
* these should be set (they're from the Key History Object). The
* number of slots in use is oncard + offcard.
*/
uint8_t pt_hist_oncard;
uint8_t pt_hist_offcard;
char *pt_hist_url;
/* Extra into in response to SELECT */
char *pt_app_label;
char *pt_app_uri;
/* Our preferred authentication method */
enum piv_pin pt_auth;
/* Supported authentication methods */
boolean_t pt_pin_global;
boolean_t pt_pin_app;
boolean_t pt_occ;
boolean_t pt_vci;
struct piv_slot *pt_slots;
struct piv_slot *pt_last_slot;
boolean_t pt_did_read_all;
/* YubicoPIV specific stuff */
boolean_t pt_ykpiv; /* Supports YubicoPIV? */
uint8_t pt_ykver[3]; /* YK FW version number */
boolean_t pt_ykserial_valid; /* YubiKey serial # only on YK5 */
uint32_t pt_ykserial;
};
enum piv_pinfo_kv_type {
PIV_PINFO_KV_UINT,
PIV_PINFO_KV_STRING,
PIV_PINFO_KV_BOOL,
PIV_PINFO_KV_DATA
};
struct piv_pinfo_kv {
struct piv_pinfo_kv *ppk_next;
struct piv_pinfo_kv *ppk_prev;
char *ppk_name;
enum piv_pinfo_kv_type ppk_type;
union {
uint ppk_uint;
char *ppk_string;
struct {
uint8_t *ppk_data;
size_t ppk_len;
};
};
};
struct piv_pinfo {
char *pp_name;
char *pp_affiliation;
char *pp_expiry;
char *pp_serial;
char *pp_issuer;
char *pp_org_1;
char *pp_org_2;
uint8_t *pp_yk_admin;
size_t pp_yk_admin_len;
struct piv_pinfo_kv *pp_kv;
};
struct piv_ctx {
boolean_t pc_scard_init;
boolean_t pc_scard_owned;
boolean_t pc_scard_nordr;
DWORD pc_scard_scope;
SCARDCONTEXT pc_scard;
struct piv_token *pc_tokens;
};
struct piv_ctx *
piv_open(void)
{
struct piv_ctx *ctx;
ctx = calloc(1, sizeof (*ctx));
return (ctx);
}
static void piv_release_one(struct piv_token *pk);
void
piv_close(struct piv_ctx *ctx)
{
struct piv_token *pt, *npt;
if (ctx == NULL)
return;
if (ctx->pc_scard_owned)
SCardReleaseContext(ctx->pc_scard);
for (pt = ctx->pc_tokens; pt != NULL; pt = npt) {
npt = pt->pt_lib_next;
piv_release_one(pt);
}
free(ctx);
}
void
piv_set_context(struct piv_ctx *ctx, SCARDCONTEXT sctx)
{
VERIFY(!ctx->pc_scard_init);
ctx->pc_scard_init = B_TRUE;
ctx->pc_scard_owned = B_FALSE;
ctx->pc_scard_nordr = B_FALSE;
ctx->pc_scard = sctx;
}
errf_t *
piv_establish_context(struct piv_ctx *ctx, DWORD scope)
{
DWORD rv;
VERIFY(!ctx->pc_scard_init);
rv = SCardEstablishContext(scope, NULL, NULL, &ctx->pc_scard);
switch (rv) {
case SCARD_S_SUCCESS:
ctx->pc_scard_init = B_TRUE;
ctx->pc_scard_owned = B_TRUE;
ctx->pc_scard_nordr = B_FALSE;
return (ERRF_OK);
case SCARD_E_NO_READERS_AVAILABLE:
ctx->pc_scard_scope = scope;
ctx->pc_scard_nordr = B_TRUE;
return (ERRF_OK);
case SCARD_E_NO_SERVICE:
#if defined(SCARD_E_SERVICE_STOPPED)
case SCARD_E_SERVICE_STOPPED: /* This is a pcsclite-ism */
#endif
return (errf("ServiceError",
pcscerrf("SCardEstablishContext", rv),
"PC/SC system service/daemon not available"));
default:
return (pcscerrf("SCardEstablishContext", rv));
}
}
/* Helper to dump out APDU data */
static inline void
debug_dump(errf_t *err, struct apdu *apdu)
{
bunyan_log(BNY_DEBUG, "APDU parsing error",
"data", BNY_BIN_HEX, apdu->a_reply.b_data + apdu->a_reply.b_offset,
apdu->a_reply.b_len,
"error", BNY_ERF, err, NULL);
}
static const char *
sw_to_name(enum iso_sw sw)
{
switch (sw) {
case SW_NO_ERROR:
return ("NO_ERROR");
case SW_FUNC_NOT_SUPPORTED:
return ("FUNC_NOT_SUPPORTED");
case SW_CONDITIONS_NOT_SATISFIED:
return ("CONDITIONS_NOT_SATISFIED");
case SW_SECURITY_STATUS_NOT_SATISFIED:
return ("SECURITY_STATUS_NOT_SATISFIED");
case SW_WARNING_EOF:
return ("WARNING_EOF");
case SW_FILE_NOT_FOUND:
return ("FILE_NOT_FOUND");
case SW_INCORRECT_P1P2:
return ("INCORRECT_P1P2");
case SW_WRONG_DATA:
return ("WRONG_DATA");
case SW_OUT_OF_MEMORY:
return ("OUT_OF_MEMORY");
case SW_WRONG_LENGTH:
return ("WRONG_LENGTH");
case SW_INS_NOT_SUP:
return ("INS_NOT_SUPPORTED");
case SW_FILE_INVALID:
return ("FILE_INVALID");
case SW_INVALID_KEY_REF:
return ("INVALID_KEY_REF");
default:
/* FALL THROUGH */
(void)0;
}
if ((sw & 0xFF00) == SW_BYTES_REMAINING_00)
return ("BYTES_REMAINING");
else if ((sw & 0xFF00) == SW_CORRECT_LE_00)
return ("CORRECT_LE");
else if ((sw & 0xFFF0) == SW_INCORRECT_PIN)
return ("INCORRECT_PIN");
else if ((sw & 0xFF00) == SW_WARNING_NO_CHANGE_00)
return ("WARNING_NO_CHANGE");
else if ((sw & 0xFF00) == SW_WARNING_00)
return ("WARNING_UNKNOWN");
return ("UNKNOWN");
}
const char *
piv_token_rdrname(const struct piv_token *token)
{
return (token->pt_rdrname);
}
boolean_t
piv_token_in_txn(const struct piv_token *token)
{
return (token->pt_intxn);
}
const struct piv_fascn *
piv_token_fascn(const struct piv_token *token)
{
if (token->pt_chuid == NULL)
return (NULL);
return (piv_chuid_get_fascn(token->pt_chuid));
}
const uint8_t *
piv_token_guid(const struct piv_token *token)
{
return (token->pt_guid);
}
const char *
piv_token_guid_hex(const struct piv_token *token)
{
if (token->pt_guidhex == NULL) {
struct piv_token *tkwrite = (struct piv_token *)token;
tkwrite->pt_guidhex = buf_to_hex(token->pt_guid,
sizeof (token->pt_guid), B_FALSE);
}
return (token->pt_guidhex);
}
size_t
piv_token_nalgs(const struct piv_token *token)
{
return (token->pt_alg_count);
}
enum piv_alg
piv_token_alg(const struct piv_token *token, size_t idx)
{
VERIFY3U(idx, <, token->pt_alg_count);
return (token->pt_algs[idx]);
}
boolean_t
piv_token_has_chuid(const struct piv_token *token)
{
return (token->pt_chuid != NULL);
}
boolean_t
piv_token_has_signed_chuid(const struct piv_token *token)
{
if (token->pt_chuid == NULL)
return (B_FALSE);
return (piv_chuid_is_signed(token->pt_chuid));
}
enum piv_pin
piv_token_default_auth(const struct piv_token *token)
{
return (token->pt_auth);
}
boolean_t
piv_token_has_auth(const struct piv_token *token, enum piv_pin auth)
{
switch (auth) {
case PIV_PIN:
return (token->pt_pin_app);
case PIV_GLOBAL_PIN:
return (token->pt_pin_global);
case PIV_PUK:
return (B_TRUE);
case PIV_OCC:
return (token->pt_occ);
default:
return (B_FALSE);
}
}
boolean_t
piv_token_has_vci(const struct piv_token *token)
{
return (token->pt_vci);
}
uint
piv_token_keyhistory_oncard(const struct piv_token *token)
{
return (token->pt_hist_oncard);
}
uint
piv_token_keyhistory_offcard(const struct piv_token *token)
{
return (token->pt_hist_offcard);
}
const char *
piv_token_offcard_url(const struct piv_token *token)
{
return (token->pt_hist_url);
}
const char *
piv_token_app_label(const struct piv_token *token)
{
return (token->pt_app_label);
}
const char *
piv_token_app_uri(const struct piv_token *token)
{
return (token->pt_app_uri);
}
boolean_t
piv_token_is_ykpiv(const struct piv_token *token)
{
return (token->pt_ykpiv);
}
const uint8_t *
ykpiv_token_version(const struct piv_token *token)
{
VERIFY(token->pt_ykpiv);
return (token->pt_ykver);
}
int
ykpiv_version_compare(const struct piv_token *token, uint8_t major,
uint8_t minor, uint8_t patch)
{
VERIFY(token->pt_ykpiv);
if (token->pt_ykver[0] < major)
return (-1);
if (token->pt_ykver[0] > major)
return (1);
if (token->pt_ykver[1] < minor)
return (-1);
if (token->pt_ykver[1] > minor)
return (1);
if (token->pt_ykver[2] < patch)
return (-1);
if (token->pt_ykver[2] > patch)
return (1);
return (0);
}
boolean_t
ykpiv_token_has_serial(const struct piv_token *token)
{
VERIFY(token->pt_ykpiv);
return (token->pt_ykserial_valid);
}
uint32_t
ykpiv_token_serial(const struct piv_token *token)
{
VERIFY(token->pt_ykpiv);
VERIFY(token->pt_ykserial_valid);
return (token->pt_ykserial);
}
struct piv_token *
piv_token_next(struct piv_token *token)
{
return (token->pt_next);
}
errf_t *
piv_auth_key(struct piv_token *tk, struct piv_slot *slot, struct sshkey *pubkey)
{
errf_t *err;
int rv;
uint8_t *chal = NULL, *sig = NULL;
size_t challen, siglen;
enum sshdigest_types hashalg;
struct sshbuf *b = NULL;
VERIFY(tk->pt_intxn);
/*
* First check that the key on the slot is at least claiming to be
* the same
*/
if (sshkey_equal_public(pubkey, slot->ps_pubkey) == 0) {
err = errf("KeysNotEqualError", NULL,
"Given public key and slot's public key do not match");
return (errf("KeyAuthError", err, "Failed to authenticate key "
"in slot %02x of PIV device '%s'", slot->ps_slot,
tk->pt_rdrname));
}
/*
* Now we generate a random challenge value and have the card sign it.
* 64 bytes is probably overkill, but more doesn't hurt (we normally
* are just sending a hash anyway).
*/
challen = 64;
chal = calloc(1, challen);
VERIFY3P(chal, !=, NULL);
arc4random_buf(chal, challen);
hashalg = 0;
err = piv_sign(tk, slot, chal, challen, &hashalg, &sig, &siglen);
if (err)
goto out;
b = sshbuf_new();
VERIFY3P(b, !=, NULL);
/* Convert it to SSH signature format so we can use sshkey_verify */
rv = sshkey_sig_from_asn1(pubkey, hashalg, sig, siglen, b);
if (rv != 0) {
err = errf("NotSupportedError",
ssherrf("sshkey_sig_from_asn1", rv),
"PIV device '%s' returned an unsupported signature format",
tk->pt_rdrname);
goto out;
}
rv = sshkey_verify(pubkey, sshbuf_ptr(b), sshbuf_len(b),
chal, challen, NULL, 0, NULL);
if (rv != 0) {
err = errf("KeyAuthError", ssherrf("sshkey_verify", rv),
"Failed to authenticate key in slot %02x of PIV "
"device '%s'", slot->ps_slot, tk->pt_rdrname);
goto out;
}
out:
sshbuf_free(b);
freezero(chal, challen);
freezero(sig, siglen);
return (err);
}
/*
* Reads the PIV applet version from a YubiKey, or YubiKey-compatible device
* (like one running PivApplet).
*
* We use the fact that a device responds to this version command to indicate
* whether we should bother trying to use any other Yubico extensions to the
* PIV interface. If the device doesn't respond to "GET VERSION" we assume it
* supports no other extensions.
*
* This command is documented in [yubico-piv]
*/
static errf_t *
ykpiv_get_version(struct piv_token *pk)
{
errf_t *err;
struct apdu *apdu;
VERIFY(pk->pt_intxn == B_TRUE);
apdu = piv_apdu_make(CLA_ISO, INS_GET_VER, 0x00, 0x00);
err = piv_apdu_transceive_chain(pk, apdu);
if (err) {
err = ioerrf(err, pk->pt_rdrname);
bunyan_log(BNY_WARN, "ykpiv_get_version.transceive_apdu failed",
"error", BNY_ERF, err, NULL);
piv_apdu_free(apdu);
return (err);
}
if (apdu->a_sw == SW_NO_ERROR) {
const uint8_t *reply =
&apdu->a_reply.b_data[apdu->a_reply.b_offset];
if (apdu->a_reply.b_len < 3) {
piv_apdu_free(apdu);
err = notsuperrf(NULL, pk->pt_rdrname, "YubicoPIV");
return (err);
}
pk->pt_ykpiv = B_TRUE;
bcopy(reply, pk->pt_ykver, 3);
err = NULL;
} else {
err = notsuperrf(swerrf("INS_YK_GET_VER", apdu->a_sw),
pk->pt_rdrname, "YubicoPIV");
}
piv_apdu_free(apdu);
return (err);
}
/*
* Reads the serial number from a YubiKey. This is only available in YubicoPIV
* applet version 5.0.0 or later.
*
* This command is undocumented at present, but can be found used by the
* official yubico tools (which are open source). The official clients use
* require a response to this command in some circumstances before they will
* operate on a device.
*/
static errf_t *
ykpiv_read_serial(struct piv_token *pt)
{
errf_t *err;
struct apdu *apdu;
VERIFY(pt->pt_intxn);
apdu = piv_apdu_make(CLA_ISO, INS_GET_SERIAL, 0x00, 0x00);
err = piv_apdu_transceive_chain(pt, apdu);
if (err) {
err = ioerrf(err, pt->pt_rdrname);
bunyan_log(BNY_WARN, "ykpiv_read_serial.transceive_apdu failed",
"error", BNY_ERF, err, NULL);
piv_apdu_free(apdu);
return (err);
}
if (apdu->a_sw == SW_NO_ERROR) {
const uint8_t *reply =
&apdu->a_reply.b_data[apdu->a_reply.b_offset];
if (apdu->a_reply.b_len < 4) {
piv_apdu_free(apdu);
err = notsuperrf(NULL, pt->pt_rdrname, "YubicoPIV v5");
return (err);
}
pt->pt_ykserial_valid = B_TRUE;
pt->pt_ykserial = reply[3] | (reply[2] << 8) |
(reply[1] << 16) | (reply[0] << 24);
err = NULL;
} else {
err = notsuperrf(swerrf("INS_YK_GET_SERIAL", apdu->a_sw),
pt->pt_rdrname, "YubicoPIV v5");
}
piv_apdu_free(apdu);
return (err);
}
/*
* Reads and parses the PIV Discovery Object.
* [piv] 800-73-4 part 1 appendix A (table 18)
*/
static errf_t *
piv_read_discov(struct piv_token *pk)
{
errf_t *err;
struct apdu *apdu;
struct tlv_state *tlv;
uint tag, policy;
VERIFY(pk->pt_intxn == B_TRUE);
tlv = tlv_init_write();
tlv_push(tlv, 0x5C);
tlv_write_u8to32(tlv, PIV_TAG_DISCOV);
tlv_pop(tlv);
apdu = piv_apdu_make(CLA_ISO, INS_GET_DATA, 0x3F, 0xFF);
apdu->a_cmd.b_data = tlv_buf(tlv);
apdu->a_cmd.b_len = tlv_len(tlv);
err = piv_apdu_transceive_chain(pk, apdu);
if (err) {
err = ioerrf(err, pk->pt_rdrname);
bunyan_log(BNY_WARN, "piv_read_chuid.transceive_apdu failed",
"error", BNY_ERF, err, NULL);
goto out;
}
tlv_free(tlv);
tlv = NULL;
if (apdu->a_sw == SW_NO_ERROR ||
(apdu->a_sw & 0xFF00) == SW_WARNING_NO_CHANGE_00 ||
(apdu->a_sw & 0xFF00) == SW_WARNING_00) {
tlv = tlv_init(apdu->a_reply.b_data, apdu->a_reply.b_offset,
apdu->a_reply.b_len);
if ((err = tlv_read_tag(tlv, &tag)))
goto invdata;
if (tag != 0x7E) {
err = tagerrf("INS_GET_DATA(DISCOV)", tag);
goto invdata;
}
while (!tlv_at_end(tlv)) {
if ((err = tlv_read_tag(tlv, &tag)))
goto invdata;
bunyan_log(BNY_TRACE, "reading discov tlv tag",
"tag", BNY_UINT, (uint)tag, NULL);
switch (tag) {
case 0x4F: /* AID */
if (tlv_rem(tlv) > sizeof (AID_PIV) ||
bcmp(AID_PIV, tlv_ptr(tlv), tlv_rem(tlv)) != 0) {
err = invderrf(errf("PIVDataError", NULL,
"PIV discovery AID tag contained "
"incorrect AID"), pk->pt_rdrname);
goto invdata;
}
tlv_skip(tlv);
break;
case 0x5F2F: /* PIN and OCC policy */
/* See [piv] 800-73-4 part 1, section 3.3.2 */
if ((err = tlv_read_u8to32(tlv, &policy)))
goto invdata;
bunyan_log(BNY_TRACE, "policy in discov",
"policy", BNY_UINT, policy, NULL);
if ((policy & 0x4000))
pk->pt_pin_app = B_TRUE;
if ((policy & 0x2000))
pk->pt_pin_global = B_TRUE;
if ((policy & 0x1000))
pk->pt_occ = B_TRUE;
if ((policy & 0x0800))
pk->pt_vci = B_TRUE;
if (pk->pt_pin_app)
pk->pt_auth = PIV_PIN;
else if (pk->pt_pin_global)
pk->pt_auth = PIV_GLOBAL_PIN;
else if (pk->pt_occ)
pk->pt_auth = PIV_OCC;
if ((policy & 0xFF) == 0x10) {
pk->pt_auth = PIV_PIN;
}
if ((policy & 0xFF) == 0x20 &&
pk->pt_pin_global) {
pk->pt_auth = PIV_GLOBAL_PIN;
}
if ((err = tlv_end(tlv)))
goto invdata;
break;
default:
err = tagerrf("INS_GET_DATA(DISCOV)", tag);
goto invdata;
}
}
if ((err = tlv_end(tlv)))
goto invdata;
err = NULL;
} else if (apdu->a_sw == SW_FILE_NOT_FOUND ||
apdu->a_sw == SW_WRONG_DATA) {
err = errf("NotFoundError", swerrf("INS_GET_DATA", apdu->a_sw),
"PIV discovery object was not found on device '%s'",
pk->pt_rdrname);
} else if (apdu->a_sw == SW_FUNC_NOT_SUPPORTED) {
err = notsuperrf(swerrf("INS_GET_DATA", apdu->a_sw),
pk->pt_rdrname, "PIV discovery object");
} else {
err = swerrf("INS_GET_DATA", apdu->a_sw);
bunyan_log(BNY_DEBUG, "unexpected card error",
"reader", BNY_STRING, pk->pt_rdrname,
"error", BNY_ERF, err, NULL);
}
out:
tlv_free(tlv);
piv_apdu_free(apdu);
return (err);
invdata:
tlv_abort(tlv);
err = invderrf(err, pk->pt_rdrname);
debug_dump(err, apdu);
goto out;
}
/*
* Reads and parses the PIV Key History Object.
* [piv] 800-73-4 part 1 section 3.3.3
* [piv] 800-73-4 part 1 appendix A (table 19)
*/
static errf_t *
piv_read_keyhist(struct piv_token *pk)
{
errf_t *rv;
struct apdu *apdu;
struct tlv_state *tlv;
uint tag, uval;
VERIFY(pk->pt_intxn == B_TRUE);
tlv = tlv_init_write();
tlv_push(tlv, 0x5C);
tlv_write_u8to32(tlv, PIV_TAG_KEYHIST);
tlv_pop(tlv);
apdu = piv_apdu_make(CLA_ISO, INS_GET_DATA, 0x3F, 0xFF);
apdu->a_cmd.b_data = tlv_buf(tlv);
apdu->a_cmd.b_len = tlv_len(tlv);
rv = piv_apdu_transceive_chain(pk, apdu);
if (rv) {
rv = ioerrf(rv, pk->pt_rdrname);
bunyan_log(BNY_WARN, "piv_read_chuid.transceive_apdu failed",
"error", BNY_ERF, rv, NULL);
goto out;
}
tlv_free(tlv);
tlv = NULL;
if (apdu->a_sw == SW_NO_ERROR ||
(apdu->a_sw & 0xFF00) == SW_WARNING_NO_CHANGE_00 ||
(apdu->a_sw & 0xFF00) == SW_WARNING_00) {
if (apdu->a_reply.b_len < 1) {
rv = errf("EmptyDataError", NULL,
"Card replied with empty APDU to "
"INS_GET_DATA(KEYHIST)");
goto invdata;
}
tlv = tlv_init(apdu->a_reply.b_data, apdu->a_reply.b_offset,
apdu->a_reply.b_len);
if ((rv = tlv_read_tag(tlv, &tag)))
goto invdata;
if (tag != 0x53) {
rv = tagerrf("INS_GET_DATA(KEYHIST)", tag);
goto invdata;
}
while (!tlv_at_end(tlv)) {
if ((rv = tlv_read_tag(tlv, &tag)))
goto invdata;
bunyan_log(BNY_TRACE, "reading keyhist tlv tag",
"tag", BNY_UINT, (uint)tag, NULL);
switch (tag) {
case 0xC1: /* # keys with on-card certs */
if ((rv = tlv_read_u8to32(tlv, &uval)))
goto invdata;
pk->pt_hist_oncard = uval;
if ((rv = tlv_end(tlv)))
goto invdata;
break;
case 0xC2: /* # keys with off-card certs */
if ((rv = tlv_read_u8to32(tlv, &uval)))
goto invdata;
pk->pt_hist_offcard = uval;
if ((rv = tlv_end(tlv)))
goto invdata;
break;
case 0xF3: /* URL for off-card certs */
rv = tlv_read_string(tlv, &pk->pt_hist_url);
if (rv != NULL)
goto invdata;
if ((rv = tlv_end(tlv)))
goto invdata;
break;
case 0xFE: /* CRC */
tlv_skip(tlv);
break;
default:
rv = tagerrf("INS_GET_DATA(KEYHIST)", tag);
goto invdata;
}
}
if ((rv = tlv_end(tlv)))
goto invdata;
rv = NULL;
} else if (apdu->a_sw == SW_FILE_NOT_FOUND ||
apdu->a_sw == SW_WRONG_DATA) {
rv = errf("NotFoundError", swerrf("INS_GET_DATA", apdu->a_sw),
"PIV key history object not found on device '%s'",
pk->pt_rdrname);
} else if (apdu->a_sw == SW_FUNC_NOT_SUPPORTED) {
rv = notsuperrf(swerrf("INS_GET_DATA", apdu->a_sw),
pk->pt_rdrname, "PIV key history object");
} else {
rv = swerrf("INS_GET_DATA", apdu->a_sw);
bunyan_log(BNY_DEBUG, "unexpected card error",
"reader", BNY_STRING, pk->pt_rdrname,
"error", BNY_ERF, rv, NULL);
}
out:
tlv_free(tlv);
piv_apdu_free(apdu);
return (rv);
invdata:
if (tlv != NULL)
tlv_abort(tlv);
rv = invderrf(rv, pk->pt_rdrname);
debug_dump(rv, apdu);
goto out;
}
/*
* Reads and parses the PIV Card Holder Unique Identifier Object.
* [piv] 800-73-4 part 1 section 3.1.2
* [piv] 800-73-4 part 1 appendix A (table 9)
*/
static errf_t *
piv_read_chuid(struct piv_token *pk)
{
errf_t *err;
struct apdu *apdu;
struct tlv_state *tlv;
uint tag, i;
VERIFY(pk->pt_intxn == B_TRUE);
tlv = tlv_init_write();
tlv_push(tlv, 0x5C);
tlv_write_u8to32(tlv, PIV_TAG_CHUID);
tlv_pop(tlv);
bunyan_log(BNY_DEBUG, "reading CHUID file", NULL);
apdu = piv_apdu_make(CLA_ISO, INS_GET_DATA, 0x3F, 0xFF);
apdu->a_cmd.b_data = tlv_buf(tlv);
apdu->a_cmd.b_len = tlv_len(tlv);
err = piv_apdu_transceive_chain(pk, apdu);
if (err) {
err = ioerrf(err, pk->pt_rdrname);
bunyan_log(BNY_WARN, "transceive_apdu failed",
"error", BNY_ERF, err, NULL);
goto out;
}
tlv_free(tlv);
tlv = NULL;