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 Francois Berder

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, 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,
0xCB, 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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, 0x0B, 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);

Committer:
feb11
Date:
Mon Sep 16 09:59:26 2013 +0000
Revision:
1:db560c045c62
Parent:
0:0687eb79c581
update lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:0687eb79c581 1 #ifndef HTTPS_CLIENT_H
feb11 0:0687eb79c581 2 #define HTTPS_CLIENT_H
feb11 0:0687eb79c581 3
feb11 0:0687eb79c581 4 #include <string>
feb11 0:0687eb79c581 5 #include "TLSConnection.h"
feb11 0:0687eb79c581 6 #include "HTTPHeader.h"
feb11 0:0687eb79c581 7
feb11 0:0687eb79c581 8
feb11 0:0687eb79c581 9 /** Simple wrapper of TLS library to send GET request to
feb11 0:0687eb79c581 10 a server and receive some web pages.
feb11 0:0687eb79c581 11 */
feb11 0:0687eb79c581 12 class HTTPSClient
feb11 0:0687eb79c581 13 {
feb11 0:0687eb79c581 14 public :
feb11 0:0687eb79c581 15
feb11 0:0687eb79c581 16 HTTPSClient();
feb11 0:0687eb79c581 17
feb11 0:0687eb79c581 18 /** Connnect to the given host.
feb11 0:0687eb79c581 19
feb11 0:0687eb79c581 20 \param host A valid hostname (e.g. "mbed.org")
feb11 0:0687eb79c581 21 \return True if the connection was established with
feb11 0:0687eb79c581 22 success, false otherwise.
feb11 0:0687eb79c581 23
feb11 0:0687eb79c581 24 \note If the client is already connected, it returns false.
feb11 0:0687eb79c581 25 */
feb11 0:0687eb79c581 26 bool connect(const std::string& host);
feb11 0:0687eb79c581 27
feb11 0:0687eb79c581 28 /** Send a GET request to the host.
feb11 0:0687eb79c581 29
feb11 0:0687eb79c581 30 \param path
feb11 0:0687eb79c581 31 \param hdr A pointer to an HTTPHeader object.
feb11 0:0687eb79c581 32 \return Returns 0 if the request was sent with success,
feb11 0:0687eb79c581 33 or -1 if an error occured.
feb11 0:0687eb79c581 34 */
feb11 0:0687eb79c581 35 int get(const std::string& path, HTTPHeader *hdr);
feb11 0:0687eb79c581 36
feb11 0:0687eb79c581 37 /** Send a get request and reads (partially) the body
feb11 0:0687eb79c581 38
feb11 0:0687eb79c581 39 \param path
feb11 0:0687eb79c581 40 \param hdr A pointer to an HTTPHeader object.
feb11 0:0687eb79c581 41 \param data A pointer to some buffer
feb11 0:0687eb79c581 42 \param length Maximum number of bytes to read
feb11 0:0687eb79c581 43 \return Number of bytes stored in data in range 0..length, or -1
feb11 0:0687eb79c581 44 if an error occured.
feb11 0:0687eb79c581 45
feb11 0:0687eb79c581 46 \note To check whether this request is sent with success, you must check
feb11 0:0687eb79c581 47 the status of the HTTPHeader and the value of the integer returned by this
feb11 0:0687eb79c581 48 function.
feb11 0:0687eb79c581 49
feb11 0:0687eb79c581 50 Example:
feb11 0:0687eb79c581 51 @code
feb11 0:0687eb79c581 52 // Assuming that an HTTPSClient object c is connected to a host
feb11 0:0687eb79c581 53 HTTPHeader hdr;
feb11 0:0687eb79c581 54 char buffer[256];
feb11 0:0687eb79c581 55 int n = c.get("/", &hdr, buffer, 256);
feb11 0:0687eb79c581 56 if(n > 0 && hdr.getStatus() == HTTP_OK) // GET request sent with success
feb11 0:0687eb79c581 57 {
feb11 0:0687eb79c581 58 // do some stuff here..
feb11 0:0687eb79c581 59 }
feb11 0:0687eb79c581 60 else
feb11 0:0687eb79c581 61 printf("Failed to send get request\n");
feb11 0:0687eb79c581 62 @endcode
feb11 0:0687eb79c581 63
feb11 0:0687eb79c581 64
feb11 0:0687eb79c581 65 \note If hdr is null, this function does not send anything and directly writes
feb11 0:0687eb79c581 66 bytes in data. This is particularly useful if you expect large answers (such as
feb11 0:0687eb79c581 67 webpages) from the host.
feb11 0:0687eb79c581 68 */
feb11 0:0687eb79c581 69 int get(const std::string& path, HTTPHeader *hdr, char *data, int length);
feb11 0:0687eb79c581 70
feb11 0:0687eb79c581 71 /** Disconnect from host
feb11 0:0687eb79c581 72
feb11 0:0687eb79c581 73 \return True if the client disconnected with success, false otherwise.
feb11 0:0687eb79c581 74 */
feb11 0:0687eb79c581 75 bool disconnect();
feb11 0:0687eb79c581 76
feb11 0:0687eb79c581 77 private :
feb11 0:0687eb79c581 78
feb11 0:0687eb79c581 79 std::string readLine();
feb11 0:0687eb79c581 80 HTTPHeader readHeader();
feb11 0:0687eb79c581 81
feb11 0:0687eb79c581 82 TLSConnection _con;
feb11 0:0687eb79c581 83
feb11 0:0687eb79c581 84 };
feb11 0:0687eb79c581 85
feb11 0:0687eb79c581 86 #endif
feb11 0:0687eb79c581 87