/src/openssl41/ssl/ssl_txt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 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 | | |
11 | | #include <stdio.h> |
12 | | #include <openssl/buffer.h> |
13 | | #include "ssl_local.h" |
14 | | |
15 | | #include "internal/comp.h" |
16 | | |
17 | | #ifndef OPENSSL_NO_STDIO |
18 | | int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x) |
19 | 0 | { |
20 | 0 | BIO *b; |
21 | 0 | int ret; |
22 | |
|
23 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
24 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB); |
25 | 0 | return 0; |
26 | 0 | } |
27 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
28 | 0 | ret = SSL_SESSION_print(b, x); |
29 | 0 | BIO_free(b); |
30 | 0 | return ret; |
31 | 0 | } |
32 | | #endif |
33 | | |
34 | | int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x) |
35 | 519 | { |
36 | 519 | size_t i; |
37 | 519 | const char *s; |
38 | 519 | int isversion13; |
39 | | |
40 | 519 | if (x == NULL) |
41 | 0 | goto err; |
42 | 519 | isversion13 = (x->ssl_version == TLS1_3_VERSION) |
43 | 497 | || (x->ssl_version == DTLS1_3_VERSION); |
44 | 519 | if (BIO_puts(bp, "SSL-Session:\n") <= 0) |
45 | 0 | goto err; |
46 | 519 | s = ssl_protocol_to_string(x->ssl_version); |
47 | 519 | if (BIO_printf(bp, " Protocol : %s\n", s) <= 0) |
48 | 0 | goto err; |
49 | | |
50 | 519 | if (x->cipher == NULL) { |
51 | 0 | if (((x->cipher_id) & 0xff000000) == 0x02000000) { |
52 | 0 | if (BIO_printf(bp, " Cipher : %06lX\n", |
53 | 0 | x->cipher_id & 0xffffff) |
54 | 0 | <= 0) |
55 | 0 | goto err; |
56 | 0 | } else { |
57 | 0 | if (BIO_printf(bp, " Cipher : %04lX\n", |
58 | 0 | x->cipher_id & 0xffff) |
59 | 0 | <= 0) |
60 | 0 | goto err; |
61 | 0 | } |
62 | 519 | } else { |
63 | 519 | if (BIO_printf(bp, " Cipher : %s\n", |
64 | 519 | ((x->cipher->name == NULL) ? "unknown" |
65 | 519 | : x->cipher->name)) |
66 | 519 | <= 0) |
67 | 0 | goto err; |
68 | 519 | } |
69 | 519 | if (BIO_puts(bp, " Session-ID: ") <= 0) |
70 | 0 | goto err; |
71 | 722 | for (i = 0; i < x->session_id_length; i++) { |
72 | 203 | if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0) |
73 | 0 | goto err; |
74 | 203 | } |
75 | 519 | if (BIO_puts(bp, "\n Session-ID-ctx: ") <= 0) |
76 | 0 | goto err; |
77 | 630 | for (i = 0; i < x->sid_ctx_length; i++) { |
78 | 111 | if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0) |
79 | 0 | goto err; |
80 | 111 | } |
81 | 519 | if (isversion13) { |
82 | 24 | if (BIO_puts(bp, "\n Resumption PSK: ") <= 0) |
83 | 0 | goto err; |
84 | 495 | } else if (BIO_puts(bp, "\n Master-Key: ") <= 0) |
85 | 0 | goto err; |
86 | 5.34k | for (i = 0; i < x->master_key_length; i++) { |
87 | 4.82k | if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0) |
88 | 0 | goto err; |
89 | 4.82k | } |
90 | 519 | #ifndef OPENSSL_NO_PSK |
91 | 519 | if (BIO_puts(bp, "\n PSK identity: ") <= 0) |
92 | 0 | goto err; |
93 | 519 | if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0) |
94 | 2 | goto err; |
95 | 517 | if (BIO_puts(bp, "\n PSK identity hint: ") <= 0) |
96 | 0 | goto err; |
97 | 517 | if (BIO_printf(bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0) |
98 | 2 | goto err; |
99 | 515 | #endif |
100 | 515 | #ifndef OPENSSL_NO_SRP |
101 | 515 | if (BIO_puts(bp, "\n SRP username: ") <= 0) |
102 | 0 | goto err; |
103 | 515 | if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0) |
104 | 2 | goto err; |
105 | 513 | #endif |
106 | 513 | if (x->ext.tick_lifetime_hint) { |
107 | 157 | if (BIO_printf(bp, |
108 | 157 | "\n TLS session ticket lifetime hint: %lu (seconds)", |
109 | 157 | x->ext.tick_lifetime_hint) |
110 | 157 | <= 0) |
111 | 0 | goto err; |
112 | 157 | } |
113 | 513 | if (x->ext.tick) { |
114 | 4 | if (BIO_puts(bp, "\n TLS session ticket:\n") <= 0) |
115 | 0 | goto err; |
116 | 4 | if (BIO_dump_indent(bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4) |
117 | 4 | <= 0) |
118 | 2 | goto err; |
119 | 4 | } |
120 | 511 | #ifndef OPENSSL_NO_COMP |
121 | 511 | if (x->compress_meth != 0) { |
122 | 2 | SSL_COMP *comp = NULL; |
123 | | |
124 | 2 | if (!ssl_cipher_get_evp(NULL, x, NULL, NULL, NULL, NULL, NULL, &comp, 0)) |
125 | 0 | goto err; |
126 | 2 | if (comp == NULL) { |
127 | 2 | if (BIO_printf(bp, "\n Compression: %u", x->compress_meth) <= 0) |
128 | 0 | goto err; |
129 | 2 | } else { |
130 | 0 | if (BIO_printf(bp, "\n Compression: %d (%s)", comp->id, |
131 | 0 | comp->name) |
132 | 0 | <= 0) |
133 | 0 | goto err; |
134 | 0 | } |
135 | 2 | } |
136 | 511 | #endif |
137 | 511 | if (!ossl_time_is_zero(x->time)) { |
138 | 507 | if (BIO_printf(bp, "\n Start Time: %lld", |
139 | 507 | (long long)ossl_time_to_time_t(x->time)) |
140 | 507 | <= 0) |
141 | 0 | goto err; |
142 | 507 | } |
143 | 511 | if (!ossl_time_is_zero(x->timeout)) { |
144 | 509 | if (BIO_printf(bp, "\n Timeout : %lld (sec)", |
145 | 509 | (long long)ossl_time2seconds(x->timeout)) |
146 | 509 | <= 0) |
147 | 0 | goto err; |
148 | 509 | } |
149 | 511 | if (BIO_puts(bp, "\n") <= 0) |
150 | 0 | goto err; |
151 | | |
152 | 511 | if (BIO_puts(bp, " Verify return code: ") <= 0) |
153 | 0 | goto err; |
154 | 511 | if (BIO_printf(bp, "%ld (%s)\n", x->verify_result, |
155 | 511 | X509_verify_cert_error_string(x->verify_result)) |
156 | 511 | <= 0) |
157 | 0 | goto err; |
158 | | |
159 | 511 | if (BIO_printf(bp, " Extended master secret: %s\n", |
160 | 511 | x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") |
161 | 511 | <= 0) |
162 | 0 | goto err; |
163 | | |
164 | 511 | if (isversion13) { |
165 | 24 | if (BIO_printf(bp, " Max Early Data: %u\n", |
166 | 24 | (unsigned int)x->ext.max_early_data) |
167 | 24 | <= 0) |
168 | 0 | goto err; |
169 | 24 | } |
170 | | |
171 | 511 | return 1; |
172 | 8 | err: |
173 | 8 | return 0; |
174 | 511 | } |
175 | | |
176 | | /* |
177 | | * print session id and master key in NSS keylog format (RSA |
178 | | * Session-ID:<session id> Master-Key:<master key>) |
179 | | */ |
180 | | int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x) |
181 | 0 | { |
182 | 0 | size_t i; |
183 | |
|
184 | 0 | if (x == NULL) |
185 | 0 | goto err; |
186 | 0 | if (x->session_id_length == 0 || x->master_key_length == 0) |
187 | 0 | goto err; |
188 | | |
189 | | /* |
190 | | * the RSA prefix is required by the format's definition although there's |
191 | | * nothing RSA-specific in the output, therefore, we don't have to check if |
192 | | * the cipher suite is based on RSA |
193 | | */ |
194 | 0 | if (BIO_puts(bp, "RSA ") <= 0) |
195 | 0 | goto err; |
196 | | |
197 | 0 | if (BIO_puts(bp, "Session-ID:") <= 0) |
198 | 0 | goto err; |
199 | 0 | for (i = 0; i < x->session_id_length; i++) { |
200 | 0 | if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0) |
201 | 0 | goto err; |
202 | 0 | } |
203 | 0 | if (BIO_puts(bp, " Master-Key:") <= 0) |
204 | 0 | goto err; |
205 | 0 | for (i = 0; i < x->master_key_length; i++) { |
206 | 0 | if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0) |
207 | 0 | goto err; |
208 | 0 | } |
209 | 0 | if (BIO_puts(bp, "\n") <= 0) |
210 | 0 | goto err; |
211 | | |
212 | 0 | return 1; |
213 | 0 | err: |
214 | 0 | return 0; |
215 | 0 | } |