blob: d3f9421b0ddc69e17f6e3f25e5898a7e04908d21 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000113#include <openssl/ssl.h>
114
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115#include <assert.h>
116#include <limits.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800117#include <string.h>
118
119#include <openssl/buf.h>
David Benjaminc895d6b2016-08-11 13:26:41 -0400120#include <openssl/bytestring.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700121#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800122#include <openssl/evp.h>
123#include <openssl/mem.h>
124#include <openssl/md5.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400125#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126#include <openssl/rand.h>
127#include <openssl/sha.h>
128#include <openssl/x509.h>
129
Robert Sloan69939df2017-01-09 10:53:07 -0800130#include "../crypto/internal.h"
Adam Langleye9ada862015-05-11 17:20:37 -0700131#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800132
133
David Benjamin1b249672016-12-06 18:25:50 -0500134SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400135 SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE));
136 if (hs == NULL) {
137 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
138 return NULL;
139 }
Robert Sloan69939df2017-01-09 10:53:07 -0800140 OPENSSL_memset(hs, 0, sizeof(SSL_HANDSHAKE));
David Benjamin1b249672016-12-06 18:25:50 -0500141 hs->ssl = ssl;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400142 hs->wait = ssl_hs_ok;
Steven Valdeze7531f02016-12-14 13:29:57 -0500143 hs->state = SSL_ST_INIT;
Robert Sloan5d625782017-02-13 09:55:39 -0800144 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
145 ssl_handshake_free(hs);
146 return NULL;
147 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400148 return hs;
149}
150
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400151void ssl_handshake_free(SSL_HANDSHAKE *hs) {
152 if (hs == NULL) {
153 return;
154 }
155
156 OPENSSL_cleanse(hs->secret, sizeof(hs->secret));
Robert Sloan69939df2017-01-09 10:53:07 -0800157 OPENSSL_cleanse(hs->client_handshake_secret,
158 sizeof(hs->client_handshake_secret));
159 OPENSSL_cleanse(hs->server_handshake_secret,
160 sizeof(hs->server_handshake_secret));
David Benjamin95add822016-10-19 01:09:12 -0400161 OPENSSL_cleanse(hs->client_traffic_secret_0,
162 sizeof(hs->client_traffic_secret_0));
163 OPENSSL_cleanse(hs->server_traffic_secret_0,
164 sizeof(hs->server_traffic_secret_0));
David Benjamin7c0d06c2016-08-11 13:26:41 -0400165 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Robert Sloan5d625782017-02-13 09:55:39 -0800166 SSL_TRANSCRIPT_cleanup(&hs->transcript);
David Benjamin95add822016-10-19 01:09:12 -0400167 OPENSSL_free(hs->cookie);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400168 OPENSSL_free(hs->key_share_bytes);
169 OPENSSL_free(hs->public_key);
170 OPENSSL_free(hs->peer_sigalgs);
David Benjamin95add822016-10-19 01:09:12 -0400171 OPENSSL_free(hs->peer_supported_group_list);
172 OPENSSL_free(hs->peer_key);
173 OPENSSL_free(hs->server_params);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400174 OPENSSL_free(hs->peer_psk_identity_hint);
David Benjamin95add822016-10-19 01:09:12 -0400175 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
176 OPENSSL_free(hs->certificate_types);
Steven Valdez909b19f2016-11-21 15:35:44 -0500177
178 if (hs->key_block != NULL) {
179 OPENSSL_cleanse(hs->key_block, hs->key_block_len);
180 OPENSSL_free(hs->key_block);
181 }
182
183 OPENSSL_free(hs->hostname);
Steven Valdeze7531f02016-12-14 13:29:57 -0500184 EVP_PKEY_free(hs->peer_pubkey);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400185 OPENSSL_free(hs);
186}
187
Robert Sloan4d1ac502017-02-06 08:36:14 -0800188int ssl_check_message_type(SSL *ssl, int type) {
189 if (ssl->s3->tmp.message_type != type) {
190 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
191 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
192 ERR_add_error_dataf("got type %d, wanted type %d",
193 ssl->s3->tmp.message_type, type);
194 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195 }
196
Robert Sloan4d1ac502017-02-06 08:36:14 -0800197 return 1;
198}
199
200static int add_record_to_flight(SSL *ssl, uint8_t type, const uint8_t *in,
201 size_t in_len) {
202 /* We'll never add a flight while in the process of writing it out. */
203 assert(ssl->s3->pending_flight_offset == 0);
204
205 if (ssl->s3->pending_flight == NULL) {
206 ssl->s3->pending_flight = BUF_MEM_new();
207 if (ssl->s3->pending_flight == NULL) {
208 return 0;
209 }
210 }
211
212 size_t max_out = in_len + SSL_max_seal_overhead(ssl);
213 size_t new_cap = ssl->s3->pending_flight->length + max_out;
214 if (max_out < in_len || new_cap < max_out) {
215 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
216 return 0;
217 }
218
219 size_t len;
220 if (!BUF_MEM_reserve(ssl->s3->pending_flight, new_cap) ||
221 !tls_seal_record(ssl, (uint8_t *)ssl->s3->pending_flight->data +
222 ssl->s3->pending_flight->length,
223 &len, max_out, type, in, in_len)) {
224 return 0;
225 }
226
227 ssl->s3->pending_flight->length += len;
David Benjaminc895d6b2016-08-11 13:26:41 -0400228 return 1;
229}
230
231int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400232 /* Pick a modest size hint to save most of the |realloc| calls. */
233 if (!CBB_init(cbb, 64) ||
234 !CBB_add_u8(cbb, type) ||
235 !CBB_add_u24_length_prefixed(cbb, body)) {
236 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800237 CBB_cleanup(cbb);
David Benjaminc895d6b2016-08-11 13:26:41 -0400238 return 0;
239 }
240
241 return 1;
242}
243
Steven Valdez909b19f2016-11-21 15:35:44 -0500244int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
245 size_t *out_len) {
246 if (!CBB_finish(cbb, out_msg, out_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400247 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
248 return 0;
249 }
250
Steven Valdez909b19f2016-11-21 15:35:44 -0500251 return 1;
252}
253
Robert Sloan4d1ac502017-02-06 08:36:14 -0800254int ssl3_add_message(SSL *ssl, uint8_t *msg, size_t len) {
255 /* Add the message to the current flight, splitting into several records if
256 * needed. */
257 int ret = 0;
258 size_t added = 0;
259 do {
260 size_t todo = len - added;
261 if (todo > ssl->max_send_fragment) {
262 todo = ssl->max_send_fragment;
263 }
264
265 if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, msg + added, todo)) {
266 goto err;
267 }
268 added += todo;
269 } while (added < len);
270
271 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg, len);
Robert Sloan5d625782017-02-13 09:55:39 -0800272 /* TODO(svaldez): Move this up a layer to fix abstraction for SSL_TRANSCRIPT
273 * on hs. */
274 if (ssl->s3->hs != NULL &&
275 !SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, msg, len)) {
276 goto err;
277 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800278 ret = 1;
279
280err:
281 OPENSSL_free(msg);
282 return ret;
283}
284
285int ssl3_add_change_cipher_spec(SSL *ssl) {
286 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
287
288 if (!add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
289 sizeof(kChangeCipherSpec))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400290 return 0;
291 }
292
Robert Sloan4d1ac502017-02-06 08:36:14 -0800293 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
294 kChangeCipherSpec, sizeof(kChangeCipherSpec));
David Benjaminc895d6b2016-08-11 13:26:41 -0400295 return 1;
296}
297
Robert Sloan4d1ac502017-02-06 08:36:14 -0800298int ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
299 uint8_t alert[2] = {level, desc};
300 if (!add_record_to_flight(ssl, SSL3_RT_ALERT, alert, sizeof(alert))) {
301 return 0;
302 }
303
304 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, alert, sizeof(alert));
305 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, ((int)level << 8) | desc);
306 return 1;
307}
308
309int ssl_add_message_cbb(SSL *ssl, CBB *cbb) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500310 uint8_t *msg;
311 size_t len;
312 if (!ssl->method->finish_message(ssl, cbb, &msg, &len) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800313 !ssl->method->add_message(ssl, msg, len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500314 return 0;
315 }
316
317 return 1;
318}
319
Robert Sloan4d1ac502017-02-06 08:36:14 -0800320int ssl3_flush_flight(SSL *ssl) {
321 if (ssl->s3->pending_flight == NULL) {
322 return 1;
323 }
324
325 if (ssl->s3->pending_flight->length > 0xffffffff ||
326 ssl->s3->pending_flight->length > INT_MAX) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400327 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan4d1ac502017-02-06 08:36:14 -0800328 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400329 }
330
Robert Sloan4d1ac502017-02-06 08:36:14 -0800331 /* The handshake flight buffer is mutually exclusive with application data.
332 *
333 * TODO(davidben): This will not be true when closure alerts use this. */
334 if (ssl_write_buffer_is_pending(ssl)) {
335 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
336 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400337 }
338
Robert Sloan4d1ac502017-02-06 08:36:14 -0800339 /* Write the pending flight. */
340 while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) {
341 int ret = BIO_write(
342 ssl->wbio,
343 ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
344 ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset);
345 if (ret <= 0) {
346 ssl->rwstate = SSL_WRITING;
347 return ret;
348 }
349
350 ssl->s3->pending_flight_offset += ret;
351 }
352
353 if (BIO_flush(ssl->wbio) <= 0) {
354 ssl->rwstate = SSL_WRITING;
355 return -1;
356 }
357
358 BUF_MEM_free(ssl->s3->pending_flight);
359 ssl->s3->pending_flight = NULL;
360 ssl->s3->pending_flight_offset = 0;
David Benjamin6e899c72016-06-09 18:02:18 -0400361 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362}
363
Robert Sloan4d1ac502017-02-06 08:36:14 -0800364int ssl3_send_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500365 SSL *const ssl = hs->ssl;
Robert Sloan5d625782017-02-13 09:55:39 -0800366 const SSL_SESSION *session = SSL_get_session(ssl);
367
David Benjamin95add822016-10-19 01:09:12 -0400368 uint8_t finished[EVP_MAX_MD_SIZE];
Robert Sloan5d625782017-02-13 09:55:39 -0800369 size_t finished_len;
370 if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len,
371 session, ssl->server,
372 ssl3_protocol_version(ssl))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400373 return 0;
374 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400375
376 /* Log the master secret, if logging is enabled. */
377 if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
Robert Sloan5d625782017-02-13 09:55:39 -0800378 session->master_key,
379 session->master_key_length)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400380 return 0;
381 }
382
David Benjamin95add822016-10-19 01:09:12 -0400383 /* Copy the Finished so we can use it for renegotiation checks. */
384 if (ssl->version != SSL3_VERSION) {
385 if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
386 finished_len > sizeof(ssl->s3->previous_server_finished)) {
387 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
388 return -1;
389 }
390
391 if (ssl->server) {
Robert Sloan69939df2017-01-09 10:53:07 -0800392 OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400393 ssl->s3->previous_server_finished_len = finished_len;
394 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800395 OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400396 ssl->s3->previous_client_finished_len = finished_len;
397 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400398 }
399
400 CBB cbb, body;
401 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
David Benjamin95add822016-10-19 01:09:12 -0400402 !CBB_add_bytes(&body, finished, finished_len) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800403 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400404 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
405 CBB_cleanup(&cbb);
406 return -1;
407 }
408
Robert Sloan4d1ac502017-02-06 08:36:14 -0800409 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410}
411
David Benjamin1b249672016-12-06 18:25:50 -0500412int ssl3_get_finished(SSL_HANDSHAKE *hs) {
413 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800414 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400415 if (ret <= 0) {
416 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 }
418
Robert Sloan4d1ac502017-02-06 08:36:14 -0800419 if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED)) {
420 return -1;
421 }
422
Adam Langleyd9e397b2015-01-22 14:27:53 -0800423 /* Snapshot the finished hash before incorporating the new message. */
David Benjamin95add822016-10-19 01:09:12 -0400424 uint8_t finished[EVP_MAX_MD_SIZE];
Robert Sloan5d625782017-02-13 09:55:39 -0800425 size_t finished_len;
426 if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len,
427 SSL_get_session(ssl), !ssl->server,
428 ssl3_protocol_version(ssl)) ||
429 !ssl_hash_current_message(hs)) {
David Benjamin95add822016-10-19 01:09:12 -0400430 return -1;
Adam Langleye9ada862015-05-11 17:20:37 -0700431 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800432
David Benjamin7c0d06c2016-08-11 13:26:41 -0400433 int finished_ok = ssl->init_num == finished_len &&
David Benjamin95add822016-10-19 01:09:12 -0400434 CRYPTO_memcmp(ssl->init_msg, finished, finished_len) == 0;
David Benjamin4969cc92016-04-22 15:02:23 -0400435#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin7c0d06c2016-08-11 13:26:41 -0400436 finished_ok = 1;
David Benjamin4969cc92016-04-22 15:02:23 -0400437#endif
David Benjamin7c0d06c2016-08-11 13:26:41 -0400438 if (!finished_ok) {
David Benjamin95add822016-10-19 01:09:12 -0400439 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Kenny Rootb8494592015-09-25 02:29:14 +0000440 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
David Benjamin95add822016-10-19 01:09:12 -0400441 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800442 }
443
David Benjamin95add822016-10-19 01:09:12 -0400444 /* Copy the Finished so we can use it for renegotiation checks. */
445 if (ssl->version != SSL3_VERSION) {
446 if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
447 finished_len > sizeof(ssl->s3->previous_server_finished)) {
448 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
449 return -1;
450 }
451
452 if (ssl->server) {
Robert Sloan69939df2017-01-09 10:53:07 -0800453 OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400454 ssl->s3->previous_client_finished_len = finished_len;
455 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800456 OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
David Benjamin95add822016-10-19 01:09:12 -0400457 ssl->s3->previous_server_finished_len = finished_len;
458 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800459 }
460
461 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462}
463
Adam Langley4139edb2016-01-13 15:00:54 -0800464int ssl3_output_cert_chain(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400465 CBB cbb, body;
466 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
467 !ssl_add_cert_chain(ssl, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800468 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400469 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
470 CBB_cleanup(&cbb);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800471 return 0;
472 }
473
David Benjaminc895d6b2016-08-11 13:26:41 -0400474 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800475}
476
David Benjamind316cba2016-06-02 16:17:39 -0400477size_t ssl_max_handshake_message_len(const SSL *ssl) {
478 /* kMaxMessageLen is the default maximum message size for handshakes which do
479 * not accept peer certificate chains. */
480 static const size_t kMaxMessageLen = 16384;
481
David Benjaminc895d6b2016-08-11 13:26:41 -0400482 if (SSL_in_init(ssl)) {
483 if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
484 kMaxMessageLen < ssl->max_cert_list) {
485 return ssl->max_cert_list;
486 }
487 return kMaxMessageLen;
David Benjamind316cba2016-06-02 16:17:39 -0400488 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400489
490 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
491 /* In TLS 1.2 and below, the largest acceptable post-handshake message is
492 * a HelloRequest. */
493 return 0;
494 }
495
496 if (ssl->server) {
497 /* The largest acceptable post-handshake message for a server is a
498 * KeyUpdate. We will never initiate post-handshake auth. */
Robert Sloan5d625782017-02-13 09:55:39 -0800499 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400500 }
501
502 /* Clients must accept NewSessionTicket and CertificateRequest, so allow the
503 * default size. */
David Benjamind316cba2016-06-02 16:17:39 -0400504 return kMaxMessageLen;
505}
506
507static int extend_handshake_buffer(SSL *ssl, size_t length) {
508 if (!BUF_MEM_reserve(ssl->init_buf, length)) {
509 return -1;
510 }
511 while (ssl->init_buf->length < length) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400512 int ret = ssl3_read_handshake_bytes(
513 ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
514 length - ssl->init_buf->length);
David Benjamind316cba2016-06-02 16:17:39 -0400515 if (ret <= 0) {
516 return ret;
517 }
518 ssl->init_buf->length += (size_t)ret;
519 }
520 return 1;
521}
522
Robert Sloan4d1ac502017-02-06 08:36:14 -0800523static int read_v2_client_hello(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400524 /* Read the first 5 bytes, the size of the TLS record header. This is
525 * sufficient to detect a V2ClientHello and ensures that we never read beyond
526 * the first record. */
527 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
528 if (ret <= 0) {
529 return ret;
530 }
531 const uint8_t *p = ssl_read_buffer(ssl);
532
533 /* Some dedicated error codes for protocol mixups should the application wish
534 * to interpret them differently. (These do not overlap with ClientHello or
535 * V2ClientHello.) */
536 if (strncmp("GET ", (const char *)p, 4) == 0 ||
537 strncmp("POST ", (const char *)p, 5) == 0 ||
538 strncmp("HEAD ", (const char *)p, 5) == 0 ||
539 strncmp("PUT ", (const char *)p, 4) == 0) {
540 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
541 return -1;
542 }
543 if (strncmp("CONNE", (const char *)p, 5) == 0) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
545 return -1;
546 }
547
548 if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
549 p[3] != SSL3_VERSION_MAJOR) {
550 /* Not a V2ClientHello. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400551 return 1;
552 }
553
554 /* Determine the length of the V2ClientHello. */
555 size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
556 if (msg_length > (1024 * 4)) {
557 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
558 return -1;
559 }
560 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
561 /* Reject lengths that are too short early. We have already read
562 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
563 * (invalid) V2ClientHello which would be shorter than that. */
564 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
565 return -1;
566 }
567
568 /* Read the remainder of the V2ClientHello. */
569 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
570 if (ret <= 0) {
571 return ret;
572 }
573
574 CBS v2_client_hello;
575 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
576
577 /* The V2ClientHello without the length is incorporated into the handshake
Robert Sloan5d625782017-02-13 09:55:39 -0800578 * hash. This is only ever called at the start of the handshake, so hs is
579 * guaranteed to be non-NULL. */
580 if (!SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript,
581 CBS_data(&v2_client_hello),
582 CBS_len(&v2_client_hello))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400583 return -1;
584 }
585
David Benjamin7c0d06c2016-08-11 13:26:41 -0400586 ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */,
David Benjaminc895d6b2016-08-11 13:26:41 -0400587 CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
588
589 uint8_t msg_type;
590 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
591 CBS cipher_specs, session_id, challenge;
592 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
593 !CBS_get_u16(&v2_client_hello, &version) ||
594 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
595 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
596 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
597 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
598 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
599 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
600 CBS_len(&v2_client_hello) != 0) {
601 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
602 return -1;
603 }
604
605 /* msg_type has already been checked. */
606 assert(msg_type == SSL2_MT_CLIENT_HELLO);
607
608 /* The client_random is the V2ClientHello challenge. Truncate or
609 * left-pad with zeros as needed. */
610 size_t rand_len = CBS_len(&challenge);
611 if (rand_len > SSL3_RANDOM_SIZE) {
612 rand_len = SSL3_RANDOM_SIZE;
613 }
614 uint8_t random[SSL3_RANDOM_SIZE];
Robert Sloan69939df2017-01-09 10:53:07 -0800615 OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE);
616 OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
617 rand_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400618
619 /* Write out an equivalent SSLv3 ClientHello. */
620 size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
621 SSL3_RANDOM_SIZE + 1 /* session ID length */ +
622 2 /* cipher list length */ +
623 CBS_len(&cipher_specs) / 3 * 2 +
624 1 /* compression length */ + 1 /* compression */;
625 CBB client_hello, hello_body, cipher_suites;
626 CBB_zero(&client_hello);
627 if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) ||
628 !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
629 ssl->init_buf->max) ||
630 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
631 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
632 !CBB_add_u16(&hello_body, version) ||
633 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
634 /* No session id. */
635 !CBB_add_u8(&hello_body, 0) ||
636 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
637 CBB_cleanup(&client_hello);
638 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
639 return -1;
640 }
641
642 /* Copy the cipher suites. */
643 while (CBS_len(&cipher_specs) > 0) {
644 uint32_t cipher_spec;
645 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
646 CBB_cleanup(&client_hello);
647 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
648 return -1;
649 }
650
651 /* Skip SSLv2 ciphers. */
652 if ((cipher_spec & 0xff0000) != 0) {
653 continue;
654 }
655 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
656 CBB_cleanup(&client_hello);
657 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
658 return -1;
659 }
660 }
661
662 /* Add the null compression scheme and finish. */
663 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
664 !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
665 CBB_cleanup(&client_hello);
666 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
667 return -1;
668 }
669
670 /* Consume and discard the V2ClientHello. */
671 ssl_read_buffer_consume(ssl, 2 + msg_length);
672 ssl_read_buffer_discard(ssl);
673
Robert Sloan4d1ac502017-02-06 08:36:14 -0800674 ssl->s3->is_v2_hello = 1;
675 /* This is the first message, so hs must be non-NULL. */
676 ssl->s3->hs->v2_clienthello = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400677 return 1;
678}
679
Robert Sloan4d1ac502017-02-06 08:36:14 -0800680int ssl3_get_message(SSL *ssl) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400681again:
682 /* Re-create the handshake buffer if needed. */
683 if (ssl->init_buf == NULL) {
684 ssl->init_buf = BUF_MEM_new();
685 if (ssl->init_buf == NULL) {
686 return -1;
687 }
688 }
689
690 if (ssl->server && !ssl->s3->v2_hello_done) {
691 /* Bypass the record layer for the first message to handle V2ClientHello. */
Robert Sloan4d1ac502017-02-06 08:36:14 -0800692 int ret = read_v2_client_hello(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400693 if (ret <= 0) {
694 return ret;
695 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400696 ssl->s3->v2_hello_done = 1;
697 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800698
Adam Langley4139edb2016-01-13 15:00:54 -0800699 if (ssl->s3->tmp.reuse_message) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800700 /* There must be a current message. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400701 assert(ssl->init_msg != NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400702 ssl->s3->tmp.reuse_message = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400703 } else {
704 ssl3_release_current_message(ssl, 0 /* don't free buffer */);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800705 }
706
David Benjamind316cba2016-06-02 16:17:39 -0400707 /* Read the message header, if we haven't yet. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400708 int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH);
David Benjamind316cba2016-06-02 16:17:39 -0400709 if (ret <= 0) {
710 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800711 }
712
David Benjamind316cba2016-06-02 16:17:39 -0400713 /* Parse out the length. Cap it so the peer cannot force us to buffer up to
714 * 2^24 bytes. */
715 const uint8_t *p = (uint8_t *)ssl->init_buf->data;
716 size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
717 if (msg_len > ssl_max_handshake_message_len(ssl)) {
718 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
719 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
720 return -1;
721 }
722
723 /* Read the message body, if we haven't yet. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400724 ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len);
David Benjamind316cba2016-06-02 16:17:39 -0400725 if (ret <= 0) {
726 return ret;
727 }
728
729 /* We have now received a complete message. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400730 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, ssl->init_buf->data,
731 ssl->init_buf->length);
David Benjamind316cba2016-06-02 16:17:39 -0400732
David Benjaminc895d6b2016-08-11 13:26:41 -0400733 ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0];
734 ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH;
735 ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH;
736
737 /* Ignore stray HelloRequest messages in the handshake before TLS 1.3. Per RFC
738 * 5246, section 7.4.1.1, the server may send HelloRequest at any time. */
739 if (!ssl->server && SSL_in_init(ssl) &&
740 (!ssl->s3->have_version || ssl3_protocol_version(ssl) < TLS1_3_VERSION) &&
741 ssl->s3->tmp.message_type == SSL3_MT_HELLO_REQUEST &&
742 ssl->init_num == 0) {
David Benjamind316cba2016-06-02 16:17:39 -0400743 goto again;
744 }
745
David Benjaminc895d6b2016-08-11 13:26:41 -0400746 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800747}
748
Steven Valdez909b19f2016-11-21 15:35:44 -0500749void ssl3_get_current_message(const SSL *ssl, CBS *out) {
750 CBS_init(out, (uint8_t *)ssl->init_buf->data, ssl->init_buf->length);
751}
752
Robert Sloan5d625782017-02-13 09:55:39 -0800753int ssl_hash_current_message(SSL_HANDSHAKE *hs) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800754 /* V2ClientHellos are hashed implicitly. */
Robert Sloan5d625782017-02-13 09:55:39 -0800755 if (hs->ssl->s3->is_v2_hello) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800756 return 1;
757 }
758
Steven Valdez909b19f2016-11-21 15:35:44 -0500759 CBS cbs;
Robert Sloan5d625782017-02-13 09:55:39 -0800760 hs->ssl->method->get_current_message(hs->ssl, &cbs);
761 return SSL_TRANSCRIPT_update(&hs->transcript, CBS_data(&cbs), CBS_len(&cbs));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800762}
763
David Benjaminc895d6b2016-08-11 13:26:41 -0400764void ssl3_release_current_message(SSL *ssl, int free_buffer) {
765 if (ssl->init_msg != NULL) {
766 /* |init_buf| never contains data beyond the current message. */
767 assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800768
David Benjaminc895d6b2016-08-11 13:26:41 -0400769 /* Clear the current message. */
770 ssl->init_msg = NULL;
771 ssl->init_num = 0;
772 ssl->init_buf->length = 0;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800773 ssl->s3->is_v2_hello = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800774 }
775
David Benjaminc895d6b2016-08-11 13:26:41 -0400776 if (free_buffer) {
777 BUF_MEM_free(ssl->init_buf);
778 ssl->init_buf = NULL;
779 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800780}
781
Adam Langleyd9e397b2015-01-22 14:27:53 -0800782int ssl_verify_alarm_type(long type) {
783 int al;
784
785 switch (type) {
786 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
787 case X509_V_ERR_UNABLE_TO_GET_CRL:
788 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
789 al = SSL_AD_UNKNOWN_CA;
790 break;
791
792 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
793 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
794 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
795 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
796 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
797 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
798 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
799 case X509_V_ERR_CERT_NOT_YET_VALID:
800 case X509_V_ERR_CRL_NOT_YET_VALID:
801 case X509_V_ERR_CERT_UNTRUSTED:
802 case X509_V_ERR_CERT_REJECTED:
David Benjamin6e899c72016-06-09 18:02:18 -0400803 case X509_V_ERR_HOSTNAME_MISMATCH:
804 case X509_V_ERR_EMAIL_MISMATCH:
805 case X509_V_ERR_IP_ADDRESS_MISMATCH:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800806 al = SSL_AD_BAD_CERTIFICATE;
807 break;
808
809 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
810 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
811 al = SSL_AD_DECRYPT_ERROR;
812 break;
813
814 case X509_V_ERR_CERT_HAS_EXPIRED:
815 case X509_V_ERR_CRL_HAS_EXPIRED:
816 al = SSL_AD_CERTIFICATE_EXPIRED;
817 break;
818
819 case X509_V_ERR_CERT_REVOKED:
820 al = SSL_AD_CERTIFICATE_REVOKED;
821 break;
822
David Benjamin6e899c72016-06-09 18:02:18 -0400823 case X509_V_ERR_UNSPECIFIED:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800824 case X509_V_ERR_OUT_OF_MEM:
David Benjamin6e899c72016-06-09 18:02:18 -0400825 case X509_V_ERR_INVALID_CALL:
826 case X509_V_ERR_STORE_LOOKUP:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800827 al = SSL_AD_INTERNAL_ERROR;
828 break;
829
830 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
831 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
832 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
833 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
834 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
835 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
836 case X509_V_ERR_INVALID_CA:
837 al = SSL_AD_UNKNOWN_CA;
838 break;
839
840 case X509_V_ERR_APPLICATION_VERIFICATION:
841 al = SSL_AD_HANDSHAKE_FAILURE;
842 break;
843
844 case X509_V_ERR_INVALID_PURPOSE:
845 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
846 break;
847
848 default:
849 al = SSL_AD_CERTIFICATE_UNKNOWN;
850 break;
851 }
852
853 return al;
854}
Steven Valdez909b19f2016-11-21 15:35:44 -0500855
856int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
857 const SSL_EXTENSION_TYPE *ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800858 size_t num_ext_types, int ignore_unknown) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500859 /* Reset everything. */
860 for (size_t i = 0; i < num_ext_types; i++) {
861 *ext_types[i].out_present = 0;
862 CBS_init(ext_types[i].out_data, NULL, 0);
863 }
864
865 CBS copy = *cbs;
866 while (CBS_len(&copy) != 0) {
867 uint16_t type;
868 CBS data;
869 if (!CBS_get_u16(&copy, &type) ||
870 !CBS_get_u16_length_prefixed(&copy, &data)) {
871 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
872 *out_alert = SSL_AD_DECODE_ERROR;
873 return 0;
874 }
875
876 const SSL_EXTENSION_TYPE *ext_type = NULL;
877 for (size_t i = 0; i < num_ext_types; i++) {
878 if (type == ext_types[i].type) {
879 ext_type = &ext_types[i];
880 break;
881 }
882 }
883
884 if (ext_type == NULL) {
Robert Sloan69939df2017-01-09 10:53:07 -0800885 if (ignore_unknown) {
886 continue;
887 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500888 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
889 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
890 return 0;
891 }
892
893 /* Duplicate ext_types are forbidden. */
894 if (*ext_type->out_present) {
895 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
896 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
897 return 0;
898 }
899
900 *ext_type->out_present = 1;
901 *ext_type->out_data = data;
902 }
903
904 return 1;
905}