handshake_util.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Copyright (c) 2018, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "handshake_util.h"
  15. #include <assert.h>
  16. #if defined(OPENSSL_LINUX) && !defined(OPENSSL_ANDROID)
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <spawn.h>
  20. #include <sys/socket.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <unistd.h>
  25. #endif
  26. #include <functional>
  27. #include "async_bio.h"
  28. #include "packeted_bio.h"
  29. #include "test_config.h"
  30. #include "test_state.h"
  31. #include <openssl/ssl.h>
  32. using namespace bssl;
  33. bool RetryAsync(SSL *ssl, int ret) {
  34. const TestConfig *config = GetTestConfig(ssl);
  35. TestState *test_state = GetTestState(ssl);
  36. // No error or not async; don't retry.
  37. if (ret >= 0 || !config->async) {
  38. return false;
  39. }
  40. if (test_state->packeted_bio != nullptr &&
  41. PacketedBioAdvanceClock(test_state->packeted_bio)) {
  42. // The DTLS retransmit logic silently ignores write failures. So the test
  43. // may progress, allow writes through synchronously.
  44. AsyncBioEnforceWriteQuota(test_state->async_bio, false);
  45. int timeout_ret = DTLSv1_handle_timeout(ssl);
  46. AsyncBioEnforceWriteQuota(test_state->async_bio, true);
  47. if (timeout_ret < 0) {
  48. fprintf(stderr, "Error retransmitting.\n");
  49. return false;
  50. }
  51. return true;
  52. }
  53. // See if we needed to read or write more. If so, allow one byte through on
  54. // the appropriate end to maximally stress the state machine.
  55. switch (SSL_get_error(ssl, ret)) {
  56. case SSL_ERROR_WANT_READ:
  57. AsyncBioAllowRead(test_state->async_bio, 1);
  58. return true;
  59. case SSL_ERROR_WANT_WRITE:
  60. AsyncBioAllowWrite(test_state->async_bio, 1);
  61. return true;
  62. case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
  63. UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id);
  64. if (!pkey) {
  65. return false;
  66. }
  67. test_state->channel_id = std::move(pkey);
  68. return true;
  69. }
  70. case SSL_ERROR_WANT_X509_LOOKUP:
  71. test_state->cert_ready = true;
  72. return true;
  73. case SSL_ERROR_PENDING_SESSION:
  74. test_state->session = std::move(test_state->pending_session);
  75. return true;
  76. case SSL_ERROR_PENDING_CERTIFICATE:
  77. test_state->early_callback_ready = true;
  78. return true;
  79. case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
  80. test_state->private_key_retries++;
  81. return true;
  82. case SSL_ERROR_WANT_CERTIFICATE_VERIFY:
  83. test_state->custom_verify_ready = true;
  84. return true;
  85. default:
  86. return false;
  87. }
  88. }
  89. int CheckIdempotentError(const char *name, SSL *ssl,
  90. std::function<int()> func) {
  91. int ret = func();
  92. int ssl_err = SSL_get_error(ssl, ret);
  93. uint32_t err = ERR_peek_error();
  94. if (ssl_err == SSL_ERROR_SSL || ssl_err == SSL_ERROR_ZERO_RETURN) {
  95. int ret2 = func();
  96. int ssl_err2 = SSL_get_error(ssl, ret2);
  97. uint32_t err2 = ERR_peek_error();
  98. if (ret != ret2 || ssl_err != ssl_err2 || err != err2) {
  99. fprintf(stderr, "Repeating %s did not replay the error.\n", name);
  100. char buf[256];
  101. ERR_error_string_n(err, buf, sizeof(buf));
  102. fprintf(stderr, "Wanted: %d %d %s\n", ret, ssl_err, buf);
  103. ERR_error_string_n(err2, buf, sizeof(buf));
  104. fprintf(stderr, "Got: %d %d %s\n", ret2, ssl_err2, buf);
  105. // runner treats exit code 90 as always failing. Otherwise, it may
  106. // accidentally consider the result an expected protocol failure.
  107. exit(90);
  108. }
  109. }
  110. return ret;
  111. }
  112. #if defined(OPENSSL_LINUX) && !defined(OPENSSL_ANDROID)
  113. // MoveBIOs moves the |BIO|s of |src| to |dst|. It is used for handoff.
  114. static void MoveBIOs(SSL *dest, SSL *src) {
  115. BIO *rbio = SSL_get_rbio(src);
  116. BIO_up_ref(rbio);
  117. SSL_set0_rbio(dest, rbio);
  118. BIO *wbio = SSL_get_wbio(src);
  119. BIO_up_ref(wbio);
  120. SSL_set0_wbio(dest, wbio);
  121. SSL_set0_rbio(src, nullptr);
  122. SSL_set0_wbio(src, nullptr);
  123. }
  124. static bool HandoffReady(SSL *ssl, int ret) {
  125. return ret < 0 && SSL_get_error(ssl, ret) == SSL_ERROR_HANDOFF;
  126. }
  127. static ssize_t read_eintr(int fd, void *out, size_t len) {
  128. ssize_t ret;
  129. do {
  130. ret = read(fd, out, len);
  131. } while (ret < 0 && errno == EINTR);
  132. return ret;
  133. }
  134. static ssize_t write_eintr(int fd, const void *in, size_t len) {
  135. ssize_t ret;
  136. do {
  137. ret = write(fd, in, len);
  138. } while (ret < 0 && errno == EINTR);
  139. return ret;
  140. }
  141. static ssize_t waitpid_eintr(pid_t pid, int *wstatus, int options) {
  142. pid_t ret;
  143. do {
  144. ret = waitpid(pid, wstatus, options);
  145. } while (ret < 0 && errno == EINTR);
  146. return ret;
  147. }
  148. // Proxy relays data between |socket|, which is connected to the client, and the
  149. // handshaker, which is connected to the numerically specified file descriptors,
  150. // until the handshaker returns control.
  151. static bool Proxy(BIO *socket, bool async, int control, int rfd, int wfd) {
  152. for (;;) {
  153. fd_set rfds;
  154. FD_ZERO(&rfds);
  155. FD_SET(wfd, &rfds);
  156. FD_SET(control, &rfds);
  157. int fd_max = wfd > control ? wfd : control;
  158. if (select(fd_max + 1, &rfds, nullptr, nullptr, nullptr) == -1) {
  159. perror("select");
  160. return false;
  161. }
  162. char buf[64];
  163. ssize_t bytes;
  164. if (FD_ISSET(wfd, &rfds) &&
  165. (bytes = read_eintr(wfd, buf, sizeof(buf))) > 0) {
  166. char *b = buf;
  167. while (bytes) {
  168. int written = BIO_write(socket, b, bytes);
  169. if (!written) {
  170. fprintf(stderr, "BIO_write wrote nothing\n");
  171. return false;
  172. }
  173. if (written < 0) {
  174. if (async) {
  175. AsyncBioAllowWrite(socket, 1);
  176. continue;
  177. }
  178. fprintf(stderr, "BIO_write failed\n");
  179. return false;
  180. }
  181. b += written;
  182. bytes -= written;
  183. }
  184. // Flush all pending data from the handshaker to the client before
  185. // considering control messages.
  186. continue;
  187. }
  188. if (!FD_ISSET(control, &rfds)) {
  189. continue;
  190. }
  191. char msg;
  192. if (read_eintr(control, &msg, 1) != 1) {
  193. perror("read");
  194. return false;
  195. }
  196. switch (msg) {
  197. case kControlMsgHandback:
  198. return true;
  199. case kControlMsgError:
  200. return false;
  201. case kControlMsgWantRead:
  202. break;
  203. default:
  204. fprintf(stderr, "Unknown control message from handshaker: %c\n", msg);
  205. return false;
  206. }
  207. char readbuf[64];
  208. if (async) {
  209. AsyncBioAllowRead(socket, 1);
  210. }
  211. int read = BIO_read(socket, readbuf, sizeof(readbuf));
  212. if (read < 1) {
  213. fprintf(stderr, "BIO_read failed\n");
  214. return false;
  215. }
  216. ssize_t written = write_eintr(rfd, readbuf, read);
  217. if (written == -1) {
  218. perror("write");
  219. return false;
  220. }
  221. if (written != read) {
  222. fprintf(stderr, "short write (%zu of %d bytes)\n", written, read);
  223. return false;
  224. }
  225. // The handshaker blocks on the control channel, so we have to signal
  226. // it that the data have been written.
  227. msg = kControlMsgWriteCompleted;
  228. if (write_eintr(control, &msg, 1) != 1) {
  229. perror("write");
  230. return false;
  231. }
  232. }
  233. }
  234. class ScopedFD {
  235. public:
  236. explicit ScopedFD(int fd): fd_(fd) {}
  237. ~ScopedFD() { close(fd_); }
  238. private:
  239. const int fd_;
  240. };
  241. // RunHandshaker forks and execs the handshaker binary, handing off |input|,
  242. // and, after proxying some amount of handshake traffic, handing back |out|.
  243. static bool RunHandshaker(BIO *bio, const TestConfig *config, bool is_resume,
  244. const Array<uint8_t> &input,
  245. Array<uint8_t> *out) {
  246. if (config->handshaker_path.empty()) {
  247. fprintf(stderr, "no -handshaker-path specified\n");
  248. return false;
  249. }
  250. struct stat dummy;
  251. if (stat(config->handshaker_path.c_str(), &dummy) == -1) {
  252. perror(config->handshaker_path.c_str());
  253. return false;
  254. }
  255. // A datagram socket guarantees that writes are all-or-nothing.
  256. int control[2];
  257. if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, control) != 0) {
  258. perror("socketpair");
  259. return false;
  260. }
  261. int rfd[2], wfd[2];
  262. // We use pipes, rather than some other mechanism, for their buffers. During
  263. // the handshake, this process acts as a dumb proxy until receiving the
  264. // handback signal, which arrives asynchronously. The race condition means
  265. // that this process could incorrectly proxy post-handshake data from the
  266. // client to the handshaker.
  267. //
  268. // To avoid this, this process never proxies data to the handshaker that the
  269. // handshaker has not explicitly requested as a result of hitting
  270. // |SSL_ERROR_WANT_READ|. Pipes allow the data to sit in a buffer while the
  271. // two processes synchronize over the |control| channel.
  272. if (pipe(rfd) != 0 || pipe(wfd) != 0) {
  273. perror("pipe2");
  274. return false;
  275. }
  276. fflush(stdout);
  277. fflush(stderr);
  278. std::vector<char *> args;
  279. bssl::UniquePtr<char> handshaker_path(
  280. OPENSSL_strdup(config->handshaker_path.c_str()));
  281. args.push_back(handshaker_path.get());
  282. char resume[] = "-handshaker-resume";
  283. if (is_resume) {
  284. args.push_back(resume);
  285. }
  286. // config->argv omits argv[0].
  287. for (int j = 0; j < config->argc; ++j) {
  288. args.push_back(config->argv[j]);
  289. }
  290. args.push_back(nullptr);
  291. posix_spawn_file_actions_t actions;
  292. if (posix_spawn_file_actions_init(&actions) != 0 ||
  293. posix_spawn_file_actions_addclose(&actions, control[0]) ||
  294. posix_spawn_file_actions_addclose(&actions, rfd[1]) ||
  295. posix_spawn_file_actions_addclose(&actions, wfd[0])) {
  296. return false;
  297. }
  298. assert(kFdControl != rfd[0]);
  299. assert(kFdControl != wfd[1]);
  300. if (control[1] != kFdControl &&
  301. posix_spawn_file_actions_adddup2(&actions, control[1], kFdControl) != 0) {
  302. return false;
  303. }
  304. assert(kFdProxyToHandshaker != wfd[1]);
  305. if (rfd[0] != kFdProxyToHandshaker &&
  306. posix_spawn_file_actions_adddup2(&actions, rfd[0],
  307. kFdProxyToHandshaker) != 0) {
  308. return false;
  309. }
  310. if (wfd[1] != kFdHandshakerToProxy &&
  311. posix_spawn_file_actions_adddup2(&actions, wfd[1],
  312. kFdHandshakerToProxy) != 0) {
  313. return false;
  314. }
  315. // MSan doesn't know that |posix_spawn| initializes its output, so initialize
  316. // it to -1.
  317. pid_t handshaker_pid = -1;
  318. int ret = posix_spawn(&handshaker_pid, args[0], &actions, nullptr,
  319. args.data(), environ);
  320. if (posix_spawn_file_actions_destroy(&actions) != 0 ||
  321. ret != 0) {
  322. return false;
  323. }
  324. close(control[1]);
  325. close(rfd[0]);
  326. close(wfd[1]);
  327. ScopedFD rfd_closer(rfd[1]);
  328. ScopedFD wfd_closer(wfd[0]);
  329. ScopedFD control_closer(control[0]);
  330. if (write_eintr(control[0], input.data(), input.size()) == -1) {
  331. perror("write");
  332. return false;
  333. }
  334. bool ok = Proxy(bio, config->async, control[0], rfd[1], wfd[0]);
  335. int wstatus;
  336. if (waitpid_eintr(handshaker_pid, &wstatus, 0) != handshaker_pid) {
  337. perror("waitpid");
  338. return false;
  339. }
  340. if (ok && wstatus) {
  341. fprintf(stderr, "handshaker exited irregularly\n");
  342. return false;
  343. }
  344. if (!ok) {
  345. return false; // This is a "good", i.e. expected, error.
  346. }
  347. constexpr size_t kBufSize = 1024 * 1024;
  348. bssl::UniquePtr<uint8_t> buf((uint8_t *) OPENSSL_malloc(kBufSize));
  349. int len = read_eintr(control[0], buf.get(), kBufSize);
  350. if (len == -1) {
  351. perror("read");
  352. return false;
  353. }
  354. out->CopyFrom({buf.get(), (size_t)len});
  355. return true;
  356. }
  357. // PrepareHandoff accepts the |ClientHello| from |ssl| and serializes state to
  358. // be passed to the handshaker. The serialized state includes both the SSL
  359. // handoff, as well test-related state.
  360. static bool PrepareHandoff(SSL *ssl, SettingsWriter *writer,
  361. Array<uint8_t> *out_handoff) {
  362. SSL_set_handoff_mode(ssl, 1);
  363. const TestConfig *config = GetTestConfig(ssl);
  364. int ret = -1;
  365. do {
  366. ret = CheckIdempotentError(
  367. "SSL_do_handshake", ssl,
  368. [&]() -> int { return SSL_do_handshake(ssl); });
  369. } while (!HandoffReady(ssl, ret) &&
  370. config->async &&
  371. RetryAsync(ssl, ret));
  372. if (!HandoffReady(ssl, ret)) {
  373. fprintf(stderr, "Handshake failed while waiting for handoff.\n");
  374. return false;
  375. }
  376. ScopedCBB cbb;
  377. SSL_CLIENT_HELLO hello;
  378. if (!CBB_init(cbb.get(), 512) ||
  379. !SSL_serialize_handoff(ssl, cbb.get(), &hello) ||
  380. !writer->WriteHandoff({CBB_data(cbb.get()), CBB_len(cbb.get())}) ||
  381. !SerializeContextState(ssl->ctx.get(), cbb.get()) ||
  382. !GetTestState(ssl)->Serialize(cbb.get())) {
  383. fprintf(stderr, "Handoff serialisation failed.\n");
  384. return false;
  385. }
  386. return CBBFinishArray(cbb.get(), out_handoff);
  387. }
  388. // DoSplitHandshake delegates the SSL handshake to a separate process, called
  389. // the handshaker. This process proxies I/O between the handshaker and the
  390. // client, using the |BIO| from |ssl|. After a successful handshake, |ssl| is
  391. // replaced with a new |SSL| object, in a way that is intended to be invisible
  392. // to the caller.
  393. bool DoSplitHandshake(UniquePtr<SSL> *ssl, SettingsWriter *writer,
  394. bool is_resume) {
  395. assert(SSL_get_rbio(ssl->get()) == SSL_get_wbio(ssl->get()));
  396. Array<uint8_t> handshaker_input;
  397. const TestConfig *config = GetTestConfig(ssl->get());
  398. // out is the response from the handshaker, which includes a serialized
  399. // handback message, but also serialized updates to the |TestState|.
  400. Array<uint8_t> out;
  401. if (!PrepareHandoff(ssl->get(), writer, &handshaker_input) ||
  402. !RunHandshaker(SSL_get_rbio(ssl->get()), config, is_resume,
  403. handshaker_input, &out)) {
  404. fprintf(stderr, "Handoff failed.\n");
  405. return false;
  406. }
  407. UniquePtr<SSL> ssl_handback =
  408. config->NewSSL((*ssl)->ctx.get(), nullptr, false, nullptr);
  409. if (!ssl_handback) {
  410. return false;
  411. }
  412. CBS output, handback;
  413. CBS_init(&output, out.data(), out.size());
  414. if (!CBS_get_u24_length_prefixed(&output, &handback) ||
  415. !DeserializeContextState(&output, ssl_handback->ctx.get()) ||
  416. !SetTestState(ssl_handback.get(), TestState::Deserialize(
  417. &output, ssl_handback->ctx.get())) ||
  418. !GetTestState(ssl_handback.get()) ||
  419. !writer->WriteHandback(handback) ||
  420. !SSL_apply_handback(ssl_handback.get(), handback)) {
  421. fprintf(stderr, "Handback failed.\n");
  422. return false;
  423. }
  424. MoveBIOs(ssl_handback.get(), ssl->get());
  425. GetTestState(ssl_handback.get())->async_bio =
  426. GetTestState(ssl->get())->async_bio;
  427. GetTestState(ssl->get())->async_bio = nullptr;
  428. *ssl = std::move(ssl_handback);
  429. return true;
  430. }
  431. #endif // defined(OPENSSL_LINUX) && !defined(OPENSSL_ANDROID)