PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/vsftpd/ssl.c

https://gitlab.com/envieidoc/tomato
C | 841 lines | 750 code | 52 blank | 39 comment | 124 complexity | 5a222329b14e92fcb73d331132424b01 MD5 | raw file
  1. /*
  2. * Part of Very Secure FTPd
  3. * Licence: GPL v2. Note that this code interfaces with with the OpenSSL
  4. * libraries, so please read LICENSE where I give explicit permission to link
  5. * against the OpenSSL libraries.
  6. * Author: Chris Evans
  7. * ssl.c
  8. *
  9. * Routines to handle a SSL/TLS-based implementation of RFC 2228, i.e.
  10. * encryption.
  11. */
  12. #include "ssl.h"
  13. #include "session.h"
  14. #include "ftpcodes.h"
  15. #include "ftpcmdio.h"
  16. #include "defs.h"
  17. #include "str.h"
  18. #include "sysutil.h"
  19. #include "tunables.h"
  20. #include "utility.h"
  21. #include "builddefs.h"
  22. #include "logging.h"
  23. #ifdef VSF_BUILD_SSL
  24. #include <openssl/ssl.h>
  25. #include <openssl/err.h>
  26. #include <openssl/rand.h>
  27. #include <openssl/bio.h>
  28. #include <openssl/ec.h>
  29. #include <errno.h>
  30. #include <limits.h>
  31. static char* get_ssl_error();
  32. static SSL* get_ssl(struct vsf_session* p_sess, int fd);
  33. static int ssl_session_init(struct vsf_session* p_sess);
  34. static void setup_bio_callbacks();
  35. static long bio_callback(
  36. BIO* p_bio, int oper, const char* p_arg, int argi, long argl, long retval);
  37. static int ssl_verify_callback(int verify_ok, X509_STORE_CTX* p_ctx);
  38. static int ssl_cert_digest(
  39. SSL* p_ssl, struct vsf_session* p_sess, struct mystr* p_str);
  40. static void maybe_log_shutdown_state(struct vsf_session* p_sess);
  41. static void maybe_log_ssl_error_state(struct vsf_session* p_sess, int ret);
  42. static int ssl_read_common(struct vsf_session* p_sess,
  43. SSL* p_ssl,
  44. char* p_buf,
  45. unsigned int len,
  46. int (*p_ssl_func)(SSL*, void*, int));
  47. static int ssl_inited;
  48. static struct mystr debug_str;
  49. void
  50. ssl_init(struct vsf_session* p_sess)
  51. {
  52. if (!ssl_inited)
  53. {
  54. SSL_CTX* p_ctx;
  55. long options;
  56. int verify_option = 0;
  57. SSL_library_init();
  58. p_ctx = SSL_CTX_new(SSLv23_server_method());
  59. if (p_ctx == NULL)
  60. {
  61. die("SSL: could not allocate SSL context");
  62. }
  63. options = SSL_OP_ALL;
  64. if (!tunable_sslv2)
  65. {
  66. options |= SSL_OP_NO_SSLv2;
  67. }
  68. if (!tunable_sslv3)
  69. {
  70. options |= SSL_OP_NO_SSLv3;
  71. }
  72. if (!tunable_tlsv1)
  73. {
  74. options |= SSL_OP_NO_TLSv1;
  75. }
  76. SSL_CTX_set_options(p_ctx, options);
  77. if (tunable_rsa_cert_file)
  78. {
  79. const char* p_key = tunable_rsa_private_key_file;
  80. if (!p_key)
  81. {
  82. p_key = tunable_rsa_cert_file;
  83. }
  84. if (SSL_CTX_use_certificate_chain_file(p_ctx, tunable_rsa_cert_file) != 1)
  85. {
  86. die("SSL: cannot load RSA certificate");
  87. }
  88. if (SSL_CTX_use_PrivateKey_file(p_ctx, p_key, X509_FILETYPE_PEM) != 1)
  89. {
  90. die("SSL: cannot load RSA private key");
  91. }
  92. }
  93. if (tunable_dsa_cert_file)
  94. {
  95. const char* p_key = tunable_dsa_private_key_file;
  96. if (!p_key)
  97. {
  98. p_key = tunable_dsa_cert_file;
  99. }
  100. if (SSL_CTX_use_certificate_chain_file(p_ctx, tunable_dsa_cert_file) != 1)
  101. {
  102. die("SSL: cannot load DSA certificate");
  103. }
  104. if (SSL_CTX_use_PrivateKey_file(p_ctx, p_key, X509_FILETYPE_PEM) != 1)
  105. {
  106. die("SSL: cannot load DSA private key");
  107. }
  108. }
  109. if (tunable_ssl_ciphers &&
  110. SSL_CTX_set_cipher_list(p_ctx, tunable_ssl_ciphers) != 1)
  111. {
  112. die("SSL: could not set cipher list");
  113. }
  114. if (RAND_status() != 1)
  115. {
  116. die("SSL: RNG is not seeded");
  117. }
  118. {
  119. EC_KEY* key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  120. if (key == NULL)
  121. {
  122. die("SSL: failed to get curve p256");
  123. }
  124. SSL_CTX_set_tmp_ecdh(p_ctx, key);
  125. EC_KEY_free(key);
  126. }
  127. if (tunable_ssl_request_cert)
  128. {
  129. verify_option |= SSL_VERIFY_PEER;
  130. }
  131. if (tunable_require_cert)
  132. {
  133. verify_option |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  134. }
  135. if (verify_option)
  136. {
  137. SSL_CTX_set_verify(p_ctx, verify_option, ssl_verify_callback);
  138. if (tunable_ca_certs_file)
  139. {
  140. STACK_OF(X509_NAME)* p_names;
  141. if (!SSL_CTX_load_verify_locations(p_ctx, tunable_ca_certs_file, NULL))
  142. {
  143. die("SSL: could not load verify file");
  144. }
  145. p_names = SSL_load_client_CA_file(tunable_ca_certs_file);
  146. if (!p_names)
  147. {
  148. die("SSL: could not load client certs file");
  149. }
  150. SSL_CTX_set_client_CA_list(p_ctx, p_names);
  151. }
  152. }
  153. {
  154. static const char* p_ctx_id = "vsftpd";
  155. SSL_CTX_set_session_id_context(p_ctx, (void*) p_ctx_id,
  156. vsf_sysutil_strlen(p_ctx_id));
  157. }
  158. if (tunable_require_ssl_reuse)
  159. {
  160. /* Ensure cached session doesn't expire */
  161. SSL_CTX_set_timeout(p_ctx, INT_MAX);
  162. }
  163. p_sess->p_ssl_ctx = p_ctx;
  164. ssl_inited = 1;
  165. }
  166. }
  167. void
  168. ssl_control_handshake(struct vsf_session* p_sess)
  169. {
  170. if (!ssl_session_init(p_sess))
  171. {
  172. struct mystr err_str = INIT_MYSTR;
  173. str_alloc_text(&err_str, "Negotiation failed: ");
  174. /* Technically, we shouldn't leak such detailed error messages. */
  175. str_append_text(&err_str, get_ssl_error());
  176. vsf_cmdio_write_str(p_sess, FTP_TLS_FAIL, &err_str);
  177. vsf_sysutil_exit(1);
  178. }
  179. p_sess->control_use_ssl = 1;
  180. }
  181. void
  182. handle_auth(struct vsf_session* p_sess)
  183. {
  184. str_upper(&p_sess->ftp_arg_str);
  185. if (str_equal_text(&p_sess->ftp_arg_str, "TLS") ||
  186. str_equal_text(&p_sess->ftp_arg_str, "TLS-C") ||
  187. str_equal_text(&p_sess->ftp_arg_str, "SSL") ||
  188. str_equal_text(&p_sess->ftp_arg_str, "TLS-P"))
  189. {
  190. vsf_cmdio_write(p_sess, FTP_AUTHOK, "Proceed with negotiation.");
  191. ssl_control_handshake(p_sess);
  192. if (str_equal_text(&p_sess->ftp_arg_str, "SSL") ||
  193. str_equal_text(&p_sess->ftp_arg_str, "TLS-P"))
  194. {
  195. p_sess->data_use_ssl = 1;
  196. }
  197. }
  198. else
  199. {
  200. vsf_cmdio_write(p_sess, FTP_BADAUTH, "Unknown AUTH type.");
  201. }
  202. }
  203. void
  204. handle_pbsz(struct vsf_session* p_sess)
  205. {
  206. if (!p_sess->control_use_ssl)
  207. {
  208. vsf_cmdio_write(p_sess, FTP_BADPBSZ, "PBSZ needs a secure connection.");
  209. }
  210. else
  211. {
  212. vsf_cmdio_write(p_sess, FTP_PBSZOK, "PBSZ set to 0.");
  213. }
  214. }
  215. void
  216. handle_prot(struct vsf_session* p_sess)
  217. {
  218. str_upper(&p_sess->ftp_arg_str);
  219. if (!p_sess->control_use_ssl)
  220. {
  221. vsf_cmdio_write(p_sess, FTP_BADPROT, "PROT needs a secure connection.");
  222. }
  223. else if (str_equal_text(&p_sess->ftp_arg_str, "C"))
  224. {
  225. p_sess->data_use_ssl = 0;
  226. vsf_cmdio_write(p_sess, FTP_PROTOK, "PROT now Clear.");
  227. }
  228. else if (str_equal_text(&p_sess->ftp_arg_str, "P"))
  229. {
  230. p_sess->data_use_ssl = 1;
  231. vsf_cmdio_write(p_sess, FTP_PROTOK, "PROT now Private.");
  232. }
  233. else if (str_equal_text(&p_sess->ftp_arg_str, "S") ||
  234. str_equal_text(&p_sess->ftp_arg_str, "E"))
  235. {
  236. vsf_cmdio_write(p_sess, FTP_NOHANDLEPROT, "PROT not supported.");
  237. }
  238. else
  239. {
  240. vsf_cmdio_write(p_sess, FTP_NOSUCHPROT, "PROT not recognized.");
  241. }
  242. }
  243. int
  244. ssl_read(struct vsf_session* p_sess, void* p_ssl, char* p_buf, unsigned int len)
  245. {
  246. return ssl_read_common(p_sess, (SSL*) p_ssl, p_buf, len, SSL_read);
  247. }
  248. int
  249. ssl_peek(struct vsf_session* p_sess, void* p_ssl, char* p_buf, unsigned int len)
  250. {
  251. return ssl_read_common(p_sess, (SSL*) p_ssl, p_buf, len, SSL_peek);
  252. }
  253. static int
  254. ssl_read_common(struct vsf_session* p_sess,
  255. SSL* p_void_ssl,
  256. char* p_buf,
  257. unsigned int len,
  258. int (*p_ssl_func)(SSL*, void*, int))
  259. {
  260. int retval;
  261. int err;
  262. SSL* p_ssl = (SSL*) p_void_ssl;
  263. do
  264. {
  265. retval = (*p_ssl_func)(p_ssl, p_buf, len);
  266. err = SSL_get_error(p_ssl, retval);
  267. }
  268. while (retval < 0 && (err == SSL_ERROR_WANT_READ ||
  269. err == SSL_ERROR_WANT_WRITE));
  270. /* If we hit an EOF, make sure it was from the peer, not injected by the
  271. * attacker.
  272. */
  273. if (retval == 0 && SSL_get_shutdown(p_ssl) != SSL_RECEIVED_SHUTDOWN)
  274. {
  275. if (p_ssl == p_sess->p_control_ssl)
  276. {
  277. str_alloc_text(&debug_str, "Control");
  278. }
  279. else
  280. {
  281. str_alloc_text(&debug_str, "DATA");
  282. }
  283. str_append_text(&debug_str, " connection terminated without SSL shutdown.");
  284. if (p_ssl != p_sess->p_control_ssl)
  285. {
  286. str_append_text(&debug_str,
  287. " Buggy client! Integrity of upload cannot be asserted.");
  288. }
  289. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  290. if (tunable_strict_ssl_read_eof)
  291. {
  292. return -1;
  293. }
  294. }
  295. return retval;
  296. }
  297. int
  298. ssl_write(void* p_ssl, const char* p_buf, unsigned int len)
  299. {
  300. int retval;
  301. int err;
  302. do
  303. {
  304. retval = SSL_write((SSL*) p_ssl, p_buf, len);
  305. err = SSL_get_error((SSL*) p_ssl, retval);
  306. }
  307. while (retval < 0 && (err == SSL_ERROR_WANT_READ ||
  308. err == SSL_ERROR_WANT_WRITE));
  309. return retval;
  310. }
  311. int
  312. ssl_write_str(void* p_ssl, const struct mystr* p_str)
  313. {
  314. unsigned int len = str_getlen(p_str);
  315. int ret = SSL_write((SSL*) p_ssl, str_getbuf(p_str), len);
  316. if ((unsigned int) ret != len)
  317. {
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. int
  323. ssl_read_into_str(struct vsf_session* p_sess, void* p_ssl, struct mystr* p_str)
  324. {
  325. unsigned int len = str_getlen(p_str);
  326. int ret = ssl_read(p_sess, p_ssl, (char*) str_getbuf(p_str), len);
  327. if (ret >= 0)
  328. {
  329. str_trunc(p_str, (unsigned int) ret);
  330. }
  331. else
  332. {
  333. str_empty(p_str);
  334. }
  335. return ret;
  336. }
  337. static void
  338. maybe_log_shutdown_state(struct vsf_session* p_sess)
  339. {
  340. if (tunable_debug_ssl)
  341. {
  342. int ret = SSL_get_shutdown(p_sess->p_data_ssl);
  343. str_alloc_text(&debug_str, "SSL shutdown state is: ");
  344. if (ret == 0)
  345. {
  346. str_append_text(&debug_str, "NONE");
  347. }
  348. else if (ret == SSL_SENT_SHUTDOWN)
  349. {
  350. str_append_text(&debug_str, "SSL_SENT_SHUTDOWN");
  351. }
  352. else if (ret == SSL_RECEIVED_SHUTDOWN)
  353. {
  354. str_append_text(&debug_str, "SSL_RECEIVED_SHUTDOWN");
  355. }
  356. else
  357. {
  358. str_append_ulong(&debug_str, ret);
  359. }
  360. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  361. }
  362. }
  363. static void
  364. maybe_log_ssl_error_state(struct vsf_session* p_sess, int ret)
  365. {
  366. if (tunable_debug_ssl)
  367. {
  368. str_alloc_text(&debug_str, "SSL ret: ");
  369. str_append_ulong(&debug_str, ret);
  370. str_append_text(&debug_str, ", SSL error: ");
  371. str_append_text(&debug_str, get_ssl_error());
  372. str_append_text(&debug_str, ", errno: ");
  373. str_append_ulong(&debug_str, errno);
  374. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  375. }
  376. }
  377. int
  378. ssl_data_close(struct vsf_session* p_sess)
  379. {
  380. int success = 1;
  381. SSL* p_ssl = p_sess->p_data_ssl;
  382. if (p_ssl)
  383. {
  384. int ret;
  385. maybe_log_shutdown_state(p_sess);
  386. /* Disable Nagle algorithm. We want the shutdown packet to be sent
  387. * immediately, there's nothing coming after.
  388. */
  389. vsf_sysutil_set_nodelay(SSL_get_fd(p_ssl));
  390. /* This is a mess. Ideally, when we're the sender, we'd like to get to the
  391. * SSL_RECEIVED_SHUTDOWN state to get a cryptographic guarantee that the
  392. * peer received all the data and shut the connection down cleanly. It
  393. * doesn't matter hugely apart from logging, but it's a nagging detail.
  394. * Unfortunately, no FTP client I found was able to get sends into that
  395. * state, so the best we can do is issue SSL_shutdown but not check the
  396. * errors / returns. At least this enables the receiver to be sure of the
  397. * integrity of the send in terms of unwanted truncation.
  398. */
  399. ret = SSL_shutdown(p_ssl);
  400. maybe_log_shutdown_state(p_sess);
  401. if (ret == 0)
  402. {
  403. ret = SSL_shutdown(p_ssl);
  404. maybe_log_shutdown_state(p_sess);
  405. if (ret != 1)
  406. {
  407. if (tunable_strict_ssl_write_shutdown)
  408. {
  409. success = 0;
  410. }
  411. maybe_log_shutdown_state(p_sess);
  412. maybe_log_ssl_error_state(p_sess, ret);
  413. }
  414. }
  415. else if (ret < 0)
  416. {
  417. if (tunable_strict_ssl_write_shutdown)
  418. {
  419. success = 0;
  420. }
  421. maybe_log_ssl_error_state(p_sess, ret);
  422. }
  423. SSL_free(p_ssl);
  424. p_sess->p_data_ssl = NULL;
  425. }
  426. return success;
  427. }
  428. int
  429. ssl_accept(struct vsf_session* p_sess, int fd)
  430. {
  431. /* SECURITY: data SSL connections don't have any auth on them as part of the
  432. * protocol. If a client sends an unfortunately optional client cert then
  433. * we can check for a match between the control and data connections.
  434. */
  435. SSL* p_ssl;
  436. int reused;
  437. if (p_sess->p_data_ssl != NULL)
  438. {
  439. die("p_data_ssl should be NULL.");
  440. }
  441. p_ssl = get_ssl(p_sess, fd);
  442. if (p_ssl == NULL)
  443. {
  444. return 0;
  445. }
  446. p_sess->p_data_ssl = p_ssl;
  447. setup_bio_callbacks(p_ssl);
  448. reused = SSL_session_reused(p_ssl);
  449. if (tunable_require_ssl_reuse && !reused)
  450. {
  451. str_alloc_text(&debug_str, "No SSL session reuse on data channel.");
  452. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  453. ssl_data_close(p_sess);
  454. return 0;
  455. }
  456. if (str_getlen(&p_sess->control_cert_digest) > 0)
  457. {
  458. static struct mystr data_cert_digest;
  459. if (!ssl_cert_digest(p_ssl, p_sess, &data_cert_digest))
  460. {
  461. str_alloc_text(&debug_str, "Missing cert on data channel.");
  462. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  463. ssl_data_close(p_sess);
  464. return 0;
  465. }
  466. if (str_strcmp(&p_sess->control_cert_digest, &data_cert_digest))
  467. {
  468. str_alloc_text(&debug_str, "DIFFERENT cert on data channel.");
  469. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  470. ssl_data_close(p_sess);
  471. return 0;
  472. }
  473. if (tunable_debug_ssl)
  474. {
  475. str_alloc_text(&debug_str, "Matching cert on data channel.");
  476. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  477. }
  478. }
  479. return 1;
  480. }
  481. void
  482. ssl_comm_channel_init(struct vsf_session* p_sess)
  483. {
  484. const struct vsf_sysutil_socketpair_retval retval =
  485. vsf_sysutil_unix_stream_socketpair();
  486. if (p_sess->ssl_consumer_fd != -1)
  487. {
  488. bug("ssl_consumer_fd active");
  489. }
  490. if (p_sess->ssl_slave_fd != -1)
  491. {
  492. bug("ssl_slave_fd active");
  493. }
  494. p_sess->ssl_consumer_fd = retval.socket_one;
  495. p_sess->ssl_slave_fd = retval.socket_two;
  496. }
  497. void
  498. ssl_comm_channel_set_consumer_context(struct vsf_session* p_sess)
  499. {
  500. if (p_sess->ssl_slave_fd == -1)
  501. {
  502. bug("ssl_slave_fd already closed");
  503. }
  504. vsf_sysutil_close(p_sess->ssl_slave_fd);
  505. p_sess->ssl_slave_fd = -1;
  506. }
  507. void
  508. ssl_comm_channel_set_producer_context(struct vsf_session* p_sess)
  509. {
  510. if (p_sess->ssl_consumer_fd == -1)
  511. {
  512. bug("ssl_consumer_fd already closed");
  513. }
  514. vsf_sysutil_close(p_sess->ssl_consumer_fd);
  515. p_sess->ssl_consumer_fd = -1;
  516. }
  517. static SSL*
  518. get_ssl(struct vsf_session* p_sess, int fd)
  519. {
  520. SSL* p_ssl = SSL_new(p_sess->p_ssl_ctx);
  521. if (p_ssl == NULL)
  522. {
  523. if (tunable_debug_ssl)
  524. {
  525. str_alloc_text(&debug_str, "SSL_new failed");
  526. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  527. }
  528. return NULL;
  529. }
  530. if (!SSL_set_fd(p_ssl, fd))
  531. {
  532. if (tunable_debug_ssl)
  533. {
  534. str_alloc_text(&debug_str, "SSL_set_fd failed");
  535. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  536. }
  537. SSL_free(p_ssl);
  538. return NULL;
  539. }
  540. if (SSL_accept(p_ssl) != 1)
  541. {
  542. const char* p_err = get_ssl_error();
  543. if (tunable_debug_ssl)
  544. {
  545. str_alloc_text(&debug_str, "SSL_accept failed: ");
  546. str_append_text(&debug_str, p_err);
  547. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  548. }
  549. /* The RFC is quite clear that we can just close the control channel
  550. * here.
  551. */
  552. die(p_err);
  553. }
  554. if (tunable_debug_ssl)
  555. {
  556. const char* p_ssl_version = SSL_get_cipher_version(p_ssl);
  557. const SSL_CIPHER* p_ssl_cipher = SSL_get_current_cipher(p_ssl);
  558. const char* p_cipher_name = SSL_CIPHER_get_name(p_ssl_cipher);
  559. X509* p_ssl_cert = SSL_get_peer_certificate(p_ssl);
  560. int reused = SSL_session_reused(p_ssl);
  561. str_alloc_text(&debug_str, "SSL version: ");
  562. str_append_text(&debug_str, p_ssl_version);
  563. str_append_text(&debug_str, ", SSL cipher: ");
  564. str_append_text(&debug_str, p_cipher_name);
  565. if (reused)
  566. {
  567. str_append_text(&debug_str, ", reused");
  568. }
  569. else
  570. {
  571. str_append_text(&debug_str, ", not reused");
  572. }
  573. if (p_ssl_cert != NULL)
  574. {
  575. str_append_text(&debug_str, ", CERT PRESENTED");
  576. X509_free(p_ssl_cert);
  577. }
  578. else
  579. {
  580. str_append_text(&debug_str, ", no cert");
  581. }
  582. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  583. }
  584. return p_ssl;
  585. }
  586. static int
  587. ssl_session_init(struct vsf_session* p_sess)
  588. {
  589. SSL* p_ssl = get_ssl(p_sess, VSFTP_COMMAND_FD);
  590. if (p_ssl == NULL)
  591. {
  592. return 0;
  593. }
  594. p_sess->p_control_ssl = p_ssl;
  595. (void) ssl_cert_digest(p_ssl, p_sess, &p_sess->control_cert_digest);
  596. setup_bio_callbacks(p_ssl);
  597. return 1;
  598. }
  599. static int
  600. ssl_cert_digest(SSL* p_ssl, struct vsf_session* p_sess, struct mystr* p_str)
  601. {
  602. X509* p_cert = SSL_get_peer_certificate(p_ssl);
  603. unsigned int num_bytes = 0;
  604. if (p_cert == NULL)
  605. {
  606. return 0;
  607. }
  608. str_reserve(p_str, EVP_MAX_MD_SIZE);
  609. str_empty(p_str);
  610. str_rpad(p_str, EVP_MAX_MD_SIZE);
  611. if (!X509_digest(p_cert, EVP_sha256(), (unsigned char*) str_getbuf(p_str),
  612. &num_bytes))
  613. {
  614. die("X509_digest failed");
  615. }
  616. X509_free(p_cert);
  617. if (tunable_debug_ssl)
  618. {
  619. unsigned int i;
  620. str_alloc_text(&debug_str, "Cert digest:");
  621. for (i = 0; i < num_bytes; ++i)
  622. {
  623. str_append_char(&debug_str, ' ');
  624. str_append_ulong(
  625. &debug_str, (unsigned long) (unsigned char) str_get_char_at(p_str, i));
  626. }
  627. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  628. }
  629. str_trunc(p_str, num_bytes);
  630. return 1;
  631. }
  632. static char*
  633. get_ssl_error()
  634. {
  635. SSL_load_error_strings();
  636. return ERR_error_string(ERR_get_error(), NULL);
  637. }
  638. static void setup_bio_callbacks(SSL* p_ssl)
  639. {
  640. BIO* p_bio = SSL_get_rbio(p_ssl);
  641. BIO_set_callback(p_bio, bio_callback);
  642. p_bio = SSL_get_wbio(p_ssl);
  643. BIO_set_callback(p_bio, bio_callback);
  644. }
  645. static long
  646. bio_callback(
  647. BIO* p_bio, int oper, const char* p_arg, int argi, long argl, long ret)
  648. {
  649. int retval = 0;
  650. int fd = 0;
  651. (void) p_arg;
  652. (void) argi;
  653. (void) argl;
  654. if (oper == (BIO_CB_READ | BIO_CB_RETURN) ||
  655. oper == (BIO_CB_WRITE | BIO_CB_RETURN))
  656. {
  657. retval = (int) ret;
  658. fd = BIO_get_fd(p_bio, NULL);
  659. }
  660. vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, fd);
  661. return ret;
  662. }
  663. static int
  664. ssl_verify_callback(int verify_ok, X509_STORE_CTX* p_ctx)
  665. {
  666. (void) p_ctx;
  667. if (tunable_validate_cert)
  668. {
  669. return verify_ok;
  670. }
  671. return 1;
  672. }
  673. void
  674. ssl_add_entropy(struct vsf_session* p_sess)
  675. {
  676. /* Although each child does seem to have its different pool of entropy, I
  677. * don't trust the interaction of OpenSSL's opaque RAND API and fork(). So
  678. * throw a bit more in (only works on systems with /dev/urandom for now).
  679. */
  680. int ret = RAND_load_file("/dev/urandom", 16);
  681. if (ret != 16)
  682. {
  683. str_alloc_text(&debug_str, "Couldn't add extra OpenSSL entropy: ");
  684. str_append_ulong(&debug_str, (unsigned long) ret);
  685. vsf_log_line(p_sess, kVSFLogEntryDebug, &debug_str);
  686. }
  687. }
  688. #else /* VSF_BUILD_SSL */
  689. void
  690. ssl_init(struct vsf_session* p_sess)
  691. {
  692. (void) p_sess;
  693. die("SSL: ssl_enable is set but SSL support not compiled in");
  694. }
  695. void
  696. ssl_control_handshake(struct vsf_session* p_sess)
  697. {
  698. (void) p_sess;
  699. }
  700. void
  701. handle_auth(struct vsf_session* p_sess)
  702. {
  703. (void) p_sess;
  704. }
  705. void
  706. handle_pbsz(struct vsf_session* p_sess)
  707. {
  708. (void) p_sess;
  709. }
  710. void
  711. handle_prot(struct vsf_session* p_sess)
  712. {
  713. (void) p_sess;
  714. }
  715. int
  716. ssl_read(struct vsf_session* p_sess, void* p_ssl, char* p_buf, unsigned int len)
  717. {
  718. (void) p_sess;
  719. (void) p_ssl;
  720. (void) p_buf;
  721. (void) len;
  722. return -1;
  723. }
  724. int
  725. ssl_peek(struct vsf_session* p_sess, void* p_ssl, char* p_buf, unsigned int len)
  726. {
  727. (void) p_sess;
  728. (void) p_ssl;
  729. (void) p_buf;
  730. (void) len;
  731. return -1;
  732. }
  733. int
  734. ssl_write(void* p_ssl, const char* p_buf, unsigned int len)
  735. {
  736. (void) p_ssl;
  737. (void) p_buf;
  738. (void) len;
  739. return -1;
  740. }
  741. int
  742. ssl_write_str(void* p_ssl, const struct mystr* p_str)
  743. {
  744. (void) p_ssl;
  745. (void) p_str;
  746. return -1;
  747. }
  748. int
  749. ssl_accept(struct vsf_session* p_sess, int fd)
  750. {
  751. (void) p_sess;
  752. (void) fd;
  753. return -1;
  754. }
  755. int
  756. ssl_data_close(struct vsf_session* p_sess)
  757. {
  758. (void) p_sess;
  759. return 1;
  760. }
  761. void
  762. ssl_comm_channel_init(struct vsf_session* p_sess)
  763. {
  764. (void) p_sess;
  765. }
  766. void
  767. ssl_comm_channel_set_consumer_context(struct vsf_session* p_sess)
  768. {
  769. (void) p_sess;
  770. }
  771. void
  772. ssl_comm_channel_set_producer_context(struct vsf_session* p_sess)
  773. {
  774. (void) p_sess;
  775. }
  776. void
  777. ssl_add_entropy(struct vsf_session* p_sess)
  778. {
  779. (void) p_sess;
  780. }
  781. int
  782. ssl_read_into_str(struct vsf_session* p_sess, void* p_ssl, struct mystr* p_str)
  783. {
  784. (void) p_sess;
  785. (void) p_ssl;
  786. (void) p_str;
  787. return -1;
  788. }
  789. #endif /* VSF_BUILD_SSL */