modussl_axtls.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015-2017 Paul Sokolovsky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/stream.h"
  30. #if MICROPY_PY_USSL && MICROPY_SSL_AXTLS
  31. #include "ssl.h"
  32. typedef struct _mp_obj_ssl_socket_t {
  33. mp_obj_base_t base;
  34. mp_obj_t sock;
  35. SSL_CTX *ssl_ctx;
  36. SSL *ssl_sock;
  37. byte *buf;
  38. uint32_t bytes_left;
  39. } mp_obj_ssl_socket_t;
  40. struct ssl_args {
  41. mp_arg_val_t server_side;
  42. mp_arg_val_t server_hostname;
  43. };
  44. STATIC const mp_obj_type_t ussl_socket_type;
  45. STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
  46. #if MICROPY_PY_USSL_FINALISER
  47. mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
  48. #else
  49. mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
  50. #endif
  51. o->base.type = &ussl_socket_type;
  52. o->buf = NULL;
  53. o->bytes_left = 0;
  54. o->sock = sock;
  55. uint32_t options = SSL_SERVER_VERIFY_LATER;
  56. if ((o->ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_CLNT_SESS)) == NULL) {
  57. mp_raise_OSError(MP_EINVAL);
  58. }
  59. if (args->server_side.u_bool) {
  60. o->ssl_sock = ssl_server_new(o->ssl_ctx, (long)sock);
  61. } else {
  62. SSL_EXTENSIONS *ext = ssl_ext_new();
  63. if (args->server_hostname.u_obj != mp_const_none) {
  64. ext->host_name = (char*)mp_obj_str_get_str(args->server_hostname.u_obj);
  65. }
  66. o->ssl_sock = ssl_client_new(o->ssl_ctx, (long)sock, NULL, 0, ext);
  67. int res = ssl_handshake_status(o->ssl_sock);
  68. // Pointer to SSL_EXTENSIONS as being passed to ssl_client_new()
  69. // is saved in ssl_sock->extensions.
  70. // As of axTLS 2.1.3, extensions aren't used beyond the initial
  71. // handshake, and that's pretty much how it's expected to be. So
  72. // we allocate them on stack and reset the pointer after handshake.
  73. if (res != SSL_OK) {
  74. printf("ssl_handshake_status: %d\n", res);
  75. ssl_display_error(res);
  76. mp_raise_OSError(MP_EIO);
  77. }
  78. }
  79. return o;
  80. }
  81. STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  82. (void)kind;
  83. mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in);
  84. mp_printf(print, "<_SSLSocket %p>", self->ssl_sock);
  85. }
  86. STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  87. mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
  88. if (o->ssl_sock == NULL) {
  89. *errcode = EBADF;
  90. return MP_STREAM_ERROR;
  91. }
  92. while (o->bytes_left == 0) {
  93. mp_int_t r = ssl_read(o->ssl_sock, &o->buf);
  94. if (r == SSL_OK) {
  95. // SSL_OK from ssl_read() means "everything is ok, but there's
  96. // not user data yet. So, we just keep reading.
  97. continue;
  98. }
  99. if (r < 0) {
  100. if (r == SSL_CLOSE_NOTIFY || r == SSL_ERROR_CONN_LOST) {
  101. // EOF
  102. return 0;
  103. }
  104. *errcode = r;
  105. return MP_STREAM_ERROR;
  106. }
  107. o->bytes_left = r;
  108. }
  109. if (size > o->bytes_left) {
  110. size = o->bytes_left;
  111. }
  112. memcpy(buf, o->buf, size);
  113. o->buf += size;
  114. o->bytes_left -= size;
  115. return size;
  116. }
  117. STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
  118. mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
  119. if (o->ssl_sock == NULL) {
  120. *errcode = EBADF;
  121. return MP_STREAM_ERROR;
  122. }
  123. mp_int_t r = ssl_write(o->ssl_sock, buf, size);
  124. if (r < 0) {
  125. *errcode = r;
  126. return MP_STREAM_ERROR;
  127. }
  128. return r;
  129. }
  130. STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
  131. // Currently supports only blocking mode
  132. (void)self_in;
  133. if (!mp_obj_is_true(flag_in)) {
  134. mp_raise_NotImplementedError(NULL);
  135. }
  136. return mp_const_none;
  137. }
  138. STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
  139. STATIC mp_obj_t socket_close(mp_obj_t self_in) {
  140. mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in);
  141. if (self->ssl_sock != NULL) {
  142. ssl_free(self->ssl_sock);
  143. ssl_ctx_free(self->ssl_ctx);
  144. self->ssl_sock = NULL;
  145. return mp_stream_close(self->sock);
  146. }
  147. return mp_const_none;
  148. }
  149. STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
  150. STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
  151. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  152. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  153. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  154. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  155. { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
  156. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socket_close_obj) },
  157. #if MICROPY_PY_USSL_FINALISER
  158. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socket_close_obj) },
  159. #endif
  160. };
  161. STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table);
  162. STATIC const mp_stream_p_t ussl_socket_stream_p = {
  163. .read = socket_read,
  164. .write = socket_write,
  165. };
  166. STATIC const mp_obj_type_t ussl_socket_type = {
  167. { &mp_type_type },
  168. // Save on qstr's, reuse same as for module
  169. .name = MP_QSTR_ussl,
  170. .print = socket_print,
  171. .getiter = NULL,
  172. .iternext = NULL,
  173. .protocol = &ussl_socket_stream_p,
  174. .locals_dict = (void*)&ussl_socket_locals_dict,
  175. };
  176. STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  177. // TODO: Implement more args
  178. static const mp_arg_t allowed_args[] = {
  179. { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
  180. { MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  181. };
  182. // TODO: Check that sock implements stream protocol
  183. mp_obj_t sock = pos_args[0];
  184. struct ssl_args args;
  185. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
  186. MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
  187. return MP_OBJ_FROM_PTR(socket_new(sock, &args));
  188. }
  189. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 1, mod_ssl_wrap_socket);
  190. STATIC const mp_rom_map_elem_t mp_module_ssl_globals_table[] = {
  191. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ussl) },
  192. { MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&mod_ssl_wrap_socket_obj) },
  193. };
  194. STATIC MP_DEFINE_CONST_DICT(mp_module_ssl_globals, mp_module_ssl_globals_table);
  195. const mp_obj_module_t mp_module_ussl = {
  196. .base = { &mp_type_module },
  197. .globals = (mp_obj_dict_t*)&mp_module_ssl_globals,
  198. };
  199. #endif // MICROPY_PY_USSL