ssl_misc.h 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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)
  28. #include "psa/crypto.h"
  29. #endif
  30. #if defined(MBEDTLS_MD5_C)
  31. #include "mbedtls/md5.h"
  32. #endif
  33. #if defined(MBEDTLS_SHA1_C)
  34. #include "mbedtls/sha1.h"
  35. #endif
  36. #if defined(MBEDTLS_SHA256_C)
  37. #include "mbedtls/sha256.h"
  38. #endif
  39. #if defined(MBEDTLS_SHA512_C)
  40. #include "mbedtls/sha512.h"
  41. #endif
  42. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  43. #include "mbedtls/ecjpake.h"
  44. #endif
  45. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  46. #include "psa/crypto.h"
  47. #include "mbedtls/psa_util.h"
  48. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  49. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  50. !defined(inline) && !defined(__cplusplus)
  51. #define inline __inline
  52. #endif
  53. /* Legacy minor version numbers as defined by:
  54. * - RFC 2246: ProtocolVersion version = { 3, 1 }; // TLS v1.0
  55. * - RFC 4346: ProtocolVersion version = { 3, 2 }; // TLS v1.1
  56. *
  57. * We no longer support these versions, but some code still references those
  58. * constants as part of negotiating with the peer, so keep them available
  59. * internally.
  60. */
  61. #define MBEDTLS_SSL_MINOR_VERSION_1 1
  62. #define MBEDTLS_SSL_MINOR_VERSION_2 2
  63. /* Determine minimum supported version */
  64. #define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  65. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  66. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  67. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  68. #define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  69. #define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  70. /* Determine maximum supported version */
  71. #define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  72. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  73. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  74. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  75. /* Shorthand for restartable ECC */
  76. #if defined(MBEDTLS_ECP_RESTARTABLE) && \
  77. defined(MBEDTLS_SSL_CLI_C) && \
  78. defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  79. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  80. #define MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED
  81. #endif
  82. #define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
  83. #define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
  84. #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
  85. #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
  86. /*
  87. * Mask of TLS 1.3 handshake extensions used in extensions_present
  88. * of mbedtls_ssl_handshake_params.
  89. */
  90. #define MBEDTLS_SSL_EXT_NONE 0
  91. #define MBEDTLS_SSL_EXT_SERVERNAME ( 1 << 0 )
  92. #define MBEDTLS_SSL_EXT_MAX_FRAGMENT_LENGTH ( 1 << 1 )
  93. #define MBEDTLS_SSL_EXT_STATUS_REQUEST ( 1 << 2 )
  94. #define MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ( 1 << 3 )
  95. #define MBEDTLS_SSL_EXT_SIG_ALG ( 1 << 4 )
  96. #define MBEDTLS_SSL_EXT_USE_SRTP ( 1 << 5 )
  97. #define MBEDTLS_SSL_EXT_HEARTBEAT ( 1 << 6 )
  98. #define MBEDTLS_SSL_EXT_ALPN ( 1 << 7 )
  99. #define MBEDTLS_SSL_EXT_SCT ( 1 << 8 )
  100. #define MBEDTLS_SSL_EXT_CLI_CERT_TYPE ( 1 << 9 )
  101. #define MBEDTLS_SSL_EXT_SERV_CERT_TYPE ( 1 << 10 )
  102. #define MBEDTLS_SSL_EXT_PADDING ( 1 << 11 )
  103. #define MBEDTLS_SSL_EXT_PRE_SHARED_KEY ( 1 << 12 )
  104. #define MBEDTLS_SSL_EXT_EARLY_DATA ( 1 << 13 )
  105. #define MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ( 1 << 14 )
  106. #define MBEDTLS_SSL_EXT_COOKIE ( 1 << 15 )
  107. #define MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ( 1 << 16 )
  108. #define MBEDTLS_SSL_EXT_CERT_AUTH ( 1 << 17 )
  109. #define MBEDTLS_SSL_EXT_OID_FILTERS ( 1 << 18 )
  110. #define MBEDTLS_SSL_EXT_POST_HANDSHAKE_AUTH ( 1 << 19 )
  111. #define MBEDTLS_SSL_EXT_SIG_ALG_CERT ( 1 << 20 )
  112. #define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 21 )
  113. /*
  114. * Helper macros for function call with return check.
  115. */
  116. /*
  117. * Exit when return non-zero value
  118. */
  119. #define MBEDTLS_SSL_PROC_CHK( f ) \
  120. do { \
  121. ret = ( f ); \
  122. if( ret != 0 ) \
  123. { \
  124. goto cleanup; \
  125. } \
  126. } while( 0 )
  127. /*
  128. * Exit when return negative value
  129. */
  130. #define MBEDTLS_SSL_PROC_CHK_NEG( f ) \
  131. do { \
  132. ret = ( f ); \
  133. if( ret < 0 ) \
  134. { \
  135. goto cleanup; \
  136. } \
  137. } while( 0 )
  138. /*
  139. * DTLS retransmission states, see RFC 6347 4.2.4
  140. *
  141. * The SENDING state is merged in PREPARING for initial sends,
  142. * but is distinct for resends.
  143. *
  144. * Note: initial state is wrong for server, but is not used anyway.
  145. */
  146. #define MBEDTLS_SSL_RETRANS_PREPARING 0
  147. #define MBEDTLS_SSL_RETRANS_SENDING 1
  148. #define MBEDTLS_SSL_RETRANS_WAITING 2
  149. #define MBEDTLS_SSL_RETRANS_FINISHED 3
  150. /*
  151. * Allow extra bytes for record, authentication and encryption overhead:
  152. * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256).
  153. */
  154. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  155. /* This macro determines whether CBC is supported. */
  156. #if defined(MBEDTLS_CIPHER_MODE_CBC) && \
  157. ( defined(MBEDTLS_AES_C) || \
  158. defined(MBEDTLS_CAMELLIA_C) || \
  159. defined(MBEDTLS_ARIA_C) || \
  160. defined(MBEDTLS_DES_C) )
  161. #define MBEDTLS_SSL_SOME_SUITES_USE_CBC
  162. #endif
  163. /* This macro determines whether a ciphersuite using a
  164. * stream cipher can be used. */
  165. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  166. #define MBEDTLS_SSL_SOME_SUITES_USE_STREAM
  167. #endif
  168. /* This macro determines whether the CBC construct used in TLS 1.2 is supported. */
  169. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
  170. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  171. #define MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC
  172. #endif
  173. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) || \
  174. defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
  175. #define MBEDTLS_SSL_SOME_SUITES_USE_MAC
  176. #endif
  177. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  178. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  179. /* Ciphersuites using HMAC */
  180. #if defined(MBEDTLS_SHA384_C)
  181. #define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
  182. #elif defined(MBEDTLS_SHA256_C)
  183. #define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
  184. #else
  185. #define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
  186. #endif
  187. #else /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
  188. /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
  189. #define MBEDTLS_SSL_MAC_ADD 16
  190. #endif
  191. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  192. #define MBEDTLS_SSL_PADDING_ADD 256
  193. #else
  194. #define MBEDTLS_SSL_PADDING_ADD 0
  195. #endif
  196. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  197. #define MBEDTLS_SSL_MAX_CID_EXPANSION MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY
  198. #else
  199. #define MBEDTLS_SSL_MAX_CID_EXPANSION 0
  200. #endif
  201. #define MBEDTLS_SSL_PAYLOAD_OVERHEAD ( MBEDTLS_MAX_IV_LENGTH + \
  202. MBEDTLS_SSL_MAC_ADD + \
  203. MBEDTLS_SSL_PADDING_ADD + \
  204. MBEDTLS_SSL_MAX_CID_EXPANSION \
  205. )
  206. #define MBEDTLS_SSL_IN_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  207. ( MBEDTLS_SSL_IN_CONTENT_LEN ) )
  208. #define MBEDTLS_SSL_OUT_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  209. ( MBEDTLS_SSL_OUT_CONTENT_LEN ) )
  210. /* The maximum number of buffered handshake messages. */
  211. #define MBEDTLS_SSL_MAX_BUFFERED_HS 4
  212. /* Maximum length we can advertise as our max content length for
  213. RFC 6066 max_fragment_length extension negotiation purposes
  214. (the lesser of both sizes, if they are unequal.)
  215. */
  216. #define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ( \
  217. (MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN) \
  218. ? ( MBEDTLS_SSL_OUT_CONTENT_LEN ) \
  219. : ( MBEDTLS_SSL_IN_CONTENT_LEN ) \
  220. )
  221. /* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */
  222. #define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN 65534
  223. /* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */
  224. #define MBEDTLS_SSL_MAX_CURVE_LIST_LEN 65535
  225. /*
  226. * Check that we obey the standard's message size bounds
  227. */
  228. #if MBEDTLS_SSL_IN_CONTENT_LEN > 16384
  229. #error "Bad configuration - incoming record content too large."
  230. #endif
  231. #if MBEDTLS_SSL_OUT_CONTENT_LEN > 16384
  232. #error "Bad configuration - outgoing record content too large."
  233. #endif
  234. #if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_IN_CONTENT_LEN + 2048
  235. #error "Bad configuration - incoming protected record payload too large."
  236. #endif
  237. #if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN + 2048
  238. #error "Bad configuration - outgoing protected record payload too large."
  239. #endif
  240. /* Calculate buffer sizes */
  241. /* Note: Even though the TLS record header is only 5 bytes
  242. long, we're internally using 8 bytes to store the
  243. implicit sequence number. */
  244. #define MBEDTLS_SSL_HEADER_LEN 13
  245. #if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  246. #define MBEDTLS_SSL_IN_BUFFER_LEN \
  247. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_IN_PAYLOAD_LEN ) )
  248. #else
  249. #define MBEDTLS_SSL_IN_BUFFER_LEN \
  250. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_IN_PAYLOAD_LEN ) \
  251. + ( MBEDTLS_SSL_CID_IN_LEN_MAX ) )
  252. #endif
  253. #if !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  254. #define MBEDTLS_SSL_OUT_BUFFER_LEN \
  255. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_OUT_PAYLOAD_LEN ) )
  256. #else
  257. #define MBEDTLS_SSL_OUT_BUFFER_LEN \
  258. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_OUT_PAYLOAD_LEN ) \
  259. + ( MBEDTLS_SSL_CID_OUT_LEN_MAX ) )
  260. #endif
  261. #define MBEDTLS_CLIENT_HELLO_RANDOM_LEN 32
  262. #define MBEDTLS_SERVER_HELLO_RANDOM_LEN 32
  263. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  264. /**
  265. * \brief Return the maximum fragment length (payload, in bytes) for
  266. * the output buffer. For the client, this is the configured
  267. * value. For the server, it is the minimum of two - the
  268. * configured value and the negotiated one.
  269. *
  270. * \sa mbedtls_ssl_conf_max_frag_len()
  271. * \sa mbedtls_ssl_get_max_out_record_payload()
  272. *
  273. * \param ssl SSL context
  274. *
  275. * \return Current maximum fragment length for the output buffer.
  276. */
  277. size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl );
  278. /**
  279. * \brief Return the maximum fragment length (payload, in bytes) for
  280. * the input buffer. This is the negotiated maximum fragment
  281. * length, or, if there is none, MBEDTLS_SSL_IN_CONTENT_LEN.
  282. * If it is not defined either, the value is 2^14. This function
  283. * works as its predecessor, \c mbedtls_ssl_get_max_frag_len().
  284. *
  285. * \sa mbedtls_ssl_conf_max_frag_len()
  286. * \sa mbedtls_ssl_get_max_in_record_payload()
  287. *
  288. * \param ssl SSL context
  289. *
  290. * \return Current maximum fragment length for the output buffer.
  291. */
  292. size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl );
  293. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  294. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  295. static inline size_t mbedtls_ssl_get_output_buflen( const mbedtls_ssl_context *ctx )
  296. {
  297. #if defined (MBEDTLS_SSL_DTLS_CONNECTION_ID)
  298. return mbedtls_ssl_get_output_max_frag_len( ctx )
  299. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
  300. + MBEDTLS_SSL_CID_OUT_LEN_MAX;
  301. #else
  302. return mbedtls_ssl_get_output_max_frag_len( ctx )
  303. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
  304. #endif
  305. }
  306. static inline size_t mbedtls_ssl_get_input_buflen( const mbedtls_ssl_context *ctx )
  307. {
  308. #if defined (MBEDTLS_SSL_DTLS_CONNECTION_ID)
  309. return mbedtls_ssl_get_input_max_frag_len( ctx )
  310. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD
  311. + MBEDTLS_SSL_CID_IN_LEN_MAX;
  312. #else
  313. return mbedtls_ssl_get_input_max_frag_len( ctx )
  314. + MBEDTLS_SSL_HEADER_LEN + MBEDTLS_SSL_PAYLOAD_OVERHEAD;
  315. #endif
  316. }
  317. #endif
  318. /*
  319. * TLS extension flags (for extensions with outgoing ServerHello content
  320. * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
  321. * of state of the renegotiation flag, so no indicator is required)
  322. */
  323. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
  324. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
  325. /**
  326. * \brief This function checks if the remaining size in a buffer is
  327. * greater or equal than a needed space.
  328. *
  329. * \param cur Pointer to the current position in the buffer.
  330. * \param end Pointer to one past the end of the buffer.
  331. * \param need Needed space in bytes.
  332. *
  333. * \return Zero if the needed space is available in the buffer, non-zero
  334. * otherwise.
  335. */
  336. static inline int mbedtls_ssl_chk_buf_ptr( const uint8_t *cur,
  337. const uint8_t *end, size_t need )
  338. {
  339. return( ( cur > end ) || ( need > (size_t)( end - cur ) ) );
  340. }
  341. /**
  342. * \brief This macro checks if the remaining size in a buffer is
  343. * greater or equal than a needed space. If it is not the case,
  344. * it returns an SSL_BUFFER_TOO_SMALL error.
  345. *
  346. * \param cur Pointer to the current position in the buffer.
  347. * \param end Pointer to one past the end of the buffer.
  348. * \param need Needed space in bytes.
  349. *
  350. */
  351. #define MBEDTLS_SSL_CHK_BUF_PTR( cur, end, need ) \
  352. do { \
  353. if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \
  354. { \
  355. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); \
  356. } \
  357. } while( 0 )
  358. /**
  359. * \brief This macro checks if the remaining length in an input buffer is
  360. * greater or equal than a needed length. If it is not the case, it
  361. * returns #MBEDTLS_ERR_SSL_DECODE_ERROR error and pends a
  362. * #MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR alert message.
  363. *
  364. * This is a function-like macro. It is guaranteed to evaluate each
  365. * argument exactly once.
  366. *
  367. * \param cur Pointer to the current position in the buffer.
  368. * \param end Pointer to one past the end of the buffer.
  369. * \param need Needed length in bytes.
  370. *
  371. */
  372. #define MBEDTLS_SSL_CHK_BUF_READ_PTR( cur, end, need ) \
  373. do { \
  374. if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \
  375. { \
  376. MBEDTLS_SSL_DEBUG_MSG( 1, \
  377. ( "missing input data in %s", __func__ ) ); \
  378. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
  379. MBEDTLS_ERR_SSL_DECODE_ERROR ); \
  380. return( MBEDTLS_ERR_SSL_DECODE_ERROR ); \
  381. } \
  382. } while( 0 )
  383. #ifdef __cplusplus
  384. extern "C" {
  385. #endif
  386. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  387. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  388. /*
  389. * Abstraction for a grid of allowed signature-hash-algorithm pairs.
  390. */
  391. struct mbedtls_ssl_sig_hash_set_t
  392. {
  393. /* At the moment, we only need to remember a single suitable
  394. * hash algorithm per signature algorithm. As long as that's
  395. * the case - and we don't need a general lookup function -
  396. * we can implement the sig-hash-set as a map from signatures
  397. * to hash algorithms. */
  398. mbedtls_md_type_t rsa;
  399. mbedtls_md_type_t ecdsa;
  400. };
  401. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  402. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  403. typedef int mbedtls_ssl_tls_prf_cb( const unsigned char *secret, size_t slen,
  404. const char *label,
  405. const unsigned char *random, size_t rlen,
  406. unsigned char *dstbuf, size_t dlen );
  407. /* cipher.h exports the maximum IV, key and block length from
  408. * all ciphers enabled in the config, regardless of whether those
  409. * ciphers are actually usable in SSL/TLS. Notably, XTS is enabled
  410. * in the default configuration and uses 64 Byte keys, but it is
  411. * not used for record protection in SSL/TLS.
  412. *
  413. * In order to prevent unnecessary inflation of key structures,
  414. * we introduce SSL-specific variants of the max-{key,block,IV}
  415. * macros here which are meant to only take those ciphers into
  416. * account which can be negotiated in SSL/TLS.
  417. *
  418. * Since the current definitions of MBEDTLS_MAX_{KEY|BLOCK|IV}_LENGTH
  419. * in cipher.h are rough overapproximations of the real maxima, here
  420. * we content ourselves with replicating those overapproximations
  421. * for the maximum block and IV length, and excluding XTS from the
  422. * computation of the maximum key length. */
  423. #define MBEDTLS_SSL_MAX_BLOCK_LENGTH 16
  424. #define MBEDTLS_SSL_MAX_IV_LENGTH 16
  425. #define MBEDTLS_SSL_MAX_KEY_LENGTH 32
  426. /**
  427. * \brief The data structure holding the cryptographic material (key and IV)
  428. * used for record protection in TLS 1.3.
  429. */
  430. struct mbedtls_ssl_key_set
  431. {
  432. /*! The key for client->server records. */
  433. unsigned char client_write_key[ MBEDTLS_SSL_MAX_KEY_LENGTH ];
  434. /*! The key for server->client records. */
  435. unsigned char server_write_key[ MBEDTLS_SSL_MAX_KEY_LENGTH ];
  436. /*! The IV for client->server records. */
  437. unsigned char client_write_iv[ MBEDTLS_SSL_MAX_IV_LENGTH ];
  438. /*! The IV for server->client records. */
  439. unsigned char server_write_iv[ MBEDTLS_SSL_MAX_IV_LENGTH ];
  440. size_t key_len; /*!< The length of client_write_key and
  441. * server_write_key, in Bytes. */
  442. size_t iv_len; /*!< The length of client_write_iv and
  443. * server_write_iv, in Bytes. */
  444. };
  445. typedef struct mbedtls_ssl_key_set mbedtls_ssl_key_set;
  446. typedef struct
  447. {
  448. unsigned char binder_key [ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
  449. unsigned char client_early_traffic_secret [ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
  450. unsigned char early_exporter_master_secret[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
  451. } mbedtls_ssl_tls1_3_early_secrets;
  452. typedef struct
  453. {
  454. unsigned char client_handshake_traffic_secret[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
  455. unsigned char server_handshake_traffic_secret[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
  456. } mbedtls_ssl_tls1_3_handshake_secrets;
  457. /*
  458. * This structure contains the parameters only needed during handshake.
  459. */
  460. struct mbedtls_ssl_handshake_params
  461. {
  462. /*
  463. * Handshake specific crypto variables
  464. */
  465. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  466. int tls1_3_kex_modes; /*!< key exchange modes for TLS 1.3 */
  467. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  468. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  469. const uint16_t *group_list;
  470. unsigned char group_list_heap_allocated;
  471. #endif
  472. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  473. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  474. mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
  475. #endif
  476. #if defined(MBEDTLS_DHM_C)
  477. mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
  478. #endif
  479. /* Adding guard for MBEDTLS_ECDSA_C to ensure no compile errors due
  480. * to guards also being in ssl_srv.c and ssl_cli.c. There is a gap
  481. * in functionality that access to ecdh_ctx structure is needed for
  482. * MBEDTLS_ECDSA_C which does not seem correct.
  483. */
  484. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  485. mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
  486. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  487. psa_key_type_t ecdh_psa_type;
  488. uint16_t ecdh_bits;
  489. psa_key_id_t ecdh_psa_privkey;
  490. unsigned char ecdh_psa_peerkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
  491. size_t ecdh_psa_peerkey_len;
  492. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  493. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C */
  494. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  495. mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
  496. #if defined(MBEDTLS_SSL_CLI_C)
  497. unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
  498. size_t ecjpake_cache_len; /*!< Length of cached data */
  499. #endif
  500. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  501. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  502. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  503. const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
  504. #endif
  505. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  506. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  507. psa_key_id_t psk_opaque; /*!< Opaque PSK from the callback */
  508. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  509. unsigned char *psk; /*!< PSK from the callback */
  510. size_t psk_len; /*!< Length of PSK from callback */
  511. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  512. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  513. mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
  514. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  515. int sni_authmode; /*!< authmode from SNI callback */
  516. mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
  517. mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
  518. mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
  519. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  520. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  521. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  522. int ecrs_enabled; /*!< Handshake supports EC restart? */
  523. mbedtls_x509_crt_restart_ctx ecrs_ctx; /*!< restart context */
  524. enum { /* this complements ssl->state with info on intra-state operations */
  525. ssl_ecrs_none = 0, /*!< nothing going on (yet) */
  526. ssl_ecrs_crt_verify, /*!< Certificate: crt_verify() */
  527. ssl_ecrs_ske_start_processing, /*!< ServerKeyExchange: pk_verify() */
  528. ssl_ecrs_cke_ecdh_calc_secret, /*!< ClientKeyExchange: ECDH step 2 */
  529. ssl_ecrs_crt_vrfy_sign, /*!< CertificateVerify: pk_sign() */
  530. } ecrs_state; /*!< current (or last) operation */
  531. mbedtls_x509_crt *ecrs_peer_cert; /*!< The peer's CRT chain. */
  532. size_t ecrs_n; /*!< place for saving a length */
  533. #endif
  534. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  535. !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  536. mbedtls_pk_context peer_pubkey; /*!< The public key from the peer. */
  537. #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  538. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  539. unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
  540. unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
  541. unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
  542. Srv: unused */
  543. unsigned char verify_cookie_len; /*!< Cli: cookie length
  544. Srv: flag for sending a cookie */
  545. uint32_t retransmit_timeout; /*!< Current value of timeout */
  546. unsigned char retransmit_state; /*!< Retransmission state */
  547. mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
  548. mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
  549. unsigned char *cur_msg_p; /*!< Position in current message */
  550. unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
  551. flight being received */
  552. mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
  553. resending messages */
  554. unsigned char alt_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; /*!< Alternative record epoch/counter
  555. for resending messages */
  556. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  557. /* The state of CID configuration in this handshake. */
  558. uint8_t cid_in_use; /*!< This indicates whether the use of the CID extension
  559. * has been negotiated. Possible values are
  560. * #MBEDTLS_SSL_CID_ENABLED and
  561. * #MBEDTLS_SSL_CID_DISABLED. */
  562. unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ]; /*! The peer's CID */
  563. uint8_t peer_cid_len; /*!< The length of
  564. * \c peer_cid. */
  565. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  566. struct
  567. {
  568. size_t total_bytes_buffered; /*!< Cumulative size of heap allocated
  569. * buffers used for message buffering. */
  570. uint8_t seen_ccs; /*!< Indicates if a CCS message has
  571. * been seen in the current flight. */
  572. struct mbedtls_ssl_hs_buffer
  573. {
  574. unsigned is_valid : 1;
  575. unsigned is_fragmented : 1;
  576. unsigned is_complete : 1;
  577. unsigned char *data;
  578. size_t data_len;
  579. } hs[MBEDTLS_SSL_MAX_BUFFERED_HS];
  580. struct
  581. {
  582. unsigned char *data;
  583. size_t len;
  584. unsigned epoch;
  585. } future_record;
  586. } buffering;
  587. uint16_t mtu; /*!< Handshake mtu, used to fragment outgoing messages */
  588. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  589. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  590. /*! TLS 1.3 transforms for 0-RTT and encrypted handshake messages.
  591. * Those pointers own the transforms they reference. */
  592. mbedtls_ssl_transform *transform_handshake;
  593. mbedtls_ssl_transform *transform_earlydata;
  594. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  595. /*
  596. * Checksum contexts
  597. */
  598. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  599. #if defined(MBEDTLS_SHA256_C)
  600. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  601. psa_hash_operation_t fin_sha256_psa;
  602. #else
  603. mbedtls_sha256_context fin_sha256;
  604. #endif
  605. #endif
  606. #if defined(MBEDTLS_SHA384_C)
  607. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  608. psa_hash_operation_t fin_sha384_psa;
  609. #else
  610. mbedtls_sha512_context fin_sha512;
  611. #endif
  612. #endif
  613. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  614. void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
  615. void (*calc_verify)(const mbedtls_ssl_context *, unsigned char *, size_t *);
  616. void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
  617. mbedtls_ssl_tls_prf_cb *tls_prf;
  618. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  619. uint16_t offered_group_id; /* The NamedGroup value for the group
  620. * that is being used for ephemeral
  621. * key exchange.
  622. *
  623. * On the client: Defaults to the first
  624. * entry in the client's group list,
  625. * but can be overwritten by the HRR. */
  626. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  627. /*
  628. * State-local variables used during the processing
  629. * of a specific handshake state.
  630. */
  631. union
  632. {
  633. /* Outgoing Finished message */
  634. struct
  635. {
  636. uint8_t preparation_done;
  637. /* Buffer holding digest of the handshake up to
  638. * but excluding the outgoing finished message. */
  639. unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  640. size_t digest_len;
  641. } finished_out;
  642. /* Incoming Finished message */
  643. struct
  644. {
  645. uint8_t preparation_done;
  646. /* Buffer holding digest of the handshake up to but
  647. * excluding the peer's incoming finished message. */
  648. unsigned char digest[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  649. size_t digest_len;
  650. } finished_in;
  651. } state_local;
  652. /* End of state-local variables. */
  653. mbedtls_ssl_ciphersuite_t const *ciphersuite_info;
  654. size_t pmslen; /*!< premaster length */
  655. unsigned char randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN +
  656. MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  657. /*!< random bytes */
  658. unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
  659. /*!< premaster secret */
  660. int resume; /*!< session resume indicator*/
  661. int max_major_ver; /*!< max. major version client*/
  662. int max_minor_ver; /*!< max. minor version client*/
  663. int cli_exts; /*!< client extension presence*/
  664. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  665. int extensions_present; /*!< extension presence; Each bitfield
  666. represents an extension and defined
  667. as \c MBEDTLS_SSL_EXT_XXX */
  668. union
  669. {
  670. unsigned char early [MBEDTLS_TLS1_3_MD_MAX_SIZE];
  671. unsigned char handshake[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  672. unsigned char app [MBEDTLS_TLS1_3_MD_MAX_SIZE];
  673. } tls1_3_master_secrets;
  674. mbedtls_ssl_tls1_3_handshake_secrets tls13_hs_secrets;
  675. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  676. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  677. int new_session_ticket; /*!< use NewSessionTicket? */
  678. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  679. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  680. int extended_ms; /*!< use Extended Master Secret? */
  681. #endif
  682. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  683. unsigned int async_in_progress : 1; /*!< an asynchronous operation is in progress */
  684. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  685. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  686. /** Asynchronous operation context. This field is meant for use by the
  687. * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
  688. * mbedtls_ssl_config::f_async_decrypt_start,
  689. * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
  690. * The library does not use it internally. */
  691. void *user_async_ctx;
  692. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  693. };
  694. typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer;
  695. /*
  696. * Representation of decryption/encryption transformations on records
  697. *
  698. * There are the following general types of record transformations:
  699. * - Stream transformations (TLS versions == 1.2 only)
  700. * Transformation adding a MAC and applying a stream-cipher
  701. * to the authenticated message.
  702. * - CBC block cipher transformations ([D]TLS versions == 1.2 only)
  703. * For TLS 1.2, no IV is generated at key extraction time, but every
  704. * encrypted record is explicitly prefixed by the IV with which it was
  705. * encrypted.
  706. * - AEAD transformations ([D]TLS versions == 1.2 only)
  707. * These come in two fundamentally different versions, the first one
  708. * used in TLS 1.2, excluding ChaChaPoly ciphersuites, and the second
  709. * one used for ChaChaPoly ciphersuites in TLS 1.2 as well as for TLS 1.3.
  710. * In the first transformation, the IV to be used for a record is obtained
  711. * as the concatenation of an explicit, static 4-byte IV and the 8-byte
  712. * record sequence number, and explicitly prepending this sequence number
  713. * to the encrypted record. In contrast, in the second transformation
  714. * the IV is obtained by XOR'ing a static IV obtained at key extraction
  715. * time with the 8-byte record sequence number, without prepending the
  716. * latter to the encrypted record.
  717. *
  718. * Additionally, DTLS 1.2 + CID as well as TLS 1.3 use an inner plaintext
  719. * which allows to add flexible length padding and to hide a record's true
  720. * content type.
  721. *
  722. * In addition to type and version, the following parameters are relevant:
  723. * - The symmetric cipher algorithm to be used.
  724. * - The (static) encryption/decryption keys for the cipher.
  725. * - For stream/CBC, the type of message digest to be used.
  726. * - For stream/CBC, (static) encryption/decryption keys for the digest.
  727. * - For AEAD transformations, the size (potentially 0) of an explicit,
  728. * random initialization vector placed in encrypted records.
  729. * - For some transformations (currently AEAD) an implicit IV. It is static
  730. * and (if present) is combined with the explicit IV in a transformation-
  731. * -dependent way (e.g. appending in TLS 1.2 and XOR'ing in TLS 1.3).
  732. * - For stream/CBC, a flag determining the order of encryption and MAC.
  733. * - The details of the transformation depend on the SSL/TLS version.
  734. * - The length of the authentication tag.
  735. *
  736. * The struct below refines this abstract view as follows:
  737. * - The cipher underlying the transformation is managed in
  738. * cipher contexts cipher_ctx_{enc/dec}, which must have the
  739. * same cipher type. The mode of these cipher contexts determines
  740. * the type of the transformation in the sense above: e.g., if
  741. * the type is MBEDTLS_CIPHER_AES_256_CBC resp. MBEDTLS_CIPHER_AES_192_GCM
  742. * then the transformation has type CBC resp. AEAD.
  743. * - The cipher keys are never stored explicitly but
  744. * are maintained within cipher_ctx_{enc/dec}.
  745. * - For stream/CBC transformations, the message digest contexts
  746. * used for the MAC's are stored in md_ctx_{enc/dec}. These contexts
  747. * are unused for AEAD transformations.
  748. * - For stream/CBC transformations, the MAC keys are not stored explicitly
  749. * but maintained within md_ctx_{enc/dec}.
  750. * - The mac_enc and mac_dec fields are unused for EAD transformations.
  751. * - For transformations using an implicit IV maintained within
  752. * the transformation context, its contents are stored within
  753. * iv_{enc/dec}.
  754. * - The value of ivlen indicates the length of the IV.
  755. * This is redundant in case of stream/CBC transformations
  756. * which always use 0 resp. the cipher's block length as the
  757. * IV length, but is needed for AEAD ciphers and may be
  758. * different from the underlying cipher's block length
  759. * in this case.
  760. * - The field fixed_ivlen is nonzero for AEAD transformations only
  761. * and indicates the length of the static part of the IV which is
  762. * constant throughout the communication, and which is stored in
  763. * the first fixed_ivlen bytes of the iv_{enc/dec} arrays.
  764. * - minor_ver denotes the SSL/TLS version
  765. * - For stream/CBC transformations, maclen denotes the length of the
  766. * authentication tag, while taglen is unused and 0.
  767. * - For AEAD transformations, taglen denotes the length of the
  768. * authentication tag, while maclen is unused and 0.
  769. * - For CBC transformations, encrypt_then_mac determines the
  770. * order of encryption and authentication. This field is unused
  771. * in other transformations.
  772. *
  773. */
  774. struct mbedtls_ssl_transform
  775. {
  776. /*
  777. * Session specific crypto layer
  778. */
  779. size_t minlen; /*!< min. ciphertext length */
  780. size_t ivlen; /*!< IV length */
  781. size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
  782. size_t maclen; /*!< MAC(CBC) len */
  783. size_t taglen; /*!< TAG(AEAD) len */
  784. unsigned char iv_enc[16]; /*!< IV (encryption) */
  785. unsigned char iv_dec[16]; /*!< IV (decryption) */
  786. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  787. mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
  788. mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
  789. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  790. int encrypt_then_mac; /*!< flag for EtM activation */
  791. #endif
  792. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
  793. mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
  794. mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
  795. int minor_ver;
  796. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  797. uint8_t in_cid_len;
  798. uint8_t out_cid_len;
  799. unsigned char in_cid [ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
  800. unsigned char out_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
  801. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  802. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  803. /* We need the Hello random bytes in order to re-derive keys from the
  804. * Master Secret and other session info,
  805. * see ssl_tls12_populate_transform() */
  806. unsigned char randbytes[MBEDTLS_SERVER_HELLO_RANDOM_LEN +
  807. MBEDTLS_CLIENT_HELLO_RANDOM_LEN];
  808. /*!< ServerHello.random+ClientHello.random */
  809. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  810. };
  811. /*
  812. * Return 1 if the transform uses an AEAD cipher, 0 otherwise.
  813. * Equivalently, return 0 if a separate MAC is used, 1 otherwise.
  814. */
  815. static inline int mbedtls_ssl_transform_uses_aead(
  816. const mbedtls_ssl_transform *transform )
  817. {
  818. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
  819. return( transform->maclen == 0 && transform->taglen != 0 );
  820. #else
  821. (void) transform;
  822. return( 1 );
  823. #endif
  824. }
  825. /*
  826. * Internal representation of record frames
  827. *
  828. * Instances come in two flavors:
  829. * (1) Encrypted
  830. * These always have data_offset = 0
  831. * (2) Unencrypted
  832. * These have data_offset set to the amount of
  833. * pre-expansion during record protection. Concretely,
  834. * this is the length of the fixed part of the explicit IV
  835. * used for encryption, or 0 if no explicit IV is used
  836. * (e.g. for stream ciphers).
  837. *
  838. * The reason for the data_offset in the unencrypted case
  839. * is to allow for in-place conversion of an unencrypted to
  840. * an encrypted record. If the offset wasn't included, the
  841. * encrypted content would need to be shifted afterwards to
  842. * make space for the fixed IV.
  843. *
  844. */
  845. #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
  846. #define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_OUT_LEN_MAX
  847. #else
  848. #define MBEDTLS_SSL_CID_LEN_MAX MBEDTLS_SSL_CID_IN_LEN_MAX
  849. #endif
  850. typedef struct
  851. {
  852. uint8_t ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; /* In TLS: The implicit record sequence number.
  853. * In DTLS: The 2-byte epoch followed by
  854. * the 6-byte sequence number.
  855. * This is stored as a raw big endian byte array
  856. * as opposed to a uint64_t because we rarely
  857. * need to perform arithmetic on this, but do
  858. * need it as a Byte array for the purpose of
  859. * MAC computations. */
  860. uint8_t type; /* The record content type. */
  861. uint8_t ver[2]; /* SSL/TLS version as present on the wire.
  862. * Convert to internal presentation of versions
  863. * using mbedtls_ssl_read_version() and
  864. * mbedtls_ssl_write_version().
  865. * Keep wire-format for MAC computations. */
  866. unsigned char *buf; /* Memory buffer enclosing the record content */
  867. size_t buf_len; /* Buffer length */
  868. size_t data_offset; /* Offset of record content */
  869. size_t data_len; /* Length of record content */
  870. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  871. uint8_t cid_len; /* Length of the CID (0 if not present) */
  872. unsigned char cid[ MBEDTLS_SSL_CID_LEN_MAX ]; /* The CID */
  873. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  874. } mbedtls_record;
  875. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  876. /*
  877. * List of certificate + private key pairs
  878. */
  879. struct mbedtls_ssl_key_cert
  880. {
  881. mbedtls_x509_crt *cert; /*!< cert */
  882. mbedtls_pk_context *key; /*!< private key */
  883. mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
  884. };
  885. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  886. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  887. /*
  888. * List of handshake messages kept around for resending
  889. */
  890. struct mbedtls_ssl_flight_item
  891. {
  892. unsigned char *p; /*!< message, including handshake headers */
  893. size_t len; /*!< length of p */
  894. unsigned char type; /*!< type of the message: handshake or CCS */
  895. mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
  896. };
  897. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  898. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  899. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  900. /* Find an entry in a signature-hash set matching a given hash algorithm. */
  901. mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
  902. mbedtls_pk_type_t sig_alg );
  903. /* Add a signature-hash-pair to a signature-hash set */
  904. void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
  905. mbedtls_pk_type_t sig_alg,
  906. mbedtls_md_type_t md_alg );
  907. /* Allow exactly one hash algorithm for each signature. */
  908. void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
  909. mbedtls_md_type_t md_alg );
  910. /* Setup an empty signature-hash set */
  911. static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
  912. {
  913. mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
  914. }
  915. #endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
  916. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  917. /**
  918. * \brief Free referenced items in an SSL transform context and clear
  919. * memory
  920. *
  921. * \param transform SSL transform context
  922. */
  923. void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
  924. /**
  925. * \brief Free referenced items in an SSL handshake context and clear
  926. * memory
  927. *
  928. * \param ssl SSL context
  929. */
  930. void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl );
  931. /* set inbound transform of ssl context */
  932. void mbedtls_ssl_set_inbound_transform( mbedtls_ssl_context *ssl,
  933. mbedtls_ssl_transform *transform );
  934. /* set outbound transform of ssl context */
  935. void mbedtls_ssl_set_outbound_transform( mbedtls_ssl_context *ssl,
  936. mbedtls_ssl_transform *transform );
  937. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  938. int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
  939. unsigned char *buf,
  940. const unsigned char *end,
  941. size_t *olen );
  942. #endif
  943. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
  944. int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
  945. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
  946. int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
  947. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
  948. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
  949. int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
  950. int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
  951. void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
  952. /**
  953. * \brief Update record layer
  954. *
  955. * This function roughly separates the implementation
  956. * of the logic of (D)TLS from the implementation
  957. * of the secure transport.
  958. *
  959. * \param ssl The SSL context to use.
  960. * \param update_hs_digest This indicates if the handshake digest
  961. * should be automatically updated in case
  962. * a handshake message is found.
  963. *
  964. * \return 0 or non-zero error code.
  965. *
  966. * \note A clarification on what is called 'record layer' here
  967. * is in order, as many sensible definitions are possible:
  968. *
  969. * The record layer takes as input an untrusted underlying
  970. * transport (stream or datagram) and transforms it into
  971. * a serially multiplexed, secure transport, which
  972. * conceptually provides the following:
  973. *
  974. * (1) Three datagram based, content-agnostic transports
  975. * for handshake, alert and CCS messages.
  976. * (2) One stream- or datagram-based transport
  977. * for application data.
  978. * (3) Functionality for changing the underlying transform
  979. * securing the contents.
  980. *
  981. * The interface to this functionality is given as follows:
  982. *
  983. * a Updating
  984. * [Currently implemented by mbedtls_ssl_read_record]
  985. *
  986. * Check if and on which of the four 'ports' data is pending:
  987. * Nothing, a controlling datagram of type (1), or application
  988. * data (2). In any case data is present, internal buffers
  989. * provide access to the data for the user to process it.
  990. * Consumption of type (1) datagrams is done automatically
  991. * on the next update, invalidating that the internal buffers
  992. * for previous datagrams, while consumption of application
  993. * data (2) is user-controlled.
  994. *
  995. * b Reading of application data
  996. * [Currently manual adaption of ssl->in_offt pointer]
  997. *
  998. * As mentioned in the last paragraph, consumption of data
  999. * is different from the automatic consumption of control
  1000. * datagrams (1) because application data is treated as a stream.
  1001. *
  1002. * c Tracking availability of application data
  1003. * [Currently manually through decreasing ssl->in_msglen]
  1004. *
  1005. * For efficiency and to retain datagram semantics for
  1006. * application data in case of DTLS, the record layer
  1007. * provides functionality for checking how much application
  1008. * data is still available in the internal buffer.
  1009. *
  1010. * d Changing the transformation securing the communication.
  1011. *
  1012. * Given an opaque implementation of the record layer in the
  1013. * above sense, it should be possible to implement the logic
  1014. * of (D)TLS on top of it without the need to know anything
  1015. * about the record layer's internals. This is done e.g.
  1016. * in all the handshake handling functions, and in the
  1017. * application data reading function mbedtls_ssl_read.
  1018. *
  1019. * \note The above tries to give a conceptual picture of the
  1020. * record layer, but the current implementation deviates
  1021. * from it in some places. For example, our implementation of
  1022. * the update functionality through mbedtls_ssl_read_record
  1023. * discards datagrams depending on the current state, which
  1024. * wouldn't fall under the record layer's responsibility
  1025. * following the above definition.
  1026. *
  1027. */
  1028. int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
  1029. unsigned update_hs_digest );
  1030. int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
  1031. int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl,
  1032. int update_checksum );
  1033. static inline int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
  1034. {
  1035. return( mbedtls_ssl_write_handshake_msg_ext( ssl, 1 /* update checksum */ ) );
  1036. }
  1037. int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush );
  1038. int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
  1039. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
  1040. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
  1041. int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
  1042. int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
  1043. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
  1044. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
  1045. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  1046. const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
  1047. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1048. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
  1049. /**
  1050. * Get the first defined PSK by order of precedence:
  1051. * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
  1052. * 2. static PSK configured by \c mbedtls_ssl_conf_psk()
  1053. * Return a code and update the pair (PSK, PSK length) passed to this function
  1054. */
  1055. static inline int mbedtls_ssl_get_psk( const mbedtls_ssl_context *ssl,
  1056. const unsigned char **psk, size_t *psk_len )
  1057. {
  1058. if( ssl->handshake->psk != NULL && ssl->handshake->psk_len > 0 )
  1059. {
  1060. *psk = ssl->handshake->psk;
  1061. *psk_len = ssl->handshake->psk_len;
  1062. }
  1063. else if( ssl->conf->psk != NULL && ssl->conf->psk_len > 0 )
  1064. {
  1065. *psk = ssl->conf->psk;
  1066. *psk_len = ssl->conf->psk_len;
  1067. }
  1068. else
  1069. {
  1070. *psk = NULL;
  1071. *psk_len = 0;
  1072. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  1073. }
  1074. return( 0 );
  1075. }
  1076. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1077. /**
  1078. * Get the first defined opaque PSK by order of precedence:
  1079. * 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
  1080. * callback
  1081. * 2. static PSK configured by \c mbedtls_ssl_conf_psk_opaque()
  1082. * Return an opaque PSK
  1083. */
  1084. static inline psa_key_id_t mbedtls_ssl_get_opaque_psk(
  1085. const mbedtls_ssl_context *ssl )
  1086. {
  1087. if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
  1088. return( ssl->handshake->psk_opaque );
  1089. if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
  1090. return( ssl->conf->psk_opaque );
  1091. return( MBEDTLS_SVC_KEY_ID_INIT );
  1092. }
  1093. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1094. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1095. #if defined(MBEDTLS_PK_C)
  1096. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
  1097. unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
  1098. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
  1099. #endif
  1100. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
  1101. unsigned char mbedtls_ssl_hash_from_md_alg( int md );
  1102. int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
  1103. #if defined(MBEDTLS_ECP_C)
  1104. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
  1105. #endif
  1106. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1107. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  1108. mbedtls_md_type_t md );
  1109. #endif
  1110. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1111. static inline mbedtls_ssl_srtp_profile mbedtls_ssl_check_srtp_profile_value
  1112. ( const uint16_t srtp_profile_value )
  1113. {
  1114. switch( srtp_profile_value )
  1115. {
  1116. case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80:
  1117. case MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32:
  1118. case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80:
  1119. case MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32:
  1120. return srtp_profile_value;
  1121. default: break;
  1122. }
  1123. return( MBEDTLS_TLS_SRTP_UNSET );
  1124. }
  1125. #endif
  1126. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1127. static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
  1128. {
  1129. mbedtls_ssl_key_cert *key_cert;
  1130. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  1131. key_cert = ssl->handshake->key_cert;
  1132. else
  1133. key_cert = ssl->conf->key_cert;
  1134. return( key_cert == NULL ? NULL : key_cert->key );
  1135. }
  1136. static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
  1137. {
  1138. mbedtls_ssl_key_cert *key_cert;
  1139. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  1140. key_cert = ssl->handshake->key_cert;
  1141. else
  1142. key_cert = ssl->conf->key_cert;
  1143. return( key_cert == NULL ? NULL : key_cert->cert );
  1144. }
  1145. /*
  1146. * Check usage of a certificate wrt extensions:
  1147. * keyUsage, extendedKeyUsage (later), and nSCertType (later).
  1148. *
  1149. * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
  1150. * check a cert we received from them)!
  1151. *
  1152. * Return 0 if everything is OK, -1 if not.
  1153. */
  1154. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  1155. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  1156. int cert_endpoint,
  1157. uint32_t *flags );
  1158. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1159. void mbedtls_ssl_write_version( int major, int minor, int transport,
  1160. unsigned char ver[2] );
  1161. void mbedtls_ssl_read_version( int *major, int *minor, int transport,
  1162. const unsigned char ver[2] );
  1163. static inline size_t mbedtls_ssl_in_hdr_len( const mbedtls_ssl_context *ssl )
  1164. {
  1165. #if !defined(MBEDTLS_SSL_PROTO_DTLS)
  1166. ((void) ssl);
  1167. #endif
  1168. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1169. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1170. {
  1171. return( 13 );
  1172. }
  1173. else
  1174. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1175. {
  1176. return( 5 );
  1177. }
  1178. }
  1179. static inline size_t mbedtls_ssl_out_hdr_len( const mbedtls_ssl_context *ssl )
  1180. {
  1181. return( (size_t) ( ssl->out_iv - ssl->out_hdr ) );
  1182. }
  1183. static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
  1184. {
  1185. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1186. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1187. return( 12 );
  1188. #else
  1189. ((void) ssl);
  1190. #endif
  1191. return( 4 );
  1192. }
  1193. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1194. void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
  1195. void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
  1196. int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
  1197. int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl );
  1198. #endif
  1199. /* Visible for testing purposes only */
  1200. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1201. int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl );
  1202. void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
  1203. #endif
  1204. int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
  1205. const mbedtls_ssl_session *src );
  1206. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1207. /* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */
  1208. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
  1209. unsigned char *hash, size_t *hashlen,
  1210. unsigned char *data, size_t data_len,
  1211. mbedtls_md_type_t md_alg );
  1212. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1213. #ifdef __cplusplus
  1214. }
  1215. #endif
  1216. void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform );
  1217. int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
  1218. mbedtls_ssl_transform *transform,
  1219. mbedtls_record *rec,
  1220. int (*f_rng)(void *, unsigned char *, size_t),
  1221. void *p_rng );
  1222. int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
  1223. mbedtls_ssl_transform *transform,
  1224. mbedtls_record *rec );
  1225. /* Length of the "epoch" field in the record header */
  1226. static inline size_t mbedtls_ssl_ep_len( const mbedtls_ssl_context *ssl )
  1227. {
  1228. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1229. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1230. return( 2 );
  1231. #else
  1232. ((void) ssl);
  1233. #endif
  1234. return( 0 );
  1235. }
  1236. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1237. int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl );
  1238. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1239. void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs );
  1240. int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl );
  1241. void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
  1242. void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
  1243. mbedtls_ssl_transform *transform );
  1244. void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl );
  1245. int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
  1246. /*
  1247. * Send pending alert
  1248. */
  1249. int mbedtls_ssl_handle_pending_alert( mbedtls_ssl_context *ssl );
  1250. /*
  1251. * Set pending fatal alert flag.
  1252. */
  1253. void mbedtls_ssl_pend_fatal_alert( mbedtls_ssl_context *ssl,
  1254. unsigned char alert_type,
  1255. int alert_reason );
  1256. /* Alias of mbedtls_ssl_pend_fatal_alert */
  1257. #define MBEDTLS_SSL_PEND_FATAL_ALERT( type, user_return_value ) \
  1258. mbedtls_ssl_pend_fatal_alert( ssl, type, user_return_value )
  1259. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1260. void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
  1261. #endif
  1262. void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
  1263. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1264. int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl );
  1265. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1266. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1267. size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
  1268. void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl );
  1269. void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight );
  1270. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1271. /**
  1272. * ssl utils functions for checking configuration.
  1273. */
  1274. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1275. static inline int mbedtls_ssl_conf_is_tls13_only( const mbedtls_ssl_config *conf )
  1276. {
  1277. if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1278. conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1279. conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 &&
  1280. conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  1281. {
  1282. return( 1 );
  1283. }
  1284. return( 0 );
  1285. }
  1286. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1287. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1288. static inline int mbedtls_ssl_conf_is_tls12_only( const mbedtls_ssl_config *conf )
  1289. {
  1290. if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1291. conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1292. conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  1293. conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1294. {
  1295. return( 1 );
  1296. }
  1297. return( 0 );
  1298. }
  1299. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1300. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1301. static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13( const mbedtls_ssl_config *conf )
  1302. {
  1303. if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1304. conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  1305. conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  1306. conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  1307. {
  1308. return( 1 );
  1309. }
  1310. return( 0 );
  1311. }
  1312. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL*/
  1313. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1314. int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl );
  1315. int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl );
  1316. void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl );
  1317. /**
  1318. * \brief TLS 1.3 client side state machine entry
  1319. *
  1320. * \param ssl SSL context
  1321. */
  1322. int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl );
  1323. /**
  1324. * \brief TLS 1.3 server side state machine entry
  1325. *
  1326. * \param ssl SSL context
  1327. */
  1328. int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl );
  1329. /*
  1330. * Helper functions around key exchange modes.
  1331. */
  1332. static inline unsigned mbedtls_ssl_conf_tls13_check_kex_modes( mbedtls_ssl_context *ssl,
  1333. int kex_mode_mask )
  1334. {
  1335. return( ( ssl->conf->tls13_kex_modes & kex_mode_mask ) != 0 );
  1336. }
  1337. static inline int mbedtls_ssl_conf_tls13_psk_enabled( mbedtls_ssl_context *ssl )
  1338. {
  1339. return( mbedtls_ssl_conf_tls13_check_kex_modes( ssl,
  1340. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK ) );
  1341. }
  1342. static inline int mbedtls_ssl_conf_tls13_psk_ephemeral_enabled( mbedtls_ssl_context *ssl )
  1343. {
  1344. return( mbedtls_ssl_conf_tls13_check_kex_modes( ssl,
  1345. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL ) );
  1346. }
  1347. static inline int mbedtls_ssl_conf_tls13_ephemeral_enabled( mbedtls_ssl_context *ssl )
  1348. {
  1349. return( mbedtls_ssl_conf_tls13_check_kex_modes( ssl,
  1350. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL ) );
  1351. }
  1352. static inline int mbedtls_ssl_conf_tls13_some_ephemeral_enabled( mbedtls_ssl_context *ssl )
  1353. {
  1354. return( mbedtls_ssl_conf_tls13_check_kex_modes( ssl,
  1355. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL_ALL ) );
  1356. }
  1357. static inline int mbedtls_ssl_conf_tls13_some_psk_enabled( mbedtls_ssl_context *ssl )
  1358. {
  1359. return( mbedtls_ssl_conf_tls13_check_kex_modes( ssl,
  1360. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_ALL ) );
  1361. }
  1362. /**
  1363. * Given a list of key exchange modes, check if at least one of them is
  1364. * supported.
  1365. *
  1366. * \param[in] ssl SSL context
  1367. * \param kex_modes_mask Mask of the key exchange modes to check
  1368. *
  1369. * \return 0 if at least one of the key exchange modes is supported,
  1370. * !=0 otherwise.
  1371. */
  1372. static inline unsigned mbedtls_ssl_tls1_3_check_kex_modes( mbedtls_ssl_context *ssl,
  1373. int kex_modes_mask )
  1374. {
  1375. return( ( ssl->handshake->tls1_3_kex_modes & kex_modes_mask ) == 0 );
  1376. }
  1377. static inline int mbedtls_ssl_tls1_3_psk_enabled( mbedtls_ssl_context *ssl )
  1378. {
  1379. return( ! mbedtls_ssl_tls1_3_check_kex_modes( ssl,
  1380. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK ) );
  1381. }
  1382. static inline int mbedtls_ssl_tls1_3_psk_ephemeral_enabled(
  1383. mbedtls_ssl_context *ssl )
  1384. {
  1385. return( ! mbedtls_ssl_tls1_3_check_kex_modes( ssl,
  1386. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL ) );
  1387. }
  1388. static inline int mbedtls_ssl_tls1_3_ephemeral_enabled( mbedtls_ssl_context *ssl )
  1389. {
  1390. return( ! mbedtls_ssl_tls1_3_check_kex_modes( ssl,
  1391. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL ) );
  1392. }
  1393. static inline int mbedtls_ssl_tls1_3_some_ephemeral_enabled( mbedtls_ssl_context *ssl )
  1394. {
  1395. return( ! mbedtls_ssl_tls1_3_check_kex_modes( ssl,
  1396. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL_ALL ) );
  1397. }
  1398. static inline int mbedtls_ssl_tls1_3_some_psk_enabled( mbedtls_ssl_context *ssl )
  1399. {
  1400. return( ! mbedtls_ssl_tls1_3_check_kex_modes( ssl,
  1401. MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_ALL ) );
  1402. }
  1403. /*
  1404. * Helper functions for NamedGroup.
  1405. */
  1406. static inline int mbedtls_ssl_tls13_named_group_is_ecdhe( uint16_t named_group )
  1407. {
  1408. return( named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 ||
  1409. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 ||
  1410. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1 ||
  1411. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X25519 ||
  1412. named_group == MBEDTLS_SSL_IANA_TLS_GROUP_X448 );
  1413. }
  1414. static inline int mbedtls_ssl_tls13_named_group_is_dhe( uint16_t named_group )
  1415. {
  1416. return( named_group >= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048 &&
  1417. named_group <= MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192 );
  1418. }
  1419. static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context *ssl,
  1420. mbedtls_ssl_states state )
  1421. {
  1422. ssl->state = ( int ) state;
  1423. }
  1424. /*
  1425. * Fetch TLS 1.3 handshake message header
  1426. */
  1427. int mbedtls_ssl_tls1_3_fetch_handshake_msg( mbedtls_ssl_context *ssl,
  1428. unsigned hs_type,
  1429. unsigned char **buf,
  1430. size_t *buf_len );
  1431. /*
  1432. * Write TLS 1.3 handshake message header
  1433. */
  1434. int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
  1435. unsigned hs_type,
  1436. unsigned char **buf,
  1437. size_t *buflen );
  1438. /*
  1439. * Handler of TLS 1.3 server certificate message
  1440. */
  1441. int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl );
  1442. /*
  1443. * Generic handler of Certificate Verify
  1444. */
  1445. int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl );
  1446. /*
  1447. * Write TLS 1.3 handshake message tail
  1448. */
  1449. int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
  1450. size_t buf_len,
  1451. size_t msg_len );
  1452. void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
  1453. unsigned hs_type,
  1454. size_t total_hs_len );
  1455. /*
  1456. * Update checksum of handshake messages.
  1457. */
  1458. void mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
  1459. unsigned hs_type,
  1460. unsigned char const *msg,
  1461. size_t msg_len );
  1462. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1463. /*
  1464. * Write TLS 1.3 Signature Algorithm extension
  1465. */
  1466. int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
  1467. unsigned char *buf,
  1468. unsigned char *end,
  1469. size_t *olen);
  1470. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1471. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1472. /* Get handshake transcript */
  1473. int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
  1474. const mbedtls_md_type_t md,
  1475. unsigned char *dst,
  1476. size_t dst_len,
  1477. size_t *olen );
  1478. /*
  1479. * Return supported groups.
  1480. *
  1481. * In future, invocations can be changed to ssl->conf->group_list
  1482. * when mbedtls_ssl_conf_curves() is deleted.
  1483. *
  1484. * ssl->handshake->group_list is either a translation of curve_list to IANA TLS group
  1485. * identifiers when mbedtls_ssl_conf_curves() has been used, or a pointer to
  1486. * ssl->conf->group_list when mbedtls_ssl_conf_groups() has been more recently invoked.
  1487. *
  1488. */
  1489. static inline const void *mbedtls_ssl_get_groups( const mbedtls_ssl_context *ssl )
  1490. {
  1491. #if defined(MBEDTLS_DEPRECATED_REMOVED) || !defined(MBEDTLS_ECP_C)
  1492. return( ssl->conf->group_list );
  1493. #else
  1494. if( ( ssl->handshake != NULL ) && ( ssl->handshake->group_list != NULL ) )
  1495. return( ssl->handshake->group_list );
  1496. else
  1497. return( ssl->conf->group_list );
  1498. #endif
  1499. }
  1500. #endif /* ssl_misc.h */