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 <tchar.h> |
20 |
#include <cardmod.h> |
21 |
#include "global.h" |
22 |
|
23 |
DWORD Authenticate(PSTR szPin, PWSTR wszUserId, PDWORD pcAttemptsRemaining) |
24 |
{ |
25 |
DWORD cbPin = (DWORD) strlen(szPin); |
26 |
DWORD dwReturn; |
27 |
__try |
28 |
{ |
29 |
if (!pCardData) |
30 |
{ |
31 |
dwReturn = SCARD_E_COMM_DATA_LOST; |
32 |
__leave; |
33 |
} |
34 |
|
35 |
dwReturn = pCardData->pfnCardAuthenticatePin( |
36 |
pCardData, |
37 |
wszUserId, |
38 |
(PBYTE) szPin, |
39 |
cbPin, |
40 |
pcAttemptsRemaining); |
41 |
} |
42 |
__finally |
43 |
{ |
44 |
} |
45 |
|
46 |
return dwReturn; |
47 |
} |
48 |
|
49 |
DWORD ChangePin(PSTR szPin, PSTR szPin2, PWSTR wszUserId, PDWORD pcAttemptsRemaining) |
50 |
{ |
51 |
DWORD cbPin = (DWORD) strlen(szPin); |
52 |
DWORD cbPin2 = (DWORD) strlen(szPin2); |
53 |
DWORD dwReturn; |
54 |
__try |
55 |
{ |
56 |
if (!pCardData) |
57 |
{ |
58 |
dwReturn = SCARD_E_COMM_DATA_LOST; |
59 |
__leave; |
60 |
} |
61 |
|
62 |
dwReturn = pCardData->pfnCardChangeAuthenticator( |
63 |
pCardData, |
64 |
wszUserId, |
65 |
(PBYTE) szPin, |
66 |
cbPin, |
67 |
(PBYTE) szPin2, |
68 |
cbPin2, |
69 |
0,CARD_AUTHENTICATE_PIN_PIN, |
70 |
pcAttemptsRemaining); |
71 |
} |
72 |
__finally |
73 |
{ |
74 |
} |
75 |
|
76 |
return dwReturn; |
77 |
} |
78 |
|
79 |
DWORD SetPuk(PSTR szPin, PSTR szPin2, PDWORD pcAttemptsRemaining) |
80 |
{ |
81 |
DWORD cbPin = (DWORD) strlen(szPin); |
82 |
DWORD cbPin2 = (DWORD) strlen(szPin2); |
83 |
DWORD dwReturn; |
84 |
__try |
85 |
{ |
86 |
if (!pCardData) |
87 |
{ |
88 |
dwReturn = SCARD_E_COMM_DATA_LOST; |
89 |
__leave; |
90 |
} |
91 |
|
92 |
dwReturn = pCardData->pfnCardChangeAuthenticatorEx( |
93 |
pCardData, |
94 |
PIN_CHANGE_FLAG_CHANGEPIN, ROLE_ADMIN, |
95 |
(PBYTE) szPin, |
96 |
cbPin, |
97 |
4, |
98 |
(PBYTE) szPin2, |
99 |
cbPin2, |
100 |
0, |
101 |
pcAttemptsRemaining); |
102 |
} |
103 |
__finally |
104 |
{ |
105 |
} |
106 |
|
107 |
return dwReturn; |
108 |
} |
109 |
|
110 |
BYTE CharToByte(BYTE b) |
111 |
{ |
112 |
if (b >= 0x30 && b <= 0x39) |
113 |
{ |
114 |
return b - 0x30; |
115 |
} |
116 |
if (b >= 0x41 && b <= 0x46) |
117 |
{ |
118 |
return b - 0x37; |
119 |
} |
120 |
if (b >= 0x61 && b <= 0x66) |
121 |
{ |
122 |
return b - 0x57; |
123 |
} |
124 |
return 0xFF; |
125 |
} |
126 |
|
127 |
DWORD SetSM(PSTR szPin, PSTR szPin2, PDWORD pcAttemptsRemaining) |
128 |
{ |
129 |
DWORD cbPin = (DWORD) strlen(szPin); |
130 |
DWORD cbPin2 = (DWORD) strlen(szPin2); |
131 |
DWORD dwReturn; |
132 |
BYTE bBuffer[24]; |
133 |
BYTE bTagBuffer[2 + 2 + 24 + 2 +24]; |
134 |
DWORD dwBufferSize, dwI; |
135 |
BOOL fSet = FALSE; |
136 |
__try |
137 |
{ |
138 |
if (!pCardData) |
139 |
{ |
140 |
dwReturn = SCARD_E_COMM_DATA_LOST; |
141 |
__leave; |
142 |
} |
143 |
if (cbPin2 % 2 || cbPin2 / 2 > ARRAYSIZE(bBuffer)) |
144 |
{ |
145 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
146 |
__leave; |
147 |
} |
148 |
dwBufferSize = cbPin2 / 2; |
149 |
if (dwBufferSize != 24 && dwBufferSize != 16) |
150 |
{ |
151 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
152 |
__leave; |
153 |
} |
154 |
for(dwI = 0; dwI < cbPin2 / 2; dwI++) |
155 |
{ |
156 |
BYTE b1, b2; |
157 |
b1 = szPin2[dwI * 2]; |
158 |
b2 = szPin2[dwI * 2 + 1]; |
159 |
b1 = CharToByte(b1); |
160 |
b2 = CharToByte(b2); |
161 |
if (b1 == 0xFF || b2 == 0xFF) |
162 |
{ |
163 |
dwReturn = SCARD_E_INVALID_PARAMETER; |
164 |
__leave; |
165 |
} |
166 |
bBuffer[dwI] = (BYTE)(b1) * 16 + (b2); |
167 |
} |
168 |
bTagBuffer[0] = 0x4d; |
169 |
bTagBuffer[1] = (BYTE) dwBufferSize * 2 + 2 * 2; |
170 |
bTagBuffer[2] = 0xD1; |
171 |
bTagBuffer[3] = (BYTE) dwBufferSize; |
172 |
memcpy(bTagBuffer + 4, bBuffer, dwBufferSize); |
173 |
bTagBuffer[2 + 2 + dwBufferSize] = 0xD2; |
174 |
bTagBuffer[3 + 2 + dwBufferSize] = (BYTE) dwBufferSize; |
175 |
memcpy(bTagBuffer + 4 + 2 + dwBufferSize, bBuffer, dwBufferSize); |
176 |
dwReturn = pCardData->pfnCardAuthenticateEx( |
177 |
pCardData, |
178 |
ROLE_ADMIN,0, |
179 |
(PBYTE) szPin, |
180 |
cbPin, NULL,NULL, |
181 |
pcAttemptsRemaining); |
182 |
if (dwReturn) |
183 |
{ |
184 |
__leave; |
185 |
} |
186 |
dwReturn = pCardData->pfnCardSetProperty(pCardData, CP_CARD_READ_ONLY, (PBYTE) &fSet, sizeof(BOOL),0); |
187 |
if (dwReturn) __leave; |
188 |
//dwReturn = pCardData->pfnCardWriteFile(pCardData, "openpgp", "smenc", 0, bBuffer, dwBufferSize); |
189 |
dwReturn = pCardData->pfnCardWriteFile(pCardData, "openpgp", "sm", 0, bTagBuffer, 6 + 2*dwBufferSize); |
190 |
if (dwReturn) __leave; |
191 |
|
192 |
} |
193 |
__finally |
194 |
{ |
195 |
} |
196 |
|
197 |
return dwReturn; |
198 |
} |
199 |
|
200 |
DWORD ResetPin(PSTR szPin, PSTR szPin2, BOOL fIsPUK, PDWORD pcAttemptsRemaining) |
201 |
{ |
202 |
DWORD cbPin = (DWORD) strlen(szPin); |
203 |
DWORD cbPin2 = (DWORD) strlen(szPin2); |
204 |
DWORD dwReturn; |
205 |
__try |
206 |
{ |
207 |
if (!pCardData) |
208 |
{ |
209 |
dwReturn = SCARD_E_COMM_DATA_LOST; |
210 |
__leave; |
211 |
} |
212 |
if (fIsPUK) |
213 |
{ |
214 |
dwReturn = pCardData->pfnCardChangeAuthenticatorEx( |
215 |
pCardData, |
216 |
PIN_CHANGE_FLAG_UNBLOCK, 5, |
217 |
(PBYTE) szPin,cbPin, |
218 |
ROLE_USER, (PBYTE)szPin2, cbPin2, 0, |
219 |
pcAttemptsRemaining); |
220 |
} |
221 |
else |
222 |
{ |
223 |
dwReturn = pCardData->pfnCardChangeAuthenticatorEx( |
224 |
pCardData, |
225 |
PIN_CHANGE_FLAG_UNBLOCK, ROLE_ADMIN, |
226 |
(PBYTE) szPin,cbPin, |
227 |
ROLE_USER, (PBYTE)szPin2, cbPin2, 0, |
228 |
pcAttemptsRemaining); |
229 |
} |
230 |
} |
231 |
__finally |
232 |
{ |
233 |
} |
234 |
|
235 |
return dwReturn; |
236 |
} |