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