Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/ech/ech_store.c
Line
Count
Source
1
/*
2
 * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/ssl.h>
11
#include <openssl/ech.h>
12
#include "../ssl_local.h"
13
#include "ech_local.h"
14
#include <openssl/rand.h>
15
#include <openssl/evp.h>
16
#include <openssl/core_names.h>
17
18
/* a size for some crypto vars */
19
0
#define OSSL_ECH_CRYPTO_VAR_SIZE 2048
20
21
/*
22
 * Used for ech_bio2buf, when reading from a BIO we allocate in chunks sized
23
 * as per below, with a max number of chunks as indicated, we don't expect to
24
 * go beyond one chunk in almost all cases
25
 */
26
0
#define OSSL_ECH_BUFCHUNK 512
27
0
#define OSSL_ECH_MAXITER 32
28
29
/*
30
 * ECHConfigList input to OSSL_ECHSTORE_read_echconfiglist()
31
 * can be either binary encoded ECHConfigList or a base64
32
 * encoded ECHConfigList.
33
 */
34
0
#define OSSL_ECH_FMT_BIN 1 /* binary ECHConfigList */
35
0
#define OSSL_ECH_FMT_B64TXT 2 /* base64 ECHConfigList */
36
37
/*
38
 * Telltales we use when guessing which form of encoded input we've
39
 * been given for an RR value or ECHConfig.
40
 * We give these the EBCDIC treatment as well - why not? :-)
41
 */
42
static const char B64_alphabet[] = "\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52"
43
                                   "\x53\x54\x55\x56\x57\x58\x59\x5a\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a"
44
                                   "\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x30\x31"
45
                                   "\x32\x33\x34\x35\x36\x37\x38\x39\x2b\x2f\x3d\x3b";
46
47
#ifndef TLSEXT_MINLEN_host_name
48
/* The shortest DNS name we allow, e.g. "a.bc" */
49
0
#define TLSEXT_MINLEN_host_name 4
50
#endif
51
52
/*
53
 * local functions - public APIs are at the end
54
 */
55
56
void ossl_echext_free(OSSL_ECHEXT *e)
57
0
{
58
0
    if (e == NULL)
59
0
        return;
60
0
    OPENSSL_free(e->val);
61
0
    OPENSSL_free(e);
62
0
    return;
63
0
}
64
65
OSSL_ECHEXT *ossl_echext_dup(const OSSL_ECHEXT *src)
66
0
{
67
0
    OSSL_ECHEXT *ext = OPENSSL_zalloc(sizeof(*src));
68
69
0
    if (ext == NULL)
70
0
        return NULL;
71
0
    *ext = *src;
72
0
    ext->val = NULL;
73
0
    if (ext->len != 0) {
74
0
        ext->val = OPENSSL_memdup(src->val, src->len);
75
0
        if (ext->val == NULL) {
76
0
            ossl_echext_free(ext);
77
0
            return NULL;
78
0
        }
79
0
    }
80
0
    return ext;
81
0
}
82
83
void ossl_echstore_entry_free(OSSL_ECHSTORE_ENTRY *ee)
84
0
{
85
0
    if (ee == NULL)
86
0
        return;
87
0
    OPENSSL_free(ee->public_name);
88
0
    OPENSSL_free(ee->pub);
89
0
    EVP_PKEY_free(ee->keyshare);
90
0
    OPENSSL_free(ee->encoded);
91
0
    OPENSSL_free(ee->suites);
92
0
    sk_OSSL_ECHEXT_pop_free(ee->exts, ossl_echext_free);
93
0
    OPENSSL_free(ee);
94
0
    return;
95
0
}
96
97
/*
98
 * @brief Read a buffer from an input 'till eof
99
 * @param in is the BIO input
100
 * @param buf is where to put the buffer, allocated inside here
101
 * @param len is the length of that buffer
102
 *
103
 * This is intended for small inputs, either files or buffers and
104
 * not other kinds of BIO.
105
 */
106
static int ech_bio2buf(BIO *in, unsigned char **buf, size_t *len)
107
0
{
108
0
    unsigned char *lptr = NULL, *lbuf = NULL, *tmp = NULL;
109
0
    size_t sofar = 0, readbytes = 0;
110
0
    int done = 0, brv, iter = 0;
111
112
0
    if (buf == NULL || len == NULL)
113
0
        return 0;
114
0
    sofar = OSSL_ECH_BUFCHUNK;
115
0
    lbuf = OPENSSL_zalloc(sofar);
116
0
    if (lbuf == NULL)
117
0
        return 0;
118
0
    lptr = lbuf;
119
0
    while (!BIO_eof(in) && !done && iter++ < OSSL_ECH_MAXITER) {
120
0
        brv = BIO_read_ex(in, lptr, OSSL_ECH_BUFCHUNK, &readbytes);
121
0
        if (brv != 1)
122
0
            goto err;
123
0
        if (BIO_eof(in) || readbytes < OSSL_ECH_BUFCHUNK) {
124
0
            done = 1;
125
0
            break;
126
0
        }
127
0
        sofar += OSSL_ECH_BUFCHUNK;
128
0
        tmp = OPENSSL_realloc(lbuf, sofar);
129
0
        if (tmp == NULL)
130
0
            goto err;
131
0
        lbuf = tmp;
132
0
        lptr = lbuf + sofar - OSSL_ECH_BUFCHUNK;
133
0
    }
134
0
    if (BIO_eof(in) && done == 1) {
135
0
        *len = sofar + readbytes - OSSL_ECH_BUFCHUNK;
136
0
        *buf = lbuf;
137
0
        return 1;
138
0
    }
139
0
err:
140
0
    OPENSSL_free(lbuf);
141
0
    return 0;
142
0
}
143
144
/*
145
 * @brief Figure out ECHConfig encoding
146
 * @param val is a buffer with the encoding
147
 * @param len is the length of that buffer
148
 * @param fmt is the detected format
149
 * @return 1 for success, 0 for error
150
 */
