Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 1 | /* 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 Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 11 | /* Includes from psasim */ |
Valerio Setti | 66fb1c1 | 2024-05-10 06:51:16 +0200 | [diff] [blame] | 12 | #include "service.h" |
| 13 | #include "error_ext.h" |
| 14 | #include "util.h" |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 15 | #include "psa_manifest/manifest.h" |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 16 | #include "psa_functions_codes.h" |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 17 | |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 18 | /* Includes from mbedtls */ |
Valerio Setti | 4362aae | 2024-05-09 09:15:39 +0200 | [diff] [blame] | 19 | #include "mbedtls/version.h" |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 20 | #include "psa/crypto.h" |
Valerio Setti | 4362aae | 2024-05-09 09:15:39 +0200 | [diff] [blame] | 21 | |
Tom Cosgrove | 9919543 | 2024-05-28 14:07:10 +0300 | [diff] [blame^] | 22 | #ifdef DEBUG |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 23 | #define SERVER_PRINT(fmt, ...) \ |
| 24 | PRINT("Server: " fmt, ##__VA_ARGS__) |
Tom Cosgrove | 9919543 | 2024-05-28 14:07:10 +0300 | [diff] [blame^] | 25 | #else |
| 26 | #define SERVER_PRINT(...) |
| 27 | #endif |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 28 | |
| 29 | #define BUF_SIZE 25 |
| 30 | |
| 31 | static int kill_on_disconnect = 0; /* Kill the server on client disconnection. */ |
| 32 | |
| 33 | void 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 Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 49 | int psa_server_main(int argc, char *argv[]) |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 50 | { |
| 51 | psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR; |
| 52 | psa_msg_t msg = { -1 }; |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 53 | const int magic_num = 66; |
| 54 | int client_disconnected = 0; |
Valerio Setti | 4362aae | 2024-05-09 09:15:39 +0200 | [diff] [blame] | 55 | char mbedtls_version[18]; |
| 56 | |
| 57 | mbedtls_version_get_string_full(mbedtls_version); |
| 58 | SERVER_PRINT("%s", mbedtls_version); |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 59 | |
| 60 | parse_input_args(argc, argv); |
| 61 | SERVER_PRINT("Starting"); |
| 62 | |
| 63 | while (!(kill_on_disconnect && client_disconnected)) { |
| 64 | psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK); |
| 65 | |
| 66 | if (signals > 0) { |
| 67 | SERVER_PRINT("Signals: 0x%08x", signals); |
| 68 | } |
| 69 | |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 70 | if (signals & PSA_CRYPTO_SIGNAL) { |
| 71 | if (PSA_SUCCESS == psa_get(PSA_CRYPTO_SIGNAL, &msg)) { |
| 72 | SERVER_PRINT("handle: %d - rhandle: %p", msg.handle, (int *) msg.rhandle); |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 73 | switch (msg.type) { |
| 74 | case PSA_IPC_CONNECT: |
| 75 | SERVER_PRINT("Got a connection message"); |
| 76 | psa_set_rhandle(msg.handle, (void *) &magic_num); |
| 77 | ret = PSA_SUCCESS; |
| 78 | break; |
| 79 | case PSA_IPC_DISCONNECT: |
| 80 | SERVER_PRINT("Got a disconnection message"); |
| 81 | ret = PSA_SUCCESS; |
| 82 | client_disconnected = 1; |
| 83 | break; |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 84 | default: |
| 85 | SERVER_PRINT("Got an IPC call of type %d", msg.type); |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 86 | switch (msg.type) { |
| 87 | case PSA_CRYPTO_INIT: |
| 88 | ret = psa_crypto_init(); |
| 89 | break; |
| 90 | default: |
| 91 | SERVER_PRINT("Unknown PSA function code"); |
| 92 | break; |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 93 | } |
Valerio Setti | 655b979 | 2024-05-09 12:20:40 +0200 | [diff] [blame] | 94 | SERVER_PRINT("Internal function call returned %d", ret); |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 95 | |
| 96 | if (msg.client_id > 0) { |
| 97 | psa_notify(msg.client_id); |
| 98 | } else { |
| 99 | SERVER_PRINT("Client is non-secure, so won't notify"); |
| 100 | } |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | psa_reply(msg.handle, ret); |
| 104 | } else { |
| 105 | SERVER_PRINT("Failed to retrieve message"); |
| 106 | } |
| 107 | } else if (SIGSTP_SIG & signals) { |
| 108 | SERVER_PRINT("Recieved SIGSTP signal. Gonna EOI it."); |
| 109 | psa_eoi(SIGSTP_SIG); |
| 110 | } else if (SIGINT_SIG & signals) { |
| 111 | SERVER_PRINT("Handling interrupt!"); |
| 112 | SERVER_PRINT("Gracefully quitting"); |
| 113 | psa_panic(); |
| 114 | } else { |
| 115 | SERVER_PRINT("No signal asserted"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |