ssl3_record_tls13.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2016-2021 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 <assert.h>
  10. #include "../ssl_local.h"
  11. #include "record_local.h"
  12. #include "internal/cryptlib.h"
  13. /*-
  14. * tls13_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
  15. * error, but not otherwise. It is the responsibility of the caller to report
  16. * a bad_record_mac.
  17. *
  18. * Returns:
  19. * 0: On failure
  20. * 1: if the record encryption/decryption was successful.
  21. */
  22. int tls13_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs, int sending,
  23. ossl_unused SSL_MAC_BUF *mac, ossl_unused size_t macsize)
  24. {
  25. EVP_CIPHER_CTX *ctx;
  26. unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
  27. size_t taglen, offset, loop, hdrlen;
  28. int ivlen;
  29. unsigned char *staticiv;
  30. unsigned char *seq;
  31. int lenu, lenf;
  32. SSL3_RECORD *rec = &recs[0];
  33. uint32_t alg_enc;
  34. WPACKET wpkt;
  35. if (n_recs != 1) {
  36. /* Should not happen */
  37. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  38. return 0;
  39. }
  40. assert(sending);
  41. ctx = s->enc_write_ctx;
  42. staticiv = s->write_iv;
  43. seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
  44. /*
  45. * If we're sending an alert and ctx != NULL then we must be forcing
  46. * plaintext alerts. If we're reading and ctx != NULL then we allow
  47. * plaintext alerts at certain points in the handshake. If we've got this
  48. * far then we have already validated that a plaintext alert is ok here.
  49. */
  50. if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
  51. memmove(rec->data, rec->input, rec->length);
  52. rec->input = rec->data;
  53. return 1;
  54. }
  55. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  56. if (ivlen < 0) {
  57. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  58. return 0;
  59. }
  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 (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, 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. /* Set up IV */
  101. if (ivlen < SEQ_NUM_SIZE) {
  102. /* Should not happen */
  103. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  104. return 0;
  105. }
  106. offset = ivlen - SEQ_NUM_SIZE;
  107. memcpy(iv, staticiv, offset);
  108. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  109. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  110. /* Increment the sequence counter */
  111. for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
  112. ++seq[loop - 1];
  113. if (seq[loop - 1] != 0)
  114. break;
  115. }
  116. if (loop == 0) {
  117. /* Sequence has wrapped */
  118. return 0;
  119. }
  120. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0) {
  121. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  122. return 0;
  123. }
  124. /* Set up the AAD */
  125. if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
  126. || !WPACKET_put_bytes_u8(&wpkt, rec->type)
  127. || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
  128. || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
  129. || !WPACKET_get_total_written(&wpkt, &hdrlen)
  130. || hdrlen != SSL3_RT_HEADER_LENGTH
  131. || !WPACKET_finish(&wpkt)) {
  132. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  133. WPACKET_cleanup(&wpkt);
  134. return 0;
  135. }
  136. /*
  137. * For CCM we must explicitly set the total plaintext length before we add
  138. * any AAD.
  139. */
  140. if (((alg_enc & SSL_AESCCM) != 0
  141. && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
  142. (unsigned int)rec->length) <= 0)
  143. || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
  144. sizeof(recheader)) <= 0
  145. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  146. (unsigned int)rec->length) <= 0
  147. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  148. || (size_t)(lenu + lenf) != rec->length) {
  149. return 0;
  150. }
  151. /* Add the tag */
  152. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  153. rec->data + rec->length) <= 0) {
  154. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  155. return 0;
  156. }
  157. rec->length += taglen;
  158. return 1;
  159. }