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