TeeApps contain a general framework for developing TEE applications and various application implementations used in federated AI/BI.
Features
TeeApps support different tee platforms: Intel SGX2, Intel TDX and Hygon Csv. It will be remote attested by Capsule Manager who holds the data keys corresponding to encrypted inputs. We also support simulation mode for users who do not have these tee environments.
TeeApps use secretflow component spec to define inputs, outputs and other attributes.
Quick Start
Prepare dataset
Before running TeeApps, you should use Capsule Manager SDK to generate data keys, encrypt datasets and then register data keys and data policies to Capsule Manager.
Here we will use the open-source breast cancer dataset as an example. The dataset is provided by the University of California, Irvine (UCI). It contains 569 samples. Each sample has an ID and 10 features, making it a typical binary classification dataset.
We have performed vertical partitioning on this dataset: Institution alice has the first 5 features, while Institution bob has the last 5 features and the label column. (alice.csv and bob.csv.)
The simplest way to run TeeApps is to use official docker image.
We provide two images, one for simulation mode and one for production mode. You can run TeeApps on non-SGX machines in simulation mode, while production mode requires a SGX2 machine.
We suppose you have prepared dataset and got encrypted files: alice.csv.enc, bob.csv.enc.
Simulation Mode
Pull and run simulation docker
```sh
docker pull secretflow/teeapps-sim-ubuntu22.04:latest
docker run -it –name teeapps-sim –network=host secretflow/teeapps-sim-ubuntu22.04:latest bash
4. Run PSI
```sh
/home/teeapp/sim/teeapps/main --plat=sim --enable_console_logger=true --enable_capsule_tls=false --entry_task_config_path=/host/integration_test/psi_task.json
Check outputs
Default log path is /host/log/app.log
The output of PSI is a encrypted table. You can skip this step and run other applications with encrypted outputs.
The output path is set in psi.json (/host/testdata/breast_cancer/join_table in this example). You can get data keys and decrypt join_table with Capsule Manager SDK. The decryption result will be a table like following:
// To accept insecure HTTPS certificate, set this option to false
“use_secure_cert”: true,
// You can use the Intel PCS or another PCCS to get quote verification collateral. Retrieval of PCK
// Certificates will always use the PCCS described in PCCS_URL. When COLLATERAL_SERVICE is not defined, both
// PCK Certs and verification collateral will be retrieved using PCCS_URL
//“collateral_service”: “https://api.trustedservices.intel.com/sgx/certification/v3/",
// If you use a PCCS service to get the quote verification collateral, you can specify which PCCS API version is to be used.
// The legacy 3.0 API will return CRLs in HEX encoded DER format and the sgx_ql_qve_collateral_t.version will be set to 3.0, while
// the new 3.1 API will return raw DER format and the sgx_ql_qve_collateral_t.version will be set to 3.1. The PCCS_API_VERSION
// setting is ignored if COLLATERAL_SERVICE is set to the Intel PCS. In this case, the PCCS_API_VERSION is forced to be 3.1
// internally. Currently, only values of 3.0 and 3.1 are valid. Note, if you set this to 3.1, the PCCS use to retrieve
// verification collateral must support the new 3.1 APIs.
//“pccs_api_version”: “3.1”,
// Maximum retry times for QCNL. If RETRY is not defined or set to 0, no retry will be performed.
// It will first wait one second and then for all forthcoming retries it will double the waiting time.
// By using RETRY_DELAY you disable this exponential backoff algorithm
“retry_times”: 6,
// Sleep this amount of seconds before each retry when a transfer has failed with a transient error
“retry_delay”: 10,
// If LOCAL_PCK_URL is defined, the QCNL will try to retrieve PCK cert chain from LOCAL_PCK_URL first,
// and failover to PCCS_URL as in legacy mode.
//“local_pck_url”: “http://localhost:8081/sgx/certification/v3/",
// If LOCAL_PCK_URL is not defined, the QCNL will cache PCK certificates in memory by default.
// The cached PCK certificates will expire after PCK_CACHE_EXPIRE_HOURS hours.
“pck_cache_expire_hours”: 168
// You can add custom request headers and parameters to the get certificate API.
// But the default PCCS implementation just ignores them.
//,”custom_request_options” : {
// “get_cert” : {
// “headers”: {
// “head1”: “value1”
// },
// “params”: {
// “param1”: “value1”,
// “param2”: “value2”
// }
// }
//}
}
Copy /etc/sgx_default_qcnl.conf to occlum instance image
3. Build
You need a pair of asymmetric key to sign TeeApps in production mode. You can generate use openssl if you do not have.
```sh
openssl genrsa -3 -out private_key.pem 3072
openssl rsa -in private_key.pem -pubout -out public_key.pem
Build occlum with your private key.
cd /home/teeapp/occlum/occlum_instance
occlum build -f --sign-key /path/to/private_key.pem
Copy encrypted file into docker (on host machine)
```sh
docker cp alice.csv.enc teeapps-sgx:/home/teeapp/occlum/occlum_instance/testdata/breast_cancer/
Run PSI
```sh
cd /home/teeapp/occlum/occlum_instance
occlum run /bin/main –plat=sgx –enable_console_logger=true –enable_capsule_tls=false –entry_task_config_path=/host/integration_test/psi_task.json
7. Check PSI output or run other applications
Default log path is /home/teeapp/occlum/occlum_instance/log/app.log
You can get data keys and decrypt /home/teeapp/occlum/occlum_instance/testdata/breast_cancer/join_table with [Capsule Manager SDK](https://github.com/secretflow/capsule-manager-sdk).
You can also modify task configs or write a new task config by yourself to run other applications with encrypted join_table.
#### TDX Mode
1. Pull and run tdx docker image in a Intel trusted domain(TD VM)
```sh
docker pull secretflow/teeapps-tdx-ubuntu22.04:latest
docker run -it --name teeapps-tdx --network=host -v /dev/tdx_guest:/dev/tdx_guest --privileged=true secretflow/teeapps-tdx-ubuntu22.04:latest bash
Modify PCCS config
Set real PCCS URL and set use_secure_cert to false in /etc/sgx_default_qcnl.conf. This step is the same as in SGX mode.
4. Run PSI
```sh
/home/teeapp/csv/teeapps/main --plat=csv --enable_console_logger=true --enable_capsule_tls=false --entry_task_config_path=/host/integration_test/psi_task.json
Check outputs or run other applications
Same as in simulation mode
Build By Source code
Enter dev docker container
# create a dev docker container
bash env.sh
# enter the dev docker container
bash env.sh enter
Simulation Mode
bash scripts/build_sim.sh
SGX Mode(Occlum Mode)
bash scripts/build_occlum.sh
TDX Mode
bash scripts/build_tdx.sh
CSV Mode
bash scripts/build_csv.sh
Support mTLS
To enable mTLS between TeeApps and Capsule Manager, you should firstly deploy a CA certification, a client certification and a client private key in following path. And then replace capsule-manager endpoint’s ip with a domain name. Finally, enable tls in start command.
TeeApps
TeeApps contain a general framework for developing TEE applications and various application implementations used in federated AI/BI.
Features
Quick Start
Prepare dataset
Before running TeeApps, you should use Capsule Manager SDK to generate data keys, encrypt datasets and then register data keys and data policies to Capsule Manager.
Here we will use the open-source breast cancer dataset as an example. The dataset is provided by the University of California, Irvine (UCI). It contains 569 samples. Each sample has an ID and 10 features, making it a typical binary classification dataset.
We have performed vertical partitioning on this dataset: Institution alice has the first 5 features, while Institution bob has the last 5 features and the label column. (alice.csv and bob.csv.)
Run by docker image
The simplest way to run TeeApps is to use official docker image.
We provide two images, one for simulation mode and one for production mode. You can run TeeApps on non-SGX machines in simulation mode, while production mode requires a SGX2 machine.
We suppose you have prepared dataset and got encrypted files: alice.csv.enc, bob.csv.enc.
Simulation Mode
docker run -it –name teeapps-sim –network=host secretflow/teeapps-sim-ubuntu22.04:latest bash
docker cp carol.crt teeapps-sim:/host/integration_test/
docker exec -it teeapps-sim bash
cd /host/integration_test
pip install -r requirements.txt
please replace params
python3 convert.py –cert_path carol.crt –prikey_path carol.key –task_config_path psi.json –scope default –capsule_manager_endpoint 127.0.0.1:8888 –tee_task_config_path psi_task.json
Default log path is /host/log/app.log
The output of PSI is a encrypted table. You can skip this step and run other applications with encrypted outputs.
The output path is set in psi.json (/host/testdata/breast_cancer/join_table in this example). You can get data keys and decrypt join_table with Capsule Manager SDK. The decryption result will be a table like following:
You can modify task configs or write a new task config by yourself to run other applications. For example, you can split dataset.
SGX Mode(Occlum Mode)
docker run -it –name teeapps-sgx –network=host -v /dev/sgx_enclave:/dev/sgx/enclave -v /dev/sgx_provision:/dev/sgx/provision –privileged=true secretflow/teeapps-sgx-ubuntu22.04:latest bash
{ // *** ATTENTION : This file is in JSON format so the keys are case sensitive. Don’t change them.
//PCCS server address “pccs_url”: “https://localhost:8081/sgx/certification/v3/",
// To accept insecure HTTPS certificate, set this option to false “use_secure_cert”: true,
// You can use the Intel PCS or another PCCS to get quote verification collateral. Retrieval of PCK // Certificates will always use the PCCS described in PCCS_URL. When COLLATERAL_SERVICE is not defined, both // PCK Certs and verification collateral will be retrieved using PCCS_URL //“collateral_service”: “https://api.trustedservices.intel.com/sgx/certification/v3/",
// If you use a PCCS service to get the quote verification collateral, you can specify which PCCS API version is to be used. // The legacy 3.0 API will return CRLs in HEX encoded DER format and the sgx_ql_qve_collateral_t.version will be set to 3.0, while // the new 3.1 API will return raw DER format and the sgx_ql_qve_collateral_t.version will be set to 3.1. The PCCS_API_VERSION // setting is ignored if COLLATERAL_SERVICE is set to the Intel PCS. In this case, the PCCS_API_VERSION is forced to be 3.1 // internally. Currently, only values of 3.0 and 3.1 are valid. Note, if you set this to 3.1, the PCCS use to retrieve // verification collateral must support the new 3.1 APIs. //“pccs_api_version”: “3.1”,
// Maximum retry times for QCNL. If RETRY is not defined or set to 0, no retry will be performed. // It will first wait one second and then for all forthcoming retries it will double the waiting time. // By using RETRY_DELAY you disable this exponential backoff algorithm “retry_times”: 6,
// Sleep this amount of seconds before each retry when a transfer has failed with a transient error “retry_delay”: 10,
// If LOCAL_PCK_URL is defined, the QCNL will try to retrieve PCK cert chain from LOCAL_PCK_URL first, // and failover to PCCS_URL as in legacy mode. //“local_pck_url”: “http://localhost:8081/sgx/certification/v3/",
// If LOCAL_PCK_URL is not defined, the QCNL will cache PCK certificates in memory by default. // The cached PCK certificates will expire after PCK_CACHE_EXPIRE_HOURS hours. “pck_cache_expire_hours”: 168
// You can add custom request headers and parameters to the get certificate API. // But the default PCCS implementation just ignores them. //,”custom_request_options” : { // “get_cert” : { // “headers”: { // “head1”: “value1” // }, // “params”: { // “param1”: “value1”, // “param2”: “value2” // } // } //} }
cp /etc/sgx_default_qcnl.conf /home/teeapp/occlum/occlum_instance/image/etc/
Build occlum with your private key.
docker cp bob.csv.enc teeapps-sgx:/home/teeapp/occlum/occlum_instance/testdata/breast_cancer/
occlum run /bin/main –plat=sgx –enable_console_logger=true –enable_capsule_tls=false –entry_task_config_path=/host/integration_test/psi_task.json
Modify PCCS config Set real PCCS URL and set use_secure_cert to false in /etc/sgx_default_qcnl.conf. This step is the same as in SGX mode.
Copy encrypted file into docker (from host machine) ```sh docker cp alice.csv.enc teeapps-tdx:/host/testdata/breast_cancer/
docker cp bob.csv.enc teeapps-tdx:/host/testdata/breast_cancer/
Run PSI
Check outputs or run other applications Same as in simulation mode
CSV Mode
Download https://gitee.com/anolis/hygon-devkit/blob/master/csv/attestation/csv-guest.c and compile with following Makefile:
Install this module
docker run -it –name teeapps-csv –network=host -v /dev/csv-guest:/dev/csv-guest –privileged=true secretflow/teeapps-csv-ubuntu22.04:latest bash
docker cp carol.crt teeapps-csv:/host/integration_test/
docker exec -it teeapps-csv bash
cd /host/integration_test
pip install -r requirements.txt
please replace params
python3 convert.py –cert_path carol.crt –prikey_path carol.key –task_config_path psi.json –scope default –capsule_manager_endpoint 127.0.0.1:8888 –tee_task_config_path psi_task.json
Build By Source code
Enter dev docker container
Simulation Mode
SGX Mode(Occlum Mode)
TDX Mode
CSV Mode
Support mTLS
To enable mTLS between TeeApps and Capsule Manager, you should firstly deploy a CA certification, a client certification and a client private key in following path. And then replace capsule-manager endpoint’s ip with a domain name. Finally, enable tls in start command.
You may need to add a record in /etc/hosts like:
Simulation Mode
SGX Mode (Occlum Mode)
TDX Mode
CSV Mode