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 "CryptoOperations.h" |
23 |
|
24 |
// 4.7 Cryptographic operations |
25 |
|
26 |
/** This function performs an RSA decryption operation on the passed buffer |
27 |
by using the private key that a container index refers to. Note that for |
28 |
ECC-only smart cards, this entry point is not defined and is set to NULL |
29 |
in the returned CARD_DATA structure from CardAcquireContext. This operation |
30 |
is restricted to a single buffer of a size equal to the key modulus.*/ |
31 |
|
32 |
DWORD WINAPI CardRSADecrypt( |
33 |
__in PCARD_DATA pCardData, |
34 |
__inout PCARD_RSA_DECRYPT_INFO pInfo |
35 |
) |
36 |
{ |
37 |
DWORD dwReturn = 0; |
38 |
|
39 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
40 |
__try |
41 |
{ |
42 |
if ( pCardData == NULL ) |
43 |
{ |
44 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
45 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
46 |
__leave; |
47 |
} |
48 |
|
49 |
if ( pInfo == NULL ) |
50 |
{ |
51 |
Trace(WINEVENT_LEVEL_ERROR, L"pInfo == NULL"); |
52 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
53 |
__leave; |
54 |
} |
55 |
if ( pInfo->pbData == NULL ) |
56 |
{ |
57 |
Trace(WINEVENT_LEVEL_ERROR, L"pInfo->pbData == NULL"); |
58 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
59 |
__leave; |
60 |
} |
61 |
if (pInfo->dwVersion < CARD_RSA_KEY_DECRYPT_INFO_CURRENT_VERSION |
62 |
&& pCardData->dwVersion == CARD_DATA_CURRENT_VERSION) |
63 |
{ |
64 |
Trace(WINEVENT_LEVEL_ERROR, L"ERROR_REVISION_MISMATCH"); |
65 |
dwReturn = ERROR_REVISION_MISMATCH; |
66 |
__leave; |
67 |
} |
68 |
if (pInfo->dwKeySpec != AT_KEYEXCHANGE) |
69 |
{ |
70 |
Trace(WINEVENT_LEVEL_ERROR, L"AT_KEYEXCHANGE %d", pInfo->dwKeySpec); |
71 |
dwReturn = SCARD_E_NO_KEY_CONTAINER ; |
72 |
__leave; |
73 |
} |
74 |
if (pInfo->bContainerIndex != Confidentiality) |
75 |
{ |
76 |
Trace(WINEVENT_LEVEL_ERROR, L"Confidentiality %d", pInfo->bContainerIndex); |
77 |
dwReturn = SCARD_E_NO_KEY_CONTAINER ; |
78 |
__leave; |
79 |
} |
80 |
dwReturn = CheckContext(pCardData); |
81 |
if ( dwReturn) |
82 |
{ |
83 |
__leave; |
84 |
} |
85 |
dwReturn = SCardDecrypt(pCardData, pInfo); |
86 |
} |
87 |
__finally |
88 |
{ |
89 |
} |
90 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
91 |
return dwReturn; |
92 |
} |
93 |
|
94 |
|
95 |
/** The CardSignData function signs a block of unpadded data. This entry either performs |
96 |
padding on the card or pads the data by using the PFN_CSP_PAD_DATA callback. All card |
97 |
minidrivers must support this entry point.*/ |
98 |
|
99 |
DWORD WINAPI CardSignData( |
100 |
__in PCARD_DATA pCardData, |
101 |
__in PCARD_SIGNING_INFO pInfo |
102 |
) |
103 |
{ |
104 |
DWORD dwReturn = 0; |
105 |
|
106 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
107 |
__try |
108 |
{ |
109 |
if ( pCardData == NULL ) |
110 |
{ |
111 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
112 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
113 |
__leave; |
114 |
} |
115 |
|
116 |
if ( pInfo == NULL ) |
117 |
{ |
118 |
Trace(WINEVENT_LEVEL_ERROR, L"pInfo == NULL"); |
119 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
120 |
__leave; |
121 |
} |
122 |
if ( pInfo->pbData == NULL ) |
123 |
{ |
124 |
Trace(WINEVENT_LEVEL_ERROR, L"pInfo->pbData == NULL"); |
125 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
126 |
__leave; |
127 |
} |
128 |
dwReturn = CheckContext(pCardData); |
129 |
if ( dwReturn) |
130 |
{ |
131 |
__leave; |
132 |
} |
133 |
switch(pInfo->bContainerIndex) |
134 |
{ |
135 |
case Authentication: |
136 |
dwReturn = SCardAuthenticate(pCardData, pInfo); |
137 |
break; |
138 |
case Signature: |
139 |
dwReturn = SCardSign(pCardData, pInfo); |
140 |
break; |
141 |
default: |
142 |
dwReturn = SCARD_E_NO_KEY_CONTAINER; |
143 |
Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex); |
144 |
__leave; |
145 |
} |
146 |
} |
147 |
__finally |
148 |
{ |
149 |
} |
150 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
151 |
return dwReturn; |
152 |
} |
153 |
|
154 |
/** This function returns the public key sizes that are supported by the card in use.*/ |
155 |
DWORD WINAPI CardQueryKeySizes( |
156 |
__in PCARD_DATA pCardData, |
157 |
__in DWORD dwKeySpec, |
158 |
__in DWORD dwFlags, |
159 |
__inout PCARD_KEY_SIZES pKeySizes |
160 |
) |
161 |
{ |
162 |
DWORD dwReturn = 0, dwVersion; |
163 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
164 |
__try |
165 |
{ |
166 |
if ( pCardData == NULL ) |
167 |
{ |
168 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
169 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
170 |
__leave; |
171 |
} |
172 |
|
173 |
if ( dwFlags != 0 ) |
174 |
{ |
175 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags != 0 : %d", dwFlags); |
176 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
177 |
__leave; |
178 |
} |
179 |
if ( pKeySizes == NULL ) |
180 |
{ |
181 |
Trace(WINEVENT_LEVEL_ERROR, L"pKeySizes == NULL"); |
182 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
183 |
__leave; |
184 |
} |
185 |
dwVersion = (pKeySizes->dwVersion == 0) ? 1 : pKeySizes->dwVersion; |
186 |
if ( dwVersion != CARD_KEY_SIZES_CURRENT_VERSION ) |
187 |
{ |
188 |
Trace(WINEVENT_LEVEL_ERROR, L"dwVersion == %d", pKeySizes->dwVersion); |
189 |
dwReturn = ERROR_REVISION_MISMATCH; |
190 |
__leave; |
191 |
} |
192 |
|
193 |
switch(dwKeySpec) |
194 |
{ |
195 |
case AT_ECDHE_P256 : |
196 |
case AT_ECDHE_P384 : |
197 |
case AT_ECDHE_P521 : |
198 |
case AT_ECDSA_P256 : |
199 |
case AT_ECDSA_P384 : |
200 |
case AT_ECDSA_P521 : |
201 |
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d", dwKeySpec); |
202 |
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
203 |
__leave; |
204 |
break; |
205 |
case AT_KEYEXCHANGE: |
206 |
case AT_SIGNATURE : |
207 |
break; |
208 |
default: |
209 |
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d", dwKeySpec); |
210 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
211 |
__leave; |
212 |
break; |
213 |
} |
214 |
|
215 |
pKeySizes->dwMinimumBitlen = 1024; |
216 |
pKeySizes->dwDefaultBitlen = 2048; |
217 |
pKeySizes->dwMaximumBitlen = 2048; |
218 |
pKeySizes->dwIncrementalBitlen = 0; |
219 |
} |
220 |
__finally |
221 |
{ |
222 |
} |
223 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
224 |
return dwReturn; |
225 |
} |
226 |
|
227 |
|
228 |
/** The CardConstructDHAgreement function performs a secret agreement calculation |
229 |
for Diffie Hellman (DH) key exchange by using a private key that is present on the |
230 |
card. For RSA-only card minidrivers, this entry point is not defined and is set to |
231 |
NULL in the CARD_DATA structure that is returned from CardAcquireContext. |
232 |
The CARD_DH_AGREEMENT structure changes to allow for return of a handle to |
233 |
the agreed secret. This raises a point about how to index the DH agreement |
234 |
on the card in an opaque manner. Maintaining a map file is unnecessary because |
235 |
Ncrypt makes no provision for persistent DH agreements and there is no way to |
236 |
retrieve one after a provider is closed. DH agreements are addressable on card |
237 |
through an opaque BYTE that the card minidriver maintains. This BYTE should be |
238 |
associated with a handle to a card-side agreement.*/ |
239 |
|
240 |
DWORD WINAPI CardConstructDHAgreement( |
241 |
__in PCARD_DATA pCardData, |
242 |
__inout PCARD_DH_AGREEMENT_INFO pSecretInfo |
243 |
) |
244 |
{ |
245 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
246 |
return SCARD_E_UNSUPPORTED_FEATURE; |
247 |
} |
248 |
|
249 |
/** The key derivation structure represents the majority of the required changes |
250 |
for FIPS 140-2 compliance for smart cards. It holds the requested key derivation |
251 |
function (KDF) and the associated input. The KDFs are defined in the CNG Reference |
252 |
documentation on MSDN. For RSA-only card minidrivers, this entry point is not defined |
253 |
and is set to NULL in the CARD_DATA structure that is returned from CardAcquireContext.*/ |
254 |
|
255 |
DWORD WINAPI CardDeriveKey( |
256 |
__in PCARD_DATA pCardData, |
257 |
__inout PCARD_DERIVE_KEY pAgreementInfo |
258 |
) |
259 |
{ |
260 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
261 |
return SCARD_E_UNSUPPORTED_FEATURE; |
262 |
} |
263 |
|
264 |
/** The CardDestroyDHAgreement function removes an agreed secret from the card. |
265 |
For RSA-only card minidrivers, this entry point is not defined and is set to |
266 |
NULL in the CARD_DATA structure that was returned from CardAcquireContext.*/ |
267 |
|
268 |
DWORD WINAPI CardDestroyDHAgreement( |
269 |
__in PCARD_DATA pCardData, |
270 |
__in BYTE bSecretAgreementIndex, |
271 |
__in DWORD dwFlags |
272 |
) |
273 |
{ |
274 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
275 |
return SCARD_E_UNSUPPORTED_FEATURE; |
276 |
} |