159#include "mbedtls/build_info.h"
160#include "mbedtls/platform.h"
161#include "mbedtls/net.h"
162#include "mbedtls/ssl.h"
163#include "mbedtls/entropy.h"
164#include "mbedtls/ctr_drbg.h"
165#include "mbedtls/debug.h"
167#include "default_certs.h"
170#define DEFAULT_PORT "443"
172#define DEFAULT_SERVER "www.google.com"
174#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
188void my_debug(
void *ctx,
int level,
const char *file,
int line,
const char *str)
193 printf(
"%s:%04d: %s", file, line, str);
203 uint8_t *allocptr = NULL;
205 printf(
"\n\nMorse SSL Client Demo (Built " __DATE__
" " __TIME__
")\n\n");
214 mbedtls_net_context server_fd;
215 const char *pers =
"sslclient";
217 mbedtls_entropy_context entropy;
218 mbedtls_ctr_drbg_context ctr_drbg;
219 mbedtls_ssl_context ssl;
220 mbedtls_ssl_config conf;
221 mbedtls_x509_crt cacert;
222 mbedtls_x509_crt clicert;
223 mbedtls_pk_context pkey;
228 printf(
"Initialising MbedTLS...");
229 mbedtls_net_init(&server_fd);
230 mbedtls_ssl_init(&ssl);
231 mbedtls_ssl_config_init(&conf);
237 mbedtls_x509_crt_init(&cacert);
238 mbedtls_x509_crt_init(&clicert);
239 mbedtls_pk_init(&pkey);
240 mbedtls_ctr_drbg_init(&ctr_drbg);
241 mbedtls_entropy_init(&entropy);
243 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
244 (
const unsigned char *) pers, strlen(pers));
247 printf(
" failed %d in mbedtls_ctr_drbg_seed()\n\n", ret);
255 allocptr = (uint8_t*)DEFAULT_ROOT_CERT;
270 printf(
"Failed to allocate memory for root certificate!\n\n");
276 len =
sizeof(DEFAULT_ROOT_CERT);
278 printf(
"Loading the CA root certificate ...");
279 ret = mbedtls_x509_crt_parse(&cacert, allocptr, len);
282 printf(
" failed %d\n\n", ret);
287 allocptr = (uint8_t*)DEFAULT_CLIENT_CERT;
302 printf(
"Failed to allocate memory for client certificate!\n\n");
308 len =
sizeof(DEFAULT_CLIENT_CERT);
310 printf(
"Loading the client cert...");
311 ret = mbedtls_x509_crt_parse(&clicert, allocptr, len);
314 printf(
" failed %d\n\n", ret);
319 allocptr = (uint8_t*)DEFAULT_CLIENT_KEY;
334 printf(
"Failed to allocate memory for client key!\n\n");
340 len =
sizeof(DEFAULT_CLIENT_KEY);
342 printf(
"Loading the client key...");
343 ret = mbedtls_pk_parse_key(&pkey, allocptr, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
346 printf(
" failed %d\n\n", ret);
351 printf(
"Setting up client certs/key...");
352 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0)
354 printf(
" failed %d\n\n", ret);
362 printf(
"Setting up SSL...");
363 ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
364 MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
367 printf(
" failed %d in mbedtls_ssl_config_defaults()\n\n", ret);
370 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
371 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
372 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
373 ret = mbedtls_ssl_setup(&ssl, &conf);
376 printf(
" failed %d in mbedtls_ssl_setup()\n\n", ret);
380 static char sslclient_server[64];
381 strncpy(sslclient_server,
DEFAULT_SERVER,
sizeof(sslclient_server));
383 if ((ret = mbedtls_ssl_set_hostname(&ssl, sslclient_server)) != 0)
385 printf(
" failed %d\n\n", ret);
394 static char sslclient_port[8];
395 strncpy(sslclient_port,
DEFAULT_PORT,
sizeof(sslclient_port));
398 printf(
"Connecting to %s:%s...", sslclient_server, sslclient_port);
400 ret = mbedtls_net_connect(&server_fd, sslclient_server, sslclient_port, MBEDTLS_NET_PROTO_TCP);
403 printf(
" failed %d\n\n", ret);
408 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout);
413 printf(
"Performing the SSL/TLS handshake...");
414 ret = mbedtls_ssl_handshake(&ssl);
417 printf(
" failed %d\n\n", ret);
425 printf(
"Verifying peer X.509 certificate...");
426 ret = mbedtls_ssl_get_verify_result(&ssl);
430 printf(
" failed %d, did you set the time?\n\n", ret);
440 printf(
"Write to server:");
445 printf(
" failed %d\n\n", ret);
448 printf(
" %d bytes written\n\n%s", ret,
GET_REQUEST);
453 printf(
"Reading response from server:\n");
454 memset(
buf, 0,
sizeof(
buf));
455 ret = mbedtls_ssl_read(&ssl, (
unsigned char*)
buf,
sizeof(
buf) - 1);
460 printf(
"Printing headers only:\n\n");
463 char* end_headers = strstr(
buf,
"\n\n");
469 end_headers = strstr(
buf,
"\r\n\r\n");
482 ret = mbedtls_ssl_read(&ssl, (
unsigned char*)
buf,
sizeof(
buf));
484 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
487 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
492 printf(
" failed with error code %d\n\n", ret);
508 printf(
"\nSuccess! %u bytes read in total.\n", total);
512 printf(
"\nFailed to read response from server!\n");
518 mbedtls_ssl_close_notify(&ssl);
521 mbedtls_net_free(&server_fd);
523 mbedtls_ssl_free(&ssl);
524 mbedtls_ssl_config_free(&conf);
525 mbedtls_ctr_drbg_free(&ctr_drbg);
526 mbedtls_entropy_free(&entropy);
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
int mmconfig_read_bytes(const char *key, void *buffer, uint32_t buffsize, uint32_t offset)
Returns the persistent store data identified by the key.
#define mmosal_malloc(size)
Allocate memory of the given size and return a pointer to it (malloc).
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
#define DEFAULT_PORT
HTTPS port number to connect to.
void my_debug(void *ctx, int level, const char *file, int line, const char *str)
Optional mbedtls debug callback handler.
#define DEFAULT_SERVER
HTTPS server to connect to.
#define GET_REQUEST
HTTPS get request string.
char buf[1408]
Statically allocated buffer for HTTP GET request, just under 1 packet size.
void app_init(void)
Main entry point to the application.