context_service.hpp 4.9 KB

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