Skip to content

Commit

Permalink
Fully remove cstruct and update ocamlformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrikstrid committed Dec 16, 2024
1 parent cba7872 commit 6a1b9ad
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 358 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version = 0.24.1
version = 0.27.0

wrap-comments = false
2 changes: 0 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
(>= 1.0.0))
(x509
(>= 0.13.0))
(cstruct
(>= 6.0.0))
astring
(yojson
(>= 1.6.0))
Expand Down
1 change: 0 additions & 1 deletion jose.opam
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ depends: [
"eqaf" {>= "0.7"}
"mirage-crypto" {>= "1.0.0"}
"x509" {>= "0.13.0"}
"cstruct" {>= "6.0.0"}
"astring"
"yojson" {>= "1.6.0"}
"zarith"
Expand Down
5 changes: 2 additions & 3 deletions jose/Header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ let to_json t =
`Assoc (List.filter_map Fun.id values @ t.extra)

let of_string header_str =
let s = U_Base64.url_decode header_str
in
let s = U_Base64.url_decode header_str in
Result.bind s (fun decoded_header ->
Yojson.Safe.from_string decoded_header |> of_json)
Yojson.Safe.from_string decoded_header |> of_json)

let to_string header =
to_json header |> Yojson.Safe.to_string |> U_Base64.url_encode_string
82 changes: 47 additions & 35 deletions jose/Jose.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(** {1 JSON Web Algorithm}
{{: https://www.tools.ietf.org/rfc/rfc7518.html } Link to RFC } *)
{{:https://www.tools.ietf.org/rfc/rfc7518.html} Link to RFC} *)
module Jwa : sig
type alg =
[ `RS256 (** HMAC using SHA-256 *)
Expand All @@ -14,10 +14,11 @@ module Jwa : sig
| `None
| `Unsupported of string ]

