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