]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_WolfSSL_Windows_Simulator/SecureTCPServerTask.c
14e244b49ff407f6d75bff1fdc10b4ca8f1d15af
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_WolfSSL_Windows_Simulator / SecureTCPServerTask.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 #pragma comment( lib, "ws2_32.lib" )\r
29 \r
30 /* Win32 includes. */\r
31 #include <WinSock2.h>\r
32 \r
33 /* wolfSSL includes. */\r
34 #include "wolfssl/ssl.h"\r
35 \r
36 /* Standard includes. */\r
37 #include <stdint.h>\r
38 #include <stdio.h>\r
39 \r
40 /* FreeRTOS includes. */\r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 \r
44 /* This application is using the FreeRTOS Windows simulator, which uses the\r
45 FreeRTOS scheduler to schedule FreeRTOS task within the Windows environment.\r
46 The Windows envrionment must not be allowed to block any Windows threads that\r
47 are running FreeRTOS tasks, unless the FreeRTOS task is running at the FreeRTOS\r
48 idle priority.  For simplicity, this demo uses the Windows TCP/IP stack, the\r
49 API for which can cause Windows threads to block.  Therefore, any FreeRTOS task\r
50 that makes calls to the Windows TCP/IP stack must be assigned the idle prioity.\r
51 Note this is only a restriction of the simulated Windows environment - real\r
52 FreeRTOS ports do not have this restriction. */\r
53 #define sstSECURE_CLIENT_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /*\r
58  * Open, configures and binds the server's TCP socket.\r
59  */\r
60 static SOCKET prvOpenServerSocket( void );\r
61 \r
62 /*\r
63  * Prepare the wolfSSL library for use.\r
64  */\r
65 static void prvInitialiseWolfSSL( void );\r
66 \r
67 /*\r
68  * The task that implements the client side of the connection.\r
69  */\r
70 extern void vSecureTCPClientTask( void *pvParameters );\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* The wolfSSL context for the server. */\r
75 static WOLFSSL_CTX* xWolfSSL_ServerContext = NULL;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* See the comments at the top of main.c. */\r
80 void vSecureTCPServerTask( void *pvParameters )\r
81 {\r
82 BaseType_t xReturned;\r
83 long lBytes;\r
84 uint8_t cReceivedString[ 60 ];\r
85 struct sockaddr_in xClient;\r
86 int xClientAddressLength = sizeof( struct sockaddr_in );\r
87 SOCKET xListeningSocket, xConnectedSocket;\r
88 WOLFSSL* xWolfSSL_Object; /* Only one connection is accepted at a time, so only one object is needed at a time. */\r
89 \r
90         /* Just to prevent compiler warnings. */\r
91         ( void ) pvParameters;\r
92 \r
93         /* Perform the initialisation necessary before wolfSSL can be used. */\r
94         prvInitialiseWolfSSL();\r
95         configASSERT( xWolfSSL_ServerContext );\r
96 \r
97         /* Attempt to open the socket. */\r
98         xListeningSocket = prvOpenServerSocket();\r
99 \r
100         /* Now the server socket has been created and the wolfSSL library has been\r
101         initialised, the task that implements the client side can be created. */\r
102         xTaskCreate( vSecureTCPClientTask, "Client", configMINIMAL_STACK_SIZE, NULL, sstSECURE_CLIENT_TASK_PRIORITY, NULL );\r
103 \r
104         if( xListeningSocket != INVALID_SOCKET )\r
105         {\r
106                 for( ;; )\r
107                 {\r
108                         /* Wait until the client connects. */\r
109                         printf( "Waiting for new connection\r\n" );\r
110                         xConnectedSocket = accept( xListeningSocket, ( struct sockaddr * ) &xClient, &xClientAddressLength );\r
111 \r
112                         if( xConnectedSocket != INVALID_SOCKET )\r
113                         {\r
114                                 printf( "Connection established\r\n" );\r
115 \r
116                                 /* A connection has been accepted by the server.  Create a\r
117                                 wolfSSL object for use with the newly connected socket. */\r
118                                 xWolfSSL_Object = NULL;\r
119                                 xWolfSSL_Object = wolfSSL_new( xWolfSSL_ServerContext );\r
120 \r
121                                 if( xWolfSSL_Object != NULL )\r
122                                 {\r
123                                         /* Associate the created wolfSSL object with the connected\r
124                                         socket. */\r
125                                         xReturned = wolfSSL_set_fd( xWolfSSL_Object, xConnectedSocket );\r
126                                         configASSERT( xReturned == SSL_SUCCESS );\r
127 \r
128                                         do\r
129                                         {\r
130                                                 /* The next line is the secure equivalent to the\r
131                                                 standard sockets call:\r
132                                                 lBytes = recv( xConnectedSocket, cReceivedString, 50, 0 ); */\r
133                                                 lBytes = wolfSSL_read( xWolfSSL_Object, cReceivedString, sizeof( cReceivedString ) );\r
134 \r
135                                                 /* Print the received characters. */\r
136                                                 if( lBytes > 0 )\r
137                                                 {\r
138                                                         printf( "Received by the secure server: %s\r\n", cReceivedString );\r
139                                                 }\r
140 \r
141                                         } while ( lBytes > 0 );\r
142 \r
143                                         /* The connection was closed, close the socket and free the\r
144                                         wolfSSL object. */\r
145                                         closesocket( xConnectedSocket );\r
146                                         wolfSSL_free( xWolfSSL_Object );\r
147                                         printf( "Connection closed, back to start\r\n\r\n" );\r
148                                 }\r
149                         }\r
150                 }\r
151         }\r
152         else\r
153         {\r
154                 /* The socket could not be opened. */\r
155                 vTaskDelete( NULL );\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static SOCKET prvOpenServerSocket( void )\r
161 {\r
162 WSADATA xWSAData;\r
163 WORD wVersionRequested;\r
164 struct sockaddr_in xConnection;\r
165 SOCKET xSocket = INVALID_SOCKET;\r
166 \r
167         wVersionRequested = MAKEWORD( 2, 2 );\r
168 \r
169         /* Prepare to use WinSock. */\r
170         if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )\r
171         {\r
172                 fprintf( stderr, "Could not open Windows connection.\n" );\r
173         }\r
174         else\r
175         {\r
176                 xSocket = socket( AF_INET, SOCK_STREAM, 0 );\r
177                 if( xSocket == INVALID_SOCKET)\r
178                 {\r
179                         fprintf( stderr, "Could not create socket.\n" );\r
180                         WSACleanup();\r
181                 }\r
182                 else\r
183                 {\r
184                         /* Zero out the server structure. */\r
185                         memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );\r
186 \r
187                         xConnection.sin_family = AF_INET;\r
188                         xConnection.sin_addr.s_addr = inet_addr("127.0.0.1");\r
189                         xConnection.sin_port = htons( configTCP_PORT_NUMBER );\r
190 \r
191                         /* Bind the address to the socket. */\r
192                         if( bind( xSocket, ( struct sockaddr * ) &xConnection, sizeof( struct sockaddr_in ) ) == -1 )\r
193                         {\r
194                                 fprintf( stderr, "Could not socket to port %d.\n", configTCP_PORT_NUMBER );\r
195                                 closesocket( xSocket );\r
196                                 xSocket = INVALID_SOCKET;\r
197                                 WSACleanup();\r
198                         }\r
199 \r
200                         if( listen( xSocket, 20 ) != 0 )\r
201                         {\r
202                                 closesocket( xSocket );\r
203                                 xSocket = INVALID_SOCKET;\r
204                                 WSACleanup();\r
205                         }\r
206                 }\r
207         }\r
208 \r
209         return xSocket;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void prvInitialiseWolfSSL( void )\r
214 {\r
215 int32_t iReturn;\r
216 \r
217         #ifdef DEBUG_WOLFSSL\r
218         {\r
219                 wolfSSL_Debugging_ON();\r
220         }\r
221         #endif\r
222 \r
223     /* Initialise wolfSSL.  This must be done before any other wolfSSL functions\r
224     are called. */\r
225     wolfSSL_Init();\r
226 \r
227     /* Attempt to create a context that uses the TLS 1.2 server protocol. */\r
228     xWolfSSL_ServerContext = wolfSSL_CTX_new( wolfTLSv1_2_server_method() );\r
229 \r
230     if( xWolfSSL_ServerContext != NULL )\r
231     {\r
232         /* Load the CA certificate.  Real applications should ensure that\r
233         wolfSSL_CTX_load_verify_locations() returns SSL_SUCCESS before\r
234                 proceeding. */\r
235         iReturn = wolfSSL_CTX_load_verify_locations( xWolfSSL_ServerContext, "ca-cert.pem", 0 );\r
236                 configASSERT( iReturn == SSL_SUCCESS );\r
237 \r
238                 iReturn = wolfSSL_CTX_use_certificate_file( xWolfSSL_ServerContext, "server-cert.pem", SSL_FILETYPE_PEM );\r
239                 configASSERT( iReturn == SSL_SUCCESS );\r
240 \r
241                 iReturn = wolfSSL_CTX_use_PrivateKey_file( xWolfSSL_ServerContext, "server-key.pem", SSL_FILETYPE_PEM );\r
242                 configASSERT( iReturn == SSL_SUCCESS );\r
243     }\r
244 }\r
245 \r