ssl_cipher.cc 49 KB

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