Skip to content

Commit dc53518

Browse files
committed
adapt to X509 0.7.0 API
1 parent a8ab219 commit dc53518

File tree

8 files changed

+41
-49
lines changed

8 files changed

+41
-49
lines changed

bin/oacmel.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let main _ rsa_pem csr_pem _acme_dir ip key endpoint cert zone =
4747
| Error e -> Error e
4848
| Ok t ->
4949
Logs.info (fun m -> m "Certificate downloaded");
50-
Bos.OS.File.write cert (Cstruct.to_string @@ X509.Encoding.Pem.Certificate.to_pem_cstruct1 t)
50+
Bos.OS.File.write cert (Cstruct.to_string @@ X509.Certificate.encode_pem t)
5151
in
5252
match r with
5353
| Ok _ -> `Ok ()

letsencrypt.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ depends: [
2424
"fmt"
2525
"lwt" {>= "2.6.0"}
2626
"nocrypto"
27-
"x509"
27+
"x509" {>= "0.7.0"}
2828
"yojson" {>= "1.6.0"}
2929
"ounit" {with-test}
3030
"dns"

src/acme_client.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ open Lwt.Infix
22

33
open Acme_common
44

5-
module Pem = X509.Encoding.Pem
6-
75
type t = {
86
account_key : Nocrypto.Rsa.priv ;
97
mutable next_nonce : string ;
@@ -329,13 +327,14 @@ let rec poll_until ?ctx sleep cli challenge =
329327

330328
let body_to_certificate der =
331329
let der = Cstruct.of_string der in
332-
match X509.Encoding.parse der with
333-
| Some crt -> Ok crt
334-
| None -> Error (`Msg "I got gibberish while trying to decode the new certificate.")
330+
match X509.Certificate.decode_der der with
331+
| Ok crt -> Ok crt
332+
| Error (`Msg e) ->
333+
Error (`Msg ("I got gibberish while trying to decode the new certificate: " ^ e))
335334

336335
let new_cert ?ctx cli csr =
337336
let url = cli.d.new_cert in
338-
let der = X509.Encoding.cs_of_signing_request csr |> Cstruct.to_string |> B64u.urlencode in
337+
let der = X509.Signing_request.encode_der csr |> Cstruct.to_string |> B64u.urlencode in
339338
let data = Printf.sprintf {|{"resource": "new-cert", "csr": "%s"}|} der in
340339
http_post_jws ?ctx cli data url >|= function
341340
| Error e -> Error e
@@ -352,12 +351,13 @@ let sign_certificate ?ctx ?(solver = default_http_solver) cli sleep csr =
352351
(fun r domain ->
353352
match r with
354353
| Ok () ->
355-
new_authz ?ctx cli domain solver.get_challenge >>= fun challenge ->
356-
solver.solve_challenge sleep cli challenge domain >>= fun () ->
354+
let name = Domain_name.to_string domain in
355+
new_authz ?ctx cli name solver.get_challenge >>= fun challenge ->
356+
solver.solve_challenge sleep cli challenge name >>= fun () ->
357357
challenge_met ?ctx cli solver.name challenge >>= fun () ->
358358
poll_until ?ctx sleep cli challenge
359359
| Error r -> Lwt.return_error r)
360-
(Ok ()) domains >>= fun () ->
360+
(Ok ()) (Domain_name.Set.elements domains) >>= fun () ->
361361
new_cert ?ctx cli csr >>= fun pem ->
362362
Lwt.return_ok pem
363363

src/acme_client.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ module Make (Client : Cohttp_lwt.S.Client) : sig
2222

2323
val sign_certificate : ?ctx:Client.ctx ->
2424
?solver:solver_t -> t -> (unit -> unit Lwt.t) ->
25-
X509.CA.signing_request ->
26-
(X509.t, [ `Msg of string ]) result Lwt.t
25+
X509.Signing_request.t ->
26+
(X509.Certificate.t, [ `Msg of string ]) result Lwt.t
2727
end

src/acme_common.ml

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,25 @@ type directory_t = {
77
}
88

