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