1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 from mach
.decorators
import Command
, CommandArgument
8 from mach
.util
import UserError
9 from mozpack
.files
import FileFinder
12 def run_module_main_on(module
, input_filename
, output_is_binary
):
13 """Run the given module (pycert, pykey, etc.) on the given
15 # By convention, the specification files have names of the form
16 # "name.ext.*spec", where "ext" is some extension, and the "*" in
17 # "*spec" identifies what kind of specification it represents
18 # (certspec, keyspec, etc.). Taking off the ".*spec" part results in
19 # the desired filename for this file.
20 output_filename
= os
.path
.splitext(input_filename
)[0]
28 with
open(output_filename
, mode
=mode
, encoding
=encoding
, newline
=newline
) as output
:
29 module
.main(output
, input_filename
)
32 def is_certspec_file(filename
):
33 """Returns True if the given filename is a certificate
34 specification file (.certspec) and False otherwise."""
35 return filename
.endswith(".certspec")
38 def is_keyspec_file(filename
):
39 """Returns True if the given filename is a key specification
40 file (.keyspec) and False otherwise."""
41 return filename
.endswith(".keyspec")
44 def is_pkcs12spec_file(filename
):
45 """Returns True if the given filename is a pkcs12
46 specification file (.pkcs12spec) and False otherwise."""
47 return filename
.endswith(".pkcs12spec")
50 def is_sctspec_file(filename
):
51 """Returns True if the given filename is an SCT
52 specification file (.sctspec) and False otherwise."""
53 return filename
.endswith(".sctspec")
56 def is_specification_file(filename
):
57 """Returns True if the given filename is a specification
58 file supported by this script, and False otherewise."""
60 is_certspec_file(filename
)
61 or is_keyspec_file(filename
)
62 or is_pkcs12spec_file(filename
)
63 or is_sctspec_file(filename
)
68 "generate-test-certs",
70 description
="Generate test certificates and keys from specifications.",
75 help="Specification files for test certs. If omitted, all certs are regenerated.",
77 def generate_test_certs(command_context
, specifications
):
78 """Generate test certificates and keys from specifications."""
84 if not specifications
:
85 specifications
= find_all_specifications(command_context
)
87 for specification
in specifications
:
88 output_is_binary
= False
89 if is_certspec_file(specification
):
91 elif is_keyspec_file(specification
):
93 elif is_pkcs12spec_file(specification
):
95 output_is_binary
= True
96 elif is_sctspec_file(specification
):
98 output_is_binary
= True
101 "'{}' is not a .certspec, .keyspec, or .pkcs12spec file".format(
105 run_module_main_on(module
, os
.path
.abspath(specification
), output_is_binary
)
109 def find_all_specifications(command_context
):
110 """Searches the source tree for all specification files
111 and returns them as a list."""
115 "security/manager/ssl/tests",
116 "services/settings/test/unit/test_remote_settings_signatures",
117 "testing/xpcshell/moz-http2",
118 "toolkit/mozapps/extensions/test/xpcshell/data/productaddons",
120 finder
= FileFinder(command_context
.topsrcdir
)
121 for inclusion_path
in inclusions
:
122 for f
, _
in finder
.find(inclusion_path
):
123 if is_specification_file(f
):
124 specifications
.append(os
.path
.join(command_context
.topsrcdir
, f
))
125 return specifications