Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ssl_cipher.c 52 KiB

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