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.6 Key Container |
25 |
|
26 |
/** The CardCreateContainer function creates a new key container that is |
27 |
identified by the container index that the bContainerIndex argument specifies. |
28 |
For applications in which the card does not support on-card key generation or |
29 |
if it is desired to archive the keys, the key material can be supplied with |
30 |
the call by specifying in flags that the card is to import the supplied key material.*/ |
31 |
|
32 |
DWORD WINAPI CardCreateContainer( |
33 |
__in PCARD_DATA pCardData, |
34 |
__in BYTE bContainerIndex, |
35 |
__in DWORD dwFlags, |
36 |
__in DWORD dwKeySpec, |
37 |
__in DWORD dwKeySize, |
38 |
__in PBYTE pbKeyData |
39 |
) |
40 |
{ |
41 |
DWORD dwReturn = 0; |
42 |
POPENPGP_CONTEXT pContext = NULL; |
43 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter bContainerIndex=%d",bContainerIndex); |
44 |
__try |
45 |
{ |
46 |
if ( pCardData == NULL ) |
47 |
{ |
48 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
49 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
50 |
__leave; |
51 |
} |
52 |
if (bContainerIndex >= MaxContainer) |
53 |
{ |
54 |
Trace(WINEVENT_LEVEL_ERROR, L"bContainerIndex == %d",bContainerIndex); |
55 |
dwReturn = SCARD_E_NO_KEY_CONTAINER; |
56 |
__leave; |
57 |
} |
58 |
if (Containers[bContainerIndex].dwKeySpec != dwKeySpec) |
59 |
{ |
60 |
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d",dwKeySpec); |
61 |
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
62 |
__leave; |
63 |
} |
64 |
// controls are done in CardCreateContainerEx |
65 |
dwReturn = CardCreateContainerEx(pCardData, |
66 |
bContainerIndex, |
67 |
dwFlags, |
68 |
dwKeySpec, |
69 |
dwKeySize, |
70 |
pbKeyData, |
71 |
Containers[bContainerIndex].PinId); |
72 |
} |
73 |
__finally |
74 |
{ |
75 |
} |
76 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
77 |
return dwReturn; |
78 |
} |
79 |
|
80 |
/** The CardCreateContainerEx function creates a new key container that the |
81 |
container index identifies and the bContainerIndex parameter specifies. The function |
82 |
associates the key container with the PIN that the PinId parameter specified. |
83 |
This function is useful if the card-edge does not allow for changing the key attributes |
84 |
after the key container is created. This function replaces the need to call |
85 |
CardSetContainerProperty to set the CCP_PIN_IDENTIFIER property CardCreateContainer |
86 |
is called. |
87 |
The caller of this function can provide the key material that the card imports. |
88 |
This is useful in those situations in which the card either does not support internal |
89 |
key generation or the caller requests that the key be archived in the card.*/ |
90 |
|
91 |
DWORD WINAPI CardCreateContainerEx( |
92 |
__in PCARD_DATA pCardData, |
93 |
__in BYTE bContainerIndex, |
94 |
__in DWORD dwFlags, |
95 |
__in DWORD dwKeySpec, |
96 |
__in DWORD dwKeySize, |
97 |
__in PBYTE pbKeyData, |
98 |
__in PIN_ID PinId |
99 |
) |
100 |
{ |
101 |
DWORD dwReturn = 0; |
102 |
POPENPGP_CONTEXT pContext = NULL; |
103 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter bContainerIndex=%d",bContainerIndex); |
104 |
__try |
105 |
{ |
106 |
if ( pCardData == NULL ) |
107 |
{ |
108 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
109 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
110 |
__leave; |
111 |
} |
112 |
if (bContainerIndex >= MaxContainer) |
113 |
{ |
114 |
Trace(WINEVENT_LEVEL_ERROR, L"bContainerIndex == %d",bContainerIndex); |
115 |
dwReturn = SCARD_E_NO_KEY_CONTAINER; |
116 |
__leave; |
117 |
} |
118 |
if (Containers[bContainerIndex].dwKeySpec != dwKeySpec) |
119 |
{ |
120 |
Trace(WINEVENT_LEVEL_ERROR, L"dwKeySpec == %d",dwKeySpec); |
121 |
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
122 |
__leave; |
123 |
} |
124 |
if (Containers[bContainerIndex].PinId != PinId) |
125 |
{ |
126 |
Trace(WINEVENT_LEVEL_ERROR, L"PinId == %d",PinId); |
127 |
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
128 |
__leave; |
129 |
} |
130 |
dwReturn = CheckContext(pCardData); |
131 |
if (dwReturn) |
132 |
{ |
133 |
__leave; |
134 |
} |
135 |
pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific; |
136 |
if (pContext->fIsReadOnly) |
137 |
{ |
138 |
dwReturn = SCARD_E_UNSUPPORTED_FEATURE; |
139 |
Trace(WINEVENT_LEVEL_ERROR, L"Readonly card"); |
140 |
__leave; |
141 |
} |
142 |
if (dwFlags == CARD_CREATE_CONTAINER_KEY_GEN) |
143 |
{ |
144 |
dwReturn = OCardCreateKey(pCardData, bContainerIndex, dwKeySize); |
145 |
} |
146 |
else if (dwFlags == CARD_CREATE_CONTAINER_KEY_IMPORT) |
147 |
{ |
148 |
if (pbKeyData == NULL) |
149 |
{ |
150 |
Trace(WINEVENT_LEVEL_ERROR, L"pbKeyData == NULL"); |
151 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
152 |
__leave; |
153 |
} |
154 |
dwReturn = OCardImportKey(pCardData, bContainerIndex, pbKeyData, dwKeySize); |
155 |
} |
156 |
else |
157 |
{ |
158 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags == %d",dwFlags); |
159 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
160 |
__leave; |
161 |
} |
162 |
} |
163 |
__finally |
164 |
{ |
165 |
} |
166 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
167 |
return dwReturn; |
168 |
} |
169 |
|
170 |
/** The CardDeleteContainer function deletes the key container specified by its index value. |
171 |
This is done by deleting all key material (public and private) that is associated with |
172 |
that index value.*/ |
173 |
|
174 |
DWORD WINAPI CardDeleteContainer( |
175 |
__in PCARD_DATA pCardData, |
176 |
__in BYTE bContainerIndex, |
177 |
__in DWORD dwReserved |
178 |
) |
179 |
{ |
180 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter"); |
181 |
return SCARD_E_UNSUPPORTED_FEATURE; |
182 |
} |
183 |
|
184 |
/** The CardGetContainerInfo function queries the specified key container for more |
185 |
information about which keys are present, such as its key specification (such as AT_ECDSA_P384).*/ |
186 |
|
187 |
DWORD WINAPI CardGetContainerInfo( |
188 |
__in PCARD_DATA pCardData, |
189 |
__in BYTE bContainerIndex, |
190 |
__in DWORD dwFlags, |
191 |
__inout PCONTAINER_INFO pContainerInfo |
192 |
) |
193 |
{ |
194 |
DWORD dwReturn = 0, dwVersion; |
195 |
Trace(WINEVENT_LEVEL_VERBOSE, L"Enter bContainerIndex=%d",bContainerIndex); |
196 |
__try |
197 |
{ |
198 |
if ( pCardData == NULL ) |
199 |
{ |
200 |
Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL"); |
201 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
202 |
__leave; |
203 |
} |
204 |
if ( pContainerInfo == NULL ) |
205 |
{ |
206 |
Trace(WINEVENT_LEVEL_ERROR, L"pContainerInfo == NULL"); |
207 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
208 |
__leave; |
209 |
} |
210 |
dwVersion = (pContainerInfo->dwVersion == 0) ? 1 : pContainerInfo->dwVersion; |
211 |
if ( dwVersion != CONTAINER_INFO_CURRENT_VERSION ) |
212 |
{ |
213 |
Trace(WINEVENT_LEVEL_ERROR, L"dwVersion == %d", pContainerInfo->dwVersion); |
214 |
dwReturn = ERROR_REVISION_MISMATCH; |
215 |
__leave; |
216 |
} |
217 |
if ( dwFlags ) |
218 |
{ |
219 |
Trace(WINEVENT_LEVEL_ERROR, L"dwFlags == %d", dwFlags); |
220 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
221 |
__leave; |
222 |
} |
223 |
if (bContainerIndex >= MaxContainer) |
224 |
{ |
225 |
Trace(WINEVENT_LEVEL_ERROR, L"bContainerIndex == %d",bContainerIndex); |
226 |
dwReturn = SCARD_E_NO_KEY_CONTAINER; |
227 |
__leave; |
228 |
} |
229 |
dwReturn = CheckContext(pCardData); |
230 |
if (dwReturn) |
231 |
{ |
232 |
__leave; |
233 |
} |
234 |
pContainerInfo->pbSigPublicKey = NULL; |
235 |
pContainerInfo->pbKeyExPublicKey = NULL; |
236 |
pContainerInfo->cbSigPublicKey = 0; |
237 |
pContainerInfo->cbKeyExPublicKey = 0; |
238 |
switch(bContainerIndex) |
239 |
{ |
240 |
case Signature: |
241 |
case Authentication: |
242 |
dwReturn = OCardReadPublicKey(pCardData, bContainerIndex, |
243 |
&(pContainerInfo->pbSigPublicKey),&(pContainerInfo->cbSigPublicKey)); |
244 |
break; |
245 |
case Confidentiality: |
246 |
dwReturn = OCardReadPublicKey(pCardData, bContainerIndex, |
247 |
&(pContainerInfo->pbKeyExPublicKey),&(pContainerInfo->cbKeyExPublicKey)); |
248 |
break; |
249 |
} |
250 |
} |
251 |
__finally |
252 |
{ |
253 |
} |
254 |
Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn); |
255 |
return dwReturn; |
256 |
} |