From 228836f79f64559c9582ac2d475e50af57684bf8 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Fri, 9 Oct 2020 13:53:23 +0900 Subject: [PATCH] gpg,ecc: Fix SOS handling when receiving from agent. * g10/export.c (transfer_format_to_openpgp): It's not simple opaque MPI, but SOS. -- Fixes-commit: f5bc94555458123f93d8b07816a68fb7485421e1 Signed-off-by: NIIBE Yutaka --- g10/export.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/g10/export.c b/g10/export.c index 08b161835..a9eba1544 100644 --- a/g10/export.c +++ b/g10/export.c @@ -982,11 +982,26 @@ transfer_format_to_openpgp (gcry_sexp_t s_pgp, PKT_public_key *pk) || pk->pubkey_algo == PUBKEY_ALGO_EDDSA || pk->pubkey_algo == PUBKEY_ALGO_ECDH) { - skey[skeyidx] = gcry_mpi_set_opaque_copy (NULL, value, valuelen*8); + unsigned int nbits = valuelen*8; + const unsigned char *p = value; + + if (nbits >= 8 && !(*p & 0x80)) + if (--nbits >= 7 && !(*p & 0x40)) + if (--nbits >= 6 && !(*p & 0x20)) + if (--nbits >= 5 && !(*p & 0x10)) + if (--nbits >= 4 && !(*p & 0x08)) + if (--nbits >= 3 && !(*p & 0x04)) + if (--nbits >= 2 && !(*p & 0x02)) + if (--nbits >= 1 && !(*p & 0x01)) + --nbits; + + skey[skeyidx] = gcry_mpi_set_opaque_copy (NULL, value, nbits); if (!skey[skeyidx]) goto outofmem; if (is_enc) gcry_mpi_set_flag (skey[skeyidx], GCRYMPI_FLAG_USER1); + else + gcry_mpi_set_flag (skey[skeyidx], GCRYMPI_FLAG_USER2); } else {