@@ -53,11 +53,26 @@ extern time_t time(time_t*);
5353
5454/* wolfSSL */
5555#include <wolfssl/ssl.h>
56- #define USE_CERT_BUFFERS_2048
57- #include <wolfssl/certs_test.h>
5856
59- #define DEFAULT_IPADDR "93.184.216.34" /* www.example.com */
57+ #if MYNEWT_VAL (WOLFSSL_MN_USE_CUSTOM_CA )
58+ #include "custom_ca.h"
59+
60+ /* TODO: set to your real target's current IP/port/hostname; see custom_ca.h */
61+ #define DEFAULT_IPADDR_PLACEHOLDER
62+ #ifdef DEFAULT_IPADDR_PLACEHOLDER
63+ #error "mynewt/client-tls-mn.c: set DEFAULT_IPADDR/DEFAULT_PORT/DEFAULT_HOSTNAME (in the " \
64+ "WOLFSSL_MN_USE_CUSTOM_CA branch above) to your real target's " \
65+ "current IP/port/hostname and remove this #error before building."
66+ #endif
67+ #define DEFAULT_IPADDR "0.0.0.0"
6068#define DEFAULT_PORT 443
69+ #define DEFAULT_HOSTNAME "example.com"
70+ #else
71+ /* Generated by setup.sh from certs/ca-cert.pem. */
72+ #include "local_ca.h"
73+ #define DEFAULT_IPADDR "127.0.0.1" /* local tls/server-tls example */
74+ #define DEFAULT_PORT 11111
75+ #endif
6176
6277struct os_sem test_sem ;
6378
@@ -120,12 +135,12 @@ static const oc_handler_t omgr_oc_handler = {
120135
121136static void net_test_readable (void * arg , int err )
122137{
123- console_printf ("net_test_readable %x - %d\n" , (int )arg , err );
138+ console_printf ("net_test_readable %lx - %d\n" , (unsigned long )( uintptr_t )arg , err );
124139}
125140
126141static void net_test_writable (void * arg , int err )
127142{
128- console_printf ("net_test_writable %x - %d\n" , (int )arg , err );
143+ console_printf ("net_test_writable %lx - %d\n" , (unsigned long )( uintptr_t )arg , err );
129144}
130145
131146static const union mn_socket_cb net_test_cbs = {
@@ -135,7 +150,7 @@ static const union mn_socket_cb net_test_cbs = {
135150
136151static int net_test_newconn (void * arg , struct mn_socket * new )
137152{
138- console_printf ("net_test_newconn %x - %x \n" , (int ) arg , (int )new );
153+ console_printf ("net_test_newconn %lx - %lx \n" , (unsigned long )( uintptr_t ) arg , (unsigned long )( uintptr_t )new );
139154 mn_socket_set_cbs (new , NULL , & net_test_cbs );
140155 net_test_socket2 = new ;
141156 return 0 ;
@@ -161,12 +176,12 @@ net_cli(int argc, char **argv)
161176 }
162177 if (!strcmp (argv [1 ], "udp" )) {
163178 rc = mn_socket (& net_test_socket , MN_PF_INET , MN_SOCK_DGRAM , 0 );
164- console_printf ("mn_socket(UDP) = %d %x \n" , rc ,
165- (int )net_test_socket );
179+ console_printf ("mn_socket(UDP) = %d %lx \n" , rc ,
180+ (unsigned long )( uintptr_t )net_test_socket );
166181 } else if (!strcmp (argv [1 ], "tcp" )) {
167182 rc = mn_socket (& net_test_socket , MN_PF_INET , MN_SOCK_STREAM , 0 );
168- console_printf ("mn_socket(TCP) = %d %x \n" , rc ,
169- (int )net_test_socket );
183+ console_printf ("mn_socket(TCP) = %d %lx \n" , rc ,
184+ (unsigned long )( uintptr_t )net_test_socket );
170185 } else if (!strcmp (argv [1 ], "connect" ) || !strcmp (argv [1 ], "bind" )) {
171186 char * addrStr = DEFAULT_IPADDR ;
172187 int port = DEFAULT_PORT ;
@@ -389,16 +404,38 @@ static int wolfssl_ctx_init() {
389404 return -1 ;
390405 }
391406
392- int ret = wolfSSL_CTX_load_verify_buffer (wolfsslCtx , ca_cert_der_2048 , sizeof_ca_cert_der_2048 , SSL_FILETYPE_ASN1 );
407+ #if MYNEWT_VAL (WOLFSSL_MN_USE_CUSTOM_CA )
408+ int ret = wolfSSL_CTX_load_verify_buffer (wolfsslCtx , custom_ca_der , sizeof_custom_ca_der , SSL_FILETYPE_ASN1 );
409+ #else
410+ int ret = wolfSSL_CTX_load_verify_buffer (wolfsslCtx , local_ca_der , sizeof_local_ca_der , SSL_FILETYPE_ASN1 );
411+ #endif
393412 if (ret != SSL_SUCCESS ) {
394413 console_printf ("Error %d loading CA cert\n" , ret );
395- return ret ;
414+ wolfSSL_CTX_free (wolfsslCtx );
415+ wolfsslCtx = NULL ;
416+ return -1 ;
396417 }
397418
398- wolfSSL_CTX_set_verify (wolfsslCtx , SSL_VERIFY_NONE , 0 );
419+ /* Depends on bundled test CA certs not expiring. */
420+ wolfSSL_CTX_set_verify (wolfsslCtx , SSL_VERIFY_PEER , NULL );
399421 /* Create a WOLFSSL object */
400422 if ((ssl = wolfSSL_new (wolfsslCtx )) == NULL ) {
401423 console_printf ("ERROR: failed to create WOLFSSL object\n" );
424+ wolfSSL_CTX_free (wolfsslCtx );
425+ wolfsslCtx = NULL ;
426+ return -1 ;
427+ }
428+
429+ #if MYNEWT_VAL (WOLFSSL_MN_USE_CUSTOM_CA )
430+ if (wolfSSL_check_domain_name (ssl , DEFAULT_HOSTNAME ) != SSL_SUCCESS ) {
431+ #else
432+ if (wolfSSL_check_domain_name (ssl , DEFAULT_IPADDR ) != SSL_SUCCESS ) {
433+ #endif
434+ console_printf ("ERROR: failed to set expected hostname\n" );
435+ wolfSSL_free (ssl );
436+ ssl = NULL ;
437+ wolfSSL_CTX_free (wolfsslCtx );
438+ wolfsslCtx = NULL ;
402439 return -1 ;
403440 }
404441
@@ -428,13 +465,19 @@ wolfssl_cli(int argc, char **argv)
428465 const char * subCommand = argv [1 ];
429466
430467 if (!strcmp (subCommand , "init" )) {
431- wolfssl_ctx_init ();
468+ if (wolfssl_ctx_init () != 0 ) {
469+ return -1 ;
470+ }
432471 wolfSSL_SetIO_Mynewt (ssl , net_test_socket , & net_test_sin );
433472 console_printf ("wolfSSL ctx initialize\n" );
434473 } else if (!strcmp (subCommand , "clear" )) {
435474 wolfssl_ctx_clear ();
436475 console_printf ("wolfSSL ctx clear\n" );
437476 } else if (!strcmp (subCommand , "connect" )) {
477+ if (ssl == NULL ) {
478+ console_printf ("ERROR: run \"wolfssl init\" first\n" );
479+ return -1 ;
480+ }
438481 rc = wolfSSL_connect (ssl );
439482 if (rc != SSL_SUCCESS ) {
440483 err = wolfSSL_get_error (ssl , 0 );
@@ -445,7 +488,11 @@ wolfssl_cli(int argc, char **argv)
445488 } else if (!strcmp (subCommand , "write" )) {
446489 char * str = "GET /index.html HTTP/1.0\r\n\r\n" ;
447490 int len ;
448- if (argc > 3 ) {
491+ if (ssl == NULL ) {
492+ console_printf ("ERROR: run \"wolfssl init\" first\n" );
493+ return -1 ;
494+ }
495+ if (argc > 2 ) {
449496 str = argv [2 ];
450497 }
451498 len = strlen (str );
@@ -459,14 +506,20 @@ wolfssl_cli(int argc, char **argv)
459506 console_printf ("wolfSSL_write() = %dL\n" , rc );
460507 } else if (!strcmp (subCommand , "read" )) {
461508 char buff [256 ];
462- rc = 0 ;
463- while (rc >= 0 ) {
509+ if (ssl == NULL ) {
510+ console_printf ("ERROR: run \"wolfssl init\" first\n" );
511+ return -1 ;
512+ }
513+ rc = 1 ;
514+ while (rc > 0 ) {
464515 memset (buff , 0 , sizeof (buff ));
465516 rc = wolfSSL_read (ssl , buff , sizeof (buff )- 1 );
466517 if (rc < 0 ) {
467518 err = wolfSSL_get_error (ssl , 0 );
468519 console_printf ("ERROR: wolfSSL_read rc:%d err:%d\n" , rc , err );
469520 return 0 ;
521+ } else if (rc == 0 ) {
522+ break ;
470523 }
471524 console_printf ("%.*s\n" , rc , buff );
472525 }
@@ -558,16 +611,18 @@ omgr_app_init(void)
558611#endif
559612
560613/**
561- * main
614+ * mynewt_main
562615 *
563616 * The main task for the project. This function initializes the packages, calls
564617 * init_tasks to initialize additional tasks (and possibly other objects),
565618 * then starts serving events from default event queue.
566619 *
620+ * Named mynewt_main because the BSP provides main().
621+ *
567622 * @return int NOTE: this function should never return!
568623 */
569624int
570- main (int argc , char * * argv )
625+ mynewt_main (int argc , char * * argv )
571626{
572627#ifdef ARCH_sim
573628 mcu_sim_parse_args (argc , argv );
0 commit comments