1919 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020 */
2121
22+ /*
23+ This was original tested with Intel Galileo acting as the Client, with a
24+ laptop acting as a server using the server example provided in examples/server.
25+ Legacy Ardunio v1.86 was used to compile and program the Galileo
26+ */
2227
28+ #define USE_CERT_BUFFERS_2048
2329#include < wolfssl.h>
2430#include < wolfssl/ssl.h>
2531#include < Ethernet.h>
32+ #include < wolfssl/certs_test.h>
33+
2634
2735const char host[] = " 192.168.1.148" ; /* server to connect to */
2836const int port = 11111 ; /* port on server to connect to */
@@ -37,123 +45,132 @@ WOLFSSL_CTX* ctx = NULL;
3745WOLFSSL* ssl = NULL ;
3846
3947void setup () {
40- WOLFSSL_METHOD* method;
41-
42- Serial.begin (9600 );
43-
44- method = wolfTLSv1_2_client_method ();
45- if (method == NULL ) {
46- Serial.println (" unable to get method" );
48+ WOLFSSL_METHOD* method;
49+ /* Initialize Return Code */
50+ int rc;
51+ Serial.begin (9600 );
52+ /* Delay need to ensure connection to server */
53+ delay (4000 );
54+
55+ method = wolfTLSv1_2_client_method ();
56+ if (method == NULL ) {
57+ Serial.println (" unable to get method" );
4758 return ;
48- }
49- ctx = wolfSSL_CTX_new (method);
50- if (ctx == NULL ) {
51- Serial.println (" unable to get ctx" );
59+ }
60+ ctx = wolfSSL_CTX_new (method);
61+ if (ctx == NULL ) {
62+ Serial.println (" unable to get ctx" );
63+ return ;
64+ }
65+ /* initialize wolfSSL using callback functions */
66+ wolfSSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, 0 );
67+ rc = wolfSSL_CTX_load_verify_buffer (ctx, ca_cert_der_2048,\
68+ sizeof_ca_cert_der_2048,\
69+ WOLFSSL_FILETYPE_ASN1);
70+ Serial.print (" \n\n Return code of load_verify is:" );
71+ Serial.println (rc);
72+ Serial.println (" " );
73+ rc = wolfSSL_CTX_use_certificate_buffer (ctx, client_cert_der_2048,\
74+ sizeof_client_cert_der_2048,\
75+ WOLFSSL_FILETYPE_ASN1);
76+ Serial.print (" \n\n Return code of use_certificate_buffer is:" );
77+ Serial.println (rc);
78+ Serial.println (" " );
79+ rc = wolfSSL_CTX_use_PrivateKey_buffer (ctx, client_key_der_2048,\
80+ sizeof_client_key_der_2048,\
81+ WOLFSSL_FILETYPE_ASN1);
82+ Serial.print (" \n\n Return code of use_PrivateKey_buffer is:" );
83+ Serial.println (rc);
84+ Serial.println (" " );
85+ wolfSSL_SetIOSend (ctx, EthernetSend);
86+ wolfSSL_SetIORecv (ctx, EthernetReceive);
5287 return ;
53- }
54- /* initialize wolfSSL using callback functions */
55- wolfSSL_CTX_set_verify (ctx, SSL_VERIFY_NONE, 0 );
56- wolfSSL_SetIOSend (ctx, EthernetSend);
57- wolfSSL_SetIORecv (ctx, EthernetReceive);
58-
59- return ;
6088}
6189
6290int EthernetSend (WOLFSSL* ssl, char * msg, int sz, void * ctx) {
63- int sent = 0 ;
64-
65- sent = client.write ((byte*)msg, sz);
66-
67- return sent;
91+ int sent = 0 ;
92+ sent = client.write ((byte*)msg, sz);
93+ return sent;
6894}
6995
7096int EthernetReceive (WOLFSSL* ssl, char * reply, int sz, void * ctx) {
71- int ret = 0 ;
72-
73- while (client.available () > 0 && ret < sz) {
74- reply[ret++] = client.read ();
75- }
76-
77- return ret;
97+ int ret = 0 ;
98+ while (client.available () > 0 && ret < sz) {
99+ reply[ret++] = client.read ();
100+ }
101+ return ret;
78102}
79103
80104void loop () {
81- int err = 0 ;
82- int input = 0 ;
83- int total_input = 0 ;
84- char msg[32 ] = " hello wolfssl!" ;
85- int msgSz = (int )strlen (msg);
86- char errBuf[80 ];
87- char reply[80 ];
88- const char * cipherName;
89-
90- if (reconnect) {
91- reconnect--;
92-
93- if (client.connect (host, port)) {
94-
95- Serial.print (" Connected to " );
96- Serial.println (host);
97-
98- ssl = wolfSSL_new (ctx);
99- if (ssl == NULL ) {
100- Serial.println (" Unable to allocate SSL object" );
101- return ;
102- }
103-
104- err = wolfSSL_connect (ssl);
105- if (err != WOLFSSL_SUCCESS) {
106- err = wolfSSL_get_error (ssl, 0 );
107- wolfSSL_ERR_error_string (err, errBuf);
108- Serial.print (" TLS Connect Error: " );
109- Serial.println (errBuf);
110- }
111-
112- Serial.print (" SSL version is " );
113- Serial.println (wolfSSL_get_version (ssl));
114-
115- cipherName = wolfSSL_get_cipher (ssl);
116- Serial.print (" SSL cipher suite is " );
117- Serial.println (cipherName);
118-
119- if ((wolfSSL_write (ssl, msg, msgSz)) == msgSz) {
120-
121- Serial.print (" Server response: " );
122- /* wait for data */
123- while (!client.available ()) {}
124- /* read data */
125- while (wolfSSL_pending (ssl)) {
126- input = wolfSSL_read (ssl, reply, sizeof (reply) - 1 );
127- total_input += input;
128- if (input < 0 ) {
129- err = wolfSSL_get_error (ssl, 0 );
130- wolfSSL_ERR_error_string (err, errBuf);
131- Serial.print (" TLS Read Error: " );
132- Serial.println (errBuf);
133- break ;
134- } else if (input > 0 ) {
135- reply[input] = ' \0 ' ;
136- Serial.print (reply);
137- } else {
138- Serial.println ();
139- }
140- }
141- } else {
142- err = wolfSSL_get_error (ssl, 0 );
143- wolfSSL_ERR_error_string (err, errBuf);
144- Serial.print (" TLS Write Error: " );
145- Serial.println (errBuf);
146- }
147-
148- wolfSSL_shutdown (ssl);
149- wolfSSL_free (ssl);
150-
151- client.stop ();
152- Serial.println (" Connection complete." );
153- reconnect = 0 ;
154- } else {
155- Serial.println (" Trying to reconnect..." );
105+ int err = 0 ;
106+ int input = 0 ;
107+ int total_input = 0 ;
108+ char msg[32 ] = " hello wolfssl!" ;
109+ int msgSz = (int )strlen (msg);
110+ char errBuf[80 ];
111+ char reply[80 ];
112+ const char * cipherName;
113+ if (reconnect) {
114+ reconnect--;
115+ if (client.connect (host, port)) {
116+ Serial.print (" Connected to " );
117+ Serial.println (host);
118+ ssl = wolfSSL_new (ctx);
119+ if (ssl == NULL ) {
120+ Serial.println (" Unable to allocate SSL object" );
121+ return ;
122+ }
123+ err = wolfSSL_connect (ssl);
124+ if (err != WOLFSSL_SUCCESS) {
125+ err = wolfSSL_get_error (ssl, 0 );
126+ wolfSSL_ERR_error_string (err, errBuf);
127+ Serial.print (" TLS Connect Error: " );
128+ Serial.println (errBuf);
129+ }
130+ Serial.print (" SSL version is " );
131+ Serial.println (wolfSSL_get_version (ssl));
132+ cipherName = wolfSSL_get_cipher (ssl);
133+ Serial.print (" SSL cipher suite is " );
134+ Serial.println (cipherName);
135+ if ((wolfSSL_write (ssl, msg, msgSz)) == msgSz) {
136+ Serial.print (" Server response: " );
137+ /* wait for data */
138+ while (!client.available ()) {}
139+ /* read data */
140+ while (wolfSSL_pending (ssl)) {
141+ input = wolfSSL_read (ssl, reply, sizeof (reply) - 1 );
142+ total_input += input;
143+ if (input < 0 ) {
144+ err = wolfSSL_get_error (ssl, 0 );
145+ wolfSSL_ERR_error_string (err, errBuf);
146+ Serial.print (" TLS Read Error: " );
147+ Serial.println (errBuf);
148+ break ;
149+ }
150+ else if (input > 0 ) {
151+ reply[input] = ' \0 ' ;
152+ Serial.print (reply);
153+ }
154+ else {
155+ Serial.println ();
156+ }
157+ }
158+ }
159+ else {
160+ err = wolfSSL_get_error (ssl, 0 );
161+ wolfSSL_ERR_error_string (err, errBuf);
162+ Serial.print (" TLS Write Error: " );
163+ Serial.println (errBuf);
164+ }
165+ wolfSSL_shutdown (ssl);
166+ wolfSSL_free (ssl);
167+ client.stop ();
168+ Serial.println (" Connection complete." );
169+ reconnect = 0 ;
170+ }
171+ else {
172+ Serial.println (" Trying to reconnect..." );
173+ }
156174 }
157- }
158- delay (1000 );
175+ delay (1000 );
159176}
0 commit comments