context.hpp 26 KB

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