ktls.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright 2018-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #if defined(OPENSSL_SYS_LINUX)
  10. # ifndef OPENSSL_NO_KTLS
  11. # include <linux/version.h>
  12. # if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
  13. # define OPENSSL_NO_KTLS
  14. # ifndef PEDANTIC
  15. # warning "KTLS requires Kernel Headers >= 4.13.0"
  16. # warning "Skipping Compilation of KTLS"
  17. # endif
  18. # endif
  19. # endif
  20. #endif
  21. #ifndef HEADER_INTERNAL_KTLS
  22. # define HEADER_INTERNAL_KTLS
  23. # pragma once
  24. # ifndef OPENSSL_NO_KTLS
  25. # if defined(__FreeBSD__)
  26. # include <sys/types.h>
  27. # include <sys/socket.h>
  28. # include <sys/ktls.h>
  29. # include <netinet/in.h>
  30. # include <netinet/tcp.h>
  31. # include <openssl/ssl3.h>
  32. # ifndef TCP_RXTLS_ENABLE
  33. # define OPENSSL_NO_KTLS_RX
  34. # endif
  35. # define OPENSSL_KTLS_AES_GCM_128
  36. # define OPENSSL_KTLS_AES_GCM_256
  37. # define OPENSSL_KTLS_TLS13
  38. # ifdef TLS_CHACHA20_IV_LEN
  39. # ifndef OPENSSL_NO_CHACHA
  40. # define OPENSSL_KTLS_CHACHA20_POLY1305
  41. # endif
  42. # endif
  43. typedef struct tls_enable ktls_crypto_info_t;
  44. /*
  45. * FreeBSD does not require any additional steps to enable KTLS before
  46. * setting keys.
  47. */
  48. static ossl_inline int ktls_enable(int fd)
  49. {
  50. return 1;
  51. }
  52. /*
  53. * The TCP_TXTLS_ENABLE socket option marks the outgoing socket buffer
  54. * as using TLS. If successful, then data sent using this socket will
  55. * be encrypted and encapsulated in TLS records using the tls_en
  56. * provided here.
  57. *
  58. * The TCP_RXTLS_ENABLE socket option marks the incoming socket buffer
  59. * as using TLS. If successful, then data received for this socket will
  60. * be authenticated and decrypted using the tls_en provided here.
  61. */
  62. static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *tls_en, int is_tx)
  63. {
  64. if (is_tx)
  65. return setsockopt(fd, IPPROTO_TCP, TCP_TXTLS_ENABLE,
  66. tls_en, sizeof(*tls_en)) ? 0 : 1;
  67. # ifndef OPENSSL_NO_KTLS_RX
  68. return setsockopt(fd, IPPROTO_TCP, TCP_RXTLS_ENABLE, tls_en,
  69. sizeof(*tls_en)) ? 0 : 1;
  70. # else
  71. return 0;
  72. # endif
  73. }
  74. /* Not supported on FreeBSD */
  75. static ossl_inline int ktls_enable_tx_zerocopy_sendfile(int fd)
  76. {
  77. return 0;
  78. }
  79. /*
  80. * Send a TLS record using the tls_en provided in ktls_start and use
  81. * record_type instead of the default SSL3_RT_APPLICATION_DATA.
  82. * When the socket is non-blocking, then this call either returns EAGAIN or
  83. * the entire record is pushed to TCP. It is impossible to send a partial
  84. * record using this control message.
  85. */
  86. static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
  87. const void *data, size_t length)
  88. {
  89. struct msghdr msg = { 0 };
  90. int cmsg_len = sizeof(record_type);
  91. struct cmsghdr *cmsg;
  92. char buf[CMSG_SPACE(cmsg_len)];
  93. struct iovec msg_iov; /* Vector of data to send/receive into */
  94. msg.msg_control = buf;
  95. msg.msg_controllen = sizeof(buf);
  96. cmsg = CMSG_FIRSTHDR(&msg);
  97. cmsg->cmsg_level = IPPROTO_TCP;
  98. cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
  99. cmsg->cmsg_len = CMSG_LEN(cmsg_len);
  100. *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
  101. msg.msg_controllen = cmsg->cmsg_len;
  102. msg_iov.iov_base = (void *)data;
  103. msg_iov.iov_len = length;
  104. msg.msg_iov = &msg_iov;
  105. msg.msg_iovlen = 1;
  106. return sendmsg(fd, &msg, 0);
  107. }
  108. # ifdef OPENSSL_NO_KTLS_RX
  109. static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
  110. {
  111. return -1;
  112. }
  113. # else /* !defined(OPENSSL_NO_KTLS_RX) */
  114. /*
  115. * Receive a TLS record using the tls_en provided in ktls_start. The
  116. * kernel strips any explicit IV and authentication tag, but provides
  117. * the TLS record header via a control message. If there is an error
  118. * with the TLS record such as an invalid header, invalid padding, or
  119. * authentication failure recvmsg() will fail with an error.
  120. */
  121. static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
  122. {
  123. struct msghdr msg = { 0 };
  124. int cmsg_len = sizeof(struct tls_get_record);
  125. struct tls_get_record *tgr;
  126. struct cmsghdr *cmsg;
  127. char buf[CMSG_SPACE(cmsg_len)];
  128. struct iovec msg_iov; /* Vector of data to send/receive into */
  129. int ret;
  130. unsigned char *p = data;
  131. const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
  132. if (length <= prepend_length) {
  133. errno = EINVAL;
  134. return -1;
  135. }
  136. msg.msg_control = buf;
  137. msg.msg_controllen = sizeof(buf);
  138. msg_iov.iov_base = p + prepend_length;
  139. msg_iov.iov_len = length - prepend_length;
  140. msg.msg_iov = &msg_iov;
  141. msg.msg_iovlen = 1;
  142. ret = recvmsg(fd, &msg, 0);
  143. if (ret <= 0)
  144. return ret;
  145. if ((msg.msg_flags & (MSG_EOR | MSG_CTRUNC)) != MSG_EOR) {
  146. errno = EMSGSIZE;
  147. return -1;
  148. }
  149. if (msg.msg_controllen == 0) {
  150. errno = EBADMSG;
  151. return -1;
  152. }
  153. cmsg = CMSG_FIRSTHDR(&msg);
  154. if (cmsg->cmsg_level != IPPROTO_TCP || cmsg->cmsg_type != TLS_GET_RECORD
  155. || cmsg->cmsg_len != CMSG_LEN(cmsg_len)) {
  156. errno = EBADMSG;
  157. return -1;
  158. }
  159. tgr = (struct tls_get_record *)CMSG_DATA(cmsg);
  160. p[0] = tgr->tls_type;
  161. p[1] = tgr->tls_vmajor;
  162. p[2] = tgr->tls_vminor;
  163. *(uint16_t *)(p + 3) = htons(ret);
  164. return ret + prepend_length;
  165. }
  166. # endif /* OPENSSL_NO_KTLS_RX */
  167. /*
  168. * KTLS enables the sendfile system call to send data from a file over
  169. * TLS.
  170. */
  171. static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off,
  172. size_t size, int flags)
  173. {
  174. off_t sbytes = 0;
  175. int ret;
  176. ret = sendfile(fd, s, off, size, NULL, &sbytes, flags);
  177. if (ret == -1 && sbytes == 0)
  178. return -1;
  179. return sbytes;
  180. }
  181. # endif /* __FreeBSD__ */
  182. # if defined(OPENSSL_SYS_LINUX)
  183. # include <linux/tls.h>
  184. # if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
  185. # define OPENSSL_NO_KTLS_RX
  186. # ifndef PEDANTIC
  187. # warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
  188. # warning "Skipping Compilation of KTLS receive data path"
  189. # endif
  190. # endif
  191. # if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
  192. # define OPENSSL_NO_KTLS_ZC_TX
  193. # ifndef PEDANTIC
  194. # warning "KTLS requires Kernel Headers >= 5.19.0 for zerocopy sendfile"
  195. # warning "Skipping Compilation of KTLS zerocopy sendfile"
  196. # endif
  197. # endif
  198. # define OPENSSL_KTLS_AES_GCM_128
  199. # if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
  200. # define OPENSSL_KTLS_AES_GCM_256
  201. # define OPENSSL_KTLS_TLS13
  202. # if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
  203. # define OPENSSL_KTLS_AES_CCM_128
  204. # if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
  205. # ifndef OPENSSL_NO_CHACHA
  206. # define OPENSSL_KTLS_CHACHA20_POLY1305
  207. # endif
  208. # endif
  209. # endif
  210. # endif
  211. # include <sys/sendfile.h>
  212. # include <netinet/tcp.h>
  213. # include <linux/socket.h>
  214. # include <openssl/ssl3.h>
  215. # include <openssl/tls1.h>
  216. # include <openssl/evp.h>
  217. # ifndef SOL_TLS
  218. # define SOL_TLS 282
  219. # endif
  220. # ifndef TCP_ULP
  221. # define TCP_ULP 31
  222. # endif
  223. # ifndef TLS_RX
  224. # define TLS_RX 2
  225. # endif
  226. struct tls_crypto_info_all {
  227. union {
  228. # ifdef OPENSSL_KTLS_AES_GCM_128
  229. struct tls12_crypto_info_aes_gcm_128 gcm128;
  230. # endif
  231. # ifdef OPENSSL_KTLS_AES_GCM_256
  232. struct tls12_crypto_info_aes_gcm_256 gcm256;
  233. # endif
  234. # ifdef OPENSSL_KTLS_AES_CCM_128
  235. struct tls12_crypto_info_aes_ccm_128 ccm128;
  236. # endif
  237. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  238. struct tls12_crypto_info_chacha20_poly1305 chacha20poly1305;
  239. # endif
  240. };
  241. size_t tls_crypto_info_len;
  242. };
  243. typedef struct tls_crypto_info_all ktls_crypto_info_t;
  244. /*
  245. * When successful, this socket option doesn't change the behaviour of the
  246. * TCP socket, except changing the TCP setsockopt handler to enable the
  247. * processing of SOL_TLS socket options. All other functionality remains the
  248. * same.
  249. */
  250. static ossl_inline int ktls_enable(int fd)
  251. {
  252. return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
  253. }
  254. /*
  255. * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
  256. * If successful, then data sent using this socket will be encrypted and
  257. * encapsulated in TLS records using the crypto_info provided here.
  258. * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
  259. * If successful, then data received using this socket will be decrypted,
  260. * authenticated and decapsulated using the crypto_info provided here.
  261. */
  262. static ossl_inline int ktls_start(int fd, ktls_crypto_info_t *crypto_info,
  263. int is_tx)
  264. {
  265. return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
  266. crypto_info, crypto_info->tls_crypto_info_len) ? 0 : 1;
  267. }
  268. static ossl_inline int ktls_enable_tx_zerocopy_sendfile(int fd)
  269. {
  270. #ifndef OPENSSL_NO_KTLS_ZC_TX
  271. int enable = 1;
  272. return setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO,
  273. &enable, sizeof(enable)) ? 0 : 1;
  274. #else
  275. return 0;
  276. #endif
  277. }
  278. /*
  279. * Send a TLS record using the crypto_info provided in ktls_start and use
  280. * record_type instead of the default SSL3_RT_APPLICATION_DATA.
  281. * When the socket is non-blocking, then this call either returns EAGAIN or
  282. * the entire record is pushed to TCP. It is impossible to send a partial
  283. * record using this control message.
  284. */
  285. static ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
  286. const void *data, size_t length)
  287. {
  288. struct msghdr msg;
  289. int cmsg_len = sizeof(record_type);
  290. struct cmsghdr *cmsg;
  291. union {
  292. struct cmsghdr hdr;
  293. char buf[CMSG_SPACE(sizeof(unsigned char))];
  294. } cmsgbuf;
  295. struct iovec msg_iov; /* Vector of data to send/receive into */
  296. memset(&msg, 0, sizeof(msg));
  297. msg.msg_control = cmsgbuf.buf;
  298. msg.msg_controllen = sizeof(cmsgbuf.buf);
  299. cmsg = CMSG_FIRSTHDR(&msg);
  300. cmsg->cmsg_level = SOL_TLS;
  301. cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
  302. cmsg->cmsg_len = CMSG_LEN(cmsg_len);
  303. *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
  304. msg.msg_controllen = cmsg->cmsg_len;
  305. msg_iov.iov_base = (void *)data;
  306. msg_iov.iov_len = length;
  307. msg.msg_iov = &msg_iov;
  308. msg.msg_iovlen = 1;
  309. return sendmsg(fd, &msg, 0);
  310. }
  311. /*
  312. * KTLS enables the sendfile system call to send data from a file over TLS.
  313. * @flags are ignored on Linux. (placeholder for FreeBSD sendfile)
  314. * */
  315. static ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
  316. {
  317. return sendfile(s, fd, &off, size);
  318. }
  319. # ifdef OPENSSL_NO_KTLS_RX
  320. static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
  321. {
  322. return -1;
  323. }
  324. # else /* !defined(OPENSSL_NO_KTLS_RX) */
  325. /*
  326. * Receive a TLS record using the crypto_info provided in ktls_start.
  327. * The kernel strips the TLS record header, IV and authentication tag,
  328. * returning only the plaintext data or an error on failure.
  329. * We add the TLS record header here to satisfy routines in rec_layer_s3.c
  330. */
  331. static ossl_inline int ktls_read_record(int fd, void *data, size_t length)
  332. {
  333. struct msghdr msg;
  334. struct cmsghdr *cmsg;
  335. union {
  336. struct cmsghdr hdr;
  337. char buf[CMSG_SPACE(sizeof(unsigned char))];
  338. } cmsgbuf;
  339. struct iovec msg_iov;
  340. int ret;
  341. unsigned char *p = data;
  342. const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
  343. if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
  344. errno = EINVAL;
  345. return -1;
  346. }
  347. memset(&msg, 0, sizeof(msg));
  348. msg.msg_control = cmsgbuf.buf;
  349. msg.msg_controllen = sizeof(cmsgbuf.buf);
  350. msg_iov.iov_base = p + prepend_length;
  351. msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
  352. msg.msg_iov = &msg_iov;
  353. msg.msg_iovlen = 1;
  354. ret = recvmsg(fd, &msg, 0);
  355. if (ret < 0)
  356. return ret;
  357. if (msg.msg_controllen > 0) {
  358. cmsg = CMSG_FIRSTHDR(&msg);
  359. if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
  360. p[0] = *((unsigned char *)CMSG_DATA(cmsg));
  361. p[1] = TLS1_2_VERSION_MAJOR;
  362. p[2] = TLS1_2_VERSION_MINOR;
  363. /* returned length is limited to msg_iov.iov_len above */
  364. p[3] = (ret >> 8) & 0xff;
  365. p[4] = ret & 0xff;
  366. ret += prepend_length;
  367. }
  368. }
  369. return ret;
  370. }
  371. # endif /* OPENSSL_NO_KTLS_RX */
  372. # endif /* OPENSSL_SYS_LINUX */
  373. # endif /* OPENSSL_NO_KTLS */
  374. #endif /* HEADER_INTERNAL_KTLS */