Coverage Report

Created: 2022-11-30 06:20

/src/openssl/ssl/t1_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* ssl/t1_lib.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * 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.
8
 *
9
 * 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).
15
 *
16
 * 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.
22
 *
23
 * 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 :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * 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.
52
 *
53
 * 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
 */
58
/* ====================================================================
59
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60
 *
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
66
 *    notice, this list of conditions and the following disclaimer.
67
 *
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
 */
111
112
#include <stdio.h>
113
#include <openssl/objects.h>
114
#include <openssl/evp.h>
115
#include <openssl/hmac.h>
116
#ifndef OPENSSL_NO_EC
117
#ifdef OPENSSL_NO_EC2M
118
# include <openssl/ec.h>
119
#endif
120
#endif
121
#include <openssl/ocsp.h>
122
#include <openssl/rand.h>
123
#include "ssl_locl.h"
124
125
const char tls1_version_str[] = "TLSv1" OPENSSL_VERSION_PTEXT;
126
127
#ifndef OPENSSL_NO_TLSEXT
128
static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
129
                              const unsigned char *sess_id, int sesslen,
130
                              SSL_SESSION **psess);
131
static int ssl_check_clienthello_tlsext_early(SSL *s);
132
int ssl_check_serverhello_tlsext(SSL *s);
133
#endif
134
135
#define CHECKLEN(curr, val, limit) \
136
0
    (((curr) >= (limit)) || (size_t)((limit) - (curr)) < (size_t)(val))
137
138
SSL3_ENC_METHOD TLSv1_enc_data = {
139
    tls1_enc,
140
    tls1_mac,
141
    tls1_setup_key_block,
142
    tls1_generate_master_secret,
143
    tls1_change_cipher_state,
144
    tls1_final_finish_mac,
145
    TLS1_FINISH_MAC_LENGTH,
146
    tls1_cert_verify_mac,
147
    TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
148
    TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
149
    tls1_alert_code,
150
    tls1_export_keying_material,
151
    0,
152
    SSL3_HM_HEADER_LENGTH,
153
    ssl3_set_handshake_header,
154
    ssl3_handshake_write
155
};
156
157
SSL3_ENC_METHOD TLSv1_1_enc_data = {
158
    tls1_enc,
159
    tls1_mac,
160
    tls1_setup_key_block,
161
    tls1_generate_master_secret,
162
    tls1_change_cipher_state,
163
    tls1_final_finish_mac,
164
    TLS1_FINISH_MAC_LENGTH,
165
    tls1_cert_verify_mac,
166
    TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
167
    TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
168
    tls1_alert_code,
169
    tls1_export_keying_material,
170
    SSL_ENC_FLAG_EXPLICIT_IV,
171
    SSL3_HM_HEADER_LENGTH,
172
    ssl3_set_handshake_header,
173
    ssl3_handshake_write
174
};
175
176
SSL3_ENC_METHOD TLSv1_2_enc_data = {
177
    tls1_enc,
178
    tls1_mac,
179
    tls1_setup_key_block,
180
    tls1_generate_master_secret,
181
    tls1_change_cipher_state,
182
    tls1_final_finish_mac,
183
    TLS1_FINISH_MAC_LENGTH,
184
    tls1_cert_verify_mac,
185
    TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
186
    TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
187
    tls1_alert_code,
188
    tls1_export_keying_material,
189
    SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
190
        | SSL_ENC_FLAG_TLS1_2_CIPHERS,
191
    SSL3_HM_HEADER_LENGTH,
192
    ssl3_set_handshake_header,
193
    ssl3_handshake_write
194
};
195
196
long tls1_default_timeout(void)
197
0
{
198
    /*
199
     * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
200
     * http, the cache would over fill
201
     */
202
0
    return (60 * 60 * 2);
203
0
}
204
205
int tls1_new(SSL *s)
206
0
{
207
0
    if (!ssl3_new(s))
208
0
        return (0);
209
0
    s->method->ssl_clear(s);
210
0
    return (1);
211
0
}
212
213
void tls1_free(SSL *s)
214
0
{
215
0
#ifndef OPENSSL_NO_TLSEXT
216
0
    if (s->tlsext_session_ticket) {
217
0
        OPENSSL_free(s->tlsext_session_ticket);
218
0
    }
219
0
#endif                          /* OPENSSL_NO_TLSEXT */
220
0
    ssl3_free(s);
221
0
}
222
223
void tls1_clear(SSL *s)
224
0
{
225
0
    ssl3_clear(s);
226
0
    s->version = s->method->version;
227
0
}
228
229
#ifndef OPENSSL_NO_EC
230
231
static int nid_list[] = {
232
    NID_sect163k1,              /* sect163k1 (1) */
233
    NID_sect163r1,              /* sect163r1 (2) */
234
    NID_sect163r2,              /* sect163r2 (3) */
235
    NID_sect193r1,              /* sect193r1 (4) */
236
    NID_sect193r2,              /* sect193r2 (5) */
237
    NID_sect233k1,              /* sect233k1 (6) */
238
    NID_sect233r1,              /* sect233r1 (7) */
239
    NID_sect239k1,              /* sect239k1 (8) */
240
    NID_sect283k1,              /* sect283k1 (9) */
241
    NID_sect283r1,              /* sect283r1 (10) */
242
    NID_sect409k1,              /* sect409k1 (11) */
243
    NID_sect409r1,              /* sect409r1 (12) */
244
    NID_sect571k1,              /* sect571k1 (13) */
245
    NID_sect571r1,              /* sect571r1 (14) */
246
    NID_secp160k1,              /* secp160k1 (15) */
247
    NID_secp160r1,              /* secp160r1 (16) */
248
    NID_secp160r2,              /* secp160r2 (17) */
249
    NID_secp192k1,              /* secp192k1 (18) */
250
    NID_X9_62_prime192v1,       /* secp192r1 (19) */
251
    NID_secp224k1,              /* secp224k1 (20) */
252
    NID_secp224r1,              /* secp224r1 (21) */
253
    NID_secp256k1,              /* secp256k1 (22) */
254
    NID_X9_62_prime256v1,       /* secp256r1 (23) */
255
    NID_secp384r1,              /* secp384r1 (24) */
256
    NID_secp521r1,              /* secp521r1 (25) */
257
    NID_brainpoolP256r1,        /* brainpoolP256r1 (26) */
258
    NID_brainpoolP384r1,        /* brainpoolP384r1 (27) */
259
    NID_brainpoolP512r1         /* brainpool512r1 (28) */
260
};
261
262
static const unsigned char ecformats_default[] = {
263
    TLSEXT_ECPOINTFORMAT_uncompressed,
264
    TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
265
    TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
266
};
267
268
/* The client's default curves / the server's 'auto' curves. */
269
static const unsigned char eccurves_auto[] = {
270
    /* Prefer P-256 which has the fastest and most secure implementations. */
271
    0, 23,                      /* secp256r1 (23) */
272
    /* Other >= 256-bit prime curves. */
273
    0, 25,                      /* secp521r1 (25) */
274
    0, 28,                      /* brainpool512r1 (28) */
275
    0, 27,                      /* brainpoolP384r1 (27) */
276
    0, 24,                      /* secp384r1 (24) */
277
    0, 26,                      /* brainpoolP256r1 (26) */
278
    0, 22,                      /* secp256k1 (22) */
279
# ifndef OPENSSL_NO_EC2M
280
    /* >= 256-bit binary curves. */
281
    0, 14,                      /* sect571r1 (14) */
282
    0, 13,                      /* sect571k1 (13) */
283
    0, 11,                      /* sect409k1 (11) */
284
    0, 12,                      /* sect409r1 (12) */
285
    0, 9,                       /* sect283k1 (9) */
286
    0, 10,                      /* sect283r1 (10) */
287
# endif
288
};
289
290
static const unsigned char eccurves_all[] = {
291
    /* Prefer P-256 which has the fastest and most secure implementations. */
292
    0, 23,                      /* secp256r1 (23) */
293
    /* Other >= 256-bit prime curves. */
294
    0, 25,                      /* secp521r1 (25) */
295
    0, 28,                      /* brainpool512r1 (28) */
296
    0, 27,                      /* brainpoolP384r1 (27) */
297
    0, 24,                      /* secp384r1 (24) */
298
    0, 26,                      /* brainpoolP256r1 (26) */
299
    0, 22,                      /* secp256k1 (22) */
300
# ifndef OPENSSL_NO_EC2M
301
    /* >= 256-bit binary curves. */
302
    0, 14,                      /* sect571r1 (14) */
303
    0, 13,                      /* sect571k1 (13) */
304
    0, 11,                      /* sect409k1 (11) */
305
    0, 12,                      /* sect409r1 (12) */
306
    0, 9,                       /* sect283k1 (9) */
307
    0, 10,                      /* sect283r1 (10) */
308
# endif
309
    /*
310
     * Remaining curves disabled by default but still permitted if set
311
     * via an explicit callback or parameters.
312
     */
313
    0, 20,                      /* secp224k1 (20) */
314
    0, 21,                      /* secp224r1 (21) */
315
    0, 18,                      /* secp192k1 (18) */
316
    0, 19,                      /* secp192r1 (19) */
317
    0, 15,                      /* secp160k1 (15) */
318
    0, 16,                      /* secp160r1 (16) */
319
    0, 17,                      /* secp160r2 (17) */
320
# ifndef OPENSSL_NO_EC2M
321
    0, 8,                       /* sect239k1 (8) */
322
    0, 6,                       /* sect233k1 (6) */
323
    0, 7,                       /* sect233r1 (7) */
324
    0, 4,                       /* sect193r1 (4) */
325
    0, 5,                       /* sect193r2 (5) */
326
    0, 1,                       /* sect163k1 (1) */
327
    0, 2,                       /* sect163r1 (2) */
328
    0, 3,                       /* sect163r2 (3) */
329
# endif
330
};
331
332
static const unsigned char suiteb_curves[] = {
333
    0, TLSEXT_curve_P_256,
334
    0, TLSEXT_curve_P_384
335
};
336
337
# ifdef OPENSSL_FIPS
338
/* Brainpool not allowed in FIPS mode */
339
static const unsigned char fips_curves_default[] = {
340
#  ifndef OPENSSL_NO_EC2M
341
    0, 14,                      /* sect571r1 (14) */
342
    0, 13,                      /* sect571k1 (13) */
343
#  endif
344
    0, 25,                      /* secp521r1 (25) */
345
#  ifndef OPENSSL_NO_EC2M
346
    0, 11,                      /* sect409k1 (11) */
347
    0, 12,                      /* sect409r1 (12) */
348
#  endif
349
    0, 24,                      /* secp384r1 (24) */
350
#  ifndef OPENSSL_NO_EC2M
351
    0, 9,                       /* sect283k1 (9) */
352
    0, 10,                      /* sect283r1 (10) */
353
#  endif
354
    0, 22,                      /* secp256k1 (22) */
355
    0, 23,                      /* secp256r1 (23) */
356
#  ifndef OPENSSL_NO_EC2M
357
    0, 8,                       /* sect239k1 (8) */
358
    0, 6,                       /* sect233k1 (6) */
359
    0, 7,                       /* sect233r1 (7) */
360
#  endif
361
    0, 20,                      /* secp224k1 (20) */
362
    0, 21,                      /* secp224r1 (21) */
363
#  ifndef OPENSSL_NO_EC2M
364
    0, 4,                       /* sect193r1 (4) */
365
    0, 5,                       /* sect193r2 (5) */
366
#  endif
367
    0, 18,                      /* secp192k1 (18) */
368
    0, 19,                      /* secp192r1 (19) */
369
#  ifndef OPENSSL_NO_EC2M
370
    0, 1,                       /* sect163k1 (1) */
371
    0, 2,                       /* sect163r1 (2) */
372
    0, 3,                       /* sect163r2 (3) */
373
#  endif
374
    0, 15,                      /* secp160k1 (15) */
375
    0, 16,                      /* secp160r1 (16) */
376
    0, 17,                      /* secp160r2 (17) */
377
};
378
# endif
379
380
int tls1_ec_curve_id2nid(int curve_id)
381
0
{
382
    /* ECC curves from RFC 4492 and RFC 7027 */
383
0
    if ((curve_id < 1) || ((unsigned int)curve_id >
384
0
                           sizeof(nid_list) / sizeof(nid_list[0])))
385
0
        return 0;
386
0
    return nid_list[curve_id - 1];
387
0
}
388
389
int tls1_ec_nid2curve_id(int nid)
390
0
{
391
    /* ECC curves from RFC 4492 and RFC 7027 */
392
0
    switch (nid) {
393
0
    case NID_sect163k1:        /* sect163k1 (1) */
394
0
        return 1;
395
0
    case NID_sect163r1:        /* sect163r1 (2) */
396
0
        return 2;
397
0
    case NID_sect163r2:        /* sect163r2 (3) */
398
0
        return 3;
399
0
    case NID_sect193r1:        /* sect193r1 (4) */
400
0
        return 4;
401
0
    case NID_sect193r2:        /* sect193r2 (5) */
402
0
        return 5;
403
0
    case NID_sect233k1:        /* sect233k1 (6) */
404
0
        return 6;
405
0
    case NID_sect233r1:        /* sect233r1 (7) */
406
0
        return 7;
407
0
    case NID_sect239k1:        /* sect239k1 (8) */
408
0
        return 8;
409
0
    case NID_sect283k1:        /* sect283k1 (9) */
410
0
        return 9;
411
0
    case NID_sect283r1:        /* sect283r1 (10) */
412
0
        return 10;
413
0
    case NID_sect409k1:        /* sect409k1 (11) */
414
0
        return 11;
415
0
    case NID_sect409r1:        /* sect409r1 (12) */
416
0
        return 12;
417
0
    case NID_sect571k1:        /* sect571k1 (13) */
418
0
        return 13;
419
0
    case NID_sect571r1:        /* sect571r1 (14) */
420
0
        return 14;
421
0
    case NID_secp160k1:        /* secp160k1 (15) */
422
0
        return 15;
423
0
    case NID_secp160r1:        /* secp160r1 (16) */
424
0
        return 16;
425
0
    case NID_secp160r2:        /* secp160r2 (17) */
426
0
        return 17;
427
0
    case NID_secp192k1:        /* secp192k1 (18) */
428
0
        return 18;
429
0
    case NID_X9_62_prime192v1: /* secp192r1 (19) */
430
0
        return 19;
431
0
    case NID_secp224k1:        /* secp224k1 (20) */
432
0
        return 20;
433
0
    case NID_secp224r1:        /* secp224r1 (21) */
434
0
        return 21;
435
0
    case NID_secp256k1:        /* secp256k1 (22) */
436
0
        return 22;
437
0
    case NID_X9_62_prime256v1: /* secp256r1 (23) */
438
0
        return 23;
439
0
    case NID_secp384r1:        /* secp384r1 (24) */
440
0
        return 24;
441
0
    case NID_secp521r1:        /* secp521r1 (25) */
442
0
        return 25;
443
0
    case NID_brainpoolP256r1:  /* brainpoolP256r1 (26) */
444
0
        return 26;
445
0
    case NID_brainpoolP384r1:  /* brainpoolP384r1 (27) */
446
0
        return 27;
447
0
    case NID_brainpoolP512r1:  /* brainpool512r1 (28) */
448
0
        return 28;
449
0
    default:
450
0
        return 0;
451
0
    }
452
0
}
453
454
/*
455
 * Get curves list, if "sess" is set return client curves otherwise
456
 * preferred list.
457
 * Sets |num_curves| to the number of curves in the list, i.e.,
458
 * the length of |pcurves| is 2 * num_curves.
459
 * Returns 1 on success and 0 if the client curves list has invalid format.
460
 * The latter indicates an internal error: we should not be accepting such
461
 * lists in the first place.
462
 * TODO(emilia): we should really be storing the curves list in explicitly
463
 * parsed form instead. (However, this would affect binary compatibility
464
 * so cannot happen in the 1.0.x series.)
465
 */
466
static int tls1_get_curvelist(SSL *s, int sess,
467
                              const unsigned char **pcurves,
468
                              size_t *num_curves)
469
0
{
470
0
    size_t pcurveslen = 0;
471
0
    if (sess) {
472
0
        *pcurves = s->session->tlsext_ellipticcurvelist;
473
0
        pcurveslen = s->session->tlsext_ellipticcurvelist_length;
474
0
    } else {
475
        /* For Suite B mode only include P-256, P-384 */
476
0
        switch (tls1_suiteb(s)) {
477
0
        case SSL_CERT_FLAG_SUITEB_128_LOS:
478
0
            *pcurves = suiteb_curves;
479
0
            pcurveslen = sizeof(suiteb_curves);
480
0
            break;
481
482
0
        case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
483
0
            *pcurves = suiteb_curves;
484
0
            pcurveslen = 2;
485
0
            break;
486
487
0
        case SSL_CERT_FLAG_SUITEB_192_LOS:
488
0
            *pcurves = suiteb_curves + 2;
489
0
            pcurveslen = 2;
490
0
            break;
491
0
        default:
492
0
            *pcurves = s->tlsext_ellipticcurvelist;
493
0
            pcurveslen = s->tlsext_ellipticcurvelist_length;
494
0
        }
495
0
        if (!*pcurves) {
496
# ifdef OPENSSL_FIPS
497
            if (FIPS_mode()) {
498
                *pcurves = fips_curves_default;
499
                pcurveslen = sizeof(fips_curves_default);
500
            } else
501
# endif
502
0
            {
503
0
                if (!s->server || s->cert->ecdh_tmp_auto) {
504
0
                    *pcurves = eccurves_auto;
505
0
                    pcurveslen = sizeof(eccurves_auto);
506
0
                } else {
507
0
                    *pcurves = eccurves_all;
508
0
                    pcurveslen = sizeof(eccurves_all);
509
0
                }
510
0
            }
511
0
        }
512
0
    }
513
    /* We do not allow odd length arrays to enter the system. */
514
0
    if (pcurveslen & 1) {
515
0
        SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
516
0
        *num_curves = 0;
517
0
        return 0;
518
0
    } else {
519
0
        *num_curves = pcurveslen / 2;
520
0
        return 1;
521
0
    }
522
0
}
523
524
/* Check a curve is one of our preferences */
525
int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
526
0
{
527
0
    const unsigned char *curves;
528
0
    size_t num_curves, i;
529
0
    unsigned int suiteb_flags = tls1_suiteb(s);
530
0
    if (len != 3 || p[0] != NAMED_CURVE_TYPE)
531
0
        return 0;
532
    /* Check curve matches Suite B preferences */
533
0
    if (suiteb_flags) {
534
0
        unsigned long cid = s->s3->tmp.new_cipher->id;
535
0
        if (p[1])
536
0
            return 0;
537
0
        if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
538
0
            if (p[2] != TLSEXT_curve_P_256)
539
0
                return 0;
540
0
        } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
541
0
            if (p[2] != TLSEXT_curve_P_384)
542
0
                return 0;
543
0
        } else                  /* Should never happen */
544
0
            return 0;
545
0
    }
546
0
    if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
547
0
        return 0;
548
0
    for (i = 0; i < num_curves; i++, curves += 2) {
549
0
        if (p[1] == curves[0] && p[2] == curves[1])
550
0
            return 1;
551
0
    }
552
0
    return 0;
553
0
}
554
555
/*-
556
 * Return |nmatch|th shared curve or NID_undef if there is no match.
557
 * For nmatch == -1, return number of  matches
558
 * For nmatch == -2, return the NID of the curve to use for
559
 * an EC tmp key, or NID_undef if there is no match.
560
 */
561
int tls1_shared_curve(SSL *s, int nmatch)
562
0
{
563
0
    const unsigned char *pref, *supp;
564
0
    size_t num_pref, num_supp, i, j;
565
0
    int k;
566
    /* Can't do anything on client side */
567
0
    if (s->server == 0)
568
0
        return -1;
569
0
    if (nmatch == -2) {
570
0
        if (tls1_suiteb(s)) {
571
            /*
572
             * For Suite B ciphersuite determines curve: we already know
573
             * these are acceptable due to previous checks.
574
             */
575
0
            unsigned long cid = s->s3->tmp.new_cipher->id;
576
0
            if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
577
0
                return NID_X9_62_prime256v1; /* P-256 */
578
0
            if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
579
0
                return NID_secp384r1; /* P-384 */
580
            /* Should never happen */
581
0
            return NID_undef;
582
0
        }
583
        /* If not Suite B just return first preference shared curve */
584
0
        nmatch = 0;
585
0
    }
586
    /*
587
     * Avoid truncation. tls1_get_curvelist takes an int
588
     * but s->options is a long...
589
     */
590
0
    if (!tls1_get_curvelist
591
0
        (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
592
0
         &num_supp))
593
        /* In practice, NID_undef == 0 but let's be precise. */
594
0
        return nmatch == -1 ? 0 : NID_undef;
595
0
    if (!tls1_get_curvelist
596
0
        (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
597
0
         &num_pref))
598
0
        return nmatch == -1 ? 0 : NID_undef;
599
600
    /*
601
     * If the client didn't send the elliptic_curves extension all of them
602
     * are allowed.
603
     */
604
0
    if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
605
0
        supp = eccurves_all;
606
0
        num_supp = sizeof(eccurves_all) / 2;
607
0
    } else if (num_pref == 0 &&
608
0
        (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
609
0
        pref = eccurves_all;
610
0
        num_pref = sizeof(eccurves_all) / 2;
611
0
    }
612
613
0
    k = 0;
614
0
    for (i = 0; i < num_pref; i++, pref += 2) {
615
0
        const unsigned char *tsupp = supp;
616
0
        for (j = 0; j < num_supp; j++, tsupp += 2) {
617
0
            if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
618
0
                if (nmatch == k) {
619
0
                    int id = (pref[0] << 8) | pref[1];
620
0
                    return tls1_ec_curve_id2nid(id);
621
0
                }
622
0
                k++;
623
0
            }
624
0
        }
625
0
    }
