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