Merge pull request #282 from rizlik/psa

psa: add tls13 ecc client/server examples
pull/290/head
David Garske 2022-02-07 10:40:33 -08:00 committed by GitHub
commit 55cad5f79a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 2082 additions and 0 deletions

33
psa/Makefile 100644
View File

@ -0,0 +1,33 @@
CC = gcc
LIB_PATH = /usr/local
CFLAGS ?= -Wall -I$(INCLUDE_PATH)
LIBS ?= -L$(LIB_PATH)/lib -lm
# PSA implementation
PSA_INCLUDE ?=
PSA_LIB_PATH ?=
# option variables
DYN_LIB = -lwolfssl -lm
STATIC_LIB = $(LIB_PATH)/lib/libwolfssl.a
DEBUG_FLAGS = -g -DDEBUG
DEBUG_INC_PATHS = -MD
OPTIMIZE = -Os
# Options
#CFLAGS+=$(DEBUG_FLAGS)
CFLAGS+=$(OPTIMIZE)
#LIBS+=$(STATIC_LIB)
LIBS+=$(DYN_LIB)
CFLAGS += -I$(PSA_INCLUDE)
LIBS += $(PSA_LIB_PATH)
TARGETS=client-tls13-ecc-psa server-tls13-ecc-psa
all: $(TARGETS)
%: %.c
$(CC) -o $@ $< $(CFLAGS) $(LIBS)
clean:
@rm -f $(TARGETS) *.o

99
psa/README.md 100644
View File

