Skip to content

Commit 295f6dd

Browse files
committed
[PAL,LibOS,CI-Examples] Reflect corrections/updates mabe by reviewers
1 parent 6bbdd0d commit 295f6dd

29 files changed

+1856
-138
lines changed

CI-Examples/python/README.md

-21
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@ Run `make SGX=1` (non-debug) or `make SGX=1 DEBUG=1` (debug) in the directory.
2323
The `scripts/sgx-quote.py` script depends on working DCAP attestation, so first
2424
make sure you have a working DCAP setup.
2525

26-
## Using the KSS Feature
27-
When KSS is enabled, you can set four values—ISV Family ID, ISV Extended Prod ID, Config ID, and Config SVN—and include them in the REPORT structure.
28-
29-
To enable KSS, modify the following section in python.manifest.template:
30-
```
31-
sgx.kss = true # Change to "false" to disable KSS
32-
```
33-
34-
To set the ISV Family ID and ISV Extended Prod ID, modify the following lines:
35-
```
36-
sgx.isvfamilyid = "0x00112233445566778899aabbccddeeff"
37-
sgx.isvextprodid = "0xcafef00dcafef00df00dcafef00dcafe"
38-
```
39-
40-
To set the Config ID and Config SVN, configure the following environment variables on the host OS before executing scripts:
41-
```
42-
export SGX_CONFIG_ID=DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678DEADBEEF12345678
43-
export SGX_CONFIG_SVN=ABCD
44-
```
45-
You can check these values by running `sgx-report.py` or `sgx-quote.py`, as described in the next section.
46-
4726
# Run Python with Gramine
4827

4928
Here's an example of running Python scripts under Gramine:

CI-Examples/python/python.manifest.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ loader.env.OMP_NUM_THREADS = "4"
1616

1717
loader.insecure__use_cmdline_argv = true
1818

19-
sys.enable_sigterm_injection = false
19+
sys.enable_sigterm_injection = true
2020

