diff --git a/ccid/pace.c b/ccid/pace.c index 2a938b3..6ad0266 100644 --- a/ccid/pace.c +++ b/ccid/pace.c @@ -854,8 +854,10 @@ int pace_test(sc_card_t *card) sm_apdu.resp = malloc(apdu.resplen); SC_TEST_RET(card->ctx, my_transmit_apdu(card, &sm_apdu), "Could not send SM APDU."); + SC_TEST_RET(card->ctx, sm_check_sw(card, sm_apdu.sw1, sm_apdu.sw2), + "Card returned error."); SC_TEST_RET(card->ctx, sm_decrypt(&sctx, card, &sm_apdu, &apdu), - "Could not decrypt APDU"); + "Could not decrypt APDU."); bin_log(card->ctx, "SM APDU response", sm_apdu.resp, apdu.resplen); return SC_SUCCESS; diff --git a/ccid/sm.c b/ccid/sm.c index 5b94253..dd0f528 100644 --- a/ccid/sm.c +++ b/ccid/sm.c @@ -43,6 +43,25 @@ static const struct sc_asn1_entry c_sm_rapdu[] = { { NULL, 0, 0, 0, NULL, NULL } }; +static const struct sc_card_error sm_errors[] = { + { 0x6987, SC_ERROR_DATA_OBJECT_NOT_FOUND, "Secure Messaging data objects are missing" }, + { 0x6988, SC_ERROR_SECURITY_STATUS_NOT_SATISFIED, "Secure Messaging data objects are incorrect" }, +}; + +int sm_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2) +{ + const int err_count = sizeof(sm_errors)/sizeof(sm_errors[0]); + int i; + + for (i = 0; i < err_count; i++) + if (sm_errors[i].SWs == ((sw1 << 8) | sw2)) { + sc_error(card->ctx, "%s\n", sm_errors[i].errorstr); + return sm_errors[i].errorno; + } + + return sc_check_sw(card, sw1, sw2); +} + void bin_log(sc_context_t *ctx, const char *label, const u8 *data, size_t len) { char buf[1024]; @@ -457,8 +476,6 @@ int sm_decrypt(const struct sm_ctx *ctx, sc_card_t *card, } - - if (sm_rapdu[0].flags & SC_ASN1_PRESENT) { if (ctx->padding_indicator != fdata[0]) { r = SC_ERROR_UNKNOWN_DATA_RECEIVED; diff --git a/ccid/sm.h b/ccid/sm.h index 073649a..7301dc9 100644 --- a/ccid/sm.h +++ b/ccid/sm.h @@ -53,6 +53,8 @@ int sm_encrypt(const struct sm_ctx *ctx, sc_card_t *card, int sm_decrypt(const struct sm_ctx *ctx, sc_card_t *card, const sc_apdu_t *sm_apdu, sc_apdu_t *apdu); +int sm_check_sw(sc_card_t *card, unsigned int sw1, unsigned int sw2); + BUF_MEM * add_iso_pad(const BUF_MEM * m, int block_size); BUF_MEM * add_padding(const struct sm_ctx *ctx, const char *data, size_t datalen);