@@ -67,6 +67,7 @@ typedef struct _mp_obj_ssl_context_t {
6767 mbedtls_pk_context pkey ;
6868 int authmode ;
6969 int * ciphersuites ;
70+ mp_obj_t handler ;
7071} mp_obj_ssl_context_t ;
7172
7273// This corresponds to an SSLSocket object.
@@ -188,6 +189,16 @@ STATIC void ssl_check_async_handshake_failure(mp_obj_ssl_socket_t *sslsock, int
188189 }
189190}
190191
192+ STATIC int ssl_sock_cert_verify (void * ptr , mbedtls_x509_crt * crt , int depth , uint32_t * flags ) {
193+ mp_obj_ssl_context_t * o = ptr ;
194+ if (o -> handler == mp_const_none ) {
195+ return 0 ;
196+ }
197+ mp_obj_array_t cert ;
198+ mp_obj_memoryview_init (& cert , 'B' , 0 , crt -> raw .len , crt -> raw .p );
199+ return mp_obj_get_int (mp_call_function_2 (o -> handler , MP_OBJ_FROM_PTR (& cert ), MP_OBJ_NEW_SMALL_INT (depth )));
200+ }
201+
191202/******************************************************************************/
192203// SSLContext type.
193204
@@ -213,6 +224,7 @@ STATIC mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args
213224 mbedtls_x509_crt_init (& self -> cert );
214225 mbedtls_pk_init (& self -> pkey );
215226 self -> ciphersuites = NULL ;
227+ self -> handler = mp_const_none ;
216228
217229 #ifdef MBEDTLS_DEBUG_C
218230 // Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
@@ -243,6 +255,7 @@ STATIC mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args
243255 self -> authmode = MBEDTLS_SSL_VERIFY_NONE ;
244256 }
245257 mbedtls_ssl_conf_authmode (& self -> conf , self -> authmode );
258+ mbedtls_ssl_conf_verify (& self -> conf , & ssl_sock_cert_verify , self );
246259 mbedtls_ssl_conf_rng (& self -> conf , mbedtls_ctr_drbg_random , & self -> ctr_drbg );
247260 #ifdef MBEDTLS_DEBUG_C
248261 mbedtls_ssl_conf_dbg (& self -> conf , mbedtls_debug , NULL );
@@ -257,6 +270,8 @@ STATIC void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
257270 // Load attribute.
258271 if (attr == MP_QSTR_verify_mode ) {
259272 dest [0 ] = MP_OBJ_NEW_SMALL_INT (self -> authmode );
273+ } else if (attr == MP_QSTR_verify_callback ) {
274+ dest [0 ] = self -> handler ;
260275 } else {
261276 // Continue lookup in locals_dict.
262277 dest [1 ] = MP_OBJ_SENTINEL ;
@@ -267,6 +282,9 @@ STATIC void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
267282 self -> authmode = mp_obj_get_int (dest [1 ]);
268283 dest [0 ] = MP_OBJ_NULL ;
269284 mbedtls_ssl_conf_authmode (& self -> conf , self -> authmode );
285+ } else if (attr == MP_QSTR_verify_callback ) {
286+ dest [0 ] = MP_OBJ_NULL ;
287+ self -> handler = dest [1 ];
270288 }
271289 }
272290}
0 commit comments