Skip to content

Commit b47d1d7

Browse files
committed
black and fix doc typo
1 parent b1ce1d0 commit b47d1d7

File tree

1 file changed

+76
-52
lines changed

1 file changed

+76
-52
lines changed

tools/gen_crt_bundle.py

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# you may not use this file except in compliance with the License.
1515
# You may obtain a copy of the License at
1616
#
17-
# http:#www.apache.org/licenses/LICENSE-2.0
17+
# http://www.apache.org/licenses/LICENSE-2.0
1818
#
1919
# Unless required by applicable law or agreed to in writing, software
2020
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -37,27 +37,29 @@
3737
from cryptography.hazmat.backends import default_backend
3838
from cryptography.hazmat.primitives import serialization
3939
except ImportError:
40-
print('The cryptography package is not installed.'
41-
'Please refer to the Get Started section of the ESP-IDF Programming Guide for '
42-
'setting up the required packages.')
40+
print(
41+
"The cryptography package is not installed."
42+
"Please refer to the Get Started section of the ESP-IDF Programming Guide for "
43+
"setting up the required packages."
44+
)
4345
raise
4446

45-
ca_bundle_bin_file = 'x509_crt_bundle'
47+
ca_bundle_bin_file = "x509_crt_bundle"
4648

4749
quiet = False
4850

4951

5052
def status(msg):
51-
""" Print status message to stderr """
53+
"""Print status message to stderr"""
5254
if not quiet:
5355
critical(msg)
5456

5557

5658
def critical(msg):
57-
""" Print critical message to stderr """
58-
sys.stderr.write('gen_crt_bundle.py: ')
59+
"""Print critical message to stderr"""
60+
sys.stderr.write("gen_crt_bundle.py: ")
5961
sys.stderr.write(msg)
60-
sys.stderr.write('\n')
62+
sys.stderr.write("\n")
6163

6264

6365
class CertificateBundle:
@@ -75,75 +77,81 @@ def add_from_path(self, crts_path):
7577
found |= self.add_from_file(os.path.join(crts_path, file_path))
7678

7779
if found is False:
78-
raise InputError('No valid x509 certificates found in %s' % crts_path)
80+
raise InputError("No valid x509 certificates found in %s" % crts_path)
7981

8082
def add_from_file(self, file_path):
8183
try:
82-
if file_path.endswith('.pem'):
83-
status('Parsing certificates from %s' % file_path)
84-
with open(file_path, 'r', encoding='utf-8') as f:
84+
if file_path.endswith(".pem"):
85+
status("Parsing certificates from %s" % file_path)
86+
with open(file_path, "r", encoding="utf-8") as f:
8587
crt_str = f.read()
8688
self.add_from_pem(crt_str)
8789
return True
8890

89-
elif file_path.endswith('.der'):
90-
status('Parsing certificates from %s' % file_path)
91-
with open(file_path, 'rb') as f:
91+
elif file_path.endswith(".der"):
92+
status("Parsing certificates from %s" % file_path)
93+
with open(file_path, "rb") as f:
9294
crt_str = f.read()
9395
self.add_from_der(crt_str)
9496
return True
9597

9698
except ValueError:
97-
critical('Invalid certificate in %s' % file_path)
98-
raise InputError('Invalid certificate')
99+
critical("Invalid certificate in %s" % file_path)
100+
raise InputError("Invalid certificate")
99101

100102
return False
101103

102104
def add_from_pem(self, crt_str):
103-
""" A single PEM file may have multiple certificates """
105+
"""A single PEM file may have multiple certificates"""
104106

105-
crt = ''
107+
crt = ""
106108
count = 0
107109
start = False
108110

109111
for strg in crt_str.splitlines(True):
110-
if strg == '-----BEGIN CERTIFICATE-----\n' and start is False:
111-
crt = ''
112+
if strg == "-----BEGIN CERTIFICATE-----\n" and start is False:
113+
crt = ""
112114
start = True
113-
elif strg == '-----END CERTIFICATE-----\n' and start is True:
114-
crt += strg + '\n'
115+
elif strg == "-----END CERTIFICATE-----\n" and start is True:
116+
crt += strg + "\n"
115117
start = False
116-
self.certificates.append(x509.load_pem_x509_certificate(crt.encode(), default_backend()))
118+
self.certificates.append(
119+
x509.load_pem_x509_certificate(crt.encode(), default_backend())
120+
)
117121
count += 1
118122
if start is True:
119123
crt += strg
120124

121-
if(count == 0):
122-
raise InputError('No certificate found')
125+
if count == 0:
126+
raise InputError("No certificate found")
123127

124-
status('Successfully added %d certificates' % count)
128+
status("Successfully added %d certificates" % count)
125129

126130
def add_from_der(self, crt_str):
127131
self.certificates.append(x509.load_der_x509_certificate(crt_str, default_backend()))
128-
status('Successfully added 1 certificate')
132+
status("Successfully added 1 certificate")
129133

