No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ssl_cipher.c 51 KiB

Factor out the buffering and low-level record code. This begins decoupling the transport from the SSL state machine. The buffering logic is hidden behind an opaque API. Fields like ssl->packet and ssl->packet_length are gone. ssl3_get_record and dtls1_get_record now call low-level tls_open_record and dtls_open_record functions that unpack a single record independent of who owns the buffer. Both may be called in-place. This removes ssl->rstate which was redundant with the buffer length. Future work will push the buffer up the stack until it is above the handshake. Then we can expose SSL_open and SSL_seal APIs which act like *_open_record but return a slightly larger enum due to other events being possible. Likewise the handshake state machine will be detached from its buffer. The existing SSL_read, SSL_write, etc., APIs will be implemented on top of SSL_open, etc., combined with ssl_read_buffer_* and ssl_write_buffer_*. (Which is why ssl_read_buffer_extend still tries to abstract between TLS's and DTLS's fairly different needs.) The new buffering logic does not support read-ahead (removed previously) since it lacks a memmove on ssl_read_buffer_discard for TLS, but this could be added if desired. The old buffering logic wasn't quite right anyway; it tried to avoid the memmove in some cases and could get stuck too far into the buffer and not accept records. (The only time the memmove is optional is in DTLS or if enough of the record header is available to know that the entire next record would fit in the buffer.) The new logic also now actually decrypts the ciphertext in-place again, rather than almost in-place when there's an explicit nonce/IV. (That accidentally switched in https://boringssl-review.googlesource.com/#/c/4792/; see 3d59e04bce96474099ba76786a2337e99ae14505.) BUG=468889 Change-Id: I403c1626253c46897f47c7ae93aeab1064b767b2 Reviewed-on: https://boringssl-review.googlesource.com/5715 Reviewed-by: Adam Langley <agl@google.com>
hace 9 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <assert.h>
  141. #include <stdio.h>
  142. #include <string.h>
  143. #include <openssl/buf.h>
  144. #include <openssl/err.h>
  145. #include <openssl/md5.h>
  146. #include <openssl/mem.h>
  147. #include <openssl/sha.h>
  148. #include <openssl/stack.h>
  149. #include "internal.h"
  150. /* kCiphers is an array of all supported ciphers, sorted by id. */
  151. const SSL_CIPHER kCiphers[] = {
  152. /* The RSA ciphers */
  153. /* Cipher 04 */
  154. {
  155. SSL3_TXT_RSA_RC4_128_MD5, SSL3_CK_RSA_RC4_128_MD5, SSL_kRSA, SSL_aRSA,
  156. SSL_RC4, SSL_MD5, SSL_SSLV3, SSL_MEDIUM,
  157. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  158. },
  159. /* Cipher 05 */
  160. {
  161. SSL3_TXT_RSA_RC4_128_SHA, SSL3_CK_RSA_RC4_128_SHA, SSL_kRSA, SSL_aRSA,
  162. SSL_RC4, SSL_SHA1, SSL_SSLV3, SSL_MEDIUM,
  163. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  164. },
  165. /* Cipher 0A */
  166. {
  167. SSL3_TXT_RSA_DES_192_CBC3_SHA, SSL3_CK_RSA_DES_192_CBC3_SHA, SSL_kRSA,
  168. SSL_aRSA, SSL_3DES, SSL_SHA1, SSL_SSLV3, SSL_HIGH | SSL_FIPS,
  169. SSL_HANDSHAKE_MAC_DEFAULT, 112, 168,
  170. },
  171. /* New AES ciphersuites */
  172. /* Cipher 2F */
  173. {
  174. TLS1_TXT_RSA_WITH_AES_128_SHA, TLS1_CK_RSA_WITH_AES_128_SHA, SSL_kRSA,
  175. SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  176. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  177. },
  178. /* Cipher 33 */
  179. {
  180. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA, TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
  181. SSL_kDHE, SSL_aRSA, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  182. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  183. },
  184. /* Cipher 35 */
  185. {
  186. TLS1_TXT_RSA_WITH_AES_256_SHA, TLS1_CK_RSA_WITH_AES_256_SHA, SSL_kRSA,
  187. SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  188. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  189. },
  190. /* Cipher 39 */
  191. {
  192. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA, TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
  193. SSL_kDHE, SSL_aRSA, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  194. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  195. },
  196. /* TLS v1.2 ciphersuites */
  197. /* Cipher 3C */
  198. {
  199. TLS1_TXT_RSA_WITH_AES_128_SHA256, TLS1_CK_RSA_WITH_AES_128_SHA256,
  200. SSL_kRSA, SSL_aRSA, SSL_AES128, SSL_SHA256, SSL_TLSV1_2,
  201. SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  202. },
  203. /* Cipher 3D */
  204. {
  205. TLS1_TXT_RSA_WITH_AES_256_SHA256, TLS1_CK_RSA_WITH_AES_256_SHA256,
  206. SSL_kRSA, SSL_aRSA, SSL_AES256, SSL_SHA256, SSL_TLSV1_2,
  207. SSL_HIGH | SSL_FIPS, SSL_HANDSHAKE_MAC_SHA256, 256, 256,
  208. },
  209. /* Cipher 67 */
  210. {
  211. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256,
  212. TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128,
  213. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  214. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  215. },
  216. /* Cipher 6B */
  217. {
  218. TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256,
  219. TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES256,
  220. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  221. SSL_HANDSHAKE_MAC_SHA256, 256, 256,
  222. },
  223. /* PSK cipher suites. */
  224. /* Cipher 8A */
  225. {
  226. TLS1_TXT_PSK_WITH_RC4_128_SHA, TLS1_CK_PSK_WITH_RC4_128_SHA, SSL_kPSK,
  227. SSL_aPSK, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
  228. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  229. },
  230. /* Cipher 8C */
  231. {
  232. TLS1_TXT_PSK_WITH_AES_128_CBC_SHA, TLS1_CK_PSK_WITH_AES_128_CBC_SHA,
  233. SSL_kPSK, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  234. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  235. },
  236. /* Cipher 8D */
  237. {
  238. TLS1_TXT_PSK_WITH_AES_256_CBC_SHA, TLS1_CK_PSK_WITH_AES_256_CBC_SHA,
  239. SSL_kPSK, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  240. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  241. },
  242. /* GCM ciphersuites from RFC5288 */
  243. /* Cipher 9C */
  244. {
  245. TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256,
  246. TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, SSL_kRSA, SSL_aRSA, SSL_AES128GCM,
  247. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  248. SSL_HANDSHAKE_MAC_SHA256,
  249. 128, 128,
  250. },
  251. /* Cipher 9D */
  252. {
  253. TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384,
  254. TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, SSL_kRSA, SSL_aRSA, SSL_AES256GCM,
  255. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  256. SSL_HANDSHAKE_MAC_SHA384,
  257. 256, 256,
  258. },
  259. /* Cipher 9E */
  260. {
  261. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256,
  262. TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kDHE, SSL_aRSA, SSL_AES128GCM,
  263. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  264. SSL_HANDSHAKE_MAC_SHA256,
  265. 128, 128,
  266. },
  267. /* Cipher 9F */
  268. {
  269. TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384,
  270. TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kDHE, SSL_aRSA, SSL_AES256GCM,
  271. SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  272. SSL_HANDSHAKE_MAC_SHA384,
  273. 256, 256,
  274. },
  275. /* Cipher C007 */
  276. {
  277. TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA,
  278. TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA, SSL_kECDHE, SSL_aECDSA, SSL_RC4,
  279. SSL_SHA1, SSL_TLSV1, SSL_MEDIUM, SSL_HANDSHAKE_MAC_DEFAULT, 128,
  280. 128,
  281. },
  282. /* Cipher C009 */
  283. {
  284. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  285. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
  286. SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  287. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  288. },
  289. /* Cipher C00A */
  290. {
  291. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  292. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aECDSA,
  293. SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  294. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  295. },
  296. /* Cipher C011 */
  297. {
  298. TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA, TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA,
  299. SSL_kECDHE, SSL_aRSA, SSL_RC4, SSL_SHA1, SSL_TLSV1, SSL_MEDIUM,
  300. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  301. },
  302. /* Cipher C013 */
  303. {
  304. TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  305. TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES128,
  306. SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  307. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  308. },
  309. /* Cipher C014 */
  310. {
  311. TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  312. TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA, SSL_kECDHE, SSL_aRSA, SSL_AES256,
  313. SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  314. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  315. },
  316. /* HMAC based TLS v1.2 ciphersuites from RFC5289 */
  317. /* Cipher C023 */
  318. {
  319. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256,
  320. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aECDSA,
  321. SSL_AES128, SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  322. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  323. },
  324. /* Cipher C024 */
  325. {
  326. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384,
  327. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aECDSA,
  328. SSL_AES256, SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  329. SSL_HANDSHAKE_MAC_SHA384, 256, 256,
  330. },
  331. /* Cipher C027 */
  332. {
  333. TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256,
  334. TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, SSL_kECDHE, SSL_aRSA, SSL_AES128,
  335. SSL_SHA256, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  336. SSL_HANDSHAKE_MAC_SHA256, 128, 128,
  337. },
  338. /* Cipher C028 */
  339. {
  340. TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384,
  341. TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, SSL_kECDHE, SSL_aRSA, SSL_AES256,
  342. SSL_SHA384, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  343. SSL_HANDSHAKE_MAC_SHA384, 256, 256,
  344. },
  345. /* GCM based TLS v1.2 ciphersuites from RFC5289 */
  346. /* Cipher C02B */
  347. {
  348. TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  349. TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aECDSA,
  350. SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  351. SSL_HANDSHAKE_MAC_SHA256,
  352. 128, 128,
  353. },
  354. /* Cipher C02C */
  355. {
  356. TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  357. TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aECDSA,
  358. SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  359. SSL_HANDSHAKE_MAC_SHA384,
  360. 256, 256,
  361. },
  362. /* Cipher C02F */
  363. {
  364. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  365. TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256, SSL_kECDHE, SSL_aRSA,
  366. SSL_AES128GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  367. SSL_HANDSHAKE_MAC_SHA256,
  368. 128, 128,
  369. },
  370. /* Cipher C030 */
  371. {
  372. TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  373. TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384, SSL_kECDHE, SSL_aRSA,
  374. SSL_AES256GCM, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH | SSL_FIPS,
  375. SSL_HANDSHAKE_MAC_SHA384,
  376. 256, 256,
  377. },
  378. /* ECDHE-PSK cipher suites. */
  379. /* Cipher C035 */
  380. {
  381. TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  382. TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA,
  383. SSL_kECDHE, SSL_aPSK, SSL_AES128, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  384. SSL_HANDSHAKE_MAC_DEFAULT, 128, 128,
  385. },
  386. /* Cipher C036 */
  387. {
  388. TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  389. TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA,
  390. SSL_kECDHE, SSL_aPSK, SSL_AES256, SSL_SHA1, SSL_TLSV1, SSL_HIGH | SSL_FIPS,
  391. SSL_HANDSHAKE_MAC_DEFAULT, 256, 256,
  392. },
  393. /* ChaCha20-Poly1305 cipher suites. */
  394. {
  395. TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  396. TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aRSA,
  397. SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  398. SSL_HANDSHAKE_MAC_SHA256,
  399. 256, 256,
  400. },
  401. {
  402. TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  403. TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305, SSL_kECDHE, SSL_aECDSA,
  404. SSL_CHACHA20POLY1305, SSL_AEAD, SSL_TLSV1_2, SSL_HIGH,
  405. SSL_HANDSHAKE_MAC_SHA256,
  406. 256, 256,
  407. },
  408. };
  409. static const size_t kCiphersLen = sizeof(kCiphers) / sizeof(kCiphers[0]);
  410. #define CIPHER_ADD 1
  411. #define CIPHER_KILL 2
  412. #define CIPHER_DEL 3
  413. #define CIPHER_ORD 4
  414. #define CIPHER_SPECIAL 5
  415. typedef struct cipher_order_st {
  416. const SSL_CIPHER *cipher;
  417. int active;
  418. int in_group;
  419. struct cipher_order_st *next, *prev;
  420. } CIPHER_ORDER;
  421. typedef struct cipher_alias_st {
  422. /* name is the name of the cipher alias. */
  423. const char *name;
  424. /* The following fields are bitmasks for the corresponding fields on
  425. * |SSL_CIPHER|. A cipher matches a cipher alias iff, for each bitmask, the
  426. * bit corresponding to the cipher's value is set to 1. If any bitmask is
  427. * all zeroes, the alias matches nothing. Use |~0u| for the default value. */
  428. uint32_t algorithm_mkey;
  429. uint32_t algorithm_auth;
  430. uint32_t algorithm_enc;
  431. uint32_t algorithm_mac;
  432. uint32_t algorithm_ssl;
  433. uint32_t algo_strength;
  434. } CIPHER_ALIAS;
  435. static const CIPHER_ALIAS kCipherAliases[] = {
  436. {SSL_TXT_ALL, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u},
  437. /* The "COMPLEMENTOFDEFAULT" rule is omitted. It matches nothing. */
  438. /* key exchange aliases
  439. * (some of those using only a single bit here combine
  440. * multiple key exchange algs according to the RFCs,
  441. * e.g. kEDH combines DHE_DSS and DHE_RSA) */
  442. {SSL_TXT_kRSA, SSL_kRSA, ~0u, ~0u, ~0u, ~0u, ~0u},
  443. {SSL_TXT_kDHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  444. {SSL_TXT_kEDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  445. {SSL_TXT_DH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  446. {SSL_TXT_kECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  447. {SSL_TXT_kEECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  448. {SSL_TXT_ECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  449. {SSL_TXT_kPSK, SSL_kPSK, ~0u, ~0u, ~0u, ~0u, ~0u},
  450. /* server authentication aliases */
  451. {SSL_TXT_aRSA, ~0u, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
  452. {SSL_TXT_aECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
  453. {SSL_TXT_ECDSA, ~0u, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u},
  454. {SSL_TXT_aPSK, ~0u, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
  455. /* aliases combining key exchange and server authentication */
  456. {SSL_TXT_DHE, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  457. {SSL_TXT_EDH, SSL_kDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  458. {SSL_TXT_ECDHE, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  459. {SSL_TXT_EECDH, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u},
  460. {SSL_TXT_RSA, SSL_kRSA, SSL_aRSA, ~0u, ~0u, ~0u, ~0u},
  461. {SSL_TXT_PSK, SSL_kPSK, SSL_aPSK, ~0u, ~0u, ~0u, ~0u},
  462. /* symmetric encryption aliases */
  463. {SSL_TXT_3DES, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u},
  464. {SSL_TXT_RC4, ~0u, ~0u, SSL_RC4, ~0u, ~0u, ~0u},
  465. {SSL_TXT_AES128, ~0u, ~0u, SSL_AES128 | SSL_AES128GCM, ~0u, ~0u, ~0u},
  466. {SSL_TXT_AES256, ~0u, ~0u, SSL_AES256 | SSL_AES256GCM, ~0u, ~0u, ~0u},
  467. {SSL_TXT_AES, ~0u, ~0u, SSL_AES, ~0u, ~0u, ~0u},
  468. {SSL_TXT_AES_GCM, ~0u, ~0u, SSL_AES128GCM | SSL_AES256GCM, ~0u, ~0u, ~0u},
  469. {SSL_TXT_CHACHA20, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u},
  470. /* MAC aliases */
  471. {SSL_TXT_MD5, ~0u, ~0u, ~0u, SSL_MD5, ~0u, ~0u},
  472. {SSL_TXT_SHA1, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
  473. {SSL_TXT_SHA, ~0u, ~0u, ~0u, SSL_SHA1, ~0u, ~0u},
  474. {SSL_TXT_SHA256, ~0u, ~0u, ~0u, SSL_SHA256, ~0u, ~0u},
  475. {SSL_TXT_SHA384, ~0u, ~0u, ~0u, SSL_SHA384, ~0u, ~0u},
  476. /* protocol version aliases */
  477. {SSL_TXT_SSLV3, ~0u, ~0u, ~0u, ~0u, SSL_SSLV3, ~0u},
  478. {SSL_TXT_TLSV1, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1, ~0u},
  479. {SSL_TXT_TLSV1_2, ~0u, ~0u, ~0u, ~0u, SSL_TLSV1_2, ~0u},
  480. /* strength classes */
  481. {SSL_TXT_MEDIUM, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_MEDIUM},
  482. {SSL_TXT_HIGH, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_HIGH},
  483. /* FIPS 140-2 approved ciphersuite */
  484. {SSL_TXT_FIPS, ~0u, ~0u, ~0u, ~0u, ~0u, SSL_FIPS},
  485. };
  486. static const size_t kCipherAliasesLen =
  487. sizeof(kCipherAliases) / sizeof(kCipherAliases[0]);
  488. static int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  489. const SSL_CIPHER *a = in_a;
  490. const SSL_CIPHER *b = in_b;
  491. if (a->id > b->id) {
  492. return 1;
  493. } else if (a->id < b->id) {
  494. return -1;
  495. } else {
  496. return 0;
  497. }
  498. }
  499. static int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **a, const SSL_CIPHER **b) {
  500. return ssl_cipher_id_cmp(*a, *b);
  501. }
  502. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  503. SSL_CIPHER c;
  504. c.id = 0x03000000L | value;
  505. return bsearch(&c, kCiphers, kCiphersLen, sizeof(SSL_CIPHER),
  506. ssl_cipher_id_cmp);
  507. }
  508. int ssl_cipher_get_evp_aead(const EVP_AEAD **out_aead,
  509. size_t *out_mac_secret_len,
  510. size_t *out_fixed_iv_len,
  511. const SSL_CIPHER *cipher, uint16_t version) {
  512. *out_aead = NULL;
  513. *out_mac_secret_len = 0;
  514. *out_fixed_iv_len = 0;
  515. switch (cipher->algorithm_enc) {
  516. case SSL_AES128GCM:
  517. *out_aead = EVP_aead_aes_128_gcm();
  518. *out_fixed_iv_len = 4;
  519. return 1;
  520. case SSL_AES256GCM:
  521. *out_aead = EVP_aead_aes_256_gcm();
  522. *out_fixed_iv_len = 4;
  523. return 1;
  524. case SSL_CHACHA20POLY1305:
  525. *out_aead = EVP_aead_chacha20_poly1305();
  526. *out_fixed_iv_len = 0;
  527. return 1;
  528. case SSL_RC4:
  529. switch (cipher->algorithm_mac) {
  530. case SSL_MD5:
  531. if (version == SSL3_VERSION) {
  532. *out_aead = EVP_aead_rc4_md5_ssl3();
  533. } else {
  534. *out_aead = EVP_aead_rc4_md5_tls();
  535. }
  536. *out_mac_secret_len = MD5_DIGEST_LENGTH;
  537. return 1;
  538. case SSL_SHA1:
  539. if (version == SSL3_VERSION) {
  540. *out_aead = EVP_aead_rc4_sha1_ssl3();
  541. } else {
  542. *out_aead = EVP_aead_rc4_sha1_tls();
  543. }
  544. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  545. return 1;
  546. default:
  547. return 0;
  548. }
  549. case SSL_AES128:
  550. switch (cipher->algorithm_mac) {
  551. case SSL_SHA1:
  552. if (version == SSL3_VERSION) {
  553. *out_aead = EVP_aead_aes_128_cbc_sha1_ssl3();
  554. *out_fixed_iv_len = 16;
  555. } else if (version == TLS1_VERSION) {
  556. *out_aead = EVP_aead_aes_128_cbc_sha1_tls_implicit_iv();
  557. *out_fixed_iv_len = 16;
  558. } else {
  559. *out_aead = EVP_aead_aes_128_cbc_sha1_tls();
  560. }
  561. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  562. return 1;
  563. case SSL_SHA256:
  564. *out_aead = EVP_aead_aes_128_cbc_sha256_tls();
  565. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  566. return 1;
  567. default:
  568. return 0;
  569. }
  570. case SSL_AES256:
  571. switch (cipher->algorithm_mac) {
  572. case SSL_SHA1:
  573. if (version == SSL3_VERSION) {
  574. *out_aead = EVP_aead_aes_256_cbc_sha1_ssl3();
  575. *out_fixed_iv_len = 16;
  576. } else if (version == TLS1_VERSION) {
  577. *out_aead = EVP_aead_aes_256_cbc_sha1_tls_implicit_iv();
  578. *out_fixed_iv_len = 16;
  579. } else {
  580. *out_aead = EVP_aead_aes_256_cbc_sha1_tls();
  581. }
  582. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  583. return 1;
  584. case SSL_SHA256:
  585. *out_aead = EVP_aead_aes_256_cbc_sha256_tls();
  586. *out_mac_secret_len = SHA256_DIGEST_LENGTH;
  587. return 1;
  588. case SSL_SHA384:
  589. *out_aead = EVP_aead_aes_256_cbc_sha384_tls();
  590. *out_mac_secret_len = SHA384_DIGEST_LENGTH;
  591. return 1;
  592. default:
  593. return 0;
  594. }
  595. case SSL_3DES:
  596. switch (cipher->algorithm_mac) {
  597. case SSL_SHA1:
  598. if (version == SSL3_VERSION) {
  599. *out_aead = EVP_aead_des_ede3_cbc_sha1_ssl3();
  600. *out_fixed_iv_len = 8;
  601. } else if (version == TLS1_VERSION) {
  602. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv();
  603. *out_fixed_iv_len = 8;
  604. } else {
  605. *out_aead = EVP_aead_des_ede3_cbc_sha1_tls();
  606. }
  607. *out_mac_secret_len = SHA_DIGEST_LENGTH;
  608. return 1;
  609. default:
  610. return 0;
  611. }
  612. default:
  613. return 0;
  614. }
  615. }
  616. const EVP_MD *ssl_get_handshake_digest(uint32_t algorithm_prf) {
  617. switch (algorithm_prf) {
  618. case SSL_HANDSHAKE_MAC_DEFAULT:
  619. return EVP_sha1();
  620. case SSL_HANDSHAKE_MAC_SHA256:
  621. return EVP_sha256();
  622. case SSL_HANDSHAKE_MAC_SHA384:
  623. return EVP_sha384();
  624. default:
  625. return NULL;
  626. }
  627. }
  628. #define ITEM_SEP(a) \
  629. (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
  630. /* rule_equals returns one iff the NUL-terminated string |rule| is equal to the
  631. * |buf_len| bytes at |buf|. */
  632. static int rule_equals(const char *rule, const char *buf, size_t buf_len) {
  633. /* |strncmp| alone only checks that |buf| is a prefix of |rule|. */
  634. return strncmp(rule, buf, buf_len) == 0 && rule[buf_len] == '\0';
  635. }
  636. static void ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  637. CIPHER_ORDER **tail) {
  638. if (curr == *tail) {
  639. return;
  640. }
  641. if (curr == *head) {
  642. *head = curr->next;
  643. }
  644. if (curr->prev != NULL) {
  645. curr->prev->next = curr->next;
  646. }
  647. if (curr->next != NULL) {
  648. curr->next->prev = curr->prev;
  649. }
  650. (*tail)->next = curr;
  651. curr->prev = *tail;
  652. curr->next = NULL;
  653. *tail = curr;
  654. }
  655. static void ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
  656. CIPHER_ORDER **tail) {
  657. if (curr == *head) {
  658. return;
  659. }
  660. if (curr == *tail) {
  661. *tail = curr->prev;
  662. }
  663. if (curr->next != NULL) {
  664. curr->next->prev = curr->prev;
  665. }
  666. if (curr->prev != NULL) {
  667. curr->prev->next = curr->next;
  668. }
  669. (*head)->prev = curr;
  670. curr->next = *head;
  671. curr->prev = NULL;
  672. *head = curr;
  673. }
  674. static void ssl_cipher_collect_ciphers(const SSL_PROTOCOL_METHOD *ssl_method,
  675. CIPHER_ORDER *co_list,
  676. CIPHER_ORDER **head_p,
  677. CIPHER_ORDER **tail_p) {
  678. /* The set of ciphers is static, but some subset may be unsupported by
  679. * |ssl_method|, so the list may be smaller. */
  680. size_t co_list_num = 0;
  681. size_t i;
  682. for (i = 0; i < kCiphersLen; i++) {
  683. const SSL_CIPHER *cipher = &kCiphers[i];
  684. if (ssl_method->supports_cipher(cipher)) {
  685. co_list[co_list_num].cipher = cipher;
  686. co_list[co_list_num].next = NULL;
  687. co_list[co_list_num].prev = NULL;
  688. co_list[co_list_num].active = 0;
  689. co_list[co_list_num].in_group = 0;
  690. co_list_num++;
  691. }
  692. }
  693. /* Prepare linked list from list entries. */
  694. if (co_list_num > 0) {
  695. co_list[0].prev = NULL;
  696. if (co_list_num > 1) {
  697. co_list[0].next = &co_list[1];
  698. for (i = 1; i < co_list_num - 1; i++) {
  699. co_list[i].prev = &co_list[i - 1];
  700. co_list[i].next = &co_list[i + 1];
  701. }
  702. co_list[co_list_num - 1].prev = &co_list[co_list_num - 2];
  703. }
  704. co_list[co_list_num - 1].next = NULL;
  705. *head_p = &co_list[0];
  706. *tail_p = &co_list[co_list_num - 1];
  707. }
  708. }
  709. /* ssl_cipher_apply_rule applies the rule type |rule| to ciphers matching its
  710. * parameters in the linked list from |*head_p| to |*tail_p|. It writes the new
  711. * head and tail of the list to |*head_p| and |*tail_p|, respectively.
  712. *
  713. * - If |cipher_id| is non-zero, only that cipher is selected.
  714. * - Otherwise, if |strength_bits| is non-negative, it selects ciphers
  715. * of that strength.
  716. * - Otherwise, it selects ciphers that match each bitmasks in |alg_*| and
  717. * |algo_strength|. */
  718. static void ssl_cipher_apply_rule(
  719. uint32_t cipher_id, uint32_t alg_mkey, uint32_t alg_auth,
  720. uint32_t alg_enc, uint32_t alg_mac, uint32_t alg_ssl,
  721. uint32_t algo_strength, int rule, int strength_bits, int in_group,
  722. CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) {
  723. CIPHER_ORDER *head, *tail, *curr, *next, *last;
  724. const SSL_CIPHER *cp;
  725. int reverse = 0;
  726. if (cipher_id == 0 && strength_bits == -1 &&
  727. (alg_mkey == 0 || alg_auth == 0 || alg_enc == 0 || alg_mac == 0 ||
  728. alg_ssl == 0 || algo_strength == 0)) {
  729. /* The rule matches nothing, so bail early. */
  730. return;
  731. }
  732. if (rule == CIPHER_DEL) {
  733. /* needed to maintain sorting between currently deleted ciphers */
  734. reverse = 1;
  735. }
  736. head = *head_p;
  737. tail = *tail_p;
  738. if (reverse) {
  739. next = tail;
  740. last = head;
  741. } else {
  742. next = head;
  743. last = tail;
  744. }
  745. curr = NULL;
  746. for (;;) {
  747. if (curr == last) {
  748. break;
  749. }
  750. curr = next;
  751. if (curr == NULL) {
  752. break;
  753. }
  754. next = reverse ? curr->prev : curr->next;
  755. cp = curr->cipher;
  756. /* Selection criteria is either a specific cipher, the value of
  757. * |strength_bits|, or the algorithms used. */
  758. if (cipher_id != 0) {
  759. if (cipher_id != cp->id) {
  760. continue;
  761. }
  762. } else if (strength_bits >= 0) {
  763. if (strength_bits != cp->strength_bits) {
  764. continue;
  765. }
  766. } else if (!(alg_mkey & cp->algorithm_mkey) ||
  767. !(alg_auth & cp->algorithm_auth) ||
  768. !(alg_enc & cp->algorithm_enc) ||
  769. !(alg_mac & cp->algorithm_mac) ||
  770. !(alg_ssl & cp->algorithm_ssl) ||
  771. !(algo_strength & cp->algo_strength)) {
  772. continue;
  773. }
  774. /* add the cipher if it has not been added yet. */
  775. if (rule == CIPHER_ADD) {
  776. /* reverse == 0 */
  777. if (!curr->active) {
  778. ll_append_tail(&head, curr, &tail);
  779. curr->active = 1;
  780. curr->in_group = in_group;
  781. }
  782. }
  783. /* Move the added cipher to this location */
  784. else if (rule == CIPHER_ORD) {
  785. /* reverse == 0 */
  786. if (curr->active) {
  787. ll_append_tail(&head, curr, &tail);
  788. curr->in_group = 0;
  789. }
  790. } else if (rule == CIPHER_DEL) {
  791. /* reverse == 1 */
  792. if (curr->active) {
  793. /* most recently deleted ciphersuites get best positions
  794. * for any future CIPHER_ADD (note that the CIPHER_DEL loop
  795. * works in reverse to maintain the order) */
  796. ll_append_head(&head, curr, &tail);
  797. curr->active = 0;
  798. curr->in_group = 0;
  799. }
  800. } else if (rule == CIPHER_KILL) {
  801. /* reverse == 0 */
  802. if (head == curr) {
  803. head = curr->next;
  804. } else {
  805. curr->prev->next = curr->next;
  806. }
  807. if (tail == curr) {
  808. tail = curr->prev;
  809. }
  810. curr->active = 0;
  811. if (curr->next != NULL) {
  812. curr->next->prev = curr->prev;
  813. }
  814. if (curr->prev != NULL) {
  815. curr->prev->next = curr->next;
  816. }
  817. curr->next = NULL;
  818. curr->prev = NULL;
  819. }
  820. }
  821. *head_p = head;
  822. *tail_p = tail;
  823. }
  824. static int ssl_cipher_strength_sort(CIPHER_ORDER **head_p,
  825. CIPHER_ORDER **tail_p) {
  826. int max_strength_bits, i, *number_uses;
  827. CIPHER_ORDER *curr;
  828. /* This routine sorts the ciphers with descending strength. The sorting must
  829. * keep the pre-sorted sequence, so we apply the normal sorting routine as
  830. * '+' movement to the end of the list. */
  831. max_strength_bits = 0;
  832. curr = *head_p;
  833. while (curr != NULL) {
  834. if (curr->active && curr->cipher->strength_bits > max_strength_bits) {
  835. max_strength_bits = curr->cipher->strength_bits;
  836. }
  837. curr = curr->next;
  838. }
  839. number_uses = OPENSSL_malloc((max_strength_bits + 1) * sizeof(int));
  840. if (!number_uses) {
  841. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  842. return 0;
  843. }
  844. memset(number_uses, 0, (max_strength_bits + 1) * sizeof(int));
  845. /* Now find the strength_bits values actually used. */
  846. curr = *head_p;
  847. while (curr != NULL) {
  848. if (curr->active) {
  849. number_uses[curr->cipher->strength_bits]++;
  850. }
  851. curr = curr->next;
  852. }
  853. /* Go through the list of used strength_bits values in descending order. */
  854. for (i = max_strength_bits; i >= 0; i--) {
  855. if (number_uses[i] > 0) {
  856. ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, 0, head_p,
  857. tail_p);
  858. }
  859. }
  860. OPENSSL_free(number_uses);
  861. return 1;
  862. }
  863. static int ssl_cipher_process_rulestr(const SSL_PROTOCOL_METHOD *ssl_method,
  864. const char *rule_str,
  865. CIPHER_ORDER **head_p,
  866. CIPHER_ORDER **tail_p) {
  867. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, algo_strength;
  868. const char *l, *buf;
  869. int multi, rule, retval, ok, in_group = 0, has_group = 0;
  870. size_t j, buf_len;
  871. uint32_t cipher_id;
  872. char ch;
  873. retval = 1;
  874. l = rule_str;
  875. for (;;) {
  876. ch = *l;
  877. if (ch == '\0') {
  878. break; /* done */
  879. }
  880. if (in_group) {
  881. if (ch == ']') {
  882. if (*tail_p) {
  883. (*tail_p)->in_group = 0;
  884. }
  885. in_group = 0;
  886. l++;
  887. continue;
  888. }
  889. if (ch == '|') {
  890. rule = CIPHER_ADD;
  891. l++;
  892. continue;
  893. } else if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') &&
  894. !(ch >= '0' && ch <= '9')) {
  895. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_OPERATOR_IN_GROUP);
  896. retval = in_group = 0;
  897. break;
  898. } else {
  899. rule = CIPHER_ADD;
  900. }
  901. } else if (ch == '-') {
  902. rule = CIPHER_DEL;
  903. l++;
  904. } else if (ch == '+') {
  905. rule = CIPHER_ORD;
  906. l++;
  907. } else if (ch == '!') {
  908. rule = CIPHER_KILL;
  909. l++;
  910. } else if (ch == '@') {
  911. rule = CIPHER_SPECIAL;
  912. l++;
  913. } else if (ch == '[') {
  914. if (in_group) {
  915. OPENSSL_PUT_ERROR(SSL, SSL_R_NESTED_GROUP);
  916. retval = in_group = 0;
  917. break;
  918. }
  919. in_group = 1;
  920. has_group = 1;
  921. l++;
  922. continue;
  923. } else {
  924. rule = CIPHER_ADD;
  925. }
  926. /* If preference groups are enabled, the only legal operator is +.
  927. * Otherwise the in_group bits will get mixed up. */
  928. if (has_group && rule != CIPHER_ADD) {
  929. OPENSSL_PUT_ERROR(SSL, SSL_R_MIXED_SPECIAL_OPERATOR_WITH_GROUPS);
  930. retval = in_group = 0;
  931. break;
  932. }
  933. if (ITEM_SEP(ch)) {
  934. l++;
  935. continue;
  936. }
  937. multi = 0;
  938. cipher_id = 0;
  939. alg_mkey = ~0u;
  940. alg_auth = ~0u;
  941. alg_enc = ~0u;
  942. alg_mac = ~0u;
  943. alg_ssl = ~0u;
  944. algo_strength = ~0u;
  945. for (;;) {
  946. ch = *l;
  947. buf = l;
  948. buf_len = 0;
  949. while (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) ||
  950. ((ch >= 'a') && (ch <= 'z')) || (ch == '-') || (ch == '.')) {
  951. ch = *(++l);
  952. buf_len++;
  953. }
  954. if (buf_len == 0) {
  955. /* We hit something we cannot deal with, it is no command or separator
  956. * nor alphanumeric, so we call this an error. */
  957. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  958. retval = in_group = 0;
  959. l++;
  960. break;
  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 < kCiphersLen; j++) {
  969. const SSL_CIPHER *cipher = &kCiphers[j];
  970. if (rule_equals(cipher->name, buf, buf_len)) {
  971. cipher_id = cipher->id;
  972. break;
  973. }
  974. }
  975. }
  976. if (cipher_id == 0) {
  977. /* If not an exact cipher, look for a matching cipher alias. */
  978. for (j = 0; j < kCipherAliasesLen; j++) {
  979. if (rule_equals(kCipherAliases[j].name, buf, buf_len)) {
  980. alg_mkey &= kCipherAliases[j].algorithm_mkey;
  981. alg_auth &= kCipherAliases[j].algorithm_auth;
  982. alg_enc &= kCipherAliases[j].algorithm_enc;
  983. alg_mac &= kCipherAliases[j].algorithm_mac;
  984. alg_ssl &= kCipherAliases[j].algorithm_ssl;
  985. algo_strength &= kCipherAliases[j].algo_strength;
  986. break;
  987. }
  988. }
  989. if (j == kCipherAliasesLen) {
  990. alg_mkey = alg_auth = alg_enc = alg_mac = alg_ssl = algo_strength = 0;
  991. }
  992. }
  993. /* Check for a multipart rule. */
  994. if (ch != '+') {
  995. break;
  996. }
  997. l++;
  998. multi = 1;
  999. }
  1000. /* Ok, we have the rule, now apply it. */
  1001. if (rule == CIPHER_SPECIAL) {
  1002. /* special command */
  1003. ok = 0;
  1004. if (buf_len == 8 && !strncmp(buf, "STRENGTH", 8)) {
  1005. ok = ssl_cipher_strength_sort(head_p, tail_p);
  1006. } else {
  1007. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1008. }
  1009. if (ok == 0) {
  1010. retval = 0;
  1011. }
  1012. /* We do not support any "multi" options together with "@", so throw away
  1013. * the rest of the command, if any left, until end or ':' is found. */
  1014. while (*l != '\0' && !ITEM_SEP(*l)) {
  1015. l++;
  1016. }
  1017. } else {
  1018. ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth, alg_enc, alg_mac,
  1019. alg_ssl, algo_strength, rule, -1, in_group, head_p,
  1020. tail_p);
  1021. }
  1022. }
  1023. if (in_group) {
  1024. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMMAND);
  1025. retval = 0;
  1026. }
  1027. return retval;
  1028. }
  1029. STACK_OF(SSL_CIPHER) *
  1030. ssl_create_cipher_list(const SSL_PROTOCOL_METHOD *ssl_method,
  1031. struct ssl_cipher_preference_list_st **out_cipher_list,
  1032. STACK_OF(SSL_CIPHER) **out_cipher_list_by_id,
  1033. const char *rule_str) {
  1034. int ok;
  1035. STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list = NULL;
  1036. const char *rule_p;
  1037. CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
  1038. uint8_t *in_group_flags = NULL;
  1039. unsigned int num_in_group_flags = 0;
  1040. struct ssl_cipher_preference_list_st *pref_list = NULL;
  1041. /* Return with error if nothing to do. */
  1042. if (rule_str == NULL || out_cipher_list == NULL) {
  1043. return NULL;
  1044. }
  1045. /* Now we have to collect the available ciphers from the compiled in ciphers.
  1046. * We cannot get more than the number compiled in, so it is used for
  1047. * allocation. */
  1048. co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * kCiphersLen);
  1049. if (co_list == NULL) {
  1050. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1051. return NULL;
  1052. }
  1053. ssl_cipher_collect_ciphers(ssl_method, co_list, &head, &tail);
  1054. /* Now arrange all ciphers by preference:
  1055. * TODO(davidben): Compute this order once and copy it. */
  1056. /* Everything else being equal, prefer ECDHE_ECDSA then ECDHE_RSA over other
  1057. * key exchange mechanisms */
  1058. ssl_cipher_apply_rule(0, SSL_kECDHE, SSL_aECDSA, ~0u, ~0u, ~0u, ~0u,
  1059. CIPHER_ADD, -1, 0, &head, &tail);
  1060. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1061. 0, &head, &tail);
  1062. ssl_cipher_apply_rule(0, SSL_kECDHE, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1,
  1063. 0, &head, &tail);
  1064. /* Order the bulk ciphers. First the preferred AEAD ciphers. We prefer
  1065. * CHACHA20 unless there is hardware support for fast and constant-time
  1066. * AES_GCM. */
  1067. if (EVP_has_aes_hardware()) {
  1068. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1069. -1, 0, &head, &tail);
  1070. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1071. -1, 0, &head, &tail);
  1072. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
  1073. CIPHER_ADD, -1, 0, &head, &tail);
  1074. } else {
  1075. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_CHACHA20POLY1305, ~0u, ~0u, ~0u,
  1076. CIPHER_ADD, -1, 0, &head, &tail);
  1077. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1078. -1, 0, &head, &tail);
  1079. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128GCM, ~0u, ~0u, ~0u, CIPHER_ADD,
  1080. -1, 0, &head, &tail);
  1081. }
  1082. /* Then the legacy non-AEAD ciphers: AES_256_CBC, AES-128_CBC, RC4_128_SHA,
  1083. * RC4_128_MD5, 3DES_EDE_CBC_SHA. */
  1084. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES256, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1085. 0, &head, &tail);
  1086. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_AES128, ~0u, ~0u, ~0u, CIPHER_ADD, -1,
  1087. 0, &head, &tail);
  1088. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, ~SSL_MD5, ~0u, ~0u, CIPHER_ADD,
  1089. -1, 0, &head, &tail);
  1090. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_RC4, SSL_MD5, ~0u, ~0u, CIPHER_ADD, -1,
  1091. 0, &head, &tail);
  1092. ssl_cipher_apply_rule(0, ~0u, ~0u, SSL_3DES, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
  1093. &head, &tail);
  1094. /* Temporarily enable everything else for sorting */
  1095. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_ADD, -1, 0,
  1096. &head, &tail);
  1097. /* Move ciphers without forward secrecy to the end. */
  1098. ssl_cipher_apply_rule(0, ~(SSL_kDHE | SSL_kECDHE), ~0u, ~0u, ~0u, ~0u, ~0u,
  1099. CIPHER_ORD, -1, 0, &head, &tail);
  1100. /* Now disable everything (maintaining the ordering!) */
  1101. ssl_cipher_apply_rule(0, ~0u, ~0u, ~0u, ~0u, ~0u, ~0u, CIPHER_DEL, -1, 0,
  1102. &head, &tail);
  1103. /* If the rule_string begins with DEFAULT, apply the default rule before
  1104. * using the (possibly available) additional rules. */
  1105. ok = 1;
  1106. rule_p = rule_str;
  1107. if (strncmp(rule_str, "DEFAULT", 7) == 0) {
  1108. ok = ssl_cipher_process_rulestr(ssl_method, SSL_DEFAULT_CIPHER_LIST, &head,
  1109. &tail);
  1110. rule_p += 7;
  1111. if (*rule_p == ':') {
  1112. rule_p++;
  1113. }
  1114. }
  1115. if (ok && strlen(rule_p) > 0) {
  1116. ok = ssl_cipher_process_rulestr(ssl_method, rule_p, &head, &tail);
  1117. }
  1118. if (!ok) {
  1119. goto err;
  1120. }
  1121. /* Allocate new "cipherstack" for the result, return with error
  1122. * if we cannot get one. */
  1123. cipherstack = sk_SSL_CIPHER_new_null();
  1124. if (cipherstack == NULL) {
  1125. goto err;
  1126. }
  1127. in_group_flags = OPENSSL_malloc(kCiphersLen);
  1128. if (!in_group_flags) {
  1129. goto err;
  1130. }
  1131. /* The cipher selection for the list is done. The ciphers are added
  1132. * to the resulting precedence to the STACK_OF(SSL_CIPHER). */
  1133. for (curr = head; curr != NULL; curr = curr->next) {
  1134. if (curr->active) {
  1135. if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
  1136. goto err;
  1137. }
  1138. in_group_flags[num_in_group_flags++] = curr->in_group;
  1139. }
  1140. }
  1141. OPENSSL_free(co_list); /* Not needed any longer */
  1142. co_list = NULL;
  1143. tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
  1144. if (tmp_cipher_list == NULL) {
  1145. goto err;
  1146. }
  1147. pref_list = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  1148. if (!pref_list) {
  1149. goto err;
  1150. }
  1151. pref_list->ciphers = cipherstack;
  1152. pref_list->in_group_flags = OPENSSL_malloc(num_in_group_flags);
  1153. if (!pref_list->in_group_flags) {
  1154. goto err;
  1155. }
  1156. memcpy(pref_list->in_group_flags, in_group_flags, num_in_group_flags);
  1157. OPENSSL_free(in_group_flags);
  1158. in_group_flags = NULL;
  1159. if (*out_cipher_list != NULL) {
  1160. ssl_cipher_preference_list_free(*out_cipher_list);
  1161. }
  1162. *out_cipher_list = pref_list;
  1163. pref_list = NULL;
  1164. if (out_cipher_list_by_id != NULL) {
  1165. sk_SSL_CIPHER_free(*out_cipher_list_by_id);
  1166. *out_cipher_list_by_id = tmp_cipher_list;
  1167. tmp_cipher_list = NULL;
  1168. (void) sk_SSL_CIPHER_set_cmp_func(*out_cipher_list_by_id,
  1169. ssl_cipher_ptr_id_cmp);
  1170. sk_SSL_CIPHER_sort(*out_cipher_list_by_id);
  1171. } else {
  1172. sk_SSL_CIPHER_free(tmp_cipher_list);
  1173. tmp_cipher_list = NULL;
  1174. }
  1175. return cipherstack;
  1176. err:
  1177. OPENSSL_free(co_list);
  1178. OPENSSL_free(in_group_flags);
  1179. sk_SSL_CIPHER_free(cipherstack);
  1180. sk_SSL_CIPHER_free(tmp_cipher_list);
  1181. if (pref_list) {
  1182. OPENSSL_free(pref_list->in_group_flags);
  1183. }
  1184. OPENSSL_free(pref_list);
  1185. return NULL;
  1186. }
  1187. uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; }
  1188. uint16_t ssl_cipher_get_value(const SSL_CIPHER *cipher) {
  1189. uint32_t id = cipher->id;
  1190. /* All ciphers are SSLv3. */
  1191. assert((id & 0xff000000) == 0x03000000);
  1192. return id & 0xffff;
  1193. }
  1194. int SSL_CIPHER_is_AES(const SSL_CIPHER *cipher) {
  1195. return (cipher->algorithm_enc & SSL_AES) != 0;
  1196. }
  1197. int SSL_CIPHER_has_MD5_HMAC(const SSL_CIPHER *cipher) {
  1198. return (cipher->algorithm_mac & SSL_MD5) != 0;
  1199. }
  1200. int SSL_CIPHER_is_AESGCM(const SSL_CIPHER *cipher) {
  1201. return (cipher->algorithm_mac & (SSL_AES128GCM | SSL_AES256GCM)) != 0;
  1202. }
  1203. int SSL_CIPHER_is_CHACHA20POLY1305(const SSL_CIPHER *cipher) {
  1204. return (cipher->algorithm_enc & SSL_CHACHA20POLY1305) != 0;
  1205. }
  1206. /* return the actual cipher being used */
  1207. const char *SSL_CIPHER_get_name(const SSL_CIPHER *cipher) {
  1208. if (cipher != NULL) {
  1209. return cipher->name;
  1210. }
  1211. return "(NONE)";
  1212. }
  1213. const char *SSL_CIPHER_get_kx_name(const SSL_CIPHER *cipher) {
  1214. if (cipher == NULL) {
  1215. return "";
  1216. }
  1217. switch (cipher->algorithm_mkey) {
  1218. case SSL_kRSA:
  1219. return "RSA";
  1220. case SSL_kDHE:
  1221. switch (cipher->algorithm_auth) {
  1222. case SSL_aRSA:
  1223. return "DHE_RSA";
  1224. default:
  1225. assert(0);
  1226. return "UNKNOWN";
  1227. }
  1228. case SSL_kECDHE:
  1229. switch (cipher->algorithm_auth) {
  1230. case SSL_aECDSA:
  1231. return "ECDHE_ECDSA";
  1232. case SSL_aRSA:
  1233. return "ECDHE_RSA";
  1234. case SSL_aPSK:
  1235. return "ECDHE_PSK";
  1236. default:
  1237. assert(0);
  1238. return "UNKNOWN";
  1239. }
  1240. case SSL_kPSK:
  1241. assert(cipher->algorithm_auth == SSL_aPSK);
  1242. return "PSK";
  1243. default:
  1244. assert(0);
  1245. return "UNKNOWN";
  1246. }
  1247. }
  1248. static const char *ssl_cipher_get_enc_name(const SSL_CIPHER *cipher) {
  1249. switch (cipher->algorithm_enc) {
  1250. case SSL_3DES:
  1251. return "3DES_EDE_CBC";
  1252. case SSL_RC4:
  1253. return "RC4";
  1254. case SSL_AES128:
  1255. return "AES_128_CBC";
  1256. case SSL_AES256:
  1257. return "AES_256_CBC";
  1258. case SSL_AES128GCM:
  1259. return "AES_128_GCM";
  1260. case SSL_AES256GCM:
  1261. return "AES_256_GCM";
  1262. case SSL_CHACHA20POLY1305:
  1263. return "CHACHA20_POLY1305";
  1264. break;
  1265. default:
  1266. assert(0);
  1267. return "UNKNOWN";
  1268. }
  1269. }
  1270. static const char *ssl_cipher_get_prf_name(const SSL_CIPHER *cipher) {
  1271. switch (cipher->algorithm_prf) {
  1272. case SSL_HANDSHAKE_MAC_DEFAULT:
  1273. /* Before TLS 1.2, the PRF component is the hash used in the HMAC, which is
  1274. * only ever MD5 or SHA-1. */
  1275. switch (cipher->algorithm_mac) {
  1276. case SSL_MD5:
  1277. return "MD5";
  1278. case SSL_SHA1:
  1279. return "SHA";
  1280. }
  1281. break;
  1282. case SSL_HANDSHAKE_MAC_SHA256:
  1283. return "SHA256";
  1284. case SSL_HANDSHAKE_MAC_SHA384:
  1285. return "SHA384";
  1286. }
  1287. assert(0);
  1288. return "UNKNOWN";
  1289. }
  1290. char *SSL_CIPHER_get_rfc_name(const SSL_CIPHER *cipher) {
  1291. if (cipher == NULL) {
  1292. return NULL;
  1293. }
  1294. const char *kx_name = SSL_CIPHER_get_kx_name(cipher);
  1295. const char *enc_name = ssl_cipher_get_enc_name(cipher);
  1296. const char *prf_name = ssl_cipher_get_prf_name(cipher);
  1297. /* The final name is TLS_{kx_name}_WITH_{enc_name}_{prf_name}. */
  1298. size_t len = 4 + strlen(kx_name) + 6 + strlen(enc_name) + 1 +
  1299. strlen(prf_name) + 1;
  1300. char *ret = OPENSSL_malloc(len);
  1301. if (ret == NULL) {
  1302. return NULL;
  1303. }
  1304. if (BUF_strlcpy(ret, "TLS_", len) >= len ||
  1305. BUF_strlcat(ret, kx_name, len) >= len ||
  1306. BUF_strlcat(ret, "_WITH_", len) >= len ||
  1307. BUF_strlcat(ret, enc_name, len) >= len ||
  1308. BUF_strlcat(ret, "_", len) >= len ||
  1309. BUF_strlcat(ret, prf_name, len) >= len) {
  1310. assert(0);
  1311. OPENSSL_free(ret);
  1312. return NULL;
  1313. }
  1314. assert(strlen(ret) + 1 == len);
  1315. return ret;
  1316. }
  1317. int SSL_CIPHER_get_bits(const SSL_CIPHER *cipher, int *out_alg_bits) {
  1318. if (cipher == NULL) {
  1319. return 0;
  1320. }
  1321. if (out_alg_bits != NULL) {
  1322. *out_alg_bits = cipher->alg_bits;
  1323. }
  1324. return cipher->strength_bits;
  1325. }
  1326. const char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf,
  1327. int len) {
  1328. const char *ver;
  1329. const char *kx, *au, *enc, *mac;
  1330. uint32_t alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
  1331. static const char *format = "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n";
  1332. alg_mkey = cipher->algorithm_mkey;
  1333. alg_auth = cipher->algorithm_auth;
  1334. alg_enc = cipher->algorithm_enc;
  1335. alg_mac = cipher->algorithm_mac;
  1336. alg_ssl = cipher->algorithm_ssl;
  1337. if (alg_ssl & SSL_SSLV3) {
  1338. ver = "SSLv3";
  1339. } else if (alg_ssl & SSL_TLSV1_2) {
  1340. ver = "TLSv1.2";
  1341. } else {
  1342. ver = "unknown";
  1343. }
  1344. switch (alg_mkey) {
  1345. case SSL_kRSA:
  1346. kx = "RSA";
  1347. break;
  1348. case SSL_kDHE:
  1349. kx = "DH";
  1350. break;
  1351. case SSL_kECDHE:
  1352. kx = "ECDH";
  1353. break;
  1354. case SSL_kPSK:
  1355. kx = "PSK";
  1356. break;
  1357. default:
  1358. kx = "unknown";
  1359. }
  1360. switch (alg_auth) {
  1361. case SSL_aRSA:
  1362. au = "RSA";
  1363. break;
  1364. case SSL_aECDSA:
  1365. au = "ECDSA";
  1366. break;
  1367. case SSL_aPSK:
  1368. au = "PSK";
  1369. break;
  1370. default:
  1371. au = "unknown";
  1372. break;
  1373. }
  1374. switch (alg_enc) {
  1375. case SSL_3DES:
  1376. enc = "3DES(168)";
  1377. break;
  1378. case SSL_RC4:
  1379. enc = "RC4(128)";
  1380. break;
  1381. case SSL_AES128:
  1382. enc = "AES(128)";
  1383. break;
  1384. case SSL_AES256:
  1385. enc = "AES(256)";
  1386. break;
  1387. case SSL_AES128GCM:
  1388. enc = "AESGCM(128)";
  1389. break;
  1390. case SSL_AES256GCM:
  1391. enc = "AESGCM(256)";
  1392. break;
  1393. case SSL_CHACHA20POLY1305:
  1394. enc = "ChaCha20-Poly1305";
  1395. break;
  1396. default:
  1397. enc = "unknown";
  1398. break;
  1399. }
  1400. switch (alg_mac) {
  1401. case SSL_MD5:
  1402. mac = "MD5";
  1403. break;
  1404. case SSL_SHA1:
  1405. mac = "SHA1";
  1406. break;
  1407. case SSL_SHA256:
  1408. mac = "SHA256";
  1409. break;
  1410. case SSL_SHA384:
  1411. mac = "SHA384";
  1412. break;
  1413. case SSL_AEAD:
  1414. mac = "AEAD";
  1415. break;
  1416. default:
  1417. mac = "unknown";
  1418. break;
  1419. }
  1420. if (buf == NULL) {
  1421. len = 128;
  1422. buf = OPENSSL_malloc(len);
  1423. if (buf == NULL) {
  1424. return NULL;
  1425. }
  1426. } else if (len < 128) {
  1427. return "Buffer too small";
  1428. }
  1429. BIO_snprintf(buf, len, format, cipher->name, ver, kx, au, enc, mac);
  1430. return buf;
  1431. }
  1432. const char *SSL_CIPHER_get_version(const SSL_CIPHER *cipher) {
  1433. return "TLSv1/SSLv3";
  1434. }
  1435. COMP_METHOD *SSL_COMP_get_compression_methods(void) { return NULL; }
  1436. int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm) { return 1; }
  1437. const char *SSL_COMP_get_name(const COMP_METHOD *comp) { return NULL; }
  1438. int ssl_cipher_get_key_type(const SSL_CIPHER *cipher) {
  1439. uint32_t alg_a = cipher->algorithm_auth;
  1440. if (alg_a & SSL_aECDSA) {
  1441. return EVP_PKEY_EC;
  1442. } else if (alg_a & SSL_aRSA) {
  1443. return EVP_PKEY_RSA;
  1444. }
  1445. return EVP_PKEY_NONE;
  1446. }
  1447. int ssl_cipher_has_server_public_key(const SSL_CIPHER *cipher) {
  1448. /* PSK-authenticated ciphers do not use a public key, except for
  1449. * RSA_PSK. */
  1450. if ((cipher->algorithm_auth & SSL_aPSK) &&
  1451. !(cipher->algorithm_mkey & SSL_kRSA)) {
  1452. return 0;
  1453. }
  1454. /* All other ciphers include it. */
  1455. return 1;
  1456. }
  1457. int ssl_cipher_requires_server_key_exchange(const SSL_CIPHER *cipher) {
  1458. /* Ephemeral Diffie-Hellman key exchanges require a ServerKeyExchange. */
  1459. if (cipher->algorithm_mkey & SSL_kDHE || cipher->algorithm_mkey & SSL_kECDHE) {
  1460. return 1;
  1461. }
  1462. /* It is optional in all others. */
  1463. return 0;
  1464. }
  1465. size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher) {
  1466. size_t block_size;
  1467. switch (cipher->algorithm_enc) {
  1468. case SSL_3DES:
  1469. block_size = 8;
  1470. break;
  1471. case SSL_AES128:
  1472. case SSL_AES256:
  1473. block_size = 16;
  1474. break;
  1475. default:
  1476. return 0;
  1477. }
  1478. size_t mac_len;
  1479. switch (cipher->algorithm_mac) {
  1480. case SSL_MD5:
  1481. mac_len = MD5_DIGEST_LENGTH;
  1482. break;
  1483. case SSL_SHA1:
  1484. mac_len = SHA_DIGEST_LENGTH;
  1485. break;
  1486. default:
  1487. return 0;
  1488. }
  1489. size_t ret = 1 + mac_len;
  1490. ret += block_size - (ret % block_size);
  1491. return ret;
  1492. }