[Assignment-7] add SGX sample code from VM
All checks were successful
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (push) Successful in 1m2s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (push) Successful in 1m3s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (push) Successful in 1m0s
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (pull_request) Successful in 30s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (pull_request) Successful in 10s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (pull_request) Successful in 8s

This commit is contained in:
Paul Zinselmeyer 2024-06-29 17:55:44 +02:00
parent 5616ddc4e5
commit ba8e969470
Signed by: pfzetto
GPG key ID: B471A1AF06C895FD
163 changed files with 24030 additions and 0 deletions

View file

@ -0,0 +1,99 @@
#include "Base64.h"
#include <iostream>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i=0; i<4; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i) {
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j=0; j<i+1; j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_];
in_++;
if (i ==4) {
for (i=0; i<4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i=0; i<3; i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j=i; j<4; j++)
char_array_4[j] = 0;
for (j=0; j<4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j=0; j<i-1; j++)
ret += char_array_3[j];
}
return ret;
}

View file

@ -0,0 +1,9 @@
#ifndef BASE64_H
#define BASE64_H
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif

View file

@ -0,0 +1,85 @@
#include "LogBase.h"
#include <iostream>
namespace util {
LogBase* LogBase::instance = NULL;
LogBase* LogBase::Inst() {
if (instance == NULL) {
instance = new LogBase();
}
return instance;
}
LogBase::LogBase() {
m_enabled[log::verbose] = false;
m_enabled[log::info] = true;
m_enabled[log::warning] = true;
m_enabled[log::error] = true;
m_enabled[log::timer] = false;
this->appender = new log4cpp::OstreamAppender("console", &std::cout);
this->appender->setLayout(new log4cpp::BasicLayout());
root.setPriority(log4cpp::Priority::INFO);
root.addAppender(this->appender);
}
LogBase::~LogBase() {}
void LogBase::Log(const log::Fmt& msg, log::Severity s) {
if (IsEnabled(s) && !IsEnabled(log::timer)) {
switch (s) {
case log::info:
root.info(msg.str());
break;
case log::error:
root.error(msg.str());
break;
case log::warning:
root.warn(msg.str());
break;
}
}
}
bool LogBase::Enable(log::Severity s, bool enable) {
bool prev = m_enabled[s];
m_enabled[s] = enable;
return prev;
}
void LogBase::DisableAll(bool b) {
m_enabled[log::verbose] = b;
m_enabled[log::info] = b;
m_enabled[log::warning] = b;
m_enabled[log::error] = b;
m_enabled[log::timer] = b;
}
bool LogBase::IsEnabled( log::Severity s ) const {
return m_enabled[s];
}
void Log(const string& str, log::Severity s) {
LogBase::Inst()->Log(log::Fmt(str), s);
}
void DisableAllLogs(bool b) {
LogBase::Inst()->DisableAll(b);
}
}

View file

@ -0,0 +1,89 @@
#ifndef LOG_H
#define LOG_H
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>
#include <boost/format.hpp>
#include <memory>
#include <bitset>
#include <mutex>
#include <string>
using namespace std;
namespace util {
namespace log {
enum Severity {
verbose,
info,
warning,
error,
timer,
severity_count
};
typedef boost::format Fmt;
}
class LogBase {
public:
static LogBase* Inst();
void Log(const log::Fmt& msg, log::Severity s = log::info);
bool Enable(log::Severity s, bool enable = true);
bool IsEnabled(log::Severity s) const;
void DisableAll(bool b);
virtual ~LogBase();
private:
LogBase();
private:
std::bitset<log::severity_count> m_enabled;
static LogBase *instance;
log4cpp::Appender *appender;
log4cpp::Category &root = log4cpp::Category::getRoot();
struct timespec start;
vector<pair<string, string>> measurements;
};
void Log(const std::string& str, log::Severity s = log::info);
void DisableAllLogs(bool b);
template <typename P1>
void Log(const std::string& fmt, const P1& p1, log::Severity s = log::info) {
LogBase::Inst()->Log(log::Fmt(fmt) % p1, s);
}
template <typename P1, typename P2>
void Log(const std::string& fmt, const P1& p1, const P2& p2, log::Severity s = log::info) {
LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2, s ) ;
}
template <typename P1, typename P2, typename P3>
void Log(const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, log::Severity s = log::info) {
LogBase::Inst()->Log(log::Fmt(fmt) % p1 % p2 % p3, s);
}
template <typename P1, typename P2, typename P3, typename P4>
void Log(const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, const P4& p4, log::Severity s = log::info) {
LogBase::Inst()->Log(log::Fmt(fmt) % p1 % p2 % p3 % p4, s);
}
template <typename P1, typename P2, typename P3, typename P4, typename P5>
void Log(const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, log::Severity s = log::info) {
LogBase::Inst()->Log(log::Fmt(fmt) % p1 % p2 % p3 % p4 % p5, s);
}
}
#endif

View file

