Skip to content

Commit bd5fe18

Browse files
committed
Adapt code to new mirage-crypto (CP-308222)
Unfortunately mirage-crypto has accumulated breaking changes: - Cstructs have been replaced with strings - The digestif library has replaced ad-hoc hash implementation A deprecation has happened as well: - RNG initialization has changed Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent 44ae9d5 commit bd5fe18

16 files changed

+35
-50
lines changed

ocaml/gencert/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(libraries
77
angstrom
88
astring
9-
cstruct
9+
digestif
1010
forkexec
1111
mirage-crypto
1212
mirage-crypto-pk
@@ -52,7 +52,7 @@
5252
(modules test_lib test_pem)
5353
(libraries
5454
alcotest
55-
cstruct
55+
digestif
5656
fmt
5757
gencertlib
5858
mirage-crypto

ocaml/gencert/lib.ml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ let validate_private_key pkcs8_private_key =
3434
let key_type = X509.(Key_type.to_string (Private_key.key_type key)) in
3535
Error (`Msg (server_certificate_key_algorithm_not_supported, [key_type]))
3636
in
37-
let raw_pem = Cstruct.of_string pkcs8_private_key in
38-
X509.Private_key.decode_pem raw_pem
37+
X509.Private_key.decode_pem pkcs8_private_key
3938
|> R.reword_error (fun (`Msg err_msg) ->
4039
let unknown_algorithm = "Unknown algorithm " in
4140
if Astring.String.is_prefix ~affix:"multi-prime RSA" err_msg then
@@ -56,9 +55,8 @@ let validate_private_key pkcs8_private_key =
5655
)
5756
>>= ensure_rsa_key_length
5857

59-
let pem_of_string x ~error_invalid =
60-
let raw_pem = Cstruct.of_string x in
61-
X509.Certificate.decode_pem raw_pem
58+
let decode_cert pem ~error_invalid =
59+
X509.Certificate.decode_pem pem
6260
|> R.reword_error (fun (`Msg err_msg) ->
6361
D.info {|Failed to validate certificate because "%s"|} err_msg ;
6462
`Msg (error_invalid, [])
@@ -76,7 +74,7 @@ let assert_not_expired ~now certificate ~error_not_yet ~error_expired =
7674

7775
let _validate_not_expired ~now (blob : string) ~error_invalid ~error_not_yet
7876
~error_expired =
79-
pem_of_string blob ~error_invalid >>= fun cert ->
77+
decode_cert blob ~error_invalid >>= fun cert ->
8078
assert_not_expired ~now cert ~error_not_yet ~error_expired
8179

8280
let validate_not_expired x ~error_not_yet ~error_expired ~error_invalid =
@@ -101,8 +99,7 @@ let validate_pem_chain ~pem_leaf ~pem_chain now private_key =
10199
Error (`Msg (server_certificate_signature_not_supported, []))
102100
in
103101
let validate_chain pem_chain =
104-
let raw_pem = Cstruct.of_string pem_chain in
105-
X509.Certificate.decode_pem_multiple raw_pem |> function
102+
X509.Certificate.decode_pem_multiple pem_chain |> function
106103
| Ok (_ :: _ as certs) ->
107104
Ok certs
108105
| Ok [] ->
@@ -135,17 +132,13 @@ let install_server_certificate ~pem_chain ~pem_leaf ~pkcs8_private_key
135132
~server_cert_path ~cert_gid =
136133
let now = Ptime_clock.now () in
137134
validate_private_key pkcs8_private_key >>= fun priv ->
138-
let pkcs8_private_key =
139-
X509.Private_key.encode_pem priv |> Cstruct.to_string
140-
in
135+
let pkcs8_private_key = X509.Private_key.encode_pem priv in
141136
validate_pem_chain ~pem_leaf ~pem_chain now priv >>= fun (cert, chain) ->
142-
let pem_leaf = X509.Certificate.encode_pem cert |> Cstruct.to_string in
137+
let pem_leaf = X509.Certificate.encode_pem cert in
143138
Option.fold
144139
~none:(Ok [pkcs8_private_key; pem_leaf])
145140
~some:(fun chain ->
146-
let pem_chain =
147-
X509.Certificate.encode_pem_multiple chain |> Cstruct.to_string
148-
in
141+
let pem_chain = X509.Certificate.encode_pem_multiple chain in
149142
Ok [pkcs8_private_key; pem_leaf; pem_chain]
150143
)
151144
chain

ocaml/gencert/selfcert.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let valid_from' date =
4343

4444
(* Needed to initialize the rng to create random serial codes when signing
4545
certificates *)
46-
let () = Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna)
46+
let () = Mirage_crypto_rng_unix.use_default ()
4747

4848
(** [write_cert] writes a PKCS12 file to [path]. The typical file
4949
extension would be ".pem". It attempts to do that atomically by
@@ -117,7 +117,6 @@ let generate_pub_priv_key length =
117117
in
118118
let* privkey =
119119
rsa_string
120-
|> Cstruct.of_string
121120
|> X509.Private_key.decode_pem
122121
|> R.reword_error (fun _ -> R.msg "decoding private key failed")
123122
in
@@ -132,9 +131,7 @@ let selfsign' issuer extensions key_length expiration =
132131
let* cert = sign expiration privkey pubkey issuer req extensions in
133132
let key_pem = X509.Private_key.encode_pem privkey in
134133
let cert_pem = X509.Certificate.encode_pem cert in
135-
let pkcs12 =
136-
String.concat "\n\n" [Cstruct.to_string key_pem; Cstruct.to_string cert_pem]
137-
in
134+
let pkcs12 = String.concat "\n\n" [key_pem; cert_pem] in
138135
Ok (cert, pkcs12)
139136

140137
let selfsign issuer extensions key_length expiration certfile cert_gid =

ocaml/gencert/selfcert.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ val write_certs : string -> int -> string -> (unit, [> Rresult.R.msg]) result
2323
val host :
2424
name:string
2525
-> dns_names:string list
26-
-> ips:Cstruct.t list
26+
-> ips:string list
2727
-> ?valid_from:Ptime.t (* default: now *)
2828
-> valid_for_days:int
2929
-> string

ocaml/gencert/test_lib.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Rresult.R.Infix
88
let ( let* ) = Rresult.R.bind
99

1010
(* Initialize RNG for testing certificates *)
11-
let () = Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna)
11+
let () = Mirage_crypto_rng_unix.use_default ()
1212

1313
let time_of_rfc3339 date =
1414
match Ptime.of_rfc3339 date with
@@ -204,7 +204,7 @@ let test_invalid_cert pem_leaf time pkey error reason =
204204
"Error must match" (error, reason) msg
205205

206206
let load_pkcs8 name =
207-
X509.Private_key.decode_pem (Cstruct.of_string (load_test_data name))
207+
X509.Private_key.decode_pem (load_test_data name)
208208
|> Rresult.R.reword_error (fun (`Msg msg) ->
209209
`Msg
210210
(Printf.sprintf "Could not load private key with name '%s': %s" name
@@ -222,7 +222,6 @@ let sign_leaf_cert host_name digest pkey_leaf =
222222
load_pkcs8 "pkey_rsa_4096" >>= fun pkey_sign ->
223223
sign_cert host_name ~pkey_sign digest pkey_leaf
224224
>>| X509.Certificate.encode_pem
225-
>>| Cstruct.to_string
226225

227226
let valid_leaf_cert_tests =
228227
List.map
@@ -300,8 +299,7 @@ let valid_chain_cert_tests =
300299
(pkey_root, Ok []) key_chain
301300
in
302301
sign_leaf_cert host_name `SHA256 pkey_leaf >>= fun pem_leaf ->
303-
chain >>| X509.Certificate.encode_pem_multiple >>| Cstruct.to_string
304-
>>| fun pem_chain ->
302+
chain >>| X509.Certificate.encode_pem_multiple >>| fun pem_chain ->
305303
test_valid_cert_chain ~pem_leaf ~pem_chain time pkey_leaf
306304
in
307305
[("Validation of a supported certificate chain", `Quick, test_cert)]

ocaml/tests/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
angstrom
1616
astring
1717
cstruct
18-
18+
digestif
1919
fmt
2020
http_lib
2121
httpsvr

ocaml/tests/test_certificates.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let pp_hash_test =
1313
(fun (hashable, expected) ->
1414
let test_hash () =
1515
let digest =
16-
Cstruct.of_string hashable |> Mirage_crypto.Hash.digest `SHA256
16+
Digestif.SHA256.(digest_string hashable |> to_raw_string)
1717
in
1818
Alcotest.(check string)
1919
"fingerprints must match" expected

ocaml/xapi-aux/networking_info.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ let dns_names () =
5555
)
5656
|> Astring.String.uniquify
5757

58-
let ipaddr_to_cstruct = function
58+
let ipaddr_to_octets = function
5959
| Ipaddr.V4 addr ->
60-
Cstruct.of_string (Ipaddr.V4.to_octets addr)
60+
Ipaddr.V4.to_octets addr
6161
| Ipaddr.V6 addr ->
62-
Cstruct.of_string (Ipaddr.V6.to_octets addr)
62+
Ipaddr.V6.to_octets addr
63+
64+
let ipaddr_to_cstruct c = ipaddr_to_octets c |> Cstruct.of_string
6365

6466
let get_management_ip_addrs ~dbg =
6567
let iface = Inventory.lookup Inventory._management_interface in
@@ -113,7 +115,7 @@ let get_host_certificate_subjects ~dbg =
113115
| Ok (preferred, others) ->
114116
let ips = List.(rev_append (rev preferred) others) in
115117
Option.fold ~none:(Error IP_missing)
116-
~some:(fun ip -> Ok (List.map ipaddr_to_cstruct ips, ip))
118+
~some:(fun ip -> Ok (List.map ipaddr_to_octets ips, ip))
117119
(List.nth_opt ips 0)
118120
in
119121
let dns_names = dns_names () in

ocaml/xapi-aux/networking_info.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ val get_management_ip_addr : dbg:string -> (string * Cstruct.t) option
3131

3232
val get_host_certificate_subjects :
3333
dbg:string
34-
-> (string * string list * Cstruct.t list, management_ip_error) Result.t
34+
-> (string * string list * string list, management_ip_error) Result.t
3535
(** [get_host_certificate_subjects ~dbg] returns the main, dns names and ip
3636
addresses that identify the host in secure connections. *)

ocaml/xapi/cert_refresh.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ let host ~__context ~type' =
7979
Server_error
8080
(cannot_contact_host, [Ref.string_of (HostSet.choose unreachable)])
8181
) ;
82-
let content = X509.Certificate.encode_pem cert |> Cstruct.to_string in
82+
let content = X509.Certificate.encode_pem cert in
8383
(* distribute public part of new cert in pool *)
8484
Cert_distrib.distribute_new_host_cert ~__context ~host ~content ;
8585
(* replace certs in file system on host *)

ocaml/xapi/certificates.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open D
3232
type t_trusted = CA_Certificate | CRL
3333

3434
let pem_of_string x =
35-
match Cstruct.of_string x |> X509.Certificate.decode_pem with
35+
match X509.Certificate.decode_pem x with
3636
| Error _ ->
3737
D.error "pem_of_string: failed to parse certificate string" ;
3838
raise
@@ -75,7 +75,7 @@ let to_string = function CA_Certificate -> "CA certificate" | CRL -> "CRL"
7575
adding a colon between every octet, in uppercase.
7676
*)
7777
let pp_hash hash =
78-
let hex = Hex.(show @@ of_cstruct hash) in
78+
let hex = Hex.(show @@ of_string hash) in
7979
let length = (3 * String.length hex / 2) - 1 in
8080
let value_of i =
8181
match (i + 1) mod 3 with
@@ -441,9 +441,7 @@ let get_internal_server_certificate () =
441441
open Rresult
442442

443443
let hostnames_of_pem_cert pem =
444-
Cstruct.of_string pem
445-
|> X509.Certificate.decode_pem
446-
>>| X509.Certificate.hostnames
444+
X509.Certificate.decode_pem pem >>| X509.Certificate.hostnames
447445

448446
let install_server_certificate ~pem_chain ~pem_leaf ~pkcs8_private_key ~path =
449447
let installation =

ocaml/xapi/certificates.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ type t_trusted = CA_Certificate | CRL
1818

1919
val pem_of_string : string -> X509.Certificate.t
2020

21-
val pp_hash : Cstruct.t -> string
21+
val pp_hash : string -> string
2222

23-
val pp_fingerprint :
24-
hash_type:Mirage_crypto.Hash.hash -> X509.Certificate.t -> string
23+
val pp_fingerprint : hash_type:Digestif.hash' -> X509.Certificate.t -> string
2524

2625
val validate_name : t_trusted -> string -> unit
2726

ocaml/xapi/certificates_sync.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ let get_server_cert path =
5757
| Error msg ->
5858
Error (`Msg (msg, []))
5959
| Ok cert ->
60-
let host_pem = cert.GP.host_cert in
6160
let* host_cert =
62-
Cstruct.of_string host_pem
63-
|> X509.Certificate.decode_pem
61+
X509.Certificate.decode_pem cert.GP.host_cert
6462
|> R.reword_error (fun (`Msg msg) ->
6563
D.info {|Failed to decode certificate because "%s"|} msg ;
6664
`Msg (server_certificate_invalid, [])

ocaml/xapi/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
clock
139139
cohttp
140140
cohttp_posix
141+
digestif
141142
domain-name
142143
ezxenstore.core
143144
fmt

ocaml/xapi/xapi_db_upgrade.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@ let upgrade_ca_fingerprints =
930930
try
931931
let* certificate =
932932
Xapi_stdext_unix.Unixext.string_of_file filename
933-
|> Cstruct.of_string
934933
|> X509.Certificate.decode_pem
935934
in
936935
let sha1 =

ocaml/xapi/xapi_session.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,12 @@ module Caching = struct
801801
and type password = string
802802
and type session = external_auth_result
803803

804-
let () = Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna)
804+
let () = Mirage_crypto_rng_unix.use_default ()
805805

806806
let create_salt () =
807807
(* Creates a Cstruct of length 8. *)
808808
let data = Mirage_crypto_rng.generate 8 in
809-
let bytes = Cstruct.to_bytes data in
809+
let bytes = Bytes.of_string data in
810810
(* Encode the salt as a hex string. Each byte becomes 2
811811
hexadecimal digits, so the length is 16 (the maximum for
812812
crypt_r). *)

0 commit comments

Comments
 (0)