blob: 77ce2694e333308cfb08f14980efabbee5e769b5 [file] [log] [blame]
Valerio Setti4f4ade92024-05-03 17:28:04 +02001/* psasim test server */
2
3/*
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include <unistd.h>
9#include <stdio.h>
10
Valerio Setti655b9792024-05-09 12:20:40 +020011/* Includes from psasim */
Valerio Setti66fb1c12024-05-10 06:51:16 +020012#include "service.h"
13#include "error_ext.h"
14#include "util.h"
Valerio Setti4f4ade92024-05-03 17:28:04 +020015#include "psa_manifest/manifest.h"
Valerio Setti655b9792024-05-09 12:20:40 +020016#include "psa_functions_codes.h"
Valerio Setti4f4ade92024-05-03 17:28:04 +020017
Valerio Setti655b9792024-05-09 12:20:40 +020018/* Includes from mbedtls */
Valerio Setti4362aae2024-05-09 09:15:39 +020019#include "mbedtls/version.h"
Valerio Setti655b9792024-05-09 12:20:40 +020020#include "psa/crypto.h"
Valerio Setti4362aae2024-05-09 09:15:39 +020021
Tom Cosgrove99195432024-05-28 14:07:10 +030022#ifdef DEBUG
Valerio Setti4f4ade92024-05-03 17:28:04 +020023#define SERVER_PRINT(fmt, ...) \
24 PRINT("Server: " fmt, ##__VA_ARGS__)
Tom Cosgrove99195432024-05-28 14:07:10 +030025#else
26#define SERVER_PRINT(...)
27#endif
Valerio Setti4f4ade92024-05-03 17:28:04 +020028
29#define BUF_SIZE 25
30
31static int kill_on_disconnect = 0; /* Kill the server on client disconnection. */
32
33void parse_input_args(int argc, char *argv[])
34{
35 int opt;
36
37 while ((opt = getopt(argc, argv, "k")) != -1) {
38 switch (opt) {
39 case 'k':
40 kill_on_disconnect = 1;
41 break;
42 default:
43 fprintf(stderr, "Usage: %s [-k]\n", argv[0]);
44 exit(EXIT_FAILURE);
45 }
46 }
47}
48
Valerio Setti655b9792024-05-09 12:20:40 +020049int psa_server_main(int argc, char *argv[])
Valerio Setti4f4ade92024-05-03 17:28:04 +020050{
51 psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR;
52 psa_msg_t msg = { -1 };
Valerio Setti4f4ade92024-05-03 17:28:04 +020053 const int magic_num = 66;
54 int client_disconnected = 0;
Valerio Setti4362aae2024-05-09 09:15:39 +020055 char mbedtls_version[18];
Tom Cosgrove3ebb8802024-05-29 10:29:39 +010056 extern psa_status_t psa_crypto_call(psa_msg_t msg);
Valerio Setti4362aae2024-05-09 09:15:39 +020057
58 mbedtls_version_get_string_full(mbedtls_version);
59 SERVER_PRINT("%s", mbedtls_version);
Valerio Setti4f4ade92024-05-03 17:28:04 +020060
61 parse_input_args(argc, argv);
62 SERVER_PRINT("Starting");
63
64 while (!(kill_on_disconnect && client_disconnected)) {
65 psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
66
67 if (signals > 0) {
68 SERVER_PRINT("Signals: 0x%08x", signals);
69 }
70
Valerio Setti655b9792024-05-09 12:20:40 +020071 if (signals & PSA_CRYPTO_SIGNAL) {
72 if (PSA_SUCCESS == psa_get(PSA_CRYPTO_SIGNAL, &msg)) {
73 SERVER_PRINT("handle: %d - rhandle: %p", msg.handle, (int *) msg.rhandle);
Valerio Setti4f4ade92024-05-03 17:28:04 +020074 switch (msg.type) {
75 case PSA_IPC_CONNECT:
76 SERVER_PRINT("Got a connection message");
77 psa_set_rhandle(msg.handle, (void *) &magic_num);
78 ret = PSA_SUCCESS;
79 break;
80 case PSA_IPC_DISCONNECT:
81 SERVER_PRINT("Got a disconnection message");
82 ret = PSA_SUCCESS;
83 client_disconnected = 1;
84 break;
Valerio Setti4f4ade92024-05-03 17:28:04 +020085 default:
86 SERVER_PRINT("Got an IPC call of type %d", msg.type);
Tom Cosgrove3ebb8802024-05-29 10:29:39 +010087 ret = psa_crypto_call(msg);
Valerio Setti655b9792024-05-09 12:20:40 +020088 SERVER_PRINT("Internal function call returned %d", ret);
Valerio Setti4f4ade92024-05-03 17:28:04 +020089
90 if (msg.client_id > 0) {
91 psa_notify(msg.client_id);
92 } else {
93 SERVER_PRINT("Client is non-secure, so won't notify");
94 }
Valerio Setti4f4ade92024-05-03 17:28:04 +020095 }
96
97 psa_reply(msg.handle, ret);
98 } else {
99 SERVER_PRINT("Failed to retrieve message");
100 }
101 } else if (SIGSTP_SIG & signals) {
102 SERVER_PRINT("Recieved SIGSTP signal. Gonna EOI it.");
103 psa_eoi(SIGSTP_SIG);
104 } else if (SIGINT_SIG & signals) {
105 SERVER_PRINT("Handling interrupt!");
106 SERVER_PRINT("Gracefully quitting");
107 psa_panic();
108 } else {
109 SERVER_PRINT("No signal asserted");
110 }
111 }
112
113 return 0;
114}