| 1 | /* |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #pragma once |
| 17 | |
| 18 | #include <s2n.h> |
| 19 | |
| 20 | struct s2n_security_policy; |
| 21 | |
| 22 | /** |
| 23 | * Security policy definitions for common use cases. |
| 24 | * These policies are versioned, with updates added as new versions. |
| 25 | */ |
| 26 | typedef enum { |
| 27 | /** |
| 28 | * A security policy that supports a wide variety of common options. |
| 29 | * It is intended for use cases where the peer in a connection is unknown, |
| 30 | * but assumed reasonably modern and standard. |
| 31 | * If you are unsure which policy to choose, this is a safe choice. |
| 32 | */ |
| 33 | S2N_POLICY_COMPATIBLE = 1, |
| 34 | /** |
| 35 | * A security policy that supports a narrow selection of the most preferred options. |
| 36 | * It is intended for use cases where the peer is known to support that narrow |
| 37 | * selection of options, usually because the same owner maintains both the clients |
| 38 | * and servers involved in connections. |
| 39 | * |
| 40 | * NOT usable with legacy libcryptos that do not fully support TLS1.3, |
| 41 | * like openssl-1.0.2. |
| 42 | */ |
| 43 | S2N_POLICY_STRICT, |
| 44 | } s2n_policy_name; |
| 45 | |
| 46 | typedef enum { |
| 47 | /** |
| 48 | * Supports only TLS1.3. |
| 49 | * Supports post-quantum key exchange (MLKEM) and signatures (MLDSA). |
| 50 | * Supports MLDSA, EC, and RSA certificates. |
| 51 | * Supports only AES-GCM encryption. |
| 52 | * Supports p256, p384, and p521 named groups. |
| 53 | * Supports only SHA256 and higher signatures. |
| 54 | * Supports only RSA-PSS padding for RSA signatures. |
| 55 | * Supports forward secrecy. |
| 56 | */ |
| 57 | S2N_STRICT_2025_08_20 = 1, |
| 58 | /** |
| 59 | * The latest version of S2N_POLICY_STRICT. Currently S2N_STRICT_2025_08_20. |
| 60 | * Confirmed to successfully handshake with all previous versions of S2N_POLICY_STRICT, |
| 61 | * as well as all previous versions of "default_tls13" and "default_pq". |
| 62 | * If a breaking change is introduced to S2N_POLICY_STRICT, then `S2N_STRICT_LATEST_1` |
| 63 | * will be frozen and a new `S2N_STRICT_LATEST_2` version will track the latest. |
| 64 | */ |
| 65 | S2N_STRICT_LATEST_1 = S2N_STRICT_2025_08_20, |
| 66 | } s2n_strict_policy_version; |
| 67 | |
| 68 | typedef enum { |
| 69 | /** |
| 70 | * Supports TLS1.2 and TLS1.3. |
| 71 | * Supports post-quantum key exchange (MLKEM) and signatures (MLDSA). |
| 72 | * Supports MLDSA, EC, and RSA certificates. |
| 73 | * Supports AES-GCM, AES-CBC, and ChaChaPoly encryption. |
| 74 | * Supports p256, x25519, p384, and p521 named groups. |
| 75 | * Supports only SHA256 and higher signatures. |
| 76 | * Supports forward secrecy. |
| 77 | */ |
| 78 | S2N_COMPAT_2025_08_20 = 1, |
| 79 | } s2n_compat_policy_version; |
| 80 | |
| 81 | /** |
| 82 | * Retrieves a security policy by name and version. |
| 83 | * |
| 84 | * @param policy The s2n_policy_name defining a policy. |
| 85 | * @param version The specific version of the policy. |
| 86 | * @returns A static library security policy |
| 87 | */ |
| 88 | const struct s2n_security_policy *s2n_security_policy_get(s2n_policy_name policy, uint64_t version); |
| 89 | |
| 90 | /** |
| 91 | * Sets the security policy on the config. |
| 92 | * |
| 93 | * "security policies" were previously known as "cipher preferences". |
| 94 | * See `s2n_config_set_cipher_preferences`. |
| 95 | * |
| 96 | * @param config The config object being updated |
| 97 | * @param policy The security policy being set |
| 98 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 99 | */ |
| 100 | int s2n_config_set_security_policy(struct s2n_config *config, const struct s2n_security_policy *policy); |
| 101 | |
| 102 | /** |
| 103 | * Sets an override security policy on the connection. |
| 104 | * |
| 105 | * "security policies" were previously known as "cipher preferences". |
| 106 | * See `s2n_connection_set_cipher_preferences`. |
| 107 | * |
| 108 | * @param conn The connection object being updated |
| 109 | * @param policy The security policy being set |
| 110 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 111 | */ |
| 112 | int s2n_connection_set_security_policy(struct s2n_connection *conn, const struct s2n_security_policy *policy); |
| 113 | |
| 114 | /** |
| 115 | * Output format types for verbose policy output. |
| 116 | */ |
| 117 | typedef enum { |
| 118 | /** |
| 119 | * Produces structured output with the following sections: |
| 120 | * - min version: <minimum_protocol_version> |
| 121 | * - rules: |
| 122 | * - <rule_name>: <yes|no> |
| 123 | * - cipher suites: |
| 124 | * - <cipher_suite_name> |
| 125 | * - signature schemes: |
| 126 | * - <signature_scheme_name> |
| 127 | * - curves: |
| 128 | * - <curve_name> |
| 129 | * - certificate signature schemes: (if present) |
| 130 | * - <cert_signature_scheme_name> |
| 131 | * - certificate keys: (if present) |
| 132 | * - <certificate_key_name> |
| 133 | * - pq: (if present) |
| 134 | * - revision: <pq_hybrid_draft_revision> |
| 135 | * - kems: (if present) |
| 136 | * -- <kem_name> |
| 137 | * - kem groups: |
| 138 | * -- <kem_group_name> |
| 139 | */ |
| 140 | S2N_POLICY_FORMAT_DEBUG_V1 = 1, |
| 141 | } s2n_policy_format; |
| 142 | |
| 143 | /** |
| 144 | * Retrieves the length of the buffer needed for s2n_security_policy_write_bytes(). |
| 145 | * This function should be used to allocate enough memory for the policy output buffer before calling |
| 146 | * s2n_security_policy_write_bytes(). |
| 147 | * |
| 148 | * @note The size of the policy output depends on the specific policy configuration. |
| 149 | * Do not expect the size to always remain the same across different policies. |
| 150 | * |
| 151 | * @param policy The security policy to get the buffer size for |
| 152 | * @param format The output format to use |
| 153 | * @param length Output parameter where the required buffer length will be written |
| 154 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure |
| 155 | */ |
| 156 | int s2n_security_policy_write_length(const struct s2n_security_policy *policy, |
| 157 | s2n_policy_format format, uint32_t *length); |
| 158 | |
| 159 | /** |
| 160 | * Writes output of a security policy to a user-provided buffer in the specified format. |
| 161 | * |
| 162 | * @param policy The security policy to output |
| 163 | * @param format The output format to use |
| 164 | * @param buffer The buffer to write to |
| 165 | * @param buffer_length The size of the buffer |
| 166 | * @param output_size Output variable to be set to the actual number of bytes written to `buffer` |
| 167 | * This value is only meaningful when the function returns S2N_SUCCESS |
| 168 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure (e.g., if buffer is too small) |
| 169 | */ |
| 170 | int s2n_security_policy_write_bytes(const struct s2n_security_policy *policy, |
| 171 | s2n_policy_format format, uint8_t *buffer, uint32_t buffer_length, uint32_t *output_size); |
| 172 | |
| 173 | /** |
| 174 | * Writes output of a security policy to a file descriptor in the specified format. |
| 175 | * |
| 176 | * @param policy The security policy to output |
| 177 | * @param format The output format to use |
| 178 | * @param fd The file descriptor to write to (e.g., STDOUT_FILENO or an open file) |
| 179 | * @param output_size Output variable to be set to the actual number of bytes written to the file descriptor |
| 180 | * This value is only meaningful when the function returns S2N_SUCCESS |
| 181 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure |
| 182 | */ |
| 183 | int s2n_security_policy_write_fd(const struct s2n_security_policy *policy, |
| 184 | s2n_policy_format format, int fd, uint32_t *output_size); |