Ciro Edgardo Romero
/
ISE_CEIoT
Proyecto integrador para asignatura de Especialización
source/main-https.cpp@36:00496782708e, 2021-04-11 (annotated)
- Committer:
- ciror00
- Date:
- Sun Apr 11 02:17:20 2021 +0000
- Revision:
- 36:00496782708e
- Parent:
- 34:7da6cfc032fc
Trabajo integrador de Especializacion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
31:66704f6f17c5 | 1 | #define MBED_CONF_MBED_TRACE_ENABLE 1 |
Jan Jongboom |
31:66704f6f17c5 | 2 | |
Jan Jongboom |
0:85fdc69bc10c | 3 | #include "select-demo.h" |
Jan Jongboom |
0:85fdc69bc10c | 4 | |
Jan Jongboom |
0:85fdc69bc10c | 5 | #if DEMO == DEMO_HTTPS |
Jan Jongboom |
0:85fdc69bc10c | 6 | |
Jan Jongboom |
0:85fdc69bc10c | 7 | #include "mbed.h" |
Jan Jongboom |
31:66704f6f17c5 | 8 | #include "mbed_trace.h" |
Jan Jongboom |
0:85fdc69bc10c | 9 | #include "https_request.h" |
Jan Jongboom |
30:4825e4f38844 | 10 | #include "network-helper.h" |
Jan Jongboom |
0:85fdc69bc10c | 11 | |
Jan Jongboom |
0:85fdc69bc10c | 12 | /* List of trusted root CA certificates |
Jan Jongboom |
34:7da6cfc032fc | 13 | * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org |
Jan Jongboom |
0:85fdc69bc10c | 14 | * |
Jan Jongboom |
10:e79a808ebbe8 | 15 | * To add more root certificates, just concatenate them. |
Jan Jongboom |
0:85fdc69bc10c | 16 | */ |
Jan Jongboom |
34:7da6cfc032fc | 17 | const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
Jan Jongboom |
34:7da6cfc032fc | 18 | "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" |
Jan Jongboom |
34:7da6cfc032fc | 19 | "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" |
Jan Jongboom |
34:7da6cfc032fc | 20 | "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" |
Jan Jongboom |
34:7da6cfc032fc | 21 | "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" |
Jan Jongboom |
34:7da6cfc032fc | 22 | "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" |
Jan Jongboom |
34:7da6cfc032fc | 23 | "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" |
Jan Jongboom |
34:7da6cfc032fc | 24 | "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" |
Jan Jongboom |
34:7da6cfc032fc | 25 | "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" |
Jan Jongboom |
34:7da6cfc032fc | 26 | "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" |
Jan Jongboom |
34:7da6cfc032fc | 27 | "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" |
Jan Jongboom |
34:7da6cfc032fc | 28 | "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" |
Jan Jongboom |
34:7da6cfc032fc | 29 | "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" |
Jan Jongboom |
34:7da6cfc032fc | 30 | "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" |
Jan Jongboom |
34:7da6cfc032fc | 31 | "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" |
Jan Jongboom |
34:7da6cfc032fc | 32 | "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" |
Jan Jongboom |
34:7da6cfc032fc | 33 | "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" |
Jan Jongboom |
34:7da6cfc032fc | 34 | "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" |
Jan Jongboom |
34:7da6cfc032fc | 35 | "rqXRfboQnoZsG4q5WTP468SQvvG5\n" |
Jan Jongboom |
4:27fd8efb5bab | 36 | "-----END CERTIFICATE-----\n" |
Jan Jongboom |
4:27fd8efb5bab | 37 | "-----BEGIN CERTIFICATE-----\n" |
Jan Jongboom |
10:e79a808ebbe8 | 38 | "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" |
Jan Jongboom |
10:e79a808ebbe8 | 39 | "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" |
Jan Jongboom |
10:e79a808ebbe8 | 40 | "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" |
Jan Jongboom |
10:e79a808ebbe8 | 41 | "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" |
Jan Jongboom |
10:e79a808ebbe8 | 42 | "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" |
Jan Jongboom |
10:e79a808ebbe8 | 43 | "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" |
Jan Jongboom |
10:e79a808ebbe8 | 44 | "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" |
Jan Jongboom |
10:e79a808ebbe8 | 45 | "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" |
Jan Jongboom |
10:e79a808ebbe8 | 46 | "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" |
Jan Jongboom |
10:e79a808ebbe8 | 47 | "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" |
Jan Jongboom |
10:e79a808ebbe8 | 48 | "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" |
Jan Jongboom |
10:e79a808ebbe8 | 49 | "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" |
Jan Jongboom |
10:e79a808ebbe8 | 50 | "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" |
Jan Jongboom |
10:e79a808ebbe8 | 51 | "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" |
Jan Jongboom |
10:e79a808ebbe8 | 52 | "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" |
Jan Jongboom |
10:e79a808ebbe8 | 53 | "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" |
Jan Jongboom |
10:e79a808ebbe8 | 54 | "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" |
Jan Jongboom |
10:e79a808ebbe8 | 55 | "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" |
Jan Jongboom |
10:e79a808ebbe8 | 56 | "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" |
Jan Jongboom |
10:e79a808ebbe8 | 57 | "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" |
Jan Jongboom |
10:e79a808ebbe8 | 58 | "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" |
Jan Jongboom |
10:e79a808ebbe8 | 59 | "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" |
Jan Jongboom |
10:e79a808ebbe8 | 60 | "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" |
Jan Jongboom |
10:e79a808ebbe8 | 61 | "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" |
Jan Jongboom |
10:e79a808ebbe8 | 62 | "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" |
Jan Jongboom |
0:85fdc69bc10c | 63 | "-----END CERTIFICATE-----\n"; |
Jan Jongboom |
0:85fdc69bc10c | 64 | |
Jan Jongboom |
0:85fdc69bc10c | 65 | void dump_response(HttpResponse* res) { |
Jan Jongboom |
31:66704f6f17c5 | 66 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 67 | |
Jan Jongboom |
31:66704f6f17c5 | 68 | printf("Headers:\n"); |
Jan Jongboom |
0:85fdc69bc10c | 69 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Jan Jongboom |
31:66704f6f17c5 | 70 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 71 | } |
Jan Jongboom |
31:66704f6f17c5 | 72 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 73 | } |
Jan Jongboom |
0:85fdc69bc10c | 74 | |
Jan Jongboom |
0:85fdc69bc10c | 75 | int main() { |
Jan Jongboom |
30:4825e4f38844 | 76 | NetworkInterface* network = connect_to_default_network_interface(); |
Jan Jongboom |
0:85fdc69bc10c | 77 | if (!network) { |
Jan Jongboom |
30:4825e4f38844 | 78 | printf("Cannot connect to the network, see serial output\n"); |
Jan Jongboom |
0:85fdc69bc10c | 79 | return 1; |
Jan Jongboom |
0:85fdc69bc10c | 80 | } |
Jan Jongboom |
0:85fdc69bc10c | 81 | |
Jan Jongboom |
31:66704f6f17c5 | 82 | mbed_trace_init(); |
Jan Jongboom |
31:66704f6f17c5 | 83 | |
Jan Jongboom |
30:4825e4f38844 | 84 | // GET request to os.mbed.com |
Jan Jongboom |
5:42f713b19337 | 85 | { |
Jan Jongboom |
5:42f713b19337 | 86 | printf("\n----- HTTPS GET request -----\n"); |
Jan Jongboom |
4:27fd8efb5bab | 87 | |
Jan Jongboom |
25:a8be9f3a530c | 88 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); |
Jan Jongboom |
4:27fd8efb5bab | 89 | |
Jan Jongboom |
5:42f713b19337 | 90 | HttpResponse* get_res = get_req->send(); |
Jan Jongboom |
5:42f713b19337 | 91 | if (!get_res) { |
Jan Jongboom |
5:42f713b19337 | 92 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Jan Jongboom |
31:66704f6f17c5 | 93 | return 1; |
Jan Jongboom |
5:42f713b19337 | 94 | } |
Jan Jongboom |
5:42f713b19337 | 95 | printf("\n----- HTTPS GET response -----\n"); |
Jan Jongboom |
5:42f713b19337 | 96 | dump_response(get_res); |
Jan Jongboom |
5:42f713b19337 | 97 | |
Jan Jongboom |
5:42f713b19337 | 98 | delete get_req; |
Jan Jongboom |
0:85fdc69bc10c | 99 | } |
Jan Jongboom |
4:27fd8efb5bab | 100 | |
Jan Jongboom |
4:27fd8efb5bab | 101 | // POST request to httpbin.org |
Jan Jongboom |
5:42f713b19337 | 102 | { |
Jan Jongboom |
5:42f713b19337 | 103 | printf("\n----- HTTPS POST request -----\n"); |
Jan Jongboom |
4:27fd8efb5bab | 104 | |
Jan Jongboom |
5:42f713b19337 | 105 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); |
Jan Jongboom |
5:42f713b19337 | 106 | post_req->set_header("Content-Type", "application/json"); |
Jan Jongboom |
0:85fdc69bc10c | 107 | |
Jan Jongboom |
5:42f713b19337 | 108 | const char body[] = "{\"hello\":\"world\"}"; |
Jan Jongboom |
4:27fd8efb5bab | 109 | |
Jan Jongboom |
5:42f713b19337 | 110 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
Jan Jongboom |
5:42f713b19337 | 111 | if (!post_res) { |
Jan Jongboom |
5:42f713b19337 | 112 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
Jan Jongboom |
5:42f713b19337 | 113 | return 1; |
Jan Jongboom |
5:42f713b19337 | 114 | } |
Jan Jongboom |
5:42f713b19337 | 115 | |
Jan Jongboom |
5:42f713b19337 | 116 | printf("\n----- HTTPS POST response -----\n"); |
Jan Jongboom |
5:42f713b19337 | 117 | dump_response(post_res); |
Jan Jongboom |
5:42f713b19337 | 118 | |
Jan Jongboom |
5:42f713b19337 | 119 | delete post_req; |
Jan Jongboom |
4:27fd8efb5bab | 120 | } |
Jan Jongboom |
4:27fd8efb5bab | 121 | |
Jan Jongboom |
28:9bccd981a393 | 122 | wait(osWaitForever); |
Jan Jongboom |
0:85fdc69bc10c | 123 | } |
Jan Jongboom |
0:85fdc69bc10c | 124 | |
Jan Jongboom |
0:85fdc69bc10c | 125 | #endif |