• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2018, 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 <stdarg.h>
12 #include <inttypes.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <json-c/json_util.h>
17 #include <json-c/json_tokener.h>
18 
19 #include <setjmp.h>
20 #include <cmocka.h>
21 
22 #include "tss2_fapi.h"
23 #include "tpm_json_serialize.h"
24 #include "tpm_json_deserialize.h"
25 #include "ifapi_json_serialize.h"
26 #include "ifapi_json_deserialize.h"
27 #include "fapi_policy.h"
28 
29 #include "util/aux_util.h"
30 
31 #define LOGMODULE tests
32 #include "util/log.h"
33 
34 /* 3 copies from ifapi_helpers.c */
35 static void
cleanup_policy_element(TPMT_POLICYELEMENT * policy)36 cleanup_policy_element(TPMT_POLICYELEMENT *policy)
37 {
38         switch (policy->type) {
39         case POLICYSECRET:
40             SAFE_FREE(policy->element.PolicySecret.objectPath);
41             break;
42         case POLICYAUTHORIZE:
43             SAFE_FREE(policy->element.PolicyAuthorize.keyPath);
44             SAFE_FREE(policy->element.PolicyAuthorize.keyPEM);
45             break;
46         case POLICYAUTHORIZENV:
47             SAFE_FREE( policy->element.PolicyAuthorizeNv.nvPath);
48             SAFE_FREE( policy->element.PolicyAuthorizeNv.policy_buffer);
49             break;
50         case POLICYSIGNED:
51             SAFE_FREE(policy->element.PolicySigned.keyPath);
52             SAFE_FREE(policy->element.PolicySigned.keyPEM);
53             break;
54         case POLICYPCR:
55             SAFE_FREE(policy->element.PolicyPCR.pcrs);
56             break;
57         case POLICYNV:
58             SAFE_FREE(policy->element.PolicyNV.nvPath);
59             break;
60         case POLICYDUPLICATIONSELECT:
61             SAFE_FREE(policy->element.PolicyDuplicationSelect.newParentPath);
62             break;
63         }
64 }
65 
cleanup_policy_elements(TPML_POLICYELEMENTS * policy)66 static void cleanup_policy_elements(TPML_POLICYELEMENTS *policy)
67 {
68     size_t i, j;
69     if (policy != NULL) {
70         for (i = 0; i < policy->count; i++) {
71             if (policy->elements[i].type ==  POLICYOR) {
72                 /* Policy with sub policies */
73                 TPML_POLICYBRANCHES *branches = policy->elements[i].element.PolicyOr.branches;
74                 for (j = 0; j < branches->count; j++) {
75                     SAFE_FREE(branches->authorizations[j].name);
76                     SAFE_FREE(branches->authorizations[j].description);
77                     cleanup_policy_elements(branches->authorizations[j].policy);
78                 }
79                 SAFE_FREE(branches);
80             } else {
81                 cleanup_policy_element(&policy->elements[i]);
82             }
83         }
84         SAFE_FREE(policy);
85     }
86 }
87 
88 /** Free memory allocated during deserialization of policy.
89  *
90  * The object will not be freed (might be declared on the stack).
91  *
92  * @param[in]  object The policy to be cleaned up.
93  *
94  */
ifapi_cleanup_policy(TPMS_POLICY * policy)95 static void ifapi_cleanup_policy(TPMS_POLICY *policy)
96 {
97     if (policy) {
98        SAFE_FREE(policy->description);
99        SAFE_FREE(policy->policyAuthorizations);
100        cleanup_policy_elements(policy->policy);
101     }
102 }
103 
normalize(const char * string)104 char * normalize(const char *string) {
105     char *string2 = malloc(strlen(string)+1);
106     int i;
107     int j = 0;
108     for(i = 0; string[i] != '\0'; i++) {
109         if ((string[i] != '\n' && string[i] != ' ')) {
110             string2[j] = string[i];
111             j += 1;
112         }
113     }
114     string2[j] = '\0';
115 	return string2;
116 }
117 
118 #define CHECK_ERROR(TYPE, SRC, RC) \
119         { \
120             TYPE out; \
121             TSS2_RC rc; \
122             json_object *jso = json_tokener_parse((SRC)); \
123             assert_non_null(jso); \
124             rc = ifapi_json_ ## TYPE ## _deserialize (jso, &out); \
125             assert_int_equal (rc, RC); \
126             json_object_put(jso); \
127         }
128 
129 
130 
131 #define CHECK_JSON2(TYPE, SRC, DST, PSERIALIZE)  \
132         { \
133             TYPE out; \
134             TSS2_RC rc; \
135             json_object *jso = json_tokener_parse((SRC)); \
136             if (!jso) fprintf(stderr, "JSON parsing failed\n"); \
137             assert_non_null(jso); \
138             rc = ifapi_json_ ## TYPE ## _deserialize (jso, &out); \
139             if (rc) fprintf(stderr, "Deserialization failed\n"); \
140             assert_int_equal (rc, TSS2_RC_SUCCESS); \
141             json_object_put(jso); \
142             jso = NULL; \
143             rc = ifapi_json_ ## TYPE ## _serialize (PSERIALIZE, &jso); \
144             assert_int_equal (rc, TSS2_RC_SUCCESS); \
145             assert_non_null(jso); \
146             const char *jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY); \
147             assert_non_null(jso_string); \
148             char *string1 = normalize(jso_string); \
149             char *string2 =  normalize(DST); \
150             assert_string_equal(string1, string2); \
151             json_object_put(jso); \
152             free(string1); \
153             free(string2); \
154         }
155 
156 #define CHECK_JSON(TYPE, SRC, DST)  \
157     CHECK_JSON2(TYPE, SRC, DST, &out)
158 
159 #define CHECK_JSON_SIMPLE(TYPE, SRC, DST)  \
160     CHECK_JSON2(TYPE, SRC, DST, out)
161 
162 #define CHECK_JSON_TO_BIN(TYPE, SRC, DST) \
163         { \
164             TYPE out; \
165             TSS2_RC rc; \
166             TYPE expected = DST; \
167             json_object *jso = json_tokener_parse((SRC)); \
168             assert_non_null(jso); \
169             rc = ifapi_json_ ## TYPE ## _deserialize (jso, &out); \
170             assert_int_equal (rc, TSS2_RC_SUCCESS); \
171             json_object_put(jso); \
172             assert_true(out == expected);       \
173         }
174 
175 #define CHECK_BIN2(TYPE, BIN, PSERIALIZE)                  \
176     TYPE BIN ## 2; \
177     { \
178         char *jso_string1, *jso_string2; \
179         json_object *jso = NULL; \
180         TSS2_RC rc = ifapi_json_ ## TYPE ## _serialize (PSERIALIZE, &jso); \
181         jso_string1 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY)); \
182         assert_int_equal (rc, TSS2_RC_SUCCESS); \
183         rc = ifapi_json_ ## TYPE ## _deserialize (jso, &BIN ## 2); \
184         assert_int_equal (rc, TSS2_RC_SUCCESS); \
185         json_object_put(jso); \
186         jso = NULL; \
187         rc = ifapi_json_ ## TYPE ## _serialize (PSERIALIZE ## 2, &jso); \
188         jso_string2 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY)); \
189         assert_int_equal (rc, TSS2_RC_SUCCESS); \
190         if (strcmp(jso_string1, jso_string2)) { \
191             fprintf(stderr,"\n jso: %s\n", jso_string1); \
192             fprintf(stderr,"\n jso: %s\n", jso_string2); \
193         } \
194         assert_string_equal(jso_string1, jso_string2); \
195         json_object_put(jso); \
196         free(jso_string1); \
197         free(jso_string2); \
198     }
199 
200 #define CHECK_BIN(TYPE, BIN) \
201     CHECK_BIN2(TYPE, BIN, &BIN)
202 
203 #define CHECK_BIN_SIMPLE(TYPE, BIN) \
204     CHECK_BIN2(TYPE, BIN, BIN)
205 
206 static void
check_bin(void ** state)207 check_bin(void **state)
208 {
209     TPM2B_PUBLIC inPublicAES = {
210         .size = 0,
211         .publicArea = {
212             .type = TPM2_ALG_SYMCIPHER,
213             .nameAlg = TPM2_ALG_SHA256,
214             .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
215                                  TPMA_OBJECT_SIGN_ENCRYPT |
216                                  TPMA_OBJECT_DECRYPT),
217 
218             .authPolicy = {
219                  .size = 0,
220              },
221             .parameters.symDetail = {
222                  .sym = {
223                      .algorithm = TPM2_ALG_AES,
224                      .keyBits = {.aes = 128},
225                      .mode = {.aes = TPM2_ALG_CFB}}
226              },
227             .unique.sym = {
228                  .size = 0,
229                  .buffer = {}
230              }
231         }
232     };
233 
234     CHECK_BIN(TPM2B_PUBLIC, inPublicAES);
235 
236     TPM2B_PUBLIC inPublicECC = {
237             .size = 0,
238             .publicArea = {
239                 .type = TPM2_ALG_ECC,
240                 .nameAlg = TPM2_ALG_SHA1,
241                 .objectAttributes = (
242                              TPMA_OBJECT_USERWITHAUTH |
243                              TPMA_OBJECT_RESTRICTED |
244                              TPMA_OBJECT_SIGN_ENCRYPT |
245                              TPMA_OBJECT_FIXEDTPM |
246                              TPMA_OBJECT_FIXEDPARENT |
247                              TPMA_OBJECT_SENSITIVEDATAORIGIN
248                              ),
249                 .authPolicy = {
250                          .size = 0,
251                      },
252 
253                 .parameters.eccDetail = {
254                      .symmetric = {
255                          .algorithm = TPM2_ALG_NULL,
256                          .keyBits.aes = 128,
257                          .mode.aes = TPM2_ALG_ECB,
258                      },
259                      .scheme = {
260                           .scheme = TPM2_ALG_ECDAA,
261                           .details = { .ecdaa = { .hashAlg = TPM2_ALG_SHA256 }},
262                       },
263                      .curveID = TPM2_ECC_BN_P256,
264                      .kdf = { .scheme = TPM2_ALG_NULL, .details = {} }
265                  },
266                 /*
267                   .parameters.asymDetail.symmetric.algorithm = TPM2_ALG_NULL,
268                 */
269                 .unique.ecc = {
270                      .x = { .size = 0, .buffer = {} } ,
271                      .y = { .size = 0, .buffer = {} } ,
272                  },
273             },
274         };
275 
276 
277     CHECK_BIN(TPM2B_PUBLIC, inPublicECC);
278 
279     TPM2B_PUBLIC inPublicRSA2 = {
280         .size = 0,
281         .publicArea = {
282             .type = TPM2_ALG_RSA,
283             .nameAlg = TPM2_ALG_SHA1,
284             .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
285                                  TPMA_OBJECT_SIGN_ENCRYPT  |
286                                  TPMA_OBJECT_FIXEDTPM |
287                                  TPMA_OBJECT_FIXEDPARENT |
288                                  TPMA_OBJECT_SENSITIVEDATAORIGIN),
289             .authPolicy = {
290                  .size = 0,
291              },
292             .parameters.rsaDetail = {
293                  .symmetric = {
294                      .algorithm = TPM2_ALG_NULL,
295                      .keyBits.aes = 128,
296                      .mode.aes = TPM2_ALG_CFB},
297                  .scheme = {
298                       .scheme = TPM2_ALG_RSAPSS,
299                       .details = {
300                           .rsapss = { .hashAlg = TPM2_ALG_SHA1 }
301                       }
302                   },
303                  .keyBits = 2048,
304                  .exponent = 0,
305              },
306             .unique.rsa = {
307                  .size = 0,
308                  .buffer = {},
309              },
310         },
311     };
312 
313     CHECK_BIN(TPM2B_PUBLIC, inPublicRSA2);
314 
315     TPMT_SIG_SCHEME ecc_scheme = { .scheme = TPM2_ALG_ECDSA, .details.ecdsa = TPM2_ALG_SHA1 };
316 
317     CHECK_BIN(TPMT_SIG_SCHEME, ecc_scheme);
318 
319     TPMT_SIG_SCHEME rsa_scheme = { .scheme = TPM2_ALG_NULL };
320 
321     CHECK_BIN(TPMT_SIG_SCHEME, rsa_scheme);
322 
323     TPMA_NV testNV = 0xffffff0f ;
324 
325     CHECK_BIN_SIMPLE(TPMA_NV, testNV);
326 
327     TPML_PCR_SELECTION pcr_selection = {
328         .count = 3,
329         .pcrSelections = {
330          {
331             .hash = TPM2_ALG_SHA1,
332             .sizeofSelect = 3,
333             .pcrSelect = { 01, 00, 03 }},
334         {
335             .hash = TPM2_ALG_SHA256,
336             .sizeofSelect = 3,
337             .pcrSelect = { 01 ,00 ,03 }},
338         {
339             .hash = TPM2_ALG_SHA384,
340             .sizeofSelect = 3,
341             .pcrSelect = { 02, 00, 02 }}
342         }
343     };
344 
345     CHECK_BIN(TPML_PCR_SELECTION, pcr_selection);
346 
347     IFAPI_IMA_EVENT imaEvent = {
348         .eventData = {
349             .size = 0,
350             .buffer = { 0 }
351         },
352         .eventName = "Event"
353     };
354 
355     CHECK_BIN(IFAPI_IMA_EVENT, imaEvent);
356     free(imaEvent2.eventName);
357 }
358 
359 static void
check_policy_bin(void ** state)360 check_policy_bin(void **state)
361 {
362     TPMS_PCRVALUE pcr_value;
363     TPML_PCRVALUES *pcr_value_list;
364     TPML_POLICYBRANCHES *or_branch_list;
365     TPMS_POLICYPCR pcr_policy;
366     TPMT_POLICYELEMENT policy_element0;
367     TPMT_POLICYELEMENT policy_element1;
368     TPMT_POLICYELEMENT policy_element_or;
369     TPML_POLICYELEMENTS *policy_elements_or;
370     TPML_POLICYELEMENTS *policy_elements0;
371     TPML_POLICYELEMENTS *policy_elements1;
372     TPMS_POLICY policy;
373     TPMS_POLICYBRANCH branch0;
374     TPMS_POLICYBRANCH branch1;
375 
376     pcr_value.pcr = 16;
377     pcr_value.hashAlg = TPM2_ALG_SHA1;
378     memset(&pcr_value.digest, 0, sizeof(TPMU_HA));
379     memset(&pcr_policy, 0, sizeof(TPMS_POLICYPCR));
380     pcr_value_list = calloc(1, sizeof(TPML_PCRVALUES) + sizeof(TPMS_PCRVALUE));
381     if (pcr_value_list == NULL) {
382         LOG_ERROR("%s", "Out of memory.");
383         return;
384     }
385     pcr_value_list->count = 1;
386     pcr_value_list->pcrs[0] = pcr_value;
387     pcr_policy.pcrs = pcr_value_list;
388     memset(&policy_element0, 0, sizeof(TPMT_POLICYELEMENT));
389     policy_element0.element.PolicyPCR = pcr_policy;
390     policy_element0.type = POLICYPCR;
391     memset(&policy_element1, 0, sizeof(TPMT_POLICYELEMENT));
392     policy_element1.element.PolicyPCR = pcr_policy;
393     policy_element1.type = POLICYPCR;
394     policy_elements0 = calloc(1, sizeof(TPML_POLICYELEMENTS) + sizeof(TPMT_POLICYELEMENT));
395     if (policy_elements0 == NULL) {
396         LOG_ERROR("%s", "Out of memory.");
397         if (pcr_policy.pcrs){
398             free(pcr_policy.pcrs);
399         }
400         return;
401     }
402     policy_elements0->count = 1;
403     policy_elements0->elements[0] = policy_element0;
404     policy.policy = policy_elements0;
405     policy.description = "hareness description";
406     policy.policyAuthorizations = NULL;
407     memset(&policy.policyDigests, 0, sizeof(TPML_DIGEST_VALUES));
408 
409     //CHECK_BIN(TPMS_POLICY, policy);
410     {
411         char *jso_string1, *jso_string2;
412         json_object *jso = NULL;
413         TSS2_RC rc = ifapi_json_TPMS_POLICY_serialize (&policy, &jso);
414         jso_string1 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY));
415         assert_int_equal (rc, TSS2_RC_SUCCESS);
416         rc = ifapi_json_TPMS_POLICY_deserialize (jso, &policy);
417         assert_int_equal (rc, TSS2_RC_SUCCESS);
418         json_object_put(jso);
419         jso = NULL;
420         rc = ifapi_json_TPMS_POLICY_serialize (&policy, &jso);
421         jso_string2 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY));
422         assert_int_equal (rc, TSS2_RC_SUCCESS);
423         if (strcmp(jso_string1, jso_string2)) {
424             fprintf(stderr,"\n jso: %s\n", jso_string1);
425             fprintf(stderr,"\n jso: %s\n", jso_string2);
426         }
427         assert_string_equal(jso_string1, jso_string2);
428         json_object_put(jso);
429         free(jso_string1);
430         free(jso_string2);
431     }
432     ifapi_cleanup_policy(&policy);
433 
434     or_branch_list = calloc(2, sizeof(TPML_POLICYBRANCHES) + (2 * sizeof(TPMS_POLICYBRANCH)));
435     if (or_branch_list == NULL) {
436         LOG_ERROR("%s", "Out of memory.");
437         return;
438     }
439     or_branch_list->count = 2;
440 
441     policy_elements1 = calloc(1, sizeof(TPML_POLICYELEMENTS) + sizeof(TPMT_POLICYELEMENT));
442     if (policy_elements1 == NULL) {
443         LOG_ERROR("%s", "Out of memory.");
444         if (or_branch_list){
445             free(or_branch_list);
446         }
447         return;
448     }
449     policy_elements1->count = 1;
450     policy_elements1->elements[0] = policy_element1;
451 
452     memset(&branch0, 0, sizeof(TPMS_POLICYBRANCH));
453     memset(&branch1, 0, sizeof(TPMS_POLICYBRANCH));
454     branch0.policy = policy_elements0;
455     branch0.name = "branch0";
456     branch0.description = "description branch 0";
457     branch1.policy = policy_elements1;
458     branch1.name = "branch1";
459     branch1.description = "description branch 1";
460     memcpy(&or_branch_list->authorizations[0], &branch0, sizeof(TPMS_POLICYBRANCH));
461     memcpy(&or_branch_list->authorizations[1], &branch1, sizeof(TPMS_POLICYBRANCH));
462     //or_policy.pcrs = pcr_branch_list;
463 
464     policy_elements_or = calloc(1, sizeof(TPML_POLICYELEMENTS) + sizeof(TPMT_POLICYELEMENT));
465     if (policy_elements_or == NULL) {
466         LOG_ERROR("%s", "Out of memory.");
467         if (or_branch_list) {
468             free(or_branch_list);
469         }
470         return;
471     }
472     policy_elements_or->count = 1;
473 
474     memset(&policy_element_or, 0, sizeof(TPMT_POLICYELEMENT));
475     policy_element_or.element.PolicyOr.branches = or_branch_list;
476     policy_element_or.type = POLICYOR;
477     policy_elements_or->elements[0] = policy_element_or;
478     policy.policy =  policy_elements_or;
479 
480     //CHECK_BIN(TPMS_POLICY, policy);
481     {
482         char *jso_string1, *jso_string2;
483         json_object *jso = NULL;
484         TSS2_RC rc = ifapi_json_TPMS_POLICY_serialize (&policy, &jso);
485         jso_string1 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY));
486         assert_int_equal (rc, TSS2_RC_SUCCESS);
487         rc = ifapi_json_TPMS_POLICY_deserialize (jso, &policy);
488         assert_int_equal (rc, TSS2_RC_SUCCESS);
489         json_object_put(jso);
490         jso = NULL;
491         rc = ifapi_json_TPMS_POLICY_serialize (&policy, &jso);
492         jso_string2 = strdup(json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY));
493         assert_int_equal (rc, TSS2_RC_SUCCESS);
494         if (strcmp(jso_string1, jso_string2)) {
495             fprintf(stderr,"\n jso: %s\n", jso_string1);
496             fprintf(stderr,"\n jso: %s\n", jso_string2);
497         }
498         assert_string_equal(jso_string1, jso_string2);
499         json_object_put(jso);
500         free(jso_string1);
501         free(jso_string2);
502     }
503     ifapi_cleanup_policy(&policy);
504 
505     free(policy_elements_or);
506     free(policy_elements0);
507     free(policy_elements1);
508     free(or_branch_list);
509     free(pcr_value_list);
510 }
511 
512 static void
check_json_to_bin(void ** state)513 check_json_to_bin(void **state)
514 {
515     CHECK_JSON_TO_BIN(UINT64, "22147483647", 22147483647);
516     CHECK_JSON_TO_BIN(UINT64, "\"0xffffffff\"", 0xffffffff);
517     CHECK_JSON_TO_BIN(UINT64, "\"0xfffffffff\"", 0xfffffffff);
518     CHECK_JSON_TO_BIN(UINT32,  "\"0xFfffffff\"", 0xffffffff);
519     CHECK_JSON_TO_BIN(UINT16, "\"0xffff\"", 0xffff);
520 }
521 
522 static void
check_json_structs(void ** state)523 check_json_structs(void **state)
524 {
525     const char *test_json_TPMS_POLICYTEMPLATE =
526         "{\n"
527         "  \"templateHash\": \"0011223344556677889900112233445566778899\"\n"
528         "}";
529     CHECK_JSON(TPMS_POLICYTEMPLATE, test_json_TPMS_POLICYTEMPLATE, test_json_TPMS_POLICYTEMPLATE);
530 
531     const char *test_json_TPM2B_PUBLIC_expected =
532         "{\n"
533         "  \"size\":0,\n"
534         "  \"publicArea\":{\n"
535         "    \"type\":\"ECC\",\n"
536         "    \"nameAlg\":\"SHA1\",\n"
537         "\"objectAttributes\":{"
538         "      \"fixedTPM\":1,"
539         "      \"stClear\":0,"
540         "      \"fixedParent\":1,"
541         "      \"sensitiveDataOrigin\":1,"
542         "      \"userWithAuth\":1,"
543         "      \"adminWithPolicy\":0,"
544         "      \"noDA\":0,"
545         "      \"encryptedDuplication\":0,"
546         "      \"restricted\":1,"
547         "      \"decrypt\":0,"
548         "      \"sign\":1"
549         "    },"
550         "    \"authPolicy\":\"\",\n"
551         "    \"parameters\":{\n"
552         "      \"symmetric\":{\n"
553         "        \"algorithm\":\"NULL\"\n"
554         "      },\n"
555         "      \"scheme\":{\n"
556         "        \"scheme\":\"ECDAA\",\n"
557         "        \"details\":{\n"
558         "          \"hashAlg\":\"SHA256\",\n"
559         "          \"count\":0\n"
560         "        }\n"
561         "      },\n"
562         "      \"curveID\":\"BN_P256\",\n"
563         "      \"kdf\":{\n"
564         "        \"scheme\":\"NULL\"\n"
565         "      }\n"
566         "    },\n"
567         "    \"unique\":{\n"
568         "      \"x\": \"\",\n"
569         "      \"y\": \"\"\n"
570         "    }\n"
571         "  }\n"
572         "}";
573 
574     const char *test_json_TPM2B_PUBLIC_src=
575         "{"
576         "  \"size\":0,"
577         "  \"publicArea\":{"
578         "    \"type\":\"ECC\","
579         "    \"nameAlg\":\"SHA1\","
580         "    \"objectAttributes\":["
581         "      \"fixedTPM\","
582         "      \"fixedParent\","
583         "      \"sensitiveDataOrigin\","
584         "      \"userWithAuth\","
585         "      \"restricted\","
586         "      \"sign\""
587         "    ],"
588         "    \"authPolicy\":\"\","
589         "    \"parameters\":{"
590         "      \"symmetric\":{"
591         "        \"algorithm\":\"NULL\""
592         "      },"
593         "      \"scheme\":{"
594         "        \"scheme\":\"ECDAA\","
595         "        \"details\":{"
596         "          \"hashAlg\":\"SHA256\","
597         "          \"count\":0"
598         "        }"
599         "      },"
600         "      \"curveID\":\"ECC_BN_P256\","
601         "      \"kdf\":{"
602         "        \"scheme\":\"NULL\""
603         "      }"
604         "    },"
605         "    \"unique\":{"
606         "      \"x\": \"\",\n"
607         "      \"y\": \"\"\n"
608         "    }"
609         "  }"
610         "}"
611         "";
612     const char *test_json_TPM2B_PUBLIC_dwnc_src =
613         "{"
614         "  \"size\":0,"
615         "  \"publicArea\":{"
616         "    \"type\":\"ecc\","
617         "    \"nameAlg\":\"sha1\","
618         "    \"objectAttributes\":["
619         "      \"fixedTPM\","
620         "      \"fixedParent\","
621         "      \"sensitiveDataOrigin\","
622         "      \"userWithAuth\","
623         "      \"restricted\","
624         "      \"sign\""
625         "    ],"
626         "    \"authPolicy\":\"\","
627         "    \"parameters\":{"
628         "      \"symmetric\":{"
629         "        \"algorithm\":\"null\""
630         "      },"
631         "      \"scheme\":{"
632         "        \"scheme\":\"ecdaa\","
633         "        \"details\":{"
634         "          \"hashAlg\":\"sha256\","
635         "          \"count\":0"
636         "        }"
637         "      },"
638         "      \"curveID\":\"ecc_BN_P256\","
639         "      \"kdf\":{"
640         "        \"scheme\":\"null\""
641         "      }"
642         "    },"
643         "    \"unique\":{"
644         "      \"x\": \"\",\n"
645         "      \"y\": \"\"\n"
646         "      }"
647         "    }"
648         "  }"
649         "}"
650         "";
651 
652     CHECK_JSON(TPM2B_PUBLIC, test_json_TPM2B_PUBLIC_src, test_json_TPM2B_PUBLIC_expected);
653     CHECK_JSON(TPM2B_PUBLIC, test_json_TPM2B_PUBLIC_dwnc_src, test_json_TPM2B_PUBLIC_expected);
654 
655     const char *test_json_TPMS_ATTEST_certify_src =
656         "{\n"
657         "    \"magic\": \"0xff544347\",\n"
658         "    \"type\": \"ST_ATTEST_CERTIFY\",\n"
659         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
660         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
661         "    \"clockInfo\": {\n"
662         "        \"clock\": 123,\n"
663         "        \"resetCount\": 23,\n"
664         "        \"restartCount\": 1,\n"
665         "        \"safe\": \"yes\"\n"
666         "    },\n"
667         "    \"firmwareVersion\": 783,\n"
668         "    \"attested\": {\n"
669         "        \"name\": \"0x00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
670         "        \"qualifiedName\": \"0x00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
671         "    }\n"
672         "}";
673     const char *test_json_TPMS_ATTEST_certify_expt =
674         "{\n"
675         "    \"magic\": \"VALUE\",\n"
676         "    \"type\": \"ATTEST_CERTIFY\",\n"
677         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
678         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
679         "    \"clockInfo\": {\n"
680         "        \"clock\": 123,\n"
681         "        \"resetCount\": 23,\n"
682         "        \"restartCount\": 1,\n"
683         "        \"safe\": \"YES\"\n"
684         "    },\n"
685         "    \"firmwareVersion\": 783,\n"
686         "    \"attested\": {\n"
687         "        \"name\": \"00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
688         "        \"qualifiedName\": \"00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
689         "    }\n"
690         "}";
691     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_certify_src, test_json_TPMS_ATTEST_certify_expt);
692 
693     const char *test_json_TPMS_ATTEST_sessionaudit_src =
694         "{\n"
695         "    \"magic\": \"0xff544347\",\n"
696         "    \"type\": \"ST_ATTEST_SESSION_AUDIT\",\n"
697         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
698         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
699         "    \"clockInfo\": {\n"
700         "        \"clock\": [12345,0],\n"
701         "        \"resetCount\": 23,\n"
702         "        \"restartCount\": 1,\n"
703         "        \"safe\": \"yes\"\n"
704         "    },\n"
705         "    \"firmwareVersion\": [783783,0],\n"
706         "    \"attested\": {\n"
707         "        \"exclusiveSession\": \"yes\",\n"
708         "        \"sessionDigest\": \"0x00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
709         "    }\n"
710         "}";
711     const char *test_json_TPMS_ATTEST_sessionaudit_expt =
712         "{\n"
713         "    \"magic\": \"VALUE\",\n"
714         "    \"type\": \"ATTEST_SESSION_AUDIT\",\n"
715         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
716         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
717         "    \"clockInfo\": {\n"
718         "        \"clock\": 53021371269120,\n"
719         "        \"resetCount\": 23,\n"
720         "        \"restartCount\": 1,\n"
721         "        \"safe\": \"YES\"\n"
722         "    },\n"
723         "    \"firmwareVersion\": [783783,0],\n"
724         "    \"attested\": {\n"
725         "        \"exclusiveSession\": \"YES\",\n"
726         "        \"sessionDigest\": \"00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
727         "    }\n"
728         "}";
729     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_sessionaudit_src, test_json_TPMS_ATTEST_sessionaudit_expt);
730 
731     const char *test_json_TPMS_ATTEST_certifycreation_src =
732         "{\n"
733         "    \"magic\": \"0xff544347\",\n"
734         "    \"type\": \"ST_ATTEST_CREATION\",\n"
735         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
736         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
737         "    \"clockInfo\": {\n"
738         "        \"clock\": 123,\n"
739         "        \"resetCount\": 23,\n"
740         "        \"restartCount\": 1,\n"
741         "        \"safe\": \"yes\"\n"
742         "    },\n"
743         "    \"firmwareVersion\": [0,783],\n"
744         "    \"attested\": {\n"
745         "        \"objectName\": \"0x00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
746         "        \"creationHash\": \"0x00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
747         "    }\n"
748         "}";
749     const char *test_json_TPMS_ATTEST_certifycreation_expt =
750         "{\n"
751         "    \"magic\": \"VALUE\",\n"
752         "    \"type\": \"ATTEST_CREATION\",\n"
753         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
754         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
755         "    \"clockInfo\": {\n"
756         "        \"clock\": 123,\n"
757         "        \"resetCount\": 23,\n"
758         "        \"restartCount\": 1,\n"
759         "        \"safe\": \"YES\"\n"
760         "    },\n"
761         "    \"firmwareVersion\": 783,\n"
762         "    \"attested\": {\n"
763         "        \"objectName\": \"00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
764         "        \"creationHash\": \"00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
765         "    }\n"
766         "}";
767     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_certifycreation_src, test_json_TPMS_ATTEST_certifycreation_expt);
768 
769     const char *test_json_TPMS_ATTEST_commandaudit_src =
770         "{\n"
771         "    \"magic\": \"0xff544347\",\n"
772         "    \"type\": \"ST_ATTEST_COMMAND_AUDIT\",\n"
773         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
774         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
775         "    \"clockInfo\": {\n"
776         "        \"clock\": 123,\n"
777         "        \"resetCount\": 23,\n"
778         "        \"restartCount\": 1,\n"
779         "        \"safe\": \"yes\"\n"
780         "    },\n"
781         "    \"firmwareVersion\": 783,\n"
782         "    \"attested\": {\n"
783         "        \"auditCounter\": 456,\n"
784         "        \"digestAlg\": \"sha1\",\n"
785         "        \"auditDigest\": \"0x00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
786         "        \"commandDigest\": \"0x00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
787         "    }\n"
788         "}";
789     const char *test_json_TPMS_ATTEST_commandaudit_expt =
790         "{\n"
791         "    \"magic\": \"VALUE\",\n"
792         "    \"type\": \"ATTEST_COMMAND_AUDIT\",\n"
793         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
794         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
795         "    \"clockInfo\": {\n"
796         "        \"clock\": 123,\n"
797         "        \"resetCount\": 23,\n"
798         "        \"restartCount\": 1,\n"
799         "        \"safe\": \"YES\"\n"
800         "    },\n"
801         "    \"firmwareVersion\": 783,\n"
802         "    \"attested\": {\n"
803         "        \"auditCounter\": 456,\n"
804         "        \"digestAlg\": \"SHA1\",\n"
805         "        \"auditDigest\": \"00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
806         "        \"commandDigest\": \"00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
807         "    }\n"
808         "}";
809     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_commandaudit_src, test_json_TPMS_ATTEST_commandaudit_expt);
810 
811     const char *test_json_TPMS_ATTEST_time_src =
812         "{\n"
813         "    \"magic\": \"0xff544347\",\n"
814         "    \"type\": \"ST_ATTEST_TIME\",\n"
815         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
816         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
817         "    \"clockInfo\": {\n"
818         "        \"clock\": 123,\n"
819         "        \"resetCount\": 23,\n"
820         "        \"restartCount\": 1,\n"
821         "        \"safe\": \"yes\"\n"
822         "    },\n"
823         "    \"firmwareVersion\": 783,\n"
824         "    \"attested\": {\n"
825         "        \"time\": {\n"
826         "            \"time\": 234,\n"
827         "            \"clockInfo\": {\n"
828         "                \"clock\": 123,\n"
829         "                \"resetCount\": 23,\n"
830         "                \"restartCount\": 1,\n"
831         "                \"safe\": \"yes\"\n"
832         "            }\n"
833         "        },\n"
834         "        \"firmwareVersion\": 783\n"
835         "    }\n"
836         "}";
837     const char *test_json_TPMS_ATTEST_time_expt =
838         "{\n"
839         "    \"magic\": \"VALUE\",\n"
840         "    \"type\": \"ATTEST_TIME\",\n"
841         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
842         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
843         "    \"clockInfo\": {\n"
844         "        \"clock\": 123,\n"
845         "        \"resetCount\": 23,\n"
846         "        \"restartCount\": 1,\n"
847         "        \"safe\": \"YES\"\n"
848         "    },\n"
849         "    \"firmwareVersion\": 783,\n"
850         "    \"attested\": {\n"
851         "        \"time\": {\n"
852         "            \"time\": 234,\n"
853         "            \"clockInfo\": {\n"
854         "                \"clock\": 123,\n"
855         "                \"resetCount\": 23,\n"
856         "                \"restartCount\": 1,\n"
857         "                \"safe\": \"YES\"\n"
858         "            }\n"
859         "        },\n"
860         "        \"firmwareVersion\": 783\n"
861         "    }\n"
862         "}";
863     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_time_src, test_json_TPMS_ATTEST_time_expt);
864 
865     const char *test_json_TPMS_ATTEST_certifynv_src =
866         "{\n"
867         "    \"magic\": \"0xff544347\",\n"
868         "    \"type\": \"ST_ATTEST_NV\",\n"
869         "    \"qualifiedSigner\": \"0x00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
870         "    \"extraData\": \"0x00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
871         "    \"clockInfo\": {\n"
872         "        \"clock\": 123,\n"
873         "        \"resetCount\": 23,\n"
874         "        \"restartCount\": 1,\n"
875         "        \"safe\": \"yes\"\n"
876         "    },\n"
877         "    \"firmwareVersion\": 783,\n"
878         "    \"attested\": {\n"
879         "        \"indexName\": \"0x00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
880         "        \"offset\": 10,\n"
881         "        \"nvContents\": \"0x00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
882         "    }\n"
883         "}";
884     const char *test_json_TPMS_ATTEST_certifynv_expt =
885         "{\n"
886         "    \"magic\": \"VALUE\",\n"
887         "    \"type\": \"ATTEST_NV\",\n"
888         "    \"qualifiedSigner\": \"00010203040506070809a0a1a2a3a4a5a6a7a8a9\",\n"
889         "    \"extraData\": \"00010203040506070809b0b1b2b3b4b5b6b7b8b9\",\n"
890         "    \"clockInfo\": {\n"
891         "        \"clock\": 123,\n"
892         "        \"resetCount\": 23,\n"
893         "        \"restartCount\": 1,\n"
894         "        \"safe\": \"YES\"\n"
895         "    },\n"
896         "    \"firmwareVersion\": 783,\n"
897         "    \"attested\": {\n"
898         "        \"indexName\": \"00010203040506070809c0c1c2c3c4c5c6c7c8c9\",\n"
899         "        \"offset\": 10,\n"
900         "        \"nvContents\": \"00010203040506070809d0d1d2d3d4d5d6d7d8d9\"\n"
901         "    }\n"
902         "}";
903     CHECK_JSON(TPMS_ATTEST, test_json_TPMS_ATTEST_certifynv_src, test_json_TPMS_ATTEST_certifynv_expt);
904 
905     const char *test_json_TPMT_KEYEDHASH_SCHEME_hmac_src =
906         "{\n"
907         "    \"scheme\": \"HMAC\",\n"
908         "    \"details\": {\n"
909         "        \"hashAlg\": \"SHA256\"\n"
910         "    }\n"
911         "}";
912     const char *test_json_TPMT_KEYEDHASH_SCHEME_hmac_expt =
913         "{\n"
914         "    \"scheme\": \"HMAC\",\n"
915         "    \"details\": {\n"
916         "        \"hashAlg\": \"SHA256\"\n"
917         "    }\n"
918         "}";
919     CHECK_JSON(TPMT_KEYEDHASH_SCHEME, test_json_TPMT_KEYEDHASH_SCHEME_hmac_src, test_json_TPMT_KEYEDHASH_SCHEME_hmac_expt);
920 
921     const char *test_json_TPMT_KEYEDHASH_SCHEME_xor_src =
922         "{\n"
923         "    \"scheme\": \"XOR\",\n"
924         "    \"details\": {\n"
925         "        \"hashAlg\": \"SHA256\",\n"
926         "        \"kdf\": \"MGF1\"\n"
927         "    }\n"
928         "}";
929     const char *test_json_TPMT_KEYEDHASH_SCHEME_xor_expt =
930         "{\n"
931         "    \"scheme\": \"XOR\",\n"
932         "    \"details\": {\n"
933         "        \"hashAlg\": \"SHA256\",\n"
934         "        \"kdf\": \"MGF1\"\n"
935         "    }\n"
936         "}";
937     CHECK_JSON(TPMT_KEYEDHASH_SCHEME, test_json_TPMT_KEYEDHASH_SCHEME_xor_src, test_json_TPMT_KEYEDHASH_SCHEME_xor_expt);
938 
939 }
940 
941 static void
check_json_constants(void ** state)942 check_json_constants(void **state)
943 {
944     CHECK_JSON_SIMPLE(TPMI_ALG_HASH, "\"sha1\"", "\"SHA1\"");
945     CHECK_JSON_SIMPLE(TPMI_ALG_HASH, "\"0x04\"", "\"SHA1\"");
946     CHECK_JSON_SIMPLE(TPMI_ALG_HASH, "4", "\"SHA1\"");
947 }
948 
949 static void
check_json_numbers(void ** state)950 check_json_numbers(void **state)
951 {
952     CHECK_JSON_SIMPLE(UINT16, "10", "10");
953     CHECK_JSON_SIMPLE(UINT16, "\"0x0a\"", "10");
954     CHECK_JSON_SIMPLE(UINT64, "10000000000000000","[2328306,1874919424]");
955 }
956 
957 static void
check_json_bits(void ** state)958 check_json_bits(void **state)
959 {
960       const char *test_json_TPMA_NV_expected =\
961                     "{"
962                     "  \"PPWRITE\":0,"
963                     "  \"OWNERWRITE\":1,"
964                     "  \"AUTHWRITE\":1,"
965                     "  \"POLICYWRITE\":1,"
966                     "  \"POLICY_DELETE\":1,"
967                     "  \"WRITELOCKED\":0,"
968                     "  \"WRITEALL\":0,"
969                     "  \"WRITEDEFINE\":0,"
970                     "  \"WRITE_STCLEAR\":0,"
971                     "  \"GLOBALLOCK\":0,"
972                     "  \"PPREAD\":0,"
973                     "  \"OWNERREAD\":1,"
974                     "  \"AUTHREAD\":1,"
975                     "  \"POLICYREAD\":1,"
976                     "  \"NO_DA\":0,"
977                     "  \"ORDERLY\":1,"
978                     "  \"CLEAR_STCLEAR\":1,"
979                     "  \"READLOCKED\":1,"
980                     "  \"WRITTEN\":1,"
981                     "  \"PLATFORMCREATE\":0,"
982                     "  \"READ_STCLEAR\":0,"
983                     "  \"TPM2_NT\":\"COUNTER\""
984                     "}";
985 
986     const char *test_json_TPMA_NV_src_array =\
987                     "["
988                     "  \"nv_ownerwrite\","
989                     "  \"nv_authwrite\","
990                     "  \"nv_policywrite\","
991                     "  \"nv_policy_delete\","
992                     "  \"nv_ownerread\","
993                     "  \"nv_authread\","
994                     "  \"nv_policyread\","
995                     "  \"nv_orderly\","
996                     "  \"nv_clear_stclear\","
997                     "  \"nv_readlocked\","
998                     "  \"nv_written\","
999                     "  {"
1000                     "    \"TPM2_NT\": \"NT_COUNTER\""
1001                     "  }"
1002                     "]";
1003 
1004        const char *test_json_TPMA_NV_src_struct =\
1005                     "{"
1006                     "  \"TPMA_NV_OWNERWRITE\":\"YES\","
1007                     "  \"TPMA_NV_AUTHWRITE\":\"yes\","
1008                     "  \"TPMA_NV_POLICYWRITE\":\"TPM2_YES\","
1009                     "  \"TPMA_NV_POLICY_DELETE\":\"tpm2_yes\","
1010                     "  \"TPMA_NV_OWNERREAD\":\"SET\","
1011                     "  \"TPMA_NV_AUTHREAD\":\"set\","
1012                     "  \"TPMA_NV_POLICYREAD\":1,"
1013                     "  \"TPMA_NV_ORDERLY\":1,"
1014                     "  \"TPMA_NV_CLEAR_STCLEAR\":1,"
1015                     "  \"TPMA_NV_READLOCKED\":1,"
1016                     "  \"TPMA_NV_WRITTEN\":1,"
1017                     "  \"TPM2_NT\":1"
1018                     "  }";
1019 
1020     CHECK_JSON_SIMPLE(TPMA_NV, test_json_TPMA_NV_src_array, test_json_TPMA_NV_expected);
1021     CHECK_JSON_SIMPLE(TPMA_NV, test_json_TPMA_NV_src_struct, test_json_TPMA_NV_expected);
1022 }
1023 
1024 static void
check_json_policy(void ** state)1025 check_json_policy(void **state)
1026 {
1027      const char *test_json_policy_nv_src =       \
1028         "{"
1029         "  \"description\":\"Description pol_nv\","
1030         "  \"policyDigests\":["
1031         "  ],"
1032         "  \"policyAuthorizations\":["
1033         "  ],"
1034         "    \"policy\":["
1035         "        {"
1036         "            \"type\": \"POLICYNV\","
1037         "                   \"nvPath\": \"myNV\","
1038         "                   \"operandB\": \"01030304\""
1039         "      }"
1040         "  ]"
1041         "}";
1042 
1043        const char *test_json_policy_nv_expected =       \
1044         "{"
1045         "  \"description\":\"Description pol_nv\","
1046         "  \"policyDigests\":["
1047         "  ],"
1048         "  \"policyAuthorizations\":["
1049         "  ],"
1050         "    \"policy\":["
1051         "        {"
1052         "            \"type\": \"POLICYNV\","
1053         "                   \"nvPath\": \"myNV\","
1054         "                   \"operandB\": \"01030304\""
1055         "     }"
1056         "  ]"
1057         "}";
1058 
1059 
1060 //    CHECK_JSON(TPMS_POLICY, test_json_policy_nv_src, test_json_policy_nv_expected);
1061         {
1062             TPMS_POLICY out;
1063             TSS2_RC rc;
1064             json_object *jso = json_tokener_parse(test_json_policy_nv_src);
1065             if (!jso) fprintf(stderr, "JSON parsing failed\n");
1066             assert_non_null(jso);
1067             rc = ifapi_json_TPMS_POLICY_deserialize (jso, &out);
1068             if (rc) fprintf(stderr, "Deserialization failed\n");
1069             assert_int_equal (rc, TSS2_RC_SUCCESS);
1070             json_object_put(jso);
1071             jso = NULL;
1072             rc = ifapi_json_TPMS_POLICY_serialize (&out, &jso);
1073             assert_int_equal (rc, TSS2_RC_SUCCESS);
1074             assert_non_null(jso);
1075             const char *jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
1076             assert_non_null(jso_string);
1077             char *string1 = normalize(jso_string);
1078             char *string2 =  normalize(test_json_policy_nv_expected);
1079             assert_string_equal(string1, string2);
1080             json_object_put(jso);
1081             ifapi_cleanup_policy(&out);
1082             free(string1);
1083             free(string2);
1084         }
1085 
1086     const char *test_json_policy_or_src =       \
1087         "{"
1088         "  \"description\":\"hareness description\","
1089         "  \"policyDigests\":["
1090         "    {"
1091         "      \"hashAlg\":\"SHA256\","
1092         "      \"digest\":\"59215cb6c21a60e26b2cc479334a021113611903795507c1227659e2aef23d16\""
1093         "    }"
1094         "  ],"
1095         "  \"policy\":["
1096         "    {"
1097         "      \"type\":\"POLICYOR\","
1098         "      \"policyDigests\":["
1099         "        {"
1100         "          \"hashAlg\":\"SHA256\","
1101         "          \"digest\":\"59215cb6c21a60e26b2cc479334a021113611903795507c1227659e2aef23d16\""
1102         "        }"
1103         "      ],"
1104         "        \"branches\":["
1105         "          {"
1106         "            \"name\":\"branch1\","
1107         "            \"description\":\"description branch 1\","
1108         "            \"policy\":["
1109         "              {"
1110         "                \"type\":\"POLICYPCR\","
1111         "                \"policyDigests\":["
1112         "                  {"
1113         "                    \"hashAlg\":\"SHA256\","
1114         "                    \"digest\":\"17d552f8e39ad882f6b3c09ae139af59616bf6a63f4093d6d20e9e1b9f7cdb6e\""
1115         "                  }"
1116         "                ],"
1117         "                  \"pcrs\":["
1118         "                    {"
1119         "                      \"pcr\":16,"
1120         "                      \"hashAlg\":\"SHA1\","
1121         "                      \"digest\":\"0000000000000000000000000000000000000000\""
1122         "                    }"
1123         "                  ]"
1124         "              }"
1125         "            ],"
1126         "            \"policyDigests\":["
1127         "              {"
1128         "                \"hashAlg\":\"SHA256\","
1129         "                \"digest\":\"17d552f8e39ad882f6b3c09ae139af59616bf6a63f4093d6d20e9e1b9f7cdb6e\""
1130         "              }"
1131         "            ]"
1132         "          },"
1133         "          {"
1134         "            \"name\":\"branch1\","
1135         "            \"description\":\"description branch 1\","
1136         "            \"policy\":["
1137         "              {"
1138         "                \"type\":\"POLICYPCR\","
1139         "                \"policyDigests\":["
1140         "                  {"
1141         "                    \"hashAlg\":\"SHA256\","
1142         "                    \"digest\":\"17d552f8e39ad882f6b3c09ae139af59616bf6a63f4093d6d20e9e1b9f7cdb6e\""
1143         "                  }"
1144         "                ],"
1145         "                  \"pcrs\":["
1146         "                    {"
1147         "                      \"pcr\":16,"
1148         "                      \"hashAlg\":\"SHA1\","
1149         "                      \"digest\":\"0000000000000000000000000000000000000000\""
1150         "                    }"
1151         "                  ]"
1152         "              }"
1153         "            ],"
1154         "            \"policyDigests\":["
1155         "              {"
1156         "                \"hashAlg\":\"SHA256\","
1157         "                \"digest\":\"17d552f8e39ad882f6b3c09ae139af59616bf6a63f4093d6d20e9e1b9f7cdb6e\""
1158         "              }"
1159         "            ]"
1160         "          }"
1161         "        ]"
1162         "    }"
1163         "  ]"
1164         "}";
1165 
1166     char *test_json_policy_or_expected = strdup(test_json_policy_or_src);
1167     if (test_json_policy_or_expected == NULL){
1168         LOG_ERROR("%s", "Out of memory.");
1169         return;
1170     }
1171 //    CHECK_JSON(TPMS_POLICY, test_json_policy_or_src, test_json_policy_or_expected);
1172         {
1173             TPMS_POLICY out;
1174             TSS2_RC rc;
1175             json_object *jso = json_tokener_parse(test_json_policy_or_src);
1176             if (!jso) fprintf(stderr, "JSON parsing failed\n");
1177             assert_non_null(jso);
1178             rc = ifapi_json_TPMS_POLICY_deserialize (jso, &out);
1179             if (rc) fprintf(stderr, "Deserialization failed\n");
1180             assert_int_equal (rc, TSS2_RC_SUCCESS);
1181             json_object_put(jso);
1182             jso = NULL;
1183             rc = ifapi_json_TPMS_POLICY_serialize (&out, &jso);
1184             assert_int_equal (rc, TSS2_RC_SUCCESS);
1185             assert_non_null(jso);
1186             const char *jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
1187             assert_non_null(jso_string);
1188             char *string1 = normalize(jso_string);
1189             char *string2 =  normalize(test_json_policy_or_expected);
1190             assert_string_equal(string1, string2);
1191             json_object_put(jso);
1192             ifapi_cleanup_policy(&out);
1193             free(string1);
1194             free(string2);
1195         }
1196     free(test_json_policy_or_expected);
1197 }
1198 
1199 
1200 static void
check_json_tpm2bs(void ** state)1201 check_json_tpm2bs(void **state)
1202 {
1203     CHECK_JSON(TPM2B_DIGEST, "\"0x0102\"", "\"0102\"");
1204     CHECK_JSON(TPM2B_DIGEST, "\"0102\"", "\"0102\"");
1205     CHECK_JSON(TPM2B_DIGEST, "\"caffee\"", "\"caffee\"");
1206 }
1207 
1208 static void
check_error(void ** state)1209 check_error(void **state)
1210 {
1211    /* Value is > then max value for UINT */
1212     CHECK_ERROR(UINT16, "\"0x10000\"", TSS2_FAPI_RC_BAD_VALUE);
1213     CHECK_ERROR(UINT32, "\"0x100000000\"", TSS2_FAPI_RC_BAD_VALUE);
1214 
1215     /* Digest/list is too large*/
1216     CHECK_ERROR(TPM2B_DIGEST, "\"0x0102222222222222222222222222222222222222222222222222222"
1217                 "22222222222222222222222222222222222222222222222222222222222222222222222222222\"",
1218                 TSS2_FAPI_RC_BAD_VALUE);
1219 
1220     /* Illegal values */
1221     CHECK_ERROR(TPMI_ALG_HASH, "\"SHA9999\"", TSS2_FAPI_RC_BAD_VALUE);
1222     CHECK_ERROR(TPM2B_DIGEST, "\"xxxx\"", TSS2_FAPI_RC_BAD_VALUE);
1223     CHECK_ERROR(TPM2B_DIGEST, "\"0x010x\"", TSS2_FAPI_RC_BAD_VALUE);
1224 }
1225 
1226 
1227 static void
check_tpmjson_tofromtxt(void ** state)1228 check_tpmjson_tofromtxt(void **state)
1229 {
1230     const char *testcase_alg_id[] = { "\"TPM_ALG_ID_SHA1\"", "\"TPM2_ALG_ID_SHA1\"",
1231                                       "\"ALG_ID_SHA1\"", "\"SHA1\"", "\"ALG_SHA1\"",
1232                                       "\"tpm2_alg_id_sha1\"", "\"sha1\"", "\"0x0004\"" };
1233     const char *expected_ald_id = { "\"SHA1\"" };
1234     for (size_t i = 0; i < sizeof(testcase_alg_id) / sizeof(testcase_alg_id[0]); i++) {
1235         CHECK_JSON_SIMPLE(TPM2_ALG_ID, testcase_alg_id[i], expected_ald_id);
1236     }
1237 
1238     const char *testcase_ecc_curve[] = { "\"TPM2_ECC_NIST_P256\"", "\"ECC_NIST_P256\"",
1239                                          "\"NIST_P256\"", "\"0x0003\"", "\"nist_p256\"" };
1240     const char *expected_ecc_curve = { "\"NIST_P256\"" };
1241     for (size_t i = 0; i < sizeof(testcase_ecc_curve) / sizeof(testcase_ecc_curve[0]); i++) {
1242         CHECK_JSON_SIMPLE(TPM2_ECC_CURVE, testcase_ecc_curve[i], expected_ecc_curve);
1243     }
1244 
1245     const char *testcase_cc[] = { "\"TPM2_CC_Startup\"", "\"CC_Startup\"",
1246                                   "\"Startup\"", "\"0x00000144\"" };
1247     const char *expected_cc = { "\"Startup\"" };
1248     for (size_t i = 0; i < sizeof(testcase_cc) / sizeof(testcase_cc[0]); i++) {
1249         CHECK_JSON_SIMPLE(TPM2_CC, testcase_cc[i], expected_cc);
1250     }
1251 
1252     const char *testcase_eo[] = { "\"TPM2_EO_EQ\"", "\"EO_EQ\"",
1253                                   "\"EQ\"", "\"0x0000\"" };
1254     const char *expected_eo = { "\"EQ\"" };
1255     for (size_t i = 0; i < sizeof(testcase_eo) / sizeof(testcase_eo[0]); i++) {
1256         CHECK_JSON_SIMPLE(TPM2_EO, testcase_eo[i], expected_eo);
1257     }
1258 
1259     const char *testcase_st[] = { "\"TPM2_ST_NO_SESSIONS\"", "\"ST_NO_SESSIONS\"",
1260                                   "\"no_SESSIONS\"", "\"0x8001\"" };
1261     const char *expected_st = { "\"NO_SESSIONS\"" };
1262     for (size_t i = 0; i < sizeof(testcase_st) / sizeof(testcase_st[0]); i++) {
1263         CHECK_JSON_SIMPLE(TPM2_ST, testcase_st[i], expected_st);
1264     }
1265 
1266     const char *testcase_pt_pcr[] = { "\"TPM2_PT_PCR_EXTEND_L0\"", "\"PT_PCR_EXTEND_L0\"",
1267                                   "\"PCR_EXTEND_L0\"", "\"EXTEND_L0\"" };
1268     const char *expected_pt_pcr = { "\"EXTEND_L0\"" };
1269     for (size_t i = 0; i < sizeof(testcase_pt_pcr) / sizeof(testcase_pt_pcr[0]); i++) {
1270         CHECK_JSON_SIMPLE(TPM2_PT_PCR, testcase_pt_pcr[i], expected_pt_pcr);
1271     }
1272 
1273     const char *testcase_alg_public[] = { "\"TPM2_ALG_RSA\"", "\"ALG_RSA\"",
1274                                           "\"RSA\"", "\"0x0001\"" };
1275     const char *expected_alg_public = { "\"RSA\"" };
1276     for (size_t i = 0; i < sizeof(testcase_alg_public) / sizeof(testcase_alg_public[0]); i++) {
1277         CHECK_JSON_SIMPLE(TPMI_ALG_PUBLIC, testcase_alg_public[i], expected_alg_public);
1278     }
1279 }
1280 
1281 int
main(int argc,char * argv[])1282 main(int argc, char *argv[])
1283 {
1284     const struct CMUnitTest tests[] = {
1285         cmocka_unit_test(check_tpmjson_tofromtxt),
1286         cmocka_unit_test(check_json_structs),
1287         cmocka_unit_test(check_json_constants),
1288         cmocka_unit_test(check_json_numbers),
1289         cmocka_unit_test(check_json_bits),
1290         cmocka_unit_test(check_json_tpm2bs),
1291         cmocka_unit_test(check_json_to_bin),
1292         cmocka_unit_test(check_bin),
1293         cmocka_unit_test(check_policy_bin),
1294         cmocka_unit_test(check_error),
1295         cmocka_unit_test(check_json_policy),
1296     };
1297     return cmocka_run_group_tests(tests, NULL, NULL);
1298 }
1299