|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015 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 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | +#include <sys/types.h> |
| 30 | + |
| 31 | +#include "py/nlr.h" |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "py/stream.h" |
| 34 | + |
| 35 | +#if MICROPY_PY_USSL |
| 36 | + |
| 37 | +#include "ssl.h" |
| 38 | + |
| 39 | +typedef struct _mp_obj_ssl_socket_t { |
| 40 | + mp_obj_base_t base; |
| 41 | + mp_obj_t sock; |
| 42 | + SSL_CTX *ssl_ctx; |
| 43 | + SSL *ssl_sock; |
| 44 | + byte *buf; |
| 45 | + uint32_t bytes_left; |
| 46 | +} mp_obj_ssl_socket_t; |
| 47 | + |
| 48 | +STATIC const mp_obj_type_t ussl_socket_type; |
| 49 | + |
| 50 | +STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock) { |
| 51 | + mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t); |
| 52 | + o->base.type = &ussl_socket_type; |
| 53 | + o->buf = NULL; |
| 54 | + o->bytes_left = 0; |
| 55 | + o->sock = sock; |
| 56 | + |
| 57 | + uint32_t options = SSL_SERVER_VERIFY_LATER; |
| 58 | + if ((o->ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_CLNT_SESS)) == NULL) |
| 59 | + { |
| 60 | + fprintf(stderr, "Error: Client context is invalid\n"); |
| 61 | + assert(0); |
| 62 | + } |
| 63 | + |
| 64 | + o->ssl_sock = ssl_client_new(o->ssl_ctx, (long)sock, NULL, 0); |
| 65 | + |
| 66 | + int res; |
| 67 | + /* check the return status */ |
| 68 | + if ((res = ssl_handshake_status(o->ssl_sock)) != SSL_OK) |
| 69 | + { |
| 70 | + printf("ssl_handshake_status: %d\n", res); |
| 71 | + ssl_display_error(res); |
| 72 | + assert(0); |
| 73 | + } |
| 74 | + |
| 75 | + return o; |
| 76 | +} |
| 77 | + |
| 78 | +STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 79 | + (void)kind; |
| 80 | + mp_obj_ssl_socket_t *self = self_in; |
| 81 | + mp_printf(print, "<_SSLSocket %p>", self->ssl_sock); |
| 82 | +} |
| 83 | + |
| 84 | +STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
| 85 | + mp_obj_ssl_socket_t *o = o_in; |
| 86 | + |
| 87 | + while (o->bytes_left == 0) { |
| 88 | + mp_int_t r = ssl_read(o->ssl_sock, &o->buf); |
| 89 | + if (r < 0) { |
| 90 | + if (r == SSL_CLOSE_NOTIFY || r == SSL_ERROR_CONN_LOST) { |
| 91 | + // EOF |
| 92 | + return 0; |
| 93 | + } |
| 94 | + *errcode = r; |
| 95 | + return MP_STREAM_ERROR; |
| 96 | + } |
| 97 | + o->bytes_left = r; |
| 98 | + } |
| 99 | + |
| 100 | + if (size > o->bytes_left) { |
| 101 | + size = o->bytes_left; |
| 102 | + } |
| 103 | + memcpy(buf, o->buf, size); |
| 104 | + o->buf += size; |
| 105 | + o->bytes_left -= size; |
| 106 | + return size; |
| 107 | +} |
| 108 | + |
| 109 | +STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { |
| 110 | + mp_obj_ssl_socket_t *o = o_in; |
| 111 | + mp_int_t r = ssl_write(o->ssl_sock, buf, size); |
| 112 | + if (r < 0) { |
| 113 | + *errcode = r; |
| 114 | + return MP_STREAM_ERROR; |
| 115 | + } |
| 116 | + return r; |
| 117 | +} |
| 118 | + |
| 119 | +STATIC mp_obj_t socket_close(mp_obj_t self_in) { |
| 120 | + mp_obj_ssl_socket_t *self = self_in; |
| 121 | + ssl_free(self->ssl_sock); |
| 122 | + ssl_ctx_free(self->ssl_ctx); |
| 123 | + |
| 124 | + mp_obj_t dest[2]; |
| 125 | + mp_load_method(self->sock, MP_QSTR_close, dest); |
| 126 | + return mp_call_method_n_kw(0, 0, dest); |
| 127 | +} |
| 128 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close); |
| 129 | + |
| 130 | +STATIC const mp_map_elem_t ussl_socket_locals_dict_table[] = { |
| 131 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 132 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, |
| 133 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, |
| 134 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, |
| 135 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 136 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, |
| 137 | +}; |
| 138 | + |
| 139 | +STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table); |
| 140 | + |
| 141 | +STATIC const mp_stream_p_t ussl_socket_stream_p = { |
| 142 | + .read = socket_read, |
| 143 | + .write = socket_write, |
| 144 | +}; |
| 145 | + |
| 146 | +STATIC const mp_obj_type_t ussl_socket_type = { |
| 147 | + { &mp_type_type }, |
| 148 | + // Save on qstr's, reuse same as for module |
| 149 | + .name = MP_QSTR_ussl, |
| 150 | + .print = socket_print, |
| 151 | + .getiter = NULL, |
| 152 | + .iternext = NULL, |
| 153 | + .stream_p = &ussl_socket_stream_p, |
| 154 | + .locals_dict = (mp_obj_t)&ussl_socket_locals_dict, |
| 155 | +}; |
| 156 | + |
| 157 | +STATIC mp_obj_t mod_ssl_wrap_socket(mp_uint_t n_args, const mp_obj_t *args) { |
| 158 | + // TODO: Implement more args |
| 159 | + assert(n_args == 1); |
| 160 | + mp_obj_t sock = args[0]; |
| 161 | + // TODO: Check that sock implements stream protocol |
| 162 | + return socket_new(sock); |
| 163 | +} |
| 164 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ssl_wrap_socket_obj, 1, 6, mod_ssl_wrap_socket); |
| 165 | + |
| 166 | +STATIC const mp_map_elem_t mp_module_ssl_globals_table[] = { |
| 167 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ussl) }, |
| 168 | + { MP_OBJ_NEW_QSTR(MP_QSTR_wrap_socket), (mp_obj_t)&mod_ssl_wrap_socket_obj }, |
| 169 | +}; |
| 170 | + |
| 171 | +STATIC MP_DEFINE_CONST_DICT(mp_module_ssl_globals, mp_module_ssl_globals_table); |
| 172 | + |
| 173 | +const mp_obj_module_t mp_module_ussl = { |
| 174 | + .base = { &mp_type_module }, |
| 175 | + .name = MP_QSTR_ussl, |
| 176 | + .globals = (mp_obj_dict_t*)&mp_module_ssl_globals, |
| 177 | +}; |
| 178 | + |
| 179 | + |
| 180 | +// These functions might be split to stream_posix.c. They are referenced by |
| 181 | +// axtls os_port.h . |
| 182 | + |
| 183 | +int mp_stream_errno; |
| 184 | + |
| 185 | +ssize_t mp_stream_posix_write(void *sock_obj, const void *buf, size_t len) { |
| 186 | + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)sock_obj; |
| 187 | + mp_uint_t out_sz = o->type->stream_p->write(o, buf, len, &mp_stream_errno); |
| 188 | + if (out_sz == MP_STREAM_ERROR) { |
| 189 | + return -1; |
| 190 | + } else { |
| 191 | + return out_sz; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +ssize_t mp_stream_posix_read(void *sock_obj, void *buf, size_t len) { |
| 196 | + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)sock_obj; |
| 197 | + mp_uint_t out_sz = o->type->stream_p->read(o, buf, len, &mp_stream_errno); |
| 198 | + if (out_sz == MP_STREAM_ERROR) { |
| 199 | + return -1; |
| 200 | + } else { |
| 201 | + return out_sz; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +#endif // MICROPY_PY_USSL |
0 commit comments