blob: 68db836388a8f706fc31f6287976cee8365ae2cc [file] [log] [blame]
Juergen Reppecd44eb2018-02-23 13:41:16 +01001/*******************************************************************************
2 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG All
3 * rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27
28#define _GNU_SOURCE
29#include <getopt.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <signal.h>
34#include <stdint.h>
35#include <sapi/tpm20.h>
36#define LOGMODULE test
37#include "log/log.h"
38#include "test.h"
39#include "sysapi_util.h"
40#include <esapi/tss2_esys.h>
41#include "esys_types.h"
42#include "esys_iutil.h"
43
44/*
45 * This test is intended to test the quote command with password
46 * authentication.
47 * We create a RSA primary signing key which will be used
48 * for signing.
49 */
50
51int
52test_invoke_esapi(ESYS_CONTEXT * esys_context)
53{
54 uint32_t r = 0;
55
56 TPM2B_AUTH authValuePrimary = {
57 .size = 5,
58 .buffer = {1, 2, 3, 4, 5}
59 };
60
61 TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
62 .size = 4,
63 .sensitive = {
64 .userAuth = {
65 .size = 0,
66 .buffer = {0},
67 },
68 .data = {
69 .size = 0,
70 .buffer = {0},
71 },
72 },
73 };
74
75 inSensitivePrimary.sensitive.userAuth = authValuePrimary;
76
77 TPM2B_PUBLIC inPublic = {
78 .size = 0,
79 .publicArea = {
80 .type = TPM2_ALG_RSA,
81 .nameAlg = TPM2_ALG_SHA1,
82 .objectAttributes = (
83 TPMA_OBJECT_USERWITHAUTH |
84 TPMA_OBJECT_RESTRICTED |
85 TPMA_OBJECT_SIGN_ENCRYPT |
86 TPMA_OBJECT_FIXEDTPM |
87 TPMA_OBJECT_FIXEDPARENT |
88 TPMA_OBJECT_SENSITIVEDATAORIGIN
89 ),
90 .authPolicy = {
91 .size = 0,
92 },
93 .parameters.rsaDetail = {
94 .symmetric = {
95 .algorithm = TPM2_ALG_NULL,
96 .keyBits.aes = 128,
97 .mode.aes = TPM2_ALG_ECB,
98 },
99 .scheme = {
100 .scheme = TPM2_ALG_RSASSA,
101 .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA1 }},
102
103 },
104 .keyBits = 2048,
105 .exponent = 0,
106 },
107 .unique.rsa = {
108 .size = 0,
109 .buffer = {},
110 },
111 },
112 };
113
114 TPM2B_AUTH authValue = {
115 .size = 0,
116 .buffer = {}
117 };
118
119
120 TPM2B_DATA outsideInfo = {
121 .size = 0,
122 .buffer = {},
123 };
124
125
126 TPML_PCR_SELECTION creationPCR = {
127 .count = 0,
128 };
129
130 LOG_INFO("\nRSA key will be created.");
131
132 r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
133 goto_if_error(r, "Error: TR_SetAuth", error);
134
135 ESYS_TR primaryHandle_handle;
136 RSRC_NODE_T *primaryHandle_node;
137 TPM2B_PUBLIC *outPublic;
138 TPM2B_CREATION_DATA *creationData;
139 TPM2B_DIGEST *creationHash;
140 TPMT_TK_CREATION *creationTicket;
141
142 r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
143 ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
144 &inPublic, &outsideInfo, &creationPCR,
145 &primaryHandle_handle, &outPublic, &creationData,
146 &creationHash, &creationTicket);
147 goto_if_error(r, "Error esys create primary", error);
148
149 r = esys_GetResourceObject(esys_context, primaryHandle_handle,
150 &primaryHandle_node);
151 goto_if_error(r, "Error Esys GetResourceObject", error);
152
153 LOG_INFO("Created Primary with handle 0x%08x...",
154 primaryHandle_node->rsrc.handle);
155
156 r = Esys_TR_SetAuth(esys_context, primaryHandle_handle,
157 &authValuePrimary);
158 goto_if_error(r, "Error: TR_SetAuth", error);
159
160 TPM2B_DATA qualifyingData = { .size = 0, .buffer = {}};
161 TPMT_SIG_SCHEME sig_scheme = { .scheme = TPM2_ALG_NULL };
162
163 TPML_PCR_SELECTION pcr_selection = {
164 .count = 2,
165 .pcrSelections = {
166 {
167 .hash = TPM2_ALG_SHA1,
168 .sizeofSelect = 3,
169 .pcrSelect = { 0,4,0 } },
170 {
171 .hash = TPM2_ALG_SHA256,
172 .sizeofSelect = 3,
173 .pcrSelect = { 0,4,0 } },
174 }};
175
176 TPM2B_ATTEST *attest;
177 TPMT_SIGNATURE *signature;
178
179 r = Esys_Quote(esys_context, primaryHandle_handle,
180 ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
181 &qualifyingData, &sig_scheme, &pcr_selection,
182 &attest, &signature);
183 goto_if_error(r, "Error esys quiote", error);
184
185 return 0;
186
187 error:
188 return 1;
189}