tls13_meth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2022-2023 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 "../../ssl_local.h"
  12. #include "../record_local.h"
  13. #include "recmethod_local.h"
  14. static int tls13_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
  15. unsigned char *key, size_t keylen,
  16. unsigned char *iv, size_t ivlen,
  17. unsigned char *mackey, size_t mackeylen,
  18. const EVP_CIPHER *ciph,
  19. size_t taglen,
  20. int mactype,
  21. const EVP_MD *md,
  22. COMP_METHOD *comp)
  23. {
  24. EVP_CIPHER_CTX *ciph_ctx;
  25. int mode;
  26. int enc = (rl->direction == OSSL_RECORD_DIRECTION_WRITE) ? 1 : 0;
  27. if (ivlen > sizeof(rl->iv)) {
  28. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  29. return OSSL_RECORD_RETURN_FATAL;
  30. }
  31. memcpy(rl->iv, iv, ivlen);
  32. ciph_ctx = rl->enc_ctx = EVP_CIPHER_CTX_new();
  33. if (ciph_ctx == NULL) {
  34. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  35. return OSSL_RECORD_RETURN_FATAL;
  36. }
  37. mode = EVP_CIPHER_get_mode(ciph);
  38. if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, enc) <= 0
  39. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen,
  40. NULL) <= 0
  41. || (mode == EVP_CIPH_CCM_MODE
  42. && EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
  43. NULL) <= 0)
  44. || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, enc) <= 0) {
  45. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  46. return OSSL_RECORD_RETURN_FATAL;
  47. }
  48. return OSSL_RECORD_RETURN_SUCCESS;
  49. }
  50. static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
  51. size_t n_recs, int sending, SSL_MAC_BUF *mac,
  52. size_t macsize)
  53. {
  54. EVP_CIPHER_CTX *ctx;
  55. unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
  56. size_t ivlen, offset, loop, hdrlen;
  57. unsigned char *staticiv;
  58. unsigned char *seq = rl->sequence;
  59. int lenu, lenf;
  60. TLS_RL_RECORD *rec = &recs[0];
  61. WPACKET wpkt;
  62. const EVP_CIPHER *cipher;
  63. int mode;
  64. if (n_recs != 1) {
  65. /* Should not happen */
  66. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  67. return 0;
  68. }
  69. ctx = rl->enc_ctx;
  70. staticiv = rl->iv;
  71. cipher = EVP_CIPHER_CTX_get0_cipher(ctx);
  72. if (cipher == NULL) {
  73. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  74. return 0;
  75. }
  76. mode = EVP_CIPHER_get_mode(cipher);
  77. /*
  78. * If we're sending an alert and ctx != NULL then we must be forcing
  79. * plaintext alerts. If we're reading and ctx != NULL then we allow
  80. * plaintext alerts at certain points in the handshake. If we've got this
  81. * far then we have already validated that a plaintext alert is ok here.
  82. */
  83. if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
  84. memmove(rec->data, rec->input, rec->length);
  85. rec->input = rec->data;
  86. return 1;
  87. }
  88. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  89. if (!sending) {
  90. /*
  91. * Take off tag. There must be at least one byte of content type as
  92. * well as the tag
  93. */
  94. if (rec->length < rl->taglen + 1)
  95. return 0;
  96. rec->length -= rl->taglen;
  97. }
  98. /* Set up IV */
  99. if (ivlen < SEQ_NUM_SIZE) {
  100. /* Should not happen */
  101. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  102. return 0;
  103. }
  104. offset = ivlen - SEQ_NUM_SIZE;
  105. memcpy(iv, staticiv, offset);
  106. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  107. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  108. if (!tls_increment_sequence_ctr(rl)) {
  109. /* RLAYERfatal already called */
  110. return 0;
  111. }
  112. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
  113. || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  114. rl->taglen,
  115. rec->data + rec->length) <= 0)) {
  116. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  117. return 0;
  118. }
  119. /* Set up the AAD */
  120. if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
  121. || !WPACKET_put_bytes_u8(&wpkt, rec->type)
  122. || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
  123. || !WPACKET_put_bytes_u16(&wpkt, rec->length + rl->taglen)
  124. || !WPACKET_get_total_written(&wpkt, &hdrlen)
  125. || hdrlen != SSL3_RT_HEADER_LENGTH
  126. || !WPACKET_finish(&wpkt)) {
  127. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  128. WPACKET_cleanup(&wpkt);
  129. return 0;
  130. }
  131. /*
  132. * For CCM we must explicitly set the total plaintext length before we add
  133. * any AAD.
  134. */
  135. if ((mode == EVP_CIPH_CCM_MODE
  136. && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
  137. (unsigned int)rec->length) <= 0)
  138. || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
  139. sizeof(recheader)) <= 0
  140. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  141. (unsigned int)rec->length) <= 0
  142. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  143. || (size_t)(lenu + lenf) != rec->length) {
  144. return 0;
  145. }
  146. if (sending) {
  147. /* Add the tag */
  148. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, rl->taglen,
  149. rec->data + rec->length) <= 0) {
  150. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  151. return 0;
  152. }
  153. rec->length += rl->taglen;
  154. }
  155. return 1;
  156. }
  157. static int tls13_validate_record_header(OSSL_RECORD_LAYER *rl,
  158. TLS_RL_RECORD *rec)
  159. {
  160. if (rec->type != SSL3_RT_APPLICATION_DATA
  161. && (rec->type != SSL3_RT_CHANGE_CIPHER_SPEC
  162. || !rl->is_first_handshake)
  163. && (rec->type != SSL3_RT_ALERT || !rl->allow_plain_alerts)) {
  164. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
  165. return 0;
  166. }
  167. if (rec->rec_version != TLS1_2_VERSION) {
  168. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_WRONG_VERSION_NUMBER);
  169. return 0;
  170. }
  171. if (rec->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
  172. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
  173. SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. static int tls13_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  179. {
  180. /* Skip this if we've received a plaintext alert */
  181. if (rec->type != SSL3_RT_ALERT) {
  182. size_t end;
  183. if (rec->length == 0
  184. || rec->type != SSL3_RT_APPLICATION_DATA) {
  185. RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
  186. SSL_R_BAD_RECORD_TYPE);
  187. return 0;
  188. }
  189. /* Strip trailing padding */
  190. for (end = rec->length - 1; end > 0 && rec->data[end] == 0; end--)
  191. continue;
  192. rec->length = end;
  193. rec->type = rec->data[end];
  194. }
  195. if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
  196. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  197. return 0;
  198. }
  199. if (!tls13_common_post_process_record(rl, rec)) {
  200. /* RLAYERfatal already called */
  201. return 0;
  202. }
  203. return 1;
  204. }
  205. static uint8_t tls13_get_record_type(OSSL_RECORD_LAYER *rl,
  206. OSSL_RECORD_TEMPLATE *template)
  207. {
  208. if (rl->allow_plain_alerts && template->type == SSL3_RT_ALERT)
  209. return SSL3_RT_ALERT;
  210. /*
  211. * Aside from the above case we always use the application data record type
  212. * when encrypting in TLSv1.3. The "inner" record type encodes the "real"
  213. * record type from the template.
  214. */
  215. return SSL3_RT_APPLICATION_DATA;
  216. }
  217. static int tls13_add_record_padding(OSSL_RECORD_LAYER *rl,
  218. OSSL_RECORD_TEMPLATE *thistempl,
  219. WPACKET *thispkt,
  220. TLS_RL_RECORD *thiswr)
  221. {
  222. size_t rlen;
  223. /* Nothing to be done in the case of a plaintext alert */
  224. if (rl->allow_plain_alerts && thistempl->type != SSL3_RT_ALERT)
  225. return 1;
  226. if (!WPACKET_put_bytes_u8(thispkt, thistempl->type)) {
  227. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  228. return 0;
  229. }
  230. TLS_RL_RECORD_add_length(thiswr, 1);
  231. /* Add TLS1.3 padding */
  232. rlen = TLS_RL_RECORD_get_length(thiswr);
  233. if (rlen < rl->max_frag_len) {
  234. size_t padding = 0;
  235. size_t max_padding = rl->max_frag_len - rlen;
  236. if (rl->padding != NULL) {
  237. padding = rl->padding(rl->cbarg, thistempl->type, rlen);
  238. } else if (rl->block_padding > 0) {
  239. size_t mask = rl->block_padding - 1;
  240. size_t remainder;
  241. /* optimize for power of 2 */
  242. if ((rl->block_padding & mask) == 0)
  243. remainder = rlen & mask;
  244. else
  245. remainder = rlen % rl->block_padding;
  246. /* don't want to add a block of padding if we don't have to */
  247. if (remainder == 0)
  248. padding = 0;
  249. else
  250. padding = rl->block_padding - remainder;
  251. }
  252. if (padding > 0) {
  253. /* do not allow the record to exceed max plaintext length */
  254. if (padding > max_padding)
  255. padding = max_padding;
  256. if (!WPACKET_memset(thispkt, 0, padding)) {
  257. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR,
  258. ERR_R_INTERNAL_ERROR);
  259. return 0;
  260. }
  261. TLS_RL_RECORD_add_length(thiswr, padding);
  262. }
  263. }
  264. return 1;
  265. }
  266. struct record_functions_st tls_1_3_funcs = {
  267. tls13_set_crypto_state,
  268. tls13_cipher,
  269. NULL,
  270. tls_default_set_protocol_version,
  271. tls_default_read_n,
  272. tls_get_more_records,
  273. tls13_validate_record_header,
  274. tls13_post_process_record,
  275. tls_get_max_records_default,
  276. tls_write_records_default,
  277. tls_allocate_write_buffers_default,
  278. tls_initialise_write_packets_default,
  279. tls13_get_record_type,
  280. tls_prepare_record_header_default,
  281. tls13_add_record_padding,
  282. tls_prepare_for_encryption_default,
  283. tls_post_encryption_processing_default,
  284. NULL
  285. };