626
0
    if (nmatch == -1)
627
0
        return k;
628
    /* Out of range (nmatch > k). */
629
0
    return NID_undef;
630
0
}
631
632
int tls1_set_curves(unsigned char **pext, size_t *pextlen,
633
                    int *curves, size_t ncurves)
634
0
{
635
0
    unsigned char *clist, *p;
636
0
    size_t i;
637
    /*
638
     * Bitmap of curves included to detect duplicates: only works while curve
639
     * ids < 32
640
     */
641
0
    unsigned long dup_list = 0;
642
# ifdef OPENSSL_NO_EC2M
643
    EC_GROUP *curve;
644
# endif
645
646
0
    clist = OPENSSL_malloc(ncurves * 2);
647
0
    if (!clist)
648
0
        return 0;
649
0
    for (i = 0, p = clist; i < ncurves; i++) {
650
0
        unsigned long idmask;
651
0
        int id;
652
0
        id = tls1_ec_nid2curve_id(curves[i]);
653
# ifdef OPENSSL_FIPS
654
        /* NB: 25 is last curve ID supported by FIPS module */
655
        if (FIPS_mode() && id > 25) {
656
            OPENSSL_free(clist);
657
            return 0;
658
        }
659
# endif
660
# ifdef OPENSSL_NO_EC2M
661
        curve = EC_GROUP_new_by_curve_name(curves[i]);
662
        if (!curve || EC_METHOD_get_field_type(EC_GROUP_method_of(curve))
663
            == NID_X9_62_characteristic_two_field) {
664
            if (curve)
665
                EC_GROUP_free(curve);
666
            OPENSSL_free(clist);
667
            return 0;
668
        } else
669
            EC_GROUP_free(curve);
670
# endif
671
0
        idmask = 1L << id;
672
0
        if (!id || (dup_list & idmask)) {
673
0
            OPENSSL_free(clist);
674
0
            return 0;
675
0
        }
676
0
        dup_list |= idmask;
677
0
        s2n(id, p);
678
0
    }
679
0
    if (*pext)
680
0
        OPENSSL_free(*pext);
681
0
    *pext = clist;
682
0
    *pextlen = ncurves * 2;
683
0
    return 1;
684
0
}
685
686
0
# define MAX_CURVELIST   28
687
688
typedef struct {
689
    size_t nidcnt;
690
    int nid_arr[MAX_CURVELIST];
691
} nid_cb_st;
692
693
static int nid_cb(const char *elem, int len, void *arg)
694
0
{
695
0
    nid_cb_st *narg = arg;
696
0
    size_t i;
697
0
    int nid;
698
0
    char etmp[20];
699
0
    if (elem == NULL)
700
0
        return 0;
701
0
    if (narg->nidcnt == MAX_CURVELIST)
702
0
        return 0;
703
0
    if (len > (int)(sizeof(etmp) - 1))
704
0
        return 0;
705
0
    memcpy(etmp, elem, len);
706
0
    etmp[len] = 0;
707
0
    nid = EC_curve_nist2nid(etmp);
708
0
    if (nid == NID_undef)
709
0
        nid = OBJ_sn2nid(etmp);
710
0
    if (nid == NID_undef)
711
0
        nid = OBJ_ln2nid(etmp);
712
0
    if (nid == NID_undef)
713
0
        return 0;
714
0
    for (i = 0; i < narg->nidcnt; i++)
715
0
        if (narg->nid_arr[i] == nid)
716
0
            return 0;
717
0
    narg->nid_arr[narg->nidcnt++] = nid;
718
0
    return 1;
719
0
}
720
721
/* Set curves based on a colon separate list */
722
int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
723
                         const char *str)
724
0
{
725
0
    nid_cb_st ncb;
726
0
    ncb.nidcnt = 0;
727
0
    if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
728
0
        return 0;
729
0
    if (pext == NULL)
730
0
        return 1;
731
0
    return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
732
0
}
733
734
/* For an EC key set TLS id and required compression based on parameters */
735
static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
736
                          EC_KEY *ec)
737
0
{
738
0
    int is_prime, id;
739
0
    const EC_GROUP *grp;
740
0
    const EC_METHOD *meth;
741
0
    if (!ec)
742
0
        return 0;
743
    /* Determine if it is a prime field */
744
0
    grp = EC_KEY_get0_group(ec);
745
0
    if (!grp)
746
0
        return 0;
747
0
    meth = EC_GROUP_method_of(grp);
748
0
    if (!meth)
749
0
        return 0;
750
0
    if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
751
0
        is_prime = 1;
752
0
    else
753
0
        is_prime = 0;
754
    /* Determine curve ID */
755
0
    id = EC_GROUP_get_curve_name(grp);
756
0
    id = tls1_ec_nid2curve_id(id);
757
    /* If we have an ID set it, otherwise set arbitrary explicit curve */
758
0
    if (id) {
759
0
        curve_id[0] = 0;
760
0
        curve_id[1] = (unsigned char)id;
761
0
    } else {
762
0
        curve_id[0] = 0xff;
763
0
        if (is_prime)
764
0
            curve_id[1] = 0x01;
765
0
        else
766
0
            curve_id[1] = 0x02;
767
0
    }
768
0
    if (comp_id) {
769
0
        if (EC_KEY_get0_public_key(ec) == NULL)
770
0
            return 0;
771
0
        if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
772
0
            if (is_prime)
773
0
                *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
774
0
            else
775
0
                *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
776
0
        } else
777
0
            *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
778
0
    }
779
0
    return 1;
780
0
}
781
782
/* Check an EC key is compatible with extensions */
783
static int tls1_check_ec_key(SSL *s,
784
                             unsigned char *curve_id, unsigned char *comp_id)
785
0
{
786
0
    const unsigned char *pformats, *pcurves;
787
0
    size_t num_formats, num_curves, i;
788
0
    int j;
789
    /*
790
     * If point formats extension present check it, otherwise everything is
791
     * supported (see RFC4492).
792
     */
793
0
    if (comp_id && s->session->tlsext_ecpointformatlist) {
794
0
        pformats = s->session->tlsext_ecpointformatlist;
795
0
        num_formats = s->session->tlsext_ecpointformatlist_length;
796
0
        for (i = 0; i < num_formats; i++, pformats++) {
797
0
            if (*comp_id == *pformats)
798
0
                break;
799
0
        }
800
0
        if (i == num_formats)
801
0
            return 0;
802
0
    }
803
0
    if (!curve_id)
804
0
        return 1;
805
    /* Check curve is consistent with client and server preferences */
806
0
    for (j = 0; j <= 1; j++) {
807
0
        if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
808
0
            return 0;
809
0
        if (j == 1 && num_curves == 0) {
810
            /*
811
             * If we've not received any curves then skip this check.
812
             * RFC 4492 does not require the supported elliptic curves extension
813
             * so if it is not sent we can just choose any curve.
814
             * It is invalid to send an empty list in the elliptic curves
815
             * extension, so num_curves == 0 always means no extension.
816
             */
817
0
            break;
818
0
        }
819
0
        for (i = 0; i < num_curves; i++, pcurves += 2) {
820
0
            if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
821
0
                break;
822
0
        }
823
0
        if (i == num_curves)
824
0
            return 0;
825
        /* For clients can only check sent curve list */
826
0
        if (!s->server)
827
0
            return 1;
828
0
    }
829
0
    return 1;
830
0
}
831
832
static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
833
                                size_t *num_formats)
834
0
{
835
    /*
836
     * If we have a custom point format list use it otherwise use default
837
     */
838
0
    if (s->tlsext_ecpointformatlist) {
839
0
        *pformats = s->tlsext_ecpointformatlist;
840
0
        *num_formats = s->tlsext_ecpointformatlist_length;
841
0
    } else {
842
0
        *pformats = ecformats_default;
843
        /* For Suite B we don't support char2 fields */
844
0
        if (tls1_suiteb(s))
845
0
            *num_formats = sizeof(ecformats_default) - 1;
846
0
        else
847
0
            *num_formats = sizeof(ecformats_default);
848
0
    }
849
0
}
850
851
/*
852
 * Check cert parameters compatible with extensions: currently just checks EC
853
 * certificates have compatible curves and compression.
854
 */
855
static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
856
0
{
857
0
    unsigned char comp_id, curve_id[2];
858
0
    EVP_PKEY *pkey;
859
0
    int rv;
860
0
    pkey = X509_get_pubkey(x);
861
0
    if (!pkey)
862
0
        return 0;
863
    /* If not EC nothing to do */
864
0
    if (pkey->type != EVP_PKEY_EC) {
865
0
        EVP_PKEY_free(pkey);
866
0
        return 1;
867
0
    }
868
0
    rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
869
0
    EVP_PKEY_free(pkey);
870
0
    if (!rv)
871
0
        return 0;
872
    /*
873
     * Can't check curve_id for client certs as we don't have a supported
874
     * curves extension.
875
     */
876
0
    rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
877
0
    if (!rv)
878
0
        return 0;
879
    /*
880
     * Special case for suite B. We *MUST* sign using SHA256+P-256 or
881
     * SHA384+P-384, adjust digest if necessary.
882
     */
883
0
    if (set_ee_md && tls1_suiteb(s)) {
884
0
        int check_md;
885
0
        size_t i;
886
0
        CERT *c = s->cert;
887
0
        if (curve_id[0])
888
0
            return 0;
889
        /* Check to see we have necessary signing algorithm */
890
0
        if (curve_id[1] == TLSEXT_curve_P_256)
891
0
            check_md = NID_ecdsa_with_SHA256;
892
0
        else if (curve_id[1] == TLSEXT_curve_P_384)
893
0
            check_md = NID_ecdsa_with_SHA384;
894
0
        else
895
0
            return 0;           /* Should never happen */
896
0
        for (i = 0; i < c->shared_sigalgslen; i++)
897
0
            if (check_md == c->shared_sigalgs[i].signandhash_nid)
898
0
                break;
899
0
        if (i == c->shared_sigalgslen)
900
0
            return 0;
901
0
        if (set_ee_md == 2) {
902
0
            if (check_md == NID_ecdsa_with_SHA256)
903
0
                c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
904
0
            else
905
0
                c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
906
0
        }
907
0
    }
908
0
    return rv;
909
0
}
910
911
# ifndef OPENSSL_NO_ECDH
912
/* Check EC temporary key is compatible with client extensions */
913
int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
914
0
{
915
0
    unsigned char curve_id[2];
916
0
    EC_KEY *ec = s->cert->ecdh_tmp;
917
#  ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
918
    /* Allow any curve: not just those peer supports */
919
    if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
920
        return 1;
921
#  endif
922
    /*
923
     * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
924
     * curves permitted.
925
     */
926
0
    if (tls1_suiteb(s)) {
927
        /* Curve to check determined by ciphersuite */
928
0
        if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
929
0
            curve_id[1] = TLSEXT_curve_P_256;
930
0
        else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
931
0
            curve_id[1] = TLSEXT_curve_P_384;
932
0
        else
933
0
            return 0;
934
0
        curve_id[0] = 0;
935
        /* Check this curve is acceptable */
936
0
        if (!tls1_check_ec_key(s, curve_id, NULL))
937
0
            return 0;
938
        /* If auto or setting curve from callback assume OK */
939
0
        if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
940
0
            return 1;
941
        /* Otherwise check curve is acceptable */
942
0
        else {
943
0
            unsigned char curve_tmp[2];
944
0
            if (!ec)
945
0
                return 0;
946
0
            if (!tls1_set_ec_id(curve_tmp, NULL, ec))
947
0
                return 0;
948
0
            if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
949
0
                return 1;
950
0
            return 0;
951
0
        }
952
953
0
    }
954
0
    if (s->cert->ecdh_tmp_auto) {
955
        /* Need a shared curve */
956
0
        if (tls1_shared_curve(s, 0))
957
0
            return 1;
958
0
        else
959
0
            return 0;
960
0
    }
961
0
    if (!ec) {
962
0
        if (s->cert->ecdh_tmp_cb)
963
0
            return 1;
964
0
        else
965
0
            return 0;
966
0
    }
967
0
    if (!tls1_set_ec_id(curve_id, NULL, ec))
968
0
        return 0;
969
/* Set this to allow use of invalid curves for testing */
970
#  if 0
971
    return 1;
972
#  else
973
0
    return tls1_check_ec_key(s, curve_id, NULL);
974
0
#  endif
975
0
}
976
# endif                         /* OPENSSL_NO_ECDH */
977
978
#else
979
980
static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
981
{
982
    return 1;
983
}
984
985
#endif                          /* OPENSSL_NO_EC */
986
987
#ifndef OPENSSL_NO_TLSEXT
988
989
/*
990
 * List of supported signature algorithms and hashes. Should make this
991
 * customisable at some point, for now include everything we support.
992
 */
993
994
# ifdef OPENSSL_NO_RSA
995
#  define tlsext_sigalg_rsa(md) /* */
996
# else
997
#  define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
998
# endif
999
1000
# ifdef OPENSSL_NO_DSA
1001
#  define tlsext_sigalg_dsa(md) /* */
1002
# else
1003
#  define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
1004
# endif
1005
1006
# ifdef OPENSSL_NO_ECDSA
1007
#  define tlsext_sigalg_ecdsa(md)
1008
                                /* */
1009
# else
1010
#  define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
1011
# endif
1012
1013
# define tlsext_sigalg(md) \
1014
                tlsext_sigalg_rsa(md) \
1015
                tlsext_sigalg_dsa(md) \
1016
                tlsext_sigalg_ecdsa(md)
1017
1018
static unsigned char tls12_sigalgs[] = {
1019
# ifndef OPENSSL_NO_SHA512
1020
    tlsext_sigalg(TLSEXT_hash_sha512)
1021
        tlsext_sigalg(TLSEXT_hash_sha384)
1022
# endif
1023
# ifndef OPENSSL_NO_SHA256
1024
        tlsext_sigalg(TLSEXT_hash_sha256)
1025
        tlsext_sigalg(TLSEXT_hash_sha224)
1026
# endif
1027
# ifndef OPENSSL_NO_SHA
1028
        tlsext_sigalg(TLSEXT_hash_sha1)
1029
# endif
1030
};
1031
1032
# ifndef OPENSSL_NO_ECDSA
1033
static unsigned char suiteb_sigalgs[] = {
1034
    tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
1035
        tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
1036
};
1037
# endif
1038
size_t tls12_get_psigalgs(SSL *s, int sent, const unsigned char **psigs)
1039
0
{
1040
    /*
1041
     * If Suite B mode use Suite B sigalgs only, ignore any other
1042
     * preferences.
1043
     */
1044
0
# ifndef OPENSSL_NO_EC
1045
0
    switch (tls1_suiteb(s)) {
1046
0
    case SSL_CERT_FLAG_SUITEB_128_LOS:
1047
0
        *psigs = suiteb_sigalgs;
1048
0
        return sizeof(suiteb_sigalgs);
1049
1050
0
    case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1051
0
        *psigs = suiteb_sigalgs;
1052
0
        return 2;
1053
1054
0
    case SSL_CERT_FLAG_SUITEB_192_LOS:
1055
0
        *psigs = suiteb_sigalgs + 2;
1056
0
        return 2;
1057
0
    }
1058
0
# endif
1059
    /* If server use client authentication sigalgs if not NULL */
1060
0
    if (s->server == sent && s->cert->client_sigalgs) {
1061
0
        *psigs = s->cert->client_sigalgs;
1062
0
        return s->cert->client_sigalgslen;
1063
0
    } else if (s->cert->conf_sigalgs) {
1064
0
        *psigs = s->cert->conf_sigalgs;
1065
0
        return s->cert->conf_sigalgslen;
1066
0
    } else {
1067
0
        *psigs = tls12_sigalgs;
1068
0
        return sizeof(tls12_sigalgs);
1069
0
    }
1070
0
}
1071
1072
/*
1073
 * Check signature algorithm is consistent with sent supported signature
1074
 * algorithms and if so return relevant digest.
1075
 */
1076
int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
1077
                            const unsigned char *sig, EVP_PKEY *pkey)
1078
0
{
1079
0
    const unsigned char *sent_sigs;
1080
0
    size_t sent_sigslen, i;
1081
0
    int sigalg = tls12_get_sigid(pkey);
1082
    /* Should never happen */
1083
0
    if (sigalg == -1)
1084
0
        return -1;
1085
    /* Check key type is consistent with signature */
1086
0
    if (sigalg != (int)sig[1]) {
1087
0
        SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1088
0
        return 0;
1089
0
    }
1090
0
# ifndef OPENSSL_NO_EC
1091
0
    if (pkey->type == EVP_PKEY_EC) {
1092
0
        unsigned char curve_id[2], comp_id;
1093
        /* Check compression and curve matches extensions */
1094
0
        if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1095
0
            return 0;
1096
0
        if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
1097
0
            SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1098
0
            return 0;
1099
0
        }
1100
        /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1101
0
        if (tls1_suiteb(s)) {
1102
0
            if (curve_id[0])
1103
0
                return 0;
1104
0
            if (curve_id[1] == TLSEXT_curve_P_256) {
1105
0
                if (sig[0] != TLSEXT_hash_sha256) {
1106
0
                    SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1107
0
                           SSL_R_ILLEGAL_SUITEB_DIGEST);
1108
0
                    return 0;
1109
0
                }
1110
0
            } else if (curve_id[1] == TLSEXT_curve_P_384) {
1111
0
                if (sig[0] != TLSEXT_hash_sha384) {
1112
0
                    SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1113
0
                           SSL_R_ILLEGAL_SUITEB_DIGEST);
1114
0
                    return 0;
1115
0
                }
1116
0
            } else
1117
0
                return 0;
1118
0
        }
1119
0
    } else if (tls1_suiteb(s))
1120
0
        return 0;
1121
0
# endif
1122
1123
    /* Check signature matches a type we sent */
1124
0
    sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1125
0
    for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
1126
0
        if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1127
0
            break;
1128
0
    }
1129
    /* Allow fallback to SHA1 if not strict mode */
1130
0
    if (i == sent_sigslen
1131
0
        && (sig[0] != TLSEXT_hash_sha1
1132
0
            || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1133
0
        SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1134
0
        return 0;
1135
0
    }
1136
0
    *pmd = tls12_get_hash(sig[0]);
1137
0
    if (*pmd == NULL) {
1138
0
        SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
1139
0
        return 0;
1140
0
    }
1141
    /*
1142
     * Store the digest used so applications can retrieve it if they wish.
1143
     */
1144
0
    if (s->session && s->session->sess_cert)
1145
0
        s->session->sess_cert->peer_key->digest = *pmd;
1146
0
    return 1;
1147
0
}
1148
1149
/*
1150
 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
1151
 * supported or doesn't appear in supported signature algorithms. Unlike
1152
 * ssl_cipher_get_disabled this applies to a specific session and not global
1153
 * settings.
1154
 */
1155
void ssl_set_client_disabled(SSL *s)
1156
0
{
1157
0
    CERT *c = s->cert;
1158
0
    const unsigned char *sigalgs;
1159
0
    size_t i, sigalgslen;
1160
0
    int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1161
0
    c->mask_a = 0;
1162
0
    c->mask_k = 0;
1163
    /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1164
0
    if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1165
0
        c->mask_ssl = SSL_TLSV1_2;
1166
0
    else
1167
0
        c->mask_ssl = 0;
1168
    /*
1169
     * Now go through all signature algorithms seeing if we support any for
1170
     * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2.
1171
     */
1172
0
    sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
1173
0
    for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
1174
0
        switch (sigalgs[1]) {
1175
0
# ifndef OPENSSL_NO_RSA
1176
0
        case TLSEXT_signature_rsa:
1177
0
            have_rsa = 1;
1178
0
            break;
1179
0
# endif
1180
0
# ifndef OPENSSL_NO_DSA
1181
0
        case TLSEXT_signature_dsa:
1182
0
            have_dsa = 1;
1183
0
            break;
1184
0
# endif
1185
0
# ifndef OPENSSL_NO_ECDSA
1186
0
        case TLSEXT_signature_ecdsa:
1187
0
            have_ecdsa = 1;
1188
0
            break;
1189
0
# endif
1190
0
        }
1191
0
    }
1192
    /*
1193
     * Disable auth and static DH if we don't include any appropriate
1194
     * signature algorithms.
1195
     */
