forked from openenclave/openenclave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattest_plugin.c
236 lines (201 loc) · 6.57 KB
/
attest_plugin.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include <openenclave/bits/defs.h>
#include <openenclave/internal/hexdump.h>
#include <openenclave/internal/print.h>
#include <openenclave/internal/raise.h>
#include <openenclave/internal/report.h>
#include <openenclave/internal/safemath.h>
#include <openenclave/internal/utils.h>
#include <stdio.h>
#include <string.h>
#include "../common/attest_plugin.h"
#include "../common/common.h"
#include <openenclave/attestation/attester.h>
#include <openenclave/attestation/verifier.h>
#include <openenclave/internal/plugin.h>
// Variables storing the attester list.
static oe_plugin_list_node_t* attesters = NULL;
oe_result_t oe_register_attester_plugin(
oe_attester_t* plugin,
const void* configuration_data,
size_t configuration_data_size)
{
return oe_attest_register_plugin(
&attesters,
(oe_attestation_role_t*)plugin,
configuration_data,
configuration_data_size);
}
oe_result_t oe_unregister_attester_plugin(oe_attester_t* plugin)
{
return oe_attest_unregister_plugin(
&attesters, (oe_attestation_role_t*)plugin);
}
static oe_result_t _wrap_with_header(
const oe_uuid_t* format_id,
const uint8_t* data,
size_t data_size,
uint8_t** total_data,
size_t* total_data_size)
{
oe_result_t result = OE_UNEXPECTED;
oe_attestation_header_t* header;
OE_CHECK(oe_safe_add_sizet(sizeof(*header), data_size, total_data_size));
*total_data = (uint8_t*)oe_malloc(*total_data_size);
if (*total_data == NULL)
OE_RAISE(OE_OUT_OF_MEMORY);
header = (oe_attestation_header_t*)*total_data;
header->version = OE_ATTESTATION_HEADER_VERSION;
header->format_id = *format_id;
header->data_size = data_size;
memcpy(header->data, data, data_size);
result = OE_OK;
done:
return result;
}
oe_result_t oe_get_evidence(
const oe_uuid_t* format_id,
uint32_t flags,
const void* custom_claims_buffer,
size_t custom_claims_buffer_size,
const void* optional_parameters,
size_t optional_parameters_size,
uint8_t** evidence_buffer,
size_t* evidence_buffer_size,
uint8_t** endorsements_buffer,
size_t* endorsements_buffer_size)
{
oe_result_t result = OE_UNEXPECTED;
oe_plugin_list_node_t* plugin_node = NULL;
oe_attester_t* plugin = NULL;
uint8_t* plugin_evidence = NULL;
size_t plugin_evidence_size = 0;
uint8_t* plugin_endorsements = NULL;
size_t plugin_endorsements_size = 0;
uint8_t* total_evidence_buf = NULL;
size_t total_evidence_size = 0;
uint8_t* total_endorsements_buf = NULL;
size_t total_endorsements_size = 0;
bool wrap_with_header = (flags & OE_EVIDENCE_FLAGS_EMBED_FORMAT_ID);
if (!format_id || !evidence_buffer || !evidence_buffer_size ||
(endorsements_buffer && !endorsements_buffer_size) ||
(!endorsements_buffer && endorsements_buffer_size))
OE_RAISE(OE_INVALID_PARAMETER);
// Find a plugin for attestation type and run its get_evidence.
plugin_node = oe_attest_find_plugin(attesters, format_id, NULL);
if (plugin_node == NULL)
OE_RAISE(OE_NOT_FOUND);
// Now get the evidence and endorsements (if desired).
plugin = (oe_attester_t*)plugin_node->plugin;
OE_CHECK(plugin->get_evidence(
plugin,
custom_claims_buffer,
custom_claims_buffer_size,
optional_parameters,
optional_parameters_size,
&plugin_evidence,
&plugin_evidence_size,
endorsements_buffer ? &plugin_endorsements : NULL,
endorsements_buffer ? &plugin_endorsements_size : NULL));
// When requested, wrap the attestation header around the evidence.
if (wrap_with_header)
{
OE_CHECK(_wrap_with_header(
format_id,
plugin_evidence,
plugin_evidence_size,
&total_evidence_buf,
&total_evidence_size));
if (endorsements_buffer)
{
OE_CHECK(_wrap_with_header(
format_id,
plugin_endorsements,
plugin_endorsements_size,
&total_endorsements_buf,
&total_endorsements_size));
}
}
else // !wrap_with_header
{
total_evidence_buf = plugin_evidence;
total_evidence_size = plugin_evidence_size;
plugin_evidence = NULL;
if (endorsements_buffer)
{
total_endorsements_buf = plugin_endorsements;
total_endorsements_size = plugin_endorsements_size;
plugin_endorsements = NULL;
}
}
// Finally, set the out parameters.
*evidence_buffer = total_evidence_buf;
*evidence_buffer_size = total_evidence_size;
total_evidence_buf = NULL;
if (endorsements_buffer)
{
*endorsements_buffer = total_endorsements_buf;
*endorsements_buffer_size = total_endorsements_size;
total_endorsements_buf = NULL;
}
result = OE_OK;
done:
if (plugin && plugin_evidence)
{
plugin->free_evidence(plugin, plugin_evidence);
if (plugin_endorsements)
plugin->free_endorsements(plugin, plugin_endorsements);
}
if (total_evidence_buf != NULL)
oe_free(total_evidence_buf);
if (total_endorsements_buf != NULL)
oe_free(total_endorsements_buf);
return result;
}
oe_result_t oe_free_evidence(uint8_t* evidence_buffer)
{
oe_free(evidence_buffer);
return OE_OK;
}
oe_result_t oe_free_endorsements(uint8_t* evidence_buffer)
{
oe_free(evidence_buffer);
return OE_OK;
}
oe_result_t oe_attester_select_format(
const oe_uuid_t* format_ids,
size_t format_ids_length,
oe_uuid_t* selected_format_id)
{
oe_result_t result = OE_NOT_FOUND;
if (!format_ids || !format_ids_length || !selected_format_id)
OE_RAISE(OE_INVALID_PARAMETER);
for (size_t i = 0; i < format_ids_length; i++)
{
if (oe_attest_find_plugin(attesters, format_ids + i, NULL))
{
memcpy(selected_format_id, format_ids + i, sizeof(oe_uuid_t));
result = OE_OK;
break;
}
}
done:
return result;
}
oe_result_t oe_find_attester_plugin(
const oe_uuid_t* format_id,
oe_attester_t** attester_plugin)
{
oe_result_t result = OE_UNEXPECTED;
oe_plugin_list_node_t* plugin_node = NULL;
if (!format_id || !attester_plugin)
OE_RAISE(OE_INVALID_PARAMETER);
plugin_node = oe_attest_find_plugin(attesters, format_id, NULL);
if (!plugin_node)
OE_RAISE(OE_NOT_FOUND);
*attester_plugin = (oe_attester_t*)plugin_node->plugin;
result = OE_OK;
done:
return result;
}