···5959 der_list := Cstruct.of_string der_cert :: !der_list)
6060 with
6161 | () -> Ok !der_list
6262- | exception Failure msg -> Rresult.R.error_msg msg
6262+ | exception Failure msg -> Error (`Msg msg)
6363+6464+let ( let* ) = Result.bind
63656466let rec map_m f l =
6567 match l with
6668 | [] -> Ok []
6769 | x :: xs ->
6868- let open Rresult.R in
6969- f x >>= fun y ->
7070- map_m f xs >>| fun ys -> y :: ys
7070+ let* y = f x in
7171+ let* ys = map_m f xs in
7272+ Ok (y :: ys)
71737274(** Load certificates from Windows' ["ROOT"] system certificate store.
7375 The C API returns a list of DER-encoded certificates. These are decoded and
7476 reencoded as a single PEM certificate. *)
7577let windows_trust_anchors () =
7676- let open Rresult.R in
7777- get_anchors () >>= map_m X509.Certificate.decode_der >>| fun cert_list ->
7878- X509.Certificate.encode_pem_multiple cert_list |> Cstruct.to_string
7878+ let* anchors = get_anchors () in
7979+ let* cert_list = map_m X509.Certificate.decode_der anchors in
8080+ Ok (X509.Certificate.encode_pem_multiple cert_list |> Cstruct.to_string)
79818082let trust_anchors () =
8181- let open Rresult.R.Infix in
8283 if Sys.win32 then windows_trust_anchors ()
8384 else
8485 (* NixOS is special and sets "NIX_SSL_CERT_FILE" as location during builds *)
···8889 detect_one x
8990 | None -> (
9091 let cmd = Bos.Cmd.(v "uname" % "-s") in
9191- Bos.OS.Cmd.(run_out cmd |> out_string |> success) >>= function
9292+ let* os = Bos.OS.Cmd.(run_out cmd |> out_string |> success) in
9393+ match os with
9294 | "FreeBSD" -> detect_one freebsd_location
9395 | "OpenBSD" -> detect_one openbsd_location
9496 | "Linux" -> detect_list linux_locations
···102104 | s -> Error (`Msg ("ca-certs: unknown system " ^ s ^ ".\n" ^ issue)))
103105104106let authenticator ?crls ?allowed_hashes () =
105105- let open Rresult.R.Infix in
106106- trust_anchors () >>= fun data ->
107107+ let* data = trust_anchors () in
107108 let time () = Some (Ptime_clock.now ()) in
108109 (* we cannot use decode_pem_multiple since this fails on the first
109110 undecodable certificate - while we'd like to stay operational, and ignore
110111 some certificates *)
111111- let sep = "-----END CERTIFICATE-----" in
112112+ let d = "-----" in
113113+ let sep = d ^ "END CERTIFICATE" ^ d in
112114 let certs = Astring.String.cuts ~sep ~empty:false data in
113115 let cas =
116116+ let affix = d ^ "BEGIN CERTIFICATE" ^ d in
114117 List.fold_left
115118 (fun acc data ->
116116- let data = data ^ sep in
117117- match X509.Certificate.decode_pem (Cstruct.of_string data) with
118118- | Ok ca -> ca :: acc
119119- | Error (`Msg msg) ->
120120- Log.warn (fun m -> m "Failed to decode a trust anchor %s." msg);
121121- Log.debug (fun m -> m "Full certificate:@.%s" data);
122122- acc)
119119+ if not (Astring.String.is_infix ~affix data) then acc
120120+ else
121121+ let data = data ^ sep in
122122+ match X509.Certificate.decode_pem (Cstruct.of_string data) with
123123+ | Ok ca -> ca :: acc
124124+ | Error (`Msg msg) ->
125125+ Log.warn (fun m -> m "Failed to decode a trust anchor %s." msg);
126126+ Log.debug (fun m -> m "Full certificate:@.%s" data);
127127+ acc)
123128 [] certs
124129 in
125130 let cas = List.rev cas in
···940940 (fun (name, data) ->
941941 let host = Domain_name.(of_string_exn name |> host_exn)
942942 and chain =
943943- Rresult.R.get_ok
943943+ Result.get_ok
944944 (X509.Certificate.decode_pem_multiple (Cstruct.of_string data))
945945 in
946946 (name, `Quick, test_one tas (Ok (Some (chain, List.hd chain))) host chain))
···949949 (fun (name, result, data, time) ->
950950 let host = Domain_name.(of_string_exn name |> host_exn)
951951 and chain =
952952- Rresult.R.get_ok
952952+ Result.get_ok
953953 (X509.Certificate.decode_pem_multiple (Cstruct.of_string data))
954954 in
955955 (name, `Quick, test_one ?time tas (Error (result host chain)) host chain))
956956 err_tests
957957958958let ta () =
959959- let open Rresult.R.Infix in
960960- Ca_certs.trust_anchors () >>| fun data ->
961961- (* we cannot use decode_pem_multiple since this fails on the first
962962- undecodable certificate - while we'd like to stay operational, and ignore
963963- some certificates *)
964964- let sep = "-----END CERTIFICATE-----" in
965965- let certs = Astring.String.cuts ~sep ~empty:false data in
966966- let cas =
967967- List.fold_left
968968- (fun acc data ->
969969- let data = data ^ sep in
970970- match X509.Certificate.decode_pem (Cstruct.of_string data) with
971971- | Ok ca -> ca :: acc
972972- | Error _ -> acc)
973973- [] certs
974974- in
975975- List.rev cas
959959+ Result.bind (Ca_certs.trust_anchors ()) (fun data ->
960960+ (* we cannot use decode_pem_multiple since this fails on the first
961961+ undecodable certificate - while we'd like to stay operational, and
962962+ ignore some certificates *)
963963+ let sep = "-----END CERTIFICATE-----" in
964964+ let certs = Astring.String.cuts ~sep ~empty:false data in
965965+ let cas =
966966+ List.fold_left
967967+ (fun acc data ->
968968+ let data = data ^ sep in
969969+ match X509.Certificate.decode_pem (Cstruct.of_string data) with
970970+ | Ok ca -> ca :: acc
971971+ | Error _ -> acc)
972972+ [] certs
973973+ in
974974+ Ok (List.rev cas))
976975977976let () =
978978- let tas = Rresult.R.get_ok (ta ()) in
977977+ let tas = Result.get_ok (ta ()) in
979978 Alcotest.run "verification tests"
980979 [ ("X509 certificate validation", tests tas) ]