blob: fdbb34059f37a3594b2829b8851e0871e62ab3ca [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 Benjamin6eb000d2015-02-11 01:17:41 -0500248 if (extension_types) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 OPENSSL_free(extension_types);
David Benjamin6eb000d2015-02-11 01:17:41 -0500250 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 return ret;
252}
David Benjamin35a7a442014-07-05 00:23:20 -0400253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) {
255 CBS client_hello, session_id, cipher_suites, compression_methods, extensions;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 if (/* Skip client version. */
260 !CBS_skip(&client_hello, 2) ||
261 /* Skip client nonce. */
262 !CBS_skip(&client_hello, 32) ||
263 /* Extract session_id. */
264 !CBS_get_u8_length_prefixed(&client_hello, &session_id)) {
265 return 0;
266 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700267
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 ctx->session_id = CBS_data(&session_id);
269 ctx->session_id_len = CBS_len(&session_id);
Adam Langleydc9b1412014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 /* Skip past DTLS cookie */
272 if (SSL_IS_DTLS(ctx->ssl)) {
273 CBS cookie;
Adam Langleydc9b1412014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) {
276 return 0;
277 }
278 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 /* Extract cipher_suites. */
281 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
282 CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) {
283 return 0;
284 }
285 ctx->cipher_suites = CBS_data(&cipher_suites);
286 ctx->cipher_suites_len = CBS_len(&cipher_suites);
Adam Langleydc9b1412014-06-20 12:00:00 -0700287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 /* Extract compression_methods. */
289 if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
290 CBS_len(&compression_methods) < 1) {
291 return 0;
292 }
293 ctx->compression_methods = CBS_data(&compression_methods);
294 ctx->compression_methods_len = CBS_len(&compression_methods);
Adam Langleydc9b1412014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 /* If the ClientHello ends here then it's valid, but doesn't have any
297 * extensions. (E.g. SSLv3.) */
298 if (CBS_len(&client_hello) == 0) {
299 ctx->extensions = NULL;
300 ctx->extensions_len = 0;
301 return 1;
302 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 /* Extract extensions and check it is valid. */
305 if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
306 !tls1_check_duplicate_extensions(&extensions) ||
307 CBS_len(&client_hello) != 0) {
308 return 0;
309 }
310 ctx->extensions = CBS_data(&extensions);
311 ctx->extensions_len = CBS_len(&extensions);
Adam Langleydc9b1412014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 return 1;
314}
Adam Langleydc9b1412014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316char SSL_early_callback_ctx_extension_get(
317 const struct ssl_early_callback_ctx *ctx, uint16_t extension_type,
318 const uint8_t **out_data, size_t *out_len) {
319 CBS extensions;
Adam Langleydc9b1412014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 CBS_init(&extensions, ctx->extensions, ctx->extensions_len);
Adam Langleydc9b1412014-06-20 12:00:00 -0700322
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 while (CBS_len(&extensions) != 0) {
324 uint16_t type;
325 CBS extension;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 /* Decode the next extension. */
328 if (!CBS_get_u16(&extensions, &type) ||
329 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
330 return 0;
331 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (type == extension_type) {
334 *out_data = CBS_data(&extension);
335 *out_len = CBS_len(&extension);
336 return 1;
337 }
338 }
Adam Langleydc9b1412014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 return 0;
341}
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamin52e5bac2014-12-27 02:27:35 -0500343struct tls_curve {
344 uint16_t curve_id;
345 int nid;
346};
347
David Benjamin70bd80a2014-12-27 03:06:46 -0500348/* ECC curves from RFC4492. */
David Benjamin52e5bac2014-12-27 02:27:35 -0500349static const struct tls_curve tls_curves[] = {
350 {21, NID_secp224r1},
351 {23, NID_X9_62_prime256v1},
352 {24, NID_secp384r1},
353 {25, NID_secp521r1},
Adam Langleyfcf25832014-12-18 17:42:32 -0800354};
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langleyfcf25832014-12-18 17:42:32 -0800356static const uint8_t ecformats_default[] = {
357 TLSEXT_ECPOINTFORMAT_uncompressed,
358};
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360static const uint16_t eccurves_default[] = {
David Benjamin55a43642015-04-20 14:45:55 -0400361 23, /* X9_62_prime256v1 */
David Benjamin52e5bac2014-12-27 02:27:35 -0500362 24, /* secp384r1 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800363};
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365int tls1_ec_curve_id2nid(uint16_t curve_id) {
David Benjamin52e5bac2014-12-27 02:27:35 -0500366 size_t i;
367 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
368 if (curve_id == tls_curves[i].curve_id) {
369 return tls_curves[i].nid;
370 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 }
David Benjamin52e5bac2014-12-27 02:27:35 -0500372 return NID_undef;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373}
Adam Langley95c29f32014-06-20 12:00:00 -0700374
David Benjamin70bd80a2014-12-27 03:06:46 -0500375int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 size_t i;
David Benjamin52e5bac2014-12-27 02:27:35 -0500377 for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) {
378 if (nid == tls_curves[i].nid) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500379 *out_curve_id = tls_curves[i].curve_id;
380 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 }
382 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 return 0;
384}
385
386/* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the
387 * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the
388 * peer's curve list. Otherwise, return the preferred list. */
David Benjamin42e9a772014-09-02 23:18:44 -0400389static void tls1_get_curvelist(SSL *s, int get_peer_curves,
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 const uint16_t **out_curve_ids,
391 size_t *out_curve_ids_len) {
392 if (get_peer_curves) {
David Benjamin55a43642015-04-20 14:45:55 -0400393 /* Only clients send a curve list, so this function is only called
394 * on the server. */
395 assert(s->server);
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist;
397 *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length;
398 return;
399 }
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 *out_curve_ids = s->tlsext_ellipticcurvelist;
402 *out_curve_ids_len = s->tlsext_ellipticcurvelist_length;
403 if (!*out_curve_ids) {
404 *out_curve_ids = eccurves_default;
405 *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]);
406 }
407}
David Benjamined439582014-07-14 19:13:02 -0400408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) {
410 uint8_t curve_type;
411 uint16_t curve_id;
412 const uint16_t *curves;
413 size_t curves_len, i;
David Benjamined439582014-07-14 19:13:02 -0400414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 /* Only support named curves. */
416 if (!CBS_get_u8(cbs, &curve_type) ||
417 curve_type != NAMED_CURVE_TYPE ||
418 !CBS_get_u16(cbs, &curve_id)) {
419 return 0;
420 }
David Benjamined439582014-07-14 19:13:02 -0400421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 tls1_get_curvelist(s, 0, &curves, &curves_len);
423 for (i = 0; i < curves_len; i++) {
424 if (curve_id == curves[i]) {
425 *out_curve_id = curve_id;
426 return 1;
427 }
428 }
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 return 0;
431}
David Benjamin072334d2014-07-13 16:24:27 -0400432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433int tls1_get_shared_curve(SSL *s) {
David Benjamin55a43642015-04-20 14:45:55 -0400434 const uint16_t *curves, *peer_curves, *pref, *supp;
435 size_t curves_len, peer_curves_len, pref_len, supp_len, i, j;
David Benjamin072334d2014-07-13 16:24:27 -0400436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 /* Can't do anything on client side */
438 if (s->server == 0) {
439 return NID_undef;
440 }
441
David Benjamin55a43642015-04-20 14:45:55 -0400442 tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len);
443 tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800444
David Benjamin55a43642015-04-20 14:45:55 -0400445 if (peer_curves_len == 0) {
446 /* Clients are not required to send a supported_curves extension. In this
447 * case, the server is free to pick any curve it likes. See RFC 4492,
448 * section 4, paragraph 3. */
449 return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]);
450 }
451
452 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
453 pref = curves;
454 pref_len = curves_len;
455 supp = peer_curves;
456 supp_len = peer_curves_len;
457 } else {
458 pref = peer_curves;
459 pref_len = peer_curves_len;
460 supp = curves;
461 supp_len = curves_len;
462 }
463
464 for (i = 0; i < pref_len; i++) {
465 for (j = 0; j < supp_len; j++) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (pref[i] == supp[j]) {
467 return tls1_ec_curve_id2nid(pref[i]);
468 }
469 }
470 }
471
472 return NID_undef;
473}
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin072334d2014-07-13 16:24:27 -0400475int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len,
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 const int *curves, size_t ncurves) {
477 uint16_t *curve_ids;
478 size_t i;
479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
481 if (curve_ids == NULL) {
482 return 0;
483 }
484
485 for (i = 0; i < ncurves; i++) {
David Benjamin70bd80a2014-12-27 03:06:46 -0500486 if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 OPENSSL_free(curve_ids);
488 return 0;
489 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 }
491
492 if (*out_curve_ids) {
493 OPENSSL_free(*out_curve_ids);
494 }
495 *out_curve_ids = curve_ids;
496 *out_curve_ids_len = ncurves;
497
498 return 1;
499}
Adam Langley95c29f32014-06-20 12:00:00 -0700500
David Benjamin072334d2014-07-13 16:24:27 -0400501/* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the
502 * TLS curve ID and point format, respectively, for |ec|. It returns one on
503 * success and zero on failure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800504static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id,
505 uint8_t *out_comp_id, EC_KEY *ec) {
506 int nid;
507 uint16_t id;
508 const EC_GROUP *grp;
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 if (ec == NULL) {
511 return 0;
512 }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 grp = EC_KEY_get0_group(ec);
515 if (grp == NULL) {
516 return 0;
517 }
David Benjamin072334d2014-07-13 16:24:27 -0400518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 /* Determine curve ID */
520 nid = EC_GROUP_get_curve_name(grp);
David Benjamin70bd80a2014-12-27 03:06:46 -0500521 if (!tls1_ec_nid2curve_id(&id, nid)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 return 0;
523 }
David Benjamin072334d2014-07-13 16:24:27 -0400524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 /* Set the named curve ID. Arbitrary explicit curves are not supported. */
526 *out_curve_id = id;
527
528 if (out_comp_id) {
529 if (EC_KEY_get0_public_key(ec) == NULL) {
530 return 0;
531 }
532 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
533 *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
534 } else {
535 *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
536 }
537 }
538
539 return 1;
540}
David Benjamin072334d2014-07-13 16:24:27 -0400541
David Benjamin42e9a772014-09-02 23:18:44 -0400542/* tls1_check_point_format returns one if |comp_id| is consistent with the
543 * peer's point format preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800544static int tls1_check_point_format(SSL *s, uint8_t comp_id) {
545 uint8_t *p = s->s3->tmp.peer_ecpointformatlist;
546 size_t plen = s->s3->tmp.peer_ecpointformatlist_length;
547 size_t i;
David Benjamin42e9a772014-09-02 23:18:44 -0400548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 /* If point formats extension present check it, otherwise everything is
550 * supported (see RFC4492). */
551 if (p == NULL) {
552 return 1;
553 }
David Benjamin42e9a772014-09-02 23:18:44 -0400554
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 for (i = 0; i < plen; i++) {
556 if (comp_id == p[i]) {
557 return 1;
558 }
559 }
David Benjamin42e9a772014-09-02 23:18:44 -0400560
Adam Langleyfcf25832014-12-18 17:42:32 -0800561 return 0;
562}
563
564/* tls1_check_curve_id returns one if |curve_id| is consistent with both our
565 * and the peer's curve preferences. Note: if called as the client, only our
David Benjamin42e9a772014-09-02 23:18:44 -0400566 * preferences are checked; the peer (the server) does not send preferences. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800567static int tls1_check_curve_id(SSL *s, uint16_t curve_id) {
568 const uint16_t *curves;
David Benjamin55a43642015-04-20 14:45:55 -0400569 size_t curves_len, i, get_peer_curves;
David Benjamin42e9a772014-09-02 23:18:44 -0400570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 /* Check against our list, then the peer's list. */
David Benjamin55a43642015-04-20 14:45:55 -0400572 for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) {
573 if (get_peer_curves && !s->server) {
574 /* Servers do not present a preference list so, if we are a client, only
575 * check our list. */
576 continue;
577 }
578
579 tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len);
580 if (get_peer_curves && curves_len == 0) {
581 /* Clients are not required to send a supported_curves extension. In this
582 * case, the server is free to pick any curve it likes. See RFC 4492,
583 * section 4, paragraph 3. */
584 continue;
585 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 for (i = 0; i < curves_len; i++) {
587 if (curves[i] == curve_id) {
588 break;
589 }
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 if (i == curves_len) {
593 return 0;
594 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 }
David Benjamin033e5f42014-11-13 18:47:41 -0500596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 return 1;
598}
David Benjamin033e5f42014-11-13 18:47:41 -0500599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600static void tls1_get_formatlist(SSL *s, const uint8_t **pformats,
601 size_t *pformatslen) {
602 /* If we have a custom point format list use it otherwise use default */
603 if (s->tlsext_ecpointformatlist) {
604 *pformats = s->tlsext_ecpointformatlist;
605 *pformatslen = s->tlsext_ecpointformatlist_length;
606 } else {
607 *pformats = ecformats_default;
608 *pformatslen = sizeof(ecformats_default);
609 }
610}
611
612int tls1_check_ec_cert(SSL *s, X509 *x) {
613 int ret = 0;
614 EVP_PKEY *pkey = X509_get_pubkey(x);
615 uint16_t curve_id;
616 uint8_t comp_id;
617
618 if (!pkey ||
619 pkey->type != EVP_PKEY_EC ||
620 !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) ||
621 !tls1_check_curve_id(s, curve_id) ||
622 !tls1_check_point_format(s, comp_id)) {
623 goto done;
624 }
625
626 ret = 1;
David Benjamin033e5f42014-11-13 18:47:41 -0500627
628done:
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 if (pkey) {
630 EVP_PKEY_free(pkey);
631 }
632 return ret;
633}
David Benjamin42e9a772014-09-02 23:18:44 -0400634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635int tls1_check_ec_tmp_key(SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 if (s->cert->ecdh_tmp_auto) {
David Benjaminc0f763b2015-03-27 02:05:39 -0400637 /* If using |ecdh_tmp_auto|, ECDH is acceptable if there is a shared
638 * curve. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 return tls1_get_shared_curve(s) != NID_undef;
640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
David Benjaminc0f763b2015-03-27 02:05:39 -0400642 if (s->cert->ecdh_nid != NID_undef) {
643 /* If the curve is preconfigured, ECDH is acceptable if the peer supports
644 * the curve. */
645 uint16_t curve_id;
646 return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) &&
647 tls1_check_curve_id(s, curve_id);
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 }
649
David Benjaminc0f763b2015-03-27 02:05:39 -0400650 /* Otherwise, assume the callback will provide an acceptable curve. */
651 return s->cert->ecdh_tmp_cb != NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800652}
Adam Langley95c29f32014-06-20 12:00:00 -0700653
654/* List of supported signature algorithms and hashes. Should make this
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 * customisable at some point, for now include everything we support. */
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langley95c29f32014-06-20 12:00:00 -0700657#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langley95c29f32014-06-20 12:00:00 -0700659#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700660
Adam Langleyfcf25832014-12-18 17:42:32 -0800661#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md)
Adam Langley95c29f32014-06-20 12:00:00 -0700662
David Benjamincff64722014-08-19 19:54:46 -0400663static const uint8_t tls12_sigalgs[] = {
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 tlsext_sigalg(TLSEXT_hash_sha512)
665 tlsext_sigalg(TLSEXT_hash_sha384)
666 tlsext_sigalg(TLSEXT_hash_sha256)
667 tlsext_sigalg(TLSEXT_hash_sha224)
668 tlsext_sigalg(TLSEXT_hash_sha1)
Adam Langley95c29f32014-06-20 12:00:00 -0700669};
David Benjamin05da6e12014-07-12 20:42:55 -0400670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) {
672 /* If server use client authentication sigalgs if not NULL */
673 if (s->server && s->cert->client_sigalgs) {
674 *psigs = s->cert->client_sigalgs;
675 return s->cert->client_sigalgslen;
676 } else if (s->cert->conf_sigalgs) {
677 *psigs = s->cert->conf_sigalgs;
678 return s->cert->conf_sigalgslen;
679 } else {
680 *psigs = tls12_sigalgs;
681 return sizeof(tls12_sigalgs);
682 }
683}
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685/* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It
686 * checks it is consistent with |s|'s sent supported signature algorithms and,
687 * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it
688 * returns 0 and writes an alert into |*out_alert|. */
689int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s,
690 CBS *cbs, EVP_PKEY *pkey) {
691 const uint8_t *sent_sigs;
692 size_t sent_sigslen, i;
693 int sigalg = tls12_get_sigid(pkey);
694 uint8_t hash, signature;
695
696 /* Should never happen */
697 if (sigalg == -1) {
698 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR);
699 *out_alert = SSL_AD_INTERNAL_ERROR;
700 return 0;
701 }
702
703 if (!CBS_get_u8(cbs, &hash) ||
704 !CBS_get_u8(cbs, &signature)) {
705 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR);
706 *out_alert = SSL_AD_DECODE_ERROR;
707 return 0;
708 }
709
710 /* Check key type is consistent with signature */
711 if (sigalg != signature) {
712 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
713 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
714 return 0;
715 }
716
717 if (pkey->type == EVP_PKEY_EC) {
718 uint16_t curve_id;
719 uint8_t comp_id;
720 /* Check compression and curve matches extensions */
721 if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) {
722 *out_alert = SSL_AD_INTERNAL_ERROR;
723 return 0;
724 }
725
726 if (s->server && (!tls1_check_curve_id(s, curve_id) ||
727 !tls1_check_point_format(s, comp_id))) {
728 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
729 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
730 return 0;
731 }
732 }
733
734 /* Check signature matches a type we sent */
735 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
736 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
737 if (hash == sent_sigs[0] && signature == sent_sigs[1]) {
738 break;
739 }
740 }
741
742 /* Allow fallback to SHA-1. */
743 if (i == sent_sigslen && hash != TLSEXT_hash_sha1) {
744 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
745 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
746 return 0;
747 }
748
749 *out_md = tls12_get_hash(hash);
750 if (*out_md == NULL) {
751 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
752 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
753 return 0;
754 }
755
756 return 1;
757}
758
759/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
760 * supported or doesn't appear in supported signature algorithms. Unlike
761 * ssl_cipher_get_disabled this applies to a specific session and not global
762 * settings. */
763void ssl_set_client_disabled(SSL *s) {
764 CERT *c = s->cert;
765 const uint8_t *sigalgs;
766 size_t i, sigalgslen;
767 int have_rsa = 0, have_ecdsa = 0;
768 c->mask_a = 0;
769 c->mask_k = 0;
770
771 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
772 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) {
773 c->mask_ssl = SSL_TLSV1_2;
774 } else {
775 c->mask_ssl = 0;
776 }
777
778 /* Now go through all signature algorithms seeing if we support any for RSA,
779 * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
780 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
781 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
782 switch (sigalgs[1]) {
783 case TLSEXT_signature_rsa:
784 have_rsa = 1;
785 break;
786
787 case TLSEXT_signature_ecdsa:
788 have_ecdsa = 1;
789 break;
790 }
791 }
792
793 /* Disable auth if we don't include any appropriate signature algorithms. */
794 if (!have_rsa) {
795 c->mask_a |= SSL_aRSA;
796 }
797 if (!have_ecdsa) {
798 c->mask_a |= SSL_aECDSA;
799 }
800
801 /* with PSK there must be client callback set */
802 if (!s->psk_client_callback) {
803 c->mask_a |= SSL_aPSK;
804 c->mask_k |= SSL_kPSK;
805 }
806}
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyb0c235e2014-06-20 12:00:00 -0700808/* header_len is the length of the ClientHello header written so far, used to
809 * compute padding. It does not include the record header. Pass 0 if no padding
810 * is to be done. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800811uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
812 size_t header_len) {
813 int extdatalen = 0;
814 uint8_t *ret = buf;
815 uint8_t *orig = buf;
816 /* See if we support any ECC ciphersuites */
817 int using_ecc = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
820 size_t i;
David Benjamin107db582015-04-08 00:41:59 -0400821 uint32_t alg_k, alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
825 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 alg_k = c->algorithm_mkey;
828 alg_a = c->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -0400829 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 using_ecc = 1;
831 break;
832 }
833 }
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 /* don't add extensions for SSLv3 unless doing secure renegotiation */
837 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
838 return orig;
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 ret += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 if (ret >= limit) {
844 return NULL; /* should never occur. */
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 if (s->tlsext_hostname != NULL) {
848 /* Add TLS extension servername to the Client Hello message */
849 unsigned long size_str;
850 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langleyfcf25832014-12-18 17:42:32 -0800852 /* check for enough space.
853 4 for the servername type and entension length
854 2 for servernamelist length
855 1 for the hostname type
856 2 for hostname length
857 + hostname length */
Adam Langley95c29f32014-06-20 12:00:00 -0700858
Adam Langleyfcf25832014-12-18 17:42:32 -0800859 lenmax = limit - ret - 9;
860 size_str = strlen(s->tlsext_hostname);
861 if (lenmax < 0 || size_str > (unsigned long)lenmax) {
862 return NULL;
863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langleyfcf25832014-12-18 17:42:32 -0800865 /* extension type and length */
866 s2n(TLSEXT_TYPE_server_name, ret);
867 s2n(size_str + 5, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 /* length of servername list */
870 s2n(size_str + 3, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 /* hostname type, length and hostname */
873 *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
874 s2n(size_str, ret);
875 memcpy(ret, s->tlsext_hostname, size_str);
876 ret += size_str;
877 }
Adam Langley75712922014-10-10 16:23:43 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 /* Add RI if renegotiating */
880 if (s->renegotiate) {
881 int el;
David Benjamind1681e62014-11-20 14:54:14 -0500882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
884 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
885 return NULL;
886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 if ((limit - ret - 4 - el) < 0) {
889 return NULL;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langleyfcf25832014-12-18 17:42:32 -0800892 s2n(TLSEXT_TYPE_renegotiate, ret);
893 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langleyfcf25832014-12-18 17:42:32 -0800895 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
896 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
897 return NULL;
898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 ret += el;
901 }
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 /* Add extended master secret. */
904 if (s->version != SSL3_VERSION) {
905 if (limit - ret - 4 < 0) {
906 return NULL;
907 }
908 s2n(TLSEXT_TYPE_extended_master_secret, ret);
909 s2n(0, ret);
910 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200911
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
913 int ticklen = 0;
914 if (!s->new_session && s->session && s->session->tlsext_tick) {
915 ticklen = s->session->tlsext_ticklen;
916 }
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 /* Check for enough room 2 for extension type, 2 for len rest for
919 * ticket. */
920 if ((long)(limit - ret - 4 - ticklen) < 0) {
921 return NULL;
922 }
923 s2n(TLSEXT_TYPE_session_ticket, ret);
924 s2n(ticklen, ret);
925 if (ticklen) {
926 memcpy(ret, s->session->tlsext_tick, ticklen);
927 ret += ticklen;
928 }
929 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700930
David Benjamin6ae7f072015-01-26 10:22:13 -0500931 if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 size_t salglen;
933 const uint8_t *salg;
934 salglen = tls12_get_psigalgs(s, &salg);
935 if ((size_t)(limit - ret) < salglen + 6) {
936 return NULL;
937 }
938 s2n(TLSEXT_TYPE_signature_algorithms, ret);
939 s2n(salglen + 2, ret);
940 s2n(salglen, ret);
941 memcpy(ret, salg, salglen);
942 ret += salglen;
943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langleyfcf25832014-12-18 17:42:32 -0800945 if (s->ocsp_stapling_enabled) {
946 /* The status_request extension is excessively extensible at every layer.
947 * On the client, only support requesting OCSP responses with an empty
948 * responder_id_list and no extensions. */
949 if (limit - ret - 4 - 1 - 2 - 2 < 0) {
950 return NULL;
951 }
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langleyfcf25832014-12-18 17:42:32 -0800953 s2n(TLSEXT_TYPE_status_request, ret);
954 s2n(1 + 2 + 2, ret);
955 /* status_type */
956 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
957 /* responder_id_list - empty */
958 s2n(0, ret);
959 /* request_extensions - empty */
960 s2n(0, ret);
961 }
Adam Langley95c29f32014-06-20 12:00:00 -0700962
David Benjamin78e69782014-12-19 04:52:17 -0500963 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len &&
964 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 /* The client advertises an emtpy extension to indicate its support for
966 * Next Protocol Negotiation */
967 if (limit - ret - 4 < 0) {
968 return NULL;
969 }
970 s2n(TLSEXT_TYPE_next_proto_neg, ret);
971 s2n(0, ret);
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) {
975 /* The client advertises an empty extension to indicate its support for
976 * certificate timestamps. */
977 if (limit - ret - 4 < 0) {
978 return NULL;
979 }
980 s2n(TLSEXT_TYPE_certificate_timestamp, ret);
981 s2n(0, ret);
982 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
985 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
986 return NULL;
987 }
988 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
989 s2n(2 + s->alpn_client_proto_list_len, ret);
990 s2n(s->alpn_client_proto_list_len, ret);
991 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
992 ret += s->alpn_client_proto_list_len;
993 }
Adam Langleyc3174b72014-06-20 12:00:00 -0700994
David Benjamin78e69782014-12-19 04:52:17 -0500995 if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800996 /* The client advertises an emtpy extension to indicate its support for
997 * Channel ID. */
998 if (limit - ret - 4 < 0) {
999 return NULL;
1000 }
1001 if (s->ctx->tlsext_channel_id_enabled_new) {
1002 s2n(TLSEXT_TYPE_channel_id_new, ret);
1003 } else {
1004 s2n(TLSEXT_TYPE_channel_id, ret);
1005 }
1006 s2n(0, ret);
1007 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (SSL_get_srtp_profiles(s)) {
1010 int el;
Adam Langleyc3174b72014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
Adam Langleyc3174b72014-06-20 12:00:00 -07001013
Adam Langleyfcf25832014-12-18 17:42:32 -08001014 if ((limit - ret - 4 - el) < 0) {
1015 return NULL;
1016 }
Adam Langleyc3174b72014-06-20 12:00:00 -07001017
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 s2n(TLSEXT_TYPE_use_srtp, ret);
1019 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001020
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1022 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1023 return NULL;
1024 }
1025 ret += el;
1026 }
Adam Langley95c29f32014-06-20 12:00:00 -07001027
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 if (using_ecc) {
1029 /* Add TLS extension ECPointFormats to the ClientHello message */
1030 long lenmax;
1031 const uint8_t *formats;
1032 const uint16_t *curves;
1033 size_t formats_len, curves_len, i;
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 tls1_get_formatlist(s, &formats, &formats_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 lenmax = limit - ret - 5;
1038 if (lenmax < 0) {
1039 return NULL;
1040 }
1041 if (formats_len > (size_t)lenmax) {
1042 return NULL;
1043 }
1044 if (formats_len > 255) {
1045 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1046 return NULL;
1047 }
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1050 s2n(formats_len + 1, ret);
1051 *(ret++) = (uint8_t)formats_len;
1052 memcpy(ret, formats, formats_len);
1053 ret += formats_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 /* Add TLS extension EllipticCurves to the ClientHello message */
1056 tls1_get_curvelist(s, 0, &curves, &curves_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001057
Adam Langleyfcf25832014-12-18 17:42:32 -08001058 lenmax = limit - ret - 6;
1059 if (lenmax < 0) {
1060 return NULL;
1061 }
1062 if (curves_len * 2 > (size_t)lenmax) {
1063 return NULL;
1064 }
1065 if (curves_len * 2 > 65532) {
1066 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1067 return NULL;
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1071 s2n((curves_len * 2) + 2, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 s2n(curves_len * 2, ret);
1074 for (i = 0; i < curves_len; i++) {
1075 s2n(curves[i], ret);
1076 }
1077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 if (header_len > 0) {
1080 size_t clienthello_minsize = 0;
1081 header_len += ret - orig;
1082 if (header_len > 0xff && header_len < 0x200) {
1083 /* Add padding to workaround bugs in F5 terminators. See
1084 * https://tools.ietf.org/html/draft-agl-tls-padding-03
1085 *
1086 * NB: because this code works out the length of all existing extensions
1087 * it MUST always appear last. */
1088 clienthello_minsize = 0x200;
1089 }
1090 if (s->fastradio_padding) {
1091 /* Pad the ClientHello record to 1024 bytes to fast forward the radio
1092 * into DCH (high data rate) state in 3G networks. Note that when
1093 * fastradio_padding is enabled, even if the header_len is less than 255
1094 * bytes, the padding will be applied regardless. This is slightly
1095 * different from the TLS padding extension suggested in
1096 * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
1097 clienthello_minsize = 0x400;
1098 }
1099 if (header_len < clienthello_minsize) {
1100 size_t padding_len = clienthello_minsize - header_len;
1101 /* Extensions take at least four bytes to encode. Always include least
1102 * one byte of data if including the extension. WebSphere Application
1103 * Server 7.0 is intolerant to the last extension being zero-length. */
1104 if (padding_len >= 4 + 1) {
1105 padding_len -= 4;
1106 } else {
1107 padding_len = 1;
1108 }
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 if (limit - ret - 4 - (long)padding_len < 0) {
1111 return NULL;
1112 }
Adam Langley75712922014-10-10 16:23:43 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 s2n(TLSEXT_TYPE_padding, ret);
1115 s2n(padding_len, ret);
1116 memset(ret, 0, padding_len);
1117 ret += padding_len;
1118 }
1119 }
Adam Langley75712922014-10-10 16:23:43 -07001120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121 extdatalen = ret - orig - 2;
1122 if (extdatalen == 0) {
1123 return orig;
1124 }
Adam Langley95c29f32014-06-20 12:00:00 -07001125
Adam Langleyfcf25832014-12-18 17:42:32 -08001126 s2n(extdatalen, orig);
1127 return ret;
1128}
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langleyfcf25832014-12-18 17:42:32 -08001130uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
1131 int extdatalen = 0;
1132 uint8_t *orig = buf;
1133 uint8_t *ret = buf;
1134 int next_proto_neg_seen;
David Benjamin107db582015-04-08 00:41:59 -04001135 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1136 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04001137 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1141 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
1142 return orig;
1143 }
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 ret += 2;
1146 if (ret >= limit) {
1147 return NULL; /* should never happen. */
1148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
1151 if ((long)(limit - ret - 4) < 0) {
1152 return NULL;
1153 }
Adam Langley95c29f32014-06-20 12:00:00 -07001154
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 s2n(TLSEXT_TYPE_server_name, ret);
1156 s2n(0, ret);
1157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 if (s->s3->send_connection_binding) {
1160 int el;
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1163 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1164 return NULL;
1165 }
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langleyfcf25832014-12-18 17:42:32 -08001167 if ((limit - ret - 4 - el) < 0) {
1168 return NULL;
1169 }
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 s2n(TLSEXT_TYPE_renegotiate, ret);
1172 s2n(el, ret);
Adam Langley95c29f32014-06-20 12:00:00 -07001173
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1175 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1176 return NULL;
1177 }
Adam Langley95c29f32014-06-20 12:00:00 -07001178
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 ret += el;
1180 }
Adam Langley95c29f32014-06-20 12:00:00 -07001181
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 if (s->s3->tmp.extended_master_secret) {
1183 if ((long)(limit - ret - 4) < 0) {
1184 return NULL;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1188 s2n(0, ret);
1189 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001190
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 if (using_ecc) {
1192 const uint8_t *plist;
1193 size_t plistlen;
1194 /* Add TLS extension ECPointFormats to the ServerHello message */
1195 long lenmax;
Adam Langley95c29f32014-06-20 12:00:00 -07001196
Adam Langleyfcf25832014-12-18 17:42:32 -08001197 tls1_get_formatlist(s, &plist, &plistlen);
1198
1199 lenmax = limit - ret - 5;
1200 if (lenmax < 0) {
1201 return NULL;
1202 }
1203 if (plistlen > (size_t)lenmax) {
1204 return NULL;
1205 }
1206 if (plistlen > 255) {
1207 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1208 return NULL;
1209 }
1210
1211 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1212 s2n(plistlen + 1, ret);
1213 *(ret++) = (uint8_t)plistlen;
1214 memcpy(ret, plist, plistlen);
1215 ret += plistlen;
1216 }
1217 /* Currently the server should not respond with a SupportedCurves extension */
1218
1219 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1220 if ((long)(limit - ret - 4) < 0) {
1221 return NULL;
1222 }
1223 s2n(TLSEXT_TYPE_session_ticket, ret);
1224 s2n(0, ret);
1225 }
1226
1227 if (s->s3->tmp.certificate_status_expected) {
1228 if ((long)(limit - ret - 4) < 0) {
1229 return NULL;
1230 }
1231 s2n(TLSEXT_TYPE_status_request, ret);
1232 s2n(0, ret);
1233 }
1234
1235 if (s->srtp_profile) {
1236 int el;
1237
1238 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1239
1240 if ((limit - ret - 4 - el) < 0) {
1241 return NULL;
1242 }
1243
1244 s2n(TLSEXT_TYPE_use_srtp, ret);
1245 s2n(el, ret);
1246
1247 if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1248 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1249 return NULL;
1250 }
1251 ret += el;
1252 }
1253
1254 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1255 s->s3->next_proto_neg_seen = 0;
1256 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1257 const uint8_t *npa;
1258 unsigned int npalen;
1259 int r;
1260
1261 r = s->ctx->next_protos_advertised_cb(
1262 s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1263 if (r == SSL_TLSEXT_ERR_OK) {
1264 if ((long)(limit - ret - 4 - npalen) < 0) {
1265 return NULL;
1266 }
1267 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1268 s2n(npalen, ret);
1269 memcpy(ret, npa, npalen);
1270 ret += npalen;
1271 s->s3->next_proto_neg_seen = 1;
1272 }
1273 }
1274
1275 if (s->s3->alpn_selected) {
1276 const uint8_t *selected = s->s3->alpn_selected;
1277 size_t len = s->s3->alpn_selected_len;
1278
1279 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
1280 return NULL;
1281 }
1282 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1283 s2n(3 + len, ret);
1284 s2n(1 + len, ret);
1285 *ret++ = len;
1286 memcpy(ret, selected, len);
1287 ret += len;
1288 }
1289
1290 /* If the client advertised support for Channel ID, and we have it
1291 * enabled, then we want to echo it back. */
1292 if (s->s3->tlsext_channel_id_valid) {
1293 if (limit - ret - 4 < 0) {
1294 return NULL;
1295 }
1296 if (s->s3->tlsext_channel_id_new) {
1297 s2n(TLSEXT_TYPE_channel_id_new, ret);
1298 } else {
1299 s2n(TLSEXT_TYPE_channel_id, ret);
1300 }
1301 s2n(0, ret);
1302 }
1303
1304 extdatalen = ret - orig - 2;
1305 if (extdatalen == 0) {
1306 return orig;
1307 }
1308
1309 s2n(extdatalen, orig);
1310 return ret;
1311}
Adam Langley95c29f32014-06-20 12:00:00 -07001312
Adam Langley95c29f32014-06-20 12:00:00 -07001313/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1314 * ClientHello.
David Benjamindc72ff72014-06-25 12:36:10 -04001315 * cbs: the contents of the extension, not including the type and length.
1316 * out_alert: a pointer to the alert value to send in the event of a zero
Adam Langley95c29f32014-06-20 12:00:00 -07001317 * return.
1318 *
David Benjamindc72ff72014-06-25 12:36:10 -04001319 * returns: 1 on success. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001320static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
1321 CBS protocol_name_list, protocol_name_list_copy;
1322 const uint8_t *selected;
1323 uint8_t selected_len;
1324 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07001325
Adam Langleyfcf25832014-12-18 17:42:32 -08001326 if (s->ctx->alpn_select_cb == NULL) {
1327 return 1;
1328 }
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
1331 CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
1332 goto parse_error;
1333 }
Adam Langley95c29f32014-06-20 12:00:00 -07001334
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 /* Validate the protocol list. */
1336 protocol_name_list_copy = protocol_name_list;
1337 while (CBS_len(&protocol_name_list_copy) > 0) {
1338 CBS protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07001339
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
1341 goto parse_error;
1342 }
1343 }
Adam Langley95c29f32014-06-20 12:00:00 -07001344
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 r = s->ctx->alpn_select_cb(
1346 s, &selected, &selected_len, CBS_data(&protocol_name_list),
1347 CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
1348 if (r == SSL_TLSEXT_ERR_OK) {
1349 if (s->s3->alpn_selected) {
1350 OPENSSL_free(s->s3->alpn_selected);
1351 }
1352 s->s3->alpn_selected = BUF_memdup(selected, selected_len);
1353 if (!s->s3->alpn_selected) {
1354 *out_alert = SSL_AD_INTERNAL_ERROR;
1355 return 0;
1356 }
1357 s->s3->alpn_selected_len = selected_len;
1358 }
1359
1360 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001361
1362parse_error:
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 *out_alert = SSL_AD_DECODE_ERROR;
1364 return 0;
1365}
Adam Langley95c29f32014-06-20 12:00:00 -07001366
Adam Langleyfcf25832014-12-18 17:42:32 -08001367static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1368 int renegotiate_seen = 0;
1369 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001370
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 s->should_ack_sni = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001372 s->srtp_profile = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 s->s3->next_proto_neg_seen = 0;
1374 s->s3->tmp.certificate_status_expected = 0;
1375 s->s3->tmp.extended_master_secret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001376
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 if (s->s3->alpn_selected) {
1378 OPENSSL_free(s->s3->alpn_selected);
1379 s->s3->alpn_selected = NULL;
1380 }
Adam Langley95c29f32014-06-20 12:00:00 -07001381
Adam Langleyfcf25832014-12-18 17:42:32 -08001382 /* Clear any signature algorithms extension received */
1383 if (s->cert->peer_sigalgs) {
1384 OPENSSL_free(s->cert->peer_sigalgs);
1385 s->cert->peer_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001386 s->cert->peer_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 }
Adam Langley95c29f32014-06-20 12:00:00 -07001388
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 /* Clear any shared signature algorithms */
1390 if (s->cert->shared_sigalgs) {
1391 OPENSSL_free(s->cert->shared_sigalgs);
1392 s->cert->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04001393 s->cert->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 }
Adam Langley95c29f32014-06-20 12:00:00 -07001395
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 /* Clear ECC extensions */
1397 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1398 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1399 s->s3->tmp.peer_ecpointformatlist = NULL;
1400 s->s3->tmp.peer_ecpointformatlist_length = 0;
1401 }
David Benjamindc72ff72014-06-25 12:36:10 -04001402
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 if (s->s3->tmp.peer_ellipticcurvelist != 0) {
1404 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1405 s->s3->tmp.peer_ellipticcurvelist = NULL;
1406 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1407 }
David Benjamindc72ff72014-06-25 12:36:10 -04001408
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 /* There may be no extensions. */
1410 if (CBS_len(cbs) == 0) {
1411 goto ri_check;
1412 }
David Benjamindc72ff72014-06-25 12:36:10 -04001413
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 /* Decode the extensions block and check it is valid. */
1415 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1416 !tls1_check_duplicate_extensions(&extensions)) {
1417 *out_alert = SSL_AD_DECODE_ERROR;
1418 return 0;
1419 }
David Benjamindc72ff72014-06-25 12:36:10 -04001420
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 while (CBS_len(&extensions) != 0) {
1422 uint16_t type;
1423 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001424
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 /* Decode the next extension. */
1426 if (!CBS_get_u16(&extensions, &type) ||
1427 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1428 *out_alert = SSL_AD_DECODE_ERROR;
1429 return 0;
1430 }
Adam Langley95c29f32014-06-20 12:00:00 -07001431
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 if (s->tlsext_debug_cb) {
1433 s->tlsext_debug_cb(s, 0, type, (uint8_t *)CBS_data(&extension),
1434 CBS_len(&extension), s->tlsext_debug_arg);
1435 }
Adam Langley95c29f32014-06-20 12:00:00 -07001436
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 /* The servername extension is treated as follows:
David Benjamindc72ff72014-06-25 12:36:10 -04001438
Adam Langleyfcf25832014-12-18 17:42:32 -08001439 - Only the hostname type is supported with a maximum length of 255.
1440 - The servername is rejected if too long or if it contains zeros, in
1441 which case an fatal alert is generated.
1442 - The servername field is maintained together with the session cache.
1443 - When a session is resumed, the servername call back invoked in order
1444 to allow the application to position itself to the right context.
1445 - The servername is acknowledged if it is new for a session or when
1446 it is identical to a previously used for the same session.
1447 Applications can control the behaviour. They can at any time
1448 set a 'desirable' servername for a new SSL object. This can be the
1449 case for example with HTTPS when a Host: header field is received and
1450 a renegotiation is requested. In this case, a possible servername
1451 presented in the new client hello is only acknowledged if it matches
1452 the value of the Host: field.
1453 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1454 if they provide for changing an explicit servername context for the
1455 session,
1456 i.e. when the session has been established with a servername extension.
1457 - On session reconnect, the servername extension may be absent. */
Adam Langley95c29f32014-06-20 12:00:00 -07001458
Adam Langleyfcf25832014-12-18 17:42:32 -08001459 if (type == TLSEXT_TYPE_server_name) {
1460 CBS server_name_list;
1461 char have_seen_host_name = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001462
Adam Langleyfcf25832014-12-18 17:42:32 -08001463 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
1464 CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
1465 *out_alert = SSL_AD_DECODE_ERROR;
1466 return 0;
1467 }
David Benjamindc72ff72014-06-25 12:36:10 -04001468
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 /* Decode each ServerName in the extension. */
1470 while (CBS_len(&server_name_list) > 0) {
1471 uint8_t name_type;
1472 CBS host_name;
David Benjamindc72ff72014-06-25 12:36:10 -04001473
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 /* Decode the NameType. */
1475 if (!CBS_get_u8(&server_name_list, &name_type)) {
1476 *out_alert = SSL_AD_DECODE_ERROR;
1477 return 0;
1478 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001479
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 /* Only host_name is supported. */
1481 if (name_type != TLSEXT_NAMETYPE_host_name) {
1482 continue;
1483 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001484
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 if (have_seen_host_name) {
1486 /* The ServerNameList MUST NOT contain more than one name of the same
1487 * name_type. */
1488 *out_alert = SSL_AD_DECODE_ERROR;
1489 return 0;
1490 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001491
Adam Langleyfcf25832014-12-18 17:42:32 -08001492 have_seen_host_name = 1;
Adam Langleyed8270a2014-09-02 13:52:56 -07001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
1495 CBS_len(&host_name) < 1) {
1496 *out_alert = SSL_AD_DECODE_ERROR;
1497 return 0;
1498 }
Adam Langley95c29f32014-06-20 12:00:00 -07001499
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
1501 CBS_contains_zero_byte(&host_name)) {
1502 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1503 return 0;
1504 }
Adam Langleyed8270a2014-09-02 13:52:56 -07001505
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 if (!s->hit) {
1507 assert(s->session->tlsext_hostname == NULL);
1508 if (s->session->tlsext_hostname) {
1509 /* This should be impossible. */
1510 *out_alert = SSL_AD_DECODE_ERROR;
1511 return 0;
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
Adam Langleyfcf25832014-12-18 17:42:32 -08001514 /* Copy the hostname as a string. */
1515 if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
1516 *out_alert = SSL_AD_INTERNAL_ERROR;
1517 return 0;
1518 }
Adam Langley95c29f32014-06-20 12:00:00 -07001519
Adam Langleyfcf25832014-12-18 17:42:32 -08001520 s->should_ack_sni = 1;
1521 }
1522 }
1523 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1524 CBS ec_point_format_list;
David Benjamindc72ff72014-06-25 12:36:10 -04001525
Adam Langleyfcf25832014-12-18 17:42:32 -08001526 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1527 CBS_len(&extension) != 0) {
1528 *out_alert = SSL_AD_DECODE_ERROR;
1529 return 0;
1530 }
Adam Langley95c29f32014-06-20 12:00:00 -07001531
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1533 &s->s3->tmp.peer_ecpointformatlist_length)) {
1534 *out_alert = SSL_AD_INTERNAL_ERROR;
1535 return 0;
1536 }
1537 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1538 CBS elliptic_curve_list;
1539 size_t i, num_curves;
David Benjamindc72ff72014-06-25 12:36:10 -04001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
1542 CBS_len(&elliptic_curve_list) == 0 ||
1543 (CBS_len(&elliptic_curve_list) & 1) != 0 ||
1544 CBS_len(&extension) != 0) {
1545 *out_alert = SSL_AD_DECODE_ERROR;
1546 return 0;
1547 }
David Benjamindc72ff72014-06-25 12:36:10 -04001548
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 if (s->s3->tmp.peer_ellipticcurvelist) {
1550 OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
1551 s->s3->tmp.peer_ellipticcurvelist_length = 0;
1552 }
David Benjamindc72ff72014-06-25 12:36:10 -04001553
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 s->s3->tmp.peer_ellipticcurvelist =
1555 (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
David Benjamindc72ff72014-06-25 12:36:10 -04001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
1558 *out_alert = SSL_AD_INTERNAL_ERROR;
1559 return 0;
1560 }
Adam Langley95c29f32014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 num_curves = CBS_len(&elliptic_curve_list) / 2;
1563 for (i = 0; i < num_curves; i++) {
1564 if (!CBS_get_u16(&elliptic_curve_list,
1565 &s->s3->tmp.peer_ellipticcurvelist[i])) {
1566 *out_alert = SSL_AD_INTERNAL_ERROR;
1567 return 0;
1568 }
1569 }
David Benjamindc72ff72014-06-25 12:36:10 -04001570
Adam Langleyfcf25832014-12-18 17:42:32 -08001571 if (CBS_len(&elliptic_curve_list) != 0) {
1572 *out_alert = SSL_AD_INTERNAL_ERROR;
1573 return 0;
1574 }
Adam Langley95c29f32014-06-20 12:00:00 -07001575
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
1577 } else if (type == TLSEXT_TYPE_renegotiate) {
1578 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
1579 return 0;
1580 }
1581 renegotiate_seen = 1;
1582 } else if (type == TLSEXT_TYPE_signature_algorithms) {
1583 CBS supported_signature_algorithms;
Adam Langley95c29f32014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 if (!CBS_get_u16_length_prefixed(&extension,
1586 &supported_signature_algorithms) ||
1587 CBS_len(&extension) != 0) {
1588 *out_alert = SSL_AD_DECODE_ERROR;
1589 return 0;
1590 }
Adam Langley95c29f32014-06-20 12:00:00 -07001591
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 /* Ensure the signature algorithms are non-empty. It contains a list of
1593 * SignatureAndHashAlgorithms which are two bytes each. */
1594 if (CBS_len(&supported_signature_algorithms) == 0 ||
1595 (CBS_len(&supported_signature_algorithms) % 2) != 0) {
1596 *out_alert = SSL_AD_DECODE_ERROR;
1597 return 0;
1598 }
David Benjamindc72ff72014-06-25 12:36:10 -04001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
1601 *out_alert = SSL_AD_DECODE_ERROR;
1602 return 0;
1603 }
1604 /* If sigalgs received and no shared algorithms fatal error. */
1605 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +01001606 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
Adam Langleyfcf25832014-12-18 17:42:32 -08001607 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1608 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
1609 return 0;
1610 }
1611 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001612 s->s3->tmp.finish_md_len == 0 && s->s3->alpn_selected == NULL &&
1613 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001614 /* The extension must be empty. */
1615 if (CBS_len(&extension) != 0) {
1616 *out_alert = SSL_AD_DECODE_ERROR;
1617 return 0;
1618 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001619
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 /* We shouldn't accept this extension on a renegotiation.
1621 *
1622 * s->new_session will be set on renegotiation, but we probably shouldn't
1623 * rely that it couldn't be set on the initial renegotation too in
1624 * certain cases (when there's some other reason to disallow resuming an
1625 * earlier session -- the current code won't be doing anything like that,
1626 * but this might change).
David Benjamindc72ff72014-06-25 12:36:10 -04001627
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 * A valid sign that there's been a previous handshake in this connection
1629 * is if s->s3->tmp.finish_md_len > 0. (We are talking about a check
1630 * that will happen in the Hello protocol round, well before a new
1631 * Finished message could have been computed.) */
1632 s->s3->next_proto_neg_seen = 1;
1633 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
1634 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
1635 if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
1636 return 0;
1637 }
1638 /* ALPN takes precedence over NPN. */
1639 s->s3->next_proto_neg_seen = 0;
David Benjamin78e69782014-12-19 04:52:17 -05001640 } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
1641 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001642 /* The extension must be empty. */
1643 if (CBS_len(&extension) != 0) {
1644 *out_alert = SSL_AD_DECODE_ERROR;
1645 return 0;
1646 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001647
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 s->s3->tlsext_channel_id_valid = 1;
1649 } else if (type == TLSEXT_TYPE_channel_id_new &&
David Benjamin78e69782014-12-19 04:52:17 -05001650 s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 /* The extension must be empty. */
1652 if (CBS_len(&extension) != 0) {
1653 *out_alert = SSL_AD_DECODE_ERROR;
1654 return 0;
1655 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 s->s3->tlsext_channel_id_valid = 1;
1658 s->s3->tlsext_channel_id_new = 1;
1659 } else if (type == TLSEXT_TYPE_use_srtp) {
1660 if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
1661 return 0;
1662 }
1663 } else if (type == TLSEXT_TYPE_extended_master_secret &&
1664 s->version != SSL3_VERSION) {
1665 if (CBS_len(&extension) != 0) {
1666 *out_alert = SSL_AD_DECODE_ERROR;
1667 return 0;
1668 }
Adam Langley75712922014-10-10 16:23:43 -07001669
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 s->s3->tmp.extended_master_secret = 1;
1671 }
1672 }
Adam Langley75712922014-10-10 16:23:43 -07001673
Adam Langleyfcf25832014-12-18 17:42:32 -08001674ri_check:
1675 /* Need RI if renegotiating */
Adam Langley95c29f32014-06-20 12:00:00 -07001676
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 if (!renegotiate_seen && s->renegotiate &&
1678 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1679 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1680 OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
1681 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1682 return 0;
1683 }
Adam Langley95c29f32014-06-20 12:00:00 -07001684
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 return 1;
1686}
Adam Langley95c29f32014-06-20 12:00:00 -07001687
Adam Langleyfcf25832014-12-18 17:42:32 -08001688int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
1689 int alert = -1;
1690 if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
1691 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
1692 return 0;
1693 }
Adam Langley95c29f32014-06-20 12:00:00 -07001694
Adam Langleyfcf25832014-12-18 17:42:32 -08001695 if (ssl_check_clienthello_tlsext(s) <= 0) {
1696 OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
1697 SSL_R_CLIENTHELLO_TLSEXT);
1698 return 0;
1699 }
Adam Langley95c29f32014-06-20 12:00:00 -07001700
Adam Langleyfcf25832014-12-18 17:42:32 -08001701 return 1;
1702}
Adam Langley95c29f32014-06-20 12:00:00 -07001703
Adam Langley95c29f32014-06-20 12:00:00 -07001704/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 * elements of zero length are allowed and the set of elements must exactly
1706 * fill the length of the block. */
1707static char ssl_next_proto_validate(const CBS *cbs) {
1708 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001709
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 while (CBS_len(&copy) != 0) {
1711 CBS proto;
1712 if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
1713 return 0;
1714 }
1715 }
Adam Langley95c29f32014-06-20 12:00:00 -07001716
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 return 1;
1718}
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langleyfcf25832014-12-18 17:42:32 -08001720static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
1721 int tlsext_servername = 0;
1722 int renegotiate_seen = 0;
1723 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07001724
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 /* TODO(davidben): Move all of these to some per-handshake state that gets
1726 * systematically reset on a new handshake; perhaps allocate it fresh each
1727 * time so it's not even kept around post-handshake. */
1728 s->s3->next_proto_neg_seen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 s->tlsext_ticket_expected = 0;
1730 s->s3->tmp.certificate_status_expected = 0;
1731 s->s3->tmp.extended_master_secret = 0;
David Benjamind1d7d3d2015-01-11 20:30:21 -05001732 s->srtp_profile = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001733
Adam Langleyfcf25832014-12-18 17:42:32 -08001734 if (s->s3->alpn_selected) {
1735 OPENSSL_free(s->s3->alpn_selected);
1736 s->s3->alpn_selected = NULL;
1737 }
David Benjamina19fc252014-10-19 00:14:36 -04001738
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 /* Clear ECC extensions */
1740 if (s->s3->tmp.peer_ecpointformatlist != 0) {
1741 OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
1742 s->s3->tmp.peer_ecpointformatlist = NULL;
1743 s->s3->tmp.peer_ecpointformatlist_length = 0;
1744 }
David Benjamin03973092014-06-24 23:27:17 -04001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* There may be no extensions. */
1747 if (CBS_len(cbs) == 0) {
1748 goto ri_check;
1749 }
Adam Langley95c29f32014-06-20 12:00:00 -07001750
Adam Langleyfcf25832014-12-18 17:42:32 -08001751 /* Decode the extensions block and check it is valid. */
1752 if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
1753 !tls1_check_duplicate_extensions(&extensions)) {
1754 *out_alert = SSL_AD_DECODE_ERROR;
1755 return 0;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 while (CBS_len(&extensions) != 0) {
1759 uint16_t type;
1760 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 /* Decode the next extension. */
1763 if (!CBS_get_u16(&extensions, &type) ||
1764 !CBS_get_u16_length_prefixed(&extensions, &extension)) {
1765 *out_alert = SSL_AD_DECODE_ERROR;
1766 return 0;
1767 }
Adam Langley95c29f32014-06-20 12:00:00 -07001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 if (s->tlsext_debug_cb) {
1770 s->tlsext_debug_cb(s, 1, type, (uint8_t *)CBS_data(&extension),
1771 CBS_len(&extension), s->tlsext_debug_arg);
1772 }
Adam Langley95c29f32014-06-20 12:00:00 -07001773
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 if (type == TLSEXT_TYPE_server_name) {
1775 /* The extension must be empty. */
1776 if (CBS_len(&extension) != 0) {
1777 *out_alert = SSL_AD_DECODE_ERROR;
1778 return 0;
1779 }
David Benjamin03973092014-06-24 23:27:17 -04001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 /* We must have sent it in ClientHello. */
1782 if (s->tlsext_hostname == NULL) {
1783 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1784 return 0;
1785 }
David Benjamin03973092014-06-24 23:27:17 -04001786
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 tlsext_servername = 1;
1788 } else if (type == TLSEXT_TYPE_ec_point_formats) {
1789 CBS ec_point_format_list;
David Benjamin03973092014-06-24 23:27:17 -04001790
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
1792 CBS_len(&extension) != 0) {
1793 *out_alert = SSL_AD_DECODE_ERROR;
1794 return 0;
1795 }
Adam Langley95c29f32014-06-20 12:00:00 -07001796
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
1798 &s->s3->tmp.peer_ecpointformatlist_length)) {
1799 *out_alert = SSL_AD_INTERNAL_ERROR;
1800 return 0;
1801 }
1802 } else if (type == TLSEXT_TYPE_session_ticket) {
1803 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
1804 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1805 return 0;
1806 }
David Benjamin03973092014-06-24 23:27:17 -04001807
Adam Langleyfcf25832014-12-18 17:42:32 -08001808 s->tlsext_ticket_expected = 1;
1809 } else if (type == TLSEXT_TYPE_status_request) {
1810 /* The extension MUST be empty and may only sent if we've requested a
1811 * status request message. */
1812 if (CBS_len(&extension) != 0) {
1813 *out_alert = SSL_AD_DECODE_ERROR;
1814 return 0;
1815 }
David Benjamin03973092014-06-24 23:27:17 -04001816
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 if (!s->ocsp_stapling_enabled) {
1818 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1819 return 0;
1820 }
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langleyfcf25832014-12-18 17:42:32 -08001822 /* Set a flag to expect a CertificateStatus message */
1823 s->s3->tmp.certificate_status_expected = 1;
1824 } else if (type == TLSEXT_TYPE_next_proto_neg &&
David Benjamin78e69782014-12-19 04:52:17 -05001825 s->s3->tmp.finish_md_len == 0 &&
1826 !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001827 uint8_t *selected;
1828 uint8_t selected_len;
David Benjamin03973092014-06-24 23:27:17 -04001829
Adam Langleyfcf25832014-12-18 17:42:32 -08001830 /* We must have requested it. */
1831 if (s->ctx->next_proto_select_cb == NULL) {
1832 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1833 return 0;
1834 }
David Benjamin03973092014-06-24 23:27:17 -04001835
Adam Langleyfcf25832014-12-18 17:42:32 -08001836 /* The data must be valid. */
1837 if (!ssl_next_proto_validate(&extension)) {
1838 *out_alert = SSL_AD_DECODE_ERROR;
1839 return 0;
1840 }
Adam Langley95c29f32014-06-20 12:00:00 -07001841
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 if (s->ctx->next_proto_select_cb(
1843 s, &selected, &selected_len, CBS_data(&extension),
1844 CBS_len(&extension),
1845 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
1846 *out_alert = SSL_AD_INTERNAL_ERROR;
1847 return 0;
1848 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001849
Adam Langleyfcf25832014-12-18 17:42:32 -08001850 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
1851 if (s->next_proto_negotiated == NULL) {
1852 *out_alert = SSL_AD_INTERNAL_ERROR;
1853 return 0;
1854 }
Adam Langley75712922014-10-10 16:23:43 -07001855
Adam Langleyfcf25832014-12-18 17:42:32 -08001856 s->next_proto_negotiated_len = selected_len;
1857 s->s3->next_proto_neg_seen = 1;
1858 } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
1859 CBS protocol_name_list, protocol_name;
Adam Langley75712922014-10-10 16:23:43 -07001860
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 /* We must have requested it. */
1862 if (s->alpn_client_proto_list == NULL) {
1863 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
1864 return 0;
1865 }
Adam Langley95c29f32014-06-20 12:00:00 -07001866
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 /* The extension data consists of a ProtocolNameList which must have
1868 * exactly one ProtocolName. Each of these is length-prefixed. */
1869 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
1870 CBS_len(&extension) != 0 ||
1871 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
1872 CBS_len(&protocol_name_list) != 0) {
1873 *out_alert = SSL_AD_DECODE_ERROR;
1874 return 0;
1875 }
Adam Langley95c29f32014-06-20 12:00:00 -07001876
Adam Langleyfcf25832014-12-18 17:42:32 -08001877 if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
1878 &s->s3->alpn_selected_len)) {
1879 *out_alert = SSL_AD_INTERNAL_ERROR;
1880 return 0;
1881 }
David Benjamin78e69782014-12-19 04:52:17 -05001882 } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001883 if (CBS_len(&extension) != 0) {
1884 *out_alert = SSL_AD_DECODE_ERROR;
1885 return 0;
1886 }
Adam Langley95c29f32014-06-20 12:00:00 -07001887
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 s->s3->tlsext_channel_id_valid = 1;
David Benjamin78e69782014-12-19 04:52:17 -05001889 } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 if (CBS_len(&extension) != 0) {
1891 *out_alert = SSL_AD_DECODE_ERROR;
1892 return 0;
1893 }
Adam Langley95c29f32014-06-20 12:00:00 -07001894
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 s->s3->tlsext_channel_id_valid = 1;
1896 s->s3->tlsext_channel_id_new = 1;
1897 } else if (type == TLSEXT_TYPE_certificate_timestamp) {
1898 if (CBS_len(&extension) == 0) {
1899 *out_alert = SSL_AD_DECODE_ERROR;
1900 return 0;
1901 }
Adam Langley95c29f32014-06-20 12:00:00 -07001902
Adam Langleyfcf25832014-12-18 17:42:32 -08001903 /* Session resumption uses the original session information. */
1904 if (!s->hit &&
1905 !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
1906 &s->session->tlsext_signed_cert_timestamp_list_length)) {
1907 *out_alert = SSL_AD_INTERNAL_ERROR;
1908 return 0;
1909 }
1910 } else if (type == TLSEXT_TYPE_renegotiate) {
1911 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
1912 return 0;
1913 }
Adam Langley95c29f32014-06-20 12:00:00 -07001914
Adam Langleyfcf25832014-12-18 17:42:32 -08001915 renegotiate_seen = 1;
1916 } else if (type == TLSEXT_TYPE_use_srtp) {
1917 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
1918 return 0;
1919 }
1920 } else if (type == TLSEXT_TYPE_extended_master_secret) {
1921 if (/* It is invalid for the server to select EMS and
1922 SSLv3. */
1923 s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
1924 *out_alert = SSL_AD_DECODE_ERROR;
1925 return 0;
1926 }
Adam Langley95c29f32014-06-20 12:00:00 -07001927
Adam Langleyfcf25832014-12-18 17:42:32 -08001928 s->s3->tmp.extended_master_secret = 1;
1929 }
1930 }
Adam Langley95c29f32014-06-20 12:00:00 -07001931
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
1933 if (s->session->tlsext_hostname == NULL) {
1934 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1935 if (!s->session->tlsext_hostname) {
1936 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
1937 return 0;
1938 }
1939 } else {
1940 *out_alert = SSL_AD_DECODE_ERROR;
1941 return 0;
1942 }
1943 }
Adam Langley95c29f32014-06-20 12:00:00 -07001944
Adam Langleyfcf25832014-12-18 17:42:32 -08001945ri_check:
1946 /* Determine if we need to see RI. Strictly speaking if we want to avoid an
1947 * attack we should *always* see RI even on initial server hello because the
1948 * client doesn't see any renegotiation during an attack. However this would
1949 * mean we could not connect to any server which doesn't support RI so for
1950 * the immediate future tolerate RI absence on initial connect only. */
1951 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
1952 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1953 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
1954 OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
1955 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1956 return 0;
1957 }
Adam Langley95c29f32014-06-20 12:00:00 -07001958
Adam Langleyfcf25832014-12-18 17:42:32 -08001959 return 1;
1960}
Adam Langley95c29f32014-06-20 12:00:00 -07001961
Adam Langleyfcf25832014-12-18 17:42:32 -08001962int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -07001963
Adam Langleyfcf25832014-12-18 17:42:32 -08001964int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
Adam Langleyed8270a2014-09-02 13:52:56 -07001965
Adam Langleyfcf25832014-12-18 17:42:32 -08001966static int ssl_check_clienthello_tlsext(SSL *s) {
1967 int ret = SSL_TLSEXT_ERR_NOACK;
1968 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langleyed8270a2014-09-02 13:52:56 -07001969
Adam Langleyfcf25832014-12-18 17:42:32 -08001970 /* The handling of the ECPointFormats extension is done elsewhere, namely in
1971 * ssl3_choose_cipher in s3_lib.c. */
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
1974 ret = s->ctx->tlsext_servername_callback(s, &al,
1975 s->ctx->tlsext_servername_arg);
1976 } else if (s->initial_ctx != NULL &&
1977 s->initial_ctx->tlsext_servername_callback != 0) {
1978 ret = s->initial_ctx->tlsext_servername_callback(
1979 s, &al, s->initial_ctx->tlsext_servername_arg);
1980 }
Adam Langley95c29f32014-06-20 12:00:00 -07001981
Adam Langleyfcf25832014-12-18 17:42:32 -08001982 switch (ret) {
1983 case SSL_TLSEXT_ERR_ALERT_FATAL:
1984 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1985 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -07001986
Adam Langleyfcf25832014-12-18 17:42:32 -08001987 case SSL_TLSEXT_ERR_ALERT_WARNING:
1988 ssl3_send_alert(s, SSL3_AL_WARNING, al);
1989 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001990
Adam Langleyfcf25832014-12-18 17:42:32 -08001991 case SSL_TLSEXT_ERR_NOACK:
1992 s->should_ack_sni = 0;
1993 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001994
Adam Langleyfcf25832014-12-18 17:42:32 -08001995 default:
1996 return 1;
1997 }
1998}
Adam Langleyed8270a2014-09-02 13:52:56 -07001999
Adam Langleyfcf25832014-12-18 17:42:32 -08002000static int ssl_check_serverhello_tlsext(SSL *s) {
2001 int ret = SSL_TLSEXT_ERR_NOACK;
2002 int al = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07002003
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 /* If we are client and using an elliptic curve cryptography cipher suite,
2005 * then if server returns an EC point formats lists extension it must contain
2006 * uncompressed. */
David Benjamin107db582015-04-08 00:41:59 -04002007 uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2008 uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
David Benjamin7061e282015-03-19 11:10:48 -04002009 if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
Adam Langleyfcf25832014-12-18 17:42:32 -08002010 !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
2011 OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
2012 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2013 return -1;
2014 }
2015 ret = SSL_TLSEXT_ERR_OK;
David Benjamin03973092014-06-24 23:27:17 -04002016
Adam Langleyfcf25832014-12-18 17:42:32 -08002017 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
2018 ret = s->ctx->tlsext_servername_callback(s, &al,
2019 s->ctx->tlsext_servername_arg);
2020 } else if (s->initial_ctx != NULL &&
David Benjaminb18f0242015-03-10 18:30:08 -04002021 s->initial_ctx->tlsext_servername_callback != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002022 ret = s->initial_ctx->tlsext_servername_callback(
2023 s, &al, s->initial_ctx->tlsext_servername_arg);
2024 }
Adam Langley95c29f32014-06-20 12:00:00 -07002025
Adam Langleyfcf25832014-12-18 17:42:32 -08002026 switch (ret) {
2027 case SSL_TLSEXT_ERR_ALERT_FATAL:
2028 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2029 return -1;
David Benjamin03973092014-06-24 23:27:17 -04002030
Adam Langleyfcf25832014-12-18 17:42:32 -08002031 case SSL_TLSEXT_ERR_ALERT_WARNING:
2032 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2033 return 1;
2034
2035 default:
2036 return 1;
2037 }
2038}
2039
2040int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
2041 int alert = -1;
2042 if (s->version < SSL3_VERSION) {
2043 return 1;
2044 }
2045
2046 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
2047 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
2048 return 0;
2049 }
2050
2051 if (ssl_check_serverhello_tlsext(s) <= 0) {
2052 OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
2053 SSL_R_SERVERHELLO_TLSEXT);
2054 return 0;
2055 }
2056
2057 return 1;
2058}
Adam Langley95c29f32014-06-20 12:00:00 -07002059
2060/* Since the server cache lookup is done early on in the processing of the
2061 * ClientHello, and other operations depend on the result, we need to handle
2062 * any TLS session ticket extension at the same time.
2063 *
Adam Langleydc9b1412014-06-20 12:00:00 -07002064 * ctx: contains the early callback context, which is the result of a
2065 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07002066 * ret: (output) on return, if a ticket was decrypted, then this is set to
2067 * point to the resulting session.
2068 *
Adam Langley95c29f32014-06-20 12:00:00 -07002069 * Returns:
2070 * -1: fatal error, either from parsing or decrypting the ticket.
2071 * 0: no ticket was found (or was ignored, based on settings).
2072 * 1: a zero length extension was found, indicating that the client supports
2073 * session tickets but doesn't currently have one to offer.
David Benjamind1681e62014-11-20 14:54:14 -05002074 * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
2075 * error.
Adam Langley95c29f32014-06-20 12:00:00 -07002076 * 3: a ticket was successfully decrypted and *ret was set.
2077 *
2078 * Side effects:
2079 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2080 * a new session ticket to the client because the client indicated support
David Benjamind1681e62014-11-20 14:54:14 -05002081 * but the client either doesn't have a session ticket or we couldn't use
2082 * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
2083 * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
Adam Langley95c29f32014-06-20 12:00:00 -07002084 */
Adam Langleydc9b1412014-06-20 12:00:00 -07002085int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -08002086 SSL_SESSION **ret) {
2087 *ret = NULL;
2088 s->tlsext_ticket_expected = 0;
2089 const uint8_t *data;
2090 size_t len;
2091 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 /* If tickets disabled behave as if no ticket present to permit stateful
2094 * resumption. */
2095 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
2096 (s->version <= SSL3_VERSION && !ctx->extensions) ||
2097 !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
2098 &data, &len)) {
2099 return 0;
2100 }
2101
2102 if (len == 0) {
2103 /* The client will accept a ticket but doesn't currently have one. */
2104 s->tlsext_ticket_expected = 1;
2105 return 1;
2106 }
2107
2108 r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
2109 ret);
2110 switch (r) {
2111 case 2: /* ticket couldn't be decrypted */
2112 s->tlsext_ticket_expected = 1;
2113 return 2;
2114
2115 case 3: /* ticket was decrypted */
2116 return r;
2117
2118 case 4: /* ticket decrypted but need to renew */
2119 s->tlsext_ticket_expected = 1;
2120 return 3;
2121
2122 default: /* fatal error */
2123 return -1;
2124 }
2125}
Adam Langley95c29f32014-06-20 12:00:00 -07002126
2127/* tls_decrypt_ticket attempts to decrypt a session ticket.
2128 *
2129 * etick: points to the body of the session ticket extension.
2130 * eticklen: the length of the session tickets extenion.
2131 * sess_id: points at the session ID.
2132 * sesslen: the length of the session ID.
2133 * psess: (output) on return, if a ticket was decrypted, then this is set to
2134 * point to the resulting session.
2135 *
2136 * Returns:
2137 * -1: fatal error, either from parsing or decrypting the ticket.
2138 * 2: the ticket couldn't be decrypted.
2139 * 3: a ticket was successfully decrypted and *psess was set.
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 * 4: same as 3, but the ticket needs to be renewed. */
2141static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
2142 const uint8_t *sess_id, int sesslen,
2143 SSL_SESSION **psess) {
2144 SSL_SESSION *sess;
2145 uint8_t *sdec;
2146 const uint8_t *p;
2147 int slen, mlen, renew_ticket = 0;
2148 uint8_t tick_hmac[EVP_MAX_MD_SIZE];
2149 HMAC_CTX hctx;
2150 EVP_CIPHER_CTX ctx;
2151 SSL_CTX *tctx = s->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002152
Adam Langleyfcf25832014-12-18 17:42:32 -08002153 /* Need at least keyname + iv + some encrypted data */
2154 if (eticklen < 48) {
2155 return 2;
2156 }
2157
2158 /* Initialize session ticket encryption and HMAC contexts */
2159 HMAC_CTX_init(&hctx);
2160 EVP_CIPHER_CTX_init(&ctx);
2161 if (tctx->tlsext_ticket_key_cb) {
2162 uint8_t *nctick = (uint8_t *)etick;
2163 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx, 0);
2164 if (rv < 0) {
2165 return -1;
2166 }
2167 if (rv == 0) {
2168 return 2;
2169 }
2170 if (rv == 2) {
2171 renew_ticket = 1;
2172 }
2173 } else {
2174 /* Check key name matches */
2175 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
2176 return 2;
2177 }
2178 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2179 NULL) ||
2180 !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2181 tctx->tlsext_tick_aes_key, etick + 16)) {
2182 HMAC_CTX_cleanup(&hctx);
2183 EVP_CIPHER_CTX_cleanup(&ctx);
2184 return -1;
2185 }
2186 }
2187
2188 /* Attempt to process session ticket, first conduct sanity and integrity
2189 * checks on ticket. */
2190 mlen = HMAC_size(&hctx);
2191 if (mlen < 0) {
2192 HMAC_CTX_cleanup(&hctx);
2193 EVP_CIPHER_CTX_cleanup(&ctx);
2194 return -1;
2195 }
2196 eticklen -= mlen;
2197 /* Check HMAC of encrypted ticket */
2198 HMAC_Update(&hctx, etick, eticklen);
2199 HMAC_Final(&hctx, tick_hmac, NULL);
2200 HMAC_CTX_cleanup(&hctx);
2201 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2202 EVP_CIPHER_CTX_cleanup(&ctx);
2203 return 2;
2204 }
2205
2206 /* Attempt to decrypt session data */
2207 /* Move p after IV to start of encrypted ticket, update length */
2208 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2209 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2210 sdec = OPENSSL_malloc(eticklen);
2211 if (!sdec) {
2212 EVP_CIPHER_CTX_cleanup(&ctx);
2213 return -1;
2214 }
2215 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2216 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
2217 EVP_CIPHER_CTX_cleanup(&ctx);
2218 OPENSSL_free(sdec);
2219 return 2;
2220 }
2221 slen += mlen;
2222 EVP_CIPHER_CTX_cleanup(&ctx);
2223 p = sdec;
2224
2225 sess = d2i_SSL_SESSION(NULL, &p, slen);
2226 OPENSSL_free(sdec);
2227 if (sess) {
2228 /* The session ID, if non-empty, is used by some clients to detect that the
2229 * ticket has been accepted. So we copy it to the session structure. If it
2230 * is empty set length to zero as required by standard. */
2231 if (sesslen) {
2232 memcpy(sess->session_id, sess_id, sesslen);
2233 }
2234 sess->session_id_length = sesslen;
2235 *psess = sess;
2236 if (renew_ticket) {
2237 return 4;
2238 }
2239 return 3;
2240 }
2241
2242 ERR_clear_error();
2243 /* For session parse failure, indicate that we need to send a new ticket. */
2244 return 2;
2245}
Adam Langley95c29f32014-06-20 12:00:00 -07002246
2247/* Tables to translate from NIDs to TLS v1.2 ids */
Adam Langleyfcf25832014-12-18 17:42:32 -08002248typedef struct {
2249 int nid;
2250 int id;
2251} tls12_lookup;
Adam Langley95c29f32014-06-20 12:00:00 -07002252
Adam Langleyfcf25832014-12-18 17:42:32 -08002253static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
2254 {NID_sha1, TLSEXT_hash_sha1},
2255 {NID_sha224, TLSEXT_hash_sha224},
2256 {NID_sha256, TLSEXT_hash_sha256},
2257 {NID_sha384, TLSEXT_hash_sha384},
2258 {NID_sha512, TLSEXT_hash_sha512}};
Adam Langley95c29f32014-06-20 12:00:00 -07002259
Adam Langleyfcf25832014-12-18 17:42:32 -08002260static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
2261 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
Adam Langley95c29f32014-06-20 12:00:00 -07002262
Adam Langleyfcf25832014-12-18 17:42:32 -08002263static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
2264 size_t i;
2265 for (i = 0; i < tlen; i++) {
2266 if (table[i].nid == nid) {
2267 return table[i].id;
2268 }
2269 }
Adam Langley95c29f32014-06-20 12:00:00 -07002270
Adam Langleyfcf25832014-12-18 17:42:32 -08002271 return -1;
2272}
Adam Langley95c29f32014-06-20 12:00:00 -07002273
Adam Langleyfcf25832014-12-18 17:42:32 -08002274static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
2275 size_t i;
2276 for (i = 0; i < tlen; i++) {
2277 if (table[i].id == id) {
2278 return table[i].nid;
2279 }
2280 }
Adam Langley95c29f32014-06-20 12:00:00 -07002281
Adam Langleyfcf25832014-12-18 17:42:32 -08002282 return NID_undef;
2283}
Adam Langley95c29f32014-06-20 12:00:00 -07002284
Adam Langleyfcf25832014-12-18 17:42:32 -08002285int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
2286 int sig_id, md_id;
Adam Langley95c29f32014-06-20 12:00:00 -07002287
Adam Langleyfcf25832014-12-18 17:42:32 -08002288 if (!md) {
2289 return 0;
2290 }
Adam Langley95c29f32014-06-20 12:00:00 -07002291
Adam Langleyfcf25832014-12-18 17:42:32 -08002292 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2293 sizeof(tls12_md) / sizeof(tls12_lookup));
2294 if (md_id == -1) {
2295 return 0;
2296 }
Adam Langley95c29f32014-06-20 12:00:00 -07002297
Adam Langleyfcf25832014-12-18 17:42:32 -08002298 sig_id = tls12_get_sigid(pk);
2299 if (sig_id == -1) {
2300 return 0;
2301 }
Adam Langley95c29f32014-06-20 12:00:00 -07002302
Adam Langleyfcf25832014-12-18 17:42:32 -08002303 p[0] = (uint8_t)md_id;
2304 p[1] = (uint8_t)sig_id;
2305 return 1;
2306}
2307
2308int tls12_get_sigid(const EVP_PKEY *pk) {
2309 return tls12_find_id(pk->type, tls12_sig,
2310 sizeof(tls12_sig) / sizeof(tls12_lookup));
2311}
2312
2313const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
2314 switch (hash_alg) {
2315 case TLSEXT_hash_md5:
2316 return EVP_md5();
2317
2318 case TLSEXT_hash_sha1:
2319 return EVP_sha1();
2320
2321 case TLSEXT_hash_sha224:
2322 return EVP_sha224();
2323
2324 case TLSEXT_hash_sha256:
2325 return EVP_sha256();
2326
2327 case TLSEXT_hash_sha384:
2328 return EVP_sha384();
2329
2330 case TLSEXT_hash_sha512:
2331 return EVP_sha512();
2332
2333 default:
2334 return NULL;
2335 }
2336}
Adam Langley95c29f32014-06-20 12:00:00 -07002337
David Benjaminec2f27d2014-11-13 19:17:25 -05002338/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
2339 * algorithm |sig_alg|. It returns -1 if the type is unknown. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002340static int tls12_get_pkey_type(uint8_t sig_alg) {
2341 switch (sig_alg) {
2342 case TLSEXT_signature_rsa:
2343 return EVP_PKEY_RSA;
2344
2345 case TLSEXT_signature_ecdsa:
2346 return EVP_PKEY_EC;
2347
2348 default:
2349 return -1;
2350 }
2351}
Adam Langley95c29f32014-06-20 12:00:00 -07002352
2353/* Convert TLS 1.2 signature algorithm extension values into NIDs */
2354static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
Adam Langleyfcf25832014-12-18 17:42:32 -08002355 int *psignhash_nid, const uint8_t *data) {
2356 int sign_nid = 0, hash_nid = 0;
2357 if (!phash_nid && !psign_nid && !psignhash_nid) {
2358 return;
2359 }
2360
2361 if (phash_nid || psignhash_nid) {
2362 hash_nid = tls12_find_nid(data[0], tls12_md,
2363 sizeof(tls12_md) / sizeof(tls12_lookup));
2364 if (phash_nid) {
2365 *phash_nid = hash_nid;
2366 }
2367 }
2368
2369 if (psign_nid || psignhash_nid) {
2370 sign_nid = tls12_find_nid(data[1], tls12_sig,
2371 sizeof(tls12_sig) / sizeof(tls12_lookup));
2372 if (psign_nid) {
2373 *psign_nid = sign_nid;
2374 }
2375 }
2376
2377 if (psignhash_nid) {
2378 if (sign_nid && hash_nid) {
2379 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
2380 } else {
2381 *psignhash_nid = NID_undef;
2382 }
2383 }
2384}
2385
Adam Langley95c29f32014-06-20 12:00:00 -07002386/* Given preference and allowed sigalgs set shared sigalgs */
Adam Langleyfcf25832014-12-18 17:42:32 -08002387static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
2388 size_t preflen, const uint8_t *allow,
2389 size_t allowlen) {
2390 const uint8_t *ptmp, *atmp;
2391 size_t i, j, nmatch = 0;
2392
2393 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
2394 /* Skip disabled hashes or signature algorithms */
2395 if (tls12_get_hash(ptmp[0]) == NULL ||
2396 tls12_get_pkey_type(ptmp[1]) == -1) {
2397 continue;
2398 }
2399
2400 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
2401 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
2402 nmatch++;
2403 if (shsig) {
2404 shsig->rhash = ptmp[0];
2405 shsig->rsign = ptmp[1];
2406 tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
2407 &shsig->signandhash_nid, ptmp);
2408 shsig++;
2409 }
2410
2411 break;
2412 }
2413 }
2414 }
2415
2416 return nmatch;
2417}
Adam Langley95c29f32014-06-20 12:00:00 -07002418
2419/* Set shared signature algorithms for SSL structures */
Adam Langleyfcf25832014-12-18 17:42:32 -08002420static int tls1_set_shared_sigalgs(SSL *s) {
2421 const uint8_t *pref, *allow, *conf;
2422 size_t preflen, allowlen, conflen;
2423 size_t nmatch;
2424 TLS_SIGALGS *salgs = NULL;
2425 CERT *c = s->cert;
2426
2427 if (c->shared_sigalgs) {
2428 OPENSSL_free(c->shared_sigalgs);
2429 c->shared_sigalgs = NULL;
David Benjamin61c0d4e2015-03-19 14:24:37 -04002430 c->shared_sigalgslen = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002431 }
2432
2433 /* If client use client signature algorithms if not NULL */
2434 if (!s->server && c->client_sigalgs) {
2435 conf = c->client_sigalgs;
2436 conflen = c->client_sigalgslen;
2437 } else if (c->conf_sigalgs) {
2438 conf = c->conf_sigalgs;
2439 conflen = c->conf_sigalgslen;
2440 } else {
2441 conflen = tls12_get_psigalgs(s, &conf);
2442 }
2443
2444 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
2445 pref = conf;
2446 preflen = conflen;
2447 allow = c->peer_sigalgs;
2448 allowlen = c->peer_sigalgslen;
2449 } else {
2450 allow = conf;
2451 allowlen = conflen;
2452 pref = c->peer_sigalgs;
2453 preflen = c->peer_sigalgslen;
2454 }
2455
2456 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
2457 if (!nmatch) {
2458 return 1;
2459 }
2460
2461 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
2462 if (!salgs) {
2463 return 0;
2464 }
2465
2466 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
2467 c->shared_sigalgs = salgs;
2468 c->shared_sigalgslen = nmatch;
2469 return 1;
2470}
Adam Langley95c29f32014-06-20 12:00:00 -07002471
2472/* Set preferred digest for each key type */
Adam Langleyfcf25832014-12-18 17:42:32 -08002473int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
2474 CERT *c = s->cert;
Adam Langley95c29f32014-06-20 12:00:00 -07002475
Adam Langleyfcf25832014-12-18 17:42:32 -08002476 /* Extension ignored for inappropriate versions */
2477 if (!SSL_USE_SIGALGS(s)) {
2478 return 1;
2479 }
David Benjamincd996942014-07-20 16:23:51 -04002480
David Benjamin61c0d4e2015-03-19 14:24:37 -04002481 if (CBS_len(sigalgs) % 2 != 0 ||
2482 !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
2483 !tls1_set_shared_sigalgs(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002484 return 0;
2485 }
Adam Langley95c29f32014-06-20 12:00:00 -07002486
Adam Langleyfcf25832014-12-18 17:42:32 -08002487 return 1;
2488}
David Benjaminec2f27d2014-11-13 19:17:25 -05002489
Adam Langleyfcf25832014-12-18 17:42:32 -08002490const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
2491 CERT *c = s->cert;
2492 int type = EVP_PKEY_id(pkey);
2493 size_t i;
David Benjaminec2f27d2014-11-13 19:17:25 -05002494
Adam Langleyfcf25832014-12-18 17:42:32 -08002495 /* Select the first shared digest supported by our key. */
2496 for (i = 0; i < c->shared_sigalgslen; i++) {
2497 const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
2498 if (md == NULL ||
2499 tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
2500 !EVP_PKEY_supports_digest(pkey, md)) {
2501 continue;
2502 }
2503 return md;
2504 }
Adam Langley95c29f32014-06-20 12:00:00 -07002505
Adam Langleyfcf25832014-12-18 17:42:32 -08002506 /* If no suitable digest may be found, default to SHA-1. */
2507 return EVP_sha1();
2508}
Adam Langley95c29f32014-06-20 12:00:00 -07002509
Adam Langleyfcf25832014-12-18 17:42:32 -08002510int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
2511 uint8_t *rsig, uint8_t *rhash) {
2512 const uint8_t *psig = s->cert->peer_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002513
Adam Langleyfcf25832014-12-18 17:42:32 -08002514 if (psig == NULL) {
2515 return 0;
2516 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002517
Adam Langleyfcf25832014-12-18 17:42:32 -08002518 if (idx >= 0) {
2519 idx <<= 1;
2520 if (idx >= (int)s->cert->peer_sigalgslen) {
2521 return 0;
2522 }
2523 psig += idx;
2524 if (rhash) {
2525 *rhash = psig[0];
2526 }
2527 if (rsig) {
2528 *rsig = psig[1];
2529 }
2530 tls1_lookup_sigalg(phash, psign, psignhash, psig);
2531 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002532
Adam Langleyfcf25832014-12-18 17:42:32 -08002533 return s->cert->peer_sigalgslen / 2;
2534}
Adam Langley1258b6a2014-06-20 12:00:00 -07002535
Adam Langleyfcf25832014-12-18 17:42:32 -08002536int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
2537 int *psignhash, uint8_t *rsig, uint8_t *rhash) {
2538 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
Adam Langley1258b6a2014-06-20 12:00:00 -07002539
Adam Langleyfcf25832014-12-18 17:42:32 -08002540 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
2541 return 0;
2542 }
2543
2544 shsigalgs += idx;
2545 if (phash) {
2546 *phash = shsigalgs->hash_nid;
2547 }
2548 if (psign) {
2549 *psign = shsigalgs->sign_nid;
2550 }
2551 if (psignhash) {
2552 *psignhash = shsigalgs->signandhash_nid;
2553 }
2554 if (rsig) {
2555 *rsig = shsigalgs->rsign;
2556 }
2557 if (rhash) {
2558 *rhash = shsigalgs->rhash;
2559 }
2560
2561 return s->cert->shared_sigalgslen;
2562}
2563
2564/* tls1_channel_id_hash calculates the signed data for a Channel ID on the
2565 * given SSL connection and writes it to |md|. */
2566int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
2567 EVP_MD_CTX ctx;
2568 uint8_t temp_digest[EVP_MAX_MD_SIZE];
2569 unsigned temp_digest_len;
2570 int i;
2571 static const char kClientIDMagic[] = "TLS Channel ID signature";
2572
2573 if (s->s3->handshake_buffer &&
2574 !ssl3_digest_cached_records(s, free_handshake_buffer)) {
2575 return 0;
2576 }
2577
2578 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
2579
2580 if (s->hit && s->s3->tlsext_channel_id_new) {
2581 static const char kResumptionMagic[] = "Resumption";
2582 EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
2583 if (s->session->original_handshake_hash_len == 0) {
2584 return 0;
2585 }
2586 EVP_DigestUpdate(md, s->session->original_handshake_hash,
2587 s->session->original_handshake_hash_len);
2588 }
2589
2590 EVP_MD_CTX_init(&ctx);
2591 for (i = 0; i < SSL_MAX_DIGEST; i++) {
2592 if (s->s3->handshake_dgst[i] == NULL) {
2593 continue;
2594 }
David Benjamin9d0847a2015-02-16 03:57:55 -05002595 if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
2596 EVP_MD_CTX_cleanup(&ctx);
2597 return 0;
2598 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002599 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
2600 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
2601 }
2602 EVP_MD_CTX_cleanup(&ctx);
2603
2604 return 1;
2605}
Adam Langley1258b6a2014-06-20 12:00:00 -07002606
2607/* tls1_record_handshake_hashes_for_channel_id records the current handshake
2608 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
Adam Langleyfcf25832014-12-18 17:42:32 -08002609int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
2610 int digest_len;
2611 /* This function should never be called for a resumed session because the
2612 * handshake hashes that we wish to record are for the original, full
2613 * handshake. */
2614 if (s->hit) {
2615 return -1;
2616 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002617
Adam Langleyfcf25832014-12-18 17:42:32 -08002618 /* It only makes sense to call this function if Channel IDs have been
2619 * negotiated. */
2620 if (!s->s3->tlsext_channel_id_new) {
2621 return -1;
2622 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002623
Adam Langleyfcf25832014-12-18 17:42:32 -08002624 digest_len =
2625 tls1_handshake_digest(s, s->session->original_handshake_hash,
2626 sizeof(s->session->original_handshake_hash));
2627 if (digest_len < 0) {
2628 return -1;
2629 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002630
Adam Langleyfcf25832014-12-18 17:42:32 -08002631 s->session->original_handshake_hash_len = digest_len;
Adam Langley1258b6a2014-06-20 12:00:00 -07002632
Adam Langleyfcf25832014-12-18 17:42:32 -08002633 return 1;
2634}
Adam Langley95c29f32014-06-20 12:00:00 -07002635
Adam Langleyfcf25832014-12-18 17:42:32 -08002636int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
2637 int client) {
2638 uint8_t *sigalgs, *sptr;
2639 int rhash, rsign;
2640 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -07002641
Adam Langleyfcf25832014-12-18 17:42:32 -08002642 if (salglen & 1) {
2643 return 0;
2644 }
Adam Langley95c29f32014-06-20 12:00:00 -07002645
Adam Langleyfcf25832014-12-18 17:42:32 -08002646 sigalgs = OPENSSL_malloc(salglen);
2647 if (sigalgs == NULL) {
2648 return 0;
2649 }
Adam Langley95c29f32014-06-20 12:00:00 -07002650
Adam Langleyfcf25832014-12-18 17:42:32 -08002651 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2652 rhash = tls12_find_id(*psig_nids++, tls12_md,
2653 sizeof(tls12_md) / sizeof(tls12_lookup));
2654 rsign = tls12_find_id(*psig_nids++, tls12_sig,
2655 sizeof(tls12_sig) / sizeof(tls12_lookup));
Adam Langley95c29f32014-06-20 12:00:00 -07002656
Adam Langleyfcf25832014-12-18 17:42:32 -08002657 if (rhash == -1 || rsign == -1) {
2658 goto err;
2659 }
2660 *sptr++ = rhash;
2661 *sptr++ = rsign;
2662 }
2663
2664 if (client) {
2665 if (c->client_sigalgs) {
2666 OPENSSL_free(c->client_sigalgs);
2667 }
2668 c->client_sigalgs = sigalgs;
2669 c->client_sigalgslen = salglen;
2670 } else {
2671 if (c->conf_sigalgs) {
2672 OPENSSL_free(c->conf_sigalgs);
2673 }
2674 c->conf_sigalgs = sigalgs;
2675 c->conf_sigalgslen = salglen;
2676 }
2677
2678 return 1;
2679
2680err:
2681 OPENSSL_free(sigalgs);
2682 return 0;
2683}