ssl3_record_tls13.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2016-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 "record_local.h"
  11. #include "internal/cryptlib.h"
  12. /*-
  13. * tls13_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
  14. * error, but not otherwise. It is the responsibility of the caller to report
  15. * a bad_record_mac.
  16. *
  17. * Returns:
  18. * 0: On failure
  19. * 1: if the record encryption/decryption was successful.
  20. */
  21. int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
  22. ossl_unused SSL_MAC_BUF *mac, ossl_unused size_t macsize)
  23. {
  24. EVP_CIPHER_CTX *ctx;
  25. unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
  26. size_t ivlen, taglen, offset, loop, hdrlen;
  27. unsigned char *staticiv;
  28. unsigned char *seq;
  29. int lenu, lenf;
  30. SSL3_RECORD *rec = &recs[0];
  31. uint32_t alg_enc;
  32. WPACKET wpkt;
  33. if (n_recs != 1) {
  34. /* Should not happen */
  35. /* TODO(TLS1.3): Support pipelining */
  36. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  37. return 0;
  38. }
  39. if (sending) {
  40. ctx = s->enc_write_ctx;
  41. staticiv = s->write_iv;
  42. seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
  43. } else {
  44. ctx = s->enc_read_ctx;
  45. staticiv = s->read_iv;
  46. seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
  47. }
  48. /*
  49. * If we're sending an alert and ctx != NULL then we must be forcing
  50. * plaintext alerts. If we're reading and ctx != NULL then we allow
  51. * plaintext alerts at certain points in the handshake. If we've got this
  52. * far then we have already validated that a plaintext alert is ok here.
  53. */
  54. if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
  55. memmove(rec->data, rec->input, rec->length);
  56. rec->input = rec->data;
  57. return 1;
  58. }
  59. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  60. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  61. || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
  62. if (s->session != NULL && s->session->ext.max_early_data > 0) {
  63. alg_enc = s->session->cipher->algorithm_enc;
  64. } else {
  65. if (!ossl_assert(s->psksession != NULL
  66. && s->psksession->ext.max_early_data > 0)) {
  67. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  68. return 0;
  69. }
  70. alg_enc = s->psksession->cipher->algorithm_enc;
  71. }
  72. } else {
  73. /*
  74. * To get here we must have selected a ciphersuite - otherwise ctx would
  75. * be NULL
  76. */
  77. if (!ossl_assert(s->s3.tmp.new_cipher != NULL)) {
  78. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  79. return 0;
  80. }
  81. alg_enc = s->s3.tmp.new_cipher->algorithm_enc;
  82. }
  83. if (alg_enc & SSL_AESCCM) {
  84. if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  85. taglen = EVP_CCM8_TLS_TAG_LEN;
  86. else
  87. taglen = EVP_CCM_TLS_TAG_LEN;
  88. if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
  89. NULL) <= 0) {
  90. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  91. return 0;
  92. }
  93. } else if (alg_enc & SSL_AESGCM) {
  94. taglen = EVP_GCM_TLS_TAG_LEN;
  95. } else if (alg_enc & SSL_CHACHA20) {
  96. taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  97. } else {
  98. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  99. return 0;
  100. }
  101. if (!sending) {
  102. /*
  103. * Take off tag. There must be at least one byte of content type as
  104. * well as the tag
  105. */
  106. if (rec->length < taglen + 1)
  107. return 0;
  108. rec->length -= taglen;
  109. }
  110. /* Set up IV */
  111. if (ivlen < SEQ_NUM_SIZE) {
  112. /* Should not happen */
  113. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  114. return 0;
  115. }
  116. offset = ivlen - SEQ_NUM_SIZE;
  117. memcpy(iv, staticiv, offset);
  118. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  119. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  120. /* Increment the sequence counter */
  121. for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
  122. ++seq[loop - 1];
  123. if (seq[loop - 1] != 0)
  124. break;
  125. }
  126. if (loop == 0) {
  127. /* Sequence has wrapped */
  128. return 0;
  129. }
  130. /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
  131. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
  132. || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  133. taglen,
  134. rec->data + rec->length) <= 0)) {
  135. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  136. return 0;
  137. }
  138. /* Set up the AAD */
  139. if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
  140. || !WPACKET_put_bytes_u8(&wpkt, rec->type)
  141. || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
  142. || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
  143. || !WPACKET_get_total_written(&wpkt, &hdrlen)
  144. || hdrlen != SSL3_RT_HEADER_LENGTH
  145. || !WPACKET_finish(&wpkt)) {
  146. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  147. WPACKET_cleanup(&wpkt);
  148. return 0;
  149. }
  150. /*
  151. * For CCM we must explicitly set the total plaintext length before we add
  152. * any AAD.
  153. */
  154. if (((alg_enc & SSL_AESCCM) != 0
  155. && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
  156. (unsigned int)rec->length) <= 0)
  157. || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
  158. sizeof(recheader)) <= 0
  159. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  160. (unsigned int)rec->length) <= 0
  161. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  162. || (size_t)(lenu + lenf) != rec->length) {
  163. return 0;
  164. }
  165. if (sending) {
  166. /* Add the tag */
  167. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  168. rec->data + rec->length) <= 0) {
  169. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  170. return 0;
  171. }
  172. rec->length += taglen;
  173. }
  174. return 1;
  175. }