openssl-src 400.0.0+4.0.1

Source of OpenSSL and logic to build it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/ssl.h>
#include <openssl/ech.h>
#include "internal/ssl_unwrap.h"
#include "../ssl_local.h"

int SSL_CTX_set1_echstore(SSL_CTX *ctx, OSSL_ECHSTORE *es)
{
    if (ctx == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    OSSL_ECHSTORE_free(ctx->ext.ech.es);
    ctx->ext.ech.es = NULL;
    if (es == NULL)
        return 1;
    if ((ctx->ext.ech.es = ossl_echstore_dup(es)) == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        return 0;
    }
    return 1;
}

int SSL_set1_echstore(SSL *ssl, OSSL_ECHSTORE *es)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    OSSL_ECHSTORE_free(s->ext.ech.es);
    s->ext.ech.es = NULL;
    if (es == NULL)
        return 1;
    if ((s->ext.ech.es = ossl_echstore_dup(es)) == NULL) {
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
        return 0;
    }
    /*
     * Here, and below, if the application calls an API that implies it
     * wants to try ECH, then we set attempted to 1
     */
    s->ext.ech.attempted = 1;
    return 1;
}

OSSL_ECHSTORE *SSL_CTX_get1_echstore(const SSL_CTX *ctx)
{
    OSSL_ECHSTORE *dup = NULL;

    if (ctx == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (ctx->ext.ech.es == NULL)
        return NULL;
    if ((dup = ossl_echstore_dup(ctx->ext.ech.es)) == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        return NULL;
    }
    return dup;
}

OSSL_ECHSTORE *SSL_get1_echstore(const SSL *ssl)
{
    SSL_CONNECTION *s;
    OSSL_ECHSTORE *dup = NULL;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (s->ext.ech.es == NULL)
        return NULL;
    if ((dup = ossl_echstore_dup(s->ext.ech.es)) == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        return NULL;
    }
    return dup;
}

int SSL_ech_set1_server_names(SSL *ssl, const char *inner_name,
    const char *outer_name, int no_outer)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    OPENSSL_free(s->ext.hostname);
    s->ext.hostname = NULL;
    if (inner_name != NULL) {
        s->ext.hostname = OPENSSL_strdup(inner_name);
        if (s->ext.hostname == NULL)
            return 0;
    }
    OPENSSL_free(s->ext.ech.outer_hostname);
    s->ext.ech.outer_hostname = NULL;
    if (no_outer == 0 && outer_name != NULL && strlen(outer_name) > 0) {
        s->ext.ech.outer_hostname = OPENSSL_strdup(outer_name);
        if (s->ext.ech.outer_hostname == NULL)
            return 0;
    }
    s->ext.ech.no_outer = no_outer;
    s->ext.ech.attempted = 1;
    return 1;
}

int SSL_ech_set1_outer_server_name(SSL *ssl, const char *outer_name,
    int no_outer)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    OPENSSL_free(s->ext.ech.outer_hostname);
    s->ext.ech.outer_hostname = NULL;
    if (no_outer == 0 && outer_name != NULL && strlen(outer_name) > 0) {
        s->ext.ech.outer_hostname = OPENSSL_strdup(outer_name);
        if (s->ext.ech.outer_hostname == NULL)
            return 0;
    }
    s->ext.ech.no_outer = no_outer;
    s->ext.ech.attempted = 1;
    return 1;
}

/*
 * Note that this function returns 1 for success and 0 for error. This
 * contrasts with SSL_set1_alpn_protos() which (unusually for OpenSSL)
 * returns 0 for success and 1 on error.
 */
int SSL_ech_set1_outer_alpn_protos(SSL *ssl, const unsigned char *protos,
    const size_t protos_len)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    OPENSSL_free(s->ext.ech.alpn_outer);
    s->ext.ech.alpn_outer = NULL;
    if (protos == NULL)
        return 1;
    if (protos_len == 0) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    s->ext.ech.alpn_outer = OPENSSL_memdup(protos, protos_len);
    if (s->ext.ech.alpn_outer == NULL)
        return 0;
    s->ext.ech.alpn_outer_len = protos_len;
    s->ext.ech.attempted = 1;
    return 1;
}

