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