SConscript 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from building import *
  2. import rtconfig
  3. Import('RTT_ROOT')
  4. import os
  5. from string import Template
  6. # 1. Specific certificate file template
  7. cert_template = """
  8. /*
  9. * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
  10. * License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. */
  25. #include "certs.h"
  26. const char mbedtls_root_certificate[] =
  27. ${CERT_CONTENT}
  28. ;
  29. const size_t mbedtls_root_certificate_len = sizeof(mbedtls_root_certificate);
  30. """
  31. # 2. Create substitute from template
  32. cert_subs = Template(cert_template)
  33. # 3. Get the current absolute path
  34. cwd = GetCurrentDir()
  35. # 4. PEM certificate file path (*.pem or *.cer)
  36. certs_user_dir = cwd + os.sep + 'certs'
  37. certs_default_dir = cwd + os.sep + (os.sep).join(['certs', 'default'])
  38. ROOT_CA_FILE = []
  39. # 5. File that stores the contents of the certificate file
  40. output_cert_file = cwd + os.sep + (os.sep).join(['ports', 'src', 'tls_certificate.c'])
  41. if GetDepend(['PKG_USING_MBEDTLS_EXAMPLE']):
  42. path = cwd + os.sep + (os.sep).join(['certs', 'default', 'DIGITAL_SIGNATURE_TRUST_ROOT_CA.cer'])
  43. if os.path.exists(path):
  44. ROOT_CA_FILE += [path]
  45. if GetDepend(['PKG_USING_MBEDTLS_USE_ALL_CERTS']):
  46. file_list = os.listdir(certs_default_dir)
  47. if len(file_list):
  48. for i in range(0, len(file_list)):
  49. path = os.path.join(certs_default_dir, file_list[i])
  50. if os.path.isfile(path):
  51. ROOT_CA_FILE += [path]
  52. if GetDepend(['PKG_USING_MBEDTLS_USER_CERTS']):
  53. file_list = os.listdir(certs_user_dir)
  54. if len(file_list):
  55. for i in range(0, len(file_list)):
  56. path = os.path.join(certs_user_dir, file_list[i])
  57. if os.path.isfile(path):
  58. ROOT_CA_FILE += [path]
  59. KCONFIG_ROOT_CA_DICT = {'PKG_USING_MBEDTLS_THAWTE_ROOT_CA': 'THAWTE_ROOT_CA.cer', \
  60. 'PKG_USING_MBEDTLS_VERSIGN_PBULIC_ROOT_CA': 'VERSIGN_PUBLIC_ROOT_CA.cer', \
  61. 'PKG_USING_MBEDTLS_VERSIGN_UNIVERSAL_ROOT_CA': 'VERSIGN_UNIVERSAL_ROOT_CA.cer', \
  62. 'PKG_USING_MBEDTLS_GEOTRUST_ROOT_CA': 'GEOTRUST_ROOT_CA.cer', \
  63. 'PKG_USING_MBEDTLS_DIGICERT_ROOT_CA': 'DIGICERT_ROOT_CA.cer', \
  64. 'PKG_USING_MBEDTLS_GODADDY_ROOT_CA': 'GODADDY_ROOT_CA.cer',
  65. 'PKG_USING_MBEDTLS_COMODOR_ROOT_CA': 'COMODOR_ROOT_CA.cer', \
  66. 'PKG_USING_MBEDTLS_DST_ROOT_CA': 'DIGITAL_SIGNATURE_TRUST_ROOT_CA.cer', \
  67. 'PKG_USING_MBEDTLS_CLOBALSIGN_ROOT_CA': 'CLOBALSIGN_ROOT_CA.cer', \
  68. 'PKG_USING_MBEDTLS_ENTRUST_ROOT_CA': 'ENTRUST_ROOT_CA.cer'}
  69. for key, value in KCONFIG_ROOT_CA_DICT.items():
  70. if GetDepend([key]):
  71. path = os.path.join(certs_default_dir, value)
  72. if os.path.exists(path) and os.path.isfile(path):
  73. ROOT_CA_FILE += [path]
  74. ROOT_CA_FILE = list(set(ROOT_CA_FILE))
  75. file_content = ""
  76. # 6. Traverse the specified certificate file
  77. for i in range(0, len(ROOT_CA_FILE)):
  78. if os.path.isfile(ROOT_CA_FILE[i]):
  79. # READ CER FILE, copy to tls_certificate.c
  80. with open(ROOT_CA_FILE[i], 'r') as ca:
  81. for line in ca.readlines():
  82. file_content += '"' + line.strip() + '\\r\\n" \\\n'
  83. # 7. Populate certificate template content
  84. cert_content = cert_subs.substitute(CERT_CONTENT = file_content)
  85. # 8. Write certificate template content to tls_certificate.c
  86. with open(output_cert_file, 'w') as f:
  87. f.write(cert_content)
  88. src = Glob('mbedtls/library/*.c')
  89. SrcRemove(src, 'mbedtls/library/net_sockets.c')
  90. src += Glob('ports/src/*.c')
  91. if GetDepend(['PKG_USING_MBEDTLS_EXAMPLE']):
  92. src += Glob('samples/*.c')
  93. CPPPATH = [
  94. cwd + '/mbedtls/include',
  95. cwd + '/mbedtls/include/mbedtls',
  96. cwd + '/ports/inc',
  97. ]
  98. if rtconfig.CROSS_TOOL == 'gcc' :
  99. CPPDEFINES = ['MBEDTLS_CONFIG_FILE=\\"tls_config.h\\"']
  100. elif rtconfig.CROSS_TOOL == 'keil' or rtconfig.CROSS_TOOL == 'iar':
  101. import shutil
  102. cp_src = cwd + '/ports/inc/tls_config.h'
  103. cp_dst = cwd + '/mbedtls/include/mbedtls/config.h'
  104. shutil.copyfile(cp_src, cp_dst)
  105. CPPDEFINES = []
  106. else:
  107. CPPDEFINES = []
  108. group = DefineGroup('mbedtls', src, depend = ['PKG_USING_MBEDTLS'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
  109. Return('group')