blob: 695f081d8647044bf571e437cf092ea7394536f5 [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-2007 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
David Benjaminf0ae1702015-04-07 23:05:04 -0400111#include <assert.h>
David Benjamine3aa1d92015-06-16 15:34:50 -0400112#include <limits.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400113#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin03973092014-06-24 23:27:17 -0400116#include <openssl/bytestring.h>
David Benjamind6a4ae92015-08-06 11:10:51 -0400117#include <openssl/digest.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/evp.h>
120#include <openssl/hmac.h>
121#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400122#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Matt Braithwaitee564a5b2015-09-30 15:24:05 -0700124#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800127
128
David Benjamin0d56f882015-12-19 17:05:56 -0500129static int ssl_check_clienthello_tlsext(SSL *ssl);
130static int ssl_check_serverhello_tlsext(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132static int compare_uint16_t(const void *p1, const void *p2) {
133 uint16_t u1 = *((const uint16_t *)p1);
134 uint16_t u2 = *((const uint16_t *)p2);
135 if (u1 < u2) {
136 return -1;
137 } else if (u1 > u2) {
138 return 1;
139 } else {
140 return 0;
141 }
142}
David Benjamin35a7a442014-07-05 00:23:20 -0400143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
145 * more than one extension of the same type in a ClientHello or ServerHello.
146 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400147 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800148static int tls1_check_duplicate_extensions(const CBS *cbs) {
149 CBS extensions = *cbs;
150 size_t num_extensions = 0, i = 0;
151 uint16_t *extension_types = NULL;
152 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 /* First pass: count the extensions. */
155 while (CBS_len(&extensions) > 0) {
156 uint16_t type;
157 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 if (!CBS_get_u16(&extensions, &type) ||
160 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
161 goto done;
162 }
David Benjamin35a7a442014-07-05 00:23:20 -0400163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 num_extensions++;
165 }
David Benjamin35a7a442014-07-05 00:23:20 -0400166
Adam Langleyfcf25832014-12-18 17:42:32 -0800167 if (num_extensions == 0) {
168 return 1;
169 }
David Benjamin9a373592014-07-25 04:27:53 -0400170
Brian Smith5ba06892016-02-07 09:36:04 -1000171 extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -0800172 if (extension_types == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800174 goto done;
175 }
David Benjamin35a7a442014-07-05 00:23:20 -0400176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 /* Second pass: gather the extension types. */
178 extensions = *cbs;
179 for (i = 0; i < num_extensions; i++) {
180 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400181
Adam Langleyfcf25832014-12-18 17:42:32 -0800182 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
183 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
184 /* This should not happen. */
185 goto done;
186 }
187 }
188 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 /* Sort the extensions and make sure there are no duplicates. */
191 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
192 for (i = 1; i < num_extensions; i++) {
193 if (extension_types[i - 1] == extension_types[i]) {
194 goto done;
195 }
196 }
David Benjamin35a7a442014-07-05 00:23:20 -0400197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret = 1;
199
David Benjamin35a7a442014-07-05 00:23:20 -0400200done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400201 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 return ret;
203}
David Benjamin35a7a442014-07-05 00:23:20 -0400204
David Benjamind7573dc2016-07-20 19:05:22 +0200205int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx,
206 const uint8_t *in, size_t in_len) {
207 memset(ctx, 0, sizeof(*ctx));
208 ctx->ssl = ssl;
209 ctx->client_hello = in;
210 ctx->client_hello_len = in_len;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400211
David Benjamind7573dc2016-07-20 19:05:22 +0200212 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 if (/* Skip client version. */
216 !CBS_skip(&client_hello, 2) ||
217 /* Skip client nonce. */
218 !CBS_skip(&client_hello, 32) ||
219 /* Extract session_id. */
220 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
221 return 0;
222 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 ctx->session_id = CBS_data(&session_id);
225 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700226
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 /* Skip past DTLS cookie */
David Benjamince079fd2016-08-02 16:22:34 -0400228 if (SSL_is_dtls(ctx->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
232 return 0;
233 }
234 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 /* Extract cipher_suites. */
237 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
238 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
239 return 0;
240 }
241 ctx->cipher_suites = CBS_data(&cipher_suites);
242 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 /* Extract compression_methods. */
245 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
246 CBS_len(&compression_methods) < 1) {
247 return 0;
248 }
249 ctx->compression_methods = CBS_data(&compression_methods);
250 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 /* If the ClientHello ends here then it's valid, but doesn't have any
253 * extensions. (E.g. SSLv3.) */
254 if (CBS_len(&client_hello) == 0) {
255 ctx->extensions = NULL;
256 ctx->extensions_len = 0;
257 return 1;
258 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 /* Extract extensions and check it is valid. */
261 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
262 !tls1_check_duplicate_extensions(&extensions) ||
263 CBS_len(&client_hello) != 0) {
264 return 0;
265 }
266 ctx->extensions = CBS_data(&extensions);
267 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 return 1;
270}
Adam Langleydc9b1412014-06-20 12:00:00 -0700271
David Benjamincec73442016-08-02 17:41:33 -0400272int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
273 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400277 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 uint16_t type;
279 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 if (!CBS_get_u16(&extensions, &type) ||
281 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
282 return 0;
283 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400286 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 return 1;
288 }
289 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700290
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292}
Adam Langley95c29f32014-06-20 12:00:00 -0700293
David Benjamincec73442016-08-02 17:41:33 -0400294int SSL_early_callback_ctx_extension_get(
295 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
296 const uint8_t **out_data, size_t *out_len) {
297 CBS cbs;
298 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
299 return 0;
300 }
301
302 *out_data = CBS_data(&cbs);
303 *out_len = CBS_len(&cbs);
304 return 1;
305}
306
Steven Valdezce902a92016-05-17 11:47:53 -0400307static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400308 SSL_CURVE_X25519,
309 SSL_CURVE_SECP256R1,
310 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700311#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400312 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700313#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800314};
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Steven Valdez5440fe02016-07-18 12:40:30 -0400316void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
317 const uint16_t **out_group_ids,
318 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400319 if (get_peer_groups) {
320 /* Only clients send a supported group list, so this function is only
321 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500322 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400323 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
324 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 return;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Steven Valdezce902a92016-05-17 11:47:53 -0400328 *out_group_ids = ssl->supported_group_list;
329 *out_group_ids_len = ssl->supported_group_list_len;
330 if (!*out_group_ids) {
331 *out_group_ids = kDefaultGroups;
332 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 }
334}
David Benjamined439582014-07-14 19:13:02 -0400335
Steven Valdezce902a92016-05-17 11:47:53 -0400336int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
337 const uint16_t *groups, *peer_groups, *pref, *supp;
338 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500341 if (ssl->server == 0) {
342 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 }
344
Steven Valdezce902a92016-05-17 11:47:53 -0400345 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
346 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800347
Steven Valdezce902a92016-05-17 11:47:53 -0400348 if (peer_groups_len == 0) {
349 /* Clients are not required to send a supported_groups extension. In this
350 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500351 * section 4, paragraph 3.
352 *
353 * However, in the interests of compatibility, we will skip ECDH if the
354 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400355 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500356 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400357 }
358
David Benjamin4298d772015-12-19 00:18:25 -0500359 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400360 pref = groups;
361 pref_len = groups_len;
362 supp = peer_groups;
363 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400364 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400365 pref = peer_groups;
366 pref_len = peer_groups_len;
367 supp = groups;
368 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400369 }
370
371 for (i = 0; i < pref_len; i++) {
372 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400374 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500375 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 }
377 }
378 }
379
David Benjamin4298d772015-12-19 00:18:25 -0500380 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800381}
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Steven Valdezce902a92016-05-17 11:47:53 -0400383int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400385 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 size_t i;
387
Steven Valdezce902a92016-05-17 11:47:53 -0400388 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
389 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 return 0;
391 }
392
393 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400394 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
395 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 return 0;
397 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 }
399
Steven Valdezce902a92016-05-17 11:47:53 -0400400 OPENSSL_free(*out_group_ids);
401 *out_group_ids = group_ids;
402 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403
404 return 1;
405}
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Steven Valdezce902a92016-05-17 11:47:53 -0400407/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
408 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400409 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400410static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 uint8_t *out_comp_id, EC_KEY *ec) {
412 int nid;
413 uint16_t id;
414 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 if (ec == NULL) {
417 return 0;
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 grp = EC_KEY_get0_group(ec);
421 if (grp == NULL) {
422 return 0;
423 }
David Benjamin072334d2014-07-13 16:24:27 -0400424
Steven Valdezce902a92016-05-17 11:47:53 -0400425 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400427 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 return 0;
429 }
David Benjamin072334d2014-07-13 16:24:27 -0400430
Steven Valdezce902a92016-05-17 11:47:53 -0400431 /* Set the named group ID. Arbitrary explicit groups are not supported. */
432 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800433
434 if (out_comp_id) {
435 if (EC_KEY_get0_public_key(ec) == NULL) {
436 return 0;
437 }
438 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
439 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
440 } else {
441 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
442 }
443 }
444
445 return 1;
446}
David Benjamin072334d2014-07-13 16:24:27 -0400447
Steven Valdezce902a92016-05-17 11:47:53 -0400448/* tls1_check_group_id returns one if |group_id| is consistent with both our
449 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400450 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400451int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
452 const uint16_t *groups;
453 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400454
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400456 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
457 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400458 /* Servers do not present a preference list so, if we are a client, only
459 * check our list. */
460 continue;
461 }
462
Steven Valdezce902a92016-05-17 11:47:53 -0400463 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
464 if (get_peer_groups && groups_len == 0) {
465 /* Clients are not required to send a supported_groups extension. In this
466 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400467 * section 4, paragraph 3. */
468 continue;
469 }
Steven Valdezce902a92016-05-17 11:47:53 -0400470 for (i = 0; i < groups_len; i++) {
471 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 break;
473 }
474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Steven Valdezce902a92016-05-17 11:47:53 -0400476 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 return 0;
478 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 }
David Benjamin033e5f42014-11-13 18:47:41 -0500480
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 return 1;
482}
David Benjamin033e5f42014-11-13 18:47:41 -0500483
David Benjamin0d56f882015-12-19 17:05:56 -0500484int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700485 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
486 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
487 return 1;
488 }
489
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700491 if (pkey == NULL) {
492 return 0;
493 }
494
495 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400496 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500498 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
499 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400500 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
501 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400502 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 goto done;
504 }
505
506 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500507
508done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400509 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 return ret;
511}
David Benjamin42e9a772014-09-02 23:18:44 -0400512
Adam Langley95c29f32014-06-20 12:00:00 -0700513/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Steven Valdez02563852016-06-23 13:33:05 -0400516static const uint16_t kDefaultSignatureAlgorithms[] = {
517 SSL_SIGN_RSA_PKCS1_SHA512,
518 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Steven Valdez02563852016-06-23 13:33:05 -0400520 SSL_SIGN_RSA_PKCS1_SHA384,
521 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Steven Valdez02563852016-06-23 13:33:05 -0400523 SSL_SIGN_RSA_PKCS1_SHA256,
524 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Steven Valdez02563852016-06-23 13:33:05 -0400526 SSL_SIGN_RSA_PKCS1_SHA1,
527 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700528};
David Benjamin05da6e12014-07-12 20:42:55 -0400529
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400530static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
531 SSL_SIGN_RSA_PSS_SHA512,
532 SSL_SIGN_RSA_PKCS1_SHA512,
533 SSL_SIGN_ECDSA_SECP521R1_SHA512,
534
535 SSL_SIGN_RSA_PSS_SHA384,
536 SSL_SIGN_RSA_PKCS1_SHA384,
537 SSL_SIGN_ECDSA_SECP384R1_SHA384,
538
539 SSL_SIGN_RSA_PSS_SHA256,
540 SSL_SIGN_RSA_PKCS1_SHA256,
541 SSL_SIGN_ECDSA_SECP256R1_SHA256,
542
543 SSL_SIGN_RSA_PKCS1_SHA1,
544 SSL_SIGN_ECDSA_SHA1,
545};
546
Steven Valdez02563852016-06-23 13:33:05 -0400547size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400548 uint16_t version;
549 if (ssl->s3->have_version) {
550 version = ssl3_protocol_version(ssl);
551 } else {
552 version = ssl->method->version_from_wire(ssl->client_version);
553 }
554
555 if (version >= TLS1_3_VERSION) {
556 *psigs = kDefaultTLS13SignatureAlgorithms;
557 return sizeof(kDefaultTLS13SignatureAlgorithms) /
558 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
559 }
560
Steven Valdez02563852016-06-23 13:33:05 -0400561 *psigs = kDefaultSignatureAlgorithms;
562 return sizeof(kDefaultSignatureAlgorithms) /
563 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800564}
Adam Langley95c29f32014-06-20 12:00:00 -0700565
David Benjamin887c3002016-07-08 16:15:32 -0700566int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400567 const uint16_t *sent_sigs;
568 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500571 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400572 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400573 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 break;
575 }
576 }
577
David Benjamin788be4a2015-10-30 17:50:57 -0400578 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400579 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800580 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
581 return 0;
582 }
583
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 return 1;
585}
586
587/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
588 * supported or doesn't appear in supported signature algorithms. Unlike
589 * ssl_cipher_get_disabled this applies to a specific session and not global
590 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500591void ssl_set_client_disabled(SSL *ssl) {
592 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400593 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 size_t i, sigalgslen;
595 int have_rsa = 0, have_ecdsa = 0;
596 c->mask_a = 0;
597 c->mask_k = 0;
598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 /* Now go through all signature algorithms seeing if we support any for RSA,
600 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500601 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400602 for (i = 0; i < sigalgslen; i++) {
603 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400604 case SSL_SIGN_RSA_PSS_SHA512:
605 case SSL_SIGN_RSA_PSS_SHA384:
606 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400607 case SSL_SIGN_RSA_PKCS1_SHA512:
608 case SSL_SIGN_RSA_PKCS1_SHA384:
609 case SSL_SIGN_RSA_PKCS1_SHA256:
610 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 have_rsa = 1;
612 break;
613
Steven Valdez02563852016-06-23 13:33:05 -0400614 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
615 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
616 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
617 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 have_ecdsa = 1;
619 break;
620 }
621 }
622
623 /* Disable auth if we don't include any appropriate signature algorithms. */
624 if (!have_rsa) {
625 c->mask_a |= SSL_aRSA;
626 }
627 if (!have_ecdsa) {
628 c->mask_a |= SSL_aECDSA;
629 }
630
631 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500632 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 c->mask_a |= SSL_aPSK;
634 c->mask_k |= SSL_kPSK;
635 }
636}
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langley614c66a2015-06-12 15:26:58 -0700638/* tls_extension represents a TLS extension that is handled internally. The
639 * |init| function is called for each handshake, before any other functions of
640 * the extension. Then the add and parse callbacks are called as needed.
641 *
642 * The parse callbacks receive a |CBS| that contains the contents of the
643 * extension (i.e. not including the type and length bytes). If an extension is
644 * not received then the parse callbacks will be called with a NULL CBS so that
645 * they can do any processing needed to handle the absence of an extension.
646 *
647 * The add callbacks receive a |CBB| to which the extension can be appended but
648 * the function is responsible for appending the type and length bytes too.
649 *
650 * All callbacks return one for success and zero for error. If a parse function
651 * returns zero then a fatal alert with value |*out_alert| will be sent. If
652 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
653struct tls_extension {
654 uint16_t value;
655 void (*init)(SSL *ssl);
656
657 int (*add_clienthello)(SSL *ssl, CBB *out);
658 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
659
660 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
661 int (*add_serverhello)(SSL *ssl, CBB *out);
662};
663
Steven Valdez6b8509a2016-07-12 13:38:32 -0400664static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
665 if (contents != NULL) {
666 /* Servers MUST NOT send this extension. */
667 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
668 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
669 return 0;
670 }
671
672 return 1;
673}
674
675static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
676 /* This extension from the client is handled elsewhere. */
677 return 1;
678}
679
680static int dont_add_serverhello(SSL *ssl, CBB *out) {
681 return 1;
682}
Adam Langley614c66a2015-06-12 15:26:58 -0700683
684/* Server name indication (SNI).
685 *
686 * https://tools.ietf.org/html/rfc6066#section-3. */
687
688static void ext_sni_init(SSL *ssl) {
689 ssl->s3->tmp.should_ack_sni = 0;
690}
691
692static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
693 if (ssl->tlsext_hostname == NULL) {
694 return 1;
695 }
696
697 CBB contents, server_name_list, name;
698 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
699 !CBB_add_u16_length_prefixed(out, &contents) ||
700 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
701 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
702 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
703 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
704 strlen(ssl->tlsext_hostname)) ||
705 !CBB_flush(out)) {
706 return 0;
707 }
708
709 return 1;
710}
711
David Benjamin0d56f882015-12-19 17:05:56 -0500712static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
713 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700714 if (contents == NULL) {
715 return 1;
716 }
717
718 if (CBS_len(contents) != 0) {
719 return 0;
720 }
721
722 assert(ssl->tlsext_hostname != NULL);
723
Steven Valdez87eab492016-06-27 16:34:59 -0400724 if (ssl->session == NULL) {
725 assert(ssl->s3->new_session->tlsext_hostname == NULL);
726 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
727 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700728 *out_alert = SSL_AD_INTERNAL_ERROR;
729 return 0;
730 }
731 }
732
733 return 1;
734}
735
David Benjamin0d56f882015-12-19 17:05:56 -0500736static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
737 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700738 if (contents == NULL) {
739 return 1;
740 }
741
David Benjamin9b611e22016-03-03 08:48:30 -0500742 CBS server_name_list, host_name;
743 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700744 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500745 !CBS_get_u8(&server_name_list, &name_type) ||
746 /* Although the server_name extension was intended to be extensible to
747 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
748 * different name types will cause an error. Further, RFC 4366 originally
749 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
750 * adding new name types is no longer feasible.
751 *
752 * Act as if the extensibility does not exist to simplify parsing. */
753 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
754 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700755 CBS_len(contents) != 0) {
756 return 0;
757 }
758
David Benjamin9b611e22016-03-03 08:48:30 -0500759 if (name_type != TLSEXT_NAMETYPE_host_name ||
760 CBS_len(&host_name) == 0 ||
761 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
762 CBS_contains_zero_byte(&host_name)) {
763 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
764 return 0;
765 }
Adam Langley614c66a2015-06-12 15:26:58 -0700766
David Benjamin9b611e22016-03-03 08:48:30 -0500767 /* TODO(davidben): SNI should be resolved before resumption. We have the
768 * early callback as a replacement, but we should fix the current callback
769 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400770 if (ssl->session == NULL) {
771 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500772
773 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400774 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500775 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700776 return 0;
777 }
778
David Benjamin9b611e22016-03-03 08:48:30 -0500779 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700780 }
781
782 return 1;
783}
784
785static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400786 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700787 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400788 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700789 return 1;
790 }
791
792 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
793 !CBB_add_u16(out, 0 /* length */)) {
794 return 0;
795 }
796
797 return 1;
798}
799
800
Adam Langley5021b222015-06-12 18:27:58 -0700801/* Renegotiation indication.
802 *
803 * https://tools.ietf.org/html/rfc5746 */
804
805static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
806 CBB contents, prev_finished;
807 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
808 !CBB_add_u16_length_prefixed(out, &contents) ||
809 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
810 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
811 ssl->s3->previous_client_finished_len) ||
812 !CBB_flush(out)) {
813 return 0;
814 }
815
816 return 1;
817}
818
819static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
820 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400821 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
822 return 0;
823 }
824
David Benjamin3e052de2015-11-25 20:10:31 -0500825 /* Servers may not switch between omitting the extension and supporting it.
826 * See RFC 5746, sections 3.5 and 4.2. */
827 if (ssl->s3->initial_handshake_complete &&
828 (contents != NULL) != ssl->s3->send_connection_binding) {
829 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
830 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
831 return 0;
832 }
833
Adam Langley5021b222015-06-12 18:27:58 -0700834 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500835 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700836 * RI even on initial ServerHello because the client doesn't see any
837 * renegotiation during an attack. However this would mean we could not
838 * connect to any server which doesn't support RI.
839 *
David Benjamine9cddb82015-11-23 14:36:40 -0500840 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
841 * practical terms every client sets it so it's just assumed here. */
842 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700843 }
844
845 const size_t expected_len = ssl->s3->previous_client_finished_len +
846 ssl->s3->previous_server_finished_len;
847
848 /* Check for logic errors */
849 assert(!expected_len || ssl->s3->previous_client_finished_len);
850 assert(!expected_len || ssl->s3->previous_server_finished_len);
851
852 /* Parse out the extension contents. */
853 CBS renegotiated_connection;
854 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
855 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400856 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700857 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
858 return 0;
859 }
860
861 /* Check that the extension matches. */
862 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400863 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700864 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
865 return 0;
866 }
867
868 const uint8_t *d = CBS_data(&renegotiated_connection);
869 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
870 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400871 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700872 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
873 return 0;
874 }
875 d += ssl->s3->previous_client_finished_len;
876
877 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
878 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400879 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700880 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
881 return 0;
882 }
883 ssl->s3->send_connection_binding = 1;
884
885 return 1;
886}
887
888static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
889 CBS *contents) {
890 /* Renegotiation isn't supported as a server so this function should never be
891 * called after the initial handshake. */
892 assert(!ssl->s3->initial_handshake_complete);
893
Steven Valdez143e8b32016-07-11 13:19:03 -0400894 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
895 return 1;
896 }
897
Adam Langley5021b222015-06-12 18:27:58 -0700898 CBS fake_contents;
899 static const uint8_t kFakeExtension[] = {0};
900
901 if (contents == NULL) {
902 if (ssl->s3->send_connection_binding) {
903 /* The renegotiation SCSV was received so pretend that we received a
904 * renegotiation extension. */
905 CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension));
906 contents = &fake_contents;
907 /* We require that the renegotiation extension is at index zero of
908 * kExtensions. */
909 ssl->s3->tmp.extensions.received |= (1u << 0);
910 } else {
911 return 1;
912 }
913 }
914
915 CBS renegotiated_connection;
916
917 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
918 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700920 return 0;
921 }
922
923 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500924 if (!CBS_mem_equal(&renegotiated_connection,
925 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700926 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400927 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700928 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
929 return 0;
930 }
931
932 ssl->s3->send_connection_binding = 1;
933
934 return 1;
935}
936
937static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400938 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
939 return 1;
940 }
941
Adam Langley5021b222015-06-12 18:27:58 -0700942 CBB contents, prev_finished;
943 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
944 !CBB_add_u16_length_prefixed(out, &contents) ||
945 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
946 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
947 ssl->s3->previous_client_finished_len) ||
948 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
949 ssl->s3->previous_server_finished_len) ||
950 !CBB_flush(out)) {
951 return 0;
952 }
953
954 return 1;
955}
956
Adam Langley0a056712015-07-01 15:03:33 -0700957
958/* Extended Master Secret.
959 *
David Benjamin43946d42016-02-01 08:42:19 -0500960 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700961
962static void ext_ems_init(SSL *ssl) {
963 ssl->s3->tmp.extended_master_secret = 0;
964}
965
966static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
967 if (ssl->version == SSL3_VERSION) {
968 return 1;
969 }
970
971 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
972 !CBB_add_u16(out, 0 /* length */)) {
973 return 0;
974 }
975
976 return 1;
977}
978
979static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
980 CBS *contents) {
981 if (contents == NULL) {
982 return 1;
983 }
984
Steven Valdez143e8b32016-07-11 13:19:03 -0400985 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
986 ssl->version == SSL3_VERSION) {
987 return 0;
988 }
989
990 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700991 return 0;
992 }
993
994 ssl->s3->tmp.extended_master_secret = 1;
995 return 1;
996}
997
David Benjamin0d56f882015-12-19 17:05:56 -0500998static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
999 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001000 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
1001 ssl->version == SSL3_VERSION) {
1002 return 1;
1003 }
1004
1005 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -07001006 return 1;
1007 }
1008
1009 if (CBS_len(contents) != 0) {
1010 return 0;
1011 }
1012
1013 ssl->s3->tmp.extended_master_secret = 1;
1014 return 1;
1015}
1016
1017static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1018 if (!ssl->s3->tmp.extended_master_secret) {
1019 return 1;
1020 }
1021
1022 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1023 !CBB_add_u16(out, 0 /* length */)) {
1024 return 0;
1025 }
1026
1027 return 1;
1028}
1029
Adam Langley9b05bc52015-07-01 15:25:33 -07001030
1031/* Session tickets.
1032 *
1033 * https://tools.ietf.org/html/rfc5077 */
1034
1035static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1036 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1037 return 1;
1038 }
1039
1040 const uint8_t *ticket_data = NULL;
1041 int ticket_len = 0;
1042
1043 /* Renegotiation does not participate in session resumption. However, still
1044 * advertise the extension to avoid potentially breaking servers which carry
1045 * over the state from the previous handshake, such as OpenSSL servers
1046 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1047 if (!ssl->s3->initial_handshake_complete &&
1048 ssl->session != NULL &&
1049 ssl->session->tlsext_tick != NULL) {
1050 ticket_data = ssl->session->tlsext_tick;
1051 ticket_len = ssl->session->tlsext_ticklen;
1052 }
1053
1054 CBB ticket;
1055 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1056 !CBB_add_u16_length_prefixed(out, &ticket) ||
1057 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1058 !CBB_flush(out)) {
1059 return 0;
1060 }
1061
1062 return 1;
1063}
1064
1065static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1066 CBS *contents) {
1067 ssl->tlsext_ticket_expected = 0;
1068
1069 if (contents == NULL) {
1070 return 1;
1071 }
1072
Steven Valdez143e8b32016-07-11 13:19:03 -04001073 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1074 return 0;
1075 }
1076
Adam Langley9b05bc52015-07-01 15:25:33 -07001077 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1078 * this function should never be called, even if the server tries to send the
1079 * extension. */
1080 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1081
1082 if (CBS_len(contents) != 0) {
1083 return 0;
1084 }
1085
1086 ssl->tlsext_ticket_expected = 1;
1087 return 1;
1088}
1089
Adam Langley9b05bc52015-07-01 15:25:33 -07001090static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1091 if (!ssl->tlsext_ticket_expected) {
1092 return 1;
1093 }
1094
1095 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1096 * true. */
1097 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1098
1099 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1100 !CBB_add_u16(out, 0 /* length */)) {
1101 return 0;
1102 }
1103
1104 return 1;
1105}
1106
1107
Adam Langley2e857bd2015-07-01 16:09:19 -07001108/* Signature Algorithms.
1109 *
1110 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1111
1112static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001113 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001114 return 1;
1115 }
1116
Steven Valdez02563852016-06-23 13:33:05 -04001117 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001118 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1119
1120 CBB contents, sigalgs;
1121 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1122 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001123 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1124 return 0;
1125 }
1126
1127 size_t i;
1128 for (i = 0; i < sigalgs_len; i++) {
1129 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1130 return 0;
1131 }
1132 }
1133
1134 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001135 return 0;
1136 }
1137
1138 return 1;
1139}
1140
Adam Langley2e857bd2015-07-01 16:09:19 -07001141static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1142 CBS *contents) {
1143 OPENSSL_free(ssl->cert->peer_sigalgs);
1144 ssl->cert->peer_sigalgs = NULL;
1145 ssl->cert->peer_sigalgslen = 0;
1146
Adam Langley2e857bd2015-07-01 16:09:19 -07001147 if (contents == NULL) {
1148 return 1;
1149 }
1150
1151 CBS supported_signature_algorithms;
1152 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001153 CBS_len(contents) != 0 ||
1154 CBS_len(&supported_signature_algorithms) == 0 ||
1155 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001156 return 0;
1157 }
1158
1159 return 1;
1160}
1161
Adam Langley2e857bd2015-07-01 16:09:19 -07001162
Adam Langleybb0bd042015-07-01 16:21:03 -07001163/* OCSP Stapling.
1164 *
1165 * https://tools.ietf.org/html/rfc6066#section-8 */
1166
1167static void ext_ocsp_init(SSL *ssl) {
1168 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001169 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001170}
1171
1172static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1173 if (!ssl->ocsp_stapling_enabled) {
1174 return 1;
1175 }
1176
1177 CBB contents;
1178 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1179 !CBB_add_u16_length_prefixed(out, &contents) ||
1180 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1181 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1182 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1183 !CBB_flush(out)) {
1184 return 0;
1185 }
1186
Adam Langleyce9d85e2016-01-24 15:58:39 -08001187 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001188 return 1;
1189}
1190
1191static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001192 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001193 if (contents == NULL) {
1194 return 1;
1195 }
1196
David Benjamin942f4ed2016-07-16 19:03:49 +03001197 /* OCSP stapling is forbidden on a non-certificate cipher. */
1198 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001199 return 0;
1200 }
1201
David Benjamin942f4ed2016-07-16 19:03:49 +03001202 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1203 if (CBS_len(contents) != 0) {
1204 return 0;
1205 }
1206
1207 ssl->s3->tmp.certificate_status_expected = 1;
1208 return 1;
1209 }
1210
1211 uint8_t status_type;
1212 CBS ocsp_response;
1213 if (!CBS_get_u8(contents, &status_type) ||
1214 status_type != TLSEXT_STATUSTYPE_ocsp ||
1215 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1216 CBS_len(&ocsp_response) == 0 ||
1217 CBS_len(contents) != 0) {
1218 return 0;
1219 }
1220
Steven Valdez87eab492016-06-27 16:34:59 -04001221 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1222 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001223 *out_alert = SSL_AD_INTERNAL_ERROR;
1224 return 0;
1225 }
1226
Adam Langleybb0bd042015-07-01 16:21:03 -07001227 return 1;
1228}
1229
1230static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1231 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001232 if (contents == NULL) {
1233 return 1;
1234 }
1235
1236 uint8_t status_type;
1237 if (!CBS_get_u8(contents, &status_type)) {
1238 return 0;
1239 }
1240
1241 /* We cannot decide whether OCSP stapling will occur yet because the correct
1242 * SSL_CTX might not have been selected. */
1243 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1244
Adam Langleybb0bd042015-07-01 16:21:03 -07001245 return 1;
1246}
1247
1248static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001249 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1250 ssl->ctx->ocsp_response_length == 0 ||
1251 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001252 return 1;
1253 }
1254
David Benjamin942f4ed2016-07-16 19:03:49 +03001255 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1256 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001257 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001258 return 1;
1259 }
1260
1261 ssl->s3->tmp.certificate_status_expected = 1;
1262
1263 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1264 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001265 }
1266
David Benjamin942f4ed2016-07-16 19:03:49 +03001267 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001268 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001269 CBB_add_u16_length_prefixed(out, &body) &&
1270 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1271 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1272 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1273 ssl->ctx->ocsp_response_length) &&
1274 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001275}
1276
1277
Adam Langley97dfcbf2015-07-01 18:35:20 -07001278/* Next protocol negotiation.
1279 *
1280 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1281
1282static void ext_npn_init(SSL *ssl) {
1283 ssl->s3->next_proto_neg_seen = 0;
1284}
1285
1286static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1287 if (ssl->s3->initial_handshake_complete ||
1288 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001289 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001290 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001291 return 1;
1292 }
1293
1294 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1295 !CBB_add_u16(out, 0 /* length */)) {
1296 return 0;
1297 }
1298
1299 return 1;
1300}
1301
1302static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1303 CBS *contents) {
1304 if (contents == NULL) {
1305 return 1;
1306 }
1307
Steven Valdez143e8b32016-07-11 13:19:03 -04001308 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1309 return 0;
1310 }
1311
Adam Langley97dfcbf2015-07-01 18:35:20 -07001312 /* If any of these are false then we should never have sent the NPN
1313 * extension in the ClientHello and thus this function should never have been
1314 * called. */
1315 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001316 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001317 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001318 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001319
David Benjamin76c2efc2015-08-31 14:24:29 -04001320 if (ssl->s3->alpn_selected != NULL) {
1321 /* NPN and ALPN may not be negotiated in the same connection. */
1322 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1323 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1324 return 0;
1325 }
1326
Adam Langley97dfcbf2015-07-01 18:35:20 -07001327 const uint8_t *const orig_contents = CBS_data(contents);
1328 const size_t orig_len = CBS_len(contents);
1329
1330 while (CBS_len(contents) != 0) {
1331 CBS proto;
1332 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1333 CBS_len(&proto) == 0) {
1334 return 0;
1335 }
1336 }
1337
1338 uint8_t *selected;
1339 uint8_t selected_len;
1340 if (ssl->ctx->next_proto_select_cb(
1341 ssl, &selected, &selected_len, orig_contents, orig_len,
1342 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1343 *out_alert = SSL_AD_INTERNAL_ERROR;
1344 return 0;
1345 }
1346
David Benjamin79978df2015-12-25 15:56:49 -05001347 OPENSSL_free(ssl->s3->next_proto_negotiated);
1348 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1349 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001350 *out_alert = SSL_AD_INTERNAL_ERROR;
1351 return 0;
1352 }
1353
David Benjamin79978df2015-12-25 15:56:49 -05001354 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001355 ssl->s3->next_proto_neg_seen = 1;
1356
1357 return 1;
1358}
1359
1360static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1361 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001362 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1363 return 1;
1364 }
1365
Adam Langley97dfcbf2015-07-01 18:35:20 -07001366 if (contents != NULL && CBS_len(contents) != 0) {
1367 return 0;
1368 }
1369
1370 if (contents == NULL ||
1371 ssl->s3->initial_handshake_complete ||
1372 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1373 * afterwards, parsing the ALPN extension will clear
1374 * |next_proto_neg_seen|. */
1375 ssl->s3->alpn_selected != NULL ||
1376 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001377 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001378 return 1;
1379 }
1380
1381 ssl->s3->next_proto_neg_seen = 1;
1382 return 1;
1383}
1384
1385static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1386 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1387 * parsed. */
1388 if (!ssl->s3->next_proto_neg_seen) {
1389 return 1;
1390 }
1391
1392 const uint8_t *npa;
1393 unsigned npa_len;
1394
1395 if (ssl->ctx->next_protos_advertised_cb(
1396 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1397 SSL_TLSEXT_ERR_OK) {
1398 ssl->s3->next_proto_neg_seen = 0;
1399 return 1;
1400 }
1401
1402 CBB contents;
1403 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1404 !CBB_add_u16_length_prefixed(out, &contents) ||
1405 !CBB_add_bytes(&contents, npa, npa_len) ||
1406 !CBB_flush(out)) {
1407 return 0;
1408 }
1409
1410 return 1;
1411}
1412
1413
Adam Langleyab8d87d2015-07-10 12:21:39 -07001414/* Signed certificate timestamps.
1415 *
1416 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1417
1418static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1419 if (!ssl->signed_cert_timestamps_enabled) {
1420 return 1;
1421 }
1422
1423 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1424 !CBB_add_u16(out, 0 /* length */)) {
1425 return 0;
1426 }
1427
1428 return 1;
1429}
1430
1431static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1432 CBS *contents) {
1433 if (contents == NULL) {
1434 return 1;
1435 }
1436
1437 /* If this is false then we should never have sent the SCT extension in the
1438 * ClientHello and thus this function should never have been called. */
1439 assert(ssl->signed_cert_timestamps_enabled);
1440
1441 if (CBS_len(contents) == 0) {
1442 *out_alert = SSL_AD_DECODE_ERROR;
1443 return 0;
1444 }
1445
1446 /* Session resumption uses the original session information. */
Steven Valdez87eab492016-06-27 16:34:59 -04001447 if (ssl->session == NULL &&
1448 !CBS_stow(
1449 contents,
1450 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1451 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001452 *out_alert = SSL_AD_INTERNAL_ERROR;
1453 return 0;
1454 }
1455
1456 return 1;
1457}
1458
1459static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1460 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001461 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001462}
1463
1464static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001465 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001466 if (ssl->session != NULL ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001467 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001468 return 1;
1469 }
1470
1471 CBB contents;
1472 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1473 CBB_add_u16_length_prefixed(out, &contents) &&
1474 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1475 ssl->ctx->signed_cert_timestamp_list_length) &&
1476 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001477}
1478
1479
Adam Langleyf18e4532015-07-10 13:39:53 -07001480/* Application-level Protocol Negotiation.
1481 *
1482 * https://tools.ietf.org/html/rfc7301 */
1483
1484static void ext_alpn_init(SSL *ssl) {
1485 OPENSSL_free(ssl->s3->alpn_selected);
1486 ssl->s3->alpn_selected = NULL;
1487}
1488
1489static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1490 if (ssl->alpn_client_proto_list == NULL ||
1491 ssl->s3->initial_handshake_complete) {
1492 return 1;
1493 }
1494
1495 CBB contents, proto_list;
1496 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1497 !CBB_add_u16_length_prefixed(out, &contents) ||
1498 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1499 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1500 ssl->alpn_client_proto_list_len) ||
1501 !CBB_flush(out)) {
1502 return 0;
1503 }
1504
1505 return 1;
1506}
1507
1508static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1509 CBS *contents) {
1510 if (contents == NULL) {
1511 return 1;
1512 }
1513
1514 assert(!ssl->s3->initial_handshake_complete);
1515 assert(ssl->alpn_client_proto_list != NULL);
1516
David Benjamin76c2efc2015-08-31 14:24:29 -04001517 if (ssl->s3->next_proto_neg_seen) {
1518 /* NPN and ALPN may not be negotiated in the same connection. */
1519 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1520 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1521 return 0;
1522 }
1523
Adam Langleyf18e4532015-07-10 13:39:53 -07001524 /* The extension data consists of a ProtocolNameList which must have
1525 * exactly one ProtocolName. Each of these is length-prefixed. */
1526 CBS protocol_name_list, protocol_name;
1527 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1528 CBS_len(contents) != 0 ||
1529 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1530 /* Empty protocol names are forbidden. */
1531 CBS_len(&protocol_name) == 0 ||
1532 CBS_len(&protocol_name_list) != 0) {
1533 return 0;
1534 }
1535
1536 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1537 &ssl->s3->alpn_selected_len)) {
1538 *out_alert = SSL_AD_INTERNAL_ERROR;
1539 return 0;
1540 }
1541
1542 return 1;
1543}
1544
1545static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1546 CBS *contents) {
1547 if (contents == NULL) {
1548 return 1;
1549 }
1550
1551 if (ssl->ctx->alpn_select_cb == NULL ||
1552 ssl->s3->initial_handshake_complete) {
1553 return 1;
1554 }
1555
1556 /* ALPN takes precedence over NPN. */
1557 ssl->s3->next_proto_neg_seen = 0;
1558
1559 CBS protocol_name_list;
1560 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1561 CBS_len(contents) != 0 ||
1562 CBS_len(&protocol_name_list) < 2) {
1563 return 0;
1564 }
1565
1566 /* Validate the protocol list. */
1567 CBS protocol_name_list_copy = protocol_name_list;
1568 while (CBS_len(&protocol_name_list_copy) > 0) {
1569 CBS protocol_name;
1570
1571 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1572 /* Empty protocol names are forbidden. */
1573 CBS_len(&protocol_name) == 0) {
1574 return 0;
1575 }
1576 }
1577
1578 const uint8_t *selected;
1579 uint8_t selected_len;
1580 if (ssl->ctx->alpn_select_cb(
1581 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1582 CBS_len(&protocol_name_list),
1583 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1584 OPENSSL_free(ssl->s3->alpn_selected);
1585 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1586 if (ssl->s3->alpn_selected == NULL) {
1587 *out_alert = SSL_AD_INTERNAL_ERROR;
1588 return 0;
1589 }
1590 ssl->s3->alpn_selected_len = selected_len;
1591 }
1592
1593 return 1;
1594}
1595
1596static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1597 if (ssl->s3->alpn_selected == NULL) {
1598 return 1;
1599 }
1600
1601 CBB contents, proto_list, proto;
1602 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1603 !CBB_add_u16_length_prefixed(out, &contents) ||
1604 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1605 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001606 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1607 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001608 !CBB_flush(out)) {
1609 return 0;
1610 }
1611
1612 return 1;
1613}
1614
1615
Adam Langley49c7af12015-07-10 14:33:46 -07001616/* Channel ID.
1617 *
1618 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1619
1620static void ext_channel_id_init(SSL *ssl) {
1621 ssl->s3->tlsext_channel_id_valid = 0;
1622}
1623
1624static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1625 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001626 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001627 return 1;
1628 }
1629
1630 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1631 !CBB_add_u16(out, 0 /* length */)) {
1632 return 0;
1633 }
1634
1635 return 1;
1636}
1637
1638static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1639 CBS *contents) {
1640 if (contents == NULL) {
1641 return 1;
1642 }
1643
Steven Valdez143e8b32016-07-11 13:19:03 -04001644 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1645 return 0;
1646 }
1647
David Benjamince079fd2016-08-02 16:22:34 -04001648 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001649 assert(ssl->tlsext_channel_id_enabled);
1650
1651 if (CBS_len(contents) != 0) {
1652 return 0;
1653 }
1654
1655 ssl->s3->tlsext_channel_id_valid = 1;
1656 return 1;
1657}
1658
1659static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1660 CBS *contents) {
1661 if (contents == NULL ||
1662 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001663 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001664 return 1;
1665 }
1666
1667 if (CBS_len(contents) != 0) {
1668 return 0;
1669 }
1670
1671 ssl->s3->tlsext_channel_id_valid = 1;
1672 return 1;
1673}
1674
1675static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001676 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1677 return 1;
1678 }
1679
Adam Langley49c7af12015-07-10 14:33:46 -07001680 if (!ssl->s3->tlsext_channel_id_valid) {
1681 return 1;
1682 }
1683
1684 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1685 !CBB_add_u16(out, 0 /* length */)) {
1686 return 0;
1687 }
1688
1689 return 1;
1690}
1691
Adam Langley391250d2015-07-15 19:06:07 -07001692
1693/* Secure Real-time Transport Protocol (SRTP) extension.
1694 *
1695 * https://tools.ietf.org/html/rfc5764 */
1696
Adam Langley391250d2015-07-15 19:06:07 -07001697
1698static void ext_srtp_init(SSL *ssl) {
1699 ssl->srtp_profile = NULL;
1700}
1701
1702static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1703 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1704 if (profiles == NULL) {
1705 return 1;
1706 }
1707 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1708 if (num_profiles == 0) {
1709 return 1;
1710 }
1711
1712 CBB contents, profile_ids;
1713 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1714 !CBB_add_u16_length_prefixed(out, &contents) ||
1715 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1716 return 0;
1717 }
1718
1719 size_t i;
1720 for (i = 0; i < num_profiles; i++) {
1721 if (!CBB_add_u16(&profile_ids,
1722 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1723 return 0;
1724 }
1725 }
1726
1727 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1728 !CBB_flush(out)) {
1729 return 0;
1730 }
1731
1732 return 1;
1733}
1734
1735static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1736 CBS *contents) {
1737 if (contents == NULL) {
1738 return 1;
1739 }
1740
1741 /* The extension consists of a u16-prefixed profile ID list containing a
1742 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1743 *
1744 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1745 CBS profile_ids, srtp_mki;
1746 uint16_t profile_id;
1747 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1748 !CBS_get_u16(&profile_ids, &profile_id) ||
1749 CBS_len(&profile_ids) != 0 ||
1750 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1751 CBS_len(contents) != 0) {
1752 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1753 return 0;
1754 }
1755
1756 if (CBS_len(&srtp_mki) != 0) {
1757 /* Must be no MKI, since we never offer one. */
1758 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1759 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1760 return 0;
1761 }
1762
1763 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1764
1765 /* Check to see if the server gave us something we support (and presumably
1766 * offered). */
1767 size_t i;
1768 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1769 const SRTP_PROTECTION_PROFILE *profile =
1770 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1771
1772 if (profile->id == profile_id) {
1773 ssl->srtp_profile = profile;
1774 return 1;
1775 }
1776 }
1777
1778 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1779 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1780 return 0;
1781}
1782
1783static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1784 CBS *contents) {
1785 if (contents == NULL) {
1786 return 1;
1787 }
1788
1789 CBS profile_ids, srtp_mki;
1790 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1791 CBS_len(&profile_ids) < 2 ||
1792 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1793 CBS_len(contents) != 0) {
1794 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1795 return 0;
1796 }
1797 /* Discard the MKI value for now. */
1798
1799 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1800 SSL_get_srtp_profiles(ssl);
1801
1802 /* Pick the server's most preferred profile. */
1803 size_t i;
1804 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1805 const SRTP_PROTECTION_PROFILE *server_profile =
1806 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1807
1808 CBS profile_ids_tmp;
1809 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1810
1811 while (CBS_len(&profile_ids_tmp) > 0) {
1812 uint16_t profile_id;
1813 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1814 return 0;
1815 }
1816
1817 if (server_profile->id == profile_id) {
1818 ssl->srtp_profile = server_profile;
1819 return 1;
1820 }
1821 }
1822 }
1823
1824 return 1;
1825}
1826
1827static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1828 if (ssl->srtp_profile == NULL) {
1829 return 1;
1830 }
1831
1832 CBB contents, profile_ids;
1833 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1834 !CBB_add_u16_length_prefixed(out, &contents) ||
1835 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1836 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1837 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1838 !CBB_flush(out)) {
1839 return 0;
1840 }
1841
1842 return 1;
1843}
1844
Adam Langleybdd5d662015-07-20 16:19:08 -07001845
1846/* EC point formats.
1847 *
1848 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1849
1850static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001851 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001852 return 0;
1853 }
1854
1855 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1856
1857 size_t i;
1858 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1859 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1860
1861 const uint32_t alg_k = cipher->algorithm_mkey;
1862 const uint32_t alg_a = cipher->algorithm_auth;
1863 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1864 return 1;
1865 }
1866 }
1867
1868 return 0;
1869}
1870
Adam Langleybdd5d662015-07-20 16:19:08 -07001871static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001872 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001873 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1874 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001875 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1876 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001877 !CBB_flush(out)) {
1878 return 0;
1879 }
1880
1881 return 1;
1882}
1883
1884static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1885 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1886 return 1;
1887 }
1888
1889 return ext_ec_point_add_extension(ssl, out);
1890}
1891
1892static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1893 CBS *contents) {
1894 if (contents == NULL) {
1895 return 1;
1896 }
1897
Steven Valdez143e8b32016-07-11 13:19:03 -04001898 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1899 return 0;
1900 }
1901
Adam Langleybdd5d662015-07-20 16:19:08 -07001902 CBS ec_point_format_list;
1903 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1904 CBS_len(contents) != 0) {
1905 return 0;
1906 }
1907
David Benjaminfc059942015-07-30 23:01:59 -04001908 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1909 * point format. */
1910 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1911 CBS_len(&ec_point_format_list)) == NULL) {
1912 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001913 return 0;
1914 }
1915
1916 return 1;
1917}
1918
1919static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1920 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001921 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1922 return 1;
1923 }
1924
Adam Langleybdd5d662015-07-20 16:19:08 -07001925 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1926}
1927
1928static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001929 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1930 return 1;
1931 }
1932
Adam Langleybdd5d662015-07-20 16:19:08 -07001933 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1934 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001935 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001936
1937 if (!using_ecc) {
1938 return 1;
1939 }
1940
1941 return ext_ec_point_add_extension(ssl, out);
1942}
1943
Adam Langley273d49c2015-07-20 16:38:52 -07001944
Steven Valdez143e8b32016-07-11 13:19:03 -04001945/* Draft Version Extension */
1946
1947static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1948 uint16_t min_version, max_version;
1949 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001950 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001951 return 1;
1952 }
1953
1954 CBB contents;
1955 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1956 !CBB_add_u16_length_prefixed(out, &contents) ||
1957 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1958 return 0;
1959 }
1960
1961 return CBB_flush(out);
1962}
1963
1964
1965/* Key Share
1966 *
1967 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
1968
1969static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
1970 uint16_t min_version, max_version;
1971 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1972 return 0;
1973 }
1974
1975 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
1976 return 1;
1977 }
1978
1979 CBB contents, kse_bytes;
1980 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
1981 !CBB_add_u16_length_prefixed(out, &contents) ||
1982 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
1983 return 0;
1984 }
1985
1986 const uint16_t *groups;
1987 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04001988 if (ssl->s3->hs->retry_group) {
1989 /* Append the new key share to the old list. */
1990 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
1991 ssl->s3->hs->key_share_bytes_len)) {
1992 return 0;
1993 }
1994 OPENSSL_free(ssl->s3->hs->key_share_bytes);
1995 ssl->s3->hs->key_share_bytes = NULL;
1996
1997 groups = &ssl->s3->hs->retry_group;
1998 groups_len = 1;
1999 } else {
2000 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2001 /* Only send the top two preferred key shares. */
2002 if (groups_len > 2) {
2003 groups_len = 2;
2004 }
2005 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002006
2007 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2008 if (ssl->s3->hs->groups == NULL) {
2009 return 0;
2010 }
2011 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2012 ssl->s3->hs->groups_len = groups_len;
2013
2014 for (size_t i = 0; i < groups_len; i++) {
2015 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2016 return 0;
2017 }
2018
2019 CBB key_exchange;
2020 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2021 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2022 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2023 !CBB_flush(&kse_bytes)) {
2024 return 0;
2025 }
2026 }
2027
Steven Valdez5440fe02016-07-18 12:40:30 -04002028 if (!ssl->s3->hs->retry_group) {
2029 /* Save the contents of the extension to repeat it in the second
2030 * ClientHello. */
2031 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2032 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2033 CBB_len(&kse_bytes));
2034 if (ssl->s3->hs->key_share_bytes == NULL) {
2035 return 0;
2036 }
2037 }
2038
Steven Valdez143e8b32016-07-11 13:19:03 -04002039 return CBB_flush(out);
2040}
2041
Steven Valdez7259f2f2016-08-02 16:55:05 -04002042int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2043 size_t *out_secret_len,
2044 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002045 CBS peer_key;
2046 uint16_t group;
2047 if (!CBS_get_u16(contents, &group) ||
David Benjamina70de142016-08-02 16:52:57 -04002048 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2049 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002050 *out_alert = SSL_AD_DECODE_ERROR;
2051 return 0;
2052 }
2053
2054 SSL_ECDH_CTX *group_ctx = NULL;
2055 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2056 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2057 group_ctx = &ssl->s3->hs->groups[i];
2058 break;
2059 }
2060 }
2061
2062 if (group_ctx == NULL) {
2063 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2064 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2065 return 0;
2066 }
2067
2068 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2069 CBS_data(&peer_key), CBS_len(&peer_key))) {
2070 *out_alert = SSL_AD_INTERNAL_ERROR;
2071 return 0;
2072 }
2073
Steven Valdez5440fe02016-07-18 12:40:30 -04002074 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002075 return 1;
2076}
2077
Steven Valdez7259f2f2016-08-02 16:55:05 -04002078int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2079 uint8_t **out_secret,
2080 size_t *out_secret_len,
2081 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002082 uint16_t group_id;
2083 CBS key_shares;
2084 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002085 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2086 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002087 return 0;
2088 }
2089
Steven Valdez5440fe02016-07-18 12:40:30 -04002090 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002091 while (CBS_len(&key_shares) > 0) {
2092 uint16_t id;
2093 CBS peer_key;
2094 if (!CBS_get_u16(&key_shares, &id) ||
2095 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2096 return 0;
2097 }
2098
Steven Valdez5440fe02016-07-18 12:40:30 -04002099 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002100 continue;
2101 }
2102
2103 SSL_ECDH_CTX group;
2104 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2105 CBB public_key;
2106 if (!CBB_init(&public_key, 0) ||
2107 !SSL_ECDH_CTX_init(&group, group_id) ||
2108 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2109 out_alert, CBS_data(&peer_key),
2110 CBS_len(&peer_key)) ||
2111 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2112 &ssl->s3->hs->public_key_len)) {
2113 SSL_ECDH_CTX_cleanup(&group);
2114 CBB_cleanup(&public_key);
2115 return 0;
2116 }
2117 SSL_ECDH_CTX_cleanup(&group);
2118
Steven Valdez5440fe02016-07-18 12:40:30 -04002119 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002120 }
2121
Steven Valdez5440fe02016-07-18 12:40:30 -04002122 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002123}
2124
Steven Valdez7259f2f2016-08-02 16:55:05 -04002125int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002126 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2127 return 1;
2128 }
2129
2130 uint16_t group_id;
2131 CBB kse_bytes, public_key;
2132 if (!tls1_get_shared_group(ssl, &group_id) ||
2133 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2134 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2135 !CBB_add_u16(&kse_bytes, group_id) ||
2136 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2137 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2138 ssl->s3->hs->public_key_len) ||
2139 !CBB_flush(out)) {
2140 return 0;
2141 }
2142
2143 return 1;
2144}
2145
2146
Steven Valdezce902a92016-05-17 11:47:53 -04002147/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002148 *
Steven Valdezce902a92016-05-17 11:47:53 -04002149 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2150 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002151
Steven Valdezce902a92016-05-17 11:47:53 -04002152static void ext_supported_groups_init(SSL *ssl) {
2153 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2154 ssl->s3->tmp.peer_supported_group_list = NULL;
2155 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002156}
2157
Steven Valdezce902a92016-05-17 11:47:53 -04002158static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002159 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2160 return 1;
2161 }
2162
Steven Valdezce902a92016-05-17 11:47:53 -04002163 CBB contents, groups_bytes;
2164 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002165 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002166 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002167 return 0;
2168 }
2169
Steven Valdezce902a92016-05-17 11:47:53 -04002170 const uint16_t *groups;
2171 size_t groups_len;
2172 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002173
2174 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002175 for (i = 0; i < groups_len; i++) {
2176 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002177 return 0;
2178 }
2179 }
2180
2181 return CBB_flush(out);
2182}
2183
Steven Valdezce902a92016-05-17 11:47:53 -04002184static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2185 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002186 /* This extension is not expected to be echoed by servers and is ignored. */
2187 return 1;
2188}
2189
Steven Valdezce902a92016-05-17 11:47:53 -04002190static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2191 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002192 if (contents == NULL) {
2193 return 1;
2194 }
2195
Steven Valdezce902a92016-05-17 11:47:53 -04002196 CBS supported_group_list;
2197 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2198 CBS_len(&supported_group_list) == 0 ||
2199 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002200 CBS_len(contents) != 0) {
2201 return 0;
2202 }
2203
Steven Valdezce902a92016-05-17 11:47:53 -04002204 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2205 CBS_len(&supported_group_list));
2206 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002207 *out_alert = SSL_AD_INTERNAL_ERROR;
2208 return 0;
2209 }
2210
Steven Valdezce902a92016-05-17 11:47:53 -04002211 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002212 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002213 for (i = 0; i < num_groups; i++) {
2214 if (!CBS_get_u16(&supported_group_list,
2215 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002216 goto err;
2217 }
2218 }
2219
Steven Valdezce902a92016-05-17 11:47:53 -04002220 assert(CBS_len(&supported_group_list) == 0);
2221 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002222
2223 return 1;
2224
2225err:
Steven Valdezce902a92016-05-17 11:47:53 -04002226 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2227 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002228 *out_alert = SSL_AD_INTERNAL_ERROR;
2229 return 0;
2230}
2231
Steven Valdezce902a92016-05-17 11:47:53 -04002232static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002233 /* Servers don't echo this extension. */
2234 return 1;
2235}
2236
2237
Adam Langley614c66a2015-06-12 15:26:58 -07002238/* kExtensions contains all the supported extensions. */
2239static const struct tls_extension kExtensions[] = {
2240 {
Adam Langley5021b222015-06-12 18:27:58 -07002241 /* The renegotiation extension must always be at index zero because the
2242 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2243 * sent as an SCSV. */
2244 TLSEXT_TYPE_renegotiate,
2245 NULL,
2246 ext_ri_add_clienthello,
2247 ext_ri_parse_serverhello,
2248 ext_ri_parse_clienthello,
2249 ext_ri_add_serverhello,
2250 },
2251 {
Adam Langley614c66a2015-06-12 15:26:58 -07002252 TLSEXT_TYPE_server_name,
2253 ext_sni_init,
2254 ext_sni_add_clienthello,
2255 ext_sni_parse_serverhello,
2256 ext_sni_parse_clienthello,
2257 ext_sni_add_serverhello,
2258 },
Adam Langley0a056712015-07-01 15:03:33 -07002259 {
2260 TLSEXT_TYPE_extended_master_secret,
2261 ext_ems_init,
2262 ext_ems_add_clienthello,
2263 ext_ems_parse_serverhello,
2264 ext_ems_parse_clienthello,
2265 ext_ems_add_serverhello,
2266 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002267 {
2268 TLSEXT_TYPE_session_ticket,
2269 NULL,
2270 ext_ticket_add_clienthello,
2271 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002272 /* Ticket extension client parsing is handled in ssl_session.c */
2273 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002274 ext_ticket_add_serverhello,
2275 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002276 {
2277 TLSEXT_TYPE_signature_algorithms,
2278 NULL,
2279 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002280 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002281 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002282 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002283 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002284 {
2285 TLSEXT_TYPE_status_request,
2286 ext_ocsp_init,
2287 ext_ocsp_add_clienthello,
2288 ext_ocsp_parse_serverhello,
2289 ext_ocsp_parse_clienthello,
2290 ext_ocsp_add_serverhello,
2291 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002292 {
2293 TLSEXT_TYPE_next_proto_neg,
2294 ext_npn_init,
2295 ext_npn_add_clienthello,
2296 ext_npn_parse_serverhello,
2297 ext_npn_parse_clienthello,
2298 ext_npn_add_serverhello,
2299 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002300 {
2301 TLSEXT_TYPE_certificate_timestamp,
2302 NULL,
2303 ext_sct_add_clienthello,
2304 ext_sct_parse_serverhello,
2305 ext_sct_parse_clienthello,
2306 ext_sct_add_serverhello,
2307 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002308 {
2309 TLSEXT_TYPE_application_layer_protocol_negotiation,
2310 ext_alpn_init,
2311 ext_alpn_add_clienthello,
2312 ext_alpn_parse_serverhello,
2313 ext_alpn_parse_clienthello,
2314 ext_alpn_add_serverhello,
2315 },
Adam Langley49c7af12015-07-10 14:33:46 -07002316 {
2317 TLSEXT_TYPE_channel_id,
2318 ext_channel_id_init,
2319 ext_channel_id_add_clienthello,
2320 ext_channel_id_parse_serverhello,
2321 ext_channel_id_parse_clienthello,
2322 ext_channel_id_add_serverhello,
2323 },
Adam Langley391250d2015-07-15 19:06:07 -07002324 {
2325 TLSEXT_TYPE_srtp,
2326 ext_srtp_init,
2327 ext_srtp_add_clienthello,
2328 ext_srtp_parse_serverhello,
2329 ext_srtp_parse_clienthello,
2330 ext_srtp_add_serverhello,
2331 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002332 {
2333 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002334 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002335 ext_ec_point_add_clienthello,
2336 ext_ec_point_parse_serverhello,
2337 ext_ec_point_parse_clienthello,
2338 ext_ec_point_add_serverhello,
2339 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002340 {
2341 TLSEXT_TYPE_draft_version,
2342 NULL,
2343 ext_draft_version_add_clienthello,
2344 forbid_parse_serverhello,
2345 ignore_parse_clienthello,
2346 dont_add_serverhello,
2347 },
2348 {
2349 TLSEXT_TYPE_key_share,
2350 NULL,
2351 ext_key_share_add_clienthello,
2352 forbid_parse_serverhello,
2353 ignore_parse_clienthello,
2354 dont_add_serverhello,
2355 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002356 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2357 * intolerant to the last extension being zero-length. See
2358 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002359 {
Steven Valdezce902a92016-05-17 11:47:53 -04002360 TLSEXT_TYPE_supported_groups,
2361 ext_supported_groups_init,
2362 ext_supported_groups_add_clienthello,
2363 ext_supported_groups_parse_serverhello,
2364 ext_supported_groups_parse_clienthello,
2365 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002366 },
Adam Langley614c66a2015-06-12 15:26:58 -07002367};
2368
2369#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2370
Adam Langley4cfa96b2015-07-01 11:56:55 -07002371OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2372 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002373 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002374OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2375 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2376 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002377 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002378
Adam Langley614c66a2015-06-12 15:26:58 -07002379static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2380 uint16_t value) {
2381 unsigned i;
2382 for (i = 0; i < kNumExtensions; i++) {
2383 if (kExtensions[i].value == value) {
2384 *out_index = i;
2385 return &kExtensions[i];
2386 }
2387 }
2388
2389 return NULL;
2390}
2391
Adam Langley09505632015-07-30 18:10:13 -07002392int SSL_extension_supported(unsigned extension_value) {
2393 uint32_t index;
2394 return extension_value == TLSEXT_TYPE_padding ||
2395 tls_extension_find(&index, extension_value) != NULL;
2396}
2397
David Benjamine8d53502015-10-10 14:13:23 -04002398int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002399 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002400 if (ssl->client_version == SSL3_VERSION &&
2401 !ssl->s3->send_connection_binding) {
2402 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002403 }
Adam Langley95c29f32014-06-20 12:00:00 -07002404
David Benjamine8d53502015-10-10 14:13:23 -04002405 CBB extensions;
2406 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002407 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002408 }
Adam Langley95c29f32014-06-20 12:00:00 -07002409
David Benjamine8d53502015-10-10 14:13:23 -04002410 ssl->s3->tmp.extensions.sent = 0;
2411 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002412
Adam Langley614c66a2015-06-12 15:26:58 -07002413 size_t i;
2414 for (i = 0; i < kNumExtensions; i++) {
2415 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002416 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002417 }
2418 }
Adam Langley95c29f32014-06-20 12:00:00 -07002419
Adam Langley614c66a2015-06-12 15:26:58 -07002420 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002421 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002422 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002423 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2424 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2425 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002426 }
Adam Langley95c29f32014-06-20 12:00:00 -07002427
Adam Langley33ad2b52015-07-20 17:43:53 -07002428 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002429 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002430 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 }
Adam Langley75712922014-10-10 16:23:43 -07002432
David Benjamine8d53502015-10-10 14:13:23 -04002433 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002434 goto err;
2435 }
2436
David Benjamince079fd2016-08-02 16:22:34 -04002437 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002438 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002439 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002440 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002441 *
2442 * NB: because this code works out the length of all existing extensions
2443 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002444 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002445 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002446 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002447 * Server 7.0 is intolerant to the last extension being zero-length. See
2448 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002449 if (padding_len >= 4 + 1) {
2450 padding_len -= 4;
2451 } else {
2452 padding_len = 1;
2453 }
Adam Langley95c29f32014-06-20 12:00:00 -07002454
Adam Langley33ad2b52015-07-20 17:43:53 -07002455 uint8_t *padding_bytes;
2456 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2457 !CBB_add_u16(&extensions, padding_len) ||
2458 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2459 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002460 }
Adam Langley75712922014-10-10 16:23:43 -07002461
Adam Langley33ad2b52015-07-20 17:43:53 -07002462 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 }
2464 }
Adam Langley75712922014-10-10 16:23:43 -07002465
David Benjamina01deee2015-12-08 18:56:31 -05002466 /* Discard empty extensions blocks. */
2467 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002468 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002469 }
2470
David Benjamine8d53502015-10-10 14:13:23 -04002471 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002472
2473err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002474 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002475 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002476}
Adam Langley95c29f32014-06-20 12:00:00 -07002477
David Benjamin56380462015-10-10 14:59:09 -04002478int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002479 CBB extensions;
2480 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002481 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002482 }
2483
2484 unsigned i;
2485 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002486 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002487 /* Don't send extensions that were not received. */
2488 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002489 }
Adam Langley95c29f32014-06-20 12:00:00 -07002490
David Benjamin56380462015-10-10 14:59:09 -04002491 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002492 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2493 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2494 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002495 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002496 }
Adam Langley95c29f32014-06-20 12:00:00 -07002497
David Benjamin56380462015-10-10 14:59:09 -04002498 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002499 goto err;
2500 }
2501
Steven Valdez143e8b32016-07-11 13:19:03 -04002502 /* Discard empty extensions blocks before TLS 1.3. */
2503 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2504 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002505 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002506 }
2507
David Benjamin56380462015-10-10 14:59:09 -04002508 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002509
2510err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002511 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002512 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002513}
Adam Langley95c29f32014-06-20 12:00:00 -07002514
David Benjamin0d56f882015-12-19 17:05:56 -05002515static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Adam Langley614c66a2015-06-12 15:26:58 -07002516 size_t i;
2517 for (i = 0; i < kNumExtensions; i++) {
2518 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002519 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002520 }
2521 }
2522
David Benjamin0d56f882015-12-19 17:05:56 -05002523 ssl->s3->tmp.extensions.received = 0;
2524 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley5021b222015-06-12 18:27:58 -07002525 /* The renegotiation extension must always be at index zero because the
2526 * |received| and |sent| bitsets need to be tweaked when the "extension" is
2527 * sent as an SCSV. */
2528 assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate);
Adam Langley614c66a2015-06-12 15:26:58 -07002529
Adam Langleyfcf25832014-12-18 17:42:32 -08002530 /* There may be no extensions. */
Adam Langley33ad2b52015-07-20 17:43:53 -07002531 if (CBS_len(cbs) != 0) {
2532 /* Decode the extensions block and check it is valid. */
2533 CBS extensions;
2534 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2535 !tls1_check_duplicate_extensions(&extensions)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002536 *out_alert = SSL_AD_DECODE_ERROR;
2537 return 0;
2538 }
Adam Langley95c29f32014-06-20 12:00:00 -07002539
Adam Langley33ad2b52015-07-20 17:43:53 -07002540 while (CBS_len(&extensions) != 0) {
2541 uint16_t type;
2542 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002543
Adam Langley33ad2b52015-07-20 17:43:53 -07002544 /* Decode the next extension. */
2545 if (!CBS_get_u16(&extensions, &type) ||
2546 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
2547 *out_alert = SSL_AD_DECODE_ERROR;
2548 return 0;
2549 }
2550
David Benjaminc7ce9772015-10-09 19:32:41 -04002551 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2552 * ambiguous. Ignore all but the renegotiation_info extension. */
David Benjamin0d56f882015-12-19 17:05:56 -05002553 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
David Benjaminc7ce9772015-10-09 19:32:41 -04002554 continue;
2555 }
2556
Adam Langley33ad2b52015-07-20 17:43:53 -07002557 unsigned ext_index;
2558 const struct tls_extension *const ext =
2559 tls_extension_find(&ext_index, type);
2560
2561 if (ext == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002562 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley09505632015-07-30 18:10:13 -07002563 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2564 return 0;
2565 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002566 continue;
2567 }
2568
David Benjamin0d56f882015-12-19 17:05:56 -05002569 ssl->s3->tmp.extensions.received |= (1u << ext_index);
Adam Langley614c66a2015-06-12 15:26:58 -07002570 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002571 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
Adam Langley614c66a2015-06-12 15:26:58 -07002572 *out_alert = alert;
Adam Langley33ad2b52015-07-20 17:43:53 -07002573 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2574 ERR_add_error_dataf("extension: %u", (unsigned)type);
Adam Langleyfcf25832014-12-18 17:42:32 -08002575 return 0;
2576 }
Adam Langley614c66a2015-06-12 15:26:58 -07002577 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002578 }
Adam Langley75712922014-10-10 16:23:43 -07002579
Adam Langley614c66a2015-06-12 15:26:58 -07002580 for (i = 0; i < kNumExtensions; i++) {
David Benjamin0d56f882015-12-19 17:05:56 -05002581 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002582 /* Extension wasn't observed so call the callback with a NULL
2583 * parameter. */
2584 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002585 if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002586 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2587 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002588 *out_alert = alert;
2589 return 0;
2590 }
2591 }
2592 }
2593
Adam Langleyfcf25832014-12-18 17:42:32 -08002594 return 1;
2595}
Adam Langley95c29f32014-06-20 12:00:00 -07002596
David Benjamin0d56f882015-12-19 17:05:56 -05002597int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002599 if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) {
2600 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 return 0;
2602 }
Adam Langley95c29f32014-06-20 12:00:00 -07002603
David Benjamin0d56f882015-12-19 17:05:56 -05002604 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002605 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002606 return 0;
2607 }
Adam Langley95c29f32014-06-20 12:00:00 -07002608
Adam Langleyfcf25832014-12-18 17:42:32 -08002609 return 1;
2610}
Adam Langley95c29f32014-06-20 12:00:00 -07002611
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002612OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2613
David Benjamin0d56f882015-12-19 17:05:56 -05002614static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002615 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2616 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2617 return 1;
2618 }
Adam Langley614c66a2015-06-12 15:26:58 -07002619
Steven Valdez143e8b32016-07-11 13:19:03 -04002620 /* Decode the extensions block and check it is valid. */
2621 CBS extensions;
2622 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2623 !tls1_check_duplicate_extensions(&extensions)) {
2624 *out_alert = SSL_AD_DECODE_ERROR;
2625 return 0;
2626 }
2627
2628 uint32_t received = 0;
2629 while (CBS_len(&extensions) != 0) {
2630 uint16_t type;
2631 CBS extension;
2632
2633 /* Decode the next extension. */
2634 if (!CBS_get_u16(&extensions, &type) ||
2635 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002636 *out_alert = SSL_AD_DECODE_ERROR;
2637 return 0;
2638 }
Adam Langley95c29f32014-06-20 12:00:00 -07002639
Steven Valdez143e8b32016-07-11 13:19:03 -04002640 unsigned ext_index;
2641 const struct tls_extension *const ext =
2642 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002643
Steven Valdez143e8b32016-07-11 13:19:03 -04002644 if (ext == NULL) {
2645 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002646 return 0;
2647 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002648 continue;
2649 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002650
Steven Valdez143e8b32016-07-11 13:19:03 -04002651 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) {
2652 /* If the extension was never sent then it is illegal. */
2653 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2654 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002655 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002656 return 0;
2657 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002658
Steven Valdez143e8b32016-07-11 13:19:03 -04002659 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002660
Steven Valdez143e8b32016-07-11 13:19:03 -04002661 uint8_t alert = SSL_AD_DECODE_ERROR;
2662 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2663 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2664 ERR_add_error_dataf("extension: %u", (unsigned)type);
2665 *out_alert = alert;
2666 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002667 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
Adam Langley95c29f32014-06-20 12:00:00 -07002669
Adam Langley33ad2b52015-07-20 17:43:53 -07002670 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002671 for (i = 0; i < kNumExtensions; i++) {
2672 if (!(received & (1u << i))) {
2673 /* Extension wasn't observed so call the callback with a NULL
2674 * parameter. */
2675 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002676 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002677 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2678 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002679 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002680 return 0;
2681 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002682 }
2683 }
Adam Langley95c29f32014-06-20 12:00:00 -07002684
Adam Langleyfcf25832014-12-18 17:42:32 -08002685 return 1;
2686}
Adam Langley95c29f32014-06-20 12:00:00 -07002687
David Benjamin0d56f882015-12-19 17:05:56 -05002688static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002689 int ret = SSL_TLSEXT_ERR_NOACK;
2690 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002691
David Benjamin78f8aab2016-03-10 16:33:58 -05002692 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002693 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002694 ssl->ctx->tlsext_servername_arg);
2695 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002696 ret = ssl->initial_ctx->tlsext_servername_callback(
2697 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002698 }
Adam Langley95c29f32014-06-20 12:00:00 -07002699
Adam Langleyfcf25832014-12-18 17:42:32 -08002700 switch (ret) {
2701 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002702 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002703 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002704
Adam Langleyfcf25832014-12-18 17:42:32 -08002705 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002706 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002708
Adam Langleyfcf25832014-12-18 17:42:32 -08002709 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002710 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002712
Adam Langleyfcf25832014-12-18 17:42:32 -08002713 default:
2714 return 1;
2715 }
2716}
Adam Langleyed8270a2014-09-02 13:52:56 -07002717
David Benjamin0d56f882015-12-19 17:05:56 -05002718static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002719 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002720 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002721
David Benjamin78f8aab2016-03-10 16:33:58 -05002722 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002723 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002724 ssl->ctx->tlsext_servername_arg);
2725 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002726 ret = ssl->initial_ctx->tlsext_servername_callback(
2727 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002728 }
Adam Langley95c29f32014-06-20 12:00:00 -07002729
Adam Langleyfcf25832014-12-18 17:42:32 -08002730 switch (ret) {
2731 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002732 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002733 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002734
Adam Langleyfcf25832014-12-18 17:42:32 -08002735 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002736 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002737 return 1;
2738
2739 default:
2740 return 1;
2741 }
2742}
2743
David Benjamin0d56f882015-12-19 17:05:56 -05002744int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002745 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002746 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2747 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002748 return 0;
2749 }
2750
David Benjamin0d56f882015-12-19 17:05:56 -05002751 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002752 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002753 return 0;
2754 }
2755
2756 return 1;
2757}
Adam Langley95c29f32014-06-20 12:00:00 -07002758
David Benjamine3aa1d92015-06-16 15:34:50 -04002759int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002760 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002761 size_t ticket_len, const uint8_t *session_id,
2762 size_t session_id_len) {
2763 int ret = 1; /* Most errors are non-fatal. */
2764 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2765 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002766
David Benjamine3aa1d92015-06-16 15:34:50 -04002767 HMAC_CTX hmac_ctx;
2768 HMAC_CTX_init(&hmac_ctx);
2769 EVP_CIPHER_CTX cipher_ctx;
2770 EVP_CIPHER_CTX_init(&cipher_ctx);
2771
David Benjaminef1b0092015-11-21 14:05:44 -05002772 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002773 *out_session = NULL;
2774
2775 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2776 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002777 }
2778
David Benjaminadcc3952015-04-26 13:07:57 -04002779 /* Ensure there is room for the key name and the largest IV
2780 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2781 * the maximum IV length should be well under the minimum size for the
2782 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002783 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2784 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002785 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002786 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002787
David Benjamine3aa1d92015-06-16 15:34:50 -04002788 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002789 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2790 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2791 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002792 if (cb_ret < 0) {
2793 ret = 0;
2794 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002796 if (cb_ret == 0) {
2797 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002798 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002799 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002800 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002801 }
2802 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002803 /* Check the key name matches. */
2804 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2805 SSL_TICKET_KEY_NAME_LEN) != 0) {
2806 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002807 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002808 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2809 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002810 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002811 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2812 ssl_ctx->tlsext_tick_aes_key, iv)) {
2813 ret = 0;
2814 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 }
2816 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002817 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002818
David Benjamine3aa1d92015-06-16 15:34:50 -04002819 /* Check the MAC at the end of the ticket. */
2820 uint8_t mac[EVP_MAX_MD_SIZE];
2821 size_t mac_len = HMAC_size(&hmac_ctx);
2822 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002823 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002824 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002825 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002826 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2827 HMAC_Final(&hmac_ctx, mac, NULL);
2828 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2829 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002830 }
2831
David Benjamine3aa1d92015-06-16 15:34:50 -04002832 /* Decrypt the session data. */
2833 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2834 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2835 mac_len;
2836 plaintext = OPENSSL_malloc(ciphertext_len);
2837 if (plaintext == NULL) {
2838 ret = 0;
2839 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002840 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002841 if (ciphertext_len >= INT_MAX) {
2842 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002844 int len1, len2;
2845 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2846 (int)ciphertext_len) ||
2847 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2848 ERR_clear_error(); /* Don't leave an error on the queue. */
2849 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 }
2851
David Benjamine3aa1d92015-06-16 15:34:50 -04002852 /* Decode the session. */
2853 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2854 if (session == NULL) {
2855 ERR_clear_error(); /* Don't leave an error on the queue. */
2856 goto done;
2857 }
2858
2859 /* Copy the client's session ID into the new session, to denote the ticket has
2860 * been accepted. */
2861 memcpy(session->session_id, session_id, session_id_len);
2862 session->session_id_length = session_id_len;
2863
2864 *out_session = session;
2865
2866done:
2867 OPENSSL_free(plaintext);
2868 HMAC_CTX_cleanup(&hmac_ctx);
2869 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2870 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002871}
Adam Langley95c29f32014-06-20 12:00:00 -07002872
Steven Valdez0d62f262015-09-04 12:41:04 -04002873int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002874 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002875 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002876 return 1;
2877 }
David Benjamincd996942014-07-20 16:23:51 -04002878
Steven Valdez0d62f262015-09-04 12:41:04 -04002879 CERT *const cert = ssl->cert;
2880 OPENSSL_free(cert->peer_sigalgs);
2881 cert->peer_sigalgs = NULL;
2882 cert->peer_sigalgslen = 0;
2883
2884 size_t num_sigalgs = CBS_len(in_sigalgs);
2885
2886 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002887 return 0;
2888 }
Steven Valdez0d62f262015-09-04 12:41:04 -04002889 num_sigalgs /= 2;
2890
2891 /* supported_signature_algorithms in the certificate request is
2892 * allowed to be empty. */
2893 if (num_sigalgs == 0) {
2894 return 1;
2895 }
2896
Steven Valdez02563852016-06-23 13:33:05 -04002897 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
2898 * and we just divided |num_sigalgs| by two. */
2899 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04002900 if (cert->peer_sigalgs == NULL) {
2901 return 0;
2902 }
2903 cert->peer_sigalgslen = num_sigalgs;
2904
2905 CBS sigalgs;
2906 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
2907
2908 size_t i;
2909 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04002910 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04002911 return 0;
2912 }
2913 }
Adam Langley95c29f32014-06-20 12:00:00 -07002914
Adam Langleyfcf25832014-12-18 17:42:32 -08002915 return 1;
2916}
David Benjaminec2f27d2014-11-13 19:17:25 -05002917
David Benjaminea9a0d52016-07-08 15:52:59 -07002918int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04002919 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04002920 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05002921
Steven Valdezf0451ca2016-06-29 13:16:27 -04002922 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
2923 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
2924 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04002925 int type = ssl_private_key_type(ssl);
2926 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002927 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04002928 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002929 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04002930 if (ssl_is_ecdsa_key_type(type)) {
2931 *out = SSL_SIGN_ECDSA_SHA1;
2932 return 1;
2933 }
2934 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2935 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002936 }
2937
Steven Valdezeff1e8d2016-07-06 14:24:47 -04002938 const uint16_t *sigalgs;
2939 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07002940 if (cert->sigalgs != NULL) {
2941 sigalgs = cert->sigalgs;
2942 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04002943 }
2944
David Benjaminea9a0d52016-07-08 15:52:59 -07002945 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
2946 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07002947 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07002948 /* If the client didn't specify any signature_algorithms extension then
2949 * we can assume that it supports SHA1. See
2950 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
2951 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
2952 SSL_SIGN_ECDSA_SHA1};
2953 peer_sigalgs = kDefaultPeerAlgorithms;
2954 peer_sigalgs_len =
2955 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
2956 }
2957
David Benjamind246b812016-07-08 15:07:02 -07002958 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002959 uint16_t sigalg = sigalgs[i];
2960 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
2961 * negotiated. */
2962 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
2963 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
2964 continue;
2965 }
2966
David Benjaminea9a0d52016-07-08 15:52:59 -07002967 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07002968 if (sigalg == peer_sigalgs[j]) {
2969 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07002970 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04002971 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002972 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002973 }
Adam Langley95c29f32014-06-20 12:00:00 -07002974
David Benjaminea9a0d52016-07-08 15:52:59 -07002975 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
2976 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002977}
Adam Langley95c29f32014-06-20 12:00:00 -07002978
David Benjamind6a4ae92015-08-06 11:10:51 -04002979int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
2980 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002981 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08002982
2983 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04002984 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
2985 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002986 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002987
David Benjamind6a4ae92015-08-06 11:10:51 -04002988 static const char kClientIDMagic[] = "TLS Channel ID signature";
2989 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
2990
Steven Valdez87eab492016-06-27 16:34:59 -04002991 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04002992 static const char kResumptionMagic[] = "Resumption";
2993 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
2994 if (ssl->session->original_handshake_hash_len == 0) {
2995 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
2996 goto err;
2997 }
2998 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
2999 ssl->session->original_handshake_hash_len);
3000 }
3001
3002 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3003 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3004 sizeof(handshake_hash));
3005 if (handshake_hash_len < 0) {
3006 goto err;
3007 }
3008 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3009 unsigned len_u;
3010 EVP_DigestFinal_ex(&ctx, out, &len_u);
3011 *out_len = len_u;
3012
3013 ret = 1;
3014
3015err:
3016 EVP_MD_CTX_cleanup(&ctx);
3017 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003018}
Adam Langley1258b6a2014-06-20 12:00:00 -07003019
3020/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003021 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003022 * data. */
3023int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003024 int digest_len;
3025 /* This function should never be called for a resumed session because the
3026 * handshake hashes that we wish to record are for the original, full
3027 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003028 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003029 return -1;
3030 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003031
Adam Langleyfcf25832014-12-18 17:42:32 -08003032 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003033 tls1_handshake_digest(
3034 ssl, ssl->s3->new_session->original_handshake_hash,
3035 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003036 if (digest_len < 0) {
3037 return -1;
3038 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003039
Steven Valdez87eab492016-06-27 16:34:59 -04003040 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003041
Adam Langleyfcf25832014-12-18 17:42:32 -08003042 return 1;
3043}