2121
fs.mounts = [
2222
{ path = "/lib", uri = "file:{{ gramine.runtimedir() }}" },
@@ -34,7 +34,7 @@ fs.mounts = [
3434
sys.stack.size = "2M"
3535
sys.enable_extra_runtime_domain_names_conf = true
3636

37-
sgx.debug = false
37+
sgx.debug = true
3838
sgx.edmm_enable = {{ 'true' if env.get('EDMM', '0') == '1' else 'false' }}
3939
sgx.enclave_size = "1G"
4040
sgx.max_threads = {{ '1' if env.get('EDMM', '0') == '1' else '32' }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*.tar.gz
2+
/OUTPUT
3+
/client
4+
/mbedtls
5+
/server
6+
/ssl/ca.*
7+
/ssl/server.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (C) 2023 Gramine contributors
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
ARCH_LIBDIR ?= /lib/$(shell $(CC) -dumpmachine)
5+
6+
ifeq ($(DEBUG),1)
7+
GRAMINE_LOG_LEVEL = debug
8+
CFLAGS += -O0 -ggdb3
9+
else
10+
GRAMINE_LOG_LEVEL = error
11+
CFLAGS += -O2
12+
endif
13+
14+
CFLAGS += -fPIE
15+
LDFLAGS += -pie
16+
17+
.PHONY: all
18+
all: app dcap
19+
20+
.PHONY: app
21+
app: ssl/server.crt server.manifest.sgx server.sig client
22+
23+
.PHONY: dcap
24+
dcap: client_dcap.manifest.sgx client_dcap.sig
25+
26+
############################# SSL DATA DEPENDENCY #############################
27+
28+
# SSL data: key and x.509 self-signed certificate
29+
ssl/server.crt: ssl/ca_config.conf
30+
openssl genrsa -out ssl/ca.key 2048
31+
openssl req -x509 -new -nodes -key ssl/ca.key -sha256 -days 1024 -out ssl/ca.crt -config ssl/ca_config.conf
32+
openssl genrsa -out ssl/server.key 2048
33+
openssl req -new -key ssl/server.key -out ssl/server.csr -config ssl/ca_config.conf
34+
openssl x509 -req -days 360 -in ssl/server.csr -CA ssl/ca.crt -CAkey ssl/ca.key -CAcreateserial -out ssl/server.crt
35+
36+
######################### CLIENT/SERVER EXECUTABLES ###########################
37+
38+
CFLAGS += $(shell pkg-config --cflags mbedtls_gramine) \
39+
$(shell pkg-config --cflags ra_tls_gramine)
40+
41+
# no need for `pkg-config --libs ra_tls_gramine` because programs use dlopen
42+
LDFLAGS += -ldl -Wl,--enable-new-dtags $(shell pkg-config --libs mbedtls_gramine)
43+
44+
server: src/server.c
45+
$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@
46+
47+
client: src/client.c
48+
$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@
49+
50+
############################### SERVER MANIFEST ###############################
51+
52+
server.manifest: server.manifest.template server
53+
gramine-manifest \
54+
-Dlog_level=$(GRAMINE_LOG_LEVEL) \
55+
-Darch_libdir=$(ARCH_LIBDIR) \
56+
$< > $@
57+
58+
server.manifest.sgx server.sig &: server.manifest
59+
gramine-sgx-sign \
60+
--manifest $< \
61+
--output $<.sgx
62+
63+
########################### CLIENT (DCAP) MANIFEST ############################
64+
65+
client_dcap.manifest: client.manifest.template client
66+
gramine-manifest \
67+
-Dlog_level=$(GRAMINE_LOG_LEVEL) \
68+
-Darch_libdir=$(ARCH_LIBDIR) \
69+
$< >$@
70+
71+
client_dcap.manifest.sgx client_dcap.sig: sgx_sign_client_dcap
72+
@:
73+
74+
.INTERMEDIATE: sgx_sign_client_dcap
75+
sgx_sign_client_dcap: client_dcap.manifest
76+
gramine-sgx-sign \
77+
--manifest $< \
78+
--output $<.sgx
79+
80+
############################### SGX CHECKS FOR CI #############################
81+
82+
.PHONY: check_dcap
83+
check_dcap: app dcap
84+
gramine-sgx server >/dev/null & SERVER_ID=$$!; \
85+
../../scripts/wait_for_server 60 127.0.0.1 4433; \
86+
./client dcap | tee OUTPUT; \
87+
./client dcap 0 0 0 0 | tee -a OUTPUT; \
88+
kill -9 $$SERVER_ID
89+
@grep -q "using default SGX-measurement verification callback" OUTPUT && echo "[ Success 1/4 ]"
90+
@grep -q "using our own SGX-measurement verification callback" OUTPUT && echo "[ Success 2/4 ]"
91+
@grep -q "Verifying peer X.509 certificate... ok" OUTPUT && echo "[ Success 3/4 ]"
92+
@(exit `grep -c "failed" "OUTPUT"`) && echo "[ Success 4/4 ]"
93+
@rm OUTPUT
94+
95+
.PHONY: check_dcap_fail
96+
check_dcap_fail: app dcap
97+
gramine-sgx server --test-malicious-quote >/dev/null & SERVER_ID=$$!; \
98+
../../scripts/wait_for_server 60 127.0.0.1 4433; \
99+
./client dcap && exit 1 || echo "[ Success 1/1 ]"; \
100+
kill -9 $$SERVER_ID
101+
102+
################################## CLEANUP ####################################
103+
104+
.PHONY: clean
105+
clean:
106+
$(RM) -r \
107+
*.sig *.manifest.sgx *.manifest server client *.so *.so.* OUTPUT
108+
109+
.PHONY: distclean
110+
distclean: clean
111+
$(RM) -r ssl/ca.* ssl/server.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# RA-TLS Minimal Example
2+
3+
This directory contains the Makefile, the template server manifest, and the
4+
minimal server and client written against the mbedTLS library.
5+
6+
The server and client are based on `ssl_server.c` and `ssl_client1.c` example
7+
programs shipped with mbedTLS. We modified them to allow using RA-TLS flows. In
8+
particular, the server uses a self-signed RA-TLS cert with the SGX quote
9+
embedded in it via `ra_tls_create_key_and_crt_der()`. The client uses an RA-TLS
10+
verification callback to verify the server RA-TLS certificate via
11+
`ra_tls_verify_callback_der()`.
12+
13+
This example uses the RA-TLS libraries `ra_tls_attest.so` for server and
14+
`ra_tls_verify_dcap.so` for client. These libraries are installed together with
15+
Gramine (you need `meson setup ... -Ddcap=enabled`, which is the default). The
16+
DCAP software infrastructure must be installed and work correctly on the host.
17+
18+
For more documentation about ECDSA (DCAP) remote attestation scheme, refer to
19+
https://gramine.readthedocs.io/en/stable/attestation.html.
20+
21+
## RA-TLS server
22+
23+
The server is supposed to run in the SGX enclave with Gramine and RA-TLS
24+
dlopen-loaded. If the server is started not in the SGX enclave, then it falls
25+
back to using normal X.509 PKI flows.
26+
27+
If server is run with a command-line argument ``--test-malicious-quote``, then
28+
the server will maliciously modify the SGX quote before sending to the client.
29+
This is useful for testing purposes.
30+
31+
## RA-TLS client
32+
33+
The client is supposed to run on a trusted machine (*not* in an SGX enclave).
34+
If RA-TLS library `ra_tls_verify_dcap.so` is not requested by user via `dcap`
35+
command-line argument respectively, the client falls back to using normal X.509
36+
PKI flows (specified as `native` command-line argument).
37+
38+
It is also possible to run the client in an SGX enclave. This will create a
39+
secure channel between two Gramine SGX processes, possibly running on different
40+
machines. It can be used as an example of in-enclave remote attestation and
41+
verification.
42+
43+
If client is run without additional command-line arguments, it uses default
44+
RA-TLS verification callback that compares `MRENCLAVE`, `MRSIGNER`,
45+
`ISV_PROD_ID` and `ISV_SVN` against the corresonding `RA_TLS_*` environment
46+
variables. `MRENCLAVE`, `MRSIGNER` and `ISV_PROD_ID` are expected to match
47+
`RA_TLS_*` ones. `ISV_SVN` is expected to be equal or greater than `RA_TLS_ISV_SVN`.
48+
To run the client with its own verification callback, execute it with four
49+
additional command-line arguments (see the source code for details).
50+
51+
Also, because this example builds and uses debug SGX enclaves (`sgx.debug` is
52+
set to `true`), we use environment variable `RA_TLS_ALLOW_DEBUG_ENCLAVE_INSECURE=1`.
53+
Note that in production environments, you must *not* use this option!
54+
55+
Moreover, we set `RA_TLS_ALLOW_OUTDATED_TCB_INSECURE=1`,
56+
`RA_TLS_ALLOW_HW_CONFIG_NEEDED=1` and `RA_TLS_ALLOW_SW_HARDENING_NEEDED=1` to
57+
allow performing the tests when some of Intel's security advisories haven't been
58+
addressed (for example, when the microcode or architectural enclaves aren't
59+
fully up-to-date). Note that in production environments, you must carefully
60+
analyze whether to use these options!
61+
62+
## KSS features
63+
In addition to the existing ra-tls example, this example shows the use of the following four values
64+
provided by the Key Separation and Sharing (KSS) feature of SGX:
65+
* ISV Extended Production ID (ISV_EXT_PROD_ID)
66+
* ISV Family ID (ISV_FAMILY_ID)
67+
* Config ID (CONFIG_ID)
68+
* Config SVN (CONFIG_SVN)
69+
70+
The KSS enable, ISV_EXT_PROD_ID and ISV_FAMILY_ID of the server enclave are specified
71+
in the manifest (`sgx.kss`, `sgx.isvextprodid` and `sgx.isvfamilyid`),
72+
while the CONFIG_ID and CONFIG_SVN are specified via command line arguments or
73+
environment variables when executing the `gramine-sgx` command.
74+
75+
These values cannot be used when running under non-SGX conditions.
76+
77+
# Quick Start
78+
79+
In most of the examples below, you need to tell the client what values it should
80+
expect for `MRENCLAVE`, `MRSIGNER`, `ISV_PROD_ID` and `ISV_SVN`. One way to
81+
obtain them is to run `gramine-sgx-sigstruct-view server.sig`.
82+
83+
For all examples, we set the following environment variables:
84+
```sh
85+
export RA_TLS_ALLOW_DEBUG_ENCLAVE_INSECURE=1
86+
export RA_TLS_ALLOW_OUTDATED_TCB_INSECURE=1
87+
export RA_TLS_ALLOW_HW_CONFIG_NEEDED=1
88+
export RA_TLS_ALLOW_SW_HARDENING_NEEDED=1
89+
```
90+
91+
- Normal non-RA-TLS flows; without SGX and without Gramine:
92+
93+
```sh
94+
make app
95+
./server &
96+
./client native
97+
# client will successfully connect to the server via normal x.509 PKI flows
98+
kill %%
99+
```
100+
101+
- RA-TLS flows with SGX and with Gramine, ECDSA-based (DCAP) attestation:
102+
103+
```sh
104+
make clean
105+
make app dcap
106+
107+
gramine-sgx --config-id=<CONFIG_ID to set to the server enclave> \
108+
--config-svn=<CONFIG_SVN to set to the server enclave> \
109+
./server &
110+
111+
RA_TLS_MRENCLAVE=<MRENCLAVE of the server enclave> \
112+
RA_TLS_MRSIGNER=<MRSIGNER of the server enclave> \
113+
RA_TLS_ISV_PROD_ID=<ISV_PROD_ID of the server enclave> \
114+
RA_TLS_ISV_SVN=<ISV_SVN of the server enclave> \
115+
RA_TLS_ISV_EXT_PROD_ID=<ISV_EXT_PROD_ID of the server enclave> \
116+
RA_TLS_ISV_FAMILY_ID=<ISV_FAMILY_ID of the server enclave> \
117+
RA_TLS_CONFIG_ID=<CONFIG_ID of the server enclave> \
118+
RA_TLS_CONFIG_SVN=<CONFIG_SVN of the server enclave> \
119+
./client dcap
120+
121+
# client will successfully connect to the server via RA-TLS/DCAP flows
122+
kill %%
123+
```
124+
125+
The environment variables SGX_CONFIG_ID and SGX_CONFIG_SVN can also be used to specify
126+
the Config ID and Config SVN.
127+
If both command line arguments and environment variables are specified,
128+
an error is returned if those values do not match.
129+
130+
``` sh
131+
SGX_CONFIG_ID=<CONFIG_ID to set to the server enclave> \
132+
SGX_CONFIG_SVN=<CONFIG_SVN to set to the server enclave> \
133+
gramine-sgx ./server
134+
```
135+
136+
- RA-TLS flows with SGX and with Gramine, client with its own verification callback:
137+
138+
```sh
139+
make clean
140+
make app dcap
141+
142+
gramine-sgx --config-id=<CONFIG_ID to set to the server enclave> \
143+
--config-svn=<CONFIG_SVN to set to the server enclave> \
144+
./server &
145+
146+
./client dcap \
147+
<MRENCLAVE of the server enclave> \
148+
<MRSIGNER of the server enclave> \
149+
<ISV_PROD_ID of the server enclave> \
150+
<ISV_SVN of the server enclave> \
151+
<ISV_EXT_PROD_ID of the server enclave> \
152+
<ISV_FAMILY_ID of the server enclave> \
153+
<CONFIG_ID of the server enclave> \
154+
<CONFIG_SVN of the server enclave>
155+
156+
# client will successfully connect to the server via RA-TLS/DCAP flows
157+
kill %%
158+
```
159+
160+
Note that the Config ID must be specified as a hexadecimal string and the Config SVN as a decimal number.
161+
162+
- RA-TLS flows with SGX and with Gramine, server sends malicious SGX quote:
163+
164+
```sh
165+
make clean
166+
make app dcap
167+
168+
gramine-sgx ./server --test-malicious-quote &
169+
./client dcap
170+
171+
# client will fail to verify the malicious SGX quote and will *not* connect to the server
172+
kill %%
173+
```
174+
175+
- RA-TLS flows with SGX and with Gramine, running DCAP client in SGX:
176+
177+
```sh
178+
make clean
179+
make app dcap
180+
181+
gramine-sgx --config-id=<CONFIG_ID to set to the server enclave> \
182+
--config-svn=<CONFIG_SVN to set to the server enclave> \
183+
./server &
184+
185+
gramine-sgx ./client_dcap dcap \
186+
<MRENCLAVE of the server enclave> \
187+
<MRSIGNER of the server enclave> \
188+
<ISV_PROD_ID of the server enclave> \
189+
<ISV_SVN of the server enclave> \
190+
<ISV_EXT_PROD_ID of the server enclave> \
191+
<ISV_FAMILY_ID of the server enclave> \
192+
<CONFIG_ID of the server enclave> \
193+
<CONFIG_SVN of the server enclave>
194+
195+
# client will successfully connect to the server via RA-TLS/DCAP flows
196+
kill %%
197+
```

0 commit comments

Comments
 (0)