s2n_cert_status.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "tls/extensions/s2n_cert_status.h"
  16. #include "tls/s2n_config.h"
  17. #include "tls/s2n_connection.h"
  18. #include "tls/s2n_tls.h"
  19. #include "tls/s2n_x509_validator.h"
  20. #include "utils/s2n_safety.h"
  21. #define U24_SIZE 3
  22. static bool s2n_cert_status_should_send(struct s2n_connection *conn);
  23. /*
  24. * The cert_status extension is sent in response to OCSP status requests in TLS 1.3. The
  25. * OCSP response is contained in the extension data. In TLS 1.2, the cert_status_response
  26. * extension is sent instead, indicating that the OCSP response will be sent in a
  27. * Certificate Status handshake message.
  28. */
  29. const s2n_extension_type s2n_cert_status_extension = {
  30. .iana_value = TLS_EXTENSION_STATUS_REQUEST,
  31. .is_response = true,
  32. .send = s2n_cert_status_send,
  33. .recv = s2n_cert_status_recv,
  34. .should_send = s2n_cert_status_should_send,
  35. .if_missing = s2n_extension_noop_if_missing,
  36. };
  37. static bool s2n_cert_status_should_send(struct s2n_connection *conn)
  38. {
  39. return conn->handshake_params.our_chain_and_key
  40. && conn->handshake_params.our_chain_and_key->ocsp_status.size > 0;
  41. }
  42. int s2n_cert_status_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  43. {
  44. POSIX_ENSURE_REF(conn);
  45. struct s2n_blob *ocsp_status = &conn->handshake_params.our_chain_and_key->ocsp_status;
  46. POSIX_ENSURE_REF(ocsp_status);
  47. POSIX_GUARD(s2n_stuffer_write_uint8(out, (uint8_t) S2N_STATUS_REQUEST_OCSP));
  48. POSIX_GUARD(s2n_stuffer_write_uint24(out, ocsp_status->size));
  49. POSIX_GUARD(s2n_stuffer_write(out, ocsp_status));
  50. return S2N_SUCCESS;
  51. }
  52. int s2n_cert_status_recv(struct s2n_connection *conn, struct s2n_stuffer *in)
  53. {
  54. POSIX_ENSURE_REF(conn);
  55. /**
  56. *= https://tools.ietf.org/rfc/rfc6066#section-8
  57. *# struct {
  58. *# CertificateStatusType status_type;
  59. *# select (status_type) {
  60. *# case ocsp: OCSPResponse;
  61. *# } response;
  62. *# } CertificateStatus;
  63. *#
  64. *# opaque OCSPResponse<1..2^24-1>;
  65. *#
  66. *# An "ocsp_response" contains a complete, DER-encoded OCSP response
  67. *# (using the ASN.1 type OCSPResponse defined in [RFC2560]). Only one
  68. *# OCSP response may be sent.
  69. **/
  70. uint8_t type;
  71. POSIX_GUARD(s2n_stuffer_read_uint8(in, &type));
  72. if (type != S2N_STATUS_REQUEST_OCSP) {
  73. /* We only support OCSP */
  74. return S2N_SUCCESS;
  75. }
  76. /* The status_type variable is only used when a client requests OCSP stapling from a
  77. * server. A server can request OCSP stapling from a client, but it is not tracked
  78. * with this variable.
  79. */
  80. if (conn->mode == S2N_CLIENT) {
  81. conn->status_type = S2N_STATUS_REQUEST_OCSP;
  82. }
  83. uint32_t status_size;
  84. POSIX_GUARD(s2n_stuffer_read_uint24(in, &status_size));
  85. POSIX_ENSURE_LTE(status_size, s2n_stuffer_data_available(in));
  86. POSIX_GUARD(s2n_realloc(&conn->status_response, status_size));
  87. POSIX_GUARD(s2n_stuffer_read_bytes(in, conn->status_response.data, status_size));
  88. POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_stapled_ocsp_response(&conn->x509_validator, conn,
  89. conn->status_response.data, conn->status_response.size));
  90. return S2N_SUCCESS;
  91. }