| 375 | } |
| 376 | |
| 377 | int CEncryptedDatagramSocket::DecryptReceivedServer(uint8_t* pbyBufIn, int nBufLen, uint8_t **ppbyBufOut, uint32_t dwBaseKey, uint32_t /*dbgIP*/) |
| 378 | { |
| 379 | int nResult = nBufLen; |
| 380 | *ppbyBufOut = pbyBufIn; |
| 381 | |
| 382 | if (nResult <= CRYPT_HEADER_WITHOUTPADDING || !thePrefs::IsServerCryptLayerUDPEnabled() || dwBaseKey == 0) { |
| 383 | return nResult; |
| 384 | } |
| 385 | |
| 386 | if(pbyBufIn[0] == OP_EDONKEYPROT) { |
| 387 | return nResult; // no encrypted packet (see description on top) |
| 388 | } |
| 389 | |
| 390 | // might be an encrypted packet, try to decrypt |
| 391 | uint8_t achKeyData[7]; |
| 392 | PokeUInt32(achKeyData, dwBaseKey); |
| 393 | achKeyData[4] = MAGICVALUE_UDP_SERVERCLIENT; |
| 394 | memcpy(achKeyData + 5, pbyBufIn + 1, 2); // random key part sent from remote server |
| 395 | |
| 396 | CRC4EncryptableBuffer receivebuffer; |
| 397 | MD5Sum md5(achKeyData, sizeof(achKeyData)); |
| 398 | receivebuffer.SetKey(md5,true); |
| 399 | |
| 400 | uint32_t dwValue; |
| 401 | receivebuffer.RC4Crypt(pbyBufIn + 3, (uint8_t*)&dwValue, sizeof(dwValue)); |
| 402 | ENDIAN_SWAP_I_32(dwValue); |
| 403 | if (dwValue == MAGICVALUE_UDP_SYNC_SERVER) { |
| 404 | // yup this is an encrypted packet |
| 405 | //DEBUG_ONLY( DebugLog(_T("Received obfuscated UDP packet from ServerIP: %s"), ipstr(dbgIP)) ); |
| 406 | uint8_t byPadLen; |
| 407 | receivebuffer.RC4Crypt(pbyBufIn + 7, (uint8_t*)&byPadLen, 1); |
| 408 | byPadLen &= 15; |
| 409 | nResult -= CRYPT_HEADER_WITHOUTPADDING; |
| 410 | |
| 411 | if (nResult <= byPadLen) { |
| 412 | //DebugLogError(_T("Invalid obfuscated UDP packet from ServerIP: %s, Paddingsize (%u) larger than received bytes"), ipstr(dbgIP), byPadLen); |
| 413 | return nBufLen; // pass through, let the Receivefunction do the errorhandling on this junk |
| 414 | } |
| 415 | |
| 416 | if (byPadLen > 0) { |
| 417 | receivebuffer.RC4Crypt(NULL, NULL, byPadLen); |
| 418 | } |
| 419 | |
| 420 | nResult -= byPadLen; |
| 421 | *ppbyBufOut = pbyBufIn + (nBufLen - nResult); |
| 422 | receivebuffer.RC4Crypt((uint8_t*)*ppbyBufOut, (uint8_t*)*ppbyBufOut, nResult); |
| 423 | |
| 424 | theStats::AddDownOverheadCrypt(nBufLen - nResult); |
| 425 | return nResult; // done |
| 426 | } else { |
| 427 | //DebugLogWarning(_T("Obfuscated packet expected but magicvalue mismatch on UDP packet from ServerIP: %s"), ipstr(dbgIP)); |
| 428 | return nBufLen; // pass through, let the Receivefunction do the errorhandling on this junk |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | int CEncryptedDatagramSocket::EncryptSendServer(uint8_t** ppbyBuf, int nBufLen, uint32_t dwBaseKey) |
| 433 | { |
nothing calls this directly
no test coverage detected