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
68 BSSL_NAMESPACE_BEGIN
69
ssl3_on_handshake_complete(SSL * ssl)70 static void ssl3_on_handshake_complete(SSL *ssl) {
71 // The handshake should have released its final message.
72 assert(!ssl->s3->has_message);
73
74 // During the handshake, |hs_buf| is retained. Release if it there is no
75 // excess in it. There may be excess left if there server sent Finished and
76 // HelloRequest in the same record.
77 //
78 // TODO(davidben): SChannel does not support this. Reject this case.
79 if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
80 ssl->s3->hs_buf.reset();
81 }
82 }
83
ssl3_set_read_state(SSL * ssl,UniquePtr<SSLAEADContext> aead_ctx)84 static bool ssl3_set_read_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
85 // Cipher changes are forbidden if the current epoch has leftover data.
86 if (tls_has_unprocessed_handshake_data(ssl)) {
87 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFERED_MESSAGES_ON_CIPHER_CHANGE);
88 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
89 return false;
90 }
91
92 OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
93 ssl->s3->aead_read_ctx = std::move(aead_ctx);
94 return true;
95 }
96
ssl3_set_write_state(SSL * ssl,UniquePtr<SSLAEADContext> aead_ctx)97 static bool ssl3_set_write_state(SSL *ssl, UniquePtr<SSLAEADContext> aead_ctx) {
98 if (!tls_flush_pending_hs_data(ssl)) {
99 return false;
100 }
101
102 OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
103 ssl->s3->aead_write_ctx = std::move(aead_ctx);
104 return true;
105 }
106
107 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
108 false /* is_dtls */,
109 ssl3_new,
110 ssl3_free,
111 ssl3_get_message,
112 ssl3_next_message,
113 ssl3_open_handshake,
114 ssl3_open_change_cipher_spec,
115 ssl3_open_app_data,
116 ssl3_write_app_data,
117 ssl3_dispatch_alert,
118 ssl3_init_message,
119 ssl3_finish_message,
120 ssl3_add_message,
121 ssl3_add_change_cipher_spec,
122 ssl3_flush_flight,
123 ssl3_on_handshake_complete,
124 ssl3_set_read_state,
125 ssl3_set_write_state,
126 };
127
ssl_noop_x509_check_client_CA_names(STACK_OF (CRYPTO_BUFFER)* names)128 static int ssl_noop_x509_check_client_CA_names(
129 STACK_OF(CRYPTO_BUFFER) *names) {
130 return 1;
131 }
132
ssl_noop_x509_clear(CERT * cert)133 static void ssl_noop_x509_clear(CERT *cert) {}
ssl_noop_x509_free(CERT * cert)134 static void ssl_noop_x509_free(CERT *cert) {}
ssl_noop_x509_dup(CERT * new_cert,const CERT * cert)135 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
ssl_noop_x509_flush_cached_leaf(CERT * cert)136 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
ssl_noop_x509_flush_cached_chain(CERT * cert)137 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
ssl_noop_x509_session_cache_objects(SSL_SESSION * sess)138 static int ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
139 return 1;
140 }
ssl_noop_x509_session_dup(SSL_SESSION * new_session,const SSL_SESSION * session)141 static int ssl_noop_x509_session_dup(SSL_SESSION *new_session,
142 const SSL_SESSION *session) {
143 return 1;
144 }
ssl_noop_x509_session_clear(SSL_SESSION * session)145 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
ssl_noop_x509_session_verify_cert_chain(SSL_SESSION * session,SSL_HANDSHAKE * hs,uint8_t * out_alert)146 static int ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
147 SSL_HANDSHAKE *hs,
148 uint8_t *out_alert) {
149 return 0;
150 }
151
ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)152 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
ssl_noop_x509_ssl_new(SSL_HANDSHAKE * hs)153 static int ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return 1; }
ssl_noop_x509_ssl_config_free(SSL_CONFIG * cfg)154 static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG * cfg)155 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE * hs)156 static int ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
157 return 1;
158 }
ssl_noop_x509_ssl_ctx_new(SSL_CTX * ctx)159 static int ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return 1; }
ssl_noop_x509_ssl_ctx_free(SSL_CTX * ctx)160 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) { }
ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)161 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
162
163 const SSL_X509_METHOD ssl_noop_x509_method = {
164 ssl_noop_x509_check_client_CA_names,
165 ssl_noop_x509_clear,
166 ssl_noop_x509_free,
167 ssl_noop_x509_dup,
168 ssl_noop_x509_flush_cached_chain,
169 ssl_noop_x509_flush_cached_leaf,
170 ssl_noop_x509_session_cache_objects,
171 ssl_noop_x509_session_dup,
172 ssl_noop_x509_session_clear,
173 ssl_noop_x509_session_verify_cert_chain,
174 ssl_noop_x509_hs_flush_cached_ca_names,
175 ssl_noop_x509_ssl_new,
176 ssl_noop_x509_ssl_config_free,
177 ssl_noop_x509_ssl_flush_cached_client_CA,
178 ssl_noop_x509_ssl_auto_chain_if_needed,
179 ssl_noop_x509_ssl_ctx_new,
180 ssl_noop_x509_ssl_ctx_free,
181 ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
182 };
183
184 BSSL_NAMESPACE_END
185
186 using namespace bssl;
187
TLS_method(void)188 const SSL_METHOD *TLS_method(void) {
189 static const SSL_METHOD kMethod = {
190 0,
191 &kTLSProtocolMethod,
192 &ssl_crypto_x509_method,
193 };
194 return &kMethod;
195 }
196
SSLv23_method(void)197 const SSL_METHOD *SSLv23_method(void) {
198 return TLS_method();
199 }
200
TLS_with_buffers_method(void)201 const SSL_METHOD *TLS_with_buffers_method(void) {
202 static const SSL_METHOD kMethod = {
203 0,
204 &kTLSProtocolMethod,
205 &ssl_noop_x509_method,
206 };
207 return &kMethod;
208 }
209
210 // Legacy version-locked methods.
211
TLSv1_2_method(void)212 const SSL_METHOD *TLSv1_2_method(void) {
213 static const SSL_METHOD kMethod = {
214 TLS1_2_VERSION,
215 &kTLSProtocolMethod,
216 &ssl_crypto_x509_method,
217 };
218 return &kMethod;
219 }
220
TLSv1_1_method(void)221 const SSL_METHOD *TLSv1_1_method(void) {
222 static const SSL_METHOD kMethod = {
223 TLS1_1_VERSION,
224 &kTLSProtocolMethod,
225 &ssl_crypto_x509_method,
226 };
227 return &kMethod;
228 }
229
TLSv1_method(void)230 const SSL_METHOD *TLSv1_method(void) {
231 static const SSL_METHOD kMethod = {
232 TLS1_VERSION,
233 &kTLSProtocolMethod,
234 &ssl_crypto_x509_method,
235 };
236 return &kMethod;
237 }
238
239 // Legacy side-specific methods.
240
TLSv1_2_server_method(void)241 const SSL_METHOD *TLSv1_2_server_method(void) {
242 return TLSv1_2_method();
243 }
244
TLSv1_1_server_method(void)245 const SSL_METHOD *TLSv1_1_server_method(void) {
246 return TLSv1_1_method();
247 }
248
TLSv1_server_method(void)249 const SSL_METHOD *TLSv1_server_method(void) {
250 return TLSv1_method();
251 }
252
TLSv1_2_client_method(void)253 const SSL_METHOD *TLSv1_2_client_method(void) {
254 return TLSv1_2_method();
255 }
256
TLSv1_1_client_method(void)257 const SSL_METHOD *TLSv1_1_client_method(void) {
258 return TLSv1_1_method();
259 }
260
TLSv1_client_method(void)261 const SSL_METHOD *TLSv1_client_method(void) {
262 return TLSv1_method();
263 }
264
SSLv23_server_method(void)265 const SSL_METHOD *SSLv23_server_method(void) {
266 return SSLv23_method();
267 }
268
SSLv23_client_method(void)269 const SSL_METHOD *SSLv23_client_method(void) {
270 return SSLv23_method();
271 }
272
TLS_server_method(void)273 const SSL_METHOD *TLS_server_method(void) {
274 return TLS_method();
275 }
276
TLS_client_method(void)277 const SSL_METHOD *TLS_client_method(void) {
278 return TLS_method();
279 }
280