mbedtls: Avoid code duplication between v2 and v3 branches

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Eshan Kelkar <eshankelkar@galorithm.com>
(cherry picked from commit d1ad796496)
This commit is contained in:
Jakub Jelen
2025-05-14 22:51:50 +02:00
parent d0ef7afdfa
commit f1998d6064

View File

@@ -919,39 +919,36 @@ ssh_string pki_key_to_blob(const ssh_key key, enum ssh_key_e type)
switch (key->type) { switch (key->type) {
case SSH_KEYTYPE_RSA: { case SSH_KEYTYPE_RSA: {
mbedtls_rsa_context *rsa = NULL; mbedtls_rsa_context *rsa = NULL;
mbedtls_mpi *E_ptr = NULL, *N_ptr = NULL;
if (mbedtls_pk_can_do(key->pk, MBEDTLS_PK_RSA) == 0) { if (mbedtls_pk_can_do(key->pk, MBEDTLS_PK_RSA) == 0) {
SSH_BUFFER_FREE(buffer); SSH_BUFFER_FREE(buffer);
return NULL; return NULL;
} }
rsa = mbedtls_pk_rsa(*key->pk); rsa = mbedtls_pk_rsa(*key->pk);
#if MBEDTLS_VERSION_MAJOR > 2 #if MBEDTLS_VERSION_MAJOR > 2
rc = mbedtls_rsa_export(rsa, &N, NULL, NULL, NULL, &E); rc = mbedtls_rsa_export(rsa, &N, NULL, NULL, NULL, &E);
if (rc != 0) { if (rc != 0) {
goto fail; goto fail;
} }
e = ssh_make_bignum_string(&E); E_ptr = &E;
if (e == NULL) { N_ptr = &N;
goto fail;
}
n = ssh_make_bignum_string(&N);
if (n == NULL) {
goto fail;
}
#else #else
e = ssh_make_bignum_string(&rsa->E); E_ptr = &rsa->E;
N_ptr = &rsa->N;
#endif
e = ssh_make_bignum_string(E_ptr);
if (e == NULL) { if (e == NULL) {
goto fail; goto fail;
} }
n = ssh_make_bignum_string(&rsa->N); n = ssh_make_bignum_string(N_ptr);
if (n == NULL) { if (n == NULL) {
goto fail; goto fail;
} }
#endif
if (type == SSH_KEY_PUBLIC) { if (type == SSH_KEY_PUBLIC) {
/* The N and E parts are swapped in the public key export ! */ /* The N and E parts are swapped in the public key export ! */
@@ -965,6 +962,9 @@ ssh_string pki_key_to_blob(const ssh_key key, enum ssh_key_e type)
goto fail; goto fail;
} }
} else if (type == SSH_KEY_PRIVATE) { } else if (type == SSH_KEY_PRIVATE) {
mbedtls_mpi *P_ptr = NULL, *Q_ptr = NULL, *D_ptr = NULL;
mbedtls_mpi *IQMP_ptr = NULL;
rc = ssh_buffer_add_ssh_string(buffer, n); rc = ssh_buffer_add_ssh_string(buffer, n);
if (rc < 0) { if (rc < 0) {
goto fail; goto fail;
@@ -981,51 +981,41 @@ ssh_string pki_key_to_blob(const ssh_key key, enum ssh_key_e type)
goto fail; goto fail;
} }
p = ssh_make_bignum_string(&P);
if (p == NULL) {
goto fail;
}
q = ssh_make_bignum_string(&Q);
if (q == NULL) {
goto fail;
}
d = ssh_make_bignum_string(&D);
if (d == NULL) {
goto fail;
}
rc = mbedtls_rsa_export_crt(rsa, NULL, NULL, &IQMP); rc = mbedtls_rsa_export_crt(rsa, NULL, NULL, &IQMP);
if (rc != 0) { if (rc != 0) {
goto fail; goto fail;
} }
iqmp = ssh_make_bignum_string(&IQMP); P_ptr = &P;
if (iqmp == NULL) { Q_ptr = &Q;
goto fail; D_ptr = &D;
} IQMP_ptr = &IQMP;
#else #else
p = ssh_make_bignum_string(&rsa->P); P_ptr = &rsa->P;
Q_ptr = &rsa->Q;
D_ptr = &rsa->D;
IQMP_ptr = &rsa->QP;
#endif
p = ssh_make_bignum_string(P_ptr);
if (p == NULL) { if (p == NULL) {
goto fail; goto fail;
} }
q = ssh_make_bignum_string(&rsa->Q); q = ssh_make_bignum_string(Q_ptr);
if (q == NULL) { if (q == NULL) {
goto fail; goto fail;
} }
d = ssh_make_bignum_string(&rsa->D); d = ssh_make_bignum_string(D_ptr);
if (d == NULL) { if (d == NULL) {
goto fail; goto fail;
} }
iqmp = ssh_make_bignum_string(&rsa->QP); iqmp = ssh_make_bignum_string(IQMP_ptr);
if (iqmp == NULL) { if (iqmp == NULL) {
goto fail; goto fail;
} }
#endif
rc = ssh_buffer_add_ssh_string(buffer, d); rc = ssh_buffer_add_ssh_string(buffer, d);
if (rc < 0) { if (rc < 0) {