/src/opensc/src/libopensc/pkcs15-pin.c
Line | Count | Source |
1 | | /* |
2 | | * pkcs15-pin.c: PKCS #15 PIN functions |
3 | | * |
4 | | * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #ifdef HAVE_CONFIG_H |
22 | | #include "config.h" |
23 | | #endif |
24 | | |
25 | | #include <string.h> |
26 | | #include <stdlib.h> |
27 | | #include <stdio.h> |
28 | | |
29 | | #include "internal.h" |
30 | | #include "asn1.h" |
31 | | #include "pkcs15.h" |
32 | | #include "ui/notify.h" |
33 | | |
34 | | int _sc_pkcs15_verify_pin(struct sc_pkcs15_card *, struct sc_pkcs15_object *, |
35 | | const unsigned char *, size_t); |
36 | | |
37 | | static const struct sc_asn1_entry c_asn1_com_ao_attr[] = { |
38 | | { "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
39 | | { NULL, 0, 0, 0, NULL, NULL } |
40 | | }; |
41 | | |
42 | | /* PIN attributes */ |
43 | | static const struct sc_asn1_entry c_asn1_pin_attr[] = { |
44 | | { "pinFlags", SC_ASN1_BIT_FIELD, SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL }, |
45 | | { "pinType", SC_ASN1_ENUMERATED, SC_ASN1_TAG_ENUMERATED, 0, NULL, NULL }, |
46 | | { "minLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL }, |
47 | | { "storedLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL }, |
48 | | { "maxLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL }, |
49 | | { "pinReference", SC_ASN1_INTEGER, SC_ASN1_CTX | 0, SC_ASN1_OPTIONAL, NULL, NULL }, |
50 | | { "padChar", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL }, |
51 | | { "lastPinChange",SC_ASN1_GENERALIZEDTIME, SC_ASN1_TAG_GENERALIZEDTIME, SC_ASN1_OPTIONAL, NULL, NULL }, |
52 | | { "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
53 | | { NULL, 0, 0, 0, NULL, NULL } |
54 | | }; |
55 | | static const struct sc_asn1_entry c_asn1_type_pin_attr[] = { |
56 | | { "pinAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
57 | | { NULL, 0, 0, 0, NULL, NULL } |
58 | | }; |
59 | | |
60 | | /* Auth Key attributes */ |
61 | | static const struct sc_asn1_entry c_asn1_authkey_attr[] = { |
62 | | { "derivedKey", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL }, |
63 | | { "authKeyId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL }, |
64 | | { NULL, 0, 0, 0, NULL, NULL } |
65 | | }; |
66 | | static const struct sc_asn1_entry c_asn1_type_authkey_attr[] = { |
67 | | { "authKeyAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL }, |
68 | | { NULL, 0, 0, 0, NULL, NULL } |
69 | | }; |
70 | | static const struct sc_asn1_entry c_asn1_auth_type[] = { |
71 | | { "authType", SC_ASN1_CHOICE, 0, 0, NULL, NULL }, |
72 | | { NULL, 0, 0, 0, NULL, NULL } |
73 | | }; |
74 | | static const struct sc_asn1_entry c_asn1_auth_type_choice[] = { |
75 | | { "pin", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
76 | | { "biometricTemplate", SC_ASN1_PKCS15_OBJECT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
77 | | { "authKey", SC_ASN1_PKCS15_OBJECT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL }, |
78 | | { NULL, 0, 0, 0, NULL, NULL } |
79 | | }; |
80 | | |
81 | | |
82 | | int |
83 | | sc_pkcs15_decode_aodf_entry(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj, |
84 | | const u8 ** buf, size_t *buflen) |
85 | 4 | { |
86 | 4 | sc_context_t *ctx = p15card->card->ctx; |
87 | 4 | struct sc_pkcs15_auth_info info; |
88 | 4 | int r; |
89 | 4 | size_t flags_len = sizeof(info.attrs.pin.flags); |
90 | 4 | size_t derived_len = sizeof(info.attrs.authkey.derived); |
91 | 4 | size_t padchar_len = 1; |
92 | 4 | struct sc_asn1_entry asn1_com_ao_attr[2]; |
93 | 4 | struct sc_asn1_entry asn1_pin_attr[10], asn1_type_pin_attr[2]; |
94 | 4 | struct sc_asn1_entry asn1_authkey_attr[3], asn1_type_authkey_attr[2]; |
95 | 4 | struct sc_asn1_entry asn1_auth_type[2]; |
96 | 4 | struct sc_asn1_entry asn1_auth_type_choice[4]; |
97 | 4 | struct sc_asn1_pkcs15_object pin_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_pin_attr }; |
98 | 4 | struct sc_asn1_pkcs15_object authkey_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_authkey_attr }; |
99 | | |
100 | 4 | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_ASN1); |
101 | | |
102 | 4 | sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type); |
103 | 4 | sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice); |
104 | | |
105 | 4 | sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr); |
106 | | |
107 | 4 | sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr); |
108 | 4 | sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr); |
109 | | |
110 | 4 | sc_copy_asn1_entry(c_asn1_type_authkey_attr, asn1_type_authkey_attr); |
111 | 4 | sc_copy_asn1_entry(c_asn1_authkey_attr, asn1_authkey_attr); |
112 | | |
113 | 4 | sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 0); |
114 | 4 | sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 0); /* 'pin' */ |
115 | 4 | sc_format_asn1_entry(asn1_auth_type_choice + 2, &authkey_obj, NULL, 0); /* 'authKey' */ |
116 | | |
117 | | /* pinAttributes */ |
118 | 4 | sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 0); |
119 | 4 | sc_format_asn1_entry(asn1_pin_attr + 0, &info.attrs.pin.flags, &flags_len, 0); |
120 | 4 | sc_format_asn1_entry(asn1_pin_attr + 1, &info.attrs.pin.type, NULL, 0); |
121 | 4 | sc_format_asn1_entry(asn1_pin_attr + 2, &info.attrs.pin.min_length, NULL, 0); |
122 | 4 | sc_format_asn1_entry(asn1_pin_attr + 3, &info.attrs.pin.stored_length, NULL, 0); |
123 | 4 | sc_format_asn1_entry(asn1_pin_attr + 4, &info.attrs.pin.max_length, NULL, 0); |
124 | 4 | sc_format_asn1_entry(asn1_pin_attr + 5, &info.attrs.pin.reference, NULL, 0); |
125 | 4 | sc_format_asn1_entry(asn1_pin_attr + 6, &info.attrs.pin.pad_char, &padchar_len, 0); |
126 | | |
127 | | /* authKeyAttributes */ |
128 | 4 | sc_format_asn1_entry(asn1_type_authkey_attr + 0, asn1_authkey_attr, NULL, 0); |
129 | 4 | sc_format_asn1_entry(asn1_authkey_attr + 0, &info.attrs.authkey.derived, &derived_len, 0); |
130 | 4 | sc_format_asn1_entry(asn1_authkey_attr + 1, &info.attrs.authkey.skey_id, NULL, 0); |
131 | | |
132 | | /* We don't support lastPinChange yet. */ |
133 | 4 | sc_format_asn1_entry(asn1_pin_attr + 8, &info.path, NULL, 0); |
134 | | |
135 | 4 | sc_format_asn1_entry(asn1_com_ao_attr + 0, &info.auth_id, NULL, 0); |
136 | | |
137 | | /* Fill in defaults */ |
138 | 4 | memset(&info, 0, sizeof(info)); |
139 | 4 | info.tries_left = -1; |
140 | 4 | info.logged_in = SC_PIN_STATE_UNKNOWN; |
141 | | |
142 | 4 | r = sc_asn1_decode(ctx, asn1_auth_type, *buf, *buflen, buf, buflen); |
143 | 4 | if (r == SC_ERROR_ASN1_END_OF_CONTENTS) |
144 | 1 | return r; |
145 | 3 | LOG_TEST_RET(ctx, r, "ASN.1 decoding failed"); |
146 | | |
147 | 1 | if (asn1_auth_type_choice[0].flags & SC_ASN1_PRESENT) { |
148 | 0 | sc_log(ctx, "AuthType: PIN"); |
149 | 0 | obj->type = SC_PKCS15_TYPE_AUTH_PIN; |
150 | 0 | info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN; |
151 | 0 | info.auth_method = SC_AC_CHV; |
152 | |
|
153 | 0 | if (info.attrs.pin.max_length == 0) { |
154 | 0 | if (p15card->card->max_pin_len != 0) |
155 | 0 | info.attrs.pin.max_length = p15card->card->max_pin_len; |
156 | 0 | else if (info.attrs.pin.stored_length != 0) |
157 | 0 | info.attrs.pin.max_length = info.attrs.pin.type != SC_PKCS15_PIN_TYPE_BCD ? |
158 | 0 | info.attrs.pin.stored_length : 2 * info.attrs.pin.stored_length; |
159 | 0 | else |
160 | 0 | info.attrs.pin.max_length = 8; /* shouldn't happen */ |
161 | 0 | } |
162 | | |
163 | | /* OpenSC 0.11.4 and older encoded "pinReference" as a negative |
164 | | value. Fixed in 0.11.5 we need to add a hack, so old cards |
165 | | continue to work. |
166 | | The same invalid encoding has some models of the proprietary PKCS#15 cards. |
167 | | */ |
168 | 0 | if (info.attrs.pin.reference < 0) |
169 | 0 | info.attrs.pin.reference += 256; |
170 | |
|
171 | 0 | if (info.attrs.pin.flags & SC_PKCS15_PIN_FLAG_LOCAL) { |
172 | | /* In OpenSC pkcs#15 framework 'path' is mandatory for the 'Local' PINs. |
173 | | * If 'path' do not present in PinAttributes, derive it from the PKCS#15 context. */ |
174 | 0 | if (!info.path.len) { |
175 | | /* Give priority to AID defined in the application DDO */ |
176 | 0 | if (p15card->app && p15card->app->ddo.aid.len) |
177 | 0 | info.path.aid = p15card->app->ddo.aid; |
178 | 0 | else if (p15card->file_app && p15card->file_app->path.len) |
179 | 0 | info.path = p15card->file_app->path; |
180 | 0 | else |
181 | 0 | return SC_ERROR_INTERNAL; |
182 | 0 | } |
183 | 0 | } |
184 | 0 | sc_debug(ctx, SC_LOG_DEBUG_ASN1, "decoded PIN(ref:%X,path:%s)", info.attrs.pin.reference, sc_print_path(&info.path)); |
185 | 0 | } |
186 | 1 | else if (asn1_auth_type_choice[1].flags & SC_ASN1_PRESENT) { |
187 | 1 | LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "BIO authentication object not yet supported"); |
188 | 1 | } |
189 | 0 | else if (asn1_auth_type_choice[2].flags & SC_ASN1_PRESENT) { |
190 | 0 | sc_log(ctx, "AuthType: AuthKey"); |
191 | 0 | obj->type = SC_PKCS15_TYPE_AUTH_AUTHKEY; |
192 | 0 | info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY; |
193 | 0 | info.auth_method = SC_AC_AUT; |
194 | 0 | if (!(asn1_authkey_attr[0].flags & SC_ASN1_PRESENT)) |
195 | 0 | info.attrs.authkey.derived = 1; |
196 | 0 | } |
197 | 0 | else { |
198 | 0 | LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "unknown authentication type"); |
199 | 0 | } |
200 | | |
201 | 0 | obj->data = malloc(sizeof(info)); |
202 | 0 | if (obj->data == NULL) |
203 | 0 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
204 | 0 | memcpy(obj->data, &info, sizeof(info)); |
205 | |
|
206 | 0 | SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_ASN1, SC_SUCCESS); |
207 | 0 | } |
208 | | |
209 | | int sc_pkcs15_encode_aodf_entry(sc_context_t *ctx, |
210 | | const struct sc_pkcs15_object *obj, |
211 | | u8 **buf, size_t *buflen) |
212 | 0 | { |
213 | 0 | struct sc_asn1_entry asn1_com_ao_attr[2], asn1_pin_attr[10], asn1_type_pin_attr[2]; |
214 | 0 | struct sc_asn1_entry asn1_auth_type[2]; |
215 | 0 | struct sc_asn1_entry asn1_auth_type_choice[4]; |
216 | 0 | struct sc_pkcs15_auth_info *info = (struct sc_pkcs15_auth_info *) obj->data; |
217 | 0 | struct sc_asn1_pkcs15_object pin_obj = { (struct sc_pkcs15_object *) obj, |
218 | 0 | asn1_com_ao_attr, NULL, asn1_type_pin_attr }; |
219 | 0 | int r; |
220 | 0 | size_t flags_len; |
221 | 0 | size_t padchar_len = 1; |
222 | |
|
223 | 0 | if (info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
224 | 0 | return SC_ERROR_NOT_SUPPORTED; |
225 | | |
226 | 0 | sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type); |
227 | 0 | sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice); |
228 | 0 | sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr); |
229 | 0 | sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr); |
230 | 0 | sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr); |
231 | |
|
232 | 0 | sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 1); |
233 | 0 | sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 1); |
234 | |
|
235 | 0 | sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 1); |
236 | |
|
237 | 0 | flags_len = sizeof(info->attrs.pin.flags); |
238 | 0 | sc_format_asn1_entry(asn1_pin_attr + 0, &info->attrs.pin.flags, &flags_len, 1); |
239 | 0 | sc_format_asn1_entry(asn1_pin_attr + 1, &info->attrs.pin.type, NULL, 1); |
240 | 0 | sc_format_asn1_entry(asn1_pin_attr + 2, &info->attrs.pin.min_length, NULL, 1); |
241 | 0 | sc_format_asn1_entry(asn1_pin_attr + 3, &info->attrs.pin.stored_length, NULL, 1); |
242 | 0 | if (info->attrs.pin.max_length > 0) |
243 | 0 | sc_format_asn1_entry(asn1_pin_attr + 4, &info->attrs.pin.max_length, NULL, 1); |
244 | 0 | if (info->attrs.pin.reference >= 0) |
245 | 0 | sc_format_asn1_entry(asn1_pin_attr + 5, &info->attrs.pin.reference, NULL, 1); |
246 | | /* FIXME: check if pad_char present */ |
247 | 0 | sc_format_asn1_entry(asn1_pin_attr + 6, &info->attrs.pin.pad_char, &padchar_len, 1); |
248 | 0 | sc_format_asn1_entry(asn1_pin_attr + 8, &info->path, NULL, info->path.len ? 1 : 0); |
249 | |
|
250 | 0 | sc_format_asn1_entry(asn1_com_ao_attr + 0, &info->auth_id, NULL, 1); |
251 | |
|
252 | 0 | r = sc_asn1_encode(ctx, asn1_auth_type, buf, buflen); |
253 | |
|
254 | 0 | return r; |
255 | 0 | } |
256 | | |
257 | | |
258 | | static int |
259 | | _validate_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_auth_info *auth_info, size_t pinlen) |
260 | 258 | { |
261 | 258 | size_t max_length; |
262 | 258 | if (p15card == NULL) { |
263 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
264 | 0 | } |
265 | | |
266 | | /* Ignore validation of the non-PIN authentication objects */ |
267 | 258 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
268 | 0 | return SC_SUCCESS; |
269 | | |
270 | | /* prevent buffer overflow from hostile card */ |
271 | 258 | if (auth_info->attrs.pin.stored_length > SC_MAX_PIN_SIZE) |
272 | 0 | return SC_ERROR_BUFFER_TOO_SMALL; |
273 | | |
274 | | /* if we use pinpad, no more checks are needed */ |
275 | 258 | if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD |
276 | 258 | || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH) |
277 | 0 | && !pinlen) |
278 | 0 | return SC_SUCCESS; |
279 | | |
280 | | /* If pin is given, make sure it is within limits */ |
281 | 258 | max_length = auth_info->attrs.pin.max_length != 0 ? auth_info->attrs.pin.max_length : SC_MAX_PIN_SIZE; |
282 | 258 | if (pinlen > max_length || pinlen < auth_info->attrs.pin.min_length) |
283 | 49 | return SC_ERROR_INVALID_PIN_LENGTH; |
284 | | |
285 | 209 | return SC_SUCCESS; |
286 | 258 | } |
287 | | |
288 | | /* |
289 | | * Verify a PIN. |
290 | | * |
291 | | * If the code given to us has zero length, this means we |
292 | | * should ask the card reader to obtain the PIN from the |
293 | | * reader's PIN pad |
294 | | */ |
295 | | int |
296 | | sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj, |
297 | | const unsigned char *pincode, size_t pinlen) |
298 | 45 | { |
299 | 45 | struct sc_context *ctx = p15card->card->ctx; |
300 | 45 | struct sc_pkcs15_auth_info *auth_info; |
301 | 45 | int r; |
302 | | |
303 | 45 | LOG_FUNC_CALLED(ctx); |
304 | | |
305 | 45 | if (!pin_obj || !pin_obj->data) |
306 | 45 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_PIN_REFERENCE); |
307 | 45 | auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data; |
308 | | |
309 | | /* Check the provided pin matches pin requirements */ |
310 | 45 | r = _validate_pin(p15card, auth_info, pinlen); |
311 | | |
312 | 45 | if (r) |
313 | 45 | LOG_FUNC_RETURN(ctx, r); |
314 | | |
315 | 39 | r = _sc_pkcs15_verify_pin(p15card, pin_obj, pincode, pinlen); |
316 | | |
317 | 39 | if (r == SC_SUCCESS) |
318 | 26 | sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen); |
319 | | |
320 | 39 | LOG_FUNC_RETURN(ctx, r); |
321 | 39 | } |
322 | | |
323 | | |
324 | | int |
325 | | _sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj, |
326 | | const unsigned char *pincode, size_t pinlen) |
327 | 39 | { |
328 | 39 | return sc_pkcs15_verify_pin_with_session_pin(p15card, pin_obj, pincode, |
329 | 39 | pinlen, NULL, NULL); |
330 | 39 | } |
331 | | |
332 | | /* |
333 | | * Verify a PIN and generate a session PIN |
334 | | * |
335 | | * If the code given to us has zero length, this means we |
336 | | * should ask the card reader to obtain the PIN from the |
337 | | * reader's PIN pad |
338 | | */ |
339 | | int sc_pkcs15_verify_pin_with_session_pin(struct sc_pkcs15_card *p15card, |
340 | | struct sc_pkcs15_object *pin_obj, |
341 | | const unsigned char *pincode, size_t pinlen, |
342 | | const unsigned char *sessionpin, size_t *sessionpinlen) |
343 | 137 | { |
344 | 137 | struct sc_context *ctx = p15card->card->ctx; |
345 | 137 | struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data; |
346 | 137 | int r; |
347 | 137 | sc_card_t *card; |
348 | 137 | struct sc_pin_cmd_data data; |
349 | | |
350 | 137 | LOG_FUNC_CALLED(ctx); |
351 | 137 | sc_log(ctx, "PIN(type:%X; method:%X; value(%p:%zu)", |
352 | 137 | auth_info->auth_type, auth_info->auth_method, pincode, pinlen); |
353 | 137 | card = p15card->card; |
354 | | |
355 | 137 | if (pinlen > SC_MAX_PIN_SIZE) { |
356 | 2 | sc_notify_id(card->ctx, &card->reader->atr, p15card, |
357 | 2 | NOTIFY_PIN_BAD); |
358 | 2 | LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "Invalid PIN size"); |
359 | 2 | } |
360 | | |
361 | | /* Initialize arguments */ |
362 | 135 | memset(&data, 0, sizeof(data)); |
363 | 135 | data.pin_type = auth_info->auth_method; |
364 | | |
365 | 135 | if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN) { |
366 | 135 | data.pin_reference = auth_info->attrs.pin.reference; |
367 | 135 | data.pin1.min_length = auth_info->attrs.pin.min_length; |
368 | 135 | data.pin1.max_length = auth_info->attrs.pin.max_length; |
369 | 135 | data.pin1.pad_length = auth_info->attrs.pin.stored_length; |
370 | 135 | data.pin1.pad_char = auth_info->attrs.pin.pad_char; |
371 | 135 | data.pin1.data = pincode; |
372 | 135 | data.pin1.len = pinlen; |
373 | | |
374 | 135 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING) |
375 | 82 | data.flags |= SC_PIN_CMD_NEED_PADDING; |
376 | | |
377 | 135 | switch (auth_info->attrs.pin.type) { |
378 | 0 | case SC_PKCS15_PIN_TYPE_BCD: |
379 | 0 | data.pin1.encoding = SC_PIN_ENCODING_BCD; |
380 | 0 | break; |
381 | 135 | case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC: |
382 | 135 | data.pin1.encoding = SC_PIN_ENCODING_ASCII; |
383 | 135 | break; |
384 | 0 | default: |
385 | | /* assume/hope the card driver knows how to encode the pin */ |
386 | 0 | data.pin1.encoding = 0; |
387 | 135 | } |
388 | 135 | } |
389 | 0 | else if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY) { |
390 | 0 | struct sc_pkcs15_object *skey_obj = NULL; |
391 | 0 | struct sc_pkcs15_id *skey_id = &auth_info->attrs.authkey.skey_id; |
392 | 0 | struct sc_pkcs15_skey_info *skey_info = NULL; |
393 | |
|
394 | 0 | r = sc_pkcs15_find_skey_by_id(p15card, skey_id, &skey_obj); |
395 | 0 | if (r) { |
396 | 0 | sc_log(ctx, "cannot find secret key with id:%s", sc_pkcs15_print_id(skey_id)); |
397 | 0 | LOG_FUNC_RETURN(ctx, r); |
398 | 0 | } |
399 | 0 | skey_info = (struct sc_pkcs15_skey_info *)skey_obj->data; |
400 | |
|
401 | 0 | sc_log(ctx, "found secret key '%s'", skey_obj->label); |
402 | 0 | data.pin_reference = skey_info->key_reference; |
403 | 0 | } |
404 | | |
405 | 135 | if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD |
406 | 135 | || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) { |
407 | 0 | if (!pincode && !pinlen) |
408 | 0 | data.flags |= SC_PIN_CMD_USE_PINPAD; |
409 | |
|
410 | 0 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) |
411 | 0 | data.pin1.prompt = "Please enter SO PIN"; |
412 | 0 | else |
413 | 0 | data.pin1.prompt = "Please enter PIN"; |
414 | 0 | } |
415 | | |
416 | 135 | if (card->caps & SC_CARD_CAP_SESSION_PIN && sessionpin && sessionpinlen) { |
417 | | /* session pin is requested and supported with standard verification*/ |
418 | 0 | data.cmd = SC_PIN_CMD_GET_SESSION_PIN; |
419 | 0 | memcpy(&data.pin2, &data.pin1, sizeof (data.pin1)); |
420 | 0 | data.pin2.data = sessionpin; |
421 | 0 | data.pin2.len = *sessionpinlen; |
422 | 135 | } else { |
423 | | /* perform a standard verify */ |
424 | 135 | data.cmd = SC_PIN_CMD_VERIFY; |
425 | 135 | if (sessionpinlen) |
426 | 96 | *sessionpinlen = 0; |
427 | 135 | } |
428 | | |
429 | 135 | r = sc_lock(card); |
430 | 135 | LOG_TEST_RET(ctx, r, "sc_lock() failed"); |
431 | | |
432 | | /* the path in the pin object is optional */ |
433 | 135 | if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) { |
434 | 31 | r = sc_select_file(card, &auth_info->path, NULL); |
435 | 31 | if (r) |
436 | 10 | goto out; |
437 | 31 | } |
438 | | |
439 | 125 | r = sc_pin_cmd(card, &data); |
440 | 125 | auth_info->tries_left = data.pin1.tries_left; |
441 | 125 | sc_log(ctx, "PIN cmd result %i", r); |
442 | 125 | if (r == SC_SUCCESS) { |
443 | 37 | sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen); |
444 | 37 | if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) { |
445 | 0 | *sessionpinlen = data.pin2.len; |
446 | 0 | } |
447 | 88 | } else { |
448 | 88 | sc_notify_id(card->ctx, &card->reader->atr, p15card, |
449 | 88 | NOTIFY_PIN_BAD); |
450 | 88 | if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) { |
451 | 0 | *sessionpinlen = 0; |
452 | 0 | } |
453 | 88 | } |
454 | | |
455 | 125 | if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN |
456 | 125 | && auth_info->auth_method != SC_AC_SESSION) { |
457 | 125 | sc_notify_id(card->ctx, &card->reader->atr, p15card, |
458 | 125 | r == SC_SUCCESS ? NOTIFY_PIN_GOOD : NOTIFY_PIN_BAD); |
459 | 125 | } |
460 | | |
461 | 135 | out: |
462 | 135 | sc_unlock(card); |
463 | 135 | LOG_FUNC_RETURN(ctx, r); |
464 | 135 | } |
465 | | |
466 | | |
467 | | |
468 | | /* |
469 | | * Change a PIN. |
470 | | */ |
471 | | int sc_pkcs15_change_pin(struct sc_pkcs15_card *p15card, |
472 | | struct sc_pkcs15_object *pin_obj, |
473 | | const u8 *oldpin, size_t oldpinlen, |
474 | | const u8 *newpin, size_t newpinlen) |
475 | 79 | { |
476 | 79 | struct sc_context *ctx = p15card->card->ctx; |
477 | 79 | struct sc_pin_cmd_data data; |
478 | 79 | struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data; |
479 | 79 | struct sc_card *card; |
480 | 79 | int r; |
481 | | |
482 | 79 | LOG_FUNC_CALLED(ctx); |
483 | 79 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
484 | 79 | LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED); |
485 | | |
486 | | /* make sure the pins are in valid range */ |
487 | 79 | r = _validate_pin(p15card, auth_info, oldpinlen); |
488 | 79 | LOG_TEST_RET(ctx, r, "Old PIN value do not conform PIN policy"); |
489 | | |
490 | 56 | r = _validate_pin(p15card, auth_info, newpinlen); |
491 | 56 | LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy"); |
492 | | |
493 | 47 | card = p15card->card; |
494 | 47 | r = sc_lock(card); |
495 | 47 | LOG_TEST_RET(ctx, r, "sc_lock() failed"); |
496 | | /* the path in the pin object is optional */ |
497 | 47 | if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) { |
498 | 16 | r = sc_select_file(card, &auth_info->path, NULL); |
499 | 16 | if (r) |
500 | 4 | goto out; |
501 | 16 | } |
502 | | |
503 | | /* set pin_cmd data */ |
504 | 43 | memset(&data, 0, sizeof(data)); |
505 | 43 | data.cmd = SC_PIN_CMD_CHANGE; |
506 | 43 | data.pin_type = SC_AC_CHV; |
507 | 43 | data.pin_reference = auth_info->attrs.pin.reference; |
508 | 43 | data.pin1.data = oldpin; |
509 | 43 | data.pin1.len = oldpinlen; |
510 | 43 | data.pin1.pad_char = auth_info->attrs.pin.pad_char; |
511 | 43 | data.pin1.min_length = auth_info->attrs.pin.min_length; |
512 | 43 | data.pin1.max_length = auth_info->attrs.pin.max_length; |
513 | 43 | data.pin1.pad_length = auth_info->attrs.pin.stored_length; |
514 | 43 | data.pin2.data = newpin; |
515 | 43 | data.pin2.len = newpinlen; |
516 | 43 | data.pin2.pad_char = auth_info->attrs.pin.pad_char; |
517 | 43 | data.pin2.min_length = auth_info->attrs.pin.min_length; |
518 | 43 | data.pin2.max_length = auth_info->attrs.pin.max_length; |
519 | 43 | data.pin2.pad_length = auth_info->attrs.pin.stored_length; |
520 | | |
521 | 43 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING) |
522 | 31 | data.flags |= SC_PIN_CMD_NEED_PADDING; |
523 | | |
524 | 43 | switch (auth_info->attrs.pin.type) { |
525 | 0 | case SC_PKCS15_PIN_TYPE_BCD: |
526 | 0 | data.pin1.encoding = SC_PIN_ENCODING_BCD; |
527 | 0 | data.pin2.encoding = SC_PIN_ENCODING_BCD; |
528 | 0 | break; |
529 | 43 | case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC: |
530 | 43 | data.pin1.encoding = SC_PIN_ENCODING_ASCII; |
531 | 43 | data.pin2.encoding = SC_PIN_ENCODING_ASCII; |
532 | 43 | break; |
533 | 43 | } |
534 | | |
535 | 43 | if((!oldpin || !newpin) |
536 | 0 | && (p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD |
537 | 0 | || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) { |
538 | 0 | data.flags |= SC_PIN_CMD_USE_PINPAD; |
539 | 0 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) { |
540 | 0 | data.pin1.prompt = "Please enter SO PIN"; |
541 | 0 | data.pin2.prompt = "Please enter new SO PIN"; |
542 | 0 | } |
543 | 0 | else { |
544 | 0 | data.pin1.prompt = "Please enter PIN"; |
545 | 0 | data.pin2.prompt = "Please enter new PIN"; |
546 | 0 | } |
547 | 0 | } |
548 | | |
549 | 43 | r = sc_pin_cmd(card, &data); |
550 | 43 | auth_info->tries_left = data.pin1.tries_left; |
551 | 43 | if (r == SC_SUCCESS) |
552 | 13 | sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen); |
553 | | |
554 | 47 | out: |
555 | 47 | sc_unlock(card); |
556 | 47 | return r; |
557 | 43 | } |
558 | | |
559 | | /* |
560 | | * Unblock a PIN. |
561 | | */ |
562 | | int sc_pkcs15_unblock_pin(struct sc_pkcs15_card *p15card, |
563 | | struct sc_pkcs15_object *pin_obj, |
564 | | const u8 *puk, size_t puklen, |
565 | | const u8 *newpin, size_t newpinlen) |
566 | 43 | { |
567 | 43 | struct sc_context *ctx = p15card->card->ctx; |
568 | 43 | struct sc_pin_cmd_data data; |
569 | 43 | struct sc_pkcs15_object *puk_obj; |
570 | 43 | struct sc_pkcs15_auth_info *puk_info = NULL; |
571 | 43 | int pukref = 0; |
572 | 43 | struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data; |
573 | 43 | struct sc_card *card = p15card->card; |
574 | 43 | int r; |
575 | | |
576 | 43 | LOG_FUNC_CALLED(ctx); |
577 | 43 | if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) |
578 | 43 | LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED); |
579 | | |
580 | | /* make sure the pins are in valid range */ |
581 | 43 | r = _validate_pin(p15card, auth_info, newpinlen); |
582 | 43 | LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy"); |
583 | | |
584 | | /* get pin_info object of the puk (this is a little bit complicated |
585 | | * as we don't have the id of the puk (at least now)) |
586 | | * note: for compatibility reasons we give no error if no puk object |
587 | | * is found */ |
588 | | /* first step: try to get the pkcs15 object of the puk */ |
589 | 35 | r = sc_pkcs15_find_pin_by_auth_id(p15card, &pin_obj->auth_id, &puk_obj); |
590 | 35 | if (r >= 0 && puk_obj) { |
591 | | /* second step: get the pkcs15 info object of the puk */ |
592 | 4 | puk_info = (struct sc_pkcs15_auth_info *)puk_obj->data; |
593 | 4 | pukref = puk_info->attrs.pin.reference; |
594 | 4 | } |
595 | | |
596 | 35 | if (!puk_info) { |
597 | 31 | sc_log(ctx, "Unable to get puk object, using pin object instead!"); |
598 | 31 | puk_info = auth_info; |
599 | 31 | } |
600 | | /* make sure the puk is in valid range */ |
601 | 35 | r = _validate_pin(p15card, puk_info, puklen); |
602 | 35 | LOG_TEST_RET(ctx, r, "PIN do not conforms PIN policy"); |
603 | | |
604 | | /* |
605 | | * With the current card driver interface we have no way of specifying different padding |
606 | | * flags for the PIN and the PUK. Therefore reject this case. |
607 | | */ |
608 | 32 | if ((auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING) != |
609 | 32 | (puk_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)) { |
610 | 0 | LOG_TEST_RET(ctx, r, "Padding mismatch for PIN/PUK"); |
611 | 0 | } |
612 | | |
613 | 32 | r = sc_lock(card); |
614 | 32 | LOG_TEST_RET(ctx, r, "sc_lock() failed"); |
615 | | |
616 | | /* the path in the pin object is optional */ |
617 | 32 | if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) { |
618 | 12 | r = sc_select_file(card, &auth_info->path, NULL); |
619 | 12 | if (r) |
620 | 6 | goto out; |
621 | 12 | } |
622 | | |
623 | | /* set pin_cmd data */ |
624 | 26 | memset(&data, 0, sizeof(data)); |
625 | 26 | data.cmd = SC_PIN_CMD_UNBLOCK; |
626 | 26 | data.pin_type = SC_AC_CHV; |
627 | 26 | data.pin_reference = auth_info->attrs.pin.reference; |
628 | 26 | data.puk_reference = pukref; |
629 | 26 | data.pin1.data = puk; |
630 | 26 | data.pin1.len = puklen; |
631 | 26 | data.pin1.pad_char = puk_info->attrs.pin.pad_char; |
632 | 26 | data.pin1.min_length = puk_info->attrs.pin.min_length; |
633 | 26 | data.pin1.max_length = puk_info->attrs.pin.max_length; |
634 | 26 | data.pin1.pad_length = puk_info->attrs.pin.stored_length; |
635 | 26 | data.pin2.data = newpin; |
636 | 26 | data.pin2.len = newpinlen; |
637 | 26 | data.pin2.pad_char = auth_info->attrs.pin.pad_char; |
638 | 26 | data.pin2.min_length = auth_info->attrs.pin.min_length; |
639 | 26 | data.pin2.max_length = auth_info->attrs.pin.max_length; |
640 | 26 | data.pin2.pad_length = auth_info->attrs.pin.stored_length; |
641 | | |
642 | 26 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING) |
643 | 15 | data.flags |= SC_PIN_CMD_NEED_PADDING; |
644 | | |
645 | 26 | switch (auth_info->attrs.pin.type) { |
646 | 0 | case SC_PKCS15_PIN_TYPE_BCD: |
647 | 0 | data.pin1.encoding = SC_PIN_ENCODING_BCD; |
648 | 0 | break; |
649 | 26 | case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC: |
650 | 26 | data.pin1.encoding = SC_PIN_ENCODING_ASCII; |
651 | 26 | break; |
652 | 26 | } |
653 | | |
654 | 26 | switch (puk_info->attrs.pin.type) { |
655 | 0 | case SC_PKCS15_PIN_TYPE_BCD: |
656 | 0 | data.pin2.encoding = SC_PIN_ENCODING_BCD; |
657 | 0 | break; |
658 | 26 | case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC: |
659 | 26 | data.pin2.encoding = SC_PIN_ENCODING_ASCII; |
660 | 26 | break; |
661 | 26 | } |
662 | | |
663 | 26 | if((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD |
664 | 26 | || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) { |
665 | 0 | data.flags |= SC_PIN_CMD_USE_PINPAD; |
666 | 0 | if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) { |
667 | 0 | data.pin1.prompt = "Please enter PUK"; |
668 | 0 | data.pin2.prompt = "Please enter new SO PIN"; |
669 | 0 | } |
670 | 0 | else { |
671 | 0 | data.pin1.prompt = "Please enter PUK"; |
672 | 0 | data.pin2.prompt = "Please enter new PIN"; |
673 | 0 | } |
674 | 0 | } |
675 | | |
676 | 26 | r = sc_pin_cmd(card, &data); |
677 | 26 | auth_info->tries_left = data.pin1.tries_left; |
678 | 26 | if (r == SC_SUCCESS) |
679 | 6 | sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen); |
680 | | |
681 | 32 | out: |
682 | 32 | sc_unlock(card); |
683 | 32 | LOG_FUNC_RETURN(ctx, r); |
684 | 32 | } |
685 | | |
686 | | int sc_pkcs15_get_pin_info(struct sc_pkcs15_card *p15card, |
687 | | struct sc_pkcs15_object *pin_obj) |
688 | 1.64k | { |
689 | 1.64k | int r; |
690 | 1.64k | struct sc_pin_cmd_data data; |
691 | 1.64k | struct sc_card *card = p15card->card; |
692 | 1.64k | struct sc_context *ctx = card->ctx; |
693 | 1.64k | struct sc_pkcs15_auth_info *pin_info = (struct sc_pkcs15_auth_info *) pin_obj->data; |
694 | | |
695 | 1.64k | LOG_FUNC_CALLED(ctx); |
696 | | |
697 | 1.64k | r = sc_lock(card); |
698 | 1.64k | if (r != SC_SUCCESS) |
699 | 0 | return r; |
700 | | |
701 | 1.64k | if (pin_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) { |
702 | 0 | r = SC_ERROR_INVALID_DATA; |
703 | 0 | goto out; |
704 | 0 | } |
705 | | |
706 | | /* the path in the pin object is optional */ |
707 | 1.64k | if ((pin_info->path.len > 0) || ((pin_info->path.aid.len > 0))) { |
708 | 45 | r = sc_select_file(card, &pin_info->path, NULL); |
709 | 45 | if (r) |
710 | 34 | goto out; |
711 | 45 | } |
712 | | |
713 | | /* Try to update PIN info from card */ |
714 | 1.61k | memset(&data, 0, sizeof(data)); |
715 | 1.61k | data.cmd = SC_PIN_CMD_GET_INFO; |
716 | 1.61k | data.pin_type = pin_info->auth_method; |
717 | 1.61k | data.pin_reference = pin_info->attrs.pin.reference; |
718 | | |
719 | 1.61k | r = sc_pin_cmd(card, &data); |
720 | 1.61k | if (r == SC_SUCCESS) { |
721 | 184 | if (data.pin1.max_tries > 0) |
722 | 0 | pin_info->max_tries = data.pin1.max_tries; |
723 | 184 | pin_info->tries_left = data.pin1.tries_left; |
724 | 184 | pin_info->logged_in = data.pin1.logged_in; |
725 | 184 | } |
726 | | |
727 | 1.64k | out: |
728 | 1.64k | sc_unlock(card); |
729 | 1.64k | LOG_FUNC_RETURN(ctx, r); |
730 | 1.64k | } |
731 | | |
732 | | |
733 | | void sc_pkcs15_free_auth_info(sc_pkcs15_auth_info_t *auth_info) |
734 | 5.29k | { |
735 | 5.29k | free(auth_info); |
736 | 5.29k | } |
737 | | |
738 | | |
739 | | /* Add a PIN to the PIN cache related to the card. Some operations can trigger re-authentication later. */ |
740 | | void sc_pkcs15_pincache_add(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj, |
741 | | const u8 *pin, size_t pinlen) |
742 | 82 | { |
743 | 82 | struct sc_context *ctx = p15card->card->ctx; |
744 | 82 | struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data; |
745 | 82 | struct sc_pkcs15_object *obj = NULL; |
746 | 82 | int r; |
747 | | |
748 | 82 | LOG_FUNC_CALLED(ctx); |
749 | | |
750 | 82 | if (!pin || !pinlen) { |
751 | 3 | sc_log(ctx, "No cache for zero length PIN"); |
752 | 3 | return; |
753 | 3 | } |
754 | 79 | else if (!p15card->opts.use_pin_cache) { |
755 | 8 | sc_log(ctx, "PIN caching not enabled"); |
756 | 8 | return; |
757 | 8 | } |
758 | 71 | else if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) { |
759 | 0 | sc_log(ctx, "only 'PIN' auth. object can be cached"); |
760 | 0 | return; |
761 | 0 | } |
762 | | |
763 | | /* If the PIN protects an object with user consent, don't cache it */ |
764 | | |
765 | 71 | obj = p15card->obj_list; |
766 | 527 | while (obj != NULL) { |
767 | | /* Compare 'sc_pkcs15_object.auth_id' with 'sc_pkcs15_pin_info.auth_id'. |
768 | | * In accordance with PKCS#15 "6.1.8 CommonObjectAttributes" and |
769 | | * "6.1.16 CommonAuthenticationObjectAttributes" with the exception that |
770 | | * "CommonObjectAttributes.accessControlRules" are not taken into account. */ |
771 | | |
772 | 457 | if (sc_pkcs15_compare_id(&obj->auth_id, &auth_info->auth_id)) { |
773 | | /* Caching is refused, if the protected object requires user consent */ |
774 | 68 | if (!p15card->opts.pin_cache_ignore_user_consent) { |
775 | 68 | if (obj->user_consent > 0) { |
776 | 1 | sc_log(ctx, "caching refused (user consent)"); |
777 | 1 | return; |
778 | 1 | } |
779 | 68 | } |
780 | 68 | } |
781 | | |
782 | 456 | obj = obj->next; |
783 | 456 | } |
784 | | |
785 | 70 | r = sc_pkcs15_allocate_object_content(ctx, pin_obj, pin, pinlen); |
786 | 70 | if (r != SC_SUCCESS) { |
787 | 0 | sc_log(ctx, "Failed to allocate object content"); |
788 | 0 | return; |
789 | 0 | } |
790 | | |
791 | 70 | pin_obj->usage_counter = 0; |
792 | 70 | sc_log(ctx, "PIN(%s) cached", pin_obj->label); |
793 | 70 | } |
794 | | |
795 | | /* Validate the PIN code associated with an object */ |
796 | | int |
797 | | sc_pkcs15_pincache_revalidate(struct sc_pkcs15_card *p15card, const sc_pkcs15_object_t *obj) |
798 | 0 | { |
799 | 0 | struct sc_context *ctx = p15card->card->ctx; |
800 | 0 | sc_pkcs15_object_t *pin_obj; |
801 | 0 | int r; |
802 | |
|
803 | 0 | LOG_FUNC_CALLED(ctx); |
804 | 0 | if (!p15card->opts.use_pin_cache) |
805 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
806 | | |
807 | | /* Apps that do not support CK_ALWAYS_AUTHENTICATE |
808 | | * may need pin_cache_ignore_user_consent = 1 */ |
809 | 0 | if (!p15card->opts.pin_cache_ignore_user_consent) { |
810 | 0 | if (obj->user_consent) |
811 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
812 | 0 | } |
813 | | |
814 | 0 | if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD |
815 | 0 | || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) |
816 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
817 | | |
818 | 0 | r = sc_pkcs15_find_pin_by_auth_id(p15card, &obj->auth_id, &pin_obj); |
819 | 0 | if (r != SC_SUCCESS) { |
820 | 0 | sc_log(ctx, "Could not find pin object for auth_id %s", sc_pkcs15_print_id(&obj->auth_id)); |
821 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
822 | 0 | } |
823 | | |
824 | 0 | if (pin_obj->usage_counter >= p15card->opts.pin_cache_counter) { |
825 | 0 | sc_pkcs15_free_object_content(pin_obj); |
826 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
827 | 0 | } |
828 | | |
829 | 0 | if (!pin_obj->content.value || !pin_obj->content.len) |
830 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
831 | | |
832 | 0 | pin_obj->usage_counter++; |
833 | 0 | r = _sc_pkcs15_verify_pin(p15card, pin_obj, pin_obj->content.value, pin_obj->content.len); |
834 | 0 | if (r != SC_SUCCESS) { |
835 | | /* Ensure that wrong PIN isn't used again */ |
836 | 0 | sc_pkcs15_free_object_content(pin_obj); |
837 | |
|
838 | 0 | sc_log(ctx, "Verify PIN error %i", r); |
839 | 0 | return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED; |
840 | 0 | } |
841 | | |
842 | 0 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
843 | 0 | } |
844 | | |
845 | | void sc_pkcs15_pincache_clear(struct sc_pkcs15_card *p15card) |
846 | 4.53k | { |
847 | 4.53k | struct sc_pkcs15_object *objs[32]; |
848 | 4.53k | int i, r; |
849 | | |
850 | 4.53k | LOG_FUNC_CALLED(p15card->card->ctx); |
851 | 4.53k | r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, objs, 32); |
852 | 9.77k | for (i = 0; i < r; i++) |
853 | 5.24k | sc_pkcs15_free_object_content(objs[i]); |
854 | 4.53k | } |
855 | | |