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