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.
 
 
 
 
 
 

2825 lines
98 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 <stdio.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <algorithm>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <openssl/base64.h>
  22. #include <openssl/bio.h>
  23. #include <openssl/cipher.h>
  24. #include <openssl/crypto.h>
  25. #include <openssl/err.h>
  26. #include <openssl/hmac.h>
  27. #include <openssl/pem.h>
  28. #include <openssl/sha.h>
  29. #include <openssl/ssl.h>
  30. #include <openssl/rand.h>
  31. #include <openssl/x509.h>
  32. #include "internal.h"
  33. #include "../crypto/internal.h"
  34. #include "../crypto/test/test_util.h"
  35. #if defined(OPENSSL_WINDOWS)
  36. /* Windows defines struct timeval in winsock2.h. */
  37. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  38. #include <winsock2.h>
  39. OPENSSL_MSVC_PRAGMA(warning(pop))
  40. #else
  41. #include <sys/time.h>
  42. #endif
  43. struct ExpectedCipher {
  44. unsigned long id;
  45. int in_group_flag;
  46. };
  47. struct CipherTest {
  48. // The rule string to apply.
  49. const char *rule;
  50. // The list of expected ciphers, in order.
  51. std::vector<ExpectedCipher> expected;
  52. };
  53. struct CurveTest {
  54. // The rule string to apply.
  55. const char *rule;
  56. // The list of expected curves, in order.
  57. std::vector<uint16_t> expected;
  58. };
  59. static const CipherTest kCipherTests[] = {
  60. // Selecting individual ciphers should work.
  61. {
  62. "ECDHE-ECDSA-CHACHA20-POLY1305:"
  63. "ECDHE-RSA-CHACHA20-POLY1305:"
  64. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  65. "ECDHE-RSA-AES128-GCM-SHA256",
  66. {
  67. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  68. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0},
  69. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  70. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  71. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  72. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  73. },
  74. },
  75. // + reorders selected ciphers to the end, keeping their relative order.
  76. {
  77. "ECDHE-ECDSA-CHACHA20-POLY1305:"
  78. "ECDHE-RSA-CHACHA20-POLY1305:"
  79. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  80. "ECDHE-RSA-AES128-GCM-SHA256:"
  81. "+aRSA",
  82. {
  83. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  84. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0},
  85. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  86. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  87. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  88. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  89. },
  90. },
  91. // ! banishes ciphers from future selections.
  92. {
  93. "!aRSA:"
  94. "ECDHE-ECDSA-CHACHA20-POLY1305:"
  95. "ECDHE-RSA-CHACHA20-POLY1305:"
  96. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  97. "ECDHE-RSA-AES128-GCM-SHA256",
  98. {
  99. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  100. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0},
  101. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  102. },
  103. },
  104. // Multiple masks can be ANDed in a single rule.
  105. {
  106. "kRSA+AESGCM+AES128",
  107. {
  108. {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0},
  109. },
  110. },
  111. // - removes selected ciphers, but preserves their order for future
  112. // selections. Select AES_128_GCM, but order the key exchanges RSA, DHE_RSA,
  113. // ECDHE_RSA.
  114. {
  115. "ALL:-kECDHE:-kDHE:-kRSA:-ALL:"
  116. "AESGCM+AES128+aRSA",
  117. {
  118. {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0},
  119. {TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  120. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  121. },
  122. },
  123. // Unknown selectors are no-ops.
  124. {
  125. "ECDHE-ECDSA-CHACHA20-POLY1305:"
  126. "ECDHE-RSA-CHACHA20-POLY1305:"
  127. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  128. "ECDHE-RSA-AES128-GCM-SHA256:"
  129. "BOGUS1:-BOGUS2:+BOGUS3:!BOGUS4",
  130. {
  131. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  132. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0},
  133. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  134. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  135. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  136. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  137. },
  138. },
  139. // Square brackets specify equi-preference groups.
  140. {
  141. "[ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-ECDSA-AES128-GCM-SHA256]:"
  142. "[ECDHE-RSA-CHACHA20-POLY1305]:"
  143. "ECDHE-RSA-AES128-GCM-SHA256",
  144. {
  145. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1},
  146. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 1},
  147. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  148. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 1},
  149. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  150. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  151. },
  152. },
  153. // @STRENGTH performs a stable strength-sort of the selected ciphers and
  154. // only the selected ciphers.
  155. {
  156. // To simplify things, banish all but {ECDHE_RSA,RSA} x
  157. // {CHACHA20,AES_256_CBC,AES_128_CBC} x SHA1.
  158. "!kEDH:!AESGCM:!3DES:!SHA256:!MD5:!SHA384:"
  159. // Order some ciphers backwards by strength.
  160. "ALL:-CHACHA20:-AES256:-AES128:-ALL:"
  161. // Select ECDHE ones and sort them by strength. Ties should resolve
  162. // based on the order above.
  163. "kECDHE:@STRENGTH:-ALL:"
  164. // Now bring back everything uses RSA. ECDHE_RSA should be first, sorted
  165. // by strength. Then RSA, backwards by strength.
  166. "aRSA",
  167. {
  168. {TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, 0},
  169. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  170. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  171. {TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, 0},
  172. {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
  173. {TLS1_CK_RSA_WITH_AES_256_SHA, 0},
  174. },
  175. },
  176. // Exact ciphers may not be used in multi-part rules; they are treated
  177. // as unknown aliases.
  178. {
  179. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  180. "ECDHE-RSA-AES128-GCM-SHA256:"
  181. "!ECDHE-RSA-AES128-GCM-SHA256+RSA:"
  182. "!ECDSA+ECDHE-ECDSA-AES128-GCM-SHA256",
  183. {
  184. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0},
  185. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0},
  186. },
  187. },
  188. // SSLv3 matches everything that existed before TLS 1.2.
  189. {
  190. "AES128-SHA:AES128-SHA256:!SSLv3",
  191. {
  192. {TLS1_CK_RSA_WITH_AES_128_SHA256, 0},
  193. },
  194. },
  195. // TLSv1.2 matches everything added in TLS 1.2.
  196. {
  197. "AES128-SHA:AES128-SHA256:!TLSv1.2",
  198. {
  199. {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
  200. },
  201. },
  202. // The two directives have no intersection.
  203. {
  204. "AES128-SHA:AES128-SHA256:!TLSv1.2+SSLv3",
  205. {
  206. {TLS1_CK_RSA_WITH_AES_128_SHA, 0},
  207. {TLS1_CK_RSA_WITH_AES_128_SHA256, 0},
  208. },
  209. },
  210. // The shared name of the CHACHA20_POLY1305 variants behaves like a cipher
  211. // name and not an alias. It may not be used in a multipart rule. (That the
  212. // shared name works is covered by the standard tests.)
  213. {
  214. "ECDHE-ECDSA-CHACHA20-POLY1305:"
  215. "ECDHE-RSA-CHACHA20-POLY1305:"
  216. "!ECDHE-RSA-CHACHA20-POLY1305+RSA:"
  217. "!ECDSA+ECDHE-ECDSA-CHACHA20-POLY1305",
  218. {
  219. {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  220. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0},
  221. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0},
  222. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0},
  223. },
  224. },
  225. };
  226. static const char *kBadRules[] = {
  227. // Invalid brackets.
  228. "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256",
  229. "RSA]",
  230. "[[RSA]]",
  231. // Operators inside brackets.
  232. "[+RSA]",
  233. // Unknown directive.
  234. "@BOGUS",
  235. // Empty cipher lists error at SSL_CTX_set_cipher_list.
  236. "",
  237. "BOGUS",
  238. // COMPLEMENTOFDEFAULT is empty.
  239. "COMPLEMENTOFDEFAULT",
  240. // Invalid command.
  241. "?BAR",
  242. // Special operators are not allowed if groups are used.
  243. "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:+FOO",
  244. "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:!FOO",
  245. "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:-FOO",
  246. "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:@STRENGTH",
  247. // Opcode supplied, but missing selector.
  248. "+",
  249. };
  250. static const char *kMustNotIncludeNull[] = {
  251. "ALL",
  252. "DEFAULT",
  253. "ALL:!eNULL",
  254. "ALL:!NULL",
  255. "HIGH",
  256. "FIPS",
  257. "SHA",
  258. "SHA1",
  259. "RSA",
  260. "SSLv3",
  261. "TLSv1",
  262. "TLSv1.2",
  263. };
  264. static const CurveTest kCurveTests[] = {
  265. {
  266. "P-256",
  267. { SSL_CURVE_SECP256R1 },
  268. },
  269. {
  270. "P-256:P-384:P-521:X25519",
  271. {
  272. SSL_CURVE_SECP256R1,
  273. SSL_CURVE_SECP384R1,
  274. SSL_CURVE_SECP521R1,
  275. SSL_CURVE_X25519,
  276. },
  277. },
  278. };
  279. static const char *kBadCurvesLists[] = {
  280. "",
  281. ":",
  282. "::",
  283. "P-256::X25519",
  284. "RSA:P-256",
  285. "P-256:RSA",
  286. "X25519:P-256:",
  287. ":X25519:P-256",
  288. };
  289. static void PrintCipherPreferenceList(ssl_cipher_preference_list_st *list) {
  290. bool in_group = false;
  291. for (size_t i = 0; i < sk_SSL_CIPHER_num(list->ciphers); i++) {
  292. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(list->ciphers, i);
  293. if (!in_group && list->in_group_flags[i]) {
  294. fprintf(stderr, "\t[\n");
  295. in_group = true;
  296. }
  297. fprintf(stderr, "\t");
  298. if (in_group) {
  299. fprintf(stderr, " ");
  300. }
  301. fprintf(stderr, "%s\n", SSL_CIPHER_get_name(cipher));
  302. if (in_group && !list->in_group_flags[i]) {
  303. fprintf(stderr, "\t]\n");
  304. in_group = false;
  305. }
  306. }
  307. }
  308. static bool TestCipherRule(const CipherTest &t) {
  309. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  310. if (!ctx) {
  311. return false;
  312. }
  313. if (!SSL_CTX_set_cipher_list(ctx.get(), t.rule)) {
  314. fprintf(stderr, "Error testing cipher rule '%s'\n", t.rule);
  315. return false;
  316. }
  317. // Compare the two lists.
  318. if (sk_SSL_CIPHER_num(ctx->cipher_list->ciphers) != t.expected.size()) {
  319. fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t.rule);
  320. PrintCipherPreferenceList(ctx->cipher_list);
  321. return false;
  322. }
  323. for (size_t i = 0; i < t.expected.size(); i++) {
  324. const SSL_CIPHER *cipher =
  325. sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i);
  326. if (t.expected[i].id != SSL_CIPHER_get_id(cipher) ||
  327. t.expected[i].in_group_flag != ctx->cipher_list->in_group_flags[i]) {
  328. fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t.rule);
  329. PrintCipherPreferenceList(ctx->cipher_list);
  330. return false;
  331. }
  332. }
  333. return true;
  334. }
  335. static bool TestRuleDoesNotIncludeNull(const char *rule) {
  336. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(SSLv23_server_method()));
  337. if (!ctx) {
  338. return false;
  339. }
  340. if (!SSL_CTX_set_cipher_list(ctx.get(), rule)) {
  341. fprintf(stderr, "Error: cipher rule '%s' failed\n", rule);
  342. return false;
  343. }
  344. for (size_t i = 0; i < sk_SSL_CIPHER_num(ctx->cipher_list->ciphers); i++) {
  345. if (SSL_CIPHER_is_NULL(sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i))) {
  346. fprintf(stderr, "Error: cipher rule '%s' includes NULL\n",rule);
  347. return false;
  348. }
  349. }
  350. return true;
  351. }
  352. static bool TestCipherRules() {
  353. for (const CipherTest &test : kCipherTests) {
  354. if (!TestCipherRule(test)) {
  355. return false;
  356. }
  357. }
  358. for (const char *rule : kBadRules) {
  359. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(SSLv23_server_method()));
  360. if (!ctx) {
  361. return false;
  362. }
  363. if (SSL_CTX_set_cipher_list(ctx.get(), rule)) {
  364. fprintf(stderr, "Cipher rule '%s' unexpectedly succeeded\n", rule);
  365. return false;
  366. }
  367. ERR_clear_error();
  368. }
  369. for (const char *rule : kMustNotIncludeNull) {
  370. if (!TestRuleDoesNotIncludeNull(rule)) {
  371. return false;
  372. }
  373. }
  374. return true;
  375. }
  376. static bool TestCurveRule(const CurveTest &t) {
  377. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  378. if (!ctx) {
  379. return false;
  380. }
  381. if (!SSL_CTX_set1_curves_list(ctx.get(), t.rule)) {
  382. fprintf(stderr, "Error testing curves list '%s'\n", t.rule);
  383. return false;
  384. }
  385. // Compare the two lists.
  386. if (ctx->supported_group_list_len != t.expected.size()) {
  387. fprintf(stderr, "Error testing curves list '%s': length\n", t.rule);
  388. return false;
  389. }
  390. for (size_t i = 0; i < t.expected.size(); i++) {
  391. if (t.expected[i] != ctx->supported_group_list[i]) {
  392. fprintf(stderr, "Error testing curves list '%s': mismatch\n", t.rule);
  393. return false;
  394. }
  395. }
  396. return true;
  397. }
  398. static bool TestCurveRules() {
  399. for (const CurveTest &test : kCurveTests) {
  400. if (!TestCurveRule(test)) {
  401. return false;
  402. }
  403. }
  404. for (const char *rule : kBadCurvesLists) {
  405. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(SSLv23_server_method()));
  406. if (!ctx) {
  407. return false;
  408. }
  409. if (SSL_CTX_set1_curves_list(ctx.get(), rule)) {
  410. fprintf(stderr, "Curves list '%s' unexpectedly succeeded\n", rule);
  411. return false;
  412. }
  413. ERR_clear_error();
  414. }
  415. return true;
  416. }
  417. // kOpenSSLSession is a serialized SSL_SESSION.
  418. static const char kOpenSSLSession[] =
  419. "MIIFqgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ"
  420. "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH"
  421. "IWoJoQYCBFRDO46iBAICASyjggR6MIIEdjCCA16gAwIBAgIIK9dUvsPWSlUwDQYJ"
  422. "KoZIhvcNAQEFBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx"
  423. "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTQxMDA4"
  424. "MTIwNzU3WhcNMTUwMTA2MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK"
  425. "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v"
  426. "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB"
  427. "AQUAA4IBDwAwggEKAoIBAQCcKeLrplAC+Lofy8t/wDwtB6eu72CVp0cJ4V3lknN6"
  428. "huH9ct6FFk70oRIh/VBNBBz900jYy+7111Jm1b8iqOTQ9aT5C7SEhNcQFJvqzH3e"
  429. "MPkb6ZSWGm1yGF7MCQTGQXF20Sk/O16FSjAynU/b3oJmOctcycWYkY0ytS/k3LBu"
  430. "Id45PJaoMqjB0WypqvNeJHC3q5JjCB4RP7Nfx5jjHSrCMhw8lUMW4EaDxjaR9KDh"
  431. "PLgjsk+LDIySRSRDaCQGhEOWLJZVLzLo4N6/UlctCHEllpBUSvEOyFga52qroGjg"
  432. "rf3WOQ925MFwzd6AK+Ich0gDRg8sQfdLH5OuP1cfLfU1AgMBAAGjggFBMIIBPTAd"
  433. "BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdv"
  434. "b2dsZS5jb20waAYIKwYBBQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtp"
  435. "Lmdvb2dsZS5jb20vR0lBRzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50"
  436. "czEuZ29vZ2xlLmNvbS9vY3NwMB0GA1UdDgQWBBQ7a+CcxsZByOpc+xpYFcIbnUMZ"
  437. "hTAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEv"
  438. "MBcGA1UdIAQQMA4wDAYKKwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRw"
  439. "Oi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBBQUAA4IBAQCa"
  440. "OXCBdoqUy5bxyq+Wrh1zsyyCFim1PH5VU2+yvDSWrgDY8ibRGJmfff3r4Lud5kal"
  441. "dKs9k8YlKD3ITG7P0YT/Rk8hLgfEuLcq5cc0xqmE42xJ+Eo2uzq9rYorc5emMCxf"
  442. "5L0TJOXZqHQpOEcuptZQ4OjdYMfSxk5UzueUhA3ogZKRcRkdB3WeWRp+nYRhx4St"
  443. "o2rt2A0MKmY9165GHUqMK9YaaXHDXqBu7Sefr1uSoAP9gyIJKeihMivsGqJ1TD6Z"
  444. "cc6LMe+dN2P8cZEQHtD1y296ul4Mivqk3jatUVL8/hCwgch9A8O4PGZq9WqBfEWm"
  445. "IyHh1dPtbg1lOXdYCWtjpAIEAKUDAgEUqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36S"
  446. "YTcLEkXqKwOBfF9vE4KX0NxeLwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9B"
  447. "sNHM362zZnY27GpTw+Kwd751CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yE"
  448. "OTDKPNj3+inbMaVigtK4PLyPq+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdA"
  449. "i4gv7Y5oliyntgMBAQA=";
  450. // kCustomSession is a custom serialized SSL_SESSION generated by
  451. // filling in missing fields from |kOpenSSLSession|. This includes
  452. // providing |peer_sha256|, so |peer| is not serialized.
  453. static const char kCustomSession[] =
  454. "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ"
  455. "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH"
  456. "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE"
  457. "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe"
  458. "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751"
  459. "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP"
  460. "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG"
  461. "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF";
  462. // kBoringSSLSession is a serialized SSL_SESSION generated from bssl client.
  463. static const char kBoringSSLSession[] =
  464. "MIIRwQIBAQICAwMEAsAvBCDdoGxGK26mR+8lM0uq6+k9xYuxPnwAjpcF9n0Yli9R"
  465. "kQQwbyshfWhdi5XQ1++7n2L1qqrcVlmHBPpr6yknT/u4pUrpQB5FZ7vqvNn8MdHf"
  466. "9rWgoQYCBFXgs7uiBAICHCCjggR6MIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJ"
  467. "KoZIhvcNAQELBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx"
  468. "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEy"
  469. "MTQ1MzE1WhcNMTUxMTEwMDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK"
  470. "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v"
  471. "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB"
  472. "AQUAA4IBDwAwggEKAoIBAQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpo"
  473. "PLuBinvhkXZo3DC133NpCBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU"
  474. "792c7hFyNXSUCG7At8Ifi3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mce"
  475. "Tv9iGKqSkSTlp8puy/9SZ/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/"
  476. "RCh8/UKc8PaL+cxlt531qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eL"
  477. "EucWQ72YZU8mUzXBoXGn0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAd"
  478. "BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdv"
  479. "b2dsZS5jb20waAYIKwYBBQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtp"
  480. "Lmdvb2dsZS5jb20vR0lBRzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50"
  481. "czEuZ29vZ2xlLmNvbS9vY3NwMB0GA1UdDgQWBBS/bzHxcE73Q4j3slC4BLbMtLjG"
  482. "GjAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEv"
  483. "MBcGA1UdIAQQMA4wDAYKKwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRw"
  484. "Oi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBCwUAA4IBAQAb"
  485. "qdWPZEHk0X7iKPCTHL6S3w6q1eR67goxZGFSM1lk1hjwyu7XcLJuvALVV9uY3ovE"
  486. "kQZSHwT+pyOPWQhsSjO+1GyjvCvK/CAwiUmBX+bQRGaqHsRcio7xSbdVcajQ3bXd"
  487. "X+s0WdbOpn6MStKAiBVloPlSxEI8pxY6x/BBCnTIk/+DMB17uZlOjG3vbAnkDkP+"
  488. "n0OTucD9sHV7EVj9XUxi51nOfNBCN/s7lpUjDS/NJ4k3iwOtbCPswiot8vLO779a"
  489. "f07vR03r349Iz/KTzk95rlFtX0IU+KYNxFNsanIXZ+C9FYGRXkwhHcvFb4qMUB1y"
  490. "TTlM80jBMOwyjZXmjRAhpAIEAKUDAgEUqQUCAwGJwKqBpwSBpOgebbmn9NRUtMWH"
  491. "+eJpqA5JLMFSMCChOsvKey3toBaCNGU7HfAEiiXNuuAdCBoK262BjQc2YYfqFzqH"
  492. "zuppopXCvhohx7j/tnCNZIMgLYt/O9SXK2RYI5z8FhCCHvB4CbD5G0LGl5EFP27s"
  493. "Jb6S3aTTYPkQe8yZSlxevg6NDwmTogLO9F7UUkaYmVcMQhzssEE2ZRYNwSOU6KjE"
  494. "0Yj+8fAiBtbQriIEIN2L8ZlpaVrdN5KFNdvcmOxJu81P8q53X55xQyGTnGWwsgMC"
  495. "ARezggvvMIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJKoZIhvcNAQELBQAwSTEL"
  496. "MAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2ds"
  497. "ZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEyMTQ1MzE1WhcNMTUxMTEw"
  498. "MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG"
  499. "A1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UE"
  500. "AwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB"
  501. "AQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpoPLuBinvhkXZo3DC133Np"
  502. "CBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU792c7hFyNXSUCG7At8If"
  503. "i3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mceTv9iGKqSkSTlp8puy/9S"
  504. "Z/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/RCh8/UKc8PaL+cxlt531"
  505. "qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eLEucWQ72YZU8mUzXBoXGn"
  506. "0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAdBgNVHSUEFjAUBggrBgEF"
  507. "BQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdvb2dsZS5jb20waAYIKwYB"
  508. "BQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtpLmdvb2dsZS5jb20vR0lB"
  509. "RzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50czEuZ29vZ2xlLmNvbS9v"
  510. "Y3NwMB0GA1UdDgQWBBS/bzHxcE73Q4j3slC4BLbMtLjGGjAMBgNVHRMBAf8EAjAA"
  511. "MB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEvMBcGA1UdIAQQMA4wDAYK"
  512. "KwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRwOi8vcGtpLmdvb2dsZS5j"
  513. "b20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBCwUAA4IBAQAbqdWPZEHk0X7iKPCTHL6S"
  514. "3w6q1eR67goxZGFSM1lk1hjwyu7XcLJuvALVV9uY3ovEkQZSHwT+pyOPWQhsSjO+"
  515. "1GyjvCvK/CAwiUmBX+bQRGaqHsRcio7xSbdVcajQ3bXdX+s0WdbOpn6MStKAiBVl"
  516. "oPlSxEI8pxY6x/BBCnTIk/+DMB17uZlOjG3vbAnkDkP+n0OTucD9sHV7EVj9XUxi"
  517. "51nOfNBCN/s7lpUjDS/NJ4k3iwOtbCPswiot8vLO779af07vR03r349Iz/KTzk95"
  518. "rlFtX0IU+KYNxFNsanIXZ+C9FYGRXkwhHcvFb4qMUB1yTTlM80jBMOwyjZXmjRAh"
  519. "MIID8DCCAtigAwIBAgIDAjqDMA0GCSqGSIb3DQEBCwUAMEIxCzAJBgNVBAYTAlVT"
  520. "MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i"
  521. "YWwgQ0EwHhcNMTMwNDA1MTUxNTU2WhcNMTYxMjMxMjM1OTU5WjBJMQswCQYDVQQG"
  522. "EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy"
  523. "bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB"
  524. "AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP"
  525. "VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/ck5vuR6RXEz/RTDfRK/J9U3n2+oGtv"
  526. "h8DQUB8oMANA2ghzUWx//zo8pzcGjr1LEQTrfSTe5vn8MXH7lNVg8y5Kr0LSy+rE"
  527. "ahqyzFPdFUuLH8gZYR/Nnag+YyuENWllhMgZxUYi+FOVvuOAShDGKuy6lyARxzmZ"
  528. "EASg8GF6lSWMTlJ14rbtCMoU/M4iarNOz0YDl5cDfsCx3nuvRTPPuj5xt970JSXC"
  529. "DTWJnZ37DhF5iR43xa+OcmkCAwEAAaOB5zCB5DAfBgNVHSMEGDAWgBTAephojYn7"
  530. "qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wDgYD"
  531. "VR0PAQH/BAQDAgEGMC4GCCsGAQUFBwEBBCIwIDAeBggrBgEFBQcwAYYSaHR0cDov"
  532. "L2cuc3ltY2QuY29tMBIGA1UdEwEB/wQIMAYBAf8CAQAwNQYDVR0fBC4wLDAqoCig"
  533. "JoYkaHR0cDovL2cuc3ltY2IuY29tL2NybHMvZ3RnbG9iYWwuY3JsMBcGA1UdIAQQ"
  534. "MA4wDAYKKwYBBAHWeQIFATANBgkqhkiG9w0BAQsFAAOCAQEAqvqpIM1qZ4PtXtR+"
  535. "3h3Ef+AlBgDFJPupyC1tft6dgmUsgWM0Zj7pUsIItMsv91+ZOmqcUHqFBYx90SpI"
  536. "hNMJbHzCzTWf84LuUt5oX+QAihcglvcpjZpNy6jehsgNb1aHA30DP9z6eX0hGfnI"
  537. "Oi9RdozHQZJxjyXON/hKTAAj78Q1EK7gI4BzfE00LshukNYQHpmEcxpw8u1VDu4X"
  538. "Bupn7jLrLN1nBz/2i8Jw3lsA5rsb0zYaImxssDVCbJAJPZPpZAkiDoUGn8JzIdPm"
  539. "X4DkjYUiOnMDsWCOrmji9D6X52ASCWg23jrW4kOVWzeBkoEfu43XrVJkFleW2V40"
  540. "fsg12DCCA30wggLmoAMCAQICAxK75jANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQG"
  541. "EwJVUzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUg"
  542. "Q2VydGlmaWNhdGUgQXV0aG9yaXR5MB4XDTAyMDUyMTA0MDAwMFoXDTE4MDgyMTA0"
  543. "MDAwMFowQjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUdlb1RydXN0IEluYy4xGzAZ"
  544. "BgNVBAMTEkdlb1RydXN0IEdsb2JhbCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEP"
  545. "ADCCAQoCggEBANrMGGMw/fQXIxpWflvfPGw45HG3eJHUvKHYTPioQ7YD6U0hBwiI"
  546. "2lgvZjkpvQV4i5046AW3an5xpObEYKaw74DkiSgPniXW7YPzraaRx5jJQhg1FJ2t"
  547. "mEaSLk/K8YdDwRaVVy1Q74ktgHpXrfLuX2vSAI25FPgUFTXZwEaje3LIkb/JVSvN"
  548. "0Jc+nCZkzN/Ogxlxyk7m1NV7qRnNVd7I7NJeOFPlXE+MLf5QIzb8ZubLjqQ5GQC3"
  549. "lQI5kQsO/jgu0R0FmvZNPm8PBx2vLB6PYDni+jZTEznUXiYr2z2oFL0y6xgDKFIE"
  550. "ceWrMz3hOLsHNoRinHnqFjD0X8Ar6HFr5PkCAwEAAaOB8DCB7TAfBgNVHSMEGDAW"
  551. "gBRI5mj5K9KylddH2CMgEE8zmJCf1DAdBgNVHQ4EFgQUwHqYaI2J+6sFZAwRfap9"
  552. "ZbjKzE4wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMw"
  553. "MTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9zZWN1cmVjYS5j"
  554. "cmwwTgYDVR0gBEcwRTBDBgRVHSAAMDswOQYIKwYBBQUHAgEWLWh0dHBzOi8vd3d3"
  555. "Lmdlb3RydXN0LmNvbS9yZXNvdXJjZXMvcmVwb3NpdG9yeTANBgkqhkiG9w0BAQUF"
  556. "AAOBgQB24RJuTksWEoYwBrKBCM/wCMfHcX5m7sLt1Dsf//DwyE7WQziwuTB9GNBV"
  557. "g6JqyzYRnOhIZqNtf7gT1Ef+i1pcc/yu2RsyGTirlzQUqpbS66McFAhJtrvlke+D"
  558. "NusdVm/K2rxzY5Dkf3s+Iss9B+1fOHSc4wNQTqGvmO5h8oQ/Eg==";
  559. // kBadSessionExtraField is a custom serialized SSL_SESSION generated by replacing
  560. // the final (optional) element of |kCustomSession| with tag number 30.
  561. static const char kBadSessionExtraField[] =
  562. "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ"
  563. "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH"
  564. "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE"
  565. "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe"
  566. "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751"
  567. "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP"
  568. "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG"
  569. "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBL4DBAEF";
  570. // kBadSessionVersion is a custom serialized SSL_SESSION generated by replacing
  571. // the version of |kCustomSession| with 2.
  572. static const char kBadSessionVersion[] =
  573. "MIIBdgIBAgICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ"
  574. "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH"
  575. "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE"
  576. "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe"
  577. "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751"
  578. "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP"
  579. "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG"
  580. "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF";
  581. // kBadSessionTrailingData is a custom serialized SSL_SESSION with trailing data
  582. // appended.
  583. static const char kBadSessionTrailingData[] =
  584. "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ"
  585. "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH"
  586. "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE"
  587. "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe"
  588. "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751"
  589. "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP"
  590. "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG"
  591. "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEFAAAA";
  592. static bool DecodeBase64(std::vector<uint8_t> *out, const char *in) {
  593. size_t len;
  594. if (!EVP_DecodedLength(&len, strlen(in))) {
  595. fprintf(stderr, "EVP_DecodedLength failed\n");
  596. return false;
  597. }
  598. out->resize(len);
  599. if (!EVP_DecodeBase64(out->data(), &len, len, (const uint8_t *)in,
  600. strlen(in))) {
  601. fprintf(stderr, "EVP_DecodeBase64 failed\n");
  602. return false;
  603. }
  604. out->resize(len);
  605. return true;
  606. }
  607. static bool TestSSL_SESSIONEncoding(const char *input_b64) {
  608. const uint8_t *cptr;
  609. uint8_t *ptr;
  610. // Decode the input.
  611. std::vector<uint8_t> input;
  612. if (!DecodeBase64(&input, input_b64)) {
  613. return false;
  614. }
  615. // Verify the SSL_SESSION decodes.
  616. bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(input.data(), input.size()));
  617. if (!session) {
  618. fprintf(stderr, "SSL_SESSION_from_bytes failed\n");
  619. return false;
  620. }
  621. // Verify the SSL_SESSION encoding round-trips.
  622. size_t encoded_len;
  623. bssl::UniquePtr<uint8_t> encoded;
  624. uint8_t *encoded_raw;
  625. if (!SSL_SESSION_to_bytes(session.get(), &encoded_raw, &encoded_len)) {
  626. fprintf(stderr, "SSL_SESSION_to_bytes failed\n");
  627. return false;
  628. }
  629. encoded.reset(encoded_raw);
  630. if (encoded_len != input.size() ||
  631. memcmp(input.data(), encoded.get(), input.size()) != 0) {
  632. fprintf(stderr, "SSL_SESSION_to_bytes did not round-trip\n");
  633. hexdump(stderr, "Before: ", input.data(), input.size());
  634. hexdump(stderr, "After: ", encoded_raw, encoded_len);
  635. return false;
  636. }
  637. // Verify the SSL_SESSION also decodes with the legacy API.
  638. cptr = input.data();
  639. session.reset(d2i_SSL_SESSION(NULL, &cptr, input.size()));
  640. if (!session || cptr != input.data() + input.size()) {
  641. fprintf(stderr, "d2i_SSL_SESSION failed\n");
  642. return false;
  643. }
  644. // Verify the SSL_SESSION encoding round-trips via the legacy API.
  645. int len = i2d_SSL_SESSION(session.get(), NULL);
  646. if (len < 0 || (size_t)len != input.size()) {
  647. fprintf(stderr, "i2d_SSL_SESSION(NULL) returned invalid length\n");
  648. return false;
  649. }
  650. encoded.reset((uint8_t *)OPENSSL_malloc(input.size()));
  651. if (!encoded) {
  652. fprintf(stderr, "malloc failed\n");
  653. return false;
  654. }
  655. ptr = encoded.get();
  656. len = i2d_SSL_SESSION(session.get(), &ptr);
  657. if (len < 0 || (size_t)len != input.size()) {
  658. fprintf(stderr, "i2d_SSL_SESSION returned invalid length\n");
  659. return false;
  660. }
  661. if (ptr != encoded.get() + input.size()) {
  662. fprintf(stderr, "i2d_SSL_SESSION did not advance ptr correctly\n");
  663. return false;
  664. }
  665. if (memcmp(input.data(), encoded.get(), input.size()) != 0) {
  666. fprintf(stderr, "i2d_SSL_SESSION did not round-trip\n");
  667. return false;
  668. }
  669. return true;
  670. }
  671. static bool TestBadSSL_SESSIONEncoding(const char *input_b64) {
  672. std::vector<uint8_t> input;
  673. if (!DecodeBase64(&input, input_b64)) {
  674. return false;
  675. }
  676. // Verify that the SSL_SESSION fails to decode.
  677. bssl::UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(input.data(), input.size()));
  678. if (session) {
  679. fprintf(stderr, "SSL_SESSION_from_bytes unexpectedly succeeded\n");
  680. return false;
  681. }
  682. ERR_clear_error();
  683. return true;
  684. }
  685. static bool TestDefaultVersion(uint16_t min_version, uint16_t max_version,
  686. const SSL_METHOD *(*method)(void)) {
  687. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method()));
  688. if (!ctx) {
  689. return false;
  690. }
  691. if (ctx->min_version != min_version || ctx->max_version != max_version) {
  692. fprintf(stderr, "Got min %04x, max %04x; wanted min %04x, max %04x\n",
  693. ctx->min_version, ctx->max_version, min_version, max_version);
  694. return false;
  695. }
  696. return true;
  697. }
  698. static bool CipherGetRFCName(std::string *out, uint16_t value) {
  699. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(value);
  700. if (cipher == NULL) {
  701. return false;
  702. }
  703. bssl::UniquePtr<char> rfc_name(SSL_CIPHER_get_rfc_name(cipher));
  704. if (!rfc_name) {
  705. return false;
  706. }
  707. out->assign(rfc_name.get());
  708. return true;
  709. }
  710. typedef struct {
  711. int id;
  712. const char *rfc_name;
  713. } CIPHER_RFC_NAME_TEST;
  714. static const CIPHER_RFC_NAME_TEST kCipherRFCNameTests[] = {
  715. {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
  716. {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"},
  717. {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
  718. {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
  719. "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
  720. {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
  721. "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
  722. {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
  723. "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
  724. {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  725. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"},
  726. {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  727. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"},
  728. {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  729. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"},
  730. {TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  731. "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA"},
  732. {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
  733. "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"},
  734. {TLS1_CK_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384"},
  735. {TLS1_CK_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256"},
  736. {TLS1_CK_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256"},
  737. // These names are non-standard:
  738. {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD,
  739. "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"},
  740. {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD,
  741. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"},
  742. };
  743. static bool TestCipherGetRFCName(void) {
  744. for (size_t i = 0;
  745. i < OPENSSL_ARRAY_SIZE(kCipherRFCNameTests); i++) {
  746. const CIPHER_RFC_NAME_TEST *test = &kCipherRFCNameTests[i];
  747. std::string rfc_name;
  748. if (!CipherGetRFCName(&rfc_name, test->id & 0xffff)) {
  749. fprintf(stderr, "SSL_CIPHER_get_rfc_name failed\n");
  750. return false;
  751. }
  752. if (rfc_name != test->rfc_name) {
  753. fprintf(stderr, "SSL_CIPHER_get_rfc_name: got '%s', wanted '%s'\n",
  754. rfc_name.c_str(), test->rfc_name);
  755. return false;
  756. }
  757. }
  758. return true;
  759. }
  760. // CreateSessionWithTicket returns a sample |SSL_SESSION| with the specified
  761. // version and ticket length or nullptr on failure.
  762. static bssl::UniquePtr<SSL_SESSION> CreateSessionWithTicket(uint16_t version,
  763. size_t ticket_len) {
  764. std::vector<uint8_t> der;
  765. if (!DecodeBase64(&der, kOpenSSLSession)) {
  766. return nullptr;
  767. }
  768. bssl::UniquePtr<SSL_SESSION> session(
  769. SSL_SESSION_from_bytes(der.data(), der.size()));
  770. if (!session) {
  771. return nullptr;
  772. }
  773. session->ssl_version = version;
  774. // Swap out the ticket for a garbage one.
  775. OPENSSL_free(session->tlsext_tick);
  776. session->tlsext_tick = reinterpret_cast<uint8_t*>(OPENSSL_malloc(ticket_len));
  777. if (session->tlsext_tick == nullptr) {
  778. return nullptr;
  779. }
  780. memset(session->tlsext_tick, 'a', ticket_len);
  781. session->tlsext_ticklen = ticket_len;
  782. // Fix up the timeout.
  783. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  784. session->time = 1234;
  785. #else
  786. session->time = time(NULL);
  787. #endif
  788. return session;
  789. }
  790. static bool GetClientHello(SSL *ssl, std::vector<uint8_t> *out) {
  791. bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
  792. if (!bio) {
  793. return false;
  794. }
  795. // Do not configure a reading BIO, but record what's written to a memory BIO.
  796. BIO_up_ref(bio.get());
  797. SSL_set_bio(ssl, nullptr /* rbio */, bio.get());
  798. int ret = SSL_connect(ssl);
  799. if (ret > 0) {
  800. // SSL_connect should fail without a BIO to write to.
  801. return false;
  802. }
  803. ERR_clear_error();
  804. const uint8_t *client_hello;
  805. size_t client_hello_len;
  806. if (!BIO_mem_contents(bio.get(), &client_hello, &client_hello_len)) {
  807. return false;
  808. }
  809. *out = std::vector<uint8_t>(client_hello, client_hello + client_hello_len);
  810. return true;
  811. }
  812. // GetClientHelloLen creates a client SSL connection with the specified version
  813. // and ticket length. It returns the length of the ClientHello, not including
  814. // the record header, on success and zero on error.
  815. static size_t GetClientHelloLen(uint16_t max_version, uint16_t session_version,
  816. size_t ticket_len) {
  817. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  818. bssl::UniquePtr<SSL_SESSION> session =
  819. CreateSessionWithTicket(session_version, ticket_len);
  820. if (!ctx || !session) {
  821. return 0;
  822. }
  823. // Set a one-element cipher list so the baseline ClientHello is unpadded.
  824. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  825. if (!ssl || !SSL_set_session(ssl.get(), session.get()) ||
  826. !SSL_set_cipher_list(ssl.get(), "ECDHE-RSA-AES128-GCM-SHA256") ||
  827. !SSL_set_max_proto_version(ssl.get(), max_version)) {
  828. return 0;
  829. }
  830. std::vector<uint8_t> client_hello;
  831. if (!GetClientHello(ssl.get(), &client_hello) ||
  832. client_hello.size() <= SSL3_RT_HEADER_LENGTH) {
  833. return 0;
  834. }
  835. return client_hello.size() - SSL3_RT_HEADER_LENGTH;
  836. }
  837. struct PaddingTest {
  838. size_t input_len, padded_len;
  839. };
  840. static const PaddingTest kPaddingTests[] = {
  841. // ClientHellos of length below 0x100 do not require padding.
  842. {0xfe, 0xfe},
  843. {0xff, 0xff},
  844. // ClientHellos of length 0x100 through 0x1fb are padded up to 0x200.
  845. {0x100, 0x200},
  846. {0x123, 0x200},
  847. {0x1fb, 0x200},
  848. // ClientHellos of length 0x1fc through 0x1ff get padded beyond 0x200. The
  849. // padding extension takes a minimum of four bytes plus one required content
  850. // byte. (To work around yet more server bugs, we avoid empty final
  851. // extensions.)
  852. {0x1fc, 0x201},
  853. {0x1fd, 0x202},
  854. {0x1fe, 0x203},
  855. {0x1ff, 0x204},
  856. // Finally, larger ClientHellos need no padding.
  857. {0x200, 0x200},
  858. {0x201, 0x201},
  859. };
  860. static bool TestPaddingExtension(uint16_t max_version,
  861. uint16_t session_version) {
  862. // Sample a baseline length.
  863. size_t base_len = GetClientHelloLen(max_version, session_version, 1);
  864. if (base_len == 0) {
  865. return false;
  866. }
  867. for (const PaddingTest &test : kPaddingTests) {
  868. if (base_len > test.input_len) {
  869. fprintf(stderr,
  870. "Baseline ClientHello too long (max_version = %04x, "
  871. "session_version = %04x).\n",
  872. max_version, session_version);
  873. return false;
  874. }
  875. size_t padded_len = GetClientHelloLen(max_version, session_version,
  876. 1 + test.input_len - base_len);
  877. if (padded_len != test.padded_len) {
  878. fprintf(stderr,
  879. "%u-byte ClientHello padded to %u bytes, not %u (max_version = "
  880. "%04x, session_version = %04x).\n",
  881. static_cast<unsigned>(test.input_len),
  882. static_cast<unsigned>(padded_len),
  883. static_cast<unsigned>(test.padded_len), max_version,
  884. session_version);
  885. return false;
  886. }
  887. }
  888. return true;
  889. }
  890. // Test that |SSL_get_client_CA_list| echoes back the configured parameter even
  891. // before configuring as a server.
  892. static bool TestClientCAList() {
  893. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  894. if (!ctx) {
  895. return false;
  896. }
  897. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  898. if (!ssl) {
  899. return false;
  900. }
  901. STACK_OF(X509_NAME) *stack = sk_X509_NAME_new_null();
  902. if (stack == nullptr) {
  903. return false;
  904. }
  905. // |SSL_set_client_CA_list| takes ownership.
  906. SSL_set_client_CA_list(ssl.get(), stack);
  907. return SSL_get_client_CA_list(ssl.get()) == stack;
  908. }
  909. static void AppendSession(SSL_SESSION *session, void *arg) {
  910. std::vector<SSL_SESSION*> *out =
  911. reinterpret_cast<std::vector<SSL_SESSION*>*>(arg);
  912. out->push_back(session);
  913. }
  914. // ExpectCache returns true if |ctx|'s session cache consists of |expected|, in
  915. // order.
  916. static bool ExpectCache(SSL_CTX *ctx,
  917. const std::vector<SSL_SESSION*> &expected) {
  918. // Check the linked list.
  919. SSL_SESSION *ptr = ctx->session_cache_head;
  920. for (SSL_SESSION *session : expected) {
  921. if (ptr != session) {
  922. return false;
  923. }
  924. // TODO(davidben): This is an absurd way to denote the end of the list.
  925. if (ptr->next ==
  926. reinterpret_cast<SSL_SESSION *>(&ctx->session_cache_tail)) {
  927. ptr = nullptr;
  928. } else {
  929. ptr = ptr->next;
  930. }
  931. }
  932. if (ptr != nullptr) {
  933. return false;
  934. }
  935. // Check the hash table.
  936. std::vector<SSL_SESSION*> actual, expected_copy;
  937. lh_SSL_SESSION_doall_arg(SSL_CTX_sessions(ctx), AppendSession, &actual);
  938. expected_copy = expected;
  939. std::sort(actual.begin(), actual.end());
  940. std::sort(expected_copy.begin(), expected_copy.end());
  941. return actual == expected_copy;
  942. }
  943. static bssl::UniquePtr<SSL_SESSION> CreateTestSession(uint32_t number) {
  944. bssl::UniquePtr<SSL_SESSION> ret(SSL_SESSION_new());
  945. if (!ret) {
  946. return nullptr;
  947. }
  948. ret->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  949. memset(ret->session_id, 0, ret->session_id_length);
  950. memcpy(ret->session_id, &number, sizeof(number));
  951. return ret;
  952. }
  953. // Test that the internal session cache behaves as expected.
  954. static bool TestInternalSessionCache() {
  955. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  956. if (!ctx) {
  957. return false;
  958. }
  959. // Prepare 10 test sessions.
  960. std::vector<bssl::UniquePtr<SSL_SESSION>> sessions;
  961. for (int i = 0; i < 10; i++) {
  962. bssl::UniquePtr<SSL_SESSION> session = CreateTestSession(i);
  963. if (!session) {
  964. return false;
  965. }
  966. sessions.push_back(std::move(session));
  967. }
  968. SSL_CTX_sess_set_cache_size(ctx.get(), 5);
  969. // Insert all the test sessions.
  970. for (const auto &session : sessions) {
  971. if (!SSL_CTX_add_session(ctx.get(), session.get())) {
  972. return false;
  973. }
  974. }
  975. // Only the last five should be in the list.
  976. std::vector<SSL_SESSION*> expected = {
  977. sessions[9].get(),
  978. sessions[8].get(),
  979. sessions[7].get(),
  980. sessions[6].get(),
  981. sessions[5].get(),
  982. };
  983. if (!ExpectCache(ctx.get(), expected)) {
  984. return false;
  985. }
  986. // Inserting an element already in the cache should fail.
  987. if (SSL_CTX_add_session(ctx.get(), sessions[7].get()) ||
  988. !ExpectCache(ctx.get(), expected)) {
  989. return false;
  990. }
  991. // Although collisions should be impossible (256-bit session IDs), the cache
  992. // must handle them gracefully.
  993. bssl::UniquePtr<SSL_SESSION> collision(CreateTestSession(7));
  994. if (!collision || !SSL_CTX_add_session(ctx.get(), collision.get())) {
  995. return false;
  996. }
  997. expected = {
  998. collision.get(),
  999. sessions[9].get(),
  1000. sessions[8].get(),
  1001. sessions[6].get(),
  1002. sessions[5].get(),
  1003. };
  1004. if (!ExpectCache(ctx.get(), expected)) {
  1005. return false;
  1006. }
  1007. // Removing sessions behaves correctly.
  1008. if (!SSL_CTX_remove_session(ctx.get(), sessions[6].get())) {
  1009. return false;
  1010. }
  1011. expected = {
  1012. collision.get(),
  1013. sessions[9].get(),
  1014. sessions[8].get(),
  1015. sessions[5].get(),
  1016. };
  1017. if (!ExpectCache(ctx.get(), expected)) {
  1018. return false;
  1019. }
  1020. // Removing sessions requires an exact match.
  1021. if (SSL_CTX_remove_session(ctx.get(), sessions[0].get()) ||
  1022. SSL_CTX_remove_session(ctx.get(), sessions[7].get()) ||
  1023. !ExpectCache(ctx.get(), expected)) {
  1024. return false;
  1025. }
  1026. return true;
  1027. }
  1028. static uint16_t EpochFromSequence(uint64_t seq) {
  1029. return static_cast<uint16_t>(seq >> 48);
  1030. }
  1031. static bssl::UniquePtr<X509> GetTestCertificate() {
  1032. static const char kCertPEM[] =
  1033. "-----BEGIN CERTIFICATE-----\n"
  1034. "MIICWDCCAcGgAwIBAgIJAPuwTC6rEJsMMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\n"
  1035. "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n"
  1036. "aWRnaXRzIFB0eSBMdGQwHhcNMTQwNDIzMjA1MDQwWhcNMTcwNDIyMjA1MDQwWjBF\n"
  1037. "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n"
  1038. "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
  1039. "gQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92kWdGMdAQhLci\n"
  1040. "HnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiFKKAnHmUcrgfV\n"
  1041. "W28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQABo1AwTjAdBgNV\n"
  1042. "HQ4EFgQUi3XVrMsIvg4fZbf6Vr5sp3Xaha8wHwYDVR0jBBgwFoAUi3XVrMsIvg4f\n"
  1043. "Zbf6Vr5sp3Xaha8wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQA76Hht\n"
  1044. "ldY9avcTGSwbwoiuIqv0jTL1fHFnzy3RHMLDh+Lpvolc5DSrSJHCP5WuK0eeJXhr\n"
  1045. "T5oQpHL9z/cCDLAKCKRa4uV0fhEdOWBqyR9p8y5jJtye72t6CuFUV5iqcpF4BH4f\n"
  1046. "j2VNHwsSrJwkD4QUGlUtH7vwnQmyCFxZMmWAJg==\n"
  1047. "-----END CERTIFICATE-----\n";
  1048. bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kCertPEM, strlen(kCertPEM)));
  1049. return bssl::UniquePtr<X509>(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
  1050. }
  1051. static bssl::UniquePtr<EVP_PKEY> GetTestKey() {
  1052. static const char kKeyPEM[] =
  1053. "-----BEGIN RSA PRIVATE KEY-----\n"
  1054. "MIICXgIBAAKBgQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92\n"
  1055. "kWdGMdAQhLciHnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiF\n"
  1056. "KKAnHmUcrgfVW28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQAB\n"
  1057. "AoGBAIBy09Fd4DOq/Ijp8HeKuCMKTHqTW1xGHshLQ6jwVV2vWZIn9aIgmDsvkjCe\n"
  1058. "i6ssZvnbjVcwzSoByhjN8ZCf/i15HECWDFFh6gt0P5z0MnChwzZmvatV/FXCT0j+\n"
  1059. "WmGNB/gkehKjGXLLcjTb6dRYVJSCZhVuOLLcbWIV10gggJQBAkEA8S8sGe4ezyyZ\n"
  1060. "m4e9r95g6s43kPqtj5rewTsUxt+2n4eVodD+ZUlCULWVNAFLkYRTBCASlSrm9Xhj\n"
  1061. "QpmWAHJUkQJBAOVzQdFUaewLtdOJoPCtpYoY1zd22eae8TQEmpGOR11L6kbxLQsk\n"
  1062. "aMly/DOnOaa82tqAGTdqDEZgSNmCeKKknmECQAvpnY8GUOVAubGR6c+W90iBuQLj\n"
  1063. "LtFp/9ihd2w/PoDwrHZaoUYVcT4VSfJQog/k7kjE4MYXYWL8eEKg3WTWQNECQQDk\n"
  1064. "104Wi91Umd1PzF0ijd2jXOERJU1wEKe6XLkYYNHWQAe5l4J4MWj9OdxFXAxIuuR/\n"
  1065. "tfDwbqkta4xcux67//khAkEAvvRXLHTaa6VFzTaiiO8SaFsHV3lQyXOtMrBpB5jd\n"
  1066. "moZWgjHvB2W9Ckn7sDqsPB+U2tyX0joDdQEyuiMECDY8oQ==\n"
  1067. "-----END RSA PRIVATE KEY-----\n";
  1068. bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kKeyPEM, strlen(kKeyPEM)));
  1069. return bssl::UniquePtr<EVP_PKEY>(
  1070. PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
  1071. }
  1072. static bssl::UniquePtr<X509> GetECDSATestCertificate() {
  1073. static const char kCertPEM[] =
  1074. "-----BEGIN CERTIFICATE-----\n"
  1075. "MIIBzzCCAXagAwIBAgIJANlMBNpJfb/rMAkGByqGSM49BAEwRTELMAkGA1UEBhMC\n"
  1076. "QVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdp\n"
  1077. "dHMgUHR5IEx0ZDAeFw0xNDA0MjMyMzIxNTdaFw0xNDA1MjMyMzIxNTdaMEUxCzAJ\n"
  1078. "BgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5l\n"
  1079. "dCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATmK2ni\n"
  1080. "v2Wfl74vHg2UikzVl2u3qR4NRvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYa\n"
  1081. "HPUdfvGULUvPciLBo1AwTjAdBgNVHQ4EFgQUq4TSrKuV8IJOFngHVVdf5CaNgtEw\n"
  1082. "HwYDVR0jBBgwFoAUq4TSrKuV8IJOFngHVVdf5CaNgtEwDAYDVR0TBAUwAwEB/zAJ\n"
  1083. "BgcqhkjOPQQBA0gAMEUCIQDyoDVeUTo2w4J5m+4nUIWOcAZ0lVfSKXQA9L4Vh13E\n"
  1084. "BwIgfB55FGohg/B6dGh5XxSZmmi08cueFV7mHzJSYV51yRQ=\n"
  1085. "-----END CERTIFICATE-----\n";
  1086. bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kCertPEM, strlen(kCertPEM)));
  1087. return bssl::UniquePtr<X509>(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
  1088. }
  1089. static bssl::UniquePtr<EVP_PKEY> GetECDSATestKey() {
  1090. static const char kKeyPEM[] =
  1091. "-----BEGIN PRIVATE KEY-----\n"
  1092. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgBw8IcnrUoEqc3VnJ\n"
  1093. "TYlodwi1b8ldMHcO6NHJzgqLtGqhRANCAATmK2niv2Wfl74vHg2UikzVl2u3qR4N\n"
  1094. "Rvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYaHPUdfvGULUvPciLB\n"
  1095. "-----END PRIVATE KEY-----\n";
  1096. bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kKeyPEM, strlen(kKeyPEM)));
  1097. return bssl::UniquePtr<EVP_PKEY>(
  1098. PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
  1099. }
  1100. static bool CompleteHandshakes(SSL *client, SSL *server) {
  1101. // Drive both their handshakes to completion.
  1102. for (;;) {
  1103. int client_ret = SSL_do_handshake(client);
  1104. int client_err = SSL_get_error(client, client_ret);
  1105. if (client_err != SSL_ERROR_NONE &&
  1106. client_err != SSL_ERROR_WANT_READ &&
  1107. client_err != SSL_ERROR_WANT_WRITE) {
  1108. fprintf(stderr, "Client error: %d\n", client_err);
  1109. return false;
  1110. }
  1111. int server_ret = SSL_do_handshake(server);
  1112. int server_err = SSL_get_error(server, server_ret);
  1113. if (server_err != SSL_ERROR_NONE &&
  1114. server_err != SSL_ERROR_WANT_READ &&
  1115. server_err != SSL_ERROR_WANT_WRITE) {
  1116. fprintf(stderr, "Server error: %d\n", server_err);
  1117. return false;
  1118. }
  1119. if (client_ret == 1 && server_ret == 1) {
  1120. break;
  1121. }
  1122. }
  1123. return true;
  1124. }
  1125. static bool ConnectClientAndServer(bssl::UniquePtr<SSL> *out_client,
  1126. bssl::UniquePtr<SSL> *out_server,
  1127. SSL_CTX *client_ctx, SSL_CTX *server_ctx,
  1128. SSL_SESSION *session) {
  1129. bssl::UniquePtr<SSL> client(SSL_new(client_ctx)), server(SSL_new(server_ctx));
  1130. if (!client || !server) {
  1131. return false;
  1132. }
  1133. SSL_set_connect_state(client.get());
  1134. SSL_set_accept_state(server.get());
  1135. SSL_set_session(client.get(), session);
  1136. BIO *bio1, *bio2;
  1137. if (!BIO_new_bio_pair(&bio1, 0, &bio2, 0)) {
  1138. return false;
  1139. }
  1140. // SSL_set_bio takes ownership.
  1141. SSL_set_bio(client.get(), bio1, bio1);
  1142. SSL_set_bio(server.get(), bio2, bio2);
  1143. if (!CompleteHandshakes(client.get(), server.get())) {
  1144. return false;
  1145. }
  1146. *out_client = std::move(client);
  1147. *out_server = std::move(server);
  1148. return true;
  1149. }
  1150. static bool TestSequenceNumber(bool is_dtls, const SSL_METHOD *method,
  1151. uint16_t version) {
  1152. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  1153. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  1154. if (!server_ctx || !client_ctx ||
  1155. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  1156. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  1157. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  1158. !SSL_CTX_set_max_proto_version(server_ctx.get(), version)) {
  1159. return false;
  1160. }
  1161. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1162. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1163. if (!cert || !key || !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  1164. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())) {
  1165. return false;
  1166. }
  1167. bssl::UniquePtr<SSL> client, server;
  1168. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  1169. server_ctx.get(), nullptr /* no session */)) {
  1170. return false;
  1171. }
  1172. // Drain any post-handshake messages to ensure there are no unread records
  1173. // on either end.
  1174. uint8_t byte = 0;
  1175. if (SSL_read(client.get(), &byte, 1) > 0 ||
  1176. SSL_read(server.get(), &byte, 1) > 0) {
  1177. fprintf(stderr, "Received unexpected data.\n");
  1178. return false;
  1179. }
  1180. uint64_t client_read_seq = SSL_get_read_sequence(client.get());
  1181. uint64_t client_write_seq = SSL_get_write_sequence(client.get());
  1182. uint64_t server_read_seq = SSL_get_read_sequence(server.get());
  1183. uint64_t server_write_seq = SSL_get_write_sequence(server.get());
  1184. if (is_dtls) {
  1185. // Both client and server must be at epoch 1.
  1186. if (EpochFromSequence(client_read_seq) != 1 ||
  1187. EpochFromSequence(client_write_seq) != 1 ||
  1188. EpochFromSequence(server_read_seq) != 1 ||
  1189. EpochFromSequence(server_write_seq) != 1) {
  1190. fprintf(stderr, "Bad epochs.\n");
  1191. return false;
  1192. }
  1193. // The next record to be written should exceed the largest received.
  1194. if (client_write_seq <= server_read_seq ||
  1195. server_write_seq <= client_read_seq) {
  1196. fprintf(stderr, "Inconsistent sequence numbers.\n");
  1197. return false;
  1198. }
  1199. } else {
  1200. // The next record to be written should equal the next to be received.
  1201. if (client_write_seq != server_read_seq ||
  1202. server_write_seq != client_read_seq) {
  1203. fprintf(stderr, "Inconsistent sequence numbers.\n");
  1204. return false;
  1205. }
  1206. }
  1207. // Send a record from client to server.
  1208. if (SSL_write(client.get(), &byte, 1) != 1 ||
  1209. SSL_read(server.get(), &byte, 1) != 1) {
  1210. fprintf(stderr, "Could not send byte.\n");
  1211. return false;
  1212. }
  1213. // The client write and server read sequence numbers should have
  1214. // incremented.
  1215. if (client_write_seq + 1 != SSL_get_write_sequence(client.get()) ||
  1216. server_read_seq + 1 != SSL_get_read_sequence(server.get())) {
  1217. fprintf(stderr, "Sequence numbers did not increment.\n");
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. static bool TestOneSidedShutdown(bool is_dtls, const SSL_METHOD *method,
  1223. uint16_t version) {
  1224. // SSL_shutdown is a no-op in DTLS.
  1225. if (is_dtls) {
  1226. return true;
  1227. }
  1228. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  1229. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  1230. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1231. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1232. if (!client_ctx || !server_ctx || !cert || !key ||
  1233. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  1234. !SSL_CTX_set_max_proto_version(server_ctx.get(), version) ||
  1235. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  1236. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  1237. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  1238. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())) {
  1239. return false;
  1240. }
  1241. bssl::UniquePtr<SSL> client, server;
  1242. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  1243. server_ctx.get(), nullptr /* no session */)) {
  1244. return false;
  1245. }
  1246. // Shut down half the connection. SSL_shutdown will return 0 to signal only
  1247. // one side has shut down.
  1248. if (SSL_shutdown(client.get()) != 0) {
  1249. fprintf(stderr, "Could not shutdown.\n");
  1250. return false;
  1251. }
  1252. // Reading from the server should consume the EOF.
  1253. uint8_t byte;
  1254. if (SSL_read(server.get(), &byte, 1) != 0 ||
  1255. SSL_get_error(server.get(), 0) != SSL_ERROR_ZERO_RETURN) {
  1256. fprintf(stderr, "Connection was not shut down cleanly.\n");
  1257. return false;
  1258. }
  1259. // However, the server may continue to write data and then shut down the
  1260. // connection.
  1261. byte = 42;
  1262. if (SSL_write(server.get(), &byte, 1) != 1 ||
  1263. SSL_read(client.get(), &byte, 1) != 1 ||
  1264. byte != 42) {
  1265. fprintf(stderr, "Could not send byte.\n");
  1266. return false;
  1267. }
  1268. // The server may then shutdown the connection.
  1269. if (SSL_shutdown(server.get()) != 1 ||
  1270. SSL_shutdown(client.get()) != 1) {
  1271. fprintf(stderr, "Could not complete shutdown.\n");
  1272. return false;
  1273. }
  1274. return true;
  1275. }
  1276. static bool TestSessionDuplication() {
  1277. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
  1278. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method()));
  1279. if (!client_ctx || !server_ctx) {
  1280. return false;
  1281. }
  1282. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1283. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1284. if (!cert || !key ||
  1285. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  1286. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())) {
  1287. return false;
  1288. }
  1289. bssl::UniquePtr<SSL> client, server;
  1290. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  1291. server_ctx.get(), nullptr /* no session */)) {
  1292. return false;
  1293. }
  1294. SSL_SESSION *session0 = SSL_get_session(client.get());
  1295. bssl::UniquePtr<SSL_SESSION> session1(SSL_SESSION_dup(session0, SSL_SESSION_DUP_ALL));
  1296. if (!session1) {
  1297. return false;
  1298. }
  1299. session1->not_resumable = 0;
  1300. uint8_t *s0_bytes, *s1_bytes;
  1301. size_t s0_len, s1_len;
  1302. if (!SSL_SESSION_to_bytes(session0, &s0_bytes, &s0_len)) {
  1303. return false;
  1304. }
  1305. bssl::UniquePtr<uint8_t> free_s0(s0_bytes);
  1306. if (!SSL_SESSION_to_bytes(session1.get(), &s1_bytes, &s1_len)) {
  1307. return false;
  1308. }
  1309. bssl::UniquePtr<uint8_t> free_s1(s1_bytes);
  1310. return s0_len == s1_len && memcmp(s0_bytes, s1_bytes, s0_len) == 0;
  1311. }
  1312. static bool ExpectFDs(const SSL *ssl, int rfd, int wfd) {
  1313. if (SSL_get_rfd(ssl) != rfd || SSL_get_wfd(ssl) != wfd) {
  1314. fprintf(stderr, "Got fds %d and %d, wanted %d and %d.\n", SSL_get_rfd(ssl),
  1315. SSL_get_wfd(ssl), rfd, wfd);
  1316. return false;
  1317. }
  1318. // The wrapper BIOs are always equal when fds are equal, even if set
  1319. // individually.
  1320. if (rfd == wfd && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  1321. fprintf(stderr, "rbio and wbio did not match.\n");
  1322. return false;
  1323. }
  1324. return true;
  1325. }
  1326. static bool TestSetFD() {
  1327. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  1328. if (!ctx) {
  1329. return false;
  1330. }
  1331. // Test setting different read and write FDs.
  1332. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  1333. if (!ssl ||
  1334. !SSL_set_rfd(ssl.get(), 1) ||
  1335. !SSL_set_wfd(ssl.get(), 2) ||
  1336. !ExpectFDs(ssl.get(), 1, 2)) {
  1337. return false;
  1338. }
  1339. // Test setting the same FD.
  1340. ssl.reset(SSL_new(ctx.get()));
  1341. if (!ssl ||
  1342. !SSL_set_fd(ssl.get(), 1) ||
  1343. !ExpectFDs(ssl.get(), 1, 1)) {
  1344. return false;
  1345. }
  1346. // Test setting the same FD one side at a time.
  1347. ssl.reset(SSL_new(ctx.get()));
  1348. if (!ssl ||
  1349. !SSL_set_rfd(ssl.get(), 1) ||
  1350. !SSL_set_wfd(ssl.get(), 1) ||
  1351. !ExpectFDs(ssl.get(), 1, 1)) {
  1352. return false;
  1353. }
  1354. // Test setting the same FD in the other order.
  1355. ssl.reset(SSL_new(ctx.get()));
  1356. if (!ssl ||
  1357. !SSL_set_wfd(ssl.get(), 1) ||
  1358. !SSL_set_rfd(ssl.get(), 1) ||
  1359. !ExpectFDs(ssl.get(), 1, 1)) {
  1360. return false;
  1361. }
  1362. // Test changing the read FD partway through.
  1363. ssl.reset(SSL_new(ctx.get()));
  1364. if (!ssl ||
  1365. !SSL_set_fd(ssl.get(), 1) ||
  1366. !SSL_set_rfd(ssl.get(), 2) ||
  1367. !ExpectFDs(ssl.get(), 2, 1)) {
  1368. return false;
  1369. }
  1370. // Test changing the write FD partway through.
  1371. ssl.reset(SSL_new(ctx.get()));
  1372. if (!ssl ||
  1373. !SSL_set_fd(ssl.get(), 1) ||
  1374. !SSL_set_wfd(ssl.get(), 2) ||
  1375. !ExpectFDs(ssl.get(), 1, 2)) {
  1376. return false;
  1377. }
  1378. // Test a no-op change to the read FD partway through.
  1379. ssl.reset(SSL_new(ctx.get()));
  1380. if (!ssl ||
  1381. !SSL_set_fd(ssl.get(), 1) ||
  1382. !SSL_set_rfd(ssl.get(), 1) ||
  1383. !ExpectFDs(ssl.get(), 1, 1)) {
  1384. return false;
  1385. }
  1386. // Test a no-op change to the write FD partway through.
  1387. ssl.reset(SSL_new(ctx.get()));
  1388. if (!ssl ||
  1389. !SSL_set_fd(ssl.get(), 1) ||
  1390. !SSL_set_wfd(ssl.get(), 1) ||
  1391. !ExpectFDs(ssl.get(), 1, 1)) {
  1392. return false;
  1393. }
  1394. // ASan builds will implicitly test that the internal |BIO| reference-counting
  1395. // is correct.
  1396. return true;
  1397. }
  1398. static bool TestSetBIO() {
  1399. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  1400. if (!ctx) {
  1401. return false;
  1402. }
  1403. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  1404. bssl::UniquePtr<BIO> bio1(BIO_new(BIO_s_mem())), bio2(BIO_new(BIO_s_mem())),
  1405. bio3(BIO_new(BIO_s_mem()));
  1406. if (!ssl || !bio1 || !bio2 || !bio3) {
  1407. return false;
  1408. }
  1409. // SSL_set_bio takes one reference when the parameters are the same.
  1410. BIO_up_ref(bio1.get());
  1411. SSL_set_bio(ssl.get(), bio1.get(), bio1.get());
  1412. // Repeating the call does nothing.
  1413. SSL_set_bio(ssl.get(), bio1.get(), bio1.get());
  1414. // It takes one reference each when the parameters are different.
  1415. BIO_up_ref(bio2.get());
  1416. BIO_up_ref(bio3.get());
  1417. SSL_set_bio(ssl.get(), bio2.get(), bio3.get());
  1418. // Repeating the call does nothing.
  1419. SSL_set_bio(ssl.get(), bio2.get(), bio3.get());
  1420. // It takes one reference when changing only wbio.
  1421. BIO_up_ref(bio1.get());
  1422. SSL_set_bio(ssl.get(), bio2.get(), bio1.get());
  1423. // It takes one reference when changing only rbio and the two are different.
  1424. BIO_up_ref(bio3.get());
  1425. SSL_set_bio(ssl.get(), bio3.get(), bio1.get());
  1426. // If setting wbio to rbio, it takes no additional references.
  1427. SSL_set_bio(ssl.get(), bio3.get(), bio3.get());
  1428. // From there, wbio may be switched to something else.
  1429. BIO_up_ref(bio1.get());
  1430. SSL_set_bio(ssl.get(), bio3.get(), bio1.get());
  1431. // If setting rbio to wbio, it takes no additional references.
  1432. SSL_set_bio(ssl.get(), bio1.get(), bio1.get());
  1433. // From there, rbio may be switched to something else, but, for historical
  1434. // reasons, it takes a reference to both parameters.
  1435. BIO_up_ref(bio1.get());
  1436. BIO_up_ref(bio2.get());
  1437. SSL_set_bio(ssl.get(), bio2.get(), bio1.get());
  1438. // ASAN builds will implicitly test that the internal |BIO| reference-counting
  1439. // is correct.
  1440. return true;
  1441. }
  1442. static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) { return 1; }
  1443. static bool TestGetPeerCertificate(bool is_dtls, const SSL_METHOD *method,
  1444. uint16_t version) {
  1445. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1446. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1447. if (!cert || !key) {
  1448. return false;
  1449. }
  1450. // Configure both client and server to accept any certificate.
  1451. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method));
  1452. if (!ctx ||
  1453. !SSL_CTX_use_certificate(ctx.get(), cert.get()) ||
  1454. !SSL_CTX_use_PrivateKey(ctx.get(), key.get()) ||
  1455. !SSL_CTX_set_min_proto_version(ctx.get(), version) ||
  1456. !SSL_CTX_set_max_proto_version(ctx.get(), version)) {
  1457. return false;
  1458. }
  1459. SSL_CTX_set_verify(
  1460. ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
  1461. SSL_CTX_set_cert_verify_callback(ctx.get(), VerifySucceed, NULL);
  1462. bssl::UniquePtr<SSL> client, server;
  1463. if (!ConnectClientAndServer(&client, &server, ctx.get(), ctx.get(),
  1464. nullptr /* no session */)) {
  1465. return false;
  1466. }
  1467. // Client and server should both see the leaf certificate.
  1468. bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(server.get()));
  1469. if (!peer || X509_cmp(cert.get(), peer.get()) != 0) {
  1470. fprintf(stderr, "Server peer certificate did not match.\n");
  1471. return false;
  1472. }
  1473. peer.reset(SSL_get_peer_certificate(client.get()));
  1474. if (!peer || X509_cmp(cert.get(), peer.get()) != 0) {
  1475. fprintf(stderr, "Client peer certificate did not match.\n");
  1476. return false;
  1477. }
  1478. // However, for historical reasons, the chain includes the leaf on the
  1479. // client, but does not on the server.
  1480. if (sk_X509_num(SSL_get_peer_cert_chain(client.get())) != 1) {
  1481. fprintf(stderr, "Client peer chain was incorrect.\n");
  1482. return false;
  1483. }
  1484. if (sk_X509_num(SSL_get_peer_cert_chain(server.get())) != 0) {
  1485. fprintf(stderr, "Server peer chain was incorrect.\n");
  1486. return false;
  1487. }
  1488. return true;
  1489. }
  1490. static bool TestRetainOnlySHA256OfCerts(bool is_dtls, const SSL_METHOD *method,
  1491. uint16_t version) {
  1492. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1493. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1494. if (!cert || !key) {
  1495. return false;
  1496. }
  1497. uint8_t *cert_der = NULL;
  1498. int cert_der_len = i2d_X509(cert.get(), &cert_der);
  1499. if (cert_der_len < 0) {
  1500. return false;
  1501. }
  1502. bssl::UniquePtr<uint8_t> free_cert_der(cert_der);
  1503. uint8_t cert_sha256[SHA256_DIGEST_LENGTH];
  1504. SHA256(cert_der, cert_der_len, cert_sha256);
  1505. // Configure both client and server to accept any certificate, but the
  1506. // server must retain only the SHA-256 of the peer.
  1507. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method));
  1508. if (!ctx ||
  1509. !SSL_CTX_use_certificate(ctx.get(), cert.get()) ||
  1510. !SSL_CTX_use_PrivateKey(ctx.get(), key.get()) ||
  1511. !SSL_CTX_set_min_proto_version(ctx.get(), version) ||
  1512. !SSL_CTX_set_max_proto_version(ctx.get(), version)) {
  1513. return false;
  1514. }
  1515. SSL_CTX_set_verify(
  1516. ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
  1517. SSL_CTX_set_cert_verify_callback(ctx.get(), VerifySucceed, NULL);
  1518. SSL_CTX_set_retain_only_sha256_of_client_certs(ctx.get(), 1);
  1519. bssl::UniquePtr<SSL> client, server;
  1520. if (!ConnectClientAndServer(&client, &server, ctx.get(), ctx.get(),
  1521. nullptr /* no session */)) {
  1522. return false;
  1523. }
  1524. // The peer certificate has been dropped.
  1525. bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(server.get()));
  1526. if (peer) {
  1527. fprintf(stderr, "Peer certificate was retained.\n");
  1528. return false;
  1529. }
  1530. SSL_SESSION *session = SSL_get_session(server.get());
  1531. if (!session->peer_sha256_valid) {
  1532. fprintf(stderr, "peer_sha256_valid was not set.\n");
  1533. return false;
  1534. }
  1535. if (memcmp(cert_sha256, session->peer_sha256, SHA256_DIGEST_LENGTH) != 0) {
  1536. fprintf(stderr, "peer_sha256 did not match.\n");
  1537. return false;
  1538. }
  1539. return true;
  1540. }
  1541. static bool ClientHelloMatches(uint16_t version, const uint8_t *expected,
  1542. size_t expected_len) {
  1543. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  1544. if (!ctx ||
  1545. !SSL_CTX_set_max_proto_version(ctx.get(), version) ||
  1546. // Our default cipher list varies by CPU capabilities, so manually place
  1547. // the ChaCha20 ciphers in front.
  1548. !SSL_CTX_set_cipher_list(ctx.get(), "CHACHA20:ALL")) {
  1549. return false;
  1550. }
  1551. bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
  1552. if (!ssl) {
  1553. return false;
  1554. }
  1555. std::vector<uint8_t> client_hello;
  1556. if (!GetClientHello(ssl.get(), &client_hello)) {
  1557. return false;
  1558. }
  1559. // Zero the client_random.
  1560. constexpr size_t kRandomOffset = 1 + 2 + 2 + // record header
  1561. 1 + 3 + // handshake message header
  1562. 2; // client_version
  1563. if (client_hello.size() < kRandomOffset + SSL3_RANDOM_SIZE) {
  1564. fprintf(stderr, "ClientHello for version %04x too short.\n", version);
  1565. return false;
  1566. }
  1567. memset(client_hello.data() + kRandomOffset, 0, SSL3_RANDOM_SIZE);
  1568. if (client_hello.size() != expected_len ||
  1569. memcmp(client_hello.data(), expected, expected_len) != 0) {
  1570. fprintf(stderr, "ClientHello for version %04x did not match:\n", version);
  1571. fprintf(stderr, "Got:\n\t");
  1572. for (size_t i = 0; i < client_hello.size(); i++) {
  1573. fprintf(stderr, "0x%02x, ", client_hello[i]);
  1574. }
  1575. fprintf(stderr, "\nWanted:\n\t");
  1576. for (size_t i = 0; i < expected_len; i++) {
  1577. fprintf(stderr, "0x%02x, ", expected[i]);
  1578. }
  1579. fprintf(stderr, "\n");
  1580. return false;
  1581. }
  1582. return true;
  1583. }
  1584. // Tests that our ClientHellos do not change unexpectedly.
  1585. static bool TestClientHello() {
  1586. static const uint8_t kSSL3ClientHello[] = {
  1587. 0x16,
  1588. 0x03, 0x00,
  1589. 0x00, 0x3f,
  1590. 0x01,
  1591. 0x00, 0x00, 0x3b,
  1592. 0x03, 0x00,
  1593. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1594. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1597. 0x00,
  1598. 0x00, 0x14,
  1599. 0xc0, 0x09,
  1600. 0xc0, 0x13,
  1601. 0x00, 0x33,
  1602. 0xc0, 0x0a,
  1603. 0xc0, 0x14,
  1604. 0x00, 0x39,
  1605. 0x00, 0x2f,
  1606. 0x00, 0x35,
  1607. 0x00, 0x0a,
  1608. 0x00, 0xff, 0x01, 0x00,
  1609. };
  1610. if (!ClientHelloMatches(SSL3_VERSION, kSSL3ClientHello,
  1611. sizeof(kSSL3ClientHello))) {
  1612. return false;
  1613. }
  1614. static const uint8_t kTLS1ClientHello[] = {
  1615. 0x16,
  1616. 0x03, 0x01,
  1617. 0x00, 0x5e,
  1618. 0x01,
  1619. 0x00, 0x00, 0x5a,
  1620. 0x03, 0x01,
  1621. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1622. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1623. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1624. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1625. 0x00,
  1626. 0x00, 0x12,
  1627. 0xc0, 0x09,
  1628. 0xc0, 0x13,
  1629. 0x00, 0x33,
  1630. 0xc0, 0x0a,
  1631. 0xc0, 0x14,
  1632. 0x00, 0x39,
  1633. 0x00, 0x2f,
  1634. 0x00, 0x35,
  1635. 0x00, 0x0a,
  1636. 0x01, 0x00, 0x00, 0x1f, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x17, 0x00,
  1637. 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00,
  1638. 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
  1639. };
  1640. if (!ClientHelloMatches(TLS1_VERSION, kTLS1ClientHello,
  1641. sizeof(kTLS1ClientHello))) {
  1642. return false;
  1643. }
  1644. static const uint8_t kTLS11ClientHello[] = {
  1645. 0x16,
  1646. 0x03, 0x01,
  1647. 0x00, 0x5e,
  1648. 0x01,
  1649. 0x00, 0x00, 0x5a,
  1650. 0x03, 0x02,
  1651. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1652. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1654. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1655. 0x00,
  1656. 0x00, 0x12,
  1657. 0xc0, 0x09,
  1658. 0xc0, 0x13,
  1659. 0x00, 0x33,
  1660. 0xc0, 0x0a,
  1661. 0xc0, 0x14,
  1662. 0x00, 0x39,
  1663. 0x00, 0x2f,
  1664. 0x00, 0x35,
  1665. 0x00, 0x0a,
  1666. 0x01, 0x00, 0x00, 0x1f, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x17, 0x00,
  1667. 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00,
  1668. 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
  1669. };
  1670. if (!ClientHelloMatches(TLS1_1_VERSION, kTLS11ClientHello,
  1671. sizeof(kTLS11ClientHello))) {
  1672. return false;
  1673. }
  1674. static const uint8_t kTLS12ClientHello[] = {
  1675. 0x16, 0x03, 0x01, 0x00, 0x9e, 0x01, 0x00, 0x00, 0x9a, 0x03, 0x03, 0x00,
  1676. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1677. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1678. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0xcc, 0xa9,
  1679. 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  1680. 0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xc0, 0x09, 0xc0, 0x23, 0xc0, 0x13,
  1681. 0xc0, 0x27, 0x00, 0x33, 0x00, 0x67, 0xc0, 0x0a, 0xc0, 0x24, 0xc0, 0x14,
  1682. 0xc0, 0x28, 0x00, 0x39, 0x00, 0x6b, 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f,
  1683. 0x00, 0x3c, 0x00, 0x35, 0x00, 0x3d, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x37,
  1684. 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x23, 0x00,
  1685. 0x00, 0x00, 0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04,
  1686. 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02,
  1687. 0x01, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00,
  1688. 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18,
  1689. };
  1690. if (!ClientHelloMatches(TLS1_2_VERSION, kTLS12ClientHello,
  1691. sizeof(kTLS12ClientHello))) {
  1692. return false;
  1693. }
  1694. // TODO(davidben): Add a change detector for TLS 1.3 once the spec and our
  1695. // implementation has settled enough that it won't change.
  1696. return true;
  1697. }
  1698. static bssl::UniquePtr<SSL_SESSION> g_last_session;
  1699. static int SaveLastSession(SSL *ssl, SSL_SESSION *session) {
  1700. // Save the most recent session.
  1701. g_last_session.reset(session);
  1702. return 1;
  1703. }
  1704. static bssl::UniquePtr<SSL_SESSION> CreateClientSession(SSL_CTX *client_ctx,
  1705. SSL_CTX *server_ctx) {
  1706. g_last_session = nullptr;
  1707. SSL_CTX_sess_set_new_cb(client_ctx, SaveLastSession);
  1708. // Connect client and server to get a session.
  1709. bssl::UniquePtr<SSL> client, server;
  1710. if (!ConnectClientAndServer(&client, &server, client_ctx, server_ctx,
  1711. nullptr /* no session */)) {
  1712. fprintf(stderr, "Failed to connect client and server.\n");
  1713. return nullptr;
  1714. }
  1715. // Run the read loop to account for post-handshake tickets in TLS 1.3.
  1716. SSL_read(client.get(), nullptr, 0);
  1717. SSL_CTX_sess_set_new_cb(client_ctx, nullptr);
  1718. if (!g_last_session) {
  1719. fprintf(stderr, "Client did not receive a session.\n");
  1720. return nullptr;
  1721. }
  1722. return std::move(g_last_session);
  1723. }
  1724. static bool ExpectSessionReused(SSL_CTX *client_ctx, SSL_CTX *server_ctx,
  1725. SSL_SESSION *session,
  1726. bool reused) {
  1727. bssl::UniquePtr<SSL> client, server;
  1728. if (!ConnectClientAndServer(&client, &server, client_ctx,
  1729. server_ctx, session)) {
  1730. fprintf(stderr, "Failed to connect client and server.\n");
  1731. return false;
  1732. }
  1733. if (SSL_session_reused(client.get()) != SSL_session_reused(server.get())) {
  1734. fprintf(stderr, "Client and server were inconsistent.\n");
  1735. return false;
  1736. }
  1737. bool was_reused = !!SSL_session_reused(client.get());
  1738. if (was_reused != reused) {
  1739. fprintf(stderr, "Session was%s reused, but we expected the opposite.\n",
  1740. was_reused ? "" : " not");
  1741. return false;
  1742. }
  1743. return true;
  1744. }
  1745. static bssl::UniquePtr<SSL_SESSION> ExpectSessionRenewed(SSL_CTX *client_ctx,
  1746. SSL_CTX *server_ctx,
  1747. SSL_SESSION *session) {
  1748. g_last_session = nullptr;
  1749. SSL_CTX_sess_set_new_cb(client_ctx, SaveLastSession);
  1750. bssl::UniquePtr<SSL> client, server;
  1751. if (!ConnectClientAndServer(&client, &server, client_ctx,
  1752. server_ctx, session)) {
  1753. fprintf(stderr, "Failed to connect client and server.\n");
  1754. return nullptr;
  1755. }
  1756. if (SSL_session_reused(client.get()) != SSL_session_reused(server.get())) {
  1757. fprintf(stderr, "Client and server were inconsistent.\n");
  1758. return nullptr;
  1759. }
  1760. if (!SSL_session_reused(client.get())) {
  1761. fprintf(stderr, "Session was not reused.\n");
  1762. return nullptr;
  1763. }
  1764. // Run the read loop to account for post-handshake tickets in TLS 1.3.
  1765. SSL_read(client.get(), nullptr, 0);
  1766. SSL_CTX_sess_set_new_cb(client_ctx, nullptr);
  1767. if (!g_last_session) {
  1768. fprintf(stderr, "Client did not receive a renewed session.\n");
  1769. return nullptr;
  1770. }
  1771. return std::move(g_last_session);
  1772. }
  1773. static int SwitchSessionIDContextSNI(SSL *ssl, int *out_alert, void *arg) {
  1774. static const uint8_t kContext[] = {3};
  1775. if (!SSL_set_session_id_context(ssl, kContext, sizeof(kContext))) {
  1776. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1777. }
  1778. return SSL_TLSEXT_ERR_OK;
  1779. }
  1780. static int SwitchSessionIDContextEarly(const SSL_CLIENT_HELLO *client_hello) {
  1781. static const uint8_t kContext[] = {3};
  1782. if (!SSL_set_session_id_context(client_hello->ssl, kContext,
  1783. sizeof(kContext))) {
  1784. return -1;
  1785. }
  1786. return 1;
  1787. }
  1788. static bool TestSessionIDContext(bool is_dtls, const SSL_METHOD *method,
  1789. uint16_t version) {
  1790. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1791. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1792. if (!cert || !key) {
  1793. return false;
  1794. }
  1795. static const uint8_t kContext1[] = {1};
  1796. static const uint8_t kContext2[] = {2};
  1797. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  1798. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  1799. if (!server_ctx || !client_ctx ||
  1800. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  1801. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  1802. !SSL_CTX_set_session_id_context(server_ctx.get(), kContext1,
  1803. sizeof(kContext1)) ||
  1804. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  1805. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  1806. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  1807. !SSL_CTX_set_max_proto_version(server_ctx.get(), version)) {
  1808. return false;
  1809. }
  1810. SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH);
  1811. SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH);
  1812. bssl::UniquePtr<SSL_SESSION> session =
  1813. CreateClientSession(client_ctx.get(), server_ctx.get());
  1814. if (!session) {
  1815. fprintf(stderr, "Error getting session.\n");
  1816. return false;
  1817. }
  1818. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1819. true /* expect session reused */)) {
  1820. fprintf(stderr, "Error resuming session.\n");
  1821. return false;
  1822. }
  1823. // Change the session ID context.
  1824. if (!SSL_CTX_set_session_id_context(server_ctx.get(), kContext2,
  1825. sizeof(kContext2))) {
  1826. return false;
  1827. }
  1828. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1829. false /* expect session not reused */)) {
  1830. fprintf(stderr, "Error connecting with a different context.\n");
  1831. return false;
  1832. }
  1833. // Change the session ID context back and install an SNI callback to switch
  1834. // it.
  1835. if (!SSL_CTX_set_session_id_context(server_ctx.get(), kContext1,
  1836. sizeof(kContext1))) {
  1837. return false;
  1838. }
  1839. SSL_CTX_set_tlsext_servername_callback(server_ctx.get(),
  1840. SwitchSessionIDContextSNI);
  1841. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1842. false /* expect session not reused */)) {
  1843. fprintf(stderr, "Error connecting with a context switch on SNI callback.\n");
  1844. return false;
  1845. }
  1846. // Switch the session ID context with the early callback instead.
  1847. SSL_CTX_set_tlsext_servername_callback(server_ctx.get(), nullptr);
  1848. SSL_CTX_set_select_certificate_cb(server_ctx.get(),
  1849. SwitchSessionIDContextEarly);
  1850. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1851. false /* expect session not reused */)) {
  1852. fprintf(stderr,
  1853. "Error connecting with a context switch on early callback.\n");
  1854. return false;
  1855. }
  1856. return true;
  1857. }
  1858. static timeval g_current_time;
  1859. static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
  1860. *out_clock = g_current_time;
  1861. }
  1862. static int RenewTicketCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1863. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1864. int encrypt) {
  1865. static const uint8_t kZeros[16] = {0};
  1866. if (encrypt) {
  1867. memcpy(key_name, kZeros, sizeof(kZeros));
  1868. RAND_bytes(iv, 16);
  1869. } else if (memcmp(key_name, kZeros, 16) != 0) {
  1870. return 0;
  1871. }
  1872. if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
  1873. !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
  1874. return -1;
  1875. }
  1876. // Returning two from the callback in decrypt mode renews the
  1877. // session in TLS 1.2 and below.
  1878. return encrypt ? 1 : 2;
  1879. }
  1880. static bool GetServerTicketTime(long *out, const SSL_SESSION *session) {
  1881. if (session->tlsext_ticklen < 16 + 16 + SHA256_DIGEST_LENGTH) {
  1882. return false;
  1883. }
  1884. const uint8_t *ciphertext = session->tlsext_tick + 16 + 16;
  1885. size_t len = session->tlsext_ticklen - 16 - 16 - SHA256_DIGEST_LENGTH;
  1886. std::unique_ptr<uint8_t[]> plaintext(new uint8_t[len]);
  1887. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1888. // Fuzzer-mode tickets are unencrypted.
  1889. memcpy(plaintext.get(), ciphertext, len);
  1890. #else
  1891. static const uint8_t kZeros[16] = {0};
  1892. const uint8_t *iv = session->tlsext_tick + 16;
  1893. bssl::ScopedEVP_CIPHER_CTX ctx;
  1894. int len1, len2;
  1895. if (!EVP_DecryptInit_ex(ctx.get(), EVP_aes_128_cbc(), nullptr, kZeros, iv) ||
  1896. !EVP_DecryptUpdate(ctx.get(), plaintext.get(), &len1, ciphertext, len) ||
  1897. !EVP_DecryptFinal_ex(ctx.get(), plaintext.get() + len1, &len2)) {
  1898. return false;
  1899. }
  1900. len = static_cast<size_t>(len1 + len2);
  1901. #endif
  1902. bssl::UniquePtr<SSL_SESSION> server_session(
  1903. SSL_SESSION_from_bytes(plaintext.get(), len));
  1904. if (!server_session) {
  1905. return false;
  1906. }
  1907. *out = server_session->time;
  1908. return true;
  1909. }
  1910. static bool TestSessionTimeout(bool is_dtls, const SSL_METHOD *method,
  1911. uint16_t version) {
  1912. bssl::UniquePtr<X509> cert = GetTestCertificate();
  1913. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  1914. if (!cert || !key) {
  1915. return false;
  1916. }
  1917. for (bool server_test : std::vector<bool>{false, true}) {
  1918. static const int kStartTime = 1000;
  1919. g_current_time.tv_sec = kStartTime;
  1920. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  1921. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  1922. if (!server_ctx || !client_ctx ||
  1923. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  1924. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  1925. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  1926. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  1927. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  1928. !SSL_CTX_set_max_proto_version(server_ctx.get(), version)) {
  1929. return false;
  1930. }
  1931. SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH);
  1932. SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH);
  1933. // Both client and server must enforce session timeouts.
  1934. if (server_test) {
  1935. SSL_CTX_set_current_time_cb(server_ctx.get(), CurrentTimeCallback);
  1936. } else {
  1937. SSL_CTX_set_current_time_cb(client_ctx.get(), CurrentTimeCallback);
  1938. }
  1939. // Configure a ticket callback which renews tickets.
  1940. SSL_CTX_set_tlsext_ticket_key_cb(server_ctx.get(), RenewTicketCallback);
  1941. bssl::UniquePtr<SSL_SESSION> session =
  1942. CreateClientSession(client_ctx.get(), server_ctx.get());
  1943. if (!session) {
  1944. fprintf(stderr, "Error getting session.\n");
  1945. return false;
  1946. }
  1947. // Advance the clock just behind the timeout.
  1948. g_current_time.tv_sec += SSL_DEFAULT_SESSION_TIMEOUT - 1;
  1949. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1950. true /* expect session reused */)) {
  1951. fprintf(stderr, "Error resuming session.\n");
  1952. return false;
  1953. }
  1954. // Advance the clock one more second.
  1955. g_current_time.tv_sec++;
  1956. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1957. false /* expect session not reused */)) {
  1958. fprintf(stderr, "Error resuming session.\n");
  1959. return false;
  1960. }
  1961. // Rewind the clock to before the session was minted.
  1962. g_current_time.tv_sec = kStartTime - 1;
  1963. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  1964. false /* expect session not reused */)) {
  1965. fprintf(stderr, "Error resuming session.\n");
  1966. return false;
  1967. }
  1968. // SSL 3.0 cannot renew sessions.
  1969. if (version == SSL3_VERSION) {
  1970. continue;
  1971. }
  1972. // Renew the session 10 seconds before expiration.
  1973. g_current_time.tv_sec = kStartTime + SSL_DEFAULT_SESSION_TIMEOUT - 10;
  1974. bssl::UniquePtr<SSL_SESSION> new_session =
  1975. ExpectSessionRenewed(client_ctx.get(), server_ctx.get(), session.get());
  1976. if (!new_session) {
  1977. fprintf(stderr, "Error renewing session.\n");
  1978. return false;
  1979. }
  1980. // This new session is not the same object as before.
  1981. if (session.get() == new_session.get()) {
  1982. fprintf(stderr, "New and old sessions alias.\n");
  1983. return false;
  1984. }
  1985. // Check the sessions have timestamps measured from issuance.
  1986. long session_time = 0;
  1987. if (server_test) {
  1988. if (!GetServerTicketTime(&session_time, new_session.get())) {
  1989. fprintf(stderr, "Failed to decode session ticket.\n");
  1990. return false;
  1991. }
  1992. } else {
  1993. session_time = new_session->time;
  1994. }
  1995. if (session_time != g_current_time.tv_sec) {
  1996. fprintf(stderr, "New session is not measured from issuance.\n");
  1997. return false;
  1998. }
  1999. // The new session is usable just before the old expiration.
  2000. g_current_time.tv_sec = kStartTime + SSL_DEFAULT_SESSION_TIMEOUT - 1;
  2001. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2002. new_session.get(),
  2003. true /* expect session reused */)) {
  2004. fprintf(stderr, "Error resuming renewed session.\n");
  2005. return false;
  2006. }
  2007. // Renewal does not extend the lifetime, so it is not usable beyond the
  2008. // old expiration.
  2009. g_current_time.tv_sec = kStartTime + SSL_DEFAULT_SESSION_TIMEOUT + 1;
  2010. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2011. new_session.get(),
  2012. false /* expect session not reused */)) {
  2013. fprintf(stderr, "Renewed session's lifetime is too long.\n");
  2014. return false;
  2015. }
  2016. }
  2017. return true;
  2018. }
  2019. static int SetSessionTimeoutCallback(SSL *ssl, void *arg) {
  2020. long timeout = *(long *) arg;
  2021. SSL_set_session_timeout(ssl, timeout);
  2022. return 1;
  2023. }
  2024. static bool TestSessionTimeoutCertCallback(bool is_dtls,
  2025. const SSL_METHOD *method,
  2026. uint16_t version) {
  2027. static const int kStartTime = 1000;
  2028. g_current_time.tv_sec = kStartTime;
  2029. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2030. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2031. if (!cert || !key) {
  2032. return false;
  2033. }
  2034. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  2035. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  2036. if (!server_ctx || !client_ctx ||
  2037. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  2038. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  2039. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  2040. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  2041. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  2042. !SSL_CTX_set_max_proto_version(server_ctx.get(), version)) {
  2043. return false;
  2044. }
  2045. SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH);
  2046. SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH);
  2047. SSL_CTX_set_current_time_cb(server_ctx.get(), CurrentTimeCallback);
  2048. long timeout = 25;
  2049. SSL_CTX_set_cert_cb(server_ctx.get(), SetSessionTimeoutCallback, &timeout);
  2050. bssl::UniquePtr<SSL_SESSION> session =
  2051. CreateClientSession(client_ctx.get(), server_ctx.get());
  2052. if (!session) {
  2053. fprintf(stderr, "Error getting session.\n");
  2054. return false;
  2055. }
  2056. // Advance the clock just behind the timeout.
  2057. g_current_time.tv_sec += timeout - 1;
  2058. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  2059. true /* expect session reused */)) {
  2060. fprintf(stderr, "Error resuming session.\n");
  2061. return false;
  2062. }
  2063. // Advance the clock one more second.
  2064. g_current_time.tv_sec++;
  2065. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(), session.get(),
  2066. false /* expect session not reused */)) {
  2067. fprintf(stderr, "Error resuming session.\n");
  2068. return false;
  2069. }
  2070. // Set session timeout to 0 to disable resumption.
  2071. timeout = 0;
  2072. g_current_time.tv_sec = kStartTime;
  2073. bssl::UniquePtr<SSL_SESSION> not_resumable_session =
  2074. CreateClientSession(client_ctx.get(), server_ctx.get());
  2075. if (!not_resumable_session) {
  2076. fprintf(stderr, "Error getting session.\n");
  2077. return false;
  2078. }
  2079. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2080. not_resumable_session.get(),
  2081. false /* expect session not reused */)) {
  2082. fprintf(stderr, "Error resuming session with timeout of 0.\n");
  2083. return false;
  2084. }
  2085. // Set both context and connection (via callback) default session timeout.
  2086. // The connection one is the one that ends up being used.
  2087. timeout = 25;
  2088. g_current_time.tv_sec = kStartTime;
  2089. SSL_CTX_set_timeout(server_ctx.get(), timeout - 10);
  2090. bssl::UniquePtr<SSL_SESSION> ctx_and_cb_session =
  2091. CreateClientSession(client_ctx.get(), server_ctx.get());
  2092. if (!ctx_and_cb_session) {
  2093. fprintf(stderr, "Error getting session.\n");
  2094. return false;
  2095. }
  2096. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2097. ctx_and_cb_session.get(),
  2098. true /* expect session reused */)) {
  2099. fprintf(stderr, "Error resuming session with timeout of 0.\n");
  2100. return false;
  2101. }
  2102. // Advance the clock just behind the timeout.
  2103. g_current_time.tv_sec += timeout - 1;
  2104. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2105. ctx_and_cb_session.get(),
  2106. true /* expect session reused */)) {
  2107. fprintf(stderr, "Error resuming session.\n");
  2108. return false;
  2109. }
  2110. // Advance the clock one more second.
  2111. g_current_time.tv_sec++;
  2112. if (!ExpectSessionReused(client_ctx.get(), server_ctx.get(),
  2113. ctx_and_cb_session.get(),
  2114. false /* expect session not reused */)) {
  2115. fprintf(stderr, "Error resuming session.\n");
  2116. return false;
  2117. }
  2118. return true;
  2119. }
  2120. static int SwitchContext(SSL *ssl, int *out_alert, void *arg) {
  2121. SSL_CTX *ctx = reinterpret_cast<SSL_CTX*>(arg);
  2122. SSL_set_SSL_CTX(ssl, ctx);
  2123. return SSL_TLSEXT_ERR_OK;
  2124. }
  2125. static bool TestSNICallback(bool is_dtls, const SSL_METHOD *method,
  2126. uint16_t version) {
  2127. // SSL 3.0 lacks extensions.
  2128. if (version == SSL3_VERSION) {
  2129. return true;
  2130. }
  2131. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2132. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2133. bssl::UniquePtr<X509> cert2 = GetECDSATestCertificate();
  2134. bssl::UniquePtr<EVP_PKEY> key2 = GetECDSATestKey();
  2135. if (!cert || !key || !cert2 || !key2) {
  2136. return false;
  2137. }
  2138. // Test that switching the |SSL_CTX| at the SNI callback behaves correctly.
  2139. static const uint16_t kECDSAWithSHA256 = SSL_SIGN_ECDSA_SECP256R1_SHA256;
  2140. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  2141. bssl::UniquePtr<SSL_CTX> server_ctx2(SSL_CTX_new(method));
  2142. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  2143. if (!server_ctx || !server_ctx2 || !client_ctx ||
  2144. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  2145. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  2146. !SSL_CTX_use_certificate(server_ctx2.get(), cert2.get()) ||
  2147. !SSL_CTX_use_PrivateKey(server_ctx2.get(), key2.get()) ||
  2148. // Historically signing preferences would be lost in some cases with the
  2149. // SNI callback, which triggers the TLS 1.2 SHA-1 default. To ensure
  2150. // this doesn't happen when |version| is TLS 1.2, configure the private
  2151. // key to only sign SHA-256.
  2152. !SSL_CTX_set_signing_algorithm_prefs(server_ctx2.get(), &kECDSAWithSHA256,
  2153. 1) ||
  2154. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  2155. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  2156. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  2157. !SSL_CTX_set_max_proto_version(server_ctx.get(), version) ||
  2158. !SSL_CTX_set_min_proto_version(server_ctx2.get(), version) ||
  2159. !SSL_CTX_set_max_proto_version(server_ctx2.get(), version)) {
  2160. return false;
  2161. }
  2162. SSL_CTX_set_tlsext_servername_callback(server_ctx.get(), SwitchContext);
  2163. SSL_CTX_set_tlsext_servername_arg(server_ctx.get(), server_ctx2.get());
  2164. bssl::UniquePtr<SSL> client, server;
  2165. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  2166. server_ctx.get(), nullptr)) {
  2167. fprintf(stderr, "Handshake failed.\n");
  2168. return false;
  2169. }
  2170. // The client should have received |cert2|.
  2171. bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(client.get()));
  2172. if (!peer || X509_cmp(peer.get(), cert2.get()) != 0) {
  2173. fprintf(stderr, "Incorrect certificate received.\n");
  2174. return false;
  2175. }
  2176. return true;
  2177. }
  2178. static int SetMaxVersion(const SSL_CLIENT_HELLO *client_hello) {
  2179. if (!SSL_set_max_proto_version(client_hello->ssl, TLS1_2_VERSION)) {
  2180. return -1;
  2181. }
  2182. return 1;
  2183. }
  2184. // TestEarlyCallbackVersionSwitch tests that the early callback can swap the
  2185. // maximum version.
  2186. static bool TestEarlyCallbackVersionSwitch() {
  2187. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2188. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2189. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method()));
  2190. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method()));
  2191. if (!cert || !key || !server_ctx || !client_ctx ||
  2192. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  2193. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  2194. !SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION) ||
  2195. !SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)) {
  2196. return false;
  2197. }
  2198. SSL_CTX_set_select_certificate_cb(server_ctx.get(), SetMaxVersion);
  2199. bssl::UniquePtr<SSL> client, server;
  2200. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  2201. server_ctx.get(), nullptr)) {
  2202. return false;
  2203. }
  2204. if (SSL_version(client.get()) != TLS1_2_VERSION) {
  2205. fprintf(stderr, "Early callback failed to switch the maximum version.\n");
  2206. return false;
  2207. }
  2208. return true;
  2209. }
  2210. static bool TestSetVersion() {
  2211. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  2212. if (!ctx) {
  2213. return false;
  2214. }
  2215. if (!SSL_CTX_set_max_proto_version(ctx.get(), TLS1_VERSION) ||
  2216. !SSL_CTX_set_max_proto_version(ctx.get(), TLS1_1_VERSION) ||
  2217. !SSL_CTX_set_min_proto_version(ctx.get(), TLS1_VERSION) ||
  2218. !SSL_CTX_set_min_proto_version(ctx.get(), TLS1_1_VERSION)) {
  2219. fprintf(stderr, "Could not set valid TLS version.\n");
  2220. return false;
  2221. }
  2222. if (SSL_CTX_set_max_proto_version(ctx.get(), DTLS1_VERSION) ||
  2223. SSL_CTX_set_max_proto_version(ctx.get(), 0x0200) ||
  2224. SSL_CTX_set_max_proto_version(ctx.get(), 0x1234) ||
  2225. SSL_CTX_set_min_proto_version(ctx.get(), DTLS1_VERSION) ||
  2226. SSL_CTX_set_min_proto_version(ctx.get(), 0x0200) ||
  2227. SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)) {
  2228. fprintf(stderr, "Unexpectedly set invalid TLS version.\n");
  2229. return false;
  2230. }
  2231. if (!SSL_CTX_set_max_proto_version(ctx.get(), 0) ||
  2232. !SSL_CTX_set_min_proto_version(ctx.get(), 0)) {
  2233. fprintf(stderr, "Could not set default TLS version.\n");
  2234. return false;
  2235. }
  2236. if (ctx->min_version != SSL3_VERSION ||
  2237. ctx->max_version != TLS1_2_VERSION) {
  2238. fprintf(stderr, "Default TLS versions were incorrect (%04x and %04x).\n",
  2239. ctx->min_version, ctx->max_version);
  2240. return false;
  2241. }
  2242. ctx.reset(SSL_CTX_new(DTLS_method()));
  2243. if (!ctx) {
  2244. return false;
  2245. }
  2246. if (!SSL_CTX_set_max_proto_version(ctx.get(), DTLS1_VERSION) ||
  2247. !SSL_CTX_set_max_proto_version(ctx.get(), DTLS1_2_VERSION) ||
  2248. !SSL_CTX_set_min_proto_version(ctx.get(), DTLS1_VERSION) ||
  2249. !SSL_CTX_set_min_proto_version(ctx.get(), DTLS1_2_VERSION)) {
  2250. fprintf(stderr, "Could not set valid DTLS version.\n");
  2251. return false;
  2252. }
  2253. if (SSL_CTX_set_max_proto_version(ctx.get(), TLS1_VERSION) ||
  2254. SSL_CTX_set_max_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */) ||
  2255. SSL_CTX_set_max_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */) ||
  2256. SSL_CTX_set_max_proto_version(ctx.get(), 0x1234) ||
  2257. SSL_CTX_set_min_proto_version(ctx.get(), TLS1_VERSION) ||
  2258. SSL_CTX_set_min_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */) ||
  2259. SSL_CTX_set_min_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */) ||
  2260. SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)) {
  2261. fprintf(stderr, "Unexpectedly set invalid DTLS version.\n");
  2262. return false;
  2263. }
  2264. if (!SSL_CTX_set_max_proto_version(ctx.get(), 0) ||
  2265. !SSL_CTX_set_min_proto_version(ctx.get(), 0)) {
  2266. fprintf(stderr, "Could not set default DTLS version.\n");
  2267. return false;
  2268. }
  2269. if (ctx->min_version != TLS1_1_VERSION ||
  2270. ctx->max_version != TLS1_2_VERSION) {
  2271. fprintf(stderr, "Default DTLS versions were incorrect (%04x and %04x).\n",
  2272. ctx->min_version, ctx->max_version);
  2273. return false;
  2274. }
  2275. return true;
  2276. }
  2277. static bool TestVersion(bool is_dtls, const SSL_METHOD *method,
  2278. uint16_t version) {
  2279. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2280. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2281. if (!cert || !key) {
  2282. return false;
  2283. }
  2284. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  2285. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  2286. bssl::UniquePtr<SSL> client, server;
  2287. if (!server_ctx || !client_ctx ||
  2288. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  2289. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  2290. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  2291. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  2292. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  2293. !SSL_CTX_set_max_proto_version(server_ctx.get(), version) ||
  2294. !ConnectClientAndServer(&client, &server, client_ctx.get(),
  2295. server_ctx.get(), nullptr /* no session */)) {
  2296. fprintf(stderr, "Failed to connect.\n");
  2297. return false;
  2298. }
  2299. if (SSL_version(client.get()) != version ||
  2300. SSL_version(server.get()) != version) {
  2301. fprintf(stderr, "Version mismatch. Got %04x and %04x, wanted %04x.\n",
  2302. SSL_version(client.get()), SSL_version(server.get()), version);
  2303. return false;
  2304. }
  2305. return true;
  2306. }
  2307. // Tests that that |SSL_get_pending_cipher| is available during the ALPN
  2308. // selection callback.
  2309. static bool TestALPNCipherAvailable(bool is_dtls, const SSL_METHOD *method,
  2310. uint16_t version) {
  2311. // SSL 3.0 lacks extensions.
  2312. if (version == SSL3_VERSION) {
  2313. return true;
  2314. }
  2315. static const uint8_t kALPNProtos[] = {0x03, 'f', 'o', 'o'};
  2316. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2317. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2318. if (!cert || !key) {
  2319. return false;
  2320. }
  2321. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method));
  2322. if (!ctx || !SSL_CTX_use_certificate(ctx.get(), cert.get()) ||
  2323. !SSL_CTX_use_PrivateKey(ctx.get(), key.get()) ||
  2324. !SSL_CTX_set_min_proto_version(ctx.get(), version) ||
  2325. !SSL_CTX_set_max_proto_version(ctx.get(), version) ||
  2326. SSL_CTX_set_alpn_protos(ctx.get(), kALPNProtos, sizeof(kALPNProtos)) !=
  2327. 0) {
  2328. return false;
  2329. }
  2330. // The ALPN callback does not fail the handshake on error, so have the
  2331. // callback write a boolean.
  2332. std::pair<uint16_t, bool> callback_state(version, false);
  2333. SSL_CTX_set_alpn_select_cb(
  2334. ctx.get(),
  2335. [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in,
  2336. unsigned in_len, void *arg) -> int {
  2337. auto state = reinterpret_cast<std::pair<uint16_t, bool> *>(arg);
  2338. if (SSL_get_pending_cipher(ssl) != nullptr &&
  2339. SSL_version(ssl) == state->first) {
  2340. state->second = true;
  2341. }
  2342. return SSL_TLSEXT_ERR_NOACK;
  2343. },
  2344. &callback_state);
  2345. bssl::UniquePtr<SSL> client, server;
  2346. if (!ConnectClientAndServer(&client, &server, ctx.get(), ctx.get(),
  2347. nullptr /* no session */)) {
  2348. return false;
  2349. }
  2350. if (!callback_state.second) {
  2351. fprintf(stderr, "The pending cipher was not known in the ALPN callback.\n");
  2352. return false;
  2353. }
  2354. return true;
  2355. }
  2356. static bool TestSSLClearSessionResumption(bool is_dtls,
  2357. const SSL_METHOD *method,
  2358. uint16_t version) {
  2359. // Skip this for TLS 1.3. TLS 1.3's ticket mechanism is incompatible with this
  2360. // API pattern.
  2361. if (version == TLS1_3_VERSION) {
  2362. return true;
  2363. }
  2364. bssl::UniquePtr<X509> cert = GetTestCertificate();
  2365. bssl::UniquePtr<EVP_PKEY> key = GetTestKey();
  2366. bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(method));
  2367. bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(method));
  2368. if (!cert || !key || !server_ctx || !client_ctx ||
  2369. !SSL_CTX_use_certificate(server_ctx.get(), cert.get()) ||
  2370. !SSL_CTX_use_PrivateKey(server_ctx.get(), key.get()) ||
  2371. !SSL_CTX_set_min_proto_version(client_ctx.get(), version) ||
  2372. !SSL_CTX_set_max_proto_version(client_ctx.get(), version) ||
  2373. !SSL_CTX_set_min_proto_version(server_ctx.get(), version) ||
  2374. !SSL_CTX_set_max_proto_version(server_ctx.get(), version)) {
  2375. return false;
  2376. }
  2377. // Connect a client and a server.
  2378. bssl::UniquePtr<SSL> client, server;
  2379. if (!ConnectClientAndServer(&client, &server, client_ctx.get(),
  2380. server_ctx.get(), nullptr /* no session */)) {
  2381. return false;
  2382. }
  2383. if (SSL_session_reused(client.get()) ||
  2384. SSL_session_reused(server.get())) {
  2385. fprintf(stderr, "Session unexpectedly reused.\n");
  2386. return false;
  2387. }
  2388. // Reset everything.
  2389. if (!SSL_clear(client.get()) ||
  2390. !SSL_clear(server.get())) {
  2391. fprintf(stderr, "SSL_clear failed.\n");
  2392. return false;
  2393. }
  2394. // Attempt to connect a second time.
  2395. if (!CompleteHandshakes(client.get(), server.get())) {
  2396. fprintf(stderr, "Could not reuse SSL objects.\n");
  2397. return false;
  2398. }
  2399. // |SSL_clear| should implicitly offer the previous session to the server.
  2400. if (!SSL_session_reused(client.get()) ||
  2401. !SSL_session_reused(server.get())) {
  2402. fprintf(stderr, "Session was not reused in second try.\n");
  2403. return false;
  2404. }
  2405. return true;
  2406. }
  2407. static bool ForEachVersion(bool (*test_func)(bool is_dtls,
  2408. const SSL_METHOD *method,
  2409. uint16_t version)) {
  2410. static uint16_t kTLSVersions[] = {
  2411. SSL3_VERSION, TLS1_VERSION, TLS1_1_VERSION,
  2412. TLS1_2_VERSION, TLS1_3_VERSION,
  2413. };
  2414. static uint16_t kDTLSVersions[] = {
  2415. DTLS1_VERSION, DTLS1_2_VERSION,
  2416. };
  2417. for (uint16_t version : kTLSVersions) {
  2418. if (!test_func(false, TLS_method(), version)) {
  2419. fprintf(stderr, "Test failed at TLS version %04x.\n", version);
  2420. return false;
  2421. }
  2422. }
  2423. for (uint16_t version : kDTLSVersions) {
  2424. if (!test_func(true, DTLS_method(), version)) {
  2425. fprintf(stderr, "Test failed at DTLS version %04x.\n", version);
  2426. return false;
  2427. }
  2428. }
  2429. return true;
  2430. }
  2431. int main() {
  2432. CRYPTO_library_init();
  2433. if (!TestCipherRules() ||
  2434. !TestCurveRules() ||
  2435. !TestSSL_SESSIONEncoding(kOpenSSLSession) ||
  2436. !TestSSL_SESSIONEncoding(kCustomSession) ||
  2437. !TestSSL_SESSIONEncoding(kBoringSSLSession) ||
  2438. !TestBadSSL_SESSIONEncoding(kBadSessionExtraField) ||
  2439. !TestBadSSL_SESSIONEncoding(kBadSessionVersion) ||
  2440. !TestBadSSL_SESSIONEncoding(kBadSessionTrailingData) ||
  2441. // TODO(svaldez): Update this when TLS 1.3 is enabled by default.
  2442. !TestDefaultVersion(SSL3_VERSION, TLS1_2_VERSION, &TLS_method) ||
  2443. !TestDefaultVersion(SSL3_VERSION, SSL3_VERSION, &SSLv3_method) ||
  2444. !TestDefaultVersion(TLS1_VERSION, TLS1_VERSION, &TLSv1_method) ||
  2445. !TestDefaultVersion(TLS1_1_VERSION, TLS1_1_VERSION, &TLSv1_1_method) ||
  2446. !TestDefaultVersion(TLS1_2_VERSION, TLS1_2_VERSION, &TLSv1_2_method) ||
  2447. !TestDefaultVersion(TLS1_1_VERSION, TLS1_2_VERSION, &DTLS_method) ||
  2448. !TestDefaultVersion(TLS1_1_VERSION, TLS1_1_VERSION, &DTLSv1_method) ||
  2449. !TestDefaultVersion(TLS1_2_VERSION, TLS1_2_VERSION, &DTLSv1_2_method) ||
  2450. !TestCipherGetRFCName() ||
  2451. // Test the padding extension at TLS 1.2.
  2452. !TestPaddingExtension(TLS1_2_VERSION, TLS1_2_VERSION) ||
  2453. // Test the padding extension at TLS 1.3 with a TLS 1.2 session, so there
  2454. // will be no PSK binder after the padding extension.
  2455. !TestPaddingExtension(TLS1_3_VERSION, TLS1_2_VERSION) ||
  2456. // Test the padding extension at TLS 1.3 with a TLS 1.3 session, so there
  2457. // will be a PSK binder after the padding extension.
  2458. !TestPaddingExtension(TLS1_3_VERSION, TLS1_3_DRAFT_VERSION) ||
  2459. !TestClientCAList() ||
  2460. !TestInternalSessionCache() ||
  2461. !ForEachVersion(TestSequenceNumber) ||
  2462. !ForEachVersion(TestOneSidedShutdown) ||
  2463. !TestSessionDuplication() ||
  2464. !TestSetFD() ||
  2465. !TestSetBIO() ||
  2466. !ForEachVersion(TestGetPeerCertificate) ||
  2467. !ForEachVersion(TestRetainOnlySHA256OfCerts) ||
  2468. !TestClientHello() ||
  2469. !ForEachVersion(TestSessionIDContext) ||
  2470. !ForEachVersion(TestSessionTimeout) ||
  2471. !ForEachVersion(TestSessionTimeoutCertCallback) ||
  2472. !ForEachVersion(TestSNICallback) ||
  2473. !TestEarlyCallbackVersionSwitch() ||
  2474. !TestSetVersion() ||
  2475. !ForEachVersion(TestVersion) ||
  2476. !ForEachVersion(TestALPNCipherAvailable) ||
  2477. !ForEachVersion(TestSSLClearSessionResumption)) {
  2478. ERR_print_errors_fp(stderr);
  2479. return 1;
  2480. }
  2481. printf("PASS\n");
  2482. return 0;
  2483. }