• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
12 
13 #include "tpm_json_serialize.h"
14 #define LOGMODULE fapijson
15 #include "util/log.h"
16 #include "util/aux_util.h"
17 
18 #define CHECK_IN_LIST(type, needle, ...) \
19     type tab[] = { __VA_ARGS__ }; \
20     size_t i; \
21     for(i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) \
22         if (needle == tab[i]) \
23             break; \
24     if (i == sizeof(tab) / sizeof(tab[0])) { \
25         LOG_ERROR("Bad value"); \
26         return TSS2_FAPI_RC_BAD_VALUE; \
27     }
28 
29 /** Serialize a TPMS_EMPTY.
30  *
31  * @param[in] in not used.
32  * @param[in] jso not used.
33  * @retval TSS2_RC_SUCCESS is always returnde.
34  */
35 TSS2_RC
ifapi_json_TPMS_EMPTY_serialize(const TPMS_EMPTY * in,json_object ** jso)36 ifapi_json_TPMS_EMPTY_serialize(const TPMS_EMPTY *in, json_object **jso)
37 {
38     (void)(in);
39     (void)(jso);
40     return TSS2_RC_SUCCESS;
41 }
42 
43 /** Serialize a pcr selection to json
44  *
45  * @param[in]  sizeofSelect size of selection byte array.
46  * @param[in]  pcrSelect selection array.
47  * @param[out] jso pointer to the json object.
48  * @retval TSS2_RC_SUCCESS if the function call was a success.
49  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
50  * @retval TSS2_FAPI_RC_BAD_VALUE if sizeofSelect is too big.
51  */
52 TSS2_RC
ifapi_json_pcr_select_serialize(const UINT8 sizeofSelect,const BYTE pcrSelect[],json_object ** jso)53 ifapi_json_pcr_select_serialize(
54     const UINT8 sizeofSelect,
55     const BYTE pcrSelect[],
56     json_object **jso)
57 {
58     if (*jso == NULL) {
59         *jso = json_object_new_array();
60         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
61     }
62     if (sizeofSelect > TPM2_PCR_SELECT_MAX) {
63         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_PCR_SELECT_MAX)",
64                   (size_t)sizeofSelect, (size_t)TPM2_PCR_SELECT_MAX);
65         return TSS2_FAPI_RC_BAD_VALUE;
66     }
67     UINT32 i1, i2;
68     json_object *jso2;
69     for (i1 = 0; i1 < TPM2_PCR_LAST - TPM2_PCR_FIRST; i1++) {
70         i2 = i1 + TPM2_PCR_FIRST;
71         if (pcrSelect[i2 / 8] & (BYTE)(1 << (i2 % 8))) {
72             jso2 = json_object_new_int(i2);
73             return_if_null(jso2, "Out of memory.", TSS2_FAPI_RC_MEMORY);
74             json_object_array_add(*jso, jso2);
75         }
76     }
77     return TSS2_RC_SUCCESS;
78 }
79 
80 /** Serialize a TPMS_PCR_SELECT structure to json.
81  *
82  * @param[in] in value to be serialized.
83  * @param[out] jso pointer to the json object.
84  * @retval TSS2_RC_SUCCESS if the function call was a success.
85  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
86  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_PCR_SELECTION.
87  */
88 TSS2_RC
ifapi_json_TPMS_PCR_SELECT_serialize(const TPMS_PCR_SELECT * in,json_object ** jso)89 ifapi_json_TPMS_PCR_SELECT_serialize(const TPMS_PCR_SELECT *in,
90                                         json_object **jso)
91 {
92     TSS2_RC r;
93 
94     r = ifapi_json_pcr_select_serialize(in->sizeofSelect, &in->pcrSelect[0], jso);
95     return_if_error(r, "Serialize pcr selection");
96 
97     return TSS2_RC_SUCCESS;
98 }
99 
100 
101 /** Serialize a TPMS_PCR_SELECTION structure to json.
102  *
103  * @param[in] in value to be serialized.
104  * @param[out] jso pointer to the json object.
105  * @retval TSS2_RC_SUCCESS if the function call was a success.
106  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
107  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_PCR_SELECTION.
108  */
109 TSS2_RC
ifapi_json_TPMS_PCR_SELECTION_serialize(const TPMS_PCR_SELECTION * in,json_object ** jso)110 ifapi_json_TPMS_PCR_SELECTION_serialize(const TPMS_PCR_SELECTION *in,
111                                         json_object **jso)
112 {
113     if (*jso == NULL) {
114         *jso = json_object_new_object();
115         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
116     }
117     TSS2_RC r;
118     json_object *jso2 = NULL;
119     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hash, &jso2);
120     return_if_error(r, "Serialize pcr selection");
121 
122     json_object_object_add(*jso, "hash", jso2);
123     jso2 = NULL;
124     r = ifapi_json_pcr_select_serialize(in->sizeofSelect, &in->pcrSelect[0], &jso2);
125     return_if_error(r, "Serialize pcr selection");
126 
127     json_object_object_add(*jso, "pcrSelect", jso2);
128     return TSS2_RC_SUCCESS;
129 }
130 
131 /** Serialize a TPMS_TAGGED_PCR_SELECT structure to json.
132  *
133  * @param[in] in value to be serialized.
134  * @param[out] jso pointer to the json object.
135  * @retval TSS2_RC_SUCCESS if the function call was a success.
136  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
137  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_TAGGED_PCR_SELECT.
138  */
139 TSS2_RC
ifapi_json_TPMS_TAGGED_PCR_SELECT_serialize(const TPMS_TAGGED_PCR_SELECT * in,json_object ** jso)140 ifapi_json_TPMS_TAGGED_PCR_SELECT_serialize(const TPMS_TAGGED_PCR_SELECT *in,
141         json_object **jso)
142 {
143     TSS2_RC r;
144     if (*jso == NULL)
145         *jso = json_object_new_object();
146     json_object *jso2 = NULL;
147     r = ifapi_json_TPM2_PT_PCR_serialize(in->tag, &jso2);
148     return_if_error(r, "Serialize pcr selection");
149 
150     json_object_object_add(*jso, "tag", jso2);
151     jso2 = NULL;
152     r = ifapi_json_pcr_select_serialize(in->sizeofSelect, &in->pcrSelect[0], &jso2);
153     return_if_error(r, "Serialize pcr selection");
154 
155     json_object_object_add(*jso, "pcrSelect", jso2);
156     return TSS2_RC_SUCCESS;
157 }
158 
159 /** Serialize a base_type UINT16 to json.
160  *
161  * @param[in] in value to be serialized.
162  * @param[out] jso pointer to the json object.
163  * @retval TSS2_RC_SUCCESS if the function call was a success.
164  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
165  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type UINT16.
166  */
167 TSS2_RC
ifapi_json_UINT16_serialize(const UINT16 in,json_object ** jso)168 ifapi_json_UINT16_serialize(const UINT16 in, json_object **jso)
169 {
170     *jso = json_object_new_int64(in);
171     if (*jso == NULL) {
172         LOG_ERROR("Bad value %04"PRIx16"", in);
173         return TSS2_FAPI_RC_BAD_VALUE;
174     }
175     return TSS2_RC_SUCCESS;
176 }
177 
178 /** Serialize a base_type UINT32 to json.
179  *
180  * @param[in] in value to be serialized.
181  * @param[out] jso pointer to the json object.
182  * @retval TSS2_RC_SUCCESS if the function call was a success.
183  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
184  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type UINT32.
185  */
186 TSS2_RC
ifapi_json_UINT32_serialize(const UINT32 in,json_object ** jso)187 ifapi_json_UINT32_serialize(const UINT32 in, json_object **jso)
188 {
189     *jso = json_object_new_int64(in);
190     if (*jso == NULL) {
191         LOG_ERROR("Bad value %"PRIx32 "", in);
192         return TSS2_FAPI_RC_BAD_VALUE;
193     }
194     return TSS2_RC_SUCCESS;
195 }
196 
197 /** Serialize a base_type INT32 to json.
198  *
199  * @param[in] in value to be serialized.
200  * @param[out] jso pointer to the json object.
201  * @retval TSS2_RC_SUCCESS if the function call was a success.
202  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
203  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type INT32.
204  */
205 TSS2_RC
ifapi_json_INT32_serialize(const INT32 in,json_object ** jso)206 ifapi_json_INT32_serialize(const INT32 in, json_object **jso)
207 {
208     *jso = json_object_new_int64(in);
209     if (*jso == NULL) {
210         LOG_ERROR("Bad value %"PRIi32 "", in);
211         return TSS2_FAPI_RC_BAD_VALUE;
212     }
213     return TSS2_RC_SUCCESS;
214 }
215 
216 /** Serialize a base_type  UINT64 to json.
217  *
218  * @param[in] in variable to be serialized.
219  * @param[out] jso pointer to the json object.
220  * @retval TSS2_RC_SUCCESS if the function call was a success.
221  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
222  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type UINT64.
223  */
224 TSS2_RC
ifapi_json_UINT64_serialize(UINT64 in,json_object ** jso)225 ifapi_json_UINT64_serialize(UINT64 in, json_object **jso)
226 {
227     json_object *jso1 = NULL, *jso2 = NULL;
228     if (in < 0x1000000000000) {
229         *jso = json_object_new_int64(in);
230         if (*jso == NULL) {
231             LOG_ERROR("Bad value %"PRIu32 "", (uint32_t)in);
232             return TSS2_FAPI_RC_BAD_VALUE;
233         }
234         return TSS2_RC_SUCCESS;
235     }
236 
237     jso1 = json_object_new_int64(in / 0x100000000);
238     return_if_null(jso1, "Out of memory.", TSS2_FAPI_RC_MEMORY);
239 
240     in %= 0x100000000;
241 
242     jso2 = json_object_new_int64(in);
243     if (!jso2) json_object_put(jso1);
244     return_if_null(jso2, "Out of memory.", TSS2_FAPI_RC_MEMORY);
245 
246     *jso = json_object_new_array();
247     if (!*jso) json_object_put(jso1);
248     if (!*jso) json_object_put(jso2);
249     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
250 
251     json_object_array_add(*jso, jso1);
252     json_object_array_add(*jso, jso2);
253 
254     return TSS2_RC_SUCCESS;
255 }
256 
257 /** Serialize TPM2_GENERATED to json.
258  *
259  * @param[in] in constant to be serialized.
260  * @param[out] jso pointer to the json object.
261  * @retval TSS2_RC_SUCCESS if the function call was a success.
262  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
263  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_GENERATED.
264  */
265 TSS2_RC
ifapi_json_TPM2_GENERATED_serialize(const TPM2_GENERATED in,json_object ** jso)266 ifapi_json_TPM2_GENERATED_serialize(const TPM2_GENERATED in, json_object **jso)
267 {
268     if (in != TPM2_GENERATED_VALUE) {
269         return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
270     }
271 
272     *jso = json_object_new_string("VALUE");
273     check_oom(*jso);
274 
275     return TSS2_RC_SUCCESS;
276 }
277 
278 /** Serialize TPM2_ALG_ID to json.
279  *
280  * @param[in] in constant to be serialized.
281  * @param[out] jso pointer to the json object.
282  * @retval TSS2_RC_SUCCESS if the function call was a success.
283  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
284  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_ALG_ID.
285  */
286 TSS2_RC
ifapi_json_TPM2_ALG_ID_serialize(const TPM2_ALG_ID in,json_object ** jso)287 ifapi_json_TPM2_ALG_ID_serialize(const TPM2_ALG_ID in, json_object **jso)
288 {
289     static const struct { TPM2_ALG_ID in; const char *name; } tab[] = {
290         { TPM2_ALG_ERROR, "ERROR" },
291         { TPM2_ALG_RSA, "RSA" },
292         { TPM2_ALG_TDES, "TDES" },
293 /* We prefer SHA1 as output over SHA */
294 /*      { TPM2_ALG_SHA, "SHA" },*/
295         { TPM2_ALG_SHA1, "SHA1" },
296         { TPM2_ALG_CMAC, "CMAC" },
297         { TPM2_ALG_HMAC, "HMAC" },
298         { TPM2_ALG_AES, "AES" },
299         { TPM2_ALG_MGF1, "MGF1" },
300         { TPM2_ALG_KEYEDHASH, "KEYEDHASH" },
301         { TPM2_ALG_XOR, "XOR" },
302         { TPM2_ALG_SHA256, "SHA256" },
303         { TPM2_ALG_SHA384, "SHA384" },
304         { TPM2_ALG_SHA512, "SHA512" },
305         { TPM2_ALG_NULL, "NULL" },
306         { TPM2_ALG_SM3_256, "SM3_256" },
307         { TPM2_ALG_SM4, "SM4" },
308         { TPM2_ALG_RSASSA, "RSASSA" },
309         { TPM2_ALG_RSAES, "RSAES" },
310         { TPM2_ALG_RSAPSS, "RSAPSS" },
311         { TPM2_ALG_OAEP, "OAEP" },
312         { TPM2_ALG_ECDSA, "ECDSA" },
313         { TPM2_ALG_ECDH, "ECDH" },
314         { TPM2_ALG_ECDAA, "ECDAA" },
315         { TPM2_ALG_SM2, "SM2" },
316         { TPM2_ALG_ECSCHNORR, "ECSCHNORR" },
317         { TPM2_ALG_ECMQV, "ECMQV" },
318         { TPM2_ALG_KDF1_SP800_56A, "KDF1_SP800_56A" },
319         { TPM2_ALG_KDF2, "KDF2" },
320         { TPM2_ALG_KDF1_SP800_108, "KDF1_SP800_108" },
321         { TPM2_ALG_ECC, "ECC" },
322         { TPM2_ALG_SYMCIPHER, "SYMCIPHER" },
323         { TPM2_ALG_CAMELLIA, "CAMELLIA" },
324         { TPM2_ALG_CTR, "CTR" },
325         { TPM2_ALG_OFB, "OFB" },
326         { TPM2_ALG_CBC, "CBC" },
327         { TPM2_ALG_CFB, "CFB" },
328         { TPM2_ALG_ECB, "ECB" },
329     };
330 
331     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
332         if (tab[i].in == in) {
333             *jso = json_object_new_string(tab[i].name);
334             check_oom(*jso);
335             return TSS2_RC_SUCCESS;
336         }
337     }
338     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
339 }
340 
341 /** Serialize TPM2_ECC_CURVE to json.
342  *
343  * @param[in] in constant to be serialized.
344  * @param[out] jso pointer to the json object.
345  * @retval TSS2_RC_SUCCESS if the function call was a success.
346  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
347  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_ECC_CURVE.
348  */
349 TSS2_RC
ifapi_json_TPM2_ECC_CURVE_serialize(const TPM2_ECC_CURVE in,json_object ** jso)350 ifapi_json_TPM2_ECC_CURVE_serialize(const TPM2_ECC_CURVE in, json_object **jso)
351 {
352     static const struct { TPM2_ECC_CURVE in; char *name; } tab[] = {
353         { TPM2_ECC_NONE, "NONE" },
354         { TPM2_ECC_NIST_P192, "NIST_P192" },
355         { TPM2_ECC_NIST_P224, "NIST_P224" },
356         { TPM2_ECC_NIST_P256, "NIST_P256" },
357         { TPM2_ECC_NIST_P384, "NIST_P384" },
358         { TPM2_ECC_NIST_P521, "NIST_P521" },
359         { TPM2_ECC_BN_P256, "BN_P256" },
360         { TPM2_ECC_BN_P638, "BN_P638" },
361         { TPM2_ECC_SM2_P256, "SM2_P256" },
362     };
363 
364     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
365         if (tab[i].in == in) {
366             *jso = json_object_new_string(tab[i].name);
367             check_oom(*jso);
368             return TSS2_RC_SUCCESS;
369         }
370     }
371     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
372 }
373 
374 /** Serialize TPM2_CC to json.
375  *
376  * @param[in] in constant to be serialized.
377  * @param[out] jso pointer to the json object.
378  * @retval TSS2_RC_SUCCESS if the function call was a success.
379  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
380  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_CC.
381  */
382 TSS2_RC
ifapi_json_TPM2_CC_serialize(const TPM2_CC in,json_object ** jso)383 ifapi_json_TPM2_CC_serialize(const TPM2_CC in, json_object **jso)
384 {
385     static const struct { TPM2_CC in; char *name; } tab[] = {
386         /* We don't want to return FIRST but the actual value */
387         /* { TPM2_CC_FIRST, "FIRST" }, */
388         { TPM2_CC_NV_UndefineSpaceSpecial, "NV_UndefineSpaceSpecial" },
389         { TPM2_CC_EvictControl, "EvictControl" },
390         { TPM2_CC_HierarchyControl, "HierarchyControl" },
391         { TPM2_CC_NV_UndefineSpace, "NV_UndefineSpace" },
392         { TPM2_CC_ChangeEPS, "ChangeEPS" },
393         { TPM2_CC_ChangePPS, "ChangePPS" },
394         { TPM2_CC_Clear, "Clear" },
395         { TPM2_CC_ClearControl, "ClearControl" },
396         { TPM2_CC_ClockSet, "ClockSet" },
397         { TPM2_CC_HierarchyChangeAuth, "HierarchyChangeAuth" },
398         { TPM2_CC_NV_DefineSpace, "NV_DefineSpace" },
399         { TPM2_CC_PCR_Allocate, "PCR_Allocate" },
400         { TPM2_CC_PCR_SetAuthPolicy, "PCR_SetAuthPolicy" },
401         { TPM2_CC_PP_Commands, "PP_Commands" },
402         { TPM2_CC_SetPrimaryPolicy, "SetPrimaryPolicy" },
403         { TPM2_CC_FieldUpgradeStart, "FieldUpgradeStart" },
404         { TPM2_CC_ClockRateAdjust, "ClockRateAdjust" },
405         { TPM2_CC_CreatePrimary, "CreatePrimary" },
406         { TPM2_CC_NV_GlobalWriteLock, "NV_GlobalWriteLock" },
407         { TPM2_CC_GetCommandAuditDigest, "GetCommandAuditDigest" },
408         { TPM2_CC_NV_Increment, "NV_Increment" },
409         { TPM2_CC_NV_SetBits, "NV_SetBits" },
410         { TPM2_CC_NV_Extend, "NV_Extend" },
411         { TPM2_CC_NV_Write, "NV_Write" },
412         { TPM2_CC_NV_WriteLock, "NV_WriteLock" },
413         { TPM2_CC_DictionaryAttackLockReset, "DictionaryAttackLockReset" },
414         { TPM2_CC_DictionaryAttackParameters, "DictionaryAttackParameters" },
415         { TPM2_CC_NV_ChangeAuth, "NV_ChangeAuth" },
416         { TPM2_CC_PCR_Event, "PCR_Event" },
417         { TPM2_CC_PCR_Reset, "PCR_Reset" },
418         { TPM2_CC_SequenceComplete, "SequenceComplete" },
419         { TPM2_CC_SetAlgorithmSet, "SetAlgorithmSet" },
420         { TPM2_CC_SetCommandCodeAuditStatus, "SetCommandCodeAuditStatus" },
421         { TPM2_CC_FieldUpgradeData, "FieldUpgradeData" },
422         { TPM2_CC_IncrementalSelfTest, "IncrementalSelfTest" },
423         { TPM2_CC_SelfTest, "SelfTest" },
424         { TPM2_CC_Startup, "Startup" },
425         { TPM2_CC_Shutdown, "Shutdown" },
426         { TPM2_CC_StirRandom, "StirRandom" },
427         { TPM2_CC_ActivateCredential, "ActivateCredential" },
428         { TPM2_CC_Certify, "Certify" },
429         { TPM2_CC_PolicyNV, "PolicyNV" },
430         { TPM2_CC_CertifyCreation, "CertifyCreation" },
431         { TPM2_CC_Duplicate, "Duplicate" },
432         { TPM2_CC_GetTime, "GetTime" },
433         { TPM2_CC_GetSessionAuditDigest, "GetSessionAuditDigest" },
434         { TPM2_CC_NV_Read, "NV_Read" },
435         { TPM2_CC_NV_ReadLock, "NV_ReadLock" },
436         { TPM2_CC_ObjectChangeAuth, "ObjectChangeAuth" },
437         { TPM2_CC_PolicySecret, "PolicySecret" },
438         { TPM2_CC_Rewrap, "Rewrap" },
439         { TPM2_CC_Create, "Create" },
440         { TPM2_CC_ECDH_ZGen, "ECDH_ZGen" },
441         { TPM2_CC_HMAC, "HMAC" },
442         { TPM2_CC_Import, "Import" },
443         { TPM2_CC_Load, "Load" },
444         { TPM2_CC_Quote, "Quote" },
445         { TPM2_CC_RSA_Decrypt, "RSA_Decrypt" },
446         { TPM2_CC_HMAC_Start, "HMAC_Start" },
447         { TPM2_CC_SequenceUpdate, "SequenceUpdate" },
448         { TPM2_CC_Sign, "Sign" },
449         { TPM2_CC_Unseal, "Unseal" },
450         { TPM2_CC_PolicySigned, "PolicySigned" },
451         { TPM2_CC_ContextLoad, "ContextLoad" },
452         { TPM2_CC_ContextSave, "ContextSave" },
453         { TPM2_CC_ECDH_KeyGen, "ECDH_KeyGen" },
454         { TPM2_CC_EncryptDecrypt, "EncryptDecrypt" },
455         { TPM2_CC_FlushContext, "FlushContext" },
456         { TPM2_CC_LoadExternal, "LoadExternal" },
457         { TPM2_CC_MakeCredential, "MakeCredential" },
458         { TPM2_CC_NV_ReadPublic, "NV_ReadPublic" },
459         { TPM2_CC_PolicyAuthorize, "PolicyAuthorize" },
460         { TPM2_CC_PolicyAuthValue, "PolicyAuthValue" },
461         { TPM2_CC_PolicyCommandCode, "PolicyCommandCode" },
462         { TPM2_CC_PolicyCounterTimer, "PolicyCounterTimer" },
463         { TPM2_CC_PolicyCpHash, "PolicyCpHash" },
464         { TPM2_CC_PolicyLocality, "PolicyLocality" },
465         { TPM2_CC_PolicyNameHash, "PolicyNameHash" },
466         { TPM2_CC_PolicyOR, "PolicyOR" },
467         { TPM2_CC_PolicyTicket, "PolicyTicket" },
468         { TPM2_CC_ReadPublic, "ReadPublic" },
469         { TPM2_CC_RSA_Encrypt, "RSA_Encrypt" },
470         { TPM2_CC_StartAuthSession, "StartAuthSession" },
471         { TPM2_CC_VerifySignature, "VerifySignature" },
472         { TPM2_CC_ECC_Parameters, "ECC_Parameters" },
473         { TPM2_CC_FirmwareRead, "FirmwareRead" },
474         { TPM2_CC_GetCapability, "GetCapability" },
475         { TPM2_CC_GetRandom, "GetRandom" },
476         { TPM2_CC_GetTestResult, "GetTestResult" },
477         { TPM2_CC_Hash, "Hash" },
478         { TPM2_CC_PCR_Read, "PCR_Read" },
479         { TPM2_CC_PolicyPCR, "PolicyPCR" },
480         { TPM2_CC_PolicyRestart, "PolicyRestart" },
481         { TPM2_CC_ReadClock, "ReadClock" },
482         { TPM2_CC_PCR_Extend, "PCR_Extend" },
483         { TPM2_CC_PCR_SetAuthValue, "PCR_SetAuthValue" },
484         { TPM2_CC_NV_Certify, "NV_Certify" },
485         { TPM2_CC_EventSequenceComplete, "EventSequenceComplete" },
486         { TPM2_CC_HashSequenceStart, "HashSequenceStart" },
487         { TPM2_CC_PolicyPhysicalPresence, "PolicyPhysicalPresence" },
488         { TPM2_CC_PolicyDuplicationSelect, "PolicyDuplicationSelect" },
489         { TPM2_CC_PolicyGetDigest, "PolicyGetDigest" },
490         { TPM2_CC_TestParms, "TestParms" },
491         { TPM2_CC_Commit, "Commit" },
492         { TPM2_CC_PolicyPassword, "PolicyPassword" },
493         { TPM2_CC_ZGen_2Phase, "ZGen_2Phase" },
494         { TPM2_CC_EC_Ephemeral, "EC_Ephemeral" },
495         { TPM2_CC_PolicyNvWritten, "PolicyNvWritten" },
496         { TPM2_CC_PolicyTemplate, "PolicyTemplate" },
497         { TPM2_CC_CreateLoaded, "CreateLoaded" },
498         { TPM2_CC_PolicyAuthorizeNV, "PolicyAuthorizeNV" },
499         { TPM2_CC_EncryptDecrypt2, "EncryptDecrypt2" },
500         /* We don't want to return LAST but the actual value */
501         /* { TPM2_CC_LAST, "LAST" }, */
502         { TPM2_CC_Vendor_TCG_Test, "Vendor_TCG_Test" },
503     };
504 
505     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
506         if (tab[i].in == in) {
507             *jso = json_object_new_string(tab[i].name);
508             check_oom(*jso);
509             return TSS2_RC_SUCCESS;
510         }
511     }
512     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
513 }
514 
515 /** Serialize TPM2_EO to json.
516  *
517  * @param[in] in constant to be serialized.
518  * @param[out] jso pointer to the json object.
519  * @retval TSS2_RC_SUCCESS if the function call was a success.
520  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
521  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_EO.
522  */
523 TSS2_RC
ifapi_json_TPM2_EO_serialize(const TPM2_EO in,json_object ** jso)524 ifapi_json_TPM2_EO_serialize(const TPM2_EO in, json_object **jso)
525 {
526     static const struct { TPM2_EO in; char *name; } tab[] = {
527         { TPM2_EO_EQ,          "EQ" },
528         { TPM2_EO_NEQ,         "TPM2_EO_NEQ" },
529         { TPM2_EO_SIGNED_GT,   "SIGNED_GT" },
530         { TPM2_EO_UNSIGNED_GT, "UNSIGNED_GT" },
531         { TPM2_EO_SIGNED_LT,   "SIGNED_LT" },
532         { TPM2_EO_UNSIGNED_LT, "UNSIGNED_LT" },
533         { TPM2_EO_SIGNED_GE,   "SIGNED_GE" },
534         { TPM2_EO_UNSIGNED_GE, "UNSIGNED_GE" },
535         { TPM2_EO_SIGNED_LE,   "SIGNED_LE" },
536         { TPM2_EO_UNSIGNED_LE, "UNSIGNED_LE" },
537         { TPM2_EO_BITSET,      "BITSET" },
538         { TPM2_EO_BITCLEAR,    "BITCLEAR" },
539     };
540 
541     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
542         if (tab[i].in == in) {
543             *jso = json_object_new_string(tab[i].name);
544             check_oom(*jso);
545             return TSS2_RC_SUCCESS;
546         }
547     }
548     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
549 }
550 
551 /** Serialize TPM2_ST to json.
552  *
553  * @param[in] in constant to be serialized.
554  * @param[out] jso pointer to the json object.
555  * @retval TSS2_RC_SUCCESS if the function call was a success.
556  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
557  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_ST.
558  */
559 TSS2_RC
ifapi_json_TPM2_ST_serialize(const TPM2_ST in,json_object ** jso)560 ifapi_json_TPM2_ST_serialize(const TPM2_ST in, json_object **jso)
561 {
562     static const struct { TPM2_ST in; char *name; } tab[] = {
563         { TPM2_ST_RSP_COMMAND, "RSP_COMMAND" },
564         { TPM2_ST_NULL, "NULL" },
565         { TPM2_ST_NO_SESSIONS, "NO_SESSIONS" },
566         { TPM2_ST_SESSIONS, "SESSIONS" },
567         { TPM2_ST_ATTEST_NV, "ATTEST_NV" },
568         { TPM2_ST_ATTEST_COMMAND_AUDIT, "ATTEST_COMMAND_AUDIT" },
569         { TPM2_ST_ATTEST_SESSION_AUDIT, "ATTEST_SESSION_AUDIT" },
570         { TPM2_ST_ATTEST_CERTIFY, "ATTEST_CERTIFY" },
571         { TPM2_ST_ATTEST_QUOTE, "ATTEST_QUOTE" },
572         { TPM2_ST_ATTEST_TIME, "ATTEST_TIME" },
573         { TPM2_ST_ATTEST_CREATION, "ATTEST_CREATION" },
574         { TPM2_ST_CREATION, "CREATION" },
575         { TPM2_ST_VERIFIED, "VERIFIED" },
576         { TPM2_ST_AUTH_SECRET, "AUTH_SECRET" },
577         { TPM2_ST_HASHCHECK, "HASHCHECK" },
578         { TPM2_ST_AUTH_SIGNED, "AUTH_SIGNED" },
579         { TPM2_ST_FU_MANIFEST, "FU_MANIFEST" },
580     };
581 
582     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
583         if (tab[i].in == in) {
584             *jso = json_object_new_string(tab[i].name);
585             check_oom(*jso);
586             return TSS2_RC_SUCCESS;
587         }
588     }
589     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
590 }
591 
592 /** Serialize TPM2_CAP to json.
593  *
594  * @param[in] in constant to be serialized.
595  * @param[out] jso pointer to the json object.
596  * @retval TSS2_RC_SUCCESS if the function call was a success.
597  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
598  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_CAP.
599  */
600 TSS2_RC
ifapi_json_TPM2_CAP_serialize(const TPM2_CAP in,json_object ** jso)601 ifapi_json_TPM2_CAP_serialize(const TPM2_CAP in, json_object **jso)
602 {
603     static const struct { TPM2_CAP in; char *name; } tab[] = {
604         { TPM2_CAP_ALGS, "ALGS" },
605         { TPM2_CAP_HANDLES, "HANDLES" },
606         { TPM2_CAP_COMMANDS, "COMMANDS" },
607         { TPM2_CAP_PP_COMMANDS, "PP_COMMANDS" },
608         { TPM2_CAP_AUDIT_COMMANDS, "AUDIT_COMMANDS" },
609         { TPM2_CAP_PCRS, "PCRS" },
610         { TPM2_CAP_TPM_PROPERTIES, "TPM_PROPERTIES" },
611         { TPM2_CAP_PCR_PROPERTIES, "PCR_PROPERTIES" },
612         { TPM2_CAP_ECC_CURVES, "ECC_CURVES" },
613         { TPM2_CAP_LAST, "LAST" },
614         { TPM2_CAP_VENDOR_PROPERTY, "VENDOR_PROPERTY" },
615     };
616 
617     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
618         if (tab[i].in == in) {
619             *jso = json_object_new_string(tab[i].name);
620             check_oom(*jso);
621             return TSS2_RC_SUCCESS;
622         }
623     }
624     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
625 }
626 
627 /** Serialize TPM2_PT to json.
628  *
629  * @param[in] in constant to be serialized.
630  * @param[out] jso pointer to the json object.
631  * @retval TSS2_RC_SUCCESS if the function call was a success.
632  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
633  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_PT.
634  */
635 TSS2_RC
ifapi_json_TPM2_PT_serialize(const TPM2_PT in,json_object ** jso)636 ifapi_json_TPM2_PT_serialize(const TPM2_PT in, json_object **jso)
637 {
638     static const struct { TPM2_PT in; char *name; } tab[] = {
639         { TPM2_PT_NONE, "NONE" },
640         { TPM2_PT_GROUP, "GROUP" },
641         { TPM2_PT_FIXED, "FIXED" },
642         { TPM2_PT_FAMILY_INDICATOR, "FAMILY_INDICATOR" },
643         { TPM2_PT_LEVEL, "LEVEL" },
644         { TPM2_PT_REVISION, "REVISION" },
645         { TPM2_PT_DAY_OF_YEAR, "DAY_OF_YEAR" },
646         { TPM2_PT_YEAR, "YEAR" },
647         { TPM2_PT_MANUFACTURER, "MANUFACTURER" },
648         { TPM2_PT_VENDOR_STRING_1, "VENDOR_STRING_1" },
649         { TPM2_PT_VENDOR_STRING_2, "VENDOR_STRING_2" },
650         { TPM2_PT_VENDOR_STRING_3, "VENDOR_STRING_3" },
651         { TPM2_PT_VENDOR_STRING_4, "VENDOR_STRING_4" },
652         { TPM2_PT_VENDOR_TPM_TYPE, "VENDOR_TPM_TYPE" },
653         { TPM2_PT_FIRMWARE_VERSION_1, "FIRMWARE_VERSION_1" },
654         { TPM2_PT_FIRMWARE_VERSION_2, "FIRMWARE_VERSION_2" },
655         { TPM2_PT_INPUT_BUFFER, "INPUT_BUFFER" },
656         { TPM2_PT_HR_TRANSIENT_MIN, "HR_TRANSIENT_MIN" },
657         { TPM2_PT_HR_PERSISTENT_MIN, "HR_PERSISTENT_MIN" },
658         { TPM2_PT_HR_LOADED_MIN, "HR_LOADED_MIN" },
659         { TPM2_PT_ACTIVE_SESSIONS_MAX, "ACTIVE_SESSIONS_MAX" },
660         { TPM2_PT_PCR_COUNT, "PCR_COUNT" },
661         { TPM2_PT_PCR_SELECT_MIN, "PCR_SELECT_MIN" },
662         { TPM2_PT_CONTEXT_GAP_MAX, "CONTEXT_GAP_MAX" },
663         { TPM2_PT_NV_COUNTERS_MAX, "NV_COUNTERS_MAX" },
664         { TPM2_PT_NV_INDEX_MAX, "NV_INDEX_MAX" },
665         { TPM2_PT_MEMORY, "MEMORY" },
666         { TPM2_PT_CLOCK_UPDATE, "CLOCK_UPDATE" },
667         { TPM2_PT_CONTEXT_HASH, "CONTEXT_HASH" },
668         { TPM2_PT_CONTEXT_SYM, "CONTEXT_SYM" },
669         { TPM2_PT_CONTEXT_SYM_SIZE, "CONTEXT_SYM_SIZE" },
670         { TPM2_PT_ORDERLY_COUNT, "ORDERLY_COUNT" },
671         { TPM2_PT_MAX_COMMAND_SIZE, "MAX_COMMAND_SIZE" },
672         { TPM2_PT_MAX_RESPONSE_SIZE, "MAX_RESPONSE_SIZE" },
673         { TPM2_PT_MAX_DIGEST, "MAX_DIGEST" },
674         { TPM2_PT_MAX_OBJECT_CONTEXT, "MAX_OBJECT_CONTEXT" },
675         { TPM2_PT_MAX_SESSION_CONTEXT, "MAX_SESSION_CONTEXT" },
676         { TPM2_PT_PS_FAMILY_INDICATOR, "PS_FAMILY_INDICATOR" },
677         { TPM2_PT_PS_LEVEL, "PS_LEVEL" },
678         { TPM2_PT_PS_REVISION, "PS_REVISION" },
679         { TPM2_PT_PS_DAY_OF_YEAR, "PS_DAY_OF_YEAR" },
680         { TPM2_PT_PS_YEAR, "PS_YEAR" },
681         { TPM2_PT_SPLIT_MAX, "SPLIT_MAX" },
682         { TPM2_PT_TOTAL_COMMANDS, "TOTAL_COMMANDS" },
683         { TPM2_PT_LIBRARY_COMMANDS, "LIBRARY_COMMANDS" },
684         { TPM2_PT_VENDOR_COMMANDS, "VENDOR_COMMANDS" },
685         { TPM2_PT_NV_BUFFER_MAX, "NV_BUFFER_MAX" },
686         { TPM2_PT_MODES, "MODES" },
687         { TPM2_PT_MAX_CAP_BUFFER, "MAX_CAP_BUFFER" },
688         { TPM2_PT_VAR, "VAR" },
689         { TPM2_PT_PERMANENT, "PERMANENT" },
690         { TPM2_PT_STARTUP_CLEAR, "STARTUP_CLEAR" },
691         { TPM2_PT_HR_NV_INDEX, "HR_NV_INDEX" },
692         { TPM2_PT_HR_LOADED, "HR_LOADED" },
693         { TPM2_PT_HR_LOADED_AVAIL, "HR_LOADED_AVAIL" },
694         { TPM2_PT_HR_ACTIVE, "HR_ACTIVE" },
695         { TPM2_PT_HR_ACTIVE_AVAIL, "HR_ACTIVE_AVAIL" },
696         { TPM2_PT_HR_TRANSIENT_AVAIL, "HR_TRANSIENT_AVAIL" },
697         { TPM2_PT_HR_PERSISTENT, "HR_PERSISTENT" },
698         { TPM2_PT_HR_PERSISTENT_AVAIL, "HR_PERSISTENT_AVAIL" },
699         { TPM2_PT_NV_COUNTERS, "NV_COUNTERS" },
700         { TPM2_PT_NV_COUNTERS_AVAIL, "NV_COUNTERS_AVAIL" },
701         { TPM2_PT_ALGORITHM_SET, "ALGORITHM_SET" },
702         { TPM2_PT_LOADED_CURVES, "LOADED_CURVES" },
703         { TPM2_PT_LOCKOUT_COUNTER, "LOCKOUT_COUNTER" },
704         { TPM2_PT_MAX_AUTH_FAIL, "MAX_AUTH_FAIL" },
705         { TPM2_PT_LOCKOUT_INTERVAL, "LOCKOUT_INTERVAL" },
706         { TPM2_PT_LOCKOUT_RECOVERY, "LOCKOUT_RECOVERY" },
707         { TPM2_PT_NV_WRITE_RECOVERY, "NV_WRITE_RECOVERY" },
708         { TPM2_PT_AUDIT_COUNTER_0, "AUDIT_COUNTER_0" },
709         { TPM2_PT_AUDIT_COUNTER_1, "AUDIT_COUNTER_1" },
710     };
711 
712     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
713         if (tab[i].in == in) {
714             *jso = json_object_new_string(tab[i].name);
715             check_oom(*jso);
716             return TSS2_RC_SUCCESS;
717         }
718     }
719     return_error2(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant: %"PRIx32, in);
720 }
721 
722 /** Serialize TPM2_PT_PCR to json.
723  *
724  * @param[in] in constant to be serialized.
725  * @param[out] jso pointer to the json object.
726  * @retval TSS2_RC_SUCCESS if the function call was a success.
727  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
728  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_PT_PCR.
729  */
730 TSS2_RC
ifapi_json_TPM2_PT_PCR_serialize(const TPM2_PT_PCR in,json_object ** jso)731 ifapi_json_TPM2_PT_PCR_serialize(const TPM2_PT_PCR in, json_object **jso)
732 {
733     static const struct { TPM2_PT_PCR in; char *name; } tab[] = {
734         { TPM2_PT_PCR_SAVE, "SAVE" },
735         { TPM2_PT_PCR_EXTEND_L0, "EXTEND_L0" },
736         { TPM2_PT_PCR_RESET_L0, "RESET_L0" },
737         { TPM2_PT_PCR_EXTEND_L1, "EXTEND_L1" },
738         { TPM2_PT_PCR_RESET_L1, "RESET_L1" },
739         { TPM2_PT_PCR_EXTEND_L2, "EXTEND_L2" },
740         { TPM2_PT_PCR_RESET_L2, "RESET_L2" },
741         { TPM2_PT_PCR_EXTEND_L3, "EXTEND_L3" },
742         { TPM2_PT_PCR_RESET_L3, "RESET_L3" },
743         { TPM2_PT_PCR_EXTEND_L4, "EXTEND_L4" },
744         { TPM2_PT_PCR_RESET_L4, "RESET_L4" },
745         { TPM2_PT_PCR_NO_INCREMENT, "NO_INCREMENT" },
746         { TPM2_PT_PCR_DRTM_RESET, "DRTM_RESET" },
747         { TPM2_PT_PCR_POLICY, "POLICY" },
748         { TPM2_PT_PCR_AUTH, "AUTH" },
749     };
750 
751     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
752         if (tab[i].in == in) {
753             *jso = json_object_new_string(tab[i].name);
754             check_oom(*jso);
755             return TSS2_RC_SUCCESS;
756         }
757     }
758     return_error2(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant: %"PRIx32, in);
759 }
760 
761 /** Serialize value of type TPM2_HANDLE to json.
762  *
763  * @param[in] in value to be serialized.
764  * @param[out] jso pointer to the json object.
765  * @retval TSS2_RC_SUCCESS if the function call was a success.
766  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
767  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2_HANDLE.
768  */
769 TSS2_RC
ifapi_json_TPM2_HANDLE_serialize(const TPM2_HANDLE in,json_object ** jso)770 ifapi_json_TPM2_HANDLE_serialize(const TPM2_HANDLE in, json_object **jso)
771 {
772     *jso = json_object_new_int(in);
773     if (*jso == NULL) {
774         LOG_ERROR("Bad value %"PRIx32 "", in);
775         return TSS2_FAPI_RC_BAD_VALUE;
776     }
777     return TSS2_RC_SUCCESS;
778 }
779 /** Serialize a TPMA_ALGORITHM to json.
780  *
781  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
782  *
783  * @param[in] in value to be serialized.
784  * @param[out] jso pointer to the json object.
785  * @retval TSS2_RC_SUCCESS if the function call was a success.
786  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
787  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMA_ALGORITHM.
788  */
789 TSS2_RC
ifapi_json_TPMA_ALGORITHM_serialize(const TPMA_ALGORITHM in,json_object ** jso)790 ifapi_json_TPMA_ALGORITHM_serialize(const TPMA_ALGORITHM in, json_object **jso)
791 {
792     static const struct { TPMA_ALGORITHM in; char *name; } tab[] = {
793         { TPMA_ALGORITHM_ASYMMETRIC, "asymmetric" },
794         { TPMA_ALGORITHM_SYMMETRIC, "symmetric" },
795         { TPMA_ALGORITHM_HASH, "hash" },
796         { TPMA_ALGORITHM_OBJECT, "object" },
797         { TPMA_ALGORITHM_SIGNING, "signing" },
798         { TPMA_ALGORITHM_ENCRYPTING, "encrypting" },
799         { TPMA_ALGORITHM_METHOD, "method" },
800     };
801 
802     UINT32 input;
803     input = (UINT32) in;
804     json_object *jso_bit;
805     if (*jso == NULL) {
806         *jso = json_object_new_object();
807         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
808     }
809     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
810         if (tab[i].in & input)
811             jso_bit = json_object_new_int(1);
812         else
813             jso_bit = json_object_new_int(0);
814         return_if_null(jso_bit, "Out of memory.", TSS2_FAPI_RC_MEMORY);
815 
816         json_object_object_add(*jso, tab[i].name, jso_bit);
817     }
818     return TSS2_RC_SUCCESS;
819 }
820 
821 /** Serialize a TPMA_OBJECT to json.
822  *
823  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
824  *
825  * @param[in] in value to be serialized.
826  * @param[out] jso pointer to the json object.
827  * @retval TSS2_RC_SUCCESS if the function call was a success.
828  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
829  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMA_OBJECT.
830  */
831 TSS2_RC
ifapi_json_TPMA_OBJECT_serialize(const TPMA_OBJECT in,json_object ** jso)832 ifapi_json_TPMA_OBJECT_serialize(const TPMA_OBJECT in, json_object **jso)
833 {
834     static const struct { TPMA_OBJECT in; char *name; } tab[] = {
835         { TPMA_OBJECT_FIXEDTPM, "fixedTPM" },
836         { TPMA_OBJECT_STCLEAR, "stClear" },
837         { TPMA_OBJECT_FIXEDPARENT, "fixedParent" },
838         { TPMA_OBJECT_SENSITIVEDATAORIGIN, "sensitiveDataOrigin" },
839         { TPMA_OBJECT_USERWITHAUTH, "userWithAuth" },
840         { TPMA_OBJECT_ADMINWITHPOLICY, "adminWithPolicy" },
841         { TPMA_OBJECT_NODA, "noDA" },
842         { TPMA_OBJECT_ENCRYPTEDDUPLICATION, "encryptedDuplication" },
843         { TPMA_OBJECT_RESTRICTED, "restricted" },
844         { TPMA_OBJECT_DECRYPT, "decrypt" },
845         { TPMA_OBJECT_SIGN_ENCRYPT, "sign" },
846     };
847     UINT32 input;
848     input = (UINT32) in;
849     json_object *jso_bit;
850     if (*jso == NULL) {
851         *jso = json_object_new_object();
852         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
853     }
854     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
855         if (tab[i].in & input)
856             jso_bit = json_object_new_int(1);
857         else
858             jso_bit = json_object_new_int(0);
859         return_if_null(jso_bit, "Out of memory.", TSS2_FAPI_RC_MEMORY);
860 
861         json_object_object_add(*jso, tab[i].name, jso_bit);
862     }
863     return TSS2_RC_SUCCESS;
864 }
865 
866 /** Serialize a TPMA_LOCALITY to json.
867  *
868  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
869  *
870  * @param[in] in value to be serialized.
871  * @param[out] jso pointer to the json object.
872  * @retval TSS2_RC_SUCCESS if the function call was a success.
873  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
874  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMA_LOCALITY.
875  */
876 TSS2_RC
ifapi_json_TPMA_LOCALITY_serialize(const TPMA_LOCALITY in,json_object ** jso)877 ifapi_json_TPMA_LOCALITY_serialize(const TPMA_LOCALITY in, json_object **jso)
878 {
879     static const struct { TPMA_LOCALITY in; char *name; } tab[] = {
880         { TPMA_LOCALITY_TPM2_LOC_ZERO, "ZERO" },
881         { TPMA_LOCALITY_TPM2_LOC_ONE, "ONE" },
882         { TPMA_LOCALITY_TPM2_LOC_TWO, "TWO" },
883         { TPMA_LOCALITY_TPM2_LOC_THREE, "THREE" },
884         { TPMA_LOCALITY_TPM2_LOC_FOUR, "FOUR" },
885     };
886 
887     UINT8 input;
888     input = (UINT8) in;
889     json_object *jso_bit;
890     json_object *jso_bit_idx;
891     if (*jso == NULL) {
892         *jso = json_object_new_object();
893         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
894     }
895     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
896         if (tab[i].in & input)
897             jso_bit = json_object_new_int(1);
898         else
899             jso_bit = json_object_new_int(0);
900         return_if_null(jso_bit, "Out of memory.", TSS2_FAPI_RC_MEMORY);
901 
902         json_object_object_add(*jso, tab[i].name, jso_bit);
903     }
904     jso_bit_idx = json_object_new_int64((TPMA_LOCALITY_EXTENDED_MASK & input) >>
905                                          5);
906     return_if_null(jso_bit_idx, "Out of memory.", TSS2_FAPI_RC_MEMORY);
907 
908     json_object_object_add(*jso, "Extended", jso_bit_idx);
909 
910     return TSS2_RC_SUCCESS;
911 }
912 
913 /** Serialize a TPMA_CC to json.
914  *
915  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
916  *
917  * @param[in] in value to be serialized.
918  * @param[out] jso pointer to the json object.
919  * @retval TSS2_RC_SUCCESS if the function call was a success.
920  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
921  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMA_CC.
922  */
923 TSS2_RC
ifapi_json_TPMA_CC_serialize(const TPMA_CC in,json_object ** jso)924 ifapi_json_TPMA_CC_serialize(const TPMA_CC in, json_object **jso)
925 {
926     static const struct { TPMA_CC in; char *name; } tab[] = {
927         { TPMA_CC_NV, "nv" },
928         { TPMA_CC_EXTENSIVE, "extensive" },
929         { TPMA_CC_FLUSHED, "flushed" },
930         { TPMA_CC_RHANDLE, "rHandle" },
931         { TPMA_CC_V, "V" },
932     };
933     TPM2_CC input;
934     input = (TPM2_CC) in;
935     json_object *jso_bit;
936     json_object *jso_bit_idx;
937     if (*jso == NULL) {
938         *jso = json_object_new_object();
939         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
940     }
941     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
942         if (tab[i].in & input)
943             jso_bit = json_object_new_int(1);
944         else
945             jso_bit = json_object_new_int(0);
946         return_if_null(jso_bit, "Out of memory.", TSS2_FAPI_RC_MEMORY);
947 
948         json_object_object_add(*jso, tab[i].name, jso_bit);
949     }
950     jso_bit_idx = json_object_new_int64((TPMA_CC_COMMANDINDEX_MASK & input) >> 0);
951     return_if_null(jso_bit_idx, "Out of memory.", TSS2_FAPI_RC_MEMORY);
952 
953     json_object_object_add(*jso, "commandIndex", jso_bit_idx);
954 
955     jso_bit_idx = json_object_new_int64((TPMA_CC_CHANDLES_MASK & input) >> 25);
956     return_if_null(jso_bit_idx, "Out of memory.", TSS2_FAPI_RC_MEMORY);
957 
958     json_object_object_add(*jso, "cHandles", jso_bit_idx);
959 
960     jso_bit_idx = json_object_new_int64((TPMA_CC_RES_MASK & input) >> 30);
961     return_if_null(jso_bit_idx, "Out of memory.", TSS2_FAPI_RC_MEMORY);
962 
963     json_object_object_add(*jso, "Res", jso_bit_idx);
964 
965     return TSS2_RC_SUCCESS;
966 }
967 
968 /** Serialize TPMI_YES_NO to json.
969  *
970  * @param[in] in variable to be serialized.
971  * @param[out] jso pointer to the json object.
972  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
973  *         the function.
974  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
975  */
976 TSS2_RC
ifapi_json_TPMI_YES_NO_serialize(const TPMI_YES_NO in,json_object ** jso)977 ifapi_json_TPMI_YES_NO_serialize(const TPMI_YES_NO in, json_object **jso)
978 {
979     if (in == YES) {
980         *jso = json_object_new_string("YES");
981     } else if (in == NO) {
982         *jso = json_object_new_string("NO");
983     } else {
984         return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
985     }
986     check_oom(*jso);
987     return TSS2_RC_SUCCESS;
988 }
989 
990 /** Serialize TPMI_RH_HIERARCHY to json.
991  *
992  * @param[in] in variable to be serialized.
993  * @param[out] jso pointer to the json object.
994  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
995  *         the function.
996  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
997  */
998 TSS2_RC
ifapi_json_TPMI_RH_HIERARCHY_serialize(const TPMI_RH_HIERARCHY in,json_object ** jso)999 ifapi_json_TPMI_RH_HIERARCHY_serialize(const TPMI_RH_HIERARCHY in,
1000                                        json_object **jso)
1001 {
1002     static const struct { TPMI_RH_HIERARCHY in; char *name; } tab[] = {
1003         { TPM2_RH_OWNER, "OWNER" },
1004         { TPM2_RH_PLATFORM, "PLATFORM" },
1005         { TPM2_RH_ENDORSEMENT, "ENDORSEMENT" },
1006         { TPM2_RH_NULL, "NULL" },
1007     };
1008 
1009     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
1010         if (tab[i].in == in) {
1011             *jso = json_object_new_string(tab[i].name);
1012             check_oom(*jso);
1013             return TSS2_RC_SUCCESS;
1014         }
1015     }
1016     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
1017 }
1018 
1019 /** Serialize value of type TPMI_RH_NV_INDEX to json.
1020  *
1021  * @param[in] in value to be serialized.
1022  * @param[out] jso pointer to the json object.
1023  * @retval TSS2_RC_SUCCESS if the function call was a success.
1024  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1025  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMI_RH_NV_INDEX.
1026  *
1027  */
1028 TSS2_RC
ifapi_json_TPMI_RH_NV_INDEX_serialize(const TPMI_RH_NV_INDEX in,json_object ** jso)1029 ifapi_json_TPMI_RH_NV_INDEX_serialize(const TPMI_RH_NV_INDEX in,
1030                                       json_object **jso)
1031 {
1032     if (in >= TPM2_NV_INDEX_FIRST && in <= TPM2_NV_INDEX_LAST) {
1033         *jso = json_object_new_int64(in);
1034     } else {
1035         return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
1036     }
1037     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1038 
1039     return TSS2_RC_SUCCESS;
1040 }
1041 
1042 /** Serialize TPMI_ALG_HASH to json.
1043  *
1044  * @param[in] in variable to be serialized.
1045  * @param[out] jso pointer to the json object.
1046  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
1047  *         the function.
1048  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
1049  */
1050 TSS2_RC
ifapi_json_TPMI_ALG_HASH_serialize(const TPMI_ALG_HASH in,json_object ** jso)1051 ifapi_json_TPMI_ALG_HASH_serialize(const TPMI_ALG_HASH in, json_object **jso)
1052 {
1053     CHECK_IN_LIST(TPMI_ALG_HASH, in, TPM2_ALG_SHA1, TPM2_ALG_SHA256, TPM2_ALG_SHA384,
1054                       TPM2_ALG_SHA512, TPM2_ALG_NULL);
1055     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
1056 }
1057 
1058 /** Serialize TPMI_ALG_SYM_OBJECT to json.
1059  *
1060  * @param[in] in variable to be serialized.
1061  * @param[out] jso pointer to the json object.
1062  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
1063  *         the function.
1064  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
1065  */
1066 TSS2_RC
ifapi_json_TPMI_ALG_SYM_OBJECT_serialize(const TPMI_ALG_SYM_OBJECT in,json_object ** jso)1067 ifapi_json_TPMI_ALG_SYM_OBJECT_serialize(const TPMI_ALG_SYM_OBJECT in,
1068         json_object **jso)
1069 {
1070     CHECK_IN_LIST(TPMI_ALG_SYM_OBJECT, in, TPM2_ALG_AES, TPM2_ALG_NULL);
1071     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
1072 }
1073 
1074 /** Serialize TPMI_ALG_SYM_MODE to json.
1075  *
1076  * @param[in] in variable to be serialized.
1077  * @param[out] jso pointer to the json object.
1078  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
1079  *         the function.
1080  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
1081  */
1082 TSS2_RC
ifapi_json_TPMI_ALG_SYM_MODE_serialize(const TPMI_ALG_SYM_MODE in,json_object ** jso)1083 ifapi_json_TPMI_ALG_SYM_MODE_serialize(const TPMI_ALG_SYM_MODE in,
1084                                        json_object **jso)
1085 {
1086     CHECK_IN_LIST(TPMI_ALG_SYM_MODE, in, TPM2_ALG_CTR, TPM2_ALG_OFB,
1087         TPM2_ALG_CBC, TPM2_ALG_CFB, TPM2_ALG_ECB, TPM2_ALG_NULL);
1088     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
1089 }
1090 
1091 /** Serialize TPMI_ALG_KDF to json.
1092  *
1093  * @param[in] in variable to be serialized.
1094  * @param[out] jso pointer to the json object.
1095  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
1096  *         the function.
1097  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
1098  */
1099 TSS2_RC
ifapi_json_TPMI_ALG_KDF_serialize(const TPMI_ALG_KDF in,json_object ** jso)1100 ifapi_json_TPMI_ALG_KDF_serialize(const TPMI_ALG_KDF in, json_object **jso)
1101 {
1102     CHECK_IN_LIST(TPMI_ALG_KDF, in, TPM2_ALG_MGF1, TPM2_ALG_KDF1_SP800_56A,
1103         TPM2_ALG_KDF1_SP800_108, TPM2_ALG_NULL);
1104     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
1105 }
1106 
1107 /** Serialize TPMI_ALG_SIG_SCHEME to json.
1108  *
1109  * @param[in] in variable to be serialized.
1110  * @param[out] jso pointer to the json object.
1111  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
1112  *         the function.
1113  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
1114  */
1115 TSS2_RC
ifapi_json_TPMI_ALG_SIG_SCHEME_serialize(const TPMI_ALG_SIG_SCHEME in,json_object ** jso)1116 ifapi_json_TPMI_ALG_SIG_SCHEME_serialize(const TPMI_ALG_SIG_SCHEME in,
1117         json_object **jso)
1118 {
1119     CHECK_IN_LIST(TPMI_ALG_SIG_SCHEME, in, TPM2_ALG_RSASSA, TPM2_ALG_RSAPSS,
1120         TPM2_ALG_ECDSA, TPM2_ALG_ECDAA, TPM2_ALG_SM2, TPM2_ALG_ECSCHNORR,
1121         TPM2_ALG_HMAC, TPM2_ALG_NULL);
1122     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
1123 }
1124 
1125 /**  Serialize a TPMU_HA to json.
1126  *
1127  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
1128  * @param[in] in the value to be serialized.
1129  * @param[in] selector the type of the HA object.
1130  * @param[out] jso pointer to the json object.
1131  * @retval TSS2_RC_SUCCESS if the function call was a success.
1132  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1133  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_HA.
1134  */
1135 TSS2_RC
ifapi_json_TPMU_HA_serialize(const TPMU_HA * in,UINT32 selector,json_object ** jso)1136 ifapi_json_TPMU_HA_serialize(const TPMU_HA *in, UINT32 selector,
1137                              json_object **jso)
1138 {
1139     size_t size;
1140     const uint8_t *buffer;
1141 
1142     switch (selector) {
1143     case TPM2_ALG_SHA1:
1144         size = TPM2_SHA1_DIGEST_SIZE;
1145         buffer = &in->sha1[0];
1146         break;
1147     case TPM2_ALG_SHA256:
1148         size = TPM2_SHA256_DIGEST_SIZE;
1149         buffer = &in->sha256[0];
1150         break;
1151     case TPM2_ALG_SHA384:
1152         size = TPM2_SHA384_DIGEST_SIZE;
1153         buffer = &in->sha384[0];
1154         break;
1155     case TPM2_ALG_SHA512:
1156         size = TPM2_SHA512_DIGEST_SIZE;
1157         buffer = &in->sha512[0];
1158         break;
1159     default:
1160         LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
1161         return TSS2_FAPI_RC_BAD_VALUE;
1162     };
1163     char hex_string[(size) * 2 + 1];
1164     for (size_t i = 0, off = 0; i < size; i++, off += 2)
1165         sprintf(&hex_string[off], "%02x", buffer[i]);
1166     hex_string[(size) * 2] = '\0';
1167     *jso = json_object_new_string(hex_string);
1168     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1169 
1170     return TSS2_RC_SUCCESS;
1171 }
1172 
1173 /** Serialize value of type TPMT_HA to json.
1174  *
1175  * @param[in] in value to be serialized.
1176  * @param[out] jso pointer to the json object.
1177  * @retval TSS2_RC_SUCCESS if the function call was a success.
1178  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1179  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_HA.
1180  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1181  */
1182 TSS2_RC
ifapi_json_TPMT_HA_serialize(const TPMT_HA * in,json_object ** jso)1183 ifapi_json_TPMT_HA_serialize(const TPMT_HA *in, json_object **jso)
1184 {
1185     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1186 
1187     TSS2_RC r;
1188     json_object *jso2;
1189     if (*jso == NULL)
1190         *jso = json_object_new_object();
1191     jso2 = NULL;
1192     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hashAlg, &jso2);
1193     return_if_error(r, "Serialize TPMI_ALG_HASH");
1194 
1195     json_object_object_add(*jso, "hashAlg", jso2);
1196     if (in->hashAlg != TPM2_ALG_NULL) {
1197         json_object *jso2 = NULL;
1198         r = ifapi_json_TPMU_HA_serialize(&in->digest, in->hashAlg, &jso2);
1199         return_if_error(r, "Serialize TPMU_HA");
1200 
1201         json_object_object_add(*jso, "digest", jso2);
1202     }
1203     return TSS2_RC_SUCCESS;
1204 }
1205 
1206 /** Serialize value of type TPM2B_DIGEST to json.
1207  *
1208  * @param[in] in value to be serialized.
1209  * @param[out] jso pointer to the json object.
1210  * @retval TSS2_RC_SUCCESS if the function call was a success.
1211  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1212  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_DIGEST.
1213  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1214  */
1215 TSS2_RC
ifapi_json_TPM2B_DIGEST_serialize(const TPM2B_DIGEST * in,json_object ** jso)1216 ifapi_json_TPM2B_DIGEST_serialize(const TPM2B_DIGEST *in, json_object **jso)
1217 {
1218     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1219 
1220     if (in->size > sizeof(TPMU_HA)) {
1221         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = sizeof(TPMU_HA))",
1222                   (size_t)in->size, (size_t)sizeof(TPMU_HA));
1223         return TSS2_FAPI_RC_BAD_VALUE;
1224     }
1225     char hex_string[((size_t)in->size)*2+1];
1226 
1227     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
1228         sprintf(&hex_string[off], "%02x", in->buffer[i]);
1229     hex_string[(in->size)*2] = '\0';
1230     *jso = json_object_new_string (hex_string);
1231     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1232 
1233     return TSS2_RC_SUCCESS;
1234 }
1235 
1236 /** Serialize value of type TPM2B_DATA to json.
1237  *
1238  * @param[in] in value to be serialized.
1239  * @param[out] jso pointer to the json object.
1240  * @retval TSS2_RC_SUCCESS if the function call was a success.
1241  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1242  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_DATA.
1243  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1244  */
1245 TSS2_RC
ifapi_json_TPM2B_DATA_serialize(const TPM2B_DATA * in,json_object ** jso)1246 ifapi_json_TPM2B_DATA_serialize(const TPM2B_DATA *in, json_object **jso)
1247 {
1248     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1249 
1250     if (in->size > sizeof(TPMT_HA)) {
1251         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = sizeof(TPMT_HA))",
1252                   (size_t)in->size, (size_t)sizeof(TPMT_HA));
1253         return TSS2_FAPI_RC_BAD_VALUE;
1254     }
1255     char hex_string[sizeof(TPMT_HA)*2+1];
1256 
1257     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
1258         sprintf(&hex_string[off], "%02x", in->buffer[i]);
1259     hex_string[(in->size)*2] = '\0';
1260     *jso = json_object_new_string (hex_string);
1261     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1262 
1263     return TSS2_RC_SUCCESS;
1264 }
1265 
1266 /** Serialize a TPM2B_NONCE to json.
1267  *
1268  * @param[in] in value of type TPM2B_NONCE to be serialized.
1269  * @param[out] jso pointer to the json object.
1270  * @retval TSS2_RC_SUCCESS if the function call was a success.
1271  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1272  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_NONCE.
1273  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1274  */
1275 TSS2_RC
ifapi_json_TPM2B_NONCE_serialize(const TPM2B_NONCE * in,json_object ** jso)1276 ifapi_json_TPM2B_NONCE_serialize(const TPM2B_NONCE *in, json_object **jso)
1277 {
1278     return ifapi_json_TPM2B_DIGEST_serialize(in, jso);
1279 }
1280 
1281 /** Serialize a TPM2B_OPERAND to json.
1282  *
1283  * @param[in] in value of type TPM2B_OPERAND to be serialized.
1284  * @param[out] jso pointer to the json object.
1285  * @retval TSS2_RC_SUCCESS if the function call was a success.
1286  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1287  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_OPERAND.
1288  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1289  */
1290 TSS2_RC
ifapi_json_TPM2B_OPERAND_serialize(const TPM2B_OPERAND * in,json_object ** jso)1291 ifapi_json_TPM2B_OPERAND_serialize(const TPM2B_OPERAND *in, json_object **jso)
1292 {
1293     return ifapi_json_TPM2B_DIGEST_serialize(in, jso);
1294 }
1295 
1296 /** Serialize value of type TPM2B_EVENT to json.
1297  *
1298  * @param[in] in value to be serialized.
1299  * @param[out] jso pointer to the json object.
1300  * @retval TSS2_RC_SUCCESS if the function call was a success.
1301  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1302  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_EVENT.
1303  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1304  */
1305 TSS2_RC
ifapi_json_TPM2B_EVENT_serialize(const TPM2B_EVENT * in,json_object ** jso)1306 ifapi_json_TPM2B_EVENT_serialize(const TPM2B_EVENT *in, json_object **jso)
1307 {
1308     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1309 
1310     if (in->size > 1024) {
1311         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = 1024)",
1312                   (size_t)in->size, (size_t)1024);
1313         return TSS2_FAPI_RC_BAD_VALUE;
1314     }
1315     char hex_string[((size_t)in->size)*2+1];
1316 
1317     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
1318         sprintf(&hex_string[off], "%02x", in->buffer[i]);
1319     hex_string[(in->size)*2] = '\0';
1320     *jso = json_object_new_string (hex_string);
1321     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1322 
1323     return TSS2_RC_SUCCESS;
1324 }
1325 
1326 /** Serialize value of type TPM2B_MAX_NV_BUFFER to json.
1327  *
1328  * @param[in] in value to be serialized.
1329  * @param[out] jso pointer to the json object.
1330  * @retval TSS2_RC_SUCCESS if the function call was a success.
1331  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1332  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_MAX_NV_BUFFER.
1333  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1334  */
1335 TSS2_RC
ifapi_json_TPM2B_MAX_NV_BUFFER_serialize(const TPM2B_MAX_NV_BUFFER * in,json_object ** jso)1336 ifapi_json_TPM2B_MAX_NV_BUFFER_serialize(const TPM2B_MAX_NV_BUFFER *in, json_object **jso)
1337 {
1338     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1339 
1340     if (in->size > TPM2_MAX_NV_BUFFER_SIZE) {
1341         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_NV_BUFFER_SIZE)",
1342                   (size_t)in->size, (size_t)TPM2_MAX_NV_BUFFER_SIZE);
1343         return TSS2_FAPI_RC_BAD_VALUE;
1344     }
1345     char hex_string[((size_t)in->size)*2+1];
1346 
1347     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
1348         sprintf(&hex_string[off], "%02x", in->buffer[i]);
1349     hex_string[(in->size)*2] = '\0';
1350     *jso = json_object_new_string (hex_string);
1351     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1352 
1353     return TSS2_RC_SUCCESS;
1354 }
1355 
1356 /** Serialize value of type TPM2B_NAME to json.
1357  *
1358  * @param[in] in value to be serialized.
1359  * @param[out] jso pointer to the json object.
1360  * @retval TSS2_RC_SUCCESS if the function call was a success.
1361  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1362  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_NAME.
1363  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1364  */
1365 TSS2_RC
ifapi_json_TPM2B_NAME_serialize(const TPM2B_NAME * in,json_object ** jso)1366 ifapi_json_TPM2B_NAME_serialize(const TPM2B_NAME *in, json_object **jso)
1367 {
1368     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1369 
1370     if (in->size > sizeof(TPMU_NAME)) {
1371         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = sizeof(TPMU_NAME))",
1372                   (size_t)in->size, (size_t)sizeof(TPMU_NAME));
1373         return TSS2_FAPI_RC_BAD_VALUE;
1374     }
1375     char hex_string[((size_t)in->size)*2+1];
1376 
1377     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
1378         sprintf(&hex_string[off], "%02x", in->name[i]);
1379     hex_string[(in->size)*2] = '\0';
1380     *jso = json_object_new_string (hex_string);
1381     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1382 
1383     return TSS2_RC_SUCCESS;
1384 }
1385 
1386 /** Serialize value of type TPMT_TK_CREATION to json.
1387  *
1388  * @param[in] in value to be serialized.
1389  * @param[out] jso pointer to the json object.
1390  * @retval TSS2_RC_SUCCESS if the function call was a success.
1391  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1392  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_TK_CREATION.
1393  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1394  */
1395 TSS2_RC
ifapi_json_TPMT_TK_CREATION_serialize(const TPMT_TK_CREATION * in,json_object ** jso)1396 ifapi_json_TPMT_TK_CREATION_serialize(const TPMT_TK_CREATION *in, json_object **jso)
1397 {
1398     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1399 
1400     TSS2_RC r;
1401     json_object *jso2;
1402     if (*jso == NULL)
1403         *jso = json_object_new_object ();
1404     if (in != NULL && in->tag != TPM2_ST_CREATION) {
1405         LOG_ERROR("BAD VALUE %"PRIuPTR" != %"PRIuPTR,(size_t)in->tag,(size_t)TPM2_ST_CREATION);
1406         return TSS2_FAPI_RC_BAD_VALUE;
1407     }
1408     jso2 = NULL;
1409     r = ifapi_json_TPM2_ST_serialize(in->tag, &jso2);
1410     return_if_error(r, "Serialize TPM2_ST");
1411 
1412     json_object_object_add(*jso, "tag", jso2);
1413     jso2 = NULL;
1414     r = ifapi_json_TPMI_RH_HIERARCHY_serialize(in->hierarchy, &jso2);
1415     return_if_error(r, "Serialize TPMI_RH_HIERARCHY");
1416 
1417     json_object_object_add(*jso, "hierarchy", jso2);
1418     jso2 = NULL;
1419     r = ifapi_json_TPM2B_DIGEST_serialize(&in->digest, &jso2);
1420     return_if_error(r, "Serialize TPM2B_DIGEST");
1421 
1422     json_object_object_add(*jso, "digest", jso2);
1423     return TSS2_RC_SUCCESS;
1424 }
1425 
1426 /** Serialize value of type TPMS_ALG_PROPERTY to json.
1427  *
1428  * @param[in] in value to be serialized.
1429  * @param[out] jso pointer to the json object.
1430  * @retval TSS2_RC_SUCCESS if the function call was a success.
1431  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1432  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ALG_PROPERTY.
1433  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1434  */
1435 TSS2_RC
ifapi_json_TPMS_ALG_PROPERTY_serialize(const TPMS_ALG_PROPERTY * in,json_object ** jso)1436 ifapi_json_TPMS_ALG_PROPERTY_serialize(const TPMS_ALG_PROPERTY *in, json_object **jso)
1437 {
1438     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1439 
1440     TSS2_RC r;
1441     json_object *jso2;
1442     if (*jso == NULL)
1443         *jso = json_object_new_object ();
1444     jso2 = NULL;
1445     r = ifapi_json_TPM2_ALG_ID_serialize(in->alg, &jso2);
1446     return_if_error(r, "Serialize TPM2_ALG_ID");
1447 
1448     json_object_object_add(*jso, "alg", jso2);
1449     jso2 = NULL;
1450     r = ifapi_json_TPMA_ALGORITHM_serialize(in->algProperties, &jso2);
1451     return_if_error(r, "Serialize TPMA_ALGORITHM");
1452 
1453     json_object_object_add(*jso, "algProperties", jso2);
1454     return TSS2_RC_SUCCESS;
1455 }
1456 
1457 /** Serialize value of type TPMS_TAGGED_PROPERTY to json.
1458  *
1459  * @param[in] in value to be serialized.
1460  * @param[out] jso pointer to the json object.
1461  * @retval TSS2_RC_SUCCESS if the function call was a success.
1462  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1463  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_TAGGED_PROPERTY.
1464  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1465  */
1466 TSS2_RC
ifapi_json_TPMS_TAGGED_PROPERTY_serialize(const TPMS_TAGGED_PROPERTY * in,json_object ** jso)1467 ifapi_json_TPMS_TAGGED_PROPERTY_serialize(const TPMS_TAGGED_PROPERTY *in, json_object **jso)
1468 {
1469     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1470 
1471     TSS2_RC r;
1472     json_object *jso2;
1473     if (*jso == NULL)
1474         *jso = json_object_new_object ();
1475     jso2 = NULL;
1476     r = ifapi_json_TPM2_PT_serialize(in->property, &jso2);
1477     return_if_error(r, "Serialize TPM2_PT");
1478 
1479     json_object_object_add(*jso, "property", jso2);
1480     jso2 = NULL;
1481     r = ifapi_json_UINT32_serialize(in->value, &jso2);
1482     return_if_error(r, "Serialize UINT32");
1483 
1484     json_object_object_add(*jso, "value", jso2);
1485     return TSS2_RC_SUCCESS;
1486 }
1487 
1488 /** Serialize value of type TPML_CC to json.
1489  *
1490  * @param[in] in value to be serialized.
1491  * @param[out] jso pointer to the json object.
1492  * @retval TSS2_RC_SUCCESS if the function call was a success.
1493  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1494  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_CC.
1495  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1496  */
1497 TSS2_RC
ifapi_json_TPML_CC_serialize(const TPML_CC * in,json_object ** jso)1498 ifapi_json_TPML_CC_serialize(const TPML_CC *in, json_object **jso)
1499 {
1500     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1501 
1502     TSS2_RC r;
1503     if (in->count > TPM2_MAX_CAP_CC) {
1504         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_CAP_CC)",
1505                   (size_t)in->count, (size_t)TPM2_MAX_CAP_CC);
1506         return TSS2_FAPI_RC_BAD_VALUE;
1507     }
1508     *jso = json_object_new_array();
1509     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1510 
1511     for (size_t i=0; i < in->count; i++) {
1512         json_object *jso2 = NULL;
1513         r = ifapi_json_TPM2_CC_serialize (in->commandCodes[i], &jso2);
1514         return_if_error(r, "Serialize TPM2_CC");
1515 
1516         json_object_array_add(*jso, jso2);
1517     }
1518     return TSS2_RC_SUCCESS;
1519 }
1520 
1521 /** Serialize value of type TPML_CCA to json.
1522  *
1523  * @param[in] in value to be serialized.
1524  * @param[out] jso pointer to the json object.
1525  * @retval TSS2_RC_SUCCESS if the function call was a success.
1526  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1527  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_CCA.
1528  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1529  */
1530 TSS2_RC
ifapi_json_TPML_CCA_serialize(const TPML_CCA * in,json_object ** jso)1531 ifapi_json_TPML_CCA_serialize(const TPML_CCA *in, json_object **jso)
1532 {
1533     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1534 
1535     TSS2_RC r;
1536     if (in->count > TPM2_MAX_CAP_CC) {
1537         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_CAP_CC)",
1538                   (size_t)in->count, (size_t)TPM2_MAX_CAP_CC);
1539         return TSS2_FAPI_RC_BAD_VALUE;
1540     }
1541     *jso = json_object_new_array();
1542     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1543 
1544     for (size_t i=0; i < in->count; i++) {
1545         json_object *jso2 = NULL;
1546         r = ifapi_json_TPMA_CC_serialize (in->commandAttributes[i], &jso2);
1547         return_if_error(r, "Serialize TPMA_CC");
1548 
1549         json_object_array_add(*jso, jso2);
1550     }
1551     return TSS2_RC_SUCCESS;
1552 }
1553 
1554 /** Serialize value of type TPML_HANDLE to json.
1555  *
1556  * @param[in] in value to be serialized.
1557  * @param[out] jso pointer to the json object.
1558  * @retval TSS2_RC_SUCCESS if the function call was a success.
1559  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1560  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_HANDLE.
1561  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1562  */
1563 TSS2_RC
ifapi_json_TPML_HANDLE_serialize(const TPML_HANDLE * in,json_object ** jso)1564 ifapi_json_TPML_HANDLE_serialize(const TPML_HANDLE *in, json_object **jso)
1565 {
1566     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1567 
1568     TSS2_RC r;
1569     if (in->count > TPM2_MAX_CAP_HANDLES) {
1570         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_CAP_HANDLES)",
1571                   (size_t)in->count, (size_t)TPM2_MAX_CAP_HANDLES);
1572         return TSS2_FAPI_RC_BAD_VALUE;
1573     }
1574     *jso = json_object_new_array();
1575     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1576 
1577     for (size_t i=0; i < in->count; i++) {
1578         json_object *jso2 = NULL;
1579         r = ifapi_json_TPM2_HANDLE_serialize (in->handle[i], &jso2);
1580         return_if_error(r, "Serialize TPM2_HANDLE");
1581 
1582         json_object_array_add(*jso, jso2);
1583     }
1584     return TSS2_RC_SUCCESS;
1585 }
1586 
1587 /** Serialize value of type TPML_DIGEST_VALUES to json.
1588  *
1589  * @param[in] in value to be serialized.
1590  * @param[out] jso pointer to the json object.
1591  * @retval TSS2_RC_SUCCESS if the function call was a success.
1592  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1593  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_DIGEST_VALUES.
1594  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1595  */
1596 TSS2_RC
ifapi_json_TPML_DIGEST_VALUES_serialize(const TPML_DIGEST_VALUES * in,json_object ** jso)1597 ifapi_json_TPML_DIGEST_VALUES_serialize(const TPML_DIGEST_VALUES *in, json_object **jso)
1598 {
1599     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1600 
1601     TSS2_RC r;
1602     if (in->count > TPM2_NUM_PCR_BANKS) {
1603         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_NUM_PCR_BANKS)",
1604                   (size_t)in->count, (size_t)TPM2_NUM_PCR_BANKS);
1605         return TSS2_FAPI_RC_BAD_VALUE;
1606     }
1607     *jso = json_object_new_array();
1608     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1609 
1610     for (size_t i=0; i < in->count; i++) {
1611         json_object *jso2 = NULL;
1612         r = ifapi_json_TPMT_HA_serialize (&in->digests[i], &jso2);
1613         return_if_error(r, "Serialize TPMT_HA");
1614 
1615         json_object_array_add(*jso, jso2);
1616     }
1617     return TSS2_RC_SUCCESS;
1618 }
1619 
1620 /** Serialize value of type TPML_PCR_SELECTION to json.
1621  *
1622  * @param[in] in value to be serialized.
1623  * @param[out] jso pointer to the json object.
1624  * @retval TSS2_RC_SUCCESS if the function call was a success.
1625  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1626  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_PCR_SELECTION.
1627  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1628  */
1629 TSS2_RC
ifapi_json_TPML_PCR_SELECTION_serialize(const TPML_PCR_SELECTION * in,json_object ** jso)1630 ifapi_json_TPML_PCR_SELECTION_serialize(const TPML_PCR_SELECTION *in, json_object **jso)
1631 {
1632     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1633 
1634     TSS2_RC r;
1635     if (in->count > TPM2_NUM_PCR_BANKS) {
1636         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_NUM_PCR_BANKS)",
1637                   (size_t)in->count, (size_t)TPM2_NUM_PCR_BANKS);
1638         return TSS2_FAPI_RC_BAD_VALUE;
1639     }
1640     *jso = json_object_new_array();
1641     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1642 
1643     for (size_t i=0; i < in->count; i++) {
1644         json_object *jso2 = NULL;
1645         r = ifapi_json_TPMS_PCR_SELECTION_serialize (&in->pcrSelections[i], &jso2);
1646         return_if_error(r, "Serialize TPMS_PCR_SELECTION");
1647 
1648         json_object_array_add(*jso, jso2);
1649     }
1650     return TSS2_RC_SUCCESS;
1651 }
1652 
1653 /** Serialize value of type TPML_ALG_PROPERTY to json.
1654  *
1655  * @param[in] in value to be serialized.
1656  * @param[out] jso pointer to the json object.
1657  * @retval TSS2_RC_SUCCESS if the function call was a success.
1658  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1659  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_ALG_PROPERTY.
1660  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1661  */
1662 TSS2_RC
ifapi_json_TPML_ALG_PROPERTY_serialize(const TPML_ALG_PROPERTY * in,json_object ** jso)1663 ifapi_json_TPML_ALG_PROPERTY_serialize(const TPML_ALG_PROPERTY *in, json_object **jso)
1664 {
1665     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1666 
1667     TSS2_RC r;
1668     if (in->count > TPM2_MAX_CAP_ALGS) {
1669         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_CAP_ALGS)",
1670                   (size_t)in->count, (size_t)TPM2_MAX_CAP_ALGS);
1671         return TSS2_FAPI_RC_BAD_VALUE;
1672     }
1673     *jso = json_object_new_array();
1674     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1675 
1676     for (size_t i=0; i < in->count; i++) {
1677         json_object *jso2 = NULL;
1678         r = ifapi_json_TPMS_ALG_PROPERTY_serialize (&in->algProperties[i], &jso2);
1679         return_if_error(r, "Serialize TPMS_ALG_PROPERTY");
1680 
1681         json_object_array_add(*jso, jso2);
1682     }
1683     return TSS2_RC_SUCCESS;
1684 }
1685 
1686 /** Serialize value of type TPML_TAGGED_TPM_PROPERTY to json.
1687  *
1688  * @param[in] in value to be serialized.
1689  * @param[out] jso pointer to the json object.
1690  * @retval TSS2_RC_SUCCESS if the function call was a success.
1691  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1692  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_TAGGED_TPM_PROPERTY.
1693  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1694  */
1695 TSS2_RC
ifapi_json_TPML_TAGGED_TPM_PROPERTY_serialize(const TPML_TAGGED_TPM_PROPERTY * in,json_object ** jso)1696 ifapi_json_TPML_TAGGED_TPM_PROPERTY_serialize(const TPML_TAGGED_TPM_PROPERTY *in, json_object **jso)
1697 {
1698     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1699 
1700     TSS2_RC r;
1701     if (in->count > TPM2_MAX_TPM_PROPERTIES) {
1702         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_TPM_PROPERTIES)",
1703                   (size_t)in->count, (size_t)TPM2_MAX_TPM_PROPERTIES);
1704         return TSS2_FAPI_RC_BAD_VALUE;
1705     }
1706     *jso = json_object_new_array();
1707     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1708 
1709     for (size_t i=0; i < in->count; i++) {
1710         json_object *jso2 = NULL;
1711         r = ifapi_json_TPMS_TAGGED_PROPERTY_serialize (&in->tpmProperty[i], &jso2);
1712         return_if_error(r, "Serialize TPMS_TAGGED_PROPERTY");
1713 
1714         json_object_array_add(*jso, jso2);
1715     }
1716     return TSS2_RC_SUCCESS;
1717 }
1718 
1719 /** Serialize value of type TPML_TAGGED_PCR_PROPERTY to json.
1720  *
1721  * @param[in] in value to be serialized.
1722  * @param[out] jso pointer to the json object.
1723  * @retval TSS2_RC_SUCCESS if the function call was a success.
1724  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1725  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_TAGGED_PCR_PROPERTY.
1726  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1727  */
1728 TSS2_RC
ifapi_json_TPML_TAGGED_PCR_PROPERTY_serialize(const TPML_TAGGED_PCR_PROPERTY * in,json_object ** jso)1729 ifapi_json_TPML_TAGGED_PCR_PROPERTY_serialize(const TPML_TAGGED_PCR_PROPERTY *in, json_object **jso)
1730 {
1731     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1732 
1733     TSS2_RC r;
1734     if (in->count > TPM2_MAX_PCR_PROPERTIES) {
1735         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_PCR_PROPERTIES)",
1736                   (size_t)in->count, (size_t)TPM2_MAX_PCR_PROPERTIES);
1737         return TSS2_FAPI_RC_BAD_VALUE;
1738     }
1739     *jso = json_object_new_array();
1740     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1741 
1742     for (size_t i=0; i < in->count; i++) {
1743         json_object *jso2 = NULL;
1744         r = ifapi_json_TPMS_TAGGED_PCR_SELECT_serialize (&in->pcrProperty[i], &jso2);
1745         return_if_error(r, "Serialize TPMS_TAGGED_PCR_SELECT");
1746 
1747         json_object_array_add(*jso, jso2);
1748     }
1749     return TSS2_RC_SUCCESS;
1750 }
1751 
1752 /** Serialize value of type TPML_ECC_CURVE to json.
1753  *
1754  * @param[in] in value to be serialized.
1755  * @param[out] jso pointer to the json object.
1756  * @retval TSS2_RC_SUCCESS if the function call was a success.
1757  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1758  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_ECC_CURVE.
1759  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1760  */
1761 TSS2_RC
ifapi_json_TPML_ECC_CURVE_serialize(const TPML_ECC_CURVE * in,json_object ** jso)1762 ifapi_json_TPML_ECC_CURVE_serialize(const TPML_ECC_CURVE *in, json_object **jso)
1763 {
1764     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1765 
1766     TSS2_RC r;
1767     if (in->count > TPM2_MAX_ECC_CURVES) {
1768         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_ECC_CURVES)",
1769                   (size_t)in->count, (size_t)TPM2_MAX_ECC_CURVES);
1770         return TSS2_FAPI_RC_BAD_VALUE;
1771     }
1772     *jso = json_object_new_array();
1773     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1774 
1775     for (size_t i=0; i < in->count; i++) {
1776         json_object *jso2 = NULL;
1777         r = ifapi_json_TPM2_ECC_CURVE_serialize (in->eccCurves[i], &jso2);
1778         return_if_error(r, "Serialize TPM2_ECC_CURVE");
1779 
1780         json_object_array_add(*jso, jso2);
1781     }
1782     return TSS2_RC_SUCCESS;
1783 }
1784 
1785 /**  Serialize a TPMU_CAPABILITIES to json.
1786  *
1787  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
1788  * @param[in] in the value to be serialized.
1789  * @param[in] selector the type of the capabilities.
1790  * @param[out] jso pointer to the json object.
1791  * @retval TSS2_RC_SUCCESS if the function call was a success.
1792  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1793  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_CAPABILITIES.
1794  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1795  */
1796 TSS2_RC
ifapi_json_TPMU_CAPABILITIES_serialize(const TPMU_CAPABILITIES * in,UINT32 selector,json_object ** jso)1797 ifapi_json_TPMU_CAPABILITIES_serialize(const TPMU_CAPABILITIES *in, UINT32 selector, json_object **jso)
1798 {
1799     switch (selector) {
1800         case TPM2_CAP_ALGS:
1801             return ifapi_json_TPML_ALG_PROPERTY_serialize(&in->algorithms, jso);
1802         case TPM2_CAP_HANDLES:
1803             return ifapi_json_TPML_HANDLE_serialize(&in->handles, jso);
1804         case TPM2_CAP_COMMANDS:
1805             return ifapi_json_TPML_CCA_serialize(&in->command, jso);
1806         case TPM2_CAP_PP_COMMANDS:
1807             return ifapi_json_TPML_CC_serialize(&in->ppCommands, jso);
1808         case TPM2_CAP_AUDIT_COMMANDS:
1809             return ifapi_json_TPML_CC_serialize(&in->auditCommands, jso);
1810         case TPM2_CAP_PCRS:
1811             return ifapi_json_TPML_PCR_SELECTION_serialize(&in->assignedPCR, jso);
1812         case TPM2_CAP_TPM_PROPERTIES:
1813             return ifapi_json_TPML_TAGGED_TPM_PROPERTY_serialize(&in->tpmProperties, jso);
1814         case TPM2_CAP_PCR_PROPERTIES:
1815             return ifapi_json_TPML_TAGGED_PCR_PROPERTY_serialize(&in->pcrProperties, jso);
1816         case TPM2_CAP_ECC_CURVES:
1817             return ifapi_json_TPML_ECC_CURVE_serialize(&in->eccCurves, jso);
1818         default:
1819             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
1820             return TSS2_FAPI_RC_BAD_VALUE;
1821     };
1822     return TSS2_RC_SUCCESS;
1823 }
1824 
1825 /** Serialize value of type TPMS_CAPABILITY_DATA to json.
1826  *
1827  * @param[in] in value to be serialized.
1828  * @param[out] jso pointer to the json object.
1829  * @retval TSS2_RC_SUCCESS if the function call was a success.
1830  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1831  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_CAPABILITY_DATA.
1832  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1833  */
1834 TSS2_RC
ifapi_json_TPMS_CAPABILITY_DATA_serialize(const TPMS_CAPABILITY_DATA * in,json_object ** jso)1835 ifapi_json_TPMS_CAPABILITY_DATA_serialize(const TPMS_CAPABILITY_DATA *in, json_object **jso)
1836 {
1837     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1838 
1839     TSS2_RC r;
1840     json_object *jso2;
1841     if (*jso == NULL)
1842         *jso = json_object_new_object ();
1843     jso2 = NULL;
1844     r = ifapi_json_TPM2_CAP_serialize(in->capability, &jso2);
1845     return_if_error(r, "Serialize TPM2_CAP");
1846 
1847     json_object_object_add(*jso, "capability", jso2);
1848     jso2 = NULL;
1849     r = ifapi_json_TPMU_CAPABILITIES_serialize(&in->data, in->capability, &jso2);
1850     return_if_error(r,"Serialize TPMU_CAPABILITIES");
1851 
1852     json_object_object_add(*jso, "data", jso2);
1853     return TSS2_RC_SUCCESS;
1854 }
1855 
1856 /** Serialize value of type TPMS_CLOCK_INFO to json.
1857  *
1858  * @param[in] in value to be serialized.
1859  * @param[out] jso pointer to the json object.
1860  * @retval TSS2_RC_SUCCESS if the function call was a success.
1861  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1862  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_CLOCK_INFO.
1863  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1864  */
1865 TSS2_RC
ifapi_json_TPMS_CLOCK_INFO_serialize(const TPMS_CLOCK_INFO * in,json_object ** jso)1866 ifapi_json_TPMS_CLOCK_INFO_serialize(const TPMS_CLOCK_INFO *in, json_object **jso)
1867 {
1868     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1869 
1870     TSS2_RC r;
1871     json_object *jso2;
1872     if (*jso == NULL)
1873         *jso = json_object_new_object ();
1874     jso2 = NULL;
1875     r = ifapi_json_UINT64_serialize(in->clock, &jso2);
1876     return_if_error(r, "Serialize UINT64");
1877 
1878     json_object_object_add(*jso, "clock", jso2);
1879     jso2 = NULL;
1880     r = ifapi_json_UINT32_serialize(in->resetCount, &jso2);
1881     return_if_error(r, "Serialize UINT32");
1882 
1883     json_object_object_add(*jso, "resetCount", jso2);
1884     jso2 = NULL;
1885     r = ifapi_json_UINT32_serialize(in->restartCount, &jso2);
1886     return_if_error(r, "Serialize UINT32");
1887 
1888     json_object_object_add(*jso, "restartCount", jso2);
1889     jso2 = NULL;
1890     r = ifapi_json_TPMI_YES_NO_serialize(in->safe, &jso2);
1891     return_if_error(r, "Serialize TPMI_YES_NO");
1892 
1893     json_object_object_add(*jso, "safe", jso2);
1894     return TSS2_RC_SUCCESS;
1895 }
1896 
1897 /** Serialize value of type TPMS_TIME_INFO to json.
1898  *
1899  * @param[in] in value to be serialized.
1900  * @param[out] jso pointer to the json object.
1901  * @retval TSS2_RC_SUCCESS if the function call was a success.
1902  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1903  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_TIME_INFO.
1904  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1905  */
1906 TSS2_RC
ifapi_json_TPMS_TIME_INFO_serialize(const TPMS_TIME_INFO * in,json_object ** jso)1907 ifapi_json_TPMS_TIME_INFO_serialize(const TPMS_TIME_INFO *in, json_object **jso)
1908 {
1909     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1910 
1911     TSS2_RC r;
1912     json_object *jso2;
1913     if (*jso == NULL)
1914         *jso = json_object_new_object ();
1915     jso2 = NULL;
1916     r = ifapi_json_UINT64_serialize(in->time, &jso2);
1917     return_if_error(r, "Serialize UINT64");
1918 
1919     json_object_object_add(*jso, "time", jso2);
1920     jso2 = NULL;
1921     r = ifapi_json_TPMS_CLOCK_INFO_serialize(&in->clockInfo, &jso2);
1922     return_if_error(r, "Serialize TPMS_CLOCK_INFO");
1923 
1924     json_object_object_add(*jso, "clockInfo", jso2);
1925     return TSS2_RC_SUCCESS;
1926 }
1927 
1928 /** Serialize value of type TPMS_TIME_ATTEST_INFO to json.
1929  *
1930  * @param[in] in value to be serialized.
1931  * @param[out] jso pointer to the json object.
1932  * @retval TSS2_RC_SUCCESS if the function call was a success.
1933  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1934  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_TIME_ATTEST_INFO.
1935  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1936  */
1937 TSS2_RC
ifapi_json_TPMS_TIME_ATTEST_INFO_serialize(const TPMS_TIME_ATTEST_INFO * in,json_object ** jso)1938 ifapi_json_TPMS_TIME_ATTEST_INFO_serialize(const TPMS_TIME_ATTEST_INFO *in, json_object **jso)
1939 {
1940     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1941 
1942     TSS2_RC r;
1943     json_object *jso2;
1944     if (*jso == NULL)
1945         *jso = json_object_new_object ();
1946     jso2 = NULL;
1947     r = ifapi_json_TPMS_TIME_INFO_serialize(&in->time, &jso2);
1948     return_if_error(r, "Serialize TPMS_TIME_INFO");
1949 
1950     json_object_object_add(*jso, "time", jso2);
1951     jso2 = NULL;
1952     r = ifapi_json_UINT64_serialize(in->firmwareVersion, &jso2);
1953     return_if_error(r, "Serialize UINT64");
1954 
1955     json_object_object_add(*jso, "firmwareVersion", jso2);
1956     return TSS2_RC_SUCCESS;
1957 }
1958 
1959 /** Serialize value of type TPMS_CERTIFY_INFO to json.
1960  *
1961  * @param[in] in value to be serialized.
1962  * @param[out] jso pointer to the json object.
1963  * @retval TSS2_RC_SUCCESS if the function call was a success.
1964  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1965  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_CERTIFY_INFO.
1966  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1967  */
1968 TSS2_RC
ifapi_json_TPMS_CERTIFY_INFO_serialize(const TPMS_CERTIFY_INFO * in,json_object ** jso)1969 ifapi_json_TPMS_CERTIFY_INFO_serialize(const TPMS_CERTIFY_INFO *in, json_object **jso)
1970 {
1971     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1972 
1973     TSS2_RC r;
1974     json_object *jso2;
1975     if (*jso == NULL)
1976         *jso = json_object_new_object ();
1977     jso2 = NULL;
1978     r = ifapi_json_TPM2B_NAME_serialize(&in->name, &jso2);
1979     return_if_error(r, "Serialize TPM2B_NAME");
1980 
1981     json_object_object_add(*jso, "name", jso2);
1982     jso2 = NULL;
1983     r = ifapi_json_TPM2B_NAME_serialize(&in->qualifiedName, &jso2);
1984     return_if_error(r, "Serialize TPM2B_NAME");
1985 
1986     json_object_object_add(*jso, "qualifiedName", jso2);
1987     return TSS2_RC_SUCCESS;
1988 }
1989 
1990 /** Serialize value of type TPMS_QUOTE_INFO to json.
1991  *
1992  * @param[in] in value to be serialized.
1993  * @param[out] jso pointer to the json object.
1994  * @retval TSS2_RC_SUCCESS if the function call was a success.
1995  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1996  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_QUOTE_INFO.
1997  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1998  */
1999 TSS2_RC
ifapi_json_TPMS_QUOTE_INFO_serialize(const TPMS_QUOTE_INFO * in,json_object ** jso)2000 ifapi_json_TPMS_QUOTE_INFO_serialize(const TPMS_QUOTE_INFO *in, json_object **jso)
2001 {
2002     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2003 
2004     TSS2_RC r;
2005     json_object *jso2;
2006     if (*jso == NULL)
2007         *jso = json_object_new_object ();
2008     jso2 = NULL;
2009     r = ifapi_json_TPML_PCR_SELECTION_serialize(&in->pcrSelect, &jso2);
2010     return_if_error(r, "Serialize TPML_PCR_SELECTION");
2011 
2012     json_object_object_add(*jso, "pcrSelect", jso2);
2013     jso2 = NULL;
2014     r = ifapi_json_TPM2B_DIGEST_serialize(&in->pcrDigest, &jso2);
2015     return_if_error(r, "Serialize TPM2B_DIGEST");
2016 
2017     json_object_object_add(*jso, "pcrDigest", jso2);
2018     return TSS2_RC_SUCCESS;
2019 }
2020 
2021 /** Serialize value of type TPMS_COMMAND_AUDIT_INFO to json.
2022  *
2023  * @param[in] in value to be serialized.
2024  * @param[out] jso pointer to the json object.
2025  * @retval TSS2_RC_SUCCESS if the function call was a success.
2026  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2027  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_COMMAND_AUDIT_INFO.
2028  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2029  */
2030 TSS2_RC
ifapi_json_TPMS_COMMAND_AUDIT_INFO_serialize(const TPMS_COMMAND_AUDIT_INFO * in,json_object ** jso)2031 ifapi_json_TPMS_COMMAND_AUDIT_INFO_serialize(const TPMS_COMMAND_AUDIT_INFO *in, json_object **jso)
2032 {
2033     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2034 
2035     TSS2_RC r;
2036     json_object *jso2;
2037     if (*jso == NULL)
2038         *jso = json_object_new_object ();
2039     jso2 = NULL;
2040     r = ifapi_json_UINT64_serialize(in->auditCounter, &jso2);
2041     return_if_error(r, "Serialize UINT64");
2042 
2043     json_object_object_add(*jso, "auditCounter", jso2);
2044     jso2 = NULL;
2045     r = ifapi_json_TPM2_ALG_ID_serialize(in->digestAlg, &jso2);
2046     return_if_error(r, "Serialize TPM2_ALG_ID");
2047 
2048     json_object_object_add(*jso, "digestAlg", jso2);
2049     jso2 = NULL;
2050     r = ifapi_json_TPM2B_DIGEST_serialize(&in->auditDigest, &jso2);
2051     return_if_error(r, "Serialize TPM2B_DIGEST");
2052 
2053     json_object_object_add(*jso, "auditDigest", jso2);
2054     jso2 = NULL;
2055     r = ifapi_json_TPM2B_DIGEST_serialize(&in->commandDigest, &jso2);
2056     return_if_error(r, "Serialize TPM2B_DIGEST");
2057 
2058     json_object_object_add(*jso, "commandDigest", jso2);
2059     return TSS2_RC_SUCCESS;
2060 }
2061 
2062 /** Serialize value of type TPMS_SESSION_AUDIT_INFO to json.
2063  *
2064  * @param[in] in value to be serialized.
2065  * @param[out] jso pointer to the json object.
2066  * @retval TSS2_RC_SUCCESS if the function call was a success.
2067  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2068  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SESSION_AUDIT_INFO.
2069  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2070  */
2071 TSS2_RC
ifapi_json_TPMS_SESSION_AUDIT_INFO_serialize(const TPMS_SESSION_AUDIT_INFO * in,json_object ** jso)2072 ifapi_json_TPMS_SESSION_AUDIT_INFO_serialize(const TPMS_SESSION_AUDIT_INFO *in, json_object **jso)
2073 {
2074     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2075 
2076     TSS2_RC r;
2077     json_object *jso2;
2078     if (*jso == NULL)
2079         *jso = json_object_new_object ();
2080     jso2 = NULL;
2081     r = ifapi_json_TPMI_YES_NO_serialize(in->exclusiveSession, &jso2);
2082     return_if_error(r, "Serialize TPMI_YES_NO");
2083 
2084     json_object_object_add(*jso, "exclusiveSession", jso2);
2085     jso2 = NULL;
2086     r = ifapi_json_TPM2B_DIGEST_serialize(&in->sessionDigest, &jso2);
2087     return_if_error(r, "Serialize TPM2B_DIGEST");
2088 
2089     json_object_object_add(*jso, "sessionDigest", jso2);
2090     return TSS2_RC_SUCCESS;
2091 }
2092 
2093 /** Serialize value of type TPMS_CREATION_INFO to json.
2094  *
2095  * @param[in] in value to be serialized.
2096  * @param[out] jso pointer to the json object.
2097  * @retval TSS2_RC_SUCCESS if the function call was a success.
2098  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2099  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_CREATION_INFO.
2100  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2101  */
2102 TSS2_RC
ifapi_json_TPMS_CREATION_INFO_serialize(const TPMS_CREATION_INFO * in,json_object ** jso)2103 ifapi_json_TPMS_CREATION_INFO_serialize(const TPMS_CREATION_INFO *in, json_object **jso)
2104 {
2105     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2106 
2107     TSS2_RC r;
2108     json_object *jso2;
2109     if (*jso == NULL)
2110         *jso = json_object_new_object ();
2111     jso2 = NULL;
2112     r = ifapi_json_TPM2B_NAME_serialize(&in->objectName, &jso2);
2113     return_if_error(r, "Serialize TPM2B_NAME");
2114 
2115     json_object_object_add(*jso, "objectName", jso2);
2116     jso2 = NULL;
2117     r = ifapi_json_TPM2B_DIGEST_serialize(&in->creationHash, &jso2);
2118     return_if_error(r, "Serialize TPM2B_DIGEST");
2119 
2120     json_object_object_add(*jso, "creationHash", jso2);
2121     return TSS2_RC_SUCCESS;
2122 }
2123 
2124 /** Serialize value of type TPMS_NV_CERTIFY_INFO to json.
2125  *
2126  * @param[in] in value to be serialized.
2127  * @param[out] jso pointer to the json object.
2128  * @retval TSS2_RC_SUCCESS if the function call was a success.
2129  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2130  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_NV_CERTIFY_INFO.
2131  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2132  */
2133 TSS2_RC
ifapi_json_TPMS_NV_CERTIFY_INFO_serialize(const TPMS_NV_CERTIFY_INFO * in,json_object ** jso)2134 ifapi_json_TPMS_NV_CERTIFY_INFO_serialize(const TPMS_NV_CERTIFY_INFO *in, json_object **jso)
2135 {
2136     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2137 
2138     TSS2_RC r;
2139     json_object *jso2;
2140     if (*jso == NULL)
2141         *jso = json_object_new_object ();
2142     jso2 = NULL;
2143     r = ifapi_json_TPM2B_NAME_serialize(&in->indexName, &jso2);
2144     return_if_error(r, "Serialize TPM2B_NAME");
2145 
2146     json_object_object_add(*jso, "indexName", jso2);
2147     jso2 = NULL;
2148     r = ifapi_json_UINT16_serialize(in->offset, &jso2);
2149     return_if_error(r, "Serialize UINT16");
2150 
2151     json_object_object_add(*jso, "offset", jso2);
2152     jso2 = NULL;
2153     r = ifapi_json_TPM2B_MAX_NV_BUFFER_serialize(&in->nvContents, &jso2);
2154     return_if_error(r, "Serialize TPM2B_MAX_NV_BUFFER");
2155 
2156     json_object_object_add(*jso, "nvContents", jso2);
2157     return TSS2_RC_SUCCESS;
2158 }
2159 
2160 /** Serialize TPMI_ST_ATTEST to json.
2161  *
2162  * @param[in] in variable to be serialized.
2163  * @param[out] jso pointer to the json object.
2164  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
2165  *         the function.
2166  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
2167  */
2168 TSS2_RC
ifapi_json_TPMI_ST_ATTEST_serialize(const TPMI_ST_ATTEST in,json_object ** jso)2169 ifapi_json_TPMI_ST_ATTEST_serialize(const TPMI_ST_ATTEST in, json_object **jso)
2170 {
2171     CHECK_IN_LIST(TPMI_ALG_HASH, in, TPM2_ST_ATTEST_CERTIFY, TPM2_ST_ATTEST_QUOTE,
2172                   TPM2_ST_ATTEST_SESSION_AUDIT, TPM2_ST_ATTEST_COMMAND_AUDIT,
2173                   TPM2_ST_ATTEST_TIME, TPM2_ST_ATTEST_CREATION, TPM2_ST_ATTEST_NV);
2174     return ifapi_json_TPM2_ST_serialize(in, jso);
2175 }
2176 
2177 /**  Serialize a TPMU_ATTEST to json.
2178  *
2179  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2180  * @param[in] in the value to be serialized.
2181  * @param[in] selector the type of the attest.
2182  * @param[out] jso pointer to the json object.
2183  * @retval TSS2_RC_SUCCESS if the function call was a success.
2184  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2185  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_ATTEST.
2186  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2187  */
2188 TSS2_RC
ifapi_json_TPMU_ATTEST_serialize(const TPMU_ATTEST * in,UINT32 selector,json_object ** jso)2189 ifapi_json_TPMU_ATTEST_serialize(const TPMU_ATTEST *in, UINT32 selector, json_object **jso)
2190 {
2191     switch (selector) {
2192         case TPM2_ST_ATTEST_CERTIFY:
2193             return ifapi_json_TPMS_CERTIFY_INFO_serialize(&in->certify, jso);
2194         case TPM2_ST_ATTEST_CREATION:
2195             return ifapi_json_TPMS_CREATION_INFO_serialize(&in->creation, jso);
2196         case TPM2_ST_ATTEST_QUOTE:
2197             return ifapi_json_TPMS_QUOTE_INFO_serialize(&in->quote, jso);
2198         case TPM2_ST_ATTEST_COMMAND_AUDIT:
2199             return ifapi_json_TPMS_COMMAND_AUDIT_INFO_serialize(&in->commandAudit, jso);
2200         case TPM2_ST_ATTEST_SESSION_AUDIT:
2201             return ifapi_json_TPMS_SESSION_AUDIT_INFO_serialize(&in->sessionAudit, jso);
2202         case TPM2_ST_ATTEST_TIME:
2203             return ifapi_json_TPMS_TIME_ATTEST_INFO_serialize(&in->time, jso);
2204         case TPM2_ST_ATTEST_NV:
2205             return ifapi_json_TPMS_NV_CERTIFY_INFO_serialize(&in->nv, jso);
2206         default:
2207             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2208             return TSS2_FAPI_RC_BAD_VALUE;
2209     };
2210     return TSS2_RC_SUCCESS;
2211 }
2212 
2213 /** Serialize value of type TPMS_ATTEST to json.
2214  *
2215  * @param[in] in value to be serialized.
2216  * @param[out] jso pointer to the json object.
2217  * @retval TSS2_RC_SUCCESS if the function call was a success.
2218  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2219  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ATTEST.
2220  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2221  */
2222 TSS2_RC
ifapi_json_TPMS_ATTEST_serialize(const TPMS_ATTEST * in,json_object ** jso)2223 ifapi_json_TPMS_ATTEST_serialize(const TPMS_ATTEST *in, json_object **jso)
2224 {
2225     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2226 
2227     TSS2_RC r;
2228     json_object *jso2;
2229     if (*jso == NULL)
2230         *jso = json_object_new_object ();
2231     jso2 = NULL;
2232     r = ifapi_json_TPM2_GENERATED_serialize(in->magic, &jso2);
2233     return_if_error(r, "Serialize TPM2_GENERATED");
2234 
2235     json_object_object_add(*jso, "magic", jso2);
2236     jso2 = NULL;
2237     r = ifapi_json_TPMI_ST_ATTEST_serialize(in->type, &jso2);
2238     return_if_error(r, "Serialize TPMI_ST_ATTEST");
2239 
2240     json_object_object_add(*jso, "type", jso2);
2241     jso2 = NULL;
2242     r = ifapi_json_TPM2B_NAME_serialize(&in->qualifiedSigner, &jso2);
2243     return_if_error(r, "Serialize TPM2B_NAME");
2244 
2245     json_object_object_add(*jso, "qualifiedSigner", jso2);
2246     jso2 = NULL;
2247     r = ifapi_json_TPM2B_DATA_serialize(&in->extraData, &jso2);
2248     return_if_error(r, "Serialize TPM2B_DATA");
2249 
2250     json_object_object_add(*jso, "extraData", jso2);
2251     jso2 = NULL;
2252     r = ifapi_json_TPMS_CLOCK_INFO_serialize(&in->clockInfo, &jso2);
2253     return_if_error(r, "Serialize TPMS_CLOCK_INFO");
2254 
2255     json_object_object_add(*jso, "clockInfo", jso2);
2256     jso2 = NULL;
2257     r = ifapi_json_UINT64_serialize(in->firmwareVersion, &jso2);
2258     return_if_error(r, "Serialize UINT64");
2259 
2260     json_object_object_add(*jso, "firmwareVersion", jso2);
2261     jso2 = NULL;
2262     r = ifapi_json_TPMU_ATTEST_serialize(&in->attested, in->type, &jso2);
2263     return_if_error(r,"Serialize TPMU_ATTEST");
2264 
2265     json_object_object_add(*jso, "attested", jso2);
2266     return TSS2_RC_SUCCESS;
2267 }
2268 
2269 /** Serialize value of type TPMI_AES_KEY_BITS to json.
2270  *
2271  * @param[in] in value to be serialized.
2272  * @param[out] jso pointer to the json object.
2273  * @retval TSS2_RC_SUCCESS if the function call was a success.
2274  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2275  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMI_AES_KEY_BITS.
2276  *
2277  */
2278 TSS2_RC
ifapi_json_TPMI_AES_KEY_BITS_serialize(const TPMI_AES_KEY_BITS in,json_object ** jso)2279 ifapi_json_TPMI_AES_KEY_BITS_serialize(const TPMI_AES_KEY_BITS in, json_object **jso)
2280 {
2281     CHECK_IN_LIST(UINT16, in, 128, 192, 256);
2282     return ifapi_json_UINT16_serialize(in, jso);
2283 }
2284 
2285 /**  Serialize a TPMU_SYM_KEY_BITS to json.
2286  *
2287  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2288  * @param[in] in the value to be serialized.
2289  * @param[in] selector the type of the symmetric algorithm.
2290  * @param[out] jso pointer to the json object.
2291  * @retval TSS2_RC_SUCCESS if the function call was a success.
2292  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2293  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_SYM_KEY_BITS.
2294  */
2295 TSS2_RC
ifapi_json_TPMU_SYM_KEY_BITS_serialize(const TPMU_SYM_KEY_BITS * in,UINT32 selector,json_object ** jso)2296 ifapi_json_TPMU_SYM_KEY_BITS_serialize(const TPMU_SYM_KEY_BITS *in, UINT32 selector, json_object **jso)
2297 {
2298     switch (selector) {
2299         case TPM2_ALG_AES:
2300             return ifapi_json_TPMI_AES_KEY_BITS_serialize(in->aes, jso);
2301         case TPM2_ALG_XOR:
2302             return ifapi_json_TPMI_ALG_HASH_serialize(in->exclusiveOr, jso);
2303         default:
2304             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2305             return TSS2_FAPI_RC_BAD_VALUE;
2306     };
2307     return TSS2_RC_SUCCESS;
2308 }
2309 
2310 /**  Serialize a TPMU_SYM_MODE to json.
2311  *
2312  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2313  * @param[in] in the value to be serialized.
2314  * @param[in] selector the type of the symmetric mode.
2315  * @param[out] jso pointer to the json object.
2316  * @retval TSS2_RC_SUCCESS if the function call was a success.
2317  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2318  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_SYM_MODE.
2319  */
2320 TSS2_RC
ifapi_json_TPMU_SYM_MODE_serialize(const TPMU_SYM_MODE * in,UINT32 selector,json_object ** jso)2321 ifapi_json_TPMU_SYM_MODE_serialize(const TPMU_SYM_MODE *in, UINT32 selector, json_object **jso)
2322 {
2323     switch (selector) {
2324         case TPM2_ALG_AES:
2325             return ifapi_json_TPMI_ALG_SYM_MODE_serialize(in->aes, jso);
2326         default:
2327             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2328             return TSS2_FAPI_RC_BAD_VALUE;
2329     };
2330     return TSS2_RC_SUCCESS;
2331 }
2332 
2333 /** Serialize value of type TPMT_SYM_DEF_OBJECT to json.
2334  *
2335  * @param[in] in value to be serialized.
2336  * @param[out] jso pointer to the json object.
2337  * @retval TSS2_RC_SUCCESS if the function call was a success.
2338  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2339  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_SYM_DEF_OBJECT.
2340  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2341  */
2342 TSS2_RC
ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(const TPMT_SYM_DEF_OBJECT * in,json_object ** jso)2343 ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(const TPMT_SYM_DEF_OBJECT *in, json_object **jso)
2344 {
2345     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2346 
2347     TSS2_RC r;
2348     json_object *jso2;
2349     if (*jso == NULL)
2350         *jso = json_object_new_object ();
2351     jso2 = NULL;
2352     r = ifapi_json_TPMI_ALG_SYM_OBJECT_serialize(in->algorithm, &jso2);
2353     return_if_error(r, "Serialize TPMI_ALG_SYM_OBJECT");
2354 
2355     json_object_object_add(*jso, "algorithm", jso2);
2356     if (in->algorithm != TPM2_ALG_NULL) {
2357         json_object *jso2 = NULL;
2358         r = ifapi_json_TPMU_SYM_KEY_BITS_serialize(&in->keyBits, in->algorithm, &jso2);
2359         return_if_error(r,"Serialize TPMU_SYM_KEY_BITS");
2360 
2361         json_object_object_add(*jso, "keyBits", jso2);
2362     }
2363     if (in->algorithm != TPM2_ALG_NULL) {
2364         json_object *jso2 = NULL;
2365         r = ifapi_json_TPMU_SYM_MODE_serialize(&in->mode, in->algorithm, &jso2);
2366         return_if_error(r,"Serialize TPMU_SYM_MODE");
2367 
2368         json_object_object_add(*jso, "mode", jso2);
2369     }
2370     return TSS2_RC_SUCCESS;
2371 }
2372 
2373 /** Serialize value of type TPMS_SYMCIPHER_PARMS to json.
2374  *
2375  * @param[in] in value to be serialized.
2376  * @param[out] jso pointer to the json object.
2377  * @retval TSS2_RC_SUCCESS if the function call was a success.
2378  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2379  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SYMCIPHER_PARMS.
2380  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2381  */
2382 TSS2_RC
ifapi_json_TPMS_SYMCIPHER_PARMS_serialize(const TPMS_SYMCIPHER_PARMS * in,json_object ** jso)2383 ifapi_json_TPMS_SYMCIPHER_PARMS_serialize(const TPMS_SYMCIPHER_PARMS *in, json_object **jso)
2384 {
2385     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2386 
2387     TSS2_RC r;
2388     json_object *jso2;
2389     if (*jso == NULL)
2390         *jso = json_object_new_object ();
2391     jso2 = NULL;
2392     r = ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(&in->sym, &jso2);
2393     return_if_error(r, "Serialize TPMT_SYM_DEF_OBJECT");
2394 
2395     json_object_object_add(*jso, "sym", jso2);
2396     return TSS2_RC_SUCCESS;
2397 }
2398 
2399 /** Serialize value of type TPMS_SCHEME_HASH to json.
2400  *
2401  * @param[in] in value to be serialized.
2402  * @param[out] jso pointer to the json object.
2403  * @retval TSS2_RC_SUCCESS if the function call was a success.
2404  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2405  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_HASH.
2406  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2407  */
2408 TSS2_RC
ifapi_json_TPMS_SCHEME_HASH_serialize(const TPMS_SCHEME_HASH * in,json_object ** jso)2409 ifapi_json_TPMS_SCHEME_HASH_serialize(const TPMS_SCHEME_HASH *in, json_object **jso)
2410 {
2411     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2412 
2413     TSS2_RC r;
2414     json_object *jso2;
2415     if (*jso == NULL)
2416         *jso = json_object_new_object ();
2417     jso2 = NULL;
2418     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hashAlg, &jso2);
2419     return_if_error(r, "Serialize TPMI_ALG_HASH");
2420 
2421     json_object_object_add(*jso, "hashAlg", jso2);
2422     return TSS2_RC_SUCCESS;
2423 }
2424 
2425 /** Serialize value of type TPMS_SCHEME_ECDAA to json.
2426  *
2427  * @param[in] in value to be serialized.
2428  * @param[out] jso pointer to the json object.
2429  * @retval TSS2_RC_SUCCESS if the function call was a success.
2430  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2431  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_ECDAA.
2432  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2433  */
2434 TSS2_RC
ifapi_json_TPMS_SCHEME_ECDAA_serialize(const TPMS_SCHEME_ECDAA * in,json_object ** jso)2435 ifapi_json_TPMS_SCHEME_ECDAA_serialize(const TPMS_SCHEME_ECDAA *in, json_object **jso)
2436 {
2437     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2438 
2439     TSS2_RC r;
2440     json_object *jso2;
2441     if (*jso == NULL)
2442         *jso = json_object_new_object ();
2443     jso2 = NULL;
2444     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hashAlg, &jso2);
2445     return_if_error(r, "Serialize TPMI_ALG_HASH");
2446 
2447     json_object_object_add(*jso, "hashAlg", jso2);
2448     jso2 = NULL;
2449     r = ifapi_json_UINT16_serialize(in->count, &jso2);
2450     return_if_error(r, "Serialize UINT16");
2451 
2452     json_object_object_add(*jso, "count", jso2);
2453     return TSS2_RC_SUCCESS;
2454 }
2455 
2456 /** Serialize TPMI_ALG_KEYEDHASH_SCHEME to json.
2457  *
2458  * @param[in] in variable to be serialized.
2459  * @param[out] jso pointer to the json object.
2460  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
2461  *         the function.
2462  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
2463  */
2464 TSS2_RC
ifapi_json_TPMI_ALG_KEYEDHASH_SCHEME_serialize(const TPMI_ALG_KEYEDHASH_SCHEME in,json_object ** jso)2465 ifapi_json_TPMI_ALG_KEYEDHASH_SCHEME_serialize(const TPMI_ALG_KEYEDHASH_SCHEME in, json_object **jso)
2466 {
2467     CHECK_IN_LIST(TPMI_ALG_HASH, in, TPM2_ALG_HMAC, TPM2_ALG_XOR, TPM2_ALG_NULL);
2468     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
2469 }
2470 
2471 /** Serialize a TPMS_SCHEME_HMAC to json.
2472  *
2473  * @param[in] in value of type TPMS_SCHEME_HMAC to be serialized.
2474  * @param[out] jso pointer to the json object.
2475  * @retval TSS2_RC_SUCCESS if the function call was a success.
2476  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2477  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_HMAC.
2478  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2479  */
2480 TSS2_RC
ifapi_json_TPMS_SCHEME_HMAC_serialize(const TPMS_SCHEME_HMAC * in,json_object ** jso)2481 ifapi_json_TPMS_SCHEME_HMAC_serialize(const TPMS_SCHEME_HMAC *in, json_object **jso)
2482 {
2483     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2484 }
2485 
2486 /** Serialize value of type TPMS_SCHEME_XOR to json.
2487  *
2488  * @param[in] in value to be serialized.
2489  * @param[out] jso pointer to the json object.
2490  * @retval TSS2_RC_SUCCESS if the function call was a success.
2491  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2492  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_XOR.
2493  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2494  */
2495 TSS2_RC
ifapi_json_TPMS_SCHEME_XOR_serialize(const TPMS_SCHEME_XOR * in,json_object ** jso)2496 ifapi_json_TPMS_SCHEME_XOR_serialize(const TPMS_SCHEME_XOR *in, json_object **jso)
2497 {
2498     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2499 
2500     TSS2_RC r;
2501     json_object *jso2;
2502     if (*jso == NULL)
2503         *jso = json_object_new_object ();
2504     jso2 = NULL;
2505     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hashAlg, &jso2);
2506     return_if_error(r, "Serialize TPMI_ALG_HASH");
2507 
2508     json_object_object_add(*jso, "hashAlg", jso2);
2509     jso2 = NULL;
2510     r = ifapi_json_TPMI_ALG_KDF_serialize(in->kdf, &jso2);
2511     return_if_error(r, "Serialize TPMI_ALG_KDF");
2512 
2513     json_object_object_add(*jso, "kdf", jso2);
2514     return TSS2_RC_SUCCESS;
2515 }
2516 
2517 /**  Serialize a TPMU_SCHEME_KEYEDHASH to json.
2518  *
2519  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2520  * @param[in] in the value to be serialized.
2521  * @param[in] selector the type of the keyedhash scheme.
2522  * @param[out] jso pointer to the json object.
2523  * @retval TSS2_RC_SUCCESS if the function call was a success.
2524  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2525  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_SCHEME_KEYEDHASH.
2526  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2527  */
2528 TSS2_RC
ifapi_json_TPMU_SCHEME_KEYEDHASH_serialize(const TPMU_SCHEME_KEYEDHASH * in,UINT32 selector,json_object ** jso)2529 ifapi_json_TPMU_SCHEME_KEYEDHASH_serialize(const TPMU_SCHEME_KEYEDHASH *in, UINT32 selector, json_object **jso)
2530 {
2531     switch (selector) {
2532         case TPM2_ALG_HMAC:
2533             return ifapi_json_TPMS_SCHEME_HMAC_serialize(&in->hmac, jso);
2534         case TPM2_ALG_XOR:
2535             return ifapi_json_TPMS_SCHEME_XOR_serialize(&in->exclusiveOr, jso);
2536         default:
2537             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2538             return TSS2_FAPI_RC_BAD_VALUE;
2539     };
2540     return TSS2_RC_SUCCESS;
2541 }
2542 
2543 /** Serialize value of type TPMT_KEYEDHASH_SCHEME to json.
2544  *
2545  * @param[in] in value to be serialized.
2546  * @param[out] jso pointer to the json object.
2547  * @retval TSS2_RC_SUCCESS if the function call was a success.
2548  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2549  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_KEYEDHASH_SCHEME.
2550  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2551  */
2552 TSS2_RC
ifapi_json_TPMT_KEYEDHASH_SCHEME_serialize(const TPMT_KEYEDHASH_SCHEME * in,json_object ** jso)2553 ifapi_json_TPMT_KEYEDHASH_SCHEME_serialize(const TPMT_KEYEDHASH_SCHEME *in, json_object **jso)
2554 {
2555     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2556 
2557     TSS2_RC r;
2558     json_object *jso2;
2559     if (*jso == NULL)
2560         *jso = json_object_new_object ();
2561     jso2 = NULL;
2562     r = ifapi_json_TPMI_ALG_KEYEDHASH_SCHEME_serialize(in->scheme, &jso2);
2563     return_if_error(r, "Serialize TPMI_ALG_KEYEDHASH_SCHEME");
2564 
2565     json_object_object_add(*jso, "scheme", jso2);
2566     if (in->scheme != TPM2_ALG_NULL) {
2567         json_object *jso2 = NULL;
2568         r = ifapi_json_TPMU_SCHEME_KEYEDHASH_serialize(&in->details, in->scheme, &jso2);
2569         return_if_error(r,"Serialize TPMU_SCHEME_KEYEDHASH");
2570 
2571         json_object_object_add(*jso, "details", jso2);
2572     }
2573     return TSS2_RC_SUCCESS;
2574 }
2575 
2576 /** Serialize a TPMS_SIG_SCHEME_RSASSA to json.
2577  *
2578  * @param[in] in value of type TPMS_SIG_SCHEME_RSASSA to be serialized.
2579  * @param[out] jso pointer to the json object.
2580  * @retval TSS2_RC_SUCCESS if the function call was a success.
2581  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2582  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_RSASSA.
2583  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2584  */
2585 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_RSASSA_serialize(const TPMS_SIG_SCHEME_RSASSA * in,json_object ** jso)2586 ifapi_json_TPMS_SIG_SCHEME_RSASSA_serialize(const TPMS_SIG_SCHEME_RSASSA *in, json_object **jso)
2587 {
2588     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2589 }
2590 
2591 /** Serialize a TPMS_SIG_SCHEME_RSAPSS to json.
2592  *
2593  * @param[in] in value of type TPMS_SIG_SCHEME_RSAPSS to be serialized.
2594  * @param[out] jso pointer to the json object.
2595  * @retval TSS2_RC_SUCCESS if the function call was a success.
2596  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2597  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_RSAPSS.
2598  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2599  */
2600 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_RSAPSS_serialize(const TPMS_SIG_SCHEME_RSAPSS * in,json_object ** jso)2601 ifapi_json_TPMS_SIG_SCHEME_RSAPSS_serialize(const TPMS_SIG_SCHEME_RSAPSS *in, json_object **jso)
2602 {
2603     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2604 }
2605 
2606 /** Serialize a TPMS_SIG_SCHEME_ECDSA to json.
2607  *
2608  * @param[in] in value of type TPMS_SIG_SCHEME_ECDSA to be serialized.
2609  * @param[out] jso pointer to the json object.
2610  * @retval TSS2_RC_SUCCESS if the function call was a success.
2611  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2612  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_ECDSA.
2613  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2614  */
2615 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_ECDSA_serialize(const TPMS_SIG_SCHEME_ECDSA * in,json_object ** jso)2616 ifapi_json_TPMS_SIG_SCHEME_ECDSA_serialize(const TPMS_SIG_SCHEME_ECDSA *in, json_object **jso)
2617 {
2618     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2619 }
2620 
2621 /** Serialize a TPMS_SIG_SCHEME_SM2 to json.
2622  *
2623  * @param[in] in value of type TPMS_SIG_SCHEME_SM2 to be serialized.
2624  * @param[out] jso pointer to the json object.
2625  * @retval TSS2_RC_SUCCESS if the function call was a success.
2626  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2627  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_SM2.
2628  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2629  */
2630 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_SM2_serialize(const TPMS_SIG_SCHEME_SM2 * in,json_object ** jso)2631 ifapi_json_TPMS_SIG_SCHEME_SM2_serialize(const TPMS_SIG_SCHEME_SM2 *in, json_object **jso)
2632 {
2633     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2634 }
2635 
2636 /** Serialize a TPMS_SIG_SCHEME_ECSCHNORR to json.
2637  *
2638  * @param[in] in value of type TPMS_SIG_SCHEME_ECSCHNORR to be serialized.
2639  * @param[out] jso pointer to the json object.
2640  * @retval TSS2_RC_SUCCESS if the function call was a success.
2641  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2642  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_ECSCHNORR.
2643  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2644  */
2645 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_ECSCHNORR_serialize(const TPMS_SIG_SCHEME_ECSCHNORR * in,json_object ** jso)2646 ifapi_json_TPMS_SIG_SCHEME_ECSCHNORR_serialize(const TPMS_SIG_SCHEME_ECSCHNORR *in, json_object **jso)
2647 {
2648     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2649 }
2650 
2651 /** Serialize a TPMS_SIG_SCHEME_ECDAA to json.
2652  *
2653  * @param[in] in value of type TPMS_SIG_SCHEME_ECDAA to be serialized.
2654  * @param[out] jso pointer to the json object.
2655  * @retval TSS2_RC_SUCCESS if the function call was a success.
2656  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2657  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIG_SCHEME_ECDAA.
2658  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2659  */
2660 TSS2_RC
ifapi_json_TPMS_SIG_SCHEME_ECDAA_serialize(const TPMS_SIG_SCHEME_ECDAA * in,json_object ** jso)2661 ifapi_json_TPMS_SIG_SCHEME_ECDAA_serialize(const TPMS_SIG_SCHEME_ECDAA *in, json_object **jso)
2662 {
2663     return ifapi_json_TPMS_SCHEME_ECDAA_serialize(in, jso);
2664 }
2665 
2666 /**  Serialize a TPMU_SIG_SCHEME to json.
2667  *
2668  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2669  * @param[in] in the value to be serialized.
2670  * @param[in] selector the type of the signature scheme.
2671  * @param[out] jso pointer to the json object.
2672  * @retval TSS2_RC_SUCCESS if the function call was a success.
2673  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2674  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_SIG_SCHEME.
2675  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2676  */
2677 TSS2_RC
ifapi_json_TPMU_SIG_SCHEME_serialize(const TPMU_SIG_SCHEME * in,UINT32 selector,json_object ** jso)2678 ifapi_json_TPMU_SIG_SCHEME_serialize(const TPMU_SIG_SCHEME *in, UINT32 selector, json_object **jso)
2679 {
2680     switch (selector) {
2681         case TPM2_ALG_RSASSA:
2682             return ifapi_json_TPMS_SIG_SCHEME_RSASSA_serialize(&in->rsassa, jso);
2683         case TPM2_ALG_RSAPSS:
2684             return ifapi_json_TPMS_SIG_SCHEME_RSAPSS_serialize(&in->rsapss, jso);
2685         case TPM2_ALG_ECDSA:
2686             return ifapi_json_TPMS_SIG_SCHEME_ECDSA_serialize(&in->ecdsa, jso);
2687         case TPM2_ALG_ECDAA:
2688             return ifapi_json_TPMS_SIG_SCHEME_ECDAA_serialize(&in->ecdaa, jso);
2689         case TPM2_ALG_SM2:
2690             return ifapi_json_TPMS_SIG_SCHEME_SM2_serialize(&in->sm2, jso);
2691         case TPM2_ALG_ECSCHNORR:
2692             return ifapi_json_TPMS_SIG_SCHEME_ECSCHNORR_serialize(&in->ecschnorr, jso);
2693         case TPM2_ALG_HMAC:
2694             return ifapi_json_TPMS_SCHEME_HMAC_serialize(&in->hmac, jso);
2695         default:
2696             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2697             return TSS2_FAPI_RC_BAD_VALUE;
2698     };
2699     return TSS2_RC_SUCCESS;
2700 }
2701 
2702 /** Serialize value of type TPMT_SIG_SCHEME to json.
2703  *
2704  * @param[in] in value to be serialized.
2705  * @param[out] jso pointer to the json object.
2706  * @retval TSS2_RC_SUCCESS if the function call was a success.
2707  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2708  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_SIG_SCHEME.
2709  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2710  */
2711 TSS2_RC
ifapi_json_TPMT_SIG_SCHEME_serialize(const TPMT_SIG_SCHEME * in,json_object ** jso)2712 ifapi_json_TPMT_SIG_SCHEME_serialize(const TPMT_SIG_SCHEME *in, json_object **jso)
2713 {
2714     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2715 
2716     TSS2_RC r;
2717     json_object *jso2;
2718     if (*jso == NULL)
2719         *jso = json_object_new_object ();
2720     jso2 = NULL;
2721     r = ifapi_json_TPMI_ALG_SIG_SCHEME_serialize(in->scheme, &jso2);
2722     return_if_error(r, "Serialize TPMI_ALG_SIG_SCHEME");
2723 
2724     json_object_object_add(*jso, "scheme", jso2);
2725     if (in->scheme != TPM2_ALG_NULL) {
2726         json_object *jso2 = NULL;
2727         r = ifapi_json_TPMU_SIG_SCHEME_serialize(&in->details, in->scheme, &jso2);
2728         return_if_error(r,"Serialize TPMU_SIG_SCHEME");
2729 
2730         json_object_object_add(*jso, "details", jso2);
2731     }
2732     return TSS2_RC_SUCCESS;
2733 }
2734 
2735 /** Serialize a TPMS_ENC_SCHEME_OAEP to json.
2736  *
2737  * @param[in] in value of type TPMS_ENC_SCHEME_OAEP to be serialized.
2738  * @param[out] jso pointer to the json object.
2739  * @retval TSS2_RC_SUCCESS if the function call was a success.
2740  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2741  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ENC_SCHEME_OAEP.
2742  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2743  */
2744 TSS2_RC
ifapi_json_TPMS_ENC_SCHEME_OAEP_serialize(const TPMS_ENC_SCHEME_OAEP * in,json_object ** jso)2745 ifapi_json_TPMS_ENC_SCHEME_OAEP_serialize(const TPMS_ENC_SCHEME_OAEP *in, json_object **jso)
2746 {
2747     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2748 }
2749 
2750 /** Serialize a TPMS_ENC_SCHEME_RSAES to json.
2751  *
2752  * @param[in] in value of type TPMS_ENC_SCHEME_RSAES to be serialized.
2753  * @param[out] jso pointer to the json object.
2754  * @retval TSS2_RC_SUCCESS if the function call was a success.
2755  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2756  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ENC_SCHEME_RSAES.
2757  */
2758 TSS2_RC
ifapi_json_TPMS_ENC_SCHEME_RSAES_serialize(const TPMS_ENC_SCHEME_RSAES * in,json_object ** jso)2759 ifapi_json_TPMS_ENC_SCHEME_RSAES_serialize(const TPMS_ENC_SCHEME_RSAES *in, json_object **jso)
2760 {
2761     return ifapi_json_TPMS_EMPTY_serialize(in, jso);
2762 }
2763 
2764 /** Serialize a TPMS_KEY_SCHEME_ECDH to json.
2765  *
2766  * @param[in] in value of type TPMS_KEY_SCHEME_ECDH to be serialized.
2767  * @param[out] jso pointer to the json object.
2768  * @retval TSS2_RC_SUCCESS if the function call was a success.
2769  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2770  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_KEY_SCHEME_ECDH.
2771  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2772  */
2773 TSS2_RC
ifapi_json_TPMS_KEY_SCHEME_ECDH_serialize(const TPMS_KEY_SCHEME_ECDH * in,json_object ** jso)2774 ifapi_json_TPMS_KEY_SCHEME_ECDH_serialize(const TPMS_KEY_SCHEME_ECDH *in, json_object **jso)
2775 {
2776     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2777 }
2778 
2779 /** Serialize a TPMS_SCHEME_MGF1 to json.
2780  *
2781  * @param[in] in value of type TPMS_SCHEME_MGF1 to be serialized.
2782  * @param[out] jso pointer to the json object.
2783  * @retval TSS2_RC_SUCCESS if the function call was a success.
2784  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2785  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_MGF1.
2786  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2787  */
2788 TSS2_RC
ifapi_json_TPMS_SCHEME_MGF1_serialize(const TPMS_SCHEME_MGF1 * in,json_object ** jso)2789 ifapi_json_TPMS_SCHEME_MGF1_serialize(const TPMS_SCHEME_MGF1 *in, json_object **jso)
2790 {
2791     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2792 }
2793 
2794 /** Serialize a TPMS_SCHEME_KDF1_SP800_56A to json.
2795  *
2796  * @param[in] in value of type TPMS_SCHEME_KDF1_SP800_56A to be serialized.
2797  * @param[out] jso pointer to the json object.
2798  * @retval TSS2_RC_SUCCESS if the function call was a success.
2799  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2800  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_KDF1_SP800_56A.
2801  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2802  */
2803 TSS2_RC
ifapi_json_TPMS_SCHEME_KDF1_SP800_56A_serialize(const TPMS_SCHEME_KDF1_SP800_56A * in,json_object ** jso)2804 ifapi_json_TPMS_SCHEME_KDF1_SP800_56A_serialize(const TPMS_SCHEME_KDF1_SP800_56A *in, json_object **jso)
2805 {
2806     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2807 }
2808 
2809 /** Serialize a TPMS_SCHEME_KDF1_SP800_108 to json.
2810  *
2811  * @param[in] in value of type TPMS_SCHEME_KDF1_SP800_108 to be serialized.
2812  * @param[out] jso pointer to the json object.
2813  * @retval TSS2_RC_SUCCESS if the function call was a success.
2814  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2815  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SCHEME_KDF1_SP800_108.
2816  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2817  */
2818 TSS2_RC
ifapi_json_TPMS_SCHEME_KDF1_SP800_108_serialize(const TPMS_SCHEME_KDF1_SP800_108 * in,json_object ** jso)2819 ifapi_json_TPMS_SCHEME_KDF1_SP800_108_serialize(const TPMS_SCHEME_KDF1_SP800_108 *in, json_object **jso)
2820 {
2821     return ifapi_json_TPMS_SCHEME_HASH_serialize(in, jso);
2822 }
2823 
2824 /**  Serialize a TPMU_KDF_SCHEME to json.
2825  *
2826  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2827  * @param[in] in the value to be serialized.
2828  * @param[in] selector the type of the KDF scheme.
2829  * @param[out] jso pointer to the json object.
2830  * @retval TSS2_RC_SUCCESS if the function call was a success.
2831  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2832  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_KDF_SCHEME.
2833  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2834  */
2835 TSS2_RC
ifapi_json_TPMU_KDF_SCHEME_serialize(const TPMU_KDF_SCHEME * in,UINT32 selector,json_object ** jso)2836 ifapi_json_TPMU_KDF_SCHEME_serialize(const TPMU_KDF_SCHEME *in, UINT32 selector, json_object **jso)
2837 {
2838     switch (selector) {
2839         case TPM2_ALG_MGF1:
2840             return ifapi_json_TPMS_SCHEME_MGF1_serialize(&in->mgf1, jso);
2841         case TPM2_ALG_KDF1_SP800_56A:
2842             return ifapi_json_TPMS_SCHEME_KDF1_SP800_56A_serialize(&in->kdf1_sp800_56a, jso);
2843         case TPM2_ALG_KDF1_SP800_108:
2844             return ifapi_json_TPMS_SCHEME_KDF1_SP800_108_serialize(&in->kdf1_sp800_108, jso);
2845         default:
2846             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2847             return TSS2_FAPI_RC_BAD_VALUE;
2848     };
2849     return TSS2_RC_SUCCESS;
2850 }
2851 
2852 /** Serialize value of type TPMT_KDF_SCHEME to json.
2853  *
2854  * @param[in] in value to be serialized.
2855  * @param[out] jso pointer to the json object.
2856  * @retval TSS2_RC_SUCCESS if the function call was a success.
2857  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2858  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_KDF_SCHEME.
2859  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2860  */
2861 TSS2_RC
ifapi_json_TPMT_KDF_SCHEME_serialize(const TPMT_KDF_SCHEME * in,json_object ** jso)2862 ifapi_json_TPMT_KDF_SCHEME_serialize(const TPMT_KDF_SCHEME *in, json_object **jso)
2863 {
2864     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2865 
2866     TSS2_RC r;
2867     json_object *jso2;
2868     if (*jso == NULL)
2869         *jso = json_object_new_object ();
2870     jso2 = NULL;
2871     r = ifapi_json_TPMI_ALG_KDF_serialize(in->scheme, &jso2);
2872     return_if_error(r, "Serialize TPMI_ALG_KDF");
2873 
2874     json_object_object_add(*jso, "scheme", jso2);
2875     if (in->scheme != TPM2_ALG_NULL) {
2876         json_object *jso2 = NULL;
2877         r = ifapi_json_TPMU_KDF_SCHEME_serialize(&in->details, in->scheme, &jso2);
2878         return_if_error(r,"Serialize TPMU_KDF_SCHEME");
2879 
2880         json_object_object_add(*jso, "details", jso2);
2881     }
2882     return TSS2_RC_SUCCESS;
2883 }
2884 
2885 /** Serialize TPMI_ALG_ASYM_SCHEME to json.
2886  *
2887  * @param[in] in variable to be serialized.
2888  * @param[out] jso pointer to the json object.
2889  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
2890  *         the function.
2891  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
2892  */
2893 TSS2_RC
ifapi_json_TPMI_ALG_ASYM_SCHEME_serialize(const TPMI_ALG_ASYM_SCHEME in,json_object ** jso)2894 ifapi_json_TPMI_ALG_ASYM_SCHEME_serialize(const TPMI_ALG_ASYM_SCHEME in, json_object **jso)
2895 {
2896     CHECK_IN_LIST(TPMI_ALG_ASYM_SCHEME, in, TPM2_ALG_ECDH, TPM2_ALG_RSASSA, TPM2_ALG_RSAPSS,
2897                   TPM2_ALG_ECDSA, TPM2_ALG_ECDAA, TPM2_ALG_SM2, TPM2_ALG_ECSCHNORR,
2898                   TPM2_ALG_RSAES, TPM2_ALG_OAEP, TPM2_ALG_NULL);
2899     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
2900 }
2901 
2902 /**  Serialize a TPMU_ASYM_SCHEME to json.
2903  *
2904  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
2905  * @param[in] in the value to be serialized.
2906  * @param[in] selector the type of the scheme.
2907  * @param[out] jso pointer to the json object.
2908  * @retval TSS2_RC_SUCCESS if the function call was a success.
2909  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2910  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_ASYM_SCHEME.
2911  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2912  */
2913 TSS2_RC
ifapi_json_TPMU_ASYM_SCHEME_serialize(const TPMU_ASYM_SCHEME * in,UINT32 selector,json_object ** jso)2914 ifapi_json_TPMU_ASYM_SCHEME_serialize(const TPMU_ASYM_SCHEME *in, UINT32 selector, json_object **jso)
2915 {
2916     switch (selector) {
2917         case TPM2_ALG_ECDH:
2918             return ifapi_json_TPMS_KEY_SCHEME_ECDH_serialize(&in->ecdh, jso);
2919         case TPM2_ALG_RSASSA:
2920             return ifapi_json_TPMS_SIG_SCHEME_RSASSA_serialize(&in->rsassa, jso);
2921         case TPM2_ALG_RSAPSS:
2922             return ifapi_json_TPMS_SIG_SCHEME_RSAPSS_serialize(&in->rsapss, jso);
2923         case TPM2_ALG_ECDSA:
2924             return ifapi_json_TPMS_SIG_SCHEME_ECDSA_serialize(&in->ecdsa, jso);
2925         case TPM2_ALG_ECDAA:
2926             return ifapi_json_TPMS_SIG_SCHEME_ECDAA_serialize(&in->ecdaa, jso);
2927         case TPM2_ALG_SM2:
2928             return ifapi_json_TPMS_SIG_SCHEME_SM2_serialize(&in->sm2, jso);
2929         case TPM2_ALG_ECSCHNORR:
2930             return ifapi_json_TPMS_SIG_SCHEME_ECSCHNORR_serialize(&in->ecschnorr, jso);
2931         case TPM2_ALG_RSAES:
2932             return ifapi_json_TPMS_ENC_SCHEME_RSAES_serialize(&in->rsaes, jso);
2933         case TPM2_ALG_OAEP:
2934             return ifapi_json_TPMS_ENC_SCHEME_OAEP_serialize(&in->oaep, jso);
2935         default:
2936             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
2937             return TSS2_FAPI_RC_BAD_VALUE;
2938     };
2939     return TSS2_RC_SUCCESS;
2940 }
2941 
2942 /** Serialize value of type TPMT_ASYM_SCHEME to json.
2943  *
2944  * @param[in] in value to be serialized.
2945  * @param[out] jso pointer to the json object.
2946  * @retval TSS2_RC_SUCCESS if the function call was a success.
2947  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2948  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_ASYM_SCHEME.
2949  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2950  */
2951 TSS2_RC
ifapi_json_TPMT_ASYM_SCHEME_serialize(const TPMT_ASYM_SCHEME * in,json_object ** jso)2952 ifapi_json_TPMT_ASYM_SCHEME_serialize(const TPMT_ASYM_SCHEME *in, json_object **jso)
2953 {
2954     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
2955 
2956     TSS2_RC r;
2957     json_object *jso2;
2958     if (*jso == NULL)
2959         *jso = json_object_new_object ();
2960     jso2 = NULL;
2961     r = ifapi_json_TPMI_ALG_ASYM_SCHEME_serialize(in->scheme, &jso2);
2962     return_if_error(r, "Serialize TPMI_ALG_ASYM_SCHEME");
2963 
2964     json_object_object_add(*jso, "scheme", jso2);
2965     if (in->scheme != TPM2_ALG_NULL) {
2966         json_object *jso2 = NULL;
2967         r = ifapi_json_TPMU_ASYM_SCHEME_serialize(&in->details, in->scheme, &jso2);
2968         return_if_error(r,"Serialize TPMU_ASYM_SCHEME");
2969 
2970         json_object_object_add(*jso, "details", jso2);
2971     }
2972     return TSS2_RC_SUCCESS;
2973 }
2974 
2975 /** Serialize TPMI_ALG_RSA_SCHEME to json.
2976  *
2977  * @param[in] in variable to be serialized.
2978  * @param[out] jso pointer to the json object.
2979  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
2980  *         the function.
2981  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
2982  */
2983 TSS2_RC
ifapi_json_TPMI_ALG_RSA_SCHEME_serialize(const TPMI_ALG_RSA_SCHEME in,json_object ** jso)2984 ifapi_json_TPMI_ALG_RSA_SCHEME_serialize(const TPMI_ALG_RSA_SCHEME in, json_object **jso)
2985 {
2986     CHECK_IN_LIST(TPMI_ALG_RSA_SCHEME, in, TPM2_ALG_RSAES, TPM2_ALG_OAEP, TPM2_ALG_RSASSA,
2987                   TPM2_ALG_RSAPSS, TPM2_ALG_NULL);
2988     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
2989 }
2990 
2991 /** Serialize value of type TPMT_RSA_SCHEME to json.
2992  *
2993  * @param[in] in value to be serialized.
2994  * @param[out] jso pointer to the json object.
2995  * @retval TSS2_RC_SUCCESS if the function call was a success.
2996  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
2997  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_RSA_SCHEME.
2998  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
2999  */
3000 TSS2_RC
ifapi_json_TPMT_RSA_SCHEME_serialize(const TPMT_RSA_SCHEME * in,json_object ** jso)3001 ifapi_json_TPMT_RSA_SCHEME_serialize(const TPMT_RSA_SCHEME *in, json_object **jso)
3002 {
3003     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3004 
3005     TSS2_RC r;
3006     json_object *jso2;
3007     if (*jso == NULL)
3008         *jso = json_object_new_object ();
3009     jso2 = NULL;
3010     r = ifapi_json_TPMI_ALG_RSA_SCHEME_serialize(in->scheme, &jso2);
3011     return_if_error(r, "Serialize TPMI_ALG_RSA_SCHEME");
3012 
3013     json_object_object_add(*jso, "scheme", jso2);
3014     if (in->scheme != TPM2_ALG_NULL) {
3015         json_object *jso2 = NULL;
3016         r = ifapi_json_TPMU_ASYM_SCHEME_serialize(&in->details, in->scheme, &jso2);
3017         return_if_error(r,"Serialize TPMU_ASYM_SCHEME");
3018 
3019         json_object_object_add(*jso, "details", jso2);
3020     }
3021     return TSS2_RC_SUCCESS;
3022 }
3023 
3024 /** Serialize value of type TPM2B_PUBLIC_KEY_RSA to json.
3025  *
3026  * @param[in] in value to be serialized.
3027  * @param[out] jso pointer to the json object.
3028  * @retval TSS2_RC_SUCCESS if the function call was a success.
3029  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3030  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_PUBLIC_KEY_RSA.
3031  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3032  */
3033 TSS2_RC
ifapi_json_TPM2B_PUBLIC_KEY_RSA_serialize(const TPM2B_PUBLIC_KEY_RSA * in,json_object ** jso)3034 ifapi_json_TPM2B_PUBLIC_KEY_RSA_serialize(const TPM2B_PUBLIC_KEY_RSA *in, json_object **jso)
3035 {
3036     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3037 
3038     if (in->size > TPM2_MAX_RSA_KEY_BYTES) {
3039         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_RSA_KEY_BYTES)",
3040                   (size_t)in->size, (size_t)TPM2_MAX_RSA_KEY_BYTES);
3041         return TSS2_FAPI_RC_BAD_VALUE;
3042     }
3043     char hex_string[((size_t)in->size)*2+1];
3044 
3045     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
3046         sprintf(&hex_string[off], "%02x", in->buffer[i]);
3047     hex_string[(in->size)*2] = '\0';
3048     *jso = json_object_new_string (hex_string);
3049     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3050 
3051     return TSS2_RC_SUCCESS;
3052 }
3053 
3054 /** Serialize value of type TPMI_RSA_KEY_BITS to json.
3055  *
3056  * @param[in] in value to be serialized.
3057  * @param[out] jso pointer to the json object.
3058  * @retval TSS2_RC_SUCCESS if the function call was a success.
3059  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3060  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMI_RSA_KEY_BITS.
3061  *
3062  */
3063 TSS2_RC
ifapi_json_TPMI_RSA_KEY_BITS_serialize(const TPMI_RSA_KEY_BITS in,json_object ** jso)3064 ifapi_json_TPMI_RSA_KEY_BITS_serialize(const TPMI_RSA_KEY_BITS in, json_object **jso)
3065 {
3066     CHECK_IN_LIST(TPMI_RSA_KEY_BITS, in, 1024, 2048);
3067     return ifapi_json_UINT16_serialize(in, jso);
3068 }
3069 
3070 /** Serialize value of type TPM2B_ECC_PARAMETER to json.
3071  *
3072  * @param[in] in value to be serialized.
3073  * @param[out] jso pointer to the json object.
3074  * @retval TSS2_RC_SUCCESS if the function call was a success.
3075  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3076  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_ECC_PARAMETER.
3077  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3078  */
3079 TSS2_RC
ifapi_json_TPM2B_ECC_PARAMETER_serialize(const TPM2B_ECC_PARAMETER * in,json_object ** jso)3080 ifapi_json_TPM2B_ECC_PARAMETER_serialize(const TPM2B_ECC_PARAMETER *in, json_object **jso)
3081 {
3082     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3083 
3084     if (in->size > TPM2_MAX_ECC_KEY_BYTES) {
3085         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = TPM2_MAX_ECC_KEY_BYTES)",
3086                   (size_t)in->size, (size_t)TPM2_MAX_ECC_KEY_BYTES);
3087         return TSS2_FAPI_RC_BAD_VALUE;
3088     }
3089     char hex_string[((size_t)in->size)*2+1];
3090 
3091     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
3092         sprintf(&hex_string[off], "%02x", in->buffer[i]);
3093     hex_string[(in->size)*2] = '\0';
3094     *jso = json_object_new_string (hex_string);
3095     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3096 
3097     return TSS2_RC_SUCCESS;
3098 }
3099 
3100 /** Serialize value of type TPMS_ECC_POINT to json.
3101  *
3102  * @param[in] in value to be serialized.
3103  * @param[out] jso pointer to the json object.
3104  * @retval TSS2_RC_SUCCESS if the function call was a success.
3105  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3106  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ECC_POINT.
3107  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3108  */
3109 TSS2_RC
ifapi_json_TPMS_ECC_POINT_serialize(const TPMS_ECC_POINT * in,json_object ** jso)3110 ifapi_json_TPMS_ECC_POINT_serialize(const TPMS_ECC_POINT *in, json_object **jso)
3111 {
3112     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3113 
3114     TSS2_RC r;
3115     json_object *jso2;
3116     if (*jso == NULL)
3117         *jso = json_object_new_object ();
3118     jso2 = NULL;
3119     r = ifapi_json_TPM2B_ECC_PARAMETER_serialize(&in->x, &jso2);
3120     return_if_error(r, "Serialize TPM2B_ECC_PARAMETER");
3121 
3122     json_object_object_add(*jso, "x", jso2);
3123     jso2 = NULL;
3124     r = ifapi_json_TPM2B_ECC_PARAMETER_serialize(&in->y, &jso2);
3125     return_if_error(r, "Serialize TPM2B_ECC_PARAMETER");
3126 
3127     json_object_object_add(*jso, "y", jso2);
3128     return TSS2_RC_SUCCESS;
3129 }
3130 
3131 /** Serialize TPMI_ALG_ECC_SCHEME to json.
3132  *
3133  * @param[in] in variable to be serialized.
3134  * @param[out] jso pointer to the json object.
3135  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
3136  *         the function.
3137  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
3138  */
3139 TSS2_RC
ifapi_json_TPMI_ALG_ECC_SCHEME_serialize(const TPMI_ALG_ECC_SCHEME in,json_object ** jso)3140 ifapi_json_TPMI_ALG_ECC_SCHEME_serialize(const TPMI_ALG_ECC_SCHEME in, json_object **jso)
3141 {
3142     CHECK_IN_LIST(TPMI_ALG_ECC_SCHEME, in, TPM2_ALG_ECDSA, TPM2_ALG_ECDAA,
3143                   TPM2_ALG_SM2, TPM2_ALG_ECSCHNORR, TPM2_ALG_ECDH, TPM2_ALG_NULL);
3144     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
3145 }
3146 
3147 /** Serialize value of type TPMI_ECC_CURVE to json.
3148  *
3149  * @param[in] in value to be serialized.
3150  * @param[out] jso pointer to the json object.
3151  * @retval TSS2_RC_SUCCESS if the function call was a success.
3152  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3153  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMI_ECC_CURVE.
3154  *
3155  */
3156 TSS2_RC
ifapi_json_TPMI_ECC_CURVE_serialize(const TPMI_ECC_CURVE in,json_object ** jso)3157 ifapi_json_TPMI_ECC_CURVE_serialize(const TPMI_ECC_CURVE in, json_object **jso)
3158 {
3159     return ifapi_json_TPM2_ECC_CURVE_serialize(in, jso);
3160 }
3161 
3162 /** Serialize value of type TPMT_ECC_SCHEME to json.
3163  *
3164  * @param[in] in value to be serialized.
3165  * @param[out] jso pointer to the json object.
3166  * @retval TSS2_RC_SUCCESS if the function call was a success.
3167  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3168  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_ECC_SCHEME.
3169  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3170  */
3171 TSS2_RC
ifapi_json_TPMT_ECC_SCHEME_serialize(const TPMT_ECC_SCHEME * in,json_object ** jso)3172 ifapi_json_TPMT_ECC_SCHEME_serialize(const TPMT_ECC_SCHEME *in, json_object **jso)
3173 {
3174     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3175 
3176     TSS2_RC r;
3177     json_object *jso2;
3178     if (*jso == NULL)
3179         *jso = json_object_new_object ();
3180     jso2 = NULL;
3181     r = ifapi_json_TPMI_ALG_ECC_SCHEME_serialize(in->scheme, &jso2);
3182     return_if_error(r, "Serialize TPMI_ALG_ECC_SCHEME");
3183 
3184     json_object_object_add(*jso, "scheme", jso2);
3185     if (in->scheme != TPM2_ALG_NULL) {
3186         json_object *jso2 = NULL;
3187         r = ifapi_json_TPMU_ASYM_SCHEME_serialize(&in->details, in->scheme, &jso2);
3188         return_if_error(r,"Serialize TPMU_ASYM_SCHEME");
3189 
3190         json_object_object_add(*jso, "details", jso2);
3191     }
3192     return TSS2_RC_SUCCESS;
3193 }
3194 
3195 /** Serialize value of type TPMS_SIGNATURE_RSA to json.
3196  *
3197  * @param[in] in value to be serialized.
3198  * @param[out] jso pointer to the json object.
3199  * @retval TSS2_RC_SUCCESS if the function call was a success.
3200  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3201  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_RSA.
3202  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3203  */
3204 TSS2_RC
ifapi_json_TPMS_SIGNATURE_RSA_serialize(const TPMS_SIGNATURE_RSA * in,json_object ** jso)3205 ifapi_json_TPMS_SIGNATURE_RSA_serialize(const TPMS_SIGNATURE_RSA *in, json_object **jso)
3206 {
3207     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3208 
3209     TSS2_RC r;
3210     json_object *jso2;
3211     if (*jso == NULL)
3212         *jso = json_object_new_object ();
3213     jso2 = NULL;
3214     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hash, &jso2);
3215     return_if_error(r, "Serialize TPMI_ALG_HASH");
3216 
3217     json_object_object_add(*jso, "hash", jso2);
3218     jso2 = NULL;
3219     r = ifapi_json_TPM2B_PUBLIC_KEY_RSA_serialize(&in->sig, &jso2);
3220     return_if_error(r, "Serialize TPM2B_PUBLIC_KEY_RSA");
3221 
3222     json_object_object_add(*jso, "sig", jso2);
3223     return TSS2_RC_SUCCESS;
3224 }
3225 
3226 /** Serialize a TPMS_SIGNATURE_RSASSA to json.
3227  *
3228  * @param[in] in value of type TPMS_SIGNATURE_RSASSA to be serialized.
3229  * @param[out] jso pointer to the json object.
3230  * @retval TSS2_RC_SUCCESS if the function call was a success.
3231  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3232  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_RSASSA.
3233  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3234  */
3235 TSS2_RC
ifapi_json_TPMS_SIGNATURE_RSASSA_serialize(const TPMS_SIGNATURE_RSASSA * in,json_object ** jso)3236 ifapi_json_TPMS_SIGNATURE_RSASSA_serialize(const TPMS_SIGNATURE_RSASSA *in, json_object **jso)
3237 {
3238     return ifapi_json_TPMS_SIGNATURE_RSA_serialize(in, jso);
3239 }
3240 
3241 /** Serialize a TPMS_SIGNATURE_RSAPSS to json.
3242  *
3243  * @param[in] in value of type TPMS_SIGNATURE_RSAPSS to be serialized.
3244  * @param[out] jso pointer to the json object.
3245  * @retval TSS2_RC_SUCCESS if the function call was a success.
3246  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3247  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_RSAPSS.
3248  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3249  */
3250 TSS2_RC
ifapi_json_TPMS_SIGNATURE_RSAPSS_serialize(const TPMS_SIGNATURE_RSAPSS * in,json_object ** jso)3251 ifapi_json_TPMS_SIGNATURE_RSAPSS_serialize(const TPMS_SIGNATURE_RSAPSS *in, json_object **jso)
3252 {
3253     return ifapi_json_TPMS_SIGNATURE_RSA_serialize(in, jso);
3254 }
3255 
3256 /** Serialize value of type TPMS_SIGNATURE_ECC to json.
3257  *
3258  * @param[in] in value to be serialized.
3259  * @param[out] jso pointer to the json object.
3260  * @retval TSS2_RC_SUCCESS if the function call was a success.
3261  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3262  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_ECC.
3263  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3264  */
3265 TSS2_RC
ifapi_json_TPMS_SIGNATURE_ECC_serialize(const TPMS_SIGNATURE_ECC * in,json_object ** jso)3266 ifapi_json_TPMS_SIGNATURE_ECC_serialize(const TPMS_SIGNATURE_ECC *in, json_object **jso)
3267 {
3268     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3269 
3270     TSS2_RC r;
3271     json_object *jso2;
3272     if (*jso == NULL)
3273         *jso = json_object_new_object ();
3274     jso2 = NULL;
3275     r = ifapi_json_TPMI_ALG_HASH_serialize(in->hash, &jso2);
3276     return_if_error(r, "Serialize TPMI_ALG_HASH");
3277 
3278     json_object_object_add(*jso, "hash", jso2);
3279     jso2 = NULL;
3280     r = ifapi_json_TPM2B_ECC_PARAMETER_serialize(&in->signatureR, &jso2);
3281     return_if_error(r, "Serialize TPM2B_ECC_PARAMETER");
3282 
3283     json_object_object_add(*jso, "signatureR", jso2);
3284     jso2 = NULL;
3285     r = ifapi_json_TPM2B_ECC_PARAMETER_serialize(&in->signatureS, &jso2);
3286     return_if_error(r, "Serialize TPM2B_ECC_PARAMETER");
3287 
3288     json_object_object_add(*jso, "signatureS", jso2);
3289     return TSS2_RC_SUCCESS;
3290 }
3291 
3292 /** Serialize a TPMS_SIGNATURE_ECDSA to json.
3293  *
3294  * @param[in] in value of type TPMS_SIGNATURE_ECDSA to be serialized.
3295  * @param[out] jso pointer to the json object.
3296  * @retval TSS2_RC_SUCCESS if the function call was a success.
3297  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3298  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_ECDSA.
3299  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3300  */
3301 TSS2_RC
ifapi_json_TPMS_SIGNATURE_ECDSA_serialize(const TPMS_SIGNATURE_ECDSA * in,json_object ** jso)3302 ifapi_json_TPMS_SIGNATURE_ECDSA_serialize(const TPMS_SIGNATURE_ECDSA *in, json_object **jso)
3303 {
3304     return ifapi_json_TPMS_SIGNATURE_ECC_serialize(in, jso);
3305 }
3306 
3307 /** Serialize a TPMS_SIGNATURE_ECDAA to json.
3308  *
3309  * @param[in] in value of type TPMS_SIGNATURE_ECDAA to be serialized.
3310  * @param[out] jso pointer to the json object.
3311  * @retval TSS2_RC_SUCCESS if the function call was a success.
3312  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3313  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_ECDAA.
3314  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3315  */
3316 TSS2_RC
ifapi_json_TPMS_SIGNATURE_ECDAA_serialize(const TPMS_SIGNATURE_ECDAA * in,json_object ** jso)3317 ifapi_json_TPMS_SIGNATURE_ECDAA_serialize(const TPMS_SIGNATURE_ECDAA *in, json_object **jso)
3318 {
3319     return ifapi_json_TPMS_SIGNATURE_ECC_serialize(in, jso);
3320 }
3321 
3322 /** Serialize a TPMS_SIGNATURE_SM2 to json.
3323  *
3324  * @param[in] in value of type TPMS_SIGNATURE_SM2 to be serialized.
3325  * @param[out] jso pointer to the json object.
3326  * @retval TSS2_RC_SUCCESS if the function call was a success.
3327  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3328  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_SM2.
3329  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3330  */
3331 TSS2_RC
ifapi_json_TPMS_SIGNATURE_SM2_serialize(const TPMS_SIGNATURE_SM2 * in,json_object ** jso)3332 ifapi_json_TPMS_SIGNATURE_SM2_serialize(const TPMS_SIGNATURE_SM2 *in, json_object **jso)
3333 {
3334     return ifapi_json_TPMS_SIGNATURE_ECC_serialize(in, jso);
3335 }
3336 
3337 /** Serialize a TPMS_SIGNATURE_ECSCHNORR to json.
3338  *
3339  * @param[in] in value of type TPMS_SIGNATURE_ECSCHNORR to be serialized.
3340  * @param[out] jso pointer to the json object.
3341  * @retval TSS2_RC_SUCCESS if the function call was a success.
3342  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3343  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_SIGNATURE_ECSCHNORR.
3344  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3345  */
3346 TSS2_RC
ifapi_json_TPMS_SIGNATURE_ECSCHNORR_serialize(const TPMS_SIGNATURE_ECSCHNORR * in,json_object ** jso)3347 ifapi_json_TPMS_SIGNATURE_ECSCHNORR_serialize(const TPMS_SIGNATURE_ECSCHNORR *in, json_object **jso)
3348 {
3349     return ifapi_json_TPMS_SIGNATURE_ECC_serialize(in, jso);
3350 }
3351 
3352 /**  Serialize a TPMU_SIGNATURE to json.
3353  *
3354  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
3355  * @param[in] in the value to be serialized.
3356  * @param[in] selector the type of the signature.
3357  * @param[out] jso pointer to the json object.
3358  * @retval TSS2_RC_SUCCESS if the function call was a success.
3359  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3360  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_SIGNATURE.
3361  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3362  */
3363 TSS2_RC
ifapi_json_TPMU_SIGNATURE_serialize(const TPMU_SIGNATURE * in,UINT32 selector,json_object ** jso)3364 ifapi_json_TPMU_SIGNATURE_serialize(const TPMU_SIGNATURE *in, UINT32 selector, json_object **jso)
3365 {
3366     switch (selector) {
3367         case TPM2_ALG_RSASSA:
3368             return ifapi_json_TPMS_SIGNATURE_RSASSA_serialize(&in->rsassa, jso);
3369         case TPM2_ALG_RSAPSS:
3370             return ifapi_json_TPMS_SIGNATURE_RSAPSS_serialize(&in->rsapss, jso);
3371         case TPM2_ALG_ECDSA:
3372             return ifapi_json_TPMS_SIGNATURE_ECDSA_serialize(&in->ecdsa, jso);
3373         case TPM2_ALG_ECDAA:
3374             return ifapi_json_TPMS_SIGNATURE_ECDAA_serialize(&in->ecdaa, jso);
3375         case TPM2_ALG_SM2:
3376             return ifapi_json_TPMS_SIGNATURE_SM2_serialize(&in->sm2, jso);
3377         case TPM2_ALG_ECSCHNORR:
3378             return ifapi_json_TPMS_SIGNATURE_ECSCHNORR_serialize(&in->ecschnorr, jso);
3379         case TPM2_ALG_HMAC:
3380             return ifapi_json_TPMT_HA_serialize(&in->hmac, jso);
3381         default:
3382             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
3383             return TSS2_FAPI_RC_BAD_VALUE;
3384     };
3385     return TSS2_RC_SUCCESS;
3386 }
3387 
3388 /** Serialize value of type TPMT_SIGNATURE to json.
3389  *
3390  * @param[in] in value to be serialized.
3391  * @param[out] jso pointer to the json object.
3392  * @retval TSS2_RC_SUCCESS if the function call was a success.
3393  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3394  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_SIGNATURE.
3395  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3396  */
3397 TSS2_RC
ifapi_json_TPMT_SIGNATURE_serialize(const TPMT_SIGNATURE * in,json_object ** jso)3398 ifapi_json_TPMT_SIGNATURE_serialize(const TPMT_SIGNATURE *in, json_object **jso)
3399 {
3400     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3401 
3402     TSS2_RC r;
3403     json_object *jso2;
3404     if (*jso == NULL)
3405         *jso = json_object_new_object ();
3406     jso2 = NULL;
3407     r = ifapi_json_TPMI_ALG_SIG_SCHEME_serialize(in->sigAlg, &jso2);
3408     return_if_error(r, "Serialize TPMI_ALG_SIG_SCHEME");
3409 
3410     json_object_object_add(*jso, "sigAlg", jso2);
3411     if (in->sigAlg != TPM2_ALG_NULL) {
3412         json_object *jso2 = NULL;
3413         r = ifapi_json_TPMU_SIGNATURE_serialize(&in->signature, in->sigAlg, &jso2);
3414         return_if_error(r,"Serialize TPMU_SIGNATURE");
3415 
3416         json_object_object_add(*jso, "signature", jso2);
3417     }
3418     return TSS2_RC_SUCCESS;
3419 }
3420 
3421 /** Serialize value of type TPM2B_ENCRYPTED_SECRET to json.
3422  *
3423  * @param[in] in value to be serialized.
3424  * @param[out] jso pointer to the json object.
3425  * @retval TSS2_RC_SUCCESS if the function call was a success.
3426  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3427  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_ENCRYPTED_SECRET.
3428  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3429  */
3430 TSS2_RC
ifapi_json_TPM2B_ENCRYPTED_SECRET_serialize(const TPM2B_ENCRYPTED_SECRET * in,json_object ** jso)3431 ifapi_json_TPM2B_ENCRYPTED_SECRET_serialize(const TPM2B_ENCRYPTED_SECRET *in, json_object **jso)
3432 {
3433     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3434 
3435     if (in->size > sizeof(TPMU_ENCRYPTED_SECRET)) {
3436         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = sizeof(TPMU_ENCRYPTED_SECRET))",
3437                   (size_t)in->size, (size_t)sizeof(TPMU_ENCRYPTED_SECRET));
3438         return TSS2_FAPI_RC_BAD_VALUE;
3439     }
3440     char hex_string[((size_t)in->size)*2+1];
3441 
3442     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
3443         sprintf(&hex_string[off], "%02x", in->secret[i]);
3444     hex_string[(in->size)*2] = '\0';
3445     *jso = json_object_new_string (hex_string);
3446     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3447 
3448     return TSS2_RC_SUCCESS;
3449 }
3450 
3451 /** Serialize TPMI_ALG_PUBLIC to json.
3452  *
3453  * @param[in] in variable to be serialized.
3454  * @param[out] jso pointer to the json object.
3455  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
3456  *         the function.
3457  * @retval TSS2_FAPI_RC_MEMORY if not enough memory can be allocated.
3458  */
3459 TSS2_RC
ifapi_json_TPMI_ALG_PUBLIC_serialize(const TPMI_ALG_PUBLIC in,json_object ** jso)3460 ifapi_json_TPMI_ALG_PUBLIC_serialize(const TPMI_ALG_PUBLIC in, json_object **jso)
3461 {
3462     CHECK_IN_LIST(TPMI_ALG_PUBLIC, in, TPM2_ALG_RSA, TPM2_ALG_KEYEDHASH,
3463                   TPM2_ALG_ECC, TPM2_ALG_SYMCIPHER, TPM2_ALG_NULL);
3464     return ifapi_json_TPM2_ALG_ID_serialize(in, jso);
3465 }
3466 
3467 /**  Serialize a TPMU_PUBLIC_ID to json.
3468  *
3469  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
3470  * @param[in] in the value to be serialized.
3471  * @param[in] selector the type of the public ID.
3472  * @param[out] jso pointer to the json object.
3473  * @retval TSS2_RC_SUCCESS if the function call was a success.
3474  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3475  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_PUBLIC_ID.
3476  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3477  */
3478 TSS2_RC
ifapi_json_TPMU_PUBLIC_ID_serialize(const TPMU_PUBLIC_ID * in,UINT32 selector,json_object ** jso)3479 ifapi_json_TPMU_PUBLIC_ID_serialize(const TPMU_PUBLIC_ID *in, UINT32 selector, json_object **jso)
3480 {
3481     switch (selector) {
3482         case TPM2_ALG_KEYEDHASH:
3483             return ifapi_json_TPM2B_DIGEST_serialize(&in->keyedHash, jso);
3484         case TPM2_ALG_SYMCIPHER:
3485             return ifapi_json_TPM2B_DIGEST_serialize(&in->sym, jso);
3486         case TPM2_ALG_RSA:
3487             return ifapi_json_TPM2B_PUBLIC_KEY_RSA_serialize(&in->rsa, jso);
3488         case TPM2_ALG_ECC:
3489             return ifapi_json_TPMS_ECC_POINT_serialize(&in->ecc, jso);
3490         default:
3491             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
3492             return TSS2_FAPI_RC_BAD_VALUE;
3493     };
3494     return TSS2_RC_SUCCESS;
3495 }
3496 
3497 /** Serialize value of type TPMS_KEYEDHASH_PARMS to json.
3498  *
3499  * @param[in] in value to be serialized.
3500  * @param[out] jso pointer to the json object.
3501  * @retval TSS2_RC_SUCCESS if the function call was a success.
3502  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3503  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_KEYEDHASH_PARMS.
3504  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3505  */
3506 TSS2_RC
ifapi_json_TPMS_KEYEDHASH_PARMS_serialize(const TPMS_KEYEDHASH_PARMS * in,json_object ** jso)3507 ifapi_json_TPMS_KEYEDHASH_PARMS_serialize(const TPMS_KEYEDHASH_PARMS *in, json_object **jso)
3508 {
3509     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3510 
3511     TSS2_RC r;
3512     json_object *jso2;
3513     if (*jso == NULL)
3514         *jso = json_object_new_object ();
3515     jso2 = NULL;
3516     r = ifapi_json_TPMT_KEYEDHASH_SCHEME_serialize(&in->scheme, &jso2);
3517     return_if_error(r, "Serialize TPMT_KEYEDHASH_SCHEME");
3518 
3519     json_object_object_add(*jso, "scheme", jso2);
3520     return TSS2_RC_SUCCESS;
3521 }
3522 
3523 /** Serialize value of type TPMS_ASYM_PARMS to json.
3524  *
3525  * @param[in] in value to be serialized.
3526  * @param[out] jso pointer to the json object.
3527  * @retval TSS2_RC_SUCCESS if the function call was a success.
3528  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3529  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ASYM_PARMS.
3530  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3531  */
3532 TSS2_RC
ifapi_json_TPMS_ASYM_PARMS_serialize(const TPMS_ASYM_PARMS * in,json_object ** jso)3533 ifapi_json_TPMS_ASYM_PARMS_serialize(const TPMS_ASYM_PARMS *in, json_object **jso)
3534 {
3535     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3536 
3537     TSS2_RC r;
3538     json_object *jso2;
3539     if (*jso == NULL)
3540         *jso = json_object_new_object ();
3541     jso2 = NULL;
3542     r = ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(&in->symmetric, &jso2);
3543     return_if_error(r, "Serialize TPMT_SYM_DEF_OBJECT");
3544 
3545     json_object_object_add(*jso, "symmetric", jso2);
3546     jso2 = NULL;
3547     r = ifapi_json_TPMT_ASYM_SCHEME_serialize(&in->scheme, &jso2);
3548     return_if_error(r, "Serialize TPMT_ASYM_SCHEME");
3549 
3550     json_object_object_add(*jso, "scheme", jso2);
3551     return TSS2_RC_SUCCESS;
3552 }
3553 
3554 /** Serialize value of type TPMS_RSA_PARMS to json.
3555  *
3556  * @param[in] in value to be serialized.
3557  * @param[out] jso pointer to the json object.
3558  * @retval TSS2_RC_SUCCESS if the function call was a success.
3559  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3560  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_RSA_PARMS.
3561  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3562  */
3563 TSS2_RC
ifapi_json_TPMS_RSA_PARMS_serialize(const TPMS_RSA_PARMS * in,json_object ** jso)3564 ifapi_json_TPMS_RSA_PARMS_serialize(const TPMS_RSA_PARMS *in, json_object **jso)
3565 {
3566     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3567 
3568     TSS2_RC r;
3569     json_object *jso2;
3570     if (*jso == NULL)
3571         *jso = json_object_new_object ();
3572     jso2 = NULL;
3573     r = ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(&in->symmetric, &jso2);
3574     return_if_error(r, "Serialize TPMT_SYM_DEF_OBJECT");
3575 
3576     json_object_object_add(*jso, "symmetric", jso2);
3577     jso2 = NULL;
3578     r = ifapi_json_TPMT_RSA_SCHEME_serialize(&in->scheme, &jso2);
3579     return_if_error(r, "Serialize TPMT_RSA_SCHEME");
3580 
3581     json_object_object_add(*jso, "scheme", jso2);
3582     jso2 = NULL;
3583     r = ifapi_json_TPMI_RSA_KEY_BITS_serialize(in->keyBits, &jso2);
3584     return_if_error(r, "Serialize TPMI_RSA_KEY_BITS");
3585 
3586     json_object_object_add(*jso, "keyBits", jso2);
3587     jso2 = NULL;
3588     r = ifapi_json_UINT32_serialize(in->exponent, &jso2);
3589     return_if_error(r, "Serialize UINT32");
3590 
3591     json_object_object_add(*jso, "exponent", jso2);
3592     return TSS2_RC_SUCCESS;
3593 }
3594 
3595 /** Serialize value of type TPMS_ECC_PARMS to json.
3596  *
3597  * @param[in] in value to be serialized.
3598  * @param[out] jso pointer to the json object.
3599  * @retval TSS2_RC_SUCCESS if the function call was a success.
3600  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3601  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_ECC_PARMS.
3602  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3603  */
3604 TSS2_RC
ifapi_json_TPMS_ECC_PARMS_serialize(const TPMS_ECC_PARMS * in,json_object ** jso)3605 ifapi_json_TPMS_ECC_PARMS_serialize(const TPMS_ECC_PARMS *in, json_object **jso)
3606 {
3607     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3608 
3609     TSS2_RC r;
3610     json_object *jso2;
3611     if (*jso == NULL)
3612         *jso = json_object_new_object ();
3613     jso2 = NULL;
3614     r = ifapi_json_TPMT_SYM_DEF_OBJECT_serialize(&in->symmetric, &jso2);
3615     return_if_error(r, "Serialize TPMT_SYM_DEF_OBJECT");
3616 
3617     json_object_object_add(*jso, "symmetric", jso2);
3618     jso2 = NULL;
3619     r = ifapi_json_TPMT_ECC_SCHEME_serialize(&in->scheme, &jso2);
3620     return_if_error(r, "Serialize TPMT_ECC_SCHEME");
3621 
3622     json_object_object_add(*jso, "scheme", jso2);
3623     jso2 = NULL;
3624     r = ifapi_json_TPMI_ECC_CURVE_serialize(in->curveID, &jso2);
3625     return_if_error(r, "Serialize TPMI_ECC_CURVE");
3626 
3627     json_object_object_add(*jso, "curveID", jso2);
3628     jso2 = NULL;
3629     r = ifapi_json_TPMT_KDF_SCHEME_serialize(&in->kdf, &jso2);
3630     return_if_error(r, "Serialize TPMT_KDF_SCHEME");
3631 
3632     json_object_object_add(*jso, "kdf", jso2);
3633     return TSS2_RC_SUCCESS;
3634 }
3635 
3636 /**  Serialize a TPMU_PUBLIC_PARMS to json.
3637  *
3638  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
3639  * @param[in] in the value to be serialized.
3640  * @param[in] selector the type of the public parameters.
3641  * @param[out] jso pointer to the json object.
3642  * @retval TSS2_RC_SUCCESS if the function call was a success.
3643  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3644  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_PUBLIC_PARMS.
3645  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3646  */
3647 TSS2_RC
ifapi_json_TPMU_PUBLIC_PARMS_serialize(const TPMU_PUBLIC_PARMS * in,UINT32 selector,json_object ** jso)3648 ifapi_json_TPMU_PUBLIC_PARMS_serialize(const TPMU_PUBLIC_PARMS *in, UINT32 selector, json_object **jso)
3649 {
3650     switch (selector) {
3651         case TPM2_ALG_KEYEDHASH:
3652             return ifapi_json_TPMS_KEYEDHASH_PARMS_serialize(&in->keyedHashDetail, jso);
3653         case TPM2_ALG_SYMCIPHER:
3654             return ifapi_json_TPMS_SYMCIPHER_PARMS_serialize(&in->symDetail, jso);
3655         case TPM2_ALG_RSA:
3656             return ifapi_json_TPMS_RSA_PARMS_serialize(&in->rsaDetail, jso);
3657         case TPM2_ALG_ECC:
3658             return ifapi_json_TPMS_ECC_PARMS_serialize(&in->eccDetail, jso);
3659         default:
3660             LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
3661             return TSS2_FAPI_RC_BAD_VALUE;
3662     };
3663     return TSS2_RC_SUCCESS;
3664 }
3665 
3666 /** Serialize value of type TPMT_PUBLIC to json.
3667  *
3668  * @param[in] in value to be serialized.
3669  * @param[out] jso pointer to the json object.
3670  * @retval TSS2_RC_SUCCESS if the function call was a success.
3671  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3672  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_PUBLIC.
3673  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3674  */
3675 TSS2_RC
ifapi_json_TPMT_PUBLIC_serialize(const TPMT_PUBLIC * in,json_object ** jso)3676 ifapi_json_TPMT_PUBLIC_serialize(const TPMT_PUBLIC *in, json_object **jso)
3677 {
3678     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3679 
3680     TSS2_RC r;
3681     json_object *jso2;
3682     if (*jso == NULL)
3683         *jso = json_object_new_object ();
3684     jso2 = NULL;
3685     r = ifapi_json_TPMI_ALG_PUBLIC_serialize(in->type, &jso2);
3686     return_if_error(r, "Serialize TPMI_ALG_PUBLIC");
3687 
3688     json_object_object_add(*jso, "type", jso2);
3689     jso2 = NULL;
3690     r = ifapi_json_TPMI_ALG_HASH_serialize(in->nameAlg, &jso2);
3691     return_if_error(r, "Serialize TPMI_ALG_HASH");
3692 
3693     json_object_object_add(*jso, "nameAlg", jso2);
3694     jso2 = NULL;
3695     r = ifapi_json_TPMA_OBJECT_serialize(in->objectAttributes, &jso2);
3696     return_if_error(r, "Serialize TPMA_OBJECT");
3697 
3698     json_object_object_add(*jso, "objectAttributes", jso2);
3699     jso2 = NULL;
3700     r = ifapi_json_TPM2B_DIGEST_serialize(&in->authPolicy, &jso2);
3701     return_if_error(r, "Serialize TPM2B_DIGEST");
3702 
3703     json_object_object_add(*jso, "authPolicy", jso2);
3704     jso2 = NULL;
3705     r = ifapi_json_TPMU_PUBLIC_PARMS_serialize(&in->parameters, in->type, &jso2);
3706     return_if_error(r,"Serialize TPMU_PUBLIC_PARMS");
3707 
3708     json_object_object_add(*jso, "parameters", jso2);
3709     jso2 = NULL;
3710     r = ifapi_json_TPMU_PUBLIC_ID_serialize(&in->unique, in->type, &jso2);
3711     return_if_error(r,"Serialize TPMU_PUBLIC_ID");
3712 
3713     json_object_object_add(*jso, "unique", jso2);
3714     return TSS2_RC_SUCCESS;
3715 }
3716 
3717 /** Serialize a TPM2B_PUBLIC to json.
3718  *
3719  * @param[in] in value to be serialized.
3720  * @param[out] jso pointer to the json object.
3721  * @retval TSS2_RC_SUCCESS if the function call was a success.
3722  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3723  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_PUBLIC.
3724  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3725  */
3726 TSS2_RC
ifapi_json_TPM2B_PUBLIC_serialize(const TPM2B_PUBLIC * in,json_object ** jso)3727 ifapi_json_TPM2B_PUBLIC_serialize(const TPM2B_PUBLIC *in, json_object **jso)
3728 {
3729     if (*jso == NULL)
3730         *jso = json_object_new_object ();
3731     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3732 
3733     json_object *jso2;
3734 
3735     jso2 = NULL;
3736     if (ifapi_json_UINT16_serialize(in->size, &jso2))
3737         return TSS2_FAPI_RC_BAD_VALUE;
3738 
3739     json_object_object_add(*jso, "size", jso2);
3740 
3741     jso2 = NULL;
3742     if (ifapi_json_TPMT_PUBLIC_serialize(&in->publicArea, &jso2))
3743         return TSS2_FAPI_RC_BAD_VALUE;
3744 
3745     json_object_object_add(*jso, "publicArea", jso2);
3746 
3747     return TSS2_RC_SUCCESS;
3748 }
3749 
3750 /** Serialize value of type TPM2B_PRIVATE to json.
3751  *
3752  * @param[in] in value to be serialized.
3753  * @param[out] jso pointer to the json object.
3754  * @retval TSS2_RC_SUCCESS if the function call was a success.
3755  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3756  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_PRIVATE.
3757  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3758  */
3759 TSS2_RC
ifapi_json_TPM2B_PRIVATE_serialize(const TPM2B_PRIVATE * in,json_object ** jso)3760 ifapi_json_TPM2B_PRIVATE_serialize(const TPM2B_PRIVATE *in, json_object **jso)
3761 {
3762     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3763 
3764     if (in->size > sizeof(_PRIVATE)) {
3765         LOG_ERROR("Too many bytes for array (%"PRIuPTR" > %"PRIuPTR" = sizeof(_PRIVATE))",
3766                   (size_t)in->size, (size_t)sizeof(_PRIVATE));
3767         return TSS2_FAPI_RC_BAD_VALUE;
3768     }
3769     char hex_string[((size_t)in->size)*2+1];
3770 
3771     for (size_t i = 0, off = 0; i < in->size; i++, off+=2)
3772         sprintf(&hex_string[off], "%02x", in->buffer[i]);
3773     hex_string[(in->size)*2] = '\0';
3774     *jso = json_object_new_string (hex_string);
3775     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3776 
3777     return TSS2_RC_SUCCESS;
3778 }
3779 
3780 /** Serialize TPM2_NT to json.
3781  *
3782  * @param[in] in constant to be serialized.
3783  * @param[out] jso pointer to the json object.
3784  * @retval TSS2_RC_SUCCESS if the function call was a success.
3785  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3786  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPM2_NT.
3787  */
3788 TSS2_RC
ifapi_json_TPM2_NT_serialize(const TPM2_NT in,json_object ** jso)3789 ifapi_json_TPM2_NT_serialize(const TPM2_NT in, json_object **jso)
3790 {
3791     static const struct { TPM2_NT in; char *name; } tab[] = {
3792         { TPM2_NT_ORDINARY, "ORDINARY" },
3793         { TPM2_NT_COUNTER, "COUNTER" },
3794         { TPM2_NT_BITS, "BITS" },
3795         { TPM2_NT_EXTEND, "EXTEND" },
3796         { TPM2_NT_PIN_FAIL, "PIN_FAIL" },
3797         { TPM2_NT_PIN_PASS, "PIN_PASS" },
3798     };
3799 
3800     for (size_t i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
3801         if (tab[i].in == in) {
3802             *jso = json_object_new_string(tab[i].name);
3803             check_oom(*jso);
3804             return TSS2_RC_SUCCESS;
3805         }
3806     }
3807     return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
3808 }
3809 
3810 /** Serialize a TPMA_NV to json.
3811  *
3812  * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
3813  *
3814  * @param[in] in value to be serialized.
3815  * @param[out] jso pointer to the json object.
3816  * @retval TSS2_RC_SUCCESS if the function call was a success.
3817  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3818  * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMA_NV.
3819  */
3820 TSS2_RC
ifapi_json_TPMA_NV_serialize(const TPMA_NV in,json_object ** jso)3821 ifapi_json_TPMA_NV_serialize(const TPMA_NV in, json_object **jso)
3822 {
3823     struct { TPMA_NV in; char *name; } tab[] = {
3824         { TPMA_NV_PPWRITE, "PPWRITE" },
3825         { TPMA_NV_OWNERWRITE, "OWNERWRITE" },
3826         { TPMA_NV_AUTHWRITE, "AUTHWRITE" },
3827         { TPMA_NV_POLICYWRITE, "POLICYWRITE" },
3828         { TPMA_NV_POLICY_DELETE, "POLICY_DELETE" },
3829         { TPMA_NV_WRITELOCKED, "WRITELOCKED" },
3830         { TPMA_NV_WRITEALL, "WRITEALL" },
3831         { TPMA_NV_WRITEDEFINE, "WRITEDEFINE" },
3832         { TPMA_NV_WRITE_STCLEAR, "WRITE_STCLEAR" },
3833         { TPMA_NV_GLOBALLOCK, "GLOBALLOCK" },
3834         { TPMA_NV_PPREAD, "PPREAD" },
3835         { TPMA_NV_OWNERREAD, "OWNERREAD" },
3836         { TPMA_NV_AUTHREAD, "AUTHREAD" },
3837         { TPMA_NV_POLICYREAD, "POLICYREAD" },
3838         { TPMA_NV_NO_DA, "NO_DA" },
3839         { TPMA_NV_ORDERLY, "ORDERLY" },
3840         { TPMA_NV_CLEAR_STCLEAR, "CLEAR_STCLEAR" },
3841         { TPMA_NV_READLOCKED, "READLOCKED" },
3842         { TPMA_NV_WRITTEN, "WRITTEN" },
3843         { TPMA_NV_PLATFORMCREATE, "PLATFORMCREATE" },
3844         { TPMA_NV_READ_STCLEAR, "READ_STCLEAR" },
3845     };
3846 
3847     UINT32 input;
3848     input = (UINT32) in;
3849     json_object *jso_bit;
3850     if (*jso == NULL) {
3851         *jso = json_object_new_object();
3852         return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3853     }
3854     size_t n = sizeof(tab) / sizeof(tab[0]);
3855     size_t i;
3856     for (i = 0; i < n; i++) {
3857         if (tab[i].in & input)
3858             jso_bit = json_object_new_int(1);
3859         else
3860             jso_bit = json_object_new_int(0);
3861         return_if_null(jso_bit, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3862 
3863         json_object_object_add(*jso, tab[i].name, jso_bit);
3864     }
3865     TPM2_NT input2 = (TPMA_NV_TPM2_NT_MASK & input)>>4;
3866     json_object *jso2 = NULL;
3867     TSS2_RC r = ifapi_json_TPM2_NT_serialize(input2, &jso2);
3868     return_if_error(r, "Bad value");
3869 
3870     json_object_object_add(*jso, "TPM2_NT", jso2);
3871 
3872     return TSS2_RC_SUCCESS;
3873 }
3874 
3875 /** Serialize value of type TPMS_NV_PUBLIC to json.
3876  *
3877  * @param[in] in value to be serialized.
3878  * @param[out] jso pointer to the json object.
3879  * @retval TSS2_RC_SUCCESS if the function call was a success.
3880  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3881  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_NV_PUBLIC.
3882  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3883  */
3884 TSS2_RC
ifapi_json_TPMS_NV_PUBLIC_serialize(const TPMS_NV_PUBLIC * in,json_object ** jso)3885 ifapi_json_TPMS_NV_PUBLIC_serialize(const TPMS_NV_PUBLIC *in, json_object **jso)
3886 {
3887     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3888 
3889     TSS2_RC r;
3890     json_object *jso2;
3891     if (*jso == NULL)
3892         *jso = json_object_new_object ();
3893     jso2 = NULL;
3894     r = ifapi_json_TPMI_RH_NV_INDEX_serialize(in->nvIndex, &jso2);
3895     return_if_error(r, "Serialize TPMI_RH_NV_INDEX");
3896 
3897     json_object_object_add(*jso, "nvIndex", jso2);
3898     jso2 = NULL;
3899     r = ifapi_json_TPMI_ALG_HASH_serialize(in->nameAlg, &jso2);
3900     return_if_error(r, "Serialize TPMI_ALG_HASH");
3901 
3902     json_object_object_add(*jso, "nameAlg", jso2);
3903     jso2 = NULL;
3904     r = ifapi_json_TPMA_NV_serialize(in->attributes, &jso2);
3905     return_if_error(r, "Serialize TPMA_NV");
3906 
3907     json_object_object_add(*jso, "attributes", jso2);
3908     jso2 = NULL;
3909     r = ifapi_json_TPM2B_DIGEST_serialize(&in->authPolicy, &jso2);
3910     return_if_error(r, "Serialize TPM2B_DIGEST");
3911 
3912     json_object_object_add(*jso, "authPolicy", jso2);
3913     jso2 = NULL;
3914     r = ifapi_json_UINT16_serialize(in->dataSize, &jso2);
3915     return_if_error(r, "Serialize UINT16");
3916 
3917     json_object_object_add(*jso, "dataSize", jso2);
3918     return TSS2_RC_SUCCESS;
3919 }
3920 
3921 /** Serialize a TPM2B_NV_PUBLIC to json.
3922  *
3923  * @param[in] in value to be serialized.
3924  * @param[out] jso pointer to the json object.
3925  * @retval TSS2_RC_SUCCESS if the function call was a success.
3926  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3927  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_NV_PUBLIC.
3928  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3929  */
3930 TSS2_RC
ifapi_json_TPM2B_NV_PUBLIC_serialize(const TPM2B_NV_PUBLIC * in,json_object ** jso)3931 ifapi_json_TPM2B_NV_PUBLIC_serialize(const TPM2B_NV_PUBLIC *in, json_object **jso)
3932 {
3933     if (*jso == NULL)
3934         *jso = json_object_new_object ();
3935     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
3936 
3937     json_object *jso2;
3938 
3939     jso2 = NULL;
3940     if (ifapi_json_UINT16_serialize(in->size, &jso2))
3941         return TSS2_FAPI_RC_BAD_VALUE;
3942 
3943     json_object_object_add(*jso, "size", jso2);
3944 
3945     jso2 = NULL;
3946     if (ifapi_json_TPMS_NV_PUBLIC_serialize(&in->nvPublic, &jso2))
3947         return TSS2_FAPI_RC_BAD_VALUE;
3948 
3949     json_object_object_add(*jso, "nvPublic", jso2);
3950 
3951     return TSS2_RC_SUCCESS;
3952 }
3953 
3954 /** Serialize value of type TPMS_CREATION_DATA to json.
3955  *
3956  * @param[in] in value to be serialized.
3957  * @param[out] jso pointer to the json object.
3958  * @retval TSS2_RC_SUCCESS if the function call was a success.
3959  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
3960  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_CREATION_DATA.
3961  * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
3962  */
3963 TSS2_RC
ifapi_json_TPMS_CREATION_DATA_serialize(const TPMS_CREATION_DATA * in,json_object ** jso)3964 ifapi_json_TPMS_CREATION_DATA_serialize(const TPMS_CREATION_DATA *in, json_object **jso)
3965 {
3966     return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
3967 
3968     TSS2_RC r;
3969     json_object *jso2;
3970     if (*jso == NULL)
3971         *jso = json_object_new_object ();
3972     jso2 = NULL;
3973     r = ifapi_json_TPML_PCR_SELECTION_serialize(&in->pcrSelect, &jso2);
3974     return_if_error(r, "Serialize TPML_PCR_SELECTION");
3975 
3976     json_object_object_add(*jso, "pcrSelect", jso2);
3977     jso2 = NULL;
3978     r = ifapi_json_TPM2B_DIGEST_serialize(&in->pcrDigest, &jso2);
3979     return_if_error(r, "Serialize TPM2B_DIGEST");
3980 
3981     json_object_object_add(*jso, "pcrDigest", jso2);
3982     jso2 = NULL;
3983     r = ifapi_json_TPMA_LOCALITY_serialize(in->locality, &jso2);
3984     return_if_error(r, "Serialize TPMA_LOCALITY");
3985 
3986     json_object_object_add(*jso, "locality", jso2);
3987     jso2 = NULL;
3988     r = ifapi_json_TPM2_ALG_ID_serialize(in->parentNameAlg, &jso2);
3989     return_if_error(r, "Serialize TPM2_ALG_ID");
3990 
3991     json_object_object_add(*jso, "parentNameAlg", jso2);
3992     jso2 = NULL;
3993     r = ifapi_json_TPM2B_NAME_serialize(&in->parentName, &jso2);
3994     return_if_error(r, "Serialize TPM2B_NAME");
3995 
3996     json_object_object_add(*jso, "parentName", jso2);
3997     jso2 = NULL;
3998     r = ifapi_json_TPM2B_NAME_serialize(&in->parentQualifiedName, &jso2);
3999     return_if_error(r, "Serialize TPM2B_NAME");
4000 
4001     json_object_object_add(*jso, "parentQualifiedName", jso2);
4002     jso2 = NULL;
4003     r = ifapi_json_TPM2B_DATA_serialize(&in->outsideInfo, &jso2);
4004     return_if_error(r, "Serialize TPM2B_DATA");
4005 
4006     json_object_object_add(*jso, "outsideInfo", jso2);
4007     return TSS2_RC_SUCCESS;
4008 }
4009 
4010 /** Serialize a TPM2B_CREATION_DATA to json.
4011  *
4012  * @param[in] in value to be serialized.
4013  * @param[out] jso pointer to the json object.
4014  * @retval TSS2_RC_SUCCESS if the function call was a success.
4015  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
4016  * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_CREATION_DATA.
4017  */
4018 TSS2_RC
ifapi_json_TPM2B_CREATION_DATA_serialize(const TPM2B_CREATION_DATA * in,json_object ** jso)4019 ifapi_json_TPM2B_CREATION_DATA_serialize(const TPM2B_CREATION_DATA *in, json_object **jso)
4020 {
4021     if (*jso == NULL)
4022         *jso = json_object_new_object ();
4023     return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
4024 
4025     json_object *jso2;
4026 
4027     jso2 = NULL;
4028     if (ifapi_json_UINT16_serialize(in->size, &jso2))
4029         return TSS2_FAPI_RC_BAD_VALUE;
4030 
4031     json_object_object_add(*jso, "size", jso2);
4032 
4033     jso2 = NULL;
4034     if (ifapi_json_TPMS_CREATION_DATA_serialize(&in->creationData, &jso2))
4035         return TSS2_FAPI_RC_BAD_VALUE;
4036 
4037     json_object_object_add(*jso, "creationData", jso2);
4038 
4039     return TSS2_RC_SUCCESS;
4040 }
4041