Skip to content

Commit c4b77ad

Browse files
Merge pull request #7007 from night1rider/ardunio-wolfssl
Ardunio Fixes relating to internal Intel Galileo Tests
2 parents 6c8bf7b + 0ff02e5 commit c4b77ad

3 files changed

Lines changed: 247 additions & 174 deletions

File tree

IDE/ARDUINO/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
##### Reformatting wolfSSL as a compatible Arduino Library
44
This is a shell script that will re-organize the wolfSSL library to be
5-
compatible with Arduino projects. The Arduino IDE requires a library's source
6-
files to be in the library's root directory with a header file in the name of
7-
the library. This script moves all src/ files to the `IDE/ARDUINO/wolfSSL`
8-
directory and creates a stub header file called `wolfssl.h`.
5+
compatible with Arduino projects that use Arduino IDE 1.5.0 or newer.
6+
The Arduino IDE requires a library's source files to be in the library's root
7+
directory with a header file in the name of the library. This script moves all
8+
src/ files to the `IDE/ARDUINO/wolfSSL/src` directory and creates a stub header
9+
file called `wolfssl.h` inside that directory.
910

1011
Step 1: To configure wolfSSL with Arduino, enter the following from within the
1112
wolfssl/IDE/ARDUINO directory:
@@ -15,7 +16,7 @@ wolfssl/IDE/ARDUINO directory:
1516
Step 2: Copy the directory wolfSSL that was just created to:
1617
`~/Documents/Arduino/libraries/` directory so the Arduino IDE can find it.
1718

18-
Step 3: Edit `<arduino-libraries>/wolfSSL/user_settings.h`
19+
Step 3: Edit `<arduino-libraries>/wolfSSL/src/user_settings.h`
1920
If building for Intel Galileo platform add: `#define INTEL_GALILEO`.
2021
Add any other custom settings, for a good start see the examples in wolfssl root
2122
"/examples/configs/user_settings_*.h"

IDE/ARDUINO/sketches/wolfssl_client/wolfssl_client.ino

Lines changed: 124 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@
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

2735
const char host[] = "192.168.1.148"; /* server to connect to */
2836
const int port = 11111; /* port on server to connect to */
@@ -37,123 +45,132 @@ WOLFSSL_CTX* ctx = NULL;
3745
WOLFSSL* ssl = NULL;
3846

3947
void 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

6290
int 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

7096
int 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

80104
void 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

Comments
 (0)