151
static int ech_check_format(const unsigned char *val, size_t len, int *fmt)
152
0
{
153
0
    size_t span = 0;
154
0
    char *copy_with_NUL = NULL;
155
156
0
    if (fmt == NULL || len <= 4 || val == NULL)
157
0
        return 0;
158
    /* binary encoding starts with two octet length and ECH version */
159
0
    if (len == 2 + ((size_t)(val[0]) * 256 + (size_t)(val[1]))
160
0
        && val[2] == ((OSSL_ECH_RFC9849_VERSION / 256) & 0xff)
161
0
        && val[3] == ((OSSL_ECH_RFC9849_VERSION % 256) & 0xff)) {
162
0
        *fmt = OSSL_ECH_FMT_BIN;
163
0
        return 1;
164
0
    }
165
    /* ensure we always end with a NUL so strspn is safe */
166
0
    copy_with_NUL = OPENSSL_malloc(len + 1);
167
0
    if (copy_with_NUL == NULL)
168
0
        return 0;
169
0
    memcpy(copy_with_NUL, val, len);
170
0
    copy_with_NUL[len] = '\0';
171
0
    span = strspn(copy_with_NUL, B64_alphabet);
172
0
    OPENSSL_free(copy_with_NUL);
173
0
    if (len <= span) {
174
0
        *fmt = OSSL_ECH_FMT_B64TXT;
175
0
        return 1;
176
0
    }
177
0
    return 0;
178
0
}
179
180
/*
181
 * @brief helper to decode ECHConfig extensions
182
 * @param ee is the OSSL_ECHSTORE entry for these
183
 * @param exts is the binary form extensions
184
 * @return 1 for good, 0 for error
185
 */
186
static int ech_decode_echconfig_exts(OSSL_ECHSTORE_ENTRY *ee, PACKET *exts)
187
0
{
188
0
    unsigned int exttype = 0;
189
0
    size_t extlen = 0;
190
0
    unsigned char *extval = NULL;
191
0
    OSSL_ECHEXT *oe = NULL;
192
0
    PACKET ext;
193
194
    /*
195
     * reminder: exts is a two-octet length prefixed list of:
196
     * - two octet extension type
197
     * - two octet extension length (can be zero)
198
     * - length octets
199
     * we've consumed the overall length before getting here
200
     */
201
0
    while (PACKET_remaining(exts) > 0) {
202
0
        exttype = 0, extlen = 0;
203
0
        extval = NULL;
204
0
        oe = NULL;
205
0
        if (!PACKET_get_net_2(exts, &exttype) || !PACKET_get_length_prefixed_2(exts, &ext)) {
206
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_ECHCONFIG_EXTENSION);
207
0
            goto err;
208
0
        }
209
0
        if (PACKET_remaining(&ext) >= OSSL_ECH_MAX_ECHCONFIGEXT_LEN) {
210
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_ECHCONFIG_EXTENSION);
211
0
            goto err;
212
0
        }
213
0
        if (!PACKET_memdup(&ext, &extval, &extlen)) {
214
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_ECHCONFIG_EXTENSION);
215
0
            goto err;
216
0
        }
217
0
        oe = OPENSSL_malloc(sizeof(*oe));
218
0
        if (oe == NULL)
219
0
            goto err;
220
0
        oe->type = (uint16_t)exttype;
221
0
        oe->val = extval;
222
0
        extval = NULL; /* avoid double free */
223
0
        oe->len = (uint16_t)extlen;
224
0
        if (ee->exts == NULL)
225
0
            ee->exts = sk_OSSL_ECHEXT_new_null();
226
0
        if (ee->exts == NULL) {
227
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
228
0
            goto err;
229
0
        }
230
0
        if (!sk_OSSL_ECHEXT_push(ee->exts, oe)) {
231
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
232
0
            goto err;
233
0
        }
234
0
    }
235
0
    return 1;
236
0
err:
237
0
    sk_OSSL_ECHEXT_pop_free(ee->exts, ossl_echext_free);
238
0
    ee->exts = NULL;
239
0
    ossl_echext_free(oe);
240
0
    OPENSSL_free(extval);
241
0
    return 0;
242
0
}
243
244
/*
245
 * @brief Check entry to see if looks good or bad
246
 * @param ee is the ECHConfig to check
247
 * @return 1 for all good, 0 otherwise
248
 */
249
static int ech_final_config_checks(OSSL_ECHSTORE_ENTRY *ee)
250
0
{
251
0
    OSSL_HPKE_SUITE hpke_suite;
252
0
    int ind, num, rv = 0, goodsuitefound = 0;
253
0
    X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
254
0
    char *lastlabel = NULL;
255
0
    size_t lllen;
256
257
0
    if (vpm == NULL) {
258
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
259
0
        goto err;
260
0
    }
261
    /* check local support for some suite */
262
0
    for (ind = 0; ind != (int)ee->nsuites; ind++) {
263
        /*
264
         * suite_check says yes to the pseudo-aead for export, but we don't
265
         * want to see it here coming from outside in an encoding
266
         */
267
0
        hpke_suite = ee->suites[ind];
268
0
        if (OSSL_HPKE_suite_check(hpke_suite) == 1
269
0
            && hpke_suite.aead_id != OSSL_HPKE_AEAD_ID_EXPORTONLY) {
270
0
            goodsuitefound = 1;
271
0
            break;
272
0
        }
273
0
    }
274
0
    if (goodsuitefound == 0) {
275
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
276
0
        goto err;
277
0
    }
278
    /* check no mandatory exts (with high bit set in type) */
279
0
    num = (ee->exts == NULL ? 0 : sk_OSSL_ECHEXT_num(ee->exts));
280
0
    for (ind = 0; ind != num; ind++) {
281
0
        OSSL_ECHEXT *oe = sk_OSSL_ECHEXT_value(ee->exts, (int)ind);
282
283
0
        if (oe->type & 0x8000) {
284
0
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
285
0
            goto err;
286
0
        }
287
0
    }
288
    /* check public_name rules, as per spec section 6.1.7 */
289
0
    if (ee->public_name == NULL
290
0
        || ee->public_name[0] == '\0'
291
0
        || ee->public_name[0] == '.'
292
0
        || ee->public_name[strlen(ee->public_name) - 1] == '.'
293
0
        || strlen(ee->public_name) > 255)
294
0
        goto err;
295
    /*
296
     * Use X509_VERIFY_PARAM_add1_host to avoid coding same checks twice.
297
     * This checks max 63 octets per label, overall length and some other
298
     * DNS label checks.
299
     */
300
0
    if (X509_VERIFY_PARAM_add1_host(vpm, ee->public_name, 0) == 0)
301
0
        goto err;
302
    /*
303
     * but we still have to check the last label restrictions, which
304
     * are intended to avoid confusion with IP address literals in
305
     * encodings browsers support, as per WHATWG (convincing, eh:-)
306
     */
307
0
    lastlabel = strrchr(ee->public_name, '.');
308
0
    if (lastlabel == NULL) /* if there are no dots */
309
0
        lastlabel = ee->public_name;
310
0
    lllen = strlen(lastlabel);
311
0
    if (lllen < 2)
312
0
        goto err;
313
0
    if (lastlabel[0] == '.') {
314
0
        lastlabel++;
315
0
        lllen--;
316
0
    }
317
0
    if (strspn(lastlabel, "0123456789") == lllen)
318
0
        goto err;
319
0
    if (lastlabel[0] == '0' && lllen > 2
320
0
        && (lastlabel[1] == 'x' || lastlabel[1] == 'X')
321
0
        && strspn(lastlabel + 2, "0123456789abcdefABCDEF") == (lllen - 2))
322
0
        goto err;
323
0
    rv = 1;
324
0
err:
325
0
    X509_VERIFY_PARAM_free(vpm);
326
0
    return rv;
327
0
}
328
329
/**
330
 * @brief decode one ECHConfig from a packet into an entry
331
 * @param rent ptr to an entry allocated within (on success)
332
 * @param pkt is the encoding
333
 * @param priv is an optional private key (NULL if absent)
334
 * @param for_retry says whether to include in a retry_config (if priv present)
335
 * @return 1 for success, 0 for error
336
 */
