blob: 0978a5d139a43705c10890b7c2d9883ab371b4cb [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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/* ====================================================================
58 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108/* ====================================================================
109 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
110 * ECC cipher suite support in OpenSSL originally developed by
111 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
112
113#include <assert.h>
114#include <limits.h>
115#include <stdio.h>
116#include <string.h>
117
118#include <openssl/buf.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/evp.h>
121#include <openssl/mem.h>
David Benjamin854dd652014-08-26 00:32:30 -0400122#include <openssl/md5.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/obj.h>
124#include <openssl/rand.h>
David Benjamin854dd652014-08-26 00:32:30 -0400125#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/x509.h>
127
David Benjamin2ee94aa2015-04-07 22:38:30 -0400128#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700129
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley24819752014-12-15 18:42:07 -0800131/* ssl3_do_write sends |s->init_buf| in records of type 'type'
132 * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns -1 on error, 1
133 * on success or zero if the transmission is still incomplete. */
134int ssl3_do_write(SSL *s, int type) {
135 int n;
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langley24819752014-12-15 18:42:07 -0800137 n = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off], s->init_num);
138 if (n < 0) {
139 return -1;
140 }
Adam Langley95c29f32014-06-20 12:00:00 -0700141
Adam Langley24819752014-12-15 18:42:07 -0800142 if (n == s->init_num) {
143 if (s->msg_callback) {
144 s->msg_callback(1, s->version, type, s->init_buf->data,
145 (size_t)(s->init_off + s->init_num), s,
146 s->msg_callback_arg);
147 }
148 return 1;
149 }
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langley24819752014-12-15 18:42:07 -0800151 s->init_off += n;
152 s->init_num -= n;
153 return 0;
154}
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley24819752014-12-15 18:42:07 -0800156int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen) {
157 uint8_t *p;
158 int n;
David Benjamin859ec3c2014-09-02 16:29:36 -0400159
Adam Langley24819752014-12-15 18:42:07 -0800160 if (s->state == a) {
161 p = ssl_handshake_start(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langley24819752014-12-15 18:42:07 -0800163 n = s->enc_method->final_finish_mac(s, sender, slen, s->s3->tmp.finish_md);
164 if (n == 0) {
165 return 0;
166 }
167 s->s3->tmp.finish_md_len = n;
168 memcpy(p, s->s3->tmp.finish_md, n);
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley24819752014-12-15 18:42:07 -0800170 /* Log the master secret, if logging is enabled. */
171 if (!ssl_ctx_log_master_secret(s->ctx, s->s3->client_random,
172 SSL3_RANDOM_SIZE, s->session->master_key,
173 s->session->master_key_length)) {
174 return 0;
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjamin8b368412015-03-14 01:54:17 -0400177 /* Copy the finished so we can use it for renegotiation checks */
Adam Langley24819752014-12-15 18:42:07 -0800178 if (s->server) {
179 assert(n <= EVP_MAX_MD_SIZE);
180 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, n);
181 s->s3->previous_server_finished_len = n;
182 } else {
183 assert(n <= EVP_MAX_MD_SIZE);
184 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, n);
185 s->s3->previous_client_finished_len = n;
186 }
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjaminfbdfefb2015-02-16 19:33:53 -0500188 if (!ssl_set_handshake_header(s, SSL3_MT_FINISHED, n)) {
189 return 0;
190 }
Adam Langley24819752014-12-15 18:42:07 -0800191 s->state = b;
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley24819752014-12-15 18:42:07 -0800194 /* SSL3_ST_SEND_xxxxxx_HELLO_B */
195 return ssl_do_write(s);
196}
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley691992b2015-06-03 09:49:55 -0700198/* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
199 * so far. */
Adam Langley24819752014-12-15 18:42:07 -0800200static void ssl3_take_mac(SSL *s) {
201 const char *sender;
202 int slen;
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley24819752014-12-15 18:42:07 -0800204 /* If no new cipher setup then return immediately: other functions will set
205 * the appropriate error. */
206 if (s->s3->tmp.new_cipher == NULL) {
207 return;
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langley24819752014-12-15 18:42:07 -0800210 if (s->state & SSL_ST_CONNECT) {
211 sender = s->enc_method->server_finished_label;
212 slen = s->enc_method->server_finished_label_len;
213 } else {
214 sender = s->enc_method->client_finished_label;
215 slen = s->enc_method->client_finished_label_len;
216 }
David Benjamin5b8f1042014-08-25 23:28:44 -0400217
Adam Langley24819752014-12-15 18:42:07 -0800218 s->s3->tmp.peer_finish_md_len = s->enc_method->final_finish_mac(
219 s, sender, slen, s->s3->tmp.peer_finish_md);
220}
Adam Langley95c29f32014-06-20 12:00:00 -0700221
Adam Langley24819752014-12-15 18:42:07 -0800222int ssl3_get_finished(SSL *s, int a, int b) {
223 int al, finished_len, ok;
224 long message_len;
225 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley24819752014-12-15 18:42:07 -0800227 message_len =
228 s->method->ssl_get_message(s, a, b, SSL3_MT_FINISHED, EVP_MAX_MD_SIZE,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500229 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langley24819752014-12-15 18:42:07 -0800231 if (!ok) {
232 return message_len;
233 }
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley24819752014-12-15 18:42:07 -0800235 /* Snapshot the finished hash before incorporating the new message. */
236 ssl3_take_mac(s);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500237 if (!ssl3_hash_current_message(s)) {
238 goto err;
239 }
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley24819752014-12-15 18:42:07 -0800241 /* If this occurs, we have missed a message.
242 * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */
243 if (!s->s3->change_cipher_spec) {
244 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
Adam Langley24819752014-12-15 18:42:07 -0800246 goto f_err;
247 }
248 s->s3->change_cipher_spec = 0;
249
250 p = s->init_msg;
251 finished_len = s->s3->tmp.peer_finish_md_len;
252
253 if (finished_len != message_len) {
254 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400255 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DIGEST_LENGTH);
Adam Langley24819752014-12-15 18:42:07 -0800256 goto f_err;
257 }
258
259 if (CRYPTO_memcmp(p, s->s3->tmp.peer_finish_md, finished_len) != 0) {
260 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400261 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
Adam Langley24819752014-12-15 18:42:07 -0800262 goto f_err;
263 }
264
265 /* Copy the finished so we can use it for renegotiation checks */
266 if (s->server) {
267 assert(finished_len <= EVP_MAX_MD_SIZE);
268 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, finished_len);
269 s->s3->previous_client_finished_len = finished_len;
270 } else {
271 assert(finished_len <= EVP_MAX_MD_SIZE);
272 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, finished_len);
273 s->s3->previous_server_finished_len = finished_len;
274 }
275
276 return 1;
277
Adam Langley95c29f32014-06-20 12:00:00 -0700278f_err:
Adam Langley24819752014-12-15 18:42:07 -0800279 ssl3_send_alert(s, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500280err:
Adam Langley24819752014-12-15 18:42:07 -0800281 return 0;
282}
Adam Langley95c29f32014-06-20 12:00:00 -0700283
284/* for these 2 messages, we need to
285 * ssl->enc_read_ctx re-init
286 * ssl->s3->read_sequence zero
287 * ssl->s3->read_mac_secret re-init
288 * ssl->session->read_sym_enc assign
289 * ssl->session->read_compression assign
Adam Langley24819752014-12-15 18:42:07 -0800290 * ssl->session->read_hash assign */
291int ssl3_send_change_cipher_spec(SSL *s, int a, int b) {
292 if (s->state == a) {
293 *((uint8_t *)s->init_buf->data) = SSL3_MT_CCS;
294 s->init_num = 1;
295 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley24819752014-12-15 18:42:07 -0800297 s->state = b;
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley24819752014-12-15 18:42:07 -0800300 /* SSL3_ST_CW_CHANGE_B */
301 return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
302}
Adam Langley95c29f32014-06-20 12:00:00 -0700303
David Benjamind1d80782015-07-05 11:54:09 -0400304int ssl3_output_cert_chain(SSL *s) {
Adam Langley24819752014-12-15 18:42:07 -0800305 uint8_t *p;
306 unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700307
David Benjamind1d80782015-07-05 11:54:09 -0400308 if (!ssl_add_cert_chain(s, &l)) {
Adam Langley24819752014-12-15 18:42:07 -0800309 return 0;
310 }
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley24819752014-12-15 18:42:07 -0800312 l -= 3 + SSL_HM_HEADER_LENGTH(s);
313 p = ssl_handshake_start(s);
314 l2n3(l, p);
315 l += 3;
David Benjaminfbdfefb2015-02-16 19:33:53 -0500316 return ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l);
Adam Langley24819752014-12-15 18:42:07 -0800317}
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley6e73d622014-12-15 18:46:16 -0800319/* Obtain handshake message of message type |msg_type| (any if |msg_type| == -1),
320 * maximum acceptable body length |max|. The first four bytes (msg_type and
321 * length) are read in state |header_state|, the body is read in state |body_state|. */
322long ssl3_get_message(SSL *s, int header_state, int body_state, int msg_type,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500323 long max, enum ssl_hash_message_t hash_message, int *ok) {
Adam Langley24819752014-12-15 18:42:07 -0800324 uint8_t *p;
325 unsigned long l;
326 long n;
Adam Langley6e73d622014-12-15 18:46:16 -0800327 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langley24819752014-12-15 18:42:07 -0800329 if (s->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500330 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
331 * ssl_dont_hash_message would have to have been applied to the previous
332 * call. */
333 assert(hash_message == ssl_hash_message);
Adam Langley24819752014-12-15 18:42:07 -0800334 s->s3->tmp.reuse_message = 0;
Adam Langley6e73d622014-12-15 18:46:16 -0800335 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
Adam Langley24819752014-12-15 18:42:07 -0800336 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400337 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley24819752014-12-15 18:42:07 -0800338 goto f_err;
339 }
340 *ok = 1;
Adam Langley6e73d622014-12-15 18:46:16 -0800341 s->state = body_state;
Adam Langley24819752014-12-15 18:42:07 -0800342 s->init_msg = (uint8_t *)s->init_buf->data + 4;
343 s->init_num = (int)s->s3->tmp.message_size;
344 return s->init_num;
345 }
Adam Langley95c29f32014-06-20 12:00:00 -0700346
Adam Langley6e73d622014-12-15 18:46:16 -0800347 p = (uint8_t *)s->init_buf->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langley6e73d622014-12-15 18:46:16 -0800349 if (s->state == header_state) {
350 assert(s->init_num < 4);
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langley6e73d622014-12-15 18:46:16 -0800352 for (;;) {
Adam Langley24819752014-12-15 18:42:07 -0800353 while (s->init_num < 4) {
David Benjamina6022772015-05-30 16:22:10 -0400354 int bytes_read = ssl3_read_bytes(s, SSL3_RT_HANDSHAKE, &p[s->init_num],
355 4 - s->init_num, 0);
Adam Langley6e73d622014-12-15 18:46:16 -0800356 if (bytes_read <= 0) {
Adam Langley24819752014-12-15 18:42:07 -0800357 *ok = 0;
Adam Langley6e73d622014-12-15 18:46:16 -0800358 return bytes_read;
Adam Langley24819752014-12-15 18:42:07 -0800359 }
Adam Langley6e73d622014-12-15 18:46:16 -0800360 s->init_num += bytes_read;
Adam Langley24819752014-12-15 18:42:07 -0800361 }
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langley6e73d622014-12-15 18:46:16 -0800363 static const uint8_t kHelloRequest[4] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
364 if (s->server || memcmp(p, kHelloRequest, sizeof(kHelloRequest)) != 0) {
365 break;
366 }
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley6e73d622014-12-15 18:46:16 -0800368 /* The server may always send 'Hello Request' messages -- we are doing
369 * a handshake anyway now, so ignore them if their format is correct.
370 * Does not count for 'Finished' MAC. */
371 s->init_num = 0;
372
373 if (s->msg_callback) {
374 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s,
375 s->msg_callback_arg);
376 }
377 }
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langley24819752014-12-15 18:42:07 -0800379 /* s->init_num == 4 */
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langley6e73d622014-12-15 18:46:16 -0800381 if (msg_type >= 0 && *p != msg_type) {
Adam Langley24819752014-12-15 18:42:07 -0800382 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400383 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley24819752014-12-15 18:42:07 -0800384 goto f_err;
385 }
386 s->s3->tmp.message_type = *(p++);
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley24819752014-12-15 18:42:07 -0800388 n2l3(p, l);
389 if (l > (unsigned long)max) {
390 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400391 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
Adam Langley24819752014-12-15 18:42:07 -0800392 goto f_err;
393 }
Adam Langley6e73d622014-12-15 18:46:16 -0800394
395 if (l && !BUF_MEM_grow_clean(s->init_buf, l + 4)) {
David Benjamin3570d732015-06-29 00:28:17 -0400396 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langley24819752014-12-15 18:42:07 -0800397 goto err;
398 }
399 s->s3->tmp.message_size = l;
Adam Langley6e73d622014-12-15 18:46:16 -0800400 s->state = body_state;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langley24819752014-12-15 18:42:07 -0800402 s->init_msg = (uint8_t *)s->init_buf->data + 4;
403 s->init_num = 0;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley6e73d622014-12-15 18:46:16 -0800406 /* next state (body_state) */
Adam Langley24819752014-12-15 18:42:07 -0800407 p = s->init_msg;
408 n = s->s3->tmp.message_size - s->init_num;
409 while (n > 0) {
David Benjamina6022772015-05-30 16:22:10 -0400410 int bytes_read = ssl3_read_bytes(s, SSL3_RT_HANDSHAKE, &p[s->init_num], n,
411 0);
Adam Langley6e73d622014-12-15 18:46:16 -0800412 if (bytes_read <= 0) {
Adam Langley24819752014-12-15 18:42:07 -0800413 s->rwstate = SSL_READING;
414 *ok = 0;
Adam Langley6e73d622014-12-15 18:46:16 -0800415 return bytes_read;
Adam Langley24819752014-12-15 18:42:07 -0800416 }
Adam Langley6e73d622014-12-15 18:46:16 -0800417 s->init_num += bytes_read;
418 n -= bytes_read;
Adam Langley24819752014-12-15 18:42:07 -0800419 }
420
421 /* Feed this message into MAC computation. */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500422 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(s)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500423 goto err;
Adam Langley6e73d622014-12-15 18:46:16 -0800424 }
425 if (s->msg_callback) {
Adam Langley24819752014-12-15 18:42:07 -0800426 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
427 (size_t)s->init_num + 4, s, s->msg_callback_arg);
Adam Langley6e73d622014-12-15 18:46:16 -0800428 }
Adam Langley24819752014-12-15 18:42:07 -0800429 *ok = 1;
430 return s->init_num;
431
Adam Langley95c29f32014-06-20 12:00:00 -0700432f_err:
Adam Langley24819752014-12-15 18:42:07 -0800433 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langley24819752014-12-15 18:42:07 -0800435err:
436 *ok = 0;
437 return -1;
438}
439
David Benjaminfbdfefb2015-02-16 19:33:53 -0500440int ssl3_hash_current_message(SSL *s) {
Adam Langley24819752014-12-15 18:42:07 -0800441 /* The handshake header (different size between DTLS and TLS) is included in
442 * the hash. */
443 size_t header_len = s->init_msg - (uint8_t *)s->init_buf->data;
David Benjamin9550c3a2015-08-05 08:50:34 -0400444 return ssl3_update_handshake_hash(s, (uint8_t *)s->init_buf->data,
445 s->init_num + header_len);
Adam Langley24819752014-12-15 18:42:07 -0800446}
David Benjamin590cbe92014-08-25 21:34:56 -0400447
David Benjamin854dd652014-08-26 00:32:30 -0400448/* ssl3_cert_verify_hash is documented as needing EVP_MAX_MD_SIZE because that
449 * is sufficient pre-TLS1.2 as well. */
450OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE > MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
Adam Langley24819752014-12-15 18:42:07 -0800451 combined_tls_hash_fits_in_max);
David Benjamin854dd652014-08-26 00:32:30 -0400452
Adam Langley24819752014-12-15 18:42:07 -0800453int ssl3_cert_verify_hash(SSL *s, uint8_t *out, size_t *out_len,
David Benjamin396a4412015-07-05 01:38:09 -0400454 const EVP_MD **out_md, int pkey_type) {
Adam Langley24819752014-12-15 18:42:07 -0800455 /* For TLS v1.2 send signature algorithm and signature using
456 * agreed digest and cached handshake records. Otherwise, use
457 * SHA1 or MD5 + SHA1 depending on key type. */
458 if (SSL_USE_SIGALGS(s)) {
Adam Langley24819752014-12-15 18:42:07 -0800459 EVP_MD_CTX mctx;
460 unsigned len;
David Benjamin854dd652014-08-26 00:32:30 -0400461
Adam Langley24819752014-12-15 18:42:07 -0800462 EVP_MD_CTX_init(&mctx);
463 if (!EVP_DigestInit_ex(&mctx, *out_md, NULL) ||
David Benjamin5375fd52015-08-06 01:32:10 -0400464 !EVP_DigestUpdate(&mctx, s->s3->handshake_buffer->data,
465 s->s3->handshake_buffer->length) ||
Adam Langley24819752014-12-15 18:42:07 -0800466 !EVP_DigestFinal(&mctx, out, &len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400467 OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
Adam Langley24819752014-12-15 18:42:07 -0800468 EVP_MD_CTX_cleanup(&mctx);
469 return 0;
470 }
471 *out_len = len;
David Benjamin396a4412015-07-05 01:38:09 -0400472 } else if (pkey_type == EVP_PKEY_RSA) {
Adam Langley24819752014-12-15 18:42:07 -0800473 if (s->enc_method->cert_verify_mac(s, NID_md5, out) == 0 ||
474 s->enc_method->cert_verify_mac(s, NID_sha1, out + MD5_DIGEST_LENGTH) ==
475 0) {
476 return 0;
477 }
478 *out_len = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
479 *out_md = EVP_md5_sha1();
David Benjamin396a4412015-07-05 01:38:09 -0400480 } else if (pkey_type == EVP_PKEY_EC) {
Adam Langley24819752014-12-15 18:42:07 -0800481 if (s->enc_method->cert_verify_mac(s, NID_sha1, out) == 0) {
482 return 0;
483 }
484 *out_len = SHA_DIGEST_LENGTH;
485 *out_md = EVP_sha1();
486 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400487 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley24819752014-12-15 18:42:07 -0800488 return 0;
489 }
David Benjamin854dd652014-08-26 00:32:30 -0400490
Adam Langley24819752014-12-15 18:42:07 -0800491 return 1;
492}
Adam Langley95c29f32014-06-20 12:00:00 -0700493
Adam Langley24819752014-12-15 18:42:07 -0800494int ssl_verify_alarm_type(long type) {
495 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langley24819752014-12-15 18:42:07 -0800497 switch (type) {
498 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
499 case X509_V_ERR_UNABLE_TO_GET_CRL:
500 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
501 al = SSL_AD_UNKNOWN_CA;
502 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langley24819752014-12-15 18:42:07 -0800504 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
505 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
506 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
507 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
508 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
509 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
510 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
511 case X509_V_ERR_CERT_NOT_YET_VALID:
512 case X509_V_ERR_CRL_NOT_YET_VALID:
513 case X509_V_ERR_CERT_UNTRUSTED:
514 case X509_V_ERR_CERT_REJECTED:
515 al = SSL_AD_BAD_CERTIFICATE;
516 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langley24819752014-12-15 18:42:07 -0800518 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
519 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
520 al = SSL_AD_DECRYPT_ERROR;
521 break;
522
523 case X509_V_ERR_CERT_HAS_EXPIRED:
524 case X509_V_ERR_CRL_HAS_EXPIRED:
525 al = SSL_AD_CERTIFICATE_EXPIRED;
526 break;
527
528 case X509_V_ERR_CERT_REVOKED:
529 al = SSL_AD_CERTIFICATE_REVOKED;
530 break;
531
532 case X509_V_ERR_OUT_OF_MEM:
533 al = SSL_AD_INTERNAL_ERROR;
534 break;
535
536 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
537 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
538 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
539 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
540 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
541 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
542 case X509_V_ERR_INVALID_CA:
543 al = SSL_AD_UNKNOWN_CA;
544 break;
545
546 case X509_V_ERR_APPLICATION_VERIFICATION:
547 al = SSL_AD_HANDSHAKE_FAILURE;
548 break;
549
550 case X509_V_ERR_INVALID_PURPOSE:
551 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
552 break;
553
554 default:
555 al = SSL_AD_CERTIFICATE_UNKNOWN;
556 break;
557 }
558
559 return al;
560}
561
David Benjamin74d8bc22015-05-21 02:16:53 -0400562int ssl_fill_hello_random(uint8_t *out, size_t len, int is_server) {
563 if (is_server) {
Adam Langley139ed192014-12-13 15:35:50 -0800564 const uint32_t current_time = time(NULL);
David Benjamin74d8bc22015-05-21 02:16:53 -0400565 uint8_t *p = out;
Adam Langley139ed192014-12-13 15:35:50 -0800566
567 if (len < 4) {
568 return 0;
569 }
570 p[0] = current_time >> 24;
571 p[1] = current_time >> 16;
572 p[2] = current_time >> 8;
573 p[3] = current_time;
574 return RAND_bytes(p + 4, len - 4);
575 } else {
David Benjamin74d8bc22015-05-21 02:16:53 -0400576 return RAND_bytes(out, len);
Adam Langley139ed192014-12-13 15:35:50 -0800577 }
578}