1196
0
    if (!have_rsa) {
1197
0
        c->mask_a |= SSL_aRSA;
1198
0
        c->mask_k |= SSL_kDHr | SSL_kECDHr;
1199
0
    }
1200
0
    if (!have_dsa) {
1201
0
        c->mask_a |= SSL_aDSS;
1202
0
        c->mask_k |= SSL_kDHd;
1203
0
    }
1204
0
    if (!have_ecdsa) {
1205
0
        c->mask_a |= SSL_aECDSA;
1206
0
        c->mask_k |= SSL_kECDHe;
1207
0
    }
1208
# ifndef OPENSSL_NO_KRB5
1209
    if (!kssl_tgt_is_available(s->kssl_ctx)) {
1210
        c->mask_a |= SSL_aKRB5;
1211
        c->mask_k |= SSL_kKRB5;
1212
    }
1213
# endif
1214
0
# ifndef OPENSSL_NO_PSK
1215
    /* with PSK there must be client callback set */
1216
0
    if (!s->psk_client_callback) {
1217
0
        c->mask_a |= SSL_aPSK;
1218
0
        c->mask_k |= SSL_kPSK;
1219
0
    }
1220
0
# endif                         /* OPENSSL_NO_PSK */
1221
0
# ifndef OPENSSL_NO_SRP
1222
0
    if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1223
0
        c->mask_a |= SSL_aSRP;
1224
0
        c->mask_k |= SSL_kSRP;
1225
0
    }
1226
0
# endif
1227
0
    c->valid = 1;
1228
0
}
1229
1230
unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1231
                                          unsigned char *limit, int *al)
1232
0
{
1233
0
    int extdatalen = 0;
1234
0
    unsigned char *orig = buf;
1235
0
    unsigned char *ret = buf;
1236
0
# ifndef OPENSSL_NO_EC
1237
    /* See if we support any ECC ciphersuites */
1238
0
    int using_ecc = 0;
1239
0
    if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1240
0
        int i;
1241
0
        unsigned long alg_k, alg_a;
1242
0
        STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1243
1244
0
        for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1245
0
            SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1246
1247
0
            alg_k = c->algorithm_mkey;
1248
0
            alg_a = c->algorithm_auth;
1249
0
            if ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)
1250
0
                 || (alg_a & SSL_aECDSA))) {
1251
0
                using_ecc = 1;
1252
0
                break;
1253
0
            }
1254
0
        }
1255
0
    }
1256
0
# endif
1257
1258
    /* don't add extensions for SSLv3 unless doing secure renegotiation */
1259
0
    if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding)
1260
0
        return orig;
1261
1262
0
    ret += 2;
1263
1264
0
    if (ret >= limit)
1265
0
        return NULL;            /* this really never occurs, but ... */
1266
1267
0
    if (s->tlsext_hostname != NULL) {
1268
        /* Add TLS extension servername to the Client Hello message */
1269
0
        size_t size_str;
1270
1271
        /*-
1272
         * check for enough space.
1273
         * 4 for the servername type and entension length
1274
         * 2 for servernamelist length
1275
         * 1 for the hostname type
1276
         * 2 for hostname length
1277
         * + hostname length
1278
         */
1279
0
        size_str = strlen(s->tlsext_hostname);
1280
0
        if (CHECKLEN(ret, 9 + size_str, limit))
1281
0
            return NULL;
1282
1283
        /* extension type and length */
1284
0
        s2n(TLSEXT_TYPE_server_name, ret);
1285
0
        s2n(size_str + 5, ret);
1286
1287
        /* length of servername list */
1288
0
        s2n(size_str + 3, ret);
1289
1290
        /* hostname type, length and hostname */
1291
0
        *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1292
0
        s2n(size_str, ret);
1293
0
        memcpy(ret, s->tlsext_hostname, size_str);
1294
0
        ret += size_str;
1295
0
    }
1296
1297
    /* Add RI if renegotiating */
1298
0
    if (s->renegotiate) {
1299
0
        int el;
1300
1301
0
        if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1302
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1303
0
            return NULL;
1304
0
        }
1305
1306
0
        if ((limit - ret - 4 - el) < 0)
1307
0
            return NULL;
1308
1309
0
        s2n(TLSEXT_TYPE_renegotiate, ret);
1310
0
        s2n(el, ret);
1311
1312
0
        if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1313
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1314
0
            return NULL;
1315
0
        }
1316
1317
0
        ret += el;
1318
0
    }
1319
0
# ifndef OPENSSL_NO_SRP
1320
    /* Add SRP username if there is one */
1321
0
    if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1322
                                     * Client Hello message */
1323
1324
0
        size_t login_len = strlen(s->srp_ctx.login);
1325
0
        if (login_len > 255 || login_len == 0) {
1326
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1327
0
            return NULL;
1328
0
        }
1329
1330
        /*-
1331
         * check for enough space.
1332
         * 4 for the srp type type and entension length
1333
         * 1 for the srp user identity
1334
         * + srp user identity length
1335
         */
1336
0
        if (CHECKLEN(ret, 5 + login_len, limit))
1337
0
            return NULL;
1338
1339
        /* fill in the extension */
1340
0
        s2n(TLSEXT_TYPE_srp, ret);
1341
0
        s2n(login_len + 1, ret);
1342
0
        (*ret++) = (unsigned char)login_len;
1343
0
        memcpy(ret, s->srp_ctx.login, login_len);
1344
0
        ret += login_len;
1345
0
    }
1346
0
# endif
1347
1348
0
# ifndef OPENSSL_NO_EC
1349
0
    if (using_ecc) {
1350
        /*
1351
         * Add TLS extension ECPointFormats to the ClientHello message
1352
         */
1353
0
        const unsigned char *pcurves, *pformats;
1354
0
        size_t num_curves, num_formats, curves_list_len;
1355
1356
0
        tls1_get_formatlist(s, &pformats, &num_formats);
1357
1358
0
        if (num_formats > 255) {
1359
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1360
0
            return NULL;
1361
0
        }
1362
        /*-
1363
         * check for enough space.
1364
         * 4 bytes for the ec point formats type and extension length
1365
         * 1 byte for the length of the formats
1366
         * + formats length
1367
         */
1368
0
        if (CHECKLEN(ret, 5 + num_formats, limit))
1369
0
            return NULL;
1370
1371
0
        s2n(TLSEXT_TYPE_ec_point_formats, ret);
1372
        /* The point format list has 1-byte length. */
1373
0
        s2n(num_formats + 1, ret);
1374
0
        *(ret++) = (unsigned char)num_formats;
1375
0
        memcpy(ret, pformats, num_formats);
1376
0
        ret += num_formats;
1377
1378
        /*
1379
         * Add TLS extension EllipticCurves to the ClientHello message
1380
         */
1381
0
        pcurves = s->tlsext_ellipticcurvelist;
1382
0
        if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1383
0
            return NULL;
1384
1385
0
        if (num_curves > 65532 / 2) {
1386
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1387
0
            return NULL;
1388
0
        }
1389
0
        curves_list_len = 2 * num_curves;
1390
        /*-
1391
         * check for enough space.
1392
         * 4 bytes for the ec curves type and extension length
1393
         * 2 bytes for the curve list length
1394
         * + curve list length
1395
         */
1396
0
        if (CHECKLEN(ret, 6 + curves_list_len, limit))
1397
0
            return NULL;
1398
1399
0
        s2n(TLSEXT_TYPE_elliptic_curves, ret);
1400
0
        s2n(curves_list_len + 2, ret);
1401
0
        s2n(curves_list_len, ret);
1402
0
        memcpy(ret, pcurves, curves_list_len);
1403
0
        ret += curves_list_len;
1404
0
    }
1405
0
# endif                         /* OPENSSL_NO_EC */
1406
1407
0
    if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1408
0
        size_t ticklen;
1409
0
        if (!s->new_session && s->session && s->session->tlsext_tick)
1410
0
            ticklen = s->session->tlsext_ticklen;
1411
0
        else if (s->session && s->tlsext_session_ticket &&
1412
0
                 s->tlsext_session_ticket->data) {
1413
0
            ticklen = s->tlsext_session_ticket->length;
1414
0
            s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1415
0
            if (!s->session->tlsext_tick)
1416
0
                return NULL;
1417
0
            memcpy(s->session->tlsext_tick,
1418
0
                   s->tlsext_session_ticket->data, ticklen);
1419
0
            s->session->tlsext_ticklen = ticklen;
1420
0
        } else
1421
0
            ticklen = 0;
1422
0
        if (ticklen == 0 && s->tlsext_session_ticket &&
1423
0
            s->tlsext_session_ticket->data == NULL)
1424
0
            goto skip_ext;
1425
        /*
1426
         * Check for enough room 2 for extension type, 2 for len rest for
1427
         * ticket
1428
         */
1429
0
        if (CHECKLEN(ret, 4 + ticklen, limit))
1430
0
            return NULL;
1431
0
        s2n(TLSEXT_TYPE_session_ticket, ret);
1432
0
        s2n(ticklen, ret);
1433
0
        if (ticklen > 0) {
1434
0
            memcpy(ret, s->session->tlsext_tick, ticklen);
1435
0
            ret += ticklen;
1436
0
        }
1437
0
    }
1438
0
 skip_ext:
1439
1440
0
    if (SSL_CLIENT_USE_SIGALGS(s)) {
1441
0
        size_t salglen;
1442
0
        const unsigned char *salg;
1443
0
        salglen = tls12_get_psigalgs(s, 1, &salg);
1444
1445
        /*-
1446
         * check for enough space.
1447
         * 4 bytes for the sigalgs type and extension length
1448
         * 2 bytes for the sigalg list length
1449
         * + sigalg list length
1450
         */
1451
0
        if (CHECKLEN(ret, salglen + 6, limit))
1452
0
            return NULL;
1453
0
        s2n(TLSEXT_TYPE_signature_algorithms, ret);
1454
0
        s2n(salglen + 2, ret);
1455
0
        s2n(salglen, ret);
1456
0
        memcpy(ret, salg, salglen);
1457
0
        ret += salglen;
1458
0
    }
1459
# ifdef TLSEXT_TYPE_opaque_prf_input
1460
    if (s->s3->client_opaque_prf_input != NULL) {
1461
        size_t col = s->s3->client_opaque_prf_input_len;
1462
1463
        if ((long)(limit - ret - 6 - col < 0))
1464
            return NULL;
1465
        if (col > 0xFFFD)       /* can't happen */
1466
            return NULL;
1467
1468
        s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1469
        s2n(col + 2, ret);
1470
        s2n(col, ret);
1471
        memcpy(ret, s->s3->client_opaque_prf_input, col);
1472
        ret += col;
1473
    }
1474
# endif
1475
1476
0
    if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1477
0
        int i;
1478
0
        size_t extlen, idlen;
1479
0
        int lentmp;
1480
0
        OCSP_RESPID *id;
1481
1482
0
        idlen = 0;
1483
0
        for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1484
0
            id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1485
0
            lentmp = i2d_OCSP_RESPID(id, NULL);
1486
0
            if (lentmp <= 0)
1487
0
                return NULL;
1488
0
            idlen += (size_t)lentmp + 2;
1489
0
        }
1490
1491
0
        if (s->tlsext_ocsp_exts) {
1492
0
            lentmp = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1493
0
            if (lentmp < 0)
1494
0
                return NULL;
1495
0
            extlen = (size_t)lentmp;
1496
0
        } else
1497
0
            extlen = 0;
1498
1499
0
        if (extlen + idlen > 0xFFF0)
1500
0
            return NULL;
1501
        /*
1502
         * 2 bytes for status request type
1503
         * 2 bytes for status request len
1504
         * 1 byte for OCSP request type
1505
         * 2 bytes for length of ids
1506
         * 2 bytes for length of extensions
1507
         * + length of ids
1508
         * + length of extensions
1509
         */
1510
0
        if (CHECKLEN(ret, 9 + idlen + extlen, limit))
1511
0
            return NULL;
1512
1513
0
        s2n(TLSEXT_TYPE_status_request, ret);
1514
0
        s2n(extlen + idlen + 5, ret);
1515
0
        *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1516
0
        s2n(idlen, ret);
1517
0
        for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1518
            /* save position of id len */
1519
0
            unsigned char *q = ret;
1520
0
            id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1521
            /* skip over id len */
1522
0
            ret += 2;
1523
0
            lentmp = i2d_OCSP_RESPID(id, &ret);
1524
            /* write id len */
1525
0
            s2n(lentmp, q);
1526
0
        }
1527
0
        s2n(extlen, ret);
1528
0
        if (extlen > 0)
1529
0
            i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1530
0
    }
1531
0
# ifndef OPENSSL_NO_HEARTBEATS
1532
    /* Add Heartbeat extension */
1533
1534
    /*-
1535
     * check for enough space.
1536
     * 4 bytes for the heartbeat ext type and extension length
1537
     * 1 byte for the mode
1538
     */
1539
0
    if (CHECKLEN(ret, 5, limit))
1540
0
        return NULL;
1541
1542
0
    s2n(TLSEXT_TYPE_heartbeat, ret);
1543
0
    s2n(1, ret);
1544
    /*-
1545
     * Set mode:
1546
     * 1: peer may send requests
1547
     * 2: peer not allowed to send requests
1548
     */
1549
0
    if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1550
0
        *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1551
0
    else
1552
0
        *(ret++) = SSL_TLSEXT_HB_ENABLED;
1553
0
# endif
1554
1555
0
# ifndef OPENSSL_NO_NEXTPROTONEG
1556
0
    if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1557
        /*
1558
         * The client advertises an emtpy extension to indicate its support
1559
         * for Next Protocol Negotiation
1560
         */
1561
1562
        /*-
1563
         * check for enough space.
1564
         * 4 bytes for the NPN ext type and extension length
1565
         */
1566
0
        if (CHECKLEN(ret, 4, limit))
1567
0
            return NULL;
1568
0
        s2n(TLSEXT_TYPE_next_proto_neg, ret);
1569
0
        s2n(0, ret);
1570
0
    }
1571
0
# endif
1572
1573
0
    if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1574
        /*-
1575
         * check for enough space.
1576
         * 4 bytes for the ALPN type and extension length
1577
         * 2 bytes for the ALPN protocol list length
1578
         * + ALPN protocol list length
1579
         */
1580
0
        if (CHECKLEN(ret, 6 + s->alpn_client_proto_list_len, limit))
1581
0
            return NULL;
1582
0
        s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1583
0
        s2n(2 + s->alpn_client_proto_list_len, ret);
1584
0
        s2n(s->alpn_client_proto_list_len, ret);
1585
0
        memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1586
0
        ret += s->alpn_client_proto_list_len;
1587
0
        s->cert->alpn_sent = 1;
1588
0
    }
1589
0
# ifndef OPENSSL_NO_SRTP
1590
0
    if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1591
0
        int el;
1592
1593
0
        ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1594
1595
        /*-
1596
         * check for enough space.
1597
         * 4 bytes for the SRTP type and extension length
1598
         * + SRTP profiles length
1599
         */
1600
0
        if (CHECKLEN(ret, 4 + el, limit))
1601
0
            return NULL;
1602
1603
0
        s2n(TLSEXT_TYPE_use_srtp, ret);
1604
0
        s2n(el, ret);
1605
1606
0
        if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1607
0
            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1608
0
            return NULL;
1609
0
        }
1610
0
        ret += el;
1611
0
    }
1612
0
# endif
1613
0
    custom_ext_init(&s->cert->cli_ext);
1614
    /* Add custom TLS Extensions to ClientHello */
1615
0
    if (!custom_ext_add(s, 0, &ret, limit, al))
1616
0
        return NULL;
1617
1618
    /*
1619
     * Add padding to workaround bugs in F5 terminators. See
1620
     * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1621
     * code works out the length of all existing extensions it MUST always
1622
     * appear last.
1623
     */
1624
0
    if (s->options & SSL_OP_TLSEXT_PADDING) {
1625
0
        int hlen = ret - (unsigned char *)s->init_buf->data;
1626
        /*
1627
         * The code in s23_clnt.c to build ClientHello messages includes the
1628
         * 5-byte record header in the buffer, while the code in s3_clnt.c
1629
         * does not.
1630
         */
1631
0
        if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
1632
0
            hlen -= 5;
1633
0
        if (hlen > 0xff && hlen < 0x200) {
1634
0
            hlen = 0x200 - hlen;
1635
0
            if (hlen >= 4)
1636
0
                hlen -= 4;
1637
0
            else
1638
0
                hlen = 0;
1639
1640
            /*-
1641
             * check for enough space. Strictly speaking we know we've already
1642
             * got enough space because to get here the message size is < 0x200,
1643
             * but we know that we've allocated far more than that in the buffer
1644
             * - but for consistency and robustness we're going to check anyway.
1645
             *
1646
             * 4 bytes for the padding type and extension length
1647
             * + padding length
1648
             */
1649
0
            if (CHECKLEN(ret, 4 + hlen, limit))
1650
0
                return NULL;
1651
0
            s2n(TLSEXT_TYPE_padding, ret);
1652
0
            s2n(hlen, ret);
1653
0
            memset(ret, 0, hlen);
1654
0
            ret += hlen;
1655
0
        }
1656
0
    }
1657
1658
0
    if ((extdatalen = ret - orig - 2) == 0)
1659
0
        return orig;
1660
1661
0
    s2n(extdatalen, orig);
1662
0
    return ret;
1663
0
}
1664
1665
unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1666
                                          unsigned char *limit, int *al)
1667
0
{
1668
0
    int extdatalen = 0;
1669
0
    unsigned char *orig = buf;
1670
0
    unsigned char *ret = buf;
1671
0
# ifndef OPENSSL_NO_NEXTPROTONEG
1672
0
    int next_proto_neg_seen;
1673
0
# endif
1674
0
# ifndef OPENSSL_NO_EC
1675
0
    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1676
0
    unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1677
0
    int using_ecc = (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
1678
0
        || (alg_a & SSL_aECDSA);
1679
0
    using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1680
0
# endif
1681
    /*
1682
     * don't add extensions for SSLv3, unless doing secure renegotiation
1683
     */
1684
0
    if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1685
0
        return orig;
1686
1687
0
    ret += 2;
1688
0
    if (ret >= limit)
1689
0
        return NULL;            /* this really never occurs, but ... */
1690
1691
0
    if (!s->hit && s->servername_done == 1
1692
0
        && s->session->tlsext_hostname != NULL) {
1693
0
        if ((long)(limit - ret - 4) < 0)
1694
0
            return NULL;
1695
1696
0
        s2n(TLSEXT_TYPE_server_name, ret);
1697
0
        s2n(0, ret);
1698
0
    }
1699
1700
0
    if (s->s3->send_connection_binding) {
1701
0
        int el;
1702
1703
0
        if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1704
0
            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1705
0
            return NULL;
1706
0
        }
1707
1708
        /*-
1709
         * check for enough space.
1710
         * 4 bytes for the reneg type and extension length
1711
         * + reneg data length
1712
         */
1713
0
        if (CHECKLEN(ret, 4 + el, limit))
1714
0
            return NULL;
1715
1716
0
        s2n(TLSEXT_TYPE_renegotiate, ret);
1717
0
        s2n(el, ret);
1718
1719
0
        if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1720
0
            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1721
0
            return NULL;
1722
0
        }
1723
1724
0
        ret += el;
1725
0
    }
1726
0
# ifndef OPENSSL_NO_EC
1727
0
    if (using_ecc) {
1728
0
        const unsigned char *plist;
1729
0
        size_t plistlen;
1730
        /*
1731
         * Add TLS extension ECPointFormats to the ServerHello message
1732
         */
1733
1734
0
        tls1_get_formatlist(s, &plist, &plistlen);
1735
1736
0
        if (plistlen > 255) {
1737
0
            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1738
0
            return NULL;
1739
0
        }
1740
1741
        /*-
1742
         * check for enough space.
1743
         * 4 bytes for the ec points format type and extension length
1744
         * 1 byte for the points format list length
1745
         * + length of points format list
1746
         */
1747
0
        if (CHECKLEN(ret, 5 + plistlen, limit))
1748
0
            return NULL;
1749
1750
0
        s2n(TLSEXT_TYPE_ec_point_formats, ret);
1751
0
        s2n(plistlen + 1, ret);
1752
0
        *(ret++) = (unsigned char)plistlen;
1753
0
        memcpy(ret, plist, plistlen);
1754
0
        ret += plistlen;
1755
1756
0
    }
1757
    /*
1758
     * Currently the server should not respond with a SupportedCurves
1759
     * extension
1760
     */
1761
0
# endif                         /* OPENSSL_NO_EC */
1762
1763
0
    if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1764
        /*-
1765
         * check for enough space.
1766
         * 4 bytes for the Ticket type and extension length
1767
         */
1768
0
        if (CHECKLEN(ret, 4, limit))
1769
0
            return NULL;
1770
0
        s2n(TLSEXT_TYPE_session_ticket, ret);
1771
0
        s2n(0, ret);
1772
0
    } else {
1773
        /* if we don't add the above TLSEXT, we can't add a session ticket later */
1774
0
        s->tlsext_ticket_expected = 0;
1775
0
    }