int SSL_ech_get1_status(SSL *ssl, char **inner_sni, char **outer_sni)
{
    char *sinner = NULL;
    char *souter = NULL;
    SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL(ssl);

    if (s == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return SSL_ECH_STATUS_FAILED;
    }
    if (outer_sni == NULL || inner_sni == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        return SSL_ECH_STATUS_FAILED;
    }
    *outer_sni = NULL;
    *inner_sni = NULL;
    if (s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
        if (s->ext.ech.returned != NULL)
            return SSL_ECH_STATUS_GREASE_ECH;
        return SSL_ECH_STATUS_GREASE;
    }
    if ((s->options & SSL_OP_ECH_GREASE) != 0 && s->ext.ech.attempted != 1)
        return SSL_ECH_STATUS_GREASE;
    if (s->ext.ech.backend == 1) {
        if (s->ext.hostname != NULL
            && (*inner_sni = OPENSSL_strdup(s->ext.hostname)) == NULL) {
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
            return SSL_ECH_STATUS_FAILED;
        }
        return SSL_ECH_STATUS_BACKEND;
    }
    if (s->ext.ech.es == NULL)
        return SSL_ECH_STATUS_NOT_CONFIGURED;
    /* Set output vars - note we may be pointing to NULL which is fine */
    if (s->server == 0) {
        sinner = s->ext.hostname;
        if (s->ext.ech.attempted == 1 && s->ext.ech.success == 0)
            sinner = s->ext.ech.former_inner;
        if (s->ext.ech.no_outer == 0)
            souter = s->ext.ech.outer_hostname;
        else
            souter = NULL;
    } else {
        if (s->ext.ech.es != NULL && s->ext.ech.success == 1) {
            sinner = s->ext.hostname;
            souter = s->ext.ech.outer_hostname;
        }
    }
    if (s->ext.ech.es != NULL && s->ext.ech.attempted == 1
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech
        && s->ext.ech.grease != OSSL_ECH_IS_GREASE) {
        long vr = X509_V_OK;

        vr = SSL_get_verify_result(ssl);
        if (sinner != NULL
            && (*inner_sni = OPENSSL_strdup(sinner)) == NULL) {
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
            return SSL_ECH_STATUS_FAILED;
        }
        if (souter != NULL
            && (*outer_sni = OPENSSL_strdup(souter)) == NULL) {
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
            return SSL_ECH_STATUS_FAILED;
        }
        if (s->ext.ech.success == 1) {
            if (vr == X509_V_OK)
                return SSL_ECH_STATUS_SUCCESS;
            else
                return SSL_ECH_STATUS_BAD_NAME;
        } else {
            if (vr == X509_V_OK && s->ext.ech.returned != NULL)
                return SSL_ECH_STATUS_FAILED_ECH;
            else if (vr != X509_V_OK && s->ext.ech.returned != NULL)
                return SSL_ECH_STATUS_FAILED_ECH_BAD_NAME;
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
            return SSL_ECH_STATUS_FAILED;
        }
    }
    return SSL_ECH_STATUS_NOT_TRIED;
}

int SSL_ech_set1_grease_suite(SSL *ssl, const char *suite)
{
    SSL_CONNECTION *s;
    OSSL_HPKE_SUITE hpke_suite_in = OSSL_HPKE_SUITE_DEFAULT;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    /* check suite makes sense */
    if (OSSL_HPKE_str2suite(suite, &hpke_suite_in) != 1) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
        return 0;
    }
    OPENSSL_free(s->ext.ech.grease_suite);
    s->ext.ech.grease_suite = NULL;
    if (suite == NULL)
        return 1;
    s->ext.ech.grease_suite = OPENSSL_strdup(suite);
    if (s->ext.ech.grease_suite == NULL)
        return 0;
    s->ext.ech.grease = OSSL_ECH_IS_GREASE;
    return 1;
}

int SSL_ech_set_grease_type(SSL *ssl, uint16_t type)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return 0;
    s->ext.ech.attempted_type = type;
    s->ext.ech.grease = OSSL_ECH_IS_GREASE;
    return 1;
}

void SSL_ech_set_callback(SSL *ssl, SSL_ech_cb_func f)
{
    SSL_CONNECTION *s;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL)
        return;
    s->ext.ech.cb = f;
    return;
}

