xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/yk.c (revision a61ed2ce)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019, Joyent, Inc.
14  */
15 
16 /*
17  * Attempt to open a YubiKey class device and get the basic information applet
18  * through an APDU.
19  */
20 
21 #include <err.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <errno.h>
29 
30 #include <sys/usb/clients/ccid/uccid.h>
31 
32 static const uint8_t yk_req[] = {
33 	0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x05, 0x27, 0x20, 0x01
34 };
35 
36 int
main(int argc,char * argv[])37 main(int argc, char *argv[])
38 {
39 	int fd;
40 	ssize_t ret, i;
41 	uccid_cmd_txn_begin_t begin;
42 	uint8_t buf[UCCID_APDU_SIZE_MAX];
43 
44 	if (argc != 2) {
45 		errx(EXIT_FAILURE, "missing required ccid path");
46 	}
47 
48 	if ((fd = open(argv[1], O_RDWR)) < 0) {
49 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
50 	}
51 
52 	bzero(&begin, sizeof (begin));
53 	begin.uct_version = UCCID_CURRENT_VERSION;
54 
55 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
56 		err(EXIT_FAILURE, "failed to issue begin ioctl");
57 	}
58 
59 	if ((ret = write(fd, yk_req, sizeof (yk_req))) < 0) {
60 		err(EXIT_FAILURE, "failed to write data");
61 	}
62 
63 	if ((ret = read(fd, buf, sizeof (buf))) < 0) {
64 		err(EXIT_FAILURE, "failed to read data");
65 	}
66 
67 	(void) printf("read %d bytes\n", ret);
68 	for (i = 0; i < ret; i++) {
69 		(void) printf("%02x", buf[i]);
70 		if (i == (ret - 1) || (i % 16) == 15) {
71 			(void) printf("\n");
72 		} else {
73 			(void) printf(" ");
74 		}
75 	}
76 
77 	return (0);
78 }
79