blob: dea29d5b8f99a5cec9c95482644ee6a34f7ef875 [file] [log] [blame]
Matt Caswell8ba708e2015-09-11 10:48:59 +01001/* ssl/statem/statem_clnt.c */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00002/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
Matt Caswell0f113f32015-01-22 03:40:55 +00008 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00009 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Matt Caswell0f113f32015-01-22 03:40:55 +000015 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000016 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
Matt Caswell0f113f32015-01-22 03:40:55 +000022 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000023 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
Matt Caswell0f113f32015-01-22 03:40:55 +000037 * 4. If you include any Windows specific code (or a derivative thereof) from
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000038 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Matt Caswell0f113f32015-01-22 03:40:55 +000040 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000041 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
Matt Caswell0f113f32015-01-22 03:40:55 +000052 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000053 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
Bodo Möller8c74b5e2002-01-14 23:40:26 +000058/* ====================================================================
Bodo Möller52b8dad2007-02-17 06:45:38 +000059 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
Bodo Möller8c74b5e2002-01-14 23:40:26 +000060 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
Matt Caswell0f113f32015-01-22 03:40:55 +000066 * notice, this list of conditions and the following disclaimer.
Bodo Möller8c74b5e2002-01-14 23:40:26 +000067 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
Bodo Möllerea262262002-08-09 08:56:08 +0000111/* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 *
Matt Caswell0f113f32015-01-22 03:40:55 +0000114 * Portions of the attached software ("Contribution") are developed by
Bodo Möllerea262262002-08-09 08:56:08 +0000115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 *
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
119 *
Bodo Möllerea262262002-08-09 08:56:08 +0000120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122 *
123 */
Nils Larschddac1972006-03-10 23:06:27 +0000124/* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
126 *
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129 * license.
130 *
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
134 *
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
138 *
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
143 *
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148 * OTHERWISE.
149 */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000150
151#include <stdio.h>
Matt Caswell8ba708e2015-09-11 10:48:59 +0100152#include "../ssl_locl.h"
Matt Caswell61ae9352015-09-11 11:23:20 +0100153#include "statem_locl.h"
Bodo Möllerec577821999-04-23 22:13:45 +0000154#include <openssl/buffer.h>
155#include <openssl/rand.h>
156#include <openssl/objects.h>
157#include <openssl/evp.h>
Ben Lauriedbad1692001-07-30 23:57:25 +0000158#include <openssl/md5.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +0000159#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +0000160# include <openssl/dh.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +0000161#endif
Geoff Thorped095b682004-05-17 18:53:47 +0000162#include <openssl/bn.h>
Dr. Stephen Henson368888b2008-06-01 22:33:24 +0000163#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000164# include <openssl/engine.h>
Dr. Stephen Henson368888b2008-06-01 22:33:24 +0000165#endif
Richard Levittef9b3bff2000-11-30 22:53:34 +0000166
Matt Caswell61ae9352015-09-11 11:23:20 +0100167static inline int cert_req_allowed(SSL *s);
Matt Caswella455d0f2015-09-14 15:06:37 +0100168static int key_exchange_expected(SSL *s);
Matt Caswelld45ba432015-04-24 15:05:27 +0100169static int ssl_set_version(SSL *s);
Matt Caswell0f113f32015-01-22 03:40:55 +0000170static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
Matt Caswelld45ba432015-04-24 15:05:27 +0100171static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
Emilia Kaspere9fa0922015-09-18 15:00:37 +0200172 unsigned char *p);
Bodo Möllerea262262002-08-09 08:56:08 +0000173
Matt Caswell61ae9352015-09-11 11:23:20 +0100174
175/*
176 * Is a CertificateRequest message allowed at the moment or not?
177 *
178 * Return values are:
179 * 1: Yes
180 * 0: No
181 */
182static inline int cert_req_allowed(SSL *s)
183{
184 /* TLS does not like anon-DH with client cert */
Matt Caswellb7fa1f92015-10-26 23:11:44 +0000185 if ((s->version > SSL3_VERSION
186 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
187 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
Matt Caswell61ae9352015-09-11 11:23:20 +0100188 return 0;
189
190 return 1;
191}
192
193/*
Matt Caswella455d0f2015-09-14 15:06:37 +0100194 * Should we expect the ServerKeyExchange message or not?
Matt Caswell61ae9352015-09-11 11:23:20 +0100195 *
196 * Return values are:
197 * 1: Yes
198 * 0: No
Matt Caswella455d0f2015-09-14 15:06:37 +0100199 * -1: Error
Matt Caswell61ae9352015-09-11 11:23:20 +0100200 */
Matt Caswella455d0f2015-09-14 15:06:37 +0100201static int key_exchange_expected(SSL *s)
Matt Caswell61ae9352015-09-11 11:23:20 +0100202{
203 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
204
205 /*
206 * Can't skip server key exchange if this is an ephemeral
Matt Caswella455d0f2015-09-14 15:06:37 +0100207 * ciphersuite or for SRP
Matt Caswell61ae9352015-09-11 11:23:20 +0100208 */
Matt Caswella455d0f2015-09-14 15:06:37 +0100209 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
210 | SSL_kSRP)) {
211 return 1;
Matt Caswell61ae9352015-09-11 11:23:20 +0100212 }
213
Matt Caswella455d0f2015-09-14 15:06:37 +0100214 /*
215 * Export ciphersuites may have temporary RSA keys if the public key in the
216 * server certificate is longer than the maximum export strength
217 */
218 if ((alg_k & SSL_kRSA) && SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
219 EVP_PKEY *pkey;
220
221 pkey = X509_get_pubkey(s->session->peer);
222 if (pkey == NULL)
223 return -1;
224
225 /*
226 * If the public key in the certificate is shorter than or equal to the
227 * maximum export strength then a temporary RSA key is not allowed
228 */
229 if (EVP_PKEY_bits(pkey)
230 <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
231 return 0;
232
233 EVP_PKEY_free(pkey);
234
235 return 1;
236 }
237
238 return 0;
Matt Caswell61ae9352015-09-11 11:23:20 +0100239}
240
241/*
Matt Caswell8481f582015-10-26 11:54:17 +0000242 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
243 * handshake state transitions when the client is reading messages from the
244 * server. The message type that the server has sent is provided in |mt|. The
245 * current state is in |s->statem.hand_state|.
Matt Caswell61ae9352015-09-11 11:23:20 +0100246 *
247 * Return values are:
248 * 1: Success (transition allowed)
249 * 0: Error (transition not allowed)
250 */
Matt Caswell8481f582015-10-26 11:54:17 +0000251int ossl_statem_client_read_transition(SSL *s, int mt)
Matt Caswell61ae9352015-09-11 11:23:20 +0100252{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100253 OSSL_STATEM *st = &s->statem;
Matt Caswella455d0f2015-09-14 15:06:37 +0100254 int ske_expected;
Matt Caswell61ae9352015-09-11 11:23:20 +0100255
256 switch(st->hand_state) {
257 case TLS_ST_CW_CLNT_HELLO:
258 if (mt == SSL3_MT_SERVER_HELLO) {
259 st->hand_state = TLS_ST_CR_SRVR_HELLO;
260 return 1;
261 }
262
263 if (SSL_IS_DTLS(s)) {
264 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
265 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
266 return 1;
267 }
268 }
269 break;
270
271 case TLS_ST_CR_SRVR_HELLO:
272 if (s->hit) {
273 if (s->tlsext_ticket_expected) {
274 if (mt == SSL3_MT_NEWSESSION_TICKET) {
275 st->hand_state = TLS_ST_CR_SESSION_TICKET;
276 return 1;
277 }
278 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
279 st->hand_state = TLS_ST_CR_CHANGE;
280 return 1;
281 }
282 } else {
283 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
284 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
285 return 1;
286 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
287 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
288 if (mt == SSL3_MT_CERTIFICATE) {
289 st->hand_state = TLS_ST_CR_CERT;
290 return 1;
291 }
292 } else {
Matt Caswella455d0f2015-09-14 15:06:37 +0100293 ske_expected = key_exchange_expected(s);
294 if (ske_expected < 0)
295 return 0;
296 /* SKE is optional for some PSK ciphersuites */
297 if (ske_expected
298 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
299 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
300 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
301 st->hand_state = TLS_ST_CR_KEY_EXCH;
302 return 1;
303 }
304 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
Matt Caswell61ae9352015-09-11 11:23:20 +0100305 && cert_req_allowed(s)) {
306 st->hand_state = TLS_ST_CR_CERT_REQ;
307 return 1;
Matt Caswella455d0f2015-09-14 15:06:37 +0100308 } else if (mt == SSL3_MT_SERVER_DONE) {
Matt Caswell61ae9352015-09-11 11:23:20 +0100309 st->hand_state = TLS_ST_CR_SRVR_DONE;
310 return 1;
Matt Caswell61ae9352015-09-11 11:23:20 +0100311 }
312 }
313 }
314 break;
315
316 case TLS_ST_CR_CERT:
317 if (s->tlsext_status_expected) {
318 if (mt == SSL3_MT_CERTIFICATE_STATUS) {
319 st->hand_state = TLS_ST_CR_CERT_STATUS;
320 return 1;
321 }
Matt Caswella455d0f2015-09-14 15:06:37 +0100322 return 0;
323 }
324 /* Fall through */
325
326 case TLS_ST_CR_CERT_STATUS:
327 ske_expected = key_exchange_expected(s);
328 if (ske_expected < 0)
329 return 0;
330 /* SKE is optional for some PSK ciphersuites */
331 if (ske_expected
332 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
333 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
Matt Caswell61ae9352015-09-11 11:23:20 +0100334 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
335 st->hand_state = TLS_ST_CR_KEY_EXCH;
336 return 1;
Matt Caswell61ae9352015-09-11 11:23:20 +0100337 }
Matt Caswella455d0f2015-09-14 15:06:37 +0100338 return 0;
Matt Caswell61ae9352015-09-11 11:23:20 +0100339 }
Matt Caswella455d0f2015-09-14 15:06:37 +0100340 /* Fall through */
Matt Caswell61ae9352015-09-11 11:23:20 +0100341
342 case TLS_ST_CR_KEY_EXCH:
Matt Caswella455d0f2015-09-14 15:06:37 +0100343 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
344 if (cert_req_allowed(s)) {
345 st->hand_state = TLS_ST_CR_CERT_REQ;
346 return 1;
347 }
348 return 0;
Matt Caswell61ae9352015-09-11 11:23:20 +0100349 }
Matt Caswella455d0f2015-09-14 15:06:37 +0100350 /* Fall through */
Matt Caswell61ae9352015-09-11 11:23:20 +0100351
352 case TLS_ST_CR_CERT_REQ:
353 if (mt == SSL3_MT_SERVER_DONE) {
354 st->hand_state = TLS_ST_CR_SRVR_DONE;
355 return 1;
356 }
357 break;
358
359 case TLS_ST_CW_FINISHED:
360 if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
361 st->hand_state = TLS_ST_CR_SESSION_TICKET;
362 return 1;
363 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
364 st->hand_state = TLS_ST_CR_CHANGE;
365 return 1;
366 }
367 break;
368
369 case TLS_ST_CR_SESSION_TICKET:
370 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
371 st->hand_state = TLS_ST_CR_CHANGE;
372 return 1;
373 }
374 break;
375
376 case TLS_ST_CR_CHANGE:
377 if (mt == SSL3_MT_FINISHED) {
378 st->hand_state = TLS_ST_CR_FINISHED;
379 return 1;
380 }
381 break;
382
383 default:
384 break;
385 }
386
387 /* No valid transition found */
388 return 0;
389}
390
391/*
392 * client_write_transition() works out what handshake state to move to next
393 * when the client is writing messages to be sent to the server.
394 */
Matt Caswell8481f582015-10-26 11:54:17 +0000395WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
Matt Caswell61ae9352015-09-11 11:23:20 +0100396{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100397 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100398
399 switch(st->hand_state) {
400 case TLS_ST_OK:
401 /* Renegotiation - fall through */
402 case TLS_ST_BEFORE:
403 st->hand_state = TLS_ST_CW_CLNT_HELLO;
404 return WRITE_TRAN_CONTINUE;
405
406 case TLS_ST_CW_CLNT_HELLO:
407 /*
408 * No transition at the end of writing because we don't know what
409 * we will be sent
410 */
411 return WRITE_TRAN_FINISHED;
412
413 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
414 st->hand_state = TLS_ST_CW_CLNT_HELLO;
415 return WRITE_TRAN_CONTINUE;
416
417 case TLS_ST_CR_SRVR_DONE:
418 if (s->s3->tmp.cert_req)
419 st->hand_state = TLS_ST_CW_CERT;
420 else
421 st->hand_state = TLS_ST_CW_KEY_EXCH;
422 return WRITE_TRAN_CONTINUE;
423
424 case TLS_ST_CW_CERT:
425 st->hand_state = TLS_ST_CW_KEY_EXCH;
426 return WRITE_TRAN_CONTINUE;
427
428 case TLS_ST_CW_KEY_EXCH:
429 /*
430 * For TLS, cert_req is set to 2, so a cert chain of nothing is
431 * sent, but no verify packet is sent
432 */
433 /*
434 * XXX: For now, we do not support client authentication in ECDH
435 * cipher suites with ECDH (rather than ECDSA) certificates. We
436 * need to skip the certificate verify message when client's
437 * ECDH public key is sent inside the client certificate.
438 */
439 if (s->s3->tmp.cert_req == 1) {
440 st->hand_state = TLS_ST_CW_CERT_VRFY;
441 } else {
442 st->hand_state = TLS_ST_CW_CHANGE;
443 }
444 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
445 st->hand_state = TLS_ST_CW_CHANGE;
446 }
447 return WRITE_TRAN_CONTINUE;
448
449 case TLS_ST_CW_CERT_VRFY:
450 st->hand_state = TLS_ST_CW_CHANGE;
451 return WRITE_TRAN_CONTINUE;
452
453 case TLS_ST_CW_CHANGE:
454#if defined(OPENSSL_NO_NEXTPROTONEG)
455 st->hand_state = TLS_ST_CW_FINISHED;
456#else
457 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
458 st->hand_state = TLS_ST_CW_NEXT_PROTO;
459 else
460 st->hand_state = TLS_ST_CW_FINISHED;
461#endif
462 return WRITE_TRAN_CONTINUE;
463
464#if !defined(OPENSSL_NO_NEXTPROTONEG)
465 case TLS_ST_CW_NEXT_PROTO:
466 st->hand_state = TLS_ST_CW_FINISHED;
467 return WRITE_TRAN_CONTINUE;
468#endif
469
470 case TLS_ST_CW_FINISHED:
471 if (s->hit) {
472 st->hand_state = TLS_ST_OK;
Matt Caswellfe3a3292015-10-05 10:39:54 +0100473 ossl_statem_set_in_init(s, 0);
Matt Caswell61ae9352015-09-11 11:23:20 +0100474 return WRITE_TRAN_CONTINUE;
475 } else {
476 return WRITE_TRAN_FINISHED;
477 }
478
479 case TLS_ST_CR_FINISHED:
480 if (s->hit) {
481 st->hand_state = TLS_ST_CW_CHANGE;
482 return WRITE_TRAN_CONTINUE;
483 } else {
484 st->hand_state = TLS_ST_OK;
Matt Caswellfe3a3292015-10-05 10:39:54 +0100485 ossl_statem_set_in_init(s, 0);
Matt Caswell61ae9352015-09-11 11:23:20 +0100486 return WRITE_TRAN_CONTINUE;
487 }
488
489 default:
490 /* Shouldn't happen */
491 return WRITE_TRAN_ERROR;
492 }
493}
494
495/*
496 * Perform any pre work that needs to be done prior to sending a message from
497 * the client to the server.
498 */
Matt Caswell8481f582015-10-26 11:54:17 +0000499WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
Matt Caswell61ae9352015-09-11 11:23:20 +0100500{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100501 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100502
503 switch(st->hand_state) {
504 case TLS_ST_CW_CLNT_HELLO:
505 s->shutdown = 0;
506 if (SSL_IS_DTLS(s)) {
507 /* every DTLS ClientHello resets Finished MAC */
508 ssl3_init_finished_mac(s);
509 }
510 break;
511
512 case TLS_ST_CW_CERT:
513 return tls_prepare_client_certificate(s, wst);
514
515 case TLS_ST_CW_CHANGE:
516 if (SSL_IS_DTLS(s)) {
517 if (s->hit) {
518 /*
519 * We're into the last flight so we don't retransmit these
520 * messages unless we need to.
521 */
522 st->use_timer = 0;
523 }
524#ifndef OPENSSL_NO_SCTP
525 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
526 return dtls_wait_for_dry(s);
527#endif
528 }
529 return WORK_FINISHED_CONTINUE;
530
531 case TLS_ST_OK:
532 return tls_finish_handshake(s, wst);
533
534 default:
535 /* No pre work to be done */
536 break;
537 }
538
539 return WORK_FINISHED_CONTINUE;
540}
541
542/*
543 * Perform any work that needs to be done after sending a message from the
544 * client to the server.
545 */
Matt Caswell8481f582015-10-26 11:54:17 +0000546WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
Matt Caswell61ae9352015-09-11 11:23:20 +0100547{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100548 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100549
550 s->init_num = 0;
551
552 switch(st->hand_state) {
553 case TLS_ST_CW_CLNT_HELLO:
554 if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
555 return WORK_MORE_A;
556#ifndef OPENSSL_NO_SCTP
557 /* Disable buffering for SCTP */
558 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
559#endif
560 /*
561 * turn on buffering for the next lot of output
562 */
563 if (s->bbio != s->wbio)
564 s->wbio = BIO_push(s->bbio, s->wbio);
565#ifndef OPENSSL_NO_SCTP
566 }
567#endif
568 if (SSL_IS_DTLS(s)) {
569 /* Treat the next message as the first packet */
570 s->first_packet = 1;
571 }
572 break;
573
574 case TLS_ST_CW_KEY_EXCH:
575 if (tls_client_key_exchange_post_work(s) == 0)
576 return WORK_ERROR;
577 break;
578
579 case TLS_ST_CW_CHANGE:
580 s->session->cipher = s->s3->tmp.new_cipher;
581#ifdef OPENSSL_NO_COMP
582 s->session->compress_meth = 0;
583#else
584 if (s->s3->tmp.new_compression == NULL)
585 s->session->compress_meth = 0;
586 else
587 s->session->compress_meth = s->s3->tmp.new_compression->id;
588#endif
589 if (!s->method->ssl3_enc->setup_key_block(s))
590 return WORK_ERROR;
591
592 if (!s->method->ssl3_enc->change_cipher_state(s,
593 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
594 return WORK_ERROR;
595
596 if (SSL_IS_DTLS(s)) {
597#ifndef OPENSSL_NO_SCTP
598 if (s->hit) {
599 /*
600 * Change to new shared key of SCTP-Auth, will be ignored if
601 * no SCTP used.
602 */
603 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
604 0, NULL);
605 }
606#endif
607
608 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
609 }
610 break;
611
612 case TLS_ST_CW_FINISHED:
613#ifndef OPENSSL_NO_SCTP
614 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
615 /*
616 * Change to new shared key of SCTP-Auth, will be ignored if
617 * no SCTP used.
618 */
619 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
620 0, NULL);
621 }
622#endif
623 if (statem_flush(s) != 1)
624 return WORK_MORE_B;
Matt Caswell61ae9352015-09-11 11:23:20 +0100625 break;
626
627 default:
628 /* No post work to be done */
629 break;
630 }
631
632 return WORK_FINISHED_CONTINUE;
633}
634
635/*
636 * Construct a message to be sent from the client to the server.
637 *
638 * Valid return values are:
639 * 1: Success
640 * 0: Error
641 */
Matt Caswell8481f582015-10-26 11:54:17 +0000642int ossl_statem_client_construct_message(SSL *s)
Matt Caswell61ae9352015-09-11 11:23:20 +0100643{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100644 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100645
646 switch(st->hand_state) {
647 case TLS_ST_CW_CLNT_HELLO:
648 return tls_construct_client_hello(s);
649
650 case TLS_ST_CW_CERT:
651 return tls_construct_client_certificate(s);
652
653 case TLS_ST_CW_KEY_EXCH:
654 return tls_construct_client_key_exchange(s);
655
656 case TLS_ST_CW_CERT_VRFY:
657 return tls_construct_client_verify(s);
658
659 case TLS_ST_CW_CHANGE:
660 if (SSL_IS_DTLS(s))
661 return dtls_construct_change_cipher_spec(s);
662 else
663 return tls_construct_change_cipher_spec(s);
664
665#if !defined(OPENSSL_NO_NEXTPROTONEG)
666 case TLS_ST_CW_NEXT_PROTO:
667 return tls_construct_next_proto(s);
668#endif
669 case TLS_ST_CW_FINISHED:
670 return tls_construct_finished(s,
671 s->method->
672 ssl3_enc->client_finished_label,
673 s->method->
674 ssl3_enc->client_finished_label_len);
675
676 default:
677 /* Shouldn't happen */
678 break;
679 }
680
681 return 0;
682}
683
684/*
685 * Returns the maximum allowed length for the current message that we are
686 * reading. Excludes the message header.
687 */
Matt Caswell8481f582015-10-26 11:54:17 +0000688unsigned long ossl_statem_client_max_message_size(SSL *s)
Matt Caswell61ae9352015-09-11 11:23:20 +0100689{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100690 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100691
692 switch(st->hand_state) {
693 case TLS_ST_CR_SRVR_HELLO:
694 return SERVER_HELLO_MAX_LENGTH;
695
696 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
697 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
698
699 case TLS_ST_CR_CERT:
700 return s->max_cert_list;
701
702 case TLS_ST_CR_CERT_STATUS:
703 return SSL3_RT_MAX_PLAIN_LENGTH;
704
705 case TLS_ST_CR_KEY_EXCH:
706 return SERVER_KEY_EXCH_MAX_LENGTH;
707
708 case TLS_ST_CR_CERT_REQ:
709 return SSL3_RT_MAX_PLAIN_LENGTH;
710
711 case TLS_ST_CR_SRVR_DONE:
712 return SERVER_HELLO_DONE_MAX_LENGTH;
713
714 case TLS_ST_CR_CHANGE:
715 return CCS_MAX_LENGTH;
716
717 case TLS_ST_CR_SESSION_TICKET:
718 return SSL3_RT_MAX_PLAIN_LENGTH;
719
720 case TLS_ST_CR_FINISHED:
721 return FINISHED_MAX_LENGTH;
722
723 default:
724 /* Shouldn't happen */
725 break;
726 }
727
728 return 0;
729}
730
731/*
732 * Process a message that the client has been received from the server.
733 */
Matt Caswell8481f582015-10-26 11:54:17 +0000734MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
Matt Caswell61ae9352015-09-11 11:23:20 +0100735{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100736 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100737
738 switch(st->hand_state) {
739 case TLS_ST_CR_SRVR_HELLO:
740 return tls_process_server_hello(s, pkt);
741
742 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
743 return dtls_process_hello_verify(s, pkt);
744
745 case TLS_ST_CR_CERT:
746 return tls_process_server_certificate(s, pkt);
747
748 case TLS_ST_CR_CERT_STATUS:
749 return tls_process_cert_status(s, pkt);
750
751 case TLS_ST_CR_KEY_EXCH:
752 return tls_process_key_exchange(s, pkt);
753
754 case TLS_ST_CR_CERT_REQ:
755 return tls_process_certificate_request(s, pkt);
756
757 case TLS_ST_CR_SRVR_DONE:
758 return tls_process_server_done(s, pkt);
759
760 case TLS_ST_CR_CHANGE:
761 return tls_process_change_cipher_spec(s, pkt);
762
763 case TLS_ST_CR_SESSION_TICKET:
764 return tls_process_new_session_ticket(s, pkt);
765
766 case TLS_ST_CR_FINISHED:
767 return tls_process_finished(s, pkt);
768
769 default:
770 /* Shouldn't happen */
771 break;
772 }
773
774 return MSG_PROCESS_ERROR;
775}
776
777/*
778 * Perform any further processing required following the receipt of a message
779 * from the server
780 */
Matt Caswell8481f582015-10-26 11:54:17 +0000781WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
Matt Caswell61ae9352015-09-11 11:23:20 +0100782{
Matt Caswelld6f1a6e2015-10-05 10:58:52 +0100783 OSSL_STATEM *st = &s->statem;
Matt Caswell61ae9352015-09-11 11:23:20 +0100784
785 switch(st->hand_state) {
786#ifndef OPENSSL_NO_SCTP
787 case TLS_ST_CR_SRVR_DONE:
788 /* We only get here if we are using SCTP and we are renegotiating */
789 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
790 s->s3->in_read_app_data = 2;
791 s->rwstate = SSL_READING;
792 BIO_clear_retry_flags(SSL_get_rbio(s));
793 BIO_set_retry_read(SSL_get_rbio(s));
Matt Caswellfe3a3292015-10-05 10:39:54 +0100794 ossl_statem_set_sctp_read_sock(s, 1);
Matt Caswell61ae9352015-09-11 11:23:20 +0100795 return WORK_MORE_A;
796 }
Matt Caswellfe3a3292015-10-05 10:39:54 +0100797 ossl_statem_set_sctp_read_sock(s, 0);
Matt Caswell61ae9352015-09-11 11:23:20 +0100798 return WORK_FINISHED_STOP;
799#endif
800
Matt Caswell61ae9352015-09-11 11:23:20 +0100801 default:
802 break;
803 }
804
805 /* Shouldn't happen */
806 return WORK_ERROR;
807}
808
Matt Caswelld45ba432015-04-24 15:05:27 +0100809/*
810 * Work out what version we should be using for the initial ClientHello if
811 * the version is currently set to (D)TLS_ANY_VERSION.
812 * Returns 1 on success
813 * Returns 0 on error
814 */
815static int ssl_set_version(SSL *s)
816{
817 unsigned long mask, options = s->options;
818
819 if (s->method->version == TLS_ANY_VERSION) {
820 /*
821 * SSL_OP_NO_X disables all protocols above X *if* there are
822 * some protocols below X enabled. This is required in order
823 * to maintain "version capability" vector contiguous. So
824 * that if application wants to disable TLS1.0 in favour of
825 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
826 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
827 */
828 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
829#if !defined(OPENSSL_NO_SSL3)
830 | SSL_OP_NO_SSLv3
831#endif
832 ;
833#if !defined(OPENSSL_NO_TLS1_2_CLIENT)
834 if (options & SSL_OP_NO_TLSv1_2) {
835 if ((options & mask) != mask) {
836 s->version = TLS1_1_VERSION;
837 } else {
838 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
839 return 0;
840 }
841 } else {
842 s->version = TLS1_2_VERSION;
843 }
844#else
845 if ((options & mask) == mask) {
846 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
847 return 0;
848 }
849 s->version = TLS1_1_VERSION;
850#endif
851
852 mask &= ~SSL_OP_NO_TLSv1_1;
853 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
854 s->version = TLS1_VERSION;
855 mask &= ~SSL_OP_NO_TLSv1;
856#if !defined(OPENSSL_NO_SSL3)
857 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
858 s->version = SSL3_VERSION;
859#endif
860
861 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
862 SSLerr(SSL_F_SSL_SET_VERSION,
863 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
864 return 0;
865 }
866
867 if (s->version == SSL3_VERSION && FIPS_mode()) {
868 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
869 return 0;
870 }
871
872 } else if (s->method->version == DTLS_ANY_VERSION) {
873 /* Determine which DTLS version to use */
874 /* If DTLS 1.2 disabled correct the version number */
875 if (options & SSL_OP_NO_DTLSv1_2) {
876 if (tls1_suiteb(s)) {
877 SSLerr(SSL_F_SSL_SET_VERSION,
878 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
879 return 0;
880 }
881 /*
882 * Disabling all versions is silly: return an error.
883 */
884 if (options & SSL_OP_NO_DTLSv1) {
885 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
886 return 0;
887 }
888 /*
889 * Update method so we don't use any DTLS 1.2 features.
890 */
891 s->method = DTLSv1_client_method();
892 s->version = DTLS1_VERSION;
893 } else {
894 /*
895 * We only support one version: update method
896 */
897 if (options & SSL_OP_NO_DTLSv1)
898 s->method = DTLSv1_2_client_method();
899 s->version = DTLS1_2_VERSION;
900 }
901 }
902
903 s->client_version = s->version;
904
905 return 1;
906}
907
Matt Caswellb9908bf2015-07-29 14:08:49 +0100908int tls_construct_client_hello(SSL *s)
909{
Matt Caswell0f113f32015-01-22 03:40:55 +0000910 unsigned char *buf;
911 unsigned char *p, *d;
912 int i;
913 unsigned long l;
914 int al = 0;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000915#ifndef OPENSSL_NO_COMP
Matt Caswell0f113f32015-01-22 03:40:55 +0000916 int j;
917 SSL_COMP *comp;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000918#endif
Matt Caswellb9908bf2015-07-29 14:08:49 +0100919 SSL_SESSION *sess = s->session;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000920
Matt Caswell0f113f32015-01-22 03:40:55 +0000921 buf = (unsigned char *)s->init_buf->data;
Matt Caswell13c9bb32015-03-31 00:18:31 +0100922
Matt Caswellb9908bf2015-07-29 14:08:49 +0100923 /* Work out what SSL/TLS/DTLS version to use */
924 if (ssl_set_version(s) == 0)
925 goto err;
Matt Caswell13c9bb32015-03-31 00:18:31 +0100926
Matt Caswellb9908bf2015-07-29 14:08:49 +0100927 if ((sess == NULL) || (sess->ssl_version != s->version) ||
Matt Caswell0f113f32015-01-22 03:40:55 +0000928 /*
Matt Caswellb9908bf2015-07-29 14:08:49 +0100929 * In the case of EAP-FAST, we can have a pre-shared
930 * "ticket" without a session ID.
Matt Caswell0f113f32015-01-22 03:40:55 +0000931 */
Matt Caswellb9908bf2015-07-29 14:08:49 +0100932 (!sess->session_id_length && !sess->tlsext_tick) ||
933 (sess->not_resumable)) {
934 if (!ssl_get_new_session(s, 0))
Matt Caswelle1b568d2015-02-26 11:56:00 +0000935 goto err;
Matt Caswellb9908bf2015-07-29 14:08:49 +0100936 }
937 /* else use the pre-loaded session */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000938
Matt Caswellb9908bf2015-07-29 14:08:49 +0100939 p = s->s3->client_random;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000940
Matt Caswellb9908bf2015-07-29 14:08:49 +0100941 /*
942 * for DTLS if client_random is initialized, reuse it, we are
943 * required to use same upon reply to HelloVerify
944 */
945 if (SSL_IS_DTLS(s)) {
946 size_t idx;
947 i = 1;
948 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
949 if (p[idx]) {
950 i = 0;
951 break;
Matt Caswell0f113f32015-01-22 03:40:55 +0000952 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000953 }
Matt Caswellb9908bf2015-07-29 14:08:49 +0100954 } else
955 i = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000956
Matt Caswellb9908bf2015-07-29 14:08:49 +0100957 if (i && ssl_fill_hello_random(s, 0, p,
958 sizeof(s->s3->client_random)) <= 0)
959 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000960
Matt Caswellb9908bf2015-07-29 14:08:49 +0100961 /* Do the message type and length last */
962 d = p = ssl_handshake_start(s);
963
964 /*-
965 * version indicates the negotiated version: for example from
966 * an SSLv2/v3 compatible client hello). The client_version
967 * field is the maximum version we permit and it is also
968 * used in RSA encrypted premaster secrets. Some servers can
969 * choke if we initially report a higher version then
970 * renegotiate to a lower one in the premaster secret. This
971 * didn't happen with TLS 1.0 as most servers supported it
972 * but it can with TLS 1.1 or later if the server only supports
973 * 1.0.
974 *
975 * Possible scenario with previous logic:
976 * 1. Client hello indicates TLS 1.2
977 * 2. Server hello says TLS 1.0
978 * 3. RSA encrypted premaster secret uses 1.2.
979 * 4. Handhaked proceeds using TLS 1.0.
980 * 5. Server sends hello request to renegotiate.
981 * 6. Client hello indicates TLS v1.0 as we now
982 * know that is maximum server supports.
983 * 7. Server chokes on RSA encrypted premaster secret
984 * containing version 1.0.
985 *
986 * For interoperability it should be OK to always use the
987 * maximum version we support in client hello and then rely
988 * on the checking of version to ensure the servers isn't
989 * being inconsistent: for example initially negotiating with
990 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
991 * client_version in client hello and not resetting it to
992 * the negotiated version.
993 */
994 *(p++) = s->client_version >> 8;
995 *(p++) = s->client_version & 0xff;
996
997 /* Random stuff */
998 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
999 p += SSL3_RANDOM_SIZE;
1000
1001 /* Session ID */
1002 if (s->new_session)
1003 i = 0;
1004 else
1005 i = s->session->session_id_length;
1006 *(p++) = i;
1007 if (i != 0) {
1008 if (i > (int)sizeof(s->session->session_id)) {
1009 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00001010 goto err;
1011 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01001012 memcpy(p, s->session->session_id, i);
Matt Caswell0f113f32015-01-22 03:40:55 +00001013 p += i;
Matt Caswell0f113f32015-01-22 03:40:55 +00001014 }
1015
Matt Caswellb9908bf2015-07-29 14:08:49 +01001016 /* cookie stuff for DTLS */
1017 if (SSL_IS_DTLS(s)) {
1018 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
1019 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1020 goto err;
1021 }
1022 *(p++) = s->d1->cookie_len;
1023 memcpy(p, s->d1->cookie, s->d1->cookie_len);
1024 p += s->d1->cookie_len;
1025 }
1026
1027 /* Ciphers supported */
1028 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
1029 if (i == 0) {
1030 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
1031 goto err;
1032 }
1033#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
1034 /*
1035 * Some servers hang if client hello > 256 bytes as hack workaround
1036 * chop number of supported ciphers to keep it well below this if we
1037 * use TLS v1.2
1038 */
1039 if (TLS1_get_version(s) >= TLS1_2_VERSION
1040 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
1041 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
1042#endif
1043 s2n(i, p);
1044 p += i;
1045
1046 /* COMPRESSION */
1047#ifdef OPENSSL_NO_COMP
1048 *(p++) = 1;
1049#else
1050
1051 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
1052 j = 0;
1053 else
1054 j = sk_SSL_COMP_num(s->ctx->comp_methods);
1055 *(p++) = 1 + j;
1056 for (i = 0; i < j; i++) {
1057 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1058 *(p++) = comp->id;
1059 }
1060#endif
1061 *(p++) = 0; /* Add the NULL method */
1062
1063 /* TLS extensions */
1064 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1065 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1066 goto err;
1067 }
1068 if ((p =
1069 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1070 &al)) == NULL) {
1071 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1072 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1073 goto err;
1074 }
1075
1076 l = p - d;
1077 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
1078 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1079 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1080 goto err;
1081 }
1082
1083 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00001084 err:
Matt Caswellfe3a3292015-10-05 10:39:54 +01001085 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01001086 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001087}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001088
Matt Caswellbe3583f2015-10-26 11:46:33 +00001089MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
Matt Caswell8ba708e2015-09-11 10:48:59 +01001090{
1091 int al;
1092 unsigned int cookie_len;
1093 PACKET cookiepkt;
1094
1095 if (!PACKET_forward(pkt, 2)
1096 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1097 al = SSL_AD_DECODE_ERROR;
1098 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1099 goto f_err;
1100 }
1101
1102 cookie_len = PACKET_remaining(&cookiepkt);
1103 if (cookie_len > sizeof(s->d1->cookie)) {
1104 al = SSL_AD_ILLEGAL_PARAMETER;
1105 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1106 goto f_err;
1107 }
1108
1109 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1110 al = SSL_AD_DECODE_ERROR;
1111 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1112 goto f_err;
1113 }
1114 s->d1->cookie_len = cookie_len;
1115
1116 return MSG_PROCESS_FINISHED_READING;
1117 f_err:
1118 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Matt Caswellfe3a3292015-10-05 10:39:54 +01001119 ossl_statem_set_error(s);
Matt Caswell8ba708e2015-09-11 10:48:59 +01001120 return MSG_PROCESS_ERROR;
1121}
1122
Matt Caswellbe3583f2015-10-26 11:46:33 +00001123MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01001124{
1125 STACK_OF(SSL_CIPHER) *sk;
1126 const SSL_CIPHER *c;
Matt Caswell73999b62015-09-10 10:22:30 +01001127 PACKET session_id;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001128 size_t session_id_len;
1129 unsigned char *cipherchars;
1130 int i, al = SSL_AD_INTERNAL_ERROR;
1131 unsigned int compression;
1132#ifndef OPENSSL_NO_COMP
1133 SSL_COMP *comp;
1134#endif
1135
Matt Caswell13c9bb32015-03-31 00:18:31 +01001136 if (s->method->version == TLS_ANY_VERSION) {
Matt Caswell50932c42015-08-04 17:36:02 +01001137 unsigned int sversion;
1138
Matt Caswell73999b62015-09-10 10:22:30 +01001139 if (!PACKET_get_net_2(pkt, &sversion)) {
Matt Caswell50932c42015-08-04 17:36:02 +01001140 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001141 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Matt Caswell50932c42015-08-04 17:36:02 +01001142 goto f_err;
1143 }
Matt Caswell13c9bb32015-03-31 00:18:31 +01001144
1145#if TLS_MAX_VERSION != TLS1_2_VERSION
1146#error Code needs updating for new TLS version
1147#endif
1148#ifndef OPENSSL_NO_SSL3
1149 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1150 if (FIPS_mode()) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001151 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell13c9bb32015-03-31 00:18:31 +01001152 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
Matt Caswelld45ba432015-04-24 15:05:27 +01001153 al = SSL_AD_PROTOCOL_VERSION;
1154 goto f_err;
Matt Caswell13c9bb32015-03-31 00:18:31 +01001155 }
1156 s->method = SSLv3_client_method();
1157 } else
1158#endif
1159 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1160 s->method = TLSv1_client_method();
1161 } else if ((sversion == TLS1_1_VERSION) &&
1162 !(s->options & SSL_OP_NO_TLSv1_1)) {
1163 s->method = TLSv1_1_client_method();
1164 } else if ((sversion == TLS1_2_VERSION) &&
1165 !(s->options & SSL_OP_NO_TLSv1_2)) {
1166 s->method = TLSv1_2_client_method();
1167 } else {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001168 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
Matt Caswelld45ba432015-04-24 15:05:27 +01001169 al = SSL_AD_PROTOCOL_VERSION;
1170 goto f_err;
Matt Caswell13c9bb32015-03-31 00:18:31 +01001171 }
1172 s->session->ssl_version = s->version = s->method->version;
1173
1174 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001175 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
Matt Caswelld45ba432015-04-24 15:05:27 +01001176 al = SSL_AD_PROTOCOL_VERSION;
1177 goto f_err;
Matt Caswell13c9bb32015-03-31 00:18:31 +01001178 }
1179 } else if (s->method->version == DTLS_ANY_VERSION) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001180 /* Work out correct protocol version to use */
Matt Caswell50932c42015-08-04 17:36:02 +01001181 unsigned int hversion;
1182 int options;
1183
Matt Caswell73999b62015-09-10 10:22:30 +01001184 if (!PACKET_get_net_2(pkt, &hversion)) {
Matt Caswell50932c42015-08-04 17:36:02 +01001185 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001186 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Matt Caswell50932c42015-08-04 17:36:02 +01001187 goto f_err;
1188 }
1189
1190 options = s->options;
Matt Caswell0f113f32015-01-22 03:40:55 +00001191 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1192 s->method = DTLSv1_2_client_method();
1193 else if (tls1_suiteb(s)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001194 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell0f113f32015-01-22 03:40:55 +00001195 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1196 s->version = hversion;
1197 al = SSL_AD_PROTOCOL_VERSION;
1198 goto f_err;
1199 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1200 s->method = DTLSv1_client_method();
1201 else {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001202 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
Matt Caswell0f113f32015-01-22 03:40:55 +00001203 s->version = hversion;
1204 al = SSL_AD_PROTOCOL_VERSION;
1205 goto f_err;
1206 }
Matt Caswell7322abf2015-05-06 11:40:06 +01001207 s->session->ssl_version = s->version = s->method->version;
Matt Caswell50932c42015-08-04 17:36:02 +01001208 } else {
1209 unsigned char *vers;
1210
Matt Caswell73999b62015-09-10 10:22:30 +01001211 if (!PACKET_get_bytes(pkt, &vers, 2)) {
Matt Caswell50932c42015-08-04 17:36:02 +01001212 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001213 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Matt Caswell50932c42015-08-04 17:36:02 +01001214 goto f_err;
1215 }
1216 if ((vers[0] != (s->version >> 8))
1217 || (vers[1] != (s->version & 0xff))) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001218 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
Matt Caswell50932c42015-08-04 17:36:02 +01001219 s->version = (s->version & 0xff00) | vers[1];
1220 al = SSL_AD_PROTOCOL_VERSION;
1221 goto f_err;
1222 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001223 }
Emilia Kasper7b3ba5082014-11-19 15:56:27 +01001224
Matt Caswell0f113f32015-01-22 03:40:55 +00001225 /* load the server hello data */
1226 /* load the server random */
Matt Caswell73999b62015-09-10 10:22:30 +01001227 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
Matt Caswell50932c42015-08-04 17:36:02 +01001228 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001229 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Matt Caswell50932c42015-08-04 17:36:02 +01001230 goto f_err;
1231 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001232
Matt Caswell0f113f32015-01-22 03:40:55 +00001233 s->hit = 0;
Ben Laurie54a656e2002-11-13 15:43:43 +00001234
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001235 /* Get the session-id. */
Matt Caswell73999b62015-09-10 10:22:30 +01001236 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001237 al = SSL_AD_DECODE_ERROR;
Matt Caswellf0659bd2015-10-22 14:02:46 +01001238 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001239 goto f_err;
1240 }
1241 session_id_len = PACKET_remaining(&session_id);
1242 if (session_id_len > sizeof s->session->session_id
1243 || session_id_len > SSL3_SESSION_ID_SIZE) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001244 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001245 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
Matt Caswell0f113f32015-01-22 03:40:55 +00001246 goto f_err;
1247 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001248
Matt Caswell73999b62015-09-10 10:22:30 +01001249 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001250 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001251 al = SSL_AD_DECODE_ERROR;
1252 goto f_err;
1253 }
1254
Matt Caswell0f113f32015-01-22 03:40:55 +00001255 /*
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001256 * Check if we can resume the session based on external pre-shared secret.
1257 * EAP-FAST (RFC 4851) supports two types of session resumption.
1258 * Resumption based on server-side state works with session IDs.
1259 * Resumption based on pre-shared Protected Access Credentials (PACs)
1260 * works by overriding the SessionTicket extension at the application
1261 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1262 * servers would honour the session ID.) Therefore, the session ID alone
1263 * is not a reliable indicator of session resumption, so we first check if
1264 * we can resume, and later peek at the next handshake message to see if the
1265 * server wants to resume.
Matt Caswell0f113f32015-01-22 03:40:55 +00001266 */
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001267 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1268 s->session->tlsext_tick) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001269 SSL_CIPHER *pref_cipher = NULL;
1270 s->session->master_key_length = sizeof(s->session->master_key);
1271 if (s->tls_session_secret_cb(s, s->session->master_key,
1272 &s->session->master_key_length,
1273 NULL, &pref_cipher,
1274 s->tls_session_secret_cb_arg)) {
1275 s->session->cipher = pref_cipher ?
Matt Caswell50932c42015-08-04 17:36:02 +01001276 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001277 } else {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001278 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001279 al = SSL_AD_INTERNAL_ERROR;
1280 goto f_err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001281 }
1282 }
Dr. Stephen Henson12bf56c2008-11-15 17:18:12 +00001283
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001284 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1285 && memcmp(PACKET_data(&session_id), s->session->session_id,
1286 session_id_len) == 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001287 if (s->sid_ctx_length != s->session->sid_ctx_length
1288 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1289 /* actually a client application bug */
1290 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001291 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell0f113f32015-01-22 03:40:55 +00001292 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1293 goto f_err;
1294 }
1295 s->hit = 1;
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001296 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +00001297 /*
Emilia Kasper6e3d0152015-04-21 18:12:58 +02001298 * If we were trying for session-id reuse but the server
1299 * didn't echo the ID, make a new SSL_SESSION.
1300 * In the case of EAP-FAST and PAC, we do not send a session ID,
1301 * so the PAC-based session secret is always preserved. It'll be
1302 * overwritten if the server refuses resumption.
Matt Caswell0f113f32015-01-22 03:40:55 +00001303 */
1304 if (s->session->session_id_length > 0) {
1305 if (!ssl_get_new_session(s, 0)) {
1306 goto f_err;
1307 }
1308 }
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001309
1310 s->session->session_id_length = session_id_len;
1311 /* session_id_len could be 0 */
1312 memcpy(s->session->session_id, PACKET_data(&session_id),
1313 session_id_len);
Matt Caswell0f113f32015-01-22 03:40:55 +00001314 }
Matt Caswell50932c42015-08-04 17:36:02 +01001315
Matt Caswell50932c42015-08-04 17:36:02 +01001316 c = ssl_get_cipher_by_char(s, cipherchars);
Matt Caswell0f113f32015-01-22 03:40:55 +00001317 if (c == NULL) {
1318 /* unknown cipher */
1319 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001320 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
Matt Caswell0f113f32015-01-22 03:40:55 +00001321 goto f_err;
1322 }
1323 /* Set version disabled mask now we know version */
1324 if (!SSL_USE_TLS1_2_CIPHERS(s))
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001325 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
Matt Caswell0f113f32015-01-22 03:40:55 +00001326 else
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001327 s->s3->tmp.mask_ssl = 0;
Dr. Stephen Henson2b573382015-11-13 14:37:24 +00001328 /* Skip TLS v1.0 ciphersuites if SSLv3 */
1329 if ((c->algorithm_ssl & SSL_TLSV1) && s->version == SSL3_VERSION)
1330 s->s3->tmp.mask_ssl |= SSL_TLSV1;
Matt Caswell0f113f32015-01-22 03:40:55 +00001331 /*
1332 * If it is a disabled cipher we didn't send it in client hello, so
1333 * return an error.
1334 */
1335 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1336 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001337 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
Matt Caswell0f113f32015-01-22 03:40:55 +00001338 goto f_err;
1339 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001340
Matt Caswell0f113f32015-01-22 03:40:55 +00001341 sk = ssl_get_ciphers_by_id(s);
1342 i = sk_SSL_CIPHER_find(sk, c);
1343 if (i < 0) {
1344 /* we did not say we would use this cipher */
1345 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001346 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
Matt Caswell0f113f32015-01-22 03:40:55 +00001347 goto f_err;
1348 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001349
Matt Caswell0f113f32015-01-22 03:40:55 +00001350 /*
1351 * Depending on the session caching (internal/external), the cipher
1352 * and/or cipher_id values may not be set. Make sure that cipher_id is
1353 * set and use it for comparison.
1354 */
1355 if (s->session->cipher)
1356 s->session->cipher_id = s->session->cipher->id;
1357 if (s->hit && (s->session->cipher_id != c->id)) {
Rich Salz9e9858d2015-02-06 10:52:12 -05001358 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001359 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Rich Salz9e9858d2015-02-06 10:52:12 -05001360 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1361 goto f_err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001362 }
1363 s->s3->tmp.new_cipher = c;
1364 /*
1365 * Don't digest cached records if no sigalgs: we may need them for client
1366 * authentication.
1367 */
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001368 if (!(SSL_USE_SIGALGS(s)
1369 || (s->s3->tmp.new_cipher->algorithm_auth
1370 & (SSL_aGOST12|SSL_aGOST01)))
1371 && !ssl3_digest_cached_records(s, 0))
Matt Caswell0f113f32015-01-22 03:40:55 +00001372 goto f_err;
1373 /* lets get the compression algorithm */
1374 /* COMPRESSION */
Matt Caswell73999b62015-09-10 10:22:30 +01001375 if (!PACKET_get_1(pkt, &compression)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001376 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
Matt Caswell50932c42015-08-04 17:36:02 +01001377 al = SSL_AD_DECODE_ERROR;
1378 goto f_err;
1379 }
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00001380#ifdef OPENSSL_NO_COMP
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001381 if (compression != 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001382 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001383 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell0f113f32015-01-22 03:40:55 +00001384 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1385 goto f_err;
1386 }
1387 /*
1388 * If compression is disabled we'd better not try to resume a session
1389 * using compression.
1390 */
1391 if (s->session->compress_meth != 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001392 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
Matt Caswell0f113f32015-01-22 03:40:55 +00001393 goto f_err;
1394 }
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00001395#else
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001396 if (s->hit && compression != s->session->compress_meth) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001397 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001398 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell0f113f32015-01-22 03:40:55 +00001399 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1400 goto f_err;
1401 }
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001402 if (compression == 0)
Matt Caswell0f113f32015-01-22 03:40:55 +00001403 comp = NULL;
1404 else if (!ssl_allow_compression(s)) {
1405 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001406 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
Matt Caswell0f113f32015-01-22 03:40:55 +00001407 goto f_err;
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001408 } else {
1409 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1410 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001411
Emilia Kasperfc5ce512015-09-18 14:09:37 +02001412 if (compression != 0 && comp == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001413 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001414 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
Matt Caswell0f113f32015-01-22 03:40:55 +00001415 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1416 goto f_err;
1417 } else {
1418 s->s3->tmp.new_compression = comp;
1419 }
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +00001420#endif
Bodo Möller761772d2007-09-21 06:54:24 +00001421
Matt Caswell0f113f32015-01-22 03:40:55 +00001422 /* TLS extensions */
Matt Caswell73999b62015-09-10 10:22:30 +01001423 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001424 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001425 goto err;
1426 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001427
Matt Caswell73999b62015-09-10 10:22:30 +01001428 if (PACKET_remaining(pkt) != 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001429 /* wrong packet length */
1430 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001431 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001432 goto f_err;
1433 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001434
Matt Caswell87235882015-09-07 16:36:53 +01001435#ifndef OPENSSL_NO_SCTP
1436 if (SSL_IS_DTLS(s) && s->hit) {
1437 unsigned char sctpauthkey[64];
1438 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1439
1440 /*
1441 * Add new shared key for SCTP-Auth, will be ignored if
1442 * no SCTP used.
1443 */
Matt Caswell141eb8c2015-10-26 12:00:00 +00001444 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1445 sizeof(DTLS1_SCTP_AUTH_LABEL));
Matt Caswell87235882015-09-07 16:36:53 +01001446
1447 if (SSL_export_keying_material(s, sctpauthkey,
1448 sizeof(sctpauthkey),
1449 labelbuffer,
1450 sizeof(labelbuffer), NULL, 0,
1451 0) <= 0)
1452 goto err;
1453
1454 BIO_ctrl(SSL_get_wbio(s),
1455 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1456 sizeof(sctpauthkey), sctpauthkey);
1457 }
1458#endif
1459
Matt Caswellb9908bf2015-07-29 14:08:49 +01001460 return MSG_PROCESS_CONTINUE_READING;
Matt Caswell0f113f32015-01-22 03:40:55 +00001461 f_err:
1462 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1463 err:
Matt Caswellfe3a3292015-10-05 10:39:54 +01001464 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01001465 return MSG_PROCESS_ERROR;
Matt Caswell0f113f32015-01-22 03:40:55 +00001466}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001467
Matt Caswellbe3583f2015-10-26 11:46:33 +00001468MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01001469{
1470 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1471 unsigned long cert_list_len, cert_len;
1472 X509 *x = NULL;
1473 unsigned char *certstart, *certbytes;
1474 STACK_OF(X509) *sk = NULL;
1475 EVP_PKEY *pkey = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001476
Matt Caswell0f113f32015-01-22 03:40:55 +00001477 if ((sk = sk_X509_new_null()) == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001478 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
Matt Caswellcc273a92015-04-30 11:32:35 +01001479 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001480 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001481
Matt Caswell73999b62015-09-10 10:22:30 +01001482 if (!PACKET_get_net_3(pkt, &cert_list_len)
1483 || PACKET_remaining(pkt) != cert_list_len) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001484 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001485 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001486 goto f_err;
1487 }
Matt Caswell73999b62015-09-10 10:22:30 +01001488 while (PACKET_remaining(pkt)) {
1489 if (!PACKET_get_net_3(pkt, &cert_len)
1490 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001491 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001492 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001493 SSL_R_CERT_LENGTH_MISMATCH);
1494 goto f_err;
1495 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001496
Matt Caswelldf758a82015-08-04 20:10:06 +01001497 certstart = certbytes;
1498 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
Matt Caswell0f113f32015-01-22 03:40:55 +00001499 if (x == NULL) {
1500 al = SSL_AD_BAD_CERTIFICATE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001501 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001502 goto f_err;
1503 }
Matt Caswelldf758a82015-08-04 20:10:06 +01001504 if (certbytes != (certstart + cert_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001505 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001506 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001507 SSL_R_CERT_LENGTH_MISMATCH);
1508 goto f_err;
1509 }
1510 if (!sk_X509_push(sk, x)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001511 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
Matt Caswellcc273a92015-04-30 11:32:35 +01001512 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001513 }
1514 x = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001515 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001516
Matt Caswell0f113f32015-01-22 03:40:55 +00001517 i = ssl_verify_cert_chain(s, sk);
Matt Caswell55a9a162015-05-12 10:27:53 +01001518 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001519 al = ssl_verify_alarm_type(s->verify_result);
Matt Caswellb9908bf2015-07-29 14:08:49 +01001520 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001521 SSL_R_CERTIFICATE_VERIFY_FAILED);
1522 goto f_err;
1523 }
1524 ERR_clear_error(); /* but we keep s->verify_result */
1525 if (i > 1) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001526 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
Matt Caswell0f113f32015-01-22 03:40:55 +00001527 al = SSL_AD_HANDSHAKE_FAILURE;
1528 goto f_err;
1529 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001530
Dr. Stephen Hensonc34b0f92015-06-21 19:34:33 +01001531 s->session->peer_chain = sk;
Matt Caswell0f113f32015-01-22 03:40:55 +00001532 /*
1533 * Inconsistency alert: cert_chain does include the peer's certificate,
1534 * which we don't include in s3_srvr.c
1535 */
1536 x = sk_X509_value(sk, 0);
1537 sk = NULL;
1538 /*
1539 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1540 */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001541
Matt Caswell0f113f32015-01-22 03:40:55 +00001542 pkey = X509_get_pubkey(x);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001543
Matt Caswell55a9a162015-05-12 10:27:53 +01001544 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001545 x = NULL;
1546 al = SSL3_AL_FATAL;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001547 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001548 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1549 goto f_err;
1550 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001551
Matt Caswell0f113f32015-01-22 03:40:55 +00001552 i = ssl_cert_type(x, pkey);
Matt Caswell55a9a162015-05-12 10:27:53 +01001553 if (i < 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001554 x = NULL;
1555 al = SSL3_AL_FATAL;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001556 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001557 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1558 goto f_err;
1559 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001560
Matt Caswell55a9a162015-05-12 10:27:53 +01001561 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001562 if (exp_idx >= 0 && i != exp_idx
1563 && (exp_idx != SSL_PKEY_GOST_EC ||
1564 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1565 && i != SSL_PKEY_GOST01))) {
Matt Caswell55a9a162015-05-12 10:27:53 +01001566 x = NULL;
1567 al = SSL_AD_ILLEGAL_PARAMETER;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001568 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
Matt Caswell55a9a162015-05-12 10:27:53 +01001569 SSL_R_WRONG_CERTIFICATE_TYPE);
1570 goto f_err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001571 }
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001572 s->session->peer_type = i;
Matt Caswell55a9a162015-05-12 10:27:53 +01001573
1574 X509_free(s->session->peer);
Dr. Stephen Henson05f0fb92015-08-31 20:29:57 +01001575 X509_up_ref(x);
Matt Caswell55a9a162015-05-12 10:27:53 +01001576 s->session->peer = x;
Matt Caswell0f113f32015-01-22 03:40:55 +00001577 s->session->verify_result = s->verify_result;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001578
Matt Caswell0f113f32015-01-22 03:40:55 +00001579 x = NULL;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001580 ret = MSG_PROCESS_CONTINUE_READING;
Rich Salz66696472015-05-01 14:29:48 -04001581 goto done;
1582
Matt Caswell0f113f32015-01-22 03:40:55 +00001583 f_err:
Rich Salz66696472015-05-01 14:29:48 -04001584 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Matt Caswellcc273a92015-04-30 11:32:35 +01001585 err:
Matt Caswellfe3a3292015-10-05 10:39:54 +01001586 ossl_statem_set_error(s);
Rich Salz66696472015-05-01 14:29:48 -04001587 done:
Matt Caswell0f113f32015-01-22 03:40:55 +00001588 EVP_PKEY_free(pkey);
1589 X509_free(x);
1590 sk_X509_pop_free(sk, X509_free);
Matt Caswellb9908bf2015-07-29 14:08:49 +01001591 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +00001592}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001593
Matt Caswellbe3583f2015-10-26 11:46:33 +00001594MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01001595{
Matt Caswell0f113f32015-01-22 03:40:55 +00001596 EVP_MD_CTX md_ctx;
Dr. Stephen Henson192e4bb2015-11-21 03:56:52 +00001597 int al, j;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001598 long alg_k, alg_a;
Matt Caswell0f113f32015-01-22 03:40:55 +00001599 EVP_PKEY *pkey = NULL;
1600 const EVP_MD *md = NULL;
Richard Levittebc36ee62001-02-20 08:13:47 +00001601#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +00001602 RSA *rsa = NULL;
Ulf Möller79df9d61999-04-27 03:19:12 +00001603#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00001604#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +00001605 DH *dh = NULL;
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001606#endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04001607#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00001608 EC_KEY *ecdh = NULL;
1609 BN_CTX *bn_ctx = NULL;
1610 EC_POINT *srvr_ecpoint = NULL;
1611 int curve_nid = 0;
Bodo Möllerea262262002-08-09 08:56:08 +00001612#endif
Matt Caswell73999b62015-09-10 10:22:30 +01001613 PACKET save_param_start, signature;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001614
Matt Caswell0f113f32015-01-22 03:40:55 +00001615 EVP_MD_CTX_init(&md_ctx);
Dr. Stephen Hensonb15f8762014-10-24 12:30:33 +01001616
Matt Caswell0f113f32015-01-22 03:40:55 +00001617 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
Dr. Stephen Hensonb15f8762014-10-24 12:30:33 +01001618
Matt Caswell73999b62015-09-10 10:22:30 +01001619 save_param_start = *pkt;
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001620
Richard Levittebc36ee62001-02-20 08:13:47 +00001621#ifndef OPENSSL_NO_RSA
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001622 RSA_free(s->s3->peer_rsa_tmp);
1623 s->s3->peer_rsa_tmp = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001624#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00001625#ifndef OPENSSL_NO_DH
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001626 DH_free(s->s3->peer_dh_tmp);
1627 s->s3->peer_dh_tmp = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001628#endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04001629#ifndef OPENSSL_NO_EC
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001630 EC_KEY_free(s->s3->peer_ecdh_tmp);
1631 s->s3->peer_ecdh_tmp = NULL;
Bodo Möllerea262262002-08-09 08:56:08 +00001632#endif
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001633
Matt Caswell0f113f32015-01-22 03:40:55 +00001634 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001635
Matt Caswell0f113f32015-01-22 03:40:55 +00001636 al = SSL_AD_DECODE_ERROR;
Matt Caswellf2be92b2014-07-26 23:47:40 +01001637
Nils Larschddac1972006-03-10 23:06:27 +00001638#ifndef OPENSSL_NO_PSK
Dr. Stephen Henson76890822015-06-28 17:15:10 +01001639 /* PSK ciphersuites are preceded by an identity hint */
1640 if (alg_k & SSL_PSK) {
Emilia Kasper32942872015-10-02 14:40:30 +02001641 PACKET psk_identity_hint;
Matt Caswell73999b62015-09-10 10:22:30 +01001642 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001643 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001644 goto f_err;
1645 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001646
Matt Caswell0f113f32015-01-22 03:40:55 +00001647 /*
1648 * Store PSK identity hint for later use, hint is used in
1649 * ssl3_send_client_key_exchange. Assume that the maximum length of
1650 * a PSK identity hint can be as long as the maximum length of a PSK
1651 * identity.
1652 */
Emilia Kasper32942872015-10-02 14:40:30 +02001653 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001654 al = SSL_AD_HANDSHAKE_FAILURE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001655 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
Matt Caswell0f113f32015-01-22 03:40:55 +00001656 goto f_err;
1657 }
Emilia Kasper32942872015-10-02 14:40:30 +02001658
Dr. Stephen Henson558ceae2015-11-13 16:00:29 +00001659 if (PACKET_remaining(&psk_identity_hint) == 0) {
1660 OPENSSL_free(s->session->psk_identity_hint);
1661 s->session->psk_identity_hint = NULL;
1662 } else if (!PACKET_strndup(&psk_identity_hint,
Emilia Kasper32942872015-10-02 14:40:30 +02001663 &s->session->psk_identity_hint)) {
1664 al = SSL_AD_INTERNAL_ERROR;
Matt Caswell0f113f32015-01-22 03:40:55 +00001665 goto f_err;
1666 }
Dr. Stephen Henson76890822015-06-28 17:15:10 +01001667 }
1668
1669 /* Nothing else to do for plain PSK or RSAPSK */
1670 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001671 } else
1672#endif /* !OPENSSL_NO_PSK */
Ben Laurieedc032b2011-03-12 17:01:19 +00001673#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00001674 if (alg_k & SSL_kSRP) {
Emilia Kasper32942872015-10-02 14:40:30 +02001675 PACKET prime, generator, salt, server_pub;
Matt Caswell73999b62015-09-10 10:22:30 +01001676 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1677 || !PACKET_get_length_prefixed_2(pkt, &generator)
1678 || !PACKET_get_length_prefixed_1(pkt, &salt)
1679 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001680 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001681 goto f_err;
1682 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001683
Emilia Kasper32942872015-10-02 14:40:30 +02001684 if ((s->srp_ctx.N =
1685 BN_bin2bn(PACKET_data(&prime),
1686 PACKET_remaining(&prime), NULL)) == NULL
1687 || (s->srp_ctx.g =
1688 BN_bin2bn(PACKET_data(&generator),
1689 PACKET_remaining(&generator), NULL)) == NULL
1690 || (s->srp_ctx.s =
1691 BN_bin2bn(PACKET_data(&salt),
1692 PACKET_remaining(&salt), NULL)) == NULL
1693 || (s->srp_ctx.B =
1694 BN_bin2bn(PACKET_data(&server_pub),
1695 PACKET_remaining(&server_pub), NULL)) == NULL) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001696 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001697 goto err;
1698 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001699
Matt Caswell0f113f32015-01-22 03:40:55 +00001700 if (!srp_verify_server_param(s, &al)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001701 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
Matt Caswell0f113f32015-01-22 03:40:55 +00001702 goto f_err;
1703 }
Dr. Stephen Henson09897902014-08-03 21:25:22 +01001704
Ben Laurieedc032b2011-03-12 17:01:19 +00001705/* We must check if there is a certificate */
Dr. Stephen Henson8df53b72015-06-21 19:18:50 +01001706 if (alg_a & (SSL_aRSA|SSL_aDSS))
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001707 pkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00001708 } else
1709#endif /* !OPENSSL_NO_SRP */
Ben Laurieedc032b2011-03-12 17:01:19 +00001710#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +00001711 if (alg_k & SSL_kRSA) {
Emilia Kasper32942872015-10-02 14:40:30 +02001712 PACKET mod, exp;
Matt Caswell0f113f32015-01-22 03:40:55 +00001713 /* Temporary RSA keys only allowed in export ciphersuites */
1714 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1715 al = SSL_AD_UNEXPECTED_MESSAGE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001716 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001717 goto f_err;
1718 }
Emilia Kasper32942872015-10-02 14:40:30 +02001719
Matt Caswell73999b62015-09-10 10:22:30 +01001720 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1721 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001722 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Emilia Kasper32942872015-10-02 14:40:30 +02001723 goto f_err;
1724 }
1725
Matt Caswell0f113f32015-01-22 03:40:55 +00001726 if ((rsa = RSA_new()) == NULL) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001727 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001728 goto err;
1729 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001730
Emilia Kasper32942872015-10-02 14:40:30 +02001731 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1732 rsa->n)) == NULL
1733 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1734 rsa->e)) == NULL) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001735 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001736 goto err;
1737 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001738
Matt Caswell0f113f32015-01-22 03:40:55 +00001739 /* this should be because we are using an export cipher */
1740 if (alg_a & SSL_aRSA)
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001741 pkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00001742 else {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001743 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00001744 goto err;
1745 }
Kurt Roeckx1dece952015-04-18 19:15:48 +02001746
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001747 s->s3->peer_rsa_tmp = rsa;
Matt Caswell0f113f32015-01-22 03:40:55 +00001748 rsa = NULL;
1749 }
1750#else /* OPENSSL_NO_RSA */
1751 if (0) ;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001752#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00001753#ifndef OPENSSL_NO_DH
Dr. Stephen Henson76890822015-06-28 17:15:10 +01001754 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
Emilia Kasper32942872015-10-02 14:40:30 +02001755 PACKET prime, generator, pub_key;
1756
Matt Caswell73999b62015-09-10 10:22:30 +01001757 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1758 || !PACKET_get_length_prefixed_2(pkt, &generator)
1759 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001760 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Emilia Kasper32942872015-10-02 14:40:30 +02001761 goto f_err;
1762 }
1763
Matt Caswell0f113f32015-01-22 03:40:55 +00001764 if ((dh = DH_new()) == NULL) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001765 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_DH_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001766 goto err;
1767 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001768
Emilia Kasper32942872015-10-02 14:40:30 +02001769 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1770 PACKET_remaining(&prime), NULL)) == NULL
1771 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1772 PACKET_remaining(&generator), NULL)) == NULL
1773 || (dh->pub_key =
1774 BN_bin2bn(PACKET_data(&pub_key),
1775 PACKET_remaining(&pub_key), NULL)) == NULL) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001776 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001777 goto err;
1778 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001779
Emilia Kasper32942872015-10-02 14:40:30 +02001780 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001781 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
Guy Leaver (guleaver)61e72d72015-08-07 15:45:21 +01001782 goto f_err;
1783 }
1784
Matt Caswell0f113f32015-01-22 03:40:55 +00001785 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1786 al = SSL_AD_HANDSHAKE_FAILURE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001787 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
Matt Caswell0f113f32015-01-22 03:40:55 +00001788 goto f_err;
1789 }
Dr. Stephen Henson8df53b72015-06-21 19:18:50 +01001790 if (alg_a & (SSL_aRSA|SSL_aDSS))
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001791 pkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00001792 /* else anonymous DH, so no certificate or pkey. */
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001793
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001794 s->s3->peer_dh_tmp = dh;
Matt Caswell0f113f32015-01-22 03:40:55 +00001795 dh = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001796 }
1797#endif /* !OPENSSL_NO_DH */
Bodo Möllerea262262002-08-09 08:56:08 +00001798
Rich Salz10bf4fc2015-03-10 19:09:27 -04001799#ifndef OPENSSL_NO_EC
Dr. Stephen Henson76890822015-06-28 17:15:10 +01001800 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001801 EC_GROUP *ngroup;
1802 const EC_GROUP *group;
Emilia Kasper32942872015-10-02 14:40:30 +02001803 PACKET encoded_pt;
1804 unsigned char *ecparams;
Nils Larsch9dd84052005-05-16 10:11:04 +00001805
Matt Caswell0f113f32015-01-22 03:40:55 +00001806 if ((ecdh = EC_KEY_new()) == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001807 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001808 goto err;
1809 }
Bodo Möllerea262262002-08-09 08:56:08 +00001810
Matt Caswell0f113f32015-01-22 03:40:55 +00001811 /*
1812 * Extract elliptic curve parameters and the server's ephemeral ECDH
Emilia Kasper32942872015-10-02 14:40:30 +02001813 * public key. For now we only support named (not generic) curves and
Matt Caswell8098fc52015-08-04 21:22:31 +01001814 * ECParameters in this case is just three bytes.
Matt Caswell0f113f32015-01-22 03:40:55 +00001815 */
Matt Caswell73999b62015-09-10 10:22:30 +01001816 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001817 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001818 goto f_err;
1819 }
1820 /*
1821 * Check curve is one of our preferences, if not server has sent an
1822 * invalid curve. ECParameters is 3 bytes.
1823 */
Emilia Kasper32942872015-10-02 14:40:30 +02001824 if (!tls1_check_curve(s, ecparams, 3)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001825 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001826 goto f_err;
1827 }
Dr. Stephen Hensond18b7162012-07-24 13:47:40 +00001828
Emilia Kasper32942872015-10-02 14:40:30 +02001829 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001830 al = SSL_AD_INTERNAL_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001831 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001832 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1833 goto f_err;
1834 }
Bodo Möllerea262262002-08-09 08:56:08 +00001835
Matt Caswell0f113f32015-01-22 03:40:55 +00001836 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1837 if (ngroup == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001838 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001839 goto err;
1840 }
1841 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001842 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00001843 goto err;
1844 }
1845 EC_GROUP_free(ngroup);
Nils Larsch9dd84052005-05-16 10:11:04 +00001846
Matt Caswell0f113f32015-01-22 03:40:55 +00001847 group = EC_KEY_get0_group(ecdh);
Bodo Möllerea262262002-08-09 08:56:08 +00001848
Matt Caswell0f113f32015-01-22 03:40:55 +00001849 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1850 (EC_GROUP_get_degree(group) > 163)) {
1851 al = SSL_AD_EXPORT_RESTRICTION;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001852 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
Matt Caswell0f113f32015-01-22 03:40:55 +00001853 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1854 goto f_err;
1855 }
Bodo Möllerea262262002-08-09 08:56:08 +00001856
Matt Caswell0f113f32015-01-22 03:40:55 +00001857 /* Next, get the encoded ECPoint */
1858 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1859 ((bn_ctx = BN_CTX_new()) == NULL)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001860 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001861 goto err;
1862 }
Bodo Möllerea262262002-08-09 08:56:08 +00001863
Matt Caswell73999b62015-09-10 10:22:30 +01001864 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001865 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Matt Caswell8098fc52015-08-04 21:22:31 +01001866 goto f_err;
1867 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001868
Emilia Kasper32942872015-10-02 14:40:30 +02001869 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1870 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001871 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001872 goto f_err;
1873 }
Bodo Möllerea262262002-08-09 08:56:08 +00001874
Matt Caswell0f113f32015-01-22 03:40:55 +00001875 /*
1876 * The ECC/TLS specification does not mention the use of DSA to sign
1877 * ECParameters in the server key exchange message. We do support RSA
1878 * and ECDSA.
1879 */
1880 if (0) ;
1881# ifndef OPENSSL_NO_RSA
1882 else if (alg_a & SSL_aRSA)
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001883 pkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00001884# endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04001885# ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00001886 else if (alg_a & SSL_aECDSA)
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01001887 pkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00001888# endif
1889 /* else anonymous ECDH, so no certificate or pkey. */
1890 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01001891 s->s3->peer_ecdh_tmp = ecdh;
Matt Caswell0f113f32015-01-22 03:40:55 +00001892 ecdh = NULL;
1893 BN_CTX_free(bn_ctx);
1894 bn_ctx = NULL;
1895 EC_POINT_free(srvr_ecpoint);
1896 srvr_ecpoint = NULL;
1897 } else if (alg_k) {
1898 al = SSL_AD_UNEXPECTED_MESSAGE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01001899 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001900 goto f_err;
1901 }
Rich Salz10bf4fc2015-03-10 19:09:27 -04001902#endif /* !OPENSSL_NO_EC */
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00001903
Matt Caswell0f113f32015-01-22 03:40:55 +00001904 /* if it was signed, check the signature */
1905 if (pkey != NULL) {
Emilia Kasper32942872015-10-02 14:40:30 +02001906 PACKET params;
1907 /*
1908 * |pkt| now points to the beginning of the signature, so the difference
1909 * equals the length of the parameters.
1910 */
1911 if (!PACKET_get_sub_packet(&save_param_start, &params,
1912 PACKET_remaining(&save_param_start) -
Matt Caswell73999b62015-09-10 10:22:30 +01001913 PACKET_remaining(pkt))) {
Emilia Kasper32942872015-10-02 14:40:30 +02001914 al = SSL_AD_INTERNAL_ERROR;
Matt Caswellf0659bd2015-10-22 14:02:46 +01001915 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
Emilia Kasper32942872015-10-02 14:40:30 +02001916 goto f_err;
1917 }
1918
Matt Caswell0f113f32015-01-22 03:40:55 +00001919 if (SSL_USE_SIGALGS(s)) {
Emilia Kasper32942872015-10-02 14:40:30 +02001920 unsigned char *sigalgs;
Matt Caswell0f113f32015-01-22 03:40:55 +00001921 int rv;
Matt Caswell73999b62015-09-10 10:22:30 +01001922 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001923 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
Matt Caswell0f113f32015-01-22 03:40:55 +00001924 goto f_err;
1925 }
Emilia Kasper32942872015-10-02 14:40:30 +02001926 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
Matt Caswell0f113f32015-01-22 03:40:55 +00001927 if (rv == -1)
1928 goto err;
1929 else if (rv == 0) {
1930 goto f_err;
1931 }
Dr. Stephen Hensona2f92002011-05-09 15:44:01 +00001932#ifdef SSL_DEBUG
Matt Caswell0f113f32015-01-22 03:40:55 +00001933 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
Dr. Stephen Hensona2f92002011-05-09 15:44:01 +00001934#endif
Dr. Stephen Henson192e4bb2015-11-21 03:56:52 +00001935 } else if (pkey->type == EVP_PKEY_RSA) {
1936 md = EVP_md5_sha1();
Emilia Kasper32942872015-10-02 14:40:30 +02001937 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +00001938 md = EVP_sha1();
Emilia Kasper32942872015-10-02 14:40:30 +02001939 }
Matt Caswellf2be92b2014-07-26 23:47:40 +01001940
Matt Caswell73999b62015-09-10 10:22:30 +01001941 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1942 || PACKET_remaining(pkt) != 0) {
Matt Caswellf0659bd2015-10-22 14:02:46 +01001943 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001944 goto f_err;
1945 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001946 j = EVP_PKEY_size(pkey);
Matt Caswell8098fc52015-08-04 21:22:31 +01001947 if (j < 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001948 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
Matt Caswell8098fc52015-08-04 21:22:31 +01001949 goto f_err;
1950 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00001951
Matt Caswell0f113f32015-01-22 03:40:55 +00001952 /*
Matt Caswell8098fc52015-08-04 21:22:31 +01001953 * Check signature length
Matt Caswell0f113f32015-01-22 03:40:55 +00001954 */
Emilia Kasper32942872015-10-02 14:40:30 +02001955 if (PACKET_remaining(&signature) > (size_t)j) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001956 /* wrong packet length */
Matt Caswellb9908bf2015-07-29 14:08:49 +01001957 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
Matt Caswell0f113f32015-01-22 03:40:55 +00001958 goto f_err;
1959 }
Dr. Stephen Henson192e4bb2015-11-21 03:56:52 +00001960 if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
1961 || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1962 SSL3_RANDOM_SIZE) <= 0
1963 || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1964 SSL3_RANDOM_SIZE) <= 0
1965 || EVP_VerifyUpdate(&md_ctx, PACKET_data(&params),
1966 PACKET_remaining(&params)) <= 0) {
1967 al = SSL_AD_INTERNAL_ERROR;
1968 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1969 goto f_err;
1970 }
1971 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1972 PACKET_remaining(&signature), pkey) <= 0) {
1973 /* bad signature */
1974 al = SSL_AD_DECRYPT_ERROR;
1975 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1976 goto f_err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001977 }
1978 } else {
Dr. Stephen Henson76890822015-06-28 17:15:10 +01001979 /* aNULL, aSRP or PSK do not need public keys */
1980 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001981 /* Might be wrong key type, check it */
1982 if (ssl3_check_cert_and_algorithm(s))
1983 /* Otherwise this shouldn't happen */
Matt Caswellb9908bf2015-07-29 14:08:49 +01001984 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00001985 goto err;
1986 }
1987 /* still data left over */
Matt Caswell73999b62015-09-10 10:22:30 +01001988 if (PACKET_remaining(pkt) != 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01001989 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
Matt Caswell0f113f32015-01-22 03:40:55 +00001990 goto f_err;
1991 }
1992 }
1993 EVP_PKEY_free(pkey);
1994 EVP_MD_CTX_cleanup(&md_ctx);
Matt Caswellb9908bf2015-07-29 14:08:49 +01001995 return MSG_PROCESS_CONTINUE_READING;
Matt Caswell0f113f32015-01-22 03:40:55 +00001996 f_err:
1997 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1998 err:
1999 EVP_PKEY_free(pkey);
Richard Levittebc36ee62001-02-20 08:13:47 +00002000#ifndef OPENSSL_NO_RSA
Rich Salzd6407082015-03-24 10:17:37 -04002001 RSA_free(rsa);
Bodo Möller6b521df1999-07-12 15:20:08 +00002002#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00002003#ifndef OPENSSL_NO_DH
Rich Salzd6407082015-03-24 10:17:37 -04002004 DH_free(dh);
Bodo Möller6b521df1999-07-12 15:20:08 +00002005#endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04002006#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00002007 BN_CTX_free(bn_ctx);
2008 EC_POINT_free(srvr_ecpoint);
Rich Salz8fdc3732015-03-25 18:35:24 -04002009 EC_KEY_free(ecdh);
Bodo Möllerea262262002-08-09 08:56:08 +00002010#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002011 EVP_MD_CTX_cleanup(&md_ctx);
Matt Caswellfe3a3292015-10-05 10:39:54 +01002012 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002013 return MSG_PROCESS_ERROR;
Matt Caswell0f113f32015-01-22 03:40:55 +00002014}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002015
Matt Caswellbe3583f2015-10-26 11:46:33 +00002016MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002017{
2018 int ret = MSG_PROCESS_ERROR;
2019 unsigned int list_len, ctype_num, i, name_len;
2020 X509_NAME *xn = NULL;
2021 unsigned char *data;
2022 unsigned char *namestart, *namebytes;
2023 STACK_OF(X509_NAME) *ca_sk = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00002024
2025 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002026 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002027 goto err;
2028 }
2029
2030 /* get the certificate types */
Matt Caswell73999b62015-09-10 10:22:30 +01002031 if (!PACKET_get_1(pkt, &ctype_num)
2032 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
Matt Caswellac112332015-08-04 22:12:53 +01002033 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002034 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
Matt Caswellac112332015-08-04 22:12:53 +01002035 goto err;
2036 }
Rich Salzb548a1f2015-05-01 10:02:07 -04002037 OPENSSL_free(s->cert->ctypes);
2038 s->cert->ctypes = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00002039 if (ctype_num > SSL3_CT_NUMBER) {
2040 /* If we exceed static buffer copy all to cert structure */
2041 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2042 if (s->cert->ctypes == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002043 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002044 goto err;
2045 }
Matt Caswellac112332015-08-04 22:12:53 +01002046 memcpy(s->cert->ctypes, data, ctype_num);
Matt Caswell0f113f32015-01-22 03:40:55 +00002047 s->cert->ctype_num = (size_t)ctype_num;
2048 ctype_num = SSL3_CT_NUMBER;
2049 }
2050 for (i = 0; i < ctype_num; i++)
Matt Caswellac112332015-08-04 22:12:53 +01002051 s->s3->tmp.ctype[i] = data[i];
2052
Matt Caswell0f113f32015-01-22 03:40:55 +00002053 if (SSL_USE_SIGALGS(s)) {
Matt Caswell73999b62015-09-10 10:22:30 +01002054 if (!PACKET_get_net_2(pkt, &list_len)
2055 || !PACKET_get_bytes(pkt, &data, list_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002056 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002057 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2058 SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00002059 goto err;
2060 }
Matt Caswellac112332015-08-04 22:12:53 +01002061
Matt Caswell0f113f32015-01-22 03:40:55 +00002062 /* Clear certificate digests and validity flags */
2063 for (i = 0; i < SSL_PKEY_NUM; i++) {
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01002064 s->s3->tmp.md[i] = NULL;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01002065 s->s3->tmp.valid_flags[i] = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00002066 }
Matt Caswellac112332015-08-04 22:12:53 +01002067 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002068 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002069 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
Matt Caswell0f113f32015-01-22 03:40:55 +00002070 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2071 goto err;
2072 }
2073 if (!tls1_process_sigalgs(s)) {
2074 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002075 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002076 goto err;
2077 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002078 }
2079
2080 /* get the CA RDNs */
Matt Caswell73999b62015-09-10 10:22:30 +01002081 if (!PACKET_get_net_2(pkt, &list_len)
2082 || PACKET_remaining(pkt) != list_len) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002083 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002084 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00002085 goto err;
2086 }
2087
Matt Caswell73999b62015-09-10 10:22:30 +01002088 while (PACKET_remaining(pkt)) {
2089 if (!PACKET_get_net_2(pkt, &name_len)
2090 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002092 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2093 SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00002094 goto err;
2095 }
2096
Matt Caswellac112332015-08-04 22:12:53 +01002097 namestart = namebytes;
Matt Caswell0f113f32015-01-22 03:40:55 +00002098
Matt Caswellac112332015-08-04 22:12:53 +01002099 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2100 name_len)) == NULL) {
Matt Caswell3c33c6f2015-02-05 15:57:54 +00002101 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002102 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
Matt Caswell3c33c6f2015-02-05 15:57:54 +00002103 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00002104 }
2105
Matt Caswellac112332015-08-04 22:12:53 +01002106 if (namebytes != (namestart + name_len)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002108 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
Matt Caswell0f113f32015-01-22 03:40:55 +00002109 SSL_R_CA_DN_LENGTH_MISMATCH);
2110 goto err;
2111 }
2112 if (!sk_X509_NAME_push(ca_sk, xn)) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002113 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002114 goto err;
2115 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002116 }
2117
Matt Caswell0f113f32015-01-22 03:40:55 +00002118 /* we should setup a certificate to return.... */
2119 s->s3->tmp.cert_req = 1;
2120 s->s3->tmp.ctype_num = ctype_num;
Rich Salz222561f2015-04-30 17:33:59 -04002121 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
Matt Caswell0f113f32015-01-22 03:40:55 +00002122 s->s3->tmp.ca_names = ca_sk;
2123 ca_sk = NULL;
2124
Matt Caswellb9908bf2015-07-29 14:08:49 +01002125 ret = MSG_PROCESS_CONTINUE_READING;
Matt Caswellcc273a92015-04-30 11:32:35 +01002126 goto done;
Matt Caswell0f113f32015-01-22 03:40:55 +00002127 err:
Matt Caswellfe3a3292015-10-05 10:39:54 +01002128 ossl_statem_set_error(s);
Matt Caswellcc273a92015-04-30 11:32:35 +01002129 done:
Rich Salz222561f2015-04-30 17:33:59 -04002130 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002131 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +00002132}
2133
2134static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2135{
2136 return (X509_NAME_cmp(*a, *b));
2137}
2138
Matt Caswellbe3583f2015-10-26 11:46:33 +00002139MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002140{
2141 int al;
2142 unsigned int ticklen;
2143 unsigned long ticket_lifetime_hint;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002144
Matt Caswell73999b62015-09-10 10:22:30 +01002145 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2146 || !PACKET_get_net_2(pkt, &ticklen)
2147 || PACKET_remaining(pkt) != ticklen) {
Emilia Kaspere711da72015-09-10 16:32:51 +02002148 al = SSL_AD_DECODE_ERROR;
Matt Caswellf0659bd2015-10-22 14:02:46 +01002149 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
Emilia Kaspere711da72015-09-10 16:32:51 +02002150 goto f_err;
2151 }
2152
2153 /* Server is allowed to change its mind and send an empty ticket. */
2154 if (ticklen == 0)
Matt Caswellc9de4a22015-10-22 15:02:14 +01002155 return MSG_PROCESS_CONTINUE_READING;
Emilia Kaspere711da72015-09-10 16:32:51 +02002156
Matt Caswell98ece4e2015-05-18 16:27:48 +01002157 if (s->session->session_id_length > 0) {
2158 int i = s->session_ctx->session_cache_mode;
2159 SSL_SESSION *new_sess;
2160 /*
2161 * We reused an existing session, so we need to replace it with a new
2162 * one
2163 */
2164 if (i & SSL_SESS_CACHE_CLIENT) {
2165 /*
2166 * Remove the old session from the cache
2167 */
2168 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2169 if (s->session_ctx->remove_session_cb != NULL)
2170 s->session_ctx->remove_session_cb(s->session_ctx,
2171 s->session);
2172 } else {
2173 /* We carry on if this fails */
2174 SSL_CTX_remove_session(s->session_ctx, s->session);
2175 }
2176 }
2177
2178 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2179 al = SSL_AD_INTERNAL_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002180 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
Matt Caswell98ece4e2015-05-18 16:27:48 +01002181 goto f_err;
2182 }
2183
2184 SSL_SESSION_free(s->session);
2185 s->session = new_sess;
2186 }
2187
Rich Salzb548a1f2015-05-01 10:02:07 -04002188 OPENSSL_free(s->session->tlsext_tick);
2189 s->session->tlsext_ticklen = 0;
Emilia Kaspere711da72015-09-10 16:32:51 +02002190
Matt Caswell0f113f32015-01-22 03:40:55 +00002191 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
Matt Caswella71edf32015-10-30 10:05:53 +00002192 if (s->session->tlsext_tick == NULL) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002193 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002194 goto err;
2195 }
Matt Caswell73999b62015-09-10 10:22:30 +01002196 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
Matt Caswell561e12b2015-08-05 14:50:24 +01002197 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002198 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
Matt Caswell561e12b2015-08-05 14:50:24 +01002199 goto f_err;
2200 }
Emilia Kaspere711da72015-09-10 16:32:51 +02002201
2202 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
Matt Caswell0f113f32015-01-22 03:40:55 +00002203 s->session->tlsext_ticklen = ticklen;
2204 /*
2205 * There are two ways to detect a resumed ticket session. One is to set
2206 * an appropriate session ID and then the server must return a match in
2207 * ServerHello. This allows the normal client session ID matching to work
2208 * and we know much earlier that the ticket has been accepted. The
2209 * other way is to set zero length session ID when the ticket is
2210 * presented and rely on the handshake to determine session resumption.
2211 * We choose the former approach because this fits in with assumptions
2212 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2213 * SHA256 is disabled) hash of the ticket.
2214 */
Matt Caswell561e12b2015-08-05 14:50:24 +01002215 EVP_Digest(s->session->tlsext_tick, ticklen,
Matt Caswell0f113f32015-01-22 03:40:55 +00002216 s->session->session_id, &s->session->session_id_length,
Matt Caswell0f113f32015-01-22 03:40:55 +00002217 EVP_sha256(), NULL);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002218 return MSG_PROCESS_CONTINUE_READING;
Matt Caswell0f113f32015-01-22 03:40:55 +00002219 f_err:
2220 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2221 err:
Matt Caswellfe3a3292015-10-05 10:39:54 +01002222 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002223 return MSG_PROCESS_ERROR;
Matt Caswell0f113f32015-01-22 03:40:55 +00002224}
2225
Matt Caswellbe3583f2015-10-26 11:46:33 +00002226MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002227{
2228 int al;
2229 unsigned long resplen;
2230 unsigned int type;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002231
Matt Caswell73999b62015-09-10 10:22:30 +01002232 if (!PACKET_get_1(pkt, &type)
Matt Caswellac637102015-08-05 15:52:26 +01002233 || type != TLSEXT_STATUSTYPE_ocsp) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002234 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002235 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002236 goto f_err;
2237 }
Matt Caswell73999b62015-09-10 10:22:30 +01002238 if (!PACKET_get_net_3(pkt, &resplen)
2239 || PACKET_remaining(pkt) != resplen) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002240 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002241 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
Matt Caswell0f113f32015-01-22 03:40:55 +00002242 goto f_err;
2243 }
Rich Salzb548a1f2015-05-01 10:02:07 -04002244 OPENSSL_free(s->tlsext_ocsp_resp);
Matt Caswellac637102015-08-05 15:52:26 +01002245 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
Matt Caswella71edf32015-10-30 10:05:53 +00002246 if (s->tlsext_ocsp_resp == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002247 al = SSL_AD_INTERNAL_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002248 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002249 goto f_err;
2250 }
Matt Caswell73999b62015-09-10 10:22:30 +01002251 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
Matt Caswellac637102015-08-05 15:52:26 +01002252 al = SSL_AD_DECODE_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002253 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
Matt Caswellac637102015-08-05 15:52:26 +01002254 goto f_err;
2255 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002256 s->tlsext_ocsp_resplen = resplen;
2257 if (s->ctx->tlsext_status_cb) {
2258 int ret;
2259 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2260 if (ret == 0) {
2261 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002262 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002263 goto f_err;
2264 }
2265 if (ret < 0) {
2266 al = SSL_AD_INTERNAL_ERROR;
Matt Caswellb9908bf2015-07-29 14:08:49 +01002267 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002268 goto f_err;
2269 }
2270 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002271 return MSG_PROCESS_CONTINUE_READING;
Matt Caswell0f113f32015-01-22 03:40:55 +00002272 f_err:
2273 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Matt Caswellfe3a3292015-10-05 10:39:54 +01002274 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002275 return MSG_PROCESS_ERROR;
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002276}
Ralf S. Engelschalldfeab061998-12-21 11:00:56 +00002277
Matt Caswellbe3583f2015-10-26 11:46:33 +00002278MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002279{
Matt Caswell73999b62015-09-10 10:22:30 +01002280 if (PACKET_remaining(pkt) > 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002281 /* should contain no data */
2282 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002283 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
Matt Caswellfe3a3292015-10-05 10:39:54 +01002284 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002285 return MSG_PROCESS_ERROR;
Matt Caswell0f113f32015-01-22 03:40:55 +00002286 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002287
2288#ifndef OPENSSL_NO_SRP
2289 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2290 if (SRP_Calc_A_param(s) <= 0) {
2291 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2292 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellfe3a3292015-10-05 10:39:54 +01002293 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002294 return MSG_PROCESS_ERROR;
2295 }
2296 }
2297#endif
2298
Matt Caswella455d0f2015-09-14 15:06:37 +01002299 /*
2300 * at this point we check that we have the required stuff from
2301 * the server
2302 */
2303 if (!ssl3_check_cert_and_algorithm(s)) {
2304 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Matt Caswellfe3a3292015-10-05 10:39:54 +01002305 ossl_statem_set_error(s);
Matt Caswella455d0f2015-09-14 15:06:37 +01002306 return MSG_PROCESS_ERROR;
2307 }
2308
Matt Caswell473483d2015-09-07 22:00:36 +01002309#ifndef OPENSSL_NO_SCTP
2310 /* Only applies to renegotiation */
2311 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2312 && s->renegotiate != 0)
2313 return MSG_PROCESS_CONTINUE_PROCESSING;
2314 else
2315#endif
2316 return MSG_PROCESS_FINISHED_READING;
Matt Caswell0f113f32015-01-22 03:40:55 +00002317}
Bodo Möller176f31d2003-02-28 15:37:10 +00002318
Matt Caswellb9908bf2015-07-29 14:08:49 +01002319int tls_construct_client_key_exchange(SSL *s)
2320{
Matt Caswell0f113f32015-01-22 03:40:55 +00002321 unsigned char *p;
2322 int n;
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002323#ifndef OPENSSL_NO_PSK
2324 size_t pskhdrlen = 0;
2325#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00002326 unsigned long alg_k;
Richard Levittebc36ee62001-02-20 08:13:47 +00002327#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +00002328 unsigned char *q;
2329 EVP_PKEY *pkey = NULL;
Ulf Möller79df9d61999-04-27 03:19:12 +00002330#endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04002331#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00002332 EC_KEY *clnt_ecdh = NULL;
2333 const EC_POINT *srvr_ecpoint = NULL;
2334 EVP_PKEY *srvr_pub_pkey = NULL;
2335 unsigned char *encodedPoint = NULL;
2336 int encoded_pt_len = 0;
2337 BN_CTX *bn_ctx = NULL;
Bodo Möllerea262262002-08-09 08:56:08 +00002338#endif
Dr. Stephen Hensonc660ec62015-01-23 02:41:09 +00002339 unsigned char *pms = NULL;
2340 size_t pmslen = 0;
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002341 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002342
Matt Caswellb9908bf2015-07-29 14:08:49 +01002343 p = ssl_handshake_start(s);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002344
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002345
2346#ifndef OPENSSL_NO_PSK
Matt Caswellb9908bf2015-07-29 14:08:49 +01002347 if (alg_k & SSL_PSK) {
2348 int psk_err = 1;
2349 /*
2350 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2351 * \0-terminated identity. The last byte is for us for simulating
2352 * strnlen.
2353 */
2354 char identity[PSK_MAX_IDENTITY_LEN + 1];
2355 size_t identitylen;
2356 unsigned char psk[PSK_MAX_PSK_LEN];
2357 size_t psklen;
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002358
Matt Caswellb9908bf2015-07-29 14:08:49 +01002359 if (s->psk_client_callback == NULL) {
2360 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2361 SSL_R_PSK_NO_CLIENT_CB);
2362 goto err;
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002363 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002364
2365 memset(identity, 0, sizeof(identity));
2366
2367 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2368 identity, sizeof(identity) - 1,
2369 psk, sizeof(psk));
2370
2371 if (psklen > PSK_MAX_PSK_LEN) {
2372 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2373 ERR_R_INTERNAL_ERROR);
2374 goto psk_err;
2375 } else if (psklen == 0) {
2376 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2377 SSL_R_PSK_IDENTITY_NOT_FOUND);
2378 goto psk_err;
2379 }
2380
2381 OPENSSL_free(s->s3->tmp.psk);
2382 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2383 OPENSSL_cleanse(psk, psklen);
2384
2385 if (s->s3->tmp.psk == NULL) {
2386 OPENSSL_cleanse(identity, sizeof(identity));
2387 goto memerr;
2388 }
2389
2390 s->s3->tmp.psklen = psklen;
2391
2392 identitylen = strlen(identity);
2393 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2394 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2395 ERR_R_INTERNAL_ERROR);
2396 goto psk_err;
2397 }
2398 OPENSSL_free(s->session->psk_identity);
2399 s->session->psk_identity = BUF_strdup(identity);
2400 if (s->session->psk_identity == NULL) {
2401 OPENSSL_cleanse(identity, sizeof(identity));
2402 goto memerr;
2403 }
2404
2405 s2n(identitylen, p);
2406 memcpy(p, identity, identitylen);
2407 pskhdrlen = 2 + identitylen;
2408 p += identitylen;
2409 psk_err = 0;
2410psk_err:
2411 OPENSSL_cleanse(identity, sizeof(identity));
2412 if (psk_err != 0) {
2413 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2414 goto err;
2415 }
2416 }
2417 if (alg_k & SSL_kPSK) {
2418 n = 0;
2419 } else
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002420#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002421
Matt Caswellb9908bf2015-07-29 14:08:49 +01002422 /* Fool emacs indentation */
2423 if (0) {
2424 }
Richard Levittebc36ee62001-02-20 08:13:47 +00002425#ifndef OPENSSL_NO_RSA
Matt Caswellb9908bf2015-07-29 14:08:49 +01002426 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2427 RSA *rsa;
2428 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2429 pms = OPENSSL_malloc(pmslen);
Matt Caswella71edf32015-10-30 10:05:53 +00002430 if (pms == NULL)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002431 goto memerr;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002432
Matt Caswellb9908bf2015-07-29 14:08:49 +01002433 if (s->session->peer == NULL) {
2434 /*
2435 * We should always have a server certificate with SSL_kRSA.
2436 */
2437 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2438 ERR_R_INTERNAL_ERROR);
2439 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00002440 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002441
2442 if (s->s3->peer_rsa_tmp != NULL)
2443 rsa = s->s3->peer_rsa_tmp;
2444 else {
2445 pkey = X509_get_pubkey(s->session->peer);
2446 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2447 || (pkey->pkey.rsa == NULL)) {
2448 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2449 ERR_R_INTERNAL_ERROR);
2450 EVP_PKEY_free(pkey);
2451 goto err;
2452 }
2453 rsa = pkey->pkey.rsa;
2454 EVP_PKEY_free(pkey);
2455 }
2456
2457 pms[0] = s->client_version >> 8;
2458 pms[1] = s->client_version & 0xff;
2459 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2460 goto err;
2461
2462 q = p;
2463 /* Fix buf for TLS and beyond */
2464 if (s->version > SSL3_VERSION)
2465 p += 2;
2466 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2467# ifdef PKCS1_CHECK
2468 if (s->options & SSL_OP_PKCS1_CHECK_1)
2469 p[1]++;
2470 if (s->options & SSL_OP_PKCS1_CHECK_2)
2471 tmp_buf[0] = 0x70;
2472# endif
2473 if (n <= 0) {
2474 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2475 SSL_R_BAD_RSA_ENCRYPT);
2476 goto err;
2477 }
2478
2479 /* Fix buf for TLS and beyond */
2480 if (s->version > SSL3_VERSION) {
2481 s2n(n, q);
2482 n += 2;
2483 }
2484 }
Richard Levittef9b3bff2000-11-30 22:53:34 +00002485#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00002486#ifndef OPENSSL_NO_DH
Matt Caswellb9908bf2015-07-29 14:08:49 +01002487 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2488 DH *dh_srvr, *dh_clnt;
2489 if (s->s3->peer_dh_tmp != NULL)
2490 dh_srvr = s->s3->peer_dh_tmp;
2491 else {
2492 /* we get them from the cert */
2493 EVP_PKEY *spkey = NULL;
2494 dh_srvr = NULL;
2495 spkey = X509_get_pubkey(s->session->peer);
2496 if (spkey) {
2497 dh_srvr = EVP_PKEY_get1_DH(spkey);
2498 EVP_PKEY_free(spkey);
Matt Caswell0f113f32015-01-22 03:40:55 +00002499 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002500 if (dh_srvr == NULL) {
2501 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2502 ERR_R_INTERNAL_ERROR);
2503 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00002504 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002505 }
2506 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2507 /* Use client certificate key */
2508 EVP_PKEY *clkey = s->cert->key->privatekey;
2509 dh_clnt = NULL;
2510 if (clkey)
2511 dh_clnt = EVP_PKEY_get1_DH(clkey);
2512 if (dh_clnt == NULL) {
2513 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2514 ERR_R_INTERNAL_ERROR);
2515 goto err;
2516 }
2517 } else {
2518 /* generate a new random key */
2519 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2520 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2521 goto err;
2522 }
2523 if (!DH_generate_key(dh_clnt)) {
2524 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +00002525 DH_free(dh_clnt);
2526 goto err;
2527 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002528 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002529
2530 pmslen = DH_size(dh_clnt);
2531 pms = OPENSSL_malloc(pmslen);
Matt Caswella71edf32015-10-30 10:05:53 +00002532 if (pms == NULL)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002533 goto memerr;
2534
2535 /*
2536 * use the 'p' output buffer for the DH key, but make sure to
2537 * clear it out afterwards
2538 */
2539
2540 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2541 if (s->s3->peer_dh_tmp == NULL)
2542 DH_free(dh_srvr);
2543
2544 if (n <= 0) {
2545 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2546 DH_free(dh_clnt);
2547 goto err;
2548 }
2549 pmslen = n;
2550
2551 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2552 n = 0;
2553 else {
2554 /* send off the data */
2555 n = BN_num_bytes(dh_clnt->pub_key);
2556 s2n(n, p);
2557 BN_bn2bin(dh_clnt->pub_key, p);
2558 n += 2;
2559 }
2560
2561 DH_free(dh_clnt);
2562 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002563#endif
Bodo Möllerea262262002-08-09 08:56:08 +00002564
Rich Salz10bf4fc2015-03-10 19:09:27 -04002565#ifndef OPENSSL_NO_EC
Matt Caswellb9908bf2015-07-29 14:08:49 +01002566 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2567 const EC_GROUP *srvr_group = NULL;
2568 EC_KEY *tkey;
2569 int ecdh_clnt_cert = 0;
2570 int field_size = 0;
2571 /*
2572 * Did we send out the client's ECDH share for use in premaster
2573 * computation as part of client certificate? If so, set
2574 * ecdh_clnt_cert to 1.
2575 */
2576 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2577 /*-
2578 * XXX: For now, we do not support client
2579 * authentication using ECDH certificates.
2580 * To add such support, one needs to add
2581 * code that checks for appropriate
2582 * conditions and sets ecdh_clnt_cert to 1.
2583 * For example, the cert have an ECC
2584 * key on the same curve as the server's
2585 * and the key should be authorized for
2586 * key agreement.
Matt Caswell0f113f32015-01-22 03:40:55 +00002587 *
Matt Caswellb9908bf2015-07-29 14:08:49 +01002588 * One also needs to add code in ssl3_connect
2589 * to skip sending the certificate verify
2590 * message.
2591 *
2592 * if ((s->cert->key->privatekey != NULL) &&
2593 * (s->cert->key->privatekey->type ==
2594 * EVP_PKEY_EC) && ...)
2595 * ecdh_clnt_cert = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00002596 */
Matt Caswell0f113f32015-01-22 03:40:55 +00002597 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002598
2599 if (s->s3->peer_ecdh_tmp != NULL) {
2600 tkey = s->s3->peer_ecdh_tmp;
2601 } else {
2602 /* Get the Server Public Key from Cert */
2603 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2604 if ((srvr_pub_pkey == NULL)
2605 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2606 || (srvr_pub_pkey->pkey.ec == NULL)) {
2607 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
Matt Caswell0f113f32015-01-22 03:40:55 +00002608 ERR_R_INTERNAL_ERROR);
2609 goto err;
2610 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002611
2612 tkey = srvr_pub_pkey->pkey.ec;
Matt Caswell0f113f32015-01-22 03:40:55 +00002613 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002614
2615 srvr_group = EC_KEY_get0_group(tkey);
2616 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2617
2618 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2619 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2620 ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00002621 goto err;
2622 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002623
Matt Caswellb9908bf2015-07-29 14:08:49 +01002624 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2625 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2626 ERR_R_MALLOC_FAILURE);
2627 goto err;
2628 }
2629
2630 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2631 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2632 goto err;
2633 }
2634 if (ecdh_clnt_cert) {
2635 /*
2636 * Reuse key info from our certificate We only need our
2637 * private key to perform the ECDH computation.
2638 */
2639 const BIGNUM *priv_key;
2640 tkey = s->cert->key->privatekey->pkey.ec;
2641 priv_key = EC_KEY_get0_private_key(tkey);
2642 if (priv_key == NULL) {
2643 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2644 ERR_R_MALLOC_FAILURE);
2645 goto err;
2646 }
2647 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2648 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2649 goto err;
2650 }
2651 } else {
2652 /* Generate a new ECDH key pair */
2653 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2654 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2655 ERR_R_ECDH_LIB);
2656 goto err;
2657 }
2658 }
2659
2660 /*
2661 * use the 'p' output buffer for the ECDH key, but make sure to
2662 * clear it out afterwards
2663 */
2664
2665 field_size = EC_GROUP_get_degree(srvr_group);
2666 if (field_size <= 0) {
2667 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2668 goto err;
2669 }
2670 pmslen = (field_size + 7) / 8;
2671 pms = OPENSSL_malloc(pmslen);
Matt Caswella71edf32015-10-30 10:05:53 +00002672 if (pms == NULL)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002673 goto memerr;
2674 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2675 if (n <= 0 || pmslen != (size_t)n) {
2676 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2677 goto err;
2678 }
2679
2680 if (ecdh_clnt_cert) {
2681 /* Send empty client key exch message */
2682 n = 0;
2683 } else {
2684 /*
2685 * First check the size of encoding and allocate memory
2686 * accordingly.
2687 */
2688 encoded_pt_len =
2689 EC_POINT_point2oct(srvr_group,
2690 EC_KEY_get0_public_key(clnt_ecdh),
2691 POINT_CONVERSION_UNCOMPRESSED,
2692 NULL, 0, NULL);
2693
2694 encodedPoint = (unsigned char *)
2695 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2696 bn_ctx = BN_CTX_new();
2697 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2698 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2699 ERR_R_MALLOC_FAILURE);
2700 goto err;
2701 }
2702
2703 /* Encode the public key */
2704 n = EC_POINT_point2oct(srvr_group,
2705 EC_KEY_get0_public_key(clnt_ecdh),
2706 POINT_CONVERSION_UNCOMPRESSED,
2707 encodedPoint, encoded_pt_len, bn_ctx);
2708
2709 *p = n; /* length of encoded point */
2710 /* Encoded point will be copied here */
2711 p += 1;
2712 /* copy the point */
2713 memcpy(p, encodedPoint, n);
2714 /* increment n to account for length field */
2715 n += 1;
2716 }
2717
2718 /* Free allocated memory */
2719 BN_CTX_free(bn_ctx);
2720 OPENSSL_free(encodedPoint);
2721 EC_KEY_free(clnt_ecdh);
2722 EVP_PKEY_free(srvr_pub_pkey);
2723 }
2724#endif /* !OPENSSL_NO_EC */
2725 else if (alg_k & SSL_kGOST) {
2726 /* GOST key exchange message creation */
2727 EVP_PKEY_CTX *pkey_ctx;
2728 X509 *peer_cert;
2729 size_t msglen;
2730 unsigned int md_len;
2731 unsigned char shared_ukm[32], tmp[256];
2732 EVP_MD_CTX *ukm_hash;
2733 EVP_PKEY *pub_key;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00002734 int dgst_nid = NID_id_GostR3411_94;
2735 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2736 dgst_nid = NID_id_GostR3411_2012_256;
2737
Matt Caswellb9908bf2015-07-29 14:08:49 +01002738
2739 pmslen = 32;
2740 pms = OPENSSL_malloc(pmslen);
Matt Caswella71edf32015-10-30 10:05:53 +00002741 if (pms == NULL)
Matt Caswellb9908bf2015-07-29 14:08:49 +01002742 goto memerr;
2743
2744 /*
2745 * Get server sertificate PKEY and create ctx from it
2746 */
2747 peer_cert = s->session->peer;
2748 if (!peer_cert) {
2749 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2750 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2751 goto err;
2752 }
2753
2754 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2755 X509_get_pubkey(peer_cert), NULL);
Matt Caswella71edf32015-10-30 10:05:53 +00002756 if (pkey_ctx == NULL) {
2757 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2758 ERR_R_MALLOC_FAILURE);
2759 goto err;
2760 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002761 /*
2762 * If we have send a certificate, and certificate key
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002763 * parameters match those of server certificate, use
Matt Caswellb9908bf2015-07-29 14:08:49 +01002764 * certificate key for key exchange
2765 */
2766
2767 /* Otherwise, generate ephemeral key pair */
2768
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002769 if (pkey_ctx == NULL
2770 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2771 /* Generate session key */
2772 || RAND_bytes(pms, pmslen) <= 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002773 EVP_PKEY_CTX_free(pkey_ctx);
2774 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2775 ERR_R_INTERNAL_ERROR);
2776 goto err;
2777 };
2778 /*
2779 * If we have client certificate, use its secret as peer key
2780 */
2781 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2782 if (EVP_PKEY_derive_set_peer
2783 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2784 /*
2785 * If there was an error - just ignore it. Ephemeral key
2786 * * would be used
2787 */
2788 ERR_clear_error();
2789 }
2790 }
2791 /*
2792 * Compute shared IV and store it in algorithm-specific context
2793 * data
2794 */
2795 ukm_hash = EVP_MD_CTX_create();
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002796 if (EVP_DigestInit(ukm_hash,
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00002797 EVP_get_digestbynid(dgst_nid)) <= 0
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002798 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2799 SSL3_RANDOM_SIZE) <= 0
2800 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2801 SSL3_RANDOM_SIZE) <= 0
2802 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2803 EVP_MD_CTX_destroy(ukm_hash);
2804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2805 ERR_R_INTERNAL_ERROR);
2806 goto err;
2807 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002808 EVP_MD_CTX_destroy(ukm_hash);
2809 if (EVP_PKEY_CTX_ctrl
2810 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2811 shared_ukm) < 0) {
2812 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2813 SSL_R_LIBRARY_BUG);
2814 goto err;
2815 }
2816 /* Make GOST keytransport blob message */
2817 /*
2818 * Encapsulate it into sequence
2819 */
2820 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2821 msglen = 255;
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002822 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01002823 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2824 SSL_R_LIBRARY_BUG);
2825 goto err;
2826 }
2827 if (msglen >= 0x80) {
2828 *(p++) = 0x81;
2829 *(p++) = msglen & 0xff;
2830 n = msglen + 3;
2831 } else {
2832 *(p++) = msglen & 0xff;
2833 n = msglen + 2;
2834 }
2835 memcpy(p, tmp, msglen);
2836 /* Check if pubkey from client certificate was used */
2837 if (EVP_PKEY_CTX_ctrl
2838 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2839 /* Set flag "skip certificate verify" */
2840 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2841 }
2842 EVP_PKEY_CTX_free(pkey_ctx);
2843 EVP_PKEY_free(pub_key);
2844
2845 }
2846#ifndef OPENSSL_NO_SRP
2847 else if (alg_k & SSL_kSRP) {
2848 if (s->srp_ctx.A != NULL) {
2849 /* send off the data */
2850 n = BN_num_bytes(s->srp_ctx.A);
2851 s2n(n, p);
2852 BN_bn2bin(s->srp_ctx.A, p);
2853 n += 2;
2854 } else {
2855 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2856 ERR_R_INTERNAL_ERROR);
2857 goto err;
2858 }
2859 OPENSSL_free(s->session->srp_username);
2860 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2861 if (s->session->srp_username == NULL) {
2862 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2863 ERR_R_MALLOC_FAILURE);
2864 goto err;
2865 }
2866 }
2867#endif
2868 else {
2869 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2870 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2871 goto err;
2872 }
2873
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002874#ifndef OPENSSL_NO_PSK
Matt Caswellb9908bf2015-07-29 14:08:49 +01002875 n += pskhdrlen;
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002876#endif
2877
Matt Caswellb9908bf2015-07-29 14:08:49 +01002878 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2879 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2880 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2881 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00002882 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002883
Matt Caswellb9908bf2015-07-29 14:08:49 +01002884 s->s3->tmp.pms = pms;
2885 s->s3->tmp.pmslen = pmslen;
2886
2887 return 1;
Dr. Stephen Hensonc660ec62015-01-23 02:41:09 +00002888 memerr:
2889 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002890 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
Matt Caswell0f113f32015-01-22 03:40:55 +00002891 err:
Rich Salz4b45c6e2015-04-30 17:57:32 -04002892 OPENSSL_clear_free(pms, pmslen);
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01002893 s->s3->tmp.pms = NULL;
Rich Salz10bf4fc2015-03-10 19:09:27 -04002894#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00002895 BN_CTX_free(bn_ctx);
Rich Salzb548a1f2015-05-01 10:02:07 -04002896 OPENSSL_free(encodedPoint);
Rich Salz8fdc3732015-03-25 18:35:24 -04002897 EC_KEY_free(clnt_ecdh);
Matt Caswell0f113f32015-01-22 03:40:55 +00002898 EVP_PKEY_free(srvr_pub_pkey);
Bodo Möllerea262262002-08-09 08:56:08 +00002899#endif
Dr. Stephen Henson76890822015-06-28 17:15:10 +01002900#ifndef OPENSSL_NO_PSK
2901 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2902 s->s3->tmp.psk = NULL;
2903#endif
Matt Caswellfe3a3292015-10-05 10:39:54 +01002904 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01002905 return 0;
2906}
2907
2908int tls_client_key_exchange_post_work(SSL *s)
2909{
2910 unsigned char *pms = NULL;
2911 size_t pmslen = 0;
2912
2913#ifndef OPENSSL_NO_SRP
2914 /* Check for SRP */
2915 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2916 if (!srp_generate_client_master_secret(s)) {
2917 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2918 ERR_R_INTERNAL_ERROR);
2919 goto err;
2920 }
2921 return 1;
2922 }
2923#endif
2924 pms = s->s3->tmp.pms;
2925 pmslen = s->s3->tmp.pmslen;
2926
2927 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2928 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2929 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2930 goto err;
2931 }
2932 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2933 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2934 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2935 goto err;
2936 }
Matt Caswell473483d2015-09-07 22:00:36 +01002937
2938#ifndef OPENSSL_NO_SCTP
2939 if (SSL_IS_DTLS(s)) {
2940 unsigned char sctpauthkey[64];
2941 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2942
2943 /*
2944 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2945 * used.
2946 */
Matt Caswell141eb8c2015-10-26 12:00:00 +00002947 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2948 sizeof(DTLS1_SCTP_AUTH_LABEL));
Matt Caswell473483d2015-09-07 22:00:36 +01002949
2950 if (SSL_export_keying_material(s, sctpauthkey,
2951 sizeof(sctpauthkey), labelbuffer,
2952 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2953 goto err;
2954
2955 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2956 sizeof(sctpauthkey), sctpauthkey);
2957 }
2958#endif
2959
Matt Caswellb9908bf2015-07-29 14:08:49 +01002960 return 1;
2961 err:
2962 OPENSSL_clear_free(pms, pmslen);
2963 s->s3->tmp.pms = NULL;
2964 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00002965}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002966
Matt Caswellb9908bf2015-07-29 14:08:49 +01002967int tls_construct_client_verify(SSL *s)
2968{
Matt Caswell0f113f32015-01-22 03:40:55 +00002969 unsigned char *p;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00002970 unsigned char data[EVP_MAX_MD_SIZE]; /* GOST R 34.11-2012-256*/
Matt Caswell0f113f32015-01-22 03:40:55 +00002971 EVP_PKEY *pkey;
2972 EVP_PKEY_CTX *pctx = NULL;
2973 EVP_MD_CTX mctx;
2974 unsigned u = 0;
2975 unsigned long n;
2976 int j;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002977
Matt Caswell0f113f32015-01-22 03:40:55 +00002978 EVP_MD_CTX_init(&mctx);
Dr. Stephen Henson855a54a2011-05-12 17:35:03 +00002979
Matt Caswellb9908bf2015-07-29 14:08:49 +01002980 p = ssl_handshake_start(s);
2981 pkey = s->cert->key->privatekey;
Matt Caswella71edf32015-10-30 10:05:53 +00002982 /* Create context from key and test if sha1 is allowed as digest */
Matt Caswellb9908bf2015-07-29 14:08:49 +01002983 pctx = EVP_PKEY_CTX_new(pkey, NULL);
Matt Caswella71edf32015-10-30 10:05:53 +00002984 if (pctx == NULL) {
2985 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2986 goto err;
2987 }
Matt Caswell5f3d93e2015-11-06 16:31:21 +00002988 if (EVP_PKEY_sign_init(pctx) <= 0) {
2989 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2990 goto err;
2991 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01002992 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2993 if (!SSL_USE_SIGALGS(s))
2994 s->method->ssl3_enc->cert_verify_mac(s,
2995 NID_sha1,
2996 &(data
2997 [MD5_DIGEST_LENGTH]));
2998 } else {
2999 ERR_clear_error();
3000 }
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003001
Matt Caswellb9908bf2015-07-29 14:08:49 +01003002 /*
3003 * For TLS v1.2 send signature algorithm and signature using agreed
3004 * digest and cached handshake records.
3005 */
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003006 if (SSL_USE_SIGALGS(s) || pkey->type == NID_id_GostR3410_2001
3007 || pkey->type == NID_id_GostR3410_2012_256
3008 || pkey->type == NID_id_GostR3410_2012_512) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01003009 long hdatalen = 0;
3010 void *hdata;
3011 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3012 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003013 if (!SSL_USE_SIGALGS(s)) {
3014 int dgst_nid;
3015 if (EVP_PKEY_get_default_digest_nid(pkey, &dgst_nid) <= 0
3016 || (md = EVP_get_digestbynid(dgst_nid)) == NULL) {
3017 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3018 goto err;
3019 }
3020 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01003021 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3022 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3023 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00003024 }
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003025 if (SSL_USE_SIGALGS(s) ) {
3026 p += 2;
3027 }
Dr. Stephen Henson855a54a2011-05-12 17:35:03 +00003028#ifdef SSL_DEBUG
Matt Caswellb9908bf2015-07-29 14:08:49 +01003029 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3030 EVP_MD_name(md));
Dr. Stephen Henson855a54a2011-05-12 17:35:03 +00003031#endif
Matt Caswellb9908bf2015-07-29 14:08:49 +01003032 if (!EVP_SignInit_ex(&mctx, md, NULL)
3033 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3034 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3035 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3036 goto err;
3037 }
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003038 if (pkey->type == NID_id_GostR3410_2001
3039 || pkey->type == NID_id_GostR3410_2012_256
3040 || pkey->type == NID_id_GostR3410_2012_512) {
3041 unsigned int i, k;
3042 for (i = u - 1, k = 0; k < u/2; k++, i--) {
3043 char c = p[2 + k];
3044 p[2 + k] = p[2 + i];
3045 p[2 + i] = c;
3046 }
3047 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01003048 s2n(u, p);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003049 n = u + 2;
3050 if (SSL_USE_SIGALGS(s))
3051 n += 2;
Matt Caswellb9908bf2015-07-29 14:08:49 +01003052 /* Digest cached records and discard handshake buffer */
3053 if (!ssl3_digest_cached_records(s, 0))
3054 goto err;
3055 } else
Richard Levittebc36ee62001-02-20 08:13:47 +00003056#ifndef OPENSSL_NO_RSA
Matt Caswellb9908bf2015-07-29 14:08:49 +01003057 if (pkey->type == EVP_PKEY_RSA) {
3058 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3059 if (RSA_sign(NID_md5_sha1, data,
3060 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3061 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3062 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3063 goto err;
3064 }
3065 s2n(u, p);
3066 n = u + 2;
3067 } else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003068#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003069#ifndef OPENSSL_NO_DSA
Matt Caswellb9908bf2015-07-29 14:08:49 +01003070 if (pkey->type == EVP_PKEY_DSA) {
3071 if (!DSA_sign(pkey->save_type,
3072 &(data[MD5_DIGEST_LENGTH]),
3073 SHA_DIGEST_LENGTH, &(p[2]),
3074 (unsigned int *)&j, pkey->pkey.dsa)) {
3075 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3076 goto err;
3077 }
3078 s2n(j, p);
3079 n = j + 2;
3080 } else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003081#endif
Rich Salz10bf4fc2015-03-10 19:09:27 -04003082#ifndef OPENSSL_NO_EC
Matt Caswellb9908bf2015-07-29 14:08:49 +01003083 if (pkey->type == EVP_PKEY_EC) {
3084 if (!ECDSA_sign(pkey->save_type,
3085 &(data[MD5_DIGEST_LENGTH]),
3086 SHA_DIGEST_LENGTH, &(p[2]),
3087 (unsigned int *)&j, pkey->pkey.ec)) {
3088 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3089 goto err;
3090 }
3091 s2n(j, p);
3092 n = j + 2;
3093 } else
Bodo Möllerea262262002-08-09 08:56:08 +00003094#endif
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003095 if (pkey->type == NID_id_GostR3410_2001
3096 || pkey->type == NID_id_GostR3410_2012_256
3097 || pkey->type == NID_id_GostR3410_2012_512) {
3098 unsigned char signbuf[128];
Matt Caswellb9908bf2015-07-29 14:08:49 +01003099 int i;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003100 size_t sigsize =
3101 (pkey->type == NID_id_GostR3410_2012_512) ? 128 : 64;
3102 int dgst_nid = NID_undef;
3103
3104 EVP_PKEY_get_default_digest_nid(pkey, &dgst_nid);
3105 s->method->ssl3_enc->cert_verify_mac(s, dgst_nid, data);
3106 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data,
3107 EVP_MD_size(EVP_get_digestbynid(dgst_nid))) <= 0) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01003108 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00003109 goto err;
3110 }
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00003111 for (i = sigsize - 1, j = 0; i >= 0; j++, i--) {
Matt Caswellb9908bf2015-07-29 14:08:49 +01003112 p[2 + j] = signbuf[i];
Matt Caswell77d514c2015-03-09 15:33:46 +00003113 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01003114 s2n(j, p);
3115 n = j + 2;
3116 } else {
3117 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3118 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00003119 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01003120 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3121 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3122 goto err;
3123 }
3124
Matt Caswell0f113f32015-01-22 03:40:55 +00003125 EVP_MD_CTX_cleanup(&mctx);
3126 EVP_PKEY_CTX_free(pctx);
Matt Caswellb9908bf2015-07-29 14:08:49 +01003127 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00003128 err:
3129 EVP_MD_CTX_cleanup(&mctx);
3130 EVP_PKEY_CTX_free(pctx);
Matt Caswellb9908bf2015-07-29 14:08:49 +01003131 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00003132}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003133
Matt Caswell0f113f32015-01-22 03:40:55 +00003134/*
3135 * Check a certificate can be used for client authentication. Currently check
3136 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3137 * certificates can be used and optionally checks suitability for Suite B.
Dr. Stephen Henson0d609392012-01-25 14:51:49 +00003138 */
3139static int ssl3_check_client_certificate(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00003140{
3141 unsigned long alg_k;
3142 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3143 return 0;
3144 /* If no suitable signature algorithm can't use certificate */
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01003145 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
Matt Caswell0f113f32015-01-22 03:40:55 +00003146 return 0;
3147 /*
3148 * If strict mode check suitability of chain before using it. This also
3149 * adjusts suite B digest if necessary.
3150 */
3151 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3152 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3153 return 0;
3154 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3155 /* See if we can use client certificate for fixed DH */
3156 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003157 int i = s->session->peer_type;
Matt Caswell0f113f32015-01-22 03:40:55 +00003158 EVP_PKEY *clkey = NULL, *spkey = NULL;
3159 clkey = s->cert->key->privatekey;
3160 /* If client key not DH assume it can be used */
3161 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3162 return 1;
3163 if (i >= 0)
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003164 spkey = X509_get_pubkey(s->session->peer);
Matt Caswell0f113f32015-01-22 03:40:55 +00003165 if (spkey) {
3166 /* Compare server and client parameters */
3167 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3168 EVP_PKEY_free(spkey);
3169 if (i != 1)
3170 return 0;
3171 }
3172 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3173 }
3174 return 1;
3175}
Dr. Stephen Henson0d609392012-01-25 14:51:49 +00003176
Matt Caswellbe3583f2015-10-26 11:46:33 +00003177WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
Matt Caswellb9908bf2015-07-29 14:08:49 +01003178{
Matt Caswell0f113f32015-01-22 03:40:55 +00003179 X509 *x509 = NULL;
3180 EVP_PKEY *pkey = NULL;
3181 int i;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003182
Matt Caswellb9908bf2015-07-29 14:08:49 +01003183 if (wst == WORK_MORE_A) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003184 /* Let cert callback update client certificates if required */
3185 if (s->cert->cert_cb) {
3186 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3187 if (i < 0) {
3188 s->rwstate = SSL_X509_LOOKUP;
Matt Caswellb9908bf2015-07-29 14:08:49 +01003189 return WORK_MORE_A;
Matt Caswell0f113f32015-01-22 03:40:55 +00003190 }
3191 if (i == 0) {
3192 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellfe3a3292015-10-05 10:39:54 +01003193 ossl_statem_set_error(s);
Matt Caswell0f113f32015-01-22 03:40:55 +00003194 return 0;
3195 }
3196 s->rwstate = SSL_NOTHING;
3197 }
3198 if (ssl3_check_client_certificate(s))
Matt Caswellb9908bf2015-07-29 14:08:49 +01003199 return WORK_FINISHED_CONTINUE;
3200
3201 /* Fall through to WORK_MORE_B */
3202 wst = WORK_MORE_B;
Matt Caswell0f113f32015-01-22 03:40:55 +00003203 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003204
Matt Caswell0f113f32015-01-22 03:40:55 +00003205 /* We need to get a client cert */
Matt Caswellb9908bf2015-07-29 14:08:49 +01003206 if (wst == WORK_MORE_B) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003207 /*
3208 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3209 * return(-1); We then get retied later
3210 */
Matt Caswell0f113f32015-01-22 03:40:55 +00003211 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3212 if (i < 0) {
3213 s->rwstate = SSL_X509_LOOKUP;
Matt Caswellb9908bf2015-07-29 14:08:49 +01003214 return WORK_MORE_B;
Matt Caswell0f113f32015-01-22 03:40:55 +00003215 }
3216 s->rwstate = SSL_NOTHING;
3217 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003218 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3219 i = 0;
3220 } else if (i == 1) {
3221 i = 0;
Matt Caswellb9908bf2015-07-29 14:08:49 +01003222 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
Matt Caswell0f113f32015-01-22 03:40:55 +00003223 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3224 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003225
Rich Salz222561f2015-04-30 17:33:59 -04003226 X509_free(x509);
Rich Salz25aaa982015-05-01 14:37:16 -04003227 EVP_PKEY_free(pkey);
Matt Caswell0f113f32015-01-22 03:40:55 +00003228 if (i && !ssl3_check_client_certificate(s))
3229 i = 0;
3230 if (i == 0) {
3231 if (s->version == SSL3_VERSION) {
3232 s->s3->tmp.cert_req = 0;
3233 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
Matt Caswellb9908bf2015-07-29 14:08:49 +01003234 return WORK_FINISHED_CONTINUE;
Matt Caswell0f113f32015-01-22 03:40:55 +00003235 } else {
3236 s->s3->tmp.cert_req = 2;
Dr. Stephen Henson124037f2015-06-16 14:44:29 +01003237 if (!ssl3_digest_cached_records(s, 0)) {
Dr. Stephen Hensondab18ab2015-05-07 00:04:48 +01003238 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellfe3a3292015-10-05 10:39:54 +01003239 ossl_statem_set_error(s);
Dr. Stephen Hensondab18ab2015-05-07 00:04:48 +01003240 return 0;
3241 }
Matt Caswell0f113f32015-01-22 03:40:55 +00003242 }
3243 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003244
Matt Caswellb9908bf2015-07-29 14:08:49 +01003245 return WORK_FINISHED_CONTINUE;
Matt Caswell0f113f32015-01-22 03:40:55 +00003246 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003247
Matt Caswellb9908bf2015-07-29 14:08:49 +01003248 /* Shouldn't ever get here */
3249 return WORK_ERROR;
3250}
3251
3252int tls_construct_client_certificate(SSL *s)
3253{
3254 if (!ssl3_output_cert_chain(s,
3255 (s->s3->tmp.cert_req ==
3256 2) ? NULL : s->cert->key)) {
3257 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3258 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Matt Caswellfe3a3292015-10-05 10:39:54 +01003259 ossl_statem_set_error(s);
Matt Caswellb9908bf2015-07-29 14:08:49 +01003260 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00003261 }
Matt Caswellb9908bf2015-07-29 14:08:49 +01003262
3263 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00003264}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003265
Matt Caswell0f113f32015-01-22 03:40:55 +00003266#define has_bits(i,m) (((i)&(m)) == (m))
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003267
Ben Laurie36d16f82005-04-26 16:02:40 +00003268int ssl3_check_cert_and_algorithm(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00003269{
3270 int i, idx;
3271 long alg_k, alg_a;
3272 EVP_PKEY *pkey = NULL;
Kurt Roeckxac381152015-04-18 12:50:25 +02003273 int pkey_bits;
Richard Levittebc36ee62001-02-20 08:13:47 +00003274#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +00003275 RSA *rsa;
Ulf Möller79df9d61999-04-27 03:19:12 +00003276#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003277#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +00003278 DH *dh;
Ulf Möller79df9d61999-04-27 03:19:12 +00003279#endif
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003280 int al = SSL_AD_HANDSHAKE_FAILURE;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003281
Matt Caswell0f113f32015-01-22 03:40:55 +00003282 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3283 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003284
Matt Caswell0f113f32015-01-22 03:40:55 +00003285 /* we don't have a certificate */
Matt Caswell55a9a162015-05-12 10:27:53 +01003286 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
Matt Caswell0f113f32015-01-22 03:40:55 +00003287 return (1);
Richard Levittebc36ee62001-02-20 08:13:47 +00003288#ifndef OPENSSL_NO_RSA
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01003289 rsa = s->s3->peer_rsa_tmp;
Ulf Möller79df9d61999-04-27 03:19:12 +00003290#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003291#ifndef OPENSSL_NO_DH
Dr. Stephen Henson8d92c1f2015-06-21 16:26:08 +01003292 dh = s->s3->peer_dh_tmp;
Ulf Möller79df9d61999-04-27 03:19:12 +00003293#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003294
Matt Caswell0f113f32015-01-22 03:40:55 +00003295 /* This is the passed certificate */
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003296
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003297 idx = s->session->peer_type;
Rich Salz10bf4fc2015-03-10 19:09:27 -04003298#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +00003299 if (idx == SSL_PKEY_ECC) {
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003300 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +00003301 /* check failed */
3302 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3303 goto f_err;
3304 } else {
3305 return 1;
3306 }
3307 } else if (alg_a & SSL_aECDSA) {
3308 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3309 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3310 goto f_err;
3311 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3312 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3313 goto f_err;
3314 }
Bodo Möllerea262262002-08-09 08:56:08 +00003315#endif
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003316 pkey = X509_get_pubkey(s->session->peer);
Kurt Roeckxac381152015-04-18 12:50:25 +02003317 pkey_bits = EVP_PKEY_bits(pkey);
Dr. Stephen Hensona273c6e2015-06-21 19:08:57 +01003318 i = X509_certificate_type(s->session->peer, pkey);
Matt Caswell0f113f32015-01-22 03:40:55 +00003319 EVP_PKEY_free(pkey);
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003320
Matt Caswell0f113f32015-01-22 03:40:55 +00003321 /* Check that we have a certificate if we require one */
3322 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3323 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3324 SSL_R_MISSING_RSA_SIGNING_CERT);
3325 goto f_err;
3326 }
Richard Levittebc36ee62001-02-20 08:13:47 +00003327#ifndef OPENSSL_NO_DSA
Matt Caswell0f113f32015-01-22 03:40:55 +00003328 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3329 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3330 SSL_R_MISSING_DSA_SIGNING_CERT);
3331 goto f_err;
3332 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003333#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003334#ifndef OPENSSL_NO_RSA
Dr. Stephen Henson76890822015-06-28 17:15:10 +01003335 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003336 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3337 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3338 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3339 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3340 goto f_err;
3341 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3342 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3343 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3344 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3345 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3346 goto f_err;
3347 }
3348 if (rsa != NULL) {
3349 /* server key exchange is not allowed. */
3350 al = SSL_AD_INTERNAL_ERROR;
3351 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3352 goto f_err;
3353 }
3354 }
3355 }
Matt Caswell0f113f32015-01-22 03:40:55 +00003356 }
Ulf Möller79df9d61999-04-27 03:19:12 +00003357#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003358#ifndef OPENSSL_NO_DH
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003359 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3360 al = SSL_AD_INTERNAL_ERROR;
3361 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
Matt Caswell0f113f32015-01-22 03:40:55 +00003362 goto f_err;
3363 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3364 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3365 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3366 SSL_R_MISSING_DH_RSA_CERT);
3367 goto f_err;
3368 }
3369# ifndef OPENSSL_NO_DSA
3370 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3371 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3372 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3373 SSL_R_MISSING_DH_DSA_CERT);
3374 goto f_err;
3375 }
3376# endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003377#endif
3378
Kurt Roeckxac381152015-04-18 12:50:25 +02003379 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3380 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
Richard Levittebc36ee62001-02-20 08:13:47 +00003381#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +00003382 if (alg_k & SSL_kRSA) {
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003383 if (rsa == NULL) {
3384 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3385 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3386 goto f_err;
3387 } else if (RSA_bits(rsa) >
Matt Caswell0f113f32015-01-22 03:40:55 +00003388 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003389 /* We have a temporary RSA key but it's too large. */
3390 al = SSL_AD_EXPORT_RESTRICTION;
Matt Caswell0f113f32015-01-22 03:40:55 +00003391 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3392 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3393 goto f_err;
3394 }
3395 } else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003396#endif
Richard Levittebc36ee62001-02-20 08:13:47 +00003397#ifndef OPENSSL_NO_DH
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003398 if (alg_k & SSL_kDHE) {
3399 if (DH_bits(dh) >
Matt Caswell0f113f32015-01-22 03:40:55 +00003400 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003401 /* We have a temporary DH key but it's too large. */
3402 al = SSL_AD_EXPORT_RESTRICTION;
Matt Caswell0f113f32015-01-22 03:40:55 +00003403 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3404 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3405 goto f_err;
3406 }
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003407 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3408 /* The cert should have had an export DH key. */
3409 al = SSL_AD_EXPORT_RESTRICTION;
3410 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3411 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3412 goto f_err;
Matt Caswell0f113f32015-01-22 03:40:55 +00003413 } else
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003414#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003415 {
3416 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3417 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3418 goto f_err;
3419 }
3420 }
3421 return (1);
3422 f_err:
Kurt Roeckx26c79d52015-04-18 12:23:12 +02003423 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Matt Caswell0f113f32015-01-22 03:40:55 +00003424 return (0);
3425}
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00003426
Matt Caswelle481f9b2015-05-15 10:49:56 +01003427#ifndef OPENSSL_NO_NEXTPROTONEG
Matt Caswellb9908bf2015-07-29 14:08:49 +01003428int tls_construct_next_proto(SSL *s)
3429{
3430 unsigned int len, padding_len;
3431 unsigned char *d;
3432
3433 len = s->next_proto_negotiated_len;
3434 padding_len = 32 - ((len + 2) % 32);
3435 d = (unsigned char *)s->init_buf->data;
3436 d[4] = len;
3437 memcpy(d + 5, s->next_proto_negotiated, len);
3438 d[5 + len] = padding_len;
3439 memset(d + 6 + len, 0, padding_len);
3440 *(d++) = SSL3_MT_NEXT_PROTO;
3441 l2n3(2 + len + padding_len, d);
3442 s->init_num = 4 + 2 + len + padding_len;
3443 s->init_off = 0;
3444
3445 return 1;
3446}
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00003447#endif
Dr. Stephen Henson368888b2008-06-01 22:33:24 +00003448
3449int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
Matt Caswell0f113f32015-01-22 03:40:55 +00003450{
3451 int i = 0;
Dr. Stephen Henson368888b2008-06-01 22:33:24 +00003452#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +00003453 if (s->ctx->client_cert_engine) {
3454 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3455 SSL_get_client_CA_list(s),
3456 px509, ppkey, NULL, NULL, NULL);
3457 if (i != 0)
3458 return i;
3459 }
Dr. Stephen Henson368888b2008-06-01 22:33:24 +00003460#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00003461 if (s->ctx->client_cert_cb)
3462 i = s->ctx->client_cert_cb(s, px509, ppkey);
3463 return i;
3464}
Matt Caswelld45ba432015-04-24 15:05:27 +01003465
3466int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
Emilia Kaspere9fa0922015-09-18 15:00:37 +02003467 unsigned char *p)
Matt Caswelld45ba432015-04-24 15:05:27 +01003468{
3469 int i, j = 0;
3470 SSL_CIPHER *c;
3471 unsigned char *q;
3472 int empty_reneg_info_scsv = !s->renegotiate;
3473 /* Set disabled masks for this session */
3474 ssl_set_client_disabled(s);
3475
3476 if (sk == NULL)
3477 return (0);
3478 q = p;
Matt Caswelld45ba432015-04-24 15:05:27 +01003479
3480 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3481 c = sk_SSL_CIPHER_value(sk, i);
3482 /* Skip disabled ciphers */
3483 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3484 continue;
3485#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3486 if (c->id == SSL3_CK_SCSV) {
3487 if (!empty_reneg_info_scsv)
3488 continue;
3489 else
3490 empty_reneg_info_scsv = 0;
3491 }
3492#endif
Emilia Kaspere9fa0922015-09-18 15:00:37 +02003493 j = s->method->put_cipher_by_char(c, p);
Matt Caswelld45ba432015-04-24 15:05:27 +01003494 p += j;
3495 }
3496 /*
3497 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3498 * applicable SCSVs.
3499 */
3500 if (p != q) {
3501 if (empty_reneg_info_scsv) {
3502 static SSL_CIPHER scsv = {
3503 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3504 };
Emilia Kaspere9fa0922015-09-18 15:00:37 +02003505 j = s->method->put_cipher_by_char(&scsv, p);
Matt Caswelld45ba432015-04-24 15:05:27 +01003506 p += j;
3507#ifdef OPENSSL_RI_DEBUG
3508 fprintf(stderr,
3509 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3510#endif
3511 }
3512 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3513 static SSL_CIPHER scsv = {
3514 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3515 };
Emilia Kaspere9fa0922015-09-18 15:00:37 +02003516 j = s->method->put_cipher_by_char(&scsv, p);
Matt Caswelld45ba432015-04-24 15:05:27 +01003517 p += j;
3518 }
3519 }
3520
3521 return (p - q);
3522}