]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/record/methods/tlsany_meth.c
Remove some final references to the SSL object in the record layer
[thirdparty/openssl.git] / ssl / record / methods / tlsany_meth.c
1 /*
2 * Copyright 2022 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
10 #include <openssl/evp.h>
11 #include "../../ssl_local.h"
12 #include "../record_local.h"
13 #include "recmethod_local.h"
14
15 static int tls_any_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
16 unsigned char *key, size_t keylen,
17 unsigned char *iv, size_t ivlen,
18 unsigned char *mackey, size_t mackeylen,
19 const EVP_CIPHER *ciph,
20 size_t taglen,
21 /* TODO(RECLAYER): This probably should not be an int */
22 int mactype,
23 const EVP_MD *md,
24 const SSL_COMP *comp)
25 {
26 if (level != OSSL_RECORD_PROTECTION_LEVEL_NONE) {
27 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
28 return OSSL_RECORD_RETURN_FATAL;
29 }
30
31 /* No crypto protection at the "NONE" level so nothing to be done */
32
33 return OSSL_RECORD_RETURN_SUCCESS;
34 }
35
36 static int tls_any_cipher(OSSL_RECORD_LAYER *rl, SSL3_RECORD *recs,
37 size_t n_recs, int sending, SSL_MAC_BUF *macs,
38 size_t macsize)
39 {
40 return 1;
41 }
42
43 static int tls_validate_record_header(OSSL_RECORD_LAYER *rl, SSL3_RECORD *rec)
44 {
45 if (rec->rec_version == SSL2_VERSION) {
46 /* SSLv2 format ClientHello */
47 if (!ossl_assert(rl->version == TLS_ANY_VERSION)) {
48 RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
49 return 0;
50 }
51 if (rec->length < MIN_SSL2_RECORD_LEN) {
52 RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
53 return 0;
54 }
55 } else {
56 if (rl->version == TLS_ANY_VERSION) {
57 if ((rec->rec_version >> 8) != SSL3_VERSION_MAJOR) {
58 if (rl->is_first_record) {
59 unsigned char *p;
60
61 /*
62 * Go back to start of packet, look at the five bytes that
63 * we have.
64 */
65 p = rl->packet;
66 if (HAS_PREFIX((char *)p, "GET ") ||
67 HAS_PREFIX((char *)p, "POST ") ||
68 HAS_PREFIX((char *)p, "HEAD ") ||
69 HAS_PREFIX((char *)p, "PUT ")) {
70 RLAYERfatal(rl, SSL_AD_NO_ALERT, SSL_R_HTTP_REQUEST);
71 return 0;
72 } else if (HAS_PREFIX((char *)p, "CONNE")) {
73 RLAYERfatal(rl, SSL_AD_NO_ALERT,
74 SSL_R_HTTPS_PROXY_REQUEST);
75 return 0;
76 }
77
78 /* Doesn't look like TLS - don't send an alert */
79 RLAYERfatal(rl, SSL_AD_NO_ALERT,
80 SSL_R_WRONG_VERSION_NUMBER);
81 return 0;
82 } else {
83 RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
84 SSL_R_WRONG_VERSION_NUMBER);
85 return 0;
86 }
87 }
88 } else if (rl->version == TLS1_3_VERSION) {
89 /*
90 * In this case we know we are going to negotiate TLSv1.3, but we've
91 * had an HRR, so we haven't actually done so yet. Nonetheless we
92 * still expect the record version to be TLSv1.2 as per a normal
93 * TLSv1.3 record
94 */
95 if (rec->rec_version != TLS1_2_VERSION) {
96 RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
97 SSL_R_WRONG_VERSION_NUMBER);
98 return 0;
99 }
100 } else if (rec->rec_version != rl->version) {
101 if ((rl->version & 0xFF00) == (rec->rec_version & 0xFF00)) {
102 if (rec->type == SSL3_RT_ALERT) {
103 /*
104 * The record is using an incorrect version number,
105 * but what we've got appears to be an alert. We
106 * haven't read the body yet to check whether its a
107 * fatal or not - but chances are it is. We probably
108 * shouldn't send a fatal alert back. We'll just
109 * end.
110 */
111 RLAYERfatal(rl, SSL_AD_NO_ALERT,
112 SSL_R_WRONG_VERSION_NUMBER);
113 return 0;
114 }
115 /* Send back error using their minor version number */
116 rl->version = (unsigned short)rec->rec_version;
117 }
118 RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
119 SSL_R_WRONG_VERSION_NUMBER);
120 return 0;
121 }
122 }
123 if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
124 /*
125 * We use SSL_R_DATA_LENGTH_TOO_LONG instead of
126 * SSL_R_ENCRYPTED_LENGTH_TOO_LONG here because we are the "any" method
127 * and we know that we are dealing with plaintext data
128 */
129 RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
130 return 0;
131 }
132 return 1;
133 }
134
135 static int tls_any_set_protocol_version(OSSL_RECORD_LAYER *rl, int vers)
136 {
137 if (rl->version != TLS_ANY_VERSION && rl->version != vers)
138 return 0;
139 rl->version = vers;
140
141 return 1;
142 }
143
144 struct record_functions_st tls_any_funcs = {
145 tls_any_set_crypto_state,
146 tls_default_read_n,
147 tls_any_cipher,
148 NULL,
149 tls_any_set_protocol_version,
150 tls_validate_record_header,
151 tls_default_post_process_record
152 };