s2n_client_cert.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "stuffer/s2n_stuffer.h"
  19. #include "tls/s2n_cipher_suites.h"
  20. #include "tls/s2n_config.h"
  21. #include "tls/s2n_connection.h"
  22. #include "tls/s2n_tls.h"
  23. #include "utils/s2n_blob.h"
  24. #include "utils/s2n_safety.h"
  25. /* In TLS1.2, the certificate list is just an opaque vector of certificates:
  26. *
  27. * opaque ASN.1Cert<1..2^24-1>;
  28. *
  29. * struct {
  30. * ASN.1Cert certificate_list<0..2^24-1>;
  31. * } Certificate;
  32. *
  33. * This construction allowed us to store the entire certificate_list blob
  34. * and return it from the s2n_connection_get_client_cert_chain method for
  35. * customers to examine.
  36. *
  37. * However, TLS1.3 introduced per-certificate extensions:
  38. *
  39. * struct {
  40. * opaque cert_data<1..2^24-1>;
  41. * ----> Extension extensions<0..2^16-1>; <----
  42. * } CertificateEntry;
  43. *
  44. * struct {
  45. * opaque certificate_request_context<0..2^8-1>;
  46. * CertificateEntry certificate_list<0..2^24-1>;
  47. * } Certificate;
  48. *
  49. * So in order to store / return the certificates in the same format as in TLS1.2,
  50. * we need to first strip out the extensions.
  51. */
  52. static S2N_RESULT s2n_client_cert_chain_store(struct s2n_connection *conn,
  53. struct s2n_blob *raw_cert_chain)
  54. {
  55. RESULT_ENSURE_REF(conn);
  56. RESULT_ENSURE_REF(raw_cert_chain);
  57. /* There shouldn't already be a client cert chain, but free just in case */
  58. RESULT_GUARD_POSIX(s2n_free(&conn->handshake_params.client_cert_chain));
  59. /* Earlier versions are a basic copy */
  60. if (conn->actual_protocol_version < S2N_TLS13) {
  61. RESULT_GUARD_POSIX(s2n_dup(raw_cert_chain, &conn->handshake_params.client_cert_chain));
  62. return S2N_RESULT_OK;
  63. }
  64. DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free);
  65. RESULT_GUARD_POSIX(s2n_realloc(&output, raw_cert_chain->size));
  66. struct s2n_stuffer cert_chain_in = { 0 };
  67. RESULT_GUARD_POSIX(s2n_stuffer_init_written(&cert_chain_in, raw_cert_chain));
  68. struct s2n_stuffer cert_chain_out = { 0 };
  69. RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_out, &output));
  70. uint32_t cert_size = 0;
  71. uint16_t extensions_size = 0;
  72. while (s2n_stuffer_data_available(&cert_chain_in)) {
  73. RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(&cert_chain_in, &cert_size));
  74. RESULT_GUARD_POSIX(s2n_stuffer_write_uint24(&cert_chain_out, cert_size));
  75. RESULT_GUARD_POSIX(s2n_stuffer_copy(&cert_chain_in, &cert_chain_out, cert_size));
  76. /* The new TLS1.3 format includes extensions, which we must skip.
  77. * Customers will not expect TLS extensions in a DER-encoded certificate.
  78. */
  79. RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&cert_chain_in, &extensions_size));
  80. RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&cert_chain_in, extensions_size));
  81. }
  82. /* We will have allocated more memory than actually necessary.
  83. * If this becomes a problem, we should consider reallocing the correct amount of memory here.
  84. */
  85. output.size = s2n_stuffer_data_available(&cert_chain_out);
  86. conn->handshake_params.client_cert_chain = output;
  87. ZERO_TO_DISABLE_DEFER_CLEANUP(output);
  88. return S2N_RESULT_OK;
  89. }
  90. int s2n_client_cert_recv(struct s2n_connection *conn)
  91. {
  92. if (conn->actual_protocol_version == S2N_TLS13) {
  93. uint8_t certificate_request_context_len;
  94. POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &certificate_request_context_len));
  95. S2N_ERROR_IF(certificate_request_context_len != 0, S2N_ERR_BAD_MESSAGE);
  96. }
  97. struct s2n_stuffer *in = &conn->handshake.io;
  98. uint32_t cert_chain_size = 0;
  99. POSIX_GUARD(s2n_stuffer_read_uint24(in, &cert_chain_size));
  100. POSIX_ENSURE(cert_chain_size <= s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE);
  101. if (cert_chain_size == 0) {
  102. POSIX_GUARD(s2n_conn_set_handshake_no_client_cert(conn));
  103. return S2N_SUCCESS;
  104. }
  105. uint8_t *cert_chain_data = s2n_stuffer_raw_read(in, cert_chain_size);
  106. POSIX_ENSURE_REF(cert_chain_data);
  107. struct s2n_blob cert_chain = { 0 };
  108. POSIX_GUARD(s2n_blob_init(&cert_chain, cert_chain_data, cert_chain_size));
  109. POSIX_ENSURE(s2n_result_is_ok(s2n_client_cert_chain_store(conn, &cert_chain)),
  110. S2N_ERR_BAD_MESSAGE);
  111. s2n_cert_public_key public_key = { 0 };
  112. POSIX_GUARD(s2n_pkey_zero_init(&public_key));
  113. /* Determine the Cert Type, Verify the Cert, and extract the Public Key */
  114. s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
  115. POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_chain(&conn->x509_validator, conn, cert_chain_data,
  116. cert_chain_size, &pkey_type, &public_key));
  117. conn->handshake_params.client_cert_pkey_type = pkey_type;
  118. POSIX_GUARD(s2n_pkey_setup_for_type(&public_key, pkey_type));
  119. POSIX_GUARD(s2n_pkey_check_key_exists(&public_key));
  120. conn->handshake_params.client_public_key = public_key;
  121. return S2N_SUCCESS;
  122. }
  123. int s2n_client_cert_send(struct s2n_connection *conn)
  124. {
  125. struct s2n_cert_chain_and_key *chain_and_key = conn->handshake_params.our_chain_and_key;
  126. if (conn->actual_protocol_version >= S2N_TLS13) {
  127. /* If this message is in response to a CertificateRequest, the value of
  128. * certificate_request_context in that message.
  129. * https://tools.ietf.org/html/rfc8446#section-4.4.2
  130. *
  131. * This field SHALL be zero length unless used for the post-handshake authentication
  132. * https://tools.ietf.org/html/rfc8446#section-4.3.2
  133. */
  134. uint8_t certificate_request_context_len = 0;
  135. POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, certificate_request_context_len));
  136. }
  137. if (chain_and_key == NULL) {
  138. POSIX_GUARD(s2n_conn_set_handshake_no_client_cert(conn));
  139. POSIX_GUARD(s2n_send_empty_cert_chain(&conn->handshake.io));
  140. return 0;
  141. }
  142. POSIX_GUARD(s2n_send_cert_chain(conn, &conn->handshake.io, chain_and_key));
  143. return S2N_SUCCESS;
  144. }