337
static int ech_decode_one_entry(OSSL_ECHSTORE_ENTRY **rent, PACKET *pkt,
338
    EVP_PKEY *priv, int for_retry)
339
0
{
340
0
    size_t ech_content_length = 0;
341
0
    unsigned int tmpi;
342
0
    const unsigned char *tmpecp = NULL;
343
0
    size_t tmpeclen = 0, test_publen = 0;
344
0
    PACKET ver_pkt, pub_pkt, cipher_suites, public_name_pkt, exts;
345
0
    uint16_t thiskemid;
346
0
    size_t suiteoctets = 0;
347
0
    unsigned int ci = 0;
348
0
    unsigned char cipher[OSSL_ECH_CIPHER_LEN], max_name_len;
349
0
    unsigned char test_pub[OSSL_ECH_CRYPTO_VAR_SIZE];
350
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
351
352
0
    if (rent == NULL) {
353
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
354
0
        return 0;
355
0
    }
356
0
    if (pkt == NULL) {
357
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
358
0
        goto err;
359
0
    }
360
0
    ee = OPENSSL_zalloc(sizeof(*ee));
361
0
    if (ee == NULL)
362
0
        goto err;
363
    /* note start of encoding so we can make a copy later */
364
0
    tmpeclen = PACKET_remaining(pkt);
365
0
    if (PACKET_peek_bytes(pkt, &tmpecp, tmpeclen) != 1
366
0
        || !PACKET_get_net_2(pkt, &tmpi)) {
367
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
368
0
        goto err;
369
0
    }
370
0
    ee->version = (uint16_t)tmpi;
371
372
    /* grab versioned packet data */
373
0
    if (!PACKET_get_length_prefixed_2(pkt, &ver_pkt)) {
374
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
375
0
        goto err;
376
0
    }
377
0
    ech_content_length = (unsigned int)PACKET_remaining(&ver_pkt);
378
0
    switch (ee->version) {
379
0
    case OSSL_ECH_RFC9849_VERSION:
380
0
        break;
381
0
    default:
382
        /* skip over in case we get something we can handle later */
383
0
        if (!PACKET_forward(&ver_pkt, ech_content_length)) {
384
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
385
0
            goto err;
386
0
        }
387
        /* nothing to return but not a fail */
388
0
        ossl_echstore_entry_free(ee);
389
0
        *rent = NULL;
390
0
        return 1;
391
0
    }
392
0
    if (!PACKET_copy_bytes(&ver_pkt, &ee->config_id, 1)
393
0
        || !PACKET_get_net_2(&ver_pkt, &tmpi)
394
0
        || !PACKET_get_length_prefixed_2(&ver_pkt, &pub_pkt)
395
0
        || !PACKET_memdup(&pub_pkt, &ee->pub, &ee->pub_len)
396
0
        || !PACKET_get_length_prefixed_2(&ver_pkt, &cipher_suites)
397
0
        || (suiteoctets = PACKET_remaining(&cipher_suites)) <= 0
398
0
        || (suiteoctets % 2) == 1
399
0
        || suiteoctets / OSSL_ECH_CIPHER_LEN > UINT_MAX) {
400
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
401
0
        goto err;
402
0
    }
403
0
    thiskemid = (uint16_t)tmpi;
404
0
    ee->nsuites = (unsigned int)(suiteoctets / OSSL_ECH_CIPHER_LEN);
405
0
    ee->suites = OPENSSL_malloc_array(ee->nsuites, sizeof(*ee->suites));
406
0
    if (ee->suites == NULL)
407
0
        goto err;
408
0
    while (PACKET_copy_bytes(&cipher_suites, cipher,
409
0
        OSSL_ECH_CIPHER_LEN)) {
410
0
        ee->suites[ci].kem_id = thiskemid;
411
0
        ee->suites[ci].kdf_id = cipher[0] << 8 | cipher[1];
412
0
        ee->suites[ci].aead_id = cipher[2] << 8 | cipher[3];
413
0
        if (ci++ >= ee->nsuites) {
414
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
415
0
            goto err;
416
0
        }
417
0
    }
418
0
    if (PACKET_remaining(&cipher_suites) > 0
419
0
        || !PACKET_copy_bytes(&ver_pkt, &max_name_len, 1)) {
420
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
421
0
        goto err;
422
0
    }
423
0
    ee->max_name_length = max_name_len;
424
0
    if (!PACKET_get_length_prefixed_1(&ver_pkt, &public_name_pkt)) {
425
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
426
0
        goto err;
427
0
    }
428
0
    if (PACKET_contains_zero_byte(&public_name_pkt)
429
0
        || PACKET_remaining(&public_name_pkt) < TLSEXT_MINLEN_host_name
430
0
        || !PACKET_strndup(&public_name_pkt, &ee->public_name)) {
431
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
432
0
        goto err;
433
0
    }
434
    /*
435
     * We don't really handle ECHConfig extensions as of now,
436
     * (none are well-defined), so we're only skipping over
437
     * whatever we find here. If/when adding real extensions
438
     * then it may be necessary to also check that the set of
439
     * extensions loaded contain no duplicate types.
440
     */
441
0
    if (!PACKET_get_length_prefixed_2(&ver_pkt, &exts)) {
442
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
443
0
        goto err;
444
0
    }
445
0
    if (PACKET_remaining(&exts) > 0
446
0
        && ech_decode_echconfig_exts(ee, &exts) != 1) {
447
0
        ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
448
0
        goto err;
449
0
    }
450
    /* set length of encoding of this ECHConfig */
451
0
    ee->encoded_len = PACKET_data(&ver_pkt) - tmpecp;
452
    /* copy encoded as it might get free'd if a reduce happens */
453
0
    ee->encoded = OPENSSL_memdup(tmpecp, ee->encoded_len);
454
0
    if (ee->encoded == NULL)
455
0
        goto err;
456
0
    if (priv != NULL) {
457
0
        if (EVP_PKEY_get_octet_string_param(priv,
458
0
                OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
459
0
                test_pub, OSSL_ECH_CRYPTO_VAR_SIZE,
460
0
                &test_publen)
461
0
            != 1) {
462
0
            ERR_raise(ERR_LIB_SSL, SSL_R_ECH_DECODE_ERROR);
463
0
            goto err;
464
0
        }
465
0
        if (test_publen == ee->pub_len
466
0
            && !memcmp(test_pub, ee->pub, ee->pub_len)) {
467
0
            EVP_PKEY_up_ref(priv); /* associate the private key */
468
0
            ee->keyshare = priv;
469
0
            ee->for_retry = for_retry;
470
0
        }
471
0
    }
472
0
    ee->loadtime = time(0);
473
0
    *rent = ee;
474
0
    return 1;
475
0
err:
476
0
    ossl_echstore_entry_free(ee);
477
0
    *rent = NULL;
478
0
    return 0;
479
0
}
480
481
/*
482
 * @brief decode and flatten a binary encoded ECHConfigList
483
 * @param es an OSSL_ECHSTORE
484
 * @param priv is an optional private key (NULL if absent)
485
 * @param for_retry says whether to include in a retry_config (if priv present)
486
 * @param binbuf binary encoded ECHConfigList (we hope)
487
 * @param binlen length of binbuf
488
 * @return 1 for success, 0 for error
489
 *
490
 * We may only get one ECHConfig per list, but there can be more.  We want each
491
 * element of the output to contain exactly one ECHConfig so that a client
492
 * could sensibly down select to the one they prefer later, and so that we have
493
 * the specific encoded value of that ECHConfig for inclusion in the HPKE info
494
 * parameter when finally encrypting or decrypting an inner ClientHello.
495
 *
496
 * If a private value is provided then that'll only be associated with the
497
 * relevant public value, if >1 public value was present in the ECHConfigList.
498
 */