@ -0,0 +1,99 @@
# Examples using wolfSSL with Platform Security Architecture (PSA)
==================================================================
## Example lists
- client/server TLS1.3 ECC example
- Trusted Firmware-M TLS1.3 on Nucleo-l552ZE-Q board
## client/server TLS1.3 ECDH-ECC example
These examples are modified versions of client/server examples found in tls/ but
using PSA for ECDH, ECDSA, HKDF, AES-GCM and SHA256.
### BUILD
To build the client/server examples you must provide the path to a static
library that implements the PSA interface using the environment variable
`PSA_LIB_PATH`. You can optionally specify the location of the PSA headers with
`PSA_INCLUDE`. After that you can run `make` to build the `client-tls13-ecc-psa`
and `server-tls13-ecc-psa`.
```
PSA_INCLUDE=/path/to/psa/include PSA_LIB_PATH=/path/to/psa/library.a make
```
wolfSSL must be built with PSA (`--enable-psa`) and public key callbacks
(`--enable-pkcallbacks`) support and must be built using the same PSA headers
used to compile the examples.
### Using MbedTLS PSA implementation
You can test these examples with mbedTLS PSA implementation. For this task the
helper script `build_with_mbedtls_psa.sh` is provided. It must run from the
wolfSSL source root directory and it compiles the mbedTLS library in
`/tmp/mbedtls` . To use the script and then compile the examples use these
commands:
```
cd /path/to/wolfSSL/src;
./path/to/wolfssl-examples/psa/build_with_mbedtls_psa.sh
make install
cd /path/to/wolfssl-examples/psa/
export PSA_INCLUDE=/tmp/mbedtls/build/include
export PSA_LIB_PATH=/tmp/mbedtls/build/library/libmbedcrypto.a
make
```
## Trusted Firmware-M TLS1.3
TLS1.3 client/server exchange a small message over memory in PSA enabled
Trusted Firmware-M (TF-M) on Nucleo-l552ZE-Q board.
This example is provided as a patch to the TF-M test repo, which is normally
used as the default Non Secure app in the TF-M repo. This way the example
integrates smoothly inside the TF-M build system.
The general requirements to build TF-M are listed here
[TF-M doc](https://tf-m-user-guide.trustedfirmware.org/docs/getting_started/tfm_getting_started.html)
To compile TF-M on Nucleo-l552ZE-Q board you additionally need:
- GNU Arm compiler v7.3.1+ [toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)
- STM32_Programmer_CLI see [here](https://www.st.com/en/development-tools/stm32cubeprog.html)
To have all the needed binary artifacts to flash on the board you need three
interacting parts: the main TF-M repo (for bootloader and Secure world), wolfSSL
PSA-enabled library, and the modified TF-M test repo (for Non-Secure world). The
provided script `build_tfm_example.sh` automatically downloads and compile all
the needed components and produces the final build artifacts. The toolchain
needs to be available on the default path for the script to work.
CAVEATS:
The example only works with TF-M commit ID f07cc31545bbba3bad1806ed078c3aee3a09dc52
After running `build_tfm_example.sh` you can flash the binaries artifacts from
the TF-M build directory (defaults to `/tmp/wolfssl_tfm/tfm/build`) and run:
```
./regression.sh && ./TFM_UPDATE.sh
```
to flash on the nucelo board. Remember that this step needs
`STM32_Programmer_CLI`installed and on the default PATH.
After that you will see client and server interacting on the UART of the board:
```
[Sec Thread] Secure image initializing!
TF-M FP mode: Software
Booting TFM v1.5.0
Non-Secure system starting...
wolfSSL demo
wolfSSL_Init Success
wolfSSL provisioning server secret key
Server is starting
Client is starting
Overriding cert date error as example for bad clock testing
Received message from client:
hello wolfssl!
```

View File

@ -0,0 +1,57 @@
#!/bin/bash
# wolfSSL with PSA enabled using mbedtls PSA implementation
# This script must run from the wolfSSL root folder
set -e
MBEDTLS_COMMIT_ID=acc74b841307659eea0461275c7c0309874c87d7
MBEDTLS_DIR=${MBEDTLS_DIR:="/tmp/mbedtls"}
MBEDTLS_INCLUDE_DIR=${MBEDTLS_DIR}/build/include/
MBEDTLS_LIB_DIR=${MBEDTLS_DIR}/build/library/
# uncomment to enable debug build
#MBEDTLS_DEBUG="-DCMAKE_BUILD_TYPE=Debug"
#WOLFSSL_DEBUG="-g -ggdb -DDEBUG_WOLFSSL -DWOLFSSL_DEBUG_TLS"
if [ ! -d ./wolfssl ]
then
echo "You need to run this script from inside the wolfSSL root folder"
exit -1
fi
download_mbedtls_src() {
echo "downloading mbedtls source in ${MBEDTLS_DIR}..."
if [ -d "${MBEDTLS_DIR}" ]
then
echo "${MBEDTLS_DIR} exists, skipping src dowload.."
return
fi
mkdir -p "${MBEDTLS_DIR}"
curl --location https://github.com/ARMmbed/mbedtls/archive/${MBEDTLS_COMMIT_ID}.tar.gz | \
tar --directory="${MBEDTLS_DIR}" --strip-components=1 -x -z
}
build_mbedtls() {
echo "building mbedtls in ${MBEDTLS_DIR}/build..."
mkdir -p "${MBEDTLS_DIR}/build"
(cd "${MBEDTLS_DIR}/build" && cmake ${MBEDTLS_DEBUG} -DCMAKE_POSITION_INDEPENDENT_CODE=ON ../)
(cd "${MBEDTLS_DIR}/build" && cmake --build .)
}
build_wolfssl() {
echo "building wolfSSL with PSA enabled..."
if [ ! -f ./configure ]
then
./autogen.sh
fi
CFLAGS="-Wno-error=redundant-decls -Werror -Wswitch-enum -Wno-error=switch-enum -DWOLFSSL_PSA_GLOBAL_LOCK ${WOLFSSL_DEBUG}" \
./configure \
--enable-psa --with-psa-include="${MBEDTLS_INCLUDE_DIR}" --enable-pkcallbacks\
--disable-examples --disable-benchmark --disable-crypttests
make
}
download_mbedtls_src
build_mbedtls
build_wolfssl

View File

@ -0,0 +1,280 @@
/* client-tls13-ecc-psa.c
*
* Copyright (C) 2006-2020 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* the usual suspects */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* socket includes */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
/* wolfSSL */
#include <wolfssl/options.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfio.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/port/psa/psa.h>
#define DEFAULT_PORT 11111
#define CERT_FILE "../certs/ca-ecc-cert.pem"
#if defined(WOLFSSL_TLS13) && defined(HAVE_SECRET_CALLBACK)
#ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
#define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
#endif
/* Callback function for TLS v1.3 secrets for use with Wireshark */
static int Tls13SecretCallback(WOLFSSL* ssl, int id, const unsigned char* secret,
int secretSz, void* ctx)
{
int i;
const char* str = NULL;
unsigned char clientRandom[32];
int clientRandomSz;
XFILE fp = stderr;
if (ctx) {
fp = XFOPEN((const char*)ctx, "ab");
if (fp == XBADFILE) {
return BAD_FUNC_ARG;
}
}
clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom,
sizeof(clientRandom));
if (clientRandomSz <= 0) {
printf("Error getting client random %d\n", clientRandomSz);
}
#if 0
printf("TLS Client Secret CB: Rand %d, Secret %d\n",
clientRandomSz, secretSz);
#endif
switch (id) {
case CLIENT_EARLY_TRAFFIC_SECRET:
str = "CLIENT_EARLY_TRAFFIC_SECRET"; break;
case EARLY_EXPORTER_SECRET:
str = "EARLY_EXPORTER_SECRET"; break;
case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break;
case SERVER_HANDSHAKE_TRAFFIC_SECRET:
str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break;
case CLIENT_TRAFFIC_SECRET:
str = "CLIENT_TRAFFIC_SECRET_0"; break;
case SERVER_TRAFFIC_SECRET:
str = "SERVER_TRAFFIC_SECRET_0"; break;
case EXPORTER_SECRET:
str = "EXPORTER_SECRET"; break;
}
fprintf(fp, "%s ", str);
for (i = 0; i < clientRandomSz; i++) {
fprintf(fp, "%02x", clientRandom[i]);
}
fprintf(fp, " ");
for (i = 0; i < secretSz; i++) {
fprintf(fp, "%02x", secret[i]);
}
fprintf(fp, "\n");
if (fp != stderr) {
XFCLOSE(fp);
}
return 0;
}
#endif /* WOLFSSL_TLS13 && HAVE_SECRET_CALLBACK */
int main(int argc, char** argv)
{
int ret = 0;
#ifdef WOLFSSL_TLS13
int sockfd = SOCKET_INVALID;
struct sockaddr_in servAddr;
char buff[256];
struct psa_ssl_ctx psa_ctx;
size_t len;
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx = NULL;
WOLFSSL* ssl = NULL;
/* Check for proper calling convention */
if (argc != 2) {
printf("usage: %s <IPv4 address>\n", argv[0]);
return 0;
}
#ifdef DEBUG
wolfSSL_Debugging_ON();
#endif
/* Create a socket that uses an internet IPv4 address,
* Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "ERROR: failed to create the socket\n");
ret = -1; goto exit;
}
/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
/* Get the server IPv4 address from the command line call */
if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
fprintf(stderr, "ERROR: invalid address\n");
ret = -1; goto exit;
}
/* Connect to the server */
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
== -1) {
fprintf(stderr, "ERROR: failed to connect\n");
goto exit;
}
/*---------------------------------*/
/* Start of security */
/*---------------------------------*/
/* Initialize wolfSSL */
if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: Failed to initialize the library\n");
goto exit;
}
/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
ret = -1; goto exit;
}
/* Load client certificates into WOLFSSL_CTX */
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CERT_FILE, NULL))
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
CERT_FILE);
goto exit;
}
if ((ret = wolfSSL_CTX_psa_enable(ctx)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed enable PSA.\n");
goto exit;
}
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
ret = -1; goto exit;
}
/* Attach wolfSSL to the socket */
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
goto exit;
}
/* attach psa context */
XMEMSET(&psa_ctx, 0, sizeof(psa_ctx));
if ((ret = wolfSSL_set_psa_ctx(ssl, &psa_ctx)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: Failed to set psa context\n");
goto exit;
}
#ifdef HAVE_SECRET_CALLBACK
/* required for getting random used */
wolfSSL_KeepArrays(ssl);
/* optional logging for wireshark */
wolfSSL_set_tls13_secret_cb(ssl, Tls13SecretCallback,
(void*)WOLFSSL_SSLKEYLOGFILE_OUTPUT);
#endif
/* Connect to wolfSSL on the server side */
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
goto exit;
}
#ifdef HAVE_SECRET_CALLBACK
wolfSSL_FreeArrays(ssl);
#endif
/* Get a message for the server from stdin */
printf("Message for server: ");
memset(buff, 0, sizeof(buff));
if (fgets(buff, sizeof(buff), stdin) == NULL) {
fprintf(stderr, "ERROR: failed to get message for server\n");
ret = -1; goto exit;
}
len = strnlen(buff, sizeof(buff));
/* Send the message to the server */
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
fprintf(stderr, "ERROR: failed to write entire message\n");
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
goto exit;
}
/* Read the server data into our buff array */
memset(buff, 0, sizeof(buff));
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) < 0) {
fprintf(stderr, "ERROR: failed to read\n");
goto exit;
}
/* Print to stdout any data the server sends */
printf("Server: %s\n", buff);
/* Return reporting a success */
ret = 0;
exit:
/* Cleanup and return */
if (sockfd != SOCKET_INVALID)
close(sockfd); /* Close the connection to the server */
if (ssl) {
wolfSSL_free_psa_ctx(&psa_ctx);
wolfSSL_free(ssl); /* Free the wolfSSL object */
}
if (ctx)
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
#else
printf("Example requires TLS v1.3\n");
#endif
(void)argc;
(void)argv;
return ret;
}

