1 |
vletoux |
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 |
|
|
dwReturn = SCardSign(pCardData, pInfo); |
134 |
|
|
} |
135 |
|
|
__finally |
136 |
|
|
{ |
137 |
|
|
} |
138 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
139 |
|
|
return dwReturn; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
/** This function returns the public key sizes that are supported by the card in use.*/ |
143 |
|
|
DWORD WINAPI CardQueryKeySizes( |
144 |
|
|
__in PCARD_DATA pCardData, |
145 |
|
|
__in DWORD dwKeySpec, |
146 |
|
|
__in DWORD dwFlags, |
147 |
|
|
__inout PCARD_KEY_SIZES pKeySizes |
148 |
|
|
) |
149 |
|
|
{ |
150 |
|
|
DWORD dwReturn = 0, dwVersion; |
151 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
152 |
|
|
__try |
153 |
|
|
{ |
154 |
|
|
if ( pCardData == NULL ) |
155 |
|
|
{ |
156 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
157 |
|
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
158 |
|
|
__leave; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
if ( dwFlags != 0 ) |
162 |
|
|
{ |
163 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags != 0 : %d", dwFlags); |
164 |
|
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
165 |
|
|
__leave; |
166 |
|
|
} |
167 |
|
|
if ( pKeySizes == NULL ) |
168 |
|
|
{ |
169 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"pKeySizes == NULL"); |
170 |
|
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
171 |
|
|
__leave; |
172 |
|
|
} |
173 |
|
|
dwVersion = (pKeySizes->dwVersion == 0) ? 1 : pKeySizes->dwVersion; |
174 |
|
|
if ( dwVersion != CARD_KEY_SIZES_CURRENT_VERSION ) |
175 |
|
|
{ |
176 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"dwVersion == %d", pKeySizes->dwVersion); |
177 |
|
|
dwReturn = ERROR_REVISION_MISMATCH; |
178 |
|
|
__leave; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
switch(dwKeySpec) |
182 |
|
|
{ |
183 |
|
|
case AT_ECDHE_P256 : |
184 |
|
|
case AT_ECDHE_P384 : |
185 |
|
|
case AT_ECDHE_P521 : |
186 |
|
|
case AT_ECDSA_P256 : |
187 |
|
|
case AT_ECDSA_P384 : |
188 |
|
|
case AT_ECDSA_P521 : |
189 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d", dwKeySpec); |
190 |
|
|
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
191 |
|
|
__leave; |
192 |
|
|
break; |
193 |
|
|
case AT_KEYEXCHANGE: |
194 |
|
|
case AT_SIGNATURE : |
195 |
|
|
break; |
196 |
|
|
default: |
197 |
|
|
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d", dwKeySpec); |
198 |
|
|
dwReturn = SCARD_E_INVALID_PARAMETER; |
199 |
|
|
__leave; |
200 |
|
|
break; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
pKeySizes->dwMinimumBitlen = 1024; |
204 |
|
|
pKeySizes->dwDefaultBitlen = 2048; |
205 |
|
|
pKeySizes->dwMaximumBitlen = 2048; |
206 |
|
|
pKeySizes->dwIncrementalBitlen = 0; |
207 |
|
|
} |
208 |
|
|
__finally |
209 |
|
|
{ |
210 |
|
|
} |
211 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
212 |
|
|
return dwReturn; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
|
216 |
|
|
/** The CardConstructDHAgreement function performs a secret agreement calculation |
217 |
|
|
for Diffie Hellman (DH) key exchange by using a private key that is present on the |
218 |
|
|
card. For RSA-only card minidrivers, this entry point is not defined and is set to |
219 |
|
|
NULL in the CARD_DATA structure that is returned from CardAcquireContext. |
220 |
|
|
The CARD_DH_AGREEMENT structure changes to allow for return of a handle to |
221 |
|
|
the agreed secret. This raises a point about how to index the DH agreement |
222 |
|
|
on the card in an opaque manner. Maintaining a map file is unnecessary because |
223 |
|
|
Ncrypt makes no provision for persistent DH agreements and there is no way to |
224 |
|
|
retrieve one after a provider is closed. DH agreements are addressable on card |
225 |
|
|
through an opaque BYTE that the card minidriver maintains. This BYTE should be |
226 |
|
|
associated with a handle to a card-side agreement.*/ |
227 |
|
|
|
228 |
|
|
DWORD WINAPI CardConstructDHAgreement( |
229 |
|
|
__in PCARD_DATA pCardData, |
230 |
|
|
__inout PCARD_DH_AGREEMENT_INFO pSecretInfo |
231 |
|
|
) |
232 |
|
|
{ |
233 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
234 |
|
|
return SCARD_E_UNSUPPORTED_FEATURE; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
/** The key derivation structure represents the majority of the required changes |
238 |
|
|
for FIPS 140-2 compliance for smart cards. It holds the requested key derivation |
239 |
|
|
function (KDF) and the associated input. The KDFs are defined in the CNG Reference |
240 |
|
|
documentation on MSDN. For RSA-only card minidrivers, this entry point is not defined |
241 |
|
|
and is set to NULL in the CARD_DATA structure that is returned from CardAcquireContext.*/ |
242 |
|
|
|
243 |
|
|
DWORD WINAPI CardDeriveKey( |
244 |
|
|
__in PCARD_DATA pCardData, |
245 |
|
|
__inout PCARD_DERIVE_KEY pAgreementInfo |
246 |
|
|
) |
247 |
|
|
{ |
248 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
249 |
|
|
return SCARD_E_UNSUPPORTED_FEATURE; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/** The CardDestroyDHAgreement function removes an agreed secret from the card. |
253 |
|
|
For RSA-only card minidrivers, this entry point is not defined and is set to |
254 |
|
|
NULL in the CARD_DATA structure that was returned from CardAcquireContext.*/ |
255 |
|
|
|
256 |
|
|
DWORD WINAPI CardDestroyDHAgreement( |
257 |
|
|
__in PCARD_DATA pCardData, |
258 |
|
|
__in BYTE bSecretAgreementIndex, |
259 |
|
|
__in DWORD dwFlags |
260 |
|
|
) |
261 |
|
|
{ |
262 |
|
|
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
263 |
|
|
return SCARD_E_UNSUPPORTED_FEATURE; |
264 |
|
|
} |