Skip to content

Commit c59f2fe

Browse files
committed
Add the Miou implementation of TLS
1 parent 556033f commit c59f2fe

File tree

6 files changed

+820
-0
lines changed

6 files changed

+820
-0
lines changed

miou/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(library
2+
(name tls_miou)
3+
(public_name tls-miou)
4+
(libraries miou.unix tls)
5+
(instrumentation
6+
(backend bisect_ppx)))

miou/tests/dune

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(test
2+
(name fuzz)
3+
(libraries
4+
mirage-crypto-rng.unix
5+
ohex
6+
rresult
7+
ptime
8+
ptime.clock.os
9+
crowbar
10+
tls-miou)
11+
(instrumentation
12+
(backend bisect_ppx)))

miou/tests/fuzz.ml

Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
let rec random_path ?(tries = 10) fmt =
2+
if tries <= 0 then failwith "Impossible to generate an available random path";
3+
let res = Bytes.create 6 in
4+
for i = 0 to Bytes.length res - 1 do
5+
let chr =
6+
match Random.int (10 + 26 + 26) with
7+
| n when n < 10 -> Char.chr (Char.code '0' + n)
8+
| n when n < 10 + 26 -> Char.chr (Char.code 'a' + n - 10)
9+
| n -> Char.chr (Char.code 'A' + n - 10 - 26)
10+
in
11+
Bytes.set res i chr
12+
done;
13+
let path = Fmt.str fmt (Bytes.unsafe_to_string res) in
14+
if Sys.file_exists path then random_path ~tries:(pred tries) fmt else path
15+
16+
let unlink_if_exists path =
17+
try Unix.unlink path with Unix.Unix_error (Unix.ENOENT, _, _) -> ()
18+
19+
let bind_and_listen ?(backlog = 16) () =
20+
let tmp = random_path "socket-%s.socket" in
21+
unlink_if_exists tmp;
22+
let socket = Unix.socket ~cloexec:true Unix.PF_UNIX Unix.SOCK_STREAM 0 in
23+
let addr = Unix.ADDR_UNIX tmp in
24+
Unix.bind socket addr;
25+
Unix.listen socket backlog;
26+
(Miou_unix.of_file_descr ~non_blocking:true socket, addr, tmp)
27+
28+
module Ca = struct
29+
open Rresult
30+
31+
let prefix =
32+
X509.Distinguished_name.
33+
[ Relative_distinguished_name.singleton (CN "Fuzzer") ]
34+
35+
let cacert_dn =
36+
X509.Distinguished_name.(
37+
prefix
38+
@ [ Relative_distinguished_name.singleton (CN "Ephemeral CA for fuzzer") ])
39+
40+
let cacert_lifetime = Ptime.Span.v (365, 0L)
41+
let cacert_serial_number = Z.zero
42+
43+
let make domain_name seed =
44+
Domain_name.of_string domain_name >>= Domain_name.host
45+
>>= fun domain_name ->
46+
let private_key =
47+
let seed = Cstruct.of_string (Base64.decode_exn ~pad:false seed) in
48+
let g = Mirage_crypto_rng.(create ~seed (module Fortuna)) in
49+
Mirage_crypto_pk.Rsa.generate ~g ~bits:2048 ()
50+
in
51+
let valid_from = Ptime.v (Ptime_clock.now_d_ps ()) in
52+
Ptime.add_span valid_from cacert_lifetime
53+
|> Option.to_result ~none:(R.msgf "End time out of range")
54+
>>= fun valid_until ->
55+
X509.Signing_request.create cacert_dn (`RSA private_key) >>= fun ca_csr ->
56+
let extensions =
57+
let open X509.Extension in
58+
let key_id =
59+
X509.Public_key.id X509.Signing_request.((info ca_csr).public_key)
60+
in
61+
let authority_key_id =
62+
( Some key_id,
63+
X509.General_name.(singleton Directory [ cacert_dn ]),
64+
Some cacert_serial_number )
65+
in
66+
empty
67+
|> add Subject_alt_name
68+
( true,
69+
X509.General_name.(
70+
singleton DNS [ Domain_name.to_string domain_name ]) )
71+
|> add Basic_constraints (true, (false, None))
72+
|> add Key_usage
73+
(true, [ `Digital_signature; `Content_commitment; `Key_encipherment ])
74+
|> add Subject_key_id (false, key_id)
75+
|> add Authority_key_id (false, authority_key_id)
76+
in
77+
X509.Signing_request.sign ~valid_from ~valid_until ~extensions
78+
~serial:cacert_serial_number ca_csr (`RSA private_key) cacert_dn
79+
|> R.reword_error (R.msgf "%a" X509.Validation.pp_signature_error)
80+
>>= fun certificate ->
81+
let fingerprint = X509.Certificate.fingerprint `SHA256 certificate in
82+
let time () = Some (Ptime_clock.now ()) in
83+
let authenticator =
84+
X509.Authenticator.server_cert_fingerprint ~time ~hash:`SHA256
85+
~fingerprint
86+
in
87+
Ok (certificate, `RSA private_key, authenticator)
88+
end
89+
90+
let fuzz_coop = "fuzz.coop"
91+
let mutex = Miou.Mutex.create ()
92+
let epr fmt = Miou.Mutex.protect mutex @@ fun () -> Fmt.epr fmt
93+
94+
type operation =
95+
| Send of string
96+
| Recv of int
97+
| Shutdown of [ `read | `write ]
98+
| Close
99+
| Noop
100+
101+
module Stop = struct
102+
type t = {
103+
mutex : Miou.Mutex.t;
104+
condition : Miou.Condition.t;
105+
mutable stop : bool;
106+
}
107+
108+
let create () =
109+
let mutex = Miou.Mutex.create () in
110+
let condition = Miou.Condition.create () in
111+
{ mutex; condition; stop = false }
112+
113+
let stop t =
114+
Miou.Mutex.protect t.mutex @@ fun () ->
115+
t.stop <- true;
116+
Miou.Condition.broadcast t.condition
117+
118+
let wait t =
119+
Miou.Mutex.protect t.mutex @@ fun () ->
120+
while t.stop = false do
121+
Miou.Condition.wait t.condition t.mutex
122+
done
123+
end
124+
125+
let inhibit fn = try fn () with _exn -> ()
126+
127+
let run ~role:_ actions tls =
128+
let rec go buf tls = function
129+
| [] -> Buffer.contents buf
130+
| Noop :: actions ->
131+
Miou.yield ();
132+
go buf tls actions
133+
| Send str :: actions ->
134+
Tls_miou.write tls str;
135+
go buf tls actions
136+
| Close :: actions ->
137+
Tls_miou.close tls;
138+
go buf tls actions
139+
| Shutdown cmd :: actions ->
140+
Tls_miou.shutdown tls (cmd :> [ `read | `write | `read_write ]);
141+
go buf tls actions
142+
| Recv len :: actions ->
143+
let tmp = Bytes.make len '\000' in
144+
Tls_miou.really_read tls tmp;
145+
Buffer.add_subbytes buf tmp 0 len;
146+
go buf tls actions
147+
in
148+
let buf = Buffer.create 0x100 in
149+
try go buf tls actions with
150+
| End_of_file | Tls_miou.Closed_by_peer | Tls_miou.Tls_alert _
151+
| Tls_miou.Tls_failure _ ->
152+
inhibit (fun () -> Miou_unix.close (Tls_miou.file_descr tls));
153+
Buffer.contents buf
154+
| exn ->
155+
inhibit (fun () -> Miou_unix.close (Tls_miou.file_descr tls));
156+
raise exn
157+
158+
let run_client ~to_client:actions cfg addr =
159+
let domain = Unix.domain_of_sockaddr addr in
160+
let socket = Unix.socket ~cloexec:true domain Unix.SOCK_STREAM 0 in
161+
Unix.connect socket addr;
162+
let fd = Miou_unix.of_file_descr ~non_blocking:true socket in
163+
let tls = Tls_miou.client_of_fd cfg fd in
164+
let finally () =
165+
inhibit (fun () -> Miou_unix.close (Tls_miou.file_descr tls))
166+
in
167+
Fun.protect ~finally @@ fun () -> run ~role:"client" actions tls
168+
169+
let rec cleanup orphans clients =
170+
match Miou.care orphans with
171+
| None | Some None -> clients
172+
| Some (Some prm) ->
173+
let clients = Miou.await prm :: clients in
174+
cleanup orphans clients
175+
176+
let rec terminate orphans clients =
177+
match Miou.care orphans with
178+
| None -> List.rev clients
179+
| Some None ->
180+
Miou.yield ();
181+
terminate orphans clients
182+
| Some (Some prm) ->
183+
let clients = Miou.await prm :: clients in
184+
terminate orphans clients
185+
186+
exception Stop
187+
188+
let run_server ~to_server:actions ~stop fd cfg =
189+
let rec go orphans clients =
190+
let clients = cleanup orphans clients in
191+
let accept = Miou.call_cc @@ fun () -> Miou_unix.accept ~cloexec:true fd in
192+
let stop =
193+
Miou.call_cc @@ fun () ->
194+
Stop.wait stop;
195+
raise Stop
196+
in
197+
match Miou.await_first [ accept; stop ] with
198+
| Error _ ->
199+
inhibit (fun () -> Miou_unix.close fd);
200+
terminate orphans clients
201+
| Ok (fd, _) ->
202+
ignore
203+
( Miou.call_cc ~orphans @@ fun () ->
204+
match Tls_miou.server_of_fd cfg fd with
205+
| tls -> run ~role:"server" actions tls
206+
| exception _ ->
207+
Miou_unix.close fd;
208+
String.empty );
209+
go orphans clients
210+
in
211+
go (Miou.orphans ()) []
212+
213+
let compile to_client to_server =
214+
let close_client close = function
215+
| Close -> close lor 0b1100
216+
| Shutdown `read -> close lor 0b1000
217+
| Shutdown `write -> close lor 0b0100
218+
| _ -> close
219+
in
220+
let close_server close = function
221+
| Close -> close lor 0b0011
222+
| Shutdown `read -> close lor 0b0010
223+
| Shutdown `write -> close lor 0b0001
224+
| _ -> close
225+
in
226+
let client = Buffer.create 0x100 in
227+
let server = Buffer.create 0x100 in
228+
let rec go close to_client to_server =
229+
match (close, to_client, to_server) with
230+
| _, [], _ | _, _, [] -> ()
231+
| close, ((Shutdown _ | Close) as operation) :: to_client, _ ->
232+
go (close_client close operation) to_client to_server
233+
| close, _, ((Shutdown _ | Close) as operation) :: to_server ->
234+
go (close_server close operation) to_client to_server
235+
| close, Noop :: to_client, to_server | close, to_client, Noop :: to_server
236+
->
237+
go close to_client to_server
238+
| close, Send str :: to_client, Recv n :: to_server ->
239+
assert (String.length str = n);
240+
if close land 0b0100 = 0 && close land 0b0010 = 0 then
241+
Buffer.add_string server str;
242+
if close land 0b0100 = 0 && close land 0b0010 = 0 then
243+
go close to_client to_server
244+
| close, Recv n :: to_client, Send str :: to_server ->
245+
assert (String.length str = n);
246+
if close land 0b1000 = 0 && close land 0b0001 = 0 then
247+
Buffer.add_string client str;
248+
if close land 0b1000 = 0 && close land 0b0001 = 0 then
249+
go close to_client to_server
250+
| _, Send _ :: _, Send _ :: _ | _, Recv _ :: _, Recv _ :: _ ->
251+
assert false (* GADT? *)
252+
in
253+
go 0x0 to_client to_server;
254+
(Buffer.contents client, Buffer.contents server)
255+
256+
let run seed operations =
257+
Miou_unix.run ~domains:1 @@ fun () ->
258+
let fd, addr, path = bind_and_listen () in
259+
let finally () = Unix.unlink path in
260+
Fun.protect ~finally @@ fun () ->
261+
let cert, pk, authenticator =
262+
Rresult.R.failwith_error_msg (Ca.make fuzz_coop seed)
263+
in
264+
let cfg_server =
265+
Tls.Config.server ~certificates:(`Single ([ cert ], pk)) ()
266+
in
267+
let cfg_client = Tls.Config.client ~authenticator () in
268+
let to_client, to_server = List.split operations in
269+
let stop = Stop.create () in
270+
let prm0 = Miou.call_cc @@ fun () -> run_server ~to_server ~stop fd cfg_server in
271+
let prm1 =
272+
Miou.call_cc @@ fun () ->
273+
let finally () = Stop.stop stop in
274+
Fun.protect ~finally @@ fun () -> run_client ~to_client cfg_client addr
275+
in
276+
let send_to_client, send_to_server = compile to_client to_server in
277+
match (Miou.await prm0, Miou.await prm1) with
278+
| Ok [ Ok send_to_server' ], Ok send_to_client' ->
279+
Crowbar.check (String.equal send_to_client send_to_client');
280+
Crowbar.check (String.equal send_to_server send_to_server');
281+
let n = String.length send_to_client in
282+
let m = String.length send_to_server in
283+
epr "[%a] %db %db transmitted\n%!" Fmt.(styled `Green string) "OK" n m
284+
| _ ->
285+
Crowbar.failf "[%a] Unexpected result\n%!" Fmt.(styled `Red string) "ERROR"
286+
287+
let label name gen = Crowbar.with_printer Fmt.(const string name) gen
288+
289+
let direction =
290+
let open Crowbar in
291+
choose
292+
[
293+
label "server-to-client" (const `To_client);
294+
label "client-to-server" (const `To_server);
295+
]
296+
297+
let shutdown =
298+
let open Crowbar in
299+
choose
300+
[
301+
label "close" (const Close);
302+
label "shutdown-recv" (const (Shutdown `read));
303+
label "shutdown-send" (const (Shutdown `write));
304+
label "noop" (const Noop);
305+
]
306+
307+
let operation =
308+
let open Crowbar in
309+
map [ direction; bytes ] @@ fun direction str ->
310+
match (direction, str) with
311+
| _, "" -> (Noop, Noop)
312+
| `To_server, str -> (Send str, Recv (String.length str))
313+
| `To_client, str -> (Recv (String.length str), Send str)
314+
315+
let counter = Atomic.make 0
316+
317+
let operations =
318+
let open Crowbar in
319+
fix @@ fun m ->
320+
let continue (to_client, to_server) =
321+
if Atomic.fetch_and_add counter 1 >= 4 then const [ (Close, Close) ]
322+
else map [ m ] @@ fun ops -> (to_client, to_server) :: ops
323+
in
324+
map
325+
[ list1 operation; dynamic_bind (pair shutdown shutdown) continue ]
326+
List.rev_append
327+
328+
let seed = Crowbar.(map [ bytes ] Base64.encode_exn)
329+
330+
let () =
331+
Sys.set_signal Sys.sigpipe Sys.Signal_ignore;
332+
Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna);
333+
Crowbar.add_test ~name:"run" Crowbar.[ seed; operations ] @@ fun seed operations ->
334+
run seed operations;
335+
Atomic.set counter 0

0 commit comments

Comments
 (0)