CMakeLists.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. idf_build_get_property(idf_target IDF_TARGET)
  2. idf_build_get_property(python PYTHON)
  3. set(priv_requires soc esp_hw_support)
  4. if(NOT BOOTLOADER_BUILD)
  5. list(APPEND priv_requires esp_pm)
  6. endif()
  7. idf_component_register(SRCS "esp_crt_bundle/esp_crt_bundle.c"
  8. INCLUDE_DIRS "port/include" "mbedtls/include" "esp_crt_bundle/include"
  9. REQUIRES lwip
  10. PRIV_REQUIRES "${priv_requires}"
  11. )
  12. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE)
  13. set(bundle_name "x509_crt_bundle")
  14. set(DEFAULT_CRT_DIR ${COMPONENT_DIR}/esp_crt_bundle)
  15. # Generate custom certificate bundle using the generate_cert_bundle utility
  16. set(GENERATE_CERT_BUNDLEPY ${python} ${COMPONENT_DIR}/esp_crt_bundle/gen_crt_bundle.py)
  17. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL)
  18. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem)
  19. elseif(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN)
  20. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem)
  21. list(APPEND args --filter ${DEFAULT_CRT_DIR}/cmn_crt_authorities.csv)
  22. endif()
  23. if(CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE)
  24. get_filename_component(custom_bundle_path
  25. ${CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH} ABSOLUTE BASE_DIR "${project_dir}")
  26. list(APPEND crt_paths ${custom_bundle_path})
  27. endif()
  28. list(APPEND args --input ${crt_paths} -q)
  29. get_filename_component(crt_bundle
  30. ${bundle_name}
  31. ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  32. # Generate bundle according to config
  33. add_custom_command(OUTPUT ${crt_bundle}
  34. COMMAND ${GENERATE_CERT_BUNDLEPY} ${args}
  35. DEPENDS ${custom_bundle_path}
  36. VERBATIM)
  37. add_custom_target(custom_bundle DEPENDS ${cert_bundle})
  38. add_dependencies(${COMPONENT_LIB} custom_bundle)
  39. target_add_binary_data(${COMPONENT_LIB} ${crt_bundle} BINARY)
  40. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  41. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  42. "${crt_bundle}")
  43. endif()
  44. # Only build mbedtls libraries
  45. set(ENABLE_TESTING CACHE BOOL OFF)
  46. set(ENABLE_PROGRAMS CACHE BOOL OFF)
  47. # Needed to for include_next includes to work from within mbedtls
  48. include_directories("${COMPONENT_DIR}/port/include")
  49. # Import mbedtls library targets
  50. add_subdirectory(mbedtls)
  51. # Use port specific implementation of net_socket.c instead of one from mbedtls
  52. get_target_property(src_tls mbedtls SOURCES)
  53. list(REMOVE_ITEM src_tls net_sockets.c)
  54. set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
  55. if(CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1)
  56. get_target_property(src_tls mbedtls SOURCES)
  57. list(REMOVE_ITEM src_tls ssl_ciphersuites.c ssl_cli.c ssl_tls.c)
  58. set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
  59. get_target_property(src_crypto mbedcrypto SOURCES)
  60. list(REMOVE_ITEM src_crypto cipher_wrap.c ecdsa.c ecp.c ecp_curves.c oid.c pk_wrap.c)
  61. set_property(TARGET mbedcrypto PROPERTY SOURCES ${src_crypto})
  62. get_target_property(src_x509 mbedx509 SOURCES)
  63. list(REMOVE_ITEM src_x509 x509_crt.c)
  64. set_property(TARGET mbedx509 PROPERTY SOURCES ${src_x509})
  65. endif()
  66. set(mbedtls_targets mbedtls mbedcrypto mbedx509)
  67. set(mbedtls_target_sources "${COMPONENT_DIR}/port/mbedtls_debug.c"
  68. "${COMPONENT_DIR}/port/net_sockets.c")
  69. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  70. set(mbedtls_target_sources ${mbedtls_target_sources}
  71. "${COMPONENT_DIR}/port/dynamic/esp_mbedtls_dynamic_impl.c"
  72. "${COMPONENT_DIR}/port/dynamic/esp_ssl_cli.c"
  73. "${COMPONENT_DIR}/port/dynamic/esp_ssl_srv.c"
  74. "${COMPONENT_DIR}/port/dynamic/esp_ssl_tls.c")
  75. endif()
  76. # Add port files to mbedtls targets
  77. target_sources(mbedtls PRIVATE ${mbedtls_target_sources})
  78. # Choose perihperal type
  79. if(CONFIG_IDF_TARGET_ESP32)
  80. set(SHA_PERIPHERAL_TYPE "parallel_engine")
  81. set(AES_PERIPHERAL_TYPE "block")
  82. else()
  83. set(SHA_PERIPHERAL_TYPE "dma")
  84. set(AES_PERIPHERAL_TYPE "dma")
  85. endif()
  86. if(SHA_PERIPHERAL_TYPE STREQUAL "dma")
  87. target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/sha/dma/include")
  88. if(CONFIG_IDF_TARGET_ESP32S2)
  89. set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_crypto_dma_impl.c")
  90. else()
  91. set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_gdma_impl.c"
  92. "${COMPONENT_DIR}/port/crypto_shared_gdma/esp_crypto_shared_gdma.c")
  93. endif()
  94. target_sources(mbedcrypto PRIVATE "${SHA_DMA_SRCS}")
  95. endif()
  96. if(AES_PERIPHERAL_TYPE STREQUAL "dma")
  97. if(CONFIG_IDF_TARGET_ESP32S2)
  98. set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_crypto_dma_impl.c")
  99. else()
  100. set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_gdma_impl.c")
  101. endif()
  102. target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/dma/include")
  103. target_sources(mbedcrypto PRIVATE "${AES_DMA_SRCS}")
  104. endif()
  105. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c"
  106. "${COMPONENT_DIR}/port/esp_mem.c"
  107. "${COMPONENT_DIR}/port/esp_timing.c"
  108. "${COMPONENT_DIR}/port/sha/esp_sha.c"
  109. "${COMPONENT_DIR}/port/aes/esp_aes_xts.c"
  110. "${COMPONENT_DIR}/port/aes/esp_aes_common.c"
  111. "${COMPONENT_DIR}/port/aes/${AES_PERIPHERAL_TYPE}/esp_aes.c"
  112. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/sha.c"
  113. )
  114. # CONFIG_ESP_TLS_USE_DS_PERIPHERAL can be enabled only for the supported targets.
  115. if(CONFIG_ESP_TLS_USE_DS_PERIPHERAL)
  116. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_ds/esp_rsa_sign_alt.c")
  117. endif()
  118. # Note: some mbedTLS hardware acceleration can be enabled/disabled by config.
  119. #
  120. # We don't need to filter aes.c as this uses a different prefix (esp_aes_x) and the
  121. # config option only changes the prefixes in the header so mbedtls_aes_x compiles to esp_aes_x
  122. #
  123. # The other port-specific files don't override internal mbedTLS functions, they just add new functions.
  124. if(CONFIG_MBEDTLS_HARDWARE_MPI)
  125. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_bignum.c"
  126. "${COMPONENT_DIR}/port/${idf_target}/bignum.c"
  127. )
  128. endif()
  129. if(CONFIG_MBEDTLS_HARDWARE_SHA)
  130. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha1.c"
  131. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha256.c"
  132. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha512.c"
  133. )
  134. endif()
  135. if(CONFIG_MBEDTLS_HARDWARE_GCM)
  136. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/esp_aes_gcm.c")
  137. endif()
  138. if(CONFIG_MBEDTLS_ROM_MD5)
  139. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/md/esp_md.c")
  140. endif()
  141. foreach(target ${mbedtls_targets})
  142. target_compile_definitions(${target} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h")
  143. endforeach()
  144. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  145. set(WRAP_FUNCTIONS
  146. mbedtls_ssl_handshake_client_step
  147. mbedtls_ssl_handshake_server_step
  148. mbedtls_ssl_read
  149. mbedtls_ssl_write
  150. mbedtls_ssl_session_reset
  151. mbedtls_ssl_free
  152. mbedtls_ssl_setup
  153. mbedtls_ssl_send_alert_message
  154. mbedtls_ssl_close_notify)
  155. foreach(wrap ${WRAP_FUNCTIONS})
  156. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}")
  157. endforeach()
  158. endif()
  159. if(CONFIG_MBEDTLS_HARDWARE_MPI)
  160. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_mpi_exp_mod")
  161. endif()
  162. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_LIBRARIES mbedtls)
  163. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_LIBRARIES idf::driver idf::${target})
  164. set_property(TARGET mbedcrypto APPEND PROPERTY INTERFACE_LINK_LIBRARIES idf::driver idf::${target})
  165. # Link mbedtls libraries to component library
  166. target_link_libraries(${COMPONENT_LIB} PUBLIC ${mbedtls_targets})
  167. if(CONFIG_ESP_TLS_USE_DS_PERIPHERAL)
  168. # Link target (e.g. esp32s2) library to component library
  169. idf_component_get_property(target_lib ${target} COMPONENT_LIB)
  170. set_property(TARGET mbedcrypto APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<LINK_ONLY:${target_lib}>)
  171. # The linker seems to be unable to resolve all the dependencies without increasing this
  172. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_MULTIPLICITY 6)
  173. target_link_libraries(${COMPONENT_LIB} PUBLIC ${target_lib})
  174. endif()
  175. # Link esp-cryptoauthlib to mbedtls
  176. if(CONFIG_ATCA_MBEDTLS_ECDSA)
  177. idf_component_get_property(cryptoauthlib esp-cryptoauthlib COMPONENT_LIB)
  178. target_link_libraries(${COMPONENT_LIB} PUBLIC ${cryptoauthlib})
  179. endif()