/Core/Dependencies/Boost/boost/asio/ssl/context.hpp

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge · C++ Header · 533 lines · 110 code · 53 blank · 370 comment · 2 complexity · 88317b50a38763e51ad5c7826ccff21b MD5 · raw file

  1. //
  2. // ssl/context.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_SSL_CONTEXT_HPP
  11. #define BOOST_ASIO_SSL_CONTEXT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_ENABLE_OLD_SSL)
  17. # include <boost/asio/ssl/basic_context.hpp>
  18. # include <boost/asio/ssl/context_service.hpp>
  19. #else // defined(BOOST_ASIO_ENABLE_OLD_SSL)
  20. # include <string>
  21. # include <boost/asio/io_service.hpp>
  22. # include <boost/asio/ssl/context_base.hpp>
  23. # include <boost/asio/ssl/detail/openssl_types.hpp>
  24. # include <boost/asio/ssl/detail/openssl_init.hpp>
  25. # include <boost/asio/ssl/detail/password_callback.hpp>
  26. # include <boost/asio/ssl/detail/verify_callback.hpp>
  27. # include <boost/asio/ssl/verify_mode.hpp>
  28. #endif // defined(BOOST_ASIO_ENABLE_OLD_SSL)
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace ssl {
  33. #if defined(BOOST_ASIO_ENABLE_OLD_SSL)
  34. /// Typedef for the typical usage of context.
  35. typedef basic_context<context_service> context;
  36. #else // defined(BOOST_ASIO_ENABLE_OLD_SSL)
  37. class context
  38. : public context_base,
  39. private noncopyable
  40. {
  41. public:
  42. /// The native handle type of the SSL context.
  43. typedef SSL_CTX* native_handle_type;
  44. /// (Deprecated: Use native_handle_type.) The native type of the SSL context.
  45. typedef SSL_CTX* impl_type;
  46. /// Constructor.
  47. BOOST_ASIO_DECL explicit context(method m);
  48. /// Deprecated constructor taking a reference to an io_service object.
  49. BOOST_ASIO_DECL context(boost::asio::io_service&, method m);
  50. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  51. /// Move-construct a context from another.
  52. /**
  53. * This constructor moves an SSL context from one object to another.
  54. *
  55. * @param other The other context object from which the move will occur.
  56. *
  57. * @note Following the move, the following operations only are valid for the
  58. * moved-from object:
  59. * @li Destruction.
  60. * @li As a target for move-assignment.
  61. */
  62. BOOST_ASIO_DECL context(context&& other);
  63. /// Move-assign a context from another.
  64. /**
  65. * This assignment operator moves an SSL context from one object to another.
  66. *
  67. * @param other The other context object from which the move will occur.
  68. *
  69. * @note Following the move, the following operations only are valid for the
  70. * moved-from object:
  71. * @li Destruction.
  72. * @li As a target for move-assignment.
  73. */
  74. BOOST_ASIO_DECL context& operator=(context&& other);
  75. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  76. /// Destructor.
  77. BOOST_ASIO_DECL ~context();
  78. /// Get the underlying implementation in the native type.
  79. /**
  80. * This function may be used to obtain the underlying implementation of the
  81. * context. This is intended to allow access to context functionality that is
  82. * not otherwise provided.
  83. */
  84. BOOST_ASIO_DECL native_handle_type native_handle();
  85. /// (Deprecated: Use native_handle().) Get the underlying implementation in
  86. /// the native type.
  87. /**
  88. * This function may be used to obtain the underlying implementation of the
  89. * context. This is intended to allow access to context functionality that is
  90. * not otherwise provided.
  91. */
  92. BOOST_ASIO_DECL impl_type impl();
  93. /// Set options on the context.
  94. /**
  95. * This function may be used to configure the SSL options used by the context.
  96. *
  97. * @param o A bitmask of options. The available option values are defined in
  98. * the context_base class. The options are bitwise-ored with any existing
  99. * value for the options.
  100. *
  101. * @throws boost::system::system_error Thrown on failure.
  102. *
  103. * @note Calls @c SSL_CTX_set_options.
  104. */
  105. BOOST_ASIO_DECL void set_options(options o);
  106. /// Set options on the context.
  107. /**
  108. * This function may be used to configure the SSL options used by the context.
  109. *
  110. * @param o A bitmask of options. The available option values are defined in
  111. * the context_base class. The options are bitwise-ored with any existing
  112. * value for the options.
  113. *
  114. * @param ec Set to indicate what error occurred, if any.
  115. *
  116. * @note Calls @c SSL_CTX_set_options.
  117. */
  118. BOOST_ASIO_DECL boost::system::error_code set_options(options o,
  119. boost::system::error_code& ec);
  120. /// Set the peer verification mode.
  121. /**
  122. * This function may be used to configure the peer verification mode used by
  123. * the context.
  124. *
  125. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  126. * available values.
  127. *
  128. * @throws boost::system::system_error Thrown on failure.
  129. *
  130. * @note Calls @c SSL_CTX_set_verify.
  131. */
  132. BOOST_ASIO_DECL void set_verify_mode(verify_mode v);
  133. /// Set the peer verification mode.
  134. /**
  135. * This function may be used to configure the peer verification mode used by
  136. * the context.
  137. *
  138. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  139. * available values.
  140. *
  141. * @param ec Set to indicate what error occurred, if any.
  142. *
  143. * @note Calls @c SSL_CTX_set_verify.
  144. */
  145. BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
  146. verify_mode v, boost::system::error_code& ec);
  147. /// Set the callback used to verify peer certificates.
  148. /**
  149. * This function is used to specify a callback function that will be called
  150. * by the implementation when it needs to verify a peer certificate.
  151. *
  152. * @param callback The function object to be used for verifying a certificate.
  153. * The function signature of the handler must be:
  154. * @code bool verify_callback(
  155. * bool preverified, // True if the certificate passed pre-verification.
  156. * verify_context& ctx // The peer certificate and other context.
  157. * ); @endcode
  158. * The return value of the callback is true if the certificate has passed
  159. * verification, false otherwise.
  160. *
  161. * @throws boost::system::system_error Thrown on failure.
  162. *
  163. * @note Calls @c SSL_CTX_set_verify.
  164. */
  165. template <typename VerifyCallback>
  166. void set_verify_callback(VerifyCallback callback);
  167. /// Set the callback used to verify peer certificates.
  168. /**
  169. * This function is used to specify a callback function that will be called
  170. * by the implementation when it needs to verify a peer certificate.
  171. *
  172. * @param callback The function object to be used for verifying a certificate.
  173. * The function signature of the handler must be:
  174. * @code bool verify_callback(
  175. * bool preverified, // True if the certificate passed pre-verification.
  176. * verify_context& ctx // The peer certificate and other context.
  177. * ); @endcode
  178. * The return value of the callback is true if the certificate has passed
  179. * verification, false otherwise.
  180. *
  181. * @param ec Set to indicate what error occurred, if any.
  182. *
  183. * @note Calls @c SSL_CTX_set_verify.
  184. */
  185. template <typename VerifyCallback>
  186. boost::system::error_code set_verify_callback(VerifyCallback callback,
  187. boost::system::error_code& ec);
  188. /// Load a certification authority file for performing verification.
  189. /**
  190. * This function is used to load one or more trusted certification authorities
  191. * from a file.
  192. *
  193. * @param filename The name of a file containing certification authority
  194. * certificates in PEM format.
  195. *
  196. * @throws boost::system::system_error Thrown on failure.
  197. *
  198. * @note Calls @c SSL_CTX_load_verify_locations.
  199. */
  200. BOOST_ASIO_DECL void load_verify_file(const std::string& filename);
  201. /// Load a certification authority file for performing verification.
  202. /**
  203. * This function is used to load the certificates for one or more trusted
  204. * certification authorities from a file.
  205. *
  206. * @param filename The name of a file containing certification authority
  207. * certificates in PEM format.
  208. *
  209. * @param ec Set to indicate what error occurred, if any.
  210. *
  211. * @note Calls @c SSL_CTX_load_verify_locations.
  212. */
  213. BOOST_ASIO_DECL boost::system::error_code load_verify_file(
  214. const std::string& filename, boost::system::error_code& ec);
  215. /// Configures the context to use the default directories for finding
  216. /// certification authority certificates.
  217. /**
  218. * This function specifies that the context should use the default,
  219. * system-dependent directories for locating certification authority
  220. * certificates.
  221. *
  222. * @throws boost::system::system_error Thrown on failure.
  223. *
  224. * @note Calls @c SSL_CTX_set_default_verify_paths.
  225. */
  226. BOOST_ASIO_DECL void set_default_verify_paths();
  227. /// Configures the context to use the default directories for finding
  228. /// certification authority certificates.
  229. /**
  230. * This function specifies that the context should use the default,
  231. * system-dependent directories for locating certification authority
  232. * certificates.
  233. *
  234. * @param ec Set to indicate what error occurred, if any.
  235. *
  236. * @note Calls @c SSL_CTX_set_default_verify_paths.
  237. */
  238. BOOST_ASIO_DECL boost::system::error_code set_default_verify_paths(
  239. boost::system::error_code& ec);
  240. /// Add a directory containing certificate authority files to be used for
  241. /// performing verification.
  242. /**
  243. * This function is used to specify the name of a directory containing
  244. * certification authority certificates. Each file in the directory must
  245. * contain a single certificate. The files must be named using the subject
  246. * name's hash and an extension of ".0".
  247. *
  248. * @param path The name of a directory containing the certificates.
  249. *
  250. * @throws boost::system::system_error Thrown on failure.
  251. *
  252. * @note Calls @c SSL_CTX_load_verify_locations.
  253. */
  254. BOOST_ASIO_DECL void add_verify_path(const std::string& path);
  255. /// Add a directory containing certificate authority files to be used for
  256. /// performing verification.
  257. /**
  258. * This function is used to specify the name of a directory containing
  259. * certification authority certificates. Each file in the directory must
  260. * contain a single certificate. The files must be named using the subject
  261. * name's hash and an extension of ".0".
  262. *
  263. * @param path The name of a directory containing the certificates.
  264. *
  265. * @param ec Set to indicate what error occurred, if any.
  266. *
  267. * @note Calls @c SSL_CTX_load_verify_locations.
  268. */
  269. BOOST_ASIO_DECL boost::system::error_code add_verify_path(
  270. const std::string& path, boost::system::error_code& ec);
  271. /// Use a certificate from a file.
  272. /**
  273. * This function is used to load a certificate into the context from a file.
  274. *
  275. * @param filename The name of the file containing the certificate.
  276. *
  277. * @param format The file format (ASN.1 or PEM).
  278. *
  279. * @throws boost::system::system_error Thrown on failure.
  280. *
  281. * @note Calls @c SSL_CTX_use_certificate_file.
  282. */
  283. BOOST_ASIO_DECL void use_certificate_file(
  284. const std::string& filename, file_format format);
  285. /// Use a certificate from a file.
  286. /**
  287. * This function is used to load a certificate into the context from a file.
  288. *
  289. * @param filename The name of the file containing the certificate.
  290. *
  291. * @param format The file format (ASN.1 or PEM).
  292. *
  293. * @param ec Set to indicate what error occurred, if any.
  294. *
  295. * @note Calls @c SSL_CTX_use_certificate_file.
  296. */
  297. BOOST_ASIO_DECL boost::system::error_code use_certificate_file(
  298. const std::string& filename, file_format format,
  299. boost::system::error_code& ec);
  300. /// Use a certificate chain from a file.
  301. /**
  302. * This function is used to load a certificate chain into the context from a
  303. * file.
  304. *
  305. * @param filename The name of the file containing the certificate. The file
  306. * must use the PEM format.
  307. *
  308. * @throws boost::system::system_error Thrown on failure.
  309. *
  310. * @note Calls @c SSL_CTX_use_certificate_chain_file.
  311. */
  312. BOOST_ASIO_DECL void use_certificate_chain_file(const std::string& filename);
  313. /// Use a certificate chain from a file.
  314. /**
  315. * This function is used to load a certificate chain into the context from a
  316. * file.
  317. *
  318. * @param filename The name of the file containing the certificate. The file
  319. * must use the PEM format.
  320. *
  321. * @param ec Set to indicate what error occurred, if any.
  322. *
  323. * @note Calls @c SSL_CTX_use_certificate_chain_file.
  324. */
  325. BOOST_ASIO_DECL boost::system::error_code use_certificate_chain_file(
  326. const std::string& filename, boost::system::error_code& ec);
  327. /// Use a private key from a file.
  328. /**
  329. * This function is used to load a private key into the context from a file.
  330. *
  331. * @param filename The name of the file containing the private key.
  332. *
  333. * @param format The file format (ASN.1 or PEM).
  334. *
  335. * @throws boost::system::system_error Thrown on failure.
  336. *
  337. * @note Calls @c SSL_CTX_use_PrivateKey_file.
  338. */
  339. BOOST_ASIO_DECL void use_private_key_file(
  340. const std::string& filename, file_format format);
  341. /// Use a private key from a file.
  342. /**
  343. * This function is used to load a private key into the context from a file.
  344. *
  345. * @param filename The name of the file containing the private key.
  346. *
  347. * @param format The file format (ASN.1 or PEM).
  348. *
  349. * @param ec Set to indicate what error occurred, if any.
  350. *
  351. * @note Calls @c SSL_CTX_use_PrivateKey_file.
  352. */
  353. BOOST_ASIO_DECL boost::system::error_code use_private_key_file(
  354. const std::string& filename, file_format format,
  355. boost::system::error_code& ec);
  356. /// Use an RSA private key from a file.
  357. /**
  358. * This function is used to load an RSA private key into the context from a
  359. * file.
  360. *
  361. * @param filename The name of the file containing the RSA private key.
  362. *
  363. * @param format The file format (ASN.1 or PEM).
  364. *
  365. * @throws boost::system::system_error Thrown on failure.
  366. *
  367. * @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
  368. */
  369. BOOST_ASIO_DECL void use_rsa_private_key_file(
  370. const std::string& filename, file_format format);
  371. /// Use an RSA private key from a file.
  372. /**
  373. * This function is used to load an RSA private key into the context from a
  374. * file.
  375. *
  376. * @param filename The name of the file containing the RSA private key.
  377. *
  378. * @param format The file format (ASN.1 or PEM).
  379. *
  380. * @param ec Set to indicate what error occurred, if any.
  381. *
  382. * @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
  383. */
  384. BOOST_ASIO_DECL boost::system::error_code use_rsa_private_key_file(
  385. const std::string& filename, file_format format,
  386. boost::system::error_code& ec);
  387. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  388. /**
  389. * This function is used to load Diffie-Hellman parameters into the context
  390. * from a file.
  391. *
  392. * @param filename The name of the file containing the Diffie-Hellman
  393. * parameters. The file must use the PEM format.
  394. *
  395. * @throws boost::system::system_error Thrown on failure.
  396. *
  397. * @note Calls @c SSL_CTX_set_tmp_dh.
  398. */
  399. BOOST_ASIO_DECL void use_tmp_dh_file(const std::string& filename);
  400. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  401. /**
  402. * This function is used to load Diffie-Hellman parameters into the context
  403. * from a file.
  404. *
  405. * @param filename The name of the file containing the Diffie-Hellman
  406. * parameters. The file must use the PEM format.
  407. *
  408. * @param ec Set to indicate what error occurred, if any.
  409. *
  410. * @note Calls @c SSL_CTX_set_tmp_dh.
  411. */
  412. BOOST_ASIO_DECL boost::system::error_code use_tmp_dh_file(
  413. const std::string& filename, boost::system::error_code& ec);
  414. /// Set the password callback.
  415. /**
  416. * This function is used to specify a callback function to obtain password
  417. * information about an encrypted key in PEM format.
  418. *
  419. * @param callback The function object to be used for obtaining the password.
  420. * The function signature of the handler must be:
  421. * @code std::string password_callback(
  422. * std::size_t max_length, // The maximum size for a password.
  423. * password_purpose purpose // Whether password is for reading or writing.
  424. * ); @endcode
  425. * The return value of the callback is a string containing the password.
  426. *
  427. * @throws boost::system::system_error Thrown on failure.
  428. *
  429. * @note Calls @c SSL_CTX_set_default_passwd_cb.
  430. */
  431. template <typename PasswordCallback>
  432. void set_password_callback(PasswordCallback callback);
  433. /// Set the password callback.
  434. /**
  435. * This function is used to specify a callback function to obtain password
  436. * information about an encrypted key in PEM format.
  437. *
  438. * @param callback The function object to be used for obtaining the password.
  439. * The function signature of the handler must be:
  440. * @code std::string password_callback(
  441. * std::size_t max_length, // The maximum size for a password.
  442. * password_purpose purpose // Whether password is for reading or writing.
  443. * ); @endcode
  444. * The return value of the callback is a string containing the password.
  445. *
  446. * @param ec Set to indicate what error occurred, if any.
  447. *
  448. * @note Calls @c SSL_CTX_set_default_passwd_cb.
  449. */
  450. template <typename PasswordCallback>
  451. boost::system::error_code set_password_callback(PasswordCallback callback,
  452. boost::system::error_code& ec);
  453. private:
  454. // Helper function used to set a peer certificate verification callback.
  455. BOOST_ASIO_DECL boost::system::error_code do_set_verify_callback(
  456. detail::verify_callback_base* callback, boost::system::error_code& ec);
  457. // Callback used when the SSL implementation wants to verify a certificate.
  458. BOOST_ASIO_DECL static int verify_callback_function(
  459. int preverified, X509_STORE_CTX* ctx);
  460. // Helper function used to set a password callback.
  461. BOOST_ASIO_DECL boost::system::error_code do_set_password_callback(
  462. detail::password_callback_base* callback, boost::system::error_code& ec);
  463. // Callback used when the SSL implementation wants a password.
  464. BOOST_ASIO_DECL static int password_callback_function(
  465. char* buf, int size, int purpose, void* data);
  466. // The underlying native implementation.
  467. native_handle_type handle_;
  468. // Ensure openssl is initialised.
  469. boost::asio::ssl::detail::openssl_init<> init_;
  470. };
  471. #endif // defined(BOOST_ASIO_ENABLE_OLD_SSL)
  472. } // namespace ssl
  473. } // namespace asio
  474. } // namespace boost
  475. #include <boost/asio/detail/pop_options.hpp>
  476. #include <boost/asio/ssl/impl/context.hpp>
  477. #if defined(BOOST_ASIO_HEADER_ONLY)
  478. # include <boost/asio/ssl/impl/context.ipp>
  479. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  480. #endif // BOOST_ASIO_SSL_CONTEXT_HPP