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