s2n_certificate.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/x509.h>
  17. #include <stdint.h>
  18. #include "api/s2n.h"
  19. #include "crypto/s2n_pkey.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #define S2N_CERT_TYPE_COUNT S2N_PKEY_TYPE_SENTINEL
  22. struct s2n_cert {
  23. s2n_pkey_type pkey_type;
  24. uint16_t ec_curve_nid;
  25. s2n_cert_public_key public_key;
  26. struct s2n_blob raw;
  27. struct s2n_cert *next;
  28. };
  29. struct s2n_cert_chain {
  30. uint32_t chain_size;
  31. struct s2n_cert *head;
  32. };
  33. struct s2n_cert_chain_and_key {
  34. struct s2n_cert_chain *cert_chain;
  35. s2n_cert_private_key *private_key;
  36. struct s2n_blob ocsp_status;
  37. struct s2n_blob sct_list;
  38. /* DNS type SubjectAlternative names from the leaf certificate to match
  39. * with the server_name extension. We ignore non-DNS SANs here since the
  40. * server_name extension only supports DNS.
  41. */
  42. struct s2n_array *san_names;
  43. /* CommonName values from the leaf certificate's Subject to match with the
  44. * server_name extension. Decoded as UTF8.
  45. */
  46. struct s2n_array *cn_names;
  47. /* Application defined data related to this cert. */
  48. void *context;
  49. };
  50. struct certs_by_type {
  51. struct s2n_cert_chain_and_key *certs[S2N_CERT_TYPE_COUNT];
  52. };
  53. /* Exposed for fuzzing */
  54. int s2n_cert_chain_and_key_load_cns(struct s2n_cert_chain_and_key *chain_and_key, X509 *x509_cert);
  55. int s2n_cert_chain_and_key_load_sans(struct s2n_cert_chain_and_key *chain_and_key, X509 *x509_cert);
  56. int s2n_cert_chain_and_key_matches_dns_name(const struct s2n_cert_chain_and_key *chain_and_key, const struct s2n_blob *dns_name);
  57. S2N_CLEANUP_RESULT s2n_cert_chain_and_key_ptr_free(struct s2n_cert_chain_and_key **cert_and_key);
  58. int s2n_cert_set_cert_type(struct s2n_cert *cert, s2n_pkey_type pkey_type);
  59. int s2n_send_cert_chain(struct s2n_connection *conn, struct s2n_stuffer *out, struct s2n_cert_chain_and_key *chain_and_key);
  60. int s2n_send_empty_cert_chain(struct s2n_stuffer *out);
  61. int s2n_create_cert_chain_from_stuffer(struct s2n_cert_chain *cert_chain_out, struct s2n_stuffer *chain_in_stuffer);
  62. int s2n_cert_chain_and_key_set_cert_chain_bytes(struct s2n_cert_chain_and_key *cert_and_key, uint8_t *cert_chain_pem, uint32_t cert_chain_len);
  63. int s2n_cert_chain_and_key_set_private_key_bytes(struct s2n_cert_chain_and_key *cert_and_key, uint8_t *private_key_pem, uint32_t private_key_len);
  64. int s2n_cert_chain_and_key_set_cert_chain(struct s2n_cert_chain_and_key *cert_and_key, const char *cert_chain_pem);
  65. int s2n_cert_chain_and_key_set_private_key(struct s2n_cert_chain_and_key *cert_and_key, const char *private_key_pem);
  66. s2n_pkey_type s2n_cert_chain_and_key_get_pkey_type(struct s2n_cert_chain_and_key *chain_and_key);
  67. int s2n_cert_chain_get_length(const struct s2n_cert_chain_and_key *chain_and_key, uint32_t *cert_length);
  68. int s2n_cert_chain_get_cert(const struct s2n_cert_chain_and_key *chain_and_key, struct s2n_cert **out_cert, const uint32_t cert_idx);
  69. int s2n_cert_get_der(const struct s2n_cert *cert, const uint8_t **out_cert_der, uint32_t *cert_length);
  70. int s2n_cert_chain_free(struct s2n_cert_chain *cert_chain);
  71. int s2n_cert_get_x509_extension_value_length(struct s2n_cert *cert, const uint8_t *oid, uint32_t *ext_value_len);
  72. int s2n_cert_get_x509_extension_value(struct s2n_cert *cert, const uint8_t *oid, uint8_t *ext_value, uint32_t *ext_value_len, bool *critical);
  73. int s2n_cert_get_utf8_string_from_extension_data_length(const uint8_t *extension_data, uint32_t extension_len, uint32_t *utf8_str_len);
  74. int s2n_cert_get_utf8_string_from_extension_data(const uint8_t *extension_data, uint32_t extension_len, uint8_t *out_data, uint32_t *out_len);