130134
def create_bundle(self):
131135
# Sort certificates in order to do binary search when looking up certificates
132-
self.certificates = sorted(self.certificates, key=lambda cert: cert.subject.public_bytes(default_backend()))
136+
self.certificates = sorted(
137+
self.certificates, key=lambda cert: cert.subject.public_bytes(default_backend())
138+
)
133139

134-
bundle = struct.pack('>H', len(self.certificates))
140+
bundle = struct.pack(">H", len(self.certificates))
135141

136142
for crt in self.certificates:
137-
""" Read the public key as DER format """
143+
"""Read the public key as DER format"""
138144
pub_key = crt.public_key()
139-
pub_key_der = pub_key.public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo)
145+
pub_key_der = pub_key.public_bytes(
146+
serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo
147+
)
140148

141149
""" Read the subject name as DER format """
142150
sub_name_der = crt.subject.public_bytes(default_backend())
143151

144152
name_len = len(sub_name_der)
145153
key_len = len(pub_key_der)
146-
len_data = struct.pack('>HH', name_len, key_len)
154+
len_data = struct.pack(">HH", name_len, key_len)
147155

148156
bundle += len_data
149157
bundle += sub_name_der
@@ -154,23 +162,25 @@ def create_bundle(self):
154162
def add_with_filter(self, crts_path, filter_path):
155163

156164
filter_set = set()
157-
with open(filter_path, 'r', encoding='utf-8') as f:
158-
csv_reader = csv.reader(f, delimiter=',')
165+
with open(filter_path, "r", encoding="utf-8") as f:
166+
csv_reader = csv.reader(f, delimiter=",")
159167

160168
# Skip header
161169
next(csv_reader)
162170
for row in csv_reader:
163171
filter_set.add(row[1])
164172

165-
status('Parsing certificates from %s' % crts_path)
173+
status("Parsing certificates from %s" % crts_path)
166174
crt_str = []
167-
with open(crts_path, 'r', encoding='utf-8') as f:
175+
with open(crts_path, "r", encoding="utf-8") as f:
168176
crt_str = f.read()
169177

170178
# Split all certs into a list of (name, certificate string) tuples
171-
pem_crts = re.findall(r'(^.+?)\n(=+\n[\s\S]+?END CERTIFICATE-----\n)', crt_str, re.MULTILINE)
179+
pem_crts = re.findall(
180+
r"(^.+?)\n(=+\n[\s\S]+?END CERTIFICATE-----\n)", crt_str, re.MULTILINE
181+
)
172182

173-
filtered_crts = ''
183+
filtered_crts = ""
174184
for name, crt in pem_crts:
175185
if name in filter_set:
176186
filtered_crts += crt
@@ -186,13 +196,27 @@ def __init__(self, e):
186196
def main():
187197
global quiet
188198

189-
parser = argparse.ArgumentParser(description='ESP-IDF x509 certificate bundle utility')
190-
191-
parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
192-
parser.add_argument('--input', '-i', nargs='+', required=True,
193-
help='Paths to the custom certificate folders or files to parse, parses all .pem or .der files')
194-
parser.add_argument('--filter', '-f', help='Path to CSV-file where the second columns contains the name of the certificates \
195-
that should be included from cacrt_all.pem')
199+
parser = argparse.ArgumentParser(description="ESP-IDF x509 certificate bundle utility")
200+
201+
parser.add_argument(
202+
"--quiet",
203+
"-q",
204+
help="Don't print non-critical status messages to stderr",
205+
action="store_true",
206+
)
207+
parser.add_argument(
208+
"--input",
209+
"-i",
210+
nargs="+",
211+
required=True,
212+
help="Paths to the custom certificate folders or files to parse, parses all .pem or .der files",
213+
)
214+
parser.add_argument(
215+
"--filter",
216+
"-f",
217+
help="Path to CSV-file where the second columns contains the name of the certificates \
218+
that should be included from cacrt_all.pem",
219+
)
196220

197221
args = parser.parse_args()
198222

@@ -202,24 +226,24 @@ def main():
202226

203227
for path in args.input:
204228
if os.path.isfile(path):
205-
if os.path.basename(path) == 'cacrt_all.pem' and args.filter:
229+
if os.path.basename(path) == "cacrt_all.pem" and args.filter:
206230
bundle.add_with_filter(path, args.filter)
207231
else:
208232
bundle.add_from_file(path)
209233
elif os.path.isdir(path):
210234
bundle.add_from_path(path)
211235
else:
212-
raise InputError('Invalid --input=%s, is neither file nor folder' % args.input)
236+
raise InputError("Invalid --input=%s, is neither file nor folder" % args.input)
213237

214-
status('Successfully added %d certificates in total' % len(bundle.certificates))
238+
status("Successfully added %d certificates in total" % len(bundle.certificates))
215239

216240
crt_bundle = bundle.create_bundle()
217241

218-
with open(ca_bundle_bin_file, 'wb') as f:
242+
with open(ca_bundle_bin_file, "wb") as f:
219243
f.write(crt_bundle)
220244

221245

222-
if __name__ == '__main__':
246+
if __name__ == "__main__":
223247
try:
224248
main()
225249
except InputError as e:

0 commit comments

Comments
 (0)