Solution to delegate TLS1.3 private key operation to TEE
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

README.md 14 KiB

5 år sedan
5 år sedan
5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # TLS sign delegator
  2. ## Introduction
  3. ### Problem description
  4. Typically, a TLS server uses a Certificate and associated Private Key in order to sign TLS session. From now on I'll call this Private Key a "traffic- private-key". Both certificate and traffic-private-key form a asymmetric cryptographic key-pair. Revealing the traffic-private-key makes it possible to perform men-in-the-middle type of attacks. Typically traffic-private-key is stored on the server's hard disk. Even if traffic-private-key is stored in encrypted form, at some point HTTPS server needs to have a possibility to decrypt it in order to use for signing. It means that at runtime the key in plaintext will be available in a memory of a HTTPS process. At this point attacker with an access to the machine may be able to dump memory of the process and learn the traffic-private-key.
  5. Hence, server operators need to take special care in order to make sure traffic-private-keys are not revealed.
  6. This situation gets more complicated in cases when server operator and domain owner are 2 different entities. For example in case of CDN, TLS offloading happens on the edge system - which often is a completely different machine than actual application server. Also it is often the case that servers (physical machines) of CDN provider are spread over the world and are located in remote data centers. Those data centers may be owned by multiple different entities.
  7. In such situations, problem of ensuring that the traffic-private-key is not copied and used by an attacker may be challenging and not obvious to solve. Clients of the CDN may also be concerned about idea of spreading the traffic-private-key over the world.
  8. ### Solution proposed
  9. For brevity I'm assuming server uses only TLS 1.3 as specified in [RFC8446], but solution can be adapted to any version of TLS.
  10. The idea is to perform TLS session signing inside Trusted Execution Environment. The traffic-private-key will be accessible only to TEE. Additionally, solution ensures that key is stored in encrypted form in trusted storage. The storage is bound to the physical machine and hence copy of the storage can’t be used on some different machine.
  11. The solution as implemented in the PoC and described below is based on ARM TrustZone and it uses open sourced TEE called OP-TEE (see [OP-TEE]), sources of OP-TEE are stored on github (see [OP-TEE-SRC]). OP-TEE was driven by the fact that author is quite familiar with environment nevertheless it can be implemented with other TEEs which provide device bound trusted storage. Author is convinced that Intel SGX with Asylo would be better choice here. Solution makes also heavy use of BoringSSL for handling with TLS traffic.
  12. Points below describe implementation in more details:
  13. 1. Key provisioning server
  14. It is assumed that machine is initially provisioned with a software
  15. which acts as a server for traffic-private-key provisioning.
  16. In order to install traffic-private-key on a machine, operator connects
  17. to key provisioning server and sends the traffic-private-key to be
  18. installed on the machine. This operation is done over TLS connection which uses
  19. client authentication. Possition of some form of TLS provisioning
  20. is required by the operator. Key provisioning server must be able
  21. to verify provisioning key, hence verification-provisioning-key is
  22. also preinstalleld.
  23. After sucessuful TLS authentication, operator sends a pair of
  24. traffic-private-key and domain name for which the key must be used.
  25. This pair is installed on secure storage which accessible from TEE
  26. only. TEE ensures traffic-provisioning-key can't be read from outside
  27. of TEE.
  28. 2. TLS session signing
  29. Solution uses BoringSSL to offload TLS traffic. BoringSSL API gives a
  30. possibility to register a function which is called during TLS handshake,
  31. when server needs to sign a session with traffic-private-key.
  32. It means that there are no modifications needed to BoringSSL in order
  33. to use it for signing TLS session with traffic-private-key stored in
  34. TEE.
  35. The code which registers signing operation looks like this:
  36. ```
  37. void signing_operation(message_to_sign, domain_name, *signature) {
  38. ... calls TEE for signing ...
  39. }
  40. SSL_PRIVATE_KEY_METHOD private_key_methods {
  41. .sign = signing_operation
  42. .decrypt = ...
  43. .complete = ...
  44. };
  45. SSL_CTX_set_private_key_method(SSL_CTX, &private_key_methods)
  46. ```
  47. TLS server calls ``signing_operation`` function when TLS session
  48. needs to be signed. This function passes ``message_to_sign`` and
  49. ``domain_name`` to the TEE. While in the TEE, the ``domain_name``
  50. is used as an index in order to retrieve right traffic-private-key
  51. (many domains can be handled by the server). TEE performs signing
  52. and ``signature`` is returned to the BoringSSL. BoringSSL
  53. continues TLS handshake as normal.
  54. 3. Key can't be used on another machine.
  55. Trusted storage in OP-TEE is bound to the physical device. It means
  56. even if the storage is coppied to another device, it won't be possible
  57. to decrypt stored data.
  58. In more detail, OP-TEE implements GlobalPlatform Trusted Storage API.
  59. Device binding is one of the requirements for trusted storage. In
  60. order to make it possible each device needs to come with preinstalled
  61. Hardware Unique Key (HUK).
  62. More details about trusted storage can be found on in OP-TEE
  63. documentation (see [OP-TEE-STORAGE]).
  64. It must be mentioned, that in order to use trusted storage, SoC
  65. specific customization is needed (see comment in orange at the bottom
  66. of [OP-TEE-STORAGE]).
  67. 4. RPMB
  68. TODO
  69. ## PoC implementation
  70. As mentioned before, implementation uses OP-TEE as a base for TEE. Example
  71. was tested with OP-TEE running inside QEMU emulating ARMv8.
  72. PoC is composed of:
  73. * ``admin_cli``: Client used for installing the private keys inside TEE. This
  74. component is used instead of ``key provisioning server`` as such server was
  75. not implemented in PoC.
  76. * ``server``: It is a TLS offloading server. Server listens on ``127.0.0.1:443``
  77. and uses BoringSSL to accept and handle TLS connection. Server implements
  78. function callback, which calls TEE when private key operation needs to be done.
  79. Only ECDSA/P256 sining is currently supported.
  80. * ``ta``: Trusted application running inside TEE. The application is responsible
  81. for processing requests from ``admin_cli``, which is storing the keys
  82. on trusted storage and deleting them if requested. As well as processing signing
  83. requests from the ``server``.
  84. The section called "Example of usage" explains how to use the software in details.
  85. ### Compilation and installation
  86. Following steps need to be taken to install the software:
  87. 1. OP-TEE building. This step is explained in details (here)[https://optee.readthedocs.io/building/gits/build.html#get-and-build-the-solution]. It is required to perform steps 1 to 5. The ``TARGET`` (see the building instruction) used by this example is called ``QEMUv8``. In case OP-TEE is started after step 5, it has to be stopped.
  88. 2. Next steps assume that Linux operating system is used and OP-TEE has been cloned to the directory called ``OPTEE_DIR``.
  89. 3. Create directory ``/tmp/tee_shared``
  90. 4. Go to ``OPTEE_DIR`` directory.
  91. 5. Clone ``git clone https://git.amongbytes.com/kris/c3-tls-sign-delegator.git projects``
  92. 6. Compile BoringSSL for aarch64 and native system: ``cd OPTEE_DIR/projects/bssl; make``. Makefile is configured to use toolchain build in step 1. This step will also build BoringSSL for host machine, it requires all dependencies for building BoringSSL are installed (see [BORING-BUILD]).
  93. 7. Compile solution: ``cd OPTEE_DIR/projects/delegator; make``
  94. ### Start process
  95. 1. Starting OP-TEE:
  96. At this point it is best to refer to [video 1](https://youtu.be/02klEwlsJIA) on youtube as step is quite complicated.
  97. <iframe width="560" height="315" src="https://www.youtube.com/embed/02klEwlsJIA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  98. User needs to:
  99. * Enter build directory: ``cd OPTEE_DIR/build``
  100. * Start QEMU with OP-TEE emulation: ``make QEMU_VIRTFS_ENABLE=y QEMU_USERNET_ENABLE=y QEMU_VIRTFS_HOST_DIR=/tmp/tee_share HOSTFWD=",hostfwd=tcp::1443-:1443" run-only``.
  101. * Just after qemu starts it will pause with following prompt:
  102. ```
  103. cd /home/hdc/repos/optee/qemuv8/build/../out/bin && /home/hdc/repos/optee/qemuv8/build/../qemu/aarch64-softmmu/qemu-system-aarch64 \
  104. -nographic \
  105. -serial tcp:localhost:54320 -serial tcp:localhost:54321 \
  106. -smp 2 \
  107. -s -S -machine virt,secure=on -cpu cortex-a57 \
  108. -d unimp -semihosting-config enable,target=native \
  109. -m 1057 \
  110. -bios bl1.bin \
  111. -initrd rootfs.cpio.gz \
  112. -kernel Image -no-acpi \
  113. -append 'console=ttyAMA0,38400 keep_bootcon root=/dev/vda2' \
  114. -fsdev local,id=fsdev0,path=/tmp/tee_share,security_model=none -device virtio-9p-device,fsdev=fsdev0,mount_tag=host -netdev user,id=vmnic,hostfwd=tcp::1443-:1443 -device virtio-net-device,netdev=vmnic
  115. QEMU 3.0.93 monitor - type 'help' for more information
  116. (qemu)
  117. ```
  118. User needs to continue the process by entering ``c``
  119. ```
  120. (qemu) c
  121. ```
  122. After a while 2 additional terminals should appear - one terminal labeld as "Normal", running linux and another terminal labeled as "Secure" with output from the TEE.
  123. 2. In the "Normal World" terminal user needs to mount file system to share data between guest and host machine. Following command needs to be used:
  124. ```
  125. mount -t 9p -o trans=virtio host /mnt
  126. ```
  127. See [video 2](https://www.youtube.com/embed/5psOtKtdlWI):
  128. <iframe width="560" height="315" src="https://www.youtube.com/embed/5psOtKtdlWI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  129. 3. Install Trusted Application inside OP-TEE:
  130. In the "Normal" terminal invoke:
  131. ```
  132. sh /mnt/out/etc/tee_install
  133. ```
  134. See [video 3](https://www.youtube.com/embed/aGbqgz_e9Ec):
  135. <iframe width="560" height="315" src="https://www.youtube.com/embed/aGbqgz_e9Ec" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  136. At this point installation and startup process is complated and solution can be used.
  137. ### Example of usage
  138. 1. Installing a key on secure storage
  139. First step is to install the key on secure storage. Ideally this step is done by "Key provisioning server". Nevertheless, this PoC doesn't implement such server. Instead ``admin_cli`` can be used to install the key.
  140. In the "Normal" terminal, go to ``/mnt/out/`` and invoke
  141. ```
  142. cd /mnt/out
  143. # ./admin_cli/admin_cli put www.test.com etc/ecdsa_256.key
  144. ```
  145. This command installs private key for ``www.test.com``. In the "Secure" terminal you should see a message ``E/TA: install_key:156 Storing a key``. After this step ``etc/ecdsa_256.key`` can be removed.
  146. See [video 4](https://www.youtube.com/embed/__7WKvx8XxM):
  147. <iframe width="560" height="315" src="https://www.youtube.com/embed/__7WKvx8XxM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  148. 2. Start a TLS server and perform TLS handshake:
  149. With private key installed TLS server can be started. In the "Normal" terminal invoke
  150. ```
  151. > cd /mnt/out
  152. > ./server/server
  153. ```
  154. Server will start listening on ``127.0.0.1:1443``. In the host machine one can try to connect to the TLS server:
  155. ```
  156. > cd OPTEE_DIR
  157. > ./projects/bssl/src/build.native/tool/bssl client -connect 127.0.0.1:1443 -server-name "www.test.com"
  158. Connecting to 127.0.0.1:1443
  159. Connected.
  160. Version: TLSv1.3
  161. Resumed session: no
  162. Cipher: TLS_AES_128_GCM_SHA256
  163. ECDHE curve: X25519
  164. Signature algorithm: ecdsa_secp256r1_sha256
  165. Secure renegotiation: yes
  166. Extended master secret: yes
  167. Next protocol negotiated:
  168. ALPN protocol:
  169. OCSP staple: no
  170. SCT list: no
  171. Early data: no
  172. Cert subject: CN = www.dmv.com
  173. Cert issuer: C = FR, ST = PACA, L = Cagnes sur Mer, OU = Domain Control Validated SARL, CN = Domain Control Validated SARL
  174. ```
  175. See [video 5](https://www.youtube.com/embed/kRRl2zhbUqc)
  176. <iframe width="560" height="315" src="https://www.youtube.com/embed/kRRl2zhbUqc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  177. 3. Trial to access different domain fails as traffic-private-key is not available.
  178. See [video 6](https://www.youtube.com/embed/LBhllWcn4RY)
  179. <iframe width="560" height="315" src="https://www.youtube.com/embed/LBhllWcn4RY" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>)
  180. ### Extensions to the idea
  181. First of all - key storage is bound to the device. In order to use stolen key for MITM, attacker needs to steal whole machine, which is much more difficult and easier to control. In order to implement such solution user doesn’t need expensive HSM, but it can simply use Intel with SGX and Asylo. Also it’s easy to imagine some extensions to this idea. For example instead of calling TEE each time for session signing during TLS handshake, one could imagine that solution can use “Delegated Credentials for TLS” (see https://tools.ietf.org/html/draft-rescorla-tls-subcerts-02). In this case TEE would be responsible for generating short lived certificates and TLS server would request such certificate every fixed amount of time (every few minutes). This idea could be combined with another – instead of storing traffic-private-key in multiple machines, one could imagine storing a key in some central location with more restricted access (but still in TEE). Combining those two ideas improves security of traffic-private-key storage without degrading time needed to perform TLS handshake. It must be noticed that “Delegated Credentials for TLS” are already implemented in BoringSSL.
  182. ## Links
  183. * [C3] https://inthecloud.withgoogle.com/computing-challenge/register.html
  184. * [RFC8446] https://tools.ietf.org/html/rfc8446
  185. * [OP-TEE] https://www.op-tee.org/
  186. * [OP-TEE-SRC] https://github.com/OP-TEE
  187. * [OP-TEE-STORAGE] https://optee.readthedocs.io/architecture/secure_storage.html
  188. * [BORING-BUILD] https://github.com/google/boringssl/blob/master/BUILDING.md