Skip to content

Add TLSSocket example #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions TLSSocket/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "mbed.h"
#include "mbed_trace.h"

const char cert[] = \
"-----BEGIN CERTIFICATE-----\n"
"MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU\n"
"MBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFkZFRydXN0IEV4dGVybmFs\n"
"IFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBDQSBSb290\n"
"MB4XDTAwMDUzMDEwNDgzOFoXDTIwMDUzMDEwNDgzOFowbzELMAkGA1UEBhMCU0Ux\n"
"FDASBgNVBAoTC0FkZFRydXN0IEFCMSYwJAYDVQQLEx1BZGRUcnVzdCBFeHRlcm5h\n"
"bCBUVFAgTmV0d29yazEiMCAGA1UEAxMZQWRkVHJ1c3QgRXh0ZXJuYWwgQ0EgUm9v\n"
"dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALf3GjPm8gAELTngTlvt\n"
"H7xsD821+iO2zt6bETOXpClMfZOfvUq8k+0DGuOPz+VtUFrWlymUWoCwSXrbLpX9\n"
"uMq/NzgtHj6RQa1wVsfwTz/oMp50ysiQVOnGXw94nZpAPA6sYapeFI+eh6FqUNzX\n"
"mk6vBbOmcZSccbNQYArHE504B4YCqOmoaSYYkKtMsE8jqzpPhNjfzp/haW+710LX\n"
"a0Tkx63ubUFfclpxCDezeWWkWaCUN/cALw3CknLa0Dhy2xSoRcRdKn23tNbE7qzN\n"
"E0S3ySvdQwAl+mG5aWpYIxG3pzOPVnVZ9c0p10a3CitlttNCbxWyuHv77+ldU9U0\n"
"WicCAwEAAaOB3DCB2TAdBgNVHQ4EFgQUrb2YejS0Jvf6xCZU7wO94CTLVBowCwYD\n"
"VR0PBAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wgZkGA1UdIwSBkTCBjoAUrb2YejS0\n"
"Jvf6xCZU7wO94CTLVBqhc6RxMG8xCzAJBgNVBAYTAlNFMRQwEgYDVQQKEwtBZGRU\n"
"cnVzdCBBQjEmMCQGA1UECxMdQWRkVHJ1c3QgRXh0ZXJuYWwgVFRQIE5ldHdvcmsx\n"
"IjAgBgNVBAMTGUFkZFRydXN0IEV4dGVybmFsIENBIFJvb3SCAQEwDQYJKoZIhvcN\n"
"AQEFBQADggEBALCb4IUlwtYj4g+WBpKdQZic2YR5gdkeWxQHIzZlj7DYd7usQWxH\n"
"YINRsPkyPef89iYTx4AWpb9a/IfPeHmJIZriTAcKhjW88t5RxNKWt9x+Tu5w/Rw5\n"
"6wwCURQtjr0W4MHfRnXnJK3s9EK0hZNwEGe6nQY1ShjTK3rMUUKhemPR5ruhxSvC\n"
"Nr4TDea9Y355e6cJDUCrat2PisP29owaQgVR1EX1n6diIWgVIEM8med8vSTYqZEX\n"
"c4g/VhsxOBi0cQ+azcgOno4uG+GMmIPLHzHxREzGBHNJdmAPx/i9F4BrLunMTA5a\n"
"mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=\n"
"-----END CERTIFICATE-----";

int main(void)
{
char *buffer = new char[256];
nsapi_size_or_error_t result;
nsapi_size_t size;
const char query[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";

mbed_trace_init();

printf("TLSSocket Example.\n");
printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);

NetworkInterface *net = NetworkInterface::get_default_instance();

if (!net) {
printf("Error! No network inteface found.\n");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface

return 0;
}

printf("Connecting to network\n");
result = net->connect();
if (result != 0) {
printf("Error! net->connect() returned: %d\n", result);
return result;
}

TLSSocket *socket = new TLSSocket;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you've configured a big main stack, no particular reason to new this? Or the buffer?

result = socket->set_root_ca_cert(cert);
if (result != 0) {
printf("Error: socket->set_root_ca_cert() returned %d\n", result);
return result;
}

result = socket->open(net);
if (result != 0) {
printf("Error! socket->open() returned: %d\n", result);
return result;
}

printf("Connecting to api.ipify.org\n");
result = socket->connect("api.ipify.org", 443);
if (result != 0) {
printf("Error! socket->connect() returned: %d\n", result);
goto DISCONNECT;
}

// Send a simple http request
size = strlen(query);
result = socket->send(query+result, size);
if (result != size) {
printf("Error! socket->send() returned: %d\n", result);
goto DISCONNECT;
}

// Receieve an HTTP response and print out the response line
while ((result = socket->recv(buffer, 255)) > 0) {
buffer[result] = 0;
printf("%s", buffer);
}
printf("\n");

if (result < 0) {
printf("Error! socket->recv() returned: %d\n", result);
goto DISCONNECT;
}


DISCONNECT:
delete[] buffer;
// Close the socket to return its memory and bring down the network interface
socket->close();

// Bring down the ethernet interface
net->disconnect();
printf("Done\n");
}
1 change: 1 addition & 0 deletions TLSSocket/mbed-os.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/ARMmbed/mbed-os/#bf6f2c3c6434a6de9eb9511feffa5948b3d1f20f
10 changes: 10 additions & 0 deletions TLSSocket/mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"macros": ["MBEDTLS_SHA1_C"],
"target_overrides": {
"*": {
"platform.stdio-convert-newlines": true,
"mbed-trace.enable": 1,
"rtos.main-thread-stack-size": 8192
}
}
}