s2n_client_cert_verify.c 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "stuffer/s2n_stuffer.h"
  18. #include "tls/s2n_async_pkey.h"
  19. #include "tls/s2n_config.h"
  20. #include "tls/s2n_connection.h"
  21. #include "tls/s2n_signature_algorithms.h"
  22. #include "tls/s2n_tls.h"
  23. #include "utils/s2n_safety.h"
  24. static int s2n_client_cert_verify_send_complete(struct s2n_connection *conn, struct s2n_blob *signature);
  25. int s2n_client_cert_verify_recv(struct s2n_connection *conn)
  26. {
  27. POSIX_ENSURE_REF(conn);
  28. struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
  29. POSIX_ENSURE_REF(hashes);
  30. struct s2n_stuffer *in = &conn->handshake.io;
  31. POSIX_GUARD_RESULT(s2n_signature_algorithm_recv(conn, in));
  32. const struct s2n_signature_scheme *chosen_sig_scheme = conn->handshake_params.client_cert_sig_scheme;
  33. POSIX_ENSURE_REF(chosen_sig_scheme);
  34. uint16_t signature_size;
  35. struct s2n_blob signature = { 0 };
  36. POSIX_GUARD(s2n_stuffer_read_uint16(in, &signature_size));
  37. signature.size = signature_size;
  38. signature.data = s2n_stuffer_raw_read(in, signature.size);
  39. POSIX_ENSURE_REF(signature.data);
  40. /* Use a copy of the hash state since the verify digest computation may modify the running hash state we need later. */
  41. struct s2n_hash_state *hash_state = &hashes->hash_workspace;
  42. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, chosen_sig_scheme->hash_alg, hash_state));
  43. /* Verify the signature */
  44. POSIX_GUARD(s2n_pkey_verify(&conn->handshake_params.client_public_key, chosen_sig_scheme->sig_alg, hash_state, &signature));
  45. /* Client certificate has been verified. Minimize required handshake hash algs */
  46. POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn));
  47. return S2N_SUCCESS;
  48. }
  49. int s2n_client_cert_verify_send(struct s2n_connection *conn)
  50. {
  51. POSIX_ENSURE_REF(conn);
  52. struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
  53. POSIX_ENSURE_REF(hashes);
  54. S2N_ASYNC_PKEY_GUARD(conn);
  55. struct s2n_stuffer *out = &conn->handshake.io;
  56. if (conn->actual_protocol_version < S2N_TLS12) {
  57. POSIX_GUARD(s2n_choose_default_sig_scheme(conn, &conn->handshake_params.client_cert_sig_scheme, S2N_CLIENT));
  58. } else {
  59. POSIX_GUARD(s2n_stuffer_write_uint16(out, conn->handshake_params.client_cert_sig_scheme->iana_value));
  60. }
  61. const struct s2n_signature_scheme *chosen_sig_scheme = conn->handshake_params.client_cert_sig_scheme;
  62. POSIX_ENSURE_REF(chosen_sig_scheme);
  63. /* Use a copy of the hash state since the verify digest computation may modify the running hash state we need later. */
  64. struct s2n_hash_state *hash_state = &hashes->hash_workspace;
  65. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, chosen_sig_scheme->hash_alg, hash_state));
  66. S2N_ASYNC_PKEY_SIGN(conn, chosen_sig_scheme->sig_alg, hash_state, s2n_client_cert_verify_send_complete);
  67. }
  68. static int s2n_client_cert_verify_send_complete(struct s2n_connection *conn, struct s2n_blob *signature)
  69. {
  70. struct s2n_stuffer *out = &conn->handshake.io;
  71. POSIX_GUARD(s2n_stuffer_write_uint16(out, signature->size));
  72. POSIX_GUARD(s2n_stuffer_write(out, signature));
  73. /* Client certificate has been verified. Minimize required handshake hash algs */
  74. POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn));
  75. return S2N_SUCCESS;
  76. }