diff options
| author | J08nY | 2024-02-05 18:12:34 +0100 |
|---|---|---|
| committer | J08nY | 2024-02-05 18:12:34 +0100 |
| commit | 4e24bb278c9da4e4f6c75f92187c6173d4269010 (patch) | |
| tree | 27f315d5c5f26d0a9b568bf35c554e25bac4d08c /src | |
| parent | 836139707bc2a826a1ff1c900c6e92854cb3d8bb (diff) | |
| download | sec-certs-4e24bb278c9da4e4f6c75f92187c6173d4269010.tar.gz sec-certs-4e24bb278c9da4e4f6c75f92187c6173d4269010.tar.zst sec-certs-4e24bb278c9da4e4f6c75f92187c6173d4269010.zip | |
Extract and reconstruct cert ids from filenames.
Before this, the regular cert_id regexes were used to extract
the cert_id from the report filename. However, the filenames often
do not use the same cert_id format, but contain all of the information
necessary to reconstruct the cert_id, but with different order for example.
This commit along with those before it introduce a new set of regular
expressions that better match the ones in the filenames. To extract
the correctly formatted canonical cert_id, the regexes are used to
obtain the parts of the cert_id (using named groups in regexes) and
those are then reconstructed into a canonical version of the cert_id
via one of the scheme-dependent functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/sec_certs/sample/cc.py | 14 | ||||
| -rw-r--r-- | src/sec_certs/sample/cc_certificate_id.py | 42 |
2 files changed, 31 insertions, 25 deletions
diff --git a/src/sec_certs/sample/cc.py b/src/sec_certs/sample/cc.py index 6ee2bd1b..8948bb76 100644 --- a/src/sec_certs/sample/cc.py +++ b/src/sec_certs/sample/cc.py @@ -19,7 +19,7 @@ import sec_certs.utils.pdf import sec_certs.utils.sanitization from sec_certs import constants from sec_certs.cert_rules import SARS_IMPLIED_FROM_EAL, cc_rules, rules, security_level_csv_scan -from sec_certs.sample.cc_certificate_id import canonicalize +from sec_certs.sample.cc_certificate_id import canonicalize, schemes from sec_certs.sample.certificate import Certificate, References, logger from sec_certs.sample.certificate import Heuristics as BaseHeuristics from sec_certs.sample.certificate import PdfData as BasePdfData @@ -337,13 +337,17 @@ class CCCertificate( """ if not self.report_filename: return {} - scheme_rules = rules["cc_cert_id"][scheme] + scheme_filename_rules = rules["cc_filename_cert_id"][scheme] + scheme_meta = schemes[scheme] matches: Counter = Counter() - for rule in scheme_rules: + for rule in scheme_filename_rules: match = re.search(rule, self.report_filename) if match: - cert_id = normalize_match_string(match.group()) - matches[cert_id] += 1 + try: + cert_id = scheme_meta(match.groupdict()) + matches[cert_id] += 1 + except Exception: + continue if not matches: return {} total = max(matches.values()) diff --git a/src/sec_certs/sample/cc_certificate_id.py b/src/sec_certs/sample/cc_certificate_id.py index ee715082..1fb7b046 100644 --- a/src/sec_certs/sample/cc_certificate_id.py +++ b/src/sec_certs/sample/cc_certificate_id.py @@ -182,6 +182,28 @@ def IT(meta) -> str: return cert_id +# We have rules for some schemes to make canonical cert_ids. +schemes = { + "FR": FR, + "DE": DE, + "US": US, + "MY": MY, + "ES": ES, + "IN": IN, + "SE": SE, + "UK": UK, + "CA": CA, + "JP": JP, + "NO": NO, + "NL": NL, + "AU": AU, + "KR": KR, + "TR": TR, + "SG": SG, + "IT": IT, +} + + @dataclass(frozen=True) class CertificateId: """ @@ -210,26 +232,6 @@ class CertificateId: """ The canonical version of this certificate id. """ - # We have rules for some schemes to make canonical cert_ids. - schemes = { - "FR": FR, - "DE": DE, - "US": US, - "MY": MY, - "ES": ES, - "IN": IN, - "SE": SE, - "UK": UK, - "CA": CA, - "JP": JP, - "NO": NO, - "NL": NL, - "AU": AU, - "KR": KR, - "TR": TR, - "SG": SG, - "IT": IT, - } clean = self.clean if self.scheme in schemes: |
