Establishing a connection to mbed.org using TLS

Dependencies:   EthernetInterface5 TLS_axTLS mbed-rtos mbed

Committer:
feb11
Date:
Thu Sep 12 15:20:12 2013 +0000
Revision:
0:e98faa82e3ee
initial import

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:e98faa82e3ee 1 /*
feb11 0:e98faa82e3ee 2 This example shows how to use TLS library. It
feb11 0:e98faa82e3ee 3 establishes a connection to mbed.org and then
feb11 0:e98faa82e3ee 4 closes this connection immediately.
feb11 0:e98faa82e3ee 5
feb11 0:e98faa82e3ee 6 Don't forget to copy certificates on your
feb11 0:e98faa82e3ee 7 mbed before running this program. You can
feb11 0:e98faa82e3ee 8 download them as a zip package from the wiki:
feb11 0:e98faa82e3ee 9 http://mbed.org/users/feb11/code/TLSExample/
feb11 0:e98faa82e3ee 10 */
feb11 0:e98faa82e3ee 11
feb11 0:e98faa82e3ee 12
feb11 0:e98faa82e3ee 13
feb11 0:e98faa82e3ee 14 #include "mbed.h"
feb11 0:e98faa82e3ee 15 #include "EthernetInterface.h"
feb11 0:e98faa82e3ee 16 #include "CertificateManager.h"
feb11 0:e98faa82e3ee 17 #include "TLSConnection.h"
feb11 0:e98faa82e3ee 18
feb11 0:e98faa82e3ee 19 const char host[] = "mbed.org";
feb11 0:e98faa82e3ee 20 LocalFileSystem local("local");
feb11 0:e98faa82e3ee 21
feb11 0:e98faa82e3ee 22 int main()
feb11 0:e98faa82e3ee 23 {
feb11 0:e98faa82e3ee 24 /*
feb11 0:e98faa82e3ee 25 Ensure that the mbed has a time value
feb11 0:e98faa82e3ee 26 approximately equal to the correct timestamp.
feb11 0:e98faa82e3ee 27 This is needed because the certificate manager
feb11 0:e98faa82e3ee 28 uses the timestamp to check whether certificates
feb11 0:e98faa82e3ee 29 expired.
feb11 0:e98faa82e3ee 30
feb11 0:e98faa82e3ee 31 Don't hesitate to remove this line if your mbed
feb11 0:e98faa82e3ee 32 already store a correct time value, or update
feb11 0:e98faa82e3ee 33 this value.
feb11 0:e98faa82e3ee 34 */
feb11 0:e98faa82e3ee 35 set_time(1378376823);
feb11 0:e98faa82e3ee 36
feb11 0:e98faa82e3ee 37 /* Starting Ethernet */
feb11 0:e98faa82e3ee 38 EthernetInterface eth;
feb11 0:e98faa82e3ee 39 if(eth.init() || eth.connect()) {
feb11 0:e98faa82e3ee 40 printf("Error with EthernetInterface\n\r");
feb11 0:e98faa82e3ee 41 return -1;
feb11 0:e98faa82e3ee 42 }
feb11 0:e98faa82e3ee 43
feb11 0:e98faa82e3ee 44 /*
feb11 0:e98faa82e3ee 45 Loading certificates in precomputed mode.
feb11 0:e98faa82e3ee 46 Notice that cert4 is the root certificate.
feb11 0:e98faa82e3ee 47 */
feb11 0:e98faa82e3ee 48 CertificateManager::add("/local/cert1.der");
feb11 0:e98faa82e3ee 49 CertificateManager::add("/local/cert2.der");
feb11 0:e98faa82e3ee 50 CertificateManager::add("/local/cert3.der");
feb11 0:e98faa82e3ee 51 CertificateManager::add("/local/cert4.der");
feb11 0:e98faa82e3ee 52 if(!CertificateManager::load(true)) {
feb11 0:e98faa82e3ee 53 printf("Failed to load certificates\n");
feb11 0:e98faa82e3ee 54 return -1;
feb11 0:e98faa82e3ee 55 }
feb11 0:e98faa82e3ee 56
feb11 0:e98faa82e3ee 57 /* Starting connection to mbed.org */
feb11 0:e98faa82e3ee 58 TLSConnection con;
feb11 0:e98faa82e3ee 59 if(con.connect(host)) {
feb11 0:e98faa82e3ee 60 printf("Connected to %s !\n", host);
feb11 0:e98faa82e3ee 61
feb11 0:e98faa82e3ee 62 /*
feb11 0:e98faa82e3ee 63 Since no memory is needed once the connection
feb11 0:e98faa82e3ee 64 is established, we don't need to call
feb11 0:e98faa82e3ee 65 CertificateManager::clear() to free memory.
feb11 0:e98faa82e3ee 66 */
feb11 0:e98faa82e3ee 67 con.close();
feb11 0:e98faa82e3ee 68 } else {
feb11 0:e98faa82e3ee 69 printf("Failed to connect to %s\n", host);
feb11 0:e98faa82e3ee 70 }
feb11 0:e98faa82e3ee 71
feb11 0:e98faa82e3ee 72 eth.disconnect();
feb11 0:e98faa82e3ee 73
feb11 0:e98faa82e3ee 74 return 0;
feb11 0:e98faa82e3ee 75 }
feb11 0:e98faa82e3ee 76