diff options
author | René Korthaus <[email protected]> | 2017-08-04 10:46:04 +0200 |
---|---|---|
committer | René Korthaus <[email protected]> | 2017-08-04 10:48:41 +0200 |
commit | b717ec3d228719eb0401b2e61d717edca704c899 (patch) | |
tree | 3dda4a8404bed481f29369b74b46aeaa1b2222f4 | |
parent | 8a29dc8209c6e93581075bbc4c39ff5bf0cdace5 (diff) |
Add support for Windows sockets to http_util
Based on the work by @slicer4ever, adds support for
Windows sockets to http_util. As a bonus, we get Windows support
for tls_client and tls_server CLI.
-rw-r--r-- | src/build-data/os/windows.txt | 1 | ||||
-rw-r--r-- | src/cli/tls_client.cpp | 52 | ||||
-rw-r--r-- | src/cli/tls_server.cpp | 39 | ||||
-rw-r--r-- | src/lib/utils/http_util/http_util.cpp | 47 | ||||
-rw-r--r-- | src/lib/utils/http_util/info.txt | 4 |
5 files changed, 139 insertions, 4 deletions
diff --git a/src/build-data/os/windows.txt b/src/build-data/os/windows.txt index 679dfe651..2a5e9bd96 100644 --- a/src/build-data/os/windows.txt +++ b/src/build-data/os/windows.txt @@ -23,6 +23,7 @@ rtlsecurezeromemory stl_filesystem_msvc threads filesystem +sockets </target_features> <aliases> diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp index 642e60373..4625ca3f1 100644 --- a/src/cli/tls_client.cpp +++ b/src/cli/tls_client.cpp @@ -1,6 +1,7 @@ /* * (C) 2014,2015 Jack Lloyd * 2016 Matthias Gierlings +* 2017 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -21,6 +22,28 @@ #include <string> #include <memory> +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) +#include <winsock2.h> +#include <WS2tcpip.h> + +int close(int fd) + { + return ::closesocket(fd); + } + +int read(int s, void* buf, size_t len) + { + return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0); + } + +int send(int s, const uint8_t* buf, size_t len, int flags) + { + return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), flags); + } + +#define STDIN_FILENO _fileno(stdin) +typedef size_t ssize_t; +#else #include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> @@ -30,6 +53,7 @@ #include <unistd.h> #include <errno.h> #include <fcntl.h> +#endif #if !defined(MSG_NOSIGNAL) #define MSG_NOSIGNAL 0 @@ -45,7 +69,31 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks TLS_Client() : Command("tls_client host --port=443 --print-certs --policy= " "--tls1.0 --tls1.1 --tls1.2 " - "--session-db= --session-db-pass= --next-protocols= --type=tcp") {} + "--session-db= --session-db-pass= --next-protocols= --type=tcp") + { +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + WSAData wsa_data; + WORD wsa_version = MAKEWORD(2, 2); + + if(::WSAStartup(wsa_version, &wsa_data) != 0) + { + throw CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError())); + } + + if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) + { + ::WSACleanup(); + throw CLI_Error("Could not find a usable version of Winsock.dll"); + } +#endif + } + + ~TLS_Client() + { +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + ::WSACleanup(); +#endif + } void go() override { @@ -343,7 +391,7 @@ class TLS_Client final : public Command, public Botan::TLS::Callbacks static void dgram_socket_write(int sockfd, const uint8_t buf[], size_t length) { - int r = send(sockfd, buf, length, MSG_NOSIGNAL); + int r = ::send(sockfd, buf, length, MSG_NOSIGNAL); if(r == -1) { diff --git a/src/cli/tls_server.cpp b/src/cli/tls_server.cpp index 41e131dce..22e592f29 100644 --- a/src/cli/tls_server.cpp +++ b/src/cli/tls_server.cpp @@ -1,6 +1,7 @@ /* * TLS echo server using BSD sockets * (C) 2014 Jack Lloyd +* 2017 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -15,6 +16,17 @@ #include <list> +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) +#include <winsock2.h> +#include <WS2tcpip.h> + +// definitions in tls_client.cpp +int close(int fd); +int read(int s, void* buf, size_t len); +int send(int s, const uint8_t* buf, size_t len, int flags); + +typedef size_t ssize_t; +#else #include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> @@ -23,6 +35,7 @@ #include <unistd.h> #include <errno.h> #include <fcntl.h> +#endif #if !defined(MSG_NOSIGNAL) #define MSG_NOSIGNAL 0 @@ -33,7 +46,31 @@ namespace Botan_CLI { class TLS_Server final : public Command { public: - TLS_Server() : Command("tls_server cert key --port=443 --type=tcp --policy=") {} + TLS_Server() : Command("tls_server cert key --port=443 --type=tcp --policy=") + { +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + WSAData wsa_data; + WORD wsa_version = MAKEWORD(2, 2); + + if(::WSAStartup(wsa_version, &wsa_data) != 0) + { + throw CLI_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError())); + } + + if(LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) + { + ::WSACleanup(); + throw CLI_Error("Could not find a usable version of Winsock.dll"); + } +#endif + } + + ~TLS_Server() + { +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + ::WSACleanup(); +#endif + } void go() override { diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp index f714c1bca..36a689c97 100644 --- a/src/lib/utils/http_util/http_util.cpp +++ b/src/lib/utils/http_util/http_util.cpp @@ -1,6 +1,7 @@ /* * Sketchy HTTP client * (C) 2013,2016 Jack Lloyd +* 2017 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -22,11 +23,33 @@ #include <boost/asio.hpp> #elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + #include <winsock2.h> + #include <WS2tcpip.h> + +int close(int fd) + { + return ::closesocket(fd); + } + +int read(int s, void* buf, size_t len) + { + return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0); + } + +int write(int s, const char* buf, size_t len) + { + return ::send(s, reinterpret_cast<const char*>(buf), static_cast<int>(len), 0); + } + +typedef size_t ssize_t; +#else #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <netinet/in.h> +#endif #else //#warning "No network support enabled in http_util" #endif @@ -63,6 +86,22 @@ std::string http_transact(const std::string& hostname, return oss.str(); #elif defined(BOTAN_TARGET_OS_HAS_SOCKETS) +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + WSAData wsa_data; + WORD wsa_version = MAKEWORD(2, 2); + + if (::WSAStartup(wsa_version, &wsa_data) != 0) + { + throw HTTP_Error("WSAStartup() failed: " + std::to_string(WSAGetLastError())); + } + + if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) + { + ::WSACleanup(); + throw HTTP_Error("Could not find a usable version of Winsock.dll"); + } +#endif + hostent* host_addr = ::gethostbyname(hostname.c_str()); uint16_t port = 80; @@ -74,7 +113,13 @@ std::string http_transact(const std::string& hostname, struct socket_raii { socket_raii(int fd) : m_fd(fd) {} - ~socket_raii() { ::close(m_fd); } + ~socket_raii() + { + ::close(m_fd); +#if defined(BOTAN_TARGET_OS_IS_WINDOWS) + ::WSACleanup(); +#endif + } int m_fd; }; diff --git a/src/lib/utils/http_util/info.txt b/src/lib/utils/http_util/info.txt index fe9fc3ea7..63e569f64 100644 --- a/src/lib/utils/http_util/info.txt +++ b/src/lib/utils/http_util/info.txt @@ -1,3 +1,7 @@ <defines> HTTP_UTIL -> 20131128 </defines> + +<libs> +windows -> Ws2_32.lib +</libs>
\ No newline at end of file |