Skip to content

Commit 430530c

Browse files
committed
SSL works until it runs out of memory
1 parent eb2c388 commit 430530c

File tree

15 files changed

+296
-19
lines changed

15 files changed

+296
-19
lines changed

ports/esp32s2/common-hal/socketpool/Socket.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const c
3939
// should become more and more common. Therefore, we optimize for the TLS case.
4040

4141
ESP_LOGI(TAG, "connecting to %s:%d %p", host, port, self->ssl_context);
42-
int result = esp_tls_conn_new_sync(host, hostlen, port, self->ssl_context, self->tcp);
42+
esp_tls_cfg_t* tls_config = NULL;
43+
if (self->ssl_context != NULL) {
44+
tls_config = &self->ssl_context->ssl_config;
45+
}
46+
int result = esp_tls_conn_new_sync(host, hostlen, port, tls_config, self->tcp);
4347
ESP_LOGI(TAG, "result %d", result);
4448
return result >= 0;
4549
}

ports/esp32s2/common-hal/socketpool/Socket.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
#include "py/obj.h"
3131

3232
#include "common-hal/socketpool/SocketPool.h"
33+
#include "common-hal/ssl/SSLContext.h"
3334

3435
#include "esp-idf/components/esp-tls/esp_tls.h"
3536

3637
typedef struct {
3738
mp_obj_base_t base;
3839
int num;
3940
esp_tls_t* tcp;
40-
esp_tls_cfg_t* ssl_context;
41+
ssl_sslcontext_obj_t* ssl_context;
4142
socketpool_socketpool_obj_t* pool;
4243
mp_uint_t timeout_ms;
4344
} socketpool_socket_obj_t;

ports/esp32s2/common-hal/ssl/SSLContext.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "shared-bindings/socketpool/SocketPool.h"
27+
#include "shared-bindings/ssl/SSLContext.h"
2828

2929
#include "py/runtime.h"
3030

31-
#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
31+
void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self) {
32+
33+
}
3234

3335
socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
3436
socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) {
3537

36-
38+
socket->ssl_context = self;
3739
// Should we store server hostname on the socket in case connect is called with an ip?
3840
return socket;
3941
}

ports/esp32s2/common-hal/ssl/SSLContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "py/obj.h"
3131

32+
#include "esp-idf/components/esp-tls/esp_tls.h"
33+
3234
typedef struct {
3335
mp_obj_base_t base;
3436
esp_tls_cfg_t ssl_config;

ports/esp32s2/common-hal/ssl/__init__.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
28-
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
27+
#include "shared-bindings/ssl/SSLContext.h"
2928

29+
#include "esp-idf/components/mbedtls/esp_crt_bundle/include/esp_crt_bundle.h"
3030

31-
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
31+
void common_hal_ssl_create_default_context(ssl_sslcontext_obj_t* self) {
32+
memset(&self->ssl_config, 0, sizeof(esp_tls_cfg_t));
33+
self->ssl_config.crt_bundle_attach = esp_crt_bundle_attach;
34+
}

ports/esp32s2/common-hal/ssl/__init__.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
28-
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL___INIT___H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL___INIT___H
2929

3030

31-
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
31+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL___INIT___H

ports/esp32s2/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "esp_log.h"
4949
static const char *TAG = "cp port";
5050

51-
#define HEAP_SIZE (64 * 1024)
51+
#define HEAP_SIZE (48 * 1024)
5252

5353
STATIC esp_timer_handle_t _tick_timer;
5454

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ endif
231231
ifeq ($(CIRCUITPY_SOCKETPOOL),1)
232232
SRC_PATTERNS += socketpool/%
233233
endif
234+
ifeq ($(CIRCUITPY_SSL),1)
235+
SRC_PATTERNS += ssl/%
236+
endif
234237
ifeq ($(CIRCUITPY_STAGE),1)
235238
SRC_PATTERNS += _stage/%
236239
endif
@@ -340,6 +343,8 @@ SRC_COMMON_HAL_ALL = \
340343
socketpool/__init__.c \
341344
socketpool/SocketPool.c \
342345
socketpool/Socket.c \
346+
ssl/__init__.c \
347+
ssl/SSLContext.c \
343348
supervisor/Runtime.c \
344349
supervisor/__init__.c \
345350
watchdog/WatchDogMode.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ extern const struct _mp_obj_module_t socketpool_module;
588588
#define SOCKETPOOL_MODULE
589589
#endif
590590

591+
#if CIRCUITPY_SSL
592+
extern const struct _mp_obj_module_t ssl_module;
593+
#define SSL_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_ssl), (mp_obj_t)&ssl_module },
594+
#else
595+
#define SSL_MODULE
596+
#endif
597+
591598
#if CIRCUITPY_STAGE
592599
extern const struct _mp_obj_module_t stage_module;
593600
#define STAGE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module },
@@ -773,6 +780,7 @@ extern const struct _mp_obj_module_t wifi_module;
773780
SDIOIO_MODULE \
774781
SHARPDISPLAY_MODULE \
775782
SOCKETPOOL_MODULE \
783+
SSL_MODULE \
776784
STAGE_MODULE \
777785
STORAGE_MODULE \
778786
STRUCT_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ CFLAGS += -DCIRCUITPY_SHARPDISPLAY=$(CIRCUITPY_SHARPDISPLAY)
188188
CIRCUITPY_SOCKETPOOL ?= $(CIRCUITPY_WIFI)
189189
CFLAGS += -DCIRCUITPY_SOCKETPOOL=$(CIRCUITPY_SOCKETPOOL)
190190

191+
CIRCUITPY_SSL ?= $(CIRCUITPY_WIFI)
192+
CFLAGS += -DCIRCUITPY_SSL=$(CIRCUITPY_SSL)
193+
191194
# Currently always off.
192195
CIRCUITPY_STAGE ?= 0
193196
CFLAGS += -DCIRCUITPY_STAGE=$(CIRCUITPY_STAGE)

0 commit comments

Comments
 (0)