ssl_misc.h 106 KB

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