1 |
/* OpenPGP Smart Card Mini Driver |
2 |
Copyright (C) 2009 Vincent Le Toux |
3 |
|
4 |
This library is Free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU Lesser General Public |
6 |
License version 2.1 as published by the Free Software Foundation. |
7 |
|
8 |
This library is distributed in the hope that it will be useful, |
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 |
Lesser General Public License for more details. |
12 |
|
13 |
You should have received a copy of the GNU Lesser General Public |
14 |
License along with this library; if not, write to the Free Software |
15 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 |
*/ |
17 |
|
18 |
#include <windows.h> |
19 |
#include <cardmod.h> |
20 |
#include "Tracing.h" |
21 |
#include "Context.h" |
22 |
#include "PublicDataOperations.h" |
23 |
|
24 |
// 4.3 Public Data Operations |
25 |
|
26 |
/** This function creates a subdirectory from the root in the file system of |
27 |
the card and applies the provided access condition. Directories are generally |
28 |
created for segregating the files that belong to a single application on the card. |
29 |
As an example, the files that belong to the Microsoft cryptographic application |
30 |
are in the mscp directory.*/ |
31 |
|
32 |
DWORD WINAPI CardCreateDirectory( |
33 |
__in PCARD_DATA pCardData, |
34 |
__in LPSTR pszDirectoryName, |
35 |
__in CARD_DIRECTORY_ACCESS_CONDITION AccessCondition |
36 |
) |
37 |
{ |
38 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
39 |
return SCARD_E_UNSUPPORTED_FEATURE; |
40 |
} |
41 |
|
42 |
/** This function deletes a directory from the card. This operation fails if it violates |
43 |
permissions on the directory or if the directory is not empty. */ |
44 |
DWORD WINAPI CardDeleteDirectory( |
45 |
__in CARD_DATA *pCardData, |
46 |
__in LPSTR pszDirectoryName |
47 |
) |
48 |
{ |
49 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
50 |
return SCARD_E_UNSUPPORTED_FEATURE; |
51 |
} |
52 |
|
53 |
/** The CardReadFile function reads the entire file at the specified location into the |
54 |
user-supplied buffer.*/ |
55 |
DWORD WINAPI CardReadFile( |
56 |
__in PCARD_DATA pCardData, |
57 |
__in_opt LPSTR pszDirectoryName, |
58 |
__in LPSTR pszFileName, |
59 |
__in DWORD dwFlags, |
60 |
__deref_out_bcount_opt(*pcbData) PBYTE *ppbData, |
61 |
__out PDWORD pcbData |
62 |
) |
63 |
{ |
64 |
DWORD dwReturn = 0; |
65 |
PSTR szFiles = NULL; |
66 |
|
67 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter %S\\%S",pszDirectoryName,pszFileName); |
68 |
__try |
69 |
{ |
70 |
if ( pCardData == NULL ) |
71 |
{ |
72 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
73 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
74 |
__leave; |
75 |
} |
76 |
if ( pszFileName == NULL ) |
77 |
{ |
78 |
Trace(WINEVENT_LEVEL_ERROR, L"pszFileName == NULL"); |
79 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
80 |
__leave; |
81 |
} |
82 |
if (ppbData == NULL) |
83 |
{ |
84 |
Trace(WINEVENT_LEVEL_ERROR, L"ppbData == NULL"); |
85 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
86 |
__leave; |
87 |
} |
88 |
if (pcbData == NULL) |
89 |
{ |
90 |
Trace(WINEVENT_LEVEL_ERROR, L"pcbData == NULL"); |
91 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
92 |
__leave; |
93 |
} |
94 |
if (dwFlags != 0) |
95 |
{ |
96 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
97 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags = 0x%08X", dwFlags); |
98 |
__leave; |
99 |
} |
100 |
dwReturn = CheckContext(pCardData); |
101 |
if (dwReturn) |
102 |
{ |
103 |
__leave; |
104 |
} |
105 |
dwReturn = SCardReadFile(pCardData, pszDirectoryName, pszFileName, ppbData, pcbData); |
106 |
} |
107 |
__finally |
108 |
{ |
109 |
} |
110 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
111 |
return dwReturn; |
112 |
} |
113 |
|
114 |
/** The CardCreateFile function creates a file on the card with a specified name and |
115 |
access permission. This function cannot be used to create directories. If the directory |
116 |
that is named by pszDirectoryName does not exist, the function fails with SCARD_E_DIR_NOT_FOUND.*/ |
117 |
DWORD WINAPI CardCreateFile( |
118 |
__in PCARD_DATA pCardData, |
119 |
__in_opt LPSTR pszDirectoryName, |
120 |
__in LPSTR pszFileName, |
121 |
__in DWORD cbInitialCreationSize, |
122 |
__in CARD_FILE_ACCESS_CONDITION AccessCondition |
123 |
) |
124 |
{ |
125 |
DWORD dwReturn = 0; |
126 |
|
127 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
128 |
__try |
129 |
{ |
130 |
if ( pCardData == NULL ) |
131 |
{ |
132 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
133 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
134 |
__leave; |
135 |
} |
136 |
if ( pszFileName == NULL || pszFileName[0] == 0) |
137 |
{ |
138 |
Trace(WINEVENT_LEVEL_ERROR, L"pszFileName == NULL or empty"); |
139 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
140 |
__leave; |
141 |
} |
142 |
dwReturn = CheckContext(pCardData); |
143 |
if (dwReturn) |
144 |
{ |
145 |
__leave; |
146 |
} |
147 |
dwReturn = SCardCreateFile(pCardData, pszDirectoryName, pszFileName); |
148 |
} |
149 |
__finally |
150 |
{ |
151 |
} |
152 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
153 |
return dwReturn; |
154 |
} |
155 |
|
156 |
/** This function retrieves information about a file, specifically its size and ACL information.*/ |
157 |
|
158 |
DWORD WINAPI CardGetFileInfo( |
159 |
__in PCARD_DATA pCardData, |
160 |
__in_opt LPSTR pszDirectoryName, |
161 |
__in LPSTR pszFileName, |
162 |
__inout PCARD_FILE_INFO pCardFileInfo |
163 |
) |
164 |
{ |
165 |
DWORD dwReturn = 0; |
166 |
|
167 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
168 |
__try |
169 |
{ |
170 |
if ( pCardData == NULL ) |
171 |
{ |
172 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
173 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
174 |
__leave; |
175 |
} |
176 |
if ( pszFileName == NULL || pszFileName[0] == 0) |
177 |
{ |
178 |
Trace(WINEVENT_LEVEL_ERROR, L"pszFileName == NULL or empty"); |
179 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
180 |
__leave; |
181 |
} |
182 |
if ( pCardFileInfo == NULL ) |
183 |
{ |
184 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardFileInfo == NULL"); |
185 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
186 |
__leave; |
187 |
} |
188 |
dwReturn = CheckContext(pCardData); |
189 |
if (dwReturn) |
190 |
{ |
191 |
__leave; |
192 |
} |
193 |
dwReturn = SCardGetFileInfo(pCardData, pszDirectoryName, pszFileName, pCardFileInfo); |
194 |
|
195 |
} |
196 |
__finally |
197 |
{ |
198 |
} |
199 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
200 |
return dwReturn; |
201 |
} |
202 |
|
203 |
/** The CardWriteFile function writes the entire contents of a data buffer to a file. |
204 |
The file contents are replaced, starting at the beginning of the file. The file must |
205 |
exist, or CardWriteFile fails.*/ |
206 |
|
207 |
DWORD WINAPI CardWriteFile( |
208 |
__in PCARD_DATA pCardData, |
209 |
__in_opt LPSTR pszDirectoryName, |
210 |
__in LPSTR pszFileName, |
211 |
__in DWORD dwFlags, |
212 |
__in_bcount(cbData) PBYTE pbData, |
213 |
__in DWORD cbData |
214 |
) |
215 |
{ |
216 |
DWORD dwReturn = 0; |
217 |
|
218 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
219 |
__try |
220 |
{ |
221 |
if ( pCardData == NULL ) |
222 |
{ |
223 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
224 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
225 |
__leave; |
226 |
} |
227 |
if ( pszFileName == NULL || pszFileName[0] == 0) |
228 |
{ |
229 |
Trace(WINEVENT_LEVEL_ERROR, L"pszFileName == NULL or empty"); |
230 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
231 |
__leave; |
232 |
} |
233 |
if ( pbData == NULL) |
234 |
{ |
235 |
Trace(WINEVENT_LEVEL_ERROR, L"pbData == NULL"); |
236 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
237 |
__leave; |
238 |
} |
239 |
if ( cbData == 0) |
240 |
{ |
241 |
Trace(WINEVENT_LEVEL_ERROR, L"cbData == 0"); |
242 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
243 |
__leave; |
244 |
} |
245 |
if (dwFlags) |
246 |
{ |
247 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags == %d", dwFlags); |
248 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
249 |
__leave; |
250 |
} |
251 |
dwReturn = CheckContext(pCardData); |
252 |
if (dwReturn) |
253 |
{ |
254 |
__leave; |
255 |
} |
256 |
dwReturn = SCardWriteFile(pCardData, pszDirectoryName, pszFileName, pbData, cbData); |
257 |
} |
258 |
__finally |
259 |
{ |
260 |
} |
261 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
262 |
return dwReturn; |
263 |
} |
264 |
|
265 |
/** The CardDeleteFile function deletes the specified file. If the file does not exist, |
266 |
the returned Status value should indicate that the file did not exist.*/ |
267 |
|
268 |
DWORD WINAPI CardDeleteFile( |
269 |
__in PCARD_DATA pCardData, |
270 |
__in_opt LPSTR pszDirectoryName, |
271 |
__in LPSTR pszFileName, |
272 |
__in DWORD dwFlags |
273 |
) |
274 |
{ |
275 |
DWORD dwReturn = 0; |
276 |
|
277 |
PBYTE pbData = NULL; |
278 |
DWORD dwSize = 0; |
279 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
280 |
__try |
281 |
{ |
282 |
if ( pCardData == NULL ) |
283 |
{ |
284 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
285 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
286 |
__leave; |
287 |
} |
288 |
if ( pszFileName == NULL || pszFileName[0] == 0) |
289 |
{ |
290 |
Trace(WINEVENT_LEVEL_ERROR, L"pszFileName == NULL or empty"); |
291 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
292 |
__leave; |
293 |
} |
294 |
if (dwFlags) |
295 |
{ |
296 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags == 0"); |
297 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
298 |
__leave; |
299 |
} |
300 |
// try to read the file to see if it exists and return the right error code |
301 |
dwReturn = CheckContext(pCardData); |
302 |
if (dwReturn) |
303 |
{ |
304 |
__leave; |
305 |
} |
306 |
dwReturn = SCardDeleteFile(pCardData, pszDirectoryName, pszFileName); |
307 |
} |
308 |
__finally |
309 |
{ |
310 |
} |
311 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
312 |
return dwReturn; |
313 |
} |
314 |
|
315 |
/** The CardEnumFiles function returns name information about available files in a |
316 |
directory as a multistring list.*/ |
317 |
|
318 |
DWORD WINAPI CardEnumFiles( |
319 |
__in PCARD_DATA pCardData, |
320 |
__in_opt LPSTR pszDirectoryName, |
321 |
__deref_out_ecount(*pdwcbFileName) LPSTR *pmszFileNames, |
322 |
__out LPDWORD pdwcbFileName, |
323 |
__in DWORD dwFlags |
324 |
) |
325 |
{ |
326 |
DWORD dwReturn = 0; |
327 |
PSTR szFiles = NULL; |
328 |
|
329 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
330 |
__try |
331 |
{ |
332 |
if ( pCardData == NULL ) |
333 |
{ |
334 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
335 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
336 |
__leave; |
337 |
} |
338 |
if ( pmszFileNames == NULL ) |
339 |
{ |
340 |
Trace(WINEVENT_LEVEL_ERROR, L"pmszFileNames == NULL"); |
341 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
342 |
__leave; |
343 |
} |
344 |
if ( pmszFileNames == NULL ) |
345 |
{ |
346 |
Trace(WINEVENT_LEVEL_ERROR, L"pmszFileNames == NULL"); |
347 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
348 |
__leave; |
349 |
} |
350 |
if (dwFlags != 0) |
351 |
{ |
352 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
353 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags = 0x%08X", dwFlags); |
354 |
__leave; |
355 |
} |
356 |
if (pszDirectoryName != NULL) |
357 |
{ |
358 |
DWORD dwLen = (DWORD) strlen(pszDirectoryName); |
359 |
if (dwLen > 8 || dwLen == 0) |
360 |
{ |
361 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
362 |
Trace(WINEVENT_LEVEL_ERROR, L"Invalid directory %S", pszDirectoryName); |
363 |
__leave; |
364 |
} |
365 |
} |
366 |
dwReturn = CheckContext(pCardData); |
367 |
if (dwReturn) |
368 |
{ |
369 |
__leave; |
370 |
} |
371 |
dwReturn = SCardEnumFile(pCardData, pszDirectoryName, pmszFileNames, pdwcbFileName); |
372 |
} |
373 |
__finally |
374 |
{ |
375 |
} |
376 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
377 |
return dwReturn; |
378 |
} |
379 |
|
380 |
/** The CardQueryFreeSpace function determines the amount of available card storage space.*/ |
381 |
DWORD WINAPI CardQueryFreeSpace( |
382 |
__in PCARD_DATA pCardData, |
383 |
__in DWORD dwFlags, |
384 |
__inout PCARD_FREE_SPACE_INFO pCardFreeSpaceInfo |
385 |
) |
386 |
{ |
387 |
DWORD dwReturn = 0; |
388 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
389 |
__try |
390 |
{ |
391 |
if ( pCardData == NULL ) |
392 |
{ |
393 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
394 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
395 |
__leave; |
396 |
} |
397 |
if ( pCardFreeSpaceInfo == NULL) |
398 |
{ |
399 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardFreeSpaceInfo == NULL"); |
400 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
401 |
__leave; |
402 |
} |
403 |
if (dwFlags) |
404 |
{ |
405 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags == 0"); |
406 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
407 |
__leave; |
408 |
} |
409 |
pCardFreeSpaceInfo->dwVersion = CARD_FREE_SPACE_INFO_CURRENT_VERSION; |
410 |
pCardFreeSpaceInfo->dwMaxKeyContainers = 3; |
411 |
pCardFreeSpaceInfo->dwKeyContainersAvailable = CARD_DATA_VALUE_UNKNOWN; |
412 |
pCardFreeSpaceInfo->dwBytesAvailable = CARD_DATA_VALUE_UNKNOWN; |
413 |
dwReturn = 0; |
414 |
} |
415 |
__finally |
416 |
{ |
417 |
} |
418 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
419 |
return dwReturn; |
420 |
} |
421 |
|