11{ stdenv
22, lib
33, fetchurl
4+ , fetchFromGitHub
45, cmake
56, coreutils
67, curl
78, file
9+ , git
810, makeWrapper
911, nixosTests
1012, protobuf
1113, python3
12- , sgx-sdk
14+ , ocaml
15+ , ocamlPackages
1316, which
1417, debug ? false
1518} :
1619stdenv . mkDerivation rec {
17- inherit ( sgx-sdk ) patches src version versionTag ;
1820 pname = "sgx-psw" ;
21+ # Version as given in se_version.h
22+ version = "2.25.100.3" ;
23+ # Version as used in the Git tag
24+ versionTag = "2.25" ;
1925
26+ src = fetchFromGitHub {
27+ owner = "intel" ;
28+ repo = "linux-sgx" ;
29+ rev = "sgx_${ versionTag } " ;
30+ hash = "sha256-RR+vFTd9ZM6XUn3KgQeUM+xoj1Ava4zQzFYA/nfXyaw=" ;
31+ fetchSubmodules = true ;
32+ } ;
33+
34+ # Extract Intel-provided, pre-built enclaves and libs.
2035 postUnpack =
2136 let
2237 # Fetch the pre-built, Intel-signed Architectural Enclaves (AE). They help
2338 # run user application enclaves, verify launch policies, produce remote
2439 # attestation quotes, and do platform certification.
2540 ae . prebuilt = fetchurl {
2641 url = "https://download.01.org/intel-sgx/sgx-linux/${ versionTag } /prebuilt_ae_${ versionTag } .tar.gz" ;
27- hash = "sha256-IGV9VEwY/cQBV4Vz2sps4JgRweWRl/l08ocb9P4SH8Q=" ;
42+ hash = "sha256-Hlh96rYOyml2y50d8ASKz6U97Fl0hbGYECeZiG9nMSQ=" ;
43+ } ;
44+
45+ # Pre-built ipp-crypto with mitigations.
46+ optlib . prebuilt = fetchurl {
47+ url = "https://download.01.org/intel-sgx/sgx-linux/${ versionTag } /optimized_libs_${ versionTag } .tar.gz" ;
48+ hash = "sha256-7mDTaLtpOQLHQ6Fv+FWJ2k/veJZPXIcuj7kOdRtRqhg=" ;
2849 } ;
29- # Also include the Data Center Attestation Primitives (DCAP) platform
30- # enclaves.
50+
51+ # Fetch the Data Center Attestation Primitives (DCAP) platform enclaves
52+ # and pre-built sgxssl.
3153 dcap = rec {
32- version = "1.21 " ;
54+ version = "1.22 " ;
3355 filename = "prebuilt_dcap_${ version } .tar.gz" ;
3456 prebuilt = fetchurl {
3557 url = "https://download.01.org/intel-sgx/sgx-dcap/${ version } /linux/${ filename } " ;
36- hash = "sha256-/PPD2MyNxoCwzNljIFcpkFvItXbyvymsJ7+Uf4IyZuk =" ;
58+ hash = "sha256-RTpJQ6epoAN8YQXSJUjJQ5mPaQIiQpStTWFsnspjjDQ =" ;
3759 } ;
3860 } ;
3961 in
40- sgx-sdk . postUnpack + ''
62+ ''
63+ # Make sure this is the right version of linux-sgx
64+ grep -q '"${ version } "' "$src/common/inc/internal/se_version.h" \
65+ || (echo "Could not find expected version ${ version } in linux-sgx source" >&2 && exit 1)
66+
67+ tar -xzvf ${ ae . prebuilt } -C $sourceRoot/
68+ tar -xzvf ${ optlib . prebuilt } -C $sourceRoot/
69+
4170 # Make sure we use the correct version of prebuilt DCAP
4271 grep -q 'ae_file_name=${ dcap . filename } ' "$src/external/dcap_source/QuoteGeneration/download_prebuilt.sh" \
4372 || (echo "Could not find expected prebuilt DCAP ${ dcap . filename } in linux-sgx source" >&2 && exit 1)
4473
45- tar -zxf ${ ae . prebuilt } -C $sourceRoot/
46- tar -zxf ${ dcap . prebuilt } -C $sourceRoot/external/dcap_source/QuoteGeneration/
74+ tar -xzvf ${ dcap . prebuilt } -C $sourceRoot/external/dcap_source ./prebuilt/
75+ tar -xzvf ${ dcap . prebuilt } -C $sourceRoot/external/dcap_source/QuoteGeneration ./psw/
76+ '' ;
77+
78+ patches = [
79+ # There's a `make preparation` step that downloads some prebuilt binaries
80+ # and applies some patches to the in-repo git submodules. This patch removes
81+ # the parts that download things, since we can't do that inside the sandbox.
82+ ./disable-downloads.patch
83+
84+ # This patch disables mtime in bundled zip file for reproducible builds.
85+ #
86+ # Context: The `aesm_service` binary depends on a vendored library called
87+ # `CppMicroServices`. At build time, this lib creates and then bundles
88+ # service resources into a zip file and then embeds this zip into the
89+ # binary. Without changes, the `aesm_service` will be different after every
90+ # build because the embedded zip file contents have different modified times.
91+ ./cppmicroservices-no-mtime.patch
92+ ] ;
93+
94+ postPatch =
95+ let
96+ # The base directories we want to copy headers from. The exact headers are
97+ # parsed from <linux/installer/common/sdk/BOMs/sdk_base.txt>
98+ bomDirsToCopyFrom = builtins . concatStringsSep "|" [
99+ "common/"
100+ "external/dcap_source/"
101+ "external/ippcp_internal/"
102+ "external/sgx-emm/"
103+ "psw/"
104+ "sdk/tlibcxx/"
105+ ] ;
106+ in
107+ ''
108+ patchShebangs \
109+ external/sgx-emm/create_symlink.sh \
110+ linux/installer/bin/build-installpkg.sh \
111+ linux/installer/common/psw/createTarball.sh \
112+ linux/installer/common/psw/install.sh
113+
114+ # Run sgx-sdk preparation step
115+ make preparation
116+
117+ # Build a fake SGX_SDK directory. Normally sgx-psw depends on first building
118+ # all of sgx-sdk, however we can actually build them independently by just
119+ # copying a few header files and building `sgx_edger8r` separately.
120+ mkdir .sgxsdk
121+ export SGX_SDK="$(readlink -f .sgxsdk)"
122+
123+ # Parse the BOM for the headers we need, then copy them into SGX_SDK
124+ # Each line in the BOM.txt looks like:
125+ # <deliverydir>/...\t<installdir>/package/...\t....
126+ # TODO(phlip9): hardlink?
127+ sed -n -r 's:^<deliverydir>/(${ bomDirsToCopyFrom } )(\S+)\s<installdir>/package/(\S+)\s.*$:\1\2\n.sgxsdk/\3:p' \
128+ < linux/installer/common/sdk/BOMs/sdk_base.txt \
129+ | xargs --max-args=2 install -v -D
47130 '' ;
48131
49132 nativeBuildInputs = [
50133 cmake
51134 file
135+ git
52136 makeWrapper
137+ ocaml
138+ ocamlPackages . ocamlbuild
53139 python3
54- sgx-sdk
55140 which
56141 ] ;
57142
@@ -60,27 +145,24 @@ stdenv.mkDerivation rec {
60145 protobuf
61146 ] ;
62147
63- hardeningDisable = [
64- # causes redefinition of _FORTIFY_SOURCE
65- "fortify3"
66- ] ++ lib . optionals debug [
67- "fortify"
68- ] ;
148+ dontUseCmakeConfigure = true ;
69149
70- postPatch = ''
71- patchShebangs \
72- linux/installer/bin/build-installpkg.sh \
73- linux/installer/common/psw/createTarball.sh \
74- linux/installer/common/psw/install.sh
75- '' ;
150+ preBuild = ''
151+ # Build `sgx_edger8r`, the enclave .edl -> .h file codegen tool.
152+ # Then place it in `$SGX_SDK/bin` and `$SGX_SDK/bin/x64`.
153+ make -C sdk/edger8r/linux
154+ mkdir -p $SGX_SDK/bin/x64
155+ sgx_edger8r_bin="$(readlink -f build/linux/sgx_edger8r)"
156+ ln -s $sgx_edger8r_bin $SGX_SDK/bin/
157+ ln -s $sgx_edger8r_bin $SGX_SDK/bin/x64/
76158
77- dontUseCmakeConfigure = true ;
159+ # Add this so we can link against libsgx_urts.
160+ build_dir="$(readlink -f build/linux)"
161+ ln -s $build_dir $SGX_SDK/lib
162+ ln -s $build_dir $SGX_SDK/lib64
163+ '' ;
78164
79- buildFlags = [
80- "psw_install_pkg"
81- ] ++ lib . optionals debug [
82- "DEBUG=1"
83- ] ;
165+ buildFlags = [ "psw_install_pkg" ] ++ lib . optionals debug [ "DEBUG=1" ] ;
84166
85167 installFlags = [
86168 "-C linux/installer/common/psw/output"
0 commit comments