1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "asio_client_tls_context.h"
26
27 #include <openssl/ssl.h>
28
29 #include <boost/asio/ssl.hpp>
30
31 #include "tls.h"
32 #include "util.h"
33
34 namespace nghttp2 {
35 namespace asio_http2 {
36 namespace client {
37
38 #ifndef OPENSSL_NO_NEXTPROTONEG
39 namespace {
client_select_next_proto_cb(SSL * ssl,unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)40 int client_select_next_proto_cb(SSL *ssl, unsigned char **out,
41 unsigned char *outlen, const unsigned char *in,
42 unsigned int inlen, void *arg) {
43 if (!util::select_h2(const_cast<const unsigned char **>(out), outlen, in,
44 inlen)) {
45 return SSL_TLSEXT_ERR_NOACK;
46 }
47 return SSL_TLSEXT_ERR_OK;
48 }
49 } // namespace
50 #endif // !OPENSSL_NO_NEXTPROTONEG
51
52 boost::system::error_code
configure_tls_context(boost::system::error_code & ec,boost::asio::ssl::context & tls_ctx)53 configure_tls_context(boost::system::error_code &ec,
54 boost::asio::ssl::context &tls_ctx) {
55 ec.clear();
56
57 auto ctx = tls_ctx.native_handle();
58
59 #ifndef OPENSSL_NO_NEXTPROTONEG
60 SSL_CTX_set_next_proto_select_cb(ctx, client_select_next_proto_cb, nullptr);
61 #endif // !OPENSSL_NO_NEXTPROTONEG
62
63 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
64 auto proto_list = util::get_default_alpn();
65
66 SSL_CTX_set_alpn_protos(ctx, proto_list.data(), proto_list.size());
67 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
68
69 return ec;
70 }
71
72 } // namespace client
73 } // namespace asio_http2
74 } // namespace nghttp2
75