158#include "mbedtls/build_info.h"
159#include "mbedtls/platform.h"
160#include "mbedtls/net.h"
161#include "mbedtls/ssl.h"
162#include "mbedtls/entropy.h"
163#include "mbedtls/ctr_drbg.h"
164#include "mbedtls/debug.h"
166#include "default_certs.h"
169#define DEFAULT_PORT "443"
171#define DEFAULT_SERVER "www.google.com"
173#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
187void my_debug(
void *ctx,
int level,
const char *file,
int line,
const char *str)
192 printf(
"%s:%04d: %s", file, line, str);
201 uint8_t *allocptr = NULL;
203 printf(
"\n\nMorse SSL Client Demo (Built " __DATE__
" " __TIME__
")\n\n");
212 mbedtls_net_context server_fd;
213 const char *pers =
"sslclient";
215 mbedtls_entropy_context entropy;
216 mbedtls_ctr_drbg_context ctr_drbg;
217 mbedtls_ssl_context ssl;
218 mbedtls_ssl_config conf;
219 mbedtls_x509_crt cacert;
220 mbedtls_x509_crt clicert;
221 mbedtls_pk_context pkey;
226 printf(
"Initialising MbedTLS...");
227 mbedtls_net_init(&server_fd);
228 mbedtls_ssl_init(&ssl);
229 mbedtls_ssl_config_init(&conf);
235 mbedtls_x509_crt_init(&cacert);
236 mbedtls_x509_crt_init(&clicert);
237 mbedtls_pk_init(&pkey);
238 mbedtls_ctr_drbg_init(&ctr_drbg);
239 mbedtls_entropy_init(&entropy);
241 ret = mbedtls_ctr_drbg_seed(&ctr_drbg,
242 mbedtls_entropy_func,
244 (
const unsigned char *)pers,
248 printf(
" failed %d in mbedtls_ctr_drbg_seed()\n\n", ret);
256 allocptr = (uint8_t *)DEFAULT_ROOT_CERT;
271 printf(
"Failed to allocate memory for root certificate!\n\n");
277 len =
sizeof(DEFAULT_ROOT_CERT);
279 printf(
"Loading the CA root certificate ...");
280 ret = mbedtls_x509_crt_parse(&cacert, allocptr, len);
283 printf(
" failed %d\n\n", ret);
288 allocptr = (uint8_t *)DEFAULT_CLIENT_CERT;
303 printf(
"Failed to allocate memory for client certificate!\n\n");
309 len =
sizeof(DEFAULT_CLIENT_CERT);
311 printf(
"Loading the client cert...");
312 ret = mbedtls_x509_crt_parse(&clicert, allocptr, len);
315 printf(
" failed %d\n\n", ret);
320 allocptr = (uint8_t *)DEFAULT_CLIENT_KEY;
335 printf(
"Failed to allocate memory for client key!\n\n");
341 len =
sizeof(DEFAULT_CLIENT_KEY);
343 printf(
"Loading the client key...");
344 ret = mbedtls_pk_parse_key(&pkey, allocptr, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
347 printf(
" failed %d\n\n", ret);
352 printf(
"Setting up client certs/key...");
353 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0)
355 printf(
" failed %d\n\n", ret);
363 printf(
"Setting up SSL...");
364 ret = mbedtls_ssl_config_defaults(&conf,
365 MBEDTLS_SSL_IS_CLIENT,
366 MBEDTLS_SSL_TRANSPORT_STREAM,
367 MBEDTLS_SSL_PRESET_DEFAULT);
370 printf(
" failed %d in mbedtls_ssl_config_defaults()\n\n", ret);
373 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
374 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
375 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
376 ret = mbedtls_ssl_setup(&ssl, &conf);
379 printf(
" failed %d in mbedtls_ssl_setup()\n\n", ret);
383 static char sslclient_server[64];
384 strncpy(sslclient_server,
DEFAULT_SERVER,
sizeof(sslclient_server));
386 if ((ret = mbedtls_ssl_set_hostname(&ssl, sslclient_server)) != 0)
388 printf(
" failed %d\n\n", ret);
397 static char sslclient_port[8];
398 strncpy(sslclient_port,
DEFAULT_PORT,
sizeof(sslclient_port));
401 printf(
"Connecting to %s:%s...", sslclient_server, sslclient_port);
403 ret = mbedtls_net_connect(&server_fd, sslclient_server, sslclient_port, MBEDTLS_NET_PROTO_TCP);
406 printf(
" failed %d\n\n", ret);
411 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout);
416 printf(
"Performing the SSL/TLS handshake...");
417 ret = mbedtls_ssl_handshake(&ssl);
420 printf(
" failed %d\n\n", ret);
428 printf(
"Verifying peer X.509 certificate...");
429 ret = mbedtls_ssl_get_verify_result(&ssl);
433 printf(
" failed %d, did you set the time?\n\n", ret);
443 printf(
"Write to server:");
448 printf(
" failed %d\n\n", ret);
451 printf(
" %d bytes written\n\n%s", ret,
GET_REQUEST);
456 printf(
"Reading response from server:\n");
457 memset(
buf, 0,
sizeof(
buf));
458 ret = mbedtls_ssl_read(&ssl, (
unsigned char *)
buf,
sizeof(
buf) - 1);
463 printf(
"Printing headers only:\n\n");
466 char *end_headers = strstr(
buf,
"\n\n");
472 end_headers = strstr(
buf,
"\r\n\r\n");
485 ret = mbedtls_ssl_read(&ssl, (
unsigned char *)
buf,
sizeof(
buf));
487 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
492 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
499 printf(
" failed with error code %d\n\n", ret);
515 printf(
"\nSuccess! %u bytes read in total.\n", total);
519 printf(
"\nFailed to read response from server!\n");
525 mbedtls_ssl_close_notify(&ssl);
528 mbedtls_net_free(&server_fd);
530 mbedtls_ssl_free(&ssl);
531 mbedtls_ssl_config_free(&conf);
532 mbedtls_ctr_drbg_free(&ctr_drbg);
533 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.