Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/ssl/ssl_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2021 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 <stdlib.h>
13
#include "ssl_local.h"
14
#include <openssl/asn1t.h>
15
#include <openssl/x509.h>
16
17
typedef struct {
18
    uint32_t version;
19
    int32_t ssl_version;
20
    ASN1_OCTET_STRING *cipher;
21
    ASN1_OCTET_STRING *comp_id;
22
    ASN1_OCTET_STRING *master_key;
23
    ASN1_OCTET_STRING *session_id;
24
    ASN1_OCTET_STRING *key_arg;
25
    int64_t time;
26
    int64_t timeout;
27
    X509 *peer;
28
    ASN1_OCTET_STRING *session_id_context;
29
    int32_t verify_result;
30
    ASN1_OCTET_STRING *tlsext_hostname;
31
    uint64_t tlsext_tick_lifetime_hint;
32
    uint32_t tlsext_tick_age_add;
33
    ASN1_OCTET_STRING *tlsext_tick;
34
#ifndef OPENSSL_NO_PSK
35
    ASN1_OCTET_STRING *psk_identity_hint;
36
    ASN1_OCTET_STRING *psk_identity;
37
#endif
38
#ifndef OPENSSL_NO_SRP
39
    ASN1_OCTET_STRING *srp_username;
40
#endif
41
    uint64_t flags;
42
    uint32_t max_early_data;
43
    ASN1_OCTET_STRING *alpn_selected;
44
    uint32_t tlsext_max_fragment_len_mode;
45
    ASN1_OCTET_STRING *ticket_appdata;
46
    uint32_t kex_group;
47
} SSL_SESSION_ASN1;
48
49
ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
50
    ASN1_EMBED(SSL_SESSION_ASN1, version, UINT32),
51
    ASN1_EMBED(SSL_SESSION_ASN1, ssl_version, INT32),
52
    ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
53
    ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
54
    ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
55
    ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
56
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, time, ZINT64, 1),
57
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, timeout, ZINT64, 2),
58
    ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
59
    ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
60
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, verify_result, ZINT32, 5),
61
    ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
62
#ifndef OPENSSL_NO_PSK
63
    ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
64
    ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
65
#endif
66
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZUINT64, 9),
67
    ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
68
    ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
69
#ifndef OPENSSL_NO_SRP
70
    ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
71
#endif
72
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, flags, ZUINT64, 13),
73
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_tick_age_add, ZUINT32, 14),
74
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, max_early_data, ZUINT32, 15),
75
    ASN1_EXP_OPT(SSL_SESSION_ASN1, alpn_selected, ASN1_OCTET_STRING, 16),
76
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_max_fragment_len_mode, ZUINT32, 17),
77
    ASN1_EXP_OPT(SSL_SESSION_ASN1, ticket_appdata, ASN1_OCTET_STRING, 18),
78
    ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, kex_group, UINT32, 19)
79
149k
} static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
80
149k
81
149k
    IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
82
149k
83
149k
/* Utility functions for i2d_SSL_SESSION */
84
149k
85
149k
/* Initialise OCTET STRING from buffer and length */
86
149k
87
149k
static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
88
149k
    const unsigned char *data, size_t len)
89
149k
{
90
9.80k
    os->data = (unsigned char *)data; /* justified cast: data is not modified */
91
9.80k
    os->length = (int)len;
92
9.80k
    os->flags = 0;
93
9.80k
    *dest = os;
94
9.80k
}
95
96
/* Initialise OCTET STRING from string */
97
static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
98
    const char *data)
99
9.46k
{
100
9.46k
    if (data != NULL)
101
297
        ssl_session_oinit(dest, os, (const unsigned char *)data, strlen(data));
102
9.16k
    else
103
9.16k
        *dest = NULL;
104
9.46k
}
105
106
int i2d_SSL_SESSION(const SSL_SESSION *in, unsigned char **pp)
107
397
{
108
109
397
    SSL_SESSION_ASN1 as;
110
111
397
    ASN1_OCTET_STRING cipher;
112
397
    unsigned char cipher_data[2];
113
397
    ASN1_OCTET_STRING master_key, session_id, sid_ctx;
114
115
397
#ifndef OPENSSL_NO_COMP
116
397
    ASN1_OCTET_STRING comp_id;
117
397
    unsigned char comp_id_data;
118
397
#endif
119
397
    ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
120
397
#ifndef OPENSSL_NO_SRP
121
397
    ASN1_OCTET_STRING srp_username;
122
397
#endif
123
397
#ifndef OPENSSL_NO_PSK
124
397
    ASN1_OCTET_STRING psk_identity, psk_identity_hint;
125
397
#endif
126
397
    ASN1_OCTET_STRING alpn_selected;
127
397
    ASN1_OCTET_STRING ticket_appdata;
128
129
397
    long l;
130
131
397
    if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
132
0
        return 0;
133
134
397
    memset(&as, 0, sizeof(as));
135
136
397
    as.version = SSL_SESSION_ASN1_VERSION;
137
397
    as.ssl_version = in->ssl_version;
138
139
397
    as.kex_group = in->kex_group;
140
141
397
    if (in->cipher == NULL)
142
0
        l = in->cipher_id;
143
397
    else
144
397
        l = in->cipher->id;
145
397
    cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
146
397
    cipher_data[1] = ((unsigned char)(l)) & 0xff;
147
148
397
    ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
149
150
397
#ifndef OPENSSL_NO_COMP
151
397
    if (in->compress_meth) {
152
1
        comp_id_data = (unsigned char)in->compress_meth;
153
1
        ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
154
1
    }
155
397
#endif
156
157
397
    ssl_session_oinit(&as.master_key, &master_key,
158
397
        in->master_key, in->master_key_length);
159
160
397
    ssl_session_oinit(&as.session_id, &session_id,
161
397
        in->session_id, in->session_id_length);
162
163
397
    ssl_session_oinit(&as.session_id_context, &sid_ctx,
164
397
        in->sid_ctx, in->sid_ctx_length);
165
166
397
    as.time = (int64_t)in->time;
167
397
    as.timeout = (int64_t)in->timeout;
168
397
    as.verify_result = in->verify_result;
169
170
397
    as.peer = in->peer;
171
172
397
    ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
173
397
        in->ext.hostname);
