Browse code

c-rest-engine :fixing tls-unexpected message ERROR on SSL_shutdown

Change-Id: I76e1985222227061fe61dc5ec1c03ae272594696
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/4484
Reviewed-by: Kumar Kaushik <kaushikk@vmware.com>
Tested-by: Kumar Kaushik <kaushikk@vmware.com>

Kumar Kaushik authored on 2017/12/08 08:24:50
Showing 2 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 Name:          c-rest-engine
2 2
 Summary:       minimal http(s) server library
3 3
 Version:       1.1
4
-Release:       5%{?dist}
4
+Release:       6%{?dist}
5 5
 Group:         Applications/System
6 6
 Vendor:        VMware, Inc.
7 7
 License:       Apache 2.0
... ...
@@ -16,6 +16,7 @@ Patch0:        socket_RW.patch
16 16
 Patch1:        syslog_noInit.patch
17 17
 Patch2:        socket_logging.patch
18 18
 Patch3:        errno_init.patch
19
+Patch4:        ssl_shutdown.patch
19 20
 %define sha1   c-rest-engine=a25927fd98ec92df5e210cc4941fa626604636f6
20 21
 
21 22
 %description
... ...
@@ -38,6 +39,7 @@ development libs and header files for c-rest-engine
38 38
 %patch1 -p1
39 39
 %patch2 -p1
40 40
 %patch3 -p1
41
+%patch4 -p1
41 42
 
42 43
 %build
43 44
 cd build
... ...
@@ -70,6 +72,8 @@ find %{buildroot} -name '*.la' -delete
70 70
 # %doc ChangeLog README COPYING
71 71
 
72 72
 %changelog
73
+*  Wed Nov 29 2017 Kumar Kaushik <kaushikk@vmware.com> 1.1-6
74
+-  Adding patch for ssl_shutdown order.
73 75
 *  Wed Nov 29 2017 Kumar Kaushik <kaushikk@vmware.com> 1.1-5
74 76
 -  Adding patch for right use of errno.
75 77
 *  Mon Nov 20 2017 Kumar Kaushik <kaushikk@vmware.com> 1.1-4
