2
0

ktls.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2018-2020 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. #if defined(__FreeBSD__)
  12. # include <crypto/cryptodev.h>
  13. /*-
  14. * Check if a given cipher is supported by the KTLS interface.
  15. * The kernel might still fail the setsockopt() if no suitable
  16. * provider is found, but this checks if the socket option
  17. * supports the cipher suite used at all.
  18. */
  19. int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
  20. const EVP_CIPHER_CTX *dd)
  21. {
  22. switch (s->version) {
  23. case TLS1_VERSION:
  24. case TLS1_1_VERSION:
  25. case TLS1_2_VERSION:
  26. case TLS1_3_VERSION:
  27. break;
  28. default:
  29. return 0;
  30. }
  31. switch (s->s3.tmp.new_cipher->algorithm_enc) {
  32. case SSL_AES128GCM:
  33. case SSL_AES256GCM:
  34. return 1;
  35. case SSL_AES128:
  36. case SSL_AES256:
  37. if (s->ext.use_etm)
  38. return 0;
  39. switch (s->s3.tmp.new_cipher->algorithm_mac) {
  40. case SSL_SHA1:
  41. case SSL_SHA256:
  42. case SSL_SHA384:
  43. return 1;
  44. default:
  45. return 0;
  46. }
  47. default:
  48. return 0;
  49. }
  50. }
  51. /* Function to configure kernel TLS structure */
  52. int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
  53. void *rl_sequence, ktls_crypto_info_t *crypto_info,
  54. unsigned char **rec_seq, unsigned char *iv,
  55. unsigned char *key, unsigned char *mac_key,
  56. size_t mac_secret_size)
  57. {
  58. memset(crypto_info, 0, sizeof(*crypto_info));
  59. switch (s->s3.tmp.new_cipher->algorithm_enc) {
  60. case SSL_AES128GCM:
  61. case SSL_AES256GCM:
  62. crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
  63. if (s->version == TLS1_3_VERSION)
  64. crypto_info->iv_len = EVP_CIPHER_CTX_iv_length(dd);
  65. else
  66. crypto_info->iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  67. break;
  68. case SSL_AES128:
  69. case SSL_AES256:
  70. switch (s->s3.tmp.new_cipher->algorithm_mac) {
  71. case SSL_SHA1:
  72. crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
  73. break;
  74. case SSL_SHA256:
  75. crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
  76. break;
  77. case SSL_SHA384:
  78. crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
  79. break;
  80. default:
  81. return 0;
  82. }
  83. crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
  84. crypto_info->iv_len = EVP_CIPHER_iv_length(c);
  85. crypto_info->auth_key = mac_key;
  86. crypto_info->auth_key_len = mac_secret_size;
  87. break;
  88. default:
  89. return 0;
  90. }
  91. crypto_info->cipher_key = key;
  92. crypto_info->cipher_key_len = EVP_CIPHER_key_length(c);
  93. crypto_info->iv = iv;
  94. crypto_info->tls_vmajor = (s->version >> 8) & 0x000000ff;
  95. crypto_info->tls_vminor = (s->version & 0x000000ff);
  96. # ifdef TCP_RXTLS_ENABLE
  97. memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
  98. if (rec_seq != NULL)
  99. *rec_seq = crypto_info->rec_seq;
  100. # else
  101. if (rec_seq != NULL)
  102. *rec_seq = NULL;
  103. # endif
  104. return 1;
  105. };
  106. #endif /* __FreeBSD__ */
  107. #if defined(OPENSSL_SYS_LINUX)
  108. /* Function to check supported ciphers in Linux */
  109. int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
  110. const EVP_CIPHER_CTX *dd)
  111. {
  112. switch (s->version) {
  113. case TLS1_2_VERSION:
  114. case TLS1_3_VERSION:
  115. break;
  116. default:
  117. return 0;
  118. }
  119. /* check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128 */
  120. switch (EVP_CIPHER_nid(c))
  121. {
  122. # ifdef OPENSSL_KTLS_AES_CCM_128
  123. case NID_aes_128_ccm:
  124. if (EVP_CIPHER_CTX_tag_length(dd) != EVP_CCM_TLS_TAG_LEN)
  125. return 0;
  126. # endif
  127. # ifdef OPENSSL_KTLS_AES_GCM_128
  128. case NID_aes_128_gcm:
  129. # endif
  130. # ifdef OPENSSL_KTLS_AES_GCM_256
  131. case NID_aes_256_gcm:
  132. # endif
  133. return 1;
  134. default:
  135. return 0;
  136. }
  137. }
  138. /* Function to configure kernel TLS structure */
  139. int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
  140. void *rl_sequence, ktls_crypto_info_t *crypto_info,
  141. unsigned char **rec_seq, unsigned char *iv,
  142. unsigned char *key, unsigned char *mac_key,
  143. size_t mac_secret_size)
  144. {
  145. unsigned char geniv[12];
  146. unsigned char *iiv = iv;
  147. if (s->version == TLS1_2_VERSION &&
  148. EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
  149. if (!EVP_CIPHER_CTX_get_iv_state(dd, geniv,
  150. EVP_GCM_TLS_FIXED_IV_LEN
  151. + EVP_GCM_TLS_EXPLICIT_IV_LEN))
  152. return 0;
  153. iiv = geniv;
  154. }
  155. memset(crypto_info, 0, sizeof(*crypto_info));
  156. switch (EVP_CIPHER_nid(c))
  157. {
  158. # ifdef OPENSSL_KTLS_AES_GCM_128
  159. case NID_aes_128_gcm:
  160. crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
  161. crypto_info->gcm128.info.version = s->version;
  162. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
  163. memcpy(crypto_info->gcm128.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
  164. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  165. memcpy(crypto_info->gcm128.salt, iiv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  166. memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_key_length(c));
  167. memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
  168. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  169. if (rec_seq != NULL)
  170. *rec_seq = crypto_info->gcm128.rec_seq;
  171. return 1;
  172. # endif
  173. # ifdef OPENSSL_KTLS_AES_GCM_256
  174. case NID_aes_256_gcm:
  175. crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
  176. crypto_info->gcm256.info.version = s->version;
  177. crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
  178. memcpy(crypto_info->gcm256.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
  179. TLS_CIPHER_AES_GCM_256_IV_SIZE);
  180. memcpy(crypto_info->gcm256.salt, iiv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
  181. memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_key_length(c));
  182. memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
  183. TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
  184. if (rec_seq != NULL)
  185. *rec_seq = crypto_info->gcm256.rec_seq;
  186. return 1;
  187. # endif
  188. # ifdef OPENSSL_KTLS_AES_CCM_128
  189. case NID_aes_128_ccm:
  190. crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
  191. crypto_info->ccm128.info.version = s->version;
  192. crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
  193. memcpy(crypto_info->ccm128.iv, iiv + EVP_CCM_TLS_FIXED_IV_LEN,
  194. TLS_CIPHER_AES_CCM_128_IV_SIZE);
  195. memcpy(crypto_info->ccm128.salt, iiv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
  196. memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_key_length(c));
  197. memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
  198. TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
  199. if (rec_seq != NULL)
  200. *rec_seq = crypto_info->ccm128.rec_seq;
  201. return 1;
  202. # endif
  203. default:
  204. return 0;
  205. }
  206. }
  207. #endif /* OPENSSL_SYS_LINUX */