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 /*-----------------------------------------------------------*/
\r
46 /* The wolfSSL context for the client. */
\r
47 static WOLFSSL_CTX* xWolfSSL_ClientContext = NULL;
\r
49 /*-----------------------------------------------------------*/
\r
51 /* See the comments at the top of main.c. */
\r
52 void vSecureTCPClientTask( void *pvParameters )
\r
54 SOCKET xClientSocket;
\r
55 struct sockaddr_in xConnection;
\r
56 WOLFSSL* xWolfSSL_Object;
\r
57 WORD wVersionRequested;
\r
60 BaseType_t lReturned;
\r
61 uint32_t ulCount = 0UL;
\r
63 /* Remove compiler warning about unused parameters. */
\r
64 ( void ) pvParameters;
\r
66 /* Prepare to use WinSock. */
\r
67 wVersionRequested = MAKEWORD( 2, 2 );
\r
68 configASSERT( WSAStartup( wVersionRequested, &xWSAData ) == 0 );
\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
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
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
86 /* Create the socket. */
\r
87 xClientSocket = socket( AF_INET, SOCK_STREAM, 0 );
\r
88 configASSERT( xClientSocket != INVALID_SOCKET );
\r
90 /* Connect to the secure server. */
\r
91 if( connect( xClientSocket, ( SOCKADDR * ) &xConnection, sizeof( xConnection ) ) == 0 )
\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
97 if( xWolfSSL_Object != NULL )
\r
99 /* Associate the created wolfSSL object with the connected
\r
101 lReturned = wolfSSL_set_fd( xWolfSSL_Object, xClientSocket );
\r
102 configASSERT( lReturned == SSL_SUCCESS );
\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
110 /* Create the string that is sent to the secure server. */
\r
111 sprintf( cString, "Message number %lu\r\n", ulCount );
\r
113 /* The next line is the secure equivalent of the standard
\r
115 lReturned = send( xClientSocket, cString, strlen( cString ) + 1, 0 ); */
\r
116 lReturned = wolfSSL_write( xWolfSSL_Object, cString, strlen( cString ) + 1 );
\r
119 /* Short delay to prevent the messages streaming up the
\r
120 console too quickly. */
\r
124 } while( ( lReturned != SOCKET_ERROR ) && ( ulCount < 10UL ) );
\r
127 wolfSSL_free( xWolfSSL_Object );
\r
128 closesocket( xClientSocket );
\r
130 /* Delay for a short time before starting over. */
\r
135 /*-----------------------------------------------------------*/
\r