blob: 07507e9be0f735d23bfbecf124bd723600e0d38f [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 Benjaminf0ae1702015-04-07 23:05:04 -0400109#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700110#include <stdio.h>
David Benjamin35a7a442014-07-05 00:23:20 -0400111#include <stdlib.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400112#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin03973092014-06-24 23:27:17 -0400114#include <openssl/bytestring.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <openssl/evp.h>
117#include <openssl/hmac.h>
118#include <openssl/mem.h>
119#include <openssl/obj.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langleyfcf25832014-12-18 17:42:32 -0800123
124
125static int tls_decrypt_ticket(SSL *s, const uint8_t *tick, int ticklen,
126 const uint8_t *sess_id, int sesslen,
127 SSL_SESSION **psess);
David Benjamin6c7aed02014-08-27 16:42:38 -0400128static int ssl_check_clienthello_tlsext(SSL *s);
129static int ssl_check_serverhello_tlsext(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjamin338fcaf2014-12-11 01:20:52 -0500131const SSL3_ENC_METHOD TLSv1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800132 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500133 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800134 tls1_setup_key_block,
135 tls1_generate_master_secret,
136 tls1_change_cipher_state,
137 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_alert_code,
142 tls1_export_keying_material,
143 0,
Adam Langleyfcf25832014-12-18 17:42:32 -0800144};
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin338fcaf2014-12-11 01:20:52 -0500146const SSL3_ENC_METHOD TLSv1_1_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500148 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 tls1_setup_key_block,
150 tls1_generate_master_secret,
151 tls1_change_cipher_state,
152 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 tls1_cert_verify_mac,
154 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
155 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
156 tls1_alert_code,
157 tls1_export_keying_material,
158 SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langleyfcf25832014-12-18 17:42:32 -0800159};
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin338fcaf2014-12-11 01:20:52 -0500161const SSL3_ENC_METHOD TLSv1_2_enc_data = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500163 tls1_prf,
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 tls1_setup_key_block,
165 tls1_generate_master_secret,
166 tls1_change_cipher_state,
167 tls1_final_finish_mac,
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 tls1_cert_verify_mac,
169 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
170 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
171 tls1_alert_code,
172 tls1_export_keying_material,
173 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
174 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langleyfcf25832014-12-18 17:42:32 -0800175};
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langleyfcf25832014-12-18 17:42:32 -0800177static int compare_uint16_t(const void *p1, const void *p2) {
178 uint16_t u1 = *((const uint16_t *)p1);
179 uint16_t u2 = *((const uint16_t *)p2);
180 if (u1 < u2) {
181 return -1;
182 } else if (u1 > u2) {
183 return 1;
184 } else {
185 return 0;
186 }
187}
David Benjamin35a7a442014-07-05 00:23:20 -0400188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
190 * more than one extension of the same type in a ClientHello or ServerHello.
191 * This function does an initial scan over the extensions block to filter those
David Benjamin35a7a442014-07-05 00:23:20 -0400192 * out. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800193static int tls1_check_duplicate_extensions(const CBS *cbs) {
194 CBS extensions = *cbs;
195 size_t num_extensions = 0, i = 0;
196 uint16_t *extension_types = NULL;
197 int ret = 0;
David Benjamin35a7a442014-07-05 00:23:20 -0400198
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 /* First pass: count the extensions. */
200 while (CBS_len(&extensions) > 0) {
201 uint16_t type;
202 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 if (!CBS_get_u16(&extensions, &type) ||
205 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
206 goto done;
207 }
David Benjamin35a7a442014-07-05 00:23:20 -0400208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 num_extensions++;
210 }
David Benjamin35a7a442014-07-05 00:23:20 -0400211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 if (num_extensions == 0) {
213 return 1;
214 }
David Benjamin9a373592014-07-25 04:27:53 -0400215
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 extension_types =
217 (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
218 if (extension_types == NULL) {
219 OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions,
220 ERR_R_MALLOC_FAILURE);
221 goto done;
222 }
David Benjamin35a7a442014-07-05 00:23:20 -0400223
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 /* Second pass: gather the extension types. */
225 extensions = *cbs;
226 for (i = 0; i < num_extensions; i++) {
227 CBS extension;
David Benjamin35a7a442014-07-05 00:23:20 -0400228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 if (!CBS_get_u16(&extensions, &extension_types[i]) ||
230 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
231 /* This should not happen. */
232 goto done;
233 }
234 }
235 assert(CBS_len(&extensions) == 0);
David Benjamin35a7a442014-07-05 00:23:20 -0400236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 /* Sort the extensions and make sure there are no duplicates. */
238 qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
239 for (i = 1; i < num_extensions; i++) {
240 if (extension_types[i - 1] == extension_types[i]) {
241 goto done;
242 }
243 }
David Benjamin35a7a442014-07-05 00:23:20 -0400244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ret = 1;
246
David Benjamin35a7a442014-07-05 00:23:20 -0400247done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400248 OPENSSL_free(extension_types);
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 return ret;
250}
David Benjamin35a7a442014-07-05 00:23:20 -0400251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
253 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400254
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 if (/* Skip client version. */
258 !CBS_skip(&client_hello, 2) ||
259 /* Skip client nonce. */
260 !CBS_skip(&client_hello, 32) ||
261 /* Extract session_id. */
262 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
263 return 0;
264 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700265
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 ctx->session_id = CBS_data(&session_id);
267 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 /* Skip past DTLS cookie */
270 if (SSL_IS_DTLS(ctx->ssl)) {
271 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
274 return 0;
275 }
276 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* Extract cipher_suites. */
279 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
280 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
281 return 0;
282 }
283 ctx->cipher_suites = CBS_data(&cipher_suites);
284 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 /* Extract compression_methods. */
287 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
288 CBS_len(&compression_methods) < 1) {
289 return 0;
290 }
291 ctx->compression_methods = CBS_data(&compression_methods);
292 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700293
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 /* If the ClientHello ends here then it's valid, but doesn't have any
295 * extensions. (E.g. SSLv3.) */
296 if (CBS_len(&client_hello) == 0) {
297 ctx->extensions = NULL;
298 ctx->extensions_len = 0;
299 return 1;
300 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 /* Extract extensions and check it is valid. */
303 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
304 !tls1_check_duplicate_extensions(&extensions) ||
305 CBS_len(&client_hello) != 0) {
306 return 0;
307 }
308 ctx->extensions = CBS_data(&extensions);
309 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 return 1;
312}
Adam Langleydc9b1412014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314char SSL_early_callback_ctx_extension_get(
315 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
316 const uint8_t **out_data, size_t *out_len) {
317 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 while (CBS_len(&extensions) != 0) {
322 uint16_t type;
323 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 /* Decode the next extension. */
326 if (!CBS_get_u16(&extensions, &type) ||
327 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
328 return 0;
329 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 if (type == extension_type) {
332 *out_data = CBS_data(&extension);
333 *out_len = CBS_len(&extension);
334 return 1;
335 }
336 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 return 0;
339}
Adam Langley95c29f32014-06-20 12:00:00 -0700340
David Benjamin52e5bac2014-12-27 02:27:35 -0500341struct tls_curve {
342 uint16_t curve_id;
343 int nid;
344};
345
David Benjamin70bd80a2014-12-27 03:06:46 -0500346/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500347static const struct tls_curve tls_curves[] = {
348 {21, NID_secp224r1},
349 {23, NID_X9_62_prime256v1},
350 {24, NID_secp384r1},
351 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800352};
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354static const uint8_t ecformats_default[] = {
355 TLSEXT_ECPOINTFORMAT_uncompressed,
356};
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langleyfcf25832014-12-18 17:42:32 -0800358static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400359 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500360 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800361};
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500364 size_t i;
365 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
366 if (curve_id == tls_curves[i].curve_id) {
367 return tls_curves[i].nid;
368 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500370 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
David Benjamin70bd80a2014-12-27 03:06:46 -0500373int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500375 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
376 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500377 *out_curve_id = tls_curves[i].curve_id;
378 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 0;
382}
383
384/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
385 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
386 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400387static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 const uint16_t **out_curve_ids,
389 size_t *out_curve_ids_len) {
390 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400391 /* Only clients send a curve list, so this function is only called
392 * on the server. */
393 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
395 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
396 return;
397 }
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 *out_curve_ids = s->tlsext_ellipticcurvelist;
400 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
401 if (!*out_curve_ids) {
402 *out_curve_ids = eccurves_default;
403 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
404 }
405}
David Benjamined439582014-07-14 19:13:02 -0400406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
408 uint8_t curve_type;
409 uint16_t curve_id;
410 const uint16_t *curves;
411 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 /* Only support named curves. */
414 if (!CBS_get_u8(cbs, &curve_type) ||
415 curve_type != NAMED_CURVE_TYPE ||
416 !CBS_get_u16(cbs, &curve_id)) {
417 return 0;
418 }
David Benjamined439582014-07-14 19:13:02 -0400419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 tls1_get_curvelist(s, 0, &curves, &curves_len);
421 for (i = 0; i < curves_len; i++) {
422 if (curve_id == curves[i]) {
423 *out_curve_id = curve_id;
424 return 1;
425 }
426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 return 0;
429}
David Benjamin072334d2014-07-13 16:24:27 -0400430
Adam Langleyfcf25832014-12-18 17:42:32 -0800431int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400432 const uint16_t *curves, *peer_curves, *pref, *supp;
433 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 /* Can't do anything on client side */
436 if (s->server == 0) {
437 return NID_undef;
438 }
439
David Benjamin55a43642015-04-20 14:45:55 -0400440 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
441 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800442
David Benjamin55a43642015-04-20 14:45:55 -0400443 if (peer_curves_len == 0) {
444 /* Clients are not required to send a supported_curves extension. In this
445 * case, the server is free to pick any curve it likes. See RFC 4492,
446 * section 4, paragraph 3. */
447 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
448 }
449
450 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
451 pref = curves;
452 pref_len = curves_len;
453 supp = peer_curves;
454 supp_len = peer_curves_len;
455 } else {
456 pref = peer_curves;
457 pref_len = peer_curves_len;
458 supp = curves;
459 supp_len = curves_len;
460 }
461
462 for (i = 0; i < pref_len; i++) {
463 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 if (pref[i] == supp[j]) {
465 return tls1_ec_curve_id2nid(pref[i]);
466 }
467 }
468 }
469
470 return NID_undef;
471}
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin072334d2014-07-13 16:24:27 -0400473int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 const int *curves, size_t ncurves) {
475 uint16_t *curve_ids;
476 size_t i;
477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
479 if (curve_ids == NULL) {
480 return 0;
481 }
482
483 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500484 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 OPENSSL_free(curve_ids);
486 return 0;
487 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 }
489
David Benjamin2755a3e2015-04-22 16:17:58 -0400490 OPENSSL_free(*out_curve_ids);
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 *out_curve_ids = curve_ids;
492 *out_curve_ids_len = ncurves;
493
494 return 1;
495}
Adam Langley95c29f32014-06-20 12:00:00 -0700496
David Benjamin072334d2014-07-13 16:24:27 -0400497/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
498 * TLS curve ID and point format, respectively, for |ec|. It returns one on
499 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800500static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
501 uint8_t *out_comp_id, EC_KEY *ec) {
502 int nid;
503 uint16_t id;
504 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 if (ec == NULL) {
507 return 0;
508 }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 grp = EC_KEY_get0_group(ec);
511 if (grp == NULL) {
512 return 0;
513 }
David Benjamin072334d2014-07-13 16:24:27 -0400514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* Determine curve ID */
516 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500517 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return 0;
519 }
David Benjamin072334d2014-07-13 16:24:27 -0400520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
522 *out_curve_id = id;
523
524 if (out_comp_id) {
525 if (EC_KEY_get0_public_key(ec) == NULL) {
526 return 0;
527 }
528 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
529 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
530 } else {
531 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
532 }
533 }
534
535 return 1;
536}
David Benjamin072334d2014-07-13 16:24:27 -0400537
David Benjamin42e9a772014-09-02 23:18:44 -0400538/* tls1_check_point_format returns one if |comp_id| is consistent with the
539 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800540static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
541 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
542 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
543 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 /* If point formats extension present check it, otherwise everything is
546 * supported (see RFC4492). */
547 if (p == NULL) {
548 return 1;
549 }
David Benjamin42e9a772014-09-02 23:18:44 -0400550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 for (i = 0; i < plen; i++) {
552 if (comp_id == p[i]) {
553 return 1;
554 }
555 }
David Benjamin42e9a772014-09-02 23:18:44 -0400556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 return 0;
558}
559
560/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
561 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400562 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800563static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
564 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400565 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400566
Adam Langleyfcf25832014-12-18 17:42:32 -0800567 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400568 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
569 if (get_peer_curves && !s->server) {
570 /* Servers do not present a preference list so, if we are a client, only
571 * check our list. */
572 continue;
573 }
574
575 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
576 if (get_peer_curves && curves_len == 0) {
577 /* Clients are not required to send a supported_curves extension. In this
578 * case, the server is free to pick any curve it likes. See RFC 4492,
579 * section 4, paragraph 3. */
580 continue;
581 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 for (i = 0; i < curves_len; i++) {
583 if (curves[i] == curve_id) {
584 break;
585 }
586 }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 if (i == curves_len) {
589 return 0;
590 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800591 }
David Benjamin033e5f42014-11-13 18:47:41 -0500592
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 return 1;
594}
David Benjamin033e5f42014-11-13 18:47:41 -0500595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
597 size_t *pformatslen) {
598 /* If we have a custom point format list use it otherwise use default */
599 if (s->tlsext_ecpointformatlist) {
600 *pformats = s->tlsext_ecpointformatlist;
601 *pformatslen = s->tlsext_ecpointformatlist_length;
602 } else {
603 *pformats = ecformats_default;
604 *pformatslen = sizeof(ecformats_default);
605 }
606}
607
608int tls1_check_ec_cert(SSL *s, X509 *x) {
609 int ret = 0;
610 EVP_PKEY *pkey = X509_get_pubkey(x);
611 uint16_t curve_id;
612 uint8_t comp_id;
613
614 if (!pkey ||
615 pkey->type != EVP_PKEY_EC ||
616 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
617 !tls1_check_curve_id(s, curve_id) ||
618 !tls1_check_point_format(s, comp_id)) {
619 goto done;
620 }
621
622 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500623
624done:
David Benjamin2755a3e2015-04-22 16:17:58 -0400625 EVP_PKEY_free(pkey);
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 return ret;
627}
David Benjamin42e9a772014-09-02 23:18:44 -0400628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629int tls1_check_ec_tmp_key(SSL *s) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400630 if (s->cert->ecdh_nid != NID_undef) {
David Benjamindd978782015-04-24 15:20:13 -0400631 /* If the curve is preconfigured, ECDH is acceptable iff the peer supports
David Benjaminc0f763b2015-03-27 02:05:39 -0400632 * the curve. */
633 uint16_t curve_id;
634 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
635 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 }
637
David Benjamindd978782015-04-24 15:20:13 -0400638 if (s->cert->ecdh_tmp_cb != NULL) {
639 /* Assume the callback will provide an acceptable curve. */
640 return 1;
641 }
642
643 /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff
644 * there is a shared curve. */
645 return tls1_get_shared_curve(s) != NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
648/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langley95c29f32014-06-20 12:00:00 -0700651#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700652
Adam Langley95c29f32014-06-20 12:00:00 -0700653#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamincff64722014-08-19 19:54:46 -0400657static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800658 tlsext_sigalg(TLSEXT_hash_sha512)
659 tlsext_sigalg(TLSEXT_hash_sha384)
660 tlsext_sigalg(TLSEXT_hash_sha256)
661 tlsext_sigalg(TLSEXT_hash_sha224)
662 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700663};
David Benjamin05da6e12014-07-12 20:42:55 -0400664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
666 /* If server use client authentication sigalgs if not NULL */
667 if (s->server && s->cert->client_sigalgs) {
668 *psigs = s->cert->client_sigalgs;
669 return s->cert->client_sigalgslen;
670 } else if (s->cert->conf_sigalgs) {
671 *psigs = s->cert->conf_sigalgs;
672 return s->cert->conf_sigalgslen;
673 } else {
674 *psigs = tls12_sigalgs;
675 return sizeof(tls12_sigalgs);
676 }
677}
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langleyfcf25832014-12-18 17:42:32 -0800679/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
680 * checks it is consistent with |s|'s sent supported signature algorithms and,
681 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
682 * returns 0 and writes an alert into |*out_alert|. */
683int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
684 CBS *cbs, EVP_PKEY *pkey) {
685 const uint8_t *sent_sigs;
686 size_t sent_sigslen, i;
687 int sigalg = tls12_get_sigid(pkey);
688 uint8_t hash, signature;
689
690 /* Should never happen */
691 if (sigalg == -1) {
692 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
693 *out_alert = SSL_AD_INTERNAL_ERROR;
694 return 0;
695 }
696
697 if (!CBS_get_u8(cbs, &hash) ||
698 !CBS_get_u8(cbs, &signature)) {
699 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
700 *out_alert = SSL_AD_DECODE_ERROR;
701 return 0;
702 }
703
704 /* Check key type is consistent with signature */
705 if (sigalg != signature) {
706 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
707 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
708 return 0;
709 }
710
711 if (pkey->type == EVP_PKEY_EC) {
712 uint16_t curve_id;
713 uint8_t comp_id;
714 /* Check compression and curve matches extensions */
715 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
716 *out_alert = SSL_AD_INTERNAL_ERROR;
717 return 0;
718 }
719
720 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
721 !tls1_check_point_format(s, comp_id))) {
722 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
723 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
724 return 0;
725 }
726 }
727
728 /* Check signature matches a type we sent */
729 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
730 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
731 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
732 break;
733 }
734 }
735
736 /* Allow fallback to SHA-1. */
737 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
738 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
739 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
740 return 0;
741 }
742
743 *out_md = tls12_get_hash(hash);
744 if (*out_md == NULL) {
745 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
746 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
747 return 0;
748 }
749
750 return 1;
751}
752
753/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
754 * supported or doesn't appear in supported signature algorithms. Unlike
755 * ssl_cipher_get_disabled this applies to a specific session and not global
756 * settings. */
757void ssl_set_client_disabled(SSL *s) {
758 CERT *c = s->cert;
759 const uint8_t *sigalgs;
760 size_t i, sigalgslen;
761 int have_rsa = 0, have_ecdsa = 0;
762 c->mask_a = 0;
763 c->mask_k = 0;
764
765 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
766 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
767 c->mask_ssl = SSL_TLSV1_2;
768 } else {
769 c->mask_ssl = 0;
770 }
771
772 /* Now go through all signature algorithms seeing if we support any for RSA,
773 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
774 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
775 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
776 switch (sigalgs[1]) {
777 case TLSEXT_signature_rsa:
778 have_rsa = 1;
779 break;
780
781 case TLSEXT_signature_ecdsa:
782 have_ecdsa = 1;
783 break;
784 }
785 }
786
787 /* Disable auth if we don't include any appropriate signature algorithms. */
788 if (!have_rsa) {
789 c->mask_a |= SSL_aRSA;
790 }
791 if (!have_ecdsa) {
792 c->mask_a |= SSL_aECDSA;
793 }
794
795 /* with PSK there must be client callback set */
796 if (!s->psk_client_callback) {
797 c->mask_a |= SSL_aPSK;
798 c->mask_k |= SSL_kPSK;
799 }
800}
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyb0c235e2014-06-20 12:00:00 -0700802/* header_len is the length of the ClientHello header written so far, used to
803 * compute padding. It does not include the record header. Pass 0 if no padding
804 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800805uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
806 size_t header_len) {
807 int extdatalen = 0;
808 uint8_t *ret = buf;
809 uint8_t *orig = buf;
810 /* See if we support any ECC ciphersuites */
811 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
814 size_t i;
David Benjamin107db582015-04-08 00:41:59 -0400815 uint32_t alg_k, alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
819 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 alg_k = c->algorithm_mkey;
822 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -0400823 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 using_ecc = 1;
825 break;
826 }
827 }
828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 /* don't add extensions for SSLv3 unless doing secure renegotiation */
831 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
832 return orig;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700836
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 if (ret >= limit) {
838 return NULL; /* should never occur. */
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 if (s->tlsext_hostname != NULL) {
842 /* Add TLS extension servername to the Client Hello message */
843 unsigned long size_str;
844 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 /* check for enough space.
847 4 for the servername type and entension length
848 2 for servernamelist length
849 1 for the hostname type
850 2 for hostname length
851 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 lenmax = limit - ret - 9;
854 size_str = strlen(s->tlsext_hostname);
855 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
856 return NULL;
857 }
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 /* extension type and length */
860 s2n(TLSEXT_TYPE_server_name, ret);
861 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 /* length of servername list */
864 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 /* hostname type, length and hostname */
867 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
868 s2n(size_str, ret);
869 memcpy(ret, s->tlsext_hostname, size_str);
870 ret += size_str;
871 }
Adam Langley75712922014-10-10 16:23:43 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 /* Add RI if renegotiating */
874 if (s->renegotiate) {
875 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500876
Adam Langleyfcf25832014-12-18 17:42:32 -0800877 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
878 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
879 return NULL;
880 }
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 if ((limit - ret - 4 - el) < 0) {
883 return NULL;
884 }
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 s2n(TLSEXT_TYPE_renegotiate, ret);
887 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
890 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
891 return NULL;
892 }
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 ret += el;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 /* Add extended master secret. */
898 if (s->version != SSL3_VERSION) {
899 if (limit - ret - 4 < 0) {
900 return NULL;
901 }
902 s2n(TLSEXT_TYPE_extended_master_secret, ret);
903 s2n(0, ret);
904 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
907 int ticklen = 0;
908 if (!s->new_session && s->session && s->session->tlsext_tick) {
909 ticklen = s->session->tlsext_ticklen;
910 }
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 /* Check for enough room 2 for extension type, 2 for len rest for
913 * ticket. */
914 if ((long)(limit - ret - 4 - ticklen) < 0) {
915 return NULL;
916 }
917 s2n(TLSEXT_TYPE_session_ticket, ret);
918 s2n(ticklen, ret);
919 if (ticklen) {
920 memcpy(ret, s->session->tlsext_tick, ticklen);
921 ret += ticklen;
922 }
923 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700924
David Benjamin6ae7f072015-01-26 10:22:13 -0500925 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 size_t salglen;
927 const uint8_t *salg;
928 salglen = tls12_get_psigalgs(s, &salg);
929 if ((size_t)(limit - ret) < salglen + 6) {
930 return NULL;
931 }
932 s2n(TLSEXT_TYPE_signature_algorithms, ret);
933 s2n(salglen + 2, ret);
934 s2n(salglen, ret);
935 memcpy(ret, salg, salglen);
936 ret += salglen;
937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Adam Langleyfcf25832014-12-18 17:42:32 -0800939 if (s->ocsp_stapling_enabled) {
940 /* The status_request extension is excessively extensible at every layer.
941 * On the client, only support requesting OCSP responses with an empty
942 * responder_id_list and no extensions. */
943 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
944 return NULL;
945 }
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langleyfcf25832014-12-18 17:42:32 -0800947 s2n(TLSEXT_TYPE_status_request, ret);
948 s2n(1 + 2 + 2, ret);
949 /* status_type */
950 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
951 /* responder_id_list - empty */
952 s2n(0, ret);
953 /* request_extensions - empty */
954 s2n(0, ret);
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
David Benjamin78e69782014-12-19 04:52:17 -0500957 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len &&
958 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 /* The client advertises an emtpy extension to indicate its support for
960 * Next Protocol Negotiation */
961 if (limit - ret - 4 < 0) {
962 return NULL;
963 }
964 s2n(TLSEXT_TYPE_next_proto_neg, ret);
965 s2n(0, ret);
966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
969 /* The client advertises an empty extension to indicate its support for
970 * certificate timestamps. */
971 if (limit - ret - 4 < 0) {
972 return NULL;
973 }
974 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
975 s2n(0, ret);
976 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700977
Adam Langleyfcf25832014-12-18 17:42:32 -0800978 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
979 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
980 return NULL;
981 }
982 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
983 s2n(2 + s->alpn_client_proto_list_len, ret);
984 s2n(s->alpn_client_proto_list_len, ret);
985 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
986 ret += s->alpn_client_proto_list_len;
987 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700988
David Benjamin78e69782014-12-19 04:52:17 -0500989 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 /* The client advertises an emtpy extension to indicate its support for
991 * Channel ID. */
992 if (limit - ret - 4 < 0) {
993 return NULL;
994 }
995 if (s->ctx->tlsext_channel_id_enabled_new) {
996 s2n(TLSEXT_TYPE_channel_id_new, ret);
997 } else {
998 s2n(TLSEXT_TYPE_channel_id, ret);
999 }
1000 s2n(0, ret);
1001 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001002
Adam Langleyfcf25832014-12-18 17:42:32 -08001003 if (SSL_get_srtp_profiles(s)) {
1004 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 if ((limit - ret - 4 - el) < 0) {
1009 return NULL;
1010 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 s2n(TLSEXT_TYPE_use_srtp, ret);
1013 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1016 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1017 return NULL;
1018 }
1019 ret += el;
1020 }
Adam Langley95c29f32014-06-20 12:00:00 -07001021
Adam Langleyfcf25832014-12-18 17:42:32 -08001022 if (using_ecc) {
1023 /* Add TLS extension ECPointFormats to the ClientHello message */
1024 long lenmax;
1025 const uint8_t *formats;
1026 const uint16_t *curves;
1027 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 lenmax = limit - ret - 5;
1032 if (lenmax < 0) {
1033 return NULL;
1034 }
1035 if (formats_len > (size_t)lenmax) {
1036 return NULL;
1037 }
1038 if (formats_len > 255) {
1039 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1040 return NULL;
1041 }
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1044 s2n(formats_len + 1, ret);
1045 *(ret++) = (uint8_t)formats_len;
1046 memcpy(ret, formats, formats_len);
1047 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 /* Add TLS extension EllipticCurves to the ClientHello message */
1050 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 lenmax = limit - ret - 6;
1053 if (lenmax < 0) {
1054 return NULL;
1055 }
1056 if (curves_len * 2 > (size_t)lenmax) {
1057 return NULL;
1058 }
1059 if (curves_len * 2 > 65532) {
1060 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1061 return NULL;
1062 }
Adam Langley95c29f32014-06-20 12:00:00 -07001063
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1065 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001066
Adam Langleyfcf25832014-12-18 17:42:32 -08001067 s2n(curves_len * 2, ret);
1068 for (i = 0; i < curves_len; i++) {
1069 s2n(curves[i], ret);
1070 }
1071 }
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 if (header_len > 0) {
1074 size_t clienthello_minsize = 0;
1075 header_len += ret - orig;
1076 if (header_len > 0xff && header_len < 0x200) {
1077 /* Add padding to workaround bugs in F5 terminators. See
1078 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1079 *
1080 * NB: because this code works out the length of all existing extensions
1081 * it MUST always appear last. */
1082 clienthello_minsize = 0x200;
1083 }
1084 if (s->fastradio_padding) {
1085 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1086 * into DCH (high data rate) state in 3G networks. Note that when
1087 * fastradio_padding is enabled, even if the header_len is less than 255
1088 * bytes, the padding will be applied regardless. This is slightly
1089 * different from the TLS padding extension suggested in
1090 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1091 clienthello_minsize = 0x400;
1092 }
1093 if (header_len < clienthello_minsize) {
1094 size_t padding_len = clienthello_minsize - header_len;
1095 /* Extensions take at least four bytes to encode. Always include least
1096 * one byte of data if including the extension. WebSphere Application
1097 * Server 7.0 is intolerant to the last extension being zero-length. */
1098 if (padding_len >= 4 + 1) {
1099 padding_len -= 4;
1100 } else {
1101 padding_len = 1;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 if (limit - ret - 4 - (long)padding_len < 0) {
1105 return NULL;
1106 }
Adam Langley75712922014-10-10 16:23:43 -07001107
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 s2n(TLSEXT_TYPE_padding, ret);
1109 s2n(padding_len, ret);
1110 memset(ret, 0, padding_len);
1111 ret += padding_len;
1112 }
1113 }
Adam Langley75712922014-10-10 16:23:43 -07001114
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 extdatalen = ret - orig - 2;
1116 if (extdatalen == 0) {
1117 return orig;
1118 }
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 s2n(extdatalen, orig);
1121 return ret;
1122}
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1125 int extdatalen = 0;
1126 uint8_t *orig = buf;
1127 uint8_t *ret = buf;
1128 int next_proto_neg_seen;
David Benjamin107db582015-04-08 00:41:59 -04001129 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1130 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001131 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001133
Adam Langleyfcf25832014-12-18 17:42:32 -08001134 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1135 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1136 return orig;
1137 }
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Adam Langleyfcf25832014-12-18 17:42:32 -08001139 ret += 2;
1140 if (ret >= limit) {
1141 return NULL; /* should never happen. */
1142 }
Adam Langley95c29f32014-06-20 12:00:00 -07001143
Adam Langleyfcf25832014-12-18 17:42:32 -08001144 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1145 if ((long)(limit - ret - 4) < 0) {
1146 return NULL;
1147 }
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 s2n(TLSEXT_TYPE_server_name, ret);
1150 s2n(0, ret);
1151 }
Adam Langley95c29f32014-06-20 12:00:00 -07001152
Adam Langleyfcf25832014-12-18 17:42:32 -08001153 if (s->s3->send_connection_binding) {
1154 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1157 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1158 return NULL;
1159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if ((limit - ret - 4 - el) < 0) {
1162 return NULL;
1163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 s2n(TLSEXT_TYPE_renegotiate, ret);
1166 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1169 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1170 return NULL;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 ret += el;
1174 }
Adam Langley95c29f32014-06-20 12:00:00 -07001175
Adam Langleyfcf25832014-12-18 17:42:32 -08001176 if (s->s3->tmp.extended_master_secret) {
1177 if ((long)(limit - ret - 4) < 0) {
1178 return NULL;
1179 }
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langleyfcf25832014-12-18 17:42:32 -08001181 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1182 s2n(0, ret);
1183 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001184
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 if (using_ecc) {
1186 const uint8_t *plist;
1187 size_t plistlen;
1188 /* Add TLS extension ECPointFormats to the ServerHello message */
1189 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 tls1_get_formatlist(s, &plist, &plistlen);
1192
1193 lenmax = limit - ret - 5;
1194 if (lenmax < 0) {
1195 return NULL;
1196 }
1197 if (plistlen > (size_t)lenmax) {
1198 return NULL;
1199 }
1200 if (plistlen > 255) {
1201 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1202 return NULL;
1203 }
1204
1205 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1206 s2n(plistlen + 1, ret);
1207 *(ret++) = (uint8_t)plistlen;
1208 memcpy(ret, plist, plistlen);
1209 ret += plistlen;
1210 }
1211 /* Currently the server should not respond with a SupportedCurves extension */
1212
1213 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1214 if ((long)(limit - ret - 4) < 0) {
1215 return NULL;
1216 }
1217 s2n(TLSEXT_TYPE_session_ticket, ret);
1218 s2n(0, ret);
1219 }
1220
1221 if (s->s3->tmp.certificate_status_expected) {
1222 if ((long)(limit - ret - 4) < 0) {
1223 return NULL;
1224 }
1225 s2n(TLSEXT_TYPE_status_request, ret);
1226 s2n(0, ret);
1227 }
1228
1229 if (s->srtp_profile) {
1230 int el;
1231
1232 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1233
1234 if ((limit - ret - 4 - el) < 0) {
1235 return NULL;
1236 }
1237
1238 s2n(TLSEXT_TYPE_use_srtp, ret);
1239 s2n(el, ret);
1240
1241 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1242 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1243 return NULL;
1244 }
1245 ret += el;
1246 }
1247
1248 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1249 s->s3->next_proto_neg_seen = 0;
1250 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1251 const uint8_t *npa;
1252 unsigned int npalen;
1253 int r;
1254
1255 r = s->ctx->next_protos_advertised_cb(
1256 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1257 if (r == SSL_TLSEXT_ERR_OK) {
1258 if ((long)(limit - ret - 4 - npalen) < 0) {
1259 return NULL;
1260 }
1261 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1262 s2n(npalen, ret);
1263 memcpy(ret, npa, npalen);
1264 ret += npalen;
1265 s->s3->next_proto_neg_seen = 1;
1266 }
1267 }
1268
1269 if (s->s3->alpn_selected) {
1270 const uint8_t *selected = s->s3->alpn_selected;
1271 size_t len = s->s3->alpn_selected_len;
1272
1273 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1274 return NULL;
1275 }
1276 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1277 s2n(3 + len, ret);
1278 s2n(1 + len, ret);
1279 *ret++ = len;
1280 memcpy(ret, selected, len);
1281 ret += len;
1282 }
1283
1284 /* If the client advertised support for Channel ID, and we have it
1285 * enabled, then we want to echo it back. */
1286 if (s->s3->tlsext_channel_id_valid) {
1287 if (limit - ret - 4 < 0) {
1288 return NULL;
1289 }
1290 if (s->s3->tlsext_channel_id_new) {
1291 s2n(TLSEXT_TYPE_channel_id_new, ret);
1292 } else {
1293 s2n(TLSEXT_TYPE_channel_id, ret);
1294 }
1295 s2n(0, ret);
1296 }
1297
1298 extdatalen = ret - orig - 2;
1299 if (extdatalen == 0) {
1300 return orig;
1301 }
1302
1303 s2n(extdatalen, orig);
1304 return ret;
1305}
Adam Langley95c29f32014-06-20 12:00:00 -07001306
Adam Langley95c29f32014-06-20 12:00:00 -07001307/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1308 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001309 * cbs: the contents of the extension, not including the type and length.
1310 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001311 * return.
1312 *
David Benjamindc72ff72014-06-25 12:36:10 -04001313 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001314static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1315 CBS protocol_name_list, protocol_name_list_copy;
1316 const uint8_t *selected;
1317 uint8_t selected_len;
1318 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001319
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 if (s->ctx->alpn_select_cb == NULL) {
1321 return 1;
1322 }
Adam Langley95c29f32014-06-20 12:00:00 -07001323
Adam Langleyfcf25832014-12-18 17:42:32 -08001324 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1325 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1326 goto parse_error;
1327 }
Adam Langley95c29f32014-06-20 12:00:00 -07001328
Adam Langleyfcf25832014-12-18 17:42:32 -08001329 /* Validate the protocol list. */
1330 protocol_name_list_copy = protocol_name_list;
1331 while (CBS_len(&protocol_name_list_copy) > 0) {
1332 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001333
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1335 goto parse_error;
1336 }
1337 }
Adam Langley95c29f32014-06-20 12:00:00 -07001338
Adam Langleyfcf25832014-12-18 17:42:32 -08001339 r = s->ctx->alpn_select_cb(
1340 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1341 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1342 if (r == SSL_TLSEXT_ERR_OK) {
David Benjamin2755a3e2015-04-22 16:17:58 -04001343 OPENSSL_free(s->s3->alpn_selected);
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1345 if (!s->s3->alpn_selected) {
1346 *out_alert = SSL_AD_INTERNAL_ERROR;
1347 return 0;
1348 }
1349 s->s3->alpn_selected_len = selected_len;
1350 }
1351
1352 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001353
1354parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 *out_alert = SSL_AD_DECODE_ERROR;
1356 return 0;
1357}
Adam Langley95c29f32014-06-20 12:00:00 -07001358
Adam Langleyfcf25832014-12-18 17:42:32 -08001359static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1360 int renegotiate_seen = 0;
1361 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001364 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 s->s3->next_proto_neg_seen = 0;
1366 s->s3->tmp.certificate_status_expected = 0;
1367 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001368
David Benjamin2755a3e2015-04-22 16:17:58 -04001369 OPENSSL_free(s->s3->alpn_selected);
1370 s->s3->alpn_selected = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001371
Adam Langleyfcf25832014-12-18 17:42:32 -08001372 /* Clear any signature algorithms extension received */
David Benjamin2755a3e2015-04-22 16:17:58 -04001373 OPENSSL_free(s->cert->peer_sigalgs);
1374 s->cert->peer_sigalgs = NULL;
1375 s->cert->peer_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 /* Clear any shared signature algorithms */
David Benjamin2755a3e2015-04-22 16:17:58 -04001378 OPENSSL_free(s->cert->shared_sigalgs);
1379 s->cert->shared_sigalgs = NULL;
1380 s->cert->shared_sigalgslen = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04001383 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1384 s->s3->tmp.peer_ecpointformatlist = NULL;
1385 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001386
David Benjamin2755a3e2015-04-22 16:17:58 -04001387 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1388 s->s3->tmp.peer_ellipticcurvelist = NULL;
1389 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001390
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 /* There may be no extensions. */
1392 if (CBS_len(cbs) == 0) {
1393 goto ri_check;
1394 }
David Benjamindc72ff72014-06-25 12:36:10 -04001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 /* Decode the extensions block and check it is valid. */
1397 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1398 !tls1_check_duplicate_extensions(&extensions)) {
1399 *out_alert = SSL_AD_DECODE_ERROR;
1400 return 0;
1401 }
David Benjamindc72ff72014-06-25 12:36:10 -04001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 while (CBS_len(&extensions) != 0) {
1404 uint16_t type;
1405 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001406
Adam Langleyfcf25832014-12-18 17:42:32 -08001407 /* Decode the next extension. */
1408 if (!CBS_get_u16(&extensions, &type) ||
1409 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1410 *out_alert = SSL_AD_DECODE_ERROR;
1411 return 0;
1412 }
Adam Langley95c29f32014-06-20 12:00:00 -07001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001415
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 - Only the hostname type is supported with a maximum length of 255.
1417 - The servername is rejected if too long or if it contains zeros, in
1418 which case an fatal alert is generated.
1419 - The servername field is maintained together with the session cache.
1420 - When a session is resumed, the servername call back invoked in order
1421 to allow the application to position itself to the right context.
1422 - The servername is acknowledged if it is new for a session or when
1423 it is identical to a previously used for the same session.
1424 Applications can control the behaviour. They can at any time
1425 set a 'desirable' servername for a new SSL object. This can be the
1426 case for example with HTTPS when a Host: header field is received and
1427 a renegotiation is requested. In this case, a possible servername
1428 presented in the new client hello is only acknowledged if it matches
1429 the value of the Host: field.
1430 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1431 if they provide for changing an explicit servername context for the
1432 session,
1433 i.e. when the session has been established with a servername extension.
1434 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001435
Adam Langleyfcf25832014-12-18 17:42:32 -08001436 if (type == TLSEXT_TYPE_server_name) {
1437 CBS server_name_list;
1438 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001439
Adam Langleyfcf25832014-12-18 17:42:32 -08001440 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1441 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1442 *out_alert = SSL_AD_DECODE_ERROR;
1443 return 0;
1444 }
David Benjamindc72ff72014-06-25 12:36:10 -04001445
Adam Langleyfcf25832014-12-18 17:42:32 -08001446 /* Decode each ServerName in the extension. */
1447 while (CBS_len(&server_name_list) > 0) {
1448 uint8_t name_type;
1449 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001450
Adam Langleyfcf25832014-12-18 17:42:32 -08001451 /* Decode the NameType. */
1452 if (!CBS_get_u8(&server_name_list, &name_type)) {
1453 *out_alert = SSL_AD_DECODE_ERROR;
1454 return 0;
1455 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001456
Adam Langleyfcf25832014-12-18 17:42:32 -08001457 /* Only host_name is supported. */
1458 if (name_type != TLSEXT_NAMETYPE_host_name) {
1459 continue;
1460 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 if (have_seen_host_name) {
1463 /* The ServerNameList MUST NOT contain more than one name of the same
1464 * name_type. */
1465 *out_alert = SSL_AD_DECODE_ERROR;
1466 return 0;
1467 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001470
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1472 CBS_len(&host_name) < 1) {
1473 *out_alert = SSL_AD_DECODE_ERROR;
1474 return 0;
1475 }
Adam Langley95c29f32014-06-20 12:00:00 -07001476
Adam Langleyfcf25832014-12-18 17:42:32 -08001477 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1478 CBS_contains_zero_byte(&host_name)) {
1479 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1480 return 0;
1481 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001482
Adam Langleyfcf25832014-12-18 17:42:32 -08001483 if (!s->hit) {
1484 assert(s->session->tlsext_hostname == NULL);
1485 if (s->session->tlsext_hostname) {
1486 /* This should be impossible. */
1487 *out_alert = SSL_AD_DECODE_ERROR;
1488 return 0;
1489 }
Adam Langley95c29f32014-06-20 12:00:00 -07001490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 /* Copy the hostname as a string. */
1492 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1493 *out_alert = SSL_AD_INTERNAL_ERROR;
1494 return 0;
1495 }
Adam Langley95c29f32014-06-20 12:00:00 -07001496
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 s->should_ack_sni = 1;
1498 }
1499 }
1500 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1501 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1504 CBS_len(&extension) != 0) {
1505 *out_alert = SSL_AD_DECODE_ERROR;
1506 return 0;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1510 &s->s3->tmp.peer_ecpointformatlist_length)) {
1511 *out_alert = SSL_AD_INTERNAL_ERROR;
1512 return 0;
1513 }
1514 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1515 CBS elliptic_curve_list;
1516 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001517
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1519 CBS_len(&elliptic_curve_list) == 0 ||
1520 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1521 CBS_len(&extension) != 0) {
1522 *out_alert = SSL_AD_DECODE_ERROR;
1523 return 0;
1524 }
David Benjamindc72ff72014-06-25 12:36:10 -04001525
David Benjamin2755a3e2015-04-22 16:17:58 -04001526 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1527 s->s3->tmp.peer_ellipticcurvelist_length = 0;
David Benjamindc72ff72014-06-25 12:36:10 -04001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 s->s3->tmp.peer_ellipticcurvelist =
1530 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1533 *out_alert = SSL_AD_INTERNAL_ERROR;
1534 return 0;
1535 }
Adam Langley95c29f32014-06-20 12:00:00 -07001536
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 num_curves = CBS_len(&elliptic_curve_list) / 2;
1538 for (i = 0; i < num_curves; i++) {
1539 if (!CBS_get_u16(&elliptic_curve_list,
1540 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1541 *out_alert = SSL_AD_INTERNAL_ERROR;
1542 return 0;
1543 }
1544 }
David Benjamindc72ff72014-06-25 12:36:10 -04001545
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 if (CBS_len(&elliptic_curve_list) != 0) {
1547 *out_alert = SSL_AD_INTERNAL_ERROR;
1548 return 0;
1549 }
Adam Langley95c29f32014-06-20 12:00:00 -07001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1552 } else if (type == TLSEXT_TYPE_renegotiate) {
1553 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1554 return 0;
1555 }
1556 renegotiate_seen = 1;
1557 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1558 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 if (!CBS_get_u16_length_prefixed(&extension,
1561 &supported_signature_algorithms) ||
1562 CBS_len(&extension) != 0) {
1563 *out_alert = SSL_AD_DECODE_ERROR;
1564 return 0;
1565 }
Adam Langley95c29f32014-06-20 12:00:00 -07001566
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 /* Ensure the signature algorithms are non-empty. It contains a list of
1568 * SignatureAndHashAlgorithms which are two bytes each. */
1569 if (CBS_len(&supported_signature_algorithms) == 0 ||
1570 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1571 *out_alert = SSL_AD_DECODE_ERROR;
1572 return 0;
1573 }
David Benjamindc72ff72014-06-25 12:36:10 -04001574
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1576 *out_alert = SSL_AD_DECODE_ERROR;
1577 return 0;
1578 }
1579 /* If sigalgs received and no shared algorithms fatal error. */
1580 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001581 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1583 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1584 return 0;
1585 }
1586 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001587 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL &&
1588 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001589 /* The extension must be empty. */
1590 if (CBS_len(&extension) != 0) {
1591 *out_alert = SSL_AD_DECODE_ERROR;
1592 return 0;
1593 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001594
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 /* We shouldn't accept this extension on a renegotiation.
1596 *
1597 * s->new_session will be set on renegotiation, but we probably shouldn't
1598 * rely that it couldn't be set on the initial renegotation too in
1599 * certain cases (when there's some other reason to disallow resuming an
1600 * earlier session -- the current code won't be doing anything like that,
1601 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001602
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 * A valid sign that there's been a previous handshake in this connection
1604 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1605 * that will happen in the Hello protocol round, well before a new
1606 * Finished message could have been computed.) */
1607 s->s3->next_proto_neg_seen = 1;
1608 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1609 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1610 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1611 return 0;
1612 }
1613 /* ALPN takes precedence over NPN. */
1614 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001615 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1616 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001617 /* The extension must be empty. */
1618 if (CBS_len(&extension) != 0) {
1619 *out_alert = SSL_AD_DECODE_ERROR;
1620 return 0;
1621 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001622
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 s->s3->tlsext_channel_id_valid = 1;
1624 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001625 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001626 /* The extension must be empty. */
1627 if (CBS_len(&extension) != 0) {
1628 *out_alert = SSL_AD_DECODE_ERROR;
1629 return 0;
1630 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001631
Adam Langleyfcf25832014-12-18 17:42:32 -08001632 s->s3->tlsext_channel_id_valid = 1;
1633 s->s3->tlsext_channel_id_new = 1;
1634 } else if (type == TLSEXT_TYPE_use_srtp) {
1635 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1636 return 0;
1637 }
1638 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1639 s->version != SSL3_VERSION) {
1640 if (CBS_len(&extension) != 0) {
1641 *out_alert = SSL_AD_DECODE_ERROR;
1642 return 0;
1643 }
Adam Langley75712922014-10-10 16:23:43 -07001644
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 s->s3->tmp.extended_master_secret = 1;
1646 }
1647 }
Adam Langley75712922014-10-10 16:23:43 -07001648
Adam Langleyfcf25832014-12-18 17:42:32 -08001649ri_check:
1650 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001651
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 if (!renegotiate_seen && s->renegotiate &&
1653 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1654 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1655 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1656 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1657 return 0;
1658 }
Adam Langley95c29f32014-06-20 12:00:00 -07001659
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 return 1;
1661}
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1664 int alert = -1;
1665 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1666 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1667 return 0;
1668 }
Adam Langley95c29f32014-06-20 12:00:00 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 if (ssl_check_clienthello_tlsext(s) <= 0) {
1671 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1672 SSL_R_CLIENTHELLO_TLSEXT);
1673 return 0;
1674 }
Adam Langley95c29f32014-06-20 12:00:00 -07001675
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 return 1;
1677}
Adam Langley95c29f32014-06-20 12:00:00 -07001678
Adam Langley95c29f32014-06-20 12:00:00 -07001679/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 * elements of zero length are allowed and the set of elements must exactly
1681 * fill the length of the block. */
1682static char ssl_next_proto_validate(const CBS *cbs) {
1683 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 while (CBS_len(&copy) != 0) {
1686 CBS proto;
1687 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1688 return 0;
1689 }
1690 }
Adam Langley95c29f32014-06-20 12:00:00 -07001691
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 return 1;
1693}
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1696 int tlsext_servername = 0;
1697 int renegotiate_seen = 0;
1698 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001699
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 /* TODO(davidben): Move all of these to some per-handshake state that gets
1701 * systematically reset on a new handshake; perhaps allocate it fresh each
1702 * time so it's not even kept around post-handshake. */
1703 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001704 s->tlsext_ticket_expected = 0;
1705 s->s3->tmp.certificate_status_expected = 0;
1706 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001707 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001708
David Benjamin2755a3e2015-04-22 16:17:58 -04001709 OPENSSL_free(s->s3->alpn_selected);
1710 s->s3->alpn_selected = NULL;
David Benjamina19fc252014-10-19 00:14:36 -04001711
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 /* Clear ECC extensions */
David Benjamin2755a3e2015-04-22 16:17:58 -04001713 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1714 s->s3->tmp.peer_ecpointformatlist = NULL;
1715 s->s3->tmp.peer_ecpointformatlist_length = 0;
David Benjamin03973092014-06-24 23:27:17 -04001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 /* There may be no extensions. */
1718 if (CBS_len(cbs) == 0) {
1719 goto ri_check;
1720 }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 /* Decode the extensions block and check it is valid. */
1723 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1724 !tls1_check_duplicate_extensions(&extensions)) {
1725 *out_alert = SSL_AD_DECODE_ERROR;
1726 return 0;
1727 }
Adam Langley95c29f32014-06-20 12:00:00 -07001728
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 while (CBS_len(&extensions) != 0) {
1730 uint16_t type;
1731 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001732
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 /* Decode the next extension. */
1734 if (!CBS_get_u16(&extensions, &type) ||
1735 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1736 *out_alert = SSL_AD_DECODE_ERROR;
1737 return 0;
1738 }
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 if (type == TLSEXT_TYPE_server_name) {
1741 /* The extension must be empty. */
1742 if (CBS_len(&extension) != 0) {
1743 *out_alert = SSL_AD_DECODE_ERROR;
1744 return 0;
1745 }
David Benjamin03973092014-06-24 23:27:17 -04001746
Adam Langleyfcf25832014-12-18 17:42:32 -08001747 /* We must have sent it in ClientHello. */
1748 if (s->tlsext_hostname == NULL) {
1749 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1750 return 0;
1751 }
David Benjamin03973092014-06-24 23:27:17 -04001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 tlsext_servername = 1;
1754 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1755 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001756
Adam Langleyfcf25832014-12-18 17:42:32 -08001757 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1758 CBS_len(&extension) != 0) {
1759 *out_alert = SSL_AD_DECODE_ERROR;
1760 return 0;
1761 }
Adam Langley95c29f32014-06-20 12:00:00 -07001762
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1764 &s->s3->tmp.peer_ecpointformatlist_length)) {
1765 *out_alert = SSL_AD_INTERNAL_ERROR;
1766 return 0;
1767 }
1768 } else if (type == TLSEXT_TYPE_session_ticket) {
1769 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1770 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1771 return 0;
1772 }
David Benjamin03973092014-06-24 23:27:17 -04001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 s->tlsext_ticket_expected = 1;
1775 } else if (type == TLSEXT_TYPE_status_request) {
1776 /* The extension MUST be empty and may only sent if we've requested a
1777 * status request message. */
1778 if (CBS_len(&extension) != 0) {
1779 *out_alert = SSL_AD_DECODE_ERROR;
1780 return 0;
1781 }
David Benjamin03973092014-06-24 23:27:17 -04001782
Adam Langleyfcf25832014-12-18 17:42:32 -08001783 if (!s->ocsp_stapling_enabled) {
1784 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1785 return 0;
1786 }
Adam Langley95c29f32014-06-20 12:00:00 -07001787
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 /* Set a flag to expect a CertificateStatus message */
1789 s->s3->tmp.certificate_status_expected = 1;
1790 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001791 s->s3->tmp.finish_md_len == 0 &&
1792 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001793 uint8_t *selected;
1794 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 /* We must have requested it. */
1797 if (s->ctx->next_proto_select_cb == NULL) {
1798 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1799 return 0;
1800 }
David Benjamin03973092014-06-24 23:27:17 -04001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 /* The data must be valid. */
1803 if (!ssl_next_proto_validate(&extension)) {
1804 *out_alert = SSL_AD_DECODE_ERROR;
1805 return 0;
1806 }
Adam Langley95c29f32014-06-20 12:00:00 -07001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 if (s->ctx->next_proto_select_cb(
1809 s, &selected, &selected_len, CBS_data(&extension),
1810 CBS_len(&extension),
1811 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1812 *out_alert = SSL_AD_INTERNAL_ERROR;
1813 return 0;
1814 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001815
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1817 if (s->next_proto_negotiated == NULL) {
1818 *out_alert = SSL_AD_INTERNAL_ERROR;
1819 return 0;
1820 }
Adam Langley75712922014-10-10 16:23:43 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 s->next_proto_negotiated_len = selected_len;
1823 s->s3->next_proto_neg_seen = 1;
1824 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1825 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001826
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 /* We must have requested it. */
1828 if (s->alpn_client_proto_list == NULL) {
1829 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1830 return 0;
1831 }
Adam Langley95c29f32014-06-20 12:00:00 -07001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 /* The extension data consists of a ProtocolNameList which must have
1834 * exactly one ProtocolName. Each of these is length-prefixed. */
1835 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1836 CBS_len(&extension) != 0 ||
1837 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1838 CBS_len(&protocol_name_list) != 0) {
1839 *out_alert = SSL_AD_DECODE_ERROR;
1840 return 0;
1841 }
Adam Langley95c29f32014-06-20 12:00:00 -07001842
Adam Langleyfcf25832014-12-18 17:42:32 -08001843 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1844 &s->s3->alpn_selected_len)) {
1845 *out_alert = SSL_AD_INTERNAL_ERROR;
1846 return 0;
1847 }
David Benjamin78e69782014-12-19 04:52:17 -05001848 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 if (CBS_len(&extension) != 0) {
1850 *out_alert = SSL_AD_DECODE_ERROR;
1851 return 0;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Adam Langleyfcf25832014-12-18 17:42:32 -08001854 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05001855 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 if (CBS_len(&extension) != 0) {
1857 *out_alert = SSL_AD_DECODE_ERROR;
1858 return 0;
1859 }
Adam Langley95c29f32014-06-20 12:00:00 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 s->s3->tlsext_channel_id_valid = 1;
1862 s->s3->tlsext_channel_id_new = 1;
1863 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1864 if (CBS_len(&extension) == 0) {
1865 *out_alert = SSL_AD_DECODE_ERROR;
1866 return 0;
1867 }
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langleyfcf25832014-12-18 17:42:32 -08001869 /* Session resumption uses the original session information. */
1870 if (!s->hit &&
1871 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1872 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1873 *out_alert = SSL_AD_INTERNAL_ERROR;
1874 return 0;
1875 }
1876 } else if (type == TLSEXT_TYPE_renegotiate) {
1877 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1878 return 0;
1879 }
Adam Langley95c29f32014-06-20 12:00:00 -07001880
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 renegotiate_seen = 1;
1882 } else if (type == TLSEXT_TYPE_use_srtp) {
1883 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1884 return 0;
1885 }
1886 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1887 if (/* It is invalid for the server to select EMS and
1888 SSLv3. */
1889 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1890 *out_alert = SSL_AD_DECODE_ERROR;
1891 return 0;
1892 }
Adam Langley95c29f32014-06-20 12:00:00 -07001893
Adam Langleyfcf25832014-12-18 17:42:32 -08001894 s->s3->tmp.extended_master_secret = 1;
1895 }
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1899 if (s->session->tlsext_hostname == NULL) {
1900 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1901 if (!s->session->tlsext_hostname) {
1902 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1903 return 0;
1904 }
1905 } else {
1906 *out_alert = SSL_AD_DECODE_ERROR;
1907 return 0;
1908 }
1909 }
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langleyfcf25832014-12-18 17:42:32 -08001911ri_check:
1912 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1913 * attack we should *always* see RI even on initial server hello because the
1914 * client doesn't see any renegotiation during an attack. However this would
1915 * mean we could not connect to any server which doesn't support RI so for
1916 * the immediate future tolerate RI absence on initial connect only. */
1917 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1918 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1919 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1920 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1921 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1922 return 0;
1923 }
Adam Langley95c29f32014-06-20 12:00:00 -07001924
Adam Langleyfcf25832014-12-18 17:42:32 -08001925 return 1;
1926}
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001929
Adam Langleyfcf25832014-12-18 17:42:32 -08001930int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001931
Adam Langleyfcf25832014-12-18 17:42:32 -08001932static int ssl_check_clienthello_tlsext(SSL *s) {
1933 int ret = SSL_TLSEXT_ERR_NOACK;
1934 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001935
Adam Langleyfcf25832014-12-18 17:42:32 -08001936 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1937 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001938
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1940 ret = s->ctx->tlsext_servername_callback(s, &al,
1941 s->ctx->tlsext_servername_arg);
1942 } else if (s->initial_ctx != NULL &&
1943 s->initial_ctx->tlsext_servername_callback != 0) {
1944 ret = s->initial_ctx->tlsext_servername_callback(
1945 s, &al, s->initial_ctx->tlsext_servername_arg);
1946 }
Adam Langley95c29f32014-06-20 12:00:00 -07001947
Adam Langleyfcf25832014-12-18 17:42:32 -08001948 switch (ret) {
1949 case SSL_TLSEXT_ERR_ALERT_FATAL:
1950 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1951 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001952
Adam Langleyfcf25832014-12-18 17:42:32 -08001953 case SSL_TLSEXT_ERR_ALERT_WARNING:
1954 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1955 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001956
Adam Langleyfcf25832014-12-18 17:42:32 -08001957 case SSL_TLSEXT_ERR_NOACK:
1958 s->should_ack_sni = 0;
1959 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001960
Adam Langleyfcf25832014-12-18 17:42:32 -08001961 default:
1962 return 1;
1963 }
1964}
Adam Langleyed8270a2014-09-02 13:52:56 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966static int ssl_check_serverhello_tlsext(SSL *s) {
1967 int ret = SSL_TLSEXT_ERR_NOACK;
1968 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 /* If we are client and using an elliptic curve cryptography cipher suite,
1971 * then if server returns an EC point formats lists extension it must contain
1972 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04001973 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1974 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001975 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08001976 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
1977 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
1978 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1979 return -1;
1980 }
1981 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04001982
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1984 ret = s->ctx->tlsext_servername_callback(s, &al,
1985 s->ctx->tlsext_servername_arg);
1986 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04001987 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001988 ret = s->initial_ctx->tlsext_servername_callback(
1989 s, &al, s->initial_ctx->tlsext_servername_arg);
1990 }
Adam Langley95c29f32014-06-20 12:00:00 -07001991
Adam Langleyfcf25832014-12-18 17:42:32 -08001992 switch (ret) {
1993 case SSL_TLSEXT_ERR_ALERT_FATAL:
1994 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1995 return -1;
David Benjamin03973092014-06-24 23:27:17 -04001996
Adam Langleyfcf25832014-12-18 17:42:32 -08001997 case SSL_TLSEXT_ERR_ALERT_WARNING:
1998 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1999 return 1;
2000
2001 default:
2002 return 1;
2003 }
2004}
2005
2006int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2007 int alert = -1;
2008 if (s->version < SSL3_VERSION) {
2009 return 1;
2010 }
2011
2012 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2013 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2014 return 0;
2015 }
2016
2017 if (ssl_check_serverhello_tlsext(s) <= 0) {
2018 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2019 SSL_R_SERVERHELLO_TLSEXT);
2020 return 0;
2021 }
2022
2023 return 1;
2024}
Adam Langley95c29f32014-06-20 12:00:00 -07002025
2026/* Since the server cache lookup is done early on in the processing of the
2027 * ClientHello, and other operations depend on the result, we need to handle
2028 * any TLS session ticket extension at the same time.
2029 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002030 * ctx: contains the early callback context, which is the result of a
2031 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002032 * ret: (output) on return, if a ticket was decrypted, then this is set to
2033 * point to the resulting session.
2034 *
Adam Langley95c29f32014-06-20 12:00:00 -07002035 * Returns:
2036 * -1: fatal error, either from parsing or decrypting the ticket.
2037 * 0: no ticket was found (or was ignored, based on settings).
2038 * 1: a zero length extension was found, indicating that the client supports
2039 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002040 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2041 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002042 * 3: a ticket was successfully decrypted and *ret was set.
2043 *
2044 * Side effects:
2045 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2046 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002047 * but the client either doesn't have a session ticket or we couldn't use
2048 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2049 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002050 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002051int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002052 SSL_SESSION **ret) {
2053 *ret = NULL;
2054 s->tlsext_ticket_expected = 0;
2055 const uint8_t *data;
2056 size_t len;
2057 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002058
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 /* If tickets disabled behave as if no ticket present to permit stateful
2060 * resumption. */
2061 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2062 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2063 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2064 &data, &len)) {
2065 return 0;
2066 }
2067
2068 if (len == 0) {
2069 /* The client will accept a ticket but doesn't currently have one. */
2070 s->tlsext_ticket_expected = 1;
2071 return 1;
2072 }
2073
2074 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2075 ret);
2076 switch (r) {
2077 case 2: /* ticket couldn't be decrypted */
2078 s->tlsext_ticket_expected = 1;
2079 return 2;
2080
2081 case 3: /* ticket was decrypted */
2082 return r;
2083
2084 case 4: /* ticket decrypted but need to renew */
2085 s->tlsext_ticket_expected = 1;
2086 return 3;
2087
2088 default: /* fatal error */
2089 return -1;
2090 }
2091}
Adam Langley95c29f32014-06-20 12:00:00 -07002092
2093/* tls_decrypt_ticket attempts to decrypt a session ticket.
2094 *
2095 * etick: points to the body of the session ticket extension.
2096 * eticklen: the length of the session tickets extenion.
2097 * sess_id: points at the session ID.
2098 * sesslen: the length of the session ID.
2099 * psess: (output) on return, if a ticket was decrypted, then this is set to
2100 * point to the resulting session.
2101 *
2102 * Returns:
2103 * -1: fatal error, either from parsing or decrypting the ticket.
2104 * 2: the ticket couldn't be decrypted.
2105 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002106 * 4: same as 3, but the ticket needs to be renewed. */
2107static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2108 const uint8_t *sess_id, int sesslen,
2109 SSL_SESSION **psess) {
2110 SSL_SESSION *sess;
2111 uint8_t *sdec;
2112 const uint8_t *p;
2113 int slen, mlen, renew_ticket = 0;
2114 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2115 HMAC_CTX hctx;
2116 EVP_CIPHER_CTX ctx;
2117 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002118
David Benjaminadcc3952015-04-26 13:07:57 -04002119 /* Ensure there is room for the key name and the largest IV
2120 * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
2121 * the maximum IV length should be well under the minimum size for the
2122 * session material and HMAC. */
2123 if (eticklen < 16 + EVP_MAX_IV_LENGTH) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002124 return 2;
2125 }
2126
2127 /* Initialize session ticket encryption and HMAC contexts */
2128 HMAC_CTX_init(&hctx);
2129 EVP_CIPHER_CTX_init(&ctx);
2130 if (tctx->tlsext_ticket_key_cb) {
2131 uint8_t *nctick = (uint8_t *)etick;
David Benjaminadcc3952015-04-26 13:07:57 -04002132 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx,
2133 0 /* decrypt */);
Adam Langleyfcf25832014-12-18 17:42:32 -08002134 if (rv < 0) {
2135 return -1;
2136 }
2137 if (rv == 0) {
2138 return 2;
2139 }
2140 if (rv == 2) {
2141 renew_ticket = 1;
2142 }
2143 } else {
2144 /* Check key name matches */
2145 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2146 return 2;
2147 }
2148 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2149 NULL) ||
2150 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2151 tctx->tlsext_tick_aes_key, etick + 16)) {
2152 HMAC_CTX_cleanup(&hctx);
2153 EVP_CIPHER_CTX_cleanup(&ctx);
2154 return -1;
2155 }
2156 }
2157
David Benjaminadcc3952015-04-26 13:07:57 -04002158 /* First, check the MAC. The MAC is at the end of the ticket. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002159 mlen = HMAC_size(&hctx);
David Benjaminadcc3952015-04-26 13:07:57 -04002160 if (eticklen < 16 + EVP_CIPHER_CTX_iv_length(&ctx) + 1 + mlen) {
2161 /* The ticket must be large enough for key name, IV, data, and MAC. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002162 HMAC_CTX_cleanup(&hctx);
2163 EVP_CIPHER_CTX_cleanup(&ctx);
David Benjaminadcc3952015-04-26 13:07:57 -04002164 return 2;
Adam Langleyfcf25832014-12-18 17:42:32 -08002165 }
2166 eticklen -= mlen;
2167 /* Check HMAC of encrypted ticket */
2168 HMAC_Update(&hctx, etick, eticklen);
2169 HMAC_Final(&hctx, tick_hmac, NULL);
2170 HMAC_CTX_cleanup(&hctx);
2171 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2172 EVP_CIPHER_CTX_cleanup(&ctx);
2173 return 2;
2174 }
2175
2176 /* Attempt to decrypt session data */
2177 /* Move p after IV to start of encrypted ticket, update length */
2178 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2179 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2180 sdec = OPENSSL_malloc(eticklen);
2181 if (!sdec) {
2182 EVP_CIPHER_CTX_cleanup(&ctx);
2183 return -1;
2184 }
2185 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2186 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2187 EVP_CIPHER_CTX_cleanup(&ctx);
2188 OPENSSL_free(sdec);
2189 return 2;
2190 }
2191 slen += mlen;
2192 EVP_CIPHER_CTX_cleanup(&ctx);
2193 p = sdec;
2194
2195 sess = d2i_SSL_SESSION(NULL, &p, slen);
2196 OPENSSL_free(sdec);
2197 if (sess) {
2198 /* The session ID, if non-empty, is used by some clients to detect that the
2199 * ticket has been accepted. So we copy it to the session structure. If it
2200 * is empty set length to zero as required by standard. */
2201 if (sesslen) {
2202 memcpy(sess->session_id, sess_id, sesslen);
2203 }
2204 sess->session_id_length = sesslen;
2205 *psess = sess;
2206 if (renew_ticket) {
2207 return 4;
2208 }
2209 return 3;
2210 }
2211
2212 ERR_clear_error();
2213 /* For session parse failure, indicate that we need to send a new ticket. */
2214 return 2;
2215}
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002218typedef struct {
2219 int nid;
2220 int id;
2221} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002222
Adam Langleyfcf25832014-12-18 17:42:32 -08002223static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2224 {NID_sha1, TLSEXT_hash_sha1},
2225 {NID_sha224, TLSEXT_hash_sha224},
2226 {NID_sha256, TLSEXT_hash_sha256},
2227 {NID_sha384, TLSEXT_hash_sha384},
2228 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002229
Adam Langleyfcf25832014-12-18 17:42:32 -08002230static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2231 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002232
Adam Langleyfcf25832014-12-18 17:42:32 -08002233static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2234 size_t i;
2235 for (i = 0; i < tlen; i++) {
2236 if (table[i].nid == nid) {
2237 return table[i].id;
2238 }
2239 }
Adam Langley95c29f32014-06-20 12:00:00 -07002240
Adam Langleyfcf25832014-12-18 17:42:32 -08002241 return -1;
2242}
Adam Langley95c29f32014-06-20 12:00:00 -07002243
Adam Langleyfcf25832014-12-18 17:42:32 -08002244static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2245 size_t i;
2246 for (i = 0; i < tlen; i++) {
2247 if (table[i].id == id) {
2248 return table[i].nid;
2249 }
2250 }
Adam Langley95c29f32014-06-20 12:00:00 -07002251
Adam Langleyfcf25832014-12-18 17:42:32 -08002252 return NID_undef;
2253}
Adam Langley95c29f32014-06-20 12:00:00 -07002254
Adam Langleyfcf25832014-12-18 17:42:32 -08002255int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2256 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002257
Adam Langleyfcf25832014-12-18 17:42:32 -08002258 if (!md) {
2259 return 0;
2260 }
Adam Langley95c29f32014-06-20 12:00:00 -07002261
Adam Langleyfcf25832014-12-18 17:42:32 -08002262 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2263 sizeof(tls12_md) / sizeof(tls12_lookup));
2264 if (md_id == -1) {
2265 return 0;
2266 }
Adam Langley95c29f32014-06-20 12:00:00 -07002267
Adam Langleyfcf25832014-12-18 17:42:32 -08002268 sig_id = tls12_get_sigid(pk);
2269 if (sig_id == -1) {
2270 return 0;
2271 }
Adam Langley95c29f32014-06-20 12:00:00 -07002272
Adam Langleyfcf25832014-12-18 17:42:32 -08002273 p[0] = (uint8_t)md_id;
2274 p[1] = (uint8_t)sig_id;
2275 return 1;
2276}
2277
2278int tls12_get_sigid(const EVP_PKEY *pk) {
2279 return tls12_find_id(pk->type, tls12_sig,
2280 sizeof(tls12_sig) / sizeof(tls12_lookup));
2281}
2282
2283const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2284 switch (hash_alg) {
2285 case TLSEXT_hash_md5:
2286 return EVP_md5();
2287
2288 case TLSEXT_hash_sha1:
2289 return EVP_sha1();
2290
2291 case TLSEXT_hash_sha224:
2292 return EVP_sha224();
2293
2294 case TLSEXT_hash_sha256:
2295 return EVP_sha256();
2296
2297 case TLSEXT_hash_sha384:
2298 return EVP_sha384();
2299
2300 case TLSEXT_hash_sha512:
2301 return EVP_sha512();
2302
2303 default:
2304 return NULL;
2305 }
2306}
Adam Langley95c29f32014-06-20 12:00:00 -07002307
David Benjaminec2f27d2014-11-13 19:17:25 -05002308/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2309 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002310static int tls12_get_pkey_type(uint8_t sig_alg) {
2311 switch (sig_alg) {
2312 case TLSEXT_signature_rsa:
2313 return EVP_PKEY_RSA;
2314
2315 case TLSEXT_signature_ecdsa:
2316 return EVP_PKEY_EC;
2317
2318 default:
2319 return -1;
2320 }
2321}
Adam Langley95c29f32014-06-20 12:00:00 -07002322
2323/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2324static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002325 int *psignhash_nid, const uint8_t *data) {
2326 int sign_nid = 0, hash_nid = 0;
2327 if (!phash_nid && !psign_nid && !psignhash_nid) {
2328 return;
2329 }
2330
2331 if (phash_nid || psignhash_nid) {
2332 hash_nid = tls12_find_nid(data[0], tls12_md,
2333 sizeof(tls12_md) / sizeof(tls12_lookup));
2334 if (phash_nid) {
2335 *phash_nid = hash_nid;
2336 }
2337 }
2338
2339 if (psign_nid || psignhash_nid) {
2340 sign_nid = tls12_find_nid(data[1], tls12_sig,
2341 sizeof(tls12_sig) / sizeof(tls12_lookup));
2342 if (psign_nid) {
2343 *psign_nid = sign_nid;
2344 }
2345 }
2346
2347 if (psignhash_nid) {
2348 if (sign_nid && hash_nid) {
2349 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2350 } else {
2351 *psignhash_nid = NID_undef;
2352 }
2353 }
2354}
2355
Adam Langley95c29f32014-06-20 12:00:00 -07002356/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002357static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2358 size_t preflen, const uint8_t *allow,
2359 size_t allowlen) {
2360 const uint8_t *ptmp, *atmp;
2361 size_t i, j, nmatch = 0;
2362
2363 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2364 /* Skip disabled hashes or signature algorithms */
2365 if (tls12_get_hash(ptmp[0]) == NULL ||
2366 tls12_get_pkey_type(ptmp[1]) == -1) {
2367 continue;
2368 }
2369
2370 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2371 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2372 nmatch++;
2373 if (shsig) {
2374 shsig->rhash = ptmp[0];
2375 shsig->rsign = ptmp[1];
2376 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2377 &shsig->signandhash_nid, ptmp);
2378 shsig++;
2379 }
2380
2381 break;
2382 }
2383 }
2384 }
2385
2386 return nmatch;
2387}
Adam Langley95c29f32014-06-20 12:00:00 -07002388
2389/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002390static int tls1_set_shared_sigalgs(SSL *s) {
2391 const uint8_t *pref, *allow, *conf;
2392 size_t preflen, allowlen, conflen;
2393 size_t nmatch;
2394 TLS_SIGALGS *salgs = NULL;
2395 CERT *c = s->cert;
2396
David Benjamin2755a3e2015-04-22 16:17:58 -04002397 OPENSSL_free(c->shared_sigalgs);
2398 c->shared_sigalgs = NULL;
2399 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002400
2401 /* If client use client signature algorithms if not NULL */
2402 if (!s->server && c->client_sigalgs) {
2403 conf = c->client_sigalgs;
2404 conflen = c->client_sigalgslen;
2405 } else if (c->conf_sigalgs) {
2406 conf = c->conf_sigalgs;
2407 conflen = c->conf_sigalgslen;
2408 } else {
2409 conflen = tls12_get_psigalgs(s, &conf);
2410 }
2411
2412 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2413 pref = conf;
2414 preflen = conflen;
2415 allow = c->peer_sigalgs;
2416 allowlen = c->peer_sigalgslen;
2417 } else {
2418 allow = conf;
2419 allowlen = conflen;
2420 pref = c->peer_sigalgs;
2421 preflen = c->peer_sigalgslen;
2422 }
2423
2424 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2425 if (!nmatch) {
2426 return 1;
2427 }
2428
2429 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2430 if (!salgs) {
2431 return 0;
2432 }
2433
2434 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2435 c->shared_sigalgs = salgs;
2436 c->shared_sigalgslen = nmatch;
2437 return 1;
2438}
Adam Langley95c29f32014-06-20 12:00:00 -07002439
2440/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002441int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2442 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002443
Adam Langleyfcf25832014-12-18 17:42:32 -08002444 /* Extension ignored for inappropriate versions */
2445 if (!SSL_USE_SIGALGS(s)) {
2446 return 1;
2447 }
David Benjamincd996942014-07-20 16:23:51 -04002448
David Benjamin61c0d4e2015-03-19 14:24:37 -04002449 if (CBS_len(sigalgs) % 2 != 0 ||
2450 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2451 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002452 return 0;
2453 }
Adam Langley95c29f32014-06-20 12:00:00 -07002454
Adam Langleyfcf25832014-12-18 17:42:32 -08002455 return 1;
2456}
David Benjaminec2f27d2014-11-13 19:17:25 -05002457
Adam Langleyfcf25832014-12-18 17:42:32 -08002458const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2459 CERT *c = s->cert;
2460 int type = EVP_PKEY_id(pkey);
2461 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002462
Adam Langleyfcf25832014-12-18 17:42:32 -08002463 /* Select the first shared digest supported by our key. */
2464 for (i = 0; i < c->shared_sigalgslen; i++) {
2465 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2466 if (md == NULL ||
2467 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2468 !EVP_PKEY_supports_digest(pkey, md)) {
2469 continue;
2470 }
2471 return md;
2472 }
Adam Langley95c29f32014-06-20 12:00:00 -07002473
Adam Langleyfcf25832014-12-18 17:42:32 -08002474 /* If no suitable digest may be found, default to SHA-1. */
2475 return EVP_sha1();
2476}
Adam Langley95c29f32014-06-20 12:00:00 -07002477
Adam Langleyfcf25832014-12-18 17:42:32 -08002478int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2479 uint8_t *rsig, uint8_t *rhash) {
2480 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002481
Adam Langleyfcf25832014-12-18 17:42:32 -08002482 if (psig == NULL) {
2483 return 0;
2484 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002485
Adam Langleyfcf25832014-12-18 17:42:32 -08002486 if (idx >= 0) {
2487 idx <<= 1;
2488 if (idx >= (int)s->cert->peer_sigalgslen) {
2489 return 0;
2490 }
2491 psig += idx;
2492 if (rhash) {
2493 *rhash = psig[0];
2494 }
2495 if (rsig) {
2496 *rsig = psig[1];
2497 }
2498 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2499 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002500
Adam Langleyfcf25832014-12-18 17:42:32 -08002501 return s->cert->peer_sigalgslen / 2;
2502}
Adam Langley1258b6a2014-06-20 12:00:00 -07002503
Adam Langleyfcf25832014-12-18 17:42:32 -08002504int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2505 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2506 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002507
Adam Langleyfcf25832014-12-18 17:42:32 -08002508 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2509 return 0;
2510 }
2511
2512 shsigalgs += idx;
2513 if (phash) {
2514 *phash = shsigalgs->hash_nid;
2515 }
2516 if (psign) {
2517 *psign = shsigalgs->sign_nid;
2518 }
2519 if (psignhash) {
2520 *psignhash = shsigalgs->signandhash_nid;
2521 }
2522 if (rsig) {
2523 *rsig = shsigalgs->rsign;
2524 }
2525 if (rhash) {
2526 *rhash = shsigalgs->rhash;
2527 }
2528
2529 return s->cert->shared_sigalgslen;
2530}
2531
2532/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2533 * given SSL connection and writes it to |md|. */
2534int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2535 EVP_MD_CTX ctx;
2536 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2537 unsigned temp_digest_len;
2538 int i;
2539 static const char kClientIDMagic[] = "TLS Channel ID signature";
2540
2541 if (s->s3->handshake_buffer &&
2542 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2543 return 0;
2544 }
2545
2546 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2547
2548 if (s->hit && s->s3->tlsext_channel_id_new) {
2549 static const char kResumptionMagic[] = "Resumption";
2550 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2551 if (s->session->original_handshake_hash_len == 0) {
2552 return 0;
2553 }
2554 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2555 s->session->original_handshake_hash_len);
2556 }
2557
2558 EVP_MD_CTX_init(&ctx);
2559 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2560 if (s->s3->handshake_dgst[i] == NULL) {
2561 continue;
2562 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002563 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2564 EVP_MD_CTX_cleanup(&ctx);
2565 return 0;
2566 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002567 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2568 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2569 }
2570 EVP_MD_CTX_cleanup(&ctx);
2571
2572 return 1;
2573}
Adam Langley1258b6a2014-06-20 12:00:00 -07002574
2575/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2576 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002577int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2578 int digest_len;
2579 /* This function should never be called for a resumed session because the
2580 * handshake hashes that we wish to record are for the original, full
2581 * handshake. */
2582 if (s->hit) {
2583 return -1;
2584 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002585
Adam Langleyfcf25832014-12-18 17:42:32 -08002586 /* It only makes sense to call this function if Channel IDs have been
2587 * negotiated. */
2588 if (!s->s3->tlsext_channel_id_new) {
2589 return -1;
2590 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002591
Adam Langleyfcf25832014-12-18 17:42:32 -08002592 digest_len =
2593 tls1_handshake_digest(s, s->session->original_handshake_hash,
2594 sizeof(s->session->original_handshake_hash));
2595 if (digest_len < 0) {
2596 return -1;
2597 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002598
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002600
Adam Langleyfcf25832014-12-18 17:42:32 -08002601 return 1;
2602}
Adam Langley95c29f32014-06-20 12:00:00 -07002603
Adam Langleyfcf25832014-12-18 17:42:32 -08002604int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2605 int client) {
2606 uint8_t *sigalgs, *sptr;
2607 int rhash, rsign;
2608 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002609
Adam Langleyfcf25832014-12-18 17:42:32 -08002610 if (salglen & 1) {
2611 return 0;
2612 }
Adam Langley95c29f32014-06-20 12:00:00 -07002613
Adam Langleyfcf25832014-12-18 17:42:32 -08002614 sigalgs = OPENSSL_malloc(salglen);
2615 if (sigalgs == NULL) {
2616 return 0;
2617 }
Adam Langley95c29f32014-06-20 12:00:00 -07002618
Adam Langleyfcf25832014-12-18 17:42:32 -08002619 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2620 rhash = tls12_find_id(*psig_nids++, tls12_md,
2621 sizeof(tls12_md) / sizeof(tls12_lookup));
2622 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2623 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002624
Adam Langleyfcf25832014-12-18 17:42:32 -08002625 if (rhash == -1 || rsign == -1) {
2626 goto err;
2627 }
2628 *sptr++ = rhash;
2629 *sptr++ = rsign;
2630 }
2631
2632 if (client) {
David Benjamin2755a3e2015-04-22 16:17:58 -04002633 OPENSSL_free(c->client_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002634 c->client_sigalgs = sigalgs;
2635 c->client_sigalgslen = salglen;
2636 } else {
David Benjamin2755a3e2015-04-22 16:17:58 -04002637 OPENSSL_free(c->conf_sigalgs);
Adam Langleyfcf25832014-12-18 17:42:32 -08002638 c->conf_sigalgs = sigalgs;
2639 c->conf_sigalgslen = salglen;
2640 }
2641
2642 return 1;
2643
2644err:
2645 OPENSSL_free(sigalgs);
2646 return 0;
2647}