| 432 | } |
| 433 | |
| 434 | int CEncryptedStreamSocket::Negotiate(const uint8* pBuffer, uint32 nLen) |
| 435 | { |
| 436 | uint32_t nRead = 0; |
| 437 | // Hitting Negotiate() with m_nReceiveBytesWanted == 0 means the |
| 438 | // negotiation state machine has consumed everything it was expecting |
| 439 | // for the current step but somebody (the kernel buffer flushing on |
| 440 | // a teardown, late bytes from a server we're switching away from, |
| 441 | // etc.) still posted a Read on this socket while it's stuck in |
| 442 | // ECS_NEGOTIATING. wxASSERT used to abort debug builds and silently |
| 443 | // UB in release (entering the while loop below with bogus byte |
| 444 | // math). wxCHECK_MSG returns the same -1 sentinel both call sites |
| 445 | // already check for (see Read() at the ECS_NEGOTIATING case and the |
| 446 | // "non-normal header" branch), so the caller cleanly aborts the |
| 447 | // connection instead. #778. |
| 448 | wxCHECK_MSG(m_nReceiveBytesWanted > 0, -1, |
| 449 | "CEncryptedStreamSocket::Negotiate: called with m_nReceiveBytesWanted == 0"); |
| 450 | |
| 451 | //DumpMem(pBuffer, nLen, "Negotiate buffer: "); |
| 452 | |
| 453 | try { |
| 454 | while (m_NegotiatingState != ONS_COMPLETE && m_nReceiveBytesWanted > 0) { |
| 455 | if (m_nReceiveBytesWanted > 512) { |
| 456 | wxFAIL; |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | const uint32_t nToRead = std::min(nLen - nRead, m_nReceiveBytesWanted); |
| 461 | //printf("Reading %i bytes, add from %i position on %i position\n",nToRead, nRead, (int)m_pfiReceiveBuffer.GetPosition()); |
| 462 | //DumpMem(pBuffer + nRead, nToRead, "Recv Buffer: "); |
| 463 | m_pfiReceiveBuffer.Write(pBuffer + nRead, nToRead); |
| 464 | nRead += nToRead; |
| 465 | m_nReceiveBytesWanted -= nToRead; |
| 466 | if (m_nReceiveBytesWanted > 0) { |
| 467 | return nRead; |
| 468 | } |
| 469 | |
| 470 | if (m_NegotiatingState != ONS_BASIC_CLIENTA_RANDOMPART && m_NegotiatingState != ONS_BASIC_SERVER_DHANSWER) { |
| 471 | // We have the keys, decrypt |
| 472 | //printf("We have the keys, so decrypt away on %s\n", (const char*) unicode2char(GetPeer())); |
| 473 | m_pfiReceiveBuffer.Encrypt(); |
| 474 | } |
| 475 | |
| 476 | m_pfiReceiveBuffer.Seek(0); |
| 477 | |
| 478 | switch (m_NegotiatingState) { |
| 479 | case ONS_NONE: // would be a bug |
| 480 | wxFAIL; |
| 481 | return 0; |
| 482 | case ONS_BASIC_CLIENTA_RANDOMPART: { |
| 483 | //printf("We are on ONS_BASIC_CLIENTA_RANDOMPART, create the keys on %s\n", (const char*) unicode2char(GetPeer())); |
| 484 | // This creates the send/receive keys. |
| 485 | |
| 486 | uint8_t achKeyData[21]; |
| 487 | md4cpy(achKeyData, thePrefs::GetUserHash().GetHash()); |
| 488 | m_pfiReceiveBuffer.Read(achKeyData + 17, 4); |
| 489 | |
| 490 | achKeyData[16] = MAGICVALUE_REQUESTER; |
| 491 |
nothing calls this directly
no test coverage detected