context.ipp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. //
  2. // ssl/impl/context.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_SSL_IMPL_CONTEXT_IPP
  12. #define BOOST_ASIO_SSL_IMPL_CONTEXT_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <cstring>
  18. #include <boost/asio/detail/throw_error.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/ssl/context.hpp>
  21. #include <boost/asio/ssl/error.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace ssl {
  26. struct context::bio_cleanup
  27. {
  28. BIO* p;
  29. ~bio_cleanup() { if (p) ::BIO_free(p); }
  30. };
  31. struct context::x509_cleanup
  32. {
  33. X509* p;
  34. ~x509_cleanup() { if (p) ::X509_free(p); }
  35. };
  36. struct context::evp_pkey_cleanup
  37. {
  38. EVP_PKEY* p;
  39. ~evp_pkey_cleanup() { if (p) ::EVP_PKEY_free(p); }
  40. };
  41. #if (OPENSSL_VERSION_NUMBER < 0x30000000L)
  42. struct context::rsa_cleanup
  43. {
  44. RSA* p;
  45. ~rsa_cleanup() { if (p) ::RSA_free(p); }
  46. };
  47. struct context::dh_cleanup
  48. {
  49. DH* p;
  50. ~dh_cleanup() { if (p) ::DH_free(p); }
  51. };
  52. #endif // (OPENSSL_VERSION_NUMBER < 0x30000000L)
  53. context::context(context::method m)
  54. : handle_(0)
  55. {
  56. ::ERR_clear_error();
  57. switch (m)
  58. {
  59. // SSL v2.
  60. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) || defined(OPENSSL_NO_SSL2)
  61. case context::sslv2:
  62. case context::sslv2_client:
  63. case context::sslv2_server:
  64. boost::asio::detail::throw_error(
  65. boost::asio::error::invalid_argument, "context");
  66. break;
  67. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L) || defined(OPENSSL_NO_SSL2)
  68. case context::sslv2:
  69. handle_ = ::SSL_CTX_new(::SSLv2_method());
  70. break;
  71. case context::sslv2_client:
  72. handle_ = ::SSL_CTX_new(::SSLv2_client_method());
  73. break;
  74. case context::sslv2_server:
  75. handle_ = ::SSL_CTX_new(::SSLv2_server_method());
  76. break;
  77. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L) || defined(OPENSSL_NO_SSL2)
  78. // SSL v3.
  79. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  80. case context::sslv3:
  81. handle_ = ::SSL_CTX_new(::TLS_method());
  82. if (handle_)
  83. {
  84. SSL_CTX_set_min_proto_version(handle_, SSL3_VERSION);
  85. SSL_CTX_set_max_proto_version(handle_, SSL3_VERSION);
  86. }
  87. break;
  88. case context::sslv3_client:
  89. handle_ = ::SSL_CTX_new(::TLS_client_method());
  90. if (handle_)
  91. {
  92. SSL_CTX_set_min_proto_version(handle_, SSL3_VERSION);
  93. SSL_CTX_set_max_proto_version(handle_, SSL3_VERSION);
  94. }
  95. break;
  96. case context::sslv3_server:
  97. handle_ = ::SSL_CTX_new(::TLS_server_method());
  98. if (handle_)
  99. {
  100. SSL_CTX_set_min_proto_version(handle_, SSL3_VERSION);
  101. SSL_CTX_set_max_proto_version(handle_, SSL3_VERSION);
  102. }
  103. break;
  104. #elif defined(OPENSSL_NO_SSL3)
  105. case context::sslv3:
  106. case context::sslv3_client:
  107. case context::sslv3_server:
  108. boost::asio::detail::throw_error(
  109. boost::asio::error::invalid_argument, "context");
  110. break;
  111. #else // defined(OPENSSL_NO_SSL3)
  112. case context::sslv3:
  113. handle_ = ::SSL_CTX_new(::SSLv3_method());
  114. break;
  115. case context::sslv3_client:
  116. handle_ = ::SSL_CTX_new(::SSLv3_client_method());
  117. break;
  118. case context::sslv3_server:
  119. handle_ = ::SSL_CTX_new(::SSLv3_server_method());
  120. break;
  121. #endif // defined(OPENSSL_NO_SSL3)
  122. // TLS v1.0.
  123. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  124. case context::tlsv1:
  125. handle_ = ::SSL_CTX_new(::TLS_method());
  126. if (handle_)
  127. {
  128. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  129. SSL_CTX_set_max_proto_version(handle_, TLS1_VERSION);
  130. }
  131. break;
  132. case context::tlsv1_client:
  133. handle_ = ::SSL_CTX_new(::TLS_client_method());
  134. if (handle_)
  135. {
  136. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  137. SSL_CTX_set_max_proto_version(handle_, TLS1_VERSION);
  138. }
  139. break;
  140. case context::tlsv1_server:
  141. handle_ = ::SSL_CTX_new(::TLS_server_method());
  142. if (handle_)
  143. {
  144. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  145. SSL_CTX_set_max_proto_version(handle_, TLS1_VERSION);
  146. }
  147. break;
  148. #elif defined(SSL_TXT_TLSV1)
  149. case context::tlsv1:
  150. handle_ = ::SSL_CTX_new(::TLSv1_method());
  151. break;
  152. case context::tlsv1_client:
  153. handle_ = ::SSL_CTX_new(::TLSv1_client_method());
  154. break;
  155. case context::tlsv1_server:
  156. handle_ = ::SSL_CTX_new(::TLSv1_server_method());
  157. break;
  158. #else // defined(SSL_TXT_TLSV1)
  159. case context::tlsv1:
  160. case context::tlsv1_client:
  161. case context::tlsv1_server:
  162. boost::asio::detail::throw_error(
  163. boost::asio::error::invalid_argument, "context");
  164. break;
  165. #endif // defined(SSL_TXT_TLSV1)
  166. // TLS v1.1.
  167. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  168. case context::tlsv11:
  169. handle_ = ::SSL_CTX_new(::TLS_method());
  170. if (handle_)
  171. {
  172. SSL_CTX_set_min_proto_version(handle_, TLS1_1_VERSION);
  173. SSL_CTX_set_max_proto_version(handle_, TLS1_1_VERSION);
  174. }
  175. break;
  176. case context::tlsv11_client:
  177. handle_ = ::SSL_CTX_new(::TLS_client_method());
  178. if (handle_)
  179. {
  180. SSL_CTX_set_min_proto_version(handle_, TLS1_1_VERSION);
  181. SSL_CTX_set_max_proto_version(handle_, TLS1_1_VERSION);
  182. }
  183. break;
  184. case context::tlsv11_server:
  185. handle_ = ::SSL_CTX_new(::TLS_server_method());
  186. if (handle_)
  187. {
  188. SSL_CTX_set_min_proto_version(handle_, TLS1_1_VERSION);
  189. SSL_CTX_set_max_proto_version(handle_, TLS1_1_VERSION);
  190. }
  191. break;
  192. #elif defined(SSL_TXT_TLSV1_1)
  193. case context::tlsv11:
  194. handle_ = ::SSL_CTX_new(::TLSv1_1_method());
  195. break;
  196. case context::tlsv11_client:
  197. handle_ = ::SSL_CTX_new(::TLSv1_1_client_method());
  198. break;
  199. case context::tlsv11_server:
  200. handle_ = ::SSL_CTX_new(::TLSv1_1_server_method());
  201. break;
  202. #else // defined(SSL_TXT_TLSV1_1)
  203. case context::tlsv11:
  204. case context::tlsv11_client:
  205. case context::tlsv11_server:
  206. boost::asio::detail::throw_error(
  207. boost::asio::error::invalid_argument, "context");
  208. break;
  209. #endif // defined(SSL_TXT_TLSV1_1)
  210. // TLS v1.2.
  211. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  212. case context::tlsv12:
  213. handle_ = ::SSL_CTX_new(::TLS_method());
  214. if (handle_)
  215. {
  216. SSL_CTX_set_min_proto_version(handle_, TLS1_2_VERSION);
  217. SSL_CTX_set_max_proto_version(handle_, TLS1_2_VERSION);
  218. }
  219. break;
  220. case context::tlsv12_client:
  221. handle_ = ::SSL_CTX_new(::TLS_client_method());
  222. if (handle_)
  223. {
  224. SSL_CTX_set_min_proto_version(handle_, TLS1_2_VERSION);
  225. SSL_CTX_set_max_proto_version(handle_, TLS1_2_VERSION);
  226. }
  227. break;
  228. case context::tlsv12_server:
  229. handle_ = ::SSL_CTX_new(::TLS_server_method());
  230. if (handle_)
  231. {
  232. SSL_CTX_set_min_proto_version(handle_, TLS1_2_VERSION);
  233. SSL_CTX_set_max_proto_version(handle_, TLS1_2_VERSION);
  234. }
  235. break;
  236. #elif defined(SSL_TXT_TLSV1_2)
  237. case context::tlsv12:
  238. handle_ = ::SSL_CTX_new(::TLSv1_2_method());
  239. break;
  240. case context::tlsv12_client:
  241. handle_ = ::SSL_CTX_new(::TLSv1_2_client_method());
  242. break;
  243. case context::tlsv12_server:
  244. handle_ = ::SSL_CTX_new(::TLSv1_2_server_method());
  245. break;
  246. #else // defined(SSL_TXT_TLSV1_2)
  247. case context::tlsv12:
  248. case context::tlsv12_client:
  249. case context::tlsv12_server:
  250. boost::asio::detail::throw_error(
  251. boost::asio::error::invalid_argument, "context");
  252. break;
  253. #endif // defined(SSL_TXT_TLSV1_2)
  254. // TLS v1.3.
  255. #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) \
  256. && !defined(LIBRESSL_VERSION_NUMBER)
  257. case context::tlsv13:
  258. handle_ = ::SSL_CTX_new(::TLS_method());
  259. if (handle_)
  260. {
  261. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  262. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  263. }
  264. break;
  265. case context::tlsv13_client:
  266. handle_ = ::SSL_CTX_new(::TLS_client_method());
  267. if (handle_)
  268. {
  269. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  270. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  271. }
  272. break;
  273. case context::tlsv13_server:
  274. handle_ = ::SSL_CTX_new(::TLS_server_method());
  275. if (handle_)
  276. {
  277. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  278. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  279. }
  280. break;
  281. #else // (OPENSSL_VERSION_NUMBER >= 0x10101000L)
  282. // && !defined(LIBRESSL_VERSION_NUMBER)
  283. case context::tlsv13:
  284. case context::tlsv13_client:
  285. case context::tlsv13_server:
  286. boost::asio::detail::throw_error(
  287. boost::asio::error::invalid_argument, "context");
  288. break;
  289. #endif // (OPENSSL_VERSION_NUMBER >= 0x10101000L)
  290. // && !defined(LIBRESSL_VERSION_NUMBER)
  291. // Any supported SSL/TLS version.
  292. case context::sslv23:
  293. handle_ = ::SSL_CTX_new(::SSLv23_method());
  294. break;
  295. case context::sslv23_client:
  296. handle_ = ::SSL_CTX_new(::SSLv23_client_method());
  297. break;
  298. case context::sslv23_server:
  299. handle_ = ::SSL_CTX_new(::SSLv23_server_method());
  300. break;
  301. // Any supported TLS version.
  302. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  303. case context::tls:
  304. handle_ = ::SSL_CTX_new(::TLS_method());
  305. if (handle_)
  306. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  307. break;
  308. case context::tls_client:
  309. handle_ = ::SSL_CTX_new(::TLS_client_method());
  310. if (handle_)
  311. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  312. break;
  313. case context::tls_server:
  314. handle_ = ::SSL_CTX_new(::TLS_server_method());
  315. if (handle_)
  316. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  317. break;
  318. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  319. case context::tls:
  320. handle_ = ::SSL_CTX_new(::SSLv23_method());
  321. if (handle_)
  322. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  323. break;
  324. case context::tls_client:
  325. handle_ = ::SSL_CTX_new(::SSLv23_client_method());
  326. if (handle_)
  327. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  328. break;
  329. case context::tls_server:
  330. handle_ = ::SSL_CTX_new(::SSLv23_server_method());
  331. if (handle_)
  332. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  333. break;
  334. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  335. default:
  336. handle_ = ::SSL_CTX_new(0);
  337. break;
  338. }
  339. if (handle_ == 0)
  340. {
  341. boost::system::error_code ec = translate_error(::ERR_get_error());
  342. boost::asio::detail::throw_error(ec, "context");
  343. }
  344. set_options(no_compression);
  345. }
  346. context::context(context::native_handle_type native_handle)
  347. : handle_(native_handle)
  348. {
  349. if (!handle_)
  350. {
  351. boost::asio::detail::throw_error(
  352. boost::asio::error::invalid_argument, "context");
  353. }
  354. }
  355. context::context(context&& other)
  356. {
  357. handle_ = other.handle_;
  358. other.handle_ = 0;
  359. }
  360. context& context::operator=(context&& other)
  361. {
  362. context tmp(static_cast<context&&>(*this));
  363. handle_ = other.handle_;
  364. other.handle_ = 0;
  365. return *this;
  366. }
  367. context::~context()
  368. {
  369. if (handle_)
  370. {
  371. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  372. && (!defined(LIBRESSL_VERSION_NUMBER) \
  373. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  374. || defined(BOOST_ASIO_USE_WOLFSSL)
  375. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  376. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  377. void* cb_userdata = handle_->default_passwd_callback_userdata;
  378. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  379. if (cb_userdata)
  380. {
  381. detail::password_callback_base* callback =
  382. static_cast<detail::password_callback_base*>(
  383. cb_userdata);
  384. delete callback;
  385. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  386. && (!defined(LIBRESSL_VERSION_NUMBER) \
  387. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  388. || defined(BOOST_ASIO_USE_WOLFSSL)
  389. ::SSL_CTX_set_default_passwd_cb_userdata(handle_, 0);
  390. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  391. handle_->default_passwd_callback_userdata = 0;
  392. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  393. }
  394. if (SSL_CTX_get_app_data(handle_))
  395. {
  396. detail::verify_callback_base* callback =
  397. static_cast<detail::verify_callback_base*>(
  398. SSL_CTX_get_app_data(handle_));
  399. delete callback;
  400. SSL_CTX_set_app_data(handle_, 0);
  401. }
  402. ::SSL_CTX_free(handle_);
  403. }
  404. }
  405. context::native_handle_type context::native_handle()
  406. {
  407. return handle_;
  408. }
  409. void context::clear_options(context::options o)
  410. {
  411. boost::system::error_code ec;
  412. clear_options(o, ec);
  413. boost::asio::detail::throw_error(ec, "clear_options");
  414. }
  415. BOOST_ASIO_SYNC_OP_VOID context::clear_options(
  416. context::options o, boost::system::error_code& ec)
  417. {
  418. #if (OPENSSL_VERSION_NUMBER >= 0x009080DFL) \
  419. && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  420. # if !defined(SSL_OP_NO_COMPRESSION)
  421. if ((o & context::no_compression) != 0)
  422. {
  423. # if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  424. handle_->comp_methods = SSL_COMP_get_compression_methods();
  425. # endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  426. o ^= context::no_compression;
  427. }
  428. # endif // !defined(SSL_OP_NO_COMPRESSION)
  429. ::SSL_CTX_clear_options(handle_, o);
  430. ec = boost::system::error_code();
  431. #else // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
  432. // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  433. (void)o;
  434. ec = boost::asio::error::operation_not_supported;
  435. #endif // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
  436. // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  437. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  438. }
  439. void context::set_options(context::options o)
  440. {
  441. boost::system::error_code ec;
  442. set_options(o, ec);
  443. boost::asio::detail::throw_error(ec, "set_options");
  444. }
  445. BOOST_ASIO_SYNC_OP_VOID context::set_options(
  446. context::options o, boost::system::error_code& ec)
  447. {
  448. #if !defined(SSL_OP_NO_COMPRESSION)
  449. if ((o & context::no_compression) != 0)
  450. {
  451. #if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  452. handle_->comp_methods =
  453. boost::asio::ssl::detail::openssl_init<>::get_null_compression_methods();
  454. #endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  455. o ^= context::no_compression;
  456. }
  457. #endif // !defined(SSL_OP_NO_COMPRESSION)
  458. ::SSL_CTX_set_options(handle_, o);
  459. ec = boost::system::error_code();
  460. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  461. }
  462. void context::set_verify_mode(verify_mode v)
  463. {
  464. boost::system::error_code ec;
  465. set_verify_mode(v, ec);
  466. boost::asio::detail::throw_error(ec, "set_verify_mode");
  467. }
  468. BOOST_ASIO_SYNC_OP_VOID context::set_verify_mode(
  469. verify_mode v, boost::system::error_code& ec)
  470. {
  471. ::SSL_CTX_set_verify(handle_, v, ::SSL_CTX_get_verify_callback(handle_));
  472. ec = boost::system::error_code();
  473. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  474. }
  475. void context::set_verify_depth(int depth)
  476. {
  477. boost::system::error_code ec;
  478. set_verify_depth(depth, ec);
  479. boost::asio::detail::throw_error(ec, "set_verify_depth");
  480. }
  481. BOOST_ASIO_SYNC_OP_VOID context::set_verify_depth(
  482. int depth, boost::system::error_code& ec)
  483. {
  484. ::SSL_CTX_set_verify_depth(handle_, depth);
  485. ec = boost::system::error_code();
  486. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  487. }
  488. void context::load_verify_file(const std::string& filename)
  489. {
  490. boost::system::error_code ec;
  491. load_verify_file(filename, ec);
  492. boost::asio::detail::throw_error(ec, "load_verify_file");
  493. }
  494. BOOST_ASIO_SYNC_OP_VOID context::load_verify_file(
  495. const std::string& filename, boost::system::error_code& ec)
  496. {
  497. ::ERR_clear_error();
  498. if (::SSL_CTX_load_verify_locations(handle_, filename.c_str(), 0) != 1)
  499. {
  500. ec = translate_error(::ERR_get_error());
  501. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  502. }
  503. ec = boost::system::error_code();
  504. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  505. }
  506. void context::add_certificate_authority(const const_buffer& ca)
  507. {
  508. boost::system::error_code ec;
  509. add_certificate_authority(ca, ec);
  510. boost::asio::detail::throw_error(ec, "add_certificate_authority");
  511. }
  512. BOOST_ASIO_SYNC_OP_VOID context::add_certificate_authority(
  513. const const_buffer& ca, boost::system::error_code& ec)
  514. {
  515. ::ERR_clear_error();
  516. bio_cleanup bio = { make_buffer_bio(ca) };
  517. if (bio.p)
  518. {
  519. if (X509_STORE* store = ::SSL_CTX_get_cert_store(handle_))
  520. {
  521. for (bool added = false;; added = true)
  522. {
  523. x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
  524. if (!cert.p)
  525. {
  526. unsigned long err = ::ERR_get_error();
  527. if (added && ERR_GET_LIB(err) == ERR_LIB_PEM
  528. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  529. break;
  530. ec = translate_error(err);
  531. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  532. }
  533. if (::X509_STORE_add_cert(store, cert.p) != 1)
  534. {
  535. ec = translate_error(::ERR_get_error());
  536. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  537. }
  538. }
  539. }
  540. }
  541. ec = boost::system::error_code();
  542. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  543. }
  544. void context::set_default_verify_paths()
  545. {
  546. boost::system::error_code ec;
  547. set_default_verify_paths(ec);
  548. boost::asio::detail::throw_error(ec, "set_default_verify_paths");
  549. }
  550. BOOST_ASIO_SYNC_OP_VOID context::set_default_verify_paths(
  551. boost::system::error_code& ec)
  552. {
  553. ::ERR_clear_error();
  554. if (::SSL_CTX_set_default_verify_paths(handle_) != 1)
  555. {
  556. ec = translate_error(::ERR_get_error());
  557. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  558. }
  559. ec = boost::system::error_code();
  560. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  561. }
  562. void context::add_verify_path(const std::string& path)
  563. {
  564. boost::system::error_code ec;
  565. add_verify_path(path, ec);
  566. boost::asio::detail::throw_error(ec, "add_verify_path");
  567. }
  568. BOOST_ASIO_SYNC_OP_VOID context::add_verify_path(
  569. const std::string& path, boost::system::error_code& ec)
  570. {
  571. ::ERR_clear_error();
  572. if (::SSL_CTX_load_verify_locations(handle_, 0, path.c_str()) != 1)
  573. {
  574. ec = translate_error(::ERR_get_error());
  575. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  576. }
  577. ec = boost::system::error_code();
  578. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  579. }
  580. void context::use_certificate(
  581. const const_buffer& certificate, file_format format)
  582. {
  583. boost::system::error_code ec;
  584. use_certificate(certificate, format, ec);
  585. boost::asio::detail::throw_error(ec, "use_certificate");
  586. }
  587. BOOST_ASIO_SYNC_OP_VOID context::use_certificate(
  588. const const_buffer& certificate, file_format format,
  589. boost::system::error_code& ec)
  590. {
  591. ::ERR_clear_error();
  592. if (format == context_base::asn1)
  593. {
  594. if (::SSL_CTX_use_certificate_ASN1(handle_,
  595. static_cast<int>(certificate.size()),
  596. static_cast<const unsigned char*>(certificate.data())) == 1)
  597. {
  598. ec = boost::system::error_code();
  599. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  600. }
  601. }
  602. else if (format == context_base::pem)
  603. {
  604. bio_cleanup bio = { make_buffer_bio(certificate) };
  605. if (bio.p)
  606. {
  607. x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
  608. if (cert.p)
  609. {
  610. if (::SSL_CTX_use_certificate(handle_, cert.p) == 1)
  611. {
  612. ec = boost::system::error_code();
  613. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  614. }
  615. }
  616. }
  617. }
  618. else
  619. {
  620. ec = boost::asio::error::invalid_argument;
  621. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  622. }
  623. ec = translate_error(::ERR_get_error());
  624. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  625. }
  626. void context::use_certificate_file(
  627. const std::string& filename, file_format format)
  628. {
  629. boost::system::error_code ec;
  630. use_certificate_file(filename, format, ec);
  631. boost::asio::detail::throw_error(ec, "use_certificate_file");
  632. }
  633. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_file(
  634. const std::string& filename, file_format format,
  635. boost::system::error_code& ec)
  636. {
  637. int file_type;
  638. switch (format)
  639. {
  640. case context_base::asn1:
  641. file_type = SSL_FILETYPE_ASN1;
  642. break;
  643. case context_base::pem:
  644. file_type = SSL_FILETYPE_PEM;
  645. break;
  646. default:
  647. {
  648. ec = boost::asio::error::invalid_argument;
  649. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  650. }
  651. }
  652. ::ERR_clear_error();
  653. if (::SSL_CTX_use_certificate_file(handle_, filename.c_str(), file_type) != 1)
  654. {
  655. ec = translate_error(::ERR_get_error());
  656. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  657. }
  658. ec = boost::system::error_code();
  659. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  660. }
  661. void context::use_certificate_chain(const const_buffer& chain)
  662. {
  663. boost::system::error_code ec;
  664. use_certificate_chain(chain, ec);
  665. boost::asio::detail::throw_error(ec, "use_certificate_chain");
  666. }
  667. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_chain(
  668. const const_buffer& chain, boost::system::error_code& ec)
  669. {
  670. ::ERR_clear_error();
  671. bio_cleanup bio = { make_buffer_bio(chain) };
  672. if (bio.p)
  673. {
  674. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  675. && (!defined(LIBRESSL_VERSION_NUMBER) \
  676. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  677. || defined(BOOST_ASIO_USE_WOLFSSL)
  678. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  679. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  680. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  681. pem_password_cb* callback = handle_->default_passwd_callback;
  682. void* cb_userdata = handle_->default_passwd_callback_userdata;
  683. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  684. x509_cleanup cert = {
  685. ::PEM_read_bio_X509_AUX(bio.p, 0,
  686. callback,
  687. cb_userdata) };
  688. if (!cert.p)
  689. {
  690. ec = translate_error(ERR_R_PEM_LIB);
  691. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  692. }
  693. int result = ::SSL_CTX_use_certificate(handle_, cert.p);
  694. if (result == 0 || ::ERR_peek_error() != 0)
  695. {
  696. ec = translate_error(::ERR_get_error());
  697. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  698. }
  699. #if ((OPENSSL_VERSION_NUMBER >= 0x10002000L) \
  700. && (!defined(LIBRESSL_VERSION_NUMBER) \
  701. || LIBRESSL_VERSION_NUMBER >= 0x2090100fL)) \
  702. || defined(BOOST_ASIO_USE_WOLFSSL)
  703. ::SSL_CTX_clear_chain_certs(handle_);
  704. #else
  705. if (handle_->extra_certs)
  706. {
  707. ::sk_X509_pop_free(handle_->extra_certs, X509_free);
  708. handle_->extra_certs = 0;
  709. }
  710. #endif // (OPENSSL_VERSION_NUMBER >= 0x10002000L)
  711. while (X509* cacert = ::PEM_read_bio_X509(bio.p, 0,
  712. callback,
  713. cb_userdata))
  714. {
  715. if (!::SSL_CTX_add_extra_chain_cert(handle_, cacert))
  716. {
  717. ec = translate_error(::ERR_get_error());
  718. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  719. }
  720. }
  721. result = ::ERR_peek_last_error();
  722. if ((ERR_GET_LIB(result) == ERR_LIB_PEM)
  723. && (ERR_GET_REASON(result) == PEM_R_NO_START_LINE))
  724. {
  725. ::ERR_clear_error();
  726. ec = boost::system::error_code();
  727. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  728. }
  729. }
  730. ec = translate_error(::ERR_get_error());
  731. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  732. }
  733. void context::use_certificate_chain_file(const std::string& filename)
  734. {
  735. boost::system::error_code ec;
  736. use_certificate_chain_file(filename, ec);
  737. boost::asio::detail::throw_error(ec, "use_certificate_chain_file");
  738. }
  739. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_chain_file(
  740. const std::string& filename, boost::system::error_code& ec)
  741. {
  742. ::ERR_clear_error();
  743. if (::SSL_CTX_use_certificate_chain_file(handle_, filename.c_str()) != 1)
  744. {
  745. ec = translate_error(::ERR_get_error());
  746. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  747. }
  748. ec = boost::system::error_code();
  749. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  750. }
  751. void context::use_private_key(
  752. const const_buffer& private_key, context::file_format format)
  753. {
  754. boost::system::error_code ec;
  755. use_private_key(private_key, format, ec);
  756. boost::asio::detail::throw_error(ec, "use_private_key");
  757. }
  758. BOOST_ASIO_SYNC_OP_VOID context::use_private_key(
  759. const const_buffer& private_key, context::file_format format,
  760. boost::system::error_code& ec)
  761. {
  762. ::ERR_clear_error();
  763. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  764. && (!defined(LIBRESSL_VERSION_NUMBER) \
  765. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  766. || defined(BOOST_ASIO_USE_WOLFSSL)
  767. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  768. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  769. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  770. pem_password_cb* callback = handle_->default_passwd_callback;
  771. void* cb_userdata = handle_->default_passwd_callback_userdata;
  772. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  773. bio_cleanup bio = { make_buffer_bio(private_key) };
  774. if (bio.p)
  775. {
  776. evp_pkey_cleanup evp_private_key = { 0 };
  777. switch (format)
  778. {
  779. case context_base::asn1:
  780. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  781. break;
  782. case context_base::pem:
  783. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  784. bio.p, 0, callback,
  785. cb_userdata);
  786. break;
  787. default:
  788. {
  789. ec = boost::asio::error::invalid_argument;
  790. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  791. }
  792. }
  793. if (evp_private_key.p)
  794. {
  795. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  796. {
  797. ec = boost::system::error_code();
  798. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  799. }
  800. }
  801. }
  802. ec = translate_error(::ERR_get_error());
  803. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  804. }
  805. void context::use_private_key_file(
  806. const std::string& filename, context::file_format format)
  807. {
  808. boost::system::error_code ec;
  809. use_private_key_file(filename, format, ec);
  810. boost::asio::detail::throw_error(ec, "use_private_key_file");
  811. }
  812. void context::use_rsa_private_key(
  813. const const_buffer& private_key, context::file_format format)
  814. {
  815. boost::system::error_code ec;
  816. use_rsa_private_key(private_key, format, ec);
  817. boost::asio::detail::throw_error(ec, "use_rsa_private_key");
  818. }
  819. BOOST_ASIO_SYNC_OP_VOID context::use_rsa_private_key(
  820. const const_buffer& private_key, context::file_format format,
  821. boost::system::error_code& ec)
  822. {
  823. ::ERR_clear_error();
  824. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  825. && (!defined(LIBRESSL_VERSION_NUMBER) \
  826. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  827. || defined(BOOST_ASIO_USE_WOLFSSL)
  828. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  829. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  830. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  831. pem_password_cb* callback = handle_->default_passwd_callback;
  832. void* cb_userdata = handle_->default_passwd_callback_userdata;
  833. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  834. bio_cleanup bio = { make_buffer_bio(private_key) };
  835. if (bio.p)
  836. {
  837. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  838. evp_pkey_cleanup evp_private_key = { 0 };
  839. switch (format)
  840. {
  841. case context_base::asn1:
  842. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  843. break;
  844. case context_base::pem:
  845. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  846. bio.p, 0, callback,
  847. cb_userdata);
  848. break;
  849. default:
  850. {
  851. ec = boost::asio::error::invalid_argument;
  852. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  853. }
  854. }
  855. if (evp_private_key.p)
  856. {
  857. if (::EVP_PKEY_is_a(evp_private_key.p, "RSA") == 0)
  858. {
  859. ec = translate_error(
  860. ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_RSA_KEY));
  861. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  862. }
  863. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  864. {
  865. ec = boost::system::error_code();
  866. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  867. }
  868. }
  869. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  870. rsa_cleanup rsa_private_key = { 0 };
  871. switch (format)
  872. {
  873. case context_base::asn1:
  874. rsa_private_key.p = ::d2i_RSAPrivateKey_bio(bio.p, 0);
  875. break;
  876. case context_base::pem:
  877. rsa_private_key.p = ::PEM_read_bio_RSAPrivateKey(
  878. bio.p, 0, callback,
  879. cb_userdata);
  880. break;
  881. default:
  882. {
  883. ec = boost::asio::error::invalid_argument;
  884. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  885. }
  886. }
  887. if (rsa_private_key.p)
  888. {
  889. if (::SSL_CTX_use_RSAPrivateKey(handle_, rsa_private_key.p) == 1)
  890. {
  891. ec = boost::system::error_code();
  892. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  893. }
  894. }
  895. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  896. }
  897. ec = translate_error(::ERR_get_error());
  898. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  899. }
  900. BOOST_ASIO_SYNC_OP_VOID context::use_private_key_file(
  901. const std::string& filename, context::file_format format,
  902. boost::system::error_code& ec)
  903. {
  904. int file_type;
  905. switch (format)
  906. {
  907. case context_base::asn1:
  908. file_type = SSL_FILETYPE_ASN1;
  909. break;
  910. case context_base::pem:
  911. file_type = SSL_FILETYPE_PEM;
  912. break;
  913. default:
  914. {
  915. ec = boost::asio::error::invalid_argument;
  916. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  917. }
  918. }
  919. ::ERR_clear_error();
  920. if (::SSL_CTX_use_PrivateKey_file(handle_, filename.c_str(), file_type) != 1)
  921. {
  922. ec = translate_error(::ERR_get_error());
  923. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  924. }
  925. ec = boost::system::error_code();
  926. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  927. }
  928. void context::use_rsa_private_key_file(
  929. const std::string& filename, context::file_format format)
  930. {
  931. boost::system::error_code ec;
  932. use_rsa_private_key_file(filename, format, ec);
  933. boost::asio::detail::throw_error(ec, "use_rsa_private_key_file");
  934. }
  935. BOOST_ASIO_SYNC_OP_VOID context::use_rsa_private_key_file(
  936. const std::string& filename, context::file_format format,
  937. boost::system::error_code& ec)
  938. {
  939. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  940. ::ERR_clear_error();
  941. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  942. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  943. bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
  944. if (bio.p)
  945. {
  946. evp_pkey_cleanup evp_private_key = { 0 };
  947. switch (format)
  948. {
  949. case context_base::asn1:
  950. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  951. break;
  952. case context_base::pem:
  953. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  954. bio.p, 0, callback,
  955. cb_userdata);
  956. break;
  957. default:
  958. {
  959. ec = boost::asio::error::invalid_argument;
  960. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  961. }
  962. }
  963. if (evp_private_key.p)
  964. {
  965. if (::EVP_PKEY_is_a(evp_private_key.p, "RSA") == 0)
  966. {
  967. ec = translate_error(
  968. ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_RSA_KEY));
  969. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  970. }
  971. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  972. {
  973. ec = boost::system::error_code();
  974. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  975. }
  976. }
  977. }
  978. ec = translate_error(::ERR_get_error());
  979. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  980. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  981. int file_type;
  982. switch (format)
  983. {
  984. case context_base::asn1:
  985. file_type = SSL_FILETYPE_ASN1;
  986. break;
  987. case context_base::pem:
  988. file_type = SSL_FILETYPE_PEM;
  989. break;
  990. default:
  991. {
  992. ec = boost::asio::error::invalid_argument;
  993. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  994. }
  995. }
  996. ::ERR_clear_error();
  997. if (::SSL_CTX_use_RSAPrivateKey_file(
  998. handle_, filename.c_str(), file_type) != 1)
  999. {
  1000. ec = translate_error(::ERR_get_error());
  1001. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1002. }
  1003. ec = boost::system::error_code();
  1004. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1005. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1006. }
  1007. void context::use_tmp_dh(const const_buffer& dh)
  1008. {
  1009. boost::system::error_code ec;
  1010. use_tmp_dh(dh, ec);
  1011. boost::asio::detail::throw_error(ec, "use_tmp_dh");
  1012. }
  1013. BOOST_ASIO_SYNC_OP_VOID context::use_tmp_dh(
  1014. const const_buffer& dh, boost::system::error_code& ec)
  1015. {
  1016. ::ERR_clear_error();
  1017. bio_cleanup bio = { make_buffer_bio(dh) };
  1018. if (bio.p)
  1019. {
  1020. return do_use_tmp_dh(bio.p, ec);
  1021. }
  1022. ec = translate_error(::ERR_get_error());
  1023. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1024. }
  1025. void context::use_tmp_dh_file(const std::string& filename)
  1026. {
  1027. boost::system::error_code ec;
  1028. use_tmp_dh_file(filename, ec);
  1029. boost::asio::detail::throw_error(ec, "use_tmp_dh_file");
  1030. }
  1031. BOOST_ASIO_SYNC_OP_VOID context::use_tmp_dh_file(
  1032. const std::string& filename, boost::system::error_code& ec)
  1033. {
  1034. ::ERR_clear_error();
  1035. bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
  1036. if (bio.p)
  1037. {
  1038. return do_use_tmp_dh(bio.p, ec);
  1039. }
  1040. ec = translate_error(::ERR_get_error());
  1041. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1042. }
  1043. BOOST_ASIO_SYNC_OP_VOID context::do_use_tmp_dh(
  1044. BIO* bio, boost::system::error_code& ec)
  1045. {
  1046. ::ERR_clear_error();
  1047. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1048. EVP_PKEY* p = ::PEM_read_bio_Parameters(bio, 0);
  1049. if (p)
  1050. {
  1051. if (::SSL_CTX_set0_tmp_dh_pkey(handle_, p) == 1)
  1052. {
  1053. ec = boost::system::error_code();
  1054. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1055. }
  1056. else
  1057. ::EVP_PKEY_free(p);
  1058. }
  1059. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1060. dh_cleanup dh = { ::PEM_read_bio_DHparams(bio, 0, 0, 0) };
  1061. if (dh.p)
  1062. {
  1063. if (::SSL_CTX_set_tmp_dh(handle_, dh.p) == 1)
  1064. {
  1065. ec = boost::system::error_code();
  1066. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1067. }
  1068. }
  1069. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1070. ec = translate_error(::ERR_get_error());
  1071. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1072. }
  1073. BOOST_ASIO_SYNC_OP_VOID context::do_set_verify_callback(
  1074. detail::verify_callback_base* callback, boost::system::error_code& ec)
  1075. {
  1076. if (SSL_CTX_get_app_data(handle_))
  1077. {
  1078. delete static_cast<detail::verify_callback_base*>(
  1079. SSL_CTX_get_app_data(handle_));
  1080. }
  1081. SSL_CTX_set_app_data(handle_, callback);
  1082. ::SSL_CTX_set_verify(handle_,
  1083. ::SSL_CTX_get_verify_mode(handle_),
  1084. &context::verify_callback_function);
  1085. ec = boost::system::error_code();
  1086. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1087. }
  1088. int context::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
  1089. {
  1090. if (ctx)
  1091. {
  1092. if (SSL* ssl = static_cast<SSL*>(
  1093. ::X509_STORE_CTX_get_ex_data(
  1094. ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
  1095. {
  1096. if (SSL_CTX* handle = ::SSL_get_SSL_CTX(ssl))
  1097. {
  1098. if (SSL_CTX_get_app_data(handle))
  1099. {
  1100. detail::verify_callback_base* callback =
  1101. static_cast<detail::verify_callback_base*>(
  1102. SSL_CTX_get_app_data(handle));
  1103. verify_context verify_ctx(ctx);
  1104. return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. return 0;
  1110. }
  1111. BOOST_ASIO_SYNC_OP_VOID context::do_set_password_callback(
  1112. detail::password_callback_base* callback, boost::system::error_code& ec)
  1113. {
  1114. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  1115. && (!defined(LIBRESSL_VERSION_NUMBER) \
  1116. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  1117. || defined(BOOST_ASIO_USE_WOLFSSL)
  1118. void* old_callback = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  1119. ::SSL_CTX_set_default_passwd_cb_userdata(handle_, callback);
  1120. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  1121. void* old_callback = handle_->default_passwd_callback_userdata;
  1122. handle_->default_passwd_callback_userdata = callback;
  1123. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  1124. if (old_callback)
  1125. delete static_cast<detail::password_callback_base*>(
  1126. old_callback);
  1127. SSL_CTX_set_default_passwd_cb(handle_, &context::password_callback_function);
  1128. ec = boost::system::error_code();
  1129. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1130. }
  1131. int context::password_callback_function(
  1132. char* buf, int size, int purpose, void* data)
  1133. {
  1134. using namespace std; // For strncat and strlen.
  1135. if (data)
  1136. {
  1137. detail::password_callback_base* callback =
  1138. static_cast<detail::password_callback_base*>(data);
  1139. std::string passwd = callback->call(static_cast<std::size_t>(size),
  1140. purpose ? context_base::for_writing : context_base::for_reading);
  1141. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  1142. strcpy_s(buf, size, passwd.c_str());
  1143. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  1144. *buf = '\0';
  1145. if (size > 0)
  1146. strncat(buf, passwd.c_str(), size - 1);
  1147. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  1148. return static_cast<int>(strlen(buf));
  1149. }
  1150. return 0;
  1151. }
  1152. BIO* context::make_buffer_bio(const const_buffer& b)
  1153. {
  1154. return ::BIO_new_mem_buf(
  1155. const_cast<void*>(b.data()),
  1156. static_cast<int>(b.size()));
  1157. }
  1158. boost::system::error_code context::translate_error(long error)
  1159. {
  1160. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1161. if (ERR_SYSTEM_ERROR(error))
  1162. {
  1163. return boost::system::error_code(
  1164. static_cast<int>(ERR_GET_REASON(error)),
  1165. boost::asio::error::get_system_category());
  1166. }
  1167. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1168. return boost::system::error_code(static_cast<int>(error),
  1169. boost::asio::error::get_ssl_category());
  1170. }
  1171. } // namespace ssl
  1172. } // namespace asio
  1173. } // namespace boost
  1174. #include <boost/asio/detail/pop_options.hpp>
  1175. #endif // BOOST_ASIO_SSL_IMPL_CONTEXT_IPP