3
3
*
4
4
* The MIT License (MIT)
5
5
*
6
- * Copyright (c) 2015 Paul Sokolovsky
6
+ * Copyright (c) 2015-2017 Paul Sokolovsky
7
7
*
8
8
* Permission is hereby granted, free of charge, to any person obtaining a copy
9
9
* of this software and associated documentation files (the "Software"), to deal
@@ -45,9 +45,14 @@ typedef struct _mp_obj_ssl_socket_t {
45
45
uint32_t bytes_left ;
46
46
} mp_obj_ssl_socket_t ;
47
47
48
+ struct ssl_args {
49
+ mp_arg_val_t server_side ;
50
+ mp_arg_val_t server_hostname ;
51
+ };
52
+
48
53
STATIC const mp_obj_type_t ussl_socket_type ;
49
54
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 ) {
51
56
mp_obj_ssl_socket_t * o = m_new_obj (mp_obj_ssl_socket_t );
52
57
o -> base .type = & ussl_socket_type ;
53
58
o -> buf = NULL ;
@@ -59,18 +64,30 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, bool server_side) {
59
64
mp_raise_OSError (MP_EINVAL );
60
65
}
61
66
62
- if (server_side ) {
67
+ if (args -> server_side . u_bool ) {
63
68
o -> ssl_sock = ssl_server_new (o -> ssl_ctx , (long )sock );
64
69
} 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
+ }
66
75
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 ) {
70
86
printf ("ssl_handshake_status: %d\n" , res );
71
87
ssl_display_error (res );
72
88
mp_raise_OSError (MP_EIO );
73
89
}
90
+
74
91
}
75
92
76
93
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_
171
188
// TODO: Implement more args
172
189
static const mp_arg_t allowed_args [] = {
173
190
{ 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 } },
174
192
};
175
193
176
194
// TODO: Check that sock implements stream protocol
177
195
mp_obj_t sock = pos_args [0 ];
178
196
179
- struct {
180
- mp_arg_val_t server_side ;
181
- } args ;
197
+ struct ssl_args args ;
182
198
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args ,
183
199
MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
184
200
185
- return MP_OBJ_FROM_PTR (socket_new (sock , args . server_side . u_bool ));
201
+ return MP_OBJ_FROM_PTR (socket_new (sock , & args ));
186
202
}
187
203
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mod_ssl_wrap_socket_obj , 1 , mod_ssl_wrap_socket );
188
204
0 commit comments