Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
268
{
36
268
    size_t i;
37
268
    const char *s;
38
268
    int isversion13;
39
40
268
    if (x == NULL)
41
0
        goto err;
42
268
    isversion13 = (x->ssl_version == TLS1_3_VERSION)
43
253
        || (x->ssl_version == DTLS1_3_VERSION);
44
268
    if (BIO_puts(bp, "SSL-Session:\n") <= 0)
45
0
        goto err;
46
268
    s = ssl_protocol_to_string(x->ssl_version);
47
268
    if (BIO_printf(bp, "    Protocol  : %s\n", s) <= 0)
48
0
        goto err;
49
50
268
    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
268
    } else {
63
268
        if (BIO_printf(bp, "    Cipher    : %s\n",
64
268
                ((x->cipher->name == NULL) ? "unknown"
65
268
                                           : x->cipher->name))
66
268
            <= 0)
67
0
            goto err;
68
268
    }
69
268
    if (BIO_puts(bp, "    Session-ID: ") <= 0)
70
0
        goto err;
71
376
    for (i = 0; i < x->session_id_length; i++) {
72
108
        if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
73
0
            goto err;
74
108
    }
75
268
    if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
76
0
        goto err;
77
319
    for (i = 0; i < x->sid_ctx_length; i++) {
78
51
        if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
79
0
            goto err;
80
51
    }
81
268
    if (isversion13) {
82
15
        if (BIO_puts(bp, "\n    Resumption PSK: ") <= 0)
83
0
            goto err;
84
253
    } else if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
85
0
        goto err;
86
1.50k
    for (i = 0; i < x->master_key_length; i++) {
87
1.23k
        if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
88
0
            goto err;
89
1.23k
    }
90
268
#ifndef OPENSSL_NO_PSK
91
268
    if (BIO_puts(bp, "\n    PSK identity: ") <= 0)
92
0
        goto err;
93
268
    if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
94
1
        goto err;
95
267
    if (BIO_puts(bp, "\n    PSK identity hint: ") <= 0)
96
0
        goto err;
97
267
    if (BIO_printf(bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
98
1
        goto err;
99
266
#endif
100
266
#ifndef OPENSSL_NO_SRP
101
266
    if (BIO_puts(bp, "\n    SRP username: ") <= 0)
102
0
        goto err;
103
266
    if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
104
2
        goto err;
105
264
#endif
106
264
    if (x->ext.tick_lifetime_hint) {
107
90
        if (BIO_printf(bp,
108
90
                "\n    TLS session ticket lifetime hint: %lu (seconds)",
109
90
                x->ext.tick_lifetime_hint)
110
90
            <= 0)
111
0
            goto err;
112
90
    }
113
264
    if (x->ext.tick) {
114
1
        if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
115
0
            goto err;
116
1
        if (BIO_dump_indent(bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
117
1
            <= 0)
118
0
            goto err;
119
1
    }
120
264
#ifndef OPENSSL_NO_COMP
121
264
    if (x->compress_meth != 0) {
122
1
        SSL_COMP *comp = NULL;
123
124
1
        if (!ssl_cipher_get_evp(NULL, x, NULL, NULL, NULL, NULL, NULL, &comp, 0))
125
0
            goto err;
126
1
        if (comp == NULL) {
127
1
            if (BIO_printf(bp, "\n    Compression: %u", x->compress_meth) <= 0)
128
0
                goto err;
129
1
        } 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
1
    }
136
264
#endif
137
264
    if (!ossl_time_is_zero(x->time)) {
138
262
        if (BIO_printf(bp, "\n    Start Time: %lld",
139
262
                (long long)ossl_time_to_time_t(x->time))
140
262
            <= 0)
141
0
            goto err;
142
262
    }
143
264
    if (!ossl_time_is_zero(x->timeout)) {
144
263
        if (BIO_printf(bp, "\n    Timeout   : %lld (sec)",
145
263
                (long long)ossl_time2seconds(x->timeout))
146
263
            <= 0)
147
0
            goto err;
148
263
    }
149
264
    if (BIO_puts(bp, "\n") <= 0)
150
0
        goto err;
151
152
264
    if (BIO_puts(bp, "    Verify return code: ") <= 0)
153
0
        goto err;
154
264
    if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
155
264
            X509_verify_cert_error_string(x->verify_result))
156
264
        <= 0)
157
0
        goto err;
158
159
264
    if (BIO_printf(bp, "    Extended master secret: %s\n",
160
264
            x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no")
161
264
        <= 0)
162
0
        goto err;
163
164
264
    if (isversion13) {
165
15
        if (BIO_printf(bp, "    Max Early Data: %u\n",
166
15
                (unsigned int)x->ext.max_early_data)
167
15
            <= 0)
168
0
            goto err;
169
15
    }
170
171
264
    return 1;
172
4
err:
173
4
    return 0;
174
264
}
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
}