-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_app.cc
229 lines (193 loc) · 7.82 KB
/
example_app.cc
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
// Copyright (c) 2021-22, VMware Inc, and the Certifier Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <openssl/err.h>
#include "certifier_framework.h"
using namespace certifier::framework;
// operations are: cold-init, warm-restart, get-certifier, run-app-as-client, run-app-as-server
DEFINE_bool(print_all, false, "verbose");
DEFINE_string(operation, "", "operation");
DEFINE_string(policy_host, "localhost", "address for policy server");
DEFINE_int32(policy_port, 8123, "port for policy server");
DEFINE_string(data_dir, "./app1_data/", "directory for application data");
DEFINE_string(server_app_host, "localhost", "address for app server");
DEFINE_int32(server_app_port, 8124, "port for server app server");
DEFINE_string(policy_store_file, "store.bin", "policy store file name");
DEFINE_string(platform_file_name, "platform_file.bin", "platform certificate");
DEFINE_string(platform_attest_endorsement, "platform_attest_endorsement.bin", "platform endorsement of attest key");
DEFINE_string(attest_key_file, "attest_key_file.bin", "attest key");
DEFINE_string(measurement_file, "example_app.measurement", "measurement");
// The test app performs five possible roles
// cold-init: This creates application keys and initializes the policy store.
// warm-restart: This retrieves the policy store data.
// get-certifier: This obtains the app admission cert naming the public app key from the service.
// run-app-as-client: This runs the app as a server.
// run-app-as-server: This runs the app as a client
#include "policy_key.cc"
cc_trust_data* app_trust_data = nullptr;
// -----------------------------------------------------------------------------------------
void client_application(secure_authenticated_channel& channel) {
printf("Client peer id is %s\n", channel.peer_id_.c_str());
if (channel.peer_cert_ != nullptr) {
printf("Client peer cert is:\n");
#ifdef DEBUG
X509_print_fp(stdout, channel.peer_cert_);
#endif
}
// client sends a message over authenticated, encrypted channel
const char* msg = "Hi from your secret client\n";
channel.write(strlen(msg), (byte*)msg);
// Get server response over authenticated, encrypted channel and print it
string out;
int n = channel.read(&out);
printf("SSL client read: %s\n", out.data());
}
void server_application(secure_authenticated_channel& channel) {
printf("Server peer id is %s\n", channel.peer_id_.c_str());
if (channel.peer_cert_ != nullptr) {
printf("Server peer cert is:\n");
#ifdef DEBUG
X509_print_fp(stdout, channel.peer_cert_);
#endif
}
// Read message from client over authenticated, encrypted channel
string out;
int n = channel.read(&out);
printf("SSL server read: %s\n", (const char*) out.data());
// Reply over authenticated, encrypted channel
const char* msg = "Hi from your secret server\n";
channel.write(strlen(msg), (byte*)msg);
}
int main(int an, char** av) {
gflags::ParseCommandLineFlags(&an, &av, true);
an = 1;
::testing::InitGoogleTest(&an, av);
if (FLAGS_operation == "") {
printf("example_app.exe --print_all=true|false --operation=op --policy_host=policy-host-address --policy_port=policy-host-port\n");
printf("\t --data_dir=-directory-for-app-data --server_app_host=my-server-host-address --server_app_port=server-host-port\n");
printf("\t --policy_cert_file=self-signed-policy-cert-file-name --policy_store_file=policy-store-file-name\n");
printf("Operations are: cold-init, warm-restart, get-certifier, run-app-as-client, run-app-as-server\n");
return 0;
}
SSL_library_init();
string enclave_type("simulated-enclave");
string purpose("authentication");
string store_file(FLAGS_data_dir);
store_file.append(FLAGS_policy_store_file);
app_trust_data = new cc_trust_data(enclave_type, purpose, store_file);
if (app_trust_data == nullptr) {
printf("couldn't initialize trust object\n");
return 1;
}
// Init policy key info
if (!app_trust_data->init_policy_key(initialized_cert_size, initialized_cert)) {
printf("Can't init policy key\n");
return 1;
}
// Init simulated enclave
string attest_key_file_name(FLAGS_data_dir);
attest_key_file_name.append(FLAGS_attest_key_file);
string platform_attest_file_name(FLAGS_data_dir);
platform_attest_file_name.append(FLAGS_platform_attest_endorsement);
string measurement_file_name(FLAGS_data_dir);
measurement_file_name.append(FLAGS_measurement_file);
string attest_endorsement_file_name(FLAGS_data_dir);
attest_endorsement_file_name.append(FLAGS_platform_attest_endorsement);
if (!app_trust_data->initialize_simulated_enclave_data(attest_key_file_name,
measurement_file_name, attest_endorsement_file_name)) {
printf("Can't init simulated enclave\n");
return 1;
}
// Standard algorithms for the enclave
string public_key_alg("rsa-2048");
string symmetric_key_alg("aes-256-cbc-hmac-sha256");
// Carry out operation
int ret = 0;
if (FLAGS_operation == "cold-init") {
if (!app_trust_data->cold_init(public_key_alg, symmetric_key_alg)) {
printf("cold-init failed\n");
ret = 1;
}
} else if (FLAGS_operation == "warm-restart") {
if (!app_trust_data->warm_restart()) {
printf("warm-restart failed\n");
ret = 1;
}
} else if (FLAGS_operation == "get-certifier") {
if (!app_trust_data->certify_me(FLAGS_policy_host, FLAGS_policy_port)) {
printf("certification failed\n");
ret = 1;
}
} else if (FLAGS_operation == "run-app-as-client") {
string my_role("client");
secure_authenticated_channel channel(my_role);
if (!app_trust_data->warm_restart()) {
printf("warm-restart failed\n");
ret = 1;
goto done;
}
printf("Running App as client\n");
if (!app_trust_data->cc_auth_key_initialized_ ||
!app_trust_data->cc_policy_info_initialized_) {
printf("trust data not initialized\n");
ret = 1;
goto done;
}
if (!channel.init_client_ssl(FLAGS_server_app_host, FLAGS_server_app_port,
app_trust_data->serialized_policy_cert_,
app_trust_data->private_auth_key_,
app_trust_data->private_auth_key_.certificate())) {
printf("Can't init client app\n");
ret = 1;
goto done;
}
// This is the actual application code.
client_application(channel);
} else if (FLAGS_operation == "run-app-as-server") {
if (!app_trust_data->warm_restart()) {
printf("warm-restart failed\n");
ret = 1;
goto done;
}
printf("Running App as server\n");
if (!server_dispatch(FLAGS_server_app_host, FLAGS_server_app_port,
app_trust_data->serialized_policy_cert_,
app_trust_data->private_auth_key_,
app_trust_data->private_auth_key_.certificate(),
server_application)) {
ret = 1;
goto done;
}
} else {
printf("Unknown operation\n");
}
done:
// app_trust_data->print_trust_data();
app_trust_data->clear_sensitive_data();
if (app_trust_data != nullptr) {
delete app_trust_data;
}
return ret;
}