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