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