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