![](/media/cache/profiles/hamster.jpg.50x50_q85.jpg)
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.
main.cpp@13:e82471c0624c, 2018-06-15 (annotated)
- Committer:
- Osamu Koizumi
- Date:
- Fri Jun 15 00:43:00 2018 +0900
- Revision:
- 13:e82471c0624c
- Parent:
- 12:7d952bcda2c1
Updated TLSSocket library.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Osamu Koizumi |
3:cf61a5596cf2 | 5 | #include "mbed-trace/mbed_trace.h" |
Osamu Koizumi |
3:cf61a5596cf2 | 6 | |
coisme | 0:f96053986356 | 7 | const char* HOST_NAME = "os.mbed.com"; |
coisme | 0:f96053986356 | 8 | const int PORT = 443; |
coisme | 0:f96053986356 | 9 | const char* HTTPS_PATH = "/"; |
coisme | 0:f96053986356 | 10 | |
coisme | 0:f96053986356 | 11 | int main(int argc, char* argv[]) { |
Osamu Koizumi |
3:cf61a5596cf2 | 12 | mbed_trace_init(); |
Osamu Koizumi |
3:cf61a5596cf2 | 13 | |
coisme | 0:f96053986356 | 14 | printf("HelloTSLSocket, HTTPS example of TLSSocket\r\n"); |
Osamu Koizumi |
1:65339c530def | 15 | printf("\r\n"); |
coisme | 0:f96053986356 | 16 | |
Osamu Koizumi |
1:65339c530def | 17 | // Open a network interface |
coisme | 0:f96053986356 | 18 | NetworkInterface* network = NULL; |
coisme | 0:f96053986356 | 19 | network = easy_connect(false); // If true, prints out connection details. |
coisme | 0:f96053986356 | 20 | if (!network) { |
coisme | 0:f96053986356 | 21 | printf("Unable to open network interface.\r\n"); |
coisme | 0:f96053986356 | 22 | return -1; |
coisme | 0:f96053986356 | 23 | } |
coisme | 0:f96053986356 | 24 | |
Osamu Koizumi |
1:65339c530def | 25 | // Create a TLS socket |
Osamu Koizumi |
7:fc43e66fb54a | 26 | TLSSocket socket = TLSSocket(); |
Osamu Koizumi |
7:fc43e66fb54a | 27 | if(socket.open(network) != 0) { |
coisme | 0:f96053986356 | 28 | printf("Unable to open TLS socket.\r\n"); |
coisme | 0:f96053986356 | 29 | return -1; |
coisme | 0:f96053986356 | 30 | } |
Osamu Koizumi |
1:65339c530def | 31 | |
Osamu Koizumi |
12:7d952bcda2c1 | 32 | // Set root CA certificate |
Osamu Koizumi |
12:7d952bcda2c1 | 33 | socket.set_root_ca_cert(MBED_CONF_APP_ROOT_CA_CERT_PEM); |
Osamu Koizumi |
12:7d952bcda2c1 | 34 | |
Osamu Koizumi |
1:65339c530def | 35 | // Connect to the server, including TLS handshake |
Osamu Koizumi |
9:38b485904577 | 36 | if(socket.connect(HOST_NAME, PORT) != 0) { |
coisme | 0:f96053986356 | 37 | printf("Failed to connect to the server."); |
coisme | 0:f96053986356 | 38 | return -1; |
coisme | 0:f96053986356 | 39 | } |
coisme | 0:f96053986356 | 40 | |
Osamu Koizumi |
6:75b01b028cd4 | 41 | const size_t buf_size = 1024; |
coisme | 0:f96053986356 | 42 | char *buf = new char[buf_size]; |
coisme | 0:f96053986356 | 43 | |
Osamu Koizumi |
1:65339c530def | 44 | // Send HTTP request |
Osamu Koizumi |
1:65339c530def | 45 | /* "Connection: close" header is specified to detect end of the body |
Osamu Koizumi |
1:65339c530def | 46 | * contents by connection close notification. If this is not specified, |
Osamu Koizumi |
1:65339c530def | 47 | * connection is kept, and need to detect end of the content in another |
Osamu Koizumi |
1:65339c530def | 48 | * way. |
Osamu Koizumi |
1:65339c530def | 49 | */ |
Osamu Koizumi |
1:65339c530def | 50 | int len = snprintf(buf, buf_size, |
Osamu Koizumi |
1:65339c530def | 51 | "GET %s HTTP/1.1\n" |
Osamu Koizumi |
1:65339c530def | 52 | "Host: %s\n" |
Osamu Koizumi |
1:65339c530def | 53 | "Connection: close\n" |
Osamu Koizumi |
1:65339c530def | 54 | "\n", HTTPS_PATH, HOST_NAME); |
Osamu Koizumi |
1:65339c530def | 55 | printf("\r\n%s", buf); |
coisme | 0:f96053986356 | 56 | int rc = 0; |
Osamu Koizumi |
7:fc43e66fb54a | 57 | rc = socket.send(buf, len); |
coisme | 0:f96053986356 | 58 | if(rc < 0) { |
coisme | 0:f96053986356 | 59 | printf("send error.\r\n"); |
coisme | 0:f96053986356 | 60 | return -1; |
coisme | 0:f96053986356 | 61 | } |
Osamu Koizumi |
1:65339c530def | 62 | |
Osamu Koizumi |
6:75b01b028cd4 | 63 | // Receive response from the server |
Osamu Koizumi |
7:fc43e66fb54a | 64 | while((rc = socket.recv(buf, buf_size - 1)) > 0) { |
coisme | 0:f96053986356 | 65 | buf[rc] = '\0'; |
coisme | 0:f96053986356 | 66 | printf("%s", buf); |
coisme | 0:f96053986356 | 67 | } |
coisme | 0:f96053986356 | 68 | if(rc < 0) { |
coisme | 0:f96053986356 | 69 | printf("\r\n! Read failed. err code = %d\r\n", rc); |
coisme | 0:f96053986356 | 70 | } |
Osamu Koizumi |
1:65339c530def | 71 | |
Osamu Koizumi |
1:65339c530def | 72 | // Done |
Osamu Koizumi |
1:65339c530def | 73 | printf("HelloTSLSocket DONE.\r\n"); |
coisme | 0:f96053986356 | 74 | delete[] buf; |
coisme | 0:f96053986356 | 75 | |
Osamu Koizumi |
7:fc43e66fb54a | 76 | socket.close(); |
coisme | 0:f96053986356 | 77 | } |