s2n_server_cert_request.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "api/s2n.h"
  16. #include "crypto/s2n_certificate.h"
  17. #include "error/s2n_errno.h"
  18. #include "extensions/s2n_extension_list.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "tls/s2n_cipher_suites.h"
  21. #include "tls/s2n_config.h"
  22. #include "tls/s2n_connection.h"
  23. #include "tls/s2n_signature_algorithms.h"
  24. #include "tls/s2n_signature_scheme.h"
  25. #include "tls/s2n_tls.h"
  26. #include "utils/s2n_array.h"
  27. #include "utils/s2n_safety.h"
  28. /* RFC's that define below values:
  29. * - https://tools.ietf.org/html/rfc5246#section-7.4.4
  30. * - https://tools.ietf.org/search/rfc4492#section-5.5
  31. */
  32. typedef enum {
  33. S2N_CERT_TYPE_RSA_SIGN = 1,
  34. S2N_CERT_TYPE_DSS_SIGN = 2,
  35. S2N_CERT_TYPE_RSA_FIXED_DH = 3,
  36. S2N_CERT_TYPE_DSS_FIXED_DH = 4,
  37. S2N_CERT_TYPE_RSA_EPHEMERAL_DH_RESERVED = 5,
  38. S2N_CERT_TYPE_DSS_EPHEMERAL_DH_RESERVED = 6,
  39. S2N_CERT_TYPE_FORTEZZA_DMS_RESERVED = 20,
  40. S2N_CERT_TYPE_ECDSA_SIGN = 64,
  41. S2N_CERT_TYPE_RSA_FIXED_ECDH = 65,
  42. S2N_CERT_TYPE_ECDSA_FIXED_ECDH = 66,
  43. } s2n_cert_type;
  44. static uint8_t s2n_cert_type_preference_list[] = {
  45. S2N_CERT_TYPE_RSA_SIGN,
  46. S2N_CERT_TYPE_ECDSA_SIGN
  47. };
  48. /*
  49. * Include DSS sign certificate type in server certificate request.
  50. * Only will be used if cert_req_dss_legacy_compat_enabled is set by calling
  51. * s2n_config_enable_cert_req_dss_legacy_compat.
  52. */
  53. static uint8_t s2n_cert_type_preference_list_legacy_dss[] = {
  54. S2N_CERT_TYPE_RSA_SIGN,
  55. S2N_CERT_TYPE_DSS_SIGN,
  56. S2N_CERT_TYPE_ECDSA_SIGN
  57. };
  58. static int s2n_recv_client_cert_preferences(struct s2n_stuffer *in, s2n_cert_type *chosen_cert_type_out)
  59. {
  60. uint8_t cert_types_len;
  61. POSIX_GUARD(s2n_stuffer_read_uint8(in, &cert_types_len));
  62. uint8_t *their_cert_type_pref_list = s2n_stuffer_raw_read(in, cert_types_len);
  63. POSIX_ENSURE_REF(their_cert_type_pref_list);
  64. /* Iterate through our preference list from most to least preferred, and return the first match that we find. */
  65. for (size_t our_cert_pref_idx = 0; our_cert_pref_idx < sizeof(s2n_cert_type_preference_list); our_cert_pref_idx++) {
  66. for (int their_cert_idx = 0; their_cert_idx < cert_types_len; their_cert_idx++) {
  67. if (their_cert_type_pref_list[their_cert_idx] == s2n_cert_type_preference_list[our_cert_pref_idx]) {
  68. *chosen_cert_type_out = s2n_cert_type_preference_list[our_cert_pref_idx];
  69. return 0;
  70. }
  71. }
  72. }
  73. POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED);
  74. }
  75. static int s2n_set_cert_chain_as_client(struct s2n_connection *conn)
  76. {
  77. if (s2n_config_get_num_default_certs(conn->config) > 0) {
  78. POSIX_GUARD(s2n_choose_sig_scheme_from_peer_preference_list(conn, &conn->handshake_params.server_sig_hash_algs,
  79. &conn->handshake_params.client_cert_sig_scheme));
  80. struct s2n_cert_chain_and_key *cert = s2n_config_get_single_default_cert(conn->config);
  81. POSIX_ENSURE_REF(cert);
  82. conn->handshake_params.our_chain_and_key = cert;
  83. conn->handshake_params.client_cert_pkey_type = s2n_cert_chain_and_key_get_pkey_type(cert);
  84. }
  85. return 0;
  86. }
  87. int s2n_tls13_cert_req_recv(struct s2n_connection *conn)
  88. {
  89. struct s2n_stuffer *in = &conn->handshake.io;
  90. /* read request context length */
  91. uint8_t request_context_length;
  92. POSIX_GUARD(s2n_stuffer_read_uint8(in, &request_context_length));
  93. /* RFC 8446: This field SHALL be zero length unless used for the post-handshake authentication */
  94. S2N_ERROR_IF(request_context_length != 0, S2N_ERR_BAD_MESSAGE);
  95. POSIX_GUARD(s2n_extension_list_recv(S2N_EXTENSION_LIST_CERT_REQ, conn, in));
  96. POSIX_GUARD(s2n_set_cert_chain_as_client(conn));
  97. return S2N_SUCCESS;
  98. }
  99. int s2n_cert_req_recv(struct s2n_connection *conn)
  100. {
  101. struct s2n_stuffer *in = &conn->handshake.io;
  102. s2n_cert_type cert_type = 0;
  103. POSIX_GUARD(s2n_recv_client_cert_preferences(in, &cert_type));
  104. if (conn->actual_protocol_version == S2N_TLS12) {
  105. POSIX_GUARD(s2n_recv_supported_sig_scheme_list(in, &conn->handshake_params.server_sig_hash_algs));
  106. }
  107. uint16_t cert_authorities_len = 0;
  108. POSIX_GUARD(s2n_stuffer_read_uint16(in, &cert_authorities_len));
  109. /* For now we don't parse X.501 encoded CA Distinguished Names.
  110. * Don't fail just yet as we still may succeed if we provide
  111. * right certificate or if ClientAuth is optional. */
  112. POSIX_GUARD(s2n_stuffer_skip_read(in, cert_authorities_len));
  113. /* In the future we may have more advanced logic to match a set of configured certificates against
  114. * The cert authorities extension and the signature algorithms advertised.
  115. * For now, this will just set the only certificate configured.
  116. */
  117. POSIX_GUARD(s2n_set_cert_chain_as_client(conn));
  118. return 0;
  119. }
  120. int s2n_tls13_cert_req_send(struct s2n_connection *conn)
  121. {
  122. struct s2n_stuffer *out = &conn->handshake.io;
  123. /* Write 0 length request context https://tools.ietf.org/html/rfc8446#section-4.3.2 */
  124. POSIX_GUARD(s2n_stuffer_write_uint8(out, 0));
  125. POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_CERT_REQ, conn, out));
  126. return S2N_SUCCESS;
  127. }
  128. int s2n_cert_req_send(struct s2n_connection *conn)
  129. {
  130. struct s2n_stuffer *out = &conn->handshake.io;
  131. uint8_t client_cert_preference_list_size = sizeof(s2n_cert_type_preference_list);
  132. if (conn->config->cert_req_dss_legacy_compat_enabled) {
  133. client_cert_preference_list_size = sizeof(s2n_cert_type_preference_list_legacy_dss);
  134. }
  135. POSIX_GUARD(s2n_stuffer_write_uint8(out, client_cert_preference_list_size));
  136. for (int i = 0; i < client_cert_preference_list_size; i++) {
  137. if (conn->config->cert_req_dss_legacy_compat_enabled) {
  138. POSIX_GUARD(s2n_stuffer_write_uint8(out, s2n_cert_type_preference_list_legacy_dss[i]));
  139. } else {
  140. POSIX_GUARD(s2n_stuffer_write_uint8(out, s2n_cert_type_preference_list[i]));
  141. }
  142. }
  143. if (conn->actual_protocol_version == S2N_TLS12) {
  144. POSIX_GUARD_RESULT(s2n_signature_algorithms_supported_list_send(conn, out));
  145. }
  146. /* RFC 5246 7.4.4 - If the certificate_authorities list is empty, then the
  147. * client MAY send any certificate of the appropriate ClientCertificateType */
  148. uint16_t acceptable_cert_authorities_len = 0;
  149. POSIX_GUARD(s2n_stuffer_write_uint16(out, acceptable_cert_authorities_len));
  150. return 0;
  151. }