1 /* wolfssl_thread_entry.c 2 * 3 * Copyright (C) 2006-2021 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 /* wolfSSL */ 23 #include <wolfssl/wolfcrypt/settings.h> 24 #include <wolfssl/ssl.h> 25 #include <wolfssl/wolfio.h> 26 #include "wolfssl_thread_entry.h" 27 28 /* FreeRTOS */ 29 #include "FreeRTOS_IP.h" 30 #include "FreeRTOS_Sockets.h" 31 32 /* Project Specific */ 33 #include "common/util.h" 34 #include <stdio.h> 35 36 void wolfssl_thread_entry(void *pvParameters) { 37 FSP_PARAMETER_NOT_USED(pvParameters); 38 39 /* FreeRTOS+TCP Objects */ 40 BaseType_t fr_status; 41 socklen_t xSize = sizeof(struct freertos_sockaddr); 42 xSocket_t xClientSocket = NULL; 43 struct freertos_sockaddr xRemoteAddress; 44 45 /* Return status */ 46 int ret = WOLFSSL_FAILURE; 47 48 /* Message to send and buffer for receive. */ 49 const char msg[] = "Hello from wolfSSL client."; 50 char buff[256]; 51 52 /* wolfSSL objects */ 53 WOLFSSL_CTX *ctx = NULL; 54 WOLFSSL *ssl = NULL; 55 56 /* Output to Renesas Debug Virtual Console */ 57 initialise_monitor_handles(); 58 #ifdef DEBUG_WOLFSSL 59 wolfSSL_Debugging_ON(); 60 #endif 61 62 /* FreeRTOS+TCP Ethernet and IP Setup */ 63 fr_status = FreeRTOS_IPInit(ucIPAddress, 64 ucNetMask, 65 ucGatewayAddress, 66 ucDNSServerAddress, 67 g_ether0_mac_address); 68 if (pdPASS != fr_status) { 69 printf("Error [%ld]: FreeRTOS_IPInit.\n",fr_status); 70 util_inf_loop(xClientSocket, ctx, ssl); 71 } 72 73 /* Client Socket Setup */ 74 xRemoteAddress.sin_port = FreeRTOS_htons(DEFAULT_PORT); 75 xRemoteAddress.sin_addr = FreeRTOS_inet_addr(SERVER_IP); 76 77 /* Create a FreeRTOS TCP Socket and connect */ 78 xClientSocket = FreeRTOS_socket(FREERTOS_AF_INET, 79 FREERTOS_SOCK_STREAM, 80 FREERTOS_IPPROTO_TCP); 81 configASSERT(xClientSocket != FREERTOS_INVALID_SOCKET); 82 FreeRTOS_bind(xClientSocket, &xRemoteAddress, sizeof(xSize)); 83 84 /* Client Socket Connect */ 85 ret = FreeRTOS_connect(xClientSocket, 86 &xRemoteAddress, 87 sizeof(xRemoteAddress)); 88 if (ret != FR_SOCKET_SUCCESS) { 89 printf("Error [%d]: FreeRTOS_connect.\n",ret); 90 util_inf_loop(xClientSocket, ctx, ssl); 91 } 92 93 /* TLS Connection Setup */ 94 /* Initialize wolfSSL */ 95 wolfSSL_Init(); 96 97 /* Create and initialize WOLFSSL_CTX */ 98 ctx = wolfSSL_CTX_new(wolfSSLv23_client_method_ex((void *)NULL)); 99 if (ctx == NULL) { 100 printf("Error: wolfSSL_CTX_new.\n"); 101 util_inf_loop(xClientSocket, ctx, ssl); 102 } 103 104 /* Load client certificates into WOLFSSL_CTX */ 105 ret = wolfSSL_CTX_load_verify_buffer(ctx, CERT, SIZEOF_CERT, 106 WOLFSSL_FILETYPE_ASN1); 107 if (ret != WOLFSSL_SUCCESS) { 108 printf("Error [%d]: wolfSSL_CTX_load_verify_buffer.\n",ret); 109 util_inf_loop(xClientSocket, ctx, ssl); 110 } 111 112 /* Create a WOLFSSL object */ 113 ssl = wolfSSL_new(ctx); 114 if (ssl == NULL) { 115 printf("Error: wolfSSL_new.\n"); 116 util_inf_loop(xClientSocket, ctx, ssl); 117 } 118 119 /* Attach wolfSSL to the socket */ 120 ret = wolfSSL_set_fd(ssl, (int) xClientSocket); 121 if (ret != WOLFSSL_SUCCESS) { 122 printf("Error [%d]: wolfSSL_set_fd.\n",ret); 123 util_inf_loop(xClientSocket, ctx, ssl); 124 } 125 126 /* Connect to wolfSSL on the server side */ 127 ret = wolfSSL_connect(ssl); 128 if (ret != WOLFSSL_SUCCESS) { 129 ret = wolfSSL_get_error(ssl, ret); 130 printf("Error [%d]: wolfSSL_connect.\n",ret); 131 util_inf_loop(xClientSocket, ctx, ssl); 132 } 133 134 /* Send the message to the server */ 135 ret = wolfSSL_write(ssl, msg, (int) XSTRLEN(msg)); 136 if (ret < 0) { 137 printf("Error: wolfSSL_write.\n"); 138 util_inf_loop(xClientSocket, ctx, ssl); 139 } 140 141 /* Read the server data into buff array */ TemplateArgument()142 XMEMSET(buff, 0, sizeof(buff)); 143 ret = wolfSSL_read(ssl, buff, sizeof(buff) - 1); 144 145 /* Shutdown TLS connection */ 146 wolfSSL_shutdown(ssl); 147 148 /* Cleanup */ 149 util_inf_loop(xClientSocket, ctx, ssl); 150 } 151