499
static int ech_decode_and_flatten(OSSL_ECHSTORE *es, EVP_PKEY *priv, int for_retry,
500
    unsigned char *binbuf, size_t binblen)
501
0
{
502
0
    int rv = 0;
503
0
    size_t remaining = 0;
504
0
    PACKET opkt, pkt;
505
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
506
507
0
    if (binbuf == NULL || binblen == 0 || binblen < OSSL_ECH_MIN_ECHCONFIG_LEN
508
0
        || binblen >= OSSL_ECH_MAX_ECHCONFIG_LEN) {
509
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
510
0
        goto err;
511
0
    }
512
0
    if (PACKET_buf_init(&opkt, binbuf, binblen) != 1
513
0
        || !PACKET_get_length_prefixed_2(&opkt, &pkt)) {
514
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
515
0
        goto err;
516
0
    }
517
0
    remaining = PACKET_remaining(&pkt);
518
0
    while (remaining > 0) {
519
0
        if (ech_decode_one_entry(&ee, &pkt, priv, for_retry) != 1) {
520
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
521
0
            goto err;
522
0
        }
523
0
        remaining = PACKET_remaining(&pkt);
524
        /* if unsupported version we can skip over */
525
0
        if (ee == NULL)
526
0
            continue;
527
        /* do final checks on suites, exts, and fail if issues */
528
0
        if (ech_final_config_checks(ee) != 1)
529
0
            goto err;
530
        /* push entry into store */
531
0
        if (es->entries == NULL)
532
0
            es->entries = sk_OSSL_ECHSTORE_ENTRY_new_null();
533
0
        if (es->entries == NULL) {
534
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
535
0
            goto err;
536
0
        }
537
0
        if (!sk_OSSL_ECHSTORE_ENTRY_push(es->entries, ee)) {
538
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
539
0
            goto err;
540
0
        }
541
0
        ee = NULL;
542
0
    }
543
0
    rv = 1;
544
0
err:
545
0
    ossl_echstore_entry_free(ee);
546
0
    return rv;
547
0
}
548
549
/*
550
 * @brief check a private matches some public
551
 * @param es is the ECH store
552
 * @param priv is the private value
553
 * @return 1 if we have a match, zero otherwise
554
 */
555
static int check_priv_matches(OSSL_ECHSTORE *es, EVP_PKEY *priv)
556
0
{
557
0
    int num, ent, gotone = 0;
558
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
559
560
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
561
0
    for (ent = 0; ent != num; ent++) {
562
0
        ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, ent);
563
0
        if (ee == NULL) {
564
0
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
565
0
            return 0;
566
0
        }
567
0
        if (EVP_PKEY_eq(ee->keyshare, priv)) {
568
0
            gotone = 1;
569
0
            break;
570
0
        }
571
0
    }
572
0
    return gotone;
573
0
}
574
575
/*
576
 * @brief decode input ECHConfigList and associate optional private info
577
 * @param es is the OSSL_ECHSTORE
578
 * @param in is the BIO from which we'll get the ECHConfigList
579
 * @param priv is an optional private key
580
 * @param for_retry 1 if the public related to priv ought be in retry_config
581
 */
582
static int ech_read_priv_echconfiglist(OSSL_ECHSTORE *es, BIO *in,
583
    EVP_PKEY *priv, int for_retry)
