context.ipp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. //
  2. // ssl/impl/context.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
  6. // Copyright (c) 2005-2025 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. || defined(BOOST_ASIO_USE_WOLFSSL)
  258. case context::tlsv13:
  259. handle_ = ::SSL_CTX_new(::TLS_method());
  260. if (handle_)
  261. {
  262. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  263. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  264. }
  265. break;
  266. case context::tlsv13_client:
  267. handle_ = ::SSL_CTX_new(::TLS_client_method());
  268. if (handle_)
  269. {
  270. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  271. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  272. }
  273. break;
  274. case context::tlsv13_server:
  275. handle_ = ::SSL_CTX_new(::TLS_server_method());
  276. if (handle_)
  277. {
  278. SSL_CTX_set_min_proto_version(handle_, TLS1_3_VERSION);
  279. SSL_CTX_set_max_proto_version(handle_, TLS1_3_VERSION);
  280. }
  281. break;
  282. #else // ((OPENSSL_VERSION_NUMBER >= 0x10101000L)
  283. // && !defined(LIBRESSL_VERSION_NUMBER))
  284. // || defined(BOOST_ASIO_USE_WOLFSSL)
  285. case context::tlsv13:
  286. case context::tlsv13_client:
  287. case context::tlsv13_server:
  288. boost::asio::detail::throw_error(
  289. boost::asio::error::invalid_argument, "context");
  290. break;
  291. #endif // ((OPENSSL_VERSION_NUMBER >= 0x10101000L)
  292. // && !defined(LIBRESSL_VERSION_NUMBER))
  293. // || defined(BOOST_ASIO_USE_WOLFSSL)
  294. // Any supported SSL/TLS version.
  295. case context::sslv23:
  296. handle_ = ::SSL_CTX_new(::SSLv23_method());
  297. break;
  298. case context::sslv23_client:
  299. handle_ = ::SSL_CTX_new(::SSLv23_client_method());
  300. break;
  301. case context::sslv23_server:
  302. handle_ = ::SSL_CTX_new(::SSLv23_server_method());
  303. break;
  304. // Any supported TLS version.
  305. #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  306. case context::tls:
  307. handle_ = ::SSL_CTX_new(::TLS_method());
  308. if (handle_)
  309. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  310. break;
  311. case context::tls_client:
  312. handle_ = ::SSL_CTX_new(::TLS_client_method());
  313. if (handle_)
  314. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  315. break;
  316. case context::tls_server:
  317. handle_ = ::SSL_CTX_new(::TLS_server_method());
  318. if (handle_)
  319. SSL_CTX_set_min_proto_version(handle_, TLS1_VERSION);
  320. break;
  321. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  322. case context::tls:
  323. handle_ = ::SSL_CTX_new(::SSLv23_method());
  324. if (handle_)
  325. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  326. break;
  327. case context::tls_client:
  328. handle_ = ::SSL_CTX_new(::SSLv23_client_method());
  329. if (handle_)
  330. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  331. break;
  332. case context::tls_server:
  333. handle_ = ::SSL_CTX_new(::SSLv23_server_method());
  334. if (handle_)
  335. SSL_CTX_set_options(handle_, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
  336. break;
  337. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  338. default:
  339. handle_ = ::SSL_CTX_new(0);
  340. break;
  341. }
  342. if (handle_ == 0)
  343. {
  344. boost::system::error_code ec = translate_error(::ERR_get_error());
  345. boost::asio::detail::throw_error(ec, "context");
  346. }
  347. set_options(no_compression);
  348. }
  349. context::context(context::native_handle_type native_handle)
  350. : handle_(native_handle)
  351. {
  352. if (!handle_)
  353. {
  354. boost::asio::detail::throw_error(
  355. boost::asio::error::invalid_argument, "context");
  356. }
  357. }
  358. context::context(context&& other)
  359. {
  360. handle_ = other.handle_;
  361. other.handle_ = 0;
  362. }
  363. context& context::operator=(context&& other)
  364. {
  365. context tmp(static_cast<context&&>(*this));
  366. handle_ = other.handle_;
  367. other.handle_ = 0;
  368. return *this;
  369. }
  370. context::~context()
  371. {
  372. if (handle_)
  373. {
  374. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  375. && (!defined(LIBRESSL_VERSION_NUMBER) \
  376. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  377. || defined(BOOST_ASIO_USE_WOLFSSL)
  378. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  379. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  380. void* cb_userdata = handle_->default_passwd_callback_userdata;
  381. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  382. if (cb_userdata)
  383. {
  384. detail::password_callback_base* callback =
  385. static_cast<detail::password_callback_base*>(
  386. cb_userdata);
  387. delete callback;
  388. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  389. && (!defined(LIBRESSL_VERSION_NUMBER) \
  390. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  391. || defined(BOOST_ASIO_USE_WOLFSSL)
  392. ::SSL_CTX_set_default_passwd_cb_userdata(handle_, 0);
  393. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  394. handle_->default_passwd_callback_userdata = 0;
  395. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  396. }
  397. if (SSL_CTX_get_app_data(handle_))
  398. {
  399. detail::verify_callback_base* callback =
  400. static_cast<detail::verify_callback_base*>(
  401. SSL_CTX_get_app_data(handle_));
  402. delete callback;
  403. SSL_CTX_set_app_data(handle_, 0);
  404. }
  405. ::SSL_CTX_free(handle_);
  406. }
  407. }
  408. context::native_handle_type context::native_handle()
  409. {
  410. return handle_;
  411. }
  412. void context::clear_options(context::options o)
  413. {
  414. boost::system::error_code ec;
  415. clear_options(o, ec);
  416. boost::asio::detail::throw_error(ec, "clear_options");
  417. }
  418. BOOST_ASIO_SYNC_OP_VOID context::clear_options(
  419. context::options o, boost::system::error_code& ec)
  420. {
  421. #if (OPENSSL_VERSION_NUMBER >= 0x009080DFL) \
  422. && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  423. # if !defined(SSL_OP_NO_COMPRESSION)
  424. if ((o & context::no_compression) != 0)
  425. {
  426. # if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  427. handle_->comp_methods = SSL_COMP_get_compression_methods();
  428. # endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  429. o ^= context::no_compression;
  430. }
  431. # endif // !defined(SSL_OP_NO_COMPRESSION)
  432. ::SSL_CTX_clear_options(handle_, o);
  433. ec = boost::system::error_code();
  434. #else // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
  435. // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  436. (void)o;
  437. ec = boost::asio::error::operation_not_supported;
  438. #endif // (OPENSSL_VERSION_NUMBER >= 0x009080DFL)
  439. // && (OPENSSL_VERSION_NUMBER != 0x00909000L)
  440. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  441. }
  442. void context::set_options(context::options o)
  443. {
  444. boost::system::error_code ec;
  445. set_options(o, ec);
  446. boost::asio::detail::throw_error(ec, "set_options");
  447. }
  448. BOOST_ASIO_SYNC_OP_VOID context::set_options(
  449. context::options o, boost::system::error_code& ec)
  450. {
  451. #if !defined(SSL_OP_NO_COMPRESSION)
  452. if ((o & context::no_compression) != 0)
  453. {
  454. #if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  455. handle_->comp_methods =
  456. boost::asio::ssl::detail::openssl_init<>::get_null_compression_methods();
  457. #endif // (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  458. o ^= context::no_compression;
  459. }
  460. #endif // !defined(SSL_OP_NO_COMPRESSION)
  461. ::SSL_CTX_set_options(handle_, o);
  462. ec = boost::system::error_code();
  463. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  464. }
  465. void context::set_verify_mode(verify_mode v)
  466. {
  467. boost::system::error_code ec;
  468. set_verify_mode(v, ec);
  469. boost::asio::detail::throw_error(ec, "set_verify_mode");
  470. }
  471. BOOST_ASIO_SYNC_OP_VOID context::set_verify_mode(
  472. verify_mode v, boost::system::error_code& ec)
  473. {
  474. ::SSL_CTX_set_verify(handle_, v, ::SSL_CTX_get_verify_callback(handle_));
  475. ec = boost::system::error_code();
  476. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  477. }
  478. void context::set_verify_depth(int depth)
  479. {
  480. boost::system::error_code ec;
  481. set_verify_depth(depth, ec);
  482. boost::asio::detail::throw_error(ec, "set_verify_depth");
  483. }
  484. BOOST_ASIO_SYNC_OP_VOID context::set_verify_depth(
  485. int depth, boost::system::error_code& ec)
  486. {
  487. ::SSL_CTX_set_verify_depth(handle_, depth);
  488. ec = boost::system::error_code();
  489. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  490. }
  491. void context::load_verify_file(const std::string& filename)
  492. {
  493. boost::system::error_code ec;
  494. load_verify_file(filename, ec);
  495. boost::asio::detail::throw_error(ec, "load_verify_file");
  496. }
  497. BOOST_ASIO_SYNC_OP_VOID context::load_verify_file(
  498. const std::string& filename, boost::system::error_code& ec)
  499. {
  500. ::ERR_clear_error();
  501. if (::SSL_CTX_load_verify_locations(handle_, filename.c_str(), 0) != 1)
  502. {
  503. ec = translate_error(::ERR_get_error());
  504. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  505. }
  506. ec = boost::system::error_code();
  507. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  508. }
  509. void context::add_certificate_authority(const const_buffer& ca)
  510. {
  511. boost::system::error_code ec;
  512. add_certificate_authority(ca, ec);
  513. boost::asio::detail::throw_error(ec, "add_certificate_authority");
  514. }
  515. BOOST_ASIO_SYNC_OP_VOID context::add_certificate_authority(
  516. const const_buffer& ca, boost::system::error_code& ec)
  517. {
  518. ::ERR_clear_error();
  519. bio_cleanup bio = { make_buffer_bio(ca) };
  520. if (bio.p)
  521. {
  522. if (X509_STORE* store = ::SSL_CTX_get_cert_store(handle_))
  523. {
  524. for (bool added = false;; added = true)
  525. {
  526. x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
  527. if (!cert.p)
  528. {
  529. unsigned long err = ::ERR_get_error();
  530. if (added && ERR_GET_LIB(err) == ERR_LIB_PEM
  531. && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
  532. break;
  533. ec = translate_error(err);
  534. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  535. }
  536. if (::X509_STORE_add_cert(store, cert.p) != 1)
  537. {
  538. ec = translate_error(::ERR_get_error());
  539. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  540. }
  541. }
  542. }
  543. }
  544. ec = boost::system::error_code();
  545. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  546. }
  547. void context::set_default_verify_paths()
  548. {
  549. boost::system::error_code ec;
  550. set_default_verify_paths(ec);
  551. boost::asio::detail::throw_error(ec, "set_default_verify_paths");
  552. }
  553. BOOST_ASIO_SYNC_OP_VOID context::set_default_verify_paths(
  554. boost::system::error_code& ec)
  555. {
  556. ::ERR_clear_error();
  557. if (::SSL_CTX_set_default_verify_paths(handle_) != 1)
  558. {
  559. ec = translate_error(::ERR_get_error());
  560. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  561. }
  562. ec = boost::system::error_code();
  563. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  564. }
  565. void context::add_verify_path(const std::string& path)
  566. {
  567. boost::system::error_code ec;
  568. add_verify_path(path, ec);
  569. boost::asio::detail::throw_error(ec, "add_verify_path");
  570. }
  571. BOOST_ASIO_SYNC_OP_VOID context::add_verify_path(
  572. const std::string& path, boost::system::error_code& ec)
  573. {
  574. ::ERR_clear_error();
  575. if (::SSL_CTX_load_verify_locations(handle_, 0, path.c_str()) != 1)
  576. {
  577. ec = translate_error(::ERR_get_error());
  578. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  579. }
  580. ec = boost::system::error_code();
  581. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  582. }
  583. void context::use_certificate(
  584. const const_buffer& certificate, file_format format)
  585. {
  586. boost::system::error_code ec;
  587. use_certificate(certificate, format, ec);
  588. boost::asio::detail::throw_error(ec, "use_certificate");
  589. }
  590. BOOST_ASIO_SYNC_OP_VOID context::use_certificate(
  591. const const_buffer& certificate, file_format format,
  592. boost::system::error_code& ec)
  593. {
  594. ::ERR_clear_error();
  595. if (format == context_base::asn1)
  596. {
  597. if (::SSL_CTX_use_certificate_ASN1(handle_,
  598. static_cast<int>(certificate.size()),
  599. static_cast<const unsigned char*>(certificate.data())) == 1)
  600. {
  601. ec = boost::system::error_code();
  602. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  603. }
  604. }
  605. else if (format == context_base::pem)
  606. {
  607. bio_cleanup bio = { make_buffer_bio(certificate) };
  608. if (bio.p)
  609. {
  610. x509_cleanup cert = { ::PEM_read_bio_X509(bio.p, 0, 0, 0) };
  611. if (cert.p)
  612. {
  613. if (::SSL_CTX_use_certificate(handle_, cert.p) == 1)
  614. {
  615. ec = boost::system::error_code();
  616. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  617. }
  618. }
  619. }
  620. }
  621. else
  622. {
  623. ec = boost::asio::error::invalid_argument;
  624. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  625. }
  626. ec = translate_error(::ERR_get_error());
  627. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  628. }
  629. void context::use_certificate_file(
  630. const std::string& filename, file_format format)
  631. {
  632. boost::system::error_code ec;
  633. use_certificate_file(filename, format, ec);
  634. boost::asio::detail::throw_error(ec, "use_certificate_file");
  635. }
  636. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_file(
  637. const std::string& filename, file_format format,
  638. boost::system::error_code& ec)
  639. {
  640. int file_type;
  641. switch (format)
  642. {
  643. case context_base::asn1:
  644. file_type = SSL_FILETYPE_ASN1;
  645. break;
  646. case context_base::pem:
  647. file_type = SSL_FILETYPE_PEM;
  648. break;
  649. default:
  650. {
  651. ec = boost::asio::error::invalid_argument;
  652. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  653. }
  654. }
  655. ::ERR_clear_error();
  656. if (::SSL_CTX_use_certificate_file(handle_, filename.c_str(), file_type) != 1)
  657. {
  658. ec = translate_error(::ERR_get_error());
  659. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  660. }
  661. ec = boost::system::error_code();
  662. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  663. }
  664. void context::use_certificate_chain(const const_buffer& chain)
  665. {
  666. boost::system::error_code ec;
  667. use_certificate_chain(chain, ec);
  668. boost::asio::detail::throw_error(ec, "use_certificate_chain");
  669. }
  670. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_chain(
  671. const const_buffer& chain, boost::system::error_code& ec)
  672. {
  673. ::ERR_clear_error();
  674. bio_cleanup bio = { make_buffer_bio(chain) };
  675. if (bio.p)
  676. {
  677. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  678. && (!defined(LIBRESSL_VERSION_NUMBER) \
  679. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  680. || defined(BOOST_ASIO_USE_WOLFSSL)
  681. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  682. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  683. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  684. pem_password_cb* callback = handle_->default_passwd_callback;
  685. void* cb_userdata = handle_->default_passwd_callback_userdata;
  686. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  687. x509_cleanup cert = {
  688. ::PEM_read_bio_X509_AUX(bio.p, 0,
  689. callback,
  690. cb_userdata) };
  691. if (!cert.p)
  692. {
  693. ec = translate_error(ERR_R_PEM_LIB);
  694. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  695. }
  696. int result = ::SSL_CTX_use_certificate(handle_, cert.p);
  697. if (result == 0 || ::ERR_peek_error() != 0)
  698. {
  699. ec = translate_error(::ERR_get_error());
  700. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  701. }
  702. #if ((OPENSSL_VERSION_NUMBER >= 0x10002000L) \
  703. && (!defined(LIBRESSL_VERSION_NUMBER) \
  704. || LIBRESSL_VERSION_NUMBER >= 0x2090100fL)) \
  705. || defined(BOOST_ASIO_USE_WOLFSSL)
  706. ::SSL_CTX_clear_chain_certs(handle_);
  707. #else
  708. if (handle_->extra_certs)
  709. {
  710. ::sk_X509_pop_free(handle_->extra_certs, X509_free);
  711. handle_->extra_certs = 0;
  712. }
  713. #endif // (OPENSSL_VERSION_NUMBER >= 0x10002000L)
  714. while (X509* cacert = ::PEM_read_bio_X509(bio.p, 0,
  715. callback,
  716. cb_userdata))
  717. {
  718. if (!::SSL_CTX_add_extra_chain_cert(handle_, cacert))
  719. {
  720. ec = translate_error(::ERR_get_error());
  721. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  722. }
  723. }
  724. result = ::ERR_peek_last_error();
  725. if ((ERR_GET_LIB(result) == ERR_LIB_PEM)
  726. && (ERR_GET_REASON(result) == PEM_R_NO_START_LINE))
  727. {
  728. ::ERR_clear_error();
  729. ec = boost::system::error_code();
  730. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  731. }
  732. }
  733. ec = translate_error(::ERR_get_error());
  734. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  735. }
  736. void context::use_certificate_chain_file(const std::string& filename)
  737. {
  738. boost::system::error_code ec;
  739. use_certificate_chain_file(filename, ec);
  740. boost::asio::detail::throw_error(ec, "use_certificate_chain_file");
  741. }
  742. BOOST_ASIO_SYNC_OP_VOID context::use_certificate_chain_file(
  743. const std::string& filename, boost::system::error_code& ec)
  744. {
  745. ::ERR_clear_error();
  746. if (::SSL_CTX_use_certificate_chain_file(handle_, filename.c_str()) != 1)
  747. {
  748. ec = translate_error(::ERR_get_error());
  749. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  750. }
  751. ec = boost::system::error_code();
  752. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  753. }
  754. void context::use_private_key(
  755. const const_buffer& private_key, context::file_format format)
  756. {
  757. boost::system::error_code ec;
  758. use_private_key(private_key, format, ec);
  759. boost::asio::detail::throw_error(ec, "use_private_key");
  760. }
  761. BOOST_ASIO_SYNC_OP_VOID context::use_private_key(
  762. const const_buffer& private_key, context::file_format format,
  763. boost::system::error_code& ec)
  764. {
  765. ::ERR_clear_error();
  766. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  767. && (!defined(LIBRESSL_VERSION_NUMBER) \
  768. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  769. || defined(BOOST_ASIO_USE_WOLFSSL)
  770. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  771. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  772. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  773. pem_password_cb* callback = handle_->default_passwd_callback;
  774. void* cb_userdata = handle_->default_passwd_callback_userdata;
  775. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  776. bio_cleanup bio = { make_buffer_bio(private_key) };
  777. if (bio.p)
  778. {
  779. evp_pkey_cleanup evp_private_key = { 0 };
  780. switch (format)
  781. {
  782. case context_base::asn1:
  783. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  784. break;
  785. case context_base::pem:
  786. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  787. bio.p, 0, callback,
  788. cb_userdata);
  789. break;
  790. default:
  791. {
  792. ec = boost::asio::error::invalid_argument;
  793. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  794. }
  795. }
  796. if (evp_private_key.p)
  797. {
  798. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  799. {
  800. ec = boost::system::error_code();
  801. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  802. }
  803. }
  804. }
  805. ec = translate_error(::ERR_get_error());
  806. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  807. }
  808. void context::use_private_key_file(
  809. const std::string& filename, context::file_format format)
  810. {
  811. boost::system::error_code ec;
  812. use_private_key_file(filename, format, ec);
  813. boost::asio::detail::throw_error(ec, "use_private_key_file");
  814. }
  815. void context::use_rsa_private_key(
  816. const const_buffer& private_key, context::file_format format)
  817. {
  818. boost::system::error_code ec;
  819. use_rsa_private_key(private_key, format, ec);
  820. boost::asio::detail::throw_error(ec, "use_rsa_private_key");
  821. }
  822. BOOST_ASIO_SYNC_OP_VOID context::use_rsa_private_key(
  823. const const_buffer& private_key, context::file_format format,
  824. boost::system::error_code& ec)
  825. {
  826. ::ERR_clear_error();
  827. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  828. && (!defined(LIBRESSL_VERSION_NUMBER) \
  829. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  830. || defined(BOOST_ASIO_USE_WOLFSSL)
  831. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  832. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  833. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  834. pem_password_cb* callback = handle_->default_passwd_callback;
  835. void* cb_userdata = handle_->default_passwd_callback_userdata;
  836. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  837. bio_cleanup bio = { make_buffer_bio(private_key) };
  838. if (bio.p)
  839. {
  840. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  841. evp_pkey_cleanup evp_private_key = { 0 };
  842. switch (format)
  843. {
  844. case context_base::asn1:
  845. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  846. break;
  847. case context_base::pem:
  848. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  849. bio.p, 0, callback,
  850. cb_userdata);
  851. break;
  852. default:
  853. {
  854. ec = boost::asio::error::invalid_argument;
  855. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  856. }
  857. }
  858. if (evp_private_key.p)
  859. {
  860. if (::EVP_PKEY_is_a(evp_private_key.p, "RSA") == 0)
  861. {
  862. ec = translate_error(
  863. ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_RSA_KEY));
  864. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  865. }
  866. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  867. {
  868. ec = boost::system::error_code();
  869. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  870. }
  871. }
  872. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  873. rsa_cleanup rsa_private_key = { 0 };
  874. switch (format)
  875. {
  876. case context_base::asn1:
  877. rsa_private_key.p = ::d2i_RSAPrivateKey_bio(bio.p, 0);
  878. break;
  879. case context_base::pem:
  880. rsa_private_key.p = ::PEM_read_bio_RSAPrivateKey(
  881. bio.p, 0, callback,
  882. cb_userdata);
  883. break;
  884. default:
  885. {
  886. ec = boost::asio::error::invalid_argument;
  887. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  888. }
  889. }
  890. if (rsa_private_key.p)
  891. {
  892. if (::SSL_CTX_use_RSAPrivateKey(handle_, rsa_private_key.p) == 1)
  893. {
  894. ec = boost::system::error_code();
  895. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  896. }
  897. }
  898. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  899. }
  900. ec = translate_error(::ERR_get_error());
  901. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  902. }
  903. BOOST_ASIO_SYNC_OP_VOID context::use_private_key_file(
  904. const std::string& filename, context::file_format format,
  905. boost::system::error_code& ec)
  906. {
  907. int file_type;
  908. switch (format)
  909. {
  910. case context_base::asn1:
  911. file_type = SSL_FILETYPE_ASN1;
  912. break;
  913. case context_base::pem:
  914. file_type = SSL_FILETYPE_PEM;
  915. break;
  916. default:
  917. {
  918. ec = boost::asio::error::invalid_argument;
  919. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  920. }
  921. }
  922. ::ERR_clear_error();
  923. if (::SSL_CTX_use_PrivateKey_file(handle_, filename.c_str(), file_type) != 1)
  924. {
  925. ec = translate_error(::ERR_get_error());
  926. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  927. }
  928. ec = boost::system::error_code();
  929. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  930. }
  931. void context::use_rsa_private_key_file(
  932. const std::string& filename, context::file_format format)
  933. {
  934. boost::system::error_code ec;
  935. use_rsa_private_key_file(filename, format, ec);
  936. boost::asio::detail::throw_error(ec, "use_rsa_private_key_file");
  937. }
  938. BOOST_ASIO_SYNC_OP_VOID context::use_rsa_private_key_file(
  939. const std::string& filename, context::file_format format,
  940. boost::system::error_code& ec)
  941. {
  942. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  943. ::ERR_clear_error();
  944. pem_password_cb* callback = ::SSL_CTX_get_default_passwd_cb(handle_);
  945. void* cb_userdata = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  946. bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
  947. if (bio.p)
  948. {
  949. evp_pkey_cleanup evp_private_key = { 0 };
  950. switch (format)
  951. {
  952. case context_base::asn1:
  953. evp_private_key.p = ::d2i_PrivateKey_bio(bio.p, 0);
  954. break;
  955. case context_base::pem:
  956. evp_private_key.p = ::PEM_read_bio_PrivateKey(
  957. bio.p, 0, callback,
  958. cb_userdata);
  959. break;
  960. default:
  961. {
  962. ec = boost::asio::error::invalid_argument;
  963. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  964. }
  965. }
  966. if (evp_private_key.p)
  967. {
  968. if (::EVP_PKEY_is_a(evp_private_key.p, "RSA") == 0)
  969. {
  970. ec = translate_error(
  971. ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_RSA_KEY));
  972. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  973. }
  974. if (::SSL_CTX_use_PrivateKey(handle_, evp_private_key.p) == 1)
  975. {
  976. ec = boost::system::error_code();
  977. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  978. }
  979. }
  980. }
  981. ec = translate_error(::ERR_get_error());
  982. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  983. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  984. int file_type;
  985. switch (format)
  986. {
  987. case context_base::asn1:
  988. file_type = SSL_FILETYPE_ASN1;
  989. break;
  990. case context_base::pem:
  991. file_type = SSL_FILETYPE_PEM;
  992. break;
  993. default:
  994. {
  995. ec = boost::asio::error::invalid_argument;
  996. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  997. }
  998. }
  999. ::ERR_clear_error();
  1000. if (::SSL_CTX_use_RSAPrivateKey_file(
  1001. handle_, filename.c_str(), file_type) != 1)
  1002. {
  1003. ec = translate_error(::ERR_get_error());
  1004. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1005. }
  1006. ec = boost::system::error_code();
  1007. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1008. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1009. }
  1010. void context::use_tmp_dh(const const_buffer& dh)
  1011. {
  1012. boost::system::error_code ec;
  1013. use_tmp_dh(dh, ec);
  1014. boost::asio::detail::throw_error(ec, "use_tmp_dh");
  1015. }
  1016. BOOST_ASIO_SYNC_OP_VOID context::use_tmp_dh(
  1017. const const_buffer& dh, boost::system::error_code& ec)
  1018. {
  1019. ::ERR_clear_error();
  1020. bio_cleanup bio = { make_buffer_bio(dh) };
  1021. if (bio.p)
  1022. {
  1023. return do_use_tmp_dh(bio.p, ec);
  1024. }
  1025. ec = translate_error(::ERR_get_error());
  1026. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1027. }
  1028. void context::use_tmp_dh_file(const std::string& filename)
  1029. {
  1030. boost::system::error_code ec;
  1031. use_tmp_dh_file(filename, ec);
  1032. boost::asio::detail::throw_error(ec, "use_tmp_dh_file");
  1033. }
  1034. BOOST_ASIO_SYNC_OP_VOID context::use_tmp_dh_file(
  1035. const std::string& filename, boost::system::error_code& ec)
  1036. {
  1037. ::ERR_clear_error();
  1038. bio_cleanup bio = { ::BIO_new_file(filename.c_str(), "r") };
  1039. if (bio.p)
  1040. {
  1041. return do_use_tmp_dh(bio.p, ec);
  1042. }
  1043. ec = translate_error(::ERR_get_error());
  1044. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1045. }
  1046. BOOST_ASIO_SYNC_OP_VOID context::do_use_tmp_dh(
  1047. BIO* bio, boost::system::error_code& ec)
  1048. {
  1049. ::ERR_clear_error();
  1050. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1051. EVP_PKEY* p = ::PEM_read_bio_Parameters(bio, 0);
  1052. if (p)
  1053. {
  1054. if (::SSL_CTX_set0_tmp_dh_pkey(handle_, p) == 1)
  1055. {
  1056. ec = boost::system::error_code();
  1057. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1058. }
  1059. else
  1060. ::EVP_PKEY_free(p);
  1061. }
  1062. #else // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1063. dh_cleanup dh = { ::PEM_read_bio_DHparams(bio, 0, 0, 0) };
  1064. if (dh.p)
  1065. {
  1066. if (::SSL_CTX_set_tmp_dh(handle_, dh.p) == 1)
  1067. {
  1068. ec = boost::system::error_code();
  1069. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1070. }
  1071. }
  1072. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1073. ec = translate_error(::ERR_get_error());
  1074. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1075. }
  1076. BOOST_ASIO_SYNC_OP_VOID context::do_set_verify_callback(
  1077. detail::verify_callback_base* callback, boost::system::error_code& ec)
  1078. {
  1079. if (SSL_CTX_get_app_data(handle_))
  1080. {
  1081. delete static_cast<detail::verify_callback_base*>(
  1082. SSL_CTX_get_app_data(handle_));
  1083. }
  1084. SSL_CTX_set_app_data(handle_, callback);
  1085. ::SSL_CTX_set_verify(handle_,
  1086. ::SSL_CTX_get_verify_mode(handle_),
  1087. &context::verify_callback_function);
  1088. ec = boost::system::error_code();
  1089. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1090. }
  1091. int context::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
  1092. {
  1093. if (ctx)
  1094. {
  1095. if (SSL* ssl = static_cast<SSL*>(
  1096. ::X509_STORE_CTX_get_ex_data(
  1097. ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
  1098. {
  1099. if (SSL_CTX* handle = ::SSL_get_SSL_CTX(ssl))
  1100. {
  1101. if (SSL_CTX_get_app_data(handle))
  1102. {
  1103. detail::verify_callback_base* callback =
  1104. static_cast<detail::verify_callback_base*>(
  1105. SSL_CTX_get_app_data(handle));
  1106. verify_context verify_ctx(ctx);
  1107. return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. return 0;
  1113. }
  1114. BOOST_ASIO_SYNC_OP_VOID context::do_set_password_callback(
  1115. detail::password_callback_base* callback, boost::system::error_code& ec)
  1116. {
  1117. #if ((OPENSSL_VERSION_NUMBER >= 0x10100000L) \
  1118. && (!defined(LIBRESSL_VERSION_NUMBER) \
  1119. || LIBRESSL_VERSION_NUMBER >= 0x2070000fL)) \
  1120. || defined(BOOST_ASIO_USE_WOLFSSL)
  1121. void* old_callback = ::SSL_CTX_get_default_passwd_cb_userdata(handle_);
  1122. ::SSL_CTX_set_default_passwd_cb_userdata(handle_, callback);
  1123. #else // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  1124. void* old_callback = handle_->default_passwd_callback_userdata;
  1125. handle_->default_passwd_callback_userdata = callback;
  1126. #endif // (OPENSSL_VERSION_NUMBER >= 0x10100000L)
  1127. if (old_callback)
  1128. delete static_cast<detail::password_callback_base*>(
  1129. old_callback);
  1130. SSL_CTX_set_default_passwd_cb(handle_, &context::password_callback_function);
  1131. ec = boost::system::error_code();
  1132. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  1133. }
  1134. int context::password_callback_function(
  1135. char* buf, int size, int purpose, void* data)
  1136. {
  1137. using namespace std; // For strncat and strlen.
  1138. if (data)
  1139. {
  1140. detail::password_callback_base* callback =
  1141. static_cast<detail::password_callback_base*>(data);
  1142. std::string passwd = callback->call(static_cast<std::size_t>(size),
  1143. purpose ? context_base::for_writing : context_base::for_reading);
  1144. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  1145. strcpy_s(buf, size, passwd.c_str());
  1146. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  1147. *buf = '\0';
  1148. if (size > 0)
  1149. strncat(buf, passwd.c_str(), size - 1);
  1150. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  1151. return static_cast<int>(strlen(buf));
  1152. }
  1153. return 0;
  1154. }
  1155. BIO* context::make_buffer_bio(const const_buffer& b)
  1156. {
  1157. return ::BIO_new_mem_buf(
  1158. const_cast<void*>(b.data()),
  1159. static_cast<int>(b.size()));
  1160. }
  1161. boost::system::error_code context::translate_error(long error)
  1162. {
  1163. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1164. if (ERR_SYSTEM_ERROR(error))
  1165. {
  1166. return boost::system::error_code(
  1167. static_cast<int>(ERR_GET_REASON(error)),
  1168. boost::asio::error::get_system_category());
  1169. }
  1170. #endif // (OPENSSL_VERSION_NUMBER >= 0x30000000L)
  1171. return boost::system::error_code(static_cast<int>(error),
  1172. boost::asio::error::get_ssl_category());
  1173. }
  1174. } // namespace ssl
  1175. } // namespace asio
  1176. } // namespace boost
  1177. #include <boost/asio/detail/pop_options.hpp>
  1178. #endif // BOOST_ASIO_SSL_IMPL_CONTEXT_IPP