CMakeLists.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. idf_build_get_property(idf_target IDF_TARGET)
  2. idf_build_get_property(python PYTHON)
  3. idf_component_register(SRCS "esp_crt_bundle/esp_crt_bundle.c"
  4. INCLUDE_DIRS "port/include" "mbedtls/include" "esp_crt_bundle/include"
  5. REQUIRES lwip
  6. PRIV_REQUIRES soc
  7. )
  8. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE)
  9. set(bundle_name "x509_crt_bundle")
  10. set(DEFAULT_CRT_DIR ${COMPONENT_DIR}/esp_crt_bundle)
  11. # Generate custom certificate bundle using the generate_cert_bundle utility
  12. set(GENERATE_CERT_BUNDLEPY ${python} ${COMPONENT_DIR}/esp_crt_bundle/gen_crt_bundle.py)
  13. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL)
  14. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem)
  15. elseif(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN)
  16. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem)
  17. list(APPEND args --filter ${DEFAULT_CRT_DIR}/cmn_crt_authorities.csv)
  18. endif()
  19. if(CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE)
  20. get_filename_component(custom_bundle_path
  21. ${CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH} ABSOLUTE BASE_DIR "${project_dir}")
  22. list(APPEND crt_paths ${custom_bundle_path})
  23. endif()
  24. list(APPEND args --input ${crt_paths} -q)
  25. get_filename_component(crt_bundle
  26. ${bundle_name}
  27. ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  28. # Generate bundle according to config
  29. add_custom_command(OUTPUT ${crt_bundle}
  30. COMMAND ${GENERATE_CERT_BUNDLEPY} ${args}
  31. DEPENDS ${custom_bundle_path}
  32. VERBATIM)
  33. add_custom_target(custom_bundle DEPENDS ${cert_bundle})
  34. add_dependencies(${COMPONENT_LIB} custom_bundle)
  35. target_add_binary_data(${COMPONENT_LIB} ${crt_bundle} BINARY)
  36. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  37. APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
  38. "${crt_bundle}")
  39. endif()
  40. # Only build mbedtls libraries
  41. set(ENABLE_TESTING CACHE BOOL OFF)
  42. set(ENABLE_PROGRAMS CACHE BOOL OFF)
  43. # Needed to for include_next includes to work from within mbedtls
  44. include_directories("${COMPONENT_DIR}/port/include")
  45. # Import mbedtls library targets
  46. add_subdirectory(mbedtls)
  47. # Use port specific implementation of net_socket.c instead of one from mbedtls
  48. get_target_property(src_tls mbedtls SOURCES)
  49. list(REMOVE_ITEM src_tls net_sockets.c)
  50. set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
  51. set(mbedtls_targets mbedtls mbedcrypto mbedx509)
  52. set(mbedtls_target_sources "${COMPONENT_DIR}/port/mbedtls_debug.c"
  53. "${COMPONENT_DIR}/port/net_sockets.c")
  54. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  55. set(mbedtls_target_sources ${mbedtls_target_sources}
  56. "${COMPONENT_DIR}/port/dynamic/esp_mbedtls_dynamic_impl.c"
  57. "${COMPONENT_DIR}/port/dynamic/esp_ssl_cli.c"
  58. "${COMPONENT_DIR}/port/dynamic/esp_ssl_srv.c"
  59. "${COMPONENT_DIR}/port/dynamic/esp_ssl_tls.c")
  60. endif()
  61. # Add port files to mbedtls targets
  62. target_sources(mbedtls PRIVATE ${mbedtls_target_sources})
  63. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c"
  64. "${COMPONENT_DIR}/port/esp_mem.c"
  65. "${COMPONENT_DIR}/port/esp_timing.c"
  66. "${COMPONENT_DIR}/port/esp_sha.c"
  67. "${COMPONENT_DIR}/port/esp_aes_xts.c"
  68. "${COMPONENT_DIR}/port/${idf_target}/aes.c"
  69. "${COMPONENT_DIR}/port/${idf_target}/sha.c"
  70. )
  71. # Note: some mbedTLS hardware acceleration can be enabled/disabled by config.
  72. #
  73. # We don't need to filter aes.c as this uses a different prefix (esp_aes_x) and the
  74. # config option only changes the prefixes in the header so mbedtls_aes_x compiles to esp_aes_x
  75. #
  76. # The other port-specific files don't override internal mbedTLS functions, they just add new functions.
  77. if(CONFIG_MBEDTLS_HARDWARE_MPI)
  78. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_bignum.c"
  79. "${COMPONENT_DIR}/port/${idf_target}/bignum.c"
  80. )
  81. endif()
  82. if(CONFIG_MBEDTLS_HARDWARE_SHA)
  83. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/${idf_target}/esp_sha1.c"
  84. "${COMPONENT_DIR}/port/${idf_target}/esp_sha256.c"
  85. "${COMPONENT_DIR}/port/${idf_target}/esp_sha512.c"
  86. )
  87. endif()
  88. foreach(target ${mbedtls_targets})
  89. target_compile_definitions(${target} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h")
  90. endforeach()
  91. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  92. set(WRAP_FUNCTIONS
  93. mbedtls_ssl_handshake_client_step
  94. mbedtls_ssl_handshake_server_step
  95. mbedtls_ssl_read
  96. mbedtls_ssl_write
  97. mbedtls_ssl_session_reset
  98. mbedtls_ssl_free
  99. mbedtls_ssl_setup
  100. mbedtls_ssl_send_alert_message
  101. mbedtls_ssl_close_notify)
  102. foreach(wrap ${WRAP_FUNCTIONS})
  103. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}")
  104. endforeach()
  105. endif()
  106. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_LIBRARIES mbedtls)
  107. # Link mbedtls libraries to component library
  108. target_link_libraries(${COMPONENT_LIB} PUBLIC ${mbedtls_targets})
  109. # Link esp-cryptoauthlib to mbedtls
  110. if(CONFIG_ATCA_MBEDTLS_ECDSA)
  111. idf_component_get_property(cryptoauthlib esp-cryptoauthlib COMPONENT_LIB)
  112. target_link_libraries(${COMPONENT_LIB} PUBLIC ${cryptoauthlib})
  113. endif()