AsyncSslSocketLayer.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. // CAsyncSslSocketLayer by Tim Kosse ([email protected])
  2. // Version 2.0 (2005-02-27)
  3. //---------------------------------------------------------------------------
  4. // Feel free to use this class, as long as you don't claim that you wrote it
  5. // and this copyright notice stays intact in the source files.
  6. // If you use this class in commercial applications, please send a short message
  7. // to [email protected]
  8. //---------------------------------------------------------------------------
  9. #include "stdafx.h"
  10. #include "AsyncSslSocketLayer.h"
  11. #include "FilezillaTools.h"
  12. #include <openssl/x509v3.h>
  13. #include <openssl/err.h>
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAsyncSslSocketLayer
  16. CCriticalSectionWrapper CAsyncSslSocketLayer::m_sCriticalSection;
  17. CAsyncSslSocketLayer::t_SslLayerList* CAsyncSslSocketLayer::m_pSslLayerList = 0;
  18. int CAsyncSslSocketLayer::m_nSslRefCount = 0;
  19. std::map<SSL_CTX *, int> CAsyncSslSocketLayer::m_contextRefCount;
  20. CAsyncSslSocketLayer::CAsyncSslSocketLayer()
  21. {
  22. m_ssl = 0;
  23. m_sslbio = 0;
  24. m_ibio = 0;
  25. m_nbio = 0;
  26. m_ssl_ctx = 0;
  27. m_bUseSSL = false;
  28. m_bSslInitialized = FALSE;
  29. m_bSslEstablished = FALSE;
  30. m_nNetworkSendBufferLen = 0;
  31. m_nNetworkSendBufferMaxLen = 0;
  32. m_pNetworkSendBuffer = NULL;
  33. m_pRetrySendBuffer = 0;
  34. m_nRetrySendBufferLen = 0;
  35. m_nNetworkError = 0;
  36. m_nShutDown = 0;
  37. m_bBlocking = FALSE;
  38. m_nSslAsyncNotifyId = 0;
  39. m_bFailureSent = FALSE;
  40. m_nVerificationResult = 0;
  41. m_nVerificationDepth = 0;
  42. m_mayTriggerRead = true;
  43. m_mayTriggerWrite = true;
  44. m_mayTriggerReadUp = true;
  45. m_mayTriggerWriteUp = true;
  46. m_onCloseCalled = false;
  47. m_Main = NULL;
  48. m_sessionid = NULL;
  49. m_sessionreuse = true;
  50. m_sessionreuse_failed = false;
  51. FCertificate = NULL;
  52. FPrivateKey = NULL;
  53. }
  54. CAsyncSslSocketLayer::~CAsyncSslSocketLayer()
  55. {
  56. UnloadSSL();
  57. delete [] m_pNetworkSendBuffer;
  58. delete [] m_pRetrySendBuffer;
  59. }
  60. int CAsyncSslSocketLayer::InitSSL()
  61. {
  62. if (m_bSslInitialized)
  63. return 0;
  64. m_sCriticalSection.Lock();
  65. if (!m_nSslRefCount)
  66. {
  67. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL))
  68. {
  69. return SSL_FAILURE_INITSSL;
  70. }
  71. }
  72. m_nSslRefCount++;
  73. m_sCriticalSection.Unlock();
  74. m_bSslInitialized = true;
  75. return 0;
  76. }
  77. void CAsyncSslSocketLayer::OnReceive(int nErrorCode)
  78. {
  79. if (m_bUseSSL)
  80. {
  81. if (m_bBlocking)
  82. {
  83. m_mayTriggerRead = true;
  84. return;
  85. }
  86. if (m_nNetworkError)
  87. {
  88. return;
  89. }
  90. char buffer[16384];
  91. m_mayTriggerRead = false;
  92. //Get number of bytes we can receive and store in the network input bio
  93. int len = BIO_ctrl_get_write_guarantee(m_nbio);
  94. if (len > 16384)
  95. len = 16384;
  96. else if (!len)
  97. {
  98. m_mayTriggerRead = true;
  99. TriggerEvents();
  100. return;
  101. }
  102. int numread = 0;
  103. // Receive data
  104. numread = ReceiveNext(buffer, len);
  105. if (numread > 0)
  106. {
  107. //Store it in the network input bio and process data
  108. int numwritten = BIO_write(m_nbio, buffer, numread);
  109. BIO_ctrl(m_nbio, BIO_CTRL_FLUSH, 0, NULL);
  110. // I have no idea why this call is needed, but without it, connections
  111. // will stall. Perhaps it triggers some internal processing.
  112. // Also, ignore return value, don't do any error checking. This function
  113. // can report errors, even though a later call can succeed.
  114. char buffer;
  115. BIO_read(m_sslbio, &buffer, 0);
  116. }
  117. if (!numread)
  118. {
  119. if (GetLayerState() == connected)
  120. TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
  121. }
  122. else if (numread == SOCKET_ERROR)
  123. {
  124. int nError = GetLastError();
  125. if (nError != WSAEWOULDBLOCK && nError != WSAENOTCONN)
  126. {
  127. m_nNetworkError = GetLastError();
  128. TriggerEvent(FD_CLOSE, 0, TRUE);
  129. return;
  130. }
  131. }
  132. if (m_pRetrySendBuffer)
  133. {
  134. int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
  135. if (numwrite >= 0)
  136. {
  137. BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
  138. delete [] m_pRetrySendBuffer;
  139. m_pRetrySendBuffer = 0;
  140. }
  141. else if (numwrite == -1)
  142. {
  143. if (!BIO_should_retry(m_sslbio))
  144. {
  145. delete [] m_pRetrySendBuffer;
  146. m_pRetrySendBuffer = 0;
  147. ::SetLastError(WSAECONNABORTED);
  148. TriggerEvent(FD_CLOSE, 0, TRUE);
  149. return;
  150. }
  151. }
  152. }
  153. if (!m_nShutDown && SSL_get_shutdown(m_ssl))
  154. {
  155. size_t pending = BIO_ctrl_pending(m_sslbio);
  156. if (pending <= 0)
  157. {
  158. if (ShutDown() || GetLastError() == WSAEWOULDBLOCK)
  159. {
  160. if (ShutDownComplete())
  161. TriggerEvent(FD_CLOSE, 0, TRUE);
  162. }
  163. else
  164. {
  165. m_nNetworkError = WSAECONNABORTED;
  166. WSASetLastError(WSAECONNABORTED);
  167. TriggerEvent(FD_CLOSE, WSAECONNABORTED, TRUE);
  168. }
  169. return;
  170. }
  171. }
  172. if (ShutDownComplete() && m_nShutDown == 1)
  173. {
  174. //Send shutdown notification if all pending data has been sent
  175. DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_INFO, SSL_INFO_SHUTDOWNCOMPLETE);
  176. m_nShutDown++;
  177. }
  178. TriggerEvents();
  179. }
  180. else
  181. {
  182. TriggerEvent(FD_READ, nErrorCode, TRUE);
  183. }
  184. }
  185. void CAsyncSslSocketLayer::OnSend(int nErrorCode)
  186. {
  187. if (m_bUseSSL)
  188. {
  189. if (m_nNetworkError)
  190. {
  191. return;
  192. }
  193. m_mayTriggerWrite = false;
  194. //Send data in the send buffer
  195. while (m_nNetworkSendBufferLen)
  196. {
  197. int numsent = SendNext(m_pNetworkSendBuffer, m_nNetworkSendBufferLen);
  198. if (numsent == SOCKET_ERROR)
  199. {
  200. int nError = GetLastError();
  201. if (nError != WSAEWOULDBLOCK && nError != WSAENOTCONN)
  202. {
  203. m_nNetworkError = nError;
  204. TriggerEvent(FD_CLOSE, 0, TRUE);
  205. }
  206. return;
  207. }
  208. else if (!numsent)
  209. {
  210. if (GetLayerState() == connected)
  211. TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
  212. }
  213. if (numsent == m_nNetworkSendBufferLen)
  214. {
  215. m_nNetworkSendBufferLen = 0;
  216. }
  217. else
  218. {
  219. memmove(m_pNetworkSendBuffer, m_pNetworkSendBuffer + numsent, m_nNetworkSendBufferLen - numsent);
  220. m_nNetworkSendBufferLen -= numsent;
  221. }
  222. }
  223. //Send the data waiting in the network bio
  224. char buffer[32 * 1024];
  225. size_t len = BIO_ctrl_pending(m_nbio);
  226. int numread = BIO_read(m_nbio, buffer, std::min(len, sizeof(buffer)));
  227. if (numread <= 0)
  228. m_mayTriggerWrite = true;
  229. while (numread > 0)
  230. {
  231. int numsent = SendNext(buffer, numread);
  232. if (!numsent)
  233. {
  234. if (GetLayerState() == connected)
  235. TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
  236. }
  237. if (numsent == SOCKET_ERROR || numsent < numread)
  238. {
  239. if (numsent == SOCKET_ERROR)
  240. if (GetLastError() != WSAEWOULDBLOCK && GetLastError() != WSAENOTCONN)
  241. {
  242. m_nNetworkError = GetLastError();
  243. TriggerEvent(FD_CLOSE, 0, TRUE);
  244. return;
  245. }
  246. else
  247. numsent = 0;
  248. // Add all data that was retrieved from the network bio but could not be sent to the send buffer.
  249. if (m_nNetworkSendBufferMaxLen < (m_nNetworkSendBufferLen + numread - numsent))
  250. {
  251. char * tmp = m_pNetworkSendBuffer;
  252. m_nNetworkSendBufferMaxLen = static_cast<int>((m_nNetworkSendBufferLen + numread - numsent) * 1.5);
  253. m_pNetworkSendBuffer = new char[m_nNetworkSendBufferMaxLen];
  254. if (tmp)
  255. {
  256. memcpy(m_pNetworkSendBuffer, tmp, m_nNetworkSendBufferLen);
  257. delete [] tmp;
  258. }
  259. }
  260. DebugAssert(m_pNetworkSendBuffer);
  261. memcpy(m_pNetworkSendBuffer + m_nNetworkSendBufferLen, buffer, numread-numsent);
  262. m_nNetworkSendBufferLen += numread - numsent;
  263. }
  264. if (!numsent)
  265. {
  266. break;
  267. }
  268. len = BIO_ctrl_pending(m_nbio);
  269. if (!len)
  270. {
  271. m_mayTriggerWrite = true;
  272. break;
  273. }
  274. numread = BIO_read(m_nbio, buffer, len);
  275. if (numread <= 0)
  276. {
  277. m_mayTriggerWrite = true;
  278. }
  279. }
  280. if (m_pRetrySendBuffer)
  281. {
  282. int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
  283. if (numwrite >= 0)
  284. {
  285. BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
  286. delete [] m_pRetrySendBuffer;
  287. m_pRetrySendBuffer = 0;
  288. }
  289. else if (numwrite == -1)
  290. {
  291. if (!BIO_should_retry(m_sslbio))
  292. {
  293. delete [] m_pRetrySendBuffer;
  294. m_pRetrySendBuffer = 0;
  295. ::SetLastError(WSAECONNABORTED);
  296. TriggerEvent(FD_CLOSE, 0, TRUE);
  297. return;
  298. }
  299. }
  300. }
  301. // No more data available, ask for more.
  302. TriggerEvents();
  303. if (m_nShutDown == 1 && ShutDownComplete())
  304. {
  305. //Send shutdown notification if all pending data has been sent
  306. // FileZilla3 calls ShutDownNext() here
  307. DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_INFO, SSL_INFO_SHUTDOWNCOMPLETE);
  308. m_nShutDown++;
  309. }
  310. }
  311. else
  312. {
  313. TriggerEvent(FD_WRITE, nErrorCode, TRUE);
  314. }
  315. }
  316. int CAsyncSslSocketLayer::Send(const void* lpBuf, int nBufLen, int nFlags)
  317. {
  318. if (m_bUseSSL)
  319. {
  320. if (!lpBuf)
  321. {
  322. return 0;
  323. }
  324. if (m_bBlocking || m_pRetrySendBuffer)
  325. {
  326. m_mayTriggerWriteUp = true;
  327. ::SetLastError(WSAEWOULDBLOCK);
  328. return SOCKET_ERROR;
  329. }
  330. if (m_nNetworkError)
  331. {
  332. SetLastError(m_nNetworkError);
  333. return SOCKET_ERROR;
  334. }
  335. if (m_nShutDown)
  336. {
  337. ::SetLastError(WSAESHUTDOWN);
  338. return SOCKET_ERROR;
  339. }
  340. if (!m_bSslEstablished)
  341. {
  342. m_mayTriggerWriteUp = true;
  343. ::SetLastError(WSAEWOULDBLOCK);
  344. return SOCKET_ERROR;
  345. }
  346. if (!nBufLen)
  347. {
  348. return 0;
  349. }
  350. if (m_onCloseCalled)
  351. {
  352. TriggerEvent(FD_CLOSE, 0, TRUE);
  353. return 0;
  354. }
  355. int len = BIO_ctrl_get_write_guarantee(m_sslbio);
  356. if (nBufLen > len)
  357. nBufLen = len;
  358. if (!len)
  359. {
  360. m_mayTriggerWriteUp = true;
  361. TriggerEvents();
  362. ::SetLastError(WSAEWOULDBLOCK);
  363. }
  364. m_pRetrySendBuffer = new char[nBufLen];
  365. m_nRetrySendBufferLen = nBufLen;
  366. memcpy(m_pRetrySendBuffer, lpBuf, nBufLen);
  367. int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
  368. if (numwrite >= 0)
  369. {
  370. BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
  371. delete [] m_pRetrySendBuffer;
  372. m_pRetrySendBuffer = 0;
  373. }
  374. else if (numwrite == -1)
  375. {
  376. if (BIO_should_retry(m_sslbio))
  377. {
  378. if (GetLayerState() == closed)
  379. {
  380. return 0;
  381. }
  382. else if (GetLayerState() != connected)
  383. {
  384. SetLastError(m_nNetworkError);
  385. return SOCKET_ERROR;
  386. }
  387. TriggerEvents();
  388. return nBufLen;
  389. }
  390. else
  391. {
  392. delete [] m_pRetrySendBuffer;
  393. m_pRetrySendBuffer = 0;
  394. ::SetLastError(WSAECONNABORTED);
  395. }
  396. return SOCKET_ERROR;
  397. }
  398. m_mayTriggerWriteUp = true;
  399. TriggerEvents();
  400. return numwrite;
  401. }
  402. else
  403. {
  404. return SendNext(lpBuf, nBufLen, nFlags);
  405. }
  406. }
  407. int CAsyncSslSocketLayer::Receive(void* lpBuf, int nBufLen, int nFlags)
  408. {
  409. if (m_bUseSSL)
  410. {
  411. if (m_bBlocking)
  412. {
  413. m_mayTriggerReadUp = true;
  414. ::SetLastError(WSAEWOULDBLOCK);
  415. return SOCKET_ERROR;
  416. }
  417. if (m_nNetworkError)
  418. {
  419. size_t pending = BIO_ctrl_pending(m_sslbio);
  420. if (pending && !m_nShutDown)
  421. {
  422. m_mayTriggerReadUp = true;
  423. TriggerEvents();
  424. return BIO_read(m_sslbio, lpBuf,nBufLen);
  425. }
  426. WSASetLastError(m_nNetworkError);
  427. return SOCKET_ERROR;
  428. }
  429. if (m_nShutDown)
  430. {
  431. ::SetLastError(WSAESHUTDOWN);
  432. return SOCKET_ERROR;
  433. }
  434. if (!nBufLen)
  435. {
  436. return 0;
  437. }
  438. size_t pending = BIO_ctrl_pending(m_sslbio);
  439. if (!pending)
  440. {
  441. if (GetLayerState() == closed)
  442. {
  443. return 0;
  444. }
  445. if (m_onCloseCalled)
  446. {
  447. TriggerEvent(FD_CLOSE, 0, TRUE);
  448. return 0;
  449. }
  450. else if (GetLayerState() != connected)
  451. {
  452. SetLastError(m_nNetworkError);
  453. return SOCKET_ERROR;
  454. }
  455. else
  456. {
  457. if (SSL_get_shutdown(m_ssl))
  458. {
  459. if (ShutDown() || GetLastError() == WSAEWOULDBLOCK)
  460. {
  461. if (ShutDownComplete())
  462. {
  463. TriggerEvent(FD_CLOSE, 0, TRUE);
  464. return 0;
  465. }
  466. else
  467. WSASetLastError(WSAEWOULDBLOCK);
  468. }
  469. else
  470. {
  471. m_nNetworkError = WSAECONNABORTED;
  472. WSASetLastError(WSAECONNABORTED);
  473. TriggerEvent(FD_CLOSE, WSAECONNABORTED, TRUE);
  474. }
  475. return SOCKET_ERROR;
  476. }
  477. }
  478. m_mayTriggerReadUp = true;
  479. TriggerEvents();
  480. ::SetLastError(WSAEWOULDBLOCK);
  481. return SOCKET_ERROR;
  482. }
  483. int numread = BIO_read(m_sslbio, lpBuf, nBufLen);
  484. if (!numread)
  485. {
  486. if (SSL_get_shutdown(m_ssl))
  487. {
  488. if (ShutDown() || GetLastError() == WSAEWOULDBLOCK)
  489. {
  490. if (ShutDownComplete())
  491. {
  492. TriggerEvent(FD_CLOSE, 0, TRUE);
  493. return 0;
  494. }
  495. else
  496. WSASetLastError(WSAEWOULDBLOCK);
  497. }
  498. else
  499. {
  500. m_nNetworkError = WSAECONNABORTED;
  501. WSASetLastError(WSAECONNABORTED);
  502. TriggerEvent(FD_CLOSE, WSAECONNABORTED, TRUE);
  503. }
  504. return SOCKET_ERROR;
  505. }
  506. m_mayTriggerReadUp = true;
  507. TriggerEvents();
  508. ::SetLastError(WSAEWOULDBLOCK);
  509. return SOCKET_ERROR;
  510. }
  511. if (numread < 0)
  512. {
  513. if (!BIO_should_retry(m_sslbio))
  514. {
  515. PrintLastErrorMsg();
  516. m_nNetworkError = WSAECONNABORTED;
  517. WSASetLastError(WSAECONNABORTED);
  518. TriggerEvent(FD_CLOSE, 0, TRUE);
  519. return SOCKET_ERROR;
  520. }
  521. else
  522. {
  523. if (SSL_get_shutdown(m_ssl))
  524. {
  525. if (ShutDown() || GetLastError() == WSAEWOULDBLOCK)
  526. {
  527. if (ShutDownComplete())
  528. {
  529. TriggerEvent(FD_CLOSE, 0, TRUE);
  530. return 0;
  531. }
  532. else
  533. WSASetLastError(WSAEWOULDBLOCK);
  534. }
  535. else
  536. {
  537. m_nNetworkError = WSAECONNABORTED;
  538. WSASetLastError(WSAECONNABORTED);
  539. TriggerEvent(FD_CLOSE, 0, TRUE);
  540. }
  541. return SOCKET_ERROR;
  542. }
  543. m_mayTriggerReadUp = true;
  544. TriggerEvents();
  545. ::SetLastError(WSAEWOULDBLOCK);
  546. return SOCKET_ERROR;
  547. }
  548. }
  549. m_mayTriggerReadUp = true;
  550. TriggerEvents();
  551. return numread;
  552. }
  553. else
  554. {
  555. return ReceiveNext(lpBuf, nBufLen, nFlags);
  556. }
  557. }
  558. void CAsyncSslSocketLayer::Close()
  559. {
  560. m_nShutDown = 0;
  561. m_onCloseCalled = false;
  562. ResetSslSession();
  563. CloseNext();
  564. }
  565. BOOL CAsyncSslSocketLayer::Connect(const SOCKADDR *lpSockAddr, int nSockAddrLen)
  566. {
  567. BOOL res = ConnectNext(lpSockAddr, nSockAddrLen);
  568. if (!res)
  569. {
  570. if (GetLastError() != WSAEWOULDBLOCK)
  571. {
  572. ResetSslSession();
  573. }
  574. }
  575. return res;
  576. }
  577. BOOL CAsyncSslSocketLayer::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
  578. {
  579. BOOL res = ConnectNext(lpszHostAddress, nHostPort);
  580. if (!res)
  581. {
  582. if (GetLastError()!=WSAEWOULDBLOCK)
  583. {
  584. ResetSslSession();
  585. }
  586. }
  587. return res;
  588. }
  589. bool CAsyncSslSocketLayer::HandleSession(SSL_SESSION * Session)
  590. {
  591. bool Result = false;
  592. if (m_sessionreuse)
  593. {
  594. if (m_sessionid != Session)
  595. {
  596. if (m_sessionid == NULL)
  597. {
  598. if (SSL_session_reused(m_ssl))
  599. {
  600. LogSocketMessageRaw(FZ_LOG_PROGRESS, L"Session ID reused");
  601. }
  602. else
  603. {
  604. if ((m_Main != NULL) && !m_Main->m_sessionreuse_failed)
  605. {
  606. LogSocketMessageRaw(FZ_LOG_INFO, L"Main TLS session ID not reused, will not try again");
  607. m_Main->m_sessionreuse_failed = true;
  608. }
  609. }
  610. LogSocketMessageRaw(FZ_LOG_DEBUG, L"Saving session ID");
  611. }
  612. else
  613. {
  614. SSL_SESSION_free(m_sessionid);
  615. LogSocketMessageRaw(FZ_LOG_INFO, L"Session ID changed");
  616. }
  617. m_sessionid = Session;
  618. Result = true;
  619. }
  620. }
  621. return Result;
  622. }
  623. int CAsyncSslSocketLayer::NewSessionCallback(struct ssl_st * Ssl, SSL_SESSION * Session)
  624. {
  625. CAsyncSslSocketLayer * Layer = LookupLayer(Ssl);
  626. int Result = 0;
  627. // This is not called for TLS 1.2 and older when session is reused (so "Session ID reused" won't be logged).
  628. // So for 1.2 and older, we call HandleSession from apps_ssl_info_callback as we always did.
  629. if ((SSL_version(Ssl) >= TLS1_3_VERSION) && Layer->HandleSession(Session))
  630. {
  631. Result = 1;
  632. }
  633. return Result;
  634. }
  635. int CAsyncSslSocketLayer::InitSSLConnection(bool clientMode,
  636. CAsyncSslSocketLayer* main, bool sessionreuse,
  637. CFileZillaTools * tools,
  638. void* pSslContext /*=0*/)
  639. {
  640. if (m_bUseSSL)
  641. return 0;
  642. int res = InitSSL();
  643. if (res)
  644. return res;
  645. m_sCriticalSection.Lock();
  646. if ((SSL_CTX*)pSslContext)
  647. {
  648. if (m_ssl_ctx)
  649. {
  650. m_sCriticalSection.Unlock();
  651. ResetSslSession();
  652. return SSL_FAILURE_INITSSL;
  653. }
  654. std::map<SSL_CTX *, int>::iterator iter = m_contextRefCount.find((SSL_CTX*)pSslContext);
  655. if (iter == m_contextRefCount.end() || iter->second < 1)
  656. {
  657. m_sCriticalSection.Unlock();
  658. ResetSslSession();
  659. return SSL_FAILURE_INITSSL;
  660. }
  661. m_ssl_ctx = (SSL_CTX*)pSslContext;
  662. iter->second++;
  663. }
  664. else if (!m_ssl_ctx)
  665. {
  666. // Create new context if none given
  667. if (!(m_ssl_ctx = SSL_CTX_new( SSLv23_method())))
  668. {
  669. m_sCriticalSection.Unlock();
  670. ResetSslSession();
  671. return SSL_FAILURE_INITSSL;
  672. }
  673. m_contextRefCount[m_ssl_ctx] = 1;
  674. if (clientMode)
  675. {
  676. USES_CONVERSION;
  677. SSL_CTX_set_verify(m_ssl_ctx, SSL_VERIFY_PEER, verify_callback);
  678. SSL_CTX_set_client_cert_cb(m_ssl_ctx, ProvideClientCert);
  679. // https://www.mail-archive.com/[email protected]/msg86186.html
  680. SSL_CTX_set_session_cache_mode(m_ssl_ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE | SSL_SESS_CACHE_NO_AUTO_CLEAR);
  681. SSL_CTX_sess_set_new_cb(m_ssl_ctx, NewSessionCallback);
  682. CFileStatus Dummy;
  683. if (CFile::GetStatus((LPCTSTR)m_CertStorage, Dummy))
  684. {
  685. SSL_CTX_load_verify_locations(m_ssl_ctx, T2CA(m_CertStorage), 0);
  686. }
  687. }
  688. }
  689. //Create new SSL session
  690. if (!(m_ssl = SSL_new(m_ssl_ctx)))
  691. {
  692. m_sCriticalSection.Unlock();
  693. ResetSslSession();
  694. return SSL_FAILURE_INITSSL;
  695. }
  696. #ifdef _DEBUG
  697. if ((main == NULL) && LoggingSocketMessage(FZ_LOG_INFO))
  698. {
  699. USES_CONVERSION;
  700. LogSocketMessageRaw(FZ_LOG_INFO, L"Supported ciphersuites:");
  701. STACK_OF(SSL_CIPHER) * ciphers = SSL_get_ciphers(m_ssl);
  702. for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); i++)
  703. {
  704. const SSL_CIPHER * cipher = sk_SSL_CIPHER_value(ciphers, i);
  705. LogSocketMessageRaw(FZ_LOG_INFO, A2CT(SSL_CIPHER_get_name(cipher)));
  706. }
  707. }
  708. #endif
  709. //Add current instance to list of active instances
  710. t_SslLayerList *tmp = m_pSslLayerList;
  711. m_pSslLayerList = new t_SslLayerList;
  712. m_pSslLayerList->pNext = tmp;
  713. m_pSslLayerList->pLayer = this;
  714. m_sCriticalSection.Unlock();
  715. SSL_set_info_callback(m_ssl, apps_ssl_info_callback);
  716. //Create bios
  717. m_sslbio = BIO_new(BIO_f_ssl());
  718. BIO_new_bio_pair(&m_ibio, 32768, &m_nbio, 32768);
  719. if (!m_sslbio || !m_nbio || !m_ibio)
  720. {
  721. ResetSslSession();
  722. return SSL_FAILURE_INITSSL;
  723. }
  724. tools->SetupSsl(m_ssl);
  725. //Init SSL connection
  726. void *ssl_sessionid = NULL;
  727. m_Main = main;
  728. m_sessionreuse = sessionreuse;
  729. if ((m_Main != NULL) && m_sessionreuse)
  730. {
  731. if (m_Main->m_sessionid == NULL)
  732. {
  733. DebugFail();
  734. SSL_set_session(m_ssl, NULL);
  735. }
  736. else if (!m_Main->m_sessionreuse_failed)
  737. {
  738. if (!SSL_set_session(m_ssl, m_Main->m_sessionid))
  739. {
  740. LogSocketMessageRaw(FZ_LOG_INFO, L"SSL_set_session failed");
  741. return SSL_FAILURE_INITSSL;
  742. }
  743. LogSocketMessageRaw(FZ_LOG_INFO, L"Trying reuse main TLS session ID");
  744. }
  745. else
  746. {
  747. LogSocketMessageRaw(FZ_LOG_INFO, L"Main TLS session ID was not reused previously, not trying again");
  748. SSL_set_session(m_ssl, NULL);
  749. }
  750. }
  751. else
  752. {
  753. SSL_set_session(m_ssl, NULL);
  754. }
  755. if (clientMode)
  756. {
  757. SSL_set_connect_state(m_ssl);
  758. }
  759. else
  760. {
  761. SSL_set_accept_state(m_ssl);
  762. }
  763. SSL_set_bio(m_ssl, m_ibio, m_ibio);
  764. BIO_ctrl(m_sslbio, BIO_C_SET_SSL, BIO_NOCLOSE, m_ssl);
  765. BIO_read(m_sslbio, (void *)1, 0);
  766. // Trigger FD_WRITE so that we can initialize SSL negotiation
  767. if (GetLayerState() == connected || GetLayerState() == attached)
  768. {
  769. TriggerEvent(FD_READ, 0);
  770. TriggerEvent(FD_WRITE, 0);
  771. TriggerEvent(FD_READ, 0, TRUE);
  772. TriggerEvent(FD_WRITE, 0, TRUE);
  773. }
  774. m_bUseSSL = true;
  775. return 0;
  776. }
  777. void CAsyncSslSocketLayer::ResetSslSession()
  778. {
  779. if (m_pRetrySendBuffer)
  780. {
  781. delete [] m_pRetrySendBuffer;
  782. m_pRetrySendBuffer = 0;
  783. }
  784. m_bFailureSent = FALSE;
  785. m_bBlocking = FALSE;
  786. m_nSslAsyncNotifyId++;
  787. m_nNetworkError = 0;
  788. m_bUseSSL = FALSE;
  789. m_nVerificationResult = 0;
  790. m_nVerificationDepth = 0;
  791. m_bSslEstablished = FALSE;
  792. if (m_sslbio)
  793. {
  794. BIO_free(m_sslbio);
  795. }
  796. if (m_ssl)
  797. {
  798. SSL_set_session(m_ssl,NULL);
  799. }
  800. if (m_nbio)
  801. {
  802. BIO_free(m_nbio);
  803. }
  804. if (m_ibio)
  805. {
  806. BIO_free(m_ibio);
  807. }
  808. m_nNetworkSendBufferLen = 0;
  809. m_nbio = 0;
  810. m_ibio = 0;
  811. m_sslbio = 0;
  812. if (m_ssl)
  813. {
  814. SSL_free(m_ssl);
  815. }
  816. m_sCriticalSection.Lock();
  817. if (m_ssl_ctx)
  818. {
  819. std::map<SSL_CTX *, int>::iterator iter = m_contextRefCount.find(m_ssl_ctx);
  820. if (iter != m_contextRefCount.end())
  821. {
  822. if (iter->second <= 1)
  823. {
  824. SSL_CTX_free(m_ssl_ctx);
  825. m_contextRefCount.erase(iter);
  826. }
  827. else
  828. iter->second--;
  829. }
  830. m_ssl_ctx = 0;
  831. }
  832. m_ssl = 0;
  833. t_SslLayerList *cur = m_pSslLayerList;
  834. if (!cur)
  835. {
  836. m_sCriticalSection.Unlock();
  837. return;
  838. }
  839. if (cur->pLayer == this)
  840. {
  841. m_pSslLayerList = cur->pNext;
  842. delete cur;
  843. }
  844. else
  845. while (cur->pNext)
  846. {
  847. if (cur->pNext->pLayer == this)
  848. {
  849. t_SslLayerList *tmp = cur->pNext;
  850. cur->pNext = cur->pNext->pNext;
  851. delete tmp;
  852. m_sCriticalSection.Unlock();
  853. return;
  854. }
  855. cur = cur->pNext;
  856. }
  857. if (m_sessionid != NULL)
  858. {
  859. SSL_SESSION_free(m_sessionid);
  860. m_sessionid = NULL;
  861. }
  862. m_sessionreuse = true;
  863. m_sessionreuse_failed = false;
  864. m_sCriticalSection.Unlock();
  865. }
  866. bool CAsyncSslSocketLayer::IsUsingSSL()
  867. {
  868. return m_bUseSSL;
  869. }
  870. BOOL CAsyncSslSocketLayer::ShutDown(int nHow /*=sends*/)
  871. {
  872. if (m_bUseSSL)
  873. {
  874. if (m_pRetrySendBuffer)
  875. {
  876. if (!m_nShutDown)
  877. m_nShutDown = 1;
  878. WSASetLastError(WSAEWOULDBLOCK);
  879. return false;
  880. }
  881. if (!m_bSslEstablished)
  882. {
  883. m_mayTriggerWriteUp = true;
  884. ::SetLastError(WSAEWOULDBLOCK);
  885. return false;
  886. }
  887. if (!m_nShutDown)
  888. m_nShutDown = 1;
  889. else
  890. {
  891. if (ShutDownComplete())
  892. return ShutDownNext();
  893. else
  894. {
  895. TriggerEvents();
  896. WSASetLastError(WSAEWOULDBLOCK);
  897. return false;
  898. }
  899. }
  900. int res = SSL_shutdown(m_ssl);
  901. if (res == 0)
  902. {
  903. res = SSL_shutdown(m_ssl);
  904. }
  905. if (res >= 0)
  906. {
  907. if (ShutDownComplete())
  908. return ShutDownNext();
  909. else
  910. {
  911. TriggerEvents();
  912. WSASetLastError(WSAEWOULDBLOCK);
  913. return FALSE;
  914. }
  915. }
  916. else
  917. {
  918. int error = SSL_get_error(m_ssl, -1);
  919. if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)
  920. {
  921. // retry shutdown later
  922. m_nShutDown = 0;
  923. TriggerEvents();
  924. WSASetLastError(WSAEWOULDBLOCK);
  925. return FALSE;
  926. }
  927. else if (ShutDownComplete())
  928. return ShutDownNext();
  929. else
  930. {
  931. TriggerEvents();
  932. WSASetLastError(WSAEWOULDBLOCK);
  933. return FALSE;
  934. }
  935. }
  936. }
  937. else
  938. {
  939. if (!m_nShutDown)
  940. m_nShutDown = 1;
  941. return ShutDownNext(nHow);
  942. }
  943. }
  944. BOOL CAsyncSslSocketLayer::ShutDownComplete()
  945. {
  946. //If a ShutDown was issued, has the connection already been shut down?
  947. if (!m_nShutDown)
  948. return FALSE;
  949. else if (!m_bUseSSL)
  950. return FALSE;
  951. else if (m_nNetworkSendBufferLen)
  952. return FALSE;
  953. else if (m_pRetrySendBuffer)
  954. return FALSE;
  955. // Empty read buffer
  956. char buffer[1000];
  957. int numread;
  958. do
  959. {
  960. numread = BIO_read(m_sslbio, buffer, 1000);
  961. } while (numread > 0);
  962. size_t pending = BIO_ctrl_pending(m_nbio);
  963. if (pending)
  964. {
  965. return FALSE;
  966. }
  967. else
  968. {
  969. return TRUE;
  970. }
  971. }
  972. void CAsyncSslSocketLayer::apps_ssl_info_callback(const SSL *s, int where, int ret)
  973. {
  974. USES_CONVERSION;
  975. CAsyncSslSocketLayer *pLayer = 0;
  976. m_sCriticalSection.Lock();
  977. t_SslLayerList *cur = m_pSslLayerList;
  978. while (cur)
  979. {
  980. if (cur->pLayer->m_ssl == s)
  981. break;
  982. cur = cur->pNext;
  983. }
  984. if (!cur)
  985. {
  986. m_sCriticalSection.Unlock();
  987. MessageBox(0, L"Can't lookup TLS session!", L"Critical error", MB_ICONEXCLAMATION);
  988. return;
  989. }
  990. else
  991. pLayer = cur->pLayer;
  992. m_sCriticalSection.Unlock();
  993. // Called while unloading?
  994. if (!pLayer->m_bUseSSL && (where != SSL_CB_LOOP))
  995. return;
  996. char * str;
  997. int w;
  998. w = where& ~SSL_ST_MASK;
  999. if (w & SSL_ST_CONNECT)
  1000. {
  1001. str = "TLS connect";
  1002. }
  1003. else if (w & SSL_ST_ACCEPT)
  1004. str = "TLS accept";
  1005. else
  1006. str = "Undefined";
  1007. if (where & SSL_CB_LOOP)
  1008. {
  1009. char* debug = NULL;
  1010. // exact SSL_CB_LOOP is abused for debugging
  1011. if (where == SSL_CB_LOOP)
  1012. {
  1013. debug = reinterpret_cast<char*>(ret);
  1014. }
  1015. char *buffer = new char[4096 + ((debug != NULL) ? strlen(debug) : 0)];
  1016. sprintf(buffer, "%s: %s",
  1017. str,
  1018. SSL_state_string_long(s));
  1019. if (debug != NULL)
  1020. {
  1021. sprintf(buffer + strlen(buffer), " [%s]", debug);
  1022. OPENSSL_free(debug);
  1023. }
  1024. USES_CONVERSION;
  1025. pLayer->LogSocketMessageRaw(FZ_LOG_INFO, A2T(buffer));
  1026. delete[] buffer;
  1027. }
  1028. else if (where & SSL_CB_ALERT)
  1029. {
  1030. str=(where & SSL_CB_READ)? "read" : "write";
  1031. const char* desc = SSL_alert_desc_string_long(ret);
  1032. // Don't send close notify warning
  1033. if (desc)
  1034. {
  1035. if (strcmp(desc, "close notify"))
  1036. {
  1037. char *buffer = new char[4096];
  1038. sprintf(buffer, "SSL3 alert %s: %s: %s",
  1039. str,
  1040. SSL_alert_type_string_long(ret),
  1041. desc);
  1042. pLayer->LogSocketMessageRaw(FZ_LOG_WARNING, A2T(buffer));
  1043. pLayer->PrintLastErrorMsg();
  1044. delete [] buffer;
  1045. }
  1046. }
  1047. }
  1048. else if (where & SSL_CB_EXIT)
  1049. {
  1050. if (ret == 0)
  1051. {
  1052. char *buffer = new char[4096];
  1053. sprintf(buffer, "%s: failed in %s",
  1054. str,
  1055. SSL_state_string_long(s));
  1056. pLayer->LogSocketMessageRaw(FZ_LOG_WARNING, A2T(buffer));
  1057. pLayer->PrintLastErrorMsg();
  1058. delete [] buffer;
  1059. if (!pLayer->m_bFailureSent)
  1060. {
  1061. pLayer->m_bFailureSent=TRUE;
  1062. pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, pLayer->m_bSslEstablished ? SSL_FAILURE_UNKNOWN : SSL_FAILURE_ESTABLISH);
  1063. }
  1064. }
  1065. else if (ret < 0)
  1066. {
  1067. int error = SSL_get_error(s,ret);
  1068. if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE)
  1069. {
  1070. char *buffer = new char[4096];
  1071. sprintf(buffer, "%s: error in %s",
  1072. str,
  1073. SSL_state_string_long(s));
  1074. pLayer->LogSocketMessageRaw(FZ_LOG_WARNING, A2T(buffer));
  1075. delete [] buffer;
  1076. if (!pLayer->m_bFailureSent)
  1077. {
  1078. pLayer->m_bFailureSent=TRUE;
  1079. pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, pLayer->m_bSslEstablished ? SSL_FAILURE_UNKNOWN : SSL_FAILURE_ESTABLISH);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. if (where & SSL_CB_HANDSHAKE_DONE)
  1085. {
  1086. // For 1.2 and older, session is always established at this point.
  1087. // For 1.3, session can be restarted later, so this is handled in NewSessionCallback.
  1088. if (SSL_version(pLayer->m_ssl) < TLS1_3_VERSION)
  1089. {
  1090. SSL_SESSION * sessionid = SSL_get1_session(pLayer->m_ssl);
  1091. if (!pLayer->HandleSession(sessionid))
  1092. {
  1093. SSL_SESSION_free(sessionid);
  1094. }
  1095. }
  1096. int error = SSL_get_verify_result(pLayer->m_ssl);
  1097. pLayer->DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_VERIFY_CERT, error);
  1098. pLayer->m_bBlocking = TRUE;
  1099. }
  1100. }
  1101. void CAsyncSslSocketLayer::UnloadSSL()
  1102. {
  1103. if (!m_bSslInitialized)
  1104. return;
  1105. ResetSslSession();
  1106. m_bSslInitialized = false;
  1107. m_sCriticalSection.Lock();
  1108. m_nSslRefCount--;
  1109. if (m_nSslRefCount)
  1110. {
  1111. m_sCriticalSection.Unlock();
  1112. return;
  1113. }
  1114. m_sCriticalSection.Unlock();
  1115. }
  1116. bool AsnTimeToValidTime(ASN1_TIME * AsnTime, t_SslCertData::t_validTime & ValidTime)
  1117. {
  1118. int i = AsnTime->length;
  1119. const char * v = (const char *)AsnTime->data;
  1120. if (i < 10)
  1121. {
  1122. return FALSE;
  1123. }
  1124. for (int i2 = 0; i2 < 10; i2++)
  1125. {
  1126. if ((v[i2] > '9') || (v[i2] < '0'))
  1127. {
  1128. return FALSE;
  1129. }
  1130. }
  1131. if (AsnTime->type == V_ASN1_UTCTIME)
  1132. {
  1133. ValidTime.y= (v[0]-'0')*10+(v[1]-'0');
  1134. if (ValidTime.y < 50) ValidTime.y+=100;
  1135. ValidTime.y += 1900;
  1136. v += 2;
  1137. i -= 2;
  1138. }
  1139. else if (AsnTime->type == V_ASN1_GENERALIZEDTIME)
  1140. {
  1141. if (i < 12)
  1142. {
  1143. return FALSE;
  1144. }
  1145. ValidTime.y = (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0');
  1146. v += 4;
  1147. i -= 4;
  1148. }
  1149. else
  1150. {
  1151. return FALSE;
  1152. }
  1153. ValidTime.M = (v[0]-'0')*10+(v[1]-'0');
  1154. if ((ValidTime.M > 12) || (ValidTime.M < 1))
  1155. {
  1156. return FALSE;
  1157. }
  1158. ValidTime.d = (v[2]-'0')*10+(v[3]-'0');
  1159. ValidTime.h = (v[4]-'0')*10+(v[5]-'0');
  1160. ValidTime.m = (v[6]-'0')*10+(v[7]-'0');
  1161. if ((i >= 10) &&
  1162. (v[8] >= '0') && (v[8] <= '9') &&
  1163. (v[9] >= '0') && (v[9] <= '9'))
  1164. {
  1165. ValidTime.s = (v[8]-'0')*10+(v[9]-'0');
  1166. }
  1167. else
  1168. {
  1169. ValidTime.s = 0;
  1170. }
  1171. return TRUE;
  1172. }
  1173. BOOL CAsyncSslSocketLayer::GetPeerCertificateData(t_SslCertData &SslCertData, LPCTSTR & Error)
  1174. {
  1175. X509 *pX509=SSL_get_peer_certificate(m_ssl);
  1176. if (!pX509)
  1177. {
  1178. Error = L"Cannot get certificate";
  1179. return FALSE;
  1180. }
  1181. //Reset the contents of SslCertData
  1182. memset(&SslCertData, 0, sizeof(t_SslCertData));
  1183. //Set subject data fields
  1184. X509_NAME *pX509Name=X509_get_subject_name(pX509);
  1185. if (pX509Name)
  1186. {
  1187. int count=X509_NAME_entry_count(pX509Name);
  1188. for (int i=0;i<count;i++)
  1189. {
  1190. X509_NAME_ENTRY *pX509NameEntry=X509_NAME_get_entry(pX509Name,i);
  1191. if (!pX509NameEntry)
  1192. continue;
  1193. ASN1_OBJECT *pObject = X509_NAME_ENTRY_get_object(pX509NameEntry);
  1194. ASN1_STRING *pString = X509_NAME_ENTRY_get_data(pX509NameEntry);
  1195. CString str;
  1196. unsigned char *out;
  1197. int len = ASN1_STRING_to_UTF8(&out, pString);
  1198. if (len > 0)
  1199. {
  1200. // Keep it huge
  1201. LPWSTR unicode = new WCHAR[len * 10];
  1202. memset(unicode, 0, sizeof(WCHAR) * len * 10);
  1203. int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, (const char *)out, len, unicode, len * 10);
  1204. if (unicodeLen > 0)
  1205. {
  1206. #ifdef _UNICODE
  1207. str = unicode;
  1208. #else
  1209. LPSTR ansi = new CHAR[len * 10];
  1210. memset(ansi, 0, sizeof(CHAR) * len * 10);
  1211. int ansiLen = WideCharToMultiByte(CP_ACP, 0, unicode, unicodeLen, ansi, len * 10, 0, 0);
  1212. if (ansiLen > 0)
  1213. str = ansi;
  1214. delete [] ansi;
  1215. #endif
  1216. }
  1217. delete [] unicode;
  1218. OPENSSL_free(out);
  1219. }
  1220. switch(OBJ_obj2nid(pObject))
  1221. {
  1222. case NID_organizationName:
  1223. _tcsncpy(SslCertData.subject.Organization, str, 255);
  1224. SslCertData.subject.Organization[255] = 0;
  1225. break;
  1226. case NID_organizationalUnitName:
  1227. _tcsncpy(SslCertData.subject.Unit, str, 255);
  1228. SslCertData.subject.Unit[255] = 0;
  1229. break;
  1230. case NID_commonName:
  1231. _tcsncpy(SslCertData.subject.CommonName, str, 255);
  1232. SslCertData.subject.CommonName[255] = 0;
  1233. break;
  1234. case NID_pkcs9_emailAddress:
  1235. _tcsncpy(SslCertData.subject.Mail, str, 255);
  1236. SslCertData.subject.Mail[255] = 0;
  1237. break;
  1238. case NID_countryName:
  1239. _tcsncpy(SslCertData.subject.Country, str, 255);
  1240. SslCertData.subject.Country[255] = 0;
  1241. break;
  1242. case NID_stateOrProvinceName:
  1243. _tcsncpy(SslCertData.subject.StateProvince, str, 255);
  1244. SslCertData.subject.StateProvince[255] = 0;
  1245. break;
  1246. case NID_localityName:
  1247. _tcsncpy(SslCertData.subject.Town, str, 255);
  1248. SslCertData.subject.Town[255] = 0;
  1249. break;
  1250. default:
  1251. if ( !OBJ_nid2sn(OBJ_obj2nid(pObject)) )
  1252. {
  1253. TCHAR tmp[20];
  1254. _stprintf(tmp, L"%d", OBJ_obj2nid(pObject));
  1255. int maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1256. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), tmp, maxlen);
  1257. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1258. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), L"=", maxlen);
  1259. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1260. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), str, maxlen);
  1261. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1262. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), L";", maxlen);
  1263. }
  1264. else
  1265. {
  1266. int maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1267. USES_CONVERSION;
  1268. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), A2CT(OBJ_nid2sn(OBJ_obj2nid(pObject))), maxlen);
  1269. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1270. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), L"=", maxlen);
  1271. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1272. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), str, maxlen);
  1273. maxlen = 1024 - _tcslen(SslCertData.subject.Other)-1;
  1274. _tcsncpy(SslCertData.subject.Other+_tcslen(SslCertData.subject.Other), L";", maxlen);
  1275. }
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. //Set issuer data fields
  1281. pX509Name=X509_get_issuer_name(pX509);
  1282. if (pX509Name)
  1283. {
  1284. int count=X509_NAME_entry_count(pX509Name);
  1285. for (int i=0;i<count;i++)
  1286. {
  1287. X509_NAME_ENTRY *pX509NameEntry=X509_NAME_get_entry(pX509Name,i);
  1288. if (!pX509NameEntry)
  1289. continue;
  1290. ASN1_STRING *pString=X509_NAME_ENTRY_get_data(pX509NameEntry);
  1291. ASN1_OBJECT *pObject=X509_NAME_ENTRY_get_object(pX509NameEntry);
  1292. CString str;
  1293. unsigned char *out;
  1294. int len = ASN1_STRING_to_UTF8(&out, pString);
  1295. if (len > 0)
  1296. {
  1297. // Keep it huge
  1298. LPWSTR unicode = new WCHAR[len * 10];
  1299. memset(unicode, 0, sizeof(WCHAR) * len * 10);
  1300. int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, (const char *)out, len, unicode, len * 10);
  1301. if (unicodeLen > 0)
  1302. {
  1303. #ifdef _UNICODE
  1304. str = unicode;
  1305. #else
  1306. LPSTR ansi = new CHAR[len * 10];
  1307. memset(ansi, 0, sizeof(CHAR) * len * 10);
  1308. int ansiLen = WideCharToMultiByte(CP_ACP, 0, unicode, unicodeLen, ansi, len * 10, 0, 0);
  1309. if (ansiLen > 0)
  1310. str = ansi;
  1311. delete [] ansi;
  1312. #endif
  1313. }
  1314. delete [] unicode;
  1315. OPENSSL_free(out);
  1316. }
  1317. switch(OBJ_obj2nid(pObject))
  1318. {
  1319. case NID_organizationName:
  1320. _tcsncpy(SslCertData.issuer.Organization, str, 255);
  1321. SslCertData.issuer.Organization[255] = 0;
  1322. break;
  1323. case NID_organizationalUnitName:
  1324. _tcsncpy(SslCertData.issuer.Unit, str, 255);
  1325. SslCertData.issuer.Unit[255] = 0;
  1326. break;
  1327. case NID_commonName:
  1328. _tcsncpy(SslCertData.issuer.CommonName, str, 255);
  1329. SslCertData.issuer.CommonName[255] = 0;
  1330. break;
  1331. case NID_pkcs9_emailAddress:
  1332. _tcsncpy(SslCertData.issuer.Mail, str, 255);
  1333. SslCertData.issuer.Mail[255] = 0;
  1334. break;
  1335. case NID_countryName:
  1336. _tcsncpy(SslCertData.issuer.Country, str, 255);
  1337. SslCertData.issuer.Country[255] = 0;
  1338. break;
  1339. case NID_stateOrProvinceName:
  1340. _tcsncpy(SslCertData.issuer.StateProvince, str, 255);
  1341. SslCertData.issuer.StateProvince[255] = 0;
  1342. break;
  1343. case NID_localityName:
  1344. _tcsncpy(SslCertData.issuer.Town, str, 255);
  1345. SslCertData.issuer.Town[255] = 0;
  1346. break;
  1347. default:
  1348. if ( !OBJ_nid2sn(OBJ_obj2nid(pObject)) )
  1349. {
  1350. TCHAR tmp[20];
  1351. _stprintf(tmp, L"%d", OBJ_obj2nid(pObject));
  1352. int maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1353. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), tmp, maxlen);
  1354. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1355. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), L"=", maxlen);
  1356. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1357. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), str, maxlen);
  1358. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1359. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), L";", maxlen);
  1360. }
  1361. else
  1362. {
  1363. int maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1364. USES_CONVERSION;
  1365. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), A2CT(OBJ_nid2sn(OBJ_obj2nid(pObject))), maxlen);
  1366. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1367. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), L"=", maxlen);
  1368. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1369. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), str, maxlen);
  1370. maxlen = 1024 - _tcslen(SslCertData.issuer.Other)-1;
  1371. _tcsncpy(SslCertData.issuer.Other+_tcslen(SslCertData.issuer.Other), L";", maxlen);
  1372. }
  1373. break;
  1374. }
  1375. }
  1376. }
  1377. //Set date fields
  1378. //Valid from
  1379. ASN1_TIME *pTime=X509_getm_notBefore(pX509);
  1380. if (!pTime)
  1381. {
  1382. X509_free(pX509);
  1383. Error = L"Cannot get start time";
  1384. return FALSE;
  1385. }
  1386. if (!AsnTimeToValidTime(pTime, SslCertData.validFrom))
  1387. {
  1388. X509_free(pX509);
  1389. Error = L"Invalid start time";
  1390. return FALSE;
  1391. }
  1392. //Valid until
  1393. pTime = X509_getm_notAfter(pX509);
  1394. if (!pTime)
  1395. {
  1396. X509_free(pX509);
  1397. Error = L"Cannot get end time";
  1398. return FALSE;
  1399. }
  1400. if (!AsnTimeToValidTime(pTime, SslCertData.validUntil))
  1401. {
  1402. X509_free(pX509);
  1403. Error = L"Invalid end time";
  1404. return FALSE;
  1405. }
  1406. int subjectAltNamePos = X509_get_ext_by_NID(pX509, NID_subject_alt_name, -1);
  1407. if (subjectAltNamePos >= 0)
  1408. {
  1409. X509_EXTENSION * subjectAltNameExtension = X509_get_ext(pX509, subjectAltNamePos);
  1410. BIO * subjectAltNameBio = BIO_new(BIO_s_mem());
  1411. if (X509V3_EXT_print(subjectAltNameBio, subjectAltNameExtension, 0, 0) == 1)
  1412. {
  1413. USES_CONVERSION;
  1414. u_char *data;
  1415. int len = BIO_get_mem_data(subjectAltNameBio, &data);
  1416. char * buf = new char[len + 1];
  1417. memcpy(buf, data, len);
  1418. buf[len] = '\0';
  1419. _tcsncpy(SslCertData.subjectAltName, A2CT(buf), LENOF(SslCertData.subjectAltName));
  1420. SslCertData.subjectAltName[LENOF(SslCertData.subjectAltName) - 1] = '\0';
  1421. delete [] buf;
  1422. }
  1423. BIO_vfree(subjectAltNameBio);
  1424. }
  1425. unsigned int length = 20;
  1426. X509_digest(pX509, EVP_sha1(), SslCertData.hash, &length);
  1427. // Inspired by ne_ssl_cert_export()
  1428. // Find the length of the DER encoding.
  1429. SslCertData.certificateLen = i2d_X509(pX509, NULL);
  1430. SslCertData.certificate = new unsigned char[SslCertData.certificateLen];
  1431. unsigned char * p = SslCertData.certificate;
  1432. i2d_X509(pX509, &p);
  1433. SslCertData.priv_data = m_nSslAsyncNotifyId;
  1434. X509_free(pX509);
  1435. SslCertData.verificationResult = m_nVerificationResult;
  1436. SslCertData.verificationDepth = m_nVerificationDepth;
  1437. return TRUE;
  1438. }
  1439. std::string CAsyncSslSocketLayer::GetTlsVersionStr()
  1440. {
  1441. return m_TlsVersionStr;
  1442. }
  1443. std::string CAsyncSslSocketLayer::GetCipherName()
  1444. {
  1445. return m_CipherName;
  1446. }
  1447. void CAsyncSslSocketLayer::SetNotifyReply(int nID, int nCode, int result)
  1448. {
  1449. if (!m_bBlocking)
  1450. return;
  1451. if (nID != m_nSslAsyncNotifyId)
  1452. return;
  1453. if (nCode != SSL_VERIFY_CERT)
  1454. return;
  1455. m_bBlocking = FALSE;
  1456. if (!result)
  1457. {
  1458. m_nNetworkError = WSAECONNABORTED;
  1459. WSASetLastError(WSAECONNABORTED);
  1460. if (!m_bFailureSent)
  1461. {
  1462. m_bFailureSent = TRUE;
  1463. DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_FAILURE, SSL_FAILURE_CERTREJECTED);
  1464. }
  1465. TriggerEvent(FD_CLOSE, 0, TRUE);
  1466. return;
  1467. }
  1468. m_bSslEstablished = TRUE;
  1469. PrintSessionInfo();
  1470. DoLayerCallback(LAYERCALLBACK_LAYERSPECIFIC, SSL_INFO, SSL_INFO_ESTABLISHED);
  1471. TriggerEvents();
  1472. }
  1473. void CAsyncSslSocketLayer::PrintSessionInfo()
  1474. {
  1475. const SSL_CIPHER *ciph;
  1476. X509 *cert;
  1477. ciph = SSL_get_current_cipher(m_ssl);
  1478. char enc[4096] = {0};
  1479. cert=SSL_get_peer_certificate(m_ssl);
  1480. if (cert != NULL)
  1481. {
  1482. EVP_PKEY *pkey = X509_get_pubkey(cert);
  1483. if (pkey != NULL)
  1484. {
  1485. if (0)
  1486. ;
  1487. #ifndef NO_RSA
  1488. else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
  1489. sprintf(enc, "%d bit RSA", EVP_PKEY_bits(pkey));
  1490. #endif
  1491. #ifndef NO_DSA
  1492. else if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA)
  1493. sprintf(enc, "%d bit DSA", EVP_PKEY_bits(pkey));
  1494. #endif
  1495. EVP_PKEY_free(pkey);
  1496. }
  1497. X509_free(cert);
  1498. /* The SSL API does not allow us to look at temporary RSA/DH keys,
  1499. * otherwise we should print their lengths too */
  1500. }
  1501. const int buffer_size = 4096;
  1502. char *buffer = new char[buffer_size];
  1503. char *buffer2 = new char[buffer_size];
  1504. // see also ne_ssl_get_version and ne_ssl_get_cipher
  1505. m_TlsVersionStr = SSL_get_version(m_ssl);
  1506. sprintf(buffer, "%s: %s, %s, %s",
  1507. SSL_CIPHER_get_version(ciph),
  1508. SSL_CIPHER_get_name(ciph),
  1509. enc,
  1510. SSL_CIPHER_description(ciph, buffer2, buffer_size));
  1511. m_CipherName = buffer;
  1512. // see TWebDAVFileSystem::CollectTLSSessionInfo()
  1513. sprintf(buffer, "Using %s, cipher %s",
  1514. m_TlsVersionStr.c_str(),
  1515. m_CipherName.c_str());
  1516. USES_CONVERSION;
  1517. LogSocketMessageRaw(FZ_LOG_PROGRESS, A2T(buffer));
  1518. delete [] buffer;
  1519. delete [] buffer2;
  1520. }
  1521. void CAsyncSslSocketLayer::OnConnect(int nErrorCode)
  1522. {
  1523. if (m_bUseSSL && nErrorCode)
  1524. TriggerEvent(FD_WRITE, 0);
  1525. TriggerEvent(FD_CONNECT, nErrorCode, TRUE);
  1526. }
  1527. CAsyncSslSocketLayer * CAsyncSslSocketLayer::LookupLayer(SSL * Ssl)
  1528. {
  1529. CAsyncSslSocketLayer * Result = NULL;
  1530. m_sCriticalSection.Lock();
  1531. t_SslLayerList * Cur = m_pSslLayerList;
  1532. while (Cur != NULL)
  1533. {
  1534. if (Cur->pLayer->m_ssl == Ssl)
  1535. {
  1536. break;
  1537. }
  1538. Cur = Cur->pNext;
  1539. }
  1540. m_sCriticalSection.Unlock();
  1541. if (Cur == NULL)
  1542. {
  1543. MessageBox(0, L"Can't lookup TLS session!", L"Critical error", MB_ICONEXCLAMATION);
  1544. Result = NULL;
  1545. }
  1546. else
  1547. {
  1548. Result = Cur->pLayer;
  1549. }
  1550. return Result;
  1551. }
  1552. int CAsyncSslSocketLayer::verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
  1553. {
  1554. X509 *err_cert;
  1555. int err, depth;
  1556. SSL *ssl;
  1557. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  1558. err = X509_STORE_CTX_get_error(ctx);
  1559. depth = X509_STORE_CTX_get_error_depth(ctx);
  1560. /*
  1561. * Retrieve the pointer to the SSL of the connection currently treated
  1562. * and the application specific data stored into the SSL object.
  1563. */
  1564. ssl = (SSL *)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  1565. CAsyncSslSocketLayer * pLayer = LookupLayer(ssl);
  1566. if (pLayer == NULL)
  1567. {
  1568. return 1;
  1569. }
  1570. /*
  1571. * Catch a too long certificate chain. The depth limit set using
  1572. * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
  1573. * that whenever the "depth>verify_depth" condition is met, we
  1574. * have violated the limit and want to log this error condition.
  1575. * We must do it here, because the CHAIN_TOO_LONG error would not
  1576. * be found explicitly; only errors introduced by cutting off the
  1577. * additional certificates would be logged.
  1578. */
  1579. if (depth > 10) {//mydata->verify_depth) {
  1580. preverify_ok = 0;
  1581. err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  1582. X509_STORE_CTX_set_error(ctx, err);
  1583. }
  1584. if (!preverify_ok)
  1585. {
  1586. if (!pLayer->m_nVerificationResult)
  1587. {
  1588. pLayer->m_nVerificationDepth = depth;
  1589. pLayer->m_nVerificationResult = err;
  1590. }
  1591. }
  1592. return 1;
  1593. }
  1594. int CAsyncSslSocketLayer::ProvideClientCert(
  1595. SSL * Ssl, X509 ** Certificate, EVP_PKEY ** PrivateKey)
  1596. {
  1597. CAsyncSslSocketLayer * Layer = LookupLayer(Ssl);
  1598. USES_CONVERSION;
  1599. CString Message;
  1600. Message.LoadString(NEED_CLIENT_CERTIFICATE);
  1601. char * Buffer = new char[Message.GetLength() + 1];
  1602. strcpy(Buffer, T2A(Message));
  1603. int Level;
  1604. int Result;
  1605. if ((Layer->FCertificate == NULL) || (Layer->FPrivateKey == NULL))
  1606. {
  1607. Level = FZ_LOG_WARNING;
  1608. Result = 0;
  1609. }
  1610. else
  1611. {
  1612. Level = FZ_LOG_PROGRESS;
  1613. *Certificate = X509_dup(Layer->FCertificate);
  1614. EVP_PKEY_up_ref(Layer->FPrivateKey);
  1615. *PrivateKey = Layer->FPrivateKey;
  1616. Result = 1;
  1617. }
  1618. Layer->LogSocketMessageRaw(Level, A2T(Buffer));
  1619. delete [] Buffer;
  1620. return Result;
  1621. }
  1622. void CAsyncSslSocketLayer::SetClientCertificate(X509 * Certificate, EVP_PKEY * PrivateKey)
  1623. {
  1624. FCertificate = Certificate;
  1625. FPrivateKey = PrivateKey;
  1626. }
  1627. BOOL CAsyncSslSocketLayer::SetCertStorage(CString file)
  1628. {
  1629. m_CertStorage = file;
  1630. return TRUE;
  1631. }
  1632. void CAsyncSslSocketLayer::OnClose(int nErrorCode)
  1633. {
  1634. m_onCloseCalled = true;
  1635. if (m_bUseSSL && BIO_ctrl)
  1636. {
  1637. size_t pending = BIO_ctrl_pending(m_sslbio);
  1638. if (pending > 0)
  1639. {
  1640. TriggerEvents();
  1641. }
  1642. else TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
  1643. }
  1644. else
  1645. TriggerEvent(FD_CLOSE, nErrorCode, TRUE);
  1646. }
  1647. void CAsyncSslSocketLayer::PrintLastErrorMsg()
  1648. {
  1649. int err = ERR_get_error();
  1650. while (err)
  1651. {
  1652. char *buffer = new char[512];
  1653. const char *reason = ERR_reason_error_string(err);
  1654. ERR_error_string(err, buffer);
  1655. err = ERR_get_error();
  1656. USES_CONVERSION;
  1657. LogSocketMessageRaw(FZ_LOG_PROGRESS, A2T(buffer));
  1658. LogSocketMessageRaw(FZ_LOG_WARNING, A2T(reason));
  1659. delete [] buffer;
  1660. }
  1661. }
  1662. void CAsyncSslSocketLayer::TriggerEvents()
  1663. {
  1664. size_t pending = BIO_ctrl_pending(m_nbio);
  1665. if (pending > 0)
  1666. {
  1667. if (m_mayTriggerWrite)
  1668. {
  1669. m_mayTriggerWrite = false;
  1670. TriggerEvent(FD_WRITE, 0);
  1671. }
  1672. }
  1673. else if (!m_nNetworkSendBufferLen && m_bSslEstablished && !m_pRetrySendBuffer)
  1674. {
  1675. if (BIO_ctrl_get_write_guarantee(m_sslbio) > 0 && m_mayTriggerWriteUp)
  1676. {
  1677. m_mayTriggerWriteUp = false;
  1678. TriggerEvent(FD_WRITE, 0, TRUE);
  1679. }
  1680. }
  1681. if (m_bSslEstablished && BIO_ctrl_pending(m_sslbio) > 0)
  1682. {
  1683. if (m_mayTriggerReadUp && !m_bBlocking)
  1684. {
  1685. m_mayTriggerReadUp = false;
  1686. TriggerEvent(FD_READ, 0, TRUE);
  1687. }
  1688. }
  1689. else
  1690. {
  1691. int len = BIO_ctrl_get_write_guarantee(m_nbio);
  1692. if (len > 0 && m_mayTriggerRead)
  1693. {
  1694. m_mayTriggerRead = false;
  1695. TriggerEvent(FD_READ, 0);
  1696. }
  1697. }
  1698. if (m_onCloseCalled && m_bSslEstablished)
  1699. {
  1700. if (BIO_ctrl_pending(m_sslbio) <= 0)
  1701. {
  1702. TriggerEvent(FD_CLOSE, 0, TRUE);
  1703. }
  1704. }
  1705. }
  1706. //---------------------------------------------------------------------------