25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

615 satır
17 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 <signal.h>
  19. #include <sys/socket.h>
  20. #include <unistd.h>
  21. #endif
  22. #include <sys/types.h>
  23. #include <openssl/bio.h>
  24. #include <openssl/bytestring.h>
  25. #include <openssl/ssl.h>
  26. #include "async_bio.h"
  27. #include "packeted_bio.h"
  28. #include "test_config.h"
  29. static int usage(const char *program) {
  30. fprintf(stderr, "Usage: %s [flags...]\n",
  31. program);
  32. return 1;
  33. }
  34. static int g_ex_data_index = 0;
  35. static void SetConfigPtr(SSL *ssl, const TestConfig *config) {
  36. SSL_set_ex_data(ssl, g_ex_data_index, (void *)config);
  37. }
  38. static const TestConfig *GetConfigPtr(SSL *ssl) {
  39. return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
  40. }
  41. static EVP_PKEY *LoadPrivateKey(const std::string &file) {
  42. BIO *bio = BIO_new(BIO_s_file());
  43. if (bio == NULL) {
  44. return NULL;
  45. }
  46. if (!BIO_read_filename(bio, file.c_str())) {
  47. BIO_free(bio);
  48. return NULL;
  49. }
  50. EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
  51. BIO_free(bio);
  52. return pkey;
  53. }
  54. static int early_callback_called = 0;
  55. static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
  56. early_callback_called = 1;
  57. const TestConfig *config = GetConfigPtr(ctx->ssl);
  58. if (config->expected_server_name.empty()) {
  59. return 1;
  60. }
  61. const uint8_t *extension_data;
  62. size_t extension_len;
  63. CBS extension, server_name_list, host_name;
  64. uint8_t name_type;
  65. if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
  66. &extension_data,
  67. &extension_len)) {
  68. fprintf(stderr, "Could not find server_name extension.\n");
  69. return -1;
  70. }
  71. CBS_init(&extension, extension_data, extension_len);
  72. if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
  73. CBS_len(&extension) != 0 ||
  74. !CBS_get_u8(&server_name_list, &name_type) ||
  75. name_type != TLSEXT_NAMETYPE_host_name ||
  76. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  77. CBS_len(&server_name_list) != 0) {
  78. fprintf(stderr, "Could not decode server_name extension.\n");
  79. return -1;
  80. }
  81. if (!CBS_mem_equal(&host_name,
  82. (const uint8_t*)config->expected_server_name.data(),
  83. config->expected_server_name.size())) {
  84. fprintf(stderr, "Server name mismatch.\n");
  85. }
  86. return 1;
  87. }
  88. static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
  89. return 1;
  90. }
  91. static int next_protos_advertised_callback(SSL *ssl,
  92. const uint8_t **out,
  93. unsigned int *out_len,
  94. void *arg) {
  95. const TestConfig *config = GetConfigPtr(ssl);
  96. if (config->advertise_npn.empty())
  97. return SSL_TLSEXT_ERR_NOACK;
  98. // TODO(davidben): Support passing byte strings with NULs to the
  99. // test shim.
  100. *out = (const uint8_t*)config->advertise_npn.data();
  101. *out_len = config->advertise_npn.size();
  102. return SSL_TLSEXT_ERR_OK;
  103. }
  104. static int next_proto_select_callback(SSL* ssl,
  105. uint8_t** out,
  106. uint8_t* outlen,
  107. const uint8_t* in,
  108. unsigned inlen,
  109. void* arg) {
  110. const TestConfig *config = GetConfigPtr(ssl);
  111. if (config->select_next_proto.empty())
  112. return SSL_TLSEXT_ERR_NOACK;
  113. *out = (uint8_t*)config->select_next_proto.data();
  114. *outlen = config->select_next_proto.size();
  115. return SSL_TLSEXT_ERR_OK;
  116. }
  117. static int alpn_select_callback(SSL* ssl,
  118. const uint8_t** out,
  119. uint8_t* outlen,
  120. const uint8_t* in,
  121. unsigned inlen,
  122. void* arg) {
  123. const TestConfig *config = GetConfigPtr(ssl);
  124. if (config->select_alpn.empty())
  125. return SSL_TLSEXT_ERR_NOACK;
  126. if (!config->expected_advertised_alpn.empty() &&
  127. (config->expected_advertised_alpn.size() != inlen ||
  128. memcmp(config->expected_advertised_alpn.data(),
  129. in, inlen) != 0)) {
  130. fprintf(stderr, "bad ALPN select callback inputs\n");
  131. exit(1);
  132. }
  133. *out = (const uint8_t*)config->select_alpn.data();
  134. *outlen = config->select_alpn.size();
  135. return SSL_TLSEXT_ERR_OK;
  136. }
  137. static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) {
  138. *cookie_len = 32;
  139. memset(cookie, 42, *cookie_len);
  140. return 1;
  141. }
  142. static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
  143. if (cookie_len != 32) {
  144. fprintf(stderr, "Cookie length mismatch.\n");
  145. return 0;
  146. }
  147. for (size_t i = 0; i < cookie_len; i++) {
  148. if (cookie[i] != 42) {
  149. fprintf(stderr, "Cookie mismatch.\n");
  150. return 0;
  151. }
  152. }
  153. return 1;
  154. }
  155. static SSL_CTX *setup_ctx(const TestConfig *config) {
  156. SSL_CTX *ssl_ctx = NULL;
  157. DH *dh = NULL;
  158. const SSL_METHOD *method;
  159. if (config->is_dtls) {
  160. // TODO(davidben): Get DTLS 1.2 working and test the version negotiation
  161. // codepath. This doesn't currently work because
  162. // - Session resumption is broken: https://crbug.com/403378
  163. // - DTLS hasn't been updated for EVP_AEAD.
  164. if (config->is_server) {
  165. method = DTLSv1_server_method();
  166. } else {
  167. method = DTLSv1_client_method();
  168. }
  169. } else {
  170. if (config->is_server) {
  171. method = SSLv23_server_method();
  172. } else {
  173. method = SSLv23_client_method();
  174. }
  175. }
  176. ssl_ctx = SSL_CTX_new(method);
  177. if (ssl_ctx == NULL) {
  178. goto err;
  179. }
  180. if (config->is_dtls) {
  181. // DTLS needs read-ahead to function on a datagram BIO.
  182. //
  183. // TODO(davidben): this should not be necessary. DTLS code should only
  184. // expect a datagram BIO.
  185. SSL_CTX_set_read_ahead(ssl_ctx, 1);
  186. }
  187. if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
  188. goto err;
  189. }
  190. if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
  191. goto err;
  192. }
  193. dh = DH_get_2048_256(NULL);
  194. if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
  195. goto err;
  196. }
  197. SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
  198. ssl_ctx->select_certificate_cb = select_certificate_callback;
  199. SSL_CTX_set_next_protos_advertised_cb(
  200. ssl_ctx, next_protos_advertised_callback, NULL);
  201. if (!config->select_next_proto.empty()) {
  202. SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
  203. }
  204. if (!config->select_alpn.empty()) {
  205. SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
  206. }
  207. SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
  208. SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
  209. ssl_ctx->tlsext_channel_id_enabled_new = 1;
  210. DH_free(dh);
  211. return ssl_ctx;
  212. err:
  213. if (dh != NULL) {
  214. DH_free(dh);
  215. }
  216. if (ssl_ctx != NULL) {
  217. SSL_CTX_free(ssl_ctx);
  218. }
  219. return NULL;
  220. }
  221. static int retry_async(SSL *ssl, int ret, BIO *bio) {
  222. // No error; don't retry.
  223. if (ret >= 0) {
  224. return 0;
  225. }
  226. // See if we needed to read or write more. If so, allow one byte through on
  227. // the appropriate end to maximally stress the state machine.
  228. int err = SSL_get_error(ssl, ret);
  229. if (err == SSL_ERROR_WANT_READ) {
  230. async_bio_allow_read(bio, 1);
  231. return 1;
  232. } else if (err == SSL_ERROR_WANT_WRITE) {
  233. async_bio_allow_write(bio, 1);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. static int do_exchange(SSL_SESSION **out_session,
  239. SSL_CTX *ssl_ctx,
  240. const TestConfig *config,
  241. bool is_resume,
  242. int fd,
  243. SSL_SESSION *session) {
  244. early_callback_called = 0;
  245. SSL *ssl = SSL_new(ssl_ctx);
  246. if (ssl == NULL) {
  247. BIO_print_errors_fp(stdout);
  248. return 1;
  249. }
  250. SetConfigPtr(ssl, config);
  251. if (config->fallback_scsv) {
  252. if (!SSL_enable_fallback_scsv(ssl)) {
  253. BIO_print_errors_fp(stdout);
  254. return 1;
  255. }
  256. }
  257. if (!config->key_file.empty()) {
  258. if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
  259. SSL_FILETYPE_PEM)) {
  260. BIO_print_errors_fp(stdout);
  261. return 1;
  262. }
  263. }
  264. if (!config->cert_file.empty()) {
  265. if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
  266. SSL_FILETYPE_PEM)) {
  267. BIO_print_errors_fp(stdout);
  268. return 1;
  269. }
  270. }
  271. if (config->require_any_client_certificate) {
  272. SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  273. skip_verify);
  274. }
  275. if (config->false_start) {
  276. SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
  277. }
  278. if (config->cbc_record_splitting) {
  279. SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
  280. }
  281. if (config->partial_write) {
  282. SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
  283. }
  284. if (config->no_tls12) {
  285. SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
  286. }
  287. if (config->no_tls11) {
  288. SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
  289. }
  290. if (config->no_tls1) {
  291. SSL_set_options(ssl, SSL_OP_NO_TLSv1);
  292. }
  293. if (config->no_ssl3) {
  294. SSL_set_options(ssl, SSL_OP_NO_SSLv3);
  295. }
  296. if (config->cookie_exchange) {
  297. SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
  298. }
  299. if (config->tls_d5_bug) {
  300. SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
  301. }
  302. if (!config->expected_channel_id.empty()) {
  303. SSL_enable_tls_channel_id(ssl);
  304. }
  305. if (!config->send_channel_id.empty()) {
  306. EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
  307. if (pkey == NULL) {
  308. BIO_print_errors_fp(stdout);
  309. return 1;
  310. }
  311. SSL_enable_tls_channel_id(ssl);
  312. if (!SSL_set1_tls_channel_id(ssl, pkey)) {
  313. EVP_PKEY_free(pkey);
  314. BIO_print_errors_fp(stdout);
  315. return 1;
  316. }
  317. EVP_PKEY_free(pkey);
  318. }
  319. if (!config->host_name.empty()) {
  320. SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
  321. }
  322. if (!config->advertise_alpn.empty()) {
  323. SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
  324. config->advertise_alpn.size());
  325. }
  326. BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
  327. if (bio == NULL) {
  328. BIO_print_errors_fp(stdout);
  329. return 1;
  330. }
  331. if (config->is_dtls) {
  332. BIO *packeted = packeted_bio_create();
  333. BIO_push(packeted, bio);
  334. bio = packeted;
  335. }
  336. if (config->async) {
  337. BIO *async =
  338. config->is_dtls ? async_bio_create_datagram() : async_bio_create();
  339. BIO_push(async, bio);
  340. bio = async;
  341. }
  342. SSL_set_bio(ssl, bio, bio);
  343. if (session != NULL) {
  344. if (SSL_set_session(ssl, session) != 1) {
  345. fprintf(stderr, "failed to set session\n");
  346. return 2;
  347. }
  348. }
  349. int ret;
  350. do {
  351. if (config->is_server) {
  352. ret = SSL_accept(ssl);
  353. } else {
  354. ret = SSL_connect(ssl);
  355. }
  356. } while (config->async && retry_async(ssl, ret, bio));
  357. if (ret != 1) {
  358. SSL_free(ssl);
  359. BIO_print_errors_fp(stdout);
  360. return 2;
  361. }
  362. if (is_resume && !SSL_session_reused(ssl)) {
  363. fprintf(stderr, "session was not reused\n");
  364. return 2;
  365. }
  366. if (!config->expected_server_name.empty()) {
  367. const char *server_name =
  368. SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
  369. if (server_name != config->expected_server_name) {
  370. fprintf(stderr, "servername mismatch (got %s; want %s)\n",
  371. server_name, config->expected_server_name.c_str());
  372. return 2;
  373. }
  374. if (!early_callback_called) {
  375. fprintf(stderr, "early callback not called\n");
  376. return 2;
  377. }
  378. }
  379. if (!config->expected_certificate_types.empty()) {
  380. uint8_t *certificate_types;
  381. int num_certificate_types =
  382. SSL_get0_certificate_types(ssl, &certificate_types);
  383. if (num_certificate_types !=
  384. (int)config->expected_certificate_types.size() ||
  385. memcmp(certificate_types,
  386. config->expected_certificate_types.data(),
  387. num_certificate_types) != 0) {
  388. fprintf(stderr, "certificate types mismatch\n");
  389. return 2;
  390. }
  391. }
  392. if (!config->expected_next_proto.empty()) {
  393. const uint8_t *next_proto;
  394. unsigned next_proto_len;
  395. SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
  396. if (next_proto_len != config->expected_next_proto.size() ||
  397. memcmp(next_proto, config->expected_next_proto.data(),
  398. next_proto_len) != 0) {
  399. fprintf(stderr, "negotiated next proto mismatch\n");
  400. return 2;
  401. }
  402. }
  403. if (!config->expected_alpn.empty()) {
  404. const uint8_t *alpn_proto;
  405. unsigned alpn_proto_len;
  406. SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
  407. if (alpn_proto_len != config->expected_alpn.size() ||
  408. memcmp(alpn_proto, config->expected_alpn.data(),
  409. alpn_proto_len) != 0) {
  410. fprintf(stderr, "negotiated alpn proto mismatch\n");
  411. return 2;
  412. }
  413. }
  414. if (!config->expected_channel_id.empty()) {
  415. uint8_t channel_id[64];
  416. if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
  417. fprintf(stderr, "no channel id negotiated\n");
  418. return 2;
  419. }
  420. if (config->expected_channel_id.size() != 64 ||
  421. memcmp(config->expected_channel_id.data(),
  422. channel_id, 64) != 0) {
  423. fprintf(stderr, "channel id mismatch\n");
  424. return 2;
  425. }
  426. }
  427. if (config->write_different_record_sizes) {
  428. if (config->is_dtls) {
  429. fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
  430. return 6;
  431. }
  432. // This mode writes a number of different record sizes in an attempt to
  433. // trip up the CBC record splitting code.
  434. uint8_t buf[32769];
  435. memset(buf, 0x42, sizeof(buf));
  436. static const size_t kRecordSizes[] = {
  437. 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
  438. for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
  439. i++) {
  440. int w;
  441. const size_t len = kRecordSizes[i];
  442. size_t off = 0;
  443. if (len > sizeof(buf)) {
  444. fprintf(stderr, "Bad kRecordSizes value.\n");
  445. return 5;
  446. }
  447. do {
  448. w = SSL_write(ssl, buf + off, len - off);
  449. if (w > 0) {
  450. off += (size_t) w;
  451. }
  452. } while ((config->async && retry_async(ssl, w, bio)) ||
  453. (w > 0 && off < len));
  454. if (w < 0 || off != len) {
  455. SSL_free(ssl);
  456. BIO_print_errors_fp(stdout);
  457. return 4;
  458. }
  459. }
  460. } else {
  461. if (config->shim_writes_first) {
  462. int w;
  463. do {
  464. w = SSL_write(ssl, "hello", 5);
  465. } while (config->async && retry_async(ssl, w, bio));
  466. }
  467. for (;;) {
  468. uint8_t buf[512];
  469. int n;
  470. do {
  471. n = SSL_read(ssl, buf, sizeof(buf));
  472. } while (config->async && retry_async(ssl, n, bio));
  473. if (n < 0) {
  474. SSL_free(ssl);
  475. BIO_print_errors_fp(stdout);
  476. return 3;
  477. } else if (n == 0) {
  478. break;
  479. } else {
  480. for (int i = 0; i < n; i++) {
  481. buf[i] ^= 0xff;
  482. }
  483. int w;
  484. do {
  485. w = SSL_write(ssl, buf, n);
  486. } while (config->async && retry_async(ssl, w, bio));
  487. if (w != n) {
  488. SSL_free(ssl);
  489. BIO_print_errors_fp(stdout);
  490. return 4;
  491. }
  492. }
  493. }
  494. }
  495. if (out_session) {
  496. *out_session = SSL_get1_session(ssl);
  497. }
  498. SSL_shutdown(ssl);
  499. SSL_free(ssl);
  500. return 0;
  501. }
  502. int main(int argc, char **argv) {
  503. #if !defined(OPENSSL_WINDOWS)
  504. signal(SIGPIPE, SIG_IGN);
  505. #endif
  506. if (!SSL_library_init()) {
  507. return 1;
  508. }
  509. g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
  510. TestConfig config;
  511. if (!ParseConfig(argc - 1, argv + 1, &config)) {
  512. return usage(argv[0]);
  513. }
  514. SSL_CTX *ssl_ctx = setup_ctx(&config);
  515. if (ssl_ctx == NULL) {
  516. BIO_print_errors_fp(stdout);
  517. return 1;
  518. }
  519. SSL_SESSION *session = NULL;
  520. int ret = do_exchange(&session,
  521. ssl_ctx, &config,
  522. false /* is_resume */,
  523. 3 /* fd */, NULL /* session */);
  524. if (ret != 0) {
  525. goto out;
  526. }
  527. if (config.resume) {
  528. ret = do_exchange(NULL,
  529. ssl_ctx, &config,
  530. true /* is_resume */,
  531. 4 /* fd */,
  532. config.is_server ? NULL : session);
  533. if (ret != 0) {
  534. goto out;
  535. }
  536. }
  537. ret = 0;
  538. out:
  539. SSL_SESSION_free(session);
  540. SSL_CTX_free(ssl_ctx);
  541. return ret;
  542. }