2001-12-22 20:43:09 +00:00
|
|
|
|
/*
|
2002-01-08 13:56:50 +00:00
|
|
|
|
* card.c: General SmartCard functions
|
2001-12-22 20:43:09 +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,
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#include "sc-internal.h"
|
2001-12-22 20:43:09 +00:00
|
|
|
|
#include "sc-log.h"
|
|
|
|
|
#include "sc-asn1.h"
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
int sc_sw_to_errorcode(struct sc_card *card, int sw1, int sw2)
|
|
|
|
|
{
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (sw1 != 0x90 || sw2 != 0)
|
|
|
|
|
error(card->ctx, "card returned SW1=%02X, SW2=%02X\n", sw1, sw2);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
switch (sw1) {
|
2001-12-29 02:07:32 +00:00
|
|
|
|
case 0x67:
|
|
|
|
|
if (sw2 == 0)
|
|
|
|
|
return SC_ERROR_WRONG_LENGTH;
|
|
|
|
|
break;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
case 0x69:
|
|
|
|
|
switch (sw2) {
|
|
|
|
|
case 0x82:
|
|
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x6A:
|
|
|
|
|
switch (sw2) {
|
|
|
|
|
case 0x81:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
case 0x82:
|
|
|
|
|
return SC_ERROR_FILE_NOT_FOUND;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
case 0x83:
|
|
|
|
|
return SC_ERROR_RECORD_NOT_FOUND;
|
|
|
|
|
case 0x85:
|
2001-12-22 20:43:09 +00:00
|
|
|
|
case 0x86:
|
|
|
|
|
case 0x87:
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
case 0x6C:
|
|
|
|
|
error(card->ctx, "incorrect length, right length is %d\n",
|
|
|
|
|
sw2);
|
|
|
|
|
return SC_ERROR_WRONG_LENGTH;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
case 0x6D:
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
case 0x6E:
|
|
|
|
|
return SC_ERROR_UNKNOWN_SMARTCARD;
|
|
|
|
|
case 0x90:
|
|
|
|
|
if (sw2 == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return SC_ERROR_UNKNOWN_REPLY;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
static int _sc_pcscret_to_error(long rv)
|
|
|
|
|
{
|
|
|
|
|
switch (rv) {
|
|
|
|
|
case SCARD_W_REMOVED_CARD:
|
|
|
|
|
return SC_ERROR_CARD_REMOVED;
|
|
|
|
|
case SCARD_W_RESET_CARD:
|
|
|
|
|
return SC_ERROR_CARD_RESET;
|
|
|
|
|
case SCARD_E_NOT_TRANSACTED:
|
|
|
|
|
return SC_ERROR_TRANSMIT_FAILED;
|
|
|
|
|
default:
|
|
|
|
|
return SC_ERROR_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
static int sc_check_apdu(struct sc_context *ctx, const struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
switch (apdu->cse) {
|
|
|
|
|
case SC_APDU_CASE_1:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen > 0) {
|
|
|
|
|
error(ctx, "Case 1 APDU with data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen > 0) {
|
|
|
|
|
error(ctx, "Case 2 APDU with data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (apdu->resplen < apdu->le) {
|
|
|
|
|
error(ctx, "Response buffer size < Le\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL) {
|
|
|
|
|
error(ctx, "Case 3 APDU with no data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_4_SHORT:
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (apdu->datalen == 0 || apdu->data == NULL) {
|
|
|
|
|
error(ctx, "Case 3 APDU with no data supplied\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
if (apdu->resplen < apdu->le) {
|
|
|
|
|
error(ctx, "Le > response buffer size\n");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
case SC_APDU_CASE_3_EXT:
|
|
|
|
|
case SC_APDU_CASE_4_EXT:
|
|
|
|
|
SC_FUNC_RETURN(ctx, 4, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sc_transceive_t0(struct sc_card *card, struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
SCARD_IO_REQUEST sSendPci, sRecvPci;
|
|
|
|
|
BYTE s[SC_MAX_APDU_BUFFER_SIZE], r[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
DWORD dwSendLength, dwRecvLength;
|
|
|
|
|
LONG rv;
|
|
|
|
|
u8 *data = s;
|
|
|
|
|
size_t 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++ = (u8) apdu->le;
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_2_EXT:
|
|
|
|
|
*data++ = (u8) 0;
|
|
|
|
|
*data++ = (u8) (apdu->le >> 8);
|
|
|
|
|
*data++ = (u8) (apdu->le & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
case SC_APDU_CASE_3_SHORT:
|
|
|
|
|
*data++ = (u8) 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++ = (u8) apdu->lc;
|
|
|
|
|
if (apdu->datalen != data_bytes)
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
memcpy(data, apdu->data, data_bytes);
|
|
|
|
|
data += data_bytes;
|
|
|
|
|
*data++ = (u8) apdu->le;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sSendPci.dwProtocol = SCARD_PROTOCOL_T0;
|
|
|
|
|
sSendPci.cbPciLength = 0;
|
|
|
|
|
sRecvPci.dwProtocol = SCARD_PROTOCOL_T0;
|
|
|
|
|
sRecvPci.cbPciLength = 0;
|
|
|
|
|
|
|
|
|
|
dwSendLength = data - s;
|
|
|
|
|
dwRecvLength = apdu->resplen + 2;
|
|
|
|
|
if (dwRecvLength > 255) /* FIXME: PC/SC Lite quirk */
|
|
|
|
|
dwRecvLength = 255;
|
|
|
|
|
if (card->ctx->debug >= 5) {
|
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
|
|
sc_hex_dump(card->ctx, s, (size_t) dwSendLength, buf, sizeof(buf));
|
|
|
|
|
debug(card->ctx, "Sending %d bytes (resp. %d bytes):\n%s",
|
|
|
|
|
dwSendLength, dwRecvLength, buf);
|
|
|
|
|
}
|
|
|
|
|
rv = SCardTransmit(card->pcsc_card, &sSendPci, s, dwSendLength,
|
|
|
|
|
&sRecvPci, r, &dwRecvLength);
|
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
switch (rv) {
|
|
|
|
|
case SCARD_W_REMOVED_CARD:
|
|
|
|
|
return SC_ERROR_CARD_REMOVED;
|
|
|
|
|
case SCARD_W_RESET_CARD:
|
|
|
|
|
return SC_ERROR_CARD_RESET;
|
|
|
|
|
case SCARD_E_NOT_TRANSACTED:
|
|
|
|
|
if (sc_detect_card(card->ctx, card->reader) != 1)
|
|
|
|
|
return SC_ERROR_CARD_REMOVED;
|
|
|
|
|
return SC_ERROR_TRANSMIT_FAILED;
|
|
|
|
|
default:
|
|
|
|
|
error(card->ctx, "SCardTransmit failed: %s\n", pcsc_stringify_error(rv));
|
|
|
|
|
return SC_ERROR_TRANSMIT_FAILED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (dwRecvLength < 2)
|
|
|
|
|
return SC_ERROR_ILLEGAL_RESPONSE;
|
|
|
|
|
apdu->sw1 = (unsigned int) r[dwRecvLength-2];
|
|
|
|
|
apdu->sw2 = (unsigned int) r[dwRecvLength-1];
|
|
|
|
|
dwRecvLength -= 2;
|
|
|
|
|
if ((size_t) dwRecvLength > apdu->resplen)
|
|
|
|
|
dwRecvLength = (DWORD) apdu->resplen;
|
|
|
|
|
else
|
|
|
|
|
apdu->resplen = (size_t) dwRecvLength;
|
|
|
|
|
if (dwRecvLength > 0)
|
|
|
|
|
memcpy(apdu->resp, r, (size_t) dwRecvLength);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_transmit_apdu(struct sc_card *card, struct sc_apdu *apdu)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
size_t orig_resplen;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
assert(card != NULL && apdu != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 4);
|
2001-12-29 02:07:32 +00:00
|
|
|
|
orig_resplen = apdu->resplen;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
r = sc_check_apdu(card->ctx, apdu);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU sanity check failed");
|
2001-12-25 20:45:48 +00:00
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
r = sc_transceive_t0(card, apdu);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r != 0) {
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "transceive_t0() failed");
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (card->ctx->debug >= 5) {
|
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
if (apdu->resplen > 0) {
|
|
|
|
|
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-12-29 02:07:32 +00:00
|
|
|
|
if (apdu->sw1 == 0x6C && apdu->resplen == 0) {
|
|
|
|
|
apdu->resplen = orig_resplen;
|
|
|
|
|
apdu->le = apdu->sw2;
|
|
|
|
|
r = sc_transceive_t0(card, apdu);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "transceive_t0() failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (apdu->sw1 == 0x61 && apdu->resplen == 0) {
|
|
|
|
|
struct sc_apdu rspapdu;
|
|
|
|
|
BYTE rsp[SC_MAX_APDU_BUFFER_SIZE];
|
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (orig_resplen == 0) {
|
|
|
|
|
apdu->sw1 = 0x90; /* FIXME: should we do this? */
|
2001-12-25 20:45:48 +00:00
|
|
|
|
apdu->sw2 = 0;
|
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return 0;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
sc_format_apdu(card, &rspapdu, SC_APDU_CASE_2_SHORT,
|
|
|
|
|
0xC0, 0, 0);
|
|
|
|
|
rspapdu.le = (size_t) apdu->sw2;
|
|
|
|
|
rspapdu.resp = rsp;
|
|
|
|
|
rspapdu.resplen = (size_t) apdu->sw2;
|
|
|
|
|
r = sc_transceive_t0(card, &rspapdu);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
error(card->ctx, "error while getting response: %s\n",
|
|
|
|
|
sc_strerror(r));
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
if (card->ctx->debug >= 5) {
|
|
|
|
|
char buf[2048];
|
|
|
|
|
buf[0] = 0;
|
|
|
|
|
if (rspapdu.resplen) {
|
|
|
|
|
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-12-29 02:07:32 +00:00
|
|
|
|
if (rspapdu.resplen) {
|
|
|
|
|
size_t c = rspapdu.resplen;
|
|
|
|
|
|
|
|
|
|
if (c > orig_resplen)
|
|
|
|
|
c = orig_resplen;
|
|
|
|
|
memcpy(apdu->resp, rspapdu.resp, c);
|
|
|
|
|
apdu->resplen = c;
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
apdu->sw1 = rspapdu.sw1;
|
|
|
|
|
apdu->sw2 = rspapdu.sw2;
|
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sc_format_apdu(struct sc_card *card, struct sc_apdu *apdu,
|
|
|
|
|
int cse, int ins, int p1, int p2)
|
|
|
|
|
{
|
|
|
|
|
assert(card != NULL && apdu != NULL);
|
|
|
|
|
memset(apdu, 0, sizeof(*apdu));
|
|
|
|
|
apdu->cla = (u8) card->cla;
|
|
|
|
|
apdu->cse = cse;
|
|
|
|
|
apdu->ins = (u8) ins;
|
|
|
|
|
apdu->p1 = (u8) p1;
|
|
|
|
|
apdu->p2 = (u8) p2;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
SCARD_READERSTATE_A rgReaderStates[SC_MAX_READERS];
|
|
|
|
|
LONG rv;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int i, r = 0;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
|
|
|
|
assert(card_out != NULL);
|
|
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
|
|
|
|
if (reader >= ctx->reader_count || reader < 0)
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_OBJECT_NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
rgReaderStates[0].szReader = ctx->readers[reader];
|
|
|
|
|
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
|
2002-01-05 19:01:55 +00:00
|
|
|
|
rgReaderStates[0].dwEventState = SCARD_STATE_UNAWARE;
|
|
|
|
|
rv = SCardGetStatusChange(ctx->pcsc_ctx, SC_STATUS_TIMEOUT, rgReaderStates, 1);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (rv != 0) {
|
|
|
|
|
error(ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(rv));
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_RESOURCE_MANAGER); /* FIXME */
|
|
|
|
|
}
|
|
|
|
|
if (!(rgReaderStates[0].dwEventState & SCARD_STATE_PRESENT))
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_CARD_NOT_PRESENT);
|
|
|
|
|
|
|
|
|
|
card = malloc(sizeof(struct sc_card));
|
|
|
|
|
if (card == NULL)
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
memset(card, 0, sizeof(struct sc_card));
|
2002-01-08 13:56:50 +00:00
|
|
|
|
card->ops = malloc(sizeof(struct sc_card_operations));
|
|
|
|
|
if (card->ops == NULL) {
|
|
|
|
|
free(card);
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, SC_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
rv = SCardConnect(ctx->pcsc_ctx, ctx->readers[reader],
|
|
|
|
|
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0,
|
|
|
|
|
&card_handle, &active_proto);
|
|
|
|
|
if (rv != 0) {
|
|
|
|
|
error(ctx, "SCardConnect failed: %s\n", pcsc_stringify_error(rv));
|
2002-01-08 13:56:50 +00:00
|
|
|
|
r = -1; /* FIXME: invent a real error value */
|
|
|
|
|
goto err;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
card->reader = reader;
|
|
|
|
|
card->ctx = ctx;
|
|
|
|
|
card->pcsc_card = card_handle;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
card->lock_count = 0;
|
2001-12-22 20:43:09 +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;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
|
|
|
|
|
if (ctx->default_driver != NULL) {
|
|
|
|
|
card->driver = ctx->default_driver;
|
|
|
|
|
memcpy(card->ops, card->driver->ops, sizeof(struct sc_card_operations));
|
|
|
|
|
if (card->ops->init != NULL) {
|
|
|
|
|
r = card->ops->init(card);
|
|
|
|
|
if (r) {
|
|
|
|
|
error(ctx, "driver '%s' init() failed: %s\n", card->driver->name,
|
|
|
|
|
sc_strerror(r));
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else for (i = 0; ctx->card_drivers[i] != NULL; i++) {
|
2001-12-22 20:43:09 +00:00
|
|
|
|
const struct sc_card_driver *drv = ctx->card_drivers[i];
|
|
|
|
|
const struct sc_card_operations *ops = drv->ops;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
if (ctx->debug >= 3)
|
|
|
|
|
debug(ctx, "trying driver: %s\n", drv->name);
|
|
|
|
|
if (ops == NULL || ops->match_card == NULL)
|
|
|
|
|
continue;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (ops->match_card(card) != 1)
|
|
|
|
|
continue;
|
|
|
|
|
if (ctx->debug >= 3)
|
|
|
|
|
debug(ctx, "matched: %s\n", drv->name);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
memcpy(card->ops, ops, sizeof(struct sc_card_operations));
|
2001-12-29 02:07:32 +00:00
|
|
|
|
card->driver = drv;
|
2002-01-08 13:56:50 +00:00
|
|
|
|
r = ops->init(card);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
error(ctx, "driver '%s' init() failed: %s\n", drv->name,
|
|
|
|
|
sc_strerror(r));
|
2001-12-29 02:07:32 +00:00
|
|
|
|
if (r == SC_ERROR_INVALID_CARD) {
|
|
|
|
|
card->driver = NULL;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
continue;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
goto err;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
break;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (card->driver == NULL) {
|
2001-12-22 20:43:09 +00:00
|
|
|
|
error(ctx, "unable to find driver for inserted card\n");
|
2002-01-08 13:56:50 +00:00
|
|
|
|
r = SC_ERROR_INVALID_CARD;
|
|
|
|
|
goto err;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
pthread_mutex_init(&card->mutex, NULL);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
card->magic = SC_CARD_MAGIC;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
*card_out = card;
|
|
|
|
|
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, 0);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
err:
|
|
|
|
|
free(card->ops);
|
|
|
|
|
free(card);
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_disconnect_card(struct sc_card *card)
|
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
struct sc_context *ctx;
|
|
|
|
|
assert(sc_card_valid(card));
|
|
|
|
|
ctx = card->ctx;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(ctx, 1);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
assert(card->lock_count == 0);
|
|
|
|
|
if (card->ops->finish) {
|
|
|
|
|
int r = card->ops->finish(card);
|
|
|
|
|
if (r)
|
|
|
|
|
error(card->ctx, "driver finish() failed: %s\n",
|
|
|
|
|
sc_strerror(r));
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SCardDisconnect(card->pcsc_card, SCARD_LEAVE_CARD);
|
|
|
|
|
pthread_mutex_destroy(&card->mutex);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
free(card->ops);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
free(card);
|
|
|
|
|
SC_FUNC_RETURN(ctx, 1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
/* internal lock function -- should make sure that the card is exclusively
|
|
|
|
|
* in our use */
|
2001-12-22 20:43:09 +00:00
|
|
|
|
static int _sc_lock_int(struct sc_card *card)
|
|
|
|
|
{
|
|
|
|
|
long rv;
|
|
|
|
|
|
|
|
|
|
rv = SCardBeginTransaction(card->pcsc_card);
|
|
|
|
|
|
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
error(card->ctx, "SCardBeginTransaction failed: %s\n", pcsc_stringify_error(rv));
|
2001-12-25 20:45:48 +00:00
|
|
|
|
return _sc_pcscret_to_error(rv);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_lock(struct sc_card *card)
|
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
|
pthread_mutex_lock(&card->mutex);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (card->lock_count == 0)
|
|
|
|
|
r = _sc_lock_int(card);
|
|
|
|
|
if (r == 0)
|
|
|
|
|
card->lock_count++;
|
|
|
|
|
pthread_mutex_unlock(&card->mutex);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
/* internal unlock function */
|
2001-12-22 20:43:09 +00:00
|
|
|
|
static int _sc_unlock_int(struct sc_card *card)
|
|
|
|
|
{
|
|
|
|
|
long rv;
|
|
|
|
|
|
|
|
|
|
rv = SCardEndTransaction(card->pcsc_card, SCARD_LEAVE_CARD);
|
|
|
|
|
if (rv != SCARD_S_SUCCESS) {
|
|
|
|
|
error(card->ctx, "SCardEndTransaction failed: %s\n", pcsc_stringify_error(rv));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_unlock(struct sc_card *card)
|
|
|
|
|
{
|
2001-12-25 20:45:48 +00:00
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
pthread_mutex_lock(&card->mutex);
|
|
|
|
|
card->lock_count--;
|
|
|
|
|
assert(card->lock_count >= 0);
|
|
|
|
|
if (card->lock_count == 0)
|
|
|
|
|
r = _sc_unlock_int(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
pthread_mutex_unlock(&card->mutex);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_list_files(struct sc_card *card, u8 *buf, int buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_apdu apdu;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
|
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);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "APDU transmit failed");
|
|
|
|
|
if (apdu.resplen == 0)
|
|
|
|
|
return sc_sw_to_errorcode(card, apdu.sw1, apdu.sw2);
|
|
|
|
|
return apdu.resplen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_create_file(struct sc_card *card, const struct sc_file *file)
|
|
|
|
|
{
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int r;
|
2001-12-22 20:43:09 +00:00
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
assert(card != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 1);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (card->ops->create_file == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
r = card->ops->create_file(card, file);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int sc_delete_file(struct sc_card *card, const struct sc_path *path)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
2002-01-08 13:56:50 +00:00
|
|
|
|
assert(card != NULL);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_CALLED(card->ctx, 1);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (card->ops->delete_file == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
r = card->ops->delete_file(card, path);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 1, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_read_binary(struct sc_card *card, unsigned int idx,
|
2001-12-29 02:07:32 +00:00
|
|
|
|
unsigned char *buf, size_t count, unsigned long flags)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL && card->ops != NULL && buf != NULL);
|
|
|
|
|
if (card->ctx->debug >= 2)
|
|
|
|
|
debug(card->ctx, "sc_read_binary: %d bytes at index %d\n", count, idx);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
if (card->ops->read_binary == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
if (count > SC_APDU_CHOP_SIZE && !(card->caps & SC_CARD_CAP_APDU_EXT)) {
|
2001-12-22 20:43:09 +00:00
|
|
|
|
int bytes_read = 0;
|
|
|
|
|
unsigned char *p = buf;
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
2001-12-22 20:43:09 +00:00
|
|
|
|
while (count > 0) {
|
2002-01-08 13:56:50 +00:00
|
|
|
|
int n = count > SC_APDU_CHOP_SIZE ? SC_APDU_CHOP_SIZE : count;
|
2001-12-29 02:07:32 +00:00
|
|
|
|
r = sc_read_binary(card, idx, p, n, flags);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r < 0) {
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_read_binary() failed");
|
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
p += r;
|
|
|
|
|
idx += r;
|
|
|
|
|
bytes_read += r;
|
|
|
|
|
count -= r;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r == 0) {
|
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_read);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(card);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_read);
|
|
|
|
|
}
|
2001-12-29 02:07:32 +00:00
|
|
|
|
r = card->ops->read_binary(card, idx, buf, count, flags);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2002-01-08 13:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_write_binary(struct sc_card *card, unsigned int idx,
|
|
|
|
|
const u8 *buf, size_t count, unsigned long flags)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL && card->ops != NULL && buf != NULL);
|
|
|
|
|
if (card->ctx->debug >= 2)
|
|
|
|
|
debug(card->ctx, "sc_write_binary: %d bytes at index %d\n", count, idx);
|
|
|
|
|
if (card->ops->write_binary == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
if (count > SC_APDU_CHOP_SIZE && !(card->caps & SC_CARD_CAP_APDU_EXT)) {
|
|
|
|
|
int bytes_written = 0;
|
|
|
|
|
const u8 *p = buf;
|
|
|
|
|
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_lock() failed");
|
|
|
|
|
while (count > 0) {
|
|
|
|
|
int n = count > SC_APDU_CHOP_SIZE ? SC_APDU_CHOP_SIZE : count;
|
|
|
|
|
r = sc_write_binary(card, idx, p, n, flags);
|
|
|
|
|
if (r < 0) {
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_TEST_RET(card->ctx, r, "sc_read_binary() failed");
|
|
|
|
|
}
|
|
|
|
|
p += r;
|
|
|
|
|
idx += r;
|
|
|
|
|
bytes_written += r;
|
|
|
|
|
count -= r;
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sc_unlock(card);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, bytes_written);
|
|
|
|
|
}
|
|
|
|
|
r = card->ops->write_binary(card, idx, buf, count, flags);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_select_file(struct sc_card *card,
|
2001-12-22 23:51:12 +00:00
|
|
|
|
const struct sc_path *in_path,
|
|
|
|
|
struct sc_file *file)
|
2001-12-22 20:43:09 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL && in_path != NULL);
|
|
|
|
|
if (card->ctx->debug >= 2) {
|
|
|
|
|
char line[128], *linep = line;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
|
2001-12-22 20:43:09 +00:00
|
|
|
|
linep += sprintf(linep, "called with type %d, path ", in_path->type);
|
|
|
|
|
for (r = 0; r < in_path->len; r++) {
|
|
|
|
|
sprintf(linep, "%02X", in_path->value[r]);
|
|
|
|
|
linep += 2;
|
|
|
|
|
}
|
|
|
|
|
strcpy(linep, "\n");
|
|
|
|
|
debug(card->ctx, line);
|
|
|
|
|
}
|
|
|
|
|
if (in_path->len > SC_MAX_PATH_SIZE)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);
|
|
|
|
|
if (card->ops->select_file == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
2001-12-22 23:51:12 +00:00
|
|
|
|
r = card->ops->select_file(card, in_path, file);
|
2001-12-22 20:43:09 +00:00
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_get_challenge(struct sc_card *card, u8 *rnd, size_t len)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
|
if (card->ops->get_challenge == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
r = card->ops->get_challenge(card, rnd, len);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
|
|
|
|
}
|
2001-12-25 20:45:48 +00:00
|
|
|
|
|
2001-12-29 02:07:32 +00:00
|
|
|
|
int sc_read_record(struct sc_card *card, unsigned int rec_nr, u8 *buf,
|
|
|
|
|
size_t count, unsigned long flags)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(card != NULL);
|
|
|
|
|
SC_FUNC_CALLED(card->ctx, 2);
|
|
|
|
|
if (card->ops->read_record == NULL)
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_NOT_SUPPORTED);
|
|
|
|
|
r = card->ops->read_record(card, rec_nr, buf, count, flags);
|
|
|
|
|
SC_FUNC_RETURN(card->ctx, 2, r);
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
inline int sc_card_valid(const struct sc_card *card) {
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
assert(card != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
return card->magic == SC_CARD_MAGIC;
|
|
|
|
|
}
|