Control: retitle -1 fldigi: FTBFS with mbedtls v4.1 (drop mbedtls) Control: severity -1 serious Control: tags -1 patch
Even if we decide to keep mbedtls in forky, you will have to get the package to build with v4.1 (in experimental). I have posted a patch to the upstream list to add OpenSSL as a crypto provider and am including it here with the necessary packaging changes.
>From 801d203d865a63344deb4e0de422ee0204cfbf05 Mon Sep 17 00:00:00 2001 From: Bastian Germann <[email protected]> Date: Wed, 2 Dec 2026 21:24:12 +0100 Subject: [PATCH] Build with OpenSSL (Closes: #1155109) --- debian/control | 2 +- debian/patches/openssl.patch | 1988 ++++++++++++++++++++++++++++++++++ debian/patches/series | 1 + debian/rules | 3 +- 4 files changed, 1991 insertions(+), 3 deletions(-) create mode 100644 debian/patches/openssl.patch diff --git a/debian/control b/debian/control index 5013b1a0..00071a4c 100644 --- a/debian/control +++ b/debian/control @@ -12,7 +12,7 @@ Build-Depends: libflxmlrpc-dev, libgpiod-dev, libhamlib-dev, - libmbedtls-dev (>= 2.16), + libssl-dev, libpng-dev, libpulse-dev, libsamplerate0-dev, diff --git a/debian/patches/openssl.patch b/debian/patches/openssl.patch new file mode 100644 index 00000000..90ce6d4a --- /dev/null +++ b/debian/patches/openssl.patch @@ -0,0 +1,1988 @@ +From: Bastian Germann <[email protected]> +Date: Wed, 2 Dec 2026 21:01:28 +0100 +Forwarded: [email protected] +Subject: [PATCH] Add optional OpenSSL TLS crypto provider + +Introduce a TLS "crypto provider" abstraction for the built-in HTTPS +client so it can be built against either mbedtls (default, bundled or +system lib) or the system OpenSSL libraries. + +* New src/include/https_provider.h defines the HttpsConnection + interface and the https_connection_new() factory. +* network.cxx (the Url class) now uses the provider instead of calling + mbedtls directly; the mbedtls TLS code moved to network/https_mbedtls.cxx. +* New network/https_openssl.cxx is the OpenSSL backend (system libssl/ + libcrypto only, never bundled), verifying against the same bundled CA + store and mirroring the legacy MBEDTLS_SSL_VERIFY_OPTIONAL behavior. +* configure.ac: opt-in --with-openssl (hard error if pkg-config cannot + find openssl >= 1.1.0); when selected, mbedtls is neither detected nor + built. Build summary now reports the crypto provider. +* m4/build.m4 and src/Makefile.am: exactly one provider backend is + compiled and linked (mbedtls by default, OpenSSL with --with-openssl). +--- + configure.ac | 55 ++- + m4/build.m4 | 12 +- + src/Makefile.am | 23 +- + src/include/https_provider.h | 99 ++++ + src/include/network.h | 46 +- + src/network/https_mbedtls.cxx | 258 ++++++++++ + src/network/https_openssl.cxx | 332 +++++++++++++ + src/network/network.cxx | 861 ++++------------------------------ + 8 files changed, 862 insertions(+), 824 deletions(-) + create mode 100644 src/include/https_provider.h + create mode 100644 src/network/https_mbedtls.cxx + create mode 100644 src/network/https_openssl.cxx + +diff --git a/configure.ac b/configure.ac +index dac82b41..86a558e1 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -296,13 +296,54 @@ AC_FLDIGI_PKG_CHECK([flxmlrpc], [flxmlrpc >= 1.0.1], + [yes], [yes], [use external flxmlrpc library @<:@autodetect@:>@], + [ENABLE_FLXMLRPC]) + ++### libopenssl (optional TLS crypto provider; never bundled) ++# Opt-in only: mbedtls remains the default provider. When selected, the ++# system OpenSSL libraries (libssl/libcrypto) are used for the HTTPS client ++# and mbedtls is not built or linked at all. ++# Set ac_cv_openssl to yes/no ++# Define USE_OPENSSL in config.h ++# Substitute OPENSSL_CFLAGS and OPENSSL_LIBS in Makefile ++# Set ENABLE_OPENSSL Makefile conditional ++AC_ARG_WITH([openssl], ++ [AS_HELP_STRING([--with-openssl], ++ [use the system OpenSSL libraries as the TLS crypto provider instead of mbedtls @<:@default: no@:>@])], ++ [case "${withval}" in ++ yes|no) ac_cv_want_openssl="${withval}" ;; ++ *) AC_MSG_ERROR([bad value "${withval}" for --with-openssl]) ;; ++ esac], ++ [ac_cv_want_openssl=no]) ++ ++if test "x$ac_cv_want_openssl" = "xyes"; then ++ PKG_CHECK_MODULES([OPENSSL], [openssl >= 1.1.0], ++ [ac_cv_openssl=yes], ++ [AC_MSG_ERROR([*** OpenSSL crypto provider requested (--with-openssl) but pkg-config ++*** could not find 'openssl >= 1.1.0'. The OpenSSL provider is never bundled; ++*** install the OpenSSL development package for this system.])]) ++else ++ ac_cv_openssl=no ++fi ++if test "x$ac_cv_want_openssl" = "xyes"; then ++ AC_DEFINE([USE_OPENSSL], 1, [Define to 1 to use the OpenSSL crypto provider]) ++else ++ AC_DEFINE([USE_OPENSSL], 0, [Define to 1 to use the OpenSSL crypto provider]) ++fi ++AM_CONDITIONAL([ENABLE_OPENSSL], [test "x$ac_cv_want_openssl" = "xyes"]) ++ + ### libmbedtls + # Set ac_cv_mbedtls to yes/no + # Define USE_LIBMBEDTLS in config.h + # Substitute LIBMBEDTLS_CFLAGS and LIBMBEDTLS_LIBS in Makefile +-AC_FLDIGI_PKG_CHECK([libmbedtls], [libmbedtls >= 2.16.0], +- [yes], [yes], [use mbedtls libraries @<:@autodetect@:>@], +- [ENABLE_LIBMBEDTLS]) ++# Skipped entirely when the OpenSSL provider is selected (--with-openssl), ++# since mbedtls is then not needed by anything else in fldigi. ++if test "x$ac_cv_want_openssl" != "xyes"; then ++ AC_FLDIGI_PKG_CHECK([libmbedtls], [libmbedtls >= 2.16.0], ++ [yes], [yes], [use mbedtls libraries @<:@autodetect@:>@], ++ [ENABLE_LIBMBEDTLS]) ++else ++ ac_cv_libmbedtls=no ++ AC_DEFINE([USE_LIBMBEDTLS], 0, [Define to 1 if we are using libmbedtls]) ++ AM_CONDITIONAL([ENABLE_LIBMBEDTLS], [test "x$ac_cv_libmbedtls" = "xyes"]) ++fi + + ### libintl + # Substitute INTL_CFLAGS in Makefile +@@ -374,6 +415,12 @@ if test "x$ac_cv_libmbedtls" = "xyes"; then + ac_mbedtls_report="external lib"; + fi + ++ac_crypto_provider_report="mbedtls ($ac_mbedtls_report)"; ++ ++if test "x$ac_cv_want_openssl" = "xyes"; then ++ ac_crypto_provider_report="OpenSSL (system library, not bundled)"; ++fi ++ + if test "x$ac_cv_want_fldigi" = "xyes"; then + AC_MSG_RESULT([ fldigi build options: + +@@ -382,7 +429,7 @@ if test "x$ac_cv_want_fldigi" = "xyes"; then + pulseaudio .................. $ac_cv_pulseaudio + libgpiod .................... $ac_cv_libgpiod + +- libmbedtls .................. $ac_mbedtls_report ++ crypto provider ............. $ac_crypto_provider_report + flxmlrpc .................... $ac_xmlrpc_report + + hamlib ...................... $ac_cv_hamlib +diff --git a/m4/build.m4 b/m4/build.m4 +index 305234a6..8546d9f1 100644 +--- a/m4/build.m4 ++++ b/m4/build.m4 +@@ -12,19 +12,21 @@ AC_DEFUN([AC_FLDIGI_BUILD_INFO], [ + if test "x$ac_cv_flxmlrpc" != "xyes"; then + FLDIGI_BUILD_CPPFLAGS="$FLDIGI_BUILD_CPPFLAGS -I\$(srcdir)/xmlrpcpp" + fi +- if test "x$ac_cv_libmbedtls" != "xyes"; then +- FLDIGI_BUILD_CPPFLAGS="$FLDIGI_BUILD_CPPFLAGS -I\$(srcdir)/mbedtls" ++ if test "x$ac_cv_want_openssl" != "xyes"; then ++ if test "x$ac_cv_libmbedtls" != "xyes"; then ++ FLDIGI_BUILD_CPPFLAGS="$FLDIGI_BUILD_CPPFLAGS -I\$(srcdir)/mbedtls" ++ fi + fi + # CXXFLAGS + FLDIGI_BUILD_CXXFLAGS="\ + $PORTAUDIO_CFLAGS $FLTK_CFLAGS $X_CFLAGS \ + $SNDFILE_CFLAGS $SAMPLERATE_CFLAGS $PULSEAUDIO_CFLAGS \ + $HAMLIB_CFLAGS $PNG_CFLAGS $XMLRPC_CFLAGS $MAC_UNIVERSAL_CFLAGS \ +-$LIBMBEDTLS_CFLAGS $LIBGPIOD_CFLAGS \ ++$LIBMBEDTLS_CFLAGS $LIBGPIOD_CFLAGS $OPENSSL_CFLAGS \ + $INTL_CFLAGS $PTW32_CFLAGS $BFD_CFLAGS -pipe -Wall -fexceptions $OPT_CFLAGS $DEBUG_CFLAGS" + + # CFLAGS +- FLDIGI_BUILD_CFLAGS="$LIBMBEDTLS_CFLAGS $LIBGPIOD_CFLAGS -pipe -Wall -fexceptions" ++ FLDIGI_BUILD_CFLAGS="$LIBMBEDTLS_CFLAGS $LIBGPIOD_CFLAGS $OPENSSL_CFLAGS -pipe -Wall -fexceptions" + + if test "x$target_darwin" = "xno"; then + FLDIGI_BUILD_CXXFLAGS="$FLDIGI_BUILD_CXXFLAGS --param=max-vartrack-size=0" +@@ -54,7 +56,7 @@ $INTL_CFLAGS $PTW32_CFLAGS $BFD_CFLAGS -pipe -Wall -fexceptions $OPT_CFLAGS $DEB + $PORTAUDIO_LIBS $FLTK_LIBS $X_LIBS \ + $SNDFILE_LIBS $SAMPLERATE_LIBS $PULSEAUDIO_LIBS \ + $HAMLIB_LIBS $PNG_LIBS $XMLRPC_LIBS $INTL_LIBS $PTW32_LIBS $BFD_LIBS \ +-$EXTRA_LIBS $FLXMLRPC_LIBS $LIBMBEDTLS_LIBS $LIBUDEV_LIBS $LIBGPIOD_LIBS" ++$EXTRA_LIBS $FLXMLRPC_LIBS $LIBMBEDTLS_LIBS $LIBUDEV_LIBS $LIBGPIOD_LIBS $OPENSSL_LIBS" + + # CPPFLAGS + FLARQ_BUILD_CPPFLAGS="-I\$(srcdir) -I\$(srcdir)/include -I\$(srcdir)/fileselector \ +diff --git a/src/Makefile.am b/src/Makefile.am +index 16b03587..c989a07d 100644 +--- a/src/Makefile.am ++++ b/src/Makefile.am +@@ -243,11 +243,22 @@ LIBMBEDTLS_SRC = \ + include/mbedtls/x509.h \ + include/mbedtls/xtea.h + ++# TLS crypto providers for the HTTPS client. Exactly one of these is ++# compiled, selected at configure time (see the conditionals below): ++# * mbedtls default (bundled or system libmbedtls) ++# * OpenSSL opt-in via --with-openssl, never bundled ++HTTPS_MBEDTLS_SRC = \ ++ network/https_mbedtls.cxx ++ ++HTTPS_OPENSSL_SRC = \ ++ network/https_openssl.cxx ++ + + EXTRA_fldigi_SOURCES = \ + $(HAMLIB_SRC) $(XMLRPC_SRC) $(FLDIGI_WIN32_RES_SRC) \ + $(COMMON_WIN32_RES_SRC) $(BENCHMARK_SRC) $(REGEX_SRC) $(STACK_SRC) \ +- $(MINGW32_SRC) $(NLS_SRC) $(XMLRPCPP_SRC) $(LIBMBEDTLS_SRC) ++ $(MINGW32_SRC) $(NLS_SRC) $(XMLRPCPP_SRC) $(LIBMBEDTLS_SRC) \ ++ $(HTTPS_MBEDTLS_SRC) $(HTTPS_OPENSSL_SRC) + + EXTRA_flarq_SOURCES = $(FLARQ_WIN32_RES_SRC) $(COMMON_WIN32_RES_SRC) $(XMLRPCPP_SRC) + +@@ -270,6 +281,14 @@ flarq_CFLAGS += @FLXMLRPC_CFLAGS@ + flarq_LDFLAGS += @FLXMLRPC_LIBS@ + endif + ++if ENABLE_OPENSSL ++fldigi_SOURCES += $(HTTPS_OPENSSL_SRC) ++fldigi_CPPFLAGS += @OPENSSL_CFLAGS@ ++fldigi_CXXFLAGS += @OPENSSL_CFLAGS@ ++fldigi_CFLAGS += @OPENSSL_CFLAGS@ ++fldigi_LDFLAGS += @OPENSSL_LIBS@ ++else ++fldigi_SOURCES += $(HTTPS_MBEDTLS_SRC) + if !ENABLE_LIBMBEDTLS + fldigi_SOURCES += $(LIBMBEDTLS_SRC) + else +@@ -278,6 +297,7 @@ fldigi_CXXFLAGS += @LIBMBEDTLS_CFLAGS@ + fldigi_CFLAGS += @LIBMBEDTLS_CFLAGS@ + fldigi_LDFLAGS += @LIBMBEDTLS_LIBS@ + endif ++endif + + if ENABLE_HAMLIB + fldigi_SOURCES += $(HAMLIB_SRC) +@@ -831,6 +851,7 @@ EXTRA_fldigi_SOURCES += \ + include/hid_win.h \ + include/htmlstrings.h \ + include/https.h \ ++ include/https_provider.h \ + include/icons.h \ + include/ifkp.h \ + include/interleave.h \ +diff --git a/src/include/https_provider.h b/src/include/https_provider.h +new file mode 100644 +index 00000000..caea1be3 +--- /dev/null ++++ b/src/include/https_provider.h +@@ -0,0 +1,99 @@ ++// ---------------------------------------------------------------------------- ++// Copyright (C) 2026 ++// The fldigi project ++// ++// This file is part of fldigi ++// ++// fldigi is free software; you can redistribute it and/or modify ++// it under the terms of the GNU General Public License as published by ++// the Free Software Foundation; either version 3 of the License, or ++// (at your option) any later version. ++// ++// fldigi is distributed in the hope that it will be useful, ++// but WITHOUT ANY WARRANTY; without even the implied warranty of ++// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++// GNU General Public License for more details. ++// ++// You should have received a copy of the GNU General Public License ++// along with this program. If not, see <http://www.gnu.org/licenses/>. ++// ---------------------------------------------------------------------------- ++ ++#ifndef HTTPS_PROVIDER_H_ ++#define HTTPS_PROVIDER_H_ ++ ++#include <sys/types.h> ++#include <string> ++ ++// ---------------------------------------------------------------------------- ++// TLS "crypto provider" abstraction for fldigi's built-in HTTPS client. ++// ++// fldigi is built with exactly one of the following providers: ++// ++// * mbedtls - the default. May be a system library (libmbedtls) or the ++// bundled copy under src/mbedtls. ++// ++// * OpenSSL - opt-in via `configure --with-openssl`. Never bundled; the ++// system OpenSSL libraries are always linked. ++// ++// Both backends implement https_connection_new() below. The backend is ++// selected at configure/build time (src/Makefile.am compiles exactly one of ++// network/https_mbedtls.cxx or network/https_openssl.cxx), so the rest of ++// fldigi is agnostic to the provider that is in use. ++// ---------------------------------------------------------------------------- ++ ++class HttpsConnection ++{ ++public: ++ virtual ~HttpsConnection() {} ++ ++ // Establish the TCP connection to host:port and complete the TLS ++ // handshake, verifying the peer certificate against the CA store ++ // bundled in network/ca_cert.cxx. Returns 0 on success, non-zero on ++ // failure (see error()/errstr()). ++ virtual int connect() = 0; ++ ++ // Write up to len bytes over the TLS channel. ++ // Returns: number of bytes written (> 0), HTTPS_IO_RETRY if the call ++ // should be retried (the TLS layer needs more network activity), ++ // or <= 0 / < HTTPS_IO_RETRY on error. ++ virtual ssize_t write(const void *buf, size_t len) = 0; ++ ++ // Read up to len bytes over the TLS channel. ++ // Returns: number of bytes read (> 0), 0 on a clean end of stream, ++ // HTTPS_IO_RETRY if the call should be retried, or an error value ++ // < HTTPS_IO_RETRY on failure. ++ virtual ssize_t read(void *buf, size_t len) = 0; ++ ++ // Provider specific last error code and a human readable description. ++ virtual int error() const = 0; ++ virtual const char *errstr() const = 0; ++ ++ // Free form text describing the outcome of the peer certificate ++ // verification; "" if verification succeeded. Certificate problems are ++ // reported but, mirroring the legacy mbedtls MBEDTLS_SSL_VERIFY_OPTIONAL ++ // behavior, never abort the handshake. ++ virtual const char *verify_info() const = 0; ++ ++ // Shut down and release all provider resources. Safe to call once, and ++ // the destructor calls it automatically. ++ virtual void close() = 0; ++}; ++ ++// Returned by read()/write() when the TLS layer needs more network activity ++// before the operation can proceed; the caller should simply retry. ++#define HTTPS_IO_RETRY (-1) ++ ++// Create (but do not connect) a TLS connection to host:port. ++// pers - provider specific seed / personalization string ++// (used only by the mbedtls DRBG; ignored by OpenSSL) ++// timeout - reserved for future use; currently ignored by both backends ++// (matching the legacy behavior of the HTTPS client) ++// On failure returns NULL and, if err is not NULL, stores a descriptive ++// error message in it. ++HttpsConnection *https_connection_new(const std::string &host, ++ const std::string &port, ++ const std::string &pers, ++ double timeout, ++ std::string *err = 0); ++ ++#endif // HTTPS_PROVIDER_H_ +diff --git a/src/include/network.h b/src/include/network.h +index f1315769..66fa8a79 100644 +--- a/src/include/network.h ++++ b/src/include/network.h +@@ -31,16 +31,6 @@ + #include <string> + #include <cmath> + +-#include "mbedtls/config.h" +-#include "mbedtls/net.h" +-#include "mbedtls/net_sockets.h" +-#include "mbedtls/debug.h" +-#include "mbedtls/ssl.h" +-#include "mbedtls/entropy.h" +-#include "mbedtls/ctr_drbg.h" +-#include "mbedtls/error.h" +-#include "mbedtls/certs.h" +- + extern bool get_http(const std::string& url, std::string& reply, double timeout = 0.0); + extern bool post_http(const std::string& url, const std::string& apikey, int profile_id, std::string adifdata, std::string& reply, double timeout = 0.0); + extern int test_api_key(std::string url, const std::string& apiKey, double timeout = 0.0); +@@ -51,10 +41,11 @@ extern size_t ca_crt_rsa_size; + //---------------------------------------------------------------------- + //#define DEBUG_LEVEL 1 + +-#define MBEDTLS_EXIT_SUCCESS 0 +-#define MBEDTLS_EXIT_FAILURE 1 +-#define MBEDTLS_DEBUG_C +-#define MBEDTLS_CHECK_PARAMS ++#define HTTPS_EXIT_SUCCESS 0 ++#define HTTPS_EXIT_FAILURE 1 ++/* Legacy aliases, kept for callers such as fl_digi.cxx */ ++#define MBEDTLS_EXIT_SUCCESS HTTPS_EXIT_SUCCESS ++#define MBEDTLS_EXIT_FAILURE HTTPS_EXIT_FAILURE + + #define H_FIELD_SIZE 512 + #define H_READ_SIZE 2048 +@@ -74,19 +65,6 @@ typedef struct + + } HTTP_HEADER; + +-typedef struct +-{ +- bool verify; +- +- mbedtls_net_context ssl_fd; +- mbedtls_entropy_context entropy; +- mbedtls_ctr_drbg_context ctr_drbg; +- mbedtls_ssl_context ssl; +- mbedtls_ssl_config conf; +- mbedtls_x509_crt cacert; +- +-} HTTP_SSL; +- + typedef struct { + + bool https; +@@ -102,7 +80,6 @@ typedef struct + + HTTP_HEADER request; + HTTP_HEADER response; +- HTTP_SSL tls; + + long length; + char r_buf[H_READ_SIZE]; +@@ -133,15 +110,6 @@ class Url { + char err_string[1024]; + char buf[4096]; + +- mbedtls_net_context server_fd; +- uint32_t flags; +- +- mbedtls_entropy_context entropy; +- mbedtls_ctr_drbg_context ctr_drbg; +- mbedtls_ssl_context ssl; +- mbedtls_ssl_config conf; +- mbedtls_x509_crt cacert; +- + bool _debug; + static int _rotate_log; + +@@ -153,6 +121,10 @@ class Url { + int https_get(std::string &response); + int http_post(std::string apikey, int profile_id, std::string adifdata, std::string &response); + int http_test(std::string apikey); ++ ++ // One TLS exchange (GET or POST) over the selected crypto provider. ++ // Returns 0 on success and fills response, non-zero on failure. ++ int https_exchange(const std::string &request, std::string &response); + int https_post(std::string apikey, int profile_id, std::string adifdata, std::string &response); + int https_test(std::string apikey); + +diff --git a/src/network/https_mbedtls.cxx b/src/network/https_mbedtls.cxx +new file mode 100644 +index 00000000..46859374 +--- /dev/null ++++ b/src/network/https_mbedtls.cxx +@@ -0,0 +1,258 @@ ++// ---------------------------------------------------------------------------- ++// https_mbedtls.cxx ++// ++// mbedtls TLS "crypto provider" for fldigi's HTTPS client. ++// ++// The TLS client code that used to live inside network.cxx was moved here so ++// that it can be swapped for the OpenSSL provider (network/https_openssl.cxx) ++// at build time. Only one of the two provider sources is ever compiled. ++// ++// This file is part of fldigi. ++// ++// fldigi is free software: you can redistribute it and/or modify ++// it under the terms of the GNU General Public License as published by ++// the Free Software Foundation, either version 3 of the License, or ++// (at your option) any later version. ++// ++// fldigi is distributed in the hope that it will be useful, ++// but WITHOUT ANY WARRANTY; without even the implied warranty of ++// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++// GNU General Public License for more details. ++// ++// You should have received a copy of the GNU General Public License ++// along with fldigi. If not, see <http://www.gnu.org/licenses/>. ++// ---------------------------------------------------------------------------- ++ ++#include <config.h> ++ ++#include <cstdio> ++#include <cstring> ++#include <string> ++ ++#include "mbedtls/config.h" ++#include "mbedtls/net.h" ++#include "mbedtls/net_sockets.h" ++#include "mbedtls/debug.h" ++#include "mbedtls/ssl.h" ++#include "mbedtls/entropy.h" ++#include "mbedtls/ctr_drbg.h" ++#include "mbedtls/error.h" ++#include "mbedtls/certs.h" ++ ++#include "https_provider.h" ++#include "network.h" ++ ++class HttpsMbedTlsConnection : public HttpsConnection ++{ ++public: ++ HttpsMbedTlsConnection(const std::string &host, const std::string &port, ++ const std::string &pers) ++ : _host(host), _port(port), _pers(pers), _err(0) ++ { ++ _errbuf[0] = 0; ++ _verify[0] = 0; ++ mbedtls_net_init(&_fd); ++ mbedtls_ssl_init(&_ssl); ++ mbedtls_ssl_config_init(&_conf); ++ mbedtls_x509_crt_init(&_cacert); ++ mbedtls_ctr_drbg_init(&_ctr_drbg); ++ mbedtls_entropy_init(&_entropy); ++ _ctx_inited = true; ++ } ++ ++ ~HttpsMbedTlsConnection() { close(); } ++ ++ int connect() override; ++ ssize_t write(const void *buf, size_t len) override; ++ ssize_t read(void *buf, size_t len) override; ++ ++ int error() const override { return _err; } ++ const char *errstr() const override { return _errbuf; } ++ const char *verify_info() const override { return _verify; } ++ ++ void close() override ++ { ++ if (!_ctx_inited) ++ return; ++ mbedtls_net_free(&_fd); ++ mbedtls_x509_crt_free(&_cacert); ++ mbedtls_ssl_free(&_ssl); ++ mbedtls_ssl_config_free(&_conf); ++ mbedtls_ctr_drbg_free(&_ctr_drbg); ++ mbedtls_entropy_free(&_entropy); ++ _ctx_inited = false; ++ } ++ ++private: ++ std::string _host; ++ std::string _port; ++ std::string _pers; ++ ++ int _err; ++ char _errbuf[256]; ++ char _verify[512]; ++ ++ bool _ctx_inited; ++ ++ mbedtls_net_context _fd; ++ mbedtls_entropy_context _entropy; ++ mbedtls_ctr_drbg_context _ctr_drbg; ++ mbedtls_ssl_context _ssl; ++ mbedtls_ssl_config _conf; ++ mbedtls_x509_crt _cacert; ++}; ++ ++int HttpsMbedTlsConnection::connect() ++{ ++ int ret = 0; ++ ++ /* ++ * 0. Initialize the RNG and seed it ++ */ ++ if ((ret = mbedtls_ctr_drbg_seed( ++ &_ctr_drbg, ++ mbedtls_entropy_func, ++ &_entropy, ++ (const unsigned char *)_pers.c_str(), ++ _pers.length())) != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ctr_drbg_seed returned %d\n", ret); ++ goto exit; ++ } ++ /* ++ * 0. Load the bundled CA root certificates ++ */ ++ ca_crt_rsa[ca_crt_rsa_size - 1] = 0; ++ ret = mbedtls_x509_crt_parse(&_cacert, (uint8_t *)ca_crt_rsa, ca_crt_rsa_size); ++ if (ret < 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_x509_crt_parse returned -0x%x\n", -ret); ++ goto exit; ++ } ++ /* ++ * 1. Start the connection ++ */ ++ if ((ret = mbedtls_net_connect( ++ &_fd, ++ _host.c_str(), ++ _port.c_str(), ++ MBEDTLS_NET_PROTO_TCP)) != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_net_connect returned %d\n", ret); ++ goto exit; ++ } ++ /* ++ * 2. Set up the SSL/TLS structure ++ */ ++ if ((ret = mbedtls_ssl_config_defaults(&_conf, ++ MBEDTLS_SSL_IS_CLIENT, ++ MBEDTLS_SSL_TRANSPORT_STREAM, ++ MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_config_defaults returned %d\n", ret); ++ goto exit; ++ } ++ /* ++ * 3. More setup ++ */ ++ /* OPTIONAL is not optimal for security, but makes interop easier in ++ * this simplified client. */ ++ mbedtls_ssl_conf_authmode(&_conf, MBEDTLS_SSL_VERIFY_OPTIONAL); ++ mbedtls_ssl_conf_ca_chain(&_conf, &_cacert, NULL); ++ mbedtls_ssl_conf_rng(&_conf, mbedtls_ctr_drbg_random, &_ctr_drbg); ++ ++ if ((ret = mbedtls_ssl_setup(&_ssl, &_conf)) != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_setup returned %d\n", ret); ++ goto exit; ++ } ++ ++ if ((ret = mbedtls_ssl_set_hostname(&_ssl, _host.c_str())) != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_set_hostname returned %d\n", ret); ++ goto exit; ++ } ++ ++ mbedtls_ssl_set_bio(&_ssl, &_fd, mbedtls_net_send, mbedtls_net_recv, NULL); ++ /* ++ * 4. Handshake ++ */ ++ while ((ret = mbedtls_ssl_handshake(&_ssl)) != 0) { ++ if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_handshake returned -0x%x\n", -ret); ++ goto exit; ++ } ++ } ++ /* ++ * 5. Verify the peer certificate (VERIFY_OPTIONAL: report, do not abort) ++ */ ++ { ++ uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl); ++ if (flags != 0) ++ mbedtls_x509_crt_verify_info(_verify, sizeof(_verify), " ! ", flags); ++ } ++ ++ ret = 0; ++ _err = 0; ++ ++exit: ++ if (ret != 0) { ++ char error_buf[100]; ++#ifdef MBEDTLS_ERROR_C ++ mbedtls_strerror(_err, error_buf, sizeof(error_buf)); ++ snprintf(_errbuf + strlen(_errbuf), ++ sizeof(_errbuf) - strlen(_errbuf), ++ "%s", error_buf); ++#endif ++ } ++ return (_err == 0) ? ret : _err; ++} ++ ++ssize_t HttpsMbedTlsConnection::write(const void *buf, size_t len) ++{ ++ int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *)buf, len); ++ if (ret > 0) ++ return ret; ++ if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) ++ return HTTPS_IO_RETRY; ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_write returned %d\n", ret); ++ return ret; ++} ++ ++ssize_t HttpsMbedTlsConnection::read(void *buf, size_t len) ++{ ++ int ret = mbedtls_ssl_read(&_ssl, (unsigned char *)buf, len); ++ if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) ++ return HTTPS_IO_RETRY; ++ if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) ++ return 0; ++ if (ret < 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "mbedtls_ssl_read returned %d\n", ret); ++ } ++ return ret; ++} ++ ++HttpsConnection *https_connection_new(const std::string &host, ++ const std::string &port, ++ const std::string &pers, ++ double timeout, ++ std::string *err) ++{ ++ (void)timeout; // reserved; both backends currently connect blocking ++ HttpsMbedTlsConnection *conn = new (std::nothrow) HttpsMbedTlsConnection(host, port, pers); ++ if (!conn && err) ++ *err = "out of memory creating mbedtls TLS provider"; ++ return conn; ++} +diff --git a/src/network/https_openssl.cxx b/src/network/https_openssl.cxx +new file mode 100644 +index 00000000..ee050515 +--- /dev/null ++++ b/src/network/https_openssl.cxx +@@ -0,0 +1,332 @@ ++// ---------------------------------------------------------------------------- ++// https_openssl.cxx ++// ++// OpenSSL TLS "crypto provider" for fldigi's HTTPS client. ++// ++// This provider is opt-in (`configure --with-openssl`) and is NEVER bundled: ++// it always links the system OpenSSL libraries, discovered via pkg-config. ++// When it is selected, the mbedtls provider (network/https_mbedtls.cxx) is ++// not compiled and the bundled mbedtls sources are not built either. ++// ++// The peer certificate is verified against the CA store bundled in ++// network/ca_cert.cxx, mirroring the behavior of the mbedtls provider. As in ++// the legacy mbedtls code (MBEDTLS_SSL_VERIFY_OPTIONAL), certificate problems ++// are reported via verify_info() but do not abort the handshake. ++// ++// This file is part of fldigi. ++// ++// fldigi is free software: you can redistribute it and/or modify ++// it under the terms of the GNU General Public License as published by ++// the Free Software Foundation, either version 3 of the License, or ++// (at your option) any later version. ++// ++// fldigi is distributed in the hope that it will be useful, ++// but WITHOUT ANY WARRANTY; without even the implied warranty of ++// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++// GNU General Public License for more details. ++// ++// You should have received a copy of the GNU General Public License ++// along with fldigi. If not, see <http://www.gnu.org/licenses/>. ++// ---------------------------------------------------------------------------- ++ ++#include <config.h> ++ ++#include <cstdio> ++#include <cstring> ++#include <string> ++#include <cerrno> ++ ++#if defined(_WIN32) ++# include <winsock2.h> ++# include <ws2tcpip.h> ++#else ++# include <sys/types.h> ++# include <sys/socket.h> ++# include <netinet/in.h> ++# include <netdb.h> ++# include <unistd.h> ++#endif ++ ++#include <openssl/ssl.h> ++#include <openssl/err.h> ++#include <openssl/x509.h> ++#include <openssl/pem.h> ++#include <openssl/bio.h> ++#include <openssl/crypto.h> ++ ++#include "https_provider.h" ++#include "network.h" ++ ++#if defined(_WIN32) ++# define SOCKET_CLOSE(s) closesocket(s) ++#else ++# define SOCKET_CLOSE(s) ::close(s) ++#endif ++ ++class HttpsOpenSSLConnection : public HttpsConnection ++{ ++public: ++ HttpsOpenSSLConnection(const std::string &host, const std::string &port) ++ : _host(host), _port(port), _fd(-1), _ctx(0), _ssl(0), _err(0) ++ { ++ _errbuf[0] = 0; ++ _verify[0] = 0; ++ } ++ ++ ~HttpsOpenSSLConnection() { close(); } ++ ++ int connect() override; ++ ssize_t write(const void *buf, size_t len) override; ++ ssize_t read(void *buf, size_t len) override; ++ ++ int error() const override { return _err; } ++ const char *errstr() const override { return _errbuf; } ++ const char *verify_info() const override { return _verify; } ++ ++ void close() override ++ { ++ if (_ssl) { ++ SSL_shutdown(_ssl); ++ SSL_free(_ssl); ++ _ssl = 0; ++ } ++ if (_ctx) { ++ SSL_CTX_free(_ctx); ++ _ctx = 0; ++ } ++ if (_fd >= 0) { ++ SOCKET_CLOSE(_fd); ++ _fd = -1; ++ } ++ } ++ ++ // Called by the OpenSSL verify callback during the handshake to record a ++ // certificate problem (untrusted chain, hostname mismatch, ...) without ++ // aborting the connection (mirrors mbedtls MBEDTLS_SSL_VERIFY_OPTIONAL). ++ void record_verify_error(X509_STORE_CTX *ctx); ++ ++private: ++ std::string _host; ++ std::string _port; ++ int _fd; ++ SSL_CTX *_ctx; ++ SSL *_ssl; ++ int _err; ++ char _errbuf[256]; ++ char _verify[512]; ++}; ++ ++// Application ex_data index used to find the provider instance from the SSL ++// object inside the verify callback. ++static int ssl_provider_idx = -1; ++ ++// Build an X509_STORE populated from the certificates bundled in ++// network/ca_cert.cxx (a single buffer of concatenated PEM certificates). ++static X509_STORE *openssl_bundled_ca_store() ++{ ++ X509_STORE *store = X509_STORE_new(); ++ BIO *bio = 0; ++ X509 *cert = 0; ++ int loaded = 0; ++ ++ if (!store) ++ return 0; ++ ++ bio = BIO_new_mem_buf((const void *)ca_crt_rsa, (int)ca_crt_rsa_size); ++ if (!bio) { ++ X509_STORE_free(store); ++ return 0; ++ } ++ ++ while ((cert = PEM_read_bio_X509(bio, 0, 0, 0)) != 0) { ++ if (X509_STORE_add_cert(store, cert) == 1) ++ loaded++; ++ X509_free(cert); ++ } ++ ++ BIO_free(bio); ++ ++ if (loaded == 0) { ++ X509_STORE_free(store); ++ return 0; ++ } ++ return store; ++} ++ ++// Verify callback used for the bundled CA store. Like the legacy mbedtls ++// MBEDTLS_SSL_VERIFY_OPTIONAL mode, it always returns 1 (the handshake is ++// never aborted because of certificate problems); the problem is recorded in ++// the provider's verify_info() instead. ++static int openssl_verify_cb(int preverify_ok, X509_STORE_CTX *ctx) ++{ ++ const SSL *ssl = (const SSL *)X509_STORE_CTX_get_ex_data( ++ ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); ++ HttpsOpenSSLConnection *self = ++ (HttpsOpenSSLConnection *)SSL_get_ex_data(ssl, ssl_provider_idx); ++ if (!preverify_ok && self) ++ self->record_verify_error(ctx); ++ (void)preverify_ok; ++ return 1; ++} ++ ++void HttpsOpenSSLConnection::record_verify_error(X509_STORE_CTX *ctx) ++{ ++ int verr = X509_STORE_CTX_get_error(ctx); ++ const char *vs = X509_verify_cert_error_string(verr); ++ char ebuf[128] = ""; ++ if (verr && vs) { ++ if (strlen(_verify) == 0) ++ snprintf(_verify, sizeof(_verify), " ! %s", vs); ++ else ++ snprintf(_verify + strlen(_verify), ++ sizeof(_verify) - strlen(_verify), "\n ! %s", vs); ++ } ++ (void)ebuf; ++} ++ ++int HttpsOpenSSLConnection::connect() ++{ ++ int ret; ++ char portbuf[16]; ++ struct addrinfo hints, *res = 0, *rp; ++ ++ snprintf(portbuf, sizeof(portbuf), "%s", _port.c_str()); ++ ++ memset(&hints, 0, sizeof(hints)); ++ hints.ai_family = AF_UNSPEC; ++ hints.ai_socktype = SOCK_STREAM; ++ ++ ret = getaddrinfo(_host.c_str(), portbuf, &hints, &res); ++ if (ret != 0) { ++ _err = ret; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "getaddrinfo(%s:%s): %s", _host.c_str(), portbuf, gai_strerror(ret)); ++ return _err; ++ } ++ ++ for (rp = res; rp; rp = rp->ai_next) { ++ _fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); ++ if (_fd < 0) ++ continue; ++ if (::connect(_fd, rp->ai_addr, (int)rp->ai_addrlen) == 0) ++ break; ++ SOCKET_CLOSE(_fd); ++ _fd = -1; ++ } ++ freeaddrinfo(res); ++ ++ if (_fd < 0) { ++ _err = -1; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "connect to %s:%s failed: %s", _host.c_str(), portbuf, strerror(errno)); ++ return _err; ++ } ++ ++ if (ssl_provider_idx < 0) ++ ssl_provider_idx = SSL_get_ex_new_index(0, (void *)"fldigi https_openssl", 0, 0, 0); ++ ++ _ctx = SSL_CTX_new(TLS_client_method()); ++ if (!_ctx) { ++ _err = -2; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "SSL_CTX_new failed: %s", ERR_error_string(ERR_get_error(), 0)); ++ goto fail; ++ } ++ ++ // Only modern TLS; never the obsolete SSL 2/3 protocol versions. ++ SSL_CTX_set_options(_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); ++ ++ // Verify against the bundled CA store (not the OS trust store). ++ { ++ X509_STORE *store = openssl_bundled_ca_store(); ++ if (!store) { ++ _err = -3; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "failed to load bundled CA certificates"); ++ goto fail; ++ } ++ SSL_CTX_set0_verify_cert_store(_ctx, store); // takes ownership ++ } ++ ++ SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, openssl_verify_cb); ++ SSL_CTX_set_verify_depth(_ctx, 8); ++ ++ _ssl = SSL_new(_ctx); ++ if (!_ssl) { ++ _err = -4; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "SSL_new failed: %s", ERR_error_string(ERR_get_error(), 0)); ++ goto fail; ++ } ++ ++ SSL_set_ex_data(_ssl, ssl_provider_idx, this); ++ SSL_set_fd(_ssl, _fd); ++ SSL_set_tlsext_host_name(_ssl, _host.c_str()); ++ // Hostname verification (works on OpenSSL 1.1.x, 3.x and 4.x) ++ { ++ X509_VERIFY_PARAM *param = SSL_get0_param(_ssl); ++ if (param) ++ X509_VERIFY_PARAM_set1_host(param, _host.c_str(), 0); ++ } ++ ++ ret = SSL_connect(_ssl); ++ if (ret != 1) { ++ _err = SSL_get_error(_ssl, ret); ++ snprintf(_errbuf, sizeof(_errbuf), ++ "SSL_connect failed: %s", ERR_error_string(ERR_get_error(), 0)); ++ goto fail; ++ } ++ ++ return 0; ++ ++fail: ++ close(); ++ return _err; ++} ++ ++ssize_t HttpsOpenSSLConnection::write(const void *buf, size_t len) ++{ ++ int ret = SSL_write(_ssl, buf, (int)len); ++ if (ret > 0) ++ return ret; ++ int ssl_err = SSL_get_error(_ssl, ret); ++ if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) ++ return HTTPS_IO_RETRY; ++ _err = ssl_err; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "SSL_write failed: %s", ERR_error_string(ERR_get_error(), 0)); ++ return ret; ++} ++ ++ssize_t HttpsOpenSSLConnection::read(void *buf, size_t len) ++{ ++ int ret = SSL_read(_ssl, buf, (int)len); ++ if (ret > 0) ++ return ret; ++ int ssl_err = SSL_get_error(_ssl, ret); ++ if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) ++ return HTTPS_IO_RETRY; ++ if (ssl_err == SSL_ERROR_ZERO_RETURN) // clean TLS end of stream ++ return 0; ++ if (ssl_err == SSL_ERROR_SYSCALL && ret == 0) // EOF on the socket ++ return 0; ++ _err = ssl_err; ++ snprintf(_errbuf, sizeof(_errbuf), ++ "SSL_read failed: %s", ERR_error_string(ERR_get_error(), 0)); ++ return ret; ++} ++ ++HttpsConnection *https_connection_new(const std::string &host, ++ const std::string &port, ++ const std::string &pers, ++ double timeout, ++ std::string *err) ++{ ++ (void)pers; // OpenSSL does not need a DRBG personalization seed ++ (void)timeout; // reserved; both backends currently connect blocking ++ ++ HttpsOpenSSLConnection *conn = new (std::nothrow) HttpsOpenSSLConnection(host, port); ++ if (!conn && err) ++ *err = "out of memory creating OpenSSL TLS provider"; ++ return conn; ++} +diff --git a/src/network/network.cxx b/src/network/network.cxx +index 235822e7..ea623c3d 100644 +--- a/src/network/network.cxx ++++ b/src/network/network.cxx +@@ -44,22 +44,13 @@ + + #include "network.h" + #include "socket.h" ++#include "https_provider.h" + + #include "fl_digi.h" + + //---------------------------------------------------------------------- + // + //---------------------------------------------------------------------- +-static void my_debug( void *ctx, int level, +- const char *file, int line, +- const char *str ) +-{ +- ((void) level); +- +- fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str ); +- fflush( (FILE *) ctx ); +-} +- + std::string stripped(std::string s) + { + while (s[0] == ' ') s.erase(0,1); +@@ -251,188 +242,9 @@ recv_exit: + + int Url::https_test(std::string apiKey) + { +- int ret = 2, len; +- _err = MBEDTLS_EXIT_SUCCESS; + std::ostringstream REQUEST; +- std::string response = ""; +- +- mbedtls_entropy_context entropy; +- mbedtls_ctr_drbg_context ctr_drbg; +- mbedtls_ssl_context ssl; +- mbedtls_ssl_config conf; +- mbedtls_x509_crt cacert; +- +-#if defined(MBEDTLS_DEBUG_C) +- mbedtls_debug_set_threshold( 1 ); +-#endif +- +- /* +- * 0. Initialize the RNG and the session data +- */ +- mbedtls_net_init( &server_fd ); +- mbedtls_ssl_init( &ssl ); +- mbedtls_ssl_config_init( &conf ); +- mbedtls_x509_crt_init( &cacert ); +- mbedtls_ctr_drbg_init( &ctr_drbg ); +- +- if (debug_file) { +- debug_file << "\n . Seeding the random number generator..."; +- debug_file.flush(); +- } +- mbedtls_entropy_init( &entropy ); +- if ( ( ret = mbedtls_ctr_drbg_seed( +- &ctr_drbg, +- mbedtls_entropy_func, +- &entropy, +- (const unsigned char *)_pers.c_str(), +- _pers.length() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); +- _err = ret; +- goto exit; +- } +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 0. Initialize certificates +- */ +- if (debug_file) { +- debug_file << " . Loading the CA root certificate ..."; +- debug_file.flush(); +- } +- ca_crt_rsa[ca_crt_rsa_size - 1] = 0; +- ret = mbedtls_x509_crt_parse(&cacert, (uint8_t *)ca_crt_rsa, ca_crt_rsa_size); +- +- if( ret < 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok (" << ret << " skipped)\n"; +- } +- +- /* +- * 1. Start the connection +- */ +- if (debug_file) { +- debug_file << " . Connecting to tcp/" +- << _host << ":" << _port << "..."; +- debug_file.flush(); +- } +- +- if( ( ret = mbedtls_net_connect( +- &server_fd, +- _host.c_str(), +- _port.c_str(), +- MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 2. Setup stuff +- */ +- +- if (debug_file) { +- debug_file << " . Setting up the SSL/TLS structure..."; +- debug_file.flush(); +- } +- if( ( ret = mbedtls_ssl_config_defaults( &conf, +- MBEDTLS_SSL_IS_CLIENT, +- MBEDTLS_SSL_TRANSPORT_STREAM, +- MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 3. More stuff +- */ +- +- /* OPTIONAL is not optimal for security, +- * but makes interop easier in this simplified example */ +- mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); +- mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); +- mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); +- mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); +- +- if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if( ( ret = mbedtls_ssl_set_hostname( &ssl, _host.c_str() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); +- +- /* +- * 4. Handshake +- */ +- if (debug_file) { +- debug_file << " . Performing the SSL/TLS handshake..."; +- debug_file.flush(); +- } +- while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; +- } +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 5. Verify the server certificate +- */ +- if (debug_file) { +- debug_file << " . Verifying peer X.509 certificate..."; +- } +- /* In real life, we probably want to bail out when ret != 0 */ +- if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) { +- char vrfy_buf[512]; +- if (debug_file) { +- debug_file << " failed\n"; +- } +- mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); +- if (debug_file) { +- debug_file << vrfy_buf << std::endl; +- } +- } else +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 6. Write the GET request +- */ +- +- if (debug_file) { +- debug_file << " > Write to server:"; +- debug_file.flush(); +- } ++ std::string response; ++ int ret = 2; + + REQUEST << "GET " << _request << "/index.php/api/auth/" << apiKey << " HTTP/1.1\r\n"; + REQUEST << +@@ -441,71 +253,15 @@ int Url::https_test(std::string apiKey) + "Content-Type: application/json; charset=utf-8\r\n" << + "Connection: Keep-Alive\r\n\r\n"; + +- len = REQUEST.str().length(); +- +- while( ( ret = mbedtls_ssl_write( +- &ssl, +- (const unsigned char *)REQUEST.str().c_str(), +- len ) ) <= 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- } +- +- len = ret; + if (debug_file) { +- debug_file << len << " bytes written\n\"" << REQUEST.str() << "\""; +- debug_file.flush(); +- } +- /* +- * 7. Read the HTTP response +- */ +- if (debug_file) { +- debug_file << "\n < Read from server:"; +- debug_file.flush(); ++ debug_file << "\nUrl::https_test(...)" << std::endl; ++ debug_file << "Host: " << _host << std::endl; ++ debug_file << "Service: https" << std::endl; ++ debug_file << "Request: " << REQUEST.str() << std::endl; + } + +- do { +- len = sizeof( buf ) - 1; +- memset( buf, 0, sizeof( buf ) ); +- ret = mbedtls_ssl_read( &ssl, (unsigned char *)buf, len ); +- +- if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) { +- continue; +- } +- +- if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) { +- break; +- } +- +- if( ret < 0 ) { +- if (debug_file) { +- debug_file << "failed\n ! mbedtls_ssl_read returned " << ret << std::endl; +- } +- break; +- } +- +- if( ret == 0 ) { +- if (debug_file) { +- debug_file << "\n\nEOF\n\n"; +- } +- break; +- } +- +- len = ret; +- if (debug_file) { +- debug_file << len << " bytes read\n\n" << (char *) buf << std::endl; +- } +- _data.append(buf); +- break; +- } while( 1 ); +- +- mbedtls_ssl_close_notify( &ssl ); +- +- response = _data; ++ if (https_exchange(REQUEST.str(), response) != HTTPS_EXIT_SUCCESS) ++ return _err; + + // Response for an API key test request + // <auth><status>Valid</status><rights>rw</rights></auth> +@@ -522,33 +278,9 @@ int Url::https_test(std::string apiKey) + ret = 0; + } + } +- _err = MBEDTLS_EXIT_SUCCESS; +-exit: +- +-#ifdef MBEDTLS_ERROR_C +- if( _err != MBEDTLS_EXIT_SUCCESS ) { +- char error_buf[100]; +- mbedtls_strerror( _err, error_buf, 100 ); +- snprintf(err_string, sizeof(err_string), +- "Last error was: %d - %s\n\n", _err, error_buf ); +- } +-#endif +- +- mbedtls_net_free( &server_fd ); +- +- mbedtls_x509_crt_free( &cacert ); +- mbedtls_ssl_free( &ssl ); +- mbedtls_ssl_config_free( &conf ); +- mbedtls_ctr_drbg_free( &ctr_drbg ); +- mbedtls_entropy_free( &entropy ); +- +- if( _err != MBEDTLS_EXIT_SUCCESS ) { +- return( _err ); +- } else { +- return ret; +- } ++ _err = HTTPS_EXIT_SUCCESS; ++ return ret; + } +- + int Url::http_post(std::string apikey, int profile_id, std::string adifdata, std::string &response) + { + std::ostringstream REQUEST; +@@ -597,189 +329,103 @@ recv_exit: + return ret; + } + +-int Url::https_get(std::string &response) ++int Url::https_exchange(const std::string &request, std::string &response) + { +- int ret = 1, len; +- _err = MBEDTLS_EXIT_SUCCESS; +- std::ostringstream REQUEST; +- +- mbedtls_entropy_context entropy; +- mbedtls_ctr_drbg_context ctr_drbg; +- mbedtls_ssl_context ssl; +- mbedtls_ssl_config conf; +- mbedtls_x509_crt cacert; +- +-#if defined(MBEDTLS_DEBUG_C) +- mbedtls_debug_set_threshold( 1 ); +-#endif +- +- /* +- * 0. Initialize the RNG and the session data +- */ +- mbedtls_net_init( &server_fd ); +- mbedtls_ssl_init( &ssl ); +- mbedtls_ssl_config_init( &conf ); +- mbedtls_x509_crt_init( &cacert ); +- mbedtls_ctr_drbg_init( &ctr_drbg ); ++ HttpsConnection *conn = 0; ++ int ret; + +- if (debug_file) { +- debug_file << "\n . Seeding the random number generator..."; +- debug_file.flush(); +- } +- mbedtls_entropy_init( &entropy ); +- if ( ( ret = mbedtls_ctr_drbg_seed( +- &ctr_drbg, +- mbedtls_entropy_func, +- &entropy, +- (const unsigned char *)_pers.c_str(), +- _pers.length() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); +- _err = ret; +- goto exit; +- } +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 0. Initialize certificates +- */ +- if (debug_file) { +- debug_file << " . Loading the CA root certificate ..."; +- debug_file.flush(); +- } +- ca_crt_rsa[ca_crt_rsa_size - 1] = 0; +- ret = mbedtls_x509_crt_parse(&cacert, (uint8_t *)ca_crt_rsa, ca_crt_rsa_size); +- +- if( ret < 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; +- } ++ _err = HTTPS_EXIT_SUCCESS; + + if (debug_file) { +- debug_file << " ok (" << ret << " skipped)\n"; +- } +- +- /* +- * 1. Start the connection +- */ +- if (debug_file) { +- debug_file << " . Connecting to tcp/" +- << _host << ":" << _port << "..."; ++ debug_file << " . Connecting to tcp/" << _host << ":" << _port << "..."; + debug_file.flush(); + } + +- if( ( ret = mbedtls_net_connect( +- &server_fd, +- _host.c_str(), +- _port.c_str(), +- MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { ++ conn = https_connection_new(_host, _port, _pers, _timeout); ++ if (!conn) { + snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); +- _err = ret; +- goto exit; ++ " failed\n ! unable to create TLS provider connection to %s:%s\n", ++ _host.c_str(), _port.c_str()); ++ _err = HTTPS_EXIT_FAILURE; ++ return _err; + } + + if (debug_file) { + debug_file << " ok\n"; + } +- /* +- * 2. Setup stuff +- */ + +- if (debug_file) { +- debug_file << " . Setting up the SSL/TLS structure..."; +- debug_file.flush(); +- } +- if( ( ret = mbedtls_ssl_config_defaults( &conf, +- MBEDTLS_SSL_IS_CLIENT, +- MBEDTLS_SSL_TRANSPORT_STREAM, +- MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) { ++ if ((ret = conn->connect()) != 0) { + snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); ++ " failed\n ! %s\n", conn->errstr()); + _err = ret; + goto exit; + } + + if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 3. More stuff +- */ +- +- /* OPTIONAL is not optimal for security, +- * but makes interop easier in this simplified example */ +- mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); +- mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); +- mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); +- mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); +- +- if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); +- _err = ret; +- goto exit; ++ const char *v = conn->verify_info(); ++ debug_file << " . SSL/TLS connected"; ++ if (v && *v) ++ debug_file << "\n" << v; ++ debug_file << std::endl; + } + +- if( ( ret = mbedtls_ssl_set_hostname( &ssl, _host.c_str() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret ); +- _err = ret; +- goto exit; ++ { ++ size_t len = request.length(); ++ size_t off = 0; ++ while (off < len) { ++ ret = conn->write(request.data() + off, len - off); ++ if (ret == HTTPS_IO_RETRY) ++ continue; ++ if (ret <= 0) { ++ snprintf(err_string, sizeof(err_string), ++ " failed\n ! %s\n", conn->errstr()); ++ _err = HTTPS_EXIT_FAILURE; ++ goto exit; ++ } ++ off += (size_t)ret; ++ } + } + +- mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); +- +- /* +- * 4. Handshake +- */ + if (debug_file) { +- debug_file << " . Performing the SSL/TLS handshake..."; ++ debug_file << " < Read from server..."; + debug_file.flush(); + } +- while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; ++ ++ response.clear(); ++ { ++ char rbuf[H_READ_SIZE]; ++ for (;;) { ++ ssize_t n = conn->read(rbuf, sizeof(rbuf) - 1); ++ if (n == HTTPS_IO_RETRY) ++ continue; ++ if (n < 0) { ++ snprintf(err_string, sizeof(err_string), ++ " failed\n ! %s\n", conn->errstr()); ++ _err = HTTPS_EXIT_FAILURE; ++ goto exit; ++ } ++ if (n == 0) ++ break; // end of stream ++ response.append(rbuf, (size_t)n); ++ break; + } + } + + if (debug_file) { + debug_file << " ok\n"; + } +- /* +- * 5. Verify the server certificate +- */ +- if (debug_file) { +- debug_file << " . Verifying peer X.509 certificate..."; +- } +- /* In real life, we probably want to bail out when ret != 0 */ +- if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) { +- char vrfy_buf[512]; +- if (debug_file) { +- debug_file << " failed\n"; +- } +- mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); +- if (debug_file) { +- debug_file << vrfy_buf << std::endl; +- } +- } else +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 6. Write the GET request +- */ + +- if (debug_file) { +- debug_file << " > Write to server:"; +- debug_file.flush(); ++exit: ++ if (conn) { ++ conn->close(); ++ delete conn; + } ++ return _err; ++} ++ ++int Url::https_get(std::string &response) ++{ ++ std::ostringstream REQUEST; + + REQUEST << "GET " << _request << " HTTP/1.1\r\n"; + REQUEST << +@@ -788,279 +434,20 @@ int Url::https_get(std::string &response) + "Content-Type: application/json; charset=utf-8\r\n" << + "Connection: Keep-Alive\r\n\r\n"; + +- len = REQUEST.str().length(); +- +- while( ( ret = mbedtls_ssl_write( +- &ssl, +- (const unsigned char *)REQUEST.str().c_str(), +- len ) ) <= 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- } +- +- len = ret; +- if (debug_file) { +- debug_file << len << " bytes written\n\"" << REQUEST.str() << "\""; +- debug_file.flush(); +- } +- /* +- * 7. Read the HTTP response +- */ + if (debug_file) { +- debug_file << "\n < Read from server:"; +- debug_file.flush(); +- } +- +- do { +- len = sizeof( buf ) - 1; +- memset( buf, 0, sizeof( buf ) ); +- ret = mbedtls_ssl_read( &ssl, (unsigned char *)buf, len ); +- +- if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) { +- continue; +- } +- +- if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) { +- break; +- } +- +- if( ret < 0 ) { +- if (debug_file) { +- debug_file << "failed\n ! mbedtls_ssl_read returned " << ret << std::endl; +- } +- break; +- } +- +- if( ret == 0 ) { +- if (debug_file) { +- debug_file << "\n\nEOF\n\n"; +- } +- break; +- } +- +- len = ret; +- if (debug_file) { +- debug_file << len << " bytes read\n\n" << (char *) buf << std::endl; +- } +- _data.append(buf); +- break; +- } while( 1 ); +- +- mbedtls_ssl_close_notify( &ssl ); +- +- response = _data; +- _err = MBEDTLS_EXIT_SUCCESS; +- +-exit: +- +-#ifdef MBEDTLS_ERROR_C +- if( _err != MBEDTLS_EXIT_SUCCESS ) { +- char error_buf[100]; +- mbedtls_strerror( _err, error_buf, 100 ); +- snprintf(err_string, sizeof(err_string), +- "Last error was: %d - %s\n\n", _err, error_buf ); ++ debug_file << "\nUrl::https_get(...)" << std::endl; ++ debug_file << "Host: " << _host << std::endl; ++ debug_file << "Service: https" << std::endl; ++ debug_file << "Request: " << REQUEST.str() << std::endl; + } +-#endif + +- mbedtls_net_free( &server_fd ); +- +- mbedtls_x509_crt_free( &cacert ); +- mbedtls_ssl_free( &ssl ); +- mbedtls_ssl_config_free( &conf ); +- mbedtls_ctr_drbg_free( &ctr_drbg ); +- mbedtls_entropy_free( &entropy ); +- +- return( _err ); ++ return https_exchange(REQUEST.str(), response); + } +- + int Url::https_post(std::string apikey, int profile_id, std::string adifdata, std::string &response) + { +- int ret = 1, len; +- _err = MBEDTLS_EXIT_SUCCESS; + std::ostringstream REQUEST; + std::ostringstream PAYLOAD; +- +- mbedtls_entropy_context entropy; +- mbedtls_ctr_drbg_context ctr_drbg; +- mbedtls_ssl_context ssl; +- mbedtls_ssl_config conf; +- mbedtls_x509_crt cacert; +- +-#if defined(MBEDTLS_DEBUG_C) +- mbedtls_debug_set_threshold( 1 ); +-#endif +- +- /* +- * 0. Initialize the RNG and the session data +- */ +- mbedtls_net_init( &server_fd ); +- mbedtls_ssl_init( &ssl ); +- mbedtls_ssl_config_init( &conf ); +- mbedtls_x509_crt_init( &cacert ); +- mbedtls_ctr_drbg_init( &ctr_drbg ); +- +- if (debug_file) { +- debug_file << "\n . Seeding the random number generator..."; +- debug_file.flush(); +- } +- mbedtls_entropy_init( &entropy ); +- if ( ( ret = mbedtls_ctr_drbg_seed( +- &ctr_drbg, +- mbedtls_entropy_func, +- &entropy, +- (const unsigned char *)_pers.c_str(), +- _pers.length() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); +- _err = ret; +- goto exit; +- } +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 0. Initialize certificates +- */ +- if (debug_file) { +- debug_file << " . Loading the CA root certificate ..."; +- debug_file.flush(); +- } +- ca_crt_rsa[ca_crt_rsa_size - 1] = 0; +- ret = mbedtls_x509_crt_parse(&cacert, (uint8_t *)ca_crt_rsa, ca_crt_rsa_size); +- +- if( ret < 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok (" << ret << " skipped)\n"; +- } +- +- /* +- * 1. Start the connection +- */ +- if (debug_file) { +- debug_file << " . Connecting to tcp/" +- << _host << ":" << _port << "..."; +- debug_file.flush(); +- } +- +- if( ( ret = mbedtls_net_connect( +- &server_fd, +- _host.c_str(), +- _port.c_str(), +- MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 2. Setup stuff +- */ +- +- if (debug_file) { +- debug_file << " . Setting up the SSL/TLS structure..."; +- debug_file.flush(); +- } +- if( ( ret = mbedtls_ssl_config_defaults( &conf, +- MBEDTLS_SSL_IS_CLIENT, +- MBEDTLS_SSL_TRANSPORT_STREAM, +- MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 3. More stuff +- */ +- +- /* OPTIONAL is not optimal for security, +- * but makes interop easier in this simplified example */ +- mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); +- mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); +- mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); +- mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); +- +- if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- if( ( ret = mbedtls_ssl_set_hostname( &ssl, _host.c_str() ) ) != 0 ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- +- mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); +- +- /* +- * 4. Handshake +- */ +- if (debug_file) { +- debug_file << " . Performing the SSL/TLS handshake..."; +- debug_file.flush(); +- } +- while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret ); +- _err = ret; +- goto exit; +- } +- } +- +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 5. Verify the server certificate +- */ +- if (debug_file) { +- debug_file << " . Verifying peer X.509 certificate..."; +- } +- /* In real life, we probably want to bail out when ret != 0 */ +- if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ) { +- char vrfy_buf[512]; +- if (debug_file) { +- debug_file << " failed\n"; +- } +- mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); +- if (debug_file) { +- debug_file << vrfy_buf << std::endl; +- } +- } else +- if (debug_file) { +- debug_file << " ok\n"; +- } +- /* +- * 6. Write the GET request +- */ +- +- if (debug_file) { +- debug_file << " > Write to server:"; +- debug_file.flush(); +- } ++ size_t len; + + PAYLOAD << + "{" << +@@ -1078,95 +465,15 @@ int Url::https_post(std::string apikey, int profile_id, std::string adifdata, st + "Content-Length: " << len << "\r\n" << + "Connection: Keep-Alive\r\n\r\n" << PAYLOAD.str(); + +- len = REQUEST.str().length(); +- +- while( ( ret = mbedtls_ssl_write( +- &ssl, +- (const unsigned char *)REQUEST.str().c_str(), +- len ) ) <= 0 ) { +- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { +- snprintf(err_string, sizeof(err_string), +- " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); +- _err = ret; +- goto exit; +- } +- } +- +- len = ret; +- if (debug_file) { +- debug_file << len << " bytes written\n\"" << REQUEST.str() << "\""; +- debug_file.flush(); +- } +- /* +- * 7. Read the HTTP response +- */ + if (debug_file) { +- debug_file << "\n < Read from server:"; +- debug_file.flush(); +- } +- +- do { +- len = sizeof( buf ) - 1; +- memset( buf, 0, sizeof( buf ) ); +- ret = mbedtls_ssl_read( &ssl, (unsigned char *)buf, len ); +- +- if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) { +- continue; +- } +- +- if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) { +- break; +- } +- +- if( ret < 0 ) { +- if (debug_file) { +- debug_file << "failed\n ! mbedtls_ssl_read returned " << ret << std::endl; +- } +- break; +- } +- +- if( ret == 0 ) { +- if (debug_file) { +- debug_file << "\n\nEOF\n\n"; +- } +- break; +- } +- +- len = ret; +- if (debug_file) { +- debug_file << len << " bytes read\n\n" << (char *) buf << std::endl; +- } +- _data.append(buf); +- break; +- } while( 1 ); +- +- mbedtls_ssl_close_notify( &ssl ); +- +- response = _data; +- _err = MBEDTLS_EXIT_SUCCESS; +- +-exit: +- +-#ifdef MBEDTLS_ERROR_C +- if( _err != MBEDTLS_EXIT_SUCCESS ) { +- char error_buf[100]; +- mbedtls_strerror( _err, error_buf, 100 ); +- snprintf(err_string, sizeof(err_string), +- "Last error was: %d - %s\n\n", _err, error_buf ); ++ debug_file << "\nUrl::https_post(...)" << std::endl; ++ debug_file << "Host: " << _host << std::endl; ++ debug_file << "Service: https" << std::endl; ++ debug_file << "Request: " << REQUEST.str() << std::endl; + } +-#endif +- +- mbedtls_net_free( &server_fd ); + +- mbedtls_x509_crt_free( &cacert ); +- mbedtls_ssl_free( &ssl ); +- mbedtls_ssl_config_free( &conf ); +- mbedtls_ctr_drbg_free( &ctr_drbg ); +- mbedtls_entropy_free( &entropy ); +- +- return( _err ); ++ return https_exchange(REQUEST.str(), response); + } +- + int Url::get(std::string url, std::string &response) + { + if (url.empty()) diff --git a/debian/patches/series b/debian/patches/series index 33bd9980..8ee0e3fd 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -1,2 +1,3 @@ localefix.patch aclocal +openssl.patch diff --git a/debian/rules b/debian/rules index 621682d2..2a400986 100755 --- a/debian/rules +++ b/debian/rules @@ -14,8 +14,7 @@ override_dh_clean: dh_clean override_dh_auto_configure: - # force using external libmbedtls (with explicit flags until it has pkg-config support, #900015) - dh_auto_configure -- --with-libmbedtls LIBMBEDTLS_CFLAGS="-I/usr/include/mbedtls" LIBMBEDTLS_LIBS="-lmbedtls -lmbedx509 -lmbedcrypto" + dh_auto_configure -- --with-openssl # remove build path from build flags sed -i -e 's/\(-f[^ ]*prefix-map\)=[^ =]*=/\1=.=/g' src/config.h

