keylog.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_GNUTLS) || \
  27. defined(USE_WOLFSSL) || \
  28. (defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || \
  29. defined(USE_QUICHE)
  30. #include "keylog.h"
  31. #include <curl/curl.h>
  32. /* The last #include files should be: */
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. #define KEYLOG_LABEL_MAXLEN (sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET") - 1)
  36. #define CLIENT_RANDOM_SIZE 32
  37. /*
  38. * The master secret in TLS 1.2 and before is always 48 bytes. In TLS 1.3, the
  39. * secret size depends on the cipher suite's hash function which is 32 bytes
  40. * for SHA-256 and 48 bytes for SHA-384.
  41. */
  42. #define SECRET_MAXLEN 48
  43. /* The fp for the open SSLKEYLOGFILE, or NULL if not open */
  44. static FILE *keylog_file_fp;
  45. void
  46. Curl_tls_keylog_open(void)
  47. {
  48. char *keylog_file_name;
  49. if(!keylog_file_fp) {
  50. keylog_file_name = curl_getenv("SSLKEYLOGFILE");
  51. if(keylog_file_name) {
  52. keylog_file_fp = fopen(keylog_file_name, FOPEN_APPENDTEXT);
  53. if(keylog_file_fp) {
  54. #ifdef _WIN32
  55. if(setvbuf(keylog_file_fp, NULL, _IONBF, 0))
  56. #else
  57. if(setvbuf(keylog_file_fp, NULL, _IOLBF, 4096))
  58. #endif
  59. {
  60. fclose(keylog_file_fp);
  61. keylog_file_fp = NULL;
  62. }
  63. }
  64. Curl_safefree(keylog_file_name);
  65. }
  66. }
  67. }
  68. void
  69. Curl_tls_keylog_close(void)
  70. {
  71. if(keylog_file_fp) {
  72. fclose(keylog_file_fp);
  73. keylog_file_fp = NULL;
  74. }
  75. }
  76. bool
  77. Curl_tls_keylog_enabled(void)
  78. {
  79. return keylog_file_fp != NULL;
  80. }
  81. bool
  82. Curl_tls_keylog_write_line(const char *line)
  83. {
  84. /* The current maximum valid keylog line length LF and NUL is 195. */
  85. size_t linelen;
  86. char buf[256];
  87. if(!keylog_file_fp || !line) {
  88. return FALSE;
  89. }
  90. linelen = strlen(line);
  91. if(linelen == 0 || linelen > sizeof(buf) - 2) {
  92. /* Empty line or too big to fit in a LF and NUL. */
  93. return FALSE;
  94. }
  95. memcpy(buf, line, linelen);
  96. if(line[linelen - 1] != '\n') {
  97. buf[linelen++] = '\n';
  98. }
  99. buf[linelen] = '\0';
  100. /* Using fputs here instead of fprintf since libcurl's fprintf replacement
  101. may not be thread-safe. */
  102. fputs(buf, keylog_file_fp);
  103. return TRUE;
  104. }
  105. bool
  106. Curl_tls_keylog_write(const char *label,
  107. const unsigned char client_random[CLIENT_RANDOM_SIZE],
  108. const unsigned char *secret, size_t secretlen)
  109. {
  110. const char *hex = "0123456789ABCDEF";
  111. size_t pos, i;
  112. char line[KEYLOG_LABEL_MAXLEN + 1 + 2 * CLIENT_RANDOM_SIZE + 1 +
  113. 2 * SECRET_MAXLEN + 1 + 1];
  114. if(!keylog_file_fp) {
  115. return FALSE;
  116. }
  117. pos = strlen(label);
  118. if(pos > KEYLOG_LABEL_MAXLEN || !secretlen || secretlen > SECRET_MAXLEN) {
  119. /* Should never happen - sanity check anyway. */
  120. return FALSE;
  121. }
  122. memcpy(line, label, pos);
  123. line[pos++] = ' ';
  124. /* Client Random */
  125. for(i = 0; i < CLIENT_RANDOM_SIZE; i++) {
  126. line[pos++] = hex[client_random[i] >> 4];
  127. line[pos++] = hex[client_random[i] & 0xF];
  128. }
  129. line[pos++] = ' ';
  130. /* Secret */
  131. for(i = 0; i < secretlen; i++) {
  132. line[pos++] = hex[secret[i] >> 4];
  133. line[pos++] = hex[secret[i] & 0xF];
  134. }
  135. line[pos++] = '\n';
  136. line[pos] = '\0';
  137. /* Using fputs here instead of fprintf since libcurl's fprintf replacement
  138. may not be thread-safe. */
  139. fputs(line, keylog_file_fp);
  140. return TRUE;
  141. }
  142. #endif /* TLS or QUIC backend */