-
Notifications
You must be signed in to change notification settings - Fork 202
/
client.c
458 lines (373 loc) · 15.1 KB
/
client.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 2020, Intel Labs
*/
/*
* SSL client demonstration program (with RA-TLS).
* This program is originally based on an mbedTLS example ssl_client1.c but uses RA-TLS flows (SGX
* Remote Attestation flows) if RA-TLS library is required by user.
* Note that this program builds against mbedTLS 3.x.
*/
#include "mbedtls/build_info.h"
#include <assert.h>
#include <ctype.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define mbedtls_fprintf fprintf
#define mbedtls_printf printf
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/debug.h"
#include "mbedtls/entropy.h"
#include "mbedtls/error.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/ssl.h"
/* RA-TLS: on client, only need to register ra_tls_verify_callback_der() for cert verification */
int (*ra_tls_verify_callback_der_f)(uint8_t* der_crt, size_t der_crt_size);
/* RA-TLS: if specified in command-line options, use our own callback to verify SGX measurements */
void (*ra_tls_set_measurement_callback_f)(int (*f_cb)(const char* mrenclave, const char* mrsigner,
const char* isv_prod_id, const char* isv_svn));
#define SERVER_PORT "4433"
#define SERVER_NAME "localhost"
#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
#define DEBUG_LEVEL 0
#define CA_CRT_PATH "ssl/ca.crt"
static void my_debug(void* ctx, int level, const char* file, int line, const char* str) {
((void)level);
mbedtls_fprintf((FILE*)ctx, "%s:%04d: %s\n", file, line, str);
fflush((FILE*)ctx);
}
static int parse_hex(const char* hex, void* buffer, size_t buffer_size) {
if (strlen(hex) != buffer_size * 2)
return -1;
for (size_t i = 0; i < buffer_size; i++) {
if (!isxdigit(hex[i * 2]) || !isxdigit(hex[i * 2 + 1]))
return -1;
sscanf(hex + i * 2, "%02hhx", &((uint8_t*)buffer)[i]);
}
return 0;
}
/* expected SGX measurements in binary form */
static char g_expected_mrenclave[32];
static char g_expected_mrsigner[32];
static char g_expected_isv_prod_id[2];
static char g_expected_isv_svn[2];
static bool g_verify_mrenclave = false;
static bool g_verify_mrsigner = false;
static bool g_verify_isv_prod_id = false;
static bool g_verify_isv_svn = false;
/* RA-TLS: our own callback to verify SGX measurements */
static int my_verify_measurements(const char* mrenclave, const char* mrsigner,
const char* isv_prod_id, const char* isv_svn) {
assert(mrenclave && mrsigner && isv_prod_id && isv_svn);
if (g_verify_mrenclave &&
memcmp(mrenclave, g_expected_mrenclave, sizeof(g_expected_mrenclave)))
return -1;
if (g_verify_mrsigner &&
memcmp(mrsigner, g_expected_mrsigner, sizeof(g_expected_mrsigner)))
return -1;
if (g_verify_isv_prod_id &&
memcmp(isv_prod_id, g_expected_isv_prod_id, sizeof(g_expected_isv_prod_id)))
return -1;
if (g_verify_isv_svn &&
memcmp(isv_svn, g_expected_isv_svn, sizeof(g_expected_isv_svn)))
return -1;
return 0;
}
/* RA-TLS: mbedTLS-specific callback to verify the x509 certificate */
static int my_verify_callback(void* data, mbedtls_x509_crt* crt, int depth, uint32_t* flags) {
(void)data;
if (depth != 0) {
/* the cert chain in RA-TLS consists of single self-signed cert, so we expect depth 0 */
return MBEDTLS_ERR_X509_INVALID_FORMAT;
}
if (flags) {
/* mbedTLS sets flags to signal that the cert is not to be trusted (e.g., it is not
* correctly signed by a trusted CA; since RA-TLS uses self-signed certs, we don't care
* what mbedTLS thinks and ignore internal cert verification logic of mbedTLS */
*flags = 0;
}
return ra_tls_verify_callback_der_f(crt->raw.p, crt->raw.len);
}
static bool getenv_client_inside_sgx() {
char* str = getenv("RA_TLS_CLIENT_INSIDE_SGX");
if (!str)
return false;
return !strcmp(str, "1") || !strcmp(str, "true") || !strcmp(str, "TRUE");
}
int main(int argc, char** argv) {
int ret;
size_t len;
int exit_code = MBEDTLS_EXIT_FAILURE;
mbedtls_net_context server_fd;
uint32_t flags;
unsigned char buf[1024];
const char* pers = "ssl_client1";
bool in_sgx = getenv_client_inside_sgx();
char* error;
void* ra_tls_verify_lib = NULL;
ra_tls_verify_callback_der_f = NULL;
ra_tls_set_measurement_callback_f = NULL;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_x509_crt cacert;
#if defined(MBEDTLS_DEBUG_C)
mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_x509_crt_init(&cacert);
mbedtls_entropy_init(&entropy);
if (argc < 2 ||
(strcmp(argv[1], "native") && strcmp(argv[1], "epid") && strcmp(argv[1], "dcap"))) {
mbedtls_printf("USAGE: %s native|epid|dcap [SGX measurements]\n", argv[0]);
return 1;
}
if (!strcmp(argv[1], "epid")) {
ra_tls_verify_lib = dlopen("libra_tls_verify_epid.so", RTLD_LAZY);
if (!ra_tls_verify_lib) {
mbedtls_printf("%s\n", dlerror());
mbedtls_printf("User requested RA-TLS verification with EPID but cannot find lib\n");
if (in_sgx) {
mbedtls_printf("Please make sure that you are using client_epid.manifest\n");
}
return 1;
}
} else if (!strcmp(argv[1], "dcap")) {
if (in_sgx) {
/*
* RA-TLS verification with DCAP inside SGX enclave uses dummies instead of real
* functions from libsgx_urts.so, thus we don't need to load this helper library.
*/
ra_tls_verify_lib = dlopen("libra_tls_verify_dcap_gramine.so", RTLD_LAZY);
if (!ra_tls_verify_lib) {
mbedtls_printf("%s\n", dlerror());
mbedtls_printf("User requested RA-TLS verification with DCAP inside SGX but cannot find lib\n");
mbedtls_printf("Please make sure that you are using client_dcap.manifest\n");
return 1;
}
} else {
void* helper_sgx_urts_lib = dlopen("libsgx_urts.so", RTLD_NOW | RTLD_GLOBAL);
if (!helper_sgx_urts_lib) {
mbedtls_printf("%s\n", dlerror());
mbedtls_printf("User requested RA-TLS verification with DCAP but cannot find helper"
" libsgx_urts.so lib\n");
return 1;
}
ra_tls_verify_lib = dlopen("libra_tls_verify_dcap.so", RTLD_LAZY);
if (!ra_tls_verify_lib) {
mbedtls_printf("%s\n", dlerror());
mbedtls_printf("User requested RA-TLS verification with DCAP but cannot find lib\n");
return 1;
}
}
}
if (ra_tls_verify_lib) {
ra_tls_verify_callback_der_f = dlsym(ra_tls_verify_lib, "ra_tls_verify_callback_der");
if ((error = dlerror()) != NULL) {
mbedtls_printf("%s\n", error);
return 1;
}
ra_tls_set_measurement_callback_f = dlsym(ra_tls_verify_lib, "ra_tls_set_measurement_callback");
if ((error = dlerror()) != NULL) {
mbedtls_printf("%s\n", error);
return 1;
}
}
if (argc > 2 && ra_tls_verify_lib) {
if (argc != 6) {
mbedtls_printf("USAGE: %s %s <expected mrenclave> <expected mrsigner>"
" <expected isv_prod_id> <expected isv_svn>\n"
" (first two in hex, last two as decimal; set to 0 to ignore)\n",
argv[0], argv[1]);
return 1;
}
mbedtls_printf("[ using our own SGX-measurement verification callback"
" (via command line options) ]\n");
g_verify_mrenclave = true;
g_verify_mrsigner = true;
g_verify_isv_prod_id = true;
g_verify_isv_svn = true;
(*ra_tls_set_measurement_callback_f)(my_verify_measurements);
if (!strcmp(argv[2], "0")) {
mbedtls_printf(" - ignoring MRENCLAVE\n");
g_verify_mrenclave = false;
} else if (parse_hex(argv[2], g_expected_mrenclave, sizeof(g_expected_mrenclave)) < 0) {
mbedtls_printf("Cannot parse MRENCLAVE!\n");
return 1;
}
if (!strcmp(argv[3], "0")) {
mbedtls_printf(" - ignoring MRSIGNER\n");
g_verify_mrsigner = false;
} else if (parse_hex(argv[3], g_expected_mrsigner, sizeof(g_expected_mrsigner)) < 0) {
mbedtls_printf("Cannot parse MRSIGNER!\n");
return 1;
}
if (!strcmp(argv[4], "0")) {
mbedtls_printf(" - ignoring ISV_PROD_ID\n");
g_verify_isv_prod_id = false;
} else {
errno = 0;
uint16_t isv_prod_id = (uint16_t)strtoul(argv[4], NULL, 10);
if (errno) {
mbedtls_printf("Cannot parse ISV_PROD_ID!\n");
return 1;
}
memcpy(g_expected_isv_prod_id, &isv_prod_id, sizeof(isv_prod_id));
}
if (!strcmp(argv[5], "0")) {
mbedtls_printf(" - ignoring ISV_SVN\n");
g_verify_isv_svn = false;
} else {
errno = 0;
uint16_t isv_svn = (uint16_t)strtoul(argv[5], NULL, 10);
if (errno) {
mbedtls_printf("Cannot parse ISV_SVN\n");
return 1;
}
memcpy(g_expected_isv_svn, &isv_svn, sizeof(isv_svn));
}
} else if (ra_tls_verify_lib) {
mbedtls_printf("[ using default SGX-measurement verification callback"
" (via RA_TLS_* environment variables) ]\n");
(*ra_tls_set_measurement_callback_f)(NULL); /* just to test RA-TLS code */
} else {
mbedtls_printf("[ using normal TLS flows ]\n");
}
mbedtls_printf("\n . Seeding the random number generator...");
fflush(stdout);
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char*)pers, strlen(pers));
if (ret != 0) {
mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
goto exit;
}
mbedtls_printf(" ok\n");
mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
fflush(stdout);
ret = mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT, MBEDTLS_NET_PROTO_TCP);
if (ret != 0) {
mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
goto exit;
}
mbedtls_printf(" ok\n");
mbedtls_printf(" . Setting up the SSL/TLS structure...");
fflush(stdout);
ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT);
if (ret != 0) {
mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
goto exit;
}
mbedtls_printf(" ok\n");
mbedtls_printf(" . Loading the CA root certificate ...");
fflush(stdout);
ret = mbedtls_x509_crt_parse_file(&cacert, CA_CRT_PATH);
if (ret < 0) {
mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", -ret );
goto exit;
}
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
mbedtls_printf(" ok\n");
if (ra_tls_verify_lib) {
/* use RA-TLS verification callback; this will overwrite CA chain set up above */
mbedtls_printf(" . Installing RA-TLS callback ...");
mbedtls_ssl_conf_verify(&conf, &my_verify_callback, NULL);
mbedtls_printf(" ok\n");
}
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
ret = mbedtls_ssl_setup(&ssl, &conf);
if (ret != 0) {
mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
goto exit;
}
ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME);
if (ret != 0) {
mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
goto exit;
}
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_printf(" . Performing the SSL/TLS handshake...");
fflush(stdout);
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret);
goto exit;
}
}
mbedtls_printf(" ok\n");
mbedtls_printf(" . Verifying peer X.509 certificate...");
flags = mbedtls_ssl_get_verify_result(&ssl);
if (flags != 0) {
char vrfy_buf[512];
mbedtls_printf(" failed\n");
mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
mbedtls_printf("%s\n", vrfy_buf);
/* verification failed for whatever reason, fail loudly */
goto exit;
} else {
mbedtls_printf(" ok\n");
}
mbedtls_printf(" > Write to server:");
fflush(stdout);
len = sprintf((char*)buf, GET_REQUEST);
while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
goto exit;
}
}
len = ret;
mbedtls_printf(" %lu bytes written\n\n%s", len, (char*)buf);
mbedtls_printf(" < Read from server:");
fflush(stdout);
do {
len = sizeof(buf) - 1;
memset(buf, 0, sizeof(buf));
ret = mbedtls_ssl_read(&ssl, buf, len);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
continue;
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
break;
if (ret < 0) {
mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
break;
}
if (ret == 0) {
mbedtls_printf("\n\nEOF\n\n");
break;
}
len = ret;
mbedtls_printf(" %lu bytes read\n\n%s", len, (char*)buf);
} while (1);
mbedtls_ssl_close_notify(&ssl);
exit_code = MBEDTLS_EXIT_SUCCESS;
exit:
#ifdef MBEDTLS_ERROR_C
if (exit_code != MBEDTLS_EXIT_SUCCESS) {
char error_buf[100];
mbedtls_strerror(ret, error_buf, sizeof(error_buf));
mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
}
#endif
if (ra_tls_verify_lib)
dlclose(ra_tls_verify_lib);
mbedtls_net_free(&server_fd);
mbedtls_x509_crt_free(&cacert);
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
return exit_code;
}