1 /*
2    rdesktop: A Remote Desktop Protocol client.
3    Smart Card support
4    Copyright (C) Alexi Volkov <alexi@myrealbox.com> 2006
5    Copyright 2016 Alexander Zakharov <uglym8@gmail.com>
6 
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <pthread.h>
22 #include "proto.h"
23 
24 /*************************************************************************/
25 /* these are the additional types needed to split out 64-vs-32-bit APIs  */
26 /*                                                                       */
27 
28 /* The point of all of this is to avoid patching the existing smartcard
29  * infrastructure (PC/SC Lite, libmusclecard+libmusclepkcs11 or CoolKey, any
30  * other apps linking against any of these) because the need for patches
31  * spreads without limit. The alternative is to patch the heck out of rdesktop,
32  * which is already being done anyway.
33  *
34  * - jared.jennings@eglin.af.mil, 2 Aug 2006
35  */
36 
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40 #ifdef HAVE_INTTYPES_H
41 #include <inttypes.h>
42 #endif
43 
44 /* A DWORD when dealing with the smartcard stuff. Could be 32 bits or 64. */
45 typedef DWORD MYPCSC_DWORD;
46 /* A DWORD when talking to the server. Must be exactly 32 bits all the time.*/
47 typedef uint32_t SERVER_DWORD;
48 
49 typedef SCARDCONTEXT MYPCSC_SCARDCONTEXT;
50 typedef SCARDHANDLE MYPCSC_SCARDHANDLE;
51 typedef uint32_t SERVER_SCARDCONTEXT;
52 typedef uint32_t SERVER_SCARDHANDLE;
53 
54 typedef SCARD_READERSTATE MYPCSC_SCARD_READERSTATE_A, *MYPCSC_LPSCARD_READERSTATE_A;
55 
56 typedef struct
57 {
58 	const char *szReader;
59 	void *pvUserData;
60 	SERVER_DWORD dwCurrentState;
61 	SERVER_DWORD dwEventState;
62 	SERVER_DWORD cbAtr;
63 	unsigned char rgbAtr[36];
64 }
65 SERVER_SCARD_READERSTATE_A;
66 
67 typedef SERVER_SCARD_READERSTATE_A *SERVER_LPSCARD_READERSTATE_A;
68 
69 #define SERVER_SCARDSTATESIZE              (sizeof(SERVER_SCARD_READERSTATE_A) - sizeof(const char *) - sizeof(void *))
70 #define MYPCSC_SCARDSTATESIZE              (sizeof(MYPCSC_SCARD_READERSTATE_A) - sizeof(const char *) - sizeof(void *))
71 
72 typedef struct _SERVER_SCARD_IO_REQUEST
73 {
74 	SERVER_DWORD dwProtocol;	/* Protocol identifier */
75 	SERVER_DWORD cbPciLength;	/* Protocol Control Inf Length */
76 }
77 SERVER_SCARD_IO_REQUEST, *SERVER_LPSCARD_IO_REQUEST;
78 
79 typedef SCARD_IO_REQUEST MYPCSC_SCARD_IO_REQUEST;
80 typedef LPSCARD_IO_REQUEST MYPCSC_LPSCARD_IO_REQUEST;
81 
82 
83 /*                                                                       */
84 /*                                                                       */
85 /*************************************************************************/
86 
87 
88 #define	SC_TRUE                     1
89 #define SC_FALSE                    0
90 
91 #define SC_ESTABLISH_CONTEXT        0x00090014	/* EstablishContext */
92 #define SC_RELEASE_CONTEXT          0x00090018	/* ReleaseContext */
93 #define SC_IS_VALID_CONTEXT         0x0009001C	/* IsValidContext */
94 #define SC_LIST_READER_GROUPS       0x00090020	/* ListReaderGroups */
95 #define SC_LIST_READERS             0x00090028	/* ListReadersA */
96 #define SC_INTRODUCE_READER_GROUP   0x00090050	/* IntroduceReaderGroup */
97 #define SC_FORGET_READER_GROUP      0x00090058	/* ForgetReader */
98 #define SC_INTRODUCE_READER         0x00090060	/* IntroduceReader */
99 #define SC_FORGET_READER            0x00090068	/* IntroduceReader */
100 #define SC_ADD_READER_TO_GROUP      0x00090070	/* AddReaderToGroup */
101 #define SC_REMOVE_READER_FROM_GROUP 0x00090078	/* RemoveReaderFromGroup */
102 #define SC_CONNECT                  0x000900AC	/* ConnectA */
103 #define SC_RECONNECT                0x000900B4	/* Reconnect */
104 #define SC_DISCONNECT               0x000900B8	/* Disconnect */
105 #define SC_GET_STATUS_CHANGE        0x000900A0	/* GetStatusChangeA */
106 #define SC_CANCEL                   0x000900A8	/* Cancel */
107 #define SC_BEGIN_TRANSACTION        0x000900BC	/* BeginTransaction */
108 #define SC_END_TRANSACTION          0x000900C0	/* EndTransaction */
109 #define SC_STATE		    0x000900C4	/* State */
110 #define SC_STATUS                   0x000900C8	/* StatusA */
111 #define SC_TRANSMIT                 0x000900D0	/* Transmit */
112 #define SC_CONTROL                  0x000900D4	/* Control */
113 #define SC_GETATTRIB                0x000900D8	/* GetAttrib */
114 #define SC_SETATTRIB                0x000900DC	/* SetAttrib */
115 #define SC_ACCESS_STARTED_EVENT	    0x000900E0	/* SCardAccessStartedEvent */
116 #define SC_LOCATE_CARDS_BY_ATR      0x000900E8	/* LocateCardsByATR */
117 
118 /* #define INPUT_LINKED                0x00020000 */
119 #define INPUT_LINKED                0xFFFFFFFF
120 
121 #define SC_THREAD_FUNCTION(f)       void *(*f)(void *)
122 
123 extern RDPDR_DEVICE g_rdpdr_device[];
124 
125 typedef struct _MEM_HANDLE
126 {
127 	struct _MEM_HANDLE *prevHandle;
128 	struct _MEM_HANDLE *nextHandle;
129 	int dataSize;
130 } MEM_HANDLE, *PMEM_HANDLE;
131 
132 typedef struct _SCARD_ATRMASK_L
133 {
134 	unsigned int cbAtr;
135 	unsigned char rgbAtr[36];
136 	unsigned char rgbMask[36];
137 } SCARD_ATRMASK_L, *PSCARD_ATRMASK_L, *LPSCARD_ATRMASK_L;
138 
139 typedef struct _TSCNameMapRec
140 {
141 	char alias[128];
142 	char name[128];
143 	char vendor[128];
144 } TSCNameMapRec, *PSCNameMapRec;
145 
146 typedef struct _TSCHCardRec
147 {
148 	DWORD hCard;
149 	char *vendor;
150 	struct _TSCHCardRec *next;
151 	struct _TSCHCardRec *prev;
152 } TSCHCardRec, *PSCHCardRec;
153 
154 typedef struct _TSCThreadData
155 {
156 	uint32 device;
157 	uint32 id;
158 	uint32 epoch;
159 	uint32 srv_buf_len;
160 	RD_NTHANDLE handle;
161 	uint32 request;
162 	STREAM in;
163 	STREAM out;
164 	PMEM_HANDLE memHandle;
165 	struct _TSCThreadData *next;
166 } TSCThreadData, *PSCThreadData;
167 
168 typedef struct _TThreadListElement
169 {
170 	pthread_t thread;
171 	pthread_mutex_t busy;
172 	pthread_cond_t nodata;
173 	PSCThreadData data;
174 	struct _TThreadListElement *next;
175 } TThreadListElement, *PThreadListElement;
176