| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// Copyright 2005 Nokia. All rights reserved. |
| 5 |
// |
| 6 |
// ECC cipher suite support in OpenSSL originally developed by |
| 7 |
// SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 8 |
// |
| 9 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 10 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 11 |
// support (see RFC 4279) to OpenSSL. |
| 12 |
// |
| 13 |
// SPDX-License-Identifier: Apache-2.0 |
| 14 |
|
| 15 |
#include <openssl/ssl.h> |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include <openssl/err.h> |
| 21 |
#include <openssl/md5.h> |
| 22 |
#include <openssl/mem.h> |
| 23 |
#include <openssl/sha.h> |
| 24 |
#include <openssl/stack.h> |
| 25 |
|
| 26 |
#include "internal.h" |
| 27 |
#include "../crypto/internal.h" |
| 28 |
|
| 29 |
|
| 30 |
BSSL_NAMESPACE_BEGIN |
| 31 |
|
| 32 |
static constexpr SSL_CIPHER kCiphers[] = { |
| 33 |
// The RSA ciphers |
| 34 |
// Cipher 02 |
| 35 |
{ |
| 36 |
SSL3_TXT_RSA_NULL_SHA, |
| 37 |
"TLS_RSA_WITH_NULL_SHA", |
| 38 |
SSL3_CK_RSA_NULL_SHA, |
| 39 |
SSL_kRSA, |
| 40 |
SSL_aRSA, |
| 41 |
SSL_eNULL, |
| 42 |
SSL_SHA1, |
| 43 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 44 |
}, |
| 45 |
|
| 46 |
// Cipher 0A |
| 47 |
{ |
| 48 |
SSL3_TXT_RSA_DES_192_CBC3_SHA, |
| 49 |
"TLS_RSA_WITH_3DES_EDE_CBC_SHA", |
| 50 |
SSL3_CK_RSA_DES_192_CBC3_SHA, |
| 51 |
SSL_kRSA, |
| 52 |
SSL_aRSA, |
| 53 |
SSL_3DES, |
| 54 |
SSL_SHA1, |
| 55 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 56 |
}, |
| 57 |
|
| 58 |
|
| 59 |
// New AES ciphersuites |
| 60 |
|
| 61 |
// Cipher 2F |
| 62 |
{ |
| 63 |
TLS1_TXT_RSA_WITH_AES_128_SHA, |
| 64 |
"TLS_RSA_WITH_AES_128_CBC_SHA", |
| 65 |
TLS1_CK_RSA_WITH_AES_128_SHA, |
| 66 |
SSL_kRSA, |
| 67 |
SSL_aRSA, |
| 68 |
SSL_AES128, |
| 69 |
SSL_SHA1, |
| 70 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 71 |
}, |
| 72 |
|
| 73 |
// Cipher 35 |
| 74 |
{ |
| 75 |
TLS1_TXT_RSA_WITH_AES_256_SHA, |
| 76 |
"TLS_RSA_WITH_AES_256_CBC_SHA", |
| 77 |
TLS1_CK_RSA_WITH_AES_256_SHA, |
| 78 |
SSL_kRSA, |
| 79 |
SSL_aRSA, |
| 80 |
SSL_AES256, |
| 81 |
SSL_SHA1, |
| 82 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 83 |
}, |
| 84 |
|
| 85 |
// Cipher 3C |
| 86 |
{ |
| 87 |
TLS1_TXT_RSA_WITH_AES_128_SHA256, |
| 88 |
"TLS_RSA_WITH_AES_128_CBC_SHA256", |
| 89 |
TLS1_CK_RSA_WITH_AES_128_SHA256, |
| 90 |
SSL_kRSA, |
| 91 |
SSL_aRSA, |
| 92 |
SSL_AES128, |
| 93 |
SSL_SHA256, |
| 94 |
SSL_HANDSHAKE_MAC_SHA256, |
| 95 |
}, |
| 96 |
|
| 97 |
// PSK cipher suites. |
| 98 |
|
| 99 |
// Cipher 8C |
| 100 |
{ |
| 101 |
TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, |
| 102 |
"TLS_PSK_WITH_AES_128_CBC_SHA", |
| 103 |
TLS1_CK_PSK_WITH_AES_128_CBC_SHA, |
| 104 |
SSL_kPSK, |
| 105 |
SSL_aPSK, |
| 106 |
SSL_AES128, |
| 107 |
SSL_SHA1, |
| 108 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 109 |
}, |
| 110 |
|
| 111 |
// Cipher 8D |
| 112 |
{ |
| 113 |
TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, |
| 114 |
"TLS_PSK_WITH_AES_256_CBC_SHA", |
| 115 |
TLS1_CK_PSK_WITH_AES_256_CBC_SHA, |
| 116 |
SSL_kPSK, |
| 117 |
SSL_aPSK, |
| 118 |
SSL_AES256, |
| 119 |
SSL_SHA1, |
| 120 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 121 |
}, |
| 122 |
|
| 123 |
// GCM ciphersuites from RFC 5288 |
| 124 |
|
| 125 |
// Cipher 9C |
| 126 |
{ |
| 127 |
TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256, |
| 128 |
"TLS_RSA_WITH_AES_128_GCM_SHA256", |
| 129 |
TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, |
| 130 |
SSL_kRSA, |
| 131 |
SSL_aRSA, |
| 132 |
SSL_AES128GCM, |
| 133 |
SSL_AEAD, |
| 134 |
SSL_HANDSHAKE_MAC_SHA256, |
| 135 |
}, |
| 136 |
|
| 137 |
// Cipher 9D |
| 138 |
{ |
| 139 |
TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384, |
| 140 |
"TLS_RSA_WITH_AES_256_GCM_SHA384", |
| 141 |
TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, |
| 142 |
SSL_kRSA, |
| 143 |
SSL_aRSA, |
| 144 |
SSL_AES256GCM, |
| 145 |
SSL_AEAD, |
| 146 |
SSL_HANDSHAKE_MAC_SHA384, |
| 147 |
}, |
| 148 |
|
| 149 |
// TLS 1.3 suites. |
| 150 |
|
| 151 |
// Cipher 1301 |
| 152 |
{ |
| 153 |
TLS1_3_RFC_AES_128_GCM_SHA256, |
| 154 |
"TLS_AES_128_GCM_SHA256", |
| 155 |
TLS1_3_CK_AES_128_GCM_SHA256, |
| 156 |
SSL_kGENERIC, |
| 157 |
SSL_aGENERIC, |
| 158 |
SSL_AES128GCM, |
| 159 |
SSL_AEAD, |
| 160 |
SSL_HANDSHAKE_MAC_SHA256, |
| 161 |
}, |
| 162 |
|
| 163 |
// Cipher 1302 |
| 164 |
{ |
| 165 |
TLS1_3_RFC_AES_256_GCM_SHA384, |
| 166 |
"TLS_AES_256_GCM_SHA384", |
| 167 |
TLS1_3_CK_AES_256_GCM_SHA384, |
| 168 |
SSL_kGENERIC, |
| 169 |
SSL_aGENERIC, |
| 170 |
SSL_AES256GCM, |
| 171 |
SSL_AEAD, |
| 172 |
SSL_HANDSHAKE_MAC_SHA384, |
| 173 |
}, |
| 174 |
|
| 175 |
// Cipher 1303 |
| 176 |
{ |
| 177 |
TLS1_3_RFC_CHACHA20_POLY1305_SHA256, |
| 178 |
"TLS_CHACHA20_POLY1305_SHA256", |
| 179 |
TLS1_3_CK_CHACHA20_POLY1305_SHA256, |
| 180 |
SSL_kGENERIC, |
| 181 |
SSL_aGENERIC, |
| 182 |
SSL_CHACHA20POLY1305, |
| 183 |
SSL_AEAD, |
| 184 |
SSL_HANDSHAKE_MAC_SHA256, |
| 185 |
}, |
| 186 |
|
| 187 |
// Cipher C009 |
| 188 |
{ |
| 189 |
TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
| 190 |
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", |
| 191 |
TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
| 192 |
SSL_kECDHE, |
| 193 |
SSL_aECDSA, |
| 194 |
SSL_AES128, |
| 195 |
SSL_SHA1, |
| 196 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 197 |
}, |
| 198 |
|
| 199 |
// Cipher C00A |
| 200 |
{ |
| 201 |
TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
| 202 |
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", |
| 203 |
TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
| 204 |
SSL_kECDHE, |
| 205 |
SSL_aECDSA, |
| 206 |
SSL_AES256, |
| 207 |
SSL_SHA1, |
| 208 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 209 |
}, |
| 210 |
|
| 211 |
// Cipher C013 |
| 212 |
{ |
| 213 |
TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
| 214 |
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
| 215 |
TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
| 216 |
SSL_kECDHE, |
| 217 |
SSL_aRSA, |
| 218 |
SSL_AES128, |
| 219 |
SSL_SHA1, |
| 220 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 221 |
}, |
| 222 |
|
| 223 |
// Cipher C014 |
| 224 |
{ |
| 225 |
TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
| 226 |
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", |
| 227 |
TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
| 228 |
SSL_kECDHE, |
| 229 |
SSL_aRSA, |
| 230 |
SSL_AES256, |
| 231 |
SSL_SHA1, |
| 232 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 233 |
}, |
| 234 |
|
| 235 |
// Cipher C027 |
| 236 |
{ |
| 237 |
TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256, |
| 238 |
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", |
| 239 |
TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
| 240 |
SSL_kECDHE, |
| 241 |
SSL_aRSA, |
| 242 |
SSL_AES128, |
| 243 |
SSL_SHA256, |
| 244 |
SSL_HANDSHAKE_MAC_SHA256, |
| 245 |
}, |
| 246 |
|
| 247 |
// Cipher C028 |
| 248 |
{ |
| 249 |
TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384, |
| 250 |
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", |
| 251 |
TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
| 252 |
SSL_kECDHE, |
| 253 |
SSL_aRSA, |
| 254 |
SSL_AES256, |
| 255 |
SSL_SHA384, |
| 256 |
SSL_HANDSHAKE_MAC_SHA384, |
| 257 |
}, |
| 258 |
|
| 259 |
// GCM based TLS v1.2 ciphersuites from RFC 5289 |
| 260 |
|
| 261 |
// Cipher C02B |
| 262 |
{ |
| 263 |
TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 264 |
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", |
| 265 |
TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 266 |
SSL_kECDHE, |
| 267 |
SSL_aECDSA, |
| 268 |
SSL_AES128GCM, |
| 269 |
SSL_AEAD, |
| 270 |
SSL_HANDSHAKE_MAC_SHA256, |
| 271 |
}, |
| 272 |
|
| 273 |
// Cipher C02C |
| 274 |
{ |
| 275 |
TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 276 |
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", |
| 277 |
TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 278 |
SSL_kECDHE, |
| 279 |
SSL_aECDSA, |
| 280 |
SSL_AES256GCM, |
| 281 |
SSL_AEAD, |
| 282 |
SSL_HANDSHAKE_MAC_SHA384, |
| 283 |
}, |
| 284 |
|
| 285 |
// Cipher C02F |
| 286 |
{ |
| 287 |
TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 288 |
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", |
| 289 |
TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 290 |
SSL_kECDHE, |
| 291 |
SSL_aRSA, |
| 292 |
SSL_AES128GCM, |
| 293 |
SSL_AEAD, |
| 294 |
SSL_HANDSHAKE_MAC_SHA256, |
| 295 |
}, |
| 296 |
|
| 297 |
// Cipher C030 |
| 298 |
{ |
| 299 |
TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 300 |
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", |
| 301 |
TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 302 |
SSL_kECDHE, |
| 303 |
SSL_aRSA, |
| 304 |
SSL_AES256GCM, |
| 305 |
SSL_AEAD, |
| 306 |
SSL_HANDSHAKE_MAC_SHA384, |
| 307 |
}, |
| 308 |
|
| 309 |
// ECDHE-PSK cipher suites. |
| 310 |
|
| 311 |
// Cipher C035 |
| 312 |
{ |
| 313 |
TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 314 |
"TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA", |
| 315 |
TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 316 |
SSL_kECDHE, |
| 317 |
SSL_aPSK, |
| 318 |
SSL_AES128, |
| 319 |
SSL_SHA1, |
| 320 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 321 |
}, |
| 322 |
|
| 323 |
// Cipher C036 |
| 324 |
{ |
| 325 |
TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
| 326 |
"TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA", |
| 327 |
TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
| 328 |
SSL_kECDHE, |
| 329 |
SSL_aPSK, |
| 330 |
SSL_AES256, |
| 331 |
SSL_SHA1, |
| 332 |
SSL_HANDSHAKE_MAC_DEFAULT, |
| 333 |
}, |
| 334 |
|
| 335 |
// ChaCha20-Poly1305 cipher suites. |
| 336 |
|
| 337 |
// Cipher CCA8 |
| 338 |
{ |
| 339 |
TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 340 |
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", |
| 341 |
TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, |
| 342 |
SSL_kECDHE, |
| 343 |
SSL_aRSA, |
| 344 |
SSL_CHACHA20POLY1305, |
| 345 |
SSL_AEAD, |
| 346 |
SSL_HANDSHAKE_MAC_SHA256, |
| 347 |
}, |
| 348 |
|
| 349 |
// Cipher CCA9 |
| 350 |
{ |
| 351 |
TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
| 352 |
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", |
| 353 |
TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, |
| 354 |
SSL_kECDHE, |
| 355 |
SSL_aECDSA, |
| 356 |
SSL_CHACHA20POLY1305, |
| 357 |
SSL_AEAD, |
| 358 |
SSL_HANDSHAKE_MAC_SHA256, |
| 359 |
}, |
| 360 |
|
| 361 |
// Cipher CCAB |
| 362 |
{ |
| 363 |
TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, |
| 364 |
"TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256", |
| 365 |
TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, |
| 366 |
SSL_kECDHE, |
| 367 |
SSL_aPSK, |
| 368 |
SSL_CHACHA20POLY1305, |
| 369 |
SSL_AEAD, |
| 370 |
SSL_HANDSHAKE_MAC_SHA256, |
| 371 |
}, |
| 372 |
|
| 373 |
}; |
| 374 |
|
| 375 |
Span<const SSL_CIPHER> AllCiphers() { |
| 376 |
return MakeConstSpan(kCiphers, OPENSSL_ARRAY_SIZE(kCiphers)); |
| 377 |
} |
| 378 |
|
| 379 |
#define CIPHER_ADD 1 |
| 380 |
#define CIPHER_KILL 2 |
| 381 |
#define CIPHER_DEL 3 |
| 382 |
#define CIPHER_ORD 4 |
| 383 |
#define CIPHER_SPECIAL 5 |
| 384 |
|
| 385 |
typedef struct cipher_order_st { |
| 386 |
const SSL_CIPHER *cipher; |
| 387 |
bool active; |
| 388 |
bool in_group; |
| 389 |
struct cipher_order_st *next, *prev; |
| 390 |
} CIPHER_ORDER; |
| 391 |
|
| 392 |
typedef struct cipher_alias_st { |
| 393 |
// name is the name of the cipher alias. |
| 394 |
const char *name; |
| 395 |
|
| 396 |
// The following fields are bitmasks for the corresponding fields on |
| 397 |
// |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the |
| 398 |
// bit corresponding to the cipher's value is set to 1. If any bitmask is |
| 399 |
// all zeroes, the alias matches nothing. Use |~0u| for the default value. |
| 400 |
uint32_t algorithm_mkey; |
| 401 |
uint32_t algorithm_auth; |
| 402 |
uint32_t algorithm_enc; |
| 403 |
uint32_t algorithm_mac; |
| 404 |
|
| 405 |
// min_version, if non-zero, matches all ciphers which were added in that |
| 406 |
// particular protocol version. |
| 407 |
uint16_t min_version; |
| 408 |
} CIPHER_ALIAS; |
| 409 |
|
| 410 |
static const CIPHER_ALIAS kCipherAliases[] = { |
| 411 |
// "ALL" is the default rule and includes everything except: SSL_3DES and |
| 412 |
// SSL_eNULL, which must be explicitly configured. |
| 413 |
// Several "known" keyword rules map to "ALL". See |
| 414 |
// is_known_default_alias_keyword_filter_rule(). |
| 415 |
{"ALL", ~0u, ~0u, ~(SSL_3DES | SSL_eNULL), ~0u, 0}, |
| 416 |
|
| 417 |
// The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. |
| 418 |
|
| 419 |
// key exchange aliases |
| 420 |
// (some of those using only a single bit here combine |
| 421 |
// multiple key exchange algs according to the RFCs. |
| 422 |
{"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0}, |
| 423 |
|
| 424 |
{"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 425 |
{"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 426 |
{"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 427 |
|
| 428 |
{"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0}, |
| 429 |
|
| 430 |
// server authentication aliases |
| 431 |
{"aRSA", ~0u, SSL_aRSA, ~0u, ~0u, 0}, |
| 432 |
{"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
| 433 |
{"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
| 434 |
{"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0}, |
| 435 |
|
| 436 |
// aliases combining key exchange and server authentication |
| 437 |
{"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 438 |
{"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 439 |
{"RSA", SSL_kRSA, SSL_aRSA, ~0u, ~0u, 0}, |
| 440 |
{"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0}, |
| 441 |
|
| 442 |
// symmetric encryption aliases |
| 443 |
{"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0}, |
| 444 |
{"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0}, |
| 445 |
{"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0}, |
| 446 |
{"AES", ~0u, ~0u, SSL_AES, ~0u, 0}, |
| 447 |
{"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0}, |
| 448 |
{"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0}, |
| 449 |
|
| 450 |
// MAC aliases |
| 451 |
{"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0}, |
| 452 |
{"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0}, |
| 453 |
{"SHA1", ~0u, ~0u, ~0u, SSL_SHA1, 0}, |
| 454 |
{"SHA", ~0u, ~0u, ~0u, SSL_SHA1, 0}, |
| 455 |
|
| 456 |
// Legacy protocol minimum version aliases. "TLSv1" is intentionally the |
| 457 |
// same as "SSLv3". |
| 458 |
{"SSLv3", ~0u, ~0u, ~SSL_3DES, ~0u, SSL3_VERSION}, |
| 459 |
{"TLSv1", ~0u, ~0u, ~SSL_3DES, ~0u, SSL3_VERSION}, |
| 460 |
{"TLSv1.2", ~0u, ~0u, ~SSL_3DES, ~0u, TLS1_2_VERSION}, |
| 461 |
}; |
| 462 |
|
| 463 |
static const size_t kCipherAliasesLen = OPENSSL_ARRAY_SIZE(kCipherAliases); |
| 464 |
|
| 465 |
bool ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
| 466 |
size_t *out_mac_secret_len, |
| 467 |
size_t *out_fixed_iv_len, const SSL_CIPHER *cipher, |
| 468 |
uint16_t version, bool is_dtls) { |
| 469 |
*out_aead = NULL; |
| 470 |
*out_mac_secret_len = 0; |
| 471 |
*out_fixed_iv_len = 0; |
| 472 |
|
| 473 |
const bool is_tls12 = version == TLS1_2_VERSION && !is_dtls; |
| 474 |
const bool is_tls13 = version == TLS1_3_VERSION && !is_dtls; |
| 475 |
|
| 476 |
if (cipher->algorithm_mac == SSL_AEAD) { |
| 477 |
if (cipher->algorithm_enc == SSL_AES128GCM) { |
| 478 |
if (is_tls12) { |
| 479 |
*out_aead = EVP_aead_aes_128_gcm_tls12(); |
| 480 |
} else if (is_tls13) { |
| 481 |
*out_aead = EVP_aead_aes_128_gcm_tls13(); |
| 482 |
} else { |
| 483 |
*out_aead = EVP_aead_aes_128_gcm(); |
| 484 |
} |
| 485 |
*out_fixed_iv_len = 4; |
| 486 |
} else if (cipher->algorithm_enc == SSL_AES256GCM) { |
| 487 |
if (is_tls12) { |
| 488 |
*out_aead = EVP_aead_aes_256_gcm_tls12(); |
| 489 |
} else if (is_tls13) { |
| 490 |
*out_aead = EVP_aead_aes_256_gcm_tls13(); |
| 491 |
} else { |
| 492 |
*out_aead = EVP_aead_aes_256_gcm(); |
| 493 |
} |
| 494 |
*out_fixed_iv_len = 4; |
| 495 |
} else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) { |
| 496 |
*out_aead = EVP_aead_chacha20_poly1305(); |
| 497 |
*out_fixed_iv_len = 12; |
| 498 |
} else { |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
// In TLS 1.3, the iv_len is equal to the AEAD nonce length whereas the code |
| 503 |
// above computes the TLS 1.2 construction. |
| 504 |
if (version >= TLS1_3_VERSION) { |
| 505 |
*out_fixed_iv_len = EVP_AEAD_nonce_length(*out_aead); |
| 506 |
} |
| 507 |
} else if (cipher->algorithm_mac == SSL_SHA1) { |
| 508 |
if (cipher->algorithm_enc == SSL_eNULL) { |
| 509 |
*out_aead = EVP_aead_null_sha1_tls(); |
| 510 |
} else if (cipher->algorithm_enc == SSL_3DES) { |
| 511 |
if (version == TLS1_VERSION) { |
| 512 |
*out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(); |
| 513 |
*out_fixed_iv_len = 8; |
| 514 |
} else { |
| 515 |
*out_aead = EVP_aead_des_ede3_cbc_sha1_tls(); |
| 516 |
} |
| 517 |
} else if (cipher->algorithm_enc == SSL_AES128) { |
| 518 |
if (version == TLS1_VERSION) { |
| 519 |
*out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(); |
| 520 |
*out_fixed_iv_len = 16; |
| 521 |
} else { |
| 522 |
*out_aead = EVP_aead_aes_128_cbc_sha1_tls(); |
| 523 |
} |
| 524 |
} else if (cipher->algorithm_enc == SSL_AES256) { |
| 525 |
if (version == TLS1_VERSION) { |
| 526 |
*out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(); |
| 527 |
*out_fixed_iv_len = 16; |
| 528 |
} else { |
| 529 |
*out_aead = EVP_aead_aes_256_cbc_sha1_tls(); |
| 530 |
} |
| 531 |
} else { |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
*out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 536 |
} else if (cipher->algorithm_mac == SSL_SHA256) { |
| 537 |
if (cipher->algorithm_enc == SSL_AES128) { |
| 538 |
if (version == TLS1_VERSION) { |
| 539 |
*out_aead = EVP_aead_aes_128_cbc_sha256_tls_implicit_iv(); |
| 540 |
*out_fixed_iv_len = 16; |
| 541 |
} else { |
| 542 |
*out_aead = EVP_aead_aes_128_cbc_sha256_tls(); |
| 543 |
} |
| 544 |
} else { |
| 545 |
return false; |
| 546 |
} |
| 547 |
*out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 548 |
|
| 549 |
} else if (cipher->algorithm_mac == SSL_SHA384) { |
| 550 |
// Defensive check: SHA-384 MAC is only supported in TLS 1.2, and we should |
| 551 |
// never reach this case in normal connection flow, as |choose_cipher| |
| 552 |
// uses |SSL_CIPHER_get_min_version| and |SSL_CIPHER_get_max_version| to |
| 553 |
// filter cipher selection appropriately. |
| 554 |
// |
| 555 |
// Additionally enforce that SHA-384 is only used with AES-256. |
| 556 |
if(version != TLS1_2_VERSION || cipher->algorithm_enc != SSL_AES256) { |
| 557 |
return false; |
| 558 |
} |
| 559 |
|
| 560 |
*out_aead = EVP_aead_aes_256_cbc_sha384_tls(); |
| 561 |
*out_mac_secret_len = SHA384_DIGEST_LENGTH; |
| 562 |
} else { |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
const EVP_MD *ssl_get_handshake_digest(uint16_t version, |
| 570 |
const SSL_CIPHER *cipher) { |
| 571 |
switch (cipher->algorithm_prf) { |
| 572 |
case SSL_HANDSHAKE_MAC_DEFAULT: |
| 573 |
return version >= TLS1_2_VERSION ? EVP_sha256() : EVP_md5_sha1(); |
| 574 |
case SSL_HANDSHAKE_MAC_SHA256: |
| 575 |
return EVP_sha256(); |
| 576 |
case SSL_HANDSHAKE_MAC_SHA384: |
| 577 |
return EVP_sha384(); |
| 578 |
default: |
| 579 |
assert(0); |
| 580 |
return NULL; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
static bool is_cipher_list_separator(char c, bool is_strict) { |
| 585 |
if (c == ':') { |
| 586 |
return true; |
| 587 |
} |
| 588 |
return !is_strict && (c == ' ' || c == ';' || c == ','); |
| 589 |
} |
| 590 |
|
| 591 |
// rule_equals returns whether the NUL-terminated string |rule| is equal to the |
| 592 |
// |buf_len| bytes at |buf|. |
| 593 |
static bool rule_equals(const char *rule, const char *buf, size_t buf_len) { |
| 594 |
// |strncmp| alone only checks that |buf| is a prefix of |rule|. |
| 595 |
return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
| 596 |
} |
| 597 |
|
| 598 |
static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
| 599 |
CIPHER_ORDER **tail) { |
| 600 |
if (curr == *tail) { |
| 601 |
return; |
| 602 |
} |
| 603 |
if (curr == *head) { |
| 604 |
*head = curr->next; |
| 605 |
} |
| 606 |
if (curr->prev != NULL) { |
| 607 |
curr->prev->next = curr->next; |
| 608 |
} |
| 609 |
if (curr->next != NULL) { |
| 610 |
curr->next->prev = curr->prev; |
| 611 |
} |
| 612 |
(*tail)->next = curr; |
| 613 |
curr->prev = *tail; |
| 614 |
curr->next = NULL; |
| 615 |
*tail = curr; |
| 616 |
} |
| 617 |
|
| 618 |
static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
| 619 |
CIPHER_ORDER **tail) { |
| 620 |
if (curr == *head) { |
| 621 |
return; |
| 622 |
} |
| 623 |
if (curr == *tail) { |
| 624 |
*tail = curr->prev; |
| 625 |
} |
| 626 |
if (curr->next != NULL) { |
| 627 |
curr->next->prev = curr->prev; |
| 628 |
} |
| 629 |
if (curr->prev != NULL) { |
| 630 |
curr->prev->next = curr->next; |
| 631 |
} |
| 632 |
(*head)->prev = curr; |
| 633 |
curr->next = *head; |
| 634 |
curr->prev = NULL; |
| 635 |
*head = curr; |
| 636 |
} |
| 637 |
|
| 638 |
static bool ssl_cipher_collect_ciphers(Array<CIPHER_ORDER> *out_co_list, |
| 639 |
CIPHER_ORDER **out_head, |
| 640 |
CIPHER_ORDER **out_tail, |
| 641 |
bool config_tls13) { |
| 642 |
Array<CIPHER_ORDER> co_list; |
| 643 |
if (!co_list.Init(OPENSSL_ARRAY_SIZE(kCiphers))) { |
| 644 |
return false; |
| 645 |
} |
| 646 |
|
| 647 |
size_t co_list_num = 0; |
| 648 |
for (const SSL_CIPHER &cipher : kCiphers) { |
| 649 |
// config_tls13 tells if only TLS 1.3 ciphers should |
| 650 |
// participate in this mechanism. When false, only non-TLS-1.3 ciphers |
| 651 |
// participate in this mechanism. |
| 652 |
if ((config_tls13 && cipher.algorithm_mkey == SSL_kGENERIC) || |
| 653 |
(!config_tls13 && cipher.algorithm_mkey != SSL_kGENERIC)) { |
| 654 |
co_list[co_list_num].cipher = &cipher; |
| 655 |
co_list[co_list_num].next = NULL; |
| 656 |
co_list[co_list_num].prev = NULL; |
| 657 |
co_list[co_list_num].active = false; |
| 658 |
co_list[co_list_num].in_group = false; |
| 659 |
co_list_num++; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
// Prepare linked list from list entries. |
| 664 |
if (co_list_num > 0) { |
| 665 |
co_list[0].prev = NULL; |
| 666 |
|
| 667 |
if (co_list_num > 1) { |
| 668 |
co_list[0].next = &co_list[1]; |
| 669 |
|
| 670 |
for (size_t i = 1; i < co_list_num - 1; i++) { |
| 671 |
co_list[i].prev = &co_list[i - 1]; |
| 672 |
co_list[i].next = &co_list[i + 1]; |
| 673 |
} |
| 674 |
|
| 675 |
co_list[co_list_num - 1].prev = &co_list[co_list_num - 2]; |
| 676 |
} |
| 677 |
|
| 678 |
co_list[co_list_num - 1].next = NULL; |
| 679 |
|
| 680 |
*out_head = &co_list[0]; |
| 681 |
*out_tail = &co_list[co_list_num - 1]; |
| 682 |
} else { |
| 683 |
*out_head = nullptr; |
| 684 |
*out_tail = nullptr; |
| 685 |
} |
| 686 |
*out_co_list = std::move(co_list); |
| 687 |
return true; |
| 688 |
} |
| 689 |
|
| 690 |
SSLCipherPreferenceList::~SSLCipherPreferenceList() { |
| 691 |
OPENSSL_free(in_group_flags); |
| 692 |
} |
| 693 |
|
| 694 |
bool SSLCipherPreferenceList::Init(UniquePtr<STACK_OF(SSL_CIPHER)> ciphers_arg, |
| 695 |
Span<const bool> in_group_flags_arg) { |
| 696 |
if (sk_SSL_CIPHER_num(ciphers_arg.get()) != in_group_flags_arg.size()) { |
| 697 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 698 |
return false; |
| 699 |
} |
| 700 |
|
| 701 |
Array<bool> copy; |
| 702 |
if (!copy.CopyFrom(in_group_flags_arg)) { |
| 703 |
return false; |
| 704 |
} |
| 705 |
ciphers = std::move(ciphers_arg); |
| 706 |
size_t unused_len; |
| 707 |
copy.Release(&in_group_flags, &unused_len); |
| 708 |
return true; |
| 709 |
} |
| 710 |
|
| 711 |
bool SSLCipherPreferenceList::Init(const SSLCipherPreferenceList& other) { |
| 712 |
size_t size = sk_SSL_CIPHER_num(other.ciphers.get()); |
| 713 |
Span<const bool> other_flags(other.in_group_flags, size); |
| 714 |
UniquePtr<STACK_OF(SSL_CIPHER)> other_ciphers(sk_SSL_CIPHER_dup( |
| 715 |
other.ciphers.get())); |
| 716 |
if (!other_ciphers) { |
| 717 |
return false; |
| 718 |
} |
| 719 |
return Init(std::move(other_ciphers), other_flags); |
| 720 |
} |
| 721 |
|
| 722 |
void SSLCipherPreferenceList::Remove(const SSL_CIPHER *cipher) { |
| 723 |
size_t index; |
| 724 |
if (!sk_SSL_CIPHER_find_awslc(ciphers.get(), &index, cipher)) { |
| 725 |
return; |
| 726 |
} |
| 727 |
if (!in_group_flags[index] /* last element of group */ && index > 0) { |
| 728 |
in_group_flags[index-1] = false; |
| 729 |
} |
| 730 |
for (size_t i = index; i < sk_SSL_CIPHER_num(ciphers.get()) - 1; ++i) { |
| 731 |
in_group_flags[i] = in_group_flags[i+1]; |
| 732 |
} |
| 733 |
sk_SSL_CIPHER_delete(ciphers.get(), index); |
| 734 |
} |
| 735 |
|
| 736 |
// ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its |
| 737 |
// parameters in the linked list from |*head_p| to |*tail_p|. It writes the new |
| 738 |
// head and tail of the list to |*head_p| and |*tail_p|, respectively. |
| 739 |
// |
| 740 |
// - If |cipher_id| is non-zero, only that cipher is selected. |
| 741 |
// - Otherwise, if |strength_bits| is non-negative, it selects ciphers |
| 742 |
// of that strength. |
| 743 |
// - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and |
| 744 |
// |min_version|. |
| 745 |
static void ssl_cipher_apply_rule( |
| 746 |
uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth, |
| 747 |
uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule, |
| 748 |
int strength_bits, bool in_group, CIPHER_ORDER **head_p, |
| 749 |
CIPHER_ORDER **tail_p) { |
| 750 |
CIPHER_ORDER *head, *tail, *curr, *next, *last; |
| 751 |
const SSL_CIPHER *cp; |
| 752 |
bool reverse = false; |
| 753 |
|
| 754 |
if (cipher_id == 0 && strength_bits == -1 && min_version == 0 && |
| 755 |
(alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) { |
| 756 |
// The rule matches nothing, so bail early. |
| 757 |
return; |
| 758 |
} |
| 759 |
|
| 760 |
if (rule == CIPHER_DEL) { |
| 761 |
// needed to maintain sorting between currently deleted ciphers |
| 762 |
reverse = true; |
| 763 |
} |
| 764 |
|
| 765 |
head = *head_p; |
| 766 |
tail = *tail_p; |
| 767 |
|
| 768 |
if (reverse) { |
| 769 |
next = tail; |
| 770 |
last = head; |
| 771 |
} else { |
| 772 |
next = head; |
| 773 |
last = tail; |
| 774 |
} |
| 775 |
|
| 776 |
curr = NULL; |
| 777 |
for (;;) { |
| 778 |
if (curr == last) { |
| 779 |
break; |
| 780 |
} |
| 781 |
|
| 782 |
curr = next; |
| 783 |
if (curr == NULL) { |
| 784 |
break; |
| 785 |
} |
| 786 |
|
| 787 |
next = reverse ? curr->prev : curr->next; |
| 788 |
cp = curr->cipher; |
| 789 |
|
| 790 |
// Selection criteria is either a specific cipher, the value of |
| 791 |
// |strength_bits|, or the algorithms used. |
| 792 |
if (cipher_id != 0) { |
| 793 |
if (cipher_id != cp->id) { |
| 794 |
continue; |
| 795 |
} |
| 796 |
} else if (strength_bits >= 0) { |
| 797 |
if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) { |
| 798 |
continue; |
| 799 |
} |
| 800 |
} else { |
| 801 |
if (!(alg_mkey & cp->algorithm_mkey) || |
| 802 |
!(alg_auth & cp->algorithm_auth) || |
| 803 |
!(alg_enc & cp->algorithm_enc) || |
| 804 |
!(alg_mac & cp->algorithm_mac) || |
| 805 |
(min_version != 0 && SSL_CIPHER_get_min_version(cp) != min_version) || |
| 806 |
// The NULL cipher must be selected explicitly. |
| 807 |
cp->algorithm_enc == SSL_eNULL) { |
| 808 |
continue; |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
// add the cipher if it has not been added yet. |
| 813 |
if (rule == CIPHER_ADD) { |
| 814 |
// reverse == false |
| 815 |
if (!curr->active) { |
| 816 |
ll_append_tail(&head, curr, &tail); |
| 817 |
curr->active = true; |
| 818 |
curr->in_group = in_group; |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
// Move the added cipher to this location |
| 823 |
else if (rule == CIPHER_ORD) { |
| 824 |
// reverse == false |
| 825 |
if (curr->active) { |
| 826 |
ll_append_tail(&head, curr, &tail); |
| 827 |
curr->in_group = false; |
| 828 |
} |
| 829 |
} else if (rule == CIPHER_DEL) { |
| 830 |
// reverse == true |
| 831 |
if (curr->active) { |
| 832 |
// most recently deleted ciphersuites get best positions |
| 833 |
// for any future CIPHER_ADD (note that the CIPHER_DEL loop |
| 834 |
// works in reverse to maintain the order) |
| 835 |
ll_append_head(&head, curr, &tail); |
| 836 |
curr->active = false; |
| 837 |
curr->in_group = false; |
| 838 |
} |
| 839 |
} else if (rule == CIPHER_KILL) { |
| 840 |
// reverse == false |
| 841 |
if (head == curr) { |
| 842 |
head = curr->next; |
| 843 |
} else { |
| 844 |
curr->prev->next = curr->next; |
| 845 |
} |
| 846 |
|
| 847 |
if (tail == curr) { |
| 848 |
tail = curr->prev; |
| 849 |
} |
| 850 |
curr->active = false; |
| 851 |
if (curr->next != NULL) { |
| 852 |
curr->next->prev = curr->prev; |
| 853 |
} |
| 854 |
if (curr->prev != NULL) { |
| 855 |
curr->prev->next = curr->next; |
| 856 |
} |
| 857 |
curr->next = NULL; |
| 858 |
curr->prev = NULL; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
*head_p = head; |
| 863 |
*tail_p = tail; |
| 864 |
} |
| 865 |
|
| 866 |
static bool ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
| 867 |
CIPHER_ORDER **tail_p) { |
| 868 |
// This routine sorts the ciphers with descending strength. The sorting must |
| 869 |
// keep the pre-sorted sequence, so we apply the normal sorting routine as |
| 870 |
// '+' movement to the end of the list. |
| 871 |
int max_strength_bits = 0; |
| 872 |
CIPHER_ORDER *curr = *head_p; |
| 873 |
while (curr != NULL) { |
| 874 |
if (curr->active && |
| 875 |
SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) { |
| 876 |
max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL); |
| 877 |
} |
| 878 |
curr = curr->next; |
| 879 |
} |
| 880 |
|
| 881 |
Array<int> number_uses; |
| 882 |
if (!number_uses.Init(max_strength_bits + 1)) { |
| 883 |
return false; |
| 884 |
} |
| 885 |
OPENSSL_memset(number_uses.data(), 0, (max_strength_bits + 1) * sizeof(int)); |
| 886 |
|
| 887 |
// Now find the strength_bits values actually used. |
| 888 |
curr = *head_p; |
| 889 |
while (curr != NULL) { |
| 890 |
if (curr->active) { |
| 891 |
number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++; |
| 892 |
} |
| 893 |
curr = curr->next; |
| 894 |
} |
| 895 |
|
| 896 |
// Go through the list of used strength_bits values in descending order. |
| 897 |
for (int i = max_strength_bits; i >= 0; i--) { |
| 898 |
if (number_uses[i] > 0) { |
| 899 |
ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, false, head_p, |
| 900 |
tail_p); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
return true; |
| 905 |
} |
| 906 |
|
| 907 |
static bool ssl_cipher_process_rulestr(const char *rule_str, |
| 908 |
CIPHER_ORDER **head_p, |
| 909 |
CIPHER_ORDER **tail_p, bool strict, |
| 910 |
bool config_tls13) { |
| 911 |
uint32_t alg_mkey, alg_auth, alg_enc, alg_mac; |
| 912 |
uint16_t min_version; |
| 913 |
const char *l, *buf; |
| 914 |
|
| 915 |
bool multi, skip_rule, in_group = false, has_group = false; |
| 916 |
size_t j, buf_len; |
| 917 |
uint32_t cipher_id; |
| 918 |
char ch; |
| 919 |
|
| 920 |
l = rule_str; |
| 921 |
for (;;) { |
| 922 |
int rule = CIPHER_ADD; |
| 923 |
ch = *l; |
| 924 |
|
| 925 |
if (ch == '\0') { |
| 926 |
break; // done |
| 927 |
} |
| 928 |
|
| 929 |
if (in_group) { |
| 930 |
if (ch == ']') { |
| 931 |
if (*tail_p) { |
| 932 |
(*tail_p)->in_group = false; |
| 933 |
} |
| 934 |
in_group = false; |
| 935 |
l++; |
| 936 |
continue; |
| 937 |
} |
| 938 |
|
| 939 |
if (ch == '|') { |
| 940 |
l++; |
| 941 |
continue; |
| 942 |
} else if (!OPENSSL_isalnum(ch)) { |
| 943 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
| 944 |
return false; |
| 945 |
} else { |
| 946 |
rule = CIPHER_ADD; |
| 947 |
} |
| 948 |
} else if (ch == '-') { |
| 949 |
rule = CIPHER_DEL; |
| 950 |
l++; |
| 951 |
} else if (ch == '+') { |
| 952 |
rule = CIPHER_ORD; |
| 953 |
l++; |
| 954 |
} else if (ch == '!') { |
| 955 |
rule = CIPHER_KILL; |
| 956 |
l++; |
| 957 |
} else if (ch == '@') { |
| 958 |
rule = CIPHER_SPECIAL; |
| 959 |
l++; |
| 960 |
} else if (ch == '[') { |
| 961 |
assert(!in_group); |
| 962 |
in_group = true; |
| 963 |
has_group = true; |
| 964 |
l++; |
| 965 |
continue; |
| 966 |
} |
| 967 |
|
| 968 |
// If preference groups are enabled, the only legal operator is +. |
| 969 |
// Otherwise the in_group bits will get mixed up. |
| 970 |
if (has_group && rule != CIPHER_ADD) { |
| 971 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
| 972 |
return false; |
| 973 |
} |
| 974 |
|
| 975 |
if (is_cipher_list_separator(ch, strict)) { |
| 976 |
l++; |
| 977 |
continue; |
| 978 |
} |
| 979 |
|
| 980 |
multi = false; |
| 981 |
cipher_id = 0; |
| 982 |
alg_mkey = ~0u; |
| 983 |
alg_auth = ~0u; |
| 984 |
alg_enc = ~0u; |
| 985 |
alg_mac = ~0u; |
| 986 |
min_version = 0; |
| 987 |
skip_rule = false; |
| 988 |
|
| 989 |
for (;;) { |
| 990 |
ch = *l; |
| 991 |
buf = l; |
| 992 |
buf_len = 0; |
| 993 |
while (OPENSSL_isalnum(ch) || ch == '-' || ch == '.' || ch == '_') { |
| 994 |
ch = *(++l); |
| 995 |
buf_len++; |
| 996 |
} |
| 997 |
|
| 998 |
if (buf_len == 0) { |
| 999 |
// We hit something we cannot deal with, it is no command or separator |
| 1000 |
// nor alphanumeric, so we call this an error. |
| 1001 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (rule == CIPHER_SPECIAL) { |
| 1006 |
break; |
| 1007 |
} |
| 1008 |
|
| 1009 |
// Look for a matching exact cipher. These aren't allowed in multipart |
| 1010 |
// rules. |
| 1011 |
if (!multi && ch != '+') { |
| 1012 |
for (j = 0; j < OPENSSL_ARRAY_SIZE(kCiphers); j++) { |
| 1013 |
const SSL_CIPHER *cipher = &kCiphers[j]; |
| 1014 |
if ((config_tls13 && cipher->algorithm_mkey != SSL_kGENERIC) || |
| 1015 |
(!config_tls13 && cipher->algorithm_mkey == SSL_kGENERIC)) { |
| 1016 |
continue; |
| 1017 |
} |
| 1018 |
if (rule_equals(cipher->name, buf, buf_len) || |
| 1019 |
rule_equals(cipher->standard_name, buf, buf_len)) { |
| 1020 |
cipher_id = cipher->id; |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
if (cipher_id == 0) { |
| 1026 |
// If not an exact cipher, look for a matching cipher alias. |
| 1027 |
for (j = 0; j < kCipherAliasesLen; j++) { |
| 1028 |
if (rule_equals(kCipherAliases[j].name, buf, buf_len)) { |
| 1029 |
alg_mkey &= kCipherAliases[j].algorithm_mkey; |
| 1030 |
alg_auth &= kCipherAliases[j].algorithm_auth; |
| 1031 |
alg_enc &= kCipherAliases[j].algorithm_enc; |
| 1032 |
alg_mac &= kCipherAliases[j].algorithm_mac; |
| 1033 |
|
| 1034 |
if (min_version != 0 && |
| 1035 |
min_version != kCipherAliases[j].min_version) { |
| 1036 |
skip_rule = true; |
| 1037 |
} else { |
| 1038 |
min_version = kCipherAliases[j].min_version; |
| 1039 |
} |
| 1040 |
break; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
if (j == kCipherAliasesLen) { |
| 1044 |
skip_rule = true; |
| 1045 |
if (strict) { |
| 1046 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
// Check for a multipart rule. |
| 1053 |
if (ch != '+') { |
| 1054 |
break; |
| 1055 |
} |
| 1056 |
l++; |
| 1057 |
multi = true; |
| 1058 |
} |
| 1059 |
|
| 1060 |
// Ok, we have the rule, now apply it. |
| 1061 |
if (rule == CIPHER_SPECIAL) { |
| 1062 |
if (buf_len != 8 || strncmp(buf, "STRENGTH", 8) != 0) { |
| 1063 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
| 1064 |
return false; |
| 1065 |
} |
| 1066 |
if (!ssl_cipher_strength_sort(head_p, tail_p)) { |
| 1067 |
return false; |
| 1068 |
} |
| 1069 |
|
| 1070 |
// We do not support any "multi" options together with "@", so throw away |
| 1071 |
// the rest of the command, if any left, until end or ':' is found. |
| 1072 |
while (*l != '\0' && !is_cipher_list_separator(*l, strict)) { |
| 1073 |
l++; |
| 1074 |
} |
| 1075 |
} else if (!skip_rule) { |
| 1076 |
ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac, |
| 1077 |
min_version, rule, -1, in_group, head_p, tail_p); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (in_group) { |
| 1082 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
| 1083 |
return false; |
| 1084 |
} |
| 1085 |
|
| 1086 |
return true; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static const char *kKnownKeywordFilterRulesMappingToDefault[] = { |
| 1090 |
"ALL", |
| 1091 |
"DEFAULT", |
| 1092 |
"FIPS", |
| 1093 |
"HIGH", |
| 1094 |
}; |
| 1095 |
|
| 1096 |
static bool is_known_default_alias_keyword_filter_rule(const char *rule, |
| 1097 |
size_t *matched_rule_length) { |
| 1098 |
|
| 1099 |
for (auto known_rule : kKnownKeywordFilterRulesMappingToDefault) { |
| 1100 |
if (strncmp(rule, known_rule, strlen(known_rule)) == 0) { |
| 1101 |
*matched_rule_length = (size_t) strlen(known_rule); |
| 1102 |
return true; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
*matched_rule_length = 0; |
| 1106 |
return false; |
| 1107 |
} |
| 1108 |
|
| 1109 |
int update_cipher_list(UniquePtr<SSLCipherPreferenceList> &dst, |
| 1110 |
UniquePtr<SSLCipherPreferenceList> &ciphers, |
| 1111 |
UniquePtr<SSLCipherPreferenceList> &tls13_ciphers) { |
| 1112 |
bssl::UniquePtr<STACK_OF(SSL_CIPHER)> tmp_cipher_list; |
| 1113 |
size_t num_removed_tls13_ciphers = 0, num_added_tls13_ciphers = 0; |
| 1114 |
Array<bool> updated_in_group_flags; |
| 1115 |
|
| 1116 |
if (ciphers && ciphers->ciphers) { |
| 1117 |
tmp_cipher_list.reset(sk_SSL_CIPHER_dup(ciphers->ciphers.get())); |
| 1118 |
} else { |
| 1119 |
tmp_cipher_list.reset(sk_SSL_CIPHER_new_null()); |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (tmp_cipher_list == nullptr) { |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
// Delete any existing TLSv1.3 ciphersuites. These will be first in the list |
| 1127 |
while (sk_SSL_CIPHER_num(tmp_cipher_list.get()) > 0 && |
| 1128 |
SSL_CIPHER_get_min_version(sk_SSL_CIPHER_value(tmp_cipher_list.get(), 0)) |
| 1129 |
== TLS1_3_VERSION) { |
| 1130 |
sk_SSL_CIPHER_delete(tmp_cipher_list.get(), 0); |
| 1131 |
num_removed_tls13_ciphers++; |
| 1132 |
} |
| 1133 |
|
| 1134 |
size_t num_updated_tls12_ciphers = sk_SSL_CIPHER_num(tmp_cipher_list.get()); |
| 1135 |
|
| 1136 |
// Add any configure tls 1.3 ciphersuites |
| 1137 |
if (tls13_ciphers && tls13_ciphers->ciphers) { |
| 1138 |
STACK_OF(SSL_CIPHER) *tls13_cipher_stack = tls13_ciphers->ciphers.get(); |
| 1139 |
num_added_tls13_ciphers = sk_SSL_CIPHER_num(tls13_cipher_stack); |
| 1140 |
for (int i = sk_SSL_CIPHER_num(tls13_cipher_stack) - 1; i >= 0; i--) { |
| 1141 |
const SSL_CIPHER *tls13_cipher = sk_SSL_CIPHER_value(tls13_cipher_stack, i); |
| 1142 |
if (!sk_SSL_CIPHER_unshift(tmp_cipher_list.get(), tls13_cipher)) { |
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
if (!updated_in_group_flags.Init(num_added_tls13_ciphers + |
| 1150 |
num_updated_tls12_ciphers)) { |
| 1151 |
return 0; |
| 1152 |
} |
| 1153 |
std::fill(updated_in_group_flags.begin(), updated_in_group_flags.end(), |
| 1154 |
false); |
| 1155 |
|
| 1156 |
// Copy in_group_flags from |ctx->tls13_cipher_list| |
| 1157 |
if (tls13_ciphers && tls13_ciphers->in_group_flags) { |
| 1158 |
const auto& tls13_flags = tls13_ciphers->in_group_flags; |
| 1159 |
// Ensure value of last element in |in_group_flags| is 0. The last cipher |
| 1160 |
// in a list must be the end of any group in that list. |
| 1161 |
if (tls13_flags[num_added_tls13_ciphers - 1] != 0) { |
| 1162 |
tls13_flags[num_added_tls13_ciphers - 1] = false; |
| 1163 |
} |
| 1164 |
for (size_t i = 0; i < num_added_tls13_ciphers; i++) { |
| 1165 |
updated_in_group_flags[i] = tls13_flags[i]; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Copy remaining in_group_flags from |ctx->cipher_list| |
| 1170 |
if (ciphers && ciphers->in_group_flags) { |
| 1171 |
for (size_t i = 0; i < num_updated_tls12_ciphers; i++) { |
| 1172 |
updated_in_group_flags[i + num_added_tls13_ciphers] = |
| 1173 |
ciphers->in_group_flags[i + num_removed_tls13_ciphers]; |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
Span<const bool> flags_span(updated_in_group_flags.data(), updated_in_group_flags.size()); |
| 1178 |
UniquePtr<SSLCipherPreferenceList> new_list = MakeUnique<SSLCipherPreferenceList>(); |
| 1179 |
if (!new_list || !new_list->Init(std::move(tmp_cipher_list), flags_span)) { |
| 1180 |
return 0; |
| 1181 |
} |
| 1182 |
|
| 1183 |
dst = std::move(new_list); |
| 1184 |
return 1; |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool ssl_create_cipher_list(UniquePtr<SSLCipherPreferenceList> *out_cipher_list, |
| 1188 |
const bool has_aes_hw, const char *rule_str, |
| 1189 |
bool strict, bool config_tls13) { |
| 1190 |
// Return with error if nothing to do. |
| 1191 |
if (rule_str == NULL || out_cipher_list == NULL) { |
| 1192 |
return false; |
| 1193 |
} |
| 1194 |
|
| 1195 |
// Now we have to collect the available ciphers from the compiled in ciphers. |
| 1196 |
// We cannot get more than the number compiled in, so it is used for |
| 1197 |
// allocation. |
| 1198 |
Array<CIPHER_ORDER> co_list; |
| 1199 |
CIPHER_ORDER *head = nullptr, *tail = nullptr; |
| 1200 |
if (!ssl_cipher_collect_ciphers(&co_list, &head, &tail, config_tls13)) { |
| 1201 |
return false; |
| 1202 |
} |
| 1203 |
|
| 1204 |
// Now arrange all ciphers by preference: |
| 1205 |
// TODO(davidben): Compute this order once and copy it. |
| 1206 |
|
| 1207 |
ssl_cipher_apply_rule(0, SSL_kGENERIC, SSL_aGENERIC, ~0u, ~0u, 0, CIPHER_ADD, |
| 1208 |
-1, false, &head, &tail); |
| 1209 |
// Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other |
| 1210 |
// key exchange mechanisms. |
| 1211 |
ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1, |
| 1212 |
false, &head, &tail); |
| 1213 |
ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false, |
| 1214 |
&head, &tail); |
| 1215 |
ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head, |
| 1216 |
&tail); |
| 1217 |
|
| 1218 |
// Order the bulk ciphers. First the preferred AEAD ciphers. We prefer |
| 1219 |
// CHACHA20 unless there is hardware support for fast and constant-time |
| 1220 |
// AES_GCM. Of the two CHACHA20 variants, the new one is preferred over the |
| 1221 |
// old one. |
| 1222 |
if (has_aes_hw) { |
| 1223 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, |
| 1224 |
false, &head, &tail); |
| 1225 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, |
| 1226 |
false, &head, &tail); |
| 1227 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD, |
| 1228 |
-1, false, &head, &tail); |
| 1229 |
} else { |
| 1230 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, 0, CIPHER_ADD, |
| 1231 |
-1, false, &head, &tail); |
| 1232 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, |
| 1233 |
false, &head, &tail); |
| 1234 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, |
| 1235 |
false, &head, &tail); |
| 1236 |
} |
| 1237 |
|
| 1238 |
// Then the legacy non-AEAD ciphers: AES_128_CBC, AES_256_CBC, |
| 1239 |
// 3DES_EDE_CBC_SHA. |
| 1240 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, false, |
| 1241 |
&head, &tail); |
| 1242 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, false, |
| 1243 |
&head, &tail); |
| 1244 |
ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, false, |
| 1245 |
&head, &tail); |
| 1246 |
|
| 1247 |
// Temporarily enable everything else for sorting |
| 1248 |
ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, false, &head, |
| 1249 |
&tail); |
| 1250 |
|
| 1251 |
// Move ciphers without forward secrecy to the end. |
| 1252 |
ssl_cipher_apply_rule(0, (SSL_kRSA | SSL_kPSK), ~0u, ~0u, ~0u, 0, CIPHER_ORD, |
| 1253 |
-1, false, &head, &tail); |
| 1254 |
|
| 1255 |
// Now disable everything (maintaining the ordering!) |
| 1256 |
ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, false, &head, |
| 1257 |
&tail); |
| 1258 |
|
| 1259 |
// Check for keyword rules that map to the default "ALL" rule. |
| 1260 |
const char *rule_p = rule_str; |
| 1261 |
size_t matched_rule_length = 0; |
| 1262 |
if (is_known_default_alias_keyword_filter_rule(rule_str, &matched_rule_length)) { |
| 1263 |
if (!ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST, &head, &tail, |
| 1264 |
strict, config_tls13)) { |
| 1265 |
return false; |
| 1266 |
} |
| 1267 |
|
| 1268 |
rule_p += matched_rule_length; |
| 1269 |
if (*rule_p == ':') { |
| 1270 |
rule_p++; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (*rule_p != '\0' && |
| 1275 |
!ssl_cipher_process_rulestr(rule_p, &head, &tail, strict, config_tls13)) { |
| 1276 |
return false; |
| 1277 |
} |
| 1278 |
|
| 1279 |
// Allocate new "cipherstack" for the result, return with error |
| 1280 |
// if we cannot get one. |
| 1281 |
UniquePtr<STACK_OF(SSL_CIPHER)> cipherstack(sk_SSL_CIPHER_new_null()); |
| 1282 |
Array<bool> in_group_flags; |
| 1283 |
if (cipherstack == nullptr || |
| 1284 |
!in_group_flags.Init(OPENSSL_ARRAY_SIZE(kCiphers))) { |
| 1285 |
return false; |
| 1286 |
} |
| 1287 |
|
| 1288 |
// The cipher selection for the list is done. The ciphers are added |
| 1289 |
// to the resulting precedence to the STACK_OF(SSL_CIPHER). |
| 1290 |
size_t num_in_group_flags = 0; |
| 1291 |
for (CIPHER_ORDER *curr = head; curr != NULL; curr = curr->next) { |
| 1292 |
if (curr->active) { |
| 1293 |
if (!sk_SSL_CIPHER_push(cipherstack.get(), curr->cipher)) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
in_group_flags[num_in_group_flags++] = curr->in_group; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
UniquePtr<SSLCipherPreferenceList> pref_list = |
| 1301 |
MakeUnique<SSLCipherPreferenceList>(); |
| 1302 |
if (!pref_list || |
| 1303 |
!pref_list->Init( |
| 1304 |
std::move(cipherstack), |
| 1305 |
MakeConstSpan(in_group_flags).subspan(0, num_in_group_flags))) { |
| 1306 |
return false; |
| 1307 |
} |
| 1308 |
|
| 1309 |
*out_cipher_list = std::move(pref_list); |
| 1310 |
|
| 1311 |
// Configuring an empty cipher list is an error when |strict| is true, but |
| 1312 |
// still updates the output. When otherwise, OpenSSL explicitly allows an |
| 1313 |
// empty list. |
| 1314 |
if ((strict || (*rule_str != '\0')) && |
| 1315 |
sk_SSL_CIPHER_num((*out_cipher_list)->ciphers.get()) == 0) { |
| 1316 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
| 1317 |
return false; |
| 1318 |
} |
| 1319 |
|
| 1320 |
return true; |
| 1321 |
} |
| 1322 |
|
| 1323 |
int ssl_get_certificate_slot_index(const EVP_PKEY *pkey) { |
| 1324 |
switch (EVP_PKEY_id(pkey)) { |
| 1325 |
case EVP_PKEY_RSA: |
| 1326 |
return SSL_PKEY_RSA; |
| 1327 |
case EVP_PKEY_EC: |
| 1328 |
return SSL_PKEY_ECC; |
| 1329 |
case EVP_PKEY_ED25519: |
| 1330 |
return SSL_PKEY_ED25519; |
| 1331 |
default: |
| 1332 |
return -1; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
uint32_t ssl_cipher_auth_mask_for_key(const EVP_PKEY *key) { |
| 1337 |
switch (EVP_PKEY_id(key)) { |
| 1338 |
case EVP_PKEY_RSA: |
| 1339 |
return SSL_aRSA; |
| 1340 |
case EVP_PKEY_EC: |
| 1341 |
case EVP_PKEY_ED25519: |
| 1342 |
// Ed25519 keys in TLS 1.2 repurpose the ECDSA ciphers. |
| 1343 |
return SSL_aECDSA; |
| 1344 |
default: |
| 1345 |
return 0; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
bool ssl_cipher_uses_certificate_auth(const SSL_CIPHER *cipher) { |
| 1350 |
return (cipher->algorithm_auth & SSL_aCERT) != 0; |
| 1351 |
} |
| 1352 |
|
| 1353 |
bool ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
| 1354 |
// Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. It is |
| 1355 |
// optional or omitted in all others. |
| 1356 |
return (cipher->algorithm_mkey & SSL_kECDHE) != 0; |
| 1357 |
} |
| 1358 |
|
| 1359 |
size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) { |
| 1360 |
size_t block_size; |
| 1361 |
switch (cipher->algorithm_enc) { |
| 1362 |
case SSL_3DES: |
| 1363 |
block_size = 8; |
| 1364 |
break; |
| 1365 |
case SSL_AES128: |
| 1366 |
case SSL_AES256: |
| 1367 |
block_size = 16; |
| 1368 |
break; |
| 1369 |
default: |
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
|
| 1373 |
// All supported TLS 1.0 ciphers use SHA-1. |
| 1374 |
assert(cipher->algorithm_mac == SSL_SHA1); |
| 1375 |
size_t ret = 1 + SHA_DIGEST_LENGTH; |
| 1376 |
ret += block_size - (ret % block_size); |
| 1377 |
return ret; |
| 1378 |
} |
| 1379 |
|
| 1380 |
BSSL_NAMESPACE_END |
| 1381 |
|
| 1382 |
using namespace bssl; |
| 1383 |
|
| 1384 |
static constexpr int ssl_cipher_id_cmp_inner(const SSL_CIPHER *a, |
| 1385 |
const SSL_CIPHER *b) { |
| 1386 |
// C++11's constexpr functions must have a body consisting of just a |
| 1387 |
// return-statement. |
| 1388 |
return (a->id > b->id) ? 1 : ((a->id < b->id) ? -1 : 0); |
| 1389 |
} |
| 1390 |
|
| 1391 |
static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) { |
| 1392 |
return ssl_cipher_id_cmp_inner(reinterpret_cast<const SSL_CIPHER *>(in_a), |
| 1393 |
reinterpret_cast<const SSL_CIPHER *>(in_b)); |
| 1394 |
} |
| 1395 |
|
| 1396 |
template <typename T, size_t N> |
| 1397 |
static constexpr size_t countof(T const (&)[N]) { |
| 1398 |
return N; |
| 1399 |
} |
| 1400 |
|
| 1401 |
template <typename T, size_t I> |
| 1402 |
static constexpr int check_order(const T (&arr)[I], size_t N) { |
| 1403 |
// C++11's constexpr functions must have a body consisting of just a |
| 1404 |
// return-statement. |
| 1405 |
return N > 1 ? ((ssl_cipher_id_cmp_inner(&arr[N - 2], &arr[N - 1]) < 0) |
| 1406 |
? check_order(arr, N - 1) |
| 1407 |
: 0) |
| 1408 |
: 1; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static_assert(check_order(kCiphers, countof(kCiphers)) == 1, |
| 1412 |
"Ciphers are not sorted, bsearch won't work"); |
| 1413 |
|
| 1414 |
const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { |
| 1415 |
SSL_CIPHER c; |
| 1416 |
|
| 1417 |
c.id = 0x03000000L | value; |
| 1418 |
return reinterpret_cast<const SSL_CIPHER *>(bsearch( |
| 1419 |
&c, kCiphers, OPENSSL_ARRAY_SIZE(kCiphers), sizeof(SSL_CIPHER), |
| 1420 |
ssl_cipher_id_cmp)); |
| 1421 |
} |
| 1422 |
|
| 1423 |
const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr) { |
| 1424 |
if (ssl != nullptr && ptr != nullptr) { |
| 1425 |
uint16_t cipher_id = CRYPTO_load_u16_be(ptr); |
| 1426 |
return SSL_get_cipher_by_value(cipher_id); |
| 1427 |
} |
| 1428 |
|
| 1429 |
return NULL; |
| 1430 |
} |
| 1431 |
|
| 1432 |
uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; } |
| 1433 |
|
| 1434 |
uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *cipher) { |
| 1435 |
// All OpenSSL cipher IDs are prefaced with 0x03. Historically this referred |
| 1436 |
// to SSLv2 vs SSLv3. |
| 1437 |
assert((cipher->id & 0xff000000) == 0x03000000); |
| 1438 |
return static_cast<uint16_t>(cipher->id); |
| 1439 |
} |
| 1440 |
|
| 1441 |
int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) { |
| 1442 |
return (cipher->algorithm_mac & SSL_AEAD) != 0; |
| 1443 |
} |
| 1444 |
|
| 1445 |
int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *cipher) { |
| 1446 |
switch (cipher->algorithm_enc) { |
| 1447 |
case SSL_eNULL: |
| 1448 |
return NID_undef; |
| 1449 |
case SSL_3DES: |
| 1450 |
return NID_des_ede3_cbc; |
| 1451 |
case SSL_AES128: |
| 1452 |
return NID_aes_128_cbc; |
| 1453 |
case SSL_AES256: |
| 1454 |
return NID_aes_256_cbc; |
| 1455 |
case SSL_AES128GCM: |
| 1456 |
return NID_aes_128_gcm; |
| 1457 |
case SSL_AES256GCM: |
| 1458 |
return NID_aes_256_gcm; |
| 1459 |
case SSL_CHACHA20POLY1305: |
| 1460 |
return NID_chacha20_poly1305; |
| 1461 |
} |
| 1462 |
assert(0); |
| 1463 |
return NID_undef; |
| 1464 |
} |
| 1465 |
|
| 1466 |
int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *cipher) { |
| 1467 |
switch (cipher->algorithm_mac) { |
| 1468 |
case SSL_AEAD: |
| 1469 |
return NID_undef; |
| 1470 |
case SSL_SHA1: |
| 1471 |
return NID_sha1; |
| 1472 |
case SSL_SHA256: |
| 1473 |
return NID_sha256; |
| 1474 |
case SSL_SHA384: |
| 1475 |
return NID_sha384; |
| 1476 |
} |
| 1477 |
assert(0); |
| 1478 |
return NID_undef; |
| 1479 |
} |
| 1480 |
|
| 1481 |
int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *cipher) { |
| 1482 |
switch (cipher->algorithm_mkey) { |
| 1483 |
case SSL_kRSA: |
| 1484 |
return NID_kx_rsa; |
| 1485 |
case SSL_kECDHE: |
| 1486 |
return NID_kx_ecdhe; |
| 1487 |
case SSL_kPSK: |
| 1488 |
return NID_kx_psk; |
| 1489 |
case SSL_kGENERIC: |
| 1490 |
return NID_kx_any; |
| 1491 |
} |
| 1492 |
assert(0); |
| 1493 |
return NID_undef; |
| 1494 |
} |
| 1495 |
|
| 1496 |
int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *cipher) { |
| 1497 |
switch (cipher->algorithm_auth) { |
| 1498 |
case SSL_aRSA: |
| 1499 |
return NID_auth_rsa; |
| 1500 |
case SSL_aECDSA: |
| 1501 |
return NID_auth_ecdsa; |
| 1502 |
case SSL_aPSK: |
| 1503 |
return NID_auth_psk; |
| 1504 |
case SSL_aGENERIC: |
| 1505 |
return NID_auth_any; |
| 1506 |
} |
| 1507 |
assert(0); |
| 1508 |
return NID_undef; |
| 1509 |
} |
| 1510 |
|
| 1511 |
const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *cipher) { |
| 1512 |
switch (cipher->algorithm_prf) { |
| 1513 |
case SSL_HANDSHAKE_MAC_DEFAULT: |
| 1514 |
return EVP_md5_sha1(); |
| 1515 |
case SSL_HANDSHAKE_MAC_SHA256: |
| 1516 |
return EVP_sha256(); |
| 1517 |
case SSL_HANDSHAKE_MAC_SHA384: |
| 1518 |
return EVP_sha384(); |
| 1519 |
} |
| 1520 |
assert(0); |
| 1521 |
return NULL; |
| 1522 |
} |
| 1523 |
|
| 1524 |
int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *cipher) { |
| 1525 |
const EVP_MD *md = SSL_CIPHER_get_handshake_digest(cipher); |
| 1526 |
if (md == NULL) { |
| 1527 |
return NID_undef; |
| 1528 |
} |
| 1529 |
return EVP_MD_nid(md); |
| 1530 |
} |
| 1531 |
|
| 1532 |
int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) { |
| 1533 |
return (cipher->algorithm_enc & SSL_eNULL) == 0 && |
| 1534 |
cipher->algorithm_mac != SSL_AEAD; |
| 1535 |
} |
| 1536 |
|
| 1537 |
uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) { |
| 1538 |
if (cipher->algorithm_mkey == SSL_kGENERIC || |
| 1539 |
cipher->algorithm_auth == SSL_aGENERIC) { |
| 1540 |
return TLS1_3_VERSION; |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) { |
| 1544 |
// Cipher suites before TLS 1.2 use the default PRF, while all those added |
| 1545 |
// afterwards specify a particular hash. |
| 1546 |
return TLS1_2_VERSION; |
| 1547 |
} |
| 1548 |
return SSL3_VERSION; |
| 1549 |
} |
| 1550 |
|
| 1551 |
uint16_t SSL_CIPHER_get_max_version(const SSL_CIPHER *cipher) { |
| 1552 |
if (cipher->algorithm_mkey == SSL_kGENERIC || |
| 1553 |
cipher->algorithm_auth == SSL_aGENERIC) { |
| 1554 |
return TLS1_3_VERSION; |
| 1555 |
} |
| 1556 |
return TLS1_2_VERSION; |
| 1557 |
} |
| 1558 |
|
| 1559 |
static const char* kUnknownCipher = "(NONE)"; |
| 1560 |
|
| 1561 |
// return the actual cipher being used |
| 1562 |
const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) { |
| 1563 |
if (cipher != NULL) { |
| 1564 |
return cipher->name; |
| 1565 |
} |
| 1566 |
|
| 1567 |
return kUnknownCipher; |
| 1568 |
} |
| 1569 |
|
| 1570 |
const char *SSL_CIPHER_standard_name(const SSL_CIPHER *cipher) { |
| 1571 |
return cipher->standard_name; |
| 1572 |
} |
| 1573 |
|
| 1574 |
const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
| 1575 |
if (cipher == NULL) { |
| 1576 |
return ""; |
| 1577 |
} |
| 1578 |
|
| 1579 |
switch (cipher->algorithm_mkey) { |
| 1580 |
case SSL_kRSA: |
| 1581 |
return "RSA"; |
| 1582 |
|
| 1583 |
case SSL_kECDHE: |
| 1584 |
switch (cipher->algorithm_auth) { |
| 1585 |
case SSL_aECDSA: |
| 1586 |
return "ECDHE_ECDSA"; |
| 1587 |
case SSL_aRSA: |
| 1588 |
return "ECDHE_RSA"; |
| 1589 |
case SSL_aPSK: |
| 1590 |
return "ECDHE_PSK"; |
| 1591 |
default: |
| 1592 |
assert(0); |
| 1593 |
return "UNKNOWN"; |
| 1594 |
} |
| 1595 |
|
| 1596 |
case SSL_kPSK: |
| 1597 |
assert(cipher->algorithm_auth == SSL_aPSK); |
| 1598 |
return "PSK"; |
| 1599 |
|
| 1600 |
case SSL_kGENERIC: |
| 1601 |
assert(cipher->algorithm_auth == SSL_aGENERIC); |
| 1602 |
return "GENERIC"; |
| 1603 |
|
| 1604 |
default: |
| 1605 |
assert(0); |
| 1606 |
return "UNKNOWN"; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) { |
| 1611 |
if (cipher == NULL) { |
| 1612 |
return 0; |
| 1613 |
} |
| 1614 |
|
| 1615 |
int alg_bits, strength_bits; |
| 1616 |
switch (cipher->algorithm_enc) { |
| 1617 |
case SSL_AES128: |
| 1618 |
case SSL_AES128GCM: |
| 1619 |
alg_bits = 128; |
| 1620 |
strength_bits = 128; |
| 1621 |
break; |
| 1622 |
|
| 1623 |
case SSL_AES256: |
| 1624 |
case SSL_AES256GCM: |
| 1625 |
case SSL_CHACHA20POLY1305: |
| 1626 |
alg_bits = 256; |
| 1627 |
strength_bits = 256; |
| 1628 |
break; |
| 1629 |
|
| 1630 |
case SSL_3DES: |
| 1631 |
alg_bits = 168; |
| 1632 |
strength_bits = 112; |
| 1633 |
break; |
| 1634 |
|
| 1635 |
case SSL_eNULL: |
| 1636 |
alg_bits = 0; |
| 1637 |
strength_bits = 0; |
| 1638 |
break; |
| 1639 |
|
| 1640 |
default: |
| 1641 |
assert(0); |
| 1642 |
alg_bits = 0; |
| 1643 |
strength_bits = 0; |
| 1644 |
} |
| 1645 |
|
| 1646 |
if (out_alg_bits != NULL) { |
| 1647 |
*out_alg_bits = alg_bits; |
| 1648 |
} |
| 1649 |
return strength_bits; |
| 1650 |
} |
| 1651 |
|
| 1652 |
const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
| 1653 |
int len) { |
| 1654 |
const char *kx, *au, *enc, *mac; |
| 1655 |
uint32_t alg_mkey, alg_auth, alg_enc, alg_mac; |
| 1656 |
|
| 1657 |
alg_mkey = cipher->algorithm_mkey; |
| 1658 |
alg_auth = cipher->algorithm_auth; |
| 1659 |
alg_enc = cipher->algorithm_enc; |
| 1660 |
alg_mac = cipher->algorithm_mac; |
| 1661 |
|
| 1662 |
switch (alg_mkey) { |
| 1663 |
case SSL_kRSA: |
| 1664 |
kx = "RSA"; |
| 1665 |
break; |
| 1666 |
|
| 1667 |
case SSL_kECDHE: |
| 1668 |
kx = "ECDH"; |
| 1669 |
break; |
| 1670 |
|
| 1671 |
case SSL_kPSK: |
| 1672 |
kx = "PSK"; |
| 1673 |
break; |
| 1674 |
|
| 1675 |
case SSL_kGENERIC: |
| 1676 |
kx = "GENERIC"; |
| 1677 |
break; |
| 1678 |
|
| 1679 |
default: |
| 1680 |
kx = "unknown"; |
| 1681 |
} |
| 1682 |
|
| 1683 |
switch (alg_auth) { |
| 1684 |
case SSL_aRSA: |
| 1685 |
au = "RSA"; |
| 1686 |
break; |
| 1687 |
|
| 1688 |
case SSL_aECDSA: |
| 1689 |
au = "ECDSA"; |
| 1690 |
break; |
| 1691 |
|
| 1692 |
case SSL_aPSK: |
| 1693 |
au = "PSK"; |
| 1694 |
break; |
| 1695 |
|
| 1696 |
case SSL_aGENERIC: |
| 1697 |
au = "GENERIC"; |
| 1698 |
break; |
| 1699 |
|
| 1700 |
default: |
| 1701 |
au = "unknown"; |
| 1702 |
break; |
| 1703 |
} |
| 1704 |
|
| 1705 |
switch (alg_enc) { |
| 1706 |
case SSL_3DES: |
| 1707 |
enc = "3DES(168)"; |
| 1708 |
break; |
| 1709 |
|
| 1710 |
case SSL_AES128: |
| 1711 |
enc = "AES(128)"; |
| 1712 |
break; |
| 1713 |
|
| 1714 |
case SSL_AES256: |
| 1715 |
enc = "AES(256)"; |
| 1716 |
break; |
| 1717 |
|
| 1718 |
case SSL_AES128GCM: |
| 1719 |
enc = "AESGCM(128)"; |
| 1720 |
break; |
| 1721 |
|
| 1722 |
case SSL_AES256GCM: |
| 1723 |
enc = "AESGCM(256)"; |
| 1724 |
break; |
| 1725 |
|
| 1726 |
case SSL_CHACHA20POLY1305: |
| 1727 |
enc = "ChaCha20-Poly1305"; |
| 1728 |
break; |
| 1729 |
|
| 1730 |
case SSL_eNULL: |
| 1731 |
enc="None"; |
| 1732 |
break; |
| 1733 |
|
| 1734 |
default: |
| 1735 |
enc = "unknown"; |
| 1736 |
break; |
| 1737 |
} |
| 1738 |
|
| 1739 |
switch (alg_mac) { |
| 1740 |
case SSL_SHA1: |
| 1741 |
mac = "SHA1"; |
| 1742 |
break; |
| 1743 |
|
| 1744 |
case SSL_SHA256: |
| 1745 |
mac = "SHA256"; |
| 1746 |
break; |
| 1747 |
|
| 1748 |
case SSL_SHA384: |
| 1749 |
mac = "SHA384"; |
| 1750 |
break; |
| 1751 |
|
| 1752 |
case SSL_AEAD: |
| 1753 |
mac = "AEAD"; |
| 1754 |
break; |
| 1755 |
|
| 1756 |
default: |
| 1757 |
mac = "unknown"; |
| 1758 |
break; |
| 1759 |
} |
| 1760 |
|
| 1761 |
if (buf == NULL) { |
| 1762 |
len = 128; |
| 1763 |
buf = (char *)OPENSSL_malloc(len); |
| 1764 |
if (buf == NULL) { |
| 1765 |
return NULL; |
| 1766 |
} |
| 1767 |
} else if (len < 128) { |
| 1768 |
return "Buffer too small"; |
| 1769 |
} |
| 1770 |
|
| 1771 |
snprintf(buf, len, "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n", cipher->name, |
| 1772 |
kx, au, enc, mac); |
| 1773 |
return buf; |
| 1774 |
} |
| 1775 |
|
| 1776 |
bool tls_print_all_supported_cipher_suites(bool use_openssl_name) { |
| 1777 |
|
| 1778 |
for (const SSL_CIPHER cipher : kCiphers) { |
| 1779 |
fprintf(stdout, "%s\n", use_openssl_name ? SSL_CIPHER_get_name(&cipher) |
| 1780 |
: SSL_CIPHER_standard_name(&cipher)); |
| 1781 |
} |
| 1782 |
|
| 1783 |
return true; |
| 1784 |
} |
| 1785 |
|
| 1786 |
const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) { |
| 1787 |
switch (SSL_CIPHER_get_min_version(cipher)) { |
| 1788 |
case TLS1_2_VERSION: |
| 1789 |
case DTLS1_2_VERSION: |
| 1790 |
return "TLSv1.2"; |
| 1791 |
case TLS1_3_VERSION: |
| 1792 |
return "TLSv1.3"; |
| 1793 |
default: |
| 1794 |
return "TLSv1/SSLv3"; |
| 1795 |
} |
| 1796 |
} |
| 1797 |
|
| 1798 |
STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void) { return NULL; } |
| 1799 |
|
| 1800 |
int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; } |
| 1801 |
|
| 1802 |
const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; } |
| 1803 |
|
| 1804 |
const char *SSL_COMP_get0_name(const SSL_COMP *comp) { return comp->name; } |
| 1805 |
|
| 1806 |
int SSL_COMP_get_id(const SSL_COMP *comp) { return comp->id; } |
| 1807 |
|
| 1808 |
void SSL_COMP_free_compression_methods(void) {} |
| 1809 |
|
| 1810 |
size_t SSL_get_all_cipher_names(const char **out, size_t max_out) { |
| 1811 |
return GetAllNames(out, max_out, MakeConstSpan(&kUnknownCipher, 1), |
| 1812 |
&SSL_CIPHER::name, MakeConstSpan(kCiphers)); |
| 1813 |
} |
| 1814 |
|
| 1815 |
size_t SSL_get_all_standard_cipher_names(const char **out, size_t max_out) { |
| 1816 |
return GetAllNames(out, max_out, Span<const char *>(), |
| 1817 |
&SSL_CIPHER::standard_name, MakeConstSpan(kCiphers)); |
| 1818 |
} |