1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2018-2019, Fraunhofer SIT sponsored by Infineon Technologies AG
4  * All rights reserved.
5  ******************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #ifndef NO_DL
12 #include <dlfcn.h>
13 #endif /* NO_DL */
14 #include <stdlib.h>
15 
16 #include<unistd.h>
17 
18 #include "tss2_fapi.h"
19 #include "tss2_tctildr.h"
20 #include "fapi_int.h"
21 #include "fapi_util.h"
22 #include "tss2_esys.h"
23 #define LOGMODULE fapi
24 #include "util/log.h"
25 #include "util/aux_util.h"
26 
27 /** One-Call function for Fapi_Finalize
28  *
29  * Fapi_Finalize() finalizes a context by closing IPC/RPC connections and freeing
30  * its consumed memory.
31  *
32  * @param[in] context The FAPI_CONTEXT
33  */
34 void
Fapi_Finalize(FAPI_CONTEXT ** context)35 Fapi_Finalize(
36     FAPI_CONTEXT **context)
37 {
38     LOG_TRACE("called for context:%p", context);
39 
40     /* Check for NULL parameters */
41     if (!context || !*context) {
42         LOG_WARNING("Attempting to free NULL context");
43         return;
44     }
45 
46     LOG_DEBUG("called: context: %p, *context: %p", context,
47               (context != NULL) ? *context : NULL);
48 
49     /* Finalize the profiles module. */
50     ifapi_profiles_finalize(&(*context)->profiles);
51 
52     /* Finalize the TCTI and ESYS contexts. */
53     TSS2_TCTI_CONTEXT *tcti = NULL;
54 
55     if ((*context)->esys) {
56         Esys_GetTcti((*context)->esys, &tcti);
57         Esys_Finalize(&((*context)->esys));
58         if (tcti) {
59             LOG_TRACE("Finalizing TCTI");
60             Tss2_TctiLdr_Finalize(&tcti);
61         }
62     }
63 
64     /* Finalize the keystore module. */
65     ifapi_cleanup_ifapi_keystore(&(*context)->keystore);
66 
67     /* Finalize the policy module. */
68     SAFE_FREE((*context)->pstore.policydir);
69 
70     /* Finalize leftovers from provisioning. */
71     SAFE_FREE((*context)->cmd.Provision.root_crt);
72     SAFE_FREE((*context)->cmd.Provision.intermed_crt);
73     SAFE_FREE((*context)->cmd.Provision.pem_cert);
74 
75     /* Finalize the config module. */
76     SAFE_FREE((*context)->config.profile_dir);
77     SAFE_FREE((*context)->config.user_dir);
78     SAFE_FREE((*context)->config.keystore_dir);
79     SAFE_FREE((*context)->config.profile_name);
80     SAFE_FREE((*context)->config.tcti);
81     SAFE_FREE((*context)->config.log_dir);
82     SAFE_FREE((*context)->config.ek_cert_file);
83     SAFE_FREE((*context)->config.intel_cert_service);
84 
85     /* Finalize the eventlog module. */
86     SAFE_FREE((*context)->eventlog.log_dir);
87 
88     /* Finalize all remaining object of the context. */
89     ifapi_free_objects(*context);
90 
91     /* Free the context's memory. */
92     free(*context);
93     *context = NULL;
94 
95     LOG_DEBUG("finished");
96 }
97