s2n_x509_validator.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <openssl/x509v3.h>
  17. #include "api/s2n.h"
  18. #include "tls/s2n_signature_scheme.h"
  19. /* one day, BoringSSL may add ocsp stapling support. Let's future proof this a bit by grabbing a definition
  20. * that would have to be there when they add support */
  21. #if defined(OPENSSL_IS_BORINGSSL) && !defined(OCSP_RESPONSE_STATUS_SUCCESSFUL)
  22. #define S2N_OCSP_STAPLING_SUPPORTED 0
  23. #else
  24. #define S2N_OCSP_STAPLING_SUPPORTED 1
  25. #endif /* defined(OPENSSL_IS_BORINGSSL) && !defined(OCSP_RESPONSE_STATUS_SUCCESSFUL) */
  26. typedef enum {
  27. UNINIT,
  28. INIT,
  29. READY_TO_VERIFY,
  30. AWAITING_CRL_CALLBACK,
  31. VALIDATED,
  32. OCSP_VALIDATED,
  33. } validator_state;
  34. /** Return TRUE for trusted, FALSE for untrusted **/
  35. typedef uint8_t (*verify_host)(const char *host_name, size_t host_name_len, void *data);
  36. struct s2n_connection;
  37. /**
  38. * Trust store simply contains the trust store each connection should validate certs against.
  39. * For most use cases, you only need one of these per application.
  40. */
  41. struct s2n_x509_trust_store {
  42. X509_STORE *trust_store;
  43. /* Indicates whether system default certs have been loaded into the trust store */
  44. unsigned loaded_system_certs : 1;
  45. };
  46. /**
  47. * You should have one instance of this per connection.
  48. */
  49. struct s2n_x509_validator {
  50. struct s2n_x509_trust_store *trust_store;
  51. X509_STORE_CTX *store_ctx;
  52. uint8_t skip_cert_validation;
  53. uint8_t check_stapled_ocsp;
  54. uint16_t max_chain_depth;
  55. STACK_OF(X509) *cert_chain_from_wire;
  56. int state;
  57. struct s2n_array *crl_lookup_list;
  58. };
  59. struct s2n_cert_validation_info {
  60. unsigned finished : 1;
  61. unsigned accepted : 1;
  62. };
  63. /** Some libcrypto implementations do not support OCSP validation. Returns 1 if supported, 0 otherwise. */
  64. uint8_t s2n_x509_ocsp_stapling_supported(void);
  65. /** Initialize the trust store to empty defaults (no allocations happen here) */
  66. void s2n_x509_trust_store_init_empty(struct s2n_x509_trust_store *store);
  67. /** Returns TRUE if the trust store has certificates installed, FALSE otherwise */
  68. uint8_t s2n_x509_trust_store_has_certs(struct s2n_x509_trust_store *store);
  69. /** Initialize trust store from a PEM. This will allocate memory, and load PEM into the Trust Store **/
  70. int s2n_x509_trust_store_add_pem(struct s2n_x509_trust_store *store, const char *pem);
  71. /** Initialize trust store from a CA file. This will allocate memory, and load each cert in the file into the trust store
  72. * Returns 0 on success, or S2N error codes on failure. */
  73. int s2n_x509_trust_store_from_ca_file(struct s2n_x509_trust_store *store, const char *ca_pem_filename, const char *ca_dir);
  74. /** Cleans up, and frees any underlying memory in the trust store. */
  75. void s2n_x509_trust_store_wipe(struct s2n_x509_trust_store *store);
  76. /** Initialize the validator in unsafe mode. No validity checks for OCSP, host checks, or X.509 will be performed. */
  77. int s2n_x509_validator_init_no_x509_validation(struct s2n_x509_validator *validator);
  78. /** Initialize the validator in safe mode. Will use trust store to validate x.509 certificates, ocsp responses, and will call
  79. * the verify host callback to determine if a subject name or alternative name from the cert should be trusted.
  80. * Returns 0 on success, and an S2N_ERR_* on failure.
  81. */
  82. int s2n_x509_validator_init(struct s2n_x509_validator *validator, struct s2n_x509_trust_store *trust_store, uint8_t check_ocsp);
  83. /**
  84. * Sets the maximum depth for a cert chain that can be used at validation.
  85. */
  86. int s2n_x509_validator_set_max_chain_depth(struct s2n_x509_validator *validator, uint16_t max_depth);
  87. /** Cleans up underlying memory and data members. Struct can be reused afterwards. */
  88. int s2n_x509_validator_wipe(struct s2n_x509_validator *validator);
  89. /**
  90. * Validates a certificate chain against the configured trust store in safe mode. In unsafe mode, it will find the public key
  91. * and return it but not validate the certificates. Alternative Names and Subject Name will be passed to the host verification callback.
  92. * The verification callback will be possibly called multiple times depending on how many names are found.
  93. * If any of those calls return TRUE, that stage of the validation will continue, otherwise once all names are tried and none matched as
  94. * trusted, the chain will be considered UNTRUSTED.
  95. *
  96. * This function can only be called once per instance of an s2n_x509_validator. If must be called prior to calling
  97. * s2n_x509_validator_validate_cert_stapled_ocsp_response().
  98. */
  99. S2N_RESULT s2n_x509_validator_validate_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
  100. uint8_t *cert_chain_in, uint32_t cert_chain_len, s2n_pkey_type *pkey_type,
  101. struct s2n_pkey *public_key_out);
  102. /**
  103. * Validates an ocsp response against the most recent certificate chain. Also verifies the timestamps on the response. This function can only be
  104. * called once per instance of an s2n_x509_validator and only after a successful call to s2n_x509_validator_validate_cert_chain().
  105. */
  106. S2N_RESULT s2n_x509_validator_validate_cert_stapled_ocsp_response(struct s2n_x509_validator *validator, struct s2n_connection *conn,
  107. const uint8_t *ocsp_response, uint32_t size);
  108. /**
  109. * Checks whether the peer's certificate chain has been received and validated.
  110. * Should be verified before any use of the peer's certificate data.
  111. */
  112. bool s2n_x509_validator_is_cert_chain_validated(const struct s2n_x509_validator *validator);
  113. /**
  114. * Validates that each certificate in a peer's cert chain contains only signature algorithms in a security policy's
  115. * certificate_signatures_preference list.
  116. */
  117. S2N_RESULT s2n_validate_certificate_signature(struct s2n_connection *conn, X509 *x509_cert);
  118. /* Checks to see if a certificate has a signature algorithm that's in our certificate_signature_preferences list */
  119. S2N_RESULT s2n_validate_sig_scheme_supported(struct s2n_connection *conn, X509 *x509_cert,
  120. const struct s2n_signature_preferences *cert_sig_preferences);