C语言 如何实现服务器名称指示 (SNI)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5113333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:52:45  来源:igfitidea点击:

How to implement Server Name Indication (SNI)

csslopensslsni

提问by 2.8a8a_G

How to implement Server Name Indication(SNI) on OpenSSL in C or C++?

如何在 C 或 C++ 中的 OpenSSL 上实现服务器名称指示(SNI)?

Are there any real world examples available?

有没有真实世界的例子?

回答by caf

On the client side, you use SSL_set_tlsext_host_name(ssl, servername)before initiating the SSL connection.

在客户端,您SSL_set_tlsext_host_name(ssl, servername)在启动 SSL 连接之前使用。

On the server side, it's a little more complicated:

在服务器端,它有点复杂:

  • Set up an additional SSL_CTX()for each different certificate;
  • Add a servername callback to each SSL_CTX()using SSL_CTX_set_tlsext_servername_callback();
  • In the callback, retrieve the client-supplied servername with SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name). Figure out the right SSL_CTXto go with that host name, then switch the SSLobject to that SSL_CTXwith SSL_set_SSL_CTX().
  • SSL_CTX()为每个不同的证书设置一个附加的;
  • 为每个SSL_CTX()使用添加一个 servername 回调SSL_CTX_set_tlsext_servername_callback()
  • 在回调中,使用 检索客户端提供的服务器名SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)。找出SSL_CTX使用该主机名的权利,然后将SSL对象切换到SSL_CTX使用SSL_set_SSL_CTX().

The s_client.cand s_server.cfiles in the apps/directory of the OpenSSL source distribution implement this functionality, so they're a good resource to see how it should be done.

s_client.cs_server.c中的文件apps/OpenSSL的源代码发布的目录实现此功能,所以他们是一个很好的资源,看看它应该怎么做。