| 367 | } |
| 368 | |
| 369 | void CEncryptedStreamSocket::StartNegotiation(bool bOutgoing) |
| 370 | { |
| 371 | //printf("Starting socket negotiation\n"); |
| 372 | if (!bOutgoing) { |
| 373 | //printf("Incoming connection negotiation on %s\n", (const char*) unicode2char(GetPeer())); |
| 374 | m_NegotiatingState = ONS_BASIC_CLIENTA_RANDOMPART; |
| 375 | m_StreamCryptState = ECS_NEGOTIATING; |
| 376 | m_nReceiveBytesWanted = 4; |
| 377 | } else if (m_StreamCryptState == ECS_PENDING) { |
| 378 | //printf("Socket is client.pending on negotiation\n"); |
| 379 | CMemFile fileRequest(29); |
| 380 | const uint8_t bySemiRandomNotProtocolMarker = GetSemiRandomNotProtocolMarker(); |
| 381 | fileRequest.WriteUInt8(bySemiRandomNotProtocolMarker); |
| 382 | fileRequest.WriteUInt32(m_nRandomKeyPart); |
| 383 | fileRequest.WriteUInt32(MAGICVALUE_SYNC); |
| 384 | const uint8_t bySupportedEncryptionMethod = ENM_OBFUSCATION; // we do not support any further encryption in this version |
| 385 | fileRequest.WriteUInt8(bySupportedEncryptionMethod); |
| 386 | fileRequest.WriteUInt8(bySupportedEncryptionMethod); // so we also prefer this one |
| 387 | uint8_t byPadding = (uint8_t)(GetRandomUint8() % (thePrefs::GetCryptTCPPaddingLength() + 1)); |
| 388 | fileRequest.WriteUInt8(byPadding); |
| 389 | for (int i = 0; i < byPadding; i++) { |
| 390 | fileRequest.WriteUInt8(GetRandomUint8()); |
| 391 | } |
| 392 | |
| 393 | m_NegotiatingState = ONS_BASIC_CLIENTB_MAGICVALUE; |
| 394 | m_StreamCryptState = ECS_NEGOTIATING; |
| 395 | m_nReceiveBytesWanted = 4; |
| 396 | |
| 397 | SendNegotiatingData(fileRequest.GetRawBuffer(), (uint32_t)fileRequest.GetLength(), 5); |
| 398 | } else if (m_StreamCryptState == ECS_PENDING_SERVER) { |
| 399 | //printf("Socket is server.pending on negotiation\n"); |
| 400 | CMemFile fileRequest(113); |
| 401 | const uint8_t bySemiRandomNotProtocolMarker = GetSemiRandomNotProtocolMarker(); |
| 402 | fileRequest.WriteUInt8(bySemiRandomNotProtocolMarker); |
| 403 | |
| 404 | m_cryptDHA.Randomize((CryptoPP::AutoSeededRandomPool&)GetRandomPool(), DHAGREEMENT_A_BITS); // our random a |
| 405 | wxASSERT( m_cryptDHA.MinEncodedSize() <= DHAGREEMENT_A_BITS / 8 ); |
| 406 | CryptoPP::Integer cryptDHPrime((uint8_t*)dh768_p, PRIMESIZE_BYTES); // our fixed prime |
| 407 | // calculate g^a % p |
| 408 | CryptoPP::Integer cryptDHGexpAmodP = a_exp_b_mod_c(CryptoPP::Integer(2), m_cryptDHA, cryptDHPrime); |
| 409 | wxASSERT( m_cryptDHA.MinEncodedSize() <= PRIMESIZE_BYTES ); |
| 410 | // put the result into a buffer |
| 411 | uint8_t aBuffer[PRIMESIZE_BYTES]; |
| 412 | cryptDHGexpAmodP.Encode(aBuffer, PRIMESIZE_BYTES); |
| 413 | |
| 414 | fileRequest.Write(aBuffer, PRIMESIZE_BYTES); |
| 415 | uint8 byPadding = (uint8)(GetRandomUint8() % 16); // add random padding |
| 416 | fileRequest.WriteUInt8(byPadding); |
| 417 | |
| 418 | for (int i = 0; i < byPadding; i++) { |
| 419 | fileRequest.WriteUInt8(GetRandomUint8()); |
| 420 | } |
| 421 | |
| 422 | m_NegotiatingState = ONS_BASIC_SERVER_DHANSWER; |
| 423 | m_StreamCryptState = ECS_NEGOTIATING; |
| 424 | m_nReceiveBytesWanted = 96; |
| 425 | |
| 426 | SendNegotiatingData(fileRequest.GetRawBuffer(), (uint32_t)fileRequest.GetLength(), (uint32_t)fileRequest.GetLength()); |
nothing calls this directly
no test coverage detected