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