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