Coverage Report

Created: 2025-06-13 06:57

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