Skip to content

Commit

Permalink
Allow creating JWK from x509 keys directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrikstrid committed Feb 16, 2023
1 parent d8e4dc2 commit 4f0e42a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jose/Jose.mli
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ module Jwk : sig
(** [to_priv_pem t] takes a JWK and returns a result PEM string or a message
of what went wrong. *)

val of_priv_x509 :
?use:use ->
X509.Private_key.t ->
(priv t, [> `Msg of string | `Not_rsa ]) result

val of_pub_x509 :
?use:use ->
X509.Public_key.t ->
(public t, [> `Msg of string | `Not_rsa ]) result

val of_priv_json :
Yojson.Safe.t ->
( priv t,
Expand Down
17 changes: 17 additions & 0 deletions jose/Jwk.ml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,23 @@ let to_priv_pem (jwk : priv t) =
Ok (X509.Private_key.encode_pem (`RSA rsa.key) |> Cstruct.to_string)
| _ -> Error `Not_rsa

let of_priv_x509 ?use x509 : (priv t, [> `Not_rsa ]) result =
match x509 with
| `RSA priv_key -> Ok (make_priv_rsa ?use priv_key)
| `P256 priv_key -> Ok (make_priv_es256 ?use priv_key)
| `P384 priv_key -> Ok (make_priv_es384 ?use priv_key)
| `P521 priv_key -> Ok (make_priv_es512 ?use priv_key)
| _ -> Error (`Msg "key type not supported")

let of_pub_x509 ?use (x509 : X509.Public_key.t) :
(public t, [> `Not_rsa ]) result =
match x509 with
| `RSA public_key -> Ok (make_pub_rsa ?use public_key)
| `P256 public_key -> Ok (make_pub_es256 ?use public_key)
| `P384 public_key -> Ok (make_pub_es384 ?use public_key)
| `P521 public_key -> Ok (make_pub_es512 ?use public_key)
| _ -> Error (`Msg "key type not supported")

let oct_to_json (oct : oct) =
let values =
[
Expand Down

0 comments on commit 4f0e42a

Please sign in to comment.