@@ -982,6 +982,11 @@ static ykpiv_rc _ykpiv_authenticate2(ykpiv_state *state, unsigned const char *ke
982982
983983 uint8_t * challenge = data + 4 ;
984984 uint32_t challenge_len = recv_len - 4 ;
985+ if (challenge_len != cipher_blocksize (mgm_key )) { // Only management key block size allowed
986+ DBG ("%s: management key block size is %zu but received %u bytes challenge" , ykpiv_strerror (YKPIV_PARSE_ERROR ), cipher_blocksize (mgm_key ), challenge_len );
987+ res = YKPIV_PARSE_ERROR ;
988+ goto Cleanup ;
989+ }
985990
986991 /* send a response to the cards challenge and a challenge of our own. */
987992 {
@@ -1007,26 +1012,23 @@ static ykpiv_rc _ykpiv_authenticate2(ykpiv_state *state, unsigned const char *ke
10071012 * dataptr ++ = challenge_len ;
10081013 challenge = dataptr ;
10091014 if (PRNG_GENERAL_ERROR == _ykpiv_prng_generate (challenge , challenge_len )) {
1010- DBG ("Failed getting randomness for authentication." );
1015+ DBG ("%s: Failed getting randomness for authentication." , ykpiv_strerror ( YKPIV_RANDOMNESS_ERROR ) );
10111016 res = YKPIV_RANDOMNESS_ERROR ;
10121017 goto Cleanup ;
10131018 }
10141019 dataptr += challenge_len ;
10151020 apdu .st .lc = (unsigned char )(dataptr - apdu .st .data );
10161021 recv_len = sizeof (data );
1017- if ((res = _ykpiv_send_apdu (state , & apdu , data , & recv_len , & sw )) != YKPIV_OK )
1018- {
1022+ if ((res = _ykpiv_send_apdu (state , & apdu , data , & recv_len , & sw )) != YKPIV_OK ) {
10191023 goto Cleanup ;
10201024 }
10211025 else if (sw != SW_SUCCESS ) {
10221026 res = YKPIV_AUTHENTICATION_ERROR ;
10231027 goto Cleanup ;
10241028 }
1025- }
10261029
1027- /* compare the response from the card with our challenge */
1028- {
1029- uint32_t out_len = challenge_len ;
1030+ /* compare the response from the card with our challenge */
1031+ out_len = challenge_len ;
10301032 drc = cipher_encrypt (mgm_key , challenge , challenge_len , challenge , & out_len );
10311033
10321034 if (drc != CIPHER_OK ) {
@@ -2126,6 +2128,7 @@ ykpiv_rc ykpiv_auth_verifyresponse(ykpiv_state *state, uint8_t *response, unsign
21262128
21272129 if (NULL == state ) return YKPIV_GENERIC_ERROR ;
21282130 if (NULL == response ) return YKPIV_GENERIC_ERROR ;
2131+ if (16 < response_len ) return YKPIV_GENERIC_ERROR ;
21292132
21302133 if (YKPIV_OK != (res = _ykpiv_begin_transaction (state ))) return res ;
21312134 /* note: do not select the applet here, as it resets the challenge state */
@@ -2141,7 +2144,7 @@ ykpiv_rc ykpiv_auth_verifyresponse(ykpiv_state *state, uint8_t *response, unsign
21412144 }
21422145 }
21432146
2144- /* send the response to the card and a challenge of our own . */
2147+ /* send the response to the card. */
21452148 APDU apdu = {0 };
21462149 apdu .st .ins = YKPIV_INS_AUTHENTICATE ;
21472150 apdu .st .p1 = metadata .algorithm ;
0 commit comments