2 // ssl/impl/context.ipp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6 // Copyright (c) 2005-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_SSL_IMPL_CONTEXT_IPP
13 #define BOOST_ASIO_SSL_IMPL_CONTEXT_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
23 # include <boost/asio/detail/throw_error.hpp>
24 # include <boost/asio/error.hpp>
25 # include <boost/asio/ssl/context.hpp>
26 # include <boost/asio/ssl/error.hpp>
27 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
29 #include <boost/asio/detail/push_options.hpp>
35 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
37 struct context::bio_cleanup
40 ~bio_cleanup() { if (p) ::BIO_free(p); }
43 struct context::x509_cleanup
46 ~x509_cleanup() { if (p) ::X509_free(p); }
49 struct context::evp_pkey_cleanup
52 ~evp_pkey_cleanup() { if (p) ::EVP_PKEY_free(p); }
55 struct context::rsa_cleanup
58 ~rsa_cleanup() { if (p) ::RSA_free(p); }
61 struct context::dh_cleanup
64 ~dh_cleanup() { if (p) ::DH_free(p); }
67 context::context(context::method m)
74 #if defined(OPENSSL_NO_SSL2) \
75 || (OPENSSL_VERSION_NUMBER >= 0x10100000L)
77 case context::sslv2_client:
78 case context::sslv2_server:
79 boost::asio::detail::throw_error(
80 boost::asio::error::invalid_argument, "context");
82 #else // defined(OPENSSL_NO_SSL2)
83 // || (OPENSSL_VERSION_NUMBER >= 0x10100000L)
85 handle_ = ::SSL_CTX_new(::SSLv2_method());
87 case context::sslv2_client:
88 handle_ = ::SSL_CTX_new(::SSLv2_client_method());
90 case context::sslv2_server:
91 handle_ = ::SSL_CTX_new(::SSLv2_server_method());
93 #endif // defined(OPENSSL_NO_SSL2)
94 // || (OPENSSL_VERSION_NUMBER >= 0x10100000L)
95 #if defined(OPENSSL_NO_SSL3)
97 case context::sslv3_client:
98 case context::sslv3_server:
99 boost::asio::detail::throw_error(
100 boost::asio::error::invalid_argument, "context");
102 #else // defined(OPENSSL_NO_SSL3)
104 handle_ = ::SSL_CTX_new(::SSLv3_method());
106 case context::sslv3_client:
107 handle_ = ::SSL_CTX_new(::SSLv3_client_method());
109 case context::sslv3_server:
110 handle_ = ::SSL_CTX_new(::SSLv3_server_method());
112 #endif // defined(OPENSSL_NO_SSL3)
113 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
115 handle_ = ::SSL_CTX_new(::TLSv1_method());
117 case context::tlsv1_client:
118 handle_ = ::SSL_CTX_new(::TLSv1_client_method());
120 case context::tlsv1_server:
121 handle_ = ::SSL_CTX_new(::TLSv1_server_method());
123 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
124 case context::sslv23:
125 handle_ = ::SSL_CTX_new(::SSLv23_method());
127 case context::sslv23_client:
128 handle_ = ::SSL_CTX_new(::SSLv23_client_method());
130 case context::sslv23_server:
131 handle_ = ::SSL_CTX_new(::SSLv23_server_method());
133 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
134 #if defined(SSL_TXT_TLSV1_1)
135 case context::tlsv11:
136 handle_ = ::SSL_CTX_new(::TLSv1_1_method());
138 case context::tlsv11_client:
139 handle_ = ::SSL_CTX_new(::TLSv1_1_client_method());
141 case context::tlsv11_server:
142 handle_ = ::SSL_CTX_new(::TLSv1_1_server_method());
144 #else // defined(SSL_TXT_TLSV1_1)
145 case context::tlsv11:
146 case context::tlsv11_client:
147 case context::tlsv11_server:
148 boost::asio::detail::throw_error(
149 boost::asio::error::invalid_argument, "context");
151 #endif // defined(SSL_TXT_TLSV1_1)
152 #if defined(SSL_TXT_TLSV1_2)
153 case context::tlsv12:
154 handle_ = ::SSL_CTX_new(::TLSv1_2_method());
156 case context::tlsv12_client:
157 handle_ = ::SSL_CTX_new(::TLSv1_2_client_method());
159 case context::tlsv12_server:
160 handle_ = ::SSL_CTX_new(::TLSv1_2_server_method());
162 #else // defined(SSL_TXT_TLSV1_2)
163 case context::tlsv12:
164 case context::tlsv12_client:
165 case context::tlsv12_server:
166 boost::asio::detail::throw_error(
167 boost::asio::error::invalid_argument, "context");
169 #endif // defined(SSL_TXT_TLSV1_2)
170 #else // (OPENSSL_VERSION_NUMBER < 0x10100000L)
172 case context::tlsv11:
173 case context::tlsv12:
174 handle_ = ::SSL_CTX_new(::TLS_method());
176 case context::tlsv1_client:
177 case context::tlsv11_client:
178 case context::tlsv12_client:
179 handle_ = ::SSL_CTX_new(::TLS_client_method());
181 case context::tlsv1_server:
182 case context::tlsv11_server:
183 case context::tlsv12_server:
184 handle_ = ::SSL_CTX_new(::TLS_server_method());
186 #endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
188 handle_ = ::SSL_CTX_new(0);
194 boost::system::error_code ec(
195 static_cast<int>(::ERR_get_error()),
196 boost::asio::error::get_ssl_category());
197 boost::asio::detail::throw_error(ec, "context");
200 set_options(no_compression);
203 context::context(boost::asio::io_service&, context::method m)
207 handle_ = tmp.handle_;
211 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
212 context::context(context&& other)
214 handle_ = other.handle_;
218 context& context::operator=(context&& other)
220 context tmp(BOOST_ASIO_MOVE_CAST(context)(*this));
221 handle_ = other.handle_;
225 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
231 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
232 void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
233 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
234 void* cb_userdata = handle_->default_passwd_callback_userdata;
235 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
238 detail::password_callback_base* callback =
239 static_cast<detail::password_callback_base*>(
242 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
243 ::SSL_CTX_set_default_passwd_cb_userdata(handle_, 0);
244 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
245 handle_->default_passwd_callback_userdata = 0;
246 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
249 if (SSL_CTX_get_app_data(handle_))
251 detail::verify_callback_base* callback =
252 static_cast<detail::verify_callback_base*>(
253 SSL_CTX_get_app_data(handle_));
255 SSL_CTX_set_app_data(handle_, 0);
258 ::SSL_CTX_free(handle_);
262 context::native_handle_type context::native_handle()
267 context::impl_type context::impl()
272 void context::clear_options(context::options o)
274 boost::system::error_code ec;
275 clear_options(o, ec);
276 boost::asio::detail::throw_error(ec, "clear_options");
279 boost::system::error_code context::clear_options(
280 context::options o, boost::system::error_code& ec)
282 #if (OPENSSL_VERSION_NUMBER >= 0x009080DFL) \
283 && (OPENSSL_VERSION_NUMBER != 0x00909000L)
284 # if !defined(SSL_OP_NO_COMPRESSION)
285 if ((o & context::no_compression) != 0)
287 # if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
288 handle_->comp_methods = SSL_COMP_get_compression_methods();
289 # endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
290 o ^= context::no_compression;
292 # endif // !defined(SSL_OP_NO_COMPRESSION)
294 ::SSL_CTX_clear_options(handle_, o);
296 ec = boost::system::error_code();
297 #else // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
298 // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
300 ec = boost::asio::error::operation_not_supported;
301 #endif // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
302 // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
306 void context::set_options(context::options o)
308 boost::system::error_code ec;
310 boost::asio::detail::throw_error(ec, "set_options");
313 boost::system::error_code context::set_options(
314 context::options o, boost::system::error_code& ec)
316 #if !defined(SSL_OP_NO_COMPRESSION)
317 if ((o & context::no_compression) != 0)
319 #if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
320 handle_->comp_methods =
321 boost::asio::ssl::detail::openssl_init<>::get_null_compression_methods();
322 #endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
323 o ^= context::no_compression;
325 #endif // !defined(SSL_OP_NO_COMPRESSION)
327 ::SSL_CTX_set_options(handle_, o);
329 ec = boost::system::error_code();
333 void context::set_verify_mode(verify_mode v)
335 boost::system::error_code ec;
336 set_verify_mode(v, ec);
337 boost::asio::detail::throw_error(ec, "set_verify_mode");
340 boost::system::error_code context::set_verify_mode(
341 verify_mode v, boost::system::error_code& ec)
343 ::SSL_CTX_set_verify(handle_, v, ::SSL_CTX_get_verify_callback(handle_));
345 ec = boost::system::error_code();
349 void context::set_verify_depth(int depth)
351 boost::system::error_code ec;
352 set_verify_depth(depth, ec);
353 boost::asio::detail::throw_error(ec, "set_verify_depth");
356 boost::system::error_code context::set_verify_depth(
357 int depth, boost::system::error_code& ec)
359 ::SSL_CTX_set_verify_depth(handle_, depth);
361 ec = boost::system::error_code();
365 void context::load_verify_file(const std::string& filename)
367 boost::system::error_code ec;
368 load_verify_file(filename, ec);
369 boost::asio::detail::throw_error(ec, "load_verify_file");
372 boost::system::error_code context::load_verify_file(
373 const std::string& filename, boost::system::error_code& ec)
377 if (::SSL_CTX_load_verify_locations(handle_, filename.c_str(), 0) != 1)
379 ec = boost::system::error_code(
380 static_cast<int>(::ERR_get_error()),
381 boost::asio::error::get_ssl_category());
385 ec = boost::system::error_code();
389 void context::add_certificate_authority(const const_buffer& ca)
391 boost::system::error_code ec;
392 add_certificate_authority(ca, ec);
393 boost::asio::detail::throw_error(ec, "add_certificate_authority");
396 boost::system::error_code context::add_certificate_authority(
397 const const_buffer& ca, boost::system::error_code& ec)
401 bio_cleanup bio = { make_buffer_bio(ca) };
404 x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
407 if (X509_STORE* store = ::SSL_CTX_get_cert_store(handle_))
409 if (::X509_STORE_add_cert(store, cert.p) == 1)
411 ec = boost::system::error_code();
418 ec = boost::system::error_code(
419 static_cast<int>(::ERR_get_error()),
420 boost::asio::error::get_ssl_category());
424 void context::set_default_verify_paths()
426 boost::system::error_code ec;
427 set_default_verify_paths(ec);
428 boost::asio::detail::throw_error(ec, "set_default_verify_paths");
431 boost::system::error_code context::set_default_verify_paths(
432 boost::system::error_code& ec)
436 if (::SSL_CTX_set_default_verify_paths(handle_) != 1)
438 ec = boost::system::error_code(
439 static_cast<int>(::ERR_get_error()),
440 boost::asio::error::get_ssl_category());
444 ec = boost::system::error_code();
448 void context::add_verify_path(const std::string& path)
450 boost::system::error_code ec;
451 add_verify_path(path, ec);
452 boost::asio::detail::throw_error(ec, "add_verify_path");
455 boost::system::error_code context::add_verify_path(
456 const std::string& path, boost::system::error_code& ec)
460 if (::SSL_CTX_load_verify_locations(handle_, 0, path.c_str()) != 1)
462 ec = boost::system::error_code(
463 static_cast<int>(::ERR_get_error()),
464 boost::asio::error::get_ssl_category());
468 ec = boost::system::error_code();
472 void context::use_certificate(
473 const const_buffer& certificate, file_format format)
475 boost::system::error_code ec;
476 use_certificate(certificate, format, ec);
477 boost::asio::detail::throw_error(ec, "use_certificate");
480 boost::system::error_code context::use_certificate(
481 const const_buffer& certificate, file_format format,
482 boost::system::error_code& ec)
486 if (format == context_base::asn1)
488 if (::SSL_CTX_use_certificate_ASN1(handle_,
489 static_cast<int>(buffer_size(certificate)),
490 buffer_cast<const unsigned char*>(certificate)) == 1)
492 ec = boost::system::error_code();
496 else if (format == context_base::pem)
498 bio_cleanup bio = { make_buffer_bio(certificate) };
501 x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
504 if (::SSL_CTX_use_certificate(handle_, cert.p) == 1)
506 ec = boost::system::error_code();
514 ec = boost::asio::error::invalid_argument;
518 ec = boost::system::error_code(
519 static_cast<int>(::ERR_get_error()),
520 boost::asio::error::get_ssl_category());
524 void context::use_certificate_file(
525 const std::string& filename, file_format format)
527 boost::system::error_code ec;
528 use_certificate_file(filename, format, ec);
529 boost::asio::detail::throw_error(ec, "use_certificate_file");
532 boost::system::error_code context::use_certificate_file(
533 const std::string& filename, file_format format,
534 boost::system::error_code& ec)
539 case context_base::asn1:
540 file_type = SSL_FILETYPE_ASN1;
542 case context_base::pem:
543 file_type = SSL_FILETYPE_PEM;
547 ec = boost::asio::error::invalid_argument;
554 if (::SSL_CTX_use_certificate_file(handle_, filename.c_str(), file_type) != 1)
556 ec = boost::system::error_code(
557 static_cast<int>(::ERR_get_error()),
558 boost::asio::error::get_ssl_category());
562 ec = boost::system::error_code();
566 void context::use_certificate_chain(const const_buffer& chain)
568 boost::system::error_code ec;
569 use_certificate_chain(chain, ec);
570 boost::asio::detail::throw_error(ec, "use_certificate_chain");
573 boost::system::error_code context::use_certificate_chain(
574 const const_buffer& chain, boost::system::error_code& ec)
578 bio_cleanup bio = { make_buffer_bio(chain) };
581 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
582 pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
583 void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
584 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
585 pem_password_cb* callback = handle_->default_passwd_callback;
586 void* cb_userdata = handle_->default_passwd_callback_userdata;
587 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
588 x509_cleanup cert = {
589 ::PEM_read_bio_X509_AUX(bio.p, 0,
594 ec = boost::system::error_code(ERR_R_PEM_LIB,
595 boost::asio::error::get_ssl_category());
599 int result = ::SSL_CTX_use_certificate(handle_, cert.p);
600 if (result == 0 || ::ERR_peek_error() != 0)
602 ec = boost::system::error_code(
603 static_cast<int>(::ERR_get_error()),
604 boost::asio::error::get_ssl_category());
608 #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) && !defined(LIBRESSL_VERSION_NUMBER)
609 ::SSL_CTX_clear_chain_certs(handle_);
611 if (handle_->extra_certs)
613 ::sk_X509_pop_free(handle_->extra_certs, X509_free);
614 handle_->extra_certs = 0;
616 #endif // (OPENSSL_VERSION_NUMBER >= 0x10002000L)
618 while (X509* cacert = ::PEM_read_bio_X509(bio.p, 0,
622 if (!::SSL_CTX_add_extra_chain_cert(handle_, cacert))
624 ec = boost::system::error_code(
625 static_cast<int>(::ERR_get_error()),
626 boost::asio::error::get_ssl_category());
631 result = ::ERR_peek_last_error();
632 if ((ERR_GET_LIB(result) == ERR_LIB_PEM)
633 && (ERR_GET_REASON(result) == PEM_R_NO_START_LINE))
636 ec = boost::system::error_code();
641 ec = boost::system::error_code(
642 static_cast<int>(::ERR_get_error()),
643 boost::asio::error::get_ssl_category());
647 void context::use_certificate_chain_file(const std::string& filename)
649 boost::system::error_code ec;
650 use_certificate_chain_file(filename, ec);
651 boost::asio::detail::throw_error(ec, "use_certificate_chain_file");
654 boost::system::error_code context::use_certificate_chain_file(
655 const std::string& filename, boost::system::error_code& ec)
659 if (::SSL_CTX_use_certificate_chain_file(handle_, filename.c_str()) != 1)
661 ec = boost::system::error_code(
662 static_cast<int>(::ERR_get_error()),
663 boost::asio::error::get_ssl_category());
667 ec = boost::system::error_code();
671 void context::use_private_key(
672 const const_buffer& private_key, context::file_format format)
674 boost::system::error_code ec;
675 use_private_key(private_key, format, ec);
676 boost::asio::detail::throw_error(ec, "use_private_key");
679 boost::system::error_code context::use_private_key(
680 const const_buffer& private_key, context::file_format format,
681 boost::system::error_code& ec)
685 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
686 pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
687 void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
688 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
689 pem_password_cb* callback = handle_->default_passwd_callback;
690 void* cb_userdata = handle_->default_passwd_callback_userdata;
691 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
693 bio_cleanup bio = { make_buffer_bio(private_key) };
696 evp_pkey_cleanup evp_private_key = { 0 };
699 case context_base::asn1:
700 evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
702 case context_base::pem:
703 evp_private_key.p = ::PEM_read_bio_PrivateKey(
709 ec = boost::asio::error::invalid_argument;
714 if (evp_private_key.p)
716 if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
718 ec = boost::system::error_code();
724 ec = boost::system::error_code(
725 static_cast<int>(::ERR_get_error()),
726 boost::asio::error::get_ssl_category());
730 void context::use_private_key_file(
731 const std::string& filename, context::file_format format)
733 boost::system::error_code ec;
734 use_private_key_file(filename, format, ec);
735 boost::asio::detail::throw_error(ec, "use_private_key_file");
738 void context::use_rsa_private_key(
739 const const_buffer& private_key, context::file_format format)
741 boost::system::error_code ec;
742 use_rsa_private_key(private_key, format, ec);
743 boost::asio::detail::throw_error(ec, "use_rsa_private_key");
746 boost::system::error_code context::use_rsa_private_key(
747 const const_buffer& private_key, context::file_format format,
748 boost::system::error_code& ec)
752 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
753 pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
754 void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
755 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
756 pem_password_cb* callback = handle_->default_passwd_callback;
757 void* cb_userdata = handle_->default_passwd_callback_userdata;
758 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
760 bio_cleanup bio = { make_buffer_bio(private_key) };
763 rsa_cleanup rsa_private_key = { 0 };
766 case context_base::asn1:
767 rsa_private_key.p = ::d2i_RSAPrivateKey_bio(bio.p, 0);
769 case context_base::pem:
770 rsa_private_key.p = ::PEM_read_bio_RSAPrivateKey(
776 ec = boost::asio::error::invalid_argument;
781 if (rsa_private_key.p)
783 if (::SSL_CTX_use_RSAPrivateKey(handle_, rsa_private_key.p) == 1)
785 ec = boost::system::error_code();
791 ec = boost::system::error_code(
792 static_cast<int>(::ERR_get_error()),
793 boost::asio::error::get_ssl_category());
797 boost::system::error_code context::use_private_key_file(
798 const std::string& filename, context::file_format format,
799 boost::system::error_code& ec)
804 case context_base::asn1:
805 file_type = SSL_FILETYPE_ASN1;
807 case context_base::pem:
808 file_type = SSL_FILETYPE_PEM;
812 ec = boost::asio::error::invalid_argument;
819 if (::SSL_CTX_use_PrivateKey_file(handle_, filename.c_str(), file_type) != 1)
821 ec = boost::system::error_code(
822 static_cast<int>(::ERR_get_error()),
823 boost::asio::error::get_ssl_category());
827 ec = boost::system::error_code();
831 void context::use_rsa_private_key_file(
832 const std::string& filename, context::file_format format)
834 boost::system::error_code ec;
835 use_rsa_private_key_file(filename, format, ec);
836 boost::asio::detail::throw_error(ec, "use_rsa_private_key_file");
839 boost::system::error_code context::use_rsa_private_key_file(
840 const std::string& filename, context::file_format format,
841 boost::system::error_code& ec)
846 case context_base::asn1:
847 file_type = SSL_FILETYPE_ASN1;
849 case context_base::pem:
850 file_type = SSL_FILETYPE_PEM;
854 ec = boost::asio::error::invalid_argument;
861 if (::SSL_CTX_use_RSAPrivateKey_file(
862 handle_, filename.c_str(), file_type) != 1)
864 ec = boost::system::error_code(
865 static_cast<int>(::ERR_get_error()),
866 boost::asio::error::get_ssl_category());
870 ec = boost::system::error_code();
874 void context::use_tmp_dh(const const_buffer& dh)
876 boost::system::error_code ec;
878 boost::asio::detail::throw_error(ec, "use_tmp_dh");
881 boost::system::error_code context::use_tmp_dh(
882 const const_buffer& dh, boost::system::error_code& ec)
886 bio_cleanup bio = { make_buffer_bio(dh) };
889 return do_use_tmp_dh(bio.p, ec);
892 ec = boost::system::error_code(
893 static_cast<int>(::ERR_get_error()),
894 boost::asio::error::get_ssl_category());
898 void context::use_tmp_dh_file(const std::string& filename)
900 boost::system::error_code ec;
901 use_tmp_dh_file(filename, ec);
902 boost::asio::detail::throw_error(ec, "use_tmp_dh_file");
905 boost::system::error_code context::use_tmp_dh_file(
906 const std::string& filename, boost::system::error_code& ec)
910 bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
913 return do_use_tmp_dh(bio.p, ec);
916 ec = boost::system::error_code(
917 static_cast<int>(::ERR_get_error()),
918 boost::asio::error::get_ssl_category());
922 boost::system::error_code context::do_use_tmp_dh(
923 BIO* bio, boost::system::error_code& ec)
927 dh_cleanup dh = { ::PEM_read_bio_DHparams(bio, 0, 0, 0) };
930 if (::SSL_CTX_set_tmp_dh(handle_, dh.p) == 1)
932 ec = boost::system::error_code();
937 ec = boost::system::error_code(
938 static_cast<int>(::ERR_get_error()),
939 boost::asio::error::get_ssl_category());
943 boost::system::error_code context::do_set_verify_callback(
944 detail::verify_callback_base* callback, boost::system::error_code& ec)
946 if (SSL_CTX_get_app_data(handle_))
948 delete static_cast<detail::verify_callback_base*>(
949 SSL_CTX_get_app_data(handle_));
952 SSL_CTX_set_app_data(handle_, callback);
954 ::SSL_CTX_set_verify(handle_,
955 ::SSL_CTX_get_verify_mode(handle_),
956 &context::verify_callback_function);
958 ec = boost::system::error_code();
962 int context::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
966 if (SSL* ssl = static_cast<SSL*>(
967 ::X509_STORE_CTX_get_ex_data(
968 ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
970 if (SSL_CTX* handle = ::SSL_get_SSL_CTX(ssl))
972 if (SSL_CTX_get_app_data(handle))
974 detail::verify_callback_base* callback =
975 static_cast<detail::verify_callback_base*>(
976 SSL_CTX_get_app_data(handle));
978 verify_context verify_ctx(ctx);
979 return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
988 boost::system::error_code context::do_set_password_callback(
989 detail::password_callback_base* callback, boost::system::error_code& ec)
991 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
992 void* old_callback = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
993 ::SSL_CTX_set_default_passwd_cb_userdata(handle_, callback);
994 #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
995 void* old_callback = handle_->default_passwd_callback_userdata;
996 handle_->default_passwd_callback_userdata = callback;
997 #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
1000 delete static_cast<detail::password_callback_base*>(
1003 SSL_CTX_set_default_passwd_cb(handle_, &context::password_callback_function);
1005 ec = boost::system::error_code();
1009 int context::password_callback_function(
1010 char* buf, int size, int purpose, void* data)
1012 using namespace std; // For strncat and strlen.
1016 detail::password_callback_base* callback =
1017 static_cast<detail::password_callback_base*>(data);
1019 std::string passwd = callback->call(static_cast<std::size_t>(size),
1020 purpose ? context_base::for_writing : context_base::for_reading);
1022 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
1023 strcpy_s(buf, size, passwd.c_str());
1024 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
1027 strncat(buf, passwd.c_str(), size - 1);
1028 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
1030 return static_cast<int>(strlen(buf));
1036 BIO* context::make_buffer_bio(const const_buffer& b)
1038 return ::BIO_new_mem_buf(
1039 const_cast<void*>(buffer_cast<const void*>(b)),
1040 static_cast<int>(buffer_size(b)));
1043 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
1047 } // namespace boost
1049 #include <boost/asio/detail/pop_options.hpp>
1051 #endif // BOOST_ASIO_SSL_IMPL_CONTEXT_IPP