### Description
after some nearest changes, there are problems with the TLS handshake (for at least ECDHE-RSA-AES256-GCM-SHA384)
because of the missed `OPENSSL_init_ssl()` call for TCP workers
#### Reproduction
try to send any SIP message over TLS to the server which accepts ECDHE-RSA-AES256-GCM-SHA384 in Server Hello during handshake
the simplest way to reproduce is to add microsoft teams server to the dispatcher and enable probing to send OPTIONS
kamailio.cfg:
```
loadmodule "tls.so"
modparam("tls", "config", "/etc/kamailio/tls.cfg")
loadmodule "dispatcher.so"
modparam("dispatcher","flags",2)
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
modparam("dispatcher", "ds_ping_method","OPTIONS")
modparam("dispatcher", "ds_ping_interval",5)
modparam("dispatcher", "ds_probing_mode",1)
```
tls.cfg:
```
[server:default]
certificate = /etc/kamailio/ssl/ssl-cert-snakeoil.pem
private_key = /etc/kamailio/ssl/ssl-cert-snakeoil.key
[client:default]
certificate = /etc/kamailio/ssl/ssl-cert-snakeoil.pem
private_key = /etc/kamailio/ssl/ssl-cert-snakeoil.key
```
dispatcher.list:
```
1 sip:sip.pstnhub.microsoft.com;transport=tls 0 0
```
#### Debugging Data
backtrace for the cipher suite lookup:
```
(gdb) bt
#0 ssl_cipher_id_cmp_BSEARCH_CMP_FN (a_=0x7ffd2a0aade0, b_=0x7fa110ceeec0 <ssl3_ciphers+11680>) at ../ssl/ssl_lib.c:4985
#1 0x00007fa110832a95 in ossl_bsearch (key=key@entry=0x7ffd2a0aade0, base=base@entry=0x7fa110cec120 <ssl3_ciphers>, num=num@entry=167, size=size@entry=80, cmp=cmp@entry=0x7fa110c7cac0 <ssl_cipher_id_cmp_BSEARCH_CMP_FN>, flags=flags@entry=0)
at ../crypto/bsearch.c:28
#2 0x00007fa1108fc229 in OBJ_bsearch_ex_ (key=key@entry=0x7ffd2a0aade0, base=base@entry=0x7fa110cec120 <ssl3_ciphers>, num=num@entry=167, size=size@entry=80, cmp=cmp@entry=0x7fa110c7cac0 <ssl_cipher_id_cmp_BSEARCH_CMP_FN>, flags=flags@entry=0)
at ../crypto/objects/obj_dat.c:699
#3 0x00007fa1108fc23c in OBJ_bsearch_ (key=key@entry=0x7ffd2a0aade0, base=base@entry=0x7fa110cec120 <ssl3_ciphers>, num=num@entry=167, size=size@entry=80, cmp=cmp@entry=0x7fa110c7cac0 <ssl_cipher_id_cmp_BSEARCH_CMP_FN>)
at ../crypto/objects/obj_dat.c:691
#4 0x00007fa110c7fb95 in OBJ_bsearch_ssl_cipher_id (key=key@entry=0x7ffd2a0aade0, base=base@entry=0x7fa110cec120 <ssl3_ciphers>, num=num@entry=167) at ../ssl/ssl_lib.c:4985
#5 0x00007fa110c6ed4c in ssl3_get_cipher_by_id (id=<optimized out>) at ../ssl/s3_lib.c:4075
#6 0x00007fa110c76077 in ssl_get_cipher_by_char (ssl=ssl@entry=0x7fa10cfca900, ptr=ptr@entry=0x7fa10cfcc767 "\3000", all=all@entry=0) at ../ssl/ssl_ciph.c:2102
#7 0x00007fa110cae0ee in set_client_ciphersuite (s=s@entry=0x7fa10cfca900, cipherchars=cipherchars@entry=0x7fa10cfcc767 "\3000") at ../ssl/statem/statem_clnt.c:1310
#8 0x00007fa110cb03b3 in tls_process_server_hello (s=0x7fa10cfca900, pkt=<optimized out>) at ../ssl/statem/statem_clnt.c:1614
#9 0x00007fa110cace72 in read_state_machine (s=0x7fa10cfca900) at ../ssl/statem/statem.c:647
#10 state_machine (s=0x7fa10cfca900, server=0) at ../ssl/statem/statem.c:442
#11 0x00007fa110d33aaa in tls_connect (c=c@entry=0x7fa10cfc64e0, error=error@entry=0x7ffd2a0ab0e4) at ./src/modules/tls/tls_server.c:542
#12 0x00007fa110d01edd in ssl_flush (size=413, buf=0x7fa10cfdafdc, error=<synthetic pointer>, tcp_c=0x7fa10cfc64e0) at ./src/modules/tls/tls_ct_wrq.c:101
#13 sbufq_flush (flush_f=<optimized out>, flush_p2=<synthetic pointer>, flush_p1=0x7fa10cfc64e0, flags=0x7ffd2a0ab1ac, q=0x7fa10cfb43a0) at ./src/modules/tls/sbufq.h:247
#14 tls_ct_q_flush (flush_p2=<synthetic pointer>, flush_p1=0x7fa10cfc64e0, flush_f=<optimized out>, flags=0x7ffd2a0ab1ac, tc_q=0x18) at ./src/modules/tls/tls_ct_q.h:122
#15 tls_ct_wq_flush (c=c@entry=0x7fa10cfc64e0, ct_q=ct_q@entry=0x7fa10cfb34c0, flags=flags@entry=0x7ffd2a0ab1ac, ssl_err=ssl_err@entry=0x7ffd2a0ab1a8) at ./src/modules/tls/tls_ct_wrq.c:147
#16 0x00007fa110d386f4 in tls_h_read_f (c=0x7fa10cfc64e0, flags=0x7ffd2a0cb5d0) at ./src/modules/tls/tls_server.c:1140
#17 0x0000562ad6b0c46e in tcp_read_headers (c=c@entry=0x7fa10cfc64e0, read_flags=read_flags@entry=0x7ffd2a0cb5d0) at core/tcp_read.c:445
#18 0x0000562ad6b0f543 in tcp_read_req (con=0x7fa10cfc64e0, bytes_read=bytes_read@entry=0x7ffd2a0cb5cc, read_flags=read_flags@entry=0x7ffd2a0cb5d0) at core/tcp_read.c:1508
#19 0x0000562ad6b14879 in handle_io (fm=fm@entry=0x7fa110ed31c8, events=events@entry=1, idx=idx@entry=-1) at core/tcp_read.c:1832
#20 0x0000562ad6b1a7fd in io_wait_loop_epoll (repeat=repeat@entry=0, t=2, h=0x562ad6de8c20 <io_w>) at core/io_wait.h:1073
#21 0x0000562ad6b1b017 in tcp_receive_loop (unix_sock=<optimized out>) at core/tcp_read.c:2032
#22 0x0000562ad6b02df5 in tcp_init_children (woneinit=woneinit@entry=0x7ffd2a0cbb0c) at core/tcp_main.c:5364
#23 0x0000562ad6928b60 in main_loop () at ./src/main.c:1936
#24 0x0000562ad691a463 in main (argc=<optimized out>, argv=<optimized out>) at ./src/main.c:3212
```
part of the `ssl3_ciphers` array that should be sorted by id ascending:
```
}, {
valid = 1,
name = 0x7fa110cc3720 "ECDHE-RSA-AES256-GCM-SHA384",
stdname = 0x7fa110cc89f8 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
id = 50380848,
...
}, {
valid = 1,
name = 0x7fa110cc3770 "PSK-NULL-SHA",
stdname = 0x7fa110cc373c "TLS_PSK_WITH_NULL_SHA",
id = 50331692,
...
```
* `ossl_bsearch` assumes that it works with sorted `ssl3_ciphers`
* `ssl3_ciphers` sorting performed by `OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings)` -> `ossl_init_ssl_base()` -> `ssl_sort_cipher_list()` -> `qsort(ssl3_ciphers...`
#### Log Messages
```
ERROR: tls [tls_util.h:49]: tls_err_ret(): TLS write:error:0A0000F8:SSL routines::unknown cipher returned (sni: unknown)
```
### Possible Solutions
* ensure `OPENSSL_init_ssl()` to be called (directly or using OPENSSL_INIT_ATFORK) for TCP workers
* fixed by `OPENSSL_init_ssl(0, NULL);` call in `tls_init.c:int tls_h_mod_init_f(void)` but it's hardly the fully correct solution
### Additional Information
* **Kamailio Version** - output of `kamailio -v`
```
# kamailio -v
version: kamailio 5.7.4 (x86_64/linux)
flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, MEM_JOIN_FREE, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLOCKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED
ADAPTIVE_WAIT_LOOPS 1024, MAX_RECV_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: unknown
compiled with gcc 13.2.0
```
```
# apt list kamailio --installed
Listing... Done
kamailio/testing,now 5.7.4-1 amd64 [installed]
```
* **Operating System**:
Debian GNU/Linux trixie/sid (reproduced on Debian bookworm either)
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/3765
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/issues/3765(a)github.com>
Hello,
the branch 5.8 was created, therefore the master branch is open for
adding new features, to be part of future release series v5.9.x (or
whatever version is decided for next series).
Any bug fix committed to master that applies to 5.8.x or older stable
branches should be backported as usual with "git cherry-pick -x ..." to
appropriate branches like 5.8 or 5.7.
Expect that v5.8.0 will be released in a few weeks from now.
Based on the workflow used during the past years, the next future
release v5.9.0 should be out after another 8-10 months of development,
plus 1-2 months of testing, so sometime during the last part of 2024 or
the beginning of 2025.
Cheers,
Daniel
--
Daniel-Constantin Mierla (@ asipto.com)
twitter.com/miconda -- linkedin.com/in/miconda
Kamailio Consultancy, Training and Development Services -- asipto.com
Kamailio World Conference, April 18-19, 2024, Berlin -- kamailioworld.com
Hello,
the branch 5.8 has been created, to be used for releasing v5.8.x series.
To check out this branch, the following commands can be used:
  git clone https://github.com/kamailio/kamailio kamailio-5.8
  cd kamailio-5.8
  git checkout -b 5.8 origin/5.8
