ssl_cipher.c 50 KB

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