Https example program using TLSSocket library.
Hello-TLSSocket
TLSSocket library example program.
In this example, https connection to os.mbed.com is established by using TLSSocket. The program is tested on K64F.
Output from console
When set mbed-trace.enable true.
HelloTSLSocket, HTTPS example of TLSSocket [INFO][TLSx]: Connecting to os.mbed.com:443 [INFO][TLSx]: Connected. [INFO][TLSx]: Starting the TLS handshake... [INFO][TLSx]: TLS connection to os.mbed.com:443 established [DBG ][TLSx]: Server certificate: cert. version : 3 serial number : 03:56:D4:79:41:63:31:CA:E0:56:06:61 issuer name : C=BE, O=GlobalSign nv-sa, CN=GlobalSign Organization Validation CA - SHA256 - G2 subject name : C=GB, ST=Cambridgeshire, L=Cambridge, O=Arm Ltd, CN=*.mbed.com issued on : 2018-05-04 15:36:03 expires on : 2019-06-06 10:31:02 signed using : RSA with SHA-256 RSA key size : 2048 bits basic constraints : CA=false subject alt name : *.mbed.com, mbed.org, *.mbed.org, mbed.com key usage : Digital Signature, Key Encipherment ext key usage : TLS Web Server Authentication, TLS Web Client Authentication [INFO][TLSx]: Certificate verification passed GET / HTTP/1.1 Host: os.mbed.com Connection: close HTTP/1.1 200 OK Server: nginx/1.11.12 Date: Wed, 13 Jun 2018 08:26:02 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: close Vary: Accept-Language,Cookie,Accept-Encoding Content-Language: en-gb Set-Cookie: csrftoken=zM3AGfeZ6W4OQZsT6nCcxNBYxEEN73sf; expires=Wed, 12-Jun-2019 08:25:33 GMT; Max-Age=31449600; Path=/ Strict-Transport-Security: max-age=31536000; includeSubdomains eae <!DOCTYPE html> ... ... ... AJAX_req.onreadystatechange = handle_AJAX_Complete; AJAX_req.send(); } </script> </body> </html> 0 HelloTSLSocket DONE.
Diff: main.cpp
- Revision:
- 1:65339c530def
- Parent:
- 0:f96053986356
- Child:
- 3:cf61a5596cf2
diff -r f96053986356 -r 65339c530def main.cpp --- a/main.cpp Tue Apr 17 03:54:46 2018 +0000 +++ b/main.cpp Tue Apr 17 18:08:10 2018 +0900 @@ -2,7 +2,6 @@ #include "easy-connect.h" #include "TLSSocket.h" -#if 0 const char* HOST_NAME = "os.mbed.com"; const int PORT = 443; const char* HTTPS_PATH = "/"; @@ -28,36 +27,13 @@ "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" "-----END CERTIFICATE-----"; -#endif -const char* HOST_NAME = "www.google.co.jp"; -const int PORT = 443; -const char* HTTPS_PATH = "/"; -const char* ROOT_CA_PEM = /* Root CA of www.google.co.jp */ -"-----BEGIN CERTIFICATE-----\n" -"MIIDVDCCAjygAwIBAgIDAjRWMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT\n" -"MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i\n" -"YWwgQ0EwHhcNMDIwNTIxMDQwMDAwWhcNMjIwNTIxMDQwMDAwWjBCMQswCQYDVQQG\n" -"EwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UEAxMSR2VvVHJ1c3Qg\n" -"R2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2swYYzD9\n" -"9BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9mOSm9BXiLnTjoBbdq\n" -"fnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIuT8rxh0PBFpVXLVDv\n" -"iS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6cJmTM386DGXHKTubU\n" -"1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmRCw7+OC7RHQWa9k0+\n" -"bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5aszPeE4uwc2hGKceeoW\n" -"MPRfwCvocWvk+QIDAQABo1MwUTAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTA\n" -"ephojYn7qwVkDBF9qn1luMrMTjAfBgNVHSMEGDAWgBTAephojYn7qwVkDBF9qn1l\n" -"uMrMTjANBgkqhkiG9w0BAQUFAAOCAQEANeMpauUvXVSOKVCUn5kaFOSPeCpilKIn\n" -"Z57QzxpeR+nBsqTP3UEaBU6bS+5Kb1VSsyShNwrrZHYqLizz/Tt1kL/6cdjHPTfS\n" -"tQWVYrmm3ok9Nns4d0iXrKYgjy6myQzCsplFAMfOEVEiIuCl6rYVSAlk6l5PdPcF\n" -"PseKUgzbFbS9bZvlxrFUaKnjaZC2mqUPuLk/IH2uSrW4nOQdtqvmlKXBx4Ot2/Un\n" -"hw4EbNX/3aBd7YdStysVAq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV\n" -"5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==\n" -"-----END CERTIFICATE-----\n"; int main(int argc, char* argv[]) { printf("HelloTSLSocket, HTTPS example of TLSSocket\r\n"); + printf("\r\n"); + // Open a network interface NetworkInterface* network = NULL; network = easy_connect(false); // If true, prints out connection details. if (!network) { @@ -65,12 +41,14 @@ return -1; } + // Create a TLS socket TLSSocket socket = TLSSocket(); if(socket.open(network) != 0) { printf("Unable to open TLS socket.\r\n"); return -1; } - + + // Connect to the server, including TLS handshake if(socket.connect(HOST_NAME, PORT, ROOT_CA_PEM) != 0) { printf("Failed to connect to the server."); return -1; @@ -79,26 +57,36 @@ const size_t buf_size = 2048; char *buf = new char[buf_size]; - int len = snprintf(buf, buf_size, "GET %s HTTP/1.1\nHost: %s\n\n", - HTTPS_PATH, HOST_NAME); - - printf("%s", buf); + // Send HTTP request + /* "Connection: close" header is specified to detect end of the body + * contents by connection close notification. If this is not specified, + * connection is kept, and need to detect end of the content in another + * way. + */ + int len = snprintf(buf, buf_size, + "GET %s HTTP/1.1\n" + "Host: %s\n" + "Connection: close\n" + "\n", HTTPS_PATH, HOST_NAME); + printf("\r\n%s", buf); int rc = 0; rc = socket.send(buf, len); if(rc < 0) { printf("send error.\r\n"); return -1; } - + + // Receive response from the server while((rc = socket.recv(buf, buf_size - 1)) > 0) { buf[rc] = '\0'; printf("%s", buf); } - if(rc < 0) { printf("\r\n! Read failed. err code = %d\r\n", rc); } - printf("DONE.\r\n"); + + // Done + printf("HelloTSLSocket DONE.\r\n"); delete[] buf; socket.close();