crl.h 11 KB

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