2001-12-22 20:43:09 +00:00
|
|
|
|
/*
|
2002-01-08 13:56:50 +00:00
|
|
|
|
* iso7816.c: Functions specified by the ISO 7816 standard
|
2001-12-22 20:43:09 +00:00
|
|
|
|
*
|
2002-04-05 14:46:44 +00:00
|
|
|
|
* Copyright (C) 2001, 2002 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
2001-12-22 20:43:09 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2002-04-05 10:44:51 +00:00
|
|
|
|
#include "internal.h"
|
|
|
|
|
#include "asn1.h"
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <ctype.h>
|
2002-03-21 09:36:38 +00:00
|
|
|
|
#include <string.h>
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
2005-08-02 21:13:06 +00:00
|
|
|
|
static const struct sc_card_error iso7816_errors[] = {
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6200, SC_ERROR_MEMORY_FAILURE, "State of non-volatile memory unchanged" },
|
|
|
|
|
{ 0x6281, SC_ERROR_MEMORY_FAILURE, "Part of returned data may be corrupted" },
|
|
|
|
|
{ 0x6282, SC_ERROR_CARD_CMD_FAILED, "End of file/record reached before reading Le bytes" },
|
|
|
|
|
{ 0x6283, SC_ERROR_CARD_CMD_FAILED, "Selected file invalidated" },
|
|
|
|
|
{ 0x6284, SC_ERROR_CARD_CMD_FAILED, "FCI not formatted according to ISO 7816-4" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6300, SC_ERROR_MEMORY_FAILURE, "State of non-volatile memory changed" },
|
|
|
|
|
{ 0x6381, SC_ERROR_CARD_CMD_FAILED, "File filled up by last write" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6581, SC_ERROR_MEMORY_FAILURE, "Memory failure" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
|
|
|
|
|
{ 0x6700, SC_ERROR_WRONG_LENGTH, "Wrong length" },
|
|
|
|
|
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6800, SC_ERROR_NO_CARD_SUPPORT, "Functions in CLA not supported" },
|
|
|
|
|
{ 0x6881, SC_ERROR_NO_CARD_SUPPORT, "Logical channel not supported" },
|
|
|
|
|
{ 0x6882, SC_ERROR_NO_CARD_SUPPORT, "Secure messaging not supported" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6900, SC_ERROR_NOT_ALLOWED, "Command not allowed" },
|
|
|
|
|
{ 0x6981, SC_ERROR_CARD_CMD_FAILED, "Command incompatible with file structure" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
{ 0x6982, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED, "Security status not satisfied" },
|
2002-03-12 13:00:57 +00:00
|
|
|
|
{ 0x6983, SC_ERROR_AUTH_METHOD_BLOCKED, "Authentication method blocked" },
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6984, SC_ERROR_CARD_CMD_FAILED, "Referenced data invalidated" },
|
|
|
|
|
{ 0x6985, SC_ERROR_NOT_ALLOWED, "Conditions of use not satisfied" },
|
|
|
|
|
{ 0x6986, SC_ERROR_NOT_ALLOWED, "Command not allowed (no current EF)" },
|
|
|
|
|
{ 0x6987, SC_ERROR_INCORRECT_PARAMETERS,"Expected SM data objects missing" },
|
|
|
|
|
{ 0x6988, SC_ERROR_INCORRECT_PARAMETERS,"SM data objects incorrect" },
|
|
|
|
|
|
|
|
|
|
{ 0x6A00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" },
|
|
|
|
|
{ 0x6A80, SC_ERROR_INCORRECT_PARAMETERS,"Incorrect parameters in the data field" },
|
2002-04-10 23:00:38 +00:00
|
|
|
|
{ 0x6A81, SC_ERROR_NO_CARD_SUPPORT, "Function not supported" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
{ 0x6A82, SC_ERROR_FILE_NOT_FOUND, "File not found" },
|
|
|
|
|
{ 0x6A83, SC_ERROR_RECORD_NOT_FOUND, "Record not found" },
|
2002-04-06 12:02:28 +00:00
|
|
|
|
{ 0x6A84, SC_ERROR_CARD_CMD_FAILED, "Not enough memory space in the file" },
|
|
|
|
|
{ 0x6A85, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with TLV structure" },
|
|
|
|
|
{ 0x6A86, SC_ERROR_INCORRECT_PARAMETERS,"Incorrect parameters P1-P2" },
|
|
|
|
|
{ 0x6A87, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with P1-P2" },
|
2003-11-20 15:38:43 +00:00
|
|
|
|
{ 0x6A88, SC_ERROR_DATA_OBJECT_NOT_FOUND,"Referenced data not found" },
|
2002-04-06 12:02:28 +00:00
|
|
|
|
|
|
|
|
|
{ 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" },
|
|
|
|
|
{ 0x6D00, SC_ERROR_INS_NOT_SUPPORTED, "Instruction code not supported or invalid" },
|
2002-02-15 23:17:58 +00:00
|
|
|
|
{ 0x6E00, SC_ERROR_CLASS_NOT_SUPPORTED, "Class not supported" },
|
2002-08-21 10:16:42 +00:00
|
|
|
|
{ 0x6F00, SC_ERROR_CARD_CMD_FAILED, "No precise diagnosis" },
|
2002-08-21 10:06:59 +00:00
|
|
|
|
|
|
|
|
|
/* Possibly TCOS / Micardo specific errors */
|
|
|
|
|
{ 0x6600, SC_ERROR_INCORRECT_PARAMETERS, "Error setting the security env"},
|
|
|
|
|
{ 0x66F0, SC_ERROR_INCORRECT_PARAMETERS, "No space left for padding"},
|
|
|
|
|
{ 0x69F0, SC_ERROR_NOT_ALLOWED, "Command not allowed"},
|
|
|
|
|
{ 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "Files exists"},
|
|
|
|
|
{ 0x6A8A, SC_ERROR_FILE_ALREADY_EXISTS, "Application exists"},
|
2002-02-15 23:17:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2005-08-05 17:18:10 +00:00
|
|
|
|
static int iso7816_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2)
|
2002-02-15 23:17:58 +00:00
|
|
|
|
{
|
|
|
|
|
const int err_count = sizeof(iso7816_errors)/sizeof(iso7816_errors[0]);
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Handle special cases here */
|
|
|
|
|
if (sw1 == 0x6C) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Wrong length; correct length is %d\n", sw2);
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return SC_ERROR_WRONG_LENGTH;
|
|
|
|
|
}
|
|
|
|
|
if (sw1 == 0x90)
|
|
|
|
|
return SC_NO_ERROR;
|
2005-08-05 17:18:10 +00:00
|
|
|
|
if (sw1 == 0x63U && (sw2 & ~0x0fU) == 0xc0U ) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Verification failed (remaining tries: %d)\n",
|
2002-08-21 10:06:59 +00:00
|
|
|
|
(sw2 & 0x0f));
|
|
|
|
|
return SC_ERROR_PIN_CODE_INCORRECT;
|
|
|
|
|
}
|
2002-02-15 23:17:58 +00:00
|
|
|
|
for (i = 0; i < err_count; i++)
|
|
|
|
|
if (iso7816_errors[i].SWs == ((sw1 << 8) | sw2)) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "%s\n", iso7816_errors[i].errorstr);
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return iso7816_errors[i].errorno;
|
|
|
|
|
}
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Unknown SWs; SW1=%02X, SW2=%02X\n", sw1, sw2);
|
2002-04-06 12:02:28 +00:00
|
|
|
|
return SC_ERROR_CARD_CMD_FAILED;
|
2002-02-15 23:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_read_binary(sc_card_t *card,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
unsigned int idx, u8 *buf, size_t count,
|
|
|
|
|
unsigned long flags)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
u8 recvbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
int r;
|
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
assert(count <= card->max_recv_size);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xB0,
|
|
|
|
|
(idx >> 8) & 0x7F, idx & 0xFF);
|
|
|
|
|
apdu.le = count;
|
|
|
|
|
apdu.resplen = count;
|
|
|
|
|
apdu.resp = recvbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.resplen == 0)
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
2001-12-22 20:43:09 +00:00
|
|
|
|
memcpy(buf, recvbuf, apdu.resplen);
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, apdu.resplen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_read_record(sc_card_t *card,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
unsigned int rec_nr, u8 *buf, size_t count,
|
|
|
|
|
unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
u8 recvbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xB2, rec_nr, 0);
|
2002-02-21 19:23:21 +00:00
|
|
|
|
apdu.p2 = (flags & SC_RECORD_EF_ID_MASK) << 3;
|
|
|
|
|
if (flags & SC_RECORD_BY_REC_NR)
|
2001-12-29 02:07:32 +00:00
|
|
|
|
apdu.p2 |= 0x04;
|
|
|
|
|
|
|
|
|
|
apdu.le = count;
|
|
|
|
|
apdu.resplen = count;
|
|
|
|
|
apdu.resp = recvbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.resplen == 0)
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
2001-12-29 02:07:32 +00:00
|
|
|
|
memcpy(buf, recvbuf, apdu.resplen);
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, apdu.resplen);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_write_record(sc_card_t *card, unsigned int rec_nr,
|
2002-02-21 19:23:21 +00:00
|
|
|
|
const u8 *buf, size_t count,
|
|
|
|
|
unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-02-21 19:23:21 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
if (count > 256) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Trying to send too many bytes\n");
|
2002-02-21 19:23:21 +00:00
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD2, rec_nr, 0);
|
|
|
|
|
apdu.p2 = (flags & SC_RECORD_EF_ID_MASK) << 3;
|
|
|
|
|
if (flags & SC_RECORD_BY_REC_NR)
|
|
|
|
|
apdu.p2 |= 0x04;
|
|
|
|
|
|
|
|
|
|
apdu.lc = count;
|
|
|
|
|
apdu.datalen = count;
|
|
|
|
|
apdu.data = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
SC_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
|
|
|
|
|
"Card returned error");
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, count);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_append_record(sc_card_t *card,
|
2002-02-21 19:23:21 +00:00
|
|
|
|
const u8 *buf, size_t count,
|
|
|
|
|
unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-02-21 19:23:21 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
if (count > 256) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Trying to send too many bytes\n");
|
2002-02-21 19:23:21 +00:00
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE2, 0, 0);
|
|
|
|
|
apdu.p2 = (flags & SC_RECORD_EF_ID_MASK) << 3;
|
|
|
|
|
|
|
|
|
|
apdu.lc = count;
|
|
|
|
|
apdu.datalen = count;
|
|
|
|
|
apdu.data = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
SC_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
|
|
|
|
|
"Card returned error");
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, count);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_update_record(sc_card_t *card, unsigned int rec_nr,
|
2002-02-21 19:23:21 +00:00
|
|
|
|
const u8 *buf, size_t count,
|
|
|
|
|
unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-02-21 19:23:21 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
if (count > 256) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "Trying to send too many bytes\n");
|
2002-02-21 19:23:21 +00:00
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xDC, rec_nr, 0);
|
|
|
|
|
apdu.p2 = (flags & SC_RECORD_EF_ID_MASK) << 3;
|
|
|
|
|
if (flags & SC_RECORD_BY_REC_NR)
|
|
|
|
|
apdu.p2 |= 0x04;
|
|
|
|
|
|
|
|
|
|
apdu.lc = count;
|
|
|
|
|
apdu.datalen = count;
|
|
|
|
|
apdu.data = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
SC_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
|
|
|
|
|
"Card returned error");
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, count);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_write_binary(sc_card_t *card,
|
2002-01-08 13:56:50 +00:00
|
|
|
|
unsigned int idx, const u8 *buf,
|
|
|
|
|
size_t count, unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
assert(count <= card->max_send_size);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD0,
|
|
|
|
|
(idx >> 8) & 0x7F, idx & 0xFF);
|
|
|
|
|
apdu.lc = count;
|
|
|
|
|
apdu.datalen = count;
|
|
|
|
|
apdu.data = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
|
2002-01-08 13:56:50 +00:00
|
|
|
|
"Card returned error");
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, count);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_update_binary(sc_card_t *card,
|
2002-01-13 23:56:13 +00:00
|
|
|
|
unsigned int idx, const u8 *buf,
|
|
|
|
|
size_t count, unsigned long flags)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-13 23:56:13 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
2003-12-18 16:35:28 +00:00
|
|
|
|
assert(count <= card->max_send_size);
|
2002-01-13 23:56:13 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6,
|
|
|
|
|
(idx >> 8) & 0x7F, idx & 0xFF);
|
|
|
|
|
apdu.lc = count;
|
|
|
|
|
apdu.datalen = count;
|
|
|
|
|
apdu.data = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, sc_check_sw(card, apdu.sw1, apdu.sw2),
|
2002-01-13 23:56:13 +00:00
|
|
|
|
"Card returned error");
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, count);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_process_fci(sc_card_t *card, sc_file_t *file,
|
2002-01-08 13:56:50 +00:00
|
|
|
|
const u8 *buf, size_t buflen)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_context_t *ctx = card->ctx;
|
2001-12-29 19:03:46 +00:00
|
|
|
|
size_t taglen, len = buflen;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
const u8 *tag = NULL, *p = buf;
|
|
|
|
|
|
|
|
|
|
if (ctx->debug >= 3)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, "processing FCI bytes\n");
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x83, &taglen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (tag != NULL && taglen == 2) {
|
|
|
|
|
file->id = (tag[0] << 8) | tag[1];
|
|
|
|
|
if (ctx->debug >= 3)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, " file identifier: 0x%02X%02X\n", tag[0],
|
2001-12-22 20:43:09 +00:00
|
|
|
|
tag[1]);
|
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x81, &taglen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (tag != NULL && taglen >= 2) {
|
|
|
|
|
int bytes = (tag[0] << 8) + tag[1];
|
|
|
|
|
if (ctx->debug >= 3)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, " bytes in file: %d\n", bytes);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
file->size = bytes;
|
|
|
|
|
}
|
2002-02-26 11:23:25 +00:00
|
|
|
|
if (tag == NULL) {
|
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x80, &taglen);
|
|
|
|
|
if (tag != NULL && taglen >= 2) {
|
|
|
|
|
int bytes = (tag[0] << 8) + tag[1];
|
|
|
|
|
if (ctx->debug >= 3)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, " bytes in file: %d\n", bytes);
|
2002-02-26 11:23:25 +00:00
|
|
|
|
file->size = bytes;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (tag != NULL) {
|
|
|
|
|
if (taglen > 0) {
|
|
|
|
|
unsigned char byte = tag[0];
|
|
|
|
|
const char *type;
|
|
|
|
|
|
|
|
|
|
file->shareable = byte & 0x40 ? 1 : 0;
|
|
|
|
|
if (ctx->debug >= 3)
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, " shareable: %s\n",
|
2001-12-22 20:43:09 +00:00
|
|
|
|
(byte & 0x40) ? "yes" : "no");
|
|
|
|
|
file->ef_structure = byte & 0x07;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
switch ((byte >> 3) & 7) {
|
|
|
|
|
case 0:
|
|
|
|
|
type = "working EF";
|
|
|
|
|
file->type = SC_FILE_TYPE_WORKING_EF;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
type = "internal EF";
|
|
|
|
|
file->type = SC_FILE_TYPE_INTERNAL_EF;
|
|
|
|
|
break;
|
|
|
|
|
case 7:
|
|
|
|
|
type = "DF";
|
|
|
|
|
file->type = SC_FILE_TYPE_DF;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
type = "unknown";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (ctx->debug >= 3) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_debug(ctx, " type: %s\n", type);
|
|
|
|
|
sc_debug(ctx, " EF structure: %d\n",
|
2001-12-22 20:43:09 +00:00
|
|
|
|
byte & 0x07);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x84, &taglen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (tag != NULL && taglen > 0 && taglen <= 16) {
|
|
|
|
|
char name[17];
|
2003-05-30 08:54:42 +00:00
|
|
|
|
size_t i;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
memcpy(file->name, tag, taglen);
|
|
|
|
|
file->namelen = taglen;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < taglen; i++) {
|
|
|
|
|
if (isalnum(tag[i]) || ispunct(tag[i])
|
|
|
|
|
|| isspace(tag[i]))
|
|
|
|
|
name[i] = tag[i];
|
|
|
|
|
else
|
|
|
|
|
name[i] = '?';
|
|
|
|
|
}
|
|
|
|
|
name[taglen] = 0;
|
|
|
|
|
if (ctx->debug >= 3)
|
2005-03-26 19:47:13 +00:00
|
|
|
|
sc_debug(ctx, " File name: %s\n", name);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x85, &taglen);
|
2002-03-28 14:13:36 +00:00
|
|
|
|
if (tag != NULL && taglen) {
|
|
|
|
|
sc_file_set_prop_attr(file, tag, taglen);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
} else
|
|
|
|
|
file->prop_attr_len = 0;
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0xA5, &taglen);
|
2002-03-28 14:13:36 +00:00
|
|
|
|
if (tag != NULL && taglen) {
|
|
|
|
|
sc_file_set_prop_attr(file, tag, taglen);
|
2001-12-29 12:39:41 +00:00
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
tag = sc_asn1_find_tag(ctx, p, len, 0x86, &taglen);
|
2002-03-28 14:13:36 +00:00
|
|
|
|
if (tag != NULL && taglen) {
|
|
|
|
|
sc_file_set_sec_attr(file, tag, taglen);
|
2002-02-11 15:55:34 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
file->magic = SC_FILE_MAGIC;
|
2003-07-14 17:39:19 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_select_file(sc_card_t *card,
|
|
|
|
|
const sc_path_t *in_path,
|
|
|
|
|
sc_file_t **file_out)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_context_t *ctx;
|
|
|
|
|
sc_apdu_t apdu;
|
2001-12-30 21:17:34 +00:00
|
|
|
|
u8 buf[SC_MAX_APDU_BUFFER_SIZE];
|
2001-12-22 20:43:09 +00:00
|
|
|
|
u8 pathbuf[SC_MAX_PATH_SIZE], *path = pathbuf;
|
|
|
|
|
int r, pathlen;
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_file_t *file = NULL;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
assert(card != NULL && in_path != NULL);
|
|
|
|
|
ctx = card->ctx;
|
|
|
|
|
memcpy(path, in_path->value, in_path->len);
|
|
|
|
|
pathlen = in_path->len;
|
|
|
|
|
|
2002-02-11 15:55:34 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0xA4, 0, 0);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
switch (in_path->type) {
|
|
|
|
|
case SC_PATH_TYPE_FILE_ID:
|
|
|
|
|
apdu.p1 = 0;
|
|
|
|
|
if (pathlen != 2)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
break;
|
|
|
|
|
case SC_PATH_TYPE_DF_NAME:
|
|
|
|
|
apdu.p1 = 4;
|
|
|
|
|
break;
|
|
|
|
|
case SC_PATH_TYPE_PATH:
|
|
|
|
|
apdu.p1 = 8;
|
|
|
|
|
if (pathlen >= 2 && memcmp(path, "\x3F\x00", 2) == 0) {
|
|
|
|
|
if (pathlen == 2) { /* only 3F00 supplied */
|
|
|
|
|
apdu.p1 = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
path += 2;
|
|
|
|
|
pathlen -= 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2002-02-11 15:55:34 +00:00
|
|
|
|
apdu.p2 = 0; /* first record, return FCI */
|
2001-12-22 20:43:09 +00:00
|
|
|
|
apdu.lc = pathlen;
|
|
|
|
|
apdu.data = path;
|
|
|
|
|
apdu.datalen = pathlen;
|
|
|
|
|
|
2002-02-20 09:56:47 +00:00
|
|
|
|
if (file_out != NULL) {
|
2002-02-11 15:55:34 +00:00
|
|
|
|
apdu.resp = buf;
|
|
|
|
|
apdu.resplen = sizeof(buf);
|
|
|
|
|
apdu.le = 256;
|
|
|
|
|
} else {
|
2001-12-29 02:07:32 +00:00
|
|
|
|
apdu.resplen = 0;
|
2002-02-11 15:55:34 +00:00
|
|
|
|
apdu.le = 0;
|
|
|
|
|
apdu.cse = SC_APDU_CASE_3_SHORT;
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-20 09:56:47 +00:00
|
|
|
|
if (file_out == NULL) {
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (apdu.sw1 == 0x61)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, 0);
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-02-15 23:17:58 +00:00
|
|
|
|
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (r)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
|
|
|
|
|
|
|
|
|
switch (apdu.resp[0]) {
|
|
|
|
|
case 0x6F:
|
2002-02-20 09:56:47 +00:00
|
|
|
|
file = sc_file_new();
|
|
|
|
|
if (file == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 0, SC_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
file->path = *in_path;
|
2003-07-14 17:39:19 +00:00
|
|
|
|
if (card->ops->process_fci == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2002-02-20 09:56:47 +00:00
|
|
|
|
if (apdu.resp[1] <= apdu.resplen)
|
2003-07-14 17:39:19 +00:00
|
|
|
|
card->ops->process_fci(card, file, apdu.resp+2, apdu.resp[1]);
|
2002-02-20 09:56:47 +00:00
|
|
|
|
*file_out = file;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0x00: /* proprietary coding */
|
2002-04-06 12:02:28 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_UNKNOWN_DATA_RECEIVED);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
default:
|
2002-04-06 12:02:28 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_UNKNOWN_DATA_RECEIVED);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
u8 buf[10];
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT,
|
|
|
|
|
0x84, 0x00, 0x00);
|
|
|
|
|
apdu.le = 8;
|
|
|
|
|
apdu.resp = buf;
|
|
|
|
|
apdu.resplen = 8; /* include SW's */
|
|
|
|
|
|
|
|
|
|
while (len > 0) {
|
2005-08-08 10:22:14 +00:00
|
|
|
|
size_t n = len > 8 ? 8 : len;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.resplen != 8)
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
memcpy(rnd, apdu.resp, n);
|
|
|
|
|
len -= n;
|
|
|
|
|
rnd += n;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_construct_fci(sc_card_t *card, const sc_file_t *file,
|
2003-07-28 13:19:49 +00:00
|
|
|
|
u8 *out, size_t *outlen)
|
2002-01-08 13:56:50 +00:00
|
|
|
|
{
|
|
|
|
|
u8 *p = out;
|
2002-01-10 23:02:48 +00:00
|
|
|
|
u8 buf[64];
|
2002-01-08 13:56:50 +00:00
|
|
|
|
|
|
|
|
|
*p++ = 0x6F;
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
buf[0] = (file->size >> 8) & 0xFF;
|
|
|
|
|
buf[1] = file->size & 0xFF;
|
|
|
|
|
sc_asn1_put_tag(0x81, buf, 2, p, 16, &p);
|
2002-04-17 13:36:35 +00:00
|
|
|
|
|
|
|
|
|
if (file->type_attr_len) {
|
|
|
|
|
memcpy(buf, file->type_attr, file->type_attr_len);
|
|
|
|
|
sc_asn1_put_tag(0x82, buf, file->type_attr_len, p, 16, &p);
|
|
|
|
|
} else {
|
|
|
|
|
buf[0] = file->shareable ? 0x40 : 0;
|
|
|
|
|
switch (file->type) {
|
|
|
|
|
case SC_FILE_TYPE_INTERNAL_EF:
|
|
|
|
|
buf[0] |= 0x08;
|
2005-07-11 21:28:55 +00:00
|
|
|
|
case SC_FILE_TYPE_WORKING_EF:
|
|
|
|
|
buf[0] |= file->ef_structure & 7;
|
2002-04-17 13:36:35 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_FILE_TYPE_DF:
|
|
|
|
|
buf[0] |= 0x38;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
sc_asn1_put_tag(0x82, buf, 1, p, 16, &p);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
buf[0] = (file->id >> 8) & 0xFF;
|
|
|
|
|
buf[1] = file->id & 0xFF;
|
|
|
|
|
sc_asn1_put_tag(0x83, buf, 2, p, 16, &p);
|
|
|
|
|
/* 0x84 = DF name */
|
|
|
|
|
if (file->prop_attr_len) {
|
|
|
|
|
memcpy(buf, file->prop_attr, file->prop_attr_len);
|
|
|
|
|
sc_asn1_put_tag(0x85, buf, file->prop_attr_len, p, 18, &p);
|
|
|
|
|
}
|
|
|
|
|
if (file->sec_attr_len) {
|
|
|
|
|
memcpy(buf, file->sec_attr, file->sec_attr_len);
|
|
|
|
|
sc_asn1_put_tag(0x86, buf, file->sec_attr_len, p, 18, &p);
|
|
|
|
|
}
|
|
|
|
|
out[1] = p - out - 2;
|
|
|
|
|
|
|
|
|
|
*outlen = p - out;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_create_file(sc_card_t *card, sc_file_t *file)
|
2002-01-08 13:56:50 +00:00
|
|
|
|
{
|
2002-01-10 13:49:54 +00:00
|
|
|
|
int r;
|
|
|
|
|
size_t len;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
|
|
|
|
|
len = SC_MAX_APDU_BUFFER_SIZE;
|
2003-07-23 14:31:21 +00:00
|
|
|
|
|
|
|
|
|
if (card->ops->construct_fci == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2003-07-28 13:19:49 +00:00
|
|
|
|
r = card->ops->construct_fci(card, file, sbuf, &len);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "construct_fci() failed");
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
|
|
|
|
|
apdu.lc = len;
|
|
|
|
|
apdu.datalen = len;
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_get_response(sc_card_t *card, sc_apdu_t *orig_apdu, size_t count)
|
2003-07-10 11:38:02 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2003-07-10 11:38:02 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xC0, 0x00, 0x00);
|
|
|
|
|
apdu.le = count;
|
|
|
|
|
apdu.resplen = count;
|
|
|
|
|
apdu.resp = orig_apdu->resp;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.resplen == 0)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
|
|
|
|
|
|
|
|
|
if (apdu.resplen != count) {
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_WRONG_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orig_apdu->resplen = apdu.resplen;
|
|
|
|
|
orig_apdu->sw1 = 0x90;
|
|
|
|
|
orig_apdu->sw2 = 0x00;
|
|
|
|
|
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 3, apdu.resplen);
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_delete_file(sc_card_t *card, const sc_path_t *path)
|
2002-01-08 13:56:50 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
u8 sbuf[2];
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 1);
|
2005-03-26 19:47:13 +00:00
|
|
|
|
if (path->type != SC_PATH_TYPE_FILE_ID || (path->len != 0 && path->len != 2)) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx, "File type has to be SC_PATH_TYPE_FILE_ID\n");
|
2002-01-08 13:56:50 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
2005-03-26 19:47:13 +00:00
|
|
|
|
|
|
|
|
|
if (path->len == 2) {
|
|
|
|
|
sbuf[0] = path->value[0];
|
|
|
|
|
sbuf[1] = path->value[1];
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x00, 0x00);
|
|
|
|
|
apdu.lc = 2;
|
|
|
|
|
apdu.datalen = 2;
|
|
|
|
|
}
|
|
|
|
|
else /* No file ID given: means currently selected file */
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0xE4, 0x00, 0x00);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_set_security_env(sc_card_t *card,
|
|
|
|
|
const sc_security_env_t *env,
|
2002-01-16 23:59:18 +00:00
|
|
|
|
int se_num)
|
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
u8 *p;
|
|
|
|
|
int r, locked = 0;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL && env != NULL);
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0, 0);
|
|
|
|
|
switch (env->operation) {
|
|
|
|
|
case SC_SEC_OPERATION_DECIPHER:
|
2002-06-03 15:05:58 +00:00
|
|
|
|
apdu.p1 = 0x81;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
apdu.p2 = 0xB8;
|
|
|
|
|
break;
|
|
|
|
|
case SC_SEC_OPERATION_SIGN:
|
2002-06-03 15:05:58 +00:00
|
|
|
|
apdu.p1 = 0x41;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
apdu.p2 = 0xB6;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
|
|
|
|
apdu.le = 0;
|
|
|
|
|
p = sbuf;
|
|
|
|
|
if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
|
|
|
|
|
*p++ = 0x80; /* algorithm reference */
|
2002-01-20 21:20:09 +00:00
|
|
|
|
*p++ = 0x01;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
*p++ = env->algorithm_ref & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
if (env->flags & SC_SEC_ENV_FILE_REF_PRESENT) {
|
|
|
|
|
*p++ = 0x81;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
*p++ = env->file_ref.len;
|
|
|
|
|
memcpy(p, env->file_ref.value, env->file_ref.len);
|
|
|
|
|
p += env->file_ref.len;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
if (env->flags & SC_SEC_ENV_KEY_REF_PRESENT) {
|
2002-01-20 21:20:09 +00:00
|
|
|
|
if (env->flags & SC_SEC_ENV_KEY_REF_ASYMMETRIC)
|
|
|
|
|
*p++ = 0x83;
|
|
|
|
|
else
|
|
|
|
|
*p++ = 0x84;
|
|
|
|
|
*p++ = env->key_ref_len;
|
|
|
|
|
memcpy(p, env->key_ref, env->key_ref_len);
|
|
|
|
|
p += env->key_ref_len;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
r = p - sbuf;
|
|
|
|
|
apdu.lc = r;
|
|
|
|
|
apdu.datalen = r;
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.resplen = 0;
|
|
|
|
|
if (se_num > 0) {
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
|
|
|
|
locked = 1;
|
|
|
|
|
}
|
|
|
|
|
if (apdu.datalen != 0) {
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
if (r) {
|
|
|
|
|
sc_perror(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
2002-02-15 23:17:58 +00:00
|
|
|
|
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2002-01-16 23:59:18 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
sc_perror(card->ctx, r, "Card returned error");
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (se_num <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0x22, 0xF2, se_num);
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2002-01-16 23:59:18 +00:00
|
|
|
|
err:
|
|
|
|
|
if (locked)
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_restore_security_env(sc_card_t *card, int se_num)
|
2002-01-16 23:59:18 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-01-16 23:59:18 +00:00
|
|
|
|
int r;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2002-01-16 23:59:18 +00:00
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x22, 0xF3, se_num);
|
|
|
|
|
apdu.resplen = sizeof(rbuf) > 250 ? 250 : sizeof(rbuf);
|
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-02-15 23:17:58 +00:00
|
|
|
|
return sc_check_sw(card, apdu.sw1, apdu.sw2);
|
2002-01-16 23:59:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_compute_signature(sc_card_t *card,
|
2002-01-20 21:20:09 +00:00
|
|
|
|
const u8 * data, size_t datalen,
|
|
|
|
|
u8 * out, size_t outlen)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-02-24 19:32:14 +00:00
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
|
|
|
|
assert(card != NULL && data != NULL && out != NULL);
|
|
|
|
|
if (datalen > 255)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
|
|
|
|
|
/* INS: 0x2A PERFORM SECURITY OPERATION
|
|
|
|
|
* P1: 0x9E Resp: Digital Signature
|
|
|
|
|
* P2: 0x9A Cmd: Input for Digital Signature */
|
2002-06-13 11:18:31 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x9E,
|
2002-01-20 21:20:09 +00:00
|
|
|
|
0x9A);
|
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
apdu.resplen = sizeof(rbuf); /* FIXME */
|
2002-06-14 12:29:19 +00:00
|
|
|
|
apdu.le = 256;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
|
|
|
|
memcpy(sbuf, data, datalen);
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.lc = datalen;
|
|
|
|
|
apdu.datalen = datalen;
|
2002-04-06 12:02:28 +00:00
|
|
|
|
apdu.sensitive = 1;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
|
2005-08-08 10:22:14 +00:00
|
|
|
|
size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
|
|
|
|
memcpy(out, apdu.resp, len);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 4, len);
|
|
|
|
|
}
|
2002-02-15 23:17:58 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 4, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
2002-01-20 21:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_decipher(sc_card_t *card,
|
2002-04-06 12:02:28 +00:00
|
|
|
|
const u8 * crgram, size_t crgram_len,
|
|
|
|
|
u8 * out, size_t outlen)
|
2002-03-15 09:42:07 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t apdu;
|
2002-03-15 09:42:07 +00:00
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
|
|
|
|
|
assert(card != NULL && crgram != NULL && out != NULL);
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
|
if (crgram_len > 255)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
|
|
|
|
|
/* INS: 0x2A PERFORM SECURITY OPERATION
|
|
|
|
|
* P1: 0x80 Resp: Plain value
|
|
|
|
|
* P2: 0x86 Cmd: Padding indicator byte followed by cryptogram */
|
2002-06-13 11:18:31 +00:00
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x86);
|
2002-03-15 09:42:07 +00:00
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
apdu.resplen = sizeof(rbuf); /* FIXME */
|
2005-01-13 20:28:57 +00:00
|
|
|
|
apdu.le = 256; /* set le to 256 == 0x00 == everything available */
|
2002-04-06 12:02:28 +00:00
|
|
|
|
apdu.sensitive = 1;
|
|
|
|
|
|
2002-03-15 09:42:07 +00:00
|
|
|
|
sbuf[0] = 0; /* padding indicator byte, 0x00 = No further indication */
|
|
|
|
|
memcpy(sbuf + 1, crgram, crgram_len);
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.lc = crgram_len + 1;
|
|
|
|
|
apdu.datalen = crgram_len + 1;
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
|
2005-08-08 10:22:14 +00:00
|
|
|
|
size_t len = apdu.resplen > outlen ? outlen : apdu.resplen;
|
2002-03-15 09:42:07 +00:00
|
|
|
|
|
|
|
|
|
memcpy(out, apdu.resp, len);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, len);
|
|
|
|
|
}
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, sc_check_sw(card, apdu.sw1, apdu.sw2));
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_build_pin_apdu(sc_card_t *card,
|
|
|
|
|
sc_apdu_t *apdu,
|
2002-12-23 18:47:27 +00:00
|
|
|
|
struct sc_pin_cmd_data *data)
|
2002-02-10 18:04:03 +00:00
|
|
|
|
{
|
2002-12-23 18:47:27 +00:00
|
|
|
|
static u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2003-10-21 08:59:11 +00:00
|
|
|
|
int r, len = 0, pad = 0, use_pin_pad = 0, ins, p1 = 0;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
switch (data->pin_type) {
|
2002-02-20 09:56:47 +00:00
|
|
|
|
case SC_AC_CHV:
|
2002-02-10 18:04:03 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
2002-12-23 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
if (data->flags & SC_PIN_CMD_NEED_PADDING)
|
|
|
|
|
pad = 1;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
if (data->flags & SC_PIN_CMD_USE_PINPAD)
|
|
|
|
|
use_pin_pad = 1;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
|
2003-10-21 08:59:11 +00:00
|
|
|
|
data->pin1.offset = 5;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
switch (data->cmd) {
|
|
|
|
|
case SC_PIN_CMD_VERIFY:
|
|
|
|
|
ins = 0x20;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
if ((r = sc_build_pin(sbuf, sizeof(sbuf), &data->pin1, pad)) < 0)
|
|
|
|
|
return r;
|
|
|
|
|
len = r;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_PIN_CMD_CHANGE:
|
|
|
|
|
ins = 0x24;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
if (data->pin1.len != 0 || use_pin_pad) {
|
|
|
|
|
if ((r = sc_build_pin(sbuf, sizeof(sbuf), &data->pin1, pad)) < 0)
|
|
|
|
|
return r;
|
|
|
|
|
len += r;
|
|
|
|
|
} else {
|
|
|
|
|
/* implicit test */
|
2002-12-23 18:47:27 +00:00
|
|
|
|
p1 = 1;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data->pin2.offset = data->pin1.offset + len;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
if ((r = sc_build_pin(sbuf+len, sizeof(sbuf)-len, &data->pin2, pad)) < 0)
|
|
|
|
|
return r;
|
|
|
|
|
len += r;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_PIN_CMD_UNBLOCK:
|
2002-12-23 18:47:27 +00:00
|
|
|
|
ins = 0x2C;
|
2003-10-21 08:59:11 +00:00
|
|
|
|
if (data->pin1.len != 0 || use_pin_pad) {
|
|
|
|
|
if ((r = sc_build_pin(sbuf, sizeof(sbuf), &data->pin1, pad)) < 0)
|
|
|
|
|
return r;
|
|
|
|
|
len += r;
|
|
|
|
|
} else {
|
|
|
|
|
p1 |= 0x02;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data->pin2.len != 0 || use_pin_pad) {
|
|
|
|
|
data->pin2.offset = data->pin1.offset + len;
|
|
|
|
|
if ((r = sc_build_pin(sbuf+len, sizeof(sbuf)-len, &data->pin2, pad)) < 0)
|
|
|
|
|
return r;
|
|
|
|
|
len += r;
|
|
|
|
|
} else {
|
|
|
|
|
p1 |= 0x01;
|
|
|
|
|
}
|
2002-12-23 18:47:27 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
2002-02-10 18:04:03 +00:00
|
|
|
|
}
|
2002-12-23 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, apdu, SC_APDU_CASE_3_SHORT,
|
|
|
|
|
ins, p1, data->pin_reference);
|
|
|
|
|
|
|
|
|
|
apdu->lc = len;
|
|
|
|
|
apdu->datalen = len;
|
|
|
|
|
apdu->data = sbuf;
|
|
|
|
|
apdu->resplen = 0;
|
|
|
|
|
apdu->sensitive = 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
2002-02-10 18:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data,
|
2002-12-23 18:47:27 +00:00
|
|
|
|
int *tries_left)
|
2002-02-10 18:04:03 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_apdu_t local_apdu, *apdu;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
int r;
|
2002-02-10 18:04:03 +00:00
|
|
|
|
|
2002-12-23 18:47:27 +00:00
|
|
|
|
if (tries_left)
|
|
|
|
|
*tries_left = -1;
|
|
|
|
|
|
|
|
|
|
/* See if we've been called from another card driver, which is
|
|
|
|
|
* passing an APDU to us (this allows to write card drivers
|
|
|
|
|
* whose PIN functions behave "mostly like ISO" except in some
|
|
|
|
|
* special circumstances.
|
|
|
|
|
*/
|
|
|
|
|
if (data->apdu == NULL) {
|
|
|
|
|
r = iso7816_build_pin_apdu(card, &local_apdu, data);
|
|
|
|
|
if (r < 0)
|
|
|
|
|
return r;
|
|
|
|
|
data->apdu = &local_apdu;
|
2002-02-10 18:04:03 +00:00
|
|
|
|
}
|
2002-12-23 18:47:27 +00:00
|
|
|
|
apdu = data->apdu;
|
|
|
|
|
|
|
|
|
|
if (!(data->flags & SC_PIN_CMD_USE_PINPAD)) {
|
|
|
|
|
/* Transmit the APDU to the card */
|
|
|
|
|
r = sc_transmit_apdu(card, apdu);
|
|
|
|
|
|
|
|
|
|
/* Clear the buffer - it may contain pins */
|
|
|
|
|
memset((void *) apdu->data, 0, apdu->datalen);
|
2002-02-10 18:04:03 +00:00
|
|
|
|
} else {
|
2002-12-23 18:47:27 +00:00
|
|
|
|
/* Call the reader driver to collect
|
|
|
|
|
* the PIN and pass on the APDU to the card */
|
2003-06-23 12:56:36 +00:00
|
|
|
|
if (data->pin1.offset == 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx,
|
2003-06-23 12:56:36 +00:00
|
|
|
|
"Card driver didn't set PIN offset");
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
2002-12-23 18:47:27 +00:00
|
|
|
|
if (card->reader
|
|
|
|
|
&& card->reader->ops
|
2003-06-23 12:56:36 +00:00
|
|
|
|
&& card->reader->ops->perform_verify) {
|
|
|
|
|
r = card->reader->ops->perform_verify(card->reader,
|
2002-12-23 18:47:27 +00:00
|
|
|
|
card->slot,
|
|
|
|
|
data);
|
2003-06-23 12:56:36 +00:00
|
|
|
|
/* sw1/sw2 filled in by reader driver */
|
2002-12-23 18:47:27 +00:00
|
|
|
|
} else {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(card->ctx,
|
2002-12-23 18:47:27 +00:00
|
|
|
|
"Card reader driver does not support "
|
|
|
|
|
"PIN entry through reader key pad");
|
|
|
|
|
r = SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2002-02-10 18:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-12-23 18:47:27 +00:00
|
|
|
|
/* Don't pass references to local variables up to the caller. */
|
|
|
|
|
if (data->apdu == &local_apdu)
|
|
|
|
|
data->apdu = NULL;
|
|
|
|
|
|
2002-02-10 18:04:03 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
2002-12-23 18:47:27 +00:00
|
|
|
|
if (apdu->sw1 == 0x63) {
|
|
|
|
|
if ((apdu->sw2 & 0xF0) == 0xC0 && tries_left != NULL)
|
|
|
|
|
*tries_left = apdu->sw2 & 0x0F;
|
|
|
|
|
return SC_ERROR_PIN_CODE_INCORRECT;
|
|
|
|
|
}
|
|
|
|
|
return sc_check_sw(card, apdu->sw1, apdu->sw2);
|
2002-02-10 18:04:03 +00:00
|
|
|
|
}
|
2002-01-20 21:20:09 +00:00
|
|
|
|
|
2003-06-15 12:56:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* For some cards, selecting the MF clears all access rights gained
|
|
|
|
|
*/
|
2005-03-08 20:59:35 +00:00
|
|
|
|
static int iso7816_logout(sc_card_t *card)
|
2003-05-20 08:30:46 +00:00
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_path_t in_path;
|
2003-05-20 08:30:46 +00:00
|
|
|
|
in_path.value[0] = 0x3F;
|
|
|
|
|
in_path.value[1] = 0x00;
|
|
|
|
|
in_path.len = 2;
|
|
|
|
|
in_path.index = 0;
|
|
|
|
|
in_path.count = 2;
|
|
|
|
|
in_path.type = SC_PATH_TYPE_PATH;
|
|
|
|
|
|
2003-06-15 12:56:07 +00:00
|
|
|
|
/* Force the SELECT FILE even if the card thinks
|
|
|
|
|
* it's already inside the MF */
|
|
|
|
|
card->cache_valid = 0;
|
|
|
|
|
|
2003-06-10 06:31:48 +00:00
|
|
|
|
return sc_select_file(card, &in_path, NULL);
|
2003-05-20 08:30:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-20 13:39:18 +00:00
|
|
|
|
static int no_match(sc_card_t *card)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
static struct sc_card_operations iso_ops = {
|
2005-08-20 13:39:18 +00:00
|
|
|
|
no_match,
|
|
|
|
|
NULL, /* init */
|
|
|
|
|
NULL, /* finish */
|
|
|
|
|
iso7816_read_binary,
|
|
|
|
|
iso7816_write_binary,
|
|
|
|
|
iso7816_update_binary,
|
|
|
|
|
NULL, /* erase_binary */
|
|
|
|
|
iso7816_read_record,
|
|
|
|
|
iso7816_write_record,
|
|
|
|
|
iso7816_append_record,
|
|
|
|
|
iso7816_update_record,
|
|
|
|
|
iso7816_select_file,
|
|
|
|
|
iso7816_get_response,
|
|
|
|
|
iso7816_get_challenge,
|
|
|
|
|
NULL, /* verify */
|
|
|
|
|
iso7816_logout,
|
|
|
|
|
iso7816_restore_security_env,
|
|
|
|
|
iso7816_set_security_env,
|
|
|
|
|
iso7816_decipher,
|
|
|
|
|
iso7816_compute_signature,
|
|
|
|
|
NULL, /* change_reference_data */
|
|
|
|
|
NULL, /* reset_retry_counter */
|
|
|
|
|
iso7816_create_file,
|
|
|
|
|
iso7816_delete_file,
|
|
|
|
|
NULL, /* list_files */
|
|
|
|
|
iso7816_check_sw,
|
|
|
|
|
NULL, /* card_ctl */
|
|
|
|
|
iso7816_process_fci,
|
|
|
|
|
iso7816_construct_fci,
|
|
|
|
|
iso7816_pin_cmd,
|
|
|
|
|
NULL, /* get_data */
|
|
|
|
|
NULL, /* put_data */
|
|
|
|
|
NULL /* delete_record */
|
2001-12-22 20:43:09 +00:00
|
|
|
|
};
|
2002-01-08 13:56:50 +00:00
|
|
|
|
|
2003-02-20 12:51:07 +00:00
|
|
|
|
static struct sc_card_driver iso_driver = {
|
2002-01-08 13:56:50 +00:00
|
|
|
|
"ISO 7816 reference driver",
|
|
|
|
|
"iso7816",
|
2005-08-20 13:39:18 +00:00
|
|
|
|
&iso_ops,
|
|
|
|
|
NULL, 0, NULL
|
2001-12-22 20:43:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
2003-02-20 12:51:07 +00:00
|
|
|
|
struct sc_card_driver * sc_get_iso7816_driver(void)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
return &iso_driver;
|
|
|
|
|
}
|