1e1051a39Sopenharmony_ci=pod 2e1051a39Sopenharmony_ci 3e1051a39Sopenharmony_ci=head1 NAME 4e1051a39Sopenharmony_ci 5e1051a39Sopenharmony_ciSSL_extension_supported, 6e1051a39Sopenharmony_ciSSL_custom_ext_add_cb_ex, 7e1051a39Sopenharmony_ciSSL_custom_ext_free_cb_ex, 8e1051a39Sopenharmony_ciSSL_custom_ext_parse_cb_ex, 9e1051a39Sopenharmony_ciSSL_CTX_add_custom_ext, 10e1051a39Sopenharmony_ciSSL_CTX_add_client_custom_ext, SSL_CTX_add_server_custom_ext, 11e1051a39Sopenharmony_cicustom_ext_add_cb, custom_ext_free_cb, custom_ext_parse_cb 12e1051a39Sopenharmony_ci- custom TLS extension handling 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci=head1 SYNOPSIS 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci #include <openssl/ssl.h> 17e1051a39Sopenharmony_ci 18e1051a39Sopenharmony_ci typedef int (*SSL_custom_ext_add_cb_ex)(SSL *s, unsigned int ext_type, 19e1051a39Sopenharmony_ci unsigned int context, 20e1051a39Sopenharmony_ci const unsigned char **out, 21e1051a39Sopenharmony_ci size_t *outlen, X509 *x, 22e1051a39Sopenharmony_ci size_t chainidx, int *al, 23e1051a39Sopenharmony_ci void *add_arg); 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ci typedef void (*SSL_custom_ext_free_cb_ex)(SSL *s, unsigned int ext_type, 26e1051a39Sopenharmony_ci unsigned int context, 27e1051a39Sopenharmony_ci const unsigned char *out, 28e1051a39Sopenharmony_ci void *add_arg); 29e1051a39Sopenharmony_ci 30e1051a39Sopenharmony_ci typedef int (*SSL_custom_ext_parse_cb_ex)(SSL *s, unsigned int ext_type, 31e1051a39Sopenharmony_ci unsigned int context, 32e1051a39Sopenharmony_ci const unsigned char *in, 33e1051a39Sopenharmony_ci size_t inlen, X509 *x, 34e1051a39Sopenharmony_ci size_t chainidx, int *al, 35e1051a39Sopenharmony_ci void *parse_arg); 36e1051a39Sopenharmony_ci 37e1051a39Sopenharmony_ci int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type, 38e1051a39Sopenharmony_ci unsigned int context, 39e1051a39Sopenharmony_ci SSL_custom_ext_add_cb_ex add_cb, 40e1051a39Sopenharmony_ci SSL_custom_ext_free_cb_ex free_cb, 41e1051a39Sopenharmony_ci void *add_arg, 42e1051a39Sopenharmony_ci SSL_custom_ext_parse_cb_ex parse_cb, 43e1051a39Sopenharmony_ci void *parse_arg); 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci typedef int (*custom_ext_add_cb)(SSL *s, unsigned int ext_type, 46e1051a39Sopenharmony_ci const unsigned char **out, 47e1051a39Sopenharmony_ci size_t *outlen, int *al, 48e1051a39Sopenharmony_ci void *add_arg); 49e1051a39Sopenharmony_ci 50e1051a39Sopenharmony_ci typedef void (*custom_ext_free_cb)(SSL *s, unsigned int ext_type, 51e1051a39Sopenharmony_ci const unsigned char *out, 52e1051a39Sopenharmony_ci void *add_arg); 53e1051a39Sopenharmony_ci 54e1051a39Sopenharmony_ci typedef int (*custom_ext_parse_cb)(SSL *s, unsigned int ext_type, 55e1051a39Sopenharmony_ci const unsigned char *in, 56e1051a39Sopenharmony_ci size_t inlen, int *al, 57e1051a39Sopenharmony_ci void *parse_arg); 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type, 60e1051a39Sopenharmony_ci custom_ext_add_cb add_cb, 61e1051a39Sopenharmony_ci custom_ext_free_cb free_cb, void *add_arg, 62e1051a39Sopenharmony_ci custom_ext_parse_cb parse_cb, 63e1051a39Sopenharmony_ci void *parse_arg); 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ci int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type, 66e1051a39Sopenharmony_ci custom_ext_add_cb add_cb, 67e1051a39Sopenharmony_ci custom_ext_free_cb free_cb, void *add_arg, 68e1051a39Sopenharmony_ci custom_ext_parse_cb parse_cb, 69e1051a39Sopenharmony_ci void *parse_arg); 70e1051a39Sopenharmony_ci 71e1051a39Sopenharmony_ci int SSL_extension_supported(unsigned int ext_type); 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_ci=head1 DESCRIPTION 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ciSSL_CTX_add_custom_ext() adds a custom extension for a TLS/DTLS client or server 76e1051a39Sopenharmony_cifor all supported protocol versions with extension type B<ext_type> and 77e1051a39Sopenharmony_cicallbacks B<add_cb>, B<free_cb> and B<parse_cb> (see the 78e1051a39Sopenharmony_ciL</EXTENSION CALLBACKS> section below). The B<context> value determines 79e1051a39Sopenharmony_ciwhich messages and under what conditions the extension will be added/parsed (see 80e1051a39Sopenharmony_cithe L</EXTENSION CONTEXTS> section below). 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ciSSL_CTX_add_client_custom_ext() adds a custom extension for a TLS/DTLS client 83e1051a39Sopenharmony_ciwith extension type B<ext_type> and callbacks B<add_cb>, B<free_cb> and 84e1051a39Sopenharmony_ciB<parse_cb>. This function is similar to SSL_CTX_add_custom_ext() except it only 85e1051a39Sopenharmony_ciapplies to clients, uses the older style of callbacks, and implicitly sets the 86e1051a39Sopenharmony_ciB<context> value to: 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ci SSL_EXT_TLS1_2_AND_BELOW_ONLY | SSL_EXT_CLIENT_HELLO 89e1051a39Sopenharmony_ci | SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_IGNORE_ON_RESUMPTION 90e1051a39Sopenharmony_ci 91e1051a39Sopenharmony_ciSSL_CTX_add_server_custom_ext() adds a custom extension for a TLS/DTLS server 92e1051a39Sopenharmony_ciwith extension type B<ext_type> and callbacks B<add_cb>, B<free_cb> and 93e1051a39Sopenharmony_ciB<parse_cb>. This function is similar to SSL_CTX_add_custom_ext() except it 94e1051a39Sopenharmony_cionly applies to servers, uses the older style of callbacks, and implicitly sets 95e1051a39Sopenharmony_cithe B<context> value to the same as for SSL_CTX_add_client_custom_ext() above. 96e1051a39Sopenharmony_ci 97e1051a39Sopenharmony_ciThe B<ext_type> parameter corresponds to the B<extension_type> field of 98e1051a39Sopenharmony_ciRFC5246 et al. It is B<not> a NID. In all cases the extension type must not be 99e1051a39Sopenharmony_cihandled by OpenSSL internally or an error occurs. 100e1051a39Sopenharmony_ci 101e1051a39Sopenharmony_ciSSL_extension_supported() returns 1 if the extension B<ext_type> is handled 102e1051a39Sopenharmony_ciinternally by OpenSSL and 0 otherwise. 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_ci=head1 EXTENSION CALLBACKS 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ciThe callback B<add_cb> is called to send custom extension data to be 107e1051a39Sopenharmony_ciincluded in various TLS messages. The B<ext_type> parameter is set to the 108e1051a39Sopenharmony_ciextension type which will be added and B<add_arg> to the value set when the 109e1051a39Sopenharmony_ciextension handler was added. When using the new style callbacks the B<context> 110e1051a39Sopenharmony_ciparameter will indicate which message is currently being constructed e.g. for 111e1051a39Sopenharmony_cithe ClientHello it will be set to B<SSL_EXT_CLIENT_HELLO>. 112e1051a39Sopenharmony_ci 113e1051a39Sopenharmony_ciIf the application wishes to include the extension B<ext_type> it should 114e1051a39Sopenharmony_ciset B<*out> to the extension data, set B<*outlen> to the length of the 115e1051a39Sopenharmony_ciextension data and return 1. 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ciIf the B<add_cb> does not wish to include the extension it must return 0. 118e1051a39Sopenharmony_ci 119e1051a39Sopenharmony_ciIf B<add_cb> returns -1 a fatal handshake error occurs using the TLS 120e1051a39Sopenharmony_cialert value specified in B<*al>. 121e1051a39Sopenharmony_ci 122e1051a39Sopenharmony_ciWhen constructing the ClientHello, if B<add_cb> is set to NULL a zero length 123e1051a39Sopenharmony_ciextension is added for B<ext_type>. For all other messages if B<add_cb> is set 124e1051a39Sopenharmony_cito NULL then no extension is added. 125e1051a39Sopenharmony_ci 126e1051a39Sopenharmony_ciWhen constructing a Certificate message the callback will be called for each 127e1051a39Sopenharmony_cicertificate in the message. The B<x> parameter will indicate the 128e1051a39Sopenharmony_cicurrent certificate and the B<chainidx> parameter will indicate the position 129e1051a39Sopenharmony_ciof the certificate in the message. The first certificate is always the end 130e1051a39Sopenharmony_cientity certificate and has a B<chainidx> value of 0. The certificates are in the 131e1051a39Sopenharmony_ciorder that they were received in the Certificate message. 132e1051a39Sopenharmony_ci 133e1051a39Sopenharmony_ciFor all messages except the ServerHello and EncryptedExtensions every 134e1051a39Sopenharmony_ciregistered B<add_cb> is always called to see if the application wishes to add an 135e1051a39Sopenharmony_ciextension (as long as all requirements of the specified B<context> are met). 136e1051a39Sopenharmony_ci 137e1051a39Sopenharmony_ciFor the ServerHello and EncryptedExtension messages every registered B<add_cb> 138e1051a39Sopenharmony_ciis called once if and only if the requirements of the specified B<context> are 139e1051a39Sopenharmony_cimet and the corresponding extension was received in the ClientHello. That is, if 140e1051a39Sopenharmony_cino corresponding extension was received in the ClientHello then B<add_cb> will 141e1051a39Sopenharmony_cinot be called. 142e1051a39Sopenharmony_ci 143e1051a39Sopenharmony_ciIf an extension is added (that is B<add_cb> returns 1) B<free_cb> is called 144e1051a39Sopenharmony_ci(if it is set) with the value of B<out> set by the add callback. It can be 145e1051a39Sopenharmony_ciused to free up any dynamic extension data set by B<add_cb>. Since B<out> is 146e1051a39Sopenharmony_ciconstant (to permit use of constant data in B<add_cb>) applications may need to 147e1051a39Sopenharmony_cicast away const to free the data. 148e1051a39Sopenharmony_ci 149e1051a39Sopenharmony_ciThe callback B<parse_cb> receives data for TLS extensions. The callback is only 150e1051a39Sopenharmony_cicalled if the extension is present and relevant for the context (see 151e1051a39Sopenharmony_ciL</EXTENSION CONTEXTS> below). 152e1051a39Sopenharmony_ci 153e1051a39Sopenharmony_ciThe extension data consists of B<inlen> bytes in the buffer B<in> for the 154e1051a39Sopenharmony_ciextension B<ext_type>. 155e1051a39Sopenharmony_ci 156e1051a39Sopenharmony_ciIf the message being parsed is a TLSv1.3 compatible Certificate message then 157e1051a39Sopenharmony_ciB<parse_cb> will be called for each certificate contained within the message. 158e1051a39Sopenharmony_ciThe B<x> parameter will indicate the current certificate and the B<chainidx> 159e1051a39Sopenharmony_ciparameter will indicate the position of the certificate in the message. The 160e1051a39Sopenharmony_cifirst certificate is always the end entity certificate and has a B<chainidx> 161e1051a39Sopenharmony_civalue of 0. 162e1051a39Sopenharmony_ci 163e1051a39Sopenharmony_ciIf the B<parse_cb> considers the extension data acceptable it must return 164e1051a39Sopenharmony_ci1. If it returns 0 or a negative value a fatal handshake error occurs 165e1051a39Sopenharmony_ciusing the TLS alert value specified in B<*al>. 166e1051a39Sopenharmony_ci 167e1051a39Sopenharmony_ciThe buffer B<in> is a temporary internal buffer which will not be valid after 168e1051a39Sopenharmony_cithe callback returns. 169e1051a39Sopenharmony_ci 170e1051a39Sopenharmony_ci=head1 EXTENSION CONTEXTS 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ciAn extension context defines which messages and under which conditions an 173e1051a39Sopenharmony_ciextension should be added or expected. The context is built up by performing 174e1051a39Sopenharmony_cia bitwise OR of multiple pre-defined values together. The valid context values 175e1051a39Sopenharmony_ciare: 176e1051a39Sopenharmony_ci 177e1051a39Sopenharmony_ci=over 4 178e1051a39Sopenharmony_ci 179e1051a39Sopenharmony_ci=item SSL_EXT_TLS_ONLY 180e1051a39Sopenharmony_ci 181e1051a39Sopenharmony_ciThe extension is only allowed in TLS 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_ci=item SSL_EXT_DTLS_ONLY 184e1051a39Sopenharmony_ci 185e1051a39Sopenharmony_ciThe extension is only allowed in DTLS 186e1051a39Sopenharmony_ci 187e1051a39Sopenharmony_ci=item SSL_EXT_TLS_IMPLEMENTATION_ONLY 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ciThe extension is allowed in DTLS, but there is only a TLS implementation 190e1051a39Sopenharmony_ciavailable (so it is ignored in DTLS). 191e1051a39Sopenharmony_ci 192e1051a39Sopenharmony_ci=item SSL_EXT_SSL3_ALLOWED 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ciExtensions are not typically defined for SSLv3. Setting this value will allow 195e1051a39Sopenharmony_cithe extension in SSLv3. Applications will not typically need to use this. 196e1051a39Sopenharmony_ci 197e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_2_AND_BELOW_ONLY 198e1051a39Sopenharmony_ci 199e1051a39Sopenharmony_ciThe extension is only defined for TLSv1.2/DTLSv1.2 and below. Servers will 200e1051a39Sopenharmony_ciignore this extension if it is present in the ClientHello and TLSv1.3 is 201e1051a39Sopenharmony_cinegotiated. 202e1051a39Sopenharmony_ci 203e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_ONLY 204e1051a39Sopenharmony_ci 205e1051a39Sopenharmony_ciThe extension is only defined for TLS1.3 and above. Servers will ignore this 206e1051a39Sopenharmony_ciextension if it is present in the ClientHello and TLSv1.2 or below is 207e1051a39Sopenharmony_cinegotiated. 208e1051a39Sopenharmony_ci 209e1051a39Sopenharmony_ci=item SSL_EXT_IGNORE_ON_RESUMPTION 210e1051a39Sopenharmony_ci 211e1051a39Sopenharmony_ciThe extension will be ignored during parsing if a previous session is being 212e1051a39Sopenharmony_cisuccessfully resumed. 213e1051a39Sopenharmony_ci 214e1051a39Sopenharmony_ci=item SSL_EXT_CLIENT_HELLO 215e1051a39Sopenharmony_ci 216e1051a39Sopenharmony_ciThe extension may be present in the ClientHello message. 217e1051a39Sopenharmony_ci 218e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_2_SERVER_HELLO 219e1051a39Sopenharmony_ci 220e1051a39Sopenharmony_ciThe extension may be present in a TLSv1.2 or below compatible ServerHello 221e1051a39Sopenharmony_cimessage. 222e1051a39Sopenharmony_ci 223e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_SERVER_HELLO 224e1051a39Sopenharmony_ci 225e1051a39Sopenharmony_ciThe extension may be present in a TLSv1.3 compatible ServerHello message. 226e1051a39Sopenharmony_ci 227e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS 228e1051a39Sopenharmony_ci 229e1051a39Sopenharmony_ciThe extension may be present in an EncryptedExtensions message. 230e1051a39Sopenharmony_ci 231e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ciThe extension may be present in a HelloRetryRequest message. 234e1051a39Sopenharmony_ci 235e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_CERTIFICATE 236e1051a39Sopenharmony_ci 237e1051a39Sopenharmony_ciThe extension may be present in a TLSv1.3 compatible Certificate message. 238e1051a39Sopenharmony_ci 239e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_NEW_SESSION_TICKET 240e1051a39Sopenharmony_ci 241e1051a39Sopenharmony_ciThe extension may be present in a TLSv1.3 compatible NewSessionTicket message. 242e1051a39Sopenharmony_ci 243e1051a39Sopenharmony_ci=item SSL_EXT_TLS1_3_CERTIFICATE_REQUEST 244e1051a39Sopenharmony_ci 245e1051a39Sopenharmony_ciThe extension may be present in a TLSv1.3 compatible CertificateRequest message. 246e1051a39Sopenharmony_ci 247e1051a39Sopenharmony_ci=back 248e1051a39Sopenharmony_ci 249e1051a39Sopenharmony_ciThe context must include at least one message value (otherwise the extension 250e1051a39Sopenharmony_ciwill never be used). 251e1051a39Sopenharmony_ci 252e1051a39Sopenharmony_ci=head1 NOTES 253e1051a39Sopenharmony_ci 254e1051a39Sopenharmony_ciThe B<add_arg> and B<parse_arg> parameters can be set to arbitrary values 255e1051a39Sopenharmony_ciwhich will be passed to the corresponding callbacks. They can, for example, 256e1051a39Sopenharmony_cibe used to store the extension data received in a convenient structure or 257e1051a39Sopenharmony_cipass the extension data to be added or freed when adding extensions. 258e1051a39Sopenharmony_ci 259e1051a39Sopenharmony_ciIf the same custom extension type is received multiple times a fatal 260e1051a39Sopenharmony_ciB<decode_error> alert is sent and the handshake aborts. If a custom extension 261e1051a39Sopenharmony_ciis received in a ServerHello/EncryptedExtensions message which was not sent in 262e1051a39Sopenharmony_cithe ClientHello a fatal B<unsupported_extension> alert is sent and the 263e1051a39Sopenharmony_cihandshake is aborted. The ServerHello/EncryptedExtensions B<add_cb> callback is 264e1051a39Sopenharmony_cionly called if the corresponding extension was received in the ClientHello. This 265e1051a39Sopenharmony_ciis compliant with the TLS specifications. This behaviour ensures that each 266e1051a39Sopenharmony_cicallback is called at most once and that an application can never send 267e1051a39Sopenharmony_ciunsolicited extensions. 268e1051a39Sopenharmony_ci 269e1051a39Sopenharmony_ci=head1 RETURN VALUES 270e1051a39Sopenharmony_ci 271e1051a39Sopenharmony_ciSSL_CTX_add_custom_ext(), SSL_CTX_add_client_custom_ext() and 272e1051a39Sopenharmony_ciSSL_CTX_add_server_custom_ext() return 1 for success and 0 for failure. A 273e1051a39Sopenharmony_cifailure can occur if an attempt is made to add the same B<ext_type> more than 274e1051a39Sopenharmony_cionce, if an attempt is made to use an extension type handled internally by 275e1051a39Sopenharmony_ciOpenSSL or if an internal error occurs (for example a memory allocation 276e1051a39Sopenharmony_cifailure). 277e1051a39Sopenharmony_ci 278e1051a39Sopenharmony_ciSSL_extension_supported() returns 1 if the extension B<ext_type> is handled 279e1051a39Sopenharmony_ciinternally by OpenSSL and 0 otherwise. 280e1051a39Sopenharmony_ci 281e1051a39Sopenharmony_ci=head1 SEE ALSO 282e1051a39Sopenharmony_ci 283e1051a39Sopenharmony_ciL<ssl(7)> 284e1051a39Sopenharmony_ci 285e1051a39Sopenharmony_ci=head1 HISTORY 286e1051a39Sopenharmony_ci 287e1051a39Sopenharmony_ciThe SSL_CTX_add_custom_ext() function was added in OpenSSL 1.1.1. 288e1051a39Sopenharmony_ci 289e1051a39Sopenharmony_ci=head1 COPYRIGHT 290e1051a39Sopenharmony_ci 291e1051a39Sopenharmony_ciCopyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved. 292e1051a39Sopenharmony_ci 293e1051a39Sopenharmony_ciLicensed under the Apache License 2.0 (the "License"). You may not use 294e1051a39Sopenharmony_cithis file except in compliance with the License. You can obtain a copy 295e1051a39Sopenharmony_ciin the file LICENSE in the source distribution or at 296e1051a39Sopenharmony_ciL<https://www.openssl.org/source/license.html>. 297e1051a39Sopenharmony_ci 298e1051a39Sopenharmony_ci=cut 299