ktls.h 12 KB

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