diff --git a/src/libopensc/card-dnie.c b/src/libopensc/card-dnie.c index 284528a3..6260e81c 100644 --- a/src/libopensc/card-dnie.c +++ b/src/libopensc/card-dnie.c @@ -1406,14 +1406,26 @@ static int dnie_get_challenge(struct sc_card *card, u8 * rnd, size_t len) static int dnie_logout(struct sc_card *card) { int result = SC_SUCCESS; + sc_file_t *file = NULL; if ((card == NULL) || (card->ctx == NULL)) return SC_ERROR_INVALID_ARGUMENTS; + LOG_FUNC_CALLED(card->ctx); - /* disable and free any sm channel related data */ - result = - cwa_create_secure_channel(card, GET_DNIE_PRIV_DATA(card)->cwa_provider, CWA_SM_OFF); - /* TODO: _logout() see comments.txt on what to do here */ + if (card->sm_ctx.sm_mode != SM_MODE_NONE) { + /* mark the channel as closed */ + result = cwa_create_secure_channel(card, + GET_DNIE_PRIV_DATA(card)->cwa_provider, CWA_SM_OFF); + LOG_TEST_RET(card->ctx, result, "Cannot close the secure channel"); + /* request the Master File to provoke an SM error and close the channel */ + result = dnie_compose_and_send_apdu(card, (const u8 *) DNIE_MF_NAME, + sizeof(DNIE_MF_NAME) - 1, 4, &file); + if (result == SC_ERROR_SM) + result = SC_SUCCESS; + } + + if (file != NULL) + sc_file_free(file); LOG_FUNC_RETURN(card->ctx, result); }