s2n_client_pq_kem.c 3.0 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. #include "tls/extensions/s2n_client_pq_kem.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "pq-crypto/s2n_pq.h"
  19. #include "tls/s2n_kem.h"
  20. #include "tls/s2n_security_policies.h"
  21. #include "tls/s2n_tls.h"
  22. #include "tls/s2n_tls_parameters.h"
  23. #include "utils/s2n_safety.h"
  24. static bool s2n_client_pq_kem_should_send(struct s2n_connection *conn);
  25. static int s2n_client_pq_kem_send(struct s2n_connection *conn, struct s2n_stuffer *out);
  26. static int s2n_client_pq_kem_recv(struct s2n_connection *conn, struct s2n_stuffer *extension);
  27. const s2n_extension_type s2n_client_pq_kem_extension = {
  28. .iana_value = TLS_EXTENSION_PQ_KEM_PARAMETERS,
  29. .is_response = false,
  30. .send = s2n_client_pq_kem_send,
  31. .recv = s2n_client_pq_kem_recv,
  32. .should_send = s2n_client_pq_kem_should_send,
  33. .if_missing = s2n_extension_noop_if_missing,
  34. };
  35. static bool s2n_client_pq_kem_should_send(struct s2n_connection *conn)
  36. {
  37. const struct s2n_security_policy *security_policy;
  38. return s2n_connection_get_security_policy(conn, &security_policy) == S2N_SUCCESS
  39. && s2n_pq_kem_is_extension_required(security_policy)
  40. && s2n_pq_is_enabled();
  41. }
  42. static int s2n_client_pq_kem_send(struct s2n_connection *conn, struct s2n_stuffer *out)
  43. {
  44. const struct s2n_kem_preferences *kem_preferences = NULL;
  45. POSIX_GUARD(s2n_connection_get_kem_preferences(conn, &kem_preferences));
  46. POSIX_ENSURE_REF(kem_preferences);
  47. POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_preferences->kem_count * sizeof(kem_extension_size)));
  48. for (int i = 0; i < kem_preferences->kem_count; i++) {
  49. POSIX_GUARD(s2n_stuffer_write_uint16(out, kem_preferences->kems[i]->kem_extension_id));
  50. }
  51. return S2N_SUCCESS;
  52. }
  53. static int s2n_client_pq_kem_recv(struct s2n_connection *conn, struct s2n_stuffer *extension)
  54. {
  55. uint16_t size_of_all;
  56. struct s2n_blob *proposed_kems = &conn->kex_params.client_pq_kem_extension;
  57. /* Ignore extension if PQ is disabled */
  58. if (!s2n_pq_is_enabled()) {
  59. return S2N_SUCCESS;
  60. }
  61. POSIX_GUARD(s2n_stuffer_read_uint16(extension, &size_of_all));
  62. if (size_of_all > s2n_stuffer_data_available(extension) || size_of_all % sizeof(kem_extension_size)) {
  63. /* Malformed length, ignore the extension */
  64. return S2N_SUCCESS;
  65. }
  66. proposed_kems->size = size_of_all;
  67. proposed_kems->data = s2n_stuffer_raw_read(extension, proposed_kems->size);
  68. POSIX_ENSURE_REF(proposed_kems->data);
  69. return S2N_SUCCESS;
  70. }