2 Copyright (c) 2013 Eugene Crosser
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must
13 not claim that you wrote the original software. If you use this
14 software in a product, an acknowledgment in the product documentation
15 would be appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must
18 not be misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source
33 #define NAMEPFX "YubikeyNEO"
35 static const BYTE selcmd[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0,
36 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
37 static const BYTE yk_cmd[] = {0x00, 0x01, 0xff, 0x00};
39 static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
41 static int slot = 2; /* second by default, people tend to leave */
42 /* the first with factory settings. */
44 static int ykn_parse_option(char *key, char *val)
46 if (!strcmp(key, "slot")) {
47 if (!strcmp(val, "1")) {
49 } else if (!strcmp(val, "2")) {
60 static DWORD ykn_check_atr_hb(BYTE *str, DWORD size)
62 if (size < strlen(NAMEPFX)) return SCARD_W_UNSUPPORTED_CARD;
63 if (memcmp(str, NAMEPFX, strlen(NAMEPFX)))
64 return SCARD_W_UNSUPPORTED_CARD;
65 return SCARD_S_SUCCESS;
68 static DWORD ykn_prologue(SCARDHANDLE hCard)
71 DWORD rsize = sizeof(buf);
72 DWORD rc = SCardBeginTransaction(hCard);
74 rc = SCardTransmit(hCard, &pioSendPci, selcmd, sizeof(selcmd),
77 if ((buf[rsize-2] == 0x90) && (buf[rsize-1] == 0x00))
78 return SCARD_S_SUCCESS;
79 else return SCARD_W_CARD_NOT_AUTHENTICATED;
82 static DWORD ykn_getserial(SCARDHANDLE hCard, BYTE *recv, LPDWORD recvsize_p)
86 DWORD rsize = sizeof(rbuf);
87 BYTE sbuf[sizeof(yk_cmd) + 1];
90 memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
91 sbuf[2] = 0x10; /* read serial */
93 rc = SCardTransmit(hCard, &pioSendPci, sbuf, sizeof(sbuf),
96 if ((rbuf[rsize-2] != 0x90) || (rbuf[rsize-1] != 0x00))
97 return SCARD_W_CARD_NOT_AUTHENTICATED;
98 serial = (rbuf[0]<<24) + (rbuf[1]<<16) + (rbuf[2]<<8) + (rbuf[3]);
99 rc = snprintf(recv, *recvsize_p, "%u", serial);
101 return SCARD_S_SUCCESS;
104 static DWORD ykn_trancieve(SCARDHANDLE hCard,
105 BYTE *send, DWORD sendsize, BYTE *recv, LPDWORD recvsize_p)
108 DWORD rsize = *recvsize_p + 2;
109 BYTE *rbuf = alloca(rsize);
110 BYTE *sbuf = alloca(sendsize + 6);
111 memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
112 sbuf[2] = cr_for_slot[slot];
113 sbuf[sizeof(yk_cmd)] = sendsize;
114 memcpy(sbuf + sizeof(yk_cmd) + 1, send, sendsize);
115 sbuf[sendsize + 5] = rsize;
116 rc = SCardTransmit(hCard, &pioSendPci, sbuf, sendsize + 6,
119 if ((rbuf[rsize-2] != 0x90) || (rbuf[rsize-1] != 0x00))
120 return SCARD_W_CARD_NOT_AUTHENTICATED;
121 memcpy(recv, rbuf, rsize - 2);
122 *recvsize_p = rsize - 2;
123 return SCARD_S_SUCCESS;
126 static DWORD ykn_epilogue(SCARDHANDLE hCard)
128 return SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
131 struct token_interface ykneo_interface = {
133 .parse_option = ykn_parse_option,
134 .check_atr_hb = ykn_check_atr_hb,
135 .prologue = ykn_prologue,
136 .getserial = ykn_getserial,
137 .trancieve = ykn_trancieve,
138 .epilogue = ykn_epilogue,