ssl_cipher.cc 48 KB

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