context_service.hpp 4.9 KB

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