diff --git a/ccid/pace.c b/ccid/pace.c index ea4d1a9..e8c5dd1 100644 --- a/ccid/pace.c +++ b/ccid/pace.c @@ -749,7 +749,7 @@ int EstablishPACEChannel(sc_card_t *card, const __u8 *in, goto err; } sctx->authenticate = pace_sm_authenticate; - sctx->cipher_ctx = sctx->authenticate; + sctx->cipher_ctx = sctx->authentication_ctx; sctx->encrypt = pace_sm_encrypt; sctx->decrypt = pace_sm_decrypt; sctx->padding_indicator = SM_ISO_PADDING; @@ -829,12 +829,12 @@ int pace_test(sc_card_t *card) in[9] = 0; // length_cert_desc in[10]= 0; // length_cert_desc - SC_TEST_RET(card->ctx, SC_LOG_TYPE_ERROR, - EstablishPACEChannel(card, in, &out, &outlen, &sctx)); + SC_TEST_RET(card->ctx, EstablishPACEChannel(card, in, &out, &outlen, &sctx), + "Could not establish PACE channel."); /* select CardSecurity */ - in[0] = 0x00; - in[1] = 0xA4; + apdu.cla = 0x00; + apdu.ins = 0xA4; apdu.p1 = 0x08; apdu.p2 = 0x00; apdu.lc = 0x02; @@ -843,12 +843,18 @@ int pace_test(sc_card_t *card) apdu.data = in; apdu.datalen = 2; apdu.le = 0x00; - apdu.lc = SC_APDU_CASE_4_SHORT; + apdu.cse = SC_APDU_CASE_4_SHORT; - sm_encrypt(&sctx, card, &apdu, &sm_apdu); - SC_TEST_RET(card->ctx, SC_LOG_TYPE_ERROR, - my_transmit_apdu(card, &sm_apdu)); - sm_decrypt(&sctx, card, &sm_apdu, &apdu); + SC_TEST_RET(card->ctx, reset_ssc(&sctx), + "Could not reset send sequence counter."); + SC_TEST_RET(card->ctx, sm_encrypt(&sctx, card, &apdu, &sm_apdu), + "Could not encrypt APDU."); + bin_log(card->ctx, "SM APDU data", sm_apdu.data, sm_apdu.datalen); + SC_TEST_RET(card->ctx, my_transmit_apdu(card, &sm_apdu), + "Could not send SM APDU."); + SC_TEST_RET(card->ctx, sm_decrypt(&sctx, card, &sm_apdu, &apdu), + "Could not decrypt APDU"); + bin_log(card->ctx, "SM APDU response", sm_apdu.resp, apdu.resplen); return SC_SUCCESS; } @@ -971,7 +977,7 @@ reset_ssc(struct sm_ctx *ctx) struct pace_sm_ctx *psmctx = ctx->cipher_ctx; psmctx->ssc = 0; - return SC_SUCCESS; + return increment_ssc(ctx); } int pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *ctx, @@ -1069,6 +1075,7 @@ int pace_sm_authenticate(sc_card_t *card, const struct sm_ctx *ctx, BUF_MEM *databuf = NULL, *macbuf = NULL; u8 *p = NULL; int r; + size_t oldlen; if (!ctx || !ctx->cipher_ctx) { r = SC_ERROR_INVALID_ARGUMENTS; @@ -1077,12 +1084,17 @@ int pace_sm_authenticate(sc_card_t *card, const struct sm_ctx *ctx, struct pace_sm_ctx *psmctx = ctx->cipher_ctx; databuf = encoded_ssc(psmctx->ssc, psmctx->ctx); - if (!databuf || - !BUF_MEM_grow(databuf, databuf->length + datalen)) { + if (!databuf) { r = SC_ERROR_INTERNAL; goto err; } - memcpy(databuf->data + databuf->length, data, datalen); + oldlen = databuf->length; + if (!BUF_MEM_grow(databuf, oldlen + datalen)) { + r = SC_ERROR_INTERNAL; + goto err; + } + memcpy(databuf->data + oldlen, data, datalen); + databuf->length = oldlen + datalen; macbuf = PACE_authenticate(psmctx->ctx, psmctx->protocol, psmctx->key_mac, databuf); diff --git a/ccid/pace.h b/ccid/pace.h index 88d600f..2b7455b 100644 --- a/ccid/pace.h +++ b/ccid/pace.h @@ -58,6 +58,9 @@ pace_sm_ctx_free(struct pace_sm_ctx *ctx); void pace_sm_ctx_clear_free(struct pace_sm_ctx *ctx); +int reset_ssc(struct sm_ctx *ctx); +int increment_ssc(struct sm_ctx *ctx); + int pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *ctx, const u8 *data, size_t datalen, u8 **enc); int pace_sm_decrypt(sc_card_t *card, const struct sm_ctx *ctx, diff --git a/ccid/sm.c b/ccid/sm.c index 183bc4a..a44eb01 100644 --- a/ccid/sm.c +++ b/ccid/sm.c @@ -222,10 +222,9 @@ int sm_encrypt(const struct sm_ctx *ctx, sc_card_t *card, const sc_apdu_t *apdu, sc_apdu_t *sm_apdu) { struct sc_asn1_entry sm_capdu[4]; - u8 *le = NULL; - size_t oldlen; - BUF_MEM *fdata = NULL, *sm_data = NULL, - *mac_data = NULL, *mac = NULL, *tmp = NULL; + u8 *le = NULL, *sm_data = NULL; + size_t oldlen, sm_data_len; + BUF_MEM *fdata = NULL, *mac_data = NULL, *mac = NULL, *tmp = NULL; int r; if (!apdu || !ctx || !card || !card->slot || !sm_apdu) { @@ -235,6 +234,11 @@ int sm_encrypt(const struct sm_ctx *ctx, sc_card_t *card, sc_copy_asn1_entry(c_sm_capdu, sm_capdu); + sm_apdu->flags = apdu->flags; + sm_apdu->cla = 0x0C; + sm_apdu->ins = apdu->ins; + sm_apdu->p1 = apdu->p1; + sm_apdu->p2 = apdu->p2; mac_data = format_head(ctx, apdu); if (!mac_data) { sc_error(card->ctx, "Could not format header of SM apdu."); @@ -365,26 +369,16 @@ int sm_encrypt(const struct sm_ctx *ctx, sc_card_t *card, /* format SM apdu */ - sm_data = BUF_MEM_new(); - if (!sm_data) { - r = SC_ERROR_OUT_OF_MEMORY; - goto err; - } - r = sc_asn1_encode(card->ctx, sm_capdu, (u8 **) &sm_data->data, &sm_data->length); + r = sc_asn1_encode(card->ctx, sm_capdu, (u8 **) &sm_data, &sm_data_len); if (r < 0) goto err; - memcpy(sm_apdu, apdu, sizeof *sm_apdu); - sm_apdu->cla = 0x0C; - sm_apdu->data = (u8 *) sm_data->data; - sm_apdu->lc = sm_data->length; + sm_apdu->data = sm_data; + sm_apdu->datalen = sm_data_len; + sm_apdu->lc = sm_apdu->datalen; sm_apdu->le = 0; sm_apdu->cse = SC_APDU_CASE_4; - - - /* BUF_MEM_free must not free sm_data->data */ - sm_data->data = NULL; - sm_data->length = 0; - sm_data->max = 0; + bin_log(card->ctx, "sm apdu data", sm_apdu->data, sm_apdu->datalen); + printf("%s:%d\n", __FILE__, __LINE__); err: if (fdata) { @@ -399,9 +393,6 @@ err: if (mac) { BUF_MEM_free(mac); } - if (sm_data) { - BUF_MEM_free(sm_data); - } if (le) { free(le); }