s2n_handshake_io.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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 <errno.h>
  16. #include <sys/param.h>
  17. #include "api/s2n.h"
  18. #include "crypto/s2n_fips.h"
  19. #include "error/s2n_errno.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #include "tls/s2n_alerts.h"
  22. #include "tls/s2n_async_pkey.h"
  23. #include "tls/s2n_cipher_suites.h"
  24. #include "tls/s2n_connection.h"
  25. #include "tls/s2n_kex.h"
  26. #include "tls/s2n_post_handshake.h"
  27. #include "tls/s2n_record.h"
  28. #include "tls/s2n_resume.h"
  29. #include "tls/s2n_tls.h"
  30. #include "tls/s2n_tls13.h"
  31. #include "tls/s2n_tls13_handshake.h"
  32. #include "tls/s2n_tls13_key_schedule.h"
  33. #include "utils/s2n_bitmap.h"
  34. #include "utils/s2n_random.h"
  35. #include "utils/s2n_safety.h"
  36. #include "utils/s2n_socket.h"
  37. /* clang-format off */
  38. struct s2n_handshake_action {
  39. uint8_t record_type;
  40. uint8_t message_type;
  41. char writer; /* 'S' or 'C' for server or client, 'B' for both */
  42. int (*handler[2]) (struct s2n_connection * conn);
  43. };
  44. static int s2n_always_fail_send(struct s2n_connection *conn)
  45. {
  46. /* This state should never be sending a handshake message. */
  47. POSIX_BAIL(S2N_ERR_HANDSHAKE_UNREACHABLE);
  48. }
  49. static int s2n_always_fail_recv(struct s2n_connection *conn)
  50. {
  51. /* This state should never have an incoming handshake message. */
  52. POSIX_BAIL(S2N_ERR_HANDSHAKE_UNREACHABLE);
  53. }
  54. /* Client and Server handlers for each message type we support.
  55. * See http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-7 for the list of handshake message types
  56. */
  57. static struct s2n_handshake_action state_machine[] = {
  58. /* message_type_t = {Record type Message type Writer S2N_SERVER S2N_CLIENT } */
  59. [CLIENT_HELLO] = {TLS_HANDSHAKE, TLS_CLIENT_HELLO, 'C', {s2n_establish_session, s2n_client_hello_send}},
  60. [SERVER_HELLO] = {TLS_HANDSHAKE, TLS_SERVER_HELLO, 'S', {s2n_server_hello_send, s2n_server_hello_recv}},
  61. [SERVER_NEW_SESSION_TICKET] = {TLS_HANDSHAKE, TLS_SERVER_NEW_SESSION_TICKET,'S', {s2n_server_nst_send, s2n_server_nst_recv}},
  62. [SERVER_CERT] = {TLS_HANDSHAKE, TLS_CERTIFICATE, 'S', {s2n_server_cert_send, s2n_server_cert_recv}},
  63. [SERVER_CERT_STATUS] = {TLS_HANDSHAKE, TLS_SERVER_CERT_STATUS, 'S', {s2n_server_status_send, s2n_server_status_recv}},
  64. [SERVER_KEY] = {TLS_HANDSHAKE, TLS_SERVER_KEY, 'S', {s2n_server_key_send, s2n_server_key_recv}},
  65. [SERVER_CERT_REQ] = {TLS_HANDSHAKE, TLS_CERT_REQ, 'S', {s2n_cert_req_send, s2n_cert_req_recv}},
  66. [SERVER_HELLO_DONE] = {TLS_HANDSHAKE, TLS_SERVER_HELLO_DONE, 'S', {s2n_server_done_send, s2n_server_done_recv}},
  67. [CLIENT_CERT] = {TLS_HANDSHAKE, TLS_CERTIFICATE, 'C', {s2n_client_cert_recv, s2n_client_cert_send}},
  68. [CLIENT_KEY] = {TLS_HANDSHAKE, TLS_CLIENT_KEY, 'C', {s2n_client_key_recv, s2n_client_key_send}},
  69. [CLIENT_CERT_VERIFY] = {TLS_HANDSHAKE, TLS_CERT_VERIFY, 'C', {s2n_client_cert_verify_recv, s2n_client_cert_verify_send}},
  70. [CLIENT_CHANGE_CIPHER_SPEC] = {TLS_CHANGE_CIPHER_SPEC, 0, 'C', {s2n_client_ccs_recv, s2n_ccs_send}},
  71. [CLIENT_NPN] = {TLS_HANDSHAKE, TLS_NPN, 'C', {s2n_next_protocol_recv, s2n_next_protocol_send}},
  72. [CLIENT_FINISHED] = {TLS_HANDSHAKE, TLS_FINISHED, 'C', {s2n_client_finished_recv, s2n_client_finished_send}},
  73. [SERVER_CHANGE_CIPHER_SPEC] = {TLS_CHANGE_CIPHER_SPEC, 0, 'S', {s2n_ccs_send, s2n_server_ccs_recv}},
  74. [SERVER_FINISHED] = {TLS_HANDSHAKE, TLS_FINISHED, 'S', {s2n_server_finished_send, s2n_server_finished_recv}},
  75. [APPLICATION_DATA] = {TLS_APPLICATION_DATA, 0, 'B', {s2n_always_fail_send, s2n_always_fail_recv}},
  76. };
  77. /*
  78. * Client and Server handlers for TLS1.3.
  79. */
  80. static struct s2n_handshake_action tls13_state_machine[] = {
  81. /* message_type_t = {Record type, Message type, Writer, {Server handler, client handler} } */
  82. [CLIENT_HELLO] = {TLS_HANDSHAKE, TLS_CLIENT_HELLO, 'C', {s2n_establish_session, s2n_client_hello_send}},
  83. [SERVER_HELLO] = {TLS_HANDSHAKE, TLS_SERVER_HELLO, 'S', {s2n_server_hello_send, s2n_server_hello_recv}},
  84. [HELLO_RETRY_MSG] = {TLS_HANDSHAKE, TLS_SERVER_HELLO, 'S', {s2n_server_hello_retry_send, s2n_server_hello_retry_recv}},
  85. [ENCRYPTED_EXTENSIONS] = {TLS_HANDSHAKE, TLS_ENCRYPTED_EXTENSIONS, 'S', {s2n_encrypted_extensions_send, s2n_encrypted_extensions_recv}},
  86. [SERVER_CERT_REQ] = {TLS_HANDSHAKE, TLS_CERT_REQ, 'S', {s2n_tls13_cert_req_send, s2n_tls13_cert_req_recv}},
  87. [SERVER_CERT] = {TLS_HANDSHAKE, TLS_CERTIFICATE, 'S', {s2n_server_cert_send, s2n_server_cert_recv}},
  88. [SERVER_CERT_VERIFY] = {TLS_HANDSHAKE, TLS_CERT_VERIFY, 'S', {s2n_tls13_cert_verify_send, s2n_tls13_cert_verify_recv}},
  89. [SERVER_FINISHED] = {TLS_HANDSHAKE, TLS_FINISHED, 'S', {s2n_tls13_server_finished_send, s2n_tls13_server_finished_recv}},
  90. [CLIENT_CERT] = {TLS_HANDSHAKE, TLS_CERTIFICATE, 'C', {s2n_client_cert_recv, s2n_client_cert_send}},
  91. [CLIENT_CERT_VERIFY] = {TLS_HANDSHAKE, TLS_CERT_VERIFY, 'C', {s2n_tls13_cert_verify_recv, s2n_tls13_cert_verify_send}},
  92. [CLIENT_FINISHED] = {TLS_HANDSHAKE, TLS_FINISHED, 'C', {s2n_tls13_client_finished_recv, s2n_tls13_client_finished_send}},
  93. [END_OF_EARLY_DATA] = {TLS_HANDSHAKE, TLS_END_OF_EARLY_DATA, 'C', {s2n_end_of_early_data_recv, s2n_end_of_early_data_send}},
  94. /* Not used by TLS1.3, except to maintain middlebox compatibility */
  95. [CLIENT_CHANGE_CIPHER_SPEC] = {TLS_CHANGE_CIPHER_SPEC, 0, 'C', {s2n_basic_ccs_recv, s2n_ccs_send}},
  96. [SERVER_CHANGE_CIPHER_SPEC] = {TLS_CHANGE_CIPHER_SPEC, 0, 'S', {s2n_ccs_send, s2n_basic_ccs_recv}},
  97. [APPLICATION_DATA] = {TLS_APPLICATION_DATA, 0, 'B', {s2n_always_fail_send, s2n_always_fail_recv}},
  98. };
  99. #define MESSAGE_NAME_ENTRY(msg) [msg] = #msg
  100. static const char *message_names[] = {
  101. MESSAGE_NAME_ENTRY(CLIENT_HELLO),
  102. MESSAGE_NAME_ENTRY(SERVER_HELLO),
  103. MESSAGE_NAME_ENTRY(ENCRYPTED_EXTENSIONS),
  104. MESSAGE_NAME_ENTRY(SERVER_NEW_SESSION_TICKET),
  105. MESSAGE_NAME_ENTRY(SERVER_CERT),
  106. MESSAGE_NAME_ENTRY(SERVER_CERT_STATUS),
  107. MESSAGE_NAME_ENTRY(SERVER_CERT_VERIFY),
  108. MESSAGE_NAME_ENTRY(SERVER_KEY),
  109. MESSAGE_NAME_ENTRY(SERVER_CERT_REQ),
  110. MESSAGE_NAME_ENTRY(SERVER_HELLO_DONE),
  111. MESSAGE_NAME_ENTRY(CLIENT_CERT),
  112. MESSAGE_NAME_ENTRY(CLIENT_KEY),
  113. MESSAGE_NAME_ENTRY(CLIENT_CERT_VERIFY),
  114. MESSAGE_NAME_ENTRY(CLIENT_CHANGE_CIPHER_SPEC),
  115. MESSAGE_NAME_ENTRY(CLIENT_FINISHED),
  116. MESSAGE_NAME_ENTRY(SERVER_CHANGE_CIPHER_SPEC),
  117. MESSAGE_NAME_ENTRY(SERVER_FINISHED),
  118. MESSAGE_NAME_ENTRY(HELLO_RETRY_MSG),
  119. MESSAGE_NAME_ENTRY(END_OF_EARLY_DATA),
  120. MESSAGE_NAME_ENTRY(APPLICATION_DATA),
  121. MESSAGE_NAME_ENTRY(CLIENT_NPN),
  122. };
  123. /* Maximum number of messages in a handshake */
  124. #define S2N_MAX_HANDSHAKE_LENGTH 32
  125. /* We support different ordering of TLS Handshake messages, depending on what is being negotiated. There's also a dummy "INITIAL" handshake
  126. * that everything starts out as until we know better.
  127. */
  128. static message_type_t handshakes[S2N_HANDSHAKES_COUNT][S2N_MAX_HANDSHAKE_LENGTH] = {
  129. [INITIAL] = {
  130. CLIENT_HELLO,
  131. SERVER_HELLO
  132. },
  133. [NEGOTIATED] = {
  134. CLIENT_HELLO,
  135. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  136. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  137. APPLICATION_DATA
  138. },
  139. [NEGOTIATED | WITH_NPN ] = {
  140. CLIENT_HELLO,
  141. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  142. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  143. APPLICATION_DATA
  144. },
  145. [NEGOTIATED | WITH_SESSION_TICKET ] = {
  146. CLIENT_HELLO,
  147. SERVER_HELLO, SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  148. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  149. APPLICATION_DATA},
  150. [NEGOTIATED | WITH_SESSION_TICKET | WITH_NPN ] = {
  151. CLIENT_HELLO,
  152. SERVER_HELLO, SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  153. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  154. APPLICATION_DATA},
  155. [NEGOTIATED | FULL_HANDSHAKE ] = {
  156. CLIENT_HELLO,
  157. SERVER_HELLO, SERVER_CERT, SERVER_HELLO_DONE,
  158. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  159. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  160. APPLICATION_DATA
  161. },
  162. [NEGOTIATED | FULL_HANDSHAKE | WITH_NPN ] = {
  163. CLIENT_HELLO,
  164. SERVER_HELLO, SERVER_CERT, SERVER_HELLO_DONE,
  165. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  166. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  167. APPLICATION_DATA
  168. },
  169. [NEGOTIATED | FULL_HANDSHAKE | WITH_SESSION_TICKET ] = {
  170. CLIENT_HELLO,
  171. SERVER_HELLO, SERVER_CERT, SERVER_HELLO_DONE,
  172. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  173. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  174. APPLICATION_DATA
  175. },
  176. [NEGOTIATED | FULL_HANDSHAKE | WITH_SESSION_TICKET | WITH_NPN ] = {
  177. CLIENT_HELLO,
  178. SERVER_HELLO, SERVER_CERT, SERVER_HELLO_DONE,
  179. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  180. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  181. APPLICATION_DATA
  182. },
  183. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY ] = {
  184. CLIENT_HELLO,
  185. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_HELLO_DONE,
  186. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  187. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  188. APPLICATION_DATA
  189. },
  190. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | WITH_NPN ] = {
  191. CLIENT_HELLO,
  192. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_HELLO_DONE,
  193. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  194. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  195. APPLICATION_DATA
  196. },
  197. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | WITH_SESSION_TICKET ] = {
  198. CLIENT_HELLO,
  199. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_HELLO_DONE,
  200. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  201. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  202. APPLICATION_DATA
  203. },
  204. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | WITH_SESSION_TICKET | WITH_NPN ] = {
  205. CLIENT_HELLO,
  206. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_HELLO_DONE,
  207. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  208. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  209. APPLICATION_DATA
  210. },
  211. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS ] ={
  212. CLIENT_HELLO,
  213. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_HELLO_DONE,
  214. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  215. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  216. APPLICATION_DATA
  217. },
  218. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | WITH_NPN ] ={
  219. CLIENT_HELLO,
  220. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_HELLO_DONE,
  221. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  222. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  223. APPLICATION_DATA
  224. },
  225. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | WITH_SESSION_TICKET ] = {
  226. CLIENT_HELLO,
  227. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_HELLO_DONE,
  228. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  229. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  230. APPLICATION_DATA
  231. },
  232. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | WITH_SESSION_TICKET | WITH_NPN ] = {
  233. CLIENT_HELLO,
  234. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_HELLO_DONE,
  235. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  236. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  237. APPLICATION_DATA
  238. },
  239. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS ] = {
  240. CLIENT_HELLO,
  241. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_HELLO_DONE,
  242. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  243. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  244. APPLICATION_DATA
  245. },
  246. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_NPN ] = {
  247. CLIENT_HELLO,
  248. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_HELLO_DONE,
  249. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  250. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  251. APPLICATION_DATA
  252. },
  253. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_SESSION_TICKET ] ={
  254. CLIENT_HELLO,
  255. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_HELLO_DONE,
  256. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  257. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  258. APPLICATION_DATA
  259. },
  260. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | WITH_SESSION_TICKET | WITH_NPN ] ={
  261. CLIENT_HELLO,
  262. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_HELLO_DONE,
  263. CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  264. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  265. APPLICATION_DATA
  266. },
  267. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH] = {
  268. CLIENT_HELLO,
  269. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  270. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  271. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  272. APPLICATION_DATA
  273. },
  274. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH| WITH_NPN ] = {
  275. CLIENT_HELLO,
  276. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  277. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  278. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  279. APPLICATION_DATA
  280. },
  281. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT ] = {
  282. CLIENT_HELLO,
  283. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  284. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  285. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  286. APPLICATION_DATA
  287. },
  288. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | WITH_NPN ] = {
  289. CLIENT_HELLO,
  290. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  291. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  292. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  293. APPLICATION_DATA
  294. },
  295. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | WITH_SESSION_TICKET] = {
  296. CLIENT_HELLO,
  297. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  298. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  299. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  300. APPLICATION_DATA
  301. },
  302. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN ] = {
  303. CLIENT_HELLO,
  304. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  305. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  306. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  307. APPLICATION_DATA
  308. },
  309. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET ] = {
  310. CLIENT_HELLO,
  311. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  312. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  313. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  314. APPLICATION_DATA
  315. },
  316. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET | WITH_NPN ] = {
  317. CLIENT_HELLO,
  318. SERVER_HELLO, SERVER_CERT, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  319. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  320. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  321. APPLICATION_DATA
  322. },
  323. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH] = {
  324. CLIENT_HELLO,
  325. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  326. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  327. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  328. APPLICATION_DATA
  329. },
  330. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | WITH_NPN ] = {
  331. CLIENT_HELLO,
  332. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  333. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  334. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  335. APPLICATION_DATA
  336. },
  337. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | NO_CLIENT_CERT ] = {
  338. CLIENT_HELLO,
  339. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  340. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  341. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  342. APPLICATION_DATA
  343. },
  344. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | NO_CLIENT_CERT | WITH_NPN ] = {
  345. CLIENT_HELLO,
  346. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  347. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  348. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  349. APPLICATION_DATA
  350. },
  351. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | WITH_SESSION_TICKET] = {
  352. CLIENT_HELLO,
  353. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  354. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  355. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  356. APPLICATION_DATA
  357. },
  358. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN ] = {
  359. CLIENT_HELLO,
  360. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  361. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  362. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  363. APPLICATION_DATA
  364. },
  365. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET ] = {
  366. CLIENT_HELLO,
  367. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  368. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  369. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  370. APPLICATION_DATA
  371. },
  372. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET | WITH_NPN ] = {
  373. CLIENT_HELLO,
  374. SERVER_HELLO, SERVER_CERT, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  375. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  376. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  377. APPLICATION_DATA
  378. },
  379. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH] = {
  380. CLIENT_HELLO,
  381. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  382. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  383. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  384. APPLICATION_DATA
  385. },
  386. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | WITH_NPN ] = {
  387. CLIENT_HELLO,
  388. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  389. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  390. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  391. APPLICATION_DATA
  392. },
  393. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT ] = {
  394. CLIENT_HELLO,
  395. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  396. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  397. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  398. APPLICATION_DATA
  399. },
  400. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_NPN ] = {
  401. CLIENT_HELLO,
  402. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  403. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  404. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  405. APPLICATION_DATA
  406. },
  407. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET] = {
  408. CLIENT_HELLO,
  409. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  410. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  411. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  412. APPLICATION_DATA
  413. },
  414. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN ] = {
  415. CLIENT_HELLO,
  416. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  417. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  418. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  419. APPLICATION_DATA
  420. },
  421. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET ] = {
  422. CLIENT_HELLO,
  423. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  424. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  425. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  426. APPLICATION_DATA
  427. },
  428. [NEGOTIATED | FULL_HANDSHAKE | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET | WITH_NPN ] = {
  429. CLIENT_HELLO,
  430. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  431. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  432. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  433. APPLICATION_DATA
  434. },
  435. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH ] = {
  436. CLIENT_HELLO,
  437. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  438. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  439. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  440. APPLICATION_DATA
  441. },
  442. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | WITH_NPN ] = {
  443. CLIENT_HELLO,
  444. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  445. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  446. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  447. APPLICATION_DATA
  448. },
  449. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT ] = {
  450. CLIENT_HELLO,
  451. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  452. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  453. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  454. APPLICATION_DATA
  455. },
  456. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_NPN ] = {
  457. CLIENT_HELLO,
  458. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  459. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  460. SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  461. APPLICATION_DATA
  462. },
  463. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET ] = {
  464. CLIENT_HELLO,
  465. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  466. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  467. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  468. APPLICATION_DATA
  469. },
  470. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | WITH_SESSION_TICKET | WITH_NPN ] = {
  471. CLIENT_HELLO,
  472. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  473. CLIENT_CERT, CLIENT_KEY, CLIENT_CERT_VERIFY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  474. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  475. APPLICATION_DATA
  476. },
  477. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET ] = {
  478. CLIENT_HELLO,
  479. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  480. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  481. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  482. APPLICATION_DATA
  483. },
  484. [NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY | OCSP_STATUS | CLIENT_AUTH | NO_CLIENT_CERT | WITH_SESSION_TICKET | WITH_NPN ] = {
  485. CLIENT_HELLO,
  486. SERVER_HELLO, SERVER_CERT, SERVER_CERT_STATUS, SERVER_KEY, SERVER_CERT_REQ, SERVER_HELLO_DONE,
  487. CLIENT_CERT, CLIENT_KEY, CLIENT_CHANGE_CIPHER_SPEC, CLIENT_NPN, CLIENT_FINISHED,
  488. SERVER_NEW_SESSION_TICKET, SERVER_CHANGE_CIPHER_SPEC, SERVER_FINISHED,
  489. APPLICATION_DATA
  490. },
  491. };
  492. /*
  493. * This selection of handshakes resembles the standard set, but with changes made to support tls1.3.
  494. *
  495. * The CHANGE_CIPHER_SPEC messages are included only for middlebox compatibility.
  496. * See https://tools.ietf.org/html/rfc8446#appendix-D.4
  497. */
  498. static message_type_t tls13_handshakes[S2N_HANDSHAKES_COUNT][S2N_MAX_HANDSHAKE_LENGTH] = {
  499. [INITIAL] = {
  500. CLIENT_HELLO,
  501. SERVER_HELLO
  502. },
  503. [INITIAL | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  504. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  505. SERVER_HELLO
  506. },
  507. [INITIAL | HELLO_RETRY_REQUEST] = {
  508. CLIENT_HELLO,
  509. HELLO_RETRY_MSG
  510. },
  511. [INITIAL | HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  512. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  513. HELLO_RETRY_MSG
  514. },
  515. [NEGOTIATED] = {
  516. CLIENT_HELLO,
  517. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  518. CLIENT_FINISHED,
  519. APPLICATION_DATA
  520. },
  521. [NEGOTIATED | WITH_EARLY_DATA] = {
  522. CLIENT_HELLO,
  523. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  524. END_OF_EARLY_DATA, CLIENT_FINISHED,
  525. APPLICATION_DATA
  526. },
  527. [NEGOTIATED | MIDDLEBOX_COMPAT] = {
  528. CLIENT_HELLO,
  529. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  530. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  531. APPLICATION_DATA
  532. },
  533. [NEGOTIATED | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  534. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  535. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  536. CLIENT_FINISHED,
  537. APPLICATION_DATA
  538. },
  539. [NEGOTIATED | MIDDLEBOX_COMPAT | WITH_EARLY_DATA] = {
  540. CLIENT_HELLO,
  541. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  542. CLIENT_CHANGE_CIPHER_SPEC, END_OF_EARLY_DATA, CLIENT_FINISHED,
  543. APPLICATION_DATA
  544. },
  545. [NEGOTIATED | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS | WITH_EARLY_DATA] = {
  546. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  547. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  548. END_OF_EARLY_DATA, CLIENT_FINISHED,
  549. APPLICATION_DATA
  550. },
  551. [NEGOTIATED | HELLO_RETRY_REQUEST] = {
  552. CLIENT_HELLO,
  553. HELLO_RETRY_MSG,
  554. CLIENT_HELLO,
  555. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  556. CLIENT_FINISHED,
  557. APPLICATION_DATA
  558. },
  559. [NEGOTIATED | HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT] = {
  560. CLIENT_HELLO,
  561. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  562. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_HELLO,
  563. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  564. CLIENT_FINISHED,
  565. APPLICATION_DATA
  566. },
  567. [NEGOTIATED | HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  568. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  569. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  570. CLIENT_HELLO,
  571. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_FINISHED,
  572. CLIENT_FINISHED,
  573. APPLICATION_DATA
  574. },
  575. [NEGOTIATED | FULL_HANDSHAKE] = {
  576. CLIENT_HELLO,
  577. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  578. CLIENT_FINISHED,
  579. APPLICATION_DATA
  580. },
  581. [NEGOTIATED | FULL_HANDSHAKE | MIDDLEBOX_COMPAT] = {
  582. CLIENT_HELLO,
  583. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  584. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_FINISHED,
  585. APPLICATION_DATA
  586. },
  587. [NEGOTIATED | FULL_HANDSHAKE | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  588. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  589. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  590. CLIENT_FINISHED,
  591. APPLICATION_DATA
  592. },
  593. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST] = {
  594. CLIENT_HELLO,
  595. HELLO_RETRY_MSG,
  596. CLIENT_HELLO,
  597. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  598. CLIENT_FINISHED,
  599. APPLICATION_DATA
  600. },
  601. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT] = {
  602. CLIENT_HELLO,
  603. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  604. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_HELLO,
  605. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  606. CLIENT_FINISHED,
  607. APPLICATION_DATA
  608. },
  609. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  610. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  611. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  612. CLIENT_HELLO,
  613. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  614. CLIENT_FINISHED,
  615. APPLICATION_DATA
  616. },
  617. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH] = {
  618. CLIENT_HELLO,
  619. HELLO_RETRY_MSG,
  620. CLIENT_HELLO,
  621. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  622. CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  623. APPLICATION_DATA
  624. },
  625. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH | MIDDLEBOX_COMPAT] = {
  626. CLIENT_HELLO,
  627. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  628. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_HELLO,
  629. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  630. CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  631. APPLICATION_DATA
  632. },
  633. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  634. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  635. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  636. CLIENT_HELLO,
  637. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  638. CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  639. APPLICATION_DATA
  640. },
  641. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH | NO_CLIENT_CERT] = {
  642. CLIENT_HELLO,
  643. HELLO_RETRY_MSG,
  644. CLIENT_HELLO,
  645. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  646. CLIENT_CERT, CLIENT_FINISHED,
  647. APPLICATION_DATA
  648. },
  649. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT] = {
  650. CLIENT_HELLO,
  651. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  652. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_HELLO,
  653. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  654. CLIENT_CERT, CLIENT_FINISHED,
  655. APPLICATION_DATA
  656. },
  657. [NEGOTIATED | FULL_HANDSHAKE | HELLO_RETRY_REQUEST | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  658. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  659. HELLO_RETRY_MSG, SERVER_CHANGE_CIPHER_SPEC,
  660. CLIENT_HELLO,
  661. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  662. CLIENT_CERT, CLIENT_FINISHED,
  663. APPLICATION_DATA
  664. },
  665. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH] = {
  666. CLIENT_HELLO,
  667. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  668. CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  669. APPLICATION_DATA
  670. },
  671. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT] = {
  672. CLIENT_HELLO,
  673. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  674. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  675. APPLICATION_DATA
  676. },
  677. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  678. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  679. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  680. CLIENT_CERT, CLIENT_CERT_VERIFY, CLIENT_FINISHED,
  681. APPLICATION_DATA
  682. },
  683. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT] = {
  684. CLIENT_HELLO,
  685. SERVER_HELLO, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  686. CLIENT_CERT, CLIENT_FINISHED,
  687. APPLICATION_DATA
  688. },
  689. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT] = {
  690. CLIENT_HELLO,
  691. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  692. CLIENT_CHANGE_CIPHER_SPEC, CLIENT_CERT, CLIENT_FINISHED,
  693. APPLICATION_DATA
  694. },
  695. [NEGOTIATED | FULL_HANDSHAKE | CLIENT_AUTH | NO_CLIENT_CERT | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS] = {
  696. CLIENT_HELLO, CLIENT_CHANGE_CIPHER_SPEC,
  697. SERVER_HELLO, SERVER_CHANGE_CIPHER_SPEC, ENCRYPTED_EXTENSIONS, SERVER_CERT_REQ, SERVER_CERT, SERVER_CERT_VERIFY, SERVER_FINISHED,
  698. CLIENT_CERT, CLIENT_FINISHED,
  699. APPLICATION_DATA
  700. },
  701. };
  702. /* clang-format on */
  703. #define MAX_HANDSHAKE_TYPE_LEN 142
  704. static char handshake_type_str[S2N_HANDSHAKES_COUNT][MAX_HANDSHAKE_TYPE_LEN] = { 0 };
  705. static const char *tls12_handshake_type_names[] = {
  706. "NEGOTIATED|",
  707. "FULL_HANDSHAKE|",
  708. "CLIENT_AUTH|",
  709. "NO_CLIENT_CERT|",
  710. "TLS12_PERFECT_FORWARD_SECRECY|",
  711. "OCSP_STATUS|",
  712. "WITH_SESSION_TICKET|",
  713. "WITH_NPN|",
  714. };
  715. static const char *tls13_handshake_type_names[] = {
  716. "NEGOTIATED|",
  717. "FULL_HANDSHAKE|",
  718. "CLIENT_AUTH|",
  719. "NO_CLIENT_CERT|",
  720. "HELLO_RETRY_REQUEST|",
  721. "MIDDLEBOX_COMPAT|",
  722. "WITH_EARLY_DATA|",
  723. "EARLY_CLIENT_CCS|",
  724. };
  725. #define IS_TLS13_HANDSHAKE(conn) ((conn)->handshake.state_machine == S2N_STATE_MACHINE_TLS13)
  726. #define ACTIVE_STATE_MACHINE(conn) (IS_TLS13_HANDSHAKE(conn) ? tls13_state_machine : state_machine)
  727. #define ACTIVE_HANDSHAKES(conn) (IS_TLS13_HANDSHAKE(conn) ? tls13_handshakes : handshakes)
  728. #define ACTIVE_MESSAGE(conn) ACTIVE_HANDSHAKES(conn)[(conn)->handshake.handshake_type][(conn)->handshake.message_number]
  729. #define ACTIVE_STATE(conn) ACTIVE_STATE_MACHINE(conn)[ACTIVE_MESSAGE((conn))]
  730. #define CCS_STATE(conn) (((conn)->mode == S2N_CLIENT) ? \
  731. ACTIVE_STATE_MACHINE(conn)[SERVER_CHANGE_CIPHER_SPEC] : \
  732. ACTIVE_STATE_MACHINE(conn)[CLIENT_CHANGE_CIPHER_SPEC])
  733. #define EXPECTED_RECORD_TYPE(conn) ACTIVE_STATE(conn).record_type
  734. #define EXPECTED_MESSAGE_TYPE(conn) ACTIVE_STATE(conn).message_type
  735. #define CONNECTION_WRITER(conn) (conn->mode == S2N_CLIENT ? 'C' : 'S')
  736. #define CONNECTION_IS_WRITER(conn) (ACTIVE_STATE(conn).writer == CONNECTION_WRITER(conn))
  737. /* Only used in our test cases. */
  738. message_type_t s2n_conn_get_current_message_type(struct s2n_connection *conn)
  739. {
  740. return ACTIVE_MESSAGE(conn);
  741. }
  742. static int s2n_advance_message(struct s2n_connection *conn)
  743. {
  744. /* Get the mode: 'C'lient or 'S'erver */
  745. char previous_writer = ACTIVE_STATE(conn).writer;
  746. char this_mode = CONNECTION_WRITER(conn);
  747. /* Actually advance the message number */
  748. conn->handshake.message_number++;
  749. /* When reading and using TLS1.3, skip optional change_cipher_spec states. */
  750. if (ACTIVE_STATE(conn).writer != this_mode && EXPECTED_RECORD_TYPE(conn) == TLS_CHANGE_CIPHER_SPEC && IS_TLS13_HANDSHAKE(conn)) {
  751. conn->handshake.message_number++;
  752. }
  753. /* Set TCP_QUICKACK to avoid artificial delay during the handshake */
  754. POSIX_GUARD(s2n_socket_quickack(conn));
  755. /* If optimized io hasn't been enabled or if the caller started out with a corked socket,
  756. * we don't mess with it
  757. */
  758. if (!conn->corked_io || s2n_socket_was_corked(conn)) {
  759. return S2N_SUCCESS;
  760. }
  761. /* Are we changing I/O directions */
  762. if (ACTIVE_STATE(conn).writer == previous_writer || ACTIVE_STATE(conn).writer == 'A') {
  763. return S2N_SUCCESS;
  764. }
  765. /* We're the new writer */
  766. if (ACTIVE_STATE(conn).writer == this_mode) {
  767. if (s2n_connection_is_managed_corked(conn)) {
  768. /* Set TCP_CORK/NOPUSH */
  769. POSIX_GUARD(s2n_socket_write_cork(conn));
  770. }
  771. return S2N_SUCCESS;
  772. }
  773. /* We're the new reader, or we reached the "B" writer stage indicating that
  774. we're at the application data stage - uncork the data */
  775. if (s2n_connection_is_managed_corked(conn)) {
  776. POSIX_GUARD(s2n_socket_write_uncork(conn));
  777. }
  778. return S2N_SUCCESS;
  779. }
  780. int s2n_generate_new_client_session_id(struct s2n_connection *conn)
  781. {
  782. if (conn->mode == S2N_SERVER) {
  783. struct s2n_blob session_id = { 0 };
  784. POSIX_GUARD(s2n_blob_init(&session_id, conn->session_id, S2N_TLS_SESSION_ID_MAX_LEN));
  785. /* Generate a new session id */
  786. POSIX_GUARD_RESULT(s2n_get_public_random_data(&session_id));
  787. conn->session_id_len = S2N_TLS_SESSION_ID_MAX_LEN;
  788. }
  789. return S2N_SUCCESS;
  790. }
  791. /* Lets the server flag whether a HelloRetryRequest is needed while processing extensions */
  792. int s2n_set_hello_retry_required(struct s2n_connection *conn)
  793. {
  794. POSIX_ENSURE_REF(conn);
  795. POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_INVALID_HELLO_RETRY);
  796. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls13_flag(conn, HELLO_RETRY_REQUEST));
  797. /* HelloRetryRequests also indicate rejection of early data.
  798. *
  799. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  800. *# A server which receives an "early_data" extension MUST behave in one
  801. *# of three ways:
  802. *
  803. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  804. *# - Request that the client send another ClientHello by responding
  805. *# with a HelloRetryRequest.
  806. **/
  807. if (conn->early_data_state == S2N_EARLY_DATA_REQUESTED) {
  808. POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_REJECTED));
  809. }
  810. return S2N_SUCCESS;
  811. }
  812. bool s2n_is_hello_retry_message(struct s2n_connection *conn)
  813. {
  814. return (conn != NULL && s2n_result_is_ok(s2n_handshake_validate(&(conn->handshake))) && ACTIVE_MESSAGE(conn) == HELLO_RETRY_MSG);
  815. }
  816. bool s2n_is_hello_retry_handshake(struct s2n_connection *conn)
  817. {
  818. return IS_HELLO_RETRY_HANDSHAKE(conn);
  819. }
  820. static S2N_RESULT s2n_conn_set_tls13_handshake_type(struct s2n_connection *conn)
  821. {
  822. RESULT_ENSURE_REF(conn);
  823. /* Most handshake type flags should be reset before we calculate the handshake type,
  824. * in order to handle changes during retries.
  825. * However, flags that have already affected the message order must be kept to avoid
  826. * rewriting the past.
  827. */
  828. conn->handshake.handshake_type &= (HELLO_RETRY_REQUEST | MIDDLEBOX_COMPAT | EARLY_CLIENT_CCS);
  829. /* A handshake type has been negotiated */
  830. RESULT_GUARD(s2n_handshake_type_set_flag(conn, NEGOTIATED));
  831. if (conn->psk_params.chosen_psk == NULL) {
  832. RESULT_GUARD(s2n_handshake_type_set_flag(conn, FULL_HANDSHAKE));
  833. }
  834. if (conn->early_data_state == S2N_EARLY_DATA_ACCEPTED) {
  835. conn->handshake.handshake_type |= WITH_EARLY_DATA;
  836. }
  837. s2n_cert_auth_type client_cert_auth_type;
  838. RESULT_GUARD_POSIX(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type));
  839. if (conn->mode == S2N_CLIENT && client_cert_auth_type == S2N_CERT_AUTH_REQUIRED
  840. && IS_FULL_HANDSHAKE(conn)) {
  841. /* If we're a client, and Client Auth is REQUIRED, then the Client must expect the CLIENT_CERT_REQ Message */
  842. RESULT_GUARD(s2n_handshake_type_set_flag(conn, CLIENT_AUTH));
  843. } else if (conn->mode == S2N_SERVER && client_cert_auth_type != S2N_CERT_AUTH_NONE
  844. && IS_FULL_HANDSHAKE(conn)) {
  845. /* If we're a server, and Client Auth is REQUIRED or OPTIONAL, then the server must send the CLIENT_CERT_REQ Message*/
  846. RESULT_GUARD(s2n_handshake_type_set_flag(conn, CLIENT_AUTH));
  847. }
  848. if (s2n_is_middlebox_compat_enabled(conn)) {
  849. RESULT_GUARD(s2n_handshake_type_set_tls13_flag(conn, MIDDLEBOX_COMPAT));
  850. }
  851. return S2N_RESULT_OK;
  852. }
  853. static S2N_RESULT s2n_validate_ems_status(struct s2n_connection *conn)
  854. {
  855. RESULT_ENSURE_REF(conn);
  856. s2n_extension_type_id ems_ext_id = 0;
  857. RESULT_GUARD_POSIX(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_EMS, &ems_ext_id));
  858. bool ems_extension_recv = S2N_CBIT_TEST(conn->extension_requests_received, ems_ext_id);
  859. /**
  860. *= https://tools.ietf.org/rfc/rfc7627#section-5.3
  861. *# If the original session used the "extended_master_secret"
  862. *# extension but the new ClientHello does not contain it, the server
  863. *# MUST abort the abbreviated handshake.
  864. **/
  865. if (conn->ems_negotiated) {
  866. RESULT_ENSURE(ems_extension_recv, S2N_ERR_MISSING_EXTENSION);
  867. }
  868. /* Since we're discarding the resumption ticket, ignore EMS value from the ticket */
  869. conn->ems_negotiated = ems_extension_recv;
  870. return S2N_RESULT_OK;
  871. }
  872. int s2n_conn_set_handshake_type(struct s2n_connection *conn)
  873. {
  874. POSIX_ENSURE_REF(conn);
  875. POSIX_ENSURE_REF(conn->secure);
  876. POSIX_GUARD_RESULT(s2n_conn_choose_state_machine(conn, conn->actual_protocol_version));
  877. if (IS_TLS13_HANDSHAKE(conn)) {
  878. POSIX_GUARD_RESULT(s2n_conn_set_tls13_handshake_type(conn));
  879. return S2N_SUCCESS;
  880. }
  881. POSIX_GUARD_RESULT(s2n_handshake_type_reset(conn));
  882. /* A handshake type has been negotiated */
  883. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, NEGOTIATED));
  884. s2n_cert_auth_type client_cert_auth_type;
  885. POSIX_GUARD(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type));
  886. if (conn->mode == S2N_CLIENT && client_cert_auth_type == S2N_CERT_AUTH_REQUIRED) {
  887. /* If we're a client, and Client Auth is REQUIRED, then the Client must expect the CLIENT_CERT_REQ Message */
  888. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, CLIENT_AUTH));
  889. } else if (conn->mode == S2N_SERVER && client_cert_auth_type != S2N_CERT_AUTH_NONE) {
  890. /* If we're a server, and Client Auth is REQUIRED or OPTIONAL, then the server must send the CLIENT_CERT_REQ Message*/
  891. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, CLIENT_AUTH));
  892. }
  893. if (conn->npn_negotiated) {
  894. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls12_flag(conn, WITH_NPN));
  895. }
  896. if (conn->config->use_tickets) {
  897. if (conn->session_ticket_status == S2N_DECRYPT_TICKET) {
  898. /* We reuse the session if a valid TLS12 ticket is provided.
  899. * Otherwise, we will perform a full handshake and then generate
  900. * a new session ticket. */
  901. if (s2n_decrypt_session_ticket(conn, &conn->client_ticket_to_decrypt) == S2N_SUCCESS) {
  902. return S2N_SUCCESS;
  903. }
  904. POSIX_GUARD_RESULT(s2n_validate_ems_status(conn));
  905. /* Set up the handshake to send a session ticket since a valid ticket was not provided */
  906. if (s2n_config_is_encrypt_decrypt_key_available(conn->config) == 1) {
  907. conn->session_ticket_status = S2N_NEW_TICKET;
  908. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls12_flag(conn, WITH_SESSION_TICKET));
  909. }
  910. /* If a session ticket is presented by the client, then skip lookup in Session ID server cache */
  911. goto skip_cache_lookup;
  912. }
  913. if (conn->session_ticket_status == S2N_NEW_TICKET) {
  914. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls12_flag(conn, WITH_SESSION_TICKET));
  915. }
  916. }
  917. /* If a TLS session is resumed, the Server should respond in its ServerHello with the same SessionId the
  918. * Client sent in the ClientHello. */
  919. if (conn->actual_protocol_version <= S2N_TLS12 && conn->mode == S2N_SERVER && s2n_allowed_to_cache_connection(conn)) {
  920. int r = s2n_resume_from_cache(conn);
  921. if (r == S2N_SUCCESS || (r < S2N_SUCCESS && S2N_ERROR_IS_BLOCKING(s2n_errno))) {
  922. return r;
  923. }
  924. POSIX_GUARD_RESULT(s2n_validate_ems_status(conn));
  925. }
  926. skip_cache_lookup:
  927. if (conn->mode == S2N_CLIENT && conn->client_session_resumed == 1) {
  928. return S2N_SUCCESS;
  929. }
  930. /* If we're doing full handshake, generate a new session id. */
  931. POSIX_GUARD(s2n_generate_new_client_session_id(conn));
  932. /* If we get this far, it's a full handshake */
  933. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, FULL_HANDSHAKE));
  934. bool is_ephemeral = false;
  935. POSIX_GUARD_RESULT(s2n_kex_is_ephemeral(conn->secure->cipher_suite->key_exchange_alg, &is_ephemeral));
  936. if (is_ephemeral) {
  937. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls12_flag(conn, TLS12_PERFECT_FORWARD_SECRECY));
  938. }
  939. if (s2n_server_can_send_ocsp(conn) || s2n_server_sent_ocsp(conn)) {
  940. POSIX_GUARD_RESULT(s2n_handshake_type_set_tls12_flag(conn, OCSP_STATUS));
  941. }
  942. return S2N_SUCCESS;
  943. }
  944. int s2n_conn_set_handshake_no_client_cert(struct s2n_connection *conn)
  945. {
  946. s2n_cert_auth_type client_cert_auth_type;
  947. POSIX_GUARD(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type));
  948. S2N_ERROR_IF(client_cert_auth_type != S2N_CERT_AUTH_OPTIONAL, S2N_ERR_BAD_MESSAGE);
  949. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, NO_CLIENT_CERT));
  950. return S2N_SUCCESS;
  951. }
  952. S2N_RESULT s2n_conn_choose_state_machine(struct s2n_connection *conn, uint8_t protocol_version)
  953. {
  954. RESULT_ENSURE_REF(conn);
  955. /* This should never be called before we know what version we're on */
  956. RESULT_ENSURE_NE(protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
  957. if (protocol_version == S2N_TLS13) {
  958. /* State machine should not change once set */
  959. RESULT_ENSURE_NE(conn->handshake.state_machine, S2N_STATE_MACHINE_TLS12);
  960. conn->handshake.state_machine = S2N_STATE_MACHINE_TLS13;
  961. } else {
  962. /* State machine should not change once set */
  963. RESULT_ENSURE_NE(conn->handshake.state_machine, S2N_STATE_MACHINE_TLS13);
  964. conn->handshake.state_machine = S2N_STATE_MACHINE_TLS12;
  965. }
  966. return S2N_RESULT_OK;
  967. }
  968. const char *s2n_connection_get_last_message_name(struct s2n_connection *conn)
  969. {
  970. PTR_ENSURE_REF(conn);
  971. PTR_GUARD_RESULT(s2n_handshake_validate(&(conn->handshake)));
  972. return message_names[ACTIVE_MESSAGE(conn)];
  973. }
  974. const char *s2n_connection_get_handshake_type_name(struct s2n_connection *conn)
  975. {
  976. PTR_ENSURE_REF(conn);
  977. PTR_PRECONDITION(s2n_handshake_validate(&(conn->handshake)));
  978. uint32_t handshake_type = conn->handshake.handshake_type;
  979. if (handshake_type == INITIAL) {
  980. return "INITIAL";
  981. }
  982. const char **handshake_type_names = tls13_handshake_type_names;
  983. size_t handshake_type_names_len = s2n_array_len(tls13_handshake_type_names);
  984. if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
  985. handshake_type_names = tls12_handshake_type_names;
  986. handshake_type_names_len = s2n_array_len(tls12_handshake_type_names);
  987. }
  988. /* Not all handshake strings will be created already. If the handshake string
  989. * is not null, we can just return the handshake. Otherwise we have to compute
  990. * it down below. */
  991. if (handshake_type_str[handshake_type][0] != '\0') {
  992. return handshake_type_str[handshake_type];
  993. }
  994. /* Compute handshake_type_str[handshake_type] by concatenating
  995. * each applicable handshake_type.
  996. *
  997. * Unit tests enforce that the elements of handshake_type_str are always
  998. * long enough to contain the longest possible valid handshake_type, but
  999. * for safety we still handle the case where we need to truncate.
  1000. */
  1001. char *p = handshake_type_str[handshake_type];
  1002. size_t remaining = sizeof(handshake_type_str[0]);
  1003. for (size_t i = 0; i < handshake_type_names_len; i++) {
  1004. if (handshake_type & (1 << i)) {
  1005. size_t bytes_to_copy = MIN(remaining, strlen(handshake_type_names[i]));
  1006. PTR_CHECKED_MEMCPY(p, handshake_type_names[i], bytes_to_copy);
  1007. p[bytes_to_copy] = '\0';
  1008. p += bytes_to_copy;
  1009. remaining -= bytes_to_copy;
  1010. }
  1011. }
  1012. if (p != handshake_type_str[handshake_type] && '|' == *(p - 1)) {
  1013. *(p - 1) = '\0';
  1014. }
  1015. return handshake_type_str[handshake_type];
  1016. }
  1017. S2N_RESULT s2n_handshake_message_send(struct s2n_connection *conn, uint8_t content_type, s2n_blocked_status *blocked)
  1018. {
  1019. RESULT_ENSURE_REF(conn);
  1020. struct s2n_stuffer *in = &conn->handshake.io;
  1021. uint32_t size = s2n_stuffer_data_available(in);
  1022. if (size == 0) {
  1023. return S2N_RESULT_OK;
  1024. }
  1025. if (s2n_connection_is_quic_enabled(conn)) {
  1026. RESULT_GUARD(s2n_quic_write_handshake_message(conn));
  1027. RESULT_GUARD_POSIX(s2n_flush(conn, blocked));
  1028. return S2N_RESULT_OK;
  1029. }
  1030. struct iovec iov = { 0 };
  1031. iov.iov_len = size;
  1032. iov.iov_base = s2n_stuffer_raw_read(in, size);
  1033. RESULT_ENSURE_REF(iov.iov_base);
  1034. RESULT_GUARD_POSIX(s2n_stuffer_rewind_read(in, size));
  1035. uint32_t total_bytes_written = 0;
  1036. while (total_bytes_written < size) {
  1037. int bytes_written = s2n_record_writev(conn, content_type, &iov, 1,
  1038. total_bytes_written, size - total_bytes_written);
  1039. RESULT_GUARD_POSIX(bytes_written);
  1040. total_bytes_written += bytes_written;
  1041. RESULT_GUARD_POSIX(s2n_stuffer_skip_read(in, bytes_written));
  1042. RESULT_GUARD_POSIX(s2n_flush(conn, blocked));
  1043. }
  1044. return S2N_RESULT_OK;
  1045. }
  1046. /* Writing is relatively straight forward, simply write each message out as a record,
  1047. * we may fragment a message across multiple records, but we never coalesce multiple
  1048. * messages into single records.
  1049. * Precondition: secure outbound I/O has already been flushed
  1050. */
  1051. static int s2n_handshake_write_io(struct s2n_connection *conn)
  1052. {
  1053. uint8_t record_type = EXPECTED_RECORD_TYPE(conn);
  1054. s2n_blocked_status blocked = S2N_NOT_BLOCKED;
  1055. /* Populate handshake.io with header/payload for the current state, once.
  1056. * Check wiped instead of s2n_stuffer_data_available to differentiate between the initial call
  1057. * to s2n_handshake_write_io and a repeated call after an EWOULDBLOCK.
  1058. */
  1059. if (s2n_stuffer_is_wiped(&conn->handshake.io)) {
  1060. if (record_type == TLS_HANDSHAKE) {
  1061. POSIX_GUARD(s2n_handshake_write_header(&conn->handshake.io, ACTIVE_STATE(conn).message_type));
  1062. }
  1063. POSIX_GUARD(ACTIVE_STATE(conn).handler[conn->mode](conn));
  1064. if (record_type == TLS_HANDSHAKE) {
  1065. POSIX_GUARD(s2n_handshake_finish_header(&conn->handshake.io));
  1066. }
  1067. }
  1068. POSIX_GUARD_RESULT(s2n_handshake_message_send(conn, record_type, &blocked));
  1069. if (record_type == TLS_HANDSHAKE) {
  1070. POSIX_GUARD_RESULT(s2n_handshake_transcript_update(conn));
  1071. }
  1072. /* We're done sending the last record, reset everything */
  1073. POSIX_GUARD(s2n_stuffer_wipe(&conn->out));
  1074. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  1075. /* Update the secrets, if necessary */
  1076. POSIX_GUARD_RESULT(s2n_tls13_secrets_update(conn));
  1077. POSIX_GUARD_RESULT(s2n_tls13_key_schedule_update(conn));
  1078. /* Advance the state machine */
  1079. POSIX_GUARD(s2n_advance_message(conn));
  1080. return S2N_SUCCESS;
  1081. }
  1082. /*
  1083. * Returns:
  1084. * 1 - more data is needed to complete the handshake message.
  1085. * 0 - we read the whole handshake message.
  1086. * -1 - error processing the handshake message.
  1087. */
  1088. static int s2n_read_full_handshake_message(struct s2n_connection *conn, uint8_t *message_type)
  1089. {
  1090. uint32_t current_handshake_data = s2n_stuffer_data_available(&conn->handshake.io);
  1091. if (current_handshake_data < TLS_HANDSHAKE_HEADER_LENGTH) {
  1092. /* The message may be so badly fragmented that we don't even read the full header, take
  1093. * what we can and then continue to the next record read iteration.
  1094. */
  1095. if (s2n_stuffer_data_available(&conn->in) < (TLS_HANDSHAKE_HEADER_LENGTH - current_handshake_data)) {
  1096. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->handshake.io, s2n_stuffer_data_available(&conn->in)));
  1097. return 1;
  1098. }
  1099. /* Get the remainder of the header */
  1100. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->handshake.io, (TLS_HANDSHAKE_HEADER_LENGTH - current_handshake_data)));
  1101. }
  1102. uint32_t handshake_message_length = 0;
  1103. POSIX_GUARD_RESULT(s2n_handshake_parse_header(&conn->handshake.io, message_type, &handshake_message_length));
  1104. S2N_ERROR_IF(handshake_message_length > S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH, S2N_ERR_BAD_MESSAGE);
  1105. uint32_t bytes_to_take = handshake_message_length - s2n_stuffer_data_available(&conn->handshake.io);
  1106. bytes_to_take = MIN(bytes_to_take, s2n_stuffer_data_available(&conn->in));
  1107. /* If the record is handshake data, add it to the handshake buffer */
  1108. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->handshake.io, bytes_to_take));
  1109. /* If we have the whole handshake message, then success */
  1110. if (s2n_stuffer_data_available(&conn->handshake.io) == handshake_message_length) {
  1111. return 0;
  1112. }
  1113. /* We don't have the whole message, so we'll need to go again */
  1114. POSIX_GUARD(s2n_stuffer_reread(&conn->handshake.io));
  1115. return 1;
  1116. }
  1117. static int s2n_handshake_handle_sslv2(struct s2n_connection *conn)
  1118. {
  1119. S2N_ERROR_IF(ACTIVE_MESSAGE(conn) != CLIENT_HELLO, S2N_ERR_BAD_MESSAGE);
  1120. /* Add the message to our handshake hashes */
  1121. struct s2n_blob hashed = { 0 };
  1122. POSIX_GUARD(s2n_blob_init(&hashed, conn->header_in.blob.data + 2, 3));
  1123. POSIX_GUARD(s2n_conn_update_handshake_hashes(conn, &hashed));
  1124. hashed.data = conn->in.blob.data;
  1125. hashed.size = s2n_stuffer_data_available(&conn->in);
  1126. POSIX_GUARD(s2n_conn_update_handshake_hashes(conn, &hashed));
  1127. /* Handle an SSLv2 client hello */
  1128. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->handshake.io, s2n_stuffer_data_available(&conn->in)));
  1129. /* Set the client hello version */
  1130. conn->client_hello_version = S2N_SSLv2;
  1131. /* Execute the state machine handler */
  1132. int r = ACTIVE_STATE(conn).handler[conn->mode](conn);
  1133. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  1134. /* We're done with the record, wipe it */
  1135. POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
  1136. POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
  1137. WITH_ERROR_BLINDING(conn, POSIX_GUARD(r));
  1138. conn->in_status = ENCRYPTED;
  1139. /* Advance the state machine */
  1140. POSIX_GUARD(s2n_advance_message(conn));
  1141. return S2N_SUCCESS;
  1142. }
  1143. static int s2n_try_delete_session_cache(struct s2n_connection *conn)
  1144. {
  1145. POSIX_ENSURE_REF(conn);
  1146. if (s2n_allowed_to_cache_connection(conn) > 0) {
  1147. conn->config->cache_delete(conn, conn->config->cache_delete_data, conn->session_id, conn->session_id_len);
  1148. }
  1149. return S2N_SUCCESS;
  1150. }
  1151. static S2N_RESULT s2n_wipe_record(struct s2n_connection *conn)
  1152. {
  1153. RESULT_ENSURE_REF(conn);
  1154. RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->header_in));
  1155. RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->in));
  1156. conn->in_status = ENCRYPTED;
  1157. return S2N_RESULT_OK;
  1158. }
  1159. static S2N_RESULT s2n_finish_read(struct s2n_connection *conn)
  1160. {
  1161. RESULT_ENSURE_REF(conn);
  1162. RESULT_GUARD(s2n_handshake_transcript_update(conn));
  1163. RESULT_GUARD_POSIX(s2n_stuffer_wipe(&conn->handshake.io));
  1164. RESULT_GUARD(s2n_tls13_secrets_update(conn));
  1165. RESULT_GUARD(s2n_tls13_key_schedule_update(conn));
  1166. RESULT_GUARD_POSIX(s2n_advance_message(conn));
  1167. return S2N_RESULT_OK;
  1168. }
  1169. static S2N_RESULT s2n_handshake_app_data_recv(struct s2n_connection *conn)
  1170. {
  1171. if (conn->early_data_expected) {
  1172. RESULT_GUARD(s2n_early_data_validate_recv(conn));
  1173. RESULT_BAIL(S2N_ERR_EARLY_DATA_BLOCKED);
  1174. }
  1175. if (conn->handshake.renegotiation) {
  1176. RESULT_GUARD(s2n_renegotiate_validate(conn));
  1177. /* During renegotiation, Application Data may only be received until
  1178. * the server acknowledges the new handshake with a ServerHello.
  1179. */
  1180. RESULT_ENSURE(ACTIVE_MESSAGE(conn) == SERVER_HELLO, S2N_ERR_BAD_MESSAGE);
  1181. RESULT_BAIL(S2N_ERR_APP_DATA_BLOCKED);
  1182. }
  1183. RESULT_BAIL(S2N_ERR_BAD_MESSAGE);
  1184. }
  1185. /* Reading is a little more complicated than writing as the TLS RFCs allow content
  1186. * types to be interleaved at the record layer. We may get an alert message
  1187. * during the handshake phase, or messages of types that we don't support (e.g.
  1188. * HEARTBEAT messages), or during renegotiations we may even get application
  1189. * data messages that need to be handled by the application. The latter is punted
  1190. * for now (s2n does not support renegotiations).
  1191. */
  1192. static int s2n_handshake_read_io(struct s2n_connection *conn)
  1193. {
  1194. uint8_t record_type;
  1195. uint8_t message_type;
  1196. int isSSLv2 = 0;
  1197. /* Fill conn->in stuffer necessary for the handshake.
  1198. * If using TCP, read a record. If using QUIC, read a message. */
  1199. if (s2n_connection_is_quic_enabled(conn)) {
  1200. record_type = TLS_HANDSHAKE;
  1201. POSIX_GUARD_RESULT(s2n_quic_read_handshake_message(conn, &message_type));
  1202. } else {
  1203. int r = s2n_read_full_record(conn, &record_type, &isSSLv2);
  1204. /**
  1205. *= https://tools.ietf.org/rfc/rfc8446#section-4.2.10
  1206. *# If the client attempts a 0-RTT handshake but the server
  1207. *# rejects it, the server will generally not have the 0-RTT record
  1208. *# protection keys and must instead use trial decryption (either with
  1209. *# the 1-RTT handshake keys or by looking for a cleartext ClientHello in
  1210. *# the case of a HelloRetryRequest) to find the first non-0-RTT message.
  1211. *#
  1212. *# If the server chooses to accept the "early_data" extension, then it
  1213. *# MUST comply with the same error-handling requirements specified for
  1214. *# all records when processing early data records. Specifically, if the
  1215. *# server fails to decrypt a 0-RTT record following an accepted
  1216. *# "early_data" extension, it MUST terminate the connection with a
  1217. *# "bad_record_mac" alert as per Section 5.2.
  1218. */
  1219. if ((r < S2N_SUCCESS) && (s2n_errno == S2N_ERR_EARLY_DATA_TRIAL_DECRYPT)) {
  1220. POSIX_GUARD(s2n_stuffer_reread(&conn->in));
  1221. POSIX_GUARD_RESULT(s2n_early_data_record_bytes(conn, s2n_stuffer_data_available(&conn->in)));
  1222. POSIX_GUARD_RESULT(s2n_wipe_record(conn));
  1223. return S2N_SUCCESS;
  1224. }
  1225. POSIX_GUARD(r);
  1226. }
  1227. if (isSSLv2) {
  1228. S2N_ERROR_IF(record_type != SSLv2_CLIENT_HELLO, S2N_ERR_BAD_MESSAGE);
  1229. POSIX_GUARD(s2n_handshake_handle_sslv2(conn));
  1230. }
  1231. /* Now we have a record, but it could be a partial fragment of a message, or it might
  1232. * contain several messages.
  1233. */
  1234. if (record_type == TLS_APPLICATION_DATA) {
  1235. POSIX_GUARD_RESULT(s2n_handshake_app_data_recv(conn));
  1236. } else if (record_type == TLS_CHANGE_CIPHER_SPEC) {
  1237. /* TLS1.3 can receive unexpected CCS messages at any point in the handshake
  1238. * due to a peer operating in middlebox compatibility mode.
  1239. * However, when operating in QUIC mode, S2N should not accept ANY CCS messages,
  1240. * including these unexpected ones.*/
  1241. if (!IS_TLS13_HANDSHAKE(conn) || s2n_connection_is_quic_enabled(conn)) {
  1242. POSIX_ENSURE(EXPECTED_RECORD_TYPE(conn) == TLS_CHANGE_CIPHER_SPEC, S2N_ERR_BAD_MESSAGE);
  1243. POSIX_ENSURE(!CONNECTION_IS_WRITER(conn), S2N_ERR_BAD_MESSAGE);
  1244. }
  1245. S2N_ERROR_IF(s2n_stuffer_data_available(&conn->in) != 1, S2N_ERR_BAD_MESSAGE);
  1246. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->handshake.io, s2n_stuffer_data_available(&conn->in)));
  1247. POSIX_GUARD(CCS_STATE(conn).handler[conn->mode](conn));
  1248. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  1249. /* We're done with the record, wipe it */
  1250. POSIX_GUARD_RESULT(s2n_wipe_record(conn));
  1251. /* Advance the state machine if this was an expected message */
  1252. if (EXPECTED_RECORD_TYPE(conn) == TLS_CHANGE_CIPHER_SPEC && !CONNECTION_IS_WRITER(conn)) {
  1253. POSIX_GUARD(s2n_advance_message(conn));
  1254. }
  1255. return S2N_SUCCESS;
  1256. } else if (record_type != TLS_HANDSHAKE) {
  1257. if (record_type == TLS_ALERT) {
  1258. POSIX_GUARD(s2n_process_alert_fragment(conn));
  1259. }
  1260. /* Ignore record types that we don't support */
  1261. /* We're done with the record, wipe it */
  1262. POSIX_GUARD_RESULT(s2n_wipe_record(conn));
  1263. return S2N_SUCCESS;
  1264. }
  1265. /* Record is a handshake message */
  1266. S2N_ERROR_IF(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_BAD_MESSAGE);
  1267. while (s2n_stuffer_data_available(&conn->in)) {
  1268. /* We're done with negotiating but we have trailing data in this record. Bail on the handshake. */
  1269. S2N_ERROR_IF(EXPECTED_RECORD_TYPE(conn) == TLS_APPLICATION_DATA, S2N_ERR_BAD_MESSAGE);
  1270. int r;
  1271. POSIX_GUARD((r = s2n_read_full_handshake_message(conn, &message_type)));
  1272. /* Do we need more data? This happens for message fragmentation */
  1273. if (r == 1) {
  1274. /* Break out of this inner loop, but since we're not changing the state, the
  1275. * outer loop in s2n_handshake_io() will read another record.
  1276. */
  1277. POSIX_GUARD_RESULT(s2n_wipe_record(conn));
  1278. return S2N_SUCCESS;
  1279. }
  1280. s2n_cert_auth_type client_cert_auth_type;
  1281. POSIX_GUARD(s2n_connection_get_client_auth_type(conn, &client_cert_auth_type));
  1282. /* If we're a Client, and received a ClientCertRequest message, and ClientAuth
  1283. * is set to optional, then switch the State Machine that we're using to expect the ClientCertRequest. */
  1284. if (conn->mode == S2N_CLIENT
  1285. && client_cert_auth_type == S2N_CERT_AUTH_OPTIONAL
  1286. && message_type == TLS_CERT_REQ) {
  1287. POSIX_ENSURE(IS_FULL_HANDSHAKE(conn), S2N_ERR_HANDSHAKE_STATE);
  1288. POSIX_GUARD_RESULT(s2n_handshake_type_set_flag(conn, CLIENT_AUTH));
  1289. }
  1290. /* According to rfc6066 section 8, server may choose not to send "CertificateStatus" message even if it has
  1291. * sent "status_request" extension in the ServerHello message. */
  1292. if (conn->mode == S2N_CLIENT
  1293. && EXPECTED_MESSAGE_TYPE(conn) == TLS_SERVER_CERT_STATUS
  1294. && message_type != TLS_SERVER_CERT_STATUS) {
  1295. POSIX_GUARD_RESULT(s2n_handshake_type_unset_tls12_flag(conn, OCSP_STATUS));
  1296. }
  1297. /*
  1298. *= https://tools.ietf.org/rfc/rfc5246#section-7.4
  1299. *# The one message that is not bound by these ordering rules
  1300. *# is the HelloRequest message, which can be sent at any time, but which
  1301. *# SHOULD be ignored by the client if it arrives in the middle of a handshake.
  1302. */
  1303. if (message_type == TLS_HELLO_REQUEST) {
  1304. POSIX_GUARD_RESULT(s2n_client_hello_request_validate(conn));
  1305. POSIX_GUARD(s2n_stuffer_wipe(&conn->handshake.io));
  1306. continue;
  1307. }
  1308. POSIX_ENSURE(record_type == EXPECTED_RECORD_TYPE(conn), S2N_ERR_BAD_MESSAGE);
  1309. POSIX_ENSURE(message_type == EXPECTED_MESSAGE_TYPE(conn), S2N_ERR_BAD_MESSAGE);
  1310. POSIX_ENSURE(!CONNECTION_IS_WRITER(conn), S2N_ERR_BAD_MESSAGE);
  1311. /* Call the relevant handler */
  1312. WITH_ERROR_BLINDING(conn, POSIX_GUARD(ACTIVE_STATE(conn).handler[conn->mode](conn)));
  1313. /* Advance the state machine */
  1314. POSIX_GUARD_RESULT(s2n_finish_read(conn));
  1315. }
  1316. /* We're done with the record, wipe it */
  1317. POSIX_GUARD_RESULT(s2n_wipe_record(conn));
  1318. return S2N_SUCCESS;
  1319. }
  1320. static int s2n_handle_retry_state(struct s2n_connection *conn)
  1321. {
  1322. /* If we were blocked reading or writing a record, then the handler is waiting on
  1323. * external data. The handler will know how to continue, so we should call the
  1324. * handler right away. We aren't going to read more handshake data yet or proceed
  1325. * to the next handler because the current message has not finished processing. */
  1326. s2n_errno = S2N_ERR_OK;
  1327. const int r = ACTIVE_STATE(conn).handler[conn->mode](conn);
  1328. if (r < S2N_SUCCESS && S2N_ERROR_IS_BLOCKING(s2n_errno)) {
  1329. /* If the handler is still waiting for data, return control to the caller. */
  1330. S2N_ERROR_PRESERVE_ERRNO();
  1331. }
  1332. /* Resume the handshake */
  1333. conn->handshake.paused = false;
  1334. if (!CONNECTION_IS_WRITER(conn)) {
  1335. /* We're done parsing the record, reset everything */
  1336. POSIX_GUARD(s2n_stuffer_wipe(&conn->header_in));
  1337. POSIX_GUARD(s2n_stuffer_wipe(&conn->in));
  1338. conn->in_status = ENCRYPTED;
  1339. }
  1340. if (CONNECTION_IS_WRITER(conn)) {
  1341. POSIX_GUARD(r);
  1342. /* If we're the writer and handler just finished, update the record header if
  1343. * needed and let the s2n_handshake_write_io write the data to the socket */
  1344. if (EXPECTED_RECORD_TYPE(conn) == TLS_HANDSHAKE) {
  1345. POSIX_GUARD(s2n_handshake_finish_header(&conn->handshake.io));
  1346. }
  1347. } else {
  1348. if (r < S2N_SUCCESS && conn->session_id_len) {
  1349. s2n_try_delete_session_cache(conn);
  1350. }
  1351. WITH_ERROR_BLINDING(conn, POSIX_GUARD(r));
  1352. /* The read handler processed the record successfully, we are done with this
  1353. * record. Advance the state machine. */
  1354. POSIX_GUARD_RESULT(s2n_finish_read(conn));
  1355. }
  1356. return S2N_SUCCESS;
  1357. }
  1358. bool s2n_handshake_is_complete(struct s2n_connection *conn)
  1359. {
  1360. return conn && ACTIVE_STATE(conn).writer == 'B';
  1361. }
  1362. int s2n_negotiate_impl(struct s2n_connection *conn, s2n_blocked_status *blocked)
  1363. {
  1364. POSIX_ENSURE_REF(conn);
  1365. POSIX_ENSURE_REF(blocked);
  1366. while (!s2n_handshake_is_complete(conn) && ACTIVE_MESSAGE(conn) != conn->handshake.end_of_messages) {
  1367. errno = 0;
  1368. s2n_errno = S2N_ERR_OK;
  1369. /* Flush any pending I/O or alert messages */
  1370. POSIX_GUARD(s2n_flush(conn, blocked));
  1371. POSIX_ENSURE(s2n_connection_check_io_status(conn, S2N_IO_FULL_DUPLEX), S2N_ERR_CLOSED);
  1372. /* If the handshake was paused, retry the current message */
  1373. if (conn->handshake.paused) {
  1374. *blocked = S2N_BLOCKED_ON_APPLICATION_INPUT;
  1375. POSIX_GUARD(s2n_handle_retry_state(conn));
  1376. }
  1377. if (CONNECTION_IS_WRITER(conn)) {
  1378. *blocked = S2N_BLOCKED_ON_WRITE;
  1379. const int write_result = s2n_handshake_write_io(conn);
  1380. if (write_result < S2N_SUCCESS) {
  1381. if (!S2N_ERROR_IS_BLOCKING(s2n_errno)) {
  1382. /* Non-retryable write error. The peer might have sent an alert. Try and read it. */
  1383. const int write_errno = errno;
  1384. const int write_s2n_errno = s2n_errno;
  1385. struct s2n_debug_info write_s2n_debug_info = _s2n_debug_info;
  1386. if (s2n_handshake_read_io(conn) < 0 && s2n_errno == S2N_ERR_ALERT) {
  1387. /* s2n_handshake_read_io has set s2n_errno */
  1388. S2N_ERROR_PRESERVE_ERRNO();
  1389. } else {
  1390. /* Let the write error take precedence if we didn't read an alert. */
  1391. errno = write_errno;
  1392. s2n_errno = write_s2n_errno;
  1393. _s2n_debug_info = write_s2n_debug_info;
  1394. S2N_ERROR_PRESERVE_ERRNO();
  1395. }
  1396. }
  1397. if (s2n_errno == S2N_ERR_ASYNC_BLOCKED) {
  1398. *blocked = S2N_BLOCKED_ON_APPLICATION_INPUT;
  1399. conn->handshake.paused = true;
  1400. } else if (s2n_errno == S2N_ERR_EARLY_DATA_BLOCKED) {
  1401. *blocked = S2N_BLOCKED_ON_EARLY_DATA;
  1402. }
  1403. S2N_ERROR_PRESERVE_ERRNO();
  1404. }
  1405. } else {
  1406. *blocked = S2N_BLOCKED_ON_READ;
  1407. const int read_result = s2n_handshake_read_io(conn);
  1408. if (read_result < S2N_SUCCESS) {
  1409. /* One blocking condition is waiting on the session resumption cache. */
  1410. /* So we don't want to delete anything if we are blocked. */
  1411. if (!S2N_ERROR_IS_BLOCKING(s2n_errno) && conn->session_id_len) {
  1412. s2n_try_delete_session_cache(conn);
  1413. }
  1414. if (s2n_errno == S2N_ERR_ASYNC_BLOCKED) {
  1415. *blocked = S2N_BLOCKED_ON_APPLICATION_INPUT;
  1416. conn->handshake.paused = true;
  1417. } else if (s2n_errno == S2N_ERR_EARLY_DATA_BLOCKED) {
  1418. *blocked = S2N_BLOCKED_ON_EARLY_DATA;
  1419. }
  1420. S2N_ERROR_PRESERVE_ERRNO();
  1421. }
  1422. }
  1423. if (ACTIVE_STATE(conn).writer == 'B') {
  1424. /* Clean up handshake secrets */
  1425. POSIX_GUARD_RESULT(s2n_tls13_secrets_clean(conn));
  1426. /* Send any pending post-handshake messages */
  1427. POSIX_GUARD(s2n_post_handshake_send(conn, blocked));
  1428. /* If the handshake has just ended, free up memory */
  1429. POSIX_GUARD(s2n_stuffer_resize(&conn->handshake.io, 0));
  1430. }
  1431. }
  1432. *blocked = S2N_NOT_BLOCKED;
  1433. return S2N_SUCCESS;
  1434. }
  1435. int s2n_negotiate(struct s2n_connection *conn, s2n_blocked_status *blocked)
  1436. {
  1437. POSIX_ENSURE_REF(conn);
  1438. POSIX_ENSURE(!conn->negotiate_in_use, S2N_ERR_REENTRANCY);
  1439. conn->negotiate_in_use = true;
  1440. int result = s2n_negotiate_impl(conn, blocked);
  1441. /* finish up sending and receiving */
  1442. POSIX_GUARD_RESULT(s2n_connection_dynamic_free_in_buffer(conn));
  1443. POSIX_GUARD_RESULT(s2n_connection_dynamic_free_out_buffer(conn));
  1444. conn->negotiate_in_use = false;
  1445. return result;
  1446. }