Coverage Report

Created: 2023-09-25 06:42

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