2
0

ssl_txt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 1995-2022 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 (!ossl_time_is_zero(x->time)) {
  125. if (BIO_printf(bp, "\n Start Time: %lld",
  126. (long long)ossl_time_to_time_t(x->time)) <= 0)
  127. goto err;
  128. }
  129. if (!ossl_time_is_zero(x->timeout)) {
  130. if (BIO_printf(bp, "\n Timeout : %lld (sec)",
  131. (long long)ossl_time2seconds(x->timeout)) <= 0)
  132. goto err;
  133. }
  134. if (BIO_puts(bp, "\n") <= 0)
  135. goto err;
  136. if (BIO_puts(bp, " Verify return code: ") <= 0)
  137. goto err;
  138. if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
  139. X509_verify_cert_error_string(x->verify_result)) <= 0)
  140. goto err;
  141. if (BIO_printf(bp, " Extended master secret: %s\n",
  142. x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
  143. goto err;
  144. if (istls13) {
  145. if (BIO_printf(bp, " Max Early Data: %u\n",
  146. (unsigned int)x->ext.max_early_data) <= 0)
  147. goto err;
  148. }
  149. return 1;
  150. err:
  151. return 0;
  152. }
  153. /*
  154. * print session id and master key in NSS keylog format (RSA
  155. * Session-ID:<session id> Master-Key:<master key>)
  156. */
  157. int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
  158. {
  159. size_t i;
  160. if (x == NULL)
  161. goto err;
  162. if (x->session_id_length == 0 || x->master_key_length == 0)
  163. goto err;
  164. /*
  165. * the RSA prefix is required by the format's definition although there's
  166. * nothing RSA-specific in the output, therefore, we don't have to check if
  167. * the cipher suite is based on RSA
  168. */
  169. if (BIO_puts(bp, "RSA ") <= 0)
  170. goto err;
  171. if (BIO_puts(bp, "Session-ID:") <= 0)
  172. goto err;
  173. for (i = 0; i < x->session_id_length; i++) {
  174. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  175. goto err;
  176. }
  177. if (BIO_puts(bp, " Master-Key:") <= 0)
  178. goto err;
  179. for (i = 0; i < x->master_key_length; i++) {
  180. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  181. goto err;
  182. }
  183. if (BIO_puts(bp, "\n") <= 0)
  184. goto err;
  185. return 1;
  186. err:
  187. return 0;
  188. }