| 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | //! Security options like cipher suites, signature algorithms, versions, etc. |
| 5 | //! |
| 6 | //! See <https://aws.github.io/s2n-tls/usage-guide/ch06-security-policies.html> |
| 7 | |
| 8 | use crate::error::Error; |
| 9 | use core::fmt; |
| 10 | use std::ffi::{CStr, CString}; |
| 11 | |
| 12 | #[derive(Clone, PartialEq)] |
| 13 | enum Context { |
| 14 | Static(&'static [u8]), |
| 15 | Owned(CString), |
| 16 | } |
| 17 | |
| 18 | #[derive(Clone, PartialEq)] |
| 19 | pub struct Policy(Context); |
| 20 | |
| 21 | impl Policy { |
| 22 | pub(crate) fn as_cstr(&self) -> &CStr { |
| 23 | match &self.0 { |
| 24 | Context::Static(x) => unsafe { |
| 25 | // Safety: Policies are always created with null-terminated strings |
| 26 | CStr::from_bytes_with_nul_unchecked(x) |
| 27 | }, |
| 28 | Context::Owned(x) => x.as_c_str(), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Construct a numbered security policy. |
| 33 | /// |
| 34 | /// Numbered security policies are stable and will not change, in comparison |
| 35 | /// to default security policies: [DEFAULT] and [DEFAULT_TLS13]. |
| 36 | /// |
| 37 | /// See the s2n-tls usage guide for details on available policies: |
| 38 | /// <https://aws.github.io/s2n-tls/usage-guide/ch06-security-policies.html> |
| 39 | /// ``` |
| 40 | /// use s2n_tls::{config, security}; |
| 41 | /// |
| 42 | /// let mut config = config::Builder::new(); |
| 43 | /// |
| 44 | /// // "20240501" is a numbered security policy. More information can be found |
| 45 | /// // in the linked s2n-tls usage guide. |
| 46 | /// let security_policy = match security::Policy::from_version("20240501") { |
| 47 | /// Ok(policy) => policy, |
| 48 | /// Err(e) => { |
| 49 | /// eprintln!("unable to construct the policy: {}", e); |
| 50 | /// return; |
| 51 | /// } |
| 52 | /// }; |
| 53 | /// config.set_security_policy(&security_policy); |
| 54 | /// ``` |
| 55 | pub fn from_version(version: &str) -> Result<Policy, Error> { |
| 56 | let cstr = CString::new(version).map_err(|_| Error::INVALID_INPUT)?; |
| 57 | let context = Context::Owned(cstr); |
| 58 | Ok(Self(context)) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl fmt::Debug for Policy { |
| 63 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 64 | f.debug_tuple("Policy").field(&self.as_cstr()).finish() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | macro_rules! policy { |
| 69 | ($version:expr) => { |
| 70 | Policy(Context::Static(concat!($version, "\0").as_bytes())) |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | /// Default policy |
| 75 | /// |
| 76 | /// # Warning |
| 77 | /// |
| 78 | /// Cipher suites, curves, signature algorithms, or other security policy options |
| 79 | /// may be added or removed from "default" in order to keep it up to date with |
| 80 | /// current security best practices. |
| 81 | /// |
| 82 | /// That means that updating the library may cause the policy to change. If peers |
| 83 | /// are expected to be reasonably modern and support standard options, then this |
| 84 | /// should not be a problem. But if peers rely on a deprecated option that is removed, |
| 85 | /// they may be unable to connect. |
| 86 | /// |
| 87 | /// If you instead need a static, versioned policy, choose one according to the s2n-tls usage guide: |
| 88 | /// <https://aws.github.io/s2n-tls/usage-guide/ch06-security-policies.html> |
| 89 | pub const DEFAULT: Policy = policy!("default"); |
| 90 | |
| 91 | /// Default policy supporting TLS1.3 |
| 92 | /// |
| 93 | /// # Warning |
| 94 | /// |
| 95 | /// Cipher suites, curves, signature algorithms, or other security policy options |
| 96 | /// may be added or removed from "default_tls13" in order to keep it up to date with |
| 97 | /// current security best practices. |
| 98 | /// |
| 99 | /// That means that updating the library may cause the policy to change. If peers |
| 100 | /// are expected to be reasonably modern and support standard options, then this |
| 101 | /// should not be a problem. But if peers rely on a deprecated option that is removed, |
| 102 | /// they may be unable to connect. |
| 103 | /// |
| 104 | /// If you instead need a static, versioned policy, choose one according to the s2n-tls usage guide: |
| 105 | /// <https://aws.github.io/s2n-tls/usage-guide/ch06-security-policies.html> |
| 106 | pub const DEFAULT_TLS13: Policy = policy!("default_tls13"); |
| 107 | |
| 108 | #[cfg(any(feature = "unstable-testing", test))] |
| 109 | pub const TESTING_TLS12: Policy = policy!("20240501"); |
| 110 | |
| 111 | #[cfg(feature = "pq")] |
| 112 | pub const TESTING_PQ: Policy = policy!("PQ-TLS-1-0-2021-05-26"); |
| 113 | |
| 114 | #[cfg(feature = "pq")] |
| 115 | pub const DEFAULT_PQ: Policy = policy!("default_pq"); |
| 116 | |
| 117 | pub const ALL_POLICIES: &[Policy] = &[ |
| 118 | DEFAULT, |
| 119 | DEFAULT_TLS13, |
| 120 | #[cfg(feature = "pq")] |
| 121 | TESTING_PQ, |
| 122 | #[cfg(feature = "pq")] |
| 123 | DEFAULT_PQ, |
| 124 | ]; |