s2n_x509_validator.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. #include <arpa/inet.h>
  16. #include <openssl/asn1.h>
  17. #include <openssl/err.h>
  18. #include <openssl/x509.h>
  19. #include <sys/socket.h>
  20. #include "crypto/s2n_libcrypto.h"
  21. #include "crypto/s2n_openssl.h"
  22. #include "crypto/s2n_openssl_x509.h"
  23. #include "tls/extensions/s2n_extension_list.h"
  24. #include "tls/s2n_config.h"
  25. #include "tls/s2n_connection.h"
  26. #include "tls/s2n_crl.h"
  27. #include "utils/s2n_result.h"
  28. #include "utils/s2n_rfc5952.h"
  29. #include "utils/s2n_safety.h"
  30. #if S2N_OCSP_STAPLING_SUPPORTED
  31. #include <openssl/ocsp.h>
  32. DEFINE_POINTER_CLEANUP_FUNC(OCSP_RESPONSE *, OCSP_RESPONSE_free);
  33. DEFINE_POINTER_CLEANUP_FUNC(OCSP_BASICRESP *, OCSP_BASICRESP_free);
  34. #endif
  35. #ifndef X509_V_FLAG_PARTIAL_CHAIN
  36. #define X509_V_FLAG_PARTIAL_CHAIN 0x80000
  37. #endif
  38. #define DEFAULT_MAX_CHAIN_DEPTH 7
  39. /* Time used by default for nextUpdate if none provided in OCSP: 1 hour since thisUpdate. */
  40. #define DEFAULT_OCSP_NEXT_UPDATE_PERIOD 3600
  41. /* s2n's internal clock measures epoch-nanoseconds stored with a uint64_t. The
  42. * maximum representable timestamp is Sunday, July 21, 2554. time_t measures
  43. * epoch-seconds in a int64_t or int32_t (platform dependent). If time_t is an
  44. * int32_t, the maximum representable timestamp is January 19, 2038.
  45. *
  46. * This means that converting from the internal clock to a time_t is not safe,
  47. * because the internal clock might hold a value that is too large to represent
  48. * in a time_t. This constant represents the largest internal clock value that
  49. * can be safely represented as a time_t.
  50. */
  51. #define MAX_32_TIMESTAMP_NANOS 2147483647 * ONE_SEC_IN_NANOS
  52. #define OSSL_VERIFY_CALLBACK_IGNORE_ERROR 1
  53. DEFINE_POINTER_CLEANUP_FUNC(STACK_OF(X509_CRL) *, sk_X509_CRL_free);
  54. DEFINE_POINTER_CLEANUP_FUNC(STACK_OF(GENERAL_NAME) *, GENERAL_NAMES_free);
  55. uint8_t s2n_x509_ocsp_stapling_supported(void)
  56. {
  57. return S2N_OCSP_STAPLING_SUPPORTED;
  58. }
  59. void s2n_x509_trust_store_init_empty(struct s2n_x509_trust_store *store)
  60. {
  61. store->trust_store = NULL;
  62. }
  63. uint8_t s2n_x509_trust_store_has_certs(struct s2n_x509_trust_store *store)
  64. {
  65. return store->trust_store ? (uint8_t) 1 : (uint8_t) 0;
  66. }
  67. int s2n_x509_trust_store_add_pem(struct s2n_x509_trust_store *store, const char *pem)
  68. {
  69. POSIX_ENSURE_REF(store);
  70. POSIX_ENSURE_REF(pem);
  71. if (!store->trust_store) {
  72. store->trust_store = X509_STORE_new();
  73. }
  74. DEFER_CLEANUP(struct s2n_stuffer pem_in_stuffer = { 0 }, s2n_stuffer_free);
  75. DEFER_CLEANUP(struct s2n_stuffer der_out_stuffer = { 0 }, s2n_stuffer_free);
  76. POSIX_GUARD(s2n_stuffer_alloc_ro_from_string(&pem_in_stuffer, pem));
  77. POSIX_GUARD(s2n_stuffer_growable_alloc(&der_out_stuffer, 2048));
  78. do {
  79. DEFER_CLEANUP(struct s2n_blob next_cert = { 0 }, s2n_free);
  80. POSIX_GUARD(s2n_stuffer_certificate_from_pem(&pem_in_stuffer, &der_out_stuffer));
  81. POSIX_GUARD(s2n_alloc(&next_cert, s2n_stuffer_data_available(&der_out_stuffer)));
  82. POSIX_GUARD(s2n_stuffer_read(&der_out_stuffer, &next_cert));
  83. const uint8_t *data = next_cert.data;
  84. DEFER_CLEANUP(X509 *ca_cert = d2i_X509(NULL, &data, next_cert.size), X509_free_pointer);
  85. S2N_ERROR_IF(ca_cert == NULL, S2N_ERR_DECODE_CERTIFICATE);
  86. if (!X509_STORE_add_cert(store->trust_store, ca_cert)) {
  87. unsigned long error = ERR_get_error();
  88. POSIX_ENSURE(ERR_GET_REASON(error) == X509_R_CERT_ALREADY_IN_HASH_TABLE, S2N_ERR_DECODE_CERTIFICATE);
  89. }
  90. } while (s2n_stuffer_data_available(&pem_in_stuffer));
  91. return 0;
  92. }
  93. int s2n_x509_trust_store_from_ca_file(struct s2n_x509_trust_store *store, const char *ca_pem_filename, const char *ca_dir)
  94. {
  95. if (!store->trust_store) {
  96. store->trust_store = X509_STORE_new();
  97. POSIX_ENSURE_REF(store->trust_store);
  98. }
  99. int err_code = X509_STORE_load_locations(store->trust_store, ca_pem_filename, ca_dir);
  100. if (!err_code) {
  101. s2n_x509_trust_store_wipe(store);
  102. POSIX_BAIL(S2N_ERR_X509_TRUST_STORE);
  103. }
  104. return 0;
  105. }
  106. void s2n_x509_trust_store_wipe(struct s2n_x509_trust_store *store)
  107. {
  108. if (store->trust_store) {
  109. X509_STORE_free(store->trust_store);
  110. store->trust_store = NULL;
  111. store->loaded_system_certs = false;
  112. }
  113. }
  114. int s2n_x509_validator_init_no_x509_validation(struct s2n_x509_validator *validator)
  115. {
  116. POSIX_ENSURE_REF(validator);
  117. validator->trust_store = NULL;
  118. validator->store_ctx = NULL;
  119. validator->skip_cert_validation = 1;
  120. validator->check_stapled_ocsp = 0;
  121. validator->max_chain_depth = DEFAULT_MAX_CHAIN_DEPTH;
  122. validator->state = INIT;
  123. validator->cert_chain_from_wire = sk_X509_new_null();
  124. validator->crl_lookup_list = NULL;
  125. return 0;
  126. }
  127. int s2n_x509_validator_init(struct s2n_x509_validator *validator, struct s2n_x509_trust_store *trust_store, uint8_t check_ocsp)
  128. {
  129. POSIX_ENSURE_REF(trust_store);
  130. validator->trust_store = trust_store;
  131. validator->skip_cert_validation = 0;
  132. validator->check_stapled_ocsp = check_ocsp;
  133. validator->max_chain_depth = DEFAULT_MAX_CHAIN_DEPTH;
  134. validator->store_ctx = NULL;
  135. if (validator->trust_store->trust_store) {
  136. validator->store_ctx = X509_STORE_CTX_new();
  137. POSIX_ENSURE_REF(validator->store_ctx);
  138. }
  139. validator->cert_chain_from_wire = sk_X509_new_null();
  140. validator->state = INIT;
  141. validator->crl_lookup_list = NULL;
  142. return 0;
  143. }
  144. static inline void wipe_cert_chain(STACK_OF(X509) *cert_chain)
  145. {
  146. if (cert_chain) {
  147. sk_X509_pop_free(cert_chain, X509_free);
  148. }
  149. }
  150. int s2n_x509_validator_wipe(struct s2n_x509_validator *validator)
  151. {
  152. if (validator->store_ctx) {
  153. X509_STORE_CTX_free(validator->store_ctx);
  154. validator->store_ctx = NULL;
  155. }
  156. wipe_cert_chain(validator->cert_chain_from_wire);
  157. validator->cert_chain_from_wire = NULL;
  158. validator->trust_store = NULL;
  159. validator->skip_cert_validation = 0;
  160. validator->state = UNINIT;
  161. validator->max_chain_depth = 0;
  162. if (validator->crl_lookup_list) {
  163. POSIX_GUARD_RESULT(s2n_array_free(validator->crl_lookup_list));
  164. validator->crl_lookup_list = NULL;
  165. }
  166. return S2N_SUCCESS;
  167. }
  168. int s2n_x509_validator_set_max_chain_depth(struct s2n_x509_validator *validator, uint16_t max_depth)
  169. {
  170. POSIX_ENSURE_REF(validator);
  171. S2N_ERROR_IF(max_depth == 0, S2N_ERR_INVALID_ARGUMENT);
  172. validator->max_chain_depth = max_depth;
  173. return 0;
  174. }
  175. static S2N_RESULT s2n_verify_host_information_san_entry(struct s2n_connection *conn, GENERAL_NAME *current_name, bool *san_found)
  176. {
  177. RESULT_ENSURE_REF(conn);
  178. RESULT_ENSURE_REF(current_name);
  179. RESULT_ENSURE_REF(san_found);
  180. if (current_name->type == GEN_DNS || current_name->type == GEN_URI) {
  181. *san_found = true;
  182. const char *name = (const char *) ASN1_STRING_data(current_name->d.ia5);
  183. RESULT_ENSURE_REF(name);
  184. int name_len = ASN1_STRING_length(current_name->d.ia5);
  185. RESULT_ENSURE_GT(name_len, 0);
  186. RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
  187. return S2N_RESULT_OK;
  188. }
  189. if (current_name->type == GEN_IPADD) {
  190. *san_found = true;
  191. /* try to validate an IP address if it's in the subject alt name. */
  192. const unsigned char *ip_addr = current_name->d.iPAddress->data;
  193. RESULT_ENSURE_REF(ip_addr);
  194. int ip_addr_len = current_name->d.iPAddress->length;
  195. RESULT_ENSURE_GT(ip_addr_len, 0);
  196. RESULT_STACK_BLOB(address, INET6_ADDRSTRLEN + 1, INET6_ADDRSTRLEN + 1);
  197. if (ip_addr_len == 4) {
  198. RESULT_GUARD(s2n_inet_ntop(AF_INET, ip_addr, &address));
  199. } else if (ip_addr_len == 16) {
  200. RESULT_GUARD(s2n_inet_ntop(AF_INET6, ip_addr, &address));
  201. } else {
  202. /* we aren't able to parse this value so skip it */
  203. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  204. }
  205. /* strlen should be safe here since we made sure we were null terminated AND that inet_ntop succeeded */
  206. const char *name = (const char *) address.data;
  207. size_t name_len = strlen(name);
  208. RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
  209. return S2N_RESULT_OK;
  210. }
  211. /* we don't understand this entry type so skip it */
  212. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  213. }
  214. static S2N_RESULT s2n_verify_host_information_san(struct s2n_connection *conn, X509 *public_cert, bool *san_found)
  215. {
  216. RESULT_ENSURE_REF(conn);
  217. RESULT_ENSURE_REF(public_cert);
  218. RESULT_ENSURE_REF(san_found);
  219. *san_found = false;
  220. DEFER_CLEANUP(STACK_OF(GENERAL_NAME) *names_list = NULL, GENERAL_NAMES_free_pointer);
  221. names_list = X509_get_ext_d2i(public_cert, NID_subject_alt_name, NULL, NULL);
  222. RESULT_ENSURE(names_list, S2N_ERR_CERT_UNTRUSTED);
  223. int n = sk_GENERAL_NAME_num(names_list);
  224. RESULT_ENSURE(n > 0, S2N_ERR_CERT_UNTRUSTED);
  225. s2n_result result = S2N_RESULT_OK;
  226. for (int i = 0; i < n; i++) {
  227. GENERAL_NAME *current_name = sk_GENERAL_NAME_value(names_list, i);
  228. /* return success on the first entry that passes verification */
  229. result = s2n_verify_host_information_san_entry(conn, current_name, san_found);
  230. if (s2n_result_is_ok(result)) {
  231. return S2N_RESULT_OK;
  232. }
  233. }
  234. /* if an error was set by one of the entries, then just propagate the error from the last SAN entry call */
  235. RESULT_GUARD(result);
  236. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  237. }
  238. static S2N_RESULT s2n_verify_host_information_common_name(struct s2n_connection *conn, X509 *public_cert, bool *cn_found)
  239. {
  240. RESULT_ENSURE_REF(conn);
  241. RESULT_ENSURE_REF(public_cert);
  242. RESULT_ENSURE_REF(cn_found);
  243. X509_NAME *subject_name = X509_get_subject_name(public_cert);
  244. RESULT_ENSURE(subject_name, S2N_ERR_CERT_UNTRUSTED);
  245. int curr_idx = -1;
  246. while (true) {
  247. int next_idx = X509_NAME_get_index_by_NID(subject_name, NID_commonName, curr_idx);
  248. if (next_idx >= 0) {
  249. curr_idx = next_idx;
  250. } else {
  251. break;
  252. }
  253. }
  254. RESULT_ENSURE(curr_idx >= 0, S2N_ERR_CERT_UNTRUSTED);
  255. ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name, curr_idx));
  256. RESULT_ENSURE(common_name, S2N_ERR_CERT_UNTRUSTED);
  257. /* X520CommonName allows the following ANSI string types per RFC 5280 Appendix A.1 */
  258. RESULT_ENSURE(ASN1_STRING_type(common_name) == V_ASN1_TELETEXSTRING
  259. || ASN1_STRING_type(common_name) == V_ASN1_PRINTABLESTRING
  260. || ASN1_STRING_type(common_name) == V_ASN1_UNIVERSALSTRING
  261. || ASN1_STRING_type(common_name) == V_ASN1_UTF8STRING
  262. || ASN1_STRING_type(common_name) == V_ASN1_BMPSTRING,
  263. S2N_ERR_CERT_UNTRUSTED);
  264. /* at this point we have a valid CN value */
  265. *cn_found = true;
  266. char peer_cn[255] = { 0 };
  267. int cn_len = ASN1_STRING_length(common_name);
  268. RESULT_ENSURE_GT(cn_len, 0);
  269. uint32_t len = (uint32_t) cn_len;
  270. RESULT_ENSURE_LTE(len, s2n_array_len(peer_cn) - 1);
  271. RESULT_CHECKED_MEMCPY(peer_cn, ASN1_STRING_data(common_name), len);
  272. RESULT_ENSURE(conn->verify_host_fn(peer_cn, len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
  273. return S2N_RESULT_OK;
  274. }
  275. /*
  276. * For each name in the cert. Iterate them. Call the callback. If one returns true, then consider it validated,
  277. * if none of them return true, the cert is considered invalid.
  278. */
  279. static S2N_RESULT s2n_verify_host_information(struct s2n_connection *conn, X509 *public_cert)
  280. {
  281. bool entry_found = false;
  282. /* Check SubjectAltNames before CommonName as per RFC 6125 6.4.4 */
  283. s2n_result result = s2n_verify_host_information_san(conn, public_cert, &entry_found);
  284. /*
  285. *= https://www.rfc-editor.org/rfc/rfc6125#section-6.4.4
  286. *# As noted, a client MUST NOT seek a match for a reference identifier
  287. *# of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,
  288. *# URI-ID, or any application-specific identifier types supported by the
  289. *# client.
  290. */
  291. if (entry_found) {
  292. return result;
  293. }
  294. /*
  295. *= https://www.rfc-editor.org/rfc/rfc6125#section-6.4.4
  296. *# Therefore, if and only if the presented identifiers do not include a
  297. *# DNS-ID, SRV-ID, URI-ID, or any application-specific identifier types
  298. *# supported by the client, then the client MAY as a last resort check
  299. *# for a string whose form matches that of a fully qualified DNS domain
  300. *# name in a Common Name field of the subject field (i.e., a CN-ID).
  301. */
  302. result = s2n_verify_host_information_common_name(conn, public_cert, &entry_found);
  303. if (entry_found) {
  304. return result;
  305. }
  306. /* make a null-terminated string in case the callback tries to use strlen */
  307. const char *name = "";
  308. size_t name_len = 0;
  309. /* at this point, we don't have anything to identify the certificate with so pass an empty string to the callback */
  310. RESULT_ENSURE(conn->verify_host_fn(name, name_len, conn->data_for_verify_host), S2N_ERR_CERT_UNTRUSTED);
  311. return S2N_RESULT_OK;
  312. }
  313. static S2N_RESULT s2n_x509_validator_read_asn1_cert(struct s2n_stuffer *cert_chain_in_stuffer, struct s2n_blob *asn1_cert)
  314. {
  315. uint32_t certificate_size = 0;
  316. RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(cert_chain_in_stuffer, &certificate_size));
  317. RESULT_ENSURE(certificate_size > 0, S2N_ERR_CERT_INVALID);
  318. RESULT_ENSURE(certificate_size <= s2n_stuffer_data_available(cert_chain_in_stuffer), S2N_ERR_CERT_INVALID);
  319. asn1_cert->size = certificate_size;
  320. asn1_cert->data = s2n_stuffer_raw_read(cert_chain_in_stuffer, certificate_size);
  321. RESULT_ENSURE_REF(asn1_cert->data);
  322. return S2N_RESULT_OK;
  323. }
  324. static S2N_RESULT s2n_x509_validator_read_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
  325. uint8_t *cert_chain_in, uint32_t cert_chain_len)
  326. {
  327. RESULT_ENSURE(validator->skip_cert_validation || s2n_x509_trust_store_has_certs(validator->trust_store), S2N_ERR_CERT_UNTRUSTED);
  328. RESULT_ENSURE(validator->state == INIT, S2N_ERR_INVALID_CERT_STATE);
  329. struct s2n_blob cert_chain_blob = { 0 };
  330. RESULT_GUARD_POSIX(s2n_blob_init(&cert_chain_blob, cert_chain_in, cert_chain_len));
  331. DEFER_CLEANUP(struct s2n_stuffer cert_chain_in_stuffer = { 0 }, s2n_stuffer_free);
  332. RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_in_stuffer, &cert_chain_blob));
  333. RESULT_GUARD_POSIX(s2n_stuffer_write(&cert_chain_in_stuffer, &cert_chain_blob));
  334. X509 *server_cert = NULL;
  335. while (s2n_stuffer_data_available(&cert_chain_in_stuffer)
  336. && sk_X509_num(validator->cert_chain_from_wire) < validator->max_chain_depth) {
  337. struct s2n_blob asn1_cert = { 0 };
  338. RESULT_GUARD(s2n_x509_validator_read_asn1_cert(&cert_chain_in_stuffer, &asn1_cert));
  339. const uint8_t *data = asn1_cert.data;
  340. /* the cert is der encoded, just convert it. */
  341. server_cert = d2i_X509(NULL, &data, asn1_cert.size);
  342. RESULT_ENSURE(server_cert, S2N_ERR_CERT_INVALID);
  343. /* add the cert to the chain. */
  344. if (!sk_X509_push(validator->cert_chain_from_wire, server_cert)) {
  345. /* After the cert is added to cert_chain_from_wire, it will be freed with the call to
  346. * s2n_x509_validator_wipe. If adding the cert fails, free it now instead. */
  347. X509_free(server_cert);
  348. RESULT_BAIL(S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
  349. }
  350. if (!validator->skip_cert_validation) {
  351. RESULT_ENSURE_OK(s2n_validate_certificate_signature(conn, server_cert), S2N_ERR_CERT_UNTRUSTED);
  352. }
  353. /* certificate extensions is a field in TLS 1.3 - https://tools.ietf.org/html/rfc8446#section-4.4.2 */
  354. if (conn->actual_protocol_version >= S2N_TLS13) {
  355. s2n_parsed_extensions_list parsed_extensions_list = { 0 };
  356. RESULT_GUARD_POSIX(s2n_extension_list_parse(&cert_chain_in_stuffer, &parsed_extensions_list));
  357. }
  358. }
  359. /* if this occurred we exceeded validator->max_chain_depth */
  360. RESULT_ENSURE(validator->skip_cert_validation || s2n_stuffer_data_available(&cert_chain_in_stuffer) == 0,
  361. S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED);
  362. RESULT_ENSURE(sk_X509_num(validator->cert_chain_from_wire) > 0, S2N_ERR_NO_CERT_FOUND);
  363. return S2N_RESULT_OK;
  364. }
  365. static S2N_RESULT s2n_x509_validator_process_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
  366. uint8_t *cert_chain_in, uint32_t cert_chain_len)
  367. {
  368. RESULT_ENSURE(validator->state == INIT, S2N_ERR_INVALID_CERT_STATE);
  369. RESULT_GUARD(s2n_x509_validator_read_cert_chain(validator, conn, cert_chain_in, cert_chain_len));
  370. if (validator->skip_cert_validation) {
  371. return S2N_RESULT_OK;
  372. }
  373. X509 *leaf = sk_X509_value(validator->cert_chain_from_wire, 0);
  374. RESULT_ENSURE_REF(leaf);
  375. if (conn->verify_host_fn) {
  376. RESULT_GUARD(s2n_verify_host_information(conn, leaf));
  377. }
  378. RESULT_GUARD_OSSL(X509_STORE_CTX_init(validator->store_ctx, validator->trust_store->trust_store, leaf,
  379. validator->cert_chain_from_wire),
  380. S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
  381. if (conn->config->crl_lookup_cb) {
  382. RESULT_GUARD(s2n_crl_invoke_lookup_callbacks(conn, validator));
  383. RESULT_GUARD(s2n_crl_handle_lookup_callback_result(validator));
  384. }
  385. validator->state = READY_TO_VERIFY;
  386. return S2N_RESULT_OK;
  387. }
  388. static S2N_RESULT s2n_x509_validator_set_no_check_time_flag(struct s2n_x509_validator *validator)
  389. {
  390. RESULT_ENSURE_REF(validator);
  391. RESULT_ENSURE_REF(validator->store_ctx);
  392. X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(validator->store_ctx);
  393. RESULT_ENSURE_REF(param);
  394. #ifdef S2N_LIBCRYPTO_SUPPORTS_FLAG_NO_CHECK_TIME
  395. RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_NO_CHECK_TIME),
  396. S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
  397. #else
  398. RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
  399. #endif
  400. return S2N_RESULT_OK;
  401. }
  402. int s2n_disable_time_validation_ossl_verify_callback(int default_ossl_ret, X509_STORE_CTX *ctx)
  403. {
  404. int err = X509_STORE_CTX_get_error(ctx);
  405. switch (err) {
  406. case X509_V_ERR_CERT_NOT_YET_VALID:
  407. case X509_V_ERR_CERT_HAS_EXPIRED:
  408. return OSSL_VERIFY_CALLBACK_IGNORE_ERROR;
  409. default:
  410. break;
  411. }
  412. /* If CRL validation is enabled, setting the time validation verify callback will override the
  413. * CRL verify callback. The CRL verify callback is manually triggered to work around this
  414. * issue.
  415. *
  416. * The CRL verify callback ignores validation errors exclusively for CRL timestamp fields. So,
  417. * if CRL validation isn't enabled, the CRL verify callback is a no-op.
  418. */
  419. return s2n_crl_ossl_verify_callback(default_ossl_ret, ctx);
  420. }
  421. static S2N_RESULT s2n_x509_validator_disable_time_validation(struct s2n_connection *conn,
  422. struct s2n_x509_validator *validator)
  423. {
  424. RESULT_ENSURE_REF(conn);
  425. RESULT_ENSURE_REF(conn->config);
  426. RESULT_ENSURE_REF(validator);
  427. RESULT_ENSURE_REF(validator->store_ctx);
  428. /* Setting an X509_STORE verify callback is not recommended with AWS-LC:
  429. * https://github.com/aws/aws-lc/blob/aa90e509f2e940916fbe9fdd469a4c90c51824f6/include/openssl/x509.h#L2980-L2990
  430. *
  431. * If the libcrypto supports the ability to disable time validation with an X509_VERIFY_PARAM
  432. * NO_CHECK_TIME flag, this method is preferred.
  433. *
  434. * However, older versions of AWS-LC and OpenSSL 1.0.2 do not support this flag. In this case,
  435. * an X509_STORE verify callback is used. This is acceptable in older versions of AWS-LC
  436. * because the versions are fixed, and updates to AWS-LC will not break the callback
  437. * implementation.
  438. */
  439. if (s2n_libcrypto_supports_flag_no_check_time()) {
  440. RESULT_GUARD(s2n_x509_validator_set_no_check_time_flag(validator));
  441. } else {
  442. X509_STORE_CTX_set_verify_cb(validator->store_ctx,
  443. s2n_disable_time_validation_ossl_verify_callback);
  444. }
  445. return S2N_RESULT_OK;
  446. }
  447. static S2N_RESULT s2n_x509_validator_verify_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn)
  448. {
  449. RESULT_ENSURE(validator->state == READY_TO_VERIFY, S2N_ERR_INVALID_CERT_STATE);
  450. X509_VERIFY_PARAM *param = X509_STORE_CTX_get0_param(validator->store_ctx);
  451. X509_VERIFY_PARAM_set_depth(param, validator->max_chain_depth);
  452. DEFER_CLEANUP(STACK_OF(X509_CRL) *crl_stack = NULL, sk_X509_CRL_free_pointer);
  453. if (conn->config->crl_lookup_cb) {
  454. X509_STORE_CTX_set_verify_cb(validator->store_ctx, s2n_crl_ossl_verify_callback);
  455. crl_stack = sk_X509_CRL_new_null();
  456. RESULT_GUARD(s2n_crl_get_crls_from_lookup_list(validator, crl_stack));
  457. /* Set the CRL list that the libcrypto will use to validate certificates with */
  458. X509_STORE_CTX_set0_crls(validator->store_ctx, crl_stack);
  459. /* Enable CRL validation for certificates in X509_verify_cert */
  460. RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK),
  461. S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
  462. /* Enable CRL validation for all certificates, not just the leaf */
  463. RESULT_GUARD_OSSL(X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK_ALL),
  464. S2N_ERR_INTERNAL_LIBCRYPTO_ERROR);
  465. }
  466. /* Disabling time validation may set a NO_CHECK_TIME flag on the X509_STORE_CTX. Calling
  467. * X509_STORE_CTX_set_time will override this flag. To prevent this, X509_STORE_CTX_set_time is
  468. * only called if time validation is enabled.
  469. */
  470. if (conn->config->disable_x509_time_validation) {
  471. RESULT_GUARD(s2n_x509_validator_disable_time_validation(conn, validator));
  472. } else {
  473. uint64_t current_sys_time = 0;
  474. RESULT_GUARD(s2n_config_wall_clock(conn->config, &current_sys_time));
  475. if (sizeof(time_t) == 4) {
  476. /* cast value to uint64_t to prevent overflow errors */
  477. RESULT_ENSURE_LTE(current_sys_time, (uint64_t) MAX_32_TIMESTAMP_NANOS);
  478. }
  479. /* this wants seconds not nanoseconds */
  480. time_t current_time = (time_t) (current_sys_time / ONE_SEC_IN_NANOS);
  481. X509_STORE_CTX_set_time(validator->store_ctx, 0, current_time);
  482. }
  483. /* It's assumed that if a valid certificate chain is received with an issuer that's present in
  484. * the trust store, the certificate chain should be trusted. This should be the case even if
  485. * the issuer in the trust store isn't a root certificate. Setting the PARTIAL_CHAIN flag
  486. * allows the libcrypto to trust certificates in the trust store that aren't root certificates.
  487. */
  488. X509_STORE_CTX_set_flags(validator->store_ctx, X509_V_FLAG_PARTIAL_CHAIN);
  489. int verify_ret = X509_verify_cert(validator->store_ctx);
  490. if (verify_ret <= 0) {
  491. int ossl_error = X509_STORE_CTX_get_error(validator->store_ctx);
  492. switch (ossl_error) {
  493. case X509_V_ERR_CERT_NOT_YET_VALID:
  494. RESULT_BAIL(S2N_ERR_CERT_NOT_YET_VALID);
  495. case X509_V_ERR_CERT_HAS_EXPIRED:
  496. RESULT_BAIL(S2N_ERR_CERT_EXPIRED);
  497. case X509_V_ERR_CERT_REVOKED:
  498. RESULT_BAIL(S2N_ERR_CERT_REVOKED);
  499. case X509_V_ERR_UNABLE_TO_GET_CRL:
  500. case X509_V_ERR_DIFFERENT_CRL_SCOPE:
  501. RESULT_BAIL(S2N_ERR_CRL_LOOKUP_FAILED);
  502. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  503. RESULT_BAIL(S2N_ERR_CRL_SIGNATURE);
  504. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  505. RESULT_BAIL(S2N_ERR_CRL_ISSUER);
  506. case X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION:
  507. RESULT_BAIL(S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION);
  508. default:
  509. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  510. }
  511. }
  512. validator->state = VALIDATED;
  513. return S2N_RESULT_OK;
  514. }
  515. static S2N_RESULT s2n_x509_validator_read_leaf_info(struct s2n_connection *conn, uint8_t *cert_chain_in, uint32_t cert_chain_len,
  516. struct s2n_pkey *public_key, s2n_pkey_type *pkey_type, s2n_parsed_extensions_list *first_certificate_extensions)
  517. {
  518. struct s2n_blob cert_chain_blob = { 0 };
  519. RESULT_GUARD_POSIX(s2n_blob_init(&cert_chain_blob, cert_chain_in, cert_chain_len));
  520. DEFER_CLEANUP(struct s2n_stuffer cert_chain_in_stuffer = { 0 }, s2n_stuffer_free);
  521. RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_in_stuffer, &cert_chain_blob));
  522. RESULT_GUARD_POSIX(s2n_stuffer_write(&cert_chain_in_stuffer, &cert_chain_blob));
  523. struct s2n_blob asn1_cert = { 0 };
  524. RESULT_GUARD(s2n_x509_validator_read_asn1_cert(&cert_chain_in_stuffer, &asn1_cert));
  525. RESULT_ENSURE(s2n_asn1der_to_public_key_and_type(public_key, pkey_type, &asn1_cert) == 0,
  526. S2N_ERR_CERT_UNTRUSTED);
  527. /* certificate extensions is a field in TLS 1.3 - https://tools.ietf.org/html/rfc8446#section-4.4.2 */
  528. if (conn->actual_protocol_version >= S2N_TLS13) {
  529. s2n_parsed_extensions_list parsed_extensions_list = { 0 };
  530. RESULT_GUARD_POSIX(s2n_extension_list_parse(&cert_chain_in_stuffer, &parsed_extensions_list));
  531. *first_certificate_extensions = parsed_extensions_list;
  532. }
  533. return S2N_RESULT_OK;
  534. }
  535. S2N_RESULT s2n_x509_validator_validate_cert_chain(struct s2n_x509_validator *validator, struct s2n_connection *conn,
  536. uint8_t *cert_chain_in, uint32_t cert_chain_len, s2n_pkey_type *pkey_type, struct s2n_pkey *public_key_out)
  537. {
  538. RESULT_ENSURE_REF(conn);
  539. RESULT_ENSURE_REF(conn->config);
  540. switch (validator->state) {
  541. case INIT:
  542. break;
  543. case AWAITING_CRL_CALLBACK:
  544. RESULT_GUARD(s2n_crl_handle_lookup_callback_result(validator));
  545. break;
  546. default:
  547. RESULT_BAIL(S2N_ERR_INVALID_CERT_STATE);
  548. }
  549. if (validator->state == INIT) {
  550. RESULT_GUARD(s2n_x509_validator_process_cert_chain(validator, conn, cert_chain_in, cert_chain_len));
  551. }
  552. if (validator->state == READY_TO_VERIFY) {
  553. RESULT_GUARD(s2n_x509_validator_verify_cert_chain(validator, conn));
  554. }
  555. DEFER_CLEANUP(struct s2n_pkey public_key = { 0 }, s2n_pkey_free);
  556. s2n_pkey_zero_init(&public_key);
  557. s2n_parsed_extensions_list first_certificate_extensions = { 0 };
  558. RESULT_GUARD(s2n_x509_validator_read_leaf_info(conn, cert_chain_in, cert_chain_len, &public_key, pkey_type,
  559. &first_certificate_extensions));
  560. if (conn->actual_protocol_version >= S2N_TLS13) {
  561. /* Only process certificate extensions received in the first certificate. Extensions received in all other
  562. * certificates are ignored.
  563. *
  564. *= https://tools.ietf.org/rfc/rfc8446#section-4.4.2
  565. *# If an extension applies to the entire chain, it SHOULD be included in
  566. *# the first CertificateEntry.
  567. */
  568. RESULT_GUARD_POSIX(s2n_extension_list_process(S2N_EXTENSION_LIST_CERTIFICATE, conn, &first_certificate_extensions));
  569. }
  570. if (conn->config->cert_validation_cb) {
  571. struct s2n_cert_validation_info info = { 0 };
  572. RESULT_ENSURE(conn->config->cert_validation_cb(conn, &info, conn->config->cert_validation_ctx) >= S2N_SUCCESS,
  573. S2N_ERR_CANCELLED);
  574. RESULT_ENSURE(info.finished, S2N_ERR_INVALID_STATE);
  575. RESULT_ENSURE(info.accepted, S2N_ERR_CERT_REJECTED);
  576. }
  577. *public_key_out = public_key;
  578. /* Reset the old struct, so we don't clean up public_key_out */
  579. s2n_pkey_zero_init(&public_key);
  580. return S2N_RESULT_OK;
  581. }
  582. S2N_RESULT s2n_x509_validator_validate_cert_stapled_ocsp_response(struct s2n_x509_validator *validator,
  583. struct s2n_connection *conn, const uint8_t *ocsp_response_raw, uint32_t ocsp_response_length)
  584. {
  585. if (validator->skip_cert_validation || !validator->check_stapled_ocsp) {
  586. validator->state = OCSP_VALIDATED;
  587. return S2N_RESULT_OK;
  588. }
  589. RESULT_ENSURE(validator->state == VALIDATED, S2N_ERR_INVALID_CERT_STATE);
  590. #if !S2N_OCSP_STAPLING_SUPPORTED
  591. /* Default to safety */
  592. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  593. #else
  594. RESULT_ENSURE_REF(ocsp_response_raw);
  595. DEFER_CLEANUP(OCSP_RESPONSE *ocsp_response = d2i_OCSP_RESPONSE(NULL, &ocsp_response_raw, ocsp_response_length),
  596. OCSP_RESPONSE_free_pointer);
  597. RESULT_ENSURE(ocsp_response != NULL, S2N_ERR_INVALID_OCSP_RESPONSE);
  598. int ocsp_status = OCSP_response_status(ocsp_response);
  599. RESULT_ENSURE(ocsp_status == OCSP_RESPONSE_STATUS_SUCCESSFUL, S2N_ERR_CERT_UNTRUSTED);
  600. DEFER_CLEANUP(OCSP_BASICRESP *basic_response = OCSP_response_get1_basic(ocsp_response), OCSP_BASICRESP_free_pointer);
  601. RESULT_ENSURE(basic_response != NULL, S2N_ERR_INVALID_OCSP_RESPONSE);
  602. /* X509_STORE_CTX_get0_chain() is better because it doesn't return a copy. But it's not available for Openssl 1.0.2.
  603. * Therefore, we call this variant and clean it up at the end of the function.
  604. * See the comments here:
  605. * https://www.openssl.org/docs/man1.0.2/man3/X509_STORE_CTX_get1_chain.html
  606. */
  607. DEFER_CLEANUP(STACK_OF(X509) *cert_chain = X509_STORE_CTX_get1_chain(validator->store_ctx),
  608. s2n_openssl_x509_stack_pop_free);
  609. RESULT_ENSURE_REF(cert_chain);
  610. const int certs_in_chain = sk_X509_num(cert_chain);
  611. RESULT_ENSURE(certs_in_chain > 0, S2N_ERR_NO_CERT_FOUND);
  612. /* leaf is the top: not the bottom. */
  613. X509 *subject = sk_X509_value(cert_chain, 0);
  614. X509 *issuer = NULL;
  615. /* find the issuer in the chain. If it's not there. Fail everything. */
  616. for (int i = 0; i < certs_in_chain; ++i) {
  617. X509 *issuer_candidate = sk_X509_value(cert_chain, i);
  618. const int issuer_value = X509_check_issued(issuer_candidate, subject);
  619. if (issuer_value == X509_V_OK) {
  620. issuer = issuer_candidate;
  621. break;
  622. }
  623. }
  624. RESULT_ENSURE(issuer != NULL, S2N_ERR_CERT_UNTRUSTED);
  625. /* Important: this checks that the stapled ocsp response CAN be verified, not that it has been verified. */
  626. const int ocsp_verify_res = OCSP_basic_verify(basic_response, cert_chain, validator->trust_store->trust_store, 0);
  627. RESULT_GUARD_OSSL(ocsp_verify_res, S2N_ERR_CERT_UNTRUSTED);
  628. /* do the crypto checks on the response.*/
  629. int status = 0;
  630. int reason = 0;
  631. /* sha1 is the only supported OCSP digest */
  632. OCSP_CERTID *cert_id = OCSP_cert_to_id(EVP_sha1(), subject, issuer);
  633. RESULT_ENSURE_REF(cert_id);
  634. /**
  635. *= https://www.rfc-editor.org/rfc/rfc6960.html#section-2.4
  636. *#
  637. *# thisUpdate The most recent time at which the status being
  638. *# indicated is known by the responder to have been
  639. *# correct.
  640. *#
  641. *# nextUpdate The time at or before which newer information will be
  642. *# available about the status of the certificate.
  643. **/
  644. ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd;
  645. /* Actual verification of the response */
  646. const int ocsp_resp_find_status_res = OCSP_resp_find_status(basic_response, cert_id, &status, &reason, &revtime, &thisupd, &nextupd);
  647. OCSP_CERTID_free(cert_id);
  648. RESULT_GUARD_OSSL(ocsp_resp_find_status_res, S2N_ERR_CERT_UNTRUSTED);
  649. uint64_t current_sys_time_nanoseconds = 0;
  650. RESULT_GUARD(s2n_config_wall_clock(conn->config, &current_sys_time_nanoseconds));
  651. if (sizeof(time_t) == 4) {
  652. /* cast value to uint64_t to prevent overflow errors */
  653. RESULT_ENSURE_LTE(current_sys_time_nanoseconds, (uint64_t) MAX_32_TIMESTAMP_NANOS);
  654. }
  655. /* convert the current_sys_time (which is in nanoseconds) to seconds */
  656. time_t current_sys_time_seconds = (time_t) (current_sys_time_nanoseconds / ONE_SEC_IN_NANOS);
  657. DEFER_CLEANUP(ASN1_GENERALIZEDTIME *current_sys_time = ASN1_GENERALIZEDTIME_set(NULL, current_sys_time_seconds), s2n_openssl_asn1_time_free_pointer);
  658. RESULT_ENSURE_REF(current_sys_time);
  659. /**
  660. * It is fine to use ASN1_TIME functions with ASN1_GENERALIZEDTIME structures
  661. * From openssl documentation:
  662. * It is recommended that functions starting with ASN1_TIME be used instead
  663. * of those starting with ASN1_UTCTIME or ASN1_GENERALIZEDTIME. The
  664. * functions starting with ASN1_UTCTIME and ASN1_GENERALIZEDTIME act only on
  665. * that specific time format. The functions starting with ASN1_TIME will
  666. * operate on either format.
  667. * https://www.openssl.org/docs/man1.1.1/man3/ASN1_TIME_to_generalizedtime.html
  668. *
  669. * ASN1_TIME_compare has a much nicer API, but is not available in Openssl
  670. * 1.0.1, so we use ASN1_TIME_diff.
  671. */
  672. int pday = 0;
  673. int psec = 0;
  674. RESULT_GUARD_OSSL(ASN1_TIME_diff(&pday, &psec, thisupd, current_sys_time), S2N_ERR_CERT_UNTRUSTED);
  675. /* ensure that current_time is after or the same as "this update" */
  676. RESULT_ENSURE(pday >= 0 && psec >= 0, S2N_ERR_CERT_INVALID);
  677. /* ensure that current_time is before or the same as "next update" */
  678. if (nextupd) {
  679. RESULT_GUARD_OSSL(ASN1_TIME_diff(&pday, &psec, current_sys_time, nextupd), S2N_ERR_CERT_UNTRUSTED);
  680. RESULT_ENSURE(pday >= 0 && psec >= 0, S2N_ERR_CERT_EXPIRED);
  681. } else {
  682. /**
  683. * if nextupd isn't present, assume that nextupd is
  684. * DEFAULT_OCSP_NEXT_UPDATE_PERIOD after thisupd. This means that if the
  685. * current time is more than DEFAULT_OCSP_NEXT_UPDATE_PERIOD
  686. * seconds ahead of thisupd, we consider it invalid. We already compared
  687. * current_sys_time to thisupd, so reuse those values
  688. */
  689. uint64_t seconds_after_thisupd = pday * (3600 * 24) + psec;
  690. RESULT_ENSURE(seconds_after_thisupd < DEFAULT_OCSP_NEXT_UPDATE_PERIOD, S2N_ERR_CERT_EXPIRED);
  691. }
  692. switch (status) {
  693. case V_OCSP_CERTSTATUS_GOOD:
  694. validator->state = OCSP_VALIDATED;
  695. return S2N_RESULT_OK;
  696. case V_OCSP_CERTSTATUS_REVOKED:
  697. RESULT_BAIL(S2N_ERR_CERT_REVOKED);
  698. default:
  699. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  700. }
  701. #endif /* S2N_OCSP_STAPLING_SUPPORTED */
  702. }
  703. S2N_RESULT s2n_validate_certificate_signature(struct s2n_connection *conn, X509 *x509_cert)
  704. {
  705. RESULT_ENSURE_REF(conn);
  706. RESULT_ENSURE_REF(x509_cert);
  707. const struct s2n_security_policy *security_policy;
  708. RESULT_GUARD_POSIX(s2n_connection_get_security_policy(conn, &security_policy));
  709. /**
  710. * We only restrict the signature algorithm on the certificates in the
  711. * peer's certificate chain if the certificate_signature_preferences field
  712. * is set in the security policy. This is contrary to the RFC, which
  713. * specifies that the signatures in the "signature_algorithms" extension
  714. * apply to signatures in the certificate chain in certain scenarios, so RFC
  715. * compliance would imply validating that the certificate chain signature
  716. * algorithm matches one of the algorithms specified in the
  717. * "signature_algorithms" extension.
  718. *
  719. *= https://www.rfc-editor.org/rfc/rfc5246#section-7.4.2
  720. *= type=exception
  721. *= reason=not implemented due to lack of utility
  722. *# If the client provided a "signature_algorithms" extension, then all
  723. *# certificates provided by the server MUST be signed by a
  724. *# hash/signature algorithm pair that appears in that extension.
  725. *
  726. *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.3
  727. *= type=exception
  728. *= reason=not implemented due to lack of utility
  729. *# If no "signature_algorithms_cert" extension is present, then the
  730. *# "signature_algorithms" extension also applies to signatures appearing in
  731. *# certificates.
  732. */
  733. if (security_policy->certificate_signature_preferences == NULL) {
  734. return S2N_RESULT_OK;
  735. }
  736. X509_NAME *issuer_name = X509_get_issuer_name(x509_cert);
  737. RESULT_ENSURE_REF(issuer_name);
  738. X509_NAME *subject_name = X509_get_subject_name(x509_cert);
  739. RESULT_ENSURE_REF(subject_name);
  740. /* Do not validate any self-signed certificates */
  741. if (X509_NAME_cmp(issuer_name, subject_name) == 0) {
  742. return S2N_RESULT_OK;
  743. }
  744. RESULT_GUARD(s2n_validate_sig_scheme_supported(conn, x509_cert, security_policy->certificate_signature_preferences));
  745. return S2N_RESULT_OK;
  746. }
  747. S2N_RESULT s2n_validate_sig_scheme_supported(struct s2n_connection *conn, X509 *x509_cert,
  748. const struct s2n_signature_preferences *cert_sig_preferences)
  749. {
  750. RESULT_ENSURE_REF(conn);
  751. RESULT_ENSURE_REF(x509_cert);
  752. RESULT_ENSURE_REF(cert_sig_preferences);
  753. int nid = 0;
  754. #if defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x02070000f)
  755. RESULT_ENSURE_REF(x509_cert->sig_alg);
  756. nid = OBJ_obj2nid(x509_cert->sig_alg->algorithm);
  757. #else
  758. nid = X509_get_signature_nid(x509_cert);
  759. #endif
  760. for (size_t i = 0; i < cert_sig_preferences->count; i++) {
  761. if (cert_sig_preferences->signature_schemes[i]->libcrypto_nid == nid) {
  762. /* SHA-1 algorithms are not supported in certificate signatures in TLS1.3 */
  763. RESULT_ENSURE(!(conn->actual_protocol_version >= S2N_TLS13
  764. && cert_sig_preferences->signature_schemes[i]->hash_alg == S2N_HASH_SHA1),
  765. S2N_ERR_CERT_UNTRUSTED);
  766. return S2N_RESULT_OK;
  767. }
  768. }
  769. RESULT_BAIL(S2N_ERR_CERT_UNTRUSTED);
  770. }
  771. bool s2n_x509_validator_is_cert_chain_validated(const struct s2n_x509_validator *validator)
  772. {
  773. return validator && (validator->state == VALIDATED || validator->state == OCSP_VALIDATED);
  774. }
  775. int s2n_cert_validation_accept(struct s2n_cert_validation_info *info)
  776. {
  777. POSIX_ENSURE_REF(info);
  778. POSIX_ENSURE(!info->finished, S2N_ERR_INVALID_STATE);
  779. info->finished = true;
  780. info->accepted = true;
  781. return S2N_SUCCESS;
  782. }
  783. int s2n_cert_validation_reject(struct s2n_cert_validation_info *info)
  784. {
  785. POSIX_ENSURE_REF(info);
  786. POSIX_ENSURE(!info->finished, S2N_ERR_INVALID_STATE);
  787. info->finished = true;
  788. info->accepted = false;
  789. return S2N_SUCCESS;
  790. }