From 0eae0449fdf41bced7d0e4639a76adf70aa57ddf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 6 Mar 2018 16:17:06 -0500
Subject: [PATCH] namedirectory: use https instead of plain http requests
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add libressl dependency to compile restbed with ssl support. Also,
this patch removes a unused file.
Bump asio to works with libressl instead of openssl.

Change-Id: I5a90dba97693ccda58899f69080e545bdea926d9
Signed-off-by: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
---
 MSVC/ring-daemon.vcxproj                |  2 +-
 configure.ac                            | 13 +++++-
 contrib/build_all_uwp.bat               | 13 +++++-
 contrib/src/asio/SHA512SUMS             |  2 +-
 contrib/src/asio/rules.mak              |  4 +-
 contrib/src/libressl/SHA512SUMS         |  1 +
 contrib/src/libressl/getpagesize.patch  | 28 +++++++++++++
 contrib/src/libressl/rules.mak          | 53 +++++++++++++++++++++++++
 contrib/src/restbed/fetch_and_patch.bat | 24 ++++++++++-
 contrib/src/restbed/findkashmir.patch   | 16 --------
 contrib/src/restbed/rules.mak           |  6 +--
 src/ringdht/namedirectory.cpp           |  8 ++--
 12 files changed, 138 insertions(+), 32 deletions(-)
 create mode 100644 contrib/src/libressl/SHA512SUMS
 create mode 100644 contrib/src/libressl/getpagesize.patch
 create mode 100644 contrib/src/libressl/rules.mak
 delete mode 100644 contrib/src/restbed/findkashmir.patch

diff --git a/MSVC/ring-daemon.vcxproj b/MSVC/ring-daemon.vcxproj
index aa789f3f79..b7d43feefb 100644
--- a/MSVC/ring-daemon.vcxproj
+++ b/MSVC/ring-daemon.vcxproj
@@ -527,7 +527,7 @@
       </Command>
     </PreBuildEvent>
     <Lib>
-      <AdditionalDependencies>ws2_32.lib;iphlpapi.lib;advapi32.lib;avcodec.lib;avdevice.lib;avfilter.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;libsamplerate.lib;libgnutls.lib;lib_json.lib;opendht.lib;Argon2Ref.lib;pcre.lib;yaml-cpp.lib;portaudio-UWP.lib;libupnp.lib;pjsip-core-x86_64-x64-vc14-Release.lib;pjsip-simple-x86_64-x64-vc14-Release.lib;pjsua2-lib-x86_64-x64-vc14-Release.lib;pjsua-lib-x86_64-x64-vc14-Release.lib;pjsip-ua-x86_64-x64-vc14-Release.lib;pjmedia-codec-x86_64-x64-vc14-Release.lib;pjmedia-x86_64-x64-vc14-Release.lib;pjlib-util-x86_64-x64-vc14-Release.lib;libspeex-x86_64-x64-vc14-Release.lib;pjlib-x86_64-x64-vc14-Release.lib;pjnath-x86_64-x64-vc14-Release.lib;restbed.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;iphlpapi.lib;advapi32.lib;avcodec.lib;avdevice.lib;avfilter.lib;avformat.lib;avutil.lib;swresample.lib;swscale.lib;libsamplerate.lib;libgnutls.lib;lib_json.lib;opendht.lib;Argon2Ref.lib;pcre.lib;yaml-cpp.lib;portaudio-UWP.lib;libupnp.lib;pjsip-core-x86_64-x64-vc14-Release.lib;pjsip-simple-x86_64-x64-vc14-Release.lib;pjsua2-lib-x86_64-x64-vc14-Release.lib;pjsua-lib-x86_64-x64-vc14-Release.lib;pjsip-ua-x86_64-x64-vc14-Release.lib;pjmedia-codec-x86_64-x64-vc14-Release.lib;pjmedia-x86_64-x64-vc14-Release.lib;pjlib-util-x86_64-x64-vc14-Release.lib;libspeex-x86_64-x64-vc14-Release.lib;pjlib-x86_64-x64-vc14-Release.lib;pjnath-x86_64-x64-vc14-Release.lib;restbed.lib;libeay32.lib;ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(ProjectDir)..\contrib\build\FFmpegInterop\ffmpeg\Build\Windows10\x64\bin;$(ProjectDir)..\contrib\build\lib\x64;$(ProjectDir)..\contrib\build\boost\stage\lib;$(ProjectDir)..\contrib\build\pjproject\pjsip\lib;$(ProjectDir)..\contrib\build\pjproject\pjmedia\lib;$(ProjectDir)..\contrib\build\pjproject\pjlib-util\lib;$(ProjectDir)..\contrib\build\pjproject\third_party\lib;$(ProjectDir)..\contrib\build\pjproject\pjlib\lib;$(ProjectDir)..\contrib\build\pjproject\pjnath\lib;$(ProjectDir)..\contrib\build\libnatpmp\msvc\x64\Release;$(ProjectDir)..\contrib\build\restbed\build\Release;$(ProjectDir)..\contrib\build\restbed\dependency\openssl\out32dll</AdditionalLibraryDirectories>
       <AdditionalOptions>/ignore:4006 /ignore:4221 %(AdditionalOptions)</AdditionalOptions>
       <LinkTimeCodeGeneration>true</LinkTimeCodeGeneration>
