ssl_keyupdate_unittest.cc (51174B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "secerr.h" 8 #include "ssl.h" 9 #include "sslerr.h" 10 #include "sslproto.h" 11 12 extern "C" { 13 // This is not something that should make you happy. 14 #include "libssl_internals.h" 15 } 16 17 #include "gtest_utils.h" 18 #include "nss_scoped_ptrs.h" 19 #include "tls_connect.h" 20 #include "tls_filter.h" 21 #include "tls_parser.h" 22 23 namespace nss_test { 24 25 TEST_F(TlsConnectTest, KeyUpdateClient) { 26 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 27 Connect(); 28 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 29 SendReceive(50); 30 SendReceive(60); 31 CheckEpochs(4, 3); 32 } 33 34 TEST_F(TlsConnectStreamTls13, KeyUpdateTooEarly_Client) { 35 StartConnect(); 36 auto filter = MakeTlsFilter<TlsEncryptedHandshakeMessageReplacer>( 37 server_, kTlsHandshakeFinished, kTlsHandshakeKeyUpdate); 38 filter->EnableDecryption(); 39 40 client_->Handshake(); 41 server_->Handshake(); 42 ExpectAlert(client_, kTlsAlertUnexpectedMessage); 43 client_->Handshake(); 44 client_->CheckErrorCode(SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE); 45 server_->Handshake(); 46 server_->CheckErrorCode(SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT); 47 } 48 49 TEST_F(TlsConnectStreamTls13, KeyUpdateTooEarly_Server) { 50 StartConnect(); 51 auto filter = MakeTlsFilter<TlsEncryptedHandshakeMessageReplacer>( 52 client_, kTlsHandshakeFinished, kTlsHandshakeKeyUpdate); 53 filter->EnableDecryption(); 54 55 client_->Handshake(); 56 server_->Handshake(); 57 client_->Handshake(); 58 ExpectAlert(server_, kTlsAlertUnexpectedMessage); 59 server_->Handshake(); 60 server_->CheckErrorCode(SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE); 61 client_->Handshake(); 62 client_->CheckErrorCode(SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT); 63 } 64 65 TEST_F(TlsConnectTest, KeyUpdateClientRequestUpdate) { 66 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 67 Connect(); 68 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_TRUE)); 69 // SendReceive() only gives each peer one chance to read. This isn't enough 70 // when the read on one side generates another handshake message. A second 71 // read gives each peer an extra chance to consume the KeyUpdate. 72 SendReceive(50); 73 SendReceive(60); // Cumulative count. 74 CheckEpochs(4, 4); 75 } 76 77 TEST_F(TlsConnectTest, KeyUpdateServer) { 78 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 79 Connect(); 80 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 81 SendReceive(50); 82 SendReceive(60); 83 CheckEpochs(3, 4); 84 } 85 86 TEST_F(TlsConnectTest, KeyUpdateServerRequestUpdate) { 87 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 88 Connect(); 89 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 90 SendReceive(50); 91 SendReceive(60); 92 CheckEpochs(4, 4); 93 } 94 95 TEST_F(TlsConnectTest, KeyUpdateConsecutiveRequests) { 96 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 97 Connect(); 98 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 99 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 100 SendReceive(50); 101 SendReceive(60); 102 // The server should have updated twice, but the client should have declined 103 // to respond to the second request from the server, since it doesn't send 104 // anything in between those two requests. 105 CheckEpochs(4, 5); 106 } 107 108 // Check that a local update can be immediately followed by a remotely triggered 109 // update even if there is no use of the keys. 110 TEST_F(TlsConnectTest, KeyUpdateLocalUpdateThenConsecutiveRequests) { 111 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 112 Connect(); 113 // This should trigger an update on the client. 114 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 115 // The client should update for the first request. 116 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 117 // ...but not the second. 118 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 119 SendReceive(50); 120 SendReceive(60); 121 // Both should have updated twice. 122 CheckEpochs(5, 5); 123 } 124 125 TEST_F(TlsConnectTest, KeyUpdateMultiple) { 126 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 127 Connect(); 128 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 129 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 130 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 131 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 132 SendReceive(50); 133 SendReceive(60); 134 CheckEpochs(5, 6); 135 } 136 137 // Both ask the other for an update, and both should react. 138 TEST_F(TlsConnectTest, KeyUpdateBothRequest) { 139 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 140 Connect(); 141 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_TRUE)); 142 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 143 SendReceive(50); 144 SendReceive(60); 145 CheckEpochs(5, 5); 146 } 147 148 // If the sequence number exceeds the number of writes before an automatic 149 // update (currently 3/4 of the max records for the cipher suite), then the 150 // stack should send an update automatically (but not request one). 151 TEST_F(TlsConnectTest, KeyUpdateAutomaticOnWrite) { 152 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 153 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 154 155 // Set this to one below the write threshold. 156 uint64_t threshold = (0x5aULL << 28) * 3 / 4; 157 EXPECT_EQ(SECSuccess, 158 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 159 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 160 161 // This should be OK. 162 client_->SendData(10); 163 server_->ReadBytes(); 164 165 // This should cause the client to update. 166 client_->SendData(20); 167 server_->ReadBytes(); 168 169 SendReceive(100); 170 CheckEpochs(4, 3); 171 } 172 173 // If the sequence number exceeds a certain number of reads (currently 7/8 of 174 // the max records for the cipher suite), then the stack should send AND request 175 // an update automatically. However, the sender (client) will be above its 176 // automatic update threshold, so the KeyUpdate - that it sends with the old 177 // cipher spec - will exceed the receiver (server) automatic update threshold. 178 // The receiver gets a packet with a sequence number over its automatic read 179 // update threshold. Even though the sender has updated, the code that checks 180 // the sequence numbers at the receiver doesn't know this and it will request an 181 // update. This causes two updates: one from the sender (without requesting a 182 // response) and one from the receiver (which does request a response). 183 TEST_F(TlsConnectTest, KeyUpdateAutomaticOnRead) { 184 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 185 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 186 187 // Move to right at the read threshold. Unlike the write test, we can't send 188 // packets because that would cause the client to update, which would spoil 189 // the test. 190 uint64_t threshold = ((0x5aULL << 28) * 7 / 8) + 1; 191 EXPECT_EQ(SECSuccess, 192 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 193 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 194 195 // This should cause the client to update, but not early enough to prevent the 196 // server from updating also. 197 client_->SendData(10); 198 server_->ReadBytes(); 199 200 // Need two SendReceive() calls to ensure that the update that the server 201 // requested is properly generated and consumed. 202 SendReceive(70); 203 SendReceive(80); 204 CheckEpochs(5, 4); 205 } 206 207 // Filter to modify KeyUpdate message. Takes as an input which byte and what 208 // value to install. 209 class TLSKeyUpdateDamager : public TlsRecordFilter { 210 public: 211 TLSKeyUpdateDamager(const std::shared_ptr<TlsAgent>& a, size_t byte, 212 uint8_t val) 213 : TlsRecordFilter(a), offset_(byte), value_(val) {} 214 215 protected: 216 PacketFilter::Action FilterRecord(const TlsRecordHeader& header, 217 const DataBuffer& record, size_t* offset, 218 DataBuffer* output) override { 219 if (!header.is_protected()) { 220 return KEEP; 221 } 222 uint16_t protection_epoch; 223 uint8_t inner_content_type; 224 DataBuffer plaintext; 225 TlsRecordHeader out_header; 226 227 if (!Unprotect(header, record, &protection_epoch, &inner_content_type, 228 &plaintext, &out_header)) { 229 return KEEP; 230 } 231 232 if (inner_content_type != ssl_ct_handshake) { 233 return KEEP; 234 } 235 236 if (plaintext.data()[0] != ssl_hs_key_update) { 237 return KEEP; 238 } 239 240 if (offset_ >= plaintext.len()) { 241 ADD_FAILURE() << "TLSKeyUpdateDamager: the input (offset_) is out " 242 "of the range (the expected len is equal to " 243 << plaintext.len() << ")." << std::endl; 244 return KEEP; 245 } 246 247 plaintext.data()[offset_] = value_; 248 DataBuffer ciphertext; 249 bool ok = Protect(spec(protection_epoch), out_header, inner_content_type, 250 plaintext, &ciphertext, &out_header); 251 if (!ok) { 252 ADD_FAILURE() << "Unable to protect the plaintext using " 253 << protection_epoch << "epoch. " << std::endl; 254 return KEEP; 255 } 256 *offset = out_header.Write(output, *offset, ciphertext); 257 return CHANGE; 258 } 259 260 protected: 261 size_t offset_; 262 uint8_t value_; 263 }; 264 265 // The next tests check the behaviour in case of malformed KeyUpdate. 266 // The first test, TLSKeyUpdateWrongValueForUpdateRequested, 267 // modifies the 4th byte (KeyUpdate) to have the incorrect value. 268 // The last tests check the incorrect values of the length. 269 270 // RFC 8446: 4. Handshake Protocol 271 // struct { 272 // HandshakeType msg_type; handshake type 273 // uint24 length; remaining bytes in message 274 // select (Handshake.msg_type) { 275 // case key_update: KeyUpdate; (4th byte) 276 // }; 277 // } Handshake; 278 279 TEST_F(TlsConnectStreamTls13, TLSKeyUpdateWrongValueForUpdateRequested) { 280 EnsureTlsSetup(); 281 // This test is setting the update_requested to be equal to 2 282 // Whereas the allowed values are [0, 1]. 283 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 4, 2); 284 filter->EnableDecryption(); 285 filter->Disable(); 286 Connect(); 287 288 filter->Enable(); 289 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 290 filter->Disable(); 291 292 ExpectAlert(server_, kTlsAlertDecodeError); 293 client_->ExpectReceiveAlert(kTlsAlertDecodeError); 294 295 server_->ExpectReadWriteError(); 296 client_->ExpectReadWriteError(); 297 server_->ReadBytes(); 298 client_->ReadBytes(); 299 300 server_->CheckErrorCode(SSL_ERROR_RX_MALFORMED_KEY_UPDATE); 301 client_->CheckErrorCode(SSL_ERROR_DECODE_ERROR_ALERT); 302 303 // Even if the client has updated his writing key, 304 client_->CheckEpochs(3, 4); 305 // the server has not. 306 server_->CheckEpochs(3, 3); 307 } 308 309 TEST_F(TlsConnectStreamTls13, TLSKeyUpdateWrongValueForLength_MessageTooLong) { 310 EnsureTlsSetup(); 311 // the first byte of the length was replaced with 0xff. 312 // The message now is too long. 313 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 1, 0xff); 314 filter->EnableDecryption(); 315 filter->Disable(); 316 Connect(); 317 318 filter->Enable(); 319 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 320 filter->Disable(); 321 322 ExpectAlert(server_, kTlsAlertDecodeError); 323 client_->ExpectReceiveAlert(kTlsAlertDecodeError); 324 325 server_->ExpectReadWriteError(); 326 client_->ExpectReadWriteError(); 327 server_->ReadBytes(); 328 client_->ReadBytes(); 329 330 server_->CheckErrorCode(SSL_ERROR_RX_MALFORMED_HANDSHAKE); 331 client_->CheckErrorCode(SSL_ERROR_DECODE_ERROR_ALERT); 332 333 // Even if the client has updated his writing key, 334 client_->CheckEpochs(3, 4); 335 // the server has not. 336 server_->CheckEpochs(3, 3); 337 } 338 339 TEST_F(TlsConnectStreamTls13, TLSKeyUpdateWrongValueForLength_MessageTooShort) { 340 EnsureTlsSetup(); 341 // Changing the value of length of the KU message to be shorter than the 342 // correct one. 343 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 0x3, 0x00); 344 filter->EnableDecryption(); 345 filter->Disable(); 346 Connect(); 347 348 filter->Enable(); 349 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 350 filter->Disable(); 351 352 ExpectAlert(server_, kTlsAlertDecodeError); 353 client_->ExpectReceiveAlert(kTlsAlertCloseNotify); 354 355 client_->SendData(10); 356 server_->ReadBytes(); 357 } 358 359 // DTLS1.3 tests 360 361 // The KeyUpdate in DTLS1.3 workflow (with the update_requested set): 362 363 // Client(P1) is asking for KeyUpdate 364 // Here the second parameter states whether the P1 requires update_requested 365 // (RFC9147, Section 8). 366 // EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), 367 // PR_FALSE)); 368 369 // The server (P2) receives the KeyUpdate request and processes it. 370 // server_->ReadBytes(); 371 372 // P2 sends ACK. 373 // SSLInt_SendImmediateACK(server_->ssl_fd()); 374 375 // P1 receives ACK and finished the KeyUpdate: 376 // client_->ReadBytes(); 377 378 // This function sends and proceeds KeyUpdate explained above (assuming 379 // updateRequested == PR_FALSE) For the explantation of the updateRequested look 380 // at the test DTLSKeyUpdateClientUpdateRequestedSucceed.*/ 381 static void SendAndProcessKU(const std::shared_ptr<TlsAgent>& sender, 382 const std::shared_ptr<TlsAgent>& receiver, 383 bool updateRequested) { 384 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(sender->ssl_fd(), updateRequested)); 385 receiver->ReadBytes(); 386 // It takes some time to send an ack message, so here we send it immediately 387 SSLInt_SendImmediateACK(receiver->ssl_fd()); 388 sender->ReadBytes(); 389 if (updateRequested) { 390 SSLInt_SendImmediateACK(sender->ssl_fd()); 391 receiver->ReadBytes(); 392 } 393 } 394 395 // This test checks that after the execution of KeyUpdate started by the client, 396 // the writing client/reading server key epoch was incremented. 397 // RFC 9147. Section 4. 398 // However, this value is set [...] of the connection epoch, 399 // which is an [...] counter incremented on every KeyUpdate. 400 TEST_F(TlsConnectDatagram13, DTLSKU_ClientKUSucceed) { 401 Connect(); 402 CheckEpochs(3, 3); 403 // Client starts KeyUpdate 404 // The updateRequested is not requested. 405 SendAndProcessKU(client_, server_, PR_FALSE); 406 // The KeyUpdate is finished, and the client writing spec/the server reading 407 // spec is incremented. 408 CheckEpochs(4, 3); 409 // Check that we can send/receive data after KeyUpdate. 410 SendReceive(50); 411 } 412 413 // This test checks that only one KeyUpdate is possible at the same time. 414 // RFC 9147 Section 5.8.4 415 // In contrast, implementations MUST NOT send KeyUpdate, NewConnectionId, or 416 // RequestConnectionId messages if an earlier message of the same type has not 417 // yet been acknowledged. 418 TEST_F(TlsConnectDatagram13, DTLSKU_ClientKUTwiceOnceIgnored) { 419 Connect(); 420 CheckEpochs(3, 3); 421 // Client sends a key update message. 422 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 423 // The second key update message will be ignored as there is KeyUpdate in 424 // progress. 425 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 426 // For the workflow see ssl_KeyUpdate_unittest.cc:SendAndProcessKU. 427 server_->ReadBytes(); 428 SSLInt_SendImmediateACK(server_->ssl_fd()); 429 client_->ReadBytes(); 430 // As only one KeyUpdate was executed, the key epoch was incremented only 431 // once. 432 CheckEpochs(4, 3); 433 SendReceive(50); 434 } 435 436 // This test checks the same as the test DTLSKeyUpdateClientKeyUpdateSucceed, 437 // except that the server sends KeyUpdate. 438 TEST_F(TlsConnectDatagram13, DTLSKU_ServerKUSucceed) { 439 Connect(); 440 CheckEpochs(3, 3); 441 SendAndProcessKU(server_, client_, PR_FALSE); 442 CheckEpochs(3, 4); 443 SendReceive(50); 444 } 445 446 // This test checks the same as the test 447 // DTLSKeyUpdateClientKeyUpdateTwiceOnceIgnored, except that the server sends 448 // KeyUpdate. 449 TEST_F(TlsConnectDatagram13, DTLSKU_PreviousKUNotYetACKed) { 450 Connect(); 451 CheckEpochs(3, 3); 452 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 453 // The second key update message will be ignored 454 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 455 456 client_->ReadBytes(); 457 SSLInt_SendImmediateACK(client_->ssl_fd()); 458 server_->ReadBytes(); 459 460 CheckEpochs(3, 4); 461 // Checking that we still can send/receive data. 462 SendReceive(50); 463 } 464 465 // This test checks that if we receive two KeyUpdates, one will be ignored 466 TEST_F(TlsConnectDatagram13, DTLSKU_TwiceReceivedOnceIgnored) { 467 Connect(); 468 CheckEpochs(3, 3); 469 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(server_); 470 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 471 472 // Here we check that there was no KeyUpdate happened 473 client_->ReadBytes(); 474 SSLInt_SendImmediateACK(client_->ssl_fd()); 475 server_->ReadBytes(); 476 CheckEpochs(3, 3); 477 478 DataBuffer d = filter->ReturnRecorded(); 479 // Sending the recorded KeyUpdate 480 server_->SendDirect(d); 481 // Sending the KeyUpdate again 482 server_->SendDirect(d); 483 484 client_->ReadBytes(); 485 SSLInt_SendImmediateACK(client_->ssl_fd()); 486 server_->ReadBytes(); 487 488 // We observe that only one KeyUpdate has happened 489 CheckEpochs(3, 4); 490 // Checking that we still can send/receive data. 491 SendReceive(50); 492 } 493 494 // The KeyUpdate in DTLS1.3 workflow (with the update_requested set): 495 496 // Client(P1) is asking for KeyUpdate 497 // EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_TRUE)); 498 499 // The server (P2) receives and processes the KeyUpdate request 500 // At the same time, P2 sends its own KeyUpdate request (due to update_requested 501 // was set) 502 // server_->ReadBytes(); 503 504 // P1 receives the ACK and finalizes the KeyUpdate. 505 // SSLInt_SendImmediateACK(server_->ssl_fd()); 506 507 // P1 receives the KeyUpdate request and processes it. 508 // client_->ReadBytes(); 509 510 // P2 receives the ACK and finalizes the KeyUpdate. 511 // SSLInt_SendImmediateACK(client_->ssl_fd()); 512 // server_->ReadBytes(); 513 514 // This test checks that after the KeyUpdate (with update requested set) 515 // both client w/r and server w/r key epochs were incremented. 516 TEST_F(TlsConnectDatagram13, DTLSKU_UpdateRequestedSucceed) { 517 Connect(); 518 CheckEpochs(3, 3); 519 // Here the second parameter sets the update_requested to true. 520 SendAndProcessKU(client_, server_, PR_TRUE); 521 // As there were two KeyUpdates executed (one by a client, another one by a 522 // server) Both of the keys were modified. 523 CheckEpochs(4, 4); 524 // Checking that we still can send/receive data. 525 SendReceive(50); 526 } 527 528 // This test checks that after two KeyUpdates (with update requested set) 529 // the keys epochs were incremented twice. 530 TEST_F(TlsConnectDatagram13, DTLSKU_UpdateRequestedTwiceSucceed) { 531 Connect(); 532 CheckEpochs(3, 3); 533 SendAndProcessKU(client_, server_, PR_TRUE); 534 // The KeyUpdate is finished, so both of the epochs got incremented. 535 CheckEpochs(4, 4); 536 SendAndProcessKU(client_, server_, PR_TRUE); 537 // The second KeyUpdate is finished, so finally the epochs were incremented 538 // twice. 539 CheckEpochs(5, 5); 540 // Checking that we still can send/receive data. 541 SendReceive(50); 542 } 543 544 // This test checks the same as the test DTLSKeyUpdateUpdateRequestedSucceed, 545 // except that the server sends KeyUpdate. 546 TEST_F(TlsConnectDatagram13, DTLSKU_ServerUpdateRequestedSucceed) { 547 Connect(); 548 CheckEpochs(3, 3); 549 SendAndProcessKU(server_, client_, PR_TRUE); 550 CheckEpochs(4, 4); 551 SendReceive(50); 552 } 553 554 // This test checks that after two KeyUpdates (with update requested set) 555 // the keys epochs were incremented twice. 556 TEST_F(TlsConnectDatagram13, DTLSKU_ServerUpdateRequestedTwiceSucceed) { 557 Connect(); 558 CheckEpochs(3, 3); 559 SendAndProcessKU(server_, client_, PR_TRUE); 560 // The KeyUpdate is finished, so both of the epochs got incremented. 561 CheckEpochs(4, 4); 562 563 // Server sends another KeyUpdate 564 SendAndProcessKU(server_, client_, PR_TRUE); 565 // The second KeyUpdate is finished, so finally the epochs were incremented 566 // twice. 567 CheckEpochs(5, 5); 568 // Checking that we still can send/receive data. 569 SendReceive(50); 570 } 571 572 // This test checks that both client and server can send the KeyUpdate in 573 // consequence. 574 TEST_F(TlsConnectDatagram13, DTLSKU_ClientServerConseqSucceed) { 575 Connect(); 576 CheckEpochs(3, 3); 577 SendAndProcessKU(client_, server_, PR_FALSE); 578 // As the server initiated KeyUpdate and did not request an update_request, 579 // Only the server writing/client reading key epoch was incremented. 580 CheckEpochs(4, 3); 581 SendAndProcessKU(server_, client_, PR_FALSE); 582 // Now the client initiated KeyUpdate and did not request an update_request, 583 // so now both of epochs got incremented. 584 CheckEpochs(4, 4); 585 // Checking that we still can send/receive data. 586 SendReceive(50); 587 } 588 589 // This test checks that both client and server can send the KeyUpdate in 590 // consequence. Compared to the DTLSKeyUpdateClientServerConseqSucceed TV, this 591 // time both parties set update_requested to be true. 592 TEST_F(TlsConnectDatagram13, DTLSKU_ClientServerUpdateRequestedBothSucceed) { 593 Connect(); 594 CheckEpochs(3, 3); 595 SendAndProcessKU(client_, server_, PR_TRUE); 596 SendAndProcessKU(server_, client_, PR_TRUE); 597 // The second KeyUpdate (update_request = True) increments again the epochs 598 // of both keys. 599 CheckEpochs(5, 5); 600 // Checking that we still can send/receive data. 601 SendReceive(50); 602 } 603 604 // This test checks that if there is an ongoing KeyUpdate, the one started 605 // durint the KU is not going to be executed. 606 TEST_F(TlsConnectDatagram13, DTLSKU_KUInTheMiddleIsRejected) { 607 Connect(); 608 CheckEpochs(3, 3); 609 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 610 client_->ReadBytes(); 611 SSLInt_SendImmediateACK(client_->ssl_fd()); 612 // Here a client starts KeyUpdate at the same time as the ongoing KeyUpdate 613 // This KeyUpdate will not execute 614 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_TRUE)); 615 server_->ReadBytes(); 616 SSLInt_SendImmediateACK(server_->ssl_fd()); 617 client_->ReadBytes(); 618 // As there was only one KeyUpdate executed, both keys got incremented only 619 // once. 620 CheckEpochs(4, 4); 621 // Checking that we still can send/receive data. 622 SendReceive(50); 623 } 624 625 // DTLS1.3 KeyUpdate - Immediate Send Tests. 626 627 // The expected behaviour of the protocol: 628 // P1 starts initiates KeyUpdate 629 // P2 receives KeyUpdate 630 // And this moment, P2 will update the reading key to n 631 // But P2 will be accepting the keys from the previous epoch until a new message 632 // encrypted with the epoch n arrives. 633 634 // This test checks that when a client sent KeyUpdate, but the KeyUpdate message 635 // was not yet received, client can still send data. 636 TEST_F(TlsConnectDatagram13, DTLSKU_ClientImmediateSend) { 637 Connect(); 638 // Client has initiated KeyUpdate 639 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 640 // Server has not yet received it, client is trying to send some additional 641 // data. 642 CheckEpochs(3, 3); 643 client_->SendData(10); 644 // Server successfully receives it. 645 WAIT_(server_->received_bytes() == 10, 2000); 646 ASSERT_EQ((size_t)10, server_->received_bytes()); 647 SendReceive(50); 648 } 649 650 // This test checks that when a client sent KeyUpdate, but the KeyUpdate message 651 // was not yet received, it can still receive data. 652 TEST_F(TlsConnectDatagram13, DTLSKU_ServerImmediateSend) { 653 Connect(); 654 // Client has initiated KeyUpdate 655 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 656 // The server can successfully send data. 657 CheckEpochs(3, 3); 658 server_->SendData(10); 659 WAIT_(client_->received_bytes() == 10, 2000); 660 ASSERT_EQ((size_t)10, client_->received_bytes()); 661 SendReceive(50); 662 } 663 664 // This test checks that when a client sent KeyUpdate, 665 // the server has not yet sent an ACK and the client has not yet ACKed 666 // KeyUpdate, both parties can exchange data. 667 TEST_F(TlsConnectDatagram13, DTLSKU_ClientImmediateSendAfterServerRead) { 668 Connect(); 669 // Client has initiated KeyUpdate 670 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 671 // Server receives KeyUpdate 672 server_->ReadBytes(); 673 // Client can send data before the server sending ACK and client receiving 674 // * ACK messages 675 // Only server keys got updated. 676 server_->CheckEpochs(4, 3); 677 client_->CheckEpochs(3, 3); 678 client_->SendData(10); 679 WAIT_(server_->received_bytes() == 10, 2000); 680 ASSERT_EQ((size_t)10, server_->received_bytes()); 681 // Server can send data 682 server_->SendData(10); 683 WAIT_(client_->received_bytes() == 10, 2000); 684 ASSERT_EQ((size_t)10, client_->received_bytes()); 685 SendReceive(50); 686 } 687 688 // This test checks that when a client sent KeyUpdate, but has not yet ACKed it, 689 // both parties can exchange data. 690 TEST_F(TlsConnectDatagram13, DTLSKU_ClientImmediateSendAfterServerReadAndACK) { 691 Connect(); 692 CheckEpochs(3, 3); 693 // Client has initiated KeyUpdate 694 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 695 // Server receives KeyUpdate 696 server_->ReadBytes(); 697 // Server sends ACK 698 SSLInt_SendImmediateACK(server_->ssl_fd()); 699 // Client can send data before he has received KeyUpdate 700 // Only server keys got updated. 701 server_->CheckEpochs(4, 3); 702 client_->CheckEpochs(3, 3); 703 client_->SendData(10); 704 WAIT_(server_->received_bytes() == 10, 2000); 705 ASSERT_EQ((size_t)10, server_->received_bytes()); 706 // Server can send data 707 server_->SendData(10); 708 WAIT_(client_->received_bytes() == 10, 2000); 709 ASSERT_EQ((size_t)10, client_->received_bytes()); 710 SendReceive(50); 711 } 712 713 // This test checks that the client writing epoch is updated only 714 // when the client has received the ACK. 715 // RFC 9147. Section 8 716 // As with other handshake messages with no built-in response, KeyUpdates MUST 717 // be acknowledged. 718 TEST_F(TlsConnectDatagram13, DTLSKU_ClientWritingEpochUpdatedAfterReceivedACK) { 719 Connect(); 720 // Previous epoch 721 CheckEpochs(3, 3); 722 // Client sends a KeyUpdate 723 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 724 // Server updates his reading key 725 server_->ReadBytes(); 726 // Now the server has a reading key = 4 727 server_->CheckEpochs(4, 3); 728 // But the client has a writing key = 3 729 client_->CheckEpochs(3, 3); 730 731 // Client sends a data, but using the old (3) keys 732 client_->SendData(10); 733 WAIT_(server_->received_bytes() == 10, 2000); 734 ASSERT_EQ((size_t)10, server_->received_bytes()); 735 736 server_->CheckEpochs(4, 3); 737 client_->CheckEpochs(3, 3); 738 739 SSLInt_SendImmediateACK(server_->ssl_fd()); 740 client_->ReadBytes(); 741 742 CheckEpochs(4, 3); 743 SendReceive(50); 744 } 745 746 // DTLS1.3 KeyUpdate - Testing the border conditions 747 // (i.e. the cases where we reached the highest epoch). 748 749 // This test checks that the maximum epoch will not be exceeded on KeyUpdate. 750 // RFC 9147. Section 8. 751 // In order to provide an extra margin of security, 752 // sending implementations MUST NOT allow the epoch to exceed 2^48-1. 753 754 // Here we use the maximum as 2^16, 755 // See bug https://bugzilla.mozilla.org/show_bug.cgi?id=1809872 756 // When the bug is solved, the constant is to be replaced with 2^48 as 757 // required by RFC. 758 TEST_F(TlsConnectDatagram13, DTLSKU_ClientMaxEpochReached) { 759 Connect(); 760 CheckEpochs(3, 3); 761 PRUint64 max_epoch_type = (0x1ULL << 16) - 1; 762 763 // We assign the maximum possible epochs 764 EXPECT_EQ(SECSuccess, 765 SSLInt_AdvanceWriteEpochNum(client_->ssl_fd(), max_epoch_type)); 766 EXPECT_EQ(SECSuccess, 767 SSLInt_AdvanceReadEpochNum(server_->ssl_fd(), max_epoch_type)); 768 CheckEpochs(max_epoch_type, 3); 769 // Upon trying to execute KeyUpdate, we return a SECFailure. 770 EXPECT_EQ(SECFailure, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 771 SendReceive(50); 772 } 773 774 // This test checks the compliance with the RFC 9147 stating the behaviour 775 // reaching the max epoch: RFC 9147 Section 8. If a sending implementation 776 // receives a KeyUpdate with request_update set to "update_requested", it MUST 777 // NOT send its own KeyUpdate if that would cause it to exceed these limits and 778 // SHOULD instead ignore the "update_requested" flag. 779 TEST_F(TlsConnectDatagram13, DTLSKU_ClientMaxEpochReachedUpdateRequested) { 780 Connect(); 781 CheckEpochs(3, 3); 782 783 PRUint64 max_epoch_type = (0x1ULL << 16) - 1; 784 785 // We assign the maximum possible epochs - 1. 786 EXPECT_EQ(SECSuccess, 787 SSLInt_AdvanceWriteEpochNum(client_->ssl_fd(), max_epoch_type)); 788 EXPECT_EQ(SECSuccess, 789 SSLInt_AdvanceReadEpochNum(server_->ssl_fd(), max_epoch_type)); 790 791 CheckEpochs(max_epoch_type, 3); 792 // Once we call KeyUpdate with update requested 793 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(server_->ssl_fd(), PR_TRUE)); 794 client_->ReadBytes(); 795 SSLInt_SendImmediateACK(client_->ssl_fd()); 796 server_->ReadBytes(); 797 SSLInt_SendImmediateACK(server_->ssl_fd()); 798 client_->ReadBytes(); 799 // Only one key (that has not reached the maximum epoch) was updated. 800 CheckEpochs(max_epoch_type, 4); 801 SendReceive(50); 802 } 803 804 // DTLS1.3 KeyUpdate - Automatic update tests 805 806 // RFC 9147 Section 4.5.3. 807 // Implementations SHOULD NOT protect more records than allowed by the limit 808 // specified for the negotiated AEAD. 809 // Implementations SHOULD initiate a key update before reaching this limit. 810 811 // These two tests check that the KeyUpdate is automatically called upon 812 // reaching the reading/writing limit. 813 TEST_F(TlsConnectDatagram13, DTLSKU_AutomaticOnWrite) { 814 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 815 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 816 CheckEpochs(3, 3); 817 818 // Set this to one below the write threshold. 819 uint64_t threshold = 0x438000000; 820 EXPECT_EQ(SECSuccess, 821 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 822 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 823 824 // This should be OK. 825 client_->SendData(10); 826 server_->ReadBytes(); 827 828 // This should cause the client to update. 829 client_->SendData(15); 830 server_->ReadBytes(); 831 SSLInt_SendImmediateACK(server_->ssl_fd()); 832 client_->ReadBytes(); 833 834 // The client key epoch was incremented. 835 CheckEpochs(4, 3); 836 // Checking that we still can send/receive data. 837 SendReceive(100); 838 } 839 840 TEST_F(TlsConnectDatagram13, DTLSKU_AutomaticOnRead) { 841 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 842 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 843 CheckEpochs(3, 3); 844 845 // Set this to one below the read threshold. 846 uint64_t threshold = 0x4ec000000 - 1; 847 EXPECT_EQ(SECSuccess, 848 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 849 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 850 851 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(client_); 852 client_->SendData(10); 853 DataBuffer d = filter->ReturnRecorded(); 854 855 client_->SendDirect(d); 856 // This message will cause the server to start KeyUpdate with updateRequested 857 // = 1. 858 server_->ReadBytes(); 859 860 SSLInt_SendImmediateACK(server_->ssl_fd()); 861 client_->ReadBytes(); 862 SSLInt_SendImmediateACK(client_->ssl_fd()); 863 server_->ReadBytes(); 864 865 // Both keys got updated. 866 CheckEpochs(4, 4); 867 // Checking that we still can send/receive data. 868 SendReceive(100); 869 } 870 871 // The test describes the situation when there was a request 872 // to execute an automatic KU, but the server has not responded. 873 TEST_F(TlsConnectDatagram13, DTLSKU_CanSendBeforeThreshold) { 874 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 875 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 876 CheckEpochs(3, 3); 877 878 uint64_t threshold = 0x5a0000000 - 2; 879 EXPECT_EQ(SECSuccess, 880 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 881 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 882 883 size_t received_bytes = server_->received_bytes(); 884 // We still can send a message 885 client_->SendData(15); 886 887 // We can not send a message anymore 888 client_->ExpectReadWriteError(); 889 client_->SendData(105); 890 891 server_->ReadBytes(); 892 // And it was not received. 893 ASSERT_EQ((size_t)received_bytes + 15, server_->received_bytes()); 894 } 895 896 TEST_F(TlsConnectDatagram13, DTLSKU_DiscardAfterThreshold) { 897 ConfigureVersion(SSL_LIBRARY_VERSION_TLS_1_3); 898 ConnectWithCipherSuite(TLS_AES_128_GCM_SHA256); 899 CheckEpochs(3, 3); 900 901 uint64_t threshold = 0x5a0000000 - 3; 902 EXPECT_EQ(SECSuccess, 903 SSLInt_AdvanceWriteSeqNum(client_->ssl_fd(), threshold)); 904 EXPECT_EQ(SECSuccess, SSLInt_AdvanceReadSeqNum(server_->ssl_fd(), threshold)); 905 906 size_t received_bytes = server_->received_bytes(); 907 908 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(client_); 909 client_->SendData(30); 910 DataBuffer d = filter->ReturnRecorded(); 911 912 client_->SendDirect(d); 913 client_->SendDirect(d); 914 915 server_->ReadBytes(); 916 // Only one message was received. 917 ASSERT_EQ((size_t)received_bytes + 30, server_->received_bytes()); 918 } 919 920 // DTLS1.3 KeyUpdate - Managing previous epoch messages 921 // RFC 9147 Section 8. 922 // Due to the possibility of an ACK message for a KeyUpdate being lost 923 // and thereby preventing the sender of the KeyUpdate from updating its 924 // keying material, receivers MUST retain the pre-update keying material 925 // until receipt and successful decryption of a message using the new 926 // keys. 927 928 // This test checks that message encrypted with the key n-1 will be accepted 929 // after KeyUpdate is executed, but before the message n has arrived. 930 TEST_F(TlsConnectDatagram13, DTLSKU_PreviousEpochIsAcceptedBeforeNew) { 931 size_t len = 10; 932 933 Connect(); 934 // Client starts KeyUpdate 935 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 936 // Server receives KeyUpdate and sends ACK 937 server_->ReadBytes(); 938 SSLInt_SendImmediateACK(server_->ssl_fd()); 939 // Client has not yet received the ACK, so the writing key epoch has not 940 // changed 941 client_->CheckEpochs(3, 3); 942 server_->CheckEpochs(4, 3); 943 944 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(client_); 945 946 // Here the message previousEpochMessageBuffer contains a message 947 // encrypted with the client 3rd epoch key, m1 = enc(message, key_3) 948 client_->SendData(len); 949 DataBuffer d = filter->ReturnRecorded(); 950 951 // Client has received the ACK 952 client_->ReadBytes(); 953 // Now he updates the writing Key to 4 954 client_->CheckEpochs(3, 4); 955 server_->CheckEpochs(4, 3); 956 957 // And now we resend the message m1 and successfully receive it 958 client_->SendDirect(d); 959 WAIT_(server_->received_bytes() == len, 2000); 960 ASSERT_EQ(len, server_->received_bytes()); 961 // Checking that we still can send/receive data. 962 SendReceive(50); 963 } 964 965 // This test checks that message encrypted with the key n-2 will not be accepted 966 // after KeyUpdate is executed, but before the message n has arrived. 967 TEST_F(TlsConnectDatagram13, DTLSKU_2EpochsAgoIsRejected) { 968 size_t len = 10; 969 970 Connect(); 971 CheckEpochs(3, 3); 972 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(client_); 973 client_->SendData(len); 974 DataBuffer d = filter->ReturnRecorded(); 975 client_->ResetSentBytes(); 976 977 SendAndProcessKU(client_, server_, PR_FALSE); 978 SendAndProcessKU(client_, server_, PR_FALSE); 979 980 // Executing 2 KeyUpdates, so the client writing key is equal to 5 now 981 CheckEpochs(5, 3); 982 // And now we resend the message m1 encrypted with the key n-2 (3) 983 client_->SendDirect(d); 984 server_->ReadBytes(); 985 // Server has still received just legal_message_len of bytes (not the 986 // previousEpochLen + legal_message_len) 987 ASSERT_EQ((size_t)0, server_->received_bytes()); 988 // Checking that we still can send/receive data. 989 SendReceive(60); 990 } 991 992 // This test checks that that message encrypted with the key n-1 will be 993 // rejected after KeyUpdate is executed, and after the message n has arrived. 994 TEST_F(TlsConnectDatagram13, DTLSKU_PreviousEpochIsAcceptedAfterNew) { 995 size_t len = 30; 996 size_t legal_message_len = 20; 997 998 Connect(); 999 // Client starts KeyUpdate 1000 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 1001 // Server receives KeyUpdate and sends ACK 1002 server_->ReadBytes(); 1003 SSLInt_SendImmediateACK(server_->ssl_fd()); 1004 // Client has not yet received the ACK, so the writing key epoch has not 1005 // changed 1006 client_->CheckEpochs(3, 3); 1007 server_->CheckEpochs(4, 3); 1008 1009 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(client_); 1010 1011 // Here the message previousEpochMessageBuffer contains a message 1012 // encrypted with the client 3rd epoch key, m1 = enc(message, key_3) 1013 client_->SendData(len); 1014 DataBuffer d = filter->ReturnRecorded(); 1015 client_->ResetSentBytes(); 1016 1017 // Client has received the ACK 1018 client_->ReadBytes(); 1019 client_->CheckEpochs(3, 4); 1020 server_->CheckEpochs(4, 3); 1021 1022 // At this moment, a client will send a message with the new key 1023 SendReceive(legal_message_len); 1024 // As soon as it's received, the server will forbid the messaged from the 1025 // previous epochs 1026 server_->ReadBytes(); 1027 1028 // If a message from the previous epoch arrives to the server (m1, the key_3 1029 // was used to encrypt it) 1030 client_->SendDirect(d); 1031 // it will be silently dropped 1032 server_->ReadBytes(); 1033 // Server has still received just legal_message_len of bytes (not the 1034 // previousEpochLen + legal_message_len) 1035 ASSERT_EQ((size_t)legal_message_len, server_->received_bytes()); 1036 // Checking that we still can send/receive data. 1037 SendReceive(50); 1038 } 1039 1040 // DTLS Epoch reconstruction test 1041 // RFC 9147 Section 8. 4.2.2. Reconstructing the Sequence Number and Epoch 1042 1043 // This test checks that the epoch reconstruction is correct. 1044 // The function under testing is dtlscon.c::dtls_ReadEpoch. 1045 // We only consider the case when dtls_IsDtls13Ciphertext is true. 1046 1047 typedef struct sslKeyUpdateReadEpochTVStr { 1048 // The current epoch 1049 DTLSEpoch epoch; 1050 // Only two-bit epoch here 1051 PRUint8 header; 1052 DTLSEpoch expected_reconstructed_epoch; 1053 } sslKeyUpdateReadEpochTV_t; 1054 1055 static const sslKeyUpdateReadEpochTV_t sslKeyUpdateReadEpochTV[26] = { 1056 {0x1, 0x1, 0x1}, 1057 1058 {0x2, 0x1, 0x1}, 1059 {0x2, 0x2, 0x2}, 1060 1061 {0x3, 0x3, 0x3}, 1062 {0x3, 0x2, 0x2}, 1063 {0x3, 0x1, 0x1}, 1064 1065 {0x4, 0x0, 0x4}, // the difference (diff) between the reconstructed and 1066 // the current epoch is equal to 0 1067 {0x4, 0x1, 0x1}, // diff == 3 1068 {0x4, 0x2, 0x2}, // diff == 2 1069 {0x4, 0x3, 0x3}, // diff == 1 1070 1071 {0x5, 0x0, 0x4}, // diff == 1 1072 {0x5, 0x1, 0x5}, // diff == 0 1073 {0x5, 0x2, 0x2}, // diff == 3 1074 {0x5, 0x3, 0x3}, // diff == 2 1075 1076 {0x6, 0x0, 0x4}, 1077 {0x6, 0x1, 0x5}, 1078 {0x6, 0x2, 0x6}, 1079 {0x6, 0x3, 0x3}, 1080 1081 {0x7, 0x0, 0x4}, 1082 {0x7, 0x1, 0x5}, 1083 {0x7, 0x2, 0x6}, 1084 {0x7, 0x3, 0x7}, 1085 1086 {0x8, 0x0, 0x8}, 1087 {0x8, 0x1, 0x5}, 1088 {0x8, 0x2, 0x6}, 1089 {0x8, 0x3, 0x7}, 1090 1091 // Starting from here the pattern (starting from 4) repeats: 1092 // if a current epoch is equal to n, 1093 // the difference will behave as for n % 4 + 4. 1094 // For example, if the current epoch is equal to 9, then 1095 // the difference between the reconstructed epoch and the current one 1096 // will be the same as for the 5th epoch. 1097 }; 1098 1099 TEST_F(TlsConnectDatagram13, DTLS_EpochReconstruction) { 1100 PRUint8 header[5] = {0}; 1101 header[0] = 0x20; 1102 DTLSEpoch epoch; 1103 1104 for (size_t i = 0; i < 26; i++) { 1105 epoch = sslKeyUpdateReadEpochTV[i].epoch; 1106 header[0] = (header[0] & 0xfc) | (sslKeyUpdateReadEpochTV[i].header & 0x3); 1107 // ReadEpoch (dtlscon.c#1339) uses only spec->version and spec->epoch. 1108 ASSERT_EQ(sslKeyUpdateReadEpochTV[i].expected_reconstructed_epoch, 1109 dtls_ReadEpoch(SSL_LIBRARY_VERSION_TLS_1_3, epoch, header)); 1110 } 1111 } 1112 1113 // RFC 9147. A.2. Handshake Protocol 1114 // struct { 1115 // HandshakeType msg_type; -- handshake type 1116 // uint24 length; -- bytes in message 1117 // uint16 message_seq; -- DTLS-required field 1118 // uint24 fragment_offset; -- DTLS-required field 1119 // uint24 fragment_length; -- DTLS-required field 1120 // select (msg_type) { 1121 // ... 1122 // case key_update: KeyUpdate; 1123 // } body; 1124 // } Handshake; 1125 // 1126 // enum { 1127 // update_not_requested(0), update_requested(1), (255) 1128 // } KeyUpdateRequest; 1129 1130 // The next tests send malformed KeyUpdate messages. 1131 // A remainder: TLSKeyUpdateDamager filter takes as an input an agent, 1132 // a byte index and a value that the existing value of the byte with the byte 1133 // index will be replaced with. The filter catchs only the KeyUpdate messages, 1134 // keeping unchanged all the rest. 1135 1136 // The first test, DTLSKeyUpdateDamagerFilterTestingNoModification, 1137 // checks the correctness of the filter itself. It replaces the value of 12th 1138 // byte with 0: The 12th byte is used to specify KeyUpdateRequest. Thus, the 1139 // modification done in the test will still result in the correct KeyUpdate 1140 // request. 1141 1142 // The test DTLSKU_WrongValueForUpdateRequested is modifying 1143 // KeyUpdateRequest byte to have an not-allowed value. 1144 1145 // The test DTLSKeyUpdateDamagedLength modifies the 3rd byte (one of the length 1146 // bytes). 1147 1148 // The test DTLSKeyUpdateDamagedLengthLongMessage changes the length of the 1149 // message as well. 1150 1151 // The test DTLSKeyUpdateDamagedFragmentLength modifies the 10th byte (one of 1152 // the fragment_length bytes) 1153 1154 TEST_F(TlsConnectDatagram13, DTLSKU_WrongValueForUpdateRequested) { 1155 EnsureTlsSetup(); 1156 // Filter replacing the update_requested with an unexpected value. 1157 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 12, 2); 1158 filter->EnableDecryption(); 1159 filter->Disable(); 1160 Connect(); 1161 filter->Enable(); 1162 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 1163 filter->Disable(); 1164 1165 ExpectAlert(server_, kTlsAlertDecodeError); 1166 client_->ExpectReceiveAlert(kTlsAlertDecodeError); 1167 1168 server_->ExpectReadWriteError(); 1169 client_->ExpectReadWriteError(); 1170 1171 server_->ReadBytes(); 1172 client_->ReadBytes(); 1173 1174 server_->CheckErrorCode(SSL_ERROR_RX_MALFORMED_KEY_UPDATE); 1175 client_->CheckErrorCode(SSL_ERROR_DECODE_ERROR_ALERT); 1176 1177 // No KeyUpdate happened. 1178 CheckEpochs(3, 3); 1179 } 1180 1181 TEST_F(TlsConnectDatagram13, DTLSKU_DamagedLength) { 1182 EnsureTlsSetup(); 1183 // Filter replacing the length value with 0. 1184 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 3, 0); 1185 filter->EnableDecryption(); 1186 filter->Disable(); 1187 Connect(); 1188 filter->Enable(); 1189 1190 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 1191 filter->Disable(); 1192 SSLInt_SendImmediateACK(server_->ssl_fd()); 1193 client_->ReadBytes(); 1194 // No KeyUpdate happened. 1195 CheckEpochs(3, 3); 1196 SendReceive(50); 1197 } 1198 1199 TEST_F(TlsConnectDatagram13, DTLSKU_DamagedLengthTooLong) { 1200 EnsureTlsSetup(); 1201 // Filter replacing the second byte of length with one 1202 // The message length is increased by 2 ^ 8 1203 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 2, 2); 1204 filter->EnableDecryption(); 1205 filter->Disable(); 1206 Connect(); 1207 filter->Enable(); 1208 1209 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 1210 filter->Disable(); 1211 SSLInt_SendImmediateACK(server_->ssl_fd()); 1212 client_->ReadBytes(); 1213 // No KeyUpdate happened. 1214 CheckEpochs(3, 3); 1215 SendReceive(50); 1216 } 1217 1218 TEST_F(TlsConnectDatagram13, DTLSKU_DamagedFragmentLength) { 1219 EnsureTlsSetup(); 1220 // Filter replacing the fragment length with 1. 1221 auto filter = MakeTlsFilter<TLSKeyUpdateDamager>(client_, 10, 1); 1222 filter->EnableDecryption(); 1223 filter->Disable(); 1224 Connect(); 1225 filter->Enable(); 1226 1227 SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE); 1228 filter->Disable(); 1229 SSLInt_SendImmediateACK(server_->ssl_fd()); 1230 client_->ReadBytes(); 1231 // No KeyUpdate happened. 1232 CheckEpochs(3, 3); 1233 SendReceive(50); 1234 } 1235 1236 // This filter is used in order to modify an ACK message. 1237 // As it's possible that one record contains several ACKs, 1238 // we fault all of them. 1239 1240 class TLSACKDamager : public TlsRecordFilter { 1241 public: 1242 TLSACKDamager(const std::shared_ptr<TlsAgent>& a, size_t byte, uint8_t val) 1243 : TlsRecordFilter(a), offset_(byte), value_(val) {} 1244 1245 protected: 1246 PacketFilter::Action FilterRecord(const TlsRecordHeader& header, 1247 const DataBuffer& record, size_t* offset, 1248 DataBuffer* output) override { 1249 if (!header.is_protected()) { 1250 return KEEP; 1251 } 1252 1253 uint16_t protection_epoch; 1254 uint8_t inner_content_type; 1255 DataBuffer plaintext; 1256 TlsRecordHeader out_header; 1257 1258 if (!Unprotect(header, record, &protection_epoch, &inner_content_type, 1259 &plaintext, &out_header)) { 1260 return KEEP; 1261 } 1262 1263 if (plaintext.data() == NULL || plaintext.len() == 0) { 1264 return KEEP; 1265 } 1266 1267 if (decrypting() && inner_content_type != ssl_ct_ack) { 1268 return KEEP; 1269 } 1270 1271 // We compute the number of ACKS in the message 1272 // As we keep processing the ACK even if one message is incorrent, 1273 // we fault all the found ACKs. 1274 1275 uint8_t ack_message_header_len = 2; 1276 uint8_t ack_message_len_one_ACK = 16; 1277 uint64_t acks = plaintext.len() - ack_message_header_len; 1278 EXPECT_EQ((uint64_t)0, acks % ack_message_len_one_ACK); 1279 acks = acks / ack_message_len_one_ACK; 1280 1281 if (plaintext.len() <= ack_message_header_len + offset_ + 1282 (acks - 1) * ack_message_len_one_ACK) { 1283 return KEEP; 1284 } 1285 1286 for (size_t i = 0; i < acks; i++) { 1287 // Here we replace the offset_-th byte after the header 1288 // i.e. headerAck + ACK(0) + ACK(1) <-- the offset_-th byte 1289 // of ACK(0), ACK(1), etc 1290 plaintext.data()[ack_message_header_len + offset_ + 1291 i * ack_message_len_one_ACK] = value_; 1292 } 1293 1294 DataBuffer ciphertext; 1295 bool ok = Protect(spec(protection_epoch), out_header, inner_content_type, 1296 plaintext, &ciphertext, &out_header); 1297 if (!ok) { 1298 return KEEP; 1299 } 1300 *offset = out_header.Write(output, *offset, ciphertext); 1301 return CHANGE; 1302 } 1303 1304 protected: 1305 size_t offset_; 1306 uint8_t value_; 1307 }; 1308 1309 // The next two tests are modifying the ACK message: 1310 1311 // First, we call KeyUpdate on the client side. The server successfully 1312 // processes it, and it's sending an ACK message. At this moment, the filter 1313 // modifies the content of the ACK message by changing the seqNum or epoch and 1314 // sends it back to the client. 1315 // 1316 // struct { 1317 // uint64 epoch; 1318 // uint64 sequence_number; 1319 // } RecordNumber; 1320 1321 // struct { 1322 // RecordNumber record_numbers<0..2^16-1>; 1323 // } ACK; 1324 1325 TEST_F(TlsConnectDatagram13, DTLSKU_ModifACKEpoch) { 1326 EnsureTlsSetup(); 1327 uint8_t byte = 3; 1328 uint8_t v = 1; 1329 // The filter will replace value-th byte of each ACK with one 1330 // The epoch will be more than v * 2 ^ ((byte - 1) * 8). 1331 // HandleACK function allows the epochs such that (epoch > RECORD_EPOCH_MAX) 1332 // where RECORD_EPOCH_MAX == ((0x1ULL << 16) - 1) 1333 auto filter = MakeTlsFilter<TLSACKDamager>(server_, byte, v); 1334 filter->EnableDecryption(); 1335 filter->Disable(); 1336 Connect(); 1337 CheckEpochs(3, 3); 1338 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 1339 server_->ReadBytes(); 1340 1341 filter->Enable(); 1342 SSLInt_SendImmediateACK(server_->ssl_fd()); 1343 filter->Disable(); 1344 1345 client_->ReadBytes(); 1346 server_->CheckEpochs(4, 3); 1347 // The client has not received the ACK, so it will not update the key. 1348 client_->CheckEpochs(3, 3); 1349 1350 // The communication still continues. 1351 SendReceive(50); 1352 } 1353 1354 TEST_F(TlsConnectDatagram13, DTLSKU_ModifACKSeqNum) { 1355 EnsureTlsSetup(); 1356 uint8_t byte = 7; 1357 uint8_t v = 1; 1358 // The filter will replace value byte of each ACK with one 1359 // The seqNum will be more than v * 2 ^ ((byte - 1) * 8). 1360 // HandleACK function allows the epochs such that (seq > RECORD_SEQ_MAX) 1361 // where RECORD_SEQ_MAX == ((0x1ULL << 48) - 1) 1362 1363 // here byte + 8 means that we modify not epoch, but sequenceNum 1364 auto filter = MakeTlsFilter<TLSACKDamager>(server_, byte + 8, v); 1365 filter->EnableDecryption(); 1366 filter->Disable(); 1367 Connect(); 1368 1369 EXPECT_EQ(SECSuccess, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 1370 server_->ReadBytes(); 1371 1372 filter->Enable(); 1373 SSLInt_SendImmediateACK(server_->ssl_fd()); 1374 filter->Disable(); 1375 1376 client_->ReadBytes(); 1377 1378 client_->ReadBytes(); 1379 server_->CheckEpochs(4, 3); 1380 // The client has not received the ACK, so it will not update the key. 1381 client_->CheckEpochs(3, 3); 1382 1383 // The communication still continues. 1384 SendReceive(50); 1385 } 1386 1387 TEST_F(TlsConnectDatagram13, DTLSKU_TooEarly_ClientCannotSendKeyUpdate) { 1388 StartConnect(); 1389 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(server_); 1390 filter->EnableDecryption(); 1391 1392 client_->Handshake(); 1393 server_->Handshake(); 1394 1395 EXPECT_EQ(SECFailure, SSL_KeyUpdate(client_->ssl_fd(), PR_FALSE)); 1396 } 1397 1398 TEST_F(TlsConnectDatagram13, DTLSKeyUpdateTooEarly_ServerCannotSendKeyUpdate) { 1399 StartConnect(); 1400 auto filter = MakeTlsFilter<TLSRecordSaveAndDropNext>(server_); 1401 filter->EnableDecryption(); 1402 1403 client_->Handshake(); 1404 server_->Handshake(); 1405 1406 EXPECT_EQ(SECFailure, SSL_KeyUpdate(server_->ssl_fd(), PR_FALSE)); 1407 } 1408 1409 class DTlsEncryptedHandshakeHeaderReplacer : public TlsRecordFilter { 1410 public: 1411 DTlsEncryptedHandshakeHeaderReplacer(const std::shared_ptr<TlsAgent>& a, 1412 uint8_t old_ct, uint8_t new_ct) 1413 : TlsRecordFilter(a), 1414 old_ct_(old_ct), 1415 new_ct_(new_ct), 1416 replaced_(false) {} 1417 1418 protected: 1419 PacketFilter::Action FilterRecord(const TlsRecordHeader& header, 1420 const DataBuffer& record, size_t* offset, 1421 DataBuffer* output) override { 1422 if (replaced_) return KEEP; 1423 1424 uint8_t inner_content_type; 1425 DataBuffer plaintext; 1426 uint16_t protection_epoch = 0; 1427 TlsRecordHeader out_header(header); 1428 1429 if (!Unprotect(header, record, &protection_epoch, &inner_content_type, 1430 &plaintext, &out_header)) { 1431 return KEEP; 1432 } 1433 1434 auto& protection_spec = spec(protection_epoch); 1435 uint32_t msg_type = 256; // Not a real message 1436 if (!plaintext.Read(0, 1, &msg_type) || msg_type == old_ct_) { 1437 replaced_ = true; 1438 plaintext.Write(0, new_ct_, 1); 1439 } 1440 1441 uint64_t seq_num = protection_spec.next_out_seqno(); 1442 if (out_header.is_dtls()) { 1443 seq_num |= out_header.sequence_number() & (0xffffULL << 48); 1444 } 1445 out_header.sequence_number(seq_num); 1446 1447 DataBuffer ciphertext; 1448 bool rv = Protect(protection_spec, out_header, inner_content_type, 1449 plaintext, &ciphertext, &out_header); 1450 if (!rv) { 1451 return KEEP; 1452 } 1453 *offset = out_header.Write(output, *offset, ciphertext); 1454 return CHANGE; 1455 } 1456 1457 private: 1458 uint8_t old_ct_; 1459 uint8_t new_ct_; 1460 bool replaced_; 1461 }; 1462 1463 // The next tests check the behaviour of KU before the handshake is finished. 1464 TEST_F(TlsConnectDatagram13, DTLSKU_TooEarly_Client) { 1465 StartConnect(); 1466 // This filter takes the record and if it finds kTlsHandshakeFinished 1467 // it replaces it with kTlsHandshakeKeyUpdate 1468 // Then, the KeyUpdate will be started when the handshake is not yet finished 1469 // This handshake will be cancelled. 1470 auto filter = MakeTlsFilter<DTlsEncryptedHandshakeHeaderReplacer>( 1471 server_, kTlsHandshakeFinished, kTlsHandshakeKeyUpdate); 1472 filter->EnableDecryption(); 1473 1474 client_->Handshake(); 1475 server_->Handshake(); 1476 ExpectAlert(client_, kTlsAlertUnexpectedMessage); 1477 client_->Handshake(); 1478 client_->CheckErrorCode(SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE); 1479 server_->Handshake(); 1480 server_->CheckErrorCode(SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT); 1481 } 1482 1483 TEST_F(TlsConnectDatagram13, DTLSKU_TooEarly_Server) { 1484 StartConnect(); 1485 // This filter takes the record and if it finds kTlsHandshakeFinished 1486 // it replaces it with kTlsHandshakeKeyUpdate 1487 auto filter = MakeTlsFilter<DTlsEncryptedHandshakeHeaderReplacer>( 1488 client_, kTlsHandshakeFinished, kTlsHandshakeKeyUpdate); 1489 filter->EnableDecryption(); 1490 1491 client_->Handshake(); 1492 server_->Handshake(); 1493 client_->Handshake(); 1494 ExpectAlert(server_, kTlsAlertUnexpectedMessage); 1495 server_->Handshake(); 1496 server_->CheckErrorCode(SSL_ERROR_RX_UNEXPECTED_KEY_UPDATE); 1497 client_->Handshake(); 1498 client_->CheckErrorCode(SSL_ERROR_HANDSHAKE_UNEXPECTED_ALERT); 1499 } 1500 1501 } // namespace nss_test