Path: blob/main/crypto/openssl/ssl/statem/extensions_cust.c
48266 views
/*1* Copyright 2014-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* Custom extension utility functions */1011#include <openssl/ct.h>12#include "../ssl_local.h"13#include "internal/cryptlib.h"14#include "internal/ssl_unwrap.h"15#include "statem_local.h"1617typedef struct {18void *add_arg;19custom_ext_add_cb add_cb;20custom_ext_free_cb free_cb;21} custom_ext_add_cb_wrap;2223typedef struct {24void *parse_arg;25custom_ext_parse_cb parse_cb;26} custom_ext_parse_cb_wrap;2728/*29* Provide thin wrapper callbacks which convert new style arguments to old style30*/31static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,32unsigned int context,33const unsigned char **out,34size_t *outlen, X509 *x, size_t chainidx,35int *al, void *add_arg)36{37custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;3839if (add_cb_wrap->add_cb == NULL)40return 1;4142return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,43add_cb_wrap->add_arg);44}4546static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,47unsigned int context,48const unsigned char *out, void *add_arg)49{50custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;5152if (add_cb_wrap->free_cb == NULL)53return;5455add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);56}5758static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,59unsigned int context,60const unsigned char *in,61size_t inlen, X509 *x, size_t chainidx,62int *al, void *parse_arg)63{64custom_ext_parse_cb_wrap *parse_cb_wrap =65(custom_ext_parse_cb_wrap *)parse_arg;6667if (parse_cb_wrap->parse_cb == NULL)68return 1;6970return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,71parse_cb_wrap->parse_arg);72}7374/*75* Find a custom extension from the list. The |role| param is there to76* support the legacy API where custom extensions for client and server could77* be set independently on the same SSL_CTX. It is set to ENDPOINT_SERVER if we78* are trying to find a method relevant to the server, ENDPOINT_CLIENT for the79* client, or ENDPOINT_BOTH for either80*/81custom_ext_method *custom_ext_find(const custom_ext_methods *exts,82ENDPOINT role, unsigned int ext_type,83size_t *idx)84{85size_t i;86custom_ext_method *meth = exts->meths;8788for (i = 0; i < exts->meths_count; i++, meth++) {89if (ext_type == meth->ext_type90&& (role == ENDPOINT_BOTH || role == meth->role91|| meth->role == ENDPOINT_BOTH)) {92if (idx != NULL)93*idx = i;94return meth;95}96}97return NULL;98}99100/*101* Initialise custom extensions flags to indicate neither sent nor received.102*/103void custom_ext_init(custom_ext_methods *exts)104{105size_t i;106custom_ext_method *meth = exts->meths;107108for (i = 0; i < exts->meths_count; i++, meth++)109meth->ext_flags &= ~(SSL_EXT_FLAG_SENT | SSL_EXT_FLAG_RECEIVED);110}111112/* Pass received custom extension data to the application for parsing. */113int custom_ext_parse(SSL_CONNECTION *s, unsigned int context,114unsigned int ext_type,115const unsigned char *ext_data, size_t ext_size, X509 *x,116size_t chainidx)117{118int al = 0;119custom_ext_methods *exts = &s->cert->custext;120custom_ext_method *meth;121ENDPOINT role = ENDPOINT_BOTH;122123if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)124role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;125126meth = custom_ext_find(exts, role, ext_type, NULL);127/* If not found return success */128if (!meth)129return 1;130131/* Check if extension is defined for our protocol. If not, skip */132if (!extension_is_relevant(s, meth->context, context))133return 1;134135if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO136| SSL_EXT_TLS1_3_SERVER_HELLO137| SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {138/*139* If it's ServerHello or EncryptedExtensions we can't have any140* extensions not sent in ClientHello.141*/142if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {143SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);144return 0;145}146}147148/*149* Extensions received in the ClientHello or CertificateRequest are marked150* with the SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent151* extensions in the response messages152*/153if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST))154!= 0)155meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;156157/* If no parse function set return success */158if (meth->parse_cb == NULL)159return 1;160161if (meth->parse_cb(SSL_CONNECTION_GET_USER_SSL(s), ext_type, context, ext_data,162ext_size, x, chainidx, &al, meth->parse_arg) <= 0) {163SSLfatal(s, al, SSL_R_BAD_EXTENSION);164return 0;165}166167return 1;168}169170/*171* Request custom extension data from the application and add to the return172* buffer.173*/174int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,175size_t chainidx, int maxversion)176{177custom_ext_methods *exts = &s->cert->custext;178custom_ext_method *meth;179size_t i;180int al;181int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;182183for (i = 0; i < exts->meths_count; i++) {184const unsigned char *out = NULL;185size_t outlen = 0;186187meth = exts->meths + i;188189if (!should_add_extension(s, meth->context, context, maxversion))190continue;191192if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO193| SSL_EXT_TLS1_3_SERVER_HELLO194| SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS195| SSL_EXT_TLS1_3_CERTIFICATE196| SSL_EXT_TLS1_3_RAW_PUBLIC_KEY197| SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) {198/* Only send extensions present in ClientHello/CertificateRequest */199if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))200continue;201}202/*203* We skip it if the callback is absent - except for a ClientHello where204* we add an empty extension.205*/206if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)207continue;208209if (meth->add_cb != NULL) {210int cb_retval = meth->add_cb(SSL_CONNECTION_GET_USER_SSL(s),211meth->ext_type, context, &out,212&outlen, x, chainidx, &al,213meth->add_arg);214215if (cb_retval < 0) {216if (!for_comp)217SSLfatal(s, al, SSL_R_CALLBACK_FAILED);218return 0; /* error */219}220if (cb_retval == 0)221continue; /* skip this extension */222}223224if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)225|| !WPACKET_start_sub_packet_u16(pkt)226|| (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))227|| !WPACKET_close(pkt)) {228if (meth->free_cb != NULL)229meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type,230context, out, meth->add_arg);231if (!for_comp)232SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);233return 0;234}235if ((context & SSL_EXT_CLIENT_HELLO) != 0) {236/*237* We can't send duplicates: code logic should prevent this.238*/239if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {240if (meth->free_cb != NULL)241meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type,242context, out, meth->add_arg);243if (!for_comp)244SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);245return 0;246}247/*248* Indicate extension has been sent: this is both a sanity check to249* ensure we don't send duplicate extensions and indicates that it250* is not an error if the extension is present in ServerHello.251*/252meth->ext_flags |= SSL_EXT_FLAG_SENT;253}254if (meth->free_cb != NULL)255meth->free_cb(SSL_CONNECTION_GET_USER_SSL(s), meth->ext_type,256context, out, meth->add_arg);257}258return 1;259}260261/* Copy the flags from src to dst for any extensions that exist in both */262int custom_exts_copy_flags(custom_ext_methods *dst,263const custom_ext_methods *src)264{265size_t i;266custom_ext_method *methsrc = src->meths;267268for (i = 0; i < src->meths_count; i++, methsrc++) {269custom_ext_method *methdst = custom_ext_find(dst, methsrc->role,270methsrc->ext_type, NULL);271272if (methdst == NULL)273continue;274275methdst->ext_flags = methsrc->ext_flags;276}277278return 1;279}280281/* Copy old style API wrapper arguments */282static void custom_ext_copy_old_cb(custom_ext_method *methdst,283const custom_ext_method *methsrc,284int *err)285{286if (methsrc->add_cb != custom_ext_add_old_cb_wrap)287return;288289if (*err) {290methdst->add_arg = NULL;291methdst->parse_arg = NULL;292return;293}294295methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,296sizeof(custom_ext_add_cb_wrap));297methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,298sizeof(custom_ext_parse_cb_wrap));299300if (methdst->add_arg == NULL || methdst->parse_arg == NULL)301*err = 1;302303return;304}305306/* Copy table of custom extensions */307int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)308{309size_t i;310int err = 0;311312if (src->meths_count > 0) {313dst->meths =314OPENSSL_memdup(src->meths,315sizeof(*src->meths) * src->meths_count);316if (dst->meths == NULL)317return 0;318dst->meths_count = src->meths_count;319320for (i = 0; i < src->meths_count; i++)321custom_ext_copy_old_cb(&dst->meths[i], &src->meths[i], &err);322}323324if (err) {325custom_exts_free(dst);326return 0;327}328329return 1;330}331332/* Copy custom extensions that were set on connection */333int custom_exts_copy_conn(custom_ext_methods *dst,334const custom_ext_methods *src)335{336size_t i;337int err = 0;338339if (src->meths_count > 0) {340size_t meths_count = 0;341342for (i = 0; i < src->meths_count; i++)343if ((src->meths[i].ext_flags & SSL_EXT_FLAG_CONN) != 0)344meths_count++;345346if (meths_count > 0) {347custom_ext_method *methdst =348OPENSSL_realloc(dst->meths,349(dst->meths_count + meths_count) *350sizeof(custom_ext_method));351352if (methdst == NULL)353return 0;354355for (i = 0; i < dst->meths_count; i++)356custom_ext_copy_old_cb(&methdst[i], &dst->meths[i], &err);357358dst->meths = methdst;359methdst += dst->meths_count;360361for (i = 0; i < src->meths_count; i++) {362custom_ext_method *methsrc = &src->meths[i];363364if ((methsrc->ext_flags & SSL_EXT_FLAG_CONN) == 0)365continue;366367memcpy(methdst, methsrc, sizeof(custom_ext_method));368custom_ext_copy_old_cb(methdst, methsrc, &err);369methdst++;370}371372dst->meths_count += meths_count;373}374}375376if (err) {377custom_exts_free(dst);378return 0;379}380381return 1;382}383384void custom_exts_free(custom_ext_methods *exts)385{386size_t i;387custom_ext_method *meth;388389for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {390if (meth->add_cb != custom_ext_add_old_cb_wrap)391continue;392393/* Old style API wrapper. Need to free the arguments too */394OPENSSL_free(meth->add_arg);395OPENSSL_free(meth->parse_arg);396}397OPENSSL_free(exts->meths);398exts->meths = NULL;399exts->meths_count = 0;400}401402/* Return true if a client custom extension exists, false otherwise */403int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)404{405return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,406NULL) != NULL;407}408409int ossl_tls_add_custom_ext_intern(SSL_CTX *ctx, custom_ext_methods *exts,410ENDPOINT role, unsigned int ext_type,411unsigned int context,412SSL_custom_ext_add_cb_ex add_cb,413SSL_custom_ext_free_cb_ex free_cb,414void *add_arg,415SSL_custom_ext_parse_cb_ex parse_cb,416void *parse_arg)417{418custom_ext_method *meth, *tmp;419420/*421* Check application error: if add_cb is not set free_cb will never be422* called.423*/424if (add_cb == NULL && free_cb != NULL)425return 0;426427if (exts == NULL)428exts = &ctx->cert->custext;429430#ifndef OPENSSL_NO_CT431/*432* We don't want applications registering callbacks for SCT extensions433* whilst simultaneously using the built-in SCT validation features, as434* these two things may not play well together.435*/436if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp437&& (context & SSL_EXT_CLIENT_HELLO) != 0438&& ctx != NULL439&& SSL_CTX_ct_is_enabled(ctx))440return 0;441#endif442443/*444* Don't add if extension supported internally, but make exception445* for extension types that previously were not supported, but now are.446*/447if (SSL_extension_supported(ext_type)448&& ext_type != TLSEXT_TYPE_signed_certificate_timestamp)449return 0;450451/* Extension type must fit in 16 bits */452if (ext_type > 0xffff)453return 0;454/* Search for duplicate */455if (custom_ext_find(exts, role, ext_type, NULL))456return 0;457tmp = OPENSSL_realloc(exts->meths,458(exts->meths_count + 1) * sizeof(custom_ext_method));459if (tmp == NULL)460return 0;461462exts->meths = tmp;463meth = exts->meths + exts->meths_count;464memset(meth, 0, sizeof(*meth));465meth->role = role;466meth->context = context;467meth->parse_cb = parse_cb;468meth->add_cb = add_cb;469meth->free_cb = free_cb;470meth->ext_type = ext_type;471meth->ext_flags = (ctx == NULL) ? SSL_EXT_FLAG_CONN : 0;472meth->add_arg = add_arg;473meth->parse_arg = parse_arg;474exts->meths_count++;475return 1;476}477478static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,479unsigned int ext_type,480unsigned int context,481custom_ext_add_cb add_cb,482custom_ext_free_cb free_cb,483void *add_arg,484custom_ext_parse_cb parse_cb, void *parse_arg)485{486custom_ext_add_cb_wrap *add_cb_wrap487= OPENSSL_malloc(sizeof(*add_cb_wrap));488custom_ext_parse_cb_wrap *parse_cb_wrap489= OPENSSL_malloc(sizeof(*parse_cb_wrap));490int ret;491492if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {493OPENSSL_free(add_cb_wrap);494OPENSSL_free(parse_cb_wrap);495return 0;496}497498add_cb_wrap->add_arg = add_arg;499add_cb_wrap->add_cb = add_cb;500add_cb_wrap->free_cb = free_cb;501parse_cb_wrap->parse_arg = parse_arg;502parse_cb_wrap->parse_cb = parse_cb;503504ret = ossl_tls_add_custom_ext_intern(ctx, NULL, role, ext_type,505context,506custom_ext_add_old_cb_wrap,507custom_ext_free_old_cb_wrap,508add_cb_wrap,509custom_ext_parse_old_cb_wrap,510parse_cb_wrap);511512if (!ret) {513OPENSSL_free(add_cb_wrap);514OPENSSL_free(parse_cb_wrap);515}516517return ret;518}519520/* Application level functions to add the old custom extension callbacks */521int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,522custom_ext_add_cb add_cb,523custom_ext_free_cb free_cb,524void *add_arg,525custom_ext_parse_cb parse_cb, void *parse_arg)526{527return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,528SSL_EXT_TLS1_2_AND_BELOW_ONLY529| SSL_EXT_CLIENT_HELLO530| SSL_EXT_TLS1_2_SERVER_HELLO531| SSL_EXT_IGNORE_ON_RESUMPTION,532add_cb, free_cb, add_arg, parse_cb, parse_arg);533}534535int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,536custom_ext_add_cb add_cb,537custom_ext_free_cb free_cb,538void *add_arg,539custom_ext_parse_cb parse_cb, void *parse_arg)540{541return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,542SSL_EXT_TLS1_2_AND_BELOW_ONLY543| SSL_EXT_CLIENT_HELLO544| SSL_EXT_TLS1_2_SERVER_HELLO545| SSL_EXT_IGNORE_ON_RESUMPTION,546add_cb, free_cb, add_arg, parse_cb, parse_arg);547}548549int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,550unsigned int context,551SSL_custom_ext_add_cb_ex add_cb,552SSL_custom_ext_free_cb_ex free_cb,553void *add_arg,554SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)555{556return ossl_tls_add_custom_ext_intern(ctx, NULL, ENDPOINT_BOTH, ext_type,557context, add_cb, free_cb, add_arg,558parse_cb, parse_arg);559}560561int SSL_extension_supported(unsigned int ext_type)562{563switch (ext_type) {564/* Internally supported extensions. */565case TLSEXT_TYPE_application_layer_protocol_negotiation:566case TLSEXT_TYPE_ec_point_formats:567case TLSEXT_TYPE_supported_groups:568case TLSEXT_TYPE_key_share:569#ifndef OPENSSL_NO_NEXTPROTONEG570case TLSEXT_TYPE_next_proto_neg:571#endif572case TLSEXT_TYPE_padding:573case TLSEXT_TYPE_renegotiate:574case TLSEXT_TYPE_max_fragment_length:575case TLSEXT_TYPE_server_name:576case TLSEXT_TYPE_session_ticket:577case TLSEXT_TYPE_signature_algorithms:578#ifndef OPENSSL_NO_SRP579case TLSEXT_TYPE_srp:580#endif581#ifndef OPENSSL_NO_OCSP582case TLSEXT_TYPE_status_request:583#endif584#ifndef OPENSSL_NO_CT585case TLSEXT_TYPE_signed_certificate_timestamp:586#endif587#ifndef OPENSSL_NO_SRTP588case TLSEXT_TYPE_use_srtp:589#endif590case TLSEXT_TYPE_encrypt_then_mac:591case TLSEXT_TYPE_supported_versions:592case TLSEXT_TYPE_extended_master_secret:593case TLSEXT_TYPE_psk_kex_modes:594case TLSEXT_TYPE_cookie:595case TLSEXT_TYPE_early_data:596case TLSEXT_TYPE_certificate_authorities:597case TLSEXT_TYPE_psk:598case TLSEXT_TYPE_post_handshake_auth:599case TLSEXT_TYPE_compress_certificate:600case TLSEXT_TYPE_client_cert_type:601case TLSEXT_TYPE_server_cert_type:602return 1;603default:604return 0;605}606}607608609