1 /*
2  * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3  *
4  * Copyright (C) 2001-2004
5  *  David Corcoran <corcoran@musclecard.com>
6  * Copyright (C) 2003-2004
7  *  Damien Sauveron <damien.sauveron@labri.fr>
8  * Copyright (C) 2002-2010
9  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
10  *
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14 
15 1. Redistributions of source code must retain the above copyright
16    notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18    notice, this list of conditions and the following disclaimer in the
19    documentation and/or other materials provided with the distribution.
20 3. The name of the author may not be used to endorse or promote products
21    derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file
37  * @brief This defines some structures and \#defines to be used over
38  * the transport layer.
39  */
40 
41 #ifndef __winscard_msg_h__
42 #define __winscard_msg_h__
43 
44 #include <stdint.h>
45 
46 #include "pcsclite.h"
47 #include "wintypes.h"
48 
49 /** Major version of the current message protocol */
50 #define PROTOCOL_VERSION_MAJOR 4
51 /** Minor version of the current message protocol */
52 #define PROTOCOL_VERSION_MINOR 4
53 
54 	/**
55 	 * @brief Information transmitted in \ref CMD_VERSION Messages.
56 	 */
57 	struct version_struct
58 	{
59 		int32_t major;	/**< IPC major \ref PROTOCOL_VERSION_MAJOR */
60 		int32_t minor;	/**< IPC minor \ref PROTOCOL_VERSION_MINOR */
61 		uint32_t rv;
62 	};
63 
64 	/**
65 	 * @brief header structure for client/server message data exchange.
66 	 */
67 	struct rxHeader
68 	{
69 		uint32_t size;		/**< size of the message excluding this header */
70 		uint32_t command;	/**< one of the \c pcsc_msg_commands */
71 	};
72 
73 	/**
74 	 * @brief Commands available to use in the field \c sharedSegmentMsg.command.
75 	 */
76 	enum pcsc_msg_commands
77 	{
78 		CMD_ENUM_FIRST,
79 		SCARD_ESTABLISH_CONTEXT = 0x01,	/**< used by SCardEstablishContext() */
80 		SCARD_RELEASE_CONTEXT = 0x02,	/**< used by SCardReleaseContext() */
81 		SCARD_LIST_READERS = 0x03,		/**< used by SCardListReaders() */
82 		SCARD_CONNECT = 0x04,			/**< used by SCardConnect() */
83 		SCARD_RECONNECT = 0x05,			/**< used by SCardReconnect() */
84 		SCARD_DISCONNECT = 0x06,		/**< used by SCardDisconnect() */
85 		SCARD_BEGIN_TRANSACTION = 0x07,	/**< used by SCardBeginTransaction() */
86 		SCARD_END_TRANSACTION = 0x08,	/**< used by SCardEndTransaction() */
87 		SCARD_TRANSMIT = 0x09,			/**< used by SCardTransmit() */
88 		SCARD_CONTROL = 0x0A,			/**< used by SCardControl() */
89 		SCARD_STATUS = 0x0B,			/**< used by SCardStatus() */
90 		SCARD_GET_STATUS_CHANGE = 0x0C,	/**< not used */
91 		SCARD_CANCEL = 0x0D,			/**< used by SCardCancel() */
92 		SCARD_CANCEL_TRANSACTION = 0x0E,/**< not used */
93 		SCARD_GET_ATTRIB = 0x0F,		/**< used by SCardGetAttrib() */
94 		SCARD_SET_ATTRIB = 0x10,		/**< used by SCardSetAttrib() */
95 		CMD_VERSION = 0x11,				/**< get the client/server protocol version */
96 		CMD_GET_READERS_STATE = 0x12,	/**< get the readers state */
97 		CMD_WAIT_READER_STATE_CHANGE = 0x13,	/**< wait for a reader state change */
98 		CMD_STOP_WAITING_READER_STATE_CHANGE = 0x14,	/**< stop waiting for a reader state change */
99 		CMD_ENUM_LAST
100 	};
101 
102 	struct client_struct
103 	{
104 		uint32_t hContext;
105 	};
106 
107 	/**
108 	 * @brief Information contained in \ref CMD_WAIT_READER_STATE_CHANGE Messages.
109 	 */
110 	struct wait_reader_state_change
111 	{
112 		uint32_t timeOut;	/**< timeout in ms */
113 		uint32_t rv;
114 	};
115 
116 	/**
117 	 * @brief Information contained in \ref SCARD_ESTABLISH_CONTEXT Messages.
118 	 *
119 	 * These data are passed throw the field \c sharedSegmentMsg.data.
120 	 */
121 	struct establish_struct
122 	{
123 		uint32_t dwScope;
124 		uint32_t hContext;
125 		uint32_t rv;
126 	};
127 
128 	/**
129 	 * @brief Information contained in \ref SCARD_RELEASE_CONTEXT Messages.
130 	 *
131 	 * These data are passed throw the field \c sharedSegmentMsg.data.
132 	 */
133 	struct release_struct
134 	{
135 		uint32_t hContext;
136 		uint32_t rv;
137 	};
138 
139 	/**
140 	 * @brief contained in \ref SCARD_CONNECT Messages.
141 	 *
142 	 * These data are passed throw the field \c sharedSegmentMsg.data.
143 	 */
144 	struct connect_struct
145 	{
146 		uint32_t hContext;
147 		char szReader[MAX_READERNAME];
148 		uint32_t dwShareMode;
149 		uint32_t dwPreferredProtocols;
150 		int32_t hCard;
151 		uint32_t dwActiveProtocol;
152 		uint32_t rv;
153 	};
154 
155 	/**
156 	 * @brief contained in \ref SCARD_RECONNECT Messages.
157 	 *
158 	 * These data are passed throw the field \c sharedSegmentMsg.data.
159 	 */
160 	struct reconnect_struct
161 	{
162 		int32_t hCard;
163 		uint32_t dwShareMode;
164 		uint32_t dwPreferredProtocols;
165 		uint32_t dwInitialization;
166 		uint32_t dwActiveProtocol;
167 		uint32_t rv;
168 	};
169 
170 	/**
171 	 * @brief contained in \ref SCARD_DISCONNECT Messages.
172 	 *
173 	 * These data are passed throw the field \c sharedSegmentMsg.data.
174 	 */
175 	struct disconnect_struct
176 	{
177 		int32_t hCard;
178 		uint32_t dwDisposition;
179 		uint32_t rv;
180 	};
181 
182 	/**
183 	 * @brief contained in \ref SCARD_BEGIN_TRANSACTION Messages.
184 	 *
185 	 * These data are passed throw the field \c sharedSegmentMsg.data.
186 	 */
187 	struct begin_struct
188 	{
189 		int32_t hCard;
190 		uint32_t rv;
191 	};
192 
193 	/**
194 	 * @brief contained in \ref SCARD_END_TRANSACTION Messages.
195 	 *
196 	 * These data are passed throw the field \c sharedSegmentMsg.data.
197 	 */
198 	struct end_struct
199 	{
200 		int32_t hCard;
201 		uint32_t dwDisposition;
202 		uint32_t rv;
203 	};
204 
205 	/**
206 	 * @brief contained in \ref SCARD_CANCEL Messages.
207 	 *
208 	 * These data are passed throw the field \c sharedSegmentMsg.data.
209 	 */
210 	struct cancel_struct
211 	{
212 		int32_t hContext;
213 		uint32_t rv;
214 	};
215 
216 	/**
217 	 * @brief contained in \ref SCARD_STATUS Messages.
218 	 *
219 	 * These data are passed throw the field \c sharedSegmentMsg.data.
220 	 */
221 	struct status_struct
222 	{
223 		int32_t hCard;
224 		uint32_t rv;
225 	};
226 
227 	/**
228 	 * @brief contained in \ref SCARD_TRANSMIT Messages.
229 	 *
230 	 * These data are passed throw the field \c sharedSegmentMsg.data.
231 	 */
232 	struct transmit_struct
233 	{
234 		int32_t hCard;
235 		uint32_t ioSendPciProtocol;
236 		uint32_t ioSendPciLength;
237 		uint32_t cbSendLength;
238 		uint32_t ioRecvPciProtocol;
239 		uint32_t ioRecvPciLength;
240 		uint32_t pcbRecvLength;
241 		uint32_t rv;
242 	};
243 
244 	/**
245 	 * @brief contained in \ref SCARD_CONTROL Messages.
246 	 *
247 	 * These data are passed throw the field \c sharedSegmentMsg.data.
248 	 */
249 	struct control_struct
250 	{
251 		int32_t hCard;
252 		uint32_t dwControlCode;
253 		uint32_t cbSendLength;
254 		uint32_t cbRecvLength;
255 		uint32_t dwBytesReturned;
256 		uint32_t rv;
257 	};
258 
259 	/**
260 	 * @brief contained in \ref SCARD_GET_ATTRIB and \c  Messages.
261 	 *
262 	 * These data are passed throw the field \c sharedSegmentMsg.data.
263 	 */
264 	struct getset_struct
265 	{
266 		int32_t hCard;
267 		uint32_t dwAttrId;
268 		uint8_t pbAttr[MAX_BUFFER_SIZE];
269 		uint32_t cbAttrLen;
270 		uint32_t rv;
271 	};
272 
273 	/*
274 	 * Now some function definitions
275 	 */
276 
277 #ifdef PCSCD
278 	int32_t InitializeSocket(void);
279 	int32_t ListenExistingSocket(int fd);
280 	int32_t ProcessEventsServer(/*@out@*/ uint32_t *);
281 #else
282 	char *getSocketName(void);
283 	int32_t ClientSetupSession(uint32_t *);
284 	void ClientCloseSession(uint32_t);
285 	LONG MessageReceiveTimeout(uint32_t command, /*@out@*/ void *buffer,
286 		uint64_t buffer_size, int32_t filedes, long timeOut);
287 	LONG MessageSendWithHeader(uint32_t command, uint32_t dwClientID,
288 		uint64_t size, void *data);
289 #endif
290 	LONG MessageSend(void *buffer, uint64_t buffer_size, int32_t filedes);
291 	LONG MessageReceive(/*@out@*/ void *buffer, uint64_t buffer_size,
292 		int32_t filedes);
293 
294 #endif
295