| 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 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | /** |
| 25 | * @file crl.h |
| 26 | * |
| 27 | * The following APIs enable applications to determine if a received certificate has been revoked by its CA, via |
| 28 | * Certificate Revocation Lists (CRLs). Please see the CRL Validation section in the usage guide for more information. |
| 29 | * |
| 30 | * The CRL APIs are currently considered unstable, since they have been recently added to s2n-tls. After gaining more |
| 31 | * confidence in the correctness and usability of these APIs, they will be made stable. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | struct s2n_crl_lookup; |
| 36 | |
| 37 | /** |
| 38 | * A callback which can be implemented to provide s2n-tls with CRLs to use for CRL validation. |
| 39 | * |
| 40 | * This callback is triggered once for each certificate received during the handshake. To provide s2n-tls with a CRL for |
| 41 | * the certificate, use `s2n_crl_lookup_set()`. To ignore the certificate and not provide a CRL, use |
| 42 | * `s2n_crl_lookup_ignore()`. |
| 43 | * |
| 44 | * This callback can be synchronous or asynchronous. For asynchronous behavior, return success without calling |
| 45 | * `s2n_crl_lookup_set()` or `s2n_crl_lookup_ignore()`. `s2n_negotiate()` will return S2N_BLOCKED_ON_APPLICATION_INPUT |
| 46 | * until one of these functions is called for each invoked callback. |
| 47 | * |
| 48 | * @param lookup The CRL lookup for the given certificate. |
| 49 | * @param context Context for the callback function. |
| 50 | * @returns 0 on success, -1 on failure. |
| 51 | */ |
| 52 | typedef int (*s2n_crl_lookup_callback)(struct s2n_crl_lookup *lookup, void *context); |
| 53 | |
| 54 | /** |
| 55 | * Set a callback to provide CRLs to use for CRL validation. |
| 56 | * |
| 57 | * @param config A pointer to the connection config |
| 58 | * @param s2n_crl_lookup_callback The function to be called for each received certificate. |
| 59 | * @param context Context to be passed to the callback function. |
| 60 | * @return S2N_SUCCESS on success, S2N_FAILURE on failure |
| 61 | */ |
| 62 | S2N_API int s2n_config_set_crl_lookup_cb(struct s2n_config *config, s2n_crl_lookup_callback callback, void *context); |
| 63 | |
| 64 | /** |
| 65 | * Allocates a new `s2n_crl` struct. |
| 66 | * |
| 67 | * Use `s2n_crl_load_pem()` to load the struct with a CRL pem. |
| 68 | * |
| 69 | * The allocated struct must be freed with `s2n_crl_free()`. |
| 70 | * |
| 71 | * @return A pointer to the allocated `s2n_crl` struct. |
| 72 | */ |
| 73 | S2N_API struct s2n_crl *s2n_crl_new(void); |
| 74 | |
| 75 | /** |
| 76 | * Loads a CRL with pem data. |
| 77 | * |
| 78 | * @param crl The CRL to load with the PEM data. |
| 79 | * @param pem The PEM data to load `crl` with. |
| 80 | * @param len The length of the pem data. |
| 81 | * @return S2N_SUCCESS on success, S2N_FAILURE on error. |
| 82 | */ |
| 83 | S2N_API int s2n_crl_load_pem(struct s2n_crl *crl, uint8_t *pem, size_t len); |
| 84 | |
| 85 | /** |
| 86 | * Frees a CRL. |
| 87 | * |
| 88 | * Frees an allocated `s2n_crl` and sets `crl` to NULL. |
| 89 | * |
| 90 | * @param crl The CRL to free. |
| 91 | * @return S2N_SUCCESS on success, S2N_FAILURE on error. |
| 92 | */ |
| 93 | S2N_API int s2n_crl_free(struct s2n_crl **crl); |
| 94 | |
| 95 | /** |
| 96 | * Retrieves the issuer hash of a CRL. |
| 97 | * |
| 98 | * This function can be used to find the CRL associated with a certificate received in the s2n_crl_lookup callback. The |
| 99 | * hash value, `hash`, corresponds with the issuer hash of a certificate, retrieved via |
| 100 | * `s2n_crl_lookup_get_cert_issuer_hash()`. |
| 101 | * |
| 102 | * @param crl The CRL to obtain the hash value of. |
| 103 | * @param hash A pointer that will be set to the hash value. |
| 104 | * @return S2N_SUCCESS on success. S2N_FAILURE on failure |
| 105 | */ |
| 106 | S2N_API int s2n_crl_get_issuer_hash(struct s2n_crl *crl, uint64_t *hash); |
| 107 | |
| 108 | /** |
| 109 | * Determines if the CRL is currently active. |
| 110 | * |
| 111 | * CRLs contain a thisUpdate field, which specifies the date at which the CRL becomes valid. This function can be called |
| 112 | * to check thisUpdate relative to the current time. If the thisUpdate date is in the past, the CRL is considered |
| 113 | * active. |
| 114 | * |
| 115 | * @param crl The CRL to validate. |
| 116 | * @return S2N_SUCCESS if `crl` is active, S2N_FAILURE if `crl` is not active, or the active status cannot be determined. |
| 117 | */ |
| 118 | S2N_API int s2n_crl_validate_active(struct s2n_crl *crl); |
| 119 | |
| 120 | /** |
| 121 | * Determines if the CRL has expired. |
| 122 | * |
| 123 | * CRLs contain a nextUpdate field, which specifies the date at which the CRL becomes expired. This function can be |
| 124 | * called to check nextUpdate relative to the current time. If the nextUpdate date is in the future, the CRL has not |
| 125 | * expired. |
| 126 | * |
| 127 | * If the CRL does not contain a thisUpdate field, the CRL is assumed to never expire. |
| 128 | * |
| 129 | * @param crl The CRL to validate. |
| 130 | * @return S2N_SUCCESS if `crl` has not expired, S2N_FAILURE if `crl` has expired, or the expiration status cannot be determined. |
| 131 | */ |
| 132 | S2N_API int s2n_crl_validate_not_expired(struct s2n_crl *crl); |
| 133 | |
| 134 | /** |
| 135 | * Retrieves the issuer hash of the certificate. |
| 136 | * |
| 137 | * The CRL lookup callback is triggered once for each received certificate. This function is used to get the issuer hash |
| 138 | * of this certificate. The hash value, `hash`, corresponds with the issuer hash of the CRL, retrieved via |
| 139 | * `s2n_crl_get_issuer_hash()`. |
| 140 | * |
| 141 | * @param lookup The CRL lookup for the given certificate. |
| 142 | * @param hash A pointer that will be set to the hash value. |
| 143 | * @return S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 144 | */ |
| 145 | S2N_API int s2n_crl_lookup_get_cert_issuer_hash(struct s2n_crl_lookup *lookup, uint64_t *hash); |
| 146 | |
| 147 | /** |
| 148 | * Provide s2n-tls with a CRL from the CRL lookup callback. |
| 149 | * |
| 150 | * A return function for `s2n_crl_lookup_cb`. This function should be used from within the CRL lookup callback to |
| 151 | * provide s2n-tls with a CRL for the given certificate. The provided CRL will be included in the list of CRLs to use |
| 152 | * when validating the certificate chain. |
| 153 | * |
| 154 | * To skip providing a CRL from the callback, use `s2n_crl_lookup_ignore()`. |
| 155 | * |
| 156 | * @param lookup The CRL lookup for the given certificate. |
| 157 | * @param crl The CRL to include in the list of CRLs used to validate the certificate chain. |
| 158 | * @return S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 159 | */ |
| 160 | S2N_API int s2n_crl_lookup_set(struct s2n_crl_lookup *lookup, struct s2n_crl *crl); |
| 161 | |
| 162 | /** |
| 163 | * Skip providing a CRL from the CRL lookup callback. |
| 164 | * |
| 165 | * A return function for `s2n_crl_lookup_cb`. This function should be used from within the CRL lookup callback to ignore |
| 166 | * the certificate, and skip providing s2n-tls with a CRL. |
| 167 | * |
| 168 | * If a certificate is ignored, and is ultimately included in the chain of trust, certificate chain validation will |
| 169 | * fail with a S2N_ERR_CRL_LOOKUP_FAILED error. However, if the certificate is extraneous and not included in the chain |
| 170 | * of trust, validation is able to proceed. |
| 171 | * |
| 172 | * @param lookup The CRL lookup for the given certificate. |
| 173 | * @return S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 174 | */ |
| 175 | S2N_API int s2n_crl_lookup_ignore(struct s2n_crl_lookup *lookup); |
| 176 | |
| 177 | struct s2n_cert_validation_info; |
| 178 | |
| 179 | /** |
| 180 | * A callback which can be implemented to perform additional validation on received certificates. |
| 181 | * |
| 182 | * The cert validation callback is invoked after receiving and validating the peer's certificate chain. The callback |
| 183 | * can be used by clients to validate server certificates, or by servers to validate client certificates in the case of |
| 184 | * mutual auth. Note that any validation performed by applications in the callback is in addition to the certificate |
| 185 | * validation already performed by s2n-tls. |
| 186 | * |
| 187 | * Applications can use either of the following APIs from within the callback to retrieve the peer's certificate chain |
| 188 | * and perform validation before proceeding with the handshake: |
| 189 | * - `s2n_connection_get_peer_cert_chain()` |
| 190 | * - `s2n_connection_get_client_cert_chain()` |
| 191 | * |
| 192 | * If the validation performed in the callback is successful, `s2n_cert_validation_accept()` MUST be called to allow |
| 193 | * `s2n_negotiate()` to continue the handshake. If the validation is unsuccessful, `s2n_cert_validation_reject()` |
| 194 | * MUST be called, which will cause `s2n_negotiate()` to error. |
| 195 | * |
| 196 | * To use the validation callback asynchronously, return `S2N_SUCCESS` without calling `s2n_cert_validation_accept()` |
| 197 | * or `s2n_cert_validation_reject()`. This will pause the handshake, and `s2n_negotiate()` will throw an `S2N_ERR_T_BLOCKED` |
| 198 | * error and `s2n_blocked_status` will be set to `S2N_BLOCKED_ON_APPLICATION_INPUT`. Applications should call |
| 199 | * `s2n_cert_validation_accept()` or `s2n_cert_validation_reject()` to unpause the handshake before retrying `s2n_negotiate()`. |
| 200 | * |
| 201 | * The `info` parameter is passed to the callback in order to call APIs specific to the cert validation callback, like |
| 202 | * `s2n_cert_validation_accept()` and `s2n_cert_validation_reject()`. The `info` argument shares the same lifetime as |
| 203 | * `s2n_connection`. |
| 204 | * |
| 205 | * After calling `s2n_cert_validation_reject()`, `s2n_negotiate()` will fail with a protocol error indicating that |
| 206 | * the cert has been rejected from the callback. If more information regarding an application's custom validation |
| 207 | * failure is required, consider adding an error code field to the custom connection context. See |
| 208 | * `s2n_connection_set_ctx()` and `s2n_connection_get_ctx()` for how to set and retrieve custom connection contexts. |
| 209 | * |
| 210 | * @param conn The connection object from which the callback was invoked. |
| 211 | * @param info The cert validation info object used to call cert validation APIs. |
| 212 | * @param context Application data provided to the callback function via `s2n_config_set_cert_validation_cb()`. |
| 213 | * @returns 0 on success, -1 on failure. |
| 214 | */ |
| 215 | typedef int (*s2n_cert_validation_callback)(struct s2n_connection *conn, struct s2n_cert_validation_info *info, |
| 216 | void *context); |
| 217 | |
| 218 | /** |
| 219 | * Sets a callback to perform additional validation on received certificates. |
| 220 | * |
| 221 | * @param config The associated connection config. |
| 222 | * @param callback The cert validation callback to set. |
| 223 | * @param context Optional application data passed to the callback function. |
| 224 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 225 | */ |
| 226 | S2N_API int s2n_config_set_cert_validation_cb(struct s2n_config *config, s2n_cert_validation_callback callback, |
| 227 | void *context); |
| 228 | |
| 229 | /** |
| 230 | * Indicates that the validation performed in the cert validation callback was successful. |
| 231 | * |
| 232 | * @param info The cert validation info object for the associated callback. |
| 233 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 234 | */ |
| 235 | S2N_API int s2n_cert_validation_accept(struct s2n_cert_validation_info *info); |
| 236 | |
| 237 | /** |
| 238 | * Indicates that the validation performed in the cert validation callback was unsuccessful. |
| 239 | * |
| 240 | * @param info The cert validation info object for the associated callback. |
| 241 | * @returns S2N_SUCCESS on success, S2N_FAILURE on failure. |
| 242 | */ |
| 243 | S2N_API int s2n_cert_validation_reject(struct s2n_cert_validation_info *info); |
| 244 | |
| 245 | #ifdef __cplusplus |
| 246 | } |
| 247 | #endif |