Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a single context for all incoming SSL connections from a given transport. #3071

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ jobs:
image: savonet/liquidsoap-ci:debian_bullseye_amd64
options: --user root --privileged --ulimit core=-1 --security-opt seccomp=unconfined -v ${{ github.workspace }}/${{ github.run_number }}:/tmp/${{ github.run_number }}
strategy:
fail-fast: false
matrix:
target: ["@citest", "@mediatest"]
env:
Expand Down
34 changes: 19 additions & 15 deletions src/core/builtins/builtins_http_ssl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@

*****************************************************************************)

let get_ctx ~password ~certificate ~key () =
let ctx = Ssl.create_context Ssl.SSLv23 Ssl.Server_context in
ignore
(Option.map
(fun password -> Ssl.set_password_callback ctx (fun _ -> password))
password);
Ssl.use_certificate ctx certificate key;
ctx

let set_socket_default ~read_timeout ~write_timeout fd =
ignore (Option.map (Unix.setsockopt_float fd Unix.SO_RCVTIMEO) read_timeout);
ignore (Option.map (Unix.setsockopt_float fd Unix.SO_SNDTIMEO) write_timeout)
Expand Down Expand Up @@ -70,7 +61,7 @@ let transport ~read_timeout ~write_timeout ~password ~certificate ~key () =
Ssl.set_verify ctx [] (Some Ssl.client_verify_callback);
(* Add certificate from transport if passed. *)
(try
let cert = Utils.read_all (certificate ()) in
let cert = Utils.read_all certificate in
Ssl.add_cert_to_store ctx cert
with _ -> ());
Ssl.set_verify_depth ctx 3;
Expand Down Expand Up @@ -111,13 +102,26 @@ let transport ~read_timeout ~write_timeout ~password ~certificate ~key () =
let bt = Printexc.get_raw_backtrace () in
Lang.raise_as_runtime ~bt ~kind:"ssl" exn

val context = Atomic.make None

method private context =
match Atomic.get context with
| Some ctx -> ctx
| None ->
let ctx = Ssl.create_context Ssl.SSLv23 Ssl.Server_context in
ignore
(Option.map
(fun password ->
Ssl.set_password_callback ctx (fun _ -> password))
password);
Ssl.use_certificate ctx certificate key;
Atomic.set context (Some ctx);
ctx

method accept sock =
let s, caller = Unix.accept ~cloexec:true sock in
set_socket_default ~read_timeout ~write_timeout s;
let ctx =
get_ctx ~password ~certificate:(certificate ()) ~key:(key ()) ()
in
let ssl_s = Ssl.embed_socket s ctx in
let ssl_s = Ssl.embed_socket s self#context in
Ssl.accept ssl_s;
(ssl_socket self ssl_s, caller)
end
Expand Down Expand Up @@ -168,7 +172,7 @@ let _ =
~message:("Cannot find SSL " ^ name ^ " file!")
"not_found"
in
let find name () =
let find name =
match Lang.to_valued_option Lang.to_string (List.assoc name p) with
| None -> raise name
| Some f when not (Sys.file_exists f) -> raise name
Expand Down
10 changes: 9 additions & 1 deletion tests/harbor/http.liq
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ def f() =
test.equals("#{resp}", "")

# transport conflict
transport = http.transport.ssl(certificate="foo", key="bla")
certificate = file.temp("cert", "pem")
file.write(data="foo", certificate)
on_shutdown({file.remove(certificate)})

key = file.temp("cert", "key")
file.write(data="foo", key)
on_shutdown({file.remove(key)})

transport = http.transport.ssl(certificate=certificate, key=key)
try
harbor.http.register("/default", transport=transport, port=3456, fun (_, _) -> ())
test.fail()
Expand Down