]> www.average.org Git - pam_pcsc_cr.git/blob - ykneo.c
typecast for snprintf
[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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <alloca.h>
31
32 #include "token.h"
33
34 #define NAMEPFX "YubikeyNEO"
35
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};
39
40 static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
41
42 static int slot = 2;    /* second by default, people tend to leave */
43                         /* the first with factory settings.        */
44
45 static int ykn_parse_option(char *key, char *val)
46 {
47         if (!strcmp(key, "slot")) {
48                 if (!strcmp(val, "1")) {
49                         slot = 1;
50                 } else if (!strcmp(val, "2")) {
51                         slot = 2;
52                 } else {
53                         return -1;
54                 }
55         } else {
56                 return -1;
57         }
58         return 0;
59 }
60
61 static DWORD ykn_check_atr_hb(BYTE *str, DWORD size)
62 {
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;
67 }
68
69 static DWORD ykn_prologue(SCARDHANDLE hCard)
70 {
71         BYTE buf[258];
72         DWORD rsize = sizeof(buf);
73         DWORD rc = SCardBeginTransaction(hCard);
74         if (rc) return rc;
75         rc = SCardTransmit(hCard, &pioSendPci, selcmd, sizeof(selcmd),
76                 NULL, buf, &rsize);
77         if (rc) return rc;
78         if ((buf[rsize-2] == 0x90) && (buf[rsize-1] == 0x00))
79                 return SCARD_S_SUCCESS;
80         else return SCARD_W_CARD_NOT_AUTHENTICATED;
81 }
82
83 static DWORD ykn_getserial(SCARDHANDLE hCard, BYTE *recv, LPDWORD recvsize_p)
84 {
85         DWORD rc;
86         BYTE rbuf[4 + 2];
87         DWORD rsize = sizeof(rbuf);
88         BYTE sbuf[sizeof(yk_cmd) + 1];
89         unsigned int serial;
90
91         memcpy(sbuf, yk_cmd, sizeof(yk_cmd));
92         sbuf[2] = 0x10; /* read serial */
93         sbuf[4] = rsize;
94         rc = SCardTransmit(hCard, &pioSendPci, sbuf, sizeof(sbuf),
95                         NULL, rbuf, &rsize);
96         if (rc) return rc;
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);
101         *recvsize_p = rc;
102         return SCARD_S_SUCCESS;
103 }
104
105 static DWORD ykn_trancieve(SCARDHANDLE hCard,
106         BYTE *send, DWORD sendsize, BYTE *recv, LPDWORD recvsize_p)
107 {
108         DWORD rc;
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,
118                 NULL, rbuf, &rsize);
119         if (rc) return rc;
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;
125 }
126
127 static DWORD ykn_epilogue(SCARDHANDLE hCard)
128 {
129         return SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
130 }
131
132 struct token_interface ykneo_interface = {
133         .name           = "ykneo",
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,
140 };