@scriptc/runtime 0.0.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (416) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +16 -6
  3. package/src/scr_array.c +488 -0
  4. package/src/scr_assert.c +1393 -0
  5. package/src/scr_async.c +2593 -0
  6. package/src/scr_async_dyn.c +607 -0
  7. package/src/scr_bytes.c +1425 -0
  8. package/src/scr_bytes_io.c +161 -0
  9. package/src/scr_child.c +3587 -0
  10. package/src/scr_closure.c +214 -0
  11. package/src/scr_console.c +143 -0
  12. package/src/scr_cycle.c +218 -0
  13. package/src/scr_dc.c +805 -0
  14. package/src/scr_dgram.c +1007 -0
  15. package/src/scr_dyn_handle.c +154 -0
  16. package/src/scr_dyn_invoke.c +644 -0
  17. package/src/scr_error.c +332 -0
  18. package/src/scr_events.c +714 -0
  19. package/src/scr_events_emitter.c +699 -0
  20. package/src/scr_exception.c +242 -0
  21. package/src/scr_fetch.c +1130 -0
  22. package/src/scr_fetch_curl.c +654 -0
  23. package/src/scr_http.c +3482 -0
  24. package/src/scr_http2.c +3030 -0
  25. package/src/scr_inspect.c +803 -0
  26. package/src/scr_inspect_island.c +51 -0
  27. package/src/scr_island.c +9363 -0
  28. package/src/scr_json.c +2151 -0
  29. package/src/scr_lib.c +3612 -0
  30. package/src/scr_loop_epoll.c +241 -0
  31. package/src/scr_loop_kqueue.c +121 -0
  32. package/src/scr_loop_wsapoll.c +208 -0
  33. package/src/scr_map.c +591 -0
  34. package/src/scr_net.c +3017 -0
  35. package/src/scr_net_island.c +458 -0
  36. package/src/scr_number.c +115 -0
  37. package/src/scr_object.c +25 -0
  38. package/src/scr_path.c +1266 -0
  39. package/src/scr_platform.h +119 -0
  40. package/src/scr_readline.c +287 -0
  41. package/src/scr_regex.c +1080 -0
  42. package/src/scr_runtime.h +5046 -0
  43. package/src/scr_stream.c +2877 -0
  44. package/src/scr_string.c +1264 -0
  45. package/src/scr_symbol.c +132 -0
  46. package/src/scr_test.c +662 -0
  47. package/src/scr_tls.c +1520 -0
  48. package/src/scr_union.c +105 -0
  49. package/src/scr_url.c +911 -0
  50. package/src/scr_url_internal.h +29 -0
  51. package/src/scr_url_params.c +561 -0
  52. package/src/scr_watch.c +568 -0
  53. package/src/scr_web.c +1778 -0
  54. package/src/scr_win.c +76 -0
  55. package/src/scr_zlib.c +197 -0
  56. package/src/scr_zlib_island.c +15 -0
  57. package/vendor/README.md +52 -0
  58. package/vendor/curl/COPYRIGHT +534 -0
  59. package/vendor/curl/include/curl/curl.h +3214 -0
  60. package/vendor/curl/include/curl/curlver.h +79 -0
  61. package/vendor/curl/include/curl/easy.h +125 -0
  62. package/vendor/curl/include/curl/header.h +74 -0
  63. package/vendor/curl/include/curl/mprintf.h +52 -0
  64. package/vendor/curl/include/curl/multi.h +460 -0
  65. package/vendor/curl/include/curl/options.h +70 -0
  66. package/vendor/curl/include/curl/stdcheaders.h +35 -0
  67. package/vendor/curl/include/curl/system.h +508 -0
  68. package/vendor/curl/include/curl/typecheck-gcc.h +716 -0
  69. package/vendor/curl/include/curl/urlapi.h +149 -0
  70. package/vendor/curl/include/curl/websockets.h +84 -0
  71. package/vendor/mbedtls/LICENSE +553 -0
  72. package/vendor/mbedtls/include/CMakeLists.txt +22 -0
  73. package/vendor/mbedtls/include/mbedtls/aes.h +631 -0
  74. package/vendor/mbedtls/include/mbedtls/aria.h +343 -0
  75. package/vendor/mbedtls/include/mbedtls/asn1.h +642 -0
  76. package/vendor/mbedtls/include/mbedtls/asn1write.h +390 -0
  77. package/vendor/mbedtls/include/mbedtls/base64.h +82 -0
  78. package/vendor/mbedtls/include/mbedtls/bignum.h +1088 -0
  79. package/vendor/mbedtls/include/mbedtls/block_cipher.h +76 -0
  80. package/vendor/mbedtls/include/mbedtls/build_info.h +194 -0
  81. package/vendor/mbedtls/include/mbedtls/camellia.h +305 -0
  82. package/vendor/mbedtls/include/mbedtls/ccm.h +526 -0
  83. package/vendor/mbedtls/include/mbedtls/chacha20.h +209 -0
  84. package/vendor/mbedtls/include/mbedtls/chachapoly.h +351 -0
  85. package/vendor/mbedtls/include/mbedtls/check_config.h +1149 -0
  86. package/vendor/mbedtls/include/mbedtls/cipher.h +1250 -0
  87. package/vendor/mbedtls/include/mbedtls/cmac.h +246 -0
  88. package/vendor/mbedtls/include/mbedtls/compat-2.x.h +46 -0
  89. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_crypto.h +578 -0
  90. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_from_psa.h +873 -0
  91. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_from_legacy.h +359 -0
  92. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_superset_legacy.h +145 -0
  93. package/vendor/mbedtls/include/mbedtls/config_adjust_ssl.h +91 -0
  94. package/vendor/mbedtls/include/mbedtls/config_adjust_x509.h +35 -0
  95. package/vendor/mbedtls/include/mbedtls/config_psa.h +61 -0
  96. package/vendor/mbedtls/include/mbedtls/constant_time.h +36 -0
  97. package/vendor/mbedtls/include/mbedtls/ctr_drbg.h +596 -0
  98. package/vendor/mbedtls/include/mbedtls/debug.h +156 -0
  99. package/vendor/mbedtls/include/mbedtls/des.h +385 -0
  100. package/vendor/mbedtls/include/mbedtls/dhm.h +972 -0
  101. package/vendor/mbedtls/include/mbedtls/ecdh.h +455 -0
  102. package/vendor/mbedtls/include/mbedtls/ecdsa.h +674 -0
  103. package/vendor/mbedtls/include/mbedtls/ecjpake.h +298 -0
  104. package/vendor/mbedtls/include/mbedtls/ecp.h +1517 -0
  105. package/vendor/mbedtls/include/mbedtls/entropy.h +274 -0
  106. package/vendor/mbedtls/include/mbedtls/error.h +201 -0
  107. package/vendor/mbedtls/include/mbedtls/gcm.h +387 -0
  108. package/vendor/mbedtls/include/mbedtls/hkdf.h +124 -0
  109. package/vendor/mbedtls/include/mbedtls/hmac_drbg.h +434 -0
  110. package/vendor/mbedtls/include/mbedtls/lms.h +440 -0
  111. package/vendor/mbedtls/include/mbedtls/mbedtls_config.h +4446 -0
  112. package/vendor/mbedtls/include/mbedtls/md.h +526 -0
  113. package/vendor/mbedtls/include/mbedtls/md5.h +190 -0
  114. package/vendor/mbedtls/include/mbedtls/memory_buffer_alloc.h +142 -0
  115. package/vendor/mbedtls/include/mbedtls/net_sockets.h +299 -0
  116. package/vendor/mbedtls/include/mbedtls/nist_kw.h +166 -0
  117. package/vendor/mbedtls/include/mbedtls/oid.h +727 -0
  118. package/vendor/mbedtls/include/mbedtls/pem.h +160 -0
  119. package/vendor/mbedtls/include/mbedtls/pk.h +1303 -0
  120. package/vendor/mbedtls/include/mbedtls/pkcs12.h +186 -0
  121. package/vendor/mbedtls/include/mbedtls/pkcs5.h +198 -0
  122. package/vendor/mbedtls/include/mbedtls/pkcs7.h +252 -0
  123. package/vendor/mbedtls/include/mbedtls/platform.h +516 -0
  124. package/vendor/mbedtls/include/mbedtls/platform_time.h +79 -0
  125. package/vendor/mbedtls/include/mbedtls/platform_util.h +247 -0
  126. package/vendor/mbedtls/include/mbedtls/poly1305.h +168 -0
  127. package/vendor/mbedtls/include/mbedtls/private_access.h +20 -0
  128. package/vendor/mbedtls/include/mbedtls/psa_util.h +207 -0
  129. package/vendor/mbedtls/include/mbedtls/ripemd160.h +136 -0
  130. package/vendor/mbedtls/include/mbedtls/rsa.h +1170 -0
  131. package/vendor/mbedtls/include/mbedtls/sha1.h +219 -0
  132. package/vendor/mbedtls/include/mbedtls/sha256.h +200 -0
  133. package/vendor/mbedtls/include/mbedtls/sha3.h +172 -0
  134. package/vendor/mbedtls/include/mbedtls/sha512.h +208 -0
  135. package/vendor/mbedtls/include/mbedtls/ssl.h +5887 -0
  136. package/vendor/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  137. package/vendor/mbedtls/include/mbedtls/ssl_ciphersuites.h +482 -0
  138. package/vendor/mbedtls/include/mbedtls/ssl_cookie.h +106 -0
  139. package/vendor/mbedtls/include/mbedtls/ssl_ticket.h +199 -0
  140. package/vendor/mbedtls/include/mbedtls/threading.h +167 -0
  141. package/vendor/mbedtls/include/mbedtls/timing.h +94 -0
  142. package/vendor/mbedtls/include/mbedtls/version.h +78 -0
  143. package/vendor/mbedtls/include/mbedtls/x509.h +500 -0
  144. package/vendor/mbedtls/include/mbedtls/x509_crl.h +184 -0
  145. package/vendor/mbedtls/include/mbedtls/x509_crt.h +1208 -0
  146. package/vendor/mbedtls/include/mbedtls/x509_csr.h +382 -0
  147. package/vendor/mbedtls/include/psa/build_info.h +20 -0
  148. package/vendor/mbedtls/include/psa/crypto.h +4998 -0
  149. package/vendor/mbedtls/include/psa/crypto_adjust_auto_enabled.h +31 -0
  150. package/vendor/mbedtls/include/psa/crypto_adjust_config_dependencies.h +51 -0
  151. package/vendor/mbedtls/include/psa/crypto_adjust_config_key_pair_types.h +101 -0
  152. package/vendor/mbedtls/include/psa/crypto_adjust_config_synonyms.h +49 -0
  153. package/vendor/mbedtls/include/psa/crypto_builtin_composites.h +214 -0
  154. package/vendor/mbedtls/include/psa/crypto_builtin_key_derivation.h +118 -0
  155. package/vendor/mbedtls/include/psa/crypto_builtin_primitives.h +114 -0
  156. package/vendor/mbedtls/include/psa/crypto_compat.h +230 -0
  157. package/vendor/mbedtls/include/psa/crypto_config.h +145 -0
  158. package/vendor/mbedtls/include/psa/crypto_driver_common.h +44 -0
  159. package/vendor/mbedtls/include/psa/crypto_driver_contexts_composites.h +151 -0
  160. package/vendor/mbedtls/include/psa/crypto_driver_contexts_key_derivation.h +52 -0
  161. package/vendor/mbedtls/include/psa/crypto_driver_contexts_primitives.h +105 -0
  162. package/vendor/mbedtls/include/psa/crypto_extra.h +2145 -0
  163. package/vendor/mbedtls/include/psa/crypto_legacy.h +88 -0
  164. package/vendor/mbedtls/include/psa/crypto_platform.h +102 -0
  165. package/vendor/mbedtls/include/psa/crypto_se_driver.h +1383 -0
  166. package/vendor/mbedtls/include/psa/crypto_sizes.h +1319 -0
  167. package/vendor/mbedtls/include/psa/crypto_struct.h +527 -0
  168. package/vendor/mbedtls/include/psa/crypto_types.h +508 -0
  169. package/vendor/mbedtls/include/psa/crypto_values.h +2782 -0
  170. package/vendor/mbedtls/library/aes.c +2294 -0
  171. package/vendor/mbedtls/library/aesce.c +624 -0
  172. package/vendor/mbedtls/library/aesce.h +136 -0
  173. package/vendor/mbedtls/library/aesni.c +846 -0
  174. package/vendor/mbedtls/library/aesni.h +162 -0
  175. package/vendor/mbedtls/library/alignment.h +704 -0
  176. package/vendor/mbedtls/library/aria.c +969 -0
  177. package/vendor/mbedtls/library/asn1parse.c +468 -0
  178. package/vendor/mbedtls/library/asn1write.c +440 -0
  179. package/vendor/mbedtls/library/base64.c +322 -0
  180. package/vendor/mbedtls/library/base64_internal.h +45 -0
  181. package/vendor/mbedtls/library/bignum.c +2583 -0
  182. package/vendor/mbedtls/library/bignum_core.c +1240 -0
  183. package/vendor/mbedtls/library/bignum_core.h +872 -0
  184. package/vendor/mbedtls/library/bignum_core_invasive.h +38 -0
  185. package/vendor/mbedtls/library/bignum_internal.h +122 -0
  186. package/vendor/mbedtls/library/bignum_mod.c +394 -0
  187. package/vendor/mbedtls/library/bignum_mod.h +452 -0
  188. package/vendor/mbedtls/library/bignum_mod_raw.c +276 -0
  189. package/vendor/mbedtls/library/bignum_mod_raw.h +416 -0
  190. package/vendor/mbedtls/library/bignum_mod_raw_invasive.h +34 -0
  191. package/vendor/mbedtls/library/block_cipher.c +207 -0
  192. package/vendor/mbedtls/library/block_cipher_internal.h +99 -0
  193. package/vendor/mbedtls/library/bn_mul.h +1094 -0
  194. package/vendor/mbedtls/library/camellia.c +1058 -0
  195. package/vendor/mbedtls/library/ccm.c +777 -0
  196. package/vendor/mbedtls/library/chacha20.c +563 -0
  197. package/vendor/mbedtls/library/chacha20_internal.h +23 -0
  198. package/vendor/mbedtls/library/chachapoly.c +487 -0
  199. package/vendor/mbedtls/library/check_crypto_config.h +136 -0
  200. package/vendor/mbedtls/library/cipher.c +1712 -0
  201. package/vendor/mbedtls/library/cipher_invasive.h +28 -0
  202. package/vendor/mbedtls/library/cipher_wrap.c +2482 -0
  203. package/vendor/mbedtls/library/cipher_wrap.h +178 -0
  204. package/vendor/mbedtls/library/cmac.c +1067 -0
  205. package/vendor/mbedtls/library/common.h +487 -0
  206. package/vendor/mbedtls/library/constant_time.c +247 -0
  207. package/vendor/mbedtls/library/constant_time_impl.h +541 -0
  208. package/vendor/mbedtls/library/constant_time_internal.h +579 -0
  209. package/vendor/mbedtls/library/ctr.h +35 -0
  210. package/vendor/mbedtls/library/ctr_drbg.c +1016 -0
  211. package/vendor/mbedtls/library/debug.c +475 -0
  212. package/vendor/mbedtls/library/debug_internal.h +185 -0
  213. package/vendor/mbedtls/library/des.c +1042 -0
  214. package/vendor/mbedtls/library/dhm.c +700 -0
  215. package/vendor/mbedtls/library/ecdh.c +696 -0
  216. package/vendor/mbedtls/library/ecdsa.c +858 -0
  217. package/vendor/mbedtls/library/ecjpake.c +1216 -0
  218. package/vendor/mbedtls/library/ecp.c +3674 -0
  219. package/vendor/mbedtls/library/ecp_curves.c +6125 -0
  220. package/vendor/mbedtls/library/ecp_curves_new.c +9 -0
  221. package/vendor/mbedtls/library/ecp_internal_alt.h +287 -0
  222. package/vendor/mbedtls/library/ecp_invasive.h +305 -0
  223. package/vendor/mbedtls/library/entropy.c +680 -0
  224. package/vendor/mbedtls/library/entropy_poll.c +233 -0
  225. package/vendor/mbedtls/library/entropy_poll.h +64 -0
  226. package/vendor/mbedtls/library/error.c +878 -0
  227. package/vendor/mbedtls/library/gcm.c +1330 -0
  228. package/vendor/mbedtls/library/hkdf.c +161 -0
  229. package/vendor/mbedtls/library/hmac_drbg.c +633 -0
  230. package/vendor/mbedtls/library/lmots.c +789 -0
  231. package/vendor/mbedtls/library/lmots.h +288 -0
  232. package/vendor/mbedtls/library/lms.c +778 -0
  233. package/vendor/mbedtls/library/md.c +1108 -0
  234. package/vendor/mbedtls/library/md5.c +426 -0
  235. package/vendor/mbedtls/library/md_psa.h +26 -0
  236. package/vendor/mbedtls/library/md_wrap.h +46 -0
  237. package/vendor/mbedtls/library/memory_buffer_alloc.c +751 -0
  238. package/vendor/mbedtls/library/mps_common.h +181 -0
  239. package/vendor/mbedtls/library/mps_error.h +89 -0
  240. package/vendor/mbedtls/library/mps_reader.c +538 -0
  241. package/vendor/mbedtls/library/mps_reader.h +366 -0
  242. package/vendor/mbedtls/library/mps_trace.c +112 -0
  243. package/vendor/mbedtls/library/mps_trace.h +154 -0
  244. package/vendor/mbedtls/library/net_sockets.c +694 -0
  245. package/vendor/mbedtls/library/nist_kw.c +729 -0
  246. package/vendor/mbedtls/library/oid.c +1166 -0
  247. package/vendor/mbedtls/library/padlock.c +157 -0
  248. package/vendor/mbedtls/library/padlock.h +111 -0
  249. package/vendor/mbedtls/library/pem.c +554 -0
  250. package/vendor/mbedtls/library/pk.c +1602 -0
  251. package/vendor/mbedtls/library/pk_ecc.c +261 -0
  252. package/vendor/mbedtls/library/pk_internal.h +241 -0
  253. package/vendor/mbedtls/library/pk_wrap.c +1618 -0
  254. package/vendor/mbedtls/library/pk_wrap.h +138 -0
  255. package/vendor/mbedtls/library/pkcs12.c +437 -0
  256. package/vendor/mbedtls/library/pkcs5.c +500 -0
  257. package/vendor/mbedtls/library/pkcs7.c +787 -0
  258. package/vendor/mbedtls/library/pkparse.c +1392 -0
  259. package/vendor/mbedtls/library/pkwrite.c +631 -0
  260. package/vendor/mbedtls/library/pkwrite.h +121 -0
  261. package/vendor/mbedtls/library/platform.c +402 -0
  262. package/vendor/mbedtls/library/platform_util.c +258 -0
  263. package/vendor/mbedtls/library/poly1305.c +492 -0
  264. package/vendor/mbedtls/library/psa_crypto.c +9532 -0
  265. package/vendor/mbedtls/library/psa_crypto_aead.c +646 -0
  266. package/vendor/mbedtls/library/psa_crypto_aead.h +499 -0
  267. package/vendor/mbedtls/library/psa_crypto_cipher.c +747 -0
  268. package/vendor/mbedtls/library/psa_crypto_cipher.h +316 -0
  269. package/vendor/mbedtls/library/psa_crypto_client.c +22 -0
  270. package/vendor/mbedtls/library/psa_crypto_core.h +983 -0
  271. package/vendor/mbedtls/library/psa_crypto_core_common.h +52 -0
  272. package/vendor/mbedtls/library/psa_crypto_driver_wrappers.h +2896 -0
  273. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.c +256 -0
  274. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.h +31 -0
  275. package/vendor/mbedtls/library/psa_crypto_ecp.c +594 -0
  276. package/vendor/mbedtls/library/psa_crypto_ecp.h +267 -0
  277. package/vendor/mbedtls/library/psa_crypto_ffdh.c +361 -0
  278. package/vendor/mbedtls/library/psa_crypto_ffdh.h +131 -0
  279. package/vendor/mbedtls/library/psa_crypto_hash.c +470 -0
  280. package/vendor/mbedtls/library/psa_crypto_hash.h +211 -0
  281. package/vendor/mbedtls/library/psa_crypto_invasive.h +92 -0
  282. package/vendor/mbedtls/library/psa_crypto_its.h +131 -0
  283. package/vendor/mbedtls/library/psa_crypto_mac.c +505 -0
  284. package/vendor/mbedtls/library/psa_crypto_mac.h +264 -0
  285. package/vendor/mbedtls/library/psa_crypto_pake.c +571 -0
  286. package/vendor/mbedtls/library/psa_crypto_pake.h +159 -0
  287. package/vendor/mbedtls/library/psa_crypto_random.c +181 -0
  288. package/vendor/mbedtls/library/psa_crypto_random.h +72 -0
  289. package/vendor/mbedtls/library/psa_crypto_random_impl.h +200 -0
  290. package/vendor/mbedtls/library/psa_crypto_rsa.c +714 -0
  291. package/vendor/mbedtls/library/psa_crypto_rsa.h +321 -0
  292. package/vendor/mbedtls/library/psa_crypto_se.c +373 -0
  293. package/vendor/mbedtls/library/psa_crypto_se.h +192 -0
  294. package/vendor/mbedtls/library/psa_crypto_slot_management.c +1137 -0
  295. package/vendor/mbedtls/library/psa_crypto_slot_management.h +344 -0
  296. package/vendor/mbedtls/library/psa_crypto_storage.c +481 -0
  297. package/vendor/mbedtls/library/psa_crypto_storage.h +392 -0
  298. package/vendor/mbedtls/library/psa_its_file.c +254 -0
  299. package/vendor/mbedtls/library/psa_util.c +614 -0
  300. package/vendor/mbedtls/library/psa_util_internal.h +100 -0
  301. package/vendor/mbedtls/library/ripemd160.c +490 -0
  302. package/vendor/mbedtls/library/rsa.c +3121 -0
  303. package/vendor/mbedtls/library/rsa_alt_helpers.c +455 -0
  304. package/vendor/mbedtls/library/rsa_alt_helpers.h +209 -0
  305. package/vendor/mbedtls/library/rsa_internal.h +188 -0
  306. package/vendor/mbedtls/library/sha1.c +480 -0
  307. package/vendor/mbedtls/library/sha256.c +983 -0
  308. package/vendor/mbedtls/library/sha3.c +721 -0
  309. package/vendor/mbedtls/library/sha512.c +1115 -0
  310. package/vendor/mbedtls/library/ssl_cache.c +410 -0
  311. package/vendor/mbedtls/library/ssl_ciphersuites.c +2050 -0
  312. package/vendor/mbedtls/library/ssl_ciphersuites_internal.h +154 -0
  313. package/vendor/mbedtls/library/ssl_client.c +1023 -0
  314. package/vendor/mbedtls/library/ssl_client.h +22 -0
  315. package/vendor/mbedtls/library/ssl_cookie.c +384 -0
  316. package/vendor/mbedtls/library/ssl_debug_helpers.h +85 -0
  317. package/vendor/mbedtls/library/ssl_debug_helpers_generated.c +251 -0
  318. package/vendor/mbedtls/library/ssl_misc.h +3198 -0
  319. package/vendor/mbedtls/library/ssl_msg.c +6619 -0
  320. package/vendor/mbedtls/library/ssl_ticket.c +556 -0
  321. package/vendor/mbedtls/library/ssl_tls.c +10234 -0
  322. package/vendor/mbedtls/library/ssl_tls12_client.c +3688 -0
  323. package/vendor/mbedtls/library/ssl_tls12_server.c +4311 -0
  324. package/vendor/mbedtls/library/ssl_tls13_client.c +3208 -0
  325. package/vendor/mbedtls/library/ssl_tls13_generic.c +1793 -0
  326. package/vendor/mbedtls/library/ssl_tls13_invasive.h +23 -0
  327. package/vendor/mbedtls/library/ssl_tls13_keys.c +1918 -0
  328. package/vendor/mbedtls/library/ssl_tls13_keys.h +668 -0
  329. package/vendor/mbedtls/library/ssl_tls13_server.c +3624 -0
  330. package/vendor/mbedtls/library/threading.c +193 -0
  331. package/vendor/mbedtls/library/threading_internal.h +28 -0
  332. package/vendor/mbedtls/library/timing.c +152 -0
  333. package/vendor/mbedtls/library/version.c +32 -0
  334. package/vendor/mbedtls/library/version_features.c +856 -0
  335. package/vendor/mbedtls/library/x509.c +1776 -0
  336. package/vendor/mbedtls/library/x509_create.c +570 -0
  337. package/vendor/mbedtls/library/x509_crl.c +708 -0
  338. package/vendor/mbedtls/library/x509_crt.c +3311 -0
  339. package/vendor/mbedtls/library/x509_csr.c +648 -0
  340. package/vendor/mbedtls/library/x509_internal.h +101 -0
  341. package/vendor/mbedtls/library/x509write.c +174 -0
  342. package/vendor/mbedtls/library/x509write_crt.c +688 -0
  343. package/vendor/mbedtls/library/x509write_csr.c +336 -0
  344. package/vendor/quickjs-ng/CMakeLists.txt +566 -0
  345. package/vendor/quickjs-ng/LICENSE +24 -0
  346. package/vendor/quickjs-ng/README.md +24 -0
  347. package/vendor/quickjs-ng/api-test.c +1195 -0
  348. package/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
  349. package/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
  350. package/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
  351. package/vendor/quickjs-ng/cutils.h +1998 -0
  352. package/vendor/quickjs-ng/dtoa.c +1619 -0
  353. package/vendor/quickjs-ng/dtoa.h +87 -0
  354. package/vendor/quickjs-ng/gen/function_source.c +81 -0
  355. package/vendor/quickjs-ng/gen/hello.c +53 -0
  356. package/vendor/quickjs-ng/gen/hello_module.c +106 -0
  357. package/vendor/quickjs-ng/gen/repl.c +3036 -0
  358. package/vendor/quickjs-ng/gen/standalone.c +323 -0
  359. package/vendor/quickjs-ng/gen/test_fib.c +81 -0
  360. package/vendor/quickjs-ng/libregexp-opcode.h +73 -0
  361. package/vendor/quickjs-ng/libregexp.c +3474 -0
  362. package/vendor/quickjs-ng/libregexp.h +101 -0
  363. package/vendor/quickjs-ng/libunicode-table.h +5173 -0
  364. package/vendor/quickjs-ng/libunicode.c +2069 -0
  365. package/vendor/quickjs-ng/libunicode.h +172 -0
  366. package/vendor/quickjs-ng/list.h +107 -0
  367. package/vendor/quickjs-ng/lre-test.c +52 -0
  368. package/vendor/quickjs-ng/qjs.c +762 -0
  369. package/vendor/quickjs-ng/qjsc.c +673 -0
  370. package/vendor/quickjs-ng/quickjs-atom.h +280 -0
  371. package/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
  372. package/vendor/quickjs-ng/quickjs-libc.c +5037 -0
  373. package/vendor/quickjs-ng/quickjs-libc.h +87 -0
  374. package/vendor/quickjs-ng/quickjs-opcode.h +377 -0
  375. package/vendor/quickjs-ng/quickjs.c +64041 -0
  376. package/vendor/quickjs-ng/quickjs.h +1447 -0
  377. package/vendor/quickjs-ng/run-test262.c +2374 -0
  378. package/vendor/quickjs-ng/unicode_gen.c +3121 -0
  379. package/vendor/quickjs-ng/unicode_gen_def.h +310 -0
  380. package/vendor/ryu/LICENSE-Boost +23 -0
  381. package/vendor/ryu/common.h +114 -0
  382. package/vendor/ryu/d2s.c +509 -0
  383. package/vendor/ryu/d2s_full_table.h +367 -0
  384. package/vendor/ryu/d2s_intrinsics.h +357 -0
  385. package/vendor/ryu/d2s_small_table.h +186 -0
  386. package/vendor/ryu/digit_table.h +35 -0
  387. package/vendor/ryu/ryu.h +46 -0
  388. package/vendor/zlib/LICENSE +22 -0
  389. package/vendor/zlib/adler32.c +164 -0
  390. package/vendor/zlib/compress.c +75 -0
  391. package/vendor/zlib/crc32.c +1049 -0
  392. package/vendor/zlib/crc32.h +9446 -0
  393. package/vendor/zlib/deflate.c +2139 -0
  394. package/vendor/zlib/deflate.h +377 -0
  395. package/vendor/zlib/gzclose.c +23 -0
  396. package/vendor/zlib/gzguts.h +214 -0
  397. package/vendor/zlib/gzlib.c +582 -0
  398. package/vendor/zlib/gzread.c +602 -0
  399. package/vendor/zlib/gzwrite.c +631 -0
  400. package/vendor/zlib/infback.c +628 -0
  401. package/vendor/zlib/inffast.c +320 -0
  402. package/vendor/zlib/inffast.h +11 -0
  403. package/vendor/zlib/inffixed.h +94 -0
  404. package/vendor/zlib/inflate.c +1526 -0
  405. package/vendor/zlib/inflate.h +126 -0
  406. package/vendor/zlib/inftrees.c +299 -0
  407. package/vendor/zlib/inftrees.h +62 -0
  408. package/vendor/zlib/trees.c +1117 -0
  409. package/vendor/zlib/trees.h +128 -0
  410. package/vendor/zlib/uncompr.c +85 -0
  411. package/vendor/zlib/zconf.h +543 -0
  412. package/vendor/zlib/zlib.h +1938 -0
  413. package/vendor/zlib/zutil.c +299 -0
  414. package/vendor/zlib/zutil.h +254 -0
  415. package/README.md +0 -3
  416. package/index.js +0 -2
