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.

Committer:
Osamu Koizumi
Date:
Tue Apr 17 18:08:10 2018 +0900
Revision:
1:65339c530def
Parent:
0:f96053986356
Child:
3:cf61a5596cf2
Updated TLSSocket library to fix known bug. Changed printout messages.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coisme 0:f96053986356 1 #include "mbed.h"
coisme 0:f96053986356 2 #include "easy-connect.h"
coisme 0:f96053986356 3 #include "TLSSocket.h"
coisme 0:f96053986356 4
coisme 0:f96053986356 5 const char* HOST_NAME = "os.mbed.com";
coisme 0:f96053986356 6 const int PORT = 443;
coisme 0:f96053986356 7 const char* HTTPS_PATH = "/";
coisme 0:f96053986356 8 const char* ROOT_CA_PEM = /* Root CA of os.mbed.com */
coisme 0:f96053986356 9 "-----BEGIN CERTIFICATE-----\n"
coisme 0:f96053986356 10 "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n"
coisme 0:f96053986356 11 "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n"
coisme 0:f96053986356 12 "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n"
coisme 0:f96053986356 13 "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n"
coisme 0:f96053986356 14 "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n"
coisme 0:f96053986356 15 "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n"
coisme 0:f96053986356 16 "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n"
coisme 0:f96053986356 17 "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n"
coisme 0:f96053986356 18 "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n"
coisme 0:f96053986356 19 "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n"
coisme 0:f96053986356 20 "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n"
coisme 0:f96053986356 21 "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n"
coisme 0:f96053986356 22 "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n"
coisme 0:f96053986356 23 "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n"
coisme 0:f96053986356 24 "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n"
coisme 0:f96053986356 25 "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n"
coisme 0:f96053986356 26 "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n"
coisme 0:f96053986356 27 "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n"
coisme 0:f96053986356 28 "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n"
coisme 0:f96053986356 29 "-----END CERTIFICATE-----";
coisme 0:f96053986356 30
coisme 0:f96053986356 31
coisme 0:f96053986356 32 int main(int argc, char* argv[]) {
coisme 0:f96053986356 33 printf("HelloTSLSocket, HTTPS example of TLSSocket\r\n");
Osamu Koizumi 1:65339c530def 34 printf("\r\n");
coisme 0:f96053986356 35
Osamu Koizumi 1:65339c530def 36 // Open a network interface
coisme 0:f96053986356 37 NetworkInterface* network = NULL;
coisme 0:f96053986356 38 network = easy_connect(false); // If true, prints out connection details.
coisme 0:f96053986356 39 if (!network) {
coisme 0:f96053986356 40 printf("Unable to open network interface.\r\n");
coisme 0:f96053986356 41 return -1;
coisme 0:f96053986356 42 }
coisme 0:f96053986356 43
Osamu Koizumi 1:65339c530def 44 // Create a TLS socket
coisme 0:f96053986356 45 TLSSocket socket = TLSSocket();
coisme 0:f96053986356 46 if(socket.open(network) != 0) {
coisme 0:f96053986356 47 printf("Unable to open TLS socket.\r\n");
coisme 0:f96053986356 48 return -1;
coisme 0:f96053986356 49 }
Osamu Koizumi 1:65339c530def 50
Osamu Koizumi 1:65339c530def 51 // Connect to the server, including TLS handshake
coisme 0:f96053986356 52 if(socket.connect(HOST_NAME, PORT, ROOT_CA_PEM) != 0) {
coisme 0:f96053986356 53 printf("Failed to connect to the server.");
coisme 0:f96053986356 54 return -1;
coisme 0:f96053986356 55 }
coisme 0:f96053986356 56
coisme 0:f96053986356 57 const size_t buf_size = 2048;
coisme 0:f96053986356 58 char *buf = new char[buf_size];
coisme 0:f96053986356 59
Osamu Koizumi 1:65339c530def 60 // Send HTTP request
Osamu Koizumi 1:65339c530def 61 /* "Connection: close" header is specified to detect end of the body
Osamu Koizumi 1:65339c530def 62 * contents by connection close notification. If this is not specified,
Osamu Koizumi 1:65339c530def 63 * connection is kept, and need to detect end of the content in another
Osamu Koizumi 1:65339c530def 64 * way.
Osamu Koizumi 1:65339c530def 65 */
Osamu Koizumi 1:65339c530def 66 int len = snprintf(buf, buf_size,
Osamu Koizumi 1:65339c530def 67 "GET %s HTTP/1.1\n"
Osamu Koizumi 1:65339c530def 68 "Host: %s\n"
Osamu Koizumi 1:65339c530def 69 "Connection: close\n"
Osamu Koizumi 1:65339c530def 70 "\n", HTTPS_PATH, HOST_NAME);
Osamu Koizumi 1:65339c530def 71 printf("\r\n%s", buf);
coisme 0:f96053986356 72 int rc = 0;
coisme 0:f96053986356 73 rc = socket.send(buf, len);
coisme 0:f96053986356 74 if(rc < 0) {
coisme 0:f96053986356 75 printf("send error.\r\n");
coisme 0:f96053986356 76 return -1;
coisme 0:f96053986356 77 }
Osamu Koizumi 1:65339c530def 78
Osamu Koizumi 1:65339c530def 79 // Receive response from the server
coisme 0:f96053986356 80 while((rc = socket.recv(buf, buf_size - 1)) > 0) {
coisme 0:f96053986356 81 buf[rc] = '\0';
coisme 0:f96053986356 82 printf("%s", buf);
coisme 0:f96053986356 83 }
coisme 0:f96053986356 84 if(rc < 0) {
coisme 0:f96053986356 85 printf("\r\n! Read failed. err code = %d\r\n", rc);
coisme 0:f96053986356 86 }
Osamu Koizumi 1:65339c530def 87
Osamu Koizumi 1:65339c530def 88 // Done
Osamu Koizumi 1:65339c530def 89 printf("HelloTSLSocket DONE.\r\n");
coisme 0:f96053986356 90 delete[] buf;
coisme 0:f96053986356 91
coisme 0:f96053986356 92 socket.close();
coisme 0:f96053986356 93 }