Coverage Report

Created: 2025-12-31 06:58

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