1776
1777
0
    if (s->tlsext_status_expected) {
1778
        /*-
1779
         * check for enough space.
1780
         * 4 bytes for the Status request type and extension length
1781
         */
1782
0
        if (CHECKLEN(ret, 4, limit))
1783
0
            return NULL;
1784
0
        s2n(TLSEXT_TYPE_status_request, ret);
1785
0
        s2n(0, ret);
1786
0
    }
1787
# ifdef TLSEXT_TYPE_opaque_prf_input
1788
    if (s->s3->server_opaque_prf_input != NULL) {
1789
        size_t sol = s->s3->server_opaque_prf_input_len;
1790
1791
        if ((long)(limit - ret - 6 - sol) < 0)
1792
            return NULL;
1793
        if (sol > 0xFFFD)       /* can't happen */
1794
            return NULL;
1795
1796
        s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1797
        s2n(sol + 2, ret);
1798
        s2n(sol, ret);
1799
        memcpy(ret, s->s3->server_opaque_prf_input, sol);
1800
        ret += sol;
1801
    }
1802
# endif
1803
1804
0
# ifndef OPENSSL_NO_SRTP
1805
0
    if (SSL_IS_DTLS(s) && s->srtp_profile) {
1806
0
        int el;
1807
1808
0
        ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1809
1810
        /*-
1811
         * check for enough space.
1812
         * 4 bytes for the SRTP profiles type and extension length
1813
         * + length of the SRTP profiles list
1814
         */
1815
0
        if (CHECKLEN(ret, 4 + el, limit))
1816
0
            return NULL;
1817
1818
0
        s2n(TLSEXT_TYPE_use_srtp, ret);
1819
0
        s2n(el, ret);
1820
1821
0
        if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1822
0
            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1823
0
            return NULL;
1824
0
        }
1825
0
        ret += el;
1826
0
    }
1827
0
# endif
1828
1829
0
    if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1830
0
         || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1831
0
        && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1832
0
        const unsigned char cryptopro_ext[36] = {
1833
0
            0xfd, 0xe8,         /* 65000 */
1834
0
            0x00, 0x20,         /* 32 bytes length */
1835
0
            0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1836
0
            0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1837
0
            0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1838
0
            0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1839
0
        };
1840
1841
        /* check for enough space. */
1842
0
        if (CHECKLEN(ret, sizeof(cryptopro_ext), limit))
1843
0
            return NULL;
1844
0
        memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext));
1845
0
        ret += sizeof(cryptopro_ext);
1846
1847
0
    }
1848
0
# ifndef OPENSSL_NO_HEARTBEATS
1849
    /* Add Heartbeat extension if we've received one */
1850
0
    if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
1851
        /*-
1852
         * check for enough space.
1853
         * 4 bytes for the Heartbeat type and extension length
1854
         * 1 byte for the mode
1855
         */
1856
0
        if (CHECKLEN(ret, 5, limit))
1857
0
            return NULL;
1858
0
        s2n(TLSEXT_TYPE_heartbeat, ret);
1859
0
        s2n(1, ret);
1860
        /*-
1861
         * Set mode:
1862
         * 1: peer may send requests
1863
         * 2: peer not allowed to send requests
1864
         */
1865
0
        if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1866
0
            *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1867
0
        else
1868
0
            *(ret++) = SSL_TLSEXT_HB_ENABLED;
1869
1870
0
    }
1871
0
# endif
1872
1873
0
# ifndef OPENSSL_NO_NEXTPROTONEG
1874
0
    next_proto_neg_seen = s->s3->next_proto_neg_seen;
1875
0
    s->s3->next_proto_neg_seen = 0;
1876
0
    if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1877
0
        const unsigned char *npa;
1878
0
        unsigned int npalen;
1879
0
        int r;
1880
1881
0
        r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1882
0
                                              s->
1883
0
                                              ctx->next_protos_advertised_cb_arg);
1884
0
        if (r == SSL_TLSEXT_ERR_OK) {
1885
            /*-
1886
             * check for enough space.
1887
             * 4 bytes for the NPN type and extension length
1888
             * + length of protocols list
1889
             */
1890
0
            if (CHECKLEN(ret, 4 + npalen, limit))
1891
0
                return NULL;
1892
0
            s2n(TLSEXT_TYPE_next_proto_neg, ret);
1893
0
            s2n(npalen, ret);
1894
0
            memcpy(ret, npa, npalen);
1895
0
            ret += npalen;
1896
0
            s->s3->next_proto_neg_seen = 1;
1897
0
        }
1898
0
    }
1899
0
# endif
1900
0
    if (!custom_ext_add(s, 1, &ret, limit, al))
1901
0
        return NULL;
1902
1903
0
    if (s->s3->alpn_selected) {
1904
0
        const unsigned char *selected = s->s3->alpn_selected;
1905
0
        size_t len = s->s3->alpn_selected_len;
1906
1907
        /*-
1908
         * check for enough space.
1909
         * 4 bytes for the ALPN type and extension length
1910
         * 2 bytes for ALPN data length
1911
         * 1 byte for selected protocol length
1912
         * + length of the selected protocol
1913
         */
1914
0
        if (CHECKLEN(ret, 7 + len, limit))
1915
0
            return NULL;
1916
0
        s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1917
0
        s2n(3 + len, ret);
1918
0
        s2n(1 + len, ret);
1919
0
        *ret++ = len;
1920
0
        memcpy(ret, selected, len);
1921
0
        ret += len;
1922
0
    }
1923
1924
0
    if ((extdatalen = ret - orig - 2) == 0)
1925
0
        return orig;
1926
1927
0
    s2n(extdatalen, orig);
1928
0
    return ret;
1929
0
}
1930
1931
# ifndef OPENSSL_NO_EC
1932
/*-
1933
 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1934
 * SecureTransport using the TLS extension block in |d|, of length |n|.
1935
 * Safari, since 10.6, sends exactly these extensions, in this order:
1936
 *   SNI,
1937
 *   elliptic_curves
1938
 *   ec_point_formats
1939
 *
1940
 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1941
 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1942
 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1943
 * 10.8..10.8.3 (which don't work).
1944
 */
1945
static void ssl_check_for_safari(SSL *s, const unsigned char *data,
1946
                                 const unsigned char *limit)
1947
0
{
1948
0
    unsigned short type, size;
1949
0
    static const unsigned char kSafariExtensionsBlock[] = {
1950
0
        0x00, 0x0a,             /* elliptic_curves extension */
1951
0
        0x00, 0x08,             /* 8 bytes */
1952
0
        0x00, 0x06,             /* 6 bytes of curve ids */
1953
0
        0x00, 0x17,             /* P-256 */
1954
0
        0x00, 0x18,             /* P-384 */
1955
0
        0x00, 0x19,             /* P-521 */
1956
1957
0
        0x00, 0x0b,             /* ec_point_formats */
1958
0
        0x00, 0x02,             /* 2 bytes */
1959
0
        0x01,                   /* 1 point format */
1960
0
        0x00,                   /* uncompressed */
1961
0
    };
1962
1963
    /* The following is only present in TLS 1.2 */
1964
0
    static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1965
0
        0x00, 0x0d,             /* signature_algorithms */
1966
0
        0x00, 0x0c,             /* 12 bytes */
1967
0
        0x00, 0x0a,             /* 10 bytes */
1968
0
        0x05, 0x01,             /* SHA-384/RSA */
1969
0
        0x04, 0x01,             /* SHA-256/RSA */
1970
0
        0x02, 0x01,             /* SHA-1/RSA */
1971
0
        0x04, 0x03,             /* SHA-256/ECDSA */
1972
0
        0x02, 0x03,             /* SHA-1/ECDSA */
1973
0
    };
1974
1975
0
    if (limit - data <= 2)
1976
0
        return;
1977
0
    data += 2;
1978
1979
0
    if (limit - data < 4)
1980
0
        return;
1981
0
    n2s(data, type);
1982
0
    n2s(data, size);
1983
1984
0
    if (type != TLSEXT_TYPE_server_name)
1985
0
        return;
1986
1987
0
    if (limit - data < size)
1988
0
        return;
1989
0
    data += size;
1990
1991
0
    if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
1992
0
        const size_t len1 = sizeof(kSafariExtensionsBlock);
1993
0
        const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1994
1995
0
        if (limit - data != (int)(len1 + len2))
1996
0
            return;
1997
0
        if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1998
0
            return;
1999
0
        if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
2000
0
            return;
2001
0
    } else {
2002
0
        const size_t len = sizeof(kSafariExtensionsBlock);
2003
2004
0
        if (limit - data != (int)(len))
2005
0
            return;
2006
0
        if (memcmp(data, kSafariExtensionsBlock, len) != 0)
2007
0
            return;
2008
0
    }
2009
2010
0
    s->s3->is_probably_safari = 1;
2011
0
}
2012
# endif                         /* !OPENSSL_NO_EC */
2013
2014
/*
2015
 * tls1_alpn_handle_client_hello is called to save the ALPN extension in a
2016
 * ClientHello.  data: the contents of the extension, not including the type
2017
 * and length.  data_len: the number of bytes in |data| al: a pointer to the
2018
 * alert value to send in the event of a non-zero return.  returns: 0 on
2019
 * success.
2020
 */
2021
static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
2022
                                         unsigned data_len, int *al)
2023
0
{
2024
0
    unsigned i;
2025
0
    unsigned proto_len;
2026
2027
0
    if (data_len < 2)
2028
0
        goto parse_error;
2029
2030
    /*
2031
     * data should contain a uint16 length followed by a series of 8-bit,
2032
     * length-prefixed strings.
2033
     */
2034
0
    i = ((unsigned)data[0]) << 8 | ((unsigned)data[1]);
2035
0
    data_len -= 2;
2036
0
    data += 2;
2037
0
    if (data_len != i)
2038
0
        goto parse_error;
2039
2040
0
    if (data_len < 2)
2041
0
        goto parse_error;
2042
2043
0
    for (i = 0; i < data_len;) {
2044
0
        proto_len = data[i];
2045
0
        i++;
2046
2047
0
        if (proto_len == 0)
2048
0
            goto parse_error;
2049
2050
0
        if (i + proto_len < i || i + proto_len > data_len)
2051
0
            goto parse_error;
2052
2053
0
        i += proto_len;
2054
0
    }
2055
2056
0
    if (s->cert->alpn_proposed != NULL)
2057
0
        OPENSSL_free(s->cert->alpn_proposed);
2058
0
    s->cert->alpn_proposed = OPENSSL_malloc(data_len);
2059
0
    if (s->cert->alpn_proposed == NULL) {
2060
0
        *al = SSL_AD_INTERNAL_ERROR;
2061
0
        return -1;
2062
0
    }
2063
0
    memcpy(s->cert->alpn_proposed, data, data_len);
2064
0
    s->cert->alpn_proposed_len = data_len;
2065
0
    return 0;
2066
2067
0
 parse_error:
2068
0
    *al = SSL_AD_DECODE_ERROR;
2069
0
    return -1;
2070
0
}
2071
2072
/*
2073
 * Process the ALPN extension in a ClientHello.
2074
 * al: a pointer to the alert value to send in the event of a failure.
2075
 * returns 1 on success, 0 on failure: al set only on failure
2076
 */
2077
static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
2078
0
{
2079
0
    const unsigned char *selected = NULL;
2080
0
    unsigned char selected_len = 0;
2081
2082
0
    if (s->ctx->alpn_select_cb != NULL && s->cert->alpn_proposed != NULL) {
2083
0
        int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
2084
0
                                       s->cert->alpn_proposed,
2085
0
                                       s->cert->alpn_proposed_len,
2086
0
                                       s->ctx->alpn_select_cb_arg);
2087
2088
0
        if (r == SSL_TLSEXT_ERR_OK) {
2089
0
            OPENSSL_free(s->s3->alpn_selected);
2090
0
            s->s3->alpn_selected = OPENSSL_malloc(selected_len);
2091
0
            if (s->s3->alpn_selected == NULL) {
2092
0
                *al = SSL_AD_INTERNAL_ERROR;
2093
0
                return 0;
2094
0
            }
2095
0
            memcpy(s->s3->alpn_selected, selected, selected_len);
2096
0
            s->s3->alpn_selected_len = selected_len;
2097
0
# ifndef OPENSSL_NO_NEXTPROTONEG
2098
            /* ALPN takes precedence over NPN. */
2099
0
            s->s3->next_proto_neg_seen = 0;
2100
0
# endif
2101
0
        }
2102
0
    }
2103
2104
0
    return 1;
2105
0
}
2106
2107
static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p,
2108
                                       unsigned char *limit, int *al)
2109
0
{
2110
0
    unsigned short type;
2111
0
    unsigned short size;
2112
0
    unsigned short len;
2113
0
    unsigned char *data = *p;
2114
0
    int renegotiate_seen = 0;
2115
2116
0
    s->servername_done = 0;
2117
0
    s->tlsext_status_type = -1;
2118
0
# ifndef OPENSSL_NO_NEXTPROTONEG
2119
0
    s->s3->next_proto_neg_seen = 0;
2120
0
# endif
2121
2122
0
    if (s->s3->alpn_selected) {
2123
0
        OPENSSL_free(s->s3->alpn_selected);
2124
0
        s->s3->alpn_selected = NULL;
2125
0
    }
2126
0
    s->s3->alpn_selected_len = 0;
2127
0
    if (s->cert->alpn_proposed) {
2128
0
        OPENSSL_free(s->cert->alpn_proposed);
2129
0
        s->cert->alpn_proposed = NULL;
2130
0
    }
2131
0
    s->cert->alpn_proposed_len = 0;
2132
0
# ifndef OPENSSL_NO_HEARTBEATS
2133
0
    s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2134
0
                             SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2135
0
# endif
2136
2137
0
# ifndef OPENSSL_NO_EC
2138
0
    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
2139
0
        ssl_check_for_safari(s, data, limit);
2140
0
# endif                         /* !OPENSSL_NO_EC */
2141
2142
    /* Clear any signature algorithms extension received */
2143
0
    if (s->cert->peer_sigalgs) {
2144
0
        OPENSSL_free(s->cert->peer_sigalgs);
2145
0
        s->cert->peer_sigalgs = NULL;
2146
0
    }
2147
0
# ifndef OPENSSL_NO_SRP
2148
0
    if (s->srp_ctx.login != NULL) {
2149
0
        OPENSSL_free(s->srp_ctx.login);
2150
0
        s->srp_ctx.login = NULL;
2151
0
    }
2152
0
# endif
2153
2154
0
    s->srtp_profile = NULL;
2155
2156
0
    if (data == limit)
2157
0
        goto ri_check;
2158
2159
0
    if (limit - data < 2)
2160
0
        goto err;
2161
2162
0
    n2s(data, len);
2163
2164
0
    if (limit - data != len)
2165
0
        goto err;
2166
2167
0
    while (limit - data >= 4) {
2168
0
        n2s(data, type);
2169
0
        n2s(data, size);
2170
2171
0
        if (limit - data < size)
2172
0
            goto err;
2173
# if 0
2174
        fprintf(stderr, "Received extension type %d size %d\n", type, size);
2175
# endif
2176
0
        if (s->tlsext_debug_cb)
2177
0
            s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
2178
/*-
2179
 * The servername extension is treated as follows:
2180
 *
2181
 * - Only the hostname type is supported with a maximum length of 255.
2182
 * - The servername is rejected if too long or if it contains zeros,
2183
 *   in which case an fatal alert is generated.
2184
 * - The servername field is maintained together with the session cache.
2185
 * - When a session is resumed, the servername call back invoked in order
2186
 *   to allow the application to position itself to the right context.
2187
 * - The servername is acknowledged if it is new for a session or when
2188
 *   it is identical to a previously used for the same session.
2189
 *   Applications can control the behaviour.  They can at any time
2190
 *   set a 'desirable' servername for a new SSL object. This can be the
2191
 *   case for example with HTTPS when a Host: header field is received and
2192
 *   a renegotiation is requested. In this case, a possible servername
2193
 *   presented in the new client hello is only acknowledged if it matches
2194
 *   the value of the Host: field.
2195
 * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2196
 *   if they provide for changing an explicit servername context for the
2197
 *   session, i.e. when the session has been established with a servername
2198
 *   extension.
2199
 * - On session reconnect, the servername extension may be absent.
2200
 *
2201
 */
2202
2203
0
        if (type == TLSEXT_TYPE_server_name) {
2204
0
            unsigned char *sdata;
2205
0
            int servname_type;
2206
0
            int dsize;
2207
2208
0
            if (size < 2)
2209
0
                goto err;
2210
0
            n2s(data, dsize);
2211
0
            size -= 2;
2212
0
            if (dsize > size)
2213
0
                goto err;
2214
2215
0
            sdata = data;
2216
0
            while (dsize > 3) {
2217
0
                servname_type = *(sdata++);
2218
0
                n2s(sdata, len);
2219
0
                dsize -= 3;
2220
2221
0
                if (len > dsize)
2222
0
                    goto err;
2223
2224
0
                if (s->servername_done == 0)
2225
0
                    switch (servname_type) {
2226
0
                    case TLSEXT_NAMETYPE_host_name:
2227
0
                        if (!s->hit) {
2228
0
                            if (s->session->tlsext_hostname)
2229
0
                                goto err;
2230
2231
0
                            if (len > TLSEXT_MAXLEN_host_name) {
2232
0
                                *al = TLS1_AD_UNRECOGNIZED_NAME;
2233
0
                                return 0;
2234
0
                            }
2235
0
                            if ((s->session->tlsext_hostname =
2236
0
                                 OPENSSL_malloc(len + 1)) == NULL) {
2237
0
                                *al = TLS1_AD_INTERNAL_ERROR;
2238
0
                                return 0;
2239
0
                            }
2240
0
                            memcpy(s->session->tlsext_hostname, sdata, len);
2241
0
                            s->session->tlsext_hostname[len] = '\0';
2242
0
                            if (strlen(s->session->tlsext_hostname) != len) {
2243
0
                                OPENSSL_free(s->session->tlsext_hostname);
2244
0
                                s->session->tlsext_hostname = NULL;
2245
0
                                *al = TLS1_AD_UNRECOGNIZED_NAME;
2246
0
                                return 0;
2247
0
                            }
2248
0
                            s->servername_done = 1;
2249
2250
0
                        } else
2251
0
                            s->servername_done = s->session->tlsext_hostname
2252
0
                                && strlen(s->session->tlsext_hostname) == len
2253
0
                                && strncmp(s->session->tlsext_hostname,
2254
0
                                           (char *)sdata, len) == 0;
2255
2256
0
                        break;
2257
2258
0
                    default:
2259
0
                        break;
2260
0
                    }
2261
2262
0
                dsize -= len;
2263
0
            }
2264
0
            if (dsize != 0)
2265
0
                goto err;
2266
2267
0
        }
2268
0
# ifndef OPENSSL_NO_SRP
2269
0
        else if (type == TLSEXT_TYPE_srp) {
2270
0
            if (size == 0 || ((len = data[0])) != (size - 1))
2271
0
                goto err;
2272
0
            if (s->srp_ctx.login != NULL)
2273
0
                goto err;
2274
0
            if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
2275
0
                return -1;
2276
0
            memcpy(s->srp_ctx.login, &data[1], len);
2277
0
            s->srp_ctx.login[len] = '\0';
2278
2279
0
            if (strlen(s->srp_ctx.login) != len)
2280
0
                goto err;
2281
0
        }
2282
0
# endif
2283
2284
0
# ifndef OPENSSL_NO_EC
2285
0
        else if (type == TLSEXT_TYPE_ec_point_formats) {
2286
0
            unsigned char *sdata = data;
2287
0
            int ecpointformatlist_length = *(sdata++);
2288
2289
0
            if (ecpointformatlist_length != size - 1 ||
2290
0
                ecpointformatlist_length < 1)
2291
0
                goto err;
2292
0
            if (!s->hit) {
2293
0
                if (s->session->tlsext_ecpointformatlist) {
2294
0
                    OPENSSL_free(s->session->tlsext_ecpointformatlist);
2295
0
                    s->session->tlsext_ecpointformatlist = NULL;
2296
0
                }
2297
0
                s->session->tlsext_ecpointformatlist_length = 0;
2298
0
                if ((s->session->tlsext_ecpointformatlist =
2299
0
                     OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2300
0
                    *al = TLS1_AD_INTERNAL_ERROR;
2301
0
                    return 0;
2302
0
                }
2303
0
                s->session->tlsext_ecpointformatlist_length =
2304
0
                    ecpointformatlist_length;
2305
0
                memcpy(s->session->tlsext_ecpointformatlist, sdata,
2306
0
                       ecpointformatlist_length);
2307
0
            }
2308
#  if 0
2309
            fprintf(stderr,
2310
                    "ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ",
2311
                    s->session->tlsext_ecpointformatlist_length);
2312
            sdata = s->session->tlsext_ecpointformatlist;
2313
            for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2314
                fprintf(stderr, "%i ", *(sdata++));
2315
            fprintf(stderr, "\n");
2316
#  endif
2317
0
        } else if (type == TLSEXT_TYPE_elliptic_curves) {
2318
0
            unsigned char *sdata = data;
2319
0
            int ellipticcurvelist_length = (*(sdata++) << 8);
2320
0
            ellipticcurvelist_length += (*(sdata++));
2321
2322
0
            if (ellipticcurvelist_length != size - 2 ||
2323
0
                ellipticcurvelist_length < 1 ||
2324
                /* Each NamedCurve is 2 bytes. */
2325
0
                ellipticcurvelist_length & 1)
2326
0
                    goto err;
2327
2328
0
            if (!s->hit) {
2329
0
                if (s->session->tlsext_ellipticcurvelist)
2330
0
                    goto err;
2331
2332
0
                s->session->tlsext_ellipticcurvelist_length = 0;
2333
0
                if ((s->session->tlsext_ellipticcurvelist =
2334
0
                     OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
2335
0
                    *al = TLS1_AD_INTERNAL_ERROR;
2336
0
                    return 0;
2337
0
                }
2338
0
                s->session->tlsext_ellipticcurvelist_length =
2339
0
                    ellipticcurvelist_length;
2340
0
                memcpy(s->session->tlsext_ellipticcurvelist, sdata,
2341
0
                       ellipticcurvelist_length);
2342
0
            }
2343
#  if 0
2344
            fprintf(stderr,
2345
                    "ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ",
2346
                    s->session->tlsext_ellipticcurvelist_length);
2347
            sdata = s->session->tlsext_ellipticcurvelist;
2348
            for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2349
                fprintf(stderr, "%i ", *(sdata++));
2350
            fprintf(stderr, "\n");
2351
#  endif
2352
0
        }
2353
0
# endif                         /* OPENSSL_NO_EC */
2354
# ifdef TLSEXT_TYPE_opaque_prf_input
2355
        else if (type == TLSEXT_TYPE_opaque_prf_input) {
2356
            unsigned char *sdata = data;
2357
2358
            if (size < 2) {
2359
                *al = SSL_AD_DECODE_ERROR;
2360
                return 0;
2361
            }
2362
            n2s(sdata, s->s3->client_opaque_prf_input_len);
2363
            if (s->s3->client_opaque_prf_input_len != size - 2) {
2364
                *al = SSL_AD_DECODE_ERROR;
2365
                return 0;
2366
            }
2367
2368
            if (s->s3->client_opaque_prf_input != NULL) {
2369
                /* shouldn't really happen */
2370
                OPENSSL_free(s->s3->client_opaque_prf_input);
2371
            }
2372
2373
            /* dummy byte just to get non-NULL */
2374
            if (s->s3->client_opaque_prf_input_len == 0)
2375
                s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
2376
            else
2377
                s->s3->client_opaque_prf_input =
2378
                    BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2379
            if (s->s3->client_opaque_prf_input == NULL) {
2380
                *al = TLS1_AD_INTERNAL_ERROR;
2381
                return 0;
2382
            }
2383
        }
2384
# endif
2385
0
        else if (type == TLSEXT_TYPE_session_ticket) {
2386
0
            if (s->tls_session_ticket_ext_cb &&
2387
0
                !s->tls_session_ticket_ext_cb(s, data, size,
2388
0
                                              s->tls_session_ticket_ext_cb_arg))
2389
0
            {
2390
0
                *al = TLS1_AD_INTERNAL_ERROR;
2391
0
                return 0;
2392
0
            }
2393
0
        } else if (type == TLSEXT_TYPE_renegotiate) {
2394
0
            if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2395
0
                return 0;
2396
0
            renegotiate_seen = 1;
2397
0
        } else if (type == TLSEXT_TYPE_signature_algorithms) {
2398
0
            int dsize;
2399
0
            if (s->cert->peer_sigalgs || size < 2)
2400
0
                goto err;
2401
0
            n2s(data, dsize);
2402
0
            size -= 2;
2403
0
            if (dsize != size || dsize & 1 || !dsize)
2404
0
                goto err;
2405
0
            if (!tls1_save_sigalgs(s, data, dsize))
2406
0
                goto err;
2407
0
        } else if (type == TLSEXT_TYPE_status_request) {
2408
2409
0
            if (size < 5)
2410
0
                goto err;
2411
2412
0
            s->tlsext_status_type = *data++;
2413
0
            size--;
2414
0
            if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2415
0
                const unsigned char *sdata;
2416
0
                int dsize;
2417
                /* Read in responder_id_list */
2418
0
                n2s(data, dsize);
2419
0
                size -= 2;
2420
0
                if (dsize > size)
2421
0
                    goto err;
2422
2423
                /*
2424
                 * We remove any OCSP_RESPIDs from a previous handshake
2425
                 * to prevent unbounded memory growth - CVE-2016-6304
2426
                 */
2427
0
                sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2428
0
                                        OCSP_RESPID_free);
2429
0
                if (dsize > 0) {
2430
0
                    s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2431
0
                    if (s->tlsext_ocsp_ids == NULL) {
2432
0
                        *al = SSL_AD_INTERNAL_ERROR;
2433
0
                        return 0;
2434
0
                    }
2435
0
                } else {
2436
0
                    s->tlsext_ocsp_ids = NULL;
2437
0
                }
2438
2439
0
                while (dsize > 0) {
2440
0
                    OCSP_RESPID *id;
2441
0
                    int idsize;
2442
0
                    if (dsize < 4)
2443
0
                        goto err;
2444
0
                    n2s(data, idsize);
2445
0
                    dsize -= 2 + idsize;
2446
0
                    size -= 2 + idsize;
2447
0
                    if (dsize < 0)
2448
0
                        goto err;
2449
0
                    sdata = data;
2450
0
                    data += idsize;
2451
0
                    id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
2452
0
                    if (!id)
2453
0
                        goto err;
2454
0
                    if (data != sdata) {
2455
0
                        OCSP_RESPID_free(id);
2456
0
                        goto err;
2457
0
                    }
2458
0
                    if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2459
0
                        OCSP_RESPID_free(id);
2460
0
                        *al = SSL_AD_INTERNAL_ERROR;
2461
0
                        return 0;
2462
0
                    }
2463
0
                }
2464
2465
                /* Read in request_extensions */
2466
0
                if (size < 2)
2467
0
                    goto err;
2468
0
                n2s(data, dsize);
2469
0
                size -= 2;
2470
0
                if (dsize != size)
2471
0
                    goto err;
2472
0
                sdata = data;
2473
0
                if (dsize > 0) {
2474
0
                    if (s->tlsext_ocsp_exts) {
2475
0
                        sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2476
0
                                                   X509_EXTENSION_free);
2477
0
                    }
2478
2479
0
                    s->tlsext_ocsp_exts =
2480
0
                        d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
2481
0
                    if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
2482
0
                        goto err;
2483
0
                }
2484
0
            }
2485
            /*
2486
             * We don't know what to do with any other type * so ignore it.
2487
             */
2488
0
            else
2489
0
                s->tlsext_status_type = -1;
2490
0
        }
