blob: b88a7ba8d42719588a568e672da2d6ef2952a917 [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 Setti4f4ade92024-05-03 17:28:04 +020012#include "psa/service.h"
Valerio Setti655b9792024-05-09 12:20:40 +020013#include "psa/error_ext.h"
Valerio Setti4f4ade92024-05-03 17:28:04 +020014#include "psa/util.h"
15#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
Valerio Setti4f4ade92024-05-03 17:28:04 +020022#define SERVER_PRINT(fmt, ...) \
23 PRINT("Server: " fmt, ##__VA_ARGS__)
24
25#define BUF_SIZE 25
26
27static int kill_on_disconnect = 0; /* Kill the server on client disconnection. */
28
29void parse_input_args(int argc, char *argv[])
30{
31 int opt;
32
33 while ((opt = getopt(argc, argv, "k")) != -1) {
34 switch (opt) {
35 case 'k':
36 kill_on_disconnect = 1;
37 break;
38 default:
39 fprintf(stderr, "Usage: %s [-k]\n", argv[0]);
40 exit(EXIT_FAILURE);
41 }
42 }
43}
44
Valerio Setti655b9792024-05-09 12:20:40 +020045int psa_server_main(int argc, char *argv[])
Valerio Setti4f4ade92024-05-03 17:28:04 +020046{
47 psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR;
48 psa_msg_t msg = { -1 };
Valerio Setti4f4ade92024-05-03 17:28:04 +020049 const int magic_num = 66;
50 int client_disconnected = 0;
Valerio Setti4362aae2024-05-09 09:15:39 +020051 char mbedtls_version[18];
52
53 mbedtls_version_get_string_full(mbedtls_version);
54 SERVER_PRINT("%s", mbedtls_version);
Valerio Setti4f4ade92024-05-03 17:28:04 +020055
56 parse_input_args(argc, argv);
57 SERVER_PRINT("Starting");
58
59 while (!(kill_on_disconnect && client_disconnected)) {
60 psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
61
62 if (signals > 0) {
63 SERVER_PRINT("Signals: 0x%08x", signals);
64 }
65
Valerio Setti655b9792024-05-09 12:20:40 +020066 if (signals & PSA_CRYPTO_SIGNAL) {
67 if (PSA_SUCCESS == psa_get(PSA_CRYPTO_SIGNAL, &msg)) {
68 SERVER_PRINT("handle: %d - rhandle: %p", msg.handle, (int *) msg.rhandle);
Valerio Setti4f4ade92024-05-03 17:28:04 +020069 switch (msg.type) {
70 case PSA_IPC_CONNECT:
71 SERVER_PRINT("Got a connection message");
72 psa_set_rhandle(msg.handle, (void *) &magic_num);
73 ret = PSA_SUCCESS;
74 break;
75 case PSA_IPC_DISCONNECT:
76 SERVER_PRINT("Got a disconnection message");
77 ret = PSA_SUCCESS;
78 client_disconnected = 1;
79 break;
Valerio Setti4f4ade92024-05-03 17:28:04 +020080 default:
81 SERVER_PRINT("Got an IPC call of type %d", msg.type);
Valerio Setti655b9792024-05-09 12:20:40 +020082 switch (msg.type) {
83 case PSA_CRYPTO_INIT:
84 ret = psa_crypto_init();
85 break;
86 default:
87 SERVER_PRINT("Unknown PSA function code");
88 break;
Valerio Setti4f4ade92024-05-03 17:28:04 +020089 }
Valerio Setti655b9792024-05-09 12:20:40 +020090 SERVER_PRINT("Internal function call returned %d", ret);
Valerio Setti4f4ade92024-05-03 17:28:04 +020091
92 if (msg.client_id > 0) {
93 psa_notify(msg.client_id);
94 } else {
95 SERVER_PRINT("Client is non-secure, so won't notify");
96 }
Valerio Setti4f4ade92024-05-03 17:28:04 +020097 }
98
99 psa_reply(msg.handle, ret);
100 } else {
101 SERVER_PRINT("Failed to retrieve message");
102 }
103 } else if (SIGSTP_SIG & signals) {
104 SERVER_PRINT("Recieved SIGSTP signal. Gonna EOI it.");
105 psa_eoi(SIGSTP_SIG);
106 } else if (SIGINT_SIG & signals) {
107 SERVER_PRINT("Handling interrupt!");
108 SERVER_PRINT("Gracefully quitting");
109 psa_panic();
110 } else {
111 SERVER_PRINT("No signal asserted");
112 }
113 }
114
115 return 0;
116}