| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef __SSL_SOCK_IMP_COMMON_H__ |
| 20 |
#define __SSL_SOCK_IMP_COMMON_H__ |
| 21 |
|
| 22 |
#include <pj/activesock.h> |
| 23 |
#include <pj/timer.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
enum ssl_state { |
| 29 |
SSL_STATE_NULL, |
| 30 |
SSL_STATE_HANDSHAKING, |
| 31 |
SSL_STATE_ESTABLISHED, |
| 32 |
SSL_STATE_ERROR |
| 33 |
}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
enum timer_id |
| 39 |
{ |
| 40 |
TIMER_NONE, |
| 41 |
TIMER_HANDSHAKE_TIMEOUT, |
| 42 |
TIMER_CLOSE |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
typedef struct read_data_t |
| 49 |
{ |
| 50 |
void *data; |
| 51 |
pj_size_t len; |
| 52 |
} read_data_t; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
typedef struct write_data_t { |
| 58 |
PJ_DECL_LIST_MEMBER(struct write_data_t); |
| 59 |
pj_ioqueue_op_key_t key; |
| 60 |
pj_size_t record_len; |
| 61 |
pj_ioqueue_op_key_t *app_key; |
| 62 |
pj_size_t plain_data_len; |
| 63 |
pj_size_t data_len; |
| 64 |
unsigned flags; |
| 65 |
union { |
| 66 |
char content[1]; |
| 67 |
const char *ptr; |
| 68 |
} data; |
| 69 |
} write_data_t; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
typedef struct ssl_send_op_t { |
| 84 |
PJ_DECL_LIST_MEMBER(struct ssl_send_op_t); |
| 85 |
pj_pool_t *pool; |
| 86 |
pj_ioqueue_op_key_t key; |
| 87 |
pj_ioqueue_op_key_t *app_key; |
| 88 |
pj_size_t plain_data_len; |
| 89 |
pj_size_t enc_len; |
| 90 |
pj_size_t enc_buf_cap; |
| 91 |
unsigned flags; |
| 92 |
char enc_data[1]; |
| 93 |
} ssl_send_op_t; |
| 94 |
|
| 95 |
|
| 96 |
typedef struct circ_buf_t { |
| 97 |
pj_ssl_sock_t *owner; |
| 98 |
pj_size_t cap; |
| 99 |
pj_size_t readp; |
| 100 |
pj_size_t writep; |
| 101 |
pj_size_t size; |
| 102 |
pj_uint8_t *buf; |
| 103 |
pj_pool_t *pool; |
| 104 |
} circ_buf_t; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
struct pj_ssl_sock_t |
| 110 |
{ |
| 111 |
pj_pool_t *pool; |
| 112 |
pj_pool_t *info_pool; |
| 113 |
|
| 114 |
|
| 115 |
pj_ssl_sock_t *parent; |
| 116 |
pj_ssl_sock_param param; |
| 117 |
pj_ssl_sock_param newsock_param; |
| 118 |
pj_ssl_cert_t *cert; |
| 119 |
|
| 120 |
pj_ssl_cert_info local_cert_info; |
| 121 |
pj_ssl_cert_info remote_cert_info; |
| 122 |
|
| 123 |
pj_bool_t is_server; |
| 124 |
enum ssl_state ssl_state; |
| 125 |
pj_ioqueue_op_key_t handshake_op_key; |
| 126 |
pj_ioqueue_op_key_t shutdown_op_key; |
| 127 |
pj_timer_entry timer; |
| 128 |
pj_status_t verify_status; |
| 129 |
pj_status_t handshake_status; |
| 130 |
|
| 131 |
pj_bool_t is_closing; |
| 132 |
unsigned long last_err; |
| 133 |
|
| 134 |
pj_sock_t sock; |
| 135 |
pj_activesock_t *asock; |
| 136 |
|
| 137 |
pj_sockaddr local_addr; |
| 138 |
pj_sockaddr rem_addr; |
| 139 |
int addr_len; |
| 140 |
|
| 141 |
pj_bool_t read_started; |
| 142 |
pj_size_t read_size; |
| 143 |
pj_uint32_t read_flags; |
| 144 |
void **asock_rbuf; |
| 145 |
read_data_t *ssock_rbuf; |
| 146 |
|
| 147 |
write_data_t write_pending; |
| 148 |
write_data_t write_pending_empty; |
| 149 |
pj_bool_t flushing_write_pend; |
| 150 |
ssl_send_op_t send_op_active; |
| 151 |
unsigned send_op_active_cnt; |
| 152 |
ssl_send_op_t send_op_free; |
| 153 |
unsigned send_op_free_cnt; |
| 154 |
pj_lock_t *write_mutex; |
| 155 |
pj_bool_t sending; |
| 156 |
|
| 157 |
ssl_send_op_t *send_op_inflight; |
| 158 |
|
| 159 |
circ_buf_t ssl_read_buf; |
| 160 |
pj_lock_t *ssl_read_buf_mutex; |
| 161 |
|
| 162 |
circ_buf_t ssl_write_buf; |
| 163 |
pj_lock_t *ssl_write_buf_mutex; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
struct pj_ssl_cert_t |
| 171 |
{ |
| 172 |
#if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL) |
| 173 |
|
| 174 |
pj_str_t CA_file; |
| 175 |
pj_str_t CA_path; |
| 176 |
pj_str_t cert_file; |
| 177 |
pj_str_t privkey_file; |
| 178 |
pj_str_t privkey_pass; |
| 179 |
|
| 180 |
|
| 181 |
pj_ssl_cert_buffer CA_buf; |
| 182 |
pj_ssl_cert_buffer cert_buf; |
| 183 |
pj_ssl_cert_buffer privkey_buf; |
| 184 |
|
| 185 |
|
| 186 |
pj_ssl_cert_direct direct; |
| 187 |
#else |
| 188 |
pj_ssl_cert_lookup_criteria criteria; |
| 189 |
#endif |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
static unsigned ssl_cipher_num; |
| 194 |
static struct ssl_ciphers_t { |
| 195 |
pj_ssl_cipher id; |
| 196 |
const char *name; |
| 197 |
} ssl_ciphers[PJ_SSL_SOCK_MAX_CIPHERS]; |
| 198 |
|
| 199 |
|
| 200 |
static unsigned ssl_curves_num; |
| 201 |
static struct ssl_curves_t { |
| 202 |
pj_ssl_curve id; |
| 203 |
const char *name; |
| 204 |
} ssl_curves[PJ_SSL_SOCK_MAX_CURVES]; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static pj_bool_t io_empty(pj_ssl_sock_t *ssock, circ_buf_t *cb); |
| 213 |
static pj_size_t io_size(pj_ssl_sock_t *ssock, circ_buf_t *cb); |
| 214 |
static void io_read(pj_ssl_sock_t *ssock, circ_buf_t *cb, |
| 215 |
pj_uint8_t *dst, pj_size_t len); |
| 216 |
static pj_status_t io_write(pj_ssl_sock_t *ssock, circ_buf_t *cb, |
| 217 |
const pj_uint8_t *src, pj_size_t len); |
| 218 |
|
| 219 |
static ssl_send_op_t* alloc_send_op(pj_ssl_sock_t *ssock, pj_size_t enc_len); |
| 220 |
static void free_send_op(pj_ssl_sock_t *ssock, ssl_send_op_t *op); |
| 221 |
static pj_status_t flush_delayed_send(pj_ssl_sock_t *ssock); |
| 222 |
|
| 223 |
#ifdef SSL_SOCK_IMP_USE_CIRC_BUF |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
static pj_status_t circ_init(pj_pool_factory *factory, |
| 231 |
circ_buf_t *cb, pj_size_t cap); |
| 232 |
static void circ_deinit(circ_buf_t *cb); |
| 233 |
static void circ_reset(circ_buf_t* cb); |
| 234 |
static pj_bool_t circ_empty(const circ_buf_t *cb); |
| 235 |
static pj_size_t circ_size(const circ_buf_t *cb); |
| 236 |
static void circ_read(circ_buf_t *cb, pj_uint8_t *dst, pj_size_t len); |
| 237 |
static void circ_read_cancel(circ_buf_t* cb, pj_size_t len); |
| 238 |
static pj_status_t circ_write(circ_buf_t *cb, |
| 239 |
const pj_uint8_t *src, pj_size_t len); |
| 240 |
|
| 241 |
inline static pj_bool_t io_empty(pj_ssl_sock_t *ssock, circ_buf_t *cb) |
| 242 |
{ |
| 243 |
PJ_UNUSED_ARG(ssock); |
| 244 |
return circ_empty(cb); |
| 245 |
} |
| 246 |
inline static pj_size_t io_size(pj_ssl_sock_t *ssock, circ_buf_t *cb) |
| 247 |
{ |
| 248 |
PJ_UNUSED_ARG(ssock); |
| 249 |
return circ_size(cb); |
| 250 |
} |
| 251 |
inline static void io_reset(pj_ssl_sock_t *ssock, circ_buf_t *cb) |
| 252 |
{ |
| 253 |
PJ_UNUSED_ARG(ssock); |
| 254 |
PJ_UNUSED_ARG(cb); |
| 255 |
} |
| 256 |
inline static void io_read(pj_ssl_sock_t *ssock, circ_buf_t *cb, |
| 257 |
pj_uint8_t *dst, pj_size_t len) |
| 258 |
{ |
| 259 |
PJ_UNUSED_ARG(ssock); |
| 260 |
circ_read(cb, dst, len); |
| 261 |
} |
| 262 |
inline static pj_status_t io_write(pj_ssl_sock_t *ssock, circ_buf_t *cb, |
| 263 |
const pj_uint8_t *src, pj_size_t len) |
| 264 |
{ |
| 265 |
PJ_UNUSED_ARG(ssock); |
| 266 |
return circ_write(cb, src, len); |
| 267 |
} |
| 268 |
|
| 269 |
#endif |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
static pj_ssl_sock_t *ssl_alloc(pj_pool_t *pool); |
| 280 |
|
| 281 |
static pj_status_t ssl_create(pj_ssl_sock_t *ssock); |
| 282 |
|
| 283 |
static void ssl_destroy(pj_ssl_sock_t *ssock); |
| 284 |
|
| 285 |
static void ssl_reset_sock_state(pj_ssl_sock_t *ssock); |
| 286 |
|
| 287 |
|
| 288 |
static void ssl_ciphers_populate(); |
| 289 |
static pj_ssl_cipher ssl_get_cipher(pj_ssl_sock_t *ssock); |
| 290 |
static void ssl_update_certs_info(pj_ssl_sock_t *ssock); |
| 291 |
#if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) |
| 292 |
static void ssl_free_cert(pj_ssl_cert_t *cert); |
| 293 |
#endif |
| 294 |
|
| 295 |
|
| 296 |
static void ssl_set_state(pj_ssl_sock_t *ssock, pj_bool_t is_server); |
| 297 |
static void ssl_set_peer_name(pj_ssl_sock_t *ssock); |
| 298 |
|
| 299 |
static pj_status_t ssl_do_handshake(pj_ssl_sock_t *ssock); |
| 300 |
static pj_status_t ssl_renegotiate(pj_ssl_sock_t *ssock); |
| 301 |
static pj_status_t ssl_read(pj_ssl_sock_t *ssock, void *data, int *size); |
| 302 |
static pj_status_t ssl_write(pj_ssl_sock_t *ssock, const void *data, |
| 303 |
pj_ssize_t size, int *nwritten); |
| 304 |
|
| 305 |
#ifdef SSL_SOCK_IMP_USE_OWN_NETWORK |
| 306 |
|
| 307 |
static void ssl_close_sockets(pj_ssl_sock_t *ssock); |
| 308 |
|
| 309 |
static pj_status_t network_send(pj_ssl_sock_t *ssock, |
| 310 |
pj_ioqueue_op_key_t *send_key, |
| 311 |
const void *data, |
| 312 |
pj_ssize_t *size, |
| 313 |
unsigned flags); |
| 314 |
static pj_status_t network_start_read(pj_ssl_sock_t *ssock, |
| 315 |
unsigned async_count, |
| 316 |
unsigned buff_size, |
| 317 |
void *readbuf[], |
| 318 |
pj_uint32_t flags); |
| 319 |
static pj_status_t network_start_accept(pj_ssl_sock_t *ssock, |
| 320 |
pj_pool_t *pool, |
| 321 |
const pj_sockaddr_t *localaddr, |
| 322 |
int addr_len, |
| 323 |
const pj_ssl_sock_param *newsock_param); |
| 324 |
static pj_status_t network_start_connect(pj_ssl_sock_t *ssock, |
| 325 |
pj_ssl_start_connect_param *connect_param); |
| 326 |
static pj_status_t network_setup_connection(pj_ssl_sock_t *ssock, |
| 327 |
void *connection); |
| 328 |
static pj_status_t network_get_localaddr(pj_ssl_sock_t *ssock, |
| 329 |
pj_sockaddr_t *addr, |
| 330 |
int *namelen); |
| 331 |
|
| 332 |
#endif |
| 333 |
|
| 334 |
#endif |