s2n_server_cert.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "error/s2n_errno.h"
  17. #include "tls/s2n_auth_selection.h"
  18. #include "tls/s2n_cipher_suites.h"
  19. #include "tls/s2n_tls.h"
  20. #include "utils/s2n_safety.h"
  21. int s2n_server_cert_recv(struct s2n_connection *conn)
  22. {
  23. if (conn->actual_protocol_version == S2N_TLS13) {
  24. uint8_t certificate_request_context_len;
  25. POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &certificate_request_context_len));
  26. S2N_ERROR_IF(certificate_request_context_len != 0, S2N_ERR_BAD_MESSAGE);
  27. }
  28. uint32_t size_of_all_certificates;
  29. POSIX_GUARD(s2n_stuffer_read_uint24(&conn->handshake.io, &size_of_all_certificates));
  30. S2N_ERROR_IF(size_of_all_certificates > s2n_stuffer_data_available(&conn->handshake.io) || size_of_all_certificates < 3,
  31. S2N_ERR_BAD_MESSAGE);
  32. s2n_cert_public_key public_key;
  33. POSIX_GUARD(s2n_pkey_zero_init(&public_key));
  34. s2n_pkey_type actual_cert_pkey_type;
  35. struct s2n_blob cert_chain = { 0 };
  36. cert_chain.size = size_of_all_certificates;
  37. cert_chain.data = s2n_stuffer_raw_read(&conn->handshake.io, size_of_all_certificates);
  38. POSIX_ENSURE_REF(cert_chain.data);
  39. POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_chain(&conn->x509_validator, conn, cert_chain.data,
  40. cert_chain.size, &actual_cert_pkey_type, &public_key));
  41. POSIX_GUARD(s2n_is_cert_type_valid_for_auth(conn, actual_cert_pkey_type));
  42. POSIX_GUARD(s2n_pkey_setup_for_type(&public_key, actual_cert_pkey_type));
  43. conn->handshake_params.server_public_key = public_key;
  44. return 0;
  45. }
  46. int s2n_server_cert_send(struct s2n_connection *conn)
  47. {
  48. S2N_ERROR_IF(conn->handshake_params.our_chain_and_key == NULL, S2N_ERR_CERT_TYPE_UNSUPPORTED);
  49. if (conn->actual_protocol_version == S2N_TLS13) {
  50. /* server's certificate request context should always be of zero length */
  51. /* https://tools.ietf.org/html/rfc8446#section-4.4.2 */
  52. uint8_t certificate_request_context_len = 0;
  53. POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, certificate_request_context_len));
  54. }
  55. POSIX_GUARD(s2n_send_cert_chain(conn, &conn->handshake.io, conn->handshake_params.our_chain_and_key));
  56. return 0;
  57. }