View File

@ -0,0 +1,379 @@
/* server-tls13-ecc-psa.c
*
* Copyright (C) 2006-2020 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* the usual suspects */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* socket includes */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define HAVE_SIGNAL
#ifdef HAVE_SIGNAL
#include <signal.h> /* signal */
#endif
/* wolfSSL */
#include <wolfssl/options.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfio.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/psa/psa.h>
#include <wolfssl/wolfcrypt/logging.h>
#define DEFAULT_PORT 11111
#define CERT_FILE "../certs/server-ecc.pem"
#include <psa/crypto.h>
#if defined(WOLFSSL_TLS13) && defined(HAVE_SECRET_CALLBACK)
#ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
#define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
#endif
/* Callback function for TLS v1.3 secrets for use with Wireshark */
static int Tls13SecretCallback(WOLFSSL* ssl, int id, const unsigned char* secret,
int secretSz, void* ctx)
{
int i;
const char* str = NULL;
unsigned char serverRandom[32];
int serverRandomSz;
XFILE fp = stderr;
if (ctx) {
fp = XFOPEN((const char*)ctx, "ab");
if (fp == XBADFILE) {
return BAD_FUNC_ARG;
}
}
serverRandomSz = (int)wolfSSL_get_server_random(ssl, serverRandom,
sizeof(serverRandom));
if (serverRandomSz <= 0) {
printf("Error getting server random %d\n", serverRandomSz);
}
#if 0
printf("TLS Server Secret CB: Rand %d, Secret %d\n",
serverRandomSz, secretSz);
#endif
switch (id) {
case CLIENT_EARLY_TRAFFIC_SECRET:
str = "CLIENT_EARLY_TRAFFIC_SECRET"; break;
case EARLY_EXPORTER_SECRET:
str = "EARLY_EXPORTER_SECRET"; break;
case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; break;
case SERVER_HANDSHAKE_TRAFFIC_SECRET:
str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; break;
case CLIENT_TRAFFIC_SECRET:
str = "CLIENT_TRAFFIC_SECRET_0"; break;
case SERVER_TRAFFIC_SECRET:
str = "SERVER_TRAFFIC_SECRET_0"; break;
case EXPORTER_SECRET:
str = "EXPORTER_SECRET"; break;
}
fprintf(fp, "%s ", str);
for (i = 0; i < (int)serverRandomSz; i++) {
fprintf(fp, "%02x", serverRandom[i]);
}
fprintf(fp, " ");
for (i = 0; i < secretSz; i++) {
fprintf(fp, "%02x", secret[i]);
}
fprintf(fp, "\n");
if (fp != stderr) {
XFCLOSE(fp);
}
return 0;
}
#endif /* WOLFSSL_TLS13 && HAVE_SECRET_CALLBACK */
static int mSockfd = SOCKET_INVALID;
static int mConnd = SOCKET_INVALID;
static int mShutdown = 0;
#ifdef HAVE_SIGNAL
static void sig_handler(const int sig)
{
fprintf(stderr, "SIGINT handled = %d.\n", sig);
mShutdown = 1;
if (mConnd != SOCKET_INVALID) {
close(mConnd); /* Close the connection to the client */
mConnd = SOCKET_INVALID;
}
if (mSockfd != SOCKET_INVALID) {
close(mSockfd); /* Close the socket listening for clients */
mSockfd = SOCKET_INVALID;
}
}
#endif
/* ./certs/key-ecc.pem */
static const unsigned char ecc_key_256[] =
{
0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85, 0xA1, 0x38,
0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC, 0xC4, 0x03, 0x8D, 0x53,
0x35, 0x04, 0xFA, 0x6C, 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8,
0x09, 0x8C
};
static int psa_private_key_provisioning(psa_key_id_t *key_id)
{
psa_key_attributes_t key_attr = { 0 };
psa_key_type_t key_type;
psa_status_t status;
key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1);
psa_set_key_type(&key_attr, key_type);
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_SIGN_HASH);
psa_set_key_algorithm(&key_attr, PSA_ALG_ECDSA_ANY);
status = psa_import_key(&key_attr, ecc_key_256,
sizeof(ecc_key_256), key_id);
if (status != PSA_SUCCESS) {
fprintf(stderr,
"ERROR: provisioning of private key failed: [%d] \n", status);
return -1;
}
return 0;
}
int main(int argc, char** argv)
{
int ret = 0;
struct sockaddr_in servAddr;
struct sockaddr_in clientAddr;
socklen_t size = sizeof(clientAddr);
char buff[256];
size_t len;
const char* reply = "I hear ya fa shizzle!\n";
struct psa_ssl_ctx psa_ctx;
/* declare wolfSSL objects */
WOLFSSL_CTX* ctx = NULL;
WOLFSSL* ssl = NULL;
psa_key_id_t ecc_key_id;
#ifdef HAVE_SIGNAL
signal(SIGINT, sig_handler);
#endif
/* Initialize wolfSSL */
wolfSSL_Init();
#ifdef DEBUG
wolfSSL_Debugging_ON();
#endif
if ((ret = psa_private_key_provisioning(&ecc_key_id)) != 0) {
fprintf(stderr, "ERROR: failed to provisioning the psa key\n");
goto exit;
}
XMEMSET(&psa_ctx, 0, sizeof(psa_ctx));
wolfSSL_psa_set_private_key_id(&psa_ctx, ecc_key_id);
/* Create a socket that uses an internet IPv4 address,
* Sets the socket to be stream based (TCP),
* 0 means choose the default protocol. */
if ((mSockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "ERROR: failed to create the socket\n");
goto exit;
}
if (setsockopt(mSockfd,
SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
fprintf(stderr, "ERROR: failed to set socket option reuseaddr\n");
/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method())) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
ret = -1;
goto exit;
}
/* Load server certificates into WOLFSSL_CTX */
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM))
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
CERT_FILE);
goto exit;
}
if ((ret = wolfSSL_CTX_psa_enable(ctx)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed enable PSA.\n");
goto exit;
}
/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));
/* Fill in the server address */
servAddr.sin_family = AF_INET; /* using IPv4 */
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */
/* Bind the server socket to our port */
if (bind(mSockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
fprintf(stderr, "ERROR: failed to bind\n");
goto exit;
}
/* Listen for a new connection, allow 5 pending connections */
if (listen(mSockfd, 5) == -1) {
fprintf(stderr, "ERROR: failed to listen\n");
goto exit;
}
/* Continue to accept clients until mShutdown is issued */
while (!mShutdown) {
printf("Waiting for a connection...\n");
/* Accept client connections */
if ((mConnd = accept(mSockfd, (struct sockaddr*)&clientAddr, &size))
== -1) {
fprintf(stderr, "ERROR: failed to accept the connection\n\n");
ret = -1; goto exit;
}
/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
ret = -1; goto exit;
}
if ((ret = wolfSSL_set_psa_ctx(ssl, &psa_ctx))
!= WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: can't enable PSA in the context\n");
goto exit;
}
/* Attach wolfSSL to the socket */
wolfSSL_set_fd(ssl, mConnd);
#ifdef HAVE_SECRET_CALLBACK
/* required for getting random used */
wolfSSL_KeepArrays(ssl);
/* optional logging for wireshark */
wolfSSL_set_tls13_secret_cb(ssl, Tls13SecretCallback,
(void*)WOLFSSL_SSLKEYLOGFILE_OUTPUT);
#endif
/* Establish TLS connection */
if ((ret = wolfSSL_accept(ssl)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "wolfSSL_accept error = %d\n",
wolfSSL_get_error(ssl, ret));
goto exit;
}
printf("Client connected successfully\n");
#ifdef HAVE_SECRET_CALLBACK
wolfSSL_FreeArrays(ssl);
#endif
/* Read the client data into our buff array */
memset(buff, 0, sizeof(buff));
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) < 0) {
fprintf(stderr, "ERROR: failed to read\n");
goto exit;
}
/* Print to stdout any data the client sends */
printf("Client: %s\n", buff);
/* Check for server shutdown command */
if (strncmp(buff, "shutdown", 8) == 0) {
printf("Shutdown command issued!\n");
mShutdown = 1;
}
/* Write our reply into buff */
memset(buff, 0, sizeof(buff));
memcpy(buff, reply, strlen(reply));
len = strnlen(buff, sizeof(buff));
/* Reply back to the client */
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
fprintf(stderr, "ERROR: failed to write\n");
goto exit;
}
/* Cleanup after this connection */
wolfSSL_shutdown(ssl);
if (ssl) {
wolfSSL_free_psa_ctx(&psa_ctx);
wolfSSL_free(ssl); /* Free the wolfSSL object */
ssl = NULL;
}
if (mConnd != SOCKET_INVALID) {
close(mConnd); /* Close the connection to the client */
mConnd = SOCKET_INVALID;
}
}
printf("Shutdown complete\n");
exit:
/* Cleanup and return */
if (ssl) {
wolfSSL_free_psa_ctx(&psa_ctx);
wolfSSL_free(ssl); /* Free the wolfSSL object */
}
if (mConnd != SOCKET_INVALID) {
close(mConnd); /* Close the connection to the client */
mConnd = SOCKET_INVALID;
}
if (mSockfd != SOCKET_INVALID) {
close(mSockfd); /* Close the socket listening for clients */
mSockfd = SOCKET_INVALID;
}
if (ctx)
wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
(void)argc;
(void)argv;
return ret;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,127 @@
#!/bin/bash
set -e
WOLFSSL_TFM_WORKDIR=${WOLFSSL_TFM_WORKDIR:="/tmp"}
TFM_GIT_URL=https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git
TFM_COMMIT_ID=f07cc31545bbba3bad1806ed078c3aee3a09dc52
TRUSTED_FIRMWARE_DIR=${TRUSTED_FIRMWARE_DIR:="${WOLFSSL_TFM_WORKDIR}/wolfssl_tfm/tfm"}
WOLFSSL_DIR=${WOLFSSL_DIR:="${WOLFSSL_TFM_WORKDIR}/wolfssl_tfm/wolfssl"}
TEST_REPO_GIT_URL=https://git.trustedfirmware.org/TF-M/tf-m-tests.git
TEST_REPO_TAG=TF-Mv1.5.0
TEST_REPO_DIR=${TEST_REPO_DIR:="${WOLFSSL_TFM_WORKDIR}/wolfssl_tfm/wolfssl_test_repo"}
download_trusted_firmware_m() {
echo "downloading trusted firmware-m source in ${TRUSTED_FIRMWARE_DIR}..."
if [ -d "${TRUSTED_FIRMWARE_DIR}" ]
then
echo "${TRUSTED_FIRMWARE_DIR} exists, skipping src dowload.."
return
fi
mkdir -p "${TRUSTED_FIRMWARE_DIR}"
git clone "${TFM_GIT_URL}" "${TRUSTED_FIRMWARE_DIR}"
(cd "${TRUSTED_FIRMWARE_DIR}" && git checkout "${TFM_COMMIT_ID}")
}
download_wolfssl_src() {
echo "downloading WolfSSL source in ${WOLFSSL_DIR}..."
if [ -d "${WOLFSSL_DIR}" ]
then
echo "${WOLFSSL_DIR} exists, skipping src dowload.."
return
fi
mkdir -p "${WOLFSSL_DIR}"
curl --location https://api.github.com/repos/wolfssl/wolfssl/tarball/master | \
tar --directory="${WOLFSSL_DIR}" --strip-components=1 -x -z
}
download_tfm_repo_test_src() {
echo "downloading tfm_test_repo in ${TEST_REPO_DIR}..."
if [ -d "${TEST_REPO_DIR}" ]
then
echo "${TEST_REPO_DIR} exists, skipping src dowload.."
return
fi
mkdir -p "${TEST_REPO_DIR}"
git clone --depth 1 --branch "${TEST_REPO_TAG}"\
"${TEST_REPO_GIT_URL}" "${TEST_REPO_DIR}"
echo "applying wolfssl_patch to ${TEST_REPO_DIR}..."
cp ./0001-WolfSSL-TLS-1.3-client-server-PSA-demo.patch "${TEST_REPO_DIR}"
(cd "${TEST_REPO_DIR}" && \
git apply ./0001-WolfSSL-TLS-1.3-client-server-PSA-demo.patch)
}
compile_tfm() {
# restart from scratch if build dir already exists
if [ -d "${TRUSTED_FIRMWARE_DIR}/build" ]
then
rm -rf "${TRUSTED_FIRMWARE_DIR}/build"
fi
(cd "${TRUSTED_FIRMWARE_DIR}" && \
mkdir build && \
cd build && \
cmake .. -DTFM_PLATFORM=stm/nucleo_l552ze_q \
-DTFM_TOOLCHAIN_FILE=../toolchain_GNUARM.cmake \
-G"Unix Makefiles" \
-DNS=ON \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DTEST_S=OFF \
-DTEST_NS=OFF \
-DTFM_TEST_REPO_PATH="${TEST_REPO_DIR}" \
-DWOLFSSL_DEMO=ON \
-DWOLFSSL_ROOT_PATH="${WOLFSSL_DIR}"\
-DTFM_NS_REG_TEST=ON)
(cd "${TRUSTED_FIRMWARE_DIR}/build" && cmake --build . -- install && ./postbuild.sh)
}
compile_wolfssl() {
(cd "${WOLFSSL_DIR}" && \
./autogen.sh && \
CFLAGS="-mcpu=cortex-m33 -Os --specs=nano.specs -fdata-sections -ffunction-sections -fno-builtin -fshort-enums -funsigned-char -mthumb -nostdlib -Wno-error=redundant-decls -Wno-error=switch-enum \
-DNO_WOLFSSL_DIR -DWOLFSSL_NO_SOCK -DNO_WRITEV -DWOLFSSL_USER_IO -DNO_SHA512 -DNO_SHA224 -DNO_SHA -DNO_ERROR_STRINGS -DNO_FILESYSTEM -DBENCH_EMBEDDED -DWOLFSSL_SMALL_STACK" \
./configure \
--host=arm-none-eabi \
--disable-examples \
--disable-rsa \
--disable-chacha \
--disable-poly1305 \
--disable-dh \
--disable-md5 \
--disable-sha512 \
--disable-sha224 \
--disable-sha \
--disable-sha384 \
--disable-pwdbased \
--disable-pkcs12 \
--disable-tlsv12 \
--disable-crypttests \
--disable-benchmark \
--enable-pkcallbacks \
--enable-psa \
--with-psa-include="${TRUSTED_FIRMWARE_DIR}/interface/include" && \
make)
}
flash_tfm() {
(cd "${TRUSTED_FIRMWARE_DIR}/build" && \
./regression.sh && \
./TFM_UPDATE.sh )
}
download_trusted_firmware_m
download_wolfssl_src
download_tfm_repo_test_src
compile_wolfssl
compile_tfm
echo "WolfSSL TF-M example built."
echo "To flash on the board run:"
echo "cd ${TRUSTED_FIRMWARE_DIR}/build && ./regression.sh && ./TFM_UPDATE.sh"
# flash_tfm