blob: a9a8fa330074d2b195f3080afbe67fdd63503666 [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) {
David Benjamin0e950152016-08-13 21:08:56 -0400546 uint16_t min_version, max_version;
547 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
548 assert(0); /* This should never happen. */
549
550 /* Return an empty list. */
551 ERR_clear_error();
552 *psigs = NULL;
553 return 0;
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400554 }
555
David Benjamin0e950152016-08-13 21:08:56 -0400556 /* TODO(davidben): Once TLS 1.3 has finalized, probably just advertise the
557 * same algorithm list regardless, as long as no fallback is needed. Note this
558 * may require care due to lingering NSS servers affected by
559 * https://bugzilla.mozilla.org/show_bug.cgi?id=1119983 */
560 if (max_version >= TLS1_3_VERSION) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400561 *psigs = kDefaultTLS13SignatureAlgorithms;
562 return sizeof(kDefaultTLS13SignatureAlgorithms) /
563 sizeof(kDefaultTLS13SignatureAlgorithms[0]);
564 }
565
Steven Valdez02563852016-06-23 13:33:05 -0400566 *psigs = kDefaultSignatureAlgorithms;
567 return sizeof(kDefaultSignatureAlgorithms) /
568 sizeof(kDefaultSignatureAlgorithms[0]);
Adam Langleyfcf25832014-12-18 17:42:32 -0800569}
Adam Langley95c29f32014-06-20 12:00:00 -0700570
David Benjamin887c3002016-07-08 16:15:32 -0700571int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
Steven Valdez02563852016-06-23 13:33:05 -0400572 const uint16_t *sent_sigs;
573 size_t sent_sigslen, i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800574
Adam Langleyfcf25832014-12-18 17:42:32 -0800575 /* Check signature matches a type we sent */
David Benjamin6e807652015-11-02 12:02:20 -0500576 sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
Steven Valdez02563852016-06-23 13:33:05 -0400577 for (i = 0; i < sent_sigslen; i++) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400578 if (sigalg == sent_sigs[i]) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 break;
580 }
581 }
582
David Benjamin788be4a2015-10-30 17:50:57 -0400583 if (i == sent_sigslen) {
David Benjamin3570d732015-06-29 00:28:17 -0400584 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
586 return 0;
587 }
588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 return 1;
590}
591
592/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
593 * supported or doesn't appear in supported signature algorithms. Unlike
594 * ssl_cipher_get_disabled this applies to a specific session and not global
595 * settings. */
David Benjamin4298d772015-12-19 00:18:25 -0500596void ssl_set_client_disabled(SSL *ssl) {
597 CERT *c = ssl->cert;
Steven Valdez02563852016-06-23 13:33:05 -0400598 const uint16_t *sigalgs;
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 size_t i, sigalgslen;
600 int have_rsa = 0, have_ecdsa = 0;
601 c->mask_a = 0;
602 c->mask_k = 0;
603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 /* Now go through all signature algorithms seeing if we support any for RSA,
605 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
David Benjamin4298d772015-12-19 00:18:25 -0500606 sigalgslen = tls12_get_psigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -0400607 for (i = 0; i < sigalgslen; i++) {
608 switch (sigalgs[i]) {
Steven Valdezeff1e8d2016-07-06 14:24:47 -0400609 case SSL_SIGN_RSA_PSS_SHA512:
610 case SSL_SIGN_RSA_PSS_SHA384:
611 case SSL_SIGN_RSA_PSS_SHA256:
Steven Valdez02563852016-06-23 13:33:05 -0400612 case SSL_SIGN_RSA_PKCS1_SHA512:
613 case SSL_SIGN_RSA_PKCS1_SHA384:
614 case SSL_SIGN_RSA_PKCS1_SHA256:
615 case SSL_SIGN_RSA_PKCS1_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 have_rsa = 1;
617 break;
618
Steven Valdez02563852016-06-23 13:33:05 -0400619 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
620 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
621 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
622 case SSL_SIGN_ECDSA_SHA1:
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 have_ecdsa = 1;
624 break;
625 }
626 }
627
628 /* Disable auth if we don't include any appropriate signature algorithms. */
629 if (!have_rsa) {
630 c->mask_a |= SSL_aRSA;
631 }
632 if (!have_ecdsa) {
633 c->mask_a |= SSL_aECDSA;
634 }
635
636 /* with PSK there must be client callback set */
David Benjamin4298d772015-12-19 00:18:25 -0500637 if (!ssl->psk_client_callback) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 c->mask_a |= SSL_aPSK;
639 c->mask_k |= SSL_kPSK;
640 }
641}
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langley614c66a2015-06-12 15:26:58 -0700643/* tls_extension represents a TLS extension that is handled internally. The
644 * |init| function is called for each handshake, before any other functions of
645 * the extension. Then the add and parse callbacks are called as needed.
646 *
647 * The parse callbacks receive a |CBS| that contains the contents of the
648 * extension (i.e. not including the type and length bytes). If an extension is
649 * not received then the parse callbacks will be called with a NULL CBS so that
650 * they can do any processing needed to handle the absence of an extension.
651 *
652 * The add callbacks receive a |CBB| to which the extension can be appended but
653 * the function is responsible for appending the type and length bytes too.
654 *
655 * All callbacks return one for success and zero for error. If a parse function
656 * returns zero then a fatal alert with value |*out_alert| will be sent. If
657 * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
658struct tls_extension {
659 uint16_t value;
660 void (*init)(SSL *ssl);
661
662 int (*add_clienthello)(SSL *ssl, CBB *out);
663 int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
664
665 int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
666 int (*add_serverhello)(SSL *ssl, CBB *out);
667};
668
Steven Valdez6b8509a2016-07-12 13:38:32 -0400669static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
670 if (contents != NULL) {
671 /* Servers MUST NOT send this extension. */
672 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
673 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
674 return 0;
675 }
676
677 return 1;
678}
679
680static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
681 /* This extension from the client is handled elsewhere. */
682 return 1;
683}
684
685static int dont_add_serverhello(SSL *ssl, CBB *out) {
686 return 1;
687}
Adam Langley614c66a2015-06-12 15:26:58 -0700688
689/* Server name indication (SNI).
690 *
691 * https://tools.ietf.org/html/rfc6066#section-3. */
692
693static void ext_sni_init(SSL *ssl) {
694 ssl->s3->tmp.should_ack_sni = 0;
695}
696
697static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
698 if (ssl->tlsext_hostname == NULL) {
699 return 1;
700 }
701
702 CBB contents, server_name_list, name;
703 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
704 !CBB_add_u16_length_prefixed(out, &contents) ||
705 !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
706 !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
707 !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
708 !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
709 strlen(ssl->tlsext_hostname)) ||
710 !CBB_flush(out)) {
711 return 0;
712 }
713
714 return 1;
715}
716
David Benjamin0d56f882015-12-19 17:05:56 -0500717static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
718 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700719 if (contents == NULL) {
720 return 1;
721 }
722
723 if (CBS_len(contents) != 0) {
724 return 0;
725 }
726
727 assert(ssl->tlsext_hostname != NULL);
728
Steven Valdez87eab492016-06-27 16:34:59 -0400729 if (ssl->session == NULL) {
730 assert(ssl->s3->new_session->tlsext_hostname == NULL);
731 ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
732 if (!ssl->s3->new_session->tlsext_hostname) {
Adam Langley614c66a2015-06-12 15:26:58 -0700733 *out_alert = SSL_AD_INTERNAL_ERROR;
734 return 0;
735 }
736 }
737
738 return 1;
739}
740
David Benjamin0d56f882015-12-19 17:05:56 -0500741static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
742 CBS *contents) {
Adam Langley614c66a2015-06-12 15:26:58 -0700743 if (contents == NULL) {
744 return 1;
745 }
746
David Benjamin9b611e22016-03-03 08:48:30 -0500747 CBS server_name_list, host_name;
748 uint8_t name_type;
Adam Langley614c66a2015-06-12 15:26:58 -0700749 if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
David Benjamin9b611e22016-03-03 08:48:30 -0500750 !CBS_get_u8(&server_name_list, &name_type) ||
751 /* Although the server_name extension was intended to be extensible to
752 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
753 * different name types will cause an error. Further, RFC 4366 originally
754 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
755 * adding new name types is no longer feasible.
756 *
757 * Act as if the extensibility does not exist to simplify parsing. */
758 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
759 CBS_len(&server_name_list) != 0 ||
Adam Langley614c66a2015-06-12 15:26:58 -0700760 CBS_len(contents) != 0) {
761 return 0;
762 }
763
David Benjamin9b611e22016-03-03 08:48:30 -0500764 if (name_type != TLSEXT_NAMETYPE_host_name ||
765 CBS_len(&host_name) == 0 ||
766 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
767 CBS_contains_zero_byte(&host_name)) {
768 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
769 return 0;
770 }
Adam Langley614c66a2015-06-12 15:26:58 -0700771
David Benjamin9b611e22016-03-03 08:48:30 -0500772 /* TODO(davidben): SNI should be resolved before resumption. We have the
773 * early callback as a replacement, but we should fix the current callback
774 * and avoid the need for |SSL_CTX_set_session_id_context|. */
Steven Valdez87eab492016-06-27 16:34:59 -0400775 if (ssl->session == NULL) {
776 assert(ssl->s3->new_session->tlsext_hostname == NULL);
David Benjamin9b611e22016-03-03 08:48:30 -0500777
778 /* Copy the hostname as a string. */
Steven Valdez87eab492016-06-27 16:34:59 -0400779 if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
David Benjamin9b611e22016-03-03 08:48:30 -0500780 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley614c66a2015-06-12 15:26:58 -0700781 return 0;
782 }
783
David Benjamin9b611e22016-03-03 08:48:30 -0500784 ssl->s3->tmp.should_ack_sni = 1;
Adam Langley614c66a2015-06-12 15:26:58 -0700785 }
786
787 return 1;
788}
789
790static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez87eab492016-06-27 16:34:59 -0400791 if (ssl->session != NULL ||
Adam Langley614c66a2015-06-12 15:26:58 -0700792 !ssl->s3->tmp.should_ack_sni ||
Steven Valdez87eab492016-06-27 16:34:59 -0400793 ssl->s3->new_session->tlsext_hostname == NULL) {
Adam Langley614c66a2015-06-12 15:26:58 -0700794 return 1;
795 }
796
797 if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
798 !CBB_add_u16(out, 0 /* length */)) {
799 return 0;
800 }
801
802 return 1;
803}
804
805
Adam Langley5021b222015-06-12 18:27:58 -0700806/* Renegotiation indication.
807 *
808 * https://tools.ietf.org/html/rfc5746 */
809
810static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
811 CBB contents, prev_finished;
812 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
813 !CBB_add_u16_length_prefixed(out, &contents) ||
814 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
815 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
816 ssl->s3->previous_client_finished_len) ||
817 !CBB_flush(out)) {
818 return 0;
819 }
820
821 return 1;
822}
823
824static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
825 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400826 if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
827 return 0;
828 }
829
David Benjamin3e052de2015-11-25 20:10:31 -0500830 /* Servers may not switch between omitting the extension and supporting it.
831 * See RFC 5746, sections 3.5 and 4.2. */
832 if (ssl->s3->initial_handshake_complete &&
833 (contents != NULL) != ssl->s3->send_connection_binding) {
834 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
835 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
836 return 0;
837 }
838
Adam Langley5021b222015-06-12 18:27:58 -0700839 if (contents == NULL) {
David Benjamine9cddb82015-11-23 14:36:40 -0500840 /* Strictly speaking, if we want to avoid an attack we should *always* see
Adam Langley5021b222015-06-12 18:27:58 -0700841 * RI even on initial ServerHello because the client doesn't see any
842 * renegotiation during an attack. However this would mean we could not
843 * connect to any server which doesn't support RI.
844 *
David Benjamine9cddb82015-11-23 14:36:40 -0500845 * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
846 * practical terms every client sets it so it's just assumed here. */
847 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700848 }
849
850 const size_t expected_len = ssl->s3->previous_client_finished_len +
851 ssl->s3->previous_server_finished_len;
852
853 /* Check for logic errors */
854 assert(!expected_len || ssl->s3->previous_client_finished_len);
855 assert(!expected_len || ssl->s3->previous_server_finished_len);
856
857 /* Parse out the extension contents. */
858 CBS renegotiated_connection;
859 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
860 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400861 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700862 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
863 return 0;
864 }
865
866 /* Check that the extension matches. */
867 if (CBS_len(&renegotiated_connection) != expected_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400868 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700869 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
870 return 0;
871 }
872
873 const uint8_t *d = CBS_data(&renegotiated_connection);
874 if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
875 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400876 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700877 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
878 return 0;
879 }
880 d += ssl->s3->previous_client_finished_len;
881
882 if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
883 ssl->s3->previous_server_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700885 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
886 return 0;
887 }
888 ssl->s3->send_connection_binding = 1;
889
890 return 1;
891}
892
893static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
894 CBS *contents) {
895 /* Renegotiation isn't supported as a server so this function should never be
896 * called after the initial handshake. */
897 assert(!ssl->s3->initial_handshake_complete);
898
Steven Valdez143e8b32016-07-11 13:19:03 -0400899 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
900 return 1;
901 }
902
Adam Langley5021b222015-06-12 18:27:58 -0700903 if (contents == NULL) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400904 return 1;
Adam Langley5021b222015-06-12 18:27:58 -0700905 }
906
907 CBS renegotiated_connection;
Adam Langley5021b222015-06-12 18:27:58 -0700908 if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
909 CBS_len(contents) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400910 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
Adam Langley5021b222015-06-12 18:27:58 -0700911 return 0;
912 }
913
914 /* Check that the extension matches */
David Benjamin0d56f882015-12-19 17:05:56 -0500915 if (!CBS_mem_equal(&renegotiated_connection,
916 ssl->s3->previous_client_finished,
Adam Langley5021b222015-06-12 18:27:58 -0700917 ssl->s3->previous_client_finished_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400918 OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
Adam Langley5021b222015-06-12 18:27:58 -0700919 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
920 return 0;
921 }
922
923 ssl->s3->send_connection_binding = 1;
924
925 return 1;
926}
927
928static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400929 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
930 return 1;
931 }
932
Adam Langley5021b222015-06-12 18:27:58 -0700933 CBB contents, prev_finished;
934 if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
935 !CBB_add_u16_length_prefixed(out, &contents) ||
936 !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
937 !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
938 ssl->s3->previous_client_finished_len) ||
939 !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
940 ssl->s3->previous_server_finished_len) ||
941 !CBB_flush(out)) {
942 return 0;
943 }
944
945 return 1;
946}
947
Adam Langley0a056712015-07-01 15:03:33 -0700948
949/* Extended Master Secret.
950 *
David Benjamin43946d42016-02-01 08:42:19 -0500951 * https://tools.ietf.org/html/rfc7627 */
Adam Langley0a056712015-07-01 15:03:33 -0700952
953static void ext_ems_init(SSL *ssl) {
954 ssl->s3->tmp.extended_master_secret = 0;
955}
956
957static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
958 if (ssl->version == SSL3_VERSION) {
959 return 1;
960 }
961
962 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
963 !CBB_add_u16(out, 0 /* length */)) {
964 return 0;
965 }
966
967 return 1;
968}
969
970static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
971 CBS *contents) {
972 if (contents == NULL) {
973 return 1;
974 }
975
Steven Valdez143e8b32016-07-11 13:19:03 -0400976 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
977 ssl->version == SSL3_VERSION) {
978 return 0;
979 }
980
981 if (CBS_len(contents) != 0) {
Adam Langley0a056712015-07-01 15:03:33 -0700982 return 0;
983 }
984
985 ssl->s3->tmp.extended_master_secret = 1;
986 return 1;
987}
988
David Benjamin0d56f882015-12-19 17:05:56 -0500989static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
990 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400991 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
992 ssl->version == SSL3_VERSION) {
993 return 1;
994 }
995
996 if (contents == NULL) {
Adam Langley0a056712015-07-01 15:03:33 -0700997 return 1;
998 }
999
1000 if (CBS_len(contents) != 0) {
1001 return 0;
1002 }
1003
1004 ssl->s3->tmp.extended_master_secret = 1;
1005 return 1;
1006}
1007
1008static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
1009 if (!ssl->s3->tmp.extended_master_secret) {
1010 return 1;
1011 }
1012
1013 if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
1014 !CBB_add_u16(out, 0 /* length */)) {
1015 return 0;
1016 }
1017
1018 return 1;
1019}
1020
Adam Langley9b05bc52015-07-01 15:25:33 -07001021
1022/* Session tickets.
1023 *
1024 * https://tools.ietf.org/html/rfc5077 */
1025
1026static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
1027 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
1028 return 1;
1029 }
1030
1031 const uint8_t *ticket_data = NULL;
1032 int ticket_len = 0;
1033
1034 /* Renegotiation does not participate in session resumption. However, still
1035 * advertise the extension to avoid potentially breaking servers which carry
1036 * over the state from the previous handshake, such as OpenSSL servers
1037 * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
1038 if (!ssl->s3->initial_handshake_complete &&
1039 ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -04001040 ssl->session->tlsext_tick != NULL &&
1041 /* Don't send TLS 1.3 session tickets in the ticket extension. */
1042 ssl->method->version_from_wire(ssl->session->ssl_version) <
1043 TLS1_3_VERSION) {
Adam Langley9b05bc52015-07-01 15:25:33 -07001044 ticket_data = ssl->session->tlsext_tick;
1045 ticket_len = ssl->session->tlsext_ticklen;
1046 }
1047
1048 CBB ticket;
1049 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1050 !CBB_add_u16_length_prefixed(out, &ticket) ||
1051 !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
1052 !CBB_flush(out)) {
1053 return 0;
1054 }
1055
1056 return 1;
1057}
1058
1059static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1060 CBS *contents) {
1061 ssl->tlsext_ticket_expected = 0;
1062
1063 if (contents == NULL) {
1064 return 1;
1065 }
1066
Steven Valdez143e8b32016-07-11 13:19:03 -04001067 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1068 return 0;
1069 }
1070
Adam Langley9b05bc52015-07-01 15:25:33 -07001071 /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
1072 * this function should never be called, even if the server tries to send the
1073 * extension. */
1074 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1075
1076 if (CBS_len(contents) != 0) {
1077 return 0;
1078 }
1079
1080 ssl->tlsext_ticket_expected = 1;
1081 return 1;
1082}
1083
Adam Langley9b05bc52015-07-01 15:25:33 -07001084static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
1085 if (!ssl->tlsext_ticket_expected) {
1086 return 1;
1087 }
1088
1089 /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
1090 * true. */
1091 assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
1092
1093 if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
1094 !CBB_add_u16(out, 0 /* length */)) {
1095 return 0;
1096 }
1097
1098 return 1;
1099}
1100
1101
Adam Langley2e857bd2015-07-01 16:09:19 -07001102/* Signature Algorithms.
1103 *
1104 * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
1105
1106static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
David Benjaminb6a0a512016-06-21 10:33:21 -04001107 if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001108 return 1;
1109 }
1110
Steven Valdez02563852016-06-23 13:33:05 -04001111 const uint16_t *sigalgs_data;
Adam Langley2e857bd2015-07-01 16:09:19 -07001112 const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data);
1113
1114 CBB contents, sigalgs;
1115 if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
1116 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdez02563852016-06-23 13:33:05 -04001117 !CBB_add_u16_length_prefixed(&contents, &sigalgs)) {
1118 return 0;
1119 }
1120
1121 size_t i;
1122 for (i = 0; i < sigalgs_len; i++) {
1123 if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) {
1124 return 0;
1125 }
1126 }
1127
1128 if (!CBB_flush(out)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001129 return 0;
1130 }
1131
1132 return 1;
1133}
1134
Adam Langley2e857bd2015-07-01 16:09:19 -07001135static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1136 CBS *contents) {
1137 OPENSSL_free(ssl->cert->peer_sigalgs);
1138 ssl->cert->peer_sigalgs = NULL;
1139 ssl->cert->peer_sigalgslen = 0;
1140
Adam Langley2e857bd2015-07-01 16:09:19 -07001141 if (contents == NULL) {
1142 return 1;
1143 }
1144
1145 CBS supported_signature_algorithms;
1146 if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
Steven Valdez0d62f262015-09-04 12:41:04 -04001147 CBS_len(contents) != 0 ||
1148 CBS_len(&supported_signature_algorithms) == 0 ||
1149 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
Adam Langley2e857bd2015-07-01 16:09:19 -07001150 return 0;
1151 }
1152
1153 return 1;
1154}
1155
Adam Langley2e857bd2015-07-01 16:09:19 -07001156
Adam Langleybb0bd042015-07-01 16:21:03 -07001157/* OCSP Stapling.
1158 *
1159 * https://tools.ietf.org/html/rfc6066#section-8 */
1160
1161static void ext_ocsp_init(SSL *ssl) {
1162 ssl->s3->tmp.certificate_status_expected = 0;
Adam Langleyce9d85e2016-01-24 15:58:39 -08001163 ssl->tlsext_status_type = -1;
Adam Langleybb0bd042015-07-01 16:21:03 -07001164}
1165
1166static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
1167 if (!ssl->ocsp_stapling_enabled) {
1168 return 1;
1169 }
1170
1171 CBB contents;
1172 if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
1173 !CBB_add_u16_length_prefixed(out, &contents) ||
1174 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
1175 !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
1176 !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
1177 !CBB_flush(out)) {
1178 return 0;
1179 }
1180
Adam Langleyce9d85e2016-01-24 15:58:39 -08001181 ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
Adam Langleybb0bd042015-07-01 16:21:03 -07001182 return 1;
1183}
1184
1185static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001186 CBS *contents) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001187 if (contents == NULL) {
1188 return 1;
1189 }
1190
David Benjamin942f4ed2016-07-16 19:03:49 +03001191 /* OCSP stapling is forbidden on a non-certificate cipher. */
1192 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleybb0bd042015-07-01 16:21:03 -07001193 return 0;
1194 }
1195
David Benjamin942f4ed2016-07-16 19:03:49 +03001196 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1197 if (CBS_len(contents) != 0) {
1198 return 0;
1199 }
1200
1201 ssl->s3->tmp.certificate_status_expected = 1;
1202 return 1;
1203 }
1204
1205 uint8_t status_type;
1206 CBS ocsp_response;
1207 if (!CBS_get_u8(contents, &status_type) ||
1208 status_type != TLSEXT_STATUSTYPE_ocsp ||
1209 !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
1210 CBS_len(&ocsp_response) == 0 ||
1211 CBS_len(contents) != 0) {
1212 return 0;
1213 }
1214
Steven Valdez87eab492016-06-27 16:34:59 -04001215 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1216 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001217 *out_alert = SSL_AD_INTERNAL_ERROR;
1218 return 0;
1219 }
1220
Adam Langleybb0bd042015-07-01 16:21:03 -07001221 return 1;
1222}
1223
1224static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1225 CBS *contents) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001226 if (contents == NULL) {
1227 return 1;
1228 }
1229
1230 uint8_t status_type;
1231 if (!CBS_get_u8(contents, &status_type)) {
1232 return 0;
1233 }
1234
1235 /* We cannot decide whether OCSP stapling will occur yet because the correct
1236 * SSL_CTX might not have been selected. */
1237 ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
1238
Adam Langleybb0bd042015-07-01 16:21:03 -07001239 return 1;
1240}
1241
1242static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001243 if (!ssl->s3->tmp.ocsp_stapling_requested ||
1244 ssl->ctx->ocsp_response_length == 0 ||
1245 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001246 return 1;
1247 }
1248
David Benjamin942f4ed2016-07-16 19:03:49 +03001249 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
1250 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez87eab492016-06-27 16:34:59 -04001251 if (ssl->session != NULL) {
David Benjamin942f4ed2016-07-16 19:03:49 +03001252 return 1;
1253 }
1254
1255 ssl->s3->tmp.certificate_status_expected = 1;
1256
1257 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
1258 CBB_add_u16(out, 0 /* length */);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001259 }
1260
David Benjamin942f4ed2016-07-16 19:03:49 +03001261 CBB body, ocsp_response;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001262 return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
David Benjamin942f4ed2016-07-16 19:03:49 +03001263 CBB_add_u16_length_prefixed(out, &body) &&
1264 CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
1265 CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
1266 CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1267 ssl->ctx->ocsp_response_length) &&
1268 CBB_flush(out);
Adam Langleybb0bd042015-07-01 16:21:03 -07001269}
1270
1271
Adam Langley97dfcbf2015-07-01 18:35:20 -07001272/* Next protocol negotiation.
1273 *
1274 * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
1275
1276static void ext_npn_init(SSL *ssl) {
1277 ssl->s3->next_proto_neg_seen = 0;
1278}
1279
1280static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
1281 if (ssl->s3->initial_handshake_complete ||
1282 ssl->ctx->next_proto_select_cb == NULL ||
David Benjamin091c4b92015-10-26 13:33:21 -04001283 (ssl->options & SSL_OP_DISABLE_NPN) ||
David Benjamince079fd2016-08-02 16:22:34 -04001284 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001285 return 1;
1286 }
1287
1288 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1289 !CBB_add_u16(out, 0 /* length */)) {
1290 return 0;
1291 }
1292
1293 return 1;
1294}
1295
1296static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1297 CBS *contents) {
1298 if (contents == NULL) {
1299 return 1;
1300 }
1301
Steven Valdez143e8b32016-07-11 13:19:03 -04001302 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1303 return 0;
1304 }
1305
Adam Langley97dfcbf2015-07-01 18:35:20 -07001306 /* If any of these are false then we should never have sent the NPN
1307 * extension in the ClientHello and thus this function should never have been
1308 * called. */
1309 assert(!ssl->s3->initial_handshake_complete);
David Benjamince079fd2016-08-02 16:22:34 -04001310 assert(!SSL_is_dtls(ssl));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001311 assert(ssl->ctx->next_proto_select_cb != NULL);
David Benjamin091c4b92015-10-26 13:33:21 -04001312 assert(!(ssl->options & SSL_OP_DISABLE_NPN));
Adam Langley97dfcbf2015-07-01 18:35:20 -07001313
David Benjamin76c2efc2015-08-31 14:24:29 -04001314 if (ssl->s3->alpn_selected != NULL) {
1315 /* NPN and ALPN may not be negotiated in the same connection. */
1316 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1317 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1318 return 0;
1319 }
1320
Adam Langley97dfcbf2015-07-01 18:35:20 -07001321 const uint8_t *const orig_contents = CBS_data(contents);
1322 const size_t orig_len = CBS_len(contents);
1323
1324 while (CBS_len(contents) != 0) {
1325 CBS proto;
1326 if (!CBS_get_u8_length_prefixed(contents, &proto) ||
1327 CBS_len(&proto) == 0) {
1328 return 0;
1329 }
1330 }
1331
1332 uint8_t *selected;
1333 uint8_t selected_len;
1334 if (ssl->ctx->next_proto_select_cb(
1335 ssl, &selected, &selected_len, orig_contents, orig_len,
1336 ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1337 *out_alert = SSL_AD_INTERNAL_ERROR;
1338 return 0;
1339 }
1340
David Benjamin79978df2015-12-25 15:56:49 -05001341 OPENSSL_free(ssl->s3->next_proto_negotiated);
1342 ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
1343 if (ssl->s3->next_proto_negotiated == NULL) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001344 *out_alert = SSL_AD_INTERNAL_ERROR;
1345 return 0;
1346 }
1347
David Benjamin79978df2015-12-25 15:56:49 -05001348 ssl->s3->next_proto_negotiated_len = selected_len;
Adam Langley97dfcbf2015-07-01 18:35:20 -07001349 ssl->s3->next_proto_neg_seen = 1;
1350
1351 return 1;
1352}
1353
1354static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1355 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001356 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1357 return 1;
1358 }
1359
Adam Langley97dfcbf2015-07-01 18:35:20 -07001360 if (contents != NULL && CBS_len(contents) != 0) {
1361 return 0;
1362 }
1363
1364 if (contents == NULL ||
1365 ssl->s3->initial_handshake_complete ||
1366 /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
1367 * afterwards, parsing the ALPN extension will clear
1368 * |next_proto_neg_seen|. */
1369 ssl->s3->alpn_selected != NULL ||
1370 ssl->ctx->next_protos_advertised_cb == NULL ||
David Benjamince079fd2016-08-02 16:22:34 -04001371 SSL_is_dtls(ssl)) {
Adam Langley97dfcbf2015-07-01 18:35:20 -07001372 return 1;
1373 }
1374
1375 ssl->s3->next_proto_neg_seen = 1;
1376 return 1;
1377}
1378
1379static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
1380 /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
1381 * parsed. */
1382 if (!ssl->s3->next_proto_neg_seen) {
1383 return 1;
1384 }
1385
1386 const uint8_t *npa;
1387 unsigned npa_len;
1388
1389 if (ssl->ctx->next_protos_advertised_cb(
1390 ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
1391 SSL_TLSEXT_ERR_OK) {
1392 ssl->s3->next_proto_neg_seen = 0;
1393 return 1;
1394 }
1395
1396 CBB contents;
1397 if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
1398 !CBB_add_u16_length_prefixed(out, &contents) ||
1399 !CBB_add_bytes(&contents, npa, npa_len) ||
1400 !CBB_flush(out)) {
1401 return 0;
1402 }
1403
1404 return 1;
1405}
1406
1407
Adam Langleyab8d87d2015-07-10 12:21:39 -07001408/* Signed certificate timestamps.
1409 *
1410 * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
1411
1412static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
1413 if (!ssl->signed_cert_timestamps_enabled) {
1414 return 1;
1415 }
1416
1417 if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
1418 !CBB_add_u16(out, 0 /* length */)) {
1419 return 0;
1420 }
1421
1422 return 1;
1423}
1424
1425static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1426 CBS *contents) {
1427 if (contents == NULL) {
1428 return 1;
1429 }
1430
1431 /* If this is false then we should never have sent the SCT extension in the
1432 * ClientHello and thus this function should never have been called. */
1433 assert(ssl->signed_cert_timestamps_enabled);
1434
1435 if (CBS_len(contents) == 0) {
1436 *out_alert = SSL_AD_DECODE_ERROR;
1437 return 0;
1438 }
1439
1440 /* Session resumption uses the original session information. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001441 if (!ssl->s3->session_reused &&
Steven Valdez87eab492016-06-27 16:34:59 -04001442 !CBS_stow(
1443 contents,
1444 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
1445 &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
Adam Langleyab8d87d2015-07-10 12:21:39 -07001446 *out_alert = SSL_AD_INTERNAL_ERROR;
1447 return 0;
1448 }
1449
1450 return 1;
1451}
1452
1453static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1454 CBS *contents) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001455 return contents == NULL || CBS_len(contents) == 0;
Adam Langleyab8d87d2015-07-10 12:21:39 -07001456}
1457
1458static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
Paul Lietar62be8ac2015-09-16 10:03:30 +01001459 /* The extension shouldn't be sent when resuming sessions. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001460 if (ssl->s3->session_reused ||
Paul Lietar62be8ac2015-09-16 10:03:30 +01001461 ssl->ctx->signed_cert_timestamp_list_length == 0) {
Paul Lietar4fac72e2015-09-09 13:44:55 +01001462 return 1;
1463 }
1464
1465 CBB contents;
1466 return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
1467 CBB_add_u16_length_prefixed(out, &contents) &&
1468 CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
1469 ssl->ctx->signed_cert_timestamp_list_length) &&
1470 CBB_flush(out);
Adam Langleyab8d87d2015-07-10 12:21:39 -07001471}
1472
1473
Adam Langleyf18e4532015-07-10 13:39:53 -07001474/* Application-level Protocol Negotiation.
1475 *
1476 * https://tools.ietf.org/html/rfc7301 */
1477
1478static void ext_alpn_init(SSL *ssl) {
1479 OPENSSL_free(ssl->s3->alpn_selected);
1480 ssl->s3->alpn_selected = NULL;
1481}
1482
1483static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
1484 if (ssl->alpn_client_proto_list == NULL ||
1485 ssl->s3->initial_handshake_complete) {
1486 return 1;
1487 }
1488
1489 CBB contents, proto_list;
1490 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1491 !CBB_add_u16_length_prefixed(out, &contents) ||
1492 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1493 !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
1494 ssl->alpn_client_proto_list_len) ||
1495 !CBB_flush(out)) {
1496 return 0;
1497 }
1498
1499 return 1;
1500}
1501
1502static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1503 CBS *contents) {
1504 if (contents == NULL) {
1505 return 1;
1506 }
1507
1508 assert(!ssl->s3->initial_handshake_complete);
1509 assert(ssl->alpn_client_proto_list != NULL);
1510
David Benjamin76c2efc2015-08-31 14:24:29 -04001511 if (ssl->s3->next_proto_neg_seen) {
1512 /* NPN and ALPN may not be negotiated in the same connection. */
1513 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1514 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
1515 return 0;
1516 }
1517
Adam Langleyf18e4532015-07-10 13:39:53 -07001518 /* The extension data consists of a ProtocolNameList which must have
1519 * exactly one ProtocolName. Each of these is length-prefixed. */
1520 CBS protocol_name_list, protocol_name;
1521 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1522 CBS_len(contents) != 0 ||
1523 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1524 /* Empty protocol names are forbidden. */
1525 CBS_len(&protocol_name) == 0 ||
1526 CBS_len(&protocol_name_list) != 0) {
1527 return 0;
1528 }
1529
David Benjamin3e517572016-08-11 11:52:23 -04001530 /* Check that the protcol name is one of the ones we advertised. */
1531 int protocol_ok = 0;
1532 CBS client_protocol_name_list, client_protocol_name;
1533 CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
1534 ssl->alpn_client_proto_list_len);
1535 while (CBS_len(&client_protocol_name_list) > 0) {
1536 if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
1537 &client_protocol_name)) {
1538 *out_alert = SSL_AD_INTERNAL_ERROR;
1539 return 0;
1540 }
1541
1542 if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
1543 memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
1544 CBS_len(&protocol_name)) == 0) {
1545 protocol_ok = 1;
1546 break;
1547 }
1548 }
1549
1550 if (!protocol_ok) {
1551 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
1552 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1553 return 0;
1554 }
1555
Adam Langleyf18e4532015-07-10 13:39:53 -07001556 if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
1557 &ssl->s3->alpn_selected_len)) {
1558 *out_alert = SSL_AD_INTERNAL_ERROR;
1559 return 0;
1560 }
1561
1562 return 1;
1563}
1564
1565static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1566 CBS *contents) {
1567 if (contents == NULL) {
1568 return 1;
1569 }
1570
1571 if (ssl->ctx->alpn_select_cb == NULL ||
1572 ssl->s3->initial_handshake_complete) {
1573 return 1;
1574 }
1575
1576 /* ALPN takes precedence over NPN. */
1577 ssl->s3->next_proto_neg_seen = 0;
1578
1579 CBS protocol_name_list;
1580 if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
1581 CBS_len(contents) != 0 ||
1582 CBS_len(&protocol_name_list) < 2) {
1583 return 0;
1584 }
1585
1586 /* Validate the protocol list. */
1587 CBS protocol_name_list_copy = protocol_name_list;
1588 while (CBS_len(&protocol_name_list_copy) > 0) {
1589 CBS protocol_name;
1590
1591 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
1592 /* Empty protocol names are forbidden. */
1593 CBS_len(&protocol_name) == 0) {
1594 return 0;
1595 }
1596 }
1597
1598 const uint8_t *selected;
1599 uint8_t selected_len;
1600 if (ssl->ctx->alpn_select_cb(
1601 ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
1602 CBS_len(&protocol_name_list),
1603 ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
1604 OPENSSL_free(ssl->s3->alpn_selected);
1605 ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
1606 if (ssl->s3->alpn_selected == NULL) {
1607 *out_alert = SSL_AD_INTERNAL_ERROR;
1608 return 0;
1609 }
1610 ssl->s3->alpn_selected_len = selected_len;
1611 }
1612
1613 return 1;
1614}
1615
1616static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
1617 if (ssl->s3->alpn_selected == NULL) {
1618 return 1;
1619 }
1620
1621 CBB contents, proto_list, proto;
1622 if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
1623 !CBB_add_u16_length_prefixed(out, &contents) ||
1624 !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
1625 !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001626 !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
1627 ssl->s3->alpn_selected_len) ||
Adam Langleyf18e4532015-07-10 13:39:53 -07001628 !CBB_flush(out)) {
1629 return 0;
1630 }
1631
1632 return 1;
1633}
1634
1635
Adam Langley49c7af12015-07-10 14:33:46 -07001636/* Channel ID.
1637 *
1638 * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
1639
1640static void ext_channel_id_init(SSL *ssl) {
1641 ssl->s3->tlsext_channel_id_valid = 0;
1642}
1643
1644static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
1645 if (!ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001646 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001647 return 1;
1648 }
1649
1650 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1651 !CBB_add_u16(out, 0 /* length */)) {
1652 return 0;
1653 }
1654
1655 return 1;
1656}
1657
1658static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1659 CBS *contents) {
1660 if (contents == NULL) {
1661 return 1;
1662 }
1663
Steven Valdez143e8b32016-07-11 13:19:03 -04001664 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1665 return 0;
1666 }
1667
David Benjamince079fd2016-08-02 16:22:34 -04001668 assert(!SSL_is_dtls(ssl));
Adam Langley49c7af12015-07-10 14:33:46 -07001669 assert(ssl->tlsext_channel_id_enabled);
1670
1671 if (CBS_len(contents) != 0) {
1672 return 0;
1673 }
1674
1675 ssl->s3->tlsext_channel_id_valid = 1;
1676 return 1;
1677}
1678
1679static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1680 CBS *contents) {
1681 if (contents == NULL ||
1682 !ssl->tlsext_channel_id_enabled ||
David Benjamince079fd2016-08-02 16:22:34 -04001683 SSL_is_dtls(ssl)) {
Adam Langley49c7af12015-07-10 14:33:46 -07001684 return 1;
1685 }
1686
1687 if (CBS_len(contents) != 0) {
1688 return 0;
1689 }
1690
1691 ssl->s3->tlsext_channel_id_valid = 1;
1692 return 1;
1693}
1694
1695static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001696 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1697 return 1;
1698 }
1699
Adam Langley49c7af12015-07-10 14:33:46 -07001700 if (!ssl->s3->tlsext_channel_id_valid) {
1701 return 1;
1702 }
1703
1704 if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
1705 !CBB_add_u16(out, 0 /* length */)) {
1706 return 0;
1707 }
1708
1709 return 1;
1710}
1711
Adam Langley391250d2015-07-15 19:06:07 -07001712
1713/* Secure Real-time Transport Protocol (SRTP) extension.
1714 *
1715 * https://tools.ietf.org/html/rfc5764 */
1716
Adam Langley391250d2015-07-15 19:06:07 -07001717
1718static void ext_srtp_init(SSL *ssl) {
1719 ssl->srtp_profile = NULL;
1720}
1721
1722static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
1723 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1724 if (profiles == NULL) {
1725 return 1;
1726 }
1727 const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
1728 if (num_profiles == 0) {
1729 return 1;
1730 }
1731
1732 CBB contents, profile_ids;
1733 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1734 !CBB_add_u16_length_prefixed(out, &contents) ||
1735 !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
1736 return 0;
1737 }
1738
1739 size_t i;
1740 for (i = 0; i < num_profiles; i++) {
1741 if (!CBB_add_u16(&profile_ids,
1742 sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
1743 return 0;
1744 }
1745 }
1746
1747 if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
1748 !CBB_flush(out)) {
1749 return 0;
1750 }
1751
1752 return 1;
1753}
1754
1755static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1756 CBS *contents) {
1757 if (contents == NULL) {
1758 return 1;
1759 }
1760
1761 /* The extension consists of a u16-prefixed profile ID list containing a
1762 * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
1763 *
1764 * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
1765 CBS profile_ids, srtp_mki;
1766 uint16_t profile_id;
1767 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1768 !CBS_get_u16(&profile_ids, &profile_id) ||
1769 CBS_len(&profile_ids) != 0 ||
1770 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1771 CBS_len(contents) != 0) {
1772 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1773 return 0;
1774 }
1775
1776 if (CBS_len(&srtp_mki) != 0) {
1777 /* Must be no MKI, since we never offer one. */
1778 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
1779 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1780 return 0;
1781 }
1782
1783 STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
1784
1785 /* Check to see if the server gave us something we support (and presumably
1786 * offered). */
1787 size_t i;
1788 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
1789 const SRTP_PROTECTION_PROFILE *profile =
1790 sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
1791
1792 if (profile->id == profile_id) {
1793 ssl->srtp_profile = profile;
1794 return 1;
1795 }
1796 }
1797
1798 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1799 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1800 return 0;
1801}
1802
1803static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1804 CBS *contents) {
1805 if (contents == NULL) {
1806 return 1;
1807 }
1808
1809 CBS profile_ids, srtp_mki;
1810 if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
1811 CBS_len(&profile_ids) < 2 ||
1812 !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
1813 CBS_len(contents) != 0) {
1814 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1815 return 0;
1816 }
1817 /* Discard the MKI value for now. */
1818
1819 const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
1820 SSL_get_srtp_profiles(ssl);
1821
1822 /* Pick the server's most preferred profile. */
1823 size_t i;
1824 for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
1825 const SRTP_PROTECTION_PROFILE *server_profile =
1826 sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
1827
1828 CBS profile_ids_tmp;
1829 CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
1830
1831 while (CBS_len(&profile_ids_tmp) > 0) {
1832 uint16_t profile_id;
1833 if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
1834 return 0;
1835 }
1836
1837 if (server_profile->id == profile_id) {
1838 ssl->srtp_profile = server_profile;
1839 return 1;
1840 }
1841 }
1842 }
1843
1844 return 1;
1845}
1846
1847static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
1848 if (ssl->srtp_profile == NULL) {
1849 return 1;
1850 }
1851
1852 CBB contents, profile_ids;
1853 if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
1854 !CBB_add_u16_length_prefixed(out, &contents) ||
1855 !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
1856 !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
1857 !CBB_add_u8(&contents, 0 /* empty MKI */) ||
1858 !CBB_flush(out)) {
1859 return 0;
1860 }
1861
1862 return 1;
1863}
1864
Adam Langleybdd5d662015-07-20 16:19:08 -07001865
1866/* EC point formats.
1867 *
1868 * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
1869
1870static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
David Benjamince079fd2016-08-02 16:22:34 -04001871 if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
Adam Langleybdd5d662015-07-20 16:19:08 -07001872 return 0;
1873 }
1874
1875 const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
1876
1877 size_t i;
1878 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1879 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
1880
1881 const uint32_t alg_k = cipher->algorithm_mkey;
1882 const uint32_t alg_a = cipher->algorithm_auth;
1883 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
1884 return 1;
1885 }
1886 }
1887
1888 return 0;
1889}
1890
Adam Langleybdd5d662015-07-20 16:19:08 -07001891static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
David Benjaminfc059942015-07-30 23:01:59 -04001892 CBB contents, formats;
Adam Langleybdd5d662015-07-20 16:19:08 -07001893 if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
1894 !CBB_add_u16_length_prefixed(out, &contents) ||
David Benjaminfc059942015-07-30 23:01:59 -04001895 !CBB_add_u8_length_prefixed(&contents, &formats) ||
1896 !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
Adam Langleybdd5d662015-07-20 16:19:08 -07001897 !CBB_flush(out)) {
1898 return 0;
1899 }
1900
1901 return 1;
1902}
1903
1904static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
1905 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
1906 return 1;
1907 }
1908
1909 return ext_ec_point_add_extension(ssl, out);
1910}
1911
1912static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
1913 CBS *contents) {
1914 if (contents == NULL) {
1915 return 1;
1916 }
1917
Steven Valdez143e8b32016-07-11 13:19:03 -04001918 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1919 return 0;
1920 }
1921
Adam Langleybdd5d662015-07-20 16:19:08 -07001922 CBS ec_point_format_list;
1923 if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
1924 CBS_len(contents) != 0) {
1925 return 0;
1926 }
1927
David Benjaminfc059942015-07-30 23:01:59 -04001928 /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
1929 * point format. */
1930 if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
1931 CBS_len(&ec_point_format_list)) == NULL) {
1932 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langleybdd5d662015-07-20 16:19:08 -07001933 return 0;
1934 }
1935
1936 return 1;
1937}
1938
1939static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
1940 CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001941 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1942 return 1;
1943 }
1944
Adam Langleybdd5d662015-07-20 16:19:08 -07001945 return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
1946}
1947
1948static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001949 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
1950 return 1;
1951 }
1952
Adam Langleybdd5d662015-07-20 16:19:08 -07001953 const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1954 const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
David Benjaminfc059942015-07-30 23:01:59 -04001955 const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleybdd5d662015-07-20 16:19:08 -07001956
1957 if (!using_ecc) {
1958 return 1;
1959 }
1960
1961 return ext_ec_point_add_extension(ssl, out);
1962}
1963
Adam Langley273d49c2015-07-20 16:38:52 -07001964
Steven Valdez143e8b32016-07-11 13:19:03 -04001965/* Draft Version Extension */
1966
1967static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
1968 uint16_t min_version, max_version;
1969 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
Steven Valdez3a287552016-07-19 05:10:50 -04001970 max_version < TLS1_3_VERSION) {
Steven Valdez143e8b32016-07-11 13:19:03 -04001971 return 1;
1972 }
1973
1974 CBB contents;
1975 if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
1976 !CBB_add_u16_length_prefixed(out, &contents) ||
1977 !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
1978 return 0;
1979 }
1980
1981 return CBB_flush(out);
1982}
1983
1984
Steven Valdez4aa154e2016-07-29 14:32:55 -04001985/* Pre Shared Key
1986 *
1987 * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
1988
1989static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
1990 uint16_t min_version, max_version;
1991 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
1992 return 0;
1993 }
1994
1995 if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
1996 ssl->method->version_from_wire(ssl->session->ssl_version) <
1997 TLS1_3_VERSION) {
1998 return 1;
1999 }
2000
2001 CBB contents, identities, identity;
2002 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2003 !CBB_add_u16_length_prefixed(out, &contents) ||
2004 !CBB_add_u16_length_prefixed(&contents, &identities) ||
2005 !CBB_add_u16_length_prefixed(&identities, &identity) ||
2006 !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
2007 ssl->session->tlsext_ticklen)) {
2008 return 0;
2009 }
2010
2011 return CBB_flush(out);
2012}
2013
2014int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2015 CBS *contents) {
2016 uint16_t psk_id;
2017 if (!CBS_get_u16(contents, &psk_id) ||
2018 CBS_len(contents) != 0) {
2019 *out_alert = SSL_AD_DECODE_ERROR;
2020 return 0;
2021 }
2022
2023 if (psk_id != 0) {
2024 *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
2025 return 0;
2026 }
2027
2028 return 1;
2029}
2030
2031int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
2032 SSL_SESSION **out_session,
2033 uint8_t *out_alert,
2034 CBS *contents) {
2035 CBS identities, identity;
2036 if (!CBS_get_u16_length_prefixed(contents, &identities) ||
2037 !CBS_get_u16_length_prefixed(&identities, &identity) ||
2038 CBS_len(contents) != 0) {
2039 *out_alert = SSL_AD_DECODE_ERROR;
2040 return 0;
2041 }
2042
2043 /* TLS 1.3 session tickets are renewed separately as part of the
2044 * NewSessionTicket. */
2045 int renew;
2046 return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
2047 CBS_len(&identity), NULL, 0);
2048}
2049
2050int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
2051 if (!ssl->s3->session_reused) {
2052 return 1;
2053 }
2054
2055 CBB contents;
2056 if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
2057 !CBB_add_u16_length_prefixed(out, &contents) ||
2058 /* We only consider the first identity for resumption */
2059 !CBB_add_u16(&contents, 0) ||
2060 !CBB_flush(out)) {
2061 return 0;
2062 }
2063
2064 return 1;
2065}
2066
2067
Steven Valdez143e8b32016-07-11 13:19:03 -04002068/* Key Share
2069 *
2070 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
2071
2072static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
2073 uint16_t min_version, max_version;
2074 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
2075 return 0;
2076 }
2077
2078 if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
2079 return 1;
2080 }
2081
2082 CBB contents, kse_bytes;
2083 if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2084 !CBB_add_u16_length_prefixed(out, &contents) ||
2085 !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
2086 return 0;
2087 }
2088
2089 const uint16_t *groups;
2090 size_t groups_len;
Steven Valdez5440fe02016-07-18 12:40:30 -04002091 if (ssl->s3->hs->retry_group) {
2092 /* Append the new key share to the old list. */
2093 if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
2094 ssl->s3->hs->key_share_bytes_len)) {
2095 return 0;
2096 }
2097 OPENSSL_free(ssl->s3->hs->key_share_bytes);
2098 ssl->s3->hs->key_share_bytes = NULL;
2099
2100 groups = &ssl->s3->hs->retry_group;
2101 groups_len = 1;
2102 } else {
2103 tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
2104 /* Only send the top two preferred key shares. */
2105 if (groups_len > 2) {
2106 groups_len = 2;
2107 }
2108 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002109
2110 ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
2111 if (ssl->s3->hs->groups == NULL) {
2112 return 0;
2113 }
2114 memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
2115 ssl->s3->hs->groups_len = groups_len;
2116
2117 for (size_t i = 0; i < groups_len; i++) {
2118 if (!CBB_add_u16(&kse_bytes, groups[i])) {
2119 return 0;
2120 }
2121
2122 CBB key_exchange;
2123 if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
2124 !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
2125 !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
2126 !CBB_flush(&kse_bytes)) {
2127 return 0;
2128 }
2129 }
2130
Steven Valdez5440fe02016-07-18 12:40:30 -04002131 if (!ssl->s3->hs->retry_group) {
2132 /* Save the contents of the extension to repeat it in the second
2133 * ClientHello. */
2134 ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
2135 ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
2136 CBB_len(&kse_bytes));
2137 if (ssl->s3->hs->key_share_bytes == NULL) {
2138 return 0;
2139 }
2140 }
2141
Steven Valdez143e8b32016-07-11 13:19:03 -04002142 return CBB_flush(out);
2143}
2144
Steven Valdez7259f2f2016-08-02 16:55:05 -04002145int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
2146 size_t *out_secret_len,
2147 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002148 CBS peer_key;
2149 uint16_t group;
2150 if (!CBS_get_u16(contents, &group) ||
David Benjamina70de142016-08-02 16:52:57 -04002151 !CBS_get_u16_length_prefixed(contents, &peer_key) ||
2152 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002153 *out_alert = SSL_AD_DECODE_ERROR;
2154 return 0;
2155 }
2156
2157 SSL_ECDH_CTX *group_ctx = NULL;
2158 for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
2159 if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group) {
2160 group_ctx = &ssl->s3->hs->groups[i];
2161 break;
2162 }
2163 }
2164
2165 if (group_ctx == NULL) {
2166 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
2167 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
2168 return 0;
2169 }
2170
2171 if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
2172 CBS_data(&peer_key), CBS_len(&peer_key))) {
2173 *out_alert = SSL_AD_INTERNAL_ERROR;
2174 return 0;
2175 }
2176
Steven Valdez5440fe02016-07-18 12:40:30 -04002177 ssl_handshake_clear_groups(ssl->s3->hs);
Steven Valdez143e8b32016-07-11 13:19:03 -04002178 return 1;
2179}
2180
Steven Valdez7259f2f2016-08-02 16:55:05 -04002181int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
2182 uint8_t **out_secret,
2183 size_t *out_secret_len,
2184 uint8_t *out_alert, CBS *contents) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002185 uint16_t group_id;
2186 CBS key_shares;
2187 if (!tls1_get_shared_group(ssl, &group_id) ||
David Benjamina70de142016-08-02 16:52:57 -04002188 !CBS_get_u16_length_prefixed(contents, &key_shares) ||
2189 CBS_len(contents) != 0) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002190 return 0;
2191 }
2192
Steven Valdez5440fe02016-07-18 12:40:30 -04002193 *out_found = 0;
Steven Valdez143e8b32016-07-11 13:19:03 -04002194 while (CBS_len(&key_shares) > 0) {
2195 uint16_t id;
2196 CBS peer_key;
2197 if (!CBS_get_u16(&key_shares, &id) ||
2198 !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
2199 return 0;
2200 }
2201
Steven Valdez5440fe02016-07-18 12:40:30 -04002202 if (id != group_id || *out_found) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002203 continue;
2204 }
2205
2206 SSL_ECDH_CTX group;
2207 memset(&group, 0, sizeof(SSL_ECDH_CTX));
2208 CBB public_key;
2209 if (!CBB_init(&public_key, 0) ||
2210 !SSL_ECDH_CTX_init(&group, group_id) ||
2211 !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
2212 out_alert, CBS_data(&peer_key),
2213 CBS_len(&peer_key)) ||
2214 !CBB_finish(&public_key, &ssl->s3->hs->public_key,
2215 &ssl->s3->hs->public_key_len)) {
2216 SSL_ECDH_CTX_cleanup(&group);
2217 CBB_cleanup(&public_key);
2218 return 0;
2219 }
2220 SSL_ECDH_CTX_cleanup(&group);
2221
Steven Valdez5440fe02016-07-18 12:40:30 -04002222 *out_found = 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002223 }
2224
Steven Valdez5440fe02016-07-18 12:40:30 -04002225 return 1;
Steven Valdez143e8b32016-07-11 13:19:03 -04002226}
2227
Steven Valdez7259f2f2016-08-02 16:55:05 -04002228int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002229 if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
2230 return 1;
2231 }
2232
2233 uint16_t group_id;
2234 CBB kse_bytes, public_key;
2235 if (!tls1_get_shared_group(ssl, &group_id) ||
2236 !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
2237 !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
2238 !CBB_add_u16(&kse_bytes, group_id) ||
2239 !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
2240 !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
2241 ssl->s3->hs->public_key_len) ||
2242 !CBB_flush(out)) {
2243 return 0;
2244 }
2245
2246 return 1;
2247}
2248
2249
Steven Valdezce902a92016-05-17 11:47:53 -04002250/* Negotiated Groups
Adam Langley273d49c2015-07-20 16:38:52 -07002251 *
Steven Valdezce902a92016-05-17 11:47:53 -04002252 * https://tools.ietf.org/html/rfc4492#section-5.1.2
2253 * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
Adam Langley273d49c2015-07-20 16:38:52 -07002254
Steven Valdezce902a92016-05-17 11:47:53 -04002255static void ext_supported_groups_init(SSL *ssl) {
2256 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2257 ssl->s3->tmp.peer_supported_group_list = NULL;
2258 ssl->s3->tmp.peer_supported_group_list_len = 0;
Adam Langley273d49c2015-07-20 16:38:52 -07002259}
2260
Steven Valdezce902a92016-05-17 11:47:53 -04002261static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002262 if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
2263 return 1;
2264 }
2265
Steven Valdezce902a92016-05-17 11:47:53 -04002266 CBB contents, groups_bytes;
2267 if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
Adam Langley273d49c2015-07-20 16:38:52 -07002268 !CBB_add_u16_length_prefixed(out, &contents) ||
Steven Valdezce902a92016-05-17 11:47:53 -04002269 !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
Adam Langley273d49c2015-07-20 16:38:52 -07002270 return 0;
2271 }
2272
Steven Valdezce902a92016-05-17 11:47:53 -04002273 const uint16_t *groups;
2274 size_t groups_len;
2275 tls1_get_grouplist(ssl, 0, &groups, &groups_len);
Adam Langley273d49c2015-07-20 16:38:52 -07002276
2277 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002278 for (i = 0; i < groups_len; i++) {
2279 if (!CBB_add_u16(&groups_bytes, groups[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002280 return 0;
2281 }
2282 }
2283
2284 return CBB_flush(out);
2285}
2286
Steven Valdezce902a92016-05-17 11:47:53 -04002287static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
2288 CBS *contents) {
David Benjamin4ac2dc42016-08-12 15:50:48 -04002289 /* This extension is not expected to be echoed by servers in TLS 1.2, but some
2290 * BigIP servers send it nonetheless, so do not enforce this. */
Adam Langley273d49c2015-07-20 16:38:52 -07002291 return 1;
2292}
2293
Steven Valdezce902a92016-05-17 11:47:53 -04002294static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
2295 CBS *contents) {
Adam Langley273d49c2015-07-20 16:38:52 -07002296 if (contents == NULL) {
2297 return 1;
2298 }
2299
Steven Valdezce902a92016-05-17 11:47:53 -04002300 CBS supported_group_list;
2301 if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
2302 CBS_len(&supported_group_list) == 0 ||
2303 (CBS_len(&supported_group_list) & 1) != 0 ||
Adam Langley273d49c2015-07-20 16:38:52 -07002304 CBS_len(contents) != 0) {
2305 return 0;
2306 }
2307
Steven Valdezce902a92016-05-17 11:47:53 -04002308 ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
2309 CBS_len(&supported_group_list));
2310 if (ssl->s3->tmp.peer_supported_group_list == NULL) {
Adam Langley273d49c2015-07-20 16:38:52 -07002311 *out_alert = SSL_AD_INTERNAL_ERROR;
2312 return 0;
2313 }
2314
Steven Valdezce902a92016-05-17 11:47:53 -04002315 const size_t num_groups = CBS_len(&supported_group_list) / 2;
Adam Langley273d49c2015-07-20 16:38:52 -07002316 size_t i;
Steven Valdezce902a92016-05-17 11:47:53 -04002317 for (i = 0; i < num_groups; i++) {
2318 if (!CBS_get_u16(&supported_group_list,
2319 &ssl->s3->tmp.peer_supported_group_list[i])) {
Adam Langley273d49c2015-07-20 16:38:52 -07002320 goto err;
2321 }
2322 }
2323
Steven Valdezce902a92016-05-17 11:47:53 -04002324 assert(CBS_len(&supported_group_list) == 0);
2325 ssl->s3->tmp.peer_supported_group_list_len = num_groups;
Adam Langley273d49c2015-07-20 16:38:52 -07002326
2327 return 1;
2328
2329err:
Steven Valdezce902a92016-05-17 11:47:53 -04002330 OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
2331 ssl->s3->tmp.peer_supported_group_list = NULL;
Adam Langley273d49c2015-07-20 16:38:52 -07002332 *out_alert = SSL_AD_INTERNAL_ERROR;
2333 return 0;
2334}
2335
Steven Valdezce902a92016-05-17 11:47:53 -04002336static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
Adam Langley273d49c2015-07-20 16:38:52 -07002337 /* Servers don't echo this extension. */
2338 return 1;
2339}
2340
2341
Adam Langley614c66a2015-06-12 15:26:58 -07002342/* kExtensions contains all the supported extensions. */
2343static const struct tls_extension kExtensions[] = {
2344 {
Adam Langley5021b222015-06-12 18:27:58 -07002345 TLSEXT_TYPE_renegotiate,
2346 NULL,
2347 ext_ri_add_clienthello,
2348 ext_ri_parse_serverhello,
2349 ext_ri_parse_clienthello,
2350 ext_ri_add_serverhello,
2351 },
2352 {
Adam Langley614c66a2015-06-12 15:26:58 -07002353 TLSEXT_TYPE_server_name,
2354 ext_sni_init,
2355 ext_sni_add_clienthello,
2356 ext_sni_parse_serverhello,
2357 ext_sni_parse_clienthello,
2358 ext_sni_add_serverhello,
2359 },
Adam Langley0a056712015-07-01 15:03:33 -07002360 {
2361 TLSEXT_TYPE_extended_master_secret,
2362 ext_ems_init,
2363 ext_ems_add_clienthello,
2364 ext_ems_parse_serverhello,
2365 ext_ems_parse_clienthello,
2366 ext_ems_add_serverhello,
2367 },
Adam Langley9b05bc52015-07-01 15:25:33 -07002368 {
2369 TLSEXT_TYPE_session_ticket,
2370 NULL,
2371 ext_ticket_add_clienthello,
2372 ext_ticket_parse_serverhello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002373 /* Ticket extension client parsing is handled in ssl_session.c */
2374 ignore_parse_clienthello,
Adam Langley9b05bc52015-07-01 15:25:33 -07002375 ext_ticket_add_serverhello,
2376 },
Adam Langley2e857bd2015-07-01 16:09:19 -07002377 {
2378 TLSEXT_TYPE_signature_algorithms,
2379 NULL,
2380 ext_sigalgs_add_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002381 forbid_parse_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002382 ext_sigalgs_parse_clienthello,
Steven Valdez6b8509a2016-07-12 13:38:32 -04002383 dont_add_serverhello,
Adam Langley2e857bd2015-07-01 16:09:19 -07002384 },
Adam Langleybb0bd042015-07-01 16:21:03 -07002385 {
2386 TLSEXT_TYPE_status_request,
2387 ext_ocsp_init,
2388 ext_ocsp_add_clienthello,
2389 ext_ocsp_parse_serverhello,
2390 ext_ocsp_parse_clienthello,
2391 ext_ocsp_add_serverhello,
2392 },
Adam Langley97dfcbf2015-07-01 18:35:20 -07002393 {
2394 TLSEXT_TYPE_next_proto_neg,
2395 ext_npn_init,
2396 ext_npn_add_clienthello,
2397 ext_npn_parse_serverhello,
2398 ext_npn_parse_clienthello,
2399 ext_npn_add_serverhello,
2400 },
Adam Langleyab8d87d2015-07-10 12:21:39 -07002401 {
2402 TLSEXT_TYPE_certificate_timestamp,
2403 NULL,
2404 ext_sct_add_clienthello,
2405 ext_sct_parse_serverhello,
2406 ext_sct_parse_clienthello,
2407 ext_sct_add_serverhello,
2408 },
Adam Langleyf18e4532015-07-10 13:39:53 -07002409 {
2410 TLSEXT_TYPE_application_layer_protocol_negotiation,
2411 ext_alpn_init,
2412 ext_alpn_add_clienthello,
2413 ext_alpn_parse_serverhello,
2414 ext_alpn_parse_clienthello,
2415 ext_alpn_add_serverhello,
2416 },
Adam Langley49c7af12015-07-10 14:33:46 -07002417 {
2418 TLSEXT_TYPE_channel_id,
2419 ext_channel_id_init,
2420 ext_channel_id_add_clienthello,
2421 ext_channel_id_parse_serverhello,
2422 ext_channel_id_parse_clienthello,
2423 ext_channel_id_add_serverhello,
2424 },
Adam Langley391250d2015-07-15 19:06:07 -07002425 {
2426 TLSEXT_TYPE_srtp,
2427 ext_srtp_init,
2428 ext_srtp_add_clienthello,
2429 ext_srtp_parse_serverhello,
2430 ext_srtp_parse_clienthello,
2431 ext_srtp_add_serverhello,
2432 },
Adam Langleybdd5d662015-07-20 16:19:08 -07002433 {
2434 TLSEXT_TYPE_ec_point_formats,
David Benjaminfc059942015-07-30 23:01:59 -04002435 NULL,
Adam Langleybdd5d662015-07-20 16:19:08 -07002436 ext_ec_point_add_clienthello,
2437 ext_ec_point_parse_serverhello,
2438 ext_ec_point_parse_clienthello,
2439 ext_ec_point_add_serverhello,
2440 },
Steven Valdez143e8b32016-07-11 13:19:03 -04002441 {
2442 TLSEXT_TYPE_draft_version,
2443 NULL,
2444 ext_draft_version_add_clienthello,
2445 forbid_parse_serverhello,
2446 ignore_parse_clienthello,
2447 dont_add_serverhello,
2448 },
2449 {
2450 TLSEXT_TYPE_key_share,
2451 NULL,
2452 ext_key_share_add_clienthello,
2453 forbid_parse_serverhello,
2454 ignore_parse_clienthello,
2455 dont_add_serverhello,
2456 },
Steven Valdez4aa154e2016-07-29 14:32:55 -04002457 {
2458 TLSEXT_TYPE_pre_shared_key,
2459 NULL,
2460 ext_pre_shared_key_add_clienthello,
2461 forbid_parse_serverhello,
2462 ignore_parse_clienthello,
2463 dont_add_serverhello,
2464 },
David Benjamin1e4ae002016-03-25 18:56:10 -04002465 /* The final extension must be non-empty. WebSphere Application Server 7.0 is
2466 * intolerant to the last extension being zero-length. See
2467 * https://crbug.com/363583. */
Adam Langley273d49c2015-07-20 16:38:52 -07002468 {
Steven Valdezce902a92016-05-17 11:47:53 -04002469 TLSEXT_TYPE_supported_groups,
2470 ext_supported_groups_init,
2471 ext_supported_groups_add_clienthello,
2472 ext_supported_groups_parse_serverhello,
2473 ext_supported_groups_parse_clienthello,
2474 ext_supported_groups_add_serverhello,
Adam Langley273d49c2015-07-20 16:38:52 -07002475 },
Adam Langley614c66a2015-06-12 15:26:58 -07002476};
2477
2478#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
2479
Adam Langley4cfa96b2015-07-01 11:56:55 -07002480OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2481 sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002482 too_many_extensions_for_sent_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002483OPENSSL_COMPILE_ASSERT(kNumExtensions <=
2484 sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
2485 8,
David Benjamin7ca4b422015-07-13 16:43:47 -04002486 too_many_extensions_for_received_bitset);
Adam Langley4cfa96b2015-07-01 11:56:55 -07002487
Adam Langley614c66a2015-06-12 15:26:58 -07002488static const struct tls_extension *tls_extension_find(uint32_t *out_index,
2489 uint16_t value) {
2490 unsigned i;
2491 for (i = 0; i < kNumExtensions; i++) {
2492 if (kExtensions[i].value == value) {
2493 *out_index = i;
2494 return &kExtensions[i];
2495 }
2496 }
2497
2498 return NULL;
2499}
2500
Adam Langley09505632015-07-30 18:10:13 -07002501int SSL_extension_supported(unsigned extension_value) {
2502 uint32_t index;
2503 return extension_value == TLSEXT_TYPE_padding ||
2504 tls_extension_find(&index, extension_value) != NULL;
2505}
2506
David Benjamine8d53502015-10-10 14:13:23 -04002507int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 /* don't add extensions for SSLv3 unless doing secure renegotiation */
David Benjamine8d53502015-10-10 14:13:23 -04002509 if (ssl->client_version == SSL3_VERSION &&
2510 !ssl->s3->send_connection_binding) {
2511 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002512 }
Adam Langley95c29f32014-06-20 12:00:00 -07002513
David Benjamine8d53502015-10-10 14:13:23 -04002514 CBB extensions;
2515 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002516 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002517 }
Adam Langley95c29f32014-06-20 12:00:00 -07002518
David Benjamine8d53502015-10-10 14:13:23 -04002519 ssl->s3->tmp.extensions.sent = 0;
2520 ssl->s3->tmp.custom_extensions.sent = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002521
Adam Langley614c66a2015-06-12 15:26:58 -07002522 size_t i;
2523 for (i = 0; i < kNumExtensions; i++) {
2524 if (kExtensions[i].init != NULL) {
David Benjamine8d53502015-10-10 14:13:23 -04002525 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002526 }
2527 }
Adam Langley95c29f32014-06-20 12:00:00 -07002528
Adam Langley614c66a2015-06-12 15:26:58 -07002529 for (i = 0; i < kNumExtensions; i++) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002530 const size_t len_before = CBB_len(&extensions);
David Benjamine8d53502015-10-10 14:13:23 -04002531 if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002532 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2533 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2534 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002535 }
Adam Langley95c29f32014-06-20 12:00:00 -07002536
Adam Langley33ad2b52015-07-20 17:43:53 -07002537 if (CBB_len(&extensions) != len_before) {
David Benjamine8d53502015-10-10 14:13:23 -04002538 ssl->s3->tmp.extensions.sent |= (1u << i);
Adam Langley614c66a2015-06-12 15:26:58 -07002539 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 }
Adam Langley75712922014-10-10 16:23:43 -07002541
David Benjamine8d53502015-10-10 14:13:23 -04002542 if (!custom_ext_add_clienthello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002543 goto err;
2544 }
2545
David Benjamince079fd2016-08-02 16:22:34 -04002546 if (!SSL_is_dtls(ssl)) {
David Benjamina01deee2015-12-08 18:56:31 -05002547 header_len += 2 + CBB_len(&extensions);
Adam Langleyfcf25832014-12-18 17:42:32 -08002548 if (header_len > 0xff && header_len < 0x200) {
Adam Langley10a1a9d2015-10-21 14:49:23 -07002549 /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
Adam Langleyfcf25832014-12-18 17:42:32 -08002550 *
2551 * NB: because this code works out the length of all existing extensions
2552 * it MUST always appear last. */
David Benjamin0a968592015-07-21 22:06:19 -04002553 size_t padding_len = 0x200 - header_len;
David Benjamin1e4ae002016-03-25 18:56:10 -04002554 /* Extensions take at least four bytes to encode. Always include at least
Adam Langleyfcf25832014-12-18 17:42:32 -08002555 * one byte of data if including the extension. WebSphere Application
David Benjamin1e4ae002016-03-25 18:56:10 -04002556 * Server 7.0 is intolerant to the last extension being zero-length. See
2557 * https://crbug.com/363583. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002558 if (padding_len >= 4 + 1) {
2559 padding_len -= 4;
2560 } else {
2561 padding_len = 1;
2562 }
Adam Langley95c29f32014-06-20 12:00:00 -07002563
Adam Langley33ad2b52015-07-20 17:43:53 -07002564 uint8_t *padding_bytes;
2565 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
2566 !CBB_add_u16(&extensions, padding_len) ||
2567 !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
2568 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002569 }
Adam Langley75712922014-10-10 16:23:43 -07002570
Adam Langley33ad2b52015-07-20 17:43:53 -07002571 memset(padding_bytes, 0, padding_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08002572 }
2573 }
Adam Langley75712922014-10-10 16:23:43 -07002574
David Benjamina01deee2015-12-08 18:56:31 -05002575 /* Discard empty extensions blocks. */
2576 if (CBB_len(&extensions) == 0) {
David Benjamine8d53502015-10-10 14:13:23 -04002577 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002578 }
2579
David Benjamine8d53502015-10-10 14:13:23 -04002580 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002581
2582err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002583 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine8d53502015-10-10 14:13:23 -04002584 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002585}
Adam Langley95c29f32014-06-20 12:00:00 -07002586
David Benjamin56380462015-10-10 14:59:09 -04002587int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
David Benjamin56380462015-10-10 14:59:09 -04002588 CBB extensions;
2589 if (!CBB_add_u16_length_prefixed(out, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002590 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002591 }
2592
2593 unsigned i;
2594 for (i = 0; i < kNumExtensions; i++) {
David Benjamin56380462015-10-10 14:59:09 -04002595 if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
Adam Langley614c66a2015-06-12 15:26:58 -07002596 /* Don't send extensions that were not received. */
2597 continue;
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 (!kExtensions[i].add_serverhello(ssl, &extensions)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002601 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
2602 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2603 goto err;
Adam Langley614c66a2015-06-12 15:26:58 -07002604 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002605 }
Adam Langley95c29f32014-06-20 12:00:00 -07002606
David Benjamin56380462015-10-10 14:59:09 -04002607 if (!custom_ext_add_serverhello(ssl, &extensions)) {
Adam Langley09505632015-07-30 18:10:13 -07002608 goto err;
2609 }
2610
Steven Valdez143e8b32016-07-11 13:19:03 -04002611 /* Discard empty extensions blocks before TLS 1.3. */
2612 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
2613 CBB_len(&extensions) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04002614 CBB_discard_child(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002615 }
2616
David Benjamin56380462015-10-10 14:59:09 -04002617 return CBB_flush(out);
Adam Langley33ad2b52015-07-20 17:43:53 -07002618
2619err:
Adam Langley33ad2b52015-07-20 17:43:53 -07002620 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -04002621 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002622}
Adam Langley95c29f32014-06-20 12:00:00 -07002623
David Benjamine14ff062016-08-09 16:21:24 -04002624static int ssl_scan_clienthello_tlsext(
2625 SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
2626 int *out_alert) {
David Benjamin1deb41b2016-08-09 19:36:38 -04002627 for (size_t i = 0; i < kNumExtensions; i++) {
Adam Langley614c66a2015-06-12 15:26:58 -07002628 if (kExtensions[i].init != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002629 kExtensions[i].init(ssl);
Adam Langley614c66a2015-06-12 15:26:58 -07002630 }
2631 }
2632
David Benjamin0d56f882015-12-19 17:05:56 -05002633 ssl->s3->tmp.extensions.received = 0;
2634 ssl->s3->tmp.custom_extensions.received = 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002635
David Benjamine14ff062016-08-09 16:21:24 -04002636 CBS extensions;
2637 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
2638 while (CBS_len(&extensions) != 0) {
2639 uint16_t type;
2640 CBS extension;
2641
2642 /* Decode the next extension. */
2643 if (!CBS_get_u16(&extensions, &type) ||
2644 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002645 *out_alert = SSL_AD_DECODE_ERROR;
2646 return 0;
2647 }
Adam Langley95c29f32014-06-20 12:00:00 -07002648
David Benjamine14ff062016-08-09 16:21:24 -04002649 /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
2650 * ambiguous. Ignore all but the renegotiation_info extension. */
2651 if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
2652 continue;
2653 }
Adam Langley95c29f32014-06-20 12:00:00 -07002654
David Benjamine14ff062016-08-09 16:21:24 -04002655 unsigned ext_index;
2656 const struct tls_extension *const ext =
2657 tls_extension_find(&ext_index, type);
Adam Langley33ad2b52015-07-20 17:43:53 -07002658
David Benjamine14ff062016-08-09 16:21:24 -04002659 if (ext == NULL) {
2660 if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002661 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002662 return 0;
2663 }
David Benjamine14ff062016-08-09 16:21:24 -04002664 continue;
2665 }
2666
2667 ssl->s3->tmp.extensions.received |= (1u << ext_index);
2668 uint8_t alert = SSL_AD_DECODE_ERROR;
2669 if (!ext->parse_clienthello(ssl, &alert, &extension)) {
2670 *out_alert = alert;
2671 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2672 ERR_add_error_dataf("extension: %u", (unsigned)type);
2673 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002674 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002675 }
Adam Langley75712922014-10-10 16:23:43 -07002676
David Benjamin1deb41b2016-08-09 19:36:38 -04002677 for (size_t i = 0; i < kNumExtensions; i++) {
2678 if (ssl->s3->tmp.extensions.received & (1u << i)) {
2679 continue;
2680 }
2681
2682 CBS *contents = NULL, fake_contents;
2683 static const uint8_t kFakeRenegotiateExtension[] = {0};
2684 if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
2685 ssl_client_cipher_list_contains_cipher(client_hello,
2686 SSL3_CK_SCSV & 0xffff)) {
2687 /* The renegotiation SCSV was received so pretend that we received a
2688 * renegotiation extension. */
2689 CBS_init(&fake_contents, kFakeRenegotiateExtension,
2690 sizeof(kFakeRenegotiateExtension));
2691 contents = &fake_contents;
2692 ssl->s3->tmp.extensions.received |= (1u << i);
2693 }
2694
2695 /* Extension wasn't observed so call the callback with a NULL
2696 * parameter. */
2697 uint8_t alert = SSL_AD_DECODE_ERROR;
2698 if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
2699 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2700 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
2701 *out_alert = alert;
2702 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002703 }
2704 }
2705
Adam Langleyfcf25832014-12-18 17:42:32 -08002706 return 1;
2707}
Adam Langley95c29f32014-06-20 12:00:00 -07002708
David Benjamine14ff062016-08-09 16:21:24 -04002709int ssl_parse_clienthello_tlsext(
2710 SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002711 int alert = -1;
David Benjamine14ff062016-08-09 16:21:24 -04002712 if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002713 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002714 return 0;
2715 }
Adam Langley95c29f32014-06-20 12:00:00 -07002716
David Benjamin0d56f882015-12-19 17:05:56 -05002717 if (ssl_check_clienthello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002718 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002719 return 0;
2720 }
Adam Langley95c29f32014-06-20 12:00:00 -07002721
Adam Langleyfcf25832014-12-18 17:42:32 -08002722 return 1;
2723}
Adam Langley95c29f32014-06-20 12:00:00 -07002724
Matt Braithwaitee564a5b2015-09-30 15:24:05 -07002725OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
2726
David Benjamin0d56f882015-12-19 17:05:56 -05002727static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
Steven Valdez143e8b32016-07-11 13:19:03 -04002728 /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
2729 if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
2730 return 1;
2731 }
Adam Langley614c66a2015-06-12 15:26:58 -07002732
Steven Valdez143e8b32016-07-11 13:19:03 -04002733 /* Decode the extensions block and check it is valid. */
2734 CBS extensions;
2735 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
2736 !tls1_check_duplicate_extensions(&extensions)) {
2737 *out_alert = SSL_AD_DECODE_ERROR;
2738 return 0;
2739 }
2740
2741 uint32_t received = 0;
2742 while (CBS_len(&extensions) != 0) {
2743 uint16_t type;
2744 CBS extension;
2745
2746 /* Decode the next extension. */
2747 if (!CBS_get_u16(&extensions, &type) ||
2748 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002749 *out_alert = SSL_AD_DECODE_ERROR;
2750 return 0;
2751 }
Adam Langley95c29f32014-06-20 12:00:00 -07002752
Steven Valdez143e8b32016-07-11 13:19:03 -04002753 unsigned ext_index;
2754 const struct tls_extension *const ext =
2755 tls_extension_find(&ext_index, type);
Adam Langley614c66a2015-06-12 15:26:58 -07002756
Steven Valdez143e8b32016-07-11 13:19:03 -04002757 if (ext == NULL) {
2758 if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002759 return 0;
2760 }
Steven Valdez143e8b32016-07-11 13:19:03 -04002761 continue;
2762 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002763
David Benjamin1deb41b2016-08-09 19:36:38 -04002764 if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
2765 type != TLSEXT_TYPE_renegotiate) {
2766 /* If the extension was never sent then it is illegal, except for the
2767 * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
Steven Valdez143e8b32016-07-11 13:19:03 -04002768 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
2769 ERR_add_error_dataf("extension :%u", (unsigned)type);
David Benjamin0c40a962016-08-01 12:05:50 -04002770 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Steven Valdez143e8b32016-07-11 13:19:03 -04002771 return 0;
2772 }
Adam Langley33ad2b52015-07-20 17:43:53 -07002773
Steven Valdez143e8b32016-07-11 13:19:03 -04002774 received |= (1u << ext_index);
Adam Langley09505632015-07-30 18:10:13 -07002775
Steven Valdez143e8b32016-07-11 13:19:03 -04002776 uint8_t alert = SSL_AD_DECODE_ERROR;
2777 if (!ext->parse_serverhello(ssl, &alert, &extension)) {
2778 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
2779 ERR_add_error_dataf("extension: %u", (unsigned)type);
2780 *out_alert = alert;
2781 return 0;
Adam Langley614c66a2015-06-12 15:26:58 -07002782 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002783 }
Adam Langley95c29f32014-06-20 12:00:00 -07002784
Adam Langley33ad2b52015-07-20 17:43:53 -07002785 size_t i;
Adam Langley614c66a2015-06-12 15:26:58 -07002786 for (i = 0; i < kNumExtensions; i++) {
2787 if (!(received & (1u << i))) {
2788 /* Extension wasn't observed so call the callback with a NULL
2789 * parameter. */
2790 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin0d56f882015-12-19 17:05:56 -05002791 if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
Adam Langley33ad2b52015-07-20 17:43:53 -07002792 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
2793 ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
Adam Langley614c66a2015-06-12 15:26:58 -07002794 *out_alert = alert;
Adam Langleyfcf25832014-12-18 17:42:32 -08002795 return 0;
2796 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002797 }
2798 }
Adam Langley95c29f32014-06-20 12:00:00 -07002799
Adam Langleyfcf25832014-12-18 17:42:32 -08002800 return 1;
2801}
Adam Langley95c29f32014-06-20 12:00:00 -07002802
David Benjamin0d56f882015-12-19 17:05:56 -05002803static int ssl_check_clienthello_tlsext(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002804 int ret = SSL_TLSEXT_ERR_NOACK;
2805 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07002806
David Benjamin78f8aab2016-03-10 16:33:58 -05002807 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002808 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002809 ssl->ctx->tlsext_servername_arg);
2810 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002811 ret = ssl->initial_ctx->tlsext_servername_callback(
2812 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002813 }
Adam Langley95c29f32014-06-20 12:00:00 -07002814
Adam Langleyfcf25832014-12-18 17:42:32 -08002815 switch (ret) {
2816 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002817 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002818 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07002819
Adam Langleyfcf25832014-12-18 17:42:32 -08002820 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002821 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002822 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002823
Adam Langleyfcf25832014-12-18 17:42:32 -08002824 case SSL_TLSEXT_ERR_NOACK:
David Benjamin0d56f882015-12-19 17:05:56 -05002825 ssl->s3->tmp.should_ack_sni = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002826 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002827
Adam Langleyfcf25832014-12-18 17:42:32 -08002828 default:
2829 return 1;
2830 }
2831}
Adam Langleyed8270a2014-09-02 13:52:56 -07002832
David Benjamin0d56f882015-12-19 17:05:56 -05002833static int ssl_check_serverhello_tlsext(SSL *ssl) {
David Benjaminfc059942015-07-30 23:01:59 -04002834 int ret = SSL_TLSEXT_ERR_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -08002835 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002836
David Benjamin78f8aab2016-03-10 16:33:58 -05002837 if (ssl->ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002838 ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
David Benjamin78f8aab2016-03-10 16:33:58 -05002839 ssl->ctx->tlsext_servername_arg);
2840 } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05002841 ret = ssl->initial_ctx->tlsext_servername_callback(
2842 ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08002843 }
Adam Langley95c29f32014-06-20 12:00:00 -07002844
Adam Langleyfcf25832014-12-18 17:42:32 -08002845 switch (ret) {
2846 case SSL_TLSEXT_ERR_ALERT_FATAL:
David Benjamin0d56f882015-12-19 17:05:56 -05002847 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002848 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002849
Adam Langleyfcf25832014-12-18 17:42:32 -08002850 case SSL_TLSEXT_ERR_ALERT_WARNING:
David Benjamin0d56f882015-12-19 17:05:56 -05002851 ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08002852 return 1;
2853
2854 default:
2855 return 1;
2856 }
2857}
2858
David Benjamin0d56f882015-12-19 17:05:56 -05002859int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002860 int alert = -1;
David Benjamin0d56f882015-12-19 17:05:56 -05002861 if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
2862 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -08002863 return 0;
2864 }
2865
David Benjamin0d56f882015-12-19 17:05:56 -05002866 if (ssl_check_serverhello_tlsext(ssl) <= 0) {
David Benjamin3570d732015-06-29 00:28:17 -04002867 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002868 return 0;
2869 }
2870
2871 return 1;
2872}
Adam Langley95c29f32014-06-20 12:00:00 -07002873
David Benjamine3aa1d92015-06-16 15:34:50 -04002874int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
David Benjaminef1b0092015-11-21 14:05:44 -05002875 int *out_renew_ticket, const uint8_t *ticket,
David Benjamine3aa1d92015-06-16 15:34:50 -04002876 size_t ticket_len, const uint8_t *session_id,
2877 size_t session_id_len) {
2878 int ret = 1; /* Most errors are non-fatal. */
2879 SSL_CTX *ssl_ctx = ssl->initial_ctx;
2880 uint8_t *plaintext = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002881
David Benjamine3aa1d92015-06-16 15:34:50 -04002882 HMAC_CTX hmac_ctx;
2883 HMAC_CTX_init(&hmac_ctx);
2884 EVP_CIPHER_CTX cipher_ctx;
2885 EVP_CIPHER_CTX_init(&cipher_ctx);
2886
David Benjaminef1b0092015-11-21 14:05:44 -05002887 *out_renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -04002888 *out_session = NULL;
2889
Steven Valdez4aa154e2016-07-29 14:32:55 -04002890 if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
2891 goto done;
2892 }
2893
David Benjamine3aa1d92015-06-16 15:34:50 -04002894 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
2895 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002896 }
2897
David Benjaminadcc3952015-04-26 13:07:57 -04002898 /* Ensure there is room for the key name and the largest IV
2899 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2900 * the maximum IV length should be well under the minimum size for the
2901 * session material and HMAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002902 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
2903 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002904 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002905 const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
Adam Langleyfcf25832014-12-18 17:42:32 -08002906
David Benjamine3aa1d92015-06-16 15:34:50 -04002907 if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05002908 int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
2909 ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
2910 &hmac_ctx, 0 /* decrypt */);
David Benjamine3aa1d92015-06-16 15:34:50 -04002911 if (cb_ret < 0) {
2912 ret = 0;
2913 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002914 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002915 if (cb_ret == 0) {
2916 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002917 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002918 if (cb_ret == 2) {
David Benjaminef1b0092015-11-21 14:05:44 -05002919 *out_renew_ticket = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08002920 }
2921 } else {
David Benjamine3aa1d92015-06-16 15:34:50 -04002922 /* Check the key name matches. */
2923 if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
2924 SSL_TICKET_KEY_NAME_LEN) != 0) {
2925 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002926 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002927 if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
2928 sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
Adam Langleyfcf25832014-12-18 17:42:32 -08002929 NULL) ||
David Benjamine3aa1d92015-06-16 15:34:50 -04002930 !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
2931 ssl_ctx->tlsext_tick_aes_key, iv)) {
2932 ret = 0;
2933 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002934 }
2935 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002936 size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08002937
David Benjamine3aa1d92015-06-16 15:34:50 -04002938 /* Check the MAC at the end of the ticket. */
2939 uint8_t mac[EVP_MAX_MD_SIZE];
2940 size_t mac_len = HMAC_size(&hmac_ctx);
2941 if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
David Benjaminadcc3952015-04-26 13:07:57 -04002942 /* The ticket must be large enough for key name, IV, data, and MAC. */
David Benjamine3aa1d92015-06-16 15:34:50 -04002943 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002944 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002945 HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
2946 HMAC_Final(&hmac_ctx, mac, NULL);
2947 if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
2948 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002949 }
2950
David Benjamine3aa1d92015-06-16 15:34:50 -04002951 /* Decrypt the session data. */
2952 const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
2953 size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
2954 mac_len;
2955 plaintext = OPENSSL_malloc(ciphertext_len);
2956 if (plaintext == NULL) {
2957 ret = 0;
2958 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002959 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002960 if (ciphertext_len >= INT_MAX) {
2961 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002962 }
David Benjamine3aa1d92015-06-16 15:34:50 -04002963 int len1, len2;
2964 if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
2965 (int)ciphertext_len) ||
2966 !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
2967 ERR_clear_error(); /* Don't leave an error on the queue. */
2968 goto done;
Adam Langleyfcf25832014-12-18 17:42:32 -08002969 }
2970
David Benjamine3aa1d92015-06-16 15:34:50 -04002971 /* Decode the session. */
2972 SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
2973 if (session == NULL) {
2974 ERR_clear_error(); /* Don't leave an error on the queue. */
2975 goto done;
2976 }
2977
2978 /* Copy the client's session ID into the new session, to denote the ticket has
2979 * been accepted. */
2980 memcpy(session->session_id, session_id, session_id_len);
2981 session->session_id_length = session_id_len;
2982
Steven Valdez4aa154e2016-07-29 14:32:55 -04002983 if (!ssl_session_is_context_valid(ssl, session) ||
2984 !ssl_session_is_time_valid(ssl, session)) {
2985 SSL_SESSION_free(session);
2986 session = NULL;
2987 }
2988
David Benjamine3aa1d92015-06-16 15:34:50 -04002989 *out_session = session;
2990
2991done:
2992 OPENSSL_free(plaintext);
2993 HMAC_CTX_cleanup(&hmac_ctx);
2994 EVP_CIPHER_CTX_cleanup(&cipher_ctx);
2995 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002996}
Adam Langley95c29f32014-06-20 12:00:00 -07002997
Steven Valdez0d62f262015-09-04 12:41:04 -04002998int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002999 /* Extension ignored for inappropriate versions */
David Benjamina1e9cab2015-12-30 00:08:49 -05003000 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003001 return 1;
3002 }
David Benjamincd996942014-07-20 16:23:51 -04003003
Steven Valdez0d62f262015-09-04 12:41:04 -04003004 CERT *const cert = ssl->cert;
3005 OPENSSL_free(cert->peer_sigalgs);
3006 cert->peer_sigalgs = NULL;
3007 cert->peer_sigalgslen = 0;
3008
3009 size_t num_sigalgs = CBS_len(in_sigalgs);
3010
3011 if (num_sigalgs % 2 != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003012 return 0;
3013 }
Steven Valdez0d62f262015-09-04 12:41:04 -04003014 num_sigalgs /= 2;
3015
3016 /* supported_signature_algorithms in the certificate request is
3017 * allowed to be empty. */
3018 if (num_sigalgs == 0) {
3019 return 1;
3020 }
3021
Steven Valdez02563852016-06-23 13:33:05 -04003022 /* This multiplication doesn't overflow because sizeof(uint16_t) is two
3023 * and we just divided |num_sigalgs| by two. */
3024 cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
Steven Valdez0d62f262015-09-04 12:41:04 -04003025 if (cert->peer_sigalgs == NULL) {
3026 return 0;
3027 }
3028 cert->peer_sigalgslen = num_sigalgs;
3029
3030 CBS sigalgs;
3031 CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
3032
3033 size_t i;
3034 for (i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04003035 if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) {
Steven Valdez0d62f262015-09-04 12:41:04 -04003036 return 0;
3037 }
3038 }
Adam Langley95c29f32014-06-20 12:00:00 -07003039
Adam Langleyfcf25832014-12-18 17:42:32 -08003040 return 1;
3041}
David Benjaminec2f27d2014-11-13 19:17:25 -05003042
David Benjaminea9a0d52016-07-08 15:52:59 -07003043int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
David Benjamind1d80782015-07-05 11:54:09 -04003044 CERT *cert = ssl->cert;
Steven Valdez0d62f262015-09-04 12:41:04 -04003045 size_t i, j;
David Benjaminec2f27d2014-11-13 19:17:25 -05003046
Steven Valdezf0451ca2016-06-29 13:16:27 -04003047 /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
3048 * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
3049 if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0c0b7e12016-07-14 13:47:55 -04003050 int type = ssl_private_key_type(ssl);
3051 if (type == NID_rsaEncryption) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003052 *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
David Benjamin0c0b7e12016-07-14 13:47:55 -04003053 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003054 }
David Benjamin0c0b7e12016-07-14 13:47:55 -04003055 if (ssl_is_ecdsa_key_type(type)) {
3056 *out = SSL_SIGN_ECDSA_SHA1;
3057 return 1;
3058 }
3059 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3060 return 0;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003061 }
3062
Steven Valdezeff1e8d2016-07-06 14:24:47 -04003063 const uint16_t *sigalgs;
3064 size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
David Benjamind246b812016-07-08 15:07:02 -07003065 if (cert->sigalgs != NULL) {
3066 sigalgs = cert->sigalgs;
3067 sigalgs_len = cert->sigalgs_len;
Steven Valdez0d62f262015-09-04 12:41:04 -04003068 }
3069
David Benjaminea9a0d52016-07-08 15:52:59 -07003070 const uint16_t *peer_sigalgs = cert->peer_sigalgs;
3071 size_t peer_sigalgs_len = cert->peer_sigalgslen;
David Benjamin51dd7d62016-07-08 16:07:01 -07003072 if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminea9a0d52016-07-08 15:52:59 -07003073 /* If the client didn't specify any signature_algorithms extension then
3074 * we can assume that it supports SHA1. See
3075 * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
3076 static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
3077 SSL_SIGN_ECDSA_SHA1};
3078 peer_sigalgs = kDefaultPeerAlgorithms;
3079 peer_sigalgs_len =
David Benjaminee32bea2016-08-17 13:36:44 -04003080 sizeof(kDefaultPeerAlgorithms) / sizeof(kDefaultPeerAlgorithms[0]);
David Benjaminea9a0d52016-07-08 15:52:59 -07003081 }
3082
David Benjamind246b812016-07-08 15:07:02 -07003083 for (i = 0; i < sigalgs_len; i++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003084 uint16_t sigalg = sigalgs[i];
3085 /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
3086 * negotiated. */
3087 if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
3088 !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
3089 continue;
3090 }
3091
David Benjaminea9a0d52016-07-08 15:52:59 -07003092 for (j = 0; j < peer_sigalgs_len; j++) {
David Benjamin1fb125c2016-07-08 18:52:12 -07003093 if (sigalg == peer_sigalgs[j]) {
3094 *out = sigalg;
David Benjaminea9a0d52016-07-08 15:52:59 -07003095 return 1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04003096 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003097 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003098 }
Adam Langley95c29f32014-06-20 12:00:00 -07003099
David Benjaminea9a0d52016-07-08 15:52:59 -07003100 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
3101 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003102}
Adam Langley95c29f32014-06-20 12:00:00 -07003103
David Benjamind6a4ae92015-08-06 11:10:51 -04003104int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
3105 int ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08003106 EVP_MD_CTX ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08003107
3108 EVP_MD_CTX_init(&ctx);
David Benjamind6a4ae92015-08-06 11:10:51 -04003109 if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
3110 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08003111 }
Adam Langleyfcf25832014-12-18 17:42:32 -08003112
David Benjamind6a4ae92015-08-06 11:10:51 -04003113 static const char kClientIDMagic[] = "TLS Channel ID signature";
3114 EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
3115
Steven Valdez87eab492016-06-27 16:34:59 -04003116 if (ssl->session != NULL) {
David Benjamind6a4ae92015-08-06 11:10:51 -04003117 static const char kResumptionMagic[] = "Resumption";
3118 EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
3119 if (ssl->session->original_handshake_hash_len == 0) {
3120 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
3121 goto err;
3122 }
3123 EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
3124 ssl->session->original_handshake_hash_len);
3125 }
3126
3127 uint8_t handshake_hash[EVP_MAX_MD_SIZE];
3128 int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
3129 sizeof(handshake_hash));
3130 if (handshake_hash_len < 0) {
3131 goto err;
3132 }
3133 EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
3134 unsigned len_u;
3135 EVP_DigestFinal_ex(&ctx, out, &len_u);
3136 *out_len = len_u;
3137
3138 ret = 1;
3139
3140err:
3141 EVP_MD_CTX_cleanup(&ctx);
3142 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08003143}
Adam Langley1258b6a2014-06-20 12:00:00 -07003144
3145/* tls1_record_handshake_hashes_for_channel_id records the current handshake
Steven Valdez87eab492016-06-27 16:34:59 -04003146 * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
David Benjamin0d56f882015-12-19 17:05:56 -05003147 * data. */
3148int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003149 int digest_len;
3150 /* This function should never be called for a resumed session because the
3151 * handshake hashes that we wish to record are for the original, full
3152 * handshake. */
Steven Valdez87eab492016-06-27 16:34:59 -04003153 if (ssl->session != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08003154 return -1;
3155 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003156
Adam Langleyfcf25832014-12-18 17:42:32 -08003157 digest_len =
Steven Valdez87eab492016-06-27 16:34:59 -04003158 tls1_handshake_digest(
3159 ssl, ssl->s3->new_session->original_handshake_hash,
3160 sizeof(ssl->s3->new_session->original_handshake_hash));
Adam Langleyfcf25832014-12-18 17:42:32 -08003161 if (digest_len < 0) {
3162 return -1;
3163 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003164
Steven Valdez87eab492016-06-27 16:34:59 -04003165 ssl->s3->new_session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07003166
Adam Langleyfcf25832014-12-18 17:42:32 -08003167 return 1;
3168}