33
33
#include <errno.h>
34
34
#include <string.h>
35
35
36
+ #include <esp_log.h>
37
+
36
38
#include "py/nlr.h"
37
39
#include "py/runtime.h"
38
40
#include "py/stream.h"
46
48
#include "mbedtls/entropy.h"
47
49
#include "mbedtls/ctr_drbg.h"
48
50
#include "mbedtls/debug.h"
51
+ #include "mbedtls/error.h"
49
52
50
53
#include "wildcard_sha2017_org.h"
51
54
55
+ #define TAG "modussl_mbedtls.c"
56
+
52
57
typedef struct _mp_obj_ssl_socket_t {
53
58
mp_obj_base_t base ;
54
59
mp_obj_t sock ;
@@ -86,6 +91,11 @@ int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
86
91
if (mp_is_nonblocking_error (err )) {
87
92
return MBEDTLS_ERR_SSL_WANT_WRITE ;
88
93
}
94
+
95
+ char errstr [256 ];
96
+ mbedtls_strerror (err , errstr , sizeof (errstr ));
97
+ ESP_LOGW (TAG , "sock_stream->write(): error %d: %s" , - err , errstr );
98
+
89
99
return - err ;
90
100
}
91
101
return out_sz ;
@@ -102,6 +112,11 @@ int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
102
112
if (mp_is_nonblocking_error (err )) {
103
113
return MBEDTLS_ERR_SSL_WANT_READ ;
104
114
}
115
+
116
+ char errstr [256 ];
117
+ mbedtls_strerror (err , errstr , sizeof (errstr ));
118
+ ESP_LOGW (TAG , "sock_stream->read(): error %d: %s" , - err , errstr );
119
+
105
120
return - err ;
106
121
}
107
122
return out_sz ;
@@ -139,8 +154,11 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
139
154
mbedtls_entropy_init (& o -> entropy );
140
155
ret = mbedtls_ctr_drbg_seed (& o -> ctr_drbg , mbedtls_entropy_func , & o -> entropy , NULL , 0 );
141
156
if (ret != 0 ) {
142
- printf ("ret=%d\n" , ret );
143
- assert (0 );
157
+ char errstr [256 ];
158
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
159
+ ESP_LOGW (TAG , "mbedtls_ctr_drbg_seed(): error %d: %s" , - ret , errstr );
160
+
161
+ mp_raise_OSError (MP_EIO );
144
162
}
145
163
146
164
bool sha2017_subdomain = false;
@@ -158,8 +176,11 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
158
176
if (sha2017_subdomain ) {
159
177
ret = mbedtls_x509_crt_parse_der (& o -> cacert , wildcard_sha2017_org , 856 );
160
178
if (ret < 0 ) {
161
- printf ("mbedtls_x509_crt_parse returned -0x%x\n\n" , - ret );
162
- assert (0 );
179
+ char errstr [256 ];
180
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
181
+ ESP_LOGW (TAG , "mbedtls_x509_crt_parse_der(): error %d: %s" , - ret , errstr );
182
+
183
+ mp_raise_OSError (MP_EIO );
163
184
}
164
185
}
165
186
@@ -168,7 +189,11 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
168
189
MBEDTLS_SSL_TRANSPORT_STREAM ,
169
190
MBEDTLS_SSL_PRESET_DEFAULT );
170
191
if (ret != 0 ) {
171
- assert (0 );
192
+ char errstr [256 ];
193
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
194
+ ESP_LOGW (TAG , "mbedtls_ssl_config_defaults(): error %d: %s" , - ret , errstr );
195
+
196
+ mp_raise_OSError (MP_EIO );
172
197
}
173
198
174
199
if (sha2017_subdomain ) {
@@ -182,14 +207,22 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
182
207
183
208
ret = mbedtls_ssl_setup (& o -> ssl , & o -> conf );
184
209
if (ret != 0 ) {
185
- assert (0 );
210
+ char errstr [256 ];
211
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
212
+ ESP_LOGW (TAG , "mbedtls_ssl_setup(): error %d: %s" , - ret , errstr );
213
+
214
+ mp_raise_OSError (MP_EIO );
186
215
}
187
216
188
217
if (args -> server_hostname .u_obj != mp_const_none ) {
189
218
const char * sni = mp_obj_str_get_str (args -> server_hostname .u_obj );
190
219
ret = mbedtls_ssl_set_hostname (& o -> ssl , sni );
191
220
if (ret != 0 ) {
192
- assert (0 );
221
+ char errstr [256 ];
222
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
223
+ ESP_LOGW (TAG , "mbedtls_ssl_set_hostname(): error %d: %s" , - ret , errstr );
224
+
225
+ mp_raise_OSError (MP_EIO );
193
226
}
194
227
}
195
228
@@ -203,25 +236,46 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
203
236
const byte * key = (const byte * )mp_obj_str_get_data (args -> key .u_obj , & key_len );
204
237
// len should include terminating null
205
238
ret = mbedtls_pk_parse_key (& o -> pkey , key , key_len + 1 , NULL , 0 );
206
- assert (ret == 0 );
239
+ if (ret != 0 ) {
240
+ char errstr [256 ];
241
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
242
+ ESP_LOGW (TAG , "mbedtls_pk_parse_key(): error %d: %s" , - ret , errstr );
243
+
244
+ mp_raise_OSError (MP_EIO );
245
+ }
207
246
208
247
size_t cert_len ;
209
248
const byte * cert = (const byte * )mp_obj_str_get_data (args -> cert .u_obj , & cert_len );
210
249
// len should include terminating null
211
250
ret = mbedtls_x509_crt_parse (& o -> cert , cert , cert_len + 1 );
212
- assert (ret == 0 );
251
+ if (ret != 0 ) {
252
+ char errstr [256 ];
253
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
254
+ ESP_LOGW (TAG , "mbedtls_x509_crt_parse(): error %d: %s" , - ret , errstr );
255
+
256
+ mp_raise_OSError (MP_EIO );
257
+ }
213
258
214
259
ret = mbedtls_ssl_conf_own_cert (& o -> conf , & o -> cert , & o -> pkey );
215
- assert (ret == 0 );
260
+ if (ret != 0 ) {
261
+ char errstr [256 ];
262
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
263
+ ESP_LOGW (TAG , "mbedtls_ssl_conf_own_cert(): error %d: %s" , - ret , errstr );
264
+
265
+ mp_raise_OSError (MP_EIO );
266
+ }
216
267
}
217
268
218
269
if (args -> server_side .u_bool ) {
219
- assert (0 );
270
+ ESP_LOGW (TAG , "args->server_side.u_bool set" );
271
+ mp_raise_OSError (MP_EIO );
220
272
} else {
221
273
while ((ret = mbedtls_ssl_handshake (& o -> ssl )) != 0 ) {
222
274
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) {
223
- //assert(0);
224
- printf ("mbedtls_ssl_handshake error: -%x\n" , - ret );
275
+ char errstr [256 ];
276
+ mbedtls_strerror (ret , errstr , sizeof (errstr ));
277
+ ESP_LOGW (TAG , "mbedtls_ssl_handshake(): error %d: %s" , - ret , errstr );
278
+
225
279
mp_raise_OSError (MP_EIO );
226
280
}
227
281
}
0 commit comments