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