ssl3_record_tls13.c 6.6 KB

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