2491
0
# ifndef OPENSSL_NO_HEARTBEATS
2492
0
        else if (type == TLSEXT_TYPE_heartbeat) {
2493
0
            switch (data[0]) {
2494
0
            case 0x01:         /* Client allows us to send HB requests */
2495
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2496
0
                break;
2497
0
            case 0x02:         /* Client doesn't accept HB requests */
2498
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2499
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2500
0
                break;
2501
0
            default:
2502
0
                *al = SSL_AD_ILLEGAL_PARAMETER;
2503
0
                return 0;
2504
0
            }
2505
0
        }
2506
0
# endif
2507
0
# ifndef OPENSSL_NO_NEXTPROTONEG
2508
0
        else if (type == TLSEXT_TYPE_next_proto_neg &&
2509
0
                 s->s3->tmp.finish_md_len == 0) {
2510
            /*-
2511
             * We shouldn't accept this extension on a
2512
             * renegotiation.
2513
             *
2514
             * s->new_session will be set on renegotiation, but we
2515
             * probably shouldn't rely that it couldn't be set on
2516
             * the initial renegotation too in certain cases (when
2517
             * there's some other reason to disallow resuming an
2518
             * earlier session -- the current code won't be doing
2519
             * anything like that, but this might change).
2520
             *
2521
             * A valid sign that there's been a previous handshake
2522
             * in this connection is if s->s3->tmp.finish_md_len >
2523
             * 0.  (We are talking about a check that will happen
2524
             * in the Hello protocol round, well before a new
2525
             * Finished message could have been computed.)
2526
             */
2527
0
            s->s3->next_proto_neg_seen = 1;
2528
0
        }
2529
0
# endif
2530
2531
0
        else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2532
0
                 s->s3->tmp.finish_md_len == 0) {
2533
0
            if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2534
0
                return 0;
2535
0
        }
2536
2537
        /* session ticket processed earlier */
2538
0
# ifndef OPENSSL_NO_SRTP
2539
0
        else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2540
0
                 && type == TLSEXT_TYPE_use_srtp) {
2541
0
            if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al))
2542
0
                return 0;
2543
0
        }
2544
0
# endif
2545
2546
0
        data += size;
2547
0
    }
2548
2549
    /* Spurious data on the end */
2550
0
    if (data != limit)
2551
0
        goto err;
2552
2553
0
    *p = data;
2554
2555
0
 ri_check:
2556
2557
    /* Need RI if renegotiating */
2558
2559
0
    if (!renegotiate_seen && s->renegotiate &&
2560
0
        !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2561
0
        *al = SSL_AD_HANDSHAKE_FAILURE;
2562
0
        SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2563
0
               SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2564
0
        return 0;
2565
0
    }
2566
2567
0
    return 1;
2568
0
err:
2569
0
    *al = SSL_AD_DECODE_ERROR;
2570
0
    return 0;
2571
0
}
2572
2573
/*
2574
 * Parse any custom extensions found.  "data" is the start of the extension data
2575
 * and "limit" is the end of the record. TODO: add strict syntax checking.
2576
 */
2577
2578
static int ssl_scan_clienthello_custom_tlsext(SSL *s,
2579
                                              const unsigned char *data,
2580
                                              const unsigned char *limit,
2581
                                              int *al)
2582
0
{
2583
0
    unsigned short type, size, len;
2584
    /* If resumed session or no custom extensions nothing to do */
2585
0
    if (s->hit || s->cert->srv_ext.meths_count == 0)
2586
0
        return 1;
2587
2588
0
    if (limit - data <= 2)
2589
0
        return 1;
2590
0
    n2s(data, len);
2591
2592
0
    if (limit - data < len)
2593
0
        return 1;
2594
2595
0
    while (limit - data >= 4) {
2596
0
        n2s(data, type);
2597
0
        n2s(data, size);
2598
2599
0
        if (limit - data < size)
2600
0
            return 1;
2601
0
        if (custom_ext_parse(s, 1 /* server */ , type, data, size, al) <= 0)
2602
0
            return 0;
2603
2604
0
        data += size;
2605
0
    }
2606
2607
0
    return 1;
2608
0
}
2609
2610
int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p,
2611
                                 unsigned char *limit)
2612
0
{
2613
0
    int al = -1;
2614
0
    unsigned char *ptmp = *p;
2615
    /*
2616
     * Internally supported extensions are parsed first so SNI can be handled
2617
     * before custom extensions. An application processing SNI will typically
2618
     * switch the parent context using SSL_set_SSL_CTX and custom extensions
2619
     * need to be handled by the new SSL_CTX structure.
2620
     */
2621
0
    if (ssl_scan_clienthello_tlsext(s, p, limit, &al) <= 0) {
2622
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
2623
0
        return 0;
2624
0
    }
2625
2626
0
    if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2627
0
        SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2628
0
        return 0;
2629
0
    }
2630
2631
0
    custom_ext_init(&s->cert->srv_ext);
2632
0
    if (ssl_scan_clienthello_custom_tlsext(s, ptmp, limit, &al) <= 0) {
2633
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
2634
0
        return 0;
2635
0
    }
2636
2637
0
    return 1;
2638
0
}
2639
2640
# ifndef OPENSSL_NO_NEXTPROTONEG
2641
/*
2642
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2643
 * elements of zero length are allowed and the set of elements must exactly
2644
 * fill the length of the block.
2645
 */
2646
static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2647
0
{
2648
0
    unsigned int off = 0;
2649
2650
0
    while (off < len) {
2651
0
        if (d[off] == 0)
2652
0
            return 0;
2653
0
        off += d[off];
2654
0
        off++;
2655
0
    }
2656
2657
0
    return off == len;
2658
0
}
2659
# endif
2660
2661
static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p,
2662
                                       unsigned char *d, int n, int *al)
2663
0
{
2664
0
    unsigned short length;
2665
0
    unsigned short type;
2666
0
    unsigned short size;
2667
0
    unsigned char *data = *p;
2668
0
    int tlsext_servername = 0;
2669
0
    int renegotiate_seen = 0;
2670
2671
0
# ifndef OPENSSL_NO_NEXTPROTONEG
2672
0
    s->s3->next_proto_neg_seen = 0;
2673
0
# endif
2674
0
    s->tlsext_ticket_expected = 0;
2675
2676
0
    if (s->s3->alpn_selected) {
2677
0
        OPENSSL_free(s->s3->alpn_selected);
2678
0
        s->s3->alpn_selected = NULL;
2679
0
    }
2680
0
# ifndef OPENSSL_NO_HEARTBEATS
2681
0
    s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2682
0
                             SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2683
0
# endif
2684
2685
0
    if ((d + n) - data <= 2)
2686
0
        goto ri_check;
2687
2688
0
    n2s(data, length);
2689
0
    if ((d + n) - data != length) {
2690
0
        *al = SSL_AD_DECODE_ERROR;
2691
0
        return 0;
2692
0
    }
2693
2694
0
    while ((d + n) - data >= 4) {
2695
0
        n2s(data, type);
2696
0
        n2s(data, size);
2697
2698
0
        if ((d + n) - data < size)
2699
0
            goto ri_check;
2700
2701
0
        if (s->tlsext_debug_cb)
2702
0
            s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2703
2704
0
        if (type == TLSEXT_TYPE_server_name) {
2705
0
            if (s->tlsext_hostname == NULL || size > 0) {
2706
0
                *al = TLS1_AD_UNRECOGNIZED_NAME;
2707
0
                return 0;
2708
0
            }
2709
0
            tlsext_servername = 1;
2710
0
        }
2711
0
# ifndef OPENSSL_NO_EC
2712
0
        else if (type == TLSEXT_TYPE_ec_point_formats) {
2713
0
            unsigned char *sdata = data;
2714
0
            int ecpointformatlist_length = *(sdata++);
2715
2716
0
            if (ecpointformatlist_length != size - 1) {
2717
0
                *al = TLS1_AD_DECODE_ERROR;
2718
0
                return 0;
2719
0
            }
2720
0
            if (!s->hit) {
2721
0
                s->session->tlsext_ecpointformatlist_length = 0;
2722
0
                if (s->session->tlsext_ecpointformatlist != NULL)
2723
0
                    OPENSSL_free(s->session->tlsext_ecpointformatlist);
2724
0
                if ((s->session->tlsext_ecpointformatlist =
2725
0
                     OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2726
0
                    *al = TLS1_AD_INTERNAL_ERROR;
2727
0
                    return 0;
2728
0
                }
2729
0
                s->session->tlsext_ecpointformatlist_length =
2730
0
                    ecpointformatlist_length;
2731
0
                memcpy(s->session->tlsext_ecpointformatlist, sdata,
2732
0
                       ecpointformatlist_length);
2733
0
            }
2734
#  if 0
2735
            fprintf(stderr,
2736
                    "ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2737
            sdata = s->session->tlsext_ecpointformatlist;
2738
            for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2739
                fprintf(stderr, "%i ", *(sdata++));
2740
            fprintf(stderr, "\n");
2741
#  endif
2742
0
        }
2743
0
# endif                         /* OPENSSL_NO_EC */
2744
2745
0
        else if (type == TLSEXT_TYPE_session_ticket) {
2746
0
            if (s->tls_session_ticket_ext_cb &&
2747
0
                !s->tls_session_ticket_ext_cb(s, data, size,
2748
0
                                              s->tls_session_ticket_ext_cb_arg))
2749
0
            {
2750
0
                *al = TLS1_AD_INTERNAL_ERROR;
2751
0
                return 0;
2752
0
            }
2753
0
            if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2754
0
                || (size > 0)) {
2755
0
                *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2756
0
                return 0;
2757
0
            }
2758
0
            s->tlsext_ticket_expected = 1;
2759
0
        }
2760
# ifdef TLSEXT_TYPE_opaque_prf_input
2761
        else if (type == TLSEXT_TYPE_opaque_prf_input) {
2762
            unsigned char *sdata = data;
2763
2764
            if (size < 2) {
2765
                *al = SSL_AD_DECODE_ERROR;
2766
                return 0;
2767
            }
2768
            n2s(sdata, s->s3->server_opaque_prf_input_len);
2769
            if (s->s3->server_opaque_prf_input_len != size - 2) {
2770
                *al = SSL_AD_DECODE_ERROR;
2771
                return 0;
2772
            }
2773
2774
            if (s->s3->server_opaque_prf_input != NULL) {
2775
                /* shouldn't really happen */
2776
                OPENSSL_free(s->s3->server_opaque_prf_input);
2777
            }
2778
            if (s->s3->server_opaque_prf_input_len == 0) {
2779
                /* dummy byte just to get non-NULL */
2780
                s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
2781
            } else {
2782
                s->s3->server_opaque_prf_input =
2783
                    BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2784
            }
2785
2786
            if (s->s3->server_opaque_prf_input == NULL) {
2787
                *al = TLS1_AD_INTERNAL_ERROR;
2788
                return 0;
2789
            }
2790
        }
2791
# endif
2792
0
        else if (type == TLSEXT_TYPE_status_request) {
2793
            /*
2794
             * MUST be empty and only sent if we've requested a status
2795
             * request message.
2796
             */
2797
0
            if ((s->tlsext_status_type == -1) || (size > 0)) {
2798
0
                *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2799
0
                return 0;
2800
0
            }
2801
            /* Set flag to expect CertificateStatus message */
2802
0
            s->tlsext_status_expected = 1;
2803
0
        }
2804
0
# ifndef OPENSSL_NO_NEXTPROTONEG
2805
0
        else if (type == TLSEXT_TYPE_next_proto_neg &&
2806
0
                 s->s3->tmp.finish_md_len == 0) {
2807
0
            unsigned char *selected;
2808
0
            unsigned char selected_len;
2809
2810
            /* We must have requested it. */
2811
0
            if (s->ctx->next_proto_select_cb == NULL) {
2812
0
                *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2813
0
                return 0;
2814
0
            }
2815
            /* The data must be valid */
2816
0
            if (!ssl_next_proto_validate(data, size)) {
2817
0
                *al = TLS1_AD_DECODE_ERROR;
2818
0
                return 0;
2819
0
            }
2820
0
            if (s->
2821
0
                ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2822
0
                                          size,
2823
0
                                          s->ctx->next_proto_select_cb_arg) !=
2824
0
                SSL_TLSEXT_ERR_OK) {
2825
0
                *al = TLS1_AD_INTERNAL_ERROR;
2826
0
                return 0;
2827
0
            }
2828
            /*
2829
             * Could be non-NULL if server has sent multiple NPN extensions in
2830
             * a single Serverhello
2831
             */
2832
0
            OPENSSL_free(s->next_proto_negotiated);
2833
0
            s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2834
0
            if (!s->next_proto_negotiated) {
2835
0
                *al = TLS1_AD_INTERNAL_ERROR;
2836
0
                return 0;
2837
0
            }
2838
0
            memcpy(s->next_proto_negotiated, selected, selected_len);
2839
0
            s->next_proto_negotiated_len = selected_len;
2840
0
            s->s3->next_proto_neg_seen = 1;
2841
0
        }
2842
0
# endif
2843
2844
0
        else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2845
0
            unsigned len;
2846
2847
            /* We must have requested it. */
2848
0
            if (!s->cert->alpn_sent) {
2849
0
                *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2850
0
                return 0;
2851
0
            }
2852
0
            if (size < 4) {
2853
0
                *al = TLS1_AD_DECODE_ERROR;
2854
0
                return 0;
2855
0
            }
2856
            /*-
2857
             * The extension data consists of:
2858
             *   uint16 list_length
2859
             *   uint8 proto_length;
2860
             *   uint8 proto[proto_length];
2861
             */
2862
0
            len = data[0];
2863
0
            len <<= 8;
2864
0
            len |= data[1];
2865
0
            if (len != (unsigned)size - 2) {
2866
0
                *al = TLS1_AD_DECODE_ERROR;
2867
0
                return 0;
2868
0
            }
2869
0
            len = data[2];
2870
0
            if (len != (unsigned)size - 3) {
2871
0
                *al = TLS1_AD_DECODE_ERROR;
2872
0
                return 0;
2873
0
            }
2874
0
            if (s->s3->alpn_selected)
2875
0
                OPENSSL_free(s->s3->alpn_selected);
2876
0
            s->s3->alpn_selected = OPENSSL_malloc(len);
2877
0
            if (!s->s3->alpn_selected) {
2878
0
                *al = TLS1_AD_INTERNAL_ERROR;
2879
0
                return 0;
2880
0
            }
2881
0
            memcpy(s->s3->alpn_selected, data + 3, len);
2882
0
            s->s3->alpn_selected_len = len;
2883
0
        }
2884
2885
0
        else if (type == TLSEXT_TYPE_renegotiate) {
2886
0
            if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2887
0
                return 0;
2888
0
            renegotiate_seen = 1;
2889
0
        }
2890
0
# ifndef OPENSSL_NO_HEARTBEATS
2891
0
        else if (type == TLSEXT_TYPE_heartbeat) {
2892
0
            switch (data[0]) {
2893
0
            case 0x01:         /* Server allows us to send HB requests */
2894
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2895
0
                break;
2896
0
            case 0x02:         /* Server doesn't accept HB requests */
2897
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2898
0
                s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2899
0
                break;
2900
0
            default:
2901
0
                *al = SSL_AD_ILLEGAL_PARAMETER;
2902
0
                return 0;
2903
0
            }
2904
0
        }
2905
0
# endif
2906
0
# ifndef OPENSSL_NO_SRTP
2907
0
        else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2908
0
            if (ssl_parse_serverhello_use_srtp_ext(s, data, size, al))
2909
0
                return 0;
2910
0
        }
