s2n_cipher_suites.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <openssl/crypto.h>
  16. #include <string.h>
  17. #include "crypto/s2n_cipher.h"
  18. #include "crypto/s2n_openssl.h"
  19. #include "error/s2n_errno.h"
  20. #include "pq-crypto/s2n_pq.h"
  21. #include "tls/s2n_auth_selection.h"
  22. #include "tls/s2n_kex.h"
  23. #include "tls/s2n_psk.h"
  24. #include "tls/s2n_security_policies.h"
  25. #include "tls/s2n_tls.h"
  26. #include "tls/s2n_tls13.h"
  27. #include "utils/s2n_safety.h"
  28. /*************************
  29. * S2n Record Algorithms *
  30. *************************/
  31. const struct s2n_record_algorithm s2n_record_alg_null = {
  32. .cipher = &s2n_null_cipher,
  33. .hmac_alg = S2N_HMAC_NONE,
  34. .flags = 0,
  35. .encryption_limit = UINT64_MAX,
  36. };
  37. const struct s2n_record_algorithm s2n_record_alg_rc4_md5 = {
  38. .cipher = &s2n_rc4,
  39. .hmac_alg = S2N_HMAC_MD5,
  40. .flags = 0,
  41. .encryption_limit = UINT64_MAX,
  42. };
  43. const struct s2n_record_algorithm s2n_record_alg_rc4_sslv3_md5 = {
  44. .cipher = &s2n_rc4,
  45. .hmac_alg = S2N_HMAC_SSLv3_MD5,
  46. .flags = 0,
  47. .encryption_limit = UINT64_MAX,
  48. };
  49. const struct s2n_record_algorithm s2n_record_alg_rc4_sha = {
  50. .cipher = &s2n_rc4,
  51. .hmac_alg = S2N_HMAC_SHA1,
  52. .flags = 0,
  53. .encryption_limit = UINT64_MAX,
  54. };
  55. const struct s2n_record_algorithm s2n_record_alg_rc4_sslv3_sha = {
  56. .cipher = &s2n_rc4,
  57. .hmac_alg = S2N_HMAC_SSLv3_SHA1,
  58. .flags = 0,
  59. .encryption_limit = UINT64_MAX,
  60. };
  61. const struct s2n_record_algorithm s2n_record_alg_3des_sha = {
  62. .cipher = &s2n_3des,
  63. .hmac_alg = S2N_HMAC_SHA1,
  64. .flags = 0,
  65. .encryption_limit = UINT64_MAX,
  66. };
  67. const struct s2n_record_algorithm s2n_record_alg_3des_sslv3_sha = {
  68. .cipher = &s2n_3des,
  69. .hmac_alg = S2N_HMAC_SSLv3_SHA1,
  70. .flags = 0,
  71. .encryption_limit = UINT64_MAX,
  72. };
  73. const struct s2n_record_algorithm s2n_record_alg_aes128_sha = {
  74. .cipher = &s2n_aes128,
  75. .hmac_alg = S2N_HMAC_SHA1,
  76. .flags = 0,
  77. .encryption_limit = UINT64_MAX,
  78. };
  79. const struct s2n_record_algorithm s2n_record_alg_aes128_sslv3_sha = {
  80. .cipher = &s2n_aes128,
  81. .hmac_alg = S2N_HMAC_SSLv3_SHA1,
  82. .flags = 0,
  83. .encryption_limit = UINT64_MAX,
  84. };
  85. const struct s2n_record_algorithm s2n_record_alg_aes128_sha_composite = {
  86. .cipher = &s2n_aes128_sha,
  87. .hmac_alg = S2N_HMAC_NONE,
  88. .flags = 0,
  89. .encryption_limit = UINT64_MAX,
  90. };
  91. const struct s2n_record_algorithm s2n_record_alg_aes128_sha256 = {
  92. .cipher = &s2n_aes128,
  93. .hmac_alg = S2N_HMAC_SHA256,
  94. .flags = 0,
  95. .encryption_limit = UINT64_MAX,
  96. };
  97. const struct s2n_record_algorithm s2n_record_alg_aes128_sha256_composite = {
  98. .cipher = &s2n_aes128_sha256,
  99. .hmac_alg = S2N_HMAC_NONE,
  100. .encryption_limit = UINT64_MAX,
  101. };
  102. const struct s2n_record_algorithm s2n_record_alg_aes256_sha = {
  103. .cipher = &s2n_aes256,
  104. .hmac_alg = S2N_HMAC_SHA1,
  105. .flags = 0,
  106. .encryption_limit = UINT64_MAX,
  107. };
  108. const struct s2n_record_algorithm s2n_record_alg_aes256_sslv3_sha = {
  109. .cipher = &s2n_aes256,
  110. .hmac_alg = S2N_HMAC_SSLv3_SHA1,
  111. .flags = 0,
  112. .encryption_limit = UINT64_MAX,
  113. };
  114. const struct s2n_record_algorithm s2n_record_alg_aes256_sha_composite = {
  115. .cipher = &s2n_aes256_sha,
  116. .hmac_alg = S2N_HMAC_NONE,
  117. .flags = 0,
  118. .encryption_limit = UINT64_MAX,
  119. };
  120. const struct s2n_record_algorithm s2n_record_alg_aes256_sha256 = {
  121. .cipher = &s2n_aes256,
  122. .hmac_alg = S2N_HMAC_SHA256,
  123. .flags = 0,
  124. .encryption_limit = UINT64_MAX,
  125. };
  126. const struct s2n_record_algorithm s2n_record_alg_aes256_sha256_composite = {
  127. .cipher = &s2n_aes256_sha256,
  128. .hmac_alg = S2N_HMAC_NONE,
  129. .encryption_limit = UINT64_MAX,
  130. };
  131. const struct s2n_record_algorithm s2n_record_alg_aes256_sha384 = {
  132. .cipher = &s2n_aes256,
  133. .hmac_alg = S2N_HMAC_SHA384,
  134. .flags = 0,
  135. .encryption_limit = UINT64_MAX,
  136. };
  137. const struct s2n_record_algorithm s2n_record_alg_aes128_gcm = {
  138. .cipher = &s2n_aes128_gcm,
  139. .hmac_alg = S2N_HMAC_NONE,
  140. .flags = S2N_TLS12_AES_GCM_AEAD_NONCE,
  141. .encryption_limit = UINT64_MAX,
  142. };
  143. const struct s2n_record_algorithm s2n_record_alg_aes256_gcm = {
  144. .cipher = &s2n_aes256_gcm,
  145. .hmac_alg = S2N_HMAC_NONE,
  146. .flags = S2N_TLS12_AES_GCM_AEAD_NONCE,
  147. .encryption_limit = UINT64_MAX,
  148. };
  149. const struct s2n_record_algorithm s2n_record_alg_chacha20_poly1305 = {
  150. .cipher = &s2n_chacha20_poly1305,
  151. .hmac_alg = S2N_HMAC_NONE,
  152. /* Per RFC 7905, ChaCha20-Poly1305 will use a nonce construction expected to be used in TLS1.3.
  153. * Give it a distinct 1.2 nonce value in case this changes.
  154. */
  155. .flags = S2N_TLS12_CHACHA_POLY_AEAD_NONCE,
  156. .encryption_limit = UINT64_MAX,
  157. };
  158. /* TLS 1.3 Record Algorithms */
  159. const struct s2n_record_algorithm s2n_tls13_record_alg_aes128_gcm = {
  160. .cipher = &s2n_tls13_aes128_gcm,
  161. .hmac_alg = S2N_HMAC_NONE, /* previously used in 1.2 prf, we do not need this */
  162. .flags = S2N_TLS13_RECORD_AEAD_NONCE,
  163. .encryption_limit = S2N_TLS13_AES_GCM_MAXIMUM_RECORD_NUMBER,
  164. };
  165. const struct s2n_record_algorithm s2n_tls13_record_alg_aes256_gcm = {
  166. .cipher = &s2n_tls13_aes256_gcm,
  167. .hmac_alg = S2N_HMAC_NONE,
  168. .flags = S2N_TLS13_RECORD_AEAD_NONCE,
  169. .encryption_limit = S2N_TLS13_AES_GCM_MAXIMUM_RECORD_NUMBER,
  170. };
  171. const struct s2n_record_algorithm s2n_tls13_record_alg_chacha20_poly1305 = {
  172. .cipher = &s2n_chacha20_poly1305,
  173. .hmac_alg = S2N_HMAC_NONE,
  174. /* this mirrors s2n_record_alg_chacha20_poly1305 with the exception of TLS 1.3 nonce flag */
  175. .flags = S2N_TLS13_RECORD_AEAD_NONCE,
  176. .encryption_limit = UINT64_MAX,
  177. };
  178. /*********************
  179. * S2n Cipher Suites *
  180. *********************/
  181. /* This is the initial cipher suite, but is never negotiated */
  182. struct s2n_cipher_suite s2n_null_cipher_suite = {
  183. .available = 1,
  184. .name = "TLS_NULL_WITH_NULL_NULL",
  185. .iana_value = { TLS_NULL_WITH_NULL_NULL },
  186. .key_exchange_alg = &s2n_rsa,
  187. .auth_method = S2N_AUTHENTICATION_RSA,
  188. .record_alg = &s2n_record_alg_null,
  189. };
  190. struct s2n_cipher_suite s2n_rsa_with_rc4_128_md5 = /* 0x00,0x04 */ {
  191. .available = 0,
  192. .name = "RC4-MD5",
  193. .iana_value = { TLS_RSA_WITH_RC4_128_MD5 },
  194. .key_exchange_alg = &s2n_rsa,
  195. .auth_method = S2N_AUTHENTICATION_RSA,
  196. .record_alg = NULL,
  197. .all_record_algs = { &s2n_record_alg_rc4_md5 },
  198. .num_record_algs = 1,
  199. .sslv3_record_alg = &s2n_record_alg_rc4_sslv3_md5,
  200. .prf_alg = S2N_HMAC_SHA256,
  201. .minimum_required_tls_version = S2N_SSLv3,
  202. };
  203. struct s2n_cipher_suite s2n_rsa_with_rc4_128_sha = /* 0x00,0x05 */ {
  204. .available = 0,
  205. .name = "RC4-SHA",
  206. .iana_value = { TLS_RSA_WITH_RC4_128_SHA },
  207. .key_exchange_alg = &s2n_rsa,
  208. .auth_method = S2N_AUTHENTICATION_RSA,
  209. .record_alg = NULL,
  210. .all_record_algs = { &s2n_record_alg_rc4_sha },
  211. .num_record_algs = 1,
  212. .sslv3_record_alg = &s2n_record_alg_rc4_sslv3_sha,
  213. .prf_alg = S2N_HMAC_SHA256,
  214. .minimum_required_tls_version = S2N_SSLv3,
  215. };
  216. struct s2n_cipher_suite s2n_rsa_with_3des_ede_cbc_sha = /* 0x00,0x0A */ {
  217. .available = 0,
  218. .name = "DES-CBC3-SHA",
  219. .iana_value = { TLS_RSA_WITH_3DES_EDE_CBC_SHA },
  220. .key_exchange_alg = &s2n_rsa,
  221. .auth_method = S2N_AUTHENTICATION_RSA,
  222. .record_alg = NULL,
  223. .all_record_algs = { &s2n_record_alg_3des_sha },
  224. .num_record_algs = 1,
  225. .sslv3_record_alg = &s2n_record_alg_3des_sslv3_sha,
  226. .prf_alg = S2N_HMAC_SHA256,
  227. .minimum_required_tls_version = S2N_SSLv3,
  228. };
  229. struct s2n_cipher_suite s2n_dhe_rsa_with_3des_ede_cbc_sha = /* 0x00,0x16 */ {
  230. .available = 0,
  231. .name = "DHE-RSA-DES-CBC3-SHA",
  232. .iana_value = { TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA },
  233. .key_exchange_alg = &s2n_dhe,
  234. .auth_method = S2N_AUTHENTICATION_RSA,
  235. .record_alg = NULL,
  236. .all_record_algs = { &s2n_record_alg_3des_sha },
  237. .num_record_algs = 1,
  238. .sslv3_record_alg = &s2n_record_alg_3des_sslv3_sha,
  239. .prf_alg = S2N_HMAC_SHA256,
  240. .minimum_required_tls_version = S2N_SSLv3,
  241. };
  242. struct s2n_cipher_suite s2n_rsa_with_aes_128_cbc_sha = /* 0x00,0x2F */ {
  243. .available = 0,
  244. .name = "AES128-SHA",
  245. .iana_value = { TLS_RSA_WITH_AES_128_CBC_SHA },
  246. .key_exchange_alg = &s2n_rsa,
  247. .auth_method = S2N_AUTHENTICATION_RSA,
  248. .record_alg = NULL,
  249. .all_record_algs = { &s2n_record_alg_aes128_sha_composite, &s2n_record_alg_aes128_sha },
  250. .num_record_algs = 2,
  251. .sslv3_record_alg = &s2n_record_alg_aes128_sslv3_sha,
  252. .prf_alg = S2N_HMAC_SHA256,
  253. .minimum_required_tls_version = S2N_SSLv3,
  254. };
  255. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_cbc_sha = /* 0x00,0x33 */ {
  256. .available = 0,
  257. .name = "DHE-RSA-AES128-SHA",
  258. .iana_value = { TLS_DHE_RSA_WITH_AES_128_CBC_SHA },
  259. .key_exchange_alg = &s2n_dhe,
  260. .auth_method = S2N_AUTHENTICATION_RSA,
  261. .record_alg = NULL,
  262. .all_record_algs = { &s2n_record_alg_aes128_sha_composite, &s2n_record_alg_aes128_sha },
  263. .num_record_algs = 2,
  264. .sslv3_record_alg = &s2n_record_alg_aes128_sslv3_sha,
  265. .prf_alg = S2N_HMAC_SHA256,
  266. .minimum_required_tls_version = S2N_SSLv3,
  267. };
  268. struct s2n_cipher_suite s2n_rsa_with_aes_256_cbc_sha = /* 0x00,0x35 */ {
  269. .available = 0,
  270. .name = "AES256-SHA",
  271. .iana_value = { TLS_RSA_WITH_AES_256_CBC_SHA },
  272. .key_exchange_alg = &s2n_rsa,
  273. .auth_method = S2N_AUTHENTICATION_RSA,
  274. .record_alg = NULL,
  275. .all_record_algs = { &s2n_record_alg_aes256_sha_composite, &s2n_record_alg_aes256_sha },
  276. .num_record_algs = 2,
  277. .sslv3_record_alg = &s2n_record_alg_aes256_sslv3_sha,
  278. .prf_alg = S2N_HMAC_SHA256,
  279. .minimum_required_tls_version = S2N_SSLv3,
  280. };
  281. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_cbc_sha = /* 0x00,0x39 */ {
  282. .available = 0,
  283. .name = "DHE-RSA-AES256-SHA",
  284. .iana_value = { TLS_DHE_RSA_WITH_AES_256_CBC_SHA },
  285. .key_exchange_alg = &s2n_dhe,
  286. .auth_method = S2N_AUTHENTICATION_RSA,
  287. .record_alg = NULL,
  288. .all_record_algs = { &s2n_record_alg_aes256_sha_composite, &s2n_record_alg_aes256_sha },
  289. .num_record_algs = 2,
  290. .sslv3_record_alg = &s2n_record_alg_aes256_sslv3_sha,
  291. .prf_alg = S2N_HMAC_SHA256,
  292. .minimum_required_tls_version = S2N_SSLv3,
  293. };
  294. struct s2n_cipher_suite s2n_rsa_with_aes_128_cbc_sha256 = /* 0x00,0x3C */ {
  295. .available = 0,
  296. .name = "AES128-SHA256",
  297. .iana_value = { TLS_RSA_WITH_AES_128_CBC_SHA256 },
  298. .key_exchange_alg = &s2n_rsa,
  299. .auth_method = S2N_AUTHENTICATION_RSA,
  300. .record_alg = NULL,
  301. .all_record_algs = { &s2n_record_alg_aes128_sha256_composite, &s2n_record_alg_aes128_sha256 },
  302. .num_record_algs = 2,
  303. .sslv3_record_alg = NULL,
  304. .prf_alg = S2N_HMAC_SHA256,
  305. .minimum_required_tls_version = S2N_TLS12,
  306. };
  307. struct s2n_cipher_suite s2n_rsa_with_aes_256_cbc_sha256 = /* 0x00,0x3D */ {
  308. .available = 0,
  309. .name = "AES256-SHA256",
  310. .iana_value = { TLS_RSA_WITH_AES_256_CBC_SHA256 },
  311. .key_exchange_alg = &s2n_rsa,
  312. .auth_method = S2N_AUTHENTICATION_RSA,
  313. .record_alg = NULL,
  314. .all_record_algs = { &s2n_record_alg_aes256_sha256_composite, &s2n_record_alg_aes256_sha256 },
  315. .num_record_algs = 2,
  316. .sslv3_record_alg = NULL,
  317. .prf_alg = S2N_HMAC_SHA256,
  318. .minimum_required_tls_version = S2N_TLS12,
  319. };
  320. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_cbc_sha256 = /* 0x00,0x67 */ {
  321. .available = 0,
  322. .name = "DHE-RSA-AES128-SHA256",
  323. .iana_value = { TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 },
  324. .key_exchange_alg = &s2n_dhe,
  325. .auth_method = S2N_AUTHENTICATION_RSA,
  326. .record_alg = NULL,
  327. .all_record_algs = { &s2n_record_alg_aes128_sha256_composite, &s2n_record_alg_aes128_sha256 },
  328. .num_record_algs = 2,
  329. .sslv3_record_alg = NULL,
  330. .prf_alg = S2N_HMAC_SHA256,
  331. .minimum_required_tls_version = S2N_TLS12,
  332. };
  333. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_cbc_sha256 = /* 0x00,0x6B */ {
  334. .available = 0,
  335. .name = "DHE-RSA-AES256-SHA256",
  336. .iana_value = { TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 },
  337. .key_exchange_alg = &s2n_dhe,
  338. .auth_method = S2N_AUTHENTICATION_RSA,
  339. .record_alg = NULL,
  340. .all_record_algs = { &s2n_record_alg_aes256_sha256_composite, &s2n_record_alg_aes256_sha256 },
  341. .num_record_algs = 2,
  342. .sslv3_record_alg = NULL,
  343. .prf_alg = S2N_HMAC_SHA256,
  344. .minimum_required_tls_version = S2N_TLS12,
  345. };
  346. struct s2n_cipher_suite s2n_rsa_with_aes_128_gcm_sha256 = /* 0x00,0x9C */ {
  347. .available = 0,
  348. .name = "AES128-GCM-SHA256",
  349. .iana_value = { TLS_RSA_WITH_AES_128_GCM_SHA256 },
  350. .key_exchange_alg = &s2n_rsa,
  351. .auth_method = S2N_AUTHENTICATION_RSA,
  352. .record_alg = NULL,
  353. .all_record_algs = { &s2n_record_alg_aes128_gcm },
  354. .num_record_algs = 1,
  355. .sslv3_record_alg = NULL,
  356. .prf_alg = S2N_HMAC_SHA256,
  357. .minimum_required_tls_version = S2N_TLS12,
  358. };
  359. struct s2n_cipher_suite s2n_rsa_with_aes_256_gcm_sha384 = /* 0x00,0x9D */ {
  360. .available = 0,
  361. .name = "AES256-GCM-SHA384",
  362. .iana_value = { TLS_RSA_WITH_AES_256_GCM_SHA384 },
  363. .key_exchange_alg = &s2n_rsa,
  364. .auth_method = S2N_AUTHENTICATION_RSA,
  365. .record_alg = NULL,
  366. .all_record_algs = { &s2n_record_alg_aes256_gcm },
  367. .num_record_algs = 1,
  368. .sslv3_record_alg = NULL,
  369. .prf_alg = S2N_HMAC_SHA384,
  370. .minimum_required_tls_version = S2N_TLS12,
  371. };
  372. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_128_gcm_sha256 = /* 0x00,0x9E */ {
  373. .available = 0,
  374. .name = "DHE-RSA-AES128-GCM-SHA256",
  375. .iana_value = { TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 },
  376. .key_exchange_alg = &s2n_dhe,
  377. .auth_method = S2N_AUTHENTICATION_RSA,
  378. .record_alg = NULL,
  379. .all_record_algs = { &s2n_record_alg_aes128_gcm },
  380. .num_record_algs = 1,
  381. .sslv3_record_alg = NULL,
  382. .prf_alg = S2N_HMAC_SHA256,
  383. .minimum_required_tls_version = S2N_TLS12,
  384. };
  385. struct s2n_cipher_suite s2n_dhe_rsa_with_aes_256_gcm_sha384 = /* 0x00,0x9F */ {
  386. .available = 0,
  387. .name = "DHE-RSA-AES256-GCM-SHA384",
  388. .iana_value = { TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 },
  389. .key_exchange_alg = &s2n_dhe,
  390. .auth_method = S2N_AUTHENTICATION_RSA,
  391. .record_alg = NULL,
  392. .all_record_algs = { &s2n_record_alg_aes256_gcm },
  393. .num_record_algs = 1,
  394. .sslv3_record_alg = NULL,
  395. .prf_alg = S2N_HMAC_SHA384,
  396. .minimum_required_tls_version = S2N_TLS12,
  397. };
  398. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_cbc_sha = /* 0xC0,0x09 */ {
  399. .available = 0,
  400. .name = "ECDHE-ECDSA-AES128-SHA",
  401. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA },
  402. .key_exchange_alg = &s2n_ecdhe,
  403. .auth_method = S2N_AUTHENTICATION_ECDSA,
  404. .record_alg = NULL,
  405. .all_record_algs = { &s2n_record_alg_aes128_sha_composite, &s2n_record_alg_aes128_sha },
  406. .num_record_algs = 2,
  407. .sslv3_record_alg = &s2n_record_alg_aes128_sslv3_sha,
  408. .prf_alg = S2N_HMAC_SHA256,
  409. .minimum_required_tls_version = S2N_SSLv3,
  410. };
  411. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_cbc_sha = /* 0xC0,0x0A */ {
  412. .available = 0,
  413. .name = "ECDHE-ECDSA-AES256-SHA",
  414. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA },
  415. .key_exchange_alg = &s2n_ecdhe,
  416. .auth_method = S2N_AUTHENTICATION_ECDSA,
  417. .record_alg = NULL,
  418. .all_record_algs = { &s2n_record_alg_aes256_sha_composite, &s2n_record_alg_aes256_sha },
  419. .num_record_algs = 2,
  420. .sslv3_record_alg = &s2n_record_alg_aes256_sslv3_sha,
  421. .prf_alg = S2N_HMAC_SHA256,
  422. .minimum_required_tls_version = S2N_SSLv3,
  423. };
  424. struct s2n_cipher_suite s2n_ecdhe_rsa_with_rc4_128_sha = /* 0xC0,0x11 */ {
  425. .available = 0,
  426. .name = "ECDHE-RSA-RC4-SHA",
  427. .iana_value = { TLS_ECDHE_RSA_WITH_RC4_128_SHA },
  428. .key_exchange_alg = &s2n_ecdhe,
  429. .auth_method = S2N_AUTHENTICATION_RSA,
  430. .record_alg = NULL,
  431. .all_record_algs = { &s2n_record_alg_rc4_sha },
  432. .num_record_algs = 1,
  433. .sslv3_record_alg = &s2n_record_alg_rc4_sslv3_sha,
  434. .prf_alg = S2N_HMAC_SHA256,
  435. .minimum_required_tls_version = S2N_SSLv3,
  436. };
  437. struct s2n_cipher_suite s2n_ecdhe_rsa_with_3des_ede_cbc_sha = /* 0xC0,0x12 */ {
  438. .available = 0,
  439. .name = "ECDHE-RSA-DES-CBC3-SHA",
  440. .iana_value = { TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA },
  441. .key_exchange_alg = &s2n_ecdhe,
  442. .auth_method = S2N_AUTHENTICATION_RSA,
  443. .record_alg = NULL,
  444. .all_record_algs = { &s2n_record_alg_3des_sha },
  445. .num_record_algs = 1,
  446. .sslv3_record_alg = &s2n_record_alg_3des_sslv3_sha,
  447. .prf_alg = S2N_HMAC_SHA256,
  448. .minimum_required_tls_version = S2N_SSLv3,
  449. };
  450. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_cbc_sha = /* 0xC0,0x13 */ {
  451. .available = 0,
  452. .name = "ECDHE-RSA-AES128-SHA",
  453. .iana_value = { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA },
  454. .key_exchange_alg = &s2n_ecdhe,
  455. .auth_method = S2N_AUTHENTICATION_RSA,
  456. .record_alg = NULL,
  457. .all_record_algs = { &s2n_record_alg_aes128_sha_composite, &s2n_record_alg_aes128_sha },
  458. .num_record_algs = 2,
  459. .sslv3_record_alg = &s2n_record_alg_aes128_sslv3_sha,
  460. .prf_alg = S2N_HMAC_SHA256,
  461. .minimum_required_tls_version = S2N_SSLv3,
  462. };
  463. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_cbc_sha = /* 0xC0,0x14 */ {
  464. .available = 0,
  465. .name = "ECDHE-RSA-AES256-SHA",
  466. .iana_value = { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA },
  467. .key_exchange_alg = &s2n_ecdhe,
  468. .auth_method = S2N_AUTHENTICATION_RSA,
  469. .record_alg = NULL,
  470. .all_record_algs = { &s2n_record_alg_aes256_sha_composite, &s2n_record_alg_aes256_sha },
  471. .num_record_algs = 2,
  472. .sslv3_record_alg = &s2n_record_alg_aes256_sslv3_sha,
  473. .prf_alg = S2N_HMAC_SHA256,
  474. .minimum_required_tls_version = S2N_SSLv3,
  475. };
  476. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256 = /* 0xC0,0x23 */ {
  477. .available = 0,
  478. .name = "ECDHE-ECDSA-AES128-SHA256",
  479. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 },
  480. .key_exchange_alg = &s2n_ecdhe,
  481. .auth_method = S2N_AUTHENTICATION_ECDSA,
  482. .record_alg = NULL,
  483. .all_record_algs = { &s2n_record_alg_aes128_sha256_composite, &s2n_record_alg_aes128_sha256 },
  484. .num_record_algs = 2,
  485. .sslv3_record_alg = NULL,
  486. .prf_alg = S2N_HMAC_SHA256,
  487. .minimum_required_tls_version = S2N_TLS12,
  488. };
  489. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384 = /* 0xC0,0x24 */ {
  490. .available = 0,
  491. .name = "ECDHE-ECDSA-AES256-SHA384",
  492. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 },
  493. .key_exchange_alg = &s2n_ecdhe,
  494. .auth_method = S2N_AUTHENTICATION_ECDSA,
  495. .record_alg = NULL,
  496. .all_record_algs = { &s2n_record_alg_aes256_sha384 },
  497. .num_record_algs = 1,
  498. .sslv3_record_alg = NULL,
  499. .prf_alg = S2N_HMAC_SHA384,
  500. .minimum_required_tls_version = S2N_TLS12,
  501. };
  502. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_cbc_sha256 = /* 0xC0,0x27 */ {
  503. .available = 0,
  504. .name = "ECDHE-RSA-AES128-SHA256",
  505. .iana_value = { TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 },
  506. .key_exchange_alg = &s2n_ecdhe,
  507. .auth_method = S2N_AUTHENTICATION_RSA,
  508. .record_alg = NULL,
  509. .all_record_algs = { &s2n_record_alg_aes128_sha256_composite, &s2n_record_alg_aes128_sha256 },
  510. .num_record_algs = 2,
  511. .sslv3_record_alg = NULL,
  512. .prf_alg = S2N_HMAC_SHA256,
  513. .minimum_required_tls_version = S2N_TLS12,
  514. };
  515. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_cbc_sha384 = /* 0xC0,0x28 */ {
  516. .available = 0,
  517. .name = "ECDHE-RSA-AES256-SHA384",
  518. .iana_value = { TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 },
  519. .key_exchange_alg = &s2n_ecdhe,
  520. .auth_method = S2N_AUTHENTICATION_RSA,
  521. .record_alg = NULL,
  522. .all_record_algs = { &s2n_record_alg_aes256_sha384 },
  523. .num_record_algs = 1,
  524. .sslv3_record_alg = NULL,
  525. .prf_alg = S2N_HMAC_SHA384,
  526. .minimum_required_tls_version = S2N_TLS12,
  527. };
  528. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256 = /* 0xC0,0x2B */ {
  529. .available = 0,
  530. .name = "ECDHE-ECDSA-AES128-GCM-SHA256",
  531. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 },
  532. .key_exchange_alg = &s2n_ecdhe,
  533. .auth_method = S2N_AUTHENTICATION_ECDSA,
  534. .record_alg = NULL,
  535. .all_record_algs = { &s2n_record_alg_aes128_gcm },
  536. .num_record_algs = 1,
  537. .sslv3_record_alg = NULL,
  538. .prf_alg = S2N_HMAC_SHA256,
  539. .minimum_required_tls_version = S2N_TLS12,
  540. };
  541. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384 = /* 0xC0,0x2C */ {
  542. .available = 0,
  543. .name = "ECDHE-ECDSA-AES256-GCM-SHA384",
  544. .iana_value = { TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 },
  545. .key_exchange_alg = &s2n_ecdhe,
  546. .auth_method = S2N_AUTHENTICATION_ECDSA,
  547. .record_alg = NULL,
  548. .all_record_algs = { &s2n_record_alg_aes256_gcm },
  549. .num_record_algs = 1,
  550. .sslv3_record_alg = NULL,
  551. .prf_alg = S2N_HMAC_SHA384,
  552. .minimum_required_tls_version = S2N_TLS12,
  553. };
  554. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_128_gcm_sha256 = /* 0xC0,0x2F */ {
  555. .available = 0,
  556. .name = "ECDHE-RSA-AES128-GCM-SHA256",
  557. .iana_value = { TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 },
  558. .key_exchange_alg = &s2n_ecdhe,
  559. .auth_method = S2N_AUTHENTICATION_RSA,
  560. .record_alg = NULL,
  561. .all_record_algs = { &s2n_record_alg_aes128_gcm },
  562. .num_record_algs = 1,
  563. .sslv3_record_alg = NULL,
  564. .prf_alg = S2N_HMAC_SHA256,
  565. .minimum_required_tls_version = S2N_TLS12,
  566. };
  567. struct s2n_cipher_suite s2n_ecdhe_rsa_with_aes_256_gcm_sha384 = /* 0xC0,0x30 */ {
  568. .available = 0,
  569. .name = "ECDHE-RSA-AES256-GCM-SHA384",
  570. .iana_value = { TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 },
  571. .key_exchange_alg = &s2n_ecdhe,
  572. .auth_method = S2N_AUTHENTICATION_RSA,
  573. .record_alg = NULL,
  574. .all_record_algs = { &s2n_record_alg_aes256_gcm },
  575. .num_record_algs = 1,
  576. .sslv3_record_alg = NULL,
  577. .prf_alg = S2N_HMAC_SHA384,
  578. .minimum_required_tls_version = S2N_TLS12,
  579. };
  580. struct s2n_cipher_suite s2n_ecdhe_rsa_with_chacha20_poly1305_sha256 = /* 0xCC,0xA8 */ {
  581. .available = 0,
  582. .name = "ECDHE-RSA-CHACHA20-POLY1305",
  583. .iana_value = { TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 },
  584. .key_exchange_alg = &s2n_ecdhe,
  585. .auth_method = S2N_AUTHENTICATION_RSA,
  586. .record_alg = NULL,
  587. .all_record_algs = { &s2n_record_alg_chacha20_poly1305 },
  588. .num_record_algs = 1,
  589. .sslv3_record_alg = NULL,
  590. .prf_alg = S2N_HMAC_SHA256,
  591. .minimum_required_tls_version = S2N_TLS12,
  592. };
  593. struct s2n_cipher_suite s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256 = /* 0xCC,0xA9 */ {
  594. .available = 0,
  595. .name = "ECDHE-ECDSA-CHACHA20-POLY1305",
  596. .iana_value = { TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 },
  597. .key_exchange_alg = &s2n_ecdhe,
  598. .auth_method = S2N_AUTHENTICATION_ECDSA,
  599. .record_alg = NULL,
  600. .all_record_algs = { &s2n_record_alg_chacha20_poly1305 },
  601. .num_record_algs = 1,
  602. .sslv3_record_alg = NULL,
  603. .prf_alg = S2N_HMAC_SHA256,
  604. .minimum_required_tls_version = S2N_TLS12,
  605. };
  606. struct s2n_cipher_suite s2n_dhe_rsa_with_chacha20_poly1305_sha256 = /* 0xCC,0xAA */ {
  607. .available = 0,
  608. .name = "DHE-RSA-CHACHA20-POLY1305",
  609. .iana_value = { TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 },
  610. .key_exchange_alg = &s2n_dhe,
  611. .auth_method = S2N_AUTHENTICATION_RSA,
  612. .record_alg = NULL,
  613. .all_record_algs = { &s2n_record_alg_chacha20_poly1305 },
  614. .num_record_algs = 1,
  615. .sslv3_record_alg = NULL,
  616. .prf_alg = S2N_HMAC_SHA256,
  617. .minimum_required_tls_version = S2N_TLS12,
  618. };
  619. /* From https://tools.ietf.org/html/draft-campagna-tls-bike-sike-hybrid */
  620. struct s2n_cipher_suite s2n_ecdhe_kyber_rsa_with_aes_256_gcm_sha384 = /* 0xFF, 0x0C */ {
  621. .available = 0,
  622. .name = "ECDHE-KYBER-RSA-AES256-GCM-SHA384",
  623. .iana_value = { TLS_ECDHE_KYBER_RSA_WITH_AES_256_GCM_SHA384 },
  624. .key_exchange_alg = &s2n_hybrid_ecdhe_kem,
  625. .auth_method = S2N_AUTHENTICATION_RSA,
  626. .record_alg = NULL,
  627. .all_record_algs = { &s2n_record_alg_aes256_gcm },
  628. .num_record_algs = 1,
  629. .sslv3_record_alg = NULL,
  630. .prf_alg = S2N_HMAC_SHA384,
  631. .minimum_required_tls_version = S2N_TLS12,
  632. };
  633. struct s2n_cipher_suite s2n_tls13_aes_128_gcm_sha256 = {
  634. .available = 0,
  635. .name = "TLS_AES_128_GCM_SHA256",
  636. .iana_value = { TLS_AES_128_GCM_SHA256 },
  637. .key_exchange_alg = NULL,
  638. .auth_method = S2N_AUTHENTICATION_METHOD_TLS13,
  639. .record_alg = NULL,
  640. .all_record_algs = { &s2n_tls13_record_alg_aes128_gcm },
  641. .num_record_algs = 1,
  642. .sslv3_record_alg = NULL,
  643. .prf_alg = S2N_HMAC_SHA256,
  644. .minimum_required_tls_version = S2N_TLS13,
  645. };
  646. struct s2n_cipher_suite s2n_tls13_aes_256_gcm_sha384 = {
  647. .available = 0,
  648. .name = "TLS_AES_256_GCM_SHA384",
  649. .iana_value = { TLS_AES_256_GCM_SHA384 },
  650. .key_exchange_alg = NULL,
  651. .auth_method = S2N_AUTHENTICATION_METHOD_TLS13,
  652. .record_alg = NULL,
  653. .all_record_algs = { &s2n_tls13_record_alg_aes256_gcm },
  654. .num_record_algs = 1,
  655. .sslv3_record_alg = NULL,
  656. .prf_alg = S2N_HMAC_SHA384,
  657. .minimum_required_tls_version = S2N_TLS13,
  658. };
  659. struct s2n_cipher_suite s2n_tls13_chacha20_poly1305_sha256 = {
  660. .available = 0,
  661. .name = "TLS_CHACHA20_POLY1305_SHA256",
  662. .iana_value = { TLS_CHACHA20_POLY1305_SHA256 },
  663. .key_exchange_alg = NULL,
  664. .auth_method = S2N_AUTHENTICATION_METHOD_TLS13,
  665. .record_alg = NULL,
  666. .all_record_algs = { &s2n_tls13_record_alg_chacha20_poly1305 },
  667. .num_record_algs = 1,
  668. .sslv3_record_alg = NULL,
  669. .prf_alg = S2N_HMAC_SHA256,
  670. .minimum_required_tls_version = S2N_TLS13,
  671. };
  672. /* All of the cipher suites that s2n negotiates in order of IANA value.
  673. * New cipher suites MUST be added here, IN ORDER, or they will not be
  674. * properly initialized.
  675. */
  676. static struct s2n_cipher_suite *s2n_all_cipher_suites[] = {
  677. &s2n_rsa_with_rc4_128_md5, /* 0x00,0x04 */
  678. &s2n_rsa_with_rc4_128_sha, /* 0x00,0x05 */
  679. &s2n_rsa_with_3des_ede_cbc_sha, /* 0x00,0x0A */
  680. &s2n_dhe_rsa_with_3des_ede_cbc_sha, /* 0x00,0x16 */
  681. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  682. &s2n_dhe_rsa_with_aes_128_cbc_sha, /* 0x00,0x33 */
  683. &s2n_rsa_with_aes_256_cbc_sha, /* 0x00,0x35 */
  684. &s2n_dhe_rsa_with_aes_256_cbc_sha, /* 0x00,0x39 */
  685. &s2n_rsa_with_aes_128_cbc_sha256, /* 0x00,0x3C */
  686. &s2n_rsa_with_aes_256_cbc_sha256, /* 0x00,0x3D */
  687. &s2n_dhe_rsa_with_aes_128_cbc_sha256, /* 0x00,0x67 */
  688. &s2n_dhe_rsa_with_aes_256_cbc_sha256, /* 0x00,0x6B */
  689. &s2n_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9C */
  690. &s2n_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9D */
  691. &s2n_dhe_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9E */
  692. &s2n_dhe_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9F */
  693. &s2n_tls13_aes_128_gcm_sha256, /* 0x13,0x01 */
  694. &s2n_tls13_aes_256_gcm_sha384, /* 0x13,0x02 */
  695. &s2n_tls13_chacha20_poly1305_sha256, /* 0x13,0x03 */
  696. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha, /* 0xC0,0x09 */
  697. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha, /* 0xC0,0x0A */
  698. &s2n_ecdhe_rsa_with_rc4_128_sha, /* 0xC0,0x11 */
  699. &s2n_ecdhe_rsa_with_3des_ede_cbc_sha, /* 0xC0,0x12 */
  700. &s2n_ecdhe_rsa_with_aes_128_cbc_sha, /* 0xC0,0x13 */
  701. &s2n_ecdhe_rsa_with_aes_256_cbc_sha, /* 0xC0,0x14 */
  702. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256, /* 0xC0,0x23 */
  703. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384, /* 0xC0,0x24 */
  704. &s2n_ecdhe_rsa_with_aes_128_cbc_sha256, /* 0xC0,0x27 */
  705. &s2n_ecdhe_rsa_with_aes_256_cbc_sha384, /* 0xC0,0x28 */
  706. &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256, /* 0xC0,0x2B */
  707. &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384, /* 0xC0,0x2C */
  708. &s2n_ecdhe_rsa_with_aes_128_gcm_sha256, /* 0xC0,0x2F */
  709. &s2n_ecdhe_rsa_with_aes_256_gcm_sha384, /* 0xC0,0x30 */
  710. &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA8 */
  711. &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA9 */
  712. &s2n_dhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xAA */
  713. &s2n_ecdhe_kyber_rsa_with_aes_256_gcm_sha384, /* 0xFF,0x0C */
  714. };
  715. /* All supported ciphers. Exposed for integration testing. */
  716. const struct s2n_cipher_preferences cipher_preferences_test_all = {
  717. .count = s2n_array_len(s2n_all_cipher_suites),
  718. .suites = s2n_all_cipher_suites,
  719. };
  720. /* All TLS12 Cipher Suites */
  721. static struct s2n_cipher_suite *s2n_all_tls12_cipher_suites[] = {
  722. &s2n_rsa_with_rc4_128_md5, /* 0x00,0x04 */
  723. &s2n_rsa_with_rc4_128_sha, /* 0x00,0x05 */
  724. &s2n_rsa_with_3des_ede_cbc_sha, /* 0x00,0x0A */
  725. &s2n_dhe_rsa_with_3des_ede_cbc_sha, /* 0x00,0x16 */
  726. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  727. &s2n_dhe_rsa_with_aes_128_cbc_sha, /* 0x00,0x33 */
  728. &s2n_rsa_with_aes_256_cbc_sha, /* 0x00,0x35 */
  729. &s2n_dhe_rsa_with_aes_256_cbc_sha, /* 0x00,0x39 */
  730. &s2n_rsa_with_aes_128_cbc_sha256, /* 0x00,0x3C */
  731. &s2n_rsa_with_aes_256_cbc_sha256, /* 0x00,0x3D */
  732. &s2n_dhe_rsa_with_aes_128_cbc_sha256, /* 0x00,0x67 */
  733. &s2n_dhe_rsa_with_aes_256_cbc_sha256, /* 0x00,0x6B */
  734. &s2n_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9C */
  735. &s2n_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9D */
  736. &s2n_dhe_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9E */
  737. &s2n_dhe_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9F */
  738. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha, /* 0xC0,0x09 */
  739. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha, /* 0xC0,0x0A */
  740. &s2n_ecdhe_rsa_with_rc4_128_sha, /* 0xC0,0x11 */
  741. &s2n_ecdhe_rsa_with_3des_ede_cbc_sha, /* 0xC0,0x12 */
  742. &s2n_ecdhe_rsa_with_aes_128_cbc_sha, /* 0xC0,0x13 */
  743. &s2n_ecdhe_rsa_with_aes_256_cbc_sha, /* 0xC0,0x14 */
  744. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256, /* 0xC0,0x23 */
  745. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384, /* 0xC0,0x24 */
  746. &s2n_ecdhe_rsa_with_aes_128_cbc_sha256, /* 0xC0,0x27 */
  747. &s2n_ecdhe_rsa_with_aes_256_cbc_sha384, /* 0xC0,0x28 */
  748. &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256, /* 0xC0,0x2B */
  749. &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384, /* 0xC0,0x2C */
  750. &s2n_ecdhe_rsa_with_aes_128_gcm_sha256, /* 0xC0,0x2F */
  751. &s2n_ecdhe_rsa_with_aes_256_gcm_sha384, /* 0xC0,0x30 */
  752. &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA8 */
  753. &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA9 */
  754. &s2n_dhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xAA */
  755. &s2n_ecdhe_kyber_rsa_with_aes_256_gcm_sha384, /* 0xFF,0x0C */
  756. };
  757. const struct s2n_cipher_preferences cipher_preferences_test_all_tls12 = {
  758. .count = s2n_array_len(s2n_all_tls12_cipher_suites),
  759. .suites = s2n_all_tls12_cipher_suites,
  760. };
  761. /* All of the cipher suites that s2n can negotiate when in FIPS mode,
  762. * in order of IANA value. Exposed for the "test_all_fips" cipher preference list.
  763. */
  764. static struct s2n_cipher_suite *s2n_all_fips_cipher_suites[] = {
  765. &s2n_rsa_with_3des_ede_cbc_sha, /* 0x00,0x0A */
  766. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  767. &s2n_rsa_with_aes_256_cbc_sha, /* 0x00,0x35 */
  768. &s2n_rsa_with_aes_128_cbc_sha256, /* 0x00,0x3C */
  769. &s2n_rsa_with_aes_256_cbc_sha256, /* 0x00,0x3D */
  770. &s2n_dhe_rsa_with_aes_128_cbc_sha256, /* 0x00,0x67 */
  771. &s2n_dhe_rsa_with_aes_256_cbc_sha256, /* 0x00,0x6B */
  772. &s2n_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9C */
  773. &s2n_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9D */
  774. &s2n_dhe_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9E */
  775. &s2n_dhe_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9F */
  776. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256, /* 0xC0,0x23 */
  777. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384, /* 0xC0,0x24 */
  778. &s2n_ecdhe_rsa_with_aes_128_cbc_sha256, /* 0xC0,0x27 */
  779. &s2n_ecdhe_rsa_with_aes_256_cbc_sha384, /* 0xC0,0x28 */
  780. &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256, /* 0xC0,0x2B */
  781. &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384, /* 0xC0,0x2C */
  782. &s2n_ecdhe_rsa_with_aes_128_gcm_sha256, /* 0xC0,0x2F */
  783. &s2n_ecdhe_rsa_with_aes_256_gcm_sha384, /* 0xC0,0x30 */
  784. };
  785. /* All supported FIPS ciphers. Exposed for integration testing. */
  786. const struct s2n_cipher_preferences cipher_preferences_test_all_fips = {
  787. .count = s2n_array_len(s2n_all_fips_cipher_suites),
  788. .suites = s2n_all_fips_cipher_suites,
  789. };
  790. /* All of the ECDSA cipher suites that s2n can negotiate, in order of IANA
  791. * value. Exposed for the "test_all_ecdsa" cipher preference list.
  792. */
  793. static struct s2n_cipher_suite *s2n_all_ecdsa_cipher_suites[] = {
  794. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha, /* 0xC0,0x09 */
  795. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha, /* 0xC0,0x0A */
  796. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256, /* 0xC0,0x23 */
  797. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384, /* 0xC0,0x24 */
  798. &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256, /* 0xC0,0x2B */
  799. &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384, /* 0xC0,0x2C */
  800. &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA9 */
  801. };
  802. /* All supported ECDSA cipher suites. Exposed for integration testing. */
  803. const struct s2n_cipher_preferences cipher_preferences_test_all_ecdsa = {
  804. .count = s2n_array_len(s2n_all_ecdsa_cipher_suites),
  805. .suites = s2n_all_ecdsa_cipher_suites,
  806. };
  807. /* All cipher suites that uses RSA key exchange. Exposed for unit or integration tests. */
  808. static struct s2n_cipher_suite *s2n_all_rsa_kex_cipher_suites[] = {
  809. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  810. &s2n_rsa_with_rc4_128_md5, /* 0x00,0x04 */
  811. &s2n_rsa_with_rc4_128_sha, /* 0x00,0x05 */
  812. &s2n_rsa_with_3des_ede_cbc_sha, /* 0x00,0x0A */
  813. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  814. &s2n_rsa_with_aes_256_cbc_sha, /* 0x00,0x35 */
  815. &s2n_rsa_with_aes_128_cbc_sha256, /* 0x00,0x3C */
  816. &s2n_rsa_with_aes_256_cbc_sha256, /* 0x00,0x3D */
  817. &s2n_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9C */
  818. &s2n_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9D */
  819. };
  820. /* Cipher preferences with rsa key exchange. Exposed for unit and integration tests. */
  821. const struct s2n_cipher_preferences cipher_preferences_test_all_rsa_kex = {
  822. .count = s2n_array_len(s2n_all_rsa_kex_cipher_suites),
  823. .suites = s2n_all_rsa_kex_cipher_suites,
  824. };
  825. /* All ECDSA cipher suites first, then the rest of the supported ciphers that s2n can negotiate.
  826. * Exposed for the "test_ecdsa_priority" cipher preference list.
  827. */
  828. static struct s2n_cipher_suite *s2n_ecdsa_priority_cipher_suites[] = {
  829. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha, /* 0xC0,0x09 */
  830. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha, /* 0xC0,0x0A */
  831. &s2n_ecdhe_ecdsa_with_aes_128_cbc_sha256, /* 0xC0,0x23 */
  832. &s2n_ecdhe_ecdsa_with_aes_256_cbc_sha384, /* 0xC0,0x24 */
  833. &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256, /* 0xC0,0x2B */
  834. &s2n_ecdhe_ecdsa_with_aes_256_gcm_sha384, /* 0xC0,0x2C */
  835. &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA9 */
  836. &s2n_rsa_with_rc4_128_md5, /* 0x00,0x04 */
  837. &s2n_rsa_with_rc4_128_sha, /* 0x00,0x05 */
  838. &s2n_rsa_with_3des_ede_cbc_sha, /* 0x00,0x0A */
  839. &s2n_dhe_rsa_with_3des_ede_cbc_sha, /* 0x00,0x16 */
  840. &s2n_rsa_with_aes_128_cbc_sha, /* 0x00,0x2F */
  841. &s2n_dhe_rsa_with_aes_128_cbc_sha, /* 0x00,0x33 */
  842. &s2n_rsa_with_aes_256_cbc_sha, /* 0x00,0x35 */
  843. &s2n_dhe_rsa_with_aes_256_cbc_sha, /* 0x00,0x39 */
  844. &s2n_rsa_with_aes_128_cbc_sha256, /* 0x00,0x3C */
  845. &s2n_rsa_with_aes_256_cbc_sha256, /* 0x00,0x3D */
  846. &s2n_dhe_rsa_with_aes_128_cbc_sha256, /* 0x00,0x67 */
  847. &s2n_dhe_rsa_with_aes_256_cbc_sha256, /* 0x00,0x6B */
  848. &s2n_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9C */
  849. &s2n_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9D */
  850. &s2n_dhe_rsa_with_aes_128_gcm_sha256, /* 0x00,0x9E */
  851. &s2n_dhe_rsa_with_aes_256_gcm_sha384, /* 0x00,0x9F */
  852. &s2n_ecdhe_rsa_with_rc4_128_sha, /* 0xC0,0x11 */
  853. &s2n_ecdhe_rsa_with_3des_ede_cbc_sha, /* 0xC0,0x12 */
  854. &s2n_ecdhe_rsa_with_aes_128_cbc_sha, /* 0xC0,0x13 */
  855. &s2n_ecdhe_rsa_with_aes_256_cbc_sha, /* 0xC0,0x14 */
  856. &s2n_ecdhe_rsa_with_aes_128_cbc_sha256, /* 0xC0,0x27 */
  857. &s2n_ecdhe_rsa_with_aes_256_cbc_sha384, /* 0xC0,0x28 */
  858. &s2n_ecdhe_rsa_with_aes_128_gcm_sha256, /* 0xC0,0x2F */
  859. &s2n_ecdhe_rsa_with_aes_256_gcm_sha384, /* 0xC0,0x30 */
  860. &s2n_ecdhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xA8 */
  861. &s2n_dhe_rsa_with_chacha20_poly1305_sha256, /* 0xCC,0xAA */
  862. };
  863. /* All cipher suites, but with ECDSA priority. Exposed for integration testing. */
  864. const struct s2n_cipher_preferences cipher_preferences_test_ecdsa_priority = {
  865. .count = s2n_array_len(s2n_ecdsa_priority_cipher_suites),
  866. .suites = s2n_ecdsa_priority_cipher_suites,
  867. };
  868. static struct s2n_cipher_suite *s2n_all_tls13_cipher_suites[] = {
  869. &s2n_tls13_aes_128_gcm_sha256, /* 0x13,0x01 */
  870. &s2n_tls13_aes_256_gcm_sha384, /* 0x13,0x02 */
  871. &s2n_tls13_chacha20_poly1305_sha256, /* 0x13,0x03 */
  872. };
  873. const struct s2n_cipher_preferences cipher_preferences_test_all_tls13 = {
  874. .count = s2n_array_len(s2n_all_tls13_cipher_suites),
  875. .suites = s2n_all_tls13_cipher_suites,
  876. };
  877. static bool should_init_crypto = true;
  878. static bool crypto_initialized = false;
  879. int s2n_crypto_disable_init(void)
  880. {
  881. POSIX_ENSURE(!crypto_initialized, S2N_ERR_INITIALIZED);
  882. should_init_crypto = false;
  883. return S2N_SUCCESS;
  884. }
  885. /* Determines cipher suite availability and selects record algorithms */
  886. int s2n_cipher_suites_init(void)
  887. {
  888. const int num_cipher_suites = s2n_array_len(s2n_all_cipher_suites);
  889. for (int i = 0; i < num_cipher_suites; i++) {
  890. struct s2n_cipher_suite *cur_suite = s2n_all_cipher_suites[i];
  891. cur_suite->available = 0;
  892. cur_suite->record_alg = NULL;
  893. /* Find the highest priority supported record algorithm */
  894. for (int j = 0; j < cur_suite->num_record_algs; j++) {
  895. /* Can we use the record algorithm's cipher? Won't be available if the system CPU architecture
  896. * doesn't support it or if the libcrypto lacks the feature. All hmac_algs are supported.
  897. */
  898. if (cur_suite->all_record_algs[j]->cipher->is_available()) {
  899. /* Found a supported record algorithm. Use it. */
  900. cur_suite->available = 1;
  901. cur_suite->record_alg = cur_suite->all_record_algs[j];
  902. break;
  903. }
  904. }
  905. /* Mark PQ cipher suites as unavailable if PQ is disabled */
  906. if (s2n_kex_includes(cur_suite->key_exchange_alg, &s2n_kem) && !s2n_pq_is_enabled()) {
  907. cur_suite->available = 0;
  908. cur_suite->record_alg = NULL;
  909. }
  910. /* Initialize SSLv3 cipher suite if SSLv3 utilizes a different record algorithm */
  911. if (cur_suite->sslv3_record_alg && cur_suite->sslv3_record_alg->cipher->is_available()) {
  912. struct s2n_blob cur_suite_mem = { 0 };
  913. POSIX_GUARD(s2n_blob_init(&cur_suite_mem, (uint8_t *) cur_suite, sizeof(struct s2n_cipher_suite)));
  914. struct s2n_blob new_suite_mem = { 0 };
  915. POSIX_GUARD(s2n_dup(&cur_suite_mem, &new_suite_mem));
  916. struct s2n_cipher_suite *new_suite = (struct s2n_cipher_suite *) (void *) new_suite_mem.data;
  917. new_suite->available = 1;
  918. new_suite->record_alg = cur_suite->sslv3_record_alg;
  919. cur_suite->sslv3_cipher_suite = new_suite;
  920. } else {
  921. cur_suite->sslv3_cipher_suite = cur_suite;
  922. }
  923. }
  924. if (should_init_crypto) {
  925. #if !S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0)
  926. /*https://wiki.openssl.org/index.php/Manual:OpenSSL_add_all_algorithms(3)*/
  927. OpenSSL_add_all_algorithms();
  928. #else
  929. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  930. #endif
  931. }
  932. crypto_initialized = true;
  933. return S2N_SUCCESS;
  934. }
  935. /* Reset any selected record algorithms */
  936. S2N_RESULT s2n_cipher_suites_cleanup(void)
  937. {
  938. const int num_cipher_suites = sizeof(s2n_all_cipher_suites) / sizeof(struct s2n_cipher_suite *);
  939. for (int i = 0; i < num_cipher_suites; i++) {
  940. struct s2n_cipher_suite *cur_suite = s2n_all_cipher_suites[i];
  941. cur_suite->available = 0;
  942. cur_suite->record_alg = NULL;
  943. /* Release custom SSLv3 cipher suites */
  944. if (cur_suite->sslv3_cipher_suite != cur_suite) {
  945. RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &cur_suite->sslv3_cipher_suite, sizeof(struct s2n_cipher_suite)));
  946. }
  947. cur_suite->sslv3_cipher_suite = NULL;
  948. }
  949. if (should_init_crypto) {
  950. #if !S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0)
  951. /*https://wiki.openssl.org/index.php/Manual:OpenSSL_add_all_algorithms(3)*/
  952. EVP_cleanup();
  953. /* per the reqs here https://www.openssl.org/docs/man1.1.0/crypto/OPENSSL_init_crypto.html we don't explicitly call
  954. * cleanup in later versions */
  955. #endif
  956. }
  957. return S2N_RESULT_OK;
  958. }
  959. S2N_RESULT s2n_cipher_suite_from_iana(const uint8_t *iana, size_t iana_len, struct s2n_cipher_suite **cipher_suite)
  960. {
  961. RESULT_ENSURE_REF(cipher_suite);
  962. *cipher_suite = NULL;
  963. RESULT_ENSURE_REF(iana);
  964. RESULT_ENSURE_EQ(iana_len, S2N_TLS_CIPHER_SUITE_LEN);
  965. int low = 0;
  966. int top = s2n_array_len(s2n_all_cipher_suites) - 1;
  967. /* Perform a textbook binary search */
  968. while (low <= top) {
  969. /* Check in the middle */
  970. size_t mid = low + ((top - low) / 2);
  971. int m = memcmp(s2n_all_cipher_suites[mid]->iana_value, iana, S2N_TLS_CIPHER_SUITE_LEN);
  972. if (m == 0) {
  973. *cipher_suite = s2n_all_cipher_suites[mid];
  974. return S2N_RESULT_OK;
  975. } else if (m > 0) {
  976. top = mid - 1;
  977. } else if (m < 0) {
  978. low = mid + 1;
  979. }
  980. }
  981. RESULT_BAIL(S2N_ERR_CIPHER_NOT_SUPPORTED);
  982. }
  983. int s2n_set_cipher_as_client(struct s2n_connection *conn, uint8_t wire[S2N_TLS_CIPHER_SUITE_LEN])
  984. {
  985. POSIX_ENSURE_REF(conn);
  986. POSIX_ENSURE_REF(conn->secure);
  987. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  988. const struct s2n_security_policy *security_policy;
  989. POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
  990. POSIX_ENSURE_REF(security_policy);
  991. /**
  992. * Ensure that the wire cipher suite is contained in the security
  993. * policy, and thus was offered by the client.
  994. *
  995. *= https://tools.ietf.org/rfc/rfc8446#4.1.3
  996. *# A client which receives a
  997. *# cipher suite that was not offered MUST abort the handshake with an
  998. *# "illegal_parameter" alert.
  999. *
  1000. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  1001. *# A client which receives a cipher suite that was not offered MUST
  1002. *# abort the handshake.
  1003. *
  1004. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  1005. *# Upon receipt of a HelloRetryRequest, the client MUST check the
  1006. *# legacy_version, legacy_session_id_echo, cipher_suite
  1007. **/
  1008. struct s2n_cipher_suite *cipher_suite = NULL;
  1009. for (size_t i = 0; i < security_policy->cipher_preferences->count; i++) {
  1010. const uint8_t *ours = security_policy->cipher_preferences->suites[i]->iana_value;
  1011. if (memcmp(wire, ours, S2N_TLS_CIPHER_SUITE_LEN) == 0) {
  1012. cipher_suite = security_policy->cipher_preferences->suites[i];
  1013. break;
  1014. }
  1015. }
  1016. POSIX_ENSURE(cipher_suite != NULL, S2N_ERR_CIPHER_NOT_SUPPORTED);
  1017. POSIX_ENSURE(cipher_suite->available, S2N_ERR_CIPHER_NOT_SUPPORTED);
  1018. /** Clients MUST verify
  1019. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  1020. *# that the server selected a cipher suite
  1021. *# indicating a Hash associated with the PSK
  1022. **/
  1023. if (conn->psk_params.chosen_psk) {
  1024. POSIX_ENSURE(cipher_suite->prf_alg == conn->psk_params.chosen_psk->hmac_alg,
  1025. S2N_ERR_CIPHER_NOT_SUPPORTED);
  1026. }
  1027. /**
  1028. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  1029. *# Upon receiving
  1030. *# the ServerHello, clients MUST check that the cipher suite supplied in
  1031. *# the ServerHello is the same as that in the HelloRetryRequest and
  1032. *# otherwise abort the handshake with an "illegal_parameter" alert.
  1033. **/
  1034. if (s2n_is_hello_retry_handshake(conn) && !s2n_is_hello_retry_message(conn)) {
  1035. POSIX_ENSURE(conn->secure->cipher_suite->iana_value == cipher_suite->iana_value, S2N_ERR_CIPHER_NOT_SUPPORTED);
  1036. return S2N_SUCCESS;
  1037. }
  1038. conn->secure->cipher_suite = cipher_suite;
  1039. /* For SSLv3 use SSLv3-specific ciphers */
  1040. if (conn->actual_protocol_version == S2N_SSLv3) {
  1041. conn->secure->cipher_suite = conn->secure->cipher_suite->sslv3_cipher_suite;
  1042. POSIX_ENSURE_REF(conn->secure->cipher_suite);
  1043. }
  1044. return 0;
  1045. }
  1046. static int s2n_wire_ciphers_contain(const uint8_t *match, const uint8_t *wire, uint32_t count, uint32_t cipher_suite_len)
  1047. {
  1048. for (size_t i = 0; i < count; i++) {
  1049. const uint8_t *theirs = wire + (i * cipher_suite_len) + (cipher_suite_len - S2N_TLS_CIPHER_SUITE_LEN);
  1050. if (!memcmp(match, theirs, S2N_TLS_CIPHER_SUITE_LEN)) {
  1051. return 1;
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. bool s2n_cipher_suite_uses_chacha20_alg(struct s2n_cipher_suite *cipher_suite)
  1057. {
  1058. return cipher_suite && cipher_suite->record_alg && cipher_suite->record_alg->cipher == &s2n_chacha20_poly1305;
  1059. }
  1060. /* Iff the server has enabled allow_chacha20_boosting and the client has a chacha20 cipher suite as its most
  1061. * preferred cipher suite, then we have mutual chacha20 boosting support.
  1062. */
  1063. static S2N_RESULT s2n_validate_chacha20_boosting(const struct s2n_cipher_preferences *cipher_preferences, const uint8_t *wire,
  1064. uint32_t cipher_suite_len)
  1065. {
  1066. RESULT_ENSURE_REF(cipher_preferences);
  1067. RESULT_ENSURE_REF(wire);
  1068. RESULT_ENSURE_EQ(cipher_preferences->allow_chacha20_boosting, true);
  1069. const uint8_t *clients_first_cipher_iana = wire + cipher_suite_len - S2N_TLS_CIPHER_SUITE_LEN;
  1070. struct s2n_cipher_suite *client_first_cipher_suite = NULL;
  1071. RESULT_GUARD(s2n_cipher_suite_from_iana(clients_first_cipher_iana, S2N_TLS_CIPHER_SUITE_LEN, &client_first_cipher_suite));
  1072. RESULT_ENSURE_REF(client_first_cipher_suite);
  1073. RESULT_ENSURE_EQ(s2n_cipher_suite_uses_chacha20_alg(client_first_cipher_suite), true);
  1074. return S2N_RESULT_OK;
  1075. }
  1076. static int s2n_set_cipher_as_server(struct s2n_connection *conn, uint8_t *wire, uint32_t count, uint32_t cipher_suite_len)
  1077. {
  1078. POSIX_ENSURE_REF(conn);
  1079. POSIX_ENSURE_REF(conn->secure);
  1080. uint8_t renegotiation_info_scsv[S2N_TLS_CIPHER_SUITE_LEN] = { TLS_EMPTY_RENEGOTIATION_INFO_SCSV };
  1081. struct s2n_cipher_suite *higher_vers_match = NULL;
  1082. struct s2n_cipher_suite *non_chacha20_match = NULL;
  1083. /* RFC 7507 - If client is attempting to negotiate a TLS Version that is lower than the highest supported server
  1084. * version, and the client cipher list contains TLS_FALLBACK_SCSV, then the server must abort the connection since
  1085. * TLS_FALLBACK_SCSV should only be present when the client previously failed to negotiate a higher TLS version.
  1086. */
  1087. if (conn->client_protocol_version < conn->server_protocol_version) {
  1088. uint8_t fallback_scsv[S2N_TLS_CIPHER_SUITE_LEN] = { TLS_FALLBACK_SCSV };
  1089. if (s2n_wire_ciphers_contain(fallback_scsv, wire, count, cipher_suite_len)) {
  1090. POSIX_BAIL(S2N_ERR_FALLBACK_DETECTED);
  1091. }
  1092. }
  1093. if (s2n_wire_ciphers_contain(renegotiation_info_scsv, wire, count, cipher_suite_len)) {
  1094. /** For renegotiation handshakes:
  1095. *= https://tools.ietf.org/rfc/rfc5746#3.7
  1096. *# o When a ClientHello is received, the server MUST verify that it
  1097. *# does not contain the TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV. If
  1098. *# the SCSV is present, the server MUST abort the handshake.
  1099. */
  1100. POSIX_ENSURE(!s2n_handshake_is_renegotiation(conn), S2N_ERR_BAD_MESSAGE);
  1101. /** For initial handshakes:
  1102. *= https://tools.ietf.org/rfc/rfc5746#3.6
  1103. *# o When a ClientHello is received, the server MUST check if it
  1104. *# includes the TLS_EMPTY_RENEGOTIATION_INFO_SCSV SCSV. If it does,
  1105. *# set the secure_renegotiation flag to TRUE.
  1106. */
  1107. conn->secure_renegotiation = 1;
  1108. }
  1109. const struct s2n_security_policy *security_policy;
  1110. POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
  1111. const struct s2n_cipher_preferences *cipher_preferences = security_policy->cipher_preferences;
  1112. POSIX_ENSURE_REF(cipher_preferences);
  1113. bool try_chacha20_boosting = s2n_result_is_ok(s2n_validate_chacha20_boosting(cipher_preferences, wire, cipher_suite_len));
  1114. /*
  1115. * s2n only respects server preference order and chooses the server's
  1116. * most preferred mutually supported cipher suite.
  1117. *
  1118. * If chacha20 boosting is enabled, we prefer chacha20 cipher suites over all
  1119. * other cipher suites.
  1120. *
  1121. * If no mutually supported cipher suites are found, we choose one with a version
  1122. * too high for the current connection (higher_vers_match).
  1123. */
  1124. for (size_t i = 0; i < cipher_preferences->count; i++) {
  1125. const uint8_t *ours = cipher_preferences->suites[i]->iana_value;
  1126. if (s2n_wire_ciphers_contain(ours, wire, count, cipher_suite_len)) {
  1127. /* We have a match */
  1128. struct s2n_cipher_suite *match = cipher_preferences->suites[i];
  1129. /* Never use TLS1.3 ciphers on a pre-TLS1.3 connection, and vice versa */
  1130. if ((conn->actual_protocol_version >= S2N_TLS13) != (match->minimum_required_tls_version >= S2N_TLS13)) {
  1131. continue;
  1132. }
  1133. /* If connection is for SSLv3, use SSLv3 version of suites */
  1134. if (conn->actual_protocol_version == S2N_SSLv3) {
  1135. match = match->sslv3_cipher_suite;
  1136. }
  1137. /* Skip the suite if we don't have an available implementation */
  1138. if (!match->available) {
  1139. continue;
  1140. }
  1141. /* Make sure the cipher is valid for available certs */
  1142. if (s2n_is_cipher_suite_valid_for_auth(conn, match) != S2N_SUCCESS) {
  1143. continue;
  1144. }
  1145. /* TLS 1.3 does not include key exchange in cipher suites */
  1146. if (match->minimum_required_tls_version < S2N_TLS13) {
  1147. /* If the kex is not supported continue to the next candidate */
  1148. bool kex_supported = false;
  1149. POSIX_GUARD_RESULT(s2n_kex_supported(match, conn, &kex_supported));
  1150. if (!kex_supported) {
  1151. continue;
  1152. }
  1153. /* If the kex is not configured correctly continue to the next candidate */
  1154. if (s2n_result_is_error(s2n_configure_kex(match, conn))) {
  1155. continue;
  1156. }
  1157. }
  1158. /**
  1159. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.11
  1160. *# The server MUST ensure that it selects a compatible PSK
  1161. *# (if any) and cipher suite.
  1162. **/
  1163. if (conn->psk_params.chosen_psk != NULL) {
  1164. if (match->prf_alg != conn->psk_params.chosen_psk->hmac_alg) {
  1165. continue;
  1166. }
  1167. }
  1168. /* Don't immediately choose a cipher the connection shouldn't be able to support */
  1169. if (conn->actual_protocol_version < match->minimum_required_tls_version) {
  1170. if (!higher_vers_match) {
  1171. higher_vers_match = match;
  1172. }
  1173. continue;
  1174. }
  1175. /* The server and client have chacha20 boosting support enabled AND the server identified a negotiable match */
  1176. if (try_chacha20_boosting) {
  1177. if (s2n_cipher_suite_uses_chacha20_alg(match)) {
  1178. conn->secure->cipher_suite = match;
  1179. return S2N_SUCCESS;
  1180. }
  1181. /* Save the valid non-chacha20 match in case no valid chacha20 match is found */
  1182. if (!non_chacha20_match) {
  1183. non_chacha20_match = match;
  1184. }
  1185. continue;
  1186. }
  1187. conn->secure->cipher_suite = match;
  1188. return S2N_SUCCESS;
  1189. }
  1190. }
  1191. if (non_chacha20_match) {
  1192. conn->secure->cipher_suite = non_chacha20_match;
  1193. return S2N_SUCCESS;
  1194. }
  1195. /* Settle for a cipher with a higher required proto version, if it was set */
  1196. if (higher_vers_match) {
  1197. conn->secure->cipher_suite = higher_vers_match;
  1198. return S2N_SUCCESS;
  1199. }
  1200. POSIX_BAIL(S2N_ERR_CIPHER_NOT_SUPPORTED);
  1201. }
  1202. int s2n_set_cipher_as_sslv2_server(struct s2n_connection *conn, uint8_t *wire, uint16_t count)
  1203. {
  1204. return s2n_set_cipher_as_server(conn, wire, count, S2N_SSLv2_CIPHER_SUITE_LEN);
  1205. }
  1206. int s2n_set_cipher_as_tls_server(struct s2n_connection *conn, uint8_t *wire, uint16_t count)
  1207. {
  1208. return s2n_set_cipher_as_server(conn, wire, count, S2N_TLS_CIPHER_SUITE_LEN);
  1209. }
  1210. bool s2n_cipher_suite_requires_ecc_extension(struct s2n_cipher_suite *cipher)
  1211. {
  1212. if (!cipher) {
  1213. return false;
  1214. }
  1215. /* TLS1.3 does not include key exchange algorithms in its cipher suites,
  1216. * but the elliptic curves extension is always required. */
  1217. if (cipher->minimum_required_tls_version >= S2N_TLS13) {
  1218. return true;
  1219. }
  1220. if (s2n_kex_includes(cipher->key_exchange_alg, &s2n_ecdhe)) {
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. bool s2n_cipher_suite_requires_pq_extension(struct s2n_cipher_suite *cipher)
  1226. {
  1227. if (!cipher) {
  1228. return false;
  1229. }
  1230. if (s2n_kex_includes(cipher->key_exchange_alg, &s2n_kem)) {
  1231. return true;
  1232. }
  1233. return false;
  1234. }