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. */ |
| 158 | const SSL_CIPHER kCiphers[] = { |
| 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 | |
| 451 | /* ChaCha20-Poly1305 cipher suites. */ |
| 452 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 453 | { |
| 454 | TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305, |
| 455 | TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA, |
| 456 | SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH, |
| 457 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | be8a869 | 2015-08-21 13:51:56 -0400 | [diff] [blame] | 458 | 256, 256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 459 | }, |
| 460 | |
| 461 | { |
| 462 | TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, |
| 463 | TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA, |
| 464 | SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH, |
| 465 | SSL_HANDSHAKE_MAC_SHA256, |
David Benjamin | be8a869 | 2015-08-21 13:51:56 -0400 | [diff] [blame] | 466 | 256, 256, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 467 | }, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]); |
| 471 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 472 | #define CIPHER_ADD 1 |
| 473 | #define CIPHER_KILL 2 |
| 474 | #define CIPHER_DEL 3 |
| 475 | #define CIPHER_ORD 4 |
| 476 | #define CIPHER_SPECIAL 5 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | typedef struct cipher_order_st { |
| 479 | const SSL_CIPHER *cipher; |
| 480 | int active; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 481 | int in_group; |
| 482 | struct cipher_order_st *next, *prev; |
| 483 | } CIPHER_ORDER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 485 | typedef struct cipher_alias_st { |
| 486 | /* name is the name of the cipher alias. */ |
| 487 | const char *name; |
| 488 | |
| 489 | /* The following fields are bitmasks for the corresponding fields on |
| 490 | * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the |
| 491 | * bit corresponding to the cipher's value is set to 1. If any bitmask is |
| 492 | * all zeroes, the alias matches nothing. Use |~0u| for the default value. */ |
| 493 | uint32_t algorithm_mkey; |
| 494 | uint32_t algorithm_auth; |
| 495 | uint32_t algorithm_enc; |
| 496 | uint32_t algorithm_mac; |
| 497 | uint32_t algorithm_ssl; |
| 498 | uint32_t algo_strength; |
| 499 | } CIPHER_ALIAS; |
| 500 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 501 | static const CIPHER_ALIAS kCipherAliases[] = { |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 502 | /* "ALL" doesn't include eNULL (must be specifically enabled) */ |
| 503 | {SSL_TXT_ALL, ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 505 | /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */ |
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 | /* key exchange aliases |
| 508 | * (some of those using only a single bit here combine |
| 509 | * multiple key exchange algs according to the RFCs, |
| 510 | * e.g. kEDH combines DHE_DSS and DHE_RSA) */ |
| 511 | {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 513 | {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 514 | {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 515 | {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 517 | {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 518 | {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 519 | {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 520 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 521 | {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 523 | /* server authentication aliases */ |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 524 | {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, ~0u}, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 525 | {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u}, |
| 526 | {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u}, |
| 527 | {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 529 | /* aliases combining key exchange and server authentication */ |
| 530 | {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 531 | {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 532 | {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
| 533 | {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u}, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 534 | {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~SSL_eNULL, ~0u, ~0u, ~0u}, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 535 | {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 537 | /* symmetric encryption aliases */ |
| 538 | {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u}, |
| 539 | {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u}, |
| 540 | {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u}, |
| 541 | {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u}, |
| 542 | {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u}, |
| 543 | {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u}, |
| 544 | {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 546 | /* MAC aliases */ |
| 547 | {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u}, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 548 | {SSL_TXT_SHA1, ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, ~0u}, |
| 549 | {SSL_TXT_SHA, ~0u, ~0u, ~SSL_eNULL, SSL_SHA1, ~0u, ~0u}, |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 550 | {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u}, |
| 551 | {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 553 | /* protocol version aliases */ |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 554 | {SSL_TXT_SSLV3, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_SSLV3, ~0u}, |
| 555 | {SSL_TXT_TLSV1, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_TLSV1, ~0u}, |
| 556 | {SSL_TXT_TLSV1_2, ~0u, ~0u, ~SSL_eNULL, ~0u, SSL_TLSV1_2, ~0u}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 558 | /* strength classes */ |
| 559 | {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM}, |
| 560 | {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH}, |
| 561 | /* FIPS 140-2 approved ciphersuite */ |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 562 | {SSL_TXT_FIPS, ~0u, ~0u, ~SSL_eNULL, ~0u, ~0u, SSL_FIPS}, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 563 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 564 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 565 | static const size_t kCipherAliasesLen = |
| 566 | sizeof(kCipherAliases) / sizeof(kCipherAliases[0]); |
| 567 | |
| 568 | static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) { |
| 569 | const SSL_CIPHER *a = in_a; |
| 570 | const SSL_CIPHER *b = in_b; |
| 571 | |
| 572 | if (a->id > b->id) { |
| 573 | return 1; |
| 574 | } else if (a->id < b->id) { |
| 575 | return -1; |
| 576 | } else { |
| 577 | return 0; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) { |
| 582 | return ssl_cipher_id_cmp(*a, *b); |
| 583 | } |
| 584 | |
| 585 | const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { |
| 586 | SSL_CIPHER c; |
| 587 | |
| 588 | c.id = 0x03000000L | value; |
| 589 | return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER), |
| 590 | ssl_cipher_id_cmp); |
| 591 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 592 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 593 | int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead, |
| 594 | size_t *out_mac_secret_len, |
| 595 | size_t *out_fixed_iv_len, |
| 596 | const SSL_CIPHER *cipher, uint16_t version) { |
| 597 | *out_aead = NULL; |
| 598 | *out_mac_secret_len = 0; |
| 599 | *out_fixed_iv_len = 0; |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 600 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 601 | switch (cipher->algorithm_enc) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 602 | case SSL_AES128GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 603 | *out_aead = EVP_aead_aes_128_gcm(); |
| 604 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 605 | return 1; |
| 606 | |
| 607 | case SSL_AES256GCM: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 608 | *out_aead = EVP_aead_aes_256_gcm(); |
| 609 | *out_fixed_iv_len = 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 610 | return 1; |
| 611 | |
| 612 | case SSL_CHACHA20POLY1305: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 613 | *out_aead = EVP_aead_chacha20_poly1305(); |
| 614 | *out_fixed_iv_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | return 1; |
| 616 | |
| 617 | case SSL_RC4: |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 618 | switch (cipher->algorithm_mac) { |
| 619 | case SSL_MD5: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 620 | if (version == SSL3_VERSION) { |
| 621 | *out_aead = EVP_aead_rc4_md5_ssl3(); |
| 622 | } else { |
| 623 | *out_aead = EVP_aead_rc4_md5_tls(); |
| 624 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 625 | *out_mac_secret_len = MD5_DIGEST_LENGTH; |
| 626 | return 1; |
| 627 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 628 | if (version == SSL3_VERSION) { |
| 629 | *out_aead = EVP_aead_rc4_sha1_ssl3(); |
| 630 | } else { |
| 631 | *out_aead = EVP_aead_rc4_sha1_tls(); |
| 632 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 633 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 634 | return 1; |
| 635 | default: |
| 636 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 638 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 639 | case SSL_AES128: |
| 640 | switch (cipher->algorithm_mac) { |
| 641 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 642 | if (version == SSL3_VERSION) { |
| 643 | *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3(); |
| 644 | *out_fixed_iv_len = 16; |
| 645 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 646 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(); |
| 647 | *out_fixed_iv_len = 16; |
| 648 | } else { |
| 649 | *out_aead = EVP_aead_aes_128_cbc_sha1_tls(); |
| 650 | } |
| 651 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 652 | return 1; |
| 653 | case SSL_SHA256: |
| 654 | *out_aead = EVP_aead_aes_128_cbc_sha256_tls(); |
| 655 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 656 | return 1; |
| 657 | default: |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | case SSL_AES256: |
| 662 | switch (cipher->algorithm_mac) { |
| 663 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 664 | if (version == SSL3_VERSION) { |
| 665 | *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3(); |
| 666 | *out_fixed_iv_len = 16; |
| 667 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 668 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(); |
| 669 | *out_fixed_iv_len = 16; |
| 670 | } else { |
| 671 | *out_aead = EVP_aead_aes_256_cbc_sha1_tls(); |
| 672 | } |
| 673 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 674 | return 1; |
| 675 | case SSL_SHA256: |
| 676 | *out_aead = EVP_aead_aes_256_cbc_sha256_tls(); |
| 677 | *out_mac_secret_len = SHA256_DIGEST_LENGTH; |
| 678 | return 1; |
| 679 | case SSL_SHA384: |
| 680 | *out_aead = EVP_aead_aes_256_cbc_sha384_tls(); |
| 681 | *out_mac_secret_len = SHA384_DIGEST_LENGTH; |
| 682 | return 1; |
| 683 | default: |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | case SSL_3DES: |
| 688 | switch (cipher->algorithm_mac) { |
| 689 | case SSL_SHA1: |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 690 | if (version == SSL3_VERSION) { |
| 691 | *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3(); |
| 692 | *out_fixed_iv_len = 8; |
| 693 | } else if (version == TLS1_VERSION) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 694 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(); |
| 695 | *out_fixed_iv_len = 8; |
| 696 | } else { |
| 697 | *out_aead = EVP_aead_des_ede3_cbc_sha1_tls(); |
| 698 | } |
| 699 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 700 | return 1; |
| 701 | default: |
| 702 | return 0; |
| 703 | } |
| 704 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 705 | case SSL_eNULL: |
| 706 | switch (cipher->algorithm_mac) { |
| 707 | case SSL_SHA1: |
| 708 | if (version == SSL3_VERSION) { |
| 709 | *out_aead = EVP_aead_null_sha1_ssl3(); |
| 710 | } else { |
| 711 | *out_aead = EVP_aead_null_sha1_tls(); |
| 712 | } |
| 713 | *out_mac_secret_len = SHA_DIGEST_LENGTH; |
| 714 | return 1; |
| 715 | default: |
| 716 | return 0; |
| 717 | } |
| 718 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 719 | default: |
| 720 | return 0; |
| 721 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 722 | } |
Adam Langley | c9fb375 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 724 | const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) { |
| 725 | switch (algorithm_prf) { |
| 726 | case SSL_HANDSHAKE_MAC_DEFAULT: |
| 727 | return EVP_sha1(); |
| 728 | case SSL_HANDSHAKE_MAC_SHA256: |
| 729 | return EVP_sha256(); |
| 730 | case SSL_HANDSHAKE_MAC_SHA384: |
| 731 | return EVP_sha384(); |
| 732 | default: |
| 733 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 734 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | #define ITEM_SEP(a) \ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 738 | (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ',')) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 740 | /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the |
| 741 | * |buf_len| bytes at |buf|. */ |
| 742 | static int rule_equals(const char *rule, const char *buf, size_t buf_len) { |
| 743 | /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */ |
| 744 | return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0'; |
| 745 | } |
| 746 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 747 | static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 748 | CIPHER_ORDER **tail) { |
| 749 | if (curr == *tail) { |
| 750 | return; |
| 751 | } |
| 752 | if (curr == *head) { |
| 753 | *head = curr->next; |
| 754 | } |
| 755 | if (curr->prev != NULL) { |
| 756 | curr->prev->next = curr->next; |
| 757 | } |
| 758 | if (curr->next != NULL) { |
| 759 | curr->next->prev = curr->prev; |
| 760 | } |
| 761 | (*tail)->next = curr; |
| 762 | curr->prev = *tail; |
| 763 | curr->next = NULL; |
| 764 | *tail = curr; |
| 765 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | |
| 767 | static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 768 | CIPHER_ORDER **tail) { |
| 769 | if (curr == *head) { |
| 770 | return; |
| 771 | } |
| 772 | if (curr == *tail) { |
| 773 | *tail = curr->prev; |
| 774 | } |
| 775 | if (curr->next != NULL) { |
| 776 | curr->next->prev = curr->prev; |
| 777 | } |
| 778 | if (curr->prev != NULL) { |
| 779 | curr->prev->next = curr->next; |
| 780 | } |
| 781 | (*head)->prev = curr; |
| 782 | curr->next = *head; |
| 783 | curr->prev = NULL; |
| 784 | *head = curr; |
| 785 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 786 | |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 787 | static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 788 | CIPHER_ORDER *co_list, |
| 789 | CIPHER_ORDER **head_p, |
| 790 | CIPHER_ORDER **tail_p) { |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 791 | /* The set of ciphers is static, but some subset may be unsupported by |
| 792 | * |ssl_method|, so the list may be smaller. */ |
| 793 | size_t co_list_num = 0; |
| 794 | size_t i; |
| 795 | for (i = 0; i < kCiphersLen; i++) { |
| 796 | const SSL_CIPHER *cipher = &kCiphers[i]; |
| 797 | if (ssl_method->supports_cipher(cipher)) { |
| 798 | co_list[co_list_num].cipher = cipher; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 799 | co_list[co_list_num].next = NULL; |
| 800 | co_list[co_list_num].prev = NULL; |
| 801 | co_list[co_list_num].active = 0; |
| 802 | co_list[co_list_num].in_group = 0; |
| 803 | co_list_num++; |
| 804 | } |
| 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 807 | /* Prepare linked list from list entries. */ |
| 808 | if (co_list_num > 0) { |
| 809 | co_list[0].prev = NULL; |
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 | if (co_list_num > 1) { |
| 812 | co_list[0].next = &co_list[1]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 813 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 814 | for (i = 1; i < co_list_num - 1; i++) { |
| 815 | co_list[i].prev = &co_list[i - 1]; |
| 816 | co_list[i].next = &co_list[i + 1]; |
| 817 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 818 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 819 | co_list[co_list_num - 1].prev = &co_list[co_list_num - 2]; |
| 820 | } |
| 821 | |
| 822 | co_list[co_list_num - 1].next = NULL; |
| 823 | |
| 824 | *head_p = &co_list[0]; |
| 825 | *tail_p = &co_list[co_list_num - 1]; |
| 826 | } |
| 827 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 829 | /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its |
| 830 | * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new |
| 831 | * head and tail of the list to |*head_p| and |*tail_p|, respectively. |
| 832 | * |
| 833 | * - If |cipher_id| is non-zero, only that cipher is selected. |
| 834 | * - Otherwise, if |strength_bits| is non-negative, it selects ciphers |
| 835 | * of that strength. |
| 836 | * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and |
| 837 | * |algo_strength|. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 838 | static void ssl_cipher_apply_rule( |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 839 | uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth, |
| 840 | uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl, |
| 841 | uint32_t algo_strength, int rule, int strength_bits, int in_group, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 842 | CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) { |
| 843 | CIPHER_ORDER *head, *tail, *curr, *next, *last; |
| 844 | const SSL_CIPHER *cp; |
| 845 | int reverse = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 846 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 847 | if (cipher_id == 0 && strength_bits == -1 && |
| 848 | (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 || |
| 849 | alg_ssl == 0 || algo_strength == 0)) { |
| 850 | /* The rule matches nothing, so bail early. */ |
| 851 | return; |
| 852 | } |
| 853 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 854 | if (rule == CIPHER_DEL) { |
| 855 | /* needed to maintain sorting between currently deleted ciphers */ |
| 856 | reverse = 1; |
| 857 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 858 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 859 | head = *head_p; |
| 860 | tail = *tail_p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | if (reverse) { |
| 863 | next = tail; |
| 864 | last = head; |
| 865 | } else { |
| 866 | next = head; |
| 867 | last = tail; |
| 868 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 869 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 870 | curr = NULL; |
| 871 | for (;;) { |
| 872 | if (curr == last) { |
| 873 | break; |
| 874 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 876 | curr = next; |
| 877 | if (curr == NULL) { |
| 878 | break; |
| 879 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 880 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 881 | next = reverse ? curr->prev : curr->next; |
| 882 | cp = curr->cipher; |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 883 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 884 | /* Selection criteria is either a specific cipher, the value of |
| 885 | * |strength_bits|, or the algorithms used. */ |
| 886 | if (cipher_id != 0) { |
| 887 | if (cipher_id != cp->id) { |
| 888 | continue; |
| 889 | } |
| 890 | } else if (strength_bits >= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 891 | if (strength_bits != cp->strength_bits) { |
| 892 | continue; |
| 893 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 894 | } else if (!(alg_mkey & cp->algorithm_mkey) || |
| 895 | !(alg_auth & cp->algorithm_auth) || |
| 896 | !(alg_enc & cp->algorithm_enc) || |
| 897 | !(alg_mac & cp->algorithm_mac) || |
| 898 | !(alg_ssl & cp->algorithm_ssl) || |
| 899 | !(algo_strength & cp->algo_strength)) { |
| 900 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 901 | } |
Adam Langley | e3142a7 | 2014-07-24 17:56:48 -0700 | [diff] [blame] | 902 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 903 | /* add the cipher if it has not been added yet. */ |
| 904 | if (rule == CIPHER_ADD) { |
| 905 | /* reverse == 0 */ |
| 906 | if (!curr->active) { |
| 907 | ll_append_tail(&head, curr, &tail); |
| 908 | curr->active = 1; |
| 909 | curr->in_group = in_group; |
| 910 | } |
| 911 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 913 | /* Move the added cipher to this location */ |
| 914 | else if (rule == CIPHER_ORD) { |
| 915 | /* reverse == 0 */ |
| 916 | if (curr->active) { |
| 917 | ll_append_tail(&head, curr, &tail); |
| 918 | curr->in_group = 0; |
| 919 | } |
| 920 | } else if (rule == CIPHER_DEL) { |
| 921 | /* reverse == 1 */ |
| 922 | if (curr->active) { |
| 923 | /* most recently deleted ciphersuites get best positions |
| 924 | * for any future CIPHER_ADD (note that the CIPHER_DEL loop |
| 925 | * works in reverse to maintain the order) */ |
| 926 | ll_append_head(&head, curr, &tail); |
| 927 | curr->active = 0; |
| 928 | curr->in_group = 0; |
| 929 | } |
| 930 | } else if (rule == CIPHER_KILL) { |
| 931 | /* reverse == 0 */ |
| 932 | if (head == curr) { |
| 933 | head = curr->next; |
| 934 | } else { |
| 935 | curr->prev->next = curr->next; |
| 936 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 937 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 938 | if (tail == curr) { |
| 939 | tail = curr->prev; |
| 940 | } |
| 941 | curr->active = 0; |
| 942 | if (curr->next != NULL) { |
| 943 | curr->next->prev = curr->prev; |
| 944 | } |
| 945 | if (curr->prev != NULL) { |
| 946 | curr->prev->next = curr->next; |
| 947 | } |
| 948 | curr->next = NULL; |
| 949 | curr->prev = NULL; |
| 950 | } |
| 951 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 952 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 953 | *head_p = head; |
| 954 | *tail_p = tail; |
| 955 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 956 | |
| 957 | static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 958 | CIPHER_ORDER **tail_p) { |
| 959 | int max_strength_bits, i, *number_uses; |
| 960 | CIPHER_ORDER *curr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 961 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 962 | /* This routine sorts the ciphers with descending strength. The sorting must |
| 963 | * keep the pre-sorted sequence, so we apply the normal sorting routine as |
| 964 | * '+' movement to the end of the list. */ |
| 965 | max_strength_bits = 0; |
| 966 | curr = *head_p; |
| 967 | while (curr != NULL) { |
| 968 | if (curr->active && curr->cipher->strength_bits > max_strength_bits) { |
| 969 | max_strength_bits = curr->cipher->strength_bits; |
| 970 | } |
| 971 | curr = curr->next; |
| 972 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 973 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int)); |
| 975 | if (!number_uses) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 976 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 977 | return 0; |
| 978 | } |
| 979 | memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 980 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 981 | /* Now find the strength_bits values actually used. */ |
| 982 | curr = *head_p; |
| 983 | while (curr != NULL) { |
| 984 | if (curr->active) { |
| 985 | number_uses[curr->cipher->strength_bits]++; |
| 986 | } |
| 987 | curr = curr->next; |
| 988 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 989 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 990 | /* Go through the list of used strength_bits values in descending order. */ |
| 991 | for (i = max_strength_bits; i >= 0; i--) { |
| 992 | if (number_uses[i] > 0) { |
| 993 | ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p, |
| 994 | tail_p); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | OPENSSL_free(number_uses); |
| 999 | return 1; |
| 1000 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1001 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1002 | static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method, |
| 1003 | const char *rule_str, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1004 | CIPHER_ORDER **head_p, |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1005 | CIPHER_ORDER **tail_p) { |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1006 | 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] | 1007 | const char *l, *buf; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1008 | int multi, rule, retval, ok, in_group = 0, has_group = 0; |
| 1009 | size_t j, buf_len; |
| 1010 | uint32_t cipher_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1011 | char ch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1012 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1013 | retval = 1; |
| 1014 | l = rule_str; |
| 1015 | for (;;) { |
| 1016 | ch = *l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1017 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1018 | if (ch == '\0') { |
| 1019 | break; /* done */ |
| 1020 | } |
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 (in_group) { |
| 1023 | if (ch == ']') { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1024 | if (*tail_p) { |
| 1025 | (*tail_p)->in_group = 0; |
| 1026 | } |
| 1027 | in_group = 0; |
| 1028 | l++; |
| 1029 | continue; |
| 1030 | } |
David Benjamin | 37d9246 | 2014-09-20 17:54:24 -0400 | [diff] [blame] | 1031 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1032 | if (ch == '|') { |
| 1033 | rule = CIPHER_ADD; |
| 1034 | l++; |
| 1035 | continue; |
| 1036 | } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && |
| 1037 | !(ch >= '0' && ch <= '9')) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1038 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1039 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1040 | break; |
| 1041 | } else { |
| 1042 | rule = CIPHER_ADD; |
| 1043 | } |
| 1044 | } else if (ch == '-') { |
| 1045 | rule = CIPHER_DEL; |
| 1046 | l++; |
| 1047 | } else if (ch == '+') { |
| 1048 | rule = CIPHER_ORD; |
| 1049 | l++; |
| 1050 | } else if (ch == '!') { |
| 1051 | rule = CIPHER_KILL; |
| 1052 | l++; |
| 1053 | } else if (ch == '@') { |
| 1054 | rule = CIPHER_SPECIAL; |
| 1055 | l++; |
| 1056 | } else if (ch == '[') { |
| 1057 | if (in_group) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1058 | OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1059 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1060 | break; |
| 1061 | } |
| 1062 | in_group = 1; |
| 1063 | has_group = 1; |
| 1064 | l++; |
| 1065 | continue; |
| 1066 | } else { |
| 1067 | rule = CIPHER_ADD; |
| 1068 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1069 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1070 | /* If preference groups are enabled, the only legal operator is +. |
| 1071 | * Otherwise the in_group bits will get mixed up. */ |
| 1072 | if (has_group && rule != CIPHER_ADD) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1073 | OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1074 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1075 | break; |
| 1076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1078 | if (ITEM_SEP(ch)) { |
| 1079 | l++; |
| 1080 | continue; |
| 1081 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1082 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1083 | multi = 0; |
| 1084 | cipher_id = 0; |
| 1085 | alg_mkey = ~0u; |
| 1086 | alg_auth = ~0u; |
| 1087 | alg_enc = ~0u; |
| 1088 | alg_mac = ~0u; |
| 1089 | alg_ssl = ~0u; |
| 1090 | algo_strength = ~0u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1091 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1092 | for (;;) { |
| 1093 | ch = *l; |
| 1094 | buf = l; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1095 | buf_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1096 | while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || |
| 1097 | ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) { |
| 1098 | ch = *(++l); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1099 | buf_len++; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1100 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1101 | |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1102 | if (buf_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1103 | /* We hit something we cannot deal with, it is no command or separator |
| 1104 | * nor alphanumeric, so we call this an error. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1105 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1106 | retval = in_group = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1107 | l++; |
| 1108 | break; |
| 1109 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1110 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1111 | if (rule == CIPHER_SPECIAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1112 | break; |
| 1113 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1114 | |
| 1115 | /* Look for a matching exact cipher. These aren't allowed in multipart |
| 1116 | * rules. */ |
| 1117 | if (!multi && ch != '+') { |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1118 | for (j = 0; j < kCiphersLen; j++) { |
| 1119 | const SSL_CIPHER *cipher = &kCiphers[j]; |
| 1120 | if (rule_equals(cipher->name, buf, buf_len)) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1121 | cipher_id = cipher->id; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | if (cipher_id == 0) { |
| 1127 | /* If not an exact cipher, look for a matching cipher alias. */ |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1128 | for (j = 0; j < kCipherAliasesLen; j++) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1129 | if (rule_equals(kCipherAliases[j].name, buf, buf_len)) { |
| 1130 | alg_mkey &= kCipherAliases[j].algorithm_mkey; |
| 1131 | alg_auth &= kCipherAliases[j].algorithm_auth; |
| 1132 | alg_enc &= kCipherAliases[j].algorithm_enc; |
| 1133 | alg_mac &= kCipherAliases[j].algorithm_mac; |
| 1134 | alg_ssl &= kCipherAliases[j].algorithm_ssl; |
| 1135 | algo_strength &= kCipherAliases[j].algo_strength; |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1139 | if (j == kCipherAliasesLen) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1140 | alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /* Check for a multipart rule. */ |
| 1145 | if (ch != '+') { |
| 1146 | break; |
| 1147 | } |
| 1148 | l++; |
| 1149 | multi = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1150 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1151 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1152 | /* Ok, we have the rule, now apply it. */ |
| 1153 | if (rule == CIPHER_SPECIAL) { |
| 1154 | /* special command */ |
| 1155 | ok = 0; |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1156 | if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1157 | ok = ssl_cipher_strength_sort(head_p, tail_p); |
| 1158 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1159 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1161 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1162 | if (ok == 0) { |
| 1163 | retval = 0; |
| 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 | /* We do not support any "multi" options together with "@", so throw away |
| 1167 | * the rest of the command, if any left, until end or ':' is found. */ |
| 1168 | while (*l != '\0' && !ITEM_SEP(*l)) { |
| 1169 | l++; |
| 1170 | } |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1171 | } else { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1172 | ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac, |
| 1173 | alg_ssl, algo_strength, rule, -1, in_group, head_p, |
| 1174 | tail_p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1175 | } |
| 1176 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1177 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1178 | if (in_group) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1179 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1180 | retval = 0; |
| 1181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1182 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1183 | return retval; |
| 1184 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1185 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1186 | STACK_OF(SSL_CIPHER) * |
| 1187 | ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method, |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1188 | struct ssl_cipher_preference_list_st **out_cipher_list, |
| 1189 | STACK_OF(SSL_CIPHER) **out_cipher_list_by_id, |
| 1190 | const char *rule_str) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1191 | int ok; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1192 | STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL; |
| 1193 | const char *rule_p; |
| 1194 | CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1195 | uint8_t *in_group_flags = NULL; |
| 1196 | unsigned int num_in_group_flags = 0; |
| 1197 | struct ssl_cipher_preference_list_st *pref_list = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1199 | /* Return with error if nothing to do. */ |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1200 | if (rule_str == NULL || out_cipher_list == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1201 | return NULL; |
| 1202 | } |
David Benjamin | 5213df4 | 2014-08-20 14:19:54 -0400 | [diff] [blame] | 1203 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1204 | /* Now we have to collect the available ciphers from the compiled in ciphers. |
| 1205 | * We cannot get more than the number compiled in, so it is used for |
| 1206 | * allocation. */ |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1207 | co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1208 | if (co_list == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1209 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1210 | return NULL; |
| 1211 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1212 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1213 | ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1214 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1215 | /* Now arrange all ciphers by preference: |
| 1216 | * TODO(davidben): Compute this order once and copy it. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1217 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1218 | /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other |
| 1219 | * key exchange mechanisms */ |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1220 | ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u, |
| 1221 | CIPHER_ADD, -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1222 | 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] | 1223 | 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1224 | ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, |
| 1225 | 0, &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1226 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1227 | /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer |
| 1228 | * CHACHA20 unless there is hardware support for fast and constant-time |
| 1229 | * AES_GCM. */ |
| 1230 | if (EVP_has_aes_hardware()) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1231 | 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] | 1232 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1233 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 1234 | -1, 0, &head, &tail); |
| 1235 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 1236 | CIPHER_ADD, -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1237 | } else { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1238 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u, |
| 1239 | CIPHER_ADD, -1, 0, &head, &tail); |
| 1240 | 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] | 1241 | -1, 0, &head, &tail); |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1242 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD, |
| 1243 | -1, 0, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1245 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1246 | /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA, |
| 1247 | * RC4_128_MD5, 3DES_EDE_CBC_SHA. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1248 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 1249 | 0, &head, &tail); |
| 1250 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1, |
| 1251 | 0, &head, &tail); |
| 1252 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD, |
| 1253 | -1, 0, &head, &tail); |
| 1254 | ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1, |
| 1255 | 0, &head, &tail); |
| 1256 | 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] | 1257 | &head, &tail); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1258 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1259 | /* Temporarily enable everything else for sorting */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1260 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0, |
| 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 | /* Move ciphers without forward secrecy to the end. */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1264 | ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u, |
| 1265 | CIPHER_ORD, -1, 0, &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 | /* Now disable everything (maintaining the ordering!) */ |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1268 | ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0, |
| 1269 | &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 | /* If the rule_string begins with DEFAULT, apply the default rule before |
| 1272 | * using the (possibly available) additional rules. */ |
| 1273 | ok = 1; |
| 1274 | rule_p = rule_str; |
| 1275 | if (strncmp(rule_str, "DEFAULT", 7) == 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1276 | ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head, |
| 1277 | &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1278 | rule_p += 7; |
| 1279 | if (*rule_p == ':') { |
| 1280 | rule_p++; |
| 1281 | } |
| 1282 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1284 | if (ok && strlen(rule_p) > 0) { |
David Benjamin | 0344daf | 2015-04-08 02:08:01 -0400 | [diff] [blame] | 1285 | ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1286 | } |
Adam Langley | 95c29f3 | 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) { |
| 1289 | goto err; |
| 1290 | } |
| 1291 | |
| 1292 | /* Allocate new "cipherstack" for the result, return with error |
| 1293 | * if we cannot get one. */ |
| 1294 | cipherstack = sk_SSL_CIPHER_new_null(); |
| 1295 | if (cipherstack == NULL) { |
| 1296 | goto err; |
| 1297 | } |
| 1298 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1299 | in_group_flags = OPENSSL_malloc(kCiphersLen); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1300 | if (!in_group_flags) { |
| 1301 | goto err; |
| 1302 | } |
| 1303 | |
| 1304 | /* The cipher selection for the list is done. The ciphers are added |
| 1305 | * to the resulting precedence to the STACK_OF(SSL_CIPHER). */ |
| 1306 | for (curr = head; curr != NULL; curr = curr->next) { |
| 1307 | if (curr->active) { |
David Benjamin | 2adb7ec | 2015-01-11 19:59:06 -0500 | [diff] [blame] | 1308 | if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) { |
| 1309 | goto err; |
| 1310 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1311 | in_group_flags[num_in_group_flags++] = curr->in_group; |
| 1312 | } |
| 1313 | } |
| 1314 | OPENSSL_free(co_list); /* Not needed any longer */ |
| 1315 | co_list = NULL; |
| 1316 | |
| 1317 | tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack); |
| 1318 | if (tmp_cipher_list == NULL) { |
| 1319 | goto err; |
| 1320 | } |
| 1321 | pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 1322 | if (!pref_list) { |
| 1323 | goto err; |
| 1324 | } |
| 1325 | pref_list->ciphers = cipherstack; |
| 1326 | pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags); |
| 1327 | if (!pref_list->in_group_flags) { |
| 1328 | goto err; |
| 1329 | } |
| 1330 | memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags); |
| 1331 | OPENSSL_free(in_group_flags); |
| 1332 | in_group_flags = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1333 | if (*out_cipher_list != NULL) { |
| 1334 | ssl_cipher_preference_list_free(*out_cipher_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1335 | } |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1336 | *out_cipher_list = pref_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1337 | pref_list = NULL; |
| 1338 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1339 | if (out_cipher_list_by_id != NULL) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1340 | sk_SSL_CIPHER_free(*out_cipher_list_by_id); |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1341 | *out_cipher_list_by_id = tmp_cipher_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1342 | tmp_cipher_list = NULL; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1343 | (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id, |
| 1344 | ssl_cipher_ptr_id_cmp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1345 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1346 | sk_SSL_CIPHER_sort(*out_cipher_list_by_id); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1347 | } else { |
| 1348 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1349 | tmp_cipher_list = NULL; |
| 1350 | } |
| 1351 | |
| 1352 | return cipherstack; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1353 | |
| 1354 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1355 | OPENSSL_free(co_list); |
| 1356 | OPENSSL_free(in_group_flags); |
| 1357 | sk_SSL_CIPHER_free(cipherstack); |
| 1358 | sk_SSL_CIPHER_free(tmp_cipher_list); |
| 1359 | if (pref_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1360 | OPENSSL_free(pref_list->in_group_flags); |
| 1361 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1362 | OPENSSL_free(pref_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1365 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1366 | uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; } |
| 1367 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1368 | uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) { |
| 1369 | uint32_t id = cipher->id; |
| 1370 | /* All ciphers are SSLv3. */ |
| 1371 | assert((id & 0xff000000) == 0x03000000); |
| 1372 | return id & 0xffff; |
| 1373 | } |
| 1374 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1375 | int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) { |
| 1376 | return (cipher->algorithm_enc & SSL_AES) != 0; |
| 1377 | } |
| 1378 | |
| 1379 | int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) { |
| 1380 | return (cipher->algorithm_mac & SSL_MD5) != 0; |
| 1381 | } |
| 1382 | |
| 1383 | int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) { |
David Benjamin | c0125ef | 2015-09-09 09:11:07 -0400 | [diff] [blame] | 1384 | return (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) != 0; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) { |
| 1388 | return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0; |
| 1389 | } |
| 1390 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 1391 | int SSL_CIPHER_is_NULL(const SSL_CIPHER *cipher) { |
| 1392 | return (cipher->algorithm_enc & SSL_eNULL) != 0; |
| 1393 | } |
| 1394 | |
| 1395 | int SSL_CIPHER_is_block_cipher(const SSL_CIPHER *cipher) { |
| 1396 | /* Neither stream cipher nor AEAD. */ |
| 1397 | return (cipher->algorithm_enc & (SSL_RC4 | SSL_eNULL)) == 0 && |
| 1398 | cipher->algorithm_mac != SSL_AEAD; |
| 1399 | } |
| 1400 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1401 | /* return the actual cipher being used */ |
| 1402 | const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) { |
| 1403 | if (cipher != NULL) { |
| 1404 | return cipher->name; |
| 1405 | } |
| 1406 | |
| 1407 | return "(NONE)"; |
| 1408 | } |
| 1409 | |
| 1410 | const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) { |
| 1411 | if (cipher == NULL) { |
| 1412 | return ""; |
| 1413 | } |
| 1414 | |
| 1415 | switch (cipher->algorithm_mkey) { |
| 1416 | case SSL_kRSA: |
| 1417 | return "RSA"; |
| 1418 | |
| 1419 | case SSL_kDHE: |
| 1420 | switch (cipher->algorithm_auth) { |
| 1421 | case SSL_aRSA: |
| 1422 | return "DHE_RSA"; |
| 1423 | default: |
| 1424 | assert(0); |
| 1425 | return "UNKNOWN"; |
| 1426 | } |
| 1427 | |
| 1428 | case SSL_kECDHE: |
| 1429 | switch (cipher->algorithm_auth) { |
| 1430 | case SSL_aECDSA: |
| 1431 | return "ECDHE_ECDSA"; |
| 1432 | case SSL_aRSA: |
| 1433 | return "ECDHE_RSA"; |
| 1434 | case SSL_aPSK: |
| 1435 | return "ECDHE_PSK"; |
| 1436 | default: |
| 1437 | assert(0); |
| 1438 | return "UNKNOWN"; |
| 1439 | } |
| 1440 | |
| 1441 | case SSL_kPSK: |
| 1442 | assert(cipher->algorithm_auth == SSL_aPSK); |
| 1443 | return "PSK"; |
| 1444 | |
| 1445 | default: |
| 1446 | assert(0); |
| 1447 | return "UNKNOWN"; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) { |
| 1452 | switch (cipher->algorithm_enc) { |
| 1453 | case SSL_3DES: |
| 1454 | return "3DES_EDE_CBC"; |
| 1455 | case SSL_RC4: |
| 1456 | return "RC4"; |
| 1457 | case SSL_AES128: |
| 1458 | return "AES_128_CBC"; |
| 1459 | case SSL_AES256: |
| 1460 | return "AES_256_CBC"; |
| 1461 | case SSL_AES128GCM: |
| 1462 | return "AES_128_GCM"; |
| 1463 | case SSL_AES256GCM: |
| 1464 | return "AES_256_GCM"; |
| 1465 | case SSL_CHACHA20POLY1305: |
| 1466 | return "CHACHA20_POLY1305"; |
| 1467 | break; |
| 1468 | default: |
| 1469 | assert(0); |
| 1470 | return "UNKNOWN"; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 1475 | switch (cipher->algorithm_prf) { |
| 1476 | case SSL_HANDSHAKE_MAC_DEFAULT: |
| 1477 | /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is |
| 1478 | * only ever MD5 or SHA-1. */ |
| 1479 | switch (cipher->algorithm_mac) { |
| 1480 | case SSL_MD5: |
| 1481 | return "MD5"; |
| 1482 | case SSL_SHA1: |
| 1483 | return "SHA"; |
| 1484 | } |
| 1485 | break; |
| 1486 | case SSL_HANDSHAKE_MAC_SHA256: |
| 1487 | return "SHA256"; |
| 1488 | case SSL_HANDSHAKE_MAC_SHA384: |
| 1489 | return "SHA384"; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1490 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 1491 | assert(0); |
| 1492 | return "UNKNOWN"; |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) { |
| 1496 | if (cipher == NULL) { |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | const char *kx_name = SSL_CIPHER_get_kx_name(cipher); |
| 1501 | const char *enc_name = ssl_cipher_get_enc_name(cipher); |
| 1502 | const char *prf_name = ssl_cipher_get_prf_name(cipher); |
| 1503 | |
| 1504 | /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */ |
| 1505 | size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 + |
| 1506 | strlen(prf_name) + 1; |
| 1507 | char *ret = OPENSSL_malloc(len); |
| 1508 | if (ret == NULL) { |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | if (BUF_strlcpy(ret, "TLS_", len) >= len || |
| 1512 | BUF_strlcat(ret, kx_name, len) >= len || |
| 1513 | BUF_strlcat(ret, "_WITH_", len) >= len || |
| 1514 | BUF_strlcat(ret, enc_name, len) >= len || |
| 1515 | BUF_strlcat(ret, "_", len) >= len || |
| 1516 | BUF_strlcat(ret, prf_name, len) >= len) { |
| 1517 | assert(0); |
| 1518 | OPENSSL_free(ret); |
| 1519 | return NULL; |
| 1520 | } |
| 1521 | assert(strlen(ret) + 1 == len); |
| 1522 | return ret; |
| 1523 | } |
| 1524 | |
| 1525 | int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) { |
| 1526 | if (cipher == NULL) { |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | if (out_alg_bits != NULL) { |
| 1531 | *out_alg_bits = cipher->alg_bits; |
| 1532 | } |
| 1533 | return cipher->strength_bits; |
| 1534 | } |
| 1535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1536 | const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, |
| 1537 | int len) { |
| 1538 | const char *ver; |
| 1539 | const char *kx, *au, *enc, *mac; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1540 | uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1541 | 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] | 1542 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1543 | alg_mkey = cipher->algorithm_mkey; |
| 1544 | alg_auth = cipher->algorithm_auth; |
| 1545 | alg_enc = cipher->algorithm_enc; |
| 1546 | alg_mac = cipher->algorithm_mac; |
| 1547 | alg_ssl = cipher->algorithm_ssl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1548 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1549 | if (alg_ssl & SSL_SSLV3) { |
| 1550 | ver = "SSLv3"; |
| 1551 | } else if (alg_ssl & SSL_TLSV1_2) { |
| 1552 | ver = "TLSv1.2"; |
| 1553 | } else { |
| 1554 | ver = "unknown"; |
| 1555 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1556 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1557 | switch (alg_mkey) { |
| 1558 | case SSL_kRSA: |
| 1559 | kx = "RSA"; |
| 1560 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1561 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1562 | case SSL_kDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1563 | kx = "DH"; |
| 1564 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1565 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1566 | case SSL_kECDHE: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1567 | kx = "ECDH"; |
| 1568 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1570 | case SSL_kPSK: |
| 1571 | kx = "PSK"; |
| 1572 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1574 | default: |
| 1575 | kx = "unknown"; |
| 1576 | } |
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 | switch (alg_auth) { |
| 1579 | case SSL_aRSA: |
| 1580 | au = "RSA"; |
| 1581 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1582 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1583 | case SSL_aECDSA: |
| 1584 | au = "ECDSA"; |
| 1585 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1586 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1587 | case SSL_aPSK: |
| 1588 | au = "PSK"; |
| 1589 | break; |
Adam Langley | 4d4bff8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1590 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1591 | default: |
| 1592 | au = "unknown"; |
| 1593 | break; |
| 1594 | } |
Adam Langley | de0b202 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1595 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1596 | switch (alg_enc) { |
| 1597 | case SSL_3DES: |
| 1598 | enc = "3DES(168)"; |
| 1599 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1600 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1601 | case SSL_RC4: |
| 1602 | enc = "RC4(128)"; |
| 1603 | break; |
| 1604 | |
| 1605 | case SSL_AES128: |
| 1606 | enc = "AES(128)"; |
| 1607 | break; |
| 1608 | |
| 1609 | case SSL_AES256: |
| 1610 | enc = "AES(256)"; |
| 1611 | break; |
| 1612 | |
| 1613 | case SSL_AES128GCM: |
| 1614 | enc = "AESGCM(128)"; |
| 1615 | break; |
| 1616 | |
| 1617 | case SSL_AES256GCM: |
| 1618 | enc = "AESGCM(256)"; |
| 1619 | break; |
| 1620 | |
| 1621 | case SSL_CHACHA20POLY1305: |
| 1622 | enc = "ChaCha20-Poly1305"; |
| 1623 | break; |
| 1624 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 1625 | case SSL_eNULL: |
| 1626 | enc="None"; |
| 1627 | break; |
| 1628 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1629 | default: |
| 1630 | enc = "unknown"; |
| 1631 | break; |
| 1632 | } |
| 1633 | |
| 1634 | switch (alg_mac) { |
| 1635 | case SSL_MD5: |
| 1636 | mac = "MD5"; |
| 1637 | break; |
| 1638 | |
| 1639 | case SSL_SHA1: |
| 1640 | mac = "SHA1"; |
| 1641 | break; |
| 1642 | |
| 1643 | case SSL_SHA256: |
| 1644 | mac = "SHA256"; |
| 1645 | break; |
| 1646 | |
| 1647 | case SSL_SHA384: |
| 1648 | mac = "SHA384"; |
| 1649 | break; |
| 1650 | |
| 1651 | case SSL_AEAD: |
| 1652 | mac = "AEAD"; |
| 1653 | break; |
| 1654 | |
| 1655 | default: |
| 1656 | mac = "unknown"; |
| 1657 | break; |
| 1658 | } |
| 1659 | |
| 1660 | if (buf == NULL) { |
| 1661 | len = 128; |
| 1662 | buf = OPENSSL_malloc(len); |
David Benjamin | 1eed2c0 | 2015-02-08 23:20:06 -0500 | [diff] [blame] | 1663 | if (buf == NULL) { |
| 1664 | return NULL; |
| 1665 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1666 | } else if (len < 128) { |
| 1667 | return "Buffer too small"; |
| 1668 | } |
| 1669 | |
| 1670 | BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac); |
| 1671 | return buf; |
| 1672 | } |
| 1673 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1674 | const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) { |
| 1675 | return "TLSv1/SSLv3"; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1678 | COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1679 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1680 | 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] | 1681 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 1682 | const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1683 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1684 | int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) { |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1685 | uint32_t alg_a = cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1686 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1687 | if (alg_a & SSL_aECDSA) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1688 | return EVP_PKEY_EC; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1689 | } else if (alg_a & SSL_aRSA) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1690 | return EVP_PKEY_RSA; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1691 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1692 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1693 | return EVP_PKEY_NONE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1694 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1695 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1696 | int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) { |
David Benjamin | 32fbdf2 | 2015-04-07 01:14:06 -0400 | [diff] [blame] | 1697 | /* PSK-authenticated ciphers do not use a public key, except for |
| 1698 | * RSA_PSK. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1699 | if ((cipher->algorithm_auth & SSL_aPSK) && |
| 1700 | !(cipher->algorithm_mkey & SSL_kRSA)) { |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | /* All other ciphers include it. */ |
| 1705 | return 1; |
| 1706 | } |
| 1707 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1708 | int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) { |
| 1709 | /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */ |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1710 | if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1711 | return 1; |
| 1712 | } |
| 1713 | |
| 1714 | /* It is optional in all others. */ |
| 1715 | return 0; |
| 1716 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1717 | |
| 1718 | size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) { |
| 1719 | size_t block_size; |
| 1720 | switch (cipher->algorithm_enc) { |
| 1721 | case SSL_3DES: |
| 1722 | block_size = 8; |
| 1723 | break; |
| 1724 | case SSL_AES128: |
| 1725 | case SSL_AES256: |
| 1726 | block_size = 16; |
| 1727 | break; |
| 1728 | default: |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | size_t mac_len; |
| 1733 | switch (cipher->algorithm_mac) { |
| 1734 | case SSL_MD5: |
| 1735 | mac_len = MD5_DIGEST_LENGTH; |
| 1736 | break; |
| 1737 | case SSL_SHA1: |
| 1738 | mac_len = SHA_DIGEST_LENGTH; |
| 1739 | break; |
| 1740 | default: |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | size_t ret = 1 + mac_len; |
| 1745 | ret += block_size - (ret % block_size); |
| 1746 | return ret; |
| 1747 | } |