Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 142 | #include <stdio.h> |
| 143 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 145 | #include <openssl/err.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 146 | #include <openssl/md5.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | #include <openssl/mem.h> |
| 148 | #include <openssl/obj.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 149 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 151 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 154 | struct handshake_digest { |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 155 | uint32_t mask; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 156 | const EVP_MD *(*md_func)(void); |
David Benjamin | d633d63 | 2014-08-30 14:43:27 -0400 | [diff] [blame] | 157 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 159 | static const struct handshake_digest ssl_handshake_digests[SSL_MAX_DIGEST] = { |
| 160 | {SSL_HANDSHAKE_MAC_MD5, EVP_md5}, |
| 161 | {SSL_HANDSHAKE_MAC_SHA, EVP_sha1}, |
| 162 | {SSL_HANDSHAKE_MAC_SHA256, EVP_sha256}, |
| 163 | {SSL_HANDSHAKE_MAC_SHA384, EVP_sha384}, |
| 164 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 166 | #define CIPHER_ADD 1 |
| 167 | #define CIPHER_KILL 2 |
| 168 | #define CIPHER_DEL 3 |
| 169 | #define CIPHER_ORD 4 |
| 170 | #define CIPHER_SPECIAL 5 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 172 | typedef struct cipher_order_st { |
| 173 | const SSL_CIPHER *cipher; |
| 174 | int active; |
| 175 | int dead; |
| 176 | int in_group; |
| 177 | struct cipher_order_st *next, *prev; |
| 178 | } CIPHER_ORDER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 180 | typedef struct cipher_alias_st { |
| 181 | /* name is the name of the cipher alias. */ |
| 182 | const char *name; |
| 183 | |
| 184 | /* The following fields are bitmasks for the corresponding fields on |
| 185 | * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the |
| 186 | * bit corresponding to the cipher's value is set to 1. If any bitmask is |
| 187 | * all zeroes, the alias matches nothing. Use |~0u| for the default value. */ |
| 188 | uint32_t algorithm_mkey; |
| 189 | uint32_t algorithm_auth; |
| 190 | uint32_t algorithm_enc; |
| 191 | uint32_t algorithm_mac; |
| 192 | uint32_t algorithm_ssl; |
| 193 | uint32_t algo_strength; |
| 194 | } CIPHER_ALIAS; |
| 195 | |
| 196 | static const CIPHER_ALIAS kCipherAliases[] = |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 197 | { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 198 | {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 200 | /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 202 | /* key exchange aliases |
| 203 | * (some of those using only a single bit here combine |
| 204 | * multiple key exchange algs according to the RFCs, |
| 205 | * e.g. kEDH combines DHE_DSS and DHE_RSA) */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 206 | {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 208 | {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 209 | {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 210 | {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 212 | {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 213 | {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 214 | {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 216 | {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 218 | /* server authentication aliases */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 219 | {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u}, |
| 220 | {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u}, |
| 221 | {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u}, |
| 222 | {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 224 | /* aliases combining key exchange and server authentication */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 225 | {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 226 | {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 227 | {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 228 | {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 229 | {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u}, |
| 230 | {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | /* symmetric encryption aliases */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 233 | {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u}, |
| 234 | {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u}, |
| 235 | {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u}, |
| 236 | {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u}, |
| 237 | {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u}, |
| 238 | {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u}, |
| 239 | {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 241 | /* MAC aliases */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 242 | {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u}, |
| 243 | {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u}, |
| 244 | {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u}, |
| 245 | {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u}, |
| 246 | {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 248 | /* protocol version aliases */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 249 | {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u}, |
| 250 | {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u}, |
| 251 | {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 253 | /* strength classes */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 254 | {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM}, |
| 255 | {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH}, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | /* FIPS 140-2 approved ciphersuite */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 257 | {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS}, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 260 | #define NUM_CIPHER_ALIASES (sizeof(kCipherAliases) / sizeof(kCipherAliases[0])) |
| 261 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 262 | int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
| 263 | size_t *out_mac_secret_len, |
| 264 | size_t *out_fixed_iv_len, |
| 265 | const SSL_CIPHER *cipher, uint16_t version) { |
| 266 | *out_aead = NULL; |
| 267 | *out_mac_secret_len = 0; |
| 268 | *out_fixed_iv_len = 0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 270 | switch (cipher->algorithm_enc) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 271 | case SSL_AES128GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 272 | *out_aead = EVP_aead_aes_128_gcm(); |
| 273 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | return 1; |
| 275 | |
| 276 | case SSL_AES256GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 277 | *out_aead = EVP_aead_aes_256_gcm(); |
| 278 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 279 | return 1; |
| 280 | |
| 281 | case SSL_CHACHA20POLY1305: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 282 | *out_aead = EVP_aead_chacha20_poly1305(); |
| 283 | *out_fixed_iv_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | return 1; |
| 285 | |
| 286 | case SSL_RC4: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 287 | switch (cipher->algorithm_mac) { |
| 288 | case SSL_MD5: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 289 | if (version == SSL3_VERSION) { |
| 290 | *out_aead = EVP_aead_rc4_md5_ssl3(); |
| 291 | } else { |
| 292 | *out_aead = EVP_aead_rc4_md5_tls(); |
| 293 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 294 | *out_mac_secret_len = MD5_DIGEST_LENGTH; |
| 295 | return 1; |
| 296 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 297 | if (version == SSL3_VERSION) { |
| 298 | *out_aead = EVP_aead_rc4_sha1_ssl3(); |
| 299 | } else { |
| 300 | *out_aead = EVP_aead_rc4_sha1_tls(); |
| 301 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 302 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 303 | return 1; |
| 304 | default: |
| 305 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 306 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 308 | case SSL_AES128: |
| 309 | switch (cipher->algorithm_mac) { |
| 310 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 311 | if (version == SSL3_VERSION) { |
| 312 | *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3(); |
| 313 | *out_fixed_iv_len = 16; |
| 314 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 315 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(); |
| 316 | *out_fixed_iv_len = 16; |
| 317 | } else { |
| 318 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls(); |
| 319 | } |
| 320 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 321 | return 1; |
| 322 | case SSL_SHA256: |
| 323 | *out_aead = EVP_aead_aes_128_cbc_sha256_tls(); |
| 324 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 325 | return 1; |
| 326 | default: |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | case SSL_AES256: |
| 331 | switch (cipher->algorithm_mac) { |
| 332 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 333 | if (version == SSL3_VERSION) { |
| 334 | *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3(); |
| 335 | *out_fixed_iv_len = 16; |
| 336 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 337 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(); |
| 338 | *out_fixed_iv_len = 16; |
| 339 | } else { |
| 340 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls(); |
| 341 | } |
| 342 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 343 | return 1; |
| 344 | case SSL_SHA256: |
| 345 | *out_aead = EVP_aead_aes_256_cbc_sha256_tls(); |
| 346 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 347 | return 1; |
| 348 | case SSL_SHA384: |
| 349 | *out_aead = EVP_aead_aes_256_cbc_sha384_tls(); |
| 350 | *out_mac_secret_len = SHA384_DIGEST_LENGTH; |
| 351 | return 1; |
| 352 | default: |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | case SSL_3DES: |
| 357 | switch (cipher->algorithm_mac) { |
| 358 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 359 | if (version == SSL3_VERSION) { |
| 360 | *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3(); |
| 361 | *out_fixed_iv_len = 8; |
| 362 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 363 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(); |
| 364 | *out_fixed_iv_len = 8; |
| 365 | } else { |
| 366 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls(); |
| 367 | } |
| 368 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 369 | return 1; |
| 370 | default: |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | default: |
| 375 | return 0; |
| 376 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 377 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 379 | int ssl_get_handshake_digest(size_t idx, uint32_t *mask, const EVP_MD **md) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 380 | if (idx >= SSL_MAX_DIGEST) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | *mask = ssl_handshake_digests[idx].mask; |
| 384 | *md = ssl_handshake_digests[idx].md_func(); |
| 385 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | #define ITEM_SEP(a) \ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 391 | /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the |
| 392 | * |buf_len| bytes at |buf|. */ |
| 393 | static int rule_equals(const char *rule, const char *buf, size_t buf_len) { |
| 394 | /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */ |
| 395 | return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
| 396 | } |
| 397 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 399 | CIPHER_ORDER **tail) { |
| 400 | if (curr == *tail) { |
| 401 | return; |
| 402 | } |
| 403 | if (curr == *head) { |
| 404 | *head = curr->next; |
| 405 | } |
| 406 | if (curr->prev != NULL) { |
| 407 | curr->prev->next = curr->next; |
| 408 | } |
| 409 | if (curr->next != NULL) { |
| 410 | curr->next->prev = curr->prev; |
| 411 | } |
| 412 | (*tail)->next = curr; |
| 413 | curr->prev = *tail; |
| 414 | curr->next = NULL; |
| 415 | *tail = curr; |
| 416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | |
| 418 | static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | CIPHER_ORDER **tail) { |
| 420 | if (curr == *head) { |
| 421 | return; |
| 422 | } |
| 423 | if (curr == *tail) { |
| 424 | *tail = curr->prev; |
| 425 | } |
| 426 | if (curr->next != NULL) { |
| 427 | curr->next->prev = curr->prev; |
| 428 | } |
| 429 | if (curr->prev != NULL) { |
| 430 | curr->prev->next = curr->next; |
| 431 | } |
| 432 | (*head)->prev = curr; |
| 433 | curr->next = *head; |
| 434 | curr->prev = NULL; |
| 435 | *head = curr; |
| 436 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 437 | |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 438 | static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 439 | size_t num_of_ciphers, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 440 | CIPHER_ORDER *co_list, |
| 441 | CIPHER_ORDER **head_p, |
| 442 | CIPHER_ORDER **tail_p) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 443 | size_t i, co_list_num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 445 | /* We have num_of_ciphers descriptions compiled in, depending on the method |
| 446 | * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a |
| 447 | * linked list with at most num entries. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 449 | /* Get the initial list of ciphers */ |
| 450 | co_list_num = 0; /* actual count of ciphers */ |
| 451 | for (i = 0; i < num_of_ciphers; i++) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 452 | const SSL_CIPHER *c = ssl_method->get_cipher(i); |
| 453 | if (c != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | co_list[co_list_num].cipher = c; |
| 455 | co_list[co_list_num].next = NULL; |
| 456 | co_list[co_list_num].prev = NULL; |
| 457 | co_list[co_list_num].active = 0; |
| 458 | co_list[co_list_num].in_group = 0; |
| 459 | co_list_num++; |
| 460 | } |
| 461 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 463 | /* Prepare linked list from list entries. */ |
| 464 | if (co_list_num > 0) { |
| 465 | co_list[0].prev = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | if (co_list_num > 1) { |
| 468 | co_list[0].next = &co_list[1]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 470 | for (i = 1; i < co_list_num - 1; i++) { |
| 471 | co_list[i].prev = &co_list[i - 1]; |
| 472 | co_list[i].next = &co_list[i + 1]; |
| 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | co_list[co_list_num - 1].prev = &co_list[co_list_num - 2]; |
| 476 | } |
| 477 | |
| 478 | co_list[co_list_num - 1].next = NULL; |
| 479 | |
| 480 | *head_p = &co_list[0]; |
| 481 | *tail_p = &co_list[co_list_num - 1]; |
| 482 | } |
| 483 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 485 | /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its |
| 486 | * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new |
| 487 | * head and tail of the list to |*head_p| and |*tail_p|, respectively. |
| 488 | * |
| 489 | * - If |cipher_id| is non-zero, only that cipher is selected. |
| 490 | * - Otherwise, if |strength_bits| is non-negative, it selects ciphers |
| 491 | * of that strength. |
| 492 | * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and |
| 493 | * |algo_strength|. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 494 | static void ssl_cipher_apply_rule( |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 495 | uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth, |
| 496 | uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl, |
| 497 | uint32_t algo_strength, int rule, int strength_bits, int in_group, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 498 | CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) { |
| 499 | CIPHER_ORDER *head, *tail, *curr, *next, *last; |
| 500 | const SSL_CIPHER *cp; |
| 501 | int reverse = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 503 | if (cipher_id == 0 && strength_bits == -1 && |
| 504 | (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 || |
| 505 | alg_ssl == 0 || algo_strength == 0)) { |
| 506 | /* The rule matches nothing, so bail early. */ |
| 507 | return; |
| 508 | } |
| 509 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 510 | if (rule == CIPHER_DEL) { |
| 511 | /* needed to maintain sorting between currently deleted ciphers */ |
| 512 | reverse = 1; |
| 513 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 515 | head = *head_p; |
| 516 | tail = *tail_p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 518 | if (reverse) { |
| 519 | next = tail; |
| 520 | last = head; |
| 521 | } else { |
| 522 | next = head; |
| 523 | last = tail; |
| 524 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 526 | curr = NULL; |
| 527 | for (;;) { |
| 528 | if (curr == last) { |
| 529 | break; |
| 530 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 531 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 532 | curr = next; |
| 533 | if (curr == NULL) { |
| 534 | break; |
| 535 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 536 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 537 | next = reverse ? curr->prev : curr->next; |
| 538 | cp = curr->cipher; |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 539 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 540 | /* Selection criteria is either a specific cipher, the value of |
| 541 | * |strength_bits|, or the algorithms used. */ |
| 542 | if (cipher_id != 0) { |
| 543 | if (cipher_id != cp->id) { |
| 544 | continue; |
| 545 | } |
| 546 | } else if (strength_bits >= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 547 | if (strength_bits != cp->strength_bits) { |
| 548 | continue; |
| 549 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 550 | } else if (!(alg_mkey & cp->algorithm_mkey) || |
| 551 | !(alg_auth & cp->algorithm_auth) || |
| 552 | !(alg_enc & cp->algorithm_enc) || |
| 553 | !(alg_mac & cp->algorithm_mac) || |
| 554 | !(alg_ssl & cp->algorithm_ssl) || |
| 555 | !(algo_strength & cp->algo_strength)) { |
| 556 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 557 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 558 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 559 | /* add the cipher if it has not been added yet. */ |
| 560 | if (rule == CIPHER_ADD) { |
| 561 | /* reverse == 0 */ |
| 562 | if (!curr->active) { |
| 563 | ll_append_tail(&head, curr, &tail); |
| 564 | curr->active = 1; |
| 565 | curr->in_group = in_group; |
| 566 | } |
| 567 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 569 | /* Move the added cipher to this location */ |
| 570 | else if (rule == CIPHER_ORD) { |
| 571 | /* reverse == 0 */ |
| 572 | if (curr->active) { |
| 573 | ll_append_tail(&head, curr, &tail); |
| 574 | curr->in_group = 0; |
| 575 | } |
| 576 | } else if (rule == CIPHER_DEL) { |
| 577 | /* reverse == 1 */ |
| 578 | if (curr->active) { |
| 579 | /* most recently deleted ciphersuites get best positions |
| 580 | * for any future CIPHER_ADD (note that the CIPHER_DEL loop |
| 581 | * works in reverse to maintain the order) */ |
| 582 | ll_append_head(&head, curr, &tail); |
| 583 | curr->active = 0; |
| 584 | curr->in_group = 0; |
| 585 | } |
| 586 | } else if (rule == CIPHER_KILL) { |
| 587 | /* reverse == 0 */ |
| 588 | if (head == curr) { |
| 589 | head = curr->next; |
| 590 | } else { |
| 591 | curr->prev->next = curr->next; |
| 592 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 594 | if (tail == curr) { |
| 595 | tail = curr->prev; |
| 596 | } |
| 597 | curr->active = 0; |
| 598 | if (curr->next != NULL) { |
| 599 | curr->next->prev = curr->prev; |
| 600 | } |
| 601 | if (curr->prev != NULL) { |
| 602 | curr->prev->next = curr->next; |
| 603 | } |
| 604 | curr->next = NULL; |
| 605 | curr->prev = NULL; |
| 606 | } |
| 607 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 608 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 609 | *head_p = head; |
| 610 | *tail_p = tail; |
| 611 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 612 | |
| 613 | static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 614 | CIPHER_ORDER **tail_p) { |
| 615 | int max_strength_bits, i, *number_uses; |
| 616 | CIPHER_ORDER *curr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 618 | /* This routine sorts the ciphers with descending strength. The sorting must |
| 619 | * keep the pre-sorted sequence, so we apply the normal sorting routine as |
| 620 | * '+' movement to the end of the list. */ |
| 621 | max_strength_bits = 0; |
| 622 | curr = *head_p; |
| 623 | while (curr != NULL) { |
| 624 | if (curr->active && curr->cipher->strength_bits > max_strength_bits) { |
| 625 | max_strength_bits = curr->cipher->strength_bits; |
| 626 | } |
| 627 | curr = curr->next; |
| 628 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 629 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 630 | number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); |
| 631 | if (!number_uses) { |
| 632 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE); |
| 633 | return 0; |
| 634 | } |
| 635 | memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 636 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | /* Now find the strength_bits values actually used. */ |
| 638 | curr = *head_p; |
| 639 | while (curr != NULL) { |
| 640 | if (curr->active) { |
| 641 | number_uses[curr->cipher->strength_bits]++; |
| 642 | } |
| 643 | curr = curr->next; |
| 644 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 646 | /* Go through the list of used strength_bits values in descending order. */ |
| 647 | for (i = max_strength_bits; i >= 0; i--) { |
| 648 | if (number_uses[i] > 0) { |
| 649 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, |
| 650 | tail_p); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | OPENSSL_free(number_uses); |
| 655 | return 1; |
| 656 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 658 | static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method, |
| 659 | const char *rule_str, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 660 | CIPHER_ORDER **head_p, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 661 | CIPHER_ORDER **tail_p) { |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 662 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | const char *l, *buf; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 664 | int multi, rule, retval, ok, in_group = 0, has_group = 0; |
| 665 | size_t j, buf_len; |
| 666 | uint32_t cipher_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 667 | char ch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 668 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 669 | retval = 1; |
| 670 | l = rule_str; |
| 671 | for (;;) { |
| 672 | ch = *l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 674 | if (ch == '\0') { |
| 675 | break; /* done */ |
| 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 678 | if (in_group) { |
| 679 | if (ch == ']') { |
| 680 | if (!in_group) { |
| 681 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 682 | SSL_R_UNEXPECTED_GROUP_CLOSE); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 683 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 684 | break; |
| 685 | } |
| 686 | if (*tail_p) { |
| 687 | (*tail_p)->in_group = 0; |
| 688 | } |
| 689 | in_group = 0; |
| 690 | l++; |
| 691 | continue; |
| 692 | } |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 693 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 694 | if (ch == '|') { |
| 695 | rule = CIPHER_ADD; |
| 696 | l++; |
| 697 | continue; |
| 698 | } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && |
| 699 | !(ch >= '0' && ch <= '9')) { |
| 700 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 701 | SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 702 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 703 | break; |
| 704 | } else { |
| 705 | rule = CIPHER_ADD; |
| 706 | } |
| 707 | } else if (ch == '-') { |
| 708 | rule = CIPHER_DEL; |
| 709 | l++; |
| 710 | } else if (ch == '+') { |
| 711 | rule = CIPHER_ORD; |
| 712 | l++; |
| 713 | } else if (ch == '!') { |
| 714 | rule = CIPHER_KILL; |
| 715 | l++; |
| 716 | } else if (ch == '@') { |
| 717 | rule = CIPHER_SPECIAL; |
| 718 | l++; |
| 719 | } else if (ch == '[') { |
| 720 | if (in_group) { |
| 721 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 722 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | in_group = 1; |
| 726 | has_group = 1; |
| 727 | l++; |
| 728 | continue; |
| 729 | } else { |
| 730 | rule = CIPHER_ADD; |
| 731 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 733 | /* If preference groups are enabled, the only legal operator is +. |
| 734 | * Otherwise the in_group bits will get mixed up. */ |
| 735 | if (has_group && rule != CIPHER_ADD) { |
| 736 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 737 | SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 738 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 739 | break; |
| 740 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 741 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 742 | if (ITEM_SEP(ch)) { |
| 743 | l++; |
| 744 | continue; |
| 745 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 746 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 747 | multi = 0; |
| 748 | cipher_id = 0; |
| 749 | alg_mkey = ~0u; |
| 750 | alg_auth = ~0u; |
| 751 | alg_enc = ~0u; |
| 752 | alg_mac = ~0u; |
| 753 | alg_ssl = ~0u; |
| 754 | algo_strength = ~0u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 755 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 756 | for (;;) { |
| 757 | ch = *l; |
| 758 | buf = l; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 759 | buf_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 760 | while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || |
| 761 | ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) { |
| 762 | ch = *(++l); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 763 | buf_len++; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 764 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 765 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 766 | if (buf_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 767 | /* We hit something we cannot deal with, it is no command or separator |
| 768 | * nor alphanumeric, so we call this an error. */ |
| 769 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 770 | SSL_R_INVALID_COMMAND); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 771 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 772 | l++; |
| 773 | break; |
| 774 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 775 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 776 | if (rule == CIPHER_SPECIAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 777 | break; |
| 778 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 779 | |
| 780 | /* Look for a matching exact cipher. These aren't allowed in multipart |
| 781 | * rules. */ |
| 782 | if (!multi && ch != '+') { |
| 783 | size_t num_ciphers = ssl_method->num_ciphers(); |
| 784 | for (j = 0; j < num_ciphers; j++) { |
| 785 | const SSL_CIPHER *cipher = ssl_method->get_cipher(j); |
| 786 | if (cipher != NULL && rule_equals(cipher->name, buf, buf_len)) { |
| 787 | cipher_id = cipher->id; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | if (cipher_id == 0) { |
| 793 | /* If not an exact cipher, look for a matching cipher alias. */ |
| 794 | for (j = 0; j < NUM_CIPHER_ALIASES; j++) { |
| 795 | if (rule_equals(kCipherAliases[j].name, buf, buf_len)) { |
| 796 | alg_mkey &= kCipherAliases[j].algorithm_mkey; |
| 797 | alg_auth &= kCipherAliases[j].algorithm_auth; |
| 798 | alg_enc &= kCipherAliases[j].algorithm_enc; |
| 799 | alg_mac &= kCipherAliases[j].algorithm_mac; |
| 800 | alg_ssl &= kCipherAliases[j].algorithm_ssl; |
| 801 | algo_strength &= kCipherAliases[j].algo_strength; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | if (j == NUM_CIPHER_ALIASES) { |
| 806 | alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* Check for a multipart rule. */ |
| 811 | if (ch != '+') { |
| 812 | break; |
| 813 | } |
| 814 | l++; |
| 815 | multi = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 816 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 817 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 818 | /* Ok, we have the rule, now apply it. */ |
| 819 | if (rule == CIPHER_SPECIAL) { |
| 820 | /* special command */ |
| 821 | ok = 0; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 822 | if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 823 | ok = ssl_cipher_strength_sort(head_p, tail_p); |
| 824 | } else { |
| 825 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 826 | SSL_R_INVALID_COMMAND); |
| 827 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 829 | if (ok == 0) { |
| 830 | retval = 0; |
| 831 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 832 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 833 | /* We do not support any "multi" options together with "@", so throw away |
| 834 | * the rest of the command, if any left, until end or ':' is found. */ |
| 835 | while (*l != '\0' && !ITEM_SEP(*l)) { |
| 836 | l++; |
| 837 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 838 | } else { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 839 | ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac, |
| 840 | alg_ssl, algo_strength, rule, -1, in_group, head_p, |
| 841 | tail_p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 842 | } |
| 843 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 844 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 845 | if (in_group) { |
| 846 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND); |
| 847 | retval = 0; |
| 848 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 849 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 850 | return retval; |
| 851 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 853 | STACK_OF(SSL_CIPHER) * |
| 854 | ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method, |
| 855 | struct ssl_cipher_preference_list_st **cipher_list, |
| 856 | STACK_OF(SSL_CIPHER) * *cipher_list_by_id, |
| 857 | const char *rule_str, CERT *c) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 858 | int ok; |
| 859 | size_t num_of_ciphers; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 860 | STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL; |
| 861 | const char *rule_p; |
| 862 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 863 | uint8_t *in_group_flags = NULL; |
| 864 | unsigned int num_in_group_flags = 0; |
| 865 | struct ssl_cipher_preference_list_st *pref_list = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 866 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 867 | /* Return with error if nothing to do. */ |
| 868 | if (rule_str == NULL || cipher_list == NULL) { |
| 869 | return NULL; |
| 870 | } |
David Benjamin | 5213df4 | 2014-08-20 14:19:54 -0400 | [diff] [blame] | 871 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 872 | /* Now we have to collect the available ciphers from the compiled in ciphers. |
| 873 | * We cannot get more than the number compiled in, so it is used for |
| 874 | * allocation. */ |
| 875 | num_of_ciphers = ssl_method->num_ciphers(); |
| 876 | co_list = |
| 877 | (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers); |
| 878 | if (co_list == NULL) { |
| 879 | OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE); |
| 880 | return NULL; |
| 881 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 882 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 883 | ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers, co_list, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 885 | /* Now arrange all ciphers by preference: |
| 886 | * TODO(davidben): Compute this order once and copy it. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 888 | /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other |
| 889 | * key exchange mechanisms */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 890 | ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u, |
| 891 | CIPHER_ADD, -1, 0, &head, &tail); |
| 892 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 893 | 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 894 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, |
| 895 | 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 897 | /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer |
| 898 | * CHACHA20 unless there is hardware support for fast and constant-time |
| 899 | * AES_GCM. */ |
| 900 | if (EVP_has_aes_hardware()) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 901 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 902 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 903 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 904 | -1, 0, &head, &tail); |
| 905 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 906 | CIPHER_ADD, -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 907 | } else { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 908 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 909 | CIPHER_ADD, -1, 0, &head, &tail); |
| 910 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 911 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 912 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 913 | -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 915 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 916 | /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA, |
| 917 | * RC4_128_MD5, 3DES_EDE_CBC_SHA. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 918 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 919 | 0, &head, &tail); |
| 920 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 921 | 0, &head, &tail); |
| 922 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD, |
| 923 | -1, 0, &head, &tail); |
| 924 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1, |
| 925 | 0, &head, &tail); |
| 926 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 927 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 928 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 929 | /* Temporarily enable everything else for sorting */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 930 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0, |
| 931 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 932 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 933 | /* Move ciphers without forward secrecy to the end. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 934 | ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u, |
| 935 | CIPHER_ORD, -1, 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 936 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 937 | /* Now disable everything (maintaining the ordering!) */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 938 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0, |
| 939 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 941 | /* If the rule_string begins with DEFAULT, apply the default rule before |
| 942 | * using the (possibly available) additional rules. */ |
| 943 | ok = 1; |
| 944 | rule_p = rule_str; |
| 945 | if (strncmp(rule_str, "DEFAULT", 7) == 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 946 | ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head, |
| 947 | &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 948 | rule_p += 7; |
| 949 | if (*rule_p == ':') { |
| 950 | rule_p++; |
| 951 | } |
| 952 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 953 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 954 | if (ok && strlen(rule_p) > 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame^] | 955 | ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 956 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 957 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 958 | if (!ok) { |
| 959 | goto err; |
| 960 | } |
| 961 | |
| 962 | /* Allocate new "cipherstack" for the result, return with error |
| 963 | * if we cannot get one. */ |
| 964 | cipherstack = sk_SSL_CIPHER_new_null(); |
| 965 | if (cipherstack == NULL) { |
| 966 | goto err; |
| 967 | } |
| 968 | |
| 969 | in_group_flags = OPENSSL_malloc(num_of_ciphers); |
| 970 | if (!in_group_flags) { |
| 971 | goto err; |
| 972 | } |
| 973 | |
| 974 | /* The cipher selection for the list is done. The ciphers are added |
| 975 | * to the resulting precedence to the STACK_OF(SSL_CIPHER). */ |
| 976 | for (curr = head; curr != NULL; curr = curr->next) { |
| 977 | if (curr->active) { |
David Benjamin | 2adb7ec | 2015-01-11 19:59:06 -0500 | [diff] [blame] | 978 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { |
| 979 | goto err; |
| 980 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 981 | in_group_flags[num_in_group_flags++] = curr->in_group; |
| 982 | } |
| 983 | } |
| 984 | OPENSSL_free(co_list); /* Not needed any longer */ |
| 985 | co_list = NULL; |
| 986 | |
| 987 | tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); |
| 988 | if (tmp_cipher_list == NULL) { |
| 989 | goto err; |
| 990 | } |
| 991 | pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 992 | if (!pref_list) { |
| 993 | goto err; |
| 994 | } |
| 995 | pref_list->ciphers = cipherstack; |
| 996 | pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags); |
| 997 | if (!pref_list->in_group_flags) { |
| 998 | goto err; |
| 999 | } |
| 1000 | memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags); |
| 1001 | OPENSSL_free(in_group_flags); |
| 1002 | in_group_flags = NULL; |
| 1003 | if (*cipher_list != NULL) { |
| 1004 | ssl_cipher_preference_list_free(*cipher_list); |
| 1005 | } |
| 1006 | *cipher_list = pref_list; |
| 1007 | pref_list = NULL; |
| 1008 | |
| 1009 | if (cipher_list_by_id != NULL) { |
| 1010 | if (*cipher_list_by_id != NULL) { |
| 1011 | sk_SSL_CIPHER_free(*cipher_list_by_id); |
| 1012 | } |
| 1013 | *cipher_list_by_id = tmp_cipher_list; |
| 1014 | tmp_cipher_list = NULL; |
Adam Langley | a307dfd | 2015-01-09 15:42:58 -0800 | [diff] [blame] | 1015 | (void) sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1016 | |
| 1017 | sk_SSL_CIPHER_sort(*cipher_list_by_id); |
| 1018 | } else { |
| 1019 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1020 | tmp_cipher_list = NULL; |
| 1021 | } |
| 1022 | |
| 1023 | return cipherstack; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1024 | |
| 1025 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1026 | if (co_list) { |
| 1027 | OPENSSL_free(co_list); |
| 1028 | } |
| 1029 | if (in_group_flags) { |
| 1030 | OPENSSL_free(in_group_flags); |
| 1031 | } |
| 1032 | if (cipherstack) { |
| 1033 | sk_SSL_CIPHER_free(cipherstack); |
| 1034 | } |
| 1035 | if (tmp_cipher_list) { |
| 1036 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1037 | } |
| 1038 | if (pref_list && pref_list->in_group_flags) { |
| 1039 | OPENSSL_free(pref_list->in_group_flags); |
| 1040 | } |
| 1041 | if (pref_list) { |
| 1042 | OPENSSL_free(pref_list); |
| 1043 | } |
| 1044 | return NULL; |
| 1045 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1046 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1047 | const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
| 1048 | int len) { |
| 1049 | const char *ver; |
| 1050 | const char *kx, *au, *enc, *mac; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1051 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1052 | static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1053 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1054 | alg_mkey = cipher->algorithm_mkey; |
| 1055 | alg_auth = cipher->algorithm_auth; |
| 1056 | alg_enc = cipher->algorithm_enc; |
| 1057 | alg_mac = cipher->algorithm_mac; |
| 1058 | alg_ssl = cipher->algorithm_ssl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1059 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1060 | if (alg_ssl & SSL_SSLV3) { |
| 1061 | ver = "SSLv3"; |
| 1062 | } else if (alg_ssl & SSL_TLSV1_2) { |
| 1063 | ver = "TLSv1.2"; |
| 1064 | } else { |
| 1065 | ver = "unknown"; |
| 1066 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1067 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1068 | switch (alg_mkey) { |
| 1069 | case SSL_kRSA: |
| 1070 | kx = "RSA"; |
| 1071 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1072 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1073 | case SSL_kDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1074 | kx = "DH"; |
| 1075 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1076 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1077 | case SSL_kECDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1078 | kx = "ECDH"; |
| 1079 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1080 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1081 | case SSL_kPSK: |
| 1082 | kx = "PSK"; |
| 1083 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1084 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1085 | default: |
| 1086 | kx = "unknown"; |
| 1087 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1088 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1089 | switch (alg_auth) { |
| 1090 | case SSL_aRSA: |
| 1091 | au = "RSA"; |
| 1092 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1093 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1094 | case SSL_aECDSA: |
| 1095 | au = "ECDSA"; |
| 1096 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1097 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1098 | case SSL_aPSK: |
| 1099 | au = "PSK"; |
| 1100 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1101 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1102 | default: |
| 1103 | au = "unknown"; |
| 1104 | break; |
| 1105 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1106 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1107 | switch (alg_enc) { |
| 1108 | case SSL_3DES: |
| 1109 | enc = "3DES(168)"; |
| 1110 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1111 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1112 | case SSL_RC4: |
| 1113 | enc = "RC4(128)"; |
| 1114 | break; |
| 1115 | |
| 1116 | case SSL_AES128: |
| 1117 | enc = "AES(128)"; |
| 1118 | break; |
| 1119 | |
| 1120 | case SSL_AES256: |
| 1121 | enc = "AES(256)"; |
| 1122 | break; |
| 1123 | |
| 1124 | case SSL_AES128GCM: |
| 1125 | enc = "AESGCM(128)"; |
| 1126 | break; |
| 1127 | |
| 1128 | case SSL_AES256GCM: |
| 1129 | enc = "AESGCM(256)"; |
| 1130 | break; |
| 1131 | |
| 1132 | case SSL_CHACHA20POLY1305: |
| 1133 | enc = "ChaCha20-Poly1305"; |
| 1134 | break; |
| 1135 | |
| 1136 | default: |
| 1137 | enc = "unknown"; |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | switch (alg_mac) { |
| 1142 | case SSL_MD5: |
| 1143 | mac = "MD5"; |
| 1144 | break; |
| 1145 | |
| 1146 | case SSL_SHA1: |
| 1147 | mac = "SHA1"; |
| 1148 | break; |
| 1149 | |
| 1150 | case SSL_SHA256: |
| 1151 | mac = "SHA256"; |
| 1152 | break; |
| 1153 | |
| 1154 | case SSL_SHA384: |
| 1155 | mac = "SHA384"; |
| 1156 | break; |
| 1157 | |
| 1158 | case SSL_AEAD: |
| 1159 | mac = "AEAD"; |
| 1160 | break; |
| 1161 | |
| 1162 | default: |
| 1163 | mac = "unknown"; |
| 1164 | break; |
| 1165 | } |
| 1166 | |
| 1167 | if (buf == NULL) { |
| 1168 | len = 128; |
| 1169 | buf = OPENSSL_malloc(len); |
David Benjamin | 1eed2c0 | 2015-02-08 23:20:06 -0500 | [diff] [blame] | 1170 | if (buf == NULL) { |
| 1171 | return NULL; |
| 1172 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1173 | } else if (len < 128) { |
| 1174 | return "Buffer too small"; |
| 1175 | } |
| 1176 | |
| 1177 | BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac); |
| 1178 | return buf; |
| 1179 | } |
| 1180 | |
| 1181 | int SSL_CIPHER_is_AES(const SSL_CIPHER *c) { |
| 1182 | return (c->algorithm_enc & SSL_AES) != 0; |
| 1183 | } |
| 1184 | |
| 1185 | int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *c) { |
| 1186 | return (c->algorithm_mac & SSL_MD5) != 0; |
| 1187 | } |
| 1188 | |
| 1189 | int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *c) { |
| 1190 | return (c->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0; |
| 1191 | } |
| 1192 | |
| 1193 | int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *c) { |
| 1194 | return (c->algorithm_enc & SSL_CHACHA20POLY1305) != 0; |
| 1195 | } |
| 1196 | |
| 1197 | const char *SSL_CIPHER_get_version(const SSL_CIPHER *c) { |
| 1198 | int i; |
| 1199 | |
| 1200 | if (c == NULL) { |
| 1201 | return "(NONE)"; |
| 1202 | } |
| 1203 | |
| 1204 | i = (int)(c->id >> 24L); |
| 1205 | if (i == 3) { |
| 1206 | return "TLSv1/SSLv3"; |
| 1207 | } else if (i == 2) { |
| 1208 | return "SSLv2"; |
| 1209 | } else { |
| 1210 | return "unknown"; |
| 1211 | } |
| 1212 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1213 | |
| 1214 | /* return the actual cipher being used */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1215 | const char *SSL_CIPHER_get_name(const SSL_CIPHER *c) { |
| 1216 | if (c != NULL) { |
| 1217 | return c->name; |
| 1218 | } |
| 1219 | |
| 1220 | return "(NONE)"; |
| 1221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1222 | |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1223 | const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
| 1224 | if (cipher == NULL) { |
| 1225 | return ""; |
| 1226 | } |
| 1227 | |
| 1228 | switch (cipher->algorithm_mkey) { |
| 1229 | case SSL_kRSA: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1230 | return "RSA"; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1231 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1232 | case SSL_kDHE: |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1233 | switch (cipher->algorithm_auth) { |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1234 | case SSL_aRSA: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1235 | return "DHE_RSA"; |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1236 | default: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1237 | assert(0); |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1238 | return "UNKNOWN"; |
| 1239 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1240 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1241 | case SSL_kECDHE: |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1242 | switch (cipher->algorithm_auth) { |
| 1243 | case SSL_aECDSA: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1244 | return "ECDHE_ECDSA"; |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1245 | case SSL_aRSA: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1246 | return "ECDHE_RSA"; |
| 1247 | case SSL_aPSK: |
| 1248 | return "ECDHE_PSK"; |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1249 | default: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1250 | assert(0); |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1251 | return "UNKNOWN"; |
| 1252 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1253 | |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1254 | case SSL_kPSK: |
| 1255 | assert(cipher->algorithm_auth == SSL_aPSK); |
| 1256 | return "PSK"; |
| 1257 | |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1258 | default: |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1259 | assert(0); |
Adam Langley | f1df2b3 | 2014-08-11 17:37:53 -0700 | [diff] [blame] | 1260 | return "UNKNOWN"; |
| 1261 | } |
| 1262 | } |
| 1263 | |
David Benjamin | 6522625 | 2015-02-05 16:49:47 -0500 | [diff] [blame] | 1264 | static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) { |
| 1265 | switch (cipher->algorithm_enc) { |
| 1266 | case SSL_3DES: |
| 1267 | return "3DES_EDE_CBC"; |
| 1268 | case SSL_RC4: |
| 1269 | return "RC4"; |
| 1270 | case SSL_AES128: |
| 1271 | return "AES_128_CBC"; |
| 1272 | case SSL_AES256: |
| 1273 | return "AES_256_CBC"; |
| 1274 | case SSL_AES128GCM: |
| 1275 | return "AES_128_GCM"; |
| 1276 | case SSL_AES256GCM: |
| 1277 | return "AES_256_GCM"; |
| 1278 | case SSL_CHACHA20POLY1305: |
| 1279 | return "CHACHA20_POLY1305"; |
| 1280 | break; |
| 1281 | default: |
| 1282 | assert(0); |
| 1283 | return "UNKNOWN"; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) { |
| 1288 | if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) { |
| 1289 | /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is |
| 1290 | * only ever MD5 or SHA-1. */ |
| 1291 | switch (cipher->algorithm_mac) { |
| 1292 | case SSL_MD5: |
| 1293 | return "MD5"; |
| 1294 | case SSL_SHA1: |
| 1295 | return "SHA"; |
| 1296 | default: |
| 1297 | assert(0); |
| 1298 | return "UNKNOWN"; |
| 1299 | } |
| 1300 | } else if (cipher->algorithm2 & TLS1_PRF_SHA256) { |
| 1301 | return "SHA256"; |
| 1302 | } else if (cipher->algorithm2 & TLS1_PRF_SHA384) { |
| 1303 | return "SHA384"; |
| 1304 | } else { |
| 1305 | assert(0); |
| 1306 | return "UNKNOWN"; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) { |
| 1311 | if (cipher == NULL) { |
| 1312 | return NULL; |
| 1313 | } |
| 1314 | |
| 1315 | const char *kx_name = SSL_CIPHER_get_kx_name(cipher); |
| 1316 | const char *enc_name = ssl_cipher_get_enc_name(cipher); |
| 1317 | const char *prf_name = ssl_cipher_get_prf_name(cipher); |
| 1318 | |
| 1319 | /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */ |
| 1320 | size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 + |
| 1321 | strlen(prf_name) + 1; |
| 1322 | char *ret = OPENSSL_malloc(len); |
| 1323 | if (ret == NULL) { |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | if (BUF_strlcpy(ret, "TLS_", len) >= len || |
| 1327 | BUF_strlcat(ret, kx_name, len) >= len || |
| 1328 | BUF_strlcat(ret, "_WITH_", len) >= len || |
| 1329 | BUF_strlcat(ret, enc_name, len) >= len || |
| 1330 | BUF_strlcat(ret, "_", len) >= len || |
| 1331 | BUF_strlcat(ret, prf_name, len) >= len) { |
| 1332 | assert(0); |
| 1333 | OPENSSL_free(ret); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | assert(strlen(ret) + 1 == len); |
| 1337 | return ret; |
| 1338 | } |
| 1339 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1340 | /* number of bits for symmetric cipher */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1341 | int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits) { |
| 1342 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1343 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1344 | if (c != NULL) { |
| 1345 | if (alg_bits != NULL) { |
| 1346 | *alg_bits = c->alg_bits; |
| 1347 | } |
| 1348 | ret = c->strength_bits; |
| 1349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1351 | return ret; |
| 1352 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1353 | |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1354 | uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c) { return c->id; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1355 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1356 | void *SSL_COMP_get_compression_methods(void) { return NULL; } |
| 1357 | |
| 1358 | int SSL_COMP_add_compression_method(int id, void *cm) { return 1; } |
| 1359 | |
| 1360 | const char *SSL_COMP_get_name(const void *comp) { return NULL; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1361 | |
| 1362 | /* For a cipher return the index corresponding to the certificate type */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1363 | int ssl_cipher_get_cert_index(const SSL_CIPHER *c) { |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1364 | uint32_t alg_a = c->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1365 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1366 | if (alg_a & SSL_aECDSA) { |
| 1367 | return SSL_PKEY_ECC; |
| 1368 | } else if (alg_a & SSL_aRSA) { |
| 1369 | return SSL_PKEY_RSA_ENC; |
| 1370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1371 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1372 | return -1; |
| 1373 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1374 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1375 | /* ssl_cipher_has_server_public_key returns 1 if |cipher| involves a server |
| 1376 | * public key in the key exchange, sent in a server Certificate message. |
| 1377 | * Otherwise it returns 0. */ |
| 1378 | int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) { |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 1379 | /* PSK-authenticated ciphers do not use a public key, except for |
| 1380 | * RSA_PSK. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1381 | if ((cipher->algorithm_auth & SSL_aPSK) && |
| 1382 | !(cipher->algorithm_mkey & SSL_kRSA)) { |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | /* All other ciphers include it. */ |
| 1387 | return 1; |
| 1388 | } |
| 1389 | |
| 1390 | /* ssl_cipher_requires_server_key_exchange returns 1 if |cipher| requires a |
| 1391 | * ServerKeyExchange message. Otherwise it returns 0. |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1392 | * |
| 1393 | * Unlike ssl_cipher_has_server_public_key, some ciphers take optional |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1394 | * ServerKeyExchanges. PSK and RSA_PSK only use the ServerKeyExchange to |
| 1395 | * communicate a psk_identity_hint, so it is optional. */ |
| 1396 | int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
| 1397 | /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */ |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1398 | if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1399 | return 1; |
| 1400 | } |
| 1401 | |
| 1402 | /* It is optional in all others. */ |
| 1403 | return 0; |
| 1404 | } |