]> www.average.org Git - pam_pcsc_cr.git/blob - ykneo.c
f9239fd784768066d7b268a44c692062a3dab93b
[pam_pcsc_cr.git] / ykneo.c
1 /*
2 Copyright (c) 2013 Eugene Crosser
3
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.
7
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:
11
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.
16
17     2. Altered source versions must be plainly marked as such, and must
18     not be misrepresented as being the original software.
19
20     3. This notice may not be removed or altered from any source
21     distribution.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <alloca.h>
30
31 #include "token.h"
32
33 #define NAMEPFX "YubikeyNEO"
34
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};
38
39 static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
40
41 static int slot = 2;    /* second by default, people tend to leave */
42                         /* the first with factory settings.        */
43
44 static int ykn_parse_option(char *key, char *val)
45 {
46         if (!strcmp(key, "slot")) {
47                 if (!strcmp(val, "1")) {
48                         slot = 1;
49                 } else if (!strcmp(val, "2")) {
50                         slot = 2;
51                 } else {
52                         return -1;
53                 }
54         } else {
55                 return -1;
56         }
57         return 0;
58 }
59
60 static DWORD ykn_check_atr_hb(BYTE *str, DWORD size)
61 {
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;
66 }
67
68 static DWORD ykn_prologue(SCARDHANDLE hCard)
69 {
70         BYTE buf[258];
71         DWORD rsize = sizeof(buf);
72         DWORD rc = SCardBeginTransaction(hCard);
73         if (rc) return rc;
74         rc = SCardTransmit(hCard, &pioSendPci, selcmd, sizeof(selcmd),
75                 NULL, buf, &rsize);
76         if (rc) return rc;
77         if ((buf[rsize-2] == 0x90) && (buf[rsize-1] == 0x00))
78                 return SCARD_S_SUCCESS;
79         else return SCARD_W_CARD_NOT_AUTHENTICATED;
80 }
81
82 static DWORD ykn_getserial(SCARDHANDLE hCard, BYTE *recv, LPDWORD recvsize_p)
83 {
84         DWORD rc;
85         BYTE rbuf[4 + 2];
86         DWORD rsize = sizeof(rbuf);
87         BYTE sbuf[sizeof(yk_cmd) + 1];
88         unsigned int serial;
89
90         memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
91         sbuf[2] = 0x10; /* read serial */
92         sbuf[4] = rsize;
93         rc = SCardTransmit(hCard, &pioSendPci, sbuf, sizeof(sbuf),
94                         NULL, rbuf, &rsize);
95         if (rc) return rc;
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);
100         *recvsize_p = rc;
101         return SCARD_S_SUCCESS;
102 }
103
104 static DWORD ykn_trancieve(SCARDHANDLE hCard,
105         BYTE *send, DWORD sendsize, BYTE *recv, LPDWORD recvsize_p)
106 {
107         DWORD rc;
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,
117                 NULL, rbuf, &rsize);
118         if (rc) return rc;
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;
124 }
125
126 static DWORD ykn_epilogue(SCARDHANDLE hCard)
127 {
128         return SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
129 }
130
131 struct token_interface ykneo_interface = {
132         .name           = "ykneo",
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,
139 };