9fbe87eb777477c110a0daa78f7394844336bccd
[pam_pcsc_cr.git] / ykneo.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <stdlib.h>
5 #include <string.h>
6 #include <alloca.h>
7
8 #include "token.h"
9
10 #define NAMEPFX "YubikeyNEO"
11
12 static const BYTE selcmd[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0,
13                                 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
14 static const BYTE cr_cmd[] = {0x00, 0x01, 0xff, 0x00};
15
16 static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
17
18 static int slot = 2;    /* second by default, people tend to leave */
19                         /* the first with factory settings.        */
20
21 static int ykn_parse_option(char *key, char *val)
22 {
23         if (!strcmp(key, "slot")) {
24                 if (!strcmp(val, "1")) {
25                         slot = 1;
26                 } else if (!strcmp(val, "2")) {
27                         slot = 2;
28                 } else {
29                         return -1;
30                 }
31         } else {
32                 return -1;
33         }
34         return 0;
35 }
36
37 static DWORD ykn_check_atr_hb(BYTE *str, DWORD size)
38 {
39         if (size < strlen(NAMEPFX)) return SCARD_W_UNSUPPORTED_CARD;
40         if (memcmp(str, NAMEPFX, strlen(NAMEPFX)))
41                 return SCARD_W_UNSUPPORTED_CARD;
42         return SCARD_S_SUCCESS;
43 }
44
45 static DWORD ykn_prologue(SCARDHANDLE hCard)
46 {
47         BYTE buf[258];
48         DWORD rsize = sizeof(buf);
49         DWORD rc = SCardBeginTransaction(hCard);
50         if (rc) return rc;
51         rc = SCardTransmit(hCard, &pioSendPci, selcmd, sizeof(selcmd),
52                 NULL, buf, &rsize);
53         if (rc) return rc;
54         if ((buf[rsize-2] == 0x90) && (buf[rsize-1] == 0x00))
55                 return SCARD_S_SUCCESS;
56         else return SCARD_W_CARD_NOT_AUTHENTICATED;
57 }
58
59 static DWORD ykn_trancieve(SCARDHANDLE hCard,
60         BYTE *send, DWORD sendsize, BYTE *recv, LPDWORD recvsize_p)
61 {
62         DWORD rc;
63         DWORD rsize = *recvsize_p + 2;
64         BYTE *rbuf = alloca(rsize);
65         BYTE *sbuf = alloca(sendsize + 6);
66         memcpy(sbuf, cr_cmd, sizeof(cr_cmd));
67         sbuf[2] = cr_for_slot[slot];
68         sbuf[sizeof(cr_cmd)] = sendsize;
69         memcpy(sbuf + sizeof(cr_cmd) + 1, send, sendsize);
70         sbuf[sendsize + 5] = rsize;
71         rc = SCardTransmit(hCard, &pioSendPci, sbuf, sendsize + 6,
72                 NULL, rbuf, &rsize);
73         if (rc) return rc;
74         if ((rbuf[rsize-2] != 0x90) || (rbuf[rsize-1] != 0x00))
75                 return SCARD_W_CARD_NOT_AUTHENTICATED;
76         memcpy(recv, rbuf, rsize - 2);
77         *recvsize_p = rsize - 2;
78         return SCARD_S_SUCCESS;
79 }
80
81 static DWORD ykn_epilogue(SCARDHANDLE hCard)
82 {
83         return SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
84 }
85
86 struct token_interface ykneo_interface = {
87         .name           = "ykneo",
88         .parse_option   = ykn_parse_option,
89         .check_atr_hb   = ykn_check_atr_hb,
90         .prologue       = ykn_prologue,
91         .trancieve      = ykn_trancieve,
92         .epilogue       = ykn_epilogue,
93 };