You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1266 regels
39 KiB

  1. /* Copyright (c) 2014, 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 <openssl/base.h>
  15. #if !defined(OPENSSL_WINDOWS)
  16. #include <arpa/inet.h>
  17. #include <netinet/in.h>
  18. #include <netinet/tcp.h>
  19. #include <signal.h>
  20. #include <sys/socket.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #else
  24. #include <io.h>
  25. #pragma warning(push, 3)
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #pragma warning(pop)
  29. #pragma comment(lib, "Ws2_32.lib")
  30. #endif
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <openssl/bio.h>
  34. #include <openssl/buf.h>
  35. #include <openssl/bytestring.h>
  36. #include <openssl/cipher.h>
  37. #include <openssl/err.h>
  38. #include <openssl/hmac.h>
  39. #include <openssl/rand.h>
  40. #include <openssl/ssl.h>
  41. #include <memory>
  42. #include <vector>
  43. #include "../../crypto/test/scoped_types.h"
  44. #include "async_bio.h"
  45. #include "packeted_bio.h"
  46. #include "scoped_types.h"
  47. #include "test_config.h"
  48. #if !defined(OPENSSL_WINDOWS)
  49. static int closesocket(int sock) {
  50. return close(sock);
  51. }
  52. static void PrintSocketError(const char *func) {
  53. perror(func);
  54. }
  55. #else
  56. static void PrintSocketError(const char *func) {
  57. fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
  58. }
  59. #endif
  60. static int Usage(const char *program) {
  61. fprintf(stderr, "Usage: %s [flags...]\n", program);
  62. return 1;
  63. }
  64. struct TestState {
  65. TestState() {
  66. // MSVC cannot initialize these inline.
  67. memset(&clock, 0, sizeof(clock));
  68. memset(&clock_delta, 0, sizeof(clock_delta));
  69. }
  70. // async_bio is async BIO which pauses reads and writes.
  71. BIO *async_bio = nullptr;
  72. // clock is the current time for the SSL connection.
  73. timeval clock;
  74. // clock_delta is how far the clock advanced in the most recent failed
  75. // |BIO_read|.
  76. timeval clock_delta;
  77. ScopedEVP_PKEY channel_id;
  78. bool cert_ready = false;
  79. ScopedSSL_SESSION session;
  80. ScopedSSL_SESSION pending_session;
  81. bool early_callback_called = false;
  82. bool handshake_done = false;
  83. // private_key is the underlying private key used when testing custom keys.
  84. ScopedEVP_PKEY private_key;
  85. std::vector<uint8_t> signature;
  86. // signature_retries is the number of times an asynchronous sign operation has
  87. // been retried.
  88. unsigned signature_retries = 0;
  89. bool got_new_session = false;
  90. };
  91. static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
  92. int index, long argl, void *argp) {
  93. delete ((TestState *)ptr);
  94. }
  95. static int g_config_index = 0;
  96. static int g_state_index = 0;
  97. static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
  98. return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
  99. }
  100. static const TestConfig *GetConfigPtr(const SSL *ssl) {
  101. return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
  102. }
  103. static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
  104. if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
  105. async.release();
  106. return true;
  107. }
  108. return false;
  109. }
  110. static TestState *GetTestState(const SSL *ssl) {
  111. return (TestState *)SSL_get_ex_data(ssl, g_state_index);
  112. }
  113. static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
  114. ScopedBIO bio(BIO_new(BIO_s_file()));
  115. if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
  116. return nullptr;
  117. }
  118. ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
  119. return pkey;
  120. }
  121. static int AsyncPrivateKeyType(SSL *ssl) {
  122. return EVP_PKEY_id(GetTestState(ssl)->private_key.get());
  123. }
  124. static int AsyncPrivateKeySupportsDigest(SSL *ssl, const EVP_MD *md) {
  125. return EVP_PKEY_supports_digest(GetTestState(ssl)->private_key.get(), md);
  126. }
  127. static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
  128. return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
  129. }
  130. static ssl_private_key_result_t AsyncPrivateKeySign(
  131. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  132. const EVP_MD *md, const uint8_t *in, size_t in_len) {
  133. TestState *test_state = GetTestState(ssl);
  134. if (!test_state->signature.empty()) {
  135. fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
  136. abort();
  137. }
  138. ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(),
  139. nullptr));
  140. if (!ctx) {
  141. return ssl_private_key_failure;
  142. }
  143. // Write the signature into |test_state|.
  144. size_t len = 0;
  145. if (!EVP_PKEY_sign_init(ctx.get()) ||
  146. !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) ||
  147. !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) {
  148. return ssl_private_key_failure;
  149. }
  150. test_state->signature.resize(len);
  151. if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len,
  152. in, in_len)) {
  153. return ssl_private_key_failure;
  154. }
  155. test_state->signature.resize(len);
  156. // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|.
  157. return ssl_private_key_retry;
  158. }
  159. static ssl_private_key_result_t AsyncPrivateKeySignComplete(
  160. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  161. TestState *test_state = GetTestState(ssl);
  162. if (test_state->signature.empty()) {
  163. fprintf(stderr,
  164. "AsyncPrivateKeySignComplete called without operation pending.\n");
  165. abort();
  166. }
  167. if (test_state->signature_retries < 2) {
  168. // Only return the signature on the second attempt, to test both incomplete
  169. // |sign| and |sign_complete|.
  170. return ssl_private_key_retry;
  171. }
  172. if (max_out < test_state->signature.size()) {
  173. fprintf(stderr, "Output buffer too small.\n");
  174. return ssl_private_key_failure;
  175. }
  176. memcpy(out, bssl::vector_data(&test_state->signature),
  177. test_state->signature.size());
  178. *out_len = test_state->signature.size();
  179. test_state->signature.clear();
  180. test_state->signature_retries = 0;
  181. return ssl_private_key_success;
  182. }
  183. static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
  184. AsyncPrivateKeyType,
  185. AsyncPrivateKeySupportsDigest,
  186. AsyncPrivateKeyMaxSignatureLen,
  187. AsyncPrivateKeySign,
  188. AsyncPrivateKeySignComplete,
  189. };
  190. static bool InstallCertificate(SSL *ssl) {
  191. const TestConfig *config = GetConfigPtr(ssl);
  192. TestState *test_state = GetTestState(ssl);
  193. if (!config->key_file.empty()) {
  194. if (config->use_async_private_key) {
  195. test_state->private_key = LoadPrivateKey(config->key_file.c_str());
  196. if (!test_state->private_key) {
  197. return false;
  198. }
  199. SSL_set_private_key_method(ssl, &g_async_private_key_method);
  200. } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
  201. SSL_FILETYPE_PEM)) {
  202. return false;
  203. }
  204. }
  205. if (!config->cert_file.empty() &&
  206. !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
  207. SSL_FILETYPE_PEM)) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
  213. const TestConfig *config = GetConfigPtr(ctx->ssl);
  214. GetTestState(ctx->ssl)->early_callback_called = true;
  215. if (!config->expected_server_name.empty()) {
  216. const uint8_t *extension_data;
  217. size_t extension_len;
  218. CBS extension, server_name_list, host_name;
  219. uint8_t name_type;
  220. if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
  221. &extension_data,
  222. &extension_len)) {
  223. fprintf(stderr, "Could not find server_name extension.\n");
  224. return -1;
  225. }
  226. CBS_init(&extension, extension_data, extension_len);
  227. if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
  228. CBS_len(&extension) != 0 ||
  229. !CBS_get_u8(&server_name_list, &name_type) ||
  230. name_type != TLSEXT_NAMETYPE_host_name ||
  231. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  232. CBS_len(&server_name_list) != 0) {
  233. fprintf(stderr, "Could not decode server_name extension.\n");
  234. return -1;
  235. }
  236. if (!CBS_mem_equal(&host_name,
  237. (const uint8_t*)config->expected_server_name.data(),
  238. config->expected_server_name.size())) {
  239. fprintf(stderr, "Server name mismatch.\n");
  240. }
  241. }
  242. if (config->fail_early_callback) {
  243. return -1;
  244. }
  245. // Install the certificate in the early callback.
  246. if (config->use_early_callback) {
  247. if (config->async) {
  248. // Install the certificate asynchronously.
  249. return 0;
  250. }
  251. if (!InstallCertificate(ctx->ssl)) {
  252. return -1;
  253. }
  254. }
  255. return 1;
  256. }
  257. static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
  258. return 1;
  259. }
  260. static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
  261. unsigned int *out_len, void *arg) {
  262. const TestConfig *config = GetConfigPtr(ssl);
  263. if (config->advertise_npn.empty()) {
  264. return SSL_TLSEXT_ERR_NOACK;
  265. }
  266. *out = (const uint8_t*)config->advertise_npn.data();
  267. *out_len = config->advertise_npn.size();
  268. return SSL_TLSEXT_ERR_OK;
  269. }
  270. static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
  271. const uint8_t* in, unsigned inlen, void* arg) {
  272. const TestConfig *config = GetConfigPtr(ssl);
  273. if (config->select_next_proto.empty()) {
  274. return SSL_TLSEXT_ERR_NOACK;
  275. }
  276. *out = (uint8_t*)config->select_next_proto.data();
  277. *outlen = config->select_next_proto.size();
  278. return SSL_TLSEXT_ERR_OK;
  279. }
  280. static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
  281. const uint8_t* in, unsigned inlen, void* arg) {
  282. const TestConfig *config = GetConfigPtr(ssl);
  283. if (config->select_alpn.empty()) {
  284. return SSL_TLSEXT_ERR_NOACK;
  285. }
  286. if (!config->expected_advertised_alpn.empty() &&
  287. (config->expected_advertised_alpn.size() != inlen ||
  288. memcmp(config->expected_advertised_alpn.data(),
  289. in, inlen) != 0)) {
  290. fprintf(stderr, "bad ALPN select callback inputs\n");
  291. exit(1);
  292. }
  293. *out = (const uint8_t*)config->select_alpn.data();
  294. *outlen = config->select_alpn.size();
  295. return SSL_TLSEXT_ERR_OK;
  296. }
  297. static unsigned PskClientCallback(SSL *ssl, const char *hint,
  298. char *out_identity,
  299. unsigned max_identity_len,
  300. uint8_t *out_psk, unsigned max_psk_len) {
  301. const TestConfig *config = GetConfigPtr(ssl);
  302. if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
  303. fprintf(stderr, "Server PSK hint did not match.\n");
  304. return 0;
  305. }
  306. // Account for the trailing '\0' for the identity.
  307. if (config->psk_identity.size() >= max_identity_len ||
  308. config->psk.size() > max_psk_len) {
  309. fprintf(stderr, "PSK buffers too small\n");
  310. return 0;
  311. }
  312. BUF_strlcpy(out_identity, config->psk_identity.c_str(),
  313. max_identity_len);
  314. memcpy(out_psk, config->psk.data(), config->psk.size());
  315. return config->psk.size();
  316. }
  317. static unsigned PskServerCallback(SSL *ssl, const char *identity,
  318. uint8_t *out_psk, unsigned max_psk_len) {
  319. const TestConfig *config = GetConfigPtr(ssl);
  320. if (strcmp(identity, config->psk_identity.c_str()) != 0) {
  321. fprintf(stderr, "Client PSK identity did not match.\n");
  322. return 0;
  323. }
  324. if (config->psk.size() > max_psk_len) {
  325. fprintf(stderr, "PSK buffers too small\n");
  326. return 0;
  327. }
  328. memcpy(out_psk, config->psk.data(), config->psk.size());
  329. return config->psk.size();
  330. }
  331. static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
  332. *out_clock = GetTestState(ssl)->clock;
  333. }
  334. static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
  335. *out_pkey = GetTestState(ssl)->channel_id.release();
  336. }
  337. static int CertCallback(SSL *ssl, void *arg) {
  338. if (!GetTestState(ssl)->cert_ready) {
  339. return -1;
  340. }
  341. if (!InstallCertificate(ssl)) {
  342. return 0;
  343. }
  344. return 1;
  345. }
  346. static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
  347. int *copy) {
  348. TestState *async_state = GetTestState(ssl);
  349. if (async_state->session) {
  350. *copy = 0;
  351. return async_state->session.release();
  352. } else if (async_state->pending_session) {
  353. return SSL_magic_pending_session_ptr();
  354. } else {
  355. return NULL;
  356. }
  357. }
  358. static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
  359. const TestConfig *config = GetConfigPtr(early_context->ssl);
  360. static int callback_num = 0;
  361. callback_num++;
  362. if (config->fail_ddos_callback ||
  363. (config->fail_second_ddos_callback && callback_num == 2)) {
  364. return 0;
  365. }
  366. return 1;
  367. }
  368. static void InfoCallback(const SSL *ssl, int type, int val) {
  369. if (type == SSL_CB_HANDSHAKE_DONE) {
  370. if (GetConfigPtr(ssl)->handshake_never_done) {
  371. fprintf(stderr, "handshake completed\n");
  372. // Abort before any expected error code is printed, to ensure the overall
  373. // test fails.
  374. abort();
  375. }
  376. GetTestState(ssl)->handshake_done = true;
  377. }
  378. }
  379. static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
  380. GetTestState(ssl)->got_new_session = true;
  381. // BoringSSL passes a reference to |session|.
  382. SSL_SESSION_free(session);
  383. return 1;
  384. }
  385. static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  386. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  387. int encrypt) {
  388. // This is just test code, so use the all-zeros key.
  389. static const uint8_t kZeros[16] = {0};
  390. if (encrypt) {
  391. memcpy(key_name, kZeros, sizeof(kZeros));
  392. RAND_bytes(iv, 16);
  393. } else if (memcmp(key_name, kZeros, 16) != 0) {
  394. return 0;
  395. }
  396. if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
  397. !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
  398. return -1;
  399. }
  400. if (!encrypt) {
  401. return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
  402. }
  403. return 1;
  404. }
  405. // kCustomExtensionValue is the extension value that the custom extension
  406. // callbacks will add.
  407. static const uint16_t kCustomExtensionValue = 1234;
  408. static void *const kCustomExtensionAddArg =
  409. reinterpret_cast<void *>(kCustomExtensionValue);
  410. static void *const kCustomExtensionParseArg =
  411. reinterpret_cast<void *>(kCustomExtensionValue + 1);
  412. static const char kCustomExtensionContents[] = "custom extension";
  413. static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
  414. const uint8_t **out, size_t *out_len,
  415. int *out_alert_value, void *add_arg) {
  416. if (extension_value != kCustomExtensionValue ||
  417. add_arg != kCustomExtensionAddArg) {
  418. abort();
  419. }
  420. if (GetConfigPtr(ssl)->custom_extension_skip) {
  421. return 0;
  422. }
  423. if (GetConfigPtr(ssl)->custom_extension_fail_add) {
  424. return -1;
  425. }
  426. *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
  427. *out_len = sizeof(kCustomExtensionContents) - 1;
  428. return 1;
  429. }
  430. static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
  431. const uint8_t *out, void *add_arg) {
  432. if (extension_value != kCustomExtensionValue ||
  433. add_arg != kCustomExtensionAddArg ||
  434. out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
  435. abort();
  436. }
  437. }
  438. static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
  439. const uint8_t *contents,
  440. size_t contents_len,
  441. int *out_alert_value, void *parse_arg) {
  442. if (extension_value != kCustomExtensionValue ||
  443. parse_arg != kCustomExtensionParseArg) {
  444. abort();
  445. }
  446. if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
  447. memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
  448. *out_alert_value = SSL_AD_DECODE_ERROR;
  449. return 0;
  450. }
  451. return 1;
  452. }
  453. // Connect returns a new socket connected to localhost on |port| or -1 on
  454. // error.
  455. static int Connect(uint16_t port) {
  456. int sock = socket(AF_INET, SOCK_STREAM, 0);
  457. if (sock == -1) {
  458. PrintSocketError("socket");
  459. return -1;
  460. }
  461. int nodelay = 1;
  462. if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
  463. reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
  464. PrintSocketError("setsockopt");
  465. closesocket(sock);
  466. return -1;
  467. }
  468. sockaddr_in sin;
  469. memset(&sin, 0, sizeof(sin));
  470. sin.sin_family = AF_INET;
  471. sin.sin_port = htons(port);
  472. if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
  473. PrintSocketError("inet_pton");
  474. closesocket(sock);
  475. return -1;
  476. }
  477. if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
  478. sizeof(sin)) != 0) {
  479. PrintSocketError("connect");
  480. closesocket(sock);
  481. return -1;
  482. }
  483. return sock;
  484. }
  485. class SocketCloser {
  486. public:
  487. explicit SocketCloser(int sock) : sock_(sock) {}
  488. ~SocketCloser() {
  489. // Half-close and drain the socket before releasing it. This seems to be
  490. // necessary for graceful shutdown on Windows. It will also avoid write
  491. // failures in the test runner.
  492. #if defined(OPENSSL_WINDOWS)
  493. shutdown(sock_, SD_SEND);
  494. #else
  495. shutdown(sock_, SHUT_WR);
  496. #endif
  497. while (true) {
  498. char buf[1024];
  499. if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
  500. break;
  501. }
  502. }
  503. closesocket(sock_);
  504. }
  505. private:
  506. const int sock_;
  507. };
  508. static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
  509. ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
  510. config->is_dtls ? DTLS_method() : TLS_method()));
  511. if (!ssl_ctx) {
  512. return nullptr;
  513. }
  514. if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
  515. return nullptr;
  516. }
  517. ScopedDH dh(DH_get_2048_256(NULL));
  518. if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
  519. return nullptr;
  520. }
  521. if (config->async && config->is_server) {
  522. // Disable the internal session cache. To test asynchronous session lookup,
  523. // we use an external session cache.
  524. SSL_CTX_set_session_cache_mode(
  525. ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
  526. SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
  527. } else {
  528. SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
  529. }
  530. ssl_ctx->select_certificate_cb = SelectCertificateCallback;
  531. SSL_CTX_set_next_protos_advertised_cb(
  532. ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
  533. if (!config->select_next_proto.empty()) {
  534. SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
  535. NULL);
  536. }
  537. if (!config->select_alpn.empty()) {
  538. SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
  539. }
  540. SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
  541. SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
  542. ssl_ctx->current_time_cb = CurrentTimeCallback;
  543. SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
  544. SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
  545. if (config->use_ticket_callback) {
  546. SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
  547. }
  548. if (config->enable_client_custom_extension &&
  549. !SSL_CTX_add_client_custom_ext(
  550. ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
  551. CustomExtensionFreeCallback, kCustomExtensionAddArg,
  552. CustomExtensionParseCallback, kCustomExtensionParseArg)) {
  553. return nullptr;
  554. }
  555. if (config->enable_server_custom_extension &&
  556. !SSL_CTX_add_server_custom_ext(
  557. ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
  558. CustomExtensionFreeCallback, kCustomExtensionAddArg,
  559. CustomExtensionParseCallback, kCustomExtensionParseArg)) {
  560. return nullptr;
  561. }
  562. return ssl_ctx;
  563. }
  564. // RetryAsync is called after a failed operation on |ssl| with return code
  565. // |ret|. If the operation should be retried, it simulates one asynchronous
  566. // event and returns true. Otherwise it returns false.
  567. static bool RetryAsync(SSL *ssl, int ret) {
  568. // No error; don't retry.
  569. if (ret >= 0) {
  570. return false;
  571. }
  572. TestState *test_state = GetTestState(ssl);
  573. if (test_state->clock_delta.tv_usec != 0 ||
  574. test_state->clock_delta.tv_sec != 0) {
  575. // Process the timeout and retry.
  576. test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
  577. test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
  578. test_state->clock.tv_usec %= 1000000;
  579. test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
  580. memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
  581. if (DTLSv1_handle_timeout(ssl) < 0) {
  582. fprintf(stderr, "Error retransmitting.\n");
  583. return false;
  584. }
  585. return true;
  586. }
  587. // See if we needed to read or write more. If so, allow one byte through on
  588. // the appropriate end to maximally stress the state machine.
  589. switch (SSL_get_error(ssl, ret)) {
  590. case SSL_ERROR_WANT_READ:
  591. AsyncBioAllowRead(test_state->async_bio, 1);
  592. return true;
  593. case SSL_ERROR_WANT_WRITE:
  594. AsyncBioAllowWrite(test_state->async_bio, 1);
  595. return true;
  596. case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
  597. ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
  598. if (!pkey) {
  599. return false;
  600. }
  601. test_state->channel_id = std::move(pkey);
  602. return true;
  603. }
  604. case SSL_ERROR_WANT_X509_LOOKUP:
  605. test_state->cert_ready = true;
  606. return true;
  607. case SSL_ERROR_PENDING_SESSION:
  608. test_state->session = std::move(test_state->pending_session);
  609. return true;
  610. case SSL_ERROR_PENDING_CERTIFICATE:
  611. // The handshake will resume without a second call to the early callback.
  612. return InstallCertificate(ssl);
  613. case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
  614. test_state->signature_retries++;
  615. return true;
  616. default:
  617. return false;
  618. }
  619. }
  620. // DoRead reads from |ssl|, resolving any asynchronous operations. It returns
  621. // the result value of the final |SSL_read| call.
  622. static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
  623. const TestConfig *config = GetConfigPtr(ssl);
  624. int ret;
  625. do {
  626. ret = SSL_read(ssl, out, max_out);
  627. } while (config->async && RetryAsync(ssl, ret));
  628. return ret;
  629. }
  630. // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
  631. // operations. It returns the result of the final |SSL_write| call.
  632. static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
  633. const TestConfig *config = GetConfigPtr(ssl);
  634. int ret;
  635. do {
  636. ret = SSL_write(ssl, in, in_len);
  637. if (ret > 0) {
  638. in += ret;
  639. in_len -= ret;
  640. }
  641. } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
  642. return ret;
  643. }
  644. // CheckHandshakeProperties checks, immediately after |ssl| completes its
  645. // initial handshake (or False Starts), whether all the properties are
  646. // consistent with the test configuration and invariants.
  647. static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
  648. const TestConfig *config = GetConfigPtr(ssl);
  649. if (SSL_get_current_cipher(ssl) == nullptr) {
  650. fprintf(stderr, "null cipher after handshake\n");
  651. return false;
  652. }
  653. if (is_resume &&
  654. (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
  655. fprintf(stderr, "session was%s reused\n",
  656. SSL_session_reused(ssl) ? "" : " not");
  657. return false;
  658. }
  659. bool expect_handshake_done = is_resume || !config->false_start;
  660. if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
  661. fprintf(stderr, "handshake was%s completed\n",
  662. GetTestState(ssl)->handshake_done ? "" : " not");
  663. return false;
  664. }
  665. if (expect_handshake_done && !config->is_server) {
  666. bool expect_new_session =
  667. !config->expect_no_session &&
  668. (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
  669. if (expect_new_session != GetTestState(ssl)->got_new_session) {
  670. fprintf(stderr,
  671. "new session was%s established, but we expected the opposite\n",
  672. GetTestState(ssl)->got_new_session ? "" : " not");
  673. return false;
  674. }
  675. }
  676. if (config->is_server && !GetTestState(ssl)->early_callback_called) {
  677. fprintf(stderr, "early callback not called\n");
  678. return false;
  679. }
  680. if (!config->expected_server_name.empty()) {
  681. const char *server_name =
  682. SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  683. if (server_name != config->expected_server_name) {
  684. fprintf(stderr, "servername mismatch (got %s; want %s)\n",
  685. server_name, config->expected_server_name.c_str());
  686. return false;
  687. }
  688. }
  689. if (!config->expected_certificate_types.empty()) {
  690. uint8_t *certificate_types;
  691. int num_certificate_types =
  692. SSL_get0_certificate_types(ssl, &certificate_types);
  693. if (num_certificate_types !=
  694. (int)config->expected_certificate_types.size() ||
  695. memcmp(certificate_types,
  696. config->expected_certificate_types.data(),
  697. num_certificate_types) != 0) {
  698. fprintf(stderr, "certificate types mismatch\n");
  699. return false;
  700. }
  701. }
  702. if (!config->expected_next_proto.empty()) {
  703. const uint8_t *next_proto;
  704. unsigned next_proto_len;
  705. SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
  706. if (next_proto_len != config->expected_next_proto.size() ||
  707. memcmp(next_proto, config->expected_next_proto.data(),
  708. next_proto_len) != 0) {
  709. fprintf(stderr, "negotiated next proto mismatch\n");
  710. return false;
  711. }
  712. }
  713. if (!config->expected_alpn.empty()) {
  714. const uint8_t *alpn_proto;
  715. unsigned alpn_proto_len;
  716. SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
  717. if (alpn_proto_len != config->expected_alpn.size() ||
  718. memcmp(alpn_proto, config->expected_alpn.data(),
  719. alpn_proto_len) != 0) {
  720. fprintf(stderr, "negotiated alpn proto mismatch\n");
  721. return false;
  722. }
  723. }
  724. if (!config->expected_channel_id.empty()) {
  725. uint8_t channel_id[64];
  726. if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
  727. fprintf(stderr, "no channel id negotiated\n");
  728. return false;
  729. }
  730. if (config->expected_channel_id.size() != 64 ||
  731. memcmp(config->expected_channel_id.data(),
  732. channel_id, 64) != 0) {
  733. fprintf(stderr, "channel id mismatch\n");
  734. return false;
  735. }
  736. }
  737. if (config->expect_extended_master_secret) {
  738. if (!ssl->session->extended_master_secret) {
  739. fprintf(stderr, "No EMS for session when expected");
  740. return false;
  741. }
  742. }
  743. if (!config->expected_ocsp_response.empty()) {
  744. const uint8_t *data;
  745. size_t len;
  746. SSL_get0_ocsp_response(ssl, &data, &len);
  747. if (config->expected_ocsp_response.size() != len ||
  748. memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
  749. fprintf(stderr, "OCSP response mismatch\n");
  750. return false;
  751. }
  752. }
  753. if (!config->expected_signed_cert_timestamps.empty()) {
  754. const uint8_t *data;
  755. size_t len;
  756. SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
  757. if (config->expected_signed_cert_timestamps.size() != len ||
  758. memcmp(config->expected_signed_cert_timestamps.data(),
  759. data, len) != 0) {
  760. fprintf(stderr, "SCT list mismatch\n");
  761. return false;
  762. }
  763. }
  764. if (!config->is_server) {
  765. /* Clients should expect a peer certificate chain iff this was not a PSK
  766. * cipher suite. */
  767. if (config->psk.empty()) {
  768. if (SSL_get_peer_cert_chain(ssl) == nullptr) {
  769. fprintf(stderr, "Missing peer certificate chain!\n");
  770. return false;
  771. }
  772. } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
  773. fprintf(stderr, "Unexpected peer certificate chain!\n");
  774. return false;
  775. }
  776. }
  777. return true;
  778. }
  779. // DoExchange runs a test SSL exchange against the peer. On success, it returns
  780. // true and sets |*out_session| to the negotiated SSL session. If the test is a
  781. // resumption attempt, |is_resume| is true and |session| is the session from the
  782. // previous exchange.
  783. static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
  784. const TestConfig *config, bool is_resume,
  785. SSL_SESSION *session) {
  786. ScopedSSL ssl(SSL_new(ssl_ctx));
  787. if (!ssl) {
  788. return false;
  789. }
  790. if (!SetConfigPtr(ssl.get(), config) ||
  791. !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
  792. return false;
  793. }
  794. if (config->fallback_scsv &&
  795. !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
  796. return false;
  797. }
  798. if (!config->use_early_callback) {
  799. if (config->async) {
  800. // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
  801. SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
  802. } else if (!InstallCertificate(ssl.get())) {
  803. return false;
  804. }
  805. }
  806. if (config->require_any_client_certificate) {
  807. SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  808. SkipVerify);
  809. }
  810. if (config->false_start) {
  811. SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
  812. }
  813. if (config->cbc_record_splitting) {
  814. SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
  815. }
  816. if (config->partial_write) {
  817. SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
  818. }
  819. if (config->no_tls12) {
  820. SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
  821. }
  822. if (config->no_tls11) {
  823. SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
  824. }
  825. if (config->no_tls1) {
  826. SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
  827. }
  828. if (config->no_ssl3) {
  829. SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
  830. }
  831. if (config->tls_d5_bug) {
  832. SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
  833. }
  834. if (config->no_legacy_server_connect) {
  835. SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
  836. }
  837. if (!config->expected_channel_id.empty()) {
  838. SSL_enable_tls_channel_id(ssl.get());
  839. }
  840. if (!config->send_channel_id.empty()) {
  841. SSL_enable_tls_channel_id(ssl.get());
  842. if (!config->async) {
  843. // The async case will be supplied by |ChannelIdCallback|.
  844. ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
  845. if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
  846. return false;
  847. }
  848. }
  849. }
  850. if (!config->host_name.empty() &&
  851. !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
  852. return false;
  853. }
  854. if (!config->advertise_alpn.empty() &&
  855. SSL_set_alpn_protos(ssl.get(),
  856. (const uint8_t *)config->advertise_alpn.data(),
  857. config->advertise_alpn.size()) != 0) {
  858. return false;
  859. }
  860. if (!config->psk.empty()) {
  861. SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
  862. SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
  863. }
  864. if (!config->psk_identity.empty() &&
  865. !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
  866. return false;
  867. }
  868. if (!config->srtp_profiles.empty() &&
  869. !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
  870. return false;
  871. }
  872. if (config->enable_ocsp_stapling &&
  873. !SSL_enable_ocsp_stapling(ssl.get())) {
  874. return false;
  875. }
  876. if (config->enable_signed_cert_timestamps &&
  877. !SSL_enable_signed_cert_timestamps(ssl.get())) {
  878. return false;
  879. }
  880. if (config->min_version != 0) {
  881. SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
  882. }
  883. if (config->max_version != 0) {
  884. SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
  885. }
  886. if (config->mtu != 0) {
  887. SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
  888. SSL_set_mtu(ssl.get(), config->mtu);
  889. }
  890. if (config->install_ddos_callback) {
  891. SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
  892. }
  893. if (!config->cipher.empty() &&
  894. !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
  895. return false;
  896. }
  897. if (!config->reject_peer_renegotiations) {
  898. /* Renegotiations are disabled by default. */
  899. SSL_set_reject_peer_renegotiations(ssl.get(), 0);
  900. }
  901. int sock = Connect(config->port);
  902. if (sock == -1) {
  903. return false;
  904. }
  905. SocketCloser closer(sock);
  906. ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
  907. if (!bio) {
  908. return false;
  909. }
  910. if (config->is_dtls) {
  911. ScopedBIO packeted =
  912. PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
  913. BIO_push(packeted.get(), bio.release());
  914. bio = std::move(packeted);
  915. }
  916. if (config->async) {
  917. ScopedBIO async_scoped =
  918. config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
  919. BIO_push(async_scoped.get(), bio.release());
  920. GetTestState(ssl.get())->async_bio = async_scoped.get();
  921. bio = std::move(async_scoped);
  922. }
  923. SSL_set_bio(ssl.get(), bio.get(), bio.get());
  924. bio.release(); // SSL_set_bio takes ownership.
  925. if (session != NULL) {
  926. if (!config->is_server) {
  927. if (SSL_set_session(ssl.get(), session) != 1) {
  928. return false;
  929. }
  930. } else if (config->async) {
  931. // The internal session cache is disabled, so install the session
  932. // manually.
  933. GetTestState(ssl.get())->pending_session.reset(
  934. SSL_SESSION_up_ref(session));
  935. }
  936. }
  937. if (SSL_get_current_cipher(ssl.get()) != nullptr) {
  938. fprintf(stderr, "non-null cipher before handshake\n");
  939. return false;
  940. }
  941. int ret;
  942. if (config->implicit_handshake) {
  943. if (config->is_server) {
  944. SSL_set_accept_state(ssl.get());
  945. } else {
  946. SSL_set_connect_state(ssl.get());
  947. }
  948. } else {
  949. do {
  950. if (config->is_server) {
  951. ret = SSL_accept(ssl.get());
  952. } else {
  953. ret = SSL_connect(ssl.get());
  954. }
  955. } while (config->async && RetryAsync(ssl.get(), ret));
  956. if (ret != 1 ||
  957. !CheckHandshakeProperties(ssl.get(), is_resume)) {
  958. return false;
  959. }
  960. // Reset the state to assert later that the callback isn't called in
  961. // renegotations.
  962. GetTestState(ssl.get())->got_new_session = false;
  963. }
  964. if (config->export_keying_material > 0) {
  965. std::vector<uint8_t> result(
  966. static_cast<size_t>(config->export_keying_material));
  967. if (!SSL_export_keying_material(
  968. ssl.get(), result.data(), result.size(),
  969. config->export_label.data(), config->export_label.size(),
  970. reinterpret_cast<const uint8_t*>(config->export_context.data()),
  971. config->export_context.size(), config->use_export_context)) {
  972. fprintf(stderr, "failed to export keying material\n");
  973. return false;
  974. }
  975. if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
  976. return false;
  977. }
  978. }
  979. if (config->tls_unique) {
  980. uint8_t tls_unique[16];
  981. size_t tls_unique_len;
  982. if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
  983. sizeof(tls_unique))) {
  984. fprintf(stderr, "failed to get tls-unique\n");
  985. return false;
  986. }
  987. if (tls_unique_len != 12) {
  988. fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
  989. static_cast<unsigned>(tls_unique_len));
  990. return false;
  991. }
  992. if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
  993. return false;
  994. }
  995. }
  996. if (config->write_different_record_sizes) {
  997. if (config->is_dtls) {
  998. fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
  999. return false;
  1000. }
  1001. // This mode writes a number of different record sizes in an attempt to
  1002. // trip up the CBC record splitting code.
  1003. static const size_t kBufLen = 32769;
  1004. std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
  1005. memset(buf.get(), 0x42, kBufLen);
  1006. static const size_t kRecordSizes[] = {
  1007. 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
  1008. for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
  1009. i++) {
  1010. const size_t len = kRecordSizes[i];
  1011. if (len > kBufLen) {
  1012. fprintf(stderr, "Bad kRecordSizes value.\n");
  1013. return false;
  1014. }
  1015. if (WriteAll(ssl.get(), buf.get(), len) < 0) {
  1016. return false;
  1017. }
  1018. }
  1019. } else {
  1020. if (config->shim_writes_first) {
  1021. if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
  1022. 5) < 0) {
  1023. return false;
  1024. }
  1025. }
  1026. for (;;) {
  1027. uint8_t buf[512];
  1028. int n = DoRead(ssl.get(), buf, sizeof(buf));
  1029. int err = SSL_get_error(ssl.get(), n);
  1030. if (err == SSL_ERROR_ZERO_RETURN ||
  1031. (n == 0 && err == SSL_ERROR_SYSCALL)) {
  1032. if (n != 0) {
  1033. fprintf(stderr, "Invalid SSL_get_error output\n");
  1034. return false;
  1035. }
  1036. // Accept shutdowns with or without close_notify.
  1037. // TODO(davidben): Write tests which distinguish these two cases.
  1038. break;
  1039. } else if (err != SSL_ERROR_NONE) {
  1040. if (n > 0) {
  1041. fprintf(stderr, "Invalid SSL_get_error output\n");
  1042. return false;
  1043. }
  1044. return false;
  1045. }
  1046. // Successfully read data.
  1047. if (n <= 0) {
  1048. fprintf(stderr, "Invalid SSL_get_error output\n");
  1049. return false;
  1050. }
  1051. // After a successful read, with or without False Start, the handshake
  1052. // must be complete.
  1053. if (!GetTestState(ssl.get())->handshake_done) {
  1054. fprintf(stderr, "handshake was not completed after SSL_read\n");
  1055. return false;
  1056. }
  1057. for (int i = 0; i < n; i++) {
  1058. buf[i] ^= 0xff;
  1059. }
  1060. if (WriteAll(ssl.get(), buf, n) < 0) {
  1061. return false;
  1062. }
  1063. }
  1064. }
  1065. if (!config->is_server && !config->false_start &&
  1066. !config->implicit_handshake &&
  1067. GetTestState(ssl.get())->got_new_session) {
  1068. fprintf(stderr, "new session was established after the handshake\n");
  1069. return false;
  1070. }
  1071. if (out_session) {
  1072. out_session->reset(SSL_get1_session(ssl.get()));
  1073. }
  1074. SSL_shutdown(ssl.get());
  1075. return true;
  1076. }
  1077. int main(int argc, char **argv) {
  1078. #if defined(OPENSSL_WINDOWS)
  1079. /* Initialize Winsock. */
  1080. WORD wsa_version = MAKEWORD(2, 2);
  1081. WSADATA wsa_data;
  1082. int wsa_err = WSAStartup(wsa_version, &wsa_data);
  1083. if (wsa_err != 0) {
  1084. fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
  1085. return 1;
  1086. }
  1087. if (wsa_data.wVersion != wsa_version) {
  1088. fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
  1089. return 1;
  1090. }
  1091. #else
  1092. signal(SIGPIPE, SIG_IGN);
  1093. #endif
  1094. if (!SSL_library_init()) {
  1095. return 1;
  1096. }
  1097. g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  1098. g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
  1099. if (g_config_index < 0 || g_state_index < 0) {
  1100. return 1;
  1101. }
  1102. TestConfig config;
  1103. if (!ParseConfig(argc - 1, argv + 1, &config)) {
  1104. return Usage(argv[0]);
  1105. }
  1106. ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
  1107. if (!ssl_ctx) {
  1108. ERR_print_errors_fp(stderr);
  1109. return 1;
  1110. }
  1111. ScopedSSL_SESSION session;
  1112. if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
  1113. NULL /* session */)) {
  1114. ERR_print_errors_fp(stderr);
  1115. return 1;
  1116. }
  1117. if (config.resume &&
  1118. !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
  1119. session.get())) {
  1120. ERR_print_errors_fp(stderr);
  1121. return 1;
  1122. }
  1123. return 0;
  1124. }