context.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //
  2. // ssl/context.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #include <string>
  17. #include <boost/asio/buffer.hpp>
  18. #include <boost/asio/io_context.hpp>
  19. #include <boost/asio/ssl/context_base.hpp>
  20. #include <boost/asio/ssl/detail/openssl_types.hpp>
  21. #include <boost/asio/ssl/detail/openssl_init.hpp>
  22. #include <boost/asio/ssl/detail/password_callback.hpp>
  23. #include <boost/asio/ssl/detail/verify_callback.hpp>
  24. #include <boost/asio/ssl/verify_mode.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace ssl {
  29. class context
  30. : public context_base,
  31. private noncopyable
  32. {
  33. public:
  34. /// The native handle type of the SSL context.
  35. typedef SSL_CTX* native_handle_type;
  36. /// Constructor.
  37. BOOST_ASIO_DECL explicit context(method m);
  38. /// Construct to take ownership of a native handle.
  39. BOOST_ASIO_DECL explicit context(native_handle_type native_handle);
  40. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  41. /// Move-construct a context from another.
  42. /**
  43. * This constructor moves an SSL context from one object to another.
  44. *
  45. * @param other The other context object from which the move will occur.
  46. *
  47. * @note Following the move, the following operations only are valid for the
  48. * moved-from object:
  49. * @li Destruction.
  50. * @li As a target for move-assignment.
  51. */
  52. BOOST_ASIO_DECL context(context&& other);
  53. /// Move-assign a context from another.
  54. /**
  55. * This assignment operator moves an SSL context from one object to another.
  56. *
  57. * @param other The other context object from which the move will occur.
  58. *
  59. * @note Following the move, the following operations only are valid for the
  60. * moved-from object:
  61. * @li Destruction.
  62. * @li As a target for move-assignment.
  63. */
  64. BOOST_ASIO_DECL context& operator=(context&& other);
  65. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  66. /// Destructor.
  67. BOOST_ASIO_DECL ~context();
  68. /// Get the underlying implementation in the native type.
  69. /**
  70. * This function may be used to obtain the underlying implementation of the
  71. * context. This is intended to allow access to context functionality that is
  72. * not otherwise provided.
  73. */
  74. BOOST_ASIO_DECL native_handle_type native_handle();
  75. /// Clear options on the context.
  76. /**
  77. * This function may be used to configure the SSL options used by the context.
  78. *
  79. * @param o A bitmask of options. The available option values are defined in
  80. * the context_base class. The specified options, if currently enabled on the
  81. * context, are cleared.
  82. *
  83. * @throws boost::system::system_error Thrown on failure.
  84. *
  85. * @note Calls @c SSL_CTX_clear_options.
  86. */
  87. BOOST_ASIO_DECL void clear_options(options o);
  88. /// Clear options on the context.
  89. /**
  90. * This function may be used to configure the SSL options used by the context.
  91. *
  92. * @param o A bitmask of options. The available option values are defined in
  93. * the context_base class. The specified options, if currently enabled on the
  94. * context, are cleared.
  95. *
  96. * @param ec Set to indicate what error occurred, if any.
  97. *
  98. * @note Calls @c SSL_CTX_clear_options.
  99. */
  100. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID clear_options(options o,
  101. boost::system::error_code& ec);
  102. /// Set options on the context.
  103. /**
  104. * This function may be used to configure the SSL options used by the context.
  105. *
  106. * @param o A bitmask of options. The available option values are defined in
  107. * the context_base class. The options are bitwise-ored with any existing
  108. * value for the options.
  109. *
  110. * @throws boost::system::system_error Thrown on failure.
  111. *
  112. * @note Calls @c SSL_CTX_set_options.
  113. */
  114. BOOST_ASIO_DECL void set_options(options o);
  115. /// Set options on the context.
  116. /**
  117. * This function may be used to configure the SSL options used by the context.
  118. *
  119. * @param o A bitmask of options. The available option values are defined in
  120. * the context_base class. The options are bitwise-ored with any existing
  121. * value for the options.
  122. *
  123. * @param ec Set to indicate what error occurred, if any.
  124. *
  125. * @note Calls @c SSL_CTX_set_options.
  126. */
  127. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_options(options o,
  128. boost::system::error_code& ec);
  129. /// Set the peer verification mode.
  130. /**
  131. * This function may be used to configure the peer verification mode used by
  132. * the context.
  133. *
  134. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  135. * available values.
  136. *
  137. * @throws boost::system::system_error Thrown on failure.
  138. *
  139. * @note Calls @c SSL_CTX_set_verify.
  140. */
  141. BOOST_ASIO_DECL void set_verify_mode(verify_mode v);
  142. /// Set the peer verification mode.
  143. /**
  144. * This function may be used to configure the peer verification mode used by
  145. * the context.
  146. *
  147. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  148. * available values.
  149. *
  150. * @param ec Set to indicate what error occurred, if any.
  151. *
  152. * @note Calls @c SSL_CTX_set_verify.
  153. */
  154. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_verify_mode(
  155. verify_mode v, boost::system::error_code& ec);
  156. /// Set the peer verification depth.
  157. /**
  158. * This function may be used to configure the maximum verification depth
  159. * allowed by the context.
  160. *
  161. * @param depth Maximum depth for the certificate chain verification that
  162. * shall be allowed.
  163. *
  164. * @throws boost::system::system_error Thrown on failure.
  165. *
  166. * @note Calls @c SSL_CTX_set_verify_depth.
  167. */
  168. BOOST_ASIO_DECL void set_verify_depth(int depth);
  169. /// Set the peer verification depth.
  170. /**
  171. * This function may be used to configure the maximum verification depth
  172. * allowed by the context.
  173. *
  174. * @param depth Maximum depth for the certificate chain verification that
  175. * shall be allowed.
  176. *
  177. * @param ec Set to indicate what error occurred, if any.
  178. *
  179. * @note Calls @c SSL_CTX_set_verify_depth.
  180. */
  181. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_verify_depth(
  182. int depth, boost::system::error_code& ec);
  183. /// Set the callback used to verify peer certificates.
  184. /**
  185. * This function is used to specify a callback function that will be called
  186. * by the implementation when it needs to verify a peer certificate.
  187. *
  188. * @param callback The function object to be used for verifying a certificate.
  189. * The function signature of the handler must be:
  190. * @code bool verify_callback(
  191. * bool preverified, // True if the certificate passed pre-verification.
  192. * verify_context& ctx // The peer certificate and other context.
  193. * ); @endcode
  194. * The return value of the callback is true if the certificate has passed
  195. * verification, false otherwise.
  196. *
  197. * @throws boost::system::system_error Thrown on failure.
  198. *
  199. * @note Calls @c SSL_CTX_set_verify.
  200. */
  201. template <typename VerifyCallback>
  202. void set_verify_callback(VerifyCallback callback);
  203. /// Set the callback used to verify peer certificates.
  204. /**
  205. * This function is used to specify a callback function that will be called
  206. * by the implementation when it needs to verify a peer certificate.
  207. *
  208. * @param callback The function object to be used for verifying a certificate.
  209. * The function signature of the handler must be:
  210. * @code bool verify_callback(
  211. * bool preverified, // True if the certificate passed pre-verification.
  212. * verify_context& ctx // The peer certificate and other context.
  213. * ); @endcode
  214. * The return value of the callback is true if the certificate has passed
  215. * verification, false otherwise.
  216. *
  217. * @param ec Set to indicate what error occurred, if any.
  218. *
  219. * @note Calls @c SSL_CTX_set_verify.
  220. */
  221. template <typename VerifyCallback>
  222. BOOST_ASIO_SYNC_OP_VOID set_verify_callback(VerifyCallback callback,
  223. boost::system::error_code& ec);
  224. /// Load a certification authority file for performing verification.
  225. /**
  226. * This function is used to load one or more trusted certification authorities
  227. * from a file.
  228. *
  229. * @param filename The name of a file containing certification authority
  230. * certificates in PEM format.
  231. *
  232. * @throws boost::system::system_error Thrown on failure.
  233. *
  234. * @note Calls @c SSL_CTX_load_verify_locations.
  235. */
  236. BOOST_ASIO_DECL void load_verify_file(const std::string& filename);
  237. /// Load a certification authority file for performing verification.
  238. /**
  239. * This function is used to load the certificates for one or more trusted
  240. * certification authorities from a file.
  241. *
  242. * @param filename The name of a file containing certification authority
  243. * certificates in PEM format.
  244. *
  245. * @param ec Set to indicate what error occurred, if any.
  246. *
  247. * @note Calls @c SSL_CTX_load_verify_locations.
  248. */
  249. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID load_verify_file(
  250. const std::string& filename, boost::system::error_code& ec);
  251. /// Add certification authority for performing verification.
  252. /**
  253. * This function is used to add one trusted certification authority
  254. * from a memory buffer.
  255. *
  256. * @param ca The buffer containing the certification authority certificate.
  257. * The certificate must use the PEM format.
  258. *
  259. * @throws boost::system::system_error Thrown on failure.
  260. *
  261. * @note Calls @c SSL_CTX_get_cert_store and @c X509_STORE_add_cert.
  262. */
  263. BOOST_ASIO_DECL void add_certificate_authority(const const_buffer& ca);
  264. /// Add certification authority for performing verification.
  265. /**
  266. * This function is used to add one trusted certification authority
  267. * from a memory buffer.
  268. *
  269. * @param ca The buffer containing the certification authority certificate.
  270. * The certificate must use the PEM format.
  271. *
  272. * @param ec Set to indicate what error occurred, if any.
  273. *
  274. * @note Calls @c SSL_CTX_get_cert_store and @c X509_STORE_add_cert.
  275. */
  276. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID add_certificate_authority(
  277. const const_buffer& ca, boost::system::error_code& ec);
  278. /// Configures the context to use the default directories for finding
  279. /// certification authority certificates.
  280. /**
  281. * This function specifies that the context should use the default,
  282. * system-dependent directories for locating certification authority
  283. * certificates.
  284. *
  285. * @throws boost::system::system_error Thrown on failure.
  286. *
  287. * @note Calls @c SSL_CTX_set_default_verify_paths.
  288. */
  289. BOOST_ASIO_DECL void set_default_verify_paths();
  290. /// Configures the context to use the default directories for finding
  291. /// certification authority certificates.
  292. /**
  293. * This function specifies that the context should use the default,
  294. * system-dependent directories for locating certification authority
  295. * certificates.
  296. *
  297. * @param ec Set to indicate what error occurred, if any.
  298. *
  299. * @note Calls @c SSL_CTX_set_default_verify_paths.
  300. */
  301. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_default_verify_paths(
  302. boost::system::error_code& ec);
  303. /// Add a directory containing certificate authority files to be used for
  304. /// performing verification.
  305. /**
  306. * This function is used to specify the name of a directory containing
  307. * certification authority certificates. Each file in the directory must
  308. * contain a single certificate. The files must be named using the subject
  309. * name's hash and an extension of ".0".
  310. *
  311. * @param path The name of a directory containing the certificates.
  312. *
  313. * @throws boost::system::system_error Thrown on failure.
  314. *
  315. * @note Calls @c SSL_CTX_load_verify_locations.
  316. */
  317. BOOST_ASIO_DECL void add_verify_path(const std::string& path);
  318. /// Add a directory containing certificate authority files to be used for
  319. /// performing verification.
  320. /**
  321. * This function is used to specify the name of a directory containing
  322. * certification authority certificates. Each file in the directory must
  323. * contain a single certificate. The files must be named using the subject
  324. * name's hash and an extension of ".0".
  325. *
  326. * @param path The name of a directory containing the certificates.
  327. *
  328. * @param ec Set to indicate what error occurred, if any.
  329. *
  330. * @note Calls @c SSL_CTX_load_verify_locations.
  331. */
  332. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID add_verify_path(
  333. const std::string& path, boost::system::error_code& ec);
  334. /// Use a certificate from a memory buffer.
  335. /**
  336. * This function is used to load a certificate into the context from a buffer.
  337. *
  338. * @param certificate The buffer containing the certificate.
  339. *
  340. * @param format The certificate format (ASN.1 or PEM).
  341. *
  342. * @throws boost::system::system_error Thrown on failure.
  343. *
  344. * @note Calls @c SSL_CTX_use_certificate or SSL_CTX_use_certificate_ASN1.
  345. */
  346. BOOST_ASIO_DECL void use_certificate(
  347. const const_buffer& certificate, file_format format);
  348. /// Use a certificate from a memory buffer.
  349. /**
  350. * This function is used to load a certificate into the context from a buffer.
  351. *
  352. * @param certificate The buffer containing the certificate.
  353. *
  354. * @param format The certificate format (ASN.1 or PEM).
  355. *
  356. * @param ec Set to indicate what error occurred, if any.
  357. *
  358. * @note Calls @c SSL_CTX_use_certificate or SSL_CTX_use_certificate_ASN1.
  359. */
  360. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate(
  361. const const_buffer& certificate, file_format format,
  362. boost::system::error_code& ec);
  363. /// Use a certificate from a file.
  364. /**
  365. * This function is used to load a certificate into the context from a file.
  366. *
  367. * @param filename The name of the file containing the certificate.
  368. *
  369. * @param format The file format (ASN.1 or PEM).
  370. *
  371. * @throws boost::system::system_error Thrown on failure.
  372. *
  373. * @note Calls @c SSL_CTX_use_certificate_file.
  374. */
  375. BOOST_ASIO_DECL void use_certificate_file(
  376. const std::string& filename, file_format format);
  377. /// Use a certificate from a file.
  378. /**
  379. * This function is used to load a certificate into the context from a file.
  380. *
  381. * @param filename The name of the file containing the certificate.
  382. *
  383. * @param format The file format (ASN.1 or PEM).
  384. *
  385. * @param ec Set to indicate what error occurred, if any.
  386. *
  387. * @note Calls @c SSL_CTX_use_certificate_file.
  388. */
  389. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_file(
  390. const std::string& filename, file_format format,
  391. boost::system::error_code& ec);
  392. /// Use a certificate chain from a memory buffer.
  393. /**
  394. * This function is used to load a certificate chain into the context from a
  395. * buffer.
  396. *
  397. * @param chain The buffer containing the certificate chain. The certificate
  398. * chain must use the PEM format.
  399. *
  400. * @throws boost::system::system_error Thrown on failure.
  401. *
  402. * @note Calls @c SSL_CTX_use_certificate and SSL_CTX_add_extra_chain_cert.
  403. */
  404. BOOST_ASIO_DECL void use_certificate_chain(const const_buffer& chain);
  405. /// Use a certificate chain from a memory buffer.
  406. /**
  407. * This function is used to load a certificate chain into the context from a
  408. * buffer.
  409. *
  410. * @param chain The buffer containing the certificate chain. The certificate
  411. * chain must use the PEM format.
  412. *
  413. * @param ec Set to indicate what error occurred, if any.
  414. *
  415. * @note Calls @c SSL_CTX_use_certificate and SSL_CTX_add_extra_chain_cert.
  416. */
  417. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_chain(
  418. const const_buffer& chain, boost::system::error_code& ec);
  419. /// Use a certificate chain from a file.
  420. /**
  421. * This function is used to load a certificate chain into the context from a
  422. * file.
  423. *
  424. * @param filename The name of the file containing the certificate. The file
  425. * must use the PEM format.
  426. *
  427. * @throws boost::system::system_error Thrown on failure.
  428. *
  429. * @note Calls @c SSL_CTX_use_certificate_chain_file.
  430. */
  431. BOOST_ASIO_DECL void use_certificate_chain_file(const std::string& filename);
  432. /// Use a certificate chain from a file.
  433. /**
  434. * This function is used to load a certificate chain into the context from a
  435. * file.
  436. *
  437. * @param filename The name of the file containing the certificate. The file
  438. * must use the PEM format.
  439. *
  440. * @param ec Set to indicate what error occurred, if any.
  441. *
  442. * @note Calls @c SSL_CTX_use_certificate_chain_file.
  443. */
  444. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_chain_file(
  445. const std::string& filename, boost::system::error_code& ec);
  446. /// Use a private key from a memory buffer.
  447. /**
  448. * This function is used to load a private key into the context from a buffer.
  449. *
  450. * @param private_key The buffer containing the private key.
  451. *
  452. * @param format The private key format (ASN.1 or PEM).
  453. *
  454. * @throws boost::system::system_error Thrown on failure.
  455. *
  456. * @note Calls @c SSL_CTX_use_PrivateKey or SSL_CTX_use_PrivateKey_ASN1.
  457. */
  458. BOOST_ASIO_DECL void use_private_key(
  459. const const_buffer& private_key, file_format format);
  460. /// Use a private key from a memory buffer.
  461. /**
  462. * This function is used to load a private key into the context from a buffer.
  463. *
  464. * @param private_key The buffer containing the private key.
  465. *
  466. * @param format The private key format (ASN.1 or PEM).
  467. *
  468. * @param ec Set to indicate what error occurred, if any.
  469. *
  470. * @note Calls @c SSL_CTX_use_PrivateKey or SSL_CTX_use_PrivateKey_ASN1.
  471. */
  472. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_private_key(
  473. const const_buffer& private_key, file_format format,
  474. boost::system::error_code& ec);
  475. /// Use a private key from a file.
  476. /**
  477. * This function is used to load a private key into the context from a file.
  478. *
  479. * @param filename The name of the file containing the private key.
  480. *
  481. * @param format The file format (ASN.1 or PEM).
  482. *
  483. * @throws boost::system::system_error Thrown on failure.
  484. *
  485. * @note Calls @c SSL_CTX_use_PrivateKey_file.
  486. */
  487. BOOST_ASIO_DECL void use_private_key_file(
  488. const std::string& filename, file_format format);
  489. /// Use a private key from a file.
  490. /**
  491. * This function is used to load a private key into the context from a file.
  492. *
  493. * @param filename The name of the file containing the private key.
  494. *
  495. * @param format The file format (ASN.1 or PEM).
  496. *
  497. * @param ec Set to indicate what error occurred, if any.
  498. *
  499. * @note Calls @c SSL_CTX_use_PrivateKey_file.
  500. */
  501. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_private_key_file(
  502. const std::string& filename, file_format format,
  503. boost::system::error_code& ec);
  504. /// Use an RSA private key from a memory buffer.
  505. /**
  506. * This function is used to load an RSA private key into the context from a
  507. * buffer.
  508. *
  509. * @param private_key The buffer containing the RSA private key.
  510. *
  511. * @param format The private key format (ASN.1 or PEM).
  512. *
  513. * @throws boost::system::system_error Thrown on failure.
  514. *
  515. * @note Calls @c SSL_CTX_use_RSAPrivateKey or SSL_CTX_use_RSAPrivateKey_ASN1.
  516. */
  517. BOOST_ASIO_DECL void use_rsa_private_key(
  518. const const_buffer& private_key, file_format format);
  519. /// Use an RSA private key from a memory buffer.
  520. /**
  521. * This function is used to load an RSA private key into the context from a
  522. * buffer.
  523. *
  524. * @param private_key The buffer containing the RSA private key.
  525. *
  526. * @param format The private key format (ASN.1 or PEM).
  527. *
  528. * @param ec Set to indicate what error occurred, if any.
  529. *
  530. * @note Calls @c SSL_CTX_use_RSAPrivateKey or SSL_CTX_use_RSAPrivateKey_ASN1.
  531. */
  532. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_rsa_private_key(
  533. const const_buffer& private_key, file_format format,
  534. boost::system::error_code& ec);
  535. /// Use an RSA private key from a file.
  536. /**
  537. * This function is used to load an RSA private key into the context from a
  538. * file.
  539. *
  540. * @param filename The name of the file containing the RSA private key.
  541. *
  542. * @param format The file format (ASN.1 or PEM).
  543. *
  544. * @throws boost::system::system_error Thrown on failure.
  545. *
  546. * @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
  547. */
  548. BOOST_ASIO_DECL void use_rsa_private_key_file(
  549. const std::string& filename, file_format format);
  550. /// Use an RSA private key from a file.
  551. /**
  552. * This function is used to load an RSA private key into the context from a
  553. * file.
  554. *
  555. * @param filename The name of the file containing the RSA private key.
  556. *
  557. * @param format The file format (ASN.1 or PEM).
  558. *
  559. * @param ec Set to indicate what error occurred, if any.
  560. *
  561. * @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
  562. */
  563. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_rsa_private_key_file(
  564. const std::string& filename, file_format format,
  565. boost::system::error_code& ec);
  566. /// Use the specified memory buffer to obtain the temporary Diffie-Hellman
  567. /// parameters.
  568. /**
  569. * This function is used to load Diffie-Hellman parameters into the context
  570. * from a buffer.
  571. *
  572. * @param dh The memory buffer containing the Diffie-Hellman parameters. The
  573. * buffer must use the PEM format.
  574. *
  575. * @throws boost::system::system_error Thrown on failure.
  576. *
  577. * @note Calls @c SSL_CTX_set_tmp_dh.
  578. */
  579. BOOST_ASIO_DECL void use_tmp_dh(const const_buffer& dh);
  580. /// Use the specified memory buffer to obtain the temporary Diffie-Hellman
  581. /// parameters.
  582. /**
  583. * This function is used to load Diffie-Hellman parameters into the context
  584. * from a buffer.
  585. *
  586. * @param dh The memory buffer containing the Diffie-Hellman parameters. The
  587. * buffer must use the PEM format.
  588. *
  589. * @param ec Set to indicate what error occurred, if any.
  590. *
  591. * @note Calls @c SSL_CTX_set_tmp_dh.
  592. */
  593. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_tmp_dh(
  594. const const_buffer& dh, boost::system::error_code& ec);
  595. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  596. /**
  597. * This function is used to load Diffie-Hellman parameters into the context
  598. * from a file.
  599. *
  600. * @param filename The name of the file containing the Diffie-Hellman
  601. * parameters. The file must use the PEM format.
  602. *
  603. * @throws boost::system::system_error Thrown on failure.
  604. *
  605. * @note Calls @c SSL_CTX_set_tmp_dh.
  606. */
  607. BOOST_ASIO_DECL void use_tmp_dh_file(const std::string& filename);
  608. /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
  609. /**
  610. * This function is used to load Diffie-Hellman parameters into the context
  611. * from a file.
  612. *
  613. * @param filename The name of the file containing the Diffie-Hellman
  614. * parameters. The file must use the PEM format.
  615. *
  616. * @param ec Set to indicate what error occurred, if any.
  617. *
  618. * @note Calls @c SSL_CTX_set_tmp_dh.
  619. */
  620. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_tmp_dh_file(
  621. const std::string& filename, boost::system::error_code& ec);
  622. /// Set the password callback.
  623. /**
  624. * This function is used to specify a callback function to obtain password
  625. * information about an encrypted key in PEM format.
  626. *
  627. * @param callback The function object to be used for obtaining the password.
  628. * The function signature of the handler must be:
  629. * @code std::string password_callback(
  630. * std::size_t max_length, // The maximum size for a password.
  631. * password_purpose purpose // Whether password is for reading or writing.
  632. * ); @endcode
  633. * The return value of the callback is a string containing the password.
  634. *
  635. * @throws boost::system::system_error Thrown on failure.
  636. *
  637. * @note Calls @c SSL_CTX_set_default_passwd_cb.
  638. */
  639. template <typename PasswordCallback>
  640. void set_password_callback(PasswordCallback callback);
  641. /// Set the password callback.
  642. /**
  643. * This function is used to specify a callback function to obtain password
  644. * information about an encrypted key in PEM format.
  645. *
  646. * @param callback The function object to be used for obtaining the password.
  647. * The function signature of the handler must be:
  648. * @code std::string password_callback(
  649. * std::size_t max_length, // The maximum size for a password.
  650. * password_purpose purpose // Whether password is for reading or writing.
  651. * ); @endcode
  652. * The return value of the callback is a string containing the password.
  653. *
  654. * @param ec Set to indicate what error occurred, if any.
  655. *
  656. * @note Calls @c SSL_CTX_set_default_passwd_cb.
  657. */
  658. template <typename PasswordCallback>
  659. BOOST_ASIO_SYNC_OP_VOID set_password_callback(PasswordCallback callback,
  660. boost::system::error_code& ec);
  661. private:
  662. struct bio_cleanup;
  663. struct x509_cleanup;
  664. struct evp_pkey_cleanup;
  665. struct rsa_cleanup;
  666. struct dh_cleanup;
  667. // Helper function used to set a peer certificate verification callback.
  668. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_set_verify_callback(
  669. detail::verify_callback_base* callback, boost::system::error_code& ec);
  670. // Callback used when the SSL implementation wants to verify a certificate.
  671. BOOST_ASIO_DECL static int verify_callback_function(
  672. int preverified, X509_STORE_CTX* ctx);
  673. // Helper function used to set a password callback.
  674. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_set_password_callback(
  675. detail::password_callback_base* callback, boost::system::error_code& ec);
  676. // Callback used when the SSL implementation wants a password.
  677. BOOST_ASIO_DECL static int password_callback_function(
  678. char* buf, int size, int purpose, void* data);
  679. // Helper function to set the temporary Diffie-Hellman parameters from a BIO.
  680. BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_use_tmp_dh(
  681. BIO* bio, boost::system::error_code& ec);
  682. // Helper function to make a BIO from a memory buffer.
  683. BOOST_ASIO_DECL BIO* make_buffer_bio(const const_buffer& b);
  684. // The underlying native implementation.
  685. native_handle_type handle_;
  686. // Ensure openssl is initialised.
  687. boost::asio::ssl::detail::openssl_init<> init_;
  688. };
  689. } // namespace ssl
  690. } // namespace asio
  691. } // namespace boost
  692. #include <boost/asio/detail/pop_options.hpp>
  693. #include <boost/asio/ssl/impl/context.hpp>
  694. #if defined(BOOST_ASIO_HEADER_ONLY)
  695. # include <boost/asio/ssl/impl/context.ipp>
  696. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  697. #endif // BOOST_ASIO_SSL_CONTEXT_HPP