Skip to content

Commit 3f24f0b

Browse files
authored
Merge pull request OpenSC#3558 from dengert/piv-history-fix
PIV: fix possible stack buffer overflow when handling history object Thanks to Nicholas Carlini <[email protected]> for reporting this issue
2 parents 6afc34a + cb35c5a commit 3f24f0b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/libopensc/card-piv.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5086,6 +5086,10 @@ piv_process_history(sc_card_t *card)
50865086

50875087
url = sc_asn1_find_tag(card->ctx, body, bodylen, 0xF3, &urllen);
50885088
if (url) {
5089+
if (urllen > 118) {
5090+
r = SC_ERROR_INVALID_ASN1_OBJECT;
5091+
goto err;
5092+
}
50895093
priv->offCardCertURL = calloc(1,urllen+1);
50905094
if (priv->offCardCertURL == NULL)
50915095
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
@@ -5112,8 +5116,9 @@ piv_process_history(sc_card_t *card)
51125116
* the card. some of the certs may be on the card as well.
51135117
*
51145118
* Get file name from url. verify that the filename is valid
5115-
* The URL ends in a SHA1 string. We will use this as the filename
5119+
* The URL ends in a SHA-256 string. We will use this as the filename
51165120
* in the directory used for the PKCS15 cache
5121+
* "http://" <DNS name> "/" <ASCII-HEX encoded SHA-256 hash of OffCardKeyHistoryFile>
51175122
*/
51185123

51195124
r = 0;
@@ -5132,6 +5137,16 @@ piv_process_history(sc_card_t *card)
51325137
goto err;
51335138
}
51345139
fp++;
5140+
if (strlen(fp) != 64) { /* ASCII-HEX encoded SHA-256 */
5141+
r = SC_ERROR_INVALID_DATA;
5142+
goto err;
5143+
}
5144+
for (i = 0; i < 64; i++) {
5145+
if (isxdigit((unsigned char)fp[i]) == 0) {
5146+
r = SC_ERROR_INVALID_DATA;
5147+
goto err;
5148+
}
5149+
}
51355150

51365151
/* Use the same directory as used for other OpenSC cached items */
51375152
r = sc_get_cache_dir(card->ctx, filename, sizeof(filename) - strlen(fp) - 2);

0 commit comments

Comments
 (0)