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