ktls.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2018-2022 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. #include "ssl_local.h"
  10. #include "internal/ktls.h"
  11. #ifndef OPENSSL_NO_KTLS_RX
  12. /*
  13. * Count the number of records that were not processed yet from record boundary.
  14. *
  15. * This function assumes that there are only fully formed records read in the
  16. * record layer. If read_ahead is enabled, then this might be false and this
  17. * function will fail.
  18. */
  19. static int count_unprocessed_records(SSL *s)
  20. {
  21. SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
  22. PACKET pkt, subpkt;
  23. int count = 0;
  24. if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
  25. return -1;
  26. while (PACKET_remaining(&pkt) > 0) {
  27. /* Skip record type and version */
  28. if (!PACKET_forward(&pkt, 3))
  29. return -1;
  30. /* Read until next record */
  31. if (!PACKET_get_length_prefixed_2(&pkt, &subpkt))
  32. return -1;
  33. count += 1;
  34. }
  35. return count;
  36. }
  37. /*
  38. * The kernel cannot offload receive if a partial TLS record has been read.
  39. * Check the read buffer for unprocessed records. If the buffer contains a
  40. * partial record, fail and return 0. Otherwise, update the sequence
  41. * number at *rec_seq for the count of unprocessed records and return 1.
  42. */
  43. static int check_rx_read_ahead(SSL *s, unsigned char *rec_seq)
  44. {
  45. int bit, count_unprocessed;
  46. count_unprocessed = count_unprocessed_records(s);
  47. if (count_unprocessed < 0)
  48. return 0;
  49. /* increment the crypto_info record sequence */
  50. while (count_unprocessed) {
  51. for (bit = 7; bit >= 0; bit--) { /* increment */
  52. ++rec_seq[bit];
  53. if (rec_seq[bit] != 0)
  54. break;
  55. }
  56. count_unprocessed--;
  57. }
  58. return 1;
  59. }
  60. #endif
  61. #if defined(__FreeBSD__)
  62. # include "crypto/cryptodev.h"
  63. /*-
  64. * Check if a given cipher is supported by the KTLS interface.
  65. * The kernel might still fail the setsockopt() if no suitable
  66. * provider is found, but this checks if the socket option
  67. * supports the cipher suite used at all.
  68. */
  69. int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
  70. const EVP_CIPHER_CTX *dd)
  71. {
  72. switch (s->version) {
  73. case TLS1_VERSION:
  74. case TLS1_1_VERSION:
  75. case TLS1_2_VERSION:
  76. case TLS1_3_VERSION:
  77. break;
  78. default:
  79. return 0;
  80. }
  81. switch (s->s3.tmp.new_cipher->algorithm_enc) {
  82. case SSL_AES128GCM:
  83. case SSL_AES256GCM:
  84. return 1;
  85. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  86. case SSL_CHACHA20POLY1305:
  87. return 1;
  88. # endif
  89. case SSL_AES128:
  90. case SSL_AES256:
  91. if (s->ext.use_etm)
  92. return 0;
  93. switch (s->s3.tmp.new_cipher->algorithm_mac) {
  94. case SSL_SHA1:
  95. case SSL_SHA256:
  96. case SSL_SHA384:
  97. return 1;
  98. default:
  99. return 0;
  100. }
  101. default:
  102. return 0;
  103. }
  104. }
  105. /* Function to configure kernel TLS structure */
  106. int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
  107. void *rl_sequence, ktls_crypto_info_t *crypto_info,
  108. int is_tx, unsigned char *iv,
  109. unsigned char *key, unsigned char *mac_key,
  110. size_t mac_secret_size)
  111. {
  112. memset(crypto_info, 0, sizeof(*crypto_info));
  113. switch (s->s3.tmp.new_cipher->algorithm_enc) {
  114. case SSL_AES128GCM:
  115. case SSL_AES256GCM:
  116. crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
  117. if (s->version == TLS1_3_VERSION)
  118. crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
  119. else
  120. crypto_info->iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  121. break;
  122. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  123. case SSL_CHACHA20POLY1305:
  124. crypto_info->cipher_algorithm = CRYPTO_CHACHA20_POLY1305;
  125. crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
  126. break;
  127. # endif
  128. case SSL_AES128:
  129. case SSL_AES256:
  130. switch (s->s3.tmp.new_cipher->algorithm_mac) {
  131. case SSL_SHA1:
  132. crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
  133. break;
  134. case SSL_SHA256:
  135. crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
  136. break;
  137. case SSL_SHA384:
  138. crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
  139. break;
  140. default:
  141. return 0;
  142. }
  143. crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
  144. crypto_info->iv_len = EVP_CIPHER_get_iv_length(c);
  145. crypto_info->auth_key = mac_key;
  146. crypto_info->auth_key_len = mac_secret_size;
  147. break;
  148. default:
  149. return 0;
  150. }
  151. crypto_info->cipher_key = key;
  152. crypto_info->cipher_key_len = EVP_CIPHER_get_key_length(c);
  153. crypto_info->iv = iv;
  154. crypto_info->tls_vmajor = (s->version >> 8) & 0x000000ff;
  155. crypto_info->tls_vminor = (s->version & 0x000000ff);
  156. # ifdef TCP_RXTLS_ENABLE
  157. memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
  158. if (!is_tx && !check_rx_read_ahead(s, crypto_info->rec_seq))
  159. return 0;
  160. # else
  161. if (!is_tx)
  162. return 0;
  163. # endif
  164. return 1;
  165. };
  166. #endif /* __FreeBSD__ */
  167. #if defined(OPENSSL_SYS_LINUX)
  168. /* Function to check supported ciphers in Linux */
  169. int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
  170. const EVP_CIPHER_CTX *dd)
  171. {
  172. switch (s->version) {
  173. case TLS1_2_VERSION:
  174. case TLS1_3_VERSION:
  175. break;
  176. default:
  177. return 0;
  178. }
  179. /* check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128
  180. * or Chacha20-Poly1305
  181. */
  182. # ifdef OPENSSL_KTLS_AES_CCM_128
  183. if (EVP_CIPHER_is_a(c, "AES-128-CCM")) {
  184. if (s->version == TLS_1_3_VERSION /* broken on 5.x kernels */
  185. || EVP_CIPHER_CTX_get_tag_length(dd) != EVP_CCM_TLS_TAG_LEN)
  186. return 0;
  187. return 1;
  188. } else
  189. # endif
  190. if (0
  191. # ifdef OPENSSL_KTLS_AES_GCM_128
  192. || EVP_CIPHER_is_a(c, "AES-128-GCM")
  193. # endif
  194. # ifdef OPENSSL_KTLS_AES_GCM_256
  195. || EVP_CIPHER_is_a(c, "AES-256-GCM")
  196. # endif
  197. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  198. || EVP_CIPHER_is_a(c, "ChaCha20-Poly1305")
  199. # endif
  200. ) {
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. /* Function to configure kernel TLS structure */
  206. int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
  207. void *rl_sequence, ktls_crypto_info_t *crypto_info,
  208. int is_tx, unsigned char *iv,
  209. unsigned char *key, unsigned char *mac_key,
  210. size_t mac_secret_size)
  211. {
  212. unsigned char geniv[12];
  213. unsigned char *iiv = iv;
  214. # ifdef OPENSSL_NO_KTLS_RX
  215. if (!is_tx)
  216. return 0;
  217. # endif
  218. if (s->version == TLS1_2_VERSION &&
  219. EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) {
  220. if (!EVP_CIPHER_CTX_get_updated_iv(dd, geniv,
  221. EVP_GCM_TLS_FIXED_IV_LEN
  222. + EVP_GCM_TLS_EXPLICIT_IV_LEN))
  223. return 0;
  224. iiv = geniv;
  225. }
  226. memset(crypto_info, 0, sizeof(*crypto_info));
  227. switch (EVP_CIPHER_get_nid(c))
  228. {
  229. # ifdef OPENSSL_KTLS_AES_GCM_128
  230. case NID_aes_128_gcm:
  231. crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
  232. crypto_info->gcm128.info.version = s->version;
  233. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
  234. memcpy(crypto_info->gcm128.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
  235. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  236. memcpy(crypto_info->gcm128.salt, iiv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  237. memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_get_key_length(c));
  238. memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
  239. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  240. if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm128.rec_seq))
  241. return 0;
  242. return 1;
  243. # endif
  244. # ifdef OPENSSL_KTLS_AES_GCM_256
  245. case NID_aes_256_gcm:
  246. crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
  247. crypto_info->gcm256.info.version = s->version;
  248. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
  249. memcpy(crypto_info->gcm256.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
  250. TLS_CIPHER_AES_GCM_256_IV_SIZE);
  251. memcpy(crypto_info->gcm256.salt, iiv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
  252. memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_get_key_length(c));
  253. memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
  254. TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
  255. if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm256.rec_seq))
  256. return 0;
  257. return 1;
  258. # endif
  259. # ifdef OPENSSL_KTLS_AES_CCM_128
  260. case NID_aes_128_ccm:
  261. crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
  262. crypto_info->ccm128.info.version = s->version;
  263. crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
  264. memcpy(crypto_info->ccm128.iv, iiv + EVP_CCM_TLS_FIXED_IV_LEN,
  265. TLS_CIPHER_AES_CCM_128_IV_SIZE);
  266. memcpy(crypto_info->ccm128.salt, iiv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
  267. memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_get_key_length(c));
  268. memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
  269. TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
  270. if (!is_tx && !check_rx_read_ahead(s, crypto_info->ccm128.rec_seq))
  271. return 0;
  272. return 1;
  273. # endif
  274. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  275. case NID_chacha20_poly1305:
  276. crypto_info->chacha20poly1305.info.cipher_type = TLS_CIPHER_CHACHA20_POLY1305;
  277. crypto_info->chacha20poly1305.info.version = s->version;
  278. crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
  279. memcpy(crypto_info->chacha20poly1305.iv, iiv,
  280. TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
  281. memcpy(crypto_info->chacha20poly1305.key, key,
  282. EVP_CIPHER_get_key_length(c));
  283. memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
  284. TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
  285. if (!is_tx
  286. && !check_rx_read_ahead(s,
  287. crypto_info->chacha20poly1305.rec_seq))
  288. return 0;
  289. return 1;
  290. # endif
  291. default:
  292. return 0;
  293. }
  294. }
  295. #endif /* OPENSSL_SYS_LINUX */