Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/libopensc/card-piv.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ static struct piv_aid piv_aids[] = {
#define CI_RSA_4096 0x00080000U /* Card supports rsa 4096 */
#define CI_25519 0x00100000U /* Card supports ED25519 and X25519 */
#define CI_EC521 0x00200000U /* Card supports EC 521 */
#define CI_ENFORCES_ECDSA_HASH_SIZE 0x00400000U /* Applet requires a fixed hash size for each ECDSA curve. */

/*
* Flags in the piv_object:
Expand Down Expand Up @@ -4717,6 +4718,9 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen,
{
piv_private_data_t * priv = PIV_DATA(card);
int r;
u8 *ecdata = NULL;
size_t ecdatalen = 0;
size_t ecpadlen = 0;
size_t nLen;
u8 rbuf[165]; /* For EC conversions 512 will fit */

Expand All @@ -4730,6 +4734,29 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen,

if (priv->alg_id == 0x11 || priv->alg_id == 0x14 || priv->alg_id == 0x32) {
nLen = BYTES4BITS(priv->key_size);

/* The applet enforces a fixed hash size for each curve.
* Windows may pass shorter input during offline sign-in,
* so pad the input to the expected hash size.
*/
const size_t hash_len = priv->alg_id == 0x11 ? 32 :
priv->alg_id == 0x14 ? 48 : 64;
if (priv->card_issues & CI_ENFORCES_ECDSA_HASH_SIZE &&
!strcmp(card->ctx->app_name, "cardmod") &&
(datalen < hash_len)) {
ecpadlen = hash_len - datalen;
} else {
ecpadlen = 0;
}

ecdatalen = datalen + ecpadlen;
ecdata = calloc(ecdatalen, sizeof(u8));
if (ecdata == NULL) {
r = SC_ERROR_OUT_OF_MEMORY;
goto err;
}
memcpy(ecdata + ecpadlen, data, datalen);

if (outlen < 2*nLen) {
sc_log(card->ctx,
" output too small for EC signature %"SC_FORMAT_LEN_SIZE_T"u < %"SC_FORMAT_LEN_SIZE_T"u",
Expand All @@ -4738,7 +4765,7 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen,
goto err;
}

r = piv_validate_general_authentication(card, data, datalen, rbuf, sizeof rbuf);
r = piv_validate_general_authentication(card, ecdata, ecdatalen, rbuf, sizeof rbuf);
if (r < 0)
goto err;

Expand All @@ -4760,6 +4787,7 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen,
}

err:
free(ecdata);
SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
}

Expand Down Expand Up @@ -5820,7 +5848,7 @@ static int piv_match_card_continued(sc_card_t *card)
break;

case SC_CARD_TYPE_PIV_II_SWISSBIT2:
priv->card_issues |= 0; /* could add others here */
priv->card_issues |= CI_ENFORCES_ECDSA_HASH_SIZE;
if (priv->swissbit_version >= 0x00010200)
priv->card_issues |= CI_RSA_4096 | CI_EC521;
break;
Expand Down