basic_context.hpp 13 KB

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