tlsany_meth.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2022-2024 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 <openssl/evp.h>
  10. #include "../../ssl_local.h"
  11. #include "../record_local.h"
  12. #include "recmethod_local.h"
  13. #define MIN_SSL2_RECORD_LEN 9
  14. static int tls_any_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
  15. unsigned char *key, size_t keylen,
  16. unsigned char *iv, size_t ivlen,
  17. unsigned char *mackey, size_t mackeylen,
  18. const EVP_CIPHER *ciph,
  19. size_t taglen,
  20. int mactype,
  21. const EVP_MD *md,
  22. COMP_METHOD *comp)
  23. {
  24. if (level != OSSL_RECORD_PROTECTION_LEVEL_NONE) {
  25. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  26. return OSSL_RECORD_RETURN_FATAL;
  27. }
  28. /* No crypto protection at the "NONE" level so nothing to be done */
  29. return OSSL_RECORD_RETURN_SUCCESS;
  30. }
  31. static int tls_any_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
  32. size_t n_recs, int sending, SSL_MAC_BUF *macs,
  33. size_t macsize)
  34. {
  35. return 1;
  36. }
  37. static int tls_validate_record_header(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
  38. {
  39. if (rec->rec_version == SSL2_VERSION) {
  40. /* SSLv2 format ClientHello */
  41. if (!ossl_assert(rl->version == TLS_ANY_VERSION)) {
  42. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  43. return 0;
  44. }
  45. if (rec->length < MIN_SSL2_RECORD_LEN) {
  46. RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
  47. return 0;
  48. }
  49. } else {
  50. if (rl->version == TLS_ANY_VERSION) {
  51. if ((rec->rec_version >> 8) != SSL3_VERSION_MAJOR) {
  52. if (rl->is_first_record) {
  53. unsigned char *p;
  54. /*
  55. * Go back to start of packet, look at the five bytes that
  56. * we have.
  57. */
  58. p = rl->packet;
  59. if (HAS_PREFIX((char *)p, "GET ") ||
  60. HAS_PREFIX((char *)p, "POST ") ||
  61. HAS_PREFIX((char *)p, "HEAD ") ||
  62. HAS_PREFIX((char *)p, "PUT ")) {
  63. RLAYERfatal(rl, SSL_AD_NO_ALERT, SSL_R_HTTP_REQUEST);
  64. return 0;
  65. } else if (HAS_PREFIX((char *)p, "CONNE")) {
  66. RLAYERfatal(rl, SSL_AD_NO_ALERT,
  67. SSL_R_HTTPS_PROXY_REQUEST);
  68. return 0;
  69. }
  70. /* Doesn't look like TLS - don't send an alert */
  71. RLAYERfatal(rl, SSL_AD_NO_ALERT,
  72. SSL_R_WRONG_VERSION_NUMBER);
  73. return 0;
  74. } else {
  75. RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
  76. SSL_R_WRONG_VERSION_NUMBER);
  77. return 0;
  78. }
  79. }
  80. } else if (rl->version == TLS1_3_VERSION) {
  81. /*
  82. * In this case we know we are going to negotiate TLSv1.3, but we've
  83. * had an HRR, so we haven't actually done so yet. In TLSv1.3 we
  84. * must ignore the legacy record version in plaintext records.
  85. */
  86. } else if (rec->rec_version != rl->version) {
  87. if ((rl->version & 0xFF00) == (rec->rec_version & 0xFF00)) {
  88. if (rec->type == SSL3_RT_ALERT) {
  89. /*
  90. * The record is using an incorrect version number,
  91. * but what we've got appears to be an alert. We
  92. * haven't read the body yet to check whether its a
  93. * fatal or not - but chances are it is. We probably
  94. * shouldn't send a fatal alert back. We'll just
  95. * end.
  96. */
  97. RLAYERfatal(rl, SSL_AD_NO_ALERT,
  98. SSL_R_WRONG_VERSION_NUMBER);
  99. return 0;
  100. }
  101. /* Send back error using their minor version number */
  102. rl->version = (unsigned short)rec->rec_version;
  103. }
  104. RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
  105. SSL_R_WRONG_VERSION_NUMBER);
  106. return 0;
  107. }
  108. }
  109. if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
  110. /*
  111. * We use SSL_R_DATA_LENGTH_TOO_LONG instead of
  112. * SSL_R_ENCRYPTED_LENGTH_TOO_LONG here because we are the "any" method
  113. * and we know that we are dealing with plaintext data
  114. */
  115. RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static int tls_any_set_protocol_version(OSSL_RECORD_LAYER *rl, int vers)
  121. {
  122. if (rl->version != TLS_ANY_VERSION && rl->version != vers)
  123. return 0;
  124. rl->version = vers;
  125. return 1;
  126. }
  127. static int tls_any_prepare_for_encryption(OSSL_RECORD_LAYER *rl,
  128. size_t mac_size,
  129. WPACKET *thispkt,
  130. TLS_RL_RECORD *thiswr)
  131. {
  132. /* No encryption, so nothing to do */
  133. return 1;
  134. }
  135. const struct record_functions_st tls_any_funcs = {
  136. tls_any_set_crypto_state,
  137. tls_any_cipher,
  138. NULL,
  139. tls_any_set_protocol_version,
  140. tls_default_read_n,
  141. tls_get_more_records,
  142. tls_validate_record_header,
  143. tls_default_post_process_record,
  144. tls_get_max_records_default,
  145. tls_write_records_default,
  146. tls_allocate_write_buffers_default,
  147. tls_initialise_write_packets_default,
  148. NULL,
  149. tls_prepare_record_header_default,
  150. NULL,
  151. tls_any_prepare_for_encryption,
  152. tls_post_encryption_processing_default,
  153. NULL
  154. };
  155. static int dtls_any_set_protocol_version(OSSL_RECORD_LAYER *rl, int vers)
  156. {
  157. if (rl->version != DTLS_ANY_VERSION && rl->version != vers)
  158. return 0;
  159. rl->version = vers;
  160. return 1;
  161. }
  162. const struct record_functions_st dtls_any_funcs = {
  163. tls_any_set_crypto_state,
  164. tls_any_cipher,
  165. NULL,
  166. dtls_any_set_protocol_version,
  167. tls_default_read_n,
  168. dtls_get_more_records,
  169. NULL,
  170. NULL,
  171. NULL,
  172. tls_write_records_default,
  173. tls_allocate_write_buffers_default,
  174. tls_initialise_write_packets_default,
  175. NULL,
  176. dtls_prepare_record_header,
  177. NULL,
  178. tls_prepare_for_encryption_default,
  179. dtls_post_encryption_processing,
  180. NULL
  181. };