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