SSL_extension_supported.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. =pod
  2. =head1 NAME
  3. SSL_extension_supported,
  4. SSL_custom_ext_add_cb_ex,
  5. SSL_custom_ext_free_cb_ex,
  6. SSL_custom_ext_parse_cb_ex,
  7. SSL_CTX_add_custom_ext,
  8. SSL_CTX_add_client_custom_ext, SSL_CTX_add_server_custom_ext,
  9. custom_ext_add_cb, custom_ext_free_cb, custom_ext_parse_cb
  10. - custom TLS extension handling
  11. =head1 SYNOPSIS
  12. #include <openssl/ssl.h>
  13. typedef int (*SSL_custom_ext_add_cb_ex)(SSL *s, unsigned int ext_type,
  14. unsigned int context,
  15. const unsigned char **out,
  16. size_t *outlen, X509 *x,
  17. size_t chainidx, int *al,
  18. void *add_arg);
  19. typedef void (*SSL_custom_ext_free_cb_ex)(SSL *s, unsigned int ext_type,
  20. unsigned int context,
  21. const unsigned char *out,
  22. void *add_arg);
  23. typedef int (*SSL_custom_ext_parse_cb_ex)(SSL *s, unsigned int ext_type,
  24. unsigned int context,
  25. const unsigned char *in,
  26. size_t inlen, X509 *x,
  27. size_t chainidx, int *al,
  28. void *parse_arg);
  29. int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  30. unsigned int context,
  31. SSL_custom_ext_add_cb_ex add_cb,
  32. SSL_custom_ext_free_cb_ex free_cb,
  33. void *add_arg,
  34. SSL_custom_ext_parse_cb_ex parse_cb,
  35. void *parse_arg);
  36. typedef int (*custom_ext_add_cb)(SSL *s, unsigned int ext_type,
  37. const unsigned char **out,
  38. size_t *outlen, int *al,
  39. void *add_arg);
  40. typedef void (*custom_ext_free_cb)(SSL *s, unsigned int ext_type,
  41. const unsigned char *out,
  42. void *add_arg);
  43. typedef int (*custom_ext_parse_cb)(SSL *s, unsigned int ext_type,
  44. const unsigned char *in,
  45. size_t inlen, int *al,
  46. void *parse_arg);
  47. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  48. custom_ext_add_cb add_cb,
  49. custom_ext_free_cb free_cb, void *add_arg,
  50. custom_ext_parse_cb parse_cb,
  51. void *parse_arg);
  52. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  53. custom_ext_add_cb add_cb,
  54. custom_ext_free_cb free_cb, void *add_arg,
  55. custom_ext_parse_cb parse_cb,
  56. void *parse_arg);
  57. int SSL_extension_supported(unsigned int ext_type);
  58. =head1 DESCRIPTION
  59. SSL_CTX_add_custom_ext() adds a custom extension for a TLS/DTLS client or server
  60. for all supported protocol versions with extension type B<ext_type> and
  61. callbacks B<add_cb>, B<free_cb> and B<parse_cb> (see the
  62. L</EXTENSION CALLBACKS> section below). The B<context> value determines
  63. which messages and under what conditions the extension will be added/parsed (see
  64. the L</EXTENSION CONTEXTS> section below).
  65. SSL_CTX_add_client_custom_ext() adds a custom extension for a TLS/DTLS client
  66. with extension type B<ext_type> and callbacks B<add_cb>, B<free_cb> and
  67. B<parse_cb>. This function is similar to SSL_CTX_add_custom_ext() except it only
  68. applies to clients, uses the older style of callbacks, and implicitly sets the
  69. B<context> value to:
  70. SSL_EXT_TLS1_2_AND_BELOW_ONLY | SSL_EXT_CLIENT_HELLO
  71. | SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_IGNORE_ON_RESUMPTION
  72. SSL_CTX_add_server_custom_ext() adds a custom extension for a TLS/DTLS server
  73. with extension type B<ext_type> and callbacks B<add_cb>, B<free_cb> and
  74. B<parse_cb>. This function is similar to SSL_CTX_add_custom_ext() except it
  75. only applies to servers, uses the older style of callbacks, and implicitly sets
  76. the B<context> value to the same as for SSL_CTX_add_client_custom_ext() above.
  77. The B<ext_type> parameter corresponds to the B<extension_type> field of
  78. RFC5246 et al. It is B<not> a NID. In all cases the extension type must not be
  79. handled by OpenSSL internally or an error occurs.
  80. SSL_extension_supported() returns 1 if the extension B<ext_type> is handled
  81. internally by OpenSSL and 0 otherwise.
  82. =head1 EXTENSION CALLBACKS
  83. The callback B<add_cb> is called to send custom extension data to be
  84. included in various TLS messages. The B<ext_type> parameter is set to the
  85. extension type which will be added and B<add_arg> to the value set when the
  86. extension handler was added. When using the new style callbacks the B<context>
  87. parameter will indicate which message is currently being constructed e.g. for
  88. the ClientHello it will be set to B<SSL_EXT_CLIENT_HELLO>.
  89. If the application wishes to include the extension B<ext_type> it should
  90. set B<*out> to the extension data, set B<*outlen> to the length of the
  91. extension data and return 1.
  92. If the B<add_cb> does not wish to include the extension it must return 0.
  93. If B<add_cb> returns -1 a fatal handshake error occurs using the TLS
  94. alert value specified in B<*al>.
  95. When constructing the ClientHello, if B<add_cb> is set to NULL a zero length
  96. extension is added for B<ext_type>. For all other messages if B<add_cb> is set
  97. to NULL then no extension is added.
  98. When constructing a Certificate message the callback will be called for each
  99. certificate in the message. The B<x> parameter will indicate the
  100. current certificate and the B<chainidx> parameter will indicate the position
  101. of the certificate in the message. The first certificate is always the end
  102. entity certificate and has a B<chainidx> value of 0. The certificates are in the
  103. order that they were received in the Certificate message.
  104. For all messages except the ServerHello and EncryptedExtensions every
  105. registered B<add_cb> is always called to see if the application wishes to add an
  106. extension (as long as all requirements of the specified B<context> are met).
  107. For the ServerHello and EncryptedExtension messages every registered B<add_cb>
  108. is called once if and only if the requirements of the specified B<context> are
  109. met and the corresponding extension was received in the ClientHello. That is, if
  110. no corresponding extension was received in the ClientHello then B<add_cb> will
  111. not be called.
  112. If an extension is added (that is B<add_cb> returns 1) B<free_cb> is called
  113. (if it is set) with the value of B<out> set by the add callback. It can be
  114. used to free up any dynamic extension data set by B<add_cb>. Since B<out> is
  115. constant (to permit use of constant data in B<add_cb>) applications may need to
  116. cast away const to free the data.
  117. The callback B<parse_cb> receives data for TLS extensions. The callback is only
  118. called if the extension is present and relevant for the context (see
  119. L</EXTENSION CONTEXTS> below).
  120. The extension data consists of B<inlen> bytes in the buffer B<in> for the
  121. extension B<ext_type>.
  122. If the message being parsed is a TLSv1.3 compatible Certificate message then
  123. B<parse_cb> will be called for each certificate contained within the message.
  124. The B<x> parameter will indicate the current certificate and the B<chainidx>
  125. parameter will indicate the position of the certificate in the message. The
  126. first certificate is always the end entity certificate and has a B<chainidx>
  127. value of 0.
  128. If the B<parse_cb> considers the extension data acceptable it must return
  129. 1. If it returns 0 or a negative value a fatal handshake error occurs
  130. using the TLS alert value specified in B<*al>.
  131. The buffer B<in> is a temporary internal buffer which will not be valid after
  132. the callback returns.
  133. =head1 EXTENSION CONTEXTS
  134. An extension context defines which messages and under which conditions an
  135. extension should be added or expected. The context is built up by performing
  136. a bitwise OR of multiple pre-defined values together. The valid context values
  137. are:
  138. =over 4
  139. =item SSL_EXT_TLS_ONLY
  140. The extension is only allowed in TLS
  141. =item SSL_EXT_DTLS_ONLY
  142. The extension is only allowed in DTLS
  143. =item SSL_EXT_TLS_IMPLEMENTATION_ONLY
  144. The extension is allowed in DTLS, but there is only a TLS implementation
  145. available (so it is ignored in DTLS).
  146. =item SSL_EXT_SSL3_ALLOWED
  147. Extensions are not typically defined for SSLv3. Setting this value will allow
  148. the extension in SSLv3. Applications will not typically need to use this.
  149. =item SSL_EXT_TLS1_2_AND_BELOW_ONLY
  150. The extension is only defined for TLSv1.2/DTLSv1.2 and below. Servers will
  151. ignore this extension if it is present in the ClientHello and TLSv1.3 is
  152. negotiated.
  153. =item SSL_EXT_TLS1_3_ONLY
  154. The extension is only defined for TLS1.3 and above. Servers will ignore this
  155. extension if it is present in the ClientHello and TLSv1.2 or below is
  156. negotiated.
  157. =item SSL_EXT_IGNORE_ON_RESUMPTION
  158. The extension will be ignored during parsing if a previous session is being
  159. successfully resumed.
  160. =item SSL_EXT_CLIENT_HELLO
  161. The extension may be present in the ClientHello message.
  162. =item SSL_EXT_TLS1_2_SERVER_HELLO
  163. The extension may be present in a TLSv1.2 or below compatible ServerHello
  164. message.
  165. =item SSL_EXT_TLS1_3_SERVER_HELLO
  166. The extension may be present in a TLSv1.3 compatible ServerHello message.
  167. =item SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  168. The extension may be present in an EncryptedExtensions message.
  169. =item SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
  170. The extension may be present in a HelloRetryRequest message.
  171. =item SSL_EXT_TLS1_3_CERTIFICATE
  172. The extension may be present in a TLSv1.3 compatible Certificate message.
  173. =item SSL_EXT_TLS1_3_NEW_SESSION_TICKET
  174. The extension may be present in a TLSv1.3 compatible NewSessionTicket message.
  175. =item SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  176. The extension may be present in a TLSv1.3 compatible CertificateRequest message.
  177. =back
  178. The context must include at least one message value (otherwise the extension
  179. will never be used).
  180. =head1 NOTES
  181. The B<add_arg> and B<parse_arg> parameters can be set to arbitrary values
  182. which will be passed to the corresponding callbacks. They can, for example,
  183. be used to store the extension data received in a convenient structure or
  184. pass the extension data to be added or freed when adding extensions.
  185. If the same custom extension type is received multiple times a fatal
  186. B<decode_error> alert is sent and the handshake aborts. If a custom extension
  187. is received in a ServerHello/EncryptedExtensions message which was not sent in
  188. the ClientHello a fatal B<unsupported_extension> alert is sent and the
  189. handshake is aborted. The ServerHello/EncryptedExtensions B<add_cb> callback is
  190. only called if the corresponding extension was received in the ClientHello. This
  191. is compliant with the TLS specifications. This behaviour ensures that each
  192. callback is called at most once and that an application can never send
  193. unsolicited extensions.
  194. =head1 RETURN VALUES
  195. SSL_CTX_add_custom_ext(), SSL_CTX_add_client_custom_ext() and
  196. SSL_CTX_add_server_custom_ext() return 1 for success and 0 for failure. A
  197. failure can occur if an attempt is made to add the same B<ext_type> more than
  198. once, if an attempt is made to use an extension type handled internally by
  199. OpenSSL or if an internal error occurs (for example a memory allocation
  200. failure).
  201. SSL_extension_supported() returns 1 if the extension B<ext_type> is handled
  202. internally by OpenSSL and 0 otherwise.
  203. =head1 SEE ALSO
  204. L<ssl(7)>
  205. =head1 HISTORY
  206. The SSL_CTX_add_custom_ext() function was added in OpenSSL 1.1.1.
  207. =head1 COPYRIGHT
  208. Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
  209. Licensed under the Apache License 2.0 (the "License"). You may not use
  210. this file except in compliance with the License. You can obtain a copy
  211. in the file LICENSE in the source distribution or at
  212. L<https://www.openssl.org/source/license.html>.
  213. =cut