2911
0
# endif
2912
        /*
2913
         * If this extension type was not otherwise handled, but matches a
2914
         * custom_cli_ext_record, then send it to the c callback
2915
         */
2916
0
        else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2917
0
            return 0;
2918
2919
0
        data += size;
2920
0
    }
2921
2922
0
    if (data != d + n) {
2923
0
        *al = SSL_AD_DECODE_ERROR;
2924
0
        return 0;
2925
0
    }
2926
2927
0
    if (!s->hit && tlsext_servername == 1) {
2928
0
        if (s->tlsext_hostname) {
2929
0
            if (s->session->tlsext_hostname == NULL) {
2930
0
                s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2931
0
                if (!s->session->tlsext_hostname) {
2932
0
                    *al = SSL_AD_UNRECOGNIZED_NAME;
2933
0
                    return 0;
2934
0
                }
2935
0
            } else {
2936
0
                *al = SSL_AD_DECODE_ERROR;
2937
0
                return 0;
2938
0
            }
2939
0
        }
2940
0
    }
2941
2942
0
    *p = data;
2943
2944
0
 ri_check:
2945
2946
    /*
2947
     * Determine if we need to see RI. Strictly speaking if we want to avoid
2948
     * an attack we should *always* see RI even on initial server hello
2949
     * because the client doesn't see any renegotiation during an attack.
2950
     * However this would mean we could not connect to any server which
2951
     * doesn't support RI so for the immediate future tolerate RI absence on
2952
     * initial connect only.
2953
     */
2954
0
    if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2955
0
        && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2956
0
        *al = SSL_AD_HANDSHAKE_FAILURE;
2957
0
        SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2958
0
               SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2959
0
        return 0;
2960
0
    }
2961
2962
0
    return 1;
2963
0
}
2964
2965
int ssl_prepare_clienthello_tlsext(SSL *s)
2966
0
{
2967
2968
# ifdef TLSEXT_TYPE_opaque_prf_input
2969
    {
2970
        int r = 1;
2971
2972
        if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
2973
            r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
2974
                                                         s->
2975
                                                         ctx->tlsext_opaque_prf_input_callback_arg);
2976
            if (!r)
2977
                return -1;
2978
        }
2979
2980
        if (s->tlsext_opaque_prf_input != NULL) {
2981
            if (s->s3->client_opaque_prf_input != NULL) {
2982
                /* shouldn't really happen */
2983
                OPENSSL_free(s->s3->client_opaque_prf_input);
2984
            }
2985
2986
            if (s->tlsext_opaque_prf_input_len == 0) {
2987
                /* dummy byte just to get non-NULL */
2988
                s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
2989
            } else {
2990
                s->s3->client_opaque_prf_input =
2991
                    BUF_memdup(s->tlsext_opaque_prf_input,
2992
                               s->tlsext_opaque_prf_input_len);
2993
            }
2994
            if (s->s3->client_opaque_prf_input == NULL) {
2995
                SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
2996
                       ERR_R_MALLOC_FAILURE);
2997
                return -1;
2998
            }
2999
            s->s3->client_opaque_prf_input_len =
3000
                s->tlsext_opaque_prf_input_len;
3001
        }
3002
3003
        if (r == 2)
3004
            /*
3005
             * at callback's request, insist on receiving an appropriate
3006
             * server opaque PRF input
3007
             */
3008
            s->s3->server_opaque_prf_input_len =
3009
                s->tlsext_opaque_prf_input_len;
3010
    }
3011
# endif
3012
3013
0
    s->cert->alpn_sent = 0;
3014
0
    return 1;
3015
0
}
3016
3017
int ssl_prepare_serverhello_tlsext(SSL *s)
3018
0
{
3019
0
    return 1;
3020
0
}
3021
3022
static int ssl_check_clienthello_tlsext_early(SSL *s)
3023
0
{
3024
0
    int ret = SSL_TLSEXT_ERR_NOACK;
3025
0
    int al = SSL_AD_UNRECOGNIZED_NAME;
3026
3027
0
# ifndef OPENSSL_NO_EC
3028
    /*
3029
     * The handling of the ECPointFormats extension is done elsewhere, namely
3030
     * in ssl3_choose_cipher in s3_lib.c.
3031
     */
3032
    /*
3033
     * The handling of the EllipticCurves extension is done elsewhere, namely
3034
     * in ssl3_choose_cipher in s3_lib.c.
3035
     */
3036
0
# endif
3037
3038
0
    if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3039
0
        ret =
3040
0
            s->ctx->tlsext_servername_callback(s, &al,
3041
0
                                               s->ctx->tlsext_servername_arg);
3042
0
    else if (s->initial_ctx != NULL
3043
0
             && s->initial_ctx->tlsext_servername_callback != 0)
3044
0
        ret =
3045
0
            s->initial_ctx->tlsext_servername_callback(s, &al,
3046
0
                                                       s->
3047
0
                                                       initial_ctx->tlsext_servername_arg);
3048
3049
# ifdef TLSEXT_TYPE_opaque_prf_input
3050
    {
3051
        /*
3052
         * This sort of belongs into ssl_prepare_serverhello_tlsext(), but we
3053
         * might be sending an alert in response to the client hello, so this
3054
         * has to happen here in ssl_check_clienthello_tlsext_early().
3055
         */
3056
3057
        int r = 1;
3058
3059
        if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
3060
            r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
3061
                                                         s->
3062
                                                         ctx->tlsext_opaque_prf_input_callback_arg);
3063
            if (!r) {
3064
                ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3065
                al = SSL_AD_INTERNAL_ERROR;
3066
                goto err;
3067
            }
3068
        }
3069
3070
        if (s->s3->server_opaque_prf_input != NULL) {
3071
            /* shouldn't really happen */
3072
            OPENSSL_free(s->s3->server_opaque_prf_input);
3073
        }
3074
        s->s3->server_opaque_prf_input = NULL;
3075
3076
        if (s->tlsext_opaque_prf_input != NULL) {
3077
            if (s->s3->client_opaque_prf_input != NULL &&
3078
                s->s3->client_opaque_prf_input_len ==
3079
                s->tlsext_opaque_prf_input_len) {
3080
                /*
3081
                 * can only use this extension if we have a server opaque PRF
3082
                 * input of the same length as the client opaque PRF input!
3083
                 */
3084
3085
                if (s->tlsext_opaque_prf_input_len == 0) {
3086
                    /* dummy byte just to get non-NULL */
3087
                    s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
3088
                } else {
3089
                    s->s3->server_opaque_prf_input =
3090
                        BUF_memdup(s->tlsext_opaque_prf_input,
3091
                                   s->tlsext_opaque_prf_input_len);
3092
                }
3093
                if (s->s3->server_opaque_prf_input == NULL) {
3094
                    ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3095
                    al = SSL_AD_INTERNAL_ERROR;
3096
                    goto err;
3097
                }
3098
                s->s3->server_opaque_prf_input_len =
3099
                    s->tlsext_opaque_prf_input_len;
3100
            }
3101
        }
3102
3103
        if (r == 2 && s->s3->server_opaque_prf_input == NULL) {
3104
            /*
3105
             * The callback wants to enforce use of the extension, but we
3106
             * can't do that with the client opaque PRF input; abort the
3107
             * handshake.
3108
             */
3109
            ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3110
            al = SSL_AD_HANDSHAKE_FAILURE;
3111
        }
3112
    }
3113
3114
 err:
3115
# endif
3116
0
    switch (ret) {
3117
0
    case SSL_TLSEXT_ERR_ALERT_FATAL:
3118
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
3119
0
        return -1;
3120
3121
0
    case SSL_TLSEXT_ERR_ALERT_WARNING:
3122
0
        ssl3_send_alert(s, SSL3_AL_WARNING, al);
3123
0
        return 1;
3124
3125
0
    case SSL_TLSEXT_ERR_NOACK:
3126
0
        s->servername_done = 0;
3127
0
    default:
3128
0
        return 1;
3129
0
    }
3130
0
}
3131
3132
int tls1_set_server_sigalgs(SSL *s)
3133
0
{
3134
0
    int al;
3135
0
    size_t i;
3136
    /* Clear any shared sigtnature algorithms */
3137
0
    if (s->cert->shared_sigalgs) {
3138
0
        OPENSSL_free(s->cert->shared_sigalgs);
3139
0
        s->cert->shared_sigalgs = NULL;
3140
0
        s->cert->shared_sigalgslen = 0;
3141
0
    }
3142
    /* Clear certificate digests and validity flags */
3143
0
    for (i = 0; i < SSL_PKEY_NUM; i++) {
3144
0
        s->cert->pkeys[i].digest = NULL;
3145
0
        s->cert->pkeys[i].valid_flags = 0;
3146
0
    }
3147
3148
    /* If sigalgs received process it. */
3149
0
    if (s->cert->peer_sigalgs) {
3150
0
        if (!tls1_process_sigalgs(s)) {
3151
0
            SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
3152
0
            al = SSL_AD_INTERNAL_ERROR;
3153
0
            goto err;
3154
0
        }
3155
        /* Fatal error is no shared signature algorithms */
3156
0
        if (!s->cert->shared_sigalgs) {
3157
0
            SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
3158
0
                   SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
3159
0
            al = SSL_AD_ILLEGAL_PARAMETER;
3160
0
            goto err;
3161
0
        }
3162
0
    } else
3163
0
        ssl_cert_set_default_md(s->cert);
3164
0
    return 1;
3165
0
 err:
3166
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
3167
0
    return 0;
3168
0
}
3169
3170
/*
3171
 * Upon success, returns 1.
3172
 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
3173
 */
3174
int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
3175
0
{
3176
3177
    /*
3178
     * If status request then ask callback what to do. Note: this must be
3179
     * called after servername callbacks in case the certificate has changed,
3180
     * and must be called after the cipher has been chosen because this may
3181
     * influence which certificate is sent
3182
     */
3183
0
    if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
3184
0
        int ret;
3185
0
        CERT_PKEY *certpkey;
3186
0
        certpkey = ssl_get_server_send_pkey(s);
3187
        /* If no certificate can't return certificate status */
3188
0
        if (certpkey != NULL) {
3189
            /*
3190
             * Set current certificate to one we will use so SSL_get_certificate
3191
             * et al can pick it up.
3192
             */
3193
0
            s->cert->key = certpkey;
3194
0
            ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3195
0
            switch (ret) {
3196
                /* We don't want to send a status request response */
3197
0
            case SSL_TLSEXT_ERR_NOACK:
3198
0
                s->tlsext_status_expected = 0;
3199
0
                break;
3200
                /* status request response should be sent */
3201
0
            case SSL_TLSEXT_ERR_OK:
3202
0
                if (s->tlsext_ocsp_resp)
3203
0
                    s->tlsext_status_expected = 1;
3204
0
                break;
3205
                /* something bad happened */
3206
0
            case SSL_TLSEXT_ERR_ALERT_FATAL:
3207
0
            default:
3208
0
                *al = SSL_AD_INTERNAL_ERROR;
3209
0
                return 0;
3210
0
            }
3211
0
        }
3212
0
    }
3213
3214
0
    if (!tls1_alpn_handle_client_hello_late(s, al)) {
3215
0
        return 0;
3216
0
    }
3217
3218
0
    return 1;
3219
0
}
3220
3221
int ssl_check_serverhello_tlsext(SSL *s)
3222
0
{
3223
0
    int ret = SSL_TLSEXT_ERR_NOACK;
3224
0
    int al = SSL_AD_UNRECOGNIZED_NAME;
3225
3226
0
# ifndef OPENSSL_NO_EC
3227
    /*
3228
     * If we are client and using an elliptic curve cryptography cipher
3229
     * suite, then if server returns an EC point formats lists extension it
3230
     * must contain uncompressed.
3231
     */
3232
0
    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3233
0
    unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3234
0
    if ((s->tlsext_ecpointformatlist != NULL)
3235
0
        && (s->tlsext_ecpointformatlist_length > 0)
3236
0
        && (s->session->tlsext_ecpointformatlist != NULL)
3237
0
        && (s->session->tlsext_ecpointformatlist_length > 0)
3238
0
        && ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
3239
0
            || (alg_a & SSL_aECDSA))) {
3240
        /* we are using an ECC cipher */
3241
0
        size_t i;
3242
0
        unsigned char *list;
3243
0
        int found_uncompressed = 0;
3244
0
        list = s->session->tlsext_ecpointformatlist;
3245
0
        for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
3246
0
            if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
3247
0
                found_uncompressed = 1;
3248
0
                break;
3249
0
            }
3250
0
        }
3251
0
        if (!found_uncompressed) {
3252
0
            SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
3253
0
                   SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3254
0
            return -1;
3255
0
        }
3256
0
    }
3257
0
    ret = SSL_TLSEXT_ERR_OK;
3258
0
# endif                         /* OPENSSL_NO_EC */
3259
3260
0
    if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3261
0
        ret =
3262
0
            s->ctx->tlsext_servername_callback(s, &al,
3263
0
                                               s->ctx->tlsext_servername_arg);
3264
0
    else if (s->initial_ctx != NULL
3265
0
             && s->initial_ctx->tlsext_servername_callback != 0)
3266
0
        ret =
3267
0
            s->initial_ctx->tlsext_servername_callback(s, &al,
3268
0
                                                       s->
3269
0
                                                       initial_ctx->tlsext_servername_arg);
3270
3271
# ifdef TLSEXT_TYPE_opaque_prf_input
3272
    if (s->s3->server_opaque_prf_input_len > 0) {
3273
        /*
3274
         * This case may indicate that we, as a client, want to insist on
3275
         * using opaque PRF inputs. So first verify that we really have a
3276
         * value from the server too.
3277
         */
3278
3279
        if (s->s3->server_opaque_prf_input == NULL) {
3280
            ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3281
            al = SSL_AD_HANDSHAKE_FAILURE;
3282
        }
3283
3284
        /*
3285
         * Anytime the server *has* sent an opaque PRF input, we need to
3286
         * check that we have a client opaque PRF input of the same size.
3287
         */
3288
        if (s->s3->client_opaque_prf_input == NULL ||
3289
            s->s3->client_opaque_prf_input_len !=
3290
            s->s3->server_opaque_prf_input_len) {
3291
            ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3292
            al = SSL_AD_ILLEGAL_PARAMETER;
3293
        }
3294
    }
3295
# endif
3296
3297
0
    OPENSSL_free(s->tlsext_ocsp_resp);
3298
0
    s->tlsext_ocsp_resp = NULL;
3299
0
    s->tlsext_ocsp_resplen = -1;
3300
    /*
3301
     * If we've requested certificate status and we wont get one tell the
3302
     * callback
3303
     */
3304
0
    if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3305
0
        && !(s->hit) && s->ctx && s->ctx->tlsext_status_cb) {
3306
0
        int r;
3307
        /*
3308
         * Call callback with resp == NULL and resplen == -1 so callback
3309
         * knows there is no response
3310
         */
3311
0
        r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3312
0
        if (r == 0) {
3313
0
            al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3314
0
            ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3315
0
        }
3316
0
        if (r < 0) {
3317
0
            al = SSL_AD_INTERNAL_ERROR;
3318
0
            ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3319
0
        }
3320
0
    }
3321
3322
0
    switch (ret) {
3323
0
    case SSL_TLSEXT_ERR_ALERT_FATAL:
3324
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
3325
0
        return -1;
3326
3327
0
    case SSL_TLSEXT_ERR_ALERT_WARNING:
3328
0
        ssl3_send_alert(s, SSL3_AL_WARNING, al);
3329
0
        return 1;
3330
3331
0
    case SSL_TLSEXT_ERR_NOACK:
3332
0
        s->servername_done = 0;
3333
0
    default:
3334
0
        return 1;
3335
0
    }
3336
0
}
3337
3338
int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
3339
                                 int n)
3340
0
{
3341
0
    int al = -1;
3342
0
    if (s->version < SSL3_VERSION)
3343
0
        return 1;
3344
0
    if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0) {
3345
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
3346
0
        return 0;
3347
0
    }
3348
3349
0
    if (ssl_check_serverhello_tlsext(s) <= 0) {
3350
0
        SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
3351
0
        return 0;
3352
0
    }
3353
0
    return 1;
3354
0
}
3355
3356
/*-
3357
 * Since the server cache lookup is done early on in the processing of the
3358
 * ClientHello, and other operations depend on the result, we need to handle
3359
 * any TLS session ticket extension at the same time.
3360
 *
3361
 *   session_id: points at the session ID in the ClientHello. This code will
3362
 *       read past the end of this in order to parse out the session ticket
3363
 *       extension, if any.
3364
 *   len: the length of the session ID.
3365
 *   limit: a pointer to the first byte after the ClientHello.
3366
 *   ret: (output) on return, if a ticket was decrypted, then this is set to
3367
 *       point to the resulting session.
3368
 *
3369
 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3370
 * ciphersuite, in which case we have no use for session tickets and one will
3371
 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3372
 *
3373
 * Returns:
3374
 *   -1: fatal error, either from parsing or decrypting the ticket.
3375
 *    0: no ticket was found (or was ignored, based on settings).
3376
 *    1: a zero length extension was found, indicating that the client supports
3377
 *       session tickets but doesn't currently have one to offer.
3378
 *    2: either s->tls_session_secret_cb was set, or a ticket was offered but
3379
 *       couldn't be decrypted because of a non-fatal error.
3380
 *    3: a ticket was successfully decrypted and *ret was set.
3381
 *
3382
 * Side effects:
3383
 *   Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3384
 *   a new session ticket to the client because the client indicated support
3385
 *   (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3386
 *   a session ticket or we couldn't use the one it gave us, or if
3387
 *   s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3388
 *   Otherwise, s->tlsext_ticket_expected is set to 0.
3389
 */
3390
int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3391
                        const unsigned char *limit, SSL_SESSION **ret)
3392
0
{
3393
    /* Point after session ID in client hello */
3394
0
    const unsigned char *p = session_id + len;
3395
0
    unsigned short i;
3396
3397
0
    *ret = NULL;
3398
0
    s->tlsext_ticket_expected = 0;
3399
3400
    /*
3401
     * If tickets disabled behave as if no ticket present to permit stateful
3402
     * resumption.
3403
     */
3404
0
    if (SSL_get_options(s) & SSL_OP_NO_TICKET)
3405
0
        return 0;
3406
0
    if ((s->version <= SSL3_VERSION) || !limit)
3407
0
        return 0;
3408
0
    if (p >= limit)
3409
0
        return -1;
3410
    /* Skip past DTLS cookie */
3411
0
    if (SSL_IS_DTLS(s)) {
3412
0
        i = *(p++);
3413
3414
0
        if (limit - p <= i)
3415
0
            return -1;
3416
3417
0
        p += i;
3418
0
    }
3419
    /* Skip past cipher list */
3420
0
    n2s(p, i);
3421
0
    if (limit - p <= i)
3422
0
        return -1;
3423
0
    p += i;
3424
3425
    /* Skip past compression algorithm list */
3426
0
    i = *(p++);
3427
0
    if (limit - p < i)
3428
0
        return -1;
3429
0
    p += i;
3430
3431
    /* Now at start of extensions */
3432
0
    if (limit - p <= 2)
3433
0
        return 0;
3434
0
    n2s(p, i);
3435
0
    while (limit - p >= 4) {
3436
0
        unsigned short type, size;
3437
0
        n2s(p, type);
3438
0
        n2s(p, size);
3439
0
        if (limit - p < size)
3440
0
            return 0;
3441
0
        if (type == TLSEXT_TYPE_session_ticket) {
3442
0
            int r;
3443
0
            if (size == 0) {
3444
                /*
3445
                 * The client will accept a ticket but doesn't currently have
3446
                 * one.
3447
                 */
3448
0
                s->tlsext_ticket_expected = 1;
3449
0
                return 1;
3450
0
            }
3451
0
            if (s->tls_session_secret_cb) {
3452
                /*
3453
                 * Indicate that the ticket couldn't be decrypted rather than
3454
                 * generating the session from ticket now, trigger
3455
                 * abbreviated handshake based on external mechanism to
3456
                 * calculate the master secret later.
3457
                 */
3458
0
                return 2;
3459
0
            }
3460
0
            r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3461
0
            switch (r) {
3462
0
            case 2:            /* ticket couldn't be decrypted */
3463
0
                s->tlsext_ticket_expected = 1;
3464
0
                return 2;
3465
0
            case 3:            /* ticket was decrypted */
3466
0
                return r;
3467
0
            case 4:            /* ticket decrypted but need to renew */
3468
0
                s->tlsext_ticket_expected = 1;
3469
0
                return 3;
3470
0
            default:           /* fatal error */
3471
0
                return -1;
3472
0
            }
3473
0
        }
3474
0
        p += size;
3475
0
    }
3476
0
    return 0;
3477
0
}
3478
3479
/*-
3480
 * tls_decrypt_ticket attempts to decrypt a session ticket.
3481
 *
3482
 *   etick: points to the body of the session ticket extension.
3483
 *   eticklen: the length of the session tickets extenion.
3484
 *   sess_id: points at the session ID.
3485
 *   sesslen: the length of the session ID.
3486
 *   psess: (output) on return, if a ticket was decrypted, then this is set to
3487
 *       point to the resulting session.
3488
 *
3489
 * Returns:
3490
 *   -1: fatal error, either from parsing or decrypting the ticket.
3491
 *    2: the ticket couldn't be decrypted.
3492
 *    3: a ticket was successfully decrypted and *psess was set.
3493
 *    4: same as 3, but the ticket needs to be renewed.
3494
 */
