opt文件夹的生成,和参数配置

本文探讨了 Linux 系统中 opt 目录的不同解释和用途,包括作为第三方软件安装目录、配置文件存放地等观点,并介绍了在项目管理和软件测试场景下的应用。

一种说法:

在Linux中opt目录是用于存放测试软件和 安装目录

另外一种说法是opt是配置文件,记录系统工作环境信息

二种说法:

使用maven来管理项目,这些配置就会自动生成。

第三种说法:

opt目录作用是安装第三方软件的地方。


另一种说法:每次调试的时候,老大都会改下核心服务的端口号,然后在Linux上跑测试。


<think>由于项目完整代码量非常大,这里将聚焦于`enclave_src`文件夹下的核心模块实现,包括:1.`enclave.edl`:Enclave定义语言文件,定义安全函数接口2.`remote_attestation.c`:远程认证实现3.`key_management.c`:密钥生成、密封加密操作4.`isolation_functions.c`:硬件隔离敏感操作(示例为安全计算)5.`CMakeLists.txt`:构建配置注意:以下代码为简化版,适合本科毕设水平,未处理所有错误情况。###1.enclave.edl(Enclave接口定义)```cenclave{include"sgx_key_exchange.h"include"sgx_tcrypto.h"//密钥管理接口trusted{publicvoidgenerate_keys([out]sgx_sealed_data_t*sealed_key,size_tsealed_size);publicsgx_status_tencrypt_data([in,size=len]constuint8_t*plaintext,size_tlen,[out]uint8_t*ciphertext);publicsgx_status_tdecrypt_data([in,size=len]constuint8_t*ciphertext,size_tlen,[out]uint8_t*plaintext);};//远程认证接口trusted{publicsgx_status_tget_attestation_report([in]constsgx_report_data_t*report_data,[out]sgx_report_t*report);publicsgx_status_tverify_peer_report([in]constsgx_report_t*peer_report,[in]constsgx_quote_nonce_t*nonce);};//安全计算接口(示例)trusted{publicvoidcompute_sensitive_data([in,size=len]constuint8_t*data,size_tlen,[out]uint8_t*result);};untrusted{//用于非可信区回调的OCALLvoidprint_string([in,string]constchar*str);};};```###2.key_management.c(密钥管理)```c#include<sgx_tcrypto.h>#include<sgx_tseal.h>#include<sgx_trts.h>#include"key_management.h"#defineAES_KEY_SIZE128//128位AES密钥#defineSEALED_SIZE(sizeof(sgx_sealed_data_t)+AES_KEY_SIZE)//密封后数据大小sgx_status_tgenerate_keys(sgx_sealed_data_t*sealed_key,size_tsealed_size){if(sealed_size< SEALED_SIZE)returnSGX_ERROR_INVALID_PARAMETER;uint8_taes_key[AES_KEY_SIZE/8];//16字节sgx_status_tret=sgx_read_rand(aes_key,sizeof(aes_key));if(ret!=SGX_SUCCESS)returnret;//密封密钥(使用MRENCLAVE作为密钥策略,只有相同Enclave才能解封)returnsgx_seal_data(0,NULL,sizeof(aes_key),aes_key,sealed_size,sealed_key);}sgx_status_tencrypt_data(constuint8_t*plaintext,size_tlen,uint8_t*ciphertext){//首先解封密钥uint32_tunsealed_size=0;uint8_taes_key[AES_KEY_SIZE/8];sgx_sealed_data_t*sealed_key=...;//从安全存储中获取密封密钥sgx_status_tret=sgx_unseal_data(sealed_key,NULL,NULL,aes_key,&unsealed_size);if(ret!=SGX_SUCCESS)returnret;//使用AES-GCM加密(需要初始化向量认证标签)constuint8_tiv[12]={0};//实际应用中应随机生成uint8_tmac[16]={0};//认证标签returnsgx_rijndael128GCM_encrypt((constsgx_aes_gcm_128bit_key_t*)aes_key,plaintext,len,ciphertext,iv,sizeof(iv),NULL,0,//附加认证数据(AAD)(sgx_aes_gcm_128bit_tag_t*)mac//输出的认证标签(实际应用需传递给接收方));}sgx_status_tdecrypt_data(constuint8_t*ciphertext,size_tlen,uint8_t*plaintext){//省略:类似加密过程,使用解封后的密钥调用sgx_rijndael128GCM_decrypt}```###3.remote_attestation.c(远程认证)```c#include<sgx_uae_epid.h>#include<sgx_utils.h>#include<sgx_tcrypto.h>#include"remote_attestation.h"sgx_status_tget_attestation_report(constsgx_report_data_t*report_data,sgx_report_t*report){//创建Enclave身份报告(通常用于证明)returnsgx_create_report(NULL,report_data,report);//第一个参数为target_info,用于本地认证可为NULL}sgx_status_tverify_peer_report(constsgx_report_t*peer_report,constsgx_quote_nonce_t*nonce){//验证对方报告(实际应验证报告签名属性)//示例:检查报告的MRSIGNERMRENCLAVE是否与预期一致constsgx_measurement_texpected_mr=...;//预期值if(memcmp(&peer_report->body.mr_signer,&expected_mr,sizeof(sgx_measurement_t))!=0)returnSGX_ERROR_INVALID_SIGNATURE;//检查报告是否针对此次会话(通过nonce防止重放攻击)if(memcmp(peer_report->body.report_data,nonce,sizeof(sgx_quote_nonce_t))!=0)returnSGX_ERROR_INVALID_REPORT;returnSGX_SUCCESS;}```###4.isolation_functions.c(安全计算示例)```c#include<sgx_trts.h>#include"isolation_functions.h"voidcompute_sensitive_data(constuint8_t*data,size_tlen,uint8_t*result){//简单示例:在安全区计算数据哈希(防止外部窥探)sgx_sha256_hash_thash;sgx_sha256_msg(data,len,&hash);//假设返回前32字节哈希memcpy(result,hash,32);}```###5.CMakeLists.txt(Enclave构建配置)```cmakecmake_minimum_required(VERSION3.10)project(enclave_src)#查找OpenSGXSDKfind_path(SGX_INCLUDE_DIRsgx.hHINTS$ENV{SGX_SDK}/includeREQUIRED)find_library(SGX_LIBRARYsgx_urtsPATH_SUFFIXESlib64)#Enclave设置add_library(enclaveOBJECTisolation_functions.ckey_management.cremote_attestation.c)#编译选项target_include_directories(enclavePRIVATE${SGX_INCLUDE_DIR}$ENV{SGX_SDK}/include)target_compile_options(enclavePRIVATE-m64-Wall-Wextra-fpie)target_compile_definitions(enclavePRIVATESGX_HW)#定义Enclave签名密钥(示例)set(SGX_ENCLAVE_SIGNING_KEY"enclave_private.pem")#生成Enclave签名工具调用add_custom_command(OUTPUTenclave.signed.soCOMMANDopensgx--signenclave.unsigned.so${SGX_ENCLAVE_SIGNING_KEY}--outputenclave.signed.soDEPENDSenclave.unsigned.soCOMMENT"Signingenclave")```###代码说明1.**enclave.edl**:定义了Enclave内外通信接口(ECALLs/OCALLs)。其中:-`trusted`部分为Enclave安全函数(ECALLs)-`untrusted`部分为主机调用的函数(OCALLs)2.**key_management.c**:-`generate_keys`:生成真随机数AES密钥并密封(只有相同Enclave才能解封)-`encrypt_data`:解封密钥后使用AES-GCM加密(带完整性机密性保护)3.**remote_attestation.c**:-`get_attestation_report`:生成本地报告(用于证明Enclave合法性)-`verify_peer_report`:验证对方报告的测量值(如MRSIGNER)nonce防重放4.**isolation_functions.c**:示例安全计算(实际应用中可替换为任意敏感操作)5.**CMakeLists.txt**:配置Enclave编译流程,包括生成签名Enclave###注意事项-**实际部署**:需替换示例中的固定IV、简化错误处理等-**签名密钥**:应使用专用工具生成签名密钥(如`opensslgenrsa`)-**远程认证**:完整实现需支持EPID/ECDSA远程证明(OpenSGX模拟器中可简化)§§相关问题§§:1.如何在OpenSGX中实现跨虚拟机远程认证?2.Enclave密钥的密封策略有几种?如何选择?3.AES-GCM模式中的IV应该如何安全管理?4.如果要在Enclave内实现同态加密,如何优化性能?</think> 以下是完整的`enclave_src`文件夹实现,包含TEE的核心功能模块: ### enclave_src完整代码实现 #### 1. 密钥管理模块 (key_management.c) ```c #include <sgx_tcrypto.h> #include <sgx_tseal.h> #include <sgx_trts.h> #include <string.h> #include "key_management.h" #define KEY_SIZE 32 // AES-256密钥长度 #define SEALED_DATA_SIZE (sizeof(sgx_sealed_data_t) + KEY_SIZE) // 生成并密封密钥 sgx_status_t generate_keys(uint8_t* sealed_key_out, uint32_t sealed_size) { uint8_t aes_key[KEY_SIZE]; // 生成安全随机密钥 sgx_status_t ret = sgx_read_rand(aes_key, KEY_SIZE); if (ret != SGX_SUCCESS) return ret; // 检查输出缓冲区大小 if (sealed_size < SEALED_DATA_SIZE) { return SGX_ERROR_INVALID_PARAMETER; } // 密封密钥到Enclave保护区域 uint32_t sealed_data_size = sgx_calc_sealed_data_size(0, KEY_SIZE); if (sealed_data_size > sealed_size) { return SGX_ERROR_INVALID_PARAMETER; } return sgx_seal_data(0, NULL, KEY_SIZE, aes_key, sealed_data_size, (sgx_sealed_data_t*)sealed_key_out); } // 解封密钥并执行AES加密 sgx_status_t encrypt_with_sealed_key(const uint8_t* sealed_key, const uint8_t* plaintext, size_t plaintext_len, uint8_t* ciphertext) { uint8_t aes_key[KEY_SIZE]; uint32_t unsealed_size = sizeof(aes_key); // 1. 解封密钥 sgx_status_t ret = sgx_unseal_data((sgx_sealed_data_t*)sealed_key, NULL, NULL, aes_key, &unsealed_size); if (ret != SGX_SUCCESS) return ret; // 2. 执行AES-GCM加密 const uint8_t iv[12] = {0}; // 实际应用中应使用随机IV uint8_t mac[SGX_AESGCM_MAC_SIZE] = {0}; return sgx_rijndael128GCM_encrypt( (sgx_aes_gcm_128bit_key_t*)aes_key, plaintext, plaintext_len, ciphertext, iv, sizeof(iv), NULL, 0, // 附加认证数据 (sgx_aes_gcm_128bit_tag_t*)mac ); } // 生成会话密钥对(用于TLS) sgx_status_t generate_session_keys(sgx_ec256_public_t* public_key) { sgx_ec256_private_t private_key; // 生成ECC密钥对(使用NIST P-256曲线) sgx_status_t ret = sgx_ecc256_open_context(&ecc_handle); if (ret != SGX_SUCCESS) return ret; ret = sgx_ecc256_create_key_pair(&private_key, public_key, ecc_handle); sgx_ecc256_close_context(ecc_handle); return ret; } ``` #### 2. 远程认证模块 (remote_attestation.c) ```c #include <sgx_uae_epid.h> #include <sgx_utils.h> #include <sgx_tcrypto.h> #include <string.h> #include "remote_attestation.h" // 获取本地Enclave报告 sgx_status_t get_local_report(const sgx_report_data_t* report_data, sgx_report_t* report) { // 创建Enclave身份报告(实际应用中需包含测量值) return sgx_create_report(NULL, report_data, report); } // 验证远程Enclave报告 sgx_status_t verify_remote_report(const sgx_report_t* remote_report, const sgx_report_data_t* expected_data) { // 1. 校验报告签名 sgx_status_t ret = sgx_verify_report(remote_report); if (ret != SGX_SUCCESS) return ret; // 2. 比对预期数据 if (memcmp(&remote_report->body.report_data, expected_data, sizeof(sgx_report_data_t)) != 0) { return SGX_ERROR_INVALID_PARAMETER; } // 3. 校验Enclave属性(生产模式、调试模式等) if ((remote_report->body.attributes.flags & SGX_FLAGS_INITTED) == 0 || (remote_report->body.attributes.flags & SGX_FLAGS_DEBUG) != 0) { return SGX_ERROR_INVALID_ATTRIBUTE; } return SGX_SUCCESS; } // 简化版远程认证流程(实际应使用EPID或DCAP) sgx_status_t perform_remote_attestation(sgx_report_t* local_report) { sgx_report_data_t report_data = {0}; sgx_report_t remote_report; // 1. 本地生成报告 SGX_CHECK(get_local_report(&report_data, local_report)); // 2. 发送报告给验证方(通常通过quote) // [模拟] 验证方生成预期的report_data sgx_report_data_t expected_data; sgx_sha256_hash_t hash; sgx_sha256_msg((const uint8_t*)"TRUSTED_ENCLAVE", 15, &hash); memcpy(&expected_data, hash, sizeof(expected_data)); // 3. 验证方返回签署的远程报告(此处简化) // [实际实现需通过quoting enclave获取合法quote] memcpy(&remote_report, local_report, sizeof(sgx_report_t)); // 4. 验证远程报告 return verify_remote_report(&remote_report, &expected_data); } ``` #### 3. 安全隔离模块 (isolation_functions.c) ```c #include <sgx_trts.h> #include <sgx_tprotected_fs.h> #include <stdlib.h> #include "isolation_functions.h" // 安全内存区域操作 void* secure_malloc(size_t size) { // 在Enclave保护内存中分配 void* ptr = malloc(size); if (!ptr) return NULL; // 使用后自动清除内存内容 sgx_register_memory(ptr, size, SGX_PROT_READ_WRITE); return ptr; } // 安全数据清理 void secure_free(void* ptr, size_t size) { if (ptr) { // 安全擦除内存内容 sgx_memset_s(ptr, size, 0, size); sgx_unregister_memory(ptr, size); free(ptr); } } // 使用受保护文件系统存储敏感数据 sgx_status_t save_protected_data(const char* filename, const uint8_t* data, size_t size) { sgx_status_t ret; SGX_FILE* file = sgx_fopen(filename, "wb", SGX_PROTECTED_FS); if (!file) return SGX_ERROR_UNEXPECTED; size_t written = sgx_fwrite(data, 1, size, file); if (written != size) { sgx_fclose(file); return SGX_ERROR_UNEXPECTED; } ret = sgx_fclose(file); return ret; } // 安全计算示例 void secure_computation(int* input, int* output, size_t size) { for (size_t i = 0; i < size; i++) { // 在安全区域内执行敏感计算 output[i] = input[i] * 2 + 1; // 示例计算 } } ``` #### 4. Enclave配置文件 (enclave.config.xml) ```xml <EnclaveConfiguration> <ProdID>0</ProdID> <ISVSVN>1</ISVSVN> <StackMaxSize>0x50000</StackMaxSize> <HeapMaxSize>0x100000</HeapMaxSize> <TCSNum>1</TCSNum> <TCSPolicy>1</TCSPolicy> <DisableDebug>0</DisableDebug> <KSS>0</KSS> <!-- 允许的ECALL函数列表 --> <EnableExtendedFeature>0x0000</EnableExtendedFeature> <EnableEnclavePageCache>1</EnableEnclavePageCache> <!-- 安全启动配置 --> <LaunchKey>0</LaunchKey> <EnableHWKey>0</EnableHWKey> <!-- 内存保护设置 --> <EnableAEX>0</EnableAEX> <ReservedMemMaxSize>0x2000000</ReservedMemMaxSize> </EnclaveConfiguration> ``` #### 5. 构建配置文件 (CMakeLists.txt) ```cmake cmake_minimum_required(VERSION 3.10) project(tee_data_sharing_enclave) set(CMAKE_C_STANDARD 11) # 查找SGX SDK find_package(SGX REQUIRED) # Enclave源文件 set(ENCLAVE_SOURCES isolation_functions.c key_management.c remote_attestation.c ) # 生成Enclave目标 add_enclave_library(enclave STATIC ${ENCLAVE_SOURCES}) # 配置Enclave属性 sgx_target_enclave_properties(enclave CONFIG_FILE enclave.config.xml) # 安装目标 install(TARGETS enclave ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin ) # 签名密钥(开发模式) configure_file(${PROJECT_SOURCE_DIR}/enclave_private.pem ${PROJECT_BINARY_DIR}/enclave_private.pem COPYONLY) ``` ### 关键模块功能说明 1. **密钥管理模块**: - 安全生成密封AES-256对称密钥 - 基于密封密钥实现AES-GCM加密 - 生成ECDSA会话密钥对用于安全通信 - 使用SGX硬件提供的真随机数生成器 2. **远程认证模块**: - 创建Enclave身份报告用于证明代码完整性 - 实现简化的远程认证流程(实际部署应使用EPID/DCAP) - 验证报告签名Enclave属性 - 确保执行环境可信且未被篡改 3. **安全隔离模块**: - 实现受保护的内存管理(自动擦除敏感数据) - 使用SGX受保护文件系统持久化敏感数据 - 在硬件隔离环境中执行敏感计算 - 预防侧信道攻击的内存管理技术 4. **构建与配置**: - 通过CMake集成SGX SDK工具链 - 配置Enclave内存布局安全策略 - 支持开发模式生产模式切换 - 简化构建过程以适配本科毕设要求 ### 使用方法 1. **环境准备**: ```bash # 在OpenSGX环境中设置开发环境 source /opt/sgxsdk/environment mkdir build && cd build cmake .. ``` 2. **编译签名**: ```bash make sgx_sign sign -key enclave_private.pem -enclave enclave.so -out enclave.signed.so ``` 3. **测试核心功能**: ```c // 示例测试代码(应位于untrusted_src) #include "sgx_urts.h" int main() { sgx_enclave_id_t eid; sgx_status_t ret = sgx_create_enclave("enclave.signed.so", SGX_DEBUG_FLAG, NULL, NULL, &eid, NULL); uint8_t sealed_key[512]; ret = ecall_generate_keys(eid, sealed_key, sizeof(sealed_key)); uint8_t data[64] = "Sensitive data"; uint8_t encrypted[128]; ret = ecall_encrypt_with_sealed_key(eid, sealed_key, data, strlen(data), encrypted); sgx_destroy_enclave(eid); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值