Skip to content

Commit

Permalink
Update card drivers with double option for PKCS#1 padding
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik authored and Jakuje committed Feb 5, 2024
1 parent b0f7fb9 commit df58bfc
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/libopensc/card-atrust-acos.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ static int atrust_acos_set_security_env(struct sc_card *card,
}
pp = p;
if (operation == SC_SEC_OPERATION_DECIPHER){
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02) {
*p++ = 0x80;
*p++ = 0x01;
*p++ = 0x02;
Expand All @@ -580,7 +580,7 @@ static int atrust_acos_set_security_env(struct sc_card *card,
}
/* try COMPUTE SIGNATURE */
if (operation == SC_SEC_OPERATION_SIGN && (
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1 ||
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01 ||
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_ISO9796)) {
if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
*p++ = 0x80;
Expand All @@ -591,7 +591,7 @@ static int atrust_acos_set_security_env(struct sc_card *card,
/* set the method to use based on the algorithm_flags */
*p++ = 0x80;
*p++ = 0x01;
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
*p++ = 0x12;
else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_RIPEMD160)
Expand Down
2 changes: 1 addition & 1 deletion src/libopensc/card-belpic.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ static int belpic_set_security_env(sc_card_t *card,
apdu.p2 = 0xB6;
sbuf[0] = 0x04; /* length of the following data */
sbuf[1] = 0x80; /* tag for algorithm reference */
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
sbuf[2] = 0x01;
else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
sbuf[2] = 0x02;
Expand Down
5 changes: 4 additions & 1 deletion src/libopensc/card-cardos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,10 @@ cardos_set_security_env(sc_card_t *card,
data[7] = 0x01;
data[8] = key_id & 0xF0;
apdu.lc = apdu.datalen = 9;
} else if (priv->sec_env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
} else if ((env->operation == SC_SEC_OPERATION_SIGN
&& priv->sec_env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
|| (env->operation == SC_SEC_OPERATION_DECIPHER
&& priv->sec_env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)) {
/* TODO this may only apply to c903 cards */
/* TODO or only for cards without any supported_algos or EIDComplient only */
data[6] = 0x80;
Expand Down
4 changes: 2 additions & 2 deletions src/libopensc/card-gemsafeV1.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,15 @@ static u8 gemsafe_flags2algref(struct sc_card *card, const struct sc_security_en
if (env->operation == SC_SEC_OPERATION_SIGN) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256)
ret = GEMSAFEV3_ALG_REF_SHA256;
else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
ret = (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) ?
GEMSAFEV3_ALG_REF_FREEFORM :
GEMSAFEV1_ALG_REF_FREEFORM;
else if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_ISO9796)
ret = 0x11;
} else if (env->operation == SC_SEC_OPERATION_DECIPHER) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)
ret = (card->type == SC_CARD_TYPE_GEMSAFEV1_PTEID ||
card->type == SC_CARD_TYPE_GEMSAFEV1_SEEID) ?
GEMSAFEV3_ALG_REF_FREEFORM :
Expand Down
4 changes: 2 additions & 2 deletions src/libopensc/card-iasecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@ iasecc_set_security_env(struct sc_card *card,

aflags = env->algorithm_flags;

if (!(aflags & SC_ALGORITHM_RSA_PAD_PKCS1))
if (!(aflags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01))
LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Only supported signature with PKCS1 padding");

if (operation == SC_SEC_OPERATION_SIGN) {
Expand Down Expand Up @@ -1842,7 +1842,7 @@ iasecc_set_security_env(struct sc_card *card,
env->algorithm_flags, prv->key_size);
switch (operation) {
case SC_SEC_OPERATION_SIGN:
if (!(env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1))
if (!(env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01))
LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Need RSA_PKCS1 specified");

if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256) {
Expand Down
2 changes: 1 addition & 1 deletion src/libopensc/card-idprime.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ idprime_set_security_env(struct sc_card *card,
new_env.algorithm_ref = 0x65;
}
priv->current_op = SC_ALGORITHM_RSA;
} else if (env->algorithm_flags & (SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_PAD_OAEP)) {
} else if (env->algorithm_flags & (SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01 | SC_ALGORITHM_RSA_PAD_OAEP)) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA256) {
new_env.algorithm_ref = 0x42;
} else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA384) {
Expand Down
3 changes: 2 additions & 1 deletion src/libopensc/card-myeid.c
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,8 @@ static int myeid_set_security_env(struct sc_card *card,
tmp.algorithm_ref = 0x00;
/* potential FIXME: return an error, if an unsupported
* pad or hash was requested, although this shouldn't happen */
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
if ((env->operation == SC_SEC_OPERATION_SIGN && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
|| (env->operation == SC_SEC_OPERATION_DECIPHER && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02))
tmp.algorithm_ref = 0x02;
if (tmp.algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
tmp.algorithm_ref |= 0x10;
Expand Down
3 changes: 2 additions & 1 deletion src/libopensc/card-setcos.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ static int setcos_set_security_env(sc_card_t *card,
/* potential FIXME: return an error, if an unsupported
* pad or hash was requested, although this shouldn't happen.
*/
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
if ((env->operation == SC_SEC_OPERATION_SIGN && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01) ||
(env->operation == SC_SEC_OPERATION_DECIPHER && env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02))
tmp.algorithm_ref = 0x02;
if (tmp.algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
tmp.algorithm_ref |= 0x10;
Expand Down
9 changes: 5 additions & 4 deletions src/libopensc/card-starcos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,8 @@ static int starcos_set_security_env(sc_card_t *card,
if (apdu.sw1 != 0x90 || apdu.sw2 != 0x00)
SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, sc_check_sw(card, apdu.sw1, apdu.sw2));

if (env->algorithm_flags == SC_ALGORITHM_RSA_PAD_PKCS1) {
if ((operation == SC_SEC_OPERATION_SIGN && env->algorithm_flags == SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01)
|| (operation == SC_SEC_OPERATION_DECIPHER && env->algorithm_flags == SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)) {
// input data will be already padded
ex_data->fix_digestInfo = 0;
} else {
Expand All @@ -1664,7 +1665,7 @@ static int starcos_set_security_env(sc_card_t *card,
}
pp = p;
if (operation == SC_SEC_OPERATION_DECIPHER){
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02) {
*p++ = 0x80;
*p++ = 0x01;
*p++ = 0x02;
Expand All @@ -1684,7 +1685,7 @@ static int starcos_set_security_env(sc_card_t *card,
}
/* try COMPUTE SIGNATURE */
if (operation == SC_SEC_OPERATION_SIGN && (
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1 ||
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01 ||
env->algorithm_flags & SC_ALGORITHM_RSA_PAD_ISO9796)) {
if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
*p++ = 0x80;
Expand All @@ -1695,7 +1696,7 @@ static int starcos_set_security_env(sc_card_t *card,
/* set the method to use based on the algorithm_flags */
*p++ = 0x80;
*p++ = 0x01;
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01) {
if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_SHA1)
*p++ = 0x12;
else if (env->algorithm_flags & SC_ALGORITHM_RSA_HASH_RIPEMD160)
Expand Down
15 changes: 7 additions & 8 deletions src/libopensc/card-tcos.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ static int tcos_set_security_env(sc_card_t *card, const sc_security_env_t *env,
env->key_ref[0], env->key_ref_len);
/* Key-Reference 0x80 ?? */
default_key= !(env->flags & SC_SEC_ENV_KEY_REF_PRESENT) || (env->key_ref_len==1 && env->key_ref[0]==0x80);
sc_log(ctx,
"TCOS3:%d PKCS1:%d\n", tcos3,
!!(env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1));
sc_log(ctx, "TCOS3:%d PKCS1 type 01:%d PKCS1 type 02: %d\n", tcos3,
!!(env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_01),
!!(env->algorithm_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02));

data->pad_flags = env->algorithm_flags;
data->next_sign = default_key;
Expand Down Expand Up @@ -635,9 +635,8 @@ static int tcos_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len,
data = (tcos_data *)card->drv_data;

LOG_FUNC_CALLED(ctx);
sc_log(ctx,
"TCOS3:%d PKCS1:%d\n", tcos3,
!!(data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1));
sc_log(ctx, "TCOS3:%d PKCS1 type 02:%d\n", tcos3,
!!(data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02));

sc_format_apdu(card, &apdu, crgram_len > 255 ? SC_APDU_CASE_4_EXT : SC_APDU_CASE_4_SHORT, 0x2A, 0x80, 0x86);
apdu.resp = rbuf;
Expand All @@ -646,7 +645,7 @@ static int tcos_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len,

apdu.data = sbuf;
apdu.lc = apdu.datalen = crgram_len + 1;
sbuf[0] = tcos3 ? 0x00 : ((data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1) ? 0x81 : 0x02);
sbuf[0] = tcos3 ? 0x00 : ((data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02) ? 0x81 : 0x02);
if (sizeof sbuf - 1 < crgram_len)
return SC_ERROR_INVALID_ARGUMENTS;
memcpy(sbuf + 1, crgram, crgram_len);
Expand All @@ -658,7 +657,7 @@ static int tcos_decipher(sc_card_t *card, const u8 * crgram, size_t crgram_len,
size_t len = (apdu.resplen > outlen) ? outlen : apdu.resplen;
unsigned int offset = 0;

if (tcos3 && (data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1)
if (tcos3 && (data->pad_flags & SC_ALGORITHM_RSA_PAD_PKCS1_TYPE_02)
&& len > 2 && apdu.resp[0] == 0 && apdu.resp[1] == 2) {
offset = 2;
while (offset < len && apdu.resp[offset] != 0)
Expand Down

0 comments on commit df58bfc

Please sign in to comment.