int SSL_ech_get1_retry_config(SSL *ssl, unsigned char **ec, size_t *eclen)
{
    SSL_CONNECTION *s;
    OSSL_ECHSTORE *ve = NULL;
    BIO *in = NULL;
    int rv = 0;
    OSSL_LIB_CTX *libctx = NULL;
    const char *propq = NULL;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL || ec == NULL || eclen == NULL
        || s->ext.ech.returned_len > INT_MAX) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
        goto err;
    }
    if (s->ext.ech.returned == NULL) {
        *ec = NULL;
        *eclen = 0;
        return 1;
    }
    /*
     * before returning retry-configs check we're in a good
     * state - either the session has worked, or else it
     * failed but ECH was the only failure (we only set the
     * retry_configs_ok flag when all else worked and we're
     * about to send the ECH required alert)
     */
    if (SSL_is_init_finished(ssl) != 1 && s->ext.ech.retry_configs_ok != 1) {
        ERR_raise(ERR_LIB_SSL, ERR_R_OSSL_STORE_LIB);
        goto err;
    }
    /*
     * To not hand rubbish to application, we'll decode the value we have
     * so only syntactically good things are passed up. We won't insist
     * though that every entry in the retry_config list seems good - it
     * could be that e.g. one is a newer version than we support now,
     * and letting the application see that might cause someone to do an
     * upgrade.
     */
    if (s->ext.ech.es != NULL) {
        libctx = s->ext.ech.es->libctx;
        propq = s->ext.ech.es->propq;
    }
    if ((in = BIO_new(BIO_s_mem())) == NULL
        || BIO_write(in, s->ext.ech.returned, (int)s->ext.ech.returned_len) <= 0
        || (ve = OSSL_ECHSTORE_new(libctx, propq)) == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    if (OSSL_ECHSTORE_read_echconfiglist(ve, in) != 1) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    /* all good, copy and return */
    *ec = OPENSSL_memdup(s->ext.ech.returned, s->ext.ech.returned_len);
    if (*ec == NULL)
        goto err;
    *eclen = s->ext.ech.returned_len;
    rv = 1;
err:
    OSSL_ECHSTORE_free(ve);
    BIO_free_all(in);
    return rv;
}

/*
 * Note that this function returns 1 for success and 0 for error. This
 * contrasts with SSL_CTX_set1_alpn_protos() which (unusually for OpenSSL)
 * returns 0 for success and 1 on error.
 */
int SSL_CTX_ech_set1_outer_alpn_protos(SSL_CTX *ctx,
    const unsigned char *protos,
    const size_t protos_len)
{
    if (ctx == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    OPENSSL_free(ctx->ext.ech.alpn_outer);
    ctx->ext.ech.alpn_outer = NULL;
    if (protos == NULL)
        return 1;
    if (protos_len == 0) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
        return 0;
    }
    ctx->ext.ech.alpn_outer = OPENSSL_memdup(protos, protos_len);
    if (ctx->ext.ech.alpn_outer == NULL)
        return 0;
    ctx->ext.ech.alpn_outer_len = protos_len;
    return 1;
}

void SSL_CTX_ech_set_callback(SSL_CTX *ctx, SSL_ech_cb_func f)
{
    if (ctx == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        return;
    }
    ctx->ext.ech.cb = f;
    return;
}

int SSL_set1_ech_config_list(SSL *ssl, const uint8_t *ecl, size_t ecl_len)
{
    int rv = 0;
    SSL_CONNECTION *s;
    OSSL_ECHSTORE *es = NULL;
    BIO *es_in = NULL;

    s = SSL_CONNECTION_FROM_SSL(ssl);
    if (s == NULL) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
        goto err;
    }
    if (ecl == NULL) {
        OSSL_ECHSTORE_free(s->ext.ech.es);
        s->ext.ech.es = NULL;
        return 1;
    }
    if (ecl_len == 0 || ecl_len > INT_MAX) {
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
        goto err;
    }
    if ((es_in = BIO_new_mem_buf(ecl, (int)ecl_len)) == NULL
        || (es = OSSL_ECHSTORE_new(NULL, NULL)) == NULL
        || OSSL_ECHSTORE_read_echconfiglist(es, es_in) != 1
        || SSL_set1_echstore(ssl, es) != 1) {
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    rv = 1;
err:
    OSSL_ECHSTORE_free(es);
    BIO_free_all(es_in);
    return rv;
}