174
397
    if (in->ext.tick) {
175
2
        ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
176
2
            in->ext.tick, in->ext.ticklen);
177
2
    }
178
397
    if (in->ext.tick_lifetime_hint > 0)
179
120
        as.tlsext_tick_lifetime_hint = in->ext.tick_lifetime_hint;
180
397
    as.tlsext_tick_age_add = in->ext.tick_age_add;
181
397
#ifndef OPENSSL_NO_PSK
182
397
    ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
183
397
        in->psk_identity_hint);
184
397
    ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
185
397
#endif /* OPENSSL_NO_PSK */
186
397
#ifndef OPENSSL_NO_SRP
187
397
    ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
188
397
#endif /* OPENSSL_NO_SRP */
189
190
397
    as.flags = in->flags;
191
397
    as.max_early_data = in->ext.max_early_data;
192
193
397
    if (in->ext.alpn_selected == NULL)
194
396
        as.alpn_selected = NULL;
195
1
    else
196
1
        ssl_session_oinit(&as.alpn_selected, &alpn_selected,
197
1
            in->ext.alpn_selected, in->ext.alpn_selected_len);
198
199
397
    as.tlsext_max_fragment_len_mode = in->ext.max_fragment_len_mode;
200
201
397
    if (in->ticket_appdata == NULL)
202
396
        as.ticket_appdata = NULL;
203
1
    else
204
1
        ssl_session_oinit(&as.ticket_appdata, &ticket_appdata,
205
1
            in->ticket_appdata, in->ticket_appdata_len);
206
207
397
    return i2d_SSL_SESSION_ASN1(&as, pp);
208
397
}
209
210
/* Utility functions for d2i_SSL_SESSION */
211
212
/* OPENSSL_strndup an OCTET STRING */
213
214
static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
215
11.7k
{
216
11.7k
    OPENSSL_free(*pdst);
217
11.7k
    *pdst = NULL;
218
11.7k
    if (src == NULL)
219
11.0k
        return 1;
220
706
    *pdst = OPENSSL_strndup((char *)src->data, src->length);
221
706
    if (*pdst == NULL)
222
0
        return 0;
223
706
    return 1;
224
706
}
225
226
/* Copy an OCTET STRING, return error if it exceeds maximum length */
227
228
static int ssl_session_memcpy(unsigned char *dst, size_t *pdstlen,
229
    ASN1_OCTET_STRING *src, size_t maxlen)
230
8.97k
{
231
8.97k
    if (src == NULL || src->length == 0) {
232
6.51k
        *pdstlen = 0;
233
6.51k
        return 1;
234
6.51k
    }
235
2.45k
    if (src->length < 0 || src->length > (int)maxlen)
236
68
        return 0;
237
2.38k
    memcpy(dst, src->data, src->length);
238
2.38k
    *pdstlen = src->length;
239
2.38k
    return 1;
240
2.45k
}
241
242
SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
243
    long length)
244
13.1k
{
245
13.1k
    long id;
246
13.1k
    size_t tmpl;
247
13.1k
    const unsigned char *p = *pp;
248
13.1k
    SSL_SESSION_ASN1 *as = NULL;
249
13.1k
    SSL_SESSION *ret = NULL;
250
251
13.1k
    as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
252
    /* ASN.1 code returns suitable error */
253
13.1k
    if (as == NULL)
254
12.5k
        goto err;
255
256
571
    if (a == NULL || *a == NULL) {
257
571
        ret = SSL_SESSION_new();
258
571
        if (ret == NULL)
259
0
            goto err;
260
571
    } else {
261
0
        ret = *a;
262
0
    }
263
264
571
    if (as->version != SSL_SESSION_ASN1_VERSION) {
265
3
        ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_SSL_VERSION);
266
3
        goto err;
267
3
    }
268
269
568
    if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
270
90
        && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
271
59
        && as->ssl_version != DTLS1_BAD_VER) {
272
55
        ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION);
