Commit c32e141e authored by LIly's avatar LIly
Browse files

add submodule to messaging agent

parent ea8d9437
[submodule "ebc_enclave_messaging"]
path = ebc_enclave_messaging
url = https://gitlab.mpi-sws.org/encounters/ebc_enclave_messaging.git
/*
* Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
# include <unistd.h>
# include <pwd.h>
# define MAX_PATH FILENAME_MAX
#include "sgx_urts.h"
#include "App.h"
#include "Enclave_u.h"
/* Global EID shared by multiple threads */
sgx_enclave_id_t global_eid = 0;
typedef struct _sgx_errlist_t {
sgx_status_t err;
const char *msg;
const char *sug; /* Suggestion */
} sgx_errlist_t;
/* Error code returned by sgx_create_enclave */
static sgx_errlist_t sgx_errlist[] = {
{
SGX_ERROR_UNEXPECTED,
"Unexpected error occurred.",
NULL
},
{
SGX_ERROR_INVALID_PARAMETER,
"Invalid parameter.",
NULL
},
{
SGX_ERROR_OUT_OF_MEMORY,
"Out of memory.",
NULL
},
{
SGX_ERROR_ENCLAVE_LOST,
"Power transition occurred.",
"Please refer to the sample \"PowerTransition\" for details."
},
{
SGX_ERROR_INVALID_ENCLAVE,
"Invalid enclave image.",
NULL
},
{
SGX_ERROR_INVALID_ENCLAVE_ID,
"Invalid enclave identification.",
NULL
},
{
SGX_ERROR_INVALID_SIGNATURE,
"Invalid enclave signature.",
NULL
},
{
SGX_ERROR_OUT_OF_EPC,
"Out of EPC memory.",
NULL
},
{
SGX_ERROR_NO_DEVICE,
"Invalid SGX device.",
"Please make sure SGX module is enabled in the BIOS, and install SGX driver afterwards."
},
{
SGX_ERROR_MEMORY_MAP_CONFLICT,
"Memory map conflicted.",
NULL
},
{
SGX_ERROR_INVALID_METADATA,
"Invalid enclave metadata.",
NULL
},
{
SGX_ERROR_DEVICE_BUSY,
"SGX device was busy.",
NULL
},
{
SGX_ERROR_INVALID_VERSION,
"Enclave version was invalid.",
NULL
},
{
SGX_ERROR_INVALID_ATTRIBUTE,
"Enclave was not authorized.",
NULL
},
{
SGX_ERROR_ENCLAVE_FILE_ACCESS,
"Can't open enclave file.",
NULL
},
{
SGX_ERROR_SERVICE_INVALID_PRIVILEGE,
"Invalid privilege error",
NULL
},
};
/* Check error conditions for loading enclave */
void print_error_message(sgx_status_t ret)
{
size_t idx = 0;
size_t ttl = sizeof sgx_errlist/sizeof sgx_errlist[0];
for (idx = 0; idx < ttl; idx++) {
if(ret == sgx_errlist[idx].err) {
if(NULL != sgx_errlist[idx].sug)
printf("Info: %s\n", sgx_errlist[idx].sug);
printf("Error: %s\n", sgx_errlist[idx].msg);
break;
}
}
if (idx == ttl)
printf("Error: Not Found. Unexpected error occurred.\n");
}
/* Initialize the enclave:
* Step 1: try to retrieve the launch token saved by last transaction
* Step 2: call sgx_create_enclave to initialize an enclave instance
* Step 3: save the launch token if it is updated
*/
int initialize_enclave(void)
{
char token_path[MAX_PATH] = {'\0'};
sgx_launch_token_t token = {0};
sgx_status_t ret = SGX_ERROR_UNEXPECTED;
int updated = 0;
/* Step 1: try to retrieve the launch token saved by last transaction
* if there is no token, then create a new one.
*/
/* try to get the token saved in $HOME */
const char *home_dir = getpwuid(getuid())->pw_dir;
if (home_dir != NULL &&
(strlen(home_dir)+strlen("/")+sizeof(TOKEN_FILENAME)+1) <= MAX_PATH) {
/* compose the token path */
strncpy(token_path, home_dir, strlen(home_dir));
strncat(token_path, "/", strlen("/"));
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME)+1);
} else {
/* if token path is too long or $HOME is NULL */
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
}
FILE *fp = fopen(token_path, "rb");
if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path);
}
if (fp != NULL) {
/* read the token from saved file */
size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
/* if token is invalid, clear the buffer */
memset(&token, 0x0, sizeof(sgx_launch_token_t));
printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
}
}
/* Step 2: call sgx_create_enclave to initialize an enclave instance */
/* Debug Support: set 2nd parameter to 1 */
ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated, &global_eid, NULL);
if (ret != SGX_SUCCESS) {
print_error_message(ret);
if (fp != NULL) fclose(fp);
return -1;
}
/* Step 3: save the launch token if it is updated */
if (updated == FALSE || fp == NULL) {
/* if the token is not updated, or file handler is invalid, do not perform saving */
if (fp != NULL) fclose(fp);
return 0;
}
/* reopen the file with write capablity */
fp = freopen(token_path, "wb", fp);
if (fp == NULL) return 0;
size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
if (write_num != sizeof(sgx_launch_token_t))
printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
fclose(fp);
return 0;
}
/* Application entry */
int SGX_CDECL main(int argc, char *argv[])
{
(void)(argc);
(void)(argv);
/* Initialize the enclave */
if(initialize_enclave() < 0){
printf("Enter a character before exit ...\n");
getchar();
return -1;
}
/* Start TLS Server in Enclave */
ecall_start_tls_server(global_eid);
/* Destroy the enclave */
sgx_destroy_enclave(global_eid);
printf("Info: Sample TLS Server successfully returned.\n");
printf("Enter a character before exit ...\n");
getchar();
return 0;
}
/*
* Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef _APP_H_
#define _APP_H_
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "sgx_error.h" /* sgx_status_t */
#include "sgx_eid.h" /* sgx_enclave_id_t */
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
# define TOKEN_FILENAME "enclave.token"
# define ENCLAVE_FILENAME "enclave.signed.so"
extern sgx_enclave_id_t global_eid; /* global enclave id */
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(__cplusplus)
}
#endif
#endif /* !_APP_H_ */
#include "Ocall_implements.h"
#include "Enclave_u.h"
long ocall_sgx_clock(void)
{
struct timespec tstart={0,0}, tend={0,0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
return tstart.tv_sec * 1000000 + tstart.tv_nsec/1000; // Return micro seconds
}
time_t ocall_sgx_time(time_t *timep, int t_len)
{
return time(timep);
}
struct tm *ocall_sgx_localtime(const time_t *timep, int t_len)
{
return localtime(timep);
}
struct tm *ocall_sgx_gmtime_r(const time_t *timep, int t_len, struct tm *tmp, int tmp_len)
{
return gmtime_r(timep, tmp);
}
int ocall_sgx_gettimeofday(void *tv, int tv_size)
{
return gettimeofday((struct timeval *)tv, NULL);
}
int ocall_sgx_getsockopt(int s, int level, int optname, char *optval, int optval_len, int* optlen)
{
return getsockopt(s, level, optname, optval, (socklen_t *)optlen);
}
int ocall_sgx_setsockopt(int s, int level, int optname, const void *optval, int optlen)
{
return setsockopt(s, level, optname, optval, optlen);
}
int ocall_sgx_socket(int af, int type, int protocol)
{
int retv;
retv = socket(af, type, protocol);
return retv;
}
int ocall_sgx_bind(int s, const void *addr, int addr_size)
{
return bind(s, (struct sockaddr *)addr, addr_size);
}
int ocall_sgx_listen(int s, int backlog)
{
return listen(s, backlog);
}
int ocall_sgx_connect(int s, const void *addr, int addrlen)
{
int retv = connect(s, (struct sockaddr *)addr, addrlen);
return retv;
}
int ocall_sgx_accept(int s, void *addr, int addr_size, int *addrlen)
{
return accept(s, (struct sockaddr *)addr, (socklen_t *)addrlen);
}
int ocall_sgx_shutdown(int fd, int how)
{
return shutdown(fd, how);
}
int ocall_sgx_read(int fd, void *buf, int n)
{
return read(fd, buf, n);
}
int ocall_sgx_write(int fd, const void *buf, int n)
{
return write(fd, buf, n);
}
int ocall_sgx_close(int fd)
{
return close(fd);
}
int ocall_sgx_getenv(const char *env, int envlen, char *ret_str,int ret_len)
{
const char *env_val = getenv(env);
if(env_val == NULL){
return -1;
}
memcpy(ret_str, env_val, strlen(env_val)+1);
return 0;
}
void ocall_print_string(const char *str)
{
printf("%s", str);
}
void ocall_sgx_exit(int e)
{
exit(e);
}
int ocall_lstat(const char *path, struct stat* buf, int size){
//printf("Entering %s\n", __func__);
return lstat(path, buf);
}
int ocall_stat(const char *path, struct stat* buf, int size){
//printf("Entering %s\n", __func__);
return stat(path, buf);
}
int ocall_fstat(int fd, struct stat* buf, int size){
//printf("Entering %s\n", __func__);
return fstat(fd, buf);
}
int ocall_ftruncate(int fd, off_t length){
//printf("Entering %s\n", __func__);
return ftruncate(fd, length);
}
char* ocall_getcwd(char *buf, size_t size){
//printf("Entering %s\n", __func__);
return getcwd(buf, size);
}
int ocall_getpid(void){
//printf("Entering %s\n", __func__);
return getpid();
}
int ocall_open64(const char *filename, int flags, mode_t mode){
//printf("Entering %s\n", __func__);
return open(filename, flags, mode); // redirect it to open() instead of open64()
}
off_t ocall_lseek64(int fd, off_t offset, int whence){
//printf("Entering %s\n", __func__);
return lseek(fd, offset, whence); // redirect it to lseek() instead of lseek64()
}
int ocall_read(int fd, void *buf, size_t count){
//printf("Entering %s\n", __func__);
return read(fd, buf, count);
}
int ocall_write(int fd, const void *buf, size_t count){
//printf("Entering %s\n", __func__);
return write(fd, buf, count);
}
int ocall_fcntl(int fd, int cmd, void* arg, size_t size){
//printf("Entering %s\n", __func__);
return fcntl(fd, cmd, arg);
}
int ocall_close(int fd){
//printf("Entering %s\n", __func__);
return close(fd);
}
int ocall_unlink(const char *pathname){
//printf("Entering %s\n", __func__);
return unlink(pathname);
}
int ocall_getuid(void){
//printf("Entering %s\n", __func__);
return getuid();
}
char* ocall_getenv(const char *name){
//printf("Entering %s\n", __func__);
return getenv(name);
}
int ocall_fsync(int fd){
//printf("Entering %s\n", __func__);
return fsync(fd);
}
int ocall_fchmod(int fd, mode_t mode){
//printf("Entering %s\n", __func__);
return fchmod(fd, mode);
}
#ifndef _OCALL_IMPLEMENTS_H_
#define _OCALL_IMPLEMENTS_H_
#include <stdio.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DO_NOT_REDEFINE_FOR_OCALL
#if defined(__cplusplus)
extern "C" {
#endif
long ocall_sgx_clock(void); /* For Performance evaluation */
time_t ocall_sgx_time(time_t *timep, int t_len);
struct tm *ocall_sgx_localtime(const time_t *timep, int t_len);
struct tm *ocall_sgx_gmtime_r(const time_t *timep, int t_len, struct tm *tmp, int tmp_len);
int ocall_sgx_gettimeofday(void *tv, int tv_size);
int ocall_sgx_getsockopt(int s, int level, int optname, char *optval, int optval_len, int* optlen);
int ocall_sgx_setsockopt(int s, int level, int optname, const void *optval, int optlen);
int ocall_sgx_socket(int af, int type, int protocol);
int ocall_sgx_bind(int s, const void *addr, int addr_size);
int ocall_sgx_connect(int s, const void *addr, int addrlen);
int ocall_sgx_accept(int s, void *addr, int addr_size, int *addrlen);
int ocall_sgx_shutdown(int fd, int how);
int ocall_sgx_read(int fd, void *buf, int n);
int ocall_sgx_write(int fd, const void *buf, int n);
int ocall_sgx_close(int fd);
int ocall_sgx_getenv(const char *env, int envlen, char *ret_str,int ret_len);
void ocall_print_string(const char *str);
int ocall_lstat(const char *path, struct stat* buf, int size);
int ocall_stat(const char *path, struct stat* buf, int size);
int ocall_fstat(int fd, struct stat* buf, int size);
int ocall_ftruncate(int fd, off_t length);
char* ocall_getcwd(char *buf, size_t size);
int ocall_getpid(void);
int ocall_open64(const char *filename, int flags, mode_t mode);
off_t ocall_lseek64(int fd, off_t offset, int whence);
int ocall_read(int fd, void *buf, size_t count);
int ocall_write(int fd, const void *buf, size_t count);
int ocall_fcntl(int fd, int cmd, void* arg, size_t size);
int ocall_close(int fd);
int ocall_unlink(const char *pathname);
int ocall_getuid(void);
char* ocall_getenv(const char *name);
int ocall_fsync(int fd);
int ocall_fchmod(int fd, mode_t mode);
#if defined(__cplusplus)
}
#endif
#endif /* !_OCALL_IMPLEMENTS_H_ */
/********************************************************************
Testing and Other Settings
********************************************************************/
// Nice print statements :) (very verbose)
#define PRINT_DEBUG 1
// Run the forwarding agent in test-performance mode (loops over the same message foreverrrr)
// Disable this in order to actually run the message forwarding service in production
#define TEST_PERF 0
// Don't upload encounters from the testing client via the network.
// I advise you keep this on while testing.
#define TEST_UPLOAD_NO_NETWORK 1
// Choose a random sender from the encounter graph to send the message
// Otherwise the sender is just 0
#define TEST_RANDOM_SENDER 1
// use the sql database to do fwding instead of in-memory. not implemented...
#define USESQL 0
/********************************************************************
These match the delimiters used to upload encounters and whatnot
in the Android code
********************************************************************/
#define AUTHDELIMITER "AUTHDELIMITER"
#define ENCDELIMITER "ENCDELIMITER"
#define LISTDELIMITER ";"