David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 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 | |
| 15 | #include <stdio.h> |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 16 | #include <string.h> |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame] | 17 | #include <time.h> |
David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 18 | |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 19 | #include <algorithm> |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 20 | #include <string> |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 21 | #include <utility> |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 24 | #include <openssl/base64.h> |
| 25 | #include <openssl/bio.h> |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 26 | #include <openssl/crypto.h> |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 27 | #include <openssl/err.h> |
| 28 | #include <openssl/ssl.h> |
| 29 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 30 | #include "test/scoped_types.h" |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 31 | #include "../crypto/test/test_util.h" |
| 32 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 33 | |
| 34 | struct ExpectedCipher { |
| 35 | unsigned long id; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 36 | int in_group_flag; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 37 | }; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 38 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 39 | struct CipherTest { |
| 40 | // The rule string to apply. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 41 | const char *rule; |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 42 | // The list of expected ciphers, in order. |
| 43 | std::vector<ExpectedCipher> expected; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 44 | }; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 45 | |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 46 | static const CipherTest kCipherTests[] = { |
| 47 | // Selecting individual ciphers should work. |
| 48 | { |
| 49 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 50 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 51 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 52 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 53 | { |
| 54 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 55 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0}, |
| 56 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 57 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 58 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 59 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 60 | }, |
| 61 | }, |
| 62 | // + reorders selected ciphers to the end, keeping their relative order. |
| 63 | { |
| 64 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 65 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 66 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 67 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 68 | "+aRSA", |
| 69 | { |
| 70 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 71 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0}, |
| 72 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 73 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 74 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 75 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 76 | }, |
| 77 | }, |
| 78 | // ! banishes ciphers from future selections. |
| 79 | { |
| 80 | "!aRSA:" |
| 81 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 82 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 83 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 84 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 85 | { |
| 86 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 87 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0}, |
| 88 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 89 | }, |
| 90 | }, |
| 91 | // Multiple masks can be ANDed in a single rule. |
| 92 | { |
| 93 | "kRSA+AESGCM+AES128", |
| 94 | { |
| 95 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 96 | }, |
| 97 | }, |
| 98 | // - removes selected ciphers, but preserves their order for future |
| 99 | // selections. Select AES_128_GCM, but order the key exchanges RSA, DHE_RSA, |
| 100 | // ECDHE_RSA. |
| 101 | { |
| 102 | "ALL:-kECDHE:-kDHE:-kRSA:-ALL:" |
| 103 | "AESGCM+AES128+aRSA", |
| 104 | { |
| 105 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 106 | {TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 107 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 108 | }, |
| 109 | }, |
| 110 | // Unknown selectors are no-ops. |
| 111 | { |
| 112 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 113 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 114 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 115 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 116 | "BOGUS1:-BOGUS2:+BOGUS3:!BOGUS4", |
| 117 | { |
| 118 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 119 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0}, |
| 120 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 121 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 122 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 123 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 124 | }, |
| 125 | }, |
| 126 | // Square brackets specify equi-preference groups. |
| 127 | { |
| 128 | "[ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-ECDSA-AES128-GCM-SHA256]:" |
| 129 | "[ECDHE-RSA-CHACHA20-POLY1305]:" |
| 130 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 131 | { |
| 132 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1}, |
| 133 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 1}, |
| 134 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 135 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 1}, |
| 136 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 137 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 138 | }, |
| 139 | }, |
| 140 | // @STRENGTH performs a stable strength-sort of the selected ciphers and |
| 141 | // only the selected ciphers. |
| 142 | { |
| 143 | // To simplify things, banish all but {ECDHE_RSA,RSA} x |
| 144 | // {CHACHA20,AES_256_CBC,AES_128_CBC,RC4} x SHA1. |
| 145 | "!kEDH:!AESGCM:!3DES:!SHA256:!MD5:!SHA384:" |
| 146 | // Order some ciphers backwards by strength. |
| 147 | "ALL:-CHACHA20:-AES256:-AES128:-RC4:-ALL:" |
| 148 | // Select ECDHE ones and sort them by strength. Ties should resolve |
| 149 | // based on the order above. |
| 150 | "kECDHE:@STRENGTH:-ALL:" |
| 151 | // Now bring back everything uses RSA. ECDHE_RSA should be first, sorted |
| 152 | // by strength. Then RSA, backwards by strength. |
| 153 | "aRSA", |
| 154 | { |
| 155 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, 0}, |
| 156 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 157 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 158 | {TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA, 0}, |
| 159 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, 0}, |
| 160 | {SSL3_CK_RSA_RC4_128_SHA, 0}, |
| 161 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 162 | {TLS1_CK_RSA_WITH_AES_256_SHA, 0}, |
| 163 | }, |
| 164 | }, |
| 165 | // Exact ciphers may not be used in multi-part rules; they are treated |
| 166 | // as unknown aliases. |
| 167 | { |
| 168 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 169 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 170 | "!ECDHE-RSA-AES128-GCM-SHA256+RSA:" |
| 171 | "!ECDSA+ECDHE-ECDSA-AES128-GCM-SHA256", |
| 172 | { |
| 173 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 174 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 175 | }, |
| 176 | }, |
| 177 | // SSLv3 matches everything that existed before TLS 1.2. |
| 178 | { |
| 179 | "AES128-SHA:AES128-SHA256:!SSLv3", |
| 180 | { |
| 181 | {TLS1_CK_RSA_WITH_AES_128_SHA256, 0}, |
| 182 | }, |
| 183 | }, |
| 184 | // TLSv1.2 matches everything added in TLS 1.2. |
| 185 | { |
| 186 | "AES128-SHA:AES128-SHA256:!TLSv1.2", |
| 187 | { |
| 188 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 189 | }, |
| 190 | }, |
| 191 | // The two directives have no intersection. |
| 192 | { |
| 193 | "AES128-SHA:AES128-SHA256:!TLSv1.2+SSLv3", |
| 194 | { |
| 195 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 196 | {TLS1_CK_RSA_WITH_AES_128_SHA256, 0}, |
| 197 | }, |
| 198 | }, |
| 199 | // The shared name of the CHACHA20_POLY1305 variants behaves like a cipher |
| 200 | // name and not an alias. It may not be used in a multipart rule. (That the |
| 201 | // shared name works is covered by the standard tests.) |
| 202 | { |
| 203 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 204 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 205 | "!ECDHE-RSA-CHACHA20-POLY1305+RSA:" |
| 206 | "!ECDSA+ECDHE-ECDSA-CHACHA20-POLY1305", |
| 207 | { |
| 208 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 209 | {TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, 0}, |
| 210 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 211 | {TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, 0}, |
| 212 | }, |
| 213 | }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static const char *kBadRules[] = { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 217 | // Invalid brackets. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 218 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256", |
| 219 | "RSA]", |
| 220 | "[[RSA]]", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 221 | // Operators inside brackets. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 222 | "[+RSA]", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 223 | // Unknown directive. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 224 | "@BOGUS", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 225 | // Empty cipher lists error at SSL_CTX_set_cipher_list. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 226 | "", |
| 227 | "BOGUS", |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 228 | // COMPLEMENTOFDEFAULT is empty. |
| 229 | "COMPLEMENTOFDEFAULT", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 230 | // Invalid command. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 231 | "?BAR", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 232 | // Special operators are not allowed if groups are used. |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 233 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:+FOO", |
| 234 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:!FOO", |
| 235 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:-FOO", |
| 236 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:@STRENGTH", |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 237 | }; |
| 238 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 239 | static const char *kMustNotIncludeNull[] = { |
| 240 | "ALL", |
| 241 | "DEFAULT", |
| 242 | "ALL:!eNULL", |
| 243 | "ALL:!NULL", |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 244 | "MEDIUM", |
| 245 | "HIGH", |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 246 | "FIPS", |
| 247 | "SHA", |
| 248 | "SHA1", |
| 249 | "RSA", |
| 250 | "SSLv3", |
| 251 | "TLSv1", |
| 252 | "TLSv1.2", |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 255 | static void PrintCipherPreferenceList(ssl_cipher_preference_list_st *list) { |
| 256 | bool in_group = false; |
| 257 | for (size_t i = 0; i < sk_SSL_CIPHER_num(list->ciphers); i++) { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 258 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(list->ciphers, i); |
| 259 | if (!in_group && list->in_group_flags[i]) { |
| 260 | fprintf(stderr, "\t[\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 261 | in_group = true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 262 | } |
| 263 | fprintf(stderr, "\t"); |
| 264 | if (in_group) { |
| 265 | fprintf(stderr, " "); |
| 266 | } |
| 267 | fprintf(stderr, "%s\n", SSL_CIPHER_get_name(cipher)); |
| 268 | if (in_group && !list->in_group_flags[i]) { |
| 269 | fprintf(stderr, "\t]\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 270 | in_group = false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 275 | static bool TestCipherRule(const CipherTest &t) { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 276 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 277 | if (!ctx) { |
| 278 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 279 | } |
| 280 | |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 281 | if (!SSL_CTX_set_cipher_list(ctx.get(), t.rule)) { |
| 282 | fprintf(stderr, "Error testing cipher rule '%s'\n", t.rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 283 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 284 | } |
| 285 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 286 | // Compare the two lists. |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 287 | if (sk_SSL_CIPHER_num(ctx->cipher_list->ciphers) != t.expected.size()) { |
| 288 | fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t.rule); |
| 289 | PrintCipherPreferenceList(ctx->cipher_list); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | for (size_t i = 0; i < t.expected.size(); i++) { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 294 | const SSL_CIPHER *cipher = |
| 295 | sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i); |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 296 | if (t.expected[i].id != SSL_CIPHER_get_id(cipher) || |
| 297 | t.expected[i].in_group_flag != ctx->cipher_list->in_group_flags[i]) { |
| 298 | fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t.rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 299 | PrintCipherPreferenceList(ctx->cipher_list); |
| 300 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 304 | return true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 307 | static bool TestRuleDoesNotIncludeNull(const char *rule) { |
| 308 | ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_server_method())); |
| 309 | if (!ctx) { |
| 310 | return false; |
| 311 | } |
| 312 | if (!SSL_CTX_set_cipher_list(ctx.get(), rule)) { |
| 313 | fprintf(stderr, "Error: cipher rule '%s' failed\n", rule); |
| 314 | return false; |
| 315 | } |
| 316 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ctx->cipher_list->ciphers); i++) { |
| 317 | if (SSL_CIPHER_is_NULL(sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i))) { |
| 318 | fprintf(stderr, "Error: cipher rule '%s' includes NULL\n",rule); |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 325 | static bool TestCipherRules() { |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 326 | for (const CipherTest &test : kCipherTests) { |
| 327 | if (!TestCipherRule(test)) { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 328 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 332 | for (const char *rule : kBadRules) { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 333 | ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_server_method())); |
| 334 | if (!ctx) { |
| 335 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 336 | } |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 337 | if (SSL_CTX_set_cipher_list(ctx.get(), rule)) { |
| 338 | fprintf(stderr, "Cipher rule '%s' unexpectedly succeeded\n", rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 339 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 340 | } |
| 341 | ERR_clear_error(); |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 342 | } |
| 343 | |
David Benjamin | fb974e6 | 2015-12-16 19:34:22 -0500 | [diff] [blame^] | 344 | for (const char *rule : kMustNotIncludeNull) { |
| 345 | if (!TestRuleDoesNotIncludeNull(rule)) { |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 350 | return true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 351 | } |
David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 352 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 353 | // kOpenSSLSession is a serialized SSL_SESSION generated from openssl |
| 354 | // s_client -sess_out. |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 355 | static const char kOpenSSLSession[] = |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 356 | "MIIFpQIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 357 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 358 | "IWoJoQYCBFRDO46iBAICASyjggR6MIIEdjCCA16gAwIBAgIIK9dUvsPWSlUwDQYJ" |
| 359 | "KoZIhvcNAQEFBQAwSTELM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bnUMZ" |
| 374 | "hTAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEv" |
| 375 | "MBcGA1UdIAQQMA4wDAYKKwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRw" |
| 376 | "Oi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBBQUAA4IBAQCa" |
| 377 | "OXCBdoqUy5bxyq+Wrh1zsyyCFim1PH5VU2+yvDSWrgDY8ibRGJmfff3r4Lud5kal" |
| 378 | "dKs9k8YlKD3ITG7P0YT/Rk8hLgfEuLcq5cc0xqmE42xJ+Eo2uzq9rYorc5emMCxf" |
| 379 | "5L0TJOXZqHQpOEcuptZQ4OjdYMfSxk5UzueUhA3ogZKRcRkdB3WeWRp+nYRhx4St" |
| 380 | "o2rt2A0MKmY9165GHUqMK9YaaXHDXqBu7Sefr1uSoAP9gyIJKeihMivsGqJ1TD6Z" |
| 381 | "cc6LMe+dN2P8cZEQHtD1y296ul4Mivqk3jatUVL8/hCwgch9A8O4PGZq9WqBfEWm" |
| 382 | "IyHh1dPtbg1lOXdYCWtjpAIEAKUDAgEUqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36S" |
| 383 | "YTcLEkXqKwOBfF9vE4KX0NxeLwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9B" |
| 384 | "sNHM362zZnY27GpTw+Kwd751CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yE" |
| 385 | "OTDKPNj3+inbMaVigtK4PLyPq+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdA" |
| 386 | "i4gv7Y5oliyn"; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 387 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 388 | // kCustomSession is a custom serialized SSL_SESSION generated by |
| 389 | // filling in missing fields from |kOpenSSLSession|. This includes |
| 390 | // providing |peer_sha256|, so |peer| is not serialized. |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 391 | static const char kCustomSession[] = |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 392 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 393 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 394 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 395 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 396 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 397 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 398 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 399 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 400 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 401 | // kBoringSSLSession is a serialized SSL_SESSION generated from bssl client. |
| 402 | static const char kBoringSSLSession[] = |
| 403 | "MIIRwQIBAQICAwMEAsAvBCDdoGxGK26mR+8lM0uq6+k9xYuxPnwAjpcF9n0Yli9R" |
| 404 | "kQQwbyshfWhdi5XQ1++7n2L1qqrcVlmHBPpr6yknT/u4pUrpQB5FZ7vqvNn8MdHf" |
| 405 | "9rWgoQYCBFXgs7uiBAICHCCjggR6MIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJ" |
| 406 | "KoZIhvcNAQELBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx" |
| 407 | "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEy" |
| 408 | "MTQ1MzE1WhcNMTUxMTEwMDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK" |
| 409 | "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v" |
| 410 | "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB" |
| 411 | "AQUAA4IBDwAwggEKAoIBAQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpo" |
| 412 | "PLuBinvhkXZo3DC133NpCBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU" |
| 413 | "792c7hFyNXSUCG7At8Ifi3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mce" |
| 414 | "Tv9iGKqSkSTlp8puy/9SZ/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/" |
| 415 | "RCh8/UKc8PaL+cxlt531qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eL" |
| 416 | "EucWQ72YZU8mUzXBoXGn0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAd" |
| 417 | "BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdv" |
| 418 | "b2dsZS5jb20waAYIKwYBBQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtp" |
| 419 | "Lmdvb2dsZS5jb20vR0lBRzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50" |
| 420 | "czEuZ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KoZIhvcNAQELBQAwSTEL" |
| 435 | "MAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2ds" |
| 436 | "ZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEyMTQ1MzE1WhcNMTUxMTEw" |
| 437 | "MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG" |
| 438 | "A1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UE" |
| 439 | "AwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB" |
| 440 | "AQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpoPLuBinvhkXZo3DC133Np" |
| 441 | "CBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU792c7hFyNXSUCG7At8If" |
| 442 | "i3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mceTv9iGKqSkSTlp8puy/9S" |
| 443 | "Z/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/RCh8/UKc8PaL+cxlt531" |
| 444 | "qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eLEucWQ72YZU8mUzXBoXGn" |
| 445 | "0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAdBgNVHSUEFjAUBggrBgEF" |
| 446 | "BQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdvb2dsZS5jb20waAYIKwYB" |
| 447 | "BQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtpLmdvb2dsZS5jb20vR0lB" |
| 448 | "RzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50czEuZ29vZ2xlLmNvbS9v" |
| 449 | "Y3NwMB0GA1UdDgQWB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|
| 493 | "cmwwTgYDVR0gBEcwRTBDBgRVHSAAMDswOQYIKwYBBQUHAgEWLWh0dHBzOi8vd3d3" |
| 494 | "Lmdlb3RydXN0LmNvbS9yZXNvdXJjZXMvcmVwb3NpdG9yeTANBgkqhkiG9w0BAQUF" |
| 495 | "AAOBgQB24RJuTksWEoYwBrKBCM/wCMfHcX5m7sLt1Dsf//DwyE7WQziwuTB9GNBV" |
| 496 | "g6JqyzYRnOhIZqNtf7gT1Ef+i1pcc/yu2RsyGTirlzQUqpbS66McFAhJtrvlke+D" |
| 497 | "NusdVm/K2rxzY5Dkf3s+Iss9B+1fOHSc4wNQTqGvmO5h8oQ/Eg=="; |
| 498 | |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 499 | // kBadSessionExtraField is a custom serialized SSL_SESSION generated by replacing |
| 500 | // the final (optional) element of |kCustomSession| with tag number 30. |
| 501 | static const char kBadSessionExtraField[] = |
| 502 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 503 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 504 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 505 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 506 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 507 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 508 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 509 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBL4DBAEF"; |
| 510 | |
David Benjamin | 338e067 | 2015-05-28 20:00:08 -0400 | [diff] [blame] | 511 | // kBadSessionVersion is a custom serialized SSL_SESSION generated by replacing |
| 512 | // the version of |kCustomSession| with 2. |
| 513 | static const char kBadSessionVersion[] = |
| 514 | "MIIBdgIBAgICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 515 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 516 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 517 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 518 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 519 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 520 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 521 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
| 522 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 523 | // kBadSessionTrailingData is a custom serialized SSL_SESSION with trailing data |
| 524 | // appended. |
| 525 | static const char kBadSessionTrailingData[] = |
| 526 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 527 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 528 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 529 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 530 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 531 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 532 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 533 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEFAAAA"; |
| 534 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 535 | static bool DecodeBase64(std::vector<uint8_t> *out, const char *in) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 536 | size_t len; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 537 | if (!EVP_DecodedLength(&len, strlen(in))) { |
| 538 | fprintf(stderr, "EVP_DecodedLength failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 539 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 540 | } |
| 541 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 542 | out->resize(len); |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 543 | if (!EVP_DecodeBase64(out->data(), &len, len, (const uint8_t *)in, |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 544 | strlen(in))) { |
| 545 | fprintf(stderr, "EVP_DecodeBase64 failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 546 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 547 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 548 | out->resize(len); |
| 549 | return true; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 550 | } |
| 551 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 552 | static bool TestSSL_SESSIONEncoding(const char *input_b64) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 553 | const uint8_t *cptr; |
| 554 | uint8_t *ptr; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 555 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 556 | // Decode the input. |
| 557 | std::vector<uint8_t> input; |
| 558 | if (!DecodeBase64(&input, input_b64)) { |
| 559 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 560 | } |
| 561 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 562 | // Verify the SSL_SESSION decodes. |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 563 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(input.data(), input.size())); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 564 | if (!session) { |
| 565 | fprintf(stderr, "SSL_SESSION_from_bytes failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 566 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 567 | } |
| 568 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 569 | // Verify the SSL_SESSION encoding round-trips. |
| 570 | size_t encoded_len; |
| 571 | ScopedOpenSSLBytes encoded; |
| 572 | uint8_t *encoded_raw; |
| 573 | if (!SSL_SESSION_to_bytes(session.get(), &encoded_raw, &encoded_len)) { |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 574 | fprintf(stderr, "SSL_SESSION_to_bytes failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 575 | return false; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 576 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 577 | encoded.reset(encoded_raw); |
| 578 | if (encoded_len != input.size() || |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 579 | memcmp(input.data(), encoded.get(), input.size()) != 0) { |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 580 | fprintf(stderr, "SSL_SESSION_to_bytes did not round-trip\n"); |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 581 | hexdump(stderr, "Before: ", input.data(), input.size()); |
| 582 | hexdump(stderr, "After: ", encoded_raw, encoded_len); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 583 | return false; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 584 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 585 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 586 | // Verify the SSL_SESSION also decodes with the legacy API. |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 587 | cptr = input.data(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 588 | session.reset(d2i_SSL_SESSION(NULL, &cptr, input.size())); |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 589 | if (!session || cptr != input.data() + input.size()) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 590 | fprintf(stderr, "d2i_SSL_SESSION failed\n"); |
| 591 | return false; |
| 592 | } |
| 593 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 594 | // Verify the SSL_SESSION encoding round-trips via the legacy API. |
| 595 | int len = i2d_SSL_SESSION(session.get(), NULL); |
| 596 | if (len < 0 || (size_t)len != input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 597 | fprintf(stderr, "i2d_SSL_SESSION(NULL) returned invalid length\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 598 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 599 | } |
| 600 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 601 | encoded.reset((uint8_t *)OPENSSL_malloc(input.size())); |
| 602 | if (!encoded) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 603 | fprintf(stderr, "malloc failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 604 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 605 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 606 | |
| 607 | ptr = encoded.get(); |
| 608 | len = i2d_SSL_SESSION(session.get(), &ptr); |
| 609 | if (len < 0 || (size_t)len != input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 610 | fprintf(stderr, "i2d_SSL_SESSION returned invalid length\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 611 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 612 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 613 | if (ptr != encoded.get() + input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 614 | fprintf(stderr, "i2d_SSL_SESSION did not advance ptr correctly\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 615 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 616 | } |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 617 | if (memcmp(input.data(), encoded.get(), input.size()) != 0) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 618 | fprintf(stderr, "i2d_SSL_SESSION did not round-trip\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 619 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 620 | } |
| 621 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 622 | return true; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 623 | } |
| 624 | |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 625 | static bool TestBadSSL_SESSIONEncoding(const char *input_b64) { |
| 626 | std::vector<uint8_t> input; |
| 627 | if (!DecodeBase64(&input, input_b64)) { |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | // Verify that the SSL_SESSION fails to decode. |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 632 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(input.data(), input.size())); |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 633 | if (session) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 634 | fprintf(stderr, "SSL_SESSION_from_bytes unexpectedly succeeded\n"); |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | ERR_clear_error(); |
| 638 | return true; |
| 639 | } |
| 640 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 641 | static bool TestDefaultVersion(uint16_t version, |
| 642 | const SSL_METHOD *(*method)(void)) { |
| 643 | ScopedSSL_CTX ctx(SSL_CTX_new(method())); |
| 644 | if (!ctx) { |
| 645 | return false; |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 646 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 647 | return ctx->min_version == version && ctx->max_version == version; |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 648 | } |
| 649 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 650 | static bool CipherGetRFCName(std::string *out, uint16_t value) { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 651 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(value); |
| 652 | if (cipher == NULL) { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 653 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 654 | } |
David Benjamin | 67be048 | 2015-04-20 16:19:00 -0400 | [diff] [blame] | 655 | ScopedOpenSSLString rfc_name(SSL_CIPHER_get_rfc_name(cipher)); |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 656 | if (!rfc_name) { |
| 657 | return false; |
| 658 | } |
David Benjamin | 67be048 | 2015-04-20 16:19:00 -0400 | [diff] [blame] | 659 | out->assign(rfc_name.get()); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 660 | return true; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | typedef struct { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 664 | int id; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 665 | const char *rfc_name; |
| 666 | } CIPHER_RFC_NAME_TEST; |
| 667 | |
| 668 | static const CIPHER_RFC_NAME_TEST kCipherRFCNameTests[] = { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 669 | { SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" }, |
| 670 | { SSL3_CK_RSA_RC4_128_MD5, "TLS_RSA_WITH_RC4_MD5" }, |
| 671 | { TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA" }, |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 672 | { TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" }, |
| 673 | { TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, |
| 674 | "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" }, |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 675 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
| 676 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, |
| 677 | { TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
| 678 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" }, |
| 679 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 680 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" }, |
| 681 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 682 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, |
| 683 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 684 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" }, |
| 685 | { TLS1_CK_PSK_WITH_RC4_128_SHA, "TLS_PSK_WITH_RC4_SHA" }, |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 686 | { TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 687 | "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" }, |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 688 | { TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 689 | "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 690 | // These names are non-standard: |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 691 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 692 | "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" }, |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 693 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 694 | "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" }, |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 695 | }; |
| 696 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 697 | static bool TestCipherGetRFCName(void) { |
| 698 | for (size_t i = 0; |
| 699 | i < sizeof(kCipherRFCNameTests) / sizeof(kCipherRFCNameTests[0]); i++) { |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 700 | const CIPHER_RFC_NAME_TEST *test = &kCipherRFCNameTests[i]; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 701 | std::string rfc_name; |
| 702 | if (!CipherGetRFCName(&rfc_name, test->id & 0xffff)) { |
| 703 | fprintf(stderr, "SSL_CIPHER_get_rfc_name failed\n"); |
| 704 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 705 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 706 | if (rfc_name != test->rfc_name) { |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 707 | fprintf(stderr, "SSL_CIPHER_get_rfc_name: got '%s', wanted '%s'\n", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 708 | rfc_name.c_str(), test->rfc_name); |
| 709 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 710 | } |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 711 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 712 | return true; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 713 | } |
| 714 | |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 715 | // CreateSessionWithTicket returns a sample |SSL_SESSION| with the ticket |
| 716 | // replaced for one of length |ticket_len| or nullptr on failure. |
| 717 | static ScopedSSL_SESSION CreateSessionWithTicket(size_t ticket_len) { |
| 718 | std::vector<uint8_t> der; |
| 719 | if (!DecodeBase64(&der, kOpenSSLSession)) { |
| 720 | return nullptr; |
| 721 | } |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 722 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(der.data(), der.size())); |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 723 | if (!session) { |
| 724 | return nullptr; |
| 725 | } |
| 726 | |
| 727 | // Swap out the ticket for a garbage one. |
| 728 | OPENSSL_free(session->tlsext_tick); |
| 729 | session->tlsext_tick = reinterpret_cast<uint8_t*>(OPENSSL_malloc(ticket_len)); |
| 730 | if (session->tlsext_tick == nullptr) { |
| 731 | return nullptr; |
| 732 | } |
| 733 | memset(session->tlsext_tick, 'a', ticket_len); |
| 734 | session->tlsext_ticklen = ticket_len; |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame] | 735 | |
| 736 | // Fix up the timeout. |
| 737 | session->time = time(NULL); |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 738 | return session; |
| 739 | } |
| 740 | |
| 741 | // GetClientHelloLen creates a client SSL connection with a ticket of length |
| 742 | // |ticket_len| and records the ClientHello. It returns the length of the |
| 743 | // ClientHello, not including the record header, on success and zero on error. |
| 744 | static size_t GetClientHelloLen(size_t ticket_len) { |
| 745 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 746 | ScopedSSL_SESSION session = CreateSessionWithTicket(ticket_len); |
| 747 | if (!ctx || !session) { |
| 748 | return 0; |
| 749 | } |
| 750 | ScopedSSL ssl(SSL_new(ctx.get())); |
| 751 | ScopedBIO bio(BIO_new(BIO_s_mem())); |
| 752 | if (!ssl || !bio || !SSL_set_session(ssl.get(), session.get())) { |
| 753 | return 0; |
| 754 | } |
| 755 | // Do not configure a reading BIO, but record what's written to a memory BIO. |
| 756 | SSL_set_bio(ssl.get(), nullptr /* rbio */, BIO_up_ref(bio.get())); |
| 757 | int ret = SSL_connect(ssl.get()); |
| 758 | if (ret > 0) { |
| 759 | // SSL_connect should fail without a BIO to write to. |
| 760 | return 0; |
| 761 | } |
| 762 | ERR_clear_error(); |
| 763 | |
| 764 | const uint8_t *unused; |
| 765 | size_t client_hello_len; |
| 766 | if (!BIO_mem_contents(bio.get(), &unused, &client_hello_len) || |
| 767 | client_hello_len <= SSL3_RT_HEADER_LENGTH) { |
| 768 | return 0; |
| 769 | } |
| 770 | return client_hello_len - SSL3_RT_HEADER_LENGTH; |
| 771 | } |
| 772 | |
| 773 | struct PaddingTest { |
| 774 | size_t input_len, padded_len; |
| 775 | }; |
| 776 | |
| 777 | static const PaddingTest kPaddingTests[] = { |
| 778 | // ClientHellos of length below 0x100 do not require padding. |
| 779 | {0xfe, 0xfe}, |
| 780 | {0xff, 0xff}, |
| 781 | // ClientHellos of length 0x100 through 0x1fb are padded up to 0x200. |
| 782 | {0x100, 0x200}, |
| 783 | {0x123, 0x200}, |
| 784 | {0x1fb, 0x200}, |
| 785 | // ClientHellos of length 0x1fc through 0x1ff get padded beyond 0x200. The |
| 786 | // padding extension takes a minimum of four bytes plus one required content |
| 787 | // byte. (To work around yet more server bugs, we avoid empty final |
| 788 | // extensions.) |
| 789 | {0x1fc, 0x201}, |
| 790 | {0x1fd, 0x202}, |
| 791 | {0x1fe, 0x203}, |
| 792 | {0x1ff, 0x204}, |
| 793 | // Finally, larger ClientHellos need no padding. |
| 794 | {0x200, 0x200}, |
| 795 | {0x201, 0x201}, |
| 796 | }; |
| 797 | |
| 798 | static bool TestPaddingExtension() { |
| 799 | // Sample a baseline length. |
| 800 | size_t base_len = GetClientHelloLen(1); |
| 801 | if (base_len == 0) { |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | for (const PaddingTest &test : kPaddingTests) { |
| 806 | if (base_len > test.input_len) { |
| 807 | fprintf(stderr, "Baseline ClientHello too long.\n"); |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | size_t padded_len = GetClientHelloLen(1 + test.input_len - base_len); |
| 812 | if (padded_len != test.padded_len) { |
| 813 | fprintf(stderr, "%u-byte ClientHello padded to %u bytes, not %u.\n", |
| 814 | static_cast<unsigned>(test.input_len), |
| 815 | static_cast<unsigned>(padded_len), |
| 816 | static_cast<unsigned>(test.padded_len)); |
| 817 | return false; |
| 818 | } |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 823 | // Test that |SSL_get_client_CA_list| echoes back the configured parameter even |
| 824 | // before configuring as a server. |
| 825 | static bool TestClientCAList() { |
| 826 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 827 | if (!ctx) { |
| 828 | return false; |
| 829 | } |
| 830 | ScopedSSL ssl(SSL_new(ctx.get())); |
| 831 | if (!ssl) { |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | STACK_OF(X509_NAME) *stack = sk_X509_NAME_new_null(); |
| 836 | if (stack == nullptr) { |
| 837 | return false; |
| 838 | } |
| 839 | // |SSL_set_client_CA_list| takes ownership. |
| 840 | SSL_set_client_CA_list(ssl.get(), stack); |
| 841 | |
| 842 | return SSL_get_client_CA_list(ssl.get()) == stack; |
| 843 | } |
| 844 | |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 845 | static void AppendSession(SSL_SESSION *session, void *arg) { |
| 846 | std::vector<SSL_SESSION*> *out = |
| 847 | reinterpret_cast<std::vector<SSL_SESSION*>*>(arg); |
| 848 | out->push_back(session); |
| 849 | } |
| 850 | |
| 851 | // ExpectCache returns true if |ctx|'s session cache consists of |expected|, in |
| 852 | // order. |
| 853 | static bool ExpectCache(SSL_CTX *ctx, |
| 854 | const std::vector<SSL_SESSION*> &expected) { |
| 855 | // Check the linked list. |
| 856 | SSL_SESSION *ptr = ctx->session_cache_head; |
| 857 | for (SSL_SESSION *session : expected) { |
| 858 | if (ptr != session) { |
| 859 | return false; |
| 860 | } |
| 861 | // TODO(davidben): This is an absurd way to denote the end of the list. |
| 862 | if (ptr->next == |
| 863 | reinterpret_cast<SSL_SESSION *>(&ctx->session_cache_tail)) { |
| 864 | ptr = nullptr; |
| 865 | } else { |
| 866 | ptr = ptr->next; |
| 867 | } |
| 868 | } |
| 869 | if (ptr != nullptr) { |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | // Check the hash table. |
| 874 | std::vector<SSL_SESSION*> actual, expected_copy; |
| 875 | lh_SSL_SESSION_doall_arg(SSL_CTX_sessions(ctx), AppendSession, &actual); |
| 876 | expected_copy = expected; |
| 877 | |
| 878 | std::sort(actual.begin(), actual.end()); |
| 879 | std::sort(expected_copy.begin(), expected_copy.end()); |
| 880 | |
| 881 | return actual == expected_copy; |
| 882 | } |
| 883 | |
| 884 | static ScopedSSL_SESSION CreateTestSession(uint32_t number) { |
| 885 | ScopedSSL_SESSION ret(SSL_SESSION_new()); |
| 886 | if (!ret) { |
| 887 | return nullptr; |
| 888 | } |
| 889 | |
| 890 | ret->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
| 891 | memset(ret->session_id, 0, ret->session_id_length); |
| 892 | memcpy(ret->session_id, &number, sizeof(number)); |
| 893 | return ret; |
| 894 | } |
| 895 | |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 896 | // Test that the internal session cache behaves as expected. |
| 897 | static bool TestInternalSessionCache() { |
| 898 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 899 | if (!ctx) { |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | // Prepare 10 test sessions. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 904 | std::vector<ScopedSSL_SESSION> sessions; |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 905 | for (int i = 0; i < 10; i++) { |
| 906 | ScopedSSL_SESSION session = CreateTestSession(i); |
| 907 | if (!session) { |
| 908 | return false; |
| 909 | } |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 910 | sessions.push_back(std::move(session)); |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | SSL_CTX_sess_set_cache_size(ctx.get(), 5); |
| 914 | |
| 915 | // Insert all the test sessions. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 916 | for (const auto &session : sessions) { |
| 917 | if (!SSL_CTX_add_session(ctx.get(), session.get())) { |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 918 | return false; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // Only the last five should be in the list. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 923 | std::vector<SSL_SESSION*> expected = { |
| 924 | sessions[9].get(), |
| 925 | sessions[8].get(), |
| 926 | sessions[7].get(), |
| 927 | sessions[6].get(), |
| 928 | sessions[5].get(), |
| 929 | }; |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 930 | if (!ExpectCache(ctx.get(), expected)) { |
| 931 | return false; |
| 932 | } |
| 933 | |
| 934 | // Inserting an element already in the cache should fail. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 935 | if (SSL_CTX_add_session(ctx.get(), sessions[7].get()) || |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 936 | !ExpectCache(ctx.get(), expected)) { |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | // Although collisions should be impossible (256-bit session IDs), the cache |
| 941 | // must handle them gracefully. |
| 942 | ScopedSSL_SESSION collision(CreateTestSession(7)); |
| 943 | if (!collision || !SSL_CTX_add_session(ctx.get(), collision.get())) { |
| 944 | return false; |
| 945 | } |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 946 | expected = { |
| 947 | collision.get(), |
| 948 | sessions[9].get(), |
| 949 | sessions[8].get(), |
| 950 | sessions[6].get(), |
| 951 | sessions[5].get(), |
| 952 | }; |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 953 | if (!ExpectCache(ctx.get(), expected)) { |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | // Removing sessions behaves correctly. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 958 | if (!SSL_CTX_remove_session(ctx.get(), sessions[6].get())) { |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 959 | return false; |
| 960 | } |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 961 | expected = { |
| 962 | collision.get(), |
| 963 | sessions[9].get(), |
| 964 | sessions[8].get(), |
| 965 | sessions[5].get(), |
| 966 | }; |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 967 | if (!ExpectCache(ctx.get(), expected)) { |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | // Removing sessions requires an exact match. |
David Benjamin | 4f6acaf | 2015-11-21 03:00:50 -0500 | [diff] [blame] | 972 | if (SSL_CTX_remove_session(ctx.get(), sessions[0].get()) || |
| 973 | SSL_CTX_remove_session(ctx.get(), sessions[7].get()) || |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 974 | !ExpectCache(ctx.get(), expected)) { |
| 975 | return false; |
| 976 | } |
| 977 | |
| 978 | return true; |
| 979 | } |
| 980 | |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 981 | int main() { |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 982 | CRYPTO_library_init(); |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 983 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 984 | if (!TestCipherRules() || |
| 985 | !TestSSL_SESSIONEncoding(kOpenSSLSession) || |
| 986 | !TestSSL_SESSIONEncoding(kCustomSession) || |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 987 | !TestSSL_SESSIONEncoding(kBoringSSLSession) || |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 988 | !TestBadSSL_SESSIONEncoding(kBadSessionExtraField) || |
David Benjamin | 338e067 | 2015-05-28 20:00:08 -0400 | [diff] [blame] | 989 | !TestBadSSL_SESSIONEncoding(kBadSessionVersion) || |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 990 | !TestBadSSL_SESSIONEncoding(kBadSessionTrailingData) || |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 991 | !TestDefaultVersion(0, &TLS_method) || |
| 992 | !TestDefaultVersion(SSL3_VERSION, &SSLv3_method) || |
| 993 | !TestDefaultVersion(TLS1_VERSION, &TLSv1_method) || |
| 994 | !TestDefaultVersion(TLS1_1_VERSION, &TLSv1_1_method) || |
David Benjamin | 6879373 | 2015-05-04 20:20:48 -0400 | [diff] [blame] | 995 | !TestDefaultVersion(TLS1_2_VERSION, &TLSv1_2_method) || |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 996 | !TestDefaultVersion(0, &DTLS_method) || |
| 997 | !TestDefaultVersion(DTLS1_VERSION, &DTLSv1_method) || |
| 998 | !TestDefaultVersion(DTLS1_2_VERSION, &DTLSv1_2_method) || |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 999 | !TestCipherGetRFCName() || |
David Benjamin | 1d128f3 | 2015-09-08 17:41:40 -0400 | [diff] [blame] | 1000 | !TestPaddingExtension() || |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 1001 | !TestClientCAList() || |
| 1002 | !TestInternalSessionCache()) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 1003 | ERR_print_errors_fp(stderr); |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 1004 | return 1; |
| 1005 | } |
| 1006 | |
David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 1007 | printf("PASS\n"); |
| 1008 | return 0; |
| 1009 | } |