584
0
{
585
0
    int rv = 0, detfmt, tdeclen = 0;
586
0
    size_t encodedlen = 0, binlen = 0;
587
0
    unsigned char *encodedval = NULL, *binbuf = NULL;
588
0
    BIO *btmp = NULL, *btmp1 = NULL;
589
590
0
    if (es == NULL || in == NULL) {
591
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
592
0
        return 0;
593
0
    }
594
0
    if (ech_bio2buf(in, &encodedval, &encodedlen) != 1) {
595
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
596
0
        return 0;
597
0
    }
598
0
    if (encodedlen >= OSSL_ECH_MAX_ECHCONFIG_LEN) { /* sanity check */
599
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
600
0
        goto err;
601
0
    }
602
0
    if (ech_check_format(encodedval, encodedlen, &detfmt) != 1) {
603
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
604
0
        goto err;
605
0
    }
606
0
    if (detfmt == OSSL_ECH_FMT_BIN) { /* copy buffer if binary format */
607
0
        binbuf = OPENSSL_memdup(encodedval, encodedlen);
608
0
        if (binbuf == NULL)
609
0
            goto err;
610
0
        binlen = encodedlen;
611
0
    }
612
0
    if (detfmt == OSSL_ECH_FMT_B64TXT) {
613
0
        btmp = BIO_new_mem_buf(encodedval, (int)encodedlen);
614
0
        if (btmp == NULL) {
615
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
616
0
            goto err;
617
0
        }
618
0
        btmp1 = BIO_new(BIO_f_base64());
619
0
        if (btmp1 == NULL) {
620
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
621
0
            goto err;
622
0
        }
623
0
        BIO_set_flags(btmp1, BIO_FLAGS_BASE64_NO_NL);
624
0
        btmp = BIO_push(btmp1, btmp);
625
        /* overestimate but good enough */
626
0
        binbuf = OPENSSL_malloc(encodedlen);
627
0
        if (binbuf == NULL)
628
0
            goto err;
629
0
        tdeclen = BIO_read(btmp, binbuf, (int)encodedlen);
630
0
        if (tdeclen <= 0) { /* need int for -1 return in failure case */
631
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
632
0
            goto err;
633
0
        }
634
0
        binlen = tdeclen;
635
0
    }
636
0
    if (ech_decode_and_flatten(es, priv, for_retry, binbuf, binlen) != 1) {
637
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
638
0
        goto err;
639
0
    }
640
0
    if (priv != NULL && check_priv_matches(es, priv) == 0)
641
0
        goto err;
642
0
    rv = 1;
643
0
err:
644
0
    BIO_free_all(btmp);
645
0
    OPENSSL_free(binbuf);
646
0
    OPENSSL_free(encodedval);
647
0
    return rv;
648
0
}
649
650
/*
651
 * API calls built around OSSL_ECHSSTORE
652
 */
653
654
OSSL_ECHSTORE *OSSL_ECHSTORE_new(OSSL_LIB_CTX *libctx, const char *propq)
655
0
{
656
0
    OSSL_ECHSTORE *es = NULL;
657
658
0
    es = OPENSSL_zalloc(sizeof(*es));
659
0
    if (es == NULL)
660
0
        return 0;
661
0
    es->libctx = libctx;
662
0
    if (propq != NULL) {
663
0
        es->propq = OPENSSL_strdup(propq);
664
0
        if (es->propq == NULL) {
665
0
            OPENSSL_free(es);
666
0
            return 0;
667
0
        }
668
0
    }
669
670
0
    return es;
671
0
}
672
673
void OSSL_ECHSTORE_free(OSSL_ECHSTORE *es)
674
0
{
675
0
    if (es == NULL)
676
0
        return;
677
0
    sk_OSSL_ECHSTORE_ENTRY_pop_free(es->entries, ossl_echstore_entry_free);
678
0
    OPENSSL_free(es->propq);
679
0
    OPENSSL_free(es);
680
0
    return;
681
0
}
682
683
int OSSL_ECHSTORE_new_config(OSSL_ECHSTORE *es,
684
    uint16_t echversion, uint8_t max_name_length,
685
    const char *public_name, OSSL_HPKE_SUITE suite)
