2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 #pragma comment( lib, "ws2_32.lib" )
\r
30 /* Win32 includes. */
\r
31 #include <WinSock2.h>
\r
33 /* wolfSSL includes. */
\r
34 #include "wolfssl/ssl.h"
\r
36 /* Standard includes. */
\r
40 /* FreeRTOS includes. */
\r
41 #include "FreeRTOS.h"
\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
55 /*-----------------------------------------------------------*/
\r
58 * Open, configures and binds the server's TCP socket.
\r
60 static SOCKET prvOpenServerSocket( void );
\r
63 * Prepare the wolfSSL library for use.
\r
65 static void prvInitialiseWolfSSL( void );
\r
68 * The task that implements the client side of the connection.
\r
70 extern void vSecureTCPClientTask( void *pvParameters );
\r
72 /*-----------------------------------------------------------*/
\r
74 /* The wolfSSL context for the server. */
\r
75 static WOLFSSL_CTX* xWolfSSL_ServerContext = NULL;
\r
77 /*-----------------------------------------------------------*/
\r
79 /* See the comments at the top of main.c. */
\r
80 void vSecureTCPServerTask( void *pvParameters )
\r
82 BaseType_t xReturned;
\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
90 /* Just to prevent compiler warnings. */
\r
91 ( void ) pvParameters;
\r
93 /* Perform the initialisation necessary before wolfSSL can be used. */
\r
94 prvInitialiseWolfSSL();
\r
95 configASSERT( xWolfSSL_ServerContext );
\r
97 /* Attempt to open the socket. */
\r
98 xListeningSocket = prvOpenServerSocket();
\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
104 if( xListeningSocket != INVALID_SOCKET )
\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
112 if( xConnectedSocket != INVALID_SOCKET )
\r
114 printf( "Connection established\r\n" );
\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
121 if( xWolfSSL_Object != NULL )
\r
123 /* Associate the created wolfSSL object with the connected
\r
125 xReturned = wolfSSL_set_fd( xWolfSSL_Object, xConnectedSocket );
\r
126 configASSERT( xReturned == SSL_SUCCESS );
\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
135 /* Print the received characters. */
\r
138 printf( "Received by the secure server: %s\r\n", cReceivedString );
\r
141 } while ( lBytes > 0 );
\r
143 /* The connection was closed, close the socket and free the
\r
145 closesocket( xConnectedSocket );
\r
146 wolfSSL_free( xWolfSSL_Object );
\r
147 printf( "Connection closed, back to start\r\n\r\n" );
\r
154 /* The socket could not be opened. */
\r
155 vTaskDelete( NULL );
\r
158 /*-----------------------------------------------------------*/
\r
160 static SOCKET prvOpenServerSocket( void )
\r
163 WORD wVersionRequested;
\r
164 struct sockaddr_in xConnection;
\r
165 SOCKET xSocket = INVALID_SOCKET;
\r
167 wVersionRequested = MAKEWORD( 2, 2 );
\r
169 /* Prepare to use WinSock. */
\r
170 if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )
\r
172 fprintf( stderr, "Could not open Windows connection.\n" );
\r
176 xSocket = socket( AF_INET, SOCK_STREAM, 0 );
\r
177 if( xSocket == INVALID_SOCKET)
\r
179 fprintf( stderr, "Could not create socket.\n" );
\r
184 /* Zero out the server structure. */
\r
185 memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );
\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
191 /* Bind the address to the socket. */
\r
192 if( bind( xSocket, ( struct sockaddr * ) &xConnection, sizeof( struct sockaddr_in ) ) == -1 )
\r
194 fprintf( stderr, "Could not socket to port %d.\n", configTCP_PORT_NUMBER );
\r
195 closesocket( xSocket );
\r
196 xSocket = INVALID_SOCKET;
\r
200 if( listen( xSocket, 20 ) != 0 )
\r
202 closesocket( xSocket );
\r
203 xSocket = INVALID_SOCKET;
\r
211 /*-----------------------------------------------------------*/
\r
213 static void prvInitialiseWolfSSL( void )
\r
217 #ifdef DEBUG_WOLFSSL
\r
219 wolfSSL_Debugging_ON();
\r
223 /* Initialise wolfSSL. This must be done before any other wolfSSL functions
\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
230 if( xWolfSSL_ServerContext != NULL )
\r
232 /* Load the CA certificate. Real applications should ensure that
\r
233 wolfSSL_CTX_load_verify_locations() returns SSL_SUCCESS before
\r
235 iReturn = wolfSSL_CTX_load_verify_locations( xWolfSSL_ServerContext, "ca-cert.pem", 0 );
\r
236 configASSERT( iReturn == SSL_SUCCESS );
\r
238 iReturn = wolfSSL_CTX_use_certificate_file( xWolfSSL_ServerContext, "server-cert.pem", SSL_FILETYPE_PEM );
\r
239 configASSERT( iReturn == SSL_SUCCESS );
\r
241 iReturn = wolfSSL_CTX_use_PrivateKey_file( xWolfSSL_ServerContext, "server-key.pem", SSL_FILETYPE_PEM );
\r
242 configASSERT( iReturn == SSL_SUCCESS );
\r