]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_WolfSSL_Windows_Simulator/SecureTCPClientTask.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_WolfSSL_Windows_Simulator / SecureTCPClientTask.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 #pragma comment( lib, "ws2_32.lib" )\r
30 \r
31 /* Win32 includes. */\r
32 #include <WinSock2.h>\r
33 \r
34 /* wolfSSL includes. */\r
35 #include "wolfssl/ssl.h"\r
36 \r
37 /* Standard includes. */\r
38 #include <stdint.h>\r
39 #include <stdio.h>\r
40 \r
41 /* FreeRTOS includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 \r
45 /*-----------------------------------------------------------*/\r
46 \r
47 /* The wolfSSL context for the client. */\r
48 static WOLFSSL_CTX* xWolfSSL_ClientContext = NULL;\r
49 \r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /* See the comments at the top of main.c. */\r
53 void vSecureTCPClientTask( void *pvParameters )\r
54 {\r
55 SOCKET xClientSocket;\r
56 struct sockaddr_in xConnection;\r
57 WOLFSSL* xWolfSSL_Object;\r
58 WORD wVersionRequested;\r
59 WSADATA xWSAData;\r
60 char cString[ 50 ];\r
61 BaseType_t lReturned;\r
62 uint32_t ulCount = 0UL;\r
63 \r
64         /* Remove compiler warning about unused parameters. */\r
65         ( void ) pvParameters;\r
66 \r
67         /* Prepare to use WinSock. */\r
68         wVersionRequested = MAKEWORD( 2, 2 );\r
69         configASSERT( WSAStartup( wVersionRequested, &xWSAData ) == 0 );\r
70 \r
71         /* Set family and port for client socket. */\r
72         memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );\r
73         xConnection.sin_family = AF_INET;\r
74         xConnection.sin_addr.s_addr = inet_addr("127.0.0.1");\r
75         xConnection.sin_port = htons( configTCP_PORT_NUMBER );\r
76 \r
77     /* Attempt to create a context that uses the TLS 1.2 server protocol. */\r
78     xWolfSSL_ClientContext = wolfSSL_CTX_new( wolfTLSv1_2_client_method() );\r
79         configASSERT( xWolfSSL_ClientContext );\r
80 \r
81     /* Load the CA certificate. */\r
82     lReturned = wolfSSL_CTX_load_verify_locations( xWolfSSL_ClientContext, "ca-cert.pem", 0 );\r
83         configASSERT( lReturned == SSL_SUCCESS );\r
84 \r
85         for( ;; )\r
86         {\r
87                 /* Create the socket. */\r
88                 xClientSocket = socket( AF_INET, SOCK_STREAM, 0 );\r
89                 configASSERT( xClientSocket != INVALID_SOCKET );\r
90 \r
91                 /* Connect to the secure server. */\r
92                 if( connect( xClientSocket, ( SOCKADDR * ) &xConnection, sizeof( xConnection ) ) == 0 )\r
93                 {\r
94                         /* The connect was successful.  Create a wolfSSL object to associate\r
95                         with this connection. */\r
96                         xWolfSSL_Object = wolfSSL_new( xWolfSSL_ClientContext );\r
97 \r
98                         if( xWolfSSL_Object != NULL )\r
99                         {\r
100                                 /* Associate the created wolfSSL object with the connected\r
101                                 socket. */\r
102                                 lReturned = wolfSSL_set_fd( xWolfSSL_Object, xClientSocket );\r
103                                 configASSERT( lReturned == SSL_SUCCESS );\r
104 \r
105                                 /* The count is used to differentiate between messages sent to\r
106                                 the server, and to break out of the do while loop below. */\r
107                                 ulCount = 0UL;\r
108 \r
109                                 do\r
110                                 {\r
111                                         /* Create the string that is sent to the secure server. */\r
112                                         sprintf( cString, "Message number %lu\r\n", ulCount );\r
113 \r
114                                         /* The next line is the secure equivalent of the standard\r
115                                         sockets call:\r
116                                         lReturned = send( xClientSocket, cString, strlen( cString ) + 1, 0 ); */\r
117                                         lReturned = wolfSSL_write( xWolfSSL_Object, cString, strlen( cString ) + 1 );\r
118 \r
119 \r
120                                         /* Short delay to prevent the messages streaming up the\r
121                                         console too quickly. */\r
122                                         vTaskDelay( 50 );\r
123                                         ulCount++;\r
124 \r
125                                 } while( ( lReturned != SOCKET_ERROR ) && ( ulCount < 10UL ) );\r
126                         }\r
127 \r
128                         wolfSSL_free( xWolfSSL_Object );\r
129                         closesocket( xClientSocket );\r
130 \r
131                         /* Delay for a short time before starting over. */\r
132                         vTaskDelay( 250 );\r
133                 }\r
134         }\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r