Skip to content

Commit d4e4291

Browse files
author
Emma Stensland
committed
F-5610 F-1701 F-1297 wolfssl macro updates, rsa verify fix and cleanup fixes
1 parent 54f85ad commit d4e4291

18 files changed

Lines changed: 132 additions & 122 deletions

File tree

ESP32/DTLS13-wifi-station-client/main/client-dtls13.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ WOLFSSL_ESP_TASK dtls13_smp_client_task(void *pvParameters)
156156
ESP_LOGI(TAG, "See ./include/client-dtls13.h to update settings.");
157157
ESP_LOGI(TAG, "Setting server address to %s, port %d.",
158158
TLS_SMP_SERVER_ADDRESS, SERV_PORT);
159-
memset(&servAddr, 0, sizeof(servAddr));
159+
XMEMSET(&servAddr, 0, sizeof(servAddr));
160160
servAddr.sin_family = AF_INET;
161161
servAddr.sin_port = htons(SERV_PORT);
162162
if (inet_pton(AF_INET, TLS_SMP_SERVER_ADDRESS, &servAddr.sin_addr) < 1) {
@@ -202,7 +202,7 @@ WOLFSSL_ESP_TASK dtls13_smp_client_task(void *pvParameters)
202202
strcpy(sendLine, "Hello World.");
203203

204204
/* Send sendLine to the server */
205-
if (wolfSSL_write(ssl, sendLine, strlen(sendLine)) != strlen(sendLine)) {
205+
if (wolfSSL_write(ssl, sendLine, XSTRLEN(sendLine)) != XSTRLEN(sendLine)) {
206206
err = wolfSSL_get_error(ssl, 0);
207207
ESP_LOGE(TAG, "err = %d, %s\n",
208208
err, wolfSSL_ERR_reason_error_string(err));

ESP32/DTLS13-wifi-station-client/main/time_helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int set_time(void)
189189
ESP_LOGI(TAG, "sntp_setservername:");
190190
for (i = 0; i < NTP_SERVER_COUNT; i++) {
191191
const char* thisServer = ntpServerList[i];
192-
if (strncmp(thisServer, "\x00", 1) == 0) {
192+
if (XSTRNCMP(thisServer, "\x00", 1) == 0) {
193193
/* just in case we run out of NTP servers */
194194
break;
195195
}

ESP32/DTLS13-wifi-station-server/main/server-dtls13.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ WOLFSSL_ESP_TASK dtls13_smp_server_task(void *pvParameters)
244244

245245
/* initialize network vars */
246246
if (ret == WOLFSSL_SUCCESS) {
247-
memset((char *)&servAddr, 0, sizeof(servAddr));
247+
XMEMSET((char *)&servAddr, 0, sizeof(servAddr));
248248
/* host-to-network-long conversion (htonl) */
249249
/* host-to-network-short conversion (htons) */
250250
servAddr.sin_family = AF_INET;

ESP32/DTLS13-wifi-station-server/main/time_helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int set_time(void)
202202
ESP_LOGI(TAG, "sntp_setservername:");
203203
for (i = 0; i < NTP_SERVER_COUNT; i++) {
204204
const char* thisServer = ntpServerList[i];
205-
if (strncmp(thisServer, "\x00", 1) == 0) {
205+
if (XSTRNCMP(thisServer, "\x00", 1) == 0) {
206206
/* just in case we run out of NTP servers */
207207
break;
208208
}

ESP32/TLS13-ENC28J60-client/main/enc28j60_example_main.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int tls_smp_client_task() {
165165
#endif /* WOLFSSL_TLS13 */
166166

167167
/* Initialize the server address struct with zeros */
168-
memset(&servAddr, 0, sizeof(servAddr));
168+
XMEMSET(&servAddr, 0, sizeof(servAddr));
169169

170170
/* Fill in the server address */
171171
servAddr.sin_family = AF_INET; /* using IPv4 */
@@ -220,13 +220,14 @@ int tls_smp_client_task() {
220220
* a non-negative integer, the socket file descriptor.
221221
*/
222222
sockfd = socket(AF_INET, SOCK_STREAM, 0);
223-
if (sockfd > 0) {
223+
if (sockfd >= 0) {
224224
WOLFSSL_MSG("socket creation successful\n");
225225
}
226226
else {
227-
// TODO show errno
227+
char err_msg[128];
228+
XSNPRINTF(err_msg, sizeof(err_msg), "ERROR: failed to create a socket (errno = %d).\n", errno);
229+
WOLFSSL_ERROR_MSG(err_msg);
228230
ret = WOLFSSL_FAILURE;
229-
WOLFSSL_ERROR_MSG("ERROR: failed to create a socket.\n");
230231
}
231232
}
232233
else {
@@ -258,8 +259,9 @@ int tls_smp_client_task() {
258259
WOLFSSL_MSG("sockfd connect successful\n");
259260
}
260261
else {
261-
// TODO show errno
262-
WOLFSSL_ERROR_MSG("ERROR: socket connect failed\n");
262+
char err_msg[128];
263+
XSNPRINTF(err_msg, sizeof(err_msg), "ERROR: socket connect failed (errno = %d)\n", errno);
264+
WOLFSSL_ERROR_MSG(err_msg);
263265
ret = WOLFSSL_FAILURE;
264266
}
265267
}
@@ -697,13 +699,13 @@ int tls_smp_client_task() {
697699
*/
698700
if (ret == WOLFSSL_SUCCESS) {
699701

700-
memset(buff, 0, BUFF_SIZE);
702+
XMEMSET(buff, 0, BUFF_SIZE);
701703

702704
/* get the length of our message, never longer than the declared size */
703705

704706
/* TODO check for zero length */
705707

706-
len = strnlen(sendMessage, sendMessageSize);
708+
len = XSTRLEN(sendMessage);
707709

708710
/* write the message over secure connection to the server */
709711
if (wolfSSL_write(ssl, sendMessage, len) == len) {
@@ -783,7 +785,7 @@ int tls_smp_client_task() {
783785
if (ret == WOLFSSL_SUCCESS) {
784786
/* even though the result should be a zero-terminated string,
785787
* we'll clear the receive buffer */
786-
memset(buff, 0, BUFF_SIZE);
788+
XMEMSET(buff, 0, BUFF_SIZE);
787789

788790
/* read the response data from our secure connection */
789791
if (wolfSSL_read(ssl, buff, BUFF_SIZE - 1) > 0) {
@@ -946,7 +948,7 @@ int set_time() {
946948
int i = 0;
947949
for (i = 0; i < NTP_SERVER_COUNT; i++) {
948950
const char* thisServer = ntpServerList[i];
949-
if (strncmp(thisServer, "\x00", 1)) {
951+
if (XSTRNCMP(thisServer, "\x00", 1)) {
950952
/* just in case we run out of NTP servers */
951953
break;
952954
}

ESP32/TLS13-ENC28J60-client/main/esp_eth_mac_enc28j60.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static esp_err_t emac_enc28j60_set_addr(esp_eth_mac_t *mac, uint8_t *addr)
619619
esp_err_t ret = ESP_OK;
620620
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
621621
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
622-
memcpy(emac->addr, addr, 6);
622+
XMEMCPY(emac->addr, addr, 6);
623623
MAC_CHECK(enc28j60_set_mac_addr(emac) == ESP_OK, "set mac address failed", out, ESP_FAIL);
624624
out:
625625
return ret;
@@ -630,7 +630,7 @@ static esp_err_t emac_enc28j60_get_addr(esp_eth_mac_t *mac, uint8_t *addr)
630630
esp_err_t ret = ESP_OK;
631631
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
632632
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
633-
memcpy(addr, emac->addr, 6);
633+
XMEMCPY(addr, emac->addr, 6);
634634
out:
635635
return ret;
636636
}

ESP32/TLS13-ENC28J60-server/components/eth_enc28j60/esp_eth_mac_enc28j60.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ static esp_err_t emac_enc28j60_set_addr(esp_eth_mac_t *mac, uint8_t *addr)
684684
esp_err_t ret = ESP_OK;
685685
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
686686
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
687-
memcpy(emac->addr, addr, 6);
687+
XMEMCPY(emac->addr, addr, 6);
688688
MAC_CHECK(enc28j60_set_mac_addr(emac) == ESP_OK, "set mac address failed", out, ESP_FAIL);
689689
out:
690690
return ret;
@@ -695,7 +695,7 @@ static esp_err_t emac_enc28j60_get_addr(esp_eth_mac_t *mac, uint8_t *addr)
695695
esp_err_t ret = ESP_OK;
696696
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
697697
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
698-
memcpy(addr, emac->addr, 6);
698+
XMEMCPY(addr, emac->addr, 6);
699699
out:
700700
return ret;
701701
}

ESP32/TLS13-ENC28J60-server/main/enc28j60_example_main.c

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ int tls_smp_server_task() {
190190
#endif /* WOLFSSL_TLS13 */
191191

192192
/* Initialize the server address struct with zeros */
193-
memset(&servAddr, 0, sizeof(servAddr));
193+
XMEMSET(&servAddr, 0, sizeof(servAddr));
194194

195195
/* Fill in the server address */
196196
servAddr.sin_family = AF_INET; /* using IPv4 */
@@ -280,13 +280,14 @@ int tls_smp_server_task() {
280280
* a non-negative integer, the socket file descriptor.
281281
*/
282282
sockfd = socket(AF_INET, SOCK_STREAM, 0);
283-
if (sockfd > 0) {
283+
if (sockfd >= 0) {
284284
WOLFSSL_MSG("socket creation successful\n");
285285
}
286286
else {
287-
// TODO show errno
287+
char err_msg[128];
288+
XSNPRINTF(err_msg, sizeof(err_msg), "ERROR: failed to create a socket (errno = %d).\n", errno);
289+
WOLFSSL_ERROR_MSG(err_msg);
288290
ret = WOLFSSL_FAILURE;
289-
WOLFSSL_ERROR_MSG("ERROR: failed to create a socket.\n");
290291
}
291292
}
292293
else {
@@ -348,9 +349,10 @@ int tls_smp_server_task() {
348349
WOLFSSL_MSG("setsockopt re-use addr successful\n");
349350
}
350351
else {
351-
// TODO show errno
352+
char err_msg[128];
353+
XSNPRINTF(err_msg, sizeof(err_msg), "ERROR: failed to setsockopt addr on socket (errno = %d).\n", errno);
354+
WOLFSSL_ERROR_MSG(err_msg);
352355
ret = WOLFSSL_FAILURE;
353-
WOLFSSL_ERROR_MSG("ERROR: failed to setsockopt addr on socket.\n");
354356
}
355357
}
356358
else {
@@ -370,10 +372,9 @@ int tls_smp_server_task() {
370372
WOLFSSL_MSG("setsockopt re-use port successful\n");
371373
}
372374
else {
373-
// TODO show errno
374-
// ret = WOLFSSL_FAILURE;
375-
// TODO what's up with the error?
376-
WOLFSSL_ERROR_MSG("ERROR: failed to setsockopt port on socket. >> IGNORED << \n");
375+
char err_msg[128];
376+
XSNPRINTF(err_msg, sizeof(err_msg), "ERROR: failed to setsockopt port on socket (errno = %d). >> IGNORED << \n", errno);
377+
WOLFSSL_ERROR_MSG(err_msg);
377378
}
378379
}
379380
else {
@@ -738,21 +739,19 @@ int tls_smp_server_task() {
738739
/* Accept client connections */
739740
if ((mConnd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
740741
== -1) {
741-
// fprintf(stderr, "ERROR: failed to accept the connection\n\n");
742-
ret = -1;
743-
// TODO goto exit;
744-
WOLFSSL_ERROR_MSG("ERROR: failed socket accept\n");
745-
ret = WOLFSSL_FAILURE;
742+
WOLFSSL_ERROR_MSG("ERROR: failed socket accept\n");
743+
ret = WOLFSSL_FAILURE;
744+
continue;
746745
}
747746

748747
/* Create a WOLFSSL object */
749748
if ((ssl = wolfSSL_new(ctx)) == NULL) {
750-
// fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
751-
ret = -1;
752-
//TODO goto exit;
753-
WOLFSSL_ERROR_MSG("ERROR: filed wolfSSL_new during loop\n");
749+
WOLFSSL_ERROR_MSG("ERROR: failed wolfSSL_new during loop\n");
754750
ret = WOLFSSL_FAILURE;
755-
}
751+
close(mConnd);
752+
mConnd = SOCKET_INVALID;
753+
continue;
754+
}
756755

757756
/* Attach wolfSSL to the socket */
758757
wolfSSL_set_fd(ssl, mConnd);
@@ -771,45 +770,42 @@ int tls_smp_server_task() {
771770
if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
772771
WOLFSSL_ERROR_MSG("ERROR: wolfSSL_accept\n");
773772
ret = WOLFSSL_FAILURE;
774-
// fprintf(stderr,
775-
// "wolfSSL_accept error = %d\n",
776-
// wolfSSL_get_error(ssl, ret));
777-
// TODO goto exit;
778773
}
779774
else {
780775
WOLFSSL_MSG("Client connected successfully\n");
781776
}
782777

783-
784778
#ifdef HAVE_SECRET_CALLBACK
785-
wolfSSL_FreeArrays(ssl);
779+
if (ret == WOLFSSL_SUCCESS) {
780+
wolfSSL_FreeArrays(ssl);
781+
}
786782
#endif
787783

788784
/* Read the client data into our buff array */
789-
memset(buff, 0, sizeof(buff));
790-
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) < 0) {
791-
// fprintf(stderr, "ERROR: failed to read\n");
792-
//TODO goto exit;
785+
if (ret == WOLFSSL_SUCCESS) {
786+
XMEMSET(buff, 0, sizeof(buff));
787+
if (wolfSSL_read(ssl, buff, sizeof(buff) - 1) <= 0) {
788+
WOLFSSL_ERROR_MSG("ERROR: failed to read\n");
789+
ret = WOLFSSL_FAILURE;
790+
}
793791
}
794792

795-
/* Print to stdout any data the client sends */
796-
// printf("Client: %s\n", buff);
797-
798-
/* Check for server shutdown command */
799-
if (strncmp(buff, "shutdown", 8) == 0) {
800-
// printf("Shutdown command issued!\n");
801-
mShutdown = 1;
802-
}
793+
if (ret == WOLFSSL_SUCCESS) {
794+
/* Check for server shutdown command */
795+
if (XSTRNCMP(buff, "shutdown", 8) == 0) {
796+
mShutdown = 1;
797+
}
803798

804-
/* Write our reply into buff */
805-
memset(buff, 0, sizeof(buff));
806-
memcpy(buff, reply, strlen(reply));
807-
len = strnlen(buff, sizeof(buff));
799+
/* Write our reply into buff */
800+
XMEMSET(buff, 0, sizeof(buff));
801+
XMEMCPY(buff, reply, XSTRLEN(reply));
802+
len = XSTRLEN(buff);
808803

809-
/* Reply back to the client */
810-
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
811-
// fprintf(stderr, "ERROR: failed to write\n");
812-
// TODO goto exit;
804+
/* Reply back to the client */
805+
if (wolfSSL_write(ssl, buff, len) != len) {
806+
WOLFSSL_ERROR_MSG("ERROR: failed to write\n");
807+
ret = WOLFSSL_FAILURE;
808+
}
813809
}
814810

815811
/* Cleanup after this connection */
@@ -967,7 +963,7 @@ int set_time() {
967963
int i = 0;
968964
for (i = 0; i < NTP_SERVER_COUNT; i++) {
969965
const char* thisServer = ntpServerList[i];
970-
if (strncmp(thisServer, "\x00", 1)) {
966+
if (XSTRNCMP(thisServer, "\x00", 1)) {
971967
/* just in case we run out of NTP servers */
972968
break;
973969
}

ESP32/TLS13-ENC28J60-server/main/esp_eth_mac_enc28j60.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static esp_err_t emac_enc28j60_set_addr(esp_eth_mac_t *mac, uint8_t *addr)
619619
esp_err_t ret = ESP_OK;
620620
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
621621
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
622-
memcpy(emac->addr, addr, 6);
622+
XMEMCPY(emac->addr, addr, 6);
623623
MAC_CHECK(enc28j60_set_mac_addr(emac) == ESP_OK, "set mac address failed", out, ESP_FAIL);
624624
out:
625625
return ret;
@@ -630,7 +630,7 @@ static esp_err_t emac_enc28j60_get_addr(esp_eth_mac_t *mac, uint8_t *addr)
630630
esp_err_t ret = ESP_OK;
631631
MAC_CHECK(addr, "can't set mac addr to null", out, ESP_ERR_INVALID_ARG);
632632
emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent);
633-
memcpy(addr, emac->addr, 6);
633+
XMEMCPY(addr, emac->addr, 6);
634634
out:
635635
return ret;
636636
}

0 commit comments

Comments
 (0)