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 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <stdio.h>
11
12 #include "tpm_json_serialize.h"
13 #include "fapi_policy.h"
14 #include "ifapi_policy_json_serialize.h"
15
16 #define LOGMODULE fapijson
17 #include "util/log.h"
18 #include "util/aux_util.h"
19
20
21 /** Serialize a character string to json.
22 *
23 * @param[in]in the character string.
24 * @param[out] out the json object.
25 * @retval TSS2_RC_SUCCESS if the function call was a success.
26 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
27 */
28 static TSS2_RC
ifapi_json_char_serialize(const char * in,json_object ** jso)29 ifapi_json_char_serialize(
30 const char *in,
31 json_object **jso)
32 {
33 if (in == NULL) {
34 *jso = json_object_new_string("");
35 } else {
36 *jso = json_object_new_string(in);
37 }
38 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
39 return TSS2_RC_SUCCESS;
40 }
41
42 typedef struct {
43 TPMI_POLICYTYPE in;
44 char *name;
45 } TPMI_POLICYTYPE_ASSIGN;
46
47 static TPMI_POLICYTYPE_ASSIGN serialize_TPMI_POLICYTYPE_tab[] = {
48 { POLICYOR, "POLICYOR" },
49 { POLICYSIGNED, "POLICYSIGNED" },
50 { POLICYSECRET, "POLICYSECRET" },
51 { POLICYPCR, "POLICYPCR" },
52 { POLICYLOCALITY, "POLICYLOCALITY" },
53 { POLICYNV, "POLICYNV" },
54 { POLICYCOUNTERTIMER, "POLICYCOUNTERTIMER" },
55 { POLICYCOMMANDCODE, "POLICYCOMMANDCODE" },
56 { POLICYPHYSICALPRESENCE, "POLICYPHYSICALPRESENCE" },
57 { POLICYCPHASH, "POLICYCPHASH" },
58 { POLICYNAMEHASH, "POLICYNAMEHASH" },
59 { POLICYDUPLICATIONSELECT, "POLICYDUPLICATIONSELECT" },
60 { POLICYAUTHORIZE, "POLICYAUTHORIZE" },
61 { POLICYAUTHVALUE, "POLICYAUTHVALUE" },
62 { POLICYPASSWORD, "POLICYPASSWORD" },
63 { POLICYNVWRITTEN, "POLICYNVWRITTEN" },
64 { POLICYTEMPLATE, "POLICYTEMPLATE" },
65 { POLICYAUTHORIZENV, "POLICYAUTHORIZENV" },
66 { POLICYACTION, "POLICYACTION" },
67 };
68
69 /** Get json object for a constant, if a variable is actually of type TPMI_POLICYTYPE.
70 *
71 * @param[in] in binary value of constant.
72 * @param[out] str_jso with text representing the constant.
73 * @retval TSS2_RC_SUCCESS if the function call was a success.
74 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
75 * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMI_POLICYTYPE.
76 */
77 TSS2_RC
ifapi_json_TPMI_POLICYTYPE_serialize_txt(const TPMI_POLICYTYPE in,json_object ** str_jso)78 ifapi_json_TPMI_POLICYTYPE_serialize_txt(
79 const TPMI_POLICYTYPE in,
80 json_object **str_jso)
81 {
82 size_t n = sizeof(serialize_TPMI_POLICYTYPE_tab) / sizeof(
83 serialize_TPMI_POLICYTYPE_tab[0]);
84 size_t i;
85 for (i = 0; i < n; i++) {
86 if (serialize_TPMI_POLICYTYPE_tab[i].in == in) {
87 *str_jso = json_object_new_string(serialize_TPMI_POLICYTYPE_tab[i].name);
88 return_if_null(str_jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
89
90 return TSS2_RC_SUCCESS;
91 }
92 }
93 return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
94 }
95
96 /** Serialize TPMI_POLICYTYPE to json.
97 *
98 * @param[in] in constant to be serialized.
99 * @param[out] jso pointer to the json object.
100 * @retval TSS2_RC_SUCCESS if the function call was a success.
101 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
102 * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type TPMI_POLICYTYPE.
103 */
104 TSS2_RC
ifapi_json_TPMI_POLICYTYPE_serialize(const TPMI_POLICYTYPE in,json_object ** jso)105 ifapi_json_TPMI_POLICYTYPE_serialize(const TPMI_POLICYTYPE in,
106 json_object **jso)
107 {
108 return ifapi_json_TPMI_POLICYTYPE_serialize_txt(in, jso);
109 }
110
111 /** Serialize value of type TPMS_POLICYSIGNED to json.
112 *
113 * @param[in] in value to be serialized.
114 * @param[out] jso pointer to the json object.
115 * @retval TSS2_RC_SUCCESS if the function call was a success.
116 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
117 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYSIGNED.
118 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
119 */
120 TSS2_RC
ifapi_json_TPMS_POLICYSIGNED_serialize(const TPMS_POLICYSIGNED * in,json_object ** jso)121 ifapi_json_TPMS_POLICYSIGNED_serialize(const TPMS_POLICYSIGNED *in,
122 json_object **jso)
123 {
124 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
125
126 TSS2_RC r;
127 json_object *jso2;
128 size_t cond_cnt = 0; /**< counter for conditional fields */
129
130 if (*jso == NULL)
131 *jso = json_object_new_object();
132
133 if (in->cpHashA.size != 0) {
134 jso2 = NULL;
135 r = ifapi_json_TPM2B_DIGEST_serialize(&in->cpHashA, &jso2);
136 return_if_error(r, "Serialize TPM2B_DIGEST");
137
138 json_object_object_add(*jso, "cpHashA", jso2);
139 }
140 if (in->policyRef.size != 0) {
141 jso2 = NULL;
142 r = ifapi_json_TPM2B_NONCE_serialize(&in->policyRef, &jso2);
143 return_if_error(r, "Serialize TPM2B_NONCE");
144
145 json_object_object_add(*jso, "policyRef", jso2);
146 }
147 if (in->keyPath && strlen(in->keyPath) > 0) {
148 jso2 = NULL;
149 cond_cnt++;
150 r = ifapi_json_char_serialize(in->keyPath, &jso2);
151 return_if_error(r, "Serialize char");
152
153 json_object_object_add(*jso, "keyPath", jso2);
154 }
155 if (in->keyPublic.type != 0) {
156 jso2 = NULL;
157 cond_cnt++;
158 r = ifapi_json_TPMT_PUBLIC_serialize(&in->keyPublic, &jso2);
159 return_if_error(r, "Serialize TPMT_PUBLIC");
160
161 json_object_object_add(*jso, "keyPublic", jso2);
162 }
163 if ((in->keyPEM) && strcmp(in->keyPEM, "") != 0) {
164 jso2 = NULL;
165 cond_cnt++;
166 r = ifapi_json_char_serialize(in->keyPEM, &jso2);
167 return_if_error(r, "Serialize char");
168
169 json_object_object_add(*jso, "keyPEM", jso2);
170 }
171 if ((in->publicKeyHint) && strcmp(in->publicKeyHint, "") != 0) {
172 jso2 = NULL;
173 r = ifapi_json_char_serialize(in->publicKeyHint, &jso2);
174 return_if_error(r, "Serialize char");
175
176 json_object_object_add(*jso, "publicKeyHint", jso2);
177 }
178 if (in->keyPEMhashAlg != 0) {
179 jso2 = NULL;
180 r = ifapi_json_TPMI_ALG_HASH_serialize(in->keyPEMhashAlg, &jso2);
181 return_if_error(r, "Serialize TPMI_ALG_HASH");
182
183 json_object_object_add(*jso, "keyPEMhashAlg", jso2);
184 }
185 /* Check whether only one conditional is used. */
186 if (cond_cnt != 1) {
187 return_error(TSS2_FAPI_RC_BAD_VALUE,
188 "Exactly one conditional is allowed for policy signed.");
189 }
190 return TSS2_RC_SUCCESS;
191 }
192
193 /** Serialize value of type TPMS_POLICYSECRET to json.
194 *
195 * @param[in] in value to be serialized.
196 * @param[out] jso pointer to the json object.
197 * @retval TSS2_RC_SUCCESS if the function call was a success.
198 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
199 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYSECRET.
200 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
201 */
202 TSS2_RC
ifapi_json_TPMS_POLICYSECRET_serialize(const TPMS_POLICYSECRET * in,json_object ** jso)203 ifapi_json_TPMS_POLICYSECRET_serialize(const TPMS_POLICYSECRET *in,
204 json_object **jso)
205 {
206 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
207
208 TSS2_RC r;
209 json_object *jso2;
210 size_t cond_cnt = 0; /**< counter for conditional fields */
211
212 if (*jso == NULL)
213 *jso = json_object_new_object();
214 jso2 = NULL;
215 r = ifapi_json_TPM2B_NONCE_serialize(&in->nonceTPM, &jso2);
216 return_if_error(r, "Serialize TPM2B_NONCE");
217
218 json_object_object_add(*jso, "nonceTPM", jso2);
219 jso2 = NULL;
220 r = ifapi_json_TPM2B_DIGEST_serialize(&in->cpHashA, &jso2);
221 return_if_error(r, "Serialize TPM2B_DIGEST");
222
223 json_object_object_add(*jso, "cpHashA", jso2);
224 if (in->policyRef.size != 0) {
225 jso2 = NULL;
226 r = ifapi_json_TPM2B_NONCE_serialize(&in->policyRef, &jso2);
227 return_if_error(r, "Serialize TPM2B_NONCE");
228
229 json_object_object_add(*jso, "policyRef", jso2);
230 }
231 jso2 = NULL;
232 r = ifapi_json_INT32_serialize(in->expiration, &jso2);
233 return_if_error(r, "Serialize INT32");
234
235 json_object_object_add(*jso, "expiration", jso2);
236 if ((in->objectPath) && strcmp(in->objectPath, "") != 0) {
237 jso2 = NULL;
238 cond_cnt++;
239 r = ifapi_json_char_serialize(in->objectPath, &jso2);
240 return_if_error(r, "Serialize char");
241
242 json_object_object_add(*jso, "objectPath", jso2);
243 }
244 if (in->objectName.size != 0) {
245 jso2 = NULL;
246 cond_cnt++;
247 r = ifapi_json_TPM2B_NAME_serialize(&in->objectName, &jso2);
248 return_if_error(r, "Serialize TPM2B_DIGEST");
249
250 json_object_object_add(*jso, "objectName", jso2);
251 }
252 if (cond_cnt != 1) {
253 return_error(TSS2_FAPI_RC_BAD_VALUE,
254 "Exactly one conditional needed for policy secret .");
255 }
256 return TSS2_RC_SUCCESS;
257 }
258
259 /** Serialize value of type TPMS_POLICYLOCALITY to json.
260 *
261 * @param[in] in value to be serialized.
262 * @param[out] jso pointer to the json object.
263 * @retval TSS2_RC_SUCCESS if the function call was a success.
264 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
265 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYLOCALITY.
266 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
267 */
268 TSS2_RC
ifapi_json_TPMS_POLICYLOCALITY_serialize(const TPMS_POLICYLOCALITY * in,json_object ** jso)269 ifapi_json_TPMS_POLICYLOCALITY_serialize(const TPMS_POLICYLOCALITY *in,
270 json_object **jso)
271 {
272 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
273
274 TSS2_RC r;
275 json_object *jso2;
276
277 if (*jso == NULL)
278 *jso = json_object_new_object();
279 jso2 = NULL;
280 r = ifapi_json_TPMA_LOCALITY_serialize(in->locality, &jso2);
281 return_if_error(r, "Serialize TPMA_LOCALITY");
282
283 json_object_object_add(*jso, "locality", jso2);
284 return TSS2_RC_SUCCESS;
285 }
286
287 /** Serialize value of type TPMS_POLICYNV to json.
288 *
289 * @param[in] in value to be serialized.
290 * @param[out] jso pointer to the json object.
291 * @retval TSS2_RC_SUCCESS if the function call was a success.
292 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
293 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYNV.
294 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
295 */
296 TSS2_RC
ifapi_json_TPMS_POLICYNV_serialize(const TPMS_POLICYNV * in,json_object ** jso)297 ifapi_json_TPMS_POLICYNV_serialize(const TPMS_POLICYNV *in, json_object **jso)
298 {
299 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
300
301 TSS2_RC r;
302 json_object *jso2;
303 size_t cond_cnt = 0; /**< counter for conditional fields */
304
305 if (*jso == NULL)
306 *jso = json_object_new_object();
307 if ((in->nvPath) && strcmp(in->nvPath, "") != 0) {
308 jso2 = NULL;
309 cond_cnt++;
310 r = ifapi_json_char_serialize(in->nvPath, &jso2);
311 return_if_error(r, "Serialize char");
312
313 json_object_object_add(*jso, "nvPath", jso2);
314 }
315 if (in->nvIndex != 0) {
316 jso2 = NULL;
317 cond_cnt++;
318 r = ifapi_json_TPMI_RH_NV_INDEX_serialize(in->nvIndex, &jso2);
319 return_if_error(r, "Serialize TPMI_RH_NV_INDEX");
320
321 json_object_object_add(*jso, "nvIndex", jso2);
322 }
323
324 if (in->nvPublic.nvPublic.nvIndex) {
325 jso2 = NULL;
326 r = ifapi_json_TPM2B_NV_PUBLIC_serialize(&in->nvPublic, &jso2);
327 return_if_error(r, "Serialize TPM2B_NV_PUBLIC");
328
329 json_object_object_add(*jso, "nvPublic", jso2);
330 }
331
332 jso2 = NULL;
333 r = ifapi_json_TPM2B_OPERAND_serialize(&in->operandB, &jso2);
334 return_if_error(r, "Serialize TPM2B_OPERAND");
335
336 json_object_object_add(*jso, "operandB", jso2);
337 if (in->offset != 0) {
338 jso2 = NULL;
339 r = ifapi_json_UINT16_serialize(in->offset, &jso2);
340 return_if_error(r, "Serialize UINT16");
341
342 json_object_object_add(*jso, "offset", jso2);
343 }
344 if (in->operation != 0) {
345 jso2 = NULL;
346 r = ifapi_json_TPM2_EO_serialize(in->operation, &jso2);
347 return_if_error(r, "Serialize TPM2_EO");
348
349 json_object_object_add(*jso, "operation", jso2);
350 }
351 /* Check whether only one conditional is used. */
352 if (cond_cnt != 1) {
353 return_error(TSS2_FAPI_RC_BAD_VALUE,
354 "Exactly one conditional is allowed for policy NV.");
355 }
356
357 return TSS2_RC_SUCCESS;
358 }
359
360 /** Serialize value of type TPMS_POLICYCOUNTERTIMER to json.
361 *
362 * @param[in] in value to be serialized.
363 * @param[out] jso pointer to the json object.
364 * @retval TSS2_RC_SUCCESS if the function call was a success.
365 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
366 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYCOUNTERTIMER.
367 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
368 */
369 TSS2_RC
ifapi_json_TPMS_POLICYCOUNTERTIMER_serialize(const TPMS_POLICYCOUNTERTIMER * in,json_object ** jso)370 ifapi_json_TPMS_POLICYCOUNTERTIMER_serialize(const TPMS_POLICYCOUNTERTIMER *in,
371 json_object **jso)
372 {
373 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
374
375 TSS2_RC r;
376 json_object *jso2;
377
378 if (*jso == NULL)
379 *jso = json_object_new_object();
380 jso2 = NULL;
381 r = ifapi_json_TPM2B_OPERAND_serialize(&in->operandB, &jso2);
382 return_if_error(r, "Serialize TPM2B_OPERAND");
383
384 json_object_object_add(*jso, "operandB", jso2);
385 if (in->offset != 0) {
386 jso2 = NULL;
387 r = ifapi_json_UINT16_serialize(in->offset, &jso2);
388 return_if_error(r, "Serialize UINT16");
389
390 json_object_object_add(*jso, "offset", jso2);
391 }
392 jso2 = NULL;
393 r = ifapi_json_TPM2_EO_serialize(in->operation, &jso2);
394 return_if_error(r, "Serialize TPM2_EO");
395
396 json_object_object_add(*jso, "operation", jso2);
397 return TSS2_RC_SUCCESS;
398 }
399
400 /** Serialize value of type TPMS_POLICYCOMMANDCODE to json.
401 *
402 * @param[in] in value to be serialized.
403 * @param[out] jso pointer to the json object.
404 * @retval TSS2_RC_SUCCESS if the function call was a success.
405 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
406 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYCOMMANDCODE.
407 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
408 */
409 TSS2_RC
ifapi_json_TPMS_POLICYCOMMANDCODE_serialize(const TPMS_POLICYCOMMANDCODE * in,json_object ** jso)410 ifapi_json_TPMS_POLICYCOMMANDCODE_serialize(const TPMS_POLICYCOMMANDCODE *in,
411 json_object **jso)
412 {
413 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
414
415 TSS2_RC r;
416 json_object *jso2;
417
418 if (*jso == NULL)
419 *jso = json_object_new_object();
420 jso2 = NULL;
421 r = ifapi_json_TPM2_CC_serialize(in->code, &jso2);
422 return_if_error(r, "Serialize TPM2_CC");
423
424 json_object_object_add(*jso, "code", jso2);
425 return TSS2_RC_SUCCESS;
426 }
427
428 /** Serialize value of type TPMS_POLICYPHYSICALPRESENCE to json.
429 *
430 * @param[in] in value to be serialized.
431 * @param[out] jso pointer to the json object.
432 * @retval TSS2_RC_SUCCESS if the function call was a success.
433 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
434 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYPHYSICALPRESENCE.
435 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
436 */
437 TSS2_RC
ifapi_json_TPMS_POLICYPHYSICALPRESENCE_serialize(const TPMS_POLICYPHYSICALPRESENCE * in,json_object ** jso)438 ifapi_json_TPMS_POLICYPHYSICALPRESENCE_serialize(const
439 TPMS_POLICYPHYSICALPRESENCE *in, json_object **jso)
440 {
441 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
442
443 if (*jso == NULL)
444 *jso = json_object_new_object();
445 return TSS2_RC_SUCCESS;
446 }
447
448 /** Serialize value of type TPMS_POLICYCPHASH to json.
449 *
450 * @param[in] in value to be serialized.
451 * @param[out] jso pointer to the json object.
452 * @retval TSS2_RC_SUCCESS if the function call was a success.
453 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
454 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYCPHASH.
455 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
456 */
457 TSS2_RC
ifapi_json_TPMS_POLICYCPHASH_serialize(const TPMS_POLICYCPHASH * in,json_object ** jso)458 ifapi_json_TPMS_POLICYCPHASH_serialize(const TPMS_POLICYCPHASH *in,
459 json_object **jso)
460 {
461 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
462
463 TSS2_RC r;
464 json_object *jso2;
465
466 if (*jso == NULL)
467 *jso = json_object_new_object();
468 jso2 = NULL;
469 r = ifapi_json_TPM2B_DIGEST_serialize(&in->cpHash, &jso2);
470 return_if_error(r, "Serialize TPM2B_DIGEST");
471
472 json_object_object_add(*jso, "cpHash", jso2);
473 return TSS2_RC_SUCCESS;
474 }
475
476 /** Serialize value of type TPMS_POLICYNAMEHASH to json.
477 *
478 * @param[in] in value to be serialized.
479 * @param[out] jso pointer to the json object.
480 * @retval TSS2_RC_SUCCESS if the function call was a success.
481 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
482 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYNAMEHASH.
483 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
484 */
485 TSS2_RC
ifapi_json_TPMS_POLICYNAMEHASH_serialize(const TPMS_POLICYNAMEHASH * in,json_object ** jso)486 ifapi_json_TPMS_POLICYNAMEHASH_serialize(const TPMS_POLICYNAMEHASH *in,
487 json_object **jso)
488 {
489 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
490
491 TSS2_RC r;
492 json_object *jso2, *jso_ary;
493 size_t i;
494 size_t cond_cnt = 0; /**< counter for conditional fields */
495
496 if (*jso == NULL) {
497 *jso = json_object_new_object();
498 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
499 }
500
501 if (in->nameHash.size) {
502 jso2 = NULL;
503 cond_cnt++;
504 r = ifapi_json_TPM2B_DIGEST_serialize(&in->nameHash, &jso2);
505 return_if_error(r, "Serialize TPM2B_DIGEST");
506
507 json_object_object_add(*jso, "nameHash", jso2);
508
509 /* No need to serialize namePaths or objectNames from which would be
510 needed to compute nameHash. */
511 return TSS2_RC_SUCCESS;
512 }
513
514 jso_ary = json_object_new_array();
515 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
516
517 if (in->namePaths[0]) {
518 /* Pathnamees for objects are used */
519 cond_cnt++;
520 for (i = 0; i < in->count; i++) {
521 jso2 = NULL;
522 r = ifapi_json_char_serialize(in->namePaths[i], &jso2);
523 return_if_error(r, "Serialize char");
524
525 json_object_array_add(jso_ary, jso2);
526 }
527 json_object_object_add(*jso, "namePaths", jso_ary);
528 } else {
529 /* TPM object names are used */
530 for (i = 0; i < in->count; i++) {
531 jso2 = NULL;
532 r = ifapi_json_TPM2B_NAME_serialize(&in->objectNames[i], &jso2);
533 return_if_error(r, "Serialize TPM2B_NAME");
534 json_object_array_add(jso_ary, jso2);
535 }
536 json_object_object_add(*jso, "objectNames", jso_ary);
537 }
538 /* Check whether only one condition field found in policy. */
539 if (cond_cnt != 1) {
540 return_error(TSS2_FAPI_RC_BAD_VALUE,
541 "Exactly one conditional is allowed for policy name hash.");
542 }
543
544 return TSS2_RC_SUCCESS;
545 }
546
547 /** Serialize value of type TPMS_POLICYDUPLICATIONSELECT to json.
548 *
549 * @param[in] in value to be serialized.
550 * @param[out] jso pointer to the json object.
551 * @retval TSS2_RC_SUCCESS if the function call was a success.
552 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
553 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYDUPLICATIONSELECT.
554 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
555 */
556 TSS2_RC
ifapi_json_TPMS_POLICYDUPLICATIONSELECT_serialize(const TPMS_POLICYDUPLICATIONSELECT * in,json_object ** jso)557 ifapi_json_TPMS_POLICYDUPLICATIONSELECT_serialize(const
558 TPMS_POLICYDUPLICATIONSELECT *in, json_object **jso)
559 {
560 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
561
562 TSS2_RC r;
563 json_object *jso2;
564 size_t cond_cnt = 0; /**< counter for conditional fields */
565
566 if (*jso == NULL)
567 *jso = json_object_new_object();
568 jso2 = NULL;
569 r = ifapi_json_TPM2B_NAME_serialize(&in->objectName, &jso2);
570 return_if_error(r, "Serialize TPM2B_NAME");
571
572 json_object_object_add(*jso, "objectName", jso2);
573
574 if (in->newParentName.size) {
575 cond_cnt++;
576 jso2 = NULL;
577 r = ifapi_json_TPM2B_NAME_serialize(&in->newParentName, &jso2);
578 return_if_error(r, "Serialize TPM2B_NAME");
579
580 json_object_object_add(*jso, "newParentName", jso2);
581 }
582 jso2 = NULL;
583 r = ifapi_json_TPMI_YES_NO_serialize(in->includeObject, &jso2);
584 return_if_error(r, "Serialize TPMI_YES_NO");
585
586 json_object_object_add(*jso, "includeObject", jso2);
587
588 if (in->newParentPath) {
589 jso2 = NULL;
590 cond_cnt++;
591 r = ifapi_json_char_serialize(in->newParentPath, &jso2);
592 return_if_error(r, "Serialize char");
593
594 json_object_object_add(*jso, "newParentPath", jso2);
595 }
596
597 if (in->newParentPublic.publicArea.type) {
598 jso2 = NULL;
599 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->newParentPublic, &jso2);
600 return_if_error(r, "Serialize TPM2B_PUBLIC");
601
602 json_object_object_add(*jso, "newParentPublic", jso2);
603 }
604
605 /* Check whether only one condition field found in policy. */
606 if (cond_cnt != 1) {
607 return_error(TSS2_FAPI_RC_BAD_VALUE,
608 "Exactly one conditional is allowed for policy "
609 "duplication select.");
610 }
611 return TSS2_RC_SUCCESS;
612 }
613
614 /** Serialize value of type TPMS_POLICYAUTHORIZE to json.
615 *
616 * @param[in] in value to be serialized.
617 * @param[out] jso pointer to the json object.
618 * @retval TSS2_RC_SUCCESS if the function call was a success.
619 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
620 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYAUTHORIZE.
621 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
622 */
623 TSS2_RC
ifapi_json_TPMS_POLICYAUTHORIZE_serialize(const TPMS_POLICYAUTHORIZE * in,json_object ** jso)624 ifapi_json_TPMS_POLICYAUTHORIZE_serialize(const TPMS_POLICYAUTHORIZE *in,
625 json_object **jso)
626 {
627 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
628
629 TSS2_RC r;
630 json_object *jso2;
631 size_t cond_cnt = 0; /**< counter for conditional fields */
632
633 if (*jso == NULL)
634 *jso = json_object_new_object();
635 jso2 = NULL;
636 r = ifapi_json_TPM2B_DIGEST_serialize(&in->approvedPolicy, &jso2);
637 return_if_error(r, "Serialize TPM2B_DIGEST");
638
639 json_object_object_add(*jso, "approvedPolicy", jso2);
640 if (in->policyRef.size != 0) {
641 jso2 = NULL;
642 r = ifapi_json_TPM2B_NONCE_serialize(&in->policyRef, &jso2);
643 return_if_error(r, "Serialize TPM2B_NONCE");
644
645 json_object_object_add(*jso, "policyRef", jso2);
646 }
647 jso2 = NULL;
648 r = ifapi_json_TPM2B_NAME_serialize(&in->keyName, &jso2);
649 return_if_error(r, "Serialize TPM2B_NAME");
650
651 json_object_object_add(*jso, "keyName", jso2);
652 jso2 = NULL;
653 /*
654 r = ifapi_json_TPMT_TK_VERIFIED_serialize(&in->checkTicket, &jso2);
655 return_if_error(r, "Serialize TPMT_TK_VERIFIED");
656
657 json_object_object_add(*jso, "checkTicket", jso2);
658 jso2 = NULL;
659 */
660 if (in->keyPath) {
661 cond_cnt++;
662 r = ifapi_json_char_serialize(in->keyPath, &jso2);
663 return_if_error(r, "Serialize char");
664
665 json_object_object_add(*jso, "keyPath", jso2);
666 }
667 if (in->keyPublic.type != 0) {
668 jso2 = NULL;
669 cond_cnt++;
670 r = ifapi_json_TPMT_PUBLIC_serialize(&in->keyPublic, &jso2);
671 return_if_error(r, "Serialize TPMT_PUBLIC");
672
673 json_object_object_add(*jso, "keyPublic", jso2);
674 }
675 if ((in->keyPEM) && strcmp(in->keyPEM, "") != 0) {
676 jso2 = NULL;
677 r = ifapi_json_char_serialize(in->keyPEM, &jso2);
678 return_if_error(r, "Serialize char");
679
680 json_object_object_add(*jso, "keyPEM", jso2);
681 }
682 if (in->keyPEMhashAlg != 0) {
683 jso2 = NULL;
684 r = ifapi_json_TPMI_ALG_HASH_serialize(in->keyPEMhashAlg, &jso2);
685 return_if_error(r, "Serialize TPMI_ALG_HASH");
686
687 json_object_object_add(*jso, "keyPEMhashAlg", jso2);
688 }
689
690 return TSS2_RC_SUCCESS;
691 }
692
693 /** Serialize value of type TPMS_POLICYAUTHVALUE to json.
694 *
695 * @param[in] in value to be serialized.
696 * @param[out] jso pointer to the json object.
697 * @retval TSS2_RC_SUCCESS if the function call was a success.
698 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
699 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYAUTHVALUE.
700 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
701 */
702 TSS2_RC
ifapi_json_TPMS_POLICYAUTHVALUE_serialize(const TPMS_POLICYAUTHVALUE * in,json_object ** jso)703 ifapi_json_TPMS_POLICYAUTHVALUE_serialize(const TPMS_POLICYAUTHVALUE *in,
704 json_object **jso)
705 {
706 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
707
708 if (*jso == NULL)
709 *jso = json_object_new_object();
710 return TSS2_RC_SUCCESS;
711 }
712
713 /** Serialize value of type TPMS_POLICYPASSWORD to json.
714 *
715 * @param[in] in value to be serialized.
716 * @param[out] jso pointer to the json object.
717 * @retval TSS2_RC_SUCCESS if the function call was a success.
718 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
719 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYPASSWORD.
720 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
721 */
722 TSS2_RC
ifapi_json_TPMS_POLICYPASSWORD_serialize(const TPMS_POLICYPASSWORD * in,json_object ** jso)723 ifapi_json_TPMS_POLICYPASSWORD_serialize(const TPMS_POLICYPASSWORD *in,
724 json_object **jso)
725 {
726 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
727
728 if (*jso == NULL)
729 *jso = json_object_new_object();
730 return TSS2_RC_SUCCESS;
731 }
732
733 /** Serialize value of type TPMS_POLICYNVWRITTEN to json.
734 *
735 * @param[in] in value to be serialized.
736 * @param[out] jso pointer to the json object.
737 * @retval TSS2_RC_SUCCESS if the function call was a success.
738 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
739 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYNVWRITTEN.
740 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
741 */
742 TSS2_RC
ifapi_json_TPMS_POLICYNVWRITTEN_serialize(const TPMS_POLICYNVWRITTEN * in,json_object ** jso)743 ifapi_json_TPMS_POLICYNVWRITTEN_serialize(const TPMS_POLICYNVWRITTEN *in,
744 json_object **jso)
745 {
746 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
747
748 TSS2_RC r;
749 json_object *jso2;
750
751 if (*jso == NULL)
752 *jso = json_object_new_object();
753 jso2 = NULL;
754 r = ifapi_json_TPMI_YES_NO_serialize(in->writtenSet, &jso2);
755 return_if_error(r, "Serialize TPMI_YES_NO");
756
757 json_object_object_add(*jso, "writtenSet", jso2);
758 return TSS2_RC_SUCCESS;
759 }
760
761 /** Serialize value of type TPMS_POLICYTEMPLATE 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 TPMS_POLICYTEMPLATE.
768 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
769 */
770 TSS2_RC
ifapi_json_TPMS_POLICYTEMPLATE_serialize(const TPMS_POLICYTEMPLATE * in,json_object ** jso)771 ifapi_json_TPMS_POLICYTEMPLATE_serialize(const TPMS_POLICYTEMPLATE *in,
772 json_object **jso)
773 {
774 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
775
776 TSS2_RC r;
777 json_object *jso2;
778 size_t cond_cnt = 0; /**< counter for conditional fields */
779
780 if (*jso == NULL)
781 *jso = json_object_new_object();
782 if (in->templateHash.size != 0) {
783 jso2 = NULL;
784 cond_cnt++;
785 r = ifapi_json_TPM2B_DIGEST_serialize(&in->templateHash, &jso2);
786 return_if_error(r, "Serialize TPM2B_DIGEST");
787
788 json_object_object_add(*jso, "templateHash", jso2);
789 }
790 if (in->templatePublic.size != 0) {
791 jso2 = NULL;
792 cond_cnt++;
793 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->templatePublic, &jso2);
794 return_if_error(r, "Serialize TPM2B_PUBLIC");
795
796 json_object_object_add(*jso, "templatePublic", jso2);
797 }
798 if (in->templateName) {
799 jso2 = NULL;
800 cond_cnt++;
801 r = ifapi_json_char_serialize(in->templateName, &jso2);
802 return_if_error(r, "Serialize char");
803
804 json_object_object_add(*jso, "templateName", jso2);
805 }
806
807 /* Check whether only one condition field found in policy. */
808 if (cond_cnt != 1) {
809 return_error(TSS2_FAPI_RC_BAD_VALUE,
810 "Exactly one conditional is allowed for policy template.");
811 }
812 return TSS2_RC_SUCCESS;
813 }
814
815 /** Serialize value of type TPMS_POLICYAUTHORIZENV to json.
816 *
817 * @param[in] in value to be serialized.
818 * @param[out] jso pointer to the json object.
819 * @retval TSS2_RC_SUCCESS if the function call was a success.
820 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
821 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYAUTHORIZENV.
822 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
823 */
824 TSS2_RC
ifapi_json_TPMS_POLICYAUTHORIZENV_serialize(const TPMS_POLICYAUTHORIZENV * in,json_object ** jso)825 ifapi_json_TPMS_POLICYAUTHORIZENV_serialize(const TPMS_POLICYAUTHORIZENV *in,
826 json_object **jso)
827 {
828 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
829
830 TSS2_RC r;
831 json_object *jso2;
832 size_t cond_cnt = 0; /**< counter for conditional fields */
833
834 if (*jso == NULL)
835 *jso = json_object_new_object();
836 jso2 = NULL;
837 if (in->nvPath) {
838 cond_cnt++;
839 r = ifapi_json_char_serialize(in->nvPath, &jso2);
840 return_if_error(r, "Serialize char");
841
842 json_object_object_add(*jso, "nvPath", jso2);
843 }
844 jso2 = NULL;
845 if (in->nvPublic.nvPublic.nvIndex > 0) {
846 cond_cnt++;
847 /* Template already instantiated */
848 r = ifapi_json_TPM2B_NV_PUBLIC_serialize(&in->nvPublic, &jso2);
849 return_if_error(r, "Serialize TPM2B_NV_PUBLIC");
850
851 json_object_object_add(*jso, "nvPublic", jso2);
852 }
853 if (cond_cnt != 1) {
854 return_error(TSS2_FAPI_RC_BAD_VALUE,
855 "Exactly one conditional needed for policy authorize nv .");
856 }
857 return TSS2_RC_SUCCESS;
858 }
859
860 /** Serialize value of type TPMS_POLICYACTION to json.
861 *
862 * @param[in] in value to be serialized.
863 * @param[out] jso pointer to the json object.
864 * @retval TSS2_RC_SUCCESS if the function call was a success.
865 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
866 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYACTION.
867 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
868 */
869 TSS2_RC
ifapi_json_TPMS_POLICYACTION_serialize(const TPMS_POLICYACTION * in,json_object ** jso)870 ifapi_json_TPMS_POLICYACTION_serialize(const TPMS_POLICYACTION *in,
871 json_object **jso)
872 {
873 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
874
875 TSS2_RC r;
876 json_object *jso2;
877
878 if (*jso == NULL)
879 *jso = json_object_new_object();
880 jso2 = NULL;
881 r = ifapi_json_char_serialize(in->action, &jso2);
882 return_if_error(r, "Serialize char");
883
884 json_object_object_add(*jso, "action", jso2);
885 return TSS2_RC_SUCCESS;
886 }
887
888 /** Serialize value of type TPMS_PCRVALUE to json.
889 *
890 * @param[in] in value to be serialized.
891 * @param[out] jso pointer to the json object.
892 * @retval TSS2_RC_SUCCESS if the function call was a success.
893 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
894 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_PCRVALUE.
895 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
896 */
897 TSS2_RC
ifapi_json_TPMS_PCRVALUE_serialize(const TPMS_PCRVALUE * in,json_object ** jso)898 ifapi_json_TPMS_PCRVALUE_serialize(const TPMS_PCRVALUE *in, json_object **jso)
899 {
900 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
901
902 TSS2_RC r;
903 json_object *jso2;
904
905 if (*jso == NULL)
906 *jso = json_object_new_object();
907 jso2 = NULL;
908 r = ifapi_json_UINT32_serialize(in->pcr, &jso2);
909 return_if_error(r, "Serialize UINT32");
910
911 json_object_object_add(*jso, "pcr", jso2);
912 jso2 = NULL;
913 r = ifapi_json_TPM2_ALG_ID_serialize(in->hashAlg, &jso2);
914 return_if_error(r, "Serialize TPM2_ALG_ID");
915
916 json_object_object_add(*jso, "hashAlg", jso2);
917 jso2 = NULL;
918 r = ifapi_json_TPMU_HA_serialize(&in->digest, in->hashAlg, &jso2);
919 return_if_error(r, "Serialize TPMU_HA");
920
921 json_object_object_add(*jso, "digest", jso2);
922 return TSS2_RC_SUCCESS;
923 }
924
925 /** Serialize value of type TPML_PCRVALUES to json.
926 *
927 * @param[in] in value to be serialized.
928 * @param[out] jso pointer to the json object.
929 * @retval TSS2_RC_SUCCESS if the function call was a success.
930 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
931 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_PCRVALUES.
932 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
933 */
934 TSS2_RC
ifapi_json_TPML_PCRVALUES_serialize(const TPML_PCRVALUES * in,json_object ** jso)935 ifapi_json_TPML_PCRVALUES_serialize(const TPML_PCRVALUES *in, json_object **jso)
936 {
937 TSS2_RC r;
938 json_object *jso2;
939
940 if (*jso == NULL)
941 *jso = json_object_new_array();
942 jso2 = NULL;
943 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
944
945 for (size_t i = 0; i < in->count; i++) {
946 jso2 = NULL;
947 r = ifapi_json_TPMS_PCRVALUE_serialize(&in->pcrs[i], &jso2);
948 return_if_error(r, "Serialize TPMS_PCRVALUE");
949
950 json_object_array_add(*jso, jso2);
951 }
952 return TSS2_RC_SUCCESS;
953 }
954
955 /** Serialize value of type TPMS_POLICYPCR to json.
956 *
957 * @param[in] in value to be serialized.
958 * @param[out] jso pointer to the json object.
959 * @retval TSS2_RC_SUCCESS if the function call was a success.
960 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
961 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYPCR.
962 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
963 */
964 TSS2_RC
ifapi_json_TPMS_POLICYPCR_serialize(const TPMS_POLICYPCR * in,json_object ** jso)965 ifapi_json_TPMS_POLICYPCR_serialize(const TPMS_POLICYPCR *in, json_object **jso)
966 {
967 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
968
969 TSS2_RC r;
970 json_object *jso2;
971 size_t cond_cnt = 0; /**< counter for conditional fields */
972
973 if (in->pcrs) {
974 if (*jso == NULL)
975 *jso = json_object_new_object();
976 jso2 = NULL;
977 cond_cnt++;
978 r = ifapi_json_TPML_PCRVALUES_serialize(in->pcrs, &jso2);
979 return_if_error(r, "Serialize TPML_PCRVALUES");
980
981 json_object_object_add(*jso, "pcrs", jso2);
982 }
983
984 if (in->currentPCRandBanks.count) {
985 jso2 = NULL;
986 cond_cnt++;
987 r = ifapi_json_TPML_PCR_SELECTION_serialize(&in->currentPCRandBanks, &jso2);
988 return_if_error(r, "Serialize TPML_PCR_SELECTION");
989
990 json_object_object_add(*jso, "currentPCRandBanks", jso2);
991 }
992
993 if (in->currentPCRs.sizeofSelect) {
994 jso2 = NULL;
995 cond_cnt++;
996 r = ifapi_json_TPMS_PCR_SELECT_serialize(&in->currentPCRs, &jso2);
997 return_if_error(r, "Serialize TPMS_PCR_SELECT");
998
999 json_object_object_add(*jso, "currentPCRs", jso2);
1000 }
1001 /* Check whether only one conditional is used. */
1002 if (cond_cnt != 1) {
1003 return_error(TSS2_FAPI_RC_BAD_VALUE,
1004 "Exactly one conditional is allowed for policy pcr.");
1005 }
1006 return TSS2_RC_SUCCESS;
1007 }
1008
1009 /** Serialize value of type TPMS_POLICYAUTHORIZATION to json.
1010 *
1011 * @param[in] in value to be serialized.
1012 * @param[out] jso pointer to the json object.
1013 * @retval TSS2_RC_SUCCESS if the function call was a success.
1014 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1015 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYAUTHORIZATION.
1016 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1017 */
1018 TSS2_RC
ifapi_json_TPMS_POLICYAUTHORIZATION_serialize(const TPMS_POLICYAUTHORIZATION * in,json_object ** jso)1019 ifapi_json_TPMS_POLICYAUTHORIZATION_serialize(
1020 const TPMS_POLICYAUTHORIZATION *in,
1021 json_object **jso)
1022 {
1023 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1024
1025 TSS2_RC r;
1026 json_object *jso2;
1027
1028 if (*jso == NULL)
1029 *jso = json_object_new_object();
1030 jso2 = NULL;
1031 r = ifapi_json_char_serialize(in->type, &jso2);
1032 return_if_error(r, "Serialize char");
1033
1034 json_object_object_add(*jso, "type", jso2);
1035 jso2 = NULL;
1036 r = ifapi_json_TPMT_PUBLIC_serialize(&in->key, &jso2);
1037 return_if_error(r, "Serialize TPMT_PUBLIC");
1038
1039 json_object_object_add(*jso, "key", jso2);
1040 jso2 = NULL;
1041 r = ifapi_json_TPM2B_NONCE_serialize(&in->policyRef, &jso2);
1042 return_if_error(r, "Serialize TPM2B_NONCE");
1043
1044 json_object_object_add(*jso, "policyRef", jso2);
1045 jso2 = NULL;
1046 r = ifapi_json_TPMT_SIGNATURE_serialize(&in->signature, &jso2);
1047 return_if_error(r, "Serialize TPMT_SIGNATURE");
1048
1049 json_object_object_add(*jso, "signature", jso2);
1050 return TSS2_RC_SUCCESS;
1051 }
1052
1053 /** Serialize value of type TPML_POLICYAUTHORIZATIONS to json.
1054 *
1055 * @param[in] in value to be serialized.
1056 * @param[out] jso pointer to the json object.
1057 * @retval TSS2_RC_SUCCESS if the function call was a success.
1058 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1059 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_POLICYAUTHORIZATIONS.
1060 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1061 */
1062 TSS2_RC
ifapi_json_TPML_POLICYAUTHORIZATIONS_serialize(const TPML_POLICYAUTHORIZATIONS * in,json_object ** jso)1063 ifapi_json_TPML_POLICYAUTHORIZATIONS_serialize(const TPML_POLICYAUTHORIZATIONS
1064 *in, json_object **jso)
1065 {
1066 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1067
1068 TSS2_RC r;
1069 json_object *jso2;
1070
1071 if (*jso == NULL)
1072 *jso = json_object_new_array();
1073 jso2 = NULL;
1074 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1075 jso2 = NULL;
1076 for (size_t i = 0; i < in->count; i++) {
1077 jso2 = NULL;
1078 r = ifapi_json_TPMS_POLICYAUTHORIZATION_serialize(&in->authorizations[i],
1079 &jso2);
1080 return_if_error(r, "Serialize TPMS_POLICYAUTHORIZATION");
1081
1082 json_object_array_add(*jso, jso2);
1083 }
1084 return TSS2_RC_SUCCESS;
1085 }
1086
1087 /** Serialize value of type TPMS_POLICYBRANCH to json.
1088 *
1089 * @param[in] in value to be serialized.
1090 * @param[out] jso pointer to the json object.
1091 * @retval TSS2_RC_SUCCESS if the function call was a success.
1092 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1093 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYBRANCH.
1094 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1095 */
1096 TSS2_RC
ifapi_json_TPMS_POLICYBRANCH_serialize(const TPMS_POLICYBRANCH * in,json_object ** jso)1097 ifapi_json_TPMS_POLICYBRANCH_serialize(const TPMS_POLICYBRANCH *in,
1098 json_object **jso)
1099 {
1100 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1101
1102 TSS2_RC r;
1103 json_object *jso2;
1104
1105 if (*jso == NULL)
1106 *jso = json_object_new_object();
1107 jso2 = NULL;
1108 r = ifapi_json_char_serialize(in->name, &jso2);
1109 return_if_error(r, "Serialize char");
1110
1111 json_object_object_add(*jso, "name", jso2);
1112 jso2 = NULL;
1113 r = ifapi_json_char_serialize(in->description, &jso2);
1114 return_if_error(r, "Serialize char");
1115
1116 json_object_object_add(*jso, "description", jso2);
1117 jso2 = NULL;
1118 r = ifapi_json_TPML_POLICYELEMENTS_serialize(in->policy, &jso2);
1119 return_if_error(r, "Serialize TPML_POLICYELEMENTS");
1120
1121 json_object_object_add(*jso, "policy", jso2);
1122 if (in->policyDigests.count > 0) {
1123 jso2 = NULL;
1124 r = ifapi_json_TPML_DIGEST_VALUES_serialize(&in->policyDigests, &jso2);
1125 return_if_error(r, "Serialize TPML_DIGEST_VALUES");
1126
1127 json_object_object_add(*jso, "policyDigests", jso2);
1128 }
1129 return TSS2_RC_SUCCESS;
1130 }
1131
1132 /** Serialize value of type TPML_POLICYBRANCHES to json.
1133 *
1134 * @param[in] in value to be serialized.
1135 * @param[out] jso pointer to the json object.
1136 * @retval TSS2_RC_SUCCESS if the function call was a success.
1137 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1138 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_POLICYBRANCHES.
1139 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1140 */
1141 TSS2_RC
ifapi_json_TPML_POLICYBRANCHES_serialize(const TPML_POLICYBRANCHES * in,json_object ** jso)1142 ifapi_json_TPML_POLICYBRANCHES_serialize(const TPML_POLICYBRANCHES *in,
1143 json_object **jso)
1144 {
1145 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1146
1147 TSS2_RC r;
1148 json_object *jso2;
1149
1150 if (*jso == NULL)
1151 *jso = json_object_new_array();
1152 jso2 = NULL;
1153 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1154 jso2 = NULL;
1155 for (size_t i = 0; i < in->count; i++) {
1156 jso2 = NULL;
1157 r = ifapi_json_TPMS_POLICYBRANCH_serialize(&in->authorizations[i], &jso2);
1158 return_if_error(r, "Serialize TPMS_POLICYBRANCH");
1159
1160 json_object_array_add(*jso, jso2);
1161 }
1162 return TSS2_RC_SUCCESS;
1163 }
1164
1165 /** Serialize value of type TPMS_POLICYOR to json.
1166 *
1167 * @param[in] in value to be serialized.
1168 * @param[out] jso pointer to the json object.
1169 * @retval TSS2_RC_SUCCESS if the function call was a success.
1170 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1171 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICYOR.
1172 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1173 */
1174 TSS2_RC
ifapi_json_TPMS_POLICYOR_serialize(const TPMS_POLICYOR * in,json_object ** jso)1175 ifapi_json_TPMS_POLICYOR_serialize(const TPMS_POLICYOR *in, json_object **jso)
1176 {
1177 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1178
1179 TSS2_RC r;
1180 json_object *jso2;
1181
1182 if (*jso == NULL)
1183 *jso = json_object_new_object();
1184 jso2 = NULL;
1185 r = ifapi_json_TPML_POLICYBRANCHES_serialize(in->branches, &jso2);
1186 return_if_error(r, "Serialize TPML_POLICYBRANCHES");
1187
1188 json_object_object_add(*jso, "branches", jso2);
1189 return TSS2_RC_SUCCESS;
1190 }
1191
1192 /** Serialize a TPMU_POLICYELEMENT to json.
1193 *
1194 * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
1195 * @param[in] in the value to be serialized.
1196 * @param[in] selector the type of the policy element.
1197 * @param[out] jso pointer to the json object.
1198 * @retval TSS2_RC_SUCCESS if the function call was a success.
1199 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1200 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMU_POLICYELEMENT.
1201 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1202 */
1203 TSS2_RC
ifapi_json_TPMU_POLICYELEMENT_serialize(const TPMU_POLICYELEMENT * in,UINT32 selector,json_object ** jso)1204 ifapi_json_TPMU_POLICYELEMENT_serialize(const TPMU_POLICYELEMENT *in,
1205 UINT32 selector, json_object **jso)
1206 {
1207 if (*jso == NULL)
1208 *jso = json_object_new_object();
1209 return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1210
1211 switch (selector) {
1212 case POLICYOR:
1213 return ifapi_json_TPMS_POLICYOR_serialize(&in->PolicyOr, jso);
1214 case POLICYSIGNED:
1215 return ifapi_json_TPMS_POLICYSIGNED_serialize(&in->PolicySigned, jso);
1216 case POLICYSECRET:
1217 return ifapi_json_TPMS_POLICYSECRET_serialize(&in->PolicySecret, jso);
1218 case POLICYPCR:
1219 return ifapi_json_TPMS_POLICYPCR_serialize(&in->PolicyPCR, jso);
1220 case POLICYLOCALITY:
1221 return ifapi_json_TPMS_POLICYLOCALITY_serialize(&in->PolicyLocality, jso);
1222 case POLICYNV:
1223 return ifapi_json_TPMS_POLICYNV_serialize(&in->PolicyNV, jso);
1224 case POLICYCOUNTERTIMER:
1225 return ifapi_json_TPMS_POLICYCOUNTERTIMER_serialize(&in->PolicyCounterTimer,
1226 jso);
1227 case POLICYCOMMANDCODE:
1228 return ifapi_json_TPMS_POLICYCOMMANDCODE_serialize(&in->PolicyCommandCode, jso);
1229 case POLICYPHYSICALPRESENCE:
1230 return ifapi_json_TPMS_POLICYPHYSICALPRESENCE_serialize(
1231 &in->PolicyPhysicalPresence, jso);
1232 case POLICYCPHASH:
1233 return ifapi_json_TPMS_POLICYCPHASH_serialize(&in->PolicyCpHash, jso);
1234 case POLICYNAMEHASH:
1235 return ifapi_json_TPMS_POLICYNAMEHASH_serialize(&in->PolicyNameHash, jso);
1236 case POLICYDUPLICATIONSELECT:
1237 return ifapi_json_TPMS_POLICYDUPLICATIONSELECT_serialize(
1238 &in->PolicyDuplicationSelect, jso);
1239 case POLICYAUTHORIZE:
1240 return ifapi_json_TPMS_POLICYAUTHORIZE_serialize(&in->PolicyAuthorize, jso);
1241 case POLICYAUTHVALUE:
1242 return ifapi_json_TPMS_POLICYAUTHVALUE_serialize(&in->PolicyAuthValue, jso);
1243 case POLICYPASSWORD:
1244 return ifapi_json_TPMS_POLICYPASSWORD_serialize(&in->PolicyPassword, jso);
1245 case POLICYNVWRITTEN:
1246 return ifapi_json_TPMS_POLICYNVWRITTEN_serialize(&in->PolicyNvWritten, jso);
1247 case POLICYTEMPLATE:
1248 return ifapi_json_TPMS_POLICYTEMPLATE_serialize(&in->PolicyTemplate, jso);
1249 case POLICYAUTHORIZENV:
1250 return ifapi_json_TPMS_POLICYAUTHORIZENV_serialize(&in->PolicyAuthorizeNv, jso);
1251 case POLICYACTION:
1252 return ifapi_json_TPMS_POLICYACTION_serialize(&in->PolicyAction, jso);
1253 default:
1254 LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
1255 return TSS2_SYS_RC_BAD_VALUE;
1256 };
1257 return TSS2_RC_SUCCESS;
1258 }
1259
1260 /** Serialize value of type TPMT_POLICYELEMENT to json.
1261 *
1262 * @param[in] in value to be serialized.
1263 * @param[out] jso pointer to the json object.
1264 * @retval TSS2_RC_SUCCESS if the function call was a success.
1265 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1266 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMT_POLICYELEMENT.
1267 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1268 */
1269 TSS2_RC
ifapi_json_TPMT_POLICYELEMENT_serialize(const TPMT_POLICYELEMENT * in,json_object ** jso)1270 ifapi_json_TPMT_POLICYELEMENT_serialize(const TPMT_POLICYELEMENT *in,
1271 json_object **jso)
1272 {
1273 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1274
1275 TSS2_RC r;
1276 json_object *jso2;
1277
1278 if (*jso == NULL)
1279 *jso = json_object_new_object();
1280 jso2 = NULL;
1281 r = ifapi_json_TPMI_POLICYTYPE_serialize(in->type, &jso2);
1282 return_if_error(r, "Serialize TPMI_POLICYTYPE");
1283
1284 json_object_object_add(*jso, "type", jso2);
1285
1286 if (in->policyDigests.count > 0) {
1287 jso2 = NULL;
1288 r = ifapi_json_TPML_DIGEST_VALUES_serialize(&in->policyDigests, &jso2);
1289 return_if_error(r, "Serialize TPML_DIGEST_VALUES");
1290
1291 json_object_object_add(*jso, "policyDigests", jso2);
1292 }
1293 r = ifapi_json_TPMU_POLICYELEMENT_serialize(&in->element, in->type, jso);
1294 return_if_error(r, "Serialize TPMU_POLICYELEMENT");
1295
1296 return TSS2_RC_SUCCESS;
1297 }
1298
1299 /** Serialize value of type TPML_POLICYELEMENTS to json.
1300 *
1301 * @param[in] in value to be serialized.
1302 * @param[out] jso pointer to the json object.
1303 * @retval TSS2_RC_SUCCESS if the function call was a success.
1304 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1305 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPML_POLICYELEMENTS.
1306 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1307 */
1308 TSS2_RC
ifapi_json_TPML_POLICYELEMENTS_serialize(const TPML_POLICYELEMENTS * in,json_object ** jso)1309 ifapi_json_TPML_POLICYELEMENTS_serialize(const TPML_POLICYELEMENTS *in,
1310 json_object **jso)
1311 {
1312 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1313
1314 TSS2_RC r;
1315 json_object *jso2;
1316
1317 if (*jso == NULL)
1318 *jso = json_object_new_array();
1319 jso2 = NULL;
1320 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
1321
1322 for (size_t i = 0; i < in->count; i++) {
1323 jso2 = NULL;
1324 r = ifapi_json_TPMT_POLICYELEMENT_serialize(&in->elements[i], &jso2);
1325 return_if_error(r, "Serialize TPMT_POLICYELEMENT");
1326
1327 json_object_array_add(*jso, jso2);
1328 }
1329 return TSS2_RC_SUCCESS;
1330 }
1331
1332 /** Serialize value of type TPMS_POLICY to json.
1333 *
1334 * @param[in] in value to be serialized.
1335 * @param[out] jso pointer to the json object.
1336 * @retval TSS2_RC_SUCCESS if the function call was a success.
1337 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
1338 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPMS_POLICY.
1339 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
1340 */
1341 TSS2_RC
ifapi_json_TPMS_POLICY_serialize(const TPMS_POLICY * in,json_object ** jso)1342 ifapi_json_TPMS_POLICY_serialize(const TPMS_POLICY *in,
1343 json_object **jso)
1344 {
1345 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
1346
1347 TSS2_RC r;
1348 json_object *jso2;
1349
1350 if (*jso == NULL)
1351 *jso = json_object_new_object();
1352 jso2 = NULL;
1353 r = ifapi_json_char_serialize(in->description, &jso2);
1354 return_if_error(r, "Serialize char");
1355
1356 json_object_object_add(*jso, "description", jso2);
1357 jso2 = NULL;
1358 r = ifapi_json_TPML_DIGEST_VALUES_serialize(&in->policyDigests, &jso2);
1359 return_if_error(r, "Serialize TPML_DIGEST_VALUES");
1360
1361 json_object_object_add(*jso, "policyDigests", jso2);
1362 if (in->policyAuthorizations) {
1363 jso2 = NULL;
1364 r = ifapi_json_TPML_POLICYAUTHORIZATIONS_serialize(in->policyAuthorizations,
1365 &jso2);
1366 return_if_error(r, "Serialize TPML_POLICYAUTHORIZATIONS");
1367
1368 json_object_object_add(*jso, "policyAuthorizations", jso2);
1369 }
1370 jso2 = NULL;
1371 r = ifapi_json_TPML_POLICYELEMENTS_serialize(in->policy, &jso2);
1372 return_if_error(r, "Serialize TPML_POLICYELEMENTS");
1373
1374 json_object_object_add(*jso, "policy", jso2);
1375 return TSS2_RC_SUCCESS;
1376 }
1377