SSL_CTX_set_tlsext_servername_callback.pod 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_tlsext_servername_callback, SSL_CTX_set_tlsext_servername_arg,
  4. SSL_get_servername_type, SSL_get_servername,
  5. SSL_set_tlsext_host_name - handle server name indication (SNI)
  6. =head1 SYNOPSIS
  7. #include <openssl/ssl.h>
  8. long SSL_CTX_set_tlsext_servername_callback(SSL_CTX *ctx,
  9. int (*cb)(SSL *s, int *al, void *arg));
  10. long SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg);
  11. const char *SSL_get_servername(const SSL *s, const int type);
  12. int SSL_get_servername_type(const SSL *s);
  13. int SSL_set_tlsext_host_name(const SSL *s, const char *name);
  14. =head1 DESCRIPTION
  15. The functionality provided by the servername callback is mostly superseded by
  16. the ClientHello callback, which can be set using SSL_CTX_set_client_hello_cb().
  17. However, even where the ClientHello callback is used, the servername callback is
  18. still necessary in order to acknowledge the servername requested by the client.
  19. SSL_CTX_set_tlsext_servername_callback() sets the application callback B<cb>
  20. used by a server to perform any actions or configuration required based on
  21. the servername extension received in the incoming connection. When B<cb>
  22. is NULL, SNI is not used.
  23. The servername callback should return one of the following values:
  24. =over 4
  25. =item SSL_TLSEXT_ERR_OK
  26. This is used to indicate that the servername requested by the client has been
  27. accepted. Typically a server will call SSL_set_SSL_CTX() in the callback to set
  28. up a different configuration for the selected servername in this case.
  29. =item SSL_TLSEXT_ERR_ALERT_FATAL
  30. In this case the servername requested by the client is not accepted and the
  31. handshake will be aborted. The value of the alert to be used should be stored in
  32. the location pointed to by the B<al> parameter to the callback. By default this
  33. value is initialised to SSL_AD_UNRECOGNIZED_NAME.
  34. =item SSL_TLSEXT_ERR_ALERT_WARNING
  35. If this value is returned then the servername is not accepted by the server.
  36. However, the handshake will continue and send a warning alert instead. The value
  37. of the alert should be stored in the location pointed to by the B<al> parameter
  38. as for SSL_TLSEXT_ERR_ALERT_FATAL above. Note that TLSv1.3 does not support
  39. warning alerts, so if TLSv1.3 has been negotiated then this return value is
  40. treated the same way as SSL_TLSEXT_ERR_NOACK.
  41. =item SSL_TLSEXT_ERR_NOACK
  42. This return value indicates that the servername is not accepted by the server.
  43. No alerts are sent and the server will not acknowledge the requested servername.
  44. =back
  45. SSL_CTX_set_tlsext_servername_arg() sets a context-specific argument to be
  46. passed into the callback (via the B<arg> parameter) for this B<SSL_CTX>.
  47. The behaviour of SSL_get_servername() depends on a number of different factors.
  48. In particular note that in TLSv1.3 the servername is negotiated in every
  49. handshake. In TLSv1.2 the servername is only negotiated on initial handshakes
  50. and not on resumption handshakes.
  51. =over 4
  52. =item On the client, before the handshake
  53. If a servername has been set via a call to SSL_set_tlsext_host_name() then it
  54. will return that servername.
  55. If one has not been set, but a TLSv1.2 resumption is being attempted and the
  56. session from the original handshake had a servername accepted by the server then
  57. it will return that servername.
  58. Otherwise it returns NULL.
  59. =item On the client, during or after the handshake and a TLSv1.2 (or below)
  60. resumption occurred
  61. If the session from the original handshake had a servername accepted by the
  62. server then it will return that servername.
  63. Otherwise it returns the servername set via SSL_set_tlsext_host_name() or NULL
  64. if it was not called.
  65. =item On the client, during or after the handshake and a TLSv1.2 (or below)
  66. resumption did not occur
  67. It will return the servername set via SSL_set_tlsext_host_name() or NULL if it
  68. was not called.
  69. =item On the server, before the handshake
  70. The function will always return NULL before the handshake
  71. =item On the server, after the servername extension has been processed and a
  72. TLSv1.2 (or below) resumption occurred
  73. If a servername was accepted by the server in the original handshake then it
  74. will return that servername, or NULL otherwise.
  75. =item On the server, after the servername extension has been processed and a
  76. TLSv1.2 (or below) resumption did not occur
  77. The function will return the servername requested by the client in this
  78. handshake or NULL if none was requested.
  79. =back
  80. Note that the ClientHello callback occurs before a servername extension from the
  81. client is processed. The servername, certificate and ALPN callbacks occur after
  82. a servername extension from the client is processed.
  83. SSL_get_servername_type() returns the servername type or -1 if no servername
  84. is present. Currently the only supported type (defined in RFC3546) is
  85. B<TLSEXT_NAMETYPE_host_name>.
  86. SSL_set_tlsext_host_name() sets the server name indication ClientHello extension
  87. to contain the value B<name>. The type of server name indication extension is set
  88. to B<TLSEXT_NAMETYPE_host_name> (defined in RFC3546).
  89. =head1 NOTES
  90. Several callbacks are executed during ClientHello processing, including
  91. the ClientHello, ALPN, and servername callbacks. The ClientHello callback is
  92. executed first, then the servername callback, followed by the ALPN callback.
  93. The SSL_set_tlsext_host_name() function should only be called on SSL objects
  94. that will act as clients; otherwise the configured B<name> will be ignored.
  95. =head1 RETURN VALUES
  96. SSL_CTX_set_tlsext_servername_callback() and
  97. SSL_CTX_set_tlsext_servername_arg() both always return 1 indicating success.
  98. SSL_set_tlsext_host_name() returns 1 on success, 0 in case of error.
  99. =head1 SEE ALSO
  100. L<ssl(7)>, L<SSL_CTX_set_alpn_select_cb(3)>,
  101. L<SSL_get0_alpn_selected(3)>, L<SSL_CTX_set_client_hello_cb(3)>
  102. =head1 HISTORY
  103. SSL_get_servername() historically provided some unexpected results in certain
  104. corner cases. This has been fixed from OpenSSL 1.1.1e.
  105. Prior to 1.1.1e, when the client requested a servername in an initial TLSv1.2
  106. handshake, the server accepted it, and then the client successfully resumed but
  107. set a different explicit servername in the second handshake then when called by
  108. the client it returned the servername from the second handshake. This has now
  109. been changed to return the servername requested in the original handshake.
  110. Also prior to 1.1.1e, if the client sent a servername in the first handshake but
  111. the server did not accept it, and then a second handshake occurred where TLSv1.2
  112. resumption was successful then when called by the server it returned the
  113. servername requested in the original handshake. This has now been changed to
  114. NULL.
  115. =head1 COPYRIGHT
  116. Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  117. Licensed under the Apache License 2.0 (the "License"). You may not use
  118. this file except in compliance with the License. You can obtain a copy
  119. in the file LICENSE in the source distribution or at
  120. L<https://www.openssl.org/source/license.html>.
  121. =cut