686
0
{
687
0
    size_t pnlen = 0, publen = OSSL_ECH_CRYPTO_VAR_SIZE;
688
0
    unsigned char pub[OSSL_ECH_CRYPTO_VAR_SIZE];
689
0
    int rv = 0;
690
0
    unsigned char *bp = NULL;
691
0
    size_t bblen = 0;
692
0
    EVP_PKEY *privp = NULL;
693
0
    uint8_t config_id = 0;
694
0
    WPACKET epkt;
695
0
    BUF_MEM *epkt_mem = NULL;
696
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
697
698
    /* basic checks */
699
0
    if (es == NULL) {
700
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
701
0
        return 0;
702
0
    }
703
0
    pnlen = (public_name == NULL ? 0 : strlen(public_name));
704
0
    if (pnlen == 0 || pnlen > OSSL_ECH_MAX_PUBLICNAME) {
705
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
706
0
        return 0;
707
0
    }
708
    /* this used have more versions and will again in future */
709
0
    switch (echversion) {
710
0
    case OSSL_ECH_RFC9849_VERSION:
711
0
        break;
712
0
    default:
713
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
714
0
        return 0;
715
0
    }
716
    /*
717
     *   Reminder, for draft-13 we want this:
718
     *
719
     *   opaque HpkePublicKey<1..2^16-1>;
720
     *   uint16 HpkeKemId;  // Defined in I-D.irtf-cfrg-hpke
721
     *   uint16 HpkeKdfId;  // Defined in I-D.irtf-cfrg-hpke
722
     *   uint16 HpkeAeadId; // Defined in I-D.irtf-cfrg-hpke
723
     *   struct {
724
     *       HpkeKdfId kdf_id;
725
     *       HpkeAeadId aead_id;
726
     *   } HpkeSymmetricCipherSuite;
727
     *   struct {
728
     *       uint8 config_id;
729
     *       HpkeKemId kem_id;
730
     *       HpkePublicKey public_key;
731
     *       HpkeSymmetricCipherSuite cipher_suites<4..2^16-4>;
732
     *   } HpkeKeyConfig;
733
     *   struct {
734
     *       HpkeKeyConfig key_config;
735
     *       uint8 maximum_name_length;
736
     *       opaque public_name<1..255>;
737
     *       Extension extensions<0..2^16-1>;
738
     *   } ECHConfigContents;
739
     *   struct {
740
     *       uint16 version;
741
     *       uint16 length;
742
     *       select (ECHConfig.version) {
743
     *         case 0xfe0d: ECHConfigContents contents;
744
     *       }
745
     *   } ECHConfig;
746
     *   ECHConfig ECHConfigList<1..2^16-1>;
747
     */
748
0
    if ((epkt_mem = BUF_MEM_new()) == NULL
749
0
        || !BUF_MEM_grow(epkt_mem, OSSL_ECH_MAX_ECHCONFIG_LEN)
750
0
        || !WPACKET_init(&epkt, epkt_mem)) {
751
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
752
0
        goto err_no_epkt;
753
0
    }
754
    /* random config_id */
755
0
    if (RAND_bytes_ex(es->libctx, (unsigned char *)&config_id, 1, 0) <= 0) {
756
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
757
0
        goto err;
758
0
    }
759
    /* key pair */
760
0
    if (OSSL_HPKE_keygen(suite, pub, &publen, &privp, NULL, 0,
761
0
            es->libctx, es->propq)
762
0
        != 1) {
763
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
764
0
        goto err;
765
0
    }
766
    /* config id, KEM, public, KDF, AEAD, max name len, public_name, exts */
767
0
    if ((bp = WPACKET_get_curr(&epkt)) == NULL
768
0
        || !WPACKET_start_sub_packet_u16(&epkt)
769
0
        || !WPACKET_put_bytes_u16(&epkt, echversion)
770
0
        || !WPACKET_start_sub_packet_u16(&epkt)
771
0
        || !WPACKET_put_bytes_u8(&epkt, config_id)
772
0
        || !WPACKET_put_bytes_u16(&epkt, suite.kem_id)
773
0
        || !WPACKET_start_sub_packet_u16(&epkt)
774
0
        || !WPACKET_memcpy(&epkt, pub, publen)
775
0
        || !WPACKET_close(&epkt)
776
0
        || !WPACKET_start_sub_packet_u16(&epkt)
777
0
        || !WPACKET_put_bytes_u16(&epkt, suite.kdf_id)
778
0
        || !WPACKET_put_bytes_u16(&epkt, suite.aead_id)
779
0
        || !WPACKET_close(&epkt)
780
0
        || !WPACKET_put_bytes_u8(&epkt, max_name_length)
781
0
        || !WPACKET_start_sub_packet_u8(&epkt)
782
0
        || !WPACKET_memcpy(&epkt, public_name, pnlen)
783
0
        || !WPACKET_close(&epkt)
784
0
        || !WPACKET_start_sub_packet_u16(&epkt)
785
0
        || !WPACKET_memcpy(&epkt, NULL, 0) /* no extensions */
786
0
        || !WPACKET_close(&epkt)
787
0
        || !WPACKET_close(&epkt)
788
0
        || !WPACKET_close(&epkt)) {
789
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
790
0
        goto err;
791
0
    }
792
    /* bp, bblen has encoding */
793
0
    if (!WPACKET_get_total_written(&epkt, &bblen)) {
794
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
795
0
        goto err;
796
0
    }
797
0
    if ((ee = OPENSSL_zalloc(sizeof(*ee))) == NULL)
798
0
        goto err;
799
0
    ee->suites = OPENSSL_malloc(sizeof(*ee->suites));
800
0
    if (ee->suites == NULL)
801
0
        goto err;
802
0
    ee->version = echversion;
803
0
    ee->pub_len = publen;
804
0
    ee->pub = OPENSSL_memdup(pub, publen);
805
0
    if (ee->pub == NULL)
806
0
        goto err;
807
0
    ee->nsuites = 1;
808
0
    ee->suites[0] = suite;
809
0
    ee->public_name = OPENSSL_strdup(public_name);
810
0
    if (ee->public_name == NULL)
811
0
        goto err;
812
0
    ee->max_name_length = max_name_length;
813
0
    ee->config_id = config_id;
814
0
    ee->keyshare = privp;
815
0
    privp = NULL; /* don't free twice */
816
    /* "steal" the encoding from the memory */
817
0
    ee->encoded = (unsigned char *)epkt_mem->data;
818
0
    ee->encoded_len = bblen;
819
0
    epkt_mem->data = NULL;
820
0
    epkt_mem->length = 0;
821
0
    ee->loadtime = time(0);
822
0
    if (ech_final_config_checks(ee) != 1) /* check our work */
823
0
        goto err;
824
    /* push entry into store */
825
0
    if (es->entries == NULL)
826
0
        es->entries = sk_OSSL_ECHSTORE_ENTRY_new_null();
827
0
    if (es->entries == NULL) {
828
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
829
0
        goto err;
830
0
    }
831
0
    if (!sk_OSSL_ECHSTORE_ENTRY_push(es->entries, ee)) {
832
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
833
0
        goto err;
834
0
    }
835
0
    WPACKET_finish(&epkt);
836
0
    BUF_MEM_free(epkt_mem);
837
0
    return 1;
838
839
0
err:
840
0
    ossl_echstore_entry_free(ee);
841
0
    EVP_PKEY_free(privp);
842
0
    WPACKET_cleanup(&epkt);
843
0
err_no_epkt:
844
0
    BUF_MEM_free(epkt_mem);
845
0
    return rv;
846
0
}
847
848
int OSSL_ECHSTORE_write_pem(OSSL_ECHSTORE *es, int index, BIO *out)
849
0
{
850
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
851
0
    int rv = 0, num = 0, chosen = 0, doall = 0;
852
0
    WPACKET epkt; /* used if we want to merge ECHConfigs for output */
853
0
    BUF_MEM *epkt_mem = NULL;
854
0
    size_t allencoded_len;
855
856
0
    if (es == NULL) {
857
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
858
0
        return 0;
859
0
    }
860
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
861
0
    if (num <= 0) {
862
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
863
0
        return 0;
864
0
    }
865
0
    if (index >= num) {
866
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
867
0
        return 0;
868
0
    }
869
0
    if (index == OSSL_ECHSTORE_ALL)
870
0
        doall = 1;
871
0
    else if (index == OSSL_ECHSTORE_LAST)
872
0
        chosen = num - 1;
873
0
    else
874
0
        chosen = index;
875
0
    memset(&epkt, 0, sizeof(epkt));
876
0
    if (doall == 0) {
877
0
        ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, chosen);
878
0
        if (ee == NULL || ee->encoded == NULL) {
879
0
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
880
0
            return 0;
881
0
        }
882
        /* private key first */
883
0
        if (ee->keyshare != NULL
884
0
            && !PEM_write_bio_PrivateKey(out, ee->keyshare, NULL, NULL, 0,
885
0
                NULL, NULL)) {
886
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
887
0
            goto err;
888
0
        }
889
0
        if (PEM_write_bio(out, PEM_STRING_ECHCONFIG, NULL,
890
0
                ee->encoded, (long)ee->encoded_len)
891
0
            <= 0) {
892
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
893
0
            goto err;
894
0
        }
895
0
    } else {
896
        /* catenate the encodings into one */
897
0
        if ((epkt_mem = BUF_MEM_new()) == NULL
898
0
            || !BUF_MEM_grow(epkt_mem, OSSL_ECH_MAX_ECHCONFIG_LEN)
899
0
            || !WPACKET_init(&epkt, epkt_mem)
900
0
            || !WPACKET_start_sub_packet_u16(&epkt)) {
901
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
902
0
            goto err;
903
0
        }
904
0
        for (chosen = 0; chosen != num; chosen++) {
905
0
            ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, chosen);
906
0
            if (ee == NULL || ee->encoded == NULL) {
907
0
                ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
908
0
                return 0;
909
0
            }
910
0
            if (!WPACKET_memcpy(&epkt, ee->encoded, ee->encoded_len)) {
911
0
                ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
912
0
                goto err;
913
0
            }
914
0
        }
