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