again, include source files from OpenSC

(unused) dependencies are satisfied with dummy implementations
This commit is contained in:
Frank Morgner
2015-11-02 22:11:01 +01:00
parent 6d91730ab4
commit 437ff70e6f

View File

@@ -22,282 +22,25 @@
#include "libopensc/internal.h"
#ifndef HAVE_SC_APDU_GET_OCTETS
/* copied from opensc/src/libopensc/apdu.c */
/** Calculates the length of the encoded APDU in octets.
* @param apdu the APDU
* @param proto the desired protocol
* @return length of the encoded APDU
*/
static 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;
}
/** Encodes a APDU as an octet string
* @param ctx sc_context_t object (used for logging)
* @param apdu APDU to be encoded as an octet string
* @param proto protocol version to be used
* @param out output buffer of size outlen.
* @param outlen size of hte output buffer
* @return SC_SUCCESS on success and an error code otherwise
*/
static 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;
/* CLA, INS, P1 and P2 */
*p++ = apdu->cla;
*p++ = apdu->ins;
*p++ = apdu->p1;
*p++ = apdu->p2;
/* case depend part */
switch (apdu->cse) {
case SC_APDU_CASE_1:
/* T0 needs an additional 0x00 byte */
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)
/* T0 extended APDUs look just like short APDUs */
*p = (u8)apdu->le;
else {
/* in case of T1 always use 3 bytes for length */
*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) {
/* in case of T0 the command is transmitted in chunks
* < 255 using the ENVELOPE command ... */
if (apdu->lc > 255) {
/* ... so if Lc is greater than 255 bytes
* an error has occurred on a higher level */
sc_log(ctx, "invalid Lc length for CASE 3 extended APDU (need ENVELOPE)");
return SC_ERROR_INVALID_ARGUMENTS;
}
}
else {
/* in case of T1 always use 3 bytes for length */
*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;
/* in case of T0 no Le byte is added */
if (proto != SC_PROTO_T0)
*p = (u8)apdu->le;
break;
case SC_APDU_CASE_4_EXT:
if (proto == SC_PROTO_T0) {
/* again a T0 extended case 4 APDU looks just
* like a short APDU, the additional data is
* transferred using ENVELOPE and GET RESPONSE */
*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;
/* only 2 bytes are use to specify the length of the
* expected data */
*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;
/* get the estimated length of encoded APDU */
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;
/* encode the APDU in the buffer */
if (sc_apdu2bytes(ctx, apdu, proto, nbuf, nlen) != SC_SUCCESS) {
free(nbuf);
return SC_ERROR_INTERNAL;
}
*buf = nbuf;
*len = nlen;
return SC_SUCCESS;
}
#endif
#ifndef HAVE_SC_APDU_SET_RESP
/* copied from opensc/src/libopensc/card.c */
#include <string.h>
int sc_apdu_set_resp(sc_context_t *ctx, sc_apdu_t *apdu, const u8 *buf,
size_t len)
{
if (len < 2) {
/* no SW1 SW2 ... something went terrible wrong */
sc_log(ctx, "invalid response: SW1 SW2 missing");
return SC_ERROR_INTERNAL;
}
/* set the SW1 and SW2 status bytes (the last two bytes of
* the response */
apdu->sw1 = (unsigned int)buf[len - 2];
apdu->sw2 = (unsigned int)buf[len - 1];
len -= 2;
/* set output length and copy the returned data if necessary */
if (len <= apdu->resplen)
apdu->resplen = len;
if (apdu->resplen != 0)
memcpy(apdu->resp, buf, apdu->resplen);
return SC_SUCCESS;
}
#endif
#ifndef HAVE__SC_MATCH_ATR
/* copied from opensc/src/libopensc/card.c */
int sc_dlclose(void *handle) {}
const char *sc_dlerror() {}
void *sc_dlsym(void *handle, const char *symbol) {}
size_t strlcpy(char *dst, const char *src, size_t siz) {}
int sc_mutex_create(const sc_context_t *ctx, void **mutex) {}
int sc_mutex_destroy(const sc_context_t *ctx, void *mutex) {}
int _sc_parse_atr(sc_reader_t *reader) {}
void *sc_dlopen(const char *filename) {}
int sc_mutex_lock(const sc_context_t *ctx, void *mutex) {}
int sc_mutex_unlock(const sc_context_t *ctx, void *mutex) {}
#include "libopensc/card.c"
#endif
static int match_atr_table(sc_context_t *ctx, struct sc_atr_table *table, struct sc_atr *atr)
{
u8 *card_atr_bin;
size_t card_atr_bin_len;
char card_atr_hex[3 * SC_MAX_ATR_SIZE];
size_t card_atr_hex_len;
unsigned int i = 0;
if (ctx == NULL || table == NULL || atr == NULL)
return -1;
card_atr_bin = atr->value;
card_atr_bin_len = atr->len;
sc_bin_to_hex(card_atr_bin, card_atr_bin_len, card_atr_hex, sizeof(card_atr_hex), ':');
card_atr_hex_len = strlen(card_atr_hex);
sc_log(ctx, "ATR : %s", card_atr_hex);
for (i = 0; table[i].atr != NULL; i++) {
const char *tatr = table[i].atr;
const char *matr = table[i].atrmask;
size_t tatr_len = strlen(tatr);
u8 mbin[SC_MAX_ATR_SIZE], tbin[SC_MAX_ATR_SIZE];
size_t mbin_len, tbin_len, s, matr_len;
size_t fix_hex_len = card_atr_hex_len;
size_t fix_bin_len = card_atr_bin_len;
sc_log(ctx, "ATR try : %s", tatr);
if (tatr_len != fix_hex_len) {
sc_log(ctx, "ignored - wrong length");
continue;
}
if (matr != NULL) {
sc_log(ctx, "ATR mask: %s", matr);
matr_len = strlen(matr);
if (tatr_len != matr_len)
continue;
tbin_len = sizeof(tbin);
sc_hex_to_bin(tatr, tbin, &tbin_len);
mbin_len = sizeof(mbin);
sc_hex_to_bin(matr, mbin, &mbin_len);
if (mbin_len != fix_bin_len) {
sc_log(ctx, "length of atr and atr mask do not match - ignored: %s - %s", tatr, matr);
continue;
}
for (s = 0; s < tbin_len; s++) {
/* reduce tatr with mask */
tbin[s] = (tbin[s] & mbin[s]);
/* create copy of card_atr_bin masked) */
mbin[s] = (card_atr_bin[s] & mbin[s]);
}
if (memcmp(tbin, mbin, tbin_len) != 0)
continue;
} else {
if (strncasecmp(tatr, card_atr_hex, tatr_len) != 0)
continue;
}
return i;
}
return -1;
}
int _sc_match_atr(sc_card_t *card, struct sc_atr_table *table, int *type_out)
{
int res;
if (card == NULL)
return -1;
res = match_atr_table(card->ctx, table, &card->atr);
if (res < 0)
return res;
if (type_out != NULL)
*type_out = table[res].type;
return res;
}
#if !defined(HAVE_SC_APDU_GET_OCTETS) || !defined(HAVE_SC_APDU_SET_RESP)
#ifdef HAVE__SC_MATCH_ATR
size_t sc_get_max_send_size(const sc_card_t *card) {}
#endif
#include "libopensc/apdu.c"
#endif
#include <libopensc/log.h>