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; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 379 | int ssl_get_handshake_digest(uint32_t *out_mask, const EVP_MD **out_md, |
| 380 | size_t idx) { |
David Benjamin | 1f5e115 | 2014-12-23 09:23:32 -0500 | [diff] [blame] | 381 | if (idx >= SSL_MAX_DIGEST) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 382 | return 0; |
| 383 | } |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 384 | *out_mask = ssl_handshake_digests[idx].mask; |
| 385 | *out_md = ssl_handshake_digests[idx].md_func(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 386 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | #define ITEM_SEP(a) \ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 390 | (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 392 | /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the |
| 393 | * |buf_len| bytes at |buf|. */ |
| 394 | static int rule_equals(const char *rule, const char *buf, size_t buf_len) { |
| 395 | /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */ |
| 396 | return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
| 397 | } |
| 398 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 400 | CIPHER_ORDER **tail) { |
| 401 | if (curr == *tail) { |
| 402 | return; |
| 403 | } |
| 404 | if (curr == *head) { |
| 405 | *head = curr->next; |
| 406 | } |
| 407 | if (curr->prev != NULL) { |
| 408 | curr->prev->next = curr->next; |
| 409 | } |
| 410 | if (curr->next != NULL) { |
| 411 | curr->next->prev = curr->prev; |
| 412 | } |
| 413 | (*tail)->next = curr; |
| 414 | curr->prev = *tail; |
| 415 | curr->next = NULL; |
| 416 | *tail = curr; |
| 417 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | |
| 419 | static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | CIPHER_ORDER **tail) { |
| 421 | if (curr == *head) { |
| 422 | return; |
| 423 | } |
| 424 | if (curr == *tail) { |
| 425 | *tail = curr->prev; |
| 426 | } |
| 427 | if (curr->next != NULL) { |
| 428 | curr->next->prev = curr->prev; |
| 429 | } |
| 430 | if (curr->prev != NULL) { |
| 431 | curr->prev->next = curr->next; |
| 432 | } |
| 433 | (*head)->prev = curr; |
| 434 | curr->next = *head; |
| 435 | curr->prev = NULL; |
| 436 | *head = curr; |
| 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 439 | static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 440 | size_t num_of_ciphers, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 441 | CIPHER_ORDER *co_list, |
| 442 | CIPHER_ORDER **head_p, |
| 443 | CIPHER_ORDER **tail_p) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 444 | size_t i, co_list_num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 446 | /* We have num_of_ciphers descriptions compiled in, depending on the method |
| 447 | * selected (SSLv2 and/or SSLv3, TLSv1 etc). These will later be sorted in a |
| 448 | * linked list with at most num entries. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 450 | /* Get the initial list of ciphers */ |
| 451 | co_list_num = 0; /* actual count of ciphers */ |
| 452 | for (i = 0; i < num_of_ciphers; i++) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 453 | const SSL_CIPHER *c = ssl_method->get_cipher(i); |
| 454 | if (c != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | co_list[co_list_num].cipher = c; |
| 456 | co_list[co_list_num].next = NULL; |
| 457 | co_list[co_list_num].prev = NULL; |
| 458 | co_list[co_list_num].active = 0; |
| 459 | co_list[co_list_num].in_group = 0; |
| 460 | co_list_num++; |
| 461 | } |
| 462 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | /* Prepare linked list from list entries. */ |
| 465 | if (co_list_num > 0) { |
| 466 | co_list[0].prev = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 468 | if (co_list_num > 1) { |
| 469 | co_list[0].next = &co_list[1]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | for (i = 1; i < co_list_num - 1; i++) { |
| 472 | co_list[i].prev = &co_list[i - 1]; |
| 473 | co_list[i].next = &co_list[i + 1]; |
| 474 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 476 | co_list[co_list_num - 1].prev = &co_list[co_list_num - 2]; |
| 477 | } |
| 478 | |
| 479 | co_list[co_list_num - 1].next = NULL; |
| 480 | |
| 481 | *head_p = &co_list[0]; |
| 482 | *tail_p = &co_list[co_list_num - 1]; |
| 483 | } |
| 484 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 486 | /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its |
| 487 | * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new |
| 488 | * head and tail of the list to |*head_p| and |*tail_p|, respectively. |
| 489 | * |
| 490 | * - If |cipher_id| is non-zero, only that cipher is selected. |
| 491 | * - Otherwise, if |strength_bits| is non-negative, it selects ciphers |
| 492 | * of that strength. |
| 493 | * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and |
| 494 | * |algo_strength|. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 495 | static void ssl_cipher_apply_rule( |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 496 | uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth, |
| 497 | uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl, |
| 498 | uint32_t algo_strength, int rule, int strength_bits, int in_group, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 499 | CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) { |
| 500 | CIPHER_ORDER *head, *tail, *curr, *next, *last; |
| 501 | const SSL_CIPHER *cp; |
| 502 | int reverse = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 504 | if (cipher_id == 0 && strength_bits == -1 && |
| 505 | (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 || |
| 506 | alg_ssl == 0 || algo_strength == 0)) { |
| 507 | /* The rule matches nothing, so bail early. */ |
| 508 | return; |
| 509 | } |
| 510 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 511 | if (rule == CIPHER_DEL) { |
| 512 | /* needed to maintain sorting between currently deleted ciphers */ |
| 513 | reverse = 1; |
| 514 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 515 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 516 | head = *head_p; |
| 517 | tail = *tail_p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 519 | if (reverse) { |
| 520 | next = tail; |
| 521 | last = head; |
| 522 | } else { |
| 523 | next = head; |
| 524 | last = tail; |
| 525 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 527 | curr = NULL; |
| 528 | for (;;) { |
| 529 | if (curr == last) { |
| 530 | break; |
| 531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 533 | curr = next; |
| 534 | if (curr == NULL) { |
| 535 | break; |
| 536 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 537 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 538 | next = reverse ? curr->prev : curr->next; |
| 539 | cp = curr->cipher; |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 540 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 541 | /* Selection criteria is either a specific cipher, the value of |
| 542 | * |strength_bits|, or the algorithms used. */ |
| 543 | if (cipher_id != 0) { |
| 544 | if (cipher_id != cp->id) { |
| 545 | continue; |
| 546 | } |
| 547 | } else if (strength_bits >= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 548 | if (strength_bits != cp->strength_bits) { |
| 549 | continue; |
| 550 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 551 | } else if (!(alg_mkey & cp->algorithm_mkey) || |
| 552 | !(alg_auth & cp->algorithm_auth) || |
| 553 | !(alg_enc & cp->algorithm_enc) || |
| 554 | !(alg_mac & cp->algorithm_mac) || |
| 555 | !(alg_ssl & cp->algorithm_ssl) || |
| 556 | !(algo_strength & cp->algo_strength)) { |
| 557 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 558 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 559 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 560 | /* add the cipher if it has not been added yet. */ |
| 561 | if (rule == CIPHER_ADD) { |
| 562 | /* reverse == 0 */ |
| 563 | if (!curr->active) { |
| 564 | ll_append_tail(&head, curr, &tail); |
| 565 | curr->active = 1; |
| 566 | curr->in_group = in_group; |
| 567 | } |
| 568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 570 | /* Move the added cipher to this location */ |
| 571 | else if (rule == CIPHER_ORD) { |
| 572 | /* reverse == 0 */ |
| 573 | if (curr->active) { |
| 574 | ll_append_tail(&head, curr, &tail); |
| 575 | curr->in_group = 0; |
| 576 | } |
| 577 | } else if (rule == CIPHER_DEL) { |
| 578 | /* reverse == 1 */ |
| 579 | if (curr->active) { |
| 580 | /* most recently deleted ciphersuites get best positions |
| 581 | * for any future CIPHER_ADD (note that the CIPHER_DEL loop |
| 582 | * works in reverse to maintain the order) */ |
| 583 | ll_append_head(&head, curr, &tail); |
| 584 | curr->active = 0; |
| 585 | curr->in_group = 0; |
| 586 | } |
| 587 | } else if (rule == CIPHER_KILL) { |
| 588 | /* reverse == 0 */ |
| 589 | if (head == curr) { |
| 590 | head = curr->next; |
| 591 | } else { |
| 592 | curr->prev->next = curr->next; |
| 593 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 595 | if (tail == curr) { |
| 596 | tail = curr->prev; |
| 597 | } |
| 598 | curr->active = 0; |
| 599 | if (curr->next != NULL) { |
| 600 | curr->next->prev = curr->prev; |
| 601 | } |
| 602 | if (curr->prev != NULL) { |
| 603 | curr->prev->next = curr->next; |
| 604 | } |
| 605 | curr->next = NULL; |
| 606 | curr->prev = NULL; |
| 607 | } |
| 608 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 609 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 610 | *head_p = head; |
| 611 | *tail_p = tail; |
| 612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | |
| 614 | static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | CIPHER_ORDER **tail_p) { |
| 616 | int max_strength_bits, i, *number_uses; |
| 617 | CIPHER_ORDER *curr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 618 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 619 | /* This routine sorts the ciphers with descending strength. The sorting must |
| 620 | * keep the pre-sorted sequence, so we apply the normal sorting routine as |
| 621 | * '+' movement to the end of the list. */ |
| 622 | max_strength_bits = 0; |
| 623 | curr = *head_p; |
| 624 | while (curr != NULL) { |
| 625 | if (curr->active && curr->cipher->strength_bits > max_strength_bits) { |
| 626 | max_strength_bits = curr->cipher->strength_bits; |
| 627 | } |
| 628 | curr = curr->next; |
| 629 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 630 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 631 | number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); |
| 632 | if (!number_uses) { |
| 633 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_strength_sort, ERR_R_MALLOC_FAILURE); |
| 634 | return 0; |
| 635 | } |
| 636 | memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 637 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 638 | /* Now find the strength_bits values actually used. */ |
| 639 | curr = *head_p; |
| 640 | while (curr != NULL) { |
| 641 | if (curr->active) { |
| 642 | number_uses[curr->cipher->strength_bits]++; |
| 643 | } |
| 644 | curr = curr->next; |
| 645 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 646 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 647 | /* Go through the list of used strength_bits values in descending order. */ |
| 648 | for (i = max_strength_bits; i >= 0; i--) { |
| 649 | if (number_uses[i] > 0) { |
| 650 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, |
| 651 | tail_p); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | OPENSSL_free(number_uses); |
| 656 | return 1; |
| 657 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 659 | static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method, |
| 660 | const char *rule_str, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 661 | CIPHER_ORDER **head_p, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 662 | CIPHER_ORDER **tail_p) { |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 663 | 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] | 664 | const char *l, *buf; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 665 | int multi, rule, retval, ok, in_group = 0, has_group = 0; |
| 666 | size_t j, buf_len; |
| 667 | uint32_t cipher_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 668 | char ch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 670 | retval = 1; |
| 671 | l = rule_str; |
| 672 | for (;;) { |
| 673 | ch = *l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 675 | if (ch == '\0') { |
| 676 | break; /* done */ |
| 677 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 678 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 679 | if (in_group) { |
| 680 | if (ch == ']') { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 681 | if (*tail_p) { |
| 682 | (*tail_p)->in_group = 0; |
| 683 | } |
| 684 | in_group = 0; |
| 685 | l++; |
| 686 | continue; |
| 687 | } |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 688 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 689 | if (ch == '|') { |
| 690 | rule = CIPHER_ADD; |
| 691 | l++; |
| 692 | continue; |
| 693 | } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && |
| 694 | !(ch >= '0' && ch <= '9')) { |
| 695 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 696 | SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 697 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 698 | break; |
| 699 | } else { |
| 700 | rule = CIPHER_ADD; |
| 701 | } |
| 702 | } else if (ch == '-') { |
| 703 | rule = CIPHER_DEL; |
| 704 | l++; |
| 705 | } else if (ch == '+') { |
| 706 | rule = CIPHER_ORD; |
| 707 | l++; |
| 708 | } else if (ch == '!') { |
| 709 | rule = CIPHER_KILL; |
| 710 | l++; |
| 711 | } else if (ch == '@') { |
| 712 | rule = CIPHER_SPECIAL; |
| 713 | l++; |
| 714 | } else if (ch == '[') { |
| 715 | if (in_group) { |
| 716 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_NESTED_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 717 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | in_group = 1; |
| 721 | has_group = 1; |
| 722 | l++; |
| 723 | continue; |
| 724 | } else { |
| 725 | rule = CIPHER_ADD; |
| 726 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 727 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 728 | /* If preference groups are enabled, the only legal operator is +. |
| 729 | * Otherwise the in_group bits will get mixed up. */ |
| 730 | if (has_group && rule != CIPHER_ADD) { |
| 731 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 732 | SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 733 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 734 | break; |
| 735 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 737 | if (ITEM_SEP(ch)) { |
| 738 | l++; |
| 739 | continue; |
| 740 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 741 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 742 | multi = 0; |
| 743 | cipher_id = 0; |
| 744 | alg_mkey = ~0u; |
| 745 | alg_auth = ~0u; |
| 746 | alg_enc = ~0u; |
| 747 | alg_mac = ~0u; |
| 748 | alg_ssl = ~0u; |
| 749 | algo_strength = ~0u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 750 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 751 | for (;;) { |
| 752 | ch = *l; |
| 753 | buf = l; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 754 | buf_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 755 | while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || |
| 756 | ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) { |
| 757 | ch = *(++l); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 758 | buf_len++; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 759 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 760 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 761 | if (buf_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 762 | /* We hit something we cannot deal with, it is no command or separator |
| 763 | * nor alphanumeric, so we call this an error. */ |
| 764 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 765 | SSL_R_INVALID_COMMAND); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 766 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 767 | l++; |
| 768 | break; |
| 769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 771 | if (rule == CIPHER_SPECIAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 772 | break; |
| 773 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 774 | |
| 775 | /* Look for a matching exact cipher. These aren't allowed in multipart |
| 776 | * rules. */ |
| 777 | if (!multi && ch != '+') { |
| 778 | size_t num_ciphers = ssl_method->num_ciphers(); |
| 779 | for (j = 0; j < num_ciphers; j++) { |
| 780 | const SSL_CIPHER *cipher = ssl_method->get_cipher(j); |
| 781 | if (cipher != NULL && rule_equals(cipher->name, buf, buf_len)) { |
| 782 | cipher_id = cipher->id; |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | if (cipher_id == 0) { |
| 788 | /* If not an exact cipher, look for a matching cipher alias. */ |
| 789 | for (j = 0; j < NUM_CIPHER_ALIASES; j++) { |
| 790 | if (rule_equals(kCipherAliases[j].name, buf, buf_len)) { |
| 791 | alg_mkey &= kCipherAliases[j].algorithm_mkey; |
| 792 | alg_auth &= kCipherAliases[j].algorithm_auth; |
| 793 | alg_enc &= kCipherAliases[j].algorithm_enc; |
| 794 | alg_mac &= kCipherAliases[j].algorithm_mac; |
| 795 | alg_ssl &= kCipherAliases[j].algorithm_ssl; |
| 796 | algo_strength &= kCipherAliases[j].algo_strength; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | if (j == NUM_CIPHER_ALIASES) { |
| 801 | alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* Check for a multipart rule. */ |
| 806 | if (ch != '+') { |
| 807 | break; |
| 808 | } |
| 809 | l++; |
| 810 | multi = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 811 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 812 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 813 | /* Ok, we have the rule, now apply it. */ |
| 814 | if (rule == CIPHER_SPECIAL) { |
| 815 | /* special command */ |
| 816 | ok = 0; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 817 | if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 818 | ok = ssl_cipher_strength_sort(head_p, tail_p); |
| 819 | } else { |
| 820 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, |
| 821 | SSL_R_INVALID_COMMAND); |
| 822 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 824 | if (ok == 0) { |
| 825 | retval = 0; |
| 826 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 827 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 828 | /* We do not support any "multi" options together with "@", so throw away |
| 829 | * the rest of the command, if any left, until end or ':' is found. */ |
| 830 | while (*l != '\0' && !ITEM_SEP(*l)) { |
| 831 | l++; |
| 832 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 833 | } else { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 834 | ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac, |
| 835 | alg_ssl, algo_strength, rule, -1, in_group, head_p, |
| 836 | tail_p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 837 | } |
| 838 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 840 | if (in_group) { |
| 841 | OPENSSL_PUT_ERROR(SSL, ssl_cipher_process_rulestr, SSL_R_INVALID_COMMAND); |
| 842 | retval = 0; |
| 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 | return retval; |
| 846 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 848 | STACK_OF(SSL_CIPHER) * |
| 849 | ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method, |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 850 | struct ssl_cipher_preference_list_st **out_cipher_list, |
| 851 | STACK_OF(SSL_CIPHER) **out_cipher_list_by_id, |
| 852 | const char *rule_str) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 853 | int ok; |
| 854 | size_t num_of_ciphers; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 855 | STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL; |
| 856 | const char *rule_p; |
| 857 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 858 | uint8_t *in_group_flags = NULL; |
| 859 | unsigned int num_in_group_flags = 0; |
| 860 | struct ssl_cipher_preference_list_st *pref_list = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | /* Return with error if nothing to do. */ |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 863 | if (rule_str == NULL || out_cipher_list == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 864 | return NULL; |
| 865 | } |
David Benjamin | 5213df4 | 2014-08-20 14:19:54 -0400 | [diff] [blame] | 866 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 867 | /* Now we have to collect the available ciphers from the compiled in ciphers. |
| 868 | * We cannot get more than the number compiled in, so it is used for |
| 869 | * allocation. */ |
| 870 | num_of_ciphers = ssl_method->num_ciphers(); |
| 871 | co_list = |
| 872 | (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers); |
| 873 | if (co_list == NULL) { |
| 874 | OPENSSL_PUT_ERROR(SSL, ssl_create_cipher_list, ERR_R_MALLOC_FAILURE); |
| 875 | return NULL; |
| 876 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 878 | 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] | 879 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 880 | /* Now arrange all ciphers by preference: |
| 881 | * TODO(davidben): Compute this order once and copy it. */ |
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 | /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other |
| 884 | * key exchange mechanisms */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 885 | ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u, |
| 886 | CIPHER_ADD, -1, 0, &head, &tail); |
| 887 | 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] | 888 | 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 889 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, |
| 890 | 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 891 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 892 | /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer |
| 893 | * CHACHA20 unless there is hardware support for fast and constant-time |
| 894 | * AES_GCM. */ |
| 895 | if (EVP_has_aes_hardware()) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 896 | 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] | 897 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 898 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 899 | -1, 0, &head, &tail); |
| 900 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 901 | CIPHER_ADD, -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 902 | } else { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 903 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 904 | CIPHER_ADD, -1, 0, &head, &tail); |
| 905 | 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] | 906 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 907 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 908 | -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 909 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 911 | /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA, |
| 912 | * RC4_128_MD5, 3DES_EDE_CBC_SHA. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 913 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 914 | 0, &head, &tail); |
| 915 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 916 | 0, &head, &tail); |
| 917 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD, |
| 918 | -1, 0, &head, &tail); |
| 919 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1, |
| 920 | 0, &head, &tail); |
| 921 | 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] | 922 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 923 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 924 | /* Temporarily enable everything else for sorting */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 925 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0, |
| 926 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 927 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 928 | /* Move ciphers without forward secrecy to the end. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 929 | ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u, |
| 930 | CIPHER_ORD, -1, 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 931 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 932 | /* Now disable everything (maintaining the ordering!) */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 933 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0, |
| 934 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 935 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 936 | /* If the rule_string begins with DEFAULT, apply the default rule before |
| 937 | * using the (possibly available) additional rules. */ |
| 938 | ok = 1; |
| 939 | rule_p = rule_str; |
| 940 | if (strncmp(rule_str, "DEFAULT", 7) == 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 941 | ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head, |
| 942 | &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 943 | rule_p += 7; |
| 944 | if (*rule_p == ':') { |
| 945 | rule_p++; |
| 946 | } |
| 947 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 948 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 949 | if (ok && strlen(rule_p) > 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 950 | ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 951 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 952 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 953 | if (!ok) { |
| 954 | goto err; |
| 955 | } |
| 956 | |
| 957 | /* Allocate new "cipherstack" for the result, return with error |
| 958 | * if we cannot get one. */ |
| 959 | cipherstack = sk_SSL_CIPHER_new_null(); |
| 960 | if (cipherstack == NULL) { |
| 961 | goto err; |
| 962 | } |
| 963 | |
| 964 | in_group_flags = OPENSSL_malloc(num_of_ciphers); |
| 965 | if (!in_group_flags) { |
| 966 | goto err; |
| 967 | } |
| 968 | |
| 969 | /* The cipher selection for the list is done. The ciphers are added |
| 970 | * to the resulting precedence to the STACK_OF(SSL_CIPHER). */ |
| 971 | for (curr = head; curr != NULL; curr = curr->next) { |
| 972 | if (curr->active) { |
David Benjamin | 2adb7ec | 2015-01-11 19:59:06 -0500 | [diff] [blame] | 973 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { |
| 974 | goto err; |
| 975 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 976 | in_group_flags[num_in_group_flags++] = curr->in_group; |
| 977 | } |
| 978 | } |
| 979 | OPENSSL_free(co_list); /* Not needed any longer */ |
| 980 | co_list = NULL; |
| 981 | |
| 982 | tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); |
| 983 | if (tmp_cipher_list == NULL) { |
| 984 | goto err; |
| 985 | } |
| 986 | pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 987 | if (!pref_list) { |
| 988 | goto err; |
| 989 | } |
| 990 | pref_list->ciphers = cipherstack; |
| 991 | pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags); |
| 992 | if (!pref_list->in_group_flags) { |
| 993 | goto err; |
| 994 | } |
| 995 | memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags); |
| 996 | OPENSSL_free(in_group_flags); |
| 997 | in_group_flags = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 998 | if (*out_cipher_list != NULL) { |
| 999 | ssl_cipher_preference_list_free(*out_cipher_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1000 | } |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1001 | *out_cipher_list = pref_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1002 | pref_list = NULL; |
| 1003 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1004 | if (out_cipher_list_by_id != NULL) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1005 | sk_SSL_CIPHER_free(*out_cipher_list_by_id); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1006 | *out_cipher_list_by_id = tmp_cipher_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1007 | tmp_cipher_list = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1008 | (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id, |
| 1009 | ssl_cipher_ptr_id_cmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1010 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1011 | sk_SSL_CIPHER_sort(*out_cipher_list_by_id); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1012 | } else { |
| 1013 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1014 | tmp_cipher_list = NULL; |
| 1015 | } |
| 1016 | |
| 1017 | return cipherstack; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1018 | |
| 1019 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1020 | OPENSSL_free(co_list); |
| 1021 | OPENSSL_free(in_group_flags); |
| 1022 | sk_SSL_CIPHER_free(cipherstack); |
| 1023 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1024 | if (pref_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1025 | OPENSSL_free(pref_list->in_group_flags); |
| 1026 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1027 | OPENSSL_free(pref_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1028 | return NULL; |
| 1029 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1031 | uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; } |
| 1032 | |
| 1033 | int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) { |
| 1034 | return (cipher->algorithm_enc & SSL_AES) != 0; |
| 1035 | } |
| 1036 | |
| 1037 | int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) { |
| 1038 | return (cipher->algorithm_mac & SSL_MD5) != 0; |
| 1039 | } |
| 1040 | |
| 1041 | int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) { |
| 1042 | return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0; |
| 1043 | } |
| 1044 | |
| 1045 | int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) { |
| 1046 | return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0; |
| 1047 | } |
| 1048 | |
| 1049 | /* return the actual cipher being used */ |
| 1050 | const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) { |
| 1051 | if (cipher != NULL) { |
| 1052 | return cipher->name; |
| 1053 | } |
| 1054 | |
| 1055 | return "(NONE)"; |
| 1056 | } |
| 1057 | |
| 1058 | const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
| 1059 | if (cipher == NULL) { |
| 1060 | return ""; |
| 1061 | } |
| 1062 | |
| 1063 | switch (cipher->algorithm_mkey) { |
| 1064 | case SSL_kRSA: |
| 1065 | return "RSA"; |
| 1066 | |
| 1067 | case SSL_kDHE: |
| 1068 | switch (cipher->algorithm_auth) { |
| 1069 | case SSL_aRSA: |
| 1070 | return "DHE_RSA"; |
| 1071 | default: |
| 1072 | assert(0); |
| 1073 | return "UNKNOWN"; |
| 1074 | } |
| 1075 | |
| 1076 | case SSL_kECDHE: |
| 1077 | switch (cipher->algorithm_auth) { |
| 1078 | case SSL_aECDSA: |
| 1079 | return "ECDHE_ECDSA"; |
| 1080 | case SSL_aRSA: |
| 1081 | return "ECDHE_RSA"; |
| 1082 | case SSL_aPSK: |
| 1083 | return "ECDHE_PSK"; |
| 1084 | default: |
| 1085 | assert(0); |
| 1086 | return "UNKNOWN"; |
| 1087 | } |
| 1088 | |
| 1089 | case SSL_kPSK: |
| 1090 | assert(cipher->algorithm_auth == SSL_aPSK); |
| 1091 | return "PSK"; |
| 1092 | |
| 1093 | default: |
| 1094 | assert(0); |
| 1095 | return "UNKNOWN"; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) { |
| 1100 | switch (cipher->algorithm_enc) { |
| 1101 | case SSL_3DES: |
| 1102 | return "3DES_EDE_CBC"; |
| 1103 | case SSL_RC4: |
| 1104 | return "RC4"; |
| 1105 | case SSL_AES128: |
| 1106 | return "AES_128_CBC"; |
| 1107 | case SSL_AES256: |
| 1108 | return "AES_256_CBC"; |
| 1109 | case SSL_AES128GCM: |
| 1110 | return "AES_128_GCM"; |
| 1111 | case SSL_AES256GCM: |
| 1112 | return "AES_256_GCM"; |
| 1113 | case SSL_CHACHA20POLY1305: |
| 1114 | return "CHACHA20_POLY1305"; |
| 1115 | break; |
| 1116 | default: |
| 1117 | assert(0); |
| 1118 | return "UNKNOWN"; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) { |
| 1123 | if ((cipher->algorithm2 & TLS1_PRF) == TLS1_PRF) { |
| 1124 | /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is |
| 1125 | * only ever MD5 or SHA-1. */ |
| 1126 | switch (cipher->algorithm_mac) { |
| 1127 | case SSL_MD5: |
| 1128 | return "MD5"; |
| 1129 | case SSL_SHA1: |
| 1130 | return "SHA"; |
| 1131 | default: |
| 1132 | assert(0); |
| 1133 | return "UNKNOWN"; |
| 1134 | } |
| 1135 | } else if (cipher->algorithm2 & TLS1_PRF_SHA256) { |
| 1136 | return "SHA256"; |
| 1137 | } else if (cipher->algorithm2 & TLS1_PRF_SHA384) { |
| 1138 | return "SHA384"; |
| 1139 | } else { |
| 1140 | assert(0); |
| 1141 | return "UNKNOWN"; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) { |
| 1146 | if (cipher == NULL) { |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
| 1150 | const char *kx_name = SSL_CIPHER_get_kx_name(cipher); |
| 1151 | const char *enc_name = ssl_cipher_get_enc_name(cipher); |
| 1152 | const char *prf_name = ssl_cipher_get_prf_name(cipher); |
| 1153 | |
| 1154 | /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */ |
| 1155 | size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 + |
| 1156 | strlen(prf_name) + 1; |
| 1157 | char *ret = OPENSSL_malloc(len); |
| 1158 | if (ret == NULL) { |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | if (BUF_strlcpy(ret, "TLS_", len) >= len || |
| 1162 | BUF_strlcat(ret, kx_name, len) >= len || |
| 1163 | BUF_strlcat(ret, "_WITH_", len) >= len || |
| 1164 | BUF_strlcat(ret, enc_name, len) >= len || |
| 1165 | BUF_strlcat(ret, "_", len) >= len || |
| 1166 | BUF_strlcat(ret, prf_name, len) >= len) { |
| 1167 | assert(0); |
| 1168 | OPENSSL_free(ret); |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | assert(strlen(ret) + 1 == len); |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) { |
| 1176 | if (cipher == NULL) { |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | if (out_alg_bits != NULL) { |
| 1181 | *out_alg_bits = cipher->alg_bits; |
| 1182 | } |
| 1183 | return cipher->strength_bits; |
| 1184 | } |
| 1185 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1186 | const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
| 1187 | int len) { |
| 1188 | const char *ver; |
| 1189 | const char *kx, *au, *enc, *mac; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1190 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1191 | 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] | 1192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1193 | alg_mkey = cipher->algorithm_mkey; |
| 1194 | alg_auth = cipher->algorithm_auth; |
| 1195 | alg_enc = cipher->algorithm_enc; |
| 1196 | alg_mac = cipher->algorithm_mac; |
| 1197 | alg_ssl = cipher->algorithm_ssl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1199 | if (alg_ssl & SSL_SSLV3) { |
| 1200 | ver = "SSLv3"; |
| 1201 | } else if (alg_ssl & SSL_TLSV1_2) { |
| 1202 | ver = "TLSv1.2"; |
| 1203 | } else { |
| 1204 | ver = "unknown"; |
| 1205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1206 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1207 | switch (alg_mkey) { |
| 1208 | case SSL_kRSA: |
| 1209 | kx = "RSA"; |
| 1210 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1211 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1212 | case SSL_kDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1213 | kx = "DH"; |
| 1214 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1216 | case SSL_kECDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1217 | kx = "ECDH"; |
| 1218 | break; |
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 | case SSL_kPSK: |
| 1221 | kx = "PSK"; |
| 1222 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1223 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1224 | default: |
| 1225 | kx = "unknown"; |
| 1226 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1227 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1228 | switch (alg_auth) { |
| 1229 | case SSL_aRSA: |
| 1230 | au = "RSA"; |
| 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_aECDSA: |
| 1234 | au = "ECDSA"; |
| 1235 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1236 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1237 | case SSL_aPSK: |
| 1238 | au = "PSK"; |
| 1239 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1240 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1241 | default: |
| 1242 | au = "unknown"; |
| 1243 | break; |
| 1244 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1245 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1246 | switch (alg_enc) { |
| 1247 | case SSL_3DES: |
| 1248 | enc = "3DES(168)"; |
| 1249 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1250 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1251 | case SSL_RC4: |
| 1252 | enc = "RC4(128)"; |
| 1253 | break; |
| 1254 | |
| 1255 | case SSL_AES128: |
| 1256 | enc = "AES(128)"; |
| 1257 | break; |
| 1258 | |
| 1259 | case SSL_AES256: |
| 1260 | enc = "AES(256)"; |
| 1261 | break; |
| 1262 | |
| 1263 | case SSL_AES128GCM: |
| 1264 | enc = "AESGCM(128)"; |
| 1265 | break; |
| 1266 | |
| 1267 | case SSL_AES256GCM: |
| 1268 | enc = "AESGCM(256)"; |
| 1269 | break; |
| 1270 | |
| 1271 | case SSL_CHACHA20POLY1305: |
| 1272 | enc = "ChaCha20-Poly1305"; |
| 1273 | break; |
| 1274 | |
| 1275 | default: |
| 1276 | enc = "unknown"; |
| 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | switch (alg_mac) { |
| 1281 | case SSL_MD5: |
| 1282 | mac = "MD5"; |
| 1283 | break; |
| 1284 | |
| 1285 | case SSL_SHA1: |
| 1286 | mac = "SHA1"; |
| 1287 | break; |
| 1288 | |
| 1289 | case SSL_SHA256: |
| 1290 | mac = "SHA256"; |
| 1291 | break; |
| 1292 | |
| 1293 | case SSL_SHA384: |
| 1294 | mac = "SHA384"; |
| 1295 | break; |
| 1296 | |
| 1297 | case SSL_AEAD: |
| 1298 | mac = "AEAD"; |
| 1299 | break; |
| 1300 | |
| 1301 | default: |
| 1302 | mac = "unknown"; |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | if (buf == NULL) { |
| 1307 | len = 128; |
| 1308 | buf = OPENSSL_malloc(len); |
David Benjamin | 1eed2c0 | 2015-02-08 23:20:06 -0500 | [diff] [blame] | 1309 | if (buf == NULL) { |
| 1310 | return NULL; |
| 1311 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1312 | } else if (len < 128) { |
| 1313 | return "Buffer too small"; |
| 1314 | } |
| 1315 | |
| 1316 | BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac); |
| 1317 | return buf; |
| 1318 | } |
| 1319 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1320 | const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) { |
| 1321 | return "TLSv1/SSLv3"; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1324 | void *SSL_COMP_get_compression_methods(void) { return NULL; } |
| 1325 | |
| 1326 | int SSL_COMP_add_compression_method(int id, void *cm) { return 1; } |
| 1327 | |
| 1328 | const char *SSL_COMP_get_name(const void *comp) { return NULL; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1330 | int ssl_cipher_get_cert_index(const SSL_CIPHER *cipher) { |
| 1331 | uint32_t alg_a = cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1332 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1333 | if (alg_a & SSL_aECDSA) { |
| 1334 | return SSL_PKEY_ECC; |
| 1335 | } else if (alg_a & SSL_aRSA) { |
| 1336 | return SSL_PKEY_RSA_ENC; |
| 1337 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1338 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1339 | return -1; |
| 1340 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1341 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1342 | int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) { |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 1343 | /* PSK-authenticated ciphers do not use a public key, except for |
| 1344 | * RSA_PSK. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1345 | if ((cipher->algorithm_auth & SSL_aPSK) && |
| 1346 | !(cipher->algorithm_mkey & SSL_kRSA)) { |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | /* All other ciphers include it. */ |
| 1351 | return 1; |
| 1352 | } |
| 1353 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1354 | int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
| 1355 | /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */ |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1356 | if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1357 | return 1; |
| 1358 | } |
| 1359 | |
| 1360 | /* It is optional in all others. */ |
| 1361 | return 0; |
| 1362 | } |