Debian Codegraph @

Xpjproject/pjlib/src/pj/ssl_sock_imp_common.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2019-2019 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 * SSL/TLS state enumeration.
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 * Internal timer types.
37 */
38 enum timer_id
39 {
40 TIMER_NONE,
41 TIMER_HANDSHAKE_TIMEOUT,
42 TIMER_CLOSE
43 };
44
45 /*
46 * Structure of SSL socket read buffer.
47 */
48 typedef struct read_data_t
49 {
50 void *data;
51 pj_size_t len;
52 } read_data_t;
53
54 /*
55 * Structure of SSL socket write data.
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 * Per-send operation data. Each op is a single contiguous allocation
73 * (header + embedded encrypted data buffer) from its own pool, so
74 * the pool can be released when the op is discarded from the free list.
75 * Replaces the old send_buf ring buffer + write_data_t for network sends.
76 */
77
78 /* PJ_SSL_SEND_OP_MIN_BUF_SIZE, PJ_SSL_SEND_OP_FREE_LIST_MAX, and
79 * PJ_SSL_SEND_OP_ACTIVE_MAX are defined in pj/config.h.
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; /* own pool, released on discard */
86 pj_ioqueue_op_key_t key; /* internal op_key for activesock */
87 pj_ioqueue_op_key_t *app_key; /* caller's op_key (for callback) */
88 pj_size_t plain_data_len;/* plaintext length for callback */
89 pj_size_t enc_len; /* actual encrypted data length */
90 pj_size_t enc_buf_cap; /* embedded buffer capacity */
91 unsigned flags; /* send flags */
92 char enc_data[1]; /* variable-length encrypted data */
93 } ssl_send_op_t;
94
95 /* Circular buffer object */
96 typedef struct circ_buf_t {
97 pj_ssl_sock_t *owner; /* owner of the circular buffer */
98 pj_size_t cap; /* maximum number of elements (must be power of 2) */
99 pj_size_t readp; /* index of oldest element */
100 pj_size_t writep; /* index at which to write new element */
101 pj_size_t size; /* number of elements */
102 pj_uint8_t *buf; /* data buffer */
103 pj_pool_t *pool; /* where new allocations will take place */
104 } circ_buf_t;
105
106 /*
107 * Secure socket structure definition.
108 */
109 struct pj_ssl_sock_t
110 {
111 pj_pool_t *pool;
112 pj_pool_t *info_pool; /* this is for certificate chain
113 * information allocation. Don't use for
114 * other purposes. */
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;/* list of pending write to ssl */
148 write_data_t write_pending_empty; /* cache for write_pending */
149 pj_bool_t flushing_write_pend; /* flag of flushing is ongoing*/
150 ssl_send_op_t send_op_active; /* send queue (pending + in-flight)*/
151 unsigned send_op_active_cnt;/* active queue count */
152 ssl_send_op_t send_op_free; /* free list for recycling */
153 unsigned send_op_free_cnt;/* free list count */
154 pj_lock_t *write_mutex; /* protect ssl_write_buf & send ops */
155 pj_bool_t sending; /* drain loop active or async send
156 * in flight (under write_mutex) */
157 ssl_send_op_t *send_op_inflight; /* op pending in ioqueue */
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 * Certificate/credential structure definition.
169 */
170 struct pj_ssl_cert_t
171 {
172 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
173 /* Certificate files. */
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 /* Certificate buffers. */
181 pj_ssl_cert_buffer CA_buf;
182 pj_ssl_cert_buffer cert_buf;
183 pj_ssl_cert_buffer privkey_buf;
184
185 /* Certificate direct (backend specific instances). */
186 pj_ssl_cert_direct direct;
187 #else
188 pj_ssl_cert_lookup_criteria criteria;
189 #endif
190 };
191
192 /* ssl available ciphers */
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 /* ssl available curves */
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 * I/O functions.
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 * Circular buffer functions.
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 * The below functions must be implemented by SSL backend.
275 *******************************************************************
276 */
277
278 /* Allocate SSL backend struct */
279 static pj_ssl_sock_t *ssl_alloc(pj_pool_t *pool);
280 /* Create and initialize new SSL context and instance */
281 static pj_status_t ssl_create(pj_ssl_sock_t *ssock);
282 /* Destroy SSL context and instance */
283 static void ssl_destroy(pj_ssl_sock_t *ssock);
284 /* Reset SSL socket state */
285 static void ssl_reset_sock_state(pj_ssl_sock_t *ssock);
286
287 /* Ciphers and certs */
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 /* SSL session functions */
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 /* __SSL_SOCK_IMP_COMMON_H__ */