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 <psa/initial_attestation.h> |
| 9 | #include <service/attestation/client/provision/attest_provision_client.h> |
| 10 | #include <service/attestation/client/psa/iat_client.h> |
| 11 | #include <service_locator.h> |
| 12 | #include <stdio.h> |
| 13 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 14 | #include "libpsats.h" |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 15 | #include "trace.h" |
| 16 | |
| 17 | static struct rpc_caller_session *rpc_session; |
| 18 | static struct service_context *attestation_service_context; |
| 19 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 20 | LIBPSATS_EXPORTED psa_status_t libpsats_init_attestation_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 | psa_status_t provision_result = PSA_ERROR_GENERIC_ERROR; |
| 24 | |
| 25 | if (rpc_session || attestation_service_context) { |
| 26 | EMSG("The client is already initialized\n"); |
| 27 | return result; |
| 28 | } |
| 29 | |
| 30 | service_locator_init(); |
| 31 | |
| 32 | attestation_service_context = service_locator_query(service_name); |
| 33 | |
| 34 | if (!attestation_service_context) { |
| 35 | EMSG("Failed to discover service\n"); |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | rpc_session = service_context_open(attestation_service_context); |
| 40 | |
| 41 | if (!rpc_session) { |
| 42 | EMSG("Failed to open rpc session\n"); |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 43 | libpsats_deinit_attestation_context(); |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 44 | return result; |
| 45 | } |
| 46 | |
| 47 | result = psa_iat_client_init(rpc_session); |
| 48 | |
| 49 | if (result) { |
| 50 | EMSG("psa_iat_client_init failed\n"); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | provision_result = attest_provision_client_init(rpc_session); |
| 55 | |
| 56 | /* If external IAK is used this call can fail */ |
| 57 | if (provision_result) |
| 58 | EMSG( |
| 59 | "attest_provision_client_init failed. Are you using external IAK key?\n"); |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | |
Gabor Toth | ee2e7cb | 2024-10-07 17:02:56 +0200 | [diff] [blame^] | 64 | LIBPSATS_EXPORTED void libpsats_deinit_attestation_context(void) |
Gabor Toth | 172659d | 2023-04-27 08:51:21 +0200 | [diff] [blame] | 65 | { |
| 66 | psa_iat_client_deinit(); |
| 67 | attest_provision_client_deinit(); |
| 68 | |
| 69 | if (attestation_service_context && rpc_session) { |
| 70 | service_context_close(attestation_service_context, rpc_session); |
| 71 | rpc_session = NULL; |
| 72 | } |
| 73 | |
| 74 | if (attestation_service_context) { |
| 75 | service_context_relinquish(attestation_service_context); |
| 76 | attestation_service_context = NULL; |
| 77 | } |
| 78 | } |