2001-11-06 18:34:19 +00:00
|
|
|
|
/*
|
|
|
|
|
* sc.c: General SmartCard functions
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
*
|
|
|
|
|
* 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,
|
2001-11-01 15:43:20 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* 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
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2001-11-27 21:11:32 +00:00
|
|
|
|
#include "config.h"
|
2001-11-24 13:32:52 +00:00
|
|
|
|
#include "opensc.h"
|
2001-12-13 21:19:11 +00:00
|
|
|
|
#include "sc-log.h"
|
2001-11-06 18:34:19 +00:00
|
|
|
|
#include "sc-asn1.h"
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2001-11-27 21:11:32 +00:00
|
|
|
|
#ifdef VERSION
|
|
|
|
|
const char *sc_version = VERSION;
|
2001-11-26 20:14:48 +00:00
|
|
|
|
#else
|
|
|
|
|
#warning FIXME: version info
|
|
|
|
|
const char *sc_version = "(undef)";
|
|
|
|
|
#endif
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
|
int sc_sw_to_errorcode(struct sc_card *card, int sw1, int sw2)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2001-11-20 22:21:58 +00:00
|
|
|
|
switch (sw1) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
case 0x69:
|
2001-11-20 22:21:58 +00:00
|
|
|
|
switch (sw2) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
case 0x82:
|
|
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
default:
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
case 0x6A:
|
2001-11-20 22:21:58 +00:00
|
|
|
|
switch (sw2) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
case 0x81:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
case 0x82:
|
|
|
|
|
case 0x83:
|
|
|
|
|
return SC_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
case 0x86:
|
|
|
|
|
case 0x87:
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
2001-11-18 01:52:32 +00:00
|
|
|
|
default:
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-11-07 14:36:57 +00:00
|
|
|
|
case 0x6D:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
case 0x6E:
|
|
|
|
|
return SC_ERROR_UNKNOWN_SMARTCARD;
|
2001-11-17 00:11:29 +00:00
|
|
|
|
case 0x90:
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (sw2 == 0)
|
2001-11-17 00:11:29 +00:00
|
|
|
|
return 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-12-13 21:19:11 +00:00
|
|
|
|
error(card->ctx, "Unknown SW's: SW1=%02X, SW2=%02X\n", sw1, sw2);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
return SC_ERROR_UNKNOWN_REPLY;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-22 15:40:51 +00:00
|
|
|
|
void sc_print_binary(FILE *f, const u8 *buf, int count)
|
2001-11-07 14:36:57 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2001-11-07 13:45:41 +00:00
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
unsigned char c = buf[i];
|
2001-11-07 13:45:41 +00:00
|
|
|
|
const char *format;
|
2001-11-07 14:36:57 +00:00
|
|
|
|
if (!isalnum(c) && !ispunct(c) && !isspace(c))
|
|
|
|
|
format = "\\x%02X";
|
|
|
|
|
else
|
|
|
|
|
format = "%c";
|
2001-11-22 15:40:51 +00:00
|
|
|
|
fprintf(f, format, c);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-11-22 15:40:51 +00:00
|
|
|
|
fflush(f);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int sc_hex_to_bin(const char *in, u8 *out, int *outlen)
|
|
|
|
|
{
|
|
|
|
|
int c = 0, err = 0, left;
|
|
|
|
|
|
|
|
|
|
assert(in != NULL && out != NULL && outlen != NULL);
|
|
|
|
|
left = *outlen;
|
|
|
|
|
|
|
|
|
|
while (*in) {
|
|
|
|
|
int byte;
|
|
|
|
|
|
|
|
|
|
if (sscanf(in, "%02X", &byte) != 1) {
|
|
|
|
|
err = SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
in += 2;
|
|
|
|
|
if (*in == ':')
|
|
|
|
|
in++;
|
|
|
|
|
if (left <= 0) {
|
|
|
|
|
err = SC_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*out++ = byte;
|
|
|
|
|
left--;
|
|
|
|
|
c++;
|
|
|
|
|
}
|
|
|
|
|
*outlen = c;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-16 18:46:32 +00:00
|
|
|
|
int sc_check_apdu(struct sc_context *ctx, const struct sc_apdu *apdu)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
|
|
|
|
switch (apdu->cse) {
|
|
|
|
|
case SC_APDU_CASE_1:
|
|
|
|
|
if (apdu->datalen)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_SHORT:
|
|
|
|
|
if (apdu->datalen)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (apdu->resplen < apdu->le)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_4_SHORT:
|
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (apdu->resplen < apdu->le)
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
case SC_APDU_CASE_3_EXT:
|
|
|
|
|
case SC_APDU_CASE_4_EXT:
|
2001-12-16 18:46:32 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sc_transceive_t0(struct sc_card *card, struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
SCARD_IO_REQUEST sSendPci, sRecvPci;
|
2001-12-15 01:29:51 +00:00
|
|
|
|
BYTE s[SC_MAX_APDU_BUFFER_SIZE], r[SC_MAX_APDU_BUFFER_SIZE];
|
2001-11-01 15:43:20 +00:00
|
|
|
|
DWORD dwSendLength, dwRecvLength;
|
|
|
|
|
LONG rv;
|
|
|
|
|
u8 *data = s;
|
|
|
|
|
int data_bytes = apdu->lc;
|
|
|
|
|
|
|
|
|
|
if (data_bytes == 0)
|
|
|
|
|
data_bytes = 256;
|
|
|
|
|
*data++ = apdu->cla;
|
|
|
|
|
*data++ = apdu->ins;
|
|
|
|
|
*data++ = apdu->p1;
|
|
|
|
|
*data++ = apdu->p2;
|
|
|
|
|
switch (apdu->cse) {
|
|
|
|
|
case SC_APDU_CASE_1:
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_SHORT:
|
|
|
|
|
*data++ = apdu->le;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
*data++ = 0;
|
|
|
|
|
*data++ = apdu->le >> 8;
|
|
|
|
|
*data++ = apdu->le & 0xFF;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
|
|
|
|
*data++ = apdu->lc;
|
|
|
|
|
if (apdu->datalen != data_bytes)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
memcpy(data, apdu->data, data_bytes);
|
|
|
|
|
data += data_bytes;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_4_SHORT:
|
|
|
|
|
*data++ = apdu->lc;
|
|
|
|
|
if (apdu->datalen != data_bytes)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
memcpy(data, apdu->data, data_bytes);
|
|
|
|
|
data += data_bytes;
|
|
|
|
|
*data++ = apdu->le;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sSendPci.dwProtocol = SCARD_PROTOCOL_T0;
|
|
|
|
|
sSendPci.cbPciLength = 0;
|
|
|
|
|
sRecvPci.dwProtocol = SCARD_PROTOCOL_T0;
|
|
|
|
|
sRecvPci.cbPciLength = 0;
|
|
|
|
|
|
|
|
|
|
dwSendLength = data - s;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
dwRecvLength = apdu->resplen + 2;
|
2001-12-16 18:46:32 +00:00
|
|
|
|
if (dwRecvLength > 255) /* FIXME: PC/SC Lite quirk */
|
|
|
|
|
dwRecvLength = 255;
|
2001-12-19 21:58:04 +00:00
|
|
|
|
if (card->ctx->debug > 3) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
|
|
sc_hex_dump(card->ctx, s, dwSendLength, buf, sizeof(buf));
|
|
|
|
|
debug(card->ctx, "Sending %d bytes (resp. %d bytes):\n%s",
|
|
|
|
|
dwSendLength, dwRecvLength, buf);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
rv = SCardTransmit(card->pcsc_card, &sSendPci, s, dwSendLength,
|
|
|
|
|
&sRecvPci, r, &dwRecvLength);
|
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
2001-11-17 15:48:10 +00:00
|
|
|
|
switch (rv) {
|
|
|
|
|
case SCARD_W_REMOVED_CARD:
|
|
|
|
|
return SC_ERROR_CARD_REMOVED;
|
2001-12-08 15:27:40 +00:00
|
|
|
|
case SCARD_W_RESET_CARD:
|
|
|
|
|
return SC_ERROR_CARD_RESET;
|
2001-11-17 15:48:10 +00:00
|
|
|
|
case SCARD_E_NOT_TRANSACTED:
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (sc_detect_card(card->ctx, card->reader) != 1)
|
2001-11-17 15:48:10 +00:00
|
|
|
|
return SC_ERROR_CARD_REMOVED;
|
|
|
|
|
return SC_ERROR_TRANSMIT_FAILED;
|
|
|
|
|
default:
|
2001-12-13 21:19:11 +00:00
|
|
|
|
error(card->ctx, "SCardTransmit failed: %s\n", pcsc_stringify_error(rv));
|
2001-12-08 15:27:40 +00:00
|
|
|
|
return SC_ERROR_TRANSMIT_FAILED;
|
2001-11-17 15:48:10 +00:00
|
|
|
|
}
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (dwRecvLength < 2)
|
|
|
|
|
return SC_ERROR_UNKNOWN_RESPONSE;
|
|
|
|
|
apdu->sw1 = r[dwRecvLength-2];
|
|
|
|
|
apdu->sw2 = r[dwRecvLength-1];
|
|
|
|
|
dwRecvLength -= 2;
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (dwRecvLength > apdu->resplen)
|
|
|
|
|
dwRecvLength = apdu->resplen;
|
|
|
|
|
else
|
|
|
|
|
apdu->resplen = dwRecvLength;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (dwRecvLength)
|
|
|
|
|
memcpy(apdu->resp, r, dwRecvLength);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
|
|
|
|
|
SC_FUNC_CALLED(card->ctx);
|
2001-12-16 18:46:32 +00:00
|
|
|
|
r = sc_check_apdu(card->ctx, apdu);
|
2001-12-15 01:29:51 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU sanity check failed");
|
2001-11-01 15:43:20 +00:00
|
|
|
|
r = sc_transceive_t0(card, apdu);
|
2001-12-15 01:29:51 +00:00
|
|
|
|
SC_TEST_RET(card->ctx, r, "transceive_t0() failed");
|
2001-12-19 21:58:04 +00:00
|
|
|
|
if (card->ctx->debug > 3) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
|
|
buf[0] = 0;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (apdu->resplen) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
sc_hex_dump(card->ctx, apdu->resp, apdu->resplen,
|
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
}
|
|
|
|
|
debug(card->ctx, "Received %d bytes (SW1=%02X SW2=%02X)\n%s",
|
|
|
|
|
apdu->resplen, apdu->sw1, apdu->sw2, buf);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
}
|
|
|
|
|
if (apdu->sw1 == 0x61 && apdu->resplen == 0) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
struct sc_apdu rspapdu;
|
2001-12-15 01:29:51 +00:00
|
|
|
|
BYTE rsp[SC_MAX_APDU_BUFFER_SIZE];
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (apdu->no_response)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &rspapdu, SC_APDU_CASE_2_SHORT,
|
|
|
|
|
0xC0, 0, 0);
|
|
|
|
|
rspapdu.le = apdu->sw2;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
rspapdu.resp = rsp;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
rspapdu.resplen = apdu->sw2;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
r = sc_transceive_t0(card, &rspapdu);
|
|
|
|
|
if (r) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
error(card->ctx, "error while getting response: %s\n",
|
|
|
|
|
sc_strerror(r));
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2001-12-19 21:58:04 +00:00
|
|
|
|
if (card->ctx->debug > 3) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
char buf[2048];
|
|
|
|
|
buf[0] = 0;
|
2001-12-15 01:29:51 +00:00
|
|
|
|
if (rspapdu.resplen) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
sc_hex_dump(card->ctx, rspapdu.resp,
|
|
|
|
|
rspapdu.resplen,
|
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
}
|
|
|
|
|
debug(card->ctx, "Response %d bytes (SW1=%02X SW2=%02X)\n%s",
|
|
|
|
|
rspapdu.resplen, rspapdu.sw1, rspapdu.sw2, buf);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
/* FIXME: Check apdu->resplen */
|
2001-11-01 15:43:20 +00:00
|
|
|
|
memcpy(apdu->resp, rspapdu.resp, rspapdu.resplen);
|
|
|
|
|
apdu->resplen = rspapdu.resplen;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
apdu->sw1 = rspapdu.sw1;
|
|
|
|
|
apdu->sw2 = rspapdu.sw2;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int sc_format_apdu(struct sc_card *card, struct sc_apdu *apdu,
|
|
|
|
|
unsigned char cse, unsigned char ins,
|
|
|
|
|
unsigned char p1, unsigned char p2)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
|
|
|
|
assert(card != NULL && apdu != NULL);
|
|
|
|
|
memset(apdu, 0, sizeof(*apdu));
|
2001-12-08 15:27:40 +00:00
|
|
|
|
apdu->cla = card->cla;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
apdu->cse = cse;
|
|
|
|
|
apdu->ins = ins;
|
|
|
|
|
apdu->p1 = p1;
|
|
|
|
|
apdu->p2 = p2;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
apdu->no_response = 0;
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_detect_card(struct sc_context *ctx, int reader)
|
|
|
|
|
{
|
|
|
|
|
LONG ret;
|
|
|
|
|
SCARD_READERSTATE_A rgReaderStates[SC_MAX_READERS];
|
|
|
|
|
|
|
|
|
|
assert(ctx != NULL);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (reader >= ctx->reader_count || reader < 0)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
|
|
|
|
|
rgReaderStates[0].szReader = ctx->readers[reader];
|
2001-11-07 13:45:41 +00:00
|
|
|
|
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
ret = SCardGetStatusChange(ctx->pcsc_ctx, 0, rgReaderStates, 1);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (ret != 0) {
|
|
|
|
|
error(ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(ret));
|
|
|
|
|
SC_FUNC_RETURN(ctx, -1); /* FIXME */
|
|
|
|
|
}
|
2001-11-07 13:45:41 +00:00
|
|
|
|
if (rgReaderStates[0].dwEventState & SCARD_STATE_PRESENT)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 1);
|
|
|
|
|
SC_FUNC_RETURN(ctx, 0);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_wait_for_card(struct sc_context *ctx, int reader, int timeout)
|
|
|
|
|
{
|
|
|
|
|
LONG ret;
|
|
|
|
|
SCARD_READERSTATE_A rgReaderStates[SC_MAX_READERS];
|
|
|
|
|
int count = 0, i;
|
|
|
|
|
|
|
|
|
|
assert(ctx != NULL);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (reader >= ctx->reader_count)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
|
|
|
|
|
if (reader < 0) {
|
|
|
|
|
if (ctx->reader_count == 0)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_NO_READERS_FOUND);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
for (i = 0; i < ctx->reader_count; i++) {
|
|
|
|
|
rgReaderStates[i].szReader = ctx->readers[i];
|
|
|
|
|
rgReaderStates[i].dwCurrentState =
|
|
|
|
|
SCARD_STATE_EMPTY;
|
|
|
|
|
}
|
|
|
|
|
count = ctx->reader_count;
|
|
|
|
|
} else {
|
|
|
|
|
rgReaderStates[0].szReader = ctx->readers[reader];
|
|
|
|
|
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
|
|
|
|
|
count = 1;
|
|
|
|
|
}
|
|
|
|
|
ret = SCardGetStatusChange(ctx->pcsc_ctx, timeout, rgReaderStates,
|
|
|
|
|
count);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (ret != 0) {
|
|
|
|
|
error(ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(ret));
|
|
|
|
|
SC_FUNC_RETURN(ctx, -1);
|
|
|
|
|
}
|
2001-11-01 15:43:20 +00:00
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
if (rgReaderStates[i].dwEventState & SCARD_STATE_CHANGED)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 1);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 0);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_establish_context(struct sc_context **ctx_out)
|
|
|
|
|
{
|
|
|
|
|
struct sc_context *ctx;
|
|
|
|
|
LONG rv;
|
|
|
|
|
DWORD reader_buf_size;
|
|
|
|
|
char *reader_buf, *p;
|
2001-11-14 13:43:39 +00:00
|
|
|
|
LPCSTR mszGroups = NULL;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
assert(ctx_out != NULL);
|
|
|
|
|
ctx = malloc(sizeof(struct sc_context));
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
ctx->use_std_output = 0;
|
|
|
|
|
ctx->use_cache = 1;
|
2001-12-19 21:58:04 +00:00
|
|
|
|
ctx->debug = 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, "localhost", NULL,
|
|
|
|
|
&ctx->pcsc_ctx);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (rv != SCARD_S_SUCCESS)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return SC_ERROR_CONNECTING_TO_RES_MGR;
|
|
|
|
|
SCardListReaders(ctx->pcsc_ctx, NULL, NULL,
|
|
|
|
|
(LPDWORD) & reader_buf_size);
|
2001-11-22 15:40:51 +00:00
|
|
|
|
if (reader_buf_size < 2) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
free(ctx);
|
|
|
|
|
return SC_ERROR_NO_READERS_FOUND;
|
|
|
|
|
}
|
|
|
|
|
reader_buf = (char *) malloc(sizeof(char) * reader_buf_size);
|
|
|
|
|
SCardListReaders(ctx->pcsc_ctx, mszGroups, reader_buf,
|
|
|
|
|
(LPDWORD) & reader_buf_size);
|
|
|
|
|
p = reader_buf;
|
|
|
|
|
ctx->reader_count = 0;
|
|
|
|
|
do {
|
|
|
|
|
ctx->readers[ctx->reader_count] = strdup(p);
|
|
|
|
|
ctx->reader_count++;
|
|
|
|
|
while (*p++ != 0);
|
2001-11-14 13:43:39 +00:00
|
|
|
|
if (ctx->reader_count == SC_MAX_READERS)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
break;
|
|
|
|
|
} while (p < (reader_buf + reader_buf_size - 1));
|
|
|
|
|
free(reader_buf);
|
|
|
|
|
|
|
|
|
|
*ctx_out = ctx;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_destroy_context(struct sc_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
assert(ctx != NULL);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
for (i = 0; i < ctx->reader_count; i++)
|
|
|
|
|
free(ctx->readers[i]);
|
|
|
|
|
free(ctx);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-26 16:14:22 +00:00
|
|
|
|
static const struct sc_defaults * find_defaults(const u8 *atr, int atrlen)
|
2001-11-17 14:55:41 +00:00
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
const struct sc_defaults *match = NULL;
|
|
|
|
|
|
|
|
|
|
while (sc_card_table[i].atr != NULL) {
|
2001-11-26 16:14:22 +00:00
|
|
|
|
u8 defatr[SC_MAX_ATR_SIZE];
|
|
|
|
|
int len = sizeof(defatr);
|
2001-11-17 14:55:41 +00:00
|
|
|
|
const struct sc_defaults *def = &sc_card_table[i];
|
|
|
|
|
const char *atrp = def->atr;
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
if (atrp == NULL)
|
|
|
|
|
break;
|
2001-11-26 16:14:22 +00:00
|
|
|
|
if (sc_hex_to_bin(atrp, defatr, &len))
|
2001-11-17 14:55:41 +00:00
|
|
|
|
continue;
|
|
|
|
|
if (len != atrlen)
|
|
|
|
|
continue;
|
|
|
|
|
if (memcmp(atr, defatr, len) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
match = def;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return match;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
int sc_connect_card(struct sc_context *ctx,
|
|
|
|
|
int reader, struct sc_card **card_out)
|
|
|
|
|
{
|
|
|
|
|
struct sc_card *card;
|
|
|
|
|
DWORD active_proto;
|
|
|
|
|
SCARDHANDLE card_handle;
|
2001-11-07 13:45:41 +00:00
|
|
|
|
SCARD_READERSTATE_A rgReaderStates[SC_MAX_READERS];
|
2001-11-01 15:43:20 +00:00
|
|
|
|
LONG rv;
|
2001-11-07 13:45:41 +00:00
|
|
|
|
int i;
|
2001-11-26 16:14:22 +00:00
|
|
|
|
const struct sc_defaults *defaults;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
assert(card_out != NULL);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (reader >= ctx->reader_count || reader < 0)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND);
|
2001-11-07 13:45:41 +00:00
|
|
|
|
|
|
|
|
|
rgReaderStates[0].szReader = ctx->readers[reader];
|
|
|
|
|
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
|
|
|
|
|
rv = SCardGetStatusChange(ctx->pcsc_ctx, 0, rgReaderStates, 1);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (rv != 0) {
|
|
|
|
|
error(ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(rv));
|
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_RESOURCE_MANAGER); /* FIXME */
|
|
|
|
|
}
|
2001-11-07 13:45:41 +00:00
|
|
|
|
if (!(rgReaderStates[0].dwEventState & SCARD_STATE_PRESENT))
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_CARD_NOT_PRESENT);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
card = malloc(sizeof(struct sc_card));
|
|
|
|
|
if (card == NULL)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
memset(card, 0, sizeof(struct sc_card));
|
|
|
|
|
rv = SCardConnect(ctx->pcsc_ctx, ctx->readers[reader],
|
|
|
|
|
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0,
|
|
|
|
|
&card_handle, &active_proto);
|
|
|
|
|
if (rv != 0) {
|
2001-12-13 21:19:11 +00:00
|
|
|
|
error(ctx, "SCardConnect failed: %s\n", pcsc_stringify_error(rv));
|
2001-11-01 15:43:20 +00:00
|
|
|
|
free(card);
|
|
|
|
|
return -1; /* FIXME */
|
|
|
|
|
}
|
2001-11-17 15:48:10 +00:00
|
|
|
|
card->reader = reader;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
card->ctx = ctx;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
card->pcsc_card = card_handle;
|
2001-11-07 13:45:41 +00:00
|
|
|
|
i = rgReaderStates[0].cbAtr;
|
|
|
|
|
if (i >= SC_MAX_ATR_SIZE)
|
|
|
|
|
i = SC_MAX_ATR_SIZE;
|
|
|
|
|
memcpy(card->atr, rgReaderStates[0].rgbAtr, i);
|
|
|
|
|
card->atr_len = i;
|
2001-11-17 14:55:41 +00:00
|
|
|
|
|
2001-11-26 16:14:22 +00:00
|
|
|
|
defaults = find_defaults(card->atr, card->atr_len);
|
|
|
|
|
if (defaults != NULL && defaults->defaults_func != NULL) {
|
|
|
|
|
defaults->defaults_func(card);
|
2001-11-17 14:55:41 +00:00
|
|
|
|
} else {
|
2001-12-08 15:27:40 +00:00
|
|
|
|
card->cla = 0; /* FIXME */
|
2001-11-17 14:55:41 +00:00
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
pthread_mutex_init(&card->mutex, NULL);
|
2001-11-07 13:45:41 +00:00
|
|
|
|
*card_out = card;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_disconnect_card(struct sc_card *card)
|
|
|
|
|
{
|
|
|
|
|
assert(card != NULL);
|
2001-11-04 14:08:38 +00:00
|
|
|
|
SCardDisconnect(card->pcsc_card, SCARD_LEAVE_CARD);
|
2001-11-20 22:21:58 +00:00
|
|
|
|
pthread_mutex_destroy(&card->mutex);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *sc_strerror(int error)
|
|
|
|
|
{
|
|
|
|
|
const char *errors[] = {
|
|
|
|
|
"Unknown error",
|
|
|
|
|
"Command too short",
|
|
|
|
|
"Command too long",
|
|
|
|
|
"Not supported",
|
|
|
|
|
"Transmit failed",
|
|
|
|
|
"File not found",
|
|
|
|
|
"Invalid arguments",
|
|
|
|
|
"PKCS#15 compatible SmartCard not found",
|
|
|
|
|
"Required parameter not found on SmartCard",
|
|
|
|
|
"Out of memory",
|
|
|
|
|
"No readers found",
|
|
|
|
|
"Object not valid",
|
|
|
|
|
"Unknown response",
|
|
|
|
|
"PIN code incorrect",
|
|
|
|
|
"Security status not satisfied",
|
|
|
|
|
"Error connecting to Resource Manager",
|
2001-11-17 15:48:10 +00:00
|
|
|
|
"Invalid ASN.1 object",
|
2001-11-04 13:57:04 +00:00
|
|
|
|
"Buffer too small",
|
2001-11-17 15:48:10 +00:00
|
|
|
|
"Card not present",
|
|
|
|
|
"Error with Resource Manager",
|
|
|
|
|
"Card removed",
|
2001-11-20 22:21:58 +00:00
|
|
|
|
"Invalid PIN length",
|
|
|
|
|
"Unknown SmartCard",
|
|
|
|
|
"Unknown reply from SmartCard",
|
|
|
|
|
"Requested object not found",
|
2001-12-19 21:58:04 +00:00
|
|
|
|
"Card reset",
|
2001-12-16 18:46:32 +00:00
|
|
|
|
"Required ASN.1 object not found",
|
|
|
|
|
"Premature end of ASN.1 stream",
|
|
|
|
|
"Too many objects",
|
2001-11-01 15:43:20 +00:00
|
|
|
|
};
|
|
|
|
|
int nr_errors = sizeof(errors) / sizeof(errors[0]);
|
|
|
|
|
|
|
|
|
|
error -= SC_ERROR_MIN;
|
|
|
|
|
if (error < 0)
|
|
|
|
|
error = -error;
|
|
|
|
|
|
|
|
|
|
if (error >= nr_errors)
|
|
|
|
|
return errors[0];
|
|
|
|
|
return errors[error];
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int _sc_lock_int(struct sc_card *card)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2001-11-20 22:21:58 +00:00
|
|
|
|
long rv;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
rv = SCardBeginTransaction(card->pcsc_card);
|
2001-11-17 00:11:29 +00:00
|
|
|
|
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
error(card->ctx, "SCardBeginTransaction failed: %s\n", pcsc_stringify_error(rv));
|
2001-11-17 00:11:29 +00:00
|
|
|
|
return -1;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
2001-11-20 22:21:58 +00:00
|
|
|
|
return 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int sc_lock(struct sc_card *card)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2001-11-20 22:21:58 +00:00
|
|
|
|
pthread_mutex_lock(&card->mutex);
|
|
|
|
|
return _sc_lock_int(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
int _sc_unlock_int(struct sc_card *card)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
|
|
|
|
long rv;
|
|
|
|
|
|
2001-11-20 22:21:58 +00:00
|
|
|
|
rv = SCardEndTransaction(card->pcsc_card, SCARD_LEAVE_CARD);
|
2001-12-13 21:19:11 +00:00
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
error(card->ctx, "SCardEndTransaction failed: %s\n", pcsc_stringify_error(rv));
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return -1;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
}
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_unlock(struct sc_card *card)
|
|
|
|
|
{
|
2001-11-20 22:21:58 +00:00
|
|
|
|
pthread_mutex_unlock(&card->mutex);
|
|
|
|
|
return _sc_unlock_int(card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 14:36:57 +00:00
|
|
|
|
int sc_list_files(struct sc_card *card, u8 *buf, int buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_apdu apdu;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xAA, 0, 0);
|
|
|
|
|
apdu.resp = buf;
|
|
|
|
|
apdu.resplen = buflen;
|
|
|
|
|
apdu.le = 0;
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
2001-11-20 22:21:58 +00:00
|
|
|
|
if (apdu.resplen == 0)
|
2001-12-13 21:19:11 +00:00
|
|
|
|
return sc_sw_to_errorcode(card, apdu.sw1, apdu.sw2);
|
2001-11-07 14:36:57 +00:00
|
|
|
|
return apdu.resplen;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-17 00:11:29 +00:00
|
|
|
|
static int construct_fci(const struct sc_file *file, u8 *out, int *outlen)
|
|
|
|
|
{
|
|
|
|
|
u8 *p = out;
|
|
|
|
|
u8 buf[32];
|
|
|
|
|
|
|
|
|
|
*p++ = 0x6F;
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
buf[0] = (file->size >> 8) & 0xFF;
|
|
|
|
|
buf[1] = file->size & 0xFF;
|
|
|
|
|
sc_asn1_put_tag(0x81, buf, 2, p, 16, &p);
|
|
|
|
|
buf[0] = file->shareable ? 0x40 : 0;
|
|
|
|
|
buf[0] |= (file->type & 7) << 3;
|
|
|
|
|
buf[0] |= file->ef_structure & 7;
|
|
|
|
|
sc_asn1_put_tag(0x82, buf, 1, p, 16, &p);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
*p++ = 0xDE;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
*outlen = p - out;
|
|
|
|
|
out[1] = p - out - 2;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_create_file(struct sc_card *card, const struct sc_file *file)
|
|
|
|
|
{
|
|
|
|
|
int r, len;
|
2001-12-15 01:29:51 +00:00
|
|
|
|
u8 sbuf[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2001-11-17 00:11:29 +00:00
|
|
|
|
struct sc_apdu apdu;
|
|
|
|
|
|
2001-12-15 01:29:51 +00:00
|
|
|
|
len = SC_MAX_APDU_BUFFER_SIZE;
|
2001-11-17 00:11:29 +00:00
|
|
|
|
r = construct_fci(file, sbuf, &len);
|
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE0, 0x00, 0x00);
|
|
|
|
|
apdu.lc = len;
|
|
|
|
|
apdu.datalen = len;
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.resplen = sizeof(rbuf);
|
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
return sc_sw_to_errorcode(card, apdu.sw1, apdu.sw2);
|
2001-11-17 00:11:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_delete_file(struct sc_card *card, int file_id)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
u8 sbuf[2];
|
2001-12-15 01:29:51 +00:00
|
|
|
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
2001-11-17 00:11:29 +00:00
|
|
|
|
struct sc_apdu apdu;
|
|
|
|
|
|
|
|
|
|
sbuf[0] = (file_id >> 8) & 0xFF;
|
|
|
|
|
sbuf[1] = file_id & 0xFF;
|
|
|
|
|
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xE4, 0x00, 0x00);
|
|
|
|
|
apdu.lc = 2;
|
|
|
|
|
apdu.datalen = 2;
|
|
|
|
|
apdu.data = sbuf;
|
|
|
|
|
apdu.resplen = sizeof(rbuf);
|
|
|
|
|
apdu.resp = rbuf;
|
|
|
|
|
|
|
|
|
|
r = sc_transmit_apdu(card, &apdu);
|
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
|
|
|
|
if (apdu.resplen != 2)
|
|
|
|
|
return -1;
|
2001-12-13 21:19:11 +00:00
|
|
|
|
return sc_sw_to_errorcode(card, apdu.sw1, apdu.sw2);
|
2001-11-17 00:11:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 14:36:57 +00:00
|
|
|
|
int sc_file_valid(const struct sc_file *file)
|
|
|
|
|
{
|
|
|
|
|
assert(file != NULL);
|
|
|
|
|
|
|
|
|
|
return file->magic == SC_FILE_MAGIC;
|
|
|
|
|
}
|