| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <limits.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 |
| 20 | #include <algorithm> |
| 21 | #include <limits> |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | #include <vector> |
| 25 |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 |
| 29 | #include <openssl/aead.h> |
| 30 | #include <openssl/base64.h> |
| 31 | #include <openssl/bio.h> |
| 32 | #include <openssl/bytestring.h> |
| 33 | #include <openssl/cipher.h> |
| 34 | #include <openssl/crypto.h> |
| 35 | #include <openssl/curve25519.h> |
| 36 | #include <openssl/err.h> |
| 37 | #include <openssl/hmac.h> |
| 38 | #include <openssl/hpke.h> |
| 39 | #include <openssl/pem.h> |
| 40 | #include <openssl/rand.h> |
| 41 | #include <openssl/sha.h> |
| 42 | #include <openssl/ssl.h> |
| 43 | #include <openssl/x509.h> |
| 44 |
| 45 | #include "../crypto/internal.h" |
| 46 | #include "../crypto/test/file_util.h" |
| 47 | #include "../crypto/test/test_util.h" |
| 48 | #include "internal.h" |
| 49 |
| 50 | #if defined(OPENSSL_WINDOWS) |
| 51 | // Windows defines struct timeval in winsock2.h. |
| 52 | #include <winsock2.h> |
| 53 | #else |
| 54 | #include <sys/time.h> |
| 55 | #endif |
| 56 |
| 57 | #if defined(OPENSSL_THREADS) |
| 58 | #include <thread> |
| 59 | #endif |
| 60 |
| 61 |
| 62 | using testing::ElementsAre; |
| 63 | using testing::Key; |
| 64 |
| 65 | BSSL_NAMESPACE_BEGIN |
| 66 |
| 67 | namespace { |
| 68 |
| 69 | #define TRACED_CALL(code) \ |
| 70 | do { \ |
| 71 | SCOPED_TRACE("<- called from here"); \ |
| 72 | code; \ |
| 73 | if (::testing::Test::HasFatalFailure()) { \ |
| 74 | return; \ |
| 75 | } \ |
| 76 | } while (false) |
| 77 |
| 78 | struct VersionParam { |
| 79 | uint16_t version; |
| 80 | enum { is_tls, is_dtls } ssl_method; |
| 81 | const char name[8]; |
| 82 | }; |
| 83 |
| 84 | static const size_t kTicketKeyLen = 48; |
| 85 |
| 86 | static const VersionParam kAllVersions[] = { |
| 87 | {TLS1_VERSION, VersionParam::is_tls, "TLS1"}, |
| 88 | {TLS1_1_VERSION, VersionParam::is_tls, "TLS1_1"}, |
| 89 | {TLS1_2_VERSION, VersionParam::is_tls, "TLS1_2"}, |
| 90 | {TLS1_3_VERSION, VersionParam::is_tls, "TLS1_3"}, |
| 91 | {DTLS1_VERSION, VersionParam::is_dtls, "DTLS1"}, |
| 92 | {DTLS1_2_VERSION, VersionParam::is_dtls, "DTLS1_2"}, |
| 93 | {DTLS1_3_VERSION, VersionParam::is_dtls, "DTLS1_3"}, |
| 94 | }; |
| 95 |
| 96 | struct ExpectedCipher { |
| 97 | unsigned long id; |
| 98 | int in_group_flag; |
| 99 | }; |
| 100 |
| 101 | struct CipherTest { |
| 102 | // The rule string to apply. |
| 103 | const char *rule; |
| 104 | // The list of expected ciphers, in order. |
| 105 | std::vector<ExpectedCipher> expected; |
| 106 | // True if this cipher list should fail in strict mode. |
| 107 | bool strict_fail; |
| 108 | }; |
| 109 |
| 110 | struct CurveTest { |
| 111 | // The rule string to apply. |
| 112 | const char *rule; |
| 113 | // The list of expected curves, in order. |
| 114 | std::vector<uint16_t> expected; |
| 115 | }; |
| 116 |
| 117 | template <typename T> |
| 118 | class UnownedSSLExData { |
| 119 | public: |
| 120 | UnownedSSLExData() { |
| 121 | index_ = SSL_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); |
| 122 | } |
| 123 |
| 124 | T *Get(const SSL *ssl) { |
| 125 | return index_ < 0 ? nullptr |
| 126 | : static_cast<T *>(SSL_get_ex_data(ssl, index_)); |
| 127 | } |
| 128 |
| 129 | bool Set(SSL *ssl, T *t) { |
| 130 | return index_ >= 0 && SSL_set_ex_data(ssl, index_, t); |
| 131 | } |
| 132 |
| 133 | private: |
| 134 | int index_; |
| 135 | }; |
| 136 |
| 137 | static const CipherTest kCipherTests[] = { |
| 138 | // Selecting individual ciphers should work. |
| 139 | { |
| 140 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 141 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 142 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 143 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 144 | { |
| 145 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 146 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 147 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 148 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 149 | }, |
| 150 | false, |
| 151 | }, |
| 152 | // + reorders selected ciphers to the end, keeping their relative order. |
| 153 | { |
| 154 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 155 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 156 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 157 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 158 | "+aRSA", |
| 159 | { |
| 160 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 161 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 162 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 163 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 164 | }, |
| 165 | false, |
| 166 | }, |
| 167 | // ! banishes ciphers from future selections. |
| 168 | { |
| 169 | "!aRSA:" |
| 170 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 171 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 172 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 173 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 174 | { |
| 175 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 176 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 177 | }, |
| 178 | false, |
| 179 | }, |
| 180 | // Multiple masks can be ANDed in a single rule. |
| 181 | { |
| 182 | "kRSA+AESGCM+AES128", |
| 183 | { |
| 184 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 185 | }, |
| 186 | false, |
| 187 | }, |
| 188 | // - removes selected ciphers, but preserves their order for future |
| 189 | // selections. Select AES_128_GCM, but order the key exchanges RSA, |
| 190 | // ECDHE_RSA. |
| 191 | { |
| 192 | "ALL:-kECDHE:" |
| 193 | "-kRSA:-ALL:" |
| 194 | "AESGCM+AES128+aRSA", |
| 195 | { |
| 196 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 197 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 198 | }, |
| 199 | false, |
| 200 | }, |
| 201 | // Unknown selectors are no-ops, except in strict mode. |
| 202 | { |
| 203 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 204 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 205 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 206 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 207 | "BOGUS1", |
| 208 | { |
| 209 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 210 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 211 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 212 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 213 | }, |
| 214 | true, |
| 215 | }, |
| 216 | // Unknown selectors are no-ops, except in strict mode. |
| 217 | { |
| 218 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 219 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 220 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 221 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 222 | "-BOGUS2:+BOGUS3:!BOGUS4", |
| 223 | { |
| 224 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 225 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 226 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 227 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 228 | }, |
| 229 | true, |
| 230 | }, |
| 231 | // Square brackets specify equi-preference groups. |
| 232 | { |
| 233 | "[ECDHE-ECDSA-CHACHA20-POLY1305|ECDHE-ECDSA-AES128-GCM-SHA256]:" |
| 234 | "[ECDHE-RSA-CHACHA20-POLY1305]:" |
| 235 | "ECDHE-RSA-AES128-GCM-SHA256", |
| 236 | { |
| 237 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1}, |
| 238 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 239 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 240 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 241 | }, |
| 242 | false, |
| 243 | }, |
| 244 | // Standard names may be used instead of OpenSSL names. |
| 245 | { |
| 246 | "[TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256|" |
| 247 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256]:" |
| 248 | "[TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256]:" |
| 249 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", |
| 250 | { |
| 251 | {TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 1}, |
| 252 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 253 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 254 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 255 | }, |
| 256 | false, |
| 257 | }, |
| 258 | // @STRENGTH performs a stable strength-sort of the selected ciphers and |
| 259 | // only the selected ciphers. |
| 260 | { |
| 261 | // To simplify things, banish all but {ECDHE_RSA,RSA} x |
| 262 | // {CHACHA20,AES_256_CBC,AES_128_CBC} x SHA1. |
| 263 | "!AESGCM:!3DES:" |
| 264 | // Order some ciphers backwards by strength. |
| 265 | "ALL:-CHACHA20:-AES256:-AES128:-ALL:" |
| 266 | // Select ECDHE ones and sort them by strength. Ties should resolve |
| 267 | // based on the order above. |
| 268 | "kECDHE:@STRENGTH:-ALL:" |
| 269 | // Now bring back everything uses RSA. ECDHE_RSA should be first, sorted |
| 270 | // by strength. Then RSA, backwards by strength. |
| 271 | "aRSA", |
| 272 | { |
| 273 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, 0}, |
| 274 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 275 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, 0}, |
| 276 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 277 | {TLS1_CK_RSA_WITH_AES_256_SHA, 0}, |
| 278 | }, |
| 279 | false, |
| 280 | }, |
| 281 | // Additional masks after @STRENGTH get silently discarded. |
| 282 | // |
| 283 | // TODO(davidben): Make this an error. If not silently discarded, they get |
| 284 | // interpreted as + opcodes which are very different. |
| 285 | { |
| 286 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 287 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 288 | "@STRENGTH+AES256", |
| 289 | { |
| 290 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 0}, |
| 291 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 292 | }, |
| 293 | false, |
| 294 | }, |
| 295 | { |
| 296 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 297 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 298 | "@STRENGTH+AES256:" |
| 299 | "ECDHE-RSA-CHACHA20-POLY1305", |
| 300 | { |
| 301 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 0}, |
| 302 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 303 | {TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 0}, |
| 304 | }, |
| 305 | false, |
| 306 | }, |
| 307 | // Exact ciphers may not be used in multi-part rules; they are treated |
| 308 | // as unknown aliases. |
| 309 | { |
| 310 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 311 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 312 | "!ECDHE-RSA-AES128-GCM-SHA256+RSA:" |
| 313 | "!ECDSA+ECDHE-ECDSA-AES128-GCM-SHA256", |
| 314 | { |
| 315 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 316 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 317 | }, |
| 318 | true, |
| 319 | }, |
| 320 | // SSLv3 matches everything that existed before TLS 1.2. |
| 321 | { |
| 322 | "AES128-SHA:ECDHE-RSA-AES128-GCM-SHA256:!SSLv3", |
| 323 | { |
| 324 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 325 | }, |
| 326 | false, |
| 327 | }, |
| 328 | // TLSv1.2 matches everything added in TLS 1.2. |
| 329 | { |
| 330 | "AES128-SHA:ECDHE-RSA-AES128-GCM-SHA256:!TLSv1.2", |
| 331 | { |
| 332 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 333 | }, |
| 334 | false, |
| 335 | }, |
| 336 | // The two directives have no intersection. But each component is valid, so |
| 337 | // even in strict mode it is accepted. |
| 338 | { |
| 339 | "AES128-SHA:ECDHE-RSA-AES128-GCM-SHA256:!TLSv1.2+SSLv3", |
| 340 | { |
| 341 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 342 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 343 | }, |
| 344 | false, |
| 345 | }, |
| 346 | // Spaces, semi-colons and commas are separators. |
| 347 | { |
| 348 | "AES128-SHA: ECDHE-RSA-AES128-GCM-SHA256 AES256-SHA " |
| 349 | ",ECDHE-ECDSA-AES128-GCM-SHA256 ; AES128-GCM-SHA256", |
| 350 | { |
| 351 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 352 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 353 | {TLS1_CK_RSA_WITH_AES_256_SHA, 0}, |
| 354 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 355 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 356 | }, |
| 357 | // …but not in strict mode. |
| 358 | true, |
| 359 | }, |
| 360 | // 3DES ciphers are disabled by default. |
| 361 | { |
| 362 | "RSA", |
| 363 | { |
| 364 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 365 | {TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, 0}, |
| 366 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 367 | {TLS1_CK_RSA_WITH_AES_256_SHA, 0}, |
| 368 | }, |
| 369 | false, |
| 370 | }, |
| 371 | // But 3DES ciphers may be specified by name. |
| 372 | { |
| 373 | "TLS_RSA_WITH_3DES_EDE_CBC_SHA", |
| 374 | { |
| 375 | {SSL3_CK_RSA_DES_192_CBC3_SHA, 0}, |
| 376 | }, |
| 377 | false, |
| 378 | }, |
| 379 | { |
| 380 | "DES-CBC3-SHA", |
| 381 | { |
| 382 | {SSL3_CK_RSA_DES_192_CBC3_SHA, 0}, |
| 383 | }, |
| 384 | false, |
| 385 | }, |
| 386 | // Or by a selector that specifically includes deprecated ciphers. |
| 387 | { |
| 388 | "3DES", |
| 389 | { |
| 390 | {SSL3_CK_RSA_DES_192_CBC3_SHA, 0}, |
| 391 | }, |
| 392 | false, |
| 393 | }, |
| 394 | // Such selectors may be combined with other selectors that would otherwise |
| 395 | // not allow deprecated ciphers. |
| 396 | { |
| 397 | "RSA+3DES", |
| 398 | { |
| 399 | {SSL3_CK_RSA_DES_192_CBC3_SHA, 0}, |
| 400 | }, |
| 401 | false, |
| 402 | }, |
| 403 | // The cipher must still match all combined selectors, however. "ECDHE+3DES" |
| 404 | // matches nothing because we do not implement |
| 405 | // TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA. (The test includes |
| 406 | // TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 so the final list is not empty.) |
| 407 | { |
| 408 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:ECDHE+3DES", |
| 409 | { |
| 410 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 411 | }, |
| 412 | false, |
| 413 | }, |
| 414 | // Although alises like "RSA" do not match 3DES when adding ciphers, they do |
| 415 | // match it when removing ciphers. |
| 416 | { |
| 417 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:RSA:RSA+3DES:!RSA", |
| 418 | { |
| 419 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 0}, |
| 420 | }, |
| 421 | false, |
| 422 | }, |
| 423 | // 3DES still participates in strength sorting. |
| 424 | { |
| 425 | "RSA:3DES:@STRENGTH", |
| 426 | { |
| 427 | {TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, 0}, |
| 428 | {TLS1_CK_RSA_WITH_AES_256_SHA, 0}, |
| 429 | {TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, 0}, |
| 430 | {TLS1_CK_RSA_WITH_AES_128_SHA, 0}, |
| 431 | {SSL3_CK_RSA_DES_192_CBC3_SHA, 0}, |
| 432 | }, |
| 433 | false, |
| 434 | }, |
| 435 | }; |
| 436 |
| 437 | static const char *kBadRules[] = { |
| 438 | // Invalid brackets. |
| 439 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256", |
| 440 | "RSA]", |
| 441 | "[[RSA]]", |
| 442 | // Operators inside brackets. |
| 443 | "[+RSA]", |
| 444 | // Unknown directive. |
| 445 | "@BOGUS", |
| 446 | // Empty cipher lists error at SSL_CTX_set_cipher_list. |
| 447 | "", |
| 448 | "BOGUS", |
| 449 | // COMPLEMENTOFDEFAULT is empty. |
| 450 | "COMPLEMENTOFDEFAULT", |
| 451 | // Invalid command. |
| 452 | "?BAR", |
| 453 | // Special operators are not allowed if equi-preference groups are used. |
| 454 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:+FOO", |
| 455 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:!FOO", |
| 456 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:-FOO", |
| 457 | "[ECDHE-RSA-CHACHA20-POLY1305|ECDHE-RSA-AES128-GCM-SHA256]:@STRENGTH", |
| 458 | // Opcode supplied, but missing selector. |
| 459 | "+", |
| 460 | // Spaces are forbidden in equal-preference groups. |
| 461 | "[AES128-SHA | AES128-SHA256]", |
| 462 | }; |
| 463 |
| 464 | static const char *kMustNotIncludeDeprecated[] = { |
| 465 | "ALL", "DEFAULT", "HIGH", "FIPS", "SHA", |
| 466 | "SHA1", "RSA", "SSLv3", "TLSv1", "TLSv1.2", |
| 467 | }; |
| 468 |
| 469 | static const char *kShouldIncludeCBCSHA256[] = { |
| 470 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", |
| 471 | "ALL:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", |
| 472 | }; |
| 473 |
| 474 | static const CurveTest kCurveTests[] = { |
| 475 | { |
| 476 | "P-256", |
| 477 | {SSL_GROUP_SECP256R1}, |
| 478 | }, |
| 479 | { |
| 480 | "P-256:X25519Kyber768Draft00", |
| 481 | {SSL_GROUP_SECP256R1, SSL_GROUP_X25519_KYBER768_DRAFT00}, |
| 482 | }, |
| 483 | { |
| 484 | "P-256:X25519MLKEM768", |
| 485 | {SSL_GROUP_SECP256R1, SSL_GROUP_X25519_MLKEM768}, |
| 486 | }, |
| 487 |
| 488 | { |
| 489 | "P-256:P-384:P-521:X25519", |
| 490 | { |
| 491 | SSL_GROUP_SECP256R1, |
| 492 | SSL_GROUP_SECP384R1, |
| 493 | SSL_GROUP_SECP521R1, |
| 494 | SSL_GROUP_X25519, |
| 495 | }, |
| 496 | }, |
| 497 | { |
| 498 | "prime256v1:secp384r1:secp521r1:x25519", |
| 499 | { |
| 500 | SSL_GROUP_SECP256R1, |
| 501 | SSL_GROUP_SECP384R1, |
| 502 | SSL_GROUP_SECP521R1, |
| 503 | SSL_GROUP_X25519, |
| 504 | }, |
| 505 | }, |
| 506 | }; |
| 507 |
| 508 | static const char *kBadCurvesLists[] = { |
| 509 | "", |
| 510 | ":", |
| 511 | "::", |
| 512 | "P-256::X25519", |
| 513 | "RSA:P-256", |
| 514 | "P-256:RSA", |
| 515 | "X25519:P-256:", |
| 516 | ":X25519:P-256", |
| 517 | }; |
| 518 |
| 519 | static std::string CipherListToString(SSL_CTX *ctx) { |
| 520 | bool in_group = false; |
| 521 | std::string ret; |
| 522 | const STACK_OF(SSL_CIPHER) *ciphers = SSL_CTX_get_ciphers(ctx); |
| 523 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 524 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 525 | if (!in_group && SSL_CTX_cipher_in_group(ctx, i)) { |
| 526 | ret += "\t[\n"; |
| 527 | in_group = true; |
| 528 | } |
| 529 | ret += "\t"; |
| 530 | if (in_group) { |
| 531 | ret += " "; |
| 532 | } |
| 533 | ret += SSL_CIPHER_get_name(cipher); |
| 534 | ret += "\n"; |
| 535 | if (in_group && !SSL_CTX_cipher_in_group(ctx, i)) { |
| 536 | ret += "\t]\n"; |
| 537 | in_group = false; |
| 538 | } |
| 539 | } |
| 540 | return ret; |
| 541 | } |
| 542 |
| 543 | static bool CipherListsEqual(SSL_CTX *ctx, |
| 544 | const std::vector<ExpectedCipher> &expected) { |
| 545 | const STACK_OF(SSL_CIPHER) *ciphers = SSL_CTX_get_ciphers(ctx); |
| 546 | if (sk_SSL_CIPHER_num(ciphers) != expected.size()) { |
| 547 | return false; |
| 548 | } |
| 549 |
| 550 | for (size_t i = 0; i < expected.size(); i++) { |
| 551 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 552 | if (expected[i].id != SSL_CIPHER_get_id(cipher) || |
| 553 | expected[i].in_group_flag != !!SSL_CTX_cipher_in_group(ctx, i)) { |
| 554 | return false; |
| 555 | } |
| 556 | } |
| 557 |
| 558 | return true; |
| 559 | } |
| 560 |
| 561 | TEST(SSLTest, CipherRules) { |
| 562 | for (const CipherTest &t : kCipherTests) { |
| 563 | SCOPED_TRACE(t.rule); |
| 564 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 565 | ASSERT_TRUE(ctx); |
| 566 |
| 567 | // Test lax mode. |
| 568 | ASSERT_TRUE(SSL_CTX_set_cipher_list(ctx.get(), t.rule)); |
| 569 | EXPECT_TRUE(CipherListsEqual(ctx.get(), t.expected)) |
| 570 | << "Cipher rule evaluated to:\n" |
| 571 | << CipherListToString(ctx.get()); |
| 572 |
| 573 | // Test strict mode. |
| 574 | if (t.strict_fail) { |
| 575 | EXPECT_FALSE(SSL_CTX_set_strict_cipher_list(ctx.get(), t.rule)); |
| 576 | } else { |
| 577 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), t.rule)); |
| 578 | EXPECT_TRUE(CipherListsEqual(ctx.get(), t.expected)) |
| 579 | << "Cipher rule evaluated to:\n" |
| 580 | << CipherListToString(ctx.get()); |
| 581 | } |
| 582 | } |
| 583 |
| 584 | for (const char *rule : kBadRules) { |
| 585 | SCOPED_TRACE(rule); |
| 586 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 587 | ASSERT_TRUE(ctx); |
| 588 |
| 589 | EXPECT_FALSE(SSL_CTX_set_cipher_list(ctx.get(), rule)); |
| 590 | ERR_clear_error(); |
| 591 | } |
| 592 |
| 593 | for (const char *rule : kMustNotIncludeDeprecated) { |
| 594 | SCOPED_TRACE(rule); |
| 595 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 596 | ASSERT_TRUE(ctx); |
| 597 |
| 598 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), rule)); |
| 599 | for (const SSL_CIPHER *cipher : SSL_CTX_get_ciphers(ctx.get())) { |
| 600 | EXPECT_NE(NID_undef, SSL_CIPHER_get_cipher_nid(cipher)); |
| 601 | EXPECT_FALSE(ssl_cipher_is_deprecated(cipher)); |
| 602 | } |
| 603 | } |
| 604 |
| 605 | { |
| 606 | for (const char *rule : kShouldIncludeCBCSHA256) { |
| 607 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 608 | ASSERT_TRUE(ctx); |
| 609 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), rule)); |
| 610 |
| 611 | bool found = false; |
| 612 | for (const SSL_CIPHER *cipher : SSL_CTX_get_ciphers(ctx.get())) { |
| 613 | if ((TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA256 & 0xffff) == |
| 614 | SSL_CIPHER_get_protocol_id(cipher)) { |
| 615 | found = true; |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | EXPECT_TRUE(found); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 |
| 624 | TEST(SSLTest, CurveRules) { |
| 625 | for (const CurveTest &t : kCurveTests) { |
| 626 | SCOPED_TRACE(t.rule); |
| 627 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 628 | ASSERT_TRUE(ctx); |
| 629 |
| 630 | ASSERT_TRUE(SSL_CTX_set1_groups_list(ctx.get(), t.rule)); |
| 631 | ASSERT_EQ(t.expected.size(), ctx->supported_group_list.size()); |
| 632 | for (size_t i = 0; i < t.expected.size(); i++) { |
| 633 | EXPECT_EQ(t.expected[i], ctx->supported_group_list[i]); |
| 634 | } |
| 635 | } |
| 636 |
| 637 | for (const char *rule : kBadCurvesLists) { |
| 638 | SCOPED_TRACE(rule); |
| 639 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 640 | ASSERT_TRUE(ctx); |
| 641 |
| 642 | EXPECT_FALSE(SSL_CTX_set1_groups_list(ctx.get(), rule)); |
| 643 | ERR_clear_error(); |
| 644 | } |
| 645 | } |
| 646 |
| 647 | // kOpenSSLSession is a serialized SSL_SESSION. |
| 648 | static const char kOpenSSLSession[] = |
| 649 | "MIIFqgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 650 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 651 | "IWoJoQYCBFRDO46iBAICASyjggR6MIIEdjCCA16gAwIBAgIIK9dUvsPWSlUwDQYJ" |
| 652 | "KoZIhvcNAQEFBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx" |
| 653 | "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTQxMDA4" |
| 654 | "MTIwNzU3WhcNMTUwMTA2MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK" |
| 655 | "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v" |
| 656 | "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB" |
| 657 | "AQUAA4IBDwAwggEKAoIBAQCcKeLrplAC+Lofy8t/wDwtB6eu72CVp0cJ4V3lknN6" |
| 658 | "huH9ct6FFk70oRIh/VBNBBz900jYy+7111Jm1b8iqOTQ9aT5C7SEhNcQFJvqzH3e" |
| 659 | "MPkb6ZSWGm1yGF7MCQTGQXF20Sk/O16FSjAynU/b3oJmOctcycWYkY0ytS/k3LBu" |
| 660 | "Id45PJaoMqjB0WypqvNeJHC3q5JjCB4RP7Nfx5jjHSrCMhw8lUMW4EaDxjaR9KDh" |
| 661 | "PLgjsk+LDIySRSRDaCQGhEOWLJZVLzLo4N6/UlctCHEllpBUSvEOyFga52qroGjg" |
| 662 | "rf3WOQ925MFwzd6AK+Ich0gDRg8sQfdLH5OuP1cfLfU1AgMBAAGjggFBMIIBPTAd" |
| 663 | "BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdv" |
| 664 | "b2dsZS5jb20waAYIKwYBBQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtp" |
| 665 | "Lmdvb2dsZS5jb20vR0lBRzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50" |
| 666 | "czEuZ29vZ2xlLmNvbS9vY3NwMB0GA1UdDgQWBBQ7a+CcxsZByOpc+xpYFcIbnUMZ" |
| 667 | "hTAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEv" |
| 668 | "MBcGA1UdIAQQMA4wDAYKKwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRw" |
| 669 | "Oi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBBQUAA4IBAQCa" |
| 670 | "OXCBdoqUy5bxyq+Wrh1zsyyCFim1PH5VU2+yvDSWrgDY8ibRGJmfff3r4Lud5kal" |
| 671 | "dKs9k8YlKD3ITG7P0YT/Rk8hLgfEuLcq5cc0xqmE42xJ+Eo2uzq9rYorc5emMCxf" |
| 672 | "5L0TJOXZqHQpOEcuptZQ4OjdYMfSxk5UzueUhA3ogZKRcRkdB3WeWRp+nYRhx4St" |
| 673 | "o2rt2A0MKmY9165GHUqMK9YaaXHDXqBu7Sefr1uSoAP9gyIJKeihMivsGqJ1TD6Z" |
| 674 | "cc6LMe+dN2P8cZEQHtD1y296ul4Mivqk3jatUVL8/hCwgch9A8O4PGZq9WqBfEWm" |
| 675 | "IyHh1dPtbg1lOXdYCWtjpAIEAKUDAgEUqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36S" |
| 676 | "YTcLEkXqKwOBfF9vE4KX0NxeLwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9B" |
| 677 | "sNHM362zZnY27GpTw+Kwd751CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yE" |
| 678 | "OTDKPNj3+inbMaVigtK4PLyPq+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdA" |
| 679 | "i4gv7Y5oliyntgMBAQA="; |
| 680 |
| 681 | // kCustomSession is a custom serialized SSL_SESSION generated by |
| 682 | // filling in missing fields from |kOpenSSLSession|. This includes |
| 683 | // providing |peer_sha256|, so |peer| is not serialized. |
| 684 | static const char kCustomSession[] = |
| 685 | "MIIBZAIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 686 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 687 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUqAcEBXdvcmxkqQUCAwGJwKqBpwSB" |
| 688 | "pBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0NxeLwjcDTpsuh3qXEaZ992r1N38" |
| 689 | "VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751CLoXFPoaMOe57dbBpXoro6Pd" |
| 690 | "3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyPq+Topyzvx9USFgRvyuoxn0Hg" |
| 691 | "b+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYGBgYGBgYGBgYGBgYGBgYGBgYG" |
| 692 | "BgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
| 693 |
| 694 | // kBoringSSLSession is a serialized SSL_SESSION generated from bssl client. |
| 695 | static const char kBoringSSLSession[] = |
| 696 | "MIIRwQIBAQICAwMEAsAvBCDdoGxGK26mR+8lM0uq6+k9xYuxPnwAjpcF9n0Yli9R" |
| 697 | "kQQwbyshfWhdi5XQ1++7n2L1qqrcVlmHBPpr6yknT/u4pUrpQB5FZ7vqvNn8MdHf" |
| 698 | "9rWgoQYCBFXgs7uiBAICHCCjggR6MIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJ" |
| 699 | "KoZIhvcNAQELBQAwSTELMAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMx" |
| 700 | "JTAjBgNVBAMTHEdvb2dsZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEy" |
| 701 | "MTQ1MzE1WhcNMTUxMTEwMDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwK" |
| 702 | "Q2FsaWZvcm5pYTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29v" |
| 703 | "Z2xlIEluYzEXMBUGA1UEAwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEB" |
| 704 | "AQUAA4IBDwAwggEKAoIBAQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpo" |
| 705 | "PLuBinvhkXZo3DC133NpCBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU" |
| 706 | "792c7hFyNXSUCG7At8Ifi3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mce" |
| 707 | "Tv9iGKqSkSTlp8puy/9SZ/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/" |
| 708 | "RCh8/UKc8PaL+cxlt531qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eL" |
| 709 | "EucWQ72YZU8mUzXBoXGn0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAd" |
| 710 | "BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdv" |
| 711 | "b2dsZS5jb20waAYIKwYBBQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtp" |
| 712 | "Lmdvb2dsZS5jb20vR0lBRzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50" |
| 713 | "czEuZ29vZ2xlLmNvbS9vY3NwMB0GA1UdDgQWBBS/bzHxcE73Q4j3slC4BLbMtLjG" |
| 714 | "GjAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEv" |
| 715 | "MBcGA1UdIAQQMA4wDAYKKwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRw" |
| 716 | "Oi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBCwUAA4IBAQAb" |
| 717 | "qdWPZEHk0X7iKPCTHL6S3w6q1eR67goxZGFSM1lk1hjwyu7XcLJuvALVV9uY3ovE" |
| 718 | "kQZSHwT+pyOPWQhsSjO+1GyjvCvK/CAwiUmBX+bQRGaqHsRcio7xSbdVcajQ3bXd" |
| 719 | "X+s0WdbOpn6MStKAiBVloPlSxEI8pxY6x/BBCnTIk/+DMB17uZlOjG3vbAnkDkP+" |
| 720 | "n0OTucD9sHV7EVj9XUxi51nOfNBCN/s7lpUjDS/NJ4k3iwOtbCPswiot8vLO779a" |
| 721 | "f07vR03r349Iz/KTzk95rlFtX0IU+KYNxFNsanIXZ+C9FYGRXkwhHcvFb4qMUB1y" |
| 722 | "TTlM80jBMOwyjZXmjRAhpAIEAKUDAgEUqQUCAwGJwKqBpwSBpOgebbmn9NRUtMWH" |
| 723 | "+eJpqA5JLMFSMCChOsvKey3toBaCNGU7HfAEiiXNuuAdCBoK262BjQc2YYfqFzqH" |
| 724 | "zuppopXCvhohx7j/tnCNZIMgLYt/O9SXK2RYI5z8FhCCHvB4CbD5G0LGl5EFP27s" |
| 725 | "Jb6S3aTTYPkQe8yZSlxevg6NDwmTogLO9F7UUkaYmVcMQhzssEE2ZRYNwSOU6KjE" |
| 726 | "0Yj+8fAiBtbQriIEIN2L8ZlpaVrdN5KFNdvcmOxJu81P8q53X55xQyGTnGWwsgMC" |
| 727 | "ARezggvvMIIEdjCCA16gAwIBAgIIf+yfD7Y6UicwDQYJKoZIhvcNAQELBQAwSTEL" |
| 728 | "MAkGA1UEBhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2ds" |
| 729 | "ZSBJbnRlcm5ldCBBdXRob3JpdHkgRzIwHhcNMTUwODEyMTQ1MzE1WhcNMTUxMTEw" |
| 730 | "MDAwMDAwWjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG" |
| 731 | "A1UEBwwNTW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UE" |
| 732 | "AwwOd3d3Lmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB" |
| 733 | "AQC0MeG5YGQ0t+IeJeoneP/PrhEaieibeKYkbKVLNZpoPLuBinvhkXZo3DC133Np" |
| 734 | "CBpy6ZktBwamqyixAyuk/NU6OjgXqwwxfQ7di1AInLIU792c7hFyNXSUCG7At8If" |
| 735 | "i3YwBX9Ba6u/1d6rWTGZJrdCq3QU11RkKYyTq2KT5mceTv9iGKqSkSTlp8puy/9S" |
| 736 | "Z/3DbU3U+BuqCFqeSlz7zjwFmk35acdCilpJlVDDN5C/RCh8/UKc8PaL+cxlt531" |
| 737 | "qoTENvYrflBno14YEZlCBZsPiFeUSILpKEj3Ccwhy0eLEucWQ72YZU8mUzXBoXGn" |
| 738 | "0zA0crFl5ci/2sTBBGZsylNBAgMBAAGjggFBMIIBPTAdBgNVHSUEFjAUBggrBgEF" |
| 739 | "BQcDAQYIKwYBBQUHAwIwGQYDVR0RBBIwEIIOd3d3Lmdvb2dsZS5jb20waAYIKwYB" |
| 740 | "BQUHAQEEXDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtpLmdvb2dsZS5jb20vR0lB" |
| 741 | "RzIuY3J0MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50czEuZ29vZ2xlLmNvbS9v" |
| 742 | "Y3NwMB0GA1UdDgQWBBS/bzHxcE73Q4j3slC4BLbMtLjGGjAMBgNVHRMBAf8EAjAA" |
| 743 | "MB8GA1UdIwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEvMBcGA1UdIAQQMA4wDAYK" |
| 744 | "KwYBBAHWeQIFATAwBgNVHR8EKTAnMCWgI6Ahhh9odHRwOi8vcGtpLmdvb2dsZS5j" |
| 745 | "b20vR0lBRzIuY3JsMA0GCSqGSIb3DQEBCwUAA4IBAQAbqdWPZEHk0X7iKPCTHL6S" |
| 746 | "3w6q1eR67goxZGFSM1lk1hjwyu7XcLJuvALVV9uY3ovEkQZSHwT+pyOPWQhsSjO+" |
| 747 | "1GyjvCvK/CAwiUmBX+bQRGaqHsRcio7xSbdVcajQ3bXdX+s0WdbOpn6MStKAiBVl" |
| 748 | "oPlSxEI8pxY6x/BBCnTIk/+DMB17uZlOjG3vbAnkDkP+n0OTucD9sHV7EVj9XUxi" |
| 749 | "51nOfNBCN/s7lpUjDS/NJ4k3iwOtbCPswiot8vLO779af07vR03r349Iz/KTzk95" |
| 750 | "rlFtX0IU+KYNxFNsanIXZ+C9FYGRXkwhHcvFb4qMUB1yTTlM80jBMOwyjZXmjRAh" |
| 751 | "MIID8DCCAtigAwIBAgIDAjqDMA0GCSqGSIb3DQEBCwUAMEIxCzAJBgNVBAYTAlVT" |
| 752 | "MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i" |
| 753 | "YWwgQ0EwHhcNMTMwNDA1MTUxNTU2WhcNMTYxMjMxMjM1OTU5WjBJMQswCQYDVQQG" |
| 754 | "EwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzElMCMGA1UEAxMcR29vZ2xlIEludGVy" |
| 755 | "bmV0IEF1dGhvcml0eSBHMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB" |
| 756 | "AJwqBHdc2FCROgajguDYUEi8iT/xGXAaiEZ+4I/F8YnOIe5a/mENtzJEiaB0C1NP" |
| 757 | "VaTOgmKV7utZX8bhBYASxF6UP7xbSDj0U/ck5vuR6RXEz/RTDfRK/J9U3n2+oGtv" |
| 758 | "h8DQUB8oMANA2ghzUWx//zo8pzcGjr1LEQTrfSTe5vn8MXH7lNVg8y5Kr0LSy+rE" |
| 759 | "ahqyzFPdFUuLH8gZYR/Nnag+YyuENWllhMgZxUYi+FOVvuOAShDGKuy6lyARxzmZ" |
| 760 | "EASg8GF6lSWMTlJ14rbtCMoU/M4iarNOz0YDl5cDfsCx3nuvRTPPuj5xt970JSXC" |
| 761 | "DTWJnZ37DhF5iR43xa+OcmkCAwEAAaOB5zCB5DAfBgNVHSMEGDAWgBTAephojYn7" |
| 762 | "qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUSt0GFhu89mi1dvWBtrtiGrpagS8wDgYD" |
| 763 | "VR0PAQH/BAQDAgEGMC4GCCsGAQUFBwEBBCIwIDAeBggrBgEFBQcwAYYSaHR0cDov" |
| 764 | "L2cuc3ltY2QuY29tMBIGA1UdEwEB/wQIMAYBAf8CAQAwNQYDVR0fBC4wLDAqoCig" |
| 765 | "JoYkaHR0cDovL2cuc3ltY2IuY29tL2NybHMvZ3RnbG9iYWwuY3JsMBcGA1UdIAQQ" |
| 766 | "MA4wDAYKKwYBBAHWeQIFATANBgkqhkiG9w0BAQsFAAOCAQEAqvqpIM1qZ4PtXtR+" |
| 767 | "3h3Ef+AlBgDFJPupyC1tft6dgmUsgWM0Zj7pUsIItMsv91+ZOmqcUHqFBYx90SpI" |
| 768 | "hNMJbHzCzTWf84LuUt5oX+QAihcglvcpjZpNy6jehsgNb1aHA30DP9z6eX0hGfnI" |
| 769 | "Oi9RdozHQZJxjyXON/hKTAAj78Q1EK7gI4BzfE00LshukNYQHpmEcxpw8u1VDu4X" |
| 770 | "Bupn7jLrLN1nBz/2i8Jw3lsA5rsb0zYaImxssDVCbJAJPZPpZAkiDoUGn8JzIdPm" |
| 771 | "X4DkjYUiOnMDsWCOrmji9D6X52ASCWg23jrW4kOVWzeBkoEfu43XrVJkFleW2V40" |
| 772 | "fsg12DCCA30wggLmoAMCAQICAxK75jANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQG" |
| 773 | "EwJVUzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUg" |
| 774 | "Q2VydGlmaWNhdGUgQXV0aG9yaXR5MB4XDTAyMDUyMTA0MDAwMFoXDTE4MDgyMTA0" |
| 775 | "MDAwMFowQjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUdlb1RydXN0IEluYy4xGzAZ" |
| 776 | "BgNVBAMTEkdlb1RydXN0IEdsb2JhbCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEP" |
| 777 | "ADCCAQoCggEBANrMGGMw/fQXIxpWflvfPGw45HG3eJHUvKHYTPioQ7YD6U0hBwiI" |
| 778 | "2lgvZjkpvQV4i5046AW3an5xpObEYKaw74DkiSgPniXW7YPzraaRx5jJQhg1FJ2t" |
| 779 | "mEaSLk/K8YdDwRaVVy1Q74ktgHpXrfLuX2vSAI25FPgUFTXZwEaje3LIkb/JVSvN" |
| 780 | "0Jc+nCZkzN/Ogxlxyk7m1NV7qRnNVd7I7NJeOFPlXE+MLf5QIzb8ZubLjqQ5GQC3" |
| 781 | "lQI5kQsO/jgu0R0FmvZNPm8PBx2vLB6PYDni+jZTEznUXiYr2z2oFL0y6xgDKFIE" |
| 782 | "ceWrMz3hOLsHNoRinHnqFjD0X8Ar6HFr5PkCAwEAAaOB8DCB7TAfBgNVHSMEGDAW" |
| 783 | "gBRI5mj5K9KylddH2CMgEE8zmJCf1DAdBgNVHQ4EFgQUwHqYaI2J+6sFZAwRfap9" |
| 784 | "ZbjKzE4wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwOgYDVR0fBDMw" |
| 785 | "MTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9zZWN1cmVjYS5j" |
| 786 | "cmwwTgYDVR0gBEcwRTBDBgRVHSAAMDswOQYIKwYBBQUHAgEWLWh0dHBzOi8vd3d3" |
| 787 | "Lmdlb3RydXN0LmNvbS9yZXNvdXJjZXMvcmVwb3NpdG9yeTANBgkqhkiG9w0BAQUF" |
| 788 | "AAOBgQB24RJuTksWEoYwBrKBCM/wCMfHcX5m7sLt1Dsf//DwyE7WQziwuTB9GNBV" |
| 789 | "g6JqyzYRnOhIZqNtf7gT1Ef+i1pcc/yu2RsyGTirlzQUqpbS66McFAhJtrvlke+D" |
| 790 | "NusdVm/K2rxzY5Dkf3s+Iss9B+1fOHSc4wNQTqGvmO5h8oQ/Eg=="; |
| 791 |
| 792 | // kBadSessionExtraField is a custom serialized SSL_SESSION generated by |
| 793 | // replacing the final (optional) element of |kCustomSession| with tag |
| 794 | // number 99. |
| 795 | static const char kBadSessionExtraField[] = |
| 796 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 797 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 798 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 799 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 800 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 801 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 802 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 803 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBOMDBAEF"; |
| 804 |
| 805 | // kBadSessionVersion is a custom serialized SSL_SESSION generated by replacing |
| 806 | // the version of |kCustomSession| with 2. |
| 807 | static const char kBadSessionVersion[] = |
| 808 | "MIIBdgIBAgICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 809 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 810 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 811 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 812 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 813 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 814 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 815 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEF"; |
| 816 |
| 817 | // kBadSessionTrailingData is a custom serialized SSL_SESSION with trailing data |
| 818 | // appended. |
| 819 | static const char kBadSessionTrailingData[] = |
| 820 | "MIIBdgIBAQICAwMEAsAvBCAG5Q1ndq4Yfmbeo1zwLkNRKmCXGdNgWvGT3cskV0yQ" |
| 821 | "kAQwJlrlzkAWBOWiLj/jJ76D7l+UXoizP2KI2C7I2FccqMmIfFmmkUy32nIJ0mZH" |
| 822 | "IWoJoQYCBFRDO46iBAICASykAwQBAqUDAgEUphAEDnd3dy5nb29nbGUuY29tqAcE" |
| 823 | "BXdvcmxkqQUCAwGJwKqBpwSBpBwUQvoeOk0Kg36SYTcLEkXqKwOBfF9vE4KX0Nxe" |
| 824 | "LwjcDTpsuh3qXEaZ992r1N38VDcyS6P7I6HBYN9BsNHM362zZnY27GpTw+Kwd751" |
| 825 | "CLoXFPoaMOe57dbBpXoro6Pd3BTbf/Tzr88K06yEOTDKPNj3+inbMaVigtK4PLyP" |
| 826 | "q+Topyzvx9USFgRvyuoxn0Hgb+R0A3j6SLRuyOdAi4gv7Y5oliynrSIEIAYGBgYG" |
| 827 | "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGrgMEAQevAwQBBLADBAEFAAAA"; |
| 828 |
| 829 | static bool DecodeBase64(std::vector<uint8_t> *out, const char *in) { |
| 830 | size_t len; |
| 831 | if (!EVP_DecodedLength(&len, strlen(in))) { |
| 832 | fprintf(stderr, "EVP_DecodedLength failed\n"); |
| 833 | return false; |
| 834 | } |
| 835 |
| 836 | out->resize(len); |
| 837 | if (!EVP_DecodeBase64(out->data(), &len, len, (const uint8_t *)in, |
| 838 | strlen(in))) { |
| 839 | fprintf(stderr, "EVP_DecodeBase64 failed\n"); |
| 840 | return false; |
| 841 | } |
| 842 | out->resize(len); |
| 843 | return true; |
| 844 | } |
| 845 |
| 846 | static bool DecodeLowerHex(std::vector<uint8_t> *out, |
| 847 | bssl::Span<const char> in) { |
| 848 | if (in.size() % 2 != 0) { |
| 849 | return false; |
| 850 | } |
| 851 | out->resize(in.size() / 2); |
| 852 | for (size_t i = 0; i < out->size(); i++) { |
| 853 | char hi = in[2 * i], lo = in[2 * i + 1]; |
| 854 | uint8_t b = 0; |
| 855 | if ('0' <= hi && hi <= '9') { |
| 856 | b |= hi - '0'; |
| 857 | } else if ('a' <= hi && hi <= 'f') { |
| 858 | b |= hi - 'a' + 10; |
| 859 | } else { |
| 860 | return false; |
| 861 | } |
| 862 | b <<= 4; |
| 863 | if ('0' <= lo && lo <= '9') { |
| 864 | b |= lo - '0'; |
| 865 | } else if ('a' <= lo && lo <= 'f') { |
| 866 | b |= lo - 'a' + 10; |
| 867 | } else { |
| 868 | return false; |
| 869 | } |
| 870 | (*out)[i] = b; |
| 871 | } |
| 872 | return true; |
| 873 | } |
| 874 |
| 875 | TEST(SSLTest, SessionEncoding) { |
| 876 | for (const char *input_b64 : { |
| 877 | kOpenSSLSession, |
| 878 | kCustomSession, |
| 879 | kBoringSSLSession, |
| 880 | }) { |
| 881 | SCOPED_TRACE(std::string(input_b64)); |
| 882 | // Decode the input. |
| 883 | std::vector<uint8_t> input; |
| 884 | ASSERT_TRUE(DecodeBase64(&input, input_b64)); |
| 885 |
| 886 | // Verify the SSL_SESSION decodes. |
| 887 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 888 | ASSERT_TRUE(ssl_ctx); |
| 889 | bssl::UniquePtr<SSL_SESSION> session( |
| 890 | SSL_SESSION_from_bytes(input.data(), input.size(), ssl_ctx.get())); |
| 891 | ASSERT_TRUE(session) << "SSL_SESSION_from_bytes failed"; |
| 892 |
| 893 | // Verify the SSL_SESSION encoding round-trips. |
| 894 | size_t encoded_len; |
| 895 | bssl::UniquePtr<uint8_t> encoded; |
| 896 | uint8_t *encoded_raw; |
| 897 | ASSERT_TRUE(SSL_SESSION_to_bytes(session.get(), &encoded_raw, &encoded_len)) |
| 898 | << "SSL_SESSION_to_bytes failed"; |
| 899 | encoded.reset(encoded_raw); |
| 900 | EXPECT_EQ(Bytes(encoded.get(), encoded_len), Bytes(input)) |
| 901 | << "SSL_SESSION_to_bytes did not round-trip"; |
| 902 |
| 903 | // Verify the SSL_SESSION also decodes with the legacy API. |
| 904 | const uint8_t *cptr = input.data(); |
| 905 | session.reset(d2i_SSL_SESSION(NULL, &cptr, input.size())); |
| 906 | ASSERT_TRUE(session) << "d2i_SSL_SESSION failed"; |
| 907 | EXPECT_EQ(cptr, input.data() + input.size()); |
| 908 |
| 909 | // Verify the SSL_SESSION encoding round-trips via the legacy API. |
| 910 | int len = i2d_SSL_SESSION(session.get(), NULL); |
| 911 | ASSERT_GT(len, 0) << "i2d_SSL_SESSION failed"; |
| 912 | ASSERT_EQ(static_cast<size_t>(len), input.size()) |
| 913 | << "i2d_SSL_SESSION(NULL) returned invalid length"; |
| 914 |
| 915 | encoded.reset((uint8_t *)OPENSSL_malloc(input.size())); |
| 916 | ASSERT_TRUE(encoded); |
| 917 |
| 918 | uint8_t *ptr = encoded.get(); |
| 919 | len = i2d_SSL_SESSION(session.get(), &ptr); |
| 920 | ASSERT_GT(len, 0) << "i2d_SSL_SESSION failed"; |
| 921 | ASSERT_EQ(static_cast<size_t>(len), input.size()) |
| 922 | << "i2d_SSL_SESSION(NULL) returned invalid length"; |
| 923 | ASSERT_EQ(ptr, encoded.get() + input.size()) |
| 924 | << "i2d_SSL_SESSION did not advance ptr correctly"; |
| 925 | EXPECT_EQ(Bytes(encoded.get(), encoded_len), Bytes(input)) |
| 926 | << "SSL_SESSION_to_bytes did not round-trip"; |
| 927 | } |
| 928 |
| 929 | for (const char *input_b64 : { |
| 930 | kBadSessionExtraField, |
| 931 | kBadSessionVersion, |
| 932 | kBadSessionTrailingData, |
| 933 | }) { |
| 934 | SCOPED_TRACE(std::string(input_b64)); |
| 935 | std::vector<uint8_t> input; |
| 936 | ASSERT_TRUE(DecodeBase64(&input, input_b64)); |
| 937 |
| 938 | // Verify that the SSL_SESSION fails to decode. |
| 939 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 940 | ASSERT_TRUE(ssl_ctx); |
| 941 | bssl::UniquePtr<SSL_SESSION> session( |
| 942 | SSL_SESSION_from_bytes(input.data(), input.size(), ssl_ctx.get())); |
| 943 | EXPECT_FALSE(session) << "SSL_SESSION_from_bytes unexpectedly succeeded"; |
| 944 | ERR_clear_error(); |
| 945 | } |
| 946 | } |
| 947 |
| 948 | static void ExpectDefaultVersion(uint16_t min_version, uint16_t max_version, |
| 949 | const SSL_METHOD *(*method)(void)) { |
| 950 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method())); |
| 951 | ASSERT_TRUE(ctx); |
| 952 | EXPECT_EQ(min_version, SSL_CTX_get_min_proto_version(ctx.get())); |
| 953 | EXPECT_EQ(max_version, SSL_CTX_get_max_proto_version(ctx.get())); |
| 954 | } |
| 955 |
| 956 | TEST(SSLTest, DefaultVersion) { |
| 957 | ExpectDefaultVersion(TLS1_2_VERSION, TLS1_3_VERSION, &TLS_method); |
| 958 | ExpectDefaultVersion(TLS1_VERSION, TLS1_VERSION, &TLSv1_method); |
| 959 | ExpectDefaultVersion(TLS1_1_VERSION, TLS1_1_VERSION, &TLSv1_1_method); |
| 960 | ExpectDefaultVersion(TLS1_2_VERSION, TLS1_2_VERSION, &TLSv1_2_method); |
| 961 | ExpectDefaultVersion(DTLS1_2_VERSION, DTLS1_2_VERSION, &DTLS_method); |
| 962 | ExpectDefaultVersion(DTLS1_VERSION, DTLS1_VERSION, &DTLSv1_method); |
| 963 | ExpectDefaultVersion(DTLS1_2_VERSION, DTLS1_2_VERSION, &DTLSv1_2_method); |
| 964 | } |
| 965 |
| 966 | TEST(SSLTest, CipherProperties) { |
| 967 | static const struct { |
| 968 | int id; |
| 969 | const char *standard_name; |
| 970 | int cipher_nid; |
| 971 | int digest_nid; |
| 972 | int kx_nid; |
| 973 | int auth_nid; |
| 974 | int prf_nid; |
| 975 | } kTests[] = { |
| 976 | { |
| 977 | SSL3_CK_RSA_DES_192_CBC3_SHA, |
| 978 | "TLS_RSA_WITH_3DES_EDE_CBC_SHA", |
| 979 | NID_des_ede3_cbc, |
| 980 | NID_sha1, |
| 981 | NID_kx_rsa, |
| 982 | NID_auth_rsa, |
| 983 | NID_md5_sha1, |
| 984 | }, |
| 985 | { |
| 986 | TLS1_CK_RSA_WITH_AES_128_SHA, |
| 987 | "TLS_RSA_WITH_AES_128_CBC_SHA", |
| 988 | NID_aes_128_cbc, |
| 989 | NID_sha1, |
| 990 | NID_kx_rsa, |
| 991 | NID_auth_rsa, |
| 992 | NID_md5_sha1, |
| 993 | }, |
| 994 | { |
| 995 | TLS1_CK_PSK_WITH_AES_256_CBC_SHA, |
| 996 | "TLS_PSK_WITH_AES_256_CBC_SHA", |
| 997 | NID_aes_256_cbc, |
| 998 | NID_sha1, |
| 999 | NID_kx_psk, |
| 1000 | NID_auth_psk, |
| 1001 | NID_md5_sha1, |
| 1002 | }, |
| 1003 | { |
| 1004 | TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
| 1005 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
| 1006 | NID_aes_128_cbc, |
| 1007 | NID_sha1, |
| 1008 | NID_kx_ecdhe, |
| 1009 | NID_auth_rsa, |
| 1010 | NID_md5_sha1, |
| 1011 | }, |
| 1012 | { |
| 1013 | TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
| 1014 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", |
| 1015 | NID_aes_256_cbc, |
| 1016 | NID_sha1, |
| 1017 | NID_kx_ecdhe, |
| 1018 | NID_auth_rsa, |
| 1019 | NID_md5_sha1, |
| 1020 | }, |
| 1021 | { |
| 1022 | TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 1023 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", |
| 1024 | NID_aes_128_gcm, |
| 1025 | NID_undef, |
| 1026 | NID_kx_ecdhe, |
| 1027 | NID_auth_rsa, |
| 1028 | NID_sha256, |
| 1029 | }, |
| 1030 | { |
| 1031 | TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 1032 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", |
| 1033 | NID_aes_128_gcm, |
| 1034 | NID_undef, |
| 1035 | NID_kx_ecdhe, |
| 1036 | NID_auth_ecdsa, |
| 1037 | NID_sha256, |
| 1038 | }, |
| 1039 | { |
| 1040 | TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 1041 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", |
| 1042 | NID_aes_256_gcm, |
| 1043 | NID_undef, |
| 1044 | NID_kx_ecdhe, |
| 1045 | NID_auth_ecdsa, |
| 1046 | NID_sha384, |
| 1047 | }, |
| 1048 | { |
| 1049 | TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 1050 | "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA", |
| 1051 | NID_aes_128_cbc, |
| 1052 | NID_sha1, |
| 1053 | NID_kx_ecdhe, |
| 1054 | NID_auth_psk, |
| 1055 | NID_md5_sha1, |
| 1056 | }, |
| 1057 | { |
| 1058 | TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 1059 | "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", |
| 1060 | NID_chacha20_poly1305, |
| 1061 | NID_undef, |
| 1062 | NID_kx_ecdhe, |
| 1063 | NID_auth_rsa, |
| 1064 | NID_sha256, |
| 1065 | }, |
| 1066 | { |
| 1067 | TLS1_3_CK_AES_256_GCM_SHA384, |
| 1068 | "TLS_AES_256_GCM_SHA384", |
| 1069 | NID_aes_256_gcm, |
| 1070 | NID_undef, |
| 1071 | NID_kx_any, |
| 1072 | NID_auth_any, |
| 1073 | NID_sha384, |
| 1074 | }, |
| 1075 | { |
| 1076 | TLS1_3_CK_AES_128_GCM_SHA256, |
| 1077 | "TLS_AES_128_GCM_SHA256", |
| 1078 | NID_aes_128_gcm, |
| 1079 | NID_undef, |
| 1080 | NID_kx_any, |
| 1081 | NID_auth_any, |
| 1082 | NID_sha256, |
| 1083 | }, |
| 1084 | { |
| 1085 | TLS1_3_CK_CHACHA20_POLY1305_SHA256, |
| 1086 | "TLS_CHACHA20_POLY1305_SHA256", |
| 1087 | NID_chacha20_poly1305, |
| 1088 | NID_undef, |
| 1089 | NID_kx_any, |
| 1090 | NID_auth_any, |
| 1091 | NID_sha256, |
| 1092 | }, |
| 1093 | }; |
| 1094 |
| 1095 | for (const auto &t : kTests) { |
| 1096 | SCOPED_TRACE(t.standard_name); |
| 1097 |
| 1098 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(t.id & 0xffff); |
| 1099 | ASSERT_TRUE(cipher); |
| 1100 | EXPECT_STREQ(t.standard_name, SSL_CIPHER_standard_name(cipher)); |
| 1101 |
| 1102 | EXPECT_EQ(t.cipher_nid, SSL_CIPHER_get_cipher_nid(cipher)); |
| 1103 | EXPECT_EQ(t.digest_nid, SSL_CIPHER_get_digest_nid(cipher)); |
| 1104 | EXPECT_EQ(t.kx_nid, SSL_CIPHER_get_kx_nid(cipher)); |
| 1105 | EXPECT_EQ(t.auth_nid, SSL_CIPHER_get_auth_nid(cipher)); |
| 1106 | EXPECT_EQ(t.prf_nid, EVP_MD_nid(SSL_CIPHER_get_handshake_digest(cipher))); |
| 1107 | EXPECT_EQ(t.prf_nid, SSL_CIPHER_get_prf_nid(cipher)); |
| 1108 | } |
| 1109 | } |
| 1110 |
| 1111 | // CreateSessionWithTicket returns a sample |SSL_SESSION| with the specified |
| 1112 | // version and ticket length or nullptr on failure. |
| 1113 | static bssl::UniquePtr<SSL_SESSION> CreateSessionWithTicket(uint16_t version, |
| 1114 | size_t ticket_len) { |
| 1115 | std::vector<uint8_t> der; |
| 1116 | if (!DecodeBase64(&der, kOpenSSLSession)) { |
| 1117 | return nullptr; |
| 1118 | } |
| 1119 |
| 1120 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 1121 | if (!ssl_ctx) { |
| 1122 | return nullptr; |
| 1123 | } |
| 1124 | // Use a garbage ticket. |
| 1125 | std::vector<uint8_t> ticket(ticket_len, 'a'); |
| 1126 | bssl::UniquePtr<SSL_SESSION> session( |
| 1127 | SSL_SESSION_from_bytes(der.data(), der.size(), ssl_ctx.get())); |
| 1128 | if (!session || // |
| 1129 | !SSL_SESSION_set_protocol_version(session.get(), version) || // |
| 1130 | !SSL_SESSION_set_ticket(session.get(), ticket.data(), ticket.size())) { |
| 1131 | return nullptr; |
| 1132 | } |
| 1133 | // Fix up the timeout. |
| 1134 | SSL_SESSION_set_time(session.get(), time(nullptr)); |
| 1135 | return session; |
| 1136 | } |
| 1137 |
| 1138 | static bool GetClientHello(SSL *ssl, std::vector<uint8_t> *out) { |
| 1139 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 1140 | if (!bio) { |
| 1141 | return false; |
| 1142 | } |
| 1143 | // Do not configure a reading BIO, but record what's written to a memory BIO. |
| 1144 | BIO_up_ref(bio.get()); |
| 1145 | SSL_set_bio(ssl, nullptr /* rbio */, bio.get()); |
| 1146 | int ret = SSL_connect(ssl); |
| 1147 | if (ret > 0) { |
| 1148 | // SSL_connect should fail without a BIO to write to. |
| 1149 | return false; |
| 1150 | } |
| 1151 | ERR_clear_error(); |
| 1152 |
| 1153 | const uint8_t *client_hello; |
| 1154 | size_t client_hello_len; |
| 1155 | if (!BIO_mem_contents(bio.get(), &client_hello, &client_hello_len)) { |
| 1156 | return false; |
| 1157 | } |
| 1158 |
| 1159 | // We did not get far enough to write a ClientHello. |
| 1160 | if (client_hello_len == 0) { |
| 1161 | return false; |
| 1162 | } |
| 1163 |
| 1164 | *out = std::vector<uint8_t>(client_hello, client_hello + client_hello_len); |
| 1165 | return true; |
| 1166 | } |
| 1167 |
| 1168 | // GetClientHelloLen creates a client SSL connection with the specified version |
| 1169 | // and ticket length. It returns the length of the ClientHello, not including |
| 1170 | // the record header, on success and zero on error. |
| 1171 | static size_t GetClientHelloLen(uint16_t max_version, uint16_t session_version, |
| 1172 | size_t ticket_len) { |
| 1173 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1174 | bssl::UniquePtr<SSL_SESSION> session = |
| 1175 | CreateSessionWithTicket(session_version, ticket_len); |
| 1176 | if (!ctx || !session) { |
| 1177 | return 0; |
| 1178 | } |
| 1179 |
| 1180 | // Set a one-element cipher list so the baseline ClientHello is unpadded. |
| 1181 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1182 | if (!ssl || !SSL_set_session(ssl.get(), session.get()) || |
| 1183 | !SSL_set_strict_cipher_list(ssl.get(), "ECDHE-RSA-AES128-GCM-SHA256") || |
| 1184 | !SSL_set_max_proto_version(ssl.get(), max_version)) { |
| 1185 | return 0; |
| 1186 | } |
| 1187 |
| 1188 | std::vector<uint8_t> client_hello; |
| 1189 | if (!GetClientHello(ssl.get(), &client_hello) || |
| 1190 | client_hello.size() <= SSL3_RT_HEADER_LENGTH) { |
| 1191 | return 0; |
| 1192 | } |
| 1193 |
| 1194 | return client_hello.size() - SSL3_RT_HEADER_LENGTH; |
| 1195 | } |
| 1196 |
| 1197 | TEST(SSLTest, Padding) { |
| 1198 | struct PaddingVersions { |
| 1199 | uint16_t max_version, session_version; |
| 1200 | }; |
| 1201 | static const PaddingVersions kPaddingVersions[] = { |
| 1202 | // Test the padding extension at TLS 1.2. |
| 1203 | {TLS1_2_VERSION, TLS1_2_VERSION}, |
| 1204 | // Test the padding extension at TLS 1.3 with a TLS 1.2 session, so there |
| 1205 | // will be no PSK binder after the padding extension. |
| 1206 | {TLS1_3_VERSION, TLS1_2_VERSION}, |
| 1207 | // Test the padding extension at TLS 1.3 with a TLS 1.3 session, so there |
| 1208 | // will be a PSK binder after the padding extension. |
| 1209 | {TLS1_3_VERSION, TLS1_3_VERSION}, |
| 1210 |
| 1211 | }; |
| 1212 |
| 1213 | struct PaddingTest { |
| 1214 | size_t input_len, padded_len; |
| 1215 | }; |
| 1216 | static const PaddingTest kPaddingTests[] = { |
| 1217 | // ClientHellos of length below 0x100 do not require padding. |
| 1218 | {0xfe, 0xfe}, |
| 1219 | {0xff, 0xff}, |
| 1220 | // ClientHellos of length 0x100 through 0x1fb are padded up to 0x200. |
| 1221 | {0x100, 0x200}, |
| 1222 | {0x123, 0x200}, |
| 1223 | {0x1fb, 0x200}, |
| 1224 | // ClientHellos of length 0x1fc through 0x1ff get padded beyond 0x200. The |
| 1225 | // padding extension takes a minimum of four bytes plus one required |
| 1226 | // content |
| 1227 | // byte. (To work around yet more server bugs, we avoid empty final |
| 1228 | // extensions.) |
| 1229 | {0x1fc, 0x201}, |
| 1230 | {0x1fd, 0x202}, |
| 1231 | {0x1fe, 0x203}, |
| 1232 | {0x1ff, 0x204}, |
| 1233 | // Finally, larger ClientHellos need no padding. |
| 1234 | {0x200, 0x200}, |
| 1235 | {0x201, 0x201}, |
| 1236 | }; |
| 1237 |
| 1238 | for (const PaddingVersions &versions : kPaddingVersions) { |
| 1239 | SCOPED_TRACE(versions.max_version); |
| 1240 | SCOPED_TRACE(versions.session_version); |
| 1241 |
| 1242 | // Sample a baseline length. |
| 1243 | size_t base_len = |
| 1244 | GetClientHelloLen(versions.max_version, versions.session_version, 1); |
| 1245 | ASSERT_NE(base_len, 0u) << "Baseline length could not be sampled"; |
| 1246 |
| 1247 | for (const PaddingTest &test : kPaddingTests) { |
| 1248 | SCOPED_TRACE(test.input_len); |
| 1249 | ASSERT_LE(base_len, test.input_len) << "Baseline ClientHello too long"; |
| 1250 |
| 1251 | size_t padded_len = |
| 1252 | GetClientHelloLen(versions.max_version, versions.session_version, |
| 1253 | 1 + test.input_len - base_len); |
| 1254 | EXPECT_EQ(padded_len, test.padded_len) |
| 1255 | << "ClientHello was not padded to expected length"; |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 |
| 1260 | static bssl::UniquePtr<X509> CertFromPEM(const char *pem) { |
| 1261 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem))); |
| 1262 | if (!bio) { |
| 1263 | return nullptr; |
| 1264 | } |
| 1265 | return bssl::UniquePtr<X509>( |
| 1266 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 1267 | } |
| 1268 |
| 1269 | static bssl::UniquePtr<EVP_PKEY> KeyFromPEM(const char *pem) { |
| 1270 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem))); |
| 1271 | if (!bio) { |
| 1272 | return nullptr; |
| 1273 | } |
| 1274 | return bssl::UniquePtr<EVP_PKEY>( |
| 1275 | PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); |
| 1276 | } |
| 1277 |
| 1278 | static bssl::UniquePtr<CRYPTO_BUFFER> BufferFromPEM(const char *pem) { |
| 1279 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem))); |
| 1280 | char *name, *header; |
| 1281 | uint8_t *data; |
| 1282 | long data_len; |
| 1283 | if (!PEM_read_bio(bio.get(), &name, &header, &data, &data_len)) { |
| 1284 | return nullptr; |
| 1285 | } |
| 1286 | OPENSSL_free(name); |
| 1287 | OPENSSL_free(header); |
| 1288 |
| 1289 | auto ret = bssl::UniquePtr<CRYPTO_BUFFER>( |
| 1290 | CRYPTO_BUFFER_new(data, data_len, nullptr)); |
| 1291 | OPENSSL_free(data); |
| 1292 | return ret; |
| 1293 | } |
| 1294 |
| 1295 | static bssl::UniquePtr<X509> X509FromBuffer( |
| 1296 | bssl::UniquePtr<CRYPTO_BUFFER> buffer) { |
| 1297 | if (!buffer) { |
| 1298 | return nullptr; |
| 1299 | } |
| 1300 | const uint8_t *derp = CRYPTO_BUFFER_data(buffer.get()); |
| 1301 | return bssl::UniquePtr<X509>( |
| 1302 | d2i_X509(NULL, &derp, CRYPTO_BUFFER_len(buffer.get()))); |
| 1303 | } |
| 1304 |
| 1305 | static bssl::UniquePtr<X509> GetTestCertificate() { |
| 1306 | static const char kCertPEM[] = |
| 1307 | "-----BEGIN CERTIFICATE-----\n" |
| 1308 | "MIICWDCCAcGgAwIBAgIJAPuwTC6rEJsMMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\n" |
| 1309 | "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n" |
| 1310 | "aWRnaXRzIFB0eSBMdGQwHhcNMTQwNDIzMjA1MDQwWhcNMTcwNDIyMjA1MDQwWjBF\n" |
| 1311 | "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n" |
| 1312 | "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n" |
| 1313 | "gQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92kWdGMdAQhLci\n" |
| 1314 | "HnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiFKKAnHmUcrgfV\n" |
| 1315 | "W28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQABo1AwTjAdBgNV\n" |
| 1316 | "HQ4EFgQUi3XVrMsIvg4fZbf6Vr5sp3Xaha8wHwYDVR0jBBgwFoAUi3XVrMsIvg4f\n" |
| 1317 | "Zbf6Vr5sp3Xaha8wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQA76Hht\n" |
| 1318 | "ldY9avcTGSwbwoiuIqv0jTL1fHFnzy3RHMLDh+Lpvolc5DSrSJHCP5WuK0eeJXhr\n" |
| 1319 | "T5oQpHL9z/cCDLAKCKRa4uV0fhEdOWBqyR9p8y5jJtye72t6CuFUV5iqcpF4BH4f\n" |
| 1320 | "j2VNHwsSrJwkD4QUGlUtH7vwnQmyCFxZMmWAJg==\n" |
| 1321 | "-----END CERTIFICATE-----\n"; |
| 1322 | return CertFromPEM(kCertPEM); |
| 1323 | } |
| 1324 | static bssl::UniquePtr<CRYPTO_BUFFER> GetTestCertificateBuffer() { |
| 1325 | static const char kCertPEM[] = |
| 1326 | "-----BEGIN CERTIFICATE-----\n" |
| 1327 | "MIICWDCCAcGgAwIBAgIJAPuwTC6rEJsMMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\n" |
| 1328 | "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n" |
| 1329 | "aWRnaXRzIFB0eSBMdGQwHhcNMTQwNDIzMjA1MDQwWhcNMTcwNDIyMjA1MDQwWjBF\n" |
| 1330 | "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n" |
| 1331 | "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n" |
| 1332 | "gQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92kWdGMdAQhLci\n" |
| 1333 | "HnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiFKKAnHmUcrgfV\n" |
| 1334 | "W28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQABo1AwTjAdBgNV\n" |
| 1335 | "HQ4EFgQUi3XVrMsIvg4fZbf6Vr5sp3Xaha8wHwYDVR0jBBgwFoAUi3XVrMsIvg4f\n" |
| 1336 | "Zbf6Vr5sp3Xaha8wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQA76Hht\n" |
| 1337 | "ldY9avcTGSwbwoiuIqv0jTL1fHFnzy3RHMLDh+Lpvolc5DSrSJHCP5WuK0eeJXhr\n" |
| 1338 | "T5oQpHL9z/cCDLAKCKRa4uV0fhEdOWBqyR9p8y5jJtye72t6CuFUV5iqcpF4BH4f\n" |
| 1339 | "j2VNHwsSrJwkD4QUGlUtH7vwnQmyCFxZMmWAJg==\n" |
| 1340 | "-----END CERTIFICATE-----\n"; |
| 1341 | return BufferFromPEM(kCertPEM); |
| 1342 | } |
| 1343 |
| 1344 | static bssl::UniquePtr<EVP_PKEY> GetTestKey() { |
| 1345 | static const char kKeyPEM[] = |
| 1346 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 1347 | "MIICXgIBAAKBgQDYK8imMuRi/03z0K1Zi0WnvfFHvwlYeyK9Na6XJYaUoIDAtB92\n" |
| 1348 | "kWdGMdAQhLciHnAjkXLI6W15OoV3gA/ElRZ1xUpxTMhjP6PyY5wqT5r6y8FxbiiF\n" |
| 1349 | "KKAnHmUcrgfVW28tQ+0rkLGMryRtrukXOgXBv7gcrmU7G1jC2a7WqmeI8QIDAQAB\n" |
| 1350 | "AoGBAIBy09Fd4DOq/Ijp8HeKuCMKTHqTW1xGHshLQ6jwVV2vWZIn9aIgmDsvkjCe\n" |
| 1351 | "i6ssZvnbjVcwzSoByhjN8ZCf/i15HECWDFFh6gt0P5z0MnChwzZmvatV/FXCT0j+\n" |
| 1352 | "WmGNB/gkehKjGXLLcjTb6dRYVJSCZhVuOLLcbWIV10gggJQBAkEA8S8sGe4ezyyZ\n" |
| 1353 | "m4e9r95g6s43kPqtj5rewTsUxt+2n4eVodD+ZUlCULWVNAFLkYRTBCASlSrm9Xhj\n" |
| 1354 | "QpmWAHJUkQJBAOVzQdFUaewLtdOJoPCtpYoY1zd22eae8TQEmpGOR11L6kbxLQsk\n" |
| 1355 | "aMly/DOnOaa82tqAGTdqDEZgSNmCeKKknmECQAvpnY8GUOVAubGR6c+W90iBuQLj\n" |
| 1356 | "LtFp/9ihd2w/PoDwrHZaoUYVcT4VSfJQog/k7kjE4MYXYWL8eEKg3WTWQNECQQDk\n" |
| 1357 | "104Wi91Umd1PzF0ijd2jXOERJU1wEKe6XLkYYNHWQAe5l4J4MWj9OdxFXAxIuuR/\n" |
| 1358 | "tfDwbqkta4xcux67//khAkEAvvRXLHTaa6VFzTaiiO8SaFsHV3lQyXOtMrBpB5jd\n" |
| 1359 | "moZWgjHvB2W9Ckn7sDqsPB+U2tyX0joDdQEyuiMECDY8oQ==\n" |
| 1360 | "-----END RSA PRIVATE KEY-----\n"; |
| 1361 | return KeyFromPEM(kKeyPEM); |
| 1362 | } |
| 1363 |
| 1364 | static bssl::UniquePtr<SSL_CTX> CreateContextWithTestCertificate( |
| 1365 | const SSL_METHOD *method) { |
| 1366 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method)); |
| 1367 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 1368 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 1369 | if (!ctx || !cert || !key || |
| 1370 | !SSL_CTX_use_certificate(ctx.get(), cert.get()) || |
| 1371 | !SSL_CTX_use_PrivateKey(ctx.get(), key.get())) { |
| 1372 | return nullptr; |
| 1373 | } |
| 1374 | return ctx; |
| 1375 | } |
| 1376 |
| 1377 | static bssl::UniquePtr<CRYPTO_BUFFER> GetECDSATestCertificateBuffer() { |
| 1378 | static const char kCertPEM[] = |
| 1379 | "-----BEGIN CERTIFICATE-----\n" |
| 1380 | "MIIBzzCCAXagAwIBAgIJANlMBNpJfb/rMAkGByqGSM49BAEwRTELMAkGA1UEBhMC\n" |
| 1381 | "QVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdp\n" |
| 1382 | "dHMgUHR5IEx0ZDAeFw0xNDA0MjMyMzIxNTdaFw0xNDA1MjMyMzIxNTdaMEUxCzAJ\n" |
| 1383 | "BgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5l\n" |
| 1384 | "dCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATmK2ni\n" |
| 1385 | "v2Wfl74vHg2UikzVl2u3qR4NRvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYa\n" |
| 1386 | "HPUdfvGULUvPciLBo1AwTjAdBgNVHQ4EFgQUq4TSrKuV8IJOFngHVVdf5CaNgtEw\n" |
| 1387 | "HwYDVR0jBBgwFoAUq4TSrKuV8IJOFngHVVdf5CaNgtEwDAYDVR0TBAUwAwEB/zAJ\n" |
| 1388 | "BgcqhkjOPQQBA0gAMEUCIQDyoDVeUTo2w4J5m+4nUIWOcAZ0lVfSKXQA9L4Vh13E\n" |
| 1389 | "BwIgfB55FGohg/B6dGh5XxSZmmi08cueFV7mHzJSYV51yRQ=\n" |
| 1390 | "-----END CERTIFICATE-----\n"; |
| 1391 | return BufferFromPEM(kCertPEM); |
| 1392 | } |
| 1393 |
| 1394 | static bssl::UniquePtr<X509> GetECDSATestCertificate() { |
| 1395 | return X509FromBuffer(GetECDSATestCertificateBuffer()); |
| 1396 | } |
| 1397 |
| 1398 |
| 1399 | static bssl::UniquePtr<EVP_PKEY> GetECDSATestKey() { |
| 1400 | static const char kKeyPEM[] = |
| 1401 | "-----BEGIN PRIVATE KEY-----\n" |
| 1402 | "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgBw8IcnrUoEqc3VnJ\n" |
| 1403 | "TYlodwi1b8ldMHcO6NHJzgqLtGqhRANCAATmK2niv2Wfl74vHg2UikzVl2u3qR4N\n" |
| 1404 | "Rvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYaHPUdfvGULUvPciLB\n" |
| 1405 | "-----END PRIVATE KEY-----\n"; |
| 1406 | return KeyFromPEM(kKeyPEM); |
| 1407 | } |
| 1408 |
| 1409 | static bssl::UniquePtr<CRYPTO_BUFFER> GetChainTestCertificateBuffer() { |
| 1410 | static const char kCertPEM[] = |
| 1411 | "-----BEGIN CERTIFICATE-----\n" |
| 1412 | "MIIC0jCCAbqgAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwDzENMAsGA1UEAwwEQiBD\n" |
| 1413 | "QTAeFw0xNjAyMjgyMDI3MDNaFw0yNjAyMjUyMDI3MDNaMBgxFjAUBgNVBAMMDUNs\n" |
| 1414 | "aWVudCBDZXJ0IEEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDRvaz8\n" |
| 1415 | "CC/cshpCafJo4jLkHEoBqDLhdgFelJoAiQUyIqyWl2O7YHPnpJH+TgR7oelzNzt/\n" |
| 1416 | "kLRcH89M/TszB6zqyLTC4aqmvzKL0peD/jL2LWBucR0WXIvjA3zoRuF/x86+rYH3\n" |
| 1417 | "tHb+xs2PSs8EGL/Ev+ss+qTzTGEn26fuGNHkNw6tOwPpc+o8+wUtzf/kAthamo+c\n" |
| 1418 | "IDs2rQ+lP7+aLZTLeU/q4gcLutlzcK5imex5xy2jPkweq48kijK0kIzl1cPlA5d1\n" |
| 1419 | "z7C8jU50Pj9X9sQDJTN32j7UYRisJeeYQF8GaaN8SbrDI6zHgKzrRLyxDt/KQa9V\n" |
| 1420 | "iLeXANgZi+Xx9KgfAgMBAAGjLzAtMAwGA1UdEwEB/wQCMAAwHQYDVR0lBBYwFAYI\n" |
| 1421 | "KwYBBQUHAwEGCCsGAQUFBwMCMA0GCSqGSIb3DQEBCwUAA4IBAQBFEVbmYl+2RtNw\n" |
| 1422 | "rDftRDF1v2QUbcN2ouSnQDHxeDQdSgasLzT3ui8iYu0Rw2WWcZ0DV5e0ztGPhWq7\n" |
| 1423 | "AO0B120aFRMOY+4+bzu9Q2FFkQqc7/fKTvTDzIJI5wrMnFvUfzzvxh3OHWMYSs/w\n" |
| 1424 | "giq33hTKeHEq6Jyk3btCny0Ycecyc3yGXH10sizUfiHlhviCkDuESk8mFDwDDzqW\n" |
| 1425 | "ZF0IipzFbEDHoIxLlm3GQxpiLoEV4k8KYJp3R5KBLFyxM6UGPz8h72mIPCJp2RuK\n" |
| 1426 | "MYgF91UDvVzvnYm6TfseM2+ewKirC00GOrZ7rEcFvtxnKSqYf4ckqfNdSU1Y+RRC\n" |
| 1427 | "1ngWZ7Ih\n" |
| 1428 | "-----END CERTIFICATE-----\n"; |
| 1429 | return BufferFromPEM(kCertPEM); |
| 1430 | } |
| 1431 |
| 1432 | static bssl::UniquePtr<X509> GetChainTestCertificate() { |
| 1433 | return X509FromBuffer(GetChainTestCertificateBuffer()); |
| 1434 | } |
| 1435 |
| 1436 | static bssl::UniquePtr<CRYPTO_BUFFER> GetChainTestIntermediateBuffer() { |
| 1437 | static const char kCertPEM[] = |
| 1438 | "-----BEGIN CERTIFICATE-----\n" |
| 1439 | "MIICwjCCAaqgAwIBAgICEAEwDQYJKoZIhvcNAQELBQAwFDESMBAGA1UEAwwJQyBS\n" |
| 1440 | "b290IENBMB4XDTE2MDIyODIwMjcwM1oXDTI2MDIyNTIwMjcwM1owDzENMAsGA1UE\n" |
| 1441 | "AwwEQiBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALsSCYmDip2D\n" |
| 1442 | "GkjFxw7ykz26JSjELkl6ArlYjFJ3aT/SCh8qbS4gln7RH8CPBd78oFdfhIKQrwtZ\n" |
| 1443 | "3/q21ykD9BAS3qHe2YdcJfm8/kWAy5DvXk6NXU4qX334KofBAEpgdA/igEFq1P1l\n" |
| 1444 | "HAuIfZCpMRfT+i5WohVsGi8f/NgpRvVaMONLNfgw57mz1lbtFeBEISmX0kbsuJxF\n" |
| 1445 | "Qj/Bwhi5/0HAEXG8e7zN4cEx0yPRvmOATRdVb/8dW2pwOHRJq9R5M0NUkIsTSnL7\n" |
| 1446 | "6N/z8hRAHMsV3IudC5Yd7GXW1AGu9a+iKU+Q4xcZCoj0DC99tL4VKujrV1kAeqsM\n" |
| 1447 | "cz5/dKzi6+cCAwEAAaMjMCEwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" |
| 1448 | "AQYwDQYJKoZIhvcNAQELBQADggEBAIIeZiEeNhWWQ8Y4D+AGDwqUUeG8NjCbKrXQ\n" |
| 1449 | "BlHg5wZ8xftFaiP1Dp/UAezmx2LNazdmuwrYB8lm3FVTyaPDTKEGIPS4wJKHgqH1\n" |
| 1450 | "QPDhqNm85ey7TEtI9oYjsNim/Rb+iGkIAMXaxt58SzxbjvP0kMr1JfJIZbic9vye\n" |
| 1451 | "NwIspMFIpP3FB8ywyu0T0hWtCQgL4J47nigCHpOu58deP88fS/Nyz/fyGVWOZ76b\n" |
| 1452 | "WhWwgM3P3X95fQ3d7oFPR/bVh0YV+Cf861INwplokXgXQ3/TCQ+HNXeAMWn3JLWv\n" |
| 1453 | "XFwk8owk9dq/kQGdndGgy3KTEW4ctPX5GNhf3LJ9Q7dLji4ReQ4=\n" |
| 1454 | "-----END CERTIFICATE-----\n"; |
| 1455 | return BufferFromPEM(kCertPEM); |
| 1456 | } |
| 1457 |
| 1458 | static bssl::UniquePtr<CRYPTO_BUFFER> GetChainTestIntermediateIssuerBuffer() { |
| 1459 | static const char kSubjectPEM[] = |
| 1460 | "-----BEGIN SUBJECT-----\n" |
| 1461 | "MBQxEjAQBgNVBAMMCUMgUm9vdCBDQQ==\n" |
| 1462 | "-----END SUBJECT-----\n"; |
| 1463 | return BufferFromPEM(kSubjectPEM); |
| 1464 | } |
| 1465 |
| 1466 | static bssl::UniquePtr<CRYPTO_BUFFER> GetTestCertIssuerBuffer() { |
| 1467 | static const char kSubjectPEM[] = |
| 1468 | "-----BEGIN SUBJECT-----\n" |
| 1469 | "MEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJ\n" |
| 1470 | "bnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQ=\n" |
| 1471 | "-----END SUBJECT-----\n"; |
| 1472 | return BufferFromPEM(kSubjectPEM); |
| 1473 | } |
| 1474 |
| 1475 | static bssl::UniquePtr<CRYPTO_BUFFER> GetBogusIssuerBuffer() { |
| 1476 | static const char kSubjectPEM[] = |
| 1477 | "-----BEGIN SUBJECT-----\n" |
| 1478 | "MBYxFDASBgNVBAMMC0RpZ2lOb3RBRm94\n" |
| 1479 | "-----END SUBJECT-----\n"; |
| 1480 | return BufferFromPEM(kSubjectPEM); |
| 1481 | } |
| 1482 |
| 1483 | static bssl::UniquePtr<X509> GetChainTestIntermediate() { |
| 1484 | return X509FromBuffer(GetChainTestIntermediateBuffer()); |
| 1485 | } |
| 1486 |
| 1487 | static bssl::UniquePtr<EVP_PKEY> GetChainTestKey() { |
| 1488 | static const char kKeyPEM[] = |
| 1489 | "-----BEGIN PRIVATE KEY-----\n" |
| 1490 | "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDRvaz8CC/cshpC\n" |
| 1491 | "afJo4jLkHEoBqDLhdgFelJoAiQUyIqyWl2O7YHPnpJH+TgR7oelzNzt/kLRcH89M\n" |
| 1492 | "/TszB6zqyLTC4aqmvzKL0peD/jL2LWBucR0WXIvjA3zoRuF/x86+rYH3tHb+xs2P\n" |
| 1493 | "Ss8EGL/Ev+ss+qTzTGEn26fuGNHkNw6tOwPpc+o8+wUtzf/kAthamo+cIDs2rQ+l\n" |
| 1494 | "P7+aLZTLeU/q4gcLutlzcK5imex5xy2jPkweq48kijK0kIzl1cPlA5d1z7C8jU50\n" |
| 1495 | "Pj9X9sQDJTN32j7UYRisJeeYQF8GaaN8SbrDI6zHgKzrRLyxDt/KQa9ViLeXANgZ\n" |
| 1496 | "i+Xx9KgfAgMBAAECggEBAK0VjSJzkyPaamcyTVSWjo7GdaBGcK60lk657RjR+lK0\n" |
| 1497 | "YJ7pkej4oM2hdsVZFsP8Cs4E33nXLa/0pDsRov/qrp0WQm2skwqGMC1I/bZ0WRPk\n" |
| 1498 | "wHaDrBBfESWnJDX/AGpVtlyOjPmgmK6J2usMPihQUDkKdAYrVWJePrMIxt1q6BMe\n" |
| 1499 | "iczs3qriMmtY3bUc4UyUwJ5fhDLjshHvfuIpYQyI6EXZM6dZksn9LylXJnigY6QJ\n" |
| 1500 | "HxOYO0BDwOsZ8yQ8J8afLk88i0GizEkgE1z3REtQUwgWfxr1WV/ud+T6/ZhSAgH9\n" |
| 1501 | "042mQvSFZnIUSEsmCvjhWuAunfxHKCTcAoYISWfzWpkCgYEA7gpf3HHU5Tn+CgUn\n" |
| 1502 | "1X5uGpG3DmcMgfeGgs2r2f/IIg/5Ac1dfYILiybL1tN9zbyLCJfcbFpWBc9hJL6f\n" |
| 1503 | "CPc5hUiwWFJqBJewxQkC1Ae/HakHbip+IZ+Jr0842O4BAArvixk4Lb7/N2Ct9sTE\n" |
| 1504 | "NJO6RtK9lbEZ5uK61DglHy8CS2UCgYEA4ZC1o36kPAMQBggajgnucb2yuUEelk0f\n" |
| 1505 | "AEr+GI32MGE+93xMr7rAhBoqLg4AITyIfEnOSQ5HwagnIHonBbv1LV/Gf9ursx8Z\n" |
| 1506 | "YOGbvT8zzzC+SU1bkDzdjAYnFQVGIjMtKOBJ3K07++ypwX1fr4QsQ8uKL8WSOWwt\n" |
| 1507 | "Z3Bym6XiZzMCgYADnhy+2OwHX85AkLt+PyGlPbmuelpyTzS4IDAQbBa6jcuW/2wA\n" |
| 1508 | "UE2km75VUXmD+u2R/9zVuLm99NzhFhSMqlUxdV1YukfqMfP5yp1EY6m/5aW7QuIP\n" |
| 1509 | "2MDa7TVL9rIFMiVZ09RKvbBbQxjhuzPQKL6X/PPspnhiTefQ+dl2k9xREQKBgHDS\n" |
| 1510 | "fMfGNEeAEKezrfSVqxphE9/tXms3L+ZpnCaT+yu/uEr5dTIAawKoQ6i9f/sf1/Sy\n" |
| 1511 | "xedsqR+IB+oKrzIDDWMgoJybN4pkZ8E5lzhVQIjFjKgFdWLzzqyW9z1gYfABQPlN\n" |
| 1512 | "FiS20WX0vgP1vcKAjdNrHzc9zyHBpgQzDmAj3NZZAoGBAI8vKCKdH7w3aL5CNkZQ\n" |
| 1513 | "2buIeWNA2HZazVwAGG5F2TU/LmXfRKnG6dX5bkU+AkBZh56jNZy//hfFSewJB4Kk\n" |
| 1514 | "buB7ERSdaNbO21zXt9FEA3+z0RfMd/Zv2vlIWOSB5nzl/7UKti3sribK6s9ZVLfi\n" |
| 1515 | "SxpiPQ8d/hmSGwn4ksrWUsJD\n" |
| 1516 | "-----END PRIVATE KEY-----\n"; |
| 1517 | return KeyFromPEM(kKeyPEM); |
| 1518 | } |
| 1519 |
| 1520 | static bool CompleteHandshakes(SSL *client, SSL *server) { |
| 1521 | // Drive both their handshakes to completion. |
| 1522 | for (;;) { |
| 1523 | int client_ret = SSL_do_handshake(client); |
| 1524 | int client_err = SSL_get_error(client, client_ret); |
| 1525 | if (client_err != SSL_ERROR_NONE && // |
| 1526 | client_err != SSL_ERROR_WANT_READ && // |
| 1527 | client_err != SSL_ERROR_WANT_WRITE && // |
| 1528 | client_err != SSL_ERROR_PENDING_TICKET) { |
| 1529 | fprintf(stderr, "Client error: %s\n", SSL_error_description(client_err)); |
| 1530 | return false; |
| 1531 | } |
| 1532 |
| 1533 | int server_ret = SSL_do_handshake(server); |
| 1534 | int server_err = SSL_get_error(server, server_ret); |
| 1535 | if (server_err != SSL_ERROR_NONE && // |
| 1536 | server_err != SSL_ERROR_WANT_READ && // |
| 1537 | server_err != SSL_ERROR_WANT_WRITE && // |
| 1538 | server_err != SSL_ERROR_PENDING_TICKET) { |
| 1539 | fprintf(stderr, "Server error: %s\n", SSL_error_description(server_err)); |
| 1540 | return false; |
| 1541 | } |
| 1542 |
| 1543 | if (client_ret == 1 && server_ret == 1) { |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
| 1547 |
| 1548 | return true; |
| 1549 | } |
| 1550 |
| 1551 | static bool FlushNewSessionTickets(SSL *client, SSL *server) { |
| 1552 | // NewSessionTickets are deferred on the server to |SSL_write|, and clients do |
| 1553 | // not pick them up until |SSL_read|. |
| 1554 | for (;;) { |
| 1555 | int server_ret = SSL_write(server, nullptr, 0); |
| 1556 | int server_err = SSL_get_error(server, server_ret); |
| 1557 | // The server may either succeed (|server_ret| is zero) or block on write |
| 1558 | // (|server_ret| is -1 and |server_err| is |SSL_ERROR_WANT_WRITE|). |
| 1559 | if (server_ret > 0 || |
| 1560 | (server_ret < 0 && server_err != SSL_ERROR_WANT_WRITE)) { |
| 1561 | fprintf(stderr, "Unexpected server result: %d %d\n", server_ret, |
| 1562 | server_err); |
| 1563 | return false; |
| 1564 | } |
| 1565 |
| 1566 | int client_ret = SSL_read(client, nullptr, 0); |
| 1567 | int client_err = SSL_get_error(client, client_ret); |
| 1568 | // The client must always block on read. |
| 1569 | if (client_ret != -1 || client_err != SSL_ERROR_WANT_READ) { |
| 1570 | fprintf(stderr, "Unexpected client result: %d %d\n", client_ret, |
| 1571 | client_err); |
| 1572 | return false; |
| 1573 | } |
| 1574 |
| 1575 | // The server flushed everything it had to write. |
| 1576 | if (server_ret == 0) { |
| 1577 | return true; |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 |
| 1582 | // CreateClientAndServer creates a client and server |SSL| objects whose |BIO|s |
| 1583 | // are paired with each other. It does not run the handshake. The caller is |
| 1584 | // expected to configure the objects and drive the handshake as needed. |
| 1585 | static bool CreateClientAndServer(bssl::UniquePtr<SSL> *out_client, |
| 1586 | bssl::UniquePtr<SSL> *out_server, |
| 1587 | SSL_CTX *client_ctx, SSL_CTX *server_ctx) { |
| 1588 | bssl::UniquePtr<SSL> client(SSL_new(client_ctx)), server(SSL_new(server_ctx)); |
| 1589 | if (!client || !server) { |
| 1590 | return false; |
| 1591 | } |
| 1592 | SSL_set_connect_state(client.get()); |
| 1593 | SSL_set_accept_state(server.get()); |
| 1594 |
| 1595 | BIO *bio1, *bio2; |
| 1596 | if (!BIO_new_bio_pair(&bio1, 0, &bio2, 0)) { |
| 1597 | return false; |
| 1598 | } |
| 1599 | // SSL_set_bio takes ownership. |
| 1600 | SSL_set_bio(client.get(), bio1, bio1); |
| 1601 | SSL_set_bio(server.get(), bio2, bio2); |
| 1602 |
| 1603 | *out_client = std::move(client); |
| 1604 | *out_server = std::move(server); |
| 1605 | return true; |
| 1606 | } |
| 1607 |
| 1608 | struct ClientConfig { |
| 1609 | SSL_SESSION *session = nullptr; |
| 1610 | STACK_OF(CRYPTO_BUFFER) *ca_names = nullptr; |
| 1611 | std::string servername; |
| 1612 | std::string verify_hostname; |
| 1613 | unsigned hostflags = 0; |
| 1614 | bool early_data = false; |
| 1615 | }; |
| 1616 |
| 1617 | static bool ConnectClientAndServer(bssl::UniquePtr<SSL> *out_client, |
| 1618 | bssl::UniquePtr<SSL> *out_server, |
| 1619 | SSL_CTX *client_ctx, SSL_CTX *server_ctx, |
| 1620 | const ClientConfig &config = ClientConfig(), |
| 1621 | bool shed_handshake_config = true) { |
| 1622 | bssl::UniquePtr<SSL> client, server; |
| 1623 | if (!CreateClientAndServer(&client, &server, client_ctx, server_ctx)) { |
| 1624 | return false; |
| 1625 | } |
| 1626 | if (config.early_data) { |
| 1627 | SSL_set_early_data_enabled(client.get(), 1); |
| 1628 | } |
| 1629 | if (config.session) { |
| 1630 | SSL_set_session(client.get(), config.session); |
| 1631 | } |
| 1632 | if (!config.servername.empty() && |
| 1633 | !SSL_set_tlsext_host_name(client.get(), config.servername.c_str())) { |
| 1634 | return false; |
| 1635 | } |
| 1636 | if (!config.verify_hostname.empty()) { |
| 1637 | if (!SSL_set1_host(client.get(), config.verify_hostname.c_str())) { |
| 1638 | return false; |
| 1639 | } |
| 1640 | SSL_set_hostflags(client.get(), config.hostflags); |
| 1641 | } |
| 1642 |
| 1643 | if (config.ca_names) { |
| 1644 | SSL_set0_CA_names(client.get(), config.ca_names); |
| 1645 | } |
| 1646 |
| 1647 | SSL_set_shed_handshake_config(client.get(), shed_handshake_config); |
| 1648 | SSL_set_shed_handshake_config(server.get(), shed_handshake_config); |
| 1649 |
| 1650 | if (!CompleteHandshakes(client.get(), server.get())) { |
| 1651 | return false; |
| 1652 | } |
| 1653 |
| 1654 | *out_client = std::move(client); |
| 1655 | *out_server = std::move(server); |
| 1656 | return true; |
| 1657 | } |
| 1658 |
| 1659 | static bssl::UniquePtr<SSL_SESSION> g_last_session; |
| 1660 |
| 1661 | static int SaveLastSession(SSL *ssl, SSL_SESSION *session) { |
| 1662 | // Save the most recent session. |
| 1663 | g_last_session.reset(session); |
| 1664 | return 1; |
| 1665 | } |
| 1666 |
| 1667 | static bssl::UniquePtr<SSL_SESSION> CreateClientSession( |
| 1668 | SSL_CTX *client_ctx, SSL_CTX *server_ctx, |
| 1669 | const ClientConfig &config = ClientConfig()) { |
| 1670 | g_last_session = nullptr; |
| 1671 | SSL_CTX_sess_set_new_cb(client_ctx, SaveLastSession); |
| 1672 |
| 1673 | // Connect client and server to get a session. |
| 1674 | bssl::UniquePtr<SSL> client, server; |
| 1675 | if (!ConnectClientAndServer(&client, &server, client_ctx, server_ctx, |
| 1676 | config) || |
| 1677 | !FlushNewSessionTickets(client.get(), server.get())) { |
| 1678 | fprintf(stderr, "Failed to connect client and server.\n"); |
| 1679 | return nullptr; |
| 1680 | } |
| 1681 |
| 1682 | SSL_CTX_sess_set_new_cb(client_ctx, nullptr); |
| 1683 |
| 1684 | if (!g_last_session) { |
| 1685 | fprintf(stderr, "Client did not receive a session.\n"); |
| 1686 | return nullptr; |
| 1687 | } |
| 1688 | return std::move(g_last_session); |
| 1689 | } |
| 1690 |
| 1691 | static void SetUpExpectedNewCodePoint(SSL_CTX *ctx) { |
| 1692 | SSL_CTX_set_select_certificate_cb( |
| 1693 | ctx, |
| 1694 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 1695 | const uint8_t *data; |
| 1696 | size_t len; |
| 1697 | if (!SSL_early_callback_ctx_extension_get( |
| 1698 | client_hello, TLSEXT_TYPE_application_settings, &data, &len)) { |
| 1699 | ADD_FAILURE() << "Could not find alps new codepoint."; |
| 1700 | return ssl_select_cert_error; |
| 1701 | } |
| 1702 | return ssl_select_cert_success; |
| 1703 | }); |
| 1704 | } |
| 1705 |
| 1706 | static void SetUpExpectedOldCodePoint(SSL_CTX *ctx) { |
| 1707 | SSL_CTX_set_select_certificate_cb( |
| 1708 | ctx, |
| 1709 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 1710 | const uint8_t *data; |
| 1711 | size_t len; |
| 1712 | if (!SSL_early_callback_ctx_extension_get( |
| 1713 | client_hello, TLSEXT_TYPE_application_settings_old, &data, |
| 1714 | &len)) { |
| 1715 | ADD_FAILURE() << "Could not find alps old codepoint."; |
| 1716 | return ssl_select_cert_error; |
| 1717 | } |
| 1718 | return ssl_select_cert_success; |
| 1719 | }); |
| 1720 | } |
| 1721 |
| 1722 | // Test that |SSL_get_client_CA_list| echoes back the configured parameter even |
| 1723 | // before configuring as a server. |
| 1724 | TEST(SSLTest, ClientCAList) { |
| 1725 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1726 | ASSERT_TRUE(ctx); |
| 1727 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1728 | ASSERT_TRUE(ssl); |
| 1729 |
| 1730 | bssl::UniquePtr<X509_NAME> name(X509_NAME_new()); |
| 1731 | ASSERT_TRUE(name); |
| 1732 |
| 1733 | bssl::UniquePtr<X509_NAME> name_dup(X509_NAME_dup(name.get())); |
| 1734 | ASSERT_TRUE(name_dup); |
| 1735 |
| 1736 | bssl::UniquePtr<STACK_OF(X509_NAME)> stack(sk_X509_NAME_new_null()); |
| 1737 | ASSERT_TRUE(stack); |
| 1738 | ASSERT_TRUE(PushToStack(stack.get(), std::move(name_dup))); |
| 1739 |
| 1740 | // |SSL_set_client_CA_list| takes ownership. |
| 1741 | SSL_set_client_CA_list(ssl.get(), stack.release()); |
| 1742 |
| 1743 | STACK_OF(X509_NAME) *result = SSL_get_client_CA_list(ssl.get()); |
| 1744 | ASSERT_TRUE(result); |
| 1745 | ASSERT_EQ(1u, sk_X509_NAME_num(result)); |
| 1746 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(result, 0), name.get())); |
| 1747 | } |
| 1748 |
| 1749 | TEST(SSLTest, AddClientCA) { |
| 1750 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 1751 | ASSERT_TRUE(ctx); |
| 1752 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 1753 | ASSERT_TRUE(ssl); |
| 1754 |
| 1755 | bssl::UniquePtr<X509> cert1 = GetTestCertificate(); |
| 1756 | bssl::UniquePtr<X509> cert2 = GetChainTestCertificate(); |
| 1757 | ASSERT_TRUE(cert1 && cert2); |
| 1758 | X509_NAME *name1 = X509_get_subject_name(cert1.get()); |
| 1759 | X509_NAME *name2 = X509_get_subject_name(cert2.get()); |
| 1760 |
| 1761 | EXPECT_EQ(0u, sk_X509_NAME_num(SSL_get_client_CA_list(ssl.get()))); |
| 1762 |
| 1763 | ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert1.get())); |
| 1764 | ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert2.get())); |
| 1765 |
| 1766 | STACK_OF(X509_NAME) *list = SSL_get_client_CA_list(ssl.get()); |
| 1767 | ASSERT_EQ(2u, sk_X509_NAME_num(list)); |
| 1768 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 0), name1)); |
| 1769 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 1), name2)); |
| 1770 |
| 1771 | ASSERT_TRUE(SSL_add_client_CA(ssl.get(), cert1.get())); |
| 1772 |
| 1773 | list = SSL_get_client_CA_list(ssl.get()); |
| 1774 | ASSERT_EQ(3u, sk_X509_NAME_num(list)); |
| 1775 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 0), name1)); |
| 1776 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 1), name2)); |
| 1777 | EXPECT_EQ(0, X509_NAME_cmp(sk_X509_NAME_value(list, 2), name1)); |
| 1778 | } |
| 1779 |
| 1780 | struct ECHConfigParams { |
| 1781 | uint16_t version = TLSEXT_TYPE_encrypted_client_hello; |
| 1782 | uint16_t config_id = 1; |
| 1783 | std::string public_name = "example.com"; |
| 1784 | const EVP_HPKE_KEY *key = nullptr; |
| 1785 | // kem_id, if zero, takes its value from |key|. |
| 1786 | uint16_t kem_id = 0; |
| 1787 | // public_key, if empty takes its value from |key|. |
| 1788 | std::vector<uint8_t> public_key; |
| 1789 | size_t max_name_len = 16; |
| 1790 | // cipher_suites is a list of code points which should contain pairs of KDF |
| 1791 | // and AEAD IDs. |
| 1792 | std::vector<uint16_t> cipher_suites = {EVP_HPKE_HKDF_SHA256, |
| 1793 | EVP_HPKE_AES_128_GCM}; |
| 1794 | std::vector<uint8_t> extensions; |
| 1795 | }; |
| 1796 |
| 1797 | // MakeECHConfig serializes an ECHConfig from |params| and writes it to |
| 1798 | // |*out|. |
| 1799 | bool MakeECHConfig(std::vector<uint8_t> *out, const ECHConfigParams ¶ms) { |
| 1800 | uint16_t kem_id = params.kem_id == 0 |
| 1801 | ? EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(params.key)) |
| 1802 | : params.kem_id; |
| 1803 | std::vector<uint8_t> public_key = params.public_key; |
| 1804 | if (public_key.empty()) { |
| 1805 | public_key.resize(EVP_HPKE_MAX_PUBLIC_KEY_LENGTH); |
| 1806 | size_t len; |
| 1807 | if (!EVP_HPKE_KEY_public_key(params.key, public_key.data(), &len, |
| 1808 | public_key.size())) { |
| 1809 | return false; |
| 1810 | } |
| 1811 | public_key.resize(len); |
| 1812 | } |
| 1813 |
| 1814 | bssl::ScopedCBB cbb; |
| 1815 | CBB contents, child; |
| 1816 | if (!CBB_init(cbb.get(), 64) || // |
| 1817 | !CBB_add_u16(cbb.get(), params.version) || // |
| 1818 | !CBB_add_u16_length_prefixed(cbb.get(), &contents) || // |
| 1819 | !CBB_add_u8(&contents, params.config_id) || // |
| 1820 | !CBB_add_u16(&contents, kem_id) || // |
| 1821 | !CBB_add_u16_length_prefixed(&contents, &child) || // |
| 1822 | !CBB_add_bytes(&child, public_key.data(), public_key.size()) || // |
| 1823 | !CBB_add_u16_length_prefixed(&contents, &child)) { |
| 1824 | return false; |
| 1825 | } |
| 1826 | for (uint16_t cipher_suite : params.cipher_suites) { |
| 1827 | if (!CBB_add_u16(&child, cipher_suite)) { |
| 1828 | return false; |
| 1829 | } |
| 1830 | } |
| 1831 | if (!CBB_add_u8(&contents, params.max_name_len) || |
| 1832 | !CBB_add_u8_length_prefixed(&contents, &child) || |
| 1833 | !CBB_add_bytes( |
| 1834 | &child, reinterpret_cast<const uint8_t *>(params.public_name.data()), |
| 1835 | params.public_name.size()) || |
| 1836 | !CBB_add_u16_length_prefixed(&contents, &child) || |
| 1837 | !CBB_add_bytes(&child, params.extensions.data(), |
| 1838 | params.extensions.size()) || |
| 1839 | !CBB_flush(cbb.get())) { |
| 1840 | return false; |
| 1841 | } |
| 1842 |
| 1843 | out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get())); |
| 1844 | return true; |
| 1845 | } |
| 1846 |
| 1847 | static bssl::UniquePtr<SSL_ECH_KEYS> MakeTestECHKeys(uint8_t config_id = 1) { |
| 1848 | bssl::ScopedEVP_HPKE_KEY key; |
| 1849 | uint8_t *ech_config; |
| 1850 | size_t ech_config_len; |
| 1851 | if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256()) || |
| 1852 | !SSL_marshal_ech_config(&ech_config, &ech_config_len, config_id, |
| 1853 | key.get(), "public.example", 16)) { |
| 1854 | return nullptr; |
| 1855 | } |
| 1856 | bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 1857 |
| 1858 | // Install a non-retry config. |
| 1859 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 1860 | if (!keys || !SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 1861 | ech_config_len, key.get())) { |
| 1862 | return nullptr; |
| 1863 | } |
| 1864 | return keys; |
| 1865 | } |
| 1866 |
| 1867 | static bool InstallECHConfigList(SSL *client, const SSL_ECH_KEYS *keys) { |
| 1868 | uint8_t *ech_config_list; |
| 1869 | size_t ech_config_list_len; |
| 1870 | if (!SSL_ECH_KEYS_marshal_retry_configs(keys, &ech_config_list, |
| 1871 | &ech_config_list_len)) { |
| 1872 | return false; |
| 1873 | } |
| 1874 | bssl::UniquePtr<uint8_t> free_ech_config_list(ech_config_list); |
| 1875 | return SSL_set1_ech_config_list(client, ech_config_list, ech_config_list_len); |
| 1876 | } |
| 1877 |
| 1878 | // Test that |SSL_marshal_ech_config| and |SSL_ECH_KEYS_marshal_retry_configs| |
| 1879 | // output values as expected. |
| 1880 | TEST(SSLTest, MarshalECHConfig) { |
| 1881 | static const uint8_t kPrivateKey[X25519_PRIVATE_KEY_LEN] = { |
| 1882 | 0xbc, 0xb5, 0x51, 0x29, 0x31, 0x10, 0x30, 0xc9, 0xed, 0x26, 0xde, |
| 1883 | 0xd4, 0xb3, 0xdf, 0x3a, 0xce, 0x06, 0x8a, 0xee, 0x17, 0xab, 0xce, |
| 1884 | 0xd7, 0xdb, 0xf3, 0x11, 0xe5, 0xa8, 0xf3, 0xb1, 0x8e, 0x24}; |
| 1885 | bssl::ScopedEVP_HPKE_KEY key; |
| 1886 | ASSERT_TRUE(EVP_HPKE_KEY_init(key.get(), EVP_hpke_x25519_hkdf_sha256(), |
| 1887 | kPrivateKey, sizeof(kPrivateKey))); |
| 1888 |
| 1889 | static const uint8_t kECHConfig[] = { |
| 1890 | // version |
| 1891 | 0xfe, 0x0d, |
| 1892 | // length |
| 1893 | 0x00, 0x41, |
| 1894 | // contents.config_id |
| 1895 | 0x01, |
| 1896 | // contents.kem_id |
| 1897 | 0x00, 0x20, |
| 1898 | // contents.public_key |
| 1899 | 0x00, 0x20, 0xa6, 0x9a, 0x41, 0x48, 0x5d, 0x32, 0x96, 0xa4, 0xe0, 0xc3, |
| 1900 | 0x6a, 0xee, 0xf6, 0x63, 0x0f, 0x59, 0x32, 0x6f, 0xdc, 0xff, 0x81, 0x29, |
| 1901 | 0x59, 0xa5, 0x85, 0xd3, 0x9b, 0x3b, 0xde, 0x98, 0x55, 0x5c, |
| 1902 | // contents.cipher_suites |
| 1903 | 0x00, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x03, |
| 1904 | // contents.maximum_name_length |
| 1905 | 0x10, |
| 1906 | // contents.public_name |
| 1907 | 0x0e, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x65, 0x78, 0x61, 0x6d, |
| 1908 | 0x70, 0x6c, 0x65, |
| 1909 | // contents.extensions |
| 1910 | 0x00, 0x00}; |
| 1911 | uint8_t *ech_config; |
| 1912 | size_t ech_config_len; |
| 1913 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 1914 | /*config_id=*/1, key.get(), |
| 1915 | "public.example", 16)); |
| 1916 | bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 1917 | EXPECT_EQ(Bytes(kECHConfig), Bytes(ech_config, ech_config_len)); |
| 1918 |
| 1919 | // Generate a second ECHConfig. |
| 1920 | bssl::ScopedEVP_HPKE_KEY key2; |
| 1921 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key2.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 1922 | uint8_t *ech_config2; |
| 1923 | size_t ech_config2_len; |
| 1924 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config2, &ech_config2_len, |
| 1925 | /*config_id=*/2, key2.get(), |
| 1926 | "public.example", 16)); |
| 1927 | bssl::UniquePtr<uint8_t> free_ech_config2(ech_config2); |
| 1928 |
| 1929 | // Install both ECHConfigs in an |SSL_ECH_KEYS|. |
| 1930 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 1931 | ASSERT_TRUE(keys); |
| 1932 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 1933 | ech_config_len, key.get())); |
| 1934 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config2, |
| 1935 | ech_config2_len, key2.get())); |
| 1936 |
| 1937 | // The ECHConfigList should be correctly serialized. |
| 1938 | uint8_t *ech_config_list; |
| 1939 | size_t ech_config_list_len; |
| 1940 | ASSERT_TRUE(SSL_ECH_KEYS_marshal_retry_configs(keys.get(), &ech_config_list, |
| 1941 | &ech_config_list_len)); |
| 1942 | bssl::UniquePtr<uint8_t> free_ech_config_list(ech_config_list); |
| 1943 |
| 1944 | // ECHConfigList is just the concatenation with a length prefix. |
| 1945 | size_t len = ech_config_len + ech_config2_len; |
| 1946 | std::vector<uint8_t> expected = {uint8_t(len >> 8), uint8_t(len)}; |
| 1947 | expected.insert(expected.end(), ech_config, ech_config + ech_config_len); |
| 1948 | expected.insert(expected.end(), ech_config2, ech_config2 + ech_config2_len); |
| 1949 | EXPECT_EQ(Bytes(expected), Bytes(ech_config_list, ech_config_list_len)); |
| 1950 | } |
| 1951 |
| 1952 | TEST(SSLTest, ECHHasDuplicateConfigID) { |
| 1953 | const struct { |
| 1954 | std::vector<uint8_t> ids; |
| 1955 | bool has_duplicate; |
| 1956 | } kTests[] = { |
| 1957 | {{}, false}, |
| 1958 | {{1}, false}, |
| 1959 | {{1, 2, 3, 255}, false}, |
| 1960 | {{1, 2, 3, 1}, true}, |
| 1961 | }; |
| 1962 | for (const auto &test : kTests) { |
| 1963 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 1964 | ASSERT_TRUE(keys); |
| 1965 | for (const uint8_t id : test.ids) { |
| 1966 | bssl::ScopedEVP_HPKE_KEY key; |
| 1967 | ASSERT_TRUE( |
| 1968 | EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 1969 | uint8_t *ech_config; |
| 1970 | size_t ech_config_len; |
| 1971 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, id, |
| 1972 | key.get(), "public.example", 16)); |
| 1973 | bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 1974 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 1975 | ech_config, ech_config_len, key.get())); |
| 1976 | } |
| 1977 |
| 1978 | EXPECT_EQ(test.has_duplicate ? 1 : 0, |
| 1979 | SSL_ECH_KEYS_has_duplicate_config_id(keys.get())); |
| 1980 | } |
| 1981 | } |
| 1982 |
| 1983 | // Test that |SSL_ECH_KEYS_add| checks consistency between the public and |
| 1984 | // private key. |
| 1985 | TEST(SSLTest, ECHKeyConsistency) { |
| 1986 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 1987 | ASSERT_TRUE(keys); |
| 1988 | bssl::ScopedEVP_HPKE_KEY key; |
| 1989 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 1990 | uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 1991 | size_t public_key_len; |
| 1992 | ASSERT_TRUE(EVP_HPKE_KEY_public_key(key.get(), public_key, &public_key_len, |
| 1993 | sizeof(public_key))); |
| 1994 |
| 1995 | // Adding an ECHConfig with the matching public key succeeds. |
| 1996 | ECHConfigParams params; |
| 1997 | params.key = key.get(); |
| 1998 | std::vector<uint8_t> ech_config; |
| 1999 | ASSERT_TRUE(MakeECHConfig(&ech_config, params)); |
| 2000 | EXPECT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2001 | ech_config.data(), ech_config.size(), |
| 2002 | key.get())); |
| 2003 |
| 2004 | // Adding an ECHConfig with the wrong public key is an error. |
| 2005 | bssl::ScopedEVP_HPKE_KEY wrong_key; |
| 2006 | ASSERT_TRUE( |
| 2007 | EVP_HPKE_KEY_generate(wrong_key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 2008 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2009 | ech_config.data(), ech_config.size(), |
| 2010 | wrong_key.get())); |
| 2011 |
| 2012 | // Adding an ECHConfig with a truncated public key is an error. |
| 2013 | ECHConfigParams truncated; |
| 2014 | truncated.key = key.get(); |
| 2015 | truncated.public_key.assign(public_key, public_key + public_key_len - 1); |
| 2016 | ASSERT_TRUE(MakeECHConfig(&ech_config, truncated)); |
| 2017 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2018 | ech_config.data(), ech_config.size(), |
| 2019 | key.get())); |
| 2020 |
| 2021 | // Adding an ECHConfig with the right public key, but wrong KEM ID, is an |
| 2022 | // error. |
| 2023 | ECHConfigParams wrong_kem; |
| 2024 | wrong_kem.key = key.get(); |
| 2025 | wrong_kem.kem_id = 0x0010; // DHKEM(P-256, HKDF-SHA256) |
| 2026 | ASSERT_TRUE(MakeECHConfig(&ech_config, wrong_kem)); |
| 2027 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2028 | ech_config.data(), ech_config.size(), |
| 2029 | key.get())); |
| 2030 | } |
| 2031 |
| 2032 | // Test that |SSL_CTX_set1_ech_keys| fails when the config list |
| 2033 | // has no retry configs. |
| 2034 | TEST(SSLTest, ECHServerConfigsWithoutRetryConfigs) { |
| 2035 | bssl::ScopedEVP_HPKE_KEY key; |
| 2036 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 2037 | uint8_t *ech_config; |
| 2038 | size_t ech_config_len; |
| 2039 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 2040 | /*config_id=*/1, key.get(), |
| 2041 | "public.example", 16)); |
| 2042 | bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 2043 |
| 2044 | // Install a non-retry config. |
| 2045 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 2046 | ASSERT_TRUE(keys); |
| 2047 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/0, ech_config, |
| 2048 | ech_config_len, key.get())); |
| 2049 |
| 2050 | // |keys| has no retry configs. |
| 2051 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 2052 | ASSERT_TRUE(ctx); |
| 2053 | EXPECT_FALSE(SSL_CTX_set1_ech_keys(ctx.get(), keys.get())); |
| 2054 |
| 2055 | // Add the same ECHConfig to the list, but this time mark it as a retry |
| 2056 | // config. |
| 2057 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 2058 | ech_config_len, key.get())); |
| 2059 | EXPECT_TRUE(SSL_CTX_set1_ech_keys(ctx.get(), keys.get())); |
| 2060 | } |
| 2061 |
| 2062 | // Test that the server APIs reject ECHConfigs with unsupported features. |
| 2063 | TEST(SSLTest, UnsupportedECHConfig) { |
| 2064 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 2065 | ASSERT_TRUE(keys); |
| 2066 | bssl::ScopedEVP_HPKE_KEY key; |
| 2067 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 2068 |
| 2069 | // Unsupported versions are rejected. |
| 2070 | ECHConfigParams unsupported_version; |
| 2071 | unsupported_version.version = 0xffff; |
| 2072 | unsupported_version.key = key.get(); |
| 2073 | std::vector<uint8_t> ech_config; |
| 2074 | ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_version)); |
| 2075 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2076 | ech_config.data(), ech_config.size(), |
| 2077 | key.get())); |
| 2078 |
| 2079 | // Unsupported cipher suites are rejected. (We only support HKDF-SHA256.) |
| 2080 | ECHConfigParams unsupported_kdf; |
| 2081 | unsupported_kdf.key = key.get(); |
| 2082 | unsupported_kdf.cipher_suites = {0x002 /* HKDF-SHA384 */, |
| 2083 | EVP_HPKE_AES_128_GCM}; |
| 2084 | ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_kdf)); |
| 2085 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2086 | ech_config.data(), ech_config.size(), |
| 2087 | key.get())); |
| 2088 | ECHConfigParams unsupported_aead; |
| 2089 | unsupported_aead.key = key.get(); |
| 2090 | unsupported_aead.cipher_suites = {EVP_HPKE_HKDF_SHA256, 0xffff}; |
| 2091 | ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_aead)); |
| 2092 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2093 | ech_config.data(), ech_config.size(), |
| 2094 | key.get())); |
| 2095 |
| 2096 |
| 2097 | // Unsupported extensions are rejected. |
| 2098 | ECHConfigParams extensions; |
| 2099 | extensions.key = key.get(); |
| 2100 | extensions.extensions = {0x00, 0x01, 0x00, 0x00}; |
| 2101 | ASSERT_TRUE(MakeECHConfig(&ech_config, extensions)); |
| 2102 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2103 | ech_config.data(), ech_config.size(), |
| 2104 | key.get())); |
| 2105 |
| 2106 | // Invalid public names are rejected. |
| 2107 | ECHConfigParams invalid_public_name; |
| 2108 | invalid_public_name.key = key.get(); |
| 2109 | invalid_public_name.public_name = "dns_names_have_no_underscores.example"; |
| 2110 | ASSERT_TRUE(MakeECHConfig(&ech_config, invalid_public_name)); |
| 2111 | EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 2112 | ech_config.data(), ech_config.size(), |
| 2113 | key.get())); |
| 2114 | } |
| 2115 |
| 2116 | // Test that |SSL_get_client_random| reports the correct value on both client |
| 2117 | // and server in ECH. The client sends two different random values. When ECH is |
| 2118 | // accepted, we should report the inner one. |
| 2119 | TEST(SSLTest, ECHClientRandomsMatch) { |
| 2120 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 2121 | CreateContextWithTestCertificate(TLS_method()); |
| 2122 | ASSERT_TRUE(server_ctx); |
| 2123 | bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(); |
| 2124 | ASSERT_TRUE(keys); |
| 2125 | ASSERT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys.get())); |
| 2126 |
| 2127 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2128 | ASSERT_TRUE(client_ctx); |
| 2129 | bssl::UniquePtr<SSL> client, server; |
| 2130 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 2131 | server_ctx.get())); |
| 2132 | ASSERT_TRUE(InstallECHConfigList(client.get(), keys.get())); |
| 2133 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 2134 |
| 2135 | EXPECT_TRUE(SSL_ech_accepted(client.get())); |
| 2136 | EXPECT_TRUE(SSL_ech_accepted(server.get())); |
| 2137 |
| 2138 | // An ECH server will fairly naturally record the inner ClientHello random, |
| 2139 | // but an ECH client may forget to update the random once ClientHelloInner is |
| 2140 | // selected. |
| 2141 | uint8_t client_random1[SSL3_RANDOM_SIZE]; |
| 2142 | uint8_t client_random2[SSL3_RANDOM_SIZE]; |
| 2143 | ASSERT_EQ(sizeof(client_random1), |
| 2144 | SSL_get_client_random(client.get(), client_random1, |
| 2145 | sizeof(client_random1))); |
| 2146 | ASSERT_EQ(sizeof(client_random2), |
| 2147 | SSL_get_client_random(server.get(), client_random2, |
| 2148 | sizeof(client_random2))); |
| 2149 | EXPECT_EQ(Bytes(client_random1), Bytes(client_random2)); |
| 2150 | } |
| 2151 |
| 2152 | // GetECHLength sets |*out_client_hello_len| and |*out_ech_len| to the lengths |
| 2153 | // of the ClientHello and ECH extension, respectively, when a client created |
| 2154 | // from |ctx| constructs a ClientHello with name |name| and an ECHConfig with |
| 2155 | // maximum name length |max_name_len|. |
| 2156 | static bool GetECHLength(SSL_CTX *ctx, size_t *out_client_hello_len, |
| 2157 | size_t *out_ech_len, size_t max_name_len, |
| 2158 | const char *name) { |
| 2159 | bssl::ScopedEVP_HPKE_KEY key; |
| 2160 | uint8_t *ech_config; |
| 2161 | size_t ech_config_len; |
| 2162 | if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256()) || |
| 2163 | !SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 2164 | /*config_id=*/1, key.get(), "public.example", |
| 2165 | max_name_len)) { |
| 2166 | return false; |
| 2167 | } |
| 2168 | bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 2169 |
| 2170 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 2171 | if (!keys || !SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 2172 | ech_config_len, key.get())) { |
| 2173 | return false; |
| 2174 | } |
| 2175 |
| 2176 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx)); |
| 2177 | if (!ssl || !InstallECHConfigList(ssl.get(), keys.get()) || |
| 2178 | (name != nullptr && !SSL_set_tlsext_host_name(ssl.get(), name))) { |
| 2179 | return false; |
| 2180 | } |
| 2181 | SSL_set_connect_state(ssl.get()); |
| 2182 |
| 2183 | std::vector<uint8_t> client_hello; |
| 2184 | SSL_CLIENT_HELLO parsed; |
| 2185 | const uint8_t *unused; |
| 2186 | if (!GetClientHello(ssl.get(), &client_hello)) { |
| 2187 | return false; |
| 2188 | } |
| 2189 | // Skip record and handshake headers. This assumes the ClientHello |
| 2190 | // fits in one record. |
| 2191 | auto body = |
| 2192 | Span(client_hello).subspan(SSL3_RT_HEADER_LENGTH + SSL3_HM_HEADER_LENGTH); |
| 2193 | if (!SSL_parse_client_hello(ssl.get(), &parsed, body.data(), body.size()) || |
| 2194 | !SSL_early_callback_ctx_extension_get( |
| 2195 | &parsed, TLSEXT_TYPE_encrypted_client_hello, &unused, out_ech_len)) { |
| 2196 | return false; |
| 2197 | } |
| 2198 | *out_client_hello_len = client_hello.size(); |
| 2199 | return true; |
| 2200 | } |
| 2201 |
| 2202 | TEST(SSLTest, ECHPadding) { |
| 2203 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 2204 | ASSERT_TRUE(ctx); |
| 2205 |
| 2206 | // Sample lengths with max_name_len = 128 as baseline. |
| 2207 | size_t client_hello_len_baseline, ech_len_baseline; |
| 2208 | ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len_baseline, |
| 2209 | &ech_len_baseline, 128, "example.com")); |
| 2210 |
| 2211 | // Check that all name lengths under the server's maximum look the same. |
| 2212 | for (size_t name_len : {1, 2, 32, 64, 127, 128}) { |
| 2213 | SCOPED_TRACE(name_len); |
| 2214 | size_t client_hello_len, ech_len; |
| 2215 | ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, |
| 2216 | std::string(name_len, 'a').c_str())); |
| 2217 | EXPECT_EQ(client_hello_len, client_hello_len_baseline); |
| 2218 | EXPECT_EQ(ech_len, ech_len_baseline); |
| 2219 | } |
| 2220 |
| 2221 | // When sending no SNI, we must still pad as if we are sending one. |
| 2222 | size_t client_hello_len, ech_len; |
| 2223 | ASSERT_TRUE( |
| 2224 | GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, nullptr)); |
| 2225 | EXPECT_EQ(client_hello_len, client_hello_len_baseline); |
| 2226 | EXPECT_EQ(ech_len, ech_len_baseline); |
| 2227 |
| 2228 | // Name lengths above the maximum do not get named-based padding, but the |
| 2229 | // overall input is padded to a multiple of 32. |
| 2230 | size_t client_hello_len_baseline2, ech_len_baseline2; |
| 2231 | ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len_baseline2, |
| 2232 | &ech_len_baseline2, 128, |
| 2233 | std::string(128 + 32, 'a').c_str())); |
| 2234 | EXPECT_EQ(ech_len_baseline2, ech_len_baseline + 32); |
| 2235 | // The ClientHello lengths may match if we are still under the threshold for |
| 2236 | // padding extension. |
| 2237 | EXPECT_GE(client_hello_len_baseline2, client_hello_len_baseline); |
| 2238 |
| 2239 | for (size_t name_len = 128 + 1; name_len < 128 + 32; name_len++) { |
| 2240 | SCOPED_TRACE(name_len); |
| 2241 | ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, |
| 2242 | std::string(name_len, 'a').c_str())); |
| 2243 | EXPECT_TRUE(ech_len == ech_len_baseline || ech_len == ech_len_baseline2) |
| 2244 | << ech_len; |
| 2245 | EXPECT_TRUE(client_hello_len == client_hello_len_baseline || |
| 2246 | client_hello_len == client_hello_len_baseline2) |
| 2247 | << client_hello_len; |
| 2248 | } |
| 2249 | } |
| 2250 |
| 2251 | TEST(SSLTest, ECHPublicName) { |
| 2252 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes(""))); |
| 2253 | EXPECT_TRUE(ssl_is_valid_ech_public_name(StringAsBytes("example.com"))); |
| 2254 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes(".example.com"))); |
| 2255 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example.com."))); |
| 2256 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example..com"))); |
| 2257 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("www.-example.com"))); |
| 2258 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("www.example-.com"))); |
| 2259 | EXPECT_FALSE( |
| 2260 | ssl_is_valid_ech_public_name(StringAsBytes("no_underscores.example"))); |
| 2261 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2262 | StringAsBytes("invalid_chars.\x01.example"))); |
| 2263 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2264 | StringAsBytes("invalid_chars.\xff.example"))); |
| 2265 | static const uint8_t kWithNUL[] = {'t', 'e', 's', 't', 0}; |
| 2266 | EXPECT_FALSE(ssl_is_valid_ech_public_name(kWithNUL)); |
| 2267 |
| 2268 | // Test an LDH label with every character and the maximum length. |
| 2269 | EXPECT_TRUE(ssl_is_valid_ech_public_name(StringAsBytes( |
| 2270 | "abcdefhijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"))); |
| 2271 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes( |
| 2272 | "abcdefhijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-01234567899"))); |
| 2273 |
| 2274 | // Inputs with trailing numeric components are rejected. |
| 2275 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("127.0.0.1"))); |
| 2276 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example.1"))); |
| 2277 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example.01"))); |
| 2278 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example.0x01"))); |
| 2279 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("example.0X01"))); |
| 2280 | // Leading zeros and values that overflow |uint32_t| are still rejected. |
| 2281 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2282 | StringAsBytes("example.123456789000000000000000"))); |
| 2283 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2284 | StringAsBytes("example.012345678900000000000000"))); |
| 2285 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2286 | StringAsBytes("example.0x123456789abcdefABCDEF0"))); |
| 2287 | EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 2288 | StringAsBytes("example.0x0123456789abcdefABCDEF"))); |
| 2289 | // Adding a non-digit or non-hex character makes it a valid DNS name again. |
| 2290 | // Single-component numbers are rejected. |
| 2291 | EXPECT_TRUE( |
| 2292 | ssl_is_valid_ech_public_name(StringAsBytes("example.1234567890a"))); |
| 2293 | EXPECT_TRUE( |
| 2294 | ssl_is_valid_ech_public_name(StringAsBytes("example.01234567890a"))); |
| 2295 | EXPECT_TRUE(ssl_is_valid_ech_public_name( |
| 2296 | StringAsBytes("example.0x123456789abcdefg"))); |
| 2297 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("1"))); |
| 2298 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("01"))); |
| 2299 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("0x01"))); |
| 2300 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("0X01"))); |
| 2301 | // Numbers with trailing dots are rejected. (They are already rejected by the |
| 2302 | // LDH label rules, but the WHATWG URL parser additionally rejects them.) |
| 2303 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("1."))); |
| 2304 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("01."))); |
| 2305 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("0x01."))); |
| 2306 | EXPECT_FALSE(ssl_is_valid_ech_public_name(StringAsBytes("0X01."))); |
| 2307 | } |
| 2308 |
| 2309 | // When using the built-in verifier, test that |SSL_get0_ech_name_override| is |
| 2310 | // applied automatically. |
| 2311 | TEST(SSLTest, ECHBuiltinVerifier) { |
| 2312 | // These test certificates generated with the following Go program. |
| 2313 | /* clang-format off |
| 2314 | func main() { |
| 2315 | notBefore := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 2316 | notAfter := time.Date(2099, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 2317 | rootKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 2318 | rootTemplate := &x509.Certificate{ |
| 2319 | SerialNumber: big.NewInt(1), |
| 2320 | Subject: pkix.Name{CommonName: "Test CA"}, |
| 2321 | NotBefore: notBefore, |
| 2322 | NotAfter: notAfter, |
| 2323 | BasicConstraintsValid: true, |
| 2324 | IsCA: true, |
| 2325 | } |
| 2326 | rootDER, _ := x509.CreateCertificate(rand.Reader, rootTemplate, rootTemplate, &rootKey.PublicKey, rootKey) |
| 2327 | root, _ := x509.ParseCertificate(rootDER) |
| 2328 | pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: rootDER}) |
| 2329 | leafKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 2330 | leafKeyDER, _ := x509.MarshalPKCS8PrivateKey(leafKey) |
| 2331 | pem.Encode(os.Stdout, &pem.Block{Type: "PRIVATE KEY", Bytes: leafKeyDER}) |
| 2332 | for i, name := range []string{"public.example", "secret.example"} { |
| 2333 | leafTemplate := &x509.Certificate{ |
| 2334 | SerialNumber: big.NewInt(int64(i) + 2), |
| 2335 | Subject: pkix.Name{CommonName: name}, |
| 2336 | NotBefore: notBefore, |
| 2337 | NotAfter: notAfter, |
| 2338 | BasicConstraintsValid: true, |
| 2339 | DNSNames: []string{name}, |
| 2340 | } |
| 2341 | leafDER, _ := x509.CreateCertificate(rand.Reader, leafTemplate, root, &leafKey.PublicKey, rootKey) |
| 2342 | pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: leafDER}) |
| 2343 | } |
| 2344 | } |
| 2345 | clang-format on */ |
| 2346 | bssl::UniquePtr<X509> root = CertFromPEM(R"( |
| 2347 | -----BEGIN CERTIFICATE----- |
| 2348 | MIIBRzCB7aADAgECAgEBMAoGCCqGSM49BAMCMBIxEDAOBgNVBAMTB1Rlc3QgQ0Ew |
| 2349 | IBcNMDAwMTAxMDAwMDAwWhgPMjA5OTAxMDEwMDAwMDBaMBIxEDAOBgNVBAMTB1Rl |
| 2350 | c3QgQ0EwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAT5JUjrI1DAxSpEl88UkmJw |
| 2351 | tAJqxo/YrSFo9V3MkcNkfTixi5p6MUtO8DazhEgekBcd2+tBAWtl7dy0qpvTqx92 |
| 2352 | ozIwMDAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTw6ftkexAI6o4r5FntJIfL |
| 2353 | GU5F4zAKBggqhkjOPQQDAgNJADBGAiEAiiNowddQeHZaZFIygwe6RW5/WG4sUXWC |
| 2354 | dkyl9CQzRaYCIQCFS1EvwZbZtMny27fYm1eeYciY0TkJTEi34H1KwyzzIA== |
| 2355 | -----END CERTIFICATE----- |
| 2356 | )"); |
| 2357 | ASSERT_TRUE(root); |
| 2358 | bssl::UniquePtr<EVP_PKEY> leaf_key = KeyFromPEM(R"( |
| 2359 | -----BEGIN PRIVATE KEY----- |
| 2360 | MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgj5WKHwHnziiyPauf |
| 2361 | 7QukxTwtTyGZkk8qNdms4puJfxqhRANCAARNrkhxabALDlJrHtvkuDwvCWUF/oVC |
| 2362 | hr6PDITHi1lDlJzvVT4aXBH87sH2n2UV5zpx13NHkq1bIC8eRT8eOIe0 |
| 2363 | -----END PRIVATE KEY----- |
| 2364 | )"); |
| 2365 | ASSERT_TRUE(leaf_key); |
| 2366 | bssl::UniquePtr<X509> leaf_public = CertFromPEM(R"( |
| 2367 | -----BEGIN CERTIFICATE----- |
| 2368 | MIIBaDCCAQ6gAwIBAgIBAjAKBggqhkjOPQQDAjASMRAwDgYDVQQDEwdUZXN0IENB |
| 2369 | MCAXDTAwMDEwMTAwMDAwMFoYDzIwOTkwMTAxMDAwMDAwWjAZMRcwFQYDVQQDEw5w |
| 2370 | dWJsaWMuZXhhbXBsZTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABE2uSHFpsAsO |
| 2371 | Umse2+S4PC8JZQX+hUKGvo8MhMeLWUOUnO9VPhpcEfzuwfafZRXnOnHXc0eSrVsg |
| 2372 | Lx5FPx44h7SjTDBKMAwGA1UdEwEB/wQCMAAwHwYDVR0jBBgwFoAU8On7ZHsQCOqO |
| 2373 | K+RZ7SSHyxlOReMwGQYDVR0RBBIwEIIOcHVibGljLmV4YW1wbGUwCgYIKoZIzj0E |
| 2374 | AwIDSAAwRQIhANqZRhDR/+QL05hsWXMYEwaiHifd9iakKoFEhKFchcF3AiBRAeXw |
| 2375 | wRGGT6+iPmTYM6N5/IDyAb5B9Ke38O6lLEsUwA== |
| 2376 | -----END CERTIFICATE----- |
| 2377 | )"); |
| 2378 | ASSERT_TRUE(leaf_public); |
| 2379 | bssl::UniquePtr<X509> leaf_secret = CertFromPEM(R"( |
| 2380 | -----BEGIN CERTIFICATE----- |
| 2381 | MIIBaTCCAQ6gAwIBAgIBAzAKBggqhkjOPQQDAjASMRAwDgYDVQQDEwdUZXN0IENB |
| 2382 | MCAXDTAwMDEwMTAwMDAwMFoYDzIwOTkwMTAxMDAwMDAwWjAZMRcwFQYDVQQDEw5z |
| 2383 | ZWNyZXQuZXhhbXBsZTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABE2uSHFpsAsO |
| 2384 | Umse2+S4PC8JZQX+hUKGvo8MhMeLWUOUnO9VPhpcEfzuwfafZRXnOnHXc0eSrVsg |
| 2385 | Lx5FPx44h7SjTDBKMAwGA1UdEwEB/wQCMAAwHwYDVR0jBBgwFoAU8On7ZHsQCOqO |
| 2386 | K+RZ7SSHyxlOReMwGQYDVR0RBBIwEIIOc2VjcmV0LmV4YW1wbGUwCgYIKoZIzj0E |
| 2387 | AwIDSQAwRgIhAPQdIz1xCFkc9WuSkxOxJDpywZiEp9SnKcxJ9nwrlRp3AiEA+O3+ |
| 2388 | XRqE7XFhHL+7TNC2a9OOAjQsEF137YPWo+rhgko= |
| 2389 | -----END CERTIFICATE----- |
| 2390 | )"); |
| 2391 | ASSERT_TRUE(leaf_secret); |
| 2392 |
| 2393 | // Use different config IDs so that fuzzer mode, which breaks trial |
| 2394 | // decryption, will observe the key mismatch. |
| 2395 | bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(/*config_id=*/1); |
| 2396 | ASSERT_TRUE(keys); |
| 2397 | bssl::UniquePtr<SSL_ECH_KEYS> wrong_keys = MakeTestECHKeys(/*config_id=*/2); |
| 2398 | ASSERT_TRUE(wrong_keys); |
| 2399 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 2400 | CreateContextWithTestCertificate(TLS_method()); |
| 2401 | ASSERT_TRUE(server_ctx); |
| 2402 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2403 | ASSERT_TRUE(client_ctx); |
| 2404 |
| 2405 | // Configure the client to verify certificates and expect the secret name. |
| 2406 | // This is the name the client is trying to connect to. If ECH is rejected, |
| 2407 | // BoringSSL will internally override this setting with the public name. |
| 2408 | bssl::UniquePtr<X509_STORE> store(X509_STORE_new()); |
| 2409 | ASSERT_TRUE(store); |
| 2410 | ASSERT_TRUE(X509_STORE_add_cert(store.get(), root.get())); |
| 2411 | SSL_CTX_set_cert_store(client_ctx.get(), store.release()); |
| 2412 | SSL_CTX_set_verify(client_ctx.get(), SSL_VERIFY_PEER, nullptr); |
| 2413 | X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(client_ctx.get()), |
| 2414 | X509_V_FLAG_NO_CHECK_TIME); |
| 2415 | static const char kSecretName[] = "secret.example"; |
| 2416 | ASSERT_TRUE(X509_VERIFY_PARAM_set1_host(SSL_CTX_get0_param(client_ctx.get()), |
| 2417 | kSecretName, strlen(kSecretName))); |
| 2418 |
| 2419 | // For simplicity, we only run through a pair of representative scenarios here |
| 2420 | // and rely on runner.go to verify that |SSL_get0_ech_name_override| behaves |
| 2421 | // correctly. |
| 2422 | for (bool accept_ech : {false, true}) { |
| 2423 | SCOPED_TRACE(accept_ech); |
| 2424 | for (bool use_leaf_secret : {false, true}) { |
| 2425 | SCOPED_TRACE(use_leaf_secret); |
| 2426 |
| 2427 | // The server will reject ECH when configured with the wrong keys. |
| 2428 | ASSERT_TRUE(SSL_CTX_set1_ech_keys( |
| 2429 | server_ctx.get(), accept_ech ? keys.get() : wrong_keys.get())); |
| 2430 |
| 2431 | bssl::UniquePtr<SSL> client, server; |
| 2432 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 2433 | server_ctx.get())); |
| 2434 | ASSERT_TRUE(InstallECHConfigList(client.get(), keys.get())); |
| 2435 |
| 2436 | // Configure the server with the selected certificate. |
| 2437 | ASSERT_TRUE(SSL_use_certificate(server.get(), use_leaf_secret |
| 2438 | ? leaf_secret.get() |
| 2439 | : leaf_public.get())); |
| 2440 | ASSERT_TRUE(SSL_use_PrivateKey(server.get(), leaf_key.get())); |
| 2441 |
| 2442 | // The handshake may fail due to name mismatch or ECH reject. We check |
| 2443 | // |SSL_get_verify_result| to confirm the handshake got far enough. |
| 2444 | CompleteHandshakes(client.get(), server.get()); |
| 2445 | EXPECT_EQ(accept_ech == use_leaf_secret ? X509_V_OK |
| 2446 | : X509_V_ERR_HOSTNAME_MISMATCH, |
| 2447 | SSL_get_verify_result(client.get())); |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 |
| 2452 | #if defined(OPENSSL_THREADS) |
| 2453 | // Test that the server ECH config can be swapped out while the |SSL_CTX| is |
| 2454 | // in use on other threads. This test is intended to be run with TSan. |
| 2455 | TEST(SSLTest, ECHThreads) { |
| 2456 | // Generate a pair of ECHConfigs. |
| 2457 | bssl::ScopedEVP_HPKE_KEY key1; |
| 2458 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key1.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 2459 | uint8_t *ech_config1; |
| 2460 | size_t ech_config1_len; |
| 2461 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config1, &ech_config1_len, |
| 2462 | /*config_id=*/1, key1.get(), |
| 2463 | "public.example", 16)); |
| 2464 | bssl::UniquePtr<uint8_t> free_ech_config1(ech_config1); |
| 2465 | bssl::ScopedEVP_HPKE_KEY key2; |
| 2466 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key2.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 2467 | uint8_t *ech_config2; |
| 2468 | size_t ech_config2_len; |
| 2469 | ASSERT_TRUE(SSL_marshal_ech_config(&ech_config2, &ech_config2_len, |
| 2470 | /*config_id=*/2, key2.get(), |
| 2471 | "public.example", 16)); |
| 2472 | bssl::UniquePtr<uint8_t> free_ech_config2(ech_config2); |
| 2473 |
| 2474 | // |keys1| contains the first config. |keys12| contains both. |
| 2475 | bssl::UniquePtr<SSL_ECH_KEYS> keys1(SSL_ECH_KEYS_new()); |
| 2476 | ASSERT_TRUE(keys1); |
| 2477 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys1.get(), /*is_retry_config=*/1, ech_config1, |
| 2478 | ech_config1_len, key1.get())); |
| 2479 | bssl::UniquePtr<SSL_ECH_KEYS> keys12(SSL_ECH_KEYS_new()); |
| 2480 | ASSERT_TRUE(keys12); |
| 2481 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys12.get(), /*is_retry_config=*/1, ech_config2, |
| 2482 | ech_config2_len, key2.get())); |
| 2483 | ASSERT_TRUE(SSL_ECH_KEYS_add(keys12.get(), /*is_retry_config=*/0, ech_config1, |
| 2484 | ech_config1_len, key1.get())); |
| 2485 |
| 2486 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 2487 | CreateContextWithTestCertificate(TLS_method()); |
| 2488 | ASSERT_TRUE(server_ctx); |
| 2489 | ASSERT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys1.get())); |
| 2490 |
| 2491 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2492 | ASSERT_TRUE(client_ctx); |
| 2493 | bssl::UniquePtr<SSL> client, server; |
| 2494 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 2495 | server_ctx.get())); |
| 2496 | ASSERT_TRUE(InstallECHConfigList(client.get(), keys1.get())); |
| 2497 |
| 2498 | // In parallel, complete the connection and reconfigure the ECHConfig. Note |
| 2499 | // |keys12| supports all the keys in |keys1|, so the handshake should complete |
| 2500 | // the same whichever the server uses. |
| 2501 | std::vector<std::thread> threads; |
| 2502 | threads.emplace_back([&] { |
| 2503 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 2504 | EXPECT_TRUE(SSL_ech_accepted(client.get())); |
| 2505 | EXPECT_TRUE(SSL_ech_accepted(server.get())); |
| 2506 | }); |
| 2507 | threads.emplace_back([&] { |
| 2508 | EXPECT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys12.get())); |
| 2509 | }); |
| 2510 | for (auto &thread : threads) { |
| 2511 | thread.join(); |
| 2512 | } |
| 2513 | } |
| 2514 | #endif // OPENSSL_THREADS |
| 2515 |
| 2516 | TEST(SSLTest, TLS13ExporterAvailability) { |
| 2517 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 2518 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 2519 | CreateContextWithTestCertificate(TLS_method()); |
| 2520 | ASSERT_TRUE(client_ctx); |
| 2521 | ASSERT_TRUE(server_ctx); |
| 2522 | // Configure only TLS 1.3. |
| 2523 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 2524 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 2525 |
| 2526 | bssl::UniquePtr<SSL> client, server; |
| 2527 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 2528 | server_ctx.get())); |
| 2529 |
| 2530 | std::vector<uint8_t> buffer(32); |
| 2531 | const char *label = "EXPORTER-test-label"; |
| 2532 |
| 2533 | // The exporters are not available before the handshake starts. |
| 2534 | EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 2535 | buffer.size(), label, strlen(label), |
| 2536 | nullptr, 0, 0)); |
| 2537 | EXPECT_FALSE(SSL_export_keying_material(server.get(), buffer.data(), |
| 2538 | buffer.size(), label, strlen(label), |
| 2539 | nullptr, 0, 0)); |
| 2540 |
| 2541 | // Send the client's first flight of handshake messages. |
| 2542 | int client_ret = SSL_do_handshake(client.get()); |
| 2543 | EXPECT_EQ(SSL_get_error(client.get(), client_ret), SSL_ERROR_WANT_READ); |
| 2544 |
| 2545 | // The handshake isn't far enough for the exporters to work. |
| 2546 | EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 2547 | buffer.size(), label, strlen(label), |
| 2548 | nullptr, 0, 0)); |
| 2549 | EXPECT_FALSE(SSL_export_keying_material(server.get(), buffer.data(), |
| 2550 | buffer.size(), label, strlen(label), |
| 2551 | nullptr, 0, 0)); |
| 2552 |
| 2553 | // Send all the server's handshake messages. |
| 2554 | int server_ret = SSL_do_handshake(server.get()); |
| 2555 | EXPECT_EQ(SSL_get_error(server.get(), server_ret), SSL_ERROR_WANT_READ); |
| 2556 |
| 2557 | // At this point in the handshake, the server should have the exporter key |
| 2558 | // derived since it's sent its Finished message. The client hasn't yet |
| 2559 | // processed the server's handshake messages, so the exporter shouldn't be |
| 2560 | // available to the client. |
| 2561 | EXPECT_FALSE(SSL_export_keying_material(client.get(), buffer.data(), |
| 2562 | buffer.size(), label, strlen(label), |
| 2563 | nullptr, 0, 0)); |
| 2564 | EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 2565 | buffer.size(), label, strlen(label), |
| 2566 | nullptr, 0, 0)); |
| 2567 |
| 2568 | // Finish the handshake on the client. |
| 2569 | EXPECT_EQ(SSL_do_handshake(client.get()), 1); |
| 2570 |
| 2571 | // The exporter should be available on both endpoints. |
| 2572 | EXPECT_TRUE(SSL_export_keying_material(client.get(), buffer.data(), |
| 2573 | buffer.size(), label, strlen(label), |
| 2574 | nullptr, 0, 0)); |
| 2575 | EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 2576 | buffer.size(), label, strlen(label), |
| 2577 | nullptr, 0, 0)); |
| 2578 |
| 2579 | // Finish the handshake on the server. |
| 2580 | EXPECT_EQ(SSL_do_handshake(server.get()), 1); |
| 2581 |
| 2582 | // The exporter should still be available on both endpoints. |
| 2583 | EXPECT_TRUE(SSL_export_keying_material(client.get(), buffer.data(), |
| 2584 | buffer.size(), label, strlen(label), |
| 2585 | nullptr, 0, 0)); |
| 2586 | EXPECT_TRUE(SSL_export_keying_material(server.get(), buffer.data(), |
| 2587 | buffer.size(), label, strlen(label), |
| 2588 | nullptr, 0, 0)); |
| 2589 | } |
| 2590 |
| 2591 | static void AppendSession(SSL_SESSION *session, void *arg) { |
| 2592 | std::vector<SSL_SESSION *> *out = |
| 2593 | reinterpret_cast<std::vector<SSL_SESSION *> *>(arg); |
| 2594 | out->push_back(session); |
| 2595 | } |
| 2596 |
| 2597 | // CacheEquals returns true if |ctx|'s session cache consists of |expected|, in |
| 2598 | // order. |
| 2599 | static bool CacheEquals(SSL_CTX *ctx, |
| 2600 | const std::vector<SSL_SESSION *> &expected) { |
| 2601 | // Check the linked list. |
| 2602 | SSL_SESSION *ptr = ctx->session_cache_head; |
| 2603 | for (SSL_SESSION *session : expected) { |
| 2604 | if (ptr != session) { |
| 2605 | return false; |
| 2606 | } |
| 2607 | // TODO(davidben): This is an absurd way to denote the end of the list. |
| 2608 | if (ptr->next == |
| 2609 | reinterpret_cast<SSL_SESSION *>(&ctx->session_cache_tail)) { |
| 2610 | ptr = nullptr; |
| 2611 | } else { |
| 2612 | ptr = ptr->next; |
| 2613 | } |
| 2614 | } |
| 2615 | if (ptr != nullptr) { |
| 2616 | return false; |
| 2617 | } |
| 2618 |
| 2619 | // Check the hash table. |
| 2620 | std::vector<SSL_SESSION *> actual, expected_copy; |
| 2621 | lh_SSL_SESSION_doall_arg(ctx->sessions, AppendSession, &actual); |
| 2622 | expected_copy = expected; |
| 2623 |
| 2624 | std::sort(actual.begin(), actual.end()); |
| 2625 | std::sort(expected_copy.begin(), expected_copy.end()); |
| 2626 |
| 2627 | return actual == expected_copy; |
| 2628 | } |
| 2629 |
| 2630 | static bssl::UniquePtr<SSL_SESSION> CreateTestSession(uint32_t number) { |
| 2631 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 2632 | if (!ssl_ctx) { |
| 2633 | return nullptr; |
| 2634 | } |
| 2635 | bssl::UniquePtr<SSL_SESSION> ret(SSL_SESSION_new(ssl_ctx.get())); |
| 2636 | if (!ret) { |
| 2637 | return nullptr; |
| 2638 | } |
| 2639 |
| 2640 | uint8_t id[SSL3_SSL_SESSION_ID_LENGTH] = {0}; |
| 2641 | OPENSSL_memcpy(id, &number, sizeof(number)); |
| 2642 | if (!SSL_SESSION_set1_id(ret.get(), id, sizeof(id))) { |
| 2643 | return nullptr; |
| 2644 | } |
| 2645 | return ret; |
| 2646 | } |
| 2647 |
| 2648 | // Test that the internal session cache behaves as expected. |
| 2649 | TEST(SSLTest, InternalSessionCache) { |
| 2650 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 2651 | ASSERT_TRUE(ctx); |
| 2652 |
| 2653 | // Prepare 10 test sessions. |
| 2654 | std::vector<bssl::UniquePtr<SSL_SESSION>> sessions; |
| 2655 | for (int i = 0; i < 10; i++) { |
| 2656 | bssl::UniquePtr<SSL_SESSION> session = CreateTestSession(i); |
| 2657 | ASSERT_TRUE(session); |
| 2658 | sessions.push_back(std::move(session)); |
| 2659 | } |
| 2660 |
| 2661 | SSL_CTX_sess_set_cache_size(ctx.get(), 5); |
| 2662 |
| 2663 | // Insert all the test sessions. |
| 2664 | for (const auto &session : sessions) { |
| 2665 | ASSERT_TRUE(SSL_CTX_add_session(ctx.get(), session.get())); |
| 2666 | } |
| 2667 |
| 2668 | // Only the last five should be in the list. |
| 2669 | ASSERT_TRUE(CacheEquals( |
| 2670 | ctx.get(), {sessions[9].get(), sessions[8].get(), sessions[7].get(), |
| 2671 | sessions[6].get(), sessions[5].get()})); |
| 2672 |
| 2673 | // Inserting an element already in the cache should fail and leave the cache |
| 2674 | // unchanged. |
| 2675 | ASSERT_FALSE(SSL_CTX_add_session(ctx.get(), sessions[7].get())); |
| 2676 | ASSERT_TRUE(CacheEquals( |
| 2677 | ctx.get(), {sessions[9].get(), sessions[8].get(), sessions[7].get(), |
| 2678 | sessions[6].get(), sessions[5].get()})); |
| 2679 |
| 2680 | // Although collisions should be impossible (256-bit session IDs), the cache |
| 2681 | // must handle them gracefully. |
| 2682 | bssl::UniquePtr<SSL_SESSION> collision(CreateTestSession(7)); |
| 2683 | ASSERT_TRUE(collision); |
| 2684 | ASSERT_TRUE(SSL_CTX_add_session(ctx.get(), collision.get())); |
| 2685 | ASSERT_TRUE(CacheEquals( |
| 2686 | ctx.get(), {collision.get(), sessions[9].get(), sessions[8].get(), |
| 2687 | sessions[6].get(), sessions[5].get()})); |
| 2688 |
| 2689 | // Removing sessions behaves correctly. |
| 2690 | ASSERT_TRUE(SSL_CTX_remove_session(ctx.get(), sessions[6].get())); |
| 2691 | ASSERT_TRUE(CacheEquals(ctx.get(), {collision.get(), sessions[9].get(), |
| 2692 | sessions[8].get(), sessions[5].get()})); |
| 2693 |
| 2694 | // Removing sessions requires an exact match. |
| 2695 | ASSERT_FALSE(SSL_CTX_remove_session(ctx.get(), sessions[0].get())); |
| 2696 | ASSERT_FALSE(SSL_CTX_remove_session(ctx.get(), sessions[7].get())); |
| 2697 |
| 2698 | // The cache remains unchanged. |
| 2699 | ASSERT_TRUE(CacheEquals(ctx.get(), {collision.get(), sessions[9].get(), |
| 2700 | sessions[8].get(), sessions[5].get()})); |
| 2701 | } |
| 2702 |
| 2703 | static uint16_t EpochFromSequence(uint64_t seq) { |
| 2704 | return static_cast<uint16_t>(seq >> 48); |
| 2705 | } |
| 2706 |
| 2707 | static const uint8_t kTestName[] = { |
| 2708 | 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 2709 | 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 2710 | 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 2711 | 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 2712 | 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 2713 | 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, |
| 2714 | }; |
| 2715 |
| 2716 | // SSLVersionTest executes its test cases under all available protocol versions. |
| 2717 | // Test cases call |Connect| to create a connection using context objects with |
| 2718 | // the protocol version fixed to the current version under test. |
| 2719 | class SSLVersionTest : public ::testing::TestWithParam<VersionParam> { |
| 2720 | protected: |
| 2721 | SSLVersionTest() : cert_(GetTestCertificate()), key_(GetTestKey()) {} |
| 2722 |
| 2723 | void SetUp() { ResetContexts(); } |
| 2724 |
| 2725 | bssl::UniquePtr<SSL_CTX> CreateContext() const { |
| 2726 | const SSL_METHOD *method = is_dtls() ? DTLS_method() : TLS_method(); |
| 2727 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(method)); |
| 2728 | if (!ctx || !SSL_CTX_set_min_proto_version(ctx.get(), version()) || |
| 2729 | !SSL_CTX_set_max_proto_version(ctx.get(), version())) { |
| 2730 | return nullptr; |
| 2731 | } |
| 2732 | return ctx; |
| 2733 | } |
| 2734 |
| 2735 | void ResetContexts() { |
| 2736 | ASSERT_TRUE(cert_); |
| 2737 | ASSERT_TRUE(key_); |
| 2738 | client_ctx_ = CreateContext(); |
| 2739 | ASSERT_TRUE(client_ctx_); |
| 2740 | server_ctx_ = CreateContext(); |
| 2741 | ASSERT_TRUE(server_ctx_); |
| 2742 | // Set up a server cert. Client certs can be set up explicitly. |
| 2743 | ASSERT_TRUE(UseCertAndKey(server_ctx_.get())); |
| 2744 | } |
| 2745 |
| 2746 | bool UseCertAndKey(SSL_CTX *ctx) const { |
| 2747 | return SSL_CTX_use_certificate(ctx, cert_.get()) && |
| 2748 | SSL_CTX_use_PrivateKey(ctx, key_.get()); |
| 2749 | } |
| 2750 |
| 2751 | bool Connect(const ClientConfig &config = ClientConfig()) { |
| 2752 | return ConnectClientAndServer(&client_, &server_, client_ctx_.get(), |
| 2753 | server_ctx_.get(), config, |
| 2754 | shed_handshake_config_); |
| 2755 | } |
| 2756 |
| 2757 | uint16_t version() const { return GetParam().version; } |
| 2758 |
| 2759 | bool is_tls13() const { |
| 2760 | return version() == TLS1_3_VERSION || version() == DTLS1_3_VERSION; |
| 2761 | } |
| 2762 |
| 2763 | bool is_dtls() const { |
| 2764 | return GetParam().ssl_method == VersionParam::is_dtls; |
| 2765 | } |
| 2766 |
| 2767 | bool shed_handshake_config_ = true; |
| 2768 | bssl::UniquePtr<SSL> client_, server_; |
| 2769 | bssl::UniquePtr<SSL_CTX> server_ctx_, client_ctx_; |
| 2770 | bssl::UniquePtr<X509> cert_; |
| 2771 | bssl::UniquePtr<EVP_PKEY> key_; |
| 2772 | }; |
| 2773 |
| 2774 | INSTANTIATE_TEST_SUITE_P(WithVersion, SSLVersionTest, |
| 2775 | testing::ValuesIn(kAllVersions), |
| 2776 | [](const testing::TestParamInfo<VersionParam> &i) { |
| 2777 | return i.param.name; |
| 2778 | }); |
| 2779 |
| 2780 | TEST_P(SSLVersionTest, SequenceNumber) { |
| 2781 | ASSERT_TRUE(Connect()); |
| 2782 |
| 2783 | // Drain any post-handshake messages to ensure there are no unread records |
| 2784 | // on either end. |
| 2785 | ASSERT_TRUE(FlushNewSessionTickets(client_.get(), server_.get())); |
| 2786 |
| 2787 | uint64_t client_read_seq = SSL_get_read_sequence(client_.get()); |
| 2788 | uint64_t client_write_seq = SSL_get_write_sequence(client_.get()); |
| 2789 | uint64_t server_read_seq = SSL_get_read_sequence(server_.get()); |
| 2790 | uint64_t server_write_seq = SSL_get_write_sequence(server_.get()); |
| 2791 |
| 2792 | if (is_dtls()) { |
| 2793 | if (version() == DTLS1_3_VERSION) { |
| 2794 | // Both client and server must be at epoch 3 (application data). |
| 2795 | EXPECT_EQ(EpochFromSequence(client_write_seq), 3); |
| 2796 | EXPECT_EQ(EpochFromSequence(server_write_seq), 3); |
| 2797 |
| 2798 | // TODO(crbug.com/42290608): Ideally we would check the read sequence |
| 2799 | // numbers and compare them against each other, but |
| 2800 | // |SSL_get_read_sequence| is ill-defined right after DTLS 1.3's key |
| 2801 | // change. See that function for details. |
| 2802 | } else { |
| 2803 | // Both client and server must be at epoch 1. |
| 2804 | EXPECT_EQ(EpochFromSequence(client_read_seq), 1); |
| 2805 | EXPECT_EQ(EpochFromSequence(client_write_seq), 1); |
| 2806 | EXPECT_EQ(EpochFromSequence(server_read_seq), 1); |
| 2807 | EXPECT_EQ(EpochFromSequence(server_write_seq), 1); |
| 2808 |
| 2809 | // The next record to be written should exceed the largest received. |
| 2810 | EXPECT_GT(client_write_seq, server_read_seq); |
| 2811 | EXPECT_GT(server_write_seq, client_read_seq); |
| 2812 | } |
| 2813 | } else { |
| 2814 | // The next record to be written should equal the next to be received. |
| 2815 | EXPECT_EQ(client_write_seq, server_read_seq); |
| 2816 | EXPECT_EQ(server_write_seq, client_read_seq); |
| 2817 | } |
| 2818 |
| 2819 | // Send a record from client to server. |
| 2820 | uint8_t byte = 0; |
| 2821 | EXPECT_EQ(SSL_write(client_.get(), &byte, 1), 1); |
| 2822 | EXPECT_EQ(SSL_read(server_.get(), &byte, 1), 1); |
| 2823 |
| 2824 | if (version() == DTLS1_3_VERSION) { |
| 2825 | // TODO(crbug.com/42290608): Write an appropriate test for incrementing both |
| 2826 | // sequence number and epoch in the following test. The server read seq was |
| 2827 | // in epoch 2, but after the write it's in epoch 3, so adding 1 doesn't work |
| 2828 | // any more. |
| 2829 | return; |
| 2830 | } |
| 2831 |
| 2832 | // The client write and server read sequence numbers should have |
| 2833 | // incremented. |
| 2834 | EXPECT_EQ(client_write_seq + 1, SSL_get_write_sequence(client_.get())); |
| 2835 | EXPECT_EQ(server_read_seq + 1, SSL_get_read_sequence(server_.get())); |
| 2836 | } |
| 2837 |
| 2838 | TEST_P(SSLVersionTest, OneSidedShutdown) { |
| 2839 | // SSL_shutdown is a no-op in DTLS. |
| 2840 | if (is_dtls()) { |
| 2841 | return; |
| 2842 | } |
| 2843 | ASSERT_TRUE(Connect()); |
| 2844 |
| 2845 | // Shut down half the connection. |SSL_shutdown| will return 0 to signal only |
| 2846 | // one side has shut down. |
| 2847 | ASSERT_EQ(SSL_shutdown(client_.get()), 0); |
| 2848 |
| 2849 | // Reading from the server should consume the EOF. |
| 2850 | uint8_t byte; |
| 2851 | ASSERT_EQ(SSL_read(server_.get(), &byte, 1), 0); |
| 2852 | ASSERT_EQ(SSL_get_error(server_.get(), 0), SSL_ERROR_ZERO_RETURN); |
| 2853 |
| 2854 | // However, the server may continue to write data and then shut down the |
| 2855 | // connection. |
| 2856 | byte = 42; |
| 2857 | ASSERT_EQ(SSL_write(server_.get(), &byte, 1), 1); |
| 2858 | ASSERT_EQ(SSL_read(client_.get(), &byte, 1), 1); |
| 2859 | ASSERT_EQ(byte, 42); |
| 2860 |
| 2861 | // The server may then shutdown the connection. |
| 2862 | EXPECT_EQ(SSL_shutdown(server_.get()), 1); |
| 2863 | EXPECT_EQ(SSL_shutdown(client_.get()), 1); |
| 2864 | } |
| 2865 |
| 2866 | // Test that, after calling |SSL_shutdown|, |SSL_write| fails. |
| 2867 | TEST_P(SSLVersionTest, WriteAfterShutdown) { |
| 2868 | ASSERT_TRUE(Connect()); |
| 2869 |
| 2870 | for (SSL *ssl : {client_.get(), server_.get()}) { |
| 2871 | SCOPED_TRACE(SSL_is_server(ssl) ? "server" : "client"); |
| 2872 |
| 2873 | bssl::UniquePtr<BIO> mem(BIO_new(BIO_s_mem())); |
| 2874 | ASSERT_TRUE(mem); |
| 2875 | SSL_set0_wbio(ssl, bssl::UpRef(mem).release()); |
| 2876 |
| 2877 | // Shut down half the connection. |SSL_shutdown| will return 0 to signal |
| 2878 | // only one side has shut down. |
| 2879 | ASSERT_EQ(SSL_shutdown(ssl), 0); |
| 2880 |
| 2881 | // |ssl| should have written an alert to the transport. |
| 2882 | const uint8_t *unused; |
| 2883 | size_t len; |
| 2884 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 2885 | EXPECT_NE(0u, len); |
| 2886 | EXPECT_TRUE(BIO_reset(mem.get())); |
| 2887 |
| 2888 | // Writing should fail. |
| 2889 | EXPECT_EQ(-1, SSL_write(ssl, "a", 1)); |
| 2890 |
| 2891 | // Nothing should be written to the transport. |
| 2892 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 2893 | EXPECT_EQ(0u, len); |
| 2894 | } |
| 2895 | } |
| 2896 |
| 2897 | // Test that, after sending a fatal alert in a failed |SSL_read|, |SSL_write| |
| 2898 | // fails. |
| 2899 | TEST_P(SSLVersionTest, WriteAfterReadSentFatalAlert) { |
| 2900 | // Decryption failures are not fatal in DTLS. |
| 2901 | if (is_dtls()) { |
| 2902 | return; |
| 2903 | } |
| 2904 |
| 2905 | ASSERT_TRUE(Connect()); |
| 2906 |
| 2907 | // Save the write |BIO|s as the test will overwrite them. |
| 2908 | bssl::UniquePtr<BIO> client_wbio = bssl::UpRef(SSL_get_wbio(client_.get())); |
| 2909 | bssl::UniquePtr<BIO> server_wbio = bssl::UpRef(SSL_get_wbio(server_.get())); |
| 2910 |
| 2911 | for (bool test_server : {false, true}) { |
| 2912 | SCOPED_TRACE(test_server ? "server" : "client"); |
| 2913 | SSL *ssl = test_server ? server_.get() : client_.get(); |
| 2914 | BIO *other_wbio = test_server ? client_wbio.get() : server_wbio.get(); |
| 2915 |
| 2916 | bssl::UniquePtr<BIO> mem(BIO_new(BIO_s_mem())); |
| 2917 | ASSERT_TRUE(mem); |
| 2918 | SSL_set0_wbio(ssl, bssl::UpRef(mem).release()); |
| 2919 |
| 2920 | // Read an invalid record from the peer. |
| 2921 | static const uint8_t kInvalidRecord[] = "invalid record"; |
| 2922 | EXPECT_EQ(int{sizeof(kInvalidRecord)}, |
| 2923 | BIO_write(other_wbio, kInvalidRecord, sizeof(kInvalidRecord))); |
| 2924 | char buf[256]; |
| 2925 | EXPECT_EQ(-1, SSL_read(ssl, buf, sizeof(buf))); |
| 2926 |
| 2927 | // |ssl| should have written an alert to the transport. |
| 2928 | const uint8_t *unused; |
| 2929 | size_t len; |
| 2930 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 2931 | EXPECT_NE(0u, len); |
| 2932 | EXPECT_TRUE(BIO_reset(mem.get())); |
| 2933 |
| 2934 | // Writing should fail. |
| 2935 | EXPECT_EQ(-1, SSL_write(ssl, "a", 1)); |
| 2936 |
| 2937 | // Nothing should be written to the transport. |
| 2938 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 2939 | EXPECT_EQ(0u, len); |
| 2940 | } |
| 2941 | } |
| 2942 |
| 2943 | // Test that, after sending a fatal alert from the handshake, |SSL_write| fails. |
| 2944 | TEST_P(SSLVersionTest, WriteAfterHandshakeSentFatalAlert) { |
| 2945 | for (bool test_server : {false, true}) { |
| 2946 | SCOPED_TRACE(test_server ? "server" : "client"); |
| 2947 |
| 2948 | bssl::UniquePtr<SSL> ssl( |
| 2949 | SSL_new(test_server ? server_ctx_.get() : client_ctx_.get())); |
| 2950 | ASSERT_TRUE(ssl); |
| 2951 | if (test_server) { |
| 2952 | SSL_set_accept_state(ssl.get()); |
| 2953 | } else { |
| 2954 | SSL_set_connect_state(ssl.get()); |
| 2955 | } |
| 2956 |
| 2957 | std::vector<uint8_t> invalid; |
| 2958 | if (is_dtls()) { |
| 2959 | // In DTLS, invalid records are discarded. To cause the handshake to fail, |
| 2960 | // use a valid handshake record with invalid contents. |
| 2961 | invalid.push_back(SSL3_RT_HANDSHAKE); |
| 2962 | invalid.push_back(DTLS1_VERSION >> 8); |
| 2963 | invalid.push_back(DTLS1_VERSION & 0xff); |
| 2964 | // epoch and sequence_number |
| 2965 | for (int i = 0; i < 8; i++) { |
| 2966 | invalid.push_back(0); |
| 2967 | } |
| 2968 | // A one-byte fragment is invalid. |
| 2969 | invalid.push_back(0); |
| 2970 | invalid.push_back(1); |
| 2971 | // Arbitrary contents. |
| 2972 | invalid.push_back(0); |
| 2973 | } else { |
| 2974 | invalid = {'i', 'n', 'v', 'a', 'l', 'i', 'd'}; |
| 2975 | } |
| 2976 | bssl::UniquePtr<BIO> rbio(BIO_new_mem_buf(invalid.data(), invalid.size())); |
| 2977 | ASSERT_TRUE(rbio); |
| 2978 | SSL_set0_rbio(ssl.get(), rbio.release()); |
| 2979 |
| 2980 | bssl::UniquePtr<BIO> mem(BIO_new(BIO_s_mem())); |
| 2981 | ASSERT_TRUE(mem); |
| 2982 | SSL_set0_wbio(ssl.get(), bssl::UpRef(mem).release()); |
| 2983 |
| 2984 | // The handshake should fail. |
| 2985 | EXPECT_EQ(-1, SSL_do_handshake(ssl.get())); |
| 2986 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(ssl.get(), -1)); |
| 2987 | uint32_t err = ERR_get_error(); |
| 2988 |
| 2989 | // |ssl| should have written an alert (and, in the client's case, a |
| 2990 | // ClientHello) to the transport. |
| 2991 | const uint8_t *unused; |
| 2992 | size_t len; |
| 2993 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 2994 | EXPECT_NE(0u, len); |
| 2995 | EXPECT_TRUE(BIO_reset(mem.get())); |
| 2996 |
| 2997 | // Writing should fail, with the same error as the handshake. |
| 2998 | EXPECT_EQ(-1, SSL_write(ssl.get(), "a", 1)); |
| 2999 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(ssl.get(), -1)); |
| 3000 | EXPECT_EQ(err, ERR_get_error()); |
| 3001 |
| 3002 | // Nothing should be written to the transport. |
| 3003 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 3004 | EXPECT_EQ(0u, len); |
| 3005 | } |
| 3006 | } |
| 3007 |
| 3008 | // Test that, after seeing TLS 1.2 in response to early data, |SSL_write| |
| 3009 | // continues to report |SSL_R_WRONG_VERSION_ON_EARLY_DATA|. See |
| 3010 | // https://crbug.com/1078515. |
| 3011 | TEST(SSLTest, WriteAfterWrongVersionOnEarlyData) { |
| 3012 | // Set up some 0-RTT-enabled contexts. |
| 3013 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 3014 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 3015 | CreateContextWithTestCertificate(TLS_method()); |
| 3016 | ASSERT_TRUE(client_ctx); |
| 3017 | ASSERT_TRUE(server_ctx); |
| 3018 | SSL_CTX_set_early_data_enabled(client_ctx.get(), 1); |
| 3019 | SSL_CTX_set_early_data_enabled(server_ctx.get(), 1); |
| 3020 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 3021 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 3022 |
| 3023 | // Get an early-data-capable session. |
| 3024 | bssl::UniquePtr<SSL_SESSION> session = |
| 3025 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 3026 | ASSERT_TRUE(session); |
| 3027 | EXPECT_TRUE(SSL_SESSION_early_data_capable(session.get())); |
| 3028 |
| 3029 | // Offer the session to the server, but now the server speaks TLS 1.2. |
| 3030 | bssl::UniquePtr<SSL> client, server; |
| 3031 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 3032 | server_ctx.get())); |
| 3033 | SSL_set_session(client.get(), session.get()); |
| 3034 | EXPECT_TRUE(SSL_set_max_proto_version(server.get(), TLS1_2_VERSION)); |
| 3035 |
| 3036 | // The client handshake initially succeeds in the early data state. |
| 3037 | EXPECT_EQ(1, SSL_do_handshake(client.get())); |
| 3038 | EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 3039 |
| 3040 | // The server processes the ClientHello and negotiates TLS 1.2. |
| 3041 | EXPECT_EQ(-1, SSL_do_handshake(server.get())); |
| 3042 | EXPECT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server.get(), -1)); |
| 3043 | EXPECT_EQ(TLS1_2_VERSION, SSL_version(server.get())); |
| 3044 |
| 3045 | // Capture the client's output. |
| 3046 | bssl::UniquePtr<BIO> mem(BIO_new(BIO_s_mem())); |
| 3047 | ASSERT_TRUE(mem); |
| 3048 | SSL_set0_wbio(client.get(), bssl::UpRef(mem).release()); |
| 3049 |
| 3050 | // The client processes the ServerHello and fails. |
| 3051 | EXPECT_EQ(-1, SSL_do_handshake(client.get())); |
| 3052 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); |
| 3053 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 3054 | SSL_R_WRONG_VERSION_ON_EARLY_DATA)); |
| 3055 |
| 3056 | // The client should have written an alert to the transport. |
| 3057 | const uint8_t *unused; |
| 3058 | size_t len; |
| 3059 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 3060 | EXPECT_NE(0u, len); |
| 3061 | EXPECT_TRUE(BIO_reset(mem.get())); |
| 3062 |
| 3063 | // Writing should fail, with the same error as the handshake. |
| 3064 | EXPECT_EQ(-1, SSL_write(client.get(), "a", 1)); |
| 3065 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); |
| 3066 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 3067 | SSL_R_WRONG_VERSION_ON_EARLY_DATA)); |
| 3068 |
| 3069 | // Nothing should be written to the transport. |
| 3070 | ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); |
| 3071 | EXPECT_EQ(0u, len); |
| 3072 | } |
| 3073 |
| 3074 | TEST(SSLTest, SessionDuplication) { |
| 3075 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 3076 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 3077 | CreateContextWithTestCertificate(TLS_method()); |
| 3078 | ASSERT_TRUE(client_ctx); |
| 3079 | ASSERT_TRUE(server_ctx); |
| 3080 |
| 3081 | bssl::UniquePtr<SSL> client, server; |
| 3082 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 3083 | server_ctx.get())); |
| 3084 |
| 3085 | SSL_SESSION *session0 = SSL_get_session(client.get()); |
| 3086 | bssl::UniquePtr<SSL_SESSION> session1 = |
| 3087 | bssl::SSL_SESSION_dup(session0, SSL_SESSION_DUP_ALL); |
| 3088 | ASSERT_TRUE(session1); |
| 3089 |
| 3090 | session1->not_resumable = false; |
| 3091 |
| 3092 | uint8_t *s0_bytes, *s1_bytes; |
| 3093 | size_t s0_len, s1_len; |
| 3094 |
| 3095 | ASSERT_TRUE(SSL_SESSION_to_bytes(session0, &s0_bytes, &s0_len)); |
| 3096 | bssl::UniquePtr<uint8_t> free_s0(s0_bytes); |
| 3097 |
| 3098 | ASSERT_TRUE(SSL_SESSION_to_bytes(session1.get(), &s1_bytes, &s1_len)); |
| 3099 | bssl::UniquePtr<uint8_t> free_s1(s1_bytes); |
| 3100 |
| 3101 | EXPECT_EQ(Bytes(s0_bytes, s0_len), Bytes(s1_bytes, s1_len)); |
| 3102 | } |
| 3103 |
| 3104 | static void ExpectFDs(const SSL *ssl, int rfd, int wfd) { |
| 3105 | EXPECT_EQ(rfd, SSL_get_fd(ssl)); |
| 3106 | EXPECT_EQ(rfd, SSL_get_rfd(ssl)); |
| 3107 | EXPECT_EQ(wfd, SSL_get_wfd(ssl)); |
| 3108 |
| 3109 | // The wrapper BIOs are always equal when fds are equal, even if set |
| 3110 | // individually. |
| 3111 | if (rfd == wfd) { |
| 3112 | EXPECT_EQ(SSL_get_rbio(ssl), SSL_get_wbio(ssl)); |
| 3113 | } |
| 3114 | } |
| 3115 |
| 3116 | TEST(SSLTest, SetFD) { |
| 3117 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 3118 | ASSERT_TRUE(ctx); |
| 3119 |
| 3120 | // Test setting different read and write FDs. |
| 3121 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 3122 | ASSERT_TRUE(ssl); |
| 3123 | EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 3124 | EXPECT_TRUE(SSL_set_wfd(ssl.get(), 2)); |
| 3125 | ExpectFDs(ssl.get(), 1, 2); |
| 3126 |
| 3127 | // Test setting the same FD. |
| 3128 | ssl.reset(SSL_new(ctx.get())); |
| 3129 | ASSERT_TRUE(ssl); |
| 3130 | EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 3131 | ExpectFDs(ssl.get(), 1, 1); |
| 3132 |
| 3133 | // Test setting the same FD one side at a time. |
| 3134 | ssl.reset(SSL_new(ctx.get())); |
| 3135 | ASSERT_TRUE(ssl); |
| 3136 | EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 3137 | EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 3138 | ExpectFDs(ssl.get(), 1, 1); |
| 3139 |
| 3140 | // Test setting the same FD in the other order. |
| 3141 | ssl.reset(SSL_new(ctx.get())); |
| 3142 | ASSERT_TRUE(ssl); |
| 3143 | EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 3144 | EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 3145 | ExpectFDs(ssl.get(), 1, 1); |
| 3146 |
| 3147 | // Test changing the read FD partway through. |
| 3148 | ssl.reset(SSL_new(ctx.get())); |
| 3149 | ASSERT_TRUE(ssl); |
| 3150 | EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 3151 | EXPECT_TRUE(SSL_set_rfd(ssl.get(), 2)); |
| 3152 | ExpectFDs(ssl.get(), 2, 1); |
| 3153 |
| 3154 | // Test changing the write FD partway through. |
| 3155 | ssl.reset(SSL_new(ctx.get())); |
| 3156 | ASSERT_TRUE(ssl); |
| 3157 | EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 3158 | EXPECT_TRUE(SSL_set_wfd(ssl.get(), 2)); |
| 3159 | ExpectFDs(ssl.get(), 1, 2); |
| 3160 |
| 3161 | // Test a no-op change to the read FD partway through. |
| 3162 | ssl.reset(SSL_new(ctx.get())); |
| 3163 | ASSERT_TRUE(ssl); |
| 3164 | EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 3165 | EXPECT_TRUE(SSL_set_rfd(ssl.get(), 1)); |
| 3166 | ExpectFDs(ssl.get(), 1, 1); |
| 3167 |
| 3168 | // Test a no-op change to the write FD partway through. |
| 3169 | ssl.reset(SSL_new(ctx.get())); |
| 3170 | ASSERT_TRUE(ssl); |
| 3171 | EXPECT_TRUE(SSL_set_fd(ssl.get(), 1)); |
| 3172 | EXPECT_TRUE(SSL_set_wfd(ssl.get(), 1)); |
| 3173 | ExpectFDs(ssl.get(), 1, 1); |
| 3174 |
| 3175 | // ASan builds will implicitly test that the internal |BIO| reference-counting |
| 3176 | // is correct. |
| 3177 | } |
| 3178 |
| 3179 | TEST(SSLTest, SetBIO) { |
| 3180 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 3181 | ASSERT_TRUE(ctx); |
| 3182 |
| 3183 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 3184 | bssl::UniquePtr<BIO> bio1(BIO_new(BIO_s_mem())), bio2(BIO_new(BIO_s_mem())), |
| 3185 | bio3(BIO_new(BIO_s_mem())); |
| 3186 | ASSERT_TRUE(ssl); |
| 3187 | ASSERT_TRUE(bio1); |
| 3188 | ASSERT_TRUE(bio2); |
| 3189 | ASSERT_TRUE(bio3); |
| 3190 |
| 3191 | // SSL_set_bio takes one reference when the parameters are the same. |
| 3192 | BIO_up_ref(bio1.get()); |
| 3193 | SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 3194 |
| 3195 | // Repeating the call does nothing. |
| 3196 | SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 3197 |
| 3198 | // It takes one reference each when the parameters are different. |
| 3199 | BIO_up_ref(bio2.get()); |
| 3200 | BIO_up_ref(bio3.get()); |
| 3201 | SSL_set_bio(ssl.get(), bio2.get(), bio3.get()); |
| 3202 |
| 3203 | // Repeating the call does nothing. |
| 3204 | SSL_set_bio(ssl.get(), bio2.get(), bio3.get()); |
| 3205 |
| 3206 | // It takes one reference when changing only wbio. |
| 3207 | BIO_up_ref(bio1.get()); |
| 3208 | SSL_set_bio(ssl.get(), bio2.get(), bio1.get()); |
| 3209 |
| 3210 | // It takes one reference when changing only rbio and the two are different. |
| 3211 | BIO_up_ref(bio3.get()); |
| 3212 | SSL_set_bio(ssl.get(), bio3.get(), bio1.get()); |
| 3213 |
| 3214 | // If setting wbio to rbio, it takes no additional references. |
| 3215 | SSL_set_bio(ssl.get(), bio3.get(), bio3.get()); |
| 3216 |
| 3217 | // From there, wbio may be switched to something else. |
| 3218 | BIO_up_ref(bio1.get()); |
| 3219 | SSL_set_bio(ssl.get(), bio3.get(), bio1.get()); |
| 3220 |
| 3221 | // If setting rbio to wbio, it takes no additional references. |
| 3222 | SSL_set_bio(ssl.get(), bio1.get(), bio1.get()); |
| 3223 |
| 3224 | // From there, rbio may be switched to something else, but, for historical |
| 3225 | // reasons, it takes a reference to both parameters. |
| 3226 | BIO_up_ref(bio1.get()); |
| 3227 | BIO_up_ref(bio2.get()); |
| 3228 | SSL_set_bio(ssl.get(), bio2.get(), bio1.get()); |
| 3229 |
| 3230 | // ASAN builds will implicitly test that the internal |BIO| reference-counting |
| 3231 | // is correct. |
| 3232 | } |
| 3233 |
| 3234 | static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) { return 1; } |
| 3235 |
| 3236 | TEST_P(SSLVersionTest, GetPeerCertificate) { |
| 3237 | ASSERT_TRUE(UseCertAndKey(client_ctx_.get())); |
| 3238 |
| 3239 | // Configure both client and server to accept any certificate. |
| 3240 | SSL_CTX_set_verify(client_ctx_.get(), |
| 3241 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 3242 | nullptr); |
| 3243 | SSL_CTX_set_cert_verify_callback(client_ctx_.get(), VerifySucceed, NULL); |
| 3244 | SSL_CTX_set_verify(server_ctx_.get(), |
| 3245 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 3246 | nullptr); |
| 3247 | SSL_CTX_set_cert_verify_callback(server_ctx_.get(), VerifySucceed, NULL); |
| 3248 |
| 3249 | ASSERT_TRUE(Connect()); |
| 3250 |
| 3251 | // Client and server should both see the leaf certificate. |
| 3252 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(server_.get())); |
| 3253 | ASSERT_TRUE(peer); |
| 3254 | ASSERT_EQ(X509_cmp(cert_.get(), peer.get()), 0); |
| 3255 |
| 3256 | peer.reset(SSL_get_peer_certificate(client_.get())); |
| 3257 | ASSERT_TRUE(peer); |
| 3258 | ASSERT_EQ(X509_cmp(cert_.get(), peer.get()), 0); |
| 3259 |
| 3260 | // However, for historical reasons, the X509 chain includes the leaf on the |
| 3261 | // client, but does not on the server. |
| 3262 | EXPECT_EQ(sk_X509_num(SSL_get_peer_cert_chain(client_.get())), 1u); |
| 3263 | EXPECT_EQ(sk_CRYPTO_BUFFER_num(SSL_get0_peer_certificates(client_.get())), |
| 3264 | 1u); |
| 3265 |
| 3266 | EXPECT_EQ(sk_X509_num(SSL_get_peer_cert_chain(server_.get())), 0u); |
| 3267 | EXPECT_EQ(sk_CRYPTO_BUFFER_num(SSL_get0_peer_certificates(server_.get())), |
| 3268 | 1u); |
| 3269 | } |
| 3270 |
| 3271 | TEST_P(SSLVersionTest, NoPeerCertificate) { |
| 3272 | SSL_CTX_set_verify(server_ctx_.get(), SSL_VERIFY_PEER, nullptr); |
| 3273 | SSL_CTX_set_cert_verify_callback(server_ctx_.get(), VerifySucceed, NULL); |
| 3274 | SSL_CTX_set_cert_verify_callback(client_ctx_.get(), VerifySucceed, NULL); |
| 3275 |
| 3276 | ASSERT_TRUE(Connect()); |
| 3277 |
| 3278 | // Server should not see a peer certificate. |
| 3279 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(server_.get())); |
| 3280 | ASSERT_FALSE(peer); |
| 3281 | ASSERT_FALSE(SSL_get0_peer_certificates(server_.get())); |
| 3282 | } |
| 3283 |
| 3284 | TEST_P(SSLVersionTest, RetainOnlySHA256OfCerts) { |
| 3285 | uint8_t *cert_der = NULL; |
| 3286 | int cert_der_len = i2d_X509(cert_.get(), &cert_der); |
| 3287 | ASSERT_GE(cert_der_len, 0); |
| 3288 | bssl::UniquePtr<uint8_t> free_cert_der(cert_der); |
| 3289 |
| 3290 | uint8_t cert_sha256[SHA256_DIGEST_LENGTH]; |
| 3291 | SHA256(cert_der, cert_der_len, cert_sha256); |
| 3292 |
| 3293 | ASSERT_TRUE(UseCertAndKey(client_ctx_.get())); |
| 3294 |
| 3295 | // Configure both client and server to accept any certificate, but the |
| 3296 | // server must retain only the SHA-256 of the peer. |
| 3297 | SSL_CTX_set_verify(client_ctx_.get(), |
| 3298 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 3299 | nullptr); |
| 3300 | SSL_CTX_set_verify(server_ctx_.get(), |
| 3301 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 3302 | nullptr); |
| 3303 | SSL_CTX_set_cert_verify_callback(client_ctx_.get(), VerifySucceed, NULL); |
| 3304 | SSL_CTX_set_cert_verify_callback(server_ctx_.get(), VerifySucceed, NULL); |
| 3305 | SSL_CTX_set_retain_only_sha256_of_client_certs(server_ctx_.get(), 1); |
| 3306 |
| 3307 | ASSERT_TRUE(Connect()); |
| 3308 |
| 3309 | // The peer certificate has been dropped. |
| 3310 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(server_.get())); |
| 3311 | EXPECT_FALSE(peer); |
| 3312 |
| 3313 | SSL_SESSION *session = SSL_get_session(server_.get()); |
| 3314 | EXPECT_TRUE(SSL_SESSION_has_peer_sha256(session)); |
| 3315 |
| 3316 | const uint8_t *peer_sha256; |
| 3317 | size_t peer_sha256_len; |
| 3318 | SSL_SESSION_get0_peer_sha256(session, &peer_sha256, &peer_sha256_len); |
| 3319 | EXPECT_EQ(Bytes(cert_sha256), Bytes(peer_sha256, peer_sha256_len)); |
| 3320 | } |
| 3321 |
| 3322 | // Tests that our ClientHellos do not change unexpectedly. These are purely |
| 3323 | // change detection tests. If they fail as part of an intentional ClientHello |
| 3324 | // change, update the test vector. |
| 3325 | TEST(SSLTest, ClientHello) { |
| 3326 | struct { |
| 3327 | uint16_t max_version; |
| 3328 | std::vector<uint8_t> expected; |
| 3329 | } kTests[] = { |
| 3330 | {TLS1_VERSION, |
| 3331 | {0x16, 0x03, 0x01, 0x00, 0x58, 0x01, 0x00, 0x00, 0x54, 0x03, 0x01, 0x00, |
| 3332 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3333 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3334 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x09, |
| 3335 | 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, |
| 3336 | 0x00, 0x1f, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 3337 | 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 3338 | 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00}}, |
| 3339 | {TLS1_1_VERSION, |
| 3340 | {0x16, 0x03, 0x01, 0x00, 0x58, 0x01, 0x00, 0x00, 0x54, 0x03, 0x02, 0x00, |
| 3341 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3342 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3343 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x09, |
| 3344 | 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x2f, 0x00, 0x35, 0x01, 0x00, |
| 3345 | 0x00, 0x1f, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 3346 | 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 3347 | 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00}}, |
| 3348 | {TLS1_2_VERSION, |
| 3349 | {0x16, 0x03, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x7c, 0x03, 0x03, 0x00, |
| 3350 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3351 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3352 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xcc, 0xa9, |
| 3353 | 0xcc, 0xa8, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, 0xc0, 0x09, |
| 3354 | 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, |
| 3355 | 0x00, 0x35, 0x01, 0x00, 0x00, 0x37, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, |
| 3356 | 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, |
| 3357 | 0x17, 0x00, 0x18, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, |
| 3358 | 0x00, 0x00, 0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, |
| 3359 | 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, |
| 3360 | 0x01}}, |
| 3361 | // TODO(davidben): Add a change detector for TLS 1.3 once the spec and our |
| 3362 | // implementation has settled enough that it won't change. |
| 3363 | }; |
| 3364 |
| 3365 | for (const auto &t : kTests) { |
| 3366 | SCOPED_TRACE(t.max_version); |
| 3367 |
| 3368 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 3369 | ASSERT_TRUE(ctx); |
| 3370 | // Our default cipher list varies by CPU capabilities, so manually place the |
| 3371 | // ChaCha20 ciphers in front. |
| 3372 | const char *cipher_list = "CHACHA20:ALL"; |
| 3373 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_VERSION)); |
| 3374 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), t.max_version)); |
| 3375 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(ctx.get(), cipher_list)); |
| 3376 |
| 3377 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 3378 | ASSERT_TRUE(ssl); |
| 3379 | std::vector<uint8_t> client_hello; |
| 3380 | ASSERT_TRUE(GetClientHello(ssl.get(), &client_hello)); |
| 3381 |
| 3382 | // Zero the client_random. |
| 3383 | constexpr size_t kRandomOffset = 1 + 2 + 2 + // record header |
| 3384 | 1 + 3 + // handshake message header |
| 3385 | 2; // client_version |
| 3386 | ASSERT_GE(client_hello.size(), kRandomOffset + SSL3_RANDOM_SIZE); |
| 3387 | OPENSSL_memset(client_hello.data() + kRandomOffset, 0, SSL3_RANDOM_SIZE); |
| 3388 |
| 3389 | if (client_hello != t.expected) { |
| 3390 | ADD_FAILURE() << "ClientHellos did not match."; |
| 3391 | // Print the value manually so it is easier to update the test vector. |
| 3392 | for (size_t i = 0; i < client_hello.size(); i += 12) { |
| 3393 | printf(" %c", i == 0 ? '{' : ' '); |
| 3394 | for (size_t j = i; j < client_hello.size() && j < i + 12; j++) { |
| 3395 | if (j > i) { |
| 3396 | printf(" "); |
| 3397 | } |
| 3398 | printf("0x%02x", client_hello[j]); |
| 3399 | if (j < client_hello.size() - 1) { |
| 3400 | printf(","); |
| 3401 | } |
| 3402 | } |
| 3403 | if (i + 12 >= client_hello.size()) { |
| 3404 | printf("}},"); |
| 3405 | } |
| 3406 | printf("\n"); |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 |
| 3412 | static void ExpectSessionReused(SSL_CTX *client_ctx, SSL_CTX *server_ctx, |
| 3413 | SSL_SESSION *session, bool want_reused) { |
| 3414 | bssl::UniquePtr<SSL> client, server; |
| 3415 | ClientConfig config; |
| 3416 | config.session = session; |
| 3417 | ASSERT_TRUE( |
| 3418 | ConnectClientAndServer(&client, &server, client_ctx, server_ctx, config)); |
| 3419 |
| 3420 | EXPECT_EQ(SSL_session_reused(client.get()), SSL_session_reused(server.get())); |
| 3421 |
| 3422 | bool was_reused = !!SSL_session_reused(client.get()); |
| 3423 | EXPECT_EQ(was_reused, want_reused); |
| 3424 | } |
| 3425 |
| 3426 | static bssl::UniquePtr<SSL_SESSION> ExpectSessionRenewed(SSL_CTX *client_ctx, |
| 3427 | SSL_CTX *server_ctx, |
| 3428 | SSL_SESSION *session) { |
| 3429 | g_last_session = nullptr; |
| 3430 | SSL_CTX_sess_set_new_cb(client_ctx, SaveLastSession); |
| 3431 |
| 3432 | bssl::UniquePtr<SSL> client, server; |
| 3433 | ClientConfig config; |
| 3434 | config.session = session; |
| 3435 | if (!ConnectClientAndServer(&client, &server, client_ctx, server_ctx, |
| 3436 | config) || |
| 3437 | !FlushNewSessionTickets(client.get(), server.get())) { |
| 3438 | fprintf(stderr, "Failed to connect client and server.\n"); |
| 3439 | return nullptr; |
| 3440 | } |
| 3441 |
| 3442 | if (SSL_session_reused(client.get()) != SSL_session_reused(server.get())) { |
| 3443 | fprintf(stderr, "Client and server were inconsistent.\n"); |
| 3444 | return nullptr; |
| 3445 | } |
| 3446 |
| 3447 | if (!SSL_session_reused(client.get())) { |
| 3448 | fprintf(stderr, "Session was not reused.\n"); |
| 3449 | return nullptr; |
| 3450 | } |
| 3451 |
| 3452 | SSL_CTX_sess_set_new_cb(client_ctx, nullptr); |
| 3453 |
| 3454 | if (!g_last_session) { |
| 3455 | fprintf(stderr, "Client did not receive a renewed session.\n"); |
| 3456 | return nullptr; |
| 3457 | } |
| 3458 | return std::move(g_last_session); |
| 3459 | } |
| 3460 |
| 3461 | static void ExpectTicketKeyChanged(SSL_CTX *ctx, uint8_t *inout_key, |
| 3462 | bool changed) { |
| 3463 | uint8_t new_key[kTicketKeyLen]; |
| 3464 | // May return 0, 1 or 48. |
| 3465 | ASSERT_EQ(SSL_CTX_get_tlsext_ticket_keys(ctx, new_key, kTicketKeyLen), 1); |
| 3466 | if (changed) { |
| 3467 | ASSERT_NE(Bytes(inout_key, kTicketKeyLen), Bytes(new_key)); |
| 3468 | } else { |
| 3469 | ASSERT_EQ(Bytes(inout_key, kTicketKeyLen), Bytes(new_key)); |
| 3470 | } |
| 3471 | OPENSSL_memcpy(inout_key, new_key, kTicketKeyLen); |
| 3472 | } |
| 3473 |
| 3474 | static int SwitchSessionIDContextSNI(SSL *ssl, int *out_alert, void *arg) { |
| 3475 | static const uint8_t kContext[] = {3}; |
| 3476 |
| 3477 | if (!SSL_set_session_id_context(ssl, kContext, sizeof(kContext))) { |
| 3478 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 3479 | } |
| 3480 |
| 3481 | return SSL_TLSEXT_ERR_OK; |
| 3482 | } |
| 3483 |
| 3484 | TEST_P(SSLVersionTest, SessionIDContext) { |
| 3485 | static const uint8_t kContext1[] = {1}; |
| 3486 | static const uint8_t kContext2[] = {2}; |
| 3487 |
| 3488 | ASSERT_TRUE(SSL_CTX_set_session_id_context(server_ctx_.get(), kContext1, |
| 3489 | sizeof(kContext1))); |
| 3490 |
| 3491 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 3492 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 3493 |
| 3494 | bssl::UniquePtr<SSL_SESSION> session = |
| 3495 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 3496 | ASSERT_TRUE(session); |
| 3497 |
| 3498 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3499 | session.get(), |
| 3500 | true /* expect session reused */)); |
| 3501 |
| 3502 | // Change the session ID context. |
| 3503 | ASSERT_TRUE(SSL_CTX_set_session_id_context(server_ctx_.get(), kContext2, |
| 3504 | sizeof(kContext2))); |
| 3505 |
| 3506 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3507 | session.get(), |
| 3508 | false /* expect session not reused */)); |
| 3509 |
| 3510 | // Change the session ID context back and install an SNI callback to switch |
| 3511 | // it. |
| 3512 | ASSERT_TRUE(SSL_CTX_set_session_id_context(server_ctx_.get(), kContext1, |
| 3513 | sizeof(kContext1))); |
| 3514 |
| 3515 | SSL_CTX_set_tlsext_servername_callback(server_ctx_.get(), |
| 3516 | SwitchSessionIDContextSNI); |
| 3517 |
| 3518 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3519 | session.get(), |
| 3520 | false /* expect session not reused */)); |
| 3521 |
| 3522 | // Switch the session ID context with the early callback instead. |
| 3523 | SSL_CTX_set_tlsext_servername_callback(server_ctx_.get(), nullptr); |
| 3524 | SSL_CTX_set_select_certificate_cb( |
| 3525 | server_ctx_.get(), |
| 3526 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 3527 | static const uint8_t kContext[] = {3}; |
| 3528 |
| 3529 | if (!SSL_set_session_id_context(client_hello->ssl, kContext, |
| 3530 | sizeof(kContext))) { |
| 3531 | return ssl_select_cert_error; |
| 3532 | } |
| 3533 |
| 3534 | return ssl_select_cert_success; |
| 3535 | }); |
| 3536 |
| 3537 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3538 | session.get(), |
| 3539 | false /* expect session not reused */)); |
| 3540 | } |
| 3541 |
| 3542 | static timeval g_current_time; |
| 3543 |
| 3544 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
| 3545 | *out_clock = g_current_time; |
| 3546 | } |
| 3547 |
| 3548 | static void FrozenTimeCallback(const SSL *ssl, timeval *out_clock) { |
| 3549 | out_clock->tv_sec = 1000; |
| 3550 | out_clock->tv_usec = 0; |
| 3551 | } |
| 3552 |
| 3553 | static int RenewTicketCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 3554 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 3555 | int encrypt) { |
| 3556 | static const uint8_t kZeros[16] = {0}; |
| 3557 |
| 3558 | if (encrypt) { |
| 3559 | OPENSSL_memcpy(key_name, kZeros, sizeof(kZeros)); |
| 3560 | RAND_bytes(iv, 16); |
| 3561 | } else if (OPENSSL_memcmp(key_name, kZeros, 16) != 0) { |
| 3562 | return 0; |
| 3563 | } |
| 3564 |
| 3565 | if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) || |
| 3566 | !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) { |
| 3567 | return -1; |
| 3568 | } |
| 3569 |
| 3570 | // Returning two from the callback in decrypt mode renews the |
| 3571 | // session in TLS 1.2 and below. |
| 3572 | return encrypt ? 1 : 2; |
| 3573 | } |
| 3574 |
| 3575 | static bool GetServerTicketTime(long *out, const SSL_SESSION *session) { |
| 3576 | const uint8_t *ticket; |
| 3577 | size_t ticket_len; |
| 3578 | SSL_SESSION_get0_ticket(session, &ticket, &ticket_len); |
| 3579 | if (ticket_len < 16 + 16 + SHA256_DIGEST_LENGTH) { |
| 3580 | return false; |
| 3581 | } |
| 3582 |
| 3583 | const uint8_t *ciphertext = ticket + 16 + 16; |
| 3584 | size_t len = ticket_len - 16 - 16 - SHA256_DIGEST_LENGTH; |
| 3585 | auto plaintext = std::make_unique<uint8_t[]>(len); |
| 3586 |
| 3587 | static const uint8_t kZeros[16] = {0}; |
| 3588 | const uint8_t *iv = ticket + 16; |
| 3589 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 3590 | int len1, len2; |
| 3591 | if (len > INT_MAX || |
| 3592 | !EVP_DecryptInit_ex(ctx.get(), EVP_aes_128_cbc(), nullptr, kZeros, iv) || |
| 3593 | !EVP_DecryptUpdate(ctx.get(), plaintext.get(), &len1, ciphertext, |
| 3594 | static_cast<int>(len)) || |
| 3595 | !EVP_DecryptFinal_ex(ctx.get(), plaintext.get() + len1, &len2)) { |
| 3596 | return false; |
| 3597 | } |
| 3598 |
| 3599 | len = static_cast<size_t>(len1 + len2); |
| 3600 |
| 3601 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(TLS_method())); |
| 3602 | if (!ssl_ctx) { |
| 3603 | return false; |
| 3604 | } |
| 3605 | bssl::UniquePtr<SSL_SESSION> server_session( |
| 3606 | SSL_SESSION_from_bytes(plaintext.get(), len, ssl_ctx.get())); |
| 3607 | if (!server_session) { |
| 3608 | return false; |
| 3609 | } |
| 3610 |
| 3611 | *out = SSL_SESSION_get_time(server_session.get()); |
| 3612 | return true; |
| 3613 | } |
| 3614 |
| 3615 | TEST_P(SSLVersionTest, SessionTimeout) { |
| 3616 | for (bool server_test : {false, true}) { |
| 3617 | SCOPED_TRACE(server_test); |
| 3618 |
| 3619 | ASSERT_NO_FATAL_FAILURE(ResetContexts()); |
| 3620 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 3621 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 3622 |
| 3623 | static const time_t kStartTime = 1000; |
| 3624 | g_current_time.tv_sec = kStartTime; |
| 3625 |
| 3626 | // We are willing to use a longer lifetime for TLS 1.3 sessions as |
| 3627 | // resumptions still perform ECDHE. |
| 3628 | const time_t timeout = is_tls13() ? SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT |
| 3629 | : SSL_DEFAULT_SESSION_TIMEOUT; |
| 3630 |
| 3631 | // Both client and server must enforce session timeouts. We configure the |
| 3632 | // other side with a frozen clock so it never expires tickets. |
| 3633 | if (server_test) { |
| 3634 | SSL_CTX_set_current_time_cb(client_ctx_.get(), FrozenTimeCallback); |
| 3635 | SSL_CTX_set_current_time_cb(server_ctx_.get(), CurrentTimeCallback); |
| 3636 | } else { |
| 3637 | SSL_CTX_set_current_time_cb(client_ctx_.get(), CurrentTimeCallback); |
| 3638 | SSL_CTX_set_current_time_cb(server_ctx_.get(), FrozenTimeCallback); |
| 3639 | } |
| 3640 |
| 3641 | // Configure a ticket callback which renews tickets. |
| 3642 | SSL_CTX_set_tlsext_ticket_key_cb(server_ctx_.get(), RenewTicketCallback); |
| 3643 |
| 3644 | bssl::UniquePtr<SSL_SESSION> session = |
| 3645 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 3646 | ASSERT_TRUE(session); |
| 3647 |
| 3648 | // Advance the clock just behind the timeout. |
| 3649 | g_current_time.tv_sec += timeout - 1; |
| 3650 |
| 3651 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3652 | session.get(), |
| 3653 | true /* expect session reused */)); |
| 3654 |
| 3655 | // Advance the clock one more second. |
| 3656 | g_current_time.tv_sec++; |
| 3657 |
| 3658 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3659 | session.get(), |
| 3660 | false /* expect session not reused */)); |
| 3661 |
| 3662 | // Rewind the clock to before the session was minted. |
| 3663 | g_current_time.tv_sec = kStartTime - 1; |
| 3664 |
| 3665 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3666 | session.get(), |
| 3667 | false /* expect session not reused */)); |
| 3668 |
| 3669 | // Renew the session 10 seconds before expiration. |
| 3670 | time_t new_start_time = kStartTime + timeout - 10; |
| 3671 | g_current_time.tv_sec = new_start_time; |
| 3672 | bssl::UniquePtr<SSL_SESSION> new_session = ExpectSessionRenewed( |
| 3673 | client_ctx_.get(), server_ctx_.get(), session.get()); |
| 3674 | ASSERT_TRUE(new_session); |
| 3675 |
| 3676 | // This new session is not the same object as before. |
| 3677 | EXPECT_NE(session.get(), new_session.get()); |
| 3678 |
| 3679 | // Check the sessions have timestamps measured from issuance. |
| 3680 | long session_time = 0; |
| 3681 | if (server_test) { |
| 3682 | ASSERT_TRUE(GetServerTicketTime(&session_time, new_session.get())); |
| 3683 | } else { |
| 3684 | session_time = SSL_SESSION_get_time(new_session.get()); |
| 3685 | } |
| 3686 |
| 3687 | ASSERT_EQ(session_time, g_current_time.tv_sec); |
| 3688 |
| 3689 | if (is_tls13()) { |
| 3690 | // Renewal incorporates fresh key material in TLS 1.3, so we extend the |
| 3691 | // lifetime TLS 1.3. |
| 3692 | g_current_time.tv_sec = new_start_time + timeout - 1; |
| 3693 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3694 | new_session.get(), |
| 3695 | true /* expect session reused */)); |
| 3696 |
| 3697 | // The new session expires after the new timeout. |
| 3698 | g_current_time.tv_sec = new_start_time + timeout + 1; |
| 3699 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3700 | new_session.get(), |
| 3701 | false /* expect session ot reused */)); |
| 3702 |
| 3703 | // Renew the session until it begins just past the auth timeout. |
| 3704 | time_t auth_end_time = kStartTime + SSL_DEFAULT_SESSION_AUTH_TIMEOUT; |
| 3705 | while (new_start_time < auth_end_time - 1000) { |
| 3706 | // Get as close as possible to target start time. |
| 3707 | new_start_time = |
| 3708 | std::min(auth_end_time - 1000, new_start_time + timeout - 1); |
| 3709 | g_current_time.tv_sec = new_start_time; |
| 3710 | new_session = ExpectSessionRenewed(client_ctx_.get(), server_ctx_.get(), |
| 3711 | new_session.get()); |
| 3712 | ASSERT_TRUE(new_session); |
| 3713 | } |
| 3714 |
| 3715 | // Now the session's lifetime is bound by the auth timeout. |
| 3716 | g_current_time.tv_sec = auth_end_time - 1; |
| 3717 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3718 | new_session.get(), |
| 3719 | true /* expect session reused */)); |
| 3720 |
| 3721 | g_current_time.tv_sec = auth_end_time + 1; |
| 3722 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3723 | new_session.get(), |
| 3724 | false /* expect session ot reused */)); |
| 3725 | } else { |
| 3726 | // The new session is usable just before the old expiration. |
| 3727 | g_current_time.tv_sec = kStartTime + timeout - 1; |
| 3728 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3729 | new_session.get(), |
| 3730 | true /* expect session reused */)); |
| 3731 |
| 3732 | // Renewal does not extend the lifetime, so it is not usable beyond the |
| 3733 | // old expiration. |
| 3734 | g_current_time.tv_sec = kStartTime + timeout + 1; |
| 3735 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3736 | new_session.get(), |
| 3737 | false /* expect session not reused */)); |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 |
| 3742 | TEST_P(SSLVersionTest, DefaultTicketKeyInitialization) { |
| 3743 | static const uint8_t kZeroKey[kTicketKeyLen] = {}; |
| 3744 | uint8_t ticket_key[kTicketKeyLen]; |
| 3745 | ASSERT_EQ(1, SSL_CTX_get_tlsext_ticket_keys(server_ctx_.get(), ticket_key, |
| 3746 | kTicketKeyLen)); |
| 3747 | ASSERT_NE(0, OPENSSL_memcmp(ticket_key, kZeroKey, kTicketKeyLen)); |
| 3748 | } |
| 3749 |
| 3750 | TEST_P(SSLVersionTest, DefaultTicketKeyRotation) { |
| 3751 | static const time_t kStartTime = 1001; |
| 3752 | g_current_time.tv_sec = kStartTime; |
| 3753 |
| 3754 | // We use session reuse as a proxy for ticket decryption success, hence |
| 3755 | // disable session timeouts. |
| 3756 | SSL_CTX_set_timeout(server_ctx_.get(), std::numeric_limits<uint32_t>::max()); |
| 3757 | SSL_CTX_set_session_psk_dhe_timeout(server_ctx_.get(), |
| 3758 | std::numeric_limits<uint32_t>::max()); |
| 3759 |
| 3760 | SSL_CTX_set_current_time_cb(client_ctx_.get(), FrozenTimeCallback); |
| 3761 | SSL_CTX_set_current_time_cb(server_ctx_.get(), CurrentTimeCallback); |
| 3762 |
| 3763 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 3764 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_OFF); |
| 3765 |
| 3766 | // Initialize ticket_key with the current key and check that it was |
| 3767 | // initialized to something, not all zeros. |
| 3768 | uint8_t ticket_key[kTicketKeyLen] = {0}; |
| 3769 | TRACED_CALL(ExpectTicketKeyChanged(server_ctx_.get(), ticket_key, |
| 3770 | true /* changed */)); |
| 3771 |
| 3772 | // Verify ticket resumption actually works. |
| 3773 | bssl::UniquePtr<SSL_SESSION> session = |
| 3774 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 3775 | ASSERT_TRUE(session); |
| 3776 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3777 | session.get(), true /* reused */)); |
| 3778 |
| 3779 | // Advance time to just before key rotation. |
| 3780 | g_current_time.tv_sec += SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL - 1; |
| 3781 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3782 | session.get(), true /* reused */)); |
| 3783 | TRACED_CALL(ExpectTicketKeyChanged(server_ctx_.get(), ticket_key, |
| 3784 | false /* NOT changed */)); |
| 3785 |
| 3786 | // Force key rotation. |
| 3787 | g_current_time.tv_sec += 1; |
| 3788 | bssl::UniquePtr<SSL_SESSION> new_session = |
| 3789 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 3790 | TRACED_CALL(ExpectTicketKeyChanged(server_ctx_.get(), ticket_key, |
| 3791 | true /* changed */)); |
| 3792 |
| 3793 | // Resumption with both old and new ticket should work. |
| 3794 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3795 | session.get(), true /* reused */)); |
| 3796 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3797 | new_session.get(), true /* reused */)); |
| 3798 | TRACED_CALL(ExpectTicketKeyChanged(server_ctx_.get(), ticket_key, |
| 3799 | false /* NOT changed */)); |
| 3800 |
| 3801 | // Force key rotation again. Resumption with the old ticket now fails. |
| 3802 | g_current_time.tv_sec += SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
| 3803 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3804 | session.get(), false /* NOT reused */)); |
| 3805 | TRACED_CALL(ExpectTicketKeyChanged(server_ctx_.get(), ticket_key, |
| 3806 | true /* changed */)); |
| 3807 |
| 3808 | // But resumption with the newer session still works. |
| 3809 | TRACED_CALL(ExpectSessionReused(client_ctx_.get(), server_ctx_.get(), |
| 3810 | new_session.get(), true /* reused */)); |
| 3811 | } |
| 3812 |
| 3813 | static int SwitchContext(SSL *ssl, int *out_alert, void *arg) { |
| 3814 | SSL_CTX *ctx = reinterpret_cast<SSL_CTX *>(arg); |
| 3815 | SSL_set_SSL_CTX(ssl, ctx); |
| 3816 | return SSL_TLSEXT_ERR_OK; |
| 3817 | } |
| 3818 |
| 3819 | TEST_P(SSLVersionTest, SNICallback) { |
| 3820 | bssl::UniquePtr<X509> cert2 = GetECDSATestCertificate(); |
| 3821 | ASSERT_TRUE(cert2); |
| 3822 | bssl::UniquePtr<EVP_PKEY> key2 = GetECDSATestKey(); |
| 3823 | ASSERT_TRUE(key2); |
| 3824 |
| 3825 | // Test that switching the |SSL_CTX| at the SNI callback behaves correctly. |
| 3826 | static const uint16_t kECDSAWithSHA256 = SSL_SIGN_ECDSA_SECP256R1_SHA256; |
| 3827 |
| 3828 | static const uint8_t kSCTList[] = {0, 6, 0, 4, 5, 6, 7, 8}; |
| 3829 | static const uint8_t kOCSPResponse[] = {1, 2, 3, 4}; |
| 3830 |
| 3831 | bssl::UniquePtr<SSL_CTX> server_ctx2 = CreateContext(); |
| 3832 | ASSERT_TRUE(server_ctx2); |
| 3833 | ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx2.get(), cert2.get())); |
| 3834 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx2.get(), key2.get())); |
| 3835 | ASSERT_TRUE(SSL_CTX_set_signed_cert_timestamp_list( |
| 3836 | server_ctx2.get(), kSCTList, sizeof(kSCTList))); |
| 3837 | ASSERT_TRUE(SSL_CTX_set_ocsp_response(server_ctx2.get(), kOCSPResponse, |
| 3838 | sizeof(kOCSPResponse))); |
| 3839 | // Historically signing preferences would be lost in some cases with the |
| 3840 | // SNI callback, which triggers the TLS 1.2 SHA-1 default. To ensure |
| 3841 | // this doesn't happen when |version| is TLS 1.2, configure the private |
| 3842 | // key to only sign SHA-256. |
| 3843 | ASSERT_TRUE(SSL_CTX_set_signing_algorithm_prefs(server_ctx2.get(), |
| 3844 | &kECDSAWithSHA256, 1)); |
| 3845 |
| 3846 | SSL_CTX_set_tlsext_servername_callback(server_ctx_.get(), SwitchContext); |
| 3847 | SSL_CTX_set_tlsext_servername_arg(server_ctx_.get(), server_ctx2.get()); |
| 3848 |
| 3849 | SSL_CTX_enable_signed_cert_timestamps(client_ctx_.get()); |
| 3850 | SSL_CTX_enable_ocsp_stapling(client_ctx_.get()); |
| 3851 |
| 3852 | ASSERT_TRUE(Connect()); |
| 3853 |
| 3854 | // The client should have received |cert2|. |
| 3855 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(client_.get())); |
| 3856 | ASSERT_TRUE(peer); |
| 3857 | EXPECT_EQ(X509_cmp(peer.get(), cert2.get()), 0); |
| 3858 |
| 3859 | // The client should have received |server_ctx2|'s SCT list. |
| 3860 | const uint8_t *data; |
| 3861 | size_t len; |
| 3862 | SSL_get0_signed_cert_timestamp_list(client_.get(), &data, &len); |
| 3863 | EXPECT_EQ(Bytes(kSCTList), Bytes(data, len)); |
| 3864 |
| 3865 | // The client should have received |server_ctx2|'s OCSP response. |
| 3866 | SSL_get0_ocsp_response(client_.get(), &data, &len); |
| 3867 | EXPECT_EQ(Bytes(kOCSPResponse), Bytes(data, len)); |
| 3868 | } |
| 3869 |
| 3870 | // Test that the early callback can swap the maximum version. |
| 3871 | TEST(SSLTest, EarlyCallbackVersionSwitch) { |
| 3872 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 3873 | CreateContextWithTestCertificate(TLS_method()); |
| 3874 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 3875 | ASSERT_TRUE(server_ctx); |
| 3876 | ASSERT_TRUE(client_ctx); |
| 3877 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 3878 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 3879 |
| 3880 | SSL_CTX_set_select_certificate_cb( |
| 3881 | server_ctx.get(), |
| 3882 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 3883 | if (!SSL_set_max_proto_version(client_hello->ssl, TLS1_2_VERSION)) { |
| 3884 | return ssl_select_cert_error; |
| 3885 | } |
| 3886 |
| 3887 | return ssl_select_cert_success; |
| 3888 | }); |
| 3889 |
| 3890 | bssl::UniquePtr<SSL> client, server; |
| 3891 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 3892 | server_ctx.get())); |
| 3893 | EXPECT_EQ(TLS1_2_VERSION, SSL_version(client.get())); |
| 3894 | } |
| 3895 |
| 3896 | TEST(SSLTest, SetVersion) { |
| 3897 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 3898 | ASSERT_TRUE(ctx); |
| 3899 |
| 3900 | // Set valid TLS versions. |
| 3901 | for (const auto &vers : kAllVersions) { |
| 3902 | SCOPED_TRACE(vers.name); |
| 3903 | if (vers.ssl_method == VersionParam::is_tls) { |
| 3904 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), vers.version)); |
| 3905 | EXPECT_EQ(SSL_CTX_get_max_proto_version(ctx.get()), vers.version); |
| 3906 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), vers.version)); |
| 3907 | EXPECT_EQ(SSL_CTX_get_min_proto_version(ctx.get()), vers.version); |
| 3908 | } |
| 3909 | } |
| 3910 |
| 3911 | // Invalid TLS versions are rejected. |
| 3912 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), DTLS1_VERSION)); |
| 3913 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x0200)); |
| 3914 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x1234)); |
| 3915 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), DTLS1_VERSION)); |
| 3916 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x0200)); |
| 3917 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)); |
| 3918 |
| 3919 | // Zero is the default version. |
| 3920 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), 0)); |
| 3921 | EXPECT_EQ(TLS1_3_VERSION, SSL_CTX_get_max_proto_version(ctx.get())); |
| 3922 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), 0)); |
| 3923 | EXPECT_EQ(TLS1_2_VERSION, SSL_CTX_get_min_proto_version(ctx.get())); |
| 3924 |
| 3925 | // SSL 3.0 is not available. |
| 3926 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), SSL3_VERSION)); |
| 3927 |
| 3928 | ctx.reset(SSL_CTX_new(DTLS_method())); |
| 3929 | ASSERT_TRUE(ctx); |
| 3930 |
| 3931 | // Set valid DTLS versions. |
| 3932 | for (const auto &vers : kAllVersions) { |
| 3933 | SCOPED_TRACE(vers.name); |
| 3934 | if (vers.ssl_method == VersionParam::is_dtls) { |
| 3935 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), vers.version)); |
| 3936 | EXPECT_EQ(SSL_CTX_get_max_proto_version(ctx.get()), vers.version); |
| 3937 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), vers.version)); |
| 3938 | EXPECT_EQ(SSL_CTX_get_min_proto_version(ctx.get()), vers.version); |
| 3939 | } |
| 3940 | } |
| 3941 |
| 3942 | // Invalid DTLS versions are rejected. |
| 3943 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_VERSION)); |
| 3944 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */)); |
| 3945 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */)); |
| 3946 | EXPECT_FALSE(SSL_CTX_set_max_proto_version(ctx.get(), 0x1234)); |
| 3947 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_VERSION)); |
| 3948 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0xfefe /* DTLS 1.1 */)); |
| 3949 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0xfffe /* DTLS 0.1 */)); |
| 3950 | EXPECT_FALSE(SSL_CTX_set_min_proto_version(ctx.get(), 0x1234)); |
| 3951 |
| 3952 | // Zero is the default version. |
| 3953 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), 0)); |
| 3954 | EXPECT_EQ(DTLS1_2_VERSION, SSL_CTX_get_max_proto_version(ctx.get())); |
| 3955 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), 0)); |
| 3956 | EXPECT_EQ(DTLS1_2_VERSION, SSL_CTX_get_min_proto_version(ctx.get())); |
| 3957 | } |
| 3958 |
| 3959 | static const char *GetVersionName(uint16_t version) { |
| 3960 | switch (version) { |
| 3961 | case TLS1_VERSION: |
| 3962 | return "TLSv1"; |
| 3963 | case TLS1_1_VERSION: |
| 3964 | return "TLSv1.1"; |
| 3965 | case TLS1_2_VERSION: |
| 3966 | return "TLSv1.2"; |
| 3967 | case TLS1_3_VERSION: |
| 3968 | return "TLSv1.3"; |
| 3969 | case DTLS1_VERSION: |
| 3970 | return "DTLSv1"; |
| 3971 | case DTLS1_2_VERSION: |
| 3972 | return "DTLSv1.2"; |
| 3973 | case DTLS1_3_VERSION: |
| 3974 | return "DTLSv1.3"; |
| 3975 | default: |
| 3976 | return "???"; |
| 3977 | } |
| 3978 | } |
| 3979 |
| 3980 | TEST_P(SSLVersionTest, Version) { |
| 3981 | ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 3982 | server_ctx_.get())); |
| 3983 | // Before the handshake, |SSL_version| reports some placeholder value. |
| 3984 | const uint16_t placeholder = is_dtls() ? DTLS1_2_VERSION : TLS1_2_VERSION; |
| 3985 | EXPECT_EQ(SSL_version(client_.get()), placeholder); |
| 3986 | EXPECT_EQ(SSL_version(server_.get()), placeholder); |
| 3987 |
| 3988 | // After the handshake, |SSL_version| reports the version. |
| 3989 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 3990 | EXPECT_EQ(SSL_version(client_.get()), version()); |
| 3991 | EXPECT_EQ(SSL_version(server_.get()), version()); |
| 3992 |
| 3993 | // Test the version name is reported as expected. |
| 3994 | const char *version_name = GetVersionName(version()); |
| 3995 | EXPECT_EQ(strcmp(version_name, SSL_get_version(client_.get())), 0); |
| 3996 | EXPECT_EQ(strcmp(version_name, SSL_get_version(server_.get())), 0); |
| 3997 |
| 3998 | // Test SSL_SESSION reports the same name. |
| 3999 | const char *client_name = |
| 4000 | SSL_SESSION_get_version(SSL_get_session(client_.get())); |
| 4001 | const char *server_name = |
| 4002 | SSL_SESSION_get_version(SSL_get_session(server_.get())); |
| 4003 | EXPECT_EQ(strcmp(version_name, client_name), 0); |
| 4004 | EXPECT_EQ(strcmp(version_name, server_name), 0); |
| 4005 |
| 4006 | // |SSL_clear| should reset the |SSL|s to the original state. |
| 4007 | ASSERT_TRUE(SSL_clear(client_.get())); |
| 4008 | ASSERT_TRUE(SSL_clear(server_.get())); |
| 4009 | EXPECT_EQ(SSL_version(client_.get()), placeholder); |
| 4010 | EXPECT_EQ(SSL_version(server_.get()), placeholder); |
| 4011 | } |
| 4012 |
| 4013 | // Tests that that |SSL_get_pending_cipher| is available during the ALPN |
| 4014 | // selection callback. |
| 4015 | TEST_P(SSLVersionTest, ALPNCipherAvailable) { |
| 4016 | ASSERT_TRUE(UseCertAndKey(client_ctx_.get())); |
| 4017 |
| 4018 | static const uint8_t kALPNProtos[] = {0x03, 'f', 'o', 'o'}; |
| 4019 | ASSERT_EQ(SSL_CTX_set_alpn_protos(client_ctx_.get(), kALPNProtos, |
| 4020 | sizeof(kALPNProtos)), |
| 4021 | 0); |
| 4022 |
| 4023 | // The ALPN callback does not fail the handshake on error, so have the |
| 4024 | // callback write a boolean. |
| 4025 | std::pair<uint16_t, bool> callback_state(version(), false); |
| 4026 | SSL_CTX_set_alpn_select_cb( |
| 4027 | server_ctx_.get(), |
| 4028 | [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 4029 | unsigned in_len, void *arg) -> int { |
| 4030 | auto state = reinterpret_cast<std::pair<uint16_t, bool> *>(arg); |
| 4031 | if (SSL_get_pending_cipher(ssl) != nullptr && |
| 4032 | SSL_version(ssl) == state->first) { |
| 4033 | state->second = true; |
| 4034 | } |
| 4035 | return SSL_TLSEXT_ERR_NOACK; |
| 4036 | }, |
| 4037 | &callback_state); |
| 4038 |
| 4039 | ASSERT_TRUE(Connect()); |
| 4040 |
| 4041 | ASSERT_TRUE(callback_state.second); |
| 4042 | } |
| 4043 |
| 4044 | TEST_P(SSLVersionTest, SSLClearSessionResumption) { |
| 4045 | // Skip this for TLS 1.3. TLS 1.3's ticket mechanism is incompatible with this |
| 4046 | // API pattern. |
| 4047 | if (is_tls13()) { |
| 4048 | return; |
| 4049 | } |
| 4050 |
| 4051 | shed_handshake_config_ = false; |
| 4052 | ASSERT_TRUE(Connect()); |
| 4053 |
| 4054 | EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 4055 | EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 4056 |
| 4057 | // Reset everything. |
| 4058 | ASSERT_TRUE(SSL_clear(client_.get())); |
| 4059 | ASSERT_TRUE(SSL_clear(server_.get())); |
| 4060 |
| 4061 | // Attempt to connect a second time. |
| 4062 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 4063 |
| 4064 | // |SSL_clear| should implicitly offer the previous session to the server. |
| 4065 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 4066 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 4067 | } |
| 4068 |
| 4069 | TEST_P(SSLVersionTest, SSLClearFailsWithShedding) { |
| 4070 | shed_handshake_config_ = false; |
| 4071 | ASSERT_TRUE(Connect()); |
| 4072 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 4073 |
| 4074 | // Reset everything. |
| 4075 | ASSERT_TRUE(SSL_clear(client_.get())); |
| 4076 | ASSERT_TRUE(SSL_clear(server_.get())); |
| 4077 |
| 4078 | // Now enable shedding, and connect a second time. |
| 4079 | shed_handshake_config_ = true; |
| 4080 | ASSERT_TRUE(Connect()); |
| 4081 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 4082 |
| 4083 | // |SSL_clear| should now fail. |
| 4084 | ASSERT_FALSE(SSL_clear(client_.get())); |
| 4085 | ASSERT_FALSE(SSL_clear(server_.get())); |
| 4086 | } |
| 4087 |
| 4088 | static bool ChainsEqual(const STACK_OF(X509) *chain, |
| 4089 | const std::vector<X509 *> &expected) { |
| 4090 | if (sk_X509_num(chain) != expected.size()) { |
| 4091 | return false; |
| 4092 | } |
| 4093 |
| 4094 | for (size_t i = 0; i < expected.size(); i++) { |
| 4095 | if (X509_cmp(sk_X509_value(chain, i), expected[i]) != 0) { |
| 4096 | return false; |
| 4097 | } |
| 4098 | } |
| 4099 |
| 4100 | return true; |
| 4101 | } |
| 4102 |
| 4103 | static bool BuffersEqual(const STACK_OF(CRYPTO_BUFFER) *chain, |
| 4104 | const std::vector<CRYPTO_BUFFER *> &expected) { |
| 4105 | if (sk_CRYPTO_BUFFER_num(chain) != expected.size()) { |
| 4106 | return false; |
| 4107 | } |
| 4108 |
| 4109 | for (size_t i = 0; i < expected.size(); i++) { |
| 4110 | const CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_value(chain, i); |
| 4111 | if (Bytes(CRYPTO_BUFFER_data(buf), CRYPTO_BUFFER_len(buf)) != |
| 4112 | Bytes(CRYPTO_BUFFER_data(expected[i]), |
| 4113 | CRYPTO_BUFFER_len(expected[i]))) { |
| 4114 | return false; |
| 4115 | } |
| 4116 | } |
| 4117 |
| 4118 | return true; |
| 4119 | } |
| 4120 |
| 4121 | TEST_P(SSLVersionTest, AutoChain) { |
| 4122 | cert_ = GetChainTestCertificate(); |
| 4123 | ASSERT_TRUE(cert_); |
| 4124 | key_ = GetChainTestKey(); |
| 4125 | ASSERT_TRUE(key_); |
| 4126 | bssl::UniquePtr<X509> intermediate = GetChainTestIntermediate(); |
| 4127 | ASSERT_TRUE(intermediate); |
| 4128 |
| 4129 | ASSERT_TRUE(UseCertAndKey(client_ctx_.get())); |
| 4130 | ASSERT_TRUE(UseCertAndKey(server_ctx_.get())); |
| 4131 |
| 4132 | // Configure both client and server to accept any certificate. Add |
| 4133 | // |intermediate| to the cert store. |
| 4134 | ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(client_ctx_.get()), |
| 4135 | intermediate.get())); |
| 4136 | ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(server_ctx_.get()), |
| 4137 | intermediate.get())); |
| 4138 | SSL_CTX_set_verify(client_ctx_.get(), |
| 4139 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 4140 | nullptr); |
| 4141 | SSL_CTX_set_verify(server_ctx_.get(), |
| 4142 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 4143 | nullptr); |
| 4144 | SSL_CTX_set_cert_verify_callback(client_ctx_.get(), VerifySucceed, NULL); |
| 4145 | SSL_CTX_set_cert_verify_callback(server_ctx_.get(), VerifySucceed, NULL); |
| 4146 |
| 4147 | // By default, the client and server should each only send the leaf. |
| 4148 | ASSERT_TRUE(Connect()); |
| 4149 |
| 4150 | EXPECT_TRUE( |
| 4151 | ChainsEqual(SSL_get_peer_full_cert_chain(client_.get()), {cert_.get()})); |
| 4152 | EXPECT_TRUE( |
| 4153 | ChainsEqual(SSL_get_peer_full_cert_chain(server_.get()), {cert_.get()})); |
| 4154 |
| 4155 | // If auto-chaining is enabled, then the intermediate is sent. |
| 4156 | SSL_CTX_clear_mode(client_ctx_.get(), SSL_MODE_NO_AUTO_CHAIN); |
| 4157 | SSL_CTX_clear_mode(server_ctx_.get(), SSL_MODE_NO_AUTO_CHAIN); |
| 4158 | ASSERT_TRUE(Connect()); |
| 4159 |
| 4160 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(client_.get()), |
| 4161 | {cert_.get(), intermediate.get()})); |
| 4162 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(server_.get()), |
| 4163 | {cert_.get(), intermediate.get()})); |
| 4164 |
| 4165 | // Auto-chaining does not override explicitly-configured intermediates. |
| 4166 | ASSERT_TRUE(SSL_CTX_add1_chain_cert(client_ctx_.get(), cert_.get())); |
| 4167 | ASSERT_TRUE(SSL_CTX_add1_chain_cert(server_ctx_.get(), cert_.get())); |
| 4168 | ASSERT_TRUE(Connect()); |
| 4169 |
| 4170 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(client_.get()), |
| 4171 | {cert_.get(), cert_.get()})); |
| 4172 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(server_.get()), |
| 4173 | {cert_.get(), cert_.get()})); |
| 4174 |
| 4175 | // Auto-chaining does not override explicitly-configured intermediates that |
| 4176 | // are configured as late as cert_cb. If this fails, something in the |
| 4177 | // handshake is likely auto-chaining too early. |
| 4178 | SSL_CTX_clear_chain_certs(client_ctx_.get()); |
| 4179 | SSL_CTX_clear_chain_certs(server_ctx_.get()); |
| 4180 | auto install_intermediate = [](SSL *ssl, void *arg) -> int { |
| 4181 | return SSL_add1_chain_cert(ssl, static_cast<X509 *>(arg)); |
| 4182 | }; |
| 4183 | SSL_CTX_set_cert_cb(client_ctx_.get(), install_intermediate, cert_.get()); |
| 4184 | SSL_CTX_set_cert_cb(server_ctx_.get(), install_intermediate, cert_.get()); |
| 4185 | ASSERT_TRUE(Connect()); |
| 4186 |
| 4187 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(client_.get()), |
| 4188 | {cert_.get(), cert_.get()})); |
| 4189 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(server_.get()), |
| 4190 | {cert_.get(), cert_.get()})); |
| 4191 | } |
| 4192 |
| 4193 | static bool ExpectSingleError(int lib, int reason) { |
| 4194 | const char *expected = ERR_reason_error_string(ERR_PACK(lib, reason)); |
| 4195 | int err = ERR_get_error(); |
| 4196 | if (ERR_GET_LIB(err) != lib || ERR_GET_REASON(err) != reason) { |
| 4197 | char buf[ERR_ERROR_STRING_BUF_LEN]; |
| 4198 | ERR_error_string_n(err, buf, sizeof(buf)); |
| 4199 | fprintf(stderr, "Wanted %s, got: %s.\n", expected, buf); |
| 4200 | return false; |
| 4201 | } |
| 4202 |
| 4203 | if (ERR_peek_error() != 0) { |
| 4204 | fprintf(stderr, "Unexpected error following %s.\n", expected); |
| 4205 | return false; |
| 4206 | } |
| 4207 |
| 4208 | return true; |
| 4209 | } |
| 4210 |
| 4211 | TEST_P(SSLVersionTest, SSLWriteRetry) { |
| 4212 | if (is_dtls()) { |
| 4213 | return; |
| 4214 | } |
| 4215 |
| 4216 | for (bool enable_partial_write : {false, true}) { |
| 4217 | SCOPED_TRACE(enable_partial_write); |
| 4218 |
| 4219 | // Connect a client and server. |
| 4220 | ASSERT_TRUE(Connect()); |
| 4221 |
| 4222 | if (enable_partial_write) { |
| 4223 | SSL_set_mode(client_.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
| 4224 | } |
| 4225 |
| 4226 | // Write without reading until the buffer is full and we have an unfinished |
| 4227 | // write. Keep a count so we may reread it again later. "hello!" will be |
| 4228 | // written in two chunks, "hello" and "!". |
| 4229 | char data[] = "hello!"; |
| 4230 | static const int kChunkLen = 5; // The length of "hello". |
| 4231 | unsigned count = 0; |
| 4232 | for (;;) { |
| 4233 | int ret = SSL_write(client_.get(), data, kChunkLen); |
| 4234 | if (ret <= 0) { |
| 4235 | ASSERT_EQ(SSL_get_error(client_.get(), ret), SSL_ERROR_WANT_WRITE); |
| 4236 | break; |
| 4237 | } |
| 4238 | ASSERT_EQ(ret, 5); |
| 4239 | count++; |
| 4240 | } |
| 4241 |
| 4242 | // Retrying with the same parameters is legal. |
| 4243 | ASSERT_EQ( |
| 4244 | SSL_get_error(client_.get(), SSL_write(client_.get(), data, kChunkLen)), |
| 4245 | SSL_ERROR_WANT_WRITE); |
| 4246 |
| 4247 | // Retrying with the same buffer but shorter length is not legal. |
| 4248 | ASSERT_EQ(SSL_get_error(client_.get(), |
| 4249 | SSL_write(client_.get(), data, kChunkLen - 1)), |
| 4250 | SSL_ERROR_SSL); |
| 4251 | ASSERT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY)); |
| 4252 |
| 4253 | // Retrying with a different buffer pointer is not legal. |
| 4254 | char data2[] = "hello"; |
| 4255 | ASSERT_EQ(SSL_get_error(client_.get(), |
| 4256 | SSL_write(client_.get(), data2, kChunkLen)), |
| 4257 | SSL_ERROR_SSL); |
| 4258 | ASSERT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY)); |
| 4259 |
| 4260 | // With |SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER|, the buffer may move. |
| 4261 | SSL_set_mode(client_.get(), SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 4262 | ASSERT_EQ(SSL_get_error(client_.get(), |
| 4263 | SSL_write(client_.get(), data2, kChunkLen)), |
| 4264 | SSL_ERROR_WANT_WRITE); |
| 4265 |
| 4266 | // |SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER| does not disable length checks. |
| 4267 | ASSERT_EQ(SSL_get_error(client_.get(), |
| 4268 | SSL_write(client_.get(), data2, kChunkLen - 1)), |
| 4269 | SSL_ERROR_SSL); |
| 4270 | ASSERT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY)); |
| 4271 |
| 4272 | // Retrying with a larger buffer is legal. |
| 4273 | ASSERT_EQ(SSL_get_error(client_.get(), |
| 4274 | SSL_write(client_.get(), data, kChunkLen + 1)), |
| 4275 | SSL_ERROR_WANT_WRITE); |
| 4276 |
| 4277 | // Drain the buffer. |
| 4278 | char buf[20]; |
| 4279 | for (unsigned i = 0; i < count; i++) { |
| 4280 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4281 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4282 | } |
| 4283 |
| 4284 | // Now that there is space, a retry with a larger buffer should flush the |
| 4285 | // pending record, skip over that many bytes of input (on assumption they |
| 4286 | // are the same), and write the remainder. If SSL_MODE_ENABLE_PARTIAL_WRITE |
| 4287 | // is set, this will complete in two steps. |
| 4288 | char data_longer[] = "_____!!!!!"; |
| 4289 | if (enable_partial_write) { |
| 4290 | ASSERT_EQ(SSL_write(client_.get(), data_longer, 2 * kChunkLen), |
| 4291 | kChunkLen); |
| 4292 | ASSERT_EQ(SSL_write(client_.get(), data_longer + kChunkLen, kChunkLen), |
| 4293 | kChunkLen); |
| 4294 | } else { |
| 4295 | ASSERT_EQ(SSL_write(client_.get(), data_longer, 2 * kChunkLen), |
| 4296 | 2 * kChunkLen); |
| 4297 | } |
| 4298 |
| 4299 | // Check the last write was correct. The data will be spread over two |
| 4300 | // records, so SSL_read returns twice. |
| 4301 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4302 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4303 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4304 | ASSERT_EQ(OPENSSL_memcmp(buf, "!!!!!", kChunkLen), 0); |
| 4305 |
| 4306 | // Fill the transport buffer again. This time only leave room for one |
| 4307 | // record. |
| 4308 | count = 0; |
| 4309 | for (;;) { |
| 4310 | int ret = SSL_write(client_.get(), data, kChunkLen); |
| 4311 | if (ret <= 0) { |
| 4312 | ASSERT_EQ(SSL_get_error(client_.get(), ret), SSL_ERROR_WANT_WRITE); |
| 4313 | break; |
| 4314 | } |
| 4315 | ASSERT_EQ(ret, 5); |
| 4316 | count++; |
| 4317 | } |
| 4318 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4319 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4320 | count--; |
| 4321 |
| 4322 | // Retry the last write, with a longer input. The first half is the most |
| 4323 | // recently failed write, from filling the buffer. |SSL_write| should write |
| 4324 | // that to the transport, and then attempt to write the second half. |
| 4325 | int ret = SSL_write(client_.get(), data_longer, 2 * kChunkLen); |
| 4326 | if (enable_partial_write) { |
| 4327 | // If partial writes are allowed, the write will succeed partially. |
| 4328 | ASSERT_EQ(ret, kChunkLen); |
| 4329 |
| 4330 | // Check the first half and make room for another record. |
| 4331 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4332 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4333 | count--; |
| 4334 |
| 4335 | // Finish writing the input. |
| 4336 | ASSERT_EQ(SSL_write(client_.get(), data_longer + kChunkLen, kChunkLen), |
| 4337 | kChunkLen); |
| 4338 | } else { |
| 4339 | // Otherwise, although the first half made it to the transport, the second |
| 4340 | // half is blocked. |
| 4341 | ASSERT_EQ(ret, -1); |
| 4342 | ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_WRITE); |
| 4343 |
| 4344 | // Check the first half and make room for another record. |
| 4345 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4346 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4347 | count--; |
| 4348 |
| 4349 | // Retrying with fewer bytes than previously attempted is an error. If the |
| 4350 | // input length is less than the number of bytes successfully written, the |
| 4351 | // check happens at a different point, with a different error. |
| 4352 | // |
| 4353 | // TODO(davidben): Should these cases use the same error? |
| 4354 | ASSERT_EQ( |
| 4355 | SSL_get_error(client_.get(), |
| 4356 | SSL_write(client_.get(), data_longer, kChunkLen - 1)), |
| 4357 | SSL_ERROR_SSL); |
| 4358 | ASSERT_TRUE(ExpectSingleError(ERR_LIB_SSL, SSL_R_BAD_LENGTH)); |
| 4359 |
| 4360 | // Complete the write with the correct retry. |
| 4361 | ASSERT_EQ(SSL_write(client_.get(), data_longer, 2 * kChunkLen), |
| 4362 | 2 * kChunkLen); |
| 4363 | } |
| 4364 |
| 4365 | // Drain the input and ensure everything was written correctly. |
| 4366 | for (unsigned i = 0; i < count; i++) { |
| 4367 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4368 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4369 | } |
| 4370 |
| 4371 | // The final write is spread over two records. |
| 4372 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4373 | ASSERT_EQ(OPENSSL_memcmp(buf, "hello", kChunkLen), 0); |
| 4374 | ASSERT_EQ(SSL_read(server_.get(), buf, sizeof(buf)), kChunkLen); |
| 4375 | ASSERT_EQ(OPENSSL_memcmp(buf, "!!!!!", kChunkLen), 0); |
| 4376 | } |
| 4377 | } |
| 4378 |
| 4379 | TEST_P(SSLVersionTest, RecordCallback) { |
| 4380 | if (version() == DTLS1_3_VERSION) { |
| 4381 | // The DTLS 1.3 record header is vastly different than the TLS or DTLS < 1.3 |
| 4382 | // header format. Instead of checking that the record header is formatted as |
| 4383 | // expected here, the runner implementation in dtls.go is strict about what |
| 4384 | // it accepts. |
| 4385 | return; |
| 4386 | } |
| 4387 | for (bool test_server : {true, false}) { |
| 4388 | SCOPED_TRACE(test_server); |
| 4389 | ASSERT_NO_FATAL_FAILURE(ResetContexts()); |
| 4390 |
| 4391 | bool read_seen = false; |
| 4392 | bool write_seen = false; |
| 4393 | auto cb = [&](int is_write, int cb_version, int cb_type, const void *buf, |
| 4394 | size_t len, SSL *ssl) { |
| 4395 | if (cb_type != SSL3_RT_HEADER) { |
| 4396 | return; |
| 4397 | } |
| 4398 |
| 4399 | // The callback does not report a version for records. |
| 4400 | EXPECT_EQ(0, cb_version); |
| 4401 |
| 4402 | if (is_write) { |
| 4403 | write_seen = true; |
| 4404 | } else { |
| 4405 | read_seen = true; |
| 4406 | } |
| 4407 |
| 4408 | // Sanity-check that the record header is plausible. |
| 4409 | CBS cbs; |
| 4410 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(buf), len); |
| 4411 | uint8_t type; |
| 4412 | uint16_t record_version, length; |
| 4413 | ASSERT_TRUE(CBS_get_u8(&cbs, &type)); |
| 4414 | ASSERT_TRUE(CBS_get_u16(&cbs, &record_version)); |
| 4415 | EXPECT_EQ(record_version >> 8, is_dtls() ? 0xfe : 0x03); |
| 4416 | if (is_dtls()) { |
| 4417 | uint16_t epoch; |
| 4418 | ASSERT_TRUE(CBS_get_u16(&cbs, &epoch)); |
| 4419 | uint16_t max_epoch = 1; |
| 4420 | EXPECT_LE(epoch, max_epoch) << "Invalid epoch: " << epoch; |
| 4421 | ASSERT_TRUE(CBS_skip(&cbs, 6)); |
| 4422 | } |
| 4423 | ASSERT_TRUE(CBS_get_u16(&cbs, &length)); |
| 4424 | EXPECT_EQ(0u, CBS_len(&cbs)); |
| 4425 | }; |
| 4426 | using CallbackType = decltype(cb); |
| 4427 | SSL_CTX *ctx = test_server ? server_ctx_.get() : client_ctx_.get(); |
| 4428 | SSL_CTX_set_msg_callback( |
| 4429 | ctx, [](int is_write, int cb_version, int cb_type, const void *buf, |
| 4430 | size_t len, SSL *ssl, void *arg) { |
| 4431 | CallbackType *cb_ptr = reinterpret_cast<CallbackType *>(arg); |
| 4432 | (*cb_ptr)(is_write, cb_version, cb_type, buf, len, ssl); |
| 4433 | }); |
| 4434 | SSL_CTX_set_msg_callback_arg(ctx, &cb); |
| 4435 |
| 4436 | ASSERT_TRUE(Connect()); |
| 4437 |
| 4438 | EXPECT_TRUE(read_seen); |
| 4439 | EXPECT_TRUE(write_seen); |
| 4440 | } |
| 4441 | } |
| 4442 |
| 4443 | TEST_P(SSLVersionTest, GetServerName) { |
| 4444 | ClientConfig config; |
| 4445 | config.servername = "host1"; |
| 4446 |
| 4447 | SSL_CTX_set_tlsext_servername_callback( |
| 4448 | server_ctx_.get(), [](SSL *ssl, int *out_alert, void *arg) -> int { |
| 4449 | // During the handshake, |SSL_get_servername| must match |config|. |
| 4450 | ClientConfig *config_p = reinterpret_cast<ClientConfig *>(arg); |
| 4451 | EXPECT_STREQ(config_p->servername.c_str(), |
| 4452 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)); |
| 4453 | return SSL_TLSEXT_ERR_OK; |
| 4454 | }); |
| 4455 | SSL_CTX_set_tlsext_servername_arg(server_ctx_.get(), &config); |
| 4456 |
| 4457 | ASSERT_TRUE(Connect(config)); |
| 4458 | // After the handshake, it must also be available. |
| 4459 | EXPECT_STREQ(config.servername.c_str(), |
| 4460 | SSL_get_servername(server_.get(), TLSEXT_NAMETYPE_host_name)); |
| 4461 |
| 4462 | // Establish a session under host1. |
| 4463 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 4464 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 4465 | bssl::UniquePtr<SSL_SESSION> session = |
| 4466 | CreateClientSession(client_ctx_.get(), server_ctx_.get(), config); |
| 4467 |
| 4468 | // If the client resumes a session with a different name, |SSL_get_servername| |
| 4469 | // must return the new name. |
| 4470 | ASSERT_TRUE(session); |
| 4471 | config.session = session.get(); |
| 4472 | config.servername = "host2"; |
| 4473 | ASSERT_TRUE(Connect(config)); |
| 4474 | EXPECT_STREQ(config.servername.c_str(), |
| 4475 | SSL_get_servername(server_.get(), TLSEXT_NAMETYPE_host_name)); |
| 4476 | } |
| 4477 |
| 4478 | // Test that session cache mode bits are honored in the client session callback. |
| 4479 | TEST_P(SSLVersionTest, ClientSessionCacheMode) { |
| 4480 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_OFF); |
| 4481 | EXPECT_FALSE(CreateClientSession(client_ctx_.get(), server_ctx_.get())); |
| 4482 |
| 4483 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_CLIENT); |
| 4484 | EXPECT_TRUE(CreateClientSession(client_ctx_.get(), server_ctx_.get())); |
| 4485 |
| 4486 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_SERVER); |
| 4487 | EXPECT_FALSE(CreateClientSession(client_ctx_.get(), server_ctx_.get())); |
| 4488 | } |
| 4489 |
| 4490 | // Test that all versions survive tiny write buffers. In particular, TLS 1.3 |
| 4491 | // NewSessionTickets are written post-handshake. Servers that block |
| 4492 | // |SSL_do_handshake| on writing them will deadlock if clients are not draining |
| 4493 | // the buffer. Test that we do not do this. |
| 4494 | TEST_P(SSLVersionTest, SmallBuffer) { |
| 4495 | // DTLS is a datagram protocol and requires packet-sized buffers. |
| 4496 | if (is_dtls()) { |
| 4497 | return; |
| 4498 | } |
| 4499 |
| 4500 | // Test both flushing NewSessionTickets with a zero-sized write and |
| 4501 | // non-zero-sized write. |
| 4502 | for (bool use_zero_write : {false, true}) { |
| 4503 | SCOPED_TRACE(use_zero_write); |
| 4504 |
| 4505 | g_last_session = nullptr; |
| 4506 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 4507 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 4508 |
| 4509 | bssl::UniquePtr<SSL> client(SSL_new(client_ctx_.get())), |
| 4510 | server(SSL_new(server_ctx_.get())); |
| 4511 | ASSERT_TRUE(client); |
| 4512 | ASSERT_TRUE(server); |
| 4513 | SSL_set_connect_state(client.get()); |
| 4514 | SSL_set_accept_state(server.get()); |
| 4515 |
| 4516 | // Use a tiny buffer. |
| 4517 | BIO *bio1, *bio2; |
| 4518 | ASSERT_TRUE(BIO_new_bio_pair(&bio1, 1, &bio2, 1)); |
| 4519 |
| 4520 | // SSL_set_bio takes ownership. |
| 4521 | SSL_set_bio(client.get(), bio1, bio1); |
| 4522 | SSL_set_bio(server.get(), bio2, bio2); |
| 4523 |
| 4524 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 4525 | if (version() >= TLS1_3_VERSION) { |
| 4526 | // The post-handshake ticket should not have been processed yet. |
| 4527 | EXPECT_FALSE(g_last_session); |
| 4528 | } |
| 4529 |
| 4530 | if (use_zero_write) { |
| 4531 | ASSERT_TRUE(FlushNewSessionTickets(client.get(), server.get())); |
| 4532 | EXPECT_TRUE(g_last_session); |
| 4533 | } |
| 4534 |
| 4535 | // Send some data from server to client. If |use_zero_write| is false, this |
| 4536 | // will also flush the NewSessionTickets. |
| 4537 | static const char kMessage[] = "hello world"; |
| 4538 | char buf[sizeof(kMessage)]; |
| 4539 | for (;;) { |
| 4540 | int server_ret = SSL_write(server.get(), kMessage, sizeof(kMessage)); |
| 4541 | int server_err = SSL_get_error(server.get(), server_ret); |
| 4542 | int client_ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 4543 | int client_err = SSL_get_error(client.get(), client_ret); |
| 4544 |
| 4545 | // The server will write a single record, so every iteration should see |
| 4546 | // |SSL_ERROR_WANT_WRITE| and |SSL_ERROR_WANT_READ|, until the final |
| 4547 | // iteration, where both will complete. |
| 4548 | if (server_ret > 0) { |
| 4549 | EXPECT_EQ(server_ret, static_cast<int>(sizeof(kMessage))); |
| 4550 | EXPECT_EQ(client_ret, static_cast<int>(sizeof(kMessage))); |
| 4551 | EXPECT_EQ(Bytes(buf), Bytes(kMessage)); |
| 4552 | break; |
| 4553 | } |
| 4554 |
| 4555 | ASSERT_EQ(server_ret, -1); |
| 4556 | ASSERT_EQ(server_err, SSL_ERROR_WANT_WRITE); |
| 4557 | ASSERT_EQ(client_ret, -1); |
| 4558 | ASSERT_EQ(client_err, SSL_ERROR_WANT_READ); |
| 4559 | } |
| 4560 |
| 4561 | // The NewSessionTickets should have been flushed and processed. |
| 4562 | EXPECT_TRUE(g_last_session); |
| 4563 | } |
| 4564 | } |
| 4565 |
| 4566 | TEST(SSLTest, AddChainCertHack) { |
| 4567 | // Ensure that we don't accidently break the hack that we have in place to |
| 4568 | // keep curl and serf happy when they use an |X509| even after transfering |
| 4569 | // ownership. |
| 4570 |
| 4571 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4572 | ASSERT_TRUE(ctx); |
| 4573 | X509 *cert = GetTestCertificate().release(); |
| 4574 | ASSERT_TRUE(cert); |
| 4575 | SSL_CTX_add0_chain_cert(ctx.get(), cert); |
| 4576 |
| 4577 | // This should not trigger a use-after-free. |
| 4578 | X509_cmp(cert, cert); |
| 4579 | } |
| 4580 |
| 4581 | TEST(SSLTest, GetCertificate) { |
| 4582 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4583 | ASSERT_TRUE(ctx); |
| 4584 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 4585 | ASSERT_TRUE(cert); |
| 4586 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 4587 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 4588 | ASSERT_TRUE(ssl); |
| 4589 |
| 4590 | X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 4591 | ASSERT_TRUE(cert2); |
| 4592 | X509 *cert3 = SSL_get_certificate(ssl.get()); |
| 4593 | ASSERT_TRUE(cert3); |
| 4594 |
| 4595 | // The old and new certificates must be identical. |
| 4596 | EXPECT_EQ(0, X509_cmp(cert.get(), cert2)); |
| 4597 | EXPECT_EQ(0, X509_cmp(cert.get(), cert3)); |
| 4598 |
| 4599 | uint8_t *der = nullptr; |
| 4600 | long der_len = i2d_X509(cert.get(), &der); |
| 4601 | ASSERT_LT(0, der_len); |
| 4602 | bssl::UniquePtr<uint8_t> free_der(der); |
| 4603 |
| 4604 | uint8_t *der2 = nullptr; |
| 4605 | long der2_len = i2d_X509(cert2, &der2); |
| 4606 | ASSERT_LT(0, der2_len); |
| 4607 | bssl::UniquePtr<uint8_t> free_der2(der2); |
| 4608 |
| 4609 | uint8_t *der3 = nullptr; |
| 4610 | long der3_len = i2d_X509(cert3, &der3); |
| 4611 | ASSERT_LT(0, der3_len); |
| 4612 | bssl::UniquePtr<uint8_t> free_der3(der3); |
| 4613 |
| 4614 | // They must also encode identically. |
| 4615 | EXPECT_EQ(Bytes(der, der_len), Bytes(der2, der2_len)); |
| 4616 | EXPECT_EQ(Bytes(der, der_len), Bytes(der3, der3_len)); |
| 4617 | } |
| 4618 |
| 4619 | TEST(SSLTest, SetChainAndKeyMismatch) { |
| 4620 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 4621 | ASSERT_TRUE(ctx); |
| 4622 |
| 4623 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 4624 | ASSERT_TRUE(key); |
| 4625 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 4626 | ASSERT_TRUE(leaf); |
| 4627 | std::vector<CRYPTO_BUFFER *> chain = { |
| 4628 | leaf.get(), |
| 4629 | }; |
| 4630 |
| 4631 | // Should fail because |GetTestKey| doesn't match the chain-test certificate. |
| 4632 | ASSERT_FALSE(SSL_CTX_set_chain_and_key(ctx.get(), chain.data(), chain.size(), |
| 4633 | key.get(), nullptr)); |
| 4634 | ERR_clear_error(); |
| 4635 | } |
| 4636 |
| 4637 | TEST(SSLTest, CertThenKeyMismatch) { |
| 4638 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4639 | ASSERT_TRUE(ctx); |
| 4640 |
| 4641 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 4642 | ASSERT_TRUE(key); |
| 4643 | bssl::UniquePtr<X509> leaf = GetChainTestCertificate(); |
| 4644 | ASSERT_TRUE(leaf); |
| 4645 |
| 4646 | // There is no key or certificate, so |SSL_CTX_check_private_key| fails. |
| 4647 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4648 |
| 4649 | // With only a certificate, |SSL_CTX_check_private_key| still fails. |
| 4650 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); |
| 4651 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4652 |
| 4653 | // The private key does not match the certificate, so it should fail. |
| 4654 | EXPECT_FALSE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 4655 |
| 4656 | // Checking the private key fails, but this is really because there is still |
| 4657 | // no private key. |
| 4658 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4659 | EXPECT_EQ(nullptr, SSL_CTX_get0_privatekey(ctx.get())); |
| 4660 | } |
| 4661 |
| 4662 | TEST(SSLTest, KeyThenCertMismatch) { |
| 4663 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4664 | ASSERT_TRUE(ctx); |
| 4665 |
| 4666 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 4667 | ASSERT_TRUE(key); |
| 4668 | bssl::UniquePtr<X509> leaf = GetChainTestCertificate(); |
| 4669 | ASSERT_TRUE(leaf); |
| 4670 |
| 4671 | // There is no key or certificate, so |SSL_CTX_check_private_key| fails. |
| 4672 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4673 |
| 4674 | // With only a key, |SSL_CTX_check_private_key| still fails. |
| 4675 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 4676 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4677 |
| 4678 | // If configuring a certificate that doesn't match the key, configuration |
| 4679 | // actually succeeds. We just silently drop the private key. |
| 4680 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); |
| 4681 | EXPECT_EQ(nullptr, SSL_CTX_get0_privatekey(ctx.get())); |
| 4682 |
| 4683 | // Some callers configure the private key, then the certificate, and then |
| 4684 | // expect |SSL_CTX_check_private_key| to check consistency. It does, but only |
| 4685 | // by way of noticing there is no private key. The actual consistency check |
| 4686 | // happened in |SSL_CTX_use_certificate|. |
| 4687 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); |
| 4688 | } |
| 4689 |
| 4690 | TEST(SSLTest, OverrideCertAndKey) { |
| 4691 | // It is possible to override an existing certificate by configuring |
| 4692 | // certificate, then key, due to |SSL_CTX_use_certificate|'s above silent |
| 4693 | // dropping behavior. |
| 4694 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4695 | ASSERT_TRUE(ctx); |
| 4696 |
| 4697 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 4698 | ASSERT_TRUE(key); |
| 4699 | bssl::UniquePtr<X509> leaf = GetTestCertificate(); |
| 4700 | ASSERT_TRUE(leaf); |
| 4701 | bssl::UniquePtr<EVP_PKEY> key2 = GetChainTestKey(); |
| 4702 | ASSERT_TRUE(key2); |
| 4703 | bssl::UniquePtr<X509> leaf2 = GetChainTestCertificate(); |
| 4704 | ASSERT_TRUE(leaf2); |
| 4705 |
| 4706 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); |
| 4707 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 4708 |
| 4709 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf2.get())); |
| 4710 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key2.get())); |
| 4711 | } |
| 4712 |
| 4713 | TEST(SSLTest, OverrideKeyMethodWithKey) { |
| 4714 | // Make an SSL_PRIVATE_KEY_METHOD that should never be called. |
| 4715 | static const SSL_PRIVATE_KEY_METHOD kErrorMethod = { |
| 4716 | [](SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 4717 | uint16_t signature_algorithm, const uint8_t *in, |
| 4718 | size_t in_len) { return ssl_private_key_failure; }, |
| 4719 | [](SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 4720 | const uint8_t *in, size_t in_len) { return ssl_private_key_failure; }, |
| 4721 | [](SSL *ssl, uint8_t *out, size_t *out_len, size_t max_oun) { |
| 4722 | return ssl_private_key_failure; |
| 4723 | }, |
| 4724 | }; |
| 4725 |
| 4726 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 4727 | ASSERT_TRUE(key); |
| 4728 | bssl::UniquePtr<X509> leaf = GetTestCertificate(); |
| 4729 | ASSERT_TRUE(leaf); |
| 4730 |
| 4731 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4732 | ASSERT_TRUE(ctx); |
| 4733 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); |
| 4734 |
| 4735 | // Configuring an |SSL_PRIVATE_KEY_METHOD| and then overwriting it with an |
| 4736 | // |EVP_PKEY| should clear the |SSL_PRIVATE_KEY_METHOD|. |
| 4737 | SSL_CTX_set_private_key_method(ctx.get(), &kErrorMethod); |
| 4738 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 4739 |
| 4740 | bssl::UniquePtr<SSL> client, server; |
| 4741 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 4742 | } |
| 4743 |
| 4744 | // Configuring a chain and then overwriting it with a different chain should |
| 4745 | // clear the old one. |
| 4746 | TEST(SSLTest, OverrideChain) { |
| 4747 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 4748 | ASSERT_TRUE(key); |
| 4749 | bssl::UniquePtr<X509> leaf = GetChainTestCertificate(); |
| 4750 | ASSERT_TRUE(leaf); |
| 4751 | bssl::UniquePtr<X509> ca = GetChainTestIntermediate(); |
| 4752 | ASSERT_TRUE(ca); |
| 4753 |
| 4754 | bssl::UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 4755 | ASSERT_TRUE(chain); |
| 4756 | ASSERT_TRUE(bssl::PushToStack(chain.get(), bssl::UpRef(ca))); |
| 4757 |
| 4758 | bssl::UniquePtr<STACK_OF(X509)> wrong_chain(sk_X509_new_null()); |
| 4759 | ASSERT_TRUE(wrong_chain); |
| 4760 | ASSERT_TRUE(bssl::PushToStack(wrong_chain.get(), bssl::UpRef(leaf))); |
| 4761 | ASSERT_TRUE(bssl::PushToStack(wrong_chain.get(), bssl::UpRef(leaf))); |
| 4762 |
| 4763 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4764 | ASSERT_TRUE(ctx); |
| 4765 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); |
| 4766 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 4767 |
| 4768 | // Configure one chain, then replace it with another. Note this API considers |
| 4769 | // the chain to exclude the leaf. |
| 4770 | ASSERT_TRUE(SSL_CTX_set1_chain(ctx.get(), wrong_chain.get())); |
| 4771 | ASSERT_TRUE(SSL_CTX_set1_chain(ctx.get(), chain.get())); |
| 4772 |
| 4773 | bssl::UniquePtr<SSL> client, server; |
| 4774 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 4775 | EXPECT_TRUE(ChainsEqual(SSL_get_peer_full_cert_chain(client.get()), |
| 4776 | {leaf.get(), ca.get()})); |
| 4777 | } |
| 4778 |
| 4779 | TEST(SSLTest, OverrideChainAndKey) { |
| 4780 | bssl::UniquePtr<EVP_PKEY> key1 = GetChainTestKey(); |
| 4781 | ASSERT_TRUE(key1); |
| 4782 | bssl::UniquePtr<CRYPTO_BUFFER> leaf1 = GetChainTestCertificateBuffer(); |
| 4783 | ASSERT_TRUE(leaf1); |
| 4784 | bssl::UniquePtr<CRYPTO_BUFFER> ca1 = GetChainTestIntermediateBuffer(); |
| 4785 | ASSERT_TRUE(ca1); |
| 4786 | bssl::UniquePtr<EVP_PKEY> key2 = GetECDSATestKey(); |
| 4787 | ASSERT_TRUE(key2); |
| 4788 | bssl::UniquePtr<CRYPTO_BUFFER> leaf2 = GetECDSATestCertificateBuffer(); |
| 4789 | ASSERT_TRUE(leaf2); |
| 4790 |
| 4791 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4792 | ASSERT_TRUE(ctx); |
| 4793 |
| 4794 | // Configure one cert and key pair, then replace it with noather. |
| 4795 | std::vector<CRYPTO_BUFFER *> certs = {leaf1.get(), ca1.get()}; |
| 4796 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(ctx.get(), certs.data(), certs.size(), |
| 4797 | key1.get(), nullptr)); |
| 4798 | certs = {leaf2.get()}; |
| 4799 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(ctx.get(), certs.data(), certs.size(), |
| 4800 | key2.get(), nullptr)); |
| 4801 |
| 4802 | bssl::UniquePtr<SSL> client, server; |
| 4803 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 4804 | EXPECT_TRUE( |
| 4805 | BuffersEqual(SSL_get0_peer_certificates(client.get()), {leaf2.get()})); |
| 4806 | } |
| 4807 |
| 4808 | TEST(SSLTest, CredentialChains) { |
| 4809 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 4810 | ASSERT_TRUE(key); |
| 4811 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 4812 | ASSERT_TRUE(leaf); |
| 4813 | bssl::UniquePtr<CRYPTO_BUFFER> ca = GetChainTestIntermediateBuffer(); |
| 4814 | ASSERT_TRUE(ca); |
| 4815 | bssl::UniquePtr<CRYPTO_BUFFER> ca_subject = |
| 4816 | GetChainTestIntermediateIssuerBuffer(); |
| 4817 | ASSERT_TRUE(ca_subject); |
| 4818 |
| 4819 | bssl::UniquePtr<CRYPTO_BUFFER> testcert = GetTestCertificateBuffer(); |
| 4820 | ASSERT_TRUE(testcert); |
| 4821 | bssl::UniquePtr<EVP_PKEY> testkey = GetTestKey(); |
| 4822 | ASSERT_TRUE(testkey); |
| 4823 |
| 4824 | std::vector<CRYPTO_BUFFER *> test_chain = {testcert.get()}; |
| 4825 | std::vector<CRYPTO_BUFFER *> chain = {leaf.get(), ca.get()}; |
| 4826 | std::vector<CRYPTO_BUFFER *> wrong_chain = {leaf.get(), leaf.get(), |
| 4827 | leaf.get()}; |
| 4828 |
| 4829 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 4830 | ASSERT_TRUE(ctx); |
| 4831 | bssl::UniquePtr<SSL_CREDENTIAL> cred(SSL_CREDENTIAL_new_x509()); |
| 4832 | ASSERT_TRUE(cred); |
| 4833 | bssl::UniquePtr<SSL_CREDENTIAL> cred2(SSL_CREDENTIAL_new_x509()); |
| 4834 | ASSERT_TRUE(cred2); |
| 4835 |
| 4836 | // Configure one chain (including the leaf), then replace it with another. |
| 4837 | ASSERT_TRUE(SSL_CREDENTIAL_set1_cert_chain(cred.get(), wrong_chain.data(), |
| 4838 | wrong_chain.size())); |
| 4839 | CBS ca_subject_cbs, ca_cbs; |
| 4840 | CRYPTO_BUFFER_init_CBS(ca.get(), &ca_cbs); |
| 4841 | ASSERT_TRUE(ssl_cert_extract_issuer(&ca_cbs, &ca_subject_cbs)); |
| 4842 | bssl::UniquePtr<CRYPTO_BUFFER> subject_buf( |
| 4843 | CRYPTO_BUFFER_new_from_CBS(&ca_subject_cbs, nullptr)); |
| 4844 | EXPECT_EQ(Bytes(CRYPTO_BUFFER_data(ca_subject.get()), |
| 4845 | CRYPTO_BUFFER_len(ca_subject.get())), |
| 4846 | Bytes(CRYPTO_BUFFER_data(subject_buf.get()), |
| 4847 | CRYPTO_BUFFER_len(subject_buf.get()))); |
| 4848 | #if !defined(BORINGSSL_SHARED_LIBRARY) |
| 4849 | ASSERT_FALSE( |
| 4850 | cred->ChainContainsIssuer(Span(CRYPTO_BUFFER_data(subject_buf.get()), |
| 4851 | CRYPTO_BUFFER_len(subject_buf.get())))); |
| 4852 | #endif |
| 4853 |
| 4854 | ASSERT_TRUE( |
| 4855 | SSL_CREDENTIAL_set1_cert_chain(cred.get(), chain.data(), chain.size())); |
| 4856 |
| 4857 | #if !defined(BORINGSSL_SHARED_LIBRARY) |
| 4858 | ASSERT_TRUE( |
| 4859 | cred->ChainContainsIssuer(Span(CRYPTO_BUFFER_data(subject_buf.get()), |
| 4860 | CRYPTO_BUFFER_len(subject_buf.get())))); |
| 4861 | #endif |
| 4862 |
| 4863 | ASSERT_TRUE(SSL_CREDENTIAL_set1_cert_chain(cred2.get(), test_chain.data(), |
| 4864 | test_chain.size())); |
| 4865 |
| 4866 | ASSERT_TRUE(SSL_CREDENTIAL_set1_private_key(cred.get(), key.get())); |
| 4867 | ASSERT_TRUE(SSL_CREDENTIAL_set1_private_key(cred2.get(), testkey.get())); |
| 4868 | SSL_CREDENTIAL_set_must_match_issuer(cred.get(), 1); |
| 4869 | SSL_CREDENTIAL_set_must_match_issuer(cred2.get(), 1); |
| 4870 | ASSERT_TRUE(SSL_CTX_add1_credential(ctx.get(), cred.get())); |
| 4871 | ASSERT_TRUE(SSL_CTX_add1_credential(ctx.get(), cred2.get())); |
| 4872 |
| 4873 | bssl::UniquePtr<SSL> client, server; |
| 4874 |
| 4875 | // With no CA requested by client, we should fail with only cred1 and cred2 |
| 4876 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 4877 |
| 4878 | // Have the client request a bogus name that will not match |
| 4879 | bssl::UniquePtr<CRYPTO_BUFFER> bogus_subject = GetBogusIssuerBuffer(); |
| 4880 | ASSERT_TRUE(bogus_subject); |
| 4881 | bssl::UniquePtr<SSL> client2, server2; |
| 4882 | ClientConfig bogus_subject_config; |
| 4883 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> bogus_subjects( |
| 4884 | sk_CRYPTO_BUFFER_new_null()); |
| 4885 | ASSERT_TRUE(bogus_subjects); |
| 4886 | ASSERT_TRUE(PushToStack(bogus_subjects.get(), std::move(bogus_subject))); |
| 4887 | bogus_subject_config.ca_names = bogus_subjects.get(); |
| 4888 | bogus_subjects.release(); |
| 4889 | // A bogus issuer that does not match should fail |
| 4890 | ASSERT_FALSE(ConnectClientAndServer(&client2, &server2, ctx.get(), ctx.get(), |
| 4891 | bogus_subject_config)); |
| 4892 |
| 4893 | // Have the client request the name of the chain ca. |
| 4894 | bssl::UniquePtr<CRYPTO_BUFFER> chain_subject = |
| 4895 | GetChainTestIntermediateIssuerBuffer(); |
| 4896 | ASSERT_TRUE(chain_subject); |
| 4897 | bssl::UniquePtr<SSL> client3, server3; |
| 4898 | ClientConfig chain_subject_config; |
| 4899 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain_subjects( |
| 4900 | sk_CRYPTO_BUFFER_new_null()); |
| 4901 | ASSERT_TRUE(chain_subjects); |
| 4902 | ASSERT_TRUE(PushToStack(chain_subjects.get(), std::move(chain_subject))); |
| 4903 | chain_subject_config.ca_names = chain_subjects.get(); |
| 4904 | chain_subjects.release(); |
| 4905 | // If we ask for the chain ca subject, we should get it |
| 4906 | ASSERT_TRUE(ConnectClientAndServer(&client3, &server3, ctx.get(), ctx.get(), |
| 4907 | chain_subject_config)); |
| 4908 | EXPECT_TRUE(BuffersEqual(SSL_get0_peer_certificates(client3.get()), |
| 4909 | {leaf.get(), ca.get()})); |
| 4910 |
| 4911 | // Have the client request the name of the test ca. |
| 4912 | bssl::UniquePtr<CRYPTO_BUFFER> test_subject = GetTestCertIssuerBuffer(); |
| 4913 | ASSERT_TRUE(test_subject); |
| 4914 | bssl::UniquePtr<SSL> client4, server4; |
| 4915 | ClientConfig test_subject_config; |
| 4916 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> test_subjects( |
| 4917 | sk_CRYPTO_BUFFER_new_null()); |
| 4918 | ASSERT_TRUE(test_subjects); |
| 4919 | ASSERT_TRUE(PushToStack(test_subjects.get(), std::move(test_subject))); |
| 4920 | test_subject_config.ca_names = test_subjects.get(); |
| 4921 | test_subjects.release(); |
| 4922 | // If we ask for the test ca subject, we should get it |
| 4923 | ASSERT_TRUE(ConnectClientAndServer(&client4, &server4, ctx.get(), ctx.get(), |
| 4924 | test_subject_config)); |
| 4925 | EXPECT_TRUE(BuffersEqual(SSL_get0_peer_certificates(client4.get()), |
| 4926 | {testcert.get()})); |
| 4927 |
| 4928 | // Add cred3 to the CTX so we have an ubiquitous credential |
| 4929 | bssl::UniquePtr<SSL_CREDENTIAL> cred3(SSL_CREDENTIAL_new_x509()); |
| 4930 | ASSERT_TRUE(cred3); |
| 4931 | ASSERT_TRUE( |
| 4932 | SSL_CREDENTIAL_set1_cert_chain(cred3.get(), chain.data(), chain.size())); |
| 4933 | ASSERT_TRUE(SSL_CREDENTIAL_set1_private_key(cred3.get(), key.get())); |
| 4934 | ASSERT_TRUE(SSL_CTX_add1_credential(ctx.get(), cred3.get())); |
| 4935 |
| 4936 | // With no CA sent, we should now succeed. |
| 4937 | bssl::UniquePtr<SSL> client5, server5; |
| 4938 | ASSERT_TRUE(ConnectClientAndServer(&client5, &server5, ctx.get(), ctx.get())); |
| 4939 | EXPECT_TRUE(BuffersEqual(SSL_get0_peer_certificates(client5.get()), |
| 4940 | {leaf.get(), ca.get()})); |
| 4941 | } |
| 4942 |
| 4943 | TEST(SSLTest, CredentialCertProperties) { |
| 4944 | // A CertificatePropertyList containing a trust_anchors property, and an |
| 4945 | // unknown property 0xbb with 0 bytes of data. |
| 4946 | bssl::UniquePtr<SSL_CREDENTIAL> cred(SSL_CREDENTIAL_new_x509()); |
| 4947 | ASSERT_TRUE(cred); |
| 4948 | static const uint8_t kTestProperties1[] = {0x00, 0x0b, 0x00, 0x00, 0x00, |
| 4949 | 0x03, 0xba, 0xdb, 0x0b, 0x00, |
| 4950 | 0xbb, 0x00, 0x00}; |
| 4951 | bssl::UniquePtr<CRYPTO_BUFFER> pl( |
| 4952 | CRYPTO_BUFFER_new(kTestProperties1, sizeof(kTestProperties1), nullptr)); |
| 4953 | ASSERT_TRUE(pl); |
| 4954 | EXPECT_TRUE( |
| 4955 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 4956 |
| 4957 | // A CertificatePropertyList containing a trust_anchors property, and an |
| 4958 | // unknown property 0xbb with 1 byte of data. |
| 4959 | static const uint8_t kTestProperties2[] = {0x00, 0x0c, 0x00, 0x00, 0x00, |
| 4960 | 0x03, 0xba, 0xdb, 0x0b, 0x00, |
| 4961 | 0xbb, 0x00, 0x01, 0xba}; |
| 4962 | pl.reset( |
| 4963 | CRYPTO_BUFFER_new(kTestProperties2, sizeof(kTestProperties2), nullptr)); |
| 4964 | ASSERT_TRUE(pl); |
| 4965 | EXPECT_TRUE( |
| 4966 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 4967 |
| 4968 | // A CertificatePropertyList containing a trust_anchors property, and an |
| 4969 | // unknown but malformed property 0xbb with missing data. |
| 4970 | static const uint8_t kTestProperties3[] = {0x00, 0x09, 0x00, 0x00, 0x00, 0x03, |
| 4971 | 0xba, 0xdb, 0x0b, 0x00, 0xbb}; |
| 4972 | pl.reset( |
| 4973 | CRYPTO_BUFFER_new(kTestProperties3, sizeof(kTestProperties3), nullptr)); |
| 4974 | ASSERT_TRUE(pl); |
| 4975 | EXPECT_FALSE( |
| 4976 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 4977 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 4978 | SSL_R_INVALID_CERTIFICATE_PROPERTY_LIST)); |
| 4979 |
| 4980 | // A CertificatePropertyList containing a trust_anchors property, and an |
| 4981 | // unknown but malformed property 0xbb with incorrect length data. |
| 4982 | static const uint8_t kTestProperties4[] = {0x00, 0x0c, 0x00, 0x00, 0x00, |
| 4983 | 0x03, 0xba, 0xdb, 0x0b, 0x00, |
| 4984 | 0xbb, 0x00, 0x03, 0xba}; |
| 4985 | pl.reset( |
| 4986 | CRYPTO_BUFFER_new(kTestProperties4, sizeof(kTestProperties4), nullptr)); |
| 4987 | ASSERT_TRUE(pl); |
| 4988 | EXPECT_FALSE( |
| 4989 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 4990 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 4991 | SSL_R_INVALID_CERTIFICATE_PROPERTY_LIST)); |
| 4992 |
| 4993 | // A CertificatePropertyList containing a trust_anchors property with 0 bytes |
| 4994 | // of data. |
| 4995 | static const uint8_t kTestProperties5[] = {0x00, 0x04, 0x00, |
| 4996 | 0x00, 0x00, 0x00}; |
| 4997 | pl.reset( |
| 4998 | CRYPTO_BUFFER_new(kTestProperties5, sizeof(kTestProperties5), nullptr)); |
| 4999 | ASSERT_TRUE(pl); |
| 5000 | EXPECT_FALSE( |
| 5001 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 5002 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 5003 | SSL_R_INVALID_TRUST_ANCHOR_LIST)); |
| 5004 |
| 5005 | // A CertificatePropertyList containing a trust_anchors property with extra |
| 5006 | // data. |
| 5007 | static const uint8_t kTestProperties6[] = {0x00, 0x08, 0x00, 0x00, 0x00, |
| 5008 | 0x03, 0xba, 0xdb, 0x0b, 0xbb}; |
| 5009 | pl.reset( |
| 5010 | CRYPTO_BUFFER_new(kTestProperties6, sizeof(kTestProperties6), nullptr)); |
| 5011 | ASSERT_TRUE(pl); |
| 5012 | EXPECT_FALSE( |
| 5013 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 5014 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 5015 | SSL_R_INVALID_CERTIFICATE_PROPERTY_LIST)); |
| 5016 |
| 5017 | // A CertificatePropertyList containing a trust_anchors property with missing |
| 5018 | // data. |
| 5019 | static const uint8_t kTestProperties7[] = {0x00, 0x06, 0x00, 0x00, |
| 5020 | 0x00, 0x03, 0xba, 0xdb}; |
| 5021 | pl.reset( |
| 5022 | CRYPTO_BUFFER_new(kTestProperties7, sizeof(kTestProperties7), nullptr)); |
| 5023 | ASSERT_TRUE(pl); |
| 5024 | EXPECT_FALSE( |
| 5025 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 5026 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 5027 | SSL_R_INVALID_CERTIFICATE_PROPERTY_LIST)); |
| 5028 |
| 5029 | // A CertificatePropertyList containing only a trust_anchors property. |
| 5030 | static const uint8_t kTestProperties8[] = {0x00, 0x07, 0x00, 0x00, 0x00, |
| 5031 | 0x03, 0xba, 0xdb, 0x0b}; |
| 5032 | pl.reset( |
| 5033 | CRYPTO_BUFFER_new(kTestProperties8, sizeof(kTestProperties8), nullptr)); |
| 5034 | ASSERT_TRUE(pl); |
| 5035 | EXPECT_TRUE( |
| 5036 | SSL_CREDENTIAL_set1_certificate_properties(cred.get(), pl.get())); |
| 5037 | } |
| 5038 |
| 5039 | TEST(SSLTest, SetChainAndKeyCtx) { |
| 5040 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5041 | ASSERT_TRUE(client_ctx); |
| 5042 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5043 | ASSERT_TRUE(server_ctx); |
| 5044 |
| 5045 | ASSERT_EQ(nullptr, SSL_CTX_get0_chain(server_ctx.get())); |
| 5046 |
| 5047 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 5048 | ASSERT_TRUE(key); |
| 5049 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 5050 | ASSERT_TRUE(leaf); |
| 5051 | bssl::UniquePtr<CRYPTO_BUFFER> intermediate = |
| 5052 | GetChainTestIntermediateBuffer(); |
| 5053 | ASSERT_TRUE(intermediate); |
| 5054 | std::vector<CRYPTO_BUFFER *> chain = { |
| 5055 | leaf.get(), |
| 5056 | intermediate.get(), |
| 5057 | }; |
| 5058 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), chain.data(), |
| 5059 | chain.size(), key.get(), nullptr)); |
| 5060 |
| 5061 | ASSERT_EQ(chain.size(), |
| 5062 | sk_CRYPTO_BUFFER_num(SSL_CTX_get0_chain(server_ctx.get()))); |
| 5063 |
| 5064 | SSL_CTX_set_custom_verify( |
| 5065 | client_ctx.get(), SSL_VERIFY_PEER, |
| 5066 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5067 | return ssl_verify_ok; |
| 5068 | }); |
| 5069 |
| 5070 | bssl::UniquePtr<SSL> client, server; |
| 5071 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5072 | server_ctx.get())); |
| 5073 | } |
| 5074 |
| 5075 | TEST(SSLTest, SetChainAndKeySSL) { |
| 5076 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5077 | ASSERT_TRUE(client_ctx); |
| 5078 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5079 | ASSERT_TRUE(server_ctx); |
| 5080 |
| 5081 | bssl::UniquePtr<SSL> client, server; |
| 5082 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 5083 | server_ctx.get())); |
| 5084 | SSL_set_shed_handshake_config(client.get(), true); |
| 5085 | SSL_set_shed_handshake_config(server.get(), true); |
| 5086 |
| 5087 | ASSERT_EQ(nullptr, SSL_get0_chain(server.get())); |
| 5088 |
| 5089 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 5090 | ASSERT_TRUE(key); |
| 5091 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 5092 | ASSERT_TRUE(leaf); |
| 5093 | bssl::UniquePtr<CRYPTO_BUFFER> intermediate = |
| 5094 | GetChainTestIntermediateBuffer(); |
| 5095 | ASSERT_TRUE(intermediate); |
| 5096 | std::vector<CRYPTO_BUFFER *> chain = { |
| 5097 | leaf.get(), |
| 5098 | intermediate.get(), |
| 5099 | }; |
| 5100 | ASSERT_TRUE(SSL_set_chain_and_key(server.get(), chain.data(), chain.size(), |
| 5101 | key.get(), nullptr)); |
| 5102 |
| 5103 | ASSERT_EQ(chain.size(), sk_CRYPTO_BUFFER_num(SSL_get0_chain(server.get()))); |
| 5104 |
| 5105 | SSL_set_custom_verify( |
| 5106 | client.get(), SSL_VERIFY_PEER, |
| 5107 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5108 | return ssl_verify_ok; |
| 5109 | }); |
| 5110 |
| 5111 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 5112 |
| 5113 | // The server is configured to shed handshake config, so the certificate is no |
| 5114 | // longer available after the handshake. |
| 5115 | ASSERT_EQ(nullptr, SSL_get0_chain(server.get())); |
| 5116 | } |
| 5117 |
| 5118 | TEST(SSLTest, BuffersFailWithoutCustomVerify) { |
| 5119 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5120 | ASSERT_TRUE(client_ctx); |
| 5121 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5122 | ASSERT_TRUE(server_ctx); |
| 5123 |
| 5124 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 5125 | ASSERT_TRUE(key); |
| 5126 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 5127 | ASSERT_TRUE(leaf); |
| 5128 | std::vector<CRYPTO_BUFFER *> chain = {leaf.get()}; |
| 5129 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), chain.data(), |
| 5130 | chain.size(), key.get(), nullptr)); |
| 5131 |
| 5132 | // Without SSL_CTX_set_custom_verify(), i.e. with everything in the default |
| 5133 | // configuration, certificate verification should fail. |
| 5134 | bssl::UniquePtr<SSL> client, server; |
| 5135 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5136 | server_ctx.get())); |
| 5137 |
| 5138 | // Whereas with a verifier, the connection should succeed. |
| 5139 | SSL_CTX_set_custom_verify( |
| 5140 | client_ctx.get(), SSL_VERIFY_PEER, |
| 5141 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5142 | return ssl_verify_ok; |
| 5143 | }); |
| 5144 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5145 | server_ctx.get())); |
| 5146 | } |
| 5147 |
| 5148 | TEST(SSLTest, CustomVerify) { |
| 5149 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5150 | ASSERT_TRUE(client_ctx); |
| 5151 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5152 | ASSERT_TRUE(server_ctx); |
| 5153 |
| 5154 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 5155 | ASSERT_TRUE(key); |
| 5156 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 5157 | ASSERT_TRUE(leaf); |
| 5158 | std::vector<CRYPTO_BUFFER *> chain = {leaf.get()}; |
| 5159 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), chain.data(), |
| 5160 | chain.size(), key.get(), nullptr)); |
| 5161 |
| 5162 | SSL_CTX_set_custom_verify( |
| 5163 | client_ctx.get(), SSL_VERIFY_PEER, |
| 5164 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5165 | return ssl_verify_ok; |
| 5166 | }); |
| 5167 |
| 5168 | bssl::UniquePtr<SSL> client, server; |
| 5169 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5170 | server_ctx.get())); |
| 5171 |
| 5172 | // With SSL_VERIFY_PEER, ssl_verify_invalid should result in a dropped |
| 5173 | // connection. |
| 5174 | SSL_CTX_set_custom_verify( |
| 5175 | client_ctx.get(), SSL_VERIFY_PEER, |
| 5176 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5177 | return ssl_verify_invalid; |
| 5178 | }); |
| 5179 |
| 5180 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5181 | server_ctx.get())); |
| 5182 |
| 5183 | // But with SSL_VERIFY_NONE, ssl_verify_invalid should not cause a dropped |
| 5184 | // connection. |
| 5185 | SSL_CTX_set_custom_verify( |
| 5186 | client_ctx.get(), SSL_VERIFY_NONE, |
| 5187 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5188 | return ssl_verify_invalid; |
| 5189 | }); |
| 5190 |
| 5191 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5192 | server_ctx.get())); |
| 5193 | } |
| 5194 |
| 5195 | TEST(SSLTest, ClientCABuffers) { |
| 5196 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5197 | ASSERT_TRUE(client_ctx); |
| 5198 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_with_buffers_method())); |
| 5199 | ASSERT_TRUE(server_ctx); |
| 5200 |
| 5201 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 5202 | ASSERT_TRUE(key); |
| 5203 | bssl::UniquePtr<CRYPTO_BUFFER> leaf = GetChainTestCertificateBuffer(); |
| 5204 | ASSERT_TRUE(leaf); |
| 5205 | bssl::UniquePtr<CRYPTO_BUFFER> intermediate = |
| 5206 | GetChainTestIntermediateBuffer(); |
| 5207 | ASSERT_TRUE(intermediate); |
| 5208 | std::vector<CRYPTO_BUFFER *> chain = { |
| 5209 | leaf.get(), |
| 5210 | intermediate.get(), |
| 5211 | }; |
| 5212 | ASSERT_TRUE(SSL_CTX_set_chain_and_key(server_ctx.get(), chain.data(), |
| 5213 | chain.size(), key.get(), nullptr)); |
| 5214 |
| 5215 | bssl::UniquePtr<CRYPTO_BUFFER> ca_name( |
| 5216 | CRYPTO_BUFFER_new(kTestName, sizeof(kTestName), nullptr)); |
| 5217 | ASSERT_TRUE(ca_name); |
| 5218 | bssl::UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names( |
| 5219 | sk_CRYPTO_BUFFER_new_null()); |
| 5220 | ASSERT_TRUE(ca_names); |
| 5221 | ASSERT_TRUE(PushToStack(ca_names.get(), std::move(ca_name))); |
| 5222 | SSL_CTX_set0_client_CAs(server_ctx.get(), ca_names.release()); |
| 5223 |
| 5224 | // Configure client and server to accept all certificates. |
| 5225 | SSL_CTX_set_custom_verify( |
| 5226 | client_ctx.get(), SSL_VERIFY_PEER, |
| 5227 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5228 | return ssl_verify_ok; |
| 5229 | }); |
| 5230 | SSL_CTX_set_custom_verify( |
| 5231 | server_ctx.get(), SSL_VERIFY_PEER, |
| 5232 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 5233 | return ssl_verify_ok; |
| 5234 | }); |
| 5235 |
| 5236 | bool cert_cb_called = false; |
| 5237 | SSL_CTX_set_cert_cb( |
| 5238 | client_ctx.get(), |
| 5239 | [](SSL *ssl, void *arg) -> int { |
| 5240 | const STACK_OF(CRYPTO_BUFFER) *peer_names = |
| 5241 | SSL_get0_server_requested_CAs(ssl); |
| 5242 | EXPECT_EQ(1u, sk_CRYPTO_BUFFER_num(peer_names)); |
| 5243 | CRYPTO_BUFFER *peer_name = sk_CRYPTO_BUFFER_value(peer_names, 0); |
| 5244 | EXPECT_EQ(Bytes(kTestName), Bytes(CRYPTO_BUFFER_data(peer_name), |
| 5245 | CRYPTO_BUFFER_len(peer_name))); |
| 5246 | *reinterpret_cast<bool *>(arg) = true; |
| 5247 | return 1; |
| 5248 | }, |
| 5249 | &cert_cb_called); |
| 5250 |
| 5251 | bssl::UniquePtr<SSL> client, server; |
| 5252 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5253 | server_ctx.get())); |
| 5254 | EXPECT_TRUE(cert_cb_called); |
| 5255 | } |
| 5256 |
| 5257 | // Configuring the empty cipher list, though an error, should still modify the |
| 5258 | // configuration. |
| 5259 | TEST(SSLTest, EmptyCipherList) { |
| 5260 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 5261 | ASSERT_TRUE(ctx); |
| 5262 |
| 5263 | // Initially, the cipher list is not empty. |
| 5264 | EXPECT_NE(0u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 5265 |
| 5266 | // Configuring the empty cipher list fails. |
| 5267 | EXPECT_FALSE(SSL_CTX_set_cipher_list(ctx.get(), "")); |
| 5268 | ERR_clear_error(); |
| 5269 |
| 5270 | // But the cipher list is still updated to empty. |
| 5271 | EXPECT_EQ(0u, sk_SSL_CIPHER_num(SSL_CTX_get_ciphers(ctx.get()))); |
| 5272 | } |
| 5273 |
| 5274 | // ssl_test_ticket_aead_failure_mode enumerates the possible ways in which the |
| 5275 | // test |SSL_TICKET_AEAD_METHOD| can fail. |
| 5276 | enum ssl_test_ticket_aead_failure_mode { |
| 5277 | ssl_test_ticket_aead_ok = 0, |
| 5278 | ssl_test_ticket_aead_seal_fail, |
| 5279 | ssl_test_ticket_aead_open_soft_fail, |
| 5280 | ssl_test_ticket_aead_open_hard_fail, |
| 5281 | }; |
| 5282 |
| 5283 | struct ssl_test_ticket_aead_state { |
| 5284 | unsigned retry_count = 0; |
| 5285 | ssl_test_ticket_aead_failure_mode failure_mode = ssl_test_ticket_aead_ok; |
| 5286 | }; |
| 5287 |
| 5288 | static int ssl_test_ticket_aead_ex_index_dup(CRYPTO_EX_DATA *to, |
| 5289 | const CRYPTO_EX_DATA *from, |
| 5290 | void **from_d, int index, |
| 5291 | long argl, void *argp) { |
| 5292 | abort(); |
| 5293 | } |
| 5294 |
| 5295 | static void ssl_test_ticket_aead_ex_index_free(void *parent, void *ptr, |
| 5296 | CRYPTO_EX_DATA *ad, int index, |
| 5297 | long argl, void *argp) { |
| 5298 | delete reinterpret_cast<ssl_test_ticket_aead_state *>(ptr); |
| 5299 | } |
| 5300 |
| 5301 | static CRYPTO_once_t g_ssl_test_ticket_aead_ex_index_once = CRYPTO_ONCE_INIT; |
| 5302 | static int g_ssl_test_ticket_aead_ex_index; |
| 5303 |
| 5304 | static int ssl_test_ticket_aead_get_ex_index() { |
| 5305 | CRYPTO_once(&g_ssl_test_ticket_aead_ex_index_once, [] { |
| 5306 | g_ssl_test_ticket_aead_ex_index = SSL_get_ex_new_index( |
| 5307 | 0, nullptr, nullptr, ssl_test_ticket_aead_ex_index_dup, |
| 5308 | ssl_test_ticket_aead_ex_index_free); |
| 5309 | }); |
| 5310 | return g_ssl_test_ticket_aead_ex_index; |
| 5311 | } |
| 5312 |
| 5313 | static size_t ssl_test_ticket_aead_max_overhead(SSL *ssl) { return 1; } |
| 5314 |
| 5315 | static int ssl_test_ticket_aead_seal(SSL *ssl, uint8_t *out, size_t *out_len, |
| 5316 | size_t max_out_len, const uint8_t *in, |
| 5317 | size_t in_len) { |
| 5318 | auto state = reinterpret_cast<ssl_test_ticket_aead_state *>( |
| 5319 | SSL_get_ex_data(ssl, ssl_test_ticket_aead_get_ex_index())); |
| 5320 |
| 5321 | if (state->failure_mode == ssl_test_ticket_aead_seal_fail || |
| 5322 | max_out_len < in_len + 1) { |
| 5323 | return 0; |
| 5324 | } |
| 5325 |
| 5326 | OPENSSL_memmove(out, in, in_len); |
| 5327 | out[in_len] = 0xff; |
| 5328 | *out_len = in_len + 1; |
| 5329 |
| 5330 | return 1; |
| 5331 | } |
| 5332 |
| 5333 | static ssl_ticket_aead_result_t ssl_test_ticket_aead_open( |
| 5334 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out_len, |
| 5335 | const uint8_t *in, size_t in_len) { |
| 5336 | auto state = reinterpret_cast<ssl_test_ticket_aead_state *>( |
| 5337 | SSL_get_ex_data(ssl, ssl_test_ticket_aead_get_ex_index())); |
| 5338 |
| 5339 | if (state->retry_count > 0) { |
| 5340 | state->retry_count--; |
| 5341 | return ssl_ticket_aead_retry; |
| 5342 | } |
| 5343 |
| 5344 | switch (state->failure_mode) { |
| 5345 | case ssl_test_ticket_aead_ok: |
| 5346 | break; |
| 5347 | case ssl_test_ticket_aead_seal_fail: |
| 5348 | // If |seal| failed then there shouldn't be any ticket to try and |
| 5349 | // decrypt. |
| 5350 | abort(); |
| 5351 | break; |
| 5352 | case ssl_test_ticket_aead_open_soft_fail: |
| 5353 | return ssl_ticket_aead_ignore_ticket; |
| 5354 | case ssl_test_ticket_aead_open_hard_fail: |
| 5355 | return ssl_ticket_aead_error; |
| 5356 | } |
| 5357 |
| 5358 | if (in_len == 0 || in[in_len - 1] != 0xff) { |
| 5359 | return ssl_ticket_aead_ignore_ticket; |
| 5360 | } |
| 5361 |
| 5362 | if (max_out_len < in_len - 1) { |
| 5363 | return ssl_ticket_aead_error; |
| 5364 | } |
| 5365 |
| 5366 | OPENSSL_memmove(out, in, in_len - 1); |
| 5367 | *out_len = in_len - 1; |
| 5368 | return ssl_ticket_aead_success; |
| 5369 | } |
| 5370 |
| 5371 | static const SSL_TICKET_AEAD_METHOD kSSLTestTicketMethod = { |
| 5372 | ssl_test_ticket_aead_max_overhead, |
| 5373 | ssl_test_ticket_aead_seal, |
| 5374 | ssl_test_ticket_aead_open, |
| 5375 | }; |
| 5376 |
| 5377 | static void ConnectClientAndServerWithTicketMethod( |
| 5378 | bssl::UniquePtr<SSL> *out_client, bssl::UniquePtr<SSL> *out_server, |
| 5379 | SSL_CTX *client_ctx, SSL_CTX *server_ctx, unsigned retry_count, |
| 5380 | ssl_test_ticket_aead_failure_mode failure_mode, SSL_SESSION *session) { |
| 5381 | bssl::UniquePtr<SSL> client(SSL_new(client_ctx)), server(SSL_new(server_ctx)); |
| 5382 | ASSERT_TRUE(client); |
| 5383 | ASSERT_TRUE(server); |
| 5384 | SSL_set_connect_state(client.get()); |
| 5385 | SSL_set_accept_state(server.get()); |
| 5386 |
| 5387 | auto state = new ssl_test_ticket_aead_state; |
| 5388 | state->retry_count = retry_count; |
| 5389 | state->failure_mode = failure_mode; |
| 5390 |
| 5391 | ASSERT_GE(ssl_test_ticket_aead_get_ex_index(), 0); |
| 5392 | ASSERT_TRUE(SSL_set_ex_data(server.get(), ssl_test_ticket_aead_get_ex_index(), |
| 5393 | state)); |
| 5394 |
| 5395 | SSL_set_session(client.get(), session); |
| 5396 |
| 5397 | BIO *bio1, *bio2; |
| 5398 | ASSERT_TRUE(BIO_new_bio_pair(&bio1, 0, &bio2, 0)); |
| 5399 |
| 5400 | // SSL_set_bio takes ownership. |
| 5401 | SSL_set_bio(client.get(), bio1, bio1); |
| 5402 | SSL_set_bio(server.get(), bio2, bio2); |
| 5403 |
| 5404 | if (CompleteHandshakes(client.get(), server.get())) { |
| 5405 | *out_client = std::move(client); |
| 5406 | *out_server = std::move(server); |
| 5407 | } else { |
| 5408 | out_client->reset(); |
| 5409 | out_server->reset(); |
| 5410 | } |
| 5411 | } |
| 5412 |
| 5413 | using TicketAEADMethodParam = |
| 5414 | testing::tuple<uint16_t, unsigned, ssl_test_ticket_aead_failure_mode>; |
| 5415 |
| 5416 | class TicketAEADMethodTest |
| 5417 | : public ::testing::TestWithParam<TicketAEADMethodParam> {}; |
| 5418 |
| 5419 | TEST_P(TicketAEADMethodTest, Resume) { |
| 5420 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 5421 | CreateContextWithTestCertificate(TLS_method()); |
| 5422 | ASSERT_TRUE(server_ctx); |
| 5423 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 5424 | ASSERT_TRUE(client_ctx); |
| 5425 |
| 5426 | const uint16_t version = testing::get<0>(GetParam()); |
| 5427 | const unsigned retry_count = testing::get<1>(GetParam()); |
| 5428 | const ssl_test_ticket_aead_failure_mode failure_mode = |
| 5429 | testing::get<2>(GetParam()); |
| 5430 |
| 5431 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), version)); |
| 5432 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), version)); |
| 5433 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), version)); |
| 5434 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), version)); |
| 5435 |
| 5436 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 5437 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 5438 | SSL_CTX_set_current_time_cb(client_ctx.get(), FrozenTimeCallback); |
| 5439 | SSL_CTX_set_current_time_cb(server_ctx.get(), FrozenTimeCallback); |
| 5440 | SSL_CTX_sess_set_new_cb(client_ctx.get(), SaveLastSession); |
| 5441 |
| 5442 | SSL_CTX_set_ticket_aead_method(server_ctx.get(), &kSSLTestTicketMethod); |
| 5443 |
| 5444 | bssl::UniquePtr<SSL> client, server; |
| 5445 | ASSERT_NO_FATAL_FAILURE(ConnectClientAndServerWithTicketMethod( |
| 5446 | &client, &server, client_ctx.get(), server_ctx.get(), retry_count, |
| 5447 | failure_mode, nullptr)); |
| 5448 | switch (failure_mode) { |
| 5449 | case ssl_test_ticket_aead_ok: |
| 5450 | case ssl_test_ticket_aead_open_hard_fail: |
| 5451 | case ssl_test_ticket_aead_open_soft_fail: |
| 5452 | ASSERT_TRUE(client); |
| 5453 | break; |
| 5454 | case ssl_test_ticket_aead_seal_fail: |
| 5455 | EXPECT_FALSE(client); |
| 5456 | return; |
| 5457 | } |
| 5458 | EXPECT_FALSE(SSL_session_reused(client.get())); |
| 5459 | EXPECT_FALSE(SSL_session_reused(server.get())); |
| 5460 |
| 5461 | ASSERT_TRUE(FlushNewSessionTickets(client.get(), server.get())); |
| 5462 | bssl::UniquePtr<SSL_SESSION> session = std::move(g_last_session); |
| 5463 | ASSERT_NO_FATAL_FAILURE(ConnectClientAndServerWithTicketMethod( |
| 5464 | &client, &server, client_ctx.get(), server_ctx.get(), retry_count, |
| 5465 | failure_mode, session.get())); |
| 5466 | switch (failure_mode) { |
| 5467 | case ssl_test_ticket_aead_ok: |
| 5468 | ASSERT_TRUE(client); |
| 5469 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 5470 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 5471 | break; |
| 5472 | case ssl_test_ticket_aead_seal_fail: |
| 5473 | abort(); |
| 5474 | break; |
| 5475 | case ssl_test_ticket_aead_open_hard_fail: |
| 5476 | EXPECT_FALSE(client); |
| 5477 | break; |
| 5478 | case ssl_test_ticket_aead_open_soft_fail: |
| 5479 | ASSERT_TRUE(client); |
| 5480 | EXPECT_FALSE(SSL_session_reused(client.get())); |
| 5481 | EXPECT_FALSE(SSL_session_reused(server.get())); |
| 5482 | } |
| 5483 | } |
| 5484 |
| 5485 | std::string TicketAEADMethodParamToString( |
| 5486 | const testing::TestParamInfo<TicketAEADMethodParam> ¶ms) { |
| 5487 | std::string ret = GetVersionName(std::get<0>(params.param)); |
| 5488 | // GTest only allows alphanumeric characters and '_' in the parameter |
| 5489 | // string. Additionally filter out the 'v' to get "TLS13" over "TLSv13". |
| 5490 | for (auto it = ret.begin(); it != ret.end();) { |
| 5491 | if (*it == '.' || *it == 'v') { |
| 5492 | it = ret.erase(it); |
| 5493 | } else { |
| 5494 | ++it; |
| 5495 | } |
| 5496 | } |
| 5497 | char retry_count[256]; |
| 5498 | snprintf(retry_count, sizeof(retry_count), "%u", std::get<1>(params.param)); |
| 5499 | ret += "_"; |
| 5500 | ret += retry_count; |
| 5501 | ret += "Retries_"; |
| 5502 | switch (std::get<2>(params.param)) { |
| 5503 | case ssl_test_ticket_aead_ok: |
| 5504 | ret += "OK"; |
| 5505 | break; |
| 5506 | case ssl_test_ticket_aead_seal_fail: |
| 5507 | ret += "SealFail"; |
| 5508 | break; |
| 5509 | case ssl_test_ticket_aead_open_soft_fail: |
| 5510 | ret += "OpenSoftFail"; |
| 5511 | break; |
| 5512 | case ssl_test_ticket_aead_open_hard_fail: |
| 5513 | ret += "OpenHardFail"; |
| 5514 | break; |
| 5515 | } |
| 5516 | return ret; |
| 5517 | } |
| 5518 |
| 5519 | INSTANTIATE_TEST_SUITE_P( |
| 5520 | TicketAEADMethodTests, TicketAEADMethodTest, |
| 5521 | testing::Combine(testing::Values(TLS1_2_VERSION, TLS1_3_VERSION), |
| 5522 | testing::Values(0, 1, 2), |
| 5523 | testing::Values(ssl_test_ticket_aead_ok, |
| 5524 | ssl_test_ticket_aead_seal_fail, |
| 5525 | ssl_test_ticket_aead_open_soft_fail, |
| 5526 | ssl_test_ticket_aead_open_hard_fail)), |
| 5527 | TicketAEADMethodParamToString); |
| 5528 |
| 5529 | TEST(SSLTest, SelectNextProto) { |
| 5530 | uint8_t *result; |
| 5531 | uint8_t result_len; |
| 5532 |
| 5533 | // If there is an overlap, it should be returned. |
| 5534 | EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 5535 | SSL_select_next_proto(&result, &result_len, |
| 5536 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5537 | (const uint8_t *)"\1x\1y\1a\1z", 8)); |
| 5538 | EXPECT_EQ(Bytes("a"), Bytes(result, result_len)); |
| 5539 |
| 5540 | EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 5541 | SSL_select_next_proto(&result, &result_len, |
| 5542 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5543 | (const uint8_t *)"\1x\1y\2bb\1z", 9)); |
| 5544 | EXPECT_EQ(Bytes("bb"), Bytes(result, result_len)); |
| 5545 |
| 5546 | EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 5547 | SSL_select_next_proto(&result, &result_len, |
| 5548 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5549 | (const uint8_t *)"\1x\1y\3ccc\1z", 10)); |
| 5550 | EXPECT_EQ(Bytes("ccc"), Bytes(result, result_len)); |
| 5551 |
| 5552 | // Peer preference order takes precedence over local. |
| 5553 | EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 5554 | SSL_select_next_proto(&result, &result_len, |
| 5555 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5556 | (const uint8_t *)"\3ccc\2bb\1a", 9)); |
| 5557 | EXPECT_EQ(Bytes("a"), Bytes(result, result_len)); |
| 5558 |
| 5559 | // If there is no overlap, opportunistically select the first local protocol. |
| 5560 | // ALPN callers should ignore this, but NPN callers may use this per |
| 5561 | // draft-agl-tls-nextprotoneg-03, section 6. |
| 5562 | EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 5563 | SSL_select_next_proto(&result, &result_len, |
| 5564 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5565 | (const uint8_t *)"\1x\2yy\3zzz", 9)); |
| 5566 | EXPECT_EQ(Bytes("x"), Bytes(result, result_len)); |
| 5567 |
| 5568 | // The peer preference order may be empty in NPN. This should be treated as no |
| 5569 | // overlap and continue to select an opportunistic protocol. |
| 5570 | EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 5571 | SSL_select_next_proto(&result, &result_len, nullptr, 0, |
| 5572 | (const uint8_t *)"\1x\2yy\3zzz", 9)); |
| 5573 | EXPECT_EQ(Bytes("x"), Bytes(result, result_len)); |
| 5574 |
| 5575 | // Although calling this function with no local protocols is a caller error, |
| 5576 | // it should cleanly return an empty protocol. |
| 5577 | EXPECT_EQ( |
| 5578 | OPENSSL_NPN_NO_OVERLAP, |
| 5579 | SSL_select_next_proto(&result, &result_len, |
| 5580 | (const uint8_t *)"\1a\2bb\3ccc", 9, nullptr, 0)); |
| 5581 | EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 5582 |
| 5583 | // Syntax errors are similarly caller errors. |
| 5584 | EXPECT_EQ( |
| 5585 | OPENSSL_NPN_NO_OVERLAP, |
| 5586 | SSL_select_next_proto(&result, &result_len, (const uint8_t *)"\4aaa", 4, |
| 5587 | (const uint8_t *)"\1a\2bb\3ccc", 9)); |
| 5588 | EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 5589 | EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 5590 | SSL_select_next_proto(&result, &result_len, |
| 5591 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5592 | (const uint8_t *)"\4aaa", 4)); |
| 5593 | EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 5594 |
| 5595 | // Protocols in protocol lists may not be empty. |
| 5596 | EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 5597 | SSL_select_next_proto(&result, &result_len, |
| 5598 | (const uint8_t *)"\0\2bb\3ccc", 8, |
| 5599 | (const uint8_t *)"\1a\2bb\3ccc", 9)); |
| 5600 | EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 5601 | SSL_select_next_proto(&result, &result_len, |
| 5602 | (const uint8_t *)"\1a\2bb\3ccc", 9, |
| 5603 | (const uint8_t *)"\0\2bb\3ccc", 8)); |
| 5604 | EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 5605 | } |
| 5606 |
| 5607 | // The client should gracefully handle no suitable ciphers being enabled. |
| 5608 | TEST(SSLTest, NoCiphersAvailable) { |
| 5609 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 5610 | ASSERT_TRUE(ctx); |
| 5611 |
| 5612 | // Configure |client_ctx| with a cipher list that does not intersect with its |
| 5613 | // version configuration. |
| 5614 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 5615 | ctx.get(), "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); |
| 5616 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_1_VERSION)); |
| 5617 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_1_VERSION)); |
| 5618 |
| 5619 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 5620 | ASSERT_TRUE(ssl); |
| 5621 | SSL_set_connect_state(ssl.get()); |
| 5622 |
| 5623 | UniquePtr<BIO> rbio(BIO_new(BIO_s_mem())), wbio(BIO_new(BIO_s_mem())); |
| 5624 | ASSERT_TRUE(rbio); |
| 5625 | ASSERT_TRUE(wbio); |
| 5626 | SSL_set0_rbio(ssl.get(), rbio.release()); |
| 5627 | SSL_set0_wbio(ssl.get(), wbio.release()); |
| 5628 |
| 5629 | int ret = SSL_do_handshake(ssl.get()); |
| 5630 | EXPECT_EQ(-1, ret); |
| 5631 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(ssl.get(), ret)); |
| 5632 | EXPECT_TRUE( |
| 5633 | ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_CIPHERS_AVAILABLE)); |
| 5634 | } |
| 5635 |
| 5636 | TEST_P(SSLVersionTest, SessionVersion) { |
| 5637 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 5638 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 5639 |
| 5640 | bssl::UniquePtr<SSL_SESSION> session = |
| 5641 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 5642 | ASSERT_TRUE(session); |
| 5643 | EXPECT_EQ(version(), SSL_SESSION_get_protocol_version(session.get())); |
| 5644 |
| 5645 | // Sessions in TLS 1.3 and later should be single-use. |
| 5646 | EXPECT_EQ(is_tls13(), !!SSL_SESSION_should_be_single_use(session.get())); |
| 5647 |
| 5648 | // Making fake sessions for testing works. |
| 5649 | session.reset(SSL_SESSION_new(client_ctx_.get())); |
| 5650 | ASSERT_TRUE(session); |
| 5651 | ASSERT_TRUE(SSL_SESSION_set_protocol_version(session.get(), version())); |
| 5652 | EXPECT_EQ(version(), SSL_SESSION_get_protocol_version(session.get())); |
| 5653 | } |
| 5654 |
| 5655 | TEST_P(SSLVersionTest, SSLPending) { |
| 5656 | UniquePtr<SSL> ssl(SSL_new(client_ctx_.get())); |
| 5657 | ASSERT_TRUE(ssl); |
| 5658 | EXPECT_EQ(0, SSL_pending(ssl.get())); |
| 5659 |
| 5660 | ASSERT_TRUE(Connect()); |
| 5661 | EXPECT_EQ(0, SSL_pending(client_.get())); |
| 5662 | EXPECT_EQ(0, SSL_has_pending(client_.get())); |
| 5663 |
| 5664 | ASSERT_EQ(5, SSL_write(server_.get(), "hello", 5)); |
| 5665 | ASSERT_EQ(5, SSL_write(server_.get(), "world", 5)); |
| 5666 | EXPECT_EQ(0, SSL_pending(client_.get())); |
| 5667 | EXPECT_EQ(0, SSL_has_pending(client_.get())); |
| 5668 |
| 5669 | char buf[10]; |
| 5670 | ASSERT_EQ(1, SSL_peek(client_.get(), buf, 1)); |
| 5671 | EXPECT_EQ(5, SSL_pending(client_.get())); |
| 5672 | EXPECT_EQ(1, SSL_has_pending(client_.get())); |
| 5673 |
| 5674 | ASSERT_EQ(1, SSL_read(client_.get(), buf, 1)); |
| 5675 | EXPECT_EQ(4, SSL_pending(client_.get())); |
| 5676 | EXPECT_EQ(1, SSL_has_pending(client_.get())); |
| 5677 |
| 5678 | ASSERT_EQ(4, SSL_read(client_.get(), buf, 10)); |
| 5679 | EXPECT_EQ(0, SSL_pending(client_.get())); |
| 5680 | if (is_dtls()) { |
| 5681 | // In DTLS, the two records would have been read as a single datagram and |
| 5682 | // buffered inside |client_|. Thus, |SSL_has_pending| should return true. |
| 5683 | // |
| 5684 | // This test is slightly unrealistic. It relies on |ConnectClientAndServer| |
| 5685 | // using a |BIO| pair, which does not preserve datagram boundaries. Reading |
| 5686 | // 1 byte, then 4 bytes, from the first record also relies on |
| 5687 | // https://crbug.com/boringssl/65. But it does test the codepaths. When |
| 5688 | // fixing either of these bugs, this test may need to be redone. |
| 5689 | EXPECT_EQ(1, SSL_has_pending(client_.get())); |
| 5690 | } else { |
| 5691 | // In TLS, we do not overread, so |SSL_has_pending| should report no data is |
| 5692 | // buffered. |
| 5693 | EXPECT_EQ(0, SSL_has_pending(client_.get())); |
| 5694 | } |
| 5695 |
| 5696 | ASSERT_EQ(2, SSL_read(client_.get(), buf, 2)); |
| 5697 | EXPECT_EQ(3, SSL_pending(client_.get())); |
| 5698 | EXPECT_EQ(1, SSL_has_pending(client_.get())); |
| 5699 | } |
| 5700 |
| 5701 | // Test that post-handshake tickets consumed by |SSL_shutdown| are ignored. |
| 5702 | TEST(SSLTest, ShutdownIgnoresTickets) { |
| 5703 | bssl::UniquePtr<SSL_CTX> ctx(CreateContextWithTestCertificate(TLS_method())); |
| 5704 | ASSERT_TRUE(ctx); |
| 5705 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 5706 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 5707 |
| 5708 | SSL_CTX_set_session_cache_mode(ctx.get(), SSL_SESS_CACHE_BOTH); |
| 5709 |
| 5710 | bssl::UniquePtr<SSL> client, server; |
| 5711 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 5712 |
| 5713 | SSL_CTX_sess_set_new_cb(ctx.get(), [](SSL *ssl, SSL_SESSION *session) -> int { |
| 5714 | ADD_FAILURE() << "New session callback called during SSL_shutdown"; |
| 5715 | return 0; |
| 5716 | }); |
| 5717 |
| 5718 | // Send close_notify. |
| 5719 | EXPECT_EQ(0, SSL_shutdown(server.get())); |
| 5720 | EXPECT_EQ(0, SSL_shutdown(client.get())); |
| 5721 |
| 5722 | // Receive close_notify. |
| 5723 | EXPECT_EQ(1, SSL_shutdown(server.get())); |
| 5724 | EXPECT_EQ(1, SSL_shutdown(client.get())); |
| 5725 | } |
| 5726 |
| 5727 | TEST(SSLTest, SignatureAlgorithmProperties) { |
| 5728 | EXPECT_EQ(EVP_PKEY_NONE, SSL_get_signature_algorithm_key_type(0x1234)); |
| 5729 | EXPECT_EQ(nullptr, SSL_get_signature_algorithm_digest(0x1234)); |
| 5730 | EXPECT_FALSE(SSL_is_signature_algorithm_rsa_pss(0x1234)); |
| 5731 |
| 5732 | EXPECT_EQ(EVP_PKEY_RSA, |
| 5733 | SSL_get_signature_algorithm_key_type(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 5734 | EXPECT_EQ(EVP_md5_sha1(), |
| 5735 | SSL_get_signature_algorithm_digest(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 5736 | EXPECT_FALSE(SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 5737 |
| 5738 | EXPECT_EQ(EVP_PKEY_EC, SSL_get_signature_algorithm_key_type( |
| 5739 | SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 5740 | EXPECT_EQ(EVP_sha256(), SSL_get_signature_algorithm_digest( |
| 5741 | SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 5742 | EXPECT_FALSE( |
| 5743 | SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 5744 |
| 5745 | EXPECT_EQ(EVP_PKEY_RSA, |
| 5746 | SSL_get_signature_algorithm_key_type(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 5747 | EXPECT_EQ(EVP_sha384(), |
| 5748 | SSL_get_signature_algorithm_digest(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 5749 | EXPECT_TRUE(SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 5750 | } |
| 5751 |
| 5752 | static int XORCompressFunc(SSL *ssl, CBB *out, const uint8_t *in, |
| 5753 | size_t in_len) { |
| 5754 | for (size_t i = 0; i < in_len; i++) { |
| 5755 | if (!CBB_add_u8(out, in[i] ^ 0x55)) { |
| 5756 | return 0; |
| 5757 | } |
| 5758 | } |
| 5759 |
| 5760 | SSL_set_app_data(ssl, XORCompressFunc); |
| 5761 |
| 5762 | return 1; |
| 5763 | } |
| 5764 |
| 5765 | static int XORDecompressFunc(SSL *ssl, CRYPTO_BUFFER **out, |
| 5766 | size_t uncompressed_len, const uint8_t *in, |
| 5767 | size_t in_len) { |
| 5768 | if (in_len != uncompressed_len) { |
| 5769 | return 0; |
| 5770 | } |
| 5771 |
| 5772 | uint8_t *data; |
| 5773 | *out = CRYPTO_BUFFER_alloc(&data, uncompressed_len); |
| 5774 | if (*out == nullptr) { |
| 5775 | return 0; |
| 5776 | } |
| 5777 |
| 5778 | for (size_t i = 0; i < in_len; i++) { |
| 5779 | data[i] = in[i] ^ 0x55; |
| 5780 | } |
| 5781 |
| 5782 | SSL_set_app_data(ssl, XORDecompressFunc); |
| 5783 |
| 5784 | return 1; |
| 5785 | } |
| 5786 |
| 5787 | TEST(SSLTest, CertCompression) { |
| 5788 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 5789 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 5790 | CreateContextWithTestCertificate(TLS_method())); |
| 5791 | ASSERT_TRUE(client_ctx); |
| 5792 | ASSERT_TRUE(server_ctx); |
| 5793 |
| 5794 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 5795 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 5796 | ASSERT_TRUE(SSL_CTX_add_cert_compression_alg( |
| 5797 | client_ctx.get(), 0x1234, XORCompressFunc, XORDecompressFunc)); |
| 5798 | ASSERT_TRUE(SSL_CTX_add_cert_compression_alg( |
| 5799 | server_ctx.get(), 0x1234, XORCompressFunc, XORDecompressFunc)); |
| 5800 |
| 5801 | bssl::UniquePtr<SSL> client, server; |
| 5802 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 5803 | server_ctx.get())); |
| 5804 |
| 5805 | EXPECT_TRUE(SSL_get_app_data(client.get()) == XORDecompressFunc); |
| 5806 | EXPECT_TRUE(SSL_get_app_data(server.get()) == XORCompressFunc); |
| 5807 | } |
| 5808 |
| 5809 | void MoveBIOs(SSL *dest, SSL *src) { |
| 5810 | BIO *rbio = SSL_get_rbio(src); |
| 5811 | BIO_up_ref(rbio); |
| 5812 | SSL_set0_rbio(dest, rbio); |
| 5813 |
| 5814 | BIO *wbio = SSL_get_wbio(src); |
| 5815 | BIO_up_ref(wbio); |
| 5816 | SSL_set0_wbio(dest, wbio); |
| 5817 |
| 5818 | SSL_set0_rbio(src, nullptr); |
| 5819 | SSL_set0_wbio(src, nullptr); |
| 5820 | } |
| 5821 |
| 5822 | void VerifyHandoff(bool use_new_alps_codepoint) { |
| 5823 | static const uint8_t alpn[] = {0x03, 'f', 'o', 'o'}; |
| 5824 | static const uint8_t proto[] = {'f', 'o', 'o'}; |
| 5825 | static const uint8_t alps[] = {0x04, 'a', 'l', 'p', 's'}; |
| 5826 |
| 5827 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 5828 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 5829 | bssl::UniquePtr<SSL_CTX> handshaker_ctx( |
| 5830 | CreateContextWithTestCertificate(TLS_method())); |
| 5831 | ASSERT_TRUE(client_ctx); |
| 5832 | ASSERT_TRUE(server_ctx); |
| 5833 | ASSERT_TRUE(handshaker_ctx); |
| 5834 |
| 5835 | if (!use_new_alps_codepoint) { |
| 5836 | SetUpExpectedOldCodePoint(server_ctx.get()); |
| 5837 | } else { |
| 5838 | SetUpExpectedNewCodePoint(server_ctx.get()); |
| 5839 | } |
| 5840 |
| 5841 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_CLIENT); |
| 5842 | SSL_CTX_sess_set_new_cb(client_ctx.get(), SaveLastSession); |
| 5843 | SSL_CTX_set_handoff_mode(server_ctx.get(), true); |
| 5844 | uint8_t keys[48]; |
| 5845 | SSL_CTX_get_tlsext_ticket_keys(server_ctx.get(), &keys, sizeof(keys)); |
| 5846 | SSL_CTX_set_tlsext_ticket_keys(handshaker_ctx.get(), &keys, sizeof(keys)); |
| 5847 |
| 5848 | for (bool early_data : {false, true}) { |
| 5849 | SCOPED_TRACE(early_data); |
| 5850 | for (bool is_resume : {false, true}) { |
| 5851 | SCOPED_TRACE(is_resume); |
| 5852 | bssl::UniquePtr<SSL> client, server; |
| 5853 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 5854 | server_ctx.get())); |
| 5855 | SSL_set_early_data_enabled(client.get(), early_data); |
| 5856 |
| 5857 | // Set up client ALPS settings. |
| 5858 | SSL_set_alps_use_new_codepoint(client.get(), use_new_alps_codepoint); |
| 5859 | ASSERT_TRUE(SSL_set_alpn_protos(client.get(), alpn, sizeof(alpn)) == 0); |
| 5860 | ASSERT_TRUE(SSL_add_application_settings(client.get(), proto, |
| 5861 | sizeof(proto), nullptr, 0)); |
| 5862 | if (is_resume) { |
| 5863 | ASSERT_TRUE(g_last_session); |
| 5864 | SSL_set_session(client.get(), g_last_session.get()); |
| 5865 | if (early_data) { |
| 5866 | EXPECT_GT(g_last_session->ticket_max_early_data, 0u); |
| 5867 | } |
| 5868 | } |
| 5869 |
| 5870 |
| 5871 | int client_ret = SSL_do_handshake(client.get()); |
| 5872 | int client_err = SSL_get_error(client.get(), client_ret); |
| 5873 |
| 5874 | uint8_t byte_written; |
| 5875 | if (early_data && is_resume) { |
| 5876 | ASSERT_EQ(client_err, 0); |
| 5877 | EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 5878 | // Attempt to write early data. |
| 5879 | byte_written = 43; |
| 5880 | EXPECT_EQ(SSL_write(client.get(), &byte_written, 1), 1); |
| 5881 | } else { |
| 5882 | ASSERT_EQ(client_err, SSL_ERROR_WANT_READ); |
| 5883 | } |
| 5884 |
| 5885 | int server_ret = SSL_do_handshake(server.get()); |
| 5886 | int server_err = SSL_get_error(server.get(), server_ret); |
| 5887 | ASSERT_EQ(server_err, SSL_ERROR_HANDOFF); |
| 5888 |
| 5889 | ScopedCBB cbb; |
| 5890 | Array<uint8_t> handoff; |
| 5891 | SSL_CLIENT_HELLO hello; |
| 5892 | ASSERT_TRUE(CBB_init(cbb.get(), 256)); |
| 5893 | ASSERT_TRUE(SSL_serialize_handoff(server.get(), cbb.get(), &hello)); |
| 5894 | ASSERT_TRUE(CBBFinishArray(cbb.get(), &handoff)); |
| 5895 |
| 5896 | bssl::UniquePtr<SSL> handshaker(SSL_new(handshaker_ctx.get())); |
| 5897 | ASSERT_TRUE(handshaker); |
| 5898 | // Note split handshakes determines 0-RTT support, for both the current |
| 5899 | // handshake and newly-issued tickets, entirely by |handshaker|. There is |
| 5900 | // no need to call |SSL_set_early_data_enabled| on |server|. |
| 5901 | SSL_set_early_data_enabled(handshaker.get(), 1); |
| 5902 |
| 5903 | // Set up handshaker ALPS settings. |
| 5904 | SSL_set_alps_use_new_codepoint(handshaker.get(), use_new_alps_codepoint); |
| 5905 | SSL_CTX_set_alpn_select_cb( |
| 5906 | handshaker_ctx.get(), |
| 5907 | [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 5908 | unsigned in_len, void *arg) -> int { |
| 5909 | return SSL_select_next_proto(const_cast<uint8_t **>(out), out_len, |
| 5910 | in, in_len, alpn, |
| 5911 | sizeof(alpn)) == OPENSSL_NPN_NEGOTIATED |
| 5912 | ? SSL_TLSEXT_ERR_OK |
| 5913 | : SSL_TLSEXT_ERR_NOACK; |
| 5914 | }, |
| 5915 | nullptr); |
| 5916 | ASSERT_TRUE(SSL_add_application_settings( |
| 5917 | handshaker.get(), proto, sizeof(proto), alps, sizeof(alps))); |
| 5918 |
| 5919 | ASSERT_TRUE(SSL_apply_handoff(handshaker.get(), handoff)); |
| 5920 |
| 5921 | MoveBIOs(handshaker.get(), server.get()); |
| 5922 |
| 5923 | int handshake_ret = SSL_do_handshake(handshaker.get()); |
| 5924 | int handshake_err = SSL_get_error(handshaker.get(), handshake_ret); |
| 5925 | ASSERT_EQ(handshake_err, SSL_ERROR_HANDBACK); |
| 5926 |
| 5927 | // Double-check that additional calls to |SSL_do_handshake| continue |
| 5928 | // to get |SSL_ERROR_HANDBACK|. |
| 5929 | handshake_ret = SSL_do_handshake(handshaker.get()); |
| 5930 | handshake_err = SSL_get_error(handshaker.get(), handshake_ret); |
| 5931 | ASSERT_EQ(handshake_err, SSL_ERROR_HANDBACK); |
| 5932 |
| 5933 | ScopedCBB cbb_handback; |
| 5934 | Array<uint8_t> handback; |
| 5935 | ASSERT_TRUE(CBB_init(cbb_handback.get(), 1024)); |
| 5936 | ASSERT_TRUE(SSL_serialize_handback(handshaker.get(), cbb_handback.get())); |
| 5937 | ASSERT_TRUE(CBBFinishArray(cbb_handback.get(), &handback)); |
| 5938 |
| 5939 | bssl::UniquePtr<SSL> server2(SSL_new(server_ctx.get())); |
| 5940 | ASSERT_TRUE(server2); |
| 5941 | ASSERT_TRUE(SSL_apply_handback(server2.get(), handback)); |
| 5942 |
| 5943 | MoveBIOs(server2.get(), handshaker.get()); |
| 5944 | ASSERT_TRUE(CompleteHandshakes(client.get(), server2.get())); |
| 5945 | EXPECT_EQ(is_resume, SSL_session_reused(client.get())); |
| 5946 | // Verify application settings. |
| 5947 | ASSERT_TRUE(SSL_has_application_settings(client.get())); |
| 5948 |
| 5949 | if (early_data && is_resume) { |
| 5950 | // In this case, one byte of early data has already been written above. |
| 5951 | EXPECT_TRUE(SSL_early_data_accepted(client.get())); |
| 5952 | } else { |
| 5953 | byte_written = 42; |
| 5954 | EXPECT_EQ(SSL_write(client.get(), &byte_written, 1), 1); |
| 5955 | } |
| 5956 | uint8_t byte; |
| 5957 | EXPECT_EQ(SSL_read(server2.get(), &byte, 1), 1); |
| 5958 | EXPECT_EQ(byte_written, byte); |
| 5959 |
| 5960 | byte = 44; |
| 5961 | EXPECT_EQ(SSL_write(server2.get(), &byte, 1), 1); |
| 5962 | EXPECT_EQ(SSL_read(client.get(), &byte, 1), 1); |
| 5963 | EXPECT_EQ(44, byte); |
| 5964 | } |
| 5965 | } |
| 5966 | } |
| 5967 |
| 5968 | TEST(SSLTest, Handoff) { |
| 5969 | for (bool use_new_alps_codepoint : {false, true}) { |
| 5970 | SCOPED_TRACE(use_new_alps_codepoint); |
| 5971 | VerifyHandoff(use_new_alps_codepoint); |
| 5972 | } |
| 5973 | } |
| 5974 |
| 5975 | TEST(SSLTest, HandoffDeclined) { |
| 5976 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 5977 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 5978 | CreateContextWithTestCertificate(TLS_method())); |
| 5979 | ASSERT_TRUE(client_ctx); |
| 5980 | ASSERT_TRUE(server_ctx); |
| 5981 |
| 5982 | SSL_CTX_set_handoff_mode(server_ctx.get(), true); |
| 5983 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 5984 |
| 5985 | bssl::UniquePtr<SSL> client, server; |
| 5986 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 5987 | server_ctx.get())); |
| 5988 |
| 5989 | int client_ret = SSL_do_handshake(client.get()); |
| 5990 | int client_err = SSL_get_error(client.get(), client_ret); |
| 5991 | ASSERT_EQ(client_err, SSL_ERROR_WANT_READ); |
| 5992 |
| 5993 | int server_ret = SSL_do_handshake(server.get()); |
| 5994 | int server_err = SSL_get_error(server.get(), server_ret); |
| 5995 | ASSERT_EQ(server_err, SSL_ERROR_HANDOFF); |
| 5996 |
| 5997 | ScopedCBB cbb; |
| 5998 | SSL_CLIENT_HELLO hello; |
| 5999 | ASSERT_TRUE(CBB_init(cbb.get(), 256)); |
| 6000 | ASSERT_TRUE(SSL_serialize_handoff(server.get(), cbb.get(), &hello)); |
| 6001 |
| 6002 | ASSERT_TRUE(SSL_decline_handoff(server.get())); |
| 6003 |
| 6004 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 6005 |
| 6006 | uint8_t byte = 42; |
| 6007 | EXPECT_EQ(SSL_write(client.get(), &byte, 1), 1); |
| 6008 | EXPECT_EQ(SSL_read(server.get(), &byte, 1), 1); |
| 6009 | EXPECT_EQ(42, byte); |
| 6010 |
| 6011 | byte = 43; |
| 6012 | EXPECT_EQ(SSL_write(server.get(), &byte, 1), 1); |
| 6013 | EXPECT_EQ(SSL_read(client.get(), &byte, 1), 1); |
| 6014 | EXPECT_EQ(43, byte); |
| 6015 | } |
| 6016 |
| 6017 | static std::string SigAlgsToString(Span<const uint16_t> sigalgs) { |
| 6018 | std::string ret = "{"; |
| 6019 |
| 6020 | for (uint16_t v : sigalgs) { |
| 6021 | if (ret.size() > 1) { |
| 6022 | ret += ", "; |
| 6023 | } |
| 6024 |
| 6025 | char buf[8]; |
| 6026 | snprintf(buf, sizeof(buf) - 1, "0x%02x", v); |
| 6027 | buf[sizeof(buf) - 1] = 0; |
| 6028 | ret += std::string(buf); |
| 6029 | } |
| 6030 |
| 6031 | ret += "}"; |
| 6032 | return ret; |
| 6033 | } |
| 6034 |
| 6035 | void ExpectSigAlgsEqual(Span<const uint16_t> expected, |
| 6036 | Span<const uint16_t> actual) { |
| 6037 | bool matches = false; |
| 6038 | if (expected.size() == actual.size()) { |
| 6039 | matches = true; |
| 6040 |
| 6041 | for (size_t i = 0; i < expected.size(); i++) { |
| 6042 | if (expected[i] != actual[i]) { |
| 6043 | matches = false; |
| 6044 | break; |
| 6045 | } |
| 6046 | } |
| 6047 | } |
| 6048 |
| 6049 | if (!matches) { |
| 6050 | ADD_FAILURE() << "expected: " << SigAlgsToString(expected) |
| 6051 | << " got: " << SigAlgsToString(actual); |
| 6052 | } |
| 6053 | } |
| 6054 |
| 6055 | TEST(SSLTest, SigAlgs) { |
| 6056 | static const struct { |
| 6057 | std::vector<int> input; |
| 6058 | bool ok; |
| 6059 | std::vector<uint16_t> expected; |
| 6060 | } kTests[] = { |
| 6061 | {{}, true, {}}, |
| 6062 | {{1}, false, {}}, |
| 6063 | {{1, 2, 3}, false, {}}, |
| 6064 | {{NID_sha256, EVP_PKEY_ED25519}, false, {}}, |
| 6065 | {{NID_sha256, EVP_PKEY_RSA, NID_sha256, EVP_PKEY_RSA}, false, {}}, |
| 6066 |
| 6067 | {{NID_sha256, EVP_PKEY_RSA}, true, {SSL_SIGN_RSA_PKCS1_SHA256}}, |
| 6068 | {{NID_sha512, EVP_PKEY_RSA}, true, {SSL_SIGN_RSA_PKCS1_SHA512}}, |
| 6069 | {{NID_sha256, EVP_PKEY_RSA_PSS}, true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 6070 | {{NID_undef, EVP_PKEY_ED25519}, true, {SSL_SIGN_ED25519}}, |
| 6071 | {{NID_undef, EVP_PKEY_ED25519, NID_sha384, EVP_PKEY_EC}, |
| 6072 | true, |
| 6073 | {SSL_SIGN_ED25519, SSL_SIGN_ECDSA_SECP384R1_SHA384}}, |
| 6074 | }; |
| 6075 |
| 6076 | UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 6077 | ASSERT_TRUE(ctx); |
| 6078 |
| 6079 | unsigned n = 1; |
| 6080 | for (const auto &test : kTests) { |
| 6081 | SCOPED_TRACE(n++); |
| 6082 |
| 6083 | const bool ok = |
| 6084 | SSL_CTX_set1_sigalgs(ctx.get(), test.input.data(), test.input.size()); |
| 6085 | EXPECT_EQ(ok, test.ok); |
| 6086 |
| 6087 | if (!ok) { |
| 6088 | ERR_clear_error(); |
| 6089 | } |
| 6090 |
| 6091 | if (!test.ok) { |
| 6092 | continue; |
| 6093 | } |
| 6094 |
| 6095 | ExpectSigAlgsEqual(test.expected, ctx->cert->legacy_credential->sigalgs); |
| 6096 | } |
| 6097 | } |
| 6098 |
| 6099 | TEST(SSLTest, SigAlgsList) { |
| 6100 | static const struct { |
| 6101 | const char *input; |
| 6102 | bool ok; |
| 6103 | std::vector<uint16_t> expected; |
| 6104 | } kTests[] = { |
| 6105 | {"", false, {}}, |
| 6106 | {":", false, {}}, |
| 6107 | {"+", false, {}}, |
| 6108 | {"RSA", false, {}}, |
| 6109 | {"RSA+", false, {}}, |
| 6110 | {"RSA+SHA256:", false, {}}, |
| 6111 | {":RSA+SHA256:", false, {}}, |
| 6112 | {":RSA+SHA256+:", false, {}}, |
| 6113 | {"!", false, {}}, |
| 6114 | {"\x01", false, {}}, |
| 6115 | {"RSA+SHA256:RSA+SHA384:RSA+SHA256", false, {}}, |
| 6116 | {"RSA-PSS+SHA256:rsa_pss_rsae_sha256", false, {}}, |
| 6117 |
| 6118 | {"RSA+SHA256", true, {SSL_SIGN_RSA_PKCS1_SHA256}}, |
| 6119 | {"RSA+SHA256:ed25519", |
| 6120 | true, |
| 6121 | {SSL_SIGN_RSA_PKCS1_SHA256, SSL_SIGN_ED25519}}, |
| 6122 | {"ECDSA+SHA256:RSA+SHA512", |
| 6123 | true, |
| 6124 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, SSL_SIGN_RSA_PKCS1_SHA512}}, |
| 6125 | {"ecdsa_secp256r1_sha256:rsa_pss_rsae_sha256", |
| 6126 | true, |
| 6127 | {SSL_SIGN_ECDSA_SECP256R1_SHA256, SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 6128 | {"RSA-PSS+SHA256", true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 6129 | {"PSS+SHA256", true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 6130 | }; |
| 6131 |
| 6132 | UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 6133 | ASSERT_TRUE(ctx); |
| 6134 |
| 6135 | unsigned n = 1; |
| 6136 | for (const auto &test : kTests) { |
| 6137 | SCOPED_TRACE(n++); |
| 6138 |
| 6139 | const bool ok = SSL_CTX_set1_sigalgs_list(ctx.get(), test.input); |
| 6140 | EXPECT_EQ(ok, test.ok); |
| 6141 |
| 6142 | if (!ok) { |
| 6143 | if (test.ok) { |
| 6144 | ERR_print_errors_fp(stderr); |
| 6145 | } |
| 6146 | ERR_clear_error(); |
| 6147 | } |
| 6148 |
| 6149 | if (!test.ok) { |
| 6150 | continue; |
| 6151 | } |
| 6152 |
| 6153 | ExpectSigAlgsEqual(test.expected, ctx->cert->legacy_credential->sigalgs); |
| 6154 | } |
| 6155 | } |
| 6156 |
| 6157 | TEST(SSLTest, ApplyHandoffRemovesUnsupportedCiphers) { |
| 6158 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 6159 | ASSERT_TRUE(server_ctx); |
| 6160 | bssl::UniquePtr<SSL> server(SSL_new(server_ctx.get())); |
| 6161 | ASSERT_TRUE(server); |
| 6162 |
| 6163 | // handoff is a handoff message that has been artificially modified to pretend |
| 6164 | // that only TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f) is supported. When |
| 6165 | // it is applied to |server|, all ciphers but that one should be removed. |
| 6166 | // |
| 6167 | // To make a new one of these, try sticking this in the |Handoff| test above: |
| 6168 | // |
| 6169 | // hexdump(stderr, "", handoff.data(), handoff.size()); |
| 6170 | // sed -e 's/\(..\)/0x\1, /g' |
| 6171 | // |
| 6172 | // and modify serialize_features() to emit only cipher 0x0A. |
| 6173 |
| 6174 | uint8_t handoff[] = { |
| 6175 | 0x30, 0x81, 0x9a, 0x02, 0x01, 0x00, 0x04, 0x00, 0x04, 0x81, 0x82, 0x01, |
| 6176 | 0x00, 0x00, 0x7e, 0x03, 0x03, 0x30, 0x8e, 0x8f, 0x79, 0xd2, 0x87, 0x39, |
| 6177 | 0xc2, 0x23, 0x23, 0x13, 0xca, 0x3c, 0x80, 0x44, 0xfd, 0x80, 0x83, 0x62, |
| 6178 | 0x3c, 0xcc, 0xf8, 0x76, 0xd3, 0x62, 0xbb, 0x54, 0xe3, 0xc4, 0x39, 0x24, |
| 6179 | 0xa5, 0x00, 0x00, 0x1e, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, |
| 6180 | 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, |
| 6181 | 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00, |
| 6182 | 0x00, 0x37, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 6183 | 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 6184 | 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00, |
| 6185 | 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, |
| 6186 | 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x04, 0x02, 0xc0, |
| 6187 | 0x2f, 0x04, 0x0a, 0x00, 0x15, 0x00, 0x17, 0x00, 0x18, 0x00, 0x19, 0x00, |
| 6188 | 0x1d, |
| 6189 | }; |
| 6190 |
| 6191 | EXPECT_LT(1u, sk_SSL_CIPHER_num(SSL_get_ciphers(server.get()))); |
| 6192 | ASSERT_TRUE( |
| 6193 | SSL_apply_handoff(server.get(), {handoff, OPENSSL_ARRAY_SIZE(handoff)})); |
| 6194 | EXPECT_EQ(1u, sk_SSL_CIPHER_num(SSL_get_ciphers(server.get()))); |
| 6195 | } |
| 6196 |
| 6197 | TEST(SSLTest, ApplyHandoffRemovesUnsupportedCurves) { |
| 6198 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 6199 | ASSERT_TRUE(server_ctx); |
| 6200 | bssl::UniquePtr<SSL> server(SSL_new(server_ctx.get())); |
| 6201 | ASSERT_TRUE(server); |
| 6202 |
| 6203 | // handoff is a handoff message that has been artificially modified to pretend |
| 6204 | // that only one ECDH group is supported. When it is applied to |server|, all |
| 6205 | // groups but that one should be removed. |
| 6206 | // |
| 6207 | // See |ApplyHandoffRemovesUnsupportedCiphers| for how to make a new one of |
| 6208 | // these. |
| 6209 | uint8_t handoff[] = { |
| 6210 | 0x30, 0x81, 0xc0, 0x02, 0x01, 0x00, 0x04, 0x00, 0x04, 0x81, 0x82, 0x01, |
| 6211 | 0x00, 0x00, 0x7e, 0x03, 0x03, 0x98, 0x30, 0xce, 0xd9, 0xb0, 0xdf, 0x5f, |
| 6212 | 0x82, 0x05, 0x4a, 0x43, 0x67, 0x7e, 0xdb, 0x6a, 0x4f, 0x21, 0x18, 0x4e, |
| 6213 | 0x0d, 0x94, 0x63, 0x18, 0x8b, 0x54, 0x89, 0xdb, 0x8b, 0x1d, 0x84, 0xbc, |
| 6214 | 0x09, 0x00, 0x00, 0x1e, 0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30, |
| 6215 | 0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x0a, 0xc0, 0x14, |
| 6216 | 0x00, 0x9c, 0x00, 0x9d, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x0a, 0x01, 0x00, |
| 6217 | 0x00, 0x37, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, |
| 6218 | 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, |
| 6219 | 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x0d, 0x00, |
| 6220 | 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08, |
| 6221 | 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, 0x04, 0x30, 0x00, |
| 6222 | 0x02, 0x00, 0x0a, 0x00, 0x2f, 0x00, 0x35, 0x00, 0x8c, 0x00, 0x8d, 0x00, |
| 6223 | 0x9c, 0x00, 0x9d, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x09, 0xc0, |
| 6224 | 0x0a, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x2b, 0xc0, 0x2c, 0xc0, 0x2f, 0xc0, |
| 6225 | 0x30, 0xc0, 0x35, 0xc0, 0x36, 0xcc, 0xa8, 0xcc, 0xa9, 0xcc, 0xac, 0x04, |
| 6226 | 0x02, 0x00, 0x17, |
| 6227 | }; |
| 6228 |
| 6229 | // The zero length means that the default list of groups is used. |
| 6230 | EXPECT_EQ(0u, server->config->supported_group_list.size()); |
| 6231 | ASSERT_TRUE( |
| 6232 | SSL_apply_handoff(server.get(), {handoff, OPENSSL_ARRAY_SIZE(handoff)})); |
| 6233 | EXPECT_EQ(1u, server->config->supported_group_list.size()); |
| 6234 | } |
| 6235 |
| 6236 | TEST(SSLTest, ZeroSizedWiteFlushesHandshakeMessages) { |
| 6237 | // If there are pending handshake mesages, an |SSL_write| of zero bytes should |
| 6238 | // flush them. |
| 6239 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 6240 | CreateContextWithTestCertificate(TLS_method())); |
| 6241 | ASSERT_TRUE(server_ctx); |
| 6242 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 6243 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 6244 |
| 6245 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 6246 | ASSERT_TRUE(client_ctx); |
| 6247 | EXPECT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 6248 | EXPECT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 6249 |
| 6250 | bssl::UniquePtr<SSL> client, server; |
| 6251 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 6252 | server_ctx.get())); |
| 6253 |
| 6254 | BIO *client_wbio = SSL_get_wbio(client.get()); |
| 6255 | EXPECT_EQ(0u, BIO_wpending(client_wbio)); |
| 6256 | EXPECT_TRUE(SSL_key_update(client.get(), SSL_KEY_UPDATE_NOT_REQUESTED)); |
| 6257 | EXPECT_EQ(0u, BIO_wpending(client_wbio)); |
| 6258 | EXPECT_EQ(0, SSL_write(client.get(), nullptr, 0)); |
| 6259 | EXPECT_NE(0u, BIO_wpending(client_wbio)); |
| 6260 | } |
| 6261 |
| 6262 | TEST_P(SSLVersionTest, VerifyBeforeCertRequest) { |
| 6263 | // Configure the server to request client certificates. |
| 6264 | SSL_CTX_set_custom_verify( |
| 6265 | server_ctx_.get(), SSL_VERIFY_PEER, |
| 6266 | [](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 6267 |
| 6268 | // Configure the client to reject the server certificate. |
| 6269 | SSL_CTX_set_custom_verify( |
| 6270 | client_ctx_.get(), SSL_VERIFY_PEER, |
| 6271 | [](SSL *ssl, uint8_t *out_alert) { return ssl_verify_invalid; }); |
| 6272 |
| 6273 | // cert_cb should not be called. Verification should fail first. |
| 6274 | SSL_CTX_set_cert_cb( |
| 6275 | client_ctx_.get(), |
| 6276 | [](SSL *ssl, void *arg) { |
| 6277 | ADD_FAILURE() << "cert_cb unexpectedly called"; |
| 6278 | return 0; |
| 6279 | }, |
| 6280 | nullptr); |
| 6281 |
| 6282 | bssl::UniquePtr<SSL> client, server; |
| 6283 | EXPECT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 6284 | server_ctx_.get())); |
| 6285 | } |
| 6286 |
| 6287 | // Test that ticket-based sessions on the client get fake session IDs. |
| 6288 | TEST_P(SSLVersionTest, FakeIDsForTickets) { |
| 6289 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6290 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6291 |
| 6292 | bssl::UniquePtr<SSL_SESSION> session = |
| 6293 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6294 | ASSERT_TRUE(session); |
| 6295 |
| 6296 | EXPECT_TRUE(SSL_SESSION_has_ticket(session.get())); |
| 6297 | unsigned session_id_length; |
| 6298 | SSL_SESSION_get_id(session.get(), &session_id_length); |
| 6299 | EXPECT_NE(session_id_length, 0u); |
| 6300 | } |
| 6301 |
| 6302 | // These tests test multi-threaded behavior. They are intended to run with |
| 6303 | // ThreadSanitizer. |
| 6304 | #if defined(OPENSSL_THREADS) |
| 6305 | TEST_P(SSLVersionTest, SessionCacheThreads) { |
| 6306 | SSL_CTX_set_options(server_ctx_.get(), SSL_OP_NO_TICKET); |
| 6307 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6308 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6309 |
| 6310 | if (is_tls13()) { |
| 6311 | // Our TLS 1.3 implementation does not support stateful resumption. |
| 6312 | ASSERT_FALSE(CreateClientSession(client_ctx_.get(), server_ctx_.get())); |
| 6313 | return; |
| 6314 | } |
| 6315 |
| 6316 | // Establish two client sessions to test with. |
| 6317 | bssl::UniquePtr<SSL_SESSION> session1 = |
| 6318 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6319 | ASSERT_TRUE(session1); |
| 6320 | bssl::UniquePtr<SSL_SESSION> session2 = |
| 6321 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6322 | ASSERT_TRUE(session2); |
| 6323 |
| 6324 | auto connect_with_session = [&](SSL_SESSION *session) { |
| 6325 | ClientConfig config; |
| 6326 | config.session = session; |
| 6327 | UniquePtr<SSL> client, server; |
| 6328 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 6329 | server_ctx_.get(), config)); |
| 6330 | }; |
| 6331 |
| 6332 | // Resume sessions in parallel with establishing new ones. |
| 6333 | { |
| 6334 | std::vector<std::thread> threads; |
| 6335 | threads.emplace_back([&] { connect_with_session(nullptr); }); |
| 6336 | threads.emplace_back([&] { connect_with_session(nullptr); }); |
| 6337 | threads.emplace_back([&] { connect_with_session(session1.get()); }); |
| 6338 | threads.emplace_back([&] { connect_with_session(session1.get()); }); |
| 6339 | threads.emplace_back([&] { connect_with_session(session2.get()); }); |
| 6340 | threads.emplace_back([&] { connect_with_session(session2.get()); }); |
| 6341 | for (auto &thread : threads) { |
| 6342 | thread.join(); |
| 6343 | } |
| 6344 | } |
| 6345 |
| 6346 | // Hit the maximum session cache size across multiple threads, to test the |
| 6347 | // size enforcement logic. |
| 6348 | size_t limit = SSL_CTX_sess_number(server_ctx_.get()) + 2; |
| 6349 | SSL_CTX_sess_set_cache_size(server_ctx_.get(), limit); |
| 6350 | { |
| 6351 | std::vector<std::thread> threads; |
| 6352 | for (int i = 0; i < 4; i++) { |
| 6353 | threads.emplace_back([&]() { |
| 6354 | connect_with_session(nullptr); |
| 6355 | EXPECT_LE(SSL_CTX_sess_number(server_ctx_.get()), limit); |
| 6356 | }); |
| 6357 | } |
| 6358 | for (auto &thread : threads) { |
| 6359 | thread.join(); |
| 6360 | } |
| 6361 | EXPECT_EQ(SSL_CTX_sess_number(server_ctx_.get()), limit); |
| 6362 | } |
| 6363 |
| 6364 | // Reset the session cache, this time with a mock clock. |
| 6365 | ASSERT_NO_FATAL_FAILURE(ResetContexts()); |
| 6366 | SSL_CTX_set_options(server_ctx_.get(), SSL_OP_NO_TICKET); |
| 6367 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6368 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6369 | SSL_CTX_set_current_time_cb(server_ctx_.get(), CurrentTimeCallback); |
| 6370 |
| 6371 | // Make some sessions at an arbitrary start time. Then expire them. |
| 6372 | g_current_time.tv_sec = 1000; |
| 6373 | bssl::UniquePtr<SSL_SESSION> expired_session1 = |
| 6374 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6375 | ASSERT_TRUE(expired_session1); |
| 6376 | bssl::UniquePtr<SSL_SESSION> expired_session2 = |
| 6377 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6378 | ASSERT_TRUE(expired_session2); |
| 6379 | g_current_time.tv_sec += 100 * SSL_DEFAULT_SESSION_TIMEOUT; |
| 6380 |
| 6381 | session1 = CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6382 | ASSERT_TRUE(session1); |
| 6383 |
| 6384 | // Every 256 connections, we flush stale sessions from the session cache. Test |
| 6385 | // this logic is correctly synchronized with other connection attempts. |
| 6386 | static const int kNumConnections = 256; |
| 6387 | { |
| 6388 | std::vector<std::thread> threads; |
| 6389 | threads.emplace_back([&] { |
| 6390 | for (int i = 0; i < kNumConnections; i++) { |
| 6391 | connect_with_session(nullptr); |
| 6392 | } |
| 6393 | }); |
| 6394 | threads.emplace_back([&] { |
| 6395 | for (int i = 0; i < kNumConnections; i++) { |
| 6396 | connect_with_session(nullptr); |
| 6397 | } |
| 6398 | }); |
| 6399 | threads.emplace_back([&] { |
| 6400 | // Never connect with |expired_session2|. The session cache eagerly |
| 6401 | // removes expired sessions when it sees them. Leaving |expired_session2| |
| 6402 | // untouched ensures it is instead cleared by periodic flushing. |
| 6403 | for (int i = 0; i < kNumConnections; i++) { |
| 6404 | connect_with_session(expired_session1.get()); |
| 6405 | } |
| 6406 | }); |
| 6407 | threads.emplace_back([&] { |
| 6408 | for (int i = 0; i < kNumConnections; i++) { |
| 6409 | connect_with_session(session1.get()); |
| 6410 | } |
| 6411 | }); |
| 6412 | for (auto &thread : threads) { |
| 6413 | thread.join(); |
| 6414 | } |
| 6415 | } |
| 6416 | } |
| 6417 |
| 6418 | TEST_P(SSLVersionTest, SessionTicketThreads) { |
| 6419 | for (bool renew_ticket : {false, true}) { |
| 6420 | SCOPED_TRACE(renew_ticket); |
| 6421 | ASSERT_NO_FATAL_FAILURE(ResetContexts()); |
| 6422 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6423 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6424 | if (renew_ticket) { |
| 6425 | SSL_CTX_set_tlsext_ticket_key_cb(server_ctx_.get(), RenewTicketCallback); |
| 6426 | } |
| 6427 |
| 6428 | // Establish two client sessions to test with. |
| 6429 | bssl::UniquePtr<SSL_SESSION> session1 = |
| 6430 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6431 | ASSERT_TRUE(session1); |
| 6432 | bssl::UniquePtr<SSL_SESSION> session2 = |
| 6433 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6434 | ASSERT_TRUE(session2); |
| 6435 |
| 6436 | auto connect_with_session = [&](SSL_SESSION *session) { |
| 6437 | ClientConfig config; |
| 6438 | config.session = session; |
| 6439 | UniquePtr<SSL> client, server; |
| 6440 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 6441 | server_ctx_.get(), config)); |
| 6442 | }; |
| 6443 |
| 6444 | // Resume sessions in parallel with establishing new ones. |
| 6445 | { |
| 6446 | std::vector<std::thread> threads; |
| 6447 | threads.emplace_back([&] { connect_with_session(nullptr); }); |
| 6448 | threads.emplace_back([&] { connect_with_session(nullptr); }); |
| 6449 | threads.emplace_back([&] { connect_with_session(session1.get()); }); |
| 6450 | threads.emplace_back([&] { connect_with_session(session1.get()); }); |
| 6451 | threads.emplace_back([&] { connect_with_session(session2.get()); }); |
| 6452 | threads.emplace_back([&] { connect_with_session(session2.get()); }); |
| 6453 | for (auto &thread : threads) { |
| 6454 | thread.join(); |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | } |
| 6459 |
| 6460 | // SSL_CTX_get0_certificate needs to lock internally. Test this works. |
| 6461 | TEST(SSLTest, GetCertificateThreads) { |
| 6462 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 6463 | ASSERT_TRUE(ctx); |
| 6464 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 6465 | ASSERT_TRUE(cert); |
| 6466 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 6467 |
| 6468 | // Existing code expects |SSL_CTX_get0_certificate| to be callable from two |
| 6469 | // threads concurrently. It originally was an immutable operation. Now we |
| 6470 | // implement it with a thread-safe cache, so it is worth testing. |
| 6471 | X509 *cert2_thread; |
| 6472 | std::thread thread( |
| 6473 | [&] { cert2_thread = SSL_CTX_get0_certificate(ctx.get()); }); |
| 6474 | X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 6475 | thread.join(); |
| 6476 |
| 6477 | ASSERT_TRUE(cert2); |
| 6478 | ASSERT_TRUE(cert2_thread); |
| 6479 | EXPECT_EQ(cert2, cert2_thread); |
| 6480 | EXPECT_EQ(0, X509_cmp(cert.get(), cert2)); |
| 6481 | } |
| 6482 |
| 6483 | // Functions which access properties on the negotiated session are thread-safe |
| 6484 | // where needed. Prior to TLS 1.3, clients resuming sessions and servers |
| 6485 | // performing stateful resumption will share an underlying SSL_SESSION object, |
| 6486 | // potentially across threads. |
| 6487 | TEST_P(SSLVersionTest, SessionPropertiesThreads) { |
| 6488 | if (is_tls13()) { |
| 6489 | // Our TLS 1.3 implementation does not support stateful resumption. |
| 6490 | ASSERT_FALSE(CreateClientSession(client_ctx_.get(), server_ctx_.get())); |
| 6491 | return; |
| 6492 | } |
| 6493 |
| 6494 | SSL_CTX_set_options(server_ctx_.get(), SSL_OP_NO_TICKET); |
| 6495 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6496 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 6497 |
| 6498 | ASSERT_TRUE(UseCertAndKey(client_ctx_.get())); |
| 6499 | ASSERT_TRUE(UseCertAndKey(server_ctx_.get())); |
| 6500 |
| 6501 | // Configure mutual authentication, so we have more session state. |
| 6502 | SSL_CTX_set_custom_verify( |
| 6503 | client_ctx_.get(), SSL_VERIFY_PEER, |
| 6504 | [](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 6505 | SSL_CTX_set_custom_verify( |
| 6506 | server_ctx_.get(), SSL_VERIFY_PEER, |
| 6507 | [](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 6508 |
| 6509 | // Establish a client session to test with. |
| 6510 | bssl::UniquePtr<SSL_SESSION> session = |
| 6511 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 6512 | ASSERT_TRUE(session); |
| 6513 |
| 6514 | // Resume with it twice. |
| 6515 | UniquePtr<SSL> ssls[4]; |
| 6516 | ClientConfig config; |
| 6517 | config.session = session.get(); |
| 6518 | ASSERT_TRUE(ConnectClientAndServer(&ssls[0], &ssls[1], client_ctx_.get(), |
| 6519 | server_ctx_.get(), config)); |
| 6520 | ASSERT_TRUE(ConnectClientAndServer(&ssls[2], &ssls[3], client_ctx_.get(), |
| 6521 | server_ctx_.get(), config)); |
| 6522 |
| 6523 | // Read properties in parallel. |
| 6524 | auto read_properties = [](const SSL *ssl) { |
| 6525 | EXPECT_TRUE(SSL_get_peer_cert_chain(ssl)); |
| 6526 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(ssl)); |
| 6527 | EXPECT_TRUE(peer); |
| 6528 | EXPECT_TRUE(SSL_get_current_cipher(ssl)); |
| 6529 | EXPECT_TRUE(SSL_get_group_id(ssl)); |
| 6530 | }; |
| 6531 |
| 6532 | std::vector<std::thread> threads; |
| 6533 | for (const auto &ssl_ptr : ssls) { |
| 6534 | const SSL *ssl = ssl_ptr.get(); |
| 6535 | threads.emplace_back([=] { read_properties(ssl); }); |
| 6536 | } |
| 6537 | for (auto &thread : threads) { |
| 6538 | thread.join(); |
| 6539 | } |
| 6540 | } |
| 6541 |
| 6542 | static void SetValueOnFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 6543 | int index, long argl, void *argp) { |
| 6544 | if (ptr != nullptr) { |
| 6545 | *static_cast<long *>(ptr) = argl; |
| 6546 | } |
| 6547 | } |
| 6548 |
| 6549 | // Test that one thread can register ex_data while another thread is destroying |
| 6550 | // an object that uses it. |
| 6551 | TEST(SSLTest, ExDataThreads) { |
| 6552 | static bool already_run = false; |
| 6553 | if (already_run) { |
| 6554 | GTEST_SKIP() << "This test consumes process-global resources and can only " |
| 6555 | "be run once in a process. It is not compatible with " |
| 6556 | "--gtest_repeat."; |
| 6557 | } |
| 6558 | already_run = true; |
| 6559 |
| 6560 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 6561 | ASSERT_TRUE(ctx); |
| 6562 |
| 6563 | // Register an initial index, so the threads can exercise having any ex_data. |
| 6564 | int first_index = |
| 6565 | SSL_get_ex_new_index(-1, nullptr, nullptr, nullptr, SetValueOnFree); |
| 6566 | ASSERT_GE(first_index, 0); |
| 6567 |
| 6568 | // Callers may register indices concurrently with using other indices. This |
| 6569 | // may happen if one part of an application is initializing while another part |
| 6570 | // is already running. |
| 6571 | static constexpr int kNumIndices = 3; |
| 6572 | static constexpr int kNumSSLs = 10; |
| 6573 | int index[kNumIndices]; |
| 6574 | long values[kNumSSLs]; |
| 6575 | std::fill(std::begin(values), std::end(values), -2); |
| 6576 | std::vector<std::thread> threads; |
| 6577 | for (size_t i = 0; i < kNumIndices; i++) { |
| 6578 | threads.emplace_back([&, i] { |
| 6579 | index[i] = SSL_get_ex_new_index(static_cast<long>(i), nullptr, nullptr, |
| 6580 | nullptr, SetValueOnFree); |
| 6581 | ASSERT_GE(index[i], 0); |
| 6582 | }); |
| 6583 | } |
| 6584 | for (size_t i = 0; i < kNumSSLs; i++) { |
| 6585 | threads.emplace_back([&, i] { |
| 6586 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 6587 | ASSERT_TRUE(ssl); |
| 6588 | ASSERT_TRUE(SSL_set_ex_data(ssl.get(), first_index, &values[i])); |
| 6589 | }); |
| 6590 | } |
| 6591 | for (auto &thread : threads) { |
| 6592 | thread.join(); |
| 6593 | } |
| 6594 |
| 6595 | // Each of the SSL threads should have set their flag via ex_data. |
| 6596 | for (size_t i = 0; i < kNumSSLs; i++) { |
| 6597 | EXPECT_EQ(values[i], -1); |
| 6598 | } |
| 6599 |
| 6600 | // Each of the newly-registered indices should be distinct and work correctly. |
| 6601 | static_assert(kNumIndices <= kNumSSLs, "values buffer too small"); |
| 6602 | std::fill(std::begin(values), std::end(values), -2); |
| 6603 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 6604 | ASSERT_TRUE(ssl); |
| 6605 | for (size_t i = 0; i < kNumIndices; i++) { |
| 6606 | for (size_t j = 0; j < i; j++) { |
| 6607 | EXPECT_NE(index[i], index[j]); |
| 6608 | } |
| 6609 | ASSERT_TRUE(SSL_set_ex_data(ssl.get(), index[i], &values[i])); |
| 6610 | } |
| 6611 | ssl = nullptr; |
| 6612 | for (size_t i = 0; i < kNumIndices; i++) { |
| 6613 | EXPECT_EQ(values[i], static_cast<long>(i)); |
| 6614 | } |
| 6615 | } |
| 6616 | #endif // OPENSSL_THREADS |
| 6617 |
| 6618 | constexpr size_t kNumQUICLevels = 4; |
| 6619 | static_assert(ssl_encryption_initial < kNumQUICLevels, |
| 6620 | "kNumQUICLevels is wrong"); |
| 6621 | static_assert(ssl_encryption_early_data < kNumQUICLevels, |
| 6622 | "kNumQUICLevels is wrong"); |
| 6623 | static_assert(ssl_encryption_handshake < kNumQUICLevels, |
| 6624 | "kNumQUICLevels is wrong"); |
| 6625 | static_assert(ssl_encryption_application < kNumQUICLevels, |
| 6626 | "kNumQUICLevels is wrong"); |
| 6627 |
| 6628 | const char *LevelToString(ssl_encryption_level_t level) { |
| 6629 | switch (level) { |
| 6630 | case ssl_encryption_initial: |
| 6631 | return "initial"; |
| 6632 | case ssl_encryption_early_data: |
| 6633 | return "early data"; |
| 6634 | case ssl_encryption_handshake: |
| 6635 | return "handshake"; |
| 6636 | case ssl_encryption_application: |
| 6637 | return "application"; |
| 6638 | } |
| 6639 | return "<unknown>"; |
| 6640 | } |
| 6641 |
| 6642 | class MockQUICTransport { |
| 6643 | public: |
| 6644 | enum class Role { kClient, kServer }; |
| 6645 |
| 6646 | explicit MockQUICTransport(Role role) : role_(role) { |
| 6647 | // The caller is expected to configure initial secrets. |
| 6648 | levels_[ssl_encryption_initial].write_secret = {1}; |
| 6649 | levels_[ssl_encryption_initial].read_secret = {1}; |
| 6650 | } |
| 6651 |
| 6652 | void set_peer(MockQUICTransport *peer) { peer_ = peer; } |
| 6653 |
| 6654 | bool has_alert() const { return has_alert_; } |
| 6655 | ssl_encryption_level_t alert_level() const { return alert_level_; } |
| 6656 | uint8_t alert() const { return alert_; } |
| 6657 |
| 6658 | bool PeerSecretsMatch(ssl_encryption_level_t level) const { |
| 6659 | return levels_[level].write_secret == peer_->levels_[level].read_secret && |
| 6660 | levels_[level].read_secret == peer_->levels_[level].write_secret && |
| 6661 | levels_[level].cipher == peer_->levels_[level].cipher; |
| 6662 | } |
| 6663 |
| 6664 | bool HasReadSecret(ssl_encryption_level_t level) const { |
| 6665 | return !levels_[level].read_secret.empty(); |
| 6666 | } |
| 6667 |
| 6668 | bool HasWriteSecret(ssl_encryption_level_t level) const { |
| 6669 | return !levels_[level].write_secret.empty(); |
| 6670 | } |
| 6671 |
| 6672 | void AllowOutOfOrderWrites() { allow_out_of_order_writes_ = true; } |
| 6673 |
| 6674 | bool SetReadSecret(ssl_encryption_level_t level, const SSL_CIPHER *cipher, |
| 6675 | Span<const uint8_t> secret) { |
| 6676 | if (HasReadSecret(level)) { |
| 6677 | ADD_FAILURE() << LevelToString(level) << " read secret configured twice"; |
| 6678 | return false; |
| 6679 | } |
| 6680 |
| 6681 | if (role_ == Role::kClient && level == ssl_encryption_early_data) { |
| 6682 | ADD_FAILURE() << "Unexpected early data read secret"; |
| 6683 | return false; |
| 6684 | } |
| 6685 |
| 6686 | ssl_encryption_level_t ack_level = |
| 6687 | level == ssl_encryption_early_data ? ssl_encryption_application : level; |
| 6688 | if (!HasWriteSecret(ack_level)) { |
| 6689 | ADD_FAILURE() << LevelToString(level) |
| 6690 | << " read secret configured before ACK write secret"; |
| 6691 | return false; |
| 6692 | } |
| 6693 |
| 6694 | if (cipher == nullptr) { |
| 6695 | ADD_FAILURE() << "Unexpected null cipher"; |
| 6696 | return false; |
| 6697 | } |
| 6698 |
| 6699 | if (level != ssl_encryption_early_data && |
| 6700 | SSL_CIPHER_get_id(cipher) != levels_[level].cipher) { |
| 6701 | ADD_FAILURE() << "Cipher suite inconsistent"; |
| 6702 | return false; |
| 6703 | } |
| 6704 |
| 6705 | levels_[level].read_secret.assign(secret.begin(), secret.end()); |
| 6706 | levels_[level].cipher = SSL_CIPHER_get_id(cipher); |
| 6707 | return true; |
| 6708 | } |
| 6709 |
| 6710 | bool SetWriteSecret(ssl_encryption_level_t level, const SSL_CIPHER *cipher, |
| 6711 | Span<const uint8_t> secret) { |
| 6712 | if (HasWriteSecret(level)) { |
| 6713 | ADD_FAILURE() << LevelToString(level) << " write secret configured twice"; |
| 6714 | return false; |
| 6715 | } |
| 6716 |
| 6717 | if (role_ == Role::kServer && level == ssl_encryption_early_data) { |
| 6718 | ADD_FAILURE() << "Unexpected early data write secret"; |
| 6719 | return false; |
| 6720 | } |
| 6721 |
| 6722 | if (cipher == nullptr) { |
| 6723 | ADD_FAILURE() << "Unexpected null cipher"; |
| 6724 | return false; |
| 6725 | } |
| 6726 |
| 6727 | levels_[level].write_secret.assign(secret.begin(), secret.end()); |
| 6728 | levels_[level].cipher = SSL_CIPHER_get_id(cipher); |
| 6729 | return true; |
| 6730 | } |
| 6731 |
| 6732 | bool WriteHandshakeData(ssl_encryption_level_t level, |
| 6733 | Span<const uint8_t> data) { |
| 6734 | if (levels_[level].write_secret.empty()) { |
| 6735 | ADD_FAILURE() << LevelToString(level) |
| 6736 | << " write secret not yet configured"; |
| 6737 | return false; |
| 6738 | } |
| 6739 |
| 6740 | // Although the levels are conceptually separate, BoringSSL finishes writing |
| 6741 | // data from a previous level before installing keys for the next level. |
| 6742 | if (!allow_out_of_order_writes_) { |
| 6743 | switch (level) { |
| 6744 | case ssl_encryption_early_data: |
| 6745 | ADD_FAILURE() << "unexpected handshake data at early data level"; |
| 6746 | return false; |
| 6747 | case ssl_encryption_initial: |
| 6748 | if (!levels_[ssl_encryption_handshake].write_secret.empty()) { |
| 6749 | ADD_FAILURE() |
| 6750 | << LevelToString(level) |
| 6751 | << " handshake data written after handshake keys installed"; |
| 6752 | return false; |
| 6753 | } |
| 6754 | [[fallthrough]]; |
| 6755 | case ssl_encryption_handshake: |
| 6756 | if (!levels_[ssl_encryption_application].write_secret.empty()) { |
| 6757 | ADD_FAILURE() |
| 6758 | << LevelToString(level) |
| 6759 | << " handshake data written after application keys installed"; |
| 6760 | return false; |
| 6761 | } |
| 6762 | [[fallthrough]]; |
| 6763 | case ssl_encryption_application: |
| 6764 | break; |
| 6765 | } |
| 6766 | } |
| 6767 |
| 6768 | levels_[level].write_data.insert(levels_[level].write_data.end(), |
| 6769 | data.begin(), data.end()); |
| 6770 | return true; |
| 6771 | } |
| 6772 |
| 6773 | bool SendAlert(ssl_encryption_level_t level, uint8_t alert_value) { |
| 6774 | if (has_alert_) { |
| 6775 | ADD_FAILURE() << "duplicate alert sent"; |
| 6776 | return false; |
| 6777 | } |
| 6778 |
| 6779 | if (levels_[level].write_secret.empty()) { |
| 6780 | ADD_FAILURE() << LevelToString(level) |
| 6781 | << " write secret not yet configured"; |
| 6782 | return false; |
| 6783 | } |
| 6784 |
| 6785 | has_alert_ = true; |
| 6786 | alert_level_ = level; |
| 6787 | alert_ = alert_value; |
| 6788 | return true; |
| 6789 | } |
| 6790 |
| 6791 | bool ReadHandshakeData(std::vector<uint8_t> *out, |
| 6792 | ssl_encryption_level_t level, |
| 6793 | size_t num = std::numeric_limits<size_t>::max()) { |
| 6794 | if (levels_[level].read_secret.empty()) { |
| 6795 | ADD_FAILURE() << "data read before keys configured in level " << level; |
| 6796 | return false; |
| 6797 | } |
| 6798 | // The peer may not have configured any keys yet. |
| 6799 | if (peer_->levels_[level].write_secret.empty()) { |
| 6800 | out->clear(); |
| 6801 | return true; |
| 6802 | } |
| 6803 | // Check the peer computed the same key. |
| 6804 | if (peer_->levels_[level].write_secret != levels_[level].read_secret) { |
| 6805 | ADD_FAILURE() << "peer write key does not match read key in level " |
| 6806 | << level; |
| 6807 | return false; |
| 6808 | } |
| 6809 | if (peer_->levels_[level].cipher != levels_[level].cipher) { |
| 6810 | ADD_FAILURE() << "peer cipher does not match in level " << level; |
| 6811 | return false; |
| 6812 | } |
| 6813 | std::vector<uint8_t> *peer_data = &peer_->levels_[level].write_data; |
| 6814 | num = std::min(num, peer_data->size()); |
| 6815 | out->assign(peer_data->begin(), peer_data->begin() + num); |
| 6816 | peer_data->erase(peer_data->begin(), peer_data->begin() + num); |
| 6817 | return true; |
| 6818 | } |
| 6819 |
| 6820 | private: |
| 6821 | Role role_; |
| 6822 | MockQUICTransport *peer_ = nullptr; |
| 6823 |
| 6824 | bool allow_out_of_order_writes_ = false; |
| 6825 | bool has_alert_ = false; |
| 6826 | ssl_encryption_level_t alert_level_ = ssl_encryption_initial; |
| 6827 | uint8_t alert_ = 0; |
| 6828 |
| 6829 | struct Level { |
| 6830 | std::vector<uint8_t> write_data; |
| 6831 | std::vector<uint8_t> write_secret; |
| 6832 | std::vector<uint8_t> read_secret; |
| 6833 | uint32_t cipher = 0; |
| 6834 | }; |
| 6835 | Level levels_[kNumQUICLevels]; |
| 6836 | }; |
| 6837 |
| 6838 | class MockQUICTransportPair { |
| 6839 | public: |
| 6840 | MockQUICTransportPair() |
| 6841 | : client_(MockQUICTransport::Role::kClient), |
| 6842 | server_(MockQUICTransport::Role::kServer) { |
| 6843 | client_.set_peer(&server_); |
| 6844 | server_.set_peer(&client_); |
| 6845 | } |
| 6846 |
| 6847 | ~MockQUICTransportPair() { |
| 6848 | client_.set_peer(nullptr); |
| 6849 | server_.set_peer(nullptr); |
| 6850 | } |
| 6851 |
| 6852 | MockQUICTransport *client() { return &client_; } |
| 6853 | MockQUICTransport *server() { return &server_; } |
| 6854 |
| 6855 | bool SecretsMatch(ssl_encryption_level_t level) const { |
| 6856 | // We only need to check |HasReadSecret| and |HasWriteSecret| on |client_|. |
| 6857 | // |PeerSecretsMatch| checks that |server_| is analogously configured. |
| 6858 | return client_.PeerSecretsMatch(level) && client_.HasWriteSecret(level) && |
| 6859 | (level == ssl_encryption_early_data || client_.HasReadSecret(level)); |
| 6860 | } |
| 6861 |
| 6862 | private: |
| 6863 | MockQUICTransport client_; |
| 6864 | MockQUICTransport server_; |
| 6865 | }; |
| 6866 |
| 6867 | class QUICMethodTest : public testing::Test { |
| 6868 | protected: |
| 6869 | void SetUp() override { |
| 6870 | client_ctx_.reset(SSL_CTX_new(TLS_method())); |
| 6871 | server_ctx_ = CreateContextWithTestCertificate(TLS_method()); |
| 6872 | ASSERT_TRUE(client_ctx_); |
| 6873 | ASSERT_TRUE(server_ctx_); |
| 6874 |
| 6875 | SSL_CTX_set_min_proto_version(server_ctx_.get(), TLS1_3_VERSION); |
| 6876 | SSL_CTX_set_max_proto_version(server_ctx_.get(), TLS1_3_VERSION); |
| 6877 | SSL_CTX_set_min_proto_version(client_ctx_.get(), TLS1_3_VERSION); |
| 6878 | SSL_CTX_set_max_proto_version(client_ctx_.get(), TLS1_3_VERSION); |
| 6879 |
| 6880 | static const uint8_t kALPNProtos[] = {0x03, 'f', 'o', 'o'}; |
| 6881 | ASSERT_EQ(SSL_CTX_set_alpn_protos(client_ctx_.get(), kALPNProtos, |
| 6882 | sizeof(kALPNProtos)), |
| 6883 | 0); |
| 6884 | SSL_CTX_set_alpn_select_cb( |
| 6885 | server_ctx_.get(), |
| 6886 | [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 6887 | unsigned in_len, void *arg) -> int { |
| 6888 | return SSL_select_next_proto( |
| 6889 | const_cast<uint8_t **>(out), out_len, in, in_len, |
| 6890 | kALPNProtos, sizeof(kALPNProtos)) == OPENSSL_NPN_NEGOTIATED |
| 6891 | ? SSL_TLSEXT_ERR_OK |
| 6892 | : SSL_TLSEXT_ERR_NOACK; |
| 6893 | }, |
| 6894 | nullptr); |
| 6895 | } |
| 6896 |
| 6897 | static MockQUICTransport *TransportFromSSL(const SSL *ssl) { |
| 6898 | return ex_data_.Get(ssl); |
| 6899 | } |
| 6900 |
| 6901 | static bool ProvideHandshakeData( |
| 6902 | SSL *ssl, size_t num = std::numeric_limits<size_t>::max()) { |
| 6903 | MockQUICTransport *transport = TransportFromSSL(ssl); |
| 6904 | ssl_encryption_level_t level = SSL_quic_read_level(ssl); |
| 6905 | std::vector<uint8_t> data; |
| 6906 | return transport->ReadHandshakeData(&data, level, num) && |
| 6907 | SSL_provide_quic_data(ssl, level, data.data(), data.size()); |
| 6908 | } |
| 6909 |
| 6910 | void AllowOutOfOrderWrites() { allow_out_of_order_writes_ = true; } |
| 6911 |
| 6912 | bool CreateClientAndServer() { |
| 6913 | client_.reset(SSL_new(client_ctx_.get())); |
| 6914 | server_.reset(SSL_new(server_ctx_.get())); |
| 6915 | if (!client_ || !server_) { |
| 6916 | return false; |
| 6917 | } |
| 6918 |
| 6919 | SSL_set_connect_state(client_.get()); |
| 6920 | SSL_set_accept_state(server_.get()); |
| 6921 |
| 6922 | transport_ = std::make_unique<MockQUICTransportPair>(); |
| 6923 | if (!ex_data_.Set(client_.get(), transport_->client()) || |
| 6924 | !ex_data_.Set(server_.get(), transport_->server())) { |
| 6925 | return false; |
| 6926 | } |
| 6927 | if (allow_out_of_order_writes_) { |
| 6928 | transport_->client()->AllowOutOfOrderWrites(); |
| 6929 | transport_->server()->AllowOutOfOrderWrites(); |
| 6930 | } |
| 6931 | static const uint8_t client_transport_params[] = {0}; |
| 6932 | if (!SSL_set_quic_transport_params(client_.get(), client_transport_params, |
| 6933 | sizeof(client_transport_params)) || |
| 6934 | !SSL_set_quic_transport_params(server_.get(), |
| 6935 | server_transport_params_.data(), |
| 6936 | server_transport_params_.size()) || |
| 6937 | !SSL_set_quic_early_data_context( |
| 6938 | server_.get(), server_quic_early_data_context_.data(), |
| 6939 | server_quic_early_data_context_.size())) { |
| 6940 | return false; |
| 6941 | } |
| 6942 | return true; |
| 6943 | } |
| 6944 |
| 6945 | enum class ExpectedError { |
| 6946 | kNoError, |
| 6947 | kClientError, |
| 6948 | kServerError, |
| 6949 | }; |
| 6950 |
| 6951 | // CompleteHandshakesForQUIC runs |SSL_do_handshake| on |client_| and |
| 6952 | // |server_| until each completes once. It returns true on success and false |
| 6953 | // on failure. |
| 6954 | bool CompleteHandshakesForQUIC() { |
| 6955 | return RunQUICHandshakesAndExpectError(ExpectedError::kNoError); |
| 6956 | } |
| 6957 |
| 6958 | // Runs |SSL_do_handshake| on |client_| and |server_| until each completes |
| 6959 | // once. If |expect_client_error| is true, it will return true only if the |
| 6960 | // client handshake failed. Otherwise, it returns true if both handshakes |
| 6961 | // succeed and false otherwise. |
| 6962 | bool RunQUICHandshakesAndExpectError(ExpectedError expected_error) { |
| 6963 | bool client_done = false, server_done = false; |
| 6964 | while (!client_done || !server_done) { |
| 6965 | if (!client_done) { |
| 6966 | if (!ProvideHandshakeData(client_.get())) { |
| 6967 | ADD_FAILURE() << "ProvideHandshakeData(client_) failed"; |
| 6968 | return false; |
| 6969 | } |
| 6970 | int client_ret = SSL_do_handshake(client_.get()); |
| 6971 | int client_err = SSL_get_error(client_.get(), client_ret); |
| 6972 | if (client_ret == 1) { |
| 6973 | client_done = true; |
| 6974 | } else if (client_ret != -1 || client_err != SSL_ERROR_WANT_READ) { |
| 6975 | if (expected_error == ExpectedError::kClientError) { |
| 6976 | return true; |
| 6977 | } |
| 6978 | ADD_FAILURE() << "Unexpected client output: " << client_ret << " " |
| 6979 | << client_err; |
| 6980 | return false; |
| 6981 | } |
| 6982 | } |
| 6983 |
| 6984 | if (!server_done) { |
| 6985 | if (!ProvideHandshakeData(server_.get())) { |
| 6986 | ADD_FAILURE() << "ProvideHandshakeData(server_) failed"; |
| 6987 | return false; |
| 6988 | } |
| 6989 | int server_ret = SSL_do_handshake(server_.get()); |
| 6990 | int server_err = SSL_get_error(server_.get(), server_ret); |
| 6991 | if (server_ret == 1) { |
| 6992 | server_done = true; |
| 6993 | } else if (server_ret != -1 || server_err != SSL_ERROR_WANT_READ) { |
| 6994 | if (expected_error == ExpectedError::kServerError) { |
| 6995 | return true; |
| 6996 | } |
| 6997 | ADD_FAILURE() << "Unexpected server output: " << server_ret << " " |
| 6998 | << server_err; |
| 6999 | return false; |
| 7000 | } |
| 7001 | } |
| 7002 | } |
| 7003 | return expected_error == ExpectedError::kNoError; |
| 7004 | } |
| 7005 |
| 7006 | bssl::UniquePtr<SSL_SESSION> CreateClientSessionForQUIC() { |
| 7007 | g_last_session = nullptr; |
| 7008 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 7009 | if (!CreateClientAndServer() || !CompleteHandshakesForQUIC()) { |
| 7010 | return nullptr; |
| 7011 | } |
| 7012 |
| 7013 | // The server sent NewSessionTicket messages in the handshake. |
| 7014 | if (!ProvideHandshakeData(client_.get()) || |
| 7015 | !SSL_process_quic_post_handshake(client_.get())) { |
| 7016 | return nullptr; |
| 7017 | } |
| 7018 |
| 7019 | return std::move(g_last_session); |
| 7020 | } |
| 7021 |
| 7022 | void ExpectHandshakeSuccess() { |
| 7023 | EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_application)); |
| 7024 | EXPECT_EQ(ssl_encryption_application, SSL_quic_read_level(client_.get())); |
| 7025 | EXPECT_EQ(ssl_encryption_application, SSL_quic_write_level(client_.get())); |
| 7026 | EXPECT_EQ(ssl_encryption_application, SSL_quic_read_level(server_.get())); |
| 7027 | EXPECT_EQ(ssl_encryption_application, SSL_quic_write_level(server_.get())); |
| 7028 | EXPECT_FALSE(transport_->client()->has_alert()); |
| 7029 | EXPECT_FALSE(transport_->server()->has_alert()); |
| 7030 |
| 7031 | // SSL_do_handshake is now idempotent. |
| 7032 | EXPECT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7033 | EXPECT_EQ(SSL_do_handshake(server_.get()), 1); |
| 7034 | } |
| 7035 |
| 7036 | // Returns a default SSL_QUIC_METHOD. Individual methods may be overwritten by |
| 7037 | // the test. |
| 7038 | SSL_QUIC_METHOD DefaultQUICMethod() { |
| 7039 | return SSL_QUIC_METHOD{ |
| 7040 | SetReadSecretCallback, SetWriteSecretCallback, AddHandshakeDataCallback, |
| 7041 | FlushFlightCallback, SendAlertCallback, |
| 7042 | }; |
| 7043 | } |
| 7044 |
| 7045 | static int SetReadSecretCallback(SSL *ssl, ssl_encryption_level_t level, |
| 7046 | const SSL_CIPHER *cipher, |
| 7047 | const uint8_t *secret, size_t secret_len) { |
| 7048 | return TransportFromSSL(ssl)->SetReadSecret(level, cipher, |
| 7049 | Span(secret, secret_len)); |
| 7050 | } |
| 7051 |
| 7052 | static int SetWriteSecretCallback(SSL *ssl, ssl_encryption_level_t level, |
| 7053 | const SSL_CIPHER *cipher, |
| 7054 | const uint8_t *secret, size_t secret_len) { |
| 7055 | return TransportFromSSL(ssl)->SetWriteSecret(level, cipher, |
| 7056 | Span(secret, secret_len)); |
| 7057 | } |
| 7058 |
| 7059 | static int AddHandshakeDataCallback(SSL *ssl, |
| 7060 | enum ssl_encryption_level_t level, |
| 7061 | const uint8_t *data, size_t len) { |
| 7062 | EXPECT_EQ(level, SSL_quic_write_level(ssl)); |
| 7063 | return TransportFromSSL(ssl)->WriteHandshakeData(level, Span(data, len)); |
| 7064 | } |
| 7065 |
| 7066 | static int FlushFlightCallback(SSL *ssl) { return 1; } |
| 7067 |
| 7068 | static int SendAlertCallback(SSL *ssl, ssl_encryption_level_t level, |
| 7069 | uint8_t alert) { |
| 7070 | EXPECT_EQ(level, SSL_quic_write_level(ssl)); |
| 7071 | return TransportFromSSL(ssl)->SendAlert(level, alert); |
| 7072 | } |
| 7073 |
| 7074 | bssl::UniquePtr<SSL_CTX> client_ctx_; |
| 7075 | bssl::UniquePtr<SSL_CTX> server_ctx_; |
| 7076 |
| 7077 | static UnownedSSLExData<MockQUICTransport> ex_data_; |
| 7078 | std::unique_ptr<MockQUICTransportPair> transport_; |
| 7079 |
| 7080 | bssl::UniquePtr<SSL> client_; |
| 7081 | bssl::UniquePtr<SSL> server_; |
| 7082 |
| 7083 | std::vector<uint8_t> server_transport_params_ = {1}; |
| 7084 | std::vector<uint8_t> server_quic_early_data_context_ = {2}; |
| 7085 |
| 7086 | bool allow_out_of_order_writes_ = false; |
| 7087 | }; |
| 7088 |
| 7089 | UnownedSSLExData<MockQUICTransport> QUICMethodTest::ex_data_; |
| 7090 |
| 7091 | // Test a full handshake and resumption work. |
| 7092 | TEST_F(QUICMethodTest, Basic) { |
| 7093 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7094 |
| 7095 | g_last_session = nullptr; |
| 7096 |
| 7097 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7098 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 7099 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7100 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7101 |
| 7102 | ASSERT_TRUE(CreateClientAndServer()); |
| 7103 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7104 |
| 7105 | ExpectHandshakeSuccess(); |
| 7106 | EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 7107 | EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 7108 |
| 7109 | // SSL_get_traffic_secrets is not defined for QUIC. |
| 7110 | Span<const uint8_t> read_secret, write_secret; |
| 7111 | EXPECT_FALSE( |
| 7112 | SSL_get_traffic_secrets(client_.get(), &read_secret, &write_secret)); |
| 7113 | EXPECT_FALSE( |
| 7114 | SSL_get_traffic_secrets(server_.get(), &read_secret, &write_secret)); |
| 7115 |
| 7116 | // The server sent NewSessionTicket messages in the handshake. |
| 7117 | EXPECT_FALSE(g_last_session); |
| 7118 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7119 | EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 7120 | EXPECT_TRUE(g_last_session); |
| 7121 |
| 7122 | // Create a second connection to verify resumption works. |
| 7123 | ASSERT_TRUE(CreateClientAndServer()); |
| 7124 | bssl::UniquePtr<SSL_SESSION> session = std::move(g_last_session); |
| 7125 | SSL_set_session(client_.get(), session.get()); |
| 7126 |
| 7127 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7128 |
| 7129 | ExpectHandshakeSuccess(); |
| 7130 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 7131 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 7132 | } |
| 7133 |
| 7134 | // Test that HelloRetryRequest in QUIC works. |
| 7135 | TEST_F(QUICMethodTest, HelloRetryRequest) { |
| 7136 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7137 |
| 7138 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7139 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7140 |
| 7141 | // BoringSSL predicts the most preferred ECDH group, so using different |
| 7142 | // preferences will trigger HelloRetryRequest. |
| 7143 | static const int kClientPrefs[] = {NID_X25519, NID_X9_62_prime256v1}; |
| 7144 | ASSERT_TRUE(SSL_CTX_set1_groups(client_ctx_.get(), kClientPrefs, |
| 7145 | OPENSSL_ARRAY_SIZE(kClientPrefs))); |
| 7146 | static const int kServerPrefs[] = {NID_X9_62_prime256v1, NID_X25519}; |
| 7147 | ASSERT_TRUE(SSL_CTX_set1_groups(server_ctx_.get(), kServerPrefs, |
| 7148 | OPENSSL_ARRAY_SIZE(kServerPrefs))); |
| 7149 |
| 7150 | ASSERT_TRUE(CreateClientAndServer()); |
| 7151 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7152 | ExpectHandshakeSuccess(); |
| 7153 | } |
| 7154 |
| 7155 | // Test that the client does not send a legacy_session_id in the ClientHello. |
| 7156 | TEST_F(QUICMethodTest, NoLegacySessionId) { |
| 7157 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7158 |
| 7159 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7160 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7161 | // Check that the session ID length is 0 in an early callback. |
| 7162 | SSL_CTX_set_select_certificate_cb( |
| 7163 | server_ctx_.get(), |
| 7164 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 7165 | EXPECT_EQ(client_hello->session_id_len, 0u); |
| 7166 | return ssl_select_cert_success; |
| 7167 | }); |
| 7168 |
| 7169 | ASSERT_TRUE(CreateClientAndServer()); |
| 7170 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7171 |
| 7172 | ExpectHandshakeSuccess(); |
| 7173 | } |
| 7174 |
| 7175 | // Test that, even in a 1-RTT handshake, the server installs keys at the right |
| 7176 | // time. Half-RTT keys are available early, but 1-RTT read keys are deferred. |
| 7177 | TEST_F(QUICMethodTest, HalfRTTKeys) { |
| 7178 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7179 |
| 7180 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7181 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7182 | ASSERT_TRUE(CreateClientAndServer()); |
| 7183 |
| 7184 | // The client sends ClientHello. |
| 7185 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7186 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client_.get(), -1)); |
| 7187 |
| 7188 | // The server reads ClientHello and sends ServerHello..Finished. |
| 7189 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7190 | ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 7191 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 7192 |
| 7193 | // At this point, the server has half-RTT write keys, but it cannot access |
| 7194 | // 1-RTT read keys until client Finished. |
| 7195 | EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 7196 | EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 7197 |
| 7198 | // Finish up the client and server handshakes. |
| 7199 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7200 |
| 7201 | // Both sides can now exchange 1-RTT data. |
| 7202 | ExpectHandshakeSuccess(); |
| 7203 | } |
| 7204 |
| 7205 | TEST_F(QUICMethodTest, ZeroRTTAccept) { |
| 7206 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7207 |
| 7208 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7209 | SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 7210 | SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 7211 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7212 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7213 |
| 7214 | bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 7215 | ASSERT_TRUE(session); |
| 7216 |
| 7217 | ASSERT_TRUE(CreateClientAndServer()); |
| 7218 | SSL_set_session(client_.get(), session.get()); |
| 7219 |
| 7220 | // The client handshake should return immediately into the early data state. |
| 7221 | ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7222 | EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 7223 | // The transport should have keys for sending 0-RTT data. |
| 7224 | EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7225 |
| 7226 | // The server will consume the ClientHello and also enter the early data |
| 7227 | // state. |
| 7228 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7229 | ASSERT_EQ(SSL_do_handshake(server_.get()), 1); |
| 7230 | EXPECT_TRUE(SSL_in_early_data(server_.get())); |
| 7231 | EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_early_data)); |
| 7232 | // At this point, the server has half-RTT write keys, but it cannot access |
| 7233 | // 1-RTT read keys until client Finished. |
| 7234 | EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 7235 | EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 7236 |
| 7237 | // Finish up the client and server handshakes. |
| 7238 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7239 |
| 7240 | // Both sides can now exchange 1-RTT data. |
| 7241 | ExpectHandshakeSuccess(); |
| 7242 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 7243 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 7244 | EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 7245 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7246 | EXPECT_TRUE(SSL_early_data_accepted(client_.get())); |
| 7247 | EXPECT_TRUE(SSL_early_data_accepted(server_.get())); |
| 7248 |
| 7249 | // Finish handling post-handshake messages after the first 0-RTT resumption. |
| 7250 | EXPECT_TRUE(ProvideHandshakeData(client_.get())); |
| 7251 | EXPECT_TRUE(SSL_process_quic_post_handshake(client_.get())); |
| 7252 |
| 7253 | // Perform a second 0-RTT resumption attempt, and confirm that 0-RTT is |
| 7254 | // accepted again. |
| 7255 | ASSERT_TRUE(CreateClientAndServer()); |
| 7256 | SSL_set_session(client_.get(), g_last_session.get()); |
| 7257 |
| 7258 | // The client handshake should return immediately into the early data state. |
| 7259 | ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7260 | EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 7261 | // The transport should have keys for sending 0-RTT data. |
| 7262 | EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7263 |
| 7264 | // The server will consume the ClientHello and also enter the early data |
| 7265 | // state. |
| 7266 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7267 | ASSERT_EQ(SSL_do_handshake(server_.get()), 1); |
| 7268 | EXPECT_TRUE(SSL_in_early_data(server_.get())); |
| 7269 | EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_early_data)); |
| 7270 | // At this point, the server has half-RTT write keys, but it cannot access |
| 7271 | // 1-RTT read keys until client Finished. |
| 7272 | EXPECT_TRUE(transport_->server()->HasWriteSecret(ssl_encryption_application)); |
| 7273 | EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_application)); |
| 7274 |
| 7275 | // Finish up the client and server handshakes. |
| 7276 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7277 |
| 7278 | // Both sides can now exchange 1-RTT data. |
| 7279 | ExpectHandshakeSuccess(); |
| 7280 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 7281 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 7282 | EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 7283 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7284 | EXPECT_TRUE(SSL_early_data_accepted(client_.get())); |
| 7285 | EXPECT_TRUE(SSL_early_data_accepted(server_.get())); |
| 7286 | EXPECT_EQ(SSL_get_early_data_reason(client_.get()), ssl_early_data_accepted); |
| 7287 | EXPECT_EQ(SSL_get_early_data_reason(server_.get()), ssl_early_data_accepted); |
| 7288 | } |
| 7289 |
| 7290 | TEST_F(QUICMethodTest, ZeroRTTRejectMismatchedParameters) { |
| 7291 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7292 |
| 7293 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7294 | SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 7295 | SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 7296 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7297 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7298 |
| 7299 |
| 7300 | bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 7301 | ASSERT_TRUE(session); |
| 7302 |
| 7303 | ASSERT_TRUE(CreateClientAndServer()); |
| 7304 | static const uint8_t new_context[] = {4}; |
| 7305 | ASSERT_TRUE(SSL_set_quic_early_data_context(server_.get(), new_context, |
| 7306 | sizeof(new_context))); |
| 7307 | SSL_set_session(client_.get(), session.get()); |
| 7308 |
| 7309 | // The client handshake should return immediately into the early data |
| 7310 | // state. |
| 7311 | ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7312 | EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 7313 | // The transport should have keys for sending 0-RTT data. |
| 7314 | EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7315 |
| 7316 | // The server will consume the ClientHello, but it will not accept 0-RTT. |
| 7317 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7318 | ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 7319 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 7320 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7321 | EXPECT_FALSE(transport_->server()->HasReadSecret(ssl_encryption_early_data)); |
| 7322 |
| 7323 | // The client consumes the server response and signals 0-RTT rejection. |
| 7324 | for (;;) { |
| 7325 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7326 | ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 7327 | int err = SSL_get_error(client_.get(), -1); |
| 7328 | if (err == SSL_ERROR_EARLY_DATA_REJECTED) { |
| 7329 | break; |
| 7330 | } |
| 7331 | ASSERT_EQ(SSL_ERROR_WANT_READ, err); |
| 7332 | } |
| 7333 |
| 7334 | // As in TLS over TCP, 0-RTT rejection is sticky. |
| 7335 | ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 7336 | ASSERT_EQ(SSL_ERROR_EARLY_DATA_REJECTED, SSL_get_error(client_.get(), -1)); |
| 7337 |
| 7338 | // Finish up the client and server handshakes. |
| 7339 | SSL_reset_early_data_reject(client_.get()); |
| 7340 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7341 |
| 7342 | // Both sides can now exchange 1-RTT data. |
| 7343 | ExpectHandshakeSuccess(); |
| 7344 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 7345 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 7346 | EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 7347 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7348 | EXPECT_FALSE(SSL_early_data_accepted(client_.get())); |
| 7349 | EXPECT_FALSE(SSL_early_data_accepted(server_.get())); |
| 7350 | } |
| 7351 |
| 7352 | TEST_F(QUICMethodTest, NoZeroRTTTicketWithoutEarlyDataContext) { |
| 7353 | server_quic_early_data_context_ = {}; |
| 7354 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7355 |
| 7356 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7357 | SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 7358 | SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 7359 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7360 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7361 |
| 7362 | bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 7363 | ASSERT_TRUE(session); |
| 7364 | EXPECT_FALSE(SSL_SESSION_early_data_capable(session.get())); |
| 7365 | } |
| 7366 |
| 7367 | TEST_F(QUICMethodTest, ZeroRTTReject) { |
| 7368 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7369 |
| 7370 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7371 | SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 7372 | SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 7373 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7374 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7375 |
| 7376 | bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 7377 | ASSERT_TRUE(session); |
| 7378 |
| 7379 | for (bool reject_hrr : {false, true}) { |
| 7380 | SCOPED_TRACE(reject_hrr); |
| 7381 |
| 7382 | ASSERT_TRUE(CreateClientAndServer()); |
| 7383 | if (reject_hrr) { |
| 7384 | // Configure the server to prefer P-256, which will reject 0-RTT via |
| 7385 | // HelloRetryRequest. |
| 7386 | int p256 = NID_X9_62_prime256v1; |
| 7387 | ASSERT_TRUE(SSL_set1_groups(server_.get(), &p256, 1)); |
| 7388 | } else { |
| 7389 | // Disable 0-RTT on the server, so it will reject it. |
| 7390 | SSL_set_early_data_enabled(server_.get(), 0); |
| 7391 | } |
| 7392 | SSL_set_session(client_.get(), session.get()); |
| 7393 |
| 7394 | // The client handshake should return immediately into the early data state. |
| 7395 | ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7396 | EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 7397 | // The transport should have keys for sending 0-RTT data. |
| 7398 | EXPECT_TRUE( |
| 7399 | transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7400 |
| 7401 | // The server will consume the ClientHello, but it will not accept 0-RTT. |
| 7402 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7403 | ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 7404 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server_.get(), -1)); |
| 7405 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7406 | EXPECT_FALSE( |
| 7407 | transport_->server()->HasReadSecret(ssl_encryption_early_data)); |
| 7408 |
| 7409 | // The client consumes the server response and signals 0-RTT rejection. |
| 7410 | for (;;) { |
| 7411 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7412 | ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 7413 | int err = SSL_get_error(client_.get(), -1); |
| 7414 | if (err == SSL_ERROR_EARLY_DATA_REJECTED) { |
| 7415 | break; |
| 7416 | } |
| 7417 | ASSERT_EQ(SSL_ERROR_WANT_READ, err); |
| 7418 | } |
| 7419 |
| 7420 | // As in TLS over TCP, 0-RTT rejection is sticky. |
| 7421 | ASSERT_EQ(-1, SSL_do_handshake(client_.get())); |
| 7422 | ASSERT_EQ(SSL_ERROR_EARLY_DATA_REJECTED, SSL_get_error(client_.get(), -1)); |
| 7423 |
| 7424 | // Finish up the client and server handshakes. |
| 7425 | SSL_reset_early_data_reject(client_.get()); |
| 7426 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7427 |
| 7428 | // Both sides can now exchange 1-RTT data. |
| 7429 | ExpectHandshakeSuccess(); |
| 7430 | EXPECT_TRUE(SSL_session_reused(client_.get())); |
| 7431 | EXPECT_TRUE(SSL_session_reused(server_.get())); |
| 7432 | EXPECT_FALSE(SSL_in_early_data(client_.get())); |
| 7433 | EXPECT_FALSE(SSL_in_early_data(server_.get())); |
| 7434 | EXPECT_FALSE(SSL_early_data_accepted(client_.get())); |
| 7435 | EXPECT_FALSE(SSL_early_data_accepted(server_.get())); |
| 7436 | } |
| 7437 | } |
| 7438 |
| 7439 | TEST_F(QUICMethodTest, NoZeroRTTKeysBeforeReverify) { |
| 7440 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7441 |
| 7442 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7443 | SSL_CTX_set_early_data_enabled(client_ctx_.get(), 1); |
| 7444 | SSL_CTX_set_reverify_on_resume(client_ctx_.get(), 1); |
| 7445 | SSL_CTX_set_early_data_enabled(server_ctx_.get(), 1); |
| 7446 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7447 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7448 |
| 7449 | bssl::UniquePtr<SSL_SESSION> session = CreateClientSessionForQUIC(); |
| 7450 | ASSERT_TRUE(session); |
| 7451 |
| 7452 | ASSERT_TRUE(CreateClientAndServer()); |
| 7453 | SSL_set_session(client_.get(), session.get()); |
| 7454 |
| 7455 | // Configure the certificate (re)verification to never complete. The client |
| 7456 | // handshake should pause. |
| 7457 | SSL_set_custom_verify( |
| 7458 | client_.get(), SSL_VERIFY_PEER, |
| 7459 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 7460 | return ssl_verify_retry; |
| 7461 | }); |
| 7462 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7463 | ASSERT_EQ(SSL_get_error(client_.get(), -1), |
| 7464 | SSL_ERROR_WANT_CERTIFICATE_VERIFY); |
| 7465 |
| 7466 | // The early data keys have not yet been released. |
| 7467 | EXPECT_FALSE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7468 |
| 7469 | // After the verification completes, the handshake progresses to the 0-RTT |
| 7470 | // point and releases keys. |
| 7471 | SSL_set_custom_verify( |
| 7472 | client_.get(), SSL_VERIFY_PEER, |
| 7473 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 7474 | return ssl_verify_ok; |
| 7475 | }); |
| 7476 | ASSERT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7477 | EXPECT_TRUE(SSL_in_early_data(client_.get())); |
| 7478 | EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_early_data)); |
| 7479 | } |
| 7480 |
| 7481 | // Test only releasing data to QUIC one byte at a time on request, to maximize |
| 7482 | // state machine pauses. Additionally, test that existing asynchronous callbacks |
| 7483 | // still work. |
| 7484 | TEST_F(QUICMethodTest, Async) { |
| 7485 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7486 |
| 7487 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7488 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7489 | ASSERT_TRUE(CreateClientAndServer()); |
| 7490 |
| 7491 | // Install an asynchronous certificate callback. |
| 7492 | bool cert_cb_ok = false; |
| 7493 | SSL_set_cert_cb( |
| 7494 | server_.get(), |
| 7495 | [](SSL *, void *arg) -> int { |
| 7496 | return *static_cast<bool *>(arg) ? 1 : -1; |
| 7497 | }, |
| 7498 | &cert_cb_ok); |
| 7499 |
| 7500 | for (;;) { |
| 7501 | int client_ret = SSL_do_handshake(client_.get()); |
| 7502 | if (client_ret != 1) { |
| 7503 | ASSERT_EQ(client_ret, -1); |
| 7504 | ASSERT_EQ(SSL_get_error(client_.get(), client_ret), SSL_ERROR_WANT_READ); |
| 7505 | ASSERT_TRUE(ProvideHandshakeData(client_.get(), 1)); |
| 7506 | } |
| 7507 |
| 7508 | int server_ret = SSL_do_handshake(server_.get()); |
| 7509 | if (server_ret != 1) { |
| 7510 | ASSERT_EQ(server_ret, -1); |
| 7511 | int ssl_err = SSL_get_error(server_.get(), server_ret); |
| 7512 | switch (ssl_err) { |
| 7513 | case SSL_ERROR_WANT_READ: |
| 7514 | ASSERT_TRUE(ProvideHandshakeData(server_.get(), 1)); |
| 7515 | break; |
| 7516 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 7517 | ASSERT_FALSE(cert_cb_ok); |
| 7518 | cert_cb_ok = true; |
| 7519 | break; |
| 7520 | default: |
| 7521 | FAIL() << "Unexpected SSL_get_error result: " << ssl_err; |
| 7522 | } |
| 7523 | } |
| 7524 |
| 7525 | if (client_ret == 1 && server_ret == 1) { |
| 7526 | break; |
| 7527 | } |
| 7528 | } |
| 7529 |
| 7530 | ExpectHandshakeSuccess(); |
| 7531 | } |
| 7532 |
| 7533 | // Test buffering write data until explicit flushes. |
| 7534 | TEST_F(QUICMethodTest, Buffered) { |
| 7535 | AllowOutOfOrderWrites(); |
| 7536 |
| 7537 | struct BufferedFlight { |
| 7538 | std::vector<uint8_t> data[kNumQUICLevels]; |
| 7539 | }; |
| 7540 | static UnownedSSLExData<BufferedFlight> buffered_flights; |
| 7541 |
| 7542 | auto add_handshake_data = [](SSL *ssl, enum ssl_encryption_level_t level, |
| 7543 | const uint8_t *data, size_t len) -> int { |
| 7544 | BufferedFlight *flight = buffered_flights.Get(ssl); |
| 7545 | flight->data[level].insert(flight->data[level].end(), data, data + len); |
| 7546 | return 1; |
| 7547 | }; |
| 7548 |
| 7549 | auto flush_flight = [](SSL *ssl) -> int { |
| 7550 | BufferedFlight *flight = buffered_flights.Get(ssl); |
| 7551 | for (size_t level = 0; level < kNumQUICLevels; level++) { |
| 7552 | if (!flight->data[level].empty()) { |
| 7553 | if (!TransportFromSSL(ssl)->WriteHandshakeData( |
| 7554 | static_cast<ssl_encryption_level_t>(level), |
| 7555 | flight->data[level])) { |
| 7556 | return 0; |
| 7557 | } |
| 7558 | flight->data[level].clear(); |
| 7559 | } |
| 7560 | } |
| 7561 | return 1; |
| 7562 | }; |
| 7563 |
| 7564 | SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7565 | quic_method.add_handshake_data = add_handshake_data; |
| 7566 | quic_method.flush_flight = flush_flight; |
| 7567 |
| 7568 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7569 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7570 | ASSERT_TRUE(CreateClientAndServer()); |
| 7571 |
| 7572 | BufferedFlight client_flight, server_flight; |
| 7573 | ASSERT_TRUE(buffered_flights.Set(client_.get(), &client_flight)); |
| 7574 | ASSERT_TRUE(buffered_flights.Set(server_.get(), &server_flight)); |
| 7575 |
| 7576 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7577 |
| 7578 | ExpectHandshakeSuccess(); |
| 7579 | } |
| 7580 |
| 7581 | // Test that excess data at one level is rejected. That is, if a single |
| 7582 | // |SSL_provide_quic_data| call included both ServerHello and |
| 7583 | // EncryptedExtensions in a single chunk, BoringSSL notices and rejects this on |
| 7584 | // key change. |
| 7585 | TEST_F(QUICMethodTest, ExcessProvidedData) { |
| 7586 | AllowOutOfOrderWrites(); |
| 7587 |
| 7588 | auto add_handshake_data = [](SSL *ssl, enum ssl_encryption_level_t level, |
| 7589 | const uint8_t *data, size_t len) -> int { |
| 7590 | // Switch everything to the initial level. |
| 7591 | return TransportFromSSL(ssl)->WriteHandshakeData(ssl_encryption_initial, |
| 7592 | Span(data, len)); |
| 7593 | }; |
| 7594 |
| 7595 | SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7596 | quic_method.add_handshake_data = add_handshake_data; |
| 7597 |
| 7598 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7599 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7600 | ASSERT_TRUE(CreateClientAndServer()); |
| 7601 |
| 7602 | // Send the ClientHello and ServerHello through Finished. |
| 7603 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7604 | ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 7605 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7606 | ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 7607 | ASSERT_EQ(SSL_get_error(server_.get(), -1), SSL_ERROR_WANT_READ); |
| 7608 |
| 7609 | // The client is still waiting for the ServerHello at initial |
| 7610 | // encryption. |
| 7611 | ASSERT_EQ(ssl_encryption_initial, SSL_quic_read_level(client_.get())); |
| 7612 |
| 7613 | // |add_handshake_data| incorrectly wrote everything at the initial level, so |
| 7614 | // this queues up ServerHello through Finished in one chunk. |
| 7615 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7616 |
| 7617 | // The client reads ServerHello successfully, but then rejects the buffered |
| 7618 | // EncryptedExtensions on key change. |
| 7619 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7620 | ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_SSL); |
| 7621 | EXPECT_TRUE( |
| 7622 | ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_EXCESS_HANDSHAKE_DATA)); |
| 7623 |
| 7624 | // The client sends an alert in response to this. The alert is sent at |
| 7625 | // handshake level because we install write secrets before read secrets and |
| 7626 | // the error is discovered when installing the read secret. (How to send |
| 7627 | // alerts on protocol syntax errors near key changes is ambiguous in general.) |
| 7628 | ASSERT_TRUE(transport_->client()->has_alert()); |
| 7629 | EXPECT_EQ(transport_->client()->alert_level(), ssl_encryption_handshake); |
| 7630 | EXPECT_EQ(transport_->client()->alert(), SSL_AD_UNEXPECTED_MESSAGE); |
| 7631 |
| 7632 | // Sanity-check handshake secrets. The error is discovered while setting the |
| 7633 | // read secret, so only the write secret has been installed. |
| 7634 | EXPECT_TRUE(transport_->client()->HasWriteSecret(ssl_encryption_handshake)); |
| 7635 | EXPECT_FALSE(transport_->client()->HasReadSecret(ssl_encryption_handshake)); |
| 7636 | } |
| 7637 |
| 7638 | // Test that |SSL_provide_quic_data| will reject data at the wrong level. |
| 7639 | TEST_F(QUICMethodTest, ProvideWrongLevel) { |
| 7640 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7641 |
| 7642 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7643 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7644 | ASSERT_TRUE(CreateClientAndServer()); |
| 7645 |
| 7646 | // Send the ClientHello and ServerHello through Finished. |
| 7647 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7648 | ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 7649 | ASSERT_TRUE(ProvideHandshakeData(server_.get())); |
| 7650 | ASSERT_EQ(SSL_do_handshake(server_.get()), -1); |
| 7651 | ASSERT_EQ(SSL_get_error(server_.get(), -1), SSL_ERROR_WANT_READ); |
| 7652 |
| 7653 | // The client is still waiting for the ServerHello at initial |
| 7654 | // encryption. |
| 7655 | ASSERT_EQ(ssl_encryption_initial, SSL_quic_read_level(client_.get())); |
| 7656 |
| 7657 | // Data cannot be provided at the next level. |
| 7658 | std::vector<uint8_t> data; |
| 7659 | ASSERT_TRUE( |
| 7660 | transport_->client()->ReadHandshakeData(&data, ssl_encryption_initial)); |
| 7661 | ASSERT_FALSE(SSL_provide_quic_data(client_.get(), ssl_encryption_handshake, |
| 7662 | data.data(), data.size())); |
| 7663 | ERR_clear_error(); |
| 7664 |
| 7665 | // Progress to EncryptedExtensions. |
| 7666 | ASSERT_TRUE(SSL_provide_quic_data(client_.get(), ssl_encryption_initial, |
| 7667 | data.data(), data.size())); |
| 7668 | ASSERT_EQ(SSL_do_handshake(client_.get()), -1); |
| 7669 | ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_WANT_READ); |
| 7670 | ASSERT_EQ(ssl_encryption_handshake, SSL_quic_read_level(client_.get())); |
| 7671 |
| 7672 | // Data cannot be provided at the previous level. |
| 7673 | ASSERT_TRUE( |
| 7674 | transport_->client()->ReadHandshakeData(&data, ssl_encryption_handshake)); |
| 7675 | ASSERT_FALSE(SSL_provide_quic_data(client_.get(), ssl_encryption_initial, |
| 7676 | data.data(), data.size())); |
| 7677 | } |
| 7678 |
| 7679 | TEST_F(QUICMethodTest, TooMuchData) { |
| 7680 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7681 |
| 7682 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7683 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7684 | ASSERT_TRUE(CreateClientAndServer()); |
| 7685 |
| 7686 | size_t limit = |
| 7687 | SSL_quic_max_handshake_flight_len(client_.get(), ssl_encryption_initial); |
| 7688 | uint8_t b = 0; |
| 7689 | for (size_t i = 0; i < limit; i++) { |
| 7690 | ASSERT_TRUE( |
| 7691 | SSL_provide_quic_data(client_.get(), ssl_encryption_initial, &b, 1)); |
| 7692 | } |
| 7693 |
| 7694 | EXPECT_FALSE( |
| 7695 | SSL_provide_quic_data(client_.get(), ssl_encryption_initial, &b, 1)); |
| 7696 | } |
| 7697 |
| 7698 | // Provide invalid post-handshake data. |
| 7699 | TEST_F(QUICMethodTest, BadPostHandshake) { |
| 7700 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7701 |
| 7702 | g_last_session = nullptr; |
| 7703 |
| 7704 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7705 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 7706 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7707 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7708 | ASSERT_TRUE(CreateClientAndServer()); |
| 7709 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7710 |
| 7711 | EXPECT_EQ(SSL_do_handshake(client_.get()), 1); |
| 7712 | EXPECT_EQ(SSL_do_handshake(server_.get()), 1); |
| 7713 | EXPECT_TRUE(transport_->SecretsMatch(ssl_encryption_application)); |
| 7714 | EXPECT_FALSE(transport_->client()->has_alert()); |
| 7715 | EXPECT_FALSE(transport_->server()->has_alert()); |
| 7716 |
| 7717 | // Junk sent as part of post-handshake data should cause an error. |
| 7718 | uint8_t kJunk[] = {0x17, 0x0, 0x0, 0x4, 0xB, 0xE, 0xE, 0xF}; |
| 7719 | ASSERT_TRUE(SSL_provide_quic_data(client_.get(), ssl_encryption_application, |
| 7720 | kJunk, sizeof(kJunk))); |
| 7721 | EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 0); |
| 7722 | } |
| 7723 |
| 7724 | static void ExpectReceivedTransportParamsEqual(const SSL *ssl, |
| 7725 | Span<const uint8_t> expected) { |
| 7726 | const uint8_t *received; |
| 7727 | size_t received_len; |
| 7728 | SSL_get_peer_quic_transport_params(ssl, &received, &received_len); |
| 7729 | ASSERT_EQ(received_len, expected.size()); |
| 7730 | EXPECT_EQ(Bytes(received, received_len), Bytes(expected)); |
| 7731 | } |
| 7732 |
| 7733 | TEST_F(QUICMethodTest, SetTransportParameters) { |
| 7734 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7735 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7736 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7737 |
| 7738 | ASSERT_TRUE(CreateClientAndServer()); |
| 7739 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7740 | uint8_t kServerParams[] = {5, 6, 7}; |
| 7741 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7742 | sizeof(kClientParams))); |
| 7743 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 7744 | sizeof(kServerParams))); |
| 7745 |
| 7746 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7747 | ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 7748 | ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 7749 | } |
| 7750 |
| 7751 | TEST_F(QUICMethodTest, SetTransportParamsInCallback) { |
| 7752 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7753 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7754 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7755 |
| 7756 | ASSERT_TRUE(CreateClientAndServer()); |
| 7757 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7758 | static uint8_t kServerParams[] = {5, 6, 7}; |
| 7759 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7760 | sizeof(kClientParams))); |
| 7761 | SSL_CTX_set_tlsext_servername_callback( |
| 7762 | server_ctx_.get(), [](SSL *ssl, int *out_alert, void *arg) -> int { |
| 7763 | EXPECT_TRUE(SSL_set_quic_transport_params(ssl, kServerParams, |
| 7764 | sizeof(kServerParams))); |
| 7765 | return SSL_TLSEXT_ERR_OK; |
| 7766 | }); |
| 7767 |
| 7768 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7769 | ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 7770 | ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 7771 | } |
| 7772 |
| 7773 | TEST_F(QUICMethodTest, ForbidCrossProtocolResumptionClient) { |
| 7774 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7775 |
| 7776 | g_last_session = nullptr; |
| 7777 |
| 7778 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7779 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 7780 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7781 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7782 |
| 7783 | ASSERT_TRUE(CreateClientAndServer()); |
| 7784 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7785 |
| 7786 | ExpectHandshakeSuccess(); |
| 7787 | EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 7788 | EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 7789 |
| 7790 | // The server sent NewSessionTicket messages in the handshake. |
| 7791 | EXPECT_FALSE(g_last_session); |
| 7792 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7793 | EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 7794 | ASSERT_TRUE(g_last_session); |
| 7795 |
| 7796 | // Pretend that g_last_session came from a TLS-over-TCP connection. |
| 7797 | g_last_session->is_quic = false; |
| 7798 |
| 7799 | // Create a second connection and verify that resumption does not occur with |
| 7800 | // a session from a non-QUIC connection. This tests that the client does not |
| 7801 | // offer over QUIC a session believed to be received over TCP. The server |
| 7802 | // believes this is a QUIC session, so if the client offered the session, the |
| 7803 | // server would have resumed it. |
| 7804 | ASSERT_TRUE(CreateClientAndServer()); |
| 7805 | bssl::UniquePtr<SSL_SESSION> session = std::move(g_last_session); |
| 7806 | SSL_set_session(client_.get(), session.get()); |
| 7807 |
| 7808 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7809 | ExpectHandshakeSuccess(); |
| 7810 | EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 7811 | EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 7812 | } |
| 7813 |
| 7814 | TEST_F(QUICMethodTest, ForbidCrossProtocolResumptionServer) { |
| 7815 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7816 |
| 7817 | g_last_session = nullptr; |
| 7818 |
| 7819 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 7820 | SSL_CTX_sess_set_new_cb(client_ctx_.get(), SaveLastSession); |
| 7821 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7822 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7823 |
| 7824 | ASSERT_TRUE(CreateClientAndServer()); |
| 7825 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7826 |
| 7827 | ExpectHandshakeSuccess(); |
| 7828 | EXPECT_FALSE(SSL_session_reused(client_.get())); |
| 7829 | EXPECT_FALSE(SSL_session_reused(server_.get())); |
| 7830 |
| 7831 | // The server sent NewSessionTicket messages in the handshake. |
| 7832 | EXPECT_FALSE(g_last_session); |
| 7833 | ASSERT_TRUE(ProvideHandshakeData(client_.get())); |
| 7834 | EXPECT_EQ(SSL_process_quic_post_handshake(client_.get()), 1); |
| 7835 | ASSERT_TRUE(g_last_session); |
| 7836 |
| 7837 | // Attempt a resumption with g_last_session using TLS_method. |
| 7838 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 7839 | ASSERT_TRUE(client_ctx); |
| 7840 |
| 7841 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), nullptr)); |
| 7842 |
| 7843 | bssl::UniquePtr<SSL> client(SSL_new(client_ctx.get())), |
| 7844 | server(SSL_new(server_ctx_.get())); |
| 7845 | ASSERT_TRUE(client); |
| 7846 | ASSERT_TRUE(server); |
| 7847 | SSL_set_connect_state(client.get()); |
| 7848 | SSL_set_accept_state(server.get()); |
| 7849 |
| 7850 | // The TLS-over-TCP client will refuse to resume with a quic session, so |
| 7851 | // mark is_quic = false to bypass the client check to test the server check. |
| 7852 | g_last_session->is_quic = false; |
| 7853 | SSL_set_session(client.get(), g_last_session.get()); |
| 7854 |
| 7855 | BIO *bio1, *bio2; |
| 7856 | ASSERT_TRUE(BIO_new_bio_pair(&bio1, 0, &bio2, 0)); |
| 7857 |
| 7858 | // SSL_set_bio takes ownership. |
| 7859 | SSL_set_bio(client.get(), bio1, bio1); |
| 7860 | SSL_set_bio(server.get(), bio2, bio2); |
| 7861 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 7862 |
| 7863 | EXPECT_FALSE(SSL_session_reused(client.get())); |
| 7864 | EXPECT_FALSE(SSL_session_reused(server.get())); |
| 7865 | } |
| 7866 |
| 7867 | TEST_F(QUICMethodTest, ClientRejectsMissingTransportParams) { |
| 7868 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7869 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7870 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7871 |
| 7872 | ASSERT_TRUE(CreateClientAndServer()); |
| 7873 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), nullptr, 0)); |
| 7874 | ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 7875 | } |
| 7876 |
| 7877 | TEST_F(QUICMethodTest, ServerRejectsMissingTransportParams) { |
| 7878 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7879 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7880 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7881 |
| 7882 | ASSERT_TRUE(CreateClientAndServer()); |
| 7883 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), nullptr, 0)); |
| 7884 | ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kClientError)); |
| 7885 | } |
| 7886 |
| 7887 | TEST_F(QUICMethodTest, QuicLegacyCodepointEnabled) { |
| 7888 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7889 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7890 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7891 |
| 7892 | ASSERT_TRUE(CreateClientAndServer()); |
| 7893 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7894 | uint8_t kServerParams[] = {5, 6, 7}; |
| 7895 | SSL_set_quic_use_legacy_codepoint(client_.get(), 1); |
| 7896 | SSL_set_quic_use_legacy_codepoint(server_.get(), 1); |
| 7897 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7898 | sizeof(kClientParams))); |
| 7899 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 7900 | sizeof(kServerParams))); |
| 7901 |
| 7902 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7903 | ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 7904 | ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 7905 | } |
| 7906 |
| 7907 | TEST_F(QUICMethodTest, QuicLegacyCodepointDisabled) { |
| 7908 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7909 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7910 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7911 |
| 7912 | ASSERT_TRUE(CreateClientAndServer()); |
| 7913 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7914 | uint8_t kServerParams[] = {5, 6, 7}; |
| 7915 | SSL_set_quic_use_legacy_codepoint(client_.get(), 0); |
| 7916 | SSL_set_quic_use_legacy_codepoint(server_.get(), 0); |
| 7917 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7918 | sizeof(kClientParams))); |
| 7919 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 7920 | sizeof(kServerParams))); |
| 7921 |
| 7922 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7923 | ExpectReceivedTransportParamsEqual(client_.get(), kServerParams); |
| 7924 | ExpectReceivedTransportParamsEqual(server_.get(), kClientParams); |
| 7925 | } |
| 7926 |
| 7927 | TEST_F(QUICMethodTest, QuicLegacyCodepointClientOnly) { |
| 7928 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7929 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7930 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7931 |
| 7932 | ASSERT_TRUE(CreateClientAndServer()); |
| 7933 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7934 | uint8_t kServerParams[] = {5, 6, 7}; |
| 7935 | SSL_set_quic_use_legacy_codepoint(client_.get(), 1); |
| 7936 | SSL_set_quic_use_legacy_codepoint(server_.get(), 0); |
| 7937 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7938 | sizeof(kClientParams))); |
| 7939 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 7940 | sizeof(kServerParams))); |
| 7941 |
| 7942 | ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 7943 | } |
| 7944 |
| 7945 | TEST_F(QUICMethodTest, QuicLegacyCodepointServerOnly) { |
| 7946 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7947 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7948 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7949 |
| 7950 | ASSERT_TRUE(CreateClientAndServer()); |
| 7951 | uint8_t kClientParams[] = {1, 2, 3, 4}; |
| 7952 | uint8_t kServerParams[] = {5, 6, 7}; |
| 7953 | SSL_set_quic_use_legacy_codepoint(client_.get(), 0); |
| 7954 | SSL_set_quic_use_legacy_codepoint(server_.get(), 1); |
| 7955 | ASSERT_TRUE(SSL_set_quic_transport_params(client_.get(), kClientParams, |
| 7956 | sizeof(kClientParams))); |
| 7957 | ASSERT_TRUE(SSL_set_quic_transport_params(server_.get(), kServerParams, |
| 7958 | sizeof(kServerParams))); |
| 7959 |
| 7960 | ASSERT_TRUE(RunQUICHandshakesAndExpectError(ExpectedError::kServerError)); |
| 7961 | } |
| 7962 |
| 7963 | // Test that the default QUIC code point is consistent with |
| 7964 | // |TLSEXT_TYPE_quic_transport_parameters|. This test ensures we remember to |
| 7965 | // update the two values together. |
| 7966 | TEST_F(QUICMethodTest, QuicCodePointDefault) { |
| 7967 | const SSL_QUIC_METHOD quic_method = DefaultQUICMethod(); |
| 7968 | ASSERT_TRUE(SSL_CTX_set_quic_method(client_ctx_.get(), &quic_method)); |
| 7969 | ASSERT_TRUE(SSL_CTX_set_quic_method(server_ctx_.get(), &quic_method)); |
| 7970 | SSL_CTX_set_select_certificate_cb( |
| 7971 | server_ctx_.get(), |
| 7972 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 7973 | const uint8_t *data; |
| 7974 | size_t len; |
| 7975 | if (!SSL_early_callback_ctx_extension_get( |
| 7976 | client_hello, TLSEXT_TYPE_quic_transport_parameters, &data, |
| 7977 | &len)) { |
| 7978 | ADD_FAILURE() << "Could not find quic_transport_parameters extension"; |
| 7979 | return ssl_select_cert_error; |
| 7980 | } |
| 7981 | return ssl_select_cert_success; |
| 7982 | }); |
| 7983 |
| 7984 | ASSERT_TRUE(CreateClientAndServer()); |
| 7985 | ASSERT_TRUE(CompleteHandshakesForQUIC()); |
| 7986 | } |
| 7987 |
| 7988 | extern "C" { |
| 7989 | int BORINGSSL_enum_c_type_test(void); |
| 7990 | } |
| 7991 |
| 7992 | TEST(SSLTest, EnumTypes) { |
| 7993 | EXPECT_EQ(sizeof(int), sizeof(ssl_private_key_result_t)); |
| 7994 | EXPECT_EQ(1, BORINGSSL_enum_c_type_test()); |
| 7995 | } |
| 7996 |
| 7997 | TEST_P(SSLVersionTest, DoubleSSLError) { |
| 7998 | // Connect the inner SSL connections. |
| 7999 | ASSERT_TRUE(Connect()); |
| 8000 |
| 8001 | // Make a pair of |BIO|s which wrap |client_| and |server_|. |
| 8002 | UniquePtr<BIO_METHOD> bio_method(BIO_meth_new(0, nullptr)); |
| 8003 | ASSERT_TRUE(bio_method); |
| 8004 | ASSERT_TRUE(BIO_meth_set_read( |
| 8005 | bio_method.get(), [](BIO *bio, char *out, int len) -> int { |
| 8006 | SSL *ssl = static_cast<SSL *>(BIO_get_data(bio)); |
| 8007 | int ret = SSL_read(ssl, out, len); |
| 8008 | int ssl_ret = SSL_get_error(ssl, ret); |
| 8009 | if (ssl_ret == SSL_ERROR_WANT_READ) { |
| 8010 | BIO_set_retry_read(bio); |
| 8011 | } |
| 8012 | return ret; |
| 8013 | })); |
| 8014 | ASSERT_TRUE(BIO_meth_set_write( |
| 8015 | bio_method.get(), [](BIO *bio, const char *in, int len) -> int { |
| 8016 | SSL *ssl = static_cast<SSL *>(BIO_get_data(bio)); |
| 8017 | int ret = SSL_write(ssl, in, len); |
| 8018 | int ssl_ret = SSL_get_error(ssl, ret); |
| 8019 | if (ssl_ret == SSL_ERROR_WANT_WRITE) { |
| 8020 | BIO_set_retry_write(bio); |
| 8021 | } |
| 8022 | return ret; |
| 8023 | })); |
| 8024 | ASSERT_TRUE(BIO_meth_set_ctrl( |
| 8025 | bio_method.get(), [](BIO *bio, int cmd, long larg, void *parg) -> long { |
| 8026 | // |SSL| objects require |BIO_flush| support. |
| 8027 | if (cmd == BIO_CTRL_FLUSH) { |
| 8028 | return 1; |
| 8029 | } |
| 8030 | return 0; |
| 8031 | })); |
| 8032 |
| 8033 | UniquePtr<BIO> client_bio(BIO_new(bio_method.get())); |
| 8034 | ASSERT_TRUE(client_bio); |
| 8035 | BIO_set_data(client_bio.get(), client_.get()); |
| 8036 | BIO_set_init(client_bio.get(), 1); |
| 8037 |
| 8038 | UniquePtr<BIO> server_bio(BIO_new(bio_method.get())); |
| 8039 | ASSERT_TRUE(server_bio); |
| 8040 | BIO_set_data(server_bio.get(), server_.get()); |
| 8041 | BIO_set_init(server_bio.get(), 1); |
| 8042 |
| 8043 | // Wrap the inner connections in another layer of SSL. |
| 8044 | UniquePtr<SSL> client_outer(SSL_new(client_ctx_.get())); |
| 8045 | ASSERT_TRUE(client_outer); |
| 8046 | SSL_set_connect_state(client_outer.get()); |
| 8047 | SSL_set_bio(client_outer.get(), client_bio.get(), client_bio.get()); |
| 8048 | client_bio.release(); // |SSL_set_bio| takes ownership. |
| 8049 |
| 8050 | UniquePtr<SSL> server_outer(SSL_new(server_ctx_.get())); |
| 8051 | ASSERT_TRUE(server_outer); |
| 8052 | SSL_set_accept_state(server_outer.get()); |
| 8053 | SSL_set_bio(server_outer.get(), server_bio.get(), server_bio.get()); |
| 8054 | server_bio.release(); // |SSL_set_bio| takes ownership. |
| 8055 |
| 8056 | // Configure |client_outer| to reject the server certificate. |
| 8057 | SSL_set_custom_verify( |
| 8058 | client_outer.get(), SSL_VERIFY_PEER, |
| 8059 | [](SSL *ssl, uint8_t *out_alert) -> ssl_verify_result_t { |
| 8060 | return ssl_verify_invalid; |
| 8061 | }); |
| 8062 |
| 8063 | for (;;) { |
| 8064 | int client_ret = SSL_do_handshake(client_outer.get()); |
| 8065 | int client_err = SSL_get_error(client_outer.get(), client_ret); |
| 8066 | if (client_err != SSL_ERROR_WANT_READ && |
| 8067 | client_err != SSL_ERROR_WANT_WRITE) { |
| 8068 | // The client handshake should terminate on a certificate verification |
| 8069 | // error. |
| 8070 | EXPECT_EQ(SSL_ERROR_SSL, client_err); |
| 8071 | EXPECT_TRUE(ErrorEquals(ERR_peek_error(), ERR_LIB_SSL, |
| 8072 | SSL_R_CERTIFICATE_VERIFY_FAILED)); |
| 8073 | break; |
| 8074 | } |
| 8075 |
| 8076 | // Run the server handshake and continue. |
| 8077 | int server_ret = SSL_do_handshake(server_outer.get()); |
| 8078 | int server_err = SSL_get_error(server_outer.get(), server_ret); |
| 8079 | ASSERT_TRUE(server_err == SSL_ERROR_NONE || |
| 8080 | server_err == SSL_ERROR_WANT_READ || |
| 8081 | server_err == SSL_ERROR_WANT_WRITE); |
| 8082 | } |
| 8083 | } |
| 8084 |
| 8085 | TEST_P(SSLVersionTest, SameKeyResume) { |
| 8086 | uint8_t key[48]; |
| 8087 | RAND_bytes(key, sizeof(key)); |
| 8088 |
| 8089 | bssl::UniquePtr<SSL_CTX> server_ctx2 = CreateContext(); |
| 8090 | ASSERT_TRUE(server_ctx2); |
| 8091 | ASSERT_TRUE(UseCertAndKey(server_ctx2.get())); |
| 8092 | ASSERT_TRUE( |
| 8093 | SSL_CTX_set_tlsext_ticket_keys(server_ctx_.get(), key, sizeof(key))); |
| 8094 | ASSERT_TRUE( |
| 8095 | SSL_CTX_set_tlsext_ticket_keys(server_ctx2.get(), key, sizeof(key))); |
| 8096 |
| 8097 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8098 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8099 | SSL_CTX_set_session_cache_mode(server_ctx2.get(), SSL_SESS_CACHE_BOTH); |
| 8100 |
| 8101 | // Establish a session for |server_ctx_|. |
| 8102 | bssl::UniquePtr<SSL_SESSION> session = |
| 8103 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 8104 | ASSERT_TRUE(session); |
| 8105 | ClientConfig config; |
| 8106 | config.session = session.get(); |
| 8107 |
| 8108 | // Resuming with |server_ctx_| again works. |
| 8109 | bssl::UniquePtr<SSL> client, server; |
| 8110 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8111 | server_ctx_.get(), config)); |
| 8112 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8113 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 8114 |
| 8115 | // Resuming with |server_ctx2| also works. |
| 8116 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8117 | server_ctx2.get(), config)); |
| 8118 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8119 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 8120 | } |
| 8121 |
| 8122 | TEST_P(SSLVersionTest, DifferentKeyNoResume) { |
| 8123 | uint8_t key1[48], key2[48]; |
| 8124 | RAND_bytes(key1, sizeof(key1)); |
| 8125 | RAND_bytes(key2, sizeof(key2)); |
| 8126 |
| 8127 | bssl::UniquePtr<SSL_CTX> server_ctx2 = CreateContext(); |
| 8128 | ASSERT_TRUE(server_ctx2); |
| 8129 | ASSERT_TRUE(UseCertAndKey(server_ctx2.get())); |
| 8130 | ASSERT_TRUE( |
| 8131 | SSL_CTX_set_tlsext_ticket_keys(server_ctx_.get(), key1, sizeof(key1))); |
| 8132 | ASSERT_TRUE( |
| 8133 | SSL_CTX_set_tlsext_ticket_keys(server_ctx2.get(), key2, sizeof(key2))); |
| 8134 |
| 8135 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8136 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8137 | SSL_CTX_set_session_cache_mode(server_ctx2.get(), SSL_SESS_CACHE_BOTH); |
| 8138 |
| 8139 | // Establish a session for |server_ctx_|. |
| 8140 | bssl::UniquePtr<SSL_SESSION> session = |
| 8141 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 8142 | ASSERT_TRUE(session); |
| 8143 | ClientConfig config; |
| 8144 | config.session = session.get(); |
| 8145 |
| 8146 | // Resuming with |server_ctx_| again works. |
| 8147 | bssl::UniquePtr<SSL> client, server; |
| 8148 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8149 | server_ctx_.get(), config)); |
| 8150 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8151 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 8152 |
| 8153 | // Resuming with |server_ctx2| does not work. |
| 8154 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8155 | server_ctx2.get(), config)); |
| 8156 | EXPECT_FALSE(SSL_session_reused(client.get())); |
| 8157 | EXPECT_FALSE(SSL_session_reused(server.get())); |
| 8158 | } |
| 8159 |
| 8160 | TEST_P(SSLVersionTest, UnrelatedServerNoResume) { |
| 8161 | bssl::UniquePtr<SSL_CTX> server_ctx2 = CreateContext(); |
| 8162 | ASSERT_TRUE(server_ctx2); |
| 8163 | ASSERT_TRUE(UseCertAndKey(server_ctx2.get())); |
| 8164 |
| 8165 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8166 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8167 | SSL_CTX_set_session_cache_mode(server_ctx2.get(), SSL_SESS_CACHE_BOTH); |
| 8168 |
| 8169 | // Establish a session for |server_ctx_|. |
| 8170 | bssl::UniquePtr<SSL_SESSION> session = |
| 8171 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 8172 | ASSERT_TRUE(session); |
| 8173 | ClientConfig config; |
| 8174 | config.session = session.get(); |
| 8175 |
| 8176 | // Resuming with |server_ctx_| again works. |
| 8177 | bssl::UniquePtr<SSL> client, server; |
| 8178 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8179 | server_ctx_.get(), config)); |
| 8180 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8181 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 8182 |
| 8183 | // Resuming with |server_ctx2| does not work. |
| 8184 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8185 | server_ctx2.get(), config)); |
| 8186 | EXPECT_FALSE(SSL_session_reused(client.get())); |
| 8187 | EXPECT_FALSE(SSL_session_reused(server.get())); |
| 8188 | } |
| 8189 |
| 8190 | Span<const uint8_t> SessionIDOf(const SSL *ssl) { |
| 8191 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 8192 | unsigned len; |
| 8193 | const uint8_t *data = SSL_SESSION_get_id(session, &len); |
| 8194 | return Span(data, len); |
| 8195 | } |
| 8196 |
| 8197 | TEST_P(SSLVersionTest, TicketSessionIDsMatch) { |
| 8198 | // This checks that the session IDs at client and server match after a ticket |
| 8199 | // resumption. It's unclear whether this should be true, but Envoy depends |
| 8200 | // on it in their tests so this will give an early signal if we break it. |
| 8201 | SSL_CTX_set_session_cache_mode(client_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8202 | SSL_CTX_set_session_cache_mode(server_ctx_.get(), SSL_SESS_CACHE_BOTH); |
| 8203 |
| 8204 | bssl::UniquePtr<SSL_SESSION> session = |
| 8205 | CreateClientSession(client_ctx_.get(), server_ctx_.get()); |
| 8206 |
| 8207 | bssl::UniquePtr<SSL> client, server; |
| 8208 | ClientConfig config; |
| 8209 | config.session = session.get(); |
| 8210 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_.get(), |
| 8211 | server_ctx_.get(), config)); |
| 8212 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8213 | EXPECT_TRUE(SSL_session_reused(server.get())); |
| 8214 |
| 8215 | EXPECT_EQ(Bytes(SessionIDOf(client.get())), Bytes(SessionIDOf(server.get()))); |
| 8216 | } |
| 8217 |
| 8218 | static void WriteHelloRequest(SSL *server) { |
| 8219 | // This function assumes TLS 1.2 with ChaCha20-Poly1305. |
| 8220 | ASSERT_EQ(SSL_version(server), TLS1_2_VERSION); |
| 8221 | ASSERT_EQ(SSL_CIPHER_get_cipher_nid(SSL_get_current_cipher(server)), |
| 8222 | NID_chacha20_poly1305); |
| 8223 |
| 8224 | // Encrypt a HelloRequest. |
| 8225 | uint8_t in[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0}; |
| 8226 | // Extract key material from |server|. |
| 8227 | static const size_t kKeyLen = 32; |
| 8228 | static const size_t kNonceLen = 12; |
| 8229 | ASSERT_EQ(2u * (kKeyLen + kNonceLen), SSL_get_key_block_len(server)); |
| 8230 | uint8_t key_block[2u * (kKeyLen + kNonceLen)]; |
| 8231 | ASSERT_TRUE(SSL_generate_key_block(server, key_block, sizeof(key_block))); |
| 8232 | Span<uint8_t> key = Span(key_block).subspan(kKeyLen, kKeyLen); |
| 8233 | Span<uint8_t> nonce = |
| 8234 | Span(key_block).subspan(kKeyLen + kKeyLen + kNonceLen, kNonceLen); |
| 8235 |
| 8236 | uint8_t ad[13]; |
| 8237 | uint64_t seq = SSL_get_write_sequence(server); |
| 8238 | for (size_t i = 0; i < 8; i++) { |
| 8239 | // The nonce is XORed with the sequence number. |
| 8240 | nonce[11 - i] ^= uint8_t(seq); |
| 8241 | ad[7 - i] = uint8_t(seq); |
| 8242 | seq >>= 8; |
| 8243 | } |
| 8244 |
| 8245 | ad[8] = SSL3_RT_HANDSHAKE; |
| 8246 | ad[9] = 3; |
| 8247 | ad[10] = 3; // TLS 1.2 |
| 8248 | ad[11] = 0; |
| 8249 | ad[12] = sizeof(in); |
| 8250 |
| 8251 | uint8_t record[5 + sizeof(in) + 16]; |
| 8252 | record[0] = SSL3_RT_HANDSHAKE; |
| 8253 | record[1] = 3; |
| 8254 | record[2] = 3; // TLS 1.2 |
| 8255 | record[3] = 0; |
| 8256 | record[4] = sizeof(record) - 5; |
| 8257 |
| 8258 | ScopedEVP_AEAD_CTX aead; |
| 8259 | ASSERT_TRUE(EVP_AEAD_CTX_init(aead.get(), EVP_aead_chacha20_poly1305(), |
| 8260 | key.data(), key.size(), |
| 8261 | EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)); |
| 8262 | size_t len; |
| 8263 | ASSERT_TRUE(EVP_AEAD_CTX_seal(aead.get(), record + 5, &len, |
| 8264 | sizeof(record) - 5, nonce.data(), nonce.size(), |
| 8265 | in, sizeof(in), ad, sizeof(ad))); |
| 8266 | ASSERT_EQ(sizeof(record) - 5, len); |
| 8267 |
| 8268 | ASSERT_EQ(int(sizeof(record)), |
| 8269 | BIO_write(SSL_get_wbio(server), record, sizeof(record))); |
| 8270 | } |
| 8271 |
| 8272 | TEST(SSLTest, WriteWhileExplicitRenegotiate) { |
| 8273 | bssl::UniquePtr<SSL_CTX> ctx(CreateContextWithTestCertificate(TLS_method())); |
| 8274 | ASSERT_TRUE(ctx); |
| 8275 |
| 8276 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 8277 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 8278 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 8279 | ctx.get(), "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")); |
| 8280 |
| 8281 | bssl::UniquePtr<SSL> client, server; |
| 8282 | ASSERT_TRUE(CreateClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 8283 | SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_explicit); |
| 8284 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 8285 |
| 8286 | static const uint8_t kInput[] = {'h', 'e', 'l', 'l', 'o'}; |
| 8287 |
| 8288 | // Write "hello" until the buffer is full, so |client| has a pending write. |
| 8289 | size_t num_writes = 0; |
| 8290 | for (;;) { |
| 8291 | int ret = SSL_write(client.get(), kInput, sizeof(kInput)); |
| 8292 | if (ret != int(sizeof(kInput))) { |
| 8293 | ASSERT_EQ(-1, ret); |
| 8294 | ASSERT_EQ(SSL_ERROR_WANT_WRITE, SSL_get_error(client.get(), ret)); |
| 8295 | break; |
| 8296 | } |
| 8297 | num_writes++; |
| 8298 | } |
| 8299 |
| 8300 | ASSERT_NO_FATAL_FAILURE(WriteHelloRequest(server.get())); |
| 8301 |
| 8302 | // |SSL_read| should pick up the HelloRequest. |
| 8303 | uint8_t byte; |
| 8304 | ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 8305 | ASSERT_EQ(SSL_ERROR_WANT_RENEGOTIATE, SSL_get_error(client.get(), -1)); |
| 8306 |
| 8307 | // Drain the data from the |client|. |
| 8308 | uint8_t buf[sizeof(kInput)]; |
| 8309 | for (size_t i = 0; i < num_writes; i++) { |
| 8310 | ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 8311 | EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 8312 | } |
| 8313 |
| 8314 | // |client| should be able to finish the pending write and continue to write, |
| 8315 | // despite the paused HelloRequest. |
| 8316 | ASSERT_EQ(int(sizeof(kInput)), |
| 8317 | SSL_write(client.get(), kInput, sizeof(kInput))); |
| 8318 | ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 8319 | EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 8320 |
| 8321 | ASSERT_EQ(int(sizeof(kInput)), |
| 8322 | SSL_write(client.get(), kInput, sizeof(kInput))); |
| 8323 | ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 8324 | EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 8325 |
| 8326 | // |SSL_read| is stuck until we acknowledge the HelloRequest. |
| 8327 | ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 8328 | ASSERT_EQ(SSL_ERROR_WANT_RENEGOTIATE, SSL_get_error(client.get(), -1)); |
| 8329 |
| 8330 | ASSERT_TRUE(SSL_renegotiate(client.get())); |
| 8331 | ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 8332 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 8333 |
| 8334 | // We never renegotiate as a server. |
| 8335 | ASSERT_EQ(-1, SSL_read(server.get(), buf, sizeof(buf))); |
| 8336 | ASSERT_EQ(SSL_ERROR_SSL, SSL_get_error(server.get(), -1)); |
| 8337 | EXPECT_TRUE( |
| 8338 | ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION)); |
| 8339 | } |
| 8340 |
| 8341 | TEST(SSLTest, ConnectionPropertiesDuringRenegotiate) { |
| 8342 | // Configure known connection properties, so we can check against them. |
| 8343 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 8344 | ASSERT_TRUE(ctx); |
| 8345 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 8346 | ASSERT_TRUE(cert); |
| 8347 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 8348 | ASSERT_TRUE(key); |
| 8349 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 8350 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 8351 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 8352 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 8353 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 8354 | ctx.get(), "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")); |
| 8355 | ASSERT_TRUE(SSL_CTX_set1_groups_list(ctx.get(), "X25519")); |
| 8356 | ASSERT_TRUE(SSL_CTX_set1_sigalgs_list(ctx.get(), "rsa_pkcs1_sha256")); |
| 8357 |
| 8358 | // Connect a client and server that accept renegotiation. |
| 8359 | bssl::UniquePtr<SSL> client, server; |
| 8360 | ASSERT_TRUE(CreateClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 8361 | SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely); |
| 8362 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 8363 |
| 8364 | auto check_properties = [&] { |
| 8365 | EXPECT_EQ(SSL_version(client.get()), TLS1_2_VERSION); |
| 8366 | const SSL_CIPHER *cipher = SSL_get_current_cipher(client.get()); |
| 8367 | ASSERT_TRUE(cipher); |
| 8368 | EXPECT_EQ(SSL_CIPHER_get_id(cipher), |
| 8369 | uint32_t{TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256}); |
| 8370 | EXPECT_EQ(SSL_get_group_id(client.get()), SSL_GROUP_X25519); |
| 8371 | EXPECT_EQ(SSL_get_negotiated_group(client.get()), NID_X25519); |
| 8372 | EXPECT_EQ(SSL_get_peer_signature_algorithm(client.get()), |
| 8373 | SSL_SIGN_RSA_PKCS1_SHA256); |
| 8374 | bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(client.get())); |
| 8375 | ASSERT_TRUE(peer); |
| 8376 | EXPECT_EQ(X509_cmp(cert.get(), peer.get()), 0); |
| 8377 | }; |
| 8378 | check_properties(); |
| 8379 |
| 8380 | // The server sends a HelloRequest. |
| 8381 | ASSERT_NO_FATAL_FAILURE(WriteHelloRequest(server.get())); |
| 8382 |
| 8383 | // Reading from the client will consume the HelloRequest, start a |
| 8384 | // renegotiation, and then block on a ServerHello from the server. |
| 8385 | uint8_t byte; |
| 8386 | ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 8387 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 8388 |
| 8389 | // Connection properties should continue to report values from the original |
| 8390 | // handshake. |
| 8391 | check_properties(); |
| 8392 | } |
| 8393 |
| 8394 | TEST(SSLTest, CopyWithoutEarlyData) { |
| 8395 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 8396 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8397 | CreateContextWithTestCertificate(TLS_method())); |
| 8398 | ASSERT_TRUE(client_ctx); |
| 8399 | ASSERT_TRUE(server_ctx); |
| 8400 |
| 8401 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 8402 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 8403 | SSL_CTX_set_early_data_enabled(client_ctx.get(), 1); |
| 8404 | SSL_CTX_set_early_data_enabled(server_ctx.get(), 1); |
| 8405 |
| 8406 | bssl::UniquePtr<SSL_SESSION> session = |
| 8407 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 8408 | ASSERT_TRUE(session); |
| 8409 |
| 8410 | // The client should attempt early data with |session|. |
| 8411 | bssl::UniquePtr<SSL> client, server; |
| 8412 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8413 | server_ctx.get())); |
| 8414 | SSL_set_session(client.get(), session.get()); |
| 8415 | SSL_set_early_data_enabled(client.get(), 1); |
| 8416 | ASSERT_EQ(1, SSL_do_handshake(client.get())); |
| 8417 | EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 8418 |
| 8419 | // |SSL_SESSION_copy_without_early_data| should disable early data but |
| 8420 | // still resume the session. |
| 8421 | bssl::UniquePtr<SSL_SESSION> session2( |
| 8422 | SSL_SESSION_copy_without_early_data(session.get())); |
| 8423 | ASSERT_TRUE(session2); |
| 8424 | EXPECT_NE(session.get(), session2.get()); |
| 8425 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8426 | server_ctx.get())); |
| 8427 | SSL_set_session(client.get(), session2.get()); |
| 8428 | SSL_set_early_data_enabled(client.get(), 1); |
| 8429 | EXPECT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 8430 | EXPECT_TRUE(SSL_session_reused(client.get())); |
| 8431 | EXPECT_EQ(ssl_early_data_unsupported_for_session, |
| 8432 | SSL_get_early_data_reason(client.get())); |
| 8433 |
| 8434 | // |SSL_SESSION_copy_without_early_data| should be a reference count increase |
| 8435 | // when passed an early-data-incapable session. |
| 8436 | bssl::UniquePtr<SSL_SESSION> session3( |
| 8437 | SSL_SESSION_copy_without_early_data(session2.get())); |
| 8438 | EXPECT_EQ(session2.get(), session3.get()); |
| 8439 | } |
| 8440 |
| 8441 | TEST(SSLTest, ProcessTLS13NewSessionTicket) { |
| 8442 | // Configure client and server to negotiate TLS 1.3 only. |
| 8443 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 8444 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8445 | CreateContextWithTestCertificate(TLS_method())); |
| 8446 | ASSERT_TRUE(client_ctx); |
| 8447 | ASSERT_TRUE(server_ctx); |
| 8448 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 8449 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 8450 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 8451 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 8452 |
| 8453 | bssl::UniquePtr<SSL> client, server; |
| 8454 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 8455 | server_ctx.get())); |
| 8456 | EXPECT_EQ(TLS1_3_VERSION, SSL_version(client.get())); |
| 8457 |
| 8458 | // Process a TLS 1.3 NewSessionTicket. |
| 8459 | static const uint8_t kTicket[] = { |
| 8460 | 0x04, 0x00, 0x00, 0xb2, 0x00, 0x02, 0xa3, 0x00, 0x04, 0x03, 0x02, 0x01, |
| 8461 | 0x01, 0x00, 0x00, 0xa0, 0x01, 0x06, 0x09, 0x11, 0x16, 0x19, 0x21, 0x26, |
| 8462 | 0x29, 0x31, 0x36, 0x39, 0x41, 0x46, 0x49, 0x51, 0x03, 0x06, 0x09, 0x13, |
| 8463 | 0x16, 0x19, 0x23, 0x26, 0x29, 0x33, 0x36, 0x39, 0x43, 0x46, 0x49, 0x53, |
| 8464 | 0xf7, 0x00, 0x29, 0xec, 0xf2, 0xc4, 0xa4, 0x41, 0xfc, 0x30, 0x17, 0x2e, |
| 8465 | 0x9f, 0x7c, 0xa8, 0xaf, 0x75, 0x70, 0xf0, 0x1f, 0xc7, 0x98, 0xf7, 0xcf, |
| 8466 | 0x5a, 0x5a, 0x6b, 0x5b, 0xfe, 0xf1, 0xe7, 0x3a, 0xe8, 0xf7, 0x6c, 0xd2, |
| 8467 | 0xa8, 0xa6, 0x92, 0x5b, 0x96, 0x8d, 0xde, 0xdb, 0xd3, 0x20, 0x6a, 0xcb, |
| 8468 | 0x69, 0x06, 0xf4, 0x91, 0x85, 0x2e, 0xe6, 0x5e, 0x0c, 0x59, 0xf2, 0x9e, |
| 8469 | 0x9b, 0x79, 0x91, 0x24, 0x7e, 0x4a, 0x32, 0x3d, 0xbe, 0x4b, 0x80, 0x70, |
| 8470 | 0xaf, 0xd0, 0x1d, 0xe2, 0xca, 0x05, 0x35, 0x09, 0x09, 0x05, 0x0f, 0xbb, |
| 8471 | 0xc4, 0xae, 0xd7, 0xc4, 0xed, 0xd7, 0xae, 0x35, 0xc8, 0x73, 0x63, 0x78, |
| 8472 | 0x64, 0xc9, 0x7a, 0x1f, 0xed, 0x7a, 0x9a, 0x47, 0x44, 0xfd, 0x50, 0xf7, |
| 8473 | 0xb7, 0xe0, 0x64, 0xa9, 0x02, 0xc1, 0x5c, 0x23, 0x18, 0x3f, 0xc4, 0xcf, |
| 8474 | 0x72, 0x02, 0x59, 0x2d, 0xe1, 0xaa, 0x61, 0x72, 0x00, 0x04, 0x5a, 0x5a, |
| 8475 | 0x00, 0x00, |
| 8476 | }; |
| 8477 | bssl::UniquePtr<SSL_SESSION> session(SSL_process_tls13_new_session_ticket( |
| 8478 | client.get(), kTicket, sizeof(kTicket))); |
| 8479 | ASSERT_TRUE(session); |
| 8480 | ASSERT_TRUE(SSL_SESSION_has_ticket(session.get())); |
| 8481 |
| 8482 | uint8_t *session_buf = nullptr; |
| 8483 | size_t session_length = 0; |
| 8484 | ASSERT_TRUE( |
| 8485 | SSL_SESSION_to_bytes(session.get(), &session_buf, &session_length)); |
| 8486 | bssl::UniquePtr<uint8_t> session_buf_free(session_buf); |
| 8487 | ASSERT_TRUE(session_buf); |
| 8488 | ASSERT_GT(session_length, 0u); |
| 8489 |
| 8490 | // Servers cannot call |SSL_process_tls13_new_session_ticket|. |
| 8491 | ASSERT_FALSE(SSL_process_tls13_new_session_ticket(server.get(), kTicket, |
| 8492 | sizeof(kTicket))); |
| 8493 |
| 8494 | // Clients cannot call |SSL_process_tls13_new_session_ticket| before the |
| 8495 | // handshake completes. |
| 8496 | bssl::UniquePtr<SSL> client2(SSL_new(client_ctx.get())); |
| 8497 | ASSERT_TRUE(client2); |
| 8498 | SSL_set_connect_state(client2.get()); |
| 8499 | ASSERT_FALSE(SSL_process_tls13_new_session_ticket(client2.get(), kTicket, |
| 8500 | sizeof(kTicket))); |
| 8501 | } |
| 8502 |
| 8503 | TEST(SSLTest, BIO) { |
| 8504 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 8505 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8506 | CreateContextWithTestCertificate(TLS_method())); |
| 8507 | ASSERT_TRUE(client_ctx); |
| 8508 | ASSERT_TRUE(server_ctx); |
| 8509 |
| 8510 | for (bool take_ownership : {true, false}) { |
| 8511 | // For simplicity, get the handshake out of the way first. |
| 8512 | bssl::UniquePtr<SSL> client, server; |
| 8513 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 8514 | server_ctx.get())); |
| 8515 |
| 8516 | // Wrap |client| in an SSL BIO. |
| 8517 | bssl::UniquePtr<BIO> client_bio(BIO_new(BIO_f_ssl())); |
| 8518 | ASSERT_TRUE(client_bio); |
| 8519 | ASSERT_EQ(1, BIO_set_ssl(client_bio.get(), client.get(), take_ownership)); |
| 8520 | if (take_ownership) { |
| 8521 | client.release(); |
| 8522 | } |
| 8523 |
| 8524 | // Flushing the BIO should not crash. |
| 8525 | EXPECT_EQ(1, BIO_flush(client_bio.get())); |
| 8526 |
| 8527 | // Exchange some data. |
| 8528 | EXPECT_EQ(5, BIO_write(client_bio.get(), "hello", 5)); |
| 8529 | uint8_t buf[5]; |
| 8530 | ASSERT_EQ(5, SSL_read(server.get(), buf, sizeof(buf))); |
| 8531 | EXPECT_EQ(Bytes("hello"), Bytes(buf)); |
| 8532 |
| 8533 | EXPECT_EQ(5, SSL_write(server.get(), "world", 5)); |
| 8534 | ASSERT_EQ(5, BIO_read(client_bio.get(), buf, sizeof(buf))); |
| 8535 | EXPECT_EQ(Bytes("world"), Bytes(buf)); |
| 8536 |
| 8537 | // |BIO_should_read| should work. |
| 8538 | EXPECT_EQ(-1, BIO_read(client_bio.get(), buf, sizeof(buf))); |
| 8539 | EXPECT_TRUE(BIO_should_read(client_bio.get())); |
| 8540 |
| 8541 | // Writing data should eventually exceed the buffer size and fail, reporting |
| 8542 | // |BIO_should_write|. |
| 8543 | int ret; |
| 8544 | for (int i = 0; i < 1024; i++) { |
| 8545 | const uint8_t kZeros[1024] = {0}; |
| 8546 | ret = BIO_write(client_bio.get(), kZeros, sizeof(kZeros)); |
| 8547 | if (ret <= 0) { |
| 8548 | break; |
| 8549 | } |
| 8550 | } |
| 8551 | EXPECT_EQ(-1, ret); |
| 8552 | EXPECT_TRUE(BIO_should_write(client_bio.get())); |
| 8553 | } |
| 8554 | } |
| 8555 |
| 8556 | TEST(SSLTest, ALPNConfig) { |
| 8557 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 8558 | ASSERT_TRUE(ctx); |
| 8559 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 8560 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 8561 | ASSERT_TRUE(cert); |
| 8562 | ASSERT_TRUE(key); |
| 8563 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 8564 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 8565 |
| 8566 | // Set up some machinery to check the configured ALPN against what is actually |
| 8567 | // sent over the wire. Note that the ALPN callback is only called when the |
| 8568 | // client offers ALPN. |
| 8569 | std::vector<uint8_t> observed_alpn; |
| 8570 | SSL_CTX_set_alpn_select_cb( |
| 8571 | ctx.get(), |
| 8572 | [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 8573 | unsigned in_len, void *arg) -> int { |
| 8574 | std::vector<uint8_t> *observed_alpn_ptr = |
| 8575 | static_cast<std::vector<uint8_t> *>(arg); |
| 8576 | observed_alpn_ptr->assign(in, in + in_len); |
| 8577 | return SSL_TLSEXT_ERR_NOACK; |
| 8578 | }, |
| 8579 | &observed_alpn); |
| 8580 | auto check_alpn_proto = [&](Span<const uint8_t> expected) { |
| 8581 | observed_alpn.clear(); |
| 8582 | bssl::UniquePtr<SSL> client, server; |
| 8583 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 8584 | EXPECT_EQ(Bytes(expected), Bytes(observed_alpn)); |
| 8585 | }; |
| 8586 |
| 8587 | // Note that |SSL_CTX_set_alpn_protos|'s return value is reversed. |
| 8588 | static const uint8_t kValidList[] = {0x03, 'f', 'o', 'o', |
| 8589 | 0x03, 'b', 'a', 'r'}; |
| 8590 | EXPECT_EQ(0, |
| 8591 | SSL_CTX_set_alpn_protos(ctx.get(), kValidList, sizeof(kValidList))); |
| 8592 | check_alpn_proto(kValidList); |
| 8593 |
| 8594 | // Invalid lists are rejected. |
| 8595 | static const uint8_t kInvalidList[] = {0x04, 'f', 'o', 'o'}; |
| 8596 | EXPECT_EQ(1, SSL_CTX_set_alpn_protos(ctx.get(), kInvalidList, |
| 8597 | sizeof(kInvalidList))); |
| 8598 |
| 8599 | // Empty lists are valid and are interpreted as disabling ALPN. |
| 8600 | EXPECT_EQ(0, SSL_CTX_set_alpn_protos(ctx.get(), nullptr, 0)); |
| 8601 | check_alpn_proto({}); |
| 8602 | } |
| 8603 |
| 8604 | // This is a basic unit-test class to verify completing handshake successfully, |
| 8605 | // sending the correct codepoint extension and having correct application |
| 8606 | // setting on different combination of ALPS codepoint settings. More integration |
| 8607 | // tests on runner.go. |
| 8608 | class AlpsNewCodepointTest : public testing::Test { |
| 8609 | protected: |
| 8610 | void SetUp() override { |
| 8611 | client_ctx_.reset(SSL_CTX_new(TLS_method())); |
| 8612 | server_ctx_ = CreateContextWithTestCertificate(TLS_method()); |
| 8613 | ASSERT_TRUE(client_ctx_); |
| 8614 | ASSERT_TRUE(server_ctx_); |
| 8615 | } |
| 8616 |
| 8617 | void SetUpApplicationSetting() { |
| 8618 | static const uint8_t alpn[] = {0x03, 'f', 'o', 'o'}; |
| 8619 | static const uint8_t proto[] = {'f', 'o', 'o'}; |
| 8620 | static const uint8_t alps[] = {0x04, 'a', 'l', 'p', 's'}; |
| 8621 | // SSL_set_alpn_protos's return value is backwards. It returns zero on |
| 8622 | // success and one on failure. |
| 8623 | ASSERT_FALSE(SSL_set_alpn_protos(client_.get(), alpn, sizeof(alpn))); |
| 8624 | SSL_CTX_set_alpn_select_cb( |
| 8625 | server_ctx_.get(), |
| 8626 | [](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 8627 | unsigned in_len, void *arg) -> int { |
| 8628 | return SSL_select_next_proto(const_cast<uint8_t **>(out), out_len, in, |
| 8629 | in_len, alpn, |
| 8630 | sizeof(alpn)) == OPENSSL_NPN_NEGOTIATED |
| 8631 | ? SSL_TLSEXT_ERR_OK |
| 8632 | : SSL_TLSEXT_ERR_NOACK; |
| 8633 | }, |
| 8634 | nullptr); |
| 8635 | ASSERT_TRUE(SSL_add_application_settings(client_.get(), proto, |
| 8636 | sizeof(proto), nullptr, 0)); |
| 8637 | ASSERT_TRUE(SSL_add_application_settings( |
| 8638 | server_.get(), proto, sizeof(proto), alps, sizeof(alps))); |
| 8639 | } |
| 8640 |
| 8641 | bssl::UniquePtr<SSL_CTX> client_ctx_; |
| 8642 | bssl::UniquePtr<SSL_CTX> server_ctx_; |
| 8643 |
| 8644 | bssl::UniquePtr<SSL> client_; |
| 8645 | bssl::UniquePtr<SSL> server_; |
| 8646 | }; |
| 8647 |
| 8648 | TEST_F(AlpsNewCodepointTest, Enabled) { |
| 8649 | SetUpExpectedNewCodePoint(server_ctx_.get()); |
| 8650 |
| 8651 | ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 8652 | server_ctx_.get())); |
| 8653 |
| 8654 | SSL_set_alps_use_new_codepoint(client_.get(), 1); |
| 8655 | SSL_set_alps_use_new_codepoint(server_.get(), 1); |
| 8656 |
| 8657 | SetUpApplicationSetting(); |
| 8658 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 8659 | ASSERT_TRUE(SSL_has_application_settings(client_.get())); |
| 8660 | } |
| 8661 |
| 8662 | TEST_F(AlpsNewCodepointTest, Disabled) { |
| 8663 | // Both client and server disable alps new codepoint. |
| 8664 | SetUpExpectedOldCodePoint(server_ctx_.get()); |
| 8665 |
| 8666 | ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 8667 | server_ctx_.get())); |
| 8668 |
| 8669 | SSL_set_alps_use_new_codepoint(client_.get(), 0); |
| 8670 | SSL_set_alps_use_new_codepoint(server_.get(), 0); |
| 8671 |
| 8672 | SetUpApplicationSetting(); |
| 8673 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 8674 | ASSERT_TRUE(SSL_has_application_settings(client_.get())); |
| 8675 | } |
| 8676 |
| 8677 | TEST_F(AlpsNewCodepointTest, ClientOnly) { |
| 8678 | // If client set new codepoint but server doesn't set, server ignores it. |
| 8679 | SetUpExpectedNewCodePoint(server_ctx_.get()); |
| 8680 |
| 8681 | ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 8682 | server_ctx_.get())); |
| 8683 |
| 8684 | SSL_set_alps_use_new_codepoint(client_.get(), 1); |
| 8685 | SSL_set_alps_use_new_codepoint(server_.get(), 0); |
| 8686 |
| 8687 | SetUpApplicationSetting(); |
| 8688 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 8689 | ASSERT_FALSE(SSL_has_application_settings(client_.get())); |
| 8690 | } |
| 8691 |
| 8692 | TEST_F(AlpsNewCodepointTest, ServerOnly) { |
| 8693 | // If client doesn't set new codepoint, while server set. |
| 8694 | SetUpExpectedOldCodePoint(server_ctx_.get()); |
| 8695 |
| 8696 | ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 8697 | server_ctx_.get())); |
| 8698 |
| 8699 | SSL_set_alps_use_new_codepoint(client_.get(), 0); |
| 8700 | SSL_set_alps_use_new_codepoint(server_.get(), 1); |
| 8701 |
| 8702 | SetUpApplicationSetting(); |
| 8703 | ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 8704 | ASSERT_FALSE(SSL_has_application_settings(client_.get())); |
| 8705 | } |
| 8706 |
| 8707 | // Test that the key usage checker can correctly handle issuerUID and |
| 8708 | // subjectUID. See https://crbug.com/1199744. |
| 8709 | TEST(SSLTest, KeyUsageWithUIDs) { |
| 8710 | static const char kGoodKeyUsage[] = R"( |
| 8711 | -----BEGIN CERTIFICATE----- |
| 8712 | MIIB7DCCAZOgAwIBAgIJANlMBNpJfb/rMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT |
| 8713 | AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn |
| 8714 | aXRzIFB0eSBMdGQwHhcNMTQwNDIzMjMyMTU3WhcNMTQwNTIzMjMyMTU3WjBFMQsw |
| 8715 | CQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu |
| 8716 | ZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE5itp |
| 8717 | 4r9ln5e+Lx4NlIpM1Zdrt6keDUb73ampHp3culoB59aXqAoY+cPEox5W4nyDSNsW |
| 8718 | Ghz1HX7xlC1Lz3IiwYEEABI0VoIEABI0VqNgMF4wHQYDVR0OBBYEFKuE0qyrlfCC |
| 8719 | ThZ4B1VXX+QmjYLRMB8GA1UdIwQYMBaAFKuE0qyrlfCCThZ4B1VXX+QmjYLRMA4G |
| 8720 | A1UdDwEB/wQEAwIHgDAMBgNVHRMEBTADAQH/MAoGCCqGSM49BAMCA0cAMEQCIEWJ |
| 8721 | 34EcqW5MHwLIA1hZ2Tj/jV2QjN02KLxis9mFsqDKAiAMlMTkzsM51vVs9Ohqa+Rc |
| 8722 | 4Z7qDhjIhiF4dM0uEDYRVA== |
| 8723 | -----END CERTIFICATE----- |
| 8724 | )"; |
| 8725 | static const char kBadKeyUsage[] = R"( |
| 8726 | -----BEGIN CERTIFICATE----- |
| 8727 | MIIB7jCCAZOgAwIBAgIJANlMBNpJfb/rMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT |
| 8728 | AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn |
| 8729 | aXRzIFB0eSBMdGQwHhcNMTQwNDIzMjMyMTU3WhcNMTQwNTIzMjMyMTU3WjBFMQsw |
| 8730 | CQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJu |
| 8731 | ZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE5itp |
| 8732 | 4r9ln5e+Lx4NlIpM1Zdrt6keDUb73ampHp3culoB59aXqAoY+cPEox5W4nyDSNsW |
| 8733 | Ghz1HX7xlC1Lz3IiwYEEABI0VoIEABI0VqNgMF4wHQYDVR0OBBYEFKuE0qyrlfCC |
| 8734 | ThZ4B1VXX+QmjYLRMB8GA1UdIwQYMBaAFKuE0qyrlfCCThZ4B1VXX+QmjYLRMA4G |
| 8735 | A1UdDwEB/wQEAwIDCDAMBgNVHRMEBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQC6 |
| 8736 | taYBUDu2gcZC6EMk79FBHArYI0ucF+kzvETegZCbBAIhANtObFec5gtso/47moPD |
| 8737 | RHrQbWsFUakETXL9QMlegh5t |
| 8738 | -----END CERTIFICATE----- |
| 8739 | )"; |
| 8740 |
| 8741 | bssl::UniquePtr<X509> good = CertFromPEM(kGoodKeyUsage); |
| 8742 | ASSERT_TRUE(good); |
| 8743 | bssl::UniquePtr<X509> bad = CertFromPEM(kBadKeyUsage); |
| 8744 | ASSERT_TRUE(bad); |
| 8745 |
| 8746 | // We check key usage when configuring EC certificates to distinguish ECDSA |
| 8747 | // and ECDH. |
| 8748 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 8749 | ASSERT_TRUE(ctx); |
| 8750 | EXPECT_TRUE(SSL_CTX_use_certificate(ctx.get(), good.get())); |
| 8751 | EXPECT_FALSE(SSL_CTX_use_certificate(ctx.get(), bad.get())); |
| 8752 | } |
| 8753 |
| 8754 | // Test that |SSL_can_release_private_key| reports true as early as expected. |
| 8755 | // The internal asserts in the library check we do not report true too early. |
| 8756 | TEST(SSLTest, CanReleasePrivateKey) { |
| 8757 | bssl::UniquePtr<SSL_CTX> client_ctx = |
| 8758 | CreateContextWithTestCertificate(TLS_method()); |
| 8759 | ASSERT_TRUE(client_ctx); |
| 8760 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 8761 |
| 8762 | // Note this assumes the transport buffer is large enough to fit the client |
| 8763 | // and server first flights. We check this with |SSL_ERROR_WANT_READ|. If the |
| 8764 | // transport buffer was too small it would return |SSL_ERROR_WANT_WRITE|. |
| 8765 | auto check_first_server_round_trip = [&](SSL *client, SSL *server) { |
| 8766 | // Write the ClientHello. |
| 8767 | ASSERT_EQ(-1, SSL_do_handshake(client)); |
| 8768 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client, -1)); |
| 8769 |
| 8770 | // Consume the ClientHello and write the server flight. |
| 8771 | ASSERT_EQ(-1, SSL_do_handshake(server)); |
| 8772 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server, -1)); |
| 8773 |
| 8774 | EXPECT_TRUE(SSL_can_release_private_key(server)); |
| 8775 | }; |
| 8776 |
| 8777 | { |
| 8778 | SCOPED_TRACE("TLS 1.2 ECDHE"); |
| 8779 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8780 | CreateContextWithTestCertificate(TLS_method())); |
| 8781 | ASSERT_TRUE(server_ctx); |
| 8782 | ASSERT_TRUE( |
| 8783 | SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 8784 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 8785 | server_ctx.get(), "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); |
| 8786 | // Configure the server to request client certificates, so we can also test |
| 8787 | // the client half. |
| 8788 | SSL_CTX_set_custom_verify( |
| 8789 | server_ctx.get(), SSL_VERIFY_PEER, |
| 8790 | [](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 8791 | bssl::UniquePtr<SSL> client, server; |
| 8792 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8793 | server_ctx.get())); |
| 8794 | check_first_server_round_trip(client.get(), server.get()); |
| 8795 |
| 8796 | // Consume the server flight and write the client response. The client still |
| 8797 | // has a Finished message to consume but can also release its key early. |
| 8798 | ASSERT_EQ(-1, SSL_do_handshake(client.get())); |
| 8799 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 8800 | EXPECT_TRUE(SSL_can_release_private_key(client.get())); |
| 8801 |
| 8802 | // However, a client that has not disabled renegotiation can never release |
| 8803 | // the key. |
| 8804 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8805 | server_ctx.get())); |
| 8806 | SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely); |
| 8807 | check_first_server_round_trip(client.get(), server.get()); |
| 8808 | ASSERT_EQ(-1, SSL_do_handshake(client.get())); |
| 8809 | ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 8810 | EXPECT_FALSE(SSL_can_release_private_key(client.get())); |
| 8811 | } |
| 8812 |
| 8813 | { |
| 8814 | SCOPED_TRACE("TLS 1.2 resumption"); |
| 8815 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8816 | CreateContextWithTestCertificate(TLS_method())); |
| 8817 | ASSERT_TRUE(server_ctx); |
| 8818 | ASSERT_TRUE( |
| 8819 | SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 8820 | bssl::UniquePtr<SSL_SESSION> session = |
| 8821 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 8822 | ASSERT_TRUE(session); |
| 8823 | bssl::UniquePtr<SSL> client, server; |
| 8824 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8825 | server_ctx.get())); |
| 8826 | SSL_set_session(client.get(), session.get()); |
| 8827 | check_first_server_round_trip(client.get(), server.get()); |
| 8828 | } |
| 8829 |
| 8830 | { |
| 8831 | SCOPED_TRACE("TLS 1.3 1-RTT"); |
| 8832 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8833 | CreateContextWithTestCertificate(TLS_method())); |
| 8834 | ASSERT_TRUE(server_ctx); |
| 8835 | ASSERT_TRUE( |
| 8836 | SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 8837 | bssl::UniquePtr<SSL> client, server; |
| 8838 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8839 | server_ctx.get())); |
| 8840 | check_first_server_round_trip(client.get(), server.get()); |
| 8841 | } |
| 8842 |
| 8843 | { |
| 8844 | SCOPED_TRACE("TLS 1.3 resumption"); |
| 8845 | bssl::UniquePtr<SSL_CTX> server_ctx( |
| 8846 | CreateContextWithTestCertificate(TLS_method())); |
| 8847 | ASSERT_TRUE(server_ctx); |
| 8848 | ASSERT_TRUE( |
| 8849 | SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 8850 | bssl::UniquePtr<SSL_SESSION> session = |
| 8851 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 8852 | ASSERT_TRUE(session); |
| 8853 | bssl::UniquePtr<SSL> client, server; |
| 8854 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 8855 | server_ctx.get())); |
| 8856 | SSL_set_session(client.get(), session.get()); |
| 8857 | check_first_server_round_trip(client.get(), server.get()); |
| 8858 | } |
| 8859 | } |
| 8860 |
| 8861 | // GetExtensionOrder sets |*out| to the list of extensions a client attached to |
| 8862 | // |ctx| will send in the ClientHello. If |ech_keys| is non-null, the client |
| 8863 | // will offer ECH with the public component. If |decrypt_ech| is true, |*out| |
| 8864 | // will be set to the ClientHelloInner's extensions, rather than |
| 8865 | // ClientHelloOuter. |
| 8866 | static bool GetExtensionOrder(SSL_CTX *client_ctx, std::vector<uint16_t> *out, |
| 8867 | SSL_ECH_KEYS *ech_keys, bool decrypt_ech) { |
| 8868 | struct AppData { |
| 8869 | std::vector<uint16_t> *out; |
| 8870 | bool decrypt_ech; |
| 8871 | bool callback_done = false; |
| 8872 | }; |
| 8873 | AppData app_data; |
| 8874 | app_data.out = out; |
| 8875 | app_data.decrypt_ech = decrypt_ech; |
| 8876 |
| 8877 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 8878 | CreateContextWithTestCertificate(TLS_method()); |
| 8879 | if (!server_ctx || // |
| 8880 | !SSL_CTX_set_app_data(server_ctx.get(), &app_data) || |
| 8881 | (decrypt_ech && !SSL_CTX_set1_ech_keys(server_ctx.get(), ech_keys))) { |
| 8882 | return false; |
| 8883 | } |
| 8884 |
| 8885 | // Configure the server to record the ClientHello extension order. We use a |
| 8886 | // server rather than |GetClientHello| so it can decrypt ClientHelloInner. |
| 8887 | SSL_CTX_set_select_certificate_cb( |
| 8888 | server_ctx.get(), |
| 8889 | [](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 8890 | AppData *app_data_ptr = static_cast<AppData *>( |
| 8891 | SSL_CTX_get_app_data(SSL_get_SSL_CTX(client_hello->ssl))); |
| 8892 | EXPECT_EQ(app_data_ptr->decrypt_ech ? 1 : 0, |
| 8893 | SSL_ech_accepted(client_hello->ssl)); |
| 8894 |
| 8895 | app_data_ptr->out->clear(); |
| 8896 | CBS extensions; |
| 8897 | CBS_init(&extensions, client_hello->extensions, |
| 8898 | client_hello->extensions_len); |
| 8899 | while (CBS_len(&extensions)) { |
| 8900 | uint16_t type; |
| 8901 | CBS body; |
| 8902 | if (!CBS_get_u16(&extensions, &type) || |
| 8903 | !CBS_get_u16_length_prefixed(&extensions, &body)) { |
| 8904 | return ssl_select_cert_error; |
| 8905 | } |
| 8906 | app_data_ptr->out->push_back(type); |
| 8907 | } |
| 8908 |
| 8909 | // Don't bother completing the handshake. |
| 8910 | app_data_ptr->callback_done = true; |
| 8911 | return ssl_select_cert_error; |
| 8912 | }); |
| 8913 |
| 8914 | bssl::UniquePtr<SSL> client, server; |
| 8915 | if (!CreateClientAndServer(&client, &server, client_ctx, server_ctx.get()) || |
| 8916 | (ech_keys != nullptr && !InstallECHConfigList(client.get(), ech_keys))) { |
| 8917 | return false; |
| 8918 | } |
| 8919 |
| 8920 | // Run the handshake far enough to process the ClientHello. |
| 8921 | SSL_do_handshake(client.get()); |
| 8922 | SSL_do_handshake(server.get()); |
| 8923 | return app_data.callback_done; |
| 8924 | } |
| 8925 |
| 8926 | // Test that, when extension permutation is enabled, the ClientHello extension |
| 8927 | // order changes, both with and without ECH, and in both ClientHelloInner and |
| 8928 | // ClientHelloOuter. |
| 8929 | TEST(SSLTest, PermuteExtensions) { |
| 8930 | bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(); |
| 8931 | ASSERT_TRUE(keys); |
| 8932 | for (bool offer_ech : {false, true}) { |
| 8933 | SCOPED_TRACE(offer_ech); |
| 8934 | SSL_ECH_KEYS *maybe_keys = offer_ech ? keys.get() : nullptr; |
| 8935 | for (bool decrypt_ech : {false, true}) { |
| 8936 | SCOPED_TRACE(decrypt_ech); |
| 8937 | if (!offer_ech && decrypt_ech) { |
| 8938 | continue; |
| 8939 | } |
| 8940 |
| 8941 | // When extension permutation is disabled, the order should be consistent. |
| 8942 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 8943 | ASSERT_TRUE(ctx); |
| 8944 | std::vector<uint16_t> order1, order2; |
| 8945 | ASSERT_TRUE( |
| 8946 | GetExtensionOrder(ctx.get(), &order1, maybe_keys, decrypt_ech)); |
| 8947 | ASSERT_TRUE( |
| 8948 | GetExtensionOrder(ctx.get(), &order2, maybe_keys, decrypt_ech)); |
| 8949 | EXPECT_EQ(order1, order2); |
| 8950 |
| 8951 | ctx.reset(SSL_CTX_new(TLS_method())); |
| 8952 | ASSERT_TRUE(ctx); |
| 8953 | SSL_CTX_set_permute_extensions(ctx.get(), 1); |
| 8954 |
| 8955 | // When extension permutation is enabled, each ClientHello should have a |
| 8956 | // different order. |
| 8957 | // |
| 8958 | // This test is inherently flaky, so we run it multiple times. We send at |
| 8959 | // least five extensions by default from TLS 1.3: supported_versions, |
| 8960 | // key_share, supported_groups, psk_key_exchange_modes, and |
| 8961 | // signature_algorithms. That means the probability of a false negative is |
| 8962 | // at most 1/120. Repeating the test 14 times lowers false negative rate |
| 8963 | // to under 2^-96. |
| 8964 | ASSERT_TRUE( |
| 8965 | GetExtensionOrder(ctx.get(), &order1, maybe_keys, decrypt_ech)); |
| 8966 | EXPECT_GE(order1.size(), 5u); |
| 8967 | static const int kNumIterations = 14; |
| 8968 | bool passed = false; |
| 8969 | for (int i = 0; i < kNumIterations; i++) { |
| 8970 | ASSERT_TRUE( |
| 8971 | GetExtensionOrder(ctx.get(), &order2, maybe_keys, decrypt_ech)); |
| 8972 | if (order1 != order2) { |
| 8973 | passed = true; |
| 8974 | break; |
| 8975 | } |
| 8976 | } |
| 8977 | EXPECT_TRUE(passed) << "Extensions were not permuted"; |
| 8978 | } |
| 8979 | } |
| 8980 | } |
| 8981 |
| 8982 | TEST(SSLTest, HostMatching) { |
| 8983 | static const char kCertPEM[] = R"( |
| 8984 | -----BEGIN CERTIFICATE----- |
| 8985 | MIIB9jCCAZ2gAwIBAgIQeudG9R61BOxUvWkeVhU5DTAKBggqhkjOPQQDAjApMRAw |
| 8986 | DgYDVQQKEwdBY21lIENvMRUwEwYDVQQDEwxleGFtcGxlMy5jb20wHhcNMjExMjA2 |
| 8987 | MjA1NjU2WhcNMjIxMjA2MjA1NjU2WjApMRAwDgYDVQQKEwdBY21lIENvMRUwEwYD |
| 8988 | VQQDEwxleGFtcGxlMy5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAS7l2VO |
| 8989 | Bl2TjVm9WfGk24+hMbVFUNB+RVHWbCvFvNZAoWiIJ2z34RLGInyZvCZ8xLAvsuaW |
| 8990 | ULDDaoeDl1M0t4Hmo4GmMIGjMA4GA1UdDwEB/wQEAwIChDATBgNVHSUEDDAKBggr |
| 8991 | BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTTJWurcc1t+VPQBko3 |
| 8992 | Gsw6cbcWSTBMBgNVHREERTBDggxleGFtcGxlMS5jb22CDGV4YW1wbGUyLmNvbYIP |
| 8993 | YSouZXhhbXBsZTQuY29tgg4qLmV4YW1wbGU1LmNvbYcEAQIDBDAKBggqhkjOPQQD |
| 8994 | AgNHADBEAiAAv0ljHJGrgyzZDkG6XvNZ5ewxRfnXcZuD0Y7E4giCZgIgNK1qjilu |
| 8995 | 5DyVbfKeeJhOCtGxqE1dWLXyJBnoRomSYBY= |
| 8996 | -----END CERTIFICATE----- |
| 8997 | )"; |
| 8998 | bssl::UniquePtr<X509> cert(CertFromPEM(kCertPEM)); |
| 8999 | ASSERT_TRUE(cert); |
| 9000 | static const char kCertNoSANsPEM[] = R"( |
| 9001 | -----BEGIN CERTIFICATE----- |
| 9002 | MIIBqzCCAVGgAwIBAgIQeudG9R61BOxUvWkeVhU5DTAKBggqhkjOPQQDAjArMRIw |
| 9003 | EAYDVQQKEwlBY21lIENvIDIxFTATBgNVBAMTDGV4YW1wbGUzLmNvbTAeFw0yMTEy |
| 9004 | MDYyMDU2NTZaFw0yMjEyMDYyMDU2NTZaMCsxEjAQBgNVBAoTCUFjbWUgQ28gMjEV |
| 9005 | MBMGA1UEAxMMZXhhbXBsZTMuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE |
| 9006 | u5dlTgZdk41ZvVnxpNuPoTG1RVDQfkVR1mwrxbzWQKFoiCds9+ESxiJ8mbwmfMSw |
| 9007 | L7LmllCww2qHg5dTNLeB5qNXMFUwDgYDVR0PAQH/BAQDAgKEMBMGA1UdJQQMMAoG |
| 9008 | CCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFNMla6txzW35U9AG |
| 9009 | SjcazDpxtxZJMAoGCCqGSM49BAMCA0gAMEUCIG3YWGWtpVhbcGV7wFKQwTfmvwHW |
| 9010 | pw4qCFZlool4hCwsAiEA+2fc6NfSbNpFEtQkDOMJW2ANiScAVEmImNqPfb2klz4= |
| 9011 | -----END CERTIFICATE----- |
| 9012 | )"; |
| 9013 | bssl::UniquePtr<X509> cert_no_sans(CertFromPEM(kCertNoSANsPEM)); |
| 9014 | ASSERT_TRUE(cert_no_sans); |
| 9015 |
| 9016 | static const char kKeyPEM[] = R"( |
| 9017 | -----BEGIN PRIVATE KEY----- |
| 9018 | MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghsaSZhUzZAcQlLyJ |
| 9019 | MDuy7WPdyqNsAX9rmEP650LF/q2hRANCAAS7l2VOBl2TjVm9WfGk24+hMbVFUNB+ |
| 9020 | RVHWbCvFvNZAoWiIJ2z34RLGInyZvCZ8xLAvsuaWULDDaoeDl1M0t4Hm |
| 9021 | -----END PRIVATE KEY----- |
| 9022 | )"; |
| 9023 | bssl::UniquePtr<EVP_PKEY> key(KeyFromPEM(kKeyPEM)); |
| 9024 | ASSERT_TRUE(key); |
| 9025 |
| 9026 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9027 | ASSERT_TRUE(client_ctx); |
| 9028 | ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(client_ctx.get()), |
| 9029 | cert.get())); |
| 9030 | ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(client_ctx.get()), |
| 9031 | cert_no_sans.get())); |
| 9032 | SSL_CTX_set_verify(client_ctx.get(), |
| 9033 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 9034 | nullptr); |
| 9035 | X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(client_ctx.get()), |
| 9036 | X509_V_FLAG_NO_CHECK_TIME); |
| 9037 |
| 9038 | struct TestCase { |
| 9039 | X509 *cert; |
| 9040 | std::string hostname; |
| 9041 | unsigned flags; |
| 9042 | bool should_match; |
| 9043 | }; |
| 9044 | std::vector<TestCase> kTests = { |
| 9045 | // These two names are present as SANs in the certificate. |
| 9046 | {cert.get(), "example1.com", 0, true}, |
| 9047 | {cert.get(), "example2.com", 0, true}, |
| 9048 | // This is the CN of the certificate, but that shouldn't matter if a SAN |
| 9049 | // extension is present. |
| 9050 | {cert.get(), "example3.com", 0, false}, |
| 9051 | // If the SAN is not present, we, for now, look for DNS names in the CN. |
| 9052 | {cert_no_sans.get(), "example3.com", 0, true}, |
| 9053 | // ... but this can be turned off. |
| 9054 | {cert_no_sans.get(), "example3.com", X509_CHECK_FLAG_NEVER_CHECK_SUBJECT, |
| 9055 | false}, |
| 9056 | // a*.example4.com is a SAN, but is invalid. |
| 9057 | {cert.get(), "abc.example4.com", 0, false}, |
| 9058 | // *.example5.com is a SAN in the certificate, which is a normal and valid |
| 9059 | // wildcard. |
| 9060 | {cert.get(), "abc.example5.com", 0, true}, |
| 9061 | // This name is not present. |
| 9062 | {cert.get(), "notexample1.com", 0, false}, |
| 9063 | // The IPv4 address 1.2.3.4 is a SAN, but that shouldn't match against a |
| 9064 | // hostname that happens to be its textual representation. |
| 9065 | {cert.get(), "1.2.3.4", 0, false}, |
| 9066 | }; |
| 9067 |
| 9068 | for (const TestCase &test : kTests) { |
| 9069 | SCOPED_TRACE(test.hostname); |
| 9070 |
| 9071 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 9072 | ASSERT_TRUE(server_ctx); |
| 9073 | ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), test.cert)); |
| 9074 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())); |
| 9075 |
| 9076 | ClientConfig config; |
| 9077 | bssl::UniquePtr<SSL> client, server; |
| 9078 | config.verify_hostname = test.hostname; |
| 9079 | config.hostflags = test.flags; |
| 9080 | EXPECT_EQ(test.should_match, |
| 9081 | ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 9082 | server_ctx.get(), config)); |
| 9083 | } |
| 9084 | } |
| 9085 |
| 9086 | TEST(SSLTest, NumTickets) { |
| 9087 | bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 9088 | ASSERT_TRUE(server_ctx); |
| 9089 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9090 | ASSERT_TRUE(client_ctx); |
| 9091 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 9092 | ASSERT_TRUE(cert); |
| 9093 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 9094 | ASSERT_TRUE(key); |
| 9095 | ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), cert.get())); |
| 9096 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())); |
| 9097 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9098 |
| 9099 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9100 | static size_t ticket_count; |
| 9101 | SSL_CTX_sess_set_new_cb(client_ctx.get(), [](SSL *, SSL_SESSION *) -> int { |
| 9102 | ticket_count++; |
| 9103 | return 0; |
| 9104 | }); |
| 9105 |
| 9106 | auto count_tickets = [&]() -> size_t { |
| 9107 | ticket_count = 0; |
| 9108 | bssl::UniquePtr<SSL> client, server; |
| 9109 | if (!ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 9110 | server_ctx.get()) || |
| 9111 | !FlushNewSessionTickets(client.get(), server.get())) { |
| 9112 | ADD_FAILURE() << "Could not run handshake"; |
| 9113 | return 0; |
| 9114 | } |
| 9115 | return ticket_count; |
| 9116 | }; |
| 9117 |
| 9118 | // By default, we should send two tickets. |
| 9119 | EXPECT_EQ(count_tickets(), 2u); |
| 9120 |
| 9121 | for (size_t num_tickets : {0, 1, 2, 3, 4, 5}) { |
| 9122 | SCOPED_TRACE(num_tickets); |
| 9123 | ASSERT_TRUE(SSL_CTX_set_num_tickets(server_ctx.get(), num_tickets)); |
| 9124 | EXPECT_EQ(SSL_CTX_get_num_tickets(server_ctx.get()), num_tickets); |
| 9125 | EXPECT_EQ(count_tickets(), num_tickets); |
| 9126 | } |
| 9127 |
| 9128 | // Configuring too many tickets causes us to stop at some point. |
| 9129 | ASSERT_TRUE(SSL_CTX_set_num_tickets(server_ctx.get(), 100000)); |
| 9130 | EXPECT_EQ(SSL_CTX_get_num_tickets(server_ctx.get()), 16u); |
| 9131 | EXPECT_EQ(count_tickets(), 16u); |
| 9132 | } |
| 9133 |
| 9134 | TEST(SSLTest, CertSubjectsToStack) { |
| 9135 | const std::string kCert1 = R"( |
| 9136 | -----BEGIN CERTIFICATE----- |
| 9137 | MIIBzzCCAXagAwIBAgIJANlMBNpJfb/rMAkGByqGSM49BAEwRTELMAkGA1UEBhMC |
| 9138 | QVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdp |
| 9139 | dHMgUHR5IEx0ZDAeFw0xNDA0MjMyMzIxNTdaFw0xNDA1MjMyMzIxNTdaMEUxCzAJ |
| 9140 | BgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5l |
| 9141 | dCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATmK2ni |
| 9142 | v2Wfl74vHg2UikzVl2u3qR4NRvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYa |
| 9143 | HPUdfvGULUvPciLBo1AwTjAdBgNVHQ4EFgQUq4TSrKuV8IJOFngHVVdf5CaNgtEw |
| 9144 | HwYDVR0jBBgwFoAUq4TSrKuV8IJOFngHVVdf5CaNgtEwDAYDVR0TBAUwAwEB/zAJ |
| 9145 | BgcqhkjOPQQBA0gAMEUCIQDyoDVeUTo2w4J5m+4nUIWOcAZ0lVfSKXQA9L4Vh13E |
| 9146 | BwIgfB55FGohg/B6dGh5XxSZmmi08cueFV7mHzJSYV51yRQ= |
| 9147 | -----END CERTIFICATE----- |
| 9148 | )"; |
| 9149 | const std::vector<uint8_t> kName1 = { |
| 9150 | 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 9151 | 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 9152 | 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 9153 | 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 9154 | 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 9155 | 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64}; |
| 9156 | const std::string kCert2 = R"( |
| 9157 | -----BEGIN CERTIFICATE----- |
| 9158 | MIICXjCCAcegAwIBAgIIWjO48ufpunYwDQYJKoZIhvcNAQELBQAwNjEaMBgGA1UE |
| 9159 | ChMRQm9yaW5nU1NMIFRFU1RJTkcxGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAg |
| 9160 | Fw0xNTAxMDEwMDAwMDBaGA8yMTAwMDEwMTAwMDAwMFowMjEaMBgGA1UEChMRQm9y |
| 9161 | aW5nU1NMIFRFU1RJTkcxFDASBgNVBAMTC2V4YW1wbGUuY29tMIGfMA0GCSqGSIb3 |
| 9162 | DQEBAQUAA4GNADCBiQKBgQDD0U0ZYgqShJ7oOjsyNKyVXEHqeafmk/bAoPqY/h1c |
| 9163 | oPw2E8KmeqiUSoTPjG5IXSblOxcqpbAXgnjPzo8DI3GNMhAf8SYNYsoH7gc7Uy7j |
| 9164 | 5x8bUrisGnuTHqkqH6d4/e7ETJ7i3CpR8bvK16DggEvQTudLipz8FBHtYhFakfdh |
| 9165 | TwIDAQABo3cwdTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEG |
| 9166 | CCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwGQYDVR0OBBIEEKN5pvbur7mlXjeMEYA0 |
| 9167 | 4nUwGwYDVR0jBBQwEoAQjBpoqLV2211Xex+NFLIGozANBgkqhkiG9w0BAQsFAAOB |
| 9168 | gQBj/p+JChp//LnXWC1k121LM/ii7hFzQzMrt70bny406SGz9jAjaPOX4S3gt38y |
| 9169 | rhjpPukBlSzgQXFg66y6q5qp1nQTD1Cw6NkKBe9WuBlY3iYfmsf7WT8nhlT1CttU |
| 9170 | xNCwyMX9mtdXdQicOfNjIGUCD5OLV5PgHFPRKiHHioBAhg== |
| 9171 | -----END CERTIFICATE----- |
| 9172 | )"; |
| 9173 | const std::vector<uint8_t> kName2 = { |
| 9174 | 0x30, 0x32, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, |
| 9175 | 0x13, 0x11, 0x42, 0x6f, 0x72, 0x69, 0x6e, 0x67, 0x53, 0x53, 0x4c, |
| 9176 | 0x20, 0x54, 0x45, 0x53, 0x54, 0x49, 0x4e, 0x47, 0x31, 0x14, 0x30, |
| 9177 | 0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0b, 0x65, 0x78, 0x61, |
| 9178 | 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d}; |
| 9179 |
| 9180 | const struct { |
| 9181 | std::vector<std::vector<uint8_t>> existing; |
| 9182 | std::string pem; |
| 9183 | std::vector<std::vector<uint8_t>> expected; |
| 9184 | } kTests[] = { |
| 9185 | // Do nothing. |
| 9186 | {{}, "", {}}, |
| 9187 | // Append to an empty list, skipping duplicates. |
| 9188 | {{}, kCert1 + kCert2 + kCert1, {kName1, kName2}}, |
| 9189 | // One of the names was already present. |
| 9190 | {{kName1}, kCert1 + kCert2, {kName1, kName2}}, |
| 9191 | // Both names were already present. |
| 9192 | {{kName1, kName2}, kCert1 + kCert2, {kName1, kName2}}, |
| 9193 | // Preserve existing duplicates. |
| 9194 | {{kName1, kName2, kName2}, kCert1 + kCert2, {kName1, kName2, kName2}}, |
| 9195 | }; |
| 9196 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) { |
| 9197 | SCOPED_TRACE(i); |
| 9198 | const auto &t = kTests[i]; |
| 9199 |
| 9200 | bssl::UniquePtr<STACK_OF(X509_NAME)> stack(sk_X509_NAME_new_null()); |
| 9201 | ASSERT_TRUE(stack); |
| 9202 | for (const auto &name : t.existing) { |
| 9203 | const uint8_t *inp = name.data(); |
| 9204 | bssl::UniquePtr<X509_NAME> name_obj( |
| 9205 | d2i_X509_NAME(nullptr, &inp, name.size())); |
| 9206 | ASSERT_TRUE(name_obj); |
| 9207 | EXPECT_EQ(inp, name.data() + name.size()); |
| 9208 | ASSERT_TRUE(bssl::PushToStack(stack.get(), std::move(name_obj))); |
| 9209 | } |
| 9210 |
| 9211 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.pem.data(), t.pem.size())); |
| 9212 | ASSERT_TRUE(bio); |
| 9213 | ASSERT_TRUE(SSL_add_bio_cert_subjects_to_stack(stack.get(), bio.get())); |
| 9214 |
| 9215 | // The function should have left |stack|'s comparison function alone. |
| 9216 | EXPECT_EQ(nullptr, sk_X509_NAME_set_cmp_func(stack.get(), nullptr)); |
| 9217 |
| 9218 | std::vector<std::vector<uint8_t>> expected = t.expected, result; |
| 9219 | for (X509_NAME *name : stack.get()) { |
| 9220 | uint8_t *der = nullptr; |
| 9221 | int der_len = i2d_X509_NAME(name, &der); |
| 9222 | ASSERT_GE(der_len, 0); |
| 9223 | result.push_back(std::vector<uint8_t>(der, der + der_len)); |
| 9224 | OPENSSL_free(der); |
| 9225 | } |
| 9226 |
| 9227 | // |SSL_add_bio_cert_subjects_to_stack| does not return the output in a |
| 9228 | // well-defined order. |
| 9229 | std::sort(expected.begin(), expected.end()); |
| 9230 | std::sort(result.begin(), result.end()); |
| 9231 | EXPECT_EQ(result, expected); |
| 9232 | } |
| 9233 | } |
| 9234 |
| 9235 | TEST(SSLTest, EmptyClientCAList) { |
| 9236 | if (SkipTempFileTests()) { |
| 9237 | GTEST_SKIP(); |
| 9238 | } |
| 9239 |
| 9240 | TemporaryFile empty; |
| 9241 | ASSERT_TRUE(empty.Init()); |
| 9242 | bssl::UniquePtr<STACK_OF(X509_NAME)> names( |
| 9243 | SSL_load_client_CA_file(empty.path().c_str())); |
| 9244 | EXPECT_FALSE(names); |
| 9245 | } |
| 9246 |
| 9247 | TEST(SSLTest, EmptyWriteBlockedOnHandshakeData) { |
| 9248 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9249 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 9250 | CreateContextWithTestCertificate(TLS_method()); |
| 9251 | ASSERT_TRUE(client_ctx); |
| 9252 | ASSERT_TRUE(server_ctx); |
| 9253 | // Configure only TLS 1.3. This test requires post-handshake NewSessionTicket. |
| 9254 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 9255 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 9256 |
| 9257 | // Connect a client and server with tiny buffer between the two. |
| 9258 | bssl::UniquePtr<SSL> client(SSL_new(client_ctx.get())), |
| 9259 | server(SSL_new(server_ctx.get())); |
| 9260 | ASSERT_TRUE(client); |
| 9261 | ASSERT_TRUE(server); |
| 9262 | SSL_set_connect_state(client.get()); |
| 9263 | SSL_set_accept_state(server.get()); |
| 9264 | BIO *bio1, *bio2; |
| 9265 | ASSERT_TRUE(BIO_new_bio_pair(&bio1, 1, &bio2, 1)); |
| 9266 | SSL_set_bio(client.get(), bio1, bio1); |
| 9267 | SSL_set_bio(server.get(), bio2, bio2); |
| 9268 | ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 9269 |
| 9270 | // We defer NewSessionTicket to the first write, so the server has a pending |
| 9271 | // NewSessionTicket. See https://boringssl-review.googlesource.com/34948. This |
| 9272 | // means an empty write will flush the ticket. However, the transport only |
| 9273 | // allows one byte through, so this will fail with |SSL_ERROR_WANT_WRITE|. |
| 9274 | int ret = SSL_write(server.get(), nullptr, 0); |
| 9275 | ASSERT_EQ(ret, -1); |
| 9276 | ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 9277 |
| 9278 | // Attempting to write non-zero data should not trip |SSL_R_BAD_WRITE_RETRY|. |
| 9279 | const uint8_t kData[] = {'h', 'e', 'l', 'l', 'o'}; |
| 9280 | ret = SSL_write(server.get(), kData, sizeof(kData)); |
| 9281 | ASSERT_EQ(ret, -1); |
| 9282 | ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 9283 |
| 9284 | // Byte by byte, the data should eventually get through. |
| 9285 | uint8_t buf[sizeof(kData)]; |
| 9286 | for (;;) { |
| 9287 | ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 9288 | ASSERT_EQ(ret, -1); |
| 9289 | ASSERT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_WANT_READ); |
| 9290 |
| 9291 | ret = SSL_write(server.get(), kData, sizeof(kData)); |
| 9292 | if (ret > 0) { |
| 9293 | ASSERT_EQ(ret, 5); |
| 9294 | break; |
| 9295 | } |
| 9296 | ASSERT_EQ(ret, -1); |
| 9297 | ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 9298 | } |
| 9299 |
| 9300 | ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 9301 | ASSERT_EQ(ret, static_cast<int>(sizeof(kData))); |
| 9302 | ASSERT_EQ(Bytes(buf, ret), Bytes(kData)); |
| 9303 | } |
| 9304 |
| 9305 | // Test that |SSL_ERROR_SYSCALL| continues to work after a close_notify. |
| 9306 | TEST(SSLTest, ErrorSyscallAfterCloseNotify) { |
| 9307 | // Make a custom |BIO| where writes fail, but without pushing to the error |
| 9308 | // queue. |
| 9309 | bssl::UniquePtr<BIO_METHOD> method(BIO_meth_new(0, nullptr)); |
| 9310 | ASSERT_TRUE(method); |
| 9311 | BIO_meth_set_create(method.get(), [](BIO *b) -> int { |
| 9312 | BIO_set_init(b, 1); |
| 9313 | return 1; |
| 9314 | }); |
| 9315 | static bool write_failed = false; |
| 9316 | BIO_meth_set_write(method.get(), [](BIO *, const char *, int) -> int { |
| 9317 | // Fail the operation and don't add to the error queue. |
| 9318 | write_failed = true; |
| 9319 | return -1; |
| 9320 | }); |
| 9321 | bssl::UniquePtr<BIO> wbio_silent_error(BIO_new(method.get())); |
| 9322 | ASSERT_TRUE(wbio_silent_error); |
| 9323 |
| 9324 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9325 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 9326 | CreateContextWithTestCertificate(TLS_method()); |
| 9327 | ASSERT_TRUE(client_ctx); |
| 9328 | ASSERT_TRUE(server_ctx); |
| 9329 | bssl::UniquePtr<SSL> client, server; |
| 9330 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 9331 | server_ctx.get())); |
| 9332 |
| 9333 | // Replace the write |BIO| with |wbio_silent_error|. |
| 9334 | SSL_set0_wbio(client.get(), wbio_silent_error.release()); |
| 9335 |
| 9336 | // Writes should fail. There is nothing in the error queue, so |
| 9337 | // |SSL_ERROR_SYSCALL| indicates the caller needs to check out-of-band. |
| 9338 | const uint8_t data[1] = {0}; |
| 9339 | int ret = SSL_write(client.get(), data, sizeof(data)); |
| 9340 | EXPECT_EQ(ret, -1); |
| 9341 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 9342 | EXPECT_TRUE(write_failed); |
| 9343 | write_failed = false; |
| 9344 |
| 9345 | // Send a close_notify from the server. It should return 0 because |
| 9346 | // close_notify was sent, but not received. Confusingly, this is a success |
| 9347 | // output for |SSL_shutdown|'s API. |
| 9348 | EXPECT_EQ(SSL_shutdown(server.get()), 0); |
| 9349 |
| 9350 | // Read the close_notify on the client. |
| 9351 | uint8_t buf[1]; |
| 9352 | ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 9353 | EXPECT_EQ(ret, 0); |
| 9354 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_ZERO_RETURN); |
| 9355 |
| 9356 | // Further calls to |SSL_read| continue to report |SSL_ERROR_ZERO_RETURN|. |
| 9357 | ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 9358 | EXPECT_EQ(ret, 0); |
| 9359 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_ZERO_RETURN); |
| 9360 |
| 9361 | // Although the client has seen close_notify, it should continue to report |
| 9362 | // |SSL_ERROR_SYSCALL| when its writes fail. |
| 9363 | ret = SSL_write(client.get(), data, sizeof(data)); |
| 9364 | EXPECT_EQ(ret, -1); |
| 9365 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 9366 | EXPECT_TRUE(write_failed); |
| 9367 | write_failed = false; |
| 9368 |
| 9369 | // Cause |BIO_write| to fail with a return value of zero instead. |
| 9370 | // |SSL_get_error| should not misinterpret this as a close_notify. |
| 9371 | // |
| 9372 | // This is not actually a correct implementation of |BIO_write|, but the rest |
| 9373 | // of the code treats zero from |BIO_write| as an error, so ensure it does so |
| 9374 | // correctly. Fixing https://crbug.com/boringssl/503 will make this case moot. |
| 9375 | BIO_meth_set_write(method.get(), [](BIO *, const char *, int) -> int { |
| 9376 | write_failed = true; |
| 9377 | return 0; |
| 9378 | }); |
| 9379 | ret = SSL_write(client.get(), data, sizeof(data)); |
| 9380 | EXPECT_EQ(ret, 0); |
| 9381 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 9382 | EXPECT_TRUE(write_failed); |
| 9383 | write_failed = false; |
| 9384 | } |
| 9385 |
| 9386 | // Test that |SSL_shutdown|, when quiet shutdown is enabled, simulates receiving |
| 9387 | // a close_notify, down to |SSL_read| reporting |SSL_ERROR_ZERO_RETURN|. |
| 9388 | TEST(SSLTest, QuietShutdown) { |
| 9389 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9390 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 9391 | CreateContextWithTestCertificate(TLS_method()); |
| 9392 | ASSERT_TRUE(client_ctx); |
| 9393 | ASSERT_TRUE(server_ctx); |
| 9394 | SSL_CTX_set_quiet_shutdown(server_ctx.get(), 1); |
| 9395 | bssl::UniquePtr<SSL> client, server; |
| 9396 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 9397 | server_ctx.get())); |
| 9398 |
| 9399 | // Quiet shutdown is enabled, so |SSL_shutdown| on the server should |
| 9400 | // immediately return that bidirectional shutdown "completed". |
| 9401 | EXPECT_EQ(SSL_shutdown(server.get()), 1); |
| 9402 |
| 9403 | // Shut down writes so the client gets an EOF. |
| 9404 | EXPECT_TRUE(BIO_shutdown_wr(SSL_get_wbio(server.get()))); |
| 9405 |
| 9406 | // Confirm no close notify was actually sent. Client reads should report a |
| 9407 | // transport EOF, not a close_notify. (Both have zero return, but |
| 9408 | // |SSL_get_error| is different.) |
| 9409 | char buf[1]; |
| 9410 | int ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 9411 | EXPECT_EQ(ret, 0); |
| 9412 | EXPECT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_SYSCALL); |
| 9413 |
| 9414 | // The server believes bidirectional shutdown completed, so reads should |
| 9415 | // replay the (simulated) close_notify. |
| 9416 | ret = SSL_read(server.get(), buf, sizeof(buf)); |
| 9417 | EXPECT_EQ(ret, 0); |
| 9418 | EXPECT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_ZERO_RETURN); |
| 9419 | } |
| 9420 |
| 9421 | TEST(SSLTest, InvalidSignatureAlgorithm) { |
| 9422 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 9423 | ASSERT_TRUE(ctx); |
| 9424 |
| 9425 | static const uint16_t kInvalidPrefs[] = {1234}; |
| 9426 | EXPECT_FALSE(SSL_CTX_set_signing_algorithm_prefs( |
| 9427 | ctx.get(), kInvalidPrefs, OPENSSL_ARRAY_SIZE(kInvalidPrefs))); |
| 9428 | EXPECT_FALSE(SSL_CTX_set_verify_algorithm_prefs( |
| 9429 | ctx.get(), kInvalidPrefs, OPENSSL_ARRAY_SIZE(kInvalidPrefs))); |
| 9430 |
| 9431 | static const uint16_t kDuplicatePrefs[] = {SSL_SIGN_RSA_PKCS1_SHA256, |
| 9432 | SSL_SIGN_RSA_PKCS1_SHA256}; |
| 9433 | EXPECT_FALSE(SSL_CTX_set_signing_algorithm_prefs( |
| 9434 | ctx.get(), kDuplicatePrefs, OPENSSL_ARRAY_SIZE(kDuplicatePrefs))); |
| 9435 | EXPECT_FALSE(SSL_CTX_set_verify_algorithm_prefs( |
| 9436 | ctx.get(), kDuplicatePrefs, OPENSSL_ARRAY_SIZE(kDuplicatePrefs))); |
| 9437 | } |
| 9438 |
| 9439 | TEST(SSLTest, InvalidGroups) { |
| 9440 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 9441 | ASSERT_TRUE(ctx); |
| 9442 |
| 9443 | static const uint16_t kInvalidIDs[] = {1234}; |
| 9444 | EXPECT_FALSE(SSL_CTX_set1_group_ids(ctx.get(), kInvalidIDs, |
| 9445 | OPENSSL_ARRAY_SIZE(kInvalidIDs))); |
| 9446 |
| 9447 | // This is a valid NID, but it is not a valid group. |
| 9448 | static const int kInvalidNIDs[] = {NID_rsaEncryption}; |
| 9449 | EXPECT_FALSE(SSL_CTX_set1_groups(ctx.get(), kInvalidNIDs, |
| 9450 | OPENSSL_ARRAY_SIZE(kInvalidNIDs))); |
| 9451 | } |
| 9452 |
| 9453 | TEST(SSLTest, NameLists) { |
| 9454 | struct { |
| 9455 | size_t (*func)(const char **, size_t); |
| 9456 | std::vector<std::string> expected; |
| 9457 | } kTests[] = { |
| 9458 | {SSL_get_all_version_names, {"TLSv1.3", "DTLSv1.2", "unknown"}}, |
| 9459 | {SSL_get_all_standard_cipher_names, |
| 9460 | {"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_AES_128_GCM_SHA256"}}, |
| 9461 | {SSL_get_all_cipher_names, |
| 9462 | {"ECDHE-ECDSA-AES128-GCM-SHA256", "TLS_AES_128_GCM_SHA256", "(NONE)"}}, |
| 9463 | {SSL_get_all_group_names, {"P-256", "X25519"}}, |
| 9464 | {SSL_get_all_signature_algorithm_names, |
| 9465 | {"rsa_pkcs1_sha256", "ecdsa_secp256r1_sha256", "ecdsa_sha256"}}, |
| 9466 | }; |
| 9467 | for (const auto &t : kTests) { |
| 9468 | size_t num = t.func(nullptr, 0); |
| 9469 | EXPECT_GT(num, 0u); |
| 9470 |
| 9471 | std::vector<const char *> list(num); |
| 9472 | EXPECT_EQ(num, t.func(list.data(), list.size())); |
| 9473 |
| 9474 | // Check the expected values are in the list. |
| 9475 | for (const auto &s : t.expected) { |
| 9476 | EXPECT_NE(list.end(), std::find(list.begin(), list.end(), s)) |
| 9477 | << "Could not find " << s; |
| 9478 | } |
| 9479 |
| 9480 | // Passing in a larger buffer should leave excess space alone. |
| 9481 | std::vector<const char *> list2(num + 1, "placeholder"); |
| 9482 | EXPECT_EQ(num, t.func(list2.data(), list2.size())); |
| 9483 | for (size_t i = 0; i < num; i++) { |
| 9484 | EXPECT_STREQ(list[i], list2[i]); |
| 9485 | } |
| 9486 | EXPECT_STREQ(list2.back(), "placeholder"); |
| 9487 |
| 9488 | // Passing in a shorter buffer should truncate the list. |
| 9489 | for (size_t l = 0; l < num; l++) { |
| 9490 | SCOPED_TRACE(l); |
| 9491 | list2.resize(l); |
| 9492 | EXPECT_EQ(num, t.func(list2.data(), list2.size())); |
| 9493 | for (size_t i = 0; i < l; i++) { |
| 9494 | EXPECT_STREQ(list[i], list2[i]); |
| 9495 | } |
| 9496 | } |
| 9497 | } |
| 9498 | } |
| 9499 |
| 9500 | // Test that it is possible for the certificate to be configured on a mix of |
| 9501 | // SSL_CTX and SSL. This ensures that we do not inadvertently overshare objects |
| 9502 | // in SSL_new. |
| 9503 | TEST(SSLTest, MixContextAndConnection) { |
| 9504 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 9505 | ASSERT_TRUE(ctx); |
| 9506 | bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 9507 | ASSERT_TRUE(cert); |
| 9508 | bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 9509 | ASSERT_TRUE(key); |
| 9510 |
| 9511 | // Configure the certificate, but not the private key, on the context. |
| 9512 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 9513 |
| 9514 | bssl::UniquePtr<SSL> ssl1(SSL_new(ctx.get())); |
| 9515 | ASSERT_TRUE(ssl1.get()); |
| 9516 | bssl::UniquePtr<SSL> ssl2(SSL_new(ctx.get())); |
| 9517 | ASSERT_TRUE(ssl2.get()); |
| 9518 |
| 9519 | // There is no private key configured yet. |
| 9520 | EXPECT_FALSE(SSL_CTX_get0_privatekey(ctx.get())); |
| 9521 | EXPECT_FALSE(SSL_get_privatekey(ssl1.get())); |
| 9522 | EXPECT_FALSE(SSL_get_privatekey(ssl2.get())); |
| 9523 |
| 9524 | // Configuring the private key on |ssl1| works. |
| 9525 | ASSERT_TRUE(SSL_use_PrivateKey(ssl1.get(), key.get())); |
| 9526 | EXPECT_TRUE(SSL_get_privatekey(ssl1.get())); |
| 9527 |
| 9528 | // It does not impact the other connection or the context. |
| 9529 | EXPECT_FALSE(SSL_CTX_get0_privatekey(ctx.get())); |
| 9530 | EXPECT_FALSE(SSL_get_privatekey(ssl2.get())); |
| 9531 | } |
| 9532 |
| 9533 | // Test that the server handshake cleanly fails if it had no certificate |
| 9534 | // configured, at all versions. |
| 9535 | TEST_P(SSLVersionTest, NoCertOrKey) { |
| 9536 | bssl::UniquePtr<X509> cert = GetChainTestCertificate(); |
| 9537 | ASSERT_TRUE(cert); |
| 9538 | bssl::UniquePtr<EVP_PKEY> key = GetChainTestKey(); |
| 9539 | ASSERT_TRUE(key); |
| 9540 | bssl::UniquePtr<X509> intermediate = GetChainTestIntermediate(); |
| 9541 | ASSERT_TRUE(intermediate); |
| 9542 | bssl::UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null()); |
| 9543 | ASSERT_TRUE(chain); |
| 9544 | ASSERT_TRUE(bssl::PushToStack(chain.get(), std::move(intermediate))); |
| 9545 |
| 9546 | const struct { |
| 9547 | bool has_cert; |
| 9548 | bool has_key; |
| 9549 | bool has_chain; |
| 9550 | } kTests[] = { |
| 9551 | // If nothing is configured, there is unambiguously no certificate. |
| 9552 | {/*has_cert=*/false, /*has_key=*/false, /*has_chain=*/false}, |
| 9553 |
| 9554 | // If only one of the key and certificate is configured, it is still |
| 9555 | // treated |
| 9556 | // as if there is no certificate. |
| 9557 | {/*has_cert=*/true, /*has_key=*/false, /*has_chain=*/false}, |
| 9558 | {/*has_cert=*/false, /*has_key=*/true, /*has_chain=*/false}, |
| 9559 |
| 9560 | // The key and intermediates may be configured, but without a leaf there |
| 9561 | // is |
| 9562 | // no certificate. This case is interesting because we internally store |
| 9563 | // the |
| 9564 | // chain with a somewhat fragile null fist entry. |
| 9565 | {/*has_cert=*/false, /*has_key=*/true, /*has_chain=*/true}, |
| 9566 | }; |
| 9567 | for (const auto &t : kTests) { |
| 9568 | SCOPED_TRACE(testing::Message() << "has_cert = " << t.has_cert); |
| 9569 | SCOPED_TRACE(testing::Message() << "has_key = " << t.has_key); |
| 9570 | SCOPED_TRACE(testing::Message() << "has_chain = " << t.has_chain); |
| 9571 | for (bool client : {false, true}) { |
| 9572 | SCOPED_TRACE(testing::Message() << "client = " << client); |
| 9573 |
| 9574 | EXPECT_NO_FATAL_FAILURE(ResetContexts()); |
| 9575 | if (client) { |
| 9576 | // Request client certificates from the server. |
| 9577 | SSL_CTX_set_verify(server_ctx_.get(), SSL_VERIFY_PEER, nullptr); |
| 9578 | SSL_CTX_set_cert_verify_callback(client_ctx_.get(), VerifySucceed, |
| 9579 | nullptr); |
| 9580 | } else { |
| 9581 | // Recreate the server context. ResetContexts automatically adds server |
| 9582 | // certificates. |
| 9583 | server_ctx_ = CreateContext(); |
| 9584 | ASSERT_TRUE(server_ctx_); |
| 9585 | } |
| 9586 |
| 9587 | SSL_CTX *ctx = client ? client_ctx_.get() : server_ctx_.get(); |
| 9588 | if (t.has_cert) { |
| 9589 | ASSERT_TRUE(SSL_CTX_use_certificate(ctx, cert.get())); |
| 9590 | } |
| 9591 | if (t.has_key) { |
| 9592 | ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx, key.get())); |
| 9593 | } |
| 9594 | if (t.has_chain) { |
| 9595 | ASSERT_TRUE(SSL_CTX_set1_chain(ctx, chain.get())); |
| 9596 | } |
| 9597 |
| 9598 | // In each of these cases, |SSL_CTX_check_private_key| should report the |
| 9599 | // certificate was not configured. |
| 9600 | EXPECT_FALSE(SSL_CTX_check_private_key(ctx)); |
| 9601 | ERR_clear_error(); |
| 9602 |
| 9603 | if (client) { |
| 9604 | // The client should cleanly handshake without asserting a certificate. |
| 9605 | EXPECT_TRUE(Connect()); |
| 9606 | EXPECT_FALSE(SSL_get0_peer_certificates(server_.get())); |
| 9607 | } else { |
| 9608 | // Servers cannot be anonymous. The connection should fail. |
| 9609 | EXPECT_FALSE(Connect()); |
| 9610 | // Depending on the TLS version, this should either appear as |
| 9611 | // NO_SHARED_CIPHER (TLS 1.2) or NO_CERTIFICATE_SET (TLS 1.3). |
| 9612 | uint32_t err = ERR_get_error(); |
| 9613 | if (!ErrorEquals(err, ERR_LIB_SSL, SSL_R_NO_SHARED_CIPHER)) { |
| 9614 | EXPECT_TRUE(ErrorEquals(err, ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET)); |
| 9615 | } |
| 9616 | } |
| 9617 | } |
| 9618 | } |
| 9619 | } |
| 9620 |
| 9621 | TEST_P(SSLVersionTest, KeyLog) { |
| 9622 | using KeyLog = std::map<std::string, std::vector<uint8_t>>; |
| 9623 | KeyLog client_log, server_log; |
| 9624 |
| 9625 | SSL_CTX_set_app_data(client_ctx_.get(), &client_log); |
| 9626 | SSL_CTX_set_app_data(server_ctx_.get(), &server_log); |
| 9627 |
| 9628 | auto keylog_callback = [](const SSL *ssl, const char *line) { |
| 9629 | SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); |
| 9630 | KeyLog *log = static_cast<KeyLog *>(SSL_CTX_get_app_data(ctx)); |
| 9631 | ASSERT_TRUE(log); |
| 9632 |
| 9633 | const char *space1 = strchr(line, ' '); |
| 9634 | ASSERT_TRUE(space1); |
| 9635 | std::string name(line, space1 - line); |
| 9636 | space1++; |
| 9637 | const char *space2 = strchr(space1, ' '); |
| 9638 | ASSERT_TRUE(space2); |
| 9639 | bssl::Span<const char> client_random_hex(space1, space2 - space1); |
| 9640 | space2++; |
| 9641 | bssl::Span<const char> secret_hex(space2, strlen(space2)); |
| 9642 |
| 9643 | std::vector<uint8_t> client_random, secret; |
| 9644 | ASSERT_TRUE(DecodeLowerHex(&client_random, client_random_hex)); |
| 9645 | ASSERT_TRUE(DecodeLowerHex(&secret, secret_hex)); |
| 9646 |
| 9647 | // The client_random field identifies the connection. Check it matches |
| 9648 | // the connection. |
| 9649 | uint8_t expected_random[SSL3_RANDOM_SIZE]; |
| 9650 | ASSERT_EQ( |
| 9651 | sizeof(expected_random), |
| 9652 | SSL_get_client_random(ssl, expected_random, sizeof(expected_random))); |
| 9653 | ASSERT_EQ(Bytes(expected_random), Bytes(client_random)); |
| 9654 |
| 9655 | ASSERT_EQ(log->count(name), 0u) << "duplicate name " << name; |
| 9656 | log->emplace(std::move(name), std::move(secret)); |
| 9657 | }; |
| 9658 | SSL_CTX_set_keylog_callback(client_ctx_.get(), keylog_callback); |
| 9659 | SSL_CTX_set_keylog_callback(server_ctx_.get(), keylog_callback); |
| 9660 |
| 9661 | // Connect and capture the various secrets. |
| 9662 | ASSERT_TRUE(Connect()); |
| 9663 |
| 9664 | // Check that we logged the secrets we expected to log. |
| 9665 | if (is_tls13()) { |
| 9666 | EXPECT_THAT(client_log, ElementsAre(Key("CLIENT_HANDSHAKE_TRAFFIC_SECRET"), |
| 9667 | Key("CLIENT_TRAFFIC_SECRET_0"), |
| 9668 | Key("EXPORTER_SECRET"), |
| 9669 | Key("SERVER_HANDSHAKE_TRAFFIC_SECRET"), |
| 9670 | Key("SERVER_TRAFFIC_SECRET_0"))); |
| 9671 |
| 9672 | if (!is_dtls()) { |
| 9673 | // Ideally we'd check the other values, but those are harder to check |
| 9674 | // without actually decrypting the records. |
| 9675 | // |
| 9676 | // TODO(crbug.com/42290608): Check the secrets in DTLS, once we have an |
| 9677 | // API for them. |
| 9678 | Span<const uint8_t> read_secret, write_secret; |
| 9679 | ASSERT_TRUE( |
| 9680 | SSL_get_traffic_secrets(client_.get(), &read_secret, &write_secret)); |
| 9681 | EXPECT_EQ(Bytes(read_secret), |
| 9682 | Bytes(client_log["SERVER_TRAFFIC_SECRET_0"])); |
| 9683 | EXPECT_EQ(Bytes(write_secret), |
| 9684 | Bytes(client_log["CLIENT_TRAFFIC_SECRET_0"])); |
| 9685 | } |
| 9686 | } else { |
| 9687 | EXPECT_THAT(client_log, ElementsAre(Key("CLIENT_RANDOM"))); |
| 9688 |
| 9689 | size_t len = |
| 9690 | SSL_SESSION_get_master_key(SSL_get0_session(client_.get()), nullptr, 0); |
| 9691 | std::vector<uint8_t> expected(len); |
| 9692 | ASSERT_EQ(SSL_SESSION_get_master_key(SSL_get0_session(client_.get()), |
| 9693 | expected.data(), expected.size()), |
| 9694 | expected.size()); |
| 9695 | EXPECT_EQ(Bytes(expected), Bytes(client_log["CLIENT_RANDOM"])); |
| 9696 | } |
| 9697 |
| 9698 | // The server should have logged the same secrets as the client. |
| 9699 | EXPECT_EQ(client_log, server_log); |
| 9700 | } |
| 9701 |
| 9702 | TEST_P(SSLVersionTest, GetTrafficSecrets) { |
| 9703 | ASSERT_TRUE(Connect()); |
| 9704 |
| 9705 | Span<const uint8_t> client_read, client_write, server_read, server_write; |
| 9706 | bool client_ok = |
| 9707 | SSL_get_traffic_secrets(client_.get(), &client_read, &client_write); |
| 9708 | bool server_ok = |
| 9709 | SSL_get_traffic_secrets(server_.get(), &server_read, &server_write); |
| 9710 | if (!is_dtls() && version() >= TLS1_3_VERSION) { |
| 9711 | ASSERT_TRUE(client_ok); |
| 9712 | ASSERT_TRUE(server_ok); |
| 9713 | EXPECT_EQ(Bytes(client_read), Bytes(server_write)); |
| 9714 | EXPECT_EQ(Bytes(server_read), Bytes(client_write)); |
| 9715 | } else { |
| 9716 | EXPECT_FALSE(client_ok); |
| 9717 | EXPECT_FALSE(server_ok); |
| 9718 | } |
| 9719 | } |
| 9720 |
| 9721 | TEST_P(SSLVersionTest, GetIVs) { |
| 9722 | std::vector<const char *> ciphers; |
| 9723 | if (version() == TLS1_2_VERSION || version() == DTLS1_2_VERSION) { |
| 9724 | // Try both CBC and AEAD ciphers. |
| 9725 | ciphers = {"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
| 9726 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"}; |
| 9727 | } else { |
| 9728 | // The defaults are fine to test. In 1.0 and 1.1, all remaining supported |
| 9729 | // ciphers are CBC. In 1.3, all ciphers are AEADs. |
| 9730 | ciphers = {"ALL"}; |
| 9731 | } |
| 9732 |
| 9733 | for (const char *cipher : ciphers) { |
| 9734 | SCOPED_TRACE(cipher); |
| 9735 |
| 9736 | ASSERT_NO_FATAL_FAILURE(ResetContexts()); |
| 9737 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(client_ctx_.get(), cipher)); |
| 9738 | ASSERT_TRUE(SSL_CTX_set_strict_cipher_list(server_ctx_.get(), cipher)); |
| 9739 | ASSERT_TRUE(Connect()); |
| 9740 |
| 9741 | const uint8_t *client_read_iv, *client_write_iv, *server_read_iv, |
| 9742 | *server_write_iv; |
| 9743 | size_t client_iv_len, server_iv_len; |
| 9744 | bool client_ivs_ok = SSL_get_ivs(client_.get(), &client_read_iv, |
| 9745 | &client_write_iv, &client_iv_len); |
| 9746 | bool server_ivs_ok = SSL_get_ivs(server_.get(), &server_read_iv, |
| 9747 | &server_write_iv, &server_iv_len); |
| 9748 |
| 9749 | // Only TLS 1.0 should support |SSL_get_ivs|. Other cases should cleanly |
| 9750 | // fail this operation. |
| 9751 | if (version() == TLS1_VERSION) { |
| 9752 | ASSERT_TRUE(client_ivs_ok); |
| 9753 | ASSERT_TRUE(server_ivs_ok); |
| 9754 | EXPECT_EQ(Bytes(client_write_iv, client_iv_len), |
| 9755 | Bytes(server_read_iv, server_iv_len)); |
| 9756 | EXPECT_EQ(Bytes(client_read_iv, client_iv_len), |
| 9757 | Bytes(server_write_iv, server_iv_len)); |
| 9758 | } else { |
| 9759 | EXPECT_FALSE(client_ivs_ok); |
| 9760 | EXPECT_FALSE(server_ivs_ok); |
| 9761 | } |
| 9762 | } |
| 9763 | } |
| 9764 |
| 9765 | TEST(SSLTest, EarlyDataVersionMismatch) { |
| 9766 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 9767 | ASSERT_TRUE(client_ctx); |
| 9768 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 9769 | CreateContextWithTestCertificate(TLS_method()); |
| 9770 | ASSERT_TRUE(server_ctx); |
| 9771 | SSL_CTX_set_early_data_enabled(client_ctx.get(), 1); |
| 9772 | SSL_CTX_set_early_data_enabled(server_ctx.get(), 1); |
| 9773 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9774 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9775 |
| 9776 | bssl::UniquePtr<SSL_SESSION> session = |
| 9777 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 9778 | ASSERT_TRUE(session); |
| 9779 | EXPECT_TRUE(SSL_SESSION_early_data_capable(session.get())); |
| 9780 |
| 9781 | // Turn off TLS 1.3 at the server. |
| 9782 | SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION); |
| 9783 | bssl::UniquePtr<SSL> client, server; |
| 9784 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 9785 | server_ctx.get())); |
| 9786 | SSL_set_session(client.get(), session.get()); |
| 9787 |
| 9788 | // Send the ClientHello. The client should immediately treat the handshake as |
| 9789 | // successful and offer early data. |
| 9790 | EXPECT_EQ(1, SSL_do_handshake(client.get())); |
| 9791 | EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 9792 |
| 9793 | // In the early data state, we report the predicted version, so that callers |
| 9794 | // see self-consistent connection properties. |
| 9795 | EXPECT_EQ(SSL_version(client.get()), TLS1_3_VERSION); |
| 9796 | EXPECT_NE(SSL_get0_peer_certificates(client.get()), nullptr); |
| 9797 |
| 9798 | // Read the ClientHello and send the ServerHello. The server will (implicitly |
| 9799 | // by negotiating TLS 1.2) reject early data. |
| 9800 | EXPECT_EQ(-1, SSL_do_handshake(server.get())); |
| 9801 | EXPECT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server.get(), -1)); |
| 9802 |
| 9803 | // Read the ServerHello. The client will now see the ServerHello and report a |
| 9804 | // version mismatch. Unlike other 0-RTT rejections, this is fatal, because a |
| 9805 | // TLS 1.2 server cannot recover from 0-RTT rejection. |
| 9806 | EXPECT_EQ(-1, SSL_do_handshake(client.get())); |
| 9807 | EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); |
| 9808 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 9809 | SSL_R_WRONG_VERSION_ON_EARLY_DATA)); |
| 9810 |
| 9811 | // |SSL_version| should continue reporting self-consistent state until the |
| 9812 | // caller calls |SSL_reset_early_data_reject|. |
| 9813 | // |
| 9814 | // TLS 1.3 to TLS 1.2 is not the most interesting version-related 0-RTT |
| 9815 | // rejection because it is fatal to the connection anyway. Once there are two |
| 9816 | // post-TLS-1.3 versions, or if we implement DTLS 1.3 0-RTT (where a DTLS 1.2 |
| 9817 | // server will skip over early data naturally), those will make for better |
| 9818 | // tests. In particular, early_data accept is signaled in EncryptedExtensions, |
| 9819 | // but the new version is learned at ServerHello. Though an implementation |
| 9820 | // could already infer based on the version that early data will be rejected. |
| 9821 | EXPECT_EQ(SSL_version(client.get()), TLS1_3_VERSION); |
| 9822 | EXPECT_NE(SSL_get0_peer_certificates(client.get()), nullptr); |
| 9823 | } |
| 9824 |
| 9825 | TEST(SSLTest, EarlyDataDisabledInDTLS13) { |
| 9826 | // Set up some 0-RTT-enabled contexts. |
| 9827 | bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(DTLS_method())); |
| 9828 | bssl::UniquePtr<SSL_CTX> server_ctx = |
| 9829 | CreateContextWithTestCertificate(DTLS_method()); |
| 9830 | ASSERT_TRUE(client_ctx); |
| 9831 | ASSERT_TRUE(server_ctx); |
| 9832 |
| 9833 | SSL_CTX_set_early_data_enabled(client_ctx.get(), true); |
| 9834 | SSL_CTX_set_early_data_enabled(server_ctx.get(), true); |
| 9835 | SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9836 | SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 9837 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), DTLS1_3_VERSION)); |
| 9838 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), DTLS1_3_VERSION)); |
| 9839 | ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), DTLS1_3_VERSION)); |
| 9840 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), DTLS1_3_VERSION)); |
| 9841 |
| 9842 | bssl::UniquePtr<SSL_SESSION> session = |
| 9843 | CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 9844 | ASSERT_TRUE(session); |
| 9845 | EXPECT_FALSE(SSL_SESSION_early_data_capable(session.get())); |
| 9846 | } |
| 9847 |
| 9848 | // ID-only TLS 1.3 sessions are impossible and should not be resumable. |
| 9849 | TEST(SSLTest, IDOnlyTLS13Session) { |
| 9850 | bssl::UniquePtr<SSL_CTX> ctx = CreateContextWithTestCertificate(TLS_method()); |
| 9851 | ASSERT_TRUE(ctx); |
| 9852 | SSL_CTX_set_session_cache_mode(ctx.get(), |
| 9853 | SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_SERVER); |
| 9854 |
| 9855 | ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 9856 | bssl::UniquePtr<SSL_SESSION> session = |
| 9857 | CreateClientSession(ctx.get(), ctx.get()); |
| 9858 | ASSERT_TRUE(session); |
| 9859 | EXPECT_TRUE(SSL_SESSION_is_resumable(session.get())); |
| 9860 |
| 9861 | session->ticket.Reset(); |
| 9862 | session->session_id.Resize(32); |
| 9863 | EXPECT_FALSE(SSL_SESSION_is_resumable(session.get())); |
| 9864 | } |
| 9865 |
| 9866 | TEST(SSLTest, DTLSReadTimeoutExpired) { |
| 9867 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(DTLS_method())); |
| 9868 | ASSERT_TRUE(ctx); |
| 9869 |
| 9870 | // Mock the clock. |
| 9871 | g_current_time.tv_sec = 1000; |
| 9872 | SSL_CTX_set_current_time_cb(ctx.get(), CurrentTimeCallback); |
| 9873 | auto advance = [](timeval delta) { |
| 9874 | g_current_time.tv_sec += delta.tv_sec; |
| 9875 | g_current_time.tv_usec += delta.tv_usec; |
| 9876 | if (g_current_time.tv_usec >= 1000000) { |
| 9877 | g_current_time.tv_usec -= 1000000; |
| 9878 | g_current_time.tv_sec++; |
| 9879 | } |
| 9880 | }; |
| 9881 |
| 9882 | // Create a client and don't connect it to anything. |
| 9883 | bssl::UniquePtr<SSL> client(SSL_new(ctx.get())); |
| 9884 | ASSERT_TRUE(client); |
| 9885 | SSL_set_connect_state(client.get()); |
| 9886 | bssl::UniquePtr<BIO> rbio(BIO_new(BIO_s_mem())); |
| 9887 | ASSERT_TRUE(rbio); |
| 9888 | SSL_set0_rbio(client.get(), rbio.release()); |
| 9889 | bssl::UniquePtr<BIO> wbio(BIO_new(BIO_s_mem())); |
| 9890 | ASSERT_TRUE(wbio); |
| 9891 | SSL_set0_wbio(client.get(), wbio.release()); |
| 9892 |
| 9893 | // Write the ClientHello and wait for a ServerHello. |
| 9894 | EXPECT_EQ(SSL_do_handshake(client.get()), -1); |
| 9895 | EXPECT_EQ(SSL_get_error(client.get(), -1), SSL_ERROR_WANT_READ); |
| 9896 |
| 9897 | for (;;) { |
| 9898 | // There should be a retransmit timer. |
| 9899 | timeval timeout; |
| 9900 | ASSERT_TRUE(DTLSv1_get_timeout(client.get(), &timeout)); |
| 9901 | EXPECT_TRUE(timeout.tv_sec != 0 || timeout.tv_usec != 0); |
| 9902 |
| 9903 | // Retransmit. At some point, the client will give up and fail. |
| 9904 | advance(timeout); |
| 9905 | int ret = DTLSv1_handle_timeout(client.get()); |
| 9906 | if (ret < 0) { |
| 9907 | break; |
| 9908 | } |
| 9909 | ASSERT_EQ(ret, 1); |
| 9910 | } |
| 9911 |
| 9912 | // The retransmit should have failed with |SSL_R_READ_TIMEOUT_EXPIRED|. |
| 9913 | EXPECT_EQ(SSL_get_error(client.get(), -1), SSL_ERROR_SSL); |
| 9914 | EXPECT_TRUE( |
| 9915 | ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_READ_TIMEOUT_EXPIRED)); |
| 9916 |
| 9917 | // There should not continue to be a timeout. Otherwise, a caller that forgets |
| 9918 | // to check |DTLSv1_handle_timeout|'s error will infinite loop. See |
| 9919 | // https://crbug.com/42224241. |
| 9920 | timeval timeout; |
| 9921 | EXPECT_FALSE(DTLSv1_get_timeout(client.get(), &timeout)); |
| 9922 |
| 9923 | // The error should also be returned from |SSL_do_handshake|. This ensures |
| 9924 | // that, if the caller missed the return from |DTLSv1_handle_timeout|, it will |
| 9925 | // be picked up from a more normal codepath. |
| 9926 | EXPECT_EQ(SSL_do_handshake(client.get()), -1); |
| 9927 | EXPECT_EQ(SSL_get_error(client.get(), -1), SSL_ERROR_SSL); |
| 9928 | EXPECT_TRUE( |
| 9929 | ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_READ_TIMEOUT_EXPIRED)); |
| 9930 | } |
| 9931 |
| 9932 | TEST(SSLTest, SetGetCompliancePolicy) { |
| 9933 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 9934 | EXPECT_EQ(SSL_CTX_get_compliance_policy(ctx.get()), |
| 9935 | ssl_compliance_policy_none); |
| 9936 |
| 9937 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 9938 | EXPECT_EQ(SSL_get_compliance_policy(ssl.get()), ssl_compliance_policy_none); |
| 9939 |
| 9940 | for (const auto policy : {ssl_compliance_policy_fips_202205, // |
| 9941 | ssl_compliance_policy_wpa3_192_202304, // |
| 9942 | ssl_compliance_policy_cnsa_202407}) { |
| 9943 | SSL_CTX_set_compliance_policy(ctx.get(), policy); |
| 9944 | EXPECT_EQ(SSL_CTX_get_compliance_policy(ctx.get()), policy); |
| 9945 | SSL_set_compliance_policy(ssl.get(), policy); |
| 9946 | EXPECT_EQ(SSL_get_compliance_policy(ssl.get()), policy); |
| 9947 | } |
| 9948 | } |
| 9949 |
| 9950 | TEST(SSLTest, ParseClientHello) { |
| 9951 | for (bool dtls : {false, true}) { |
| 9952 | SCOPED_TRACE(dtls); |
| 9953 | bssl::UniquePtr<SSL_CTX> ctx( |
| 9954 | SSL_CTX_new(dtls ? DTLS_method() : TLS_method())); |
| 9955 | ASSERT_TRUE(ctx); |
| 9956 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 9957 | ASSERT_TRUE(ssl); |
| 9958 |
| 9959 | // From draft-ietf-tls-tls13-vectors-06, Section 7, annotated. |
| 9960 | static const uint8_t kClientHelloTLS[] = { |
| 9961 | // legacy_version |
| 9962 | 0x03, 0x03, |
| 9963 | // random |
| 9964 | 0x37, 0xb0, 0x76, 0xd2, 0xfa, 0x50, 0x94, 0x39, 0x5e, 0x99, 0x71, 0xd7, |
| 9965 | 0x53, 0xc3, 0xc4, 0xcf, 0x07, 0x56, 0xb9, 0x40, 0x70, 0x13, 0xcb, 0xca, |
| 9966 | 0xc7, 0xf4, 0x4a, 0xc3, 0x28, 0x13, 0xf6, 0x0f, |
| 9967 | // legacy_session_id |
| 9968 | 0x20, 0x91, 0x41, 0xb7, 0x89, 0x83, 0xd3, 0x67, 0xa0, 0xfe, 0x97, 0x08, |
| 9969 | 0xdf, 0x32, 0xf5, 0xb9, 0x88, 0x8f, 0xe5, 0x9e, 0xde, 0x4e, 0x61, 0x2c, |
| 9970 | 0xf6, 0xbd, 0xb1, 0xfb, 0xbe, 0xe6, 0xf9, 0xef, 0xfe, |
| 9971 | // cipher_suites |
| 9972 | 0x00, 0x06, 0x13, 0x01, 0x13, 0x03, 0x13, 0x02, |
| 9973 | // legacy_compression_methods |
| 9974 | 0x01, 0x00, |
| 9975 | // extensions |
| 9976 | 0x00, 0x8d, |
| 9977 | // server_name |
| 9978 | 0x00, 0x00, 0x00, 0x0b, 0x00, 0x09, 0x00, 0x00, 0x06, 0x73, 0x65, 0x72, |
| 9979 | 0x76, 0x65, 0x72, |
| 9980 | // renegotiation_info |
| 9981 | 0xff, 0x01, 0x00, 0x01, 0x00, |
| 9982 | // supported_groups |
| 9983 | 0x00, 0x0a, 0x00, 0x14, 0x00, 0x12, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, |
| 9984 | 0x00, 0x19, 0x01, 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04, |
| 9985 | // key_share |
| 9986 | 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0xd5, 0x15, |
| 9987 | 0x42, 0x62, 0x5f, 0x25, 0xa9, 0x2d, 0x44, 0xa3, 0xaa, 0xde, 0xf5, 0x9c, |
| 9988 | 0xa8, 0x49, 0xad, 0x2f, 0x8e, 0xfa, 0x9f, 0x04, 0xb8, 0xf5, 0xda, 0xb4, |
| 9989 | 0x02, 0xac, 0xbc, 0x57, 0x1f, 0x16, |
| 9990 | // supported_versions |
| 9991 | 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, |
| 9992 | // signature_algorithms |
| 9993 | 0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, |
| 9994 | 0x02, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01, |
| 9995 | 0x06, 0x01, 0x02, 0x01, 0x04, 0x02, 0x05, 0x02, 0x06, 0x02, 0x02, 0x02, |
| 9996 | // psk_key_exchange_modes |
| 9997 | 0x00, 0x2d, 0x00, 0x02, 0x01, 0x01, |
| 9998 | // record_size_limit |
| 9999 | 0x00, 0x1c, 0x00, 0x02, 0x40, 0x01}; |
| 10000 | // The above, modified for DTLS 1.3. (Versions switched to DTLS spelling, a |
| 10001 | // cookie added.) |
| 10002 | static const uint8_t kClientHelloDTLS[] = { |
| 10003 | // legacy_version |
| 10004 | 0xfe, 0xfd, |
| 10005 | // random |
| 10006 | 0x37, 0xb0, 0x76, 0xd2, 0xfa, 0x50, 0x94, 0x39, 0x5e, 0x99, 0x71, 0xd7, |
| 10007 | 0x53, 0xc3, 0xc4, 0xcf, 0x07, 0x56, 0xb9, 0x40, 0x70, 0x13, 0xcb, 0xca, |
| 10008 | 0xc7, 0xf4, 0x4a, 0xc3, 0x28, 0x13, 0xf6, 0x0f, |
| 10009 | // legacy_session_id |
| 10010 | 0x20, 0x91, 0x41, 0xb7, 0x89, 0x83, 0xd3, 0x67, 0xa0, 0xfe, 0x97, 0x08, |
| 10011 | 0xdf, 0x32, 0xf5, 0xb9, 0x88, 0x8f, 0xe5, 0x9e, 0xde, 0x4e, 0x61, 0x2c, |
| 10012 | 0xf6, 0xbd, 0xb1, 0xfb, 0xbe, 0xe6, 0xf9, 0xef, 0xfe, |
| 10013 | // legacy_cookie |
| 10014 | 0x04, 0x01, 0x02, 0x03, 0x04, |
| 10015 | // cipher_suites |
| 10016 | 0x00, 0x06, 0x13, 0x01, 0x13, 0x03, 0x13, 0x02, |
| 10017 | // legacy_compression_methods |
| 10018 | 0x01, 0x00, |
| 10019 | // extensions |
| 10020 | 0x00, 0x8d, |
| 10021 | // server_name |
| 10022 | 0x00, 0x00, 0x00, 0x0b, 0x00, 0x09, 0x00, 0x00, 0x06, 0x73, 0x65, 0x72, |
| 10023 | 0x76, 0x65, 0x72, |
| 10024 | // renegotiation_info |
| 10025 | 0xff, 0x01, 0x00, 0x01, 0x00, |
| 10026 | // supported_groups |
| 10027 | 0x00, 0x0a, 0x00, 0x14, 0x00, 0x12, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, |
| 10028 | 0x00, 0x19, 0x01, 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04, |
| 10029 | // key_share |
| 10030 | 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0xd5, 0x15, |
| 10031 | 0x42, 0x62, 0x5f, 0x25, 0xa9, 0x2d, 0x44, 0xa3, 0xaa, 0xde, 0xf5, 0x9c, |
| 10032 | 0xa8, 0x49, 0xad, 0x2f, 0x8e, 0xfa, 0x9f, 0x04, 0xb8, 0xf5, 0xda, 0xb4, |
| 10033 | 0x02, 0xac, 0xbc, 0x57, 0x1f, 0x16, |
| 10034 | // supported_versions |
| 10035 | 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, |
| 10036 | // signature_algorithms |
| 10037 | 0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, |
| 10038 | 0x02, 0x03, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01, |
| 10039 | 0x06, 0x01, 0x02, 0x01, 0x04, 0x02, 0x05, 0x02, 0x06, 0x02, 0x02, 0x02, |
| 10040 | // psk_key_exchange_modes |
| 10041 | 0x00, 0x2d, 0x00, 0x02, 0x01, 0x01, |
| 10042 | // record_size_limit |
| 10043 | 0x00, 0x1c, 0x00, 0x02, 0x40, 0x01}; |
| 10044 |
| 10045 | auto in = dtls ? Span(kClientHelloDTLS) : Span(kClientHelloTLS); |
| 10046 | SSL_CLIENT_HELLO client_hello; |
| 10047 | ASSERT_TRUE( |
| 10048 | SSL_parse_client_hello(ssl.get(), &client_hello, in.data(), in.size())); |
| 10049 | EXPECT_EQ(client_hello.ssl, ssl.get()); |
| 10050 | EXPECT_EQ(Bytes(client_hello.client_hello, client_hello.client_hello_len), |
| 10051 | Bytes(in)); |
| 10052 | EXPECT_EQ(client_hello.version, dtls ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 10053 | static const uint8_t kRandom[] = { |
| 10054 | 0x37, 0xb0, 0x76, 0xd2, 0xfa, 0x50, 0x94, 0x39, 0x5e, 0x99, 0x71, |
| 10055 | 0xd7, 0x53, 0xc3, 0xc4, 0xcf, 0x07, 0x56, 0xb9, 0x40, 0x70, 0x13, |
| 10056 | 0xcb, 0xca, 0xc7, 0xf4, 0x4a, 0xc3, 0x28, 0x13, 0xf6, 0x0f}; |
| 10057 | EXPECT_EQ(Bytes(client_hello.random, client_hello.random_len), |
| 10058 | Bytes(kRandom)); |
| 10059 | static const uint8_t kSessionID[] = { |
| 10060 | 0x91, 0x41, 0xb7, 0x89, 0x83, 0xd3, 0x67, 0xa0, 0xfe, 0x97, 0x08, |
| 10061 | 0xdf, 0x32, 0xf5, 0xb9, 0x88, 0x8f, 0xe5, 0x9e, 0xde, 0x4e, 0x61, |
| 10062 | 0x2c, 0xf6, 0xbd, 0xb1, 0xfb, 0xbe, 0xe6, 0xf9, 0xef, 0xfe}; |
| 10063 | EXPECT_EQ(Bytes(client_hello.session_id, client_hello.session_id_len), |
| 10064 | Bytes(kSessionID)); |
| 10065 | if (dtls) { |
| 10066 | static const uint8_t kCookie[] = {0x01, 0x02, 0x03, 0x04}; |
| 10067 | EXPECT_EQ(Bytes(client_hello.dtls_cookie, client_hello.dtls_cookie_len), |
| 10068 | Bytes(kCookie)); |
| 10069 | } else { |
| 10070 | EXPECT_EQ(client_hello.dtls_cookie, nullptr); |
| 10071 | EXPECT_EQ(client_hello.dtls_cookie_len, 0u); |
| 10072 | } |
| 10073 | static const uint8_t kCipherSuites[] = {0x13, 0x01, 0x13, 0x03, 0x13, 0x02}; |
| 10074 | EXPECT_EQ(Bytes(client_hello.cipher_suites, client_hello.cipher_suites_len), |
| 10075 | Bytes(kCipherSuites)); |
| 10076 | static const uint8_t kCompressionMethods[] = {0x00}; |
| 10077 | EXPECT_EQ(Bytes(client_hello.compression_methods, |
| 10078 | client_hello.compression_methods_len), |
| 10079 | Bytes(kCompressionMethods)); |
| 10080 | auto extensions = in.last(141); |
| 10081 | EXPECT_EQ(Bytes(client_hello.extensions, client_hello.extensions_len), |
| 10082 | Bytes(extensions)); |
| 10083 |
| 10084 | static const uint8_t kServerName[] = {0x00, 0x09, 0x00, 0x00, 0x06, 0x73, |
| 10085 | 0x65, 0x72, 0x76, 0x65, 0x72}; |
| 10086 | const uint8_t *data; |
| 10087 | size_t len; |
| 10088 | ASSERT_TRUE(SSL_early_callback_ctx_extension_get( |
| 10089 | &client_hello, TLSEXT_TYPE_server_name, &data, &len)); |
| 10090 | EXPECT_EQ(Bytes(data, len), Bytes(kServerName)); |
| 10091 | EXPECT_FALSE(SSL_early_callback_ctx_extension_get( |
| 10092 | &client_hello, TLSEXT_TYPE_encrypted_client_hello, &data, &len)); |
| 10093 |
| 10094 | // Trailing data should be rejected. |
| 10095 | std::vector<uint8_t> trailing_data(in.begin(), in.end()); |
| 10096 | trailing_data.push_back(0); |
| 10097 | EXPECT_FALSE(SSL_parse_client_hello( |
| 10098 | ssl.get(), &client_hello, trailing_data.data(), trailing_data.size())); |
| 10099 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 10100 | SSL_R_CLIENTHELLO_PARSE_FAILED)); |
| 10101 | ERR_clear_error(); |
| 10102 |
| 10103 | // Other invalid inputs. |
| 10104 | static const uint8_t kInvalid[] = {'n', 'o', 'p', 'e'}; |
| 10105 | EXPECT_FALSE(SSL_parse_client_hello(ssl.get(), &client_hello, kInvalid, |
| 10106 | sizeof(kInvalid))); |
| 10107 | EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, |
| 10108 | SSL_R_CLIENTHELLO_PARSE_FAILED)); |
| 10109 | ERR_clear_error(); |
| 10110 | } |
| 10111 | } |
| 10112 |
| 10113 | class SSLPAKETest : public testing::Test { |
| 10114 | public: |
| 10115 | static Span<const uint8_t> pake_context() { |
| 10116 | return StringAsBytes("test context"); |
| 10117 | } |
| 10118 | static Span<const uint8_t> client_identity() { |
| 10119 | return StringAsBytes("client"); |
| 10120 | } |
| 10121 | static Span<const uint8_t> server_identity() { |
| 10122 | return StringAsBytes("client"); |
| 10123 | } |
| 10124 |
| 10125 | static UniquePtr<SSL_CTX> NewClientContext(std::string_view password, |
| 10126 | uint32_t attempts) { |
| 10127 | auto reg = Register(password); |
| 10128 | if (!reg) { |
| 10129 | return nullptr; |
| 10130 | } |
| 10131 |
| 10132 | UniquePtr<SSL_CREDENTIAL> cred(SSL_CREDENTIAL_new_spake2plusv1_client( |
| 10133 | pake_context().data(), pake_context().size(), client_identity().data(), |
| 10134 | client_identity().size(), server_identity().data(), |
| 10135 | server_identity().size(), attempts, reg->pw_verifier_w0, |
| 10136 | sizeof(reg->pw_verifier_w0), reg->pw_verifier_w1, |
| 10137 | sizeof(reg->pw_verifier_w1))); |
| 10138 | if (cred == nullptr) { |
| 10139 | return nullptr; |
| 10140 | } |
| 10141 |
| 10142 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 10143 | if (ctx == nullptr || !SSL_CTX_add1_credential(ctx.get(), cred.get())) { |
| 10144 | return nullptr; |
| 10145 | } |
| 10146 | return ctx; |
| 10147 | } |
| 10148 |
| 10149 | static UniquePtr<SSL_CTX> NewServerContext(std::string_view password, |
| 10150 | uint32_t attempts) { |
| 10151 | auto reg = Register(password); |
| 10152 | if (!reg) { |
| 10153 | return nullptr; |
| 10154 | } |
| 10155 |
| 10156 | UniquePtr<SSL_CREDENTIAL> cred(SSL_CREDENTIAL_new_spake2plusv1_server( |
| 10157 | pake_context().data(), pake_context().size(), client_identity().data(), |
| 10158 | client_identity().size(), server_identity().data(), |
| 10159 | server_identity().size(), attempts, reg->pw_verifier_w0, |
| 10160 | sizeof(reg->pw_verifier_w0), reg->registration_record, |
| 10161 | sizeof(reg->registration_record))); |
| 10162 | if (cred == nullptr) { |
| 10163 | return nullptr; |
| 10164 | } |
| 10165 |
| 10166 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 10167 | if (ctx == nullptr || !SSL_CTX_add1_credential(ctx.get(), cred.get())) { |
| 10168 | return nullptr; |
| 10169 | } |
| 10170 | return ctx; |
| 10171 | } |
| 10172 |
| 10173 | private: |
| 10174 | struct PAKERegistration { |
| 10175 | uint8_t pw_verifier_w0[32]; |
| 10176 | uint8_t pw_verifier_w1[32]; |
| 10177 | uint8_t registration_record[65]; |
| 10178 | }; |
| 10179 |
| 10180 | static std::optional<PAKERegistration> Register(std::string_view password) { |
| 10181 | auto password_bytes = StringAsBytes(password); |
| 10182 | PAKERegistration ret; |
| 10183 | if (!SSL_spake2plusv1_register( |
| 10184 | ret.pw_verifier_w0, ret.pw_verifier_w1, ret.registration_record, |
| 10185 | password_bytes.data(), password_bytes.size(), |
| 10186 | client_identity().data(), client_identity().size(), |
| 10187 | server_identity().data(), server_identity().size())) { |
| 10188 | return std::nullopt; |
| 10189 | } |
| 10190 | return ret; |
| 10191 | } |
| 10192 | }; |
| 10193 |
| 10194 | TEST_F(SSLPAKETest, SPAKE2PLUS) { |
| 10195 | UniquePtr<SSL_CTX> client_ctx = NewClientContext("password", 1); |
| 10196 | ASSERT_TRUE(client_ctx); |
| 10197 | UniquePtr<SSL_CTX> server_ctx = NewServerContext("password", 1); |
| 10198 | ASSERT_TRUE(server_ctx); |
| 10199 | bssl::UniquePtr<SSL> client, server; |
| 10200 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10201 | server_ctx.get())); |
| 10202 | } |
| 10203 |
| 10204 | TEST_F(SSLPAKETest, ClientLimit) { |
| 10205 | static constexpr uint32_t kLimit = 5; |
| 10206 | static constexpr uint32_t kUnlimited = UINT32_MAX; |
| 10207 |
| 10208 | UniquePtr<SSL_CTX> client_ctx = NewClientContext("password", kLimit); |
| 10209 | ASSERT_TRUE(client_ctx); |
| 10210 | UniquePtr<SSL_CTX> server_ctx_good = NewServerContext("password", kUnlimited); |
| 10211 | ASSERT_TRUE(server_ctx_good); |
| 10212 | UniquePtr<SSL_CTX> server_ctx_bad = NewServerContext("wrong", kUnlimited); |
| 10213 | ASSERT_TRUE(server_ctx_bad); |
| 10214 |
| 10215 | // The client sees confirmV before revealing a password confirmation, so |
| 10216 | // neither successful nor unfinished handshakes contribute to the limit. |
| 10217 | bssl::UniquePtr<SSL> client, server; |
| 10218 | for (uint32_t i = 0; i < kLimit * 2; i++) { |
| 10219 | // Unfinished handshake. |
| 10220 | ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 10221 | server_ctx_good.get())); |
| 10222 | ASSERT_EQ(SSL_do_handshake(client.get()), -1); // Write ClientHello. |
| 10223 | ASSERT_EQ(SSL_get_error(client.get(), -1), SSL_ERROR_WANT_READ); |
| 10224 |
| 10225 | // Successful handshake. |
| 10226 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10227 | server_ctx_good.get())); |
| 10228 | } |
| 10229 |
| 10230 | // After kLimit - 1 password mismatches, the credential still functions. |
| 10231 | for (uint32_t i = 0; i < kLimit - 1; i++) { |
| 10232 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10233 | server_ctx_bad.get())); |
| 10234 | } |
| 10235 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10236 | server_ctx_good.get())); |
| 10237 |
| 10238 | // But after one more password mismatch... |
| 10239 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10240 | server_ctx_bad.get())); |
| 10241 |
| 10242 | // ...the client should refuse to use the credential at all. |
| 10243 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 10244 | server_ctx_good.get())); |
| 10245 | ASSERT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_PAKE_EXHAUSTED)); |
| 10246 | } |
| 10247 |
| 10248 | TEST_F(SSLPAKETest, ServerLimit) { |
| 10249 | static constexpr uint32_t kLimit = 5; |
| 10250 | static constexpr uint32_t kUnlimited = UINT32_MAX; |
| 10251 |
| 10252 | UniquePtr<SSL_CTX> server_ctx = NewServerContext("password", kLimit); |
| 10253 | ASSERT_TRUE(server_ctx); |
| 10254 | UniquePtr<SSL_CTX> client_ctx_good = NewClientContext("password", kUnlimited); |
| 10255 | ASSERT_TRUE(client_ctx_good); |
| 10256 | UniquePtr<SSL_CTX> client_ctx_bad = NewClientContext("wrong", kUnlimited); |
| 10257 | ASSERT_TRUE(client_ctx_bad); |
| 10258 |
| 10259 | // Successful handshakes do not (indefinitely) contribute to the limit. If the |
| 10260 | // server sees one good handshake at a time, the limit does not impact it. |
| 10261 | bssl::UniquePtr<SSL> client, server; |
| 10262 | for (uint32_t i = 0; i < kLimit * 2; i++) { |
| 10263 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10264 | server_ctx.get())); |
| 10265 | } |
| 10266 |
| 10267 | // The server sends confirmV before confirming the client knew the password, |
| 10268 | // so any handshake in between ClientHello and ServerHello counts towards the |
| 10269 | // limit. |
| 10270 | struct ClientServerPair { |
| 10271 | bssl::UniquePtr<SSL> client, server; |
| 10272 | }; |
| 10273 | std::vector<ClientServerPair> pending; |
| 10274 | auto handshake_up_to_serverhello = [](ClientServerPair *pair) { |
| 10275 | // Send ClientHello. |
| 10276 | ASSERT_EQ(SSL_do_handshake(pair->client.get()), -1); |
| 10277 | ASSERT_EQ(SSL_get_error(pair->client.get(), -1), SSL_ERROR_WANT_READ); |
| 10278 | // Send ServerHello..Finished. |
| 10279 | ASSERT_EQ(SSL_do_handshake(pair->server.get()), -1); |
| 10280 | ASSERT_EQ(SSL_get_error(pair->server.get(), -1), SSL_ERROR_WANT_READ); |
| 10281 | }; |
| 10282 |
| 10283 | // First, go just under the limit. |
| 10284 | for (uint32_t i = 0; i < kLimit - 1; i++) { |
| 10285 | ClientServerPair pair; |
| 10286 | ASSERT_TRUE(CreateClientAndServer(&pair.client, &pair.server, |
| 10287 | client_ctx_good.get(), server_ctx.get())); |
| 10288 | ASSERT_NO_FATAL_FAILURE(handshake_up_to_serverhello(&pair)); |
| 10289 | pending.push_back(std::move(pair)); |
| 10290 | } |
| 10291 |
| 10292 | // The server can still complete a handshake. |
| 10293 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10294 | server_ctx.get())); |
| 10295 |
| 10296 | // Start one more unfinished handshake. |
| 10297 | ClientServerPair pair; |
| 10298 | ASSERT_TRUE(CreateClientAndServer(&pair.client, &pair.server, |
| 10299 | client_ctx_good.get(), server_ctx.get())); |
| 10300 | ASSERT_NO_FATAL_FAILURE(handshake_up_to_serverhello(&pair)); |
| 10301 | pending.push_back(std::move(pair)); |
| 10302 |
| 10303 | // The credential is at its limit. |
| 10304 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10305 | server_ctx.get())); |
| 10306 | ASSERT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_PAKE_EXHAUSTED)); |
| 10307 |
| 10308 | // Complete some of the handshakes. As they complete, the server learns that |
| 10309 | // the client had the correct guess, so the connections no longer count |
| 10310 | // towards the brute force limit. |
| 10311 | static constexpr uint32_t kRemainingLimit = kLimit / 2; |
| 10312 | for (uint32_t i = 0; i < kRemainingLimit; i++) { |
| 10313 | ASSERT_TRUE(CompleteHandshakes(pending.back().client.get(), |
| 10314 | pending.back().server.get())); |
| 10315 | pending.pop_back(); |
| 10316 | } |
| 10317 |
| 10318 | // The server can complete a handshake now that some of the limit has been |
| 10319 | // released. |
| 10320 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10321 | server_ctx.get())); |
| 10322 |
| 10323 | // Failed handshakes consume the limit. First consume all but one of the newly |
| 10324 | // released limit. |
| 10325 | for (uint32_t i = 0; i < kRemainingLimit - 1; i++) { |
| 10326 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_bad.get(), |
| 10327 | server_ctx.get())); |
| 10328 | } |
| 10329 | ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10330 | server_ctx.get())); |
| 10331 |
| 10332 | // Consume the last of the limit. |
| 10333 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_bad.get(), |
| 10334 | server_ctx.get())); |
| 10335 | // The credential is disabled again. |
| 10336 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10337 | server_ctx.get())); |
| 10338 | ASSERT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_PAKE_EXHAUSTED)); |
| 10339 |
| 10340 | // The unfinished handshakes continue to count toward the limit even if they |
| 10341 | // are destroyed. |
| 10342 | pending.clear(); |
| 10343 | ASSERT_FALSE(ConnectClientAndServer(&client, &server, client_ctx_good.get(), |
| 10344 | server_ctx.get())); |
| 10345 | ASSERT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_PAKE_EXHAUSTED)); |
| 10346 | } |
| 10347 |
| 10348 | #if defined(OPENSSL_THREADS) |
| 10349 | // The PAKE limit mechanism should be thread-safe. |
| 10350 | TEST_F(SSLPAKETest, ClientThreads) { |
| 10351 | static constexpr uint32_t kLimit = 5; |
| 10352 | static constexpr uint32_t kUnlimited = UINT32_MAX; |
| 10353 | static constexpr int kThreads = 10; |
| 10354 |
| 10355 | UniquePtr<SSL_CTX> client_ctx = NewClientContext("password", kLimit); |
| 10356 | ASSERT_TRUE(client_ctx); |
| 10357 | UniquePtr<SSL_CTX> server_ctx_good = NewServerContext("password", kUnlimited); |
| 10358 | ASSERT_TRUE(server_ctx_good); |
| 10359 | UniquePtr<SSL_CTX> server_ctx_bad = NewServerContext("wrong", kUnlimited); |
| 10360 | ASSERT_TRUE(server_ctx_bad); |
| 10361 |
| 10362 | auto connect = [&](SSL_CTX *server_ctx) { |
| 10363 | bssl::UniquePtr<SSL> client, server; |
| 10364 | ConnectClientAndServer(&client, &server, client_ctx.get(), server_ctx); |
| 10365 | }; |
| 10366 |
| 10367 | std::vector<std::thread> threads; |
| 10368 | for (int i = 0; i < kThreads; i++) { |
| 10369 | threads.emplace_back([&] { connect(server_ctx_good.get()); }); |
| 10370 | threads.emplace_back([&] { connect(server_ctx_bad.get()); }); |
| 10371 | } |
| 10372 | for (auto &thread : threads) { |
| 10373 | thread.join(); |
| 10374 | } |
| 10375 | } |
| 10376 | TEST_F(SSLPAKETest, ServerThreads) { |
| 10377 | static constexpr uint32_t kLimit = 5; |
| 10378 | static constexpr uint32_t kUnlimited = UINT32_MAX; |
| 10379 | static constexpr int kThreads = 10; |
| 10380 |
| 10381 | UniquePtr<SSL_CTX> server_ctx = NewServerContext("password", kLimit); |
| 10382 | ASSERT_TRUE(server_ctx); |
| 10383 | UniquePtr<SSL_CTX> client_ctx_good = NewClientContext("password", kUnlimited); |
| 10384 | ASSERT_TRUE(client_ctx_good); |
| 10385 | UniquePtr<SSL_CTX> client_ctx_bad = NewClientContext("wrong", kUnlimited); |
| 10386 | ASSERT_TRUE(client_ctx_bad); |
| 10387 |
| 10388 | auto connect = [&](SSL_CTX *client_ctx) { |
| 10389 | bssl::UniquePtr<SSL> client, server; |
| 10390 | ConnectClientAndServer(&client, &server, client_ctx, server_ctx.get()); |
| 10391 | }; |
| 10392 |
| 10393 | std::vector<std::thread> threads; |
| 10394 | for (int i = 0; i < kThreads; i++) { |
| 10395 | threads.emplace_back([&] { connect(client_ctx_good.get()); }); |
| 10396 | threads.emplace_back([&] { connect(client_ctx_bad.get()); }); |
| 10397 | } |
| 10398 | for (auto &thread : threads) { |
| 10399 | thread.join(); |
| 10400 | } |
| 10401 | } |
| 10402 | #endif // OPENSSL_THREADS |
| 10403 |
| 10404 | } // namespace |
| 10405 | BSSL_NAMESPACE_END |
| 10406 |