|
| 1 | +/* wolfssl-lwip-client.c |
| 2 | + * |
| 3 | + * Based on client-tls-pkcallback.c |
| 4 | + * |
| 5 | + * Copyright (C) 2006-2024 wolfSSL Inc. |
| 6 | + * |
| 7 | + * This file is part of wolfSSL. |
| 8 | + * |
| 9 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 2 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * wolfSSL is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 22 | + */ |
| 23 | + |
| 24 | +/* This example shows how to write a simple TLS client that uses the features |
| 25 | + * of the Analog Devices MAXQ1065 and 1080 USS. Note that this is not a |
| 26 | + * stand-alone application. This is part of an example that can be found in the |
| 27 | + * SDK supplied by Analog Devices. */ |
| 28 | + |
| 29 | +#include "lwip/opt.h" |
| 30 | + |
| 31 | +#if !NO_SYS /* don't build if not configured to run an OS in lwipopts.h */ |
| 32 | + |
| 33 | +/***** Includes *****/ |
| 34 | +#include <stdio.h> |
| 35 | + |
| 36 | +#include <wolfssl/wolfcrypt/sha256.h> |
| 37 | +#include <wolfssl/wolfcrypt/cryptocb.h> |
| 38 | +#include <wolfssl/wolfcrypt/ecc.h> |
| 39 | +#include <wolfssl/wolfcrypt/rsa.h> |
| 40 | +#include <wolfssl/wolfcrypt/asn.h> |
| 41 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 42 | + |
| 43 | +#include "lwip_crypto_libs_app.h" |
| 44 | +#include "crypto_keys.h" |
| 45 | + |
| 46 | +#define TLS12 1 |
| 47 | +#define TLS13 2 |
| 48 | + |
| 49 | +#define ECC 1 |
| 50 | +#define RSA 2 |
| 51 | +#define PSK 3 |
| 52 | + |
| 53 | +#if defined(TEST_TLS_1_3) |
| 54 | +static int tls_version = TLS13; |
| 55 | +#elif defined(TEST_TLS_1_2) |
| 56 | +static int tls_version = TLS12; |
| 57 | +#else |
| 58 | +#error TEST_TLS_1_3 or TEST_TLS_1_2 MUST be defined in project.mk to test TLS |
| 59 | +#endif |
| 60 | + |
| 61 | +#if defined(TEST_PSK) |
| 62 | +static int alg = PSK; |
| 63 | +#elif defined(TEST_RSA) |
| 64 | +static int alg = RSA; |
| 65 | +#else |
| 66 | +static int alg = ECC; |
| 67 | +#endif |
| 68 | + |
| 69 | +static char *ciphersuite = NULL; |
| 70 | +static const char* kIdentityStr = "Client_identity"; |
| 71 | + |
| 72 | +static unsigned int my_psk_client_cb(struct WOLFSSL* ssl, const char* hint, |
| 73 | + char* identity, unsigned int id_max_len, |
| 74 | + uint8_t* key, unsigned int key_max_len) |
| 75 | +{ |
| 76 | + (void)ssl; |
| 77 | + (void)hint; |
| 78 | + (void)key; |
| 79 | + (void)key_max_len; |
| 80 | + |
| 81 | + /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| 82 | + XSTRNCPY(identity, kIdentityStr, id_max_len); |
| 83 | + |
| 84 | + /* For TLS 1.2, we indicate that MAXQ has the PSK. */ |
| 85 | + return USE_HW_PSK; |
| 86 | +} |
| 87 | + |
| 88 | +#ifdef WOLFSSL_TLS13 |
| 89 | +static unsigned int my_psk_client_cs_cb(struct WOLFSSL* ssl, const char* hint, |
| 90 | + char* identity, unsigned int id_max_len, |
| 91 | + unsigned char* key, unsigned int key_max_len, |
| 92 | + const char* ciphersuite) |
| 93 | +{ |
| 94 | + (void)ssl; |
| 95 | + (void)hint; |
| 96 | + (void)key; |
| 97 | + (void)key_max_len; |
| 98 | + |
| 99 | +#ifdef WOLFSSL_PSK_MULTI_ID_PER_CS |
| 100 | + /* Multiple calls for each cipher suite. First identity byte indicates the |
| 101 | + * number of identites seen so far for cipher suite. */ |
| 102 | + if (identity[0] != 0) { |
| 103 | + return 0; |
| 104 | + } |
| 105 | +#endif /* WOLFSSL_PSK_MULTI_ID_PER_CS */ |
| 106 | + |
| 107 | + /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */ |
| 108 | + XSTRNCPY(identity, kIdentityStr, id_max_len); |
| 109 | + XSTRNCAT(identity, ciphersuite + XSTRLEN(ciphersuite) - 6, id_max_len); |
| 110 | + |
| 111 | + /* For TLS 1.3, we just return an unmodified key. */ |
| 112 | + return 32; |
| 113 | +} |
| 114 | +#endif /* WOLFSSL_TLS13 */ |
| 115 | + |
| 116 | +void mxc_wolfssl_create(int sockfd, WOLFSSL **ssl) |
| 117 | +{ |
| 118 | + int ret; |
| 119 | + int exit_clean = 0; |
| 120 | + |
| 121 | + /* declare wolfSSL objects */ |
| 122 | + WOLFSSL_CTX* ctx = NULL; |
| 123 | + |
| 124 | + /*---------------------------------------------------*/ |
| 125 | + /* Start of wolfSSL initialization and configuration */ |
| 126 | + /*---------------------------------------------------*/ |
| 127 | +#ifdef DEBUG_WOLFSSL |
| 128 | + wolfSSL_Debugging_ON(); |
| 129 | +#endif |
| 130 | + |
| 131 | + /* Initialize wolfSSL */ |
| 132 | + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { |
| 133 | + MXC_ERR_MSG("ERROR: Failed to initialize the library\n"); |
| 134 | + exit_clean = 1; |
| 135 | + goto exit; |
| 136 | + } |
| 137 | + |
| 138 | + /* Create and initialize WOLFSSL_CTX */ |
| 139 | +#ifdef WOLFSSL_TLS13 |
| 140 | + if (tls_version == TLS13) { |
| 141 | + MXC_DEBUG_MSG_GRN("TLS v1.3\n"); |
| 142 | + ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()); |
| 143 | + } else |
| 144 | +#endif /* WOLFSSL_TLS13 */ |
| 145 | + { |
| 146 | + MXC_DEBUG_MSG_GRN("TLS v1.2\n"); |
| 147 | + ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); |
| 148 | + } |
| 149 | + |
| 150 | + if (ctx == NULL) { |
| 151 | + MXC_ERR_MSG("ERROR: failed to create WOLFSSL_CTX\n"); |
| 152 | + ret = -1; |
| 153 | + exit_clean = 1; |
| 154 | + goto exit; |
| 155 | + } |
| 156 | + |
| 157 | + /* At this point you would normally register a CA certificate, however, it |
| 158 | + * resides in MAXQ10xx and has already been registered. */ |
| 159 | +#ifndef NO_FILESYSTEM |
| 160 | + /* Load the dummy private key; actually a public key. The actual private |
| 161 | + * key resides in MAXQ 10xx. */ |
| 162 | + if (key_file != NULL) { |
| 163 | + if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, key_file, |
| 164 | + WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { |
| 165 | + MXC_ERR_MSG("ERROR: failed to load %s, please check the " |
| 166 | + "file.\n", key_file); |
| 167 | + exit_clean = 1; |
| 168 | + goto exit; |
| 169 | + } |
| 170 | + } |
| 171 | +#else |
| 172 | + |
| 173 | +#if defined (TEST_RSA) |
| 174 | +ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, rsa_key_der_2048, |
| 175 | + sizeof(rsa_key_der_2048), WOLFSSL_FILETYPE_ASN1); |
| 176 | +#elif defined (HAVE_ECC) |
| 177 | + ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, ecc_key_der_256, |
| 178 | + sizeof(ecc_key_der_256), WOLFSSL_FILETYPE_ASN1); |
| 179 | +#else |
| 180 | + ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048, |
| 181 | + sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1); |
| 182 | +#endif |
| 183 | + |
| 184 | + if (ret != WOLFSSL_SUCCESS) { |
| 185 | + MXC_DEBUG_MSG("error loading private key\n"); |
| 186 | + goto exit; |
| 187 | + } |
| 188 | +#endif |
| 189 | + /* If specified, set the ciphersuite. */ |
| 190 | + if (ciphersuite != NULL) { |
| 191 | + if (wolfSSL_CTX_set_cipher_list(ctx, ciphersuite) != WOLFSSL_SUCCESS) { |
| 192 | + MXC_ERR_MSG("Invalid cipher suite.\n"); |
| 193 | + exit_clean = 1; |
| 194 | + goto exit; |
| 195 | + } |
| 196 | + } |
| 197 | +#ifndef NO_PSK |
| 198 | + if (alg == PSK) { |
| 199 | + wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb); |
| 200 | +#ifdef WOLFSSL_TLS13 |
| 201 | + if (tls_version == TLS13) { |
| 202 | + wolfSSL_CTX_set_psk_client_cs_callback(ctx, my_psk_client_cs_cb); |
| 203 | + } |
| 204 | +#endif /* WOLFSSL_TLS13 */ |
| 205 | + |
| 206 | + if (ciphersuite != NULL) { |
| 207 | + wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)ciphersuite); |
| 208 | + } |
| 209 | + } |
| 210 | +#endif |
| 211 | + /* Validate peer certificate */ |
| 212 | + wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL); |
| 213 | + |
| 214 | + /* Create a WOLFSSL object */ |
| 215 | + if ((*ssl = wolfSSL_new(ctx)) == NULL) { |
| 216 | + MXC_ERR_MSG("ERROR: failed to create WOLFSSL object\n"); |
| 217 | + ret = -1; |
| 218 | + exit_clean = 1; |
| 219 | + goto exit; |
| 220 | + } |
| 221 | +exit: |
| 222 | + /* Cleanup and return */ |
| 223 | + if (ctx != NULL) |
| 224 | + wolfSSL_CTX_free(ctx); |
| 225 | + if (exit_clean) |
| 226 | + mxc_wolfssl_close(sockfd, *ssl); |
| 227 | +} |
| 228 | + |
| 229 | +void mxc_wolfssl_connect(int sockfd, WOLFSSL *ssl) |
| 230 | +{ |
| 231 | + int ret, err; |
| 232 | + int exit_clean = 0; |
| 233 | + |
| 234 | +#ifdef WOLFSSL_TLS13 |
| 235 | + if (alg == RSA) { |
| 236 | + if (wolfSSL_UseKeyShare(ssl, WOLFSSL_FFDHE_2048) != WOLFSSL_SUCCESS) { |
| 237 | + MXC_ERR_MSG("ERROR: failed to create WOLFSSL object\n"); |
| 238 | + ret = -1; |
| 239 | + exit_clean = 1; |
| 240 | + goto exit; |
| 241 | + } |
| 242 | + } |
| 243 | +#endif /* WOLFSSL_TLS13 */ |
| 244 | + //MXC_DEBUG_MSG("mxc_wolfssl_connect->wolfSSL_set_fd\n"); |
| 245 | + /* Attach wolfSSL to the socket */ |
| 246 | + if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { |
| 247 | + MXC_ERR_MSG("ERROR: Failed to set the file descriptor\n"); |
| 248 | + exit_clean = 1; |
| 249 | + goto exit; |
| 250 | + } |
| 251 | + |
| 252 | + wolfSSL_SSLSetIORecv(ssl, mxc_wolfssl_receive); |
| 253 | + wolfSSL_SSLSetIOSend(ssl, mxc_wolfssl_send); |
| 254 | + |
| 255 | + //MXC_DEBUG_MSG("mxc_wolfssl_connect->wolfSSL_connect\n"); |
| 256 | + /* Connect to wolfSSL on the server side */ |
| 257 | + do { |
| 258 | + ret = wolfSSL_connect(ssl); |
| 259 | + err = wolfSSL_get_error(ssl, ret); |
| 260 | + } while (err == WC_PENDING_E); |
| 261 | + if (ret != WOLFSSL_SUCCESS) { |
| 262 | + MXC_ERR_MSG("ERROR: failed to connect to wolfSSL\n"); |
| 263 | + exit_clean = 1; |
| 264 | + goto exit; |
| 265 | + } |
| 266 | + |
| 267 | + MXC_DEBUG_MSG_GRN("\nmxc_wolfssl_connect-> Done\n\n"); |
| 268 | + |
| 269 | +exit: |
| 270 | + if (exit_clean) |
| 271 | + mxc_wolfssl_close(sockfd, ssl); |
| 272 | +} |
| 273 | + |
| 274 | +void mxc_wolfssl_write(WOLFSSL* ssl, const void* data, int sz) |
| 275 | +{ |
| 276 | + int ret; |
| 277 | + |
| 278 | + MXC_DEBUG_MSG(". mxc_wolfssl_write...\n"); |
| 279 | + |
| 280 | + /* Send the message to the server */ |
| 281 | + if ((ret = wolfSSL_write(ssl, data, sz)) != sz) { |
| 282 | + MXC_ERR_MSG("ERROR: failed to write entire message\n"); |
| 283 | + MXC_ERR_MSG("%d bytes of %d bytes were sent", ret, sz); |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +int mxc_wolfssl_read(WOLFSSL* ssl) |
| 288 | +{ |
| 289 | + int ret; |
| 290 | + char buff[SOCKET_BUF_SIZE]; |
| 291 | + |
| 292 | + /* Read the server data into our buff array */ |
| 293 | + memset(buff, 0, sizeof(buff)); |
| 294 | + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { |
| 295 | + MXC_ERR_MSG("ERROR: failed to read\n"); |
| 296 | + } |
| 297 | + |
| 298 | + /* Print to stdout any data the server sends */ |
| 299 | + MXC_DEBUG_MSG("Server: %s\n", buff); |
| 300 | + return ret; |
| 301 | + // ret = 0; /* success */ |
| 302 | +} |
| 303 | + |
| 304 | +void mxc_wolfssl_close(int sockfd, WOLFSSL* ssl) |
| 305 | +{ |
| 306 | + unsigned char lBuff[SOCKET_BUF_SIZE]; |
| 307 | + /* Cleanup and return */ |
| 308 | + if (ssl != NULL) |
| 309 | + wolfSSL_free(ssl); |
| 310 | + |
| 311 | + wolfSSL_Cleanup(); |
| 312 | + |
| 313 | + if (sockfd >= 0) |
| 314 | + lwip_close(sockfd); |
| 315 | + while(lwip_read(sockfd,lBuff, SOCKET_BUF_SIZE) > 0); |
| 316 | +} |
| 317 | + |
| 318 | +#endif /* !NO_SYS */ |
0 commit comments