From fd62679c25b09a9c46ae8bd2d29d9c1dcededb1a Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Sun, 4 Sep 2022 19:50:47 +0200 Subject: [PATCH] SSL Test Pt. 1 --- socket/include/socket.h | 20 ++++++++++++++ socket/socket.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/socket/include/socket.h b/socket/include/socket.h index 784324f..5e54946 100644 --- a/socket/include/socket.h +++ b/socket/include/socket.h @@ -17,15 +17,35 @@ #include #include +#ifdef IBM +#endif +#ifdef APL +#endif +#ifdef LIN +#include +#include +#include +#include +#include +#endif + class Socket { private: +#ifdef LIN + SSL *ssl; + int sock; +#endif char lastPath[513] = ""; char lastHash[2 * MD5LEN + 1] = ""; std::mutex wsLock; std::string url; std::function toLog; +#ifdef LIN + void logSSL(); +#endif + public: explicit Socket(std::string url, std::function toLog); diff --git a/socket/socket.cpp b/socket/socket.cpp index 3b0d546..0c06785 100644 --- a/socket/socket.cpp +++ b/socket/socket.cpp @@ -6,6 +6,52 @@ Socket::Socket(std::string url, std::function toLog) #ifdef IBM // WSA INIT int a = 0; +#endif +#ifdef APL + int a = 0; +#endif +#ifdef LIN + int s; + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + toLog("Error creating socket."); + return; + } + struct sockaddr_in sa; + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = + inet_addr("136.243.123.153"); // address of german-airlines.de + sa.sin_port = htons(443); + socklen_t socklen = sizeof(sa); + if (connect(s, (struct sockaddr *)&sa, socklen)) { + toLog("Error connecting to server."); + return; + } + SSL_library_init(); + SSLeay_add_ssl_algorithms(); + SSL_load_error_strings(); + const SSL_METHOD *meth = TLSv1_2_client_method(); + SSL_CTX *ctx = SSL_CTX_new(meth); + ssl = SSL_new(ctx); + if (!ssl) { + toLog("Error creating SSL."); + logSSL(); + return; + } + sock = SSL_get_fd(ssl); + SSL_set_fd(ssl, s); + int err = SSL_connect(ssl); + if (err <= 0) { + std::ostringstream msg; + msg << "Error creating SSL connection. err=" << err; + toLog(msg.str()); + logSSL(); + return; + } + std::ostringstream msg; + msg << "SSL connection using " << SSL_get_cipher(ssl); + toLog(msg.str()); #endif // PLATFORM AGNOSTIC } @@ -44,3 +90,16 @@ void Socket::sendData(data d) // SEND } } + +#ifdef LIN +void Socket::logSSL() +{ + int err; + while ((err = ERR_get_error())) { + char *str = ERR_error_string(err, 0); + if (!str) + return; + toLog(str); + } +} +#endif \ No newline at end of file