Skip to content

Commit

Permalink
Fixes issues with gen_crt_bundle.py for unicode chars
Browse files Browse the repository at this point in the history
gen_crt_bundle.py could fail to parse the certificates if it contained a non-ascii character.
  • Loading branch information
ESP-Marius committed Jun 8, 2020
1 parent 58e1100 commit c343323
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions components/mbedtls/esp_crt_bundle/gen_crt_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import argparse
import csv
import re
from io import open

try:
from cryptography import x509
Expand Down Expand Up @@ -80,7 +81,7 @@ def add_from_file(self, file_path):
try:
if file_path.endswith('.pem'):
status("Parsing certificates from %s" % file_path)
with open(file_path, 'r') as f:
with open(file_path, 'r', encoding='utf-8') as f:
crt_str = f.read()
self.add_from_pem(crt_str)
return True
Expand Down Expand Up @@ -153,7 +154,7 @@ def create_bundle(self):
def add_with_filter(self, crts_path, filter_path):

filter_set = set()
with open(filter_path, 'r') as f:
with open(filter_path, 'r', encoding='utf-8') as f:
csv_reader = csv.reader(f, delimiter=',')

# Skip header
Expand All @@ -163,7 +164,7 @@ def add_with_filter(self, crts_path, filter_path):

status("Parsing certificates from %s" % crts_path)
crt_str = []
with open(crts_path, 'r') as f:
with open(crts_path, 'r', encoding='utf-8') as f:
crt_str = f.read()

# Split all certs into a list of (name, certificate string) tuples
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
NetLock Arany (Class Gold) Főtanúsítvány
========================================
-----BEGIN CERTIFICATE-----
MIIEFTCCAv2gAwIBAgIGSUEs5AAQMA0GCSqGSIb3DQEBCwUAMIGnMQswCQYDVQQGEwJIVTERMA8G
A1UEBwwIQnVkYXBlc3QxFTATBgNVBAoMDE5ldExvY2sgS2Z0LjE3MDUGA1UECwwuVGFuw7pzw610
dsOhbnlraWFkw7NrIChDZXJ0aWZpY2F0aW9uIFNlcnZpY2VzKTE1MDMGA1UEAwwsTmV0TG9jayBB
cmFueSAoQ2xhc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uLjbvrW5Kfna
NwUASZQDhETnv0Mxz3WLJdH0pmT1kvarBes96aULNmLazAZfNou2XjG4Kvte9nHfRCaexOYNkbQu
dZWAUWpLMKawYqGT8ZvYzsRjdT9ZR7E=
-----END CERTIFICATE-----
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
verified_der_bundle = 'baltimore_crt_bundle'
verified_pem_bundle = 'entrust_crt_bundle'
invalid_test_file = 'invalid_crt.pem'
non_ascii_file = 'non_ascii_crt.pem'
ca_crts_all_file = 'cacrt_all.pem'


Expand Down Expand Up @@ -72,6 +73,12 @@ def test_invalid_crt_input(self):
with self.assertRaisesRegex(gen_crt_bundle.InputError, "No certificate found"):
bundle.add_from_pem("")

def test_non_ascii_crt_input(self):
bundle = gen_crt_bundle.CertificateBundle()

bundle.add_from_file(test_crts_path + non_ascii_file)
self.assertTrue(len(bundle.certificates))


if __name__ == "__main__":
unittest.main()

0 comments on commit c343323

Please sign in to comment.