@@ -166,6 +166,13 @@ static NORETURN void mbedtls_raise_error(int err) {
166
166
#endif
167
167
}
168
168
169
+ // Stores the current SSLContext for use in mbedtls callbacks where the current state is not passed.
170
+ static inline void store_active_context (mp_obj_ssl_context_t * ssl_context ) {
171
+ #if MICROPY_PY_SSL_MBEDTLS_NEED_ACTIVE_CONTEXT
172
+ MP_STATE_THREAD (tls_ssl_context ) = ssl_context ;
173
+ #endif
174
+ }
175
+
169
176
static void ssl_check_async_handshake_failure (mp_obj_ssl_socket_t * sslsock , int * errcode ) {
170
177
if (
171
178
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
@@ -497,6 +504,9 @@ static int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
497
504
static mp_obj_t ssl_socket_make_new (mp_obj_ssl_context_t * ssl_context , mp_obj_t sock ,
498
505
bool server_side , bool do_handshake_on_connect , mp_obj_t server_hostname ) {
499
506
507
+ // Store the current SSL context.
508
+ store_active_context (ssl_context );
509
+
500
510
// Verify the socket object has the full stream protocol
501
511
mp_get_stream_raise (sock , MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL );
502
512
@@ -602,6 +612,9 @@ static mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
602
612
return MP_STREAM_ERROR ;
603
613
}
604
614
615
+ // Store the current SSL context.
616
+ store_active_context (o -> ssl_context );
617
+
605
618
int ret = mbedtls_ssl_read (& o -> ssl , buf , size );
606
619
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ) {
607
620
// end of stream
@@ -643,6 +656,9 @@ static mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, in
643
656
return MP_STREAM_ERROR ;
644
657
}
645
658
659
+ // Store the current SSL context.
660
+ store_active_context (o -> ssl_context );
661
+
646
662
int ret = mbedtls_ssl_write (& o -> ssl , buf , size );
647
663
if (ret >= 0 ) {
648
664
return ret ;
@@ -680,6 +696,9 @@ static mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i
680
696
mp_obj_t sock = self -> sock ;
681
697
682
698
if (request == MP_STREAM_CLOSE ) {
699
+ // Clear the SSL context.
700
+ store_active_context (NULL );
701
+
683
702
if (sock == MP_OBJ_NULL ) {
684
703
// Already closed socket, do nothing.
685
704
return 0 ;
0 commit comments