This example shows how to use the TLS_cyassl library. It connects to twitter.com and downloads a webpage.
Dependencies: EthernetInterface7 HTTPSClient_cyassl TLS_cyassl mbed-rtos mbed
Fork of HTTPSClientExample2 by
Once you imported this program, you need to load the right certificate. To do so, open the file cert.h in the TLS_cyassl library then, replace the content by this :
root certificate of twitter
static const uint8_t root_cert[] =
{
0x30, 0x82, 0x04, 0xD3, 0x30, 0x82, 0x03, 0xBB, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x18,
0xDA, 0xD1, 0x9E, 0x26, 0x7D, 0xE8, 0xBB, 0x4A, 0x21, 0x58, 0xCD, 0xCC, 0x6B, 0x3B, 0x4A, 0x30,
0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x81,
0xCA, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17,
0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0E, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
0x6E, 0x2C, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x04, 0x0B,
0x13, 0x16, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6E, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74,
0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x31, 0x3A, 0x30, 0x38, 0x06, 0x03, 0x55, 0x04,
0x0B, 0x13, 0x31, 0x28, 0x63, 0x29, 0x20, 0x32, 0x30, 0x30, 0x36, 0x20, 0x56, 0x65, 0x72, 0x69,
0x53, 0x69, 0x67, 0x6E, 0x2C, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x20, 0x2D, 0x20, 0x46, 0x6F, 0x72,
0x20, 0x61, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69, 0x7A, 0x65, 0x64, 0x20, 0x75, 0x73, 0x65, 0x20,
0x6F, 0x6E, 0x6C, 0x79, 0x31, 0x45, 0x30, 0x43, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x3C, 0x56,
0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6E, 0x20, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x20, 0x33, 0x20,
0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x20, 0x50, 0x72, 0x69, 0x6D, 0x61, 0x72, 0x79, 0x20, 0x43,
0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x41, 0x75, 0x74,
0x68, 0x6F, 0x72, 0x69, 0x74, 0x79, 0x20, 0x2D, 0x20, 0x47, 0x35, 0x30, 0x1E, 0x17, 0x0D, 0x30,
0x36, 0x31, 0x31, 0x30, 0x38, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x17, 0x0D, 0x33, 0x36,
0x30, 0x37, 0x31, 0x36, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5A, 0x30, 0x81, 0xCA, 0x31, 0x0B,
0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30, 0x15, 0x06,
0x03, 0x55, 0x04, 0x0A, 0x13, 0x0E, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6E, 0x2C, 0x20,
0x49, 0x6E, 0x63, 0x2E, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x16, 0x56,
0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6E, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x4E, 0x65,
0x74, 0x77, 0x6F, 0x72, 0x6B, 0x31, 0x3A, 0x30, 0x38, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x31,
0x28, 0x63, 0x29, 0x20, 0x32, 0x30, 0x30, 0x36, 0x20, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
0x6E, 0x2C, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x20, 0x2D, 0x20, 0x46, 0x6F, 0x72, 0x20, 0x61, 0x75,
0x74, 0x68, 0x6F, 0x72, 0x69, 0x7A, 0x65, 0x64, 0x20, 0x75, 0x73, 0x65, 0x20, 0x6F, 0x6E, 0x6C,
0x79, 0x31, 0x45, 0x30, 0x43, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x3C, 0x56, 0x65, 0x72, 0x69,
0x53, 0x69, 0x67, 0x6E, 0x20, 0x43, 0x6C, 0x61, 0x73, 0x73, 0x20, 0x33, 0x20, 0x50, 0x75, 0x62,
0x6C, 0x69, 0x63, 0x20, 0x50, 0x72, 0x69, 0x6D, 0x61, 0x72, 0x79, 0x20, 0x43, 0x65, 0x72, 0x74,
0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72,
0x69, 0x74, 0x79, 0x20, 0x2D, 0x20, 0x47, 0x35, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09,
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,
0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xAF, 0x24, 0x08, 0x08, 0x29, 0x7A, 0x35,
0x9E, 0x60, 0x0C, 0xAA, 0xE7, 0x4B, 0x3B, 0x4E, 0xDC, 0x7C, 0xBC, 0x3C, 0x45, 0x1C, 0xBB, 0x2B,
0xE0, 0xFE, 0x29, 0x02, 0xF9, 0x57, 0x08, 0xA3, 0x64, 0x85, 0x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 0xFE, 0x47, 0xBD, 0x2B, 0x7E, 0x4F, 0xC5, 0x95, 0xAE, 0x0E, 0x9D, 0xD4, 0xD1, 0x43, 0xC0,
0x67, 0x73, 0xE3, 0x14, 0x08, 0x7E, 0xE5, 0x3F, 0x9F, 0x73, 0xB8, 0x33, 0x0A, 0xCF, 0x5D, 0x3F,
0x34, 0x87, 0x96, 0x8A, 0xEE, 0x53, 0xE8, 0x25, 0x15, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x81,
0xB2, 0x30, 0x81, 0xAF, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05,
0x30, 0x03, 0x01, 0x01, 0xFF, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01, 0xFF, 0x04,
0x04, 0x03, 0x02, 0x01, 0x06, 0x30, 0x6D, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01,
0x0C, 0x04, 0x61, 0x30, 0x5F, 0xA1, 0x5D, 0xA0, 0x5B, 0x30, 0x59, 0x30, 0x57, 0x30, 0x55, 0x16,
0x09, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x67, 0x69, 0x66, 0x30, 0x21, 0x30, 0x1F, 0x30, 0x07,
0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x04, 0x14, 0x8F, 0xE5, 0xD3, 0x1A, 0x86, 0xAC, 0x8D,
0x8E, 0x6B, 0xC3, 0xCF, 0x80, 0x6A, 0xD4, 0x48, 0x18, 0x2C, 0x7B, 0x19, 0x2E, 0x30, 0x25, 0x16,
0x23, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6C, 0x6F, 0x67, 0x6F, 0x2E, 0x76, 0x65, 0x72,
0x69, 0x73, 0x69, 0x67, 0x6E, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x76, 0x73, 0x6C, 0x6F,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 0x02, 0xAA, 0x25, 0x17, 0x39, 0xB5, 0xDB, 0x7F,
0xE7, 0x84, 0x65, 0x2A, 0x95, 0x8A, 0xBD, 0x86, 0xDE, 0x5E, 0x81, 0x16, 0x83, 0x2D, 0x10, 0xCC,
0xDE, 0xFD, 0xA8, 0x82, 0x2A, 0x6D, 0x28, 0x1F, 0x0D, 0x0B, 0xC4, 0xE5, 0xE7, 0x1A, 0x26, 0x19,
0xE1, 0xF4, 0x11, 0x6F, 0x10, 0xB5, 0x95, 0xFC, 0xE7, 0x42, 0x05, 0x32, 0xDB, 0xCE, 0x9D, 0x51,
0x5E, 0x28, 0xB6, 0x9E, 0x85, 0xD3, 0x5B, 0xEF, 0xA5, 0x7D, 0x45, 0x40, 0x72, 0x8E, 0xB7, 0x0E,
0x6B, 0x0E, 0x06, 0xFB, 0x33, 0x35, 0x48, 0x71, 0xB8, 0x9D, 0x27, 0x8B, 0xC4, 0x65, 0x5F, 0x0D,
0x86, 0x76, 0x9C, 0x44, 0x7A, 0xF6, 0x95, 0x5C, 0xF6, 0x5D, 0x32, 0x08, 0x33, 0xA4, 0x54, 0xB6,
0x18, 0x3F, 0x68, 0x5C, 0xF2, 0x42, 0x4A, 0x85, 0x38, 0x54, 0x83, 0x5F, 0xD1, 0xE8, 0x2C, 0xF2,
0xAC, 0x11, 0xD6, 0xA8, 0xED, 0x63, 0x6A
};
static const uint32_t root_cert_len = sizeof(root_cert);
Revision 0:0687eb79c581, committed 2013-09-16
- Comitter:
- feb11
- Date:
- Mon Sep 16 09:01:32 2013 +0000
- Child:
- 1:db560c045c62
- Commit message:
- initial import
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon Sep 16 09:01:32 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/feb11/code/EthernetInterface7/#ff6ee7b9fd0c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPHeader.cpp Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,31 @@
+#include "HTTPHeader.h"
+
+HTTPHeader::HTTPHeader(HTTPStatus status):
+ _status(status),
+ _bodyLength(0)
+{
+}
+
+std::string HTTPHeader::getRequest(const std::string &path, const std::string &host, const int port)
+{
+
+ std::string request = "GET ";
+ request += path;
+ request += " HTTP/1.0\r\nHost: ";
+ request += host;
+ request += ":";
+ request += port;
+ request += "\r\n\r\n";
+ return request;
+}
+
+HTTPStatus HTTPHeader::getStatus() const
+{
+ return _status;
+}
+
+int HTTPHeader::getBodyLength() const
+{
+ return _bodyLength;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPHeader.h Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,28 @@
+#ifndef HTTP_HEADER_H
+#define HTTP_HEADER_H
+
+#include <string>
+#include "HTTPStatus.h"
+
+class HTTPSClient;
+
+class HTTPHeader
+{
+public :
+
+ friend class HTTPSClient;
+
+ HTTPHeader(HTTPStatus status = HTTP_INVALID);
+
+ static std::string getRequest(const std::string &path, const std::string &host, const int port);
+
+ HTTPStatus getStatus() const;
+ int getBodyLength() const;
+private :
+
+ HTTPStatus _status;
+ int _bodyLength;
+};
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPSClient.cpp Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,88 @@
+#include "HTTPSClient.h"
+#include "HTTPHeader.h"
+#include <stdio.h>
+#include <string.h>
+
+HTTPSClient::HTTPSClient():
+ _con()
+{
+}
+
+bool HTTPSClient::connect(const std::string& host)
+{
+ if(_con.is_connected())
+ return false;
+
+ return _con.connect(host.c_str());
+}
+
+std::string HTTPSClient::readLine()
+{
+ std::string line;
+ char c;
+ _con.receive(&c, 1);
+ while(c != '\r') {
+ line += c;
+ _con.receive(&c, 1);
+ }
+ _con.receive(&c, 1); // skip \n
+ return line;
+}
+
+HTTPHeader HTTPSClient::readHeader()
+{
+ HTTPHeader hdr;
+ std::string line = readLine();
+ sscanf(line.c_str(), "HTTP/1.%*d %d OK", &hdr._status);
+ do {
+ if(!line.compare(0,strlen("Content-Length"), "Content-Length"))
+ sscanf(line.c_str(), "Content-Length: %d", &hdr._bodyLength);
+ else if(!line.compare(0,strlen("content-length"), "content-length"))
+ sscanf(line.c_str(), "content-length: %d", &hdr._bodyLength);
+ line = readLine();
+ } while(line.size());
+ return hdr;
+}
+
+int HTTPSClient::get(const std::string& path, HTTPHeader *hdr)
+{
+ if(!_con.is_connected())
+ return -1;
+
+ const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443);
+
+ if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1)
+ return -1;
+
+ *hdr = readHeader();
+ return hdr->_status == HTTP_OK ? 0 : -1;
+}
+
+int HTTPSClient::get(const std::string& path, HTTPHeader *hdr, char *data, int length)
+{
+ if(!_con.is_connected())
+ return -1;
+
+ if(hdr != NULL) {
+ const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443);
+ if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1)
+ return -1;
+ *hdr = readHeader();
+ if(hdr->_status != HTTP_OK)
+ return -1;
+
+ if(hdr->_bodyLength > 0)
+ return _con.receive(data, hdr->_bodyLength > length ? length : hdr->_bodyLength);
+
+ return 0;
+ } else
+ return _con.receive(data, length);
+}
+
+bool HTTPSClient::disconnect()
+{
+ if(!_con.is_connected())
+ return true;
+
+ return _con.close() == 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPSClient.h Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,87 @@
+#ifndef HTTPS_CLIENT_H
+#define HTTPS_CLIENT_H
+
+#include <string>
+#include "TLSConnection.h"
+#include "HTTPHeader.h"
+
+
+/** Simple wrapper of TLS library to send GET request to
+ a server and receive some web pages.
+*/
+class HTTPSClient
+{
+public :
+
+ HTTPSClient();
+
+ /** Connnect to the given host.
+
+ \param host A valid hostname (e.g. "mbed.org")
+ \return True if the connection was established with
+ success, false otherwise.
+
+ \note If the client is already connected, it returns false.
+ */
+ bool connect(const std::string& host);
+
+ /** Send a GET request to the host.
+
+ \param path
+ \param hdr A pointer to an HTTPHeader object.
+ \return Returns 0 if the request was sent with success,
+ or -1 if an error occured.
+ */
+ int get(const std::string& path, HTTPHeader *hdr);
+
+ /** Send a get request and reads (partially) the body
+
+ \param path
+ \param hdr A pointer to an HTTPHeader object.
+ \param data A pointer to some buffer
+ \param length Maximum number of bytes to read
+ \return Number of bytes stored in data in range 0..length, or -1
+ if an error occured.
+
+ \note To check whether this request is sent with success, you must check
+ the status of the HTTPHeader and the value of the integer returned by this
+ function.
+
+ Example:
+ @code
+ // Assuming that an HTTPSClient object c is connected to a host
+ HTTPHeader hdr;
+ char buffer[256];
+ int n = c.get("/", &hdr, buffer, 256);
+ if(n > 0 && hdr.getStatus() == HTTP_OK) // GET request sent with success
+ {
+ // do some stuff here..
+ }
+ else
+ printf("Failed to send get request\n");
+ @endcode
+
+
+ \note If hdr is null, this function does not send anything and directly writes
+ bytes in data. This is particularly useful if you expect large answers (such as
+ webpages) from the host.
+ */
+ int get(const std::string& path, HTTPHeader *hdr, char *data, int length);
+
+ /** Disconnect from host
+
+ \return True if the client disconnected with success, false otherwise.
+ */
+ bool disconnect();
+
+private :
+
+ std::string readLine();
+ HTTPHeader readHeader();
+
+ TLSConnection _con;
+
+};
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPStatus.h Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,17 @@
+#ifndef HTTP_STATUS_H
+#define HTTP_STATUS_H
+
+
+enum HTTPStatus {
+ HTTP_OK = 200,
+ HTTP_BAD_REQUEST = 400,
+ HTTP_UNAUTHORIZED,
+ HTTP_FORBIDDEN = 403,
+ HTTP_NOT_FOUND,
+ HTTP_INTERNAL_ERROR = 500,
+ HTTP_INVALID = 0
+};
+
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TLS_cyassl.lib Mon Sep 16 09:01:32 2013 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/feb11/code/TLS_cyassl/#9494492e9bf7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Sep 16 09:01:32 2013 +0000
@@ -0,0 +1,86 @@
+/*
+ This examples shows how to use the HTTPS library
+ in order to download a webpage from twitter.com
+
+ Don't forget to download certificates and copy on
+ your mbed before running this program. You can
+ download them from this link:
+ http://mbed.org/media/uploads/feb11/certificates-twitter.zip
+*/
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "HTTPSClient.h"
+
+const char host[] = "twitter.com";
+const char request[] = "https://twitter.com";
+LocalFileSystem local("local");
+
+int main()
+{
+ set_time(1378370406);
+
+ /* Starting Ethernet */
+ EthernetInterface eth;
+ if(eth.init() || eth.connect())
+ {
+ printf("Error with EthernetInterface\n\r");
+ return -1;
+ }
+
+ /* Now, let's connect to twitter.com */
+ HTTPSClient client;
+ if(!client.connect(host))
+ {
+ printf("Failed to connect to %s\n", host);
+ return -1;
+ }
+ printf("Connected to %s !\n", host);
+
+ /* Let's send our GET request to get the webpage */
+ char buffer[256];
+ int bufferLength = sizeof(buffer);
+ HTTPHeader header;
+ int read = client.get(request, &header, buffer, bufferLength);
+ if(header.getStatus() != HTTP_OK || read < 0)
+ {
+ printf("Failed sending GET request : %s to %s", request, host);
+ return -1;
+ }
+
+ /* index.htm is used to store the webpage */
+ FILE *fp = fopen("/local/index.htm", "w");
+ if(fp == NULL)
+ {
+ printf("Failed to open file index.htm\n");
+ return -1;
+ }
+ fwrite(buffer, 1, read, fp); // writing the first part of the body
+
+ int totalRead = read;
+ while(totalRead < header.getBodyLength())
+ {
+ if(bufferLength > header.getBodyLength() - totalRead)
+ bufferLength = header.getBodyLength() - totalRead;
+
+ /* This function does not send a get request but instead
+ just receive data from the host.
+ */
+ read = client.get("", NULL, buffer, bufferLength);
+ if(read < 0)
+ {
+ printf("Error while getting data from %s\n", host);
+ return -1;
+ }
+ fwrite(buffer, 1, read, fp);
+ totalRead += read;
+ }
+
+ /* We're done, let's close everything */
+ fclose(fp);
+ printf("Disconnecting from %s\n", host);
+ client.disconnect();
+ eth.disconnect();
+
+ return 0;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon Sep 16 09:01:32 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ee87e782d34f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Sep 16 09:01:32 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb \ No newline at end of file
