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