| 121 | {} |
| 122 | |
| 123 | int CEncryptedDatagramSocket::DecryptReceivedClient(uint8_t *bufIn, int bufLen, uint8_t **bufOut, uint32_t ip, uint32_t *receiverVerifyKey, uint32_t *senderVerifyKey) |
| 124 | { |
| 125 | int result = bufLen; |
| 126 | *bufOut = bufIn; |
| 127 | |
| 128 | if (receiverVerifyKey == NULL || senderVerifyKey == NULL) { |
| 129 | wxFAIL; |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | *receiverVerifyKey = 0; |
| 134 | *senderVerifyKey = 0; |
| 135 | |
| 136 | if (result <= CRYPT_HEADER_WITHOUTPADDING /*|| !thePrefs.IsClientCryptLayerSupported()*/) { |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | switch (bufIn[0]) { |
| 141 | case OP_EMULEPROT: |
| 142 | case OP_KADEMLIAPACKEDPROT: |
| 143 | case OP_KADEMLIAHEADER: |
| 144 | case OP_UDPRESERVEDPROT1: |
| 145 | case OP_UDPRESERVEDPROT2: |
| 146 | case OP_PACKEDPROT: |
| 147 | return result; // no encrypted packet (see description on top) |
| 148 | default: |
| 149 | ; |
| 150 | } |
| 151 | |
| 152 | // might be an encrypted packet, try to decrypt |
| 153 | CRC4EncryptableBuffer receivebuffer; |
| 154 | uint32_t value = 0; |
| 155 | // check the marker bit which type this packet could be and which key to test first, this is only an indicator since old clients have it set random |
| 156 | // see the header for marker bits explanation |
| 157 | uint8_t currentTry = ((bufIn[0] & 0x03) == 3) ? 1 : (bufIn[0] & 0x03); |
| 158 | uint8_t tries; |
| 159 | if (Kademlia::CKademlia::GetPrefs() == NULL) { |
| 160 | // if kad never run, no point in checking anything except for ed2k encryption |
| 161 | tries = 1; |
| 162 | currentTry = 1; |
| 163 | } else { |
| 164 | tries = 3; |
| 165 | } |
| 166 | bool kad = false; |
| 167 | do { |
| 168 | receivebuffer.FullReset(); |
| 169 | tries--; |
| 170 | MD5Sum md5; |
| 171 | |
| 172 | if (currentTry == 0) { |
| 173 | // kad packet with NodeID as key |
| 174 | kad = true; |
| 175 | if (Kademlia::CKademlia::GetPrefs()) { |
| 176 | uint8_t keyData[18]; |
| 177 | Kademlia::CKademlia::GetPrefs()->GetKadID().StoreCryptValue((uint8_t *)&keyData); |
| 178 | memcpy(keyData + 16, bufIn + 1, 2); // random key part sent from remote client |
| 179 | md5.Calculate(keyData, sizeof(keyData)); |
| 180 | } |
nothing calls this directly
no test coverage detected