-
Notifications
You must be signed in to change notification settings - Fork 968
Expand file tree
/
Copy pathserver-tls.c
More file actions
467 lines (416 loc) · 15.8 KB
/
server-tls.c
File metadata and controls
467 lines (416 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* server-tls.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include "server-tls.h"
/* Espressif FreeRTOS */
#ifndef SINGLE_THREADED
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#endif
/* socket includes */
#include <lwip/netdb.h>
#include <lwip/sockets.h>
#include <netinet/tcp.h> /* For TCP options */
#include <sys/socket.h>
#ifndef TCP_RTO_MIN
#define TCP_RTO_MIN 1500
#endif
/* wolfSSL */
/* Always include wolfcrypt/settings.h before any other wolfSSL file. */
/* Reminder: settings.h pulls in user_settings.h; don't include it here. */
#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#ifndef WOLFSSL_ESPIDF
#warning "Problem with wolfSSL user_settings."
#warning "Check components/wolfssl/include"
#endif
#include <wolfssl/ssl.h>
#else
/* Define WOLFSSL_USER_SETTINGS project wide for settings.h to include */
/* wolfSSL user settings in ./components/wolfssl/include/user_settings.h */
#error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile:\
CFLAGS +=-DWOLFSSL_USER_SETTINGS"
#endif
#if defined(WOLFSSL_HAVE_MLKEM)
#include <wolfssl/wolfcrypt/wc_mlkem.h>
#endif
#if defined(USE_CERT_BUFFERS_2048) || defined(USE_CERT_BUFFERS_1024)
#include <wolfssl/certs_test.h>
#endif
#ifdef WOLFSSL_TRACK_MEMORY
#include <wolfssl/wolfcrypt/mem_track.h>
#endif
#ifndef NO_DH
/* see also wolfssl/test.h */
#undef DEFAULT_MIN_DHKEY_BITS
#define DEFAULT_MIN_DHKEY_BITS 1024
#undef DEFAULT_MAX_DHKEY_BITS
#define DEFAULT_MAX_DHKEY_BITS 2048
#endif
/* Project */
#include "wifi_connect.h"
#include "time_helper.h"
static const char* const TAG = "server-tls";
int stack_start = -1;
int ShowCiphers(WOLFSSL* ssl)
{
#define CLIENT_TLS_MAX_CIPHER_LENGTH 4096
char ciphers[CLIENT_TLS_MAX_CIPHER_LENGTH];
const char* cipher_used;
int ret = 0;
if (ssl == NULL) {
ESP_LOGI(TAG, "WOLFSSL* ssl is NULL, so no cipher in use");
ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
if (ret == WOLFSSL_SUCCESS) {
for (int i = 0; i < CLIENT_TLS_MAX_CIPHER_LENGTH; i++) {
if (ciphers[i] == ':') {
ciphers[i] = '\n';
}
}
ESP_LOGI(TAG, "Available Ciphers:\n%s\n", ciphers);
}
else {
ESP_LOGE(TAG, "Failed to call wolfSSL_get_ciphers. Error: %d", ret);
}
}
else {
cipher_used = wolfSSL_get_cipher_name(ssl);
ESP_LOGI(TAG, "WOLFSSL* ssl using %s", cipher_used);
}
return ret;
}
/* FreeRTOS */
/* server task */
WOLFSSL_ESP_TASK tls_smp_server_task(void *args)
{
#if defined(SINGLE_THREADED)
#define TLS_SMP_SERVER_TASK_RET ret
#else
#define TLS_SMP_SERVER_TASK_RET
#endif
char buff[256];
const char msg[] = "I hear you fa shizzle!";
struct sockaddr_in servAddr;
struct sockaddr_in clientAddr;
int sockfd;
int connd;
int shutdown = 0;
int ret;
int ret_i; /* interim return values */
socklen_t size = sizeof(clientAddr);
size_t len;
#if 0
/* optionally set TCP RTO. See also below. */
int rto_min = 200; /* Minimum TCP RTO in milliseconds */
#endif
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
WOLFSSL_ENTER("tls_smp_server_task");
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
ShowCiphers(NULL);
#endif
/* Initialize wolfSSL */
WOLFSSL_MSG("Start wolfSSL_Init()");
wolfSSL_Init();
/* Create a socket that uses an internet IPv4 address,
* Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */
WOLFSSL_MSG( "start socket())");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
ESP_LOGE(TAG, "ERROR: failed to create the socket");
}
/* Optionally set TCP RTO
setsockopt(sockfd, IPPROTO_TCP, TCP_RTO_MIN, &rto_min, sizeof(rto_min)); */
/* Create and initialize WOLFSSL_CTX */
WOLFSSL_MSG("Create and initialize WOLFSSL_CTX");
#if defined(WOLFSSL_SM2) || defined(WOLFSSL_SM3) || defined(WOLFSSL_SM4)
ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
/* ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()); for only TLS 1.3 */
if (ctx == NULL) {
ESP_LOGE(TAG, "ERROR: failed to create WOLFSSL_CTX");
}
#else
if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())) == NULL) {
ESP_LOGE(TAG, "ERROR: failed to create WOLFSSL_CTX");
}
#endif
#if defined(WOLFSSL_SM2) || defined(WOLFSSL_SM3) || defined(WOLFSSL_SM4)
ESP_LOGI(TAG, "Start SM3\n");
/* Optional set explicit ciphers
ret = wolfSSL_CTX_set_cipher_list(ctx, WOLFSSL_ESP32_CIPHER_SUITE);
if (ret == SSL_SUCCESS) {
ESP_LOGI(TAG, "Set cipher list: "WOLFSSL_ESP32_CIPHER_SUITE"\n");
}
else {
ESP_LOGE(TAG, "ERROR: failed to set cipher list: "WOLFSSL_ESP32_CIPHER_SUITE"\n");
}
*/
ShowCiphers(NULL);
ESP_LOGI(TAG, "Stack used: %d\n", CONFIG_ESP_MAIN_TASK_STACK_SIZE
- uxTaskGetStackHighWaterMark(NULL));
WOLFSSL_MSG("Loading certificate...");
/* -c Load server certificates into WOLFSSL_CTX */
ret = wolfSSL_CTX_use_certificate_chain_buffer_format(ctx,
CTX_SERVER_CERT,
CTX_SERVER_CERT_SIZE,
CTX_SERVER_CERT_TYPE
);
/* optional wolfSSL_CTX_use_certificate_buffer
ret = wolfSSL_CTX_use_certificate_buffer(ctx,
server_sm2,
sizeof_server_sm2,
WOLFSSL_FILETYPE_PEM);
*/
if (ret == SSL_SUCCESS) {
ESP_LOGI(TAG, "Loaded server_sm2\n");
}
else {
ESP_LOGE(TAG, "ERROR: failed to load cert\n");
}
ESP_LOGI(TAG, "Stack used: %d\n", CONFIG_ESP_MAIN_TASK_STACK_SIZE
- uxTaskGetStackHighWaterMark(NULL));
#ifndef NO_DH
#define DEFAULT_MIN_DHKEY_BITS 1024
#define DEFAULT_MAX_DHKEY_BITS 2048
int minDhKeyBits = DEFAULT_MIN_DHKEY_BITS;
ret = wolfSSL_CTX_SetMinDhKey_Sz(ctx, (word16)minDhKeyBits);
#endif
#ifndef NO_RSA
#define DEFAULT_MIN_RSAKEY_BITS 1024
short minRsaKeyBits = DEFAULT_MIN_RSAKEY_BITS;
ret = wolfSSL_CTX_SetMinRsaKey_Sz(ctx, minRsaKeyBits);
#endif
WOLFSSL_MSG("Loading key info...");
/* -k Load server key into WOLFSSL_CTX */
ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx,
CTX_SERVER_KEY,
CTX_SERVER_KEY_SIZE,
CTX_SERVER_KEY_TYPE);
if (ret == SSL_SUCCESS) {
ESP_LOGI(TAG, "Loaded PrivateKey_buffer server_sm2_priv\n");
}
else {
ESP_LOGE(TAG, "ERROR: failed to load "
"PrivateKey_buffer server_sm2_priv\n");
}
ESP_LOGI(TAG, "Stack used: %d\n", CONFIG_ESP_MAIN_TASK_STACK_SIZE
- uxTaskGetStackHighWaterMark(NULL));
/* -A load authority */
ret = wolfSSL_CTX_load_verify_buffer(ctx,
client_sm2,
sizeof_client_sm2,
WOLFSSL_FILETYPE_PEM);
if (ret == SSL_SUCCESS) {
ESP_LOGI(TAG, "Success: load verify buffer\n");
}
else {
ESP_LOGE(TAG, "ERROR: failed to load verify buffer\n");
}
ESP_LOGI(TAG, "Finish SM2\n");
#else
WOLFSSL_MSG("Loading certificate...");
/* Load server certificates into WOLFSSL_CTX */
if ((ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
sizeof_server_cert_der_2048,
WOLFSSL_FILETYPE_ASN1)) != SSL_SUCCESS) {
ESP_LOGE(TAG, "ERROR: failed to load cert");
}
WOLFSSL_MSG("Loading key info...");
/* Load server key into WOLFSSL_CTX */
if((ret=wolfSSL_CTX_use_PrivateKey_buffer(ctx,
server_key_der_2048, sizeof_server_key_der_2048,
WOLFSSL_FILETYPE_ASN1)) != SSL_SUCCESS) {
ESP_LOGE(TAG, "ERROR: failed to load privatekey");
}
#endif
/* TODO when using ECDSA,it loads the provisioned certificate and present it.
TODO when using ECDSA,it uses the generated key instead of loading key */
/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
servAddr.sin_port = htons(TLS_SMP_DEFAULT_PORT); /* on port */
servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
/* Bind the server socket to our port */
if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
ESP_LOGE(TAG, "ERROR: failed to bind");
}
/* Listen for a new connection, allow 5 pending connections */
if (listen(sockfd, 5) == -1) {
ESP_LOGE(TAG, "ERROR: failed to listen");
}
#if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
&& defined(WOLFSSL_ATECC508A)
atcatls_set_callbacks(ctx);
/* when using a custom slot allocation */
#if defined(CUSTOM_SLOT_ALLOCATION)
my_atmel_slotInit();
atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
#endif
#endif
#ifdef WOLFSSL_EXAMPLE_VERBOSITY
ESP_LOGI(TAG, "Initial stack used: %d\n",
TLS_SMP_SERVER_TASK_BYTES - uxTaskGetStackHighWaterMark(NULL) );
#endif
ESP_LOGI(TAG, "accept clients...");
/* Continue to accept clients until shutdown is issued */
while (!shutdown) {
WOLFSSL_MSG("Waiting for a connection...");
#if ESP_IDF_VERSION_MAJOR >=4
/* TODO: IP Address is problematic in RTOS SDK 3.4 */
wifi_show_ip();
#endif
/* Accept client socket connections */
if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size))
== -1) {
ESP_LOGE(TAG, "ERROR: failed to accept the connection");
}
#if defined(WOLFSSL_EXPERIMENTAL_SETTINGS)
ESP_LOGW(TAG, "WOLFSSL_EXPERIMENTAL_SETTINGS is enabled");
#endif
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
ESP_LOGE(TAG, "ERROR: failed to create WOLFSSL object");
}
else {
#ifdef DEBUG_WOLFSSL
ESP_LOGI(TAG, "\nCreated WOLFSSL object:");
ShowCiphers(ssl);
this_heap = esp_get_free_heap_size();
ESP_LOGI(TAG, "tls_smp_client_task heap @ %p = %d",
&this_heap, this_heap);
#endif
#if defined(WOLFSSL_HAVE_MLKEM)
/* Client sets the keyshare; we at the server only need to enable it. */
ESP_LOGI(TAG, "WOLFSSL_HAVE_MLKEM is enabled");
ret_i = WOLFSSL_SUCCESS;
#if defined(WOLFSSL_KYBER1024)
ESP_LOGI(TAG, "WOLFSSL_KYBER1024 is enabled");
#elif defined(WOLFSSL_KYBER768)
ESP_LOGI(TAG, "WOLFSSL_KYBER768 is enabled");
#elif defined(WOLFSSL_KYBER512)
ESP_LOGI(TAG, "WOLFSSL_KYBER512 is enabled");
#else
ESP_LOGW(TAG, "WOLFSSL_HAVE_MLKEM enabled but no key size available.");
ret_i = ESP_FAIL;
#endif
if (ret_i == WOLFSSL_SUCCESS) {
ESP_LOGI(TAG, "WOLFSSL_HAVE_MLKEM success");
}
else {
ESP_LOGE(TAG, "WOLFSSL_HAVE_MLKEM failed");
}
#else
ESP_LOGI(TAG, "WOLFSSL_HAVE_MLKEM is not enabled, not using PQ.");
#endif
}
/* show what cipher connected for this WOLFSSL* object */
ShowCiphers(ssl);
/* Attach wolfSSL to the socket */
wolfSSL_set_fd(ssl, connd);
/* Establish TLS connection */
ret = wolfSSL_accept(ssl);
if (ret == SSL_SUCCESS) {
ShowCiphers(ssl);
const char* curve = wolfSSL_get_curve_name(ssl);
ESP_LOGI(TAG, "Server negotiated key share group: %s", curve);
}
else {
ESP_LOGE(TAG, "wolfSSL_accept error %d",
wolfSSL_get_error(ssl, ret));
}
ESP_LOGI(TAG, "Client connected successfully");
/* Read the client data into our buff array */
memset(buff, 0, sizeof(buff));
if (wolfSSL_read(ssl, buff, sizeof(buff)-1) == -1) {
ESP_LOGE(TAG, "ERROR: failed to read");
}
ESP_LOGI(TAG, "Client sends: %s", buff);
/* Check for server shutdown command */
if (strncmp(buff, "shutdown", 8) == 0) {
ESP_LOGI(TAG, "Shutdown command issued!");
shutdown = 1;
}
/* Write our reply into buff */
memset(buff, 0, sizeof(buff));
memcpy(buff, msg, sizeof(msg));
len = strnlen(buff, sizeof(buff));
/* Reply back to the client */
if (wolfSSL_write(ssl, buff, len) != len) {
ESP_LOGE(TAG, "ERROR: failed to write");
}
ESP_LOGI(TAG, "Done! Cleanup...");
/* Cleanup after this connection */
wolfSSL_free(ssl); /* Free the wolfSSL object */
close(connd); /* Close the connection to the client */
#ifdef WOLFSSL_EXAMPLE_VERBOSITY
ESP_LOGI(TAG, "Stack used: %d\n",
TLS_SMP_SERVER_TASK_BYTES - uxTaskGetStackHighWaterMark(NULL));
#endif
} /* !shutdown */
/* Cleanup and return */
wolfSSL_free(ssl); /* Free the wolfSSL object */
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
close(sockfd); /* Close the socket listening for clients */
vTaskDelete(NULL);
return TLS_SMP_SERVER_TASK_RET;
}
#if defined(SINGLE_THREADED)
/* we don't initialize a thread */
#else
/* create task */
WOLFSSL_ESP_TASK tls_smp_server_init(void* args)
{
#if defined(SINGLE_THREADED)
#define TLS_SMP_CLIENT_TASK_RET ret
#else
#define TLS_SMP_CLIENT_TASK_RET
#endif
int thisPort = 0;
int ret_i = 0; /* interim return result */
if (thisPort == 0) {
thisPort = TLS_SMP_DEFAULT_PORT;
}
#if ESP_IDF_VERSION_MAJOR >= 4
TaskHandle_t _handle;
#else
xTaskHandle _handle;
#endif
/* Note that despite vanilla FreeRTOS using WORDS for a parameter,
* Espressif uses BYTES for the task stack size here. */
ESP_LOGI(TAG, "Creating tls_smp_server_task with stack size = %d",
TLS_SMP_SERVER_TASK_BYTES);
ret_i = xTaskCreate(tls_smp_server_task,
TLS_SMP_SERVER_TASK_NAME,
TLS_SMP_SERVER_TASK_BYTES,
(void*)&thisPort,
TLS_SMP_SERVER_TASK_PRIORITY,
&_handle);
if (ret_i != pdPASS) {
ESP_LOGI(TAG, "create thread %s failed", TLS_SMP_SERVER_TASK_NAME);
}
/* vTaskStartScheduler(); called automatically in ESP-IDF */
return TLS_SMP_CLIENT_TASK_RET;
}
#endif