ssl3_record_tls13.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_locl.h"
  10. #include "record_locl.h"
  11. #include "internal/cryptlib.h"
  12. /*-
  13. * tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
  14. * internal errors, but not otherwise.
  15. *
  16. * Returns:
  17. * 0: (in non-constant time) if the record is publically invalid (i.e. too
  18. * short etc).
  19. * 1: if the record encryption was successful.
  20. * -1: if the record's AEAD-authenticator is invalid or, if sending,
  21. * an internal error occurred.
  22. */
  23. int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
  24. {
  25. EVP_CIPHER_CTX *ctx;
  26. unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
  27. size_t ivlen, taglen, offset, loop, hdrlen;
  28. unsigned char *staticiv;
  29. unsigned char *seq;
  30. int lenu, lenf;
  31. SSL3_RECORD *rec = &recs[0];
  32. uint32_t alg_enc;
  33. WPACKET wpkt;
  34. if (n_recs != 1) {
  35. /* Should not happen */
  36. /* TODO(TLS1.3): Support pipelining */
  37. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
  38. ERR_R_INTERNAL_ERROR);
  39. return -1;
  40. }
  41. if (sending) {
  42. ctx = s->enc_write_ctx;
  43. staticiv = s->write_iv;
  44. seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
  45. } else {
  46. ctx = s->enc_read_ctx;
  47. staticiv = s->read_iv;
  48. seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
  49. }
  50. if (ctx == NULL) {
  51. memmove(rec->data, rec->input, rec->length);
  52. rec->input = rec->data;
  53. return 1;
  54. }
  55. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  56. if (s->early_data_state == SSL_EARLY_DATA_WRITING
  57. || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
  58. if (s->session != NULL && s->session->ext.max_early_data > 0) {
  59. alg_enc = s->session->cipher->algorithm_enc;
  60. } else {
  61. if (!ossl_assert(s->psksession != NULL
  62. && s->psksession->ext.max_early_data > 0)) {
  63. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
  64. ERR_R_INTERNAL_ERROR);
  65. return -1;
  66. }
  67. alg_enc = s->psksession->cipher->algorithm_enc;
  68. }
  69. } else {
  70. /*
  71. * To get here we must have selected a ciphersuite - otherwise ctx would
  72. * be NULL
  73. */
  74. if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
  75. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
  76. ERR_R_INTERNAL_ERROR);
  77. return -1;
  78. }
  79. alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
  80. }
  81. if (alg_enc & SSL_AESCCM) {
  82. if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  83. taglen = EVP_CCM8_TLS_TAG_LEN;
  84. else
  85. taglen = EVP_CCM_TLS_TAG_LEN;
  86. if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
  87. NULL) <= 0) {
  88. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
  89. ERR_R_INTERNAL_ERROR);
  90. return -1;
  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, SSL_F_TLS13_ENC,
  98. ERR_R_INTERNAL_ERROR);
  99. return -1;
  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, SSL_F_TLS13_ENC,
  114. ERR_R_INTERNAL_ERROR);
  115. return -1;
  116. }
  117. offset = ivlen - SEQ_NUM_SIZE;
  118. memcpy(iv, staticiv, offset);
  119. for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
  120. iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
  121. /* Increment the sequence counter */
  122. for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
  123. ++seq[loop - 1];
  124. if (seq[loop - 1] != 0)
  125. break;
  126. }
  127. if (loop == 0) {
  128. /* Sequence has wrapped */
  129. return -1;
  130. }
  131. /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
  132. if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
  133. || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  134. taglen,
  135. rec->data + rec->length) <= 0)) {
  136. return -1;
  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. WPACKET_cleanup(&wpkt);
  147. return -1;
  148. }
  149. /*
  150. * For CCM we must explicitly set the total plaintext length before we add
  151. * any AAD.
  152. */
  153. if (((alg_enc & SSL_AESCCM) != 0
  154. && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
  155. (unsigned int)rec->length) <= 0)
  156. || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
  157. sizeof(recheader)) <= 0
  158. || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
  159. (unsigned int)rec->length) <= 0
  160. || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
  161. || (size_t)(lenu + lenf) != rec->length) {
  162. return -1;
  163. }
  164. if (sending) {
  165. /* Add the tag */
  166. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
  167. rec->data + rec->length) <= 0) {
  168. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
  169. ERR_R_INTERNAL_ERROR);
  170. return -1;
  171. }
  172. rec->length += taglen;
  173. }
  174. return 1;
  175. }