1/*
2 * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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/*
11 * Externally-visible data structures and prototypes for handling
12 * shared-mode Encrypted ClientHello (ECH).
13 */
14#ifndef OPENSSL_ECH_H
15#define OPENSSL_ECH_H
16#pragma once
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#include <openssl/ssl.h>
23#include <openssl/hpke.h>
24
25#ifndef OPENSSL_NO_ECH
26
27/*
28 * Some externally visible limits - most used for sanity checks that could be
29 * bigger if needed, but that work for now
30 */
31#define OSSL_ECH_MAX_PAYLOAD_LEN 1500 /* max ECH ciphertext to en/decode */
32#define OSSL_ECH_MIN_ECHCONFIG_LEN 32 /* min for all encodings */
33#define OSSL_ECH_MAX_ECHCONFIG_LEN 1500 /* max for all encodings */
34#define OSSL_ECH_MAX_ECHCONFIGEXT_LEN 512 /* ECHConfig extension max */
35#define OSSL_ECH_MAX_MAXNAMELEN 255 /* ECHConfig max for max name length */
36#define OSSL_ECH_MAX_PUBLICNAME 255 /* max ECHConfig public name length */
37#define OSSL_ECH_MAX_ALPNLEN 255 /* max alpn length */
38#define OSSL_ECH_OUTERS_MAX 20 /* max extensions we compress via outer-exts */
39#define OSSL_ECH_ALLEXTS_MAX 32 /* max total number of extension we allow */
40
41/*
42 * ECH version. We only support RFC 9849 as of now. As/if new ECHConfig
43 * versions are added, those will be noted here.
44 */
45#define OSSL_ECH_RFC9849_VERSION 0xfe0d /* official ECHConfig version */
46/* latest version from an RFC */
47#define OSSL_ECH_CURRENT_VERSION OSSL_ECH_RFC9849_VERSION
48
49/* Return codes from SSL_ech_get1_status */
50#define SSL_ECH_STATUS_BACKEND 4 /* ECH backend: saw an ech_is_inner */
51#define SSL_ECH_STATUS_GREASE_ECH 3 /* GREASEd and got an ECH in return */
52#define SSL_ECH_STATUS_GREASE 2 /* ECH GREASE happened */
53#define SSL_ECH_STATUS_SUCCESS 1 /* Success */
54#define SSL_ECH_STATUS_FAILED 0 /* Some internal or protocol error */
55#define SSL_ECH_STATUS_BAD_CALL -100 /* Some in/out arguments were NULL */
56#define SSL_ECH_STATUS_NOT_TRIED -101 /* ECH wasn't attempted */
57#define SSL_ECH_STATUS_BAD_NAME -102 /* ECH ok but server cert bad */
58#define SSL_ECH_STATUS_NOT_CONFIGURED -103 /* ECH wasn't configured */
59#define SSL_ECH_STATUS_FAILED_ECH -105 /* Tried, failed, got an ECH, from a good name */
60#define SSL_ECH_STATUS_FAILED_ECH_BAD_NAME -106 /* Tried, failed, got an ECH, from a bad name */
61
62/* if a caller wants to index the last entry in the store */
63#define OSSL_ECHSTORE_LAST -1
64/* if a caller wants all entries in the store, e.g. to print public values */
65#define OSSL_ECHSTORE_ALL -2
66
67/* Values for the for_retry inputs */
68#define OSSL_ECH_FOR_RETRY 1
69#define OSSL_ECH_NO_RETRY 0
70
71/*
72 * API calls built around OSSL_ECHSTORE
73 */
74OSSL_ECHSTORE *OSSL_ECHSTORE_new(OSSL_LIB_CTX *libctx, const char *propq);
75void OSSL_ECHSTORE_free(OSSL_ECHSTORE *es);
76int OSSL_ECHSTORE_new_config(OSSL_ECHSTORE *es,
77 uint16_t echversion, uint8_t max_name_length,
78 const char *public_name, OSSL_HPKE_SUITE suite);
79int OSSL_ECHSTORE_write_pem(OSSL_ECHSTORE *es, int index, BIO *out);
80int OSSL_ECHSTORE_read_echconfiglist(OSSL_ECHSTORE *es, BIO *in);
81int OSSL_ECHSTORE_get1_info(OSSL_ECHSTORE *es, int index, time_t *loaded_secs,
82 char **public_name, char **echconfig,
83 int *has_private, int *for_retry);
84int OSSL_ECHSTORE_downselect(OSSL_ECHSTORE *es, int index);
85int OSSL_ECHSTORE_set1_key_and_read_pem(OSSL_ECHSTORE *es, EVP_PKEY *priv,
86 BIO *in, int for_retry);
87int OSSL_ECHSTORE_read_pem(OSSL_ECHSTORE *es, BIO *in, int for_retry);
88int OSSL_ECHSTORE_num_entries(const OSSL_ECHSTORE *es, int *numentries);
89int OSSL_ECHSTORE_num_keys(OSSL_ECHSTORE *es, int *numkeys);
90int OSSL_ECHSTORE_flush_keys(OSSL_ECHSTORE *es, time_t age);
91
92/*
93 * APIs relating OSSL_ECHSTORE to SSL/SSL_CTX
94 */
95int SSL_CTX_set1_echstore(SSL_CTX *ctx, OSSL_ECHSTORE *es);
96int SSL_set1_echstore(SSL *s, OSSL_ECHSTORE *es);
97
98OSSL_ECHSTORE *SSL_CTX_get1_echstore(const SSL_CTX *ctx);
99OSSL_ECHSTORE *SSL_get1_echstore(const SSL *s);
100
101int SSL_ech_set1_server_names(SSL *s, const char *inner_name,
102 const char *outer_name, int no_outer);
103int SSL_ech_set1_outer_server_name(SSL *s, const char *outer_name, int no_outer);
104/*
105 * Note that this function returns 1 for success and 0 for error. This
106 * contrasts with SSL_set1_alpn_protos() which (unusually for OpenSSL)
107 * returns 0 for success and 1 on error.
108 */
109int SSL_ech_set1_outer_alpn_protos(SSL *s, const unsigned char *protos,
110 const size_t protos_len);
111
112int SSL_ech_get1_status(SSL *s, char **inner_sni, char **outer_sni);
113int SSL_ech_set1_grease_suite(SSL *s, const char *suite);
114int SSL_ech_set_grease_type(SSL *s, uint16_t type);
115typedef unsigned int (*SSL_ech_cb_func)(SSL *s, const char *str);
116void SSL_ech_set_callback(SSL *s, SSL_ech_cb_func f);
117int SSL_ech_get1_retry_config(SSL *s, unsigned char **ec, size_t *eclen);
118
119/*
120 * Note that this function returns 1 for success and 0 for error. This
121 * contrasts with SSL_set1_alpn_protos() which (unusually for OpenSSL)
122 * returns 0 for success and 1 on error.
123 */
124int SSL_CTX_ech_set1_outer_alpn_protos(SSL_CTX *s, const unsigned char *protos,
125 const size_t protos_len);
126void SSL_CTX_ech_set_callback(SSL_CTX *ctx, SSL_ech_cb_func f);
127int SSL_set1_ech_config_list(SSL *ssl, const uint8_t *ecl, size_t ecl_len);
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif
134#endif
135