xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/yk-readonly.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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  * Open a YubiKey class device read-only and try to get the basic information
18  * applet through an APDU, which should fail. Try to get the status, which
19  * should succeed, and attempt to power off, which should fail.
20  */
21 
22 #include <err.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <strings.h>
28 #include <unistd.h>
29 #include <errno.h>
30 
31 #include <sys/usb/clients/ccid/uccid.h>
32 
33 static const uint8_t yk_req[] = {
34 	0x00, 0xa4, 0x04, 0x00, 0x07, 0xa0, 0x00, 0x00, 0x05, 0x27, 0x20, 0x01
35 };
36 
37 int
38 main(int argc, char *argv[])
39 {
40 	int fd, ret;
41 	uccid_cmd_icc_modify_t uci;
42 	uccid_cmd_txn_begin_t begin;
43 	uccid_cmd_status_t ucs;
44 	uint8_t buf[UCCID_APDU_SIZE_MAX];
45 
46 	if (argc != 2) {
47 		errx(EXIT_FAILURE, "missing required ccid path");
48 	}
49 
50 	if ((fd = open(argv[1], O_RDONLY)) < 0) {
51 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
52 	}
53 
54 	bzero(&begin, sizeof (begin));
55 	begin.uct_version = UCCID_CURRENT_VERSION;
56 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) == 0) {
57 		errx(EXIT_FAILURE, "didn't fail to issue begin ioctl");
58 	}
59 
60 	if ((ret = write(fd, yk_req, sizeof (yk_req))) != -1) {
61 		errx(EXIT_FAILURE, "didn't fail to write data");
62 	}
63 
64 	if (errno != EBADF) {
65 		err(EXIT_FAILURE, "wrong errno for failed write, "
66 		    "expected EBADF");
67 	}
68 
69 	if ((ret = read(fd, buf, sizeof (buf))) != -1) {
70 		errx(EXIT_FAILURE, "didn't fail to read data");
71 	}
72 
73 	if (errno != EACCES) {
74 		err(EXIT_FAILURE, "wrong errno for failed read, "
75 		    "expected EACCES");
76 	}
77 
78 	/* get card status */
79 	bzero(&ucs, sizeof (ucs));
80 	ucs.ucs_version = UCCID_CURRENT_VERSION;
81 	if ((ret = ioctl(fd, UCCID_CMD_STATUS, &ucs)) != 0) {
82 		err(EXIT_FAILURE, "failed to get status");
83 	}
84 
85 
86 	/* try to power off the card while opened read-only */
87 	bzero(&uci, sizeof (uci));
88 	uci.uci_version = UCCID_CURRENT_VERSION;
89 	uci.uci_action = UCCID_ICC_POWER_OFF;
90 	if ((ret = ioctl(fd, UCCID_CMD_ICC_MODIFY, &uci)) == 0) {
91 		errx(EXIT_FAILURE, "didn't fail to power off ICC");
92 	}
93 
94 	if (errno != EBADF) {
95 		err(EXIT_FAILURE, "wrong errno for failed write, "
96 		    "expected EBADF");
97 	}
98 
99 	return (0);
100 }
101