ssl3_record_tls13.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  36. return 0;
  37. }
  38. if (sending) {
  39. ctx = s->enc_write_ctx;
  40. staticiv = s->write_iv;
  41. seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
  42. } else {
  43. ctx = s->enc_read_ctx;
  44. staticiv = s->read_iv;
  45. seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
  46. }
  47. /*
  48. * If we're sending an alert and ctx != NULL then we must be forcing
  49. * plaintext alerts. If we're reading and ctx != NULL then we allow
  50. * plaintext alerts at certain points in the handshake. If we've got this
  51. * far then we have already validated that a plaintext alert is ok here.
  52. */
  53. if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
  54. memmove(rec->data, rec->input, rec->length);
  55. rec->input = rec->data;
  56. return 1;
  57. }
  58. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  59. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  60. || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
  61. if (s->session != NULL && s->session->ext.max_early_data > 0) {
  62. alg_enc = s->session->cipher->algorithm_enc;
  63. } else {
  64. if (!ossl_assert(s->psksession != NULL
  65. && s->psksession->ext.max_early_data > 0)) {
  66. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  67. return 0;
  68. }
  69. alg_enc = s->psksession->cipher->algorithm_enc;
  70. }
  71. } else {
  72. /*
  73. * To get here we must have selected a ciphersuite - otherwise ctx would
  74. * be NULL
  75. */
  76. if (!ossl_assert(s->s3.tmp.new_cipher != NULL)) {
  77. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  78. return 0;
  79. }
  80. alg_enc = s->s3.tmp.new_cipher->algorithm_enc;
  81. }
  82. if (alg_enc & SSL_AESCCM) {
  83. if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  84. taglen = EVP_CCM8_TLS_TAG_LEN;
  85. else
  86. taglen = EVP_CCM_TLS_TAG_LEN;
  87. if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
  88. NULL) <= 0) {
  89. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  90. return 0;
  91. }
  92. } else if (alg_enc & SSL_AESGCM) {
  93. taglen = EVP_GCM_TLS_TAG_LEN;
  94. } else if (alg_enc & SSL_CHACHA20) {
  95. taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  96. } else {
  97. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  98. return 0;
  99. }
  100. if (!sending) {
  101. /*
  102. * Take off tag. There must be at least one byte of content type as
  103. * well as the tag
  104. */
  105. if (rec->length < taglen + 1)
  106. return 0;
  107. rec->length -= taglen;
  108. }
  109. /* Set up IV */
  110. if (ivlen < SEQ_NUM_SIZE) {
  111. /* Should not happen */
  112. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  113. return 0;
  114. }
  115. offset = ivlen - SEQ_NUM_SIZE;
  116. memcpy(iv, staticiv, offset);
  117. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  118. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  119. /* Increment the sequence counter */
  120. for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
  121. ++seq[loop - 1];
  122. if (seq[loop - 1] != 0)
  123. break;
  124. }
  125. if (loop == 0) {
  126. /* Sequence has wrapped */
  127. return 0;
  128. }
  129. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
  130. || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  131. taglen,
  132. rec->data + rec->length) <= 0)) {
  133. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  134. return 0;
  135. }
  136. /* Set up the AAD */
  137. if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
  138. || !WPACKET_put_bytes_u8(&wpkt, rec->type)
  139. || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
  140. || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
  141. || !WPACKET_get_total_written(&wpkt, &hdrlen)
  142. || hdrlen != SSL3_RT_HEADER_LENGTH
  143. || !WPACKET_finish(&wpkt)) {
  144. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  145. WPACKET_cleanup(&wpkt);
  146. return 0;
  147. }
  148. /*
  149. * For CCM we must explicitly set the total plaintext length before we add
  150. * any AAD.
  151. */
  152. if (((alg_enc & SSL_AESCCM) != 0
  153. && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
  154. (unsigned int)rec->length) <= 0)
  155. || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
  156. sizeof(recheader)) <= 0
  157. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  158. (unsigned int)rec->length) <= 0
  159. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  160. || (size_t)(lenu + lenf) != rec->length) {
  161. return 0;
  162. }
  163. if (sending) {
  164. /* Add the tag */
  165. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  166. rec->data + rec->length) <= 0) {
  167. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  168. return 0;
  169. }
  170. rec->length += taglen;
  171. }
  172. return 1;
  173. }