915
0
        if (!WPACKET_close(&epkt)) {
916
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
917
0
            goto err;
918
0
        }
919
0
        if (!WPACKET_get_total_written(&epkt, &allencoded_len)) {
920
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
921
0
            goto err;
922
0
        }
923
0
        if (PEM_write_bio(out, PEM_STRING_ECHCONFIG, NULL,
924
0
                (unsigned char *)epkt_mem->data,
925
0
                (long)allencoded_len)
926
0
            <= 0) {
927
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
928
0
            goto err;
929
0
        }
930
0
    }
931
0
    rv = 1;
932
0
err:
933
0
    WPACKET_cleanup(&epkt);
934
0
    BUF_MEM_free(epkt_mem);
935
0
    return rv;
936
0
}
937
938
int OSSL_ECHSTORE_read_echconfiglist(OSSL_ECHSTORE *es, BIO *in)
939
0
{
940
0
    return ech_read_priv_echconfiglist(es, in, NULL, 0);
941
0
}
942
943
int OSSL_ECHSTORE_get1_info(OSSL_ECHSTORE *es, int index, time_t *loaded_secs,
944
    char **public_name, char **echconfig,
945
    int *has_private, int *for_retry)
946
0
{
947
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
948
0
    unsigned int j = 0;
949
0
    int num = 0;
950
0
    BIO *out = NULL;
951
0
    time_t now = time(0);
952
0
    size_t ehlen;
953
0
    unsigned char *ignore = NULL;
954
955
0
    if (es == NULL || loaded_secs == NULL || public_name == NULL
956
0
        || echconfig == NULL || has_private == NULL || for_retry == NULL) {
957
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
958
0
        return 0;
959
0
    }
960
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
961
0
    if (num == 0 || index < 0 || index >= num) {
962
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
963
0
        return 0;
964
0
    }
965
0
    ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, index);
966
0
    if (ee == NULL) {
967
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
968
0
        return 0;
969
0
    }
970
0
    *loaded_secs = now - ee->loadtime;
971
0
    *public_name = NULL;
972
0
    *echconfig = NULL;
973
0
    if (ee->public_name != NULL) {
974
0
        *public_name = OPENSSL_strdup(ee->public_name);
975
0
        if (*public_name == NULL)
976
0
            goto err;
977
0
    }
978
0
    *has_private = (ee->keyshare == NULL ? 0 : 1);
979
0
    *for_retry = ee->for_retry;
980
    /* Now "print" the ECHConfigList */
981
0
    out = BIO_new(BIO_s_mem());
982
0
    if (out == NULL) {
983
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
984
0
        goto err;
985
0
    }
986
0
    if (ee->version != OSSL_ECH_RFC9849_VERSION) {
987
        /* just note we don't support that one today */
988
0
        BIO_printf(out, "[Unsupported version (%04x)]", ee->version);
989
0
    } else {
990
        /* version, config_id, public_name, and kem */
991
0
        BIO_printf(out, "[%04x,%02x,%s,[", ee->version, ee->config_id,
992
0
            ee->public_name != NULL ? (char *)ee->public_name : "NULL");
993
        /* ciphersuites */
994
0
        for (j = 0; j != ee->nsuites; j++) {
995
0
            BIO_printf(out, "%04x,%04x,%04x", ee->suites[j].kem_id,
996
0
                ee->suites[j].kdf_id, ee->suites[j].aead_id);
997
0
            if (j < (ee->nsuites - 1))
998
0
                BIO_printf(out, ",");
999
0
        }
1000
0
        BIO_printf(out, "],");
1001
        /* public key */
1002
0
        for (j = 0; j != ee->pub_len; j++)
1003
0
            BIO_printf(out, "%02x", ee->pub[j]);
1004
        /* max name length and (only) number of extensions */
1005
0
        BIO_printf(out, ",%02x,%02x]", ee->max_name_length,
1006
0
            ee->exts == NULL ? 0 : sk_OSSL_ECHEXT_num(ee->exts));
1007
0
    }
1008
0
    ehlen = BIO_get_mem_data(out, &ignore);
1009
0
    if (ehlen > INT_MAX)
1010
0
        goto err;
1011
0
    *echconfig = OPENSSL_malloc(ehlen + 1);
1012
0
    if (*echconfig == NULL)
1013
0
        goto err;
1014
0
    if (BIO_read(out, *echconfig, (int)ehlen) <= 0) {
1015
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1016
0
        goto err;
1017
0
    }
1018
0
    (*echconfig)[ehlen] = '\0';
1019
0
    BIO_free(out);
1020
0
    return 1;
1021
0
err:
1022
0
    BIO_free(out);
1023
0
    OPENSSL_free(*public_name);
1024
0
    *public_name = NULL;
1025
0
    OPENSSL_free(*echconfig);
1026
0
    *echconfig = NULL;
1027
0
    return 0;
