..

Committer:
ImranBilalButt
Date:
Fri Aug 23 13:29:35 2019 +0000
Revision:
0:5f745af3ec9b
..

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ImranBilalButt 0:5f745af3ec9b 1 # WORK-IN-PROGRESS: TLSSocket
ImranBilalButt 0:5f745af3ec9b 2
ImranBilalButt 0:5f745af3ec9b 3 This implementation is based on yet unreleased Abstract socket interfaces currently in Mbed OS master branch.
ImranBilalButt 0:5f745af3ec9b 4 This interface will be released in Mbed OS 5.10, but is not yet released in 5.9
ImranBilalButt 0:5f745af3ec9b 5
ImranBilalButt 0:5f745af3ec9b 6 More info:
ImranBilalButt 0:5f745af3ec9b 7 * https://github.com/ARMmbed/mbed-os-5-docs/pull/610
ImranBilalButt 0:5f745af3ec9b 8 * https://github.com/ARMmbed/mbed-os/pull/7192
ImranBilalButt 0:5f745af3ec9b 9
ImranBilalButt 0:5f745af3ec9b 10 This library is created based on [mbed-os-example-tls-tls-client](https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-tls-tls-client/) project.
ImranBilalButt 0:5f745af3ec9b 11
ImranBilalButt 0:5f745af3ec9b 12 # How to use it
ImranBilalButt 0:5f745af3ec9b 13 ```
ImranBilalButt 0:5f745af3ec9b 14 #include "mbed.h"
ImranBilalButt 0:5f745af3ec9b 15 #include "NetworkInterface.h"
ImranBilalButt 0:5f745af3ec9b 16 #include "TLSSocket.h"
ImranBilalButt 0:5f745af3ec9b 17
ImranBilalButt 0:5f745af3ec9b 18 const char* HOST_NAME = "os.mbed.com";
ImranBilalButt 0:5f745af3ec9b 19 const int PORT = 443;
ImranBilalButt 0:5f745af3ec9b 20 const char* HTTPS_PATH = "/";
ImranBilalButt 0:5f745af3ec9b 21
ImranBilalButt 0:5f745af3ec9b 22 const char* cert = \
ImranBilalButt 0:5f745af3ec9b 23 "-----BEGIN CERTIFICATE-----\n" \
ImranBilalButt 0:5f745af3ec9b 24 "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" \
ImranBilalButt 0:5f745af3ec9b 25 "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n" \
ImranBilalButt 0:5f745af3ec9b 26 "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n" \
ImranBilalButt 0:5f745af3ec9b 27 "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n" \
ImranBilalButt 0:5f745af3ec9b 28 "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n" \
ImranBilalButt 0:5f745af3ec9b 29 "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n" \
ImranBilalButt 0:5f745af3ec9b 30 "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n" \
ImranBilalButt 0:5f745af3ec9b 31 "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n" \
ImranBilalButt 0:5f745af3ec9b 32 "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n" \
ImranBilalButt 0:5f745af3ec9b 33 "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n" \
ImranBilalButt 0:5f745af3ec9b 34 "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n" \
ImranBilalButt 0:5f745af3ec9b 35 "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n" \
ImranBilalButt 0:5f745af3ec9b 36 "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n" \
ImranBilalButt 0:5f745af3ec9b 37 "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n" \
ImranBilalButt 0:5f745af3ec9b 38 "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n" \
ImranBilalButt 0:5f745af3ec9b 39 "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n" \
ImranBilalButt 0:5f745af3ec9b 40 "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n" \
ImranBilalButt 0:5f745af3ec9b 41 "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" \
ImranBilalButt 0:5f745af3ec9b 42 "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" \
ImranBilalButt 0:5f745af3ec9b 43 "-----END CERTIFICATE-----";
ImranBilalButt 0:5f745af3ec9b 44
ImranBilalButt 0:5f745af3ec9b 45
ImranBilalButt 0:5f745af3ec9b 46 /** Demonstrate download from a HTTP server through abstract socket interface
ImranBilalButt 0:5f745af3ec9b 47 * Socket can be any connected socket, TCP or TLS
ImranBilalButt 0:5f745af3ec9b 48 */
ImranBilalButt 0:5f745af3ec9b 49 int http_get(Socket *socket)
ImranBilalButt 0:5f745af3ec9b 50 {
ImranBilalButt 0:5f745af3ec9b 51 const size_t buf_size = 1024;
ImranBilalButt 0:5f745af3ec9b 52 char *buf = new char[buf_size];
ImranBilalButt 0:5f745af3ec9b 53
ImranBilalButt 0:5f745af3ec9b 54 // Send HTTP request
ImranBilalButt 0:5f745af3ec9b 55 /* "Connection: close" header is specified to detect end of the body
ImranBilalButt 0:5f745af3ec9b 56 * contents by connection close notification. If this is not specified,
ImranBilalButt 0:5f745af3ec9b 57 * connection is kept, and need to detect end of the content in another
ImranBilalButt 0:5f745af3ec9b 58 * way.
ImranBilalButt 0:5f745af3ec9b 59 */
ImranBilalButt 0:5f745af3ec9b 60 int len = snprintf(buf, buf_size,
ImranBilalButt 0:5f745af3ec9b 61 "GET %s HTTP/1.1\n"
ImranBilalButt 0:5f745af3ec9b 62 "Host: %s\n"
ImranBilalButt 0:5f745af3ec9b 63 "Connection: close\n"
ImranBilalButt 0:5f745af3ec9b 64 "\n", HTTPS_PATH, HOST_NAME);
ImranBilalButt 0:5f745af3ec9b 65 printf("\n%s", buf);
ImranBilalButt 0:5f745af3ec9b 66 int rc = 0;
ImranBilalButt 0:5f745af3ec9b 67 rc = socket->send(buf, len);
ImranBilalButt 0:5f745af3ec9b 68 if(rc < 0) {
ImranBilalButt 0:5f745af3ec9b 69 printf("send error.\n");
ImranBilalButt 0:5f745af3ec9b 70 return rc;
ImranBilalButt 0:5f745af3ec9b 71 }
ImranBilalButt 0:5f745af3ec9b 72
ImranBilalButt 0:5f745af3ec9b 73 // Receive response from the server
ImranBilalButt 0:5f745af3ec9b 74 while((rc = socket->recv(buf, buf_size - 1)) > 0) {
ImranBilalButt 0:5f745af3ec9b 75 buf[rc] = '\0';
ImranBilalButt 0:5f745af3ec9b 76 printf("%s", buf);
ImranBilalButt 0:5f745af3ec9b 77 }
ImranBilalButt 0:5f745af3ec9b 78 if(rc < 0) {
ImranBilalButt 0:5f745af3ec9b 79 printf("\n! Read failed. err code = %d\n", rc);
ImranBilalButt 0:5f745af3ec9b 80 return rc;
ImranBilalButt 0:5f745af3ec9b 81 }
ImranBilalButt 0:5f745af3ec9b 82
ImranBilalButt 0:5f745af3ec9b 83 delete[] buf;
ImranBilalButt 0:5f745af3ec9b 84 return 0;
ImranBilalButt 0:5f745af3ec9b 85 }
ImranBilalButt 0:5f745af3ec9b 86
ImranBilalButt 0:5f745af3ec9b 87 int main(int argc, char* argv[]) {
ImranBilalButt 0:5f745af3ec9b 88 mbed_trace_init();
ImranBilalButt 0:5f745af3ec9b 89
ImranBilalButt 0:5f745af3ec9b 90 printf("HelloTSLSocket, HTTPS example of TLSSocket\n");
ImranBilalButt 0:5f745af3ec9b 91 printf("\n");
ImranBilalButt 0:5f745af3ec9b 92
ImranBilalButt 0:5f745af3ec9b 93 // Open a network interface
ImranBilalButt 0:5f745af3ec9b 94 NetworkInterface* network = NetworkInterface::get_default_instance();
ImranBilalButt 0:5f745af3ec9b 95
ImranBilalButt 0:5f745af3ec9b 96 if (network->connect()) {
ImranBilalButt 0:5f745af3ec9b 97 printf("Unable to connect to network\n");
ImranBilalButt 0:5f745af3ec9b 98 return -1;
ImranBilalButt 0:5f745af3ec9b 99 }
ImranBilalButt 0:5f745af3ec9b 100
ImranBilalButt 0:5f745af3ec9b 101 printf("Connected to network!\n");
ImranBilalButt 0:5f745af3ec9b 102
ImranBilalButt 0:5f745af3ec9b 103
ImranBilalButt 0:5f745af3ec9b 104 // Create transport socket
ImranBilalButt 0:5f745af3ec9b 105 TCPSocket tcp;
ImranBilalButt 0:5f745af3ec9b 106 nsapi_error_t err = tcp.open(network);
ImranBilalButt 0:5f745af3ec9b 107 MBED_ASSERT(err == NSAPI_ERROR_OK);
ImranBilalButt 0:5f745af3ec9b 108
ImranBilalButt 0:5f745af3ec9b 109 // Resolve target name
ImranBilalButt 0:5f745af3ec9b 110 SocketAddress addr;
ImranBilalButt 0:5f745af3ec9b 111 err = network->gethostbyname(HOST_NAME, &addr);
ImranBilalButt 0:5f745af3ec9b 112 MBED_ASSERT(err == NSAPI_ERROR_OK);
ImranBilalButt 0:5f745af3ec9b 113 addr.set_port(PORT);
ImranBilalButt 0:5f745af3ec9b 114
ImranBilalButt 0:5f745af3ec9b 115 // Connect the trasport
ImranBilalButt 0:5f745af3ec9b 116 printf("Connecting to %s\n", HOST_NAME);
ImranBilalButt 0:5f745af3ec9b 117
ImranBilalButt 0:5f745af3ec9b 118 err = tcp.connect(addr);
ImranBilalButt 0:5f745af3ec9b 119 MBED_ASSERT(err == NSAPI_ERROR_OK);
ImranBilalButt 0:5f745af3ec9b 120
ImranBilalButt 0:5f745af3ec9b 121 // Create a TLS socket
ImranBilalButt 0:5f745af3ec9b 122 TLSSocket tls(&tcp);
ImranBilalButt 0:5f745af3ec9b 123
ImranBilalButt 0:5f745af3ec9b 124 // Set root CA certificate
ImranBilalButt 0:5f745af3ec9b 125 tls.set_root_ca_cert(cert);
ImranBilalButt 0:5f745af3ec9b 126
ImranBilalButt 0:5f745af3ec9b 127 // Start TLS handshake
ImranBilalButt 0:5f745af3ec9b 128 printf("Start TLS handshake\n");
ImranBilalButt 0:5f745af3ec9b 129 if(tls.start_handshake(HOST_NAME) != 0) {
ImranBilalButt 0:5f745af3ec9b 130 printf("Failed to connect to the server.");
ImranBilalButt 0:5f745af3ec9b 131 return -1;
ImranBilalButt 0:5f745af3ec9b 132 }
ImranBilalButt 0:5f745af3ec9b 133
ImranBilalButt 0:5f745af3ec9b 134 err = http_get(&tls);
ImranBilalButt 0:5f745af3ec9b 135 if (err == 0) {
ImranBilalButt 0:5f745af3ec9b 136 printf("HTTP Download succesfull\n");
ImranBilalButt 0:5f745af3ec9b 137 }
ImranBilalButt 0:5f745af3ec9b 138
ImranBilalButt 0:5f745af3ec9b 139 // Done
ImranBilalButt 0:5f745af3ec9b 140 printf("HelloTSLSocket DONE.\n");
ImranBilalButt 0:5f745af3ec9b 141
ImranBilalButt 0:5f745af3ec9b 142 tcp.close();
ImranBilalButt 0:5f745af3ec9b 143 }
ImranBilalButt 0:5f745af3ec9b 144 ```