2003-08-05 19:11:04 +02:00
|
|
|
/* iso7816.c - ISO 7816 commands
|
2009-01-08 20:56:30 +01:00
|
|
|
* Copyright (C) 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-07-12 12:49:12 +02:00
|
|
|
#if defined(GNUPG_MAJOR_VERSION)
|
2019-05-16 08:24:29 +02:00
|
|
|
# include "scdaemon.h"
|
2019-07-12 12:49:12 +02:00
|
|
|
#endif /*GNUPG_MAJOR_VERSION*/
|
2003-10-02 12:27:34 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
#include "iso7816.h"
|
|
|
|
#include "apdu.h"
|
2003-10-02 12:27:34 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
#define CMD_SELECT_FILE 0xA4
|
2021-03-19 18:26:03 +01:00
|
|
|
#define CMD_SELECT_DATA 0xA5
|
2005-11-28 12:52:25 +01:00
|
|
|
#define CMD_VERIFY ISO7816_VERIFY
|
|
|
|
#define CMD_CHANGE_REFERENCE_DATA ISO7816_CHANGE_REFERENCE_DATA
|
|
|
|
#define CMD_RESET_RETRY_COUNTER ISO7816_RESET_RETRY_COUNTER
|
2003-08-05 19:11:04 +02:00
|
|
|
#define CMD_GET_DATA 0xCA
|
|
|
|
#define CMD_PUT_DATA 0xDA
|
2004-01-28 17:21:57 +01:00
|
|
|
#define CMD_MSE 0x22
|
2003-08-05 19:11:04 +02:00
|
|
|
#define CMD_PSO 0x2A
|
2019-01-20 11:41:23 +01:00
|
|
|
#define CMD_GENERAL_AUTHENTICATE 0x87
|
2003-08-05 19:11:04 +02:00
|
|
|
#define CMD_INTERNAL_AUTHENTICATE 0x88
|
|
|
|
#define CMD_GENERATE_KEYPAIR 0x47
|
|
|
|
#define CMD_GET_CHALLENGE 0x84
|
2004-01-27 17:40:42 +01:00
|
|
|
#define CMD_READ_BINARY 0xB0
|
|
|
|
#define CMD_READ_RECORD 0xB2
|
2020-07-02 18:35:34 +02:00
|
|
|
#define CMD_UPDATE_BINARY 0xD6
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
static gpg_error_t
|
|
|
|
map_sw (int sw)
|
|
|
|
{
|
|
|
|
gpg_err_code_t ec;
|
|
|
|
|
|
|
|
switch (sw)
|
|
|
|
{
|
|
|
|
case SW_EEPROM_FAILURE: ec = GPG_ERR_HARDWARE; break;
|
2014-12-15 17:38:40 +01:00
|
|
|
case SW_TERM_STATE: ec = GPG_ERR_OBJ_TERM_STATE; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_WRONG_LENGTH: ec = GPG_ERR_INV_VALUE; break;
|
2019-02-21 07:16:11 +01:00
|
|
|
case SW_ACK_TIMEOUT: ec = GPG_ERR_TIMEOUT; break;
|
2008-09-23 11:57:45 +02:00
|
|
|
case SW_SM_NOT_SUP: ec = GPG_ERR_NOT_SUPPORTED; break;
|
|
|
|
case SW_CC_NOT_SUP: ec = GPG_ERR_NOT_SUPPORTED; break;
|
2021-02-23 12:56:42 +01:00
|
|
|
case SW_FILE_STRUCT: ec = GPG_ERR_CARD; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_CHV_WRONG: ec = GPG_ERR_BAD_PIN; break;
|
|
|
|
case SW_CHV_BLOCKED: ec = GPG_ERR_PIN_BLOCKED; break;
|
|
|
|
case SW_USE_CONDITIONS: ec = GPG_ERR_USE_CONDITIONS; break;
|
2021-01-27 18:31:12 +01:00
|
|
|
case SW_NO_CURRENT_EF: ec = GPG_ERR_ENOENT; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
|
|
|
|
case SW_BAD_PARAMETER: ec = GPG_ERR_INV_VALUE; break;
|
2004-01-27 17:40:42 +01:00
|
|
|
case SW_FILE_NOT_FOUND: ec = GPG_ERR_ENOENT; break;
|
|
|
|
case SW_RECORD_NOT_FOUND:ec= GPG_ERR_NOT_FOUND; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_REF_NOT_FOUND: ec = GPG_ERR_NO_OBJ; break;
|
2019-07-22 03:14:21 +02:00
|
|
|
case SW_INCORRECT_P0_P1:ec = GPG_ERR_INV_VALUE; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_BAD_P0_P1: ec = GPG_ERR_INV_VALUE; break;
|
2005-09-05 16:36:36 +02:00
|
|
|
case SW_EXACT_LENGTH: ec = GPG_ERR_INV_VALUE; break;
|
2003-08-05 19:11:04 +02:00
|
|
|
case SW_INS_NOT_SUP: ec = GPG_ERR_CARD; break;
|
|
|
|
case SW_CLA_NOT_SUP: ec = GPG_ERR_CARD; break;
|
|
|
|
case SW_SUCCESS: ec = 0; break;
|
|
|
|
|
|
|
|
case SW_HOST_OUT_OF_CORE: ec = GPG_ERR_ENOMEM; break;
|
|
|
|
case SW_HOST_INV_VALUE: ec = GPG_ERR_INV_VALUE; break;
|
|
|
|
case SW_HOST_INCOMPLETE_CARD_RESPONSE: ec = GPG_ERR_CARD; break;
|
2004-04-20 16:17:10 +02:00
|
|
|
case SW_HOST_NOT_SUPPORTED: ec = GPG_ERR_NOT_SUPPORTED; break;
|
|
|
|
case SW_HOST_LOCKING_FAILED: ec = GPG_ERR_BUG; break;
|
|
|
|
case SW_HOST_BUSY: ec = GPG_ERR_EBUSY; break;
|
2004-07-16 17:45:25 +02:00
|
|
|
case SW_HOST_NO_CARD: ec = GPG_ERR_CARD_NOT_PRESENT; break;
|
|
|
|
case SW_HOST_CARD_INACTIVE: ec = GPG_ERR_CARD_RESET; break;
|
|
|
|
case SW_HOST_CARD_IO_ERROR: ec = GPG_ERR_EIO; break;
|
|
|
|
case SW_HOST_GENERAL_ERROR: ec = GPG_ERR_GENERAL; break;
|
|
|
|
case SW_HOST_NO_READER: ec = GPG_ERR_ENODEV; break;
|
2017-09-20 03:06:43 +02:00
|
|
|
case SW_HOST_ABORTED: ec = GPG_ERR_INV_RESPONSE; break;
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
case SW_HOST_NO_PINPAD: ec = GPG_ERR_NOT_SUPPORTED; break;
|
2017-09-20 03:06:43 +02:00
|
|
|
case SW_HOST_CANCELLED: ec = GPG_ERR_CANCELED; break;
|
2020-08-12 16:06:49 +02:00
|
|
|
case SW_HOST_USB_OTHER: ec = GPG_ERR_EIO; break;
|
|
|
|
case SW_HOST_USB_IO: ec = GPG_ERR_EIO; break;
|
|
|
|
case SW_HOST_USB_ACCESS: ec = GPG_ERR_EACCES; break;
|
|
|
|
case SW_HOST_USB_NO_DEVICE: ec = GPG_ERR_ENODEV; break;
|
|
|
|
case SW_HOST_USB_BUSY: ec = GPG_ERR_EBUSY; break;
|
|
|
|
case SW_HOST_USB_TIMEOUT: ec = GPG_ERR_TIMEOUT; break;
|
|
|
|
case SW_HOST_USB_OVERFLOW: ec = GPG_ERR_EOVERFLOW; break;
|
2020-11-27 07:17:56 +01:00
|
|
|
case SW_HOST_UI_CANCELLED: ec = GPG_ERR_CANCELED; break;
|
|
|
|
case SW_HOST_UI_TIMEOUT: ec = GPG_ERR_TIMEOUT; break;
|
2004-07-16 17:45:25 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
default:
|
|
|
|
if ((sw & 0x010000))
|
|
|
|
ec = GPG_ERR_GENERAL; /* Should not happen. */
|
|
|
|
else if ((sw & 0xff00) == SW_MORE_DATA)
|
|
|
|
ec = 0; /* This should actually never been seen here. */
|
2020-04-07 19:09:10 +02:00
|
|
|
else if ((sw & 0xfff0) == 0x63C0)
|
|
|
|
ec = GPG_ERR_BAD_PIN;
|
2003-08-05 19:11:04 +02:00
|
|
|
else
|
|
|
|
ec = GPG_ERR_CARD;
|
|
|
|
}
|
|
|
|
return gpg_error (ec);
|
|
|
|
}
|
|
|
|
|
2004-10-14 11:12:36 +02:00
|
|
|
/* Map a status word from the APDU layer to a gpg-error code. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_map_sw (int sw)
|
|
|
|
{
|
|
|
|
/* All APDU functions should return 0x9000 on success but for
|
|
|
|
historical reasons of the implementation some return 0 to
|
|
|
|
indicate success. We allow for that here. */
|
|
|
|
return sw? map_sw (sw) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* This function is specialized version of the SELECT FILE command.
|
|
|
|
SLOT is the card and reader as created for example by
|
|
|
|
apdu_open_reader (), AID is a buffer of size AIDLEN holding the
|
|
|
|
requested application ID. The function can't be used to enumerate
|
|
|
|
AIDs and won't return the AID on success. The return value is 0
|
2004-01-27 17:40:42 +01:00
|
|
|
for okay or a GPG error code. Note that ISO error codes are
|
2005-11-28 12:52:25 +01:00
|
|
|
internally mapped. Bit 0 of FLAGS should be set if the card does
|
|
|
|
not understand P2=0xC0. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2005-11-28 12:52:25 +01:00
|
|
|
iso7816_select_application (int slot, const char *aid, size_t aidlen,
|
|
|
|
unsigned int flags)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
2008-09-23 11:57:45 +02:00
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE, 4,
|
2005-11-28 12:52:25 +01:00
|
|
|
(flags&1)? 0 :0x0c, aidlen, aid);
|
2003-08-05 19:11:04 +02:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-03 15:18:15 +01:00
|
|
|
/* This is the same as iso7816_select_application but may return data
|
|
|
|
* at RESULT,RESULTLEN). */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_select_application_ext (int slot, const char *aid, size_t aidlen,
|
|
|
|
unsigned int flags,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
sw = apdu_send (slot, 0, 0x00, CMD_SELECT_FILE, 4,
|
|
|
|
(flags&1)? 0:0x0c, aidlen, aid,
|
|
|
|
result, resultlen);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-07 16:00:11 +02:00
|
|
|
/* Simple MF selection as supported by some cards. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_select_mf (int slot)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
2023-11-16 17:04:02 +01:00
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE, 0x00, 0x0c, -1, NULL);
|
2020-04-07 16:00:11 +02:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
gpg_error_t
|
2017-03-06 05:39:46 +01:00
|
|
|
iso7816_select_file (int slot, int tag, int is_dir)
|
2004-01-27 17:40:42 +01:00
|
|
|
{
|
|
|
|
int sw, p0, p1;
|
|
|
|
unsigned char tagbuf[2];
|
|
|
|
|
|
|
|
tagbuf[0] = (tag >> 8) & 0xff;
|
|
|
|
tagbuf[1] = tag & 0xff;
|
|
|
|
|
2017-03-06 05:39:46 +01:00
|
|
|
p0 = (tag == 0x3F00)? 0: is_dir? 1:2;
|
|
|
|
p1 = 0x0c; /* No FC return. */
|
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE,
|
|
|
|
p0, p1, 2, (char*)tagbuf );
|
|
|
|
return map_sw (sw);
|
2004-01-27 17:40:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-27 18:31:12 +01:00
|
|
|
/* Do a select file command with a direct path. If TOPDF is set, the
|
|
|
|
* actual used path is 3f00/<topdf>/<path>. */
|
2005-09-05 16:36:36 +02:00
|
|
|
gpg_error_t
|
2021-01-26 17:42:55 +01:00
|
|
|
iso7816_select_path (int slot, const unsigned short *path, size_t pathlen,
|
2021-01-27 18:31:12 +01:00
|
|
|
unsigned short topdf)
|
2005-09-05 16:36:36 +02:00
|
|
|
{
|
|
|
|
int sw, p0, p1;
|
|
|
|
unsigned char buffer[100];
|
2021-01-27 18:31:12 +01:00
|
|
|
int buflen = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2021-01-27 18:31:12 +01:00
|
|
|
if (pathlen*2 + 2 >= sizeof buffer)
|
2005-09-05 16:36:36 +02:00
|
|
|
return gpg_error (GPG_ERR_TOO_LARGE);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2021-01-27 18:31:12 +01:00
|
|
|
if (topdf)
|
|
|
|
{
|
|
|
|
buffer[buflen++] = topdf >> 8;
|
|
|
|
buffer[buflen++] = topdf;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; pathlen; pathlen--, path++)
|
2005-09-05 16:36:36 +02:00
|
|
|
{
|
|
|
|
buffer[buflen++] = (*path >> 8);
|
|
|
|
buffer[buflen++] = *path;
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:31:12 +01:00
|
|
|
p0 = 0x08;
|
2005-09-05 16:36:36 +02:00
|
|
|
p1 = 0x0c; /* No FC return. */
|
2008-09-23 11:57:45 +02:00
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_SELECT_FILE,
|
2005-09-05 16:36:36 +02:00
|
|
|
p0, p1, buflen, (char*)buffer );
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
/* This is a private command currently only working for TCOS cards. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_list_directory (int slot, int list_dirs,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-03-30 14:46:06 +02:00
|
|
|
sw = apdu_send (slot, 0, 0x80, 0xAA, list_dirs? 1:2, 0, -1, NULL,
|
2004-01-27 17:40:42 +01:00
|
|
|
result, resultlen);
|
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
}
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 15:49:20 +01:00
|
|
|
/* Wrapper around apdu_send. RESULT can be NULL if no result is
|
|
|
|
* expected. In addition to an gpg-error return code the actual
|
|
|
|
* status word is stored at R_SW unless that is NULL. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_send_apdu (int slot, int extended_mode,
|
|
|
|
int class, int ins, int p0, int p1,
|
|
|
|
int lc, const void *data,
|
|
|
|
unsigned int *r_sw,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw = apdu_send (slot, extended_mode, class, ins, p0, p1, lc, data,
|
|
|
|
result, resultlen);
|
|
|
|
if (sw != SW_SUCCESS && result)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
}
|
|
|
|
if (r_sw)
|
|
|
|
*r_sw = sw;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 03:06:33 +02:00
|
|
|
/* This function sends an already formatted APDU to the card. With
|
2009-01-08 20:56:30 +01:00
|
|
|
HANDLE_MORE set to true a MORE DATA status will be handled
|
|
|
|
internally. The return value is a gpg error code (i.e. a mapped
|
|
|
|
status word). This is basically the same as apdu_send_direct but
|
|
|
|
it maps the status word and does not return it in the result
|
2019-01-20 11:41:23 +01:00
|
|
|
buffer. However, it R_SW is not NULL the status word is stored
|
|
|
|
R_SW for closer inspection. */
|
2009-01-08 20:56:30 +01:00
|
|
|
gpg_error_t
|
2011-02-04 12:57:53 +01:00
|
|
|
iso7816_apdu_direct (int slot, const void *apdudata, size_t apdudatalen,
|
2019-01-20 11:41:23 +01:00
|
|
|
int handle_more, unsigned int *r_sw,
|
2009-01-08 20:56:30 +01:00
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
2019-01-20 11:41:23 +01:00
|
|
|
int sw, sw2;
|
2009-01-08 20:56:30 +01:00
|
|
|
|
2019-01-20 11:41:23 +01:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
}
|
2009-01-08 20:56:30 +01:00
|
|
|
|
2009-03-30 14:46:06 +02:00
|
|
|
sw = apdu_send_direct (slot, 0, apdudata, apdudatalen, handle_more,
|
2019-01-20 11:41:23 +01:00
|
|
|
&sw2, result, resultlen);
|
2009-01-08 20:56:30 +01:00
|
|
|
if (!sw)
|
|
|
|
{
|
2019-01-20 11:41:23 +01:00
|
|
|
if (!result)
|
|
|
|
sw = sw2;
|
|
|
|
else if (*resultlen < 2)
|
2009-01-08 20:56:30 +01:00
|
|
|
sw = SW_HOST_GENERAL_ERROR;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sw = ((*result)[*resultlen-2] << 8) | (*result)[*resultlen-1];
|
|
|
|
(*resultlen)--;
|
|
|
|
(*resultlen)--;
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 11:41:23 +01:00
|
|
|
if (sw != SW_SUCCESS && result)
|
2009-01-08 20:56:30 +01:00
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
}
|
2019-01-20 11:41:23 +01:00
|
|
|
if (r_sw)
|
|
|
|
*r_sw = sw;
|
2009-01-08 20:56:30 +01:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
/* Check whether the reader supports the ISO command code COMMAND on
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
the pinpad. Returns 0 on success. */
|
2005-11-28 12:52:25 +01:00
|
|
|
gpg_error_t
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
iso7816_check_pinpad (int slot, int command, pininfo_t *pininfo)
|
2005-11-28 12:52:25 +01:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
sw = apdu_check_pinpad (slot, command, pininfo);
|
2006-11-20 17:49:41 +01:00
|
|
|
return iso7816_map_sw (sw);
|
2005-11-28 12:52:25 +01:00
|
|
|
}
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Perform a VERIFY command on SLOT using the card holder verification
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
vector CHVNO. With PININFO non-NULL the pinpad of the reader will
|
2011-11-28 08:16:38 +01:00
|
|
|
be used. Returns 0 on success. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
SCD: API cleanup for keypad handling.
* scd/iso7816.h (struct pininfo_s): Rename from iso7816_pininfo_s.
Change meaning of MODE.
(pininfo_t): Rename from iso7816_pininfo_t.
* scd/sc-copykeys.c: Include "iso7816.h".
* scd/scdaemon.c, scd/command.c: Likewise.
* scd/ccid-driver.c: Include "scdaemon.h" and "iso7816.h".
(ccid_transceive_secure): Follow the change of PININFO_T.
* scd/app.c: Include "apdu.h" after "iso7816.h".
* scd/iso7816.c (iso7816_check_keypad, iso7816_verify_kp)
(iso7816_change_reference_data_kp): Follow the change of API.
* scd/apdu.c (struct reader_table_s): Change API of CHECK_KEYPAD,
KEYPAD_VERIFY, KEYPAD_MODIFY to have arg of PININFO_T.
(check_pcsc_keypad, check_ccid_keypad): Likewise.
(apdu_check_keypad, apdu_keypad_verify, apdu_keypad_modify): Likewise.
(pcsc_keypad_verify, pcsc_keypad_modify, ct_send_apdu)
(pcsc_send_apdu_direct, pcsc_send_apdu_wrapped, pcsc_send_apdu)
(send_apdu_ccid, ccid_keypad_operation, my_rapdu_send_apdu, send_apdu)
(send_le): Follow the change of API.
* scd/apdu.h (apdu_check_keypad, apdu_keypad_verify)
(apdu_keypad_modify): Change the API.
* scd/app-dinsig.c, scd/app-nks.c, scd/app-openpgp.c: Follow the
change.
2013-01-09 06:10:08 +01:00
|
|
|
iso7816_verify_kp (int slot, int chvno, pininfo_t *pininfo)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
sw = apdu_pinpad_verify (slot, 0x00, CMD_VERIFY, 0, chvno, pininfo);
|
2003-08-05 19:11:04 +02:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
/* Perform a VERIFY command on SLOT using the card holder verification
|
2015-11-16 12:41:46 +01:00
|
|
|
vector CHVNO with a CHV of length CHVLEN. Returns 0 on success. */
|
2005-11-28 12:52:25 +01:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_verify (int slot, int chvno, const char *chv, size_t chvlen)
|
|
|
|
{
|
2011-11-28 08:16:38 +01:00
|
|
|
int sw;
|
|
|
|
|
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_VERIFY, 0, chvno, chvlen, chv);
|
|
|
|
return map_sw (sw);
|
2005-11-28 12:52:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:25:41 +02:00
|
|
|
|
|
|
|
/* Some cards support a VERIFY command variant to check the status of
|
|
|
|
* the the CHV without a need to try a CHV. In contrast to the other
|
|
|
|
* functions this function returns the special codes ISO7816_VERIFY_*
|
|
|
|
* or a non-negative number with the left attempts. */
|
|
|
|
int
|
|
|
|
iso7816_verify_status (int slot, int chvno)
|
|
|
|
{
|
|
|
|
unsigned char apdu[4];
|
|
|
|
unsigned int sw;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
apdu[0] = 0x00;
|
|
|
|
apdu[1] = ISO7816_VERIFY;
|
|
|
|
apdu[2] = 0x00;
|
|
|
|
apdu[3] = chvno;
|
|
|
|
if (!iso7816_apdu_direct (slot, apdu, 4, 0, &sw, NULL, NULL))
|
|
|
|
result = ISO7816_VERIFY_NOT_NEEDED; /* Not returned by all cards. */
|
|
|
|
else if (sw == 0x6a88 || sw == 0x6a80)
|
|
|
|
result = ISO7816_VERIFY_NO_PIN;
|
|
|
|
else if (sw == 0x6983)
|
|
|
|
result = ISO7816_VERIFY_BLOCKED;
|
|
|
|
else if (sw == 0x6985)
|
|
|
|
result = ISO7816_VERIFY_NULLPIN; /* TCOS card */
|
|
|
|
else if ((sw & 0xfff0) == 0x63C0)
|
|
|
|
result = (sw & 0x000f);
|
|
|
|
else
|
|
|
|
result = ISO7816_VERIFY_ERROR;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 03:59:32 +01:00
|
|
|
/* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
verification vector CHVNO. With PININFO non-NULL the pinpad of the
|
2011-12-02 05:57:12 +01:00
|
|
|
reader will be used. If IS_EXCHANGE is 0, a "change reference
|
|
|
|
data" is done, otherwise an "exchange reference data". */
|
2011-11-29 03:59:32 +01:00
|
|
|
gpg_error_t
|
2011-12-02 05:57:12 +01:00
|
|
|
iso7816_change_reference_data_kp (int slot, int chvno, int is_exchange,
|
SCD: API cleanup for keypad handling.
* scd/iso7816.h (struct pininfo_s): Rename from iso7816_pininfo_s.
Change meaning of MODE.
(pininfo_t): Rename from iso7816_pininfo_t.
* scd/sc-copykeys.c: Include "iso7816.h".
* scd/scdaemon.c, scd/command.c: Likewise.
* scd/ccid-driver.c: Include "scdaemon.h" and "iso7816.h".
(ccid_transceive_secure): Follow the change of PININFO_T.
* scd/app.c: Include "apdu.h" after "iso7816.h".
* scd/iso7816.c (iso7816_check_keypad, iso7816_verify_kp)
(iso7816_change_reference_data_kp): Follow the change of API.
* scd/apdu.c (struct reader_table_s): Change API of CHECK_KEYPAD,
KEYPAD_VERIFY, KEYPAD_MODIFY to have arg of PININFO_T.
(check_pcsc_keypad, check_ccid_keypad): Likewise.
(apdu_check_keypad, apdu_keypad_verify, apdu_keypad_modify): Likewise.
(pcsc_keypad_verify, pcsc_keypad_modify, ct_send_apdu)
(pcsc_send_apdu_direct, pcsc_send_apdu_wrapped, pcsc_send_apdu)
(send_apdu_ccid, ccid_keypad_operation, my_rapdu_send_apdu, send_apdu)
(send_le): Follow the change of API.
* scd/apdu.h (apdu_check_keypad, apdu_keypad_verify)
(apdu_keypad_modify): Change the API.
* scd/app-dinsig.c, scd/app-nks.c, scd/app-openpgp.c: Follow the
change.
2013-01-09 06:10:08 +01:00
|
|
|
pininfo_t *pininfo)
|
2011-11-29 03:59:32 +01:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
scd: Rename 'keypad' to 'pinpad'.
* NEWS: Mention scd changes.
* agent/divert-scd.c (getpin_cb): Change message.
* agent/call-scd.c (inq_needpin): Change the protocol to
POPUPPINPADPROMPT and DISMISSPINPADPROMPT.
* scd/command.c (pin_cb): Likewise.
* scd/apdu.c (struct reader_table_s): Rename member functions.
(check_pcsc_pinpad, pcsc_pinpad_verify, pcsc_pinpad_modify,
check_ccid_pinpad, ccid_pinpad_operation, apdu_check_pinpad
apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/apdu.h (SW_HOST_NO_PINPAD, apdu_check_pinpad)
(apdu_pinpad_verify, apdu_pinpad_modify): Rename.
* scd/iso7816.h (iso7816_check_pinpad): Rename.
* scd/iso7816.c (map_sw): Use SW_HOST_NO_PINPAD.
(iso7816_check_pinpad): Rename.
(iso7816_verify_kp, iso7816_change_reference_data_kp): Follow
the change.
* scd/ccid-driver.h (CCID_DRIVER_ERR_NO_PINPAD): Rename.
* scd/ccid-driver.c (ccid_transceive_secure): Use it.
* scd/app-dinsig.c (verify_pin): Follow the change.
* scd/app-nks.c (verify_pin): Follow the change.
* scd/app-openpgp.c (check_pinpad_request): Rename.
(parse_login_data, verify_a_chv, verify_chv3, do_change_pin): Follow
the change.
* scd/scdaemon.c (oDisablePinpad, oEnablePinpadVarlen): Rename.
* scd/scdaemon.h (opt): Rename to disable_pinpad,
enable_pinpad_varlen.
* tools/gpgconf-comp.c (gc_options_scdaemon): Rename to
disable-pinpad.
2013-02-07 02:07:51 +01:00
|
|
|
sw = apdu_pinpad_modify (slot, 0x00, CMD_CHANGE_REFERENCE_DATA,
|
SCD: API cleanup for keypad handling.
* scd/iso7816.h (struct pininfo_s): Rename from iso7816_pininfo_s.
Change meaning of MODE.
(pininfo_t): Rename from iso7816_pininfo_t.
* scd/sc-copykeys.c: Include "iso7816.h".
* scd/scdaemon.c, scd/command.c: Likewise.
* scd/ccid-driver.c: Include "scdaemon.h" and "iso7816.h".
(ccid_transceive_secure): Follow the change of PININFO_T.
* scd/app.c: Include "apdu.h" after "iso7816.h".
* scd/iso7816.c (iso7816_check_keypad, iso7816_verify_kp)
(iso7816_change_reference_data_kp): Follow the change of API.
* scd/apdu.c (struct reader_table_s): Change API of CHECK_KEYPAD,
KEYPAD_VERIFY, KEYPAD_MODIFY to have arg of PININFO_T.
(check_pcsc_keypad, check_ccid_keypad): Likewise.
(apdu_check_keypad, apdu_keypad_verify, apdu_keypad_modify): Likewise.
(pcsc_keypad_verify, pcsc_keypad_modify, ct_send_apdu)
(pcsc_send_apdu_direct, pcsc_send_apdu_wrapped, pcsc_send_apdu)
(send_apdu_ccid, ccid_keypad_operation, my_rapdu_send_apdu, send_apdu)
(send_le): Follow the change of API.
* scd/apdu.h (apdu_check_keypad, apdu_keypad_verify)
(apdu_keypad_modify): Change the API.
* scd/app-dinsig.c, scd/app-nks.c, scd/app-openpgp.c: Follow the
change.
2013-01-09 06:10:08 +01:00
|
|
|
is_exchange ? 1 : 0, chvno, pininfo);
|
2011-11-29 03:59:32 +01:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Perform a CHANGE_REFERENCE_DATA command on SLOT for the card holder
|
|
|
|
verification vector CHVNO. If the OLDCHV is NULL (and OLDCHVLEN
|
|
|
|
0), a "change reference data" is done, otherwise an "exchange
|
|
|
|
reference data". The new reference data is expected in NEWCHV of
|
2011-11-29 03:59:32 +01:00
|
|
|
length NEWCHVLEN. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2011-11-29 03:59:32 +01:00
|
|
|
iso7816_change_reference_data (int slot, int chvno,
|
|
|
|
const char *oldchv, size_t oldchvlen,
|
|
|
|
const char *newchv, size_t newchvlen)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if ((!oldchv && oldchvlen)
|
|
|
|
|| (oldchv && !oldchvlen)
|
|
|
|
|| !newchv || !newchvlen )
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
buf = xtrymalloc (oldchvlen + newchvlen);
|
|
|
|
if (!buf)
|
2004-03-16 11:49:37 +01:00
|
|
|
return gpg_error (gpg_err_code_from_errno (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
if (oldchvlen)
|
|
|
|
memcpy (buf, oldchv, oldchvlen);
|
|
|
|
memcpy (buf+oldchvlen, newchv, newchvlen);
|
|
|
|
|
2011-11-29 03:59:32 +01:00
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_CHANGE_REFERENCE_DATA,
|
|
|
|
oldchvlen? 0 : 1, chvno, oldchvlen+newchvlen, buf);
|
2019-02-05 14:48:49 +01:00
|
|
|
wipememory (buf, oldchvlen+newchvlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (buf);
|
|
|
|
return map_sw (sw);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
|
2008-09-25 12:06:02 +02:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_reset_retry_counter_with_rc (int slot, int chvno,
|
|
|
|
const char *data, size_t datalen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!data || !datalen )
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_RESET_RETRY_COUNTER,
|
|
|
|
0, chvno, datalen, data);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_reset_retry_counter (int slot, int chvno,
|
|
|
|
const char *newchv, size_t newchvlen)
|
|
|
|
{
|
2011-11-29 09:56:22 +01:00
|
|
|
int sw;
|
|
|
|
|
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_RESET_RETRY_COUNTER,
|
|
|
|
2, chvno, newchvlen, newchv);
|
|
|
|
return map_sw (sw);
|
2005-11-28 12:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-13 00:09:23 +02:00
|
|
|
/* Perform a SELECT DATA command to OCCURRENCE of TAG. */
|
2021-03-19 18:26:03 +01:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_select_data (int slot, int occurrence, int tag)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
int datalen;
|
|
|
|
unsigned char data[7];
|
|
|
|
|
|
|
|
data[0] = 0x60;
|
|
|
|
data[2] = 0x5c;
|
|
|
|
if (tag <= 0xff)
|
|
|
|
{
|
|
|
|
data[3] = 1;
|
|
|
|
data[4] = tag;
|
|
|
|
datalen = 5;
|
|
|
|
}
|
|
|
|
else if (tag <= 0xffff)
|
|
|
|
{
|
|
|
|
data[3] = 2;
|
|
|
|
data[4] = (tag >> 8);
|
|
|
|
data[5] = tag;
|
|
|
|
datalen = 6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data[3] = 3;
|
|
|
|
data[4] = (tag >> 16);
|
|
|
|
data[5] = (tag >> 8);
|
|
|
|
data[6] = tag;
|
|
|
|
datalen = 7;
|
|
|
|
}
|
|
|
|
data[1] = datalen - 2;
|
|
|
|
|
|
|
|
sw = apdu_send_le (slot, 0, 0x00, CMD_SELECT_DATA,
|
|
|
|
occurrence, 0x04, datalen, data, 0, NULL, NULL);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2005-11-28 12:52:25 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Perform a GET DATA command requesting TAG and storing the result in
|
|
|
|
a newly allocated buffer at the address passed by RESULT. Return
|
|
|
|
the length of this data at the address of RESULTLEN. */
|
|
|
|
gpg_error_t
|
2009-06-17 11:45:50 +02:00
|
|
|
iso7816_get_data (int slot, int extended_mode, int tag,
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
2009-06-17 11:45:50 +02:00
|
|
|
int le;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
if (!result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-06-17 11:45:50 +02:00
|
|
|
if (extended_mode > 0 && extended_mode < 256)
|
|
|
|
le = 65534; /* Not 65535 in case it is used as some special flag. */
|
|
|
|
else if (extended_mode > 0)
|
|
|
|
le = extended_mode;
|
|
|
|
else
|
|
|
|
le = 256;
|
|
|
|
|
|
|
|
sw = apdu_send_le (slot, extended_mode, 0x00, CMD_GET_DATA,
|
|
|
|
((tag >> 8) & 0xff), (tag & 0xff), -1, NULL, le,
|
|
|
|
result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-03 15:18:15 +01:00
|
|
|
/* Perform a GET DATA command requesting TAG and storing the result in
|
|
|
|
* a newly allocated buffer at the address passed by RESULT. Return
|
|
|
|
* the length of this data at the address of RESULTLEN. This variant
|
|
|
|
* is needed for long (3 octet) tags. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_get_data_odd (int slot, int extended_mode, unsigned int tag,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
int le;
|
|
|
|
int datalen;
|
|
|
|
unsigned char data[5];
|
|
|
|
|
|
|
|
if (!result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
if (extended_mode > 0 && extended_mode < 256)
|
|
|
|
le = 65534; /* Not 65535 in case it is used as some special flag. */
|
|
|
|
else if (extended_mode > 0)
|
|
|
|
le = extended_mode;
|
|
|
|
else
|
|
|
|
le = 256;
|
|
|
|
|
|
|
|
data[0] = 0x5c;
|
|
|
|
if (tag <= 0xff)
|
|
|
|
{
|
|
|
|
data[1] = 1;
|
|
|
|
data[2] = tag;
|
|
|
|
datalen = 3;
|
|
|
|
}
|
|
|
|
else if (tag <= 0xffff)
|
|
|
|
{
|
|
|
|
data[1] = 2;
|
|
|
|
data[2] = (tag >> 8);
|
|
|
|
data[3] = tag;
|
|
|
|
datalen = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data[1] = 3;
|
|
|
|
data[2] = (tag >> 16);
|
|
|
|
data[3] = (tag >> 8);
|
|
|
|
data[4] = tag;
|
|
|
|
datalen = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw = apdu_send_le (slot, extended_mode, 0x00, CMD_GET_DATA + 1,
|
|
|
|
0x3f, 0xff, datalen, data, le,
|
|
|
|
result, resultlen);
|
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/* Perform a PUT DATA command on card in SLOT. Write DATA of length
|
2008-09-23 11:57:45 +02:00
|
|
|
DATALEN to TAG. EXTENDED_MODE controls whether extended length
|
|
|
|
headers or command chaining is used instead of single length
|
|
|
|
bytes. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2008-09-23 11:57:45 +02:00
|
|
|
iso7816_put_data (int slot, int extended_mode, int tag,
|
2009-09-03 12:57:23 +02:00
|
|
|
const void *data, size_t datalen)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
2008-09-23 11:57:45 +02:00
|
|
|
sw = apdu_send_simple (slot, extended_mode, 0x00, CMD_PUT_DATA,
|
2003-08-05 19:11:04 +02:00
|
|
|
((tag >> 8) & 0xff), (tag & 0xff),
|
2005-06-16 10:12:03 +02:00
|
|
|
datalen, (const char*)data);
|
2003-08-05 19:11:04 +02:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2009-03-30 14:46:06 +02:00
|
|
|
/* Same as iso7816_put_data but uses an odd instruction byte. */
|
2008-09-25 12:06:02 +02:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_put_data_odd (int slot, int extended_mode, int tag,
|
2009-09-03 12:57:23 +02:00
|
|
|
const void *data, size_t datalen)
|
2008-09-25 12:06:02 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
sw = apdu_send_simple (slot, extended_mode, 0x00, CMD_PUT_DATA+1,
|
|
|
|
((tag >> 8) & 0xff), (tag & 0xff),
|
|
|
|
datalen, (const char*)data);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2004-01-28 17:21:57 +01:00
|
|
|
/* Manage Security Environment. This is a weird operation and there
|
|
|
|
is no easy abstraction for it. Furthermore, some card seem to have
|
2018-10-24 21:56:18 +02:00
|
|
|
a different interpretation of 7816-8 and thus we resort to let the
|
2004-01-28 17:21:57 +01:00
|
|
|
caller decide what to do. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_manage_security_env (int slot, int p1, int p2,
|
|
|
|
const unsigned char *data, size_t datalen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
2005-04-27 21:47:53 +02:00
|
|
|
if (p1 < 0 || p1 > 255 || p2 < 0 || p2 > 255 )
|
2004-01-28 17:21:57 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
sw = apdu_send_simple (slot, 0, 0x00, CMD_MSE, p1, p2,
|
2005-06-16 10:12:03 +02:00
|
|
|
data? datalen : -1, (const char*)data);
|
2004-01-28 17:21:57 +01:00
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2024-05-13 00:09:23 +02:00
|
|
|
/* Perform the security operation COMPUTE DIGITAL SIGNATURE. On
|
2018-10-24 21:56:18 +02:00
|
|
|
success 0 is returned and the data is available in a newly
|
2003-08-05 19:11:04 +02:00
|
|
|
allocated buffer stored at RESULT with its length stored at
|
2009-07-10 12:15:33 +02:00
|
|
|
RESULTLEN. For LE see do_generate_keypair. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2009-07-10 12:15:33 +02:00
|
|
|
iso7816_compute_ds (int slot, int extended_mode,
|
|
|
|
const unsigned char *data, size_t datalen, int le,
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-07-10 12:15:33 +02:00
|
|
|
if (!extended_mode)
|
|
|
|
le = 256; /* Ignore provided Le and use what apdu_send uses. */
|
|
|
|
else if (le >= 0 && le < 256)
|
|
|
|
le = 256;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
2009-07-10 12:15:33 +02:00
|
|
|
0x00, CMD_PSO, 0x9E, 0x9A,
|
|
|
|
datalen, (const char*)data,
|
|
|
|
le,
|
|
|
|
result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-28 17:21:57 +01:00
|
|
|
/* Perform the security operation DECIPHER. PADIND is the padding
|
|
|
|
indicator to be used. It should be 0 if no padding is required, a
|
|
|
|
value of -1 suppresses the padding byte. On success 0 is returned
|
|
|
|
and the plaintext is available in a newly allocated buffer stored
|
2009-09-03 12:57:23 +02:00
|
|
|
at RESULT with its length stored at RESULTLEN. For LE see
|
|
|
|
do_generate_keypair. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2011-02-04 12:57:53 +01:00
|
|
|
iso7816_decipher (int slot, int extended_mode,
|
2009-09-03 12:57:23 +02:00
|
|
|
const unsigned char *data, size_t datalen, int le,
|
2004-01-28 17:21:57 +01:00
|
|
|
int padind, unsigned char **result, size_t *resultlen)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-09-03 12:57:23 +02:00
|
|
|
if (!extended_mode)
|
|
|
|
le = 256; /* Ignore provided Le and use what apdu_send uses. */
|
|
|
|
else if (le >= 0 && le < 256)
|
|
|
|
le = 256;
|
|
|
|
|
2004-01-28 17:21:57 +01:00
|
|
|
if (padind >= 0)
|
|
|
|
{
|
|
|
|
/* We need to prepend the padding indicator. */
|
|
|
|
buf = xtrymalloc (datalen + 1);
|
|
|
|
if (!buf)
|
2004-03-16 11:49:37 +01:00
|
|
|
return gpg_error (gpg_err_code_from_errno (errno));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-01-28 17:21:57 +01:00
|
|
|
*buf = padind; /* Padding indicator. */
|
|
|
|
memcpy (buf+1, data, datalen);
|
2011-02-04 12:57:53 +01:00
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
2009-09-03 12:57:23 +02:00
|
|
|
0x00, CMD_PSO, 0x80, 0x86,
|
|
|
|
datalen+1, (char*)buf, le,
|
|
|
|
result, resultlen);
|
2004-01-28 17:21:57 +01:00
|
|
|
xfree (buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-03 12:57:23 +02:00
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
|
|
|
0x00, CMD_PSO, 0x80, 0x86,
|
|
|
|
datalen, (const char *)data, le,
|
|
|
|
result, resultlen);
|
2004-01-28 17:21:57 +01:00
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-29 14:48:11 +02:00
|
|
|
/* Perform the security operation COMPUTE SHARED SECRET. On success 0
|
|
|
|
is returned and the shared secret is available in a newly allocated
|
|
|
|
buffer stored at RESULT with its length stored at RESULTLEN. For
|
|
|
|
LE see do_generate_keypair. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_pso_csv (int slot, int extended_mode,
|
|
|
|
const unsigned char *data, size_t datalen, int le,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
unsigned char *buf;
|
2021-04-22 11:04:30 +02:00
|
|
|
unsigned int nbuf;
|
2021-03-29 14:48:11 +02:00
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
if (!extended_mode)
|
|
|
|
le = 256; /* Ignore provided Le and use what apdu_send uses. */
|
|
|
|
else if (le >= 0 && le < 256)
|
|
|
|
le = 256;
|
|
|
|
|
2021-04-22 11:04:30 +02:00
|
|
|
/* Data needs to be in BER-TLV format. */
|
|
|
|
buf = xtrymalloc (datalen + 4);
|
2021-03-29 14:48:11 +02:00
|
|
|
if (!buf)
|
|
|
|
return gpg_error_from_syserror ();
|
2021-04-22 11:04:30 +02:00
|
|
|
nbuf = 0;
|
|
|
|
buf[nbuf++] = 0x9c;
|
|
|
|
if (datalen < 128)
|
|
|
|
buf[nbuf++] = datalen;
|
|
|
|
else if (datalen < 256)
|
|
|
|
{
|
|
|
|
buf[nbuf++] = 0x81;
|
|
|
|
buf[nbuf++] = datalen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[nbuf++] = 0x82;
|
|
|
|
buf[nbuf++] = datalen << 8;
|
|
|
|
buf[nbuf++] = datalen;
|
|
|
|
}
|
|
|
|
memcpy (buf+nbuf, data, datalen);
|
2021-03-29 14:48:11 +02:00
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
|
|
|
0x00, CMD_PSO, 0x80, 0xa6,
|
2021-04-22 11:04:30 +02:00
|
|
|
datalen+nbuf, (const char *)buf, le,
|
2021-03-29 14:48:11 +02:00
|
|
|
result, resultlen);
|
|
|
|
xfree (buf);
|
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-20 11:41:23 +01:00
|
|
|
/* For LE see do_generate_keypair. */
|
2003-08-05 19:11:04 +02:00
|
|
|
gpg_error_t
|
2009-07-10 12:15:33 +02:00
|
|
|
iso7816_internal_authenticate (int slot, int extended_mode,
|
2003-08-05 19:11:04 +02:00
|
|
|
const unsigned char *data, size_t datalen,
|
2009-07-10 12:15:33 +02:00
|
|
|
int le,
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-07-10 12:15:33 +02:00
|
|
|
if (!extended_mode)
|
|
|
|
le = 256; /* Ignore provided Le and use what apdu_send uses. */
|
|
|
|
else if (le >= 0 && le < 256)
|
|
|
|
le = 256;
|
|
|
|
|
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
|
|
|
0x00, CMD_INTERNAL_AUTHENTICATE, 0, 0,
|
|
|
|
datalen, (const char*)data,
|
|
|
|
le,
|
|
|
|
result, resultlen);
|
2019-01-20 11:41:23 +01:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* For LE see do_generate_keypair. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_general_authenticate (int slot, int extended_mode,
|
|
|
|
int algoref, int keyref,
|
|
|
|
const unsigned char *data, size_t datalen,
|
|
|
|
int le,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
if (!extended_mode)
|
|
|
|
le = 256; /* Ignore provided Le and use what apdu_send uses. */
|
|
|
|
else if (le >= 0 && le < 256)
|
|
|
|
le = 256;
|
|
|
|
|
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
|
|
|
0x00, CMD_GENERAL_AUTHENTICATE, algoref, keyref,
|
|
|
|
datalen, (const char*)data,
|
|
|
|
le,
|
|
|
|
result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 17:07:45 +02:00
|
|
|
/* LE is the expected return length. This is usually 0 except if
|
|
|
|
extended length mode is used and more than 256 byte will be
|
|
|
|
returned. In that case a value of -1 uses a large default
|
|
|
|
(e.g. 4096 bytes), a value larger 256 used that value. */
|
2003-08-05 19:11:04 +02:00
|
|
|
static gpg_error_t
|
2019-02-06 14:07:42 +01:00
|
|
|
do_generate_keypair (int slot, int extended_mode, int p1, int p2,
|
2016-10-18 13:40:09 +02:00
|
|
|
const char *data, size_t datalen, int le,
|
2009-05-08 17:07:45 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (!data || !datalen || !result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
2009-05-08 17:07:45 +02:00
|
|
|
sw = apdu_send_le (slot, extended_mode,
|
2019-02-06 14:07:42 +01:00
|
|
|
0x00, CMD_GENERATE_KEYPAIR, p1, p2,
|
2016-10-18 13:40:09 +02:00
|
|
|
datalen, data,
|
2009-05-08 17:07:45 +02:00
|
|
|
le >= 0 && le < 256? 256:le,
|
|
|
|
result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gpg_error_t
|
2019-02-06 14:07:42 +01:00
|
|
|
iso7816_generate_keypair (int slot, int extended_mode, int p1, int p2,
|
2016-10-18 13:40:09 +02:00
|
|
|
const char *data, size_t datalen,
|
2011-02-04 12:57:53 +01:00
|
|
|
int le,
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
2019-02-06 14:07:42 +01:00
|
|
|
return do_generate_keypair (slot, extended_mode, p1, p2,
|
2009-05-08 17:07:45 +02:00
|
|
|
data, datalen, le, result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gpg_error_t
|
2009-05-08 17:07:45 +02:00
|
|
|
iso7816_read_public_key (int slot, int extended_mode,
|
2016-10-18 13:40:09 +02:00
|
|
|
const char *data, size_t datalen,
|
2011-02-04 12:57:53 +01:00
|
|
|
int le,
|
2009-05-08 17:07:45 +02:00
|
|
|
unsigned char **result, size_t *resultlen)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2019-02-06 14:07:42 +01:00
|
|
|
return do_generate_keypair (slot, extended_mode, 0x81, 0,
|
2009-05-08 17:07:45 +02:00
|
|
|
data, datalen, le, result, resultlen);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_get_challenge (int slot, int length, unsigned char *buffer)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
unsigned char *result;
|
|
|
|
size_t resultlen, n;
|
|
|
|
|
|
|
|
if (!buffer || length < 1)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = NULL;
|
|
|
|
n = length > 254? 254 : length;
|
2011-02-04 12:57:53 +01:00
|
|
|
sw = apdu_send_le (slot, 0,
|
2009-03-30 14:46:06 +02:00
|
|
|
0x00, CMD_GET_CHALLENGE, 0, 0, -1, NULL, n,
|
2003-08-05 19:11:04 +02:00
|
|
|
&result, &resultlen);
|
|
|
|
if (sw != SW_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (result);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
if (resultlen > n)
|
|
|
|
resultlen = n;
|
|
|
|
memcpy (buffer, result, resultlen);
|
|
|
|
buffer += resultlen;
|
|
|
|
length -= resultlen;
|
|
|
|
xfree (result);
|
|
|
|
}
|
|
|
|
while (length > 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-27 17:40:42 +01:00
|
|
|
|
|
|
|
/* Perform a READ BINARY command requesting a maximum of NMAX bytes
|
2020-11-25 15:27:21 +01:00
|
|
|
* from OFFSET. With NMAX = 0 the entire file is read. The result is
|
|
|
|
* stored in a newly allocated buffer at the address passed by RESULT.
|
|
|
|
* Returns the length of this data at the address of RESULTLEN. If
|
|
|
|
* R_SW is not NULL the last status word is stored there. */
|
2004-01-27 17:40:42 +01:00
|
|
|
gpg_error_t
|
2020-03-31 11:53:36 +02:00
|
|
|
iso7816_read_binary_ext (int slot, int extended_mode,
|
|
|
|
size_t offset, size_t nmax,
|
2020-11-25 15:27:21 +01:00
|
|
|
unsigned char **result, size_t *resultlen,
|
|
|
|
int *r_sw)
|
2004-01-27 17:40:42 +01:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
unsigned char *buffer;
|
|
|
|
size_t bufferlen;
|
2004-03-16 19:59:21 +01:00
|
|
|
int read_all = !nmax;
|
|
|
|
size_t n;
|
2004-01-27 17:40:42 +01:00
|
|
|
|
2020-11-25 15:27:21 +01:00
|
|
|
if (r_sw)
|
|
|
|
*r_sw = 0;
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
if (!result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
/* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
|
|
|
|
we check for this limit. */
|
2004-03-16 19:59:21 +01:00
|
|
|
if (offset > 32767)
|
2004-01-27 17:40:42 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
buffer = NULL;
|
|
|
|
bufferlen = 0;
|
2009-01-08 20:56:30 +01:00
|
|
|
n = read_all? 0 : nmax;
|
2020-03-31 11:53:36 +02:00
|
|
|
sw = apdu_send_le (slot, extended_mode, 0x00, CMD_READ_BINARY,
|
2004-03-16 19:59:21 +01:00
|
|
|
((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
|
|
|
|
n, &buffer, &bufferlen);
|
2005-09-05 16:36:36 +02:00
|
|
|
if ( SW_EXACT_LENGTH_P(sw) )
|
|
|
|
{
|
|
|
|
n = (sw & 0x00ff);
|
2020-03-31 11:53:36 +02:00
|
|
|
sw = apdu_send_le (slot, extended_mode, 0x00, CMD_READ_BINARY,
|
2005-09-05 16:36:36 +02:00
|
|
|
((offset>>8) & 0xff), (offset & 0xff) , -1, NULL,
|
|
|
|
n, &buffer, &bufferlen);
|
|
|
|
}
|
2020-11-25 15:27:21 +01:00
|
|
|
if (r_sw)
|
|
|
|
*r_sw = sw;
|
2005-09-05 16:36:36 +02:00
|
|
|
|
2021-06-16 20:29:47 +02:00
|
|
|
if (*result && (sw == SW_BAD_P0_P1 || sw == SW_INCORRECT_P0_P1))
|
2005-09-05 16:36:36 +02:00
|
|
|
{
|
|
|
|
/* Bad Parameter means that the offset is outside of the
|
|
|
|
EF. When reading all data we take this as an indication
|
|
|
|
for EOF. */
|
|
|
|
break;
|
|
|
|
}
|
2004-01-27 17:40:42 +01:00
|
|
|
|
|
|
|
if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (buffer);
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
if (*result) /* Need to extend the buffer. */
|
|
|
|
{
|
|
|
|
unsigned char *p = xtryrealloc (*result, *resultlen + bufferlen);
|
|
|
|
if (!p)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
gpg_error_t err = gpg_error_from_syserror ();
|
2004-01-27 17:40:42 +01:00
|
|
|
xfree (buffer);
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
*result = p;
|
|
|
|
memcpy (*result + *resultlen, buffer, bufferlen);
|
|
|
|
*resultlen += bufferlen;
|
|
|
|
xfree (buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
else /* Transfer the buffer into our result. */
|
|
|
|
{
|
|
|
|
*result = buffer;
|
|
|
|
*resultlen = bufferlen;
|
|
|
|
}
|
|
|
|
offset += bufferlen;
|
|
|
|
if (offset > 32767)
|
|
|
|
break; /* We simply truncate the result for too large
|
|
|
|
files. */
|
2004-03-16 19:59:21 +01:00
|
|
|
if (nmax > bufferlen)
|
|
|
|
nmax -= bufferlen;
|
|
|
|
else
|
|
|
|
nmax = 0;
|
2004-01-27 17:40:42 +01:00
|
|
|
}
|
2004-03-16 19:59:21 +01:00
|
|
|
while ((read_all && sw != SW_EOF_REACHED) || (!read_all && nmax));
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:53:36 +02:00
|
|
|
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_read_binary (int slot, size_t offset, size_t nmax,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
2020-11-25 15:27:21 +01:00
|
|
|
return iso7816_read_binary_ext (slot, 0, offset, nmax,
|
|
|
|
result, resultlen, NULL);
|
2020-03-31 11:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
/* Perform a READ RECORD command. RECNO gives the record number to
|
|
|
|
read with 0 indicating the current record. RECCOUNT must be 1 (not
|
2004-03-16 11:49:37 +01:00
|
|
|
all cards support reading of more than one record). SHORT_EF
|
|
|
|
should be 0 to read the current EF or contain a short EF. The
|
|
|
|
result is stored in a newly allocated buffer at the address passed
|
|
|
|
by RESULT. Returns the length of this data at the address of
|
2020-11-25 15:27:21 +01:00
|
|
|
RESULTLEN. If R_SW is not NULL the last status word is stored
|
|
|
|
there. */
|
2004-01-27 17:40:42 +01:00
|
|
|
gpg_error_t
|
2020-11-25 15:27:21 +01:00
|
|
|
iso7816_read_record_ext (int slot, int recno, int reccount, int short_ef,
|
|
|
|
unsigned char **result, size_t *resultlen,
|
|
|
|
int *r_sw)
|
2004-01-27 17:40:42 +01:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
unsigned char *buffer;
|
|
|
|
size_t bufferlen;
|
|
|
|
|
2020-11-25 15:27:21 +01:00
|
|
|
if (r_sw)
|
|
|
|
*r_sw = 0;
|
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
if (!result || !resultlen)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
|
|
|
|
/* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
|
|
|
|
we check for this limit. */
|
2004-03-16 11:49:37 +01:00
|
|
|
if (recno < 0 || recno > 255 || reccount != 1
|
|
|
|
|| short_ef < 0 || short_ef > 254 )
|
2004-01-27 17:40:42 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
buffer = NULL;
|
|
|
|
bufferlen = 0;
|
2009-03-30 14:46:06 +02:00
|
|
|
sw = apdu_send_le (slot, 0, 0x00, CMD_READ_RECORD,
|
2011-02-04 12:57:53 +01:00
|
|
|
recno,
|
2004-03-16 11:49:37 +01:00
|
|
|
short_ef? short_ef : 0x04,
|
2004-01-27 17:40:42 +01:00
|
|
|
-1, NULL,
|
2009-01-08 20:56:30 +01:00
|
|
|
0, &buffer, &bufferlen);
|
2020-11-25 15:27:21 +01:00
|
|
|
if (r_sw)
|
|
|
|
*r_sw = sw;
|
2004-01-27 17:40:42 +01:00
|
|
|
|
|
|
|
if (sw != SW_SUCCESS && sw != SW_EOF_REACHED)
|
|
|
|
{
|
|
|
|
/* Make sure that pending buffers are released. */
|
|
|
|
xfree (buffer);
|
|
|
|
xfree (*result);
|
|
|
|
*result = NULL;
|
|
|
|
*resultlen = 0;
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|
|
|
|
*result = buffer;
|
|
|
|
*resultlen = bufferlen;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-01-27 17:40:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-02 18:35:34 +02:00
|
|
|
|
|
|
|
|
2020-11-25 15:27:21 +01:00
|
|
|
gpg_error_t
|
|
|
|
iso7816_read_record (int slot, int recno, int reccount, int short_ef,
|
|
|
|
unsigned char **result, size_t *resultlen)
|
|
|
|
{
|
|
|
|
return iso7816_read_record_ext (slot, recno, reccount, short_ef,
|
|
|
|
result, resultlen, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-02 18:35:34 +02:00
|
|
|
/* Perform an UPDATE BINARY command on card in SLOT. Write DATA of
|
|
|
|
* length DATALEN to a transparent file at OFFSET. */
|
|
|
|
gpg_error_t
|
|
|
|
iso7816_update_binary (int slot, int extended_mode, size_t offset,
|
|
|
|
const void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
/* We can only encode 15 bits in p0,p1 to indicate an offset. Thus
|
|
|
|
* we check for this limit. */
|
|
|
|
if (offset > 32767)
|
|
|
|
return gpg_error (GPG_ERR_INV_VALUE);
|
|
|
|
|
|
|
|
sw = apdu_send_simple (slot, extended_mode, 0x00, CMD_UPDATE_BINARY,
|
|
|
|
((offset>>8) & 0xff), (offset & 0xff),
|
|
|
|
datalen, (const char*)data);
|
|
|
|
return map_sw (sw);
|
|
|
|
}
|