blob: 243cef5ad0e86ead49ace0ffbfa49859ce2691de [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Bodo Möllerf1fd4542006-01-03 03:27:19 +00008 */
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00009
10#include <stdio.h>
Emilia Kasperaa474d12016-02-19 17:24:44 +010011#include <stdlib.h>
Bodo Möllerec577821999-04-23 22:13:45 +000012#include <openssl/objects.h>
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +000013#include <openssl/evp.h>
14#include <openssl/hmac.h>
Dr. Stephen Henson67c8e7f2007-09-26 21:56:59 +000015#include <openssl/ocsp.h>
Matt Caswell5951e842016-04-20 16:38:29 +010016#include <openssl/conf.h>
17#include <openssl/x509v3.h>
Rich Salz3c272082016-03-18 14:30:20 -040018#include <openssl/dh.h>
19#include <openssl/bn.h>
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000020#include "ssl_locl.h"
Rich Salz3c272082016-03-18 14:30:20 -040021#include <openssl/ct.h>
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000022
Matt Caswell0f113f32015-01-22 03:40:55 +000023SSL3_ENC_METHOD const TLSv1_enc_data = {
24 tls1_enc,
25 tls1_mac,
26 tls1_setup_key_block,
27 tls1_generate_master_secret,
28 tls1_change_cipher_state,
29 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000030 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
31 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
32 tls1_alert_code,
33 tls1_export_keying_material,
34 0,
Matt Caswella29fa982016-09-29 22:40:15 +010035 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010036 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000037 ssl3_handshake_write
38};
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +000039
Matt Caswell0f113f32015-01-22 03:40:55 +000040SSL3_ENC_METHOD const TLSv1_1_enc_data = {
41 tls1_enc,
42 tls1_mac,
43 tls1_setup_key_block,
44 tls1_generate_master_secret,
45 tls1_change_cipher_state,
46 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000047 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
48 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
49 tls1_alert_code,
50 tls1_export_keying_material,
51 SSL_ENC_FLAG_EXPLICIT_IV,
Matt Caswella29fa982016-09-29 22:40:15 +010052 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010053 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000054 ssl3_handshake_write
55};
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +000056
Matt Caswell0f113f32015-01-22 03:40:55 +000057SSL3_ENC_METHOD const TLSv1_2_enc_data = {
58 tls1_enc,
59 tls1_mac,
60 tls1_setup_key_block,
61 tls1_generate_master_secret,
62 tls1_change_cipher_state,
63 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000064 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
65 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
66 tls1_alert_code,
67 tls1_export_keying_material,
68 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
69 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
Matt Caswella29fa982016-09-29 22:40:15 +010070 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010071 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000072 ssl3_handshake_write
73};
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000074
Matt Caswell582a17d2016-10-21 17:39:33 +010075SSL3_ENC_METHOD const TLSv1_3_enc_data = {
Matt Caswellbebc0c72016-11-17 18:00:17 +000076 tls13_enc,
Matt Caswell582a17d2016-10-21 17:39:33 +010077 tls1_mac,
Matt Caswell92760c22016-11-09 14:06:12 +000078 tls13_setup_key_block,
79 tls13_generate_master_secret,
80 tls13_change_cipher_state,
81 tls13_final_finish_mac,
Matt Caswell582a17d2016-10-21 17:39:33 +010082 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
83 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
Matt Caswell04904312016-12-30 11:26:39 +000084 tls13_alert_code,
Matt Caswell582a17d2016-10-21 17:39:33 +010085 tls1_export_keying_material,
Matt Caswellbebc0c72016-11-17 18:00:17 +000086 SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
Matt Caswell582a17d2016-10-21 17:39:33 +010087 ssl3_set_handshake_header,
88 tls_close_construct_packet,
89 ssl3_handshake_write
90};
91
Dr. Stephen Hensonf3b656b2005-08-05 23:56:11 +000092long tls1_default_timeout(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000093{
94 /*
95 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
96 * http, the cache would over fill
97 */
98 return (60 * 60 * 2);
99}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000100
Ulf Möller6b691a51999-04-19 21:31:43 +0000101int tls1_new(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000102{
103 if (!ssl3_new(s))
104 return (0);
105 s->method->ssl_clear(s);
106 return (1);
107}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000108
Ulf Möller6b691a51999-04-19 21:31:43 +0000109void tls1_free(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000110{
Rich Salzaff8c122016-12-08 14:18:40 -0500111 OPENSSL_free(s->ext.session_ticket);
Matt Caswell0f113f32015-01-22 03:40:55 +0000112 ssl3_free(s);
113}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000114
Ulf Möller6b691a51999-04-19 21:31:43 +0000115void tls1_clear(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000116{
117 ssl3_clear(s);
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500118 if (s->method->version == TLS_ANY_VERSION)
119 s->version = TLS_MAX_VERSION;
120 else
121 s->version = s->method->version;
Matt Caswell0f113f32015-01-22 03:40:55 +0000122}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000123
Dr. Stephen Henson525de5d2007-08-12 23:59:05 +0000124#ifndef OPENSSL_NO_EC
Dr. Stephen Hensoneda37662011-05-30 17:58:13 +0000125
Matt Caswell0f113f32015-01-22 03:40:55 +0000126typedef struct {
127 int nid; /* Curve NID */
128 int secbits; /* Bits of security (from SP800-57) */
129 unsigned int flags; /* Flags: currently just field type */
130} tls_curve_info;
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +0000131
Dr. Stephen Henson2dc1aee2016-02-13 15:27:43 +0000132/*
133 * Table of curve information.
Rich Salzddb4c042016-01-03 13:24:32 -0500134 * Do not delete entries or reorder this array! It is used as a lookup
Dr. Stephen Henson2dc1aee2016-02-13 15:27:43 +0000135 * table: the index of each entry is one less than the TLS curve id.
136 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000137static const tls_curve_info nid_list[] = {
138 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
139 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
140 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
141 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
142 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
143 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
144 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
145 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
146 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
147 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
148 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
149 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
150 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
151 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
152 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
153 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
154 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
155 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
156 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
157 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
158 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
159 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
160 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
161 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
162 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
163 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
164 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
165 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
Dr. Stephen Hensonec246302016-08-11 15:41:49 +0100166 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
Matt Caswell0f113f32015-01-22 03:40:55 +0000167};
Dr. Stephen Hensoneda37662011-05-30 17:58:13 +0000168
Matt Caswell0f113f32015-01-22 03:40:55 +0000169static const unsigned char ecformats_default[] = {
170 TLSEXT_ECPOINTFORMAT_uncompressed,
171 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
172 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
173};
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000174
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100175/* The default curves */
176static const unsigned char eccurves_default[] = {
Dr. Stephen Henson1db31072016-02-25 17:46:14 +0000177 0, 29, /* X25519 (29) */
Emilia Kasperde57d232015-05-20 15:47:51 +0200178 0, 23, /* secp256r1 (23) */
Matt Caswell0f113f32015-01-22 03:40:55 +0000179 0, 25, /* secp521r1 (25) */
Matt Caswell0f113f32015-01-22 03:40:55 +0000180 0, 24, /* secp384r1 (24) */
Emilia Kasperde57d232015-05-20 15:47:51 +0200181};
182
Matt Caswell0f113f32015-01-22 03:40:55 +0000183static const unsigned char suiteb_curves[] = {
184 0, TLSEXT_curve_P_256,
185 0, TLSEXT_curve_P_384
186};
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000187
Dr. Stephen Hensonec246302016-08-11 15:41:49 +0100188int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
Matt Caswell0f113f32015-01-22 03:40:55 +0000189{
Dr. Stephen Hensonec246302016-08-11 15:41:49 +0100190 const tls_curve_info *cinfo;
Matt Caswell0f113f32015-01-22 03:40:55 +0000191 /* ECC curves from RFC 4492 and RFC 7027 */
Dr. Stephen Hensonb6eb9822015-05-02 18:30:00 +0100192 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 return 0;
Dr. Stephen Hensonec246302016-08-11 15:41:49 +0100194 cinfo = nid_list + curve_id - 1;
195 if (pflags)
196 *pflags = cinfo->flags;
197 return cinfo->nid;
Matt Caswell0f113f32015-01-22 03:40:55 +0000198}
Dr. Stephen Henson525de5d2007-08-12 23:59:05 +0000199
200int tls1_ec_nid2curve_id(int nid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000201{
Dr. Stephen Henson2fa2d152016-02-13 15:28:25 +0000202 size_t i;
203 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
204 if (nid_list[i].nid == nid)
Matt Caswell348240c2016-10-19 15:11:24 +0100205 return (int)(i + 1);
Matt Caswell0f113f32015-01-22 03:40:55 +0000206 }
Dr. Stephen Henson2fa2d152016-02-13 15:28:25 +0000207 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000208}
209
Emilia Kasper740580c2014-12-01 16:55:55 +0100210/*
211 * Get curves list, if "sess" is set return client curves otherwise
212 * preferred list.
213 * Sets |num_curves| to the number of curves in the list, i.e.,
214 * the length of |pcurves| is 2 * num_curves.
215 * Returns 1 on success and 0 if the client curves list has invalid format.
216 * The latter indicates an internal error: we should not be accepting such
217 * lists in the first place.
218 * TODO(emilia): we should really be storing the curves list in explicitly
219 * parsed form instead. (However, this would affect binary compatibility
220 * so cannot happen in the 1.0.x series.)
Dr. Stephen Hensonfd2b65c2012-04-04 14:41:01 +0000221 */
Matt Caswell6b473ac2016-11-24 16:59:48 +0000222int tls1_get_curvelist(SSL *s, int sess, const unsigned char **pcurves,
223 size_t *num_curves)
Matt Caswell0f113f32015-01-22 03:40:55 +0000224{
225 size_t pcurveslen = 0;
Rich Salz3e373512016-09-19 13:09:58 -0400226
Matt Caswell0f113f32015-01-22 03:40:55 +0000227 if (sess) {
Rich Salzaff8c122016-12-08 14:18:40 -0500228 *pcurves = s->session->ext.supportedgroups;
229 pcurveslen = s->session->ext.supportedgroups_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000230 } else {
231 /* For Suite B mode only include P-256, P-384 */
232 switch (tls1_suiteb(s)) {
233 case SSL_CERT_FLAG_SUITEB_128_LOS:
234 *pcurves = suiteb_curves;
235 pcurveslen = sizeof(suiteb_curves);
236 break;
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000237
Matt Caswell0f113f32015-01-22 03:40:55 +0000238 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
239 *pcurves = suiteb_curves;
240 pcurveslen = 2;
241 break;
Dr. Stephen Hensonb34aa492012-12-10 02:02:16 +0000242
Matt Caswell0f113f32015-01-22 03:40:55 +0000243 case SSL_CERT_FLAG_SUITEB_192_LOS:
244 *pcurves = suiteb_curves + 2;
245 pcurveslen = 2;
246 break;
247 default:
Rich Salzaff8c122016-12-08 14:18:40 -0500248 *pcurves = s->ext.supportedgroups;
249 pcurveslen = s->ext.supportedgroups_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000250 }
251 if (!*pcurves) {
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100252 *pcurves = eccurves_default;
253 pcurveslen = sizeof(eccurves_default);
Matt Caswell0f113f32015-01-22 03:40:55 +0000254 }
255 }
Emilia Kasper740580c2014-12-01 16:55:55 +0100256
Matt Caswell0f113f32015-01-22 03:40:55 +0000257 /* We do not allow odd length arrays to enter the system. */
258 if (pcurveslen & 1) {
259 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
260 *num_curves = 0;
261 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000262 }
Rich Salz3e373512016-09-19 13:09:58 -0400263 *num_curves = pcurveslen / 2;
264 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000265}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +0000266
267/* See if curve is allowed by security callback */
Matt Caswell6b473ac2016-11-24 16:59:48 +0000268int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +0000269{
270 const tls_curve_info *cinfo;
271 if (curve[0])
272 return 1;
Dr. Stephen Hensonb6eb9822015-05-02 18:30:00 +0100273 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
Matt Caswell0f113f32015-01-22 03:40:55 +0000274 return 0;
275 cinfo = &nid_list[curve[1] - 1];
276# ifdef OPENSSL_NO_EC2M
277 if (cinfo->flags & TLS_CURVE_CHAR2)
278 return 0;
279# endif
280 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
281}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +0000282
Dr. Stephen Hensond18b7162012-07-24 13:47:40 +0000283/* Check a curve is one of our preferences */
284int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000285{
286 const unsigned char *curves;
287 size_t num_curves, i;
288 unsigned int suiteb_flags = tls1_suiteb(s);
289 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
290 return 0;
291 /* Check curve matches Suite B preferences */
292 if (suiteb_flags) {
293 unsigned long cid = s->s3->tmp.new_cipher->id;
294 if (p[1])
295 return 0;
296 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
297 if (p[2] != TLSEXT_curve_P_256)
298 return 0;
299 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
300 if (p[2] != TLSEXT_curve_P_384)
301 return 0;
302 } else /* Should never happen */
303 return 0;
304 }
305 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
306 return 0;
307 for (i = 0; i < num_curves; i++, curves += 2) {
308 if (p[1] == curves[0] && p[2] == curves[1])
309 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
310 }
311 return 0;
312}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000313
Tim Hudson1d97c842014-12-28 12:48:40 +1000314/*-
Matt Caswellde4d7642016-11-09 14:51:06 +0000315 * For nmatch >= 0, return the NID of the |nmatch|th shared group or NID_undef
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100316 * if there is no match.
317 * For nmatch == -1, return number of matches
Matt Caswellde4d7642016-11-09 14:51:06 +0000318 * For nmatch == -2, return the NID of the group to use for
Emilia Kasper376e2ca2014-12-04 15:00:11 +0100319 * an EC tmp key, or NID_undef if there is no match.
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000320 */
Matt Caswellde4d7642016-11-09 14:51:06 +0000321int tls1_shared_group(SSL *s, int nmatch)
Matt Caswell0f113f32015-01-22 03:40:55 +0000322{
323 const unsigned char *pref, *supp;
324 size_t num_pref, num_supp, i, j;
325 int k;
Rich Salz3e373512016-09-19 13:09:58 -0400326
Matt Caswell0f113f32015-01-22 03:40:55 +0000327 /* Can't do anything on client side */
328 if (s->server == 0)
329 return -1;
330 if (nmatch == -2) {
331 if (tls1_suiteb(s)) {
332 /*
333 * For Suite B ciphersuite determines curve: we already know
334 * these are acceptable due to previous checks.
335 */
336 unsigned long cid = s->s3->tmp.new_cipher->id;
Rich Salz3e373512016-09-19 13:09:58 -0400337
Matt Caswell0f113f32015-01-22 03:40:55 +0000338 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
339 return NID_X9_62_prime256v1; /* P-256 */
340 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
341 return NID_secp384r1; /* P-384 */
342 /* Should never happen */
343 return NID_undef;
344 }
345 /* If not Suite B just return first preference shared curve */
346 nmatch = 0;
347 }
348 /*
349 * Avoid truncation. tls1_get_curvelist takes an int
350 * but s->options is a long...
351 */
Rich Salz3e373512016-09-19 13:09:58 -0400352 if (!tls1_get_curvelist(s,
353 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0,
354 &supp, &num_supp))
Matt Caswell0f113f32015-01-22 03:40:55 +0000355 /* In practice, NID_undef == 0 but let's be precise. */
356 return nmatch == -1 ? 0 : NID_undef;
Rich Salz3e373512016-09-19 13:09:58 -0400357 if (!tls1_get_curvelist(s,
358 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0,
359 &pref, &num_pref))
Matt Caswell0f113f32015-01-22 03:40:55 +0000360 return nmatch == -1 ? 0 : NID_undef;
Kurt Roeckx3c065132015-05-30 19:20:12 +0200361
Rich Salz3e373512016-09-19 13:09:58 -0400362 for (k = 0, i = 0; i < num_pref; i++, pref += 2) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000363 const unsigned char *tsupp = supp;
Rich Salz3e373512016-09-19 13:09:58 -0400364
Matt Caswell0f113f32015-01-22 03:40:55 +0000365 for (j = 0; j < num_supp; j++, tsupp += 2) {
366 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
367 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
368 continue;
369 if (nmatch == k) {
370 int id = (pref[0] << 8) | pref[1];
Rich Salz3e373512016-09-19 13:09:58 -0400371
Dr. Stephen Hensonec246302016-08-11 15:41:49 +0100372 return tls1_ec_curve_id2nid(id, NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +0000373 }
374 k++;
375 }
376 }
377 }
378 if (nmatch == -1)
379 return k;
380 /* Out of range (nmatch > k). */
381 return NID_undef;
382}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000383
Matt Caswellde4d7642016-11-09 14:51:06 +0000384int tls1_set_groups(unsigned char **pext, size_t *pextlen,
385 int *groups, size_t ngroups)
Matt Caswell0f113f32015-01-22 03:40:55 +0000386{
Matt Caswellde4d7642016-11-09 14:51:06 +0000387 unsigned char *glist, *p;
Matt Caswell0f113f32015-01-22 03:40:55 +0000388 size_t i;
389 /*
Matt Caswellde4d7642016-11-09 14:51:06 +0000390 * Bitmap of groups included to detect duplicates: only works while group
Matt Caswell0f113f32015-01-22 03:40:55 +0000391 * ids < 32
392 */
393 unsigned long dup_list = 0;
Matt Caswellde4d7642016-11-09 14:51:06 +0000394 glist = OPENSSL_malloc(ngroups * 2);
395 if (glist == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000396 return 0;
Matt Caswellde4d7642016-11-09 14:51:06 +0000397 for (i = 0, p = glist; i < ngroups; i++) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000398 unsigned long idmask;
399 int id;
Matt Caswellde4d7642016-11-09 14:51:06 +0000400 /* TODO(TLS1.3): Convert for DH groups */
401 id = tls1_ec_nid2curve_id(groups[i]);
Matt Caswell0f113f32015-01-22 03:40:55 +0000402 idmask = 1L << id;
403 if (!id || (dup_list & idmask)) {
Matt Caswellde4d7642016-11-09 14:51:06 +0000404 OPENSSL_free(glist);
Matt Caswell0f113f32015-01-22 03:40:55 +0000405 return 0;
406 }
407 dup_list |= idmask;
408 s2n(id, p);
409 }
Rich Salzb548a1f2015-05-01 10:02:07 -0400410 OPENSSL_free(*pext);
Matt Caswellde4d7642016-11-09 14:51:06 +0000411 *pext = glist;
412 *pextlen = ngroups * 2;
Matt Caswell0f113f32015-01-22 03:40:55 +0000413 return 1;
414}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000415
Matt Caswell0f113f32015-01-22 03:40:55 +0000416# define MAX_CURVELIST 28
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000417
Matt Caswell0f113f32015-01-22 03:40:55 +0000418typedef struct {
419 size_t nidcnt;
420 int nid_arr[MAX_CURVELIST];
421} nid_cb_st;
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000422
423static int nid_cb(const char *elem, int len, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000424{
425 nid_cb_st *narg = arg;
426 size_t i;
427 int nid;
428 char etmp[20];
Kurt Roeckx2747d732015-01-24 14:46:50 +0100429 if (elem == NULL)
430 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000431 if (narg->nidcnt == MAX_CURVELIST)
432 return 0;
433 if (len > (int)(sizeof(etmp) - 1))
434 return 0;
435 memcpy(etmp, elem, len);
436 etmp[len] = 0;
437 nid = EC_curve_nist2nid(etmp);
438 if (nid == NID_undef)
439 nid = OBJ_sn2nid(etmp);
440 if (nid == NID_undef)
441 nid = OBJ_ln2nid(etmp);
442 if (nid == NID_undef)
443 return 0;
444 for (i = 0; i < narg->nidcnt; i++)
445 if (narg->nid_arr[i] == nid)
446 return 0;
447 narg->nid_arr[narg->nidcnt++] = nid;
448 return 1;
449}
450
Matt Caswellde4d7642016-11-09 14:51:06 +0000451/* Set groups based on a colon separate list */
452int tls1_set_groups_list(unsigned char **pext, size_t *pextlen, const char *str)
Matt Caswell0f113f32015-01-22 03:40:55 +0000453{
454 nid_cb_st ncb;
455 ncb.nidcnt = 0;
456 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
457 return 0;
458 if (pext == NULL)
459 return 1;
Matt Caswellde4d7642016-11-09 14:51:06 +0000460 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
Matt Caswell0f113f32015-01-22 03:40:55 +0000461}
462
Dr. Stephen Hensonfd2b65c2012-04-04 14:41:01 +0000463/* For an EC key set TLS id and required compression based on parameters */
464static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
Matt Caswell0f113f32015-01-22 03:40:55 +0000465 EC_KEY *ec)
466{
Dr. Stephen Henson2235b7f2016-02-13 15:26:15 +0000467 int id;
Matt Caswell0f113f32015-01-22 03:40:55 +0000468 const EC_GROUP *grp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000469 if (!ec)
470 return 0;
471 /* Determine if it is a prime field */
472 grp = EC_KEY_get0_group(ec);
473 if (!grp)
474 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000475 /* Determine curve ID */
476 id = EC_GROUP_get_curve_name(grp);
477 id = tls1_ec_nid2curve_id(id);
Dr. Stephen Henson2235b7f2016-02-13 15:26:15 +0000478 /* If no id return error: we don't support arbitrary explicit curves */
479 if (id == 0)
480 return 0;
481 curve_id[0] = 0;
482 curve_id[1] = (unsigned char)id;
Matt Caswell0f113f32015-01-22 03:40:55 +0000483 if (comp_id) {
484 if (EC_KEY_get0_public_key(ec) == NULL)
485 return 0;
Dr. Stephen Henson2235b7f2016-02-13 15:26:15 +0000486 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
487 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
488 } else {
489 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
Matt Caswell0f113f32015-01-22 03:40:55 +0000490 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
491 else
492 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
Dr. Stephen Henson2235b7f2016-02-13 15:26:15 +0000493 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000494 }
495 return 1;
496}
497
Dr. Stephen Hensonfd2b65c2012-04-04 14:41:01 +0000498/* Check an EC key is compatible with extensions */
499static int tls1_check_ec_key(SSL *s,
Matt Caswell0f113f32015-01-22 03:40:55 +0000500 unsigned char *curve_id, unsigned char *comp_id)
501{
502 const unsigned char *pformats, *pcurves;
503 size_t num_formats, num_curves, i;
504 int j;
505 /*
506 * If point formats extension present check it, otherwise everything is
507 * supported (see RFC4492).
508 */
Rich Salzaff8c122016-12-08 14:18:40 -0500509 if (comp_id && s->session->ext.ecpointformats) {
510 pformats = s->session->ext.ecpointformats;
511 num_formats = s->session->ext.ecpointformats_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000512 for (i = 0; i < num_formats; i++, pformats++) {
513 if (*comp_id == *pformats)
514 break;
515 }
516 if (i == num_formats)
517 return 0;
518 }
519 if (!curve_id)
520 return 1;
521 /* Check curve is consistent with client and server preferences */
522 for (j = 0; j <= 1; j++) {
523 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
524 return 0;
Matt Caswellb79d2412015-03-20 15:10:16 +0000525 if (j == 1 && num_curves == 0) {
526 /*
527 * If we've not received any curves then skip this check.
528 * RFC 4492 does not require the supported elliptic curves extension
529 * so if it is not sent we can just choose any curve.
530 * It is invalid to send an empty list in the elliptic curves
531 * extension, so num_curves == 0 always means no extension.
532 */
533 break;
534 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000535 for (i = 0; i < num_curves; i++, pcurves += 2) {
536 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
537 break;
538 }
539 if (i == num_curves)
540 return 0;
541 /* For clients can only check sent curve list */
542 if (!s->server)
543 break;
544 }
545 return 1;
546}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +0000547
Matt Caswell7da160b2016-11-25 10:22:02 +0000548void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
549 size_t *num_formats)
Matt Caswell0f113f32015-01-22 03:40:55 +0000550{
551 /*
552 * If we have a custom point format list use it otherwise use default
553 */
Rich Salzaff8c122016-12-08 14:18:40 -0500554 if (s->ext.ecpointformats) {
555 *pformats = s->ext.ecpointformats;
556 *num_formats = s->ext.ecpointformats_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000557 } else {
558 *pformats = ecformats_default;
559 /* For Suite B we don't support char2 fields */
560 if (tls1_suiteb(s))
561 *num_formats = sizeof(ecformats_default) - 1;
562 else
563 *num_formats = sizeof(ecformats_default);
564 }
565}
Dr. Stephen Henson5087afa2012-11-26 18:38:10 +0000566
Matt Caswell0f113f32015-01-22 03:40:55 +0000567/*
568 * Check cert parameters compatible with extensions: currently just checks EC
569 * certificates have compatible curves and compression.
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +0000570 */
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000571static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
Matt Caswell0f113f32015-01-22 03:40:55 +0000572{
573 unsigned char comp_id, curve_id[2];
574 EVP_PKEY *pkey;
575 int rv;
Dr. Stephen Henson8382fd32015-12-20 00:32:36 +0000576 pkey = X509_get0_pubkey(x);
Matt Caswell0f113f32015-01-22 03:40:55 +0000577 if (!pkey)
578 return 0;
579 /* If not EC nothing to do */
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +0000580 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
Matt Caswell0f113f32015-01-22 03:40:55 +0000581 return 1;
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +0000582 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
Matt Caswell0f113f32015-01-22 03:40:55 +0000583 if (!rv)
584 return 0;
585 /*
586 * Can't check curve_id for client certs as we don't have a supported
587 * curves extension.
588 */
589 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
590 if (!rv)
591 return 0;
592 /*
593 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
594 * SHA384+P-384, adjust digest if necessary.
595 */
596 if (set_ee_md && tls1_suiteb(s)) {
597 int check_md;
598 size_t i;
599 CERT *c = s->cert;
600 if (curve_id[0])
601 return 0;
602 /* Check to see we have necessary signing algorithm */
603 if (curve_id[1] == TLSEXT_curve_P_256)
604 check_md = NID_ecdsa_with_SHA256;
605 else if (curve_id[1] == TLSEXT_curve_P_384)
606 check_md = NID_ecdsa_with_SHA384;
607 else
608 return 0; /* Should never happen */
609 for (i = 0; i < c->shared_sigalgslen; i++)
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000610 if (check_md == c->shared_sigalgs[i]->sigandhash)
Matt Caswell0f113f32015-01-22 03:40:55 +0000611 break;
612 if (i == c->shared_sigalgslen)
613 return 0;
614 if (set_ee_md == 2) {
615 if (check_md == NID_ecdsa_with_SHA256)
Dr. Stephen Hensond376e572015-05-12 18:56:39 +0100616 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
Matt Caswell0f113f32015-01-22 03:40:55 +0000617 else
Dr. Stephen Hensond376e572015-05-12 18:56:39 +0100618 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
Matt Caswell0f113f32015-01-22 03:40:55 +0000619 }
620 }
621 return rv;
622}
623
Rich Salz10bf4fc2015-03-10 19:09:27 -0400624# ifndef OPENSSL_NO_EC
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100625/*
FdaSilvaYY8483a002016-03-10 21:34:48 +0100626 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100627 * @s: SSL connection
628 * @cid: Cipher ID we're considering using
629 *
630 * Checks that the kECDHE cipher suite we're considering using
631 * is compatible with the client extensions.
632 *
633 * Returns 0 when the cipher can't be used or 1 when it can.
634 */
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000635int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000636{
Matt Caswell0f113f32015-01-22 03:40:55 +0000637 /*
638 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
639 * curves permitted.
640 */
641 if (tls1_suiteb(s)) {
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100642 unsigned char curve_id[2];
Matt Caswell0f113f32015-01-22 03:40:55 +0000643 /* Curve to check determined by ciphersuite */
644 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
645 curve_id[1] = TLSEXT_curve_P_256;
646 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
647 curve_id[1] = TLSEXT_curve_P_384;
648 else
649 return 0;
650 curve_id[0] = 0;
651 /* Check this curve is acceptable */
652 if (!tls1_check_ec_key(s, curve_id, NULL))
653 return 0;
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100654 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000655 }
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100656 /* Need a shared curve */
Matt Caswellde4d7642016-11-09 14:51:06 +0000657 if (tls1_shared_group(s, 0))
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100658 return 1;
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100659 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000660}
Rich Salz10bf4fc2015-03-10 19:09:27 -0400661# endif /* OPENSSL_NO_EC */
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000662
Dr. Stephen Henson14536c82013-08-17 17:40:08 +0100663#else
664
665static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
Matt Caswell0f113f32015-01-22 03:40:55 +0000666{
667 return 1;
668}
Dr. Stephen Henson14536c82013-08-17 17:40:08 +0100669
Matt Caswell0f113f32015-01-22 03:40:55 +0000670#endif /* OPENSSL_NO_EC */
Bodo Möllerf1fd4542006-01-03 03:27:19 +0000671
Matt Caswell703bcee2016-12-14 14:31:21 +0000672/* Default sigalg schemes */
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000673static const uint16_t tls12_sigalgs[] = {
Matt Caswell703bcee2016-12-14 14:31:21 +0000674#ifndef OPENSSL_NO_EC
675 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
676 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
677 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
Matt Caswelle481f9b2015-05-15 10:49:56 +0100678#endif
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000679
Matt Caswell536199e2016-12-14 16:37:48 +0000680 TLSEXT_SIGALG_rsa_pss_sha256,
681 TLSEXT_SIGALG_rsa_pss_sha384,
682 TLSEXT_SIGALG_rsa_pss_sha512,
683
Matt Caswell703bcee2016-12-14 14:31:21 +0000684 TLSEXT_SIGALG_rsa_pkcs1_sha256,
685 TLSEXT_SIGALG_rsa_pkcs1_sha384,
686 TLSEXT_SIGALG_rsa_pkcs1_sha512,
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000687
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000688#ifndef OPENSSL_NO_EC
Matt Caswell42ab2232017-01-10 13:45:24 +0000689 TLSEXT_SIGALG_ecdsa_sha1,
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000690#endif
Matt Caswell42ab2232017-01-10 13:45:24 +0000691 TLSEXT_SIGALG_rsa_pkcs1_sha1,
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000692#ifndef OPENSSL_NO_DSA
Matt Caswell42ab2232017-01-10 13:45:24 +0000693 TLSEXT_SIGALG_dsa_sha1,
694
Matt Caswell703bcee2016-12-14 14:31:21 +0000695 TLSEXT_SIGALG_dsa_sha256,
696 TLSEXT_SIGALG_dsa_sha384,
697 TLSEXT_SIGALG_dsa_sha512
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000698#endif
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000699};
Matt Caswell0f113f32015-01-22 03:40:55 +0000700
Matt Caswelle481f9b2015-05-15 10:49:56 +0100701#ifndef OPENSSL_NO_EC
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000702static const uint16_t suiteb_sigalgs[] = {
Matt Caswell703bcee2016-12-14 14:31:21 +0000703 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
704 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000705};
Matt Caswelle481f9b2015-05-15 10:49:56 +0100706#endif
Rich Salzaff8c122016-12-08 14:18:40 -0500707
Matt Caswell7a531ee2017-01-10 14:23:02 +0000708static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000709#ifndef OPENSSL_NO_EC
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000710 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000711 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
712 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000713 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000714 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
715 NID_ecdsa_with_SHA384, NID_secp384r1},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000716 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000717 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
718 NID_ecdsa_with_SHA512, NID_secp521r1},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000719 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000720 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
721 NID_ecdsa_with_SHA1, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000722#endif
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000723 {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000724 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
725 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000726 {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000727 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
728 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000729 {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000730 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
731 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000732 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000733 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000734 NID_sha256WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000735 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000736 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000737 NID_sha384WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000738 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000739 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000740 NID_sha512WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000741 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000742 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000743 NID_sha1WithRSAEncryption, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000744#ifndef OPENSSL_NO_DSA
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000745 {NULL, TLSEXT_SIGALG_dsa_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000746 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
747 NID_dsa_with_SHA256, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000748 {NULL, TLSEXT_SIGALG_dsa_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000749 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
750 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000751 {NULL, TLSEXT_SIGALG_dsa_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000752 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
753 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000754 {NULL, TLSEXT_SIGALG_dsa_sha1,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000755 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
756 NID_dsaWithSHA1, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000757#endif
758#ifndef OPENSSL_NO_GOST
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000759 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000760 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
761 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
762 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000763 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000764 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
765 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
766 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000767 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000768 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
769 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
770 NID_undef, NID_undef}
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000771#endif
Matt Caswell703bcee2016-12-14 14:31:21 +0000772};
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000773/* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
774static const SIGALG_LOOKUP legacy_rsa_sigalg = {
775 "rsa_pkcs1_md5_sha1", 0,
776 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
777 EVP_PKEY_RSA, SSL_PKEY_RSA,
778 NID_undef, NID_undef
779};
780
781/*
782 * Default signature algorithm values used if signature algorithms not present.
783 * From RFC5246. Note: order must match certificate index order.
784 */
785static const uint16_t tls_default_sigalg[] = {
786 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
787 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
788 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
789 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
790 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
791 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 /* SSL_PKEY_GOST12_512 */
792};
Matt Caswell703bcee2016-12-14 14:31:21 +0000793
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000794/* Lookup TLS signature algorithm */
795static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
Matt Caswell703bcee2016-12-14 14:31:21 +0000796{
797 size_t i;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000798 const SIGALG_LOOKUP *s;
Matt Caswell703bcee2016-12-14 14:31:21 +0000799
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000800 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
801 i++, s++) {
802 if (s->sigalg == sigalg)
803 return s;
Matt Caswell703bcee2016-12-14 14:31:21 +0000804 }
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000805 return NULL;
806}
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000807/*
808 * Return a signature algorithm for TLS < 1.2 where the signature type
809 * is fixed by the certificate type.
810 */
811static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
812{
813 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
814 return NULL;
815 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
816 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
817
818 if (lu == NULL || ssl_md(lu->hash_idx) == NULL) {
819 return NULL;
820 }
821 return lu;
822 }
823 return &legacy_rsa_sigalg;
824}
825/* Set peer sigalg based key type */
826int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
827{
828 int idx = ssl_cert_type(NULL, pkey);
829
830 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, idx);
831 if (lu == NULL)
832 return 0;
833 s->s3->tmp.peer_sigalg = lu;
834 return 1;
835}
Matt Caswell703bcee2016-12-14 14:31:21 +0000836
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000837static int tls_sigalg_get_sig(uint16_t sigalg)
Matt Caswell703bcee2016-12-14 14:31:21 +0000838{
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000839 const SIGALG_LOOKUP *r = tls1_lookup_sigalg(sigalg);
Matt Caswell703bcee2016-12-14 14:31:21 +0000840
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000841 return r != NULL ? r->sig : 0;
Matt Caswell703bcee2016-12-14 14:31:21 +0000842}
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000843
844size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
Matt Caswell0f113f32015-01-22 03:40:55 +0000845{
846 /*
847 * If Suite B mode use Suite B sigalgs only, ignore any other
848 * preferences.
849 */
Matt Caswelle481f9b2015-05-15 10:49:56 +0100850#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +0000851 switch (tls1_suiteb(s)) {
852 case SSL_CERT_FLAG_SUITEB_128_LOS:
853 *psigs = suiteb_sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +0000854 return OSSL_NELEM(suiteb_sigalgs);
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000855
Matt Caswell0f113f32015-01-22 03:40:55 +0000856 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
857 *psigs = suiteb_sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +0000858 return 1;
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000859
Matt Caswell0f113f32015-01-22 03:40:55 +0000860 case SSL_CERT_FLAG_SUITEB_192_LOS:
Matt Caswell7a531ee2017-01-10 14:23:02 +0000861 *psigs = suiteb_sigalgs + 1;
862 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000863 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100864#endif
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +0000865 /*
866 * We use client_sigalgs (if not NULL) if we're a server
867 * and sending a certificate request or if we're a client and
868 * determining which shared algorithm to use.
869 */
870 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000871 *psigs = s->cert->client_sigalgs;
872 return s->cert->client_sigalgslen;
873 } else if (s->cert->conf_sigalgs) {
874 *psigs = s->cert->conf_sigalgs;
875 return s->cert->conf_sigalgslen;
876 } else {
877 *psigs = tls12_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +0000878 return OSSL_NELEM(tls12_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +0000879 }
880}
881
882/*
883 * Check signature algorithm is consistent with sent supported signature
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +0000884 * algorithms and if so set relevant digest and signature scheme in
885 * s.
Dr. Stephen Hensonec4a50b2012-07-24 18:11:27 +0000886 */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000887int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000888{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000889 const uint16_t *sent_sigs;
Dr. Stephen Henson5554fac2017-01-25 16:46:02 +0000890 const EVP_MD *md = NULL;
Matt Caswell703bcee2016-12-14 14:31:21 +0000891 char sigalgstr[2];
Matt Caswell0f113f32015-01-22 03:40:55 +0000892 size_t sent_sigslen, i;
Matt Caswell536199e2016-12-14 16:37:48 +0000893 int pkeyid = EVP_PKEY_id(pkey);
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000894 const SIGALG_LOOKUP *lu;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000895
Matt Caswell0f113f32015-01-22 03:40:55 +0000896 /* Should never happen */
Matt Caswell536199e2016-12-14 16:37:48 +0000897 if (pkeyid == -1)
Matt Caswell0f113f32015-01-22 03:40:55 +0000898 return -1;
Dr. Stephen Henson5a8916d2017-02-17 16:08:19 +0000899 if (SSL_IS_TLS13(s)) {
900 /* Disallow DSA for TLS 1.3 */
901 if (pkeyid == EVP_PKEY_DSA) {
902 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
903 return 0;
904 }
905 /* Only allow PSS for TLS 1.3 */
906 if (pkeyid == EVP_PKEY_RSA)
907 pkeyid = EVP_PKEY_RSA_PSS;
908 }
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000909 lu = tls1_lookup_sigalg(sig);
910 /*
911 * Check sigalgs is known and key type is consistent with signature:
912 * RSA keys can be used for RSA-PSS
913 */
914 if (lu == NULL || (pkeyid != lu->sig
915 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000916 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
917 return 0;
918 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100919#ifndef OPENSSL_NO_EC
Matt Caswellfe3066e2017-01-03 10:01:39 +0000920 if (pkeyid == EVP_PKEY_EC) {
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000921 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
Dr. Stephen Hensonf1adb002017-01-30 22:02:11 +0000922 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
Dr. Stephen Henson44b63182017-01-30 17:27:35 +0000923
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000924 if (SSL_IS_TLS13(s)) {
925 /* For TLS 1.3 check curve matches signature algorithm */
Dr. Stephen Henson44b63182017-01-30 17:27:35 +0000926
Dr. Stephen Hensona34a9df2017-02-14 14:27:15 +0000927 if (lu->curve != NID_undef && curve != lu->curve) {
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000928 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000929 return 0;
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000930 }
931 } else {
932 unsigned char curve_id[2], comp_id;
Dr. Stephen Henson44b63182017-01-30 17:27:35 +0000933
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000934 /* Check compression and curve matches extensions */
935 if (!tls1_set_ec_id(curve_id, &comp_id, ec))
936 return 0;
937 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
938 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
939 return 0;
940 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000941 if (tls1_suiteb(s)) {
Dr. Stephen Hensonf1adb002017-01-30 22:02:11 +0000942 /* Check sigalg matches a permissible Suite B value */
943 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
944 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
945 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
946 SSL_R_WRONG_SIGNATURE_TYPE);
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000947 return 0;
Dr. Stephen Hensonf1adb002017-01-30 22:02:11 +0000948 }
949 /*
950 * Suite B also requires P-256+SHA256 and P-384+SHA384:
951 * this matches the TLS 1.3 requirements so we can just
952 * check the curve is the expected TLS 1.3 value.
953 * If this fails an inappropriate digest is being used.
954 */
955 if (curve != lu->curve) {
956 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
957 SSL_R_ILLEGAL_SUITEB_DIGEST);
Matt Caswell0f113f32015-01-22 03:40:55 +0000958 return 0;
959 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000960 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000961 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000962 } else if (tls1_suiteb(s)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000963 return 0;
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000964 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100965#endif
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000966
Matt Caswell0f113f32015-01-22 03:40:55 +0000967 /* Check signature matches a type we sent */
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +0000968 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
Matt Caswell536199e2016-12-14 16:37:48 +0000969 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
Matt Caswell703bcee2016-12-14 14:31:21 +0000970 if (sig == *sent_sigs)
Matt Caswell0f113f32015-01-22 03:40:55 +0000971 break;
972 }
973 /* Allow fallback to SHA1 if not strict mode */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000974 if (i == sent_sigslen && (lu->hash != NID_sha1
975 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000976 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
977 return 0;
978 }
Dr. Stephen Henson44b63182017-01-30 17:27:35 +0000979 md = ssl_md(lu->hash_idx);
Dr. Stephen Henson5554fac2017-01-25 16:46:02 +0000980 if (md == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000981 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
982 return 0;
983 }
Matt Caswell703bcee2016-12-14 14:31:21 +0000984 /*
985 * Make sure security callback allows algorithm. For historical reasons we
986 * have to pass the sigalg as a two byte char array.
987 */
988 sigalgstr[0] = (sig >> 8) & 0xff;
989 sigalgstr[1] = sig & 0xff;
Matt Caswell0f113f32015-01-22 03:40:55 +0000990 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
Dr. Stephen Henson5554fac2017-01-25 16:46:02 +0000991 EVP_MD_size(md) * 4, EVP_MD_type(md),
Matt Caswell703bcee2016-12-14 14:31:21 +0000992 (void *)sigalgstr)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000993 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
994 return 0;
995 }
Dr. Stephen Henson6cbebb52017-01-30 18:10:17 +0000996 /* Store the sigalg the peer uses */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000997 s->s3->tmp.peer_sigalg = lu;
Matt Caswell0f113f32015-01-22 03:40:55 +0000998 return 1;
999}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001000
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001001int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
1002{
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +00001003 if (s->s3->tmp.peer_sigalg == NULL)
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001004 return 0;
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +00001005 *pnid = s->s3->tmp.peer_sigalg->sig;
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001006 return 1;
1007}
1008
Matt Caswell0f113f32015-01-22 03:40:55 +00001009/*
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001010 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1011 * supported, doesn't appear in supported signature algorithms, isn't supported
1012 * by the enabled protocol versions or by the security level.
1013 *
1014 * This function should only be used for checking which ciphers are supported
1015 * by the client.
1016 *
1017 * Call ssl_cipher_disabled() to check that it's enabled or not.
Dr. Stephen Hensonb7bfe692012-07-18 14:09:46 +00001018 */
1019void ssl_set_client_disabled(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001020{
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001021 s->s3->tmp.mask_a = 0;
1022 s->s3->tmp.mask_k = 0;
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001023 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001024 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
Emilia Kaspera230b262016-08-05 19:03:17 +02001025#ifndef OPENSSL_NO_PSK
Matt Caswell0f113f32015-01-22 03:40:55 +00001026 /* with PSK there must be client callback set */
1027 if (!s->psk_client_callback) {
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001028 s->s3->tmp.mask_a |= SSL_aPSK;
Dr. Stephen Hensonfe5eef32015-06-28 17:01:07 +01001029 s->s3->tmp.mask_k |= SSL_PSK;
Matt Caswell0f113f32015-01-22 03:40:55 +00001030 }
Emilia Kaspera230b262016-08-05 19:03:17 +02001031#endif /* OPENSSL_NO_PSK */
Matt Caswelle481f9b2015-05-15 10:49:56 +01001032#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00001033 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001034 s->s3->tmp.mask_a |= SSL_aSRP;
1035 s->s3->tmp.mask_k |= SSL_kSRP;
Matt Caswell0f113f32015-01-22 03:40:55 +00001036 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001037#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001038}
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +00001039
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001040/*
1041 * ssl_cipher_disabled - check that a cipher is disabled or not
1042 * @s: SSL connection that you want to use the cipher on
1043 * @c: cipher to check
1044 * @op: Security check that you want to do
1045 *
1046 * Returns 1 when it's disabled, 0 when enabled.
1047 */
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001048int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00001049{
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001050 if (c->algorithm_mkey & s->s3->tmp.mask_k
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001051 || c->algorithm_auth & s->s3->tmp.mask_a)
Matt Caswell0f113f32015-01-22 03:40:55 +00001052 return 1;
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001053 if (s->s3->tmp.max_ver == 0)
1054 return 1;
1055 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
Emilia Kaspera230b262016-08-05 19:03:17 +02001056 || (c->max_tls < s->s3->tmp.min_ver)))
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001057 return 1;
1058 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
Emilia Kaspera230b262016-08-05 19:03:17 +02001059 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001060 return 1;
1061
Matt Caswell0f113f32015-01-22 03:40:55 +00001062 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1063}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001064
Matt Caswell7da160b2016-11-25 10:22:02 +00001065int tls_use_ticket(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001066{
Matt Caswell08191292017-01-19 11:23:06 +00001067 if ((s->options & SSL_OP_NO_TICKET))
Matt Caswell0f113f32015-01-22 03:40:55 +00001068 return 0;
1069 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1070}
Dr. Stephen Henson8b8e5be2014-01-14 14:55:21 +00001071
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001072/* Initialise digests to default values */
Dr. Stephen Hensona0f63822015-11-24 00:08:35 +00001073void ssl_set_default_md(SSL *s)
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001074{
1075 const EVP_MD **pmd = s->s3->tmp.md;
1076#ifndef OPENSSL_NO_DSA
Dr. Stephen Henson152fbc22015-11-29 16:27:08 +00001077 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001078#endif
1079#ifndef OPENSSL_NO_RSA
Dr. Stephen Hensond18d31a2015-08-29 22:11:05 +01001080 if (SSL_USE_SIGALGS(s))
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001081 pmd[SSL_PKEY_RSA] = ssl_md(SSL_MD_SHA1_IDX);
Dr. Stephen Hensond18d31a2015-08-29 22:11:05 +01001082 else
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001083 pmd[SSL_PKEY_RSA] = ssl_md(SSL_MD_MD5_SHA1_IDX);
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001084#endif
1085#ifndef OPENSSL_NO_EC
Dr. Stephen Henson152fbc22015-11-29 16:27:08 +00001086 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001087#endif
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001088#ifndef OPENSSL_NO_GOST
Dr. Stephen Henson152fbc22015-11-29 16:27:08 +00001089 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
1090 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
1091 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001092#endif
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001093}
Bodo Möller36ca4ba2006-03-11 23:46:37 +00001094
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001095int tls1_set_server_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001096{
1097 int al;
1098 size_t i;
FdaSilvaYY8483a002016-03-10 21:34:48 +01001099
1100 /* Clear any shared signature algorithms */
Rich Salzb548a1f2015-05-01 10:02:07 -04001101 OPENSSL_free(s->cert->shared_sigalgs);
1102 s->cert->shared_sigalgs = NULL;
1103 s->cert->shared_sigalgslen = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001104 /* Clear certificate digests and validity flags */
1105 for (i = 0; i < SSL_PKEY_NUM; i++) {
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001106 s->s3->tmp.md[i] = NULL;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001107 s->s3->tmp.valid_flags[i] = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001108 }
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001109
Matt Caswell0f113f32015-01-22 03:40:55 +00001110 /* If sigalgs received process it. */
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001111 if (s->s3->tmp.peer_sigalgs) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001112 if (!tls1_process_sigalgs(s)) {
1113 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
1114 al = SSL_AD_INTERNAL_ERROR;
1115 goto err;
1116 }
1117 /* Fatal error is no shared signature algorithms */
1118 if (!s->cert->shared_sigalgs) {
1119 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
FdaSilvaYYf430ba32016-06-19 19:39:01 +02001120 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
Matt Caswell0f113f32015-01-22 03:40:55 +00001121 al = SSL_AD_ILLEGAL_PARAMETER;
1122 goto err;
1123 }
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001124 } else {
1125 ssl_set_default_md(s);
1126 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001127 return 1;
1128 err:
1129 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1130 return 0;
1131}
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001132
Tim Hudson1d97c842014-12-28 12:48:40 +10001133/*-
Matt Caswell1ab38362016-10-22 17:24:37 +01001134 * Gets the ticket information supplied by the client if any.
Dr. Stephen Hensone7f0d922015-12-04 19:48:15 +00001135 *
Matt Caswell1ab38362016-10-22 17:24:37 +01001136 * hello: The parsed ClientHello data
Bodo Möllerc519e892011-09-05 13:36:23 +00001137 * ret: (output) on return, if a ticket was decrypted, then this is set to
1138 * point to the resulting session.
1139 *
1140 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
1141 * ciphersuite, in which case we have no use for session tickets and one will
Rich Salzaff8c122016-12-08 14:18:40 -05001142 * never be decrypted, nor will s->ext.ticket_expected be set to 1.
Bodo Möllerc519e892011-09-05 13:36:23 +00001143 *
1144 * Returns:
1145 * -1: fatal error, either from parsing or decrypting the ticket.
1146 * 0: no ticket was found (or was ignored, based on settings).
1147 * 1: a zero length extension was found, indicating that the client supports
1148 * session tickets but doesn't currently have one to offer.
1149 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
1150 * couldn't be decrypted because of a non-fatal error.
1151 * 3: a ticket was successfully decrypted and *ret was set.
1152 *
1153 * Side effects:
Rich Salzaff8c122016-12-08 14:18:40 -05001154 * Sets s->ext.ticket_expected to 1 if the server will have to issue
Bodo Möllerc519e892011-09-05 13:36:23 +00001155 * a new session ticket to the client because the client indicated support
1156 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1157 * a session ticket or we couldn't use the one it gave us, or if
Rich Salzaff8c122016-12-08 14:18:40 -05001158 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1159 * Otherwise, s->ext.ticket_expected is set to 0.
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001160 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001161TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1162 SSL_SESSION **ret)
Matt Caswell0f113f32015-01-22 03:40:55 +00001163{
Matt Caswell1ab38362016-10-22 17:24:37 +01001164 int retv;
Matt Caswell1ab38362016-10-22 17:24:37 +01001165 size_t size;
1166 RAW_EXTENSION *ticketext;
Dr. Stephen Hensone7f0d922015-12-04 19:48:15 +00001167
Matt Caswell0f113f32015-01-22 03:40:55 +00001168 *ret = NULL;
Rich Salzaff8c122016-12-08 14:18:40 -05001169 s->ext.ticket_expected = 0;
Dr. Stephen Hensone8da6a12008-09-03 22:17:11 +00001170
Matt Caswell0f113f32015-01-22 03:40:55 +00001171 /*
Matt Caswell9362c932016-11-08 16:10:21 +00001172 * If tickets disabled or not supported by the protocol version
1173 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
Matt Caswell0f113f32015-01-22 03:40:55 +00001174 * resumption.
1175 */
Matt Caswell1ab38362016-10-22 17:24:37 +01001176 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
Matt Caswellddf6ec02017-01-20 16:01:27 +00001177 return TICKET_NONE;
Matt Caswell9ceb2422015-04-16 10:06:25 +01001178
Matt Caswell70af3d82016-11-28 09:31:59 +00001179 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1180 if (!ticketext->present)
Matt Caswellddf6ec02017-01-20 16:01:27 +00001181 return TICKET_NONE;
Matt Caswell1ab38362016-10-22 17:24:37 +01001182
1183 size = PACKET_remaining(&ticketext->data);
1184 if (size == 0) {
1185 /*
1186 * The client will accept a ticket but doesn't currently have
1187 * one.
1188 */
Rich Salzaff8c122016-12-08 14:18:40 -05001189 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001190 return TICKET_EMPTY;
Matt Caswell9ceb2422015-04-16 10:06:25 +01001191 }
Rich Salzaff8c122016-12-08 14:18:40 -05001192 if (s->ext.session_secret_cb) {
Matt Caswell1ab38362016-10-22 17:24:37 +01001193 /*
1194 * Indicate that the ticket couldn't be decrypted rather than
1195 * generating the session from ticket now, trigger
1196 * abbreviated handshake based on external mechanism to
1197 * calculate the master secret later.
1198 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001199 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001200 }
Matt Caswell70af3d82016-11-28 09:31:59 +00001201
1202 retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1203 hello->session_id, hello->session_id_len, ret);
Matt Caswell1ab38362016-10-22 17:24:37 +01001204 switch (retv) {
Matt Caswell61c32642017-01-27 12:11:23 +00001205 case TICKET_NO_DECRYPT:
Rich Salzaff8c122016-12-08 14:18:40 -05001206 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001207 return TICKET_NO_DECRYPT;
Matt Caswell1ab38362016-10-22 17:24:37 +01001208
Matt Caswell61c32642017-01-27 12:11:23 +00001209 case TICKET_SUCCESS:
Matt Caswellddf6ec02017-01-20 16:01:27 +00001210 return TICKET_SUCCESS;
Matt Caswell1ab38362016-10-22 17:24:37 +01001211
Matt Caswell61c32642017-01-27 12:11:23 +00001212 case TICKET_SUCCESS_RENEW:
Rich Salzaff8c122016-12-08 14:18:40 -05001213 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001214 return TICKET_SUCCESS;
Matt Caswell1ab38362016-10-22 17:24:37 +01001215
Matt Caswell61c32642017-01-27 12:11:23 +00001216 default:
Matt Caswellddf6ec02017-01-20 16:01:27 +00001217 return TICKET_FATAL_ERR_OTHER;
Matt Caswell1ab38362016-10-22 17:24:37 +01001218 }
1219}
1220
Tim Hudson1d97c842014-12-28 12:48:40 +10001221/*-
1222 * tls_decrypt_ticket attempts to decrypt a session ticket.
Bodo Möllerc519e892011-09-05 13:36:23 +00001223 *
1224 * etick: points to the body of the session ticket extension.
FdaSilvaYY8483a002016-03-10 21:34:48 +01001225 * eticklen: the length of the session tickets extension.
Bodo Möllerc519e892011-09-05 13:36:23 +00001226 * sess_id: points at the session ID.
1227 * sesslen: the length of the session ID.
1228 * psess: (output) on return, if a ticket was decrypted, then this is set to
1229 * point to the resulting session.
Bodo Möllerc519e892011-09-05 13:36:23 +00001230 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001231TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1232 size_t eticklen, const unsigned char *sess_id,
1233 size_t sesslen, SSL_SESSION **psess)
Matt Caswell0f113f32015-01-22 03:40:55 +00001234{
1235 SSL_SESSION *sess;
1236 unsigned char *sdec;
1237 const unsigned char *p;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001238 int slen, renew_ticket = 0, declen;
1239 TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER;
Matt Caswell348240c2016-10-19 15:11:24 +01001240 size_t mlen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001241 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
Richard Levittebf7c6812015-11-30 13:44:28 +01001242 HMAC_CTX *hctx = NULL;
Richard Levitte846ec072015-12-13 22:08:41 +01001243 EVP_CIPHER_CTX *ctx;
Todd Short222da972017-01-31 15:32:50 -05001244 SSL_CTX *tctx = s->session_ctx;
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001245
Matt Caswell0f113f32015-01-22 03:40:55 +00001246 /* Initialize session ticket encryption and HMAC contexts */
Richard Levittebf7c6812015-11-30 13:44:28 +01001247 hctx = HMAC_CTX_new();
1248 if (hctx == NULL)
Matt Caswell1053a6e2017-01-18 16:28:23 +00001249 return TICKET_FATAL_ERR_MALLOC;
Richard Levitte846ec072015-12-13 22:08:41 +01001250 ctx = EVP_CIPHER_CTX_new();
Matt Caswell35b1a432016-02-13 23:22:45 +00001251 if (ctx == NULL) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001252 ret = TICKET_FATAL_ERR_MALLOC;
Matt Caswell35b1a432016-02-13 23:22:45 +00001253 goto err;
1254 }
Rich Salzaff8c122016-12-08 14:18:40 -05001255 if (tctx->ext.ticket_key_cb) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001256 unsigned char *nctick = (unsigned char *)etick;
Rich Salzaff8c122016-12-08 14:18:40 -05001257 int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16,
Richard Levitte846ec072015-12-13 22:08:41 +01001258 ctx, hctx, 0);
Matt Caswell0f113f32015-01-22 03:40:55 +00001259 if (rv < 0)
Matt Caswell35b1a432016-02-13 23:22:45 +00001260 goto err;
1261 if (rv == 0) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001262 ret = TICKET_NO_DECRYPT;
Matt Caswell35b1a432016-02-13 23:22:45 +00001263 goto err;
1264 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001265 if (rv == 2)
1266 renew_ticket = 1;
1267 } else {
1268 /* Check key name matches */
Rich Salzaff8c122016-12-08 14:18:40 -05001269 if (memcmp(etick, tctx->ext.tick_key_name,
1270 sizeof(tctx->ext.tick_key_name)) != 0) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001271 ret = TICKET_NO_DECRYPT;
Matt Caswell35b1a432016-02-13 23:22:45 +00001272 goto err;
1273 }
Rich Salzaff8c122016-12-08 14:18:40 -05001274 if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
1275 sizeof(tctx->ext.tick_hmac_key),
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001276 EVP_sha256(), NULL) <= 0
Emilia Kaspera230b262016-08-05 19:03:17 +02001277 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
Rich Salzaff8c122016-12-08 14:18:40 -05001278 tctx->ext.tick_aes_key,
Matt Caswell1053a6e2017-01-18 16:28:23 +00001279 etick
1280 + sizeof(tctx->ext.tick_key_name)) <= 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001281 goto err;
Emilia Kaspera230b262016-08-05 19:03:17 +02001282 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001283 }
1284 /*
1285 * Attempt to process session ticket, first conduct sanity and integrity
1286 * checks on ticket.
1287 */
Richard Levittebf7c6812015-11-30 13:44:28 +01001288 mlen = HMAC_size(hctx);
Matt Caswell348240c2016-10-19 15:11:24 +01001289 if (mlen == 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001290 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001291 }
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001292 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1293 if (eticklen <=
Matt Caswell348240c2016-10-19 15:11:24 +01001294 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001295 ret = TICKET_NO_DECRYPT;
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001296 goto err;
1297 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001298 eticklen -= mlen;
1299 /* Check HMAC of encrypted ticket */
Richard Levittebf7c6812015-11-30 13:44:28 +01001300 if (HMAC_Update(hctx, etick, eticklen) <= 0
Emilia Kaspera230b262016-08-05 19:03:17 +02001301 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001302 goto err;
1303 }
Richard Levittebf7c6812015-11-30 13:44:28 +01001304 HMAC_CTX_free(hctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001305 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
Richard Levitte846ec072015-12-13 22:08:41 +01001306 EVP_CIPHER_CTX_free(ctx);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001307 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001308 }
1309 /* Attempt to decrypt session data */
1310 /* Move p after IV to start of encrypted ticket, update length */
Bernd Edlinger57b0d652017-02-13 18:36:13 +01001311 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1312 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001313 sdec = OPENSSL_malloc(eticklen);
Matt Caswell348240c2016-10-19 15:11:24 +01001314 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1315 (int)eticklen) <= 0) {
Richard Levitte846ec072015-12-13 22:08:41 +01001316 EVP_CIPHER_CTX_free(ctx);
Matt Caswelld1247df2016-03-15 11:38:56 +00001317 OPENSSL_free(sdec);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001318 return TICKET_FATAL_ERR_OTHER;
Matt Caswell0f113f32015-01-22 03:40:55 +00001319 }
Matt Caswell348240c2016-10-19 15:11:24 +01001320 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
Richard Levitte846ec072015-12-13 22:08:41 +01001321 EVP_CIPHER_CTX_free(ctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001322 OPENSSL_free(sdec);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001323 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001324 }
Matt Caswell348240c2016-10-19 15:11:24 +01001325 slen += declen;
Richard Levitte846ec072015-12-13 22:08:41 +01001326 EVP_CIPHER_CTX_free(ctx);
1327 ctx = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001328 p = sdec;
Bodo Möllerc519e892011-09-05 13:36:23 +00001329
Matt Caswell0f113f32015-01-22 03:40:55 +00001330 sess = d2i_SSL_SESSION(NULL, &p, slen);
1331 OPENSSL_free(sdec);
1332 if (sess) {
1333 /*
1334 * The session ID, if non-empty, is used by some clients to detect
1335 * that the ticket has been accepted. So we copy it to the session
1336 * structure. If it is empty set length to zero as required by
1337 * standard.
1338 */
1339 if (sesslen)
1340 memcpy(sess->session_id, sess_id, sesslen);
1341 sess->session_id_length = sesslen;
1342 *psess = sess;
1343 if (renew_ticket)
Matt Caswell1053a6e2017-01-18 16:28:23 +00001344 return TICKET_SUCCESS_RENEW;
Matt Caswell0f113f32015-01-22 03:40:55 +00001345 else
Matt Caswell1053a6e2017-01-18 16:28:23 +00001346 return TICKET_SUCCESS;
Matt Caswell0f113f32015-01-22 03:40:55 +00001347 }
1348 ERR_clear_error();
1349 /*
1350 * For session parse failure, indicate that we need to send a new ticket.
1351 */
Matt Caswell1053a6e2017-01-18 16:28:23 +00001352 return TICKET_NO_DECRYPT;
Emilia Kaspera230b262016-08-05 19:03:17 +02001353 err:
Richard Levitte846ec072015-12-13 22:08:41 +01001354 EVP_CIPHER_CTX_free(ctx);
Richard Levittebf7c6812015-11-30 13:44:28 +01001355 HMAC_CTX_free(hctx);
Matt Caswell35b1a432016-02-13 23:22:45 +00001356 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +00001357}
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001358
Matt Caswell536199e2016-12-14 16:37:48 +00001359static int tls12_get_pkey_idx(int sig_nid)
Matt Caswell0f113f32015-01-22 03:40:55 +00001360{
Matt Caswell536199e2016-12-14 16:37:48 +00001361 switch (sig_nid) {
Matt Caswelle481f9b2015-05-15 10:49:56 +01001362#ifndef OPENSSL_NO_RSA
Matt Caswell536199e2016-12-14 16:37:48 +00001363 case EVP_PKEY_RSA:
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001364 return SSL_PKEY_RSA;
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +00001365 /*
1366 * For now return RSA key for PSS. When we support PSS only keys
1367 * this will need to be updated.
1368 */
1369 case EVP_PKEY_RSA_PSS:
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001370 return SSL_PKEY_RSA;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001371#endif
1372#ifndef OPENSSL_NO_DSA
Matt Caswell536199e2016-12-14 16:37:48 +00001373 case EVP_PKEY_DSA:
Matt Caswell0f113f32015-01-22 03:40:55 +00001374 return SSL_PKEY_DSA_SIGN;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001375#endif
1376#ifndef OPENSSL_NO_EC
Matt Caswell536199e2016-12-14 16:37:48 +00001377 case EVP_PKEY_EC:
Matt Caswell0f113f32015-01-22 03:40:55 +00001378 return SSL_PKEY_ECC;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001379#endif
Emilia Kaspera230b262016-08-05 19:03:17 +02001380#ifndef OPENSSL_NO_GOST
Matt Caswell536199e2016-12-14 16:37:48 +00001381 case NID_id_GostR3410_2001:
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001382 return SSL_PKEY_GOST01;
1383
Matt Caswell536199e2016-12-14 16:37:48 +00001384 case NID_id_GostR3410_2012_256:
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001385 return SSL_PKEY_GOST12_256;
1386
Matt Caswell536199e2016-12-14 16:37:48 +00001387 case NID_id_GostR3410_2012_512:
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001388 return SSL_PKEY_GOST12_512;
Emilia Kaspera230b262016-08-05 19:03:17 +02001389#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001390 }
1391 return -1;
1392}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001393
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001394/* Check to see if a signature algorithm is allowed */
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001395static int tls12_sigalg_allowed(SSL *s, int op, uint16_t ptmp)
Matt Caswell0f113f32015-01-22 03:40:55 +00001396{
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001397 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(ptmp);
Matt Caswell703bcee2016-12-14 14:31:21 +00001398 unsigned char sigalgstr[2];
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001399 int secbits;
Matt Caswell703bcee2016-12-14 14:31:21 +00001400
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001401 /* See if sigalgs is recognised and if hash is enabled */
1402 if (lu == NULL || ssl_md(lu->hash_idx) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001403 return 0;
1404 /* See if public key algorithm allowed */
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001405 if (tls12_get_pkey_idx(lu->sig) == -1)
Matt Caswell0f113f32015-01-22 03:40:55 +00001406 return 0;
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001407 /* Security bits: half digest bits */
1408 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
Matt Caswell0f113f32015-01-22 03:40:55 +00001409 /* Finally see if security callback allows it */
Matt Caswell703bcee2016-12-14 14:31:21 +00001410 sigalgstr[0] = (ptmp >> 8) & 0xff;
1411 sigalgstr[1] = ptmp & 0xff;
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001412 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
Matt Caswell0f113f32015-01-22 03:40:55 +00001413}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001414
Matt Caswell0f113f32015-01-22 03:40:55 +00001415/*
1416 * Get a mask of disabled public key algorithms based on supported signature
1417 * algorithms. For example if no signature algorithm supports RSA then RSA is
1418 * disabled.
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001419 */
1420
Dr. Stephen Henson90d9e492015-11-05 16:14:17 +00001421void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00001422{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001423 const uint16_t *sigalgs;
Matt Caswell0f113f32015-01-22 03:40:55 +00001424 size_t i, sigalgslen;
1425 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1426 /*
1427 * Now go through all signature algorithms seeing if we support any for
1428 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
1429 * down calls to security callback only check if we have to.
1430 */
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +00001431 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
Matt Caswell703bcee2016-12-14 14:31:21 +00001432 for (i = 0; i < sigalgslen; i ++, sigalgs++) {
1433 switch (tls_sigalg_get_sig(*sigalgs)) {
Matt Caswelle481f9b2015-05-15 10:49:56 +01001434#ifndef OPENSSL_NO_RSA
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +00001435 /* Any RSA-PSS signature algorithms also mean we allow RSA */
1436 case EVP_PKEY_RSA_PSS:
Matt Caswell536199e2016-12-14 16:37:48 +00001437 case EVP_PKEY_RSA:
Matt Caswell703bcee2016-12-14 14:31:21 +00001438 if (!have_rsa && tls12_sigalg_allowed(s, op, *sigalgs))
Matt Caswell0f113f32015-01-22 03:40:55 +00001439 have_rsa = 1;
1440 break;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001441#endif
1442#ifndef OPENSSL_NO_DSA
Matt Caswell536199e2016-12-14 16:37:48 +00001443 case EVP_PKEY_DSA:
Matt Caswell703bcee2016-12-14 14:31:21 +00001444 if (!have_dsa && tls12_sigalg_allowed(s, op, *sigalgs))
Matt Caswell0f113f32015-01-22 03:40:55 +00001445 have_dsa = 1;
1446 break;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001447#endif
1448#ifndef OPENSSL_NO_EC
Matt Caswell536199e2016-12-14 16:37:48 +00001449 case EVP_PKEY_EC:
Matt Caswell703bcee2016-12-14 14:31:21 +00001450 if (!have_ecdsa && tls12_sigalg_allowed(s, op, *sigalgs))
Matt Caswell0f113f32015-01-22 03:40:55 +00001451 have_ecdsa = 1;
1452 break;
Matt Caswelle481f9b2015-05-15 10:49:56 +01001453#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001454 }
1455 }
1456 if (!have_rsa)
1457 *pmask_a |= SSL_aRSA;
1458 if (!have_dsa)
1459 *pmask_a |= SSL_aDSS;
1460 if (!have_ecdsa)
1461 *pmask_a |= SSL_aECDSA;
1462}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001463
Matt Caswellae2f7b32016-09-05 17:34:04 +01001464int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001465 const uint16_t *psig, size_t psiglen)
Matt Caswell2c7b4db2016-08-03 20:57:52 +01001466{
1467 size_t i;
Matt Caswellc0f9e232016-09-13 09:40:38 +01001468
Matt Caswell703bcee2016-12-14 14:31:21 +00001469 for (i = 0; i < psiglen; i++, psig++) {
1470 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, *psig)) {
1471 if (!WPACKET_put_bytes_u16(pkt, *psig))
Matt Caswell2c7b4db2016-08-03 20:57:52 +01001472 return 0;
1473 }
1474 }
1475 return 1;
1476}
1477
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001478/* Given preference and allowed sigalgs set shared sigalgs */
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001479static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001480 const uint16_t *pref, size_t preflen,
1481 const uint16_t *allow, size_t allowlen)
Matt Caswell0f113f32015-01-22 03:40:55 +00001482{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001483 const uint16_t *ptmp, *atmp;
Matt Caswell0f113f32015-01-22 03:40:55 +00001484 size_t i, j, nmatch = 0;
Matt Caswell703bcee2016-12-14 14:31:21 +00001485 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001486 /* Skip disabled hashes or signature algorithms */
Matt Caswell703bcee2016-12-14 14:31:21 +00001487 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, *ptmp))
Matt Caswell0f113f32015-01-22 03:40:55 +00001488 continue;
Matt Caswell703bcee2016-12-14 14:31:21 +00001489 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1490 if (*ptmp == *atmp) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001491 nmatch++;
1492 if (shsig) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001493 *shsig = tls1_lookup_sigalg(*ptmp);
Matt Caswell0f113f32015-01-22 03:40:55 +00001494 shsig++;
1495 }
1496 break;
1497 }
1498 }
1499 }
1500 return nmatch;
1501}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001502
1503/* Set shared signature algorithms for SSL structures */
1504static int tls1_set_shared_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001505{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001506 const uint16_t *pref, *allow, *conf;
Matt Caswell0f113f32015-01-22 03:40:55 +00001507 size_t preflen, allowlen, conflen;
1508 size_t nmatch;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001509 const SIGALG_LOOKUP **salgs = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001510 CERT *c = s->cert;
1511 unsigned int is_suiteb = tls1_suiteb(s);
Rich Salzb548a1f2015-05-01 10:02:07 -04001512
1513 OPENSSL_free(c->shared_sigalgs);
1514 c->shared_sigalgs = NULL;
1515 c->shared_sigalgslen = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001516 /* If client use client signature algorithms if not NULL */
1517 if (!s->server && c->client_sigalgs && !is_suiteb) {
1518 conf = c->client_sigalgs;
1519 conflen = c->client_sigalgslen;
1520 } else if (c->conf_sigalgs && !is_suiteb) {
1521 conf = c->conf_sigalgs;
1522 conflen = c->conf_sigalgslen;
1523 } else
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +00001524 conflen = tls12_get_psigalgs(s, 0, &conf);
Matt Caswell0f113f32015-01-22 03:40:55 +00001525 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1526 pref = conf;
1527 preflen = conflen;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001528 allow = s->s3->tmp.peer_sigalgs;
1529 allowlen = s->s3->tmp.peer_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001530 } else {
1531 allow = conf;
1532 allowlen = conflen;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001533 pref = s->s3->tmp.peer_sigalgs;
1534 preflen = s->s3->tmp.peer_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001535 }
1536 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
Dr. Stephen Henson34e3edb2015-03-03 13:20:57 +00001537 if (nmatch) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001538 salgs = OPENSSL_malloc(nmatch * sizeof(*salgs));
Matt Caswella71edf32015-10-30 10:05:53 +00001539 if (salgs == NULL)
Dr. Stephen Henson34e3edb2015-03-03 13:20:57 +00001540 return 0;
1541 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1542 } else {
1543 salgs = NULL;
1544 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001545 c->shared_sigalgs = salgs;
1546 c->shared_sigalgslen = nmatch;
1547 return 1;
1548}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001549
Dr. Stephen Henson6b7be582011-05-06 13:00:07 +00001550/* Set preferred digest for each key type */
1551
Matt Caswell703bcee2016-12-14 14:31:21 +00001552int tls1_save_sigalgs(SSL *s, PACKET *pkt)
Matt Caswell0f113f32015-01-22 03:40:55 +00001553{
1554 CERT *c = s->cert;
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001555 unsigned int stmp;
Matt Caswell703bcee2016-12-14 14:31:21 +00001556 size_t size, i;
1557
Matt Caswell0f113f32015-01-22 03:40:55 +00001558 /* Extension ignored for inappropriate versions */
1559 if (!SSL_USE_SIGALGS(s))
1560 return 1;
1561 /* Should never happen */
1562 if (!c)
1563 return 0;
Dr. Stephen Henson6b7be582011-05-06 13:00:07 +00001564
Matt Caswell703bcee2016-12-14 14:31:21 +00001565 size = PACKET_remaining(pkt);
1566
1567 /* Invalid data length */
1568 if ((size & 1) != 0)
1569 return 0;
1570
1571 size >>= 1;
1572
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001573 OPENSSL_free(s->s3->tmp.peer_sigalgs);
Matt Caswell536199e2016-12-14 16:37:48 +00001574 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(size
1575 * sizeof(*s->s3->tmp.peer_sigalgs));
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001576 if (s->s3->tmp.peer_sigalgs == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001577 return 0;
Matt Caswell703bcee2016-12-14 14:31:21 +00001578 s->s3->tmp.peer_sigalgslen = size;
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001579 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
1580 s->s3->tmp.peer_sigalgs[i] = stmp;
Matt Caswell703bcee2016-12-14 14:31:21 +00001581
1582 if (i != size)
1583 return 0;
1584
Matt Caswell0f113f32015-01-22 03:40:55 +00001585 return 1;
1586}
Dr. Stephen Henson6b7be582011-05-06 13:00:07 +00001587
Dr. Stephen Hensonc800c272014-10-09 20:37:27 +01001588int tls1_process_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001589{
1590 int idx;
1591 size_t i;
1592 const EVP_MD *md;
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001593 const EVP_MD **pmd = s->s3->tmp.md;
Dr. Stephen Hensonf7d53482015-07-14 23:19:11 +01001594 uint32_t *pvalid = s->s3->tmp.valid_flags;
Matt Caswell0f113f32015-01-22 03:40:55 +00001595 CERT *c = s->cert;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001596
Matt Caswell0f113f32015-01-22 03:40:55 +00001597 if (!tls1_set_shared_sigalgs(s))
1598 return 0;
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001599
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001600 for (i = 0; i < c->shared_sigalgslen; i++) {
1601 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
1602
Matt Caswell523fb322016-12-30 14:08:19 +00001603 /* Ignore PKCS1 based sig algs in TLSv1.3 */
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001604 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
Matt Caswell523fb322016-12-30 14:08:19 +00001605 continue;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001606 idx = tls12_get_pkey_idx(sigptr->sig);
Dr. Stephen Henson8fd19b22017-02-10 04:22:18 +00001607 if (idx >= 0 && pmd[idx] == NULL) {
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001608 md = ssl_md(sigptr->hash_idx);
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001609 pmd[idx] = md;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001610 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
Matt Caswell0f113f32015-01-22 03:40:55 +00001611 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001612 }
1613 /*
Matt Caswell523fb322016-12-30 14:08:19 +00001614 * In strict mode or TLS1.3 leave unset digests as NULL to indicate we can't
1615 * use the certificate for signing.
Matt Caswell0f113f32015-01-22 03:40:55 +00001616 */
Matt Caswell523fb322016-12-30 14:08:19 +00001617 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
1618 && !SSL_IS_TLS13(s)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001619 /*
1620 * Set any remaining keys to default values. NOTE: if alg is not
1621 * supported it stays as NULL.
1622 */
Matt Caswelle481f9b2015-05-15 10:49:56 +01001623#ifndef OPENSSL_NO_DSA
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001624 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
1625 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
Matt Caswelle481f9b2015-05-15 10:49:56 +01001626#endif
1627#ifndef OPENSSL_NO_RSA
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001628 if (pmd[SSL_PKEY_RSA] == NULL) {
1629 pmd[SSL_PKEY_RSA] = EVP_sha1();
Matt Caswell0f113f32015-01-22 03:40:55 +00001630 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001631#endif
1632#ifndef OPENSSL_NO_EC
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001633 if (pmd[SSL_PKEY_ECC] == NULL)
1634 pmd[SSL_PKEY_ECC] = EVP_sha1();
Matt Caswelle481f9b2015-05-15 10:49:56 +01001635#endif
Emilia Kaspera230b262016-08-05 19:03:17 +02001636#ifndef OPENSSL_NO_GOST
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001637 if (pmd[SSL_PKEY_GOST01] == NULL)
1638 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
1639 if (pmd[SSL_PKEY_GOST12_256] == NULL)
Emilia Kaspera230b262016-08-05 19:03:17 +02001640 pmd[SSL_PKEY_GOST12_256] =
1641 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001642 if (pmd[SSL_PKEY_GOST12_512] == NULL)
Emilia Kaspera230b262016-08-05 19:03:17 +02001643 pmd[SSL_PKEY_GOST12_512] =
1644 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
1645#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001646 }
1647 return 1;
1648}
Dr. Stephen Henson48175042011-12-31 22:59:57 +00001649
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00001650int SSL_get_sigalgs(SSL *s, int idx,
Matt Caswell0f113f32015-01-22 03:40:55 +00001651 int *psign, int *phash, int *psignhash,
1652 unsigned char *rsig, unsigned char *rhash)
1653{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001654 uint16_t *psig = s->s3->tmp.peer_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +00001655 size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
Matt Caswell348240c2016-10-19 15:11:24 +01001656 if (psig == NULL || numsigalgs > INT_MAX)
Matt Caswell0f113f32015-01-22 03:40:55 +00001657 return 0;
1658 if (idx >= 0) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001659 const SIGALG_LOOKUP *lu;
1660
Matt Caswell703bcee2016-12-14 14:31:21 +00001661 if (idx >= (int)numsigalgs)
Matt Caswell0f113f32015-01-22 03:40:55 +00001662 return 0;
1663 psig += idx;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001664 if (rhash != NULL)
Matt Caswell536199e2016-12-14 16:37:48 +00001665 *rhash = (unsigned char)((*psig >> 8) & 0xff);
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001666 if (rsig != NULL)
Matt Caswell536199e2016-12-14 16:37:48 +00001667 *rsig = (unsigned char)(*psig & 0xff);
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001668 lu = tls1_lookup_sigalg(*psig);
1669 if (psign != NULL)
1670 *psign = lu != NULL ? lu->sig : NID_undef;
1671 if (phash != NULL)
1672 *phash = lu != NULL ? lu->hash : NID_undef;
1673 if (psignhash != NULL)
1674 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
Matt Caswell0f113f32015-01-22 03:40:55 +00001675 }
Matt Caswell348240c2016-10-19 15:11:24 +01001676 return (int)numsigalgs;
Matt Caswell0f113f32015-01-22 03:40:55 +00001677}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001678
1679int SSL_get_shared_sigalgs(SSL *s, int idx,
Matt Caswell0f113f32015-01-22 03:40:55 +00001680 int *psign, int *phash, int *psignhash,
1681 unsigned char *rsig, unsigned char *rhash)
1682{
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001683 const SIGALG_LOOKUP *shsigalgs;
1684 if (s->cert->shared_sigalgs == NULL
Peter Wu6d047e02017-02-02 12:11:10 +01001685 || idx < 0
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001686 || idx >= (int)s->cert->shared_sigalgslen
1687 || s->cert->shared_sigalgslen > INT_MAX)
Matt Caswell0f113f32015-01-22 03:40:55 +00001688 return 0;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001689 shsigalgs = s->cert->shared_sigalgs[idx];
1690 if (phash != NULL)
1691 *phash = shsigalgs->hash;
1692 if (psign != NULL)
1693 *psign = shsigalgs->sig;
1694 if (psignhash != NULL)
1695 *psignhash = shsigalgs->sigandhash;
1696 if (rsig != NULL)
1697 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
1698 if (rhash != NULL)
1699 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
Matt Caswell348240c2016-10-19 15:11:24 +01001700 return (int)s->cert->shared_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001701}
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00001702
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001703/* Maximum possible number of unique entries in sigalgs array */
1704#define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
Matt Caswell0f113f32015-01-22 03:40:55 +00001705
1706typedef struct {
1707 size_t sigalgcnt;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001708 int sigalgs[TLS_MAX_SIGALGCNT];
Matt Caswell0f113f32015-01-22 03:40:55 +00001709} sig_cb_st;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001710
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001711static void get_sigorhash(int *psig, int *phash, const char *str)
1712{
1713 if (strcmp(str, "RSA") == 0) {
1714 *psig = EVP_PKEY_RSA;
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +00001715 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
1716 *psig = EVP_PKEY_RSA_PSS;
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001717 } else if (strcmp(str, "DSA") == 0) {
1718 *psig = EVP_PKEY_DSA;
1719 } else if (strcmp(str, "ECDSA") == 0) {
1720 *psig = EVP_PKEY_EC;
1721 } else {
1722 *phash = OBJ_sn2nid(str);
1723 if (*phash == NID_undef)
1724 *phash = OBJ_ln2nid(str);
1725 }
1726}
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001727/* Maximum length of a signature algorithm string component */
1728#define TLS_MAX_SIGSTRING_LEN 40
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001729
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001730static int sig_cb(const char *elem, int len, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +00001731{
1732 sig_cb_st *sarg = arg;
1733 size_t i;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001734 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001735 int sig_alg = NID_undef, hash_alg = NID_undef;
Kurt Roeckx2747d732015-01-24 14:46:50 +01001736 if (elem == NULL)
1737 return 0;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001738 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
Matt Caswell0f113f32015-01-22 03:40:55 +00001739 return 0;
1740 if (len > (int)(sizeof(etmp) - 1))
1741 return 0;
1742 memcpy(etmp, elem, len);
1743 etmp[len] = 0;
1744 p = strchr(etmp, '+');
Dr. Stephen Henson8a43a422017-01-26 14:40:59 +00001745 /* See if we have a match for TLS 1.3 names */
1746 if (p == NULL) {
1747 const SIGALG_LOOKUP *s;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001748
Dr. Stephen Henson8a43a422017-01-26 14:40:59 +00001749 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1750 i++, s++) {
1751 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
1752 sig_alg = s->sig;
1753 hash_alg = s->hash;
1754 break;
1755 }
1756 }
1757 } else {
1758 *p = 0;
1759 p++;
1760 if (*p == 0)
1761 return 0;
1762 get_sigorhash(&sig_alg, &hash_alg, etmp);
1763 get_sigorhash(&sig_alg, &hash_alg, p);
1764 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001765
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001766 if (sig_alg == NID_undef || hash_alg == NID_undef)
Matt Caswell0f113f32015-01-22 03:40:55 +00001767 return 0;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001768
Matt Caswell0f113f32015-01-22 03:40:55 +00001769 for (i = 0; i < sarg->sigalgcnt; i += 2) {
1770 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
1771 return 0;
1772 }
1773 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
1774 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
1775 return 1;
1776}
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001777
Matt Caswell0f113f32015-01-22 03:40:55 +00001778/*
FdaSilvaYY9d226662016-06-28 22:39:55 +02001779 * Set supported signature algorithms based on a colon separated list of the
Matt Caswell0f113f32015-01-22 03:40:55 +00001780 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1781 */
Dr. Stephen Henson3dbc46d2012-07-03 12:51:14 +00001782int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
Matt Caswell0f113f32015-01-22 03:40:55 +00001783{
1784 sig_cb_st sig;
1785 sig.sigalgcnt = 0;
1786 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
1787 return 0;
1788 if (c == NULL)
1789 return 1;
1790 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
1791}
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001792
Emilia Kaspera230b262016-08-05 19:03:17 +02001793int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
Matt Caswell0f113f32015-01-22 03:40:55 +00001794{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001795 uint16_t *sigalgs, *sptr;
Matt Caswell0f113f32015-01-22 03:40:55 +00001796 size_t i;
Matt Caswell63c1df02016-12-14 16:50:14 +00001797
Matt Caswell0f113f32015-01-22 03:40:55 +00001798 if (salglen & 1)
1799 return 0;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001800 sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs));
Matt Caswell0f113f32015-01-22 03:40:55 +00001801 if (sigalgs == NULL)
1802 return 0;
1803 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
Matt Caswell63c1df02016-12-14 16:50:14 +00001804 size_t j;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001805 const SIGALG_LOOKUP *curr;
Matt Caswell63c1df02016-12-14 16:50:14 +00001806 int md_id = *psig_nids++;
1807 int sig_id = *psig_nids++;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001808
Matt Caswell63c1df02016-12-14 16:50:14 +00001809 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
1810 j++, curr++) {
Matt Caswellfe3066e2017-01-03 10:01:39 +00001811 if (curr->hash == md_id && curr->sig == sig_id) {
Matt Caswell63c1df02016-12-14 16:50:14 +00001812 *sptr++ = curr->sigalg;
1813 break;
1814 }
1815 }
1816
1817 if (j == OSSL_NELEM(sigalg_lookup_tbl))
Matt Caswell0f113f32015-01-22 03:40:55 +00001818 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001819 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001820
Matt Caswell0f113f32015-01-22 03:40:55 +00001821 if (client) {
Rich Salzb548a1f2015-05-01 10:02:07 -04001822 OPENSSL_free(c->client_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +00001823 c->client_sigalgs = sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001824 c->client_sigalgslen = salglen / 2;
Matt Caswell0f113f32015-01-22 03:40:55 +00001825 } else {
Rich Salzb548a1f2015-05-01 10:02:07 -04001826 OPENSSL_free(c->conf_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +00001827 c->conf_sigalgs = sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001828 c->conf_sigalgslen = salglen / 2;
Matt Caswell0f113f32015-01-22 03:40:55 +00001829 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001830
Matt Caswell0f113f32015-01-22 03:40:55 +00001831 return 1;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001832
Matt Caswell0f113f32015-01-22 03:40:55 +00001833 err:
1834 OPENSSL_free(sigalgs);
1835 return 0;
1836}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001837
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001838static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
Matt Caswell0f113f32015-01-22 03:40:55 +00001839{
1840 int sig_nid;
1841 size_t i;
1842 if (default_nid == -1)
1843 return 1;
1844 sig_nid = X509_get_signature_nid(x);
1845 if (default_nid)
1846 return sig_nid == default_nid ? 1 : 0;
1847 for (i = 0; i < c->shared_sigalgslen; i++)
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001848 if (sig_nid == c->shared_sigalgs[i]->sigandhash)
Matt Caswell0f113f32015-01-22 03:40:55 +00001849 return 1;
1850 return 0;
1851}
1852
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001853/* Check to see if a certificate issuer name matches list of CA names */
1854static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
Matt Caswell0f113f32015-01-22 03:40:55 +00001855{
1856 X509_NAME *nm;
1857 int i;
1858 nm = X509_get_issuer_name(x);
1859 for (i = 0; i < sk_X509_NAME_num(names); i++) {
1860 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
1861 return 1;
1862 }
1863 return 0;
1864}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001865
Matt Caswell0f113f32015-01-22 03:40:55 +00001866/*
1867 * Check certificate chain is consistent with TLS extensions and is usable by
1868 * server. This servers two purposes: it allows users to check chains before
1869 * passing them to the server and it allows the server to check chains before
1870 * attempting to use them.
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001871 */
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001872
1873/* Flags which need to be set for a certificate when stict mode not set */
1874
Matt Caswelle481f9b2015-05-15 10:49:56 +01001875#define CERT_PKEY_VALID_FLAGS \
Matt Caswell0f113f32015-01-22 03:40:55 +00001876 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001877/* Strict mode flags */
Matt Caswelle481f9b2015-05-15 10:49:56 +01001878#define CERT_PKEY_STRICT_FLAGS \
Matt Caswell0f113f32015-01-22 03:40:55 +00001879 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
1880 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001881
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001882int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
Matt Caswell0f113f32015-01-22 03:40:55 +00001883 int idx)
1884{
1885 int i;
1886 int rv = 0;
1887 int check_flags = 0, strict_mode;
1888 CERT_PKEY *cpk = NULL;
1889 CERT *c = s->cert;
Dr. Stephen Hensonf7d53482015-07-14 23:19:11 +01001890 uint32_t *pvalid;
Matt Caswell0f113f32015-01-22 03:40:55 +00001891 unsigned int suiteb_flags = tls1_suiteb(s);
1892 /* idx == -1 means checking server chains */
1893 if (idx != -1) {
1894 /* idx == -2 means checking client certificate chains */
1895 if (idx == -2) {
1896 cpk = c->key;
Matt Caswell348240c2016-10-19 15:11:24 +01001897 idx = (int)(cpk - c->pkeys);
Matt Caswell0f113f32015-01-22 03:40:55 +00001898 } else
1899 cpk = c->pkeys + idx;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001900 pvalid = s->s3->tmp.valid_flags + idx;
Matt Caswell0f113f32015-01-22 03:40:55 +00001901 x = cpk->x509;
1902 pk = cpk->privatekey;
1903 chain = cpk->chain;
1904 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
1905 /* If no cert or key, forget it */
1906 if (!x || !pk)
1907 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001908 } else {
1909 if (!x || !pk)
Matt Caswelld813f9e2015-03-11 17:01:38 +00001910 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001911 idx = ssl_cert_type(x, pk);
1912 if (idx == -1)
Matt Caswelld813f9e2015-03-11 17:01:38 +00001913 return 0;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001914 pvalid = s->s3->tmp.valid_flags + idx;
1915
Matt Caswell0f113f32015-01-22 03:40:55 +00001916 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
1917 check_flags = CERT_PKEY_STRICT_FLAGS;
1918 else
1919 check_flags = CERT_PKEY_VALID_FLAGS;
1920 strict_mode = 1;
1921 }
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001922
Matt Caswell0f113f32015-01-22 03:40:55 +00001923 if (suiteb_flags) {
1924 int ok;
1925 if (check_flags)
1926 check_flags |= CERT_PKEY_SUITEB;
1927 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
1928 if (ok == X509_V_OK)
1929 rv |= CERT_PKEY_SUITEB;
1930 else if (!check_flags)
1931 goto end;
1932 }
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001933
Matt Caswell0f113f32015-01-22 03:40:55 +00001934 /*
1935 * Check all signature algorithms are consistent with signature
1936 * algorithms extension if TLS 1.2 or later and strict mode.
1937 */
1938 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
1939 int default_nid;
Matt Caswell536199e2016-12-14 16:37:48 +00001940 int rsign = 0;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001941 if (s->s3->tmp.peer_sigalgs)
Matt Caswell0f113f32015-01-22 03:40:55 +00001942 default_nid = 0;
1943 /* If no sigalgs extension use defaults from RFC5246 */
1944 else {
1945 switch (idx) {
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001946 case SSL_PKEY_RSA:
Matt Caswell536199e2016-12-14 16:37:48 +00001947 rsign = EVP_PKEY_RSA;
Matt Caswell0f113f32015-01-22 03:40:55 +00001948 default_nid = NID_sha1WithRSAEncryption;
1949 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001950
Matt Caswell0f113f32015-01-22 03:40:55 +00001951 case SSL_PKEY_DSA_SIGN:
Matt Caswell536199e2016-12-14 16:37:48 +00001952 rsign = EVP_PKEY_DSA;
Matt Caswell0f113f32015-01-22 03:40:55 +00001953 default_nid = NID_dsaWithSHA1;
1954 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001955
Matt Caswell0f113f32015-01-22 03:40:55 +00001956 case SSL_PKEY_ECC:
Matt Caswell536199e2016-12-14 16:37:48 +00001957 rsign = EVP_PKEY_EC;
Matt Caswell0f113f32015-01-22 03:40:55 +00001958 default_nid = NID_ecdsa_with_SHA1;
1959 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001960
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001961 case SSL_PKEY_GOST01:
Matt Caswell536199e2016-12-14 16:37:48 +00001962 rsign = NID_id_GostR3410_2001;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001963 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
1964 break;
1965
1966 case SSL_PKEY_GOST12_256:
Matt Caswell536199e2016-12-14 16:37:48 +00001967 rsign = NID_id_GostR3410_2012_256;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001968 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
1969 break;
1970
1971 case SSL_PKEY_GOST12_512:
Matt Caswell536199e2016-12-14 16:37:48 +00001972 rsign = NID_id_GostR3410_2012_512;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001973 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
1974 break;
1975
Matt Caswell0f113f32015-01-22 03:40:55 +00001976 default:
1977 default_nid = -1;
1978 break;
1979 }
1980 }
1981 /*
1982 * If peer sent no signature algorithms extension and we have set
1983 * preferred signature algorithms check we support sha1.
1984 */
1985 if (default_nid > 0 && c->conf_sigalgs) {
1986 size_t j;
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001987 const uint16_t *p = c->conf_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +00001988 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001989 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
1990
1991 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
Matt Caswell0f113f32015-01-22 03:40:55 +00001992 break;
1993 }
1994 if (j == c->conf_sigalgslen) {
1995 if (check_flags)
1996 goto skip_sigs;
1997 else
1998 goto end;
1999 }
2000 }
2001 /* Check signature algorithm of each cert in chain */
2002 if (!tls1_check_sig_alg(c, x, default_nid)) {
2003 if (!check_flags)
2004 goto end;
2005 } else
2006 rv |= CERT_PKEY_EE_SIGNATURE;
2007 rv |= CERT_PKEY_CA_SIGNATURE;
2008 for (i = 0; i < sk_X509_num(chain); i++) {
2009 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
2010 if (check_flags) {
2011 rv &= ~CERT_PKEY_CA_SIGNATURE;
2012 break;
2013 } else
2014 goto end;
2015 }
2016 }
2017 }
2018 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
2019 else if (check_flags)
2020 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
2021 skip_sigs:
2022 /* Check cert parameters are consistent */
2023 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
2024 rv |= CERT_PKEY_EE_PARAM;
2025 else if (!check_flags)
2026 goto end;
2027 if (!s->server)
2028 rv |= CERT_PKEY_CA_PARAM;
2029 /* In strict mode check rest of chain too */
2030 else if (strict_mode) {
2031 rv |= CERT_PKEY_CA_PARAM;
2032 for (i = 0; i < sk_X509_num(chain); i++) {
2033 X509 *ca = sk_X509_value(chain, i);
2034 if (!tls1_check_cert_param(s, ca, 0)) {
2035 if (check_flags) {
2036 rv &= ~CERT_PKEY_CA_PARAM;
2037 break;
2038 } else
2039 goto end;
2040 }
2041 }
2042 }
2043 if (!s->server && strict_mode) {
2044 STACK_OF(X509_NAME) *ca_dn;
2045 int check_type = 0;
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +00002046 switch (EVP_PKEY_id(pk)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002047 case EVP_PKEY_RSA:
2048 check_type = TLS_CT_RSA_SIGN;
2049 break;
2050 case EVP_PKEY_DSA:
2051 check_type = TLS_CT_DSS_SIGN;
2052 break;
2053 case EVP_PKEY_EC:
2054 check_type = TLS_CT_ECDSA_SIGN;
2055 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00002056 }
2057 if (check_type) {
2058 const unsigned char *ctypes;
2059 int ctypelen;
2060 if (c->ctypes) {
2061 ctypes = c->ctypes;
2062 ctypelen = (int)c->ctype_num;
2063 } else {
2064 ctypes = (unsigned char *)s->s3->tmp.ctype;
2065 ctypelen = s->s3->tmp.ctype_num;
2066 }
2067 for (i = 0; i < ctypelen; i++) {
2068 if (ctypes[i] == check_type) {
2069 rv |= CERT_PKEY_CERT_TYPE;
2070 break;
2071 }
2072 }
2073 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2074 goto end;
2075 } else
2076 rv |= CERT_PKEY_CERT_TYPE;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002077
Matt Caswell0f113f32015-01-22 03:40:55 +00002078 ca_dn = s->s3->tmp.ca_names;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002079
Matt Caswell0f113f32015-01-22 03:40:55 +00002080 if (!sk_X509_NAME_num(ca_dn))
2081 rv |= CERT_PKEY_ISSUER_NAME;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002082
Matt Caswell0f113f32015-01-22 03:40:55 +00002083 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2084 if (ssl_check_ca_name(ca_dn, x))
2085 rv |= CERT_PKEY_ISSUER_NAME;
2086 }
2087 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2088 for (i = 0; i < sk_X509_num(chain); i++) {
2089 X509 *xtmp = sk_X509_value(chain, i);
2090 if (ssl_check_ca_name(ca_dn, xtmp)) {
2091 rv |= CERT_PKEY_ISSUER_NAME;
2092 break;
2093 }
2094 }
2095 }
2096 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2097 goto end;
2098 } else
2099 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002100
Matt Caswell0f113f32015-01-22 03:40:55 +00002101 if (!check_flags || (rv & check_flags) == check_flags)
2102 rv |= CERT_PKEY_VALID;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002103
Matt Caswell0f113f32015-01-22 03:40:55 +00002104 end:
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002105
Matt Caswell0f113f32015-01-22 03:40:55 +00002106 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01002107 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
Matt Caswell0f113f32015-01-22 03:40:55 +00002108 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01002109 else if (s->s3->tmp.md[idx] != NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00002110 rv |= CERT_PKEY_SIGN;
2111 } else
2112 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002113
Matt Caswell0f113f32015-01-22 03:40:55 +00002114 /*
2115 * When checking a CERT_PKEY structure all flags are irrelevant if the
2116 * chain is invalid.
2117 */
2118 if (!check_flags) {
2119 if (rv & CERT_PKEY_VALID)
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01002120 *pvalid = rv;
Matt Caswell0f113f32015-01-22 03:40:55 +00002121 else {
2122 /* Preserve explicit sign flag, clear rest */
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01002123 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
Matt Caswell0f113f32015-01-22 03:40:55 +00002124 return 0;
2125 }
2126 }
2127 return rv;
2128}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002129
2130/* Set validity of certificates in an SSL structure */
2131void tls1_set_cert_validity(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00002132{
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00002133 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
Matt Caswell17dd65e2015-03-24 15:10:15 +00002134 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
Matt Caswell17dd65e2015-03-24 15:10:15 +00002135 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00002136 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2137 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2138 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
Matt Caswell0f113f32015-01-22 03:40:55 +00002139}
2140
Dr. Stephen Henson18d71582012-06-29 14:24:42 +00002141/* User level utiity function to check a chain is suitable */
2142int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
Matt Caswell0f113f32015-01-22 03:40:55 +00002143{
2144 return tls1_check_chain(s, x, pk, chain, -1);
2145}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002146
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002147#ifndef OPENSSL_NO_DH
2148DH *ssl_get_auto_dh(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00002149{
2150 int dh_secbits = 80;
2151 if (s->cert->dh_tmp_auto == 2)
2152 return DH_get_1024_160();
Dr. Stephen Hensonadc55062015-06-28 17:01:52 +01002153 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002154 if (s->s3->tmp.new_cipher->strength_bits == 256)
2155 dh_secbits = 128;
2156 else
2157 dh_secbits = 80;
2158 } else {
Dr. Stephen Hensona497cf22017-02-14 00:35:26 +00002159 if (s->s3->tmp.cert == NULL)
Dr. Stephen Hensonf365a3e2017-02-13 16:32:06 +00002160 return NULL;
Dr. Stephen Hensona497cf22017-02-14 00:35:26 +00002161 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
Matt Caswell0f113f32015-01-22 03:40:55 +00002162 }
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002163
Matt Caswell0f113f32015-01-22 03:40:55 +00002164 if (dh_secbits >= 128) {
2165 DH *dhp = DH_new();
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002166 BIGNUM *p, *g;
Matt Caswella71edf32015-10-30 10:05:53 +00002167 if (dhp == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00002168 return NULL;
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002169 g = BN_new();
2170 if (g != NULL)
2171 BN_set_word(g, 2);
Matt Caswell0f113f32015-01-22 03:40:55 +00002172 if (dh_secbits >= 192)
Rich Salz9021a5d2016-04-18 07:43:54 -04002173 p = BN_get_rfc3526_prime_8192(NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +00002174 else
Rich Salz9021a5d2016-04-18 07:43:54 -04002175 p = BN_get_rfc3526_prime_3072(NULL);
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002176 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002177 DH_free(dhp);
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002178 BN_free(p);
2179 BN_free(g);
Matt Caswell0f113f32015-01-22 03:40:55 +00002180 return NULL;
2181 }
2182 return dhp;
2183 }
2184 if (dh_secbits >= 112)
2185 return DH_get_2048_224();
2186 return DH_get_1024_160();
2187}
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002188#endif
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002189
2190static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00002191{
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002192 int secbits = -1;
Dr. Stephen Henson8382fd32015-12-20 00:32:36 +00002193 EVP_PKEY *pkey = X509_get0_pubkey(x);
Matt Caswell0f113f32015-01-22 03:40:55 +00002194 if (pkey) {
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002195 /*
2196 * If no parameters this will return -1 and fail using the default
2197 * security callback for any non-zero security level. This will
2198 * reject keys which omit parameters but this only affects DSA and
2199 * omission of parameters is never (?) done in practice.
2200 */
Matt Caswell0f113f32015-01-22 03:40:55 +00002201 secbits = EVP_PKEY_security_bits(pkey);
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002202 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002203 if (s)
2204 return ssl_security(s, op, secbits, 0, x);
2205 else
2206 return ssl_ctx_security(ctx, op, secbits, 0, x);
2207}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002208
2209static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00002210{
2211 /* Lookup signature algorithm digest */
2212 int secbits = -1, md_nid = NID_undef, sig_nid;
Dr. Stephen Henson221c7b52016-02-11 15:25:11 +00002213 /* Don't check signature if self signed */
2214 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2215 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +00002216 sig_nid = X509_get_signature_nid(x);
2217 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
2218 const EVP_MD *md;
2219 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
2220 secbits = EVP_MD_size(md) * 4;
2221 }
2222 if (s)
2223 return ssl_security(s, op, secbits, md_nid, x);
2224 else
2225 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
2226}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002227
2228int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
Matt Caswell0f113f32015-01-22 03:40:55 +00002229{
2230 if (vfy)
2231 vfy = SSL_SECOP_PEER;
2232 if (is_ee) {
2233 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2234 return SSL_R_EE_KEY_TOO_SMALL;
2235 } else {
2236 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2237 return SSL_R_CA_KEY_TOO_SMALL;
2238 }
2239 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2240 return SSL_R_CA_MD_TOO_WEAK;
2241 return 1;
2242}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002243
Matt Caswell0f113f32015-01-22 03:40:55 +00002244/*
2245 * Check security of a chain, if sk includes the end entity certificate then
2246 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
2247 * one to the peer. Return values: 1 if ok otherwise error code to use
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002248 */
2249
2250int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
Matt Caswell0f113f32015-01-22 03:40:55 +00002251{
2252 int rv, start_idx, i;
2253 if (x == NULL) {
2254 x = sk_X509_value(sk, 0);
2255 start_idx = 1;
2256 } else
2257 start_idx = 0;
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002258
Matt Caswell0f113f32015-01-22 03:40:55 +00002259 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2260 if (rv != 1)
2261 return rv;
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002262
Matt Caswell0f113f32015-01-22 03:40:55 +00002263 for (i = start_idx; i < sk_X509_num(sk); i++) {
2264 x = sk_X509_value(sk, i);
2265 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2266 if (rv != 1)
2267 return rv;
2268 }
2269 return 1;
2270}
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002271
2272/*
2273 * Choose an appropriate signature algorithm based on available certificates
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002274 * Sets chosen certificate and signature algorithm.
2275 *
2276 * For servers if we fail to find a required certificate it is a fatal error
2277 * and an appropriate error code is set and the TLS alert set in *al.
2278 *
2279 * For clients al is set to NULL. If a certificate is not suitable it is not
2280 * a fatal error: we will either try another certificate or not present one
2281 * to the server. In this case no error is set.
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002282 */
Dr. Stephen Henson4a419f62017-02-13 15:50:43 +00002283int tls_choose_sigalg(SSL *s, int *al)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002284{
Dr. Stephen Henson7b3a4d62017-02-16 01:29:14 +00002285 int idx = -1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002286 const SIGALG_LOOKUP *lu = NULL;
2287
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002288 s->s3->tmp.cert = NULL;
2289 s->s3->tmp.sigalg = NULL;
2290
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002291 if (SSL_IS_TLS13(s)) {
2292 size_t i;
Richard Levitte21f198e2017-02-03 13:56:54 +01002293#ifndef OPENSSL_NO_EC
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002294 int curve = -1;
Richard Levitte21f198e2017-02-03 13:56:54 +01002295#endif
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002296
2297 /* Look for a certificate matching shared sigaglgs */
2298 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002299 lu = s->cert->shared_sigalgs[i];
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002300
Dr. Stephen Henson5a8916d2017-02-17 16:08:19 +00002301 /* Skip DSA and RSA if not PSS */
2302 if (lu->sig == EVP_PKEY_DSA || lu->sig == EVP_PKEY_RSA)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002303 continue;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002304 if (ssl_md(lu->hash_idx) == NULL)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002305 continue;
2306 idx = lu->sig_idx;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002307 if (!ssl_has_cert(s, idx))
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002308 continue;
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002309 if (lu->sig == EVP_PKEY_EC) {
Richard Levitte21f198e2017-02-03 13:56:54 +01002310#ifndef OPENSSL_NO_EC
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002311 if (curve == -1) {
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002312 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey);
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002313
2314 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2315 }
Dr. Stephen Hensona34a9df2017-02-14 14:27:15 +00002316 if (lu->curve != NID_undef && curve != lu->curve)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002317 continue;
Richard Levitte21f198e2017-02-03 13:56:54 +01002318#else
2319 continue;
2320#endif
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002321 }
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002322 break;
2323 }
2324 if (i == s->cert->shared_sigalgslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002325 if (al == NULL)
2326 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002327 *al = SSL_AD_HANDSHAKE_FAILURE;
2328 SSLerr(SSL_F_TLS_CHOOSE_SIGALG,
2329 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2330 return 0;
2331 }
2332 } else {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002333 if (s->server) {
2334 /* Find index corresponding to ciphersuite */
2335 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2336 /* If no certificate for ciphersuite return */
2337 if (idx == -1)
2338 return 1;
2339 if (idx == SSL_PKEY_GOST_EC) {
2340 /* Work out which GOST certificate is avaiable */
2341 if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
2342 idx = SSL_PKEY_GOST12_512;
2343 } else if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
2344 idx = SSL_PKEY_GOST12_256;
2345 } else if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
2346 idx = SSL_PKEY_GOST01;
2347 } else {
2348 if (al == NULL)
2349 return 1;
2350 *al = SSL_AD_INTERNAL_ERROR;
2351 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2352 return 0;
2353 }
2354 } else if (!ssl_has_cert(s, idx)) {
2355 if (al == NULL)
2356 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002357 *al = SSL_AD_INTERNAL_ERROR;
2358 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2359 return 0;
2360 }
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002361 } else {
2362 /* Find index for client certificate */
2363 idx = s->cert->key - s->cert->pkeys;
2364 if (!ssl_has_cert(s, idx))
2365 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002366 }
2367
2368 if (SSL_USE_SIGALGS(s)) {
2369 if (s->s3->tmp.peer_sigalgs != NULL) {
2370 size_t i;
2371
2372 /*
2373 * Find highest preference signature algorithm matching
2374 * cert type
2375 */
2376 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2377 lu = s->cert->shared_sigalgs[i];
2378 if (lu->sig_idx == idx)
2379 break;
2380 if (idx == SSL_PKEY_RSA && lu->sig == EVP_PKEY_RSA_PSS)
2381 break;
2382 }
2383 if (i == s->cert->shared_sigalgslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002384 if (al == NULL)
2385 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002386 *al = SSL_AD_INTERNAL_ERROR;
2387 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2388 return 0;
2389 }
2390 } else {
2391 /*
2392 * If we have no sigalg use defaults
2393 */
2394 const uint16_t *sent_sigs;
2395 size_t sent_sigslen, i;
2396
2397 if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002398 if (al == NULL)
2399 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002400 *al = SSL_AD_INTERNAL_ERROR;
2401 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2402 return 0;
2403 }
2404
2405 /* Check signature matches a type we sent */
2406 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2407 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2408 if (lu->sigalg == *sent_sigs)
2409 break;
2410 }
2411 if (i == sent_sigslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002412 if (al == NULL)
2413 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002414 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
2415 *al = SSL_AD_HANDSHAKE_FAILURE;
2416 return 0;
2417 }
2418 }
2419 } else {
2420 if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002421 if (al == NULL)
2422 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002423 *al = SSL_AD_INTERNAL_ERROR;
2424 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2425 return 0;
2426 }
2427 }
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002428 }
Dr. Stephen Henson7b3a4d62017-02-16 01:29:14 +00002429 if (idx == -1) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002430 if (al != NULL) {
2431 *al = SSL_AD_INTERNAL_ERROR;
2432 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2433 }
Dr. Stephen Henson7b3a4d62017-02-16 01:29:14 +00002434 return 0;
2435 }
Dr. Stephen Hensona497cf22017-02-14 00:35:26 +00002436 s->s3->tmp.cert = &s->cert->pkeys[idx];
Dr. Stephen Henson59088e42017-02-15 15:28:56 +00002437 s->cert->key = s->s3->tmp.cert;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002438 s->s3->tmp.sigalg = lu;
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002439 return 1;
2440}