]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_WolfSSL_Windows_Simulator/SecureTCPClientTask.c
ad8d22cc43bc445f7f0b25fb8399ca92b0c9d0ef
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_WolfSSL_Windows_Simulator / SecureTCPClientTask.c
1 /*\r
2  * FreeRTOS Kernel V10.2.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 /*-----------------------------------------------------------*/\r
45 \r
46 /* The wolfSSL context for the client. */\r
47 static WOLFSSL_CTX* xWolfSSL_ClientContext = NULL;\r
48 \r
49 /*-----------------------------------------------------------*/\r
50 \r
51 /* See the comments at the top of main.c. */\r
52 void vSecureTCPClientTask( void *pvParameters )\r
53 {\r
54 SOCKET xClientSocket;\r
55 struct sockaddr_in xConnection;\r
56 WOLFSSL* xWolfSSL_Object;\r
57 WORD wVersionRequested;\r
58 WSADATA xWSAData;\r
59 char cString[ 50 ];\r
60 BaseType_t lReturned;\r
61 uint32_t ulCount = 0UL;\r
62 \r
63         /* Remove compiler warning about unused parameters. */\r
64         ( void ) pvParameters;\r
65 \r
66         /* Prepare to use WinSock. */\r
67         wVersionRequested = MAKEWORD( 2, 2 );\r
68         configASSERT( WSAStartup( wVersionRequested, &xWSAData ) == 0 );\r
69 \r
70         /* Set family and port for client socket. */\r
71         memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );\r
72         xConnection.sin_family = AF_INET;\r
73         xConnection.sin_addr.s_addr = inet_addr("127.0.0.1");\r
74         xConnection.sin_port = htons( configTCP_PORT_NUMBER );\r
75 \r
76     /* Attempt to create a context that uses the TLS 1.2 server protocol. */\r
77     xWolfSSL_ClientContext = wolfSSL_CTX_new( wolfTLSv1_2_client_method() );\r
78         configASSERT( xWolfSSL_ClientContext );\r
79 \r
80     /* Load the CA certificate. */\r
81     lReturned = wolfSSL_CTX_load_verify_locations( xWolfSSL_ClientContext, "ca-cert.pem", 0 );\r
82         configASSERT( lReturned == SSL_SUCCESS );\r
83 \r
84         for( ;; )\r
85         {\r
86                 /* Create the socket. */\r
87                 xClientSocket = socket( AF_INET, SOCK_STREAM, 0 );\r
88                 configASSERT( xClientSocket != INVALID_SOCKET );\r
89 \r
90                 /* Connect to the secure server. */\r
91                 if( connect( xClientSocket, ( SOCKADDR * ) &xConnection, sizeof( xConnection ) ) == 0 )\r
92                 {\r
93                         /* The connect was successful.  Create a wolfSSL object to associate\r
94                         with this connection. */\r
95                         xWolfSSL_Object = wolfSSL_new( xWolfSSL_ClientContext );\r
96 \r
97                         if( xWolfSSL_Object != NULL )\r
98                         {\r
99                                 /* Associate the created wolfSSL object with the connected\r
100                                 socket. */\r
101                                 lReturned = wolfSSL_set_fd( xWolfSSL_Object, xClientSocket );\r
102                                 configASSERT( lReturned == SSL_SUCCESS );\r
103 \r
104                                 /* The count is used to differentiate between messages sent to\r
105                                 the server, and to break out of the do while loop below. */\r
106                                 ulCount = 0UL;\r
107 \r
108                                 do\r
109                                 {\r
110                                         /* Create the string that is sent to the secure server. */\r
111                                         sprintf( cString, "Message number %lu\r\n", ulCount );\r
112 \r
113                                         /* The next line is the secure equivalent of the standard\r
114                                         sockets call:\r
115                                         lReturned = send( xClientSocket, cString, strlen( cString ) + 1, 0 ); */\r
116                                         lReturned = wolfSSL_write( xWolfSSL_Object, cString, strlen( cString ) + 1 );\r
117 \r
118 \r
119                                         /* Short delay to prevent the messages streaming up the\r
120                                         console too quickly. */\r
121                                         vTaskDelay( 50 );\r
122                                         ulCount++;\r
123 \r
124                                 } while( ( lReturned != SOCKET_ERROR ) && ( ulCount < 10UL ) );\r
125                         }\r
126 \r
127                         wolfSSL_free( xWolfSSL_Object );\r
128                         closesocket( xClientSocket );\r
129 \r
130                         /* Delay for a short time before starting over. */\r
131                         vTaskDelay( 250 );\r
132                 }\r
133         }\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r