context.hpp 25 KB

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