1 |
twoaday |
342 |
/* wptCardPCSC.cpp - PC/SC card API interface |
2 |
|
|
* Copyright (C) 2003, 2006 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
*/ |
16 |
|
|
#ifdef HAVE_CONFIG_H |
17 |
|
|
#include <config.h> |
18 |
|
|
#endif |
19 |
|
|
|
20 |
|
|
#include <stdio.h> |
21 |
|
|
#include <windows.h> |
22 |
|
|
#ifndef __MINGW32__ |
23 |
|
|
#include <winscard.h> |
24 |
|
|
#else |
25 |
|
|
/* mingw32 has no win32 smartcard API definitions. */ |
26 |
|
|
typedef ULONG_PTR SCARDCONTEXT; |
27 |
|
|
#define SCARD_SCOPE_SYSTEM 2 |
28 |
|
|
#define SCARD_S_SUCCESS NO_ERROR |
29 |
|
|
#endif |
30 |
|
|
|
31 |
|
|
#include "wptTypes.h" |
32 |
|
|
#include "wptCard.h" |
33 |
|
|
|
34 |
|
|
#define load_dll_fnc(cast, name) (cast)GetProcAddress(hlib, name) |
35 |
|
|
|
36 |
|
|
|
37 |
|
|
typedef LONG (WINAPI *pcsc_establish_context_t) (unsigned long scope, |
38 |
|
|
const void *reserved1, |
39 |
|
|
const void *reserved2, |
40 |
|
|
unsigned long *r_context); |
41 |
|
|
typedef LONG (WINAPI *pcsc_release_context_t) (unsigned long context); |
42 |
|
|
|
43 |
|
|
static pcsc_establish_context_t pcsc_establish = NULL; |
44 |
|
|
static pcsc_release_context_t pcsc_release = NULL; |
45 |
|
|
static int pcsc_init = 0; |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
static int |
49 |
|
|
pcsc_loadlib (void) |
50 |
|
|
{ |
51 |
|
|
HMODULE hlib; |
52 |
|
|
|
53 |
|
|
if (pcsc_init) |
54 |
|
|
return 0; |
55 |
|
|
hlib = LoadLibrary ("WINSCARD"); |
56 |
|
|
if (!hlib) |
57 |
|
|
goto fail; |
58 |
|
|
pcsc_establish = load_dll_fnc (pcsc_establish_context_t, |
59 |
|
|
"SCardEstablishContext"); |
60 |
|
|
pcsc_release = load_dll_fnc (pcsc_release_context_t, |
61 |
|
|
"SCardReleaseContext"); |
62 |
|
|
if (pcsc_establish != NULL && pcsc_release != NULL) |
63 |
|
|
goto success; |
64 |
|
|
|
65 |
|
|
fail: |
66 |
|
|
if (hlib != NULL) |
67 |
|
|
FreeLibrary (hlib); |
68 |
|
|
return -1; |
69 |
|
|
|
70 |
|
|
success: |
71 |
|
|
pcsc_init = 1; |
72 |
|
|
return 0; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
/* Return != 0 if the PC/SC smart card interface is available. |
77 |
|
|
The purpose of the function is to disable smart card releation |
78 |
|
|
commands on systems with no scard readers. */ |
79 |
|
|
int |
80 |
|
|
pcsc_available (void) |
81 |
|
|
{ |
82 |
|
|
SCARDCONTEXT ctx; |
83 |
|
|
LONG err; |
84 |
|
|
|
85 |
|
|
if (pcsc_loadlib ()) |
86 |
|
|
return 0; |
87 |
|
|
|
88 |
|
|
err = pcsc_establish (SCARD_SCOPE_SYSTEM, NULL, NULL, &ctx); |
89 |
|
|
if (err == SCARD_S_SUCCESS) { |
90 |
|
|
pcsc_release (ctx); |
91 |
|
|
return -1; |
92 |
|
|
} |
93 |
|
|
return 0; |
94 |
|
|
} |