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 
17 #include "tss2_fapi.h"
18 
19 #include "test-fapi.h"
20 #define LOGMODULE test
21 #include "util/log.h"
22 #include "util/aux_util.h"
23 
24 #define EVENT_SIZE 10
25 
26 /** Test the FAPI functions for quote commands.
27  *
28  * Tested FAPI commands:
29  *  - Fapi_Provision()
30  *  - Fapi_CreateKey()
31  *  - Fapi_PcrExtend()
32  *  - Fapi_Quote()
33  *  - Fapi_VerifyQuote()
34  *  - Fapi_List()
35  *  - Fapi_Delete()
36  *
37  * @param[in,out] context The FAPI_CONTEXT.
38  * @retval EXIT_FAILURE
39  * @retval EXIT_SUCCESS
40  */
41 int
test_fapi_quote_destructive(FAPI_CONTEXT * context)42 test_fapi_quote_destructive(FAPI_CONTEXT *context)
43 {
44     TSS2_RC r;
45     char *pubkey_pem = NULL;
46     uint8_t *signature = NULL;
47     char *quoteInfo = NULL;
48     char *pcrEventLog = NULL;
49     char *certificate = NULL;
50     char *export_data = NULL;
51     uint8_t *pcr_digest = NULL;
52     char *log = NULL;
53     char *pathlist = NULL;
54 
55     uint8_t data[EVENT_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
56     size_t signatureSize = 0;
57     uint32_t pcrList[2] = { 11, 16 };
58 
59     r = Fapi_Provision(context, NULL, NULL, NULL);
60 
61     goto_if_error(r, "Error Fapi_Provision", error);
62 
63     r = Fapi_CreateKey(context, "HS/SRK/mySignKey", "sign,noDa", "", NULL);
64     goto_if_error(r, "Error Fapi_CreateKey", error);
65 
66    r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "  \
67         "CERTIFICATE-----[...]-----END CERTIFICATE-----");
68     goto_if_error(r, "Error Fapi_SetCertificate", error);
69 
70     uint8_t qualifyingData[20] = {
71         0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
72         0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
73     };
74 
75     r = pcr_reset(context, 16);
76     goto_if_error(r, "Error pcr_reset", error);
77 
78     r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
79     goto_if_error(r, "Error Fapi_PcrExtend", error);
80 
81     r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
82     goto_if_error(r, "Error Fapi_PcrExtend", error);
83 
84     r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
85     goto_if_error(r, "Error Fapi_PcrExtend", error);
86 
87     r = Fapi_Quote(context, pcrList, 2, "HS/SRK/mySignKey",
88                    "TPM-Quote",
89                    qualifyingData, 20,
90                    &quoteInfo,
91                    &signature, &signatureSize,
92                    &pcrEventLog, &certificate);
93     goto_if_error(r, "Error Fapi_Quote", error);
94     ASSERT(quoteInfo != NULL);
95     ASSERT(signature != NULL);
96     ASSERT(pcrEventLog != NULL);
97     ASSERT(certificate != NULL);
98     ASSERT(strlen(quoteInfo) > ASSERT_SIZE);
99     ASSERT(strlen(pcrEventLog) > ASSERT_SIZE);
100     ASSERT(strlen(certificate) > ASSERT_SIZE);
101 
102     LOG_INFO("\npcrEventLog: %s\n", pcrEventLog);
103 
104     r = Fapi_VerifyQuote(context, "HS/SRK/mySignKey",
105                          qualifyingData, 20,  quoteInfo,
106                          signature, signatureSize, pcrEventLog);
107     goto_if_error(r, "Error Fapi_Verfiy_Quote", error);
108 
109     r = Fapi_List(context, "/", &pathlist);
110     goto_if_error(r, "Pathlist", error);
111     ASSERT(pathlist != NULL);
112     ASSERT(strlen(pathlist) > ASSERT_SIZE);
113 
114     r = Fapi_Delete(context, "/");
115     goto_if_error(r, "Error Fapi_Delete", error);
116 
117     SAFE_FREE(pubkey_pem);
118     SAFE_FREE(signature);
119     SAFE_FREE(quoteInfo);
120     SAFE_FREE(pcrEventLog);
121     SAFE_FREE(certificate);
122     SAFE_FREE(export_data);
123     SAFE_FREE(pcr_digest);
124     SAFE_FREE(log);
125     SAFE_FREE(pathlist);
126     return EXIT_SUCCESS;
127 
128 error:
129     Fapi_Delete(context, "/");
130     SAFE_FREE(pubkey_pem);
131     SAFE_FREE(signature);
132     SAFE_FREE(quoteInfo);
133     SAFE_FREE(pcrEventLog);
134     SAFE_FREE(certificate);
135     SAFE_FREE(export_data);
136     SAFE_FREE(pcr_digest);
137     SAFE_FREE(log);
138     SAFE_FREE(pathlist);
139     return EXIT_FAILURE;
140 }
141 
142 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)143 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
144 {
145     return test_fapi_quote_destructive(fapi_context);
146 }
147