-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSConscript
164 lines (135 loc) · 6.52 KB
/
SConscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from building import *
import rtconfig
Import('RTT_ROOT')
import os
from string import Template
# 1. Specific certificate file template
cert_template = """
/*
* Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "mbedtls/certs.h"
const char mbedtls_root_certificate[] =
"-----BEGIN CERTIFICATE-----\\r\\n\" \\
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\\r\\n" \\
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\\r\\n" \\
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\\r\\n" \\
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3Q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\\r\\n" \\
${CERT_CONTENT}
;
const size_t mbedtls_root_certificate_len = sizeof(mbedtls_root_certificate);
"""
# 2. Create substitute from template
cert_subs = Template(cert_template)
# 3. Get the current absolute path
cwd = GetCurrentDir()
# 4. PEM certificate file path (*.pem or *.cer)
certs_user_dir = cwd + os.sep + 'certs'
certs_default_dir = cwd + os.sep + (os.sep).join(['certs', 'default'])
ROOT_CA_FILE = []
# 5. File that stores the contents of the certificate file
output_cert_file = cwd + os.sep + (os.sep).join(['ports', 'src', 'tls_certificate.c'])
if GetDepend(['PKG_USING_MBEDTLS_EXAMPLE']):
path = cwd + os.sep + (os.sep).join(['certs', 'default', 'DIGITAL_SIGNATURE_TRUST_ROOT_CA.cer'])
if os.path.exists(path):
ROOT_CA_FILE += [path]
if GetDepend(['PKG_USING_MBEDTLS_USE_ALL_CERTS']):
file_list = os.listdir(certs_default_dir)
if len(file_list):
for i in range(0, len(file_list)):
path = os.path.join(certs_default_dir, file_list[i])
if os.path.isfile(path):
ROOT_CA_FILE += [path]
if GetDepend(['PKG_USING_MBEDTLS_USER_CERTS']):
file_list = os.listdir(certs_user_dir)
if len(file_list):
for i in range(0, len(file_list)):
path = os.path.join(certs_user_dir, file_list[i])
if os.path.isfile(path):
ROOT_CA_FILE += [path]
KCONFIG_ROOT_CA_DICT = {'PKG_USING_MBEDTLS_THAWTE_ROOT_CA': 'THAWTE_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_VERSIGN_PBULIC_ROOT_CA': 'VERSIGN_PUBLIC_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_VERSIGN_UNIVERSAL_ROOT_CA': 'VERSIGN_UNIVERSAL_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_GEOTRUST_ROOT_CA': 'GEOTRUST_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_DIGICERT_ROOT_CA': 'DIGICERT_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_GODADDY_ROOT_CA': 'GODADDY_ROOT_CA.cer',
'PKG_USING_MBEDTLS_COMODOR_ROOT_CA': 'COMODOR_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_DST_ROOT_CA': 'DIGITAL_SIGNATURE_TRUST_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_CLOBALSIGN_ROOT_CA': 'CLOBALSIGN_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_ENTRUST_ROOT_CA': 'ENTRUST_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_CERTUM_TRUSTED_NETWORK_ROOT_CA': 'CERTUM_TRUSTED_NETWORK_ROOT_CA.cer', \
'PKG_USING_MBEDTLS_AMAZON_ROOT_CA': 'AMAZON_ROOT_CA.cer'}
for key, value in KCONFIG_ROOT_CA_DICT.items():
if GetDepend([key]):
path = os.path.join(certs_default_dir, value)
if os.path.exists(path) and os.path.isfile(path):
ROOT_CA_FILE += [path]
ROOT_CA_FILE = list(set(ROOT_CA_FILE))
file_content = ""
# 6. Traverse the specified certificate file
if len(ROOT_CA_FILE) > 0:
for i in range(0, len(ROOT_CA_FILE)):
if os.path.isfile(ROOT_CA_FILE[i]):
# READ CER FILE, copy to tls_certificate.c
with open(ROOT_CA_FILE[i], 'r') as ca:
# Pre-read, check first line
if not ca.readline().startswith("-----BEGIN CERTIFICATE"):
print("[mbedtls] Warning: ", ROOT_CA_FILE[i], "is not CA file! Skipped!")
continue
ca.seek(0)
for line in ca.readlines():
file_content += '"' + line.strip() + '\\r\\n" \\\n'
# 7. Populate certificate template content
cert_content = cert_subs.substitute(CERT_CONTENT = file_content)
# 8. Write certificate template content to tls_certificate.c
with open(output_cert_file, 'w') as f:
f.write(cert_content)
src = Glob('mbedtls/library/*.c')
SrcRemove(src, 'mbedtls/library/net_sockets.c')
src += Glob('ports/src/*.c')
if GetDepend(['PKG_USING_MBEDTLS_EXAMPLE']):
src += Glob('samples/*.c')
CPPPATH = [
cwd + '/mbedtls/include',
cwd + '/mbedtls/library',
cwd + '/ports/inc',
]
if rtconfig.CROSS_TOOL == 'gcc' or rtconfig.CROSS_TOOL == 'keil' or rtconfig.CROSS_TOOL == 'iar':
import shutil
cp_src = cwd + '/ports/inc/tls_config.h'
cp_dst = cwd + '/mbedtls/include/mbedtls/config.h'
shutil.copyfile(cp_src, cp_dst)
CPPDEFINES = []
else:
CPPDEFINES = []
group = DefineGroup('mbedtls', src, depend = ['PKG_USING_MBEDTLS'], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
Return('group')