ssl_txt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <openssl/buffer.h>
  12. #include "ssl_locl.h"
  13. #ifndef OPENSSL_NO_STDIO
  14. int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
  15. {
  16. BIO *b;
  17. int ret;
  18. if ((b = BIO_new(BIO_s_file())) == NULL) {
  19. SSLerr(SSL_F_SSL_SESSION_PRINT_FP, ERR_R_BUF_LIB);
  20. return 0;
  21. }
  22. BIO_set_fp(b, fp, BIO_NOCLOSE);
  23. ret = SSL_SESSION_print(b, x);
  24. BIO_free(b);
  25. return ret;
  26. }
  27. #endif
  28. int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
  29. {
  30. size_t i;
  31. const char *s;
  32. int istls13;
  33. if (x == NULL)
  34. goto err;
  35. istls13 = (x->ssl_version == TLS1_3_VERSION);
  36. if (BIO_puts(bp, "SSL-Session:\n") <= 0)
  37. goto err;
  38. s = ssl_protocol_to_string(x->ssl_version);
  39. if (BIO_printf(bp, " Protocol : %s\n", s) <= 0)
  40. goto err;
  41. if (x->cipher == NULL) {
  42. if (((x->cipher_id) & 0xff000000) == 0x02000000) {
  43. if (BIO_printf(bp, " Cipher : %06lX\n",
  44. x->cipher_id & 0xffffff) <= 0)
  45. goto err;
  46. } else {
  47. if (BIO_printf(bp, " Cipher : %04lX\n",
  48. x->cipher_id & 0xffff) <= 0)
  49. goto err;
  50. }
  51. } else {
  52. if (BIO_printf(bp, " Cipher : %s\n",
  53. ((x->cipher->name == NULL) ? "unknown"
  54. : x->cipher->name)) <= 0)
  55. goto err;
  56. }
  57. if (BIO_puts(bp, " Session-ID: ") <= 0)
  58. goto err;
  59. for (i = 0; i < x->session_id_length; i++) {
  60. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  61. goto err;
  62. }
  63. if (BIO_puts(bp, "\n Session-ID-ctx: ") <= 0)
  64. goto err;
  65. for (i = 0; i < x->sid_ctx_length; i++) {
  66. if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
  67. goto err;
  68. }
  69. if (istls13) {
  70. if (BIO_puts(bp, "\n Resumption PSK: ") <= 0)
  71. goto err;
  72. } else if (BIO_puts(bp, "\n Master-Key: ") <= 0)
  73. goto err;
  74. for (i = 0; i < x->master_key_length; i++) {
  75. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  76. goto err;
  77. }
  78. #ifndef OPENSSL_NO_PSK
  79. if (BIO_puts(bp, "\n PSK identity: ") <= 0)
  80. goto err;
  81. if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
  82. goto err;
  83. if (BIO_puts(bp, "\n PSK identity hint: ") <= 0)
  84. goto err;
  85. if (BIO_printf
  86. (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
  87. goto err;
  88. #endif
  89. #ifndef OPENSSL_NO_SRP
  90. if (BIO_puts(bp, "\n SRP username: ") <= 0)
  91. goto err;
  92. if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
  93. goto err;
  94. #endif
  95. if (x->ext.tick_lifetime_hint) {
  96. if (BIO_printf(bp,
  97. "\n TLS session ticket lifetime hint: %ld (seconds)",
  98. x->ext.tick_lifetime_hint) <= 0)
  99. goto err;
  100. }
  101. if (x->ext.tick) {
  102. if (BIO_puts(bp, "\n TLS session ticket:\n") <= 0)
  103. goto err;
  104. /* TODO(size_t): Convert this call */
  105. if (BIO_dump_indent
  106. (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
  107. <= 0)
  108. goto err;
  109. }
  110. #ifndef OPENSSL_NO_COMP
  111. if (x->compress_meth != 0) {
  112. SSL_COMP *comp = NULL;
  113. if (!ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp, 0))
  114. goto err;
  115. if (comp == NULL) {
  116. if (BIO_printf(bp, "\n Compression: %d", x->compress_meth) <= 0)
  117. goto err;
  118. } else {
  119. if (BIO_printf(bp, "\n Compression: %d (%s)", comp->id,
  120. comp->name) <= 0)
  121. goto err;
  122. }
  123. }
  124. #endif
  125. if (x->time != 0L) {
  126. if (BIO_printf(bp, "\n Start Time: %ld", x->time) <= 0)
  127. goto err;
  128. }
  129. if (x->timeout != 0L) {
  130. if (BIO_printf(bp, "\n Timeout : %ld (sec)", x->timeout) <= 0)
  131. goto err;
  132. }
  133. if (BIO_puts(bp, "\n") <= 0)
  134. goto err;
  135. if (BIO_puts(bp, " Verify return code: ") <= 0)
  136. goto err;
  137. if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
  138. X509_verify_cert_error_string(x->verify_result)) <= 0)
  139. goto err;
  140. if (BIO_printf(bp, " Extended master secret: %s\n",
  141. x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
  142. goto err;
  143. if (istls13) {
  144. if (BIO_printf(bp, " Max Early Data: %u\n",
  145. x->ext.max_early_data) <= 0)
  146. goto err;
  147. }
  148. return 1;
  149. err:
  150. return 0;
  151. }
  152. /*
  153. * print session id and master key in NSS keylog format (RSA
  154. * Session-ID:<session id> Master-Key:<master key>)
  155. */
  156. int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
  157. {
  158. size_t i;
  159. if (x == NULL)
  160. goto err;
  161. if (x->session_id_length == 0 || x->master_key_length == 0)
  162. goto err;
  163. /*
  164. * the RSA prefix is required by the format's definition although there's
  165. * nothing RSA-specific in the output, therefore, we don't have to check if
  166. * the cipher suite is based on RSA
  167. */
  168. if (BIO_puts(bp, "RSA ") <= 0)
  169. goto err;
  170. if (BIO_puts(bp, "Session-ID:") <= 0)
  171. goto err;
  172. for (i = 0; i < x->session_id_length; i++) {
  173. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  174. goto err;
  175. }
  176. if (BIO_puts(bp, " Master-Key:") <= 0)
  177. goto err;
  178. for (i = 0; i < x->master_key_length; i++) {
  179. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  180. goto err;
  181. }
  182. if (BIO_puts(bp, "\n") <= 0)
  183. goto err;
  184. return 1;
  185. err:
  186. return 0;
  187. }