title: OpenSSL SSL_read Failure (error:00000005:lib(0):func(0):DH lib)
body: <p>I'm using OpenSSL version 1.1.0f to encrypt the connection between my client and a relaying server for which I don't have any access. Some times the client's connection is getting terminated abruptly with the readLine failure on client side.</p>
  <p>Here is the error code:</p>
  <blockquote>
  <p>readLine: readString returned -1 , Error Msg - error:00000005:lib(0):func(0):DH lib</p>
  </blockquote>
  <p>I did search for the appropriate reasons for this kind of error code, everybody (Example <a href="https://stackoverflow.com/questions/23479376/openssl-ssl-accept-error-5">this</a> thread) says that there seems to be a problem with the Diffie-Hellman key exchange during the connection establishment. But The connection was going fine for few minutes(some times it may not even occur and it may occur very frequently once after 2 to 3 minutes of connection success) until it get terminated abruptly.</p>
  <p>Here are some more errors from the logs that I have</p>
  <blockquote>
  <p>SSL_read failed with error - 5, bytes received -1, error string error:00000005:lib(0):func(0):DH lib, wsaError 0</p>
  <p>SSL Error - 5:error:FFFFFFFF:lib(255):func(4095):reason(4095)</p>
  </blockquote>
  <p>Here is the socket read code from the SSL client</p>
  <pre><code>if ((isSecureMode() == true) &amp;&amp; (lpSSL != NULL))
  {
      bytesReceived = SSL_read(lpSSL, receiveBuf, bufferSize) ;
  
      if ((bytesReceived &lt;= 0))
      {
          int sslErrorCode = lpSSL ? SSL_get_error(lpSSL, bytesReceived) : -1;
  
          char sslErrorString[MAX_ERROR_MSG_LEN] = {'\0'};
  
          ERR_error_string(sslErrorCode, sslErrorString);
          setLastError(sslErrorCode, std::string(sslErrorString));
  
          int wsaError = WSAGetLastError();
  
          if(isShutdownInitiated == false)
          {
              if (Logger)
              {
                  Logger-&gt;log(LOG_WARNING, &quot;receiveString - SSL_read failed with error - %d, bytes received %d, error string %s, wsaError %d&quot;, sslErrorCode, bytesReceived, std::string(sslErrorString), wsaError);
              }
                                                      // Always return -1 incase of failure
              bytesReceived = -1;
          } 
          else
          {
              if (Logger)
              {
                  Logger-&gt;log(LOG_WARNING, &quot;receiveString - Socket was Shutdown, SSL_read failed with error - %d, bytes received %d, error string %s , wsaError %d&quot;, sslErrorCode, bytesReceived, std::string(sslErrorString), wsaError);
              }
                                      
              setLastError(ERROR_ALREADY_SHUTDOWN);
              bytesReceived = -1;
          }
      }
  }
  </code></pre>
  <p>I'm not an expert in OpenSSL and I doubted that the problem may be with the server but there are no error messages that can prove it except for the above ones.</p>
  <p>I did analyze a lot and spent weeks without any improvement.</p>
  <p><strong>Update :</strong>
  Upon analyzing the error messages the error code SSL_ERROR_SYSCALL along with the wsa error_code 10054 says that the connection has been terminated by the remote party. The same error message is being written on server logs. That leads to the doubt for the connection being reset by the intermediate devices (may be routers that reside in between the client and the server) as per <a href="https://curl.haxx.se/mail/lib-2003-01/0115.html" rel="nofollow noreferrer">this</a> thread. I'm trying to analyze further and sort out the problem.</p>
  <p><strong>Update 2:</strong> I also found out that the problem happens some times that is only on some networks. It works perfectly fine on few networks.The above mentioned thread also tells the same.</p>
  <p><strong>Update 3:</strong> I had figured out one of the reasons for connection termination, The error 10054 was actually due to firewall and the network that has been resolved now.</p>
  <p>Debugging the readline failure with error</p>
  <blockquote>
  <p>SSL_read failed with error - 5, bytes received -1, error string
  error:00000005:lib(0):func(0):DH lib, wsaError 0</p>
  </blockquote>
  <p>Any help would be appreciated.</p>