1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3 * Copyright (c) 2017-2018, Intel Corporation
4 *
5 * All rights reserved.
6 ***********************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "tss2_tpm2_types.h"
12 #include "sapi-util.h"
13 #include "session-util.h"
14
15 static ENTITY *entities = NULL;
16
17 int
AddEntity(TPM2_HANDLE handle,TPM2B_AUTH * auth)18 AddEntity(TPM2_HANDLE handle, TPM2B_AUTH *auth)
19 {
20 ENTITY *e;
21
22 HASH_FIND_INT(entities, &handle, e);
23
24 if (!e) {
25 e = calloc(1, sizeof(*e));
26 if (!e)
27 return -1;
28
29 e->entityHandle = handle;
30 HASH_ADD_INT(entities, entityHandle, e);
31 }
32 CopySizedByteBuffer((TPM2B *)&e->entityAuth, (TPM2B *)auth);
33 return 0;
34 }
35
36 void
DeleteEntity(TPM2_HANDLE handle)37 DeleteEntity(TPM2_HANDLE handle)
38 {
39 ENTITY *e;
40
41 HASH_FIND_INT(entities, &handle, e);
42 if (!e)
43 return;
44
45 HASH_DEL(entities, e);
46 free(e);
47 }
48
49 int
GetEntityAuth(TPM2_HANDLE handle,TPM2B_AUTH * auth)50 GetEntityAuth(TPM2_HANDLE handle, TPM2B_AUTH *auth)
51 {
52 ENTITY *e;
53
54 HASH_FIND_INT(entities, &handle, e);
55 if (!e)
56 return -1;
57
58 CopySizedByteBuffer((TPM2B *)auth, (TPM2B *)&e->entityAuth);
59 return 0;
60 }
61
62 ENTITY *
GetEntity(TPM2_HANDLE handle)63 GetEntity(TPM2_HANDLE handle)
64 {
65 ENTITY *e;
66
67 HASH_FIND_INT(entities, &handle, e);
68 return e;
69 }
70