diff options
author | Jack Lloyd <[email protected]> | 2016-10-28 16:44:05 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-28 16:49:11 -0400 |
commit | f98c184fe66e6c0f624b381a186c6dddfc62539a (patch) | |
tree | 325aca069972bf38d92032d74b4a3d53d5b081d9 /src/lib/entropy | |
parent | 8141ea4c2a51e908fae3ebb463154acffeac9186 (diff) |
Remove HMAC_RNG, X9.31-RNG, BeOS stats, EGD reader, Unix process runner
Change AutoSeeded_RNG to use SHA-384, SHA-256, SHA-3(256), or SHA-1,
whichever is available (in that order).
Diffstat (limited to 'src/lib/entropy')
-rw-r--r-- | src/lib/entropy/beos_stats/es_beos.cpp | 66 | ||||
-rw-r--r-- | src/lib/entropy/beos_stats/es_beos.h | 28 | ||||
-rw-r--r-- | src/lib/entropy/beos_stats/info.txt | 17 | ||||
-rw-r--r-- | src/lib/entropy/egd/es_egd.cpp | 157 | ||||
-rw-r--r-- | src/lib/entropy/egd/es_egd.h | 52 | ||||
-rw-r--r-- | src/lib/entropy/egd/info.txt | 32 | ||||
-rw-r--r-- | src/lib/entropy/entropy_srcs.cpp | 28 | ||||
-rw-r--r-- | src/lib/entropy/unix_procs/info.txt | 25 | ||||
-rw-r--r-- | src/lib/entropy/unix_procs/unix_proc_sources.cpp | 65 | ||||
-rw-r--r-- | src/lib/entropy/unix_procs/unix_procs.cpp | 292 | ||||
-rw-r--r-- | src/lib/entropy/unix_procs/unix_procs.h | 93 |
11 files changed, 0 insertions, 855 deletions
diff --git a/src/lib/entropy/beos_stats/es_beos.cpp b/src/lib/entropy/beos_stats/es_beos.cpp deleted file mode 100644 index 907ca37bb..000000000 --- a/src/lib/entropy/beos_stats/es_beos.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* -* BeOS EntropySource -* (C) 1999-2008 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/internal/es_beos.h> - -#include <kernel/OS.h> -#include <kernel/image.h> -#include <interface/InterfaceDefs.h> - -namespace Botan { - -/** -* BeOS entropy poll -*/ -size_t BeOS_EntropySource::poll(RandomNumberGenerator& rng) - { - size_t bits = 0; - - system_info info_sys; - get_system_info(&info_sys); - rng.add_entropy_T(info_sys); - - key_info info_key; // current state of the keyboard - get_key_info(&info_key); - rng.add_entropy_T(info_key); - - team_info info_team; - int32 cookie_team = 0; - - while(get_next_team_info(&cookie_team, &info_team) == B_OK) - { - rng.add_entropy_T(info_team); - - team_id id = info_team.team; - int32 cookie = 0; - - thread_info info_thr; - while(get_next_thread_info(id, &cookie, &info_thr) == B_OK) - rng.add_entropy_T(info_thr); - - cookie = 0; - image_info info_img; - while(get_next_image_info(id, &cookie, &info_img) == B_OK) - rng.add_entropy_T(info_img); - - cookie = 0; - sem_info info_sem; - while(get_next_sem_info(id, &cookie, &info_sem) == B_OK) - rng.add_entropy_T(info_sem); - - cookie = 0; - area_info info_area; - while(get_next_area_info(id, &cookie, &info_area) == B_OK) - rng.add_entropy_T(info_area); - - bits += 32; - } - - return bits; - } - -} diff --git a/src/lib/entropy/beos_stats/es_beos.h b/src/lib/entropy/beos_stats/es_beos.h deleted file mode 100644 index e40433b6c..000000000 --- a/src/lib/entropy/beos_stats/es_beos.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -* BeOS EntropySource -* (C) 1999-2008 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_ENTROPY_SRC_BEOS_H__ -#define BOTAN_ENTROPY_SRC_BEOS_H__ - -#include <botan/entropy_src.h> - -namespace Botan { - -/** -* BeOS Entropy Source -*/ -class BeOS_EntropySource final : public Entropy_Source - { - private: - std::string name() const override { return "system_stats"; } - - size_t poll(RandomNumberGenerator& rng) override; - }; - -} - -#endif diff --git a/src/lib/entropy/beos_stats/info.txt b/src/lib/entropy/beos_stats/info.txt deleted file mode 100644 index 9ae527f49..000000000 --- a/src/lib/entropy/beos_stats/info.txt +++ /dev/null @@ -1,17 +0,0 @@ -define ENTROPY_SRC_BEOS 20131128 - -<source> -es_beos.cpp -</source> - -<header:internal> -es_beos.h -</header:internal> - -<os> -haiku -</os> - -<libs> -haiku -> root,be -</libs> diff --git a/src/lib/entropy/egd/es_egd.cpp b/src/lib/entropy/egd/es_egd.cpp deleted file mode 100644 index fdc1c9a0f..000000000 --- a/src/lib/entropy/egd/es_egd.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* -* EGD EntropySource -* (C) 1999-2009 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/internal/es_egd.h> -#include <botan/parsing.h> -#include <botan/exceptn.h> -#include <botan/mem_ops.h> - -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <unistd.h> - -#include <sys/socket.h> -#include <sys/un.h> - -#ifndef PF_LOCAL - #define PF_LOCAL PF_UNIX -#endif - -namespace Botan { - -EGD_EntropySource::EGD_Socket::EGD_Socket(const std::string& path) : - m_socket_path(path), m_fd(-1) - { - } - -/** -* Attempt a connection to an EGD/PRNGD socket -*/ -int EGD_EntropySource::EGD_Socket::open_socket(const std::string& path) - { - int fd = ::socket(PF_LOCAL, SOCK_STREAM, 0); - - if(fd >= 0) - { - sockaddr_un addr; - clear_mem(&addr, 1); - addr.sun_family = PF_LOCAL; - - if(path.length() >= sizeof(addr.sun_path)) - throw Invalid_Argument("EGD socket path is too long"); - - std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path)); - - int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1; - - if(::connect(fd, reinterpret_cast<struct ::sockaddr*>(&addr), len) < 0) - { - ::close(fd); - fd = -1; - } - } - - return fd; - } - -/** -* Attempt to read entropy from EGD -*/ -size_t EGD_EntropySource::EGD_Socket::read(byte outbuf[], size_t length) - { - if(length == 0) - return 0; - - if(m_fd < 0) - { - m_fd = open_socket(m_socket_path); - if(m_fd < 0) - return 0; - } - - try - { - // 1 == EGD command for non-blocking read - byte egd_read_command[2] = { - 1, static_cast<byte>(std::min<size_t>(length, 255)) }; - - if(::write(m_fd, egd_read_command, 2) != 2) - throw Exception("Writing entropy read command to EGD failed"); - - byte out_len = 0; - if(::read(m_fd, &out_len, 1) != 1) - throw Exception("Reading response length from EGD failed"); - - if(out_len > egd_read_command[1]) - throw Exception("Bogus length field received from EGD"); - - ssize_t count = ::read(m_fd, outbuf, out_len); - - if(count != out_len) - throw Exception("Reading entropy result from EGD failed"); - - return static_cast<size_t>(count); - } - catch(std::exception) - { - this->close(); - // Will attempt to reopen next poll - } - - return 0; - } - -void EGD_EntropySource::EGD_Socket::close() - { - if(m_fd >= 0) - { - ::close(m_fd); - m_fd = -1; - } - } - -/** -* EGD_EntropySource constructor -*/ -EGD_EntropySource::EGD_EntropySource(const std::vector<std::string>& paths) - { - for(size_t i = 0; i != paths.size(); ++i) - m_sockets.push_back(EGD_Socket(paths[i])); - } - -EGD_EntropySource::~EGD_EntropySource() - { - for(size_t i = 0; i != m_sockets.size(); ++i) - m_sockets[i].close(); - m_sockets.clear(); - } - -/** -* Gather Entropy from EGD -*/ -size_t EGD_EntropySource::poll(RandomNumberGenerator& rng) - { - lock_guard_type<mutex_type> lock(m_mutex); - - secure_vector<byte> buf(BOTAN_SYSTEM_RNG_POLL_REQUEST); - - for(size_t i = 0; i != m_sockets.size(); ++i) - { - size_t got = m_sockets[i].read(m_io_buf.data(), m_io_buf.size()); - - if(got) - { - rng.add_entropy(m_io_buf.data(), got); - return got * 8; - } - } - - return 0; - } - -} diff --git a/src/lib/entropy/egd/es_egd.h b/src/lib/entropy/egd/es_egd.h deleted file mode 100644 index e0fb9c2d5..000000000 --- a/src/lib/entropy/egd/es_egd.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -* EGD EntropySource -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_ENTROPY_SRC_EGD_H__ -#define BOTAN_ENTROPY_SRC_EGD_H__ - -#include <botan/entropy_src.h> -#include <string> -#include <vector> -#include <botan/mutex.h> - -namespace Botan { - -/** -* EGD Entropy Source -*/ -class EGD_EntropySource final : public Entropy_Source - { - public: - std::string name() const override { return "egd"; } - - size_t poll(RandomNumberGenerator& rng) override; - - EGD_EntropySource(const std::vector<std::string>&); - ~EGD_EntropySource(); - private: - class EGD_Socket - { - public: - EGD_Socket(const std::string& path); - - void close(); - size_t read(byte outbuf[], size_t length); - private: - static int open_socket(const std::string& path); - - std::string m_socket_path; - int m_fd; // cached fd - }; - - mutex_type m_mutex; - std::vector<EGD_Socket> m_sockets; - secure_vector<uint8_t> m_io_buf; - }; - -} - -#endif diff --git a/src/lib/entropy/egd/info.txt b/src/lib/entropy/egd/info.txt deleted file mode 100644 index b7b951c2b..000000000 --- a/src/lib/entropy/egd/info.txt +++ /dev/null @@ -1,32 +0,0 @@ -define ENTROPY_SRC_EGD 20131128 - -load_on auto - -<source> -es_egd.cpp -</source> - -<header:internal> -es_egd.h -</header:internal> - -<libs> -solaris -> socket -qnx -> socket -</libs> - -<os> -android -aix -cygwin -darwin -freebsd -dragonfly -hpux -irix -linux -netbsd -openbsd -qnx -solaris -</os> diff --git a/src/lib/entropy/entropy_srcs.cpp b/src/lib/entropy/entropy_srcs.cpp index 5c232a56e..21dfcff41 100644 --- a/src/lib/entropy/entropy_srcs.cpp +++ b/src/lib/entropy/entropy_srcs.cpp @@ -24,18 +24,6 @@ #include <botan/internal/dev_random.h> #endif -#if defined(BOTAN_HAS_ENTROPY_SRC_EGD) - #include <botan/internal/es_egd.h> -#endif - -#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER) - #include <botan/internal/unix_procs.h> -#endif - -#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) - #include <botan/internal/es_beos.h> -#endif - #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) #include <botan/internal/es_capi.h> #endif @@ -116,22 +104,6 @@ std::unique_ptr<Entropy_Source> Entropy_Source::create(const std::string& name) { #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) return std::unique_ptr<Entropy_Source>(new Win32_EntropySource); -#elif defined(BOTAN_HAS_ENTROPY_SRC_BEOS) - return std::unique_ptr<Entropy_Source>(new BeOS_EntropySource); -#endif - } - - if(name == "unix_procs") - { -#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER) - return std::unique_ptr<Entropy_Source>(new Unix_EntropySource(BOTAN_ENTROPY_SAFE_PATHS)); -#endif - } - - if(name == "egd") - { -#if defined(BOTAN_HAS_ENTROPY_SRC_EGD) - return std::unique_ptr<Entropy_Source>(new EGD_EntropySource(BOTAN_ENTROPY_EGD_PATHS)); #endif } diff --git a/src/lib/entropy/unix_procs/info.txt b/src/lib/entropy/unix_procs/info.txt deleted file mode 100644 index ee382d8da..000000000 --- a/src/lib/entropy/unix_procs/info.txt +++ /dev/null @@ -1,25 +0,0 @@ -define ENTROPY_SRC_UNIX_PROCESS_RUNNER 20131128 - -<source> -unix_procs.cpp -unix_proc_sources.cpp -</source> - -<header:internal> -unix_procs.h -</header:internal> - -<os> -android -aix -cygwin -darwin -freebsd -haiku -hpux -irix -linux -netbsd -qnx -solaris -</os> diff --git a/src/lib/entropy/unix_procs/unix_proc_sources.cpp b/src/lib/entropy/unix_procs/unix_proc_sources.cpp deleted file mode 100644 index 429198706..000000000 --- a/src/lib/entropy/unix_procs/unix_proc_sources.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* -* Program List for Unix_EntropySource -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/internal/unix_procs.h> - -namespace Botan { - -/** -* Default Commands for Entropy Gathering -*/ -std::vector<std::vector<std::string>> Unix_EntropySource::get_default_sources() - { - std::vector<std::vector<std::string>> srcs; - - srcs.push_back({ "netstat", "-in" }); - srcs.push_back({ "pfstat" }); - srcs.push_back({ "vmstat", "-s" }); - srcs.push_back({ "vmstat" }); - - srcs.push_back({ "arp", "-a", "-n" }); - srcs.push_back({ "ifconfig", "-a" }); - srcs.push_back({ "iostat" }); - srcs.push_back({ "ipcs", "-a" }); - srcs.push_back({ "mpstat" }); - srcs.push_back({ "netstat", "-an" }); - srcs.push_back({ "netstat", "-s" }); - srcs.push_back({ "nfsstat" }); - srcs.push_back({ "portstat" }); - srcs.push_back({ "procinfo", "-a" }); - srcs.push_back({ "pstat", "-T" }); - srcs.push_back({ "pstat", "-s" }); - srcs.push_back({ "uname", "-a" }); - srcs.push_back({ "uptime" }); - - srcs.push_back({ "listarea" }); - srcs.push_back({ "listdev" }); - srcs.push_back({ "ps", "-A" }); - srcs.push_back({ "sysinfo" }); - - srcs.push_back({ "finger" }); - srcs.push_back({ "mailstats" }); - srcs.push_back({ "rpcinfo", "-p", "localhost" }); - srcs.push_back({ "who" }); - - srcs.push_back({ "df", "-l" }); - srcs.push_back({ "dmesg" }); - srcs.push_back({ "last", "-5" }); - srcs.push_back({ "ls", "-alni", "/proc" }); - srcs.push_back({ "ls", "-alni", "/tmp" }); - srcs.push_back({ "pstat", "-f" }); - - srcs.push_back({ "ps", "-elf" }); - srcs.push_back({ "ps", "aux" }); - - srcs.push_back({ "lsof", "-n" }); - srcs.push_back({ "sar", "-A" }); - - return srcs; - } - -} diff --git a/src/lib/entropy/unix_procs/unix_procs.cpp b/src/lib/entropy/unix_procs/unix_procs.cpp deleted file mode 100644 index eae1b5255..000000000 --- a/src/lib/entropy/unix_procs/unix_procs.cpp +++ /dev/null @@ -1,292 +0,0 @@ - /* -* Gather entropy by running various system commands in the hopes that -* some of the output cannot be guessed by a remote attacker. -* -* (C) 1999-2009,2013 Jack Lloyd -* 2012 Markus Wanner -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/internal/unix_procs.h> -#include <botan/exceptn.h> -#include <botan/parsing.h> -#include <algorithm> -#include <atomic> - -#include <sys/time.h> -#include <sys/stat.h> -#include <sys/wait.h> -#include <sys/resource.h> -#include <unistd.h> -#include <signal.h> -#include <stdlib.h> - -namespace Botan { - -namespace { - -std::string find_full_path_if_exists(const std::vector<std::string>& trusted_path, - const std::string& proc) - { - for(auto dir : trusted_path) - { - const std::string full_path = dir + "/" + proc; - if(::access(full_path.c_str(), X_OK) == 0) - return full_path; - } - - return ""; - } - -size_t concurrent_processes(size_t user_request) - { - const size_t DEFAULT_CONCURRENT = 2; - const size_t MAX_CONCURRENT = 8; - - if(user_request > 0) - return std::min(user_request, MAX_CONCURRENT); - - const long online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); - - if(online_cpus > 0) - return static_cast<size_t>(online_cpus); // maybe fewer? - - return DEFAULT_CONCURRENT; - } - -} - -/** -* Unix_EntropySource Constructor -*/ -Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& trusted_paths, - size_t proc_count) : - m_trusted_paths(trusted_paths), - m_concurrent(concurrent_processes(proc_count)) - { - } - -size_t UnixProcessInfo_EntropySource::poll(RandomNumberGenerator& rng) - { - rng.add_entropy_T(::getpid()); - rng.add_entropy_T(::getppid()); - rng.add_entropy_T(::getuid()); - rng.add_entropy_T(::getgid()); - rng.add_entropy_T(::getpgrp()); - - struct ::rusage usage; - ::getrusage(RUSAGE_SELF, &usage); - rng.add_entropy_T(usage); - -#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME) - -#define CLOCK_GETTIME_POLL(src) \ - do { \ - struct timespec ts; \ - ::clock_gettime(src, &ts); \ - rng.add_entropy_T(ts); \ - } while(0) - -#if defined(CLOCK_REALTIME) - CLOCK_GETTIME_POLL(CLOCK_REALTIME); -#endif - -#if defined(CLOCK_MONOTONIC) - CLOCK_GETTIME_POLL(CLOCK_MONOTONIC); -#endif - -#if defined(CLOCK_MONOTONIC_RAW) - CLOCK_GETTIME_POLL(CLOCK_MONOTONIC_RAW); -#endif - -#if defined(CLOCK_PROCESS_CPUTIME_ID) - CLOCK_GETTIME_POLL(CLOCK_PROCESS_CPUTIME_ID); -#endif - -#if defined(CLOCK_THREAD_CPUTIME_ID) - CLOCK_GETTIME_POLL(CLOCK_THREAD_CPUTIME_ID); -#endif - -#undef CLOCK_GETTIME_POLL - -#endif - - return 0; - } - -void Unix_EntropySource::Unix_Process::spawn(const std::vector<std::string>& args) - { - if(args.empty()) - throw Invalid_Argument("Cannot spawn process without path"); - - shutdown(); - - int pipe[2]; - if(::pipe(pipe) != 0) - return; - - pid_t pid = ::fork(); - - if(pid == -1) - { - ::close(pipe[0]); - ::close(pipe[1]); - } - else if(pid > 0) // in parent - { - m_pid = pid; - m_fd = pipe[0]; - ::close(pipe[1]); - } - else // in child - { - if(::dup2(pipe[1], STDOUT_FILENO) == -1) - ::exit(127); - if(::close(pipe[0]) != 0 || ::close(pipe[1]) != 0) - ::exit(127); - if(close(STDERR_FILENO) != 0) - ::exit(127); - - const char* arg0 = args[0].c_str(); - const char* arg1 = (args.size() > 1) ? args[1].c_str() : nullptr; - const char* arg2 = (args.size() > 2) ? args[2].c_str() : nullptr; - const char* arg3 = (args.size() > 3) ? args[3].c_str() : nullptr; - const char* arg4 = (args.size() > 4) ? args[4].c_str() : nullptr; - - ::execl(arg0, arg0, arg1, arg2, arg3, arg4, NULL); - ::exit(127); - } - } - -void Unix_EntropySource::Unix_Process::shutdown() - { - if(m_pid == -1) - return; - - ::close(m_fd); - m_fd = -1; - - pid_t reaped = waitpid(m_pid, nullptr, WNOHANG); - - if(reaped == 0) - { - /* - * Child is still alive - send it SIGTERM, sleep for a bit and - * try to reap again, if still alive send SIGKILL - */ - kill(m_pid, SIGTERM); - - struct ::timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 1000; - select(0, nullptr, nullptr, nullptr, &tv); - - reaped = ::waitpid(m_pid, nullptr, WNOHANG); - - if(reaped == 0) - { - ::kill(m_pid, SIGKILL); - do - reaped = ::waitpid(m_pid, nullptr, 0); - while(reaped == -1); - } - } - - m_pid = -1; - } - -const std::vector<std::string>& Unix_EntropySource::next_source() - { - const auto& src = m_sources.at(m_sources_idx); - m_sources_idx = (m_sources_idx + 1) % m_sources.size(); - return src; - } - -size_t Unix_EntropySource::poll(RandomNumberGenerator& rng) - { - // refuse to run setuid or setgid, or as root - if((getuid() != geteuid()) || (getgid() != getegid()) || (geteuid() == 0)) - return 0; - - lock_guard_type<mutex_type> lock(m_mutex); - - if(m_sources.empty()) - { - auto sources = get_default_sources(); - - for(auto src : sources) - { - const std::string path = find_full_path_if_exists(m_trusted_paths, src[0]); - if(path != "") - { - src[0] = path; - m_sources.push_back(src); - } - } - } - - if(m_sources.empty()) - return 0; // still empty, really nothing to try - - const size_t MS_WAIT_TIME = 32; - - m_buf.resize(4096); - - size_t bytes = 0; - - while(bytes < 128 * 1024) // arbitrary limit... - { - while(m_procs.size() < m_concurrent) - m_procs.emplace_back(Unix_Process(next_source())); - - fd_set read_set; - FD_ZERO(&read_set); - - std::vector<int> fds; - - for(auto& proc : m_procs) - { - int fd = proc.fd(); - if(fd > 0) - { - fds.push_back(fd); - FD_SET(fd, &read_set); - } - } - - if(fds.empty()) - break; - - const int max_fd = *std::max_element(fds.begin(), fds.end()); - - struct ::timeval timeout; - timeout.tv_sec = (MS_WAIT_TIME / 1000); - timeout.tv_usec = (MS_WAIT_TIME % 1000) * 1000; - - if(::select(max_fd + 1, &read_set, nullptr, nullptr, &timeout) < 0) - break; // or continue? - - for(auto& proc : m_procs) - { - int fd = proc.fd(); - - if(FD_ISSET(fd, &read_set)) - { - const ssize_t got = ::read(fd, m_buf.data(), m_buf.size()); - - if(got > 0) - { - rng.add_entropy(m_buf.data(), got); - bytes += got; - } - else - proc.spawn(next_source()); - } - } - } - - return bytes / 1024; - } - -} diff --git a/src/lib/entropy/unix_procs/unix_procs.h b/src/lib/entropy/unix_procs/unix_procs.h deleted file mode 100644 index 24c10fff0..000000000 --- a/src/lib/entropy/unix_procs/unix_procs.h +++ /dev/null @@ -1,93 +0,0 @@ -/* -* Unix EntropySource -* (C) 1999-2009,2013 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_ENTROPY_SRC_UNIX_H__ -#define BOTAN_ENTROPY_SRC_UNIX_H__ - -#include <botan/entropy_src.h> -#include <vector> -#include <botan/mutex.h> - -namespace Botan { - -/** -* Entropy source for generic Unix. Runs various programs trying to -* gather data hard for a remote attacker to guess. Probably not too -* effective against local attackers as they can sample from the same -* distribution. -*/ -class Unix_EntropySource final : public Entropy_Source - { - public: - std::string name() const override { return "unix_procs"; } - - size_t poll(RandomNumberGenerator& rng) override; - - /** - * @param trusted_paths is a list of directories that are assumed - * to contain only 'safe' binaries. If an attacker can write - * an executable to one of these directories then we will - * run arbitrary code. - * @param proc_count number of concurrent processes executing, - * when set to zero, number of processors is used - */ - Unix_EntropySource(const std::vector<std::string>& trusted_paths, - size_t proc_count = 0); - private: - static std::vector<std::vector<std::string>> get_default_sources(); - - class Unix_Process - { - public: - int fd() const { return m_fd; } - - void spawn(const std::vector<std::string>& args); - void shutdown(); - - Unix_Process() {} - - Unix_Process(const std::vector<std::string>& args) { spawn(args); } - - ~Unix_Process() { shutdown(); } - - Unix_Process(Unix_Process&& other) - { - std::swap(m_fd, other.m_fd); - std::swap(m_pid, other.m_pid); - } - - Unix_Process(const Unix_Process&) = delete; - Unix_Process& operator=(const Unix_Process&) = delete; - private: - int m_fd = -1; - int m_pid = -1; - }; - - const std::vector<std::string>& next_source(); - - mutex_type m_mutex; - const std::vector<std::string> m_trusted_paths; - const size_t m_concurrent; - - std::vector<std::vector<std::string>> m_sources; - size_t m_sources_idx = 0; - - std::vector<Unix_Process> m_procs; - secure_vector<byte> m_buf; - }; - -class UnixProcessInfo_EntropySource final : public Entropy_Source - { - public: - std::string name() const override { return "proc_info"; } - - size_t poll(RandomNumberGenerator& rng) override; - }; - -} - -#endif |