Fix signal handling race condition.
[gnupg.git] / scd / iso7816.c
blob2286090b661513afac2924ea4039b13199864298
1 /* iso7816.c - ISO 7816 commands
2 * Copyright (C) 2003, 2004, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * $Id$
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #if defined(GNUPG_SCD_MAIN_HEADER)
29 #include GNUPG_SCD_MAIN_HEADER
30 #elif GNUPG_MAJOR_VERSION == 1
31 /* This is used with GnuPG version < 1.9. The code has been source
32 copied from the current GnuPG >= 1.9 and is maintained over
33 there. */
34 #include "options.h"
35 #include "errors.h"
36 #include "memory.h"
37 #include "util.h"
38 #include "i18n.h"
39 #else /* GNUPG_MAJOR_VERSION != 1 */
40 #include "scdaemon.h"
41 #endif /* GNUPG_MAJOR_VERSION != 1 */
43 #include "iso7816.h"
44 #include "apdu.h"
47 #define CMD_SELECT_FILE 0xA4
48 #define CMD_VERIFY ISO7816_VERIFY
49 #define CMD_CHANGE_REFERENCE_DATA ISO7816_CHANGE_REFERENCE_DATA
50 #define CMD_RESET_RETRY_COUNTER ISO7816_RESET_RETRY_COUNTER
51 #define CMD_GET_DATA 0xCA
52 #define CMD_PUT_DATA 0xDA
53 #define CMD_MSE 0x22
54 #define CMD_PSO 0x2A
55 #define CMD_INTERNAL_AUTHENTICATE 0x88
56 #define CMD_GENERATE_KEYPAIR 0x47
57 #define CMD_GET_CHALLENGE 0x84
58 #define CMD_READ_BINARY 0xB0
59 #define CMD_READ_RECORD 0xB2
61 static gpg_error_t
62 map_sw (int sw)
64 gpg_err_code_t ec;
66 switch (sw)
68 case SW_EEPROM_FAILURE: ec = GPG_ERR_HARDWARE; break;
69 case SW_TERM_STATE: ec = GPG_ERR_CARD; break;
70 case SW_WRONG_LENGTH: ec = GPG_ERR_INV_VALUE; break;
71 case SW_SM_NOT_SUP: ec = GPG_ERR_NOT_SUPPORTED; break;
72 case SW_CC_NOT_SUP: ec = GPG_ERR_NOT_SUPPORTED; break;
73 case SW_CHV_WRONG: ec = GPG_ERR_BAD_PIN; break;
74 case SW_CHV_BLOCKED: ec = GPG_ERR_PIN_BLOCKED; break;
75 case SW_USE_CONDITIONS: ec = GPG_ERR_USE_CONDITIONS; break;
76 case SW_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
77 case SW_BAD_PARAMETER: ec = GPG_ERR_INV_VALUE; break;
78 case SW_FILE_NOT_FOUND: ec = GPG_ERR_ENOENT; break;
79 case SW_RECORD_NOT_FOUND:ec= GPG_ERR_NOT_FOUND; break;
80 case SW_REF_NOT_FOUND: ec = GPG_ERR_NO_OBJ; break;
81 case SW_BAD_P0_P1: ec = GPG_ERR_INV_VALUE; break;
82 case SW_EXACT_LENGTH: ec = GPG_ERR_INV_VALUE; break;
83 case SW_INS_NOT_SUP: ec = GPG_ERR_CARD; break;
84 case SW_CLA_NOT_SUP: ec = GPG_ERR_CARD; break;
85 case SW_SUCCESS: ec = 0; break;
87 case SW_HOST_OUT_OF_CORE: ec = GPG_ERR_ENOMEM; break;
88 case SW_HOST_INV_VALUE: ec = GPG_ERR_INV_VALUE; break;
89 case SW_HOST_INCOMPLETE_CARD_RESPONSE: ec = GPG_ERR_CARD; break;
90 case SW_HOST_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
91 case SW_HOST_LOCKING_FAILED: ec = GPG_ERR_BUG; break;
92 case SW_HOST_BUSY: ec = GPG_ERR_EBUSY; break;
93 case SW_HOST_NO_CARD: ec = GPG_ERR_CARD_NOT_PRESENT; break;
94 case SW_HOST_CARD_INACTIVE: ec = GPG_ERR_CARD_RESET; break;
95 case SW_HOST_CARD_IO_ERROR: ec = GPG_ERR_EIO; break;
96 case SW_HOST_GENERAL_ERROR: ec = GPG_ERR_GENERAL; break;
97 case SW_HOST_NO_READER: ec = GPG_ERR_ENODEV; break;
98 case SW_HOST_ABORTED: ec = GPG_ERR_CANCELED; break;
99 case SW_HOST_NO_KEYPAD: ec = GPG_ERR_NOT_SUPPORTED; break;
101 default:
102 if ((sw & 0x010000))
103 ec = GPG_ERR_GENERAL; /* Should not happen. */
104 else if ((sw & 0xff00) == SW_MORE_DATA)
105 ec = 0; /* This should actually never been seen here. */
106 else
107 ec = GPG_ERR_CARD;
109 return gpg_error (ec);
112 /* Map a status word from the APDU layer to a gpg-error code. */
113 gpg_error_t
114 iso7816_map_sw (int sw)
116 /* All APDU functions should return 0x9000 on success but for
117 historical reasons of the implementation some return 0 to
118 indicate success. We allow for that here. */
119 return sw? map_sw (sw) : 0;
123 /* This function is specialized version of the SELECT FILE command.
124 SLOT is the card and reader as created for example by
125 apdu_open_reader (), AID is a buffer of size AIDLEN holding the
126 requested application ID. The function can't be used to enumerate
127 AIDs and won't return the AID on success. The return value is 0
128 for okay or a GPG error code. Note that ISO error codes are
129 internally mapped. Bit 0 of FLAGS should be set if the card does
130 not understand P2=0xC0. */
131 gpg_error_t
132 iso7816_select_application (int slot, const char *aid, size_t aidlen,
133 unsigned int flags)
135 int sw;
136 sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE, 4,
137 (flags&1)? 0 :0x0c, aidlen, aid);
138 return map_sw (sw);
142 gpg_error_t
143 iso7816_select_file (int slot, int tag, int is_dir,
144 unsigned char **result, size_t *resultlen)
146 int sw, p0, p1;
147 unsigned char tagbuf[2];
149 tagbuf[0] = (tag >> 8) & 0xff;
150 tagbuf[1] = tag & 0xff;
152 if (result || resultlen)
154 *result = NULL;
155 *resultlen = 0;
156 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
158 else
160 p0 = (tag == 0x3F00)? 0: is_dir? 1:2;
161 p1 = 0x0c; /* No FC return. */
162 sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE,
163 p0, p1, 2, (char*)tagbuf );
164 return map_sw (sw);
167 return 0;
171 /* Do a select file command with a direct path. */
172 gpg_error_t
173 iso7816_select_path (int slot, const unsigned short *path, size_t pathlen,
174 unsigned char **result, size_t *resultlen)
176 int sw, p0, p1;
177 unsigned char buffer[100];
178 int buflen;
180 if (result || resultlen)
182 *result = NULL;
183 *resultlen = 0;
184 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
187 if (pathlen/2 >= sizeof buffer)
188 return gpg_error (GPG_ERR_TOO_LARGE);
190 for (buflen = 0; pathlen; pathlen--, path++)
192 buffer[buflen++] = (*path >> 8);
193 buffer[buflen++] = *path;
196 p0 = 0x08;
197 p1 = 0x0c; /* No FC return. */
198 sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE,
199 p0, p1, buflen, (char*)buffer );
200 return map_sw (sw);
204 /* This is a private command currently only working for TCOS cards. */
205 gpg_error_t
206 iso7816_list_directory (int slot, int list_dirs,
207 unsigned char **result, size_t *resultlen)
209 int sw;
211 if (!result || !resultlen)
212 return gpg_error (GPG_ERR_INV_VALUE);
213 *result = NULL;
214 *resultlen = 0;
216 sw = apdu_send (slot, 0x80, 0xAA, list_dirs? 1:2, 0, -1, NULL,
217 result, resultlen);
218 if (sw != SW_SUCCESS)
220 /* Make sure that pending buffers are released. */
221 xfree (*result);
222 *result = NULL;
223 *resultlen = 0;
225 return map_sw (sw);
229 /* Check whether the reader supports the ISO command code COMMAND on
230 the keypad. Returns 0 on success. */
231 gpg_error_t
232 iso7816_check_keypad (int slot, int command, iso7816_pininfo_t *pininfo)
234 int sw;
236 sw = apdu_check_keypad (slot, command,
237 pininfo->mode, pininfo->minlen, pininfo->maxlen,
238 pininfo->padlen);
239 return iso7816_map_sw (sw);
243 /* Perform a VERIFY command on SLOT using the card holder verification
244 vector CHVNO with a CHV of lenght CHVLEN. With PININFO non-NULL
245 the keypad of the reader will be used. Returns 0 on success. */
246 gpg_error_t
247 iso7816_verify_kp (int slot, int chvno, const char *chv, size_t chvlen,
248 iso7816_pininfo_t *pininfo)
250 int sw;
252 if (pininfo && pininfo->mode)
253 sw = apdu_send_simple_kp (slot, 0x00, CMD_VERIFY, 0, chvno, chvlen, chv,
254 pininfo->mode,
255 pininfo->minlen,
256 pininfo->maxlen,
257 pininfo->padlen);
258 else
259 sw = apdu_send_simple (slot, 0, 0x00, CMD_VERIFY, 0, chvno, chvlen, chv);
260 return map_sw (sw);
263 /* Perform a VERIFY command on SLOT using the card holder verification
264 vector CHVNO with a CHV of lenght CHVLEN. Returns 0 on success. */
265 gpg_error_t
266 iso7816_verify (int slot, int chvno, const char *chv, size_t chvlen)
268 return iso7816_verify_kp (slot, chvno, chv, chvlen, NULL);
271 /* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
272 verification vector CHVNO. If the OLDCHV is NULL (and OLDCHVLEN
273 0), a "change reference data" is done, otherwise an "exchange
274 reference data". The new reference data is expected in NEWCHV of
275 length NEWCHVLEN. With PININFO non-NULL the keypad of the reader
276 will be used. */
277 gpg_error_t
278 iso7816_change_reference_data_kp (int slot, int chvno,
279 const char *oldchv, size_t oldchvlen,
280 const char *newchv, size_t newchvlen,
281 iso7816_pininfo_t *pininfo)
283 int sw;
284 char *buf;
286 if ((!oldchv && oldchvlen)
287 || (oldchv && !oldchvlen)
288 || !newchv || !newchvlen )
289 return gpg_error (GPG_ERR_INV_VALUE);
291 buf = xtrymalloc (oldchvlen + newchvlen);
292 if (!buf)
293 return gpg_error (gpg_err_code_from_errno (errno));
294 if (oldchvlen)
295 memcpy (buf, oldchv, oldchvlen);
296 memcpy (buf+oldchvlen, newchv, newchvlen);
298 if (pininfo && pininfo->mode)
299 sw = apdu_send_simple_kp (slot, 0x00, CMD_CHANGE_REFERENCE_DATA,
300 oldchvlen? 0 : 1, chvno, oldchvlen+newchvlen, buf,
301 pininfo->mode,
302 pininfo->minlen,
303 pininfo->maxlen,
304 pininfo->padlen);
305 else
306 sw = apdu_send_simple (slot, 0, 0x00, CMD_CHANGE_REFERENCE_DATA,
307 oldchvlen? 0 : 1, chvno, oldchvlen+newchvlen, buf);
308 xfree (buf);
309 return map_sw (sw);
313 /* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
314 verification vector CHVNO. If the OLDCHV is NULL (and OLDCHVLEN
315 0), a "change reference data" is done, otherwise an "exchange
316 reference data". The new reference data is expected in NEWCHV of
317 length NEWCHVLEN. */
318 gpg_error_t
319 iso7816_change_reference_data (int slot, int chvno,
320 const char *oldchv, size_t oldchvlen,
321 const char *newchv, size_t newchvlen)
323 return iso7816_change_reference_data_kp (slot, chvno, oldchv, oldchvlen,
324 newchv, newchvlen, NULL);
328 gpg_error_t
329 iso7816_reset_retry_counter_kp (int slot, int chvno,
330 const char *newchv, size_t newchvlen,
331 iso7816_pininfo_t *pininfo)
333 int sw;
335 if (!newchv || !newchvlen )
336 return gpg_error (GPG_ERR_INV_VALUE);
338 /* FIXME: The keypad mode has not yet been tested. */
339 if (pininfo && pininfo->mode)
340 sw = apdu_send_simple_kp (slot, 0x00, CMD_RESET_RETRY_COUNTER,
341 2, chvno, newchvlen, newchv,
342 pininfo->mode,
343 pininfo->minlen,
344 pininfo->maxlen,
345 pininfo->padlen);
346 else
347 sw = apdu_send_simple (slot, 0, 0x00, CMD_RESET_RETRY_COUNTER,
348 2, chvno, newchvlen, newchv);
349 return map_sw (sw);
353 gpg_error_t
354 iso7816_reset_retry_counter_with_rc (int slot, int chvno,
355 const char *data, size_t datalen)
357 int sw;
359 if (!data || !datalen )
360 return gpg_error (GPG_ERR_INV_VALUE);
362 sw = apdu_send_simple (slot, 0, 0x00, CMD_RESET_RETRY_COUNTER,
363 0, chvno, datalen, data);
364 return map_sw (sw);
368 gpg_error_t
369 iso7816_reset_retry_counter (int slot, int chvno,
370 const char *newchv, size_t newchvlen)
372 return iso7816_reset_retry_counter_kp (slot, chvno, newchv, newchvlen, NULL);
377 /* Perform a GET DATA command requesting TAG and storing the result in
378 a newly allocated buffer at the address passed by RESULT. Return
379 the length of this data at the address of RESULTLEN. */
380 gpg_error_t
381 iso7816_get_data (int slot, int tag,
382 unsigned char **result, size_t *resultlen)
384 int sw;
386 if (!result || !resultlen)
387 return gpg_error (GPG_ERR_INV_VALUE);
388 *result = NULL;
389 *resultlen = 0;
391 sw = apdu_send (slot, 0x00, CMD_GET_DATA,
392 ((tag >> 8) & 0xff), (tag & 0xff), -1, NULL,
393 result, resultlen);
394 if (sw != SW_SUCCESS)
396 /* Make sure that pending buffers are released. */
397 xfree (*result);
398 *result = NULL;
399 *resultlen = 0;
400 return map_sw (sw);
403 return 0;
407 /* Perform a PUT DATA command on card in SLOT. Write DATA of length
408 DATALEN to TAG. EXTENDED_MODE controls whether extended length
409 headers or command chaining is used instead of single length
410 bytes. */
411 gpg_error_t
412 iso7816_put_data (int slot, int extended_mode, int tag,
413 const unsigned char *data, size_t datalen)
415 int sw;
417 sw = apdu_send_simple (slot, extended_mode, 0x00, CMD_PUT_DATA,
418 ((tag >> 8) & 0xff), (tag & 0xff),
419 datalen, (const char*)data);
420 return map_sw (sw);
423 /* Same as iso7816_put_data but uses an odd instrcution byte. */
424 gpg_error_t
425 iso7816_put_data_odd (int slot, int extended_mode, int tag,
426 const unsigned char *data, size_t datalen)
428 int sw;
430 sw = apdu_send_simple (slot, extended_mode, 0x00, CMD_PUT_DATA+1,
431 ((tag >> 8) & 0xff), (tag & 0xff),
432 datalen, (const char*)data);
433 return map_sw (sw);
436 /* Manage Security Environment. This is a weird operation and there
437 is no easy abstraction for it. Furthermore, some card seem to have
438 a different interpreation of 7816-8 and thus we resort to let the
439 caller decide what to do. */
440 gpg_error_t
441 iso7816_manage_security_env (int slot, int p1, int p2,
442 const unsigned char *data, size_t datalen)
444 int sw;
446 if (p1 < 0 || p1 > 255 || p2 < 0 || p2 > 255 )
447 return gpg_error (GPG_ERR_INV_VALUE);
449 sw = apdu_send_simple (slot, 0, 0x00, CMD_MSE, p1, p2,
450 data? datalen : -1, (const char*)data);
451 return map_sw (sw);
455 /* Perform the security operation COMPUTE DIGITAL SIGANTURE. On
456 success 0 is returned and the data is availavle in a newly
457 allocated buffer stored at RESULT with its length stored at
458 RESULTLEN. */
459 gpg_error_t
460 iso7816_compute_ds (int slot, const unsigned char *data, size_t datalen,
461 unsigned char **result, size_t *resultlen)
463 int sw;
465 if (!data || !datalen || !result || !resultlen)
466 return gpg_error (GPG_ERR_INV_VALUE);
467 *result = NULL;
468 *resultlen = 0;
470 sw = apdu_send (slot, 0x00, CMD_PSO, 0x9E, 0x9A, datalen, (const char*)data,
471 result, resultlen);
472 if (sw != SW_SUCCESS)
474 /* Make sure that pending buffers are released. */
475 xfree (*result);
476 *result = NULL;
477 *resultlen = 0;
478 return map_sw (sw);
481 return 0;
485 /* Perform the security operation DECIPHER. PADIND is the padding
486 indicator to be used. It should be 0 if no padding is required, a
487 value of -1 suppresses the padding byte. On success 0 is returned
488 and the plaintext is available in a newly allocated buffer stored
489 at RESULT with its length stored at RESULTLEN. */
490 gpg_error_t
491 iso7816_decipher (int slot, const unsigned char *data, size_t datalen,
492 int padind, unsigned char **result, size_t *resultlen)
494 int sw;
495 unsigned char *buf;
497 if (!data || !datalen || !result || !resultlen)
498 return gpg_error (GPG_ERR_INV_VALUE);
499 *result = NULL;
500 *resultlen = 0;
502 if (padind >= 0)
504 /* We need to prepend the padding indicator. */
505 buf = xtrymalloc (datalen + 1);
506 if (!buf)
507 return gpg_error (gpg_err_code_from_errno (errno));
509 *buf = padind; /* Padding indicator. */
510 memcpy (buf+1, data, datalen);
511 sw = apdu_send (slot, 0x00, CMD_PSO, 0x80, 0x86,
512 datalen+1, (char*)buf,
513 result, resultlen);
514 xfree (buf);
516 else
518 sw = apdu_send (slot, 0x00, CMD_PSO, 0x80, 0x86,
519 datalen, (const char *)data,
520 result, resultlen);
522 if (sw != SW_SUCCESS)
524 /* Make sure that pending buffers are released. */
525 xfree (*result);
526 *result = NULL;
527 *resultlen = 0;
528 return map_sw (sw);
531 return 0;
535 gpg_error_t
536 iso7816_internal_authenticate (int slot,
537 const unsigned char *data, size_t datalen,
538 unsigned char **result, size_t *resultlen)
540 int sw;
542 if (!data || !datalen || !result || !resultlen)
543 return gpg_error (GPG_ERR_INV_VALUE);
544 *result = NULL;
545 *resultlen = 0;
547 sw = apdu_send (slot, 0x00, CMD_INTERNAL_AUTHENTICATE, 0, 0,
548 datalen, (const char*)data, result, resultlen);
549 if (sw != SW_SUCCESS)
551 /* Make sure that pending buffers are released. */
552 xfree (*result);
553 *result = NULL;
554 *resultlen = 0;
555 return map_sw (sw);
558 return 0;
562 static gpg_error_t
563 do_generate_keypair (int slot, int readonly,
564 const unsigned char *data, size_t datalen,
565 unsigned char **result, size_t *resultlen)
567 int sw;
569 if (!data || !datalen || !result || !resultlen)
570 return gpg_error (GPG_ERR_INV_VALUE);
571 *result = NULL;
572 *resultlen = 0;
574 sw = apdu_send (slot, 0x00, CMD_GENERATE_KEYPAIR, readonly? 0x81:0x80, 0,
575 datalen, (const char*)data, result, resultlen);
576 if (sw != SW_SUCCESS)
578 /* Make sure that pending buffers are released. */
579 xfree (*result);
580 *result = NULL;
581 *resultlen = 0;
582 return map_sw (sw);
585 return 0;
589 gpg_error_t
590 iso7816_generate_keypair (int slot,
591 const unsigned char *data, size_t datalen,
592 unsigned char **result, size_t *resultlen)
594 return do_generate_keypair (slot, 0, data, datalen, result, resultlen);
598 gpg_error_t
599 iso7816_read_public_key (int slot,
600 const unsigned char *data, size_t datalen,
601 unsigned char **result, size_t *resultlen)
603 return do_generate_keypair (slot, 1, data, datalen, result, resultlen);
608 gpg_error_t
609 iso7816_get_challenge (int slot, int length, unsigned char *buffer)
611 int sw;
612 unsigned char *result;
613 size_t resultlen, n;
615 if (!buffer || length < 1)
616 return gpg_error (GPG_ERR_INV_VALUE);
620 result = NULL;
621 n = length > 254? 254 : length;
622 sw = apdu_send_le (slot, 0x00, CMD_GET_CHALLENGE, 0, 0, -1, NULL,
624 &result, &resultlen);
625 if (sw != SW_SUCCESS)
627 /* Make sure that pending buffers are released. */
628 xfree (result);
629 return map_sw (sw);
631 if (resultlen > n)
632 resultlen = n;
633 memcpy (buffer, result, resultlen);
634 buffer += resultlen;
635 length -= resultlen;
636 xfree (result);
638 while (length > 0);
640 return 0;
643 /* Perform a READ BINARY command requesting a maximum of NMAX bytes
644 from OFFSET. With NMAX = 0 the entire file is read. The result is
645 stored in a newly allocated buffer at the address passed by RESULT.
646 Returns the length of this data at the address of RESULTLEN. */
647 gpg_error_t
648 iso7816_read_binary (int slot, size_t offset, size_t nmax,
649 unsigned char **result, size_t *resultlen)
651 int sw;
652 unsigned char *buffer;
653 size_t bufferlen;
654 int read_all = !nmax;
655 size_t n;
657 if (!result || !resultlen)
658 return gpg_error (GPG_ERR_INV_VALUE);
659 *result = NULL;
660 *resultlen = 0;
662 /* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
663 we check for this limit. */
664 if (offset > 32767)
665 return gpg_error (GPG_ERR_INV_VALUE);
669 buffer = NULL;
670 bufferlen = 0;
671 /* Note, that we to set N to 254 due to problems either with the
672 ccid driver or some TCOS cards. It actually should be 0
673 which is the official ISO value to read a variable length
674 object. */
675 if (read_all || nmax > 254)
676 n = 254;
677 else
678 n = nmax;
679 sw = apdu_send_le (slot, 0x00, CMD_READ_BINARY,
680 ((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
681 n, &buffer, &bufferlen);
682 if ( SW_EXACT_LENGTH_P(sw) )
684 n = (sw & 0x00ff);
685 sw = apdu_send_le (slot, 0x00, CMD_READ_BINARY,
686 ((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
687 n, &buffer, &bufferlen);
690 if (*result && sw == SW_BAD_P0_P1)
692 /* Bad Parameter means that the offset is outside of the
693 EF. When reading all data we take this as an indication
694 for EOF. */
695 break;
698 if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
700 /* Make sure that pending buffers are released. */
701 xfree (buffer);
702 xfree (*result);
703 *result = NULL;
704 *resultlen = 0;
705 return map_sw (sw);
707 if (*result) /* Need to extend the buffer. */
709 unsigned char *p = xtryrealloc (*result, *resultlen + bufferlen);
710 if (!p)
712 gpg_error_t err = gpg_error_from_syserror ();
713 xfree (buffer);
714 xfree (*result);
715 *result = NULL;
716 *resultlen = 0;
717 return err;
719 *result = p;
720 memcpy (*result + *resultlen, buffer, bufferlen);
721 *resultlen += bufferlen;
722 xfree (buffer);
723 buffer = NULL;
725 else /* Transfer the buffer into our result. */
727 *result = buffer;
728 *resultlen = bufferlen;
730 offset += bufferlen;
731 if (offset > 32767)
732 break; /* We simply truncate the result for too large
733 files. */
734 if (nmax > bufferlen)
735 nmax -= bufferlen;
736 else
737 nmax = 0;
739 while ((read_all && sw != SW_EOF_REACHED) || (!read_all && nmax));
741 return 0;
744 /* Perform a READ RECORD command. RECNO gives the record number to
745 read with 0 indicating the current record. RECCOUNT must be 1 (not
746 all cards support reading of more than one record). SHORT_EF
747 should be 0 to read the current EF or contain a short EF. The
748 result is stored in a newly allocated buffer at the address passed
749 by RESULT. Returns the length of this data at the address of
750 RESULTLEN. */
751 gpg_error_t
752 iso7816_read_record (int slot, int recno, int reccount, int short_ef,
753 unsigned char **result, size_t *resultlen)
755 int sw;
756 unsigned char *buffer;
757 size_t bufferlen;
759 if (!result || !resultlen)
760 return gpg_error (GPG_ERR_INV_VALUE);
761 *result = NULL;
762 *resultlen = 0;
764 /* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
765 we check for this limit. */
766 if (recno < 0 || recno > 255 || reccount != 1
767 || short_ef < 0 || short_ef > 254 )
768 return gpg_error (GPG_ERR_INV_VALUE);
770 buffer = NULL;
771 bufferlen = 0;
772 /* Fixme: Either the ccid driver or the TCOS cards have problems
773 with an Le of 0. */
774 sw = apdu_send_le (slot, 0x00, CMD_READ_RECORD,
775 recno,
776 short_ef? short_ef : 0x04,
777 -1, NULL,
778 254, &buffer, &bufferlen);
780 if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
782 /* Make sure that pending buffers are released. */
783 xfree (buffer);
784 xfree (*result);
785 *result = NULL;
786 *resultlen = 0;
787 return map_sw (sw);
789 *result = buffer;
790 *resultlen = bufferlen;
792 return 0;