basic_context.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // basic_context.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_BASIC_CONTEXT_HPP
  12. #define BOOST_ASIO_SSL_BASIC_CONTEXT_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/ssl/context_base.hpp>
  24. #include <boost/asio/detail/throw_error.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace ssl {
  28. /// SSL context.
  29. template <typename Service>
  30. class basic_context
  31. : public context_base,
  32. private boost::noncopyable
  33. {
  34. public:
  35. /// The type of the service that will be used to provide context operations.
  36. typedef Service service_type;
  37. /// The native implementation type of the locking dispatcher.
  38. typedef typename service_type::impl_type impl_type;
  39. /// Constructor.
  40. basic_context(boost::asio::io_service& io_service, method m)
  41. : service_(boost::asio::use_service<Service>(io_service)),
  42. impl_(service_.null())
  43. {
  44. service_.create(impl_, m);
  45. }
  46. /// Destructor.
  47. ~basic_context()
  48. {
  49. service_.destroy(impl_);
  50. }
  51. /// Get the underlying implementation in the native type.
  52. /**
  53. * This function may be used to obtain the underlying implementation of the
  54. * context. This is intended to allow access to context functionality that is
  55. * not otherwise provided.
  56. */
  57. impl_type impl()
  58. {
  59. return impl_;
  60. }
  61. /// Set options on the context.
  62. /**
  63. * This function may be used to configure the SSL options used by the context.
  64. *
  65. * @param o A bitmask of options. The available option values are defined in
  66. * the context_base class. The options are bitwise-ored with any existing
  67. * value for the options.
  68. *
  69. * @throws boost::system::system_error Thrown on failure.
  70. */
  71. void set_options(options o)
  72. {
  73. boost::system::error_code ec;
  74. service_.set_options(impl_, o, ec);
  75. boost::asio::detail::throw_error(ec);
  76. }
  77. /// Set options on the context.
  78. /**
  79. * This function may be used to configure the SSL options used by the context.
  80. *
  81. * @param o A bitmask of options. The available option values are defined in
  82. * the context_base class. The options are bitwise-ored with any existing
  83. * value for the options.
  84. *
  85. * @param ec Set to indicate what error occurred, if any.
  86. */
  87. boost::system::error_code set_options(options o,
  88. boost::system::error_code& ec)
  89. {
  90. return service_.set_options(impl_, o, ec);
  91. }
  92. /// Set the peer verification mode.
  93. /**
  94. * This function may be used to configure the peer verification mode used by
  95. * the context.
  96. *
  97. * @param v A bitmask of peer verification modes. The available verify_mode
  98. * values are defined in the context_base class.
  99. *
  100. * @throws boost::system::system_error Thrown on failure.
  101. */
  102. void set_verify_mode(verify_mode v)
  103. {
  104. boost::system::error_code ec;
  105. service_.set_verify_mode(impl_, v, ec);
  106. boost::asio::detail::throw_error(ec);
  107. }
  108. /// Set the peer verification mode.
  109. /**
  110. * This function may be used to configure the peer verification mode used by
  111. * the context.
  112. *
  113. * @param v A bitmask of peer verification modes. The available verify_mode
  114. * values are defined in the context_base class.
  115. *
  116. * @param ec Set to indicate what error occurred, if any.
  117. */
  118. boost::system::error_code set_verify_mode(verify_mode v,
  119. boost::system::error_code& ec)
  120. {
  121. return service_.set_verify_mode(impl_, v, ec);
  122. }
  123. /// Load a certification authority file for performing verification.
  124. /**
  125. * This function is used to load one or more trusted certification authorities
  126. * from a file.
  127. *
  128. * @param filename The name of a file containing certification authority
  129. * certificates in PEM format.
  130. *
  131. * @throws boost::system::system_error Thrown on failure.
  132. */
  133. void load_verify_file(const std::string& filename)
  134. {
  135. boost::system::error_code ec;
  136. service_.load_verify_file(impl_, filename, ec);
  137. boost::asio::detail::throw_error(ec);
  138. }
  139. /// Load a certification authority file for performing verification.
  140. /**
  141. * This function is used to load the certificates for one or more trusted
  142. * certification authorities from a file.
  143. *
  144. * @param filename The name of a file containing certification authority
  145. * certificates in PEM format.
  146. *
  147. * @param ec Set to indicate what error occurred, if any.
  148. */
  149. boost::system::error_code load_verify_file(const std::string& filename,
  150. boost::system::error_code& ec)
  151. {
  152. return service_.load_verify_file(impl_, filename, ec);
  153. }
  154. /// Add a directory containing certificate authority files to be used for
  155. /// performing verification.
  156. /**
  157. * This function is used to specify the name of a directory containing
  158. * certification authority certificates. Each file in the directory must
  159. * contain a single certificate. The files must be named using the subject
  160. * name's hash and an extension of ".0".
  161. *
  162. * @param path The name of a directory containing the certificates.
  163. *
  164. * @throws boost::system::system_error Thrown on failure.
  165. */
  166. void add_verify_path(const std::string& path)
  167. {
  168. boost::system::error_code ec;
  169. service_.add_verify_path(impl_, path, ec);
  170. boost::asio::detail::throw_error(ec);
  171. }
  172. /// Add a directory containing certificate authority files to be used for
  173. /// performing verification.
  174. /**
  175. * This function is used to specify the name of a directory containing
  176. * certification authority certificates. Each file in the directory must
  177. * contain a single certificate. The files must be named using the subject
  178. * name's hash and an extension of ".0".
  179. *
  180. * @param path The name of a directory containing the certificates.
  181. *
  182. * @param ec Set to indicate what error occurred, if any.
  183. */
  184. boost::system::error_code add_verify_path(const std::string& path,
  185. boost::system::error_code& ec)
  186. {
  187. return service_.add_verify_path(impl_, path, ec);
  188. }
  189. /// Use a certificate from a file.
  190. /**
  191. * This function is used to load a certificate into the context from a file.
  192. *
  193. * @param filename The name of the file containing the certificate.
  194. *
  195. * @param format The file format (ASN.1 or PEM).
  196. *
  197. * @throws boost::system::system_error Thrown on failure.
  198. */
  199. void use_certificate_file(const std::string& filename, file_format format)
  200. {
  201. boost::system::error_code ec;
  202. service_.use_certificate_file(impl_, filename, format, ec);
  203. boost::asio::detail::throw_error(ec);
  204. }
  205. /// Use a certificate from a file.
  206. /**
  207. * This function is used to load a certificate into the context from a file.
  208. *
  209. * @param filename The name of the file containing the certificate.
  210. *
  211. * @param format The file format (ASN.1 or PEM).
  212. *
  213. * @param ec Set to indicate what error occurred, if any.
  214. */
  215. boost::system::error_code use_certificate_file(const std::string& filename,
  216. file_format format, boost::system::error_code& ec)
  217. {
  218. return service_.use_certificate_file(impl_, filename, format, ec);
  219. }
  220. /// Use a certificate chain from a file.
  221. /**
  222. * This function is used to load a certificate chain into the context from a
  223. * file.
  224. *
  225. * @param filename The name of the file containing the certificate. The file
  226. * must use the PEM format.
  227. *
  228. * @throws boost::system::system_error Thrown on failure.
  229. */
  230. void use_certificate_chain_file(const std::string& filename)
  231. {
  232. boost::system::error_code ec;
  233. service_.use_certificate_chain_file(impl_, filename, ec);
  234. boost::asio::detail::throw_error(ec);
  235. }
  236. /// Use a certificate chain from a file.
  237. /**
  238. * This function is used to load a certificate chain into the context from a
  239. * file.
  240. *
  241. * @param filename The name of the file containing the certificate. The file
  242. * must use the PEM format.
  243. *
  244. * @param ec Set to indicate what error occurred, if any.
  245. */
  246. boost::system::error_code use_certificate_chain_file(
  247. const std::string& filename, boost::system::error_code& ec)
  248. {
  249. return service_.use_certificate_chain_file(impl_, filename, ec);
  250. }
  251. /// Use a private key from a file.
  252. /**
  253. * This function is used to load a private key into the context from a file.
  254. *
  255. * @param filename The name of the file containing the private key.
  256. *
  257. * @param format The file format (ASN.1 or PEM).
  258. *
  259. * @throws boost::system::system_error Thrown on failure.
  260. */
  261. void use_private_key_file(const std::string& filename, file_format format)
  262. {
  263. boost::system::error_code ec;
  264. service_.use_private_key_file(impl_, filename, format, ec);
  265. boost::asio::detail::throw_error(ec);
  266. }
  267. /// Use a private key from a file.
  268. /**
  269. * This function is used to load a private key into the context from a file.
  270. *
  271. * @param filename The name of the file containing the private key.
  272. *
  273. * @param format The file format (ASN.1 or PEM).
  274. *
  275. * @param ec Set to indicate what error occurred, if any.
  276. */
  277. boost::system::error_code use_private_key_file(const std::string& filename,
  278. file_format format, boost::system::error_code& ec)
  279. {
  280. return service_.use_private_key_file(impl_, filename, format, ec);
  281. }
  282. /// Use an RSA private key from a file.
  283. /**
  284. * This function is used to load an RSA private key into the context from a
  285. * file.
  286. *
  287. * @param filename The name of the file containing the RSA private key.
  288. *
  289. * @param format The file format (ASN.1 or PEM).
  290. *
  291. * @throws boost::system::system_error Thrown on failure.
  292. */
  293. void use_rsa_private_key_file(const std::string& filename, file_format format)
  294. {
  295. boost::system::error_code ec;
  296. service_.use_rsa_private_key_file(impl_, filename, format, ec);
  297. boost::asio::detail::throw_error(ec);
  298. }
  299. /// Use an RSA private key from a file.
  300. /**
  301. * This function is used to load an RSA private key into the context from a
  302. * file.
  303. *
  304. * @param filename The name of the file containing the RSA private key.
  305. *
  306. * @param format The file format (ASN.1 or PEM).
  307. *
  308. * @param ec Set to indicate what error occurred, if any.
  309. */
  310. boost::system::error_code use_rsa_private_key_file(
  311. const std::string& filename, file_format format,
  312. boost::system::error_code& ec)
  313. {
  314. return service_.use_rsa_private_key_file(impl_, filename, format, ec);
  315. }
  316. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  317. /**
  318. * This function is used to load Diffie-Hellman parameters into the context
  319. * from a file.
  320. *
  321. * @param filename The name of the file containing the Diffie-Hellman
  322. * parameters. The file must use the PEM format.
  323. *
  324. * @throws boost::system::system_error Thrown on failure.
  325. */
  326. void use_tmp_dh_file(const std::string& filename)
  327. {
  328. boost::system::error_code ec;
  329. service_.use_tmp_dh_file(impl_, filename, ec);
  330. boost::asio::detail::throw_error(ec);
  331. }
  332. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  333. /**
  334. * This function is used to load Diffie-Hellman parameters into the context
  335. * from a file.
  336. *
  337. * @param filename The name of the file containing the Diffie-Hellman
  338. * parameters. The file must use the PEM format.
  339. *
  340. * @param ec Set to indicate what error occurred, if any.
  341. */
  342. boost::system::error_code use_tmp_dh_file(const std::string& filename,
  343. boost::system::error_code& ec)
  344. {
  345. return service_.use_tmp_dh_file(impl_, filename, ec);
  346. }
  347. /// Set the password callback.
  348. /**
  349. * This function is used to specify a callback function to obtain password
  350. * information about an encrypted key in PEM format.
  351. *
  352. * @param callback The function object to be used for obtaining the password.
  353. * The function signature of the handler must be:
  354. * @code std::string password_callback(
  355. * std::size_t max_length, // The maximum size for a password.
  356. * password_purpose purpose // Whether password is for reading or writing.
  357. * ); @endcode
  358. * The return value of the callback is a string containing the password.
  359. *
  360. * @throws boost::system::system_error Thrown on failure.
  361. */
  362. template <typename PasswordCallback>
  363. void set_password_callback(PasswordCallback callback)
  364. {
  365. boost::system::error_code ec;
  366. service_.set_password_callback(impl_, callback, ec);
  367. boost::asio::detail::throw_error(ec);
  368. }
  369. /// Set the password callback.
  370. /**
  371. * This function is used to specify a callback function to obtain password
  372. * information about an encrypted key in PEM format.
  373. *
  374. * @param callback The function object to be used for obtaining the password.
  375. * The function signature of the handler must be:
  376. * @code std::string password_callback(
  377. * std::size_t max_length, // The maximum size for a password.
  378. * password_purpose purpose // Whether password is for reading or writing.
  379. * ); @endcode
  380. * The return value of the callback is a string containing the password.
  381. *
  382. * @param ec Set to indicate what error occurred, if any.
  383. */
  384. template <typename PasswordCallback>
  385. boost::system::error_code set_password_callback(PasswordCallback callback,
  386. boost::system::error_code& ec)
  387. {
  388. return service_.set_password_callback(impl_, callback, ec);
  389. }
  390. private:
  391. /// The backend service implementation.
  392. service_type& service_;
  393. /// The underlying native implementation.
  394. impl_type impl_;
  395. };
  396. } // namespace ssl
  397. } // namespace asio
  398. } // namespace boost
  399. #include <boost/asio/detail/pop_options.hpp>
  400. #endif // BOOST_ASIO_SSL_BASIC_CONTEXT_HPP