context_service.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // context_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  7. //
  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)
  10. //
  11. #ifndef BOOST_ASIO_SSL_CONTEXT_SERVICE_HPP
  12. #define BOOST_ASIO_SSL_CONTEXT_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. #include <string>
  19. #include <boost/noncopyable.hpp>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/io_service.hpp>
  23. #include <boost/asio/detail/service_base.hpp>
  24. #include <boost/asio/ssl/context_base.hpp>
  25. #include <boost/asio/ssl/detail/openssl_context_service.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace ssl {
  29. /// Default service implementation for a context.
  30. class context_service
  31. #if defined(GENERATING_DOCUMENTATION)
  32. : public boost::asio::io_service::service
  33. #else
  34. : public boost::asio::detail::service_base<context_service>
  35. #endif
  36. {
  37. private:
  38. // The type of the platform-specific implementation.
  39. typedef detail::openssl_context_service service_impl_type;
  40. public:
  41. #if defined(GENERATING_DOCUMENTATION)
  42. /// The unique service identifier.
  43. static boost::asio::io_service::id id;
  44. #endif
  45. /// The type of the context.
  46. #if defined(GENERATING_DOCUMENTATION)
  47. typedef implementation_defined impl_type;
  48. #else
  49. typedef service_impl_type::impl_type impl_type;
  50. #endif
  51. /// Constructor.
  52. explicit context_service(boost::asio::io_service& io_service)
  53. : boost::asio::detail::service_base<context_service>(io_service),
  54. service_impl_(boost::asio::use_service<service_impl_type>(io_service))
  55. {
  56. }
  57. /// Destroy all user-defined handler objects owned by the service.
  58. void shutdown_service()
  59. {
  60. }
  61. /// Return a null context implementation.
  62. impl_type null() const
  63. {
  64. return service_impl_.null();
  65. }
  66. /// Create a new context implementation.
  67. void create(impl_type& impl, context_base::method m)
  68. {
  69. service_impl_.create(impl, m);
  70. }
  71. /// Destroy a context implementation.
  72. void destroy(impl_type& impl)
  73. {
  74. service_impl_.destroy(impl);
  75. }
  76. /// Set options on the context.
  77. boost::system::error_code set_options(impl_type& impl,
  78. context_base::options o, boost::system::error_code& ec)
  79. {
  80. return service_impl_.set_options(impl, o, ec);
  81. }
  82. /// Set peer verification mode.
  83. boost::system::error_code set_verify_mode(impl_type& impl,
  84. context_base::verify_mode v, boost::system::error_code& ec)
  85. {
  86. return service_impl_.set_verify_mode(impl, v, ec);
  87. }
  88. /// Load a certification authority file for performing verification.
  89. boost::system::error_code load_verify_file(impl_type& impl,
  90. const std::string& filename, boost::system::error_code& ec)
  91. {
  92. return service_impl_.load_verify_file(impl, filename, ec);
  93. }
  94. /// Add a directory containing certification authority files to be used for
  95. /// performing verification.
  96. boost::system::error_code add_verify_path(impl_type& impl,
  97. const std::string& path, boost::system::error_code& ec)
  98. {
  99. return service_impl_.add_verify_path(impl, path, ec);
  100. }
  101. /// Use a certificate from a file.
  102. boost::system::error_code use_certificate_file(impl_type& impl,
  103. const std::string& filename, context_base::file_format format,
  104. boost::system::error_code& ec)
  105. {
  106. return service_impl_.use_certificate_file(impl, filename, format, ec);
  107. }
  108. /// Use a certificate chain from a file.
  109. boost::system::error_code use_certificate_chain_file(impl_type& impl,
  110. const std::string& filename, boost::system::error_code& ec)
  111. {
  112. return service_impl_.use_certificate_chain_file(impl, filename, ec);
  113. }
  114. /// Use a private key from a file.
  115. boost::system::error_code use_private_key_file(impl_type& impl,
  116. const std::string& filename, context_base::file_format format,
  117. boost::system::error_code& ec)
  118. {
  119. return service_impl_.use_private_key_file(impl, filename, format, ec);
  120. }
  121. /// Use an RSA private key from a file.
  122. boost::system::error_code use_rsa_private_key_file(impl_type& impl,
  123. const std::string& filename, context_base::file_format format,
  124. boost::system::error_code& ec)
  125. {
  126. return service_impl_.use_rsa_private_key_file(impl, filename, format, ec);
  127. }
  128. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  129. boost::system::error_code use_tmp_dh_file(impl_type& impl,
  130. const std::string& filename, boost::system::error_code& ec)
  131. {
  132. return service_impl_.use_tmp_dh_file(impl, filename, ec);
  133. }
  134. /// Set the password callback.
  135. template <typename PasswordCallback>
  136. boost::system::error_code set_password_callback(impl_type& impl,
  137. PasswordCallback callback, boost::system::error_code& ec)
  138. {
  139. return service_impl_.set_password_callback(impl, callback, ec);
  140. }
  141. private:
  142. // The service that provides the platform-specific implementation.
  143. service_impl_type& service_impl_;
  144. };
  145. } // namespace ssl
  146. } // namespace asio
  147. } // namespace boost
  148. #include <boost/asio/detail/pop_options.hpp>
  149. #endif // BOOST_ASIO_SSL_CONTEXT_SERVICE_HPP