1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* SPDX-License-Identifier: BSD-2-Clause */
/*******************************************************************************
* Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
* All rights reserved.
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "tss2_fapi.h"
#include "test-fapi.h"
#define LOGMODULE test
#include "util/log.h"
#include "util/aux_util.h"
/** Test the FAPI functions for key creation and usage.
*
* Tested FAPI commands:
* - Fapi_Provision()
* - Fapi_CreateKey()
* - Fapi_Sign()
* - Fapi_Delete()
* - Fapi_List()
*
* @param[in,out] context The FAPI_CONTEXT.
* @retval EXIT_FAILURE
* @retval EXIT_SUCCESS
*/
#define SIZE 2000
int
test_fapi_duplicate(FAPI_CONTEXT *context)
{
TSS2_RC r;
char *policy_name = "/policy/pol_duplicate";
char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_duplicate.json";
FILE *stream = NULL;
char *json_policy = NULL;
long policy_size;
char *json_duplicate = NULL;
char *json_string_pub_key = NULL;
r = Fapi_Provision(context, NULL, NULL, NULL);
goto_if_error(r, "Error Fapi_Provision", error);
r = pcr_reset(context, 16);
goto_if_error(r, "Error pcr_reset", error);
stream = fopen(policy_file, "r");
if (!stream) {
LOG_ERROR("File %s does not exist", policy_file);
goto error;
}
fseek(stream, 0L, SEEK_END);
policy_size = ftell(stream);
fclose(stream);
json_policy = malloc(policy_size + 1);
goto_if_null(json_policy,
"Could not allocate memory for the JSON policy",
TSS2_FAPI_RC_MEMORY, error);
stream = fopen(policy_file, "r");
ssize_t ret = read(fileno(stream), json_policy, policy_size);
if (ret != policy_size) {
LOG_ERROR("IO error %s.", policy_file);
goto error;
}
json_policy[policy_size] = '\0';
r = Fapi_Import(context, policy_name, json_policy);
goto_if_error(r, "Error Fapi_List", error);
r = Fapi_CreateKey(context, "HS/SRK/myCryptKey", "restricted,decrypt,noDa",
"", NULL);
goto_if_error(r, "Error Fapi_CreateKey", error);
r = Fapi_ExportKey(context, "HS/SRK/myCryptKey", NULL, &json_string_pub_key);
goto_if_error(r, "Error Fapi_CreateKey", error);
r = Fapi_Import(context, "ext/myNewParent", json_string_pub_key);
goto_if_error(r, "Error Fapi_Import", error);
r = Fapi_CreateKey(context, "HS/SRK/myCryptKey/myCryptKey2",
"exportable,decrypt,noDa", policy_name, NULL);
goto_if_error(r, "Error Fapi_CreateKey", error);
r = Fapi_ExportKey(context, "HS/SRK/myCryptKey/myCryptKey2",
"ext/myNewParent", &json_duplicate);
goto_if_error(r, "Error Fapi_CreateKey", error);
fprintf(stderr, "\nExport Data:\n%s\n", json_duplicate);
r = Fapi_Import(context, "importedKey", json_duplicate);
goto_if_error(r, "Error Fapi_Import", error);
fprintf(stderr, "Duplicate:\n%s\n", json_duplicate);
r = Fapi_Delete(context, "/");
goto_if_error(r, "Error Fapi_Delete", error);
SAFE_FREE(json_string_pub_key);
SAFE_FREE(json_duplicate);
SAFE_FREE(json_policy);
return EXIT_SUCCESS;
error:
SAFE_FREE(json_string_pub_key);
SAFE_FREE(json_duplicate);
SAFE_FREE(json_policy);
return EXIT_FAILURE;
}
int
test_invoke_fapi(FAPI_CONTEXT *fapi_context)
{
return test_fapi_duplicate(fapi_context);
}
|