savoirfairelinux / opendht

OpenDHT: a C++17 Distributed Hash Table implementation
GNU General Public License v3.0
1.02k stars 172 forks source link

Microsoft C++ exception: dht::crypto::DecryptError (Successful reported) #465

Open Mazecreator opened 4 years ago

Mazecreator commented 4 years ago

I am getting this exception reported to VisualStudio as I run my OpenDHT application. I have been chasing this for a long time now and don't know where to go. It also seems intermittent as it will work from time to time, but fail most of the time. The "putEncrypt" always reports success in the callback....

Exception thrown at 0x00007FF9A9ACA839 in WinNeurSecure.exe: Microsoft C++ exception: dht::crypto::DecryptError at memory location 0x00000010C9CFE348.

I initially thought this was a race conditions, so I setup a 10 second delay between the DhtRunner "RUN" and trying to encrypt a message. Signing works fine in the sequence.

I checked, the certificate is live on the nodes with "g infohash". Not sure what to try to get this rock solid. I am currently using a private node and run upwards to 5 nodes to see if there was a bottle neck but no real problem. My test applications has 2 users initialize a connection to the NET, each send a "Signed" message then one sends an Encrypted as the other might send 2 Encrypted.

binarytrails commented 4 years ago

@Mazecreator dht::crypto::DecryptError is an explicit throw from OpenDHT located in crypto.cpp, I would tend to be inclined to say that you must simply catch it ! Perhaps, some operation is not permitted to invalid data, members or something else that is not expected internally and is explicitly checked for. :)

Mazecreator commented 4 years ago

Hi @binarytrails ,

I don't seem to be able to catch the exception... I did the following around the Put ( it is the putEncrypt this is silently failing):

Try {
        if (sendTo == "Signed")
        {
            dht.putSigned(c_infoHash, package, [this, id, sendTo](bool ok) {
                    if (not ok)
                    {
                        std::cout << id<<" putSigned Message publishing failed !" << std::endl;
                    }
                    else
                    {
                        std::cout << id << " putSigned Message published!" << std::endl;
                    }
                });
        }
        else if (sendTo.length() > 0)
        {
            dht.putEncrypted(c_infoHash, InfoHash(sendTo), package, [this,sendTo, id](bool ok) {
                    if (not ok)
                    {
                        std::cout << "putEncrypted(to:" << sendTo << ") Message publishing failed !" << std::endl;
                    }
                    else
                    {
                        std::cout << "putEncrypted(to:" << sendTo << ") Message published!" << std::endl;
                    }
                }
            );
        }

        ShowEvent(std::string(std::to_string(id) + " putMsg Returning!  SendTo= "), sendTo + " : " + this->c_infoHash.toString() + "   " + this->c_infoHashKey);
        }
        catch (const dht::crypto::DecryptError & e)
        {
            FILE_LOG(lerror) << "DecryptError: " << e.what();
            ShowEvent(std::string("DecryptError: "), e.what());
        }
        catch (const std::exception & e)
        {
            FILE_LOG(lerror) << "ERROR EXCEPTION: " << e.what();
        }
        catch (...)
        {
            FILE_LOG(lerror) << "ERROR EXCEPTION: Unknown";
        }
Mazecreator commented 4 years ago

I have done some additional testing:

I removed the putEncrypt() and with with putSigned() to see if that was the root issue or not. Also, I managed to get logging setup for OpenDHT. What I found is the same intermittent function within the system but I see this error within Visual Studio:

Exception thrown at 0x00007FF87C6BA839 in WinNeurSecure.exe: Microsoft C++ exception: dht::net::DhtProtocolException at memory location 0x0000002DCA5FEB10.

I also see in the log file this error reported:

Partial message with given TID already exists

This seems to be logged from network_engine.cpp: "void NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, SockAddr f)".

Thanks, Greg

EDIT: a few other odd errors listed in the log files (not in the same run but still failed): [071705.538341] Can't process message: DhtException occurred: Can't find transaction [071705.538364] Can't process message: DhtException occurred: Can't find transaction [071705.538425] Error receiving packet: No error [071705.538625] Error receiving packet: No error [071705.538655] Error receiving packet: No error

Mazecreator commented 4 years ago

I managed to get OpenDHT so I can have the debugger trap this exception. This is where the fault occurs, also, the "key" value does not appear to be set. I am using the thread-safe "DhtRunner" so I assume it isn't a threading problem.

image

Also, the error code returned is -24 if that helps.

binarytrails commented 4 years ago

@Mazecreator, I would check your private key by parsing it into a file to make sure it is valid once this function is called. Here is some additional info I call pull for you:

flags: zero for now

ciphertext: holds the data to be decrypted

plaintext: will contain the decrypted data, allocated with gnutls_malloc()

This function will decrypt the given data using the algorithm supported by the private key.

Returns: On success, GNUTLS_E_SUCCESS (0) is returned, otherwise a negative error value.

Since: 2.12.0

Mazecreator commented 4 years ago

@binarytrails ,

I am working on trying to capture the encoded data to this point. This is a Callback which I have no control over. The Data is successfully encoded and placed on the DHT, but the receiving node for some reason throws this error, I really have no control as it pulls the public cert from the DHT and tries to decode it for that receiving Node.

What I will try to do is get better access to the OpenDHT code and capture the encoded data on the originating node and then the data passed to the decode routine on the callback from the receiving node. The Cert did seem okay on the Receiving node so don't think that was the problem. I am wondering if it is intermittently getting partial records so I can't properly decrypt since the entire message is not being processed.

ghenry commented 2 years ago

Still a requirement?

Thanks.