Coverage Report

Created: 2025-08-11 07:04

/src/openssl34/ssl/ssl_txt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2024 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
1.54k
{
36
1.54k
    size_t i;
37
1.54k
    const char *s;
38
1.54k
    int istls13;
39
40
1.54k
    if (x == NULL)
41
0
        goto err;
42
1.54k
    istls13 = (x->ssl_version == TLS1_3_VERSION);
43
1.54k
    if (BIO_puts(bp, "SSL-Session:\n") <= 0)
44
0
        goto err;
45
1.54k
    s = ssl_protocol_to_string(x->ssl_version);
46
1.54k
    if (BIO_printf(bp, "    Protocol  : %s\n", s) <= 0)
47
0
        goto err;
48
49
1.54k
    if (x->cipher == NULL) {
50
0
        if (((x->cipher_id) & 0xff000000) == 0x02000000) {
51
0
            if (BIO_printf(bp, "    Cipher    : %06lX\n",
52
0
                           x->cipher_id & 0xffffff) <= 0)
53
0
                goto err;
54
0
        } else {
55
0
            if (BIO_printf(bp, "    Cipher    : %04lX\n",
56
0
                           x->cipher_id & 0xffff) <= 0)
57
0
                goto err;
58
0
        }
59
1.54k
    } else {
60
1.54k
        if (BIO_printf(bp, "    Cipher    : %s\n",
61
1.54k
                       ((x->cipher->name == NULL) ? "unknown"
62
1.54k
                                                  : x->cipher->name)) <= 0)
63
0
            goto err;
64
1.54k
    }
65
1.54k
    if (BIO_puts(bp, "    Session-ID: ") <= 0)
66
0
        goto err;
67
2.70k
    for (i = 0; i < x->session_id_length; i++) {
68
1.16k
        if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
69
0
            goto err;
70
1.16k
    }
71
1.54k
    if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
72
0
        goto err;
73
1.91k
    for (i = 0; i < x->sid_ctx_length; i++) {
74
375
        if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
75
0
            goto err;
76
375
    }
77
1.54k
    if (istls13) {
78
100
        if (BIO_puts(bp, "\n    Resumption PSK: ") <= 0)
79
0
            goto err;
80
1.44k
    } else if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
81
0
        goto err;
82
13.7k
    for (i = 0; i < x->master_key_length; i++) {
83
12.2k
        if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
84
0
            goto err;
85
12.2k
    }
86
1.54k
#ifndef OPENSSL_NO_PSK
87
1.54k
    if (BIO_puts(bp, "\n    PSK identity: ") <= 0)
88
0
        goto err;
89
1.54k
    if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
90
6
        goto err;
91
1.53k
    if (BIO_puts(bp, "\n    PSK identity hint: ") <= 0)
92
0
        goto err;
93
1.53k
    if (BIO_printf
94
1.53k
        (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
95
5
        goto err;
96
1.53k
#endif
97
1.53k
#ifndef OPENSSL_NO_SRP
98
1.53k
    if (BIO_puts(bp, "\n    SRP username: ") <= 0)
99
0
        goto err;
100
1.53k
    if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
101
6
        goto err;
102
1.52k
#endif
103
1.52k
    if (x->ext.tick_lifetime_hint) {
104
382
        if (BIO_printf(bp,
105
382
                       "\n    TLS session ticket lifetime hint: %ld (seconds)",
106
382
                       x->ext.tick_lifetime_hint) <= 0)
107
0
            goto err;
108
382
    }
109
1.52k
    if (x->ext.tick) {
110
12
        if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
111
0
            goto err;
112
12
        if (BIO_dump_indent
113
12
            (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
114
12
            <= 0)
115
6
            goto err;
116
12
    }
117
1.51k
#ifndef OPENSSL_NO_COMP
118
1.51k
    if (x->compress_meth != 0) {
119
5
        SSL_COMP *comp = NULL;
120
121
5
        if (!ssl_cipher_get_evp(NULL, x, NULL, NULL, NULL, NULL, &comp, 0))
122
0
            goto err;
123
5
        if (comp == NULL) {
124
5
            if (BIO_printf(bp, "\n    Compression: %d", x->compress_meth) <= 0)
125
0
                goto err;
126
5
        } else {
127
0
            if (BIO_printf(bp, "\n    Compression: %d (%s)", comp->id,
128
0
                           comp->name) <= 0)
129
0
                goto err;
130
0
        }
131
5
    }
132
1.51k
#endif
133
1.51k
    if (!ossl_time_is_zero(x->time)) {
134
1.51k
        if (BIO_printf(bp, "\n    Start Time: %lld",
135
1.51k
                       (long long)ossl_time_to_time_t(x->time)) <= 0)
136
0
            goto err;
137
1.51k
    }
138
1.51k
    if (!ossl_time_is_zero(x->timeout)) {
139
1.51k
        if (BIO_printf(bp, "\n    Timeout   : %lld (sec)",
140
1.51k
                       (long long)ossl_time2seconds(x->timeout)) <= 0)
141
0
            goto err;
142
1.51k
    }
143
1.51k
    if (BIO_puts(bp, "\n") <= 0)
144
0
        goto err;
145
146
1.51k
    if (BIO_puts(bp, "    Verify return code: ") <= 0)
147
0
        goto err;
148
1.51k
    if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
149
1.51k
                   X509_verify_cert_error_string(x->verify_result)) <= 0)
150
0
        goto err;
151
152
1.51k
    if (BIO_printf(bp, "    Extended master secret: %s\n",
153
1.51k
                   x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
154
0
        goto err;
155
156
1.51k
    if (istls13) {
157
100
        if (BIO_printf(bp, "    Max Early Data: %u\n",
158
100
                       (unsigned int)x->ext.max_early_data) <= 0)
159
0
            goto err;
160
100
    }
161
162
1.51k
    return 1;
163
23
 err:
164
23
    return 0;
165
1.51k
}
166
167
/*
168
 * print session id and master key in NSS keylog format (RSA
169
 * Session-ID:<session id> Master-Key:<master key>)
170
 */
171
int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
172
0
{
173
0
    size_t i;
174
175
0
    if (x == NULL)
176
0
        goto err;
177
0
    if (x->session_id_length == 0 || x->master_key_length == 0)
178
0
        goto err;
179
180
    /*
181
     * the RSA prefix is required by the format's definition although there's
182
     * nothing RSA-specific in the output, therefore, we don't have to check if
183
     * the cipher suite is based on RSA
184
     */
185
0
    if (BIO_puts(bp, "RSA ") <= 0)
186
0
        goto err;
187
188
0
    if (BIO_puts(bp, "Session-ID:") <= 0)
189
0
        goto err;
190
0
    for (i = 0; i < x->session_id_length; i++) {
191
0
        if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
192
0
            goto err;
193
0
    }
194
0
    if (BIO_puts(bp, " Master-Key:") <= 0)
195
0
        goto err;
196
0
    for (i = 0; i < x->master_key_length; i++) {
197
0
        if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
198
0
            goto err;
199
0
    }
200
0
    if (BIO_puts(bp, "\n") <= 0)
201
0
        goto err;
202
203
0
    return 1;
204
0
 err:
205
0
    return 0;
206
0
}