blob: c7395f145b1b6b58d9b96b86a1501d04ff16f7e4 [file] [log] [blame]
Jerry Yu58118692023-05-23 16:14:47 +08001#!/usr/bin/env python3
2
3"""
4Generate `tests/src/test_certs.h` which includes certficaties/keys/certificate list for testing.
5"""
6
7#
8# Copyright The Mbed TLS Contributors
9# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
22
23
24import os
25import sys
26import argparse
27import jinja2
28
29MACROS = [
30 ("comment1", None, None),
31 ("string", "TEST_CA_CRT_EC_PEM", "tests/data_files/test-ca2.crt"),
32 ("binary", "TEST_CA_CRT_EC_DER", "tests/data_files/test-ca2.crt.der"),
33 ("string", "TEST_CA_KEY_EC_PEM", "tests/data_files/test-ca2.key.enc"),
34 ("password", "TEST_CA_PWD_EC_PEM", "PolarSSLTest"),
35 ("binary", "TEST_CA_KEY_EC_DER", "tests/data_files/test-ca2.key.der"),
36 ("string", "TEST_CA_CRT_RSA_SHA256_PEM",
37 "tests/data_files/test-ca-sha256.crt"),
38 ("binary", "TEST_CA_CRT_RSA_SHA256_DER",
39 "tests/data_files/test-ca-sha256.crt.der"),
40 ("string", "TEST_CA_CRT_RSA_SHA1_PEM", "tests/data_files/test-ca-sha1.crt"),
41 ("binary", "TEST_CA_CRT_RSA_SHA1_DER", "tests/data_files/test-ca-sha1.crt.der"),
42 ("string", "TEST_CA_KEY_RSA_PEM", "tests/data_files/test-ca.key"),
43 ("password", "TEST_CA_PWD_RSA_PEM", "PolarSSLTest"),
44 ("binary", "TEST_CA_KEY_RSA_DER", "tests/data_files/test-ca.key.der"),
45 ("comment2", None, None),
46 ("string", "TEST_SRV_CRT_EC_PEM", "tests/data_files/server5.crt"),
47 ("binary", "TEST_SRV_CRT_EC_DER", "tests/data_files/server5.crt.der"),
48 ("string", "TEST_SRV_KEY_EC_PEM", "tests/data_files/server5.key"),
49 ("binary", "TEST_SRV_KEY_EC_DER", "tests/data_files/server5.key.der"),
50 ("string", "TEST_SRV_CRT_RSA_SHA256_PEM",
51 "tests/data_files/server2-sha256.crt"),
52 ("binary", "TEST_SRV_CRT_RSA_SHA256_DER",
53 "tests/data_files/server2-sha256.crt.der"),
54 ("string", "TEST_SRV_CRT_RSA_SHA1_PEM", "tests/data_files/server2.crt"),
55 ("binary", "TEST_SRV_CRT_RSA_SHA1_DER", "tests/data_files/server2.crt.der"),
56 ("string", "TEST_SRV_KEY_RSA_PEM", "tests/data_files/server2.key"),
57 ("binary", "TEST_SRV_KEY_RSA_DER", "tests/data_files/server2.key.der"),
58 ("comment3", None, None),
59 ("string", "TEST_CLI_CRT_EC_PEM", "tests/data_files/cli2.crt"),
60 ("binary", "TEST_CLI_CRT_EC_DER", "tests/data_files/cli2.crt.der"),
61 ("string", "TEST_CLI_KEY_EC_PEM", "tests/data_files/cli2.key"),
62 ("binary", "TEST_CLI_KEY_EC_DER", "tests/data_files/cli2.key.der"),
63 ("string", "TEST_CLI_CRT_RSA_PEM", "tests/data_files/cli-rsa-sha256.crt"),
64 ("binary", "TEST_CLI_CRT_RSA_DER", "tests/data_files/cli-rsa-sha256.crt.der"),
65 ("string", "TEST_CLI_KEY_RSA_PEM", "tests/data_files/cli-rsa.key"),
66 ("binary", "TEST_CLI_KEY_RSA_DER", "tests/data_files/cli-rsa.key.der")
67]
68
69
70class CustomAction(argparse.Action):
71 def __call__(self, parser, namespace, values, option_string):
72 if not hasattr(namespace, 'values'):
73 setattr(namespace, 'values', [])
74 macro_name, filename = values
75 if self.dest in ('string', 'binary') and not os.path.exists(filename):
76 raise argparse.ArgumentError(
77 None, '`{}`: Input file does not exist.'.format(filename))
78 namespace.values.append((self.dest, macro_name, filename))
79
80
81def custom_type(value):
82 ret = value.split('=', 1)
83 if len(ret) != 2:
84 raise argparse.ArgumentTypeError(
85 '`{}` is not MACRO=value format'.format(value))
86 return ret
87
88
89def build_argparser(parser):
90 parser.description = __doc__
91 parser.add_argument('--string', type=custom_type, action=CustomAction,
92 metavar='MACRO_NAME=path/to/file', help='PEM to C string. ')
93 parser.add_argument('--binary', type=custom_type, action=CustomAction, metavar='MACRO_NAME=path/to/file',
94 help='DER to C arrary.')
95 parser.add_argument('--password', type=custom_type, action=CustomAction,
96 metavar='MACRO_NAME=password', help='Password to C string.')
97 parser.add_argument('--output', type=str, required=True)
98
99
100def main():
101 parser = argparse.ArgumentParser()
102 build_argparser(parser)
103 args = parser.parse_args()
104 return generate(**vars(args))
105
106 # sys.exit(0)
107
108
109def generate(values=[],output=None, **kwargs):
110 this_dir = os.path.dirname(os.path.abspath(__file__))
111 project_root = os.path.abspath(os.path.join(this_dir, '..', '..'))
112 template_loader = jinja2.FileSystemLoader(
113 searchpath=os.path.join(this_dir, '..', 'data_files'))
114 template_env = jinja2.Environment(
115 loader=template_loader, lstrip_blocks=True, trim_blocks=True)
116
117 def read_as_c_array(filename):
118 with open(filename, 'rb') as f:
119 data = f.read(12)
120 while data:
121 yield ', '.join(['{:#04x}'.format(b) for b in data])
122 data = f.read(12)
123
124 def read_lines(filename):
125 with open(filename) as f:
126 try:
127 for line in f:
128 yield line.strip()
129 except:
130 print(filename)
131 raise
132
133 def put_to_column(value, position=0):
134 return ' '*position + value
135
136 template_env.filters['read_as_c_array'] = read_as_c_array
137 template_env.filters['read_lines'] = read_lines
138 template_env.filters['put_to_column'] = put_to_column
139
140 template = template_env.get_template('test_certs.h.jinja2')
141
142 with open(output, 'w') as f:
143 f.write(template.render(macros=values))
144
145
146if __name__ == '__main__':
147 sys.exit(main())