blob: dc6c6ddc4708cb40c14efd1327bdd2425de7ad20 [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 Benjamine14ff062016-08-09 16:21:24 -0400212 CBS client_hello, random, session_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
David Benjamine14ff062016-08-09 16:21:24 -0400214 if (!CBS_get_u16(&client_hello, &ctx->version) ||
215 !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
216 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
217 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 return 0;
219 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700220
David Benjamine14ff062016-08-09 16:21:24 -0400221 ctx->random = CBS_data(&random);
222 ctx->random_len = CBS_len(&random);
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 ctx->session_id = CBS_data(&session_id);
224 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 /* Skip past DTLS cookie */
David Benjamince079fd2016-08-02 16:22:34 -0400227 if (SSL_is_dtls(ctx->ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 CBS cookie;
David Benjamine14ff062016-08-09 16:21:24 -0400229 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
230 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 return 0;
232 }
233 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700234
David Benjamine14ff062016-08-09 16:21:24 -0400235 CBS cipher_suites, compression_methods;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamine14ff062016-08-09 16:21:24 -0400237 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
238 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 CBS_len(&compression_methods) < 1) {
240 return 0;
241 }
David Benjamine14ff062016-08-09 16:21:24 -0400242
243 ctx->cipher_suites = CBS_data(&cipher_suites);
244 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ctx->compression_methods = CBS_data(&compression_methods);
246 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 /* If the ClientHello ends here then it's valid, but doesn't have any
249 * extensions. (E.g. SSLv3.) */
250 if (CBS_len(&client_hello) == 0) {
251 ctx->extensions = NULL;
252 ctx->extensions_len = 0;
253 return 1;
254 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700255
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 /* Extract extensions and check it is valid. */
David Benjamine14ff062016-08-09 16:21:24 -0400257 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
259 !tls1_check_duplicate_extensions(&extensions) ||
260 CBS_len(&client_hello) != 0) {
261 return 0;
262 }
David Benjamine14ff062016-08-09 16:21:24 -0400263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 ctx->extensions = CBS_data(&extensions);
265 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langleydc9b1412014-06-20 12:00:00 -0700269
David Benjamincec73442016-08-02 17:41:33 -0400270int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx,
271 CBS *out, uint16_t extension_type) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 CBS extensions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 while (CBS_len(&extensions) != 0) {
David Benjamincec73442016-08-02 17:41:33 -0400275 /* Decode the next extension. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint16_t type;
277 CBS extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 if (!CBS_get_u16(&extensions, &type) ||
279 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
280 return 0;
281 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (type == extension_type) {
David Benjamincec73442016-08-02 17:41:33 -0400284 *out = extension;
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 1;
286 }
287 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700288
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamincec73442016-08-02 17:41:33 -0400292int SSL_early_callback_ctx_extension_get(
293 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
294 const uint8_t **out_data, size_t *out_len) {
295 CBS cbs;
296 if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) {
297 return 0;
298 }
299
300 *out_data = CBS_data(&cbs);
301 *out_len = CBS_len(&cbs);
302 return 1;
303}
304
Steven Valdezce902a92016-05-17 11:47:53 -0400305static const uint16_t kDefaultGroups[] = {
David Benjamin9e68f192016-06-30 14:55:33 -0400306 SSL_CURVE_X25519,
307 SSL_CURVE_SECP256R1,
308 SSL_CURVE_SECP384R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700309#if defined(BORINGSSL_ANDROID_SYSTEM)
David Benjamin9e68f192016-06-30 14:55:33 -0400310 SSL_CURVE_SECP521R1,
Adam Langleyd98dc132015-09-23 16:41:33 -0700311#endif
Adam Langleyfcf25832014-12-18 17:42:32 -0800312};
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Steven Valdez5440fe02016-07-18 12:40:30 -0400314void tls1_get_grouplist(SSL *ssl, int get_peer_groups,
315 const uint16_t **out_group_ids,
316 size_t *out_group_ids_len) {
Steven Valdezce902a92016-05-17 11:47:53 -0400317 if (get_peer_groups) {
318 /* Only clients send a supported group list, so this function is only
319 * called on the server. */
David Benjamin0d56f882015-12-19 17:05:56 -0500320 assert(ssl->server);
Steven Valdezce902a92016-05-17 11:47:53 -0400321 *out_group_ids = ssl->s3->tmp.peer_supported_group_list;
322 *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 return;
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Steven Valdezce902a92016-05-17 11:47:53 -0400326 *out_group_ids = ssl->supported_group_list;
327 *out_group_ids_len = ssl->supported_group_list_len;
328 if (!*out_group_ids) {
329 *out_group_ids = kDefaultGroups;
330 *out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 }
332}
David Benjamined439582014-07-14 19:13:02 -0400333
Steven Valdezce902a92016-05-17 11:47:53 -0400334int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) {
335 const uint16_t *groups, *peer_groups, *pref, *supp;
336 size_t groups_len, peer_groups_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 /* Can't do anything on client side */
David Benjamin4298d772015-12-19 00:18:25 -0500339 if (ssl->server == 0) {
340 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 }
342
Steven Valdezce902a92016-05-17 11:47:53 -0400343 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
344 tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800345
Steven Valdezce902a92016-05-17 11:47:53 -0400346 if (peer_groups_len == 0) {
347 /* Clients are not required to send a supported_groups extension. In this
348 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin4298d772015-12-19 00:18:25 -0500349 * section 4, paragraph 3.
350 *
351 * However, in the interests of compatibility, we will skip ECDH if the
352 * client didn't send an extension because we can't be sure that they'll
Steven Valdezce902a92016-05-17 11:47:53 -0400353 * support our favoured group. */
David Benjamin4298d772015-12-19 00:18:25 -0500354 return 0;
David Benjamin55a43642015-04-20 14:45:55 -0400355 }
356
David Benjamin4298d772015-12-19 00:18:25 -0500357 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400358 pref = groups;
359 pref_len = groups_len;
360 supp = peer_groups;
361 supp_len = peer_groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400362 } else {
Steven Valdezce902a92016-05-17 11:47:53 -0400363 pref = peer_groups;
364 pref_len = peer_groups_len;
365 supp = groups;
366 supp_len = groups_len;
David Benjamin55a43642015-04-20 14:45:55 -0400367 }
368
369 for (i = 0; i < pref_len; i++) {
370 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 if (pref[i] == supp[j]) {
Steven Valdezce902a92016-05-17 11:47:53 -0400372 *out_group_id = pref[i];
David Benjamin4298d772015-12-19 00:18:25 -0500373 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 }
375 }
376 }
377
David Benjamin4298d772015-12-19 00:18:25 -0500378 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Steven Valdezce902a92016-05-17 11:47:53 -0400381int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 const int *curves, size_t ncurves) {
Steven Valdezce902a92016-05-17 11:47:53 -0400383 uint16_t *group_ids;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 size_t i;
385
Steven Valdezce902a92016-05-17 11:47:53 -0400386 group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t));
387 if (group_ids == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 0;
389 }
390
391 for (i = 0; i < ncurves; i++) {
Steven Valdezce902a92016-05-17 11:47:53 -0400392 if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
393 OPENSSL_free(group_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 0;
395 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 }
397
Steven Valdezce902a92016-05-17 11:47:53 -0400398 OPENSSL_free(*out_group_ids);
399 *out_group_ids = group_ids;
400 *out_group_ids_len = ncurves;
Adam Langleyfcf25832014-12-18 17:42:32 -0800401
402 return 1;
403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Steven Valdezce902a92016-05-17 11:47:53 -0400405/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the
406 * TLS group ID and point format, respectively, for |ec|. It returns one on
David Benjamin072334d2014-07-13 16:24:27 -0400407 * success and zero on failure. */
Steven Valdezce902a92016-05-17 11:47:53 -0400408static int tls1_curve_params_from_ec_key(uint16_t *out_group_id,
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 uint8_t *out_comp_id, EC_KEY *ec) {
410 int nid;
411 uint16_t id;
412 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 if (ec == NULL) {
415 return 0;
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 grp = EC_KEY_get0_group(ec);
419 if (grp == NULL) {
420 return 0;
421 }
David Benjamin072334d2014-07-13 16:24:27 -0400422
Steven Valdezce902a92016-05-17 11:47:53 -0400423 /* Determine group ID */
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 nid = EC_GROUP_get_curve_name(grp);
Steven Valdezce902a92016-05-17 11:47:53 -0400425 if (!ssl_nid_to_group_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 return 0;
427 }
David Benjamin072334d2014-07-13 16:24:27 -0400428
Steven Valdezce902a92016-05-17 11:47:53 -0400429 /* Set the named group ID. Arbitrary explicit groups are not supported. */
430 *out_group_id = id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431
432 if (out_comp_id) {
433 if (EC_KEY_get0_public_key(ec) == NULL) {
434 return 0;
435 }
436 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
437 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
438 } else {
439 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
440 }
441 }
442
443 return 1;
444}
David Benjamin072334d2014-07-13 16:24:27 -0400445
Steven Valdezce902a92016-05-17 11:47:53 -0400446/* tls1_check_group_id returns one if |group_id| is consistent with both our
447 * and the peer's group preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400448 * preferences are checked; the peer (the server) does not send preferences. */
Steven Valdezce902a92016-05-17 11:47:53 -0400449int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
450 const uint16_t *groups;
451 size_t groups_len, i, get_peer_groups;
David Benjamin42e9a772014-09-02 23:18:44 -0400452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 /* Check against our list, then the peer's list. */
Steven Valdezce902a92016-05-17 11:47:53 -0400454 for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) {
455 if (get_peer_groups && !ssl->server) {
David Benjamin55a43642015-04-20 14:45:55 -0400456 /* Servers do not present a preference list so, if we are a client, only
457 * check our list. */
458 continue;
459 }
460
Steven Valdezce902a92016-05-17 11:47:53 -0400461 tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len);
462 if (get_peer_groups && groups_len == 0) {
463 /* Clients are not required to send a supported_groups extension. In this
464 * case, the server is free to pick any group it likes. See RFC 4492,
David Benjamin55a43642015-04-20 14:45:55 -0400465 * section 4, paragraph 3. */
466 continue;
467 }
Steven Valdezce902a92016-05-17 11:47:53 -0400468 for (i = 0; i < groups_len; i++) {
469 if (groups[i] == group_id) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 break;
471 }
472 }
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Steven Valdezce902a92016-05-17 11:47:53 -0400474 if (i == groups_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 return 0;
476 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 }
David Benjamin033e5f42014-11-13 18:47:41 -0500478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 return 1;
480}
David Benjamin033e5f42014-11-13 18:47:41 -0500481
David Benjamin0d56f882015-12-19 17:05:56 -0500482int tls1_check_ec_cert(SSL *ssl, X509 *x) {
David Benjamin75ea5bb2016-07-08 17:43:29 -0700483 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
484 /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */
485 return 1;
486 }
487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 EVP_PKEY *pkey = X509_get_pubkey(x);
David Benjamin75ea5bb2016-07-08 17:43:29 -0700489 if (pkey == NULL) {
490 return 0;
491 }
492
493 int ret = 0;
Steven Valdezce902a92016-05-17 11:47:53 -0400494 uint16_t group_id;
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 uint8_t comp_id;
David Benjamin758d1272015-11-20 17:47:25 -0500496 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
497 if (ec_key == NULL ||
Steven Valdezce902a92016-05-17 11:47:53 -0400498 !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) ||
499 !tls1_check_group_id(ssl, group_id) ||
David Benjaminfc059942015-07-30 23:01:59 -0400500 comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 goto done;
502 }
503
504 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500505
506done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400507 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 return ret;
509}
David Benjamin42e9a772014-09-02 23:18:44 -0400510
Adam Langley95c29f32014-06-20 12:00:00 -0700511/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Steven Valdez02563852016-06-23 13:33:05 -0400514static const uint16_t kDefaultSignatureAlgorithms[] = {
515 SSL_SIGN_RSA_PKCS1_SHA512,
516 SSL_SIGN_ECDSA_SECP521R1_SHA512,
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Steven Valdez02563852016-06-23 13:33:05 -0400518 SSL_SIGN_RSA_PKCS1_SHA384,
519 SSL_SIGN_ECDSA_SECP384R1_SHA384,
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Steven Valdez02563852016-06-23 13:33:05 -0400521 SSL_SIGN_RSA_PKCS1_SHA256,
522 SSL_SIGN_ECDSA_SECP256R1_SHA256,
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Steven Valdez02563852016-06-23 13:33:05 -0400524 SSL_SIGN_RSA_PKCS1_SHA1,
525 SSL_SIGN_ECDSA_SHA1,
Adam Langley95c29f32014-06-20 12:00:00 -0700526};
David Benjamin05da6e12014-07-12 20:42:55 -0400527
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400528static const uint16_t kDefaultTLS13SignatureAlgorithms[] = {
529 SSL_SIGN_RSA_PSS_SHA512,
530 SSL_SIGN_RSA_PKCS1_SHA512,
531 SSL_SIGN_ECDSA_SECP521R1_SHA512,
532
533 SSL_SIGN_RSA_PSS_SHA384,
534 SSL_SIGN_RSA_PKCS1_SHA384,
535 SSL_SIGN_ECDSA_SECP384R1_SHA384,
536
537 SSL_SIGN_RSA_PSS_SHA256,
538 SSL_SIGN_RSA_PKCS1_SHA256,
539 SSL_SIGN_ECDSA_SECP256R1_SHA256,
540
541 SSL_SIGN_RSA_PKCS1_SHA1,
542 SSL_SIGN_ECDSA_SHA1,
543};
544
Steven Valdez02563852016-06-23 13:33:05 -0400545size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400546 uint16_t version;
547 if (ssl->s3->have_version) {
548 version = ssl3_protocol_version(ssl);
549 } else {
550 version = ssl->method->version_from_wire(ssl->client_version);
551 }
552
553 if (version >= TLS1_3_VERSION) {
554 *psigs = kDefaultTLS13SignatureAlgorithms;
555 return sizeof(kDefaultTLS13SignatureAlgorithms) /
556 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
557 }
558
Steven Valdez02563852016-06-23 13:33:05 -0400559 *psigs = kDefaultSignatureAlgorithms;
560 return sizeof(kDefaultSignatureAlgorithms) /
561 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800562}
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamin887c3002016-07-08 16:15:32 -0700564int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400565 const uint16_t *sent_sigs;
566 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800567
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500569 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400570 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400571 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800572 break;
573 }
574 }
575
David Benjamin788be4a2015-10-30 17:50:57 -0400576 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400577 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
579 return 0;
580 }
581
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 return 1;
583}
584
585/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
586 * supported or doesn't appear in supported signature algorithms. Unlike
587 * ssl_cipher_get_disabled this applies to a specific session and not global
588 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500589void ssl_set_client_disabled(SSL *ssl) {
590 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400591 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 size_t i, sigalgslen;
593 int have_rsa = 0, have_ecdsa = 0;
594 c->mask_a = 0;
595 c->mask_k = 0;
596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 /* Now go through all signature algorithms seeing if we support any for RSA,
598 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500599 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400600 for (i = 0; i < sigalgslen; i++) {
601 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400602 case SSL_SIGN_RSA_PSS_SHA512:
603 case SSL_SIGN_RSA_PSS_SHA384:
604 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400605 case SSL_SIGN_RSA_PKCS1_SHA512:
606 case SSL_SIGN_RSA_PKCS1_SHA384:
607 case SSL_SIGN_RSA_PKCS1_SHA256:
608 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 have_rsa = 1;
610 break;
611
Steven Valdez02563852016-06-23 13:33:05 -0400612 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
613 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
614 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
615 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 have_ecdsa = 1;
617 break;
618 }
619 }
620
621 /* Disable auth if we don't include any appropriate signature algorithms. */
622 if (!have_rsa) {
623 c->mask_a |= SSL_aRSA;
624 }
625 if (!have_ecdsa) {
626 c->mask_a |= SSL_aECDSA;
627 }
628
629 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500630 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 c->mask_a |= SSL_aPSK;
632 c->mask_k |= SSL_kPSK;
633 }
634}
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley614c66a2015-06-12 15:26:58 -0700636/* tls_extension represents a TLS extension that is handled internally. The
637 * |init| function is called for each handshake, before any other functions of
638 * the extension. Then the add and parse callbacks are called as needed.
639 *
640 * The parse callbacks receive a |CBS| that contains the contents of the
641 * extension (i.e. not including the type and length bytes). If an extension is
642 * not received then the parse callbacks will be called with a NULL CBS so that
643 * they can do any processing needed to handle the absence of an extension.
644 *
645 * The add callbacks receive a |CBB| to which the extension can be appended but
646 * the function is responsible for appending the type and length bytes too.
647 *
648 * All callbacks return one for success and zero for error. If a parse function
649 * returns zero then a fatal alert with value |*out_alert| will be sent. If
650 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
651struct tls_extension {
652 uint16_t value;
653 void (*init)(SSL *ssl);
654
655 int (*add_clienthello)(SSL *ssl, CBB *out);
656 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
657
658 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
659 int (*add_serverhello)(SSL *ssl, CBB *out);
660};
661
Steven Valdez6b8509a2016-07-12 13:38:32 -0400662static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
663 if (contents != NULL) {
664 /* Servers MUST NOT send this extension. */
665 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
666 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
667 return 0;
668 }
669
670 return 1;
671}
672
673static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
674 /* This extension from the client is handled elsewhere. */
675 return 1;
676}
677
678static int dont_add_serverhello(SSL *ssl, CBB *out) {
679 return 1;
680}
Adam Langley614c66a2015-06-12 15:26:58 -0700681
682/* Server name indication (SNI).
683 *
684 * https://tools.ietf.org/html/rfc6066#section-3. */
685
686static void ext_sni_init(SSL *ssl) {
687 ssl->s3->tmp.should_ack_sni = 0;
688}
689
690static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
691 if (ssl->tlsext_hostname == NULL) {
692 return 1;
693 }
694
695 CBB contents, server_name_list, name;
696 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
697 !CBB_add_u16_length_prefixed(out, &contents) ||
698 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
699 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
700 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
701 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
702 strlen(ssl->tlsext_hostname)) ||
703 !CBB_flush(out)) {
704 return 0;
705 }
706
707 return 1;
708}
709
David Benjamin0d56f882015-12-19 17:05:56 -0500710static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
711 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700712 if (contents == NULL) {
713 return 1;
714 }
715
716 if (CBS_len(contents) != 0) {
717 return 0;
718 }
719
720 assert(ssl->tlsext_hostname != NULL);
721
Steven Valdez87eab492016-06-27 16:34:59 -0400722 if (ssl->session == NULL) {
723 assert(ssl->s3->new_session->tlsext_hostname == NULL);
724 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
725 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700726 *out_alert = SSL_AD_INTERNAL_ERROR;
727 return 0;
728 }
729 }
730
731 return 1;
732}
733
David Benjamin0d56f882015-12-19 17:05:56 -0500734static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
735 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700736 if (contents == NULL) {
737 return 1;
738 }
739
David Benjamin9b611e22016-03-03 08:48:30 -0500740 CBS server_name_list, host_name;
741 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700742 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500743 !CBS_get_u8(&server_name_list, &name_type) ||
744 /* Although the server_name extension was intended to be extensible to
745 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
746 * different name types will cause an error. Further, RFC 4366 originally
747 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
748 * adding new name types is no longer feasible.
749 *
750 * Act as if the extensibility does not exist to simplify parsing. */
751 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
752 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700753 CBS_len(contents) != 0) {
754 return 0;
755 }
756
David Benjamin9b611e22016-03-03 08:48:30 -0500757 if (name_type != TLSEXT_NAMETYPE_host_name ||
758 CBS_len(&host_name) == 0 ||
759 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
760 CBS_contains_zero_byte(&host_name)) {
761 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
762 return 0;
763 }
Adam Langley614c66a2015-06-12 15:26:58 -0700764
David Benjamin9b611e22016-03-03 08:48:30 -0500765 /* TODO(davidben): SNI should be resolved before resumption. We have the
766 * early callback as a replacement, but we should fix the current callback
767 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400768 if (ssl->session == NULL) {
769 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500770
771 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400772 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500773 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700774 return 0;
775 }
776
David Benjamin9b611e22016-03-03 08:48:30 -0500777 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700778 }
779
780 return 1;
781}
782
783static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400784 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700785 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400786 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700787 return 1;
788 }
789
790 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
791 !CBB_add_u16(out, 0 /* length */)) {
792 return 0;
793 }
794
795 return 1;
796}
797
798
Adam Langley5021b222015-06-12 18:27:58 -0700799/* Renegotiation indication.
800 *
801 * https://tools.ietf.org/html/rfc5746 */
802
803static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
804 CBB contents, prev_finished;
805 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
806 !CBB_add_u16_length_prefixed(out, &contents) ||
807 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
808 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
809 ssl->s3->previous_client_finished_len) ||
810 !CBB_flush(out)) {
811 return 0;
812 }
813
814 return 1;
815}
816
817static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
818 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400819 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
820 return 0;
821 }
822
David Benjamin3e052de2015-11-25 20:10:31 -0500823 /* Servers may not switch between omitting the extension and supporting it.
824 * See RFC 5746, sections 3.5 and 4.2. */
825 if (ssl->s3->initial_handshake_complete &&
826 (contents != NULL) != ssl->s3->send_connection_binding) {
827 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
828 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
829 return 0;
830 }
831
Adam Langley5021b222015-06-12 18:27:58 -0700832 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500833 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700834 * RI even on initial ServerHello because the client doesn't see any
835 * renegotiation during an attack. However this would mean we could not
836 * connect to any server which doesn't support RI.
837 *
David Benjamine9cddb82015-11-23 14:36:40 -0500838 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
839 * practical terms every client sets it so it's just assumed here. */
840 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700841 }
842
843 const size_t expected_len = ssl->s3->previous_client_finished_len +
844 ssl->s3->previous_server_finished_len;
845
846 /* Check for logic errors */
847 assert(!expected_len || ssl->s3->previous_client_finished_len);
848 assert(!expected_len || ssl->s3->previous_server_finished_len);
849
850 /* Parse out the extension contents. */
851 CBS renegotiated_connection;
852 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
853 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400854 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700855 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
856 return 0;
857 }
858
859 /* Check that the extension matches. */
860 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700862 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
863 return 0;
864 }
865
866 const uint8_t *d = CBS_data(&renegotiated_connection);
867 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
868 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700870 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
871 return 0;
872 }
873 d += ssl->s3->previous_client_finished_len;
874
875 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
876 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400877 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700878 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
879 return 0;
880 }
881 ssl->s3->send_connection_binding = 1;
882
883 return 1;
884}
885
886static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
887 CBS *contents) {
888 /* Renegotiation isn't supported as a server so this function should never be
889 * called after the initial handshake. */
890 assert(!ssl->s3->initial_handshake_complete);
891
Steven Valdez143e8b32016-07-11 13:19:03 -0400892 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
893 return 1;
894 }
895
Adam Langley5021b222015-06-12 18:27:58 -0700896 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400897 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700898 }
899
900 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700901 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
902 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400903 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700904 return 0;
905 }
906
907 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500908 if (!CBS_mem_equal(&renegotiated_connection,
909 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700910 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400911 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700912 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
913 return 0;
914 }
915
916 ssl->s3->send_connection_binding = 1;
917
918 return 1;
919}
920
921static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400922 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
923 return 1;
924 }
925
Adam Langley5021b222015-06-12 18:27:58 -0700926 CBB contents, prev_finished;
927 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
928 !CBB_add_u16_length_prefixed(out, &contents) ||
929 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
930 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
931 ssl->s3->previous_client_finished_len) ||
932 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
933 ssl->s3->previous_server_finished_len) ||
934 !CBB_flush(out)) {
935 return 0;
936 }
937
938 return 1;
939}
940
Adam Langley0a056712015-07-01 15:03:33 -0700941
942/* Extended Master Secret.
943 *
David Benjamin43946d42016-02-01 08:42:19 -0500944 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700945
946static void ext_ems_init(SSL *ssl) {
947 ssl->s3->tmp.extended_master_secret = 0;
948}
949
950static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
951 if (ssl->version == SSL3_VERSION) {
952 return 1;
953 }
954
955 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
956 !CBB_add_u16(out, 0 /* length */)) {
957 return 0;
958 }
959
960 return 1;
961}
962
963static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
964 CBS *contents) {
965 if (contents == NULL) {
966 return 1;
967 }
968
Steven Valdez143e8b32016-07-11 13:19:03 -0400969 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
970 ssl->version == SSL3_VERSION) {
971 return 0;
972 }
973
974 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700975 return 0;
976 }
977
978 ssl->s3->tmp.extended_master_secret = 1;
979 return 1;
980}
981
David Benjamin0d56f882015-12-19 17:05:56 -0500982static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
983 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400984 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
985 ssl->version == SSL3_VERSION) {
986 return 1;
987 }
988
989 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700990 return 1;
991 }
992
993 if (CBS_len(contents) != 0) {
994 return 0;
995 }
996
997 ssl->s3->tmp.extended_master_secret = 1;
998 return 1;
999}
1000
1001static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1002 if (!ssl->s3->tmp.extended_master_secret) {
1003 return 1;
1004 }
1005
1006 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1007 !CBB_add_u16(out, 0 /* length */)) {
1008 return 0;
1009 }
1010
1011 return 1;
1012}
1013
Adam Langley9b05bc52015-07-01 15:25:33 -07001014
1015/* Session tickets.
1016 *
1017 * https://tools.ietf.org/html/rfc5077 */
1018
1019static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1020 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1021 return 1;
1022 }
1023
1024 const uint8_t *ticket_data = NULL;
1025 int ticket_len = 0;
1026
1027 /* Renegotiation does not participate in session resumption. However, still
1028 * advertise the extension to avoid potentially breaking servers which carry
1029 * over the state from the previous handshake, such as OpenSSL servers
1030 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1031 if (!ssl->s3->initial_handshake_complete &&
1032 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001033 ssl->session->tlsext_tick != NULL &&
1034 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1035 ssl->method->version_from_wire(ssl->session->ssl_version) <
1036 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001037 ticket_data = ssl->session->tlsext_tick;
1038 ticket_len = ssl->session->tlsext_ticklen;
1039 }
1040
1041 CBB ticket;
1042 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1043 !CBB_add_u16_length_prefixed(out, &ticket) ||
1044 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1045 !CBB_flush(out)) {
1046 return 0;
1047 }
1048
1049 return 1;
1050}
1051
1052static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1053 CBS *contents) {
1054 ssl->tlsext_ticket_expected = 0;
1055
1056 if (contents == NULL) {
1057 return 1;
1058 }
1059
Steven Valdez143e8b32016-07-11 13:19:03 -04001060 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1061 return 0;
1062 }
1063
Adam Langley9b05bc52015-07-01 15:25:33 -07001064 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1065 * this function should never be called, even if the server tries to send the
1066 * extension. */
1067 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1068
1069 if (CBS_len(contents) != 0) {
1070 return 0;
1071 }
1072
1073 ssl->tlsext_ticket_expected = 1;
1074 return 1;
1075}
1076
Adam Langley9b05bc52015-07-01 15:25:33 -07001077static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1078 if (!ssl->tlsext_ticket_expected) {
1079 return 1;
1080 }
1081
1082 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1083 * true. */
1084 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1085
1086 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1087 !CBB_add_u16(out, 0 /* length */)) {
1088 return 0;
1089 }
1090
1091 return 1;
1092}
1093
1094
Adam Langley2e857bd2015-07-01 16:09:19 -07001095/* Signature Algorithms.
1096 *
1097 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1098
1099static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001100 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001101 return 1;
1102 }
1103
Steven Valdez02563852016-06-23 13:33:05 -04001104 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001105 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1106
1107 CBB contents, sigalgs;
1108 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1109 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001110 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1111 return 0;
1112 }
1113
1114 size_t i;
1115 for (i = 0; i < sigalgs_len; i++) {
1116 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1117 return 0;
1118 }
1119 }
1120
1121 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001122 return 0;
1123 }
1124
1125 return 1;
1126}
1127
Adam Langley2e857bd2015-07-01 16:09:19 -07001128static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1129 CBS *contents) {
1130 OPENSSL_free(ssl->cert->peer_sigalgs);
1131 ssl->cert->peer_sigalgs = NULL;
1132 ssl->cert->peer_sigalgslen = 0;
1133
Adam Langley2e857bd2015-07-01 16:09:19 -07001134 if (contents == NULL) {
1135 return 1;
1136 }
1137
1138 CBS supported_signature_algorithms;
1139 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001140 CBS_len(contents) != 0 ||
1141 CBS_len(&supported_signature_algorithms) == 0 ||
1142 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001143 return 0;
1144 }
1145
1146 return 1;
1147}
1148
Adam Langley2e857bd2015-07-01 16:09:19 -07001149
Adam Langleybb0bd042015-07-01 16:21:03 -07001150/* OCSP Stapling.
1151 *
1152 * https://tools.ietf.org/html/rfc6066#section-8 */
1153
1154static void ext_ocsp_init(SSL *ssl) {
1155 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001156 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001157}
1158
1159static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1160 if (!ssl->ocsp_stapling_enabled) {
1161 return 1;
1162 }
1163
1164 CBB contents;
1165 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1166 !CBB_add_u16_length_prefixed(out, &contents) ||
1167 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1168 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1169 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1170 !CBB_flush(out)) {
1171 return 0;
1172 }
1173
Adam Langleyce9d85e2016-01-24 15:58:39 -08001174 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001175 return 1;
1176}
1177
1178static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001179 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001180 if (contents == NULL) {
1181 return 1;
1182 }
1183
David Benjamin942f4ed2016-07-16 19:03:49 +03001184 /* OCSP stapling is forbidden on a non-certificate cipher. */
1185 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001186 return 0;
1187 }
1188
David Benjamin942f4ed2016-07-16 19:03:49 +03001189 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1190 if (CBS_len(contents) != 0) {
1191 return 0;
1192 }
1193
1194 ssl->s3->tmp.certificate_status_expected = 1;
1195 return 1;
1196 }
1197
1198 uint8_t status_type;
1199 CBS ocsp_response;
1200 if (!CBS_get_u8(contents, &status_type) ||
1201 status_type != TLSEXT_STATUSTYPE_ocsp ||
1202 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1203 CBS_len(&ocsp_response) == 0 ||
1204 CBS_len(contents) != 0) {
1205 return 0;
1206 }
1207
Steven Valdez87eab492016-06-27 16:34:59 -04001208 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1209 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001210 *out_alert = SSL_AD_INTERNAL_ERROR;
1211 return 0;
1212 }
1213
Adam Langleybb0bd042015-07-01 16:21:03 -07001214 return 1;
1215}
1216
1217static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1218 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001219 if (contents == NULL) {
1220 return 1;
1221 }
1222
1223 uint8_t status_type;
1224 if (!CBS_get_u8(contents, &status_type)) {
1225 return 0;
1226 }
1227
1228 /* We cannot decide whether OCSP stapling will occur yet because the correct
1229 * SSL_CTX might not have been selected. */
1230 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1231
Adam Langleybb0bd042015-07-01 16:21:03 -07001232 return 1;
1233}
1234
1235static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001236 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1237 ssl->ctx->ocsp_response_length == 0 ||
1238 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001239 return 1;
1240 }
1241
David Benjamin942f4ed2016-07-16 19:03:49 +03001242 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1243 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001244 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001245 return 1;
1246 }
1247
1248 ssl->s3->tmp.certificate_status_expected = 1;
1249
1250 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1251 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001252 }
1253
David Benjamin942f4ed2016-07-16 19:03:49 +03001254 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001255 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001256 CBB_add_u16_length_prefixed(out, &body) &&
1257 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1258 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1259 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1260 ssl->ctx->ocsp_response_length) &&
1261 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001262}
1263
1264
Adam Langley97dfcbf2015-07-01 18:35:20 -07001265/* Next protocol negotiation.
1266 *
1267 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1268
1269static void ext_npn_init(SSL *ssl) {
1270 ssl->s3->next_proto_neg_seen = 0;
1271}
1272
1273static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1274 if (ssl->s3->initial_handshake_complete ||
1275 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001276 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001277 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001278 return 1;
1279 }
1280
1281 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1282 !CBB_add_u16(out, 0 /* length */)) {
1283 return 0;
1284 }
1285
1286 return 1;
1287}
1288
1289static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1290 CBS *contents) {
1291 if (contents == NULL) {
1292 return 1;
1293 }
1294
Steven Valdez143e8b32016-07-11 13:19:03 -04001295 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1296 return 0;
1297 }
1298
Adam Langley97dfcbf2015-07-01 18:35:20 -07001299 /* If any of these are false then we should never have sent the NPN
1300 * extension in the ClientHello and thus this function should never have been
1301 * called. */
1302 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001303 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001304 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001305 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001306
David Benjamin76c2efc2015-08-31 14:24:29 -04001307 if (ssl->s3->alpn_selected != NULL) {
1308 /* NPN and ALPN may not be negotiated in the same connection. */
1309 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1310 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1311 return 0;
1312 }
1313
Adam Langley97dfcbf2015-07-01 18:35:20 -07001314 const uint8_t *const orig_contents = CBS_data(contents);
1315 const size_t orig_len = CBS_len(contents);
1316
1317 while (CBS_len(contents) != 0) {
1318 CBS proto;
1319 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1320 CBS_len(&proto) == 0) {
1321 return 0;
1322 }
1323 }
1324
1325 uint8_t *selected;
1326 uint8_t selected_len;
1327 if (ssl->ctx->next_proto_select_cb(
1328 ssl, &selected, &selected_len, orig_contents, orig_len,
1329 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1330 *out_alert = SSL_AD_INTERNAL_ERROR;
1331 return 0;
1332 }
1333
David Benjamin79978df2015-12-25 15:56:49 -05001334 OPENSSL_free(ssl->s3->next_proto_negotiated);
1335 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1336 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001337 *out_alert = SSL_AD_INTERNAL_ERROR;
1338 return 0;
1339 }
1340
David Benjamin79978df2015-12-25 15:56:49 -05001341 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001342 ssl->s3->next_proto_neg_seen = 1;
1343
1344 return 1;
1345}
1346
1347static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1348 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001349 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1350 return 1;
1351 }
1352
Adam Langley97dfcbf2015-07-01 18:35:20 -07001353 if (contents != NULL && CBS_len(contents) != 0) {
1354 return 0;
1355 }
1356
1357 if (contents == NULL ||
1358 ssl->s3->initial_handshake_complete ||
1359 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1360 * afterwards, parsing the ALPN extension will clear
1361 * |next_proto_neg_seen|. */
1362 ssl->s3->alpn_selected != NULL ||
1363 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001364 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001365 return 1;
1366 }
1367
1368 ssl->s3->next_proto_neg_seen = 1;
1369 return 1;
1370}
1371
1372static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1373 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1374 * parsed. */
1375 if (!ssl->s3->next_proto_neg_seen) {
1376 return 1;
1377 }
1378
1379 const uint8_t *npa;
1380 unsigned npa_len;
1381
1382 if (ssl->ctx->next_protos_advertised_cb(
1383 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1384 SSL_TLSEXT_ERR_OK) {
1385 ssl->s3->next_proto_neg_seen = 0;
1386 return 1;
1387 }
1388
1389 CBB contents;
1390 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1391 !CBB_add_u16_length_prefixed(out, &contents) ||
1392 !CBB_add_bytes(&contents, npa, npa_len) ||
1393 !CBB_flush(out)) {
1394 return 0;
1395 }
1396
1397 return 1;
1398}
1399
1400
Adam Langleyab8d87d2015-07-10 12:21:39 -07001401/* Signed certificate timestamps.
1402 *
1403 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1404
1405static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1406 if (!ssl->signed_cert_timestamps_enabled) {
1407 return 1;
1408 }
1409
1410 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1411 !CBB_add_u16(out, 0 /* length */)) {
1412 return 0;
1413 }
1414
1415 return 1;
1416}
1417
1418static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1419 CBS *contents) {
1420 if (contents == NULL) {
1421 return 1;
1422 }
1423
1424 /* If this is false then we should never have sent the SCT extension in the
1425 * ClientHello and thus this function should never have been called. */
1426 assert(ssl->signed_cert_timestamps_enabled);
1427
1428 if (CBS_len(contents) == 0) {
1429 *out_alert = SSL_AD_DECODE_ERROR;
1430 return 0;
1431 }
1432
1433 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001434 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001435 !CBS_stow(
1436 contents,
1437 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1438 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001439 *out_alert = SSL_AD_INTERNAL_ERROR;
1440 return 0;
1441 }
1442
1443 return 1;
1444}
1445
1446static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1447 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001448 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001449}
1450
1451static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001452 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001453 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001454 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001455 return 1;
1456 }
1457
1458 CBB contents;
1459 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1460 CBB_add_u16_length_prefixed(out, &contents) &&
1461 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1462 ssl->ctx->signed_cert_timestamp_list_length) &&
1463 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001464}
1465
1466
Adam Langleyf18e4532015-07-10 13:39:53 -07001467/* Application-level Protocol Negotiation.
1468 *
1469 * https://tools.ietf.org/html/rfc7301 */
1470
1471static void ext_alpn_init(SSL *ssl) {
1472 OPENSSL_free(ssl->s3->alpn_selected);
1473 ssl->s3->alpn_selected = NULL;
1474}
1475
1476static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1477 if (ssl->alpn_client_proto_list == NULL ||
1478 ssl->s3->initial_handshake_complete) {
1479 return 1;
1480 }
1481
1482 CBB contents, proto_list;
1483 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1484 !CBB_add_u16_length_prefixed(out, &contents) ||
1485 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1486 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1487 ssl->alpn_client_proto_list_len) ||
1488 !CBB_flush(out)) {
1489 return 0;
1490 }
1491
1492 return 1;
1493}
1494
1495static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1496 CBS *contents) {
1497 if (contents == NULL) {
1498 return 1;
1499 }
1500
1501 assert(!ssl->s3->initial_handshake_complete);
1502 assert(ssl->alpn_client_proto_list != NULL);
1503
David Benjamin76c2efc2015-08-31 14:24:29 -04001504 if (ssl->s3->next_proto_neg_seen) {
1505 /* NPN and ALPN may not be negotiated in the same connection. */
1506 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1507 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1508 return 0;
1509 }
1510
Adam Langleyf18e4532015-07-10 13:39:53 -07001511 /* The extension data consists of a ProtocolNameList which must have
1512 * exactly one ProtocolName. Each of these is length-prefixed. */
1513 CBS protocol_name_list, protocol_name;
1514 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1515 CBS_len(contents) != 0 ||
1516 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1517 /* Empty protocol names are forbidden. */
1518 CBS_len(&protocol_name) == 0 ||
1519 CBS_len(&protocol_name_list) != 0) {
1520 return 0;
1521 }
1522
David Benjamin3e517572016-08-11 11:52:23 -04001523 /* Check that the protcol name is one of the ones we advertised. */
1524 int protocol_ok = 0;
1525 CBS client_protocol_name_list, client_protocol_name;
1526 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1527 ssl->alpn_client_proto_list_len);
1528 while (CBS_len(&client_protocol_name_list) > 0) {
1529 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1530 &client_protocol_name)) {
1531 *out_alert = SSL_AD_INTERNAL_ERROR;
1532 return 0;
1533 }
1534
1535 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1536 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1537 CBS_len(&protocol_name)) == 0) {
1538 protocol_ok = 1;
1539 break;
1540 }
1541 }
1542
1543 if (!protocol_ok) {
1544 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1545 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1546 return 0;
1547 }
1548
Adam Langleyf18e4532015-07-10 13:39:53 -07001549 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1550 &ssl->s3->alpn_selected_len)) {
1551 *out_alert = SSL_AD_INTERNAL_ERROR;
1552 return 0;
1553 }
1554
1555 return 1;
1556}
1557
1558static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1559 CBS *contents) {
1560 if (contents == NULL) {
1561 return 1;
1562 }
1563
1564 if (ssl->ctx->alpn_select_cb == NULL ||
1565 ssl->s3->initial_handshake_complete) {
1566 return 1;
1567 }
1568
1569 /* ALPN takes precedence over NPN. */
1570 ssl->s3->next_proto_neg_seen = 0;
1571
1572 CBS protocol_name_list;
1573 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1574 CBS_len(contents) != 0 ||
1575 CBS_len(&protocol_name_list) < 2) {
1576 return 0;
1577 }
1578
1579 /* Validate the protocol list. */
1580 CBS protocol_name_list_copy = protocol_name_list;
1581 while (CBS_len(&protocol_name_list_copy) > 0) {
1582 CBS protocol_name;
1583
1584 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1585 /* Empty protocol names are forbidden. */
1586 CBS_len(&protocol_name) == 0) {
1587 return 0;
1588 }
1589 }
1590
1591 const uint8_t *selected;
1592 uint8_t selected_len;
1593 if (ssl->ctx->alpn_select_cb(
1594 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1595 CBS_len(&protocol_name_list),
1596 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1597 OPENSSL_free(ssl->s3->alpn_selected);
1598 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1599 if (ssl->s3->alpn_selected == NULL) {
1600 *out_alert = SSL_AD_INTERNAL_ERROR;
1601 return 0;
1602 }
1603 ssl->s3->alpn_selected_len = selected_len;
1604 }
1605
1606 return 1;
1607}
1608
1609static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1610 if (ssl->s3->alpn_selected == NULL) {
1611 return 1;
1612 }
1613
1614 CBB contents, proto_list, proto;
1615 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1616 !CBB_add_u16_length_prefixed(out, &contents) ||
1617 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1618 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001619 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1620 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001621 !CBB_flush(out)) {
1622 return 0;
1623 }
1624
1625 return 1;
1626}
1627
1628
Adam Langley49c7af12015-07-10 14:33:46 -07001629/* Channel ID.
1630 *
1631 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1632
1633static void ext_channel_id_init(SSL *ssl) {
1634 ssl->s3->tlsext_channel_id_valid = 0;
1635}
1636
1637static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1638 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001639 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001640 return 1;
1641 }
1642
1643 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1644 !CBB_add_u16(out, 0 /* length */)) {
1645 return 0;
1646 }
1647
1648 return 1;
1649}
1650
1651static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1652 CBS *contents) {
1653 if (contents == NULL) {
1654 return 1;
1655 }
1656
Steven Valdez143e8b32016-07-11 13:19:03 -04001657 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1658 return 0;
1659 }
1660
David Benjamince079fd2016-08-02 16:22:34 -04001661 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001662 assert(ssl->tlsext_channel_id_enabled);
1663
1664 if (CBS_len(contents) != 0) {
1665 return 0;
1666 }
1667
1668 ssl->s3->tlsext_channel_id_valid = 1;
1669 return 1;
1670}
1671
1672static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1673 CBS *contents) {
1674 if (contents == NULL ||
1675 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001676 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001677 return 1;
1678 }
1679
1680 if (CBS_len(contents) != 0) {
1681 return 0;
1682 }
1683
1684 ssl->s3->tlsext_channel_id_valid = 1;
1685 return 1;
1686}
1687
1688static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001689 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1690 return 1;
1691 }
1692
Adam Langley49c7af12015-07-10 14:33:46 -07001693 if (!ssl->s3->tlsext_channel_id_valid) {
1694 return 1;
1695 }
1696
1697 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1698 !CBB_add_u16(out, 0 /* length */)) {
1699 return 0;
1700 }
1701
1702 return 1;
1703}
1704
Adam Langley391250d2015-07-15 19:06:07 -07001705
1706/* Secure Real-time Transport Protocol (SRTP) extension.
1707 *
1708 * https://tools.ietf.org/html/rfc5764 */
1709
Adam Langley391250d2015-07-15 19:06:07 -07001710
1711static void ext_srtp_init(SSL *ssl) {
1712 ssl->srtp_profile = NULL;
1713}
1714
1715static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1716 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1717 if (profiles == NULL) {
1718 return 1;
1719 }
1720 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1721 if (num_profiles == 0) {
1722 return 1;
1723 }
1724
1725 CBB contents, profile_ids;
1726 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1727 !CBB_add_u16_length_prefixed(out, &contents) ||
1728 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1729 return 0;
1730 }
1731
1732 size_t i;
1733 for (i = 0; i < num_profiles; i++) {
1734 if (!CBB_add_u16(&profile_ids,
1735 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1736 return 0;
1737 }
1738 }
1739
1740 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1741 !CBB_flush(out)) {
1742 return 0;
1743 }
1744
1745 return 1;
1746}
1747
1748static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1749 CBS *contents) {
1750 if (contents == NULL) {
1751 return 1;
1752 }
1753
1754 /* The extension consists of a u16-prefixed profile ID list containing a
1755 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1756 *
1757 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1758 CBS profile_ids, srtp_mki;
1759 uint16_t profile_id;
1760 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1761 !CBS_get_u16(&profile_ids, &profile_id) ||
1762 CBS_len(&profile_ids) != 0 ||
1763 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1764 CBS_len(contents) != 0) {
1765 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1766 return 0;
1767 }
1768
1769 if (CBS_len(&srtp_mki) != 0) {
1770 /* Must be no MKI, since we never offer one. */
1771 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1772 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1773 return 0;
1774 }
1775
1776 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1777
1778 /* Check to see if the server gave us something we support (and presumably
1779 * offered). */
1780 size_t i;
1781 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1782 const SRTP_PROTECTION_PROFILE *profile =
1783 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1784
1785 if (profile->id == profile_id) {
1786 ssl->srtp_profile = profile;
1787 return 1;
1788 }
1789 }
1790
1791 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1792 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1793 return 0;
1794}
1795
1796static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1797 CBS *contents) {
1798 if (contents == NULL) {
1799 return 1;
1800 }
1801
1802 CBS profile_ids, srtp_mki;
1803 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1804 CBS_len(&profile_ids) < 2 ||
1805 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1806 CBS_len(contents) != 0) {
1807 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1808 return 0;
1809 }
1810 /* Discard the MKI value for now. */
1811
1812 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1813 SSL_get_srtp_profiles(ssl);
1814
1815 /* Pick the server's most preferred profile. */
1816 size_t i;
1817 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1818 const SRTP_PROTECTION_PROFILE *server_profile =
1819 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1820
1821 CBS profile_ids_tmp;
1822 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1823
1824 while (CBS_len(&profile_ids_tmp) > 0) {
1825 uint16_t profile_id;
1826 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1827 return 0;
1828 }
1829
1830 if (server_profile->id == profile_id) {
1831 ssl->srtp_profile = server_profile;
1832 return 1;
1833 }
1834 }
1835 }
1836
1837 return 1;
1838}
1839
1840static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1841 if (ssl->srtp_profile == NULL) {
1842 return 1;
1843 }
1844
1845 CBB contents, profile_ids;
1846 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1847 !CBB_add_u16_length_prefixed(out, &contents) ||
1848 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1849 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1850 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1851 !CBB_flush(out)) {
1852 return 0;
1853 }
1854
1855 return 1;
1856}
1857
Adam Langleybdd5d662015-07-20 16:19:08 -07001858
1859/* EC point formats.
1860 *
1861 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1862
1863static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001864 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001865 return 0;
1866 }
1867
1868 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1869
1870 size_t i;
1871 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1872 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1873
1874 const uint32_t alg_k = cipher->algorithm_mkey;
1875 const uint32_t alg_a = cipher->algorithm_auth;
1876 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1877 return 1;
1878 }
1879 }
1880
1881 return 0;
1882}
1883
Adam Langleybdd5d662015-07-20 16:19:08 -07001884static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001885 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001886 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1887 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001888 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1889 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001890 !CBB_flush(out)) {
1891 return 0;
1892 }
1893
1894 return 1;
1895}
1896
1897static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1898 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1899 return 1;
1900 }
1901
1902 return ext_ec_point_add_extension(ssl, out);
1903}
1904
1905static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1906 CBS *contents) {
1907 if (contents == NULL) {
1908 return 1;
1909 }
1910
Steven Valdez143e8b32016-07-11 13:19:03 -04001911 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1912 return 0;
1913 }
1914
Adam Langleybdd5d662015-07-20 16:19:08 -07001915 CBS ec_point_format_list;
1916 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1917 CBS_len(contents) != 0) {
1918 return 0;
1919 }
1920
David Benjaminfc059942015-07-30 23:01:59 -04001921 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1922 * point format. */
1923 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1924 CBS_len(&ec_point_format_list)) == NULL) {
1925 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001926 return 0;
1927 }
1928
1929 return 1;
1930}
1931
1932static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1933 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001934 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1935 return 1;
1936 }
1937
Adam Langleybdd5d662015-07-20 16:19:08 -07001938 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1939}
1940
1941static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001942 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1943 return 1;
1944 }
1945
Adam Langleybdd5d662015-07-20 16:19:08 -07001946 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1947 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001948 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001949
1950 if (!using_ecc) {
1951 return 1;
1952 }
1953
1954 return ext_ec_point_add_extension(ssl, out);
1955}
1956
Adam Langley273d49c2015-07-20 16:38:52 -07001957
Steven Valdez143e8b32016-07-11 13:19:03 -04001958/* Draft Version Extension */
1959
1960static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1961 uint16_t min_version, max_version;
1962 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001963 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001964 return 1;
1965 }
1966
1967 CBB contents;
1968 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1969 !CBB_add_u16_length_prefixed(out, &contents) ||
1970 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1971 return 0;
1972 }
1973
1974 return CBB_flush(out);
1975}
1976
1977
Steven Valdez4aa154e2016-07-29 14:32:55 -04001978/* Pre Shared Key
1979 *
1980 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
1981
1982static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1983 uint16_t min_version, max_version;
1984 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1985 return 0;
1986 }
1987
1988 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1989 ssl->method->version_from_wire(ssl->session->ssl_version) <
1990 TLS1_3_VERSION) {
1991 return 1;
1992 }
1993
1994 CBB contents, identities, identity;
1995 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
1996 !CBB_add_u16_length_prefixed(out, &contents) ||
1997 !CBB_add_u16_length_prefixed(&contents, &identities) ||
1998 !CBB_add_u16_length_prefixed(&identities, &identity) ||
1999 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2000 ssl->session->tlsext_ticklen)) {
2001 return 0;
2002 }
2003
2004 return CBB_flush(out);
2005}
2006
2007int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2008 CBS *contents) {
2009 uint16_t psk_id;
2010 if (!CBS_get_u16(contents, &psk_id) ||
2011 CBS_len(contents) != 0) {
2012 *out_alert = SSL_AD_DECODE_ERROR;
2013 return 0;
2014 }
2015
2016 if (psk_id != 0) {
2017 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2018 return 0;
2019 }
2020
2021 return 1;
2022}
2023
2024int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2025 SSL_SESSION **out_session,
2026 uint8_t *out_alert,
2027 CBS *contents) {
2028 CBS identities, identity;
2029 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2030 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2031 CBS_len(contents) != 0) {
2032 *out_alert = SSL_AD_DECODE_ERROR;
2033 return 0;
2034 }
2035
2036 /* TLS 1.3 session tickets are renewed separately as part of the
2037 * NewSessionTicket. */
2038 int renew;
2039 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2040 CBS_len(&identity), NULL, 0);
2041}
2042
2043int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2044 if (!ssl->s3->session_reused) {
2045 return 1;
2046 }
2047
2048 CBB contents;
2049 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2050 !CBB_add_u16_length_prefixed(out, &contents) ||
2051 /* We only consider the first identity for resumption */
2052 !CBB_add_u16(&contents, 0) ||
2053 !CBB_flush(out)) {
2054 return 0;
2055 }
2056
2057 return 1;
2058}
2059
2060
Steven Valdez143e8b32016-07-11 13:19:03 -04002061/* Key Share
2062 *
2063 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2064
2065static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2066 uint16_t min_version, max_version;
2067 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2068 return 0;
2069 }
2070
2071 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2072 return 1;
2073 }
2074
2075 CBB contents, kse_bytes;
2076 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2077 !CBB_add_u16_length_prefixed(out, &contents) ||
2078 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2079 return 0;
2080 }
2081
2082 const uint16_t *groups;
2083 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002084 if (ssl->s3->hs->retry_group) {
2085 /* Append the new key share to the old list. */
2086 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2087 ssl->s3->hs->key_share_bytes_len)) {
2088 return 0;
2089 }
2090 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2091 ssl->s3->hs->key_share_bytes = NULL;
2092
2093 groups = &ssl->s3->hs->retry_group;
2094 groups_len = 1;
2095 } else {
2096 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2097 /* Only send the top two preferred key shares. */
2098 if (groups_len > 2) {
2099 groups_len = 2;
2100 }
2101 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002102
2103 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2104 if (ssl->s3->hs->groups == NULL) {
2105 return 0;
2106 }
2107 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2108 ssl->s3->hs->groups_len = groups_len;
2109
2110 for (size_t i = 0; i < groups_len; i++) {
2111 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2112 return 0;
2113 }
2114
2115 CBB key_exchange;
2116 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2117 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2118 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2119 !CBB_flush(&kse_bytes)) {
2120 return 0;
2121 }
2122 }
2123
Steven Valdez5440fe02016-07-18 12:40:30 -04002124 if (!ssl->s3->hs->retry_group) {
2125 /* Save the contents of the extension to repeat it in the second
2126 * ClientHello. */
2127 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2128 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2129 CBB_len(&kse_bytes));
2130 if (ssl->s3->hs->key_share_bytes == NULL) {
2131 return 0;
2132 }
2133 }
2134
Steven Valdez143e8b32016-07-11 13:19:03 -04002135 return CBB_flush(out);
2136}
2137
Steven Valdez7259f2f2016-08-02 16:55:05 -04002138int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2139 size_t *out_secret_len,
2140 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002141 CBS peer_key;
2142 uint16_t group;
2143 if (!CBS_get_u16(contents, &group) ||
David Benjamina70de142016-08-02 16:52:57 -04002144 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2145 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002146 *out_alert = SSL_AD_DECODE_ERROR;
2147 return 0;
2148 }
2149
2150 SSL_ECDH_CTX *group_ctx = NULL;
2151 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2152 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2153 group_ctx = &ssl->s3->hs->groups[i];
2154 break;
2155 }
2156 }
2157
2158 if (group_ctx == NULL) {
2159 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2160 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2161 return 0;
2162 }
2163
2164 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2165 CBS_data(&peer_key), CBS_len(&peer_key))) {
2166 *out_alert = SSL_AD_INTERNAL_ERROR;
2167 return 0;
2168 }
2169
Steven Valdez5440fe02016-07-18 12:40:30 -04002170 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002171 return 1;
2172}
2173
Steven Valdez7259f2f2016-08-02 16:55:05 -04002174int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2175 uint8_t **out_secret,
2176 size_t *out_secret_len,
2177 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 uint16_t group_id;
2179 CBS key_shares;
2180 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002181 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2182 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002183 return 0;
2184 }
2185
Steven Valdez5440fe02016-07-18 12:40:30 -04002186 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002187 while (CBS_len(&key_shares) > 0) {
2188 uint16_t id;
2189 CBS peer_key;
2190 if (!CBS_get_u16(&key_shares, &id) ||
2191 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2192 return 0;
2193 }
2194
Steven Valdez5440fe02016-07-18 12:40:30 -04002195 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002196 continue;
2197 }
2198
2199 SSL_ECDH_CTX group;
2200 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2201 CBB public_key;
2202 if (!CBB_init(&public_key, 0) ||
2203 !SSL_ECDH_CTX_init(&group, group_id) ||
2204 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2205 out_alert, CBS_data(&peer_key),
2206 CBS_len(&peer_key)) ||
2207 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2208 &ssl->s3->hs->public_key_len)) {
2209 SSL_ECDH_CTX_cleanup(&group);
2210 CBB_cleanup(&public_key);
2211 return 0;
2212 }
2213 SSL_ECDH_CTX_cleanup(&group);
2214
Steven Valdez5440fe02016-07-18 12:40:30 -04002215 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002216 }
2217
Steven Valdez5440fe02016-07-18 12:40:30 -04002218 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002219}
2220
Steven Valdez7259f2f2016-08-02 16:55:05 -04002221int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002222 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2223 return 1;
2224 }
2225
2226 uint16_t group_id;
2227 CBB kse_bytes, public_key;
2228 if (!tls1_get_shared_group(ssl, &group_id) ||
2229 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2230 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2231 !CBB_add_u16(&kse_bytes, group_id) ||
2232 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2233 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2234 ssl->s3->hs->public_key_len) ||
2235 !CBB_flush(out)) {
2236 return 0;
2237 }
2238
2239 return 1;
2240}
2241
2242
Steven Valdezce902a92016-05-17 11:47:53 -04002243/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002244 *
Steven Valdezce902a92016-05-17 11:47:53 -04002245 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2246 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002247
Steven Valdezce902a92016-05-17 11:47:53 -04002248static void ext_supported_groups_init(SSL *ssl) {
2249 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2250 ssl->s3->tmp.peer_supported_group_list = NULL;
2251 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002252}
2253
Steven Valdezce902a92016-05-17 11:47:53 -04002254static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002255 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2256 return 1;
2257 }
2258
Steven Valdezce902a92016-05-17 11:47:53 -04002259 CBB contents, groups_bytes;
2260 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002261 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002262 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002263 return 0;
2264 }
2265
Steven Valdezce902a92016-05-17 11:47:53 -04002266 const uint16_t *groups;
2267 size_t groups_len;
2268 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002269
2270 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002271 for (i = 0; i < groups_len; i++) {
2272 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002273 return 0;
2274 }
2275 }
2276
2277 return CBB_flush(out);
2278}
2279
Steven Valdezce902a92016-05-17 11:47:53 -04002280static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2281 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002282 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2283 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002284 return 1;
2285}
2286
Steven Valdezce902a92016-05-17 11:47:53 -04002287static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2288 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002289 if (contents == NULL) {
2290 return 1;
2291 }
2292
Steven Valdezce902a92016-05-17 11:47:53 -04002293 CBS supported_group_list;
2294 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2295 CBS_len(&supported_group_list) == 0 ||
2296 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002297 CBS_len(contents) != 0) {
2298 return 0;
2299 }
2300
Steven Valdezce902a92016-05-17 11:47:53 -04002301 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2302 CBS_len(&supported_group_list));
2303 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002304 *out_alert = SSL_AD_INTERNAL_ERROR;
2305 return 0;
2306 }
2307
Steven Valdezce902a92016-05-17 11:47:53 -04002308 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002309 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002310 for (i = 0; i < num_groups; i++) {
2311 if (!CBS_get_u16(&supported_group_list,
2312 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002313 goto err;
2314 }
2315 }
2316
Steven Valdezce902a92016-05-17 11:47:53 -04002317 assert(CBS_len(&supported_group_list) == 0);
2318 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002319
2320 return 1;
2321
2322err:
Steven Valdezce902a92016-05-17 11:47:53 -04002323 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2324 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002325 *out_alert = SSL_AD_INTERNAL_ERROR;
2326 return 0;
2327}
2328
Steven Valdezce902a92016-05-17 11:47:53 -04002329static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002330 /* Servers don't echo this extension. */
2331 return 1;
2332}
2333
2334
Adam Langley614c66a2015-06-12 15:26:58 -07002335/* kExtensions contains all the supported extensions. */
2336static const struct tls_extension kExtensions[] = {
2337 {
Adam Langley5021b222015-06-12 18:27:58 -07002338 TLSEXT_TYPE_renegotiate,
2339 NULL,
2340 ext_ri_add_clienthello,
2341 ext_ri_parse_serverhello,
2342 ext_ri_parse_clienthello,
2343 ext_ri_add_serverhello,
2344 },
2345 {
Adam Langley614c66a2015-06-12 15:26:58 -07002346 TLSEXT_TYPE_server_name,
2347 ext_sni_init,
2348 ext_sni_add_clienthello,
2349 ext_sni_parse_serverhello,
2350 ext_sni_parse_clienthello,
2351 ext_sni_add_serverhello,
2352 },
Adam Langley0a056712015-07-01 15:03:33 -07002353 {
2354 TLSEXT_TYPE_extended_master_secret,
2355 ext_ems_init,
2356 ext_ems_add_clienthello,
2357 ext_ems_parse_serverhello,
2358 ext_ems_parse_clienthello,
2359 ext_ems_add_serverhello,
2360 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002361 {
2362 TLSEXT_TYPE_session_ticket,
2363 NULL,
2364 ext_ticket_add_clienthello,
2365 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002366 /* Ticket extension client parsing is handled in ssl_session.c */
2367 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002368 ext_ticket_add_serverhello,
2369 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002370 {
2371 TLSEXT_TYPE_signature_algorithms,
2372 NULL,
2373 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002374 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002375 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002376 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002377 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002378 {
2379 TLSEXT_TYPE_status_request,
2380 ext_ocsp_init,
2381 ext_ocsp_add_clienthello,
2382 ext_ocsp_parse_serverhello,
2383 ext_ocsp_parse_clienthello,
2384 ext_ocsp_add_serverhello,
2385 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002386 {
2387 TLSEXT_TYPE_next_proto_neg,
2388 ext_npn_init,
2389 ext_npn_add_clienthello,
2390 ext_npn_parse_serverhello,
2391 ext_npn_parse_clienthello,
2392 ext_npn_add_serverhello,
2393 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002394 {
2395 TLSEXT_TYPE_certificate_timestamp,
2396 NULL,
2397 ext_sct_add_clienthello,
2398 ext_sct_parse_serverhello,
2399 ext_sct_parse_clienthello,
2400 ext_sct_add_serverhello,
2401 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002402 {
2403 TLSEXT_TYPE_application_layer_protocol_negotiation,
2404 ext_alpn_init,
2405 ext_alpn_add_clienthello,
2406 ext_alpn_parse_serverhello,
2407 ext_alpn_parse_clienthello,
2408 ext_alpn_add_serverhello,
2409 },
Adam Langley49c7af12015-07-10 14:33:46 -07002410 {
2411 TLSEXT_TYPE_channel_id,
2412 ext_channel_id_init,
2413 ext_channel_id_add_clienthello,
2414 ext_channel_id_parse_serverhello,
2415 ext_channel_id_parse_clienthello,
2416 ext_channel_id_add_serverhello,
2417 },
Adam Langley391250d2015-07-15 19:06:07 -07002418 {
2419 TLSEXT_TYPE_srtp,
2420 ext_srtp_init,
2421 ext_srtp_add_clienthello,
2422 ext_srtp_parse_serverhello,
2423 ext_srtp_parse_clienthello,
2424 ext_srtp_add_serverhello,
2425 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002426 {
2427 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002428 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002429 ext_ec_point_add_clienthello,
2430 ext_ec_point_parse_serverhello,
2431 ext_ec_point_parse_clienthello,
2432 ext_ec_point_add_serverhello,
2433 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002434 {
2435 TLSEXT_TYPE_draft_version,
2436 NULL,
2437 ext_draft_version_add_clienthello,
2438 forbid_parse_serverhello,
2439 ignore_parse_clienthello,
2440 dont_add_serverhello,
2441 },
2442 {
2443 TLSEXT_TYPE_key_share,
2444 NULL,
2445 ext_key_share_add_clienthello,
2446 forbid_parse_serverhello,
2447 ignore_parse_clienthello,
2448 dont_add_serverhello,
2449 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002450 {
2451 TLSEXT_TYPE_pre_shared_key,
2452 NULL,
2453 ext_pre_shared_key_add_clienthello,
2454 forbid_parse_serverhello,
2455 ignore_parse_clienthello,
2456 dont_add_serverhello,
2457 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002458 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2459 * intolerant to the last extension being zero-length. See
2460 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002461 {
Steven Valdezce902a92016-05-17 11:47:53 -04002462 TLSEXT_TYPE_supported_groups,
2463 ext_supported_groups_init,
2464 ext_supported_groups_add_clienthello,
2465 ext_supported_groups_parse_serverhello,
2466 ext_supported_groups_parse_clienthello,
2467 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002468 },
Adam Langley614c66a2015-06-12 15:26:58 -07002469};
2470
2471#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2472
Adam Langley4cfa96b2015-07-01 11:56:55 -07002473OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2474 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002475 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002476OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2477 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2478 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002479 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002480
Adam Langley614c66a2015-06-12 15:26:58 -07002481static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2482 uint16_t value) {
2483 unsigned i;
2484 for (i = 0; i < kNumExtensions; i++) {
2485 if (kExtensions[i].value == value) {
2486 *out_index = i;
2487 return &kExtensions[i];
2488 }
2489 }
2490
2491 return NULL;
2492}
2493
Adam Langley09505632015-07-30 18:10:13 -07002494int SSL_extension_supported(unsigned extension_value) {
2495 uint32_t index;
2496 return extension_value == TLSEXT_TYPE_padding ||
2497 tls_extension_find(&index, extension_value) != NULL;
2498}
2499
David Benjamine8d53502015-10-10 14:13:23 -04002500int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002502 if (ssl->client_version == SSL3_VERSION &&
2503 !ssl->s3->send_connection_binding) {
2504 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002505 }
Adam Langley95c29f32014-06-20 12:00:00 -07002506
David Benjamine8d53502015-10-10 14:13:23 -04002507 CBB extensions;
2508 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002509 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002510 }
Adam Langley95c29f32014-06-20 12:00:00 -07002511
David Benjamine8d53502015-10-10 14:13:23 -04002512 ssl->s3->tmp.extensions.sent = 0;
2513 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002514
Adam Langley614c66a2015-06-12 15:26:58 -07002515 size_t i;
2516 for (i = 0; i < kNumExtensions; i++) {
2517 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002518 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002519 }
2520 }
Adam Langley95c29f32014-06-20 12:00:00 -07002521
Adam Langley614c66a2015-06-12 15:26:58 -07002522 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002523 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002524 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002525 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2526 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2527 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002528 }
Adam Langley95c29f32014-06-20 12:00:00 -07002529
Adam Langley33ad2b52015-07-20 17:43:53 -07002530 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002531 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002532 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 }
Adam Langley75712922014-10-10 16:23:43 -07002534
David Benjamine8d53502015-10-10 14:13:23 -04002535 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002536 goto err;
2537 }
2538
David Benjamince079fd2016-08-02 16:22:34 -04002539 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002540 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002541 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002542 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002543 *
2544 * NB: because this code works out the length of all existing extensions
2545 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002546 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002547 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002549 * Server 7.0 is intolerant to the last extension being zero-length. See
2550 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002551 if (padding_len >= 4 + 1) {
2552 padding_len -= 4;
2553 } else {
2554 padding_len = 1;
2555 }
Adam Langley95c29f32014-06-20 12:00:00 -07002556
Adam Langley33ad2b52015-07-20 17:43:53 -07002557 uint8_t *padding_bytes;
2558 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2559 !CBB_add_u16(&extensions, padding_len) ||
2560 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2561 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002562 }
Adam Langley75712922014-10-10 16:23:43 -07002563
Adam Langley33ad2b52015-07-20 17:43:53 -07002564 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002565 }
2566 }
Adam Langley75712922014-10-10 16:23:43 -07002567
David Benjamina01deee2015-12-08 18:56:31 -05002568 /* Discard empty extensions blocks. */
2569 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002570 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002571 }
2572
David Benjamine8d53502015-10-10 14:13:23 -04002573 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002574
2575err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002576 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002577 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002578}
Adam Langley95c29f32014-06-20 12:00:00 -07002579
David Benjamin56380462015-10-10 14:59:09 -04002580int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002581 CBB extensions;
2582 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002583 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002584 }
2585
2586 unsigned i;
2587 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002588 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002589 /* Don't send extensions that were not received. */
2590 continue;
Adam Langleyfcf25832014-12-18 17:42:32 -08002591 }
Adam Langley95c29f32014-06-20 12:00:00 -07002592
David Benjamin56380462015-10-10 14:59:09 -04002593 if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002594 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2595 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2596 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002597 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002598 }
Adam Langley95c29f32014-06-20 12:00:00 -07002599
David Benjamin56380462015-10-10 14:59:09 -04002600 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002601 goto err;
2602 }
2603
Steven Valdez143e8b32016-07-11 13:19:03 -04002604 /* Discard empty extensions blocks before TLS 1.3. */
2605 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2606 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002607 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002608 }
2609
David Benjamin56380462015-10-10 14:59:09 -04002610 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002611
2612err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002613 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002614 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002615}
Adam Langley95c29f32014-06-20 12:00:00 -07002616
David Benjamine14ff062016-08-09 16:21:24 -04002617static int ssl_scan_clienthello_tlsext(
2618 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2619 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002620 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002621 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002622 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002623 }
2624 }
2625
David Benjamin0d56f882015-12-19 17:05:56 -05002626 ssl->s3->tmp.extensions.received = 0;
2627 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002628
David Benjamine14ff062016-08-09 16:21:24 -04002629 CBS extensions;
2630 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2631 while (CBS_len(&extensions) != 0) {
2632 uint16_t type;
2633 CBS extension;
2634
2635 /* Decode the next extension. */
2636 if (!CBS_get_u16(&extensions, &type) ||
2637 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 *out_alert = SSL_AD_DECODE_ERROR;
2639 return 0;
2640 }
Adam Langley95c29f32014-06-20 12:00:00 -07002641
David Benjamine14ff062016-08-09 16:21:24 -04002642 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2643 * ambiguous. Ignore all but the renegotiation_info extension. */
2644 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2645 continue;
2646 }
Adam Langley95c29f32014-06-20 12:00:00 -07002647
David Benjamine14ff062016-08-09 16:21:24 -04002648 unsigned ext_index;
2649 const struct tls_extension *const ext =
2650 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002651
David Benjamine14ff062016-08-09 16:21:24 -04002652 if (ext == NULL) {
2653 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002654 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002655 return 0;
2656 }
David Benjamine14ff062016-08-09 16:21:24 -04002657 continue;
2658 }
2659
2660 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2661 uint8_t alert = SSL_AD_DECODE_ERROR;
2662 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2663 *out_alert = alert;
2664 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2665 ERR_add_error_dataf("extension: %u", (unsigned)type);
2666 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002667 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002668 }
Adam Langley75712922014-10-10 16:23:43 -07002669
David Benjamin1deb41b2016-08-09 19:36:38 -04002670 for (size_t i = 0; i < kNumExtensions; i++) {
2671 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2672 continue;
2673 }
2674
2675 CBS *contents = NULL, fake_contents;
2676 static const uint8_t kFakeRenegotiateExtension[] = {0};
2677 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2678 ssl_client_cipher_list_contains_cipher(client_hello,
2679 SSL3_CK_SCSV & 0xffff)) {
2680 /* The renegotiation SCSV was received so pretend that we received a
2681 * renegotiation extension. */
2682 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2683 sizeof(kFakeRenegotiateExtension));
2684 contents = &fake_contents;
2685 ssl->s3->tmp.extensions.received |= (1u << i);
2686 }
2687
2688 /* Extension wasn't observed so call the callback with a NULL
2689 * parameter. */
2690 uint8_t alert = SSL_AD_DECODE_ERROR;
2691 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2692 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2693 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2694 *out_alert = alert;
2695 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002696 }
2697 }
2698
Adam Langleyfcf25832014-12-18 17:42:32 -08002699 return 1;
2700}
Adam Langley95c29f32014-06-20 12:00:00 -07002701
David Benjamine14ff062016-08-09 16:21:24 -04002702int ssl_parse_clienthello_tlsext(
2703 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002704 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002705 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002706 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002707 return 0;
2708 }
Adam Langley95c29f32014-06-20 12:00:00 -07002709
David Benjamin0d56f882015-12-19 17:05:56 -05002710 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002711 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002712 return 0;
2713 }
Adam Langley95c29f32014-06-20 12:00:00 -07002714
Adam Langleyfcf25832014-12-18 17:42:32 -08002715 return 1;
2716}
Adam Langley95c29f32014-06-20 12:00:00 -07002717
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002718OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2719
David Benjamin0d56f882015-12-19 17:05:56 -05002720static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002721 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2722 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2723 return 1;
2724 }
Adam Langley614c66a2015-06-12 15:26:58 -07002725
Steven Valdez143e8b32016-07-11 13:19:03 -04002726 /* Decode the extensions block and check it is valid. */
2727 CBS extensions;
2728 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2729 !tls1_check_duplicate_extensions(&extensions)) {
2730 *out_alert = SSL_AD_DECODE_ERROR;
2731 return 0;
2732 }
2733
2734 uint32_t received = 0;
2735 while (CBS_len(&extensions) != 0) {
2736 uint16_t type;
2737 CBS extension;
2738
2739 /* Decode the next extension. */
2740 if (!CBS_get_u16(&extensions, &type) ||
2741 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002742 *out_alert = SSL_AD_DECODE_ERROR;
2743 return 0;
2744 }
Adam Langley95c29f32014-06-20 12:00:00 -07002745
Steven Valdez143e8b32016-07-11 13:19:03 -04002746 unsigned ext_index;
2747 const struct tls_extension *const ext =
2748 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002749
Steven Valdez143e8b32016-07-11 13:19:03 -04002750 if (ext == NULL) {
2751 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002752 return 0;
2753 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002754 continue;
2755 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002756
David Benjamin1deb41b2016-08-09 19:36:38 -04002757 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2758 type != TLSEXT_TYPE_renegotiate) {
2759 /* If the extension was never sent then it is illegal, except for the
2760 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002761 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2762 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002763 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002764 return 0;
2765 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002766
Steven Valdez143e8b32016-07-11 13:19:03 -04002767 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002768
Steven Valdez143e8b32016-07-11 13:19:03 -04002769 uint8_t alert = SSL_AD_DECODE_ERROR;
2770 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2771 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2772 ERR_add_error_dataf("extension: %u", (unsigned)type);
2773 *out_alert = alert;
2774 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002775 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002776 }
Adam Langley95c29f32014-06-20 12:00:00 -07002777
Adam Langley33ad2b52015-07-20 17:43:53 -07002778 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002779 for (i = 0; i < kNumExtensions; i++) {
2780 if (!(received & (1u << i))) {
2781 /* Extension wasn't observed so call the callback with a NULL
2782 * parameter. */
2783 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002784 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002785 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2786 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002787 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002788 return 0;
2789 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002790 }
2791 }
Adam Langley95c29f32014-06-20 12:00:00 -07002792
Adam Langleyfcf25832014-12-18 17:42:32 -08002793 return 1;
2794}
Adam Langley95c29f32014-06-20 12:00:00 -07002795
David Benjamin0d56f882015-12-19 17:05:56 -05002796static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 int ret = SSL_TLSEXT_ERR_NOACK;
2798 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002799
David Benjamin78f8aab2016-03-10 16:33:58 -05002800 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002801 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002802 ssl->ctx->tlsext_servername_arg);
2803 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002804 ret = ssl->initial_ctx->tlsext_servername_callback(
2805 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002806 }
Adam Langley95c29f32014-06-20 12:00:00 -07002807
Adam Langleyfcf25832014-12-18 17:42:32 -08002808 switch (ret) {
2809 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002810 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002811 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002812
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002814 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002816
Adam Langleyfcf25832014-12-18 17:42:32 -08002817 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002818 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002819 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002820
Adam Langleyfcf25832014-12-18 17:42:32 -08002821 default:
2822 return 1;
2823 }
2824}
Adam Langleyed8270a2014-09-02 13:52:56 -07002825
David Benjamin0d56f882015-12-19 17:05:56 -05002826static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002827 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002829
David Benjamin78f8aab2016-03-10 16:33:58 -05002830 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002831 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002832 ssl->ctx->tlsext_servername_arg);
2833 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002834 ret = ssl->initial_ctx->tlsext_servername_callback(
2835 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002836 }
Adam Langley95c29f32014-06-20 12:00:00 -07002837
Adam Langleyfcf25832014-12-18 17:42:32 -08002838 switch (ret) {
2839 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002840 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002841 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002842
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002844 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 return 1;
2846
2847 default:
2848 return 1;
2849 }
2850}
2851
David Benjamin0d56f882015-12-19 17:05:56 -05002852int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002853 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002854 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2855 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002856 return 0;
2857 }
2858
David Benjamin0d56f882015-12-19 17:05:56 -05002859 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002860 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002861 return 0;
2862 }
2863
2864 return 1;
2865}
Adam Langley95c29f32014-06-20 12:00:00 -07002866
David Benjamine3aa1d92015-06-16 15:34:50 -04002867int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002868 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002869 size_t ticket_len, const uint8_t *session_id,
2870 size_t session_id_len) {
2871 int ret = 1; /* Most errors are non-fatal. */
2872 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2873 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002874
David Benjamine3aa1d92015-06-16 15:34:50 -04002875 HMAC_CTX hmac_ctx;
2876 HMAC_CTX_init(&hmac_ctx);
2877 EVP_CIPHER_CTX cipher_ctx;
2878 EVP_CIPHER_CTX_init(&cipher_ctx);
2879
David Benjaminef1b0092015-11-21 14:05:44 -05002880 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002881 *out_session = NULL;
2882
Steven Valdez4aa154e2016-07-29 14:32:55 -04002883 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2884 goto done;
2885 }
2886
David Benjamine3aa1d92015-06-16 15:34:50 -04002887 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2888 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002889 }
2890
David Benjaminadcc3952015-04-26 13:07:57 -04002891 /* Ensure there is room for the key name and the largest IV
2892 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2893 * the maximum IV length should be well under the minimum size for the
2894 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002895 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2896 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002897 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002898 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002899
David Benjamine3aa1d92015-06-16 15:34:50 -04002900 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002901 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2902 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2903 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002904 if (cb_ret < 0) {
2905 ret = 0;
2906 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002907 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002908 if (cb_ret == 0) {
2909 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002910 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002911 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002912 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002913 }
2914 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002915 /* Check the key name matches. */
2916 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2917 SSL_TICKET_KEY_NAME_LEN) != 0) {
2918 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002919 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002920 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2921 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002922 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002923 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2924 ssl_ctx->tlsext_tick_aes_key, iv)) {
2925 ret = 0;
2926 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002927 }
2928 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002929 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002930
David Benjamine3aa1d92015-06-16 15:34:50 -04002931 /* Check the MAC at the end of the ticket. */
2932 uint8_t mac[EVP_MAX_MD_SIZE];
2933 size_t mac_len = HMAC_size(&hmac_ctx);
2934 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002935 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002936 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002937 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002938 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2939 HMAC_Final(&hmac_ctx, mac, NULL);
2940 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2941 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002942 }
2943
David Benjamine3aa1d92015-06-16 15:34:50 -04002944 /* Decrypt the session data. */
2945 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2946 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2947 mac_len;
2948 plaintext = OPENSSL_malloc(ciphertext_len);
2949 if (plaintext == NULL) {
2950 ret = 0;
2951 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002952 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002953 if (ciphertext_len >= INT_MAX) {
2954 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002955 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002956 int len1, len2;
2957 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2958 (int)ciphertext_len) ||
2959 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2960 ERR_clear_error(); /* Don't leave an error on the queue. */
2961 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 }
2963
David Benjamine3aa1d92015-06-16 15:34:50 -04002964 /* Decode the session. */
2965 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2966 if (session == NULL) {
2967 ERR_clear_error(); /* Don't leave an error on the queue. */
2968 goto done;
2969 }
2970
2971 /* Copy the client's session ID into the new session, to denote the ticket has
2972 * been accepted. */
2973 memcpy(session->session_id, session_id, session_id_len);
2974 session->session_id_length = session_id_len;
2975
Steven Valdez4aa154e2016-07-29 14:32:55 -04002976 if (!ssl_session_is_context_valid(ssl, session) ||
2977 !ssl_session_is_time_valid(ssl, session)) {
2978 SSL_SESSION_free(session);
2979 session = NULL;
2980 }
2981
David Benjamine3aa1d92015-06-16 15:34:50 -04002982 *out_session = session;
2983
2984done:
2985 OPENSSL_free(plaintext);
2986 HMAC_CTX_cleanup(&hmac_ctx);
2987 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2988 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002989}
Adam Langley95c29f32014-06-20 12:00:00 -07002990
Steven Valdez0d62f262015-09-04 12:41:04 -04002991int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002992 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05002993 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002994 return 1;
2995 }
David Benjamincd996942014-07-20 16:23:51 -04002996
Steven Valdez0d62f262015-09-04 12:41:04 -04002997 CERT *const cert = ssl->cert;
2998 OPENSSL_free(cert->peer_sigalgs);
2999 cert->peer_sigalgs = NULL;
3000 cert->peer_sigalgslen = 0;
3001
3002 size_t num_sigalgs = CBS_len(in_sigalgs);
3003
3004 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003005 return 0;
3006 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003007 num_sigalgs /= 2;
3008
3009 /* supported_signature_algorithms in the certificate request is
3010 * allowed to be empty. */
3011 if (num_sigalgs == 0) {
3012 return 1;
3013 }
3014
Steven Valdez02563852016-06-23 13:33:05 -04003015 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3016 * and we just divided |num_sigalgs| by two. */
3017 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04003018 if (cert->peer_sigalgs == NULL) {
3019 return 0;
3020 }
3021 cert->peer_sigalgslen = num_sigalgs;
3022
3023 CBS sigalgs;
3024 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
3025
3026 size_t i;
3027 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04003028 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003029 return 0;
3030 }
3031 }
Adam Langley95c29f32014-06-20 12:00:00 -07003032
Adam Langleyfcf25832014-12-18 17:42:32 -08003033 return 1;
3034}
David Benjaminec2f27d2014-11-13 19:17:25 -05003035
David Benjaminea9a0d52016-07-08 15:52:59 -07003036int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003037 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04003038 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05003039
Steven Valdezf0451ca2016-06-29 13:16:27 -04003040 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3041 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3042 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003043 int type = ssl_private_key_type(ssl);
3044 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003045 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003046 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003047 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003048 if (ssl_is_ecdsa_key_type(type)) {
3049 *out = SSL_SIGN_ECDSA_SHA1;
3050 return 1;
3051 }
3052 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3053 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003054 }
3055
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003056 const uint16_t *sigalgs;
3057 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003058 if (cert->sigalgs != NULL) {
3059 sigalgs = cert->sigalgs;
3060 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04003061 }
3062
David Benjaminea9a0d52016-07-08 15:52:59 -07003063 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
3064 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07003065 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003066 /* If the client didn't specify any signature_algorithms extension then
3067 * we can assume that it supports SHA1. See
3068 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3069 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3070 SSL_SIGN_ECDSA_SHA1};
3071 peer_sigalgs = kDefaultPeerAlgorithms;
3072 peer_sigalgs_len =
3073 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms);
3074 }
3075
David Benjamind246b812016-07-08 15:07:02 -07003076 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003077 uint16_t sigalg = sigalgs[i];
3078 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3079 * negotiated. */
3080 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3081 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3082 continue;
3083 }
3084
David Benjaminea9a0d52016-07-08 15:52:59 -07003085 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003086 if (sigalg == peer_sigalgs[j]) {
3087 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003088 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003089 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003090 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003091 }
Adam Langley95c29f32014-06-20 12:00:00 -07003092
David Benjaminea9a0d52016-07-08 15:52:59 -07003093 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3094 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003095}
Adam Langley95c29f32014-06-20 12:00:00 -07003096
David Benjamind6a4ae92015-08-06 11:10:51 -04003097int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3098 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003099 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003100
3101 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003102 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3103 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003104 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003105
David Benjamind6a4ae92015-08-06 11:10:51 -04003106 static const char kClientIDMagic[] = "TLS Channel ID signature";
3107 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3108
Steven Valdez87eab492016-06-27 16:34:59 -04003109 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003110 static const char kResumptionMagic[] = "Resumption";
3111 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3112 if (ssl->session->original_handshake_hash_len == 0) {
3113 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3114 goto err;
3115 }
3116 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3117 ssl->session->original_handshake_hash_len);
3118 }
3119
3120 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3121 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3122 sizeof(handshake_hash));
3123 if (handshake_hash_len < 0) {
3124 goto err;
3125 }
3126 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3127 unsigned len_u;
3128 EVP_DigestFinal_ex(&ctx, out, &len_u);
3129 *out_len = len_u;
3130
3131 ret = 1;
3132
3133err:
3134 EVP_MD_CTX_cleanup(&ctx);
3135 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003136}
Adam Langley1258b6a2014-06-20 12:00:00 -07003137
3138/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003139 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003140 * data. */
3141int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003142 int digest_len;
3143 /* This function should never be called for a resumed session because the
3144 * handshake hashes that we wish to record are for the original, full
3145 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003146 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003147 return -1;
3148 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003149
Adam Langleyfcf25832014-12-18 17:42:32 -08003150 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003151 tls1_handshake_digest(
3152 ssl, ssl->s3->new_session->original_handshake_hash,
3153 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 if (digest_len < 0) {
3155 return -1;
3156 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003157
Steven Valdez87eab492016-06-27 16:34:59 -04003158 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003159
Adam Langleyfcf25832014-12-18 17:42:32 -08003160 return 1;
3161}