Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <protocols/rpc/common/packed-c/encoding.h> |
| 8 | #include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h> |
| 9 | #include <service/secure_storage/frontend/psa/its/its_frontend.h> |
| 10 | #include <service_locator.h> |
| 11 | #include <stdio.h> |
| 12 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 13 | #include "libpsats.h" |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 14 | #include "trace.h" |
| 15 | |
| 16 | static struct rpc_caller_session *rpc_session; |
| 17 | static struct service_context *its_service_context; |
| 18 | static struct secure_storage_client its_storage_client; |
| 19 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 20 | LIBPSATS_EXPORTED psa_status_t libpsats_init_its_context(const char *service_name) |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 21 | { |
| 22 | psa_status_t result = PSA_ERROR_GENERIC_ERROR; |
| 23 | |
| 24 | if (rpc_session || its_service_context) { |
| 25 | EMSG("The client is already initialized\n"); |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | service_locator_init(); |
| 30 | |
| 31 | its_service_context = service_locator_query(service_name); |
| 32 | |
| 33 | if (!its_service_context) { |
| 34 | EMSG("Failed to discover service\n"); |
| 35 | return result; |
| 36 | } |
| 37 | |
| 38 | rpc_session = service_context_open(its_service_context); |
| 39 | |
| 40 | if (!rpc_session) { |
| 41 | EMSG("Failed to open rpc session\n"); |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 42 | libpsats_deinit_its_context(); |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 43 | return result; |
| 44 | } |
| 45 | |
| 46 | struct storage_backend *its_storage_backend = |
| 47 | secure_storage_client_init(&its_storage_client, rpc_session); |
| 48 | |
| 49 | if (!its_storage_backend) { |
| 50 | EMSG("Failed to initialize storage backend\n"); |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 51 | libpsats_deinit_its_context(); |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 52 | return result; |
| 53 | } |
| 54 | |
| 55 | result = psa_its_frontend_init(its_storage_backend); |
| 56 | |
| 57 | return result; |
| 58 | } |
| 59 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 60 | LIBPSATS_EXPORTED void libpsats_deinit_its_context(void) |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 61 | { |
| 62 | psa_its_frontend_init(NULL); |
| 63 | secure_storage_client_deinit(&its_storage_client); |
| 64 | |
| 65 | if (its_service_context && rpc_session) { |
| 66 | service_context_close(its_service_context, rpc_session); |
| 67 | rpc_session = NULL; |
| 68 | } |
| 69 | |
| 70 | if (its_service_context) { |
| 71 | service_context_relinquish(its_service_context); |
| 72 | its_service_context = NULL; |
| 73 | } |
| 74 | } |