Example TLS client with wolfSSL, with cert
Dependencies: EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wolfSSL
Diff: client-tls.cpp
- Revision:
- 5:26b87ccd43d1
- Parent:
- 4:ebcf8e2d846a
- Child:
- 6:e636986882e8
--- a/client-tls.cpp Tue Jul 21 22:49:46 2015 +0000 +++ b/client-tls.cpp Tue Jul 21 22:58:30 2015 +0000 @@ -21,6 +21,7 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "NTPClient.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -30,6 +31,15 @@ #define MAXDATASIZE (1024*4) +#if defined(NO_FILESYSTEM) + #define USE_CERT_BUFFERS_2048 + #include <wolfssl/certs_test.h> +#else + #include "SDFileSystem.h" + SDFileSystem sdCard(PTE3, PTE1, PTE2, PTE4, "sd"); + const char* certFile = "/sd/ca-cert.pem"; +#endif + static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock) { return ((TCPSocketConnection *)sock)->receive(buf, sz) ; @@ -135,6 +145,15 @@ wolfSSL_SetIORecv(ctx, SocketReceive) ; wolfSSL_SetIOSend(ctx, SocketSend) ; +#ifndef NO_FILESYSTEM + if (wolfSSL_CTX_load_verify_locations(ctx, certFile,0) != SSL_SUCCESS) + printf("can't load ca file\n"); +#else + if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048, + sizeof_ca_cert_der_2048, SSL_FILETYPE_ASN1) != SSL_SUCCESS) + printf("can't load ca data"); +#endif + if ((ssl = wolfSSL_new(ctx)) == NULL) { printf("wolfSSL_new error.\n"); return EXIT_FAILURE; @@ -176,6 +195,12 @@ eth.connect(); printf("Client Addr: %s\n", eth.getIPAddress()); + NTPClient ntp; + if(ntp.setTime("ntp.jst.mfeed.ad.jp") != 0){ + printf("NTP Error\n") ; + return ; + } + getline("Server Addr: ", server_addr, sizeof(server_addr)) ; getline("Server Port: ", server_port, sizeof(server_port)) ;