s2n_key_log.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  16. * This module implements key logging as defined by the NSS Key Log Format
  17. *
  18. * See https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
  19. *
  20. * This key log file is a series of lines. Comment lines begin with a sharp
  21. * character ('#') and are ignored. Secrets follow the format
  22. * <Label> <space> <ClientRandom> <space> <Secret> where:
  23. *
  24. * <Label> describes the following secret.
  25. * <ClientRandom> is 32 bytes Random value from the Client Hello message, encoded as 64 hexadecimal characters.
  26. * <Secret> depends on the Label (see below).
  27. *
  28. * The following labels are defined, followed by a description of the secret:
  29. *
  30. * RSA: 48 bytes for the premaster secret, encoded as 96 hexadecimal characters (removed in NSS 3.34)
  31. * CLIENT_RANDOM: 48 bytes for the master secret, encoded as 96 hexadecimal characters (for SSL 3.0, TLS 1.0, 1.1 and 1.2)
  32. * CLIENT_EARLY_TRAFFIC_SECRET: the hex-encoded early traffic secret for the client side (for TLS 1.3)
  33. * CLIENT_HANDSHAKE_TRAFFIC_SECRET: the hex-encoded handshake traffic secret for the client side (for TLS 1.3)
  34. * SERVER_HANDSHAKE_TRAFFIC_SECRET: the hex-encoded handshake traffic secret for the server side (for TLS 1.3)
  35. * CLIENT_TRAFFIC_SECRET_0: the first hex-encoded application traffic secret for the client side (for TLS 1.3)
  36. * SERVER_TRAFFIC_SECRET_0: the first hex-encoded application traffic secret for the server side (for TLS 1.3)
  37. * EARLY_EXPORTER_SECRET: the hex-encoded early exporter secret (for TLS 1.3).
  38. * EXPORTER_SECRET: the hex-encoded exporter secret (for TLS 1.3)
  39. */
  40. #include "api/s2n.h"
  41. #include "tls/s2n_config.h"
  42. #include "tls/s2n_connection.h"
  43. #include "tls/s2n_crypto_constants.h"
  44. #include "tls/s2n_quic_support.h" /* this currently holds the s2n_secret_type_t enum */
  45. #include "utils/s2n_blob.h"
  46. #include "utils/s2n_safety.h"
  47. /* hex requires 2 chars per byte */
  48. #define HEX_ENCODING_SIZE 2
  49. S2N_RESULT s2n_key_log_hex_encode(struct s2n_stuffer *output, uint8_t *bytes, size_t len)
  50. {
  51. RESULT_ENSURE_MUT(output);
  52. RESULT_ENSURE_REF(bytes);
  53. const uint8_t chars[] = "0123456789abcdef";
  54. for (size_t i = 0; i < len; i++) {
  55. uint8_t upper = bytes[i] >> 4;
  56. uint8_t lower = bytes[i] & 0x0f;
  57. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, chars[upper]));
  58. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, chars[lower]));
  59. }
  60. return S2N_RESULT_OK;
  61. }
  62. S2N_RESULT s2n_key_log_tls13_secret(struct s2n_connection *conn, const struct s2n_blob *secret, s2n_secret_type_t secret_type)
  63. {
  64. RESULT_ENSURE_REF(conn);
  65. RESULT_ENSURE_REF(conn->config);
  66. RESULT_ENSURE_REF(secret);
  67. /* only emit keys if the callback has been set */
  68. if (!conn->config->key_log_cb) {
  69. return S2N_RESULT_OK;
  70. }
  71. const uint8_t client_early_traffic_label[] = "CLIENT_EARLY_TRAFFIC_SECRET ";
  72. const uint8_t client_handshake_label[] = "CLIENT_HANDSHAKE_TRAFFIC_SECRET ";
  73. const uint8_t server_handshake_label[] = "SERVER_HANDSHAKE_TRAFFIC_SECRET ";
  74. const uint8_t client_traffic_label[] = "CLIENT_TRAFFIC_SECRET_0 ";
  75. const uint8_t server_traffic_label[] = "SERVER_TRAFFIC_SECRET_0 ";
  76. const uint8_t exporter_secret_label[] = "EXPORTER_SECRET ";
  77. const uint8_t *label = NULL;
  78. uint8_t label_size = 0;
  79. switch (secret_type) {
  80. case S2N_CLIENT_EARLY_TRAFFIC_SECRET:
  81. label = client_early_traffic_label;
  82. label_size = sizeof(client_early_traffic_label) - 1;
  83. break;
  84. case S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET:
  85. label = client_handshake_label;
  86. label_size = sizeof(client_handshake_label) - 1;
  87. break;
  88. case S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET:
  89. label = server_handshake_label;
  90. label_size = sizeof(server_handshake_label) - 1;
  91. break;
  92. case S2N_CLIENT_APPLICATION_TRAFFIC_SECRET:
  93. label = client_traffic_label;
  94. label_size = sizeof(client_traffic_label) - 1;
  95. break;
  96. case S2N_SERVER_APPLICATION_TRAFFIC_SECRET:
  97. label = server_traffic_label;
  98. label_size = sizeof(server_traffic_label) - 1;
  99. break;
  100. case S2N_EXPORTER_SECRET:
  101. label = exporter_secret_label;
  102. label_size = sizeof(exporter_secret_label) - 1;
  103. break;
  104. default:
  105. /* Ignore the secret types we don't understand */
  106. return S2N_RESULT_OK;
  107. }
  108. const uint8_t len = label_size
  109. + S2N_TLS_RANDOM_DATA_LEN * HEX_ENCODING_SIZE
  110. + 1 /* SPACE */
  111. + secret->size * HEX_ENCODING_SIZE;
  112. DEFER_CLEANUP(struct s2n_stuffer output, s2n_stuffer_free);
  113. RESULT_GUARD_POSIX(s2n_stuffer_alloc(&output, len));
  114. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&output, label, label_size));
  115. RESULT_GUARD(s2n_key_log_hex_encode(&output, conn->handshake_params.client_random, S2N_TLS_RANDOM_DATA_LEN));
  116. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&output, ' '));
  117. RESULT_GUARD(s2n_key_log_hex_encode(&output, secret->data, secret->size));
  118. uint8_t *data = s2n_stuffer_raw_read(&output, len);
  119. RESULT_ENSURE_REF(data);
  120. conn->config->key_log_cb(conn->config->key_log_ctx, conn, data, len);
  121. return S2N_RESULT_OK;
  122. }
  123. S2N_RESULT s2n_key_log_tls12_secret(struct s2n_connection *conn)
  124. {
  125. RESULT_ENSURE_REF(conn);
  126. RESULT_ENSURE_REF(conn->config);
  127. /* only emit keys if the callback has been set */
  128. if (!conn->config->key_log_cb) {
  129. return S2N_RESULT_OK;
  130. }
  131. /* CLIENT_RANDOM: 48 bytes for the master secret, encoded as 96 hexadecimal characters (for SSL 3.0, TLS 1.0, 1.1 and 1.2) */
  132. const uint8_t label[] = "CLIENT_RANDOM ";
  133. const uint8_t label_size = sizeof(label) - 1;
  134. const uint8_t len = label_size
  135. + S2N_TLS_RANDOM_DATA_LEN * HEX_ENCODING_SIZE
  136. + 1 /* SPACE */
  137. + S2N_TLS_SECRET_LEN * HEX_ENCODING_SIZE;
  138. DEFER_CLEANUP(struct s2n_stuffer output, s2n_stuffer_free);
  139. RESULT_GUARD_POSIX(s2n_stuffer_alloc(&output, len));
  140. RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&output, label, label_size));
  141. RESULT_GUARD(s2n_key_log_hex_encode(&output, conn->handshake_params.client_random, S2N_TLS_RANDOM_DATA_LEN));
  142. RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&output, ' '));
  143. RESULT_GUARD(s2n_key_log_hex_encode(&output, conn->secrets.version.tls12.master_secret, S2N_TLS_SECRET_LEN));
  144. uint8_t *data = s2n_stuffer_raw_read(&output, len);
  145. RESULT_ENSURE_REF(data);
  146. conn->config->key_log_cb(conn->config->key_log_ctx, conn, data, len);
  147. return S2N_RESULT_OK;
  148. }