From a6b28adcc6afef2258a0790f1ca2a50bdae83982 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 1 Feb 2021 14:00:01 +0100 Subject: [PATCH 1/3] Fixing WiFi code to allow HTTPS-GET instead of HTTP-GET (required for Arduino IoT Cloud OTA). --- libraries/WiFi/src/WiFiHelpers.cpp | 13 ++++++++++--- libraries/WiFi/src/utility/https_request.h | 15 +++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/libraries/WiFi/src/WiFiHelpers.cpp b/libraries/WiFi/src/WiFiHelpers.cpp index 353515b30..edb6952c9 100644 --- a/libraries/WiFi/src/WiFiHelpers.cpp +++ b/libraries/WiFi/src/WiFiHelpers.cpp @@ -17,6 +17,7 @@ #include "WiFi.h" #include "mbed.h" #include "utility/http_request.h" +#include "utility/https_request.h" static FILE* target; @@ -26,7 +27,13 @@ void body_callback(const char* data, uint32_t data_len) { int WiFiClass::download(char* url, const char* target_file) { target = fopen(target_file, "wb"); - HttpRequest* req = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback); - req->send(NULL, 0); - fclose(target); + HttpsRequest* req = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback); + if (req->send(NULL, 0) == NULL) + { + fclose(target); + return req->get_error(); + } + int const size = ftell(target); + fclose(target); + return size; } diff --git a/libraries/WiFi/src/utility/https_request.h b/libraries/WiFi/src/utility/https_request.h index d5335ddb7..9dad7dc1c 100644 --- a/libraries/WiFi/src/utility/https_request.h +++ b/libraries/WiFi/src/utility/https_request.h @@ -49,16 +49,20 @@ class HttpsRequest : public HttpRequestBase { const char* ssl_ca_pem, http_method method, const char* url, - Callback body_callback = 0) + mbed::Callback body_callback = 0) : HttpRequestBase(NULL, body_callback) { + _error = 0; + _network = network; + _parsed_url = new ParsedUrl(url); _request_builder = new HttpRequestBuilder(method, _parsed_url); _response = NULL; _socket = new TLSSocket(); ((TLSSocket*)_socket)->open(network); - ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); + //((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); + ((TLSSocket*)_socket)->set_root_ca_cert("/wlan/", 0); _we_created_socket = true; } @@ -76,7 +80,7 @@ class HttpsRequest : public HttpRequestBase { HttpsRequest(TLSSocket* socket, http_method method, const char* url, - Callback body_callback = 0) + mbed::Callback body_callback = 0) : HttpRequestBase(socket, body_callback) { _parsed_url = new ParsedUrl(url); @@ -91,7 +95,10 @@ class HttpsRequest : public HttpRequestBase { protected: virtual nsapi_error_t connect_socket(char *host, uint16_t port) { - return ((TLSSocket*)_socket)->connect(host, port); + SocketAddress socketAddress = SocketAddress(); + socketAddress.set_port(port); + _network->gethostbyname(host, &socketAddress); + return ((TLSSocket*)_socket)->connect(socketAddress); } }; From 8af1b1915c50c9f261d75a0bee791142b74a299b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 1 Feb 2021 14:31:56 +0100 Subject: [PATCH 2/3] If no SSL certificate is provided directly then load the ones stored on QSPI. --- libraries/WiFi/src/utility/https_request.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/WiFi/src/utility/https_request.h b/libraries/WiFi/src/utility/https_request.h index 9dad7dc1c..8458722ef 100644 --- a/libraries/WiFi/src/utility/https_request.h +++ b/libraries/WiFi/src/utility/https_request.h @@ -61,8 +61,10 @@ class HttpsRequest : public HttpRequestBase { _socket = new TLSSocket(); ((TLSSocket*)_socket)->open(network); - //((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); - ((TLSSocket*)_socket)->set_root_ca_cert("/wlan/", 0); + if (ssl_ca_pem) + ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); + else + ((TLSSocket*)_socket)->set_root_ca_cert("/wlan/", 0); _we_created_socket = true; } From ad8b8d5dcd53946d7969b35215c83ad86c8dc709 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 2 Feb 2021 09:54:39 +0100 Subject: [PATCH 3/3] Support both HTTP and HTTPS-GET --- libraries/WiFi/src/WiFi.h | 2 +- libraries/WiFi/src/WiFiHelpers.cpp | 36 +++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 5352e98b5..6f9a6566a 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -288,7 +288,7 @@ class WiFiClass int ping(const String &hostname, uint8_t ttl = 128); int ping(IPAddress host, uint8_t ttl = 128); - int download(char* url, const char* target); + int download(char* url, const char* target, bool const is_https = false); friend class WiFiClient; friend class WiFiServer; diff --git a/libraries/WiFi/src/WiFiHelpers.cpp b/libraries/WiFi/src/WiFiHelpers.cpp index edb6952c9..3d1e35cc8 100644 --- a/libraries/WiFi/src/WiFiHelpers.cpp +++ b/libraries/WiFi/src/WiFiHelpers.cpp @@ -21,18 +21,42 @@ static FILE* target; -void body_callback(const char* data, uint32_t data_len) { +void body_callback(const char* data, uint32_t data_len) +{ fwrite(data, 1, data_len, target); } -int WiFiClass::download(char* url, const char* target_file) { +int WiFiClass::download(char* url, const char* target_file, bool const is_https) +{ target = fopen(target_file, "wb"); - HttpsRequest* req = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback); - if (req->send(NULL, 0) == NULL) + + HttpRequest * req_http = nullptr; + HttpsRequest * req_https = nullptr; + HttpResponse * rsp = nullptr; + + if (is_https) + { + req_https = new HttpsRequest(getNetwork(), nullptr, HTTP_GET, url, &body_callback); + rsp = req_https->send(NULL, 0); + if (rsp == NULL) { + fclose(target); + return req_https->get_error(); + } + } + else { - fclose(target); - return req->get_error(); + req_http = new HttpRequest(getNetwork(), HTTP_GET, url, &body_callback); + rsp = req_http->send(NULL, 0); + if (rsp == NULL) { + fclose(target); + return req_http->get_error(); + } } + + while (!rsp->is_message_complete()) { + delay(10); + } + int const size = ftell(target); fclose(target); return size;