diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index 2bc46d3a..0fa2ca7a 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -676,7 +676,7 @@ static int piv_select_aid(sc_card_t* card, u8* aid, size_t aidlen, u8* response, SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Got args: aid=%x, aidlen=%d, response=%x, responselen=%d\n", - aid, aidlen, response, *responselen); + aid, aidlen, response, responselen ? *responselen : 0); sc_format_apdu(card, &apdu, response == NULL ? SC_APDU_CASE_3_SHORT : SC_APDU_CASE_4_SHORT, 0xA4, 0x04, 0x00); @@ -684,11 +684,12 @@ static int piv_select_aid(sc_card_t* card, u8* aid, size_t aidlen, u8* response, apdu.data = aid; apdu.datalen = aidlen; apdu.resp = response; - apdu.resplen = *responselen; + apdu.resplen = responselen ? *responselen : 0; apdu.le = response == NULL ? 0 : 256; /* could be 21 for fci */ r = sc_transmit_apdu(card, &apdu); - *responselen = apdu.resplen; + if (responselen) + *responselen = apdu.resplen; SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, r, "PIV select failed"); SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2)); } @@ -2103,9 +2104,17 @@ static int piv_select_file(sc_card_t *card, const sc_path_t *in_path, /* only support single EF in current application */ - if (pathlen > 2 && memcmp(path, "\x3F\x00", 2) == 0) { - path += 2; - pathlen -= 2; + if (memcmp(path, "\x3F\x00", 2) == 0) { + if (pathlen == 2) { + r = piv_select_aid(card, piv_aids[0].value, piv_aids[0].len_short, NULL, NULL); + SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, r, "Cannot select PIV AID"); + + SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_NORMAL, r); + } + else if (pathlen > 2) { + path += 2; + pathlen -= 2; + } } i = piv_find_obj_by_containerid(card, path);