ssl_txt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the OpenSSL license (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. if (x == NULL)
  33. goto err;
  34. if (BIO_puts(bp, "SSL-Session:\n") <= 0)
  35. goto err;
  36. s = ssl_protocol_to_string(x->ssl_version);
  37. if (BIO_printf(bp, " Protocol : %s\n", s) <= 0)
  38. goto err;
  39. if (x->cipher == NULL) {
  40. if (((x->cipher_id) & 0xff000000) == 0x02000000) {
  41. if (BIO_printf
  42. (bp, " Cipher : %06lX\n", x->cipher_id & 0xffffff) <= 0)
  43. goto err;
  44. } else {
  45. if (BIO_printf
  46. (bp, " Cipher : %04lX\n", x->cipher_id & 0xffff) <= 0)
  47. goto err;
  48. }
  49. } else {
  50. if (BIO_printf
  51. (bp, " Cipher : %s\n",
  52. ((x->cipher == NULL) ? "unknown" : x->cipher->name)) <= 0)
  53. goto err;
  54. }
  55. if (BIO_puts(bp, " Session-ID: ") <= 0)
  56. goto err;
  57. for (i = 0; i < x->session_id_length; i++) {
  58. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  59. goto err;
  60. }
  61. if (BIO_puts(bp, "\n Session-ID-ctx: ") <= 0)
  62. goto err;
  63. for (i = 0; i < x->sid_ctx_length; i++) {
  64. if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
  65. goto err;
  66. }
  67. if (BIO_puts(bp, "\n Master-Key: ") <= 0)
  68. goto err;
  69. for (i = 0; i < x->master_key_length; i++) {
  70. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  71. goto err;
  72. }
  73. #ifndef OPENSSL_NO_PSK
  74. if (BIO_puts(bp, "\n PSK identity: ") <= 0)
  75. goto err;
  76. if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
  77. goto err;
  78. if (BIO_puts(bp, "\n PSK identity hint: ") <= 0)
  79. goto err;
  80. if (BIO_printf
  81. (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
  82. goto err;
  83. #endif
  84. #ifndef OPENSSL_NO_SRP
  85. if (BIO_puts(bp, "\n SRP username: ") <= 0)
  86. goto err;
  87. if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
  88. goto err;
  89. #endif
  90. if (x->ext.tick_lifetime_hint) {
  91. if (BIO_printf(bp,
  92. "\n TLS session ticket lifetime hint: %ld (seconds)",
  93. x->ext.tick_lifetime_hint) <= 0)
  94. goto err;
  95. }
  96. if (x->ext.tick) {
  97. if (BIO_puts(bp, "\n TLS session ticket:\n") <= 0)
  98. goto err;
  99. /* TODO(size_t): Convert this call */
  100. if (BIO_dump_indent
  101. (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
  102. <= 0)
  103. goto err;
  104. }
  105. #ifndef OPENSSL_NO_COMP
  106. if (x->compress_meth != 0) {
  107. SSL_COMP *comp = NULL;
  108. if (!ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp, 0))
  109. goto err;
  110. if (comp == NULL) {
  111. if (BIO_printf(bp, "\n Compression: %d", x->compress_meth) <= 0)
  112. goto err;
  113. } else {
  114. if (BIO_printf(bp, "\n Compression: %d (%s)", comp->id,
  115. comp->name) <= 0)
  116. goto err;
  117. }
  118. }
  119. #endif
  120. if (x->time != 0L) {
  121. if (BIO_printf(bp, "\n Start Time: %ld", x->time) <= 0)
  122. goto err;
  123. }
  124. if (x->timeout != 0L) {
  125. if (BIO_printf(bp, "\n Timeout : %ld (sec)", x->timeout) <= 0)
  126. goto err;
  127. }
  128. if (BIO_puts(bp, "\n") <= 0)
  129. goto err;
  130. if (BIO_puts(bp, " Verify return code: ") <= 0)
  131. goto err;
  132. if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
  133. X509_verify_cert_error_string(x->verify_result)) <= 0)
  134. goto err;
  135. if (BIO_printf(bp, " Extended master secret: %s\n",
  136. x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
  137. goto err;
  138. return 1;
  139. err:
  140. return 0;
  141. }
  142. /*
  143. * print session id and master key in NSS keylog format (RSA
  144. * Session-ID:<session id> Master-Key:<master key>)
  145. */
  146. int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
  147. {
  148. size_t i;
  149. if (x == NULL)
  150. goto err;
  151. if (x->session_id_length == 0 || x->master_key_length == 0)
  152. goto err;
  153. /*
  154. * the RSA prefix is required by the format's definition although there's
  155. * nothing RSA-specific in the output, therefore, we don't have to check if
  156. * the cipher suite is based on RSA
  157. */
  158. if (BIO_puts(bp, "RSA ") <= 0)
  159. goto err;
  160. if (BIO_puts(bp, "Session-ID:") <= 0)
  161. goto err;
  162. for (i = 0; i < x->session_id_length; i++) {
  163. if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
  164. goto err;
  165. }
  166. if (BIO_puts(bp, " Master-Key:") <= 0)
  167. goto err;
  168. for (i = 0; i < x->master_key_length; i++) {
  169. if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
  170. goto err;
  171. }
  172. if (BIO_puts(bp, "\n") <= 0)
  173. goto err;
  174. return 1;
  175. err:
  176. return 0;
  177. }