@@ -0,0 +1,4311 @@
1
+ /*
2
+ * TLS server-side functions
3
+ *
4
+ * Copyright The Mbed TLS Contributors
5
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
+ */
7
+
8
+ #include "common.h"
9
+
10
+ #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
11
+
12
+ #include "mbedtls/platform.h"
13
+
14
+ #include "mbedtls/ssl.h"
15
+ #include "ssl_misc.h"
16
+ #include "debug_internal.h"
17
+ #include "mbedtls/error.h"
18
+ #include "mbedtls/platform_util.h"
19
+ #include "constant_time_internal.h"
20
+ #include "mbedtls/constant_time.h"
21
+
22
+ #include <string.h>
23
+
24
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
25
+ /* Define a local translating function to save code size by not using too many
26
+ * arguments in each translating place. */
27
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED) || \
28
+ defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
29
+ static int local_err_translation(psa_status_t status)
30
+ {
31
+ return psa_status_to_mbedtls(status, psa_to_ssl_errors,
32
+ ARRAY_LENGTH(psa_to_ssl_errors),
33
+ psa_generic_status_to_mbedtls);
34
+ }
35
+ #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
36
+ #endif
37
+ #endif
38
+
39
+ #if defined(MBEDTLS_ECP_C)
40
+ #include "mbedtls/ecp.h"
41
+ #endif
42
+
43
+ #if defined(MBEDTLS_HAVE_TIME)
44
+ #include "mbedtls/platform_time.h"
45
+ #endif
46
+
47
+ #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
48
+ int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context *ssl,
49
+ const unsigned char *info,
50
+ size_t ilen)
51
+ {
52
+ if (ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER) {
53
+ return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
54
+ }
55
+
56
+ mbedtls_free(ssl->cli_id);
57
+
58
+ if ((ssl->cli_id = mbedtls_calloc(1, ilen)) == NULL) {
59
+ return MBEDTLS_ERR_SSL_ALLOC_FAILED;
60
+ }
61
+
62
+ memcpy(ssl->cli_id, info, ilen);
63
+ ssl->cli_id_len = ilen;
64
+
65
+ return 0;
66
+ }
67
+
68
+ void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
69
+ mbedtls_ssl_cookie_write_t *f_cookie_write,
70
+ mbedtls_ssl_cookie_check_t *f_cookie_check,
71
+ void *p_cookie)
72
+ {
73
+ conf->f_cookie_write = f_cookie_write;
74
+ conf->f_cookie_check = f_cookie_check;
75
+ conf->p_cookie = p_cookie;
76
+ }
77
+ #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
78
+
79
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
80
+ MBEDTLS_CHECK_RETURN_CRITICAL
81
+ static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
82
+ {
83
+ if (conf->f_psk != NULL) {
84
+ return 1;
85
+ }
86
+
87
+ if (conf->psk_identity_len == 0 || conf->psk_identity == NULL) {
88
+ return 0;
89
+ }
90
+
91
+
92
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
93
+ if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
94
+ return 1;
95
+ }
96
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
97
+
98
+ if (conf->psk != NULL && conf->psk_len != 0) {
99
+ return 1;
100
+ }
101
+
102
+ return 0;
103
+ }
104
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
105
+
106
+ MBEDTLS_CHECK_RETURN_CRITICAL
107
+ static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
108
+ const unsigned char *buf,
109
+ size_t len)
110
+ {
111
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
112
+ if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
113
+ /* Check verify-data in constant-time. The length OTOH is no secret */
114
+ if (len != 1 + ssl->verify_data_len ||
115
+ buf[0] != ssl->verify_data_len ||
116
+ mbedtls_ct_memcmp(buf + 1, ssl->peer_verify_data,
117
+ ssl->verify_data_len) != 0) {
118
+ MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
119
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
120
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
121
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
122
+ }
123
+ } else
124
+ #endif /* MBEDTLS_SSL_RENEGOTIATION */
125
+ {
126
+ if (len != 1 || buf[0] != 0x0) {
127
+ MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
128
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
129
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
130
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
131
+ }
132
+
133
+ ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
134
+ }
135
+
136
+ return 0;
137
+ }
138
+
139
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
140
+ defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
141
+ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
142
+ /*
143
+ * Function for parsing a supported groups (TLS 1.3) or supported elliptic
144
+ * curves (TLS 1.2) extension.
145
+ *
146
+ * The "extension_data" field of a supported groups extension contains a
147
+ * "NamedGroupList" value (TLS 1.3 RFC8446):
148
+ * enum {
149
+ * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
150
+ * x25519(0x001D), x448(0x001E),
151
+ * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
152
+ * ffdhe6144(0x0103), ffdhe8192(0x0104),
153
+ * ffdhe_private_use(0x01FC..0x01FF),
154
+ * ecdhe_private_use(0xFE00..0xFEFF),
155
+ * (0xFFFF)
156
+ * } NamedGroup;
157
+ * struct {
158
+ * NamedGroup named_group_list<2..2^16-1>;
159
+ * } NamedGroupList;
160
+ *
161
+ * The "extension_data" field of a supported elliptic curves extension contains
162
+ * a "NamedCurveList" value (TLS 1.2 RFC 8422):
163
+ * enum {
164
+ * deprecated(1..22),
165
+ * secp256r1 (23), secp384r1 (24), secp521r1 (25),
166
+ * x25519(29), x448(30),
167
+ * reserved (0xFE00..0xFEFF),
168
+ * deprecated(0xFF01..0xFF02),
169
+ * (0xFFFF)
170
+ * } NamedCurve;
171
+ * struct {
172
+ * NamedCurve named_curve_list<2..2^16-1>
173
+ * } NamedCurveList;
174
+ *
175
+ * The TLS 1.3 supported groups extension was defined to be a compatible
176
+ * generalization of the TLS 1.2 supported elliptic curves extension. They both
177
+ * share the same extension identifier.
178
+ *
179
+ */
180
+ MBEDTLS_CHECK_RETURN_CRITICAL
181
+ static int ssl_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
182
+ const unsigned char *buf,
183
+ size_t len)
184
+ {
185
+ size_t list_size, our_size;
186
+ const unsigned char *p;
187
+ uint16_t *curves_tls_id;
188
+
189
+ if (len < 2) {
190
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
191
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
192
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
193
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
194
+ }
195
+ list_size = MBEDTLS_GET_UINT16_BE(buf, 0);
196
+ if (list_size + 2 != len ||
197
+ list_size % 2 != 0) {
198
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
199
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
200
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
201
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
202
+ }
203
+
204
+ /* Should never happen unless client duplicates the extension */
205
+ if (ssl->handshake->curves_tls_id != NULL) {
206
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
207
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
208
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
209
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
210
+ }
211
+
212
+ /* Don't allow our peer to make us allocate too much memory,
213
+ * and leave room for a final 0 */
214
+ our_size = list_size / 2 + 1;
215
+ if (our_size > MBEDTLS_ECP_DP_MAX) {
216
+ our_size = MBEDTLS_ECP_DP_MAX;
217
+ }
218
+
219
+ if ((curves_tls_id = mbedtls_calloc(our_size,
220
+ sizeof(*curves_tls_id))) == NULL) {
221
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
222
+ MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
223
+ return MBEDTLS_ERR_SSL_ALLOC_FAILED;
224
+ }
225
+
226
+ ssl->handshake->curves_tls_id = curves_tls_id;
227
+
228
+ p = buf + 2;
229
+ while (list_size > 0 && our_size > 1) {
230
+ uint16_t curr_tls_id = MBEDTLS_GET_UINT16_BE(p, 0);
231
+
232
+ if (mbedtls_ssl_get_ecp_group_id_from_tls_id(curr_tls_id) !=
233
+ MBEDTLS_ECP_DP_NONE) {
234
+ *curves_tls_id++ = curr_tls_id;
235
+ our_size--;
236
+ }
237
+
238
+ list_size -= 2;
239
+ p += 2;
240
+ }
241
+
242
+ return 0;
243
+ }
244
+
245
+ MBEDTLS_CHECK_RETURN_CRITICAL
246
+ static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
247
+ const unsigned char *buf,
248
+ size_t len)
249
+ {
250
+ size_t list_size;
251
+ const unsigned char *p;
252
+
253
+ if (len == 0 || (size_t) (buf[0] + 1) != len) {
254
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
255
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
256
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
257
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
258
+ }
259
+ list_size = buf[0];
260
+
261
+ p = buf + 1;
262
+ while (list_size > 0) {
263
+ if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
264
+ p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
265
+ #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
266
+ defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
267
+ ssl->handshake->ecdh_ctx.point_format = p[0];
268
+ #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
269
+ #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
270
+ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
271
+ mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
272
+ p[0]);
273
+ #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
274
+ MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
275
+ return 0;
276
+ }
277
+
278
+ list_size--;
279
+ p++;
280
+ }
281
+
282
+ return 0;
283
+ }
284
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
285
+ MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
286
+ MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
287
+
288
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
289
+ MBEDTLS_CHECK_RETURN_CRITICAL
290
+ static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
291
+ const unsigned char *buf,
292
+ size_t len)
293
+ {
294
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
295
+
296
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
297
+ if (ssl->handshake->psa_pake_ctx_is_ok != 1)
298
+ #else
299
+ if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
300
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
301
+ {
302
+ MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
303
+ return 0;
304
+ }
305
+
306
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
307
+ if ((ret = mbedtls_psa_ecjpake_read_round(
308
+ &ssl->handshake->psa_pake_ctx, buf, len,
309
+ MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
310
+ psa_destroy_key(ssl->handshake->psa_pake_password);
311
+ psa_pake_abort(&ssl->handshake->psa_pake_ctx);
312
+
313
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
314
+ mbedtls_ssl_send_alert_message(
315
+ ssl,
316
+ MBEDTLS_SSL_ALERT_LEVEL_FATAL,
317
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
318
+
319
+ return ret;
320
+ }
321
+ #else
322
+ if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
323
+ buf, len)) != 0) {
324
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
325
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
326
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
327
+ return ret;
328
+ }
329
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
330
+
331
+ /* Only mark the extension as OK when we're sure it is */
332
+ ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
333
+
334
+ return 0;
335
+ }
336
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
337
+
338
+ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
339
+ MBEDTLS_CHECK_RETURN_CRITICAL
340
+ static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
341
+ const unsigned char *buf,
342
+ size_t len)
343
+ {
344
+ if (len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID) {
345
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
346
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
347
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
348
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
349
+ }
350
+
351
+ ssl->session_negotiate->mfl_code = buf[0];
352
+
353
+ return 0;
354
+ }
355
+ #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
356
+
357
+ #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
358
+ MBEDTLS_CHECK_RETURN_CRITICAL
359
+ static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
360
+ const unsigned char *buf,
361
+ size_t len)
362
+ {
363
+ size_t peer_cid_len;
364
+
365
+ /* CID extension only makes sense in DTLS */
366
+ if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
367
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
368
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
369
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
370
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
371
+ }
372
+
373
+ /*
374
+ * struct {
375
+ * opaque cid<0..2^8-1>;
376
+ * } ConnectionId;
377
+ */
378
+
379
+ if (len < 1) {
380
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
381
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
382
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
383
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
384
+ }
385
+
386
+ peer_cid_len = *buf++;
387
+ len--;
388
+
389
+ if (len != peer_cid_len) {
390
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
391
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
392
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
393
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
394
+ }
395
+
396
+ /* Ignore CID if the user has disabled its use. */
397
+ if (ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
398
+ /* Leave ssl->handshake->cid_in_use in its default
399
+ * value of MBEDTLS_SSL_CID_DISABLED. */
400
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
401
+ return 0;
402
+ }
403
+
404
+ if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
405
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
406
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
407
+ MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
408
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
409
+ }
410
+
411
+ ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
412
+ ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
413
+ memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
414
+
415
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
416
+ MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf, peer_cid_len);
417
+
418
+ return 0;
419
+ }
420
+ #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
421
+
422
+ #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
423
+ MBEDTLS_CHECK_RETURN_CRITICAL
424
+ static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
425
+ const unsigned char *buf,
426
+ size_t len)
427
+ {
428
+ if (len != 0) {
429
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
430
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
431
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
432
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
433
+ }
434
+
435
+ ((void) buf);
436
+
437
+ if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
438
+ ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
439
+ }
440
+
441
+ return 0;
442
+ }
443
+ #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
444
+
445
+ #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
446
+ MBEDTLS_CHECK_RETURN_CRITICAL
447
+ static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
448
+ const unsigned char *buf,
449
+ size_t len)
450
+ {
451
+ if (len != 0) {
452
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
453
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
454
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
455
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
456
+ }
457
+
458
+ ((void) buf);
459
+
460
+ if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
461
+ ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
462
+ }
463
+
464
+ return 0;
465
+ }
466
+ #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
467
+
468
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
469
+ MBEDTLS_CHECK_RETURN_CRITICAL
470
+ static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
471
+ unsigned char *buf,
472
+ size_t len)
473
+ {
474
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
475
+ mbedtls_ssl_session session;
476
+
477
+ mbedtls_ssl_session_init(&session);
478
+
479
+ if (ssl->conf->f_ticket_parse == NULL ||
480
+ ssl->conf->f_ticket_write == NULL) {
481
+ return 0;
482
+ }
483
+
484
+ /* Remember the client asked us to send a new ticket */
485
+ ssl->handshake->new_session_ticket = 1;
486
+
487
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, len));
488
+
489
+ if (len == 0) {
490
+ return 0;
491
+ }
492
+
493
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
494
+ if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
495
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
496
+ return 0;
497
+ }
498
+ #endif /* MBEDTLS_SSL_RENEGOTIATION */
499
+
500
+ /*
501
+ * Failures are ok: just ignore the ticket and proceed.
502
+ */
503
+ if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
504
+ buf, len)) != 0) {
505
+ mbedtls_ssl_session_free(&session);
506
+
507
+ if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
508
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
509
+ } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
510
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
511
+ } else {
512
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
513
+ }
514
+
515
+ return 0;
516
+ }
517
+
518
+ /*
519
+ * Keep the session ID sent by the client, since we MUST send it back to
520
+ * inform them we're accepting the ticket (RFC 5077 section 3.4)
521
+ */
522
+ session.id_len = ssl->session_negotiate->id_len;
523
+ memcpy(&session.id, ssl->session_negotiate->id, session.id_len);
524
+
525
+ mbedtls_ssl_session_free(ssl->session_negotiate);
526
+ memcpy(ssl->session_negotiate, &session, sizeof(mbedtls_ssl_session));
527
+
528
+ /* Zeroize instead of free as we copied the content */
529
+ mbedtls_platform_zeroize(&session, sizeof(mbedtls_ssl_session));
530
+
531
+ MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
532
+
533
+ ssl->handshake->resume = 1;
534
+
535
+ /* Don't send a new ticket after all, this one is OK */
536
+ ssl->handshake->new_session_ticket = 0;
537
+
538
+ return 0;
539
+ }
540
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
541
+
542
+ #if defined(MBEDTLS_SSL_DTLS_SRTP)
543
+ MBEDTLS_CHECK_RETURN_CRITICAL
544
+ static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
545
+ const unsigned char *buf,
546
+ size_t len)
547
+ {
548
+ mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
549
+ size_t i, j;
550
+ size_t profile_length;
551
+ uint16_t mki_length;
552
+ /*! 2 bytes for profile length and 1 byte for mki len */
553
+ const size_t size_of_lengths = 3;
554
+
555
+ /* If use_srtp is not configured, just ignore the extension */
556
+ if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
557
+ (ssl->conf->dtls_srtp_profile_list == NULL) ||
558
+ (ssl->conf->dtls_srtp_profile_list_len == 0)) {
559
+ return 0;
560
+ }
561
+
562
+ /* RFC5764 section 4.1.1
563
+ * uint8 SRTPProtectionProfile[2];
564
+ *
565
+ * struct {
566
+ * SRTPProtectionProfiles SRTPProtectionProfiles;
567
+ * opaque srtp_mki<0..255>;
568
+ * } UseSRTPData;
569
+
570
+ * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
571
+ */
572
+
573
+ /*
574
+ * Min length is 5: at least one protection profile(2 bytes)
575
+ * and length(2 bytes) + srtp_mki length(1 byte)
576
+ * Check here that we have at least 2 bytes of protection profiles length
577
+ * and one of srtp_mki length
578
+ */
579
+ if (len < size_of_lengths) {
580
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
581
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
582
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
583
+ }
584
+
585
+ ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
586
+
587
+ /* first 2 bytes are protection profile length(in bytes) */
588
+ profile_length = (buf[0] << 8) | buf[1];
589
+ buf += 2;
590
+
591
+ /* The profile length cannot be bigger than input buffer size - lengths fields */
592
+ if (profile_length > len - size_of_lengths ||
593
+ profile_length % 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
594
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
595
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
596
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
597
+ }
598
+ /*
599
+ * parse the extension list values are defined in
600
+ * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
601
+ */
602
+ for (j = 0; j < profile_length; j += 2) {
603
+ uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
604
+ client_protection = mbedtls_ssl_check_srtp_profile_value(protection_profile_value);
605
+
606
+ if (client_protection != MBEDTLS_TLS_SRTP_UNSET) {
607
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
608
+ mbedtls_ssl_get_srtp_profile_as_string(
609
+ client_protection)));
610
+ } else {
611
+ continue;
612
+ }
613
+ /* check if suggested profile is in our list */
614
+ for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
615
+ if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
616
+ ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
617
+ MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
618
+ mbedtls_ssl_get_srtp_profile_as_string(
619
+ client_protection)));
620
+ break;
621
+ }
622
+ }
623
+ if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET) {
624
+ break;
625
+ }
626
+ }
627
+ buf += profile_length; /* buf points to the mki length */
628
+ mki_length = *buf;
629
+ buf++;
630
+
631
+ if (mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
632
+ mki_length + profile_length + size_of_lengths != len) {
633
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
634
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
635
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
636
+ }
637
+
638
+ /* Parse the mki only if present and mki is supported locally */
639
+ if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
640
+ mki_length > 0) {
641
+ ssl->dtls_srtp_info.mki_len = mki_length;
642
+
643
+ memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
644
+
645
+ MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl->dtls_srtp_info.mki_value,
646
+ ssl->dtls_srtp_info.mki_len);
647
+ }
648
+
649
+ return 0;
650
+ }
651
+ #endif /* MBEDTLS_SSL_DTLS_SRTP */
652
+
653
+ /*
654
+ * Auxiliary functions for ServerHello parsing and related actions
655
+ */
656
+
657
+ #if defined(MBEDTLS_X509_CRT_PARSE_C)
658
+ /*
659
+ * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
660
+ */
661
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
662
+ MBEDTLS_CHECK_RETURN_CRITICAL
663
+ static int ssl_check_key_curve(mbedtls_pk_context *pk,
664
+ uint16_t *curves_tls_id)
665
+ {
666
+ uint16_t *curr_tls_id = curves_tls_id;
667
+ mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(pk);
668
+ mbedtls_ecp_group_id curr_grp_id;
669
+
670
+ while (*curr_tls_id != 0) {
671
+ curr_grp_id = mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
672
+ if (curr_grp_id == grp_id) {
673
+ return 0;
674
+ }
675
+ curr_tls_id++;
676
+ }
677
+
678
+ return -1;
679
+ }
680
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED */
681
+
682
+ /*
683
+ * Try picking a certificate for this ciphersuite,
684
+ * return 0 on success and -1 on failure.
685
+ */
686
+ MBEDTLS_CHECK_RETURN_CRITICAL
687
+ static int ssl_pick_cert(mbedtls_ssl_context *ssl,
688
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
689
+ {
690
+ mbedtls_ssl_key_cert *cur, *list;
691
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
692
+ psa_algorithm_t pk_alg =
693
+ mbedtls_ssl_get_ciphersuite_sig_pk_psa_alg(ciphersuite_info);
694
+ psa_key_usage_t pk_usage =
695
+ mbedtls_ssl_get_ciphersuite_sig_pk_psa_usage(ciphersuite_info);
696
+ #else
697
+ mbedtls_pk_type_t pk_alg =
698
+ mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
699
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
700
+ uint32_t flags;
701
+
702
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
703
+ if (ssl->handshake->sni_key_cert != NULL) {
704
+ list = ssl->handshake->sni_key_cert;
705
+ } else
706
+ #endif
707
+ list = ssl->conf->key_cert;
708
+
709
+ int pk_alg_is_none = 0;
710
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
711
+ pk_alg_is_none = (pk_alg == PSA_ALG_NONE);
712
+ #else
713
+ pk_alg_is_none = (pk_alg == MBEDTLS_PK_NONE);
714
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
715
+ if (pk_alg_is_none) {
716
+ return 0;
717
+ }
718
+
719
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
720
+
721
+ if (list == NULL) {
722
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
723
+ return -1;
724
+ }
725
+
726
+ for (cur = list; cur != NULL; cur = cur->next) {
727
+ flags = 0;
728
+ MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
729
+ cur->cert);
730
+
731
+ int key_type_matches = 0;
732
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
733
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
734
+ key_type_matches = ((ssl->conf->f_async_sign_start != NULL ||
735
+ ssl->conf->f_async_decrypt_start != NULL ||
736
+ mbedtls_pk_can_do_ext(cur->key, pk_alg, pk_usage)) &&
737
+ mbedtls_pk_can_do_ext(&cur->cert->pk, pk_alg, pk_usage));
738
+ #else
739
+ key_type_matches = (
740
+ mbedtls_pk_can_do_ext(cur->key, pk_alg, pk_usage));
741
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
742
+ #else
743
+ key_type_matches = mbedtls_pk_can_do(&cur->cert->pk, pk_alg);
744
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
745
+ if (!key_type_matches) {
746
+ MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
747
+ continue;
748
+ }
749
+
750
+ /*
751
+ * This avoids sending the client a cert it'll reject based on
752
+ * keyUsage or other extensions.
753
+ *
754
+ * It also allows the user to provision different certificates for
755
+ * different uses based on keyUsage, eg if they want to avoid signing
756
+ * and decrypting with the same RSA key.
757
+ */
758
+ if (mbedtls_ssl_check_cert_usage(cur->cert, ciphersuite_info,
759
+ MBEDTLS_SSL_IS_CLIENT,
760
+ MBEDTLS_SSL_VERSION_TLS1_2,
761
+ &flags) != 0) {
762
+ MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
763
+ "(extended) key usage extension"));
764
+ continue;
765
+ }
766
+
767
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
768
+ if (pk_alg == MBEDTLS_PK_ECDSA &&
769
+ ssl_check_key_curve(&cur->cert->pk,
770
+ ssl->handshake->curves_tls_id) != 0) {
771
+ MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
772
+ continue;
773
+ }
774
+ #endif
775
+
776
+ /* If we get there, we got a winner */
777
+ break;
778
+ }
779
+
780
+ /* Do not update ssl->handshake->key_cert unless there is a match */
781
+ if (cur != NULL) {
782
+ ssl->handshake->key_cert = cur;
783
+ MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
784
+ ssl->handshake->key_cert->cert);
785
+ return 0;
786
+ }
787
+
788
+ return -1;
789
+ }
790
+ #endif /* MBEDTLS_X509_CRT_PARSE_C */
791
+
792
+ /*
793
+ * Check if a given ciphersuite is suitable for use with our config/keys/etc
794
+ * Sets ciphersuite_info only if the suite matches.
795
+ */
796
+ MBEDTLS_CHECK_RETURN_CRITICAL
797
+ static int ssl_ciphersuite_match(mbedtls_ssl_context *ssl, int suite_id,
798
+ const mbedtls_ssl_ciphersuite_t **ciphersuite_info)
799
+ {
800
+ const mbedtls_ssl_ciphersuite_t *suite_info;
801
+
802
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
803
+ mbedtls_pk_type_t sig_type;
804
+ #endif
805
+
806
+ suite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
807
+ if (suite_info == NULL) {
808
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
809
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
810
+ }
811
+
812
+ MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
813
+ (unsigned int) suite_id, suite_info->name));
814
+
815
+ if (suite_info->min_tls_version > ssl->tls_version ||
816
+ suite_info->max_tls_version < ssl->tls_version) {
817
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
818
+ return 0;
819
+ }
820
+
821
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
822
+ if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
823
+ (ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK) == 0) {
824
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
825
+ "not configured or ext missing"));
826
+ return 0;
827
+ }
828
+ #endif
829
+
830
+
831
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
832
+ defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
833
+ if (mbedtls_ssl_ciphersuite_uses_ec(suite_info) &&
834
+ (ssl->handshake->curves_tls_id == NULL ||
835
+ ssl->handshake->curves_tls_id[0] == 0)) {
836
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
837
+ "no common elliptic curve"));
838
+ return 0;
839
+ }
840
+ #endif
841
+
842
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
843
+ /* If the ciphersuite requires a pre-shared key and we don't
844
+ * have one, skip it now rather than failing later */
845
+ if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
846
+ ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
847
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
848
+ return 0;
849
+ }
850
+ #endif
851
+
852
+ #if defined(MBEDTLS_X509_CRT_PARSE_C)
853
+ /*
854
+ * Final check: if ciphersuite requires us to have a
855
+ * certificate/key of a particular type:
856
+ * - select the appropriate certificate if we have one, or
857
+ * - try the next ciphersuite if we don't
858
+ * This must be done last since we modify the key_cert list.
859
+ */
860
+ if (ssl_pick_cert(ssl, suite_info) != 0) {
861
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
862
+ "no suitable certificate"));
863
+ return 0;
864
+ }
865
+ #endif
866
+
867
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
868
+ /* If the ciphersuite requires signing, check whether
869
+ * a suitable hash algorithm is present. */
870
+ sig_type = mbedtls_ssl_get_ciphersuite_sig_alg(suite_info);
871
+ if (sig_type != MBEDTLS_PK_NONE &&
872
+ mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
873
+ ssl, mbedtls_ssl_sig_from_pk_alg(sig_type)) == MBEDTLS_SSL_HASH_NONE) {
874
+ MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
875
+ "for signature algorithm %u", (unsigned) sig_type));
876
+ return 0;
877
+ }
878
+
879
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
880
+
881
+ *ciphersuite_info = suite_info;
882
+ return 0;
883
+ }
884
+
885
+ /* This function doesn't alert on errors that happen early during
886
+ ClientHello parsing because they might indicate that the client is
887
+ not talking SSL/TLS at all and would not understand our alert. */
888
+ MBEDTLS_CHECK_RETURN_CRITICAL
889
+ static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
890
+ {
891
+ int ret, got_common_suite;
892
+ size_t i, j;
893
+ size_t ciph_offset, comp_offset, ext_offset;
894
+ size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
895
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
896
+ size_t cookie_offset, cookie_len;
897
+ #endif
898
+ unsigned char *buf, *p, *ext;
899
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
900
+ int renegotiation_info_seen = 0;
901
+ #endif
902
+ int handshake_failure = 0;
903
+ const int *ciphersuites;
904
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
905
+
906
+ /* If there is no signature-algorithm extension present,
907
+ * we need to fall back to the default values for allowed
908
+ * signature-hash pairs. */
909
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
910
+ int sig_hash_alg_ext_present = 0;
911
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
912
+
913
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
914
+
915
+ /*
916
+ * Fetch the expected ClientHello handshake message. Do not ask
917
+ * mbedtls_ssl_read_record() to update the handshake digest, because the
918
+ * ClientHello may already have been read in ssl_tls13_process_client_hello()
919
+ * or as a post-handshake message (renegotiation). In those cases we need
920
+ * to update the digest ourselves, and it is simpler to do so
921
+ * unconditionally than to track whether it is needed.
922
+ */
923
+ if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
924
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record ", ret);
925
+
926
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
927
+ /*
928
+ * In the case of an alert message corresponding to the termination of
929
+ * a previous connection, `ssl_parse_record_header()` and then
930
+ * `mbedtls_ssl_read_record()` may return
931
+ * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD because of a non zero epoch.
932
+ *
933
+ * Historically, the library has returned
934
+ * MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE in this situation.
935
+ * The sample program dtls_server.c relies on this behavior
936
+ * (see
937
+ * https://github.com/Mbed-TLS/mbedtls/blob/d5e35a376bee23fad0b17f2e3e94a32ce4017c64/programs/ssl/dtls_server.c#L295),
938
+ * and user applications may rely on it as well.
939
+ *
940
+ * For compatibility, map MBEDTLS_ERR_SSL_UNEXPECTED_RECORD
941
+ * to MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE here.
942
+ *
943
+ * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD does not appear to be
944
+ * used to detect a specific error condition, so this mapping
945
+ * should not remove any meaningful distinction.
946
+ */
947
+ if ((ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM)
948
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
949
+ && (ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE)
950
+ #endif
951
+ ) {
952
+ if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
953
+ MBEDTLS_SSL_DEBUG_MSG(1, ("mapping UNEXPECTED_RECORD to UNEXPECTED_MESSAGE"));
954
+ ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
955
+ }
956
+ }
957
+ #endif /* MBEDTLS_SSL_PROTO_DTLS */
958
+
959
+ return ret;
960
+ }
961
+
962
+ /*
963
+ * Update the handshake checksum.
964
+ *
965
+ * Note that the checksum must be updated before parsing the extensions
966
+ * because ssl_parse_session_ticket_ext() may decrypt the ticket in place
967
+ * and therefore modify the ClientHello message. This occurs when using
968
+ * the Mbed TLS ssl_ticket.c implementation.
969
+ */
970
+ ret = mbedtls_ssl_update_handshake_status(ssl);
971
+ if (0 != ret) {
972
+ MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
973
+ return ret;
974
+ }
975
+
976
+ buf = ssl->in_msg;
977
+ msg_len = ssl->in_hslen;
978
+
979
+ /*
980
+ * Handshake layer:
981
+ * 0 . 0 handshake type
982
+ * 1 . 3 handshake length
983
+ * 4 . 5 DTLS only: message sequence number
984
+ * 6 . 8 DTLS only: fragment offset
985
+ * 9 . 11 DTLS only: fragment length
986
+ */
987
+ if ((ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) ||
988
+ (buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO)) {
989
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
990
+ return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
991
+ }
992
+
993
+ buf += mbedtls_ssl_hs_hdr_len(ssl);
994
+ msg_len -= mbedtls_ssl_hs_hdr_len(ssl);
995
+
996
+ /*
997
+ * ClientHello layout:
998
+ * 0 . 1 protocol version
999
+ * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1000
+ * 34 . 34 session id length (1 byte)
1001
+ * 35 . 34+x session id, where x = session id length from byte 34
1002
+ * 35+x . 35+x DTLS only: cookie length (1 byte)
1003
+ * 36+x . .. DTLS only: cookie
1004
+ * .. . .. ciphersuite list length (2 bytes)
1005
+ * .. . .. ciphersuite list
1006
+ * .. . .. compression alg. list length (1 byte)
1007
+ * .. . .. compression alg. list
1008
+ * .. . .. extensions length (2 bytes, optional)
1009
+ * .. . .. extensions (optional)
1010
+ */
1011
+
1012
+ /*
1013
+ * Minimal length (with everything empty and extensions omitted) is
1014
+ * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1015
+ * read at least up to session id length without worrying.
1016
+ */
1017
+ if (msg_len < 38) {
1018
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1019
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1020
+ }
1021
+
1022
+ /*
1023
+ * Check and save the protocol version
1024
+ */
1025
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
1026
+
1027
+ ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
1028
+ ssl->conf->transport);
1029
+ ssl->session_negotiate->tls_version = ssl->tls_version;
1030
+ ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1031
+
1032
+ if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
1033
+ MBEDTLS_SSL_DEBUG_MSG(1, ("server only supports TLS 1.2"));
1034
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1035
+ MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
1036
+ return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1037
+ }
1038
+
1039
+ /*
1040
+ * Save client random (inc. Unix time)
1041
+ */
1042
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf + 2, 32);
1043
+
1044
+ memcpy(ssl->handshake->randbytes, buf + 2, 32);
1045
+
1046
+ /*
1047
+ * Check the session ID length and save session ID
1048
+ */
1049
+ sess_len = buf[34];
1050
+
1051
+ if (sess_len > sizeof(ssl->session_negotiate->id) ||
1052
+ sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
1053
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1054
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1055
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1056
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1057
+ }
1058
+
1059
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 35, sess_len);
1060
+
1061
+ ssl->session_negotiate->id_len = sess_len;
1062
+ memset(ssl->session_negotiate->id, 0,
1063
+ sizeof(ssl->session_negotiate->id));
1064
+ memcpy(ssl->session_negotiate->id, buf + 35,
1065
+ ssl->session_negotiate->id_len);
1066
+
1067
+ /*
1068
+ * Check the cookie length and content
1069
+ */
1070
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
1071
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1072
+ cookie_offset = 35 + sess_len;
1073
+ cookie_len = buf[cookie_offset];
1074
+
1075
+ if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
1076
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1077
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1078
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1079
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1080
+ }
1081
+
1082
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
1083
+ buf + cookie_offset + 1, cookie_len);
1084
+
1085
+ #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1086
+ if (ssl->conf->f_cookie_check != NULL
1087
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1088
+ && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
1089
+ #endif
1090
+ ) {
1091
+ if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
1092
+ buf + cookie_offset + 1, cookie_len,
1093
+ ssl->cli_id, ssl->cli_id_len) != 0) {
1094
+ MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
1095
+ ssl->handshake->cookie_verify_result = 1;
1096
+ } else {
1097
+ MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
1098
+ ssl->handshake->cookie_verify_result = 0;
1099
+ }
1100
+ } else
1101
+ #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1102
+ {
1103
+ /* We know we didn't send a cookie, so it should be empty */
1104
+ if (cookie_len != 0) {
1105
+ /* This may be an attacker's probe, so don't send an alert */
1106
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1107
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1108
+ }
1109
+
1110
+ MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
1111
+ }
1112
+
1113
+ /*
1114
+ * Check the ciphersuitelist length (will be parsed later)
1115
+ */
1116
+ ciph_offset = cookie_offset + 1 + cookie_len;
1117
+ } else
1118
+ #endif /* MBEDTLS_SSL_PROTO_DTLS */
1119
+ ciph_offset = 35 + sess_len;
1120
+
1121
+ ciph_len = MBEDTLS_GET_UINT16_BE(buf, ciph_offset);
1122
+
1123
+ if (ciph_len < 2 ||
1124
+ ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1125
+ (ciph_len % 2) != 0) {
1126
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1127
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1128
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1129
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1130
+ }
1131
+
1132
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1133
+ buf + ciph_offset + 2, ciph_len);
1134
+
1135
+ /*
1136
+ * Check the compression algorithm's length.
1137
+ * The list contents are ignored because implementing
1138
+ * MBEDTLS_SSL_COMPRESS_NULL is mandatory and is the only
1139
+ * option supported by Mbed TLS.
1140
+ */
1141
+ comp_offset = ciph_offset + 2 + ciph_len;
1142
+
1143
+ comp_len = buf[comp_offset];
1144
+
1145
+ if (comp_len < 1 ||
1146
+ comp_len > 16 ||
1147
+ comp_len + comp_offset + 1 > msg_len) {
1148
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1149
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1150
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1151
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1152
+ }
1153
+
1154
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
1155
+ buf + comp_offset + 1, comp_len);
1156
+
1157
+ /*
1158
+ * Check the extension length
1159
+ */
1160
+ ext_offset = comp_offset + 1 + comp_len;
1161
+ if (msg_len > ext_offset) {
1162
+ if (msg_len < ext_offset + 2) {
1163
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1164
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1165
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1166
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1167
+ }
1168
+
1169
+ ext_len = MBEDTLS_GET_UINT16_BE(buf, ext_offset);
1170
+
1171
+ if (msg_len != ext_offset + 2 + ext_len) {
1172
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1173
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1174
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1175
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1176
+ }
1177
+ } else {
1178
+ ext_len = 0;
1179
+ }
1180
+
1181
+ ext = buf + ext_offset + 2;
1182
+ MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
1183
+
1184
+ while (ext_len != 0) {
1185
+ unsigned int ext_id;
1186
+ unsigned int ext_size;
1187
+ if (ext_len < 4) {
1188
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1189
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1190
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1191
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1192
+ }
1193
+ ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
1194
+ ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
1195
+
1196
+ if (ext_size + 4 > ext_len) {
1197
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1198
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1199
+ MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
1200
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
1201
+ }
1202
+ switch (ext_id) {
1203
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1204
+ case MBEDTLS_TLS_EXT_SERVERNAME:
1205
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1206
+ ret = mbedtls_ssl_parse_server_name_ext(ssl, ext + 4,
1207
+ ext + 4 + ext_size);
1208
+ if (ret != 0) {
1209
+ return ret;
1210
+ }
1211
+ break;
1212
+ #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1213
+
1214
+ case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
1215
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
1216
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1217
+ renegotiation_info_seen = 1;
1218
+ #endif
1219
+
1220
+ ret = ssl_parse_renegotiation_info(ssl, ext + 4, ext_size);
1221
+ if (ret != 0) {
1222
+ return ret;
1223
+ }
1224
+ break;
1225
+
1226
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1227
+ case MBEDTLS_TLS_EXT_SIG_ALG:
1228
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
1229
+
1230
+ ret = mbedtls_ssl_parse_sig_alg_ext(ssl, ext + 4, ext + 4 + ext_size);
1231
+ if (ret != 0) {
1232
+ return ret;
1233
+ }
1234
+
1235
+ sig_hash_alg_ext_present = 1;
1236
+ break;
1237
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1238
+
1239
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
1240
+ defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
1241
+ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1242
+ case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
1243
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
1244
+
1245
+ ret = ssl_parse_supported_groups_ext(ssl, ext + 4, ext_size);
1246
+ if (ret != 0) {
1247
+ return ret;
1248
+ }
1249
+ break;
1250
+
1251
+ case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
1252
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
1253
+ ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
1254
+
1255
+ ret = ssl_parse_supported_point_formats(ssl, ext + 4, ext_size);
1256
+ if (ret != 0) {
1257
+ return ret;
1258
+ }
1259
+ break;
1260
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED || \
1261
+ MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
1262
+ MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1263
+
1264
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1265
+ case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
1266
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
1267
+
1268
+ ret = ssl_parse_ecjpake_kkpp(ssl, ext + 4, ext_size);
1269
+ if (ret != 0) {
1270
+ return ret;
1271
+ }
1272
+ break;
1273
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1274
+
1275
+ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1276
+ case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
1277
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
1278
+
1279
+ ret = ssl_parse_max_fragment_length_ext(ssl, ext + 4, ext_size);
1280
+ if (ret != 0) {
1281
+ return ret;
1282
+ }
1283
+ break;
1284
+ #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1285
+
1286
+ #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1287
+ case MBEDTLS_TLS_EXT_CID:
1288
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
1289
+
1290
+ ret = ssl_parse_cid_ext(ssl, ext + 4, ext_size);
1291
+ if (ret != 0) {
1292
+ return ret;
1293
+ }
1294
+ break;
1295
+ #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1296
+
1297
+ #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1298
+ case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
1299
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
1300
+
1301
+ ret = ssl_parse_encrypt_then_mac_ext(ssl, ext + 4, ext_size);
1302
+ if (ret != 0) {
1303
+ return ret;
1304
+ }
1305
+ break;
1306
+ #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
1307
+
1308
+ #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1309
+ case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
1310
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
1311
+
1312
+ ret = ssl_parse_extended_ms_ext(ssl, ext + 4, ext_size);
1313
+ if (ret != 0) {
1314
+ return ret;
1315
+ }
1316
+ break;
1317
+ #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1318
+
1319
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1320
+ case MBEDTLS_TLS_EXT_SESSION_TICKET:
1321
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
1322
+ /*
1323
+ * If the Mbed TLS ssl_ticket.c implementation is used, the
1324
+ * ticket is decrypted in place. This modifies the ClientHello
1325
+ * message in the input buffer.
1326
+ */
1327
+ ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
1328
+ if (ret != 0) {
1329
+ return ret;
1330
+ }
1331
+ break;
1332
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1333
+
1334
+ #if defined(MBEDTLS_SSL_ALPN)
1335
+ case MBEDTLS_TLS_EXT_ALPN:
1336
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
1337
+
1338
+ ret = mbedtls_ssl_parse_alpn_ext(ssl, ext + 4,
1339
+ ext + 4 + ext_size);
1340
+ if (ret != 0) {
1341
+ return ret;
1342
+ }
1343
+ break;
1344
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1345
+
1346
+ #if defined(MBEDTLS_SSL_DTLS_SRTP)
1347
+ case MBEDTLS_TLS_EXT_USE_SRTP:
1348
+ MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
1349
+
1350
+ ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size);
1351
+ if (ret != 0) {
1352
+ return ret;
1353
+ }
1354
+ break;
1355
+ #endif /* MBEDTLS_SSL_DTLS_SRTP */
1356
+
1357
+ default:
1358
+ MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
1359
+ ext_id));
1360
+ }
1361
+
1362
+ ext_len -= 4 + ext_size;
1363
+ ext += 4 + ext_size;
1364
+ }
1365
+
1366
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1367
+
1368
+ /*
1369
+ * Try to fall back to default hash SHA1 if the client
1370
+ * hasn't provided any preferred signature-hash combinations.
1371
+ */
1372
+ if (!sig_hash_alg_ext_present) {
1373
+ uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
1374
+ const uint16_t default_sig_algs[] = {
1375
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
1376
+ MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA,
1377
+ MBEDTLS_SSL_HASH_SHA1),
1378
+ #endif
1379
+ #if defined(MBEDTLS_RSA_C)
1380
+ MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA,
1381
+ MBEDTLS_SSL_HASH_SHA1),
1382
+ #endif
1383
+ MBEDTLS_TLS_SIG_NONE
1384
+ };
1385
+
1386
+ MBEDTLS_STATIC_ASSERT(sizeof(default_sig_algs) / sizeof(default_sig_algs[0])
1387
+ <= MBEDTLS_RECEIVED_SIG_ALGS_SIZE,
1388
+ "default_sig_algs is too big");
1389
+
1390
+ memcpy(received_sig_algs, default_sig_algs, sizeof(default_sig_algs));
1391
+ }
1392
+
1393
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1394
+
1395
+ /*
1396
+ * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1397
+ */
1398
+ for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
1399
+ if (p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
1400
+ MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
1401
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1402
+ if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
1403
+ MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
1404
+ "during renegotiation"));
1405
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1406
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1407
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1408
+ }
1409
+ #endif
1410
+ ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
1411
+ break;
1412
+ }
1413
+ }
1414
+
1415
+ /*
1416
+ * Renegotiation security checks
1417
+ */
1418
+ if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
1419
+ ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
1420
+ MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
1421
+ handshake_failure = 1;
1422
+ }
1423
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1424
+ else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1425
+ ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
1426
+ renegotiation_info_seen == 0) {
1427
+ MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
1428
+ handshake_failure = 1;
1429
+ } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1430
+ ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1431
+ ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
1432
+ MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
1433
+ handshake_failure = 1;
1434
+ } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
1435
+ ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
1436
+ renegotiation_info_seen == 1) {
1437
+ MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
1438
+ handshake_failure = 1;
1439
+ }
1440
+ #endif /* MBEDTLS_SSL_RENEGOTIATION */
1441
+
1442
+ if (handshake_failure == 1) {
1443
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1444
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1445
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1446
+ }
1447
+
1448
+ /*
1449
+ * Server certification selection (after processing TLS extensions)
1450
+ */
1451
+ if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1452
+ MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1453
+ return ret;
1454
+ }
1455
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1456
+ ssl->handshake->sni_name = NULL;
1457
+ ssl->handshake->sni_name_len = 0;
1458
+ #endif
1459
+
1460
+ /*
1461
+ * Search for a matching ciphersuite
1462
+ * (At the end because we need information from the EC-based extensions
1463
+ * and certificate from the SNI callback triggered by the SNI extension
1464
+ * or certificate from server certificate selection callback.)
1465
+ */
1466
+ got_common_suite = 0;
1467
+ ciphersuites = ssl->conf->ciphersuite_list;
1468
+ ciphersuite_info = NULL;
1469
+
1470
+ if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT) {
1471
+ for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
1472
+ for (i = 0; ciphersuites[i] != 0; i++) {
1473
+ if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
1474
+ continue;
1475
+ }
1476
+
1477
+ got_common_suite = 1;
1478
+
1479
+ if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1480
+ &ciphersuite_info)) != 0) {
1481
+ return ret;
1482
+ }
1483
+
1484
+ if (ciphersuite_info != NULL) {
1485
+ goto have_ciphersuite;
1486
+ }
1487
+ }
1488
+ }
1489
+ } else {
1490
+ for (i = 0; ciphersuites[i] != 0; i++) {
1491
+ for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
1492
+ if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
1493
+ continue;
1494
+ }
1495
+
1496
+ got_common_suite = 1;
1497
+
1498
+ if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
1499
+ &ciphersuite_info)) != 0) {
1500
+ return ret;
1501
+ }
1502
+
1503
+ if (ciphersuite_info != NULL) {
1504
+ goto have_ciphersuite;
1505
+ }
1506
+ }
1507
+ }
1508
+ }
1509
+
1510
+ if (got_common_suite) {
1511
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
1512
+ "but none of them usable"));
1513
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1514
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1515
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1516
+ } else {
1517
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
1518
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1519
+ MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
1520
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1521
+ }
1522
+
1523
+ have_ciphersuite:
1524
+ MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
1525
+
1526
+ ssl->session_negotiate->ciphersuite = ciphersuites[i];
1527
+ ssl->handshake->ciphersuite_info = ciphersuite_info;
1528
+
1529
+ mbedtls_ssl_handshake_increment_state(ssl);
1530
+
1531
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
1532
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1533
+ mbedtls_ssl_recv_flight_completed(ssl);
1534
+ }
1535
+ #endif
1536
+
1537
+ /* Debugging-only output for testsuite */
1538
+ #if defined(MBEDTLS_DEBUG_C) && \
1539
+ defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
1540
+ mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info);
1541
+ if (sig_alg != MBEDTLS_PK_NONE) {
1542
+ unsigned int sig_hash = mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
1543
+ ssl, mbedtls_ssl_sig_from_pk_alg(sig_alg));
1544
+ MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %u",
1545
+ sig_hash));
1546
+ } else {
1547
+ MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
1548
+ "%u - should not happen", (unsigned) sig_alg));
1549
+ }
1550
+ #endif
1551
+
1552
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1553
+
1554
+ return 0;
1555
+ }
1556
+
1557
+ #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1558
+ static void ssl_write_cid_ext(mbedtls_ssl_context *ssl,
1559
+ unsigned char *buf,
1560
+ size_t *olen)
1561
+ {
1562
+ unsigned char *p = buf;
1563
+ size_t ext_len;
1564
+ const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1565
+
1566
+ *olen = 0;
1567
+
1568
+ /* Skip writing the extension if we don't want to use it or if
1569
+ * the client hasn't offered it. */
1570
+ if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED) {
1571
+ return;
1572
+ }
1573
+
1574
+ /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
1575
+ * which is at most 255, so the increment cannot overflow. */
1576
+ if (end < p || (size_t) (end - p) < (unsigned) (ssl->own_cid_len + 5)) {
1577
+ MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
1578
+ return;
1579
+ }
1580
+
1581
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
1582
+
1583
+ /*
1584
+ * struct {
1585
+ * opaque cid<0..2^8-1>;
1586
+ * } ConnectionId;
1587
+ */
1588
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
1589
+ p += 2;
1590
+ ext_len = (size_t) ssl->own_cid_len + 1;
1591
+ MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
1592
+ p += 2;
1593
+
1594
+ *p++ = (uint8_t) ssl->own_cid_len;
1595
+ memcpy(p, ssl->own_cid, ssl->own_cid_len);
1596
+
1597
+ *olen = ssl->own_cid_len + 5;
1598
+ }
1599
+ #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1600
+
1601
+ #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
1602
+ static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
1603
+ unsigned char *buf,
1604
+ size_t *olen)
1605
+ {
1606
+ unsigned char *p = buf;
1607
+ const mbedtls_ssl_ciphersuite_t *suite = NULL;
1608
+
1609
+ /*
1610
+ * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1611
+ * from a client and then selects a stream or Authenticated Encryption
1612
+ * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1613
+ * encrypt-then-MAC response extension back to the client."
1614
+ */
1615
+ suite = mbedtls_ssl_ciphersuite_from_id(
1616
+ ssl->session_negotiate->ciphersuite);
1617
+ if (suite == NULL) {
1618
+ ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
1619
+ } else {
1620
+ mbedtls_ssl_mode_t ssl_mode =
1621
+ mbedtls_ssl_get_mode_from_ciphersuite(
1622
+ ssl->session_negotiate->encrypt_then_mac,
1623
+ suite);
1624
+
1625
+ if (ssl_mode != MBEDTLS_SSL_MODE_CBC_ETM) {
1626
+ ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
1627
+ }
1628
+ }
1629
+
1630
+ if (ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
1631
+ *olen = 0;
1632
+ return;
1633
+ }
1634
+
1635
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
1636
+
1637
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
1638
+ p += 2;
1639
+
1640
+ *p++ = 0x00;
1641
+ *p++ = 0x00;
1642
+
1643
+ *olen = 4;
1644
+ }
1645
+ #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
1646
+
1647
+ #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1648
+ static void ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
1649
+ unsigned char *buf,
1650
+ size_t *olen)
1651
+ {
1652
+ unsigned char *p = buf;
1653
+
1654
+ if (ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
1655
+ *olen = 0;
1656
+ return;
1657
+ }
1658
+
1659
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
1660
+ "extension"));
1661
+
1662
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
1663
+ p += 2;
1664
+
1665
+ *p++ = 0x00;
1666
+ *p++ = 0x00;
1667
+
1668
+ *olen = 4;
1669
+ }
1670
+ #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1671
+
1672
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
1673
+ static void ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
1674
+ unsigned char *buf,
1675
+ size_t *olen)
1676
+ {
1677
+ unsigned char *p = buf;
1678
+
1679
+ if (ssl->handshake->new_session_ticket == 0) {
1680
+ *olen = 0;
1681
+ return;
1682
+ }
1683
+
1684
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
1685
+
1686
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
1687
+ p += 2;
1688
+
1689
+ *p++ = 0x00;
1690
+ *p++ = 0x00;
1691
+
1692
+ *olen = 4;
1693
+ }
1694
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
1695
+
1696
+ static void ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
1697
+ unsigned char *buf,
1698
+ size_t *olen)
1699
+ {
1700
+ unsigned char *p = buf;
1701
+
1702
+ if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION) {
1703
+ *olen = 0;
1704
+ return;
1705
+ }
1706
+
1707
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
1708
+
1709
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
1710
+ p += 2;
1711
+
1712
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1713
+ if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
1714
+ *p++ = 0x00;
1715
+ *p++ = (ssl->verify_data_len * 2 + 1) & 0xFF;
1716
+ *p++ = ssl->verify_data_len * 2 & 0xFF;
1717
+
1718
+ memcpy(p, ssl->peer_verify_data, ssl->verify_data_len);
1719
+ p += ssl->verify_data_len;
1720
+ memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
1721
+ p += ssl->verify_data_len;
1722
+ } else
1723
+ #endif /* MBEDTLS_SSL_RENEGOTIATION */
1724
+ {
1725
+ *p++ = 0x00;
1726
+ *p++ = 0x01;
1727
+ *p++ = 0x00;
1728
+ }
1729
+
1730
+ *olen = (size_t) (p - buf);
1731
+ }
1732
+
1733
+ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1734
+ static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
1735
+ unsigned char *buf,
1736
+ size_t *olen)
1737
+ {
1738
+ unsigned char *p = buf;
1739
+
1740
+ if (ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
1741
+ *olen = 0;
1742
+ return;
1743
+ }
1744
+
1745
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
1746
+
1747
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
1748
+ p += 2;
1749
+
1750
+ *p++ = 0x00;
1751
+ *p++ = 1;
1752
+
1753
+ *p++ = ssl->session_negotiate->mfl_code;
1754
+
1755
+ *olen = 5;
1756
+ }
1757
+ #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1758
+
1759
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
1760
+ defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
1761
+ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1762
+ static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
1763
+ unsigned char *buf,
1764
+ size_t *olen)
1765
+ {
1766
+ unsigned char *p = buf;
1767
+ ((void) ssl);
1768
+
1769
+ if ((ssl->handshake->cli_exts &
1770
+ MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT) == 0) {
1771
+ *olen = 0;
1772
+ return;
1773
+ }
1774
+
1775
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
1776
+
1777
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
1778
+ p += 2;
1779
+
1780
+ *p++ = 0x00;
1781
+ *p++ = 2;
1782
+
1783
+ *p++ = 1;
1784
+ *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
1785
+
1786
+ *olen = 6;
1787
+ }
1788
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
1789
+ MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
1790
+ MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1791
+
1792
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
1793
+ static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
1794
+ unsigned char *buf,
1795
+ size_t *olen)
1796
+ {
1797
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1798
+ unsigned char *p = buf;
1799
+ const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1800
+ size_t kkpp_len;
1801
+
1802
+ *olen = 0;
1803
+
1804
+ /* Skip costly computation if not needed */
1805
+ if (ssl->handshake->ciphersuite_info->key_exchange !=
1806
+ MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
1807
+ return 0;
1808
+ }
1809
+
1810
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
1811
+
1812
+ if (end - p < 4) {
1813
+ MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
1814
+ return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1815
+ }
1816
+
1817
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
1818
+ p += 2;
1819
+
1820
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
1821
+ ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
1822
+ p + 2, (size_t) (end - p - 2), &kkpp_len,
1823
+ MBEDTLS_ECJPAKE_ROUND_ONE);
1824
+ if (ret != 0) {
1825
+ psa_destroy_key(ssl->handshake->psa_pake_password);
1826
+ psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1827
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
1828
+ return ret;
1829
+ }
1830
+ #else
1831
+ ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
1832
+ p + 2, (size_t) (end - p - 2), &kkpp_len,
1833
+ ssl->conf->f_rng, ssl->conf->p_rng);
1834
+ if (ret != 0) {
1835
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
1836
+ return ret;
1837
+ }
1838
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1839
+
1840
+ MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
1841
+ p += 2;
1842
+
1843
+ *olen = kkpp_len + 4;
1844
+ return 0;
1845
+ }
1846
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
1847
+
1848
+ #if defined(MBEDTLS_SSL_DTLS_SRTP) && defined(MBEDTLS_SSL_PROTO_DTLS)
1849
+ static void ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
1850
+ unsigned char *buf,
1851
+ size_t *olen)
1852
+ {
1853
+ size_t mki_len = 0, ext_len = 0;
1854
+ uint16_t profile_value = 0;
1855
+ const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
1856
+
1857
+ *olen = 0;
1858
+
1859
+ if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
1860
+ (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET)) {
1861
+ return;
1862
+ }
1863
+
1864
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
1865
+
1866
+ if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
1867
+ mki_len = ssl->dtls_srtp_info.mki_len;
1868
+ }
1869
+
1870
+ /* The extension total size is 9 bytes :
1871
+ * - 2 bytes for the extension tag
1872
+ * - 2 bytes for the total size
1873
+ * - 2 bytes for the protection profile length
1874
+ * - 2 bytes for the protection profile
1875
+ * - 1 byte for the mki length
1876
+ * + the actual mki length
1877
+ * Check we have enough room in the output buffer */
1878
+ if ((size_t) (end - buf) < mki_len + 9) {
1879
+ MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
1880
+ return;
1881
+ }
1882
+
1883
+ /* extension */
1884
+ MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0);
1885
+ /*
1886
+ * total length 5 and mki value: only one profile(2 bytes)
1887
+ * and length(2 bytes) and srtp_mki )
1888
+ */
1889
+ ext_len = 5 + mki_len;
1890
+ MBEDTLS_PUT_UINT16_BE(ext_len, buf, 2);
1891
+
1892
+ /* protection profile length: 2 */
1893
+ buf[4] = 0x00;
1894
+ buf[5] = 0x02;
1895
+ profile_value = mbedtls_ssl_check_srtp_profile_value(
1896
+ ssl->dtls_srtp_info.chosen_dtls_srtp_profile);
1897
+ if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
1898
+ MBEDTLS_PUT_UINT16_BE(profile_value, buf, 6);
1899
+ } else {
1900
+ MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
1901
+ return;
1902
+ }
1903
+
1904
+ buf[8] = mki_len & 0xFF;
1905
+ memcpy(&buf[9], ssl->dtls_srtp_info.mki_value, mki_len);
1906
+
1907
+ *olen = 9 + mki_len;
1908
+ }
1909
+ #endif /* MBEDTLS_SSL_DTLS_SRTP */
1910
+
1911
+ #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
1912
+ MBEDTLS_CHECK_RETURN_CRITICAL
1913
+ static int ssl_write_hello_verify_request(mbedtls_ssl_context *ssl)
1914
+ {
1915
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1916
+ unsigned char *p = ssl->out_msg + 4;
1917
+ unsigned char *cookie_len_byte;
1918
+
1919
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
1920
+
1921
+ /*
1922
+ * struct {
1923
+ * ProtocolVersion server_version;
1924
+ * opaque cookie<0..2^8-1>;
1925
+ * } HelloVerifyRequest;
1926
+ */
1927
+
1928
+ /* The RFC is not clear on this point, but sending the actual negotiated
1929
+ * version looks like the most interoperable thing to do. */
1930
+ mbedtls_ssl_write_version(p, ssl->conf->transport, ssl->tls_version);
1931
+ MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
1932
+ p += 2;
1933
+
1934
+ /* If we get here, f_cookie_check is not null */
1935
+ if (ssl->conf->f_cookie_write == NULL) {
1936
+ MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
1937
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1938
+ }
1939
+
1940
+ /* Skip length byte until we know the length */
1941
+ cookie_len_byte = p++;
1942
+
1943
+ if ((ret = ssl->conf->f_cookie_write(ssl->conf->p_cookie,
1944
+ &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
1945
+ ssl->cli_id, ssl->cli_id_len)) != 0) {
1946
+ MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret);
1947
+ return ret;
1948
+ }
1949
+
1950
+ *cookie_len_byte = (unsigned char) (p - (cookie_len_byte + 1));
1951
+
1952
+ MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte);
1953
+
1954
+ ssl->out_msglen = (size_t) (p - ssl->out_msg);
1955
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
1956
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
1957
+
1958
+ mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT);
1959
+
1960
+ if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
1961
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
1962
+ return ret;
1963
+ }
1964
+
1965
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
1966
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
1967
+ (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
1968
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
1969
+ return ret;
1970
+ }
1971
+ #endif /* MBEDTLS_SSL_PROTO_DTLS */
1972
+
1973
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
1974
+
1975
+ return 0;
1976
+ }
1977
+ #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
1978
+
1979
+ static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
1980
+ {
1981
+ int ret;
1982
+ mbedtls_ssl_session session_tmp;
1983
+ mbedtls_ssl_session * const session = ssl->session_negotiate;
1984
+
1985
+ /* Resume is 0 by default, see ssl_handshake_init().
1986
+ * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
1987
+ if (ssl->handshake->resume == 1) {
1988
+ return;
1989
+ }
1990
+ if (session->id_len == 0) {
1991
+ return;
1992
+ }
1993
+ if (ssl->conf->f_get_cache == NULL) {
1994
+ return;
1995
+ }
1996
+ #if defined(MBEDTLS_SSL_RENEGOTIATION)
1997
+ if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
1998
+ return;
1999
+ }
2000
+ #endif
2001
+
2002
+ mbedtls_ssl_session_init(&session_tmp);
2003
+
2004
+ ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
2005
+ session->id,
2006
+ session->id_len,
2007
+ &session_tmp);
2008
+ if (ret != 0) {
2009
+ goto exit;
2010
+ }
2011
+
2012
+ if (session->ciphersuite != session_tmp.ciphersuite) {
2013
+ /* Mismatch between cached and negotiated session */
2014
+ goto exit;
2015
+ }
2016
+
2017
+ /* Move semantics */
2018
+ mbedtls_ssl_session_free(session);
2019
+ *session = session_tmp;
2020
+ memset(&session_tmp, 0, sizeof(session_tmp));
2021
+
2022
+ MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
2023
+ ssl->handshake->resume = 1;
2024
+
2025
+ exit:
2026
+
2027
+ mbedtls_ssl_session_free(&session_tmp);
2028
+ }
2029
+
2030
+ MBEDTLS_CHECK_RETURN_CRITICAL
2031
+ static int ssl_write_server_hello(mbedtls_ssl_context *ssl)
2032
+ {
2033
+ #if defined(MBEDTLS_HAVE_TIME)
2034
+ mbedtls_time_t t;
2035
+ #endif
2036
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2037
+ size_t olen, ext_len = 0, n;
2038
+ unsigned char *buf, *p;
2039
+
2040
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
2041
+
2042
+ #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
2043
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2044
+ ssl->handshake->cookie_verify_result != 0) {
2045
+ MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
2046
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2047
+
2048
+ return ssl_write_hello_verify_request(ssl);
2049
+ }
2050
+ #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
2051
+
2052
+ /*
2053
+ * 0 . 0 handshake type
2054
+ * 1 . 3 handshake length
2055
+ * 4 . 5 protocol version
2056
+ * 6 . 9 UNIX time()
2057
+ * 10 . 37 random bytes
2058
+ */
2059
+ buf = ssl->out_msg;
2060
+ p = buf + 4;
2061
+
2062
+ mbedtls_ssl_write_version(p, ssl->conf->transport, ssl->tls_version);
2063
+ p += 2;
2064
+
2065
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
2066
+ buf[4], buf[5]));
2067
+
2068
+ #if defined(MBEDTLS_HAVE_TIME)
2069
+ t = mbedtls_time(NULL);
2070
+ MBEDTLS_PUT_UINT32_BE(t, p, 0);
2071
+ p += 4;
2072
+
2073
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
2074
+ (long long) t));
2075
+ #else
2076
+ if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
2077
+ return ret;
2078
+ }
2079
+
2080
+ p += 4;
2081
+ #endif /* MBEDTLS_HAVE_TIME */
2082
+
2083
+ if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 20)) != 0) {
2084
+ return ret;
2085
+ }
2086
+ p += 20;
2087
+
2088
+ #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
2089
+ /*
2090
+ * RFC 8446
2091
+ * TLS 1.3 has a downgrade protection mechanism embedded in the server's
2092
+ * random value. TLS 1.3 servers which negotiate TLS 1.2 or below in
2093
+ * response to a ClientHello MUST set the last 8 bytes of their Random
2094
+ * value specially in their ServerHello.
2095
+ */
2096
+ if (mbedtls_ssl_conf_is_tls13_enabled(ssl->conf)) {
2097
+ static const unsigned char magic_tls12_downgrade_string[] =
2098
+ { 'D', 'O', 'W', 'N', 'G', 'R', 'D', 1 };
2099
+
2100
+ MBEDTLS_STATIC_ASSERT(
2101
+ sizeof(magic_tls12_downgrade_string) == 8,
2102
+ "magic_tls12_downgrade_string does not have the expected size");
2103
+
2104
+ memcpy(p, magic_tls12_downgrade_string,
2105
+ sizeof(magic_tls12_downgrade_string));
2106
+ } else
2107
+ #endif
2108
+ {
2109
+ if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 8)) != 0) {
2110
+ return ret;
2111
+ }
2112
+ }
2113
+ p += 8;
2114
+
2115
+ memcpy(ssl->handshake->randbytes + 32, buf + 6, 32);
2116
+
2117
+ MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 6, 32);
2118
+
2119
+ ssl_handle_id_based_session_resumption(ssl);
2120
+
2121
+ if (ssl->handshake->resume == 0) {
2122
+ /*
2123
+ * New session, create a new session id,
2124
+ * unless we're about to issue a session ticket
2125
+ */
2126
+ mbedtls_ssl_handshake_increment_state(ssl);
2127
+
2128
+ #if defined(MBEDTLS_HAVE_TIME)
2129
+ ssl->session_negotiate->start = mbedtls_time(NULL);
2130
+ #endif
2131
+
2132
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2133
+ if (ssl->handshake->new_session_ticket != 0) {
2134
+ ssl->session_negotiate->id_len = n = 0;
2135
+ memset(ssl->session_negotiate->id, 0, 32);
2136
+ } else
2137
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
2138
+ {
2139
+ ssl->session_negotiate->id_len = n = 32;
2140
+ if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, ssl->session_negotiate->id,
2141
+ n)) != 0) {
2142
+ return ret;
2143
+ }
2144
+ }
2145
+ } else {
2146
+ /*
2147
+ * Resuming a session
2148
+ */
2149
+ n = ssl->session_negotiate->id_len;
2150
+ mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC);
2151
+
2152
+ if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
2153
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
2154
+ return ret;
2155
+ }
2156
+ }
2157
+
2158
+ /*
2159
+ * 38 . 38 session id length
2160
+ * 39 . 38+n session id
2161
+ * 39+n . 40+n chosen ciphersuite
2162
+ * 41+n . 41+n chosen compression alg.
2163
+ * 42+n . 43+n extensions length
2164
+ * 44+n . 43+n+m extensions
2165
+ */
2166
+ *p++ = (unsigned char) ssl->session_negotiate->id_len;
2167
+ memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
2168
+ p += ssl->session_negotiate->id_len;
2169
+
2170
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
2171
+ MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 39, n);
2172
+ MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
2173
+ ssl->handshake->resume ? "a" : "no"));
2174
+
2175
+ MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
2176
+ p += 2;
2177
+ *p++ = MBEDTLS_BYTE_0(MBEDTLS_SSL_COMPRESS_NULL);
2178
+
2179
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
2180
+ mbedtls_ssl_get_ciphersuite_name(ssl->session_negotiate->ciphersuite)));
2181
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
2182
+ (unsigned int) MBEDTLS_SSL_COMPRESS_NULL));
2183
+
2184
+ /*
2185
+ * First write extensions, then the total length
2186
+ */
2187
+ ssl_write_renegotiation_ext(ssl, p + 2 + ext_len, &olen);
2188
+ ext_len += olen;
2189
+
2190
+ #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2191
+ ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len, &olen);
2192
+ ext_len += olen;
2193
+ #endif
2194
+
2195
+ #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2196
+ ssl_write_cid_ext(ssl, p + 2 + ext_len, &olen);
2197
+ ext_len += olen;
2198
+ #endif
2199
+
2200
+ #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2201
+ ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len, &olen);
2202
+ ext_len += olen;
2203
+ #endif
2204
+
2205
+ #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
2206
+ ssl_write_extended_ms_ext(ssl, p + 2 + ext_len, &olen);
2207
+ ext_len += olen;
2208
+ #endif
2209
+
2210
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
2211
+ ssl_write_session_ticket_ext(ssl, p + 2 + ext_len, &olen);
2212
+ ext_len += olen;
2213
+ #endif
2214
+
2215
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
2216
+ defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
2217
+ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2218
+ const mbedtls_ssl_ciphersuite_t *suite =
2219
+ mbedtls_ssl_ciphersuite_from_id(ssl->session_negotiate->ciphersuite);
2220
+ if (suite != NULL && mbedtls_ssl_ciphersuite_uses_ec(suite)) {
2221
+ ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len, &olen);
2222
+ ext_len += olen;
2223
+ }
2224
+ #endif
2225
+
2226
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2227
+ ret = ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len, &olen);
2228
+ if (ret != 0) {
2229
+ MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
2230
+ return ret;
2231
+ }
2232
+ ext_len += olen;
2233
+ #endif
2234
+
2235
+ #if defined(MBEDTLS_SSL_ALPN)
2236
+ unsigned char *end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
2237
+ if ((ret = mbedtls_ssl_write_alpn_ext(ssl, p + 2 + ext_len, end, &olen))
2238
+ != 0) {
2239
+ return ret;
2240
+ }
2241
+
2242
+ ext_len += olen;
2243
+ #endif
2244
+
2245
+ #if defined(MBEDTLS_SSL_DTLS_SRTP)
2246
+ ssl_write_use_srtp_ext(ssl, p + 2 + ext_len, &olen);
2247
+ ext_len += olen;
2248
+ #endif
2249
+
2250
+ MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
2251
+ ext_len));
2252
+
2253
+ if (ext_len > 0) {
2254
+ MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
2255
+ p += 2 + ext_len;
2256
+ }
2257
+
2258
+ ssl->out_msglen = (size_t) (p - buf);
2259
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2260
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
2261
+
2262
+ ret = mbedtls_ssl_write_handshake_msg(ssl);
2263
+
2264
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2265
+
2266
+ return ret;
2267
+ }
2268
+
2269
+ #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
2270
+ MBEDTLS_CHECK_RETURN_CRITICAL
2271
+ static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
2272
+ {
2273
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2274
+ ssl->handshake->ciphersuite_info;
2275
+
2276
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
2277
+
2278
+ if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
2279
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
2280
+ mbedtls_ssl_handshake_increment_state(ssl);
2281
+ return 0;
2282
+ }
2283
+
2284
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2285
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2286
+ }
2287
+ #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2288
+ MBEDTLS_CHECK_RETURN_CRITICAL
2289
+ static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
2290
+ {
2291
+ int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2292
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2293
+ ssl->handshake->ciphersuite_info;
2294
+ uint16_t dn_size, total_dn_size; /* excluding length bytes */
2295
+ size_t ct_len, sa_len; /* including length bytes */
2296
+ unsigned char *buf, *p;
2297
+ const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
2298
+ const mbedtls_x509_crt *crt;
2299
+ int authmode;
2300
+
2301
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
2302
+
2303
+ mbedtls_ssl_handshake_increment_state(ssl);
2304
+
2305
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2306
+ if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
2307
+ authmode = ssl->handshake->sni_authmode;
2308
+ } else
2309
+ #endif
2310
+ authmode = ssl->conf->authmode;
2311
+
2312
+ if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info) ||
2313
+ authmode == MBEDTLS_SSL_VERIFY_NONE) {
2314
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
2315
+ return 0;
2316
+ }
2317
+
2318
+ /*
2319
+ * 0 . 0 handshake type
2320
+ * 1 . 3 handshake length
2321
+ * 4 . 4 cert type count
2322
+ * 5 .. m-1 cert types
2323
+ * m .. m+1 sig alg length (TLS 1.2 only)
2324
+ * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
2325
+ * n .. n+1 length of all DNs
2326
+ * n+2 .. n+3 length of DN 1
2327
+ * n+4 .. ... Distinguished Name #1
2328
+ * ... .. ... length of DN 2, etc.
2329
+ */
2330
+ buf = ssl->out_msg;
2331
+ p = buf + 4;
2332
+
2333
+ /*
2334
+ * Supported certificate types
2335
+ *
2336
+ * ClientCertificateType certificate_types<1..2^8-1>;
2337
+ * enum { (255) } ClientCertificateType;
2338
+ */
2339
+ ct_len = 0;
2340
+
2341
+ #if defined(MBEDTLS_RSA_C)
2342
+ p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
2343
+ #endif
2344
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
2345
+ p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
2346
+ #endif
2347
+
2348
+ p[0] = (unsigned char) ct_len++;
2349
+ p += ct_len;
2350
+
2351
+ sa_len = 0;
2352
+
2353
+ /*
2354
+ * Add signature_algorithms for verify (TLS 1.2)
2355
+ *
2356
+ * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2357
+ *
2358
+ * struct {
2359
+ * HashAlgorithm hash;
2360
+ * SignatureAlgorithm signature;
2361
+ * } SignatureAndHashAlgorithm;
2362
+ *
2363
+ * enum { (255) } HashAlgorithm;
2364
+ * enum { (255) } SignatureAlgorithm;
2365
+ */
2366
+ const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
2367
+ if (sig_alg == NULL) {
2368
+ return MBEDTLS_ERR_SSL_BAD_CONFIG;
2369
+ }
2370
+
2371
+ for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
2372
+ unsigned char hash = MBEDTLS_BYTE_1(*sig_alg);
2373
+
2374
+ if (mbedtls_ssl_set_calc_verify_md(ssl, hash)) {
2375
+ continue;
2376
+ }
2377
+ if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
2378
+ continue;
2379
+ }
2380
+
2381
+ /* Write elements at offsets starting from 1 (offset 0 is for the
2382
+ * length). Thus the offset of each element is the length of the
2383
+ * partial list including that element. */
2384
+ sa_len += 2;
2385
+ MBEDTLS_PUT_UINT16_BE(*sig_alg, p, sa_len);
2386
+
2387
+ }
2388
+
2389
+ /* Fill in list length. */
2390
+ MBEDTLS_PUT_UINT16_BE(sa_len, p, 0);
2391
+ sa_len += 2;
2392
+ p += sa_len;
2393
+
2394
+ /*
2395
+ * DistinguishedName certificate_authorities<0..2^16-1>;
2396
+ * opaque DistinguishedName<1..2^16-1>;
2397
+ */
2398
+ p += 2;
2399
+
2400
+ total_dn_size = 0;
2401
+
2402
+ if (ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED) {
2403
+ /* NOTE: If trusted certificates are provisioned
2404
+ * via a CA callback (configured through
2405
+ * `mbedtls_ssl_conf_ca_cb()`, then the
2406
+ * CertificateRequest is currently left empty. */
2407
+
2408
+ #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
2409
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2410
+ if (ssl->handshake->dn_hints != NULL) {
2411
+ crt = ssl->handshake->dn_hints;
2412
+ } else
2413
+ #endif
2414
+ if (ssl->conf->dn_hints != NULL) {
2415
+ crt = ssl->conf->dn_hints;
2416
+ } else
2417
+ #endif
2418
+ #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2419
+ if (ssl->handshake->sni_ca_chain != NULL) {
2420
+ crt = ssl->handshake->sni_ca_chain;
2421
+ } else
2422
+ #endif
2423
+ crt = ssl->conf->ca_chain;
2424
+
2425
+ while (crt != NULL && crt->version != 0) {
2426
+ /* It follows from RFC 5280 A.1 that this length
2427
+ * can be represented in at most 11 bits. */
2428
+ dn_size = (uint16_t) crt->subject_raw.len;
2429
+
2430
+ if (end < p || (size_t) (end - p) < 2 + (size_t) dn_size) {
2431
+ MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
2432
+ break;
2433
+ }
2434
+
2435
+ MBEDTLS_PUT_UINT16_BE(dn_size, p, 0);
2436
+ p += 2;
2437
+ memcpy(p, crt->subject_raw.p, dn_size);
2438
+ p += dn_size;
2439
+
2440
+ MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p - dn_size, dn_size);
2441
+
2442
+ total_dn_size += (unsigned short) (2 + dn_size);
2443
+ crt = crt->next;
2444
+ }
2445
+ }
2446
+
2447
+ ssl->out_msglen = (size_t) (p - buf);
2448
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2449
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
2450
+ MBEDTLS_PUT_UINT16_BE(total_dn_size, ssl->out_msg, 4 + ct_len + sa_len);
2451
+
2452
+ ret = mbedtls_ssl_write_handshake_msg(ssl);
2453
+
2454
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2455
+
2456
+ return ret;
2457
+ }
2458
+ #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
2459
+
2460
+ #if (defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2461
+ defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED))
2462
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
2463
+ MBEDTLS_CHECK_RETURN_CRITICAL
2464
+ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
2465
+ {
2466
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2467
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2468
+ mbedtls_pk_context *pk;
2469
+ mbedtls_pk_type_t pk_type;
2470
+ psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
2471
+ unsigned char buf[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
2472
+ size_t key_len;
2473
+ #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2474
+ uint16_t tls_id = 0;
2475
+ psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
2476
+ mbedtls_ecp_group_id grp_id;
2477
+ mbedtls_ecp_keypair *key;
2478
+ #endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
2479
+
2480
+ pk = mbedtls_ssl_own_key(ssl);
2481
+
2482
+ if (pk == NULL) {
2483
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2484
+ }
2485
+
2486
+ pk_type = mbedtls_pk_get_type(pk);
2487
+
2488
+ switch (pk_type) {
2489
+ case MBEDTLS_PK_OPAQUE:
2490
+ #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2491
+ case MBEDTLS_PK_ECKEY:
2492
+ case MBEDTLS_PK_ECKEY_DH:
2493
+ case MBEDTLS_PK_ECDSA:
2494
+ #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2495
+ if (!mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
2496
+ return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
2497
+ }
2498
+
2499
+ /* Get the attributes of the key previously parsed by PK module in
2500
+ * order to extract its type and length (in bits). */
2501
+ status = psa_get_key_attributes(pk->priv_id, &key_attributes);
2502
+ if (status != PSA_SUCCESS) {
2503
+ ret = PSA_TO_MBEDTLS_ERR(status);
2504
+ goto exit;
2505
+ }
2506
+ ssl->handshake->xxdh_psa_type = psa_get_key_type(&key_attributes);
2507
+ ssl->handshake->xxdh_psa_bits = psa_get_key_bits(&key_attributes);
2508
+
2509
+ #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2510
+ if (pk_type != MBEDTLS_PK_OPAQUE) {
2511
+ /* PK_ECKEY[_DH] and PK_ECDSA instead as parsed from the PK
2512
+ * module and only have ECDSA capabilities. Since we need
2513
+ * them for ECDH later, we export and then re-import them with
2514
+ * proper flags and algorithm. Of course We also set key's type
2515
+ * and bits that we just got above. */
2516
+ key_attributes = psa_key_attributes_init();
2517
+ psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2518
+ psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2519
+ psa_set_key_type(&key_attributes,
2520
+ PSA_KEY_TYPE_ECC_KEY_PAIR(ssl->handshake->xxdh_psa_type));
2521
+ psa_set_key_bits(&key_attributes, ssl->handshake->xxdh_psa_bits);
2522
+
2523
+ status = psa_export_key(pk->priv_id, buf, sizeof(buf), &key_len);
2524
+ if (status != PSA_SUCCESS) {
2525
+ ret = PSA_TO_MBEDTLS_ERR(status);
2526
+ goto exit;
2527
+ }
2528
+ status = psa_import_key(&key_attributes, buf, key_len,
2529
+ &ssl->handshake->xxdh_psa_privkey);
2530
+ if (status != PSA_SUCCESS) {
2531
+ ret = PSA_TO_MBEDTLS_ERR(status);
2532
+ goto exit;
2533
+ }
2534
+
2535
+ /* Set this key as owned by the TLS library: it will be its duty
2536
+ * to clear it exit. */
2537
+ ssl->handshake->xxdh_psa_privkey_is_external = 0;
2538
+
2539
+ ret = 0;
2540
+ break;
2541
+ }
2542
+ #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
2543
+
2544
+ /* Opaque key is created by the user (externally from Mbed TLS)
2545
+ * so we assume it already has the right algorithm and flags
2546
+ * set. Just copy its ID as reference. */
2547
+ ssl->handshake->xxdh_psa_privkey = pk->priv_id;
2548
+ ssl->handshake->xxdh_psa_privkey_is_external = 1;
2549
+ ret = 0;
2550
+ break;
2551
+
2552
+ #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
2553
+ case MBEDTLS_PK_ECKEY:
2554
+ case MBEDTLS_PK_ECKEY_DH:
2555
+ case MBEDTLS_PK_ECDSA:
2556
+ key = mbedtls_pk_ec_rw(*pk);
2557
+ grp_id = mbedtls_pk_get_ec_group_id(pk);
2558
+ if (grp_id == MBEDTLS_ECP_DP_NONE) {
2559
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2560
+ }
2561
+ tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
2562
+ if (tls_id == 0) {
2563
+ /* This elliptic curve is not supported */
2564
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
2565
+ }
2566
+
2567
+ /* If the above conversion to TLS ID was fine, then also this one will
2568
+ be, so there is no need to check the return value here */
2569
+ mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
2570
+ &ssl->handshake->xxdh_psa_bits);
2571
+
2572
+ ssl->handshake->xxdh_psa_type = key_type;
2573
+
2574
+ key_attributes = psa_key_attributes_init();
2575
+ psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2576
+ psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2577
+ psa_set_key_type(&key_attributes,
2578
+ PSA_KEY_TYPE_ECC_KEY_PAIR(ssl->handshake->xxdh_psa_type));
2579
+ psa_set_key_bits(&key_attributes, ssl->handshake->xxdh_psa_bits);
2580
+
2581
+ ret = mbedtls_ecp_write_key_ext(key, &key_len, buf, sizeof(buf));
2582
+ if (ret != 0) {
2583
+ mbedtls_platform_zeroize(buf, sizeof(buf));
2584
+ break;
2585
+ }
2586
+
2587
+ status = psa_import_key(&key_attributes, buf, key_len,
2588
+ &ssl->handshake->xxdh_psa_privkey);
2589
+ if (status != PSA_SUCCESS) {
2590
+ ret = PSA_TO_MBEDTLS_ERR(status);
2591
+ mbedtls_platform_zeroize(buf, sizeof(buf));
2592
+ break;
2593
+ }
2594
+
2595
+ mbedtls_platform_zeroize(buf, sizeof(buf));
2596
+ ret = 0;
2597
+ break;
2598
+ #endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
2599
+ default:
2600
+ ret = MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
2601
+ }
2602
+
2603
+ exit:
2604
+ psa_reset_key_attributes(&key_attributes);
2605
+ mbedtls_platform_zeroize(buf, sizeof(buf));
2606
+
2607
+ return ret;
2608
+ }
2609
+ #else /* MBEDTLS_USE_PSA_CRYPTO */
2610
+ MBEDTLS_CHECK_RETURN_CRITICAL
2611
+ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
2612
+ {
2613
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2614
+
2615
+ const mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
2616
+ if (private_key == NULL) {
2617
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no server private key"));
2618
+ return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
2619
+ }
2620
+
2621
+ if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_ECKEY)) {
2622
+ MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
2623
+ return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
2624
+ }
2625
+
2626
+ if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
2627
+ mbedtls_pk_ec_ro(*mbedtls_ssl_own_key(ssl)),
2628
+ MBEDTLS_ECDH_OURS)) != 0) {
2629
+ MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
2630
+ return ret;
2631
+ }
2632
+
2633
+ return 0;
2634
+ }
2635
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
2636
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2637
+ MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2638
+
2639
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
2640
+ defined(MBEDTLS_SSL_ASYNC_PRIVATE)
2641
+ MBEDTLS_CHECK_RETURN_CRITICAL
2642
+ static int ssl_resume_server_key_exchange(mbedtls_ssl_context *ssl,
2643
+ size_t *signature_len)
2644
+ {
2645
+ /* Append the signature to ssl->out_msg, leaving 2 bytes for the
2646
+ * signature length which will be added in ssl_write_server_key_exchange
2647
+ * after the call to ssl_prepare_server_key_exchange.
2648
+ * ssl_write_server_key_exchange also takes care of incrementing
2649
+ * ssl->out_msglen. */
2650
+ unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
2651
+ size_t sig_max_len = (ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
2652
+ - sig_start);
2653
+ int ret = ssl->conf->f_async_resume(ssl,
2654
+ sig_start, signature_len, sig_max_len);
2655
+ if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
2656
+ ssl->handshake->async_in_progress = 0;
2657
+ mbedtls_ssl_set_async_operation_data(ssl, NULL);
2658
+ }
2659
+ MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret);
2660
+ return ret;
2661
+ }
2662
+ #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
2663
+ defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
2664
+
2665
+ /* Prepare the ServerKeyExchange message, up to and including
2666
+ * calculating the signature if any, but excluding formatting the
2667
+ * signature and sending the message. */
2668
+ MBEDTLS_CHECK_RETURN_CRITICAL
2669
+ static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
2670
+ size_t *signature_len)
2671
+ {
2672
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2673
+ ssl->handshake->ciphersuite_info;
2674
+
2675
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
2676
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2677
+ unsigned char *dig_signed = NULL;
2678
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2679
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
2680
+
2681
+ (void) ciphersuite_info; /* unused in some configurations */
2682
+ #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2683
+ (void) signature_len;
2684
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
2685
+
2686
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2687
+ #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2688
+ size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
2689
+ #else
2690
+ size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf);
2691
+ #endif
2692
+ #endif
2693
+
2694
+ ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
2695
+
2696
+ /*
2697
+ *
2698
+ * Part 1: Provide key exchange parameters for chosen ciphersuite.
2699
+ *
2700
+ */
2701
+
2702
+ /*
2703
+ * - ECJPAKE key exchanges
2704
+ */
2705
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2706
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
2707
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2708
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
2709
+ unsigned char *out_p = ssl->out_msg + ssl->out_msglen;
2710
+ unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
2711
+ ssl->out_msglen;
2712
+ size_t output_offset = 0;
2713
+ size_t output_len = 0;
2714
+
2715
+ /*
2716
+ * The first 3 bytes are:
2717
+ * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
2718
+ * [1, 2] elliptic curve's TLS ID
2719
+ *
2720
+ * However since we only support secp256r1 for now, we hardcode its
2721
+ * TLS ID here
2722
+ */
2723
+ uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
2724
+ MBEDTLS_ECP_DP_SECP256R1);
2725
+ if (tls_id == 0) {
2726
+ return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2727
+ }
2728
+ *out_p = MBEDTLS_ECP_TLS_NAMED_CURVE;
2729
+ MBEDTLS_PUT_UINT16_BE(tls_id, out_p, 1);
2730
+ output_offset += 3;
2731
+
2732
+ ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
2733
+ out_p + output_offset,
2734
+ end_p - out_p - output_offset, &output_len,
2735
+ MBEDTLS_ECJPAKE_ROUND_TWO);
2736
+ if (ret != 0) {
2737
+ psa_destroy_key(ssl->handshake->psa_pake_password);
2738
+ psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2739
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
2740
+ return ret;
2741
+ }
2742
+
2743
+ output_offset += output_len;
2744
+ ssl->out_msglen += output_offset;
2745
+ #else
2746
+ size_t len = 0;
2747
+
2748
+ ret = mbedtls_ecjpake_write_round_two(
2749
+ &ssl->handshake->ecjpake_ctx,
2750
+ ssl->out_msg + ssl->out_msglen,
2751
+ MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
2752
+ ssl->conf->f_rng, ssl->conf->p_rng);
2753
+ if (ret != 0) {
2754
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
2755
+ return ret;
2756
+ }
2757
+
2758
+ ssl->out_msglen += len;
2759
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
2760
+ }
2761
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2762
+
2763
+ /*
2764
+ * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
2765
+ * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
2766
+ * we use empty support identity hints here.
2767
+ **/
2768
+ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2769
+ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2770
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
2771
+ ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
2772
+ ssl->out_msg[ssl->out_msglen++] = 0x00;
2773
+ ssl->out_msg[ssl->out_msglen++] = 0x00;
2774
+ }
2775
+ #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2776
+ MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2777
+
2778
+ /*
2779
+ * - DHE key exchanges
2780
+ */
2781
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
2782
+ if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info)) {
2783
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2784
+ size_t len = 0;
2785
+
2786
+ if (ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL) {
2787
+ MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
2788
+ return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2789
+ }
2790
+
2791
+ /*
2792
+ * Ephemeral DH parameters:
2793
+ *
2794
+ * struct {
2795
+ * opaque dh_p<1..2^16-1>;
2796
+ * opaque dh_g<1..2^16-1>;
2797
+ * opaque dh_Ys<1..2^16-1>;
2798
+ * } ServerDHParams;
2799
+ */
2800
+ if ((ret = mbedtls_dhm_set_group(&ssl->handshake->dhm_ctx,
2801
+ &ssl->conf->dhm_P,
2802
+ &ssl->conf->dhm_G)) != 0) {
2803
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret);
2804
+ return ret;
2805
+ }
2806
+
2807
+ if ((ret = mbedtls_dhm_make_params(
2808
+ &ssl->handshake->dhm_ctx,
2809
+ (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
2810
+ ssl->out_msg + ssl->out_msglen, &len,
2811
+ ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2812
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret);
2813
+ return ret;
2814
+ }
2815
+
2816
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2817
+ dig_signed = ssl->out_msg + ssl->out_msglen;
2818
+ #endif
2819
+
2820
+ ssl->out_msglen += len;
2821
+
2822
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
2823
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
2824
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
2825
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
2826
+ }
2827
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
2828
+
2829
+ /*
2830
+ * - ECDHE key exchanges
2831
+ */
2832
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
2833
+ if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info)) {
2834
+ /*
2835
+ * Ephemeral ECDH parameters:
2836
+ *
2837
+ * struct {
2838
+ * ECParameters curve_params;
2839
+ * ECPoint public;
2840
+ * } ServerECDHParams;
2841
+ */
2842
+ uint16_t *curr_tls_id = ssl->handshake->curves_tls_id;
2843
+ const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
2844
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2845
+ size_t len = 0;
2846
+
2847
+ /* Match our preference list against the offered curves */
2848
+ if ((group_list == NULL) || (curr_tls_id == NULL)) {
2849
+ return MBEDTLS_ERR_SSL_BAD_CONFIG;
2850
+ }
2851
+ for (; *group_list != 0; group_list++) {
2852
+ for (curr_tls_id = ssl->handshake->curves_tls_id;
2853
+ *curr_tls_id != 0; curr_tls_id++) {
2854
+ if (*curr_tls_id == *group_list) {
2855
+ goto curve_matching_done;
2856
+ }
2857
+ }
2858
+ }
2859
+
2860
+ curve_matching_done:
2861
+ if (*curr_tls_id == 0) {
2862
+ MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
2863
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
2864
+ }
2865
+
2866
+ MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s",
2867
+ mbedtls_ssl_get_curve_name_from_tls_id(*curr_tls_id)));
2868
+
2869
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
2870
+ psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2871
+ psa_key_attributes_t key_attributes;
2872
+ mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2873
+ uint8_t *p = ssl->out_msg + ssl->out_msglen;
2874
+ const size_t header_size = 4; // curve_type(1), namedcurve(2),
2875
+ // data length(1)
2876
+ const size_t data_length_size = 1;
2877
+ psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
2878
+ size_t ec_bits = 0;
2879
+
2880
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based ECDH computation."));
2881
+
2882
+ /* Convert EC's TLS ID to PSA key type. */
2883
+ if (mbedtls_ssl_get_psa_curve_info_from_tls_id(*curr_tls_id,
2884
+ &key_type,
2885
+ &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
2886
+ MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid ecc group parse."));
2887
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2888
+ }
2889
+ handshake->xxdh_psa_type = key_type;
2890
+ handshake->xxdh_psa_bits = ec_bits;
2891
+
2892
+ key_attributes = psa_key_attributes_init();
2893
+ psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
2894
+ psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
2895
+ psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
2896
+ psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
2897
+
2898
+ /*
2899
+ * ECParameters curve_params
2900
+ *
2901
+ * First byte is curve_type, always named_curve
2902
+ */
2903
+ *p++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
2904
+
2905
+ /*
2906
+ * Next two bytes are the namedcurve value
2907
+ */
2908
+ MBEDTLS_PUT_UINT16_BE(*curr_tls_id, p, 0);
2909
+ p += 2;
2910
+
2911
+ /* Generate ECDH private key. */
2912
+ status = psa_generate_key(&key_attributes,
2913
+ &handshake->xxdh_psa_privkey);
2914
+ if (status != PSA_SUCCESS) {
2915
+ ret = PSA_TO_MBEDTLS_ERR(status);
2916
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
2917
+ return ret;
2918
+ }
2919
+
2920
+ /*
2921
+ * ECPoint public
2922
+ *
2923
+ * First byte is data length.
2924
+ * It will be filled later. p holds now the data length location.
2925
+ */
2926
+
2927
+ /* Export the public part of the ECDH private key from PSA.
2928
+ * Make one byte space for the length.
2929
+ */
2930
+ unsigned char *own_pubkey = p + data_length_size;
2931
+
2932
+ size_t own_pubkey_max_len = (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN
2933
+ - (own_pubkey - ssl->out_msg));
2934
+
2935
+ status = psa_export_public_key(handshake->xxdh_psa_privkey,
2936
+ own_pubkey, own_pubkey_max_len,
2937
+ &len);
2938
+ if (status != PSA_SUCCESS) {
2939
+ ret = PSA_TO_MBEDTLS_ERR(status);
2940
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
2941
+ (void) psa_destroy_key(handshake->xxdh_psa_privkey);
2942
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
2943
+ return ret;
2944
+ }
2945
+
2946
+ /* Store the length of the exported public key. */
2947
+ *p = (uint8_t) len;
2948
+
2949
+ /* Determine full message length. */
2950
+ len += header_size;
2951
+ #else
2952
+ mbedtls_ecp_group_id curr_grp_id =
2953
+ mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
2954
+
2955
+ if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
2956
+ curr_grp_id)) != 0) {
2957
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
2958
+ return ret;
2959
+ }
2960
+
2961
+ if ((ret = mbedtls_ecdh_make_params(
2962
+ &ssl->handshake->ecdh_ctx, &len,
2963
+ ssl->out_msg + ssl->out_msglen,
2964
+ MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
2965
+ ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2966
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
2967
+ return ret;
2968
+ }
2969
+
2970
+ MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2971
+ MBEDTLS_DEBUG_ECDH_Q);
2972
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
2973
+
2974
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2975
+ dig_signed = ssl->out_msg + ssl->out_msglen;
2976
+ #endif
2977
+
2978
+ ssl->out_msglen += len;
2979
+ }
2980
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
2981
+
2982
+ /*
2983
+ *
2984
+ * Part 2: For key exchanges involving the server signing the
2985
+ * exchange parameters, compute and add the signature here.
2986
+ *
2987
+ */
2988
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
2989
+ if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
2990
+ if (dig_signed == NULL) {
2991
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2992
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2993
+ }
2994
+
2995
+ size_t dig_signed_len = (size_t) (ssl->out_msg + ssl->out_msglen - dig_signed);
2996
+ size_t hashlen = 0;
2997
+ unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2998
+
2999
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3000
+
3001
+ /*
3002
+ * 2.1: Choose hash algorithm:
3003
+ * For TLS 1.2, obey signature-hash-algorithm extension
3004
+ * to choose appropriate hash.
3005
+ */
3006
+
3007
+ mbedtls_pk_type_t sig_alg =
3008
+ mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
3009
+
3010
+ unsigned char sig_hash =
3011
+ (unsigned char) mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
3012
+ ssl, mbedtls_ssl_sig_from_pk_alg(sig_alg));
3013
+
3014
+ mbedtls_md_type_t md_alg = mbedtls_ssl_md_alg_from_hash(sig_hash);
3015
+
3016
+ /* For TLS 1.2, obey signature-hash-algorithm extension
3017
+ * (RFC 5246, Sec. 7.4.1.4.1). */
3018
+ if (sig_alg == MBEDTLS_PK_NONE || md_alg == MBEDTLS_MD_NONE) {
3019
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3020
+ /* (... because we choose a cipher suite
3021
+ * only if there is a matching hash.) */
3022
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3023
+ }
3024
+
3025
+ MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg));
3026
+
3027
+ /*
3028
+ * 2.2: Compute the hash to be signed
3029
+ */
3030
+ if (md_alg != MBEDTLS_MD_NONE) {
3031
+ ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
3032
+ dig_signed,
3033
+ dig_signed_len,
3034
+ md_alg);
3035
+ if (ret != 0) {
3036
+ return ret;
3037
+ }
3038
+ } else {
3039
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3040
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3041
+ }
3042
+
3043
+ MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
3044
+
3045
+ /*
3046
+ * 2.3: Compute and add the signature
3047
+ */
3048
+ /*
3049
+ * We need to specify signature and hash algorithm explicitly through
3050
+ * a prefix to the signature.
3051
+ *
3052
+ * struct {
3053
+ * HashAlgorithm hash;
3054
+ * SignatureAlgorithm signature;
3055
+ * } SignatureAndHashAlgorithm;
3056
+ *
3057
+ * struct {
3058
+ * SignatureAndHashAlgorithm algorithm;
3059
+ * opaque signature<0..2^16-1>;
3060
+ * } DigitallySigned;
3061
+ *
3062
+ */
3063
+
3064
+ ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_hash_from_md_alg(md_alg);
3065
+ ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_sig_from_pk_alg(sig_alg);
3066
+
3067
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3068
+ if (ssl->conf->f_async_sign_start != NULL) {
3069
+ ret = ssl->conf->f_async_sign_start(ssl,
3070
+ mbedtls_ssl_own_cert(ssl),
3071
+ md_alg, hash, hashlen);
3072
+ switch (ret) {
3073
+ case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3074
+ /* act as if f_async_sign was null */
3075
+ break;
3076
+ case 0:
3077
+ ssl->handshake->async_in_progress = 1;
3078
+ return ssl_resume_server_key_exchange(ssl, signature_len);
3079
+ case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3080
+ ssl->handshake->async_in_progress = 1;
3081
+ return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3082
+ default:
3083
+ MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret);
3084
+ return ret;
3085
+ }
3086
+ }
3087
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3088
+
3089
+ if (mbedtls_ssl_own_key(ssl) == NULL) {
3090
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
3091
+ return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
3092
+ }
3093
+
3094
+ /* Append the signature to ssl->out_msg, leaving 2 bytes for the
3095
+ * signature length which will be added in ssl_write_server_key_exchange
3096
+ * after the call to ssl_prepare_server_key_exchange.
3097
+ * ssl_write_server_key_exchange also takes care of incrementing
3098
+ * ssl->out_msglen. */
3099
+ if ((ret = mbedtls_pk_sign(mbedtls_ssl_own_key(ssl),
3100
+ md_alg, hash, hashlen,
3101
+ ssl->out_msg + ssl->out_msglen + 2,
3102
+ out_buf_len - ssl->out_msglen - 2,
3103
+ signature_len,
3104
+ ssl->conf->f_rng,
3105
+ ssl->conf->p_rng)) != 0) {
3106
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
3107
+ return ret;
3108
+ }
3109
+ }
3110
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3111
+
3112
+ return 0;
3113
+ }
3114
+
3115
+ /* Prepare the ServerKeyExchange message and send it. For ciphersuites
3116
+ * that do not include a ServerKeyExchange message, do nothing. Either
3117
+ * way, if successful, move on to the next step in the SSL state
3118
+ * machine. */
3119
+ MBEDTLS_CHECK_RETURN_CRITICAL
3120
+ static int ssl_write_server_key_exchange(mbedtls_ssl_context *ssl)
3121
+ {
3122
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3123
+ size_t signature_len = 0;
3124
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
3125
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
3126
+ ssl->handshake->ciphersuite_info;
3127
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
3128
+
3129
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
3130
+
3131
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
3132
+ /* Extract static ECDH parameters and abort if ServerKeyExchange
3133
+ * is not needed. */
3134
+ if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info)) {
3135
+ /* For suites involving ECDH, extract DH parameters
3136
+ * from certificate at this point. */
3137
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
3138
+ if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info)) {
3139
+ ret = ssl_get_ecdh_params_from_cert(ssl);
3140
+ if (ret != 0) {
3141
+ MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
3142
+ return ret;
3143
+ }
3144
+ }
3145
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
3146
+
3147
+ /* Key exchanges not involving ephemeral keys don't use
3148
+ * ServerKeyExchange, so end here. */
3149
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
3150
+ mbedtls_ssl_handshake_increment_state(ssl);
3151
+ return 0;
3152
+ }
3153
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
3154
+
3155
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
3156
+ defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3157
+ /* If we have already prepared the message and there is an ongoing
3158
+ * signature operation, resume signing. */
3159
+ if (ssl->handshake->async_in_progress != 0) {
3160
+ MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
3161
+ ret = ssl_resume_server_key_exchange(ssl, &signature_len);
3162
+ } else
3163
+ #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
3164
+ defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
3165
+ {
3166
+ /* ServerKeyExchange is needed. Prepare the message. */
3167
+ ret = ssl_prepare_server_key_exchange(ssl, &signature_len);
3168
+ }
3169
+
3170
+ if (ret != 0) {
3171
+ /* If we're starting to write a new message, set ssl->out_msglen
3172
+ * to 0. But if we're resuming after an asynchronous message,
3173
+ * out_msglen is the amount of data written so far and mst be
3174
+ * preserved. */
3175
+ if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3176
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
3177
+ } else {
3178
+ ssl->out_msglen = 0;
3179
+ }
3180
+ return ret;
3181
+ }
3182
+
3183
+ /* If there is a signature, write its length.
3184
+ * ssl_prepare_server_key_exchange already wrote the signature
3185
+ * itself at its proper place in the output buffer. */
3186
+ #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
3187
+ if (signature_len != 0) {
3188
+ ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1(signature_len);
3189
+ ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0(signature_len);
3190
+
3191
+ MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
3192
+ ssl->out_msg + ssl->out_msglen,
3193
+ signature_len);
3194
+
3195
+ /* Skip over the already-written signature */
3196
+ ssl->out_msglen += signature_len;
3197
+ }
3198
+ #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
3199
+
3200
+ /* Add header and send. */
3201
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3202
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
3203
+
3204
+ mbedtls_ssl_handshake_increment_state(ssl);
3205
+
3206
+ if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3207
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3208
+ return ret;
3209
+ }
3210
+
3211
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
3212
+ return 0;
3213
+ }
3214
+
3215
+ MBEDTLS_CHECK_RETURN_CRITICAL
3216
+ static int ssl_write_server_hello_done(mbedtls_ssl_context *ssl)
3217
+ {
3218
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3219
+
3220
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
3221
+
3222
+ ssl->out_msglen = 4;
3223
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3224
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
3225
+
3226
+ mbedtls_ssl_handshake_increment_state(ssl);
3227
+
3228
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
3229
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3230
+ mbedtls_ssl_send_flight_completed(ssl);
3231
+ }
3232
+ #endif
3233
+
3234
+ if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3235
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3236
+ return ret;
3237
+ }
3238
+
3239
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
3240
+ if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3241
+ (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3242
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3243
+ return ret;
3244
+ }
3245
+ #endif /* MBEDTLS_SSL_PROTO_DTLS */
3246
+
3247
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
3248
+
3249
+ return 0;
3250
+ }
3251
+
3252
+ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3253
+ defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3254
+ MBEDTLS_CHECK_RETURN_CRITICAL
3255
+ static int ssl_parse_client_dh_public(mbedtls_ssl_context *ssl, unsigned char **p,
3256
+ const unsigned char *end)
3257
+ {
3258
+ int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3259
+ size_t n;
3260
+
3261
+ /*
3262
+ * Receive G^Y mod P, premaster = (G^Y)^X mod P
3263
+ */
3264
+ if (*p + 2 > end) {
3265
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3266
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3267
+ }
3268
+
3269
+ n = MBEDTLS_GET_UINT16_BE(*p, 0);
3270
+ *p += 2;
3271
+
3272
+ if (*p + n > end) {
3273
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3274
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3275
+ }
3276
+
3277
+ if ((ret = mbedtls_dhm_read_public(&ssl->handshake->dhm_ctx, *p, n)) != 0) {
3278
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret);
3279
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3280
+ }
3281
+
3282
+ *p += n;
3283
+
3284
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
3285
+
3286
+ return ret;
3287
+ }
3288
+ #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3289
+ MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3290
+
3291
+ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3292
+ defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3293
+
3294
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3295
+ MBEDTLS_CHECK_RETURN_CRITICAL
3296
+ static int ssl_resume_decrypt_pms(mbedtls_ssl_context *ssl,
3297
+ unsigned char *peer_pms,
3298
+ size_t *peer_pmslen,
3299
+ size_t peer_pmssize)
3300
+ {
3301
+ int ret = ssl->conf->f_async_resume(ssl,
3302
+ peer_pms, peer_pmslen, peer_pmssize);
3303
+ if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3304
+ ssl->handshake->async_in_progress = 0;
3305
+ mbedtls_ssl_set_async_operation_data(ssl, NULL);
3306
+ }
3307
+ MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret);
3308
+ return ret;
3309
+ }
3310
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3311
+
3312
+ MBEDTLS_CHECK_RETURN_CRITICAL
3313
+ static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context *ssl,
3314
+ const unsigned char *p,
3315
+ const unsigned char *end,
3316
+ unsigned char *peer_pms,
3317
+ size_t *peer_pmslen,
3318
+ size_t peer_pmssize)
3319
+ {
3320
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3321
+
3322
+ mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert(ssl);
3323
+ if (own_cert == NULL) {
3324
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no local certificate"));
3325
+ return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
3326
+ }
3327
+ mbedtls_pk_context *public_key = &own_cert->pk;
3328
+ mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
3329
+ size_t len = mbedtls_pk_get_len(public_key);
3330
+
3331
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3332
+ /* If we have already started decoding the message and there is an ongoing
3333
+ * decryption operation, resume signing. */
3334
+ if (ssl->handshake->async_in_progress != 0) {
3335
+ MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
3336
+ return ssl_resume_decrypt_pms(ssl,
3337
+ peer_pms, peer_pmslen, peer_pmssize);
3338
+ }
3339
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3340
+
3341
+ /*
3342
+ * Prepare to decrypt the premaster using own private RSA key
3343
+ */
3344
+ if (p + 2 > end) {
3345
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3346
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3347
+ }
3348
+ if (*p++ != MBEDTLS_BYTE_1(len) ||
3349
+ *p++ != MBEDTLS_BYTE_0(len)) {
3350
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3351
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3352
+ }
3353
+
3354
+ if (p + len != end) {
3355
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3356
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3357
+ }
3358
+
3359
+ /*
3360
+ * Decrypt the premaster secret
3361
+ */
3362
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3363
+ if (ssl->conf->f_async_decrypt_start != NULL) {
3364
+ ret = ssl->conf->f_async_decrypt_start(ssl,
3365
+ mbedtls_ssl_own_cert(ssl),
3366
+ p, len);
3367
+ switch (ret) {
3368
+ case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
3369
+ /* act as if f_async_decrypt_start was null */
3370
+ break;
3371
+ case 0:
3372
+ ssl->handshake->async_in_progress = 1;
3373
+ return ssl_resume_decrypt_pms(ssl,
3374
+ peer_pms,
3375
+ peer_pmslen,
3376
+ peer_pmssize);
3377
+ case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
3378
+ ssl->handshake->async_in_progress = 1;
3379
+ return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
3380
+ default:
3381
+ MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret);
3382
+ return ret;
3383
+ }
3384
+ }
3385
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3386
+
3387
+ if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_RSA)) {
3388
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
3389
+ return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
3390
+ }
3391
+
3392
+ ret = mbedtls_pk_decrypt(private_key, p, len,
3393
+ peer_pms, peer_pmslen, peer_pmssize,
3394
+ ssl->conf->f_rng, ssl->conf->p_rng);
3395
+ return ret;
3396
+ }
3397
+
3398
+ MBEDTLS_CHECK_RETURN_CRITICAL
3399
+ static int ssl_parse_encrypted_pms(mbedtls_ssl_context *ssl,
3400
+ const unsigned char *p,
3401
+ const unsigned char *end,
3402
+ size_t pms_offset)
3403
+ {
3404
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3405
+ unsigned char *pms = ssl->handshake->premaster + pms_offset;
3406
+ unsigned char ver[2];
3407
+ unsigned char fake_pms[48], peer_pms[48];
3408
+ size_t peer_pmslen;
3409
+ mbedtls_ct_condition_t diff;
3410
+
3411
+ /* In case of a failure in decryption, the decryption may write less than
3412
+ * 2 bytes of output, but we always read the first two bytes. It doesn't
3413
+ * matter in the end because diff will be nonzero in that case due to
3414
+ * ret being nonzero, and we only care whether diff is 0.
3415
+ * But do initialize peer_pms and peer_pmslen for robustness anyway. This
3416
+ * also makes memory analyzers happy (don't access uninitialized memory,
3417
+ * even if it's an unsigned char). */
3418
+ peer_pms[0] = peer_pms[1] = ~0;
3419
+ peer_pmslen = 0;
3420
+
3421
+ ret = ssl_decrypt_encrypted_pms(ssl, p, end,
3422
+ peer_pms,
3423
+ &peer_pmslen,
3424
+ sizeof(peer_pms));
3425
+
3426
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3427
+ if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
3428
+ return ret;
3429
+ }
3430
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3431
+
3432
+ mbedtls_ssl_write_version(ver, ssl->conf->transport,
3433
+ ssl->session_negotiate->tls_version);
3434
+
3435
+ /* Avoid data-dependent branches while checking for invalid
3436
+ * padding, to protect against timing-based Bleichenbacher-type
3437
+ * attacks. */
3438
+ diff = mbedtls_ct_bool(ret);
3439
+ diff = mbedtls_ct_bool_or(diff, mbedtls_ct_uint_ne(peer_pmslen, 48));
3440
+ diff = mbedtls_ct_bool_or(diff, mbedtls_ct_uint_ne(peer_pms[0], ver[0]));
3441
+ diff = mbedtls_ct_bool_or(diff, mbedtls_ct_uint_ne(peer_pms[1], ver[1]));
3442
+
3443
+ /*
3444
+ * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3445
+ * must not cause the connection to end immediately; instead, send a
3446
+ * bad_record_mac later in the handshake.
3447
+ * To protect against timing-based variants of the attack, we must
3448
+ * not have any branch that depends on whether the decryption was
3449
+ * successful. In particular, always generate the fake premaster secret,
3450
+ * regardless of whether it will ultimately influence the output or not.
3451
+ */
3452
+ ret = ssl->conf->f_rng(ssl->conf->p_rng, fake_pms, sizeof(fake_pms));
3453
+ if (ret != 0) {
3454
+ /* It's ok to abort on an RNG failure, since this does not reveal
3455
+ * anything about the RSA decryption. */
3456
+ return ret;
3457
+ }
3458
+
3459
+ #if defined(MBEDTLS_SSL_DEBUG_ALL)
3460
+ if (diff != MBEDTLS_CT_FALSE) {
3461
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3462
+ }
3463
+ #endif
3464
+
3465
+ if (sizeof(ssl->handshake->premaster) < pms_offset ||
3466
+ sizeof(ssl->handshake->premaster) - pms_offset < 48) {
3467
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3468
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3469
+ }
3470
+ ssl->handshake->pmslen = 48;
3471
+
3472
+ /* Set pms to either the true or the fake PMS, without
3473
+ * data-dependent branches. */
3474
+ mbedtls_ct_memcpy_if(diff, pms, fake_pms, peer_pms, ssl->handshake->pmslen);
3475
+
3476
+ return 0;
3477
+ }
3478
+ #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
3479
+ MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3480
+
3481
+ #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
3482
+ MBEDTLS_CHECK_RETURN_CRITICAL
3483
+ static int ssl_parse_client_psk_identity(mbedtls_ssl_context *ssl, unsigned char **p,
3484
+ const unsigned char *end)
3485
+ {
3486
+ int ret = 0;
3487
+ uint16_t n;
3488
+
3489
+ if (ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
3490
+ MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
3491
+ return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
3492
+ }
3493
+
3494
+ /*
3495
+ * Receive client pre-shared key identity name
3496
+ */
3497
+ if (end - *p < 2) {
3498
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3499
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3500
+ }
3501
+
3502
+ n = MBEDTLS_GET_UINT16_BE(*p, 0);
3503
+ *p += 2;
3504
+
3505
+ if (n == 0 || n > end - *p) {
3506
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3507
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3508
+ }
3509
+
3510
+ if (ssl->conf->f_psk != NULL) {
3511
+ if (ssl->conf->f_psk(ssl->conf->p_psk, ssl, *p, n) != 0) {
3512
+ ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3513
+ }
3514
+ } else {
3515
+ /* Identity is not a big secret since clients send it in the clear,
3516
+ * but treat it carefully anyway, just in case */
3517
+ if (n != ssl->conf->psk_identity_len ||
3518
+ mbedtls_ct_memcmp(ssl->conf->psk_identity, *p, n) != 0) {
3519
+ ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3520
+ }
3521
+ }
3522
+
3523
+ if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
3524
+ MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p, n);
3525
+ mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3526
+ MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY);
3527
+ return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
3528
+ }
3529
+
3530
+ *p += n;
3531
+
3532
+ return 0;
3533
+ }
3534
+ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
3535
+
3536
+ MBEDTLS_CHECK_RETURN_CRITICAL
3537
+ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
3538
+ {
3539
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3540
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3541
+ unsigned char *p, *end;
3542
+
3543
+ ciphersuite_info = ssl->handshake->ciphersuite_info;
3544
+
3545
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
3546
+
3547
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
3548
+ (defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
3549
+ defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED))
3550
+ if ((ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
3551
+ ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) &&
3552
+ (ssl->handshake->async_in_progress != 0)) {
3553
+ /* We've already read a record and there is an asynchronous
3554
+ * operation in progress to decrypt it. So skip reading the
3555
+ * record. */
3556
+ MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
3557
+ } else
3558
+ #endif
3559
+ if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3560
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
3561
+ return ret;
3562
+ }
3563
+
3564
+ p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
3565
+ end = ssl->in_msg + ssl->in_hslen;
3566
+
3567
+ if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3568
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3569
+ return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3570
+ }
3571
+
3572
+ if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE) {
3573
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
3574
+ return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3575
+ }
3576
+
3577
+ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
3578
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
3579
+ if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
3580
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
3581
+ return ret;
3582
+ }
3583
+
3584
+ if (p != end) {
3585
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3586
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3587
+ }
3588
+
3589
+ if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3590
+ ssl->handshake->premaster,
3591
+ MBEDTLS_PREMASTER_SIZE,
3592
+ &ssl->handshake->pmslen,
3593
+ ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3594
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3595
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3596
+ }
3597
+
3598
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
3599
+ } else
3600
+ #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
3601
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
3602
+ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3603
+ defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3604
+ defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
3605
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
3606
+ ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
3607
+ ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
3608
+ ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
3609
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
3610
+ size_t data_len = (size_t) (*p++);
3611
+ size_t buf_len = (size_t) (end - p);
3612
+ psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3613
+ mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3614
+
3615
+ MBEDTLS_SSL_DEBUG_MSG(3, ("Read the peer's public key."));
3616
+
3617
+ /*
3618
+ * We must have at least two bytes (1 for length, at least 1 for data)
3619
+ */
3620
+ if (buf_len < 2) {
3621
+ MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid buffer length: %" MBEDTLS_PRINTF_SIZET,
3622
+ buf_len));
3623
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
3624
+ }
3625
+
3626
+ if (data_len < 1 || data_len > buf_len) {
3627
+ MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid data length: %" MBEDTLS_PRINTF_SIZET
3628
+ " > %" MBEDTLS_PRINTF_SIZET,
3629
+ data_len, buf_len));
3630
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
3631
+ }
3632
+
3633
+ /* Store peer's ECDH public key. */
3634
+ if (data_len > sizeof(handshake->xxdh_psa_peerkey)) {
3635
+ MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %" MBEDTLS_PRINTF_SIZET
3636
+ " > %" MBEDTLS_PRINTF_SIZET,
3637
+ data_len,
3638
+ sizeof(handshake->xxdh_psa_peerkey)));
3639
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
3640
+ }
3641
+ memcpy(handshake->xxdh_psa_peerkey, p, data_len);
3642
+ handshake->xxdh_psa_peerkey_len = data_len;
3643
+
3644
+ /* Compute ECDH shared secret. */
3645
+ status = psa_raw_key_agreement(
3646
+ PSA_ALG_ECDH, handshake->xxdh_psa_privkey,
3647
+ handshake->xxdh_psa_peerkey, handshake->xxdh_psa_peerkey_len,
3648
+ handshake->premaster, sizeof(handshake->premaster),
3649
+ &handshake->pmslen);
3650
+ if (status != PSA_SUCCESS) {
3651
+ ret = PSA_TO_MBEDTLS_ERR(status);
3652
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
3653
+ if (handshake->xxdh_psa_privkey_is_external == 0) {
3654
+ (void) psa_destroy_key(handshake->xxdh_psa_privkey);
3655
+ }
3656
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3657
+ return ret;
3658
+ }
3659
+
3660
+ if (handshake->xxdh_psa_privkey_is_external == 0) {
3661
+ status = psa_destroy_key(handshake->xxdh_psa_privkey);
3662
+
3663
+ if (status != PSA_SUCCESS) {
3664
+ ret = PSA_TO_MBEDTLS_ERR(status);
3665
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
3666
+ return ret;
3667
+ }
3668
+ }
3669
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3670
+ #else
3671
+ if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
3672
+ p, (size_t) (end - p))) != 0) {
3673
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
3674
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3675
+ }
3676
+
3677
+ MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3678
+ MBEDTLS_DEBUG_ECDH_QP);
3679
+
3680
+ if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
3681
+ &ssl->handshake->pmslen,
3682
+ ssl->handshake->premaster,
3683
+ MBEDTLS_PREMASTER_SIZE,
3684
+ ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3685
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
3686
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3687
+ }
3688
+
3689
+ MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3690
+ MBEDTLS_DEBUG_ECDH_Z);
3691
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
3692
+ } else
3693
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3694
+ MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3695
+ MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3696
+ MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
3697
+ #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
3698
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
3699
+ if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
3700
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
3701
+ return ret;
3702
+ }
3703
+
3704
+ if (p != end) {
3705
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3706
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3707
+ }
3708
+
3709
+ #if !defined(MBEDTLS_USE_PSA_CRYPTO)
3710
+ if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3711
+ (mbedtls_key_exchange_type_t) ciphersuite_info->
3712
+ key_exchange)) != 0) {
3713
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
3714
+ return ret;
3715
+ }
3716
+ #endif /* !MBEDTLS_USE_PSA_CRYPTO */
3717
+ } else
3718
+ #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
3719
+ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
3720
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
3721
+ #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
3722
+ if (ssl->handshake->async_in_progress != 0) {
3723
+ /* There is an asynchronous operation in progress to
3724
+ * decrypt the encrypted premaster secret, so skip
3725
+ * directly to resuming this operation. */
3726
+ MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
3727
+ /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
3728
+ * won't actually use it, but maintain p anyway for robustness. */
3729
+ p += ssl->conf->psk_identity_len + 2;
3730
+ } else
3731
+ #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3732
+ if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
3733
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
3734
+ return ret;
3735
+ }
3736
+
3737
+ if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 2)) != 0) {
3738
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret);
3739
+ return ret;
3740
+ }
3741
+
3742
+ #if !defined(MBEDTLS_USE_PSA_CRYPTO)
3743
+ if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3744
+ (mbedtls_key_exchange_type_t) ciphersuite_info->
3745
+ key_exchange)) != 0) {
3746
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
3747
+ return ret;
3748
+ }
3749
+ #endif /* !MBEDTLS_USE_PSA_CRYPTO */
3750
+ } else
3751
+ #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
3752
+ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
3753
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
3754
+ if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
3755
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
3756
+ return ret;
3757
+ }
3758
+ if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
3759
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
3760
+ return ret;
3761
+ }
3762
+
3763
+ if (p != end) {
3764
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
3765
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3766
+ }
3767
+
3768
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
3769
+ unsigned char *pms = ssl->handshake->premaster;
3770
+ unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
3771
+ size_t pms_len;
3772
+
3773
+ /* Write length only when we know the actual value */
3774
+ if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
3775
+ pms + 2, pms_end - (pms + 2), &pms_len,
3776
+ ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
3777
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
3778
+ return ret;
3779
+ }
3780
+ MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
3781
+ pms += 2 + pms_len;
3782
+
3783
+ MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
3784
+ #else
3785
+ if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3786
+ (mbedtls_key_exchange_type_t) ciphersuite_info->
3787
+ key_exchange)) != 0) {
3788
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
3789
+ return ret;
3790
+ }
3791
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
3792
+ } else
3793
+ #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
3794
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3795
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
3796
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
3797
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3798
+ psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
3799
+ size_t ecpoint_len;
3800
+
3801
+ mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3802
+
3803
+ if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
3804
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
3805
+ psa_destroy_key(handshake->xxdh_psa_privkey);
3806
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3807
+ return ret;
3808
+ }
3809
+
3810
+ /* Keep a copy of the peer's public key */
3811
+ if (p >= end) {
3812
+ psa_destroy_key(handshake->xxdh_psa_privkey);
3813
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3814
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3815
+ }
3816
+
3817
+ ecpoint_len = *(p++);
3818
+ if ((size_t) (end - p) < ecpoint_len) {
3819
+ psa_destroy_key(handshake->xxdh_psa_privkey);
3820
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3821
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3822
+ }
3823
+
3824
+ /* When FFDH is enabled, the array handshake->xxdh_psa_peer_key size takes into account
3825
+ the sizes of the FFDH keys which are at least 2048 bits.
3826
+ The size of the array is thus greater than 256 bytes which is greater than any
3827
+ possible value of ecpoint_len (type uint8_t) and the check below can be skipped.*/
3828
+ #if !defined(PSA_WANT_ALG_FFDH)
3829
+ if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
3830
+ psa_destroy_key(handshake->xxdh_psa_privkey);
3831
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3832
+ return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
3833
+ }
3834
+ #else
3835
+ MBEDTLS_STATIC_ASSERT(sizeof(handshake->xxdh_psa_peerkey) >= UINT8_MAX,
3836
+ "peer key buffer too small");
3837
+ #endif
3838
+
3839
+ memcpy(handshake->xxdh_psa_peerkey, p, ecpoint_len);
3840
+ handshake->xxdh_psa_peerkey_len = ecpoint_len;
3841
+ p += ecpoint_len;
3842
+
3843
+ /* As RFC 5489 section 2, the premaster secret is formed as follows:
3844
+ * - a uint16 containing the length (in octets) of the ECDH computation
3845
+ * - the octet string produced by the ECDH computation
3846
+ * - a uint16 containing the length (in octets) of the PSK
3847
+ * - the PSK itself
3848
+ */
3849
+ unsigned char *psm = ssl->handshake->premaster;
3850
+ const unsigned char * const psm_end =
3851
+ psm + sizeof(ssl->handshake->premaster);
3852
+ /* uint16 to store length (in octets) of the ECDH computation */
3853
+ const size_t zlen_size = 2;
3854
+ size_t zlen = 0;
3855
+
3856
+ /* Compute ECDH shared secret. */
3857
+ status = psa_raw_key_agreement(PSA_ALG_ECDH,
3858
+ handshake->xxdh_psa_privkey,
3859
+ handshake->xxdh_psa_peerkey,
3860
+ handshake->xxdh_psa_peerkey_len,
3861
+ psm + zlen_size,
3862
+ psm_end - (psm + zlen_size),
3863
+ &zlen);
3864
+
3865
+ destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
3866
+ handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
3867
+
3868
+ if (status != PSA_SUCCESS) {
3869
+ return PSA_TO_MBEDTLS_ERR(status);
3870
+ } else if (destruction_status != PSA_SUCCESS) {
3871
+ return PSA_TO_MBEDTLS_ERR(destruction_status);
3872
+ }
3873
+
3874
+ /* Write the ECDH computation length before the ECDH computation */
3875
+ MBEDTLS_PUT_UINT16_BE(zlen, psm, 0);
3876
+ psm += zlen_size + zlen;
3877
+
3878
+ #else /* MBEDTLS_USE_PSA_CRYPTO */
3879
+ if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
3880
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
3881
+ return ret;
3882
+ }
3883
+
3884
+ if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
3885
+ p, (size_t) (end - p))) != 0) {
3886
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
3887
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
3888
+ }
3889
+
3890
+ MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
3891
+ MBEDTLS_DEBUG_ECDH_QP);
3892
+
3893
+ if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
3894
+ (mbedtls_key_exchange_type_t) ciphersuite_info->
3895
+ key_exchange)) != 0) {
3896
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
3897
+ return ret;
3898
+ }
3899
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
3900
+ } else
3901
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
3902
+ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
3903
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
3904
+ if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 0)) != 0) {
3905
+ MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret);
3906
+ return ret;
3907
+ }
3908
+ } else
3909
+ #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
3910
+ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
3911
+ if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
3912
+ #if defined(MBEDTLS_USE_PSA_CRYPTO)
3913
+ if ((ret = mbedtls_psa_ecjpake_read_round(
3914
+ &ssl->handshake->psa_pake_ctx, p, (size_t) (end - p),
3915
+ MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
3916
+ psa_destroy_key(ssl->handshake->psa_pake_password);
3917
+ psa_pake_abort(&ssl->handshake->psa_pake_ctx);
3918
+
3919
+ MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
3920
+ return ret;
3921
+ }
3922
+ #else
3923
+ ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
3924
+ p, (size_t) (end - p));
3925
+ if (ret != 0) {
3926
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
3927
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3928
+ }
3929
+
3930
+ ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
3931
+ ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
3932
+ ssl->conf->f_rng, ssl->conf->p_rng);
3933
+ if (ret != 0) {
3934
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
3935
+ return ret;
3936
+ }
3937
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
3938
+ } else
3939
+ #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
3940
+ {
3941
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3942
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3943
+ }
3944
+
3945
+ if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
3946
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
3947
+ return ret;
3948
+ }
3949
+
3950
+ mbedtls_ssl_handshake_increment_state(ssl);
3951
+
3952
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
3953
+
3954
+ return 0;
3955
+ }
3956
+
3957
+ #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
3958
+ MBEDTLS_CHECK_RETURN_CRITICAL
3959
+ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
3960
+ {
3961
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
3962
+ ssl->handshake->ciphersuite_info;
3963
+
3964
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
3965
+
3966
+ if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3967
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
3968
+ mbedtls_ssl_handshake_increment_state(ssl);
3969
+ return 0;
3970
+ }
3971
+
3972
+ MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3973
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3974
+ }
3975
+ #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
3976
+ MBEDTLS_CHECK_RETURN_CRITICAL
3977
+ static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
3978
+ {
3979
+ int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3980
+ size_t i, sig_len;
3981
+ unsigned char hash[48];
3982
+ unsigned char *hash_start = hash;
3983
+ size_t hashlen;
3984
+ mbedtls_pk_type_t pk_alg;
3985
+ mbedtls_md_type_t md_alg;
3986
+ const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
3987
+ ssl->handshake->ciphersuite_info;
3988
+ mbedtls_pk_context *peer_pk;
3989
+
3990
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
3991
+
3992
+ if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
3993
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
3994
+ mbedtls_ssl_handshake_increment_state(ssl);
3995
+ return 0;
3996
+ }
3997
+
3998
+ #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3999
+ if (ssl->session_negotiate->peer_cert == NULL) {
4000
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4001
+ mbedtls_ssl_handshake_increment_state(ssl);
4002
+ return 0;
4003
+ }
4004
+ #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4005
+ if (ssl->session_negotiate->peer_cert_digest == NULL) {
4006
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
4007
+ mbedtls_ssl_handshake_increment_state(ssl);
4008
+ return 0;
4009
+ }
4010
+ #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4011
+
4012
+ /* Read the message without adding it to the checksum */
4013
+ ret = mbedtls_ssl_read_record(ssl, 0 /* no checksum update */);
4014
+ if (0 != ret) {
4015
+ MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret);
4016
+ return ret;
4017
+ }
4018
+
4019
+ mbedtls_ssl_handshake_increment_state(ssl);
4020
+
4021
+ /* Process the message contents */
4022
+ if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
4023
+ ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY) {
4024
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4025
+ return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
4026
+ }
4027
+
4028
+ i = mbedtls_ssl_hs_hdr_len(ssl);
4029
+
4030
+ #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
4031
+ peer_pk = &ssl->handshake->peer_pubkey;
4032
+ #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4033
+ if (ssl->session_negotiate->peer_cert == NULL) {
4034
+ /* Should never happen */
4035
+ return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4036
+ }
4037
+ peer_pk = &ssl->session_negotiate->peer_cert->pk;
4038
+ #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4039
+
4040
+ /*
4041
+ * struct {
4042
+ * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
4043
+ * opaque signature<0..2^16-1>;
4044
+ * } DigitallySigned;
4045
+ */
4046
+ if (i + 2 > ssl->in_hslen) {
4047
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4048
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
4049
+ }
4050
+
4051
+ /*
4052
+ * Hash
4053
+ */
4054
+ md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
4055
+
4056
+ if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
4057
+ MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4058
+ " for verify message"));
4059
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
4060
+ }
4061
+
4062
+ #if !defined(MBEDTLS_MD_SHA1)
4063
+ if (MBEDTLS_MD_SHA1 == md_alg) {
4064
+ hash_start += 16;
4065
+ }
4066
+ #endif
4067
+
4068
+ /* Info from md_alg will be used instead */
4069
+ hashlen = 0;
4070
+
4071
+ i++;
4072
+
4073
+ /*
4074
+ * Signature
4075
+ */
4076
+ if ((pk_alg = mbedtls_ssl_pk_alg_from_sig(ssl->in_msg[i]))
4077
+ == MBEDTLS_PK_NONE) {
4078
+ MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
4079
+ " for verify message"));
4080
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
4081
+ }
4082
+
4083
+ /*
4084
+ * Check the certificate's key type matches the signature alg
4085
+ */
4086
+ if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
4087
+ MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
4088
+ return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
4089
+ }
4090
+
4091
+ i++;
4092
+
4093
+ if (i + 2 > ssl->in_hslen) {
4094
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4095
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
4096
+ }
4097
+
4098
+ sig_len = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i);
4099
+ i += 2;
4100
+
4101
+ if (i + sig_len != ssl->in_hslen) {
4102
+ MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
4103
+ return MBEDTLS_ERR_SSL_DECODE_ERROR;
4104
+ }
4105
+
4106
+ /* Calculate hash and verify signature */
4107
+ {
4108
+ size_t dummy_hlen;
4109
+ ret = ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
4110
+ if (0 != ret) {
4111
+ MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
4112
+ return ret;
4113
+ }
4114
+ }
4115
+
4116
+ if ((ret = mbedtls_pk_verify(peer_pk,
4117
+ md_alg, hash_start, hashlen,
4118
+ ssl->in_msg + i, sig_len)) != 0) {
4119
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
4120
+ return ret;
4121
+ }
4122
+
4123
+ ret = mbedtls_ssl_update_handshake_status(ssl);
4124
+ if (0 != ret) {
4125
+ MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
4126
+ return ret;
4127
+ }
4128
+
4129
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
4130
+
4131
+ return ret;
4132
+ }
4133
+ #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
4134
+
4135
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4136
+ MBEDTLS_CHECK_RETURN_CRITICAL
4137
+ static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
4138
+ {
4139
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4140
+ size_t tlen = 0;
4141
+ uint32_t lifetime = 0;
4142
+
4143
+ MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
4144
+
4145
+ ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4146
+ ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
4147
+
4148
+ /*
4149
+ * struct {
4150
+ * uint32 ticket_lifetime_hint;
4151
+ * opaque ticket<0..2^16-1>;
4152
+ * } NewSessionTicket;
4153
+ *
4154
+ * 4 . 7 ticket_lifetime_hint (0 = unspecified)
4155
+ * 8 . 9 ticket_len (n)
4156
+ * 10 . 9+n ticket content
4157
+ */
4158
+
4159
+ #if defined(MBEDTLS_HAVE_TIME)
4160
+ ssl->session_negotiate->ticket_creation_time = mbedtls_ms_time();
4161
+ #endif
4162
+ if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
4163
+ ssl->session_negotiate,
4164
+ ssl->out_msg + 10,
4165
+ ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
4166
+ &tlen, &lifetime)) != 0) {
4167
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
4168
+ }
4169
+
4170
+ MBEDTLS_PUT_UINT32_BE(lifetime, ssl->out_msg, 4);
4171
+ MBEDTLS_PUT_UINT16_BE(tlen, ssl->out_msg, 8);
4172
+ ssl->out_msglen = 10 + tlen;
4173
+
4174
+ /*
4175
+ * Morally equivalent to updating ssl->state, but NewSessionTicket and
4176
+ * ChangeCipherSpec share the same state.
4177
+ */
4178
+ ssl->handshake->new_session_ticket = 0;
4179
+
4180
+ if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4181
+ MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4182
+ return ret;
4183
+ }
4184
+
4185
+ MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
4186
+
4187
+ return 0;
4188
+ }
4189
+ #endif /* MBEDTLS_SSL_SESSION_TICKETS */
4190
+
4191
+ /*
4192
+ * SSL handshake -- server side -- single step
4193
+ */
4194
+ int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
4195
+ {
4196
+ int ret = 0;
4197
+
4198
+ MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl->state));
4199
+
4200
+ switch (ssl->state) {
4201
+ case MBEDTLS_SSL_HELLO_REQUEST:
4202
+ mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
4203
+ break;
4204
+
4205
+ /*
4206
+ * <== ClientHello
4207
+ */
4208
+ case MBEDTLS_SSL_CLIENT_HELLO:
4209
+ ret = ssl_parse_client_hello(ssl);
4210
+ break;
4211
+
4212
+ #if defined(MBEDTLS_SSL_PROTO_DTLS)
4213
+ case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
4214
+ return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
4215
+ #endif
4216
+
4217
+ /*
4218
+ * ==> ServerHello
4219
+ * Certificate
4220
+ * ( ServerKeyExchange )
4221
+ * ( CertificateRequest )
4222
+ * ServerHelloDone
4223
+ */
4224
+ case MBEDTLS_SSL_SERVER_HELLO:
4225
+ ret = ssl_write_server_hello(ssl);
4226
+ break;
4227
+
4228
+ case MBEDTLS_SSL_SERVER_CERTIFICATE:
4229
+ ret = mbedtls_ssl_write_certificate(ssl);
4230
+ break;
4231
+
4232
+ case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
4233
+ ret = ssl_write_server_key_exchange(ssl);
4234
+ break;
4235
+
4236
+ case MBEDTLS_SSL_CERTIFICATE_REQUEST:
4237
+ ret = ssl_write_certificate_request(ssl);
4238
+ break;
4239
+
4240
+ case MBEDTLS_SSL_SERVER_HELLO_DONE:
4241
+ ret = ssl_write_server_hello_done(ssl);
4242
+ break;
4243
+
4244
+ /*
4245
+ * <== ( Certificate/Alert )
4246
+ * ClientKeyExchange
4247
+ * ( CertificateVerify )
4248
+ * ChangeCipherSpec
4249
+ * Finished
4250
+ */
4251
+ case MBEDTLS_SSL_CLIENT_CERTIFICATE:
4252
+ ret = mbedtls_ssl_parse_certificate(ssl);
4253
+ break;
4254
+
4255
+ case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
4256
+ ret = ssl_parse_client_key_exchange(ssl);
4257
+ break;
4258
+
4259
+ case MBEDTLS_SSL_CERTIFICATE_VERIFY:
4260
+ ret = ssl_parse_certificate_verify(ssl);
4261
+ break;
4262
+
4263
+ case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
4264
+ ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
4265
+ break;
4266
+
4267
+ case MBEDTLS_SSL_CLIENT_FINISHED:
4268
+ ret = mbedtls_ssl_parse_finished(ssl);
4269
+ break;
4270
+
4271
+ /*
4272
+ * ==> ( NewSessionTicket )
4273
+ * ChangeCipherSpec
4274
+ * Finished
4275
+ */
4276
+ case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
4277
+ #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4278
+ if (ssl->handshake->new_session_ticket != 0) {
4279
+ ret = ssl_write_new_session_ticket(ssl);
4280
+ } else
4281
+ #endif
4282
+ ret = mbedtls_ssl_write_change_cipher_spec(ssl);
4283
+ break;
4284
+
4285
+ case MBEDTLS_SSL_SERVER_FINISHED:
4286
+ ret = mbedtls_ssl_write_finished(ssl);
4287
+ break;
4288
+
4289
+ case MBEDTLS_SSL_FLUSH_BUFFERS:
4290
+ MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
4291
+ mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
4292
+ break;
4293
+
4294
+ case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
4295
+ mbedtls_ssl_handshake_wrapup(ssl);
4296
+ break;
4297
+
4298
+ default:
4299
+ MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
4300
+ return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4301
+ }
4302
+
4303
+ return ret;
4304
+ }
4305
+
4306
+ void mbedtls_ssl_conf_preference_order(mbedtls_ssl_config *conf, int order)
4307
+ {
4308
+ conf->respect_cli_pref = order;
4309
+ }
4310
+
4311
+ #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_2 */