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 #include <string.h>
13
14 #include "ifapi_json_serialize.h"
15 #include "tpm_json_serialize.h"
16 #include "fapi_policy.h"
17 #include "ifapi_policy_json_serialize.h"
18
19 #define LOGMODULE fapijson
20 #include "util/log.h"
21 #include "util/aux_util.h"
22
23
24 /** Serialize a character string to json.
25 *
26 * @param[in] in value to be serialized.
27 * @param[out] jso pointer to the json object.
28 * @retval TSS2_RC_SUCCESS if the function call was a success.
29 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
30 */
31 TSS2_RC
ifapi_json_char_serialize(const char * in,json_object ** jso)32 ifapi_json_char_serialize(
33 const char *in,
34 json_object **jso)
35 {
36 if (in == NULL) {
37 *jso = json_object_new_string("");
38 } else {
39 *jso = json_object_new_string(in);
40 }
41 return_if_null(jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
42 return TSS2_RC_SUCCESS;
43 }
44
45 /** Serialize value of type UINT8_ARY to json.
46 *
47 * @param[in] in value to be serialized.
48 * @param[out] jso pointer to the json object.
49 * @retval TSS2_RC_SUCCESS if the function call was a success.
50 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
51 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2B_DIGEST.
52 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
53 */
54 TSS2_RC
ifapi_json_UINT8_ARY_serialize(const UINT8_ARY * in,json_object ** jso)55 ifapi_json_UINT8_ARY_serialize(const UINT8_ARY *in, json_object **jso)
56 {
57 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
58
59 char hex_string[(in->size) * 2 + 1];
60
61 if (in->size > 0) {
62 uint8_t *buffer = in->buffer;
63
64 for (size_t i = 0, off = 0; i < in->size; i++, off += 2)
65 sprintf(&hex_string[off], "%02x", buffer[i]);
66 }
67 hex_string[(in->size) * 2] = '\0';
68 *jso = json_object_new_string(hex_string);
69 return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
70
71 return TSS2_RC_SUCCESS;
72 }
73
74 /** Serialize value of type IFAPI_KEY to json.
75 *
76 * @param[in] in value to be serialized.
77 * @param[out] jso pointer to the json object.
78 * @retval TSS2_RC_SUCCESS if the function call was a success.
79 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
80 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_KEY.
81 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
82 */
83 TSS2_RC
ifapi_json_IFAPI_KEY_serialize(const IFAPI_KEY * in,json_object ** jso)84 ifapi_json_IFAPI_KEY_serialize(const IFAPI_KEY *in, json_object **jso)
85 {
86 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
87
88 TSS2_RC r;
89 json_object *jso2;
90
91 if (*jso == NULL)
92 *jso = json_object_new_object();
93 jso2 = NULL;
94 r = ifapi_json_TPMI_YES_NO_serialize(in->with_auth, &jso2);
95 return_if_error(r, "Serialize TPMI_YES_NO");
96
97 json_object_object_add(*jso, "with_auth", jso2);
98 jso2 = NULL;
99 r = ifapi_json_UINT32_serialize(in->persistent_handle, &jso2);
100 return_if_error(r, "Serialize UINT32");
101
102 json_object_object_add(*jso, "persistent_handle", jso2);
103 jso2 = NULL;
104 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->public, &jso2);
105 return_if_error(r, "Serialize TPM2B_PUBLIC");
106
107 json_object_object_add(*jso, "public", jso2);
108 jso2 = NULL;
109 r = ifapi_json_UINT8_ARY_serialize(&in->serialization, &jso2);
110 return_if_error(r, "Serialize UINT8_ARY");
111
112 json_object_object_add(*jso, "serialization", jso2);
113 if (in->private.buffer != NULL) {
114 jso2 = NULL;
115 r = ifapi_json_UINT8_ARY_serialize(&in->private, &jso2);
116 return_if_error(r, "Serialize UINT8_ARY");
117
118 json_object_object_add(*jso, "private", jso2);
119 }
120 if (in->appData.buffer != NULL) {
121 jso2 = NULL;
122 r = ifapi_json_UINT8_ARY_serialize(&in->appData, &jso2);
123 return_if_error(r, "Serialize UINT8_ARY");
124
125 json_object_object_add(*jso, "appData", jso2);
126 }
127 jso2 = NULL;
128 r = ifapi_json_char_serialize(in->policyInstance, &jso2);
129 return_if_error(r, "Serialize char");
130
131 json_object_object_add(*jso, "policyInstance", jso2);
132
133 /* Creation Data is not available for imported keys */
134 if (in->creationData.size) {
135 jso2 = NULL;
136 r = ifapi_json_TPM2B_CREATION_DATA_serialize(&in->creationData, &jso2);
137 return_if_error(r, "Serialize TPM2B_CREATION_DATA");
138
139 json_object_object_add(*jso, "creationData", jso2);
140 }
141 /* Creation Ticket is not available for imported keys */
142 if (in->creationTicket.tag) {
143 jso2 = NULL;
144 r = ifapi_json_TPMT_TK_CREATION_serialize(&in->creationTicket, &jso2);
145 return_if_error(r, "Serialize TPMT_TK_CREATION");
146
147 json_object_object_add(*jso, "creationTicket", jso2);
148 }
149 jso2 = NULL;
150 r = ifapi_json_char_serialize(in->description, &jso2);
151 return_if_error(r, "Serialize char");
152
153 json_object_object_add(*jso, "description", jso2);
154 jso2 = NULL;
155 r = ifapi_json_char_serialize(in->certificate, &jso2);
156 return_if_error(r, "Serialize char");
157
158 json_object_object_add(*jso, "certificate", jso2);
159
160 if (in->public.publicArea.type != TPM2_ALG_KEYEDHASH) {
161 /* Keyed hash objects to not need a signing scheme. */
162 jso2 = NULL;
163 r = ifapi_json_TPMT_SIG_SCHEME_serialize(&in->signing_scheme, &jso2);
164 return_if_error(r, "Serialize TPMT_SIG_SCHEME");
165
166 json_object_object_add(*jso, "signing_scheme", jso2);
167 }
168 jso2 = NULL;
169 r = ifapi_json_TPM2B_NAME_serialize(&in->name, &jso2);
170 return_if_error(r, "Serialize TPM2B_NAME");
171
172 json_object_object_add(*jso, "name", jso2);
173 return TSS2_RC_SUCCESS;
174 }
175
176 /** Serialize value of type IFAPI_EXT_PUB_KEY to json.
177 *
178 * @param[in] in value to be serialized.
179 * @param[out] jso pointer to the json object.
180 * @retval TSS2_RC_SUCCESS if the function call was a success.
181 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
182 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_EXT_PUB_KEY.
183 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
184 */
185 TSS2_RC
ifapi_json_IFAPI_EXT_PUB_KEY_serialize(const IFAPI_EXT_PUB_KEY * in,json_object ** jso)186 ifapi_json_IFAPI_EXT_PUB_KEY_serialize(const IFAPI_EXT_PUB_KEY *in,
187 json_object **jso)
188 {
189 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
190
191 TSS2_RC r;
192 json_object *jso2;
193
194 if (*jso == NULL)
195 *jso = json_object_new_object();
196 jso2 = NULL;
197 r = ifapi_json_char_serialize(in->pem_ext_public, &jso2);
198 return_if_error(r, "Serialize char");
199
200 json_object_object_add(*jso, "pem_ext_public", jso2);
201 jso2 = NULL;
202 if (in->certificate) {
203 r = ifapi_json_char_serialize(in->certificate, &jso2);
204 return_if_error(r, "Serialize char");
205
206 json_object_object_add(*jso, "certificate", jso2);
207 }
208 if (in->public.publicArea.type) {
209 /* Public area was initialized */
210 jso2 = NULL;
211 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->public, &jso2);
212 return_if_error(r, "Serialize TPM2B_PUBLIC");
213
214 json_object_object_add(*jso, "public", jso2);
215 }
216 return TSS2_RC_SUCCESS;
217 }
218
219 /** Serialize value of type IFAPI_NV to json.
220 *
221 * @param[in] in value to be serialized.
222 * @param[out] jso pointer to the json object.
223 * @retval TSS2_RC_SUCCESS if the function call was a success.
224 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
225 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_NV.
226 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
227 */
228 TSS2_RC
ifapi_json_IFAPI_NV_serialize(const IFAPI_NV * in,json_object ** jso)229 ifapi_json_IFAPI_NV_serialize(const IFAPI_NV *in, json_object **jso)
230 {
231 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
232
233 TSS2_RC r;
234 json_object *jso2;
235
236 if (*jso == NULL)
237 *jso = json_object_new_object();
238 jso2 = NULL;
239 r = ifapi_json_TPMI_YES_NO_serialize(in->with_auth, &jso2);
240 return_if_error(r, "Serialize TPMI_YES_NO");
241
242 json_object_object_add(*jso, "with_auth", jso2);
243
244 /* Add tag to classify json NV objects without deserialization */
245 jso2 = json_object_new_boolean(true);
246 json_object_object_add(*jso, "nv_object", jso2);
247
248 jso2 = NULL;
249 r = ifapi_json_TPM2B_NV_PUBLIC_serialize(&in->public, &jso2);
250 return_if_error(r, "Serialize TPM2B_NV_PUBLIC");
251
252 json_object_object_add(*jso, "public", jso2);
253 jso2 = NULL;
254 r = ifapi_json_UINT8_ARY_serialize(&in->serialization, &jso2);
255 return_if_error(r, "Serialize UINT8_ARY");
256
257 json_object_object_add(*jso, "serialization", jso2);
258 jso2 = NULL;
259 r = ifapi_json_UINT32_serialize(in->hierarchy, &jso2);
260 return_if_error(r, "Serialize UINT32");
261
262 json_object_object_add(*jso, "hierarchy", jso2);
263 jso2 = NULL;
264 r = ifapi_json_char_serialize(in->policyInstance, &jso2);
265 return_if_error(r, "Serialize char");
266
267 json_object_object_add(*jso, "policyInstance", jso2);
268 jso2 = NULL;
269 r = ifapi_json_char_serialize(in->description, &jso2);
270 return_if_error(r, "Serialize char");
271
272 json_object_object_add(*jso, "description", jso2);
273
274 if (in->appData.buffer != NULL) {
275 jso2 = NULL;
276 r = ifapi_json_UINT8_ARY_serialize(&in->appData, &jso2);
277 return_if_error(r, "Serialize UINT8_ARY");
278
279 json_object_object_add(*jso, "appData", jso2);
280 }
281 jso2 = NULL;
282 if (in->event_log) {
283 r = ifapi_json_char_serialize(in->event_log, &jso2);
284 return_if_error(r, "Serialize event log");
285
286 json_object_object_add(*jso, "event_log", jso2);
287 }
288 return TSS2_RC_SUCCESS;
289 }
290
291 /** Serialize value of type IFAPI_NV to json.
292 *
293 * @param[in] in value to be serialized.
294 * @param[out] jso pointer to the json object.
295 * @retval TSS2_RC_SUCCESS if the function call was a success.
296 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
297 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_NV.
298 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
299 */
300 TSS2_RC
ifapi_json_IFAPI_HIERARCHY_serialize(const IFAPI_HIERARCHY * in,json_object ** jso)301 ifapi_json_IFAPI_HIERARCHY_serialize(const IFAPI_HIERARCHY *in, json_object **jso)
302 {
303 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
304
305 TSS2_RC r;
306 json_object *jso2;
307
308 if (*jso == NULL)
309 *jso = json_object_new_object();
310 jso2 = NULL;
311 r = ifapi_json_TPMI_YES_NO_serialize(in->with_auth, &jso2);
312 return_if_error(r, "Serialize TPMI_YES_NO");
313
314 json_object_object_add(*jso, "with_auth", jso2);
315
316 jso2 = NULL;
317 r = ifapi_json_TPM2B_DIGEST_serialize(&in->authPolicy, &jso2);
318 return_if_error(r, "Serialize TPM2B_DIGEST");
319
320 json_object_object_add(*jso, "authPolicy", jso2);
321
322 jso2 = NULL;
323 r = ifapi_json_char_serialize(in->description, &jso2);
324 return_if_error(r, "Serialize char");
325
326 json_object_object_add(*jso, "description", jso2);
327
328 return TSS2_RC_SUCCESS;
329 }
330
331 /** Serialize value of type FAPI_QUOTE_INFO to json.
332 *
333 * @param[in] in value to be serialized.
334 * @param[out] jso pointer to the json object.
335 * @retval TSS2_RC_SUCCESS if the function call was a success.
336 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
337 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type FAPI_QUOTE_INFO.
338 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
339 */
340 TSS2_RC
ifapi_json_FAPI_QUOTE_INFO_serialize(const FAPI_QUOTE_INFO * in,json_object ** jso)341 ifapi_json_FAPI_QUOTE_INFO_serialize(const FAPI_QUOTE_INFO *in,
342 json_object **jso)
343 {
344 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
345
346 TSS2_RC r;
347 json_object *jso2;
348
349 if (*jso == NULL)
350 *jso = json_object_new_object();
351 jso2 = NULL;
352 r = ifapi_json_TPMT_SIG_SCHEME_serialize(&in->sig_scheme, &jso2);
353 return_if_error(r, "Serialize TPMT_SIG_SCHEME");
354
355 json_object_object_add(*jso, "sig_scheme", jso2);
356 jso2 = NULL;
357 r = ifapi_json_TPMS_ATTEST_serialize(&in->attest, &jso2);
358 return_if_error(r, "Serialize TPMS_ATTEST");
359
360 json_object_object_add(*jso, "attest", jso2);
361 return TSS2_RC_SUCCESS;
362 }
363
364
365 /** Serialize value of type IFAPI_DUPLICATE to json.
366 *
367 * @param[in] in value to be serialized.
368 * @param[out] jso pointer to the json object.
369 * @retval TSS2_RC_SUCCESS if the function call was a success.
370 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
371 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_DUPLICATE.
372 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
373 */
374 TSS2_RC
ifapi_json_IFAPI_DUPLICATE_serialize(const IFAPI_DUPLICATE * in,json_object ** jso)375 ifapi_json_IFAPI_DUPLICATE_serialize(const IFAPI_DUPLICATE *in,
376 json_object **jso)
377 {
378 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
379
380 TSS2_RC r;
381 json_object *jso2;
382
383 if (*jso == NULL)
384 *jso = json_object_new_object();
385 jso2 = NULL;
386 r = ifapi_json_TPM2B_PRIVATE_serialize(&in->duplicate, &jso2);
387 return_if_error(r, "Serialize TPM2B_PRIVATE");
388
389 json_object_object_add(*jso, "duplicate", jso2);
390 jso2 = NULL;
391 r = ifapi_json_TPM2B_ENCRYPTED_SECRET_serialize(&in->encrypted_seed, &jso2);
392 return_if_error(r, "Serialize TPM2B_ENCRYPTED_SECRET");
393
394 json_object_object_add(*jso, "encrypted_seed", jso2);
395 jso2 = NULL;
396 if (in->certificate) {
397 r = ifapi_json_char_serialize(in->certificate, &jso2);
398 return_if_error(r, "Serialize certificate");
399
400 json_object_object_add(*jso, "certificate", jso2);
401 }
402 jso2 = NULL;
403 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->public, &jso2);
404 return_if_error(r, "Serialize TPM2B_PUBLIC");
405
406 json_object_object_add(*jso, "public", jso2);
407
408 jso2 = NULL;
409 r = ifapi_json_TPM2B_PUBLIC_serialize(&in->public_parent, &jso2);
410 return_if_error(r, "Serialize TPM2B_PUBLIC");
411
412 json_object_object_add(*jso, "public_parent", jso2);
413 if (in->policy) {
414 jso2 = NULL;
415 r = ifapi_json_TPMS_POLICY_serialize(in->policy, &jso2);
416 return_if_error(r, "Serialize policy");
417
418 json_object_object_add(*jso, "policy", jso2);
419 }
420
421 return TSS2_RC_SUCCESS;
422 }
423
424 /** Serialize value of type IFAPI_OBJECT_TYPE_CONSTANT to json.
425 *
426 * @param[in] in value to be serialized.
427 * @param[out] jso pointer to the json object.
428 * @retval TSS2_RC_SUCCESS if the function call was a success.
429 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
430 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type TPM2_HANDLE.
431 */
432 TSS2_RC
ifapi_json_IFAPI_OBJECT_TYPE_CONSTANT_serialize(const IFAPI_OBJECT_TYPE_CONSTANT in,json_object ** jso)433 ifapi_json_IFAPI_OBJECT_TYPE_CONSTANT_serialize(const IFAPI_OBJECT_TYPE_CONSTANT
434 in, json_object **jso)
435 {
436 *jso = json_object_new_int(in);
437 if (*jso == NULL) {
438 LOG_ERROR("Bad value %"PRIx32 "", in);
439 return TSS2_FAPI_RC_BAD_VALUE;
440 }
441 return TSS2_RC_SUCCESS;
442 }
443
444 /** Serialize a IFAPI_OBJECT to json.
445 *
446 * @param[in] in value to be serialized.
447 * @param[out] jso pointer to the json object.
448 * @retval TSS2_RC_SUCCESS if the function call was a success.
449 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
450 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_OBJECT.
451 * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
452 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
453 */
454 TSS2_RC
ifapi_json_IFAPI_OBJECT_serialize(const IFAPI_OBJECT * in,json_object ** jso)455 ifapi_json_IFAPI_OBJECT_serialize(const IFAPI_OBJECT *in,
456 json_object **jso)
457 {
458 TSS2_RC r;
459
460 if (*jso == NULL)
461 *jso = json_object_new_object();
462 return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
463 json_object *jso2;
464
465 jso2 = NULL;
466 r = ifapi_json_IFAPI_OBJECT_TYPE_CONSTANT_serialize(in->objectType, &jso2);
467 return_if_error(r, "Serialize IFAPI_OBJECT");
468
469 json_object_object_add(*jso, "objectType", jso2);
470 jso2 = NULL;
471 r = ifapi_json_TPMI_YES_NO_serialize(in->system, &jso2);
472 return_if_error(r, "Serialize TPMI_YES_NO");
473
474 json_object_object_add(*jso, "system", jso2);
475
476 switch (in->objectType) {
477 case IFAPI_HIERARCHY_OBJ:
478 r = ifapi_json_IFAPI_HIERARCHY_serialize(&in->misc.hierarchy, jso);
479 return_if_error(r, "Error serialize FAPI hierarchy object");
480
481 break;
482 case IFAPI_NV_OBJ:
483 r = ifapi_json_IFAPI_NV_serialize(&in->misc.nv, jso);
484 return_if_error(r, "Error serialize FAPI NV object");
485
486 break;
487
488 case IFAPI_DUPLICATE_OBJ:
489 r = ifapi_json_IFAPI_DUPLICATE_serialize(&in->misc.key_tree, jso);
490 return_if_error(r, "Serialize IFAPI_OBJECT");
491
492 break;
493
494 case IFAPI_KEY_OBJ:
495 r = ifapi_json_IFAPI_KEY_serialize(&in->misc.key, jso);
496 return_if_error(r, "Error serialize FAPI KEY object");
497 break;
498
499 case IFAPI_EXT_PUB_KEY_OBJ:
500 r = ifapi_json_IFAPI_EXT_PUB_KEY_serialize(&in->misc.ext_pub_key, jso);
501 return_if_error(r, "Serialize IFAPI_OBJECT");
502
503 break;
504
505 default:
506 return_error(TSS2_FAPI_RC_GENERAL_FAILURE, "Invalid call get_json");
507 }
508
509 if (in->policy) {
510 jso2 = NULL;
511 r = ifapi_json_TPMS_POLICY_serialize(in->policy, &jso2);
512 return_if_error(r, "Serialize policy");
513
514 json_object_object_add(*jso, "policy", jso2);
515 }
516
517 if (in->policy) {
518 jso2 = NULL;
519 r = ifapi_json_TPMS_POLICY_serialize(in->policy, &jso2);
520 return_if_error(r, "Serialize policy");
521
522 json_object_object_add(*jso, "policy", jso2);
523 }
524 return TSS2_RC_SUCCESS;
525 }
526
527 /** Serialize value of type IFAPI_CAP_INFO to json.
528 *
529 * @param[in] in value to be serialized.
530 * @param[out] jso pointer to the json object.
531 * @retval TSS2_RC_SUCCESS if the function call was a success.
532 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
533 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_INFO.
534 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
535 */
536 TSS2_RC
ifapi_json_IFAPI_CAP_INFO_serialize(const IFAPI_CAP_INFO * in,json_object ** jso)537 ifapi_json_IFAPI_CAP_INFO_serialize(const IFAPI_CAP_INFO *in, json_object **jso)
538 {
539 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
540
541 TSS2_RC r;
542 json_object *jso2;
543
544 if (*jso == NULL)
545 *jso = json_object_new_object();
546 jso2 = NULL;
547 r = ifapi_json_char_serialize(in->description, &jso2);
548 return_if_error(r, "Serialize char");
549
550 json_object_object_add(*jso, "description", jso2);
551
552 jso2 = NULL;
553 r = ifapi_json_TPMS_CAPABILITY_DATA_serialize(in->capability, &jso2);
554 return_if_error(r, "Serialize TPMS_CAPABILITY_DATA");
555
556 json_object_object_add(*jso, "info", jso2);
557
558 return TSS2_RC_SUCCESS;
559 }
560
561 /** Serialize value of type IFAPI_INFO to json.
562 *
563 * @param[in] in value to be serialized.
564 * @param[out] jso pointer to the json object.
565 * @retval TSS2_RC_SUCCESS if the function call was a success.
566 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
567 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_INFO.
568 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
569 */
570 TSS2_RC
ifapi_json_IFAPI_INFO_serialize(const IFAPI_INFO * in,json_object ** jso)571 ifapi_json_IFAPI_INFO_serialize(const IFAPI_INFO *in, json_object **jso)
572 {
573 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
574
575 TSS2_RC r;
576 json_object *jso2;
577 json_object *jso_cap_list;
578 size_t i;
579
580 if (*jso == NULL)
581 *jso = json_object_new_object();
582 jso2 = NULL;
583 r = ifapi_json_char_serialize(in->fapi_version, &jso2);
584 return_if_error(r, "Serialize char");
585
586 json_object_object_add(*jso, "version", jso2);
587 jso2 = NULL;
588 r = ifapi_json_char_serialize(in->fapi_config, &jso2);
589 return_if_error(r, "Serialize char");
590
591 json_object_object_add(*jso, "fapi_config", jso2);
592 jso_cap_list = json_object_new_array();
593
594 for (i = 0; i < IFAPI_MAX_CAP_INFO; i++) {
595 jso2 = NULL;
596 r = ifapi_json_IFAPI_CAP_INFO_serialize(&in->cap[i], &jso2);
597 return_if_error(r, "Serialize TPMS_CAPABILITY_DATA");
598
599 json_object_array_add(jso_cap_list, jso2);
600
601 }
602 json_object_object_add(*jso, "capabilities", jso_cap_list);
603
604 return TSS2_RC_SUCCESS;
605 }
606
607 /** Serialize IFAPI_EVENT_TYPE to json.
608 *
609 * @param[in] in constant to be serialized.
610 * @param[out] jso pointer to the json object.
611 * @retval TSS2_RC_SUCCESS if the function call was a success.
612 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
613 * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type IFAPI_EVENT_TYPE.
614 */
615 TSS2_RC
ifapi_json_IFAPI_EVENT_TYPE_serialize(const IFAPI_EVENT_TYPE in,json_object ** jso)616 ifapi_json_IFAPI_EVENT_TYPE_serialize(const IFAPI_EVENT_TYPE in,
617 json_object **jso)
618 {
619 return ifapi_json_IFAPI_EVENT_TYPE_serialize_txt(in, jso);
620 }
621
622 typedef struct {
623 IFAPI_EVENT_TYPE in;
624 char *name;
625 } IFAPI_EVENT_TYPE_ASSIGN;
626
627 static IFAPI_EVENT_TYPE_ASSIGN serialize_IFAPI_EVENT_TYPE_tab[] = {
628 { IFAPI_IMA_EVENT_TAG, "ima-legacy" },
629 { IFAPI_TSS_EVENT_TAG, "tss2" },
630 };
631
632 /** Get json object for a constant, if a variable is actually of type IFAPI_EVENT_TYPE.
633 *
634 * @param[in] in binary value of constant.
635 * @param[out] str_jso object with text representing the constant.
636 * @retval TSS2_RC_SUCCESS if the function call was a success.
637 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
638 * @retval TSS2_FAPI_RC_BAD_VALUE if the constant is not of type IFAPI_EVENT_TYPE.
639 */
640 TSS2_RC
ifapi_json_IFAPI_EVENT_TYPE_serialize_txt(const IFAPI_EVENT_TYPE in,json_object ** str_jso)641 ifapi_json_IFAPI_EVENT_TYPE_serialize_txt(
642 const IFAPI_EVENT_TYPE in,
643 json_object **str_jso)
644 {
645 size_t n = sizeof(serialize_IFAPI_EVENT_TYPE_tab) / sizeof(
646 serialize_IFAPI_EVENT_TYPE_tab[0]);
647 size_t i;
648 for (i = 0; i < n; i++) {
649 if (serialize_IFAPI_EVENT_TYPE_tab[i].in == in) {
650 *str_jso = json_object_new_string(serialize_IFAPI_EVENT_TYPE_tab[i].name);
651 return_if_null(str_jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
652
653 return TSS2_RC_SUCCESS;
654 }
655 }
656 return_error(TSS2_FAPI_RC_BAD_VALUE, "Undefined constant.");
657 }
658
659 /** Serialize value of type IFAPI_TSS_EVENT to json.
660 *
661 * @param[in] in value to be serialized.
662 * @param[out] jso pointer to the json object.
663 * @retval TSS2_RC_SUCCESS if the function call was a success.
664 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
665 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_TSS_EVENT.
666 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
667 */
668 TSS2_RC
ifapi_json_IFAPI_TSS_EVENT_serialize(const IFAPI_TSS_EVENT * in,json_object ** jso)669 ifapi_json_IFAPI_TSS_EVENT_serialize(const IFAPI_TSS_EVENT *in,
670 json_object **jso)
671 {
672 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
673
674 TSS2_RC r;
675 json_object *jso2;
676
677 if (*jso == NULL)
678 *jso = json_object_new_object();
679 jso2 = NULL;
680 r = ifapi_json_TPM2B_EVENT_serialize(&in->data, &jso2);
681 return_if_error(r, "Serialize TPM2B_EVENT");
682
683 json_object_object_add(*jso, "data", jso2);
684
685 if (in->event) {
686 /* The in->event field is somewhat special. Its an arbitrary json
687 object that shall be serialized under the event field. Thus we
688 first have to deserialize the string before we can add it to
689 the data structure. */
690 jso2 = json_tokener_parse(in->event);
691 return_if_null(jso2, "Event is not valid JSON.", TSS2_FAPI_RC_BAD_VALUE);
692
693 json_object_object_add(*jso, "event", jso2);
694 }
695 return TSS2_RC_SUCCESS;
696 }
697
698 /** Serialize value of type IFAPI_IMA_EVENT to json.
699 *
700 * @param[in] in value to be serialized.
701 * @param[out] jso pointer to the json object.
702 * @retval TSS2_RC_SUCCESS if the function call was a success.
703 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
704 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_IMA_EVENT.
705 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
706 */
707 TSS2_RC
ifapi_json_IFAPI_IMA_EVENT_serialize(const IFAPI_IMA_EVENT * in,json_object ** jso)708 ifapi_json_IFAPI_IMA_EVENT_serialize(const IFAPI_IMA_EVENT *in,
709 json_object **jso)
710 {
711 return_if_null(in, "Bad reference.", TSS2_FAPI_RC_BAD_REFERENCE);
712
713 TSS2_RC r;
714 json_object *jso2;
715
716 if (*jso == NULL)
717 *jso = json_object_new_object();
718 jso2 = NULL;
719 r = ifapi_json_TPM2B_DIGEST_serialize(&in->eventData, &jso2);
720 return_if_error(r, "Serialize TPM2B_DIGEST");
721
722 json_object_object_add(*jso, "eventData", jso2);
723 jso2 = NULL;
724 r = ifapi_json_char_serialize(in->eventName, &jso2);
725 return_if_error(r, "Serialize char");
726
727 json_object_object_add(*jso, "eventName", jso2);
728 return TSS2_RC_SUCCESS;
729 }
730
731 /** Serialize a IFAPI_EVENT_UNION to json.
732 *
733 * This function expects the Bitfield to be encoded as unsigned int in host-endianess.
734 * @param[in] in the value to be serialized.
735 * @param[in] selector the type of the event.
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 IFAPI_EVENT_UNION.
740 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
741 */
742 TSS2_RC
ifapi_json_IFAPI_EVENT_UNION_serialize(const IFAPI_EVENT_UNION * in,UINT32 selector,json_object ** jso)743 ifapi_json_IFAPI_EVENT_UNION_serialize(const IFAPI_EVENT_UNION *in,
744 UINT32 selector, json_object **jso)
745 {
746 if (*jso == NULL)
747 *jso = json_object_new_object();
748 return_if_null(*jso, "Out of memory.", TSS2_FAPI_RC_MEMORY);
749
750 switch (selector) {
751 case IFAPI_TSS_EVENT_TAG:
752 return ifapi_json_IFAPI_TSS_EVENT_serialize(&in->tss_event, jso);
753 case IFAPI_IMA_EVENT_TAG:
754 return ifapi_json_IFAPI_IMA_EVENT_serialize(&in->ima_event, jso);
755 default:
756 LOG_ERROR("\nSelector %"PRIx32 " did not match", selector);
757 return TSS2_SYS_RC_BAD_VALUE;
758 };
759 return TSS2_RC_SUCCESS;
760 }
761
762 /** Serialize value of type IFAPI_EVENT to json.
763 *
764 * @param[in] in value to be serialized.
765 * @param[out] jso pointer to the json object.
766 * @retval TSS2_RC_SUCCESS if the function call was a success.
767 * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory.
768 * @retval TSS2_FAPI_RC_BAD_VALUE if the value is not of type IFAPI_EVENT.
769 * @retval TSS2_FAPI_RC_BAD_REFERENCE a invalid null pointer is passed.
770 */
771 TSS2_RC
ifapi_json_IFAPI_EVENT_serialize(const IFAPI_EVENT * in,json_object ** jso)772 ifapi_json_IFAPI_EVENT_serialize(const IFAPI_EVENT *in, 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
779 if (*jso == NULL)
780 *jso = json_object_new_object();
781 jso2 = NULL;
782 r = ifapi_json_UINT32_serialize(in->recnum, &jso2);
783 return_if_error(r, "Serialize UINT32");
784
785 json_object_object_add(*jso, "recnum", jso2);
786 jso2 = NULL;
787 r = ifapi_json_TPM2_HANDLE_serialize(in->pcr, &jso2);
788 return_if_error(r, "Serialize TPM2_HANDLE");
789
790 json_object_object_add(*jso, "pcr", jso2);
791 jso2 = NULL;
792 r = ifapi_json_TPML_DIGEST_VALUES_serialize(&in->digests, &jso2);
793 return_if_error(r, "Serialize TPML_DIGEST");
794
795 json_object_object_add(*jso, "digests", jso2);
796 jso2 = NULL;
797 r = ifapi_json_IFAPI_EVENT_TYPE_serialize(in->type, &jso2);
798 return_if_error(r, "Serialize IFAPI_EVENT_TYPE");
799
800 json_object_object_add(*jso, "type", jso2);
801 jso2 = NULL;
802 r = ifapi_json_IFAPI_EVENT_UNION_serialize(&in->sub_event, in->type, &jso2);
803 return_if_error(r, "Serialize IFAPI_EVENT_UNION");
804
805 json_object_object_add(*jso, "sub_event", jso2);
806 return TSS2_RC_SUCCESS;
807 }
808