99
let domains_of_csr csr =
10-
let flat_map f xs = List.map f xs |> List.concat in
11-
let info = X509.CA.info csr in
10+
let open X509.Signing_request in
11+
let info = info csr in
1212
let subject_alt_names =
13-
info.X509.CA.extensions
14-
|> flat_map (function
15-
| `Extensions extensions -> List.map snd extensions
16-
| `Name _ | `Password _ -> [])
17-
|> flat_map (function
18-
| `Subject_alt_name names -> names
19-
| _ -> [])
20-
|> List.map (function
21-
| `DNS name -> name
22-
| _ -> assert false)
13+
match Ext.(find Extensions info.extensions) with
14+
| Some exts ->
15+
begin match X509.Extension.(find Subject_alt_name exts) with
16+
| None -> Domain_name.Set.empty
17+
| Some (_, san) -> match X509.General_name.(find DNS san) with
18+
| None -> Domain_name.Set.empty
19+
| Some names -> names
20+
end
21+
| _ -> Domain_name.Set.empty
2322
in
24-
match subject_alt_names with
25-
| [] ->
23+
if Domain_name.Set.is_empty subject_alt_names then
2624
(* XXX: I'm assuming there is always exactly one CN in a subject. *)
27-
info.X509.CA.subject
28-
|> List.find (function
29-
| `CN _ -> true
30-
| _ -> false)
31-
|> (function
32-
| `CN name -> [name]
33-
| _ -> assert false)
34-
| _ -> subject_alt_names
25+
let cn = X509.Distinguished_name.(get CN info.subject) in
26+
Domain_name.Set.singleton (Domain_name.of_string_exn cn)
27+
else
28+
subject_alt_names
3529

3630
let letsencrypt_url = Uri.of_string
3731
"https://acme-v01.api.letsencrypt.org/directory"

src/letsencrypt.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ module Client: sig
3737

3838
val sign_certificate : ?ctx:Client.ctx ->
3939
?solver:solver_t -> t -> (unit -> unit Lwt.t) ->
40-
X509.CA.signing_request ->
41-
(X509.t, [ `Msg of string ]) result Lwt.t
40+
X509.Signing_request.t ->
41+
(X509.Certificate.t, [ `Msg of string ]) result Lwt.t
4242
(** [get_crt ~directory_url ~solver sleep rsa_pem csr_pem] asks the CA identified at url
4343
[directory] for signing [csr_pem] with account key [account_pem] for all
4444
domains in [csr_pem]. This functions accepts an optionl argument

src/primitives.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
open Nocrypto
22

3-
module Pem = X509.Encoding.Pem
4-
53
let priv_of_pem rsa_pem =
6-
try
7-
match Pem.Private_key.of_pem_cstruct1 (Cstruct.of_string rsa_pem) with
8-
| `RSA priv -> Ok priv
9-
with Invalid_argument e -> Error e
4+
match X509.Private_key.decode_pem (Cstruct.of_string rsa_pem) with
5+
| Ok (`RSA priv) -> Ok priv
6+
| Error (`Msg e) -> Error e
107

118
let csr_of_pem pem =
12-
try Ok (Pem.Certificate_signing_request.of_pem_cstruct1 (Cstruct.of_string pem))
13-
with Invalid_argument i -> Error i
9+
match X509.Signing_request.decode_pem (Cstruct.of_string pem) with
10+
| Ok it -> Ok it
11+
| Error (`Msg e) -> Error e
1412

1513
let pub_of_priv = Rsa.pub_of_priv
1614
let pub_of_z ~e ~n = Rsa.{e; n}
@@ -19,16 +17,16 @@ let pub_to_z (key : Rsa.pub) = Rsa.(key.e, key.n)
1917
let rs256_sign priv data =
2018
let data = Cstruct.of_string data in
2119
let h = Hash.SHA256.digest data in
22-
let pkcs1_digest = X509.Encoding.pkcs1_digest_info_to_cstruct (`SHA256, h) in
20+
let pkcs1_digest = X509.Certificate.encode_pkcs1_digest_info (`SHA256, h) in
2321
Rsa.PKCS1.sig_encode ~key:priv pkcs1_digest |> Cstruct.to_string
2422

2523
let rs256_verify pub data signature =
2624
let data = Cstruct.of_string data in
2725
match Rsa.PKCS1.sig_decode ~key:pub (Cstruct.of_string signature) with
2826
| Some pkcs1_digest ->
2927
begin
30-
match X509.Encoding.pkcs1_digest_info_of_cstruct pkcs1_digest with
31-
| Some (`SHA256, hash) -> hash = Hash.SHA256.digest data
28+
match X509.Certificate.decode_pkcs1_digest_info pkcs1_digest with
29+
| Ok (`SHA256, hash) -> hash = Hash.SHA256.digest data
3230
| _ -> false
3331
end
3432
| _ -> false

src/primitives.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Nocrypto.Rsa
22

33
val priv_of_pem : string -> (priv, string) result
44

5-
val csr_of_pem : string -> (X509.CA.signing_request, string) result
5+
val csr_of_pem : string -> (X509.Signing_request.t, string) result
66

77
val pub_of_priv : priv -> pub
88
val pub_of_z : e:Z.t -> n:Z.t -> pub

0 commit comments

Comments
 (0)