Francois Berder / Mbed 2 deprecated TLS_axTLS-Example

Dependencies:   EthernetInterface5 TLS_axTLS mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     This example shows how to use TLS library. It
00003     establishes a connection to mbed.org and then
00004     closes this connection immediately.
00005 
00006     Don't forget to copy certificates on your
00007     mbed before running this program. You can
00008     download them as a zip package from the wiki:
00009     http://mbed.org/users/feb11/code/TLSExample/
00010 */
00011 
00012 
00013 
00014 #include "mbed.h"
00015 #include "EthernetInterface.h"
00016 #include "CertificateManager.h"
00017 #include "TLSConnection.h"
00018 
00019 const char host[] = "mbed.org";
00020 LocalFileSystem local("local");
00021 
00022 int main()
00023 {
00024     /*
00025         Ensure that the mbed has a time value
00026         approximately equal to the correct timestamp.
00027         This is needed because the certificate manager
00028         uses the timestamp to check whether certificates
00029         expired.
00030 
00031         Don't hesitate to remove this line if your mbed
00032         already store a correct time value, or update
00033         this value.
00034     */
00035     set_time(1378376823);
00036 
00037     /* Starting Ethernet */
00038     EthernetInterface eth;
00039     if(eth.init() || eth.connect()) {
00040         printf("Error with EthernetInterface\n\r");
00041         return -1;
00042     }
00043 
00044     /*
00045         Loading certificates in precomputed mode.
00046         Notice that cert4 is the root certificate.
00047     */
00048     CertificateManager::add("/local/cert1.der");
00049     CertificateManager::add("/local/cert2.der");
00050     CertificateManager::add("/local/cert3.der");
00051     CertificateManager::add("/local/cert4.der");
00052     if(!CertificateManager::load(true)) {
00053         printf("Failed to load certificates\n");
00054         return -1;
00055     }
00056 
00057     /* Starting connection to mbed.org */
00058     TLSConnection con;
00059     if(con.connect(host)) {
00060         printf("Connected to %s !\n", host);
00061 
00062         /*
00063             Since no memory is needed once the connection
00064             is established, we don't need to call
00065             CertificateManager::clear() to free memory.
00066         */
00067         con.close();
00068     } else {
00069         printf("Failed to connect to %s\n", host);
00070     }
00071 
00072     eth.disconnect();
00073 
00074     return 0;
00075 }
00076