s2n_tls13_certificate_verify.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/s2n_tls13_certificate_verify.h"
  16. #include <stdint.h>
  17. #include "crypto/s2n_hash.h"
  18. #include "error/s2n_errno.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "tls/s2n_async_pkey.h"
  21. #include "tls/s2n_connection.h"
  22. #include "tls/s2n_tls13_handshake.h"
  23. #include "utils/s2n_safety.h"
  24. /**
  25. * Specified in https://tools.ietf.org/html/rfc8446#section-4.4.3
  26. *
  27. * Servers MUST send this message when authenticating via a certificate.
  28. * Clients MUST send this message whenever authenticating via a certificate.
  29. * When sent, this message MUST appear immediately after the Certificate
  30. * message and immediately prior to the Finished message.
  31. **/
  32. /* 64 'space' characters (0x20) */
  33. const uint8_t S2N_CERT_VERIFY_PREFIX[] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  34. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  35. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  36. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
  37. /* 'TLS 1.3, server CertificateVerify' with 0x00 separator */
  38. const uint8_t S2N_SERVER_CERT_VERIFY_CONTEXT[] = { 0x54, 0x4c, 0x53, 0x20, 0x31, 0x2e, 0x33,
  39. 0x2c, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69,
  40. 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x00 };
  41. /* 'TLS 1.3, client CertificateVerify' with 0x00 separator */
  42. const uint8_t S2N_CLIENT_CERT_VERIFY_CONTEXT[] = { 0x54, 0x4c, 0x53, 0x20, 0x31, 0x2e, 0x33,
  43. 0x2c, 0x20, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69,
  44. 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x00 };
  45. static int s2n_tls13_write_cert_verify_signature(struct s2n_connection *conn,
  46. const struct s2n_signature_scheme *chosen_sig_scheme);
  47. static int s2n_tls13_write_signature(struct s2n_connection *conn, struct s2n_blob *signature);
  48. static int s2n_tls13_generate_unsigned_cert_verify_content(struct s2n_connection *conn,
  49. struct s2n_stuffer *unsigned_content, s2n_mode mode);
  50. static int s2n_tls13_cert_read_and_verify_signature(struct s2n_connection *conn,
  51. const struct s2n_signature_scheme *chosen_sig_scheme);
  52. static uint8_t s2n_tls13_cert_verify_header_length(s2n_mode mode);
  53. int s2n_tls13_cert_verify_send(struct s2n_connection *conn)
  54. {
  55. S2N_ASYNC_PKEY_GUARD(conn);
  56. if (conn->mode == S2N_SERVER) {
  57. /* Write digital signature */
  58. POSIX_GUARD(s2n_tls13_write_cert_verify_signature(conn, conn->handshake_params.server_cert_sig_scheme));
  59. } else {
  60. /* Write digital signature */
  61. POSIX_GUARD(s2n_tls13_write_cert_verify_signature(conn, conn->handshake_params.client_cert_sig_scheme));
  62. }
  63. return 0;
  64. }
  65. int s2n_tls13_write_cert_verify_signature(struct s2n_connection *conn,
  66. const struct s2n_signature_scheme *chosen_sig_scheme)
  67. {
  68. POSIX_ENSURE_REF(conn->handshake_params.our_chain_and_key);
  69. /* Write the SignatureScheme out */
  70. struct s2n_stuffer *out = &conn->handshake.io;
  71. POSIX_GUARD(s2n_stuffer_write_uint16(out, chosen_sig_scheme->iana_value));
  72. DEFER_CLEANUP(struct s2n_hash_state message_hash = { 0 }, s2n_hash_free);
  73. POSIX_GUARD(s2n_hash_new(&message_hash));
  74. POSIX_GUARD(s2n_hash_init(&message_hash, chosen_sig_scheme->hash_alg));
  75. DEFER_CLEANUP(struct s2n_stuffer unsigned_content = { 0 }, s2n_stuffer_free);
  76. POSIX_GUARD(s2n_tls13_generate_unsigned_cert_verify_content(conn, &unsigned_content, conn->mode));
  77. POSIX_GUARD(s2n_hash_update(&message_hash, unsigned_content.blob.data,
  78. s2n_stuffer_data_available(&unsigned_content)));
  79. S2N_ASYNC_PKEY_SIGN(conn, chosen_sig_scheme->sig_alg, &message_hash, s2n_tls13_write_signature);
  80. }
  81. int s2n_tls13_write_signature(struct s2n_connection *conn, struct s2n_blob *signature)
  82. {
  83. struct s2n_stuffer *out = &conn->handshake.io;
  84. POSIX_GUARD(s2n_stuffer_write_uint16(out, signature->size));
  85. POSIX_GUARD(s2n_stuffer_write_bytes(out, signature->data, signature->size));
  86. return 0;
  87. }
  88. int s2n_tls13_generate_unsigned_cert_verify_content(struct s2n_connection *conn,
  89. struct s2n_stuffer *unsigned_content, s2n_mode mode)
  90. {
  91. s2n_tls13_connection_keys(tls13_ctx, conn);
  92. uint8_t hash_digest_length = tls13_ctx.size;
  93. uint8_t digest_out[S2N_MAX_DIGEST_LEN];
  94. /* Get current handshake hash */
  95. POSIX_ENSURE_REF(conn->handshake.hashes);
  96. struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
  97. POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, tls13_ctx.hash_algorithm, hash_state));
  98. POSIX_GUARD(s2n_hash_digest(hash_state, digest_out, hash_digest_length));
  99. /* Concatenate the content to be signed/verified */
  100. POSIX_GUARD(s2n_stuffer_alloc(unsigned_content, hash_digest_length + s2n_tls13_cert_verify_header_length(mode)));
  101. POSIX_GUARD(s2n_stuffer_write_bytes(unsigned_content, S2N_CERT_VERIFY_PREFIX, sizeof(S2N_CERT_VERIFY_PREFIX)));
  102. if (mode == S2N_CLIENT) {
  103. POSIX_GUARD(s2n_stuffer_write_bytes(unsigned_content, S2N_CLIENT_CERT_VERIFY_CONTEXT,
  104. sizeof(S2N_CLIENT_CERT_VERIFY_CONTEXT)));
  105. } else {
  106. POSIX_GUARD(s2n_stuffer_write_bytes(unsigned_content, S2N_SERVER_CERT_VERIFY_CONTEXT,
  107. sizeof(S2N_SERVER_CERT_VERIFY_CONTEXT)));
  108. }
  109. POSIX_GUARD(s2n_stuffer_write_bytes(unsigned_content, digest_out, hash_digest_length));
  110. return 0;
  111. }
  112. uint8_t s2n_tls13_cert_verify_header_length(s2n_mode mode)
  113. {
  114. if (mode == S2N_CLIENT) {
  115. return sizeof(S2N_CERT_VERIFY_PREFIX) + sizeof(S2N_CLIENT_CERT_VERIFY_CONTEXT);
  116. }
  117. return sizeof(S2N_CERT_VERIFY_PREFIX) + sizeof(S2N_SERVER_CERT_VERIFY_CONTEXT);
  118. }
  119. int s2n_tls13_cert_verify_recv(struct s2n_connection *conn)
  120. {
  121. POSIX_GUARD_RESULT(s2n_signature_algorithm_recv(conn, &conn->handshake.io));
  122. /* Read the rest of the signature and verify */
  123. if (conn->mode == S2N_SERVER) {
  124. POSIX_GUARD(s2n_tls13_cert_read_and_verify_signature(conn,
  125. conn->handshake_params.client_cert_sig_scheme));
  126. } else {
  127. POSIX_GUARD(s2n_tls13_cert_read_and_verify_signature(conn,
  128. conn->handshake_params.server_cert_sig_scheme));
  129. }
  130. return 0;
  131. }
  132. int s2n_tls13_cert_read_and_verify_signature(struct s2n_connection *conn,
  133. const struct s2n_signature_scheme *chosen_sig_scheme)
  134. {
  135. struct s2n_stuffer *in = &conn->handshake.io;
  136. DEFER_CLEANUP(struct s2n_blob signed_content = { 0 }, s2n_free);
  137. DEFER_CLEANUP(struct s2n_stuffer unsigned_content = { 0 }, s2n_stuffer_free);
  138. DEFER_CLEANUP(struct s2n_hash_state message_hash = { 0 }, s2n_hash_free);
  139. POSIX_GUARD(s2n_hash_new(&message_hash));
  140. /* Get signature size */
  141. uint16_t signature_size;
  142. POSIX_GUARD(s2n_stuffer_read_uint16(in, &signature_size));
  143. S2N_ERROR_IF(signature_size > s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE);
  144. /* Get wire signature */
  145. POSIX_GUARD(s2n_alloc(&signed_content, signature_size));
  146. signed_content.size = signature_size;
  147. POSIX_GUARD(s2n_stuffer_read_bytes(in, signed_content.data, signature_size));
  148. /* Verify signature. We send the opposite mode as we are trying to verify what was sent to us */
  149. if (conn->mode == S2N_CLIENT) {
  150. POSIX_GUARD(s2n_tls13_generate_unsigned_cert_verify_content(conn, &unsigned_content, S2N_SERVER));
  151. } else {
  152. POSIX_GUARD(s2n_tls13_generate_unsigned_cert_verify_content(conn, &unsigned_content, S2N_CLIENT));
  153. }
  154. POSIX_GUARD(s2n_hash_init(&message_hash, chosen_sig_scheme->hash_alg));
  155. POSIX_GUARD(s2n_hash_update(&message_hash, unsigned_content.blob.data,
  156. s2n_stuffer_data_available(&unsigned_content)));
  157. if (conn->mode == S2N_CLIENT) {
  158. POSIX_GUARD(s2n_pkey_verify(&conn->handshake_params.server_public_key, chosen_sig_scheme->sig_alg,
  159. &message_hash, &signed_content));
  160. } else {
  161. POSIX_GUARD(s2n_pkey_verify(&conn->handshake_params.client_public_key, chosen_sig_scheme->sig_alg,
  162. &message_hash, &signed_content));
  163. }
  164. return 0;
  165. }