SSL_extension_supported.pod 12 KB

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