273
55
        goto err;
274
55
    }
275
276
513
    ret->ssl_version = (int)as->ssl_version;
277
278
513
    ret->kex_group = as->kex_group;
279
280
513
    if (as->cipher->length != 2) {
281
1
        ERR_raise(ERR_LIB_SSL, SSL_R_CIPHER_CODE_WRONG_LENGTH);
282
1
        goto err;
283
1
    }
284
285
512
    id = 0x03000000L | ((unsigned long)as->cipher->data[0] << 8L)
286
512
        | (unsigned long)as->cipher->data[1];
287
288
512
    ret->cipher_id = id;
289
512
    ret->cipher = ssl3_get_cipher_by_id(id);
290
512
    if (ret->cipher == NULL)
291
9
        goto err;
292
293
503
    if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
294
503
            as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
295
2
        goto err;
296
297
501
    if (!ssl_session_memcpy(ret->master_key, &tmpl,
298
501
            as->master_key, TLS13_MAX_RESUMPTION_PSK_LENGTH))
299
9
        goto err;
300
301
492
    ret->master_key_length = tmpl;
302
303
492
    if (as->time != 0)
304
196
        ret->time = (time_t)as->time;
305
296
    else
306
296
        ret->time = time(NULL);
307
308
492
    if (as->timeout != 0)
309
179
        ret->timeout = (time_t)as->timeout;
310
313
    else
311
313
        ret->timeout = 3;
312
492
    ssl_session_calculate_timeout(ret);
313
314
492
    X509_free(ret->peer);
315
492
    ret->peer = as->peer;
316
492
    as->peer = NULL;
317
318
492
    if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
319
492
            as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
320
1
        goto err;
321
322
    /* NB: this defaults to zero which is X509_V_OK */
323
491
    ret->verify_result = as->verify_result;
324
325
491
    if (!ssl_session_strndup(&ret->ext.hostname, as->tlsext_hostname))
326
0
        goto err;
327
328
491
#ifndef OPENSSL_NO_PSK
329
491
    if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
330
0
        goto err;
331
491
    if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
332
0
        goto err;
333
491
#endif
334
335
491
    ret->ext.tick_lifetime_hint = (unsigned long)as->tlsext_tick_lifetime_hint;
336
491
    ret->ext.tick_age_add = as->tlsext_tick_age_add;
337
491
    OPENSSL_free(ret->ext.tick);
338
491
    if (as->tlsext_tick != NULL) {
339
4
        ret->ext.tick = as->tlsext_tick->data;
340
4
        ret->ext.ticklen = as->tlsext_tick->length;
341
4
        as->tlsext_tick->data = NULL;
342
487
    } else {
343
487
        ret->ext.tick = NULL;
344
487
    }
345
491
#ifndef OPENSSL_NO_COMP
346
491
    if (as->comp_id) {
347
3
        if (as->comp_id->length != 1) {
348
2
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
349
2
            goto err;
350
2
        }
351
1
        ret->compress_meth = as->comp_id->data[0];
352
488
    } else {
353
488
        ret->compress_meth = 0;
354
488
    }
355
489
#endif
356
357
489
#ifndef OPENSSL_NO_SRP
358
489
    if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
359
0
        goto err;
360
489
#endif /* OPENSSL_NO_SRP */
361
    /* Flags defaults to zero which is fine */
362
489
    ret->flags = (int32_t)as->flags;
363
489
    ret->ext.max_early_data = as->max_early_data;
364
365
489
    OPENSSL_free(ret->ext.alpn_selected);
366
489
    if (as->alpn_selected != NULL) {
367
2
        ret->ext.alpn_selected = as->alpn_selected->data;
368
2
        ret->ext.alpn_selected_len = as->alpn_selected->length;
369
2
        as->alpn_selected->data = NULL;
370
487
    } else {
371
487
        ret->ext.alpn_selected = NULL;
372
487
        ret->ext.alpn_selected_len = 0;
373
487
    }
374
375
489
    ret->ext.max_fragment_len_mode = as->tlsext_max_fragment_len_mode;
376
377
489
    OPENSSL_free(ret->ticket_appdata);
378
489
    if (as->ticket_appdata != NULL) {
379
1
        ret->ticket_appdata = as->ticket_appdata->data;
380
1
        ret->ticket_appdata_len = as->ticket_appdata->length;
381
1
        as->ticket_appdata->data = NULL;
382
488
    } else {
383
488
        ret->ticket_appdata = NULL;
384
488
        ret->ticket_appdata_len = 0;
385
488
    }
386
387
489
    M_ASN1_free_of(as, SSL_SESSION_ASN1);
388
389
489
    if ((a != NULL) && (*a == NULL))
390
0
        *a = ret;
391
489
    *pp = p;
392
489
    return ret;
393
394
12.6k
err:
395
12.6k
    M_ASN1_free_of(as, SSL_SESSION_ASN1);
396
12.6k
    if ((a == NULL) || (*a != ret))
397
12.6k
        SSL_SESSION_free(ret);
398
    return NULL;
399
489
}