76 78
new file mode 100644
... ...
@@ -0,0 +1,123 @@
0
+From a7aba08f7fd2700c92fd9661eee212755ba8795e Mon Sep 17 00:00:00 2001
1
+From: Kumar Kaushik <kaushikk@vmware.com>
2
+Date: Thu, 7 Dec 2017 12:53:17 -0800
3
+Subject: [PATCH] Fixing tls: unexpected message ERROR
4
+
5
+Change-Id: I63dce89d4dcfa445b6594d4b776ef8827f1498ff
6
+(cherry picked from commit 424f6d0f3435eccad781d39ef0c656a00395dc46)
7
+---
8
+ transport/posix/socket.c | 34 +++++++++++++++++++++++-----------
9
+ 1 file changed, 23 insertions(+), 11 deletions(-)
10
+
11
+diff --git a/transport/posix/socket.c b/transport/posix/socket.c
12
+index d3a3dbc..18cef89 100644
13
+--- a/transport/posix/socket.c
14
+@@ -576,7 +576,6 @@ VmSockPosixWaitForEvent(
15
+                                    FALSE
16
+                                    );
17
+                     BAIL_ON_VMREST_ERROR(dwError);
18
+-                
19
+                 }
20
+ 
21
+                 dwError = VmSockPosixEventQueueAdd_inlock(
22
+@@ -895,7 +894,7 @@ VmSockPosixRead(
23
+         }
24
+         else
25
+         {
26
+-            VMREST_LOG_ERROR(pRESTHandle,"%s","Unknown socket read error: errno %u, errorCode %u, nRead %d", errno, errorCode, nRead);
27
++            VMREST_LOG_ERROR(pRESTHandle,"Unknown socket read error: errno %u, errorCode %u, nRead %d", errno, errorCode, nRead);
28
+             dwError = REST_ENGINE_FAILURE;
29
+         }
30
+     }
31
+@@ -1007,6 +1006,9 @@ VmSockPosixWrite(
32
+ 
33
+     while(nWrittenTotal < nBufLen )
34
+     {
35
++         nWritten = 0;
36
++         errorCode = 0;
37
++         errno = 0;
38
+          if (pRESTHandle->pSSLInfo->isSecure && (pSocket->ssl != NULL))
39
+          {
40
+              nWritten = SSL_write(pSocket->ssl,(pszBuffer + nWrittenTotal),nRemaining);
41
+@@ -1030,7 +1032,7 @@ VmSockPosixWrite(
42
+          }
43
+          else
44
+          {
45
+-             if (errorCode == EAGAIN || errorCode == EWOULDBLOCK || errorCode == SSL_ERROR_WANT_WRITE)
46
++             if ((nWritten < 0) && (errorCode == EAGAIN || errorCode == EWOULDBLOCK || errorCode == SSL_ERROR_WANT_WRITE))
47
+              {
48
+                  if (timeOutSec >= 0)
49
+                  {
50
+@@ -1101,6 +1103,8 @@ VmSockPosixCloseSocket(
51
+     )
52
+ {
53
+     DWORD                            dwError = REST_ENGINE_SUCCESS;
54
++    int                              ret = 0;
55
++    uint32_t                         errorCode = 0;
56
+     BOOLEAN                          bLocked = FALSE;
57
+ 
58
+     if (!pRESTHandle || !pSocket )
59
+@@ -1121,18 +1125,24 @@ VmSockPosixCloseSocket(
60
+         pSocket->pTimerSocket->fd = -1;
61
+     }
62
+ 
63
++    if (pRESTHandle->pSSLInfo->isSecure && pSocket->ssl)
64
++    {
65
++        ret = SSL_shutdown(pSocket->ssl);
66
++        if (ret < 0)
67
++        {
68
++            errorCode = SSL_get_error(pSocket->ssl, ret);
69
++            VMREST_LOG_ERROR(pRESTHandle,"Error on SSL_shutdown on socket %d, return value %d, errorCode %u", pSocket->fd, ret, errorCode);
70
++        }
71
++        SSL_free(pSocket->ssl);
72
++        pSocket->ssl = NULL;
73
++    }
74
++
75
+     if (pSocket->fd >= 0)
76
+     {
77
+         close(pSocket->fd);
78
+         pSocket->fd = -1;
79
+     }
80
+ 
81
+-    if (pRESTHandle->pSSLInfo->isSecure && pSocket->ssl)
82
+-    {
83
+-        SSL_shutdown(pSocket->ssl);
84
+-        SSL_free(pSocket->ssl);
85
+-        pSocket->ssl = NULL;
86
+-    }
87
+ 
88
+ cleanup:
89
+ 
90
+@@ -1742,6 +1752,7 @@ VmSockPosixIsSafeToCloseConnOnTimeOut(
91
+     BAIL_ON_VMREST_ERROR(dwError);
92
+ 
93
+     pSocket = pTimerSocket->pIoSocket;
94
++    errno = 0;
95
+ 
96
+     if ((pRESTHandle->pSSLInfo->isSecure) && (pSocket->ssl))
97
+     {
98
+@@ -1771,6 +1782,7 @@ VmSockPosixIsSafeToCloseConnOnTimeOut(
99
+             do
100
+             {
101
+                 errorCode = 0;
102
++                errno = 0;
103
+                 nRead = 0;
104
+                 nRead = read(pTimerSocket->fd, &res, sizeof(res));
105
+                 errorCode = errno;
106
+@@ -1918,13 +1930,13 @@ VmRESTAcceptSSLContext(
107
+     }
108
+     else if ((ret == -1) && ((errorCode == SSL_ERROR_WANT_READ) || (errorCode == SSL_ERROR_WANT_WRITE)))
109
+     {
110
+-       VMREST_LOG_DEBUG(pRESTHandle,"SSL handshake not completed for socket %d", pSocket->fd);
111
++       VMREST_LOG_DEBUG(pRESTHandle," SSL handshake not completed for socket %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
112
+        pSocket->bSSLHandShakeCompleted = FALSE;
113
+        bReArm = TRUE;
114
+     }
115
+     else
116
+     {
117
+-        VMREST_LOG_ERROR(pRESTHandle, "SSL handshake failed...connection will be closed for socket with fd %d", pSocket->fd);
118
++        VMREST_LOG_ERROR(pRESTHandle, "SSL handshake failed...connection will be closed for socket with fd %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
119
+         dwError = VMREST_TRANSPORT_SSL_ACCEPT_FAILED;
120
+         BAIL_ON_VMREST_ERROR(dwError);
121
+     }