2008-02-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / scd / iso7816.c
blobb38ae4fd0bec78c20d063a304853a9f0f8d436a1
1 /* iso7816.c - ISO 7816 commands
2 * Copyright (C) 2003, 2004 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_WRONG_LENGTH: ec = GPG_ERR_INV_VALUE; break;
70 case SW_CHV_WRONG: ec = GPG_ERR_BAD_PIN; break;
71 case SW_CHV_BLOCKED: ec = GPG_ERR_PIN_BLOCKED; break;
72 case SW_USE_CONDITIONS: ec = GPG_ERR_USE_CONDITIONS; break;
73 case SW_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
74 case SW_BAD_PARAMETER: ec = GPG_ERR_INV_VALUE; break;
75 case SW_FILE_NOT_FOUND: ec = GPG_ERR_ENOENT; break;
76 case SW_RECORD_NOT_FOUND:ec= GPG_ERR_NOT_FOUND; break;
77 case SW_REF_NOT_FOUND: ec = GPG_ERR_NO_OBJ; break;
78 case SW_BAD_P0_P1: ec = GPG_ERR_INV_VALUE; break;
79 case SW_EXACT_LENGTH: ec = GPG_ERR_INV_VALUE; break;
80 case SW_INS_NOT_SUP: ec = GPG_ERR_CARD; break;
81 case SW_CLA_NOT_SUP: ec = GPG_ERR_CARD; break;
82 case SW_SUCCESS: ec = 0; break;
84 case SW_HOST_OUT_OF_CORE: ec = GPG_ERR_ENOMEM; break;
85 case SW_HOST_INV_VALUE: ec = GPG_ERR_INV_VALUE; break;
86 case SW_HOST_INCOMPLETE_CARD_RESPONSE: ec = GPG_ERR_CARD; break;
87 case SW_HOST_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
88 case SW_HOST_LOCKING_FAILED: ec = GPG_ERR_BUG; break;
89 case SW_HOST_BUSY: ec = GPG_ERR_EBUSY; break;
90 case SW_HOST_NO_CARD: ec = GPG_ERR_CARD_NOT_PRESENT; break;
91 case SW_HOST_CARD_INACTIVE: ec = GPG_ERR_CARD_RESET; break;
92 case SW_HOST_CARD_IO_ERROR: ec = GPG_ERR_EIO; break;
93 case SW_HOST_GENERAL_ERROR: ec = GPG_ERR_GENERAL; break;
94 case SW_HOST_NO_READER: ec = GPG_ERR_ENODEV; break;
95 case SW_HOST_ABORTED: ec = GPG_ERR_CANCELED; break;
96 case SW_HOST_NO_KEYPAD: ec = GPG_ERR_NOT_SUPPORTED; break;
98 default:
99 if ((sw & 0x010000))
100 ec = GPG_ERR_GENERAL; /* Should not happen. */
101 else if ((sw & 0xff00) == SW_MORE_DATA)
102 ec = 0; /* This should actually never been seen here. */
103 else
104 ec = GPG_ERR_CARD;
106 return gpg_error (ec);
109 /* Map a status word from the APDU layer to a gpg-error code. */
110 gpg_error_t
111 iso7816_map_sw (int sw)
113 /* All APDU functions should return 0x9000 on success but for
114 historical reasons of the implementation some return 0 to
115 indicate success. We allow for that here. */
116 return sw? map_sw (sw) : 0;
120 /* This function is specialized version of the SELECT FILE command.
121 SLOT is the card and reader as created for example by
122 apdu_open_reader (), AID is a buffer of size AIDLEN holding the
123 requested application ID. The function can't be used to enumerate
124 AIDs and won't return the AID on success. The return value is 0
125 for okay or a GPG error code. Note that ISO error codes are
126 internally mapped. Bit 0 of FLAGS should be set if the card does
127 not understand P2=0xC0. */
128 gpg_error_t
129 iso7816_select_application (int slot, const char *aid, size_t aidlen,
130 unsigned int flags)
132 int sw;
133 sw = apdu_send_simple (slot, 0x00, CMD_SELECT_FILE, 4,
134 (flags&1)? 0 :0x0c, aidlen, aid);
135 return map_sw (sw);
139 gpg_error_t
140 iso7816_select_file (int slot, int tag, int is_dir,
141 unsigned char **result, size_t *resultlen)
143 int sw, p0, p1;
144 unsigned char tagbuf[2];
146 tagbuf[0] = (tag >> 8) & 0xff;
147 tagbuf[1] = tag & 0xff;
149 if (result || resultlen)
151 *result = NULL;
152 *resultlen = 0;
153 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
155 else
157 p0 = (tag == 0x3F00)? 0: is_dir? 1:2;
158 p1 = 0x0c; /* No FC return. */
159 sw = apdu_send_simple (slot, 0x00, CMD_SELECT_FILE,
160 p0, p1, 2, (char*)tagbuf );
161 return map_sw (sw);
164 return 0;
168 /* Do a select file command with a direct path. */
169 gpg_error_t
170 iso7816_select_path (int slot, const unsigned short *path, size_t pathlen,
171 unsigned char **result, size_t *resultlen)
173 int sw, p0, p1;
174 unsigned char buffer[100];
175 int buflen;
177 if (result || resultlen)
179 *result = NULL;
180 *resultlen = 0;
181 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
184 if (pathlen/2 >= sizeof buffer)
185 return gpg_error (GPG_ERR_TOO_LARGE);
187 for (buflen = 0; pathlen; pathlen--, path++)
189 buffer[buflen++] = (*path >> 8);
190 buffer[buflen++] = *path;
193 p0 = 0x08;
194 p1 = 0x0c; /* No FC return. */
195 sw = apdu_send_simple (slot, 0x00, CMD_SELECT_FILE,
196 p0, p1, buflen, (char*)buffer );
197 return map_sw (sw);
201 /* This is a private command currently only working for TCOS cards. */
202 gpg_error_t
203 iso7816_list_directory (int slot, int list_dirs,
204 unsigned char **result, size_t *resultlen)
206 int sw;
208 if (!result || !resultlen)
209 return gpg_error (GPG_ERR_INV_VALUE);
210 *result = NULL;
211 *resultlen = 0;
213 sw = apdu_send (slot, 0x80, 0xAA, list_dirs? 1:2, 0, -1, NULL,
214 result, resultlen);
215 if (sw != SW_SUCCESS)
217 /* Make sure that pending buffers are released. */
218 xfree (*result);
219 *result = NULL;
220 *resultlen = 0;
222 return map_sw (sw);
226 /* Check whether the reader supports the ISO command code COMMAND on
227 the keypad. Returns 0 on success. */
228 gpg_error_t
229 iso7816_check_keypad (int slot, int command, iso7816_pininfo_t *pininfo)
231 int sw;
233 sw = apdu_check_keypad (slot, command,
234 pininfo->mode, pininfo->minlen, pininfo->maxlen,
235 pininfo->padlen);
236 return iso7816_map_sw (sw);
240 /* Perform a VERIFY command on SLOT using the card holder verification
241 vector CHVNO with a CHV of lenght CHVLEN. With PININFO non-NULL
242 the keypad of the reader will be used. Returns 0 on success. */
243 gpg_error_t
244 iso7816_verify_kp (int slot, int chvno, const char *chv, size_t chvlen,
245 iso7816_pininfo_t *pininfo)
247 int sw;
249 if (pininfo && pininfo->mode)
250 sw = apdu_send_simple_kp (slot, 0x00, CMD_VERIFY, 0, chvno, chvlen, chv,
251 pininfo->mode,
252 pininfo->minlen,
253 pininfo->maxlen,
254 pininfo->padlen);
255 else
256 sw = apdu_send_simple (slot, 0x00, CMD_VERIFY, 0, chvno, chvlen, chv);
257 return map_sw (sw);
260 /* Perform a VERIFY command on SLOT using the card holder verification
261 vector CHVNO with a CHV of lenght CHVLEN. Returns 0 on success. */
262 gpg_error_t
263 iso7816_verify (int slot, int chvno, const char *chv, size_t chvlen)
265 return iso7816_verify_kp (slot, chvno, chv, chvlen, NULL);
268 /* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
269 verification vector CHVNO. If the OLDCHV is NULL (and OLDCHVLEN
270 0), a "change reference data" is done, otherwise an "exchange
271 reference data". The new reference data is expected in NEWCHV of
272 length NEWCHVLEN. With PININFO non-NULL the keypad of the reader
273 will be used. */
274 gpg_error_t
275 iso7816_change_reference_data_kp (int slot, int chvno,
276 const char *oldchv, size_t oldchvlen,
277 const char *newchv, size_t newchvlen,
278 iso7816_pininfo_t *pininfo)
280 int sw;
281 char *buf;
283 if ((!oldchv && oldchvlen)
284 || (oldchv && !oldchvlen)
285 || !newchv || !newchvlen )
286 return gpg_error (GPG_ERR_INV_VALUE);
288 buf = xtrymalloc (oldchvlen + newchvlen);
289 if (!buf)
290 return gpg_error (gpg_err_code_from_errno (errno));
291 if (oldchvlen)
292 memcpy (buf, oldchv, oldchvlen);
293 memcpy (buf+oldchvlen, newchv, newchvlen);
295 if (pininfo && pininfo->mode)
296 sw = apdu_send_simple_kp (slot, 0x00, CMD_CHANGE_REFERENCE_DATA,
297 oldchvlen? 0 : 1, chvno, oldchvlen+newchvlen, buf,
298 pininfo->mode,
299 pininfo->minlen,
300 pininfo->maxlen,
301 pininfo->padlen);
302 else
303 sw = apdu_send_simple (slot, 0x00, CMD_CHANGE_REFERENCE_DATA,
304 oldchvlen? 0 : 1, chvno, oldchvlen+newchvlen, buf);
305 xfree (buf);
306 return map_sw (sw);
310 /* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
311 verification vector CHVNO. If the OLDCHV is NULL (and OLDCHVLEN
312 0), a "change reference data" is done, otherwise an "exchange
313 reference data". The new reference data is expected in NEWCHV of
314 length NEWCHVLEN. */
315 gpg_error_t
316 iso7816_change_reference_data (int slot, int chvno,
317 const char *oldchv, size_t oldchvlen,
318 const char *newchv, size_t newchvlen)
320 return iso7816_change_reference_data_kp (slot, chvno, oldchv, oldchvlen,
321 newchv, newchvlen, NULL);
325 gpg_error_t
326 iso7816_reset_retry_counter_kp (int slot, int chvno,
327 const char *newchv, size_t newchvlen,
328 iso7816_pininfo_t *pininfo)
330 int sw;
332 if (!newchv || !newchvlen )
333 return gpg_error (GPG_ERR_INV_VALUE);
335 if (pininfo && pininfo->mode)
336 sw = apdu_send_simple_kp (slot, 0x00, CMD_RESET_RETRY_COUNTER,
337 2, chvno, newchvlen, newchv,
338 pininfo->mode,
339 pininfo->minlen,
340 pininfo->maxlen,
341 pininfo->padlen);
342 else
343 sw = apdu_send_simple (slot, 0x00, CMD_RESET_RETRY_COUNTER,
344 2, chvno, newchvlen, newchv);
345 return map_sw (sw);
349 gpg_error_t
350 iso7816_reset_retry_counter (int slot, int chvno,
351 const char *newchv, size_t newchvlen)
353 return iso7816_reset_retry_counter_kp (slot, chvno, newchv, newchvlen, NULL);
358 /* Perform a GET DATA command requesting TAG and storing the result in
359 a newly allocated buffer at the address passed by RESULT. Return
360 the length of this data at the address of RESULTLEN. */
361 gpg_error_t
362 iso7816_get_data (int slot, int tag,
363 unsigned char **result, size_t *resultlen)
365 int sw;
367 if (!result || !resultlen)
368 return gpg_error (GPG_ERR_INV_VALUE);
369 *result = NULL;
370 *resultlen = 0;
372 sw = apdu_send (slot, 0x00, CMD_GET_DATA,
373 ((tag >> 8) & 0xff), (tag & 0xff), -1, NULL,
374 result, resultlen);
375 if (sw != SW_SUCCESS)
377 /* Make sure that pending buffers are released. */
378 xfree (*result);
379 *result = NULL;
380 *resultlen = 0;
381 return map_sw (sw);
384 return 0;
388 /* Perform a PUT DATA command on card in SLOT. Write DATA of length
389 DATALEN to TAG. */
390 gpg_error_t
391 iso7816_put_data (int slot, int tag,
392 const unsigned char *data, size_t datalen)
394 int sw;
396 sw = apdu_send_simple (slot, 0x00, CMD_PUT_DATA,
397 ((tag >> 8) & 0xff), (tag & 0xff),
398 datalen, (const char*)data);
399 return map_sw (sw);
402 /* Manage Security Environment. This is a weird operation and there
403 is no easy abstraction for it. Furthermore, some card seem to have
404 a different interpreation of 7816-8 and thus we resort to let the
405 caller decide what to do. */
406 gpg_error_t
407 iso7816_manage_security_env (int slot, int p1, int p2,
408 const unsigned char *data, size_t datalen)
410 int sw;
412 if (p1 < 0 || p1 > 255 || p2 < 0 || p2 > 255 )
413 return gpg_error (GPG_ERR_INV_VALUE);
415 sw = apdu_send_simple (slot, 0x00, CMD_MSE, p1, p2,
416 data? datalen : -1, (const char*)data);
417 return map_sw (sw);
421 /* Perform the security operation COMPUTE DIGITAL SIGANTURE. On
422 success 0 is returned and the data is availavle in a newly
423 allocated buffer stored at RESULT with its length stored at
424 RESULTLEN. */
425 gpg_error_t
426 iso7816_compute_ds (int slot, const unsigned char *data, size_t datalen,
427 unsigned char **result, size_t *resultlen)
429 int sw;
431 if (!data || !datalen || !result || !resultlen)
432 return gpg_error (GPG_ERR_INV_VALUE);
433 *result = NULL;
434 *resultlen = 0;
436 sw = apdu_send (slot, 0x00, CMD_PSO, 0x9E, 0x9A, datalen, (const char*)data,
437 result, resultlen);
438 if (sw != SW_SUCCESS)
440 /* Make sure that pending buffers are released. */
441 xfree (*result);
442 *result = NULL;
443 *resultlen = 0;
444 return map_sw (sw);
447 return 0;
451 /* Perform the security operation DECIPHER. PADIND is the padding
452 indicator to be used. It should be 0 if no padding is required, a
453 value of -1 suppresses the padding byte. On success 0 is returned
454 and the plaintext is available in a newly allocated buffer stored
455 at RESULT with its length stored at RESULTLEN. */
456 gpg_error_t
457 iso7816_decipher (int slot, const unsigned char *data, size_t datalen,
458 int padind, unsigned char **result, size_t *resultlen)
460 int sw;
461 unsigned char *buf;
463 if (!data || !datalen || !result || !resultlen)
464 return gpg_error (GPG_ERR_INV_VALUE);
465 *result = NULL;
466 *resultlen = 0;
468 if (padind >= 0)
470 /* We need to prepend the padding indicator. */
471 buf = xtrymalloc (datalen + 1);
472 if (!buf)
473 return gpg_error (gpg_err_code_from_errno (errno));
475 *buf = padind; /* Padding indicator. */
476 memcpy (buf+1, data, datalen);
477 sw = apdu_send (slot, 0x00, CMD_PSO, 0x80, 0x86,
478 datalen+1, (char*)buf,
479 result, resultlen);
480 xfree (buf);
482 else
484 sw = apdu_send (slot, 0x00, CMD_PSO, 0x80, 0x86,
485 datalen, (const char *)data,
486 result, resultlen);
488 if (sw != SW_SUCCESS)
490 /* Make sure that pending buffers are released. */
491 xfree (*result);
492 *result = NULL;
493 *resultlen = 0;
494 return map_sw (sw);
497 return 0;
501 gpg_error_t
502 iso7816_internal_authenticate (int slot,
503 const unsigned char *data, size_t datalen,
504 unsigned char **result, size_t *resultlen)
506 int sw;
508 if (!data || !datalen || !result || !resultlen)
509 return gpg_error (GPG_ERR_INV_VALUE);
510 *result = NULL;
511 *resultlen = 0;
513 sw = apdu_send (slot, 0x00, CMD_INTERNAL_AUTHENTICATE, 0, 0,
514 datalen, (const char*)data, result, resultlen);
515 if (sw != SW_SUCCESS)
517 /* Make sure that pending buffers are released. */
518 xfree (*result);
519 *result = NULL;
520 *resultlen = 0;
521 return map_sw (sw);
524 return 0;
528 static gpg_error_t
529 do_generate_keypair (int slot, int readonly,
530 const unsigned char *data, size_t datalen,
531 unsigned char **result, size_t *resultlen)
533 int sw;
535 if (!data || !datalen || !result || !resultlen)
536 return gpg_error (GPG_ERR_INV_VALUE);
537 *result = NULL;
538 *resultlen = 0;
540 sw = apdu_send (slot, 0x00, CMD_GENERATE_KEYPAIR, readonly? 0x81:0x80, 0,
541 datalen, (const char*)data, result, resultlen);
542 if (sw != SW_SUCCESS)
544 /* Make sure that pending buffers are released. */
545 xfree (*result);
546 *result = NULL;
547 *resultlen = 0;
548 return map_sw (sw);
551 return 0;
555 gpg_error_t
556 iso7816_generate_keypair (int slot,
557 const unsigned char *data, size_t datalen,
558 unsigned char **result, size_t *resultlen)
560 return do_generate_keypair (slot, 0, data, datalen, result, resultlen);
564 gpg_error_t
565 iso7816_read_public_key (int slot,
566 const unsigned char *data, size_t datalen,
567 unsigned char **result, size_t *resultlen)
569 return do_generate_keypair (slot, 1, data, datalen, result, resultlen);
574 gpg_error_t
575 iso7816_get_challenge (int slot, int length, unsigned char *buffer)
577 int sw;
578 unsigned char *result;
579 size_t resultlen, n;
581 if (!buffer || length < 1)
582 return gpg_error (GPG_ERR_INV_VALUE);
586 result = NULL;
587 n = length > 254? 254 : length;
588 sw = apdu_send_le (slot, 0x00, CMD_GET_CHALLENGE, 0, 0, -1, NULL,
590 &result, &resultlen);
591 if (sw != SW_SUCCESS)
593 /* Make sure that pending buffers are released. */
594 xfree (result);
595 return map_sw (sw);
597 if (resultlen > n)
598 resultlen = n;
599 memcpy (buffer, result, resultlen);
600 buffer += resultlen;
601 length -= resultlen;
602 xfree (result);
604 while (length > 0);
606 return 0;
609 /* Perform a READ BINARY command requesting a maximum of NMAX bytes
610 from OFFSET. With NMAX = 0 the entire file is read. The result is
611 stored in a newly allocated buffer at the address passed by RESULT.
612 Returns the length of this data at the address of RESULTLEN. */
613 gpg_error_t
614 iso7816_read_binary (int slot, size_t offset, size_t nmax,
615 unsigned char **result, size_t *resultlen)
617 int sw;
618 unsigned char *buffer;
619 size_t bufferlen;
620 int read_all = !nmax;
621 size_t n;
623 if (!result || !resultlen)
624 return gpg_error (GPG_ERR_INV_VALUE);
625 *result = NULL;
626 *resultlen = 0;
628 /* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
629 we check for this limit. */
630 if (offset > 32767)
631 return gpg_error (GPG_ERR_INV_VALUE);
635 buffer = NULL;
636 bufferlen = 0;
637 /* Note, that we to set N to 254 due to problems either with the
638 ccid driver or some TCOS cards. It actually should be 0
639 which is the official ISO value to read a variable length
640 object. */
641 if (read_all || nmax > 254)
642 n = 254;
643 else
644 n = nmax;
645 sw = apdu_send_le (slot, 0x00, CMD_READ_BINARY,
646 ((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
647 n, &buffer, &bufferlen);
648 if ( SW_EXACT_LENGTH_P(sw) )
650 n = (sw & 0x00ff);
651 sw = apdu_send_le (slot, 0x00, CMD_READ_BINARY,
652 ((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
653 n, &buffer, &bufferlen);
656 if (*result && sw == SW_BAD_P0_P1)
658 /* Bad Parameter means that the offset is outside of the
659 EF. When reading all data we take this as an indication
660 for EOF. */
661 break;
664 if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
666 /* Make sure that pending buffers are released. */
667 xfree (buffer);
668 xfree (*result);
669 *result = NULL;
670 *resultlen = 0;
671 return map_sw (sw);
673 if (*result) /* Need to extend the buffer. */
675 unsigned char *p = xtryrealloc (*result, *resultlen + bufferlen);
676 if (!p)
678 gpg_error_t err = gpg_error_from_syserror ();
679 xfree (buffer);
680 xfree (*result);
681 *result = NULL;
682 *resultlen = 0;
683 return err;
685 *result = p;
686 memcpy (*result + *resultlen, buffer, bufferlen);
687 *resultlen += bufferlen;
688 xfree (buffer);
689 buffer = NULL;
691 else /* Transfer the buffer into our result. */
693 *result = buffer;
694 *resultlen = bufferlen;
696 offset += bufferlen;
697 if (offset > 32767)
698 break; /* We simply truncate the result for too large
699 files. */
700 if (nmax > bufferlen)
701 nmax -= bufferlen;
702 else
703 nmax = 0;
705 while ((read_all && sw != SW_EOF_REACHED) || (!read_all && nmax));
707 return 0;
710 /* Perform a READ RECORD command. RECNO gives the record number to
711 read with 0 indicating the current record. RECCOUNT must be 1 (not
712 all cards support reading of more than one record). SHORT_EF
713 should be 0 to read the current EF or contain a short EF. The
714 result is stored in a newly allocated buffer at the address passed
715 by RESULT. Returns the length of this data at the address of
716 RESULTLEN. */
717 gpg_error_t
718 iso7816_read_record (int slot, int recno, int reccount, int short_ef,
719 unsigned char **result, size_t *resultlen)
721 int sw;
722 unsigned char *buffer;
723 size_t bufferlen;
725 if (!result || !resultlen)
726 return gpg_error (GPG_ERR_INV_VALUE);
727 *result = NULL;
728 *resultlen = 0;
730 /* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
731 we check for this limit. */
732 if (recno < 0 || recno > 255 || reccount != 1
733 || short_ef < 0 || short_ef > 254 )
734 return gpg_error (GPG_ERR_INV_VALUE);
736 buffer = NULL;
737 bufferlen = 0;
738 /* Fixme: Either the ccid driver or the TCOS cards have problems
739 with an Le of 0. */
740 sw = apdu_send_le (slot, 0x00, CMD_READ_RECORD,
741 recno,
742 short_ef? short_ef : 0x04,
743 -1, NULL,
744 254, &buffer, &bufferlen);
746 if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
748 /* Make sure that pending buffers are released. */
749 xfree (buffer);
750 xfree (*result);
751 *result = NULL;
752 *resultlen = 0;
753 return map_sw (sw);
755 *result = buffer;
756 *resultlen = bufferlen;
758 return 0;