1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3 * Copyright 2017-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 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <json-c/json.h>
17 #include <json-c/json_util.h>
18 #include <json-c/json_tokener.h>
19
20 #include "tss2_fapi.h"
21
22 #include "test-fapi.h"
23 #define LOGMODULE test
24 #include "util/log.h"
25 #include "util/aux_util.h"
26
27 #define EVENT_SIZE 10
28
29 /** Test the FAPI functions for quote commands.
30 *
31 * Tested FAPI commands:
32 * - Fapi_Provision()
33 * - Fapi_CreateKey()
34 * - Fapi_PcrExtend()
35 * - Fapi_Quote()
36 * - Fapi_ExportKey()
37 * - Fapi_Import()
38 * - Fapi_PcrRead()
39 * - Fapi_VerifyQuote()
40 * - Fapi_List()
41 * - Fapi_Delete()
42 *
43 * @param[in,out] context The FAPI_CONTEXT.
44 * @retval EXIT_FAILURE
45 * @retval EXIT_SUCCESS
46 */
47 int
test_fapi_quote(FAPI_CONTEXT * context)48 test_fapi_quote(FAPI_CONTEXT *context)
49 {
50 TSS2_RC r;
51 json_object *jso = NULL;
52 char *pubkey_pem = NULL;
53 uint8_t *signature = NULL;
54 char *quoteInfo = NULL;
55 char *pcrEventLog = NULL;
56 char *certificate = NULL;
57 char *export_data = NULL;
58 json_object *jso_public = NULL;
59 uint8_t *pcr_digest = NULL;
60 char *log = NULL;
61 char *pathlist = NULL;
62
63 uint8_t data[EVENT_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
64 size_t signatureSize = 0;
65 uint32_t pcrList[1] = { 16 };
66 size_t pcr_digest_size = 0;
67
68 r = Fapi_Provision(context, NULL, NULL, NULL);
69
70 goto_if_error(r, "Error Fapi_Provision", error);
71
72 r = Fapi_CreateKey(context, "HS/SRK/mySignKey", "sign,noDa", "", NULL);
73 goto_if_error(r, "Error Fapi_CreateKey", error);
74
75 uint8_t qualifyingData[20] = {
76 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
77 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
78 };
79
80 r = pcr_reset(context, 16);
81 goto_if_error(r, "Error pcr_reset", error);
82
83 r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
84 goto_if_error(r, "Error Fapi_PcrExtend", error);
85
86 r = Fapi_Quote(context, pcrList, 1, "HS/SRK/mySignKey",
87 "TPM-Quote",
88 qualifyingData, 20,
89 "eInfo,
90 &signature, &signatureSize,
91 &pcrEventLog, &certificate);
92 goto_if_error(r, "Error Fapi_Quote", error);
93
94 r = Fapi_ExportKey(context, "HS/SRK/mySignKey", NULL, &export_data);
95 goto_if_error(r, "Export.", error);
96
97 jso = json_tokener_parse(export_data);
98
99 LOG_INFO("\nExported: %s\n", export_data);
100
101 if (!jso || !json_object_object_get_ex(jso, "pem_ext_public", &jso_public)) {
102 LOG_ERROR("No public key eyported.");
103 goto error;
104 }
105 pubkey_pem = strdup(json_object_get_string(jso_public));
106 if (!pubkey_pem) {
107 LOG_ERROR("Out of memory.");
108 goto error;
109 }
110
111 r = Fapi_Import(context, "/ext/myExtPubKey", pubkey_pem);
112 goto_if_error(r, "Error Fapi_Import", error);
113
114 r = Fapi_PcrRead(context, 16, &pcr_digest,
115 &pcr_digest_size, &log);
116 goto_if_error(r, "Error Fapi_PcrRead", error);
117
118 LOG_INFO("\nLog:\n%s\n", log);
119 LOG_INFO("Quote Info:\n%s\n", quoteInfo);
120
121 r = Fapi_VerifyQuote(context, "HS/SRK/mySignKey",
122 qualifyingData, 20, quoteInfo,
123 signature, signatureSize, log);
124 goto_if_error(r, "Error Fapi_Verfiy_Quote", error);
125
126 r = Fapi_Delete(context, "/HS/SRK");
127 goto_if_error(r, "Error Fapi_Delete", error);
128
129 r = Fapi_List(context, "/", &pathlist);
130 goto_if_error(r, "Pathlist", error);
131
132 json_object_put(jso);
133 SAFE_FREE(pubkey_pem);
134 SAFE_FREE(signature);
135 SAFE_FREE(quoteInfo);
136 SAFE_FREE(pcrEventLog);
137 SAFE_FREE(certificate);
138 SAFE_FREE(export_data);
139 SAFE_FREE(pcr_digest);
140 SAFE_FREE(log);
141 SAFE_FREE(pathlist);
142 return EXIT_SUCCESS;
143
144 error:
145 if (jso)
146 json_object_put(jso);
147 SAFE_FREE(pubkey_pem);
148 SAFE_FREE(signature);
149 SAFE_FREE(quoteInfo);
150 SAFE_FREE(pcrEventLog);
151 SAFE_FREE(certificate);
152 SAFE_FREE(export_data);
153 SAFE_FREE(pcr_digest);
154 SAFE_FREE(log);
155 SAFE_FREE(pathlist);
156 return EXIT_FAILURE;
157 }
158
159 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)160 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
161 {
162 return test_fapi_quote(fapi_context);
163 }
164