Debian Codegraph @

Xpjproject/pjlib/src/pj/ssl_sock_imp_common.c

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 #include <pj/ssl_sock.h>
19 #include <pj/assert.h>
20 #include <pj/errno.h>
21 #include <pj/log.h>
22 #include <pj/math.h>
23 #include <pj/os.h>
24 #include <pj/pool.h>
25 #include <pj/string.h>
26
27 #include "ssl_sock_imp_common.h"
28
29 /* Workaround for ticket #985 and #1930 */
30 #ifndef PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT
31 # define PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT 500
32 #endif
33
34 enum { MAX_BIND_RETRY = 100 };
35
36 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
37 static pj_bool_t asock_on_data_read (pj_activesock_t *asock,
38 void *data,
39 pj_size_t size,
40 pj_status_t status,
41 pj_size_t *remainder);
42
43 static pj_bool_t asock_on_data_sent (pj_activesock_t *asock,
44 pj_ioqueue_op_key_t *send_key,
45 pj_ssize_t sent);
46 #endif
47
48 #ifdef SSL_SOCK_IMP_USE_CIRC_BUF
49 /*
50 *******************************************************************
51 * Circular buffer functions.
52 *******************************************************************
53 */
54
55 static pj_size_t next_pow2(pj_size_t n)
56 {
57 /* Next 32-bit power of two */
58 pj_size_t power = 1;
59 while (power < n && power < 0x8000000) {
60 power <<= 1;
61 }
62 return power;
63 }
64
65 static pj_status_t circ_init(pj_pool_factory *factory,
66 circ_buf_t *cb, pj_size_t cap)
67 {
68 /* Round-up cap */
69 cap = next_pow2(cap);
70
71 cb->cap = cap;
72 cb->readp = 0;
73 cb->writep = 0;
74 cb->size = 0;
75
76 /* Initial pool holding the buffer elements */
77 cb->pool = pj_pool_create(factory, "tls-circ%p", cap, cap, NULL);
78 if (!cb->pool)
79 return PJ_ENOMEM;
80
81 /* Allocate circular buffer */
82 cb->buf = pj_pool_alloc(cb->pool, cap);
83 if (!cb->buf) {
84 pj_pool_secure_release(&cb->pool);
85 return PJ_ENOMEM;
86 }
87
88 return PJ_SUCCESS;
89 }
90
91 static void circ_reset(circ_buf_t* cb)
92 {
93 cb->readp = 0;
94 cb->writep = 0;
95 cb->size = 0;
96 }
97
98 static void circ_deinit(circ_buf_t *cb)
99 {
100 if (cb->pool) {
101 pj_pool_secure_release(&cb->pool);
102 cb->pool = NULL;
103 }
104 }
105
106 static pj_bool_t circ_empty(const circ_buf_t *cb)
107 {
108 return cb->size == 0;
109 }
110
111 static pj_size_t circ_size(const circ_buf_t *cb)
112 {
113 return cb->size;
114 }
115
116 static pj_size_t circ_avail(const circ_buf_t *cb)
117 {
118 return cb->cap - cb->size;
119 }
120
121 static void circ_read(circ_buf_t *cb, pj_uint8_t *dst, pj_size_t len)
122 {
123 pj_size_t size_after = cb->cap - cb->readp;
124 pj_size_t tbc = PJ_MIN(size_after, len);
125 pj_size_t rem = len - tbc;
126
127 pj_assert(cb->size >= len);
128
129 pj_memcpy(dst, cb->buf + cb->readp, tbc);
130 pj_memcpy(dst + tbc, cb->buf, rem);
131
132 cb->readp += len;
133 cb->readp &= (cb->cap - 1);
134
135 cb->size -= len;
136 }
137
138 /* Cancel previous read, partially or fully.
139 * Should be called in the same mutex block as circ_read().
140 */
141 static void circ_read_cancel(circ_buf_t* cb, pj_size_t len)
142 {
143 pj_assert(cb->cap - cb->size >= len);
144
145 if (cb->readp < len)
146 cb->readp = cb->cap - (len - cb->readp);
147 else
148 cb->readp -= len;
149
150 cb->size += len;
151 }
152
153 static pj_status_t circ_write(circ_buf_t *cb,
154 const pj_uint8_t *src, pj_size_t len)
155 {
156 /* Overflow condition: resize */
157 if (len > circ_avail(cb)) {
158 /* Minimum required capacity */
159 pj_size_t min_cap = len + cb->size;
160
161 /* Round-up minimum capacity */
162 min_cap = next_pow2(min_cap);
163
164 /* Create a new pool to hold a bigger buffer, using the same factory */
165 pj_pool_t *pool = pj_pool_create(cb->pool->factory, "tls-circ%p",
166 min_cap, min_cap, NULL);
167 if (!pool)
168 return PJ_ENOMEM;
169
170 /* Allocate our new buffer */
171 pj_uint8_t *buf = pj_pool_alloc(pool, min_cap);
172 if (!buf) {
173 pj_pool_release(pool);
174 return PJ_ENOMEM;
175 }
176
177 /* Save old size, which we shall restore after the next read */
178 pj_size_t old_size = cb->size;
179
180 /* Copy old data into beginning of new buffer */
181 circ_read(cb, buf, cb->size);
182
183 /* Restore old size now */
184 cb->size = old_size;
185
186 /* Release the previous pool */
187 pj_pool_secure_release(&cb->pool);
188
189 /* Update circular buffer members */
190 cb->pool = pool;
191 cb->buf = buf;
192 cb->readp = 0;
193 cb->writep = cb->size;
194 cb->cap = min_cap;
195 }
196
197 pj_size_t size_after = cb->cap - cb->writep;
198 pj_size_t tbc = PJ_MIN(size_after, len);
199 pj_size_t rem = len - tbc;
200
201 pj_memcpy(cb->buf + cb->writep, src, tbc);
202 pj_memcpy(cb->buf, src + tbc, rem);
203
204 cb->writep += len;
205 cb->writep &= (cb->cap - 1);
206
207 cb->size += len;
208
209 return PJ_SUCCESS;
210 }
211 #endif
212
213 /*
214 *******************************************************************
215 * Helper functions.
216 *******************************************************************
217 */
218
219 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
220
221 /* Check IP address version. */
222 static int get_ip_addr_ver(const pj_str_t *host)
223 {
224 pj_in_addr dummy;
225 pj_in6_addr dummy6;
226
227 /* Check for empty address */
228 if (host->slen == 0)
229 return 0;
230
231 /* First check if this is an IPv4 address */
232 if (pj_inet_pton(pj_AF_INET(), host, &dummy) == PJ_SUCCESS)
233 return 4;
234
235 /* Then check if this is an IPv6 address */
236 if (pj_inet_pton(pj_AF_INET6(), host, &dummy6) == PJ_SUCCESS)
237 return 6;
238
239 /* Not an IP address */
240 return 0;
241 }
242
243 /* Close sockets */
244 static void ssl_close_sockets(pj_ssl_sock_t *ssock)
245 {
246 pj_activesock_t *asock;
247 pj_sock_t sock;
248
249 /* This can happen when pj_ssl_sock_create() fails. */
250 if (!ssock->write_mutex)
251 return;
252
253 pj_lock_acquire(ssock->write_mutex);
254 asock = ssock->asock;
255 if (asock) {
256 // Don't set ssock->asock to NULL, as it may trigger assertion in
257 // send operation. This should be safe as active socket will simply
258 // return PJ_EINVALIDOP on any operation if it is already closed.
259 //ssock->asock = NULL;
260 ssock->sock = PJ_INVALID_SOCKET;
261 }
262 sock = ssock->sock;
263 if (sock != PJ_INVALID_SOCKET)
264 ssock->sock = PJ_INVALID_SOCKET;
265 pj_lock_release(ssock->write_mutex);
266
267 if (asock)
268 pj_activesock_close(asock);
269
270 if (sock != PJ_INVALID_SOCKET)
271 pj_sock_close(sock);
272 }
273 #endif
274
275 /* When handshake completed:
276 * - notify application
277 * - if handshake failed, reset SSL state
278 * - return PJ_FALSE when SSL socket instance is destroyed by application.
279 */
280 static pj_bool_t on_handshake_complete(pj_ssl_sock_t *ssock,
281 pj_status_t status)
282 {
283 pj_lock_acquire(ssock->write_mutex);
284 if (ssock->handshake_status != PJ_EUNKNOWN) {
285 pj_lock_release(ssock->write_mutex);
286 return (ssock->handshake_status == PJ_SUCCESS)? PJ_TRUE: PJ_FALSE;
287 }
288 ssock->handshake_status = status;
289 pj_lock_release(ssock->write_mutex);
290
291 /* Cancel handshake timer */
292 if (ssock->timer.id == TIMER_HANDSHAKE_TIMEOUT) {
293 pj_timer_heap_cancel(ssock->param.timer_heap, &ssock->timer);
294 ssock->timer.id = TIMER_NONE;
295 }
296
297 /* Update certificates info on successful handshake */
298 if (status == PJ_SUCCESS)
299 ssl_update_certs_info(ssock);
300
301 /* Accepting */
302 if (ssock->is_server) {
303 pj_bool_t ret = PJ_TRUE;
304
305 if (status != PJ_SUCCESS) {
306 /* Handshake failed in accepting, destroy our self silently. */
307
308 char buf1[PJ_INET6_ADDRSTRLEN+10];
309 char buf2[PJ_INET6_ADDRSTRLEN+10];
310
311 if (pj_sockaddr_has_addr(&ssock->local_addr))
312 pj_sockaddr_print(&ssock->local_addr, buf1, sizeof(buf1), 3);
313 else
314 pj_ansi_snprintf(buf1, sizeof(buf1), "(unknown)");
315
316 if (pj_sockaddr_has_addr(&ssock->rem_addr))
317 pj_sockaddr_print(&ssock->rem_addr, buf2, sizeof(buf2), 3);
318 else
319 pj_ansi_snprintf(buf2, sizeof(buf2), "(unknown)");
320
321 PJ_PERROR(3,(ssock->pool->obj_name, status,
322 "Handshake failed on %s in accepting %s", buf1, buf2));
323
324 if (ssock->param.cb.on_accept_complete2) {
325 (*ssock->param.cb.on_accept_complete2)
326 (ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr,
327 pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr),
328 status);
329 }
330
331 /* Decrement ref count of parent */
332 if (ssock->parent->param.grp_lock) {
333 pj_grp_lock_dec_ref(ssock->parent->param.grp_lock);
334 ssock->parent = NULL;
335 }
336
337 /* Originally, this is a workaround for ticket #985. However,
338 * a race condition may occur in multiple worker threads
339 * environment when we are destroying SSL objects while other
340 * threads are still accessing them.
341 * Please see ticket #1930 for more info.
342 */
343 #if 1 //(defined(PJ_WIN32) && PJ_WIN32!=0)||(defined(PJ_WIN64) && PJ_WIN64!=0)
344 if (ssock->param.timer_heap) {
345 pj_time_val interval = {0, PJ_SSL_SOCK_DELAYED_CLOSE_TIMEOUT};
346 pj_status_t status1;
347
348 ssock->ssl_state = SSL_STATE_NULL;
349 ssl_close_sockets(ssock);
350
351 if (ssock->timer.id != TIMER_NONE) {
352 pj_timer_heap_cancel(ssock->param.timer_heap,
353 &ssock->timer);
354 }
355 pj_time_val_normalize(&interval);
356 status1 = pj_timer_heap_schedule_w_grp_lock(
357 ssock->param.timer_heap,
358 &ssock->timer,
359 &interval,
360 TIMER_CLOSE,
361 ssock->param.grp_lock);
362 if (status1 != PJ_SUCCESS) {
363 PJ_PERROR(3,(ssock->pool->obj_name, status,
364 "Failed to schedule a delayed close. "
365 "Race condition may occur."));
366 ssock->timer.id = TIMER_NONE;
367 pj_ssl_sock_close(ssock);
368 }
369 } else {
370 pj_ssl_sock_close(ssock);
371 }
372 #else
373 {
374 pj_ssl_sock_close(ssock);
375 }
376 #endif
377
378 return PJ_FALSE;
379 }
380
381 /* Notify application the newly accepted SSL socket */
382 if (ssock->param.cb.on_accept_complete2) {
383 ret = (*ssock->param.cb.on_accept_complete2)
384 (ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr,
385 pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr),
386 status);
387 } else if (ssock->param.cb.on_accept_complete) {
388 ret = (*ssock->param.cb.on_accept_complete)
389 (ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr,
390 pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr));
391 }
392
393 /* Decrement ref count of parent and reset parent (we don't need it
394 * anymore, right?).
395 */
396 if (ssock->parent->param.grp_lock) {
397 pj_grp_lock_dec_ref(ssock->parent->param.grp_lock);
398 ssock->parent = NULL;
399 }
400
401 if (ret == PJ_FALSE)
402 return PJ_FALSE;
403 }
404
405 /* Connecting */
406 else {
407 /* On failure, reset SSL socket state first, as app may try to
408 * reconnect in the callback.
409 */
410 if (status != PJ_SUCCESS) {
411 char buf1[PJ_INET6_ADDRSTRLEN+10];
412 char buf2[PJ_INET6_ADDRSTRLEN+10];
413
414 if (pj_sockaddr_has_addr(&ssock->local_addr))
415 pj_sockaddr_print(&ssock->local_addr, buf1, sizeof(buf1), 3);
416 else
417 pj_ansi_snprintf(buf1, sizeof(buf1), "(unknown)");
418
419 if (pj_sockaddr_has_addr(&ssock->rem_addr))
420 pj_sockaddr_print(&ssock->rem_addr, buf2, sizeof(buf2), 3);
421 else
422 pj_ansi_snprintf(buf2, sizeof(buf2), "(unknown)");
423
424 PJ_PERROR(3,(ssock->pool->obj_name, status,
425 "Handshake failed on %s in connecting to %s",
426 buf1, buf2));
427
428 /* Server disconnected us, possibly due to SSL nego failure */
429 ssl_reset_sock_state(ssock);
430 }
431 if (ssock->param.cb.on_connect_complete) {
432 pj_bool_t ret;
433 ret = (*ssock->param.cb.on_connect_complete)(ssock, status);
434 if (ret == PJ_FALSE)
435 return PJ_FALSE;
436 }
437 }
438
439 return PJ_TRUE;
440 }
441
442 static ssl_send_op_t* alloc_send_op(pj_ssl_sock_t *ssock, pj_size_t enc_len)
443 {
444 ssl_send_op_t *op;
445 pj_pool_t *op_pool;
446 pj_size_t alloc_len;
447
448 /* Apply minimum buffer size for better reuse */
449 if (enc_len < PJ_SSL_SEND_OP_MIN_BUF_SIZE)
450 enc_len = PJ_SSL_SEND_OP_MIN_BUF_SIZE;
451
452 /* Scan free list for first op with sufficient capacity */
453 if (!pj_list_empty(&ssock->send_op_free)) {
454 ssl_send_op_t *p = ssock->send_op_free.next;
455 while (p != &ssock->send_op_free) {
456 if (p->enc_buf_cap >= enc_len) {
457 pj_list_erase(p);
458 ssock->send_op_free_cnt--;
459 return p;
460 }
461 p = p->next;
462 }
463 /* No suitable op found, allocate new below */
464 }
465
466 /* Allocate new op + embedded buffer from its own pool.
467 * Each op has its own pool so it can be truly freed when discarded
468 * from the free list (cap exceeded).
469 */
470 alloc_len = sizeof(ssl_send_op_t) - 1 + enc_len;
471 op_pool = pj_pool_create(ssock->pool->factory, "ssl_sop",
472 alloc_len + 256, 0, NULL);
473 if (!op_pool)
474 return NULL;
475
476 op = (ssl_send_op_t *)pj_pool_alloc(op_pool, alloc_len);
477 if (!op) {
478 pj_pool_release(op_pool);
479 return NULL;
480 }
481 pj_bzero(op, sizeof(ssl_send_op_t) - 1);
482 op->pool = op_pool;
483 op->enc_buf_cap = enc_len;
484 return op;
485 }
486
487 static void free_send_op(pj_ssl_sock_t *ssock, ssl_send_op_t *op)
488 {
489 pj_list_erase(op);
490 if (ssock->send_op_active_cnt > 0)
491 ssock->send_op_active_cnt--;
492
493 if (ssock->send_op_free_cnt < PJ_SSL_SEND_OP_FREE_LIST_MAX) {
494 pj_list_push_back(&ssock->send_op_free, op);
495 ssock->send_op_free_cnt++;
496 } else {
497 /* Free list full, release the pool to truly free memory */
498 pj_pool_secure_release(&op->pool);
499 }
500 }
501
502 /* Forward declarations */
503 static pj_status_t ssl_do_handshake_and_flush(pj_ssl_sock_t *ssock);
504 static pj_bool_t on_handshake_complete(pj_ssl_sock_t *ssock,
505 pj_status_t status);
506
507 /* Enqueue encrypted data from ssl_write_buf into a send op.
508 * MUST be called with write_mutex held.
509 * On return, *p_drain is PJ_TRUE if caller must call drain_send_queue().
510 */
511 static pj_status_t enqueue_ssl_write_buf(pj_ssl_sock_t *ssock,
512 pj_ioqueue_op_key_t *send_key,
513 pj_size_t orig_len,
514 unsigned flags,
515 pj_bool_t *p_drain)
516 {
517 pj_ssize_t len;
518 ssl_send_op_t *op;
519
520 *p_drain = PJ_FALSE;
521
522 if (io_empty(ssock, &ssock->ssl_write_buf))
523 return PJ_SUCCESS;
524
525 len = io_size(ssock, &ssock->ssl_write_buf);
526 if (len == 0)
527 return PJ_SUCCESS;
528
529 op = alloc_send_op(ssock, len);
530 if (!op)
531 return PJ_ENOMEM;
532
533 pj_ioqueue_op_key_init(&op->key, sizeof(pj_ioqueue_op_key_t));
534 op->key.user_data = op;
535 op->app_key = send_key;
536 op->plain_data_len = orig_len;
537 op->enc_len = len;
538 op->flags = flags;
539 io_read(ssock, &ssock->ssl_write_buf, (pj_uint8_t *)op->enc_data, len);
540
541 pj_list_push_back(&ssock->send_op_active, op);
542 ssock->send_op_active_cnt++;
543
544 if (ssock->sending) {
545 /* Another thread/callback is draining. Just queue. */
546 return PJ_EPENDING;
547 }
548
549 ssock->sending = PJ_TRUE;
550 *p_drain = PJ_TRUE;
551 return PJ_EPENDING;
552 }
553
554 /* Drain the send_op_active queue. Called WITHOUT holding write_mutex.
555 * Caller must have set ssock->sending = PJ_TRUE.
556 *
557 * suppress_first_cb: when PJ_TRUE, the first synchronously completed
558 * op does NOT get its callback fired — the caller (flush_ssl_write_buf)
559 * returns the status to the app directly. When PJ_FALSE (called from
560 * ssock_on_data_sent), all sync completions fire callbacks because
561 * their callers already received PJ_EPENDING.
562 */
563 static pj_status_t drain_send_queue(pj_ssl_sock_t *ssock,
564 pj_bool_t suppress_first_cb)
565 {
566 pj_status_t first_status = PJ_SUCCESS;
567 pj_bool_t is_first = suppress_first_cb;
568
569 for (;;) {
570 ssl_send_op_t *op;
571 pj_ioqueue_op_key_t *app_key;
572 pj_ssize_t plain_data_len;
573 pj_ssize_t len;
574 pj_status_t status;
575
576 /* Pop the front of the queue — remove BEFORE sending so that
577 * ssock_on_data_sent → drain_send_queue won't double-send.
578 * Set send_op_inflight BEFORE releasing the lock so that
579 * ssl_on_destroy can find it if the socket closes between
580 * the send and the async callback.
581 */
582 pj_lock_acquire(ssock->write_mutex);
583 if (pj_list_empty(&ssock->send_op_active)) {
584 ssock->sending = PJ_FALSE;
585 pj_lock_release(ssock->write_mutex);
586 break;
587 }
588 op = ssock->send_op_active.next;
589 pj_list_erase(op);
590 pj_assert(ssock->send_op_inflight == NULL);
591 ssock->send_op_inflight = op;
592 pj_lock_release(ssock->write_mutex);
593
594 /* Send WITHOUT holding write_mutex — no deadlock possible */
595 len = op->enc_len;
596 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
597 status = network_send(ssock, &op->key, op->enc_data, &len,
598 op->flags);
599 #else
600 if (ssock->param.sock_type == pj_SOCK_STREAM()) {
601 status = pj_activesock_send(ssock->asock, &op->key,
602 op->enc_data, &len, op->flags);
603 } else {
604 status = pj_activesock_sendto(ssock->asock, &op->key,
605 op->enc_data, &len, op->flags,
606 (pj_sockaddr_t*)&ssock->rem_addr,
607 ssock->addr_len);
608 }
609 #endif
610
611 if (status == PJ_EPENDING) {
612 /* Async — ssock_on_data_sent will resume draining.
613 * send_op_inflight is already set above under write_mutex.
614 */
615 if (is_first)
616 first_status = PJ_EPENDING;
617 break;
618 }
619
620 /* Completed synchronously — clear inflight, free op */
621 app_key = op->app_key;
622 plain_data_len = (pj_ssize_t)op->plain_data_len;
623
624 pj_lock_acquire(ssock->write_mutex);
625 ssock->send_op_inflight = NULL;
626 free_send_op(ssock, op);
627 pj_lock_release(ssock->write_mutex);
628
629 if (status != PJ_SUCCESS) {
630 /* Send error — stop draining */
631 pj_lock_acquire(ssock->write_mutex);
632 ssock->sending = PJ_FALSE;
633 pj_lock_release(ssock->write_mutex);
634
635 if (is_first) {
636 first_status = status;
637 } else if (app_key != &ssock->handshake_op_key &&
638 app_key != &ssock->shutdown_op_key &&
639 ssock->param.cb.on_data_sent)
640 {
641 /* Non-first op's caller got PJ_EPENDING — must fire
642 * an error callback since the op was already freed.
643 */
644 pj_bool_t ret;
645 ret = (*ssock->param.cb.on_data_sent)(ssock, app_key,
646 -(pj_ssize_t)status);
647 if (!ret) {
648 return first_status; /* destroyed */
649 }
650 }
651 break;
652 }
653
654 if (is_first) {
655 first_status = PJ_SUCCESS;
656 is_first = PJ_FALSE;
657 continue; /* first op: caller returns status directly */
658 }
659
660 /* Non-first op completed sync: its caller got PJ_EPENDING,
661 * so we must fire the callback here.
662 */
663 if (ssock->ssl_state == SSL_STATE_HANDSHAKING) {
664 pj_status_t hs;
665 hs = ssl_do_handshake_and_flush(ssock);
666 if (hs != PJ_EPENDING) {
667 if (!on_handshake_complete(ssock, hs))
668 return first_status;
669 }
670 } else if (app_key != &ssock->handshake_op_key &&
671 app_key != &ssock->shutdown_op_key)
672 {
673 if (ssock->param.cb.on_data_sent) {
674 pj_bool_t ret;
675 ret = (*ssock->param.cb.on_data_sent)(ssock, app_key,
676 plain_data_len);
677 if (!ret)
678 return first_status; /* destroyed */
679 }
680 }
681
682 /* After processing a handshake op, try to flush delayed sends.
683 * During renegotiation, sends are delayed in write_pending.
684 * When the last handshake op completes (renego done), the
685 * delayed sends can now proceed.
686 */
687 if (!pj_list_empty(&ssock->write_pending)) {
688 flush_delayed_send(ssock);
689 }
690 }
691
692 return first_status;
693 }
694
695 /* Flush SSL write buffer to network socket. */
696 static pj_status_t flush_ssl_write_buf(pj_ssl_sock_t *ssock,
697 pj_ioqueue_op_key_t *send_key,
698 pj_size_t orig_len, unsigned flags)
699 {
700 pj_bool_t should_drain;
701 pj_status_t status;
702
703 pj_lock_acquire(ssock->write_mutex);
704 status = enqueue_ssl_write_buf(ssock, send_key, orig_len, flags,
705 &should_drain);
706 pj_lock_release(ssock->write_mutex);
707
708 if (should_drain)
709 status = drain_send_queue(ssock, PJ_TRUE);
710
711 return status;
712 }
713
714 /* ssl_do_handshake_and_flush: common wrapper that calls the backend's
715 * ssl_do_handshake() then flushes any handshake data from ssl_write_buf.
716 * This unifies error handling across all SSL backends.
717 */
718 static pj_status_t ssl_do_handshake_and_flush(pj_ssl_sock_t *ssock)
719 {
720 pj_status_t status;
721 pj_status_t flush_status;
722
723 status = ssl_do_handshake(ssock);
724
725 /* Flush any handshake data produced by the SSL library.
726 * Must flush unconditionally — even on handshake failure, the SSL
727 * library may have queued error alert records that should be sent
728 * to the peer.
729 */
730 flush_status = flush_ssl_write_buf(ssock,
731 &ssock->handshake_op_key, 0, 0);
732 if (status == PJ_SUCCESS || status == PJ_EPENDING) {
733 /* Only override status with flush error when handshake itself
734 * was still in progress. Don't mask handshake failure.
735 */
736 if (flush_status != PJ_SUCCESS && flush_status != PJ_EPENDING) {
737 status = flush_status;
738 }
739 }
740
741 return status;
742 }
743
744 static void on_timer(pj_timer_heap_t *th, struct pj_timer_entry *te)
745 {
746 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)te->user_data;
747 int timer_id = te->id;
748
749 te->id = TIMER_NONE;
750
751 PJ_UNUSED_ARG(th);
752
753 switch (timer_id) {
754 case TIMER_HANDSHAKE_TIMEOUT:
755 PJ_LOG(1,(ssock->pool->obj_name, "SSL timeout after %ld.%lds",
756 ssock->param.timeout.sec, ssock->param.timeout.msec));
757
758 on_handshake_complete(ssock, PJ_ETIMEDOUT);
759 break;
760 case TIMER_CLOSE:
761 pj_ssl_sock_close(ssock);
762 break;
763 default:
764 pj_assert(!"Unknown timer");
765 break;
766 }
767 }
768
769 /* Fire error callbacks for pending sends that will never complete.
770 * Called from pj_ssl_sock_close() so callbacks fire synchronously
771 * while the app's resources are still valid. Must be called AFTER
772 * ssl_reset_sock_state (sockets closed, no more async completions).
773 */
774 static void cancel_pending_sends(pj_ssl_sock_t *ssock)
775 {
776 /* In-flight send_op — removed from send_op_active but pending in
777 * ioqueue. Socket close cancelled it without callback.
778 */
779 if (ssock->send_op_inflight) {
780 ssl_send_op_t *op = ssock->send_op_inflight;
781 pj_ioqueue_op_key_t *app_key = op->app_key;
782
783 ssock->send_op_inflight = NULL;
784
785 if (app_key != &ssock->handshake_op_key &&
786 app_key != &ssock->shutdown_op_key &&
787 ssock->param.cb.on_data_sent)
788 {
789 (*ssock->param.cb.on_data_sent)(ssock, app_key,
790 -PJ_ECANCELLED);
791 }
792
793 pj_lock_acquire(ssock->write_mutex);
794 free_send_op(ssock, op);
795 pj_lock_release(ssock->write_mutex);
796 }
797
798 /* Active send_ops — encrypted data queued but not yet sent */
799 while (!pj_list_empty(&ssock->send_op_active)) {
800 ssl_send_op_t *op = ssock->send_op_active.next;
801 pj_ioqueue_op_key_t *app_key = op->app_key;
802
803 pj_list_erase(op);
804
805 if (app_key != &ssock->handshake_op_key &&
806 app_key != &ssock->shutdown_op_key &&
807 ssock->param.cb.on_data_sent)
808 {
809 (*ssock->param.cb.on_data_sent)(ssock, app_key,
810 -PJ_ECANCELLED);
811 }
812
813 pj_lock_acquire(ssock->write_mutex);
814 free_send_op(ssock, op);
815 pj_lock_release(ssock->write_mutex);
816 }
817
818 /* Delayed sends from renegotiation — never encrypted */
819 while (!pj_list_empty(&ssock->write_pending)) {
820 write_data_t *wp = ssock->write_pending.next;
821 pj_ioqueue_op_key_t *app_key = wp->app_key;
822
823 pj_list_erase(wp);
824
825 if (app_key != &ssock->handshake_op_key &&
826 app_key != &ssock->shutdown_op_key &&
827 ssock->param.cb.on_data_sent)
828 {
829 (*ssock->param.cb.on_data_sent)(ssock, app_key,
830 -PJ_ECANCELLED);
831 }
832 }
833 }
834
835 static void ssl_on_destroy(void *arg)
836 {
837 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)arg;
838
839 ssl_destroy(ssock);
840
841 /* Defensive: free any remaining ops without callbacks.
842 * Normally cancel_pending_sends() in pj_ssl_sock_close() already
843 * drained these, but handle the case where destroy fires without
844 * a prior close (e.g., grp_lock ref dropped externally).
845 */
846 if (ssock->send_op_inflight) {
847 pj_pool_secure_release(&ssock->send_op_inflight->pool);
848 ssock->send_op_inflight = NULL;
849 }
850 while (!pj_list_empty(&ssock->send_op_active)) {
851 ssl_send_op_t *op = ssock->send_op_active.next;
852 pj_list_erase(op);
853 pj_pool_secure_release(&op->pool);
854 }
855
856 /* Release all send op pools (each op has its own pool) */
857 while (!pj_list_empty(&ssock->send_op_free)) {
858 ssl_send_op_t *op = ssock->send_op_free.next;
859 pj_list_erase(op);
860 pj_pool_secure_release(&op->pool);
861 }
862 ssock->send_op_free_cnt = 0;
863
864 if (ssock->ssl_read_buf_mutex) {
865 pj_lock_destroy(ssock->ssl_read_buf_mutex);
866 ssock->ssl_read_buf_mutex = NULL;
867 }
868
869 if (ssock->ssl_write_buf_mutex) {
870 pj_lock_destroy(ssock->ssl_write_buf_mutex);
871 ssock->ssl_write_buf_mutex = NULL;
872 ssock->write_mutex = NULL;
873 }
874
875 /* Secure release pool, i.e: all memory blocks will be zeroed first */
876 pj_pool_secure_release(&ssock->info_pool);
877 pj_pool_secure_release(&ssock->pool);
878 }
879
880
881 /*
882 *******************************************************************
883 * Network callbacks.
884 *******************************************************************
885 */
886
887 /*
888 * Get the offset of pointer to read-buffer of SSL socket from read-buffer
889 * of active socket. Note that both SSL socket and active socket employ
890 * different but correlated read-buffers (as much as async_cnt for each),
891 * and to make it easier/faster to find corresponding SSL socket's read-buffer
892 * from known active socket's read-buffer, the pointer of corresponding
893 * SSL socket's read-buffer is stored right after the end of active socket's
894 * read-buffer.
895 */
896 #define OFFSET_OF_READ_DATA_PTR(ssock, asock_rbuf) \
897 (read_data_t**) \
898 ((pj_int8_t*)(asock_rbuf) + \
899 ssock->param.read_buffer_size)
900
901 static pj_bool_t ssock_on_data_read (pj_ssl_sock_t *ssock,
902 void *data,
903 pj_size_t size,
904 pj_status_t status,
905 pj_size_t *remainder)
906 {
907 if (status != PJ_SUCCESS)
908 goto on_error;
909
910 if (data && size > 0) {
911 pj_status_t status_;
912
913 /* Consume the whole data */
914 if (ssock->ssl_read_buf_mutex)
915 pj_lock_acquire(ssock->ssl_read_buf_mutex);
916 status_ = io_write(ssock,&ssock->ssl_read_buf, data, size);
917 if (ssock->ssl_read_buf_mutex)
918 pj_lock_release(ssock->ssl_read_buf_mutex);
919 if (status_ != PJ_SUCCESS) {
920 status = status_;
921 goto on_error;
922 }
923 }
924
925 /* Check if SSL handshake hasn't finished yet */
926 if (ssock->ssl_state == SSL_STATE_HANDSHAKING) {
927 pj_bool_t ret = PJ_TRUE;
928
929 if (status == PJ_SUCCESS)
930 status = ssl_do_handshake_and_flush(ssock);
931
932 /* Not pending is either success or failed */
933 if (status != PJ_EPENDING)
934 ret = on_handshake_complete(ssock, status);
935
936 return ret;
937 }
938
939 /* See if there is any decrypted data for the application */
940 if (data && ssock->read_started) {
941 do {
942 read_data_t *buf = *(OFFSET_OF_READ_DATA_PTR(ssock, data));
943 void *data_ = (pj_int8_t*)buf->data + buf->len;
944 int size_ = (int)(ssock->read_size - buf->len);
945 pj_status_t status_;
946
947 status_ = ssl_read(ssock, data_, &size_);
948
949 if (size_ > 0) {
950 if (ssock->param.cb.on_data_read) {
951 pj_bool_t ret;
952 pj_size_t remainder_ = 0;
953
954 buf->len += size_;
955
956 ret = (*ssock->param.cb.on_data_read)(ssock, buf->data,
957 buf->len, status,
958 &remainder_);
959 if (!ret) {
960 /* We've been destroyed */
961 return PJ_FALSE;
962 }
963
964 /* Application may have left some data to be consumed
965 * later.
966 */
967 buf->len = remainder_;
968 }
969
970 /* Active socket signalled connection closed/error, this has
971 * been signalled to the application along with any remaining
972 * buffer. So, let's just reset SSL socket now.
973 */
974 /*
975 // This has been handled in on_error
976 if (status != PJ_SUCCESS) {
977 ssl_reset_sock_state(ssock);
978 return PJ_FALSE;
979 }
980 */
981
982 } else if (status_ == PJ_SUCCESS) {
983 break;
984 } else if (status_ == PJ_ETRYAGAIN) {
985 /* SSL_read may have produced handshake data (e.g.
986 * ServerHello during renegotiation). Flush it before
987 * calling ssl_do_handshake which may not produce more.
988 */
989 flush_ssl_write_buf(ssock, &ssock->handshake_op_key,
990 0, 0);
991 status = ssl_do_handshake_and_flush(ssock);
992 if (status == PJ_SUCCESS) {
993 /* Renegotiation completed */
994
995 /* Update certificates */
996 ssl_update_certs_info(ssock);
997
998 // Ticket #1573: Don't hold mutex while calling
999 // PJLIB socket send().
1000 //pj_lock_acquire(ssock->write_mutex);
1001 status = flush_delayed_send(ssock);
1002 //pj_lock_release(ssock->write_mutex);
1003
1004 /* If flushing is ongoing, treat it as success */
1005 if (status == PJ_EBUSY)
1006 status = PJ_SUCCESS;
1007
1008 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
1009 PJ_PERROR(1,(ssock->pool->obj_name, status,
1010 "Failed to flush delayed send"));
1011 goto on_error;
1012 }
1013
1014 /* If renego has been completed, continue reading data */
1015 if (status == PJ_SUCCESS)
1016 continue;
1017
1018 } else if (status != PJ_EPENDING) {
1019 PJ_PERROR(1,(ssock->pool->obj_name, status,
1020 "Renegotiation failed"));
1021 goto on_error;
1022 }
1023
1024 break;
1025 } else {
1026 /* Error */
1027 status = status_;
1028 goto on_error;
1029 }
1030
1031 } while (1);
1032 }
1033
1034 /* SSL_read may have generated protocol-level responses in the
1035 * write buffer (e.g. renegotiation rejection alert, session
1036 * ticket, etc.) that must be sent to the peer. Flush them.
1037 */
1038 flush_ssl_write_buf(ssock, &ssock->handshake_op_key, 0, 0);
1039
1040 /* Flush delayed sends that may have been queued during
1041 * renegotiation. OpenSSL handles renegotiation transparently
1042 * inside SSL_read, so by the time ssl_read returns application
1043 * data again, the renegotiation has completed and delayed sends
1044 * can proceed.
1045 */
1046 if (!pj_list_empty(&ssock->write_pending)) {
1047 flush_delayed_send(ssock);
1048 }
1049
1050 return PJ_TRUE;
1051
1052 on_error:
1053 if (ssock->ssl_state == SSL_STATE_HANDSHAKING)
1054 return on_handshake_complete(ssock, status);
1055
1056 if (ssock->read_started && ssock->param.cb.on_data_read) {
1057 pj_bool_t ret;
1058 ret = (*ssock->param.cb.on_data_read)(ssock, NULL, 0, status,
1059 remainder);
1060 if (!ret) {
1061 /* We've been destroyed */
1062 return PJ_FALSE;
1063 }
1064 }
1065
1066 ssl_reset_sock_state(ssock);
1067 return PJ_FALSE;
1068 }
1069
1070 static pj_bool_t ssock_on_data_sent (pj_ssl_sock_t *ssock,
1071 pj_ioqueue_op_key_t *send_key,
1072 pj_ssize_t sent)
1073 {
1074 ssl_send_op_t *op;
1075 pj_ioqueue_op_key_t *app_key;
1076 pj_ssize_t sent_len;
1077
1078 /* Skip late callbacks arriving after close (e.g., IOCP cancelled
1079 * completions, or ioqueue drain on epoll/select). All pending ops
1080 * are handled by cancel_pending_sends() in pj_ssl_sock_close().
1081 */
1082 if (ssock->is_closing)
1083 return PJ_FALSE;
1084
1085 op = (ssl_send_op_t *)send_key->user_data;
1086 app_key = op->app_key;
1087 sent_len = (sent > 0) ? (pj_ssize_t)op->plain_data_len : sent;
1088
1089 /* Clear in-flight tracking and free the completed send op */
1090 pj_lock_acquire(ssock->write_mutex);
1091 ssock->send_op_inflight = NULL;
1092 free_send_op(ssock, op);
1093 pj_lock_release(ssock->write_mutex);
1094 op = NULL;
1095
1096 if (ssock->ssl_state == SSL_STATE_HANDSHAKING) {
1097 /* Handshaking — continue handshake */
1098 pj_status_t status;
1099
1100 status = ssl_do_handshake_and_flush(ssock);
1101 if (status != PJ_EPENDING) {
1102 pj_bool_t ret = on_handshake_complete(ssock, status);
1103 if (!ret)
1104 return PJ_FALSE;
1105 /* Fall through to drain remaining queue */
1106 }
1107
1108 } else if (app_key != &ssock->handshake_op_key &&
1109 app_key != &ssock->shutdown_op_key)
1110 {
1111 /* Application data — notify application */
1112 if (ssock->param.cb.on_data_sent) {
1113 pj_bool_t ret;
1114 ret = (*ssock->param.cb.on_data_sent)(ssock, app_key,
1115 sent_len);
1116 if (!ret) {
1117 /* We've been destroyed */
1118 return PJ_FALSE;
1119 }
1120 }
1121 }
1122
1123 /* Drain write_pending (delayed sends from renegotiation) */
1124 if (!pj_list_empty(&ssock->write_pending)) {
1125 flush_delayed_send(ssock);
1126 }
1127
1128 /* Continue draining the send queue — ssock->sending is still
1129 * PJ_TRUE from the original drain_send_queue call. Don't
1130 * suppress callbacks: all remaining ops' callers got PJ_EPENDING.
1131 */
1132 drain_send_queue(ssock, PJ_FALSE);
1133
1134 return PJ_TRUE;
1135 }
1136
1137 static pj_status_t get_localaddr(pj_ssl_sock_t *ssock,
1138 pj_sockaddr_t *addr,
1139 int *namelen)
1140 {
1141 PJ_UNUSED_ARG(addr);
1142 PJ_UNUSED_ARG(namelen);
1143
1144 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
1145 return network_get_localaddr(ssock, &ssock->local_addr,
1146 &ssock->addr_len);
1147 #else
1148 return pj_sock_getsockname(ssock->sock, &ssock->local_addr,
1149 &ssock->addr_len);
1150 #endif
1151 }
1152
1153
1154 static pj_bool_t ssock_on_accept_complete (pj_ssl_sock_t *ssock_parent,
1155 pj_sock_t newsock,
1156 void *newconn,
1157 const pj_sockaddr_t *src_addr,
1158 int src_addr_len,
1159 pj_status_t accept_status)
1160 {
1161 pj_ssl_sock_t *ssock = NULL;
1162 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
1163 pj_activesock_cb asock_cb;
1164 pj_activesock_cfg asock_cfg;
1165 #endif
1166 unsigned i;
1167 pj_status_t status;
1168
1169 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
1170 PJ_UNUSED_ARG(newconn);
1171 #endif
1172
1173 /* Set close-on-exec flag */
1174 if (ssock_parent->newsock_param.sock_cloexec)
1175 pj_set_cloexec_flag((int)newsock);
1176
1177 if (accept_status != PJ_SUCCESS) {
1178 if (ssock_parent->param.cb.on_accept_complete2) {
1179 (*ssock_parent->param.cb.on_accept_complete2)(ssock_parent, NULL,
1180 src_addr,
1181 src_addr_len,
1182 accept_status);
1183 }
1184 return PJ_TRUE;
1185 }
1186
1187 /* Create new SSL socket instance */
1188 status = pj_ssl_sock_create(ssock_parent->pool,
1189 &ssock_parent->newsock_param, &ssock);
1190 if (status != PJ_SUCCESS)
1191 goto on_return;
1192
1193 /* Set parent and add ref count (avoid parent destroy during handshake) */
1194 ssock->parent = ssock_parent;
1195 if (ssock->parent->param.grp_lock)
1196 pj_grp_lock_add_ref(ssock->parent->param.grp_lock);
1197
1198 /* Update new SSL socket attributes */
1199 ssock->sock = newsock;
1200 ssock->is_server = PJ_TRUE;
1201 if (ssock_parent->cert) {
1202 status = pj_ssl_sock_set_certificate(ssock, ssock->pool,
1203 ssock_parent->cert);
1204 if (status != PJ_SUCCESS)
1205 goto on_return;
1206 }
1207
1208 /* Set local address */
1209 ssock->addr_len = src_addr_len;
1210 pj_sockaddr_cp(&ssock->local_addr, &ssock_parent->local_addr);
1211
1212 /* Set remote address */
1213 pj_sockaddr_cp(&ssock->rem_addr, src_addr);
1214
1215 /* Create SSL context */
1216 status = ssl_create(ssock);
1217 if (status != PJ_SUCCESS)
1218 goto on_return;
1219
1220 /* Set peer name */
1221 ssl_set_peer_name(ssock);
1222
1223 /* Prepare read buffer */
1224 ssock->asock_rbuf = (void**)pj_pool_calloc(ssock->pool,
1225 ssock->param.async_cnt,
1226 sizeof(void*));
1227 if (!ssock->asock_rbuf) {
1228 status = PJ_ENOMEM;
1229 goto on_return;
1230 }
1231
1232 for (i = 0; i<ssock->param.async_cnt; ++i) {
1233 ssock->asock_rbuf[i] = (void*) pj_pool_alloc(
1234 ssock->pool,
1235 ssock->param.read_buffer_size +
1236 sizeof(read_data_t*));
1237 if (!ssock->asock_rbuf[i]) {
1238 status = PJ_ENOMEM;
1239 goto on_return;
1240 }
1241 }
1242
1243 /* If listener socket has group lock, automatically create group lock
1244 * for the new socket.
1245 */
1246 if (ssock_parent->param.grp_lock) {
1247 pj_grp_lock_t *glock;
1248
1249 status = pj_grp_lock_create(ssock->pool, NULL, &glock);
1250 if (status != PJ_SUCCESS)
1251 goto on_return;
1252
1253 pj_grp_lock_add_ref(glock);
1254 ssock->param.grp_lock = glock;
1255 pj_grp_lock_add_handler(ssock->param.grp_lock, ssock->pool, ssock,
1256 ssl_on_destroy);
1257 }
1258
1259 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
1260 status = network_setup_connection(ssock, newconn);
1261 if (status != PJ_SUCCESS)
1262 goto on_return;
1263
1264 #else
1265 /* Apply QoS, if specified */
1266 status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type,
1267 &ssock->param.qos_params, 1,
1268 ssock->pool->obj_name, NULL);
1269 if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error)
1270 goto on_return;
1271
1272 /* Apply socket options, if specified */
1273 if (ssock->param.sockopt_params.cnt) {
1274 status = pj_sock_setsockopt_params(ssock->sock,
1275 &ssock->param.sockopt_params);
1276 if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error)
1277 goto on_return;
1278 }
1279
1280 /* Create active socket */
1281 pj_activesock_cfg_default(&asock_cfg);
1282 asock_cfg.grp_lock = ssock->param.grp_lock;
1283 asock_cfg.async_cnt = ssock->param.async_cnt;
1284 asock_cfg.concurrency = ssock->param.concurrency;
1285 asock_cfg.whole_data = PJ_TRUE;
1286
1287 pj_bzero(&asock_cb, sizeof(asock_cb));
1288 asock_cb.on_data_read = asock_on_data_read;
1289 asock_cb.on_data_sent = asock_on_data_sent;
1290
1291 status = pj_activesock_create(ssock->pool,
1292 ssock->sock,
1293 ssock->param.sock_type,
1294 &asock_cfg,
1295 ssock->param.ioqueue,
1296 &asock_cb,
1297 ssock,
1298 &ssock->asock);
1299
1300 if (status != PJ_SUCCESS)
1301 goto on_return;
1302
1303 /* Start read */
1304 status = pj_activesock_start_read2(ssock->asock, ssock->pool,
1305 (unsigned)ssock->param.read_buffer_size,
1306 ssock->asock_rbuf,
1307 PJ_IOQUEUE_ALWAYS_ASYNC);
1308 if (status != PJ_SUCCESS)
1309 goto on_return;
1310 #endif
1311
1312 /* Update local address */
1313 status = get_localaddr(ssock, &ssock->local_addr, &ssock->addr_len);
1314 if (status != PJ_SUCCESS) {
1315 /* This fails on few envs, e.g: win IOCP, just tolerate this and
1316 * use parent local address instead.
1317 */
1318 pj_sockaddr_cp(&ssock->local_addr, &ssock_parent->local_addr);
1319 }
1320
1321 /* Start handshake timer */
1322 if (ssock->param.timer_heap && (ssock->param.timeout.sec != 0 ||
1323 ssock->param.timeout.msec != 0))
1324 {
1325 pj_assert(ssock->timer.id == TIMER_NONE);
1326 status = pj_timer_heap_schedule_w_grp_lock(ssock->param.timer_heap,
1327 &ssock->timer,
1328 &ssock->param.timeout,
1329 TIMER_HANDSHAKE_TIMEOUT,
1330 ssock->param.grp_lock);
1331 if (status != PJ_SUCCESS) {
1332 ssock->timer.id = TIMER_NONE;
1333 }
1334 }
1335
1336 /* Start SSL handshake */
1337 /* Use write_mutex (not ssl_read_buf_mutex) to serialise the
1338 * ssl_state transition and the first ssl_do_handshake() call
1339 * against a concurrent ssock_on_data_read() that could fire on
1340 * the newly-registered ioqueue key. write_mutex is the correct
1341 * outer lock here because backends that touch shared SSL session
1342 * state acquire write_mutex internally, and the consistent lock
1343 * order everywhere is: write_mutex -> ssl_read_buf_mutex.
1344 */
1345 pj_lock_acquire(ssock->write_mutex);
1346 ssock->ssl_state = SSL_STATE_HANDSHAKING;
1347 ssl_set_state(ssock, PJ_TRUE);
1348 status = ssl_do_handshake_and_flush(ssock);
1349 pj_lock_release(ssock->write_mutex);
1350
1351 on_return:
1352 if (ssock && status != PJ_EPENDING) {
1353 on_handshake_complete(ssock, status);
1354 }
1355
1356 /* Must return PJ_TRUE whatever happened, as we must continue listening */
1357 return PJ_TRUE;
1358 }
1359
1360 static pj_bool_t ssock_on_connect_complete (pj_ssl_sock_t *ssock,
1361 pj_status_t status)
1362 {
1363 unsigned i;
1364
1365 if (status != PJ_SUCCESS)
1366 goto on_return;
1367
1368 /* Update local address */
1369 ssock->addr_len = sizeof(pj_sockaddr);
1370 status = get_localaddr(ssock, &ssock->local_addr, &ssock->addr_len);
1371 if (status != PJ_SUCCESS)
1372 goto on_return;
1373
1374 /* Create SSL context */
1375 status = ssl_create(ssock);
1376 if (status != PJ_SUCCESS)
1377 goto on_return;
1378
1379 /* Prepare read buffer */
1380 ssock->asock_rbuf = (void**)pj_pool_calloc(ssock->pool,
1381 ssock->param.async_cnt,
1382 sizeof(void*));
1383 if (!ssock->asock_rbuf) {
1384 status = PJ_ENOMEM;
1385 goto on_return;
1386 }
1387
1388 for (i = 0; i<ssock->param.async_cnt; ++i) {
1389 ssock->asock_rbuf[i] = (void*) pj_pool_alloc(
1390 ssock->pool,
1391 ssock->param.read_buffer_size +
1392 sizeof(read_data_t*));
1393 if (!ssock->asock_rbuf[i]) {
1394 status = PJ_ENOMEM;
1395 goto on_return;
1396 }
1397 }
1398
1399 /* Start read */
1400 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
1401 status = network_start_read(ssock, ssock->param.async_cnt,
1402 (unsigned)ssock->param.read_buffer_size,
1403 ssock->asock_rbuf, 0);
1404 #else
1405 status = pj_activesock_start_read2(ssock->asock, ssock->pool,
1406 (unsigned)ssock->param.read_buffer_size,
1407 ssock->asock_rbuf,
1408 PJ_IOQUEUE_ALWAYS_ASYNC);
1409 #endif
1410 if (status != PJ_SUCCESS)
1411 goto on_return;
1412
1413 /* Set peer name */
1414 ssl_set_peer_name(ssock);
1415
1416 /* Start SSL handshake */
1417 ssock->ssl_state = SSL_STATE_HANDSHAKING;
1418 ssl_set_state(ssock, PJ_FALSE);
1419
1420 status = ssl_do_handshake_and_flush(ssock);
1421 if (status != PJ_EPENDING)
1422 goto on_return;
1423
1424 return PJ_TRUE;
1425
1426 on_return:
1427 return on_handshake_complete(ssock, status);
1428 }
1429
1430 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
1431 static pj_bool_t asock_on_data_read (pj_activesock_t *asock,
1432 void *data,
1433 pj_size_t size,
1434 pj_status_t status,
1435 pj_size_t *remainder)
1436 {
1437 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)
1438 pj_activesock_get_user_data(asock);
1439
1440 return ssock_on_data_read(ssock, data, size, status, remainder);
1441 }
1442
1443 static pj_bool_t asock_on_data_sent (pj_activesock_t *asock,
1444 pj_ioqueue_op_key_t *send_key,
1445 pj_ssize_t sent)
1446 {
1447 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)
1448 pj_activesock_get_user_data(asock);
1449
1450 return ssock_on_data_sent(ssock, send_key, sent);
1451 }
1452
1453 static pj_bool_t asock_on_accept_complete2(pj_activesock_t *asock,
1454 pj_sock_t newsock,
1455 const pj_sockaddr_t *src_addr,
1456 int src_addr_len,
1457 pj_status_t status)
1458 {
1459 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)
1460 pj_activesock_get_user_data(asock);
1461
1462 return ssock_on_accept_complete(ssock, newsock, NULL,
1463 src_addr, src_addr_len, status);
1464 }
1465
1466 static pj_bool_t asock_on_connect_complete (pj_activesock_t *asock,
1467 pj_status_t status)
1468 {
1469 pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)
1470 pj_activesock_get_user_data(asock);
1471
1472 return ssock_on_connect_complete(ssock, status);
1473 }
1474 #endif
1475
1476 /*
1477 *******************************************************************
1478 * API
1479 *******************************************************************
1480 */
1481
1482 /* Get available ciphers. */
1483 PJ_DEF(pj_status_t) pj_ssl_cipher_get_availables(pj_ssl_cipher ciphers[],
1484 unsigned *cipher_num)
1485 {
1486 unsigned i;
1487
1488 PJ_ASSERT_RETURN(ciphers && cipher_num, PJ_EINVAL);
1489
1490 ssl_ciphers_populate();
1491
1492 if (ssl_cipher_num == 0) {
1493 *cipher_num = 0;
1494 return PJ_ENOTFOUND;
1495 }
1496
1497 *cipher_num = PJ_MIN(*cipher_num, ssl_cipher_num);
1498
1499 for (i = 0; i < *cipher_num; ++i)
1500 ciphers[i] = ssl_ciphers[i].id;
1501
1502 return PJ_SUCCESS;
1503 }
1504
1505 /* Get cipher name string */
1506 PJ_DEF(const char*) pj_ssl_cipher_name(pj_ssl_cipher cipher)
1507 {
1508 unsigned i;
1509
1510 ssl_ciphers_populate();
1511
1512 for (i = 0; i < ssl_cipher_num; ++i) {
1513 if (cipher == ssl_ciphers[i].id)
1514 return ssl_ciphers[i].name;
1515 }
1516
1517 return NULL;
1518 }
1519
1520 /* Get cipher identifier */
1521 PJ_DEF(pj_ssl_cipher) pj_ssl_cipher_id(const char *cipher_name)
1522 {
1523 unsigned i;
1524
1525 ssl_ciphers_populate();
1526
1527 for (i = 0; i < ssl_cipher_num; ++i) {
1528 if (!pj_ansi_stricmp(ssl_ciphers[i].name, cipher_name))
1529 return ssl_ciphers[i].id;
1530 }
1531
1532 return PJ_TLS_UNKNOWN_CIPHER;
1533 }
1534
1535 /* Check if the specified cipher is supported by SSL/TLS backend. */
1536 PJ_DEF(pj_bool_t) pj_ssl_cipher_is_supported(pj_ssl_cipher cipher)
1537 {
1538 unsigned i;
1539
1540 ssl_ciphers_populate();
1541
1542 for (i = 0; i < ssl_cipher_num; ++i) {
1543 if (cipher == ssl_ciphers[i].id)
1544 return PJ_TRUE;
1545 }
1546
1547 return PJ_FALSE;
1548 }
1549
1550 /* Get available curves. */
1551 PJ_DEF(pj_status_t) pj_ssl_curve_get_availables(pj_ssl_curve curves[],
1552 unsigned *curve_num)
1553 {
1554 unsigned i;
1555
1556 PJ_ASSERT_RETURN(curves && curve_num, PJ_EINVAL);
1557
1558 ssl_ciphers_populate();
1559
1560 if (ssl_curves_num == 0) {
1561 *curve_num = 0;
1562 return PJ_ENOTFOUND;
1563 }
1564
1565 *curve_num = PJ_MIN(*curve_num, ssl_curves_num);
1566
1567 for (i = 0; i < *curve_num; ++i)
1568 curves[i] = ssl_curves[i].id;
1569
1570 return PJ_SUCCESS;
1571 }
1572
1573 /* Get curve name string. */
1574 PJ_DEF(const char*) pj_ssl_curve_name(pj_ssl_curve curve)
1575 {
1576 unsigned i;
1577
1578 ssl_ciphers_populate();
1579
1580 for (i = 0; i < ssl_curves_num; ++i) {
1581 if (curve == ssl_curves[i].id)
1582 return ssl_curves[i].name;
1583 }
1584
1585 return NULL;
1586 }
1587
1588 /* Get curve ID from curve name string. */
1589 PJ_DEF(pj_ssl_curve) pj_ssl_curve_id(const char *curve_name)
1590 {
1591 unsigned i;
1592
1593 ssl_ciphers_populate();
1594
1595 for (i = 0; i < ssl_curves_num; ++i) {
1596 if (ssl_curves[i].name &&
1597 !pj_ansi_stricmp(ssl_curves[i].name, curve_name))
1598 {
1599 return ssl_curves[i].id;
1600 }
1601 }
1602
1603 return PJ_TLS_UNKNOWN_CURVE;
1604 }
1605
1606 /* Check if the specified curve is supported by SSL/TLS backend. */
1607 PJ_DEF(pj_bool_t) pj_ssl_curve_is_supported(pj_ssl_curve curve)
1608 {
1609 unsigned i;
1610
1611 ssl_ciphers_populate();
1612
1613 for (i = 0; i < ssl_curves_num; ++i) {
1614 if (curve == ssl_curves[i].id)
1615 return PJ_TRUE;
1616 }
1617
1618 return PJ_FALSE;
1619 }
1620
1621 /*
1622 * Create SSL socket instance.
1623 */
1624 PJ_DEF(pj_status_t) pj_ssl_sock_create (pj_pool_t *pool,
1625 const pj_ssl_sock_param *param,
1626 pj_ssl_sock_t **p_ssock)
1627 {
1628 pj_ssl_sock_t *ssock;
1629 pj_status_t status;
1630 pj_pool_t *info_pool;
1631
1632 PJ_ASSERT_RETURN(pool && param && p_ssock, PJ_EINVAL);
1633 PJ_ASSERT_RETURN((param->sock_type & 0xF) == pj_SOCK_STREAM(), PJ_ENOTSUP);
1634
1635 info_pool = pj_pool_create(pool->factory, "ssl_chain%p", 512, 512, NULL);
1636 pool = pj_pool_create(pool->factory, "ssl%p", 512, 512, NULL);
1637
1638 /* Create secure socket */
1639 ssock = ssl_alloc(pool);
1640 if (!ssock)
1641 return PJ_ENOMEM;
1642 ssock->pool = pool;
1643 ssock->info_pool = info_pool;
1644 ssock->sock = PJ_INVALID_SOCKET;
1645 ssock->ssl_state = SSL_STATE_NULL;
1646 ssock->ssl_read_buf.owner = ssock;
1647 ssock->ssl_write_buf.owner = ssock;
1648 ssock->handshake_status = PJ_EUNKNOWN;
1649 pj_list_init(&ssock->write_pending);
1650 pj_list_init(&ssock->write_pending_empty);
1651 pj_list_init(&ssock->send_op_active);
1652 pj_list_init(&ssock->send_op_free);
1653 pj_timer_entry_init(&ssock->timer, 0, ssock, &on_timer);
1654 pj_ioqueue_op_key_init(&ssock->handshake_op_key,
1655 sizeof(pj_ioqueue_op_key_t));
1656 pj_ioqueue_op_key_init(&ssock->shutdown_op_key,
1657 sizeof(pj_ioqueue_op_key_t));
1658
1659 /* Create secure socket mutex */
1660 status = pj_lock_create_recursive_mutex(pool, pool->obj_name,
1661 &ssock->ssl_write_buf_mutex);
1662 ssock->write_mutex = ssock->ssl_write_buf_mutex;
1663 if (status != PJ_SUCCESS)
1664 return status;
1665
1666 /* Create input circular buffer mutex */
1667 status = pj_lock_create_recursive_mutex(pool, pool->obj_name,
1668 &ssock->ssl_read_buf_mutex);
1669 if (status != PJ_SUCCESS)
1670 return status;
1671
1672 /* Create active socket send mutex */
1673 ssock->sending = PJ_FALSE;
1674
1675 /* Init secure socket param */
1676 pj_ssl_sock_param_copy(pool, &ssock->param, param);
1677
1678 if (ssock->param.grp_lock) {
1679 pj_grp_lock_add_ref(ssock->param.grp_lock);
1680 pj_grp_lock_add_handler(ssock->param.grp_lock, pool, ssock,
1681 ssl_on_destroy);
1682 }
1683
1684 ssock->param.read_buffer_size = ((ssock->param.read_buffer_size+7)>>3)<<3;
1685 if (!ssock->param.timer_heap) {
1686 PJ_LOG(3,(ssock->pool->obj_name, "Warning: timer heap is not "
1687 "available. It is recommended to supply one to avoid "
1688 "a race condition if more than one worker threads "
1689 "are used."));
1690 }
1691
1692 /* Finally */
1693 *p_ssock = ssock;
1694
1695 return PJ_SUCCESS;
1696 }
1697
1698
1699 /*
1700 * Close the secure socket. This will unregister the socket from the
1701 * ioqueue and ultimately close the socket.
1702 */
1703 PJ_DEF(pj_status_t) pj_ssl_sock_close(pj_ssl_sock_t *ssock)
1704 {
1705 PJ_ASSERT_RETURN(ssock, PJ_EINVAL);
1706
1707 if (!ssock->pool || ssock->is_closing)
1708 return PJ_SUCCESS;
1709
1710 ssock->is_closing = PJ_TRUE;
1711
1712 if (ssock->timer.id != TIMER_NONE) {
1713 pj_timer_heap_cancel(ssock->param.timer_heap, &ssock->timer);
1714 ssock->timer.id = TIMER_NONE;
1715 }
1716
1717 ssl_reset_sock_state(ssock);
1718
1719 /* Fire error callbacks for pending sends synchronously, while
1720 * the app's context is still valid (before we return from close).
1721 * Must be after ssl_reset_sock_state which closes the socket,
1722 * ensuring no more async completions can race with us.
1723 */
1724 cancel_pending_sends(ssock);
1725
1726 /* Wipe out cert & key buffer. */
1727 if (ssock->cert) {
1728 pj_ssl_cert_wipe_keys(ssock->cert);
1729 //ssock->cert = NULL;
1730 }
1731
1732 if (ssock->param.grp_lock) {
1733 pj_grp_lock_dec_ref(ssock->param.grp_lock);
1734 } else {
1735 ssl_on_destroy(ssock);
1736 }
1737
1738 return PJ_SUCCESS;
1739 }
1740
1741
1742 /*
1743 * Associate arbitrary data with the secure socket.
1744 */
1745 PJ_DEF(pj_status_t) pj_ssl_sock_set_user_data(pj_ssl_sock_t *ssock,
1746 void *user_data)
1747 {
1748 PJ_ASSERT_RETURN(ssock, PJ_EINVAL);
1749
1750 ssock->param.user_data = user_data;
1751 return PJ_SUCCESS;
1752 }
1753
1754
1755 /*
1756 * Retrieve the user data previously associated with this secure
1757 * socket.
1758 */
1759 PJ_DEF(void*) pj_ssl_sock_get_user_data(pj_ssl_sock_t *ssock)
1760 {
1761 PJ_ASSERT_RETURN(ssock, NULL);
1762
1763 return ssock->param.user_data;
1764 }
1765
1766 /*
1767 * Retrieve the local address and port used by specified SSL socket.
1768 */
1769 PJ_DEF(pj_status_t) pj_ssl_sock_get_info (pj_ssl_sock_t *ssock,
1770 pj_ssl_sock_info *info)
1771 {
1772 pj_bzero(info, sizeof(*info));
1773
1774 /* Established flag */
1775 info->established = (ssock->ssl_state == SSL_STATE_ESTABLISHED);
1776
1777 /* Protocol */
1778 info->proto = ssock->param.proto;
1779
1780 /* Local address */
1781 pj_sockaddr_cp(&info->local_addr, &ssock->local_addr);
1782
1783 /* Certificates info */
1784 info->local_cert_info = &ssock->local_cert_info;
1785 info->remote_cert_info = &ssock->remote_cert_info;
1786
1787 /* Remote address */
1788 if (pj_sockaddr_has_addr(&ssock->rem_addr))
1789 pj_sockaddr_cp(&info->remote_addr, &ssock->rem_addr);
1790
1791 if (info->established) {
1792 info->cipher = ssl_get_cipher(ssock);
1793 }
1794
1795 /* Verification status */
1796 info->verify_status = ssock->verify_status;
1797
1798 /* Last known SSL error code */
1799 info->last_native_err = ssock->last_err;
1800
1801 /* Group lock */
1802 info->grp_lock = ssock->param.grp_lock;
1803
1804 /* Native SSL object */
1805 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0 && \
1806 (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
1807 {
1808 ossl_sock_t *ossock = (ossl_sock_t *)ssock;
1809 info->native_ssl = ossock->ossl_ssl;
1810 }
1811 #endif
1812
1813 return PJ_SUCCESS;
1814 }
1815
1816
1817 /*
1818 * Starts read operation on this secure socket.
1819 */
1820 PJ_DEF(pj_status_t) pj_ssl_sock_start_read (pj_ssl_sock_t *ssock,
1821 pj_pool_t *pool,
1822 unsigned buff_size,
1823 pj_uint32_t flags)
1824 {
1825 void **readbuf;
1826 unsigned i;
1827
1828 PJ_ASSERT_RETURN(ssock && pool && buff_size, PJ_EINVAL);
1829
1830 if (ssock->ssl_state != SSL_STATE_ESTABLISHED)
1831 return PJ_EINVALIDOP;
1832
1833 readbuf = (void**) pj_pool_calloc(pool, ssock->param.async_cnt,
1834 sizeof(void*));
1835 if (!readbuf)
1836 return PJ_ENOMEM;
1837
1838 for (i=0; i<ssock->param.async_cnt; ++i) {
1839 readbuf[i] = pj_pool_alloc(pool, buff_size);
1840 if (!readbuf[i])
1841 return PJ_ENOMEM;
1842 }
1843
1844 return pj_ssl_sock_start_read2(ssock, pool, buff_size,
1845 readbuf, flags);
1846 }
1847
1848
1849 /*
1850 * Same as #pj_ssl_sock_start_read(), except that the application
1851 * supplies the buffers for the read operation so that the acive socket
1852 * does not have to allocate the buffers.
1853 */
1854 PJ_DEF(pj_status_t) pj_ssl_sock_start_read2 (pj_ssl_sock_t *ssock,
1855 pj_pool_t *pool,
1856 unsigned buff_size,
1857 void *readbuf[],
1858 pj_uint32_t flags)
1859 {
1860 unsigned i;
1861
1862 PJ_ASSERT_RETURN(ssock && pool && buff_size && readbuf, PJ_EINVAL);
1863
1864 if (ssock->ssl_state != SSL_STATE_ESTABLISHED)
1865 return PJ_EINVALIDOP;
1866
1867 /* Create SSL socket read buffer */
1868 ssock->ssock_rbuf = (read_data_t*)pj_pool_calloc(pool,
1869 ssock->param.async_cnt,
1870 sizeof(read_data_t));
1871 if (!ssock->ssock_rbuf)
1872 return PJ_ENOMEM;
1873
1874 /* Store SSL socket read buffer pointer in the activesock read buffer */
1875 for (i=0; i<ssock->param.async_cnt; ++i) {
1876 read_data_t **p_ssock_rbuf =
1877 OFFSET_OF_READ_DATA_PTR(ssock, ssock->asock_rbuf[i]);
1878
1879 ssock->ssock_rbuf[i].data = readbuf[i];
1880 ssock->ssock_rbuf[i].len = 0;
1881
1882 *p_ssock_rbuf = &ssock->ssock_rbuf[i];
1883 }
1884
1885 ssock->read_size = buff_size;
1886 ssock->read_started = PJ_TRUE;
1887 ssock->read_flags = flags;
1888
1889 for (i=0; i<ssock->param.async_cnt; ++i) {
1890 if (ssock->asock_rbuf[i]) {
1891 pj_size_t remainder = 0;
1892 ssock_on_data_read(ssock, ssock->asock_rbuf[i], 0,
1893 PJ_SUCCESS, &remainder);
1894 }
1895 }
1896
1897 return PJ_SUCCESS;
1898 }
1899
1900
1901 /*
1902 * Same as pj_ssl_sock_start_read(), except that this function is used
1903 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
1904 * callback instead.
1905 */
1906 PJ_DEF(pj_status_t) pj_ssl_sock_start_recvfrom (pj_ssl_sock_t *ssock,
1907 pj_pool_t *pool,
1908 unsigned buff_size,
1909 pj_uint32_t flags)
1910 {
1911 PJ_UNUSED_ARG(ssock);
1912 PJ_UNUSED_ARG(pool);
1913 PJ_UNUSED_ARG(buff_size);
1914 PJ_UNUSED_ARG(flags);
1915
1916 return PJ_ENOTSUP;
1917 }
1918
1919
1920 /*
1921 * Same as #pj_ssl_sock_start_recvfrom() except that the recvfrom()
1922 * operation takes the buffer from the argument rather than creating
1923 * new ones.
1924 */
1925 PJ_DEF(pj_status_t) pj_ssl_sock_start_recvfrom2 (pj_ssl_sock_t *ssock,
1926 pj_pool_t *pool,
1927 unsigned buff_size,
1928 void *readbuf[],
1929 pj_uint32_t flags)
1930 {
1931 PJ_UNUSED_ARG(ssock);
1932 PJ_UNUSED_ARG(pool);
1933 PJ_UNUSED_ARG(buff_size);
1934 PJ_UNUSED_ARG(readbuf);
1935 PJ_UNUSED_ARG(flags);
1936
1937 return PJ_ENOTSUP;
1938 }
1939
1940
1941 /* Write plain data to SSL and flush the buffer. */
1942 static pj_status_t ssl_send (pj_ssl_sock_t *ssock,
1943 pj_ioqueue_op_key_t *send_key,
1944 const void *data,
1945 pj_ssize_t size,
1946 unsigned flags)
1947 {
1948 pj_status_t status;
1949 int nwritten = 0;
1950 pj_bool_t should_drain = PJ_FALSE;
1951
1952 /* Encrypt and enqueue atomically under write_mutex.
1953 * This eliminates the race between ssl_write and flush that could
1954 * merge encrypted data from different threads into one send op.
1955 */
1956 pj_lock_acquire(ssock->write_mutex);
1957 status = ssl_write(ssock, data, size, &nwritten);
1958
1959 if (status == PJ_SUCCESS && nwritten == size) {
1960 /* All data written — enqueue for sending (still under lock) */
1961 status = enqueue_ssl_write_buf(ssock, send_key, size, flags,
1962 &should_drain);
1963 pj_lock_release(ssock->write_mutex);
1964
1965 if (should_drain)
1966 status = drain_send_queue(ssock, PJ_TRUE);
1967
1968 } else if (status == PJ_ETRYAGAIN) {
1969 /* Re-negotiation in progress — flush handshake data */
1970 status = enqueue_ssl_write_buf(ssock, &ssock->handshake_op_key,
1971 0, 0, &should_drain);
1972 pj_lock_release(ssock->write_mutex);
1973
1974 if (should_drain) {
1975 pj_status_t ds = drain_send_queue(ssock, PJ_TRUE);
1976 if (ds == PJ_SUCCESS || ds == PJ_EPENDING)
1977 status = PJ_EBUSY;
1978 else
1979 status = ds;
1980 } else {
1981 status = PJ_EBUSY;
1982 }
1983 } else {
1984 pj_lock_release(ssock->write_mutex);
1985 }
1986
1987 return status;
1988 }
1989
1990 /* Flush delayed data sending in the write pending list. */
1991 static pj_status_t flush_delayed_send(pj_ssl_sock_t *ssock)
1992 {
1993 /* Check for another ongoing flush */
1994 if (ssock->flushing_write_pend)
1995 return PJ_EBUSY;
1996
1997 pj_lock_acquire(ssock->write_mutex);
1998
1999 /* Again, check for another ongoing flush */
2000 if (ssock->flushing_write_pend) {
2001 pj_lock_release(ssock->write_mutex);
2002 return PJ_EBUSY;
2003 }
2004
2005 /* Set ongoing flush flag */
2006 ssock->flushing_write_pend = PJ_TRUE;
2007
2008 while (!pj_list_empty(&ssock->write_pending)) {
2009 write_data_t *wp;
2010 pj_ioqueue_op_key_t *app_key;
2011 pj_ssize_t plain_data_len;
2012 pj_status_t status;
2013
2014 wp = ssock->write_pending.next;
2015 app_key = wp->app_key;
2016 plain_data_len = wp->plain_data_len;
2017
2018 /* Ticket #1573: Don't hold mutex while calling socket send. */
2019 pj_lock_release(ssock->write_mutex);
2020
2021 /* Pass the original app_key (not &wp->key) so the correct
2022 * key propagates through to the final on_data_sent callback.
2023 */
2024 status = ssl_send(ssock, app_key, wp->data.ptr,
2025 plain_data_len, wp->flags);
2026
2027 if (status == PJ_EPENDING) {
2028 /* Data encrypted and queued for async sending.
2029 * Remove wp to prevent double-processing on next flush.
2030 */
2031 pj_lock_acquire(ssock->write_mutex);
2032 pj_list_erase(wp);
2033 pj_list_push_back(&ssock->write_pending_empty, wp);
2034 pj_lock_release(ssock->write_mutex);
2035
2036 ssock->flushing_write_pend = PJ_FALSE;
2037 return PJ_EPENDING;
2038 }
2039
2040 if (status != PJ_SUCCESS) {
2041 /* Reset ongoing flush flag first. */
2042 ssock->flushing_write_pend = PJ_FALSE;
2043 return status;
2044 }
2045
2046 /* PJ_SUCCESS: sent synchronously. Remove wp. */
2047 pj_lock_acquire(ssock->write_mutex);
2048 pj_list_erase(wp);
2049 pj_list_push_back(&ssock->write_pending_empty, wp);
2050 pj_lock_release(ssock->write_mutex);
2051
2052 /* Invoke callback — app originally got PJ_EPENDING from
2053 * pj_ssl_sock_send, so it expects a completion callback.
2054 */
2055 if (ssock->param.cb.on_data_sent) {
2056 pj_bool_t ret;
2057 ret = (*ssock->param.cb.on_data_sent)(ssock, app_key,
2058 plain_data_len);
2059 if (!ret) {
2060 /* We've been destroyed. Do NOT touch ssock. */
2061 return PJ_SUCCESS;
2062 }
2063 }
2064
2065 pj_lock_acquire(ssock->write_mutex);
2066 }
2067
2068 /* Reset ongoing flush flag */
2069 ssock->flushing_write_pend = PJ_FALSE;
2070
2071 pj_lock_release(ssock->write_mutex);
2072
2073 return PJ_SUCCESS;
2074 }
2075
2076 /* Sending is delayed, push back the sending data into pending list. */
2077 static pj_status_t delay_send (pj_ssl_sock_t *ssock,
2078 pj_ioqueue_op_key_t *send_key,
2079 const void *data,
2080 pj_ssize_t size,
2081 unsigned flags)
2082 {
2083 write_data_t *wp;
2084
2085 pj_lock_acquire(ssock->write_mutex);
2086
2087 /* Init write pending instance */
2088 if (!pj_list_empty(&ssock->write_pending_empty)) {
2089 wp = ssock->write_pending_empty.next;
2090 pj_list_erase(wp);
2091 } else {
2092 wp = PJ_POOL_ZALLOC_T(ssock->pool, write_data_t);
2093 }
2094
2095 wp->app_key = send_key;
2096 wp->plain_data_len = size;
2097 wp->data.ptr = data;
2098 wp->flags = flags;
2099
2100 pj_list_push_back(&ssock->write_pending, wp);
2101
2102 pj_lock_release(ssock->write_mutex);
2103
2104 /* Must return PJ_EPENDING */
2105 return PJ_EPENDING;
2106 }
2107
2108
2109 /**
2110 * Send data using the socket.
2111 */
2112 PJ_DEF(pj_status_t) pj_ssl_sock_send (pj_ssl_sock_t *ssock,
2113 pj_ioqueue_op_key_t *send_key,
2114 const void *data,
2115 pj_ssize_t *size,
2116 unsigned flags)
2117 {
2118 pj_status_t status;
2119
2120 PJ_ASSERT_RETURN(ssock && data && size && (*size>0), PJ_EINVAL);
2121
2122 if (ssock->ssl_state != SSL_STATE_ESTABLISHED)
2123 return PJ_EINVALIDOP;
2124
2125 // Ticket #1573: Don't hold mutex while calling PJLIB socket send().
2126 //pj_lock_acquire(ssock->write_mutex);
2127
2128 /* Flush delayed send first. Sending data might be delayed when
2129 * re-negotiation is on-progress.
2130 */
2131 status = flush_delayed_send(ssock);
2132 if (status == PJ_EBUSY || status == PJ_EPENDING) {
2133 /* Re-negotiation or flushing is on progress, delay sending */
2134 status = delay_send(ssock, send_key, data, *size, flags);
2135 goto on_return;
2136 } else if (status != PJ_SUCCESS) {
2137 goto on_return;
2138 }
2139
2140 /* Prevent unbounded queue growth when the network stalls.
2141 * Check BEFORE ssl_send which encrypts the plaintext — once
2142 * encrypted, the data cannot be "un-sent" without corruption.
2143 */
2144 #if PJ_SSL_SEND_OP_ACTIVE_MAX > 0
2145 if (ssock->send_op_active_cnt >= PJ_SSL_SEND_OP_ACTIVE_MAX) {
2146 status = PJ_EBUSY;
2147 goto on_return;
2148 }
2149 #endif
2150
2151 /* Write data to SSL */
2152 status = ssl_send(ssock, send_key, data, *size, flags);
2153 if (status == PJ_EBUSY) {
2154 /* Re-negotiation is on progress, delay sending */
2155 status = delay_send(ssock, send_key, data, *size, flags);
2156 }
2157
2158 on_return:
2159 //pj_lock_release(ssock->write_mutex);
2160 return status;
2161 }
2162
2163
2164 /**
2165 * Send datagram using the socket.
2166 */
2167 PJ_DEF(pj_status_t) pj_ssl_sock_sendto (pj_ssl_sock_t *ssock,
2168 pj_ioqueue_op_key_t *send_key,
2169 const void *data,
2170 pj_ssize_t *size,
2171 unsigned flags,
2172 const pj_sockaddr_t *addr,
2173 int addr_len)
2174 {
2175 PJ_UNUSED_ARG(ssock);
2176 PJ_UNUSED_ARG(send_key);
2177 PJ_UNUSED_ARG(data);
2178 PJ_UNUSED_ARG(size);
2179 PJ_UNUSED_ARG(flags);
2180 PJ_UNUSED_ARG(addr);
2181 PJ_UNUSED_ARG(addr_len);
2182
2183 return PJ_ENOTSUP;
2184 }
2185
2186
2187 /**
2188 * Starts asynchronous socket accept() operations on this secure socket.
2189 */
2190 PJ_DEF(pj_status_t) pj_ssl_sock_start_accept (pj_ssl_sock_t *ssock,
2191 pj_pool_t *pool,
2192 const pj_sockaddr_t *localaddr,
2193 int addr_len)
2194 {
2195 return pj_ssl_sock_start_accept2(ssock, pool, localaddr, addr_len,
2196 &ssock->param);
2197 }
2198
2199
2200 /**
2201 * Same as #pj_ssl_sock_start_accept(), but application provides parameter
2202 * for new accepted secure sockets.
2203 */
2204 PJ_DEF(pj_status_t)
2205 pj_ssl_sock_start_accept2(pj_ssl_sock_t *ssock,
2206 pj_pool_t *pool,
2207 const pj_sockaddr_t *localaddr,
2208 int addr_len,
2209 const pj_ssl_sock_param *newsock_param)
2210 {
2211 pj_status_t status;
2212 #ifndef SSL_SOCK_IMP_USE_OWN_NETWORK
2213 pj_activesock_cb asock_cb;
2214 pj_activesock_cfg asock_cfg;
2215 #endif
2216
2217 PJ_ASSERT_RETURN(ssock && pool && localaddr && addr_len, PJ_EINVAL);
2218
2219 /* Verify new socket parameters */
2220 if (newsock_param->grp_lock != ssock->param.grp_lock ||
2221 newsock_param->sock_af != ssock->param.sock_af ||
2222 newsock_param->sock_type != ssock->param.sock_type)
2223 {
2224 return PJ_EINVAL;
2225 }
2226
2227 ssock->is_server = PJ_TRUE;
2228
2229 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
2230 status = network_start_accept(ssock, pool, localaddr, addr_len,
2231 newsock_param);
2232 if (status != PJ_SUCCESS)
2233 goto on_error;
2234 #else
2235 /* Create socket */
2236 if (ssock->param.sock_cloexec)
2237 ssock->param.sock_type |= pj_SOCK_CLOEXEC();
2238 status = pj_sock_socket(ssock->param.sock_af, ssock->param.sock_type, 0,
2239 &ssock->sock);
2240 if (status != PJ_SUCCESS)
2241 goto on_error;
2242
2243 /* Apply SO_REUSEADDR */
2244 if (ssock->param.reuse_addr) {
2245 int enabled = 1;
2246 status = pj_sock_setsockopt(ssock->sock, pj_SOL_SOCKET(),
2247 pj_SO_REUSEADDR(),
2248 &enabled, sizeof(enabled));
2249 if (status != PJ_SUCCESS) {
2250 PJ_PERROR(4,(ssock->pool->obj_name, status,
2251 "Warning: error applying SO_REUSEADDR"));
2252 }
2253 }
2254
2255 /* Apply QoS, if specified */
2256 status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type,
2257 &ssock->param.qos_params, 2,
2258 ssock->pool->obj_name, NULL);
2259 if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error)
2260 goto on_error;
2261
2262 /* Apply socket options, if specified */
2263 if (ssock->param.sockopt_params.cnt) {
2264 status = pj_sock_setsockopt_params(ssock->sock,
2265 &ssock->param.sockopt_params);
2266
2267 if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error)
2268 goto on_error;
2269 }
2270
2271 /* Bind socket */
2272 status = pj_sock_bind(ssock->sock, localaddr, addr_len);
2273 if (status != PJ_SUCCESS)
2274 goto on_error;
2275
2276 /* Start listening to the address */
2277 status = pj_sock_listen(ssock->sock, PJ_SOMAXCONN);
2278 if (status != PJ_SUCCESS)
2279 goto on_error;
2280
2281 /* Create active socket */
2282 pj_activesock_cfg_default(&asock_cfg);
2283 asock_cfg.async_cnt = ssock->param.async_cnt;
2284 asock_cfg.concurrency = ssock->param.concurrency;
2285 asock_cfg.whole_data = PJ_FALSE;
2286 asock_cfg.grp_lock = ssock->param.grp_lock;
2287
2288 pj_bzero(&asock_cb, sizeof(asock_cb));
2289 //asock_cb.on_accept_complete = asock_on_accept_complete;
2290 asock_cb.on_accept_complete2 = asock_on_accept_complete2;
2291
2292 status = pj_activesock_create(pool,
2293 ssock->sock,
2294 ssock->param.sock_type,
2295 &asock_cfg,
2296 ssock->param.ioqueue,
2297 &asock_cb,
2298 ssock,
2299 &ssock->asock);
2300
2301 if (status != PJ_SUCCESS)
2302 goto on_error;
2303
2304 /* Start accepting */
2305 pj_ssl_sock_param_copy(pool, &ssock->newsock_param, newsock_param);
2306 ssock->newsock_param.grp_lock = NULL;
2307 status = pj_activesock_start_accept(ssock->asock, pool);
2308 if (status != PJ_SUCCESS)
2309 goto on_error;
2310
2311 /* Update local address */
2312 ssock->addr_len = addr_len;
2313 status = pj_sock_getsockname(ssock->sock, &ssock->local_addr,
2314 &ssock->addr_len);
2315 if (status != PJ_SUCCESS)
2316 pj_sockaddr_cp(&ssock->local_addr, localaddr);
2317 #endif
2318
2319 return PJ_SUCCESS;
2320
2321 on_error:
2322 ssl_reset_sock_state(ssock);
2323 return status;
2324 }
2325
2326
2327 /**
2328 * Starts asynchronous socket connect() operation.
2329 */
2330 PJ_DEF(pj_status_t) pj_ssl_sock_start_connect(pj_ssl_sock_t *ssock,
2331 pj_pool_t *pool,
2332 const pj_sockaddr_t *localaddr,
2333 const pj_sockaddr_t *remaddr,
2334 int addr_len)
2335 {
2336 pj_ssl_start_connect_param param;
2337 param.pool = pool;
2338 param.localaddr = localaddr;
2339 param.local_port_range = 0;
2340 param.remaddr = remaddr;
2341 param.addr_len = addr_len;
2342
2343 return pj_ssl_sock_start_connect2(ssock, &param);
2344 }
2345
2346 PJ_DEF(pj_status_t) pj_ssl_sock_start_connect2(
2347 pj_ssl_sock_t *ssock,
2348 pj_ssl_start_connect_param *connect_param)
2349 {
2350 pj_status_t status;
2351 #ifdef SSL_SOCK_IMP_USE_OWN_NETWORK
2352 status = network_start_connect(ssock, connect_param);
2353 if (status != PJ_EPENDING)
2354 goto on_error;
2355 #else
2356 pj_activesock_cb asock_cb;
2357 pj_activesock_cfg asock_cfg;
2358
2359 pj_pool_t *pool = connect_param->pool;
2360 const pj_sockaddr_t *localaddr = connect_param->localaddr;
2361 pj_uint16_t port_range = connect_param->local_port_range;
2362 const pj_sockaddr_t *remaddr = connect_param->remaddr;
2363 int addr_len = connect_param->addr_len;
2364
2365 PJ_ASSERT_RETURN(ssock && pool && localaddr && remaddr && addr_len,
2366 PJ_EINVAL);
2367
2368 /* Create socket */
2369 if (ssock->param.sock_cloexec)
2370 ssock->param.sock_type |= pj_SOCK_CLOEXEC();
2371 status = pj_sock_socket(ssock->param.sock_af, ssock->param.sock_type, 0,
2372 &ssock->sock);
2373 if (status != PJ_SUCCESS)
2374 goto on_error;
2375
2376 /* Apply QoS, if specified */
2377 status = pj_sock_apply_qos2(ssock->sock, ssock->param.qos_type,
2378 &ssock->param.qos_params, 2,
2379 ssock->pool->obj_name, NULL);
2380 if (status != PJ_SUCCESS && !ssock->param.qos_ignore_error)
2381 goto on_error;
2382
2383 /* Apply socket options, if specified */
2384 if (ssock->param.sockopt_params.cnt) {
2385 status = pj_sock_setsockopt_params(ssock->sock,
2386 &ssock->param.sockopt_params);
2387
2388 if (status != PJ_SUCCESS && !ssock->param.sockopt_ignore_error)
2389 goto on_error;
2390 }
2391
2392 /* Bind socket */
2393 if (port_range) {
2394 pj_uint16_t max_bind_retry = MAX_BIND_RETRY;
2395 if (port_range && port_range < max_bind_retry)
2396 {
2397 max_bind_retry = port_range;
2398 }
2399 status = pj_sock_bind_random(ssock->sock, localaddr, port_range,
2400 max_bind_retry);
2401 } else {
2402 status = pj_sock_bind(ssock->sock, localaddr, addr_len);
2403 }
2404
2405 if (status != PJ_SUCCESS)
2406 goto on_error;
2407
2408 /* Create active socket */
2409 pj_activesock_cfg_default(&asock_cfg);
2410 asock_cfg.async_cnt = ssock->param.async_cnt;
2411 asock_cfg.concurrency = ssock->param.concurrency;
2412 asock_cfg.whole_data = PJ_TRUE;
2413 asock_cfg.grp_lock = ssock->param.grp_lock;
2414
2415 pj_bzero(&asock_cb, sizeof(asock_cb));
2416 asock_cb.on_connect_complete = asock_on_connect_complete;
2417 asock_cb.on_data_read = asock_on_data_read;
2418 asock_cb.on_data_sent = asock_on_data_sent;
2419
2420 status = pj_activesock_create(pool,
2421 ssock->sock,
2422 ssock->param.sock_type,
2423 &asock_cfg,
2424 ssock->param.ioqueue,
2425 &asock_cb,
2426 ssock,
2427 &ssock->asock);
2428
2429 if (status != PJ_SUCCESS)
2430 goto on_error;
2431
2432 /* Save remote address */
2433 pj_sockaddr_cp(&ssock->rem_addr, remaddr);
2434
2435 status = pj_activesock_start_connect(ssock->asock, pool, remaddr,
2436 addr_len);
2437
2438 if (status == PJ_SUCCESS) {
2439 /* Synchronous connect completion. The callback handles
2440 * local address update, SSL create, handshake, and user
2441 * notification. Don't access ssock after this — the user
2442 * callback may have destroyed it.
2443 */
2444 asock_on_connect_complete(ssock->asock, PJ_SUCCESS);
2445 return PJ_EPENDING;
2446 } else if (status != PJ_EPENDING) {
2447 goto on_error;
2448 }
2449
2450 /* Update local address */
2451 ssock->addr_len = addr_len;
2452 status = pj_sock_getsockname(ssock->sock, &ssock->local_addr,
2453 &ssock->addr_len);
2454 /* Note that we may not get an IP address here. This can
2455 * happen for example on Windows, where getsockname()
2456 * would return 0.0.0.0 if socket has just started the
2457 * async connect. In this case, just leave the local
2458 * address with 0.0.0.0 for now; it will be updated
2459 * once the socket is established.
2460 */
2461
2462 #endif
2463
2464 /* Start timer */
2465 if (ssock->param.timer_heap &&
2466 (ssock->param.timeout.sec != 0 || ssock->param.timeout.msec != 0))
2467 {
2468 pj_assert(ssock->timer.id == TIMER_NONE);
2469 status = pj_timer_heap_schedule_w_grp_lock(ssock->param.timer_heap,
2470 &ssock->timer,
2471 &ssock->param.timeout,
2472 TIMER_HANDSHAKE_TIMEOUT,
2473 ssock->param.grp_lock);
2474 if (status != PJ_SUCCESS) {
2475 ssock->timer.id = TIMER_NONE;
2476 status = PJ_SUCCESS;
2477 }
2478 }
2479
2480 /* Update SSL state */
2481 ssock->is_server = PJ_FALSE;
2482
2483 return PJ_EPENDING;
2484
2485 on_error:
2486 ssl_reset_sock_state(ssock);
2487 return status;
2488 }
2489
2490
2491 PJ_DEF(pj_status_t) pj_ssl_sock_renegotiate(pj_ssl_sock_t *ssock)
2492 {
2493 pj_status_t status;
2494
2495 PJ_ASSERT_RETURN(ssock, PJ_EINVAL);
2496
2497 if (ssock->ssl_state != SSL_STATE_ESTABLISHED)
2498 return PJ_EINVALIDOP;
2499
2500 status = ssl_renegotiate(ssock);
2501 if (status == PJ_SUCCESS) {
2502 status = ssl_do_handshake_and_flush(ssock);
2503 }
2504
2505 return status;
2506 }
2507
2508 static void wipe_buf(pj_str_t *buf)
2509 {
2510 volatile char *p = buf->ptr;
2511 pj_ssize_t len = buf->slen;
2512 while (len--) *p++ = 0;
2513 buf->slen = 0;
2514 }
2515
2516 PJ_DEF(void) pj_ssl_cert_wipe_keys(pj_ssl_cert_t *cert)
2517 {
2518 if (cert) {
2519 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2520 wipe_buf(&cert->CA_file);
2521 wipe_buf(&cert->CA_path);
2522 wipe_buf(&cert->cert_file);
2523 wipe_buf(&cert->privkey_file);
2524 wipe_buf(&cert->privkey_pass);
2525 wipe_buf(&cert->CA_buf);
2526 wipe_buf(&cert->cert_buf);
2527 wipe_buf(&cert->privkey_buf);
2528 #else
2529 cert->criteria.type = PJ_SSL_CERT_LOOKUP_NONE;
2530 wipe_buf(&cert->criteria.keyword);
2531 #endif
2532
2533 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
2534 ssl_free_cert(cert);
2535 #endif
2536 }
2537 }
2538
2539 /* Load credentials from files. */
2540 PJ_DEF(pj_status_t) pj_ssl_cert_load_from_files (pj_pool_t *pool,
2541 const pj_str_t *CA_file,
2542 const pj_str_t *cert_file,
2543 const pj_str_t *privkey_file,
2544 const pj_str_t *privkey_pass,
2545 pj_ssl_cert_t **p_cert)
2546 {
2547 return pj_ssl_cert_load_from_files2(pool, CA_file, NULL, cert_file,
2548 privkey_file, privkey_pass, p_cert);
2549 }
2550
2551 PJ_DEF(pj_status_t) pj_ssl_cert_load_from_files2(pj_pool_t *pool,
2552 const pj_str_t *CA_file,
2553 const pj_str_t *CA_path,
2554 const pj_str_t *cert_file,
2555 const pj_str_t *privkey_file,
2556 const pj_str_t *privkey_pass,
2557 pj_ssl_cert_t **p_cert)
2558 {
2559 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2560 pj_ssl_cert_t *cert;
2561
2562 PJ_ASSERT_RETURN(pool && p_cert &&
2563 (CA_file || CA_path || cert_file || privkey_file),
2564 PJ_EINVAL);
2565
2566 cert = *p_cert;
2567 if (!cert)
2568 cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t);
2569 if (!cert)
2570 return PJ_ENOMEM;
2571
2572 if (CA_file) {
2573 pj_strdup_with_null(pool, &cert->CA_file, CA_file);
2574 }
2575 if (CA_path) {
2576 pj_strdup_with_null(pool, &cert->CA_path, CA_path);
2577 }
2578 if (cert_file) {
2579 pj_strdup_with_null(pool, &cert->cert_file, cert_file);
2580 }
2581 if (privkey_file) {
2582 pj_strdup_with_null(pool, &cert->privkey_file, privkey_file);
2583 }
2584 if (privkey_pass) {
2585 pj_strdup_with_null(pool, &cert->privkey_pass, privkey_pass);
2586 }
2587
2588 *p_cert = cert;
2589
2590 return PJ_SUCCESS;
2591 #else
2592 PJ_UNUSED_ARG(pool);
2593 PJ_UNUSED_ARG(CA_file);
2594 PJ_UNUSED_ARG(CA_path);
2595 PJ_UNUSED_ARG(cert_file);
2596 PJ_UNUSED_ARG(privkey_file);
2597 PJ_UNUSED_ARG(privkey_pass);
2598 PJ_UNUSED_ARG(p_cert);
2599 return PJ_ENOTSUP;
2600 #endif
2601 }
2602
2603 PJ_DEF(pj_status_t) pj_ssl_cert_load_from_buffer(pj_pool_t *pool,
2604 const pj_ssl_cert_buffer *CA_buf,
2605 const pj_ssl_cert_buffer *cert_buf,
2606 const pj_ssl_cert_buffer *privkey_buf,
2607 const pj_str_t *privkey_pass,
2608 pj_ssl_cert_t **p_cert)
2609 {
2610 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2611 pj_ssl_cert_t *cert;
2612
2613 PJ_ASSERT_RETURN(pool && p_cert &&
2614 (CA_buf || cert_buf || privkey_buf),
2615 PJ_EINVAL);
2616
2617 cert = *p_cert;
2618 if (!cert)
2619 cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t);
2620 if (!cert)
2621 return PJ_ENOMEM;
2622
2623 if (CA_buf) {
2624 pj_strdup(pool, &cert->CA_buf, CA_buf);
2625 }
2626 if (cert_buf) {
2627 pj_strdup(pool, &cert->cert_buf, cert_buf);
2628 }
2629 if (privkey_buf) {
2630 pj_strdup(pool, &cert->privkey_buf, privkey_buf);
2631 }
2632 if (privkey_pass) {
2633 pj_strdup_with_null(pool, &cert->privkey_pass, privkey_pass);
2634 }
2635
2636 *p_cert = cert;
2637
2638 return PJ_SUCCESS;
2639 #else
2640 PJ_UNUSED_ARG(pool);
2641 PJ_UNUSED_ARG(CA_buf);
2642 PJ_UNUSED_ARG(cert_buf);
2643 PJ_UNUSED_ARG(privkey_buf);
2644 PJ_UNUSED_ARG(privkey_pass);
2645 PJ_UNUSED_ARG(p_cert);
2646 return PJ_ENOTSUP;
2647 #endif
2648 }
2649
2650
2651 PJ_DEF(pj_status_t) pj_ssl_cert_load_from_store(
2652 pj_pool_t *pool,
2653 const pj_ssl_cert_lookup_criteria *criteria,
2654 pj_ssl_cert_t **p_cert)
2655 {
2656 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_SCHANNEL)
2657 pj_ssl_cert_t *cert;
2658
2659 PJ_ASSERT_RETURN(pool && p_cert && criteria, PJ_EINVAL);
2660
2661 cert = *p_cert;
2662 if (!cert)
2663 cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t);
2664 if (!cert)
2665 return PJ_ENOMEM;
2666
2667 pj_memcpy(&cert->criteria, criteria, sizeof(*criteria));
2668 pj_strdup_with_null(pool, &cert->criteria.keyword, &criteria->keyword);
2669
2670 *p_cert = cert;
2671
2672 return PJ_SUCCESS;
2673 #else
2674 PJ_UNUSED_ARG(pool);
2675 PJ_UNUSED_ARG(criteria);
2676 PJ_UNUSED_ARG(p_cert);
2677 return PJ_ENOTSUP;
2678 #endif
2679 }
2680
2681
2682 PJ_DEF(pj_status_t) pj_ssl_cert_load_direct(
2683 pj_pool_t *pool,
2684 pj_ssl_cert_direct *cert_direct,
2685 pj_ssl_cert_t **p_cert)
2686 {
2687 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
2688 pj_ssl_cert_t *cert;
2689
2690 PJ_ASSERT_RETURN(pool && p_cert && cert_direct, PJ_EINVAL);
2691
2692 cert = *p_cert;
2693 if (!cert)
2694 cert = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t);
2695 if (!cert)
2696 return PJ_ENOMEM;
2697
2698 cert->direct = *cert_direct;
2699
2700 *p_cert = cert;
2701
2702 return PJ_SUCCESS;
2703 #else
2704 PJ_UNUSED_ARG(pool);
2705 PJ_UNUSED_ARG(cert_direct);
2706 PJ_UNUSED_ARG(p_cert);
2707 return PJ_ENOTSUP;
2708 #endif
2709 }
2710
2711
2712 /* Set SSL socket credentials. */
2713 PJ_DEF(pj_status_t) pj_ssl_sock_set_certificate(
2714 pj_ssl_sock_t *ssock,
2715 pj_pool_t *pool,
2716 const pj_ssl_cert_t *cert)
2717 {
2718 pj_ssl_cert_t *cert_;
2719
2720 PJ_ASSERT_RETURN(ssock && pool && cert, PJ_EINVAL);
2721
2722 cert_ = PJ_POOL_ZALLOC_T(pool, pj_ssl_cert_t);
2723 pj_memcpy(cert_, cert, sizeof(pj_ssl_cert_t));
2724
2725 #if (PJ_SSL_SOCK_IMP != PJ_SSL_SOCK_IMP_SCHANNEL)
2726 pj_strdup_with_null(pool, &cert_->CA_file, &cert->CA_file);
2727 pj_strdup_with_null(pool, &cert_->CA_path, &cert->CA_path);
2728 pj_strdup_with_null(pool, &cert_->cert_file, &cert->cert_file);
2729 pj_strdup_with_null(pool, &cert_->privkey_file, &cert->privkey_file);
2730 pj_strdup_with_null(pool, &cert_->privkey_pass, &cert->privkey_pass);
2731
2732 pj_strdup(pool, &cert_->CA_buf, &cert->CA_buf);
2733 pj_strdup(pool, &cert_->cert_buf, &cert->cert_buf);
2734 pj_strdup(pool, &cert_->privkey_buf, &cert->privkey_buf);
2735
2736 /* For OpenSSL version >= 3.0, add ref EVP_PKEY & X509 */
2737 # if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) && \
2738 (OPENSSL_VERSION_NUMBER >= 0x30000000L)
2739 if ((cert_->direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY) &&
2740 cert_->direct.privkey)
2741 {
2742 EVP_PKEY_up_ref(cert_->direct.privkey);
2743 }
2744
2745 if ((cert_->direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT) &&
2746 cert_->direct.cert)
2747 {
2748 X509_up_ref(cert_->direct.cert);
2749 }
2750 # endif
2751
2752 #else
2753 pj_strdup_with_null(pool, &cert_->criteria.keyword,
2754 &cert->criteria.keyword);
2755 #endif
2756
2757 ssock->cert = cert_;
2758
2759 return PJ_SUCCESS;
2760 }
2761