/src/opensc/src/libopensc/card-cac1.c
Line | Count | Source |
1 | | /* |
2 | | * card-cac1.c: Support for legacy CAC-1 |
3 | | * card-default.c: Support for cards with no driver |
4 | | * |
5 | | * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
6 | | * Copyright (C) 2005,2006,2007,2008,2009,2010 Douglas E. Engert <deengert@anl.gov> |
7 | | * Copyright (C) 2006, Identity Alliance, Thomas Harning <thomas.harning@identityalliance.com> |
8 | | * Copyright (C) 2007, EMC, Russell Larner <rlarner@rsa.com> |
9 | | * Copyright (C) 2016 - 2018, Red Hat, Inc. |
10 | | * |
11 | | * CAC driver author: Robert Relyea <rrelyea@redhat.com> |
12 | | * Further work: Jakub Jelen <jjelen@redhat.com> |
13 | | * |
14 | | * This library is free software; you can redistribute it and/or |
15 | | * modify it under the terms of the GNU Lesser General Public |
16 | | * License as published by the Free Software Foundation; either |
17 | | * version 2.1 of the License, or (at your option) any later version. |
18 | | * |
19 | | * This library is distributed in the hope that it will be useful, |
20 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 | | * Lesser General Public License for more details. |
23 | | * |
24 | | * You should have received a copy of the GNU Lesser General Public |
25 | | * License along with this library; if not, write to the Free Software |
26 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
27 | | */ |
28 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | #include "config.h" |
31 | | #endif |
32 | | |
33 | | #include <ctype.h> |
34 | | #include <fcntl.h> |
35 | | #include <limits.h> |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | | #ifdef _WIN32 |
40 | | #include <io.h> |
41 | | #else |
42 | | #include <unistd.h> |
43 | | #endif |
44 | | |
45 | | #ifdef ENABLE_OPENSSL |
46 | | #include <openssl/sha.h> |
47 | | #endif /* ENABLE_OPENSSL */ |
48 | | |
49 | | #include "internal.h" |
50 | | #include "simpletlv.h" |
51 | | #include "cardctl.h" |
52 | | #include "iso7816.h" |
53 | | #include "card-cac-common.h" |
54 | | #include "pkcs15.h" |
55 | | |
56 | | /* |
57 | | * CAC hardware and APDU constants |
58 | | */ |
59 | 1.07k | #define CAC_INS_GET_CERTIFICATE 0x36 /* CAC1 command to read a certificate */ |
60 | | |
61 | | /* |
62 | | * OLD cac read certificate, only use with CAC-1 card. |
63 | | */ |
64 | | static int cac_cac1_get_certificate(sc_card_t *card, u8 **out_buf, size_t *out_len) |
65 | 591 | { |
66 | 591 | u8 buf[CAC_MAX_SIZE]; |
67 | 591 | u8 *out_ptr; |
68 | 591 | size_t size = 0; |
69 | 591 | size_t left = 0; |
70 | 591 | size_t len; |
71 | 591 | sc_apdu_t apdu; |
72 | 591 | int r = SC_SUCCESS; |
73 | | |
74 | 591 | if (!out_buf || !out_len) |
75 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
76 | | |
77 | 591 | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
78 | | /* get the size */ |
79 | 591 | size = left = *out_buf ? *out_len : sizeof(buf); |
80 | 591 | out_ptr = *out_buf ? *out_buf : buf; |
81 | 591 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, CAC_INS_GET_CERTIFICATE, 0, 0 ); |
82 | 591 | len = MIN(left, 100); |
83 | 638 | while (left > 0) { |
84 | 619 | apdu.resp = out_ptr; |
85 | 619 | apdu.le = len; |
86 | 619 | apdu.resplen = left; |
87 | 619 | r = sc_transmit_apdu(card, &apdu); |
88 | 619 | if (r < 0) { |
89 | 39 | break; |
90 | 39 | } |
91 | 580 | if (apdu.resplen == 0) { |
92 | 123 | r = SC_ERROR_INTERNAL; |
93 | 123 | break; |
94 | 123 | } |
95 | | /* in the old CAC-1, 0x63 means 'more data' in addition to 'pin failed' */ |
96 | 457 | if (apdu.sw1 != 0x63 || apdu.sw2 < 1) { |
97 | | /* we've either finished reading, or hit an error, break */ |
98 | 410 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
99 | 410 | left -= apdu.resplen; |
100 | 410 | break; |
101 | 410 | } |
102 | | /* Adjust the lengths */ |
103 | 47 | left -= apdu.resplen; |
104 | 47 | out_ptr += apdu.resplen; |
105 | 47 | len = MIN(left, apdu.sw2); |
106 | 47 | } |
107 | 591 | if (r < 0) { |
108 | 211 | SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r); |
109 | 211 | } |
110 | 380 | r = (int)(size - left); |
111 | 380 | if (*out_buf == NULL) { |
112 | 323 | *out_buf = malloc(r); |
113 | 323 | if (*out_buf == NULL) { |
114 | 0 | SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, |
115 | 0 | SC_ERROR_OUT_OF_MEMORY); |
116 | 0 | } |
117 | 323 | memcpy(*out_buf, buf, r); |
118 | 323 | } |
119 | 380 | *out_len = r; |
120 | 380 | SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r); |
121 | 380 | } |
122 | | |
123 | | /* |
124 | | * Callers of this may be expecting a certificate, |
125 | | * select file will have saved the object type for us |
126 | | * as well as set that we want the cert from the object. |
127 | | */ |
128 | | static int cac_read_binary(sc_card_t *card, unsigned int idx, |
129 | | unsigned char *buf, size_t count, unsigned long *flags) |
130 | 1.68k | { |
131 | 1.68k | cac_private_data_t * priv = CAC_DATA(card); |
132 | 1.68k | int r = 0; |
133 | 1.68k | u8 *val = NULL; |
134 | 1.68k | u8 *cert_ptr; |
135 | 1.68k | size_t val_len = 0; |
136 | 1.68k | size_t len, cert_len; |
137 | 1.68k | u8 cert_type; |
138 | | |
139 | 1.68k | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
140 | | |
141 | 1.68k | if (!priv || !buf) |
142 | 1.68k | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS); |
143 | | |
144 | | /* if we didn't return it all last time, return the remainder */ |
145 | 1.68k | if (priv->cached) { |
146 | 1.23k | sc_log(card->ctx, "returning cached value idx=%d count=%zu", idx, count); |
147 | 1.23k | if (idx > priv->cache_buf_len) { |
148 | 0 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_END_REACHED); |
149 | 0 | } |
150 | 1.23k | len = MIN(count, priv->cache_buf_len-idx); |
151 | 1.23k | memcpy(buf, &priv->cache_buf[idx], len); |
152 | 1.23k | LOG_FUNC_RETURN(card->ctx, (int)len); |
153 | 1.23k | } |
154 | | |
155 | 449 | sc_log(card->ctx, "clearing cache idx=%d count=%zu", idx, count); |
156 | 449 | free(priv->cache_buf); |
157 | 449 | priv->cache_buf = NULL; |
158 | 449 | priv->cache_buf_len = 0; |
159 | | |
160 | 449 | r = cac_cac1_get_certificate(card, &val, &val_len); |
161 | 449 | if (r < 0) |
162 | 126 | goto done; |
163 | 323 | if (val_len < 1) { |
164 | 0 | r = SC_ERROR_INVALID_DATA; |
165 | 0 | goto done; |
166 | 0 | } |
167 | | |
168 | 323 | cert_type = val[0]; |
169 | 323 | cert_ptr = val + 1; |
170 | 323 | cert_len = val_len - 1; |
171 | | |
172 | | /* if the info byte is 1, then the cert is compressed, decompress it */ |
173 | 323 | if ((cert_type & 0x3) == 1) { |
174 | 153 | if (flags) |
175 | 153 | *flags |= SC_FILE_FLAG_COMPRESSED_AUTO; |
176 | 153 | } |
177 | 323 | if (cert_len > 0) { |
178 | 239 | priv->cache_buf = malloc(cert_len); |
179 | 239 | if (priv->cache_buf == NULL) { |
180 | 0 | r = SC_ERROR_OUT_OF_MEMORY; |
181 | 0 | goto done; |
182 | 0 | } |
183 | 239 | priv->cache_buf_len = cert_len; |
184 | 239 | memcpy(priv->cache_buf, cert_ptr, cert_len); |
185 | 239 | } |
186 | | |
187 | | /* OK we've read the data, now copy the required portion out to the callers buffer */ |
188 | 323 | if (idx > priv->cache_buf_len) { |
189 | 0 | r = SC_ERROR_FILE_END_REACHED; |
190 | 0 | goto done; |
191 | 0 | } |
192 | 323 | priv->cached = 1; |
193 | 323 | len = MIN(count, priv->cache_buf_len-idx); |
194 | 323 | if (len && priv->cache_buf) |
195 | 239 | memcpy(buf, &priv->cache_buf[idx], len); |
196 | 323 | r = (int)len; |
197 | 449 | done: |
198 | 449 | if (val) |
199 | 323 | free(val); |
200 | 449 | LOG_FUNC_RETURN(card->ctx, r); |
201 | 449 | } |
202 | | |
203 | | /* |
204 | | * CAC cards use SC_PATH_SELECT_OBJECT_ID rather than SC_PATH_SELECT_FILE_ID. In order to use more |
205 | | * of the PKCS #15 structure, we call the selection SC_PATH_SELECT_FILE_ID, but we set p1 to 2 instead |
206 | | * of 0. Also cac1 does not do any FCI, but it doesn't understand not selecting it. It returns invalid INS |
207 | | * if it doesn't like anything about the select, so we always 'request' FCI for CAC1 |
208 | | * |
209 | | * The rest is just copied from iso7816_select_file |
210 | | */ |
211 | | static int cac_select_file_by_type(sc_card_t *card, const sc_path_t *in_path, sc_file_t **file_out) |
212 | 47.6k | { |
213 | 47.6k | struct sc_context *ctx; |
214 | 47.6k | struct sc_apdu apdu; |
215 | 47.6k | unsigned char buf[SC_MAX_APDU_BUFFER_SIZE]; |
216 | 47.6k | unsigned char pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf; |
217 | 47.6k | int r, pathtype; |
218 | 47.6k | size_t pathlen; |
219 | 47.6k | struct sc_file *file = NULL; |
220 | 47.6k | cac_private_data_t *priv; |
221 | | |
222 | 47.6k | if (card == NULL || in_path == NULL) |
223 | 0 | return SC_ERROR_INTERNAL; |
224 | 47.6k | if (in_path->len > sizeof(pathbuf)) |
225 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
226 | | |
227 | 47.6k | ctx = card->ctx; |
228 | 47.6k | priv = CAC_DATA(card); |
229 | | |
230 | 47.6k | SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); |
231 | | |
232 | 47.6k | memcpy(path, in_path->value, in_path->len); |
233 | 47.6k | pathlen = in_path->len; |
234 | 47.6k | pathtype = in_path->type; |
235 | | |
236 | 47.6k | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, |
237 | 47.6k | "path=%s, path->value=%s path->type=%d (%x)", |
238 | 47.6k | sc_print_path(in_path), |
239 | 47.6k | sc_dump_hex(in_path->value, in_path->len), |
240 | 47.6k | in_path->type, in_path->type); |
241 | 47.6k | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "file_out=%p index=%d count=%d\n", |
242 | 47.6k | file_out, in_path->index, in_path->count); |
243 | | |
244 | | /* Sigh, iso7816_select_file expects paths to keys to have specific |
245 | | * formats. There is no override. We have to add some bytes to the |
246 | | * path to make it happy. |
247 | | * We only need to do this for private keys. |
248 | | */ |
249 | 47.6k | if ((pathlen > 2) && (pathlen <= 4) && memcmp(path, "\x3F\x00", 2) == 0) { |
250 | 426 | path += 2; |
251 | 426 | pathlen -= 2; |
252 | 426 | } |
253 | | |
254 | | |
255 | | /* CAC has multiple different type of objects that aren't PKCS #15. When we read |
256 | | * them we need convert them to something PKCS #15 would understand. Find the object |
257 | | * and object type here: |
258 | | */ |
259 | 47.6k | if (priv) { /* don't record anything if we haven't been initialized yet */ |
260 | | /* forget any old cached values */ |
261 | 4.32k | if (priv->cache_buf) { |
262 | 235 | free(priv->cache_buf); |
263 | 235 | priv->cache_buf = NULL; |
264 | 235 | } |
265 | 4.32k | priv->cache_buf_len = 0; |
266 | 4.32k | priv->cached = 0; |
267 | 4.32k | } |
268 | | |
269 | 47.6k | if (in_path->aid.len) { |
270 | 47.2k | if (in_path->aid.len > sizeof(pathbuf)) |
271 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
272 | 47.2k | if (!pathlen) { |
273 | 47.2k | memcpy(path, in_path->aid.value, in_path->aid.len); |
274 | 47.2k | pathlen = in_path->aid.len; |
275 | 47.2k | pathtype = SC_PATH_TYPE_DF_NAME; |
276 | 47.2k | } else { |
277 | | /* First, select the application */ |
278 | 0 | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,"select application" ); |
279 | 0 | sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 4, 0); |
280 | 0 | apdu.data = in_path->aid.value; |
281 | 0 | apdu.datalen = in_path->aid.len; |
282 | 0 | apdu.lc = in_path->aid.len; |
283 | |
|
284 | 0 | r = sc_transmit_apdu(card, &apdu); |
285 | 0 | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
286 | 0 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
287 | 0 | if (r) |
288 | 0 | LOG_FUNC_RETURN(ctx, r); |
289 | |
|
290 | 0 | } |
291 | 47.2k | } |
292 | | |
293 | 47.6k | sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0, 0); |
294 | | |
295 | 47.6k | switch (pathtype) { |
296 | | /* ideally we would had SC_PATH_TYPE_OBJECT_ID and add code to the iso7816 select. |
297 | | * Unfortunately we'd also need to update the caching code as well. For now just |
298 | | * use FILE_ID and change p1 here */ |
299 | 0 | case SC_PATH_TYPE_FILE_ID: |
300 | 0 | apdu.p1 = 2; |
301 | 0 | if (pathlen != 2) |
302 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
303 | 0 | break; |
304 | 47.2k | case SC_PATH_TYPE_DF_NAME: |
305 | 47.2k | apdu.p1 = 4; |
306 | 47.2k | break; |
307 | 426 | default: |
308 | 426 | LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS); |
309 | 47.6k | } |
310 | 47.2k | apdu.lc = pathlen; |
311 | 47.2k | apdu.data = path; |
312 | 47.2k | apdu.datalen = pathlen; |
313 | 47.2k | apdu.resp = buf; |
314 | 47.2k | apdu.resplen = sizeof(buf); |
315 | 47.2k | apdu.le = sc_get_max_recv_size(card) < 256 ? sc_get_max_recv_size(card) : 256; |
316 | 47.2k | apdu.p2 = 0x00; |
317 | | |
318 | 47.2k | r = sc_transmit_apdu(card, &apdu); |
319 | 47.2k | LOG_TEST_RET(ctx, r, "APDU transmit failed"); |
320 | | |
321 | 46.9k | if (file_out == NULL) { |
322 | | /* For some cards 'SELECT' can be only with request to return FCI/FCP. */ |
323 | 45.8k | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
324 | 45.8k | if (apdu.sw1 == 0x6A && apdu.sw2 == 0x86) { |
325 | 82 | apdu.p2 = 0x00; |
326 | 82 | apdu.resplen = sizeof(buf); |
327 | 82 | if (sc_transmit_apdu(card, &apdu) == SC_SUCCESS) |
328 | 35 | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
329 | 82 | } |
330 | 45.8k | if (apdu.sw1 == 0x61) |
331 | 45.8k | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
332 | 45.7k | LOG_FUNC_RETURN(ctx, r); |
333 | 45.7k | } |
334 | | |
335 | 1.12k | r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
336 | 1.12k | if (r) |
337 | 1.12k | LOG_FUNC_RETURN(ctx, r); |
338 | | |
339 | | /* CAC cards never return FCI, fake one */ |
340 | 449 | file = sc_file_new(); |
341 | 449 | if (file == NULL) |
342 | 449 | LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); |
343 | 449 | file->path = *in_path; |
344 | 449 | file->size = CAC_MAX_SIZE; /* we don't know how big, just give a large size until we can read the file */ |
345 | | |
346 | 449 | *file_out = file; |
347 | 449 | LOG_FUNC_RETURN(ctx, SC_SUCCESS); |
348 | | |
349 | 449 | } |
350 | | |
351 | | static int cac_select_file(sc_card_t *card, const sc_path_t *in_path, sc_file_t **file_out) |
352 | 1.56k | { |
353 | 1.56k | return cac_select_file_by_type(card, in_path, file_out); |
354 | 1.56k | } |
355 | | |
356 | | static int cac_finish(sc_card_t *card) |
357 | 142 | { |
358 | 142 | cac_private_data_t * priv = CAC_DATA(card); |
359 | | |
360 | 142 | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
361 | 142 | if (priv) { |
362 | 142 | cac_free_private_data(priv); |
363 | 142 | card->drv_data = NULL; |
364 | 142 | } |
365 | 142 | return SC_SUCCESS; |
366 | 142 | } |
367 | | |
368 | | |
369 | | /* select a CAC pki applet by index */ |
370 | | static int cac_select_pki_applet(sc_card_t *card, int index) |
371 | 46.1k | { |
372 | 46.1k | sc_path_t applet_path = cac_cac_pki_obj.path; |
373 | 46.1k | applet_path.aid.value[applet_path.aid.len-1] = index; |
374 | 46.1k | return cac_select_file_by_type(card, &applet_path, NULL); |
375 | 46.1k | } |
376 | | |
377 | | /* |
378 | | * Find the first existing CAC applet. If none found, then this isn't a CAC |
379 | | */ |
380 | | static int cac_find_first_pki_applet(sc_card_t *card, int *index_out) |
381 | 3.01k | { |
382 | 3.01k | int r, i; |
383 | | |
384 | 3.01k | if (!index_out) |
385 | 0 | return SC_ERROR_INVALID_ARGUMENTS; |
386 | | |
387 | 46.0k | for (i = 0; i < MAX_CAC_SLOTS; i++) { |
388 | 43.3k | r = cac_select_pki_applet(card, i); |
389 | 43.3k | if (r == SC_SUCCESS) { |
390 | 480 | u8 data[2]; |
391 | 480 | sc_apdu_t apdu; |
392 | | |
393 | | /* Try to read first two bytes of the buffer to |
394 | | * make sure it is not just malfunctioning card |
395 | | */ |
396 | 480 | sc_format_apdu(card, &apdu, SC_APDU_CASE_2, |
397 | 480 | CAC_INS_GET_CERTIFICATE, 0x00, 0x00); |
398 | 480 | apdu.le = 0x02; |
399 | 480 | apdu.resplen = 2; |
400 | 480 | apdu.resp = data; |
401 | 480 | r = sc_transmit_apdu(card, &apdu); |
402 | | /* SW1 = 0x63 means more data in CAC1 */ |
403 | 480 | if (r == SC_SUCCESS && apdu.sw1 != 0x63) |
404 | 138 | continue; |
405 | | |
406 | 342 | *index_out = i; |
407 | 342 | return SC_SUCCESS; |
408 | 480 | } |
409 | 43.3k | } |
410 | 2.67k | return SC_ERROR_OBJECT_NOT_FOUND; |
411 | 3.01k | } |
412 | | |
413 | | static int cac_populate_cac1(sc_card_t *card, int index, cac_private_data_t *priv) |
414 | 169 | { |
415 | 169 | int r, i; |
416 | 169 | cac_object_t pki_obj = cac_cac_pki_obj; |
417 | 169 | u8 buf[100]; |
418 | 169 | u8 *val; |
419 | 169 | size_t val_len; |
420 | | |
421 | | /* populate PKI objects */ |
422 | 2.76k | for (i = index; i < MAX_CAC_SLOTS; i++) { |
423 | 2.59k | r = cac_select_pki_applet(card, i); |
424 | 2.59k | if (r == SC_SUCCESS) { |
425 | 1.16k | pki_obj.name = get_cac_label(i); |
426 | 1.16k | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, |
427 | 1.16k | "CAC: pki_object found, cert_next=%d (%s)", |
428 | 1.16k | i, pki_obj.name); |
429 | 1.16k | pki_obj.path.aid.value[pki_obj.path.aid.len-1] = i; |
430 | 1.16k | pki_obj.fd = i+1; /* don't use id of zero */ |
431 | 1.16k | cac_add_object_to_list(&priv->pki_list, &pki_obj); |
432 | 1.16k | } |
433 | 2.59k | } |
434 | | |
435 | | /* |
436 | | * create a cuid to simulate the cac 2 cuid. |
437 | | */ |
438 | 169 | priv->cuid = cac_cac_cuid; |
439 | | /* create a serial number by hashing the first 100 bytes of the |
440 | | * first certificate on the card */ |
441 | 169 | r = cac_select_pki_applet(card, index); |
442 | 169 | if (r < 0) { |
443 | 27 | return r; /* shouldn't happen unless the card has been removed or is malfunctioning */ |
444 | 27 | } |
445 | 142 | val = buf; |
446 | 142 | val_len = sizeof(buf); |
447 | 142 | r = cac_cac1_get_certificate(card, &val, &val_len); |
448 | 142 | if (r >= 0) { |
449 | 57 | priv->cac_id = malloc(20); |
450 | 57 | if (priv->cac_id == NULL) { |
451 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
452 | 0 | } |
453 | 57 | #ifdef ENABLE_OPENSSL |
454 | 57 | SHA1(val, val_len, priv->cac_id); |
455 | 57 | priv->cac_id_len = 20; |
456 | 57 | sc_debug_hex(card->ctx, SC_LOG_DEBUG_VERBOSE, |
457 | 57 | "cuid", priv->cac_id, priv->cac_id_len); |
458 | | #else |
459 | | sc_log(card->ctx, "OpenSSL Required"); |
460 | | free(priv->cac_id); |
461 | | priv->cac_id = NULL; |
462 | | return SC_ERROR_NOT_SUPPORTED; |
463 | | #endif /* ENABLE_OPENSSL */ |
464 | 57 | } |
465 | 142 | return SC_SUCCESS; |
466 | 142 | } |
467 | | |
468 | | /* |
469 | | * Look for a CAC card. If it exists, initialize our data structures |
470 | | */ |
471 | | static int cac_find_and_initialize(sc_card_t *card, int initialize) |
472 | 3.01k | { |
473 | 3.01k | int r, index; |
474 | 3.01k | cac_private_data_t *priv = NULL; |
475 | | |
476 | | /* already initialized? */ |
477 | 3.01k | if (card->drv_data) { |
478 | 0 | return SC_SUCCESS; |
479 | 0 | } |
480 | | |
481 | | /* is this a CAC Alt token without any accompanying structures */ |
482 | 3.01k | r = cac_find_first_pki_applet(card, &index); |
483 | 3.01k | if (r == SC_SUCCESS) { |
484 | 342 | sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "PKI applet found, is bare CAC-1"); |
485 | 342 | if (!initialize) /* match card only */ |
486 | 173 | return r; |
487 | | |
488 | 169 | if (!priv) { |
489 | 169 | priv = cac_new_private_data(); |
490 | 169 | if (!priv) |
491 | 0 | return SC_ERROR_OUT_OF_MEMORY; |
492 | 169 | } |
493 | 169 | card->drv_data = priv; /* needed for the read_binary() */ |
494 | 169 | r = cac_populate_cac1(card, index, priv); |
495 | 169 | if (r == SC_SUCCESS) { |
496 | 142 | card->type = SC_CARD_TYPE_CAC_I; |
497 | 142 | return r; |
498 | 142 | } |
499 | 27 | card->drv_data = NULL; /* reset on failure */ |
500 | 27 | } |
501 | 2.70k | if (priv) { |
502 | 27 | cac_free_private_data(priv); |
503 | 27 | } |
504 | 2.70k | return r; |
505 | 3.01k | } |
506 | | |
507 | | |
508 | | /* NOTE: returns a bool, 1 card matches, 0 it does not */ |
509 | | static int cac_match_card(sc_card_t *card) |
510 | 2.84k | { |
511 | 2.84k | int r; |
512 | 2.84k | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
513 | | |
514 | 2.84k | r = cac_find_and_initialize(card, 0); |
515 | 2.84k | return (r == SC_SUCCESS); /* never match */ |
516 | 2.84k | } |
517 | | |
518 | | |
519 | | static int cac_init(sc_card_t *card) |
520 | 173 | { |
521 | 173 | int r; |
522 | 173 | unsigned long flags; |
523 | | |
524 | 173 | SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); |
525 | | |
526 | 173 | r = cac_find_and_initialize(card, 1); |
527 | 173 | if (r < 0) { |
528 | 31 | LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD); |
529 | 31 | } |
530 | 142 | flags = SC_ALGORITHM_RSA_RAW; |
531 | | |
532 | 142 | _sc_card_add_rsa_alg(card, 1024, flags, 0); /* mandatory */ |
533 | 142 | _sc_card_add_rsa_alg(card, 2048, flags, 0); /* optional */ |
534 | 142 | _sc_card_add_rsa_alg(card, 3072, flags, 0); /* optional */ |
535 | | |
536 | 142 | card->caps |= SC_CARD_CAP_RNG | SC_CARD_CAP_ISO7816_PIN_INFO; |
537 | | |
538 | 142 | LOG_FUNC_RETURN(card->ctx, SC_SUCCESS); |
539 | 142 | } |
540 | | |
541 | | static int cac_logout(sc_card_t *card) |
542 | 0 | { |
543 | 0 | int index; |
544 | 0 | return cac_find_first_pki_applet(card, &index); |
545 | 0 | } |
546 | | |
547 | | static struct sc_card_operations cac_ops; |
548 | | |
549 | | static struct sc_card_driver cac1_drv = { |
550 | | "Common Access Card (CAC 1)", |
551 | | "cac1", |
552 | | &cac_ops, |
553 | | NULL, 0, NULL |
554 | | }; |
555 | | |
556 | | static struct sc_card_driver * sc_get_driver(void) |
557 | 16.8k | { |
558 | | /* Inherit most of the things from the CAC driver */ |
559 | 16.8k | struct sc_card_driver *cac_drv = sc_get_cac_driver(); |
560 | | |
561 | 16.8k | cac_ops = *cac_drv->ops; |
562 | 16.8k | cac_ops.match_card = cac_match_card; |
563 | 16.8k | cac_ops.init = cac_init; |
564 | 16.8k | cac_ops.finish = cac_finish; |
565 | | |
566 | 16.8k | cac_ops.select_file = cac_select_file; /* need to record object type */ |
567 | 16.8k | cac_ops.read_binary = cac_read_binary; |
568 | 16.8k | cac_ops.logout = cac_logout; |
569 | | |
570 | 16.8k | return &cac1_drv; |
571 | 16.8k | } |
572 | | |
573 | | |
574 | | struct sc_card_driver * sc_get_cac1_driver(void) |
575 | 16.8k | { |
576 | 16.8k | return sc_get_driver(); |
577 | 16.8k | } |