blob: b1254b09d7e2edf31918d7683a938b787f7b88d6 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Rich Salzc80149d2017-06-20 10:14:36 -04003 * Copyright 2005 Nokia. All rights reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00004 *
Rich Salz846e33c2016-05-17 14:18:30 -04005 * 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
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00009 */
Rich Salz846e33c2016-05-17 14:18:30 -040010
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000011#include <stdio.h>
Bodo Möllerec577821999-04-23 22:13:45 +000012#include <openssl/buffer.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000013#include "ssl_locl.h"
14
Rich Salz4b618842015-01-14 15:57:28 -050015#ifndef OPENSSL_NO_STDIO
Ben Laurie0821bcd2005-03-30 10:26:02 +000016int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
Matt Caswell0f113f32015-01-22 03:40:55 +000017{
18 BIO *b;
19 int ret;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000020
Rich Salz9982cbb2015-09-30 14:32:49 -040021 if ((b = BIO_new(BIO_s_file())) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +000022 SSLerr(SSL_F_SSL_SESSION_PRINT_FP, ERR_R_BUF_LIB);
KaoruToda26a7d932017-10-17 23:04:09 +090023 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +000024 }
25 BIO_set_fp(b, fp, BIO_NOCLOSE);
26 ret = SSL_SESSION_print(b, x);
27 BIO_free(b);
KaoruToda26a7d932017-10-17 23:04:09 +090028 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000029}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000030#endif
31
Ben Laurie0821bcd2005-03-30 10:26:02 +000032int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
Matt Caswell0f113f32015-01-22 03:40:55 +000033{
Matt Caswellec60ccc2016-10-04 20:31:19 +010034 size_t i;
Matt Caswell0f113f32015-01-22 03:40:55 +000035 const char *s;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000036
Matt Caswell0f113f32015-01-22 03:40:55 +000037 if (x == NULL)
38 goto err;
39 if (BIO_puts(bp, "SSL-Session:\n") <= 0)
40 goto err;
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +010041 s = ssl_protocol_to_string(x->ssl_version);
Matt Caswell0f113f32015-01-22 03:40:55 +000042 if (BIO_printf(bp, " Protocol : %s\n", s) <= 0)
43 goto err;
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000044
Matt Caswell0f113f32015-01-22 03:40:55 +000045 if (x->cipher == NULL) {
46 if (((x->cipher_id) & 0xff000000) == 0x02000000) {
47 if (BIO_printf
48 (bp, " Cipher : %06lX\n", x->cipher_id & 0xffffff) <= 0)
49 goto err;
50 } else {
51 if (BIO_printf
52 (bp, " Cipher : %04lX\n", x->cipher_id & 0xffff) <= 0)
53 goto err;
54 }
55 } else {
56 if (BIO_printf
57 (bp, " Cipher : %s\n",
58 ((x->cipher == NULL) ? "unknown" : x->cipher->name)) <= 0)
59 goto err;
60 }
61 if (BIO_puts(bp, " Session-ID: ") <= 0)
62 goto err;
63 for (i = 0; i < x->session_id_length; i++) {
64 if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
65 goto err;
66 }
67 if (BIO_puts(bp, "\n Session-ID-ctx: ") <= 0)
68 goto err;
69 for (i = 0; i < x->sid_ctx_length; i++) {
70 if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
71 goto err;
72 }
73 if (BIO_puts(bp, "\n Master-Key: ") <= 0)
74 goto err;
Matt Caswellec60ccc2016-10-04 20:31:19 +010075 for (i = 0; i < x->master_key_length; i++) {
Matt Caswell0f113f32015-01-22 03:40:55 +000076 if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
77 goto err;
78 }
Nils Larschddac1972006-03-10 23:06:27 +000079#ifndef OPENSSL_NO_PSK
Matt Caswell0f113f32015-01-22 03:40:55 +000080 if (BIO_puts(bp, "\n PSK identity: ") <= 0)
81 goto err;
82 if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
83 goto err;
84 if (BIO_puts(bp, "\n PSK identity hint: ") <= 0)
85 goto err;
86 if (BIO_printf
87 (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
88 goto err;
Nils Larschddac1972006-03-10 23:06:27 +000089#endif
Ben Laurieedc032b2011-03-12 17:01:19 +000090#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +000091 if (BIO_puts(bp, "\n SRP username: ") <= 0)
92 goto err;
93 if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
94 goto err;
Ben Laurieedc032b2011-03-12 17:01:19 +000095#endif
Rich Salzaff8c122016-12-08 14:18:40 -050096 if (x->ext.tick_lifetime_hint) {
Matt Caswell0f113f32015-01-22 03:40:55 +000097 if (BIO_printf(bp,
98 "\n TLS session ticket lifetime hint: %ld (seconds)",
Rich Salzaff8c122016-12-08 14:18:40 -050099 x->ext.tick_lifetime_hint) <= 0)
Matt Caswell0f113f32015-01-22 03:40:55 +0000100 goto err;
101 }
Rich Salzaff8c122016-12-08 14:18:40 -0500102 if (x->ext.tick) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000103 if (BIO_puts(bp, "\n TLS session ticket:\n") <= 0)
104 goto err;
Matt Caswell348240c2016-10-19 15:11:24 +0100105 /* TODO(size_t): Convert this call */
Emilia Kaspera230b262016-08-05 19:03:17 +0200106 if (BIO_dump_indent
Rich Salzaff8c122016-12-08 14:18:40 -0500107 (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
Matt Caswell0f113f32015-01-22 03:40:55 +0000108 <= 0)
109 goto err;
110 }
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000111#ifndef OPENSSL_NO_COMP
Matt Caswell0f113f32015-01-22 03:40:55 +0000112 if (x->compress_meth != 0) {
113 SSL_COMP *comp = NULL;
Mark J. Cox413c4f41999-02-16 09:22:21 +0000114
Viktor Dukhovni61986d32015-04-16 01:50:03 -0400115 if (!ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp, 0))
Matt Caswell69f68232015-03-06 14:37:17 +0000116 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000117 if (comp == NULL) {
Emilia Kaspera230b262016-08-05 19:03:17 +0200118 if (BIO_printf(bp, "\n Compression: %d", x->compress_meth) <= 0)
Matt Caswell0f113f32015-01-22 03:40:55 +0000119 goto err;
120 } else {
Rich Salz9a555702015-05-08 12:05:36 -0400121 if (BIO_printf(bp, "\n Compression: %d (%s)", comp->id,
Emilia Kaspera230b262016-08-05 19:03:17 +0200122 comp->name) <= 0)
Matt Caswell0f113f32015-01-22 03:40:55 +0000123 goto err;
124 }
125 }
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000126#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000127 if (x->time != 0L) {
128 if (BIO_printf(bp, "\n Start Time: %ld", x->time) <= 0)
129 goto err;
130 }
131 if (x->timeout != 0L) {
132 if (BIO_printf(bp, "\n Timeout : %ld (sec)", x->timeout) <= 0)
133 goto err;
134 }
135 if (BIO_puts(bp, "\n") <= 0)
136 goto err;
Dr. Stephen Henson25f923d2000-01-09 14:21:40 +0000137
Matt Caswell0f113f32015-01-22 03:40:55 +0000138 if (BIO_puts(bp, " Verify return code: ") <= 0)
139 goto err;
140 if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
141 X509_verify_cert_error_string(x->verify_result)) <= 0)
142 goto err;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000143
Dr. Stephen Hensonddc06b32015-01-23 02:45:13 +0000144 if (BIO_printf(bp, " Extended master secret: %s\n",
145 x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
146 goto err;
147
KaoruToda208fb892017-10-09 20:05:58 +0900148 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000149 err:
KaoruToda26a7d932017-10-17 23:04:09 +0900150 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000151}
152
153/*
154 * print session id and master key in NSS keylog format (RSA
155 * Session-ID:<session id> Master-Key:<master key>)
156 */
Martin Kaiser189ae362014-05-24 00:02:24 +0100157int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
Matt Caswell0f113f32015-01-22 03:40:55 +0000158{
Matt Caswellec60ccc2016-10-04 20:31:19 +0100159 size_t i;
Martin Kaiser189ae362014-05-24 00:02:24 +0100160
Matt Caswell0f113f32015-01-22 03:40:55 +0000161 if (x == NULL)
162 goto err;
163 if (x->session_id_length == 0 || x->master_key_length == 0)
164 goto err;
Martin Kaiser189ae362014-05-24 00:02:24 +0100165
Matt Caswell0f113f32015-01-22 03:40:55 +0000166 /*
167 * the RSA prefix is required by the format's definition although there's
FdaSilvaYY8483a002016-03-10 21:34:48 +0100168 * nothing RSA-specific in the output, therefore, we don't have to check if
Matt Caswell0f113f32015-01-22 03:40:55 +0000169 * the cipher suite is based on RSA
170 */
171 if (BIO_puts(bp, "RSA ") <= 0)
172 goto err;
Martin Kaiser189ae362014-05-24 00:02:24 +0100173
Matt Caswell0f113f32015-01-22 03:40:55 +0000174 if (BIO_puts(bp, "Session-ID:") <= 0)
175 goto err;
176 for (i = 0; i < x->session_id_length; i++) {
177 if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
178 goto err;
179 }
180 if (BIO_puts(bp, " Master-Key:") <= 0)
181 goto err;
Matt Caswellec60ccc2016-10-04 20:31:19 +0100182 for (i = 0; i < x->master_key_length; i++) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000183 if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
184 goto err;
185 }
186 if (BIO_puts(bp, "\n") <= 0)
187 goto err;
Martin Kaiser189ae362014-05-24 00:02:24 +0100188
KaoruToda208fb892017-10-09 20:05:58 +0900189 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000190 err:
KaoruToda26a7d932017-10-17 23:04:09 +0900191 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000192}