blob: 9582e21eea42dd436159298b2934360c8c908852 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
Pauli677963e2017-08-18 13:52:46 +10002 * Copyright 1995-2017 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>
Pauli677963e2017-08-18 13:52:46 +100020#include "internal/nelem.h"
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000021#include "ssl_locl.h"
Rich Salz3c272082016-03-18 14:30:20 -040022#include <openssl/ct.h>
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000023
Matt Caswell0f113f32015-01-22 03:40:55 +000024SSL3_ENC_METHOD const TLSv1_enc_data = {
25 tls1_enc,
26 tls1_mac,
27 tls1_setup_key_block,
28 tls1_generate_master_secret,
29 tls1_change_cipher_state,
30 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000031 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
32 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
33 tls1_alert_code,
34 tls1_export_keying_material,
35 0,
Matt Caswella29fa982016-09-29 22:40:15 +010036 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010037 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000038 ssl3_handshake_write
39};
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +000040
Matt Caswell0f113f32015-01-22 03:40:55 +000041SSL3_ENC_METHOD const TLSv1_1_enc_data = {
42 tls1_enc,
43 tls1_mac,
44 tls1_setup_key_block,
45 tls1_generate_master_secret,
46 tls1_change_cipher_state,
47 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000048 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
49 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
50 tls1_alert_code,
51 tls1_export_keying_material,
52 SSL_ENC_FLAG_EXPLICIT_IV,
Matt Caswella29fa982016-09-29 22:40:15 +010053 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010054 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000055 ssl3_handshake_write
56};
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +000057
Matt Caswell0f113f32015-01-22 03:40:55 +000058SSL3_ENC_METHOD const TLSv1_2_enc_data = {
59 tls1_enc,
60 tls1_mac,
61 tls1_setup_key_block,
62 tls1_generate_master_secret,
63 tls1_change_cipher_state,
64 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000065 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
66 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
67 tls1_alert_code,
68 tls1_export_keying_material,
69 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
70 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
Matt Caswella29fa982016-09-29 22:40:15 +010071 ssl3_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010072 tls_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000073 ssl3_handshake_write
74};
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000075
Matt Caswell582a17d2016-10-21 17:39:33 +010076SSL3_ENC_METHOD const TLSv1_3_enc_data = {
Matt Caswellbebc0c72016-11-17 18:00:17 +000077 tls13_enc,
Matt Caswell582a17d2016-10-21 17:39:33 +010078 tls1_mac,
Matt Caswell92760c22016-11-09 14:06:12 +000079 tls13_setup_key_block,
80 tls13_generate_master_secret,
81 tls13_change_cipher_state,
82 tls13_final_finish_mac,
Matt Caswell582a17d2016-10-21 17:39:33 +010083 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
84 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
Matt Caswell04904312016-12-30 11:26:39 +000085 tls13_alert_code,
Matt Caswell0ca8d1e2017-06-27 14:57:15 +010086 tls13_export_keying_material,
Matt Caswellbebc0c72016-11-17 18:00:17 +000087 SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
Matt Caswell582a17d2016-10-21 17:39:33 +010088 ssl3_set_handshake_header,
89 tls_close_construct_packet,
90 ssl3_handshake_write
91};
92
Dr. Stephen Hensonf3b656b2005-08-05 23:56:11 +000093long tls1_default_timeout(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000094{
95 /*
96 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
97 * http, the cache would over fill
98 */
99 return (60 * 60 * 2);
100}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000101
Ulf Möller6b691a51999-04-19 21:31:43 +0000102int tls1_new(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000103{
104 if (!ssl3_new(s))
Matt Caswellb77f3ed2017-05-22 12:33:42 +0100105 return 0;
106 if (!s->method->ssl_clear(s))
107 return 0;
108
109 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000110}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000111
Ulf Möller6b691a51999-04-19 21:31:43 +0000112void tls1_free(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000113{
Rich Salzaff8c122016-12-08 14:18:40 -0500114 OPENSSL_free(s->ext.session_ticket);
Matt Caswell0f113f32015-01-22 03:40:55 +0000115 ssl3_free(s);
116}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000117
Matt Caswellb77f3ed2017-05-22 12:33:42 +0100118int tls1_clear(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000119{
Matt Caswellb77f3ed2017-05-22 12:33:42 +0100120 if (!ssl3_clear(s))
121 return 0;
122
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500123 if (s->method->version == TLS_ANY_VERSION)
124 s->version = TLS_MAX_VERSION;
125 else
126 s->version = s->method->version;
Matt Caswellb77f3ed2017-05-22 12:33:42 +0100127
128 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000129}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000130
Dr. Stephen Henson525de5d2007-08-12 23:59:05 +0000131#ifndef OPENSSL_NO_EC
Dr. Stephen Hensoneda37662011-05-30 17:58:13 +0000132
Dr. Stephen Henson2dc1aee2016-02-13 15:27:43 +0000133/*
134 * Table of curve information.
Rich Salzddb4c042016-01-03 13:24:32 -0500135 * Do not delete entries or reorder this array! It is used as a lookup
Dr. Stephen Henson2dc1aee2016-02-13 15:27:43 +0000136 * table: the index of each entry is one less than the TLS curve id.
137 */
Dr. Stephen Henson0e464d92017-09-22 23:47:54 +0100138static const TLS_GROUP_INFO nid_list[] = {
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
140 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
141 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
142 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
143 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
144 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
145 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
146 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
147 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
148 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
149 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
150 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
151 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
152 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
153 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
154 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
155 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
156 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
157 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
158 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
159 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
160 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
161 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
162 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
163 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
164 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
165 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
166 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
Dr. Stephen Hensond2916a52017-06-20 16:32:44 +0100167 {EVP_PKEY_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
Matt Caswell0f113f32015-01-22 03:40:55 +0000168};
Dr. Stephen Hensoneda37662011-05-30 17:58:13 +0000169
Matt Caswell0f113f32015-01-22 03:40:55 +0000170static const unsigned char ecformats_default[] = {
171 TLSEXT_ECPOINTFORMAT_uncompressed,
172 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
173 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
174};
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000175
Kurt Roeckxfe6ef242015-12-04 22:30:36 +0100176/* The default curves */
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100177static const uint16_t eccurves_default[] = {
178 29, /* X25519 (29) */
179 23, /* secp256r1 (23) */
180 25, /* secp521r1 (25) */
181 24, /* secp384r1 (24) */
Emilia Kasperde57d232015-05-20 15:47:51 +0200182};
183
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100184static const uint16_t suiteb_curves[] = {
185 TLSEXT_curve_P_256,
186 TLSEXT_curve_P_384
Matt Caswell0f113f32015-01-22 03:40:55 +0000187};
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000188
Dr. Stephen Hensonf48d8262017-09-26 15:41:34 +0100189const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
Matt Caswell0f113f32015-01-22 03:40:55 +0000190{
191 /* ECC curves from RFC 4492 and RFC 7027 */
Dr. Stephen Hensonf48d8262017-09-26 15:41:34 +0100192 if (group_id < 1 || group_id > OSSL_NELEM(nid_list))
Dr. Stephen Henson43b95d72017-09-23 00:15:34 +0100193 return NULL;
Dr. Stephen Hensonf48d8262017-09-26 15:41:34 +0100194 return &nid_list[group_id - 1];
Matt Caswell0f113f32015-01-22 03:40:55 +0000195}
Dr. Stephen Henson525de5d2007-08-12 23:59:05 +0000196
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100197static uint16_t tls1_nid2group_id(int nid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000198{
Dr. Stephen Henson2fa2d152016-02-13 15:28:25 +0000199 size_t i;
200 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
201 if (nid_list[i].nid == nid)
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100202 return i + 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000203 }
Dr. Stephen Henson2fa2d152016-02-13 15:28:25 +0000204 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000205}
206
Emilia Kasper740580c2014-12-01 16:55:55 +0100207/*
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100208 * Set *pgroups to the supported groups list and *pgroupslen to
209 * the number of groups supported.
Dr. Stephen Hensonfd2b65c2012-04-04 14:41:01 +0000210 */
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100211void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
212 size_t *pgroupslen)
Matt Caswell0f113f32015-01-22 03:40:55 +0000213{
Rich Salz3e373512016-09-19 13:09:58 -0400214
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100215 /* For Suite B mode only include P-256, P-384 */
216 switch (tls1_suiteb(s)) {
217 case SSL_CERT_FLAG_SUITEB_128_LOS:
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100218 *pgroups = suiteb_curves;
219 *pgroupslen = OSSL_NELEM(suiteb_curves);
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100220 break;
Emilia Kasper740580c2014-12-01 16:55:55 +0100221
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100222 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100223 *pgroups = suiteb_curves;
224 *pgroupslen = 1;
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100225 break;
226
227 case SSL_CERT_FLAG_SUITEB_192_LOS:
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100228 *pgroups = suiteb_curves + 1;
229 *pgroupslen = 1;
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100230 break;
231
232 default:
233 if (s->ext.supportedgroups == NULL) {
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100234 *pgroups = eccurves_default;
235 *pgroupslen = OSSL_NELEM(eccurves_default);
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100236 } else {
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100237 *pgroups = s->ext.supportedgroups;
238 *pgroupslen = s->ext.supportedgroups_len;
Dr. Stephen Henson34e52922017-09-24 03:26:26 +0100239 }
240 break;
241 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000242}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +0000243
244/* See if curve is allowed by security callback */
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100245int tls_curve_allowed(SSL *s, uint16_t curve, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +0000246{
Dr. Stephen Henson5ce5f782017-09-24 21:58:58 +0100247 const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve);
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100248 unsigned char ctmp[2];
Dr. Stephen Henson5ce5f782017-09-24 21:58:58 +0100249
250 if (cinfo == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000251 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000252# ifdef OPENSSL_NO_EC2M
253 if (cinfo->flags & TLS_CURVE_CHAR2)
254 return 0;
255# endif
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100256 ctmp[0] = curve >> 8;
257 ctmp[1] = curve & 0xff;
258 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp);
Matt Caswell0f113f32015-01-22 03:40:55 +0000259}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +0000260
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100261/* Return 1 if "id" is in "list" */
262static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
263{
264 size_t i;
265 for (i = 0; i < listlen; i++)
266 if (list[i] == id)
267 return 1;
268 return 0;
269}
270
Dr. Stephen Hensond18b7162012-07-24 13:47:40 +0000271/* Check a curve is one of our preferences */
272int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000273{
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100274 const uint16_t *curves;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100275 size_t num_curves;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100276 uint16_t curve_id;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100277
Matt Caswell0f113f32015-01-22 03:40:55 +0000278 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
279 return 0;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100280 curve_id = (p[1] << 8) | p[2];
Matt Caswell0f113f32015-01-22 03:40:55 +0000281 /* Check curve matches Suite B preferences */
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100282 if (tls1_suiteb(s)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000283 unsigned long cid = s->s3->tmp.new_cipher->id;
Matt Caswell0f113f32015-01-22 03:40:55 +0000284 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100285 if (curve_id != TLSEXT_curve_P_256)
Matt Caswell0f113f32015-01-22 03:40:55 +0000286 return 0;
287 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100288 if (curve_id != TLSEXT_curve_P_384)
Matt Caswell0f113f32015-01-22 03:40:55 +0000289 return 0;
290 } else /* Should never happen */
291 return 0;
292 }
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100293 tls1_get_supported_groups(s, &curves, &num_curves);
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100294 if (!tls1_in_list(curve_id, curves, num_curves))
295 return 0;
296 return tls_curve_allowed(s, curve_id, SSL_SECOP_CURVE_CHECK);
Matt Caswell0f113f32015-01-22 03:40:55 +0000297}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000298
Tim Hudson1d97c842014-12-28 12:48:40 +1000299/*-
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100300 * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100301 * if there is no match.
302 * For nmatch == -1, return number of matches
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100303 * For nmatch == -2, return the id of the group to use for
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100304 * a tmp key, or 0 if there is no match.
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000305 */
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100306uint16_t tls1_shared_group(SSL *s, int nmatch)
Matt Caswell0f113f32015-01-22 03:40:55 +0000307{
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100308 const uint16_t *pref, *supp;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100309 size_t num_pref, num_supp, i;
Matt Caswell0f113f32015-01-22 03:40:55 +0000310 int k;
Rich Salz3e373512016-09-19 13:09:58 -0400311
Matt Caswell0f113f32015-01-22 03:40:55 +0000312 /* Can't do anything on client side */
313 if (s->server == 0)
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100314 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000315 if (nmatch == -2) {
316 if (tls1_suiteb(s)) {
317 /*
318 * For Suite B ciphersuite determines curve: we already know
319 * these are acceptable due to previous checks.
320 */
321 unsigned long cid = s->s3->tmp.new_cipher->id;
Rich Salz3e373512016-09-19 13:09:58 -0400322
Matt Caswell0f113f32015-01-22 03:40:55 +0000323 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100324 return TLSEXT_curve_P_256;
Matt Caswell0f113f32015-01-22 03:40:55 +0000325 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100326 return TLSEXT_curve_P_384;
Matt Caswell0f113f32015-01-22 03:40:55 +0000327 /* Should never happen */
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100328 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000329 }
330 /* If not Suite B just return first preference shared curve */
331 nmatch = 0;
332 }
333 /*
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100334 * If server preference set, our groups are the preference order
335 * otherwise peer decides.
Matt Caswell0f113f32015-01-22 03:40:55 +0000336 */
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100337 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
338 tls1_get_supported_groups(s, &pref, &num_pref);
339 tls1_get_peer_groups(s, &supp, &num_supp);
340 } else {
341 tls1_get_peer_groups(s, &pref, &num_pref);
342 tls1_get_supported_groups(s, &supp, &num_supp);
343 }
Kurt Roeckx3c065132015-05-30 19:20:12 +0200344
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100345 for (k = 0, i = 0; i < num_pref; i++) {
346 uint16_t id = pref[i];
Rich Salz3e373512016-09-19 13:09:58 -0400347
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100348 if (!tls1_in_list(id, supp, num_supp)
349 || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED))
Matt Caswell0f113f32015-01-22 03:40:55 +0000350 continue;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100351 if (nmatch == k)
352 return id;
353 k++;
Matt Caswell0f113f32015-01-22 03:40:55 +0000354 }
355 if (nmatch == -1)
356 return k;
357 /* Out of range (nmatch > k). */
Dr. Stephen Henson88411542017-09-22 23:43:03 +0100358 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000359}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000360
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100361int tls1_set_groups(uint16_t **pext, size_t *pextlen,
Matt Caswellde4d7642016-11-09 14:51:06 +0000362 int *groups, size_t ngroups)
Matt Caswell0f113f32015-01-22 03:40:55 +0000363{
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100364 uint16_t *glist;
Matt Caswell0f113f32015-01-22 03:40:55 +0000365 size_t i;
366 /*
Matt Caswellde4d7642016-11-09 14:51:06 +0000367 * Bitmap of groups included to detect duplicates: only works while group
Matt Caswell0f113f32015-01-22 03:40:55 +0000368 * ids < 32
369 */
370 unsigned long dup_list = 0;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100371 glist = OPENSSL_malloc(ngroups * sizeof(*glist));
Matt Caswellde4d7642016-11-09 14:51:06 +0000372 if (glist == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000373 return 0;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100374 for (i = 0; i < ngroups; i++) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000375 unsigned long idmask;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100376 uint16_t id;
Matt Caswellde4d7642016-11-09 14:51:06 +0000377 /* TODO(TLS1.3): Convert for DH groups */
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100378 id = tls1_nid2group_id(groups[i]);
Matt Caswell0f113f32015-01-22 03:40:55 +0000379 idmask = 1L << id;
380 if (!id || (dup_list & idmask)) {
Matt Caswellde4d7642016-11-09 14:51:06 +0000381 OPENSSL_free(glist);
Matt Caswell0f113f32015-01-22 03:40:55 +0000382 return 0;
383 }
384 dup_list |= idmask;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100385 glist[i] = id;
Matt Caswell0f113f32015-01-22 03:40:55 +0000386 }
Rich Salzb548a1f2015-05-01 10:02:07 -0400387 OPENSSL_free(*pext);
Matt Caswellde4d7642016-11-09 14:51:06 +0000388 *pext = glist;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100389 *pextlen = ngroups;
Matt Caswell0f113f32015-01-22 03:40:55 +0000390 return 1;
391}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000392
Matt Caswell0f113f32015-01-22 03:40:55 +0000393# define MAX_CURVELIST 28
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000394
Matt Caswell0f113f32015-01-22 03:40:55 +0000395typedef struct {
396 size_t nidcnt;
397 int nid_arr[MAX_CURVELIST];
398} nid_cb_st;
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000399
400static int nid_cb(const char *elem, int len, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000401{
402 nid_cb_st *narg = arg;
403 size_t i;
404 int nid;
405 char etmp[20];
Kurt Roeckx2747d732015-01-24 14:46:50 +0100406 if (elem == NULL)
407 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000408 if (narg->nidcnt == MAX_CURVELIST)
409 return 0;
410 if (len > (int)(sizeof(etmp) - 1))
411 return 0;
412 memcpy(etmp, elem, len);
413 etmp[len] = 0;
414 nid = EC_curve_nist2nid(etmp);
415 if (nid == NID_undef)
416 nid = OBJ_sn2nid(etmp);
417 if (nid == NID_undef)
418 nid = OBJ_ln2nid(etmp);
419 if (nid == NID_undef)
420 return 0;
421 for (i = 0; i < narg->nidcnt; i++)
422 if (narg->nid_arr[i] == nid)
423 return 0;
424 narg->nid_arr[narg->nidcnt++] = nid;
425 return 1;
426}
427
Matt Caswellde4d7642016-11-09 14:51:06 +0000428/* Set groups based on a colon separate list */
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100429int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
Matt Caswell0f113f32015-01-22 03:40:55 +0000430{
431 nid_cb_st ncb;
432 ncb.nidcnt = 0;
433 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
434 return 0;
435 if (pext == NULL)
436 return 1;
Matt Caswellde4d7642016-11-09 14:51:06 +0000437 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
Matt Caswell0f113f32015-01-22 03:40:55 +0000438}
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100439/* Return group id of a key */
440static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000441{
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100442 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
Matt Caswell0f113f32015-01-22 03:40:55 +0000443 const EC_GROUP *grp;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100444
445 if (ec == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000446 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000447 grp = EC_KEY_get0_group(ec);
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100448 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
Matt Caswell0f113f32015-01-22 03:40:55 +0000449}
450
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100451/* Check a key is compatible with compression extension */
452static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000453{
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100454 const EC_KEY *ec;
455 const EC_GROUP *grp;
456 unsigned char comp_id;
457 size_t i;
458
459 /* If not an EC key nothing to check */
460 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
461 return 1;
462 ec = EVP_PKEY_get0_EC_KEY(pkey);
463 grp = EC_KEY_get0_group(ec);
464
465 /* Get required compression id */
466 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
467 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
468 } else if (SSL_IS_TLS13(s)) {
469 /* Compression not allowed in TLS 1.3 */
470 return 0;
471 } else {
472 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
473
474 if (field_type == NID_X9_62_prime_field)
475 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
476 else if (field_type == NID_X9_62_prime_field)
477 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
478 else
479 return 0;
480 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000481 /*
482 * If point formats extension present check it, otherwise everything is
483 * supported (see RFC4492).
484 */
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100485 if (s->session->ext.ecpointformats == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000486 return 1;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100487
488 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
489 if (s->session->ext.ecpointformats[i] == comp_id)
490 return 1;
491 }
492 return 0;
493}
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100494
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100495/* Check a group id matches preferences */
496static int tls1_check_group_id(SSL *s, uint16_t group_id)
497 {
498 const uint16_t *groups;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100499 size_t groups_len;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100500
501 if (group_id == 0)
502 return 0;
503
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100504 if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
505 return 0;
506
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100507 /* Check group is one of our preferences */
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100508 tls1_get_supported_groups(s, &groups, &groups_len);
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100509 if (!tls1_in_list(group_id, groups, groups_len))
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100510 return 0;
511
512 /* For clients, nothing more to check */
513 if (!s->server)
514 return 1;
515
516 /* Check group is one of peers preferences */
Dr. Stephen Hensonff6d20a2017-09-26 15:28:16 +0100517 tls1_get_peer_groups(s, &groups, &groups_len);
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100518
519 /*
520 * RFC 4492 does not require the supported elliptic curves extension
521 * so if it is not sent we can just choose any curve.
522 * It is invalid to send an empty list in the supported groups
523 * extension, so groups_len == 0 always means no extension.
524 */
525 if (groups_len == 0)
526 return 1;
Dr. Stephen Hensonb50951d2017-09-24 21:59:39 +0100527 return tls1_in_list(group_id, groups, groups_len);
Matt Caswell0f113f32015-01-22 03:40:55 +0000528}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +0000529
Matt Caswell7da160b2016-11-25 10:22:02 +0000530void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
531 size_t *num_formats)
Matt Caswell0f113f32015-01-22 03:40:55 +0000532{
533 /*
534 * If we have a custom point format list use it otherwise use default
535 */
Rich Salzaff8c122016-12-08 14:18:40 -0500536 if (s->ext.ecpointformats) {
537 *pformats = s->ext.ecpointformats;
538 *num_formats = s->ext.ecpointformats_len;
Matt Caswell0f113f32015-01-22 03:40:55 +0000539 } else {
540 *pformats = ecformats_default;
541 /* For Suite B we don't support char2 fields */
542 if (tls1_suiteb(s))
543 *num_formats = sizeof(ecformats_default) - 1;
544 else
545 *num_formats = sizeof(ecformats_default);
546 }
547}
Dr. Stephen Henson5087afa2012-11-26 18:38:10 +0000548
Matt Caswell0f113f32015-01-22 03:40:55 +0000549/*
550 * Check cert parameters compatible with extensions: currently just checks EC
551 * certificates have compatible curves and compression.
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +0000552 */
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +0000553static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
Matt Caswell0f113f32015-01-22 03:40:55 +0000554{
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100555 uint16_t group_id;
Matt Caswell0f113f32015-01-22 03:40:55 +0000556 EVP_PKEY *pkey;
Dr. Stephen Henson8382fd32015-12-20 00:32:36 +0000557 pkey = X509_get0_pubkey(x);
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100558 if (pkey == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000559 return 0;
560 /* If not EC nothing to do */
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +0000561 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
Matt Caswell0f113f32015-01-22 03:40:55 +0000562 return 1;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100563 /* Check compression */
564 if (!tls1_check_pkey_comp(s, pkey))
Matt Caswell0f113f32015-01-22 03:40:55 +0000565 return 0;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100566 group_id = tls1_get_group_id(pkey);
567 if (!tls1_check_group_id(s, group_id))
Matt Caswell0f113f32015-01-22 03:40:55 +0000568 return 0;
569 /*
570 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +0000571 * SHA384+P-384.
Matt Caswell0f113f32015-01-22 03:40:55 +0000572 */
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +0000573 if (check_ee_md && tls1_suiteb(s)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000574 int check_md;
575 size_t i;
576 CERT *c = s->cert;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100577
Matt Caswell0f113f32015-01-22 03:40:55 +0000578 /* Check to see we have necessary signing algorithm */
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100579 if (group_id == TLSEXT_curve_P_256)
Matt Caswell0f113f32015-01-22 03:40:55 +0000580 check_md = NID_ecdsa_with_SHA256;
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100581 else if (group_id == TLSEXT_curve_P_384)
Matt Caswell0f113f32015-01-22 03:40:55 +0000582 check_md = NID_ecdsa_with_SHA384;
583 else
584 return 0; /* Should never happen */
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100585 for (i = 0; i < c->shared_sigalgslen; i++) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000586 if (check_md == c->shared_sigalgs[i]->sigandhash)
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100587 return 1;;
588 }
589 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000590 }
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100591 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000592}
593
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100594/*
FdaSilvaYY8483a002016-03-10 21:34:48 +0100595 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
Kurt Roeckx6977e8e2015-12-04 22:25:11 +0100596 * @s: SSL connection
597 * @cid: Cipher ID we're considering using
598 *
599 * Checks that the kECDHE cipher suite we're considering using
600 * is compatible with the client extensions.
601 *
602 * Returns 0 when the cipher can't be used or 1 when it can.
603 */
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000604int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000605{
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100606 /* If not Suite B just need a shared group */
607 if (!tls1_suiteb(s))
608 return tls1_shared_group(s, 0) != 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000609 /*
610 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
611 * curves permitted.
612 */
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100613 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
614 return tls1_check_group_id(s, TLSEXT_curve_P_256);
615 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
616 return tls1_check_group_id(s, TLSEXT_curve_P_384);
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +0100617
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100618 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000619}
Dr. Stephen Hensond0595f12012-03-28 15:05:04 +0000620
Dr. Stephen Henson14536c82013-08-17 17:40:08 +0100621#else
622
623static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
Matt Caswell0f113f32015-01-22 03:40:55 +0000624{
625 return 1;
626}
Dr. Stephen Henson14536c82013-08-17 17:40:08 +0100627
Matt Caswell0f113f32015-01-22 03:40:55 +0000628#endif /* OPENSSL_NO_EC */
Bodo Möllerf1fd4542006-01-03 03:27:19 +0000629
Matt Caswell703bcee2016-12-14 14:31:21 +0000630/* Default sigalg schemes */
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000631static const uint16_t tls12_sigalgs[] = {
Matt Caswell703bcee2016-12-14 14:31:21 +0000632#ifndef OPENSSL_NO_EC
633 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
634 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
635 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
Dr. Stephen Henson3d234c92017-05-24 21:56:38 +0100636 TLSEXT_SIGALG_ed25519,
Matt Caswelle481f9b2015-05-15 10:49:56 +0100637#endif
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000638
Matt Caswell536199e2016-12-14 16:37:48 +0000639 TLSEXT_SIGALG_rsa_pss_sha256,
640 TLSEXT_SIGALG_rsa_pss_sha384,
641 TLSEXT_SIGALG_rsa_pss_sha512,
642
Matt Caswell703bcee2016-12-14 14:31:21 +0000643 TLSEXT_SIGALG_rsa_pkcs1_sha256,
644 TLSEXT_SIGALG_rsa_pkcs1_sha384,
645 TLSEXT_SIGALG_rsa_pkcs1_sha512,
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000646
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000647#ifndef OPENSSL_NO_EC
Matt Caswelld8311fc2017-06-08 17:15:45 +0100648 TLSEXT_SIGALG_ecdsa_sha224,
Matt Caswell42ab2232017-01-10 13:45:24 +0000649 TLSEXT_SIGALG_ecdsa_sha1,
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000650#endif
Matt Caswelld8311fc2017-06-08 17:15:45 +0100651 TLSEXT_SIGALG_rsa_pkcs1_sha224,
Matt Caswell42ab2232017-01-10 13:45:24 +0000652 TLSEXT_SIGALG_rsa_pkcs1_sha1,
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000653#ifndef OPENSSL_NO_DSA
Matt Caswelld8311fc2017-06-08 17:15:45 +0100654 TLSEXT_SIGALG_dsa_sha224,
Matt Caswell42ab2232017-01-10 13:45:24 +0000655 TLSEXT_SIGALG_dsa_sha1,
656
Matt Caswell703bcee2016-12-14 14:31:21 +0000657 TLSEXT_SIGALG_dsa_sha256,
658 TLSEXT_SIGALG_dsa_sha384,
659 TLSEXT_SIGALG_dsa_sha512
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000660#endif
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +0000661};
Matt Caswell0f113f32015-01-22 03:40:55 +0000662
Matt Caswelle481f9b2015-05-15 10:49:56 +0100663#ifndef OPENSSL_NO_EC
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000664static const uint16_t suiteb_sigalgs[] = {
Matt Caswell703bcee2016-12-14 14:31:21 +0000665 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
666 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000667};
Matt Caswelle481f9b2015-05-15 10:49:56 +0100668#endif
Rich Salzaff8c122016-12-08 14:18:40 -0500669
Matt Caswell7a531ee2017-01-10 14:23:02 +0000670static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000671#ifndef OPENSSL_NO_EC
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000672 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000673 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
674 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000675 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000676 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
677 NID_ecdsa_with_SHA384, NID_secp384r1},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000678 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000679 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
680 NID_ecdsa_with_SHA512, NID_secp521r1},
Dr. Stephen Hensonb04d4e32017-05-24 15:28:26 +0100681 {"ed25519", TLSEXT_SIGALG_ed25519,
Dr. Stephen Hensond2916a52017-06-20 16:32:44 +0100682 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
Dr. Stephen Hensonb04d4e32017-05-24 15:28:26 +0100683 NID_undef, NID_undef},
Matt Caswelld8311fc2017-06-08 17:15:45 +0100684 {NULL, TLSEXT_SIGALG_ecdsa_sha224,
685 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
686 NID_ecdsa_with_SHA224, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000687 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000688 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
689 NID_ecdsa_with_SHA1, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000690#endif
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000691 {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000692 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
693 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000694 {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000695 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
696 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000697 {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000698 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
699 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000700 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000701 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000702 NID_sha256WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000703 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000704 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000705 NID_sha384WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000706 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000707 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000708 NID_sha512WithRSAEncryption, NID_undef},
Matt Caswelld8311fc2017-06-08 17:15:45 +0100709 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
710 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
711 NID_sha224WithRSAEncryption, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000712 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +0000713 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000714 NID_sha1WithRSAEncryption, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000715#ifndef OPENSSL_NO_DSA
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000716 {NULL, TLSEXT_SIGALG_dsa_sha256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000717 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
718 NID_dsa_with_SHA256, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000719 {NULL, TLSEXT_SIGALG_dsa_sha384,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000720 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
721 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000722 {NULL, TLSEXT_SIGALG_dsa_sha512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000723 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
724 NID_undef, NID_undef},
Matt Caswelld8311fc2017-06-08 17:15:45 +0100725 {NULL, TLSEXT_SIGALG_dsa_sha224,
726 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
727 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000728 {NULL, TLSEXT_SIGALG_dsa_sha1,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000729 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
730 NID_dsaWithSHA1, NID_undef},
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000731#endif
732#ifndef OPENSSL_NO_GOST
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000733 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000734 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
735 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
736 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000737 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000738 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
739 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
740 NID_undef, NID_undef},
Dr. Stephen Hensonedbfba12017-01-26 14:23:05 +0000741 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
Dr. Stephen Henson17ae3842017-01-30 16:05:23 +0000742 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
743 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
744 NID_undef, NID_undef}
Matt Caswell5eeb6c62017-01-10 14:38:09 +0000745#endif
Matt Caswell703bcee2016-12-14 14:31:21 +0000746};
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000747/* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
748static const SIGALG_LOOKUP legacy_rsa_sigalg = {
749 "rsa_pkcs1_md5_sha1", 0,
750 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
751 EVP_PKEY_RSA, SSL_PKEY_RSA,
752 NID_undef, NID_undef
753};
754
755/*
756 * Default signature algorithm values used if signature algorithms not present.
757 * From RFC5246. Note: order must match certificate index order.
758 */
759static const uint16_t tls_default_sigalg[] = {
760 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
Dr. Stephen Henson045d0782017-09-13 13:53:03 +0100761 0, /* SSL_PKEY_RSA_PSS_SIGN */
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000762 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
763 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
764 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
765 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
Dr. Stephen Henson07afa3d2017-05-24 21:54:03 +0100766 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
767 0 /* SSL_PKEY_ED25519 */
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000768};
Matt Caswell703bcee2016-12-14 14:31:21 +0000769
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000770/* Lookup TLS signature algorithm */
771static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
Matt Caswell703bcee2016-12-14 14:31:21 +0000772{
773 size_t i;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000774 const SIGALG_LOOKUP *s;
Matt Caswell703bcee2016-12-14 14:31:21 +0000775
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000776 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
777 i++, s++) {
778 if (s->sigalg == sigalg)
779 return s;
Matt Caswell703bcee2016-12-14 14:31:21 +0000780 }
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000781 return NULL;
782}
Dr. Stephen Henson168067b2017-06-14 16:54:08 +0100783/* Lookup hash: return 0 if invalid or not enabled */
784int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
785{
786 const EVP_MD *md;
787 if (lu == NULL)
788 return 0;
789 /* lu->hash == NID_undef means no associated digest */
790 if (lu->hash == NID_undef) {
791 md = NULL;
792 } else {
793 md = ssl_md(lu->hash_idx);
794 if (md == NULL)
795 return 0;
796 }
797 if (pmd)
798 *pmd = md;
799 return 1;
800}
801
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000802/*
803 * Return a signature algorithm for TLS < 1.2 where the signature type
804 * is fixed by the certificate type.
805 */
806static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
807{
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +0100808 if (idx == -1) {
809 if (s->server) {
810 size_t i;
811
812 /* Work out index corresponding to ciphersuite */
813 for (i = 0; i < SSL_PKEY_NUM; i++) {
814 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
815
816 if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) {
817 idx = i;
818 break;
819 }
820 }
821 } else {
822 idx = s->cert->key - s->cert->pkeys;
823 }
824 }
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000825 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
826 return NULL;
827 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
828 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
829
Dr. Stephen Henson168067b2017-06-14 16:54:08 +0100830 if (!tls1_lookup_md(lu, NULL))
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000831 return NULL;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000832 return lu;
833 }
834 return &legacy_rsa_sigalg;
835}
836/* Set peer sigalg based key type */
837int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
838{
Dr. Stephen Henson52fd27f2017-06-29 15:20:09 +0100839 size_t idx;
840 const SIGALG_LOOKUP *lu;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000841
Dr. Stephen Henson52fd27f2017-06-29 15:20:09 +0100842 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
843 return 0;
844 lu = tls1_get_legacy_sigalg(s, idx);
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +0000845 if (lu == NULL)
846 return 0;
847 s->s3->tmp.peer_sigalg = lu;
848 return 1;
849}
Matt Caswell703bcee2016-12-14 14:31:21 +0000850
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000851size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
Matt Caswell0f113f32015-01-22 03:40:55 +0000852{
853 /*
854 * If Suite B mode use Suite B sigalgs only, ignore any other
855 * preferences.
856 */
Matt Caswelle481f9b2015-05-15 10:49:56 +0100857#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +0000858 switch (tls1_suiteb(s)) {
859 case SSL_CERT_FLAG_SUITEB_128_LOS:
860 *psigs = suiteb_sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +0000861 return OSSL_NELEM(suiteb_sigalgs);
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000862
Matt Caswell0f113f32015-01-22 03:40:55 +0000863 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
864 *psigs = suiteb_sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +0000865 return 1;
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000866
Matt Caswell0f113f32015-01-22 03:40:55 +0000867 case SSL_CERT_FLAG_SUITEB_192_LOS:
Matt Caswell7a531ee2017-01-10 14:23:02 +0000868 *psigs = suiteb_sigalgs + 1;
869 return 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000870 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100871#endif
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +0000872 /*
873 * We use client_sigalgs (if not NULL) if we're a server
874 * and sending a certificate request or if we're a client and
875 * determining which shared algorithm to use.
876 */
877 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000878 *psigs = s->cert->client_sigalgs;
879 return s->cert->client_sigalgslen;
880 } else if (s->cert->conf_sigalgs) {
881 *psigs = s->cert->conf_sigalgs;
882 return s->cert->conf_sigalgslen;
883 } else {
884 *psigs = tls12_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +0000885 return OSSL_NELEM(tls12_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +0000886 }
887}
888
889/*
890 * Check signature algorithm is consistent with sent supported signature
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +0000891 * algorithms and if so set relevant digest and signature scheme in
892 * s.
Dr. Stephen Hensonec4a50b2012-07-24 18:11:27 +0000893 */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000894int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000895{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +0000896 const uint16_t *sent_sigs;
Dr. Stephen Henson5554fac2017-01-25 16:46:02 +0000897 const EVP_MD *md = NULL;
Matt Caswell703bcee2016-12-14 14:31:21 +0000898 char sigalgstr[2];
Matt Caswell0f113f32015-01-22 03:40:55 +0000899 size_t sent_sigslen, i;
Matt Caswell536199e2016-12-14 16:37:48 +0000900 int pkeyid = EVP_PKEY_id(pkey);
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000901 const SIGALG_LOOKUP *lu;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +0000902
Matt Caswell0f113f32015-01-22 03:40:55 +0000903 /* Should never happen */
Matt Caswell536199e2016-12-14 16:37:48 +0000904 if (pkeyid == -1)
Matt Caswell0f113f32015-01-22 03:40:55 +0000905 return -1;
Dr. Stephen Henson5a8916d2017-02-17 16:08:19 +0000906 if (SSL_IS_TLS13(s)) {
907 /* Disallow DSA for TLS 1.3 */
908 if (pkeyid == EVP_PKEY_DSA) {
909 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
910 return 0;
911 }
912 /* Only allow PSS for TLS 1.3 */
913 if (pkeyid == EVP_PKEY_RSA)
914 pkeyid = EVP_PKEY_RSA_PSS;
915 }
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000916 lu = tls1_lookup_sigalg(sig);
917 /*
Matt Caswelld8311fc2017-06-08 17:15:45 +0100918 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
919 * is consistent with signature: RSA keys can be used for RSA-PSS
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000920 */
Matt Caswelld8311fc2017-06-08 17:15:45 +0100921 if (lu == NULL
922 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
Dr. Stephen Henson095a9822017-02-25 00:25:04 +0000923 || (pkeyid != lu->sig
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000924 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000925 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
926 return 0;
927 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100928#ifndef OPENSSL_NO_EC
Matt Caswellfe3066e2017-01-03 10:01:39 +0000929 if (pkeyid == EVP_PKEY_EC) {
Dr. Stephen Henson44b63182017-01-30 17:27:35 +0000930
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100931 /* Check point compression is permitted */
932 if (!tls1_check_pkey_comp(s, pkey)) {
933 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
934 SSL_R_ILLEGAL_POINT_COMPRESSION);
935 return 0;
936 }
937
938 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
939 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
940 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
941 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
942
Dr. Stephen Hensona34a9df2017-02-14 14:27:15 +0000943 if (lu->curve != NID_undef && curve != lu->curve) {
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000944 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
Matt Caswell0f113f32015-01-22 03:40:55 +0000945 return 0;
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000946 }
Dr. Stephen Henson4a1b4282017-09-24 01:45:27 +0100947 }
948 if (!SSL_IS_TLS13(s)) {
949 /* Check curve matches extensions */
950 if (!tls1_check_group_id(s, tls1_get_group_id(pkey))) {
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000951 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
952 return 0;
953 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000954 if (tls1_suiteb(s)) {
Dr. Stephen Hensonf1adb002017-01-30 22:02:11 +0000955 /* Check sigalg matches a permissible Suite B value */
956 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
957 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
958 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
959 SSL_R_WRONG_SIGNATURE_TYPE);
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000960 return 0;
Dr. Stephen Hensonf1adb002017-01-30 22:02:11 +0000961 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000962 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000963 }
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000964 } else if (tls1_suiteb(s)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000965 return 0;
Dr. Stephen Henson8f88cb52017-01-30 14:45:20 +0000966 }
Matt Caswelle481f9b2015-05-15 10:49:56 +0100967#endif
Dr. Stephen Henson2ea80352012-08-15 15:15:05 +0000968
Matt Caswell0f113f32015-01-22 03:40:55 +0000969 /* Check signature matches a type we sent */
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +0000970 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
Matt Caswell536199e2016-12-14 16:37:48 +0000971 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
Matt Caswell703bcee2016-12-14 14:31:21 +0000972 if (sig == *sent_sigs)
Matt Caswell0f113f32015-01-22 03:40:55 +0000973 break;
974 }
975 /* Allow fallback to SHA1 if not strict mode */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +0000976 if (i == sent_sigslen && (lu->hash != NID_sha1
977 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000978 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
979 return 0;
980 }
Dr. Stephen Henson168067b2017-06-14 16:54:08 +0100981 if (!tls1_lookup_md(lu, &md)) {
982 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
983 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000984 }
Dr. Stephen Henson168067b2017-06-14 16:54:08 +0100985 if (md != NULL) {
986 /*
987 * Make sure security callback allows algorithm. For historical
988 * reasons we have to pass the sigalg as a two byte char array.
989 */
990 sigalgstr[0] = (sig >> 8) & 0xff;
991 sigalgstr[1] = sig & 0xff;
992 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
993 EVP_MD_size(md) * 4, EVP_MD_type(md),
994 (void *)sigalgstr)) {
995 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
996 return 0;
997 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000998 }
Dr. Stephen Henson6cbebb52017-01-30 18:10:17 +0000999 /* Store the sigalg the peer uses */
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +00001000 s->s3->tmp.peer_sigalg = lu;
Matt Caswell0f113f32015-01-22 03:40:55 +00001001 return 1;
1002}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001003
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001004int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
1005{
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +00001006 if (s->s3->tmp.peer_sigalg == NULL)
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001007 return 0;
Dr. Stephen Hensonf742cda2017-01-30 13:48:39 +00001008 *pnid = s->s3->tmp.peer_sigalg->sig;
Dr. Stephen Henson42ef7ae2017-01-25 23:28:57 +00001009 return 1;
1010}
1011
Matt Caswell0f113f32015-01-22 03:40:55 +00001012/*
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001013 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1014 * supported, doesn't appear in supported signature algorithms, isn't supported
1015 * by the enabled protocol versions or by the security level.
1016 *
1017 * This function should only be used for checking which ciphers are supported
1018 * by the client.
1019 *
1020 * Call ssl_cipher_disabled() to check that it's enabled or not.
Dr. Stephen Hensonb7bfe692012-07-18 14:09:46 +00001021 */
1022void ssl_set_client_disabled(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001023{
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001024 s->s3->tmp.mask_a = 0;
1025 s->s3->tmp.mask_k = 0;
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001026 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
Matt Caswell38a73152017-04-26 11:28:20 +01001027 ssl_get_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
Emilia Kaspera230b262016-08-05 19:03:17 +02001028#ifndef OPENSSL_NO_PSK
Matt Caswell0f113f32015-01-22 03:40:55 +00001029 /* with PSK there must be client callback set */
1030 if (!s->psk_client_callback) {
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001031 s->s3->tmp.mask_a |= SSL_aPSK;
Dr. Stephen Hensonfe5eef32015-06-28 17:01:07 +01001032 s->s3->tmp.mask_k |= SSL_PSK;
Matt Caswell0f113f32015-01-22 03:40:55 +00001033 }
Emilia Kaspera230b262016-08-05 19:03:17 +02001034#endif /* OPENSSL_NO_PSK */
Matt Caswelle481f9b2015-05-15 10:49:56 +01001035#ifndef OPENSSL_NO_SRP
Matt Caswell0f113f32015-01-22 03:40:55 +00001036 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001037 s->s3->tmp.mask_a |= SSL_aSRP;
1038 s->s3->tmp.mask_k |= SSL_kSRP;
Matt Caswell0f113f32015-01-22 03:40:55 +00001039 }
Matt Caswelle481f9b2015-05-15 10:49:56 +01001040#endif
Matt Caswell0f113f32015-01-22 03:40:55 +00001041}
Dr. Stephen Hensonfc101f82011-05-11 16:33:28 +00001042
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001043/*
1044 * ssl_cipher_disabled - check that a cipher is disabled or not
1045 * @s: SSL connection that you want to use the cipher on
1046 * @c: cipher to check
1047 * @op: Security check that you want to do
Matt Caswell8af91fd2017-04-12 17:02:42 +01001048 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001049 *
1050 * Returns 1 when it's disabled, 0 when enabled.
1051 */
Matt Caswell8af91fd2017-04-12 17:02:42 +01001052int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
Matt Caswell0f113f32015-01-22 03:40:55 +00001053{
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001054 if (c->algorithm_mkey & s->s3->tmp.mask_k
Dr. Stephen Henson4d69f9e2015-05-18 23:29:57 +01001055 || c->algorithm_auth & s->s3->tmp.mask_a)
Matt Caswell0f113f32015-01-22 03:40:55 +00001056 return 1;
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001057 if (s->s3->tmp.max_ver == 0)
1058 return 1;
Matt Caswell8af91fd2017-04-12 17:02:42 +01001059 if (!SSL_IS_DTLS(s)) {
1060 int min_tls = c->min_tls;
1061
1062 /*
1063 * For historical reasons we will allow ECHDE to be selected by a server
1064 * in SSLv3 if we are a client
1065 */
1066 if (min_tls == TLS1_VERSION && ecdhe
1067 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
1068 min_tls = SSL3_VERSION;
1069
1070 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
1071 return 1;
1072 }
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001073 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
Emilia Kaspera230b262016-08-05 19:03:17 +02001074 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
Kurt Roeckx3eb2aff2016-02-07 20:17:07 +01001075 return 1;
1076
Matt Caswell0f113f32015-01-22 03:40:55 +00001077 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1078}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001079
Matt Caswell7da160b2016-11-25 10:22:02 +00001080int tls_use_ticket(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001081{
Matt Caswell08191292017-01-19 11:23:06 +00001082 if ((s->options & SSL_OP_NO_TICKET))
Matt Caswell0f113f32015-01-22 03:40:55 +00001083 return 0;
1084 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1085}
Dr. Stephen Henson8b8e5be2014-01-14 14:55:21 +00001086
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001087int tls1_set_server_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001088{
1089 int al;
1090 size_t i;
FdaSilvaYY8483a002016-03-10 21:34:48 +01001091
1092 /* Clear any shared signature algorithms */
Rich Salzb548a1f2015-05-01 10:02:07 -04001093 OPENSSL_free(s->cert->shared_sigalgs);
1094 s->cert->shared_sigalgs = NULL;
1095 s->cert->shared_sigalgslen = 0;
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001096 /* Clear certificate validity flags */
1097 for (i = 0; i < SSL_PKEY_NUM; i++)
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001098 s->s3->tmp.valid_flags[i] = 0;
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00001099 /*
1100 * If peer sent no signature algorithms check to see if we support
1101 * the default algorithm for each certificate type
1102 */
1103 if (s->s3->tmp.peer_sigalgs == NULL) {
1104 const uint16_t *sent_sigs;
1105 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001106
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00001107 for (i = 0; i < SSL_PKEY_NUM; i++) {
1108 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
1109 size_t j;
1110
1111 if (lu == NULL)
1112 continue;
1113 /* Check default matches a type we sent */
1114 for (j = 0; j < sent_sigslen; j++) {
1115 if (lu->sigalg == sent_sigs[j]) {
1116 s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN;
1117 break;
1118 }
1119 }
1120 }
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001121 return 1;
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00001122 }
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001123
1124 if (!tls1_process_sigalgs(s)) {
1125 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
1126 al = SSL_AD_INTERNAL_ERROR;
1127 goto err;
Dr. Stephen Hensond376e572015-05-12 18:56:39 +01001128 }
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001129 if (s->cert->shared_sigalgs != NULL)
1130 return 1;
Matt Caswellfb34a0f2017-05-16 17:28:23 +01001131 /* Fatal error if no shared signature algorithms */
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001132 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
Matt Caswellfb34a0f2017-05-16 17:28:23 +01001133 al = SSL_AD_HANDSHAKE_FAILURE;
Matt Caswell0f113f32015-01-22 03:40:55 +00001134 err:
1135 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1136 return 0;
1137}
Dr. Stephen Hensone469af82014-11-17 16:52:59 +00001138
Tim Hudson1d97c842014-12-28 12:48:40 +10001139/*-
Matt Caswell1ab38362016-10-22 17:24:37 +01001140 * Gets the ticket information supplied by the client if any.
Dr. Stephen Hensone7f0d922015-12-04 19:48:15 +00001141 *
Matt Caswell1ab38362016-10-22 17:24:37 +01001142 * hello: The parsed ClientHello data
Bodo Möllerc519e892011-09-05 13:36:23 +00001143 * ret: (output) on return, if a ticket was decrypted, then this is set to
1144 * point to the resulting session.
1145 *
1146 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
1147 * ciphersuite, in which case we have no use for session tickets and one will
Rich Salzaff8c122016-12-08 14:18:40 -05001148 * never be decrypted, nor will s->ext.ticket_expected be set to 1.
Bodo Möllerc519e892011-09-05 13:36:23 +00001149 *
1150 * Returns:
1151 * -1: fatal error, either from parsing or decrypting the ticket.
1152 * 0: no ticket was found (or was ignored, based on settings).
1153 * 1: a zero length extension was found, indicating that the client supports
1154 * session tickets but doesn't currently have one to offer.
1155 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
1156 * couldn't be decrypted because of a non-fatal error.
1157 * 3: a ticket was successfully decrypted and *ret was set.
1158 *
1159 * Side effects:
Rich Salzaff8c122016-12-08 14:18:40 -05001160 * Sets s->ext.ticket_expected to 1 if the server will have to issue
Bodo Möllerc519e892011-09-05 13:36:23 +00001161 * a new session ticket to the client because the client indicated support
1162 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1163 * a session ticket or we couldn't use the one it gave us, or if
Rich Salzaff8c122016-12-08 14:18:40 -05001164 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1165 * Otherwise, s->ext.ticket_expected is set to 0.
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001166 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001167TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1168 SSL_SESSION **ret)
Matt Caswell0f113f32015-01-22 03:40:55 +00001169{
Matt Caswell1ab38362016-10-22 17:24:37 +01001170 int retv;
Matt Caswell1ab38362016-10-22 17:24:37 +01001171 size_t size;
1172 RAW_EXTENSION *ticketext;
Dr. Stephen Hensone7f0d922015-12-04 19:48:15 +00001173
Matt Caswell0f113f32015-01-22 03:40:55 +00001174 *ret = NULL;
Rich Salzaff8c122016-12-08 14:18:40 -05001175 s->ext.ticket_expected = 0;
Dr. Stephen Hensone8da6a12008-09-03 22:17:11 +00001176
Matt Caswell0f113f32015-01-22 03:40:55 +00001177 /*
Matt Caswell9362c932016-11-08 16:10:21 +00001178 * If tickets disabled or not supported by the protocol version
1179 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
Matt Caswell0f113f32015-01-22 03:40:55 +00001180 * resumption.
1181 */
Matt Caswell1ab38362016-10-22 17:24:37 +01001182 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
Matt Caswellddf6ec02017-01-20 16:01:27 +00001183 return TICKET_NONE;
Matt Caswell9ceb2422015-04-16 10:06:25 +01001184
Matt Caswell70af3d82016-11-28 09:31:59 +00001185 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1186 if (!ticketext->present)
Matt Caswellddf6ec02017-01-20 16:01:27 +00001187 return TICKET_NONE;
Matt Caswell1ab38362016-10-22 17:24:37 +01001188
1189 size = PACKET_remaining(&ticketext->data);
1190 if (size == 0) {
1191 /*
1192 * The client will accept a ticket but doesn't currently have
1193 * one.
1194 */
Rich Salzaff8c122016-12-08 14:18:40 -05001195 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001196 return TICKET_EMPTY;
Matt Caswell9ceb2422015-04-16 10:06:25 +01001197 }
Rich Salzaff8c122016-12-08 14:18:40 -05001198 if (s->ext.session_secret_cb) {
Matt Caswell1ab38362016-10-22 17:24:37 +01001199 /*
1200 * Indicate that the ticket couldn't be decrypted rather than
1201 * generating the session from ticket now, trigger
1202 * abbreviated handshake based on external mechanism to
1203 * calculate the master secret later.
1204 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001205 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001206 }
Matt Caswell70af3d82016-11-28 09:31:59 +00001207
1208 retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1209 hello->session_id, hello->session_id_len, ret);
Matt Caswell1ab38362016-10-22 17:24:37 +01001210 switch (retv) {
Matt Caswell61c32642017-01-27 12:11:23 +00001211 case TICKET_NO_DECRYPT:
Rich Salzaff8c122016-12-08 14:18:40 -05001212 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001213 return TICKET_NO_DECRYPT;
Matt Caswell1ab38362016-10-22 17:24:37 +01001214
Matt Caswell61c32642017-01-27 12:11:23 +00001215 case TICKET_SUCCESS:
Matt Caswellddf6ec02017-01-20 16:01:27 +00001216 return TICKET_SUCCESS;
Matt Caswell1ab38362016-10-22 17:24:37 +01001217
Matt Caswell61c32642017-01-27 12:11:23 +00001218 case TICKET_SUCCESS_RENEW:
Rich Salzaff8c122016-12-08 14:18:40 -05001219 s->ext.ticket_expected = 1;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001220 return TICKET_SUCCESS;
Matt Caswell1ab38362016-10-22 17:24:37 +01001221
Matt Caswell61c32642017-01-27 12:11:23 +00001222 default:
Matt Caswellddf6ec02017-01-20 16:01:27 +00001223 return TICKET_FATAL_ERR_OTHER;
Matt Caswell1ab38362016-10-22 17:24:37 +01001224 }
1225}
1226
Tim Hudson1d97c842014-12-28 12:48:40 +10001227/*-
1228 * tls_decrypt_ticket attempts to decrypt a session ticket.
Bodo Möllerc519e892011-09-05 13:36:23 +00001229 *
1230 * etick: points to the body of the session ticket extension.
FdaSilvaYY8483a002016-03-10 21:34:48 +01001231 * eticklen: the length of the session tickets extension.
Bodo Möllerc519e892011-09-05 13:36:23 +00001232 * sess_id: points at the session ID.
1233 * sesslen: the length of the session ID.
1234 * psess: (output) on return, if a ticket was decrypted, then this is set to
1235 * point to the resulting session.
Bodo Möllerc519e892011-09-05 13:36:23 +00001236 */
Matt Caswellddf6ec02017-01-20 16:01:27 +00001237TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1238 size_t eticklen, const unsigned char *sess_id,
1239 size_t sesslen, SSL_SESSION **psess)
Matt Caswell0f113f32015-01-22 03:40:55 +00001240{
1241 SSL_SESSION *sess;
1242 unsigned char *sdec;
1243 const unsigned char *p;
Matt Caswellddf6ec02017-01-20 16:01:27 +00001244 int slen, renew_ticket = 0, declen;
1245 TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER;
Matt Caswell348240c2016-10-19 15:11:24 +01001246 size_t mlen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001247 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
Richard Levittebf7c6812015-11-30 13:44:28 +01001248 HMAC_CTX *hctx = NULL;
Richard Levitte846ec072015-12-13 22:08:41 +01001249 EVP_CIPHER_CTX *ctx;
Todd Short222da972017-01-31 15:32:50 -05001250 SSL_CTX *tctx = s->session_ctx;
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001251
Matt Caswell0f113f32015-01-22 03:40:55 +00001252 /* Initialize session ticket encryption and HMAC contexts */
Richard Levittebf7c6812015-11-30 13:44:28 +01001253 hctx = HMAC_CTX_new();
1254 if (hctx == NULL)
Matt Caswell1053a6e2017-01-18 16:28:23 +00001255 return TICKET_FATAL_ERR_MALLOC;
Richard Levitte846ec072015-12-13 22:08:41 +01001256 ctx = EVP_CIPHER_CTX_new();
Matt Caswell35b1a432016-02-13 23:22:45 +00001257 if (ctx == NULL) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001258 ret = TICKET_FATAL_ERR_MALLOC;
Matt Caswell35b1a432016-02-13 23:22:45 +00001259 goto err;
1260 }
Rich Salzaff8c122016-12-08 14:18:40 -05001261 if (tctx->ext.ticket_key_cb) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001262 unsigned char *nctick = (unsigned char *)etick;
Rich Salzaff8c122016-12-08 14:18:40 -05001263 int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16,
Richard Levitte846ec072015-12-13 22:08:41 +01001264 ctx, hctx, 0);
Matt Caswell0f113f32015-01-22 03:40:55 +00001265 if (rv < 0)
Matt Caswell35b1a432016-02-13 23:22:45 +00001266 goto err;
1267 if (rv == 0) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001268 ret = TICKET_NO_DECRYPT;
Matt Caswell35b1a432016-02-13 23:22:45 +00001269 goto err;
1270 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001271 if (rv == 2)
1272 renew_ticket = 1;
1273 } else {
1274 /* Check key name matches */
Rich Salzaff8c122016-12-08 14:18:40 -05001275 if (memcmp(etick, tctx->ext.tick_key_name,
1276 sizeof(tctx->ext.tick_key_name)) != 0) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001277 ret = TICKET_NO_DECRYPT;
Matt Caswell35b1a432016-02-13 23:22:45 +00001278 goto err;
1279 }
Rich Salzaff8c122016-12-08 14:18:40 -05001280 if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
1281 sizeof(tctx->ext.tick_hmac_key),
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001282 EVP_sha256(), NULL) <= 0
Emilia Kaspera230b262016-08-05 19:03:17 +02001283 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
Rich Salzaff8c122016-12-08 14:18:40 -05001284 tctx->ext.tick_aes_key,
Matt Caswell1053a6e2017-01-18 16:28:23 +00001285 etick
1286 + sizeof(tctx->ext.tick_key_name)) <= 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001287 goto err;
Emilia Kaspera230b262016-08-05 19:03:17 +02001288 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001289 }
1290 /*
1291 * Attempt to process session ticket, first conduct sanity and integrity
1292 * checks on ticket.
1293 */
Richard Levittebf7c6812015-11-30 13:44:28 +01001294 mlen = HMAC_size(hctx);
Matt Caswell348240c2016-10-19 15:11:24 +01001295 if (mlen == 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001296 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001297 }
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001298 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1299 if (eticklen <=
Matt Caswell348240c2016-10-19 15:11:24 +01001300 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
Matt Caswell1053a6e2017-01-18 16:28:23 +00001301 ret = TICKET_NO_DECRYPT;
Dr. Stephen Hensone97763c2016-08-22 17:20:01 +01001302 goto err;
1303 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001304 eticklen -= mlen;
1305 /* Check HMAC of encrypted ticket */
Richard Levittebf7c6812015-11-30 13:44:28 +01001306 if (HMAC_Update(hctx, etick, eticklen) <= 0
Emilia Kaspera230b262016-08-05 19:03:17 +02001307 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
Matt Caswell5f3d93e2015-11-06 16:31:21 +00001308 goto err;
1309 }
Richard Levittebf7c6812015-11-30 13:44:28 +01001310 HMAC_CTX_free(hctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001311 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
Richard Levitte846ec072015-12-13 22:08:41 +01001312 EVP_CIPHER_CTX_free(ctx);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001313 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001314 }
1315 /* Attempt to decrypt session data */
1316 /* Move p after IV to start of encrypted ticket, update length */
Bernd Edlinger57b0d652017-02-13 18:36:13 +01001317 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1318 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001319 sdec = OPENSSL_malloc(eticklen);
Matt Caswell348240c2016-10-19 15:11:24 +01001320 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1321 (int)eticklen) <= 0) {
Richard Levitte846ec072015-12-13 22:08:41 +01001322 EVP_CIPHER_CTX_free(ctx);
Matt Caswelld1247df2016-03-15 11:38:56 +00001323 OPENSSL_free(sdec);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001324 return TICKET_FATAL_ERR_OTHER;
Matt Caswell0f113f32015-01-22 03:40:55 +00001325 }
Matt Caswell348240c2016-10-19 15:11:24 +01001326 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
Richard Levitte846ec072015-12-13 22:08:41 +01001327 EVP_CIPHER_CTX_free(ctx);
Matt Caswell0f113f32015-01-22 03:40:55 +00001328 OPENSSL_free(sdec);
Matt Caswell1053a6e2017-01-18 16:28:23 +00001329 return TICKET_NO_DECRYPT;
Matt Caswell0f113f32015-01-22 03:40:55 +00001330 }
Matt Caswell348240c2016-10-19 15:11:24 +01001331 slen += declen;
Richard Levitte846ec072015-12-13 22:08:41 +01001332 EVP_CIPHER_CTX_free(ctx);
1333 ctx = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001334 p = sdec;
Bodo Möllerc519e892011-09-05 13:36:23 +00001335
Matt Caswell0f113f32015-01-22 03:40:55 +00001336 sess = d2i_SSL_SESSION(NULL, &p, slen);
Bernd Edlingerd3bc9802017-03-10 15:10:41 +01001337 slen -= p - sdec;
Matt Caswell0f113f32015-01-22 03:40:55 +00001338 OPENSSL_free(sdec);
1339 if (sess) {
Bernd Edlinger79020b22017-02-22 11:59:44 +01001340 /* Some additional consistency checks */
Bernd Edlingerd3bc9802017-03-10 15:10:41 +01001341 if (slen != 0 || sess->session_id_length != 0) {
Bernd Edlinger79020b22017-02-22 11:59:44 +01001342 SSL_SESSION_free(sess);
Bernd Edlinger0b1f2662017-02-22 08:14:07 +01001343 return TICKET_NO_DECRYPT;
Bernd Edlinger79020b22017-02-22 11:59:44 +01001344 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001345 /*
1346 * The session ID, if non-empty, is used by some clients to detect
1347 * that the ticket has been accepted. So we copy it to the session
1348 * structure. If it is empty set length to zero as required by
1349 * standard.
1350 */
1351 if (sesslen)
1352 memcpy(sess->session_id, sess_id, sesslen);
1353 sess->session_id_length = sesslen;
1354 *psess = sess;
1355 if (renew_ticket)
Matt Caswell1053a6e2017-01-18 16:28:23 +00001356 return TICKET_SUCCESS_RENEW;
Matt Caswell0f113f32015-01-22 03:40:55 +00001357 else
Matt Caswell1053a6e2017-01-18 16:28:23 +00001358 return TICKET_SUCCESS;
Matt Caswell0f113f32015-01-22 03:40:55 +00001359 }
1360 ERR_clear_error();
1361 /*
1362 * For session parse failure, indicate that we need to send a new ticket.
1363 */
Matt Caswell1053a6e2017-01-18 16:28:23 +00001364 return TICKET_NO_DECRYPT;
Emilia Kaspera230b262016-08-05 19:03:17 +02001365 err:
Richard Levitte846ec072015-12-13 22:08:41 +01001366 EVP_CIPHER_CTX_free(ctx);
Richard Levittebf7c6812015-11-30 13:44:28 +01001367 HMAC_CTX_free(hctx);
Matt Caswell35b1a432016-02-13 23:22:45 +00001368 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +00001369}
Dr. Stephen Henson6434abb2007-08-11 23:18:29 +00001370
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001371/* Check to see if a signature algorithm is allowed */
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001372static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
Matt Caswell0f113f32015-01-22 03:40:55 +00001373{
Matt Caswell703bcee2016-12-14 14:31:21 +00001374 unsigned char sigalgstr[2];
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001375 int secbits;
Matt Caswell703bcee2016-12-14 14:31:21 +00001376
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001377 /* See if sigalgs is recognised and if hash is enabled */
Dr. Stephen Henson168067b2017-06-14 16:54:08 +01001378 if (!tls1_lookup_md(lu, NULL))
Matt Caswell0f113f32015-01-22 03:40:55 +00001379 return 0;
Dr. Stephen Henson224b4e32017-03-01 17:15:43 +00001380 /* DSA is not allowed in TLS 1.3 */
1381 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
1382 return 0;
Benjamin Kaduk6ffeb262017-04-24 18:20:33 -05001383 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1384 if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION
1385 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
1386 || lu->hash_idx == SSL_MD_MD5_IDX
1387 || lu->hash_idx == SSL_MD_SHA224_IDX))
1388 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001389 /* See if public key algorithm allowed */
Dr. Stephen Hensonb8858ae2017-06-28 17:56:45 +01001390 if (ssl_cert_is_disabled(lu->sig_idx))
Matt Caswell0f113f32015-01-22 03:40:55 +00001391 return 0;
Dr. Stephen Henson168067b2017-06-14 16:54:08 +01001392 if (lu->hash == NID_undef)
1393 return 1;
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001394 /* Security bits: half digest bits */
1395 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
Matt Caswell0f113f32015-01-22 03:40:55 +00001396 /* Finally see if security callback allows it */
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001397 sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
1398 sigalgstr[1] = lu->sigalg & 0xff;
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001399 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
Matt Caswell0f113f32015-01-22 03:40:55 +00001400}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001401
Matt Caswell0f113f32015-01-22 03:40:55 +00001402/*
1403 * Get a mask of disabled public key algorithms based on supported signature
1404 * algorithms. For example if no signature algorithm supports RSA then RSA is
1405 * disabled.
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001406 */
1407
Dr. Stephen Henson90d9e492015-11-05 16:14:17 +00001408void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00001409{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001410 const uint16_t *sigalgs;
Matt Caswell0f113f32015-01-22 03:40:55 +00001411 size_t i, sigalgslen;
Dr. Stephen Henson13cc2572017-07-04 11:38:23 +01001412 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
Matt Caswell0f113f32015-01-22 03:40:55 +00001413 /*
Dr. Stephen Henson13cc2572017-07-04 11:38:23 +01001414 * Go through all signature algorithms seeing if we support any
1415 * in disabled_mask.
Matt Caswell0f113f32015-01-22 03:40:55 +00001416 */
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +00001417 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
Matt Caswell703bcee2016-12-14 14:31:21 +00001418 for (i = 0; i < sigalgslen; i ++, sigalgs++) {
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001419 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
Dr. Stephen Henson13cc2572017-07-04 11:38:23 +01001420 const SSL_CERT_LOOKUP *clu;
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001421
1422 if (lu == NULL)
1423 continue;
Dr. Stephen Henson13cc2572017-07-04 11:38:23 +01001424
1425 clu = ssl_cert_lookup_by_idx(lu->sig_idx);
1426
1427 /* If algorithm is disabled see if we can enable it */
1428 if ((clu->amask & disabled_mask) != 0
1429 && tls12_sigalg_allowed(s, op, lu))
1430 disabled_mask &= ~clu->amask;
Matt Caswell0f113f32015-01-22 03:40:55 +00001431 }
Dr. Stephen Henson13cc2572017-07-04 11:38:23 +01001432 *pmask_a |= disabled_mask;
Matt Caswell0f113f32015-01-22 03:40:55 +00001433}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00001434
Matt Caswellae2f7b32016-09-05 17:34:04 +01001435int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001436 const uint16_t *psig, size_t psiglen)
Matt Caswell2c7b4db2016-08-03 20:57:52 +01001437{
1438 size_t i;
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001439 int rv = 0;
Matt Caswellc0f9e232016-09-13 09:40:38 +01001440
Matt Caswell703bcee2016-12-14 14:31:21 +00001441 for (i = 0; i < psiglen; i++, psig++) {
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001442 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
1443
1444 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
1445 continue;
1446 if (!WPACKET_put_bytes_u16(pkt, *psig))
1447 return 0;
1448 /*
1449 * If TLS 1.3 must have at least one valid TLS 1.3 message
Matt Caswelld8311fc2017-06-08 17:15:45 +01001450 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001451 */
1452 if (rv == 0 && (!SSL_IS_TLS13(s)
Matt Caswelld8311fc2017-06-08 17:15:45 +01001453 || (lu->sig != EVP_PKEY_RSA
1454 && lu->hash != NID_sha1
1455 && lu->hash != NID_sha224)))
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001456 rv = 1;
Matt Caswell2c7b4db2016-08-03 20:57:52 +01001457 }
Dr. Stephen Henson5528d682017-03-03 03:23:27 +00001458 if (rv == 0)
1459 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001460 return rv;
Matt Caswell2c7b4db2016-08-03 20:57:52 +01001461}
1462
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001463/* Given preference and allowed sigalgs set shared sigalgs */
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001464static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001465 const uint16_t *pref, size_t preflen,
1466 const uint16_t *allow, size_t allowlen)
Matt Caswell0f113f32015-01-22 03:40:55 +00001467{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001468 const uint16_t *ptmp, *atmp;
Matt Caswell0f113f32015-01-22 03:40:55 +00001469 size_t i, j, nmatch = 0;
Matt Caswell703bcee2016-12-14 14:31:21 +00001470 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001471 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
1472
Matt Caswell0f113f32015-01-22 03:40:55 +00001473 /* Skip disabled hashes or signature algorithms */
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001474 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
Matt Caswell0f113f32015-01-22 03:40:55 +00001475 continue;
Matt Caswell703bcee2016-12-14 14:31:21 +00001476 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1477 if (*ptmp == *atmp) {
Matt Caswell0f113f32015-01-22 03:40:55 +00001478 nmatch++;
Dr. Stephen Hensonb0e9ab92017-03-03 03:10:13 +00001479 if (shsig)
1480 *shsig++ = lu;
Matt Caswell0f113f32015-01-22 03:40:55 +00001481 break;
1482 }
1483 }
1484 }
1485 return nmatch;
1486}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001487
1488/* Set shared signature algorithms for SSL structures */
1489static int tls1_set_shared_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001490{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001491 const uint16_t *pref, *allow, *conf;
Matt Caswell0f113f32015-01-22 03:40:55 +00001492 size_t preflen, allowlen, conflen;
1493 size_t nmatch;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001494 const SIGALG_LOOKUP **salgs = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +00001495 CERT *c = s->cert;
1496 unsigned int is_suiteb = tls1_suiteb(s);
Rich Salzb548a1f2015-05-01 10:02:07 -04001497
1498 OPENSSL_free(c->shared_sigalgs);
1499 c->shared_sigalgs = NULL;
1500 c->shared_sigalgslen = 0;
Matt Caswell0f113f32015-01-22 03:40:55 +00001501 /* If client use client signature algorithms if not NULL */
1502 if (!s->server && c->client_sigalgs && !is_suiteb) {
1503 conf = c->client_sigalgs;
1504 conflen = c->client_sigalgslen;
1505 } else if (c->conf_sigalgs && !is_suiteb) {
1506 conf = c->conf_sigalgs;
1507 conflen = c->conf_sigalgslen;
1508 } else
Dr. Stephen Hensona9669dd2017-01-25 19:12:48 +00001509 conflen = tls12_get_psigalgs(s, 0, &conf);
Matt Caswell0f113f32015-01-22 03:40:55 +00001510 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1511 pref = conf;
1512 preflen = conflen;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001513 allow = s->s3->tmp.peer_sigalgs;
1514 allowlen = s->s3->tmp.peer_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001515 } else {
1516 allow = conf;
1517 allowlen = conflen;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001518 pref = s->s3->tmp.peer_sigalgs;
1519 preflen = s->s3->tmp.peer_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001520 }
1521 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
Dr. Stephen Henson34e3edb2015-03-03 13:20:57 +00001522 if (nmatch) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001523 salgs = OPENSSL_malloc(nmatch * sizeof(*salgs));
Matt Caswella71edf32015-10-30 10:05:53 +00001524 if (salgs == NULL)
Dr. Stephen Henson34e3edb2015-03-03 13:20:57 +00001525 return 0;
1526 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1527 } else {
1528 salgs = NULL;
1529 }
Matt Caswell0f113f32015-01-22 03:40:55 +00001530 c->shared_sigalgs = salgs;
1531 c->shared_sigalgslen = nmatch;
1532 return 1;
1533}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001534
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001535int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
Matt Caswell0f113f32015-01-22 03:40:55 +00001536{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001537 unsigned int stmp;
Matt Caswell703bcee2016-12-14 14:31:21 +00001538 size_t size, i;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001539 uint16_t *buf;
Dr. Stephen Henson6b7be582011-05-06 13:00:07 +00001540
Matt Caswell703bcee2016-12-14 14:31:21 +00001541 size = PACKET_remaining(pkt);
1542
1543 /* Invalid data length */
Dr. Stephen Henson8f122962017-03-03 02:44:18 +00001544 if (size == 0 || (size & 1) != 0)
Matt Caswell703bcee2016-12-14 14:31:21 +00001545 return 0;
1546
1547 size >>= 1;
1548
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001549 buf = OPENSSL_malloc(size * sizeof(*buf));
1550 if (buf == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00001551 return 0;
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001552 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001553 buf[i] = stmp;
Matt Caswell703bcee2016-12-14 14:31:21 +00001554
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001555 if (i != size) {
1556 OPENSSL_free(buf);
Matt Caswell703bcee2016-12-14 14:31:21 +00001557 return 0;
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001558 }
1559
1560 OPENSSL_free(*pdest);
1561 *pdest = buf;
1562 *pdestlen = size;
Matt Caswell703bcee2016-12-14 14:31:21 +00001563
Matt Caswell0f113f32015-01-22 03:40:55 +00001564 return 1;
1565}
Dr. Stephen Henson6b7be582011-05-06 13:00:07 +00001566
Dr. Stephen Henson9e84a422017-09-22 16:06:52 +01001567int tls1_save_sigalgs(SSL *s, PACKET *pkt)
1568{
1569 /* Extension ignored for inappropriate versions */
1570 if (!SSL_USE_SIGALGS(s))
1571 return 1;
1572 /* Should never happen */
1573 if (s->cert == NULL)
1574 return 0;
1575
1576 return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs,
1577 &s->s3->tmp.peer_sigalgslen);
1578
1579 return 1;
1580}
1581
1582/* Set preferred digest for each key type */
1583
Dr. Stephen Hensonc800c272014-10-09 20:37:27 +01001584int tls1_process_sigalgs(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00001585{
Matt Caswell0f113f32015-01-22 03:40:55 +00001586 size_t i;
Dr. Stephen Hensonf7d53482015-07-14 23:19:11 +01001587 uint32_t *pvalid = s->s3->tmp.valid_flags;
Matt Caswell0f113f32015-01-22 03:40:55 +00001588 CERT *c = s->cert;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001589
Matt Caswell0f113f32015-01-22 03:40:55 +00001590 if (!tls1_set_shared_sigalgs(s))
1591 return 0;
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001592
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001593 for (i = 0; i < SSL_PKEY_NUM; i++)
1594 pvalid[i] = 0;
1595
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001596 for (i = 0; i < c->shared_sigalgslen; i++) {
1597 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001598 int idx = sigptr->sig_idx;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001599
Matt Caswell523fb322016-12-30 14:08:19 +00001600 /* Ignore PKCS1 based sig algs in TLSv1.3 */
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001601 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
Matt Caswell523fb322016-12-30 14:08:19 +00001602 continue;
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001603 /* If not disabled indicate we can explicitly sign */
Dr. Stephen Hensonb8858ae2017-06-28 17:56:45 +01001604 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
1605 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
Matt Caswell0f113f32015-01-22 03:40:55 +00001606 }
1607 return 1;
1608}
Dr. Stephen Henson48175042011-12-31 22:59:57 +00001609
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00001610int SSL_get_sigalgs(SSL *s, int idx,
Matt Caswell0f113f32015-01-22 03:40:55 +00001611 int *psign, int *phash, int *psignhash,
1612 unsigned char *rsig, unsigned char *rhash)
1613{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001614 uint16_t *psig = s->s3->tmp.peer_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +00001615 size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
Matt Caswell348240c2016-10-19 15:11:24 +01001616 if (psig == NULL || numsigalgs > INT_MAX)
Matt Caswell0f113f32015-01-22 03:40:55 +00001617 return 0;
1618 if (idx >= 0) {
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001619 const SIGALG_LOOKUP *lu;
1620
Matt Caswell703bcee2016-12-14 14:31:21 +00001621 if (idx >= (int)numsigalgs)
Matt Caswell0f113f32015-01-22 03:40:55 +00001622 return 0;
1623 psig += idx;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001624 if (rhash != NULL)
Matt Caswell536199e2016-12-14 16:37:48 +00001625 *rhash = (unsigned char)((*psig >> 8) & 0xff);
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001626 if (rsig != NULL)
Matt Caswell536199e2016-12-14 16:37:48 +00001627 *rsig = (unsigned char)(*psig & 0xff);
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001628 lu = tls1_lookup_sigalg(*psig);
1629 if (psign != NULL)
1630 *psign = lu != NULL ? lu->sig : NID_undef;
1631 if (phash != NULL)
1632 *phash = lu != NULL ? lu->hash : NID_undef;
1633 if (psignhash != NULL)
1634 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
Matt Caswell0f113f32015-01-22 03:40:55 +00001635 }
Matt Caswell348240c2016-10-19 15:11:24 +01001636 return (int)numsigalgs;
Matt Caswell0f113f32015-01-22 03:40:55 +00001637}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001638
1639int SSL_get_shared_sigalgs(SSL *s, int idx,
Matt Caswell0f113f32015-01-22 03:40:55 +00001640 int *psign, int *phash, int *psignhash,
1641 unsigned char *rsig, unsigned char *rhash)
1642{
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001643 const SIGALG_LOOKUP *shsigalgs;
1644 if (s->cert->shared_sigalgs == NULL
Peter Wu6d047e02017-02-02 12:11:10 +01001645 || idx < 0
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001646 || idx >= (int)s->cert->shared_sigalgslen
1647 || s->cert->shared_sigalgslen > INT_MAX)
Matt Caswell0f113f32015-01-22 03:40:55 +00001648 return 0;
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001649 shsigalgs = s->cert->shared_sigalgs[idx];
1650 if (phash != NULL)
1651 *phash = shsigalgs->hash;
1652 if (psign != NULL)
1653 *psign = shsigalgs->sig;
1654 if (psignhash != NULL)
1655 *psignhash = shsigalgs->sigandhash;
1656 if (rsig != NULL)
1657 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
1658 if (rhash != NULL)
1659 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
Matt Caswell348240c2016-10-19 15:11:24 +01001660 return (int)s->cert->shared_sigalgslen;
Matt Caswell0f113f32015-01-22 03:40:55 +00001661}
Dr. Stephen Hensone7f8ff42012-03-06 14:28:21 +00001662
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001663/* Maximum possible number of unique entries in sigalgs array */
1664#define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
Matt Caswell0f113f32015-01-22 03:40:55 +00001665
1666typedef struct {
1667 size_t sigalgcnt;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001668 int sigalgs[TLS_MAX_SIGALGCNT];
Matt Caswell0f113f32015-01-22 03:40:55 +00001669} sig_cb_st;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001670
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001671static void get_sigorhash(int *psig, int *phash, const char *str)
1672{
1673 if (strcmp(str, "RSA") == 0) {
1674 *psig = EVP_PKEY_RSA;
Dr. Stephen Hensonb2eb6992017-01-25 16:22:13 +00001675 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
1676 *psig = EVP_PKEY_RSA_PSS;
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001677 } else if (strcmp(str, "DSA") == 0) {
1678 *psig = EVP_PKEY_DSA;
1679 } else if (strcmp(str, "ECDSA") == 0) {
1680 *psig = EVP_PKEY_EC;
1681 } else {
1682 *phash = OBJ_sn2nid(str);
1683 if (*phash == NID_undef)
1684 *phash = OBJ_ln2nid(str);
1685 }
1686}
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001687/* Maximum length of a signature algorithm string component */
1688#define TLS_MAX_SIGSTRING_LEN 40
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001689
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001690static int sig_cb(const char *elem, int len, void *arg)
Matt Caswell0f113f32015-01-22 03:40:55 +00001691{
1692 sig_cb_st *sarg = arg;
1693 size_t i;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001694 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
Dr. Stephen Henson431f4582015-07-23 14:57:42 +01001695 int sig_alg = NID_undef, hash_alg = NID_undef;
Kurt Roeckx2747d732015-01-24 14:46:50 +01001696 if (elem == NULL)
1697 return 0;
Dr. Stephen Henson787ebca2017-01-29 15:12:58 +00001698 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
Matt Caswell0f113f32015-01-22 03:40:55 +00001699 return 0;
1700 if (len > (int)(sizeof(etmp) - 1))
1701 return 0;
1702 memcpy(etmp, elem, len);
1703 etmp[len] = 0;
1704 p = strchr(etmp, '+');
Dr. Stephen Henson8a43a422017-01-26 14:40:59 +00001705 /* See if we have a match for TLS 1.3 names */
1706 if (p == NULL) {
1707 const SIGALG_LOOKUP *s;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001708
Dr. Stephen Henson8a43a422017-01-26 14:40:59 +00001709 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1710 i++, s++) {
1711 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
1712 sig_alg = s->sig;
1713 hash_alg = s->hash;
1714 break;
1715 }
1716 }
1717 } else {
1718 *p = 0;
1719 p++;
1720 if (*p == 0)
1721 return 0;
1722 get_sigorhash(&sig_alg, &hash_alg, etmp);
1723 get_sigorhash(&sig_alg, &hash_alg, p);
1724 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001725
Dr. Stephen Henson168067b2017-06-14 16:54:08 +01001726 if (sig_alg == NID_undef || (p != NULL && hash_alg == NID_undef))
Matt Caswell0f113f32015-01-22 03:40:55 +00001727 return 0;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001728
Matt Caswell0f113f32015-01-22 03:40:55 +00001729 for (i = 0; i < sarg->sigalgcnt; i += 2) {
1730 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
1731 return 0;
1732 }
1733 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
1734 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
1735 return 1;
1736}
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001737
Matt Caswell0f113f32015-01-22 03:40:55 +00001738/*
FdaSilvaYY9d226662016-06-28 22:39:55 +02001739 * Set supported signature algorithms based on a colon separated list of the
Matt Caswell0f113f32015-01-22 03:40:55 +00001740 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1741 */
Dr. Stephen Henson3dbc46d2012-07-03 12:51:14 +00001742int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
Matt Caswell0f113f32015-01-22 03:40:55 +00001743{
1744 sig_cb_st sig;
1745 sig.sigalgcnt = 0;
1746 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
1747 return 0;
1748 if (c == NULL)
1749 return 1;
1750 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
1751}
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001752
Emilia Kaspera230b262016-08-05 19:03:17 +02001753int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
Matt Caswell0f113f32015-01-22 03:40:55 +00001754{
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001755 uint16_t *sigalgs, *sptr;
Matt Caswell0f113f32015-01-22 03:40:55 +00001756 size_t i;
Matt Caswell63c1df02016-12-14 16:50:14 +00001757
Matt Caswell0f113f32015-01-22 03:40:55 +00001758 if (salglen & 1)
1759 return 0;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001760 sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs));
Matt Caswell0f113f32015-01-22 03:40:55 +00001761 if (sigalgs == NULL)
1762 return 0;
1763 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
Matt Caswell63c1df02016-12-14 16:50:14 +00001764 size_t j;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001765 const SIGALG_LOOKUP *curr;
Matt Caswell63c1df02016-12-14 16:50:14 +00001766 int md_id = *psig_nids++;
1767 int sig_id = *psig_nids++;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001768
Matt Caswell63c1df02016-12-14 16:50:14 +00001769 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
1770 j++, curr++) {
Matt Caswellfe3066e2017-01-03 10:01:39 +00001771 if (curr->hash == md_id && curr->sig == sig_id) {
Matt Caswell63c1df02016-12-14 16:50:14 +00001772 *sptr++ = curr->sigalg;
1773 break;
1774 }
1775 }
1776
1777 if (j == OSSL_NELEM(sigalg_lookup_tbl))
Matt Caswell0f113f32015-01-22 03:40:55 +00001778 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +00001779 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001780
Matt Caswell0f113f32015-01-22 03:40:55 +00001781 if (client) {
Rich Salzb548a1f2015-05-01 10:02:07 -04001782 OPENSSL_free(c->client_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +00001783 c->client_sigalgs = sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001784 c->client_sigalgslen = salglen / 2;
Matt Caswell0f113f32015-01-22 03:40:55 +00001785 } else {
Rich Salzb548a1f2015-05-01 10:02:07 -04001786 OPENSSL_free(c->conf_sigalgs);
Matt Caswell0f113f32015-01-22 03:40:55 +00001787 c->conf_sigalgs = sigalgs;
Matt Caswell7a531ee2017-01-10 14:23:02 +00001788 c->conf_sigalgslen = salglen / 2;
Matt Caswell0f113f32015-01-22 03:40:55 +00001789 }
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001790
Matt Caswell0f113f32015-01-22 03:40:55 +00001791 return 1;
Dr. Stephen Henson0f229cc2012-06-22 14:03:31 +00001792
Matt Caswell0f113f32015-01-22 03:40:55 +00001793 err:
1794 OPENSSL_free(sigalgs);
1795 return 0;
1796}
Dr. Stephen Henson4453cd82012-06-25 14:32:30 +00001797
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001798static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
Matt Caswell0f113f32015-01-22 03:40:55 +00001799{
1800 int sig_nid;
1801 size_t i;
1802 if (default_nid == -1)
1803 return 1;
1804 sig_nid = X509_get_signature_nid(x);
1805 if (default_nid)
1806 return sig_nid == default_nid ? 1 : 0;
1807 for (i = 0; i < c->shared_sigalgslen; i++)
Dr. Stephen Henson4d43ee22017-01-26 15:24:35 +00001808 if (sig_nid == c->shared_sigalgs[i]->sigandhash)
Matt Caswell0f113f32015-01-22 03:40:55 +00001809 return 1;
1810 return 0;
1811}
1812
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001813/* Check to see if a certificate issuer name matches list of CA names */
1814static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
Matt Caswell0f113f32015-01-22 03:40:55 +00001815{
1816 X509_NAME *nm;
1817 int i;
1818 nm = X509_get_issuer_name(x);
1819 for (i = 0; i < sk_X509_NAME_num(names); i++) {
1820 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
1821 return 1;
1822 }
1823 return 0;
1824}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001825
Matt Caswell0f113f32015-01-22 03:40:55 +00001826/*
1827 * Check certificate chain is consistent with TLS extensions and is usable by
1828 * server. This servers two purposes: it allows users to check chains before
1829 * passing them to the server and it allows the server to check chains before
1830 * attempting to use them.
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001831 */
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001832
FdaSilvaYY69687aa2017-03-28 23:57:28 +02001833/* Flags which need to be set for a certificate when strict mode not set */
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001834
Matt Caswelle481f9b2015-05-15 10:49:56 +01001835#define CERT_PKEY_VALID_FLAGS \
Matt Caswell0f113f32015-01-22 03:40:55 +00001836 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001837/* Strict mode flags */
Matt Caswelle481f9b2015-05-15 10:49:56 +01001838#define CERT_PKEY_STRICT_FLAGS \
Matt Caswell0f113f32015-01-22 03:40:55 +00001839 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
1840 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001841
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001842int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
Matt Caswell0f113f32015-01-22 03:40:55 +00001843 int idx)
1844{
1845 int i;
1846 int rv = 0;
1847 int check_flags = 0, strict_mode;
1848 CERT_PKEY *cpk = NULL;
1849 CERT *c = s->cert;
Dr. Stephen Hensonf7d53482015-07-14 23:19:11 +01001850 uint32_t *pvalid;
Matt Caswell0f113f32015-01-22 03:40:55 +00001851 unsigned int suiteb_flags = tls1_suiteb(s);
1852 /* idx == -1 means checking server chains */
1853 if (idx != -1) {
1854 /* idx == -2 means checking client certificate chains */
1855 if (idx == -2) {
1856 cpk = c->key;
Matt Caswell348240c2016-10-19 15:11:24 +01001857 idx = (int)(cpk - c->pkeys);
Matt Caswell0f113f32015-01-22 03:40:55 +00001858 } else
1859 cpk = c->pkeys + idx;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001860 pvalid = s->s3->tmp.valid_flags + idx;
Matt Caswell0f113f32015-01-22 03:40:55 +00001861 x = cpk->x509;
1862 pk = cpk->privatekey;
1863 chain = cpk->chain;
1864 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
1865 /* If no cert or key, forget it */
1866 if (!x || !pk)
1867 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +00001868 } else {
Dr. Stephen Henson52fd27f2017-06-29 15:20:09 +01001869 size_t certidx;
1870
Matt Caswell0f113f32015-01-22 03:40:55 +00001871 if (!x || !pk)
Matt Caswelld813f9e2015-03-11 17:01:38 +00001872 return 0;
Dr. Stephen Henson52fd27f2017-06-29 15:20:09 +01001873
1874 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
Matt Caswelld813f9e2015-03-11 17:01:38 +00001875 return 0;
Dr. Stephen Henson52fd27f2017-06-29 15:20:09 +01001876 idx = certidx;
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01001877 pvalid = s->s3->tmp.valid_flags + idx;
1878
Matt Caswell0f113f32015-01-22 03:40:55 +00001879 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
1880 check_flags = CERT_PKEY_STRICT_FLAGS;
1881 else
1882 check_flags = CERT_PKEY_VALID_FLAGS;
1883 strict_mode = 1;
1884 }
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001885
Matt Caswell0f113f32015-01-22 03:40:55 +00001886 if (suiteb_flags) {
1887 int ok;
1888 if (check_flags)
1889 check_flags |= CERT_PKEY_SUITEB;
1890 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
1891 if (ok == X509_V_OK)
1892 rv |= CERT_PKEY_SUITEB;
1893 else if (!check_flags)
1894 goto end;
1895 }
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00001896
Matt Caswell0f113f32015-01-22 03:40:55 +00001897 /*
1898 * Check all signature algorithms are consistent with signature
1899 * algorithms extension if TLS 1.2 or later and strict mode.
1900 */
1901 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
1902 int default_nid;
Matt Caswell536199e2016-12-14 16:37:48 +00001903 int rsign = 0;
Dr. Stephen Henson76106e62015-05-12 17:17:37 +01001904 if (s->s3->tmp.peer_sigalgs)
Matt Caswell0f113f32015-01-22 03:40:55 +00001905 default_nid = 0;
1906 /* If no sigalgs extension use defaults from RFC5246 */
1907 else {
1908 switch (idx) {
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00001909 case SSL_PKEY_RSA:
Matt Caswell536199e2016-12-14 16:37:48 +00001910 rsign = EVP_PKEY_RSA;
Matt Caswell0f113f32015-01-22 03:40:55 +00001911 default_nid = NID_sha1WithRSAEncryption;
1912 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001913
Matt Caswell0f113f32015-01-22 03:40:55 +00001914 case SSL_PKEY_DSA_SIGN:
Matt Caswell536199e2016-12-14 16:37:48 +00001915 rsign = EVP_PKEY_DSA;
Matt Caswell0f113f32015-01-22 03:40:55 +00001916 default_nid = NID_dsaWithSHA1;
1917 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001918
Matt Caswell0f113f32015-01-22 03:40:55 +00001919 case SSL_PKEY_ECC:
Matt Caswell536199e2016-12-14 16:37:48 +00001920 rsign = EVP_PKEY_EC;
Matt Caswell0f113f32015-01-22 03:40:55 +00001921 default_nid = NID_ecdsa_with_SHA1;
1922 break;
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00001923
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001924 case SSL_PKEY_GOST01:
Matt Caswell536199e2016-12-14 16:37:48 +00001925 rsign = NID_id_GostR3410_2001;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001926 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
1927 break;
1928
1929 case SSL_PKEY_GOST12_256:
Matt Caswell536199e2016-12-14 16:37:48 +00001930 rsign = NID_id_GostR3410_2012_256;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001931 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
1932 break;
1933
1934 case SSL_PKEY_GOST12_512:
Matt Caswell536199e2016-12-14 16:37:48 +00001935 rsign = NID_id_GostR3410_2012_512;
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00001936 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
1937 break;
1938
Matt Caswell0f113f32015-01-22 03:40:55 +00001939 default:
1940 default_nid = -1;
1941 break;
1942 }
1943 }
1944 /*
1945 * If peer sent no signature algorithms extension and we have set
1946 * preferred signature algorithms check we support sha1.
1947 */
1948 if (default_nid > 0 && c->conf_sigalgs) {
1949 size_t j;
Dr. Stephen Henson98c792d2017-01-25 14:33:55 +00001950 const uint16_t *p = c->conf_sigalgs;
Matt Caswell703bcee2016-12-14 14:31:21 +00001951 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
Dr. Stephen Henson44b63182017-01-30 17:27:35 +00001952 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
1953
1954 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
Matt Caswell0f113f32015-01-22 03:40:55 +00001955 break;
1956 }
1957 if (j == c->conf_sigalgslen) {
1958 if (check_flags)
1959 goto skip_sigs;
1960 else
1961 goto end;
1962 }
1963 }
1964 /* Check signature algorithm of each cert in chain */
1965 if (!tls1_check_sig_alg(c, x, default_nid)) {
1966 if (!check_flags)
1967 goto end;
1968 } else
1969 rv |= CERT_PKEY_EE_SIGNATURE;
1970 rv |= CERT_PKEY_CA_SIGNATURE;
1971 for (i = 0; i < sk_X509_num(chain); i++) {
1972 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
1973 if (check_flags) {
1974 rv &= ~CERT_PKEY_CA_SIGNATURE;
1975 break;
1976 } else
1977 goto end;
1978 }
1979 }
1980 }
1981 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
1982 else if (check_flags)
1983 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
1984 skip_sigs:
1985 /* Check cert parameters are consistent */
Dr. Stephen Henson9195ddc2017-02-18 03:42:15 +00001986 if (tls1_check_cert_param(s, x, 1))
Matt Caswell0f113f32015-01-22 03:40:55 +00001987 rv |= CERT_PKEY_EE_PARAM;
1988 else if (!check_flags)
1989 goto end;
1990 if (!s->server)
1991 rv |= CERT_PKEY_CA_PARAM;
1992 /* In strict mode check rest of chain too */
1993 else if (strict_mode) {
1994 rv |= CERT_PKEY_CA_PARAM;
1995 for (i = 0; i < sk_X509_num(chain); i++) {
1996 X509 *ca = sk_X509_value(chain, i);
1997 if (!tls1_check_cert_param(s, ca, 0)) {
1998 if (check_flags) {
1999 rv &= ~CERT_PKEY_CA_PARAM;
2000 break;
2001 } else
2002 goto end;
2003 }
2004 }
2005 }
2006 if (!s->server && strict_mode) {
2007 STACK_OF(X509_NAME) *ca_dn;
2008 int check_type = 0;
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +00002009 switch (EVP_PKEY_id(pk)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002010 case EVP_PKEY_RSA:
2011 check_type = TLS_CT_RSA_SIGN;
2012 break;
2013 case EVP_PKEY_DSA:
2014 check_type = TLS_CT_DSS_SIGN;
2015 break;
2016 case EVP_PKEY_EC:
2017 check_type = TLS_CT_ECDSA_SIGN;
2018 break;
Matt Caswell0f113f32015-01-22 03:40:55 +00002019 }
2020 if (check_type) {
Dr. Stephen Henson75c13e72017-02-23 22:12:28 +00002021 const uint8_t *ctypes = s->s3->tmp.ctype;
2022 size_t j;
2023
2024 for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) {
2025 if (*ctypes == check_type) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002026 rv |= CERT_PKEY_CERT_TYPE;
2027 break;
2028 }
2029 }
2030 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2031 goto end;
Dr. Stephen Henson75c13e72017-02-23 22:12:28 +00002032 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +00002033 rv |= CERT_PKEY_CERT_TYPE;
Dr. Stephen Henson75c13e72017-02-23 22:12:28 +00002034 }
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002035
Dr. Stephen Hensonfa7c2632017-03-13 15:00:36 +00002036 ca_dn = s->s3->tmp.peer_ca_names;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002037
Matt Caswell0f113f32015-01-22 03:40:55 +00002038 if (!sk_X509_NAME_num(ca_dn))
2039 rv |= CERT_PKEY_ISSUER_NAME;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002040
Matt Caswell0f113f32015-01-22 03:40:55 +00002041 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2042 if (ssl_check_ca_name(ca_dn, x))
2043 rv |= CERT_PKEY_ISSUER_NAME;
2044 }
2045 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2046 for (i = 0; i < sk_X509_num(chain); i++) {
2047 X509 *xtmp = sk_X509_value(chain, i);
2048 if (ssl_check_ca_name(ca_dn, xtmp)) {
2049 rv |= CERT_PKEY_ISSUER_NAME;
2050 break;
2051 }
2052 }
2053 }
2054 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2055 goto end;
2056 } else
2057 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002058
Matt Caswell0f113f32015-01-22 03:40:55 +00002059 if (!check_flags || (rv & check_flags) == check_flags)
2060 rv |= CERT_PKEY_VALID;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002061
Matt Caswell0f113f32015-01-22 03:40:55 +00002062 end:
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002063
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00002064 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2065 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
2066 else
Matt Caswell0f113f32015-01-22 03:40:55 +00002067 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
Dr. Stephen Henson6dbb6212012-07-27 13:39:23 +00002068
Matt Caswell0f113f32015-01-22 03:40:55 +00002069 /*
2070 * When checking a CERT_PKEY structure all flags are irrelevant if the
2071 * chain is invalid.
2072 */
2073 if (!check_flags) {
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00002074 if (rv & CERT_PKEY_VALID) {
Dr. Stephen Henson6383d312015-05-12 22:17:34 +01002075 *pvalid = rv;
Dr. Stephen Hensona8bb9122017-02-19 18:47:16 +00002076 } else {
2077 /* Preserve sign and explicit sign flag, clear rest */
2078 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
Matt Caswell0f113f32015-01-22 03:40:55 +00002079 return 0;
2080 }
2081 }
2082 return rv;
2083}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002084
2085/* Set validity of certificates in an SSL structure */
2086void tls1_set_cert_validity(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00002087{
Dr. Stephen Hensond0ff28f2017-02-10 04:23:53 +00002088 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
Dr. Stephen Henson045d0782017-09-13 13:53:03 +01002089 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
Matt Caswell17dd65e2015-03-24 15:10:15 +00002090 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
Matt Caswell17dd65e2015-03-24 15:10:15 +00002091 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
Dmitry Belyavskye44380a2015-11-17 15:32:30 +00002092 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2093 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2094 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
Dr. Stephen Henson3d234c92017-05-24 21:56:38 +01002095 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
Matt Caswell0f113f32015-01-22 03:40:55 +00002096}
2097
FdaSilvaYY69687aa2017-03-28 23:57:28 +02002098/* User level utility function to check a chain is suitable */
Dr. Stephen Henson18d71582012-06-29 14:24:42 +00002099int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
Matt Caswell0f113f32015-01-22 03:40:55 +00002100{
2101 return tls1_check_chain(s, x, pk, chain, -1);
2102}
Dr. Stephen Hensond61ff832012-06-28 12:45:49 +00002103
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002104#ifndef OPENSSL_NO_DH
2105DH *ssl_get_auto_dh(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +00002106{
2107 int dh_secbits = 80;
2108 if (s->cert->dh_tmp_auto == 2)
2109 return DH_get_1024_160();
Dr. Stephen Hensonadc55062015-06-28 17:01:52 +01002110 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002111 if (s->s3->tmp.new_cipher->strength_bits == 256)
2112 dh_secbits = 128;
2113 else
2114 dh_secbits = 80;
2115 } else {
Dr. Stephen Hensona497cf22017-02-14 00:35:26 +00002116 if (s->s3->tmp.cert == NULL)
Dr. Stephen Hensonf365a3e2017-02-13 16:32:06 +00002117 return NULL;
Dr. Stephen Hensona497cf22017-02-14 00:35:26 +00002118 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
Matt Caswell0f113f32015-01-22 03:40:55 +00002119 }
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002120
Matt Caswell0f113f32015-01-22 03:40:55 +00002121 if (dh_secbits >= 128) {
2122 DH *dhp = DH_new();
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002123 BIGNUM *p, *g;
Matt Caswella71edf32015-10-30 10:05:53 +00002124 if (dhp == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +00002125 return NULL;
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002126 g = BN_new();
2127 if (g != NULL)
2128 BN_set_word(g, 2);
Matt Caswell0f113f32015-01-22 03:40:55 +00002129 if (dh_secbits >= 192)
Rich Salz9021a5d2016-04-18 07:43:54 -04002130 p = BN_get_rfc3526_prime_8192(NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +00002131 else
Rich Salz9021a5d2016-04-18 07:43:54 -04002132 p = BN_get_rfc3526_prime_3072(NULL);
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002133 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
Matt Caswell0f113f32015-01-22 03:40:55 +00002134 DH_free(dhp);
Matt Caswell0aeddcf2016-04-06 17:49:48 +01002135 BN_free(p);
2136 BN_free(g);
Matt Caswell0f113f32015-01-22 03:40:55 +00002137 return NULL;
2138 }
2139 return dhp;
2140 }
2141 if (dh_secbits >= 112)
2142 return DH_get_2048_224();
2143 return DH_get_1024_160();
2144}
Dr. Stephen Henson09599b52014-01-22 16:22:48 +00002145#endif
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002146
2147static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00002148{
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002149 int secbits = -1;
Dr. Stephen Henson8382fd32015-12-20 00:32:36 +00002150 EVP_PKEY *pkey = X509_get0_pubkey(x);
Matt Caswell0f113f32015-01-22 03:40:55 +00002151 if (pkey) {
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002152 /*
2153 * If no parameters this will return -1 and fail using the default
2154 * security callback for any non-zero security level. This will
2155 * reject keys which omit parameters but this only affects DSA and
2156 * omission of parameters is never (?) done in practice.
2157 */
Matt Caswell0f113f32015-01-22 03:40:55 +00002158 secbits = EVP_PKEY_security_bits(pkey);
Dr. Stephen Henson72245f32015-12-30 13:34:53 +00002159 }
Matt Caswell0f113f32015-01-22 03:40:55 +00002160 if (s)
2161 return ssl_security(s, op, secbits, 0, x);
2162 else
2163 return ssl_ctx_security(ctx, op, secbits, 0, x);
2164}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002165
2166static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
Matt Caswell0f113f32015-01-22 03:40:55 +00002167{
2168 /* Lookup signature algorithm digest */
Dr. Stephen Henson65e89732017-05-24 22:01:00 +01002169 int secbits, nid, pknid;
Dr. Stephen Henson221c7b52016-02-11 15:25:11 +00002170 /* Don't check signature if self signed */
2171 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2172 return 1;
Dr. Stephen Henson65e89732017-05-24 22:01:00 +01002173 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
2174 secbits = -1;
2175 /* If digest NID not defined use signature NID */
2176 if (nid == NID_undef)
2177 nid = pknid;
Matt Caswell0f113f32015-01-22 03:40:55 +00002178 if (s)
Dr. Stephen Henson65e89732017-05-24 22:01:00 +01002179 return ssl_security(s, op, secbits, nid, x);
Matt Caswell0f113f32015-01-22 03:40:55 +00002180 else
Dr. Stephen Henson65e89732017-05-24 22:01:00 +01002181 return ssl_ctx_security(ctx, op, secbits, nid, x);
Matt Caswell0f113f32015-01-22 03:40:55 +00002182}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002183
2184int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
Matt Caswell0f113f32015-01-22 03:40:55 +00002185{
2186 if (vfy)
2187 vfy = SSL_SECOP_PEER;
2188 if (is_ee) {
2189 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2190 return SSL_R_EE_KEY_TOO_SMALL;
2191 } else {
2192 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2193 return SSL_R_CA_KEY_TOO_SMALL;
2194 }
2195 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2196 return SSL_R_CA_MD_TOO_WEAK;
2197 return 1;
2198}
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002199
Matt Caswell0f113f32015-01-22 03:40:55 +00002200/*
FdaSilvaYY69687aa2017-03-28 23:57:28 +02002201 * Check security of a chain, if |sk| includes the end entity certificate then
2202 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
Matt Caswell0f113f32015-01-22 03:40:55 +00002203 * one to the peer. Return values: 1 if ok otherwise error code to use
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002204 */
2205
2206int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
Matt Caswell0f113f32015-01-22 03:40:55 +00002207{
2208 int rv, start_idx, i;
2209 if (x == NULL) {
2210 x = sk_X509_value(sk, 0);
2211 start_idx = 1;
2212 } else
2213 start_idx = 0;
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002214
Matt Caswell0f113f32015-01-22 03:40:55 +00002215 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2216 if (rv != 1)
2217 return rv;
Dr. Stephen Hensonb362cca2013-12-15 13:32:24 +00002218
Matt Caswell0f113f32015-01-22 03:40:55 +00002219 for (i = start_idx; i < sk_X509_num(sk); i++) {
2220 x = sk_X509_value(sk, i);
2221 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2222 if (rv != 1)
2223 return rv;
2224 }
2225 return 1;
2226}
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002227
2228/*
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002229 * For TLS 1.2 servers check if we have a certificate which can be used
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002230 * with the signature algorithm "lu" and return index of certificate.
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002231 */
2232
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002233static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002234{
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002235 int sig_idx = lu->sig_idx;
2236 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002237
2238 /* If not recognised or not supported by cipher mask it is not suitable */
2239 if (clu == NULL || !(clu->amask & s->s3->tmp.new_cipher->algorithm_auth))
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002240 return -1;
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002241
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002242 /* If PSS and we have no PSS cert use RSA */
2243 if (sig_idx == SSL_PKEY_RSA_PSS_SIGN && !ssl_has_cert(s, sig_idx))
2244 sig_idx = SSL_PKEY_RSA;
2245
2246 return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002247}
2248
2249/*
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002250 * Choose an appropriate signature algorithm based on available certificates
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002251 * Sets chosen certificate and signature algorithm.
2252 *
2253 * For servers if we fail to find a required certificate it is a fatal error
2254 * and an appropriate error code is set and the TLS alert set in *al.
2255 *
2256 * For clients al is set to NULL. If a certificate is not suitable it is not
2257 * a fatal error: we will either try another certificate or not present one
2258 * to the server. In this case no error is set.
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002259 */
Dr. Stephen Henson4a419f62017-02-13 15:50:43 +00002260int tls_choose_sigalg(SSL *s, int *al)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002261{
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002262 const SIGALG_LOOKUP *lu = NULL;
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002263 int sig_idx = -1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002264
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002265 s->s3->tmp.cert = NULL;
2266 s->s3->tmp.sigalg = NULL;
2267
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002268 if (SSL_IS_TLS13(s)) {
2269 size_t i;
Richard Levitte21f198e2017-02-03 13:56:54 +01002270#ifndef OPENSSL_NO_EC
Dr. Stephen Hensone892e322017-02-24 20:43:02 +00002271 int curve = -1, skip_ec = 0;
Richard Levitte21f198e2017-02-03 13:56:54 +01002272#endif
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002273
FdaSilvaYY69687aa2017-03-28 23:57:28 +02002274 /* Look for a certificate matching shared sigalgs */
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002275 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002276 lu = s->cert->shared_sigalgs[i];
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002277
Matt Caswelld8311fc2017-06-08 17:15:45 +01002278 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2279 if (lu->hash == NID_sha1
2280 || lu->hash == NID_sha224
2281 || lu->sig == EVP_PKEY_DSA
Dr. Stephen Henson095a9822017-02-25 00:25:04 +00002282 || lu->sig == EVP_PKEY_RSA)
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002283 continue;
Dr. Stephen Henson168067b2017-06-14 16:54:08 +01002284 if (!tls1_lookup_md(lu, NULL))
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002285 continue;
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002286 if (!ssl_has_cert(s, lu->sig_idx)) {
2287 if (lu->sig_idx != SSL_PKEY_RSA_PSS_SIGN
2288 || !ssl_has_cert(s, SSL_PKEY_RSA))
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002289 continue;
Patrick Steuerb9ff0482017-09-21 00:10:16 +02002290 sig_idx = SSL_PKEY_RSA;
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002291 }
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002292 if (lu->sig == EVP_PKEY_EC) {
Richard Levitte21f198e2017-02-03 13:56:54 +01002293#ifndef OPENSSL_NO_EC
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002294 if (curve == -1) {
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002295 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002296
2297 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
Dr. Stephen Hensone892e322017-02-24 20:43:02 +00002298 if (EC_KEY_get_conv_form(ec)
2299 != POINT_CONVERSION_UNCOMPRESSED)
2300 skip_ec = 1;
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002301 }
Dr. Stephen Hensone892e322017-02-24 20:43:02 +00002302 if (skip_ec || (lu->curve != NID_undef && curve != lu->curve))
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002303 continue;
Richard Levitte21f198e2017-02-03 13:56:54 +01002304#else
2305 continue;
2306#endif
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002307 }
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002308 break;
2309 }
2310 if (i == s->cert->shared_sigalgslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002311 if (al == NULL)
2312 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002313 *al = SSL_AD_HANDSHAKE_FAILURE;
2314 SSLerr(SSL_F_TLS_CHOOSE_SIGALG,
2315 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2316 return 0;
2317 }
2318 } else {
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002319 /* If ciphersuite doesn't require a cert nothing to do */
2320 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT))
2321 return 1;
2322 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002323 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002324
2325 if (SSL_USE_SIGALGS(s)) {
2326 if (s->s3->tmp.peer_sigalgs != NULL) {
2327 size_t i;
Dr. Stephen Henson599b5862017-02-24 16:39:57 +00002328#ifndef OPENSSL_NO_EC
2329 int curve;
2330
2331 /* For Suite B need to match signature algorithm to curve */
2332 if (tls1_suiteb(s)) {
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002333 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
Dr. Stephen Henson599b5862017-02-24 16:39:57 +00002334 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2335 } else {
2336 curve = -1;
2337 }
2338#endif
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002339
2340 /*
2341 * Find highest preference signature algorithm matching
2342 * cert type
2343 */
2344 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2345 lu = s->cert->shared_sigalgs[i];
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002346
2347 if (s->server) {
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002348 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002349 continue;
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002350 } else {
2351 int cc_idx = s->cert->key - s->cert->pkeys;
2352
2353 sig_idx = lu->sig_idx;
2354 if (cc_idx != sig_idx) {
2355 if (sig_idx != SSL_PKEY_RSA_PSS_SIGN
2356 || cc_idx != SSL_PKEY_RSA)
2357 continue;
2358 sig_idx = SSL_PKEY_RSA;
2359 }
Dr. Stephen Hensonb2021552017-06-16 18:55:28 +01002360 }
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002361#ifndef OPENSSL_NO_EC
2362 if (curve == -1 || lu->curve == curve)
Dr. Stephen Henson599b5862017-02-24 16:39:57 +00002363#endif
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002364 break;
2365 }
2366 if (i == s->cert->shared_sigalgslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002367 if (al == NULL)
2368 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002369 *al = SSL_AD_INTERNAL_ERROR;
2370 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2371 return 0;
2372 }
2373 } else {
2374 /*
2375 * If we have no sigalg use defaults
2376 */
2377 const uint16_t *sent_sigs;
2378 size_t sent_sigslen, i;
2379
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002380 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002381 if (al == NULL)
2382 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002383 *al = SSL_AD_INTERNAL_ERROR;
2384 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2385 return 0;
2386 }
2387
2388 /* Check signature matches a type we sent */
2389 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2390 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2391 if (lu->sigalg == *sent_sigs)
2392 break;
2393 }
2394 if (i == sent_sigslen) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002395 if (al == NULL)
2396 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002397 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
Matt Caswellfb34a0f2017-05-16 17:28:23 +01002398 *al = SSL_AD_ILLEGAL_PARAMETER;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002399 return 0;
2400 }
2401 }
2402 } else {
Dr. Stephen Henson7f6b4662017-06-29 14:55:06 +01002403 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
Dr. Stephen Henson717a2652017-02-15 16:19:43 +00002404 if (al == NULL)
2405 return 1;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002406 *al = SSL_AD_INTERNAL_ERROR;
2407 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2408 return 0;
2409 }
2410 }
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002411 }
Dr. Stephen Hensonb46867d2017-09-14 14:53:52 +01002412 if (sig_idx == -1)
2413 sig_idx = lu->sig_idx;
2414 s->s3->tmp.cert = &s->cert->pkeys[sig_idx];
Dr. Stephen Henson59088e42017-02-15 15:28:56 +00002415 s->cert->key = s->s3->tmp.cert;
Dr. Stephen Henson0972bc52017-02-13 16:04:07 +00002416 s->s3->tmp.sigalg = lu;
Dr. Stephen Henson93a77f92017-01-31 17:45:00 +00002417 return 1;
2418}