1028
0
}
1029
1030
int OSSL_ECHSTORE_downselect(OSSL_ECHSTORE *es, int index)
1031
0
{
1032
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
1033
0
    int i, num = 0, chosen = OSSL_ECHSTORE_ALL;
1034
1035
0
    if (es == NULL) {
1036
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1037
0
        return 0;
1038
0
    }
1039
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
1040
0
    if (num == 0) {
1041
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1042
0
        return 0;
1043
0
    }
1044
0
    if (index <= OSSL_ECHSTORE_ALL) {
1045
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1046
0
        return 0;
1047
0
    }
1048
0
    if (index == OSSL_ECHSTORE_LAST) {
1049
0
        chosen = num - 1;
1050
0
    } else if (index >= num) {
1051
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1052
0
        return 0;
1053
0
    } else {
1054
0
        chosen = index;
1055
0
    }
1056
0
    for (i = num - 1; i >= 0; i--) {
1057
0
        if (i == chosen)
1058
0
            continue;
1059
0
        ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, i);
1060
0
        ossl_echstore_entry_free(ee);
1061
0
        sk_OSSL_ECHSTORE_ENTRY_delete(es->entries, i);
1062
0
    }
1063
0
    return 1;
1064
0
}
1065
1066
int OSSL_ECHSTORE_set1_key_and_read_pem(OSSL_ECHSTORE *es, EVP_PKEY *priv,
1067
    BIO *in, int for_retry)
1068
0
{
1069
0
    unsigned char *b64 = NULL;
1070
0
    long b64len = 0;
1071
0
    BIO *b64bio = NULL;
1072
0
    int rv = 0;
1073
0
    char *pname = NULL, *pheader = NULL;
1074
1075
    /* we allow for a NULL private key */
1076
0
    if (es == NULL || in == NULL) {
1077
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1078
0
        return 0;
1079
0
    }
1080
0
    if (PEM_read_bio(in, &pname, &pheader, &b64, &b64len) != 1) {
1081
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1082
0
        return 0;
1083
0
    }
1084
0
    if (pname == NULL || strcmp(pname, PEM_STRING_ECHCONFIG) != 0) {
1085
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1086
0
        goto err;
1087
0
    }
1088
0
    b64bio = BIO_new(BIO_s_mem());
1089
0
    if (b64bio == NULL
1090
0
        || BIO_write(b64bio, b64, b64len) <= 0
1091
0
        || ech_read_priv_echconfiglist(es, b64bio, priv, for_retry) != 1) {
1092
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1093
0
        goto err;
1094
0
    }
1095
0
    rv = 1;
1096
0
err:
1097
0
    OPENSSL_free(pname);
1098
0
    OPENSSL_free(pheader);
1099
0
    BIO_free_all(b64bio);
1100
0
    OPENSSL_free(b64);
1101
0
    return rv;
1102
0
}
1103
1104
int OSSL_ECHSTORE_read_pem(OSSL_ECHSTORE *es, BIO *in, int for_retry)
1105
0
{
1106
0
    EVP_PKEY *priv = NULL;
1107
0
    int rv = 0;
1108
0
    BIO *fbio = BIO_new(BIO_f_buffer());
1109
1110
0
    if (fbio == NULL || es == NULL || in == NULL) {
1111
0
        BIO_free_all(fbio);
1112
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1113
0
        return 0;
1114
0
    }
1115
    /*
1116
     * Read private key then handoff to set1_key_and_read_pem.
1117
     * We allow for no private key as an option, to handle that
1118
     * the BIO_f_buffer allows us to seek back to the start.
1119
     */
1120
0
    BIO_push(fbio, in);
1121
0
    if (!PEM_read_bio_PrivateKey_ex(fbio, &priv, NULL, NULL, es->libctx, es->propq)
1122
0
        && BIO_seek(fbio, 0) < 0) {
1123
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1124
0
        goto err;
1125
0
    }
1126
0
    rv = OSSL_ECHSTORE_set1_key_and_read_pem(es, priv, fbio, for_retry);
1127
0
err:
1128
0
    EVP_PKEY_free(priv);
1129
0
    BIO_pop(fbio);
1130
0
    BIO_free_all(fbio);
1131
0
    return rv;
1132
0
}
1133
1134
int OSSL_ECHSTORE_num_entries(const OSSL_ECHSTORE *es, int *numentries)
1135
0
{
1136
0
    if (es == NULL || numentries == NULL) {
1137
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1138
0
        return 0;
1139
0
    }
1140
0
    *numentries = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
1141
0
    return 1;
1142
0
}
1143
1144
int OSSL_ECHSTORE_num_keys(OSSL_ECHSTORE *es, int *numkeys)
1145
0
{
1146
0
    int i, num = 0, count = 0;
1147
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
1148
1149
0
    if (es == NULL || numkeys == NULL) {
1150
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1151
0
        return 0;
1152
0
    }
1153
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
1154
0
    for (i = 0; i != num; i++) {
1155
0
        ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, i);
1156
0
        if (ee == NULL) {
1157
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1158
0
            return 0;
1159
0
        }
1160
0
        count += (ee->keyshare != NULL);
1161
0
    }
1162
0
    *numkeys = count;
1163
0
    return 1;
1164
0
}
1165
1166
int OSSL_ECHSTORE_flush_keys(OSSL_ECHSTORE *es, time_t age)
1167
0
{
1168
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
1169
0
    int i, num = 0;
1170
0
    time_t now = time(0);
1171
1172
0
    if (es == NULL) {
1173
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
1174
0
        return 0;
1175
0
    }
1176
0
    num = (es->entries == NULL ? 0 : sk_OSSL_ECHSTORE_ENTRY_num(es->entries));
1177
0
    if (num == 0) {
1178
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1179
0
        return 0;
1180
0
    }
1181
0
    for (i = num - 1; i >= 0; i--) {
1182
0
        ee = sk_OSSL_ECHSTORE_ENTRY_value(es->entries, i);
1183
0
        if (ee == NULL) {
1184
0
            ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
1185
0
            return 0;
1186
0
        }
1187
0
        if (ee->keyshare != NULL && ee->loadtime + age <= now) {
1188
0
            ossl_echstore_entry_free(ee);
1189
0
            sk_OSSL_ECHSTORE_ENTRY_delete(es->entries, i);
1190
0
        }
1191
0
    }
1192
0
    return 1;
1193
0
}