@ -0,0 +1,313 @@
#include "UtilityFunctions.h"
using namespace util;
void SafeFree(void *ptr) {
if (NULL != ptr) {
free(ptr);
ptr = NULL;
}
}
string GetRandomString() {
string str = lexical_cast<string>((random_generator())());
str.erase(remove(str.begin(), str.end(), '-'), str.end());
return str;
}
string ByteArrayToString(const uint8_t *arr, int size) {
ostringstream convert;
for (int a = 0; a < size; a++) {
convert << setfill('0') << setw(2) << hex << (unsigned int)arr[a];
}
return convert.str();
}
string ByteArrayToStringNoFill(const uint8_t *arr, int size) {
ostringstream convert;
for (int a = 0; a < size; a++) {
convert << hex << (int)arr[a];
}
return convert.str();
}
int HexStringToByteArray(string str, uint8_t **arr) {
vector<uint8_t> bytes;
for (unsigned int i=0; i<str.length(); i+=2) {
string byteString = str.substr(i, 2);
char byte = (char) strtol(byteString.c_str(), NULL, 16);
bytes.push_back((unsigned char)byte);
}
*arr = (uint8_t*) malloc(sizeof(uint8_t) * bytes.size());
copy(bytes.begin(), bytes.end(), *arr);
return bytes.size();
}
int StringToByteArray(string str, uint8_t **arr) {
vector<uint8_t> vec(str.begin(), str.end());
*arr = (uint8_t*) malloc(sizeof(uint8_t) * vec.size());
copy(vec.begin(), vec.end(), *arr);
return vec.size();
}
string ByteArrayToNoHexString(const uint8_t *arr, int size) {
std::ostringstream convert;
for (int a = 0; a < size; a++) {
convert << (uint8_t)arr[a];
}
return convert.str();
}
string UIntToString(uint32_t *arr, int size) {
stringstream ss;
for (int i=0; i<size; i++) {
ss << arr[i];
}
return ss.str();
}
int SaveBufferToFile(string filePath, string content) {
std::ofstream out(filePath);
out << content;
out.close();
return 0;
}
int ReadFileToBuffer(string filePath, char **content) {
ifstream t(filePath);
string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
*content = (char*) malloc(sizeof(char) * (str.size()+1));
memset(*content, '\0', (str.size()+1));
str.copy(*content, str.size());
return str.size();
}
int ReadFileToBuffer(string filePath, uint8_t **content) {
ifstream file(filePath, ios::binary | ios::ate);
streamsize file_size = file.tellg();
file.seekg(0, ios::beg);
std::vector<char> buffer(file_size);
if (file.read(buffer.data(), file_size)) {
string str(buffer.begin(), buffer.end());
vector<uint8_t> vec(str.begin(), str.end());
*content = (uint8_t*) malloc(sizeof(uint8_t) * vec.size());
copy(vec.begin(), vec.end(), *content);
return str.length();
}
return -1;
}
int RemoveFile(string filePath) {
if (remove(filePath.c_str()) != 0 ) {
Log("Error deleting file: " + filePath);
return 1;
} else
Log("File deleted successfully: " + filePath);
return 0;
}
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_MODE_INCOMPATIBLE,
"Target enclave mode is incompatible with the mode of the current RTS",
NULL
},
{
SGX_ERROR_SERVICE_UNAVAILABLE,
"sgx_create_enclave() needs the AE service to get a launch token",
NULL
},
{
SGX_ERROR_SERVICE_TIMEOUT,
"The request to the AE service timed out",
NULL
},
{
SGX_ERROR_SERVICE_INVALID_PRIVILEGE,
"The request requires some special attributes for the enclave, but is not privileged",
NULL
},
{
SGX_ERROR_NDEBUG_ENCLAVE,
"The enclave is signed as a product enclave and cannot be created as a debuggable enclave",
NULL
},
{
SGX_ERROR_UNDEFINED_SYMBOL,
"The enclave contains an import table",
NULL
},
{
SGX_ERROR_INVALID_MISC,
"The MiscSelct/MiscMask settings are not correct",
NULL
},
{
SGX_ERROR_MAC_MISMATCH,
"The input MAC does not match the MAC calculated",
NULL
}
};
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)
Log("%s", sgx_errlist[idx].sug);
Log("%s", sgx_errlist[idx].msg);
break;
}
}
if (idx == ttl)
Log("Unexpected error occurred");
}
string Base64decode(const string val) {
return base64_decode(val);
}
string Base64encodeUint8(uint8_t *val, uint32_t len) {
return base64_encode(val, len);
}

View file

@ -0,0 +1,65 @@
#ifndef UTILITY_FUNCTIONS_H
#define UTILITY_FUNCTIONS_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
// #include <jsoncpp/json/json.h>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "LogBase.h"
#include "sgx_urts.h"
#include "Base64.h"
using namespace std;
using namespace boost::archive::iterators;
using boost::lexical_cast;
using boost::uuids::uuid;
using boost::uuids::random_generator;
#define FILE_UUID_LENGTH 32
typedef struct _sgx_errlist_t {
sgx_status_t err;
const char *msg;
const char *sug; /* Suggestion */
} sgx_errlist_t;
void print_error_message(sgx_status_t ret);
void SafeFree(void *ptr);
string GetRandomString();
string ByteArrayToString(const uint8_t *arr, int size);
string ByteArrayToStringNoFill(const uint8_t *arr, int size);
int StringToByteArray(string str, uint8_t **arr);
string ByteArrayToNoHexString(const uint8_t *arr, int size);
string UIntToString(uint32_t *arr, int size);
int HexStringToByteArray(string str, uint8_t **arr);
int ReadFileToBuffer(string filePath, uint8_t **content);
int ReadFileToBuffer(string filePath, char **content);
int SaveBufferToFile(string filePath, string content);
int RemoveFile(string filePath);
string Base64encode(const string val);
string Base64decode(const string val);
string Base64encodeUint8(uint8_t *val, uint32_t len);
#endif