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 | |
| 11 | #include "psa/service.h" |
| 12 | #include "psa/error.h" |
| 13 | #include "psa/util.h" |
| 14 | #include "psa_manifest/manifest.h" |
| 15 | |
Valerio Setti | 4362aae | 2024-05-09 09:15:39 +0200 | [diff] [blame^] | 16 | #include "mbedtls/version.h" |
| 17 | |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 18 | #define SERVER_PRINT(fmt, ...) \ |
| 19 | PRINT("Server: " fmt, ##__VA_ARGS__) |
| 20 | |
| 21 | #define BUF_SIZE 25 |
| 22 | |
| 23 | static int kill_on_disconnect = 0; /* Kill the server on client disconnection. */ |
| 24 | |
| 25 | void parse_input_args(int argc, char *argv[]) |
| 26 | { |
| 27 | int opt; |
| 28 | |
| 29 | while ((opt = getopt(argc, argv, "k")) != -1) { |
| 30 | switch (opt) { |
| 31 | case 'k': |
| 32 | kill_on_disconnect = 1; |
| 33 | break; |
| 34 | default: |
| 35 | fprintf(stderr, "Usage: %s [-k]\n", argv[0]); |
| 36 | exit(EXIT_FAILURE); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | int psa_sha256_main(int argc, char *argv[]) |
| 42 | { |
| 43 | psa_status_t ret = PSA_ERROR_PROGRAMMER_ERROR; |
| 44 | psa_msg_t msg = { -1 }; |
| 45 | char foo[BUF_SIZE] = { 0 }; |
| 46 | const int magic_num = 66; |
| 47 | int client_disconnected = 0; |
Valerio Setti | 4362aae | 2024-05-09 09:15:39 +0200 | [diff] [blame^] | 48 | char mbedtls_version[18]; |
| 49 | |
| 50 | mbedtls_version_get_string_full(mbedtls_version); |
| 51 | SERVER_PRINT("%s", mbedtls_version); |
Valerio Setti | 4f4ade9 | 2024-05-03 17:28:04 +0200 | [diff] [blame] | 52 | |
| 53 | parse_input_args(argc, argv); |
| 54 | SERVER_PRINT("Starting"); |
| 55 | |
| 56 | while (!(kill_on_disconnect && client_disconnected)) { |
| 57 | psa_signal_t signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK); |
| 58 | |
| 59 | if (signals > 0) { |
| 60 | SERVER_PRINT("Signals: 0x%08x", signals); |
| 61 | } |
| 62 | |
| 63 | if (signals & PSA_SHA256_SIGNAL) { |
| 64 | if (PSA_SUCCESS == psa_get(PSA_SHA256_SIGNAL, &msg)) { |
| 65 | SERVER_PRINT("My handle is %d", msg.handle); |
| 66 | SERVER_PRINT("My rhandle is %p", (int *) msg.rhandle); |
| 67 | switch (msg.type) { |
| 68 | case PSA_IPC_CONNECT: |
| 69 | SERVER_PRINT("Got a connection message"); |
| 70 | psa_set_rhandle(msg.handle, (void *) &magic_num); |
| 71 | ret = PSA_SUCCESS; |
| 72 | break; |
| 73 | case PSA_IPC_DISCONNECT: |
| 74 | SERVER_PRINT("Got a disconnection message"); |
| 75 | ret = PSA_SUCCESS; |
| 76 | client_disconnected = 1; |
| 77 | break; |
| 78 | |
| 79 | default: |
| 80 | SERVER_PRINT("Got an IPC call of type %d", msg.type); |
| 81 | ret = 42; |
| 82 | size_t size = msg.in_size[0]; |
| 83 | |
| 84 | if ((size > 0) && (size <= sizeof(foo))) { |
| 85 | psa_read(msg.handle, 0, foo, 6); |
| 86 | foo[(BUF_SIZE-1)] = '\0'; |
| 87 | SERVER_PRINT("Reading payload: %s", foo); |
| 88 | psa_read(msg.handle, 0, foo+6, 6); |
| 89 | foo[(BUF_SIZE-1)] = '\0'; |
| 90 | SERVER_PRINT("Reading payload: %s", foo); |
| 91 | } |
| 92 | |
| 93 | size = msg.out_size[0]; |
| 94 | if ((size > 0)) { |
| 95 | SERVER_PRINT("Writing response"); |
| 96 | psa_write(msg.handle, 0, "RESP", 4); |
| 97 | psa_write(msg.handle, 0, "ONSE", 4); |
| 98 | } |
| 99 | |
| 100 | if (msg.client_id > 0) { |
| 101 | psa_notify(msg.client_id); |
| 102 | } else { |
| 103 | SERVER_PRINT("Client is non-secure, so won't notify"); |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | psa_reply(msg.handle, ret); |
| 109 | } else { |
| 110 | SERVER_PRINT("Failed to retrieve message"); |
| 111 | } |
| 112 | } else if (SIGSTP_SIG & signals) { |
| 113 | SERVER_PRINT("Recieved SIGSTP signal. Gonna EOI it."); |
| 114 | psa_eoi(SIGSTP_SIG); |
| 115 | } else if (SIGINT_SIG & signals) { |
| 116 | SERVER_PRINT("Handling interrupt!"); |
| 117 | SERVER_PRINT("Gracefully quitting"); |
| 118 | psa_panic(); |
| 119 | } else { |
| 120 | SERVER_PRINT("No signal asserted"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |