keylog.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(USE_OPENSSL) || \
  26. defined(USE_WOLFSSL) || \
  27. (defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || \
  28. defined(USE_QUICHE)
  29. #include "keylog.h"
  30. #include <curl/curl.h>
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. #define KEYLOG_LABEL_MAXLEN (sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET") - 1)
  35. #define CLIENT_RANDOM_SIZE 32
  36. /*
  37. * The master secret in TLS 1.2 and before is always 48 bytes. In TLS 1.3, the
  38. * secret size depends on the cipher suite's hash function which is 32 bytes
  39. * for SHA-256 and 48 bytes for SHA-384.
  40. */
  41. #define SECRET_MAXLEN 48
  42. /* The fp for the open SSLKEYLOGFILE, or NULL if not open */
  43. static FILE *keylog_file_fp;
  44. void
  45. Curl_tls_keylog_open(void)
  46. {
  47. char *keylog_file_name;
  48. if(!keylog_file_fp) {
  49. keylog_file_name = curl_getenv("SSLKEYLOGFILE");
  50. if(keylog_file_name) {
  51. keylog_file_fp = fopen(keylog_file_name, FOPEN_APPENDTEXT);
  52. if(keylog_file_fp) {
  53. #ifdef _WIN32
  54. if(setvbuf(keylog_file_fp, NULL, _IONBF, 0))
  55. #else
  56. if(setvbuf(keylog_file_fp, NULL, _IOLBF, 4096))
  57. #endif
  58. {
  59. fclose(keylog_file_fp);
  60. keylog_file_fp = NULL;
  61. }
  62. }
  63. Curl_safefree(keylog_file_name);
  64. }
  65. }
  66. }
  67. void
  68. Curl_tls_keylog_close(void)
  69. {
  70. if(keylog_file_fp) {
  71. fclose(keylog_file_fp);
  72. keylog_file_fp = NULL;
  73. }
  74. }
  75. bool
  76. Curl_tls_keylog_enabled(void)
  77. {
  78. return keylog_file_fp != NULL;
  79. }
  80. bool
  81. Curl_tls_keylog_write_line(const char *line)
  82. {
  83. /* The current maximum valid keylog line length LF and NUL is 195. */
  84. size_t linelen;
  85. char buf[256];
  86. if(!keylog_file_fp || !line) {
  87. return false;
  88. }
  89. linelen = strlen(line);
  90. if(linelen == 0 || linelen > sizeof(buf) - 2) {
  91. /* Empty line or too big to fit in a LF and NUL. */
  92. return false;
  93. }
  94. memcpy(buf, line, linelen);
  95. if(line[linelen - 1] != '\n') {
  96. buf[linelen++] = '\n';
  97. }
  98. buf[linelen] = '\0';
  99. /* Using fputs here instead of fprintf since libcurl's fprintf replacement
  100. may not be thread-safe. */
  101. fputs(buf, keylog_file_fp);
  102. return true;
  103. }
  104. bool
  105. Curl_tls_keylog_write(const char *label,
  106. const unsigned char client_random[CLIENT_RANDOM_SIZE],
  107. const unsigned char *secret, size_t secretlen)
  108. {
  109. const char *hex = "0123456789ABCDEF";
  110. size_t pos, i;
  111. char line[KEYLOG_LABEL_MAXLEN + 1 + 2 * CLIENT_RANDOM_SIZE + 1 +
  112. 2 * SECRET_MAXLEN + 1 + 1];
  113. if(!keylog_file_fp) {
  114. return false;
  115. }
  116. pos = strlen(label);
  117. if(pos > KEYLOG_LABEL_MAXLEN || !secretlen || secretlen > SECRET_MAXLEN) {
  118. /* Should never happen - sanity check anyway. */
  119. return false;
  120. }
  121. memcpy(line, label, pos);
  122. line[pos++] = ' ';
  123. /* Client Random */
  124. for(i = 0; i < CLIENT_RANDOM_SIZE; i++) {
  125. line[pos++] = hex[client_random[i] >> 4];
  126. line[pos++] = hex[client_random[i] & 0xF];
  127. }
  128. line[pos++] = ' ';
  129. /* Secret */
  130. for(i = 0; i < secretlen; i++) {
  131. line[pos++] = hex[secret[i] >> 4];
  132. line[pos++] = hex[secret[i] & 0xF];
  133. }
  134. line[pos++] = '\n';
  135. line[pos] = '\0';
  136. /* Using fputs here instead of fprintf since libcurl's fprintf replacement
  137. may not be thread-safe. */
  138. fputs(line, keylog_file_fp);
  139. return true;
  140. }
  141. #endif /* TLS or QUIC backend */