(** {{: https://tools.ietf.org/html/rfc7518#section-3.1 } Link to RFC}
(** {{:https://tools.ietf.org/html/rfc7518#section-3.1} Link to RFC}
- [RS256] and [HS256] and none is currently the only supported algs for
signature - [RSA_OAEP] is currently the only supported alg for encryption *)
signature - [RSA_OAEP] is currently the only supported alg for
encryption *)

val alg_to_string : alg -> string
val alg_of_string : string -> alg
Expand All @@ -26,12 +27,13 @@ module Jwa : sig

type kty =
[ `oct (** Octet sequence (used to represent symmetric keys) *)
| `RSA (** RSA {{: https://tools.ietf.org/html/rfc3447} Link to RFC} *)
| `RSA (** RSA {{:https://tools.ietf.org/html/rfc3447} Link to RFC} *)
| `EC (** Elliptic Curve *)
| `OKP
(** Octet Key Pair {{: https://www.rfc-editor.org/rfc/rfc8037.html} Link to RFC} *)
(** Octet Key Pair
{{:https://www.rfc-editor.org/rfc/rfc8037.html} Link to RFC} *)
| `Unsupported of string ]
(** {{: https://tools.ietf.org/html/rfc7518#section-6.1 } Link to RFC } *)
(** {{:https://tools.ietf.org/html/rfc7518#section-6.1} Link to RFC} *)

val kty_to_string : kty -> string
val kty_of_string : string -> kty
Expand All @@ -49,7 +51,7 @@ end

(** {1 JSON Web Key}
{{: https://tools.ietf.org/html/rfc7517 } Link to RFC } *)
{{:https://tools.ietf.org/html/rfc7517} Link to RFC} *)
module Jwk : sig
type use = [ `Sig | `Enc | `Unsupported of string ]
(** [use] will default to [`Sig] in all functions unless supplied *)
Expand Down Expand Up @@ -96,10 +98,12 @@ module Jwk : sig
(** [es512] represents a private JWK with [kty] [`EC] and a [P512.priv] key *)

type priv_ed25519 = Mirage_crypto_ec.Ed25519.priv jwk
(** [ed25519] represents a public JWK with [kty] [`OKP] and a [Ed25519.pub] key *)
(** [ed25519] represents a public JWK with [kty] [`OKP] and a [Ed25519.pub]
key *)

type pub_ed25519 = Mirage_crypto_ec.Ed25519.pub jwk
(** [ed25519] represents a private JWK with [kty] [`OKP] and a [Ed25519.priv] key *)
(** [ed25519] represents a private JWK with [kty] [`OKP] and a [Ed25519.priv]
key *)

(** [t] describes a JSON Web Key which can be either [public] or [private] *)
type 'a t =
Expand All @@ -117,7 +121,8 @@ module Jwk : sig

(** {1 Public keys}
These keys are safe to show and should be used to verify signed content. *)
These keys are safe to show and should be used to verify signed content.
*)

val make_pub_rsa : ?use:use -> Mirage_crypto_pk.Rsa.pub -> public t
(** [rsa_of_pub use pub] takes a public key generated by Nocrypto and returns
Expand All @@ -140,7 +145,8 @@ module Jwk : sig
( public t,
[> `Json_parse_failed of string | `Msg of string | `Unsupported_kty ] )
result
(** [of_pub_json t] takes a [Yojson.Safe.t] and tries to return a [public t] *)
(** [of_pub_json t] takes a [Yojson.Safe.t] and tries to return a [public t]
*)

val of_pub_json_string :
string ->
Expand Down Expand Up @@ -218,9 +224,9 @@ module Jwk : sig
When using it on [Oct] keys it will just return the same as it's a
symetric key. *)

(** {1 Utils }
(** {1 Utils}
Utils to get different data from a JWK *)
Utils to get different data from a JWK *)

val get_kid : 'a t -> string option
(** [get_kid jwk] is a convencience function to get the kid string *)
Expand All @@ -231,10 +237,9 @@ module Jwk : sig
val get_alg : 'a t -> Jwa.alg option
(** [get_alg jwk] is a convencience function to get the algorithm *)

val get_thumbprint :
Digestif.hash' -> 'a t -> (string, [> `Unsafe ]) result
val get_thumbprint : Digestif.hash' -> 'a t -> (string, [> `Unsafe ]) result
(** [get_thumbprint hash jwk] calculates the thumbprint of [jwk] with [hash],
following {{: https://tools.ietf.org/html/rfc7638 } RFC 7638 }.
following {{:https://tools.ietf.org/html/rfc7638} RFC 7638}.
Returns an error for symmetric keys: sharing the hash may leak information
about the key itself ans it's deemed unsafe. *)
Expand All @@ -245,7 +250,7 @@ end

(** {1 JSON Web Key Set}
{{: https://tools.ietf.org/html/rfc7517#section-5 } Link to RFC } *)
{{:https://tools.ietf.org/html/rfc7517#section-5} Link to RFC} *)
module Jwks : sig
type t = { keys : Jwk.public Jwk.t list }
(** [t] describes a Private JSON Web Key Set *)
Expand All @@ -259,7 +264,8 @@ module Jwks : sig

val of_string : string -> t
(** [of_string json_string] takes a JSON string representation and returns a
[t]. Keys that can not be serialized safely will be removed from the list *)
[t]. Keys that can not be serialized safely will be removed from the list
*)

val to_string : t -> string
(** [to_string t] takes a t and returns a JSON string representation *)
Expand All @@ -281,17 +287,19 @@ module Header : sig
}
(** The [header] has the following properties:
- [alg] {! Jwa.alg }
- [jwk] JSON Web Key
- [kid] Key ID - We currently always expect this to be there, this can change in the future
- [x5t] X.509 Certificate SHA-1 Thumbprint -
- [x5t#S256] X.509 Certificate SHA-256 Thumbprint
- [typ] Type
- [cty] Content Type Not implemented
- [alg] {! Jwa.alg }
- [jwk] JSON Web Key
- [kid] Key ID - We currently always expect this to be there, this can
change in the future
- [x5t] X.509 Certificate SHA-1 Thumbprint -
- [x5t#S256] X.509 Certificate SHA-256 Thumbprint
- [typ] Type
- [cty] Content Type Not implemented
{{: https://tools.ietf.org/html/rfc7515#section-4.1 } Link to RFC }
{{:https://tools.ietf.org/html/rfc7515#section-4.1} Link to RFC}
{{: https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-header-parameters } Complete list of registered header parameters} *)
{{:https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-header-parameters}
Complete list of registered header parameters} *)

val make_header :
?typ:string ->
Expand All @@ -302,7 +310,8 @@ module Header : sig
Jwk.priv Jwk.t ->
t
(** [make_header typ alg enc jwk] if [alg] is not provided it will be derived
from [jwk]. [jwk_header] decides if the jwk should be put in the header. *)
from [jwk]. [jwk_header] decides if the jwk should be put in the header.
*)

val of_string : string -> (t, [> `Msg of string ]) result
val to_string : t -> string
Expand All @@ -312,7 +321,7 @@ end

(** {1 JSON Web Signature}
{{: https://tools.ietf.org/html/rfc7515 } Link to RFC } *)
{{:https://tools.ietf.org/html/rfc7515} Link to RFC} *)
module Jws : sig
type signature = string

Expand Down Expand Up @@ -378,7 +387,8 @@ module Jwt : sig
| `Not_json
| `Not_supported ] )
result
(** [of_string ~jwk jwt_string] parses and validates the encoded JWT string. *)
(** [of_string ~jwk jwt_string] parses and validates the encoded JWT string.
*)

val unsafe_of_string :
string -> (t, [> `Msg of string | `Not_json | `Not_supported ]) result
Expand All @@ -392,15 +402,16 @@ module Jwt : sig
Jws.validate to validate the signature *)

val check_expiration : now:Ptime.t -> t -> (t, [> `Expired ]) result
(** [check_expiration ~now t] checks whether the JWT is valid at the current time. *)
(** [check_expiration ~now t] checks whether the JWT is valid at the current
time. *)

val validate :
jwk:'a Jwk.t ->
now:Ptime.t ->
t ->
(t, [> `Expired | `Invalid_signature | `Msg of string ]) result
(** [validate ~jwk ~now t] does the same validation as `validate_signature` and
additionally checks expiration. *)
(** [validate ~jwk ~now t] does the same validation as `validate_signature`
and additionally checks expiration. *)

val sign :
?header:Header.t ->
Expand All @@ -415,7 +426,7 @@ module Jwt : sig
end

module Jwe : sig
(** {{: https://tools.ietf.org/html/rfc7516 } Link to RFC } *)
(** {{:https://tools.ietf.org/html/rfc7516} Link to RFC} *)

type t = {
header : Header.t;
Expand Down Expand Up @@ -448,5 +459,6 @@ module Jwe : sig
[> `Invalid_JWE | `Invalid_JWK | `Decrypt_cek_failed | `Msg of string ]
)
result
(** [decrypt jwk string] decrypts a compact string formated JWE into a {! t } *)
(** [decrypt jwk string] decrypts a compact string formated JWE into a {! t }
*)
end
Loading

0 comments on commit 6a1b9ad

Please sign in to comment.