ssl_txt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 1995-2020 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_local.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. ERR_raise(ERR_LIB_SSL, 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. if (BIO_dump_indent
  105. (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
  106. <= 0)
  107. goto err;
  108. }
  109. #ifndef OPENSSL_NO_COMP
  110. if (x->compress_meth != 0) {
  111. SSL_COMP *comp = NULL;
  112. if (!ssl_cipher_get_evp(NULL, x, NULL, NULL, NULL, NULL, &comp, 0))
  113. goto err;
  114. if (comp == NULL) {
  115. if (BIO_printf(bp, "\n Compression: %d", x->compress_meth) <= 0)
  116. goto err;
  117. } else {
  118. if (BIO_printf(bp, "\n Compression: %d (%s)", comp->id,
  119. comp->name) <= 0)
  120. goto err;
  121. }
  122. }
  123. #endif
  124. if (x->time != 0L) {
  125. if (BIO_printf(bp, "\n Start Time: %ld", x->time) <= 0)
  126. goto err;
  127. }
  128. if (x->timeout != 0L) {
  129. if (BIO_printf(bp, "\n Timeout : %ld (sec)", x->timeout) <= 0)
  130. goto err;
  131. }
  132. if (BIO_puts(bp, "\n") <= 0)
  133. goto err;
  134. if (BIO_puts(bp, " Verify return code: ") <= 0)
  135. goto err;
  136. if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
  137. X509_verify_cert_error_string(x->verify_result)) <= 0)
  138. goto err;
  139. if (BIO_printf(bp, " Extended master secret: %s\n",
  140. x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
  141. goto err;
  142. if (istls13) {
  143. if (BIO_printf(bp, " Max Early Data: %u\n",
  144. x->ext.max_early_data) <= 0)
  145. goto err;
  146. }
  147. return 1;
  148. err:
  149. return 0;
  150. }
  151. /*
  152. * print session id and master key in NSS keylog format (RSA
  153. * Session-ID:<session id> Master-Key:<master key>)
  154. */
  155. int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
  156. {
  157. size_t i;
  158. if (x == NULL)
  159. goto err;
  160. if (x->session_id_length == 0 || x->master_key_length == 0)
  161. goto err;
  162. /*
  163. * the RSA prefix is required by the format's definition although there's
  164. * nothing RSA-specific in the output, therefore, we don't have to check if
  165. * the cipher suite is based on RSA
  166. */
  167. if (BIO_puts(bp, "RSA ") <= 0)
  168. goto err;
  169. if (BIO_puts(bp, "Session-ID:") <= 0)
  170. goto err;
  171. for (i = 0; i < x->session_id_length; i++) {
  172. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  173. goto err;
  174. }
  175. if (BIO_puts(bp, " Master-Key:") <= 0)
  176. goto err;
  177. for (i = 0; i < x->master_key_length; i++) {
  178. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  179. goto err;
  180. }
  181. if (BIO_puts(bp, "\n") <= 0)
  182. goto err;
  183. return 1;
  184. err:
  185. return 0;
  186. }