3495
static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3496
                              int eticklen, const unsigned char *sess_id,
3497
                              int sesslen, SSL_SESSION **psess)
3498
0
{
3499
0
    SSL_SESSION *sess;
3500
0
    unsigned char *sdec;
3501
0
    const unsigned char *p;
3502
0
    int slen, mlen, renew_ticket = 0;
3503
0
    unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3504
0
    HMAC_CTX hctx;
3505
0
    EVP_CIPHER_CTX ctx;
3506
0
    SSL_CTX *tctx = s->initial_ctx;
3507
3508
    /* Initialize session ticket encryption and HMAC contexts */
3509
0
    HMAC_CTX_init(&hctx);
3510
0
    EVP_CIPHER_CTX_init(&ctx);
3511
0
    if (tctx->tlsext_ticket_key_cb) {
3512
0
        unsigned char *nctick = (unsigned char *)etick;
3513
0
        int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3514
0
                                            &ctx, &hctx, 0);
3515
0
        if (rv < 0)
3516
0
            return -1;
3517
0
        if (rv == 0)
3518
0
            return 2;
3519
0
        if (rv == 2)
3520
0
            renew_ticket = 1;
3521
0
    } else {
3522
        /* Check key name matches */
3523
0
        if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3524
0
            return 2;
3525
0
        if (HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3526
0
                         tlsext_tick_md(), NULL) <= 0
3527
0
                || EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3528
0
                                      tctx->tlsext_tick_aes_key,
3529
0
                                      etick + 16) <= 0) {
3530
0
            goto err;
3531
0
       }
3532
0
    }
3533
    /*
3534
     * Attempt to process session ticket, first conduct sanity and integrity
3535
     * checks on ticket.
3536
     */
3537
0
    mlen = HMAC_size(&hctx);
3538
0
    if (mlen < 0) {
3539
0
        goto err;
3540
0
    }
3541
    /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3542
0
    if (eticklen <= 16 + EVP_CIPHER_CTX_iv_length(&ctx) + mlen) {
3543
0
        HMAC_CTX_cleanup(&hctx);
3544
0
        EVP_CIPHER_CTX_cleanup(&ctx);
3545
0
        return 2;
3546
0
    }
3547
3548
0
    eticklen -= mlen;
3549
    /* Check HMAC of encrypted ticket */
3550
0
    if (HMAC_Update(&hctx, etick, eticklen) <= 0
3551
0
            || HMAC_Final(&hctx, tick_hmac, NULL) <= 0) {
3552
0
        goto err;
3553
0
    }
3554
0
    HMAC_CTX_cleanup(&hctx);
3555
0
    if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3556
0
        EVP_CIPHER_CTX_cleanup(&ctx);
3557
0
        return 2;
3558
0
    }
3559
    /* Attempt to decrypt session data */
3560
    /* Move p after IV to start of encrypted ticket, update length */
3561
0
    p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3562
0
    eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3563
0
    sdec = OPENSSL_malloc(eticklen);
3564
0
    if (sdec == NULL
3565
0
            || EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) {
3566
0
        EVP_CIPHER_CTX_cleanup(&ctx);
3567
0
        OPENSSL_free(sdec);
3568
0
        return -1;
3569
0
    }
3570
0
    if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
3571
0
        EVP_CIPHER_CTX_cleanup(&ctx);
3572
0
        OPENSSL_free(sdec);
3573
0
        return 2;
3574
0
    }
3575
0
    slen += mlen;
3576
0
    EVP_CIPHER_CTX_cleanup(&ctx);
3577
0
    p = sdec;
3578
3579
0
    sess = d2i_SSL_SESSION(NULL, &p, slen);
3580
0
    slen -= p - sdec;
3581
0
    OPENSSL_free(sdec);
3582
0
    if (sess) {
3583
        /* Some additional consistency checks */
3584
0
        if (slen != 0 || sess->session_id_length != 0) {
3585
0
            SSL_SESSION_free(sess);
3586
0
            return 2;
3587
0
        }
3588
        /*
3589
         * The session ID, if non-empty, is used by some clients to detect
3590
         * that the ticket has been accepted. So we copy it to the session
3591
         * structure. If it is empty set length to zero as required by
3592
         * standard.
3593
         */
3594
0
        if (sesslen)
3595
0
            memcpy(sess->session_id, sess_id, sesslen);
3596
0
        sess->session_id_length = sesslen;
3597
0
        *psess = sess;
3598
0
        if (renew_ticket)
3599
0
            return 4;
3600
0
        else
3601
0
            return 3;
3602
0
    }
3603
0
    ERR_clear_error();
3604
    /*
3605
     * For session parse failure, indicate that we need to send a new ticket.
3606
     */
3607
0
    return 2;
3608
0
err:
3609
0
    EVP_CIPHER_CTX_cleanup(&ctx);
3610
0
    HMAC_CTX_cleanup(&hctx);
3611
0
    return -1;
3612
0
}
3613
3614
/* Tables to translate from NIDs to TLS v1.2 ids */
3615
3616
typedef struct {
3617
    int nid;
3618
    int id;
3619
} tls12_lookup;
3620
3621
static tls12_lookup tls12_md[] = {
3622
    {NID_md5, TLSEXT_hash_md5},
3623
    {NID_sha1, TLSEXT_hash_sha1},
3624
    {NID_sha224, TLSEXT_hash_sha224},
3625
    {NID_sha256, TLSEXT_hash_sha256},
3626
    {NID_sha384, TLSEXT_hash_sha384},
3627
    {NID_sha512, TLSEXT_hash_sha512}
3628
};
3629
3630
static tls12_lookup tls12_sig[] = {
3631
    {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3632
    {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3633
    {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3634
};
3635
3636
static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3637
0
{
3638
0
    size_t i;
3639
0
    for (i = 0; i < tlen; i++) {
3640
0
        if (table[i].nid == nid)
3641
0
            return table[i].id;
3642
0
    }
3643
0
    return -1;
3644
0
}
3645
3646
static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3647
0
{
3648
0
    size_t i;
3649
0
    for (i = 0; i < tlen; i++) {
3650
0
        if ((table[i].id) == id)
3651
0
            return table[i].nid;
3652
0
    }
3653
0
    return NID_undef;
3654
0
}
3655
3656
int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3657
                         const EVP_MD *md)
3658
0
{
3659
0
    int sig_id, md_id;
3660
0
    if (!md)
3661
0
        return 0;
3662
0
    md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3663
0
                          sizeof(tls12_md) / sizeof(tls12_lookup));
3664
0
    if (md_id == -1)
3665
0
        return 0;
3666
0
    sig_id = tls12_get_sigid(pk);
3667
0
    if (sig_id == -1)
3668
0
        return 0;
3669
0
    p[0] = (unsigned char)md_id;
3670
0
    p[1] = (unsigned char)sig_id;
3671
0
    return 1;
3672
0
}
3673
3674
int tls12_get_sigid(const EVP_PKEY *pk)
3675
0
{
3676
0
    return tls12_find_id(pk->type, tls12_sig,
3677
0
                         sizeof(tls12_sig) / sizeof(tls12_lookup));
3678
0
}
3679
3680
const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3681
0
{
3682
0
    switch (hash_alg) {
3683
0
# ifndef OPENSSL_NO_MD5
3684
0
    case TLSEXT_hash_md5:
3685
#  ifdef OPENSSL_FIPS
3686
        if (FIPS_mode())
3687
            return NULL;
3688
#  endif
3689
0
        return EVP_md5();
3690
0
# endif
3691
0
# ifndef OPENSSL_NO_SHA
3692
0
    case TLSEXT_hash_sha1:
3693
0
        return EVP_sha1();
3694
0
# endif
3695
0
# ifndef OPENSSL_NO_SHA256
3696
0
    case TLSEXT_hash_sha224:
3697
0
        return EVP_sha224();
3698
3699
0
    case TLSEXT_hash_sha256:
3700
0
        return EVP_sha256();
3701
0
# endif
3702
0
# ifndef OPENSSL_NO_SHA512
3703
0
    case TLSEXT_hash_sha384:
3704
0
        return EVP_sha384();
3705
3706
0
    case TLSEXT_hash_sha512:
3707
0
        return EVP_sha512();
3708
0
# endif
3709
0
    default:
3710
0
        return NULL;
3711
3712
0
    }
3713
0
}
3714
3715
static int tls12_get_pkey_idx(unsigned char sig_alg)
3716
0
{
3717
0
    switch (sig_alg) {
3718
0
# ifndef OPENSSL_NO_RSA
3719
0
    case TLSEXT_signature_rsa:
3720
0
        return SSL_PKEY_RSA_SIGN;
3721
0
# endif
3722
0
# ifndef OPENSSL_NO_DSA
3723
0
    case TLSEXT_signature_dsa:
3724
0
        return SSL_PKEY_DSA_SIGN;
3725
0
# endif
3726
0
# ifndef OPENSSL_NO_ECDSA
3727
0
    case TLSEXT_signature_ecdsa:
3728
0
        return SSL_PKEY_ECC;
3729
0
# endif
3730
0
    }
3731
0
    return -1;
3732
0
}
3733
3734
/* Convert TLS 1.2 signature algorithm extension values into NIDs */
3735
static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3736
                               int *psignhash_nid, const unsigned char *data)
3737
0
{
3738
0
    int sign_nid = NID_undef, hash_nid = NID_undef;
3739
0
    if (!phash_nid && !psign_nid && !psignhash_nid)
3740
0
        return;
3741
0
    if (phash_nid || psignhash_nid) {
3742
0
        hash_nid = tls12_find_nid(data[0], tls12_md,
3743
0
                                  sizeof(tls12_md) / sizeof(tls12_lookup));
3744
0
        if (phash_nid)
3745
0
            *phash_nid = hash_nid;
3746
0
    }
3747
0
    if (psign_nid || psignhash_nid) {
3748
0
        sign_nid = tls12_find_nid(data[1], tls12_sig,
3749
0
                                  sizeof(tls12_sig) / sizeof(tls12_lookup));
3750
0
        if (psign_nid)
3751
0
            *psign_nid = sign_nid;
3752
0
    }
3753
0
    if (psignhash_nid) {
3754
0
        if (sign_nid == NID_undef || hash_nid == NID_undef
3755
0
                || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3756
0
                                          sign_nid) <= 0)
3757
0
            *psignhash_nid = NID_undef;
3758
0
    }
3759
0
}
3760
3761
/* Given preference and allowed sigalgs set shared sigalgs */
3762
static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3763
                                   const unsigned char *pref, size_t preflen,
3764
                                   const unsigned char *allow,
3765
                                   size_t allowlen)
3766
0
{
3767
0
    const unsigned char *ptmp, *atmp;
3768
0
    size_t i, j, nmatch = 0;
3769
0
    for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3770
        /* Skip disabled hashes or signature algorithms */
3771
0
        if (tls12_get_hash(ptmp[0]) == NULL)
3772
0
            continue;
3773
0
        if (tls12_get_pkey_idx(ptmp[1]) == -1)
3774
0
            continue;
3775
0
        for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3776
0
            if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3777
0
                nmatch++;
3778
0
                if (shsig) {
3779
0
                    shsig->rhash = ptmp[0];
3780
0
                    shsig->rsign = ptmp[1];
3781
0
                    tls1_lookup_sigalg(&shsig->hash_nid,
3782
0
                                       &shsig->sign_nid,
3783
0
                                       &shsig->signandhash_nid, ptmp);
3784
0
                    shsig++;
3785
0
                }
3786
0
                break;
3787
0
            }
3788
0
        }
3789
0
    }
3790
0
    return nmatch;
3791
0
}
3792
3793
/* Set shared signature algorithms for SSL structures */
3794
static int tls1_set_shared_sigalgs(SSL *s)
3795
0
{
3796
0
    const unsigned char *pref, *allow, *conf;
3797
0
    size_t preflen, allowlen, conflen;
3798
0
    size_t nmatch;
3799
0
    TLS_SIGALGS *salgs = NULL;
3800
0
    CERT *c = s->cert;
3801
0
    unsigned int is_suiteb = tls1_suiteb(s);
3802
0
    if (c->shared_sigalgs) {
3803
0
        OPENSSL_free(c->shared_sigalgs);
3804
0
        c->shared_sigalgs = NULL;
3805
0
        c->shared_sigalgslen = 0;
3806
0
    }
3807
    /* If client use client signature algorithms if not NULL */
3808
0
    if (!s->server && c->client_sigalgs && !is_suiteb) {
3809
0
        conf = c->client_sigalgs;
3810
0
        conflen = c->client_sigalgslen;
3811
0
    } else if (c->conf_sigalgs && !is_suiteb) {
3812
0
        conf = c->conf_sigalgs;
3813
0
        conflen = c->conf_sigalgslen;
3814
0
    } else
3815
0
        conflen = tls12_get_psigalgs(s, 0, &conf);
3816
0
    if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3817
0
        pref = conf;
3818
0
        preflen = conflen;
3819
0
        allow = c->peer_sigalgs;
3820
0
        allowlen = c->peer_sigalgslen;
3821
0
    } else {
3822
0
        allow = conf;
3823
0
        allowlen = conflen;
3824
0
        pref = c->peer_sigalgs;
3825
0
        preflen = c->peer_sigalgslen;
3826
0
    }
3827
0
    nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
3828
0
    if (nmatch) {
3829
0
        salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3830
0
        if (!salgs)
3831
0
            return 0;
3832
0
        nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
3833
0
    } else {
3834
0
        salgs = NULL;
3835
0
    }
3836
0
    c->shared_sigalgs = salgs;
3837
0
    c->shared_sigalgslen = nmatch;
3838
0
    return 1;
3839
0
}
3840
3841
/* Set preferred digest for each key type */
3842
3843
int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3844
0
{
3845
0
    CERT *c = s->cert;
3846
    /* Extension ignored for inappropriate versions */
3847
0
    if (!SSL_USE_SIGALGS(s))
3848
0
        return 1;
3849
    /* Should never happen */
3850
0
    if (!c)
3851
0
        return 0;
3852
3853
0
    if (c->peer_sigalgs)
3854
0
        OPENSSL_free(c->peer_sigalgs);
3855
0
    c->peer_sigalgs = OPENSSL_malloc(dsize);
3856
0
    if (!c->peer_sigalgs)
3857
0
        return 0;
3858
0
    c->peer_sigalgslen = dsize;
3859
0
    memcpy(c->peer_sigalgs, data, dsize);
3860
0
    return 1;
3861
0
}
3862
3863
int tls1_process_sigalgs(SSL *s)
3864
0
{
3865
0
    int idx;
3866
0
    size_t i;
3867
0
    const EVP_MD *md;
3868
0
    CERT *c = s->cert;
3869
0
    TLS_SIGALGS *sigptr;
3870
0
    if (!tls1_set_shared_sigalgs(s))
3871
0
        return 0;
3872
3873
# ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3874
    if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3875
        /*
3876
         * Use first set signature preference to force message digest,
3877
         * ignoring any peer preferences.
3878
         */
3879
        const unsigned char *sigs = NULL;
3880
        if (s->server)
3881
            sigs = c->conf_sigalgs;
3882
        else
3883
            sigs = c->client_sigalgs;
3884
        if (sigs) {
3885
            idx = tls12_get_pkey_idx(sigs[1]);
3886
            md = tls12_get_hash(sigs[0]);
3887
            c->pkeys[idx].digest = md;
3888
            c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3889
            if (idx == SSL_PKEY_RSA_SIGN) {
3890
                c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
3891
                    CERT_PKEY_EXPLICIT_SIGN;
3892
                c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3893
            }
3894
        }
3895
    }
3896
# endif
3897
3898
0
    for (i = 0, sigptr = c->shared_sigalgs;
3899
0
         i < c->shared_sigalgslen; i++, sigptr++) {
3900
0
        idx = tls12_get_pkey_idx(sigptr->rsign);
3901
0
        if (idx > 0 && c->pkeys[idx].digest == NULL) {
3902
0
            md = tls12_get_hash(sigptr->rhash);
3903
0
            c->pkeys[idx].digest = md;
3904
0
            c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3905
0
            if (idx == SSL_PKEY_RSA_SIGN) {
3906
0
                c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
3907
0
                    CERT_PKEY_EXPLICIT_SIGN;
3908
0
                c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3909
0
            }
3910
0
        }
3911
3912
0
    }
3913
    /*
3914
     * In strict mode leave unset digests as NULL to indicate we can't use
3915
     * the certificate for signing.
3916
     */
3917
0
    if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3918
        /*
3919
         * Set any remaining keys to default values. NOTE: if alg is not
3920
         * supported it stays as NULL.
3921
         */
3922
0
# ifndef OPENSSL_NO_DSA
3923
0
        if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
3924
0
            c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
3925
0
# endif
3926
0
# ifndef OPENSSL_NO_RSA
3927
0
        if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) {
3928
0
            c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
3929
0
            c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
3930
0
        }
3931
0
# endif
3932
0
# ifndef OPENSSL_NO_ECDSA
3933
0
        if (!c->pkeys[SSL_PKEY_ECC].digest)
3934
0
            c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
3935
0
# endif
3936
0
    }
3937
0
    return 1;
3938
0
}
3939
3940
int SSL_get_sigalgs(SSL *s, int idx,
3941
                    int *psign, int *phash, int *psignhash,
3942
                    unsigned char *rsig, unsigned char *rhash)
3943
0
{
3944
0
    const unsigned char *psig = s->cert->peer_sigalgs;
3945
0
    if (psig == NULL)
3946
0
        return 0;
3947
0
    if (idx >= 0) {
3948
0
        idx <<= 1;
3949
0
        if (idx >= (int)s->cert->peer_sigalgslen)
3950
0
            return 0;
3951
0
        psig += idx;
3952
0
        if (rhash)
3953
0
            *rhash = psig[0];
3954
0
        if (rsig)
3955
0
            *rsig = psig[1];
3956
0
        tls1_lookup_sigalg(phash, psign, psignhash, psig);
3957
0
    }
3958
0
    return s->cert->peer_sigalgslen / 2;
3959
0
}
3960
3961
int SSL_get_shared_sigalgs(SSL *s, int idx,
3962
                           int *psign, int *phash, int *psignhash,
3963
                           unsigned char *rsig, unsigned char *rhash)
3964
0
{
3965
0
    TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3966
0
    if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3967
0
        return 0;
3968
0
    shsigalgs += idx;
3969
0
    if (phash)
3970
0
        *phash = shsigalgs->hash_nid;
3971
0
    if (psign)
3972
0
        *psign = shsigalgs->sign_nid;
3973
0
    if (psignhash)
3974
0
        *psignhash = shsigalgs->signandhash_nid;
3975
0
    if (rsig)
3976
0
        *rsig = shsigalgs->rsign;
3977
0
    if (rhash)
3978
0
        *rhash = shsigalgs->rhash;
3979
0
    return s->cert->shared_sigalgslen;
3980
0
}
3981
3982
# ifndef OPENSSL_NO_HEARTBEATS
3983
int tls1_process_heartbeat(SSL *s)
3984
0
{
3985
0
    unsigned char *p = &s->s3->rrec.data[0], *pl;
3986
0
    unsigned short hbtype;
3987
0
    unsigned int payload;
3988
0
    unsigned int padding = 16;  /* Use minimum padding */
3989
3990
0
    if (s->msg_callback)
3991
0
        s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3992
0
                        &s->s3->rrec.data[0], s->s3->rrec.length,
3993
0
                        s, s->msg_callback_arg);
