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
34 #define NAMEPFX "YubikeyNEO"
36 static const BYTE selcmd[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0,
37 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
38 static const BYTE yk_cmd[] = {0x00, 0x01, 0xff, 0x00};
40 static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
42 static int slot = 2; /* second by default, people tend to leave */
43 /* the first with factory settings. */
45 static int ykn_parse_option(char *key, char *val)
47 if (!strcmp(key, "slot")) {
48 if (!strcmp(val, "1")) {
50 } else if (!strcmp(val, "2")) {
61 static DWORD ykn_check_atr_hb(BYTE *str, DWORD size)
63 if (size < strlen(NAMEPFX)) return SCARD_W_UNSUPPORTED_CARD;
64 if (memcmp(str, NAMEPFX, strlen(NAMEPFX)))
65 return SCARD_W_UNSUPPORTED_CARD;
66 return SCARD_S_SUCCESS;
69 static DWORD ykn_prologue(SCARDHANDLE hCard)
72 DWORD rsize = sizeof(buf);
73 DWORD rc = SCardBeginTransaction(hCard);
75 rc = SCardTransmit(hCard, &pioSendPci, selcmd, sizeof(selcmd),
78 if ((buf[rsize-2] == 0x90) && (buf[rsize-1] == 0x00))
79 return SCARD_S_SUCCESS;
80 else return SCARD_W_CARD_NOT_AUTHENTICATED;
83 static DWORD ykn_getserial(SCARDHANDLE hCard, BYTE *recv, LPDWORD recvsize_p)
87 DWORD rsize = sizeof(rbuf);
88 BYTE sbuf[sizeof(yk_cmd) + 1];
91 memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
92 sbuf[2] = 0x10; /* read serial */
94 rc = SCardTransmit(hCard, &pioSendPci, sbuf, sizeof(sbuf),
97 if ((rbuf[rsize-2] != 0x90) || (rbuf[rsize-1] != 0x00))
98 return SCARD_W_CARD_NOT_AUTHENTICATED;
99 serial = (rbuf[0]<<24) + (rbuf[1]<<16) + (rbuf[2]<<8) + (rbuf[3]);
100 rc = snprintf((char*)recv, *recvsize_p, "%u", serial);
102 return SCARD_S_SUCCESS;
105 static DWORD ykn_trancieve(SCARDHANDLE hCard,
106 BYTE *send, DWORD sendsize, BYTE *recv, LPDWORD recvsize_p)
109 DWORD rsize = *recvsize_p + 2;
110 BYTE *rbuf = alloca(rsize);
111 BYTE *sbuf = alloca(sendsize + 6);
112 memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
113 sbuf[2] = cr_for_slot[slot];
114 sbuf[sizeof(yk_cmd)] = sendsize;
115 memcpy(sbuf + sizeof(yk_cmd) + 1, send, sendsize);
116 sbuf[sendsize + 5] = rsize;
117 rc = SCardTransmit(hCard, &pioSendPci, sbuf, sendsize + 6,
120 if ((rbuf[rsize-2] != 0x90) || (rbuf[rsize-1] != 0x00))
121 return SCARD_W_CARD_NOT_AUTHENTICATED;
122 memcpy(recv, rbuf, rsize - 2);
123 *recvsize_p = rsize - 2;
124 return SCARD_S_SUCCESS;
127 static DWORD ykn_epilogue(SCARDHANDLE hCard)
129 return SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
132 struct token_interface ykneo_interface = {
134 .parse_option = ykn_parse_option,
135 .check_atr_hb = ykn_check_atr_hb,
136 .prologue = ykn_prologue,
137 .getserial = ykn_getserial,
138 .trancieve = ykn_trancieve,
139 .epilogue = ykn_epilogue,