33 *
44 * The MIT License (MIT)
55 *
6- * Copyright (c) 2015 Paul Sokolovsky
6+ * Copyright (c) 2015-2017 Paul Sokolovsky
77 *
88 * Permission is hereby granted, free of charge, to any person obtaining a copy
99 * of this software and associated documentation files (the "Software"), to deal
@@ -45,9 +45,14 @@ typedef struct _mp_obj_ssl_socket_t {
4545 uint32_t bytes_left ;
4646} mp_obj_ssl_socket_t ;
4747
48+ struct ssl_args {
49+ mp_arg_val_t server_side ;
50+ mp_arg_val_t server_hostname ;
51+ };
52+
4853STATIC const mp_obj_type_t ussl_socket_type ;
4954
50- STATIC mp_obj_ssl_socket_t * socket_new (mp_obj_t sock , bool server_side ) {
55+ STATIC mp_obj_ssl_socket_t * socket_new (mp_obj_t sock , struct ssl_args * args ) {
5156 mp_obj_ssl_socket_t * o = m_new_obj (mp_obj_ssl_socket_t );
5257 o -> base .type = & ussl_socket_type ;
5358 o -> buf = NULL ;
@@ -59,18 +64,30 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, bool server_side) {
5964 mp_raise_OSError (MP_EINVAL );
6065 }
6166
62- if (server_side ) {
67+ if (args -> server_side . u_bool ) {
6368 o -> ssl_sock = ssl_server_new (o -> ssl_ctx , (long )sock );
6469 } else {
65- o -> ssl_sock = ssl_client_new (o -> ssl_ctx , (long )sock , NULL , 0 , NULL );
70+ SSL_EXTENSIONS * ext = ssl_ext_new ();
71+
72+ if (args -> server_hostname .u_obj != mp_const_none ) {
73+ ext -> host_name = (char * )mp_obj_str_get_str (args -> server_hostname .u_obj );
74+ }
6675
67- int res ;
68- /* check the return status */
69- if ((res = ssl_handshake_status (o -> ssl_sock )) != SSL_OK ) {
76+ o -> ssl_sock = ssl_client_new (o -> ssl_ctx , (long )sock , NULL , 0 , ext );
77+
78+ int res = ssl_handshake_status (o -> ssl_sock );
79+ // Pointer to SSL_EXTENSIONS as being passed to ssl_client_new()
80+ // is saved in ssl_sock->extensions.
81+ // As of axTLS 2.1.3, extensions aren't used beyond the initial
82+ // handshake, and that's pretty much how it's expected to be. So
83+ // we allocate them on stack and reset the pointer after handshake.
84+
85+ if (res != SSL_OK ) {
7086 printf ("ssl_handshake_status: %d\n" , res );
7187 ssl_display_error (res );
7288 mp_raise_OSError (MP_EIO );
7389 }
90+
7491 }
7592
7693 return o ;
@@ -171,18 +188,17 @@ STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_
171188 // TODO: Implement more args
172189 static const mp_arg_t allowed_args [] = {
173190 { MP_QSTR_server_side , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
191+ { MP_QSTR_server_hostname , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
174192 };
175193
176194 // TODO: Check that sock implements stream protocol
177195 mp_obj_t sock = pos_args [0 ];
178196
179- struct {
180- mp_arg_val_t server_side ;
181- } args ;
197+ struct ssl_args args ;
182198 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args ,
183199 MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
184200
185- return MP_OBJ_FROM_PTR (socket_new (sock , args . server_side . u_bool ));
201+ return MP_OBJ_FROM_PTR (socket_new (sock , & args ));
186202}
187203STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mod_ssl_wrap_socket_obj , 1 , mod_ssl_wrap_socket );
188204
0 commit comments