Pushing commits in this branch:
 git push origin 5.8:5.8
Note that 5.8 is an official stable branch, so only bug fixes, missing
kemi exports (discuss on sr-dev if not sure) or improvements to
documentation or helper tools can be pushed to this branch.
As usual, if there is a bug fixed, commit and push first to master
branch and then cherry pick to 5.8 branch:
 git cherry-pick -x COMMITID
In few weeks, the first release from branch 5.8 will be out,
respectively Kamailio v5.8.0.
Cheers,
Daniel
--
Daniel-Constantin Mierla (@ asipto.com)
twitter.com/miconda -- linkedin.com/in/miconda
Kamailio Consultancy, Training and Development Services -- asipto.com
Kamailio World Conference, April 18-19, 2024, Berlin -- kamailioworld.com
Module: kamailio
Branch: master
Commit: 48f4da344bd96e9a19b7c99c37d9dfe29de78933
URL: https://github.com/kamailio/kamailio/commit/48f4da344bd96e9a19b7c99c37d9dfe…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2024-02-23T19:28:52+01:00
Makefile.defs: version set to 5.9.0-dev0
- master branch is open for new features to be part of the future major
series, to be versioned 5.9.x or maybe 6.0.x
---
Modified: src/Makefile.defs
---
Diff: https://github.com/kamailio/kamailio/commit/48f4da344bd96e9a19b7c99c37d9dfe…
Patch: https://github.com/kamailio/kamailio/commit/48f4da344bd96e9a19b7c99c37d9dfe…
---
diff --git a/src/Makefile.defs b/src/Makefile.defs
index b195c7fe59d..4165ca084fb 100644
--- a/src/Makefile.defs
+++ b/src/Makefile.defs
@@ -105,9 +105,9 @@ INSTALL_FLAVOUR=$(FLAVOUR)
# version number
VERSION = 5
-PATCHLEVEL = 8
+PATCHLEVEL = 9
SUBLEVEL = 0
-EXTRAVERSION = -pre1
+EXTRAVERSION = -dev0
# memory manager switcher
# 0 - f_malloc (fast malloc)
Module: kamailio
Branch: master
Commit: 17536b1064b40c305f28c4c19392504afc0b9701
URL: https://github.com/kamailio/kamailio/commit/17536b1064b40c305f28c4c19392504…
Author: Dennis Yurasov <dennis-y(a)yandex-team.ru>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2024-02-23T12:36:20+01:00
dispatcher: added two new flags to mode parameter of ds_is_from_list function for more strictly matching
- Two new flgs added:
DS_MATCH_SOCKET (8) to take in account socket/sockname attribute of gw
DS_MATCH_TRY_FULLADDRSOCK (16) try to find the most complete "address/protocol/port/local socket" combination for all dispatcher targets
---
Modified: src/modules/dispatcher/dispatch.c
Modified: src/modules/dispatcher/dispatch.h
Modified: src/modules/dispatcher/doc/dispatcher_admin.xml
---
Diff: https://github.com/kamailio/kamailio/commit/17536b1064b40c305f28c4c19392504…
Patch: https://github.com/kamailio/kamailio/commit/17536b1064b40c305f28c4c19392504…