#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "internal.h"
#include "asn1.h"
size_t sc_apdu_get_length(const sc_apdu_t *apdu, unsigned int proto)
{
size_t ret = 4;
switch (apdu->cse) {
case SC_APDU_CASE_1:
if (proto == SC_PROTO_T0)
ret++;
break;
case SC_APDU_CASE_2_SHORT:
ret++;
break;
case SC_APDU_CASE_2_EXT:
ret += (proto == SC_PROTO_T0 ? 1 : 3);
break;
case SC_APDU_CASE_3_SHORT:
ret += 1 + apdu->lc;
break;
case SC_APDU_CASE_3_EXT:
ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 3);
break;
case SC_APDU_CASE_4_SHORT:
ret += apdu->lc + (proto != SC_PROTO_T0 ? 2 : 1);
break;
case SC_APDU_CASE_4_EXT:
ret += apdu->lc + (proto == SC_PROTO_T0 ? 1 : 5);
break;
default:
return 0;
}
return ret;
}
int sc_apdu2bytes(sc_context_t *ctx, const sc_apdu_t *apdu,
unsigned int proto, u8 *out, size_t outlen)
{
u8 *p = out;
size_t len = sc_apdu_get_length(apdu, proto);
if (out == NULL || outlen < len)
return SC_ERROR_INVALID_ARGUMENTS;
*p++ = apdu->cla;
*p++ = apdu->ins;
*p++ = apdu->p1;
*p++ = apdu->p2;
switch (apdu->cse) {
case SC_APDU_CASE_1:
if (proto == SC_PROTO_T0)
*p = (u8)0x00;
break;
case SC_APDU_CASE_2_SHORT:
*p = (u8)apdu->le;
break;
case SC_APDU_CASE_2_EXT:
if (proto == SC_PROTO_T0)
*p = (u8)apdu->le;
else {
*p++ = (u8)0x00;
*p++ = (u8)(apdu->le >> 8);
*p = (u8)apdu->le;
}
break;
case SC_APDU_CASE_3_SHORT:
*p++ = (u8)apdu->lc;
memcpy(p, apdu->data, apdu->lc);
break;
case SC_APDU_CASE_3_EXT:
if (proto == SC_PROTO_T0) {
if (apdu->lc > 255) {
sc_log(ctx, "invalid Lc length for CASE 3 extended APDU (need ENVELOPE)");
return SC_ERROR_INVALID_ARGUMENTS;
}
}
else {
*p++ = (u8)0x00;
*p++ = (u8)(apdu->lc >> 8);
*p++ = (u8)apdu->lc;
}
memcpy(p, apdu->data, apdu->lc);
break;
case SC_APDU_CASE_4_SHORT:
*p++ = (u8)apdu->lc;
memcpy(p, apdu->data, apdu->lc);
p += apdu->lc;
if (proto != SC_PROTO_T0)
*p = (u8)apdu->le;
break;
case SC_APDU_CASE_4_EXT:
if (proto == SC_PROTO_T0) {
*p++ = (u8)apdu->lc;
memcpy(p, apdu->data, apdu->lc);
}
else {
*p++ = (u8)0x00;
*p++ = (u8)(apdu->lc >> 8);
*p++ = (u8)apdu->lc;
memcpy(p, apdu->data, apdu->lc);
p += apdu->lc;
*p++ = (u8)(apdu->le >> 8);
*p = (u8)apdu->le;
}
break;
}
return SC_SUCCESS;
}
int sc_apdu_get_octets(sc_context_t *ctx, const sc_apdu_t *apdu, u8 **buf,
size_t *len, unsigned int proto)
{
size_t nlen;
u8 *nbuf;
if (apdu == NULL || buf == NULL || len == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
nlen = sc_apdu_get_length(apdu, proto);
if (nlen == 0)
return SC_ERROR_INTERNAL;
nbuf = malloc(nlen);
if (nbuf == NULL)
return SC_ERROR_OUT_OF_MEMORY;
if (sc_apdu2bytes(ctx, apdu, proto, nbuf, nlen) != SC_SUCCESS) {
free(nbuf);
return SC_ERROR_INTERNAL;
}
*buf = nbuf;
*len = nlen;
return SC_SUCCESS;
}
int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf,
size_t len)
{
if (len < 2) {
sc_log(ctx, "invalid response: SW1 SW2 missing");
return SC_ERROR_INTERNAL;
}
apdu->sw1 = (unsigned int)buf[len - 2];
apdu->sw2 = (unsigned int)buf[len - 1];
len -= 2;
if (len <= apdu->resplen)
apdu->resplen = len;
if (apdu->resplen != 0)
memcpy(apdu->resp, buf, apdu->resplen);
return SC_SUCCESS;
}
int
sc_check_apdu(sc_card_t *card, const sc_apdu_t *apdu)
{
if ((apdu->cse & ~SC_APDU_SHORT_MASK) == 0) {
if (apdu->le > 256 || (apdu->lc > 255 && (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0)) {
sc_log(card->ctx, "failed length check for short APDU");
goto error;
}
}
else if ((apdu->cse & SC_APDU_EXT) != 0) {
if ((card->caps & SC_CARD_CAP_APDU_EXT) == 0) {
sc_log(card->ctx, "card doesn't support extended APDUs");
goto error;
}
if (apdu->le > 65536 || apdu->lc > 65535) {
sc_log(card->ctx, "failed length check for extended APDU");
goto error;
}
}
else {
goto error;
}
switch (apdu->cse & SC_APDU_SHORT_MASK) {
case SC_APDU_CASE_1:
if (apdu->datalen != 0 || apdu->lc != 0 || apdu->le != 0)
goto error;
break;
case SC_APDU_CASE_2_SHORT:
if (apdu->datalen != 0 || apdu->lc != 0)
goto error;
if (apdu->resplen == 0 || apdu->resp == NULL)
goto error;
break;
case SC_APDU_CASE_3_SHORT:
if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0)
goto error;
if (apdu->le != 0)
goto error;
if (apdu->datalen != apdu->lc)
goto error;
break;
case SC_APDU_CASE_4_SHORT:
if (apdu->datalen == 0 || apdu->data == NULL || apdu->lc == 0)
goto error;
if (apdu->resplen == 0 || apdu->resp == NULL)
goto error;
if (apdu->datalen != apdu->lc)
goto error;
break;
default:
sc_log(card->ctx, "Invalid APDU case %d", apdu->cse);
return SC_ERROR_INVALID_ARGUMENTS;
}
return SC_SUCCESS;
error:
sc_log(card->ctx, "Invalid Case %d %s APDU:\n"
"cse=%02x cla=%02x ins=%02x p1=%02x p2=%02x lc=%lu le=%lu\n"
"resp=%p resplen=%lu data=%p datalen=%lu",
apdu->cse & SC_APDU_SHORT_MASK,
(apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
apdu->cse, apdu->cla, apdu->ins, apdu->p1, apdu->p2,
(unsigned long) apdu->lc, (unsigned long) apdu->le,
apdu->resp, (unsigned long) apdu->resplen,
apdu->data, (unsigned long) apdu->datalen);
return SC_ERROR_INVALID_ARGUMENTS;
}
static void
sc_detect_apdu_cse(const sc_card_t *card, sc_apdu_t *apdu)
{
if (apdu->cse == SC_APDU_CASE_2 || apdu->cse == SC_APDU_CASE_3 ||
apdu->cse == SC_APDU_CASE_4) {
int btype = apdu->cse & SC_APDU_SHORT_MASK;
if ((apdu->le > 256 || (apdu->lc > 255 && (apdu->flags & SC_APDU_FLAGS_CHAINING) == 0)) &&
(card->caps & SC_CARD_CAP_APDU_EXT) != 0)
btype |= SC_APDU_EXT;
apdu->cse = btype;
}
}
static int
sc_single_transmit(struct sc_card *card, struct sc_apdu *apdu)
{
struct sc_context *ctx = card->ctx;
int rv;
LOG_FUNC_CALLED(ctx);
if (card->reader->ops->transmit == NULL)
LOG_TEST_RET(card->ctx, SC_ERROR_NOT_SUPPORTED, "cannot transmit APDU");
sc_log(ctx,
"CLA:%X, INS:%X, P1:%X, P2:%X, data(%"SC_FORMAT_LEN_SIZE_T"u) %p",
apdu->cla, apdu->ins, apdu->p1, apdu->p2, apdu->datalen,
apdu->data);
#ifdef ENABLE_SM
if (card->sm_ctx.sm_mode == SM_MODE_TRANSMIT
&& (apdu->flags & SC_APDU_FLAGS_NO_SM) == 0) {
LOG_FUNC_RETURN(ctx, sc_sm_single_transmit(card, apdu));
}
#endif
rv = card->reader->ops->transmit(card->reader, apdu);
LOG_TEST_RET(ctx, rv, "unable to transmit APDU");
LOG_FUNC_RETURN(ctx, rv);
}
static int
sc_set_le_and_transmit(struct sc_card *card, struct sc_apdu *apdu, size_t olen)
{
struct sc_context *ctx = card->ctx;
size_t nlen = apdu->sw2 ? (size_t)apdu->sw2 : 256;
int rv;
LOG_FUNC_CALLED(ctx);
if (olen < nlen)
LOG_TEST_RET(ctx, SC_ERROR_WRONG_LENGTH, "wrong length: required length exceeds resplen");
apdu->flags |= SC_APDU_FLAGS_NO_RETRY_WL;
apdu->resplen = olen;
apdu->le = nlen;
if (card->type == SC_CARD_TYPE_BELPIC_EID)
msleep(40);
rv = sc_single_transmit(card, apdu);
LOG_TEST_RET(ctx, rv, "cannot re-transmit APDU");
LOG_FUNC_RETURN(ctx, rv);
}
static int
sc_get_response(struct sc_card *card, struct sc_apdu *apdu, size_t olen)
{
struct sc_context *ctx = card->ctx;
size_t le, minlen, buflen;
unsigned char *buf;
int rv;
LOG_FUNC_CALLED(ctx);
if (apdu->le == 0) {
apdu->sw1 = 0x90;
apdu->sw2 = 0x00;
return SC_SUCCESS;
}
if (!card->ops->get_response)
LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "no GET RESPONSE command");
buf = apdu->resp + apdu->resplen;
buflen = olen - apdu->resplen;
le = apdu->sw2 != 0 ? (size_t)apdu->sw2 : 256;
minlen = le;
do {
unsigned char resp[256];
size_t resp_len = le;
memset(resp, 0, sizeof(resp));
rv = card->ops->get_response(card, &resp_len, resp);
if (rv < 0) {
#ifdef ENABLE_SM
if (resp_len) {
sc_log_hex(ctx, "SM response data", resp, resp_len);
sc_sm_update_apdu_response(card, resp, resp_len, rv, apdu);
}
#endif
LOG_TEST_RET(ctx, rv, "GET RESPONSE error");
}
le = resp_len;
if (buflen < le)
le = buflen;
memcpy(buf, resp, le);
buf += le;
buflen -= le;
if (buflen == 0)
break;
minlen -= le;
if (rv != 0)
le = minlen = (size_t)rv;
else
le = minlen;
} while (rv != 0 && minlen != 0);
apdu->resplen = buf - apdu->resp;
apdu->sw1 = 0x90;
apdu->sw2 = 0x00;
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
static int
sc_transmit(sc_card_t *card, sc_apdu_t *apdu)
{
struct sc_context *ctx = card->ctx;
size_t olen = apdu->resplen;
int r;
LOG_FUNC_CALLED(ctx);
r = sc_single_transmit(card, apdu);
LOG_TEST_RET(ctx, r, "transmit APDU failed");
if (apdu->sw1 == 0x6C && (apdu->flags & SC_APDU_FLAGS_NO_RETRY_WL) == 0)
r = sc_set_le_and_transmit(card, apdu, olen);
LOG_TEST_RET(ctx, r, "cannot re-transmit APDU ");
if (apdu->sw1 == 0x61 && (apdu->flags & SC_APDU_FLAGS_NO_GET_RESP) == 0)
r = sc_get_response(card, apdu, olen);
LOG_TEST_RET(ctx, r, "cannot get all data with 'GET RESPONSE'");
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu)
{
int r = SC_SUCCESS;
if (card == NULL || apdu == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
LOG_FUNC_CALLED(card->ctx);
sc_detect_apdu_cse(card, apdu);
r = sc_check_apdu(card, apdu);
if (r != SC_SUCCESS)
return SC_ERROR_INVALID_ARGUMENTS;
r = sc_lock(card);
if (r != SC_SUCCESS) {
sc_log(card->ctx, "unable to acquire lock");
return r;
}
if ((apdu->flags & SC_APDU_FLAGS_CHAINING) != 0) {
size_t len = apdu->datalen;
const u8 *buf = apdu->data;
size_t max_send_size = sc_get_max_send_size(card);
while (len != 0) {
size_t plen;
sc_apdu_t tapdu;
int last = 0;
tapdu = *apdu;
tapdu.flags &= ~SC_APDU_FLAGS_CHAINING;
if (len > max_send_size) {
if ((tapdu.cse & SC_APDU_SHORT_MASK) == SC_APDU_CASE_4_SHORT)
tapdu.cse--;
plen = max_send_size;
tapdu.cla |= 0x10;
tapdu.le = 0;
tapdu.lc = 0;
tapdu.resplen = 0;
tapdu.resp = NULL;
} else {
plen = len;
last = 1;
}
tapdu.data = buf;
tapdu.datalen = tapdu.lc = plen;
r = sc_check_apdu(card, &tapdu);
if (r != SC_SUCCESS) {
sc_log(card->ctx, "inconsistent APDU while chaining");
break;
}
r = sc_transmit(card, &tapdu);
if (r != SC_SUCCESS)
break;
if (last != 0) {
apdu->sw1 = tapdu.sw1;
apdu->sw2 = tapdu.sw2;
apdu->resplen = tapdu.resplen;
} else {
r = sc_check_sw(card, tapdu.sw1, tapdu.sw2);
if (r != SC_SUCCESS)
break;
}
len -= plen;
buf += plen;
}
} else {
r = sc_transmit(card, apdu);
}
if (r == SC_ERROR_CARD_RESET || r == SC_ERROR_READER_REATTACHED) {
sc_invalidate_cache(card);
if (card->ops->card_reader_lock_obtained)
card->ops->card_reader_lock_obtained(card, 1);
}
if (sc_unlock(card) != SC_SUCCESS)
sc_log(card->ctx, "sc_unlock failed");
return r;
}
int
sc_bytes2apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu)
{
const unsigned char *p;
size_t len0;
if (!buf || !apdu)
return SC_ERROR_INVALID_ARGUMENTS;
len0 = len;
if (len < 4) {
sc_log(ctx, "APDU too short (must be at least 4 bytes)");
return SC_ERROR_INVALID_DATA;
}
memset(apdu, 0, sizeof *apdu);
p = buf;
apdu->cla = *p++;
apdu->ins = *p++;
apdu->p1 = *p++;
apdu->p2 = *p++;
len -= 4;
if (!len) {
apdu->cse = SC_APDU_CASE_1;
sc_log(ctx,
"CASE_1 APDU: %"SC_FORMAT_LEN_SIZE_T"u bytes:\tins=%02x p1=%02x p2=%02x lc=%04"SC_FORMAT_LEN_SIZE_T"x le=%04"SC_FORMAT_LEN_SIZE_T"x",
len0, apdu->ins, apdu->p1, apdu->p2, apdu->lc, apdu->le);
return SC_SUCCESS;
}
if (*p == 0 && len >= 3) {
p++;
if (len == 3) {
apdu->le = (*p++)<<8;
apdu->le += *p++;
if (apdu->le == 0)
apdu->le = 0xffff+1;
len -= 3;
apdu->cse = SC_APDU_CASE_2_EXT;
}
else {
apdu->lc = (*p++)<<8;
apdu->lc += *p++;
len -= 3;
if (len < apdu->lc) {
sc_log(ctx,
"APDU too short (need %"SC_FORMAT_LEN_SIZE_T"u more bytes)",
apdu->lc - len);
return SC_ERROR_INVALID_DATA;
}
apdu->data = p;
apdu->datalen = apdu->lc;
len -= apdu->lc;
p += apdu->lc;
if (!len) {
apdu->cse = SC_APDU_CASE_3_EXT;
}
else {
if (len < 2) {
sc_debug(ctx, SC_LOG_DEBUG_VERBOSE, "APDU too short (need 2 more bytes)\n");
return SC_ERROR_INVALID_DATA;
}
apdu->le = (*p++)<<8;
apdu->le += *p++;
if (apdu->le == 0)
apdu->le = 0xffff+1;
len -= 2;
apdu->cse = SC_APDU_CASE_4_EXT;
}
}
}
else {
if (len == 1) {
apdu->le = *p++;
if (apdu->le == 0)
apdu->le = 0xff+1;
len--;
apdu->cse = SC_APDU_CASE_2_SHORT;
}
else {
apdu->lc = *p++;
len--;
if (len < apdu->lc) {
sc_log(ctx,
"APDU too short (need %"SC_FORMAT_LEN_SIZE_T"u more bytes)",
apdu->lc - len);
return SC_ERROR_INVALID_DATA;
}
apdu->data = p;
apdu->datalen = apdu->lc;
len -= apdu->lc;
p += apdu->lc;
if (!len) {
apdu->cse = SC_APDU_CASE_3_SHORT;
}
else {
apdu->le = *p++;
if (apdu->le == 0)
apdu->le = 0xff+1;
len--;
apdu->cse = SC_APDU_CASE_4_SHORT;
}
}
}
if (len) {
sc_log(ctx, "APDU too long (%lu bytes extra)",(unsigned long) len);
return SC_ERROR_INVALID_DATA;
}
sc_log(ctx,
"Case %d %s APDU, %"SC_FORMAT_LEN_SIZE_T"u bytes:\tins=%02x p1=%02x p2=%02x lc=%04"SC_FORMAT_LEN_SIZE_T"x le=%04"SC_FORMAT_LEN_SIZE_T"x",
apdu->cse & SC_APDU_SHORT_MASK,
(apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
len0, apdu->ins, apdu->p1, apdu->p2, apdu->lc,
apdu->le);
return SC_SUCCESS;
}