ktls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <s2n.h>
  17. /**
  18. * @file ktls.h
  19. *
  20. * The following APIs enable applications to use kernel TLS (kTLS), meaning that
  21. * encrypting and decrypting TLS records is handled by the kernel rather than by
  22. * the s2n-tls library.
  23. *
  24. * The kTLS APIs are currently considered unstable. kTLS is a relatively new
  25. * feature with limited and volatile support from different kernels and hardware.
  26. *
  27. * Currently, s2n-tls supports ktls for only very limited scenarios:
  28. * - You must be using Linux. We have not tested with other kernels.
  29. * - Your kernel must support kTLS. For Linux, versions >4.13 should support kTLS.
  30. * - The TLS kernel module must be enabled. While some environments enable the
  31. * module by default, most will require you to run `sudo modprobe tls`.
  32. * - You must negotiate TLS1.2. TLS1.3 support is blocked on kernel support for
  33. * TLS KeyUpdate messages.
  34. * - You must negotiate AES128-GCM, which is the most preferred cipher suite
  35. * in the "default" security policy. Other ciphers are supported by the kernel,
  36. * but not implemented in s2n-tls yet.
  37. */
  38. /**
  39. * Enables sending using kTLS on a given connection.
  40. *
  41. * See above for the limitations on when kTLS can be enabled. Additionally,
  42. * s2n_connection_ktls_enable_send must be called after the handshake completes.
  43. * It may be called after some application data is sent and received without kTLS,
  44. * but there must be no pending application data that requires flushing. If these
  45. * requirements are not met, enabling kTLS will fail with an error.
  46. *
  47. * After kTLS is enabled for sending, s2n_send, s2n_sendv, and s2n_sendv_with_offset
  48. * will use kTLS. kTLS should result in memory and CPU savings. s2n_sendfile will
  49. * also become available.
  50. *
  51. * For applications using kTLS to avoid copying or allocating memory, s2n_sendv
  52. * should be preferred over s2n_sendv_with_offset. For s2n_sendv_with_offset,
  53. * s2n-tls may need to copy the provided iovec array to apply the offset, and may
  54. * need to allocate memory to copy large (>16) iovec arrays.
  55. *
  56. * If kTLS is enabled for sending, s2n_connection_get_wire_bytes_out will always
  57. * return 0 instead of an accurate count.
  58. *
  59. * @warning Due to the uncertainty around kTLS support, the signature of this
  60. * method is likely to change before kTLS is marked as stable.
  61. *
  62. * @param conn A pointer to the connection.
  63. * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully
  64. * enabled, returns S2N_FAILURE but the connection may proceed without kTLS.
  65. */
  66. S2N_API int s2n_connection_ktls_enable_send(struct s2n_connection *conn);
  67. /**
  68. * Enables receiving using kTLS on a given connection.
  69. *
  70. * See above for the limitations on when kTLS can be enabled. Additionally,
  71. * s2n_connection_ktls_enable_recv must be called after the handshake completes.
  72. * It may be called after some application data is sent and received without kTLS,
  73. * but there must be no buffered application data that requires draining. If these
  74. * requirements are not met, enabling kTLS will fail with an error.
  75. *
  76. * After kTLS is enabled for receiving, s2n_recv will use kTLS. This may result
  77. * in memory and CPU savings, but currently will still buffer and copy application data.
  78. * We will further optimize s2n_recv for kTLS in the future.
  79. *
  80. * If kTLS is enabled for receiving, s2n_connection_get_wire_bytes_in will always
  81. * return 0 instead of an accurate count.
  82. *
  83. * @warning Due to the uncertainty around kTLS support, the signature of this
  84. * method is likely to change before kTLS is marked as stable.
  85. *
  86. * @param conn A pointer to the connection.
  87. * @returns S2N_SUCCESS if kTLS is successfully enabled. If kTlS is not successfully
  88. * enabled, returns S2N_FAILURE but the connection may proceed without kTLS.
  89. */
  90. S2N_API int s2n_connection_ktls_enable_recv(struct s2n_connection *conn);
  91. /**
  92. * Sends the contents of a file as application data.
  93. *
  94. * s2n_sendfile should be more efficient than s2n_send because the copy between
  95. * the file and the write socket happens inside the kernel.
  96. *
  97. * This method is only supported if kTLS is enabled for sending.
  98. *
  99. * @param conn A pointer to the connection.
  100. * @param fd The file descriptor to read from. It must be opened for reading and
  101. * support mmap-like operations (i.e., it cannot be a socket).
  102. * @param offset The offset in the file to begin reading at.
  103. * @param count The maximum number of bytes to read from the file.
  104. * @param bytes_written Will be set to the number of bytes written if successful.
  105. * @param blocked Will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  106. * @returns S2N_SUCCESS if any bytes are successfully written, S2N_FAILURE otherwise.
  107. */
  108. S2N_API int s2n_sendfile(struct s2n_connection *conn, int fd, off_t offset, size_t count,
  109. size_t *bytes_written, s2n_blocked_status *blocked);