3994
3995
    /* Read type and payload length first */
3996
0
    if (1 + 2 + 16 > s->s3->rrec.length)
3997
0
        return 0;               /* silently discard */
3998
0
    hbtype = *p++;
3999
0
    n2s(p, payload);
4000
0
    if (1 + 2 + payload + 16 > s->s3->rrec.length)
4001
0
        return 0;               /* silently discard per RFC 6520 sec. 4 */
4002
0
    pl = p;
4003
4004
0
    if (hbtype == TLS1_HB_REQUEST) {
4005
0
        unsigned char *buffer, *bp;
4006
0
        int r;
4007
4008
        /*
4009
         * Allocate memory for the response, size is 1 bytes message type,
4010
         * plus 2 bytes payload length, plus payload, plus padding
4011
         */
4012
0
        buffer = OPENSSL_malloc(1 + 2 + payload + padding);
4013
0
        if (buffer == NULL)
4014
0
            return -1;
4015
0
        bp = buffer;
4016
4017
        /* Enter response type, length and copy payload */
4018
0
        *bp++ = TLS1_HB_RESPONSE;
4019
0
        s2n(payload, bp);
4020
0
        memcpy(bp, pl, payload);
4021
0
        bp += payload;
4022
        /* Random padding */
4023
0
        if (RAND_bytes(bp, padding) <= 0) {
4024
0
            OPENSSL_free(buffer);
4025
0
            return -1;
4026
0
        }
4027
4028
0
        r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
4029
0
                             3 + payload + padding);
4030
4031
0
        if (r >= 0 && s->msg_callback)
4032
0
            s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4033
0
                            buffer, 3 + payload + padding,
4034
0
                            s, s->msg_callback_arg);
4035
4036
0
        OPENSSL_free(buffer);
4037
4038
0
        if (r < 0)
4039
0
            return r;
4040
0
    } else if (hbtype == TLS1_HB_RESPONSE) {
4041
0
        unsigned int seq;
4042
4043
        /*
4044
         * We only send sequence numbers (2 bytes unsigned int), and 16
4045
         * random bytes, so we just try to read the sequence number
4046
         */
4047
0
        n2s(pl, seq);
4048
4049
0
        if (payload == 18 && seq == s->tlsext_hb_seq) {
4050
0
            s->tlsext_hb_seq++;
4051
0
            s->tlsext_hb_pending = 0;
4052
0
        }
4053
0
    }
4054
4055
0
    return 0;
4056
0
}
4057
4058
int tls1_heartbeat(SSL *s)
4059
0
{
4060
0
    unsigned char *buf, *p;
4061
0
    int ret = -1;
4062
0
    unsigned int payload = 18;  /* Sequence number + random bytes */
4063
0
    unsigned int padding = 16;  /* Use minimum padding */
4064
4065
    /* Only send if peer supports and accepts HB requests... */
4066
0
    if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
4067
0
        s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
4068
0
        SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
4069
0
        return -1;
4070
0
    }
4071
4072
    /* ...and there is none in flight yet... */
4073
0
    if (s->tlsext_hb_pending) {
4074
0
        SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
4075
0
        return -1;
4076
0
    }
4077
4078
    /* ...and no handshake in progress. */
4079
0
    if (SSL_in_init(s) || s->in_handshake) {
4080
0
        SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
4081
0
        return -1;
4082
0
    }
4083
4084
    /*
4085
     * Check if padding is too long, payload and padding must not exceed 2^14
4086
     * - 3 = 16381 bytes in total.
4087
     */
4088
0
    OPENSSL_assert(payload + padding <= 16381);
4089
4090
    /*-
4091
     * Create HeartBeat message, we just use a sequence number
4092
     * as payload to distuingish different messages and add
4093
     * some random stuff.
4094
     *  - Message Type, 1 byte
4095
     *  - Payload Length, 2 bytes (unsigned int)
4096
     *  - Payload, the sequence number (2 bytes uint)
4097
     *  - Payload, random bytes (16 bytes uint)
4098
     *  - Padding
4099
     */
4100
0
    buf = OPENSSL_malloc(1 + 2 + payload + padding);
4101
0
    if (buf == NULL)
4102
0
        return -1;
4103
0
    p = buf;
4104
    /* Message Type */
4105
0
    *p++ = TLS1_HB_REQUEST;
4106
    /* Payload length (18 bytes here) */
4107
0
    s2n(payload, p);
4108
    /* Sequence number */
4109
0
    s2n(s->tlsext_hb_seq, p);
4110
    /* 16 random bytes */
4111
0
    if (RAND_bytes(p, 16) <= 0) {
4112
0
        SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
4113
0
        goto err;
4114
0
    }
4115
0
    p += 16;
4116
    /* Random padding */
4117
0
    if (RAND_bytes(p, padding) <= 0) {
4118
0
        SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
4119
0
        goto err;
4120
0
    }
4121
4122
0
    ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
4123
0
    if (ret >= 0) {
4124
0
        if (s->msg_callback)
4125
0
            s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4126
0
                            buf, 3 + payload + padding,
4127
0
                            s, s->msg_callback_arg);
4128
4129
0
        s->tlsext_hb_pending = 1;
4130
0
    }
4131
4132
0
err:
4133
0
    OPENSSL_free(buf);
4134
4135
0
    return ret;
4136
0
}
4137
# endif
4138
4139
0
# define MAX_SIGALGLEN   (TLSEXT_hash_num * TLSEXT_signature_num * 2)
4140
4141
typedef struct {
4142
    size_t sigalgcnt;
4143
    int sigalgs[MAX_SIGALGLEN];
4144
} sig_cb_st;
4145
4146
static int sig_cb(const char *elem, int len, void *arg)
4147
0
{
4148
0
    sig_cb_st *sarg = arg;
4149
0
    size_t i;
4150
0
    char etmp[20], *p;
4151
0
    int sig_alg, hash_alg;
4152
0
    if (elem == NULL)
4153
0
        return 0;
4154
0
    if (sarg->sigalgcnt == MAX_SIGALGLEN)
4155
0
        return 0;
4156
0
    if (len > (int)(sizeof(etmp) - 1))
4157
0
        return 0;
4158
0
    memcpy(etmp, elem, len);
4159
0
    etmp[len] = 0;
4160
0
    p = strchr(etmp, '+');
4161
0
    if (!p)
4162
0
        return 0;
4163
0
    *p = 0;
4164
0
    p++;
4165
0
    if (!*p)
4166
0
        return 0;
4167
4168
0
    if (!strcmp(etmp, "RSA"))
4169
0
        sig_alg = EVP_PKEY_RSA;
4170
0
    else if (!strcmp(etmp, "DSA"))
4171
0
        sig_alg = EVP_PKEY_DSA;
4172
0
    else if (!strcmp(etmp, "ECDSA"))
4173
0
        sig_alg = EVP_PKEY_EC;
4174
0
    else
4175
0
        return 0;
4176
4177
0
    hash_alg = OBJ_sn2nid(p);
4178
0
    if (hash_alg == NID_undef)
4179
0
        hash_alg = OBJ_ln2nid(p);
4180
0
    if (hash_alg == NID_undef)
4181
0
        return 0;
4182
4183
0
    for (i = 0; i < sarg->sigalgcnt; i += 2) {
4184
0
        if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
4185
0
            return 0;
4186
0
    }
4187
0
    sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
4188
0
    sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
4189
0
    return 1;
4190
0
}
4191
4192
/*
4193
 * Set suppored signature algorithms based on a colon separated list of the
4194
 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
4195
 */
4196
int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
4197
0
{
4198
0
    sig_cb_st sig;
4199
0
    sig.sigalgcnt = 0;
4200
0
    if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
4201
0
        return 0;
4202
0
    if (c == NULL)
4203
0
        return 1;
4204
0
    return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
4205
0
}
4206
4207
int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
4208
                     int client)
4209
0
{
4210
0
    unsigned char *sigalgs, *sptr;
4211
0
    int rhash, rsign;
4212
0
    size_t i;
4213
0
    if (salglen & 1)
4214
0
        return 0;
4215
0
    sigalgs = OPENSSL_malloc(salglen);
4216
0
    if (sigalgs == NULL)
4217
0
        return 0;
4218
0
    for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
4219
0
        rhash = tls12_find_id(*psig_nids++, tls12_md,
4220
0
                              sizeof(tls12_md) / sizeof(tls12_lookup));
4221
0
        rsign = tls12_find_id(*psig_nids++, tls12_sig,
4222
0
                              sizeof(tls12_sig) / sizeof(tls12_lookup));
4223
4224
0
        if (rhash == -1 || rsign == -1)
4225
0
            goto err;
4226
0
        *sptr++ = rhash;
4227
0
        *sptr++ = rsign;
4228
0
    }
4229
4230
0
    if (client) {
4231
0
        if (c->client_sigalgs)
4232
0
            OPENSSL_free(c->client_sigalgs);
4233
0
        c->client_sigalgs = sigalgs;
4234
0
        c->client_sigalgslen = salglen;
4235
0
    } else {
4236
0
        if (c->conf_sigalgs)
4237
0
            OPENSSL_free(c->conf_sigalgs);
4238
0
        c->conf_sigalgs = sigalgs;
4239
0
        c->conf_sigalgslen = salglen;
4240
0
    }
4241
4242
0
    return 1;
4243
4244
0
 err:
4245
0
    OPENSSL_free(sigalgs);
4246
0
    return 0;
4247
0
}
4248
4249
static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
4250
0
{
4251
0
    int sig_nid;
4252
0
    size_t i;
4253
0
    if (default_nid == -1)
4254
0
        return 1;
4255
0
    sig_nid = X509_get_signature_nid(x);
4256
0
    if (default_nid)
4257
0
        return sig_nid == default_nid ? 1 : 0;
4258
0
    for (i = 0; i < c->shared_sigalgslen; i++)
4259
0
        if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
4260
0
            return 1;
4261
0
    return 0;
4262
0
}
4263
4264
/* Check to see if a certificate issuer name matches list of CA names */
4265
static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
4266
0
{
4267
0
    X509_NAME *nm;
4268
0
    int i;
4269
0
    nm = X509_get_issuer_name(x);
4270
0
    for (i = 0; i < sk_X509_NAME_num(names); i++) {
4271
0
        if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
4272
0
            return 1;
4273
0
    }
4274
0
    return 0;
4275
0
}
4276
4277
/*
4278
 * Check certificate chain is consistent with TLS extensions and is usable by
4279
 * server. This servers two purposes: it allows users to check chains before
4280
 * passing them to the server and it allows the server to check chains before
4281
 * attempting to use them.
4282
 */
4283
4284
/* Flags which need to be set for a certificate when stict mode not set */
4285
4286
# define CERT_PKEY_VALID_FLAGS \
4287
0
        (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
4288
/* Strict mode flags */
4289
# define CERT_PKEY_STRICT_FLAGS \
4290
0
         (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
4291
0
         | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
4292
4293
int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
4294
                     int idx)
4295
0
{
4296
0
    int i;
4297
0
    int rv = 0;
4298
0
    int check_flags = 0, strict_mode;
4299
0
    CERT_PKEY *cpk = NULL;
4300
0
    CERT *c = s->cert;
4301
0
    unsigned int suiteb_flags = tls1_suiteb(s);
4302
    /* idx == -1 means checking server chains */
4303
0
    if (idx != -1) {
4304
        /* idx == -2 means checking client certificate chains */
4305
0
        if (idx == -2) {
4306
0
            cpk = c->key;
4307
0
            idx = cpk - c->pkeys;
4308
0
        } else
4309
0
            cpk = c->pkeys + idx;
4310
0
        x = cpk->x509;
4311
0
        pk = cpk->privatekey;
4312
0
        chain = cpk->chain;
4313
0
        strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
4314
        /* If no cert or key, forget it */
4315
0
        if (!x || !pk)
4316
0
            goto end;
4317
# ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4318
        /* Allow any certificate to pass test */
4319
        if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
4320
            rv = CERT_PKEY_STRICT_FLAGS | CERT_PKEY_EXPLICIT_SIGN |
4321
                CERT_PKEY_VALID | CERT_PKEY_SIGN;
4322
            cpk->valid_flags = rv;
4323
            return rv;
4324
        }
4325
# endif
4326
0
    } else {
4327
0
        if (!x || !pk)
4328
0
            return 0;
4329
0
        idx = ssl_cert_type(x, pk);
4330
0
        if (idx == -1)
4331
0
            return 0;
4332
0
        cpk = c->pkeys + idx;
4333
0
        if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
4334
0
            check_flags = CERT_PKEY_STRICT_FLAGS;
4335
0
        else
4336
0
            check_flags = CERT_PKEY_VALID_FLAGS;
4337
0
        strict_mode = 1;
4338
0
    }
4339
4340
0
    if (suiteb_flags) {
4341
0
        int ok;
4342
0
        if (check_flags)
4343
0
            check_flags |= CERT_PKEY_SUITEB;
4344
0
        ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4345
0
        if (ok == X509_V_OK)
4346
0
            rv |= CERT_PKEY_SUITEB;
4347
0
        else if (!check_flags)
4348
0
            goto end;
4349
0
    }
4350
4351
    /*
4352
     * Check all signature algorithms are consistent with signature
4353
     * algorithms extension if TLS 1.2 or later and strict mode.
4354
     */
4355
0
    if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
4356
0
        int default_nid;
4357
0
        unsigned char rsign = 0;
4358
0
        if (c->peer_sigalgs)
4359
0
            default_nid = 0;
4360
        /* If no sigalgs extension use defaults from RFC5246 */
4361
0
        else {
4362
0
            switch (idx) {
4363
0
            case SSL_PKEY_RSA_ENC:
4364
0
            case SSL_PKEY_RSA_SIGN:
4365
0
            case SSL_PKEY_DH_RSA:
4366
0
                rsign = TLSEXT_signature_rsa;
4367
0
                default_nid = NID_sha1WithRSAEncryption;
4368
0
                break;
4369
4370
0
            case SSL_PKEY_DSA_SIGN:
4371
0
            case SSL_PKEY_DH_DSA:
4372
0
                rsign = TLSEXT_signature_dsa;
4373
0
                default_nid = NID_dsaWithSHA1;
4374
0
                break;
4375
4376
0
            case SSL_PKEY_ECC:
4377
0
                rsign = TLSEXT_signature_ecdsa;
4378
0
                default_nid = NID_ecdsa_with_SHA1;
4379
0
                break;
4380
4381
0
            default:
4382
0
                default_nid = -1;
4383
0
                break;
4384
0
            }
4385
0
        }
4386
        /*
4387
         * If peer sent no signature algorithms extension and we have set
4388
         * preferred signature algorithms check we support sha1.
4389
         */
4390
0
        if (default_nid > 0 && c->conf_sigalgs) {
4391
0
            size_t j;
4392
0
            const unsigned char *p = c->conf_sigalgs;
4393
0
            for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
4394
0
                if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4395
0
                    break;
4396
0
            }
4397
0
            if (j == c->conf_sigalgslen) {
4398
0
                if (check_flags)
4399
0
                    goto skip_sigs;
4400
0
                else
4401
0
                    goto end;
4402
0
            }
4403
0
        }
4404
        /* Check signature algorithm of each cert in chain */
4405
0
        if (!tls1_check_sig_alg(c, x, default_nid)) {
4406
0
            if (!check_flags)
4407
0
                goto end;
4408
0
        } else
4409
0
            rv |= CERT_PKEY_EE_SIGNATURE;
4410
0
        rv |= CERT_PKEY_CA_SIGNATURE;
4411
0
        for (i = 0; i < sk_X509_num(chain); i++) {
4412
0
            if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
4413
0
                if (check_flags) {
4414
0
                    rv &= ~CERT_PKEY_CA_SIGNATURE;
4415
0
                    break;
4416
0
                } else
4417
0
                    goto end;
4418
0
            }
4419
0
        }
4420
0
    }
4421
    /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4422
0
    else if (check_flags)
4423
0
        rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
4424
0
 skip_sigs:
4425
    /* Check cert parameters are consistent */
4426
0
    if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4427
0
        rv |= CERT_PKEY_EE_PARAM;
4428
0
    else if (!check_flags)
4429
0
        goto end;
4430
0
    if (!s->server)
4431
0
        rv |= CERT_PKEY_CA_PARAM;
4432
    /* In strict mode check rest of chain too */
4433
0
    else if (strict_mode) {
4434
0
        rv |= CERT_PKEY_CA_PARAM;
4435
0
        for (i = 0; i < sk_X509_num(chain); i++) {
4436
0
            X509 *ca = sk_X509_value(chain, i);
4437
0
            if (!tls1_check_cert_param(s, ca, 0)) {
4438
0
                if (check_flags) {
4439
0
                    rv &= ~CERT_PKEY_CA_PARAM;
4440
0
                    break;
4441
0
                } else
4442
0
                    goto end;
4443
0
            }
4444
0
        }
4445
0
    }
4446
0
    if (!s->server && strict_mode) {
4447
0
        STACK_OF(X509_NAME) *ca_dn;
4448
0
        int check_type = 0;
4449
0
        switch (pk->type) {
4450
0
        case EVP_PKEY_RSA:
4451
0
            check_type = TLS_CT_RSA_SIGN;
4452
0
            break;
4453
0
        case EVP_PKEY_DSA:
4454
0
            check_type = TLS_CT_DSS_SIGN;
4455
0
            break;
4456
0
        case EVP_PKEY_EC:
4457
0
            check_type = TLS_CT_ECDSA_SIGN;
4458
0
            break;
4459
0
        case EVP_PKEY_DH:
4460
0
        case EVP_PKEY_DHX:
4461
0
            {
4462
0
                int cert_type = X509_certificate_type(x, pk);
4463
0
                if (cert_type & EVP_PKS_RSA)
4464
0
                    check_type = TLS_CT_RSA_FIXED_DH;
4465
0
                if (cert_type & EVP_PKS_DSA)
4466
0
                    check_type = TLS_CT_DSS_FIXED_DH;
4467
0
            }
4468
0
        }
4469
0
        if (check_type) {
4470
0
            const unsigned char *ctypes;
4471
0
            int ctypelen;
4472
0
            if (c->ctypes) {
4473
0
                ctypes = c->ctypes;
4474
0
                ctypelen = (int)c->ctype_num;
4475
0
            } else {
4476
0
                ctypes = (unsigned char *)s->s3->tmp.ctype;
4477
0
                ctypelen = s->s3->tmp.ctype_num;
4478
0
            }
4479
0
            for (i = 0; i < ctypelen; i++) {
4480
0
                if (ctypes[i] == check_type) {
4481
0
                    rv |= CERT_PKEY_CERT_TYPE;
4482
0
                    break;
4483
0
                }
4484
0
            }
4485
0
            if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4486
0
                goto end;
4487
0
        } else
4488
0
            rv |= CERT_PKEY_CERT_TYPE;
4489
4490
0
        ca_dn = s->s3->tmp.ca_names;
4491
4492
0
        if (!sk_X509_NAME_num(ca_dn))
4493
0
            rv |= CERT_PKEY_ISSUER_NAME;
4494
4495
0
        if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4496
0
            if (ssl_check_ca_name(ca_dn, x))
4497
0
                rv |= CERT_PKEY_ISSUER_NAME;
4498
0
        }
4499
0
        if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4500
0
            for (i = 0; i < sk_X509_num(chain); i++) {
4501
0
                X509 *xtmp = sk_X509_value(chain, i);
4502
0
                if (ssl_check_ca_name(ca_dn, xtmp)) {
4503
0
                    rv |= CERT_PKEY_ISSUER_NAME;
4504
0
                    break;
4505
0
                }
4506
0
            }
4507
0
        }
4508
0
        if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4509
0
            goto end;
4510
0
    } else
4511
0
        rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4512
4513
0
    if (!check_flags || (rv & check_flags) == check_flags)
4514
0
        rv |= CERT_PKEY_VALID;
4515
4516
0
 end:
4517
4518
0
    if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4519
0
        if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
4520
0
            rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4521
0
        else if (cpk->digest)
4522
0
            rv |= CERT_PKEY_SIGN;
4523
0
    } else
4524
0
        rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4525
4526
    /*
4527
     * When checking a CERT_PKEY structure all flags are irrelevant if the
4528
     * chain is invalid.
4529
     */
4530
0
    if (!check_flags) {
4531
0
        if (rv & CERT_PKEY_VALID)
4532
0
            cpk->valid_flags = rv;
4533
0
        else {
4534
            /* Preserve explicit sign flag, clear rest */
4535
0
            cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
4536
0
            return 0;
4537
0
        }
4538
0
    }
4539
0
    return rv;
4540
0
}
4541
4542
/* Set validity of certificates in an SSL structure */
4543
void tls1_set_cert_validity(SSL *s)
4544
0
{
4545
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4546
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4547
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4548
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4549
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4550
0
    tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4551
0
}
4552
4553
/* User level utiity function to check a chain is suitable */
4554
int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4555
0
{
4556
0
    return tls1_check_chain(s, x, pk, chain, -1);
4557
0
}
4558
4559
#endif