context.ipp 35 KB

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