Validate algorithm attributes before key creation

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-08-14 19:50:21 +02:00
parent 14b39a8f9f
commit 999d7180b2
4 changed files with 25 additions and 0 deletions
+3
View File
@@ -141,6 +141,9 @@ int cmd_import_data(void) {
if (algo_len == 0 || algo_len > OPENPGP_MAX_ALGORITHM_ATTR_SIZE) {
return SW_WRONG_DATA();
}
if (!openpgp_algorithm_attr_supported(algo, algo_len)) {
return SW_WRONG_DATA();
}
int r = 0;
if (algo[0] == ALGO_RSA) {
if (algo_len < 3) {
+3
View File
@@ -60,6 +60,9 @@ int cmd_keypair_gen(void) {
if (algo_len == 0 || algo_len > OPENPGP_MAX_ALGORITHM_ATTR_SIZE) {
return SW_WRONG_DATA();
}
if (!openpgp_algorithm_attr_supported(algo, algo_len)) {
return SW_WRONG_DATA();
}
if (P1(apdu) == 0x80) { //generate
if (algo[0] == ALGO_RSA) {
if (algo_len < 3) {
+18
View File
@@ -1441,6 +1441,24 @@ mbedtls_ecp_group_id get_ec_group_id_from_attr(const uint8_t *algo, size_t algo_
return MBEDTLS_ECP_DP_NONE;
}
bool openpgp_algorithm_attr_supported(const uint8_t *algo, size_t algo_len) {
if (!algo || algo_len < 2 || algo_len > OPENPGP_MAX_ALGORITHM_ATTR_SIZE) {
return false;
}
if (algo[0] == ALGO_RSA) {
uint16_t modulus_bits;
if (algo_len != 6 || algo[3] != 0 || algo[4] != 0x20 || algo[5] != 0) {
return false;
}
modulus_bits = ((uint16_t)algo[1] << 8) | algo[2];
return modulus_bits == 1024 || modulus_bits == 2048 || modulus_bits == 3072 || modulus_bits == 4096;
}
if (algo[0] == ALGO_ECDH || algo[0] == ALGO_ECDSA || algo[0] == ALGO_EDDSA) {
return get_ec_group_id_from_attr(algo + 1, algo_len - 1) != MBEDTLS_ECP_DP_NONE;
}
return false;
}
void make_rsa_response(mbedtls_rsa_context *rsa) {
memcpy(res_APDU, "\x7f\x49\x82\x00\x00", 5);
res_APDU_size = 5;
+1
View File
@@ -76,6 +76,7 @@ extern int openpgp_adminless_disable(void);
extern int openpgp_adminless_reset(void);
#endif
extern mbedtls_ecp_group_id get_ec_group_id_from_attr(const uint8_t *algo, size_t algo_len);
extern bool openpgp_algorithm_attr_supported(const uint8_t *algo, size_t algo_len);
extern int reset_sig_count(void);
extern uint16_t algo_dec, algo_aut, pk_dec, pk_aut;
extern bool wait_button_pressed_fid(uint16_t fid);