Mercurial > dropbear
view libtommath/bn_s_mp_rand_platform.c @ 1788:1fc0012b9c38
Fix handling of replies to global requests (#112)
The current code assumes that all global requests want / need a reply.
This isn't always true and the request itself indicates if it wants a
reply or not.
It causes a specific problem with [email protected] messages.
These are sent by OpenSSH after authentication to inform the client of
potential other host keys for the host. This can be used to add a new
type of host key or to rotate host keys.
The initial information message from the server is sent as a global
request, but with want_reply set to false. This means that the server
doesn't expect an answer to this message. Instead the client needs to
send a prove request as a reply if it wants to receive proof of
ownership for the host keys.
The bug doesn't cause any current problems with due to how OpenSSH
treats receiving the failure message. It instead treats it as a
keepalive message and further ignores it.
Arguably this is a protocol violation though of Dropbear and it is only
accidental that it doesn't cause a problem with OpenSSH.
The bug was found when adding host keys support to libssh, which is more
strict protocol wise and treats the unexpected failure message an error,
also see https://gitlab.com/libssh/libssh-mirror/-/merge_requests/145
for more information.
The fix here is to honor the want_reply flag in the global request and
to only send a reply if the other side expects a reply.
author | Dirkjan Bussink <d.bussink@gmail.com> |
---|---|
date | Thu, 10 Dec 2020 16:13:13 +0100 |
parents | 1051e4eea25a |
children |
line wrap: on
line source
#include "tommath_private.h" #ifdef BN_S_MP_RAND_PLATFORM_C /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ /* First the OS-specific special cases * - *BSD * - Windows */ #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) #define BN_S_READ_ARC4RANDOM_C static mp_err s_read_arc4random(void *p, size_t n) { arc4random_buf(p, n); return MP_OKAY; } #endif #if defined(_WIN32) || defined(_WIN32_WCE) #define BN_S_READ_WINCSP_C #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0400 #endif #ifdef _WIN32_WCE #define UNDER_CE #define ARM #endif #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <wincrypt.h> static mp_err s_read_wincsp(void *p, size_t n) { static HCRYPTPROV hProv = 0; if (hProv == 0) { HCRYPTPROV h = 0; if (!CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL, (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) && !CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) { return MP_ERR; } hProv = h; } return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR; } #endif /* WIN32 */ #if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ) #if __GLIBC_PREREQ(2, 25) #define BN_S_READ_GETRANDOM_C #include <sys/random.h> #include <errno.h> static mp_err s_read_getrandom(void *p, size_t n) { char *q = (char *)p; while (n > 0u) { ssize_t ret = getrandom(q, n, 0); if (ret < 0) { if (errno == EINTR) { continue; } return MP_ERR; } q += ret; n -= (size_t)ret; } return MP_OKAY; } #endif #endif /* We assume all platforms besides windows provide "/dev/urandom". * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time. */ #if !defined(BN_S_READ_WINCSP_C) && !defined(MP_NO_DEV_URANDOM) #define BN_S_READ_URANDOM_C #ifndef MP_DEV_URANDOM #define MP_DEV_URANDOM "/dev/urandom" #endif #include <fcntl.h> #include <errno.h> #include <unistd.h> static mp_err s_read_urandom(void *p, size_t n) { int fd; char *q = (char *)p; do { fd = open(MP_DEV_URANDOM, O_RDONLY); } while ((fd == -1) && (errno == EINTR)); if (fd == -1) return MP_ERR; while (n > 0u) { ssize_t ret = read(fd, p, n); if (ret < 0) { if (errno == EINTR) { continue; } close(fd); return MP_ERR; } q += ret; n -= (size_t)ret; } close(fd); return MP_OKAY; } #endif #if defined(MP_PRNG_ENABLE_LTM_RNG) #define BN_S_READ_LTM_RNG unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); void (*ltm_rng_callback)(void); static mp_err s_read_ltm_rng(void *p, size_t n) { unsigned long res; if (ltm_rng == NULL) return MP_ERR; res = ltm_rng(p, n, ltm_rng_callback); if (res != n) return MP_ERR; return MP_OKAY; } #endif mp_err s_read_arc4random(void *p, size_t n); mp_err s_read_wincsp(void *p, size_t n); mp_err s_read_getrandom(void *p, size_t n); mp_err s_read_urandom(void *p, size_t n); mp_err s_read_ltm_rng(void *p, size_t n); mp_err s_mp_rand_platform(void *p, size_t n) { mp_err err = MP_ERR; if ((err != MP_OKAY) && MP_HAS(S_READ_ARC4RANDOM)) err = s_read_arc4random(p, n); if ((err != MP_OKAY) && MP_HAS(S_READ_WINCSP)) err = s_read_wincsp(p, n); if ((err != MP_OKAY) && MP_HAS(S_READ_GETRANDOM)) err = s_read_getrandom(p, n); if ((err != MP_OKAY) && MP_HAS(S_READ_URANDOM)) err = s_read_urandom(p, n); if ((err != MP_OKAY) && MP_HAS(S_READ_LTM_RNG)) err = s_read_ltm_rng(p, n); return err; } #endif