1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/ssl.h>
58
59 #include <assert.h>
60 #include <string.h>
61
62 #include <openssl/buf.h>
63
64 #include "../crypto/internal.h"
65 #include "internal.h"
66
67
ssl3_version_from_wire(uint16_t * out_version,uint16_t wire_version)68 static int ssl3_version_from_wire(uint16_t *out_version,
69 uint16_t wire_version) {
70 switch (wire_version) {
71 case SSL3_VERSION:
72 case TLS1_VERSION:
73 case TLS1_1_VERSION:
74 case TLS1_2_VERSION:
75 *out_version = wire_version;
76 return 1;
77 case TLS1_3_DRAFT_VERSION:
78 *out_version = TLS1_3_VERSION;
79 return 1;
80 }
81
82 return 0;
83 }
84
ssl3_version_to_wire(uint16_t version)85 static uint16_t ssl3_version_to_wire(uint16_t version) {
86 switch (version) {
87 case SSL3_VERSION:
88 case TLS1_VERSION:
89 case TLS1_1_VERSION:
90 case TLS1_2_VERSION:
91 return version;
92 case TLS1_3_VERSION:
93 return TLS1_3_DRAFT_VERSION;
94 }
95
96 /* It is an error to use this function with an invalid version. */
97 assert(0);
98 return 0;
99 }
100
ssl3_supports_cipher(const SSL_CIPHER * cipher)101 static int ssl3_supports_cipher(const SSL_CIPHER *cipher) { return 1; }
102
ssl3_expect_flight(SSL * ssl)103 static void ssl3_expect_flight(SSL *ssl) {}
104
ssl3_received_flight(SSL * ssl)105 static void ssl3_received_flight(SSL *ssl) {}
106
ssl3_set_read_state(SSL * ssl,SSL_AEAD_CTX * aead_ctx)107 static int ssl3_set_read_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
108 if (ssl->s3->rrec.length != 0) {
109 /* There may not be unprocessed record data at a cipher change. */
110 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
111 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
112 SSL_AEAD_CTX_free(aead_ctx);
113 return 0;
114 }
115
116 OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
117
118 SSL_AEAD_CTX_free(ssl->s3->aead_read_ctx);
119 ssl->s3->aead_read_ctx = aead_ctx;
120 return 1;
121 }
122
ssl3_set_write_state(SSL * ssl,SSL_AEAD_CTX * aead_ctx)123 static int ssl3_set_write_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
124 OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
125
126 SSL_AEAD_CTX_free(ssl->s3->aead_write_ctx);
127 ssl->s3->aead_write_ctx = aead_ctx;
128 return 1;
129 }
130
131 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
132 0 /* is_dtls */,
133 SSL3_VERSION,
134 TLS1_3_VERSION,
135 ssl3_version_from_wire,
136 ssl3_version_to_wire,
137 ssl3_new,
138 ssl3_free,
139 ssl3_get_message,
140 ssl3_get_current_message,
141 ssl3_release_current_message,
142 ssl3_read_app_data,
143 ssl3_read_change_cipher_spec,
144 ssl3_read_close_notify,
145 ssl3_write_app_data,
146 ssl3_dispatch_alert,
147 ssl3_supports_cipher,
148 ssl3_init_message,
149 ssl3_finish_message,
150 ssl3_add_message,
151 ssl3_add_change_cipher_spec,
152 ssl3_add_alert,
153 ssl3_flush_flight,
154 ssl3_expect_flight,
155 ssl3_received_flight,
156 ssl3_set_read_state,
157 ssl3_set_write_state,
158 };
159
TLS_method(void)160 const SSL_METHOD *TLS_method(void) {
161 static const SSL_METHOD kMethod = {
162 0,
163 &kTLSProtocolMethod,
164 &ssl_crypto_x509_method,
165 };
166 return &kMethod;
167 }
168
SSLv23_method(void)169 const SSL_METHOD *SSLv23_method(void) {
170 return TLS_method();
171 }
172
173 /* Legacy version-locked methods. */
174
TLSv1_2_method(void)175 const SSL_METHOD *TLSv1_2_method(void) {
176 static const SSL_METHOD kMethod = {
177 TLS1_2_VERSION,
178 &kTLSProtocolMethod,
179 &ssl_crypto_x509_method,
180 };
181 return &kMethod;
182 }
183
TLSv1_1_method(void)184 const SSL_METHOD *TLSv1_1_method(void) {
185 static const SSL_METHOD kMethod = {
186 TLS1_1_VERSION,
187 &kTLSProtocolMethod,
188 &ssl_crypto_x509_method,
189 };
190 return &kMethod;
191 }
192
TLSv1_method(void)193 const SSL_METHOD *TLSv1_method(void) {
194 static const SSL_METHOD kMethod = {
195 TLS1_VERSION,
196 &kTLSProtocolMethod,
197 &ssl_crypto_x509_method,
198 };
199 return &kMethod;
200 }
201
SSLv3_method(void)202 const SSL_METHOD *SSLv3_method(void) {
203 static const SSL_METHOD kMethod = {
204 SSL3_VERSION,
205 &kTLSProtocolMethod,
206 &ssl_crypto_x509_method,
207 };
208 return &kMethod;
209 }
210
211 /* Legacy side-specific methods. */
212
TLSv1_2_server_method(void)213 const SSL_METHOD *TLSv1_2_server_method(void) {
214 return TLSv1_2_method();
215 }
216
TLSv1_1_server_method(void)217 const SSL_METHOD *TLSv1_1_server_method(void) {
218 return TLSv1_1_method();
219 }
220
TLSv1_server_method(void)221 const SSL_METHOD *TLSv1_server_method(void) {
222 return TLSv1_method();
223 }
224
SSLv3_server_method(void)225 const SSL_METHOD *SSLv3_server_method(void) {
226 return SSLv3_method();
227 }
228
TLSv1_2_client_method(void)229 const SSL_METHOD *TLSv1_2_client_method(void) {
230 return TLSv1_2_method();
231 }
232
TLSv1_1_client_method(void)233 const SSL_METHOD *TLSv1_1_client_method(void) {
234 return TLSv1_1_method();
235 }
236
TLSv1_client_method(void)237 const SSL_METHOD *TLSv1_client_method(void) {
238 return TLSv1_method();
239 }
240
SSLv3_client_method(void)241 const SSL_METHOD *SSLv3_client_method(void) {
242 return SSLv3_method();
243 }
244
SSLv23_server_method(void)245 const SSL_METHOD *SSLv23_server_method(void) {
246 return SSLv23_method();
247 }
248
SSLv23_client_method(void)249 const SSL_METHOD *SSLv23_client_method(void) {
250 return SSLv23_method();
251 }
252
TLS_server_method(void)253 const SSL_METHOD *TLS_server_method(void) {
254 return TLS_method();
255 }
256
TLS_client_method(void)257 const SSL_METHOD *TLS_client_method(void) {
258 return TLS_method();
259 }
260
ssl_noop_x509_check_client_CA_names(STACK_OF (CRYPTO_BUFFER)* names)261 static int ssl_noop_x509_check_client_CA_names(
262 STACK_OF(CRYPTO_BUFFER) *names) {
263 return 1;
264 }
265
ssl_noop_x509_clear(CERT * cert)266 static void ssl_noop_x509_clear(CERT *cert) {}
ssl_noop_x509_free(CERT * cert)267 static void ssl_noop_x509_free(CERT *cert) {}
ssl_noop_x509_dup(CERT * new_cert,const CERT * cert)268 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
ssl_noop_x509_flush_cached_leaf(CERT * cert)269 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
ssl_noop_x509_flush_cached_chain(CERT * cert)270 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
ssl_noop_x509_session_cache_objects(SSL_SESSION * sess)271 static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
272 return 1;
273 }
ssl_noop_x509_session_dup(SSL_SESSION * new_session,const SSL_SESSION * session)274 static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
275 const SSL_SESSION *session) {
276 return 1;
277 }
ssl_noop_x509_session_clear(SSL_SESSION * session)278 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
ssl_noop_x509_session_verify_cert_chain(SSL_SESSION * session,SSL * ssl)279 static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
280 SSL *ssl) {
281 if (!ssl->ctx->i_promise_to_verify_certs_after_the_handshake) {
282 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_CA);
283 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
284 return 0;
285 }
286
287 session->verify_result = X509_V_OK;
288 return 1;
289 }
290
ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)291 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
ssl_noop_x509_ssl_new(SSL * ctx)292 static int ssl_noop_x509_ssl_new(SSL *ctx) { return 1; }
ssl_noop_x509_ssl_free(SSL * ctx)293 static void ssl_noop_x509_ssl_free(SSL *ctx) { }
ssl_noop_x509_ssl_flush_cached_client_CA(SSL * ssl)294 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL *ssl) {}
ssl_noop_x509_ssl_auto_chain_if_needed(SSL * ssl)295 static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL *ssl) { return 1; }
ssl_noop_x509_ssl_ctx_new(SSL_CTX * ctx)296 static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
ssl_noop_x509_ssl_ctx_free(SSL_CTX * ctx)297 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)298 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
299
300 static const SSL_X509_METHOD ssl_noop_x509_method = {
301 ssl_noop_x509_check_client_CA_names,
302 ssl_noop_x509_clear,
303 ssl_noop_x509_free,
304 ssl_noop_x509_dup,
305 ssl_noop_x509_flush_cached_chain,
306 ssl_noop_x509_flush_cached_leaf,
307 ssl_noop_x509_session_cache_objects,
308 ssl_noop_x509_session_dup,
309 ssl_noop_x509_session_clear,
310 ssl_noop_x509_session_verify_cert_chain,
311 ssl_noop_x509_hs_flush_cached_ca_names,
312 ssl_noop_x509_ssl_new,
313 ssl_noop_x509_ssl_free,
314 ssl_noop_x509_ssl_flush_cached_client_CA,
315 ssl_noop_x509_ssl_auto_chain_if_needed,
316 ssl_noop_x509_ssl_ctx_new,
317 ssl_noop_x509_ssl_ctx_free,
318 ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
319 };
320
TLS_with_buffers_method(void)321 const SSL_METHOD *TLS_with_buffers_method(void) {
322 static const SSL_METHOD kMethod = {
323 0,
324 &kTLSProtocolMethod,
325 &ssl_noop_x509_method,
326 };
327 return &kMethod;
328 }
329