diff --git a/configure.ac b/configure.ac
index 1245cd40df..0db2434381 100644
--- a/configure.ac
+++ b/configure.ac
@@ -418,8 +418,17 @@ AS_IF([test "x$enable_restbed_old_api" = "xyes"], [
   AC_DEFINE([RESTBED_OLD_API], [], [Defined if you use the old restbed API])
 ])
 
-AS_IF([test "x$enable_ringns" != "xno" || test "x$with_restcpp" = "xyes"],
-AC_CHECK_LIB(restbed, exit,, AC_MSG_ERROR([Missing restbed files])));
+AS_IF([test "x$enable_ringns" != "xno" || test "x$with_restcpp" = "xyes"], [
+  AC_CHECK_LIB(restbed, exit,, AC_MSG_ERROR([Missing restbed files]))
+  PKG_CHECK_MODULES(LIBCRYPTO, libcrypto,, AC_MSG_ERROR([Missing libcrypto development files]))
+  PKG_CHECK_MODULES(LIBSSL, libssl,, AC_MSG_ERROR([Missing libssl development files]))
+
+  LIBS="${LIBS} -lssl -lcrypto"
+  AS_IF([test "${HAVE_WIN32}" = "1"],[
+      LIBS="${LIBS} -lssleay32 -leay32"
+  ])
+  ], []
+);
 
 AS_IF([test "x$with_restcpp" = "xyes"], [
     AS_AC_EXPAND(SBINDIR, $sbindir)
diff --git a/contrib/build_all_uwp.bat b/contrib/build_all_uwp.bat
index e83698f15a..6af4e3678f 100644
--- a/contrib/build_all_uwp.bat
+++ b/contrib/build_all_uwp.bat
@@ -61,11 +61,20 @@ set MSYS2_BIN="C:\msys64\usr\bin\bash.exe"
 call BuildFFmpeg.bat win10 x64
 cd ..\..
 
+rem * build openssl UWP *
+cd build\restbed\dependency\openssl
+call perl Configure no-asm no-hw no-dso VC-WINUNIVERSAL
+call ms\do_winuniversal
+call ms\setVSvars universal10.0x64
+call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
+call nmake -f ms\ntdll.mak
+set PATH=restbed\dependency\openssl\out32dll;%PATH%
+
 rem * build restbed w/asio *
-cd build\restbed
+cd ..\..
 mkdir build
 cd build
-cmake -DBUILD_SSL=OFF -G "Visual Studio 14 2015 Win64" ..
+cmake -DBUILD_SSL=ON -G "Visual Studio 14 2015 Win64" ..
 cmake --build . --target ALL_BUILD --config Release
 cd ..\..
 
diff --git a/contrib/src/asio/SHA512SUMS b/contrib/src/asio/SHA512SUMS
index 7075eae7c4..1eb007c3d0 100644
--- a/contrib/src/asio/SHA512SUMS
+++ b/contrib/src/asio/SHA512SUMS
@@ -1 +1 @@
-a34e17ee3c9ac38de83314f268328f6bee3a2276b459f692f1d83568d65cc037db34e6fd567e379c8ec4ec0bdb36dc04395ae7148f49b7c24a10efbaeaabdd8d  asio-f5c570826d2ebf50eb38c44039181946a473148b.tar.gz
+cde4a043e24268ffbcea08c6cb5d669fc983631213fe5e7640d0ef58902f6ee2332a4e777accd2a42456eb7c65c1da9c6c0d219f54c407e190ee593d66d8a731  asio-631c4f89dafc771fcaf9ee7aa565ab33168459a6.tar.gz
diff --git a/contrib/src/asio/rules.mak b/contrib/src/asio/rules.mak
index be1dc04386..d933a6db55 100644
--- a/contrib/src/asio/rules.mak
+++ b/contrib/src/asio/rules.mak
@@ -18,8 +18,8 @@
 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 #
 
-ASIO_VERSION := f5c570826d2ebf50eb38c44039181946a473148b
-ASIO_URL := https://github.com/chriskohlhoff/asio/archive/$(ASIO_VERSION).tar.gz
+ASIO_VERSION := 631c4f89dafc771fcaf9ee7aa565ab33168459a6
+ASIO_URL := https://github.com/hlysunnaram/asio/archive/$(ASIO_VERSION).tar.gz
 
 # Pure dependency of restbed: do not add to PKGS.
 
diff --git a/contrib/src/libressl/SHA512SUMS b/contrib/src/libressl/SHA512SUMS
new file mode 100644
index 0000000000..c4895eaa5e
--- /dev/null
+++ b/contrib/src/libressl/SHA512SUMS
@@ -0,0 +1 @@
+f177ba77d6f1ec6e7d843325997e86ad86b309e3e7aa495a7c0960d97ef838690d78c8200077b08e6f430bf007b28949453e5515f788de19fbcc97057560385f  portable-190bd346e75575b9436a2e9e14b28618f0234e1b.tar.gz
diff --git a/contrib/src/libressl/getpagesize.patch b/contrib/src/libressl/getpagesize.patch
new file mode 100644
index 0000000000..38a81fffb4
--- /dev/null
+++ b/contrib/src/libressl/getpagesize.patch
@@ -0,0 +1,28 @@
+From b381d7010e03ada41f36f529468b1ee553a9c765 Mon Sep 17 00:00:00 2001
+From: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
+Date: Fri, 9 Mar 2018 12:07:17 -0500
+Subject: [PATCH] fix redefinition of getpagesize
+
+---
+ crypto/compat/getpagesize.c | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/crypto/compat/getpagesize.c b/crypto/compat/getpagesize.c
+index 098efa9..79fcc56 100644
+--- a/crypto/compat/getpagesize.c
++++ b/crypto/compat/getpagesize.c
+@@ -6,6 +6,7 @@
+ #include <windows.h>
+ #endif
+
++#ifndef __ANDROID__
+ int
+ getpagesize(void) {
+ #ifdef _MSC_VER
+@@ -16,3 +17,4 @@ getpagesize(void) {
+ 	return sysconf(_SC_PAGESIZE);
+ #endif
+ }
++#endif
+--
+2.14.3
diff --git a/contrib/src/libressl/rules.mak b/contrib/src/libressl/rules.mak
new file mode 100644
index 0000000000..a39219b93d
--- /dev/null
+++ b/contrib/src/libressl/rules.mak
@@ -0,0 +1,53 @@
+# -*- mode: makefile; -*-
+#
+#  Copyright (C) 2018 Savoir-faire Linux Inc.
+#
+#  Author: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
+#
+#  This program 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.
+#
+#  This program 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, write to the Free Software
+#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+#
+
+LIBRESSL_VERSION := 190bd346e75575b9436a2e9e14b28618f0234e1b
+LIBRESSL_URL := https://github.com/libressl-portable/portable/archive/$(LIBRESSL_VERSION).tar.gz
+
+
+# Pure dependency of restbed: do not add to PKGS.
+
+$(TARBALLS)/portable-$(LIBRESSL_VERSION).tar.gz:
+	$(call download,$(LIBRESSL_URL))
+
+libressl: portable-$(LIBRESSL_VERSION).tar.gz
+	$(UNPACK)
+	$(APPLY) $(SRC)/libressl/getpagesize.patch
+	$(MOVE)
+
+.libressl: libressl .sum-libressl
+	mkdir -p "$(PREFIX)/include"
+	cd $< && ./autogen.sh
+ifdef HAVE_WIN32
+	cd $< && CC=i686-w64-mingw32-gcc CPPFLAGS=-D__MINGW_USE_VC2005_COMPAT ./configure --host=i686-w64-mingw32 && $(MAKE) && DESTDIR=$(PREFIX) $(MAKE) install
+else ifdef HAVE_WIN64
+	cd $< && CC=x86_64-w64-mingw32-gcc ./configure --host=x86_64-w64-mingw32 && $(MAKE) && DESTDIR=$(PREFIX) $(MAKE) install
+else ifeq ($(IOS_TARGET_PLATFORM),iPhoneOS)
+	cd $< && mkdir build && cd build && $(CMAKE) -DDESTDIR=$(PREFIX) -DCMAKE_C_FLAGS='-miphoneos-version-min=9.3 -fembed-bitcode -arch arm64' .. && $(MAKE) && $(MAKE) install
+else ifeq ($(IOS_TARGET_PLATFORM),iPhoneSimulator)
+	cd $< && mkdir build && cd build && $(CMAKE) -DDESTDIR=$(PREFIX) -DCMAKE_C_FLAGS='-miphoneos-version-min=9.3 -fembed-bitcode -arch x86_64' .. && $(MAKE) && $(MAKE) install
+else
+	cd $< && mkdir build && cd build && $(CMAKE) -DDESTDIR=$(PREFIX) .. && $(MAKE) && $(MAKE) install
+endif
+	rm -rf $(PREFIX)/lib/*.so $(PREFIX)/lib/*.so.*
+	touch $@
+
+.sum-libressl: portable-$(LIBRESSL_VERSION).tar.gz
diff --git a/contrib/src/restbed/fetch_and_patch.bat b/contrib/src/restbed/fetch_and_patch.bat
index 95729d6464..fa1423947e 100644
--- a/contrib/src/restbed/fetch_and_patch.bat
+++ b/contrib/src/restbed/fetch_and_patch.bat
@@ -11,6 +11,8 @@ if %USE_CACHE%==1 (
     wget %RESTBED_URL%
 )
 
+rem ------------ restbed ------------
+
 7z -y x %RESTBED_VERSION%.tar.gz && 7z -y x %RESTBED_VERSION%.tar -o%BUILD%
 del %RESTBED_VERSION%.tar && del %RESTBED_VERSION%.tar.gz && del %BUILD%\pax_global_header
 rename %BUILD%\restbed-%RESTBED_VERSION% restbed
@@ -25,6 +27,8 @@ rmdir /s /q %BUILD%\restbed\dependency
 mkdir %BUILD%\restbed\dependency
 cd %BUILD%\restbed\dependency
 
+rem ------------ asio ------------
+
 set ASIO_VERSION=276846097ab5073b67e772dbdfa12596224a54a5
 set ASIO_URL=https://github.com/Corvusoft/asio-dependency/archive/%ASIO_VERSION%.tar.gz
 
@@ -40,9 +44,10 @@ rename asio-dependency-%ASIO_VERSION% asio
 
 cd asio
 git apply --reject --whitespace=fix %SRC%\restbed\asio-uwp.patch
-
 cd ..
 
+rem ------------ catch ------------
+
 set CATCH_VERSION=35f510545d55a831372d3113747bf1314ff4f2ef
 set CATCH_URL=https://github.com/Corvusoft/catch-dependency/archive/%CATCH_VERSION%.tar.gz
 
@@ -56,6 +61,23 @@ if %USE_CACHE%==1 (
 del %CATCH_VERSION%.tar && del %CATCH_VERSION%.tar.gz && del pax_global_header
 rename catch-dependency-%CATCH_VERSION% catch
 
+rem ------------ openssl ------------
+
+set OPENSSL_VERSION=c7ba244789ce9f9b6675ff88e61dd5d5e5cac53e
+set OPENSSL_URL=https://github.com/Microsoft/openssl/archive/%OPENSSL_VERSION%.tar.gz
+
+if %USE_CACHE%==1 (
+    copy %CACHE_DIR%\%OPENSSL_VERSION%.tar.gz %cd%
+) else (
+    wget %OPENSSL_URL%
+)
+
+7z -y x %OPENSSL_VERSION%.tar.gz && 7z -y x %OPENSSL_VERSION%.tar
+del %OPENSSL_VERSION%.tar && del %OPENSSL_VERSION%.tar.gz && del pax_global_header
+rename openssl-%OPENSSL_VERSION% openssl
+
+rem ------------ kashmir ------------
+
 set KASHMIR_VERSION=2f3913f49c4ac7f9bff9224db5178f6f8f0ff3ee
 set KASHMIR_URL=https://github.com/corvusoft/kashmir-dependency/archive/%KASHMIR_VERSION%.tar.gz
 
diff --git a/contrib/src/restbed/findkashmir.patch b/contrib/src/restbed/findkashmir.patch
deleted file mode 100644
index c890fcb2bb..0000000000
--- a/contrib/src/restbed/findkashmir.patch
+++ /dev/null
@@ -1,16 +0,0 @@
---- a/cmake/modules/Findkashmir.cmake	2017-06-02 12:01:30.415787179 -0400
-+++ b/cmake/modules/Findkashmir.cmake	2017-06-02 12:01:33.329120391 -0400
-@@ -1,11 +1,2 @@
--# Copyright 2014-2016, Corvusoft Ltd, All Rights Reserved.
--
--find_path( kashmir_INCLUDE kashmir HINTS "${PROJECT_SOURCE_DIR}/dependency/kashmir" "/usr/include" "/usr/local/include" "/opt/local/include" )
--
--if ( kashmir_INCLUDE )
--    set( KASHMIR_FOUND TRUE )
--
--    message( STATUS "${Green}Found Kashmir include at: ${kashmir_INCLUDE}${Reset}" )
--else ( )
--    message( FATAL_ERROR "${Red}Failed to locate Kashmir dependency.${Reset}" )
--endif ( )
-+set( KASHMIR_FOUND TRUE )
-+set( kashmir_INCLUDE "${PROJECT_SOURCE_DIR}/dependency/kashmir" )
diff --git a/contrib/src/restbed/rules.mak b/contrib/src/restbed/rules.mak
index 957a06f8b1..318c675453 100644
--- a/contrib/src/restbed/rules.mak
+++ b/contrib/src/restbed/rules.mak
@@ -20,7 +20,7 @@
 #
 
 RESTBED_VERSION := df867a858dddc4cf6ca8642da02720bd65ba239a
-RESTBED_URL := https://github.com/corvusoft//restbed/archive/$(RESTBED_VERSION).tar.gz
+RESTBED_URL := https://github.com/corvusoft/restbed/archive/$(RESTBED_VERSION).tar.gz
 
 # Pure dependency of OpenDHT: do not add to PKGS.
 
@@ -31,11 +31,11 @@ endif
 $(TARBALLS)/restbed-$(RESTBED_VERSION).tar.gz:
 	$(call download,$(RESTBED_URL))
 
-DEPS_restbed = asio kashmir-dependency
+DEPS_restbed = asio kashmir-dependency libressl
 
 RESTBED_CONF = -DBUILD_TESTS=NO \
 			-DBUILD_EXAMPLES=NO \
-			-DBUILD_SSL=NO \
+			-DBUILD_SSL=YES \
 			-DBUILD_SHARED=NO \
 			-DCMAKE_INCLUDE_PATH=$(PREFIX)/include \
 			-DCMAKE_INSTALL_PREFIX=$(PREFIX) \
diff --git a/src/ringdht/namedirectory.cpp b/src/ringdht/namedirectory.cpp
index 5988720057..c5f2033dc9 100644
--- a/src/ringdht/namedirectory.cpp
+++ b/src/ringdht/namedirectory.cpp
@@ -36,7 +36,7 @@ namespace ring {
 
 constexpr const char* const QUERY_NAME {"/name/"};
 constexpr const char* const QUERY_ADDR {"/addr/"};
-constexpr const char* const HTTP_PROTO {"http://"};
+constexpr const char* const HTTPS_PROTO {"https://"};
 
 /** Parser for Ring URIs.         ( protocol        )    ( username         ) ( hostname                            ) */
 const std::regex URI_VALIDATOR {"^([a-zA-Z]+:(?://)?)?(?:([a-z0-9-_]{1,64})@)?([a-zA-Z0-9\\-._~%!$&'()*+,;=:\\[\\]]+)"};
@@ -106,7 +106,7 @@ void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
             return;
         }
 
-        restbed::Uri uri(HTTP_PROTO + serverHost_ + QUERY_ADDR + addr);
+        restbed::Uri uri(HTTPS_PROTO + serverHost_ + QUERY_ADDR + addr);
         auto req = std::make_shared<restbed::Request>(uri);
         req->set_header("Accept", "*/*");
         req->set_header("Host", serverHost_);
@@ -177,7 +177,7 @@ void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
             return;
         }
 
-        restbed::Uri uri(HTTP_PROTO + serverHost_ + QUERY_NAME + name);
+        restbed::Uri uri(HTTPS_PROTO + serverHost_ + QUERY_NAME + name);
         auto request = std::make_shared<restbed::Request>(std::move(uri));
         request->set_header("Accept", "*/*");
         request->set_header("Host", serverHost_);
@@ -258,7 +258,7 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
             return;
         }
 
-        auto request = std::make_shared<restbed::Request>(restbed::Uri(HTTP_PROTO + serverHost_ + QUERY_NAME + name));
+        auto request = std::make_shared<restbed::Request>(restbed::Uri(HTTPS_PROTO + serverHost_ + QUERY_NAME + name));
         request->set_header("Accept", "*/*");
         request->set_header("Host", serverHost_);
         request->set_header("Content-Type", "application/json");
-- 
GitLab