1 |
/* 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 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <stdio.h> |
25 |
#include <windows.h> |
26 |
#ifndef __MINGW32__ |
27 |
#include <winscard.h> |
28 |
#else |
29 |
/* mingw32 has no win32 smartcard API definitions. */ |
30 |
typedef ULONG_PTR SCARDCONTEXT; |
31 |
#define SCARD_SCOPE_SYSTEM 2 |
32 |
#define SCARD_S_SUCCESS NO_ERROR |
33 |
#endif |
34 |
|
35 |
#include "gpgme.h" |
36 |
#include "wptTypes.h" |
37 |
#include "wptCard.h" |
38 |
|
39 |
|
40 |
static LONG (WINAPI *pcsc_establish_context) (unsigned long scope, |
41 |
const void *reserved1, |
42 |
const void *reserved2, |
43 |
unsigned long *r_context); |
44 |
static LONG (WINAPI *pcsc_release_context) (unsigned long context); |
45 |
|
46 |
static int pcsc_init = 0; |
47 |
|
48 |
|
49 |
#define load_one_fnc(ptr, hlib, name) do { \ |
50 |
a = ptr = (void *)GetProcAddress (hlib, name); \ |
51 |
if (!a) { \ |
52 |
char msg[256]; \ |
53 |
_snprintf (msg, sizeof msg-1, "Could not load '%s'", name); \ |
54 |
MessageBox (NULL, msg, "PC/SC Driver", MB_ICONERROR|MB_OK); \ |
55 |
goto fail; \ |
56 |
} \ |
57 |
} while (0) |
58 |
|
59 |
|
60 |
static int |
61 |
pcsc_loadlib (void) |
62 |
{ |
63 |
HMODULE hlib; |
64 |
FARPROC a; |
65 |
|
66 |
if (pcsc_init) |
67 |
return 0; |
68 |
hlib = LoadLibrary ("WINSCARD"); |
69 |
if (!hlib) |
70 |
goto fail; |
71 |
load_one_fnc (pcsc_establish_context, hlib, "SCardEstablishContext"); |
72 |
load_one_fnc (pcsc_release_context, hlib, "SCardReleaseContext"); |
73 |
goto success; |
74 |
|
75 |
fail: |
76 |
if (hlib != NULL) |
77 |
FreeLibrary (hlib); |
78 |
return -1; |
79 |
|
80 |
success: |
81 |
pcsc_init = 1; |
82 |
return 0; |
83 |
} |
84 |
|
85 |
|
86 |
/* Return != 0 if the PC/SC smart card interface is available. |
87 |
The purpose of the function is to disable smart card releation |
88 |
commands on systems with no scard readers. */ |
89 |
int |
90 |
pcsc_available (void) |
91 |
{ |
92 |
SCARDCONTEXT ctx; |
93 |
LONG err; |
94 |
|
95 |
if (pcsc_loadlib ()) |
96 |
return 0; |
97 |
|
98 |
err = pcsc_establish_context (SCARD_SCOPE_SYSTEM, NULL, NULL, &ctx); |
99 |
if (err == SCARD_S_SUCCESS) { |
100 |
pcsc_release_context (ctx); |
101 |
return -1; |
102 |
} |
103 |
return 0; |
104 |
} |