-
Notifications
You must be signed in to change notification settings - Fork 299
CP-311259: Define tls lib to manage TLS version and ciphers #6981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| (library | ||
| (name tls) | ||
| (public_name tls) | ||
| (wrapped false) | ||
|
liulinC marked this conversation as resolved.
Outdated
|
||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| (* | ||
| * Copyright (C) Citrix Systems Inc. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU Lesser General Public License as published | ||
| * by the Free Software Foundation; version 2.1 only. with the special | ||
| * exception on linking described in file LICENSE. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Lesser General Public License for more details. | ||
| *) | ||
|
|
||
| type version = TLS_1_2 | TLS_1_3 | ||
|
|
||
| type cipher = AES_128_GCM | AES_256_GCM | ||
|
|
||
| type curve = Secp384r1 | ||
|
|
||
| type kex = ECDHE_RSA | ||
|
|
||
| type policy = { | ||
| versions: version list | ||
| ; ciphers: cipher list | ||
| ; curves: curve list | ||
| ; kex: kex list | ||
| ; server_preference: bool | ||
| } | ||
|
|
||
| (* ---- Default XenServer policy ------------------------------------------- *) | ||
|
liulinC marked this conversation as resolved.
Outdated
|
||
|
|
||
| let default = | ||
| { | ||
| versions= [TLS_1_2] | ||
| ; ciphers= [AES_256_GCM; AES_128_GCM] | ||
| ; curves= [Secp384r1] | ||
| ; kex= [ECDHE_RSA] | ||
| ; server_preference= true | ||
| } | ||
|
|
||
| (** Common interface both renderers implement. *) | ||
| module type Renderer = sig | ||
| val string_of_versions : version list -> string | ||
| (** Format-specific colon-joined version list from a version list. *) | ||
|
|
||
| val string_of_ciphers : cipher list -> string | ||
| (** Format-specific colon-joined cipher list from a cipher list. *) | ||
|
|
||
| val string_of_policy : policy -> string | ||
| (** Format-specific combined cipher suite string from a policy. *) | ||
|
|
||
| val string_of_curves : curve list -> string | ||
| (** Format-specific colon-joined curve list from a curve list. *) | ||
|
|
||
| val string_of_server_preference : bool -> string | ||
| (** Format-specific server-preference flag *) | ||
| end | ||
|
|
||
| (* ---- GnuTLS renderer ---------------------------------------------------- *) | ||
|
|
||
| module GnutlsImpl = struct | ||
| let string_of_version = function | ||
| | TLS_1_2 -> | ||
| "+VERS-TLS1.2" | ||
| | TLS_1_3 -> | ||
| "+VERS-TLS1.3" | ||
|
|
||
| let string_of_cipher = function | ||
| | AES_128_GCM -> | ||
| "+AES-128-GCM" | ||
| | AES_256_GCM -> | ||
| "+AES-256-GCM" | ||
|
|
||
| let string_of_versions versions = | ||
| List.map string_of_version versions |> String.concat ":" | ||
|
|
||
| let string_of_ciphers ciphers = | ||
| List.map string_of_cipher ciphers |> String.concat ":" | ||
|
|
||
| let string_of_curve = function Secp384r1 -> "+GROUP-SECP384R1" | ||
|
|
||
| let string_of_curves curves = | ||
| List.map string_of_curve curves |> String.concat ":" | ||
|
|
||
| let string_of_kex = function ECDHE_RSA -> "+ECDHE-RSA" | ||
|
|
||
| let string_of_server_preference = function | ||
| | true -> | ||
| "%SERVER_PRECEDENCE" | ||
| | false -> | ||
| "" | ||
|
|
||
| (** Build a GnuTLS priority string from a policy. | ||
| Example: | ||
| ["NONE:+VERS-TLS1.2:+AES-256-GCM:+AES-128-GCM:+AEAD:+ECDHE-RSA:+SIGN-ALL:+GROUP-SECP384R1:+COMP-NULL:%SERVER_PRECEDENCE"] | ||
| Suitable for Samba's [tls priority =] in [smb.conf]. *) | ||
| let string_of_policy {versions; ciphers; kex; curves; server_preference} = | ||
| (* GnuTLS priority token order (from the GnuTLS manual): | ||
| versions -> ciphers -> MACs -> KEX -> signatures -> groups -> compression -> flags *) | ||
| let is_aead = | ||
| List.exists (function AES_128_GCM | AES_256_GCM -> true) ciphers | ||
| in | ||
| let tokens = | ||
| List.map string_of_version versions | ||
| @ List.map string_of_cipher ciphers | ||
| @ ( if is_aead then | ||
| ["+AEAD"] | ||
| else | ||
| [] | ||
| ) | ||
| @ List.map string_of_kex kex | ||
| @ ["+SIGN-ALL"] | ||
| @ List.map string_of_curve curves | ||
| @ ["+COMP-NULL"] | ||
| @ | ||
| if server_preference then | ||
| ["%SERVER_PRECEDENCE"] | ||
| else | ||
| [] | ||
| in | ||
| Printf.sprintf "NONE:%s" (String.concat ":" tokens) | ||
| end | ||
|
|
||
| (* ---- OpenSSL renderer --------------------------------------------------- *) | ||
|
|
||
| module OpensslImpl = struct | ||
| (* OpenSSL TLS 1.2 suite name: ECDHE-RSA-CIPHER-HASH. | ||
| For GCM (AEAD) suites the hash is used only as the PRF: | ||
| AES-256-GCM -> SHA-384, AES-128-GCM -> SHA-256. *) | ||
| let string_of_cipher = function | ||
| | AES_256_GCM -> | ||
| "ECDHE-RSA-AES256-GCM-SHA384" | ||
| | AES_128_GCM -> | ||
| "ECDHE-RSA-AES128-GCM-SHA256" | ||
|
|
||
| let string_of_version = function TLS_1_2 -> "TLSv1.2" | TLS_1_3 -> "TLSv1.3" | ||
|
|
||
| let string_of_versions versions = | ||
| List.map string_of_version versions |> String.concat ":" | ||
|
|
||
| let string_of_ciphers ciphers = | ||
| List.map string_of_cipher ciphers |> String.concat ":" | ||
|
|
||
| let string_of_curve = function Secp384r1 -> "secp384r1" | ||
|
|
||
| let string_of_curves curves = | ||
| List.map string_of_curve curves |> String.concat ":" | ||
|
|
||
| let string_of_server_preference = function | ||
| | true -> | ||
| "CIPHER_SERVER_PREFERENCE" | ||
| | false -> | ||
| "" | ||
|
|
||
| let string_of_policy _ = failwith "Not supported" | ||
| end | ||
|
|
||
| (** Extends any [Renderer] with convenience values pre-applied to [default]. *) | ||
| module type RendererWithDefaults = sig | ||
| include Renderer | ||
|
|
||
| val default_policy : unit -> string | ||
|
|
||
| val default_ciphers : string | ||
|
|
||
| val default_version : string | ||
|
|
||
| val default_curve : string | ||
|
|
||
| val default_server_preference : string | ||
| end | ||
|
|
||
| module WithDefaults (R : Renderer) : RendererWithDefaults = struct | ||
| include R | ||
|
|
||
| let default_policy () = R.string_of_policy default | ||
|
|
||
| let default_ciphers = R.string_of_ciphers default.ciphers | ||
|
|
||
| let default_version = R.string_of_versions default.versions | ||
|
|
||
| let default_curve = R.string_of_curves default.curves | ||
|
|
||
| let default_server_preference = | ||
| R.string_of_server_preference default.server_preference | ||
| end | ||
|
|
||
| module Gnutls = WithDefaults (GnutlsImpl) | ||
| module Openssl = WithDefaults (OpensslImpl) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| (* | ||
| * Copyright (C) Citrix Systems Inc. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU Lesser General Public License as published | ||
| * by the Free Software Foundation; version 2.1 only. with the special | ||
| * exception on linking described in file LICENSE. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Lesser General Public License for more details. | ||
| *) | ||
|
|
||
| (** TLS policy types and format-specific string renderers. | ||
|
|
||
| Usage: | ||
| {[ | ||
| let () = print_endline (Tls.Gnutls.default_policy ()) | ||
| let () = print_endline (Tls.Openssl.default_policy ()) | ||
| let () = print_endline Tls.Openssl.default_curve | ||
| (* or with a custom policy: *) | ||
| let my_policy = { ... } | ||
| let () = print_endline (Tls.Openssl.string_of_policy my_policy) | ||
| ]} *) | ||
|
|
||
| type version = TLS_1_2 | TLS_1_3 | ||
|
|
||
| type cipher = | ||
| | AES_128_GCM (** AEAD; paired with SHA-256 in OpenSSL suite names *) | ||
| | AES_256_GCM (** AEAD; paired with SHA-384 in OpenSSL suite names *) | ||
|
|
||
| type curve = Secp384r1 | ||
|
|
||
| type kex = ECDHE_RSA | ||
|
|
||
| type policy = { | ||
| versions: version list | ||
| ; ciphers: cipher list | ||
| ; curves: curve list (** Only the first curve is used for stunnel. *) | ||
| ; kex: kex list | ||
| ; server_preference: bool (** When [true], the server picks the cipher. *) | ||
| } | ||
|
|
||
| (** Common interface both renderers implement. *) | ||
| module type Renderer = sig | ||
| val string_of_versions : version list -> string | ||
| (** Format-specific colon-joined version list from a version list. *) | ||
|
|
||
| val string_of_ciphers : cipher list -> string | ||
| (** Format-specific colon-joined cipher list from a cipher list. *) | ||
|
|
||
| val string_of_policy : policy -> string | ||
| (** Format-specific combined cipher suite string from a policy. *) | ||
|
|
||
| val string_of_curves : curve list -> string | ||
| (** Format-specific colon-joined curve list from a curve list. *) | ||
|
|
||
| val string_of_server_preference : bool -> string | ||
| (** Format-specific server-preference flag, or ["" ] if disabled. *) | ||
| end | ||
|
|
||
| (** Extends [Renderer] with convenience values pre-applied to the default policy. *) | ||
| module type RendererWithDefaults = sig | ||
| include Renderer | ||
|
|
||
| val default_policy : unit -> string | ||
| (** Format-specific combined cipher suite string for the default policy. | ||
|
|
||
| This is a [unit -> string] function rather than a plain [string] value to | ||
| avoid eager evaluation at module initialisation time. OCaml evaluates | ||
| every top-level [let] in a functor body when the functor is applied, so a | ||
| plain [string] binding would call [string_of_policy] immediately — | ||
| crashing for renderers (e.g. [Openssl]) where [string_of_policy] raises. | ||
| The other [default_*] values are plain strings because their underlying | ||
| functions are defined for every renderer. *) | ||
|
|
||
| val default_ciphers : string | ||
| (** Colon-joined cipher string for the default cipher list. *) | ||
|
|
||
| val default_version : string | ||
| (** Colon-joined version string for the default version list. *) | ||
|
|
||
| val default_curve : string | ||
| (** Colon-joined curve string for the default curve list. *) | ||
|
|
||
| val default_server_preference : string | ||
| (** Server-preference string for the default policy. *) | ||
| end | ||
|
|
||
| (** GnuTLS priority string renderer. *) | ||
| module Gnutls : RendererWithDefaults | ||
|
|
||
| (** OpenSSL cipher list renderer. *) | ||
| module Openssl : RendererWithDefaults |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.