tls.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. .. _kernel_tls:
  2. ==========
  3. Kernel TLS
  4. ==========
  5. Overview
  6. ========
  7. Transport Layer Security (TLS) is a Upper Layer Protocol (ULP) that runs over
  8. TCP. TLS provides end-to-end data integrity and confidentiality.
  9. User interface
  10. ==============
  11. Creating a TLS connection
  12. -------------------------
  13. First create a new TCP socket and set the TLS ULP.
  14. .. code-block:: c
  15. sock = socket(AF_INET, SOCK_STREAM, 0);
  16. setsockopt(sock, SOL_TCP, TCP_ULP, "tls", sizeof("tls"));
  17. Setting the TLS ULP allows us to set/get TLS socket options. Currently
  18. only the symmetric encryption is handled in the kernel. After the TLS
  19. handshake is complete, we have all the parameters required to move the
  20. data-path to the kernel. There is a separate socket option for moving
  21. the transmit and the receive into the kernel.
  22. .. code-block:: c
  23. /* From linux/tls.h */
  24. struct tls_crypto_info {
  25. unsigned short version;
  26. unsigned short cipher_type;
  27. };
  28. struct tls12_crypto_info_aes_gcm_128 {
  29. struct tls_crypto_info info;
  30. unsigned char iv[TLS_CIPHER_AES_GCM_128_IV_SIZE];
  31. unsigned char key[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
  32. unsigned char salt[TLS_CIPHER_AES_GCM_128_SALT_SIZE];
  33. unsigned char rec_seq[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
  34. };
  35. struct tls12_crypto_info_aes_gcm_128 crypto_info;
  36. crypto_info.info.version = TLS_1_2_VERSION;
  37. crypto_info.info.cipher_type = TLS_CIPHER_AES_GCM_128;
  38. memcpy(crypto_info.iv, iv_write, TLS_CIPHER_AES_GCM_128_IV_SIZE);
  39. memcpy(crypto_info.rec_seq, seq_number_write,
  40. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  41. memcpy(crypto_info.key, cipher_key_write, TLS_CIPHER_AES_GCM_128_KEY_SIZE);
  42. memcpy(crypto_info.salt, implicit_iv_write, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  43. setsockopt(sock, SOL_TLS, TLS_TX, &crypto_info, sizeof(crypto_info));
  44. Transmit and receive are set separately, but the setup is the same, using either
  45. TLS_TX or TLS_RX.
  46. Sending TLS application data
  47. ----------------------------
  48. After setting the TLS_TX socket option all application data sent over this
  49. socket is encrypted using TLS and the parameters provided in the socket option.
  50. For example, we can send an encrypted hello world record as follows:
  51. .. code-block:: c
  52. const char *msg = "hello world\n";
  53. send(sock, msg, strlen(msg));
  54. send() data is directly encrypted from the userspace buffer provided
  55. to the encrypted kernel send buffer if possible.
  56. The sendfile system call will send the file's data over TLS records of maximum
  57. length (2^14).
  58. .. code-block:: c
  59. file = open(filename, O_RDONLY);
  60. fstat(file, &stat);
  61. sendfile(sock, file, &offset, stat.st_size);
  62. TLS records are created and sent after each send() call, unless
  63. MSG_MORE is passed. MSG_MORE will delay creation of a record until
  64. MSG_MORE is not passed, or the maximum record size is reached.
  65. The kernel will need to allocate a buffer for the encrypted data.
  66. This buffer is allocated at the time send() is called, such that
  67. either the entire send() call will return -ENOMEM (or block waiting
  68. for memory), or the encryption will always succeed. If send() returns
  69. -ENOMEM and some data was left on the socket buffer from a previous
  70. call using MSG_MORE, the MSG_MORE data is left on the socket buffer.
  71. Receiving TLS application data
  72. ------------------------------
  73. After setting the TLS_RX socket option, all recv family socket calls
  74. are decrypted using TLS parameters provided. A full TLS record must
  75. be received before decryption can happen.
  76. .. code-block:: c
  77. char buffer[16384];
  78. recv(sock, buffer, 16384);
  79. Received data is decrypted directly in to the user buffer if it is
  80. large enough, and no additional allocations occur. If the userspace
  81. buffer is too small, data is decrypted in the kernel and copied to
  82. userspace.
  83. ``EINVAL`` is returned if the TLS version in the received message does not
  84. match the version passed in setsockopt.
  85. ``EMSGSIZE`` is returned if the received message is too big.
  86. ``EBADMSG`` is returned if decryption failed for any other reason.
  87. Send TLS control messages
  88. -------------------------
  89. Other than application data, TLS has control messages such as alert
  90. messages (record type 21) and handshake messages (record type 22), etc.
  91. These messages can be sent over the socket by providing the TLS record type
  92. via a CMSG. For example the following function sends @data of @length bytes
  93. using a record of type @record_type.
  94. .. code-block:: c
  95. /* send TLS control message using record_type */
  96. static int klts_send_ctrl_message(int sock, unsigned char record_type,
  97. void *data, size_t length)
  98. {
  99. struct msghdr msg = {0};
  100. int cmsg_len = sizeof(record_type);
  101. struct cmsghdr *cmsg;
  102. char buf[CMSG_SPACE(cmsg_len)];
  103. struct iovec msg_iov; /* Vector of data to send/receive into. */
  104. msg.msg_control = buf;
  105. msg.msg_controllen = sizeof(buf);
  106. cmsg = CMSG_FIRSTHDR(&msg);
  107. cmsg->cmsg_level = SOL_TLS;
  108. cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
  109. cmsg->cmsg_len = CMSG_LEN(cmsg_len);
  110. *CMSG_DATA(cmsg) = record_type;
  111. msg.msg_controllen = cmsg->cmsg_len;
  112. msg_iov.iov_base = data;
  113. msg_iov.iov_len = length;
  114. msg.msg_iov = &msg_iov;
  115. msg.msg_iovlen = 1;
  116. return sendmsg(sock, &msg, 0);
  117. }
  118. Control message data should be provided unencrypted, and will be
  119. encrypted by the kernel.
  120. Receiving TLS control messages
  121. ------------------------------
  122. TLS control messages are passed in the userspace buffer, with message
  123. type passed via cmsg. If no cmsg buffer is provided, an error is
  124. returned if a control message is received. Data messages may be
  125. received without a cmsg buffer set.
  126. .. code-block:: c
  127. char buffer[16384];
  128. char cmsg[CMSG_SPACE(sizeof(unsigned char))];
  129. struct msghdr msg = {0};
  130. msg.msg_control = cmsg;
  131. msg.msg_controllen = sizeof(cmsg);
  132. struct iovec msg_iov;
  133. msg_iov.iov_base = buffer;
  134. msg_iov.iov_len = 16384;
  135. msg.msg_iov = &msg_iov;
  136. msg.msg_iovlen = 1;
  137. int ret = recvmsg(sock, &msg, 0 /* flags */);
  138. struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  139. if (cmsg->cmsg_level == SOL_TLS &&
  140. cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
  141. int record_type = *((unsigned char *)CMSG_DATA(cmsg));
  142. // Do something with record_type, and control message data in
  143. // buffer.
  144. //
  145. // Note that record_type may be == to application data (23).
  146. } else {
  147. // Buffer contains application data.
  148. }
  149. recv will never return data from mixed types of TLS records.
  150. Integrating in to userspace TLS library
  151. ---------------------------------------
  152. At a high level, the kernel TLS ULP is a replacement for the record
  153. layer of a userspace TLS library.
  154. A patchset to OpenSSL to use ktls as the record layer is
  155. `here <https://github.com/Mellanox/openssl/commits/tls_rx2>`_.
  156. `An example <https://github.com/ktls/af_ktls-tool/commits/RX>`_
  157. of calling send directly after a handshake using gnutls.
  158. Since it doesn't implement a full record layer, control
  159. messages are not supported.
  160. Optional optimizations
  161. ----------------------
  162. There are certain condition-specific optimizations the TLS ULP can make,
  163. if requested. Those optimizations are either not universally beneficial
  164. or may impact correctness, hence they require an opt-in.
  165. All options are set per-socket using setsockopt(), and their
  166. state can be checked using getsockopt() and via socket diag (``ss``).
  167. TLS_TX_ZEROCOPY_RO
  168. ~~~~~~~~~~~~~~~~~~
  169. For device offload only. Allow sendfile() data to be transmitted directly
  170. to the NIC without making an in-kernel copy. This allows true zero-copy
  171. behavior when device offload is enabled.
  172. The application must make sure that the data is not modified between being
  173. submitted and transmission completing. In other words this is mostly
  174. applicable if the data sent on a socket via sendfile() is read-only.
  175. Modifying the data may result in different versions of the data being used
  176. for the original TCP transmission and TCP retransmissions. To the receiver
  177. this will look like TLS records had been tampered with and will result
  178. in record authentication failures.
  179. TLS_RX_EXPECT_NO_PAD
  180. ~~~~~~~~~~~~~~~~~~~~
  181. TLS 1.3 only. Expect the sender to not pad records. This allows the data
  182. to be decrypted directly into user space buffers with TLS 1.3.
  183. This optimization is safe to enable only if the remote end is trusted,
  184. otherwise it is an attack vector to doubling the TLS processing cost.
  185. If the record decrypted turns out to had been padded or is not a data
  186. record it will be decrypted again into a kernel buffer without zero copy.
  187. Such events are counted in the ``TlsDecryptRetry`` statistic.
  188. Statistics
  189. ==========
  190. TLS implementation exposes the following per-namespace statistics
  191. (``/proc/net/tls_stat``):
  192. - ``TlsCurrTxSw``, ``TlsCurrRxSw`` -
  193. number of TX and RX sessions currently installed where host handles
  194. cryptography
  195. - ``TlsCurrTxDevice``, ``TlsCurrRxDevice`` -
  196. number of TX and RX sessions currently installed where NIC handles
  197. cryptography
  198. - ``TlsTxSw``, ``TlsRxSw`` -
  199. number of TX and RX sessions opened with host cryptography
  200. - ``TlsTxDevice``, ``TlsRxDevice`` -
  201. number of TX and RX sessions opened with NIC cryptography
  202. - ``TlsDecryptError`` -
  203. record decryption failed (e.g. due to incorrect authentication tag)
  204. - ``TlsDeviceRxResync`` -
  205. number of RX resyncs sent to NICs handling cryptography
  206. - ``TlsDecryptRetry`` -
  207. number of RX records which had to be re-decrypted due to
  208. ``TLS_RX_EXPECT_NO_PAD`` mis-prediction. Note that this counter will
  209. also increment for non-data records.
  210. - ``TlsRxNoPadViolation`` -
  211. number of data RX records which had to be re-decrypted due to
  212. ``TLS_RX_EXPECT_NO_PAD`` mis-prediction.