ssl_misc.h 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. /**
  2. * \file ssl_misc.h
  3. *
  4. * \brief Internal functions shared by the SSL modules
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_SSL_MISC_H
  11. #define MBEDTLS_SSL_MISC_H
  12. #include "mbedtls/build_info.h"
  13. #include "mbedtls/error.h"
  14. #include "mbedtls/ssl.h"
  15. #include "mbedtls/cipher.h"
  16. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
  17. #include "psa/crypto.h"
  18. #include "psa_util_internal.h"
  19. #endif
  20. #if defined(MBEDTLS_MD_CAN_MD5)
  21. #include "mbedtls/md5.h"
  22. #endif
  23. #if defined(MBEDTLS_MD_CAN_SHA1)
  24. #include "mbedtls/sha1.h"
  25. #endif
  26. #if defined(MBEDTLS_MD_CAN_SHA256)
  27. #include "mbedtls/sha256.h"
  28. #endif
  29. #if defined(MBEDTLS_MD_CAN_SHA512)
  30. #include "mbedtls/sha512.h"
  31. #endif
  32. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
  33. !defined(MBEDTLS_USE_PSA_CRYPTO)
  34. #include "mbedtls/ecjpake.h"
  35. #endif
  36. #include "mbedtls/pk.h"
  37. #include "ssl_ciphersuites_internal.h"
  38. #include "x509_internal.h"
  39. #include "pk_internal.h"
  40. #include "common.h"
  41. /* Shorthand for restartable ECC */
  42. #if defined(MBEDTLS_ECP_RESTARTABLE) && \
  43. defined(MBEDTLS_SSL_CLI_C) && \
  44. defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  45. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  46. #define MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED
  47. #endif
  48. #define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
  49. #define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
  50. #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
  51. #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
  52. /* Faked handshake message identity for HelloRetryRequest. */
  53. #define MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST (-MBEDTLS_SSL_HS_SERVER_HELLO)
  54. /*
  55. * Internal identity of handshake extensions
  56. */
  57. #define MBEDTLS_SSL_EXT_ID_UNRECOGNIZED 0
  58. #define MBEDTLS_SSL_EXT_ID_SERVERNAME 1
  59. #define MBEDTLS_SSL_EXT_ID_SERVERNAME_HOSTNAME 1
  60. #define MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH 2
  61. #define MBEDTLS_SSL_EXT_ID_STATUS_REQUEST 3
  62. #define MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS 4
  63. #define MBEDTLS_SSL_EXT_ID_SUPPORTED_ELLIPTIC_CURVES 4
  64. #define MBEDTLS_SSL_EXT_ID_SIG_ALG 5
  65. #define MBEDTLS_SSL_EXT_ID_USE_SRTP 6
  66. #define MBEDTLS_SSL_EXT_ID_HEARTBEAT 7
  67. #define MBEDTLS_SSL_EXT_ID_ALPN 8
  68. #define MBEDTLS_SSL_EXT_ID_SCT 9
  69. #define MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE 10
  70. #define MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE 11
  71. #define MBEDTLS_SSL_EXT_ID_PADDING 12
  72. #define MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY 13
  73. #define MBEDTLS_SSL_EXT_ID_EARLY_DATA 14
  74. #define MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS 15
  75. #define MBEDTLS_SSL_EXT_ID_COOKIE 16
  76. #define MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES 17
  77. #define MBEDTLS_SSL_EXT_ID_CERT_AUTH 18
  78. #define MBEDTLS_SSL_EXT_ID_OID_FILTERS 19
  79. #define MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH 20
  80. #define MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT 21
  81. #define MBEDTLS_SSL_EXT_ID_KEY_SHARE 22
  82. #define MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC 23
  83. #define MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS 24
  84. #define MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC 25
  85. #define MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET 26
  86. #define MBEDTLS_SSL_EXT_ID_SESSION_TICKET 27
  87. #define MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT 28
  88. /* Utility for translating IANA extension type. */
  89. uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type);
  90. uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type);
  91. /* Macros used to define mask constants */
  92. #define MBEDTLS_SSL_EXT_MASK(id) (1ULL << (MBEDTLS_SSL_EXT_ID_##id))
  93. /* Reset value of extension mask */
  94. #define MBEDTLS_SSL_EXT_MASK_NONE 0
  95. /* In messages containing extension requests, we should ignore unrecognized
  96. * extensions. In messages containing extension responses, unrecognized
  97. * extensions should result in handshake abortion. Messages containing
  98. * extension requests include ClientHello, CertificateRequest and
  99. * NewSessionTicket. Messages containing extension responses include
  100. * ServerHello, HelloRetryRequest, EncryptedExtensions and Certificate.
  101. *
  102. * RFC 8446 section 4.1.3
  103. *
  104. * The ServerHello MUST only include extensions which are required to establish
  105. * the cryptographic context and negotiate the protocol version.
  106. *
  107. * RFC 8446 section 4.2
  108. *
  109. * If an implementation receives an extension which it recognizes and which is
  110. * not specified for the message in which it appears, it MUST abort the handshake
  111. * with an "illegal_parameter" alert.
  112. */
  113. /* Extensions that are not recognized by TLS 1.3 */
  114. #define MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED \
  115. (MBEDTLS_SSL_EXT_MASK(SUPPORTED_POINT_FORMATS) | \
  116. MBEDTLS_SSL_EXT_MASK(ENCRYPT_THEN_MAC) | \
  117. MBEDTLS_SSL_EXT_MASK(EXTENDED_MASTER_SECRET) | \
  118. MBEDTLS_SSL_EXT_MASK(SESSION_TICKET) | \
  119. MBEDTLS_SSL_EXT_MASK(TRUNCATED_HMAC) | \
  120. MBEDTLS_SSL_EXT_MASK(UNRECOGNIZED))
  121. /* RFC 8446 section 4.2. Allowed extensions for ClientHello */
  122. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH \
  123. (MBEDTLS_SSL_EXT_MASK(SERVERNAME) | \
  124. MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH) | \
  125. MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST) | \
  126. MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) | \
  127. MBEDTLS_SSL_EXT_MASK(SIG_ALG) | \
  128. MBEDTLS_SSL_EXT_MASK(USE_SRTP) | \
  129. MBEDTLS_SSL_EXT_MASK(HEARTBEAT) | \
  130. MBEDTLS_SSL_EXT_MASK(ALPN) | \
  131. MBEDTLS_SSL_EXT_MASK(SCT) | \
  132. MBEDTLS_SSL_EXT_MASK(CLI_CERT_TYPE) | \
  133. MBEDTLS_SSL_EXT_MASK(SERV_CERT_TYPE) | \
  134. MBEDTLS_SSL_EXT_MASK(PADDING) | \
  135. MBEDTLS_SSL_EXT_MASK(KEY_SHARE) | \
  136. MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | \
  137. MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES) | \
  138. MBEDTLS_SSL_EXT_MASK(EARLY_DATA) | \
  139. MBEDTLS_SSL_EXT_MASK(COOKIE) | \
  140. MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS) | \
  141. MBEDTLS_SSL_EXT_MASK(CERT_AUTH) | \
  142. MBEDTLS_SSL_EXT_MASK(POST_HANDSHAKE_AUTH) | \
  143. MBEDTLS_SSL_EXT_MASK(SIG_ALG_CERT) | \
  144. MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT) | \
  145. MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
  146. /* RFC 8446 section 4.2. Allowed extensions for EncryptedExtensions */
  147. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE \
  148. (MBEDTLS_SSL_EXT_MASK(SERVERNAME) | \
  149. MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH) | \
  150. MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) | \
  151. MBEDTLS_SSL_EXT_MASK(USE_SRTP) | \
  152. MBEDTLS_SSL_EXT_MASK(HEARTBEAT) | \
  153. MBEDTLS_SSL_EXT_MASK(ALPN) | \
  154. MBEDTLS_SSL_EXT_MASK(CLI_CERT_TYPE) | \
  155. MBEDTLS_SSL_EXT_MASK(SERV_CERT_TYPE) | \
  156. MBEDTLS_SSL_EXT_MASK(EARLY_DATA) | \
  157. MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT))
  158. /* RFC 8446 section 4.2. Allowed extensions for CertificateRequest */
  159. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR \
  160. (MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST) | \
  161. MBEDTLS_SSL_EXT_MASK(SIG_ALG) | \
  162. MBEDTLS_SSL_EXT_MASK(SCT) | \
  163. MBEDTLS_SSL_EXT_MASK(CERT_AUTH) | \
  164. MBEDTLS_SSL_EXT_MASK(OID_FILTERS) | \
  165. MBEDTLS_SSL_EXT_MASK(SIG_ALG_CERT) | \
  166. MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
  167. /* RFC 8446 section 4.2. Allowed extensions for Certificate */
  168. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT \
  169. (MBEDTLS_SSL_EXT_MASK(STATUS_REQUEST) | \
  170. MBEDTLS_SSL_EXT_MASK(SCT))
  171. /* RFC 8446 section 4.2. Allowed extensions for ServerHello */
  172. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH \
  173. (MBEDTLS_SSL_EXT_MASK(KEY_SHARE) | \
  174. MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | \
  175. MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS))
  176. /* RFC 8446 section 4.2. Allowed extensions for HelloRetryRequest */
  177. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR \
  178. (MBEDTLS_SSL_EXT_MASK(KEY_SHARE) | \
  179. MBEDTLS_SSL_EXT_MASK(COOKIE) | \
  180. MBEDTLS_SSL_EXT_MASK(SUPPORTED_VERSIONS))
  181. /* RFC 8446 section 4.2. Allowed extensions for NewSessionTicket */
  182. #define MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST \
  183. (MBEDTLS_SSL_EXT_MASK(EARLY_DATA) | \
  184. MBEDTLS_SSL_TLS1_3_EXT_MASK_UNRECOGNIZED)
  185. /*
  186. * Helper macros for function call with return check.
  187. */
  188. /*
  189. * Exit when return non-zero value
  190. */
  191. #define MBEDTLS_SSL_PROC_CHK(f) \
  192. do { \
  193. ret = (f); \
  194. if (ret != 0) \
  195. { \
  196. goto cleanup; \
  197. } \
  198. } while (0)
  199. /*
  200. * Exit when return negative value
  201. */
  202. #define MBEDTLS_SSL_PROC_CHK_NEG(f) \
  203. do { \
  204. ret = (f); \
  205. if (ret < 0) \
  206. { \
  207. goto cleanup; \
  208. } \
  209. } while (0)
  210. /*
  211. * DTLS retransmission states, see RFC 6347 4.2.4
  212. *
  213. * The SENDING state is merged in PREPARING for initial sends,
  214. * but is distinct for resends.
  215. *
  216. * Note: initial state is wrong for server, but is not used anyway.
  217. */
  218. #define MBEDTLS_SSL_RETRANS_PREPARING 0
  219. #define MBEDTLS_SSL_RETRANS_SENDING 1
  220. #define MBEDTLS_SSL_RETRANS_WAITING 2
  221. #define MBEDTLS_SSL_RETRANS_FINISHED 3
  222. /*
  223. * Allow extra bytes for record, authentication and encryption overhead:
  224. * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256).
  225. */
  226. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  227. /* This macro determines whether CBC is supported. */
  228. #if defined(MBEDTLS_SSL_HAVE_CBC) && \
  229. (defined(MBEDTLS_SSL_HAVE_AES) || \
  230. defined(MBEDTLS_SSL_HAVE_CAMELLIA) || \
  231. defined(MBEDTLS_SSL_HAVE_ARIA))
  232. #define MBEDTLS_SSL_SOME_SUITES_USE_CBC
  233. #endif
  234. /* This macro determines whether a ciphersuite using a
  235. * stream cipher can be used. */
  236. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  237. #define MBEDTLS_SSL_SOME_SUITES_USE_STREAM
  238. #endif
  239. /* This macro determines whether the CBC construct used in TLS 1.2 is supported. */
  240. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
  241. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  242. #define MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC
  243. #endif
  244. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) || \
  245. defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
  246. #define MBEDTLS_SSL_SOME_SUITES_USE_MAC
  247. #endif
  248. /* This macro determines whether a ciphersuite uses Encrypt-then-MAC with CBC */
  249. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
  250. defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  251. #define MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM
  252. #endif
  253. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  254. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  255. /* Ciphersuites using HMAC */
  256. #if defined(MBEDTLS_MD_CAN_SHA384)
  257. #define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
  258. #elif defined(MBEDTLS_MD_CAN_SHA256)
  259. #define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
  260. #else
  261. #define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
  262. #endif
  263. #else /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
  264. /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
  265. #define MBEDTLS_SSL_MAC_ADD 16
  266. #endif
  267. #if defined(MBEDTLS_SSL_HAVE_CBC)
  268. #define MBEDTLS_SSL_PADDING_ADD 256
  269. #else
  270. #define MBEDTLS_SSL_PADDING_ADD 0
  271. #endif
  272. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  273. #define MBEDTLS_SSL_MAX_CID_EXPANSION MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY
  274. #else
  275. #define MBEDTLS_SSL_MAX_CID_EXPANSION 0
  276. #endif
  277. #define MBEDTLS_SSL_PAYLOAD_OVERHEAD (MBEDTLS_MAX_IV_LENGTH + \
  278. MBEDTLS_SSL_MAC_ADD + \
  279. MBEDTLS_SSL_PADDING_ADD + \
  280. MBEDTLS_SSL_MAX_CID_EXPANSION \
  281. )
  282. #define MBEDTLS_SSL_IN_PAYLOAD_LEN (MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  283. (MBEDTLS_SSL_IN_CONTENT_LEN))
  284. #define MBEDTLS_SSL_OUT_PAYLOAD_LEN (MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  285. (MBEDTLS_SSL_OUT_CONTENT_LEN))
  286. /* The maximum number of buffered handshake messages. */
  287. #define MBEDTLS_SSL_MAX_BUFFERED_HS 4
  288. /* Maximum length we can advertise as our max content length for
  289. RFC 6066 max_fragment_length extension negotiation purposes
  290. (the lesser of both sizes, if they are unequal.)
  291. */
  292. #define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ( \
  293. (MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN) \
  294. ? (MBEDTLS_SSL_OUT_CONTENT_LEN) \
  295. : (MBEDTLS_SSL_IN_CONTENT_LEN) \
  296. )
  297. /* Maximum size in bytes of list in signature algorithms ext., RFC 5246/8446 */
  298. #define MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN 65534
  299. /* Minimum size in bytes of list in signature algorithms ext., RFC 5246/8446 */
  300. #define MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN 2
  301. /* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
  302. #define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
  303. #define MBEDTLS_RECEIVED_SIG_ALGS_SIZE 20
  304. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  305. #define MBEDTLS_TLS_SIG_NONE MBEDTLS_TLS1_3_SIG_NONE
  306. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  307. #define MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(sig, hash) ((hash << 8) | sig)
  308. #define MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(alg) (alg & 0xFF)
  309. #define MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(alg) (alg >> 8)
  310. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  311. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  312. /*
  313. * Check that we obey the standard's message size bounds
  314. */
  315. #if MBEDTLS_SSL_IN_CONTENT_LEN > 16384
  316. #error "Bad configuration - incoming record content too large."
  317. #endif
  318. #if MBEDTLS_SSL_OUT_CONTENT_LEN > 16384
  319. #error "Bad configuration - outgoing record content too large."
  320. #endif
  321. #if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_IN_CONTENT_LEN + 2048
  322. #error "Bad configuration - incoming protected record payload too large."
  323. #endif
  324. #if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN + 2048
  325. #error "Bad configuration - outgoing protected record payload too large."
  326. #endif
  327. /* Calculate buffer sizes */
  328. /* Note: Even though the TLS record header is only 5 bytes
  329. long, we're internally using 8 bytes to store the
  330. implicit sequence number. */
  331. #define MBEDTLS_SSL_HEADER_LEN 13
  332. #if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  333. #define MBEDTLS_SSL_IN_BUFFER_LEN \
  334. ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN))
  335. #else
  336. #define MBEDTLS_SSL_IN_BUFFER_LEN \
  337. ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_IN_PAYLOAD_LEN) \
  338. + (MBEDTLS_SSL_CID_IN_LEN_MAX))
  339. #endif
  340. #if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  341. #define MBEDTLS_SSL_OUT_BUFFER_LEN \
  342. ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN))
  343. #else
  344. #define MBEDTLS_SSL_OUT_BUFFER_LEN \
  345. ((MBEDTLS_SSL_HEADER_LEN) + (MBEDTLS_SSL_OUT_PAYLOAD_LEN) \
  346. + (MBEDTLS_SSL_CID_OUT_LEN_MAX))
  347. #endif
  348. #define MBEDTLS_CLIENT_HELLO_RANDOM_LEN 32
  349. #define MBEDTLS_SERVER_HELLO_RANDOM_LEN 32
  350. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  351. /**
  352. * \brief Return the maximum fragment length (payload, in bytes) for
  353. * the output buffer. For the client, this is the configured
  354. * value. For the server, it is the minimum of two - the
  355. * configured value and the negotiated one.
  356. *
  357. * \sa mbedtls_ssl_conf_max_frag_len()
  358. * \sa mbedtls_ssl_get_max_out_record_payload()
  359. *
  360. * \param ssl SSL context
  361. *
  362. * \return Current maximum fragment length for the output buffer.
  363. */
  364. size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl);
  365. /**
  366. * \brief Return the maximum fragment length (payload, in bytes) for
  367. * the input buffer. This is the negotiated maximum fragment
  368. * length, or, if there is none, MBEDTLS_SSL_IN_CONTENT_LEN.
  369. * If it is not defined either, the value is 2^14. This function
  370. * works as its predecessor, \c mbedtls_ssl_get_max_frag_len().
  371. *
  372. * \sa mbedtls_ssl_conf_max_frag_len()
  373. * \sa mbedtls_ssl_get_max_in_record_payload()
  374. *
  375. * \param ssl SSL context
  376. *
  377. * \return Current maximum fragment length for the output buffer.
  378. */
  379. size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl);
  380. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  381. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  382. /**
  383. * \brief Get the size limit in bytes for the protected outgoing records
  384. * as defined in RFC 8449
  385. *
  386. * \param ssl SSL context
  387. *
  388. * \return The size limit in bytes for the protected outgoing
  389. * records as defined in RFC 8449.
  390. */
  391. size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl);
  392. #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
  393. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  394. static inline size_t mbedtls_ssl_get_output_buflen(const mbedtls_ssl_context *ctx)
  395. {
  396. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  397. return mbedtls_ssl_get_output_max_frag_len(ctx)
  398. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
  399. + MBEDTLS_SSL_CID_OUT_LEN_MAX;
  400. #else
  401. return mbedtls_ssl_get_output_max_frag_len(ctx)
  402. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
  403. #endif
  404. }
  405. static inline size_t mbedtls_ssl_get_input_buflen(const mbedtls_ssl_context *ctx)
  406. {
  407. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  408. return mbedtls_ssl_get_input_max_frag_len(ctx)
  409. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
  410. + MBEDTLS_SSL_CID_IN_LEN_MAX;
  411. #else
  412. return mbedtls_ssl_get_input_max_frag_len(ctx)
  413. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
  414. #endif
  415. }
  416. #endif
  417. /*
  418. * TLS extension flags (for extensions with outgoing ServerHello content
  419. * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
  420. * of state of the renegotiation flag, so no indicator is required)
  421. */
  422. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
  423. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
  424. /**
  425. * \brief This function checks if the remaining size in a buffer is
  426. * greater or equal than a needed space.
  427. *
  428. * \param cur Pointer to the current position in the buffer.
  429. * \param end Pointer to one past the end of the buffer.
  430. * \param need Needed space in bytes.
  431. *
  432. * \return Zero if the needed space is available in the buffer, non-zero
  433. * otherwise.
  434. */
  435. #if !defined(MBEDTLS_TEST_HOOKS)
  436. static inline int mbedtls_ssl_chk_buf_ptr(const uint8_t *cur,
  437. const uint8_t *end, size_t need)
  438. {
  439. return (cur > end) || (need > (size_t) (end - cur));
  440. }
  441. #else
  442. typedef struct {
  443. const uint8_t *cur;
  444. const uint8_t *end;
  445. size_t need;
  446. } mbedtls_ssl_chk_buf_ptr_args;
  447. void mbedtls_ssl_set_chk_buf_ptr_fail_args(
  448. const uint8_t *cur, const uint8_t *end, size_t need);
  449. void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void);
  450. MBEDTLS_CHECK_RETURN_CRITICAL
  451. int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args);
  452. static inline int mbedtls_ssl_chk_buf_ptr(const uint8_t *cur,
  453. const uint8_t *end, size_t need)
  454. {
  455. if ((cur > end) || (need > (size_t) (end - cur))) {
  456. mbedtls_ssl_set_chk_buf_ptr_fail_args(cur, end, need);
  457. return 1;
  458. }
  459. return 0;
  460. }
  461. #endif /* MBEDTLS_TEST_HOOKS */
  462. /**
  463. * \brief This macro checks if the remaining size in a buffer is
  464. * greater or equal than a needed space. If it is not the case,
  465. * it returns an SSL_BUFFER_TOO_SMALL error.
  466. *
  467. * \param cur Pointer to the current position in the buffer.
  468. * \param end Pointer to one past the end of the buffer.
  469. * \param need Needed space in bytes.
  470. *
  471. */
  472. #define MBEDTLS_SSL_CHK_BUF_PTR(cur, end, need) \
  473. do { \
  474. if (mbedtls_ssl_chk_buf_ptr((cur), (end), (need)) != 0) \
  475. { \
  476. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; \
  477. } \
  478. } while (0)
  479. /**
  480. * \brief This macro checks if the remaining length in an input buffer is
  481. * greater or equal than a needed length. If it is not the case, it
  482. * returns #MBEDTLS_ERR_SSL_DECODE_ERROR error and pends a
  483. * #MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR alert message.
  484. *
  485. * This is a function-like macro. It is guaranteed to evaluate each
  486. * argument exactly once.
  487. *
  488. * \param cur Pointer to the current position in the buffer.
  489. * \param end Pointer to one past the end of the buffer.
  490. * \param need Needed length in bytes.
  491. *
  492. */
  493. #define MBEDTLS_SSL_CHK_BUF_READ_PTR(cur, end, need) \
  494. do { \
  495. if (mbedtls_ssl_chk_buf_ptr((cur), (end), (need)) != 0) \
  496. { \
  497. MBEDTLS_SSL_DEBUG_MSG(1, \
  498. ("missing input data in %s", __func__)); \
  499. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
  500. MBEDTLS_ERR_SSL_DECODE_ERROR); \
  501. return MBEDTLS_ERR_SSL_DECODE_ERROR; \
  502. } \
  503. } while (0)
  504. #ifdef __cplusplus
  505. extern "C" {
  506. #endif
  507. typedef int mbedtls_ssl_tls_prf_cb(const unsigned char *secret, size_t slen,
  508. const char *label,
  509. const unsigned char *random, size_t rlen,
  510. unsigned char *dstbuf, size_t dlen);
  511. /* cipher.h exports the maximum IV, key and block length from
  512. * all ciphers enabled in the config, regardless of whether those
  513. * ciphers are actually usable in SSL/TLS. Notably, XTS is enabled
  514. * in the default configuration and uses 64 Byte keys, but it is
  515. * not used for record protection in SSL/TLS.
  516. *
  517. * In order to prevent unnecessary inflation of key structures,
  518. * we introduce SSL-specific variants of the max-{key,block,IV}
  519. * macros here which are meant to only take those ciphers into
  520. * account which can be negotiated in SSL/TLS.
  521. *
  522. * Since the current definitions of MBEDTLS_MAX_{KEY|BLOCK|IV}_LENGTH
  523. * in cipher.h are rough overapproximations of the real maxima, here
  524. * we content ourselves with replicating those overapproximations
  525. * for the maximum block and IV length, and excluding XTS from the
  526. * computation of the maximum key length. */
  527. #define MBEDTLS_SSL_MAX_BLOCK_LENGTH 16
  528. #define MBEDTLS_SSL_MAX_IV_LENGTH 16
  529. #define MBEDTLS_SSL_MAX_KEY_LENGTH 32
  530. /**
  531. * \brief The data structure holding the cryptographic material (key and IV)
  532. * used for record protection in TLS 1.3.
  533. */
  534. struct mbedtls_ssl_key_set {
  535. /*! The key for client->server records. */
  536. unsigned char client_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
  537. /*! The key for server->client records. */
  538. unsigned char server_write_key[MBEDTLS_SSL_MAX_KEY_LENGTH];
  539. /*! The IV for client->server records. */
  540. unsigned char client_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
  541. /*! The IV for server->client records. */
  542. unsigned char server_write_iv[MBEDTLS_SSL_MAX_IV_LENGTH];
  543. size_t key_len; /*!< The length of client_write_key and
  544. * server_write_key, in Bytes. */
  545. size_t iv_len; /*!< The length of client_write_iv and
  546. * server_write_iv, in Bytes. */
  547. };
  548. typedef struct mbedtls_ssl_key_set mbedtls_ssl_key_set;
  549. typedef struct {
  550. unsigned char binder_key[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  551. unsigned char client_early_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  552. unsigned char early_exporter_master_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  553. } mbedtls_ssl_tls13_early_secrets;
  554. typedef struct {
  555. unsigned char client_handshake_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  556. unsigned char server_handshake_traffic_secret[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  557. } mbedtls_ssl_tls13_handshake_secrets;
  558. /*
  559. * This structure contains the parameters only needed during handshake.
  560. */
  561. struct mbedtls_ssl_handshake_params {
  562. /* Frequently-used boolean or byte fields (placed early to take
  563. * advantage of smaller code size for indirect access on Arm Thumb) */
  564. uint8_t resume; /*!< session resume indicator*/
  565. uint8_t cli_exts; /*!< client extension presence*/
  566. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  567. uint8_t sni_authmode; /*!< authmode from SNI callback */
  568. #endif
  569. #if defined(MBEDTLS_SSL_SRV_C)
  570. /* Flag indicating if a CertificateRequest message has been sent
  571. * to the client or not. */
  572. uint8_t certificate_request_sent;
  573. #if defined(MBEDTLS_SSL_EARLY_DATA)
  574. /* Flag indicating if the server has accepted early data or not. */
  575. uint8_t early_data_accepted;
  576. #endif
  577. #endif /* MBEDTLS_SSL_SRV_C */
  578. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  579. uint8_t new_session_ticket; /*!< use NewSessionTicket? */
  580. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  581. #if defined(MBEDTLS_SSL_CLI_C)
  582. /** Minimum TLS version to be negotiated.
  583. *
  584. * It is set up in the ClientHello writing preparation stage and used
  585. * throughout the ClientHello writing. Not relevant anymore as soon as
  586. * the protocol version has been negotiated thus as soon as the
  587. * ServerHello is received.
  588. * For a fresh handshake not linked to any previous handshake, it is
  589. * equal to the configured minimum minor version to be negotiated. When
  590. * renegotiating or resuming a session, it is equal to the previously
  591. * negotiated minor version.
  592. *
  593. * There is no maximum TLS version field in this handshake context.
  594. * From the start of the handshake, we need to define a current protocol
  595. * version for the record layer which we define as the maximum TLS
  596. * version to be negotiated. The `tls_version` field of the SSL context is
  597. * used to store this maximum value until it contains the actual
  598. * negotiated value.
  599. */
  600. mbedtls_ssl_protocol_version min_tls_version;
  601. #endif
  602. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  603. uint8_t extended_ms; /*!< use Extended Master Secret? */
  604. #endif
  605. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  606. uint8_t async_in_progress; /*!< an asynchronous operation is in progress */
  607. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  608. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  609. unsigned char retransmit_state; /*!< Retransmission state */
  610. #endif
  611. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  612. unsigned char group_list_heap_allocated;
  613. unsigned char sig_algs_heap_allocated;
  614. #endif
  615. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  616. uint8_t ecrs_enabled; /*!< Handshake supports EC restart? */
  617. enum { /* this complements ssl->state with info on intra-state operations */
  618. ssl_ecrs_none = 0, /*!< nothing going on (yet) */
  619. ssl_ecrs_crt_verify, /*!< Certificate: crt_verify() */
  620. ssl_ecrs_ske_start_processing, /*!< ServerKeyExchange: pk_verify() */
  621. ssl_ecrs_cke_ecdh_calc_secret, /*!< ClientKeyExchange: ECDH step 2 */
  622. ssl_ecrs_crt_vrfy_sign, /*!< CertificateVerify: pk_sign() */
  623. } ecrs_state; /*!< current (or last) operation */
  624. mbedtls_x509_crt *ecrs_peer_cert; /*!< The peer's CRT chain. */
  625. size_t ecrs_n; /*!< place for saving a length */
  626. #endif
  627. mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
  628. MBEDTLS_CHECK_RETURN_CRITICAL
  629. int (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
  630. MBEDTLS_CHECK_RETURN_CRITICAL
  631. int (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
  632. MBEDTLS_CHECK_RETURN_CRITICAL
  633. int (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
  634. mbedtls_ssl_tls_prf_cb *tls_prf;
  635. /*
  636. * Handshake specific crypto variables
  637. */
  638. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  639. uint8_t key_exchange_mode; /*!< Selected key exchange mode */
  640. /**
  641. * Flag indicating if, in the course of the current handshake, an
  642. * HelloRetryRequest message has been sent by the server or received by
  643. * the client (<> 0) or not (0).
  644. */
  645. uint8_t hello_retry_request_flag;
  646. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  647. /**
  648. * Flag indicating if, in the course of the current handshake, a dummy
  649. * change_cipher_spec (CCS) record has already been sent. Used to send only
  650. * one CCS per handshake while not complicating the handshake state
  651. * transitions for that purpose.
  652. */
  653. uint8_t ccs_sent;
  654. #endif
  655. #if defined(MBEDTLS_SSL_SRV_C)
  656. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  657. uint8_t tls13_kex_modes; /*!< Key exchange modes supported by the client */
  658. #endif
  659. /** selected_group of key_share extension in HelloRetryRequest message. */
  660. uint16_t hrr_selected_group;
  661. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  662. uint16_t new_session_tickets_count; /*!< number of session tickets */
  663. #endif
  664. #endif /* MBEDTLS_SSL_SRV_C */
  665. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  666. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  667. uint16_t received_sig_algs[MBEDTLS_RECEIVED_SIG_ALGS_SIZE];
  668. #endif
  669. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  670. const uint16_t *group_list;
  671. const uint16_t *sig_algs;
  672. #endif
  673. #if defined(MBEDTLS_DHM_C)
  674. mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
  675. #endif
  676. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  677. defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
  678. mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
  679. #endif /* !MBEDTLS_USE_PSA_CRYPTO &&
  680. MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
  681. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
  682. psa_key_type_t xxdh_psa_type;
  683. size_t xxdh_psa_bits;
  684. mbedtls_svc_key_id_t xxdh_psa_privkey;
  685. uint8_t xxdh_psa_privkey_is_external;
  686. unsigned char xxdh_psa_peerkey[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
  687. size_t xxdh_psa_peerkey_len;
  688. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
  689. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  690. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  691. psa_pake_operation_t psa_pake_ctx; /*!< EC J-PAKE key exchange */
  692. mbedtls_svc_key_id_t psa_pake_password;
  693. uint8_t psa_pake_ctx_is_ok;
  694. #else
  695. mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
  696. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  697. #if defined(MBEDTLS_SSL_CLI_C)
  698. unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
  699. size_t ecjpake_cache_len; /*!< Length of cached data */
  700. #endif
  701. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  702. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
  703. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) || \
  704. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  705. uint16_t *curves_tls_id; /*!< List of TLS IDs of supported elliptic curves */
  706. #endif
  707. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
  708. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  709. mbedtls_svc_key_id_t psk_opaque; /*!< Opaque PSK from the callback */
  710. uint8_t psk_opaque_is_internal;
  711. #else
  712. unsigned char *psk; /*!< PSK from the callback */
  713. size_t psk_len; /*!< Length of PSK from callback */
  714. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  715. uint16_t selected_identity;
  716. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
  717. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  718. mbedtls_x509_crt_restart_ctx ecrs_ctx; /*!< restart context */
  719. #endif
  720. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  721. mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
  722. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  723. mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
  724. mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
  725. mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
  726. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  727. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  728. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  729. !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  730. mbedtls_pk_context peer_pubkey; /*!< The public key from the peer. */
  731. #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  732. struct {
  733. size_t total_bytes_buffered; /*!< Cumulative size of heap allocated
  734. * buffers used for message buffering. */
  735. uint8_t seen_ccs; /*!< Indicates if a CCS message has
  736. * been seen in the current flight. */
  737. struct mbedtls_ssl_hs_buffer {
  738. unsigned is_valid : 1;
  739. unsigned is_fragmented : 1;
  740. unsigned is_complete : 1;
  741. unsigned char *data;
  742. size_t data_len;
  743. } hs[MBEDTLS_SSL_MAX_BUFFERED_HS];
  744. struct {
  745. unsigned char *data;
  746. size_t len;
  747. unsigned epoch;
  748. } future_record;
  749. } buffering;
  750. #if defined(MBEDTLS_SSL_CLI_C) && \
  751. (defined(MBEDTLS_SSL_PROTO_DTLS) || \
  752. defined(MBEDTLS_SSL_PROTO_TLS1_3))
  753. unsigned char *cookie; /*!< HelloVerifyRequest cookie for DTLS
  754. * HelloRetryRequest cookie for TLS 1.3 */
  755. #if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
  756. /* RFC 6347 page 15
  757. ...
  758. opaque cookie<0..2^8-1>;
  759. ...
  760. */
  761. uint8_t cookie_len;
  762. #else
  763. /* RFC 8446 page 39
  764. ...
  765. opaque cookie<0..2^16-1>;
  766. ...
  767. If TLS1_3 is enabled, the max length is 2^16 - 1
  768. */
  769. uint16_t cookie_len; /*!< DTLS: HelloVerifyRequest cookie length
  770. * TLS1_3: HelloRetryRequest cookie length */
  771. #endif
  772. #endif /* MBEDTLS_SSL_CLI_C &&
  773. ( MBEDTLS_SSL_PROTO_DTLS ||
  774. MBEDTLS_SSL_PROTO_TLS1_3 ) */
  775. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_DTLS)
  776. unsigned char cookie_verify_result; /*!< Srv: flag for sending a cookie */
  777. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS */
  778. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  779. unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
  780. unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
  781. uint32_t retransmit_timeout; /*!< Current value of timeout */
  782. mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
  783. mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
  784. unsigned char *cur_msg_p; /*!< Position in current message */
  785. unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
  786. flight being received */
  787. mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
  788. resending messages */
  789. unsigned char alt_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; /*!< Alternative record epoch/counter
  790. for resending messages */
  791. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  792. /* The state of CID configuration in this handshake. */
  793. uint8_t cid_in_use; /*!< This indicates whether the use of the CID extension
  794. * has been negotiated. Possible values are
  795. * #MBEDTLS_SSL_CID_ENABLED and
  796. * #MBEDTLS_SSL_CID_DISABLED. */
  797. unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX]; /*! The peer's CID */
  798. uint8_t peer_cid_len; /*!< The length of
  799. * \c peer_cid. */
  800. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  801. uint16_t mtu; /*!< Handshake mtu, used to fragment outgoing messages */
  802. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  803. /*
  804. * Checksum contexts
  805. */
  806. #if defined(MBEDTLS_MD_CAN_SHA256)
  807. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  808. psa_hash_operation_t fin_sha256_psa;
  809. #else
  810. mbedtls_md_context_t fin_sha256;
  811. #endif
  812. #endif
  813. #if defined(MBEDTLS_MD_CAN_SHA384)
  814. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  815. psa_hash_operation_t fin_sha384_psa;
  816. #else
  817. mbedtls_md_context_t fin_sha384;
  818. #endif
  819. #endif
  820. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  821. uint16_t offered_group_id; /* The NamedGroup value for the group
  822. * that is being used for ephemeral
  823. * key exchange.
  824. *
  825. * On the client: Defaults to the first
  826. * entry in the client's group list,
  827. * but can be overwritten by the HRR. */
  828. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  829. #if defined(MBEDTLS_SSL_CLI_C)
  830. uint8_t client_auth; /*!< used to check if CertificateRequest has been
  831. received from server side. If CertificateRequest
  832. has been received, Certificate and CertificateVerify
  833. should be sent to server */
  834. #endif /* MBEDTLS_SSL_CLI_C */
  835. /*
  836. * State-local variables used during the processing
  837. * of a specific handshake state.
  838. */
  839. union {
  840. /* Outgoing Finished message */
  841. struct {
  842. uint8_t preparation_done;
  843. /* Buffer holding digest of the handshake up to
  844. * but excluding the outgoing finished message. */
  845. unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  846. size_t digest_len;
  847. } finished_out;
  848. /* Incoming Finished message */
  849. struct {
  850. uint8_t preparation_done;
  851. /* Buffer holding digest of the handshake up to but
  852. * excluding the peer's incoming finished message. */
  853. unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  854. size_t digest_len;
  855. } finished_in;
  856. } state_local;
  857. /* End of state-local variables. */
  858. unsigned char randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN +
  859. MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  860. /*!< random bytes */
  861. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  862. unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
  863. /*!< premaster secret */
  864. size_t pmslen; /*!< premaster length */
  865. #endif
  866. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  867. uint32_t sent_extensions; /*!< extensions sent by endpoint */
  868. uint32_t received_extensions; /*!< extensions received by endpoint */
  869. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  870. unsigned char certificate_request_context_len;
  871. unsigned char *certificate_request_context;
  872. #endif
  873. /** TLS 1.3 transform for encrypted handshake messages. */
  874. mbedtls_ssl_transform *transform_handshake;
  875. union {
  876. unsigned char early[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  877. unsigned char handshake[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  878. unsigned char app[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  879. } tls13_master_secrets;
  880. mbedtls_ssl_tls13_handshake_secrets tls13_hs_secrets;
  881. #if defined(MBEDTLS_SSL_EARLY_DATA)
  882. /** TLS 1.3 transform for early data and handshake messages. */
  883. mbedtls_ssl_transform *transform_earlydata;
  884. #endif
  885. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  886. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  887. /** Asynchronous operation context. This field is meant for use by the
  888. * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
  889. * mbedtls_ssl_config::f_async_decrypt_start,
  890. * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
  891. * The library does not use it internally. */
  892. void *user_async_ctx;
  893. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  894. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  895. const unsigned char *sni_name; /*!< raw SNI */
  896. size_t sni_name_len; /*!< raw SNI len */
  897. #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  898. const mbedtls_x509_crt *dn_hints; /*!< acceptable client cert issuers */
  899. #endif
  900. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  901. };
  902. typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
  903. /*
  904. * Representation of decryption/encryption transformations on records
  905. *
  906. * There are the following general types of record transformations:
  907. * - Stream transformations (TLS versions == 1.2 only)
  908. * Transformation adding a MAC and applying a stream-cipher
  909. * to the authenticated message.
  910. * - CBC block cipher transformations ([D]TLS versions == 1.2 only)
  911. * For TLS 1.2, no IV is generated at key extraction time, but every
  912. * encrypted record is explicitly prefixed by the IV with which it was
  913. * encrypted.
  914. * - AEAD transformations ([D]TLS versions == 1.2 only)
  915. * These come in two fundamentally different versions, the first one
  916. * used in TLS 1.2, excluding ChaChaPoly ciphersuites, and the second
  917. * one used for ChaChaPoly ciphersuites in TLS 1.2 as well as for TLS 1.3.
  918. * In the first transformation, the IV to be used for a record is obtained
  919. * as the concatenation of an explicit, static 4-byte IV and the 8-byte
  920. * record sequence number, and explicitly prepending this sequence number
  921. * to the encrypted record. In contrast, in the second transformation
  922. * the IV is obtained by XOR'ing a static IV obtained at key extraction
  923. * time with the 8-byte record sequence number, without prepending the
  924. * latter to the encrypted record.
  925. *
  926. * Additionally, DTLS 1.2 + CID as well as TLS 1.3 use an inner plaintext
  927. * which allows to add flexible length padding and to hide a record's true
  928. * content type.
  929. *
  930. * In addition to type and version, the following parameters are relevant:
  931. * - The symmetric cipher algorithm to be used.
  932. * - The (static) encryption/decryption keys for the cipher.
  933. * - For stream/CBC, the type of message digest to be used.
  934. * - For stream/CBC, (static) encryption/decryption keys for the digest.
  935. * - For AEAD transformations, the size (potentially 0) of an explicit,
  936. * random initialization vector placed in encrypted records.
  937. * - For some transformations (currently AEAD) an implicit IV. It is static
  938. * and (if present) is combined with the explicit IV in a transformation-
  939. * -dependent way (e.g. appending in TLS 1.2 and XOR'ing in TLS 1.3).
  940. * - For stream/CBC, a flag determining the order of encryption and MAC.
  941. * - The details of the transformation depend on the SSL/TLS version.
  942. * - The length of the authentication tag.
  943. *
  944. * The struct below refines this abstract view as follows:
  945. * - The cipher underlying the transformation is managed in
  946. * cipher contexts cipher_ctx_{enc/dec}, which must have the
  947. * same cipher type. The mode of these cipher contexts determines
  948. * the type of the transformation in the sense above: e.g., if
  949. * the type is MBEDTLS_CIPHER_AES_256_CBC resp. MBEDTLS_CIPHER_AES_192_GCM
  950. * then the transformation has type CBC resp. AEAD.
  951. * - The cipher keys are never stored explicitly but
  952. * are maintained within cipher_ctx_{enc/dec}.
  953. * - For stream/CBC transformations, the message digest contexts
  954. * used for the MAC's are stored in md_ctx_{enc/dec}. These contexts
  955. * are unused for AEAD transformations.
  956. * - For stream/CBC transformations, the MAC keys are not stored explicitly
  957. * but maintained within md_ctx_{enc/dec}.
  958. * - The mac_enc and mac_dec fields are unused for EAD transformations.
  959. * - For transformations using an implicit IV maintained within
  960. * the transformation context, its contents are stored within
  961. * iv_{enc/dec}.
  962. * - The value of ivlen indicates the length of the IV.
  963. * This is redundant in case of stream/CBC transformations
  964. * which always use 0 resp. the cipher's block length as the
  965. * IV length, but is needed for AEAD ciphers and may be
  966. * different from the underlying cipher's block length
  967. * in this case.
  968. * - The field fixed_ivlen is nonzero for AEAD transformations only
  969. * and indicates the length of the static part of the IV which is
  970. * constant throughout the communication, and which is stored in
  971. * the first fixed_ivlen bytes of the iv_{enc/dec} arrays.
  972. * - tls_version denotes the 2-byte TLS version
  973. * - For stream/CBC transformations, maclen denotes the length of the
  974. * authentication tag, while taglen is unused and 0.
  975. * - For AEAD transformations, taglen denotes the length of the
  976. * authentication tag, while maclen is unused and 0.
  977. * - For CBC transformations, encrypt_then_mac determines the
  978. * order of encryption and authentication. This field is unused
  979. * in other transformations.
  980. *
  981. */
  982. struct mbedtls_ssl_transform {
  983. /*
  984. * Session specific crypto layer
  985. */
  986. size_t minlen; /*!< min. ciphertext length */
  987. size_t ivlen; /*!< IV length */
  988. size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
  989. size_t maclen; /*!< MAC(CBC) len */
  990. size_t taglen; /*!< TAG(AEAD) len */
  991. unsigned char iv_enc[16]; /*!< IV (encryption) */
  992. unsigned char iv_dec[16]; /*!< IV (decryption) */
  993. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  994. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  995. mbedtls_svc_key_id_t psa_mac_enc; /*!< MAC (encryption) */
  996. mbedtls_svc_key_id_t psa_mac_dec; /*!< MAC (decryption) */
  997. psa_algorithm_t psa_mac_alg; /*!< psa MAC algorithm */
  998. #else
  999. mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
  1000. mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
  1001. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1002. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1003. int encrypt_then_mac; /*!< flag for EtM activation */
  1004. #endif
  1005. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
  1006. mbedtls_ssl_protocol_version tls_version;
  1007. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1008. mbedtls_svc_key_id_t psa_key_enc; /*!< psa encryption key */
  1009. mbedtls_svc_key_id_t psa_key_dec; /*!< psa decryption key */
  1010. psa_algorithm_t psa_alg; /*!< psa algorithm */
  1011. #else
  1012. mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
  1013. mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
  1014. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1015. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1016. uint8_t in_cid_len;
  1017. uint8_t out_cid_len;
  1018. unsigned char in_cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
  1019. unsigned char out_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX];
  1020. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1021. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1022. /* We need the Hello random bytes in order to re-derive keys from the
  1023. * Master Secret and other session info,
  1024. * see ssl_tls12_populate_transform() */
  1025. unsigned char randbytes[MBEDTLS_SERVER_HELLO_RANDOM_LEN +
  1026. MBEDTLS_CLIENT_HELLO_RANDOM_LEN];
  1027. /*!< ServerHello.random+ClientHello.random */
  1028. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  1029. };
  1030. /*
  1031. * Return 1 if the transform uses an AEAD cipher, 0 otherwise.
  1032. * Equivalently, return 0 if a separate MAC is used, 1 otherwise.
  1033. */
  1034. static inline int mbedtls_ssl_transform_uses_aead(
  1035. const mbedtls_ssl_transform *transform)
  1036. {
  1037. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  1038. return transform->maclen == 0 && transform->taglen != 0;
  1039. #else
  1040. (void) transform;
  1041. return 1;
  1042. #endif
  1043. }
  1044. /*
  1045. * Internal representation of record frames
  1046. *
  1047. * Instances come in two flavors:
  1048. * (1) Encrypted
  1049. * These always have data_offset = 0
  1050. * (2) Unencrypted
  1051. * These have data_offset set to the amount of
  1052. * pre-expansion during record protection. Concretely,
  1053. * this is the length of the fixed part of the explicit IV
  1054. * used for encryption, or 0 if no explicit IV is used
  1055. * (e.g. for stream ciphers).
  1056. *
  1057. * The reason for the data_offset in the unencrypted case
  1058. * is to allow for in-place conversion of an unencrypted to
  1059. * an encrypted record. If the offset wasn't included, the
  1060. * encrypted content would need to be shifted afterwards to
  1061. * make space for the fixed IV.
  1062. *
  1063. */
  1064. #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
  1065. #define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_OUT_LEN_MAX
  1066. #else
  1067. #define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_IN_LEN_MAX
  1068. #endif
  1069. typedef struct {
  1070. uint8_t ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; /* In TLS: The implicit record sequence number.
  1071. * In DTLS: The 2-byte epoch followed by
  1072. * the 6-byte sequence number.
  1073. * This is stored as a raw big endian byte array
  1074. * as opposed to a uint64_t because we rarely
  1075. * need to perform arithmetic on this, but do
  1076. * need it as a Byte array for the purpose of
  1077. * MAC computations. */
  1078. uint8_t type; /* The record content type. */
  1079. uint8_t ver[2]; /* SSL/TLS version as present on the wire.
  1080. * Convert to internal presentation of versions
  1081. * using mbedtls_ssl_read_version() and
  1082. * mbedtls_ssl_write_version().
  1083. * Keep wire-format for MAC computations. */
  1084. unsigned char *buf; /* Memory buffer enclosing the record content */
  1085. size_t buf_len; /* Buffer length */
  1086. size_t data_offset; /* Offset of record content */
  1087. size_t data_len; /* Length of record content */
  1088. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1089. uint8_t cid_len; /* Length of the CID (0 if not present) */
  1090. unsigned char cid[MBEDTLS_SSL_CID_LEN_MAX]; /* The CID */
  1091. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1092. } mbedtls_record;
  1093. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1094. /*
  1095. * List of certificate + private key pairs
  1096. */
  1097. struct mbedtls_ssl_key_cert {
  1098. mbedtls_x509_crt *cert; /*!< cert */
  1099. mbedtls_pk_context *key; /*!< private key */
  1100. mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
  1101. };
  1102. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1103. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1104. /*
  1105. * List of handshake messages kept around for resending
  1106. */
  1107. struct mbedtls_ssl_flight_item {
  1108. unsigned char *p; /*!< message, including handshake headers */
  1109. size_t len; /*!< length of p */
  1110. unsigned char type; /*!< type of the message: handshake or CCS */
  1111. mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
  1112. };
  1113. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1114. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1115. /**
  1116. * \brief Given an SSL context and its associated configuration, write the TLS
  1117. * 1.2 specific extensions of the ClientHello message.
  1118. *
  1119. * \param[in] ssl SSL context
  1120. * \param[in] buf Base address of the buffer where to write the extensions
  1121. * \param[in] end End address of the buffer where to write the extensions
  1122. * \param uses_ec Whether one proposed ciphersuite uses an elliptic curve
  1123. * (<> 0) or not ( 0 ).
  1124. * \param[out] out_len Length of the data written into the buffer \p buf
  1125. */
  1126. MBEDTLS_CHECK_RETURN_CRITICAL
  1127. int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
  1128. unsigned char *buf,
  1129. const unsigned char *end,
  1130. int uses_ec,
  1131. size_t *out_len);
  1132. #endif
  1133. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1134. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1135. /**
  1136. * \brief Find the preferred hash for a given signature algorithm.
  1137. *
  1138. * \param[in] ssl SSL context
  1139. * \param[in] sig_alg A signature algorithm identifier as defined in the
  1140. * TLS 1.2 SignatureAlgorithm enumeration.
  1141. *
  1142. * \return The preferred hash algorithm for \p sig_alg. It is a hash algorithm
  1143. * identifier as defined in the TLS 1.2 HashAlgorithm enumeration.
  1144. */
  1145. unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
  1146. mbedtls_ssl_context *ssl,
  1147. unsigned int sig_alg);
  1148. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  1149. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1150. /**
  1151. * \brief Free referenced items in an SSL transform context and clear
  1152. * memory
  1153. *
  1154. * \param transform SSL transform context
  1155. */
  1156. void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform);
  1157. /**
  1158. * \brief Free referenced items in an SSL handshake context and clear
  1159. * memory
  1160. *
  1161. * \param ssl SSL context
  1162. */
  1163. void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl);
  1164. /* set inbound transform of ssl context */
  1165. void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
  1166. mbedtls_ssl_transform *transform);
  1167. /* set outbound transform of ssl context */
  1168. void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
  1169. mbedtls_ssl_transform *transform);
  1170. MBEDTLS_CHECK_RETURN_CRITICAL
  1171. int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl);
  1172. MBEDTLS_CHECK_RETURN_CRITICAL
  1173. int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl);
  1174. void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl);
  1175. static inline void mbedtls_ssl_handshake_set_state(mbedtls_ssl_context *ssl,
  1176. mbedtls_ssl_states state)
  1177. {
  1178. ssl->state = (int) state;
  1179. }
  1180. MBEDTLS_CHECK_RETURN_CRITICAL
  1181. int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl);
  1182. MBEDTLS_CHECK_RETURN_CRITICAL
  1183. int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
  1184. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1185. MBEDTLS_CHECK_RETURN_CRITICAL
  1186. int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl);
  1187. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1188. MBEDTLS_CHECK_RETURN_CRITICAL
  1189. int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl);
  1190. MBEDTLS_CHECK_RETURN_CRITICAL
  1191. int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl);
  1192. MBEDTLS_CHECK_RETURN_CRITICAL
  1193. int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
  1194. /**
  1195. * \brief Update record layer
  1196. *
  1197. * This function roughly separates the implementation
  1198. * of the logic of (D)TLS from the implementation
  1199. * of the secure transport.
  1200. *
  1201. * \param ssl The SSL context to use.
  1202. * \param update_hs_digest This indicates if the handshake digest
  1203. * should be automatically updated in case
  1204. * a handshake message is found.
  1205. *
  1206. * \return 0 or non-zero error code.
  1207. *
  1208. * \note A clarification on what is called 'record layer' here
  1209. * is in order, as many sensible definitions are possible:
  1210. *
  1211. * The record layer takes as input an untrusted underlying
  1212. * transport (stream or datagram) and transforms it into
  1213. * a serially multiplexed, secure transport, which
  1214. * conceptually provides the following:
  1215. *
  1216. * (1) Three datagram based, content-agnostic transports
  1217. * for handshake, alert and CCS messages.
  1218. * (2) One stream- or datagram-based transport
  1219. * for application data.
  1220. * (3) Functionality for changing the underlying transform
  1221. * securing the contents.
  1222. *
  1223. * The interface to this functionality is given as follows:
  1224. *
  1225. * a Updating
  1226. * [Currently implemented by mbedtls_ssl_read_record]
  1227. *
  1228. * Check if and on which of the four 'ports' data is pending:
  1229. * Nothing, a controlling datagram of type (1), or application
  1230. * data (2). In any case data is present, internal buffers
  1231. * provide access to the data for the user to process it.
  1232. * Consumption of type (1) datagrams is done automatically
  1233. * on the next update, invalidating that the internal buffers
  1234. * for previous datagrams, while consumption of application
  1235. * data (2) is user-controlled.
  1236. *
  1237. * b Reading of application data
  1238. * [Currently manual adaption of ssl->in_offt pointer]
  1239. *
  1240. * As mentioned in the last paragraph, consumption of data
  1241. * is different from the automatic consumption of control
  1242. * datagrams (1) because application data is treated as a stream.
  1243. *
  1244. * c Tracking availability of application data
  1245. * [Currently manually through decreasing ssl->in_msglen]
  1246. *
  1247. * For efficiency and to retain datagram semantics for
  1248. * application data in case of DTLS, the record layer
  1249. * provides functionality for checking how much application
  1250. * data is still available in the internal buffer.
  1251. *
  1252. * d Changing the transformation securing the communication.
  1253. *
  1254. * Given an opaque implementation of the record layer in the
  1255. * above sense, it should be possible to implement the logic
  1256. * of (D)TLS on top of it without the need to know anything
  1257. * about the record layer's internals. This is done e.g.
  1258. * in all the handshake handling functions, and in the
  1259. * application data reading function mbedtls_ssl_read.
  1260. *
  1261. * \note The above tries to give a conceptual picture of the
  1262. * record layer, but the current implementation deviates
  1263. * from it in some places. For example, our implementation of
  1264. * the update functionality through mbedtls_ssl_read_record
  1265. * discards datagrams depending on the current state, which
  1266. * wouldn't fall under the record layer's responsibility
  1267. * following the above definition.
  1268. *
  1269. */
  1270. MBEDTLS_CHECK_RETURN_CRITICAL
  1271. int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
  1272. unsigned update_hs_digest);
  1273. MBEDTLS_CHECK_RETURN_CRITICAL
  1274. int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want);
  1275. /*
  1276. * Write handshake message header
  1277. */
  1278. MBEDTLS_CHECK_RETURN_CRITICAL
  1279. int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned char hs_type,
  1280. unsigned char **buf, size_t *buf_len);
  1281. MBEDTLS_CHECK_RETURN_CRITICAL
  1282. int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
  1283. int update_checksum,
  1284. int force_flush);
  1285. static inline int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl)
  1286. {
  1287. return mbedtls_ssl_write_handshake_msg_ext(ssl, 1 /* update checksum */, 1 /* force flush */);
  1288. }
  1289. /*
  1290. * Write handshake message tail
  1291. */
  1292. MBEDTLS_CHECK_RETURN_CRITICAL
  1293. int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
  1294. size_t buf_len, size_t msg_len);
  1295. MBEDTLS_CHECK_RETURN_CRITICAL
  1296. int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush);
  1297. MBEDTLS_CHECK_RETURN_CRITICAL
  1298. int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl);
  1299. MBEDTLS_CHECK_RETURN_CRITICAL
  1300. int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl);
  1301. MBEDTLS_CHECK_RETURN_CRITICAL
  1302. int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl);
  1303. MBEDTLS_CHECK_RETURN_CRITICAL
  1304. int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl);
  1305. MBEDTLS_CHECK_RETURN_CRITICAL
  1306. int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl);
  1307. MBEDTLS_CHECK_RETURN_CRITICAL
  1308. int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl);
  1309. MBEDTLS_CHECK_RETURN_CRITICAL
  1310. int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl);
  1311. void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
  1312. const mbedtls_ssl_ciphersuite_t *ciphersuite_info);
  1313. /*
  1314. * Update checksum of handshake messages.
  1315. */
  1316. MBEDTLS_CHECK_RETURN_CRITICAL
  1317. int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
  1318. unsigned hs_type,
  1319. unsigned char const *msg,
  1320. size_t msg_len);
  1321. MBEDTLS_CHECK_RETURN_CRITICAL
  1322. int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
  1323. unsigned hs_type,
  1324. size_t total_hs_len);
  1325. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1326. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  1327. MBEDTLS_CHECK_RETURN_CRITICAL
  1328. int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl,
  1329. mbedtls_key_exchange_type_t key_ex);
  1330. #endif /* !MBEDTLS_USE_PSA_CRYPTO */
  1331. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1332. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
  1333. #if defined(MBEDTLS_SSL_CLI_C) || defined(MBEDTLS_SSL_SRV_C)
  1334. MBEDTLS_CHECK_RETURN_CRITICAL
  1335. int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf);
  1336. #endif
  1337. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1338. /**
  1339. * Get the first defined opaque PSK by order of precedence:
  1340. * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
  1341. * callback
  1342. * 2. static PSK configured by \c mbedtls_ssl_conf_psk_opaque()
  1343. * Return an opaque PSK
  1344. */
  1345. static inline mbedtls_svc_key_id_t mbedtls_ssl_get_opaque_psk(
  1346. const mbedtls_ssl_context *ssl)
  1347. {
  1348. if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
  1349. return ssl->handshake->psk_opaque;
  1350. }
  1351. if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
  1352. return ssl->conf->psk_opaque;
  1353. }
  1354. return MBEDTLS_SVC_KEY_ID_INIT;
  1355. }
  1356. #else
  1357. /**
  1358. * Get the first defined PSK by order of precedence:
  1359. * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
  1360. * 2. static PSK configured by \c mbedtls_ssl_conf_psk()
  1361. * Return a code and update the pair (PSK, PSK length) passed to this function
  1362. */
  1363. static inline int mbedtls_ssl_get_psk(const mbedtls_ssl_context *ssl,
  1364. const unsigned char **psk, size_t *psk_len)
  1365. {
  1366. if (ssl->handshake->psk != NULL && ssl->handshake->psk_len > 0) {
  1367. *psk = ssl->handshake->psk;
  1368. *psk_len = ssl->handshake->psk_len;
  1369. } else if (ssl->conf->psk != NULL && ssl->conf->psk_len > 0) {
  1370. *psk = ssl->conf->psk;
  1371. *psk_len = ssl->conf->psk_len;
  1372. } else {
  1373. *psk = NULL;
  1374. *psk_len = 0;
  1375. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  1376. }
  1377. return 0;
  1378. }
  1379. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1380. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
  1381. #if defined(MBEDTLS_PK_C)
  1382. unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk);
  1383. unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type);
  1384. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig);
  1385. #endif
  1386. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash);
  1387. unsigned char mbedtls_ssl_hash_from_md_alg(int md);
  1388. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1389. MBEDTLS_CHECK_RETURN_CRITICAL
  1390. int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md);
  1391. #endif
  1392. MBEDTLS_CHECK_RETURN_CRITICAL
  1393. int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id);
  1394. #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
  1395. MBEDTLS_CHECK_RETURN_CRITICAL
  1396. int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id);
  1397. #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
  1398. /**
  1399. * \brief Return PSA EC info for the specified TLS ID.
  1400. *
  1401. * \param tls_id The TLS ID to look for
  1402. * \param type If the TLD ID is supported, then proper \c psa_key_type_t
  1403. * value is returned here. Can be NULL.
  1404. * \param bits If the TLD ID is supported, then proper bit size is returned
  1405. * here. Can be NULL.
  1406. * \return PSA_SUCCESS if the TLS ID is supported,
  1407. * PSA_ERROR_NOT_SUPPORTED otherwise
  1408. *
  1409. * \note If either \c family or \c bits parameters are NULL, then
  1410. * the corresponding value is not returned.
  1411. * The function can be called with both parameters as NULL
  1412. * simply to check if a specific TLS ID is supported.
  1413. */
  1414. int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
  1415. psa_key_type_t *type,
  1416. size_t *bits);
  1417. /**
  1418. * \brief Return \c mbedtls_ecp_group_id for the specified TLS ID.
  1419. *
  1420. * \param tls_id The TLS ID to look for
  1421. * \return Proper \c mbedtls_ecp_group_id if the TLS ID is supported,
  1422. * or MBEDTLS_ECP_DP_NONE otherwise
  1423. */
  1424. mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id);
  1425. /**
  1426. * \brief Return TLS ID for the specified \c mbedtls_ecp_group_id.
  1427. *
  1428. * \param grp_id The \c mbedtls_ecp_group_id ID to look for
  1429. * \return Proper TLS ID if the \c mbedtls_ecp_group_id is supported,
  1430. * or 0 otherwise
  1431. */
  1432. uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id);
  1433. #if defined(MBEDTLS_DEBUG_C)
  1434. /**
  1435. * \brief Return EC's name for the specified TLS ID.
  1436. *
  1437. * \param tls_id The TLS ID to look for
  1438. * \return A pointer to a const string with the proper name. If TLS
  1439. * ID is not supported, a NULL pointer is returned instead.
  1440. */
  1441. const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id);
  1442. #endif
  1443. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1444. static inline mbedtls_ssl_srtp_profile mbedtls_ssl_check_srtp_profile_value
  1445. (const uint16_t srtp_profile_value)
  1446. {
  1447. switch (srtp_profile_value) {
  1448. case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
  1449. case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32:
  1450. case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80:
  1451. case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32:
  1452. return srtp_profile_value;
  1453. default: break;
  1454. }
  1455. return MBEDTLS_TLS_SRTP_UNSET;
  1456. }
  1457. #endif
  1458. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1459. static inline mbedtls_pk_context *mbedtls_ssl_own_key(mbedtls_ssl_context *ssl)
  1460. {
  1461. mbedtls_ssl_key_cert *key_cert;
  1462. if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
  1463. key_cert = ssl->handshake->key_cert;
  1464. } else {
  1465. key_cert = ssl->conf->key_cert;
  1466. }
  1467. return key_cert == NULL ? NULL : key_cert->key;
  1468. }
  1469. static inline mbedtls_x509_crt *mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
  1470. {
  1471. mbedtls_ssl_key_cert *key_cert;
  1472. if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
  1473. key_cert = ssl->handshake->key_cert;
  1474. } else {
  1475. key_cert = ssl->conf->key_cert;
  1476. }
  1477. return key_cert == NULL ? NULL : key_cert->cert;
  1478. }
  1479. /*
  1480. * Verify a certificate.
  1481. *
  1482. * [in/out] ssl: misc. things read
  1483. * ssl->session_negotiate->verify_result updated
  1484. * [in] authmode: one of MBEDTLS_SSL_VERIFY_{NONE,OPTIONAL,REQUIRED}
  1485. * [in] chain: the certificate chain to verify (ie the peer's chain)
  1486. * [in] ciphersuite_info: For TLS 1.2, this session's ciphersuite;
  1487. * for TLS 1.3, may be left NULL.
  1488. * [in] rs_ctx: restart context if restartable ECC is in use;
  1489. * leave NULL for no restartable behaviour.
  1490. *
  1491. * Return:
  1492. * - 0 if the handshake should continue. Depending on the
  1493. * authmode it means:
  1494. * - REQUIRED: the certificate was found to be valid, trusted & acceptable.
  1495. * ssl->session_negotiate->verify_result is 0.
  1496. * - OPTIONAL: the certificate may or may not be acceptable, but
  1497. * ssl->session_negotiate->verify_result was updated with the result.
  1498. * - NONE: the certificate wasn't even checked.
  1499. * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED or MBEDTLS_ERR_SSL_BAD_CERTIFICATE if
  1500. * the certificate was found to be invalid/untrusted/unacceptable and the
  1501. * handshake should be aborted (can only happen with REQUIRED).
  1502. * - another error code if another error happened (out-of-memory, etc.)
  1503. */
  1504. MBEDTLS_CHECK_RETURN_CRITICAL
  1505. int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
  1506. int authmode,
  1507. mbedtls_x509_crt *chain,
  1508. const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
  1509. void *rs_ctx);
  1510. /*
  1511. * Check usage of a certificate wrt usage extensions:
  1512. * keyUsage and extendedKeyUsage.
  1513. * (Note: nSCertType is deprecated and not standard, we don't check it.)
  1514. *
  1515. * Note: if tls_version is 1.3, ciphersuite is ignored and can be NULL.
  1516. *
  1517. * Note: recv_endpoint is the receiver's endpoint.
  1518. *
  1519. * Return 0 if everything is OK, -1 if not.
  1520. */
  1521. MBEDTLS_CHECK_RETURN_CRITICAL
  1522. int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
  1523. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  1524. int recv_endpoint,
  1525. mbedtls_ssl_protocol_version tls_version,
  1526. uint32_t *flags);
  1527. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1528. void mbedtls_ssl_write_version(unsigned char version[2], int transport,
  1529. mbedtls_ssl_protocol_version tls_version);
  1530. uint16_t mbedtls_ssl_read_version(const unsigned char version[2],
  1531. int transport);
  1532. static inline size_t mbedtls_ssl_in_hdr_len(const mbedtls_ssl_context *ssl)
  1533. {
  1534. #if !defined(MBEDTLS_SSL_PROTO_DTLS)
  1535. ((void) ssl);
  1536. #endif
  1537. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1538. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1539. return 13;
  1540. } else
  1541. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1542. {
  1543. return 5;
  1544. }
  1545. }
  1546. static inline size_t mbedtls_ssl_out_hdr_len(const mbedtls_ssl_context *ssl)
  1547. {
  1548. return (size_t) (ssl->out_iv - ssl->out_hdr);
  1549. }
  1550. static inline size_t mbedtls_ssl_hs_hdr_len(const mbedtls_ssl_context *ssl)
  1551. {
  1552. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1553. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1554. return 12;
  1555. }
  1556. #else
  1557. ((void) ssl);
  1558. #endif
  1559. return 4;
  1560. }
  1561. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1562. void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl);
  1563. void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl);
  1564. MBEDTLS_CHECK_RETURN_CRITICAL
  1565. int mbedtls_ssl_resend(mbedtls_ssl_context *ssl);
  1566. MBEDTLS_CHECK_RETURN_CRITICAL
  1567. int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl);
  1568. #endif
  1569. /* Visible for testing purposes only */
  1570. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1571. MBEDTLS_CHECK_RETURN_CRITICAL
  1572. int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl);
  1573. void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl);
  1574. #endif
  1575. MBEDTLS_CHECK_RETURN_CRITICAL
  1576. int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
  1577. const mbedtls_ssl_session *src);
  1578. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1579. /* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */
  1580. MBEDTLS_CHECK_RETURN_CRITICAL
  1581. int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
  1582. unsigned char *hash, size_t *hashlen,
  1583. unsigned char *data, size_t data_len,
  1584. mbedtls_md_type_t md_alg);
  1585. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1586. #ifdef __cplusplus
  1587. }
  1588. #endif
  1589. void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform);
  1590. MBEDTLS_CHECK_RETURN_CRITICAL
  1591. int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
  1592. mbedtls_ssl_transform *transform,
  1593. mbedtls_record *rec,
  1594. int (*f_rng)(void *, unsigned char *, size_t),
  1595. void *p_rng);
  1596. MBEDTLS_CHECK_RETURN_CRITICAL
  1597. int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
  1598. mbedtls_ssl_transform *transform,
  1599. mbedtls_record *rec);
  1600. /* Length of the "epoch" field in the record header */
  1601. static inline size_t mbedtls_ssl_ep_len(const mbedtls_ssl_context *ssl)
  1602. {
  1603. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1604. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1605. return 2;
  1606. }
  1607. #else
  1608. ((void) ssl);
  1609. #endif
  1610. return 0;
  1611. }
  1612. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1613. MBEDTLS_CHECK_RETURN_CRITICAL
  1614. int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl);
  1615. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1616. void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs);
  1617. MBEDTLS_CHECK_RETURN_CRITICAL
  1618. int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl);
  1619. void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl);
  1620. void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
  1621. mbedtls_ssl_transform *transform);
  1622. void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
  1623. MBEDTLS_CHECK_RETURN_CRITICAL
  1624. int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial);
  1625. void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
  1626. int partial);
  1627. /*
  1628. * Send pending alert
  1629. */
  1630. MBEDTLS_CHECK_RETURN_CRITICAL
  1631. int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl);
  1632. /*
  1633. * Set pending fatal alert flag.
  1634. */
  1635. void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
  1636. unsigned char alert_type,
  1637. int alert_reason);
  1638. /* Alias of mbedtls_ssl_pend_fatal_alert */
  1639. #define MBEDTLS_SSL_PEND_FATAL_ALERT(type, user_return_value) \
  1640. mbedtls_ssl_pend_fatal_alert(ssl, type, user_return_value)
  1641. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1642. void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl);
  1643. #endif
  1644. void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl);
  1645. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1646. MBEDTLS_CHECK_RETURN_CRITICAL
  1647. int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl);
  1648. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1649. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1650. size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl);
  1651. void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl);
  1652. void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight);
  1653. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1654. /**
  1655. * ssl utils functions for checking configuration.
  1656. */
  1657. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  1658. static inline int mbedtls_ssl_conf_is_tls13_only(const mbedtls_ssl_config *conf)
  1659. {
  1660. return conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
  1661. conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3;
  1662. }
  1663. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  1664. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1665. static inline int mbedtls_ssl_conf_is_tls12_only(const mbedtls_ssl_config *conf)
  1666. {
  1667. return conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
  1668. conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_2;
  1669. }
  1670. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1671. static inline int mbedtls_ssl_conf_is_tls13_enabled(const mbedtls_ssl_config *conf)
  1672. {
  1673. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  1674. return conf->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
  1675. conf->max_tls_version >= MBEDTLS_SSL_VERSION_TLS1_3;
  1676. #else
  1677. ((void) conf);
  1678. return 0;
  1679. #endif
  1680. }
  1681. static inline int mbedtls_ssl_conf_is_tls12_enabled(const mbedtls_ssl_config *conf)
  1682. {
  1683. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1684. return conf->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 &&
  1685. conf->max_tls_version >= MBEDTLS_SSL_VERSION_TLS1_2;
  1686. #else
  1687. ((void) conf);
  1688. return 0;
  1689. #endif
  1690. }
  1691. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
  1692. static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13(const mbedtls_ssl_config *conf)
  1693. {
  1694. return conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
  1695. conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3;
  1696. }
  1697. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */
  1698. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  1699. /** \brief Initialize the PSA crypto subsystem if necessary.
  1700. *
  1701. * Call this function before doing any cryptography in a TLS 1.3 handshake.
  1702. *
  1703. * This is necessary in Mbed TLS 3.x for backward compatibility.
  1704. * Up to Mbed TLS 3.5, in the default configuration, you could perform
  1705. * a TLS connection with default parameters without having called
  1706. * psa_crypto_init(), since the TLS layer only supported TLS 1.2 and
  1707. * did not use PSA crypto. (TLS 1.2 only uses PSA crypto if
  1708. * MBEDTLS_USE_PSA_CRYPTO is enabled, which is not the case in the default
  1709. * configuration.) Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled
  1710. * by default, and the TLS 1.3 layer uses PSA crypto. This means that
  1711. * applications that are not otherwise using PSA crypto and that worked
  1712. * with Mbed TLS 3.5 started failing in TLS 3.6.0 if they connected to
  1713. * a peer that supports TLS 1.3. See
  1714. * https://github.com/Mbed-TLS/mbedtls/issues/9072
  1715. */
  1716. int mbedtls_ssl_tls13_crypto_init(mbedtls_ssl_context *ssl);
  1717. extern const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
  1718. MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  1719. MBEDTLS_CHECK_RETURN_CRITICAL
  1720. int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl);
  1721. MBEDTLS_CHECK_RETURN_CRITICAL
  1722. int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl);
  1723. void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl);
  1724. /**
  1725. * \brief Given an SSL context and its associated configuration, write the TLS
  1726. * 1.3 specific extensions of the ClientHello message.
  1727. *
  1728. * \param[in] ssl SSL context
  1729. * \param[in] buf Base address of the buffer where to write the extensions
  1730. * \param[in] end End address of the buffer where to write the extensions
  1731. * \param[out] out_len Length of the data written into the buffer \p buf
  1732. */
  1733. MBEDTLS_CHECK_RETURN_CRITICAL
  1734. int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
  1735. unsigned char *buf,
  1736. unsigned char *end,
  1737. size_t *out_len);
  1738. /**
  1739. * \brief TLS 1.3 client side state machine entry
  1740. *
  1741. * \param ssl SSL context
  1742. */
  1743. MBEDTLS_CHECK_RETURN_CRITICAL
  1744. int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl);
  1745. /**
  1746. * \brief TLS 1.3 server side state machine entry
  1747. *
  1748. * \param ssl SSL context
  1749. */
  1750. MBEDTLS_CHECK_RETURN_CRITICAL
  1751. int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl);
  1752. /*
  1753. * Helper functions around key exchange modes.
  1754. */
  1755. static inline int mbedtls_ssl_conf_tls13_is_kex_mode_enabled(mbedtls_ssl_context *ssl,
  1756. int kex_mode_mask)
  1757. {
  1758. return (ssl->conf->tls13_kex_modes & kex_mode_mask) != 0;
  1759. }
  1760. static inline int mbedtls_ssl_conf_tls13_is_psk_enabled(mbedtls_ssl_context *ssl)
  1761. {
  1762. return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
  1763. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK);
  1764. }
  1765. static inline int mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(mbedtls_ssl_context *ssl)
  1766. {
  1767. return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
  1768. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL);
  1769. }
  1770. static inline int mbedtls_ssl_conf_tls13_is_ephemeral_enabled(mbedtls_ssl_context *ssl)
  1771. {
  1772. return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
  1773. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL);
  1774. }
  1775. static inline int mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(mbedtls_ssl_context *ssl)
  1776. {
  1777. return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
  1778. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
  1779. }
  1780. static inline int mbedtls_ssl_conf_tls13_is_some_psk_enabled(mbedtls_ssl_context *ssl)
  1781. {
  1782. return mbedtls_ssl_conf_tls13_is_kex_mode_enabled(ssl,
  1783. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
  1784. }
  1785. #if defined(MBEDTLS_SSL_SRV_C) && \
  1786. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1787. /**
  1788. * Given a list of key exchange modes, check if at least one of them is
  1789. * supported by peer.
  1790. *
  1791. * \param[in] ssl SSL context
  1792. * \param kex_modes_mask Mask of the key exchange modes to check
  1793. *
  1794. * \return Non-zero if at least one of the key exchange modes is supported by
  1795. * the peer, otherwise \c 0.
  1796. */
  1797. static inline int mbedtls_ssl_tls13_is_kex_mode_supported(mbedtls_ssl_context *ssl,
  1798. int kex_modes_mask)
  1799. {
  1800. return (ssl->handshake->tls13_kex_modes & kex_modes_mask) != 0;
  1801. }
  1802. static inline int mbedtls_ssl_tls13_is_psk_supported(mbedtls_ssl_context *ssl)
  1803. {
  1804. return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
  1805. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK);
  1806. }
  1807. static inline int mbedtls_ssl_tls13_is_psk_ephemeral_supported(
  1808. mbedtls_ssl_context *ssl)
  1809. {
  1810. return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
  1811. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL);
  1812. }
  1813. static inline int mbedtls_ssl_tls13_is_ephemeral_supported(mbedtls_ssl_context *ssl)
  1814. {
  1815. return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
  1816. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL);
  1817. }
  1818. static inline int mbedtls_ssl_tls13_is_some_ephemeral_supported(mbedtls_ssl_context *ssl)
  1819. {
  1820. return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
  1821. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
  1822. }
  1823. static inline int mbedtls_ssl_tls13_is_some_psk_supported(mbedtls_ssl_context *ssl)
  1824. {
  1825. return mbedtls_ssl_tls13_is_kex_mode_supported(ssl,
  1826. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
  1827. }
  1828. #endif /* MBEDTLS_SSL_SRV_C &&
  1829. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  1830. /*
  1831. * Helper functions for extensions checking.
  1832. */
  1833. MBEDTLS_CHECK_RETURN_CRITICAL
  1834. int mbedtls_ssl_tls13_check_received_extension(
  1835. mbedtls_ssl_context *ssl,
  1836. int hs_msg_type,
  1837. unsigned int received_extension_type,
  1838. uint32_t hs_msg_allowed_extensions_mask);
  1839. static inline void mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  1840. mbedtls_ssl_context *ssl, unsigned int extension_type)
  1841. {
  1842. ssl->handshake->sent_extensions |=
  1843. mbedtls_ssl_get_extension_mask(extension_type);
  1844. }
  1845. /*
  1846. * Helper functions to check the selected key exchange mode.
  1847. */
  1848. static inline int mbedtls_ssl_tls13_key_exchange_mode_check(
  1849. mbedtls_ssl_context *ssl, int kex_mask)
  1850. {
  1851. return (ssl->handshake->key_exchange_mode & kex_mask) != 0;
  1852. }
  1853. static inline int mbedtls_ssl_tls13_key_exchange_mode_with_psk(
  1854. mbedtls_ssl_context *ssl)
  1855. {
  1856. return mbedtls_ssl_tls13_key_exchange_mode_check(ssl,
  1857. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
  1858. }
  1859. static inline int mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(
  1860. mbedtls_ssl_context *ssl)
  1861. {
  1862. return mbedtls_ssl_tls13_key_exchange_mode_check(ssl,
  1863. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ALL);
  1864. }
  1865. /*
  1866. * Fetch TLS 1.3 handshake message header
  1867. */
  1868. MBEDTLS_CHECK_RETURN_CRITICAL
  1869. int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
  1870. unsigned hs_type,
  1871. unsigned char **buf,
  1872. size_t *buf_len);
  1873. /**
  1874. * \brief Detect if a list of extensions contains a supported_versions
  1875. * extension or not.
  1876. *
  1877. * \param[in] ssl SSL context
  1878. * \param[in] buf Address of the first byte of the extensions vector.
  1879. * \param[in] end End of the buffer containing the list of extensions.
  1880. * \param[out] supported_versions_data If the extension is present, address of
  1881. * its first byte of data, NULL otherwise.
  1882. * \param[out] supported_versions_data_end If the extension is present, address
  1883. * of the first byte immediately
  1884. * following the extension data, NULL
  1885. * otherwise.
  1886. * \return 0 if the list of extensions does not contain a supported_versions
  1887. * extension.
  1888. * \return 1 if the list of extensions contains a supported_versions
  1889. * extension.
  1890. * \return A negative value if an error occurred while parsing the
  1891. * extensions.
  1892. */
  1893. MBEDTLS_CHECK_RETURN_CRITICAL
  1894. int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
  1895. mbedtls_ssl_context *ssl,
  1896. const unsigned char *buf, const unsigned char *end,
  1897. const unsigned char **supported_versions_data,
  1898. const unsigned char **supported_versions_data_end);
  1899. /*
  1900. * Handler of TLS 1.3 server certificate message
  1901. */
  1902. MBEDTLS_CHECK_RETURN_CRITICAL
  1903. int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl);
  1904. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  1905. /*
  1906. * Handler of TLS 1.3 write Certificate message
  1907. */
  1908. MBEDTLS_CHECK_RETURN_CRITICAL
  1909. int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl);
  1910. /*
  1911. * Handler of TLS 1.3 write Certificate Verify message
  1912. */
  1913. MBEDTLS_CHECK_RETURN_CRITICAL
  1914. int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl);
  1915. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  1916. /*
  1917. * Generic handler of Certificate Verify
  1918. */
  1919. MBEDTLS_CHECK_RETURN_CRITICAL
  1920. int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl);
  1921. /*
  1922. * Write of dummy-CCS's for middlebox compatibility
  1923. */
  1924. MBEDTLS_CHECK_RETURN_CRITICAL
  1925. int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl);
  1926. MBEDTLS_CHECK_RETURN_CRITICAL
  1927. int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl);
  1928. #if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
  1929. MBEDTLS_CHECK_RETURN_CRITICAL
  1930. int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
  1931. mbedtls_ssl_context *ssl,
  1932. uint16_t named_group,
  1933. unsigned char *buf,
  1934. unsigned char *end,
  1935. size_t *out_len);
  1936. #endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
  1937. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1938. int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
  1939. int in_new_session_ticket,
  1940. unsigned char *buf,
  1941. const unsigned char *end,
  1942. size_t *out_len);
  1943. int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
  1944. size_t early_data_len);
  1945. typedef enum {
  1946. /*
  1947. * The client has not sent the first ClientHello yet, the negotiation of early
  1948. * data has not started yet.
  1949. */
  1950. MBEDTLS_SSL_EARLY_DATA_STATE_IDLE,
  1951. /*
  1952. * In its ClientHello, the client has not included an early data indication
  1953. * extension.
  1954. */
  1955. MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT,
  1956. /*
  1957. * The client has sent an early data indication extension in its first
  1958. * ClientHello, it has not received the response (ServerHello or
  1959. * HelloRetryRequest) from the server yet. The transform to protect early data
  1960. * is not set either as for middlebox compatibility a dummy CCS may have to be
  1961. * sent in clear. Early data cannot be sent to the server yet.
  1962. */
  1963. MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT,
  1964. /*
  1965. * The client has sent an early data indication extension in its first
  1966. * ClientHello, it has not received the response (ServerHello or
  1967. * HelloRetryRequest) from the server yet. The transform to protect early data
  1968. * has been set and early data can be written now.
  1969. */
  1970. MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE,
  1971. /*
  1972. * The client has indicated the use of early data and the server has accepted
  1973. * it.
  1974. */
  1975. MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED,
  1976. /*
  1977. * The client has indicated the use of early data but the server has rejected
  1978. * it.
  1979. */
  1980. MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED,
  1981. /*
  1982. * The client has sent an early data indication extension in its first
  1983. * ClientHello, the server has accepted them and the client has received the
  1984. * server Finished message. It cannot send early data to the server anymore.
  1985. */
  1986. MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED,
  1987. } mbedtls_ssl_early_data_state;
  1988. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1989. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  1990. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  1991. /*
  1992. * Write Signature Algorithm extension
  1993. */
  1994. MBEDTLS_CHECK_RETURN_CRITICAL
  1995. int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
  1996. const unsigned char *end, size_t *out_len);
  1997. /*
  1998. * Parse TLS Signature Algorithm extension
  1999. */
  2000. MBEDTLS_CHECK_RETURN_CRITICAL
  2001. int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
  2002. const unsigned char *buf,
  2003. const unsigned char *end);
  2004. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  2005. /* Get handshake transcript */
  2006. MBEDTLS_CHECK_RETURN_CRITICAL
  2007. int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
  2008. const mbedtls_md_type_t md,
  2009. unsigned char *dst,
  2010. size_t dst_len,
  2011. size_t *olen);
  2012. /*
  2013. * Return supported groups.
  2014. *
  2015. * In future, invocations can be changed to ssl->conf->group_list
  2016. * when mbedtls_ssl_conf_curves() is deleted.
  2017. *
  2018. * ssl->handshake->group_list is either a translation of curve_list to IANA TLS group
  2019. * identifiers when mbedtls_ssl_conf_curves() has been used, or a pointer to
  2020. * ssl->conf->group_list when mbedtls_ssl_conf_groups() has been more recently invoked.
  2021. *
  2022. */
  2023. static inline const void *mbedtls_ssl_get_groups(const mbedtls_ssl_context *ssl)
  2024. {
  2025. #if defined(MBEDTLS_DEPRECATED_REMOVED) || !defined(MBEDTLS_ECP_C)
  2026. return ssl->conf->group_list;
  2027. #else
  2028. if ((ssl->handshake != NULL) && (ssl->handshake->group_list != NULL)) {
  2029. return ssl->handshake->group_list;
  2030. } else {
  2031. return ssl->conf->group_list;
  2032. }
  2033. #endif
  2034. }
  2035. /*
  2036. * Helper functions for NamedGroup.
  2037. */
  2038. static inline int mbedtls_ssl_tls12_named_group_is_ecdhe(uint16_t named_group)
  2039. {
  2040. /*
  2041. * RFC 8422 section 5.1.1
  2042. */
  2043. return named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X25519 ||
  2044. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1 ||
  2045. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1 ||
  2046. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1 ||
  2047. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X448 ||
  2048. /* Below deprecated curves should be removed with notice to users */
  2049. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1 ||
  2050. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1 ||
  2051. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1 ||
  2052. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1 ||
  2053. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1 ||
  2054. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 ||
  2055. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 ||
  2056. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1;
  2057. }
  2058. static inline int mbedtls_ssl_tls13_named_group_is_ecdhe(uint16_t named_group)
  2059. {
  2060. return named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X25519 ||
  2061. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 ||
  2062. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 ||
  2063. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1 ||
  2064. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X448;
  2065. }
  2066. static inline int mbedtls_ssl_tls13_named_group_is_ffdh(uint16_t named_group)
  2067. {
  2068. return named_group >= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048 &&
  2069. named_group <= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192;
  2070. }
  2071. static inline int mbedtls_ssl_named_group_is_offered(
  2072. const mbedtls_ssl_context *ssl, uint16_t named_group)
  2073. {
  2074. const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
  2075. if (group_list == NULL) {
  2076. return 0;
  2077. }
  2078. for (; *group_list != 0; group_list++) {
  2079. if (*group_list == named_group) {
  2080. return 1;
  2081. }
  2082. }
  2083. return 0;
  2084. }
  2085. static inline int mbedtls_ssl_named_group_is_supported(uint16_t named_group)
  2086. {
  2087. #if defined(PSA_WANT_ALG_ECDH)
  2088. if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
  2089. if (mbedtls_ssl_get_ecp_group_id_from_tls_id(named_group) !=
  2090. MBEDTLS_ECP_DP_NONE) {
  2091. return 1;
  2092. }
  2093. }
  2094. #endif
  2095. #if defined(PSA_WANT_ALG_FFDH)
  2096. if (mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
  2097. return 1;
  2098. }
  2099. #endif
  2100. #if !defined(PSA_WANT_ALG_ECDH) && !defined(PSA_WANT_ALG_FFDH)
  2101. (void) named_group;
  2102. #endif
  2103. return 0;
  2104. }
  2105. /*
  2106. * Return supported signature algorithms.
  2107. *
  2108. * In future, invocations can be changed to ssl->conf->sig_algs when
  2109. * mbedtls_ssl_conf_sig_hashes() is deleted.
  2110. *
  2111. * ssl->handshake->sig_algs is either a translation of sig_hashes to IANA TLS
  2112. * signature algorithm identifiers when mbedtls_ssl_conf_sig_hashes() has been
  2113. * used, or a pointer to ssl->conf->sig_algs when mbedtls_ssl_conf_sig_algs() has
  2114. * been more recently invoked.
  2115. *
  2116. */
  2117. static inline const void *mbedtls_ssl_get_sig_algs(
  2118. const mbedtls_ssl_context *ssl)
  2119. {
  2120. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  2121. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  2122. if (ssl->handshake != NULL &&
  2123. ssl->handshake->sig_algs_heap_allocated == 1 &&
  2124. ssl->handshake->sig_algs != NULL) {
  2125. return ssl->handshake->sig_algs;
  2126. }
  2127. #endif
  2128. return ssl->conf->sig_algs;
  2129. #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  2130. ((void) ssl);
  2131. return NULL;
  2132. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  2133. }
  2134. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2135. static inline int mbedtls_ssl_sig_alg_is_received(const mbedtls_ssl_context *ssl,
  2136. uint16_t own_sig_alg)
  2137. {
  2138. const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
  2139. if (sig_alg == NULL) {
  2140. return 0;
  2141. }
  2142. for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
  2143. if (*sig_alg == own_sig_alg) {
  2144. return 1;
  2145. }
  2146. }
  2147. return 0;
  2148. }
  2149. static inline int mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
  2150. const uint16_t sig_alg)
  2151. {
  2152. switch (sig_alg) {
  2153. #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
  2154. #if defined(PSA_WANT_ALG_SHA_256) && defined(PSA_WANT_ECC_SECP_R1_256)
  2155. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
  2156. break;
  2157. #endif /* PSA_WANT_ALG_SHA_256 && MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  2158. #if defined(PSA_WANT_ALG_SHA_384) && defined(PSA_WANT_ECC_SECP_R1_384)
  2159. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
  2160. break;
  2161. #endif /* PSA_WANT_ALG_SHA_384 && MBEDTLS_ECP_DP_SECP384R1_ENABLED */
  2162. #if defined(PSA_WANT_ALG_SHA_512) && defined(PSA_WANT_ECC_SECP_R1_521)
  2163. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
  2164. break;
  2165. #endif /* PSA_WANT_ALG_SHA_512 && MBEDTLS_ECP_DP_SECP521R1_ENABLED */
  2166. #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
  2167. #if defined(MBEDTLS_PKCS1_V21)
  2168. #if defined(PSA_WANT_ALG_SHA_256)
  2169. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
  2170. break;
  2171. #endif /* PSA_WANT_ALG_SHA_256 */
  2172. #if defined(PSA_WANT_ALG_SHA_384)
  2173. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
  2174. break;
  2175. #endif /* PSA_WANT_ALG_SHA_384 */
  2176. #if defined(PSA_WANT_ALG_SHA_512)
  2177. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
  2178. break;
  2179. #endif /* PSA_WANT_ALG_SHA_512 */
  2180. #endif /* MBEDTLS_PKCS1_V21 */
  2181. default:
  2182. return 0;
  2183. }
  2184. return 1;
  2185. }
  2186. static inline int mbedtls_ssl_tls13_sig_alg_is_supported(
  2187. const uint16_t sig_alg)
  2188. {
  2189. switch (sig_alg) {
  2190. #if defined(MBEDTLS_PKCS1_V15)
  2191. #if defined(MBEDTLS_MD_CAN_SHA256)
  2192. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
  2193. break;
  2194. #endif /* MBEDTLS_MD_CAN_SHA256 */
  2195. #if defined(MBEDTLS_MD_CAN_SHA384)
  2196. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
  2197. break;
  2198. #endif /* MBEDTLS_MD_CAN_SHA384 */
  2199. #if defined(MBEDTLS_MD_CAN_SHA512)
  2200. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
  2201. break;
  2202. #endif /* MBEDTLS_MD_CAN_SHA512 */
  2203. #endif /* MBEDTLS_PKCS1_V15 */
  2204. default:
  2205. return mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
  2206. sig_alg);
  2207. }
  2208. return 1;
  2209. }
  2210. MBEDTLS_CHECK_RETURN_CRITICAL
  2211. int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
  2212. mbedtls_pk_context *key);
  2213. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2214. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  2215. static inline int mbedtls_ssl_sig_alg_is_offered(const mbedtls_ssl_context *ssl,
  2216. uint16_t proposed_sig_alg)
  2217. {
  2218. const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
  2219. if (sig_alg == NULL) {
  2220. return 0;
  2221. }
  2222. for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
  2223. if (*sig_alg == proposed_sig_alg) {
  2224. return 1;
  2225. }
  2226. }
  2227. return 0;
  2228. }
  2229. static inline int mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
  2230. uint16_t sig_alg, mbedtls_pk_type_t *pk_type, mbedtls_md_type_t *md_alg)
  2231. {
  2232. *pk_type = mbedtls_ssl_pk_alg_from_sig(sig_alg & 0xff);
  2233. *md_alg = mbedtls_ssl_md_alg_from_hash((sig_alg >> 8) & 0xff);
  2234. if (*pk_type != MBEDTLS_PK_NONE && *md_alg != MBEDTLS_MD_NONE) {
  2235. return 0;
  2236. }
  2237. switch (sig_alg) {
  2238. #if defined(MBEDTLS_PKCS1_V21)
  2239. #if defined(MBEDTLS_MD_CAN_SHA256)
  2240. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
  2241. *md_alg = MBEDTLS_MD_SHA256;
  2242. *pk_type = MBEDTLS_PK_RSASSA_PSS;
  2243. break;
  2244. #endif /* MBEDTLS_MD_CAN_SHA256 */
  2245. #if defined(MBEDTLS_MD_CAN_SHA384)
  2246. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
  2247. *md_alg = MBEDTLS_MD_SHA384;
  2248. *pk_type = MBEDTLS_PK_RSASSA_PSS;
  2249. break;
  2250. #endif /* MBEDTLS_MD_CAN_SHA384 */
  2251. #if defined(MBEDTLS_MD_CAN_SHA512)
  2252. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
  2253. *md_alg = MBEDTLS_MD_SHA512;
  2254. *pk_type = MBEDTLS_PK_RSASSA_PSS;
  2255. break;
  2256. #endif /* MBEDTLS_MD_CAN_SHA512 */
  2257. #endif /* MBEDTLS_PKCS1_V21 */
  2258. default:
  2259. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2260. }
  2261. return 0;
  2262. }
  2263. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2264. static inline int mbedtls_ssl_tls12_sig_alg_is_supported(
  2265. const uint16_t sig_alg)
  2266. {
  2267. /* High byte is hash */
  2268. unsigned char hash = MBEDTLS_BYTE_1(sig_alg);
  2269. unsigned char sig = MBEDTLS_BYTE_0(sig_alg);
  2270. switch (hash) {
  2271. #if defined(MBEDTLS_MD_CAN_MD5)
  2272. case MBEDTLS_SSL_HASH_MD5:
  2273. break;
  2274. #endif
  2275. #if defined(MBEDTLS_MD_CAN_SHA1)
  2276. case MBEDTLS_SSL_HASH_SHA1:
  2277. break;
  2278. #endif
  2279. #if defined(MBEDTLS_MD_CAN_SHA224)
  2280. case MBEDTLS_SSL_HASH_SHA224:
  2281. break;
  2282. #endif
  2283. #if defined(MBEDTLS_MD_CAN_SHA256)
  2284. case MBEDTLS_SSL_HASH_SHA256:
  2285. break;
  2286. #endif
  2287. #if defined(MBEDTLS_MD_CAN_SHA384)
  2288. case MBEDTLS_SSL_HASH_SHA384:
  2289. break;
  2290. #endif
  2291. #if defined(MBEDTLS_MD_CAN_SHA512)
  2292. case MBEDTLS_SSL_HASH_SHA512:
  2293. break;
  2294. #endif
  2295. default:
  2296. return 0;
  2297. }
  2298. switch (sig) {
  2299. #if defined(MBEDTLS_RSA_C)
  2300. case MBEDTLS_SSL_SIG_RSA:
  2301. break;
  2302. #endif
  2303. #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
  2304. case MBEDTLS_SSL_SIG_ECDSA:
  2305. break;
  2306. #endif
  2307. default:
  2308. return 0;
  2309. }
  2310. return 1;
  2311. }
  2312. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2313. static inline int mbedtls_ssl_sig_alg_is_supported(
  2314. const mbedtls_ssl_context *ssl,
  2315. const uint16_t sig_alg)
  2316. {
  2317. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2318. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
  2319. return mbedtls_ssl_tls12_sig_alg_is_supported(sig_alg);
  2320. }
  2321. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2322. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2323. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
  2324. return mbedtls_ssl_tls13_sig_alg_is_supported(sig_alg);
  2325. }
  2326. #endif
  2327. ((void) ssl);
  2328. ((void) sig_alg);
  2329. return 0;
  2330. }
  2331. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  2332. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
  2333. /* Corresponding PSA algorithm for MBEDTLS_CIPHER_NULL.
  2334. * Same value is used for PSA_ALG_CATEGORY_CIPHER, hence it is
  2335. * guaranteed to not be a valid PSA algorithm identifier.
  2336. */
  2337. #define MBEDTLS_SSL_NULL_CIPHER 0x04000000
  2338. /**
  2339. * \brief Translate mbedtls cipher type/taglen pair to psa:
  2340. * algorithm, key type and key size.
  2341. *
  2342. * \param mbedtls_cipher_type [in] given mbedtls cipher type
  2343. * \param taglen [in] given tag length
  2344. * 0 - default tag length
  2345. * \param alg [out] corresponding PSA alg
  2346. * There is no corresponding PSA
  2347. * alg for MBEDTLS_CIPHER_NULL, so
  2348. * in this case MBEDTLS_SSL_NULL_CIPHER
  2349. * is returned via this parameter
  2350. * \param key_type [out] corresponding PSA key type
  2351. * \param key_size [out] corresponding PSA key size
  2352. *
  2353. * \return PSA_SUCCESS on success or PSA_ERROR_NOT_SUPPORTED if
  2354. * conversion is not supported.
  2355. */
  2356. psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
  2357. size_t taglen,
  2358. psa_algorithm_t *alg,
  2359. psa_key_type_t *key_type,
  2360. size_t *key_size);
  2361. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  2362. /**
  2363. * \brief Convert given PSA status to mbedtls error code.
  2364. *
  2365. * \param status [in] given PSA status
  2366. *
  2367. * \return corresponding mbedtls error code
  2368. */
  2369. static inline MBEDTLS_DEPRECATED int psa_ssl_status_to_mbedtls(psa_status_t status)
  2370. {
  2371. switch (status) {
  2372. case PSA_SUCCESS:
  2373. return 0;
  2374. case PSA_ERROR_INSUFFICIENT_MEMORY:
  2375. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2376. case PSA_ERROR_NOT_SUPPORTED:
  2377. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2378. case PSA_ERROR_INVALID_SIGNATURE:
  2379. return MBEDTLS_ERR_SSL_INVALID_MAC;
  2380. case PSA_ERROR_INVALID_ARGUMENT:
  2381. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2382. case PSA_ERROR_BAD_STATE:
  2383. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2384. case PSA_ERROR_BUFFER_TOO_SMALL:
  2385. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  2386. default:
  2387. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  2388. }
  2389. }
  2390. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  2391. #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
  2392. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
  2393. defined(MBEDTLS_USE_PSA_CRYPTO)
  2394. typedef enum {
  2395. MBEDTLS_ECJPAKE_ROUND_ONE,
  2396. MBEDTLS_ECJPAKE_ROUND_TWO
  2397. } mbedtls_ecjpake_rounds_t;
  2398. /**
  2399. * \brief Parse the provided input buffer for getting the first round
  2400. * of key exchange. This code is common between server and client
  2401. *
  2402. * \param pake_ctx [in] the PAKE's operation/context structure
  2403. * \param buf [in] input buffer to parse
  2404. * \param len [in] length of the input buffer
  2405. * \param round [in] either MBEDTLS_ECJPAKE_ROUND_ONE or
  2406. * MBEDTLS_ECJPAKE_ROUND_TWO
  2407. *
  2408. * \return 0 on success or a negative error code in case of failure
  2409. */
  2410. int mbedtls_psa_ecjpake_read_round(
  2411. psa_pake_operation_t *pake_ctx,
  2412. const unsigned char *buf,
  2413. size_t len, mbedtls_ecjpake_rounds_t round);
  2414. /**
  2415. * \brief Write the first round of key exchange into the provided output
  2416. * buffer. This code is common between server and client
  2417. *
  2418. * \param pake_ctx [in] the PAKE's operation/context structure
  2419. * \param buf [out] the output buffer in which data will be written to
  2420. * \param len [in] length of the output buffer
  2421. * \param olen [out] the length of the data really written on the buffer
  2422. * \param round [in] either MBEDTLS_ECJPAKE_ROUND_ONE or
  2423. * MBEDTLS_ECJPAKE_ROUND_TWO
  2424. *
  2425. * \return 0 on success or a negative error code in case of failure
  2426. */
  2427. int mbedtls_psa_ecjpake_write_round(
  2428. psa_pake_operation_t *pake_ctx,
  2429. unsigned char *buf,
  2430. size_t len, size_t *olen,
  2431. mbedtls_ecjpake_rounds_t round);
  2432. #endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
  2433. /**
  2434. * \brief TLS record protection modes
  2435. */
  2436. typedef enum {
  2437. MBEDTLS_SSL_MODE_STREAM = 0,
  2438. MBEDTLS_SSL_MODE_CBC,
  2439. MBEDTLS_SSL_MODE_CBC_ETM,
  2440. MBEDTLS_SSL_MODE_AEAD
  2441. } mbedtls_ssl_mode_t;
  2442. mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
  2443. const mbedtls_ssl_transform *transform);
  2444. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
  2445. mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
  2446. int encrypt_then_mac,
  2447. const mbedtls_ssl_ciphersuite_t *suite);
  2448. #else
  2449. mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
  2450. const mbedtls_ssl_ciphersuite_t *suite);
  2451. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
  2452. #if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
  2453. MBEDTLS_CHECK_RETURN_CRITICAL
  2454. int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
  2455. const unsigned char *buf,
  2456. size_t buf_len);
  2457. #endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
  2458. static inline int mbedtls_ssl_tls13_cipher_suite_is_offered(
  2459. mbedtls_ssl_context *ssl, int cipher_suite)
  2460. {
  2461. const int *ciphersuite_list = ssl->conf->ciphersuite_list;
  2462. /* Check whether we have offered this ciphersuite */
  2463. for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
  2464. if (ciphersuite_list[i] == cipher_suite) {
  2465. return 1;
  2466. }
  2467. }
  2468. return 0;
  2469. }
  2470. /**
  2471. * \brief Validate cipher suite against config in SSL context.
  2472. *
  2473. * \param ssl SSL context
  2474. * \param suite_info Cipher suite to validate
  2475. * \param min_tls_version Minimal TLS version to accept a cipher suite
  2476. * \param max_tls_version Maximal TLS version to accept a cipher suite
  2477. *
  2478. * \return 0 if valid, negative value otherwise.
  2479. */
  2480. MBEDTLS_CHECK_RETURN_CRITICAL
  2481. int mbedtls_ssl_validate_ciphersuite(
  2482. const mbedtls_ssl_context *ssl,
  2483. const mbedtls_ssl_ciphersuite_t *suite_info,
  2484. mbedtls_ssl_protocol_version min_tls_version,
  2485. mbedtls_ssl_protocol_version max_tls_version);
  2486. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2487. MBEDTLS_CHECK_RETURN_CRITICAL
  2488. int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
  2489. const unsigned char *buf,
  2490. const unsigned char *end);
  2491. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  2492. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  2493. #define MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH (2)
  2494. #define MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN (64) /* As defined in RFC 8449 */
  2495. MBEDTLS_CHECK_RETURN_CRITICAL
  2496. int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
  2497. const unsigned char *buf,
  2498. const unsigned char *end);
  2499. MBEDTLS_CHECK_RETURN_CRITICAL
  2500. int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
  2501. unsigned char *buf,
  2502. const unsigned char *end,
  2503. size_t *out_len);
  2504. #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
  2505. #if defined(MBEDTLS_SSL_ALPN)
  2506. MBEDTLS_CHECK_RETURN_CRITICAL
  2507. int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
  2508. const unsigned char *buf,
  2509. const unsigned char *end);
  2510. MBEDTLS_CHECK_RETURN_CRITICAL
  2511. int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
  2512. unsigned char *buf,
  2513. unsigned char *end,
  2514. size_t *out_len);
  2515. #endif /* MBEDTLS_SSL_ALPN */
  2516. #if defined(MBEDTLS_TEST_HOOKS)
  2517. int mbedtls_ssl_check_dtls_clihlo_cookie(
  2518. mbedtls_ssl_context *ssl,
  2519. const unsigned char *cli_id, size_t cli_id_len,
  2520. const unsigned char *in, size_t in_len,
  2521. unsigned char *obuf, size_t buf_len, size_t *olen);
  2522. #endif
  2523. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2524. /**
  2525. * \brief Given an SSL context and its associated configuration, write the TLS
  2526. * 1.3 specific Pre-Shared key extension.
  2527. *
  2528. * \param[in] ssl SSL context
  2529. * \param[in] buf Base address of the buffer where to write the extension
  2530. * \param[in] end End address of the buffer where to write the extension
  2531. * \param[out] out_len Length in bytes of the Pre-Shared key extension: data
  2532. * written into the buffer \p buf by this function plus
  2533. * the length of the binders to be written.
  2534. * \param[out] binders_len Length of the binders to be written at the end of
  2535. * the extension.
  2536. */
  2537. MBEDTLS_CHECK_RETURN_CRITICAL
  2538. int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
  2539. mbedtls_ssl_context *ssl,
  2540. unsigned char *buf, unsigned char *end,
  2541. size_t *out_len, size_t *binders_len);
  2542. /**
  2543. * \brief Given an SSL context and its associated configuration, write the TLS
  2544. * 1.3 specific Pre-Shared key extension binders at the end of the
  2545. * ClientHello.
  2546. *
  2547. * \param[in] ssl SSL context
  2548. * \param[in] buf Base address of the buffer where to write the binders
  2549. * \param[in] end End address of the buffer where to write the binders
  2550. */
  2551. MBEDTLS_CHECK_RETURN_CRITICAL
  2552. int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
  2553. mbedtls_ssl_context *ssl,
  2554. unsigned char *buf, unsigned char *end);
  2555. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  2556. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
  2557. defined(MBEDTLS_SSL_SESSION_TICKETS) && \
  2558. defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
  2559. defined(MBEDTLS_SSL_CLI_C)
  2560. MBEDTLS_CHECK_RETURN_CRITICAL
  2561. int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
  2562. const char *hostname);
  2563. #endif
  2564. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
  2565. defined(MBEDTLS_SSL_ALPN)
  2566. MBEDTLS_CHECK_RETURN_CRITICAL
  2567. int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
  2568. const char *alpn);
  2569. #endif
  2570. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
  2571. #define MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME (604800)
  2572. static inline unsigned int mbedtls_ssl_tls13_session_get_ticket_flags(
  2573. mbedtls_ssl_session *session, unsigned int flags)
  2574. {
  2575. return session->ticket_flags &
  2576. (flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2577. }
  2578. /**
  2579. * Check if at least one of the given flags is set in
  2580. * the session ticket. See the definition of
  2581. * `MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK` to get all
  2582. * permitted flags.
  2583. */
  2584. static inline int mbedtls_ssl_tls13_session_ticket_has_flags(
  2585. mbedtls_ssl_session *session, unsigned int flags)
  2586. {
  2587. return mbedtls_ssl_tls13_session_get_ticket_flags(session, flags) != 0;
  2588. }
  2589. static inline int mbedtls_ssl_tls13_session_ticket_allow_psk(
  2590. mbedtls_ssl_session *session)
  2591. {
  2592. return mbedtls_ssl_tls13_session_ticket_has_flags(
  2593. session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_RESUMPTION);
  2594. }
  2595. static inline int mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(
  2596. mbedtls_ssl_session *session)
  2597. {
  2598. return mbedtls_ssl_tls13_session_ticket_has_flags(
  2599. session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_EPHEMERAL_RESUMPTION);
  2600. }
  2601. static inline unsigned int mbedtls_ssl_tls13_session_ticket_allow_early_data(
  2602. mbedtls_ssl_session *session)
  2603. {
  2604. return mbedtls_ssl_tls13_session_ticket_has_flags(
  2605. session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
  2606. }
  2607. static inline void mbedtls_ssl_tls13_session_set_ticket_flags(
  2608. mbedtls_ssl_session *session, unsigned int flags)
  2609. {
  2610. session->ticket_flags |= (flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2611. }
  2612. static inline void mbedtls_ssl_tls13_session_clear_ticket_flags(
  2613. mbedtls_ssl_session *session, unsigned int flags)
  2614. {
  2615. session->ticket_flags &= ~(flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2616. }
  2617. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
  2618. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  2619. #define MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT 0
  2620. #define MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT 1
  2621. #define MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK \
  2622. (1 << MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT)
  2623. #define MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK \
  2624. (1 << MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT)
  2625. static inline int mbedtls_ssl_conf_get_session_tickets(
  2626. const mbedtls_ssl_config *conf)
  2627. {
  2628. return conf->session_tickets & MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK ?
  2629. MBEDTLS_SSL_SESSION_TICKETS_ENABLED :
  2630. MBEDTLS_SSL_SESSION_TICKETS_DISABLED;
  2631. }
  2632. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  2633. static inline int mbedtls_ssl_conf_is_signal_new_session_tickets_enabled(
  2634. const mbedtls_ssl_config *conf)
  2635. {
  2636. return conf->session_tickets & MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK ?
  2637. MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED :
  2638. MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED;
  2639. }
  2640. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  2641. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  2642. #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
  2643. int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl);
  2644. #endif
  2645. #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  2646. /** Compute the HMAC of variable-length data with constant flow.
  2647. *
  2648. * This function computes the HMAC of the concatenation of \p add_data and \p
  2649. * data, and does with a code flow and memory access pattern that does not
  2650. * depend on \p data_len_secret, but only on \p min_data_len and \p
  2651. * max_data_len. In particular, this function always reads exactly \p
  2652. * max_data_len bytes from \p data.
  2653. *
  2654. * \param ctx The HMAC context. It must have keys configured
  2655. * with mbedtls_md_hmac_starts() and use one of the
  2656. * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
  2657. * It is reset using mbedtls_md_hmac_reset() after
  2658. * the computation is complete to prepare for the
  2659. * next computation.
  2660. * \param add_data The first part of the message whose HMAC is being
  2661. * calculated. This must point to a readable buffer
  2662. * of \p add_data_len bytes.
  2663. * \param add_data_len The length of \p add_data in bytes.
  2664. * \param data The buffer containing the second part of the
  2665. * message. This must point to a readable buffer
  2666. * of \p max_data_len bytes.
  2667. * \param data_len_secret The length of the data to process in \p data.
  2668. * This must be no less than \p min_data_len and no
  2669. * greater than \p max_data_len.
  2670. * \param min_data_len The minimal length of the second part of the
  2671. * message, read from \p data.
  2672. * \param max_data_len The maximal length of the second part of the
  2673. * message, read from \p data.
  2674. * \param output The HMAC will be written here. This must point to
  2675. * a writable buffer of sufficient size to hold the
  2676. * HMAC value.
  2677. *
  2678. * \retval 0 on success.
  2679. * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
  2680. * The hardware accelerator failed.
  2681. */
  2682. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2683. int mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
  2684. psa_algorithm_t mac_alg,
  2685. const unsigned char *add_data,
  2686. size_t add_data_len,
  2687. const unsigned char *data,
  2688. size_t data_len_secret,
  2689. size_t min_data_len,
  2690. size_t max_data_len,
  2691. unsigned char *output);
  2692. #else
  2693. int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
  2694. const unsigned char *add_data,
  2695. size_t add_data_len,
  2696. const unsigned char *data,
  2697. size_t data_len_secret,
  2698. size_t min_data_len,
  2699. size_t max_data_len,
  2700. unsigned char *output);
  2701. #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
  2702. #endif /* MBEDTLS_TEST_HOOKS && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) */
  2703. #endif /* ssl_misc.h */