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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 144 | #include <stdio.h> |
| 145 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 147 | #include <openssl/buf.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 148 | #include <openssl/err.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 149 | #include <openssl/md5.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | #include <openssl/mem.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 151 | #include <openssl/sha.h> |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 152 | #include <openssl/stack.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 154 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 157 | /* kCiphers is an array of all supported ciphers, sorted by id. */ |
David Benjamin | 20c3731 | 2015-11-11 21:33:18 -0800 | [diff] [blame] | 158 | static const SSL_CIPHER kCiphers[] = { |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 159 | /* The RSA ciphers */ |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 160 | /* Cipher 02 */ |
| 161 | { |
| 162 | SSL3_TXT_RSA_NULL_SHA, SSL3_CK_RSA_NULL_SHA, SSL_kRSA, SSL_aRSA, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 163 | SSL_eNULL, SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 164 | }, |
| 165 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 166 | /* Cipher 04 */ |
| 167 | { |
| 168 | SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 169 | SSL_RC4, SSL_MD5, SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 170 | }, |
| 171 | |
| 172 | /* Cipher 05 */ |
| 173 | { |
| 174 | SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 175 | SSL_RC4, SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 176 | }, |
| 177 | |
| 178 | /* Cipher 0A */ |
| 179 | { |
| 180 | SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 181 | SSL_aRSA, SSL_3DES, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 182 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 183 | }, |
| 184 | |
| 185 | |
| 186 | /* New AES ciphersuites */ |
| 187 | |
| 188 | /* Cipher 2F */ |
| 189 | { |
| 190 | TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 191 | SSL_aRSA, SSL_AES128, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 192 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 193 | }, |
| 194 | |
| 195 | /* Cipher 33 */ |
| 196 | { |
| 197 | TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 198 | SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 199 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 200 | }, |
| 201 | |
| 202 | /* Cipher 35 */ |
| 203 | { |
| 204 | TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 205 | SSL_aRSA, SSL_AES256, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 206 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 207 | }, |
| 208 | |
| 209 | /* Cipher 39 */ |
| 210 | { |
| 211 | TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 212 | SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 213 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 214 | }, |
| 215 | |
| 216 | |
| 217 | /* TLS v1.2 ciphersuites */ |
| 218 | |
| 219 | /* Cipher 3C */ |
| 220 | { |
| 221 | TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 222 | SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 223 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 224 | }, |
| 225 | |
| 226 | /* Cipher 3D */ |
| 227 | { |
| 228 | TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256, |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 229 | SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 230 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 231 | }, |
| 232 | |
| 233 | /* Cipher 67 */ |
| 234 | { |
| 235 | TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256, |
| 236 | TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 237 | SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 238 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 239 | }, |
| 240 | |
| 241 | /* Cipher 6B */ |
| 242 | { |
| 243 | TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256, |
| 244 | TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 245 | SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 246 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 247 | }, |
| 248 | |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 249 | /* PSK cipher suites. */ |
| 250 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 251 | /* Cipher 8A */ |
| 252 | { |
| 253 | TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 254 | SSL_aPSK, SSL_RC4, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 255 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 256 | }, |
| 257 | |
| 258 | /* Cipher 8C */ |
| 259 | { |
| 260 | TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 261 | SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 262 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 263 | }, |
| 264 | |
| 265 | /* Cipher 8D */ |
| 266 | { |
| 267 | TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 268 | SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 269 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 270 | }, |
| 271 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 272 | /* GCM ciphersuites from RFC5288 */ |
| 273 | |
| 274 | /* Cipher 9C */ |
| 275 | { |
| 276 | TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256, |
| 277 | TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 278 | SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 279 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 280 | }, |
| 281 | |
| 282 | /* Cipher 9D */ |
| 283 | { |
| 284 | TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384, |
| 285 | TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 286 | SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 287 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 288 | }, |
| 289 | |
| 290 | /* Cipher 9E */ |
| 291 | { |
| 292 | TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256, |
| 293 | TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 294 | SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 295 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 296 | }, |
| 297 | |
| 298 | /* Cipher 9F */ |
| 299 | { |
| 300 | TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384, |
| 301 | TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 302 | SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 303 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 304 | }, |
| 305 | |
| 306 | /* Cipher C007 */ |
| 307 | { |
| 308 | TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA, |
| 309 | TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 310 | SSL_SHA1, SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 311 | }, |
| 312 | |
| 313 | /* Cipher C009 */ |
| 314 | { |
| 315 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, |
| 316 | TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 317 | SSL_AES128, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 318 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 319 | }, |
| 320 | |
| 321 | /* Cipher C00A */ |
| 322 | { |
| 323 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, |
| 324 | TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 325 | SSL_AES256, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 326 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 327 | }, |
| 328 | |
| 329 | /* Cipher C011 */ |
| 330 | { |
| 331 | TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 332 | SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 333 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 334 | }, |
| 335 | |
| 336 | /* Cipher C013 */ |
| 337 | { |
| 338 | TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA, |
| 339 | TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 340 | SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 341 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 342 | }, |
| 343 | |
| 344 | /* Cipher C014 */ |
| 345 | { |
| 346 | TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA, |
| 347 | TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 348 | SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 349 | SSL_HANDSHAKE_MAC_DEFAULT, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 350 | }, |
| 351 | |
| 352 | |
| 353 | /* HMAC based TLS v1.2 ciphersuites from RFC5289 */ |
| 354 | |
| 355 | /* Cipher C023 */ |
| 356 | { |
| 357 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256, |
| 358 | TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 359 | SSL_AES128, SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 360 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 361 | }, |
| 362 | |
| 363 | /* Cipher C024 */ |
| 364 | { |
| 365 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384, |
| 366 | TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 367 | SSL_AES256, SSL_SHA384, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 368 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 369 | }, |
| 370 | |
| 371 | /* Cipher C027 */ |
| 372 | { |
| 373 | TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256, |
| 374 | TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 375 | SSL_SHA256, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 376 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 377 | }, |
| 378 | |
| 379 | /* Cipher C028 */ |
| 380 | { |
| 381 | TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384, |
| 382 | TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 383 | SSL_SHA384, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 384 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 385 | }, |
| 386 | |
| 387 | |
| 388 | /* GCM based TLS v1.2 ciphersuites from RFC5289 */ |
| 389 | |
| 390 | /* Cipher C02B */ |
| 391 | { |
| 392 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 393 | TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 394 | SSL_AES128GCM, SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 395 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 396 | }, |
| 397 | |
| 398 | /* Cipher C02C */ |
| 399 | { |
| 400 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 401 | TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 402 | SSL_AES256GCM, SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 403 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 404 | }, |
| 405 | |
| 406 | /* Cipher C02F */ |
| 407 | { |
| 408 | TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256, |
| 409 | TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 410 | SSL_AES128GCM, SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 411 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 412 | }, |
| 413 | |
| 414 | /* Cipher C030 */ |
| 415 | { |
| 416 | TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384, |
| 417 | TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 418 | SSL_AES256GCM, SSL_AEAD, |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 419 | SSL_HANDSHAKE_MAC_SHA384, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 420 | }, |
| 421 | |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 422 | /* ECDHE-PSK cipher suites. */ |
| 423 | |
| 424 | /* Cipher C035 */ |
| 425 | { |
| 426 | TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
| 427 | TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 428 | SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 429 | SSL_HANDSHAKE_MAC_DEFAULT, |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 430 | }, |
| 431 | |
| 432 | /* Cipher C036 */ |
| 433 | { |
| 434 | TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
| 435 | TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 436 | SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 437 | SSL_HANDSHAKE_MAC_DEFAULT, |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 438 | }, |
| 439 | |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 440 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
Adam Langley | 85bc560 | 2015-06-09 09:54:04 -0700 | [diff] [blame] | 441 | /* ChaCha20-Poly1305 cipher suites. */ |
| 442 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 443 | { |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 444 | TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305_OLD, |
| 445 | TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aRSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 446 | SSL_CHACHA20POLY1305_OLD, SSL_AEAD, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 447 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 448 | }, |
| 449 | |
| 450 | { |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 451 | TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_OLD, |
| 452 | TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305_OLD, SSL_kECDHE, SSL_aECDSA, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 453 | SSL_CHACHA20POLY1305_OLD, SSL_AEAD, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 454 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 455 | }, |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 456 | #endif |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]); |
| 460 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 461 | #define CIPHER_ADD 1 |
| 462 | #define CIPHER_KILL 2 |
| 463 | #define CIPHER_DEL 3 |
| 464 | #define CIPHER_ORD 4 |
| 465 | #define CIPHER_SPECIAL 5 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | typedef struct cipher_order_st { |
| 468 | const SSL_CIPHER *cipher; |
| 469 | int active; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 470 | int in_group; |
| 471 | struct cipher_order_st *next, *prev; |
| 472 | } CIPHER_ORDER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 474 | typedef struct cipher_alias_st { |
| 475 | /* name is the name of the cipher alias. */ |
| 476 | const char *name; |
| 477 | |
| 478 | /* The following fields are bitmasks for the corresponding fields on |
| 479 | * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the |
| 480 | * bit corresponding to the cipher's value is set to 1. If any bitmask is |
| 481 | * all zeroes, the alias matches nothing. Use |~0u| for the default value. */ |
| 482 | uint32_t algorithm_mkey; |
| 483 | uint32_t algorithm_auth; |
| 484 | uint32_t algorithm_enc; |
| 485 | uint32_t algorithm_mac; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 486 | |
| 487 | /* min_version, if non-zero, matches all ciphers which were added in that |
| 488 | * particular protocol version. */ |
| 489 | uint16_t min_version; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 490 | } CIPHER_ALIAS; |
| 491 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 492 | static const CIPHER_ALIAS kCipherAliases[] = { |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 493 | /* "ALL" doesn't include eNULL (must be specifically enabled) */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 494 | {"ALL", ~0u, ~0u, ~SSL_eNULL, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 496 | /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 498 | /* key exchange aliases |
| 499 | * (some of those using only a single bit here combine |
| 500 | * multiple key exchange algs according to the RFCs, |
| 501 | * e.g. kEDH combines DHE_DSS and DHE_RSA) */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 502 | {"kRSA", SSL_kRSA, ~0u, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 504 | {"kDHE", SSL_kDHE, ~0u, ~0u, ~0u, 0}, |
| 505 | {"kEDH", SSL_kDHE, ~0u, ~0u, ~0u, 0}, |
| 506 | {"DH", SSL_kDHE, ~0u, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 508 | {"kECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 509 | {"kEECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 510 | {"ECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 512 | {"kPSK", SSL_kPSK, ~0u, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 514 | /* server authentication aliases */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 515 | {"aRSA", ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, 0}, |
| 516 | {"aECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
| 517 | {"ECDSA", ~0u, SSL_aECDSA, ~0u, ~0u, 0}, |
| 518 | {"aPSK", ~0u, SSL_aPSK, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 520 | /* aliases combining key exchange and server authentication */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 521 | {"DHE", SSL_kDHE, ~0u, ~0u, ~0u, 0}, |
| 522 | {"EDH", SSL_kDHE, ~0u, ~0u, ~0u, 0}, |
| 523 | {"ECDHE", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 524 | {"EECDH", SSL_kECDHE, ~0u, ~0u, ~0u, 0}, |
| 525 | {"RSA", SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, 0}, |
| 526 | {"PSK", SSL_kPSK, SSL_aPSK, ~0u, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 527 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 528 | /* symmetric encryption aliases */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 529 | {"3DES", ~0u, ~0u, SSL_3DES, ~0u, 0}, |
| 530 | {"RC4", ~0u, ~0u, SSL_RC4, ~0u, 0}, |
| 531 | {"AES128", ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, 0}, |
| 532 | {"AES256", ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, 0}, |
| 533 | {"AES", ~0u, ~0u, SSL_AES, ~0u, 0}, |
| 534 | {"AESGCM", ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, 0}, |
| 535 | {"CHACHA20", ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 537 | /* MAC aliases */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 538 | {"MD5", ~0u, ~0u, ~0u, SSL_MD5, 0}, |
| 539 | {"SHA1", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0}, |
| 540 | {"SHA", ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, 0}, |
| 541 | {"SHA256", ~0u, ~0u, ~0u, SSL_SHA256, 0}, |
| 542 | {"SHA384", ~0u, ~0u, ~0u, SSL_SHA384, 0}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 544 | /* Legacy protocol minimum version aliases. "TLSv1" is intentionally the |
| 545 | * same as "SSLv3". */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 546 | {"SSLv3", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION}, |
| 547 | {"TLSv1", ~0u, ~0u, ~SSL_eNULL, ~0u, SSL3_VERSION}, |
| 548 | {"TLSv1.2", ~0u, ~0u, ~SSL_eNULL, ~0u, TLS1_2_VERSION}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 550 | /* Legacy strength classes. */ |
| 551 | {"MEDIUM", ~0u, ~0u, SSL_RC4, ~0u, 0}, |
| 552 | {"HIGH", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0}, |
| 553 | {"FIPS", ~0u, ~0u, ~(SSL_eNULL|SSL_RC4), ~0u, 0}, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 556 | static const size_t kCipherAliasesLen = |
| 557 | sizeof(kCipherAliases) / sizeof(kCipherAliases[0]); |
| 558 | |
| 559 | static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) { |
| 560 | const SSL_CIPHER *a = in_a; |
| 561 | const SSL_CIPHER *b = in_b; |
| 562 | |
| 563 | if (a->id > b->id) { |
| 564 | return 1; |
| 565 | } else if (a->id < b->id) { |
| 566 | return -1; |
| 567 | } else { |
| 568 | return 0; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) { |
| 573 | return ssl_cipher_id_cmp(*a, *b); |
| 574 | } |
| 575 | |
| 576 | const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { |
| 577 | SSL_CIPHER c; |
| 578 | |
| 579 | c.id = 0x03000000L | value; |
| 580 | return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER), |
| 581 | ssl_cipher_id_cmp); |
| 582 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 583 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 584 | int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
| 585 | size_t *out_mac_secret_len, |
| 586 | size_t *out_fixed_iv_len, |
| 587 | const SSL_CIPHER *cipher, uint16_t version) { |
| 588 | *out_aead = NULL; |
| 589 | *out_mac_secret_len = 0; |
| 590 | *out_fixed_iv_len = 0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 591 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 592 | switch (cipher->algorithm_enc) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 593 | case SSL_AES128GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 594 | *out_aead = EVP_aead_aes_128_gcm(); |
| 595 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 596 | return 1; |
| 597 | |
| 598 | case SSL_AES256GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 599 | *out_aead = EVP_aead_aes_256_gcm(); |
| 600 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 601 | return 1; |
| 602 | |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 603 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 604 | case SSL_CHACHA20POLY1305_OLD: |
Brian Smith | 3e23e4c | 2015-10-03 11:38:58 -1000 | [diff] [blame] | 605 | *out_aead = EVP_aead_chacha20_poly1305_old(); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 606 | *out_fixed_iv_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 607 | return 1; |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 608 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 609 | |
| 610 | case SSL_RC4: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 611 | switch (cipher->algorithm_mac) { |
| 612 | case SSL_MD5: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 613 | if (version == SSL3_VERSION) { |
| 614 | *out_aead = EVP_aead_rc4_md5_ssl3(); |
| 615 | } else { |
| 616 | *out_aead = EVP_aead_rc4_md5_tls(); |
| 617 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 618 | *out_mac_secret_len = MD5_DIGEST_LENGTH; |
| 619 | return 1; |
| 620 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 621 | if (version == SSL3_VERSION) { |
| 622 | *out_aead = EVP_aead_rc4_sha1_ssl3(); |
| 623 | } else { |
| 624 | *out_aead = EVP_aead_rc4_sha1_tls(); |
| 625 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 626 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 627 | return 1; |
| 628 | default: |
| 629 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 630 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 631 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 632 | case SSL_AES128: |
| 633 | switch (cipher->algorithm_mac) { |
| 634 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 635 | if (version == SSL3_VERSION) { |
| 636 | *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3(); |
| 637 | *out_fixed_iv_len = 16; |
| 638 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 639 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(); |
| 640 | *out_fixed_iv_len = 16; |
| 641 | } else { |
| 642 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls(); |
| 643 | } |
| 644 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 645 | return 1; |
| 646 | case SSL_SHA256: |
| 647 | *out_aead = EVP_aead_aes_128_cbc_sha256_tls(); |
| 648 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 649 | return 1; |
| 650 | default: |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | case SSL_AES256: |
| 655 | switch (cipher->algorithm_mac) { |
| 656 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 657 | if (version == SSL3_VERSION) { |
| 658 | *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3(); |
| 659 | *out_fixed_iv_len = 16; |
| 660 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 661 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(); |
| 662 | *out_fixed_iv_len = 16; |
| 663 | } else { |
| 664 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls(); |
| 665 | } |
| 666 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 667 | return 1; |
| 668 | case SSL_SHA256: |
| 669 | *out_aead = EVP_aead_aes_256_cbc_sha256_tls(); |
| 670 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 671 | return 1; |
| 672 | case SSL_SHA384: |
| 673 | *out_aead = EVP_aead_aes_256_cbc_sha384_tls(); |
| 674 | *out_mac_secret_len = SHA384_DIGEST_LENGTH; |
| 675 | return 1; |
| 676 | default: |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | case SSL_3DES: |
| 681 | switch (cipher->algorithm_mac) { |
| 682 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 683 | if (version == SSL3_VERSION) { |
| 684 | *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3(); |
| 685 | *out_fixed_iv_len = 8; |
| 686 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 687 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(); |
| 688 | *out_fixed_iv_len = 8; |
| 689 | } else { |
| 690 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls(); |
| 691 | } |
| 692 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 693 | return 1; |
| 694 | default: |
| 695 | return 0; |
| 696 | } |
| 697 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 698 | case SSL_eNULL: |
| 699 | switch (cipher->algorithm_mac) { |
| 700 | case SSL_SHA1: |
| 701 | if (version == SSL3_VERSION) { |
| 702 | *out_aead = EVP_aead_null_sha1_ssl3(); |
| 703 | } else { |
| 704 | *out_aead = EVP_aead_null_sha1_tls(); |
| 705 | } |
| 706 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 707 | return 1; |
| 708 | default: |
| 709 | return 0; |
| 710 | } |
| 711 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 712 | default: |
| 713 | return 0; |
| 714 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 715 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 716 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 717 | const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) { |
| 718 | switch (algorithm_prf) { |
| 719 | case SSL_HANDSHAKE_MAC_DEFAULT: |
| 720 | return EVP_sha1(); |
| 721 | case SSL_HANDSHAKE_MAC_SHA256: |
| 722 | return EVP_sha256(); |
| 723 | case SSL_HANDSHAKE_MAC_SHA384: |
| 724 | return EVP_sha384(); |
| 725 | default: |
| 726 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 727 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | #define ITEM_SEP(a) \ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 731 | (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 733 | /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the |
| 734 | * |buf_len| bytes at |buf|. */ |
| 735 | static int rule_equals(const char *rule, const char *buf, size_t buf_len) { |
| 736 | /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */ |
| 737 | return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
| 738 | } |
| 739 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 740 | static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 741 | CIPHER_ORDER **tail) { |
| 742 | if (curr == *tail) { |
| 743 | return; |
| 744 | } |
| 745 | if (curr == *head) { |
| 746 | *head = curr->next; |
| 747 | } |
| 748 | if (curr->prev != NULL) { |
| 749 | curr->prev->next = curr->next; |
| 750 | } |
| 751 | if (curr->next != NULL) { |
| 752 | curr->next->prev = curr->prev; |
| 753 | } |
| 754 | (*tail)->next = curr; |
| 755 | curr->prev = *tail; |
| 756 | curr->next = NULL; |
| 757 | *tail = curr; |
| 758 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | |
| 760 | static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 761 | CIPHER_ORDER **tail) { |
| 762 | if (curr == *head) { |
| 763 | return; |
| 764 | } |
| 765 | if (curr == *tail) { |
| 766 | *tail = curr->prev; |
| 767 | } |
| 768 | if (curr->next != NULL) { |
| 769 | curr->next->prev = curr->prev; |
| 770 | } |
| 771 | if (curr->prev != NULL) { |
| 772 | curr->prev->next = curr->next; |
| 773 | } |
| 774 | (*head)->prev = curr; |
| 775 | curr->next = *head; |
| 776 | curr->prev = NULL; |
| 777 | *head = curr; |
| 778 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 779 | |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 780 | static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 781 | CIPHER_ORDER *co_list, |
| 782 | CIPHER_ORDER **head_p, |
| 783 | CIPHER_ORDER **tail_p) { |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 784 | /* The set of ciphers is static, but some subset may be unsupported by |
| 785 | * |ssl_method|, so the list may be smaller. */ |
| 786 | size_t co_list_num = 0; |
| 787 | size_t i; |
| 788 | for (i = 0; i < kCiphersLen; i++) { |
| 789 | const SSL_CIPHER *cipher = &kCiphers[i]; |
| 790 | if (ssl_method->supports_cipher(cipher)) { |
| 791 | co_list[co_list_num].cipher = cipher; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 792 | co_list[co_list_num].next = NULL; |
| 793 | co_list[co_list_num].prev = NULL; |
| 794 | co_list[co_list_num].active = 0; |
| 795 | co_list[co_list_num].in_group = 0; |
| 796 | co_list_num++; |
| 797 | } |
| 798 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 799 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 800 | /* Prepare linked list from list entries. */ |
| 801 | if (co_list_num > 0) { |
| 802 | co_list[0].prev = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 803 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 804 | if (co_list_num > 1) { |
| 805 | co_list[0].next = &co_list[1]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 807 | for (i = 1; i < co_list_num - 1; i++) { |
| 808 | co_list[i].prev = &co_list[i - 1]; |
| 809 | co_list[i].next = &co_list[i + 1]; |
| 810 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 811 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 812 | co_list[co_list_num - 1].prev = &co_list[co_list_num - 2]; |
| 813 | } |
| 814 | |
| 815 | co_list[co_list_num - 1].next = NULL; |
| 816 | |
| 817 | *head_p = &co_list[0]; |
| 818 | *tail_p = &co_list[co_list_num - 1]; |
| 819 | } |
| 820 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 822 | /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its |
| 823 | * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new |
| 824 | * head and tail of the list to |*head_p| and |*tail_p|, respectively. |
| 825 | * |
| 826 | * - If |cipher_id| is non-zero, only that cipher is selected. |
| 827 | * - Otherwise, if |strength_bits| is non-negative, it selects ciphers |
| 828 | * of that strength. |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 829 | * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 830 | * |min_version|. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | static void ssl_cipher_apply_rule( |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 832 | uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 833 | uint32_t alg_enc, uint32_t alg_mac, uint16_t min_version, int rule, |
| 834 | int strength_bits, int in_group, CIPHER_ORDER **head_p, |
| 835 | CIPHER_ORDER **tail_p) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 836 | CIPHER_ORDER *head, *tail, *curr, *next, *last; |
| 837 | const SSL_CIPHER *cp; |
| 838 | int reverse = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 840 | if (cipher_id == 0 && strength_bits == -1 && min_version == 0 && |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 841 | (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0)) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 842 | /* The rule matches nothing, so bail early. */ |
| 843 | return; |
| 844 | } |
| 845 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 846 | if (rule == CIPHER_DEL) { |
| 847 | /* needed to maintain sorting between currently deleted ciphers */ |
| 848 | reverse = 1; |
| 849 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 850 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 851 | head = *head_p; |
| 852 | tail = *tail_p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 853 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 854 | if (reverse) { |
| 855 | next = tail; |
| 856 | last = head; |
| 857 | } else { |
| 858 | next = head; |
| 859 | last = tail; |
| 860 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | curr = NULL; |
| 863 | for (;;) { |
| 864 | if (curr == last) { |
| 865 | break; |
| 866 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 867 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 868 | curr = next; |
| 869 | if (curr == NULL) { |
| 870 | break; |
| 871 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 872 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 873 | next = reverse ? curr->prev : curr->next; |
| 874 | cp = curr->cipher; |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 875 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 876 | /* Selection criteria is either a specific cipher, the value of |
| 877 | * |strength_bits|, or the algorithms used. */ |
| 878 | if (cipher_id != 0) { |
| 879 | if (cipher_id != cp->id) { |
| 880 | continue; |
| 881 | } |
| 882 | } else if (strength_bits >= 0) { |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 883 | if (strength_bits != SSL_CIPHER_get_bits(cp, NULL)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 884 | continue; |
| 885 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 886 | } else if (!(alg_mkey & cp->algorithm_mkey) || |
| 887 | !(alg_auth & cp->algorithm_auth) || |
| 888 | !(alg_enc & cp->algorithm_enc) || |
| 889 | !(alg_mac & cp->algorithm_mac) || |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 890 | (min_version != 0 && |
| 891 | SSL_CIPHER_get_min_version(cp) != min_version)) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 892 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 893 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 894 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 895 | /* add the cipher if it has not been added yet. */ |
| 896 | if (rule == CIPHER_ADD) { |
| 897 | /* reverse == 0 */ |
| 898 | if (!curr->active) { |
| 899 | ll_append_tail(&head, curr, &tail); |
| 900 | curr->active = 1; |
| 901 | curr->in_group = in_group; |
| 902 | } |
| 903 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 904 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 905 | /* Move the added cipher to this location */ |
| 906 | else if (rule == CIPHER_ORD) { |
| 907 | /* reverse == 0 */ |
| 908 | if (curr->active) { |
| 909 | ll_append_tail(&head, curr, &tail); |
| 910 | curr->in_group = 0; |
| 911 | } |
| 912 | } else if (rule == CIPHER_DEL) { |
| 913 | /* reverse == 1 */ |
| 914 | if (curr->active) { |
| 915 | /* most recently deleted ciphersuites get best positions |
| 916 | * for any future CIPHER_ADD (note that the CIPHER_DEL loop |
| 917 | * works in reverse to maintain the order) */ |
| 918 | ll_append_head(&head, curr, &tail); |
| 919 | curr->active = 0; |
| 920 | curr->in_group = 0; |
| 921 | } |
| 922 | } else if (rule == CIPHER_KILL) { |
| 923 | /* reverse == 0 */ |
| 924 | if (head == curr) { |
| 925 | head = curr->next; |
| 926 | } else { |
| 927 | curr->prev->next = curr->next; |
| 928 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 929 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 930 | if (tail == curr) { |
| 931 | tail = curr->prev; |
| 932 | } |
| 933 | curr->active = 0; |
| 934 | if (curr->next != NULL) { |
| 935 | curr->next->prev = curr->prev; |
| 936 | } |
| 937 | if (curr->prev != NULL) { |
| 938 | curr->prev->next = curr->next; |
| 939 | } |
| 940 | curr->next = NULL; |
| 941 | curr->prev = NULL; |
| 942 | } |
| 943 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 944 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 945 | *head_p = head; |
| 946 | *tail_p = tail; |
| 947 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 948 | |
| 949 | static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 950 | CIPHER_ORDER **tail_p) { |
| 951 | int max_strength_bits, i, *number_uses; |
| 952 | CIPHER_ORDER *curr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 953 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 954 | /* This routine sorts the ciphers with descending strength. The sorting must |
| 955 | * keep the pre-sorted sequence, so we apply the normal sorting routine as |
| 956 | * '+' movement to the end of the list. */ |
| 957 | max_strength_bits = 0; |
| 958 | curr = *head_p; |
| 959 | while (curr != NULL) { |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 960 | if (curr->active && |
| 961 | SSL_CIPHER_get_bits(curr->cipher, NULL) > max_strength_bits) { |
| 962 | max_strength_bits = SSL_CIPHER_get_bits(curr->cipher, NULL); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 963 | } |
| 964 | curr = curr->next; |
| 965 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 967 | number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); |
| 968 | if (!number_uses) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 969 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 973 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | /* Now find the strength_bits values actually used. */ |
| 975 | curr = *head_p; |
| 976 | while (curr != NULL) { |
| 977 | if (curr->active) { |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 978 | number_uses[SSL_CIPHER_get_bits(curr->cipher, NULL)]++; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 979 | } |
| 980 | curr = curr->next; |
| 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 | /* Go through the list of used strength_bits values in descending order. */ |
| 984 | for (i = max_strength_bits; i >= 0; i--) { |
| 985 | if (number_uses[i] > 0) { |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 986 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, tail_p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | OPENSSL_free(number_uses); |
| 991 | return 1; |
| 992 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 993 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 994 | static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method, |
| 995 | const char *rule_str, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 996 | CIPHER_ORDER **head_p, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 997 | CIPHER_ORDER **tail_p) { |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 998 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 999 | uint16_t min_version; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1000 | const char *l, *buf; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1001 | int multi, skip_rule, rule, retval, ok, in_group = 0, has_group = 0; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1002 | size_t j, buf_len; |
| 1003 | uint32_t cipher_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1004 | char ch; |
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 | retval = 1; |
| 1007 | l = rule_str; |
| 1008 | for (;;) { |
| 1009 | ch = *l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1010 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1011 | if (ch == '\0') { |
| 1012 | break; /* done */ |
| 1013 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1015 | if (in_group) { |
| 1016 | if (ch == ']') { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1017 | if (*tail_p) { |
| 1018 | (*tail_p)->in_group = 0; |
| 1019 | } |
| 1020 | in_group = 0; |
| 1021 | l++; |
| 1022 | continue; |
| 1023 | } |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 1024 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1025 | if (ch == '|') { |
| 1026 | rule = CIPHER_ADD; |
| 1027 | l++; |
| 1028 | continue; |
| 1029 | } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && |
| 1030 | !(ch >= '0' && ch <= '9')) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1031 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1032 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1033 | break; |
| 1034 | } else { |
| 1035 | rule = CIPHER_ADD; |
| 1036 | } |
| 1037 | } else if (ch == '-') { |
| 1038 | rule = CIPHER_DEL; |
| 1039 | l++; |
| 1040 | } else if (ch == '+') { |
| 1041 | rule = CIPHER_ORD; |
| 1042 | l++; |
| 1043 | } else if (ch == '!') { |
| 1044 | rule = CIPHER_KILL; |
| 1045 | l++; |
| 1046 | } else if (ch == '@') { |
| 1047 | rule = CIPHER_SPECIAL; |
| 1048 | l++; |
| 1049 | } else if (ch == '[') { |
| 1050 | if (in_group) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1051 | OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1052 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1053 | break; |
| 1054 | } |
| 1055 | in_group = 1; |
| 1056 | has_group = 1; |
| 1057 | l++; |
| 1058 | continue; |
| 1059 | } else { |
| 1060 | rule = CIPHER_ADD; |
| 1061 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1062 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1063 | /* If preference groups are enabled, the only legal operator is +. |
| 1064 | * Otherwise the in_group bits will get mixed up. */ |
| 1065 | if (has_group && rule != CIPHER_ADD) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1066 | OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1067 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1068 | break; |
| 1069 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1070 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1071 | if (ITEM_SEP(ch)) { |
| 1072 | l++; |
| 1073 | continue; |
| 1074 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1075 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1076 | multi = 0; |
| 1077 | cipher_id = 0; |
| 1078 | alg_mkey = ~0u; |
| 1079 | alg_auth = ~0u; |
| 1080 | alg_enc = ~0u; |
| 1081 | alg_mac = ~0u; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1082 | min_version = 0; |
| 1083 | skip_rule = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1084 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1085 | for (;;) { |
| 1086 | ch = *l; |
| 1087 | buf = l; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1088 | buf_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1089 | while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || |
| 1090 | ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) { |
| 1091 | ch = *(++l); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1092 | buf_len++; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1093 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1095 | if (buf_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1096 | /* We hit something we cannot deal with, it is no command or separator |
| 1097 | * nor alphanumeric, so we call this an error. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1098 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1099 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1100 | l++; |
| 1101 | break; |
| 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1104 | if (rule == CIPHER_SPECIAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1105 | break; |
| 1106 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1107 | |
| 1108 | /* Look for a matching exact cipher. These aren't allowed in multipart |
| 1109 | * rules. */ |
| 1110 | if (!multi && ch != '+') { |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1111 | for (j = 0; j < kCiphersLen; j++) { |
| 1112 | const SSL_CIPHER *cipher = &kCiphers[j]; |
| 1113 | if (rule_equals(cipher->name, buf, buf_len)) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1114 | cipher_id = cipher->id; |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | if (cipher_id == 0) { |
| 1120 | /* If not an exact cipher, look for a matching cipher alias. */ |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1121 | for (j = 0; j < kCipherAliasesLen; j++) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1122 | if (rule_equals(kCipherAliases[j].name, buf, buf_len)) { |
| 1123 | alg_mkey &= kCipherAliases[j].algorithm_mkey; |
| 1124 | alg_auth &= kCipherAliases[j].algorithm_auth; |
| 1125 | alg_enc &= kCipherAliases[j].algorithm_enc; |
| 1126 | alg_mac &= kCipherAliases[j].algorithm_mac; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1127 | |
| 1128 | if (min_version != 0 && |
| 1129 | min_version != kCipherAliases[j].min_version) { |
| 1130 | skip_rule = 1; |
| 1131 | } else { |
| 1132 | min_version = kCipherAliases[j].min_version; |
| 1133 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1137 | if (j == kCipherAliasesLen) { |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1138 | skip_rule = 1; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | /* Check for a multipart rule. */ |
| 1143 | if (ch != '+') { |
| 1144 | break; |
| 1145 | } |
| 1146 | l++; |
| 1147 | multi = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 | /* Ok, we have the rule, now apply it. */ |
| 1151 | if (rule == CIPHER_SPECIAL) { |
| 1152 | /* special command */ |
| 1153 | ok = 0; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1154 | if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1155 | ok = ssl_cipher_strength_sort(head_p, tail_p); |
| 1156 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1157 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1158 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1159 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1160 | if (ok == 0) { |
| 1161 | retval = 0; |
| 1162 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1163 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1164 | /* We do not support any "multi" options together with "@", so throw away |
| 1165 | * the rest of the command, if any left, until end or ':' is found. */ |
| 1166 | while (*l != '\0' && !ITEM_SEP(*l)) { |
| 1167 | l++; |
| 1168 | } |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1169 | } else if (!skip_rule) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1170 | ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac, |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1171 | min_version, rule, -1, in_group, head_p, tail_p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1175 | if (in_group) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1176 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1177 | retval = 0; |
| 1178 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1179 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1180 | return retval; |
| 1181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1182 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1183 | STACK_OF(SSL_CIPHER) * |
| 1184 | ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method, |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1185 | struct ssl_cipher_preference_list_st **out_cipher_list, |
| 1186 | STACK_OF(SSL_CIPHER) **out_cipher_list_by_id, |
| 1187 | const char *rule_str) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1188 | int ok; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1189 | STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL; |
| 1190 | const char *rule_p; |
| 1191 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1192 | uint8_t *in_group_flags = NULL; |
| 1193 | unsigned int num_in_group_flags = 0; |
| 1194 | struct ssl_cipher_preference_list_st *pref_list = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1195 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1196 | /* Return with error if nothing to do. */ |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1197 | if (rule_str == NULL || out_cipher_list == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1198 | return NULL; |
| 1199 | } |
David Benjamin | 5213df4 | 2014-08-20 14:19:54 -0400 | [diff] [blame] | 1200 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1201 | /* Now we have to collect the available ciphers from the compiled in ciphers. |
| 1202 | * We cannot get more than the number compiled in, so it is used for |
| 1203 | * allocation. */ |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1204 | co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1205 | if (co_list == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1206 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1207 | return NULL; |
| 1208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1209 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1210 | ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1211 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1212 | /* Now arrange all ciphers by preference: |
| 1213 | * TODO(davidben): Compute this order once and copy it. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1214 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1215 | /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other |
| 1216 | * key exchange mechanisms */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1217 | ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, 0, CIPHER_ADD, -1, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1218 | 0, &head, &tail); |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1219 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1220 | &head, &tail); |
| 1221 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, |
| 1222 | &head, &tail); |
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 | /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer |
| 1225 | * CHACHA20 unless there is hardware support for fast and constant-time |
| 1226 | * AES_GCM. */ |
| 1227 | if (EVP_has_aes_hardware()) { |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1228 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1229 | &head, &tail); |
| 1230 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1231 | &head, &tail); |
| 1232 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1233 | CIPHER_ADD, -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1234 | } else { |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1235 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305_OLD, ~0u, 0, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1236 | CIPHER_ADD, -1, 0, &head, &tail); |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1237 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1238 | &head, &tail); |
| 1239 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1240 | &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1241 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1242 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1243 | /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA, |
| 1244 | * RC4_128_MD5, 3DES_EDE_CBC_SHA. */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1245 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, 0, CIPHER_ADD, -1, 0, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1246 | &head, &tail); |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1247 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, 0, CIPHER_ADD, -1, 0, |
| 1248 | &head, &tail); |
| 1249 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, 0, CIPHER_ADD, -1, 0, |
| 1250 | &head, &tail); |
| 1251 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, 0, CIPHER_ADD, -1, 0, |
| 1252 | &head, &tail); |
| 1253 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, 0, CIPHER_ADD, -1, 0, &head, |
| 1254 | &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1255 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1256 | /* Temporarily enable everything else for sorting */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1257 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_ADD, -1, 0, &head, |
| 1258 | &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1259 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1260 | /* Move ciphers without forward secrecy to the end. */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1261 | ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, 0, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1262 | CIPHER_ORD, -1, 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1264 | /* Now disable everything (maintaining the ordering!) */ |
David Benjamin | d6e9eec | 2015-11-18 09:48:55 -0500 | [diff] [blame] | 1265 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, 0, CIPHER_DEL, -1, 0, &head, |
| 1266 | &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1268 | /* If the rule_string begins with DEFAULT, apply the default rule before |
| 1269 | * using the (possibly available) additional rules. */ |
| 1270 | ok = 1; |
| 1271 | rule_p = rule_str; |
| 1272 | if (strncmp(rule_str, "DEFAULT", 7) == 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1273 | ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head, |
| 1274 | &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1275 | rule_p += 7; |
| 1276 | if (*rule_p == ':') { |
| 1277 | rule_p++; |
| 1278 | } |
| 1279 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1280 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1281 | if (ok && strlen(rule_p) > 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1282 | ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1283 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1284 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1285 | if (!ok) { |
| 1286 | goto err; |
| 1287 | } |
| 1288 | |
| 1289 | /* Allocate new "cipherstack" for the result, return with error |
| 1290 | * if we cannot get one. */ |
| 1291 | cipherstack = sk_SSL_CIPHER_new_null(); |
| 1292 | if (cipherstack == NULL) { |
| 1293 | goto err; |
| 1294 | } |
| 1295 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1296 | in_group_flags = OPENSSL_malloc(kCiphersLen); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1297 | if (!in_group_flags) { |
| 1298 | goto err; |
| 1299 | } |
| 1300 | |
| 1301 | /* The cipher selection for the list is done. The ciphers are added |
| 1302 | * to the resulting precedence to the STACK_OF(SSL_CIPHER). */ |
| 1303 | for (curr = head; curr != NULL; curr = curr->next) { |
| 1304 | if (curr->active) { |
David Benjamin | 2adb7ec | 2015-01-11 19:59:06 -0500 | [diff] [blame] | 1305 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { |
| 1306 | goto err; |
| 1307 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1308 | in_group_flags[num_in_group_flags++] = curr->in_group; |
| 1309 | } |
| 1310 | } |
| 1311 | OPENSSL_free(co_list); /* Not needed any longer */ |
| 1312 | co_list = NULL; |
| 1313 | |
| 1314 | tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); |
| 1315 | if (tmp_cipher_list == NULL) { |
| 1316 | goto err; |
| 1317 | } |
| 1318 | pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 1319 | if (!pref_list) { |
| 1320 | goto err; |
| 1321 | } |
| 1322 | pref_list->ciphers = cipherstack; |
| 1323 | pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags); |
| 1324 | if (!pref_list->in_group_flags) { |
| 1325 | goto err; |
| 1326 | } |
| 1327 | memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags); |
| 1328 | OPENSSL_free(in_group_flags); |
| 1329 | in_group_flags = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1330 | if (*out_cipher_list != NULL) { |
| 1331 | ssl_cipher_preference_list_free(*out_cipher_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1332 | } |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1333 | *out_cipher_list = pref_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1334 | pref_list = NULL; |
| 1335 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1336 | if (out_cipher_list_by_id != NULL) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1337 | sk_SSL_CIPHER_free(*out_cipher_list_by_id); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1338 | *out_cipher_list_by_id = tmp_cipher_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1339 | tmp_cipher_list = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1340 | (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id, |
| 1341 | ssl_cipher_ptr_id_cmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1342 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1343 | sk_SSL_CIPHER_sort(*out_cipher_list_by_id); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1344 | } else { |
| 1345 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1346 | tmp_cipher_list = NULL; |
| 1347 | } |
| 1348 | |
| 1349 | return cipherstack; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1350 | |
| 1351 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1352 | OPENSSL_free(co_list); |
| 1353 | OPENSSL_free(in_group_flags); |
| 1354 | sk_SSL_CIPHER_free(cipherstack); |
| 1355 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1356 | if (pref_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1357 | OPENSSL_free(pref_list->in_group_flags); |
| 1358 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1359 | OPENSSL_free(pref_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1360 | return NULL; |
| 1361 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1363 | uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; } |
| 1364 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1365 | uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) { |
| 1366 | uint32_t id = cipher->id; |
| 1367 | /* All ciphers are SSLv3. */ |
| 1368 | assert((id & 0xff000000) == 0x03000000); |
| 1369 | return id & 0xffff; |
| 1370 | } |
| 1371 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1372 | int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) { |
| 1373 | return (cipher->algorithm_enc & SSL_AES) != 0; |
| 1374 | } |
| 1375 | |
| 1376 | int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) { |
| 1377 | return (cipher->algorithm_mac & SSL_MD5) != 0; |
| 1378 | } |
| 1379 | |
David Benjamin | ef793f4 | 2015-11-05 18:16:27 -0500 | [diff] [blame] | 1380 | int SSL_CIPHER_has_SHA1_HMAC(const SSL_CIPHER *cipher) { |
| 1381 | return (cipher->algorithm_mac & SSL_SHA1) != 0; |
| 1382 | } |
| 1383 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1384 | int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) { |
David Benjamin | c0125ef | 2015-09-09 09:11:07 -0400 | [diff] [blame] | 1385 | return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
David Benjamin | ef793f4 | 2015-11-05 18:16:27 -0500 | [diff] [blame] | 1388 | int SSL_CIPHER_is_AES128GCM(const SSL_CIPHER *cipher) { |
| 1389 | return (cipher->algorithm_enc & SSL_AES128GCM) != 0; |
| 1390 | } |
| 1391 | |
Adam Langley | b00061c | 2015-11-16 17:44:52 -0800 | [diff] [blame] | 1392 | int SSL_CIPHER_is_AES128CBC(const SSL_CIPHER *cipher) { |
| 1393 | return (cipher->algorithm_enc & SSL_AES128) != 0; |
| 1394 | } |
| 1395 | |
| 1396 | int SSL_CIPHER_is_AES256CBC(const SSL_CIPHER *cipher) { |
| 1397 | return (cipher->algorithm_enc & SSL_AES256) != 0; |
| 1398 | } |
| 1399 | |
David Benjamin | 51a01a5 | 2015-10-29 13:19:56 -0400 | [diff] [blame] | 1400 | int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) { |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 1401 | return (cipher->algorithm_enc & SSL_CHACHA20POLY1305_OLD) != 0; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1402 | } |
| 1403 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 1404 | int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) { |
| 1405 | return (cipher->algorithm_enc & SSL_eNULL) != 0; |
| 1406 | } |
| 1407 | |
Adam Langley | d7fe75c | 2015-09-18 15:40:48 -0700 | [diff] [blame] | 1408 | int SSL_CIPHER_is_RC4(const SSL_CIPHER *cipher) { |
| 1409 | return (cipher->algorithm_enc & SSL_RC4) != 0; |
| 1410 | } |
| 1411 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 1412 | int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) { |
| 1413 | /* Neither stream cipher nor AEAD. */ |
| 1414 | return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 && |
| 1415 | cipher->algorithm_mac != SSL_AEAD; |
| 1416 | } |
| 1417 | |
David Benjamin | ef793f4 | 2015-11-05 18:16:27 -0500 | [diff] [blame] | 1418 | int SSL_CIPHER_is_ECDSA(const SSL_CIPHER *cipher) { |
| 1419 | return (cipher->algorithm_auth & SSL_aECDSA) != 0; |
| 1420 | } |
| 1421 | |
| 1422 | uint16_t SSL_CIPHER_get_min_version(const SSL_CIPHER *cipher) { |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1423 | if (cipher->algorithm_prf != SSL_HANDSHAKE_MAC_DEFAULT) { |
| 1424 | /* Cipher suites before TLS 1.2 use the default PRF, while all those added |
| 1425 | * afterwards specify a particular hash. */ |
David Benjamin | ef793f4 | 2015-11-05 18:16:27 -0500 | [diff] [blame] | 1426 | return TLS1_2_VERSION; |
| 1427 | } |
| 1428 | return SSL3_VERSION; |
| 1429 | } |
| 1430 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1431 | /* return the actual cipher being used */ |
| 1432 | const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) { |
| 1433 | if (cipher != NULL) { |
| 1434 | return cipher->name; |
| 1435 | } |
| 1436 | |
| 1437 | return "(NONE)"; |
| 1438 | } |
| 1439 | |
| 1440 | const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
| 1441 | if (cipher == NULL) { |
| 1442 | return ""; |
| 1443 | } |
| 1444 | |
| 1445 | switch (cipher->algorithm_mkey) { |
| 1446 | case SSL_kRSA: |
| 1447 | return "RSA"; |
| 1448 | |
| 1449 | case SSL_kDHE: |
| 1450 | switch (cipher->algorithm_auth) { |
| 1451 | case SSL_aRSA: |
| 1452 | return "DHE_RSA"; |
| 1453 | default: |
| 1454 | assert(0); |
| 1455 | return "UNKNOWN"; |
| 1456 | } |
| 1457 | |
| 1458 | case SSL_kECDHE: |
| 1459 | switch (cipher->algorithm_auth) { |
| 1460 | case SSL_aECDSA: |
| 1461 | return "ECDHE_ECDSA"; |
| 1462 | case SSL_aRSA: |
| 1463 | return "ECDHE_RSA"; |
| 1464 | case SSL_aPSK: |
| 1465 | return "ECDHE_PSK"; |
| 1466 | default: |
| 1467 | assert(0); |
| 1468 | return "UNKNOWN"; |
| 1469 | } |
| 1470 | |
| 1471 | case SSL_kPSK: |
| 1472 | assert(cipher->algorithm_auth == SSL_aPSK); |
| 1473 | return "PSK"; |
| 1474 | |
| 1475 | default: |
| 1476 | assert(0); |
| 1477 | return "UNKNOWN"; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) { |
| 1482 | switch (cipher->algorithm_enc) { |
| 1483 | case SSL_3DES: |
| 1484 | return "3DES_EDE_CBC"; |
| 1485 | case SSL_RC4: |
| 1486 | return "RC4"; |
| 1487 | case SSL_AES128: |
| 1488 | return "AES_128_CBC"; |
| 1489 | case SSL_AES256: |
| 1490 | return "AES_256_CBC"; |
| 1491 | case SSL_AES128GCM: |
| 1492 | return "AES_128_GCM"; |
| 1493 | case SSL_AES256GCM: |
| 1494 | return "AES_256_GCM"; |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 1495 | case SSL_CHACHA20POLY1305_OLD: |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1496 | return "CHACHA20_POLY1305"; |
| 1497 | break; |
| 1498 | default: |
| 1499 | assert(0); |
| 1500 | return "UNKNOWN"; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 1505 | switch (cipher->algorithm_prf) { |
| 1506 | case SSL_HANDSHAKE_MAC_DEFAULT: |
| 1507 | /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is |
| 1508 | * only ever MD5 or SHA-1. */ |
| 1509 | switch (cipher->algorithm_mac) { |
| 1510 | case SSL_MD5: |
| 1511 | return "MD5"; |
| 1512 | case SSL_SHA1: |
| 1513 | return "SHA"; |
| 1514 | } |
| 1515 | break; |
| 1516 | case SSL_HANDSHAKE_MAC_SHA256: |
| 1517 | return "SHA256"; |
| 1518 | case SSL_HANDSHAKE_MAC_SHA384: |
| 1519 | return "SHA384"; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1520 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 1521 | assert(0); |
| 1522 | return "UNKNOWN"; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) { |
| 1526 | if (cipher == NULL) { |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | |
| 1530 | const char *kx_name = SSL_CIPHER_get_kx_name(cipher); |
| 1531 | const char *enc_name = ssl_cipher_get_enc_name(cipher); |
| 1532 | const char *prf_name = ssl_cipher_get_prf_name(cipher); |
| 1533 | |
| 1534 | /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */ |
| 1535 | size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 + |
| 1536 | strlen(prf_name) + 1; |
| 1537 | char *ret = OPENSSL_malloc(len); |
| 1538 | if (ret == NULL) { |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | if (BUF_strlcpy(ret, "TLS_", len) >= len || |
| 1542 | BUF_strlcat(ret, kx_name, len) >= len || |
| 1543 | BUF_strlcat(ret, "_WITH_", len) >= len || |
| 1544 | BUF_strlcat(ret, enc_name, len) >= len || |
| 1545 | BUF_strlcat(ret, "_", len) >= len || |
| 1546 | BUF_strlcat(ret, prf_name, len) >= len) { |
| 1547 | assert(0); |
| 1548 | OPENSSL_free(ret); |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | assert(strlen(ret) + 1 == len); |
| 1552 | return ret; |
| 1553 | } |
| 1554 | |
| 1555 | int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) { |
| 1556 | if (cipher == NULL) { |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 1560 | int alg_bits, strength_bits; |
| 1561 | switch (cipher->algorithm_enc) { |
| 1562 | case SSL_AES128: |
| 1563 | case SSL_AES128GCM: |
| 1564 | case SSL_RC4: |
| 1565 | alg_bits = 128; |
| 1566 | strength_bits = 128; |
| 1567 | break; |
| 1568 | |
| 1569 | case SSL_AES256: |
| 1570 | case SSL_AES256GCM: |
| 1571 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
| 1572 | case SSL_CHACHA20POLY1305_OLD: |
| 1573 | #endif |
| 1574 | alg_bits = 256; |
| 1575 | strength_bits = 256; |
| 1576 | break; |
| 1577 | |
| 1578 | case SSL_3DES: |
| 1579 | alg_bits = 168; |
| 1580 | strength_bits = 112; |
| 1581 | break; |
| 1582 | |
| 1583 | case SSL_eNULL: |
| 1584 | alg_bits = 0; |
| 1585 | strength_bits = 0; |
| 1586 | break; |
| 1587 | |
| 1588 | default: |
| 1589 | assert(0); |
| 1590 | alg_bits = 0; |
| 1591 | strength_bits = 0; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1592 | } |
David Benjamin | 9f2e277 | 2015-11-18 09:59:43 -0500 | [diff] [blame^] | 1593 | |
| 1594 | if (out_alg_bits != NULL) { |
| 1595 | *out_alg_bits = alg_bits; |
| 1596 | } |
| 1597 | return strength_bits; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1600 | const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
| 1601 | int len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1602 | const char *kx, *au, *enc, *mac; |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1603 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac; |
| 1604 | static const char *format = "%-23s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1605 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1606 | alg_mkey = cipher->algorithm_mkey; |
| 1607 | alg_auth = cipher->algorithm_auth; |
| 1608 | alg_enc = cipher->algorithm_enc; |
| 1609 | alg_mac = cipher->algorithm_mac; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1610 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1611 | switch (alg_mkey) { |
| 1612 | case SSL_kRSA: |
| 1613 | kx = "RSA"; |
| 1614 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1615 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1616 | case SSL_kDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1617 | kx = "DH"; |
| 1618 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1619 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1620 | case SSL_kECDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1621 | kx = "ECDH"; |
| 1622 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1623 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1624 | case SSL_kPSK: |
| 1625 | kx = "PSK"; |
| 1626 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1627 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1628 | default: |
| 1629 | kx = "unknown"; |
| 1630 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1631 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1632 | switch (alg_auth) { |
| 1633 | case SSL_aRSA: |
| 1634 | au = "RSA"; |
| 1635 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1636 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1637 | case SSL_aECDSA: |
| 1638 | au = "ECDSA"; |
| 1639 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1640 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1641 | case SSL_aPSK: |
| 1642 | au = "PSK"; |
| 1643 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1644 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1645 | default: |
| 1646 | au = "unknown"; |
| 1647 | break; |
| 1648 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1649 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1650 | switch (alg_enc) { |
| 1651 | case SSL_3DES: |
| 1652 | enc = "3DES(168)"; |
| 1653 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1654 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1655 | case SSL_RC4: |
| 1656 | enc = "RC4(128)"; |
| 1657 | break; |
| 1658 | |
| 1659 | case SSL_AES128: |
| 1660 | enc = "AES(128)"; |
| 1661 | break; |
| 1662 | |
| 1663 | case SSL_AES256: |
| 1664 | enc = "AES(256)"; |
| 1665 | break; |
| 1666 | |
| 1667 | case SSL_AES128GCM: |
| 1668 | enc = "AESGCM(128)"; |
| 1669 | break; |
| 1670 | |
| 1671 | case SSL_AES256GCM: |
| 1672 | enc = "AESGCM(256)"; |
| 1673 | break; |
| 1674 | |
Brian Smith | 271777f | 2015-10-03 13:53:33 -1000 | [diff] [blame] | 1675 | case SSL_CHACHA20POLY1305_OLD: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1676 | enc = "ChaCha20-Poly1305"; |
| 1677 | break; |
| 1678 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 1679 | case SSL_eNULL: |
| 1680 | enc="None"; |
| 1681 | break; |
| 1682 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1683 | default: |
| 1684 | enc = "unknown"; |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | switch (alg_mac) { |
| 1689 | case SSL_MD5: |
| 1690 | mac = "MD5"; |
| 1691 | break; |
| 1692 | |
| 1693 | case SSL_SHA1: |
| 1694 | mac = "SHA1"; |
| 1695 | break; |
| 1696 | |
| 1697 | case SSL_SHA256: |
| 1698 | mac = "SHA256"; |
| 1699 | break; |
| 1700 | |
| 1701 | case SSL_SHA384: |
| 1702 | mac = "SHA384"; |
| 1703 | break; |
| 1704 | |
| 1705 | case SSL_AEAD: |
| 1706 | mac = "AEAD"; |
| 1707 | break; |
| 1708 | |
| 1709 | default: |
| 1710 | mac = "unknown"; |
| 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | if (buf == NULL) { |
| 1715 | len = 128; |
| 1716 | buf = OPENSSL_malloc(len); |
David Benjamin | 1eed2c0 | 2015-02-08 23:20:06 -0500 | [diff] [blame] | 1717 | if (buf == NULL) { |
| 1718 | return NULL; |
| 1719 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1720 | } else if (len < 128) { |
| 1721 | return "Buffer too small"; |
| 1722 | } |
| 1723 | |
David Benjamin | dcb6ef0 | 2015-11-06 15:35:54 -0500 | [diff] [blame] | 1724 | BIO_snprintf(buf, len, format, cipher->name, kx, au, enc, mac); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1725 | return buf; |
| 1726 | } |
| 1727 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1728 | const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) { |
| 1729 | return "TLSv1/SSLv3"; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1732 | COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1733 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1734 | int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1735 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1736 | const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1737 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1738 | int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) { |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1739 | uint32_t alg_a = cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1740 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1741 | if (alg_a & SSL_aECDSA) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1742 | return EVP_PKEY_EC; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1743 | } else if (alg_a & SSL_aRSA) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1744 | return EVP_PKEY_RSA; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1745 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1746 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1747 | return EVP_PKEY_NONE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1748 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1749 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1750 | int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) { |
David Benjamin | df0905a | 2015-09-19 09:58:31 -0400 | [diff] [blame] | 1751 | /* PSK-authenticated ciphers do not use a certificate. (RSA_PSK is not |
| 1752 | * supported.) */ |
| 1753 | if (cipher->algorithm_auth & SSL_aPSK) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | /* All other ciphers include it. */ |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1761 | int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
| 1762 | /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */ |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1763 | if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1764 | return 1; |
| 1765 | } |
| 1766 | |
| 1767 | /* It is optional in all others. */ |
| 1768 | return 0; |
| 1769 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1770 | |
| 1771 | size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) { |
| 1772 | size_t block_size; |
| 1773 | switch (cipher->algorithm_enc) { |
| 1774 | case SSL_3DES: |
| 1775 | block_size = 8; |
| 1776 | break; |
| 1777 | case SSL_AES128: |
| 1778 | case SSL_AES256: |
| 1779 | block_size = 16; |
| 1780 | break; |
| 1781 | default: |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | size_t mac_len; |
| 1786 | switch (cipher->algorithm_mac) { |
| 1787 | case SSL_MD5: |
| 1788 | mac_len = MD5_DIGEST_LENGTH; |
| 1789 | break; |
| 1790 | case SSL_SHA1: |
| 1791 | mac_len = SHA_DIGEST_LENGTH; |
| 1792 | break; |
| 1793 | default: |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | size_t ret = 1 + mac_len; |
| 1798 | ret += block_size - (ret % block_size); |
| 1799 | return ret; |
| 1800 | } |