ktls_meth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. #include <openssl/evp.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/rand.h>
  12. #include "../../ssl_local.h"
  13. #include "../record_local.h"
  14. #include "recmethod_local.h"
  15. #include "internal/ktls.h"
  16. static struct record_functions_st ossl_ktls_funcs;
  17. #if defined(__FreeBSD__)
  18. # include "crypto/cryptodev.h"
  19. /*-
  20. * Check if a given cipher is supported by the KTLS interface.
  21. * The kernel might still fail the setsockopt() if no suitable
  22. * provider is found, but this checks if the socket option
  23. * supports the cipher suite used at all.
  24. */
  25. static int ktls_int_check_supported_cipher(OSSL_RECORD_LAYER *rl,
  26. const EVP_CIPHER *c,
  27. const EVP_MD *md,
  28. size_t taglen)
  29. {
  30. switch (rl->version) {
  31. case TLS1_VERSION:
  32. case TLS1_1_VERSION:
  33. case TLS1_2_VERSION:
  34. #ifdef OPENSSL_KTLS_TLS13
  35. case TLS1_3_VERSION:
  36. #endif
  37. break;
  38. default:
  39. return 0;
  40. }
  41. if (EVP_CIPHER_is_a(c, "AES-128-GCM")
  42. || EVP_CIPHER_is_a(c, "AES-256-GCM")
  43. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  44. || EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")
  45. # endif
  46. )
  47. return 1;
  48. if (!EVP_CIPHER_is_a(c, "AES-128-CBC")
  49. && !EVP_CIPHER_is_a(c, "AES-256-CBC"))
  50. return 0;
  51. if (rl->use_etm)
  52. return 0;
  53. if (md == NULL)
  54. return 0;
  55. if (EVP_MD_is_a(md, "SHA1")
  56. || EVP_MD_is_a(md, "SHA2-256")
  57. || EVP_MD_is_a(md, "SHA2-384"))
  58. return 1;
  59. return 0;
  60. }
  61. /* Function to configure kernel TLS structure */
  62. static
  63. int ktls_configure_crypto(OSSL_LIB_CTX *libctx, int version, const EVP_CIPHER *c,
  64. EVP_MD *md, void *rl_sequence,
  65. ktls_crypto_info_t *crypto_info, int is_tx,
  66. unsigned char *iv, size_t ivlen,
  67. unsigned char *key, size_t keylen,
  68. unsigned char *mac_key, size_t mac_secret_size)
  69. {
  70. memset(crypto_info, 0, sizeof(*crypto_info));
  71. if (EVP_CIPHER_is_a(c, "AES-128-GCM")
  72. || EVP_CIPHER_is_a(c, "AES-256-GCM")) {
  73. crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
  74. crypto_info->iv_len = ivlen;
  75. } else
  76. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  77. if (EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")) {
  78. crypto_info->cipher_algorithm = CRYPTO_CHACHA20_POLY1305;
  79. crypto_info->iv_len = ivlen;
  80. } else
  81. # endif
  82. if (EVP_CIPHER_is_a(c, "AES-128-CBC") || EVP_CIPHER_is_a(c, "AES-256-CBC")) {
  83. if (md == NULL)
  84. return 0;
  85. if (EVP_MD_is_a(md, "SHA1"))
  86. crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
  87. else if (EVP_MD_is_a(md, "SHA2-256"))
  88. crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
  89. else if (EVP_MD_is_a(md, "SHA2-384"))
  90. crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
  91. else
  92. return 0;
  93. crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
  94. crypto_info->iv_len = ivlen;
  95. crypto_info->auth_key = mac_key;
  96. crypto_info->auth_key_len = mac_secret_size;
  97. } else {
  98. return 0;
  99. }
  100. crypto_info->cipher_key = key;
  101. crypto_info->cipher_key_len = keylen;
  102. crypto_info->iv = iv;
  103. crypto_info->tls_vmajor = (version >> 8) & 0x000000ff;
  104. crypto_info->tls_vminor = (version & 0x000000ff);
  105. # ifdef TCP_RXTLS_ENABLE
  106. memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
  107. # else
  108. if (!is_tx)
  109. return 0;
  110. # endif
  111. return 1;
  112. };
  113. #endif /* __FreeBSD__ */
  114. #if defined(OPENSSL_SYS_LINUX)
  115. /* Function to check supported ciphers in Linux */
  116. static int ktls_int_check_supported_cipher(OSSL_RECORD_LAYER *rl,
  117. const EVP_CIPHER *c,
  118. const EVP_MD *md,
  119. size_t taglen)
  120. {
  121. switch (rl->version) {
  122. case TLS1_2_VERSION:
  123. #ifdef OPENSSL_KTLS_TLS13
  124. case TLS1_3_VERSION:
  125. #endif
  126. break;
  127. default:
  128. return 0;
  129. }
  130. /*
  131. * Check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128
  132. * or Chacha20-Poly1305
  133. */
  134. # ifdef OPENSSL_KTLS_AES_CCM_128
  135. if (EVP_CIPHER_is_a(c, "AES-128-CCM")) {
  136. if (taglen != EVP_CCM_TLS_TAG_LEN)
  137. return 0;
  138. return 1;
  139. } else
  140. # endif
  141. if (0
  142. # ifdef OPENSSL_KTLS_AES_GCM_128
  143. || EVP_CIPHER_is_a(c, "AES-128-GCM")
  144. # endif
  145. # ifdef OPENSSL_KTLS_AES_GCM_256
  146. || EVP_CIPHER_is_a(c, "AES-256-GCM")
  147. # endif
  148. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  149. || EVP_CIPHER_is_a(c, "ChaCha20-Poly1305")
  150. # endif
  151. ) {
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. /* Function to configure kernel TLS structure */
  157. static
  158. int ktls_configure_crypto(OSSL_LIB_CTX *libctx, int version, const EVP_CIPHER *c,
  159. const EVP_MD *md, void *rl_sequence,
  160. ktls_crypto_info_t *crypto_info, int is_tx,
  161. unsigned char *iv, size_t ivlen,
  162. unsigned char *key, size_t keylen,
  163. unsigned char *mac_key, size_t mac_secret_size)
  164. {
  165. unsigned char geniv[EVP_GCM_TLS_EXPLICIT_IV_LEN];
  166. unsigned char *eiv = NULL;
  167. # ifdef OPENSSL_NO_KTLS_RX
  168. if (!is_tx)
  169. return 0;
  170. # endif
  171. if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE
  172. || EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE) {
  173. if (!ossl_assert(EVP_GCM_TLS_FIXED_IV_LEN == EVP_CCM_TLS_FIXED_IV_LEN)
  174. || !ossl_assert(EVP_GCM_TLS_EXPLICIT_IV_LEN
  175. == EVP_CCM_TLS_EXPLICIT_IV_LEN))
  176. return 0;
  177. if (version == TLS1_2_VERSION) {
  178. if (!ossl_assert(ivlen == EVP_GCM_TLS_FIXED_IV_LEN))
  179. return 0;
  180. if (is_tx) {
  181. if (RAND_bytes_ex(libctx, geniv,
  182. EVP_GCM_TLS_EXPLICIT_IV_LEN, 0) <= 0)
  183. return 0;
  184. } else {
  185. memset(geniv, 0, EVP_GCM_TLS_EXPLICIT_IV_LEN);
  186. }
  187. eiv = geniv;
  188. } else {
  189. if (!ossl_assert(ivlen == EVP_GCM_TLS_FIXED_IV_LEN
  190. + EVP_GCM_TLS_EXPLICIT_IV_LEN))
  191. return 0;
  192. eiv = iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE;
  193. }
  194. }
  195. memset(crypto_info, 0, sizeof(*crypto_info));
  196. switch (EVP_CIPHER_get_nid(c)) {
  197. # ifdef OPENSSL_KTLS_AES_GCM_128
  198. case NID_aes_128_gcm:
  199. if (!ossl_assert(TLS_CIPHER_AES_GCM_128_SALT_SIZE
  200. == EVP_GCM_TLS_FIXED_IV_LEN)
  201. || !ossl_assert(TLS_CIPHER_AES_GCM_128_IV_SIZE
  202. == EVP_GCM_TLS_EXPLICIT_IV_LEN))
  203. return 0;
  204. crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
  205. crypto_info->gcm128.info.version = version;
  206. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
  207. memcpy(crypto_info->gcm128.iv, eiv, TLS_CIPHER_AES_GCM_128_IV_SIZE);
  208. memcpy(crypto_info->gcm128.salt, iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  209. memcpy(crypto_info->gcm128.key, key, keylen);
  210. memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
  211. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  212. return 1;
  213. # endif
  214. # ifdef OPENSSL_KTLS_AES_GCM_256
  215. case NID_aes_256_gcm:
  216. if (!ossl_assert(TLS_CIPHER_AES_GCM_256_SALT_SIZE
  217. == EVP_GCM_TLS_FIXED_IV_LEN)
  218. || !ossl_assert(TLS_CIPHER_AES_GCM_256_IV_SIZE
  219. == EVP_GCM_TLS_EXPLICIT_IV_LEN))
  220. return 0;
  221. crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
  222. crypto_info->gcm256.info.version = version;
  223. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
  224. memcpy(crypto_info->gcm256.iv, eiv, TLS_CIPHER_AES_GCM_256_IV_SIZE);
  225. memcpy(crypto_info->gcm256.salt, iv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
  226. memcpy(crypto_info->gcm256.key, key, keylen);
  227. memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
  228. TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
  229. return 1;
  230. # endif
  231. # ifdef OPENSSL_KTLS_AES_CCM_128
  232. case NID_aes_128_ccm:
  233. if (!ossl_assert(TLS_CIPHER_AES_CCM_128_SALT_SIZE
  234. == EVP_CCM_TLS_FIXED_IV_LEN)
  235. || !ossl_assert(TLS_CIPHER_AES_CCM_128_IV_SIZE
  236. == EVP_CCM_TLS_EXPLICIT_IV_LEN))
  237. return 0;
  238. crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
  239. crypto_info->ccm128.info.version = version;
  240. crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
  241. memcpy(crypto_info->ccm128.iv, eiv, TLS_CIPHER_AES_CCM_128_IV_SIZE);
  242. memcpy(crypto_info->ccm128.salt, iv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
  243. memcpy(crypto_info->ccm128.key, key, keylen);
  244. memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
  245. TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
  246. return 1;
  247. # endif
  248. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  249. case NID_chacha20_poly1305:
  250. if (!ossl_assert(ivlen == TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE))
  251. return 0;
  252. crypto_info->chacha20poly1305.info.cipher_type
  253. = TLS_CIPHER_CHACHA20_POLY1305;
  254. crypto_info->chacha20poly1305.info.version = version;
  255. crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
  256. memcpy(crypto_info->chacha20poly1305.iv, iv, ivlen);
  257. memcpy(crypto_info->chacha20poly1305.key, key, keylen);
  258. memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
  259. TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
  260. return 1;
  261. # endif
  262. default:
  263. return 0;
  264. }
  265. }
  266. #endif /* OPENSSL_SYS_LINUX */
  267. static int ktls_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
  268. unsigned char *key, size_t keylen,
  269. unsigned char *iv, size_t ivlen,
  270. unsigned char *mackey, size_t mackeylen,
  271. const EVP_CIPHER *ciph,
  272. size_t taglen,
  273. int mactype,
  274. const EVP_MD *md,
  275. COMP_METHOD *comp)
  276. {
  277. ktls_crypto_info_t crypto_info;
  278. /*
  279. * Check if we are suitable for KTLS. If not suitable we return
  280. * OSSL_RECORD_RETURN_NON_FATAL_ERR so that other record layers can be tried
  281. * instead
  282. */
  283. if (comp != NULL)
  284. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  285. /* ktls supports only the maximum fragment size */
  286. if (rl->max_frag_len != SSL3_RT_MAX_PLAIN_LENGTH)
  287. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  288. /* check that cipher is supported */
  289. if (!ktls_int_check_supported_cipher(rl, ciph, md, taglen))
  290. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  291. /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
  292. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
  293. if (BIO_flush(rl->bio) <= 0)
  294. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  295. /* KTLS does not support record padding */
  296. if (rl->padding != NULL || rl->block_padding > 0)
  297. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  298. }
  299. if (!ktls_configure_crypto(rl->libctx, rl->version, ciph, md, rl->sequence,
  300. &crypto_info,
  301. rl->direction == OSSL_RECORD_DIRECTION_WRITE,
  302. iv, ivlen, key, keylen, mackey, mackeylen))
  303. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  304. if (!BIO_set_ktls(rl->bio, &crypto_info, rl->direction))
  305. return OSSL_RECORD_RETURN_NON_FATAL_ERR;
  306. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE &&
  307. (rl->options & SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE) != 0)
  308. /* Ignore errors. The application opts in to using the zerocopy
  309. * optimization. If the running kernel doesn't support it, just
  310. * continue without the optimization.
  311. */
  312. BIO_set_ktls_tx_zerocopy_sendfile(rl->bio);
  313. return OSSL_RECORD_RETURN_SUCCESS;
  314. }
  315. static int ktls_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
  316. int clearold, size_t *readbytes)
  317. {
  318. int ret;
  319. ret = tls_default_read_n(rl, n, max, extend, clearold, readbytes);
  320. if (ret < OSSL_RECORD_RETURN_RETRY) {
  321. switch (errno) {
  322. case EBADMSG:
  323. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
  324. SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  325. break;
  326. case EMSGSIZE:
  327. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  328. SSL_R_PACKET_LENGTH_TOO_LONG);
  329. break;
  330. case EINVAL:
  331. RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
  332. SSL_R_WRONG_VERSION_NUMBER);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. return ret;
  339. }
  340. static int ktls_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *inrecs,
  341. size_t n_recs, int sending, SSL_MAC_BUF *mac,
  342. size_t macsize)
  343. {
  344. return 1;
  345. }
  346. static int ktls_validate_record_header(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  347. {
  348. if (rec->rec_version != TLS1_2_VERSION) {
  349. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_WRONG_VERSION_NUMBER);
  350. return 0;
  351. }
  352. return 1;
  353. }
  354. static int ktls_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  355. {
  356. if (rl->version == TLS1_3_VERSION)
  357. return tls13_common_post_process_record(rl, rec);
  358. return 1;
  359. }
  360. static int
  361. ktls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  362. int role, int direction, int level, uint16_t epoch,
  363. unsigned char *secret, size_t secretlen,
  364. unsigned char *key, size_t keylen, unsigned char *iv,
  365. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  366. const EVP_CIPHER *ciph, size_t taglen,
  367. int mactype,
  368. const EVP_MD *md, COMP_METHOD *comp,
  369. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  370. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  371. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  372. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  373. OSSL_RECORD_LAYER **retrl)
  374. {
  375. int ret;
  376. ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
  377. ciph, taglen, md, comp, prev,
  378. transport, next, settings,
  379. options, fns, cbarg, retrl);
  380. if (ret != OSSL_RECORD_RETURN_SUCCESS)
  381. return ret;
  382. (*retrl)->funcs = &ossl_ktls_funcs;
  383. ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
  384. ivlen, mackey, mackeylen, ciph,
  385. taglen, mactype, md, comp);
  386. if (ret != OSSL_RECORD_RETURN_SUCCESS) {
  387. OPENSSL_free(*retrl);
  388. *retrl = NULL;
  389. } else {
  390. /*
  391. * With KTLS we always try and read as much as possible and fill the
  392. * buffer
  393. */
  394. (*retrl)->read_ahead = 1;
  395. }
  396. return ret;
  397. }
  398. static int ktls_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
  399. OSSL_RECORD_TEMPLATE *templates,
  400. size_t numtempl, size_t *prefix)
  401. {
  402. if (!ossl_assert(numtempl == 1))
  403. return 0;
  404. /*
  405. * We just use the end application buffer in the case of KTLS, so nothing
  406. * to do. We pretend we set up one buffer.
  407. */
  408. rl->numwpipes = 1;
  409. return 1;
  410. }
  411. static int ktls_initialise_write_packets(OSSL_RECORD_LAYER *rl,
  412. OSSL_RECORD_TEMPLATE *templates,
  413. size_t numtempl,
  414. OSSL_RECORD_TEMPLATE *prefixtempl,
  415. WPACKET *pkt,
  416. TLS_BUFFER *bufs,
  417. size_t *wpinited)
  418. {
  419. TLS_BUFFER *wb;
  420. /*
  421. * We just use the application buffer directly and don't use any WPACKET
  422. * structures
  423. */
  424. wb = &bufs[0];
  425. wb->type = templates[0].type;
  426. /*
  427. * ktls doesn't modify the buffer, but to avoid a warning we need
  428. * to discard the const qualifier.
  429. * This doesn't leak memory because the buffers have never been allocated
  430. * with KTLS
  431. */
  432. TLS_BUFFER_set_buf(wb, (unsigned char *)templates[0].buf);
  433. TLS_BUFFER_set_offset(wb, 0);
  434. TLS_BUFFER_set_app_buffer(wb, 1);
  435. return 1;
  436. }
  437. static int ktls_prepare_record_header(OSSL_RECORD_LAYER *rl,
  438. WPACKET *thispkt,
  439. OSSL_RECORD_TEMPLATE *templ,
  440. uint8_t rectype,
  441. unsigned char **recdata)
  442. {
  443. /* The kernel writes the record header, so nothing to do */
  444. *recdata = NULL;
  445. return 1;
  446. }
  447. static int ktls_prepare_for_encryption(OSSL_RECORD_LAYER *rl,
  448. size_t mac_size,
  449. WPACKET *thispkt,
  450. TLS_RL_RECORD *thiswr)
  451. {
  452. /* No encryption, so nothing to do */
  453. return 1;
  454. }
  455. static int ktls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
  456. size_t mac_size,
  457. OSSL_RECORD_TEMPLATE *templ,
  458. WPACKET *thispkt,
  459. TLS_RL_RECORD *thiswr)
  460. {
  461. /* The kernel does anything that is needed, so nothing to do here */
  462. return 1;
  463. }
  464. static int ktls_prepare_write_bio(OSSL_RECORD_LAYER *rl, int type)
  465. {
  466. /*
  467. * To prevent coalescing of control and data messages,
  468. * such as in buffer_write, we flush the BIO
  469. */
  470. if (type != SSL3_RT_APPLICATION_DATA) {
  471. int ret, i = BIO_flush(rl->bio);
  472. if (i <= 0) {
  473. if (BIO_should_retry(rl->bio))
  474. ret = OSSL_RECORD_RETURN_RETRY;
  475. else
  476. ret = OSSL_RECORD_RETURN_FATAL;
  477. return ret;
  478. }
  479. BIO_set_ktls_ctrl_msg(rl->bio, type);
  480. }
  481. return OSSL_RECORD_RETURN_SUCCESS;
  482. }
  483. static int ktls_alloc_buffers(OSSL_RECORD_LAYER *rl)
  484. {
  485. /* We use the application buffer directly for writing */
  486. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE)
  487. return 1;
  488. return tls_alloc_buffers(rl);
  489. }
  490. static int ktls_free_buffers(OSSL_RECORD_LAYER *rl)
  491. {
  492. /* We use the application buffer directly for writing */
  493. if (rl->direction == OSSL_RECORD_DIRECTION_WRITE)
  494. return 1;
  495. return tls_free_buffers(rl);
  496. }
  497. static struct record_functions_st ossl_ktls_funcs = {
  498. ktls_set_crypto_state,
  499. ktls_cipher,
  500. NULL,
  501. tls_default_set_protocol_version,
  502. ktls_read_n,
  503. tls_get_more_records,
  504. ktls_validate_record_header,
  505. ktls_post_process_record,
  506. tls_get_max_records_default,
  507. tls_write_records_default,
  508. ktls_allocate_write_buffers,
  509. ktls_initialise_write_packets,
  510. NULL,
  511. ktls_prepare_record_header,
  512. NULL,
  513. ktls_prepare_for_encryption,
  514. ktls_post_encryption_processing,
  515. ktls_prepare_write_bio
  516. };
  517. const OSSL_RECORD_METHOD ossl_ktls_record_method = {
  518. ktls_new_record_layer,
  519. tls_free,
  520. tls_unprocessed_read_pending,
  521. tls_processed_read_pending,
  522. tls_app_data_pending,
  523. tls_get_max_records,
  524. tls_write_records,
  525. tls_retry_write_records,
  526. tls_read_record,
  527. tls_release_record,
  528. tls_get_alert_code,
  529. tls_set1_bio,
  530. tls_set_protocol_version,
  531. tls_set_plain_alerts,
  532. tls_set_first_handshake,
  533. tls_set_max_pipelines,
  534. NULL,
  535. tls_get_state,
  536. tls_set_options,
  537. tls_get_compression,
  538. tls_set_max_frag_len,
  539. NULL,
  540. tls_increment_sequence_ctr,
  541. ktls_alloc_buffers,
  542. ktls_free_buffers
  543. };