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 | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 17 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 21 | #include <openssl/base64.h> |
| 22 | #include <openssl/bio.h> |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 23 | #include <openssl/err.h> |
| 24 | #include <openssl/ssl.h> |
| 25 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 26 | #include "test/scoped_types.h" |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 27 | #include "../crypto/test/test_util.h" |
| 28 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 29 | |
| 30 | struct ExpectedCipher { |
| 31 | unsigned long id; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 32 | int in_group_flag; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 33 | }; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 34 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 35 | struct CipherTest { |
| 36 | // The rule string to apply. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 37 | const char *rule; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 38 | // The list of expected ciphers, in order, terminated with -1. |
| 39 | const ExpectedCipher *expected; |
| 40 | }; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 41 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 42 | // Selecting individual ciphers should work. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 43 | static const char kRule1[] = |
| 44 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 45 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 46 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 47 | "ECDHE-RSA-AES128-GCM-SHA256"; |
| 48 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 49 | static const ExpectedCipher kExpected1[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 50 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 0 }, |
| 51 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 0 }, |
| 52 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 53 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 54 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 57 | // + reorders selected ciphers to the end, keeping their relative |
| 58 | // order. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 59 | static const char kRule2[] = |
| 60 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 61 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 62 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 63 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 64 | "+aRSA"; |
| 65 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 66 | static const ExpectedCipher kExpected2[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 67 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 0 }, |
| 68 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 69 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 0 }, |
| 70 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 71 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 74 | // ! banishes ciphers from future selections. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 75 | static const char kRule3[] = |
| 76 | "!aRSA:" |
| 77 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 78 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 79 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 80 | "ECDHE-RSA-AES128-GCM-SHA256"; |
| 81 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 82 | static const ExpectedCipher kExpected3[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 83 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 0 }, |
| 84 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 85 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 86 | }; |
| 87 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 88 | // Multiple masks can be ANDed in a single rule. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 89 | static const char kRule4[] = "kRSA+AESGCM+AES128"; |
| 90 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 91 | static const ExpectedCipher kExpected4[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 92 | { TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 93 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 96 | // - removes selected ciphers, but preserves their order for future |
| 97 | // selections. Select AES_128_GCM, but order the key exchanges RSA, |
| 98 | // DHE_RSA, ECDHE_RSA. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 99 | static const char kRule5[] = |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 100 | "ALL:-kECDHE:-kDHE:-kRSA:-ALL:" |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 101 | "AESGCM+AES128+aRSA"; |
| 102 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 103 | static const ExpectedCipher kExpected5[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 104 | { TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 105 | { TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 106 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 107 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 110 | // Unknown selectors are no-ops. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 111 | static const char kRule6[] = |
| 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 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 118 | static const ExpectedCipher kExpected6[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 119 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 0 }, |
| 120 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 0 }, |
| 121 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 122 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 123 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 126 | // Square brackets specify equi-preference groups. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 127 | static const char kRule7[] = |
| 128 | "[ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-ECDSA-AES128-GCM-SHA256]:" |
| 129 | "[ECDHE-RSA-CHACHA20-POLY1305]:" |
| 130 | "ECDHE-RSA-AES128-GCM-SHA256"; |
| 131 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 132 | static const ExpectedCipher kExpected7[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 133 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 1 }, |
| 134 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0 }, |
| 135 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 0 }, |
| 136 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 137 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 140 | // @STRENGTH performs a stable strength-sort of the selected |
| 141 | // ciphers and only the selected ciphers. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 142 | static const char kRule8[] = |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 143 | // To simplify things, banish all but {ECDHE_RSA,RSA} x |
| 144 | // {CHACHA20,AES_256_CBC,AES_128_CBC,RC4} x SHA1. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 145 | "!kEDH:!AESGCM:!3DES:!SHA256:!MD5:!SHA384:" |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 146 | // Order some ciphers backwards by strength. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 147 | "ALL:-CHACHA20:-AES256:-AES128:-RC4:-ALL:" |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 148 | // Select ECDHE ones and sort them by strength. Ties should resolve |
| 149 | // based on the order above. |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 150 | "kECDHE:@STRENGTH:-ALL:" |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 151 | // Now bring back everything uses RSA. ECDHE_RSA should be first, |
| 152 | // sorted by strength. Then RSA, backwards by strength. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 153 | "aRSA"; |
| 154 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 155 | static const ExpectedCipher kExpected8[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 156 | { TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, 0 }, |
| 157 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 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 }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 163 | { 0, 0 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 164 | }; |
| 165 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 166 | // Exact ciphers may not be used in multi-part rules; they are treated |
| 167 | // as unknown aliases. |
| 168 | static const char kRule9[] = |
| 169 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 170 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 171 | "!ECDHE-RSA-CHACHA20-POLY1305+RSA:" |
| 172 | "!ECDSA+ECDHE-ECDSA-CHACHA20-POLY1305"; |
| 173 | |
| 174 | static const ExpectedCipher kExpected9[] = { |
| 175 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, 0 }, |
| 176 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, 0 }, |
| 177 | { 0, 0 }, |
| 178 | }; |
| 179 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 180 | static CipherTest kCipherTests[] = { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 181 | { kRule1, kExpected1 }, |
| 182 | { kRule2, kExpected2 }, |
| 183 | { kRule3, kExpected3 }, |
| 184 | { kRule4, kExpected4 }, |
| 185 | { kRule5, kExpected5 }, |
| 186 | { kRule6, kExpected6 }, |
| 187 | { kRule7, kExpected7 }, |
| 188 | { kRule8, kExpected8 }, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 189 | { kRule9, kExpected9 }, |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 190 | { NULL, NULL }, |
| 191 | }; |
| 192 | |
| 193 | static const char *kBadRules[] = { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 194 | // Invalid brackets. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 195 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256", |
| 196 | "RSA]", |
| 197 | "[[RSA]]", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 198 | // Operators inside brackets. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 199 | "[+RSA]", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 200 | // Unknown directive. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 201 | "@BOGUS", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 202 | // Empty cipher lists error at SSL_CTX_set_cipher_list. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 203 | "", |
| 204 | "BOGUS", |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 205 | // COMPLEMENTOFDEFAULT is empty. |
| 206 | "COMPLEMENTOFDEFAULT", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 207 | // Invalid command. |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 208 | "?BAR", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 209 | // Special operators are not allowed if groups are used. |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 210 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:+FOO", |
| 211 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:!FOO", |
| 212 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:-FOO", |
| 213 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:@STRENGTH", |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 214 | NULL, |
| 215 | }; |
| 216 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame^] | 217 | static const char *kMustNotIncludeNull[] = { |
| 218 | "ALL", |
| 219 | "DEFAULT", |
| 220 | "ALL:!eNULL", |
| 221 | "ALL:!NULL", |
| 222 | "FIPS", |
| 223 | "SHA", |
| 224 | "SHA1", |
| 225 | "RSA", |
| 226 | "SSLv3", |
| 227 | "TLSv1", |
| 228 | "TLSv1.2", |
| 229 | NULL |
| 230 | }; |
| 231 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 232 | static void PrintCipherPreferenceList(ssl_cipher_preference_list_st *list) { |
| 233 | bool in_group = false; |
| 234 | 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] | 235 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(list->ciphers, i); |
| 236 | if (!in_group && list->in_group_flags[i]) { |
| 237 | fprintf(stderr, "\t[\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 238 | in_group = true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 239 | } |
| 240 | fprintf(stderr, "\t"); |
| 241 | if (in_group) { |
| 242 | fprintf(stderr, " "); |
| 243 | } |
| 244 | fprintf(stderr, "%s\n", SSL_CIPHER_get_name(cipher)); |
| 245 | if (in_group && !list->in_group_flags[i]) { |
| 246 | fprintf(stderr, "\t]\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 247 | in_group = false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 252 | static bool TestCipherRule(CipherTest *t) { |
| 253 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 254 | if (!ctx) { |
| 255 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 256 | } |
| 257 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 258 | if (!SSL_CTX_set_cipher_list(ctx.get(), t->rule)) { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 259 | fprintf(stderr, "Error testing cipher rule '%s'\n", t->rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 260 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 261 | } |
| 262 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 263 | // Compare the two lists. |
| 264 | size_t i; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 265 | for (i = 0; i < sk_SSL_CIPHER_num(ctx->cipher_list->ciphers); i++) { |
| 266 | const SSL_CIPHER *cipher = |
| 267 | sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i); |
| 268 | if (t->expected[i].id != SSL_CIPHER_get_id(cipher) || |
| 269 | t->expected[i].in_group_flag != ctx->cipher_list->in_group_flags[i]) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 270 | fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t->rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 271 | PrintCipherPreferenceList(ctx->cipher_list); |
| 272 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 276 | if (t->expected[i].id != 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 277 | fprintf(stderr, "Error: cipher rule '%s' evaluated to:\n", t->rule); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 278 | PrintCipherPreferenceList(ctx->cipher_list); |
| 279 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 280 | } |
| 281 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 282 | return true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame^] | 285 | static bool TestRuleDoesNotIncludeNull(const char *rule) { |
| 286 | ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_server_method())); |
| 287 | if (!ctx) { |
| 288 | return false; |
| 289 | } |
| 290 | if (!SSL_CTX_set_cipher_list(ctx.get(), rule)) { |
| 291 | fprintf(stderr, "Error: cipher rule '%s' failed\n", rule); |
| 292 | return false; |
| 293 | } |
| 294 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ctx->cipher_list->ciphers); i++) { |
| 295 | if (SSL_CIPHER_is_NULL(sk_SSL_CIPHER_value(ctx->cipher_list->ciphers, i))) { |
| 296 | fprintf(stderr, "Error: cipher rule '%s' includes NULL\n",rule); |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 303 | static bool TestCipherRules() { |
| 304 | for (size_t i = 0; kCipherTests[i].rule != NULL; i++) { |
| 305 | if (!TestCipherRule(&kCipherTests[i])) { |
| 306 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 310 | for (size_t i = 0; kBadRules[i] != NULL; i++) { |
| 311 | ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_server_method())); |
| 312 | if (!ctx) { |
| 313 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 314 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 315 | if (SSL_CTX_set_cipher_list(ctx.get(), kBadRules[i])) { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 316 | fprintf(stderr, "Cipher rule '%s' unexpectedly succeeded\n", kBadRules[i]); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 317 | return false; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 318 | } |
| 319 | ERR_clear_error(); |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame^] | 322 | for (size_t i = 0; kMustNotIncludeNull[i] != NULL; i++) { |
| 323 | if (!TestRuleDoesNotIncludeNull(kMustNotIncludeNull[i])) { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 328 | return true; |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 329 | } |
David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 330 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 331 | // kOpenSSLSession is a serialized SSL_SESSION generated from openssl |
| 332 | // s_client -sess_out. |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 333 | static const char kOpenSSLSession[] = |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 334 | "MIIFpQIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 335 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 336 | "IWoJoQYCBFRDO46iBAICASyjggR6MIIEdjCCA16gAwIBAgIIK9dUvsPWSlUwDQYJ" |
| 337 | "KoZIhvcNAQEFBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx" |
| 338 | "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTQxMDA4" |
| 339 | "MTIwNzU3WhcNMTUwMTA2MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK" |
| 340 | "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v" |
| 341 | "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB" |
| 342 | "AQUAA4IBDwAwggEKAoIBAQCcKeLrplAC+Lofy8t/wDwtB6eu72CVp0cJ4V3lknN6" |
| 343 | "huH9ct6FFk70oRIh/VBNBBz900jYy+7111Jm1b8iqOTQ9aT5C7SEhNcQFJvqzH3e" |
| 344 | "MPkb6ZSWGm1yGF7MCQTGQXF20Sk/O16FSjAynU/b3oJmOctcycWYkY0ytS/k3LBu" |
| 345 | "Id45PJaoMqjB0WypqvNeJHC3q5JjCB4RP7Nfx5jjHSrCMhw8lUMW4EaDxjaR9KDh" |
| 346 | "PLgjsk+LDIySRSRDaCQGhEOWLJZVLzLo4N6/UlctCHEllpBUSvEOyFga52qroGjg" |
| 347 | "rf3WOQ925MFwzd6AK+Ich0gDR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DcyS6P7I6HBYN9B" |
| 362 | "sNHM362zZnY27GpTw+Kwd751CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yE" |
| 363 | "OTDKPNj3+inbMaVigtK4PLyPq+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdA" |
| 364 | "i4gv7Y5oliyn"; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 365 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 366 | // kCustomSession is a custom serialized SSL_SESSION generated by |
| 367 | // filling in missing fields from |kOpenSSLSession|. This includes |
| 368 | // providing |peer_sha256|, so |peer| is not serialized. |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 369 | static const char kCustomSession[] = |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 370 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 371 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 372 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 373 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 374 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 375 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 376 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 377 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 378 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 379 | // kBoringSSLSession is a serialized SSL_SESSION generated from bssl client. |
| 380 | static const char kBoringSSLSession[] = |
| 381 | "MIIRwQIBAQICAwMEAsAvBCDdoGxGK26mR+8lM0uq6+k9xYuxPnwAjpcF9n0Yli9R" |
| 382 | "kQQwbyshfWhdi5XQ1++7n2L1qqrcVlmHBPpr6yknT/u4pUrpQB5FZ7vqvNn8MdHf" |
| 383 | "9rWgoQYCBFXgs7uiBAICHCCjggR6MIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJ" |
| 384 | "KoZIhvcNAQELBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx" |
| 385 | "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEy" |
| 386 | "MTQ1MzE1WhcNMTUxMTEwMDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK" |
| 387 | "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v" |
| 388 | "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB" |
| 389 | "AQUAA4IBDwAwggEKAoIBAQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpo" |
| 390 | "PLuBinvhkXZo3DC133NpCBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU" |
| 391 | "792c7hFyNXSUCG7At8Ifi3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mce" |
| 392 | "Tv9iGKqSkSTlp8puy/9SZ/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/" |
| 393 | "RCh8/UKc8PaL+cxlt531qoTENvYrflBno14YEZlCB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pqA5JLMFSMCChOsvKey3toBaCNGU7HfAEiiXNuuAdCBoK262BjQc2YYfqFzqH" |
| 409 | "zuppopXCvhohx7j/tnCNZIMgLYt/O9SXK2RYI5z8FhCCHvB4CbD5G0LGl5EFP27s" |
| 410 | "Jb6S3aTTYPkQe8yZSlxevg6NDwmTogLO9F7UUkaYmVcMQhzssEE2ZRYNwSOU6KjE" |
| 411 | "0Yj+8fAiBtbQriIEIN2L8ZlpaVrdN5KFNdvcmOxJu81P8q53X55xQyGTnGWwsgMC" |
| 412 | "ARezggvvMIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJKoZIhvcNAQELBQAwSTEL" |
| 413 | "MAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2ds" |
| 414 | "ZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEyMTQ1MzE1WhcNMTUxMTEw" |
| 415 | "MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG" |
| 416 | "A1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UE" |
| 417 | "AwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB" |
| 418 | "AQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpoPLuBinvhkXZo3DC133Np" |
| 419 | "CBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU792c7hFyNXSUCG7At8If" |
| 420 | "i3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mceTv9iGKqSkSTlp8puy/9S" |
| 421 | "Z/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/RCh8/UKc8PaL+cxlt531" |
| 422 | "qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eLEucWQ72YZU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PtXtR+" |
| 452 | "3h3Ef+AlBgDFJPupyC1tft6dgmUsgWM0Zj7pUsIItMsv91+ZOmqcUHqFBYx90SpI" |
| 453 | "hNMJbHzCzTWf84LuUt5oX+QAihcglvcpjZpNy6jehsgNb1aHA30DP9z6eX0hGfnI" |
| 454 | "Oi9RdozHQZJxjyXON/hKTAAj78Q1EK7gI4BzfE00LshukNYQHpmEcxpw8u1VDu4X" |
| 455 | "Bupn7jLrLN1nBz/2i8Jw3lsA5rsb0zYaImxssDVCbJAJPZPpZAkiDoUGn8JzIdPm" |
| 456 | "X4DkjYUiOnMDsWCOrmji9D6X52ASCWg23jrW4kOVWzeBkoEfu43XrVJkFleW2V40" |
| 457 | "fsg12DCCA30wggLmoAMCAQICAxK75jANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQG" |
| 458 | "EwJVUzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUg" |
| 459 | "Q2VydGlmaWNhdGUgQXV0aG9yaXR5MB4XDTAyMDUyMTA0MDAwMFoXDTE4MDgyMTA0" |
| 460 | "MDAwMFowQjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUdlb1RydXN0IEluYy4xGzAZ" |
| 461 | "BgNVBAMTEkdlb1RydXN0IEdsb2JhbCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEP" |
| 462 | "ADCCAQoCggEBANrMGGMw/fQXIxpWflvfPGw45HG3eJHUvKHYTPioQ7YD6U0hBwiI" |
| 463 | "2lgvZjkpvQV4i5046AW3an5xpObEYKaw74DkiSgPniXW7YPzraaRx5jJQhg1FJ2t" |
| 464 | "mEaSLk/K8YdDwRaVVy1Q74ktgHpXrfLuX2vSAI25FPgUFTXZwEaje3LIkb/JVSvN" |
| 465 | "0Jc+nCZkzN/Ogxlxyk7m1NV7qRnNVd7I7NJeOFPlXE+MLf5QIzb8ZubLjqQ5GQC3" |
| 466 | "lQI5kQsO/jgu0R0FmvZNPm8PBx2vLB6PYDni+jZTEznUXiYr2z2oFL0y6xgDKFIE" |
| 467 | "ceWrMz3hOLsHNoRinHnqFjD0X8Ar6HFr5PkCAwEAAaOB8DCB7TAfBgNVHSMEGDAW" |
| 468 | "gBRI5mj5K9KylddH2CMgEE8zmJCf1DAdBgNVHQ4EFgQUwHqYaI2J+6sFZAwRfap9" |
| 469 | "ZbjKzE4wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMw" |
| 470 | "MTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9zZWN1cmVjYS5j" |
| 471 | "cmwwTgYDVR0gBEcwRTBDBgRVHSAAMDswOQYIKwYBBQUHAgEWLWh0dHBzOi8vd3d3" |
| 472 | "Lmdlb3RydXN0LmNvbS9yZXNvdXJjZXMvcmVwb3NpdG9yeTANBgkqhkiG9w0BAQUF" |
| 473 | "AAOBgQB24RJuTksWEoYwBrKBCM/wCMfHcX5m7sLt1Dsf//DwyE7WQziwuTB9GNBV" |
| 474 | "g6JqyzYRnOhIZqNtf7gT1Ef+i1pcc/yu2RsyGTirlzQUqpbS66McFAhJtrvlke+D" |
| 475 | "NusdVm/K2rxzY5Dkf3s+Iss9B+1fOHSc4wNQTqGvmO5h8oQ/Eg=="; |
| 476 | |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 477 | // kBadSessionExtraField is a custom serialized SSL_SESSION generated by replacing |
| 478 | // the final (optional) element of |kCustomSession| with tag number 30. |
| 479 | static const char kBadSessionExtraField[] = |
| 480 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 481 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 482 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 483 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 484 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 485 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 486 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 487 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBL4DBAEF"; |
| 488 | |
David Benjamin | 338e067 | 2015-05-28 20:00:08 -0400 | [diff] [blame] | 489 | // kBadSessionVersion is a custom serialized SSL_SESSION generated by replacing |
| 490 | // the version of |kCustomSession| with 2. |
| 491 | static const char kBadSessionVersion[] = |
| 492 | "MIIBdgIBAgICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 493 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 494 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 495 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 496 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 497 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 498 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 499 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
| 500 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 501 | // kBadSessionTrailingData is a custom serialized SSL_SESSION with trailing data |
| 502 | // appended. |
| 503 | static const char kBadSessionTrailingData[] = |
| 504 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 505 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 506 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 507 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 508 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 509 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 510 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 511 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEFAAAA"; |
| 512 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 513 | static bool DecodeBase64(std::vector<uint8_t> *out, const char *in) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 514 | size_t len; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 515 | if (!EVP_DecodedLength(&len, strlen(in))) { |
| 516 | fprintf(stderr, "EVP_DecodedLength failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 517 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 518 | } |
| 519 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 520 | out->resize(len); |
| 521 | if (!EVP_DecodeBase64(bssl::vector_data(out), &len, len, (const uint8_t *)in, |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 522 | strlen(in))) { |
| 523 | fprintf(stderr, "EVP_DecodeBase64 failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 524 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 525 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 526 | out->resize(len); |
| 527 | return true; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 528 | } |
| 529 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 530 | static bool TestSSL_SESSIONEncoding(const char *input_b64) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 531 | const uint8_t *cptr; |
| 532 | uint8_t *ptr; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 533 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 534 | // Decode the input. |
| 535 | std::vector<uint8_t> input; |
| 536 | if (!DecodeBase64(&input, input_b64)) { |
| 537 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 538 | } |
| 539 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 540 | // Verify the SSL_SESSION decodes. |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 541 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(bssl::vector_data(&input), |
| 542 | input.size())); |
| 543 | if (!session) { |
| 544 | fprintf(stderr, "SSL_SESSION_from_bytes failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 545 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 546 | } |
| 547 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 548 | // Verify the SSL_SESSION encoding round-trips. |
| 549 | size_t encoded_len; |
| 550 | ScopedOpenSSLBytes encoded; |
| 551 | uint8_t *encoded_raw; |
| 552 | if (!SSL_SESSION_to_bytes(session.get(), &encoded_raw, &encoded_len)) { |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 553 | fprintf(stderr, "SSL_SESSION_to_bytes failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 554 | return false; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 555 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 556 | encoded.reset(encoded_raw); |
| 557 | if (encoded_len != input.size() || |
| 558 | memcmp(bssl::vector_data(&input), encoded.get(), input.size()) != 0) { |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 559 | fprintf(stderr, "SSL_SESSION_to_bytes did not round-trip\n"); |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 560 | hexdump(stderr, "Before: ", input.data(), input.size()); |
| 561 | hexdump(stderr, "After: ", encoded_raw, encoded_len); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 562 | return false; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 563 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 564 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 565 | // Verify the SSL_SESSION also decodes with the legacy API. |
| 566 | cptr = bssl::vector_data(&input); |
| 567 | session.reset(d2i_SSL_SESSION(NULL, &cptr, input.size())); |
| 568 | if (!session || cptr != bssl::vector_data(&input) + input.size()) { |
| 569 | fprintf(stderr, "d2i_SSL_SESSION failed\n"); |
| 570 | return false; |
| 571 | } |
| 572 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 573 | // Verify the SSL_SESSION encoding round-trips via the legacy API. |
| 574 | int len = i2d_SSL_SESSION(session.get(), NULL); |
| 575 | if (len < 0 || (size_t)len != input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 576 | fprintf(stderr, "i2d_SSL_SESSION(NULL) returned invalid length\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 577 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 578 | } |
| 579 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 580 | encoded.reset((uint8_t *)OPENSSL_malloc(input.size())); |
| 581 | if (!encoded) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 582 | fprintf(stderr, "malloc failed\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 583 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 584 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 585 | |
| 586 | ptr = encoded.get(); |
| 587 | len = i2d_SSL_SESSION(session.get(), &ptr); |
| 588 | if (len < 0 || (size_t)len != input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 589 | fprintf(stderr, "i2d_SSL_SESSION returned invalid length\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 590 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 591 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 592 | if (ptr != encoded.get() + input.size()) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 593 | fprintf(stderr, "i2d_SSL_SESSION did not advance ptr correctly\n"); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 594 | return false; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 595 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 596 | if (memcmp(bssl::vector_data(&input), encoded.get(), input.size()) != 0) { |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 597 | fprintf(stderr, "i2d_SSL_SESSION did not round-trip\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 | return true; |
David Benjamin | 751e889 | 2014-10-19 00:59:36 -0400 | [diff] [blame] | 602 | } |
| 603 | |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 604 | static bool TestBadSSL_SESSIONEncoding(const char *input_b64) { |
| 605 | std::vector<uint8_t> input; |
| 606 | if (!DecodeBase64(&input, input_b64)) { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | // Verify that the SSL_SESSION fails to decode. |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 611 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(bssl::vector_data(&input), |
| 612 | input.size())); |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 613 | if (session) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 614 | fprintf(stderr, "SSL_SESSION_from_bytes unexpectedly succeeded\n"); |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | ERR_clear_error(); |
| 618 | return true; |
| 619 | } |
| 620 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 621 | static bool TestDefaultVersion(uint16_t version, |
| 622 | const SSL_METHOD *(*method)(void)) { |
| 623 | ScopedSSL_CTX ctx(SSL_CTX_new(method())); |
| 624 | if (!ctx) { |
| 625 | return false; |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 626 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 627 | return ctx->min_version == version && ctx->max_version == version; |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 628 | } |
| 629 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 630 | static bool CipherGetRFCName(std::string *out, uint16_t value) { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 631 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(value); |
| 632 | if (cipher == NULL) { |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 633 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 634 | } |
David Benjamin | 67be048 | 2015-04-20 16:19:00 -0400 | [diff] [blame] | 635 | ScopedOpenSSLString rfc_name(SSL_CIPHER_get_rfc_name(cipher)); |
David Benjamin | 3fa65f0 | 2015-05-15 19:11:57 -0400 | [diff] [blame] | 636 | if (!rfc_name) { |
| 637 | return false; |
| 638 | } |
David Benjamin | 67be048 | 2015-04-20 16:19:00 -0400 | [diff] [blame] | 639 | out->assign(rfc_name.get()); |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 640 | return true; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | typedef struct { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 644 | int id; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 645 | const char *rfc_name; |
| 646 | } CIPHER_RFC_NAME_TEST; |
| 647 | |
| 648 | static const CIPHER_RFC_NAME_TEST kCipherRFCNameTests[] = { |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 649 | { SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA" }, |
| 650 | { SSL3_CK_RSA_RC4_128_MD5, "TLS_RSA_WITH_RC4_MD5" }, |
| 651 | { 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] | 652 | { TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" }, |
| 653 | { TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, |
| 654 | "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" }, |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 655 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
| 656 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" }, |
| 657 | { TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
| 658 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" }, |
| 659 | { TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 660 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" }, |
| 661 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 662 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, |
| 663 | { TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 664 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" }, |
| 665 | { TLS1_CK_PSK_WITH_RC4_128_SHA, "TLS_PSK_WITH_RC4_SHA" }, |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 666 | { TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 667 | "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" }, |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 668 | // These names are non-standard: |
David Benjamin | 2bdb35c | 2015-02-21 11:03:06 -0500 | [diff] [blame] | 669 | { TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, |
| 670 | "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" }, |
| 671 | { TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, |
| 672 | "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" }, |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 673 | }; |
| 674 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 675 | static bool TestCipherGetRFCName(void) { |
| 676 | for (size_t i = 0; |
| 677 | i < sizeof(kCipherRFCNameTests) / sizeof(kCipherRFCNameTests[0]); i++) { |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 678 | const CIPHER_RFC_NAME_TEST *test = &kCipherRFCNameTests[i]; |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 679 | std::string rfc_name; |
| 680 | if (!CipherGetRFCName(&rfc_name, test->id & 0xffff)) { |
| 681 | fprintf(stderr, "SSL_CIPHER_get_rfc_name failed\n"); |
| 682 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 683 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 684 | if (rfc_name != test->rfc_name) { |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 685 | fprintf(stderr, "SSL_CIPHER_get_rfc_name: got '%s', wanted '%s'\n", |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 686 | rfc_name.c_str(), test->rfc_name); |
| 687 | return false; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 688 | } |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 689 | } |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 690 | return true; |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 691 | } |
| 692 | |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 693 | // CreateSessionWithTicket returns a sample |SSL_SESSION| with the ticket |
| 694 | // replaced for one of length |ticket_len| or nullptr on failure. |
| 695 | static ScopedSSL_SESSION CreateSessionWithTicket(size_t ticket_len) { |
| 696 | std::vector<uint8_t> der; |
| 697 | if (!DecodeBase64(&der, kOpenSSLSession)) { |
| 698 | return nullptr; |
| 699 | } |
| 700 | ScopedSSL_SESSION session(SSL_SESSION_from_bytes(bssl::vector_data(&der), |
| 701 | der.size())); |
| 702 | if (!session) { |
| 703 | return nullptr; |
| 704 | } |
| 705 | |
| 706 | // Swap out the ticket for a garbage one. |
| 707 | OPENSSL_free(session->tlsext_tick); |
| 708 | session->tlsext_tick = reinterpret_cast<uint8_t*>(OPENSSL_malloc(ticket_len)); |
| 709 | if (session->tlsext_tick == nullptr) { |
| 710 | return nullptr; |
| 711 | } |
| 712 | memset(session->tlsext_tick, 'a', ticket_len); |
| 713 | session->tlsext_ticklen = ticket_len; |
| 714 | return session; |
| 715 | } |
| 716 | |
| 717 | // GetClientHelloLen creates a client SSL connection with a ticket of length |
| 718 | // |ticket_len| and records the ClientHello. It returns the length of the |
| 719 | // ClientHello, not including the record header, on success and zero on error. |
| 720 | static size_t GetClientHelloLen(size_t ticket_len) { |
| 721 | ScopedSSL_CTX ctx(SSL_CTX_new(TLS_method())); |
| 722 | ScopedSSL_SESSION session = CreateSessionWithTicket(ticket_len); |
| 723 | if (!ctx || !session) { |
| 724 | return 0; |
| 725 | } |
| 726 | ScopedSSL ssl(SSL_new(ctx.get())); |
| 727 | ScopedBIO bio(BIO_new(BIO_s_mem())); |
| 728 | if (!ssl || !bio || !SSL_set_session(ssl.get(), session.get())) { |
| 729 | return 0; |
| 730 | } |
| 731 | // Do not configure a reading BIO, but record what's written to a memory BIO. |
| 732 | SSL_set_bio(ssl.get(), nullptr /* rbio */, BIO_up_ref(bio.get())); |
| 733 | int ret = SSL_connect(ssl.get()); |
| 734 | if (ret > 0) { |
| 735 | // SSL_connect should fail without a BIO to write to. |
| 736 | return 0; |
| 737 | } |
| 738 | ERR_clear_error(); |
| 739 | |
| 740 | const uint8_t *unused; |
| 741 | size_t client_hello_len; |
| 742 | if (!BIO_mem_contents(bio.get(), &unused, &client_hello_len) || |
| 743 | client_hello_len <= SSL3_RT_HEADER_LENGTH) { |
| 744 | return 0; |
| 745 | } |
| 746 | return client_hello_len - SSL3_RT_HEADER_LENGTH; |
| 747 | } |
| 748 | |
| 749 | struct PaddingTest { |
| 750 | size_t input_len, padded_len; |
| 751 | }; |
| 752 | |
| 753 | static const PaddingTest kPaddingTests[] = { |
| 754 | // ClientHellos of length below 0x100 do not require padding. |
| 755 | {0xfe, 0xfe}, |
| 756 | {0xff, 0xff}, |
| 757 | // ClientHellos of length 0x100 through 0x1fb are padded up to 0x200. |
| 758 | {0x100, 0x200}, |
| 759 | {0x123, 0x200}, |
| 760 | {0x1fb, 0x200}, |
| 761 | // ClientHellos of length 0x1fc through 0x1ff get padded beyond 0x200. The |
| 762 | // padding extension takes a minimum of four bytes plus one required content |
| 763 | // byte. (To work around yet more server bugs, we avoid empty final |
| 764 | // extensions.) |
| 765 | {0x1fc, 0x201}, |
| 766 | {0x1fd, 0x202}, |
| 767 | {0x1fe, 0x203}, |
| 768 | {0x1ff, 0x204}, |
| 769 | // Finally, larger ClientHellos need no padding. |
| 770 | {0x200, 0x200}, |
| 771 | {0x201, 0x201}, |
| 772 | }; |
| 773 | |
| 774 | static bool TestPaddingExtension() { |
| 775 | // Sample a baseline length. |
| 776 | size_t base_len = GetClientHelloLen(1); |
| 777 | if (base_len == 0) { |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | for (const PaddingTest &test : kPaddingTests) { |
| 782 | if (base_len > test.input_len) { |
| 783 | fprintf(stderr, "Baseline ClientHello too long.\n"); |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | size_t padded_len = GetClientHelloLen(1 + test.input_len - base_len); |
| 788 | if (padded_len != test.padded_len) { |
| 789 | fprintf(stderr, "%u-byte ClientHello padded to %u bytes, not %u.\n", |
| 790 | static_cast<unsigned>(test.input_len), |
| 791 | static_cast<unsigned>(padded_len), |
| 792 | static_cast<unsigned>(test.padded_len)); |
| 793 | return false; |
| 794 | } |
| 795 | } |
| 796 | return true; |
| 797 | } |
| 798 | |
David Benjamin | c44d2f4 | 2014-08-20 16:24:00 -0400 | [diff] [blame] | 799 | int main(void) { |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 800 | SSL_library_init(); |
| 801 | |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 802 | if (!TestCipherRules() || |
| 803 | !TestSSL_SESSIONEncoding(kOpenSSLSession) || |
| 804 | !TestSSL_SESSIONEncoding(kCustomSession) || |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 805 | !TestSSL_SESSIONEncoding(kBoringSSLSession) || |
David Benjamin | f297e02 | 2015-05-28 19:55:29 -0400 | [diff] [blame] | 806 | !TestBadSSL_SESSIONEncoding(kBadSessionExtraField) || |
David Benjamin | 338e067 | 2015-05-28 20:00:08 -0400 | [diff] [blame] | 807 | !TestBadSSL_SESSIONEncoding(kBadSessionVersion) || |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 808 | !TestBadSSL_SESSIONEncoding(kBadSessionTrailingData) || |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 809 | !TestDefaultVersion(0, &TLS_method) || |
| 810 | !TestDefaultVersion(SSL3_VERSION, &SSLv3_method) || |
| 811 | !TestDefaultVersion(TLS1_VERSION, &TLSv1_method) || |
| 812 | !TestDefaultVersion(TLS1_1_VERSION, &TLSv1_1_method) || |
David Benjamin | 6879373 | 2015-05-04 20:20:48 -0400 | [diff] [blame] | 813 | !TestDefaultVersion(TLS1_2_VERSION, &TLSv1_2_method) || |
David Benjamin | 1d77e56 | 2015-03-22 17:22:08 -0400 | [diff] [blame] | 814 | !TestDefaultVersion(0, &DTLS_method) || |
| 815 | !TestDefaultVersion(DTLS1_VERSION, &DTLSv1_method) || |
| 816 | !TestDefaultVersion(DTLS1_2_VERSION, &DTLSv1_2_method) || |
David Benjamin | 422fe08 | 2015-07-21 22:03:43 -0400 | [diff] [blame] | 817 | !TestCipherGetRFCName() || |
| 818 | !TestPaddingExtension()) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 819 | ERR_print_errors_fp(stderr); |
David Benjamin | bb0a17c | 2014-09-20 15:35:39 -0400 | [diff] [blame] | 820 | return 1; |
| 821 | } |
| 822 | |
David Benjamin | 2e52121 | 2014-07-16 14:37:51 -0400 | [diff] [blame] | 823 | printf("PASS\n"); |
| 824 | return 0; |
| 825 | } |