Skip to content

Commit cbe985c

Browse files
thomassarobhoes
authored andcommitted
CA-269137: get_nbd_info: get subject from TLS cert
Use the x509 library to parse the certificate and read its DNS subject names, if any. (This replaces the stopgap of using Host.hostname for the value of the "subject" field of the vdi_nbd_server_info objects.) Signed-off-by: Thomas Sanders <[email protected]>
1 parent dde58cc commit cbe985c

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

ocaml/xapi/certificates.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,8 @@ let get_server_certificate () =
289289
warn "Exception reading server certificate: %s"
290290
(ExnHelper.string_of_exn e);
291291
raise_library_corrupt()
292+
293+
let hostnames_of_pem_cert pem =
294+
Cstruct.of_string pem |>
295+
X509.Encoding.Pem.Certificate.of_pem_cstruct1 |>
296+
X509.hostnames

ocaml/xapi/jbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ let () = Printf.ksprintf Jbuild_plugin.V1.send {|
6666
(package xapi)
6767
(flags (:standard -bin-annot %s -warn-error +a-3-4-6-9-27-28-29-52))
6868
(libraries (
69+
x509
6970
oPasswd
7071
pam
7172
pciutil

ocaml/xapi/xapi_vdi.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,13 @@ let _get_nbd_info ~__context ~self ~get_server_certificate =
10721072
if ips = [] then [] else
10731073
let cert = get_server_certificate ~host in
10741074
let port = 10809L in
1075-
(* Stopgap measure: use hostname instead of reading a subject out of the cert. *)
1076-
let subject = Db.Host.get_hostname ~__context ~self:host in
1075+
let subject = match Certificates.hostnames_of_pem_cert cert with
1076+
| [] -> (
1077+
error "Found no subject DNS names in this hosts's certificate. Returning empty string as subject.";
1078+
""
1079+
)
1080+
| name :: _ -> name
1081+
in
10771082
let template = API.{
10781083
vdi_nbd_server_info_exportname = exportname;
10791084
vdi_nbd_server_info_address = "";

xapi.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ build-test: [[ "jbuilder" "runtest" "-p" name "-j" jobs ]]
1010

1111
depends: [
1212
"jbuilder" {build & >= "1.0+beta11"}
13+
"x509"
1314
"cdrom"
1415
"fd-send-recv"
1516
"nbd"

0 commit comments

Comments
 (0)