BSSLConnection.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /**
  2. * @file BSSLConnection.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <prerror.h>
  23. #include <ssl.h>
  24. #include <inttypes.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <base/BLog.h>
  28. #include "BSSLConnection.h"
  29. #include <generated/blog_channel_BSSLConnection.h>
  30. static void connection_init_job_handler (BSSLConnection *o);
  31. static void connection_init_up (BSSLConnection *o);
  32. static void connection_try_io (BSSLConnection *o);
  33. static void connection_recv_job_handler (BSSLConnection *o);
  34. static void connection_try_send (BSSLConnection *o);
  35. static void connection_try_recv (BSSLConnection *o);
  36. static void connection_send_if_handler_send (BSSLConnection *o, uint8_t *data, int data_len);
  37. static void connection_recv_if_handler_recv (BSSLConnection *o, uint8_t *data, int data_len);
  38. int bprconnection_initialized = 0;
  39. PRDescIdentity bprconnection_identity;
  40. static PRFileDesc * get_bottom (PRFileDesc *layer)
  41. {
  42. while (layer->lower) {
  43. layer = layer->lower;
  44. }
  45. return layer;
  46. }
  47. static PRStatus method_close (PRFileDesc *fd)
  48. {
  49. struct BSSLConnection_backend *b = (struct BSSLConnection_backend *)fd->secret;
  50. ASSERT(!b->con)
  51. // free backend
  52. free(b);
  53. // set no secret
  54. fd->secret = NULL;
  55. return PR_SUCCESS;
  56. }
  57. static PRInt32 method_read (PRFileDesc *fd, void *buf, PRInt32 amount)
  58. {
  59. struct BSSLConnection_backend *b = (struct BSSLConnection_backend *)fd->secret;
  60. ASSERT(amount > 0)
  61. // if we are receiving into buffer or buffer has no data left, refuse recv
  62. if (b->recv_busy || b->recv_pos == b->recv_len) {
  63. // start receiving if not already
  64. if (!b->recv_busy) {
  65. // set recv busy
  66. b->recv_busy = 1;
  67. // receive into buffer
  68. StreamRecvInterface_Receiver_Recv(b->recv_if, b->recv_buf, BSSLCONNECTION_BUF_SIZE);
  69. }
  70. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  71. return -1;
  72. }
  73. // limit amount to available data
  74. if (amount > b->recv_len - b->recv_pos) {
  75. amount = b->recv_len - b->recv_pos;
  76. }
  77. // copy data
  78. memcpy(buf, b->recv_buf + b->recv_pos, amount);
  79. // update buffer
  80. b->recv_pos += amount;
  81. return amount;
  82. }
  83. static PRInt32 method_write (PRFileDesc *fd, const void *buf, PRInt32 amount)
  84. {
  85. struct BSSLConnection_backend *b = (struct BSSLConnection_backend *)fd->secret;
  86. ASSERT(amount > 0)
  87. // if there is data in buffer, refuse send
  88. if (b->send_pos < b->send_len) {
  89. PR_SetError(PR_WOULD_BLOCK_ERROR, 0);
  90. return -1;
  91. }
  92. // limit amount to buffer size
  93. if (amount > BSSLCONNECTION_BUF_SIZE) {
  94. amount = BSSLCONNECTION_BUF_SIZE;
  95. }
  96. // init buffer
  97. memcpy(b->send_buf, buf, amount);
  98. b->send_pos = 0;
  99. b->send_len = amount;
  100. // start sending
  101. StreamPassInterface_Sender_Send(b->send_if, b->send_buf + b->send_pos, b->send_len - b->send_pos);
  102. return amount;
  103. }
  104. static PRStatus method_shutdown (PRFileDesc *fd, PRIntn how)
  105. {
  106. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  107. return PR_FAILURE;
  108. }
  109. static PRInt32 method_recv (PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
  110. {
  111. ASSERT(flags == 0)
  112. return method_read(fd, buf, amount);
  113. }
  114. static PRInt32 method_send (PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, PRIntervalTime timeout)
  115. {
  116. ASSERT(flags == 0)
  117. return method_write(fd, buf, amount);
  118. }
  119. static PRInt16 method_poll (PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
  120. {
  121. *out_flags = 0;
  122. return in_flags;
  123. }
  124. static PRStatus method_getpeername (PRFileDesc *fd, PRNetAddr *addr)
  125. {
  126. memset(addr, 0, sizeof(*addr));
  127. addr->raw.family = PR_AF_INET;
  128. return PR_SUCCESS;
  129. }
  130. static PRStatus method_getsocketoption (PRFileDesc *fd, PRSocketOptionData *data)
  131. {
  132. switch (data->option) {
  133. case PR_SockOpt_Nonblocking:
  134. data->value.non_blocking = PR_TRUE;
  135. return PR_SUCCESS;
  136. }
  137. PR_SetError(PR_UNKNOWN_ERROR, 0);
  138. return PR_FAILURE;
  139. }
  140. static PRStatus method_setsocketoption (PRFileDesc *fd, const PRSocketOptionData *data)
  141. {
  142. PR_SetError(PR_UNKNOWN_ERROR, 0);
  143. return PR_FAILURE;
  144. }
  145. static PRIntn _PR_InvalidIntn (void)
  146. {
  147. ASSERT(0)
  148. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  149. return -1;
  150. }
  151. static PRInt32 _PR_InvalidInt32 (void)
  152. {
  153. ASSERT(0)
  154. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  155. return -1;
  156. }
  157. static PRInt64 _PR_InvalidInt64 (void)
  158. {
  159. ASSERT(0)
  160. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  161. return -1;
  162. }
  163. static PROffset32 _PR_InvalidOffset32 (void)
  164. {
  165. ASSERT(0)
  166. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  167. return -1;
  168. }
  169. static PROffset64 _PR_InvalidOffset64 (void)
  170. {
  171. ASSERT(0)
  172. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  173. return -1;
  174. }
  175. static PRStatus _PR_InvalidStatus (void)
  176. {
  177. ASSERT(0)
  178. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  179. return PR_FAILURE;
  180. }
  181. static PRFileDesc *_PR_InvalidDesc (void)
  182. {
  183. ASSERT(0)
  184. PR_SetError(PR_INVALID_METHOD_ERROR, 0);
  185. return NULL;
  186. }
  187. static PRIOMethods methods = {
  188. (PRDescType)0,
  189. method_close,
  190. method_read,
  191. method_write,
  192. (PRAvailableFN)_PR_InvalidInt32,
  193. (PRAvailable64FN)_PR_InvalidInt64,
  194. (PRFsyncFN)_PR_InvalidStatus,
  195. (PRSeekFN)_PR_InvalidOffset32,
  196. (PRSeek64FN)_PR_InvalidOffset64,
  197. (PRFileInfoFN)_PR_InvalidStatus,
  198. (PRFileInfo64FN)_PR_InvalidStatus,
  199. (PRWritevFN)_PR_InvalidInt32,
  200. (PRConnectFN)_PR_InvalidStatus,
  201. (PRAcceptFN)_PR_InvalidDesc,
  202. (PRBindFN)_PR_InvalidStatus,
  203. (PRListenFN)_PR_InvalidStatus,
  204. method_shutdown,
  205. method_recv,
  206. method_send,
  207. (PRRecvfromFN)_PR_InvalidInt32,
  208. (PRSendtoFN)_PR_InvalidInt32,
  209. method_poll,
  210. (PRAcceptreadFN)_PR_InvalidInt32,
  211. (PRTransmitfileFN)_PR_InvalidInt32,
  212. (PRGetsocknameFN)_PR_InvalidStatus,
  213. method_getpeername,
  214. (PRReservedFN)_PR_InvalidIntn,
  215. (PRReservedFN)_PR_InvalidIntn,
  216. method_getsocketoption,
  217. method_setsocketoption,
  218. (PRSendfileFN)_PR_InvalidInt32,
  219. (PRConnectcontinueFN)_PR_InvalidStatus,
  220. (PRReservedFN)_PR_InvalidIntn,
  221. (PRReservedFN)_PR_InvalidIntn,
  222. (PRReservedFN)_PR_InvalidIntn,
  223. (PRReservedFN)_PR_InvalidIntn
  224. };
  225. static void backend_send_if_handler_done (struct BSSLConnection_backend *b, int data_len)
  226. {
  227. ASSERT(b->send_len > 0)
  228. ASSERT(b->send_pos < b->send_len)
  229. ASSERT(data_len > 0)
  230. ASSERT(data_len <= b->send_len - b->send_pos)
  231. // update buffer
  232. b->send_pos += data_len;
  233. // send more if needed
  234. if (b->send_pos < b->send_len) {
  235. StreamPassInterface_Sender_Send(b->send_if, b->send_buf + b->send_pos, b->send_len - b->send_pos);
  236. return;
  237. }
  238. // notify connection
  239. if (b->con && !b->con->have_error) {
  240. connection_try_io(b->con);
  241. return;
  242. }
  243. }
  244. static void backend_recv_if_handler_done (struct BSSLConnection_backend *b, int data_len)
  245. {
  246. ASSERT(b->recv_busy)
  247. ASSERT(data_len > 0)
  248. ASSERT(data_len <= BSSLCONNECTION_BUF_SIZE)
  249. // init buffer
  250. b->recv_busy = 0;
  251. b->recv_pos = 0;
  252. b->recv_len = data_len;
  253. // notify connection
  254. if (b->con && !b->con->have_error) {
  255. connection_try_io(b->con);
  256. return;
  257. }
  258. }
  259. static void connection_report_error (BSSLConnection *o)
  260. {
  261. ASSERT(!o->have_error)
  262. // set error
  263. o->have_error = 1;
  264. // report error
  265. DEBUGERROR(&o->d_err, o->handler(o->user, BSSLCONNECTION_EVENT_ERROR));
  266. }
  267. static void connection_init_job_handler (BSSLConnection *o)
  268. {
  269. DebugObject_Access(&o->d_obj);
  270. ASSERT(!o->have_error)
  271. ASSERT(!o->up)
  272. connection_try_io(o);
  273. return;
  274. }
  275. static void connection_init_up (BSSLConnection *o)
  276. {
  277. // init send interface
  278. StreamPassInterface_Init(&o->send_if, (StreamPassInterface_handler_send)connection_send_if_handler_send, o, o->pg);
  279. // init recv interface
  280. StreamRecvInterface_Init(&o->recv_if, (StreamRecvInterface_handler_recv)connection_recv_if_handler_recv, o, o->pg);
  281. // init recv job
  282. BPending_Init(&o->recv_job, o->pg, (BPending_handler)connection_recv_job_handler, o);
  283. // set no send data
  284. o->send_len = -1;
  285. // set no recv data
  286. o->recv_avail = -1;
  287. // set up
  288. o->up = 1;
  289. }
  290. static void connection_try_io (BSSLConnection *o)
  291. {
  292. DebugObject_Access(&o->d_obj);
  293. ASSERT(!o->have_error)
  294. if (!o->up) {
  295. // unset init job (in case backend called us before it executed)
  296. BPending_Unset(&o->init_job);
  297. // try handshake
  298. SECStatus res = SSL_ForceHandshake(o->prfd);
  299. if (res == SECFailure) {
  300. PRErrorCode error = PR_GetError();
  301. if (error == PR_WOULD_BLOCK_ERROR) {
  302. return;
  303. }
  304. BLog(BLOG_ERROR, "SSL_ForceHandshake failed (%"PRIi32")", error);
  305. connection_report_error(o);
  306. return;
  307. }
  308. // init up
  309. connection_init_up(o);
  310. // report up
  311. o->handler(o->user, BSSLCONNECTION_EVENT_UP);
  312. return;
  313. }
  314. if (o->send_len > 0) {
  315. if (o->recv_avail > 0) {
  316. BPending_Set(&o->recv_job);
  317. }
  318. connection_try_send(o);
  319. return;
  320. }
  321. if (o->recv_avail > 0) {
  322. connection_try_recv(o);
  323. return;
  324. }
  325. }
  326. static void connection_recv_job_handler (BSSLConnection *o)
  327. {
  328. DebugObject_Access(&o->d_obj);
  329. ASSERT(!o->have_error)
  330. ASSERT(o->up)
  331. ASSERT(o->recv_avail > 0)
  332. connection_try_recv(o);
  333. return;
  334. }
  335. static void connection_try_send (BSSLConnection *o)
  336. {
  337. ASSERT(!o->have_error)
  338. ASSERT(o->up)
  339. ASSERT(o->send_len > 0)
  340. // send
  341. PRInt32 res = PR_Write(o->prfd, o->send_data, o->send_len);
  342. if (res < 0) {
  343. PRErrorCode error = PR_GetError();
  344. if (error == PR_WOULD_BLOCK_ERROR) {
  345. return;
  346. }
  347. BLog(BLOG_ERROR, "PR_Write failed (%"PRIi32")", error);
  348. connection_report_error(o);
  349. return;
  350. }
  351. ASSERT(res > 0)
  352. ASSERT(res <= o->send_len)
  353. // set no send data
  354. o->send_len = -1;
  355. // done
  356. StreamPassInterface_Done(&o->send_if, res);
  357. }
  358. static void connection_try_recv (BSSLConnection *o)
  359. {
  360. ASSERT(!o->have_error)
  361. ASSERT(o->up)
  362. ASSERT(o->recv_avail > 0)
  363. // unset recv job
  364. BPending_Unset(&o->recv_job);
  365. // recv
  366. PRInt32 res = PR_Read(o->prfd, o->recv_data, o->recv_avail);
  367. if (res < 0) {
  368. PRErrorCode error = PR_GetError();
  369. if (error == PR_WOULD_BLOCK_ERROR) {
  370. return;
  371. }
  372. BLog(BLOG_ERROR, "PR_Read failed (%"PRIi32")", error);
  373. connection_report_error(o);
  374. return;
  375. }
  376. if (res == 0) {
  377. BLog(BLOG_ERROR, "PR_Read returned 0");
  378. connection_report_error(o);
  379. return;
  380. }
  381. ASSERT(res > 0)
  382. ASSERT(res <= o->recv_avail)
  383. // set no recv data
  384. o->recv_avail = -1;
  385. // done
  386. StreamRecvInterface_Done(&o->recv_if, res);
  387. }
  388. static void connection_send_if_handler_send (BSSLConnection *o, uint8_t *data, int data_len)
  389. {
  390. DebugObject_Access(&o->d_obj);
  391. ASSERT(!o->have_error)
  392. ASSERT(o->up)
  393. ASSERT(o->send_len == -1)
  394. ASSERT(data_len > 0)
  395. // limit amount for PR_Write
  396. if (data_len > INT32_MAX) {
  397. data_len = INT32_MAX;
  398. }
  399. // set send data
  400. o->send_data = data;
  401. o->send_len = data_len;
  402. // start sending
  403. connection_try_send(o);
  404. return;
  405. }
  406. static void connection_recv_if_handler_recv (BSSLConnection *o, uint8_t *data, int data_len)
  407. {
  408. DebugObject_Access(&o->d_obj);
  409. ASSERT(!o->have_error)
  410. ASSERT(o->up)
  411. ASSERT(o->recv_avail == -1)
  412. ASSERT(data_len > 0)
  413. // limit amount for PR_Read
  414. if (data_len > INT32_MAX) {
  415. data_len = INT32_MAX;
  416. }
  417. // set recv data
  418. o->recv_data = data;
  419. o->recv_avail = data_len;
  420. // start receiving
  421. connection_try_recv(o);
  422. return;
  423. }
  424. int BSSLConnection_GlobalInit (void)
  425. {
  426. ASSERT(!bprconnection_initialized)
  427. if ((bprconnection_identity = PR_GetUniqueIdentity("BSSLConnection")) == PR_INVALID_IO_LAYER) {
  428. BLog(BLOG_ERROR, "PR_GetUniqueIdentity failed");
  429. return 0;
  430. }
  431. bprconnection_initialized = 1;
  432. return 1;
  433. }
  434. int BSSLConnection_MakeBackend (PRFileDesc *prfd, StreamPassInterface *send_if, StreamRecvInterface *recv_if)
  435. {
  436. ASSERT(bprconnection_initialized)
  437. // allocate backend
  438. struct BSSLConnection_backend *b = malloc(sizeof(*b));
  439. if (!b) {
  440. BLog(BLOG_ERROR, "malloc failed");
  441. return 0;
  442. }
  443. // init arguments
  444. b->send_if = send_if;
  445. b->recv_if = recv_if;
  446. // init interfaces
  447. StreamPassInterface_Sender_Init(b->send_if, (StreamPassInterface_handler_done)backend_send_if_handler_done, b);
  448. StreamRecvInterface_Receiver_Init(b->recv_if, (StreamRecvInterface_handler_done)backend_recv_if_handler_done, b);
  449. // set no connection
  450. b->con = NULL;
  451. // init send buffer
  452. b->send_len = 0;
  453. b->send_pos = 0;
  454. // init recv buffer
  455. b->recv_busy = 0;
  456. b->recv_pos = 0;
  457. b->recv_len = 0;
  458. // init prfd
  459. memset(prfd, 0, sizeof(*prfd));
  460. prfd->methods = &methods;
  461. prfd->secret = (PRFilePrivate *)b;
  462. prfd->identity = bprconnection_identity;
  463. return 1;
  464. }
  465. void BSSLConnection_Init (BSSLConnection *o, PRFileDesc *prfd, int force_handshake, BPendingGroup *pg, void *user,
  466. BSSLConnection_handler handler)
  467. {
  468. ASSERT(force_handshake == 0 || force_handshake == 1)
  469. ASSERT(handler)
  470. ASSERT(bprconnection_initialized)
  471. ASSERT(get_bottom(prfd)->identity == bprconnection_identity)
  472. ASSERT(!((struct BSSLConnection_backend *)(get_bottom(prfd)->secret))->con)
  473. // init arguments
  474. o->prfd = prfd;
  475. o->pg = pg;
  476. o->user = user;
  477. o->handler = handler;
  478. // set backend
  479. o->backend = (struct BSSLConnection_backend *)(get_bottom(prfd)->secret);
  480. // set have no error
  481. o->have_error = 0;
  482. // init init job
  483. BPending_Init(&o->init_job, o->pg, (BPending_handler)connection_init_job_handler, o);
  484. if (force_handshake) {
  485. // set not up
  486. o->up = 0;
  487. // set init job
  488. BPending_Set(&o->init_job);
  489. } else {
  490. // init up
  491. connection_init_up(o);
  492. }
  493. // set backend connection
  494. o->backend->con = o;
  495. DebugError_Init(&o->d_err, o->pg);
  496. DebugObject_Init(&o->d_obj);
  497. }
  498. void BSSLConnection_Free (BSSLConnection *o)
  499. {
  500. DebugObject_Free(&o->d_obj);
  501. DebugError_Free(&o->d_err);
  502. if (o->up) {
  503. // free recv job
  504. BPending_Free(&o->recv_job);
  505. // free recv interface
  506. StreamRecvInterface_Free(&o->recv_if);
  507. // free send interface
  508. StreamPassInterface_Free(&o->send_if);
  509. }
  510. // free init job
  511. BPending_Free(&o->init_job);
  512. // unset backend connection
  513. o->backend->con = NULL;
  514. }
  515. StreamPassInterface * BSSLConnection_GetSendIf (BSSLConnection *o)
  516. {
  517. DebugObject_Access(&o->d_obj);
  518. ASSERT(o->up)
  519. return &o->send_if;
  520. }
  521. StreamRecvInterface * BSSLConnection_GetRecvIf (BSSLConnection *o)
  522. {
  523. DebugObject_Access(&o->d_obj);
  524. ASSERT(o->up)
  525. return &o->recv_if;
  526. }