- sm.c (sm_encrypt) fixed header of sm apdu
- pace.c (EstablishPACEChannel) Fixed initialisation of authentication_ctx. (pace_test) Fixed usage of SC_TEST_RET and initialisation of select apdu for EF.CardSecurity. git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@55 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
40
ccid/pace.c
40
ccid/pace.c
@@ -749,7 +749,7 @@ int EstablishPACEChannel(sc_card_t *card, const __u8 *in,
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
sctx->authenticate = pace_sm_authenticate;
|
sctx->authenticate = pace_sm_authenticate;
|
||||||
sctx->cipher_ctx = sctx->authenticate;
|
sctx->cipher_ctx = sctx->authentication_ctx;
|
||||||
sctx->encrypt = pace_sm_encrypt;
|
sctx->encrypt = pace_sm_encrypt;
|
||||||
sctx->decrypt = pace_sm_decrypt;
|
sctx->decrypt = pace_sm_decrypt;
|
||||||
sctx->padding_indicator = SM_ISO_PADDING;
|
sctx->padding_indicator = SM_ISO_PADDING;
|
||||||
@@ -829,12 +829,12 @@ int pace_test(sc_card_t *card)
|
|||||||
in[9] = 0; // length_cert_desc
|
in[9] = 0; // length_cert_desc
|
||||||
in[10]= 0; // length_cert_desc
|
in[10]= 0; // length_cert_desc
|
||||||
|
|
||||||
SC_TEST_RET(card->ctx, SC_LOG_TYPE_ERROR,
|
SC_TEST_RET(card->ctx, EstablishPACEChannel(card, in, &out, &outlen, &sctx),
|
||||||
EstablishPACEChannel(card, in, &out, &outlen, &sctx));
|
"Could not establish PACE channel.");
|
||||||
|
|
||||||
/* select CardSecurity */
|
/* select CardSecurity */
|
||||||
in[0] = 0x00;
|
apdu.cla = 0x00;
|
||||||
in[1] = 0xA4;
|
apdu.ins = 0xA4;
|
||||||
apdu.p1 = 0x08;
|
apdu.p1 = 0x08;
|
||||||
apdu.p2 = 0x00;
|
apdu.p2 = 0x00;
|
||||||
apdu.lc = 0x02;
|
apdu.lc = 0x02;
|
||||||
@@ -843,12 +843,18 @@ int pace_test(sc_card_t *card)
|
|||||||
apdu.data = in;
|
apdu.data = in;
|
||||||
apdu.datalen = 2;
|
apdu.datalen = 2;
|
||||||
apdu.le = 0x00;
|
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, reset_ssc(&sctx),
|
||||||
SC_TEST_RET(card->ctx, SC_LOG_TYPE_ERROR,
|
"Could not reset send sequence counter.");
|
||||||
my_transmit_apdu(card, &sm_apdu));
|
SC_TEST_RET(card->ctx, sm_encrypt(&sctx, card, &apdu, &sm_apdu),
|
||||||
sm_decrypt(&sctx, card, &sm_apdu, &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;
|
return SC_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -971,7 +977,7 @@ reset_ssc(struct sm_ctx *ctx)
|
|||||||
struct pace_sm_ctx *psmctx = ctx->cipher_ctx;
|
struct pace_sm_ctx *psmctx = ctx->cipher_ctx;
|
||||||
psmctx->ssc = 0;
|
psmctx->ssc = 0;
|
||||||
|
|
||||||
return SC_SUCCESS;
|
return increment_ssc(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
int pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *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;
|
BUF_MEM *databuf = NULL, *macbuf = NULL;
|
||||||
u8 *p = NULL;
|
u8 *p = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
size_t oldlen;
|
||||||
|
|
||||||
if (!ctx || !ctx->cipher_ctx) {
|
if (!ctx || !ctx->cipher_ctx) {
|
||||||
r = SC_ERROR_INVALID_ARGUMENTS;
|
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;
|
struct pace_sm_ctx *psmctx = ctx->cipher_ctx;
|
||||||
|
|
||||||
databuf = encoded_ssc(psmctx->ssc, psmctx->ctx);
|
databuf = encoded_ssc(psmctx->ssc, psmctx->ctx);
|
||||||
if (!databuf ||
|
if (!databuf) {
|
||||||
!BUF_MEM_grow(databuf, databuf->length + datalen)) {
|
|
||||||
r = SC_ERROR_INTERNAL;
|
r = SC_ERROR_INTERNAL;
|
||||||
goto err;
|
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,
|
macbuf = PACE_authenticate(psmctx->ctx, psmctx->protocol,
|
||||||
psmctx->key_mac, databuf);
|
psmctx->key_mac, databuf);
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ pace_sm_ctx_free(struct pace_sm_ctx *ctx);
|
|||||||
void
|
void
|
||||||
pace_sm_ctx_clear_free(struct pace_sm_ctx *ctx);
|
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,
|
int pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *ctx,
|
||||||
const u8 *data, size_t datalen, u8 **enc);
|
const u8 *data, size_t datalen, u8 **enc);
|
||||||
int pace_sm_decrypt(sc_card_t *card, const struct sm_ctx *ctx,
|
int pace_sm_decrypt(sc_card_t *card, const struct sm_ctx *ctx,
|
||||||
|
|||||||
37
ccid/sm.c
37
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)
|
const sc_apdu_t *apdu, sc_apdu_t *sm_apdu)
|
||||||
{
|
{
|
||||||
struct sc_asn1_entry sm_capdu[4];
|
struct sc_asn1_entry sm_capdu[4];
|
||||||
u8 *le = NULL;
|
u8 *le = NULL, *sm_data = NULL;
|
||||||
size_t oldlen;
|
size_t oldlen, sm_data_len;
|
||||||
BUF_MEM *fdata = NULL, *sm_data = NULL,
|
BUF_MEM *fdata = NULL, *mac_data = NULL, *mac = NULL, *tmp = NULL;
|
||||||
*mac_data = NULL, *mac = NULL, *tmp = NULL;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (!apdu || !ctx || !card || !card->slot || !sm_apdu) {
|
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);
|
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);
|
mac_data = format_head(ctx, apdu);
|
||||||
if (!mac_data) {
|
if (!mac_data) {
|
||||||
sc_error(card->ctx, "Could not format header of SM apdu.");
|
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 */
|
/* format SM apdu */
|
||||||
sm_data = BUF_MEM_new();
|
r = sc_asn1_encode(card->ctx, sm_capdu, (u8 **) &sm_data, &sm_data_len);
|
||||||
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);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto err;
|
goto err;
|
||||||
memcpy(sm_apdu, apdu, sizeof *sm_apdu);
|
sm_apdu->data = sm_data;
|
||||||
sm_apdu->cla = 0x0C;
|
sm_apdu->datalen = sm_data_len;
|
||||||
sm_apdu->data = (u8 *) sm_data->data;
|
sm_apdu->lc = sm_apdu->datalen;
|
||||||
sm_apdu->lc = sm_data->length;
|
|
||||||
sm_apdu->le = 0;
|
sm_apdu->le = 0;
|
||||||
sm_apdu->cse = SC_APDU_CASE_4;
|
sm_apdu->cse = SC_APDU_CASE_4;
|
||||||
|
bin_log(card->ctx, "sm apdu data", sm_apdu->data, sm_apdu->datalen);
|
||||||
|
printf("%s:%d\n", __FILE__, __LINE__);
|
||||||
/* BUF_MEM_free must not free sm_data->data */
|
|
||||||
sm_data->data = NULL;
|
|
||||||
sm_data->length = 0;
|
|
||||||
sm_data->max = 0;
|
|
||||||
|
|
||||||
err:
|
err:
|
||||||
if (fdata) {
|
if (fdata) {
|
||||||
@@ -399,9 +393,6 @@ err:
|
|||||||
if (mac) {
|
if (mac) {
|
||||||
BUF_MEM_free(mac);
|
BUF_MEM_free(mac);
|
||||||
}
|
}
|
||||||
if (sm_data) {
|
|
||||||
BUF_MEM_free(sm_data);
|
|
||||||
}
|
|
||||||
if (le) {
|
if (le) {
|
||||||
free(le);
|
free(le);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user