ssl3_record_tls13.c 6.8 KB

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