modussl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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 <stdint.h>
  27. #include "simplelink.h"
  28. #include "py/mpconfig.h"
  29. #include "py/obj.h"
  30. #include "py/objstr.h"
  31. #include "py/runtime.h"
  32. #include "modnetwork.h"
  33. #include "modusocket.h"
  34. #include "mpexception.h"
  35. /******************************************************************************
  36. DEFINE CONSTANTS
  37. ******************************************************************************/
  38. #define SSL_CERT_NONE (0)
  39. #define SSL_CERT_OPTIONAL (1)
  40. #define SSL_CERT_REQUIRED (2)
  41. /******************************************************************************
  42. DEFINE TYPES
  43. ******************************************************************************/
  44. typedef struct _mp_obj_ssl_socket_t {
  45. mp_obj_base_t base;
  46. mod_network_socket_base_t sock_base;
  47. mp_obj_t o_sock;
  48. } mp_obj_ssl_socket_t;
  49. /******************************************************************************
  50. DECLARE PRIVATE DATA
  51. ******************************************************************************/
  52. STATIC const mp_obj_type_t ssl_socket_type;
  53. /******************************************************************************/
  54. // MicroPython bindings; SSL class
  55. // ssl sockets inherit from normal socket, so we take its
  56. // locals and stream methods
  57. STATIC const mp_obj_type_t ssl_socket_type = {
  58. { &mp_type_type },
  59. .name = MP_QSTR_ussl,
  60. .getiter = NULL,
  61. .iternext = NULL,
  62. .protocol = &socket_stream_p,
  63. .locals_dict = (mp_obj_t)&socket_locals_dict,
  64. };
  65. STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  66. STATIC const mp_arg_t allowed_args[] = {
  67. { MP_QSTR_sock, MP_ARG_REQUIRED | MP_ARG_OBJ, },
  68. { MP_QSTR_keyfile, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  69. { MP_QSTR_certfile, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  70. { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
  71. { MP_QSTR_cert_reqs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SSL_CERT_NONE} },
  72. { MP_QSTR_ssl_version, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SL_SO_SEC_METHOD_TLSV1} },
  73. { MP_QSTR_ca_certs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
  74. };
  75. // parse arguments
  76. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  77. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  78. // chech if ca validation is required
  79. if (args[4].u_int != SSL_CERT_NONE && args[5].u_obj == mp_const_none) {
  80. goto arg_error;
  81. }
  82. // retrieve the file paths (with an 6 byte offset in order to strip it from the '/flash' prefix)
  83. const char *keyfile = (args[1].u_obj == mp_const_none) ? NULL : &(mp_obj_str_get_str(args[1].u_obj)[6]);
  84. const char *certfile = (args[2].u_obj == mp_const_none) ? NULL : &(mp_obj_str_get_str(args[2].u_obj)[6]);
  85. const char *cafile = (args[6].u_obj == mp_const_none || args[4].u_int != SSL_CERT_REQUIRED) ?
  86. NULL : &(mp_obj_str_get_str(args[6].u_obj)[6]);
  87. // server side requires both certfile and keyfile
  88. if (args[3].u_bool && (!keyfile || !certfile)) {
  89. goto arg_error;
  90. }
  91. _i16 _errno;
  92. _i16 sd = ((mod_network_socket_obj_t *)args[0].u_obj)->sock_base.sd;
  93. // set the requested SSL method
  94. _u8 method = args[5].u_int;
  95. if ((_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method))) < 0) {
  96. goto socket_error;
  97. }
  98. if (keyfile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, keyfile, strlen(keyfile))) < 0) {
  99. goto socket_error;
  100. }
  101. if (certfile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, certfile, strlen(certfile))) < 0) {
  102. goto socket_error;
  103. }
  104. if (cafile && (_errno = sl_SetSockOpt(sd, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME, cafile, strlen(cafile))) < 0) {
  105. goto socket_error;
  106. }
  107. // create the ssl socket
  108. mp_obj_ssl_socket_t *ssl_sock = m_new_obj(mp_obj_ssl_socket_t);
  109. // ssl sockets inherit all properties from the original socket
  110. memcpy (&ssl_sock->sock_base, &((mod_network_socket_obj_t *)args[0].u_obj)->sock_base, sizeof(mod_network_socket_base_t));
  111. ssl_sock->base.type = &ssl_socket_type;
  112. ssl_sock->sock_base.cert_req = (args[4].u_int == SSL_CERT_REQUIRED) ? true : false;
  113. ssl_sock->o_sock = args[0].u_obj;
  114. return ssl_sock;
  115. socket_error:
  116. mp_raise_OSError(_errno);
  117. arg_error:
  118. mp_raise_ValueError(mpexception_value_invalid_arguments);
  119. }
  120. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 0, mod_ssl_wrap_socket);
  121. STATIC const mp_rom_map_elem_t mp_module_ussl_globals_table[] = {
  122. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ussl) },
  123. { MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&mod_ssl_wrap_socket_obj) },
  124. // class exceptions
  125. { MP_ROM_QSTR(MP_QSTR_SSLError), MP_ROM_PTR(&mp_type_OSError) },
  126. // class constants
  127. { MP_ROM_QSTR(MP_QSTR_CERT_NONE), MP_ROM_INT(SSL_CERT_NONE) },
  128. { MP_ROM_QSTR(MP_QSTR_CERT_OPTIONAL), MP_ROM_INT(SSL_CERT_OPTIONAL) },
  129. { MP_ROM_QSTR(MP_QSTR_CERT_REQUIRED), MP_ROM_INT(SSL_CERT_REQUIRED) },
  130. { MP_ROM_QSTR(MP_QSTR_PROTOCOL_SSLv3), MP_ROM_INT(SL_SO_SEC_METHOD_SSLV3) },
  131. { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLSv1), MP_ROM_INT(SL_SO_SEC_METHOD_TLSV1) },
  132. { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLSv1_1), MP_ROM_INT(SL_SO_SEC_METHOD_TLSV1_1) },
  133. { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLSv1_2), MP_ROM_INT(SL_SO_SEC_METHOD_TLSV1_2) },
  134. };
  135. STATIC MP_DEFINE_CONST_DICT(mp_module_ussl_globals, mp_module_ussl_globals_table);
  136. const mp_obj_module_t mp_module_ussl = {
  137. .base = { &mp_type_module },
  138. .globals = (mp_obj_dict_t*)&mp_module_ussl_globals,
  139. };