diff options
| author | J08nY | 2024-02-03 00:11:20 +0100 |
|---|---|---|
| committer | J08nY | 2024-02-03 00:11:20 +0100 |
| commit | 67861ffd16f215f7d476e90bb8e480a5c60dc01b (patch) | |
| tree | c7b82af862e71932742abc645d4206cf0e578f03 | |
| parent | c380870abd04c84da69a9dd438abf22d430568a8 (diff) | |
| download | sec-certs-67861ffd16f215f7d476e90bb8e480a5c60dc01b.tar.gz sec-certs-67861ffd16f215f7d476e90bb8e480a5c60dc01b.tar.zst sec-certs-67861ffd16f215f7d476e90bb8e480a5c60dc01b.zip | |
Improve BSI regexes.
| -rw-r--r-- | src/sec_certs/rules.yaml | 20 | ||||
| -rw-r--r-- | src/sec_certs/sample/cc_certificate_id.py | 61 | ||||
| -rw-r--r-- | tests/cc/test_cc_misc.py | 3 |
3 files changed, 45 insertions, 39 deletions
diff --git a/src/sec_certs/rules.yaml b/src/sec_certs/rules.yaml index 87acd04b..5604b709 100644 --- a/src/sec_certs/rules.yaml +++ b/src/sec_certs/rules.yaml @@ -5,17 +5,17 @@ ##### cc_cert_id: DE: - - "BSI-DSZ-CC-[0-9]+?-[0-9]+" - - "BSI-DSZ-CC-[0-9]+?-(?:V|v)[0-9]+-[0-9]+" - - "BSI-DSZ-CC-[0-9]+?-(?:V|v)[0-9]+" - - "BSI-DSZ-CC-[0-9]+-(?:V|v)[0-9]+(-[0-9]{4})*" # German BSI (number + version + year or without year) - - "BSI-DSZ-CC-[0-9]+-[0-9]{4}" # German BSI (number + year, no version) - - "BSI-DSZ-CC-[0-9]+-(?:V|v)[0-9]+(?!-)" # German BSI (number + version but no year => no - after version) - # - "BSI-DSZ-CC-[0-9]+" # Maybe? + - "BSI-DSZ-CC-(?:(?P<s>S)-)?(?P<counter>[0-9]{3,5})-(?:(?P<version>[vV][0-9])-)?(?P<year>[0-9]{4})?(?:-(?P<doc>(?:RA|MA)(?:-[0-9]+)?))?" + # Examples: + # BSI-DSZ-CC-1004 + # BSI-DSZ-CC-0973-2016 + # BSI-DSZ-CC-0831-V4-2021 + # BSI-DSZ-CC-0837-V2-2014-MA-01 + # BSI-DSZ-CC-S-0192-2021 FR: - "DCSS[Ii]-(?P<year>[0-9]{2,4})/(?P<counter>[0-9]+)([vV](?P<version>[0-9]))?" - - "Certification Report (?P<year>[0-9]{2,4})/(?P<counter>[0-9]+)([vV](?P<version>[0-9]))?" - "Rapport de certification (?P<year>[0-9]{2,4})/(?P<counter>[0-9]+)([vV](?P<version>[0-9]))?" + - "Certification Report (?P<year>[0-9]{2,4})/(?P<counter>[0-9]+)([vV](?P<version>[0-9]))?" - "ANSS[Ii](?:-CC)?[ -](?P<year>[0-9]{2,4})[/_-](?P<counter>[0-9]+)(?:-(?P<doc>(?:[MSR][0-9]+)))?([vV](?P<version>[0-9]))?" # Examples: # DCSSI-2009/07 @@ -30,7 +30,9 @@ cc_cert_id: # CC-16-31801-CR4 (no NSCIB) # NSCIB-CC-98209 (no year, no CR) "NO": - - "SERTIT-(?P<counter>[0-9]+)" # Norway + - "SERTIT-(?P<counter>[0-9]+)" + # Examples: + # SERTIT-101 US: - "CCEVS-VR-(?:CC-|VID)?[0-9]+-[0-9]+[a-z]?(?:-[0-9]+)?" # US NSA (CCEVS-VR-10884-2018 CCEVS-VR-VID10877-2018) CA: diff --git a/src/sec_certs/sample/cc_certificate_id.py b/src/sec_certs/sample/cc_certificate_id.py index 9bce0764..188a2163 100644 --- a/src/sec_certs/sample/cc_certificate_id.py +++ b/src/sec_certs/sample/cc_certificate_id.py @@ -6,6 +6,16 @@ from dataclasses import dataclass from sec_certs.cert_rules import rules +def _parse_year(year: str | None) -> int | None: + if year is None: + return None + y = int(year) + if y < 100: + return y + 1900 + else: + return y + + @dataclass(eq=True, frozen=True) class CertificateId: """ @@ -20,7 +30,7 @@ class CertificateId: for rule in rules["cc_cert_id"]["FR"]: if match := re.match(rule, new_cert_id): groups = match.groupdict() - year = int(groups["year"]) if len(groups["year"]) == 4 else int(groups["year"]) + 2000 + year = _parse_year(groups["year"]) counter = groups["counter"] doc = groups.get("doc") version = groups.get("version") @@ -34,35 +44,26 @@ class CertificateId: return new_cert_id def _canonical_de(self) -> str: - def extract_parts(bsi_parts: list[str]) -> tuple: - cert_num = None - cert_version = None - cert_year = None - - if len(bsi_parts) > 3: - cert_num = bsi_parts[3] - if len(bsi_parts) > 4: - if bsi_parts[4].startswith("V") or bsi_parts[4].startswith("v"): - cert_version = bsi_parts[4].upper() # get version in uppercase - else: - cert_year = bsi_parts[4] - if len(bsi_parts) > 5: - cert_year = bsi_parts[5] - - return cert_num, cert_version, cert_year - - bsi_parts = self.clean.split("-") - - cert_num, cert_version, cert_year = extract_parts(bsi_parts) - - # reconstruct BSI number again - new_cert_id = "BSI-DSZ-CC" - if cert_num is not None: - new_cert_id += "-" + cert_num - if cert_version is not None: - new_cert_id += "-" + cert_version - if cert_year is not None: - new_cert_id += "-" + cert_year + new_cert_id = self.clean + for rule in rules["cc_cert_id"]["DE"]: + if match := re.match(rule, new_cert_id): + groups = match.groupdict() + s = groups.get("s") + counter = groups["counter"] + version = groups.get("version") + year = _parse_year(groups.get("year")) + doc = groups.get("doc") + new_cert_id = "BSI-DSZ-CC" + if s: + new_cert_id += f"-{s}" + new_cert_id += f"-{counter}" + if version: + new_cert_id += f"-{version.upper()}" + if year: + new_cert_id += f"-{year}" + if doc: + new_cert_id += f"-{doc}" + return new_cert_id return new_cert_id diff --git a/tests/cc/test_cc_misc.py b/tests/cc/test_cc_misc.py index ae9a83aa..b9654c91 100644 --- a/tests/cc/test_cc_misc.py +++ b/tests/cc/test_cc_misc.py @@ -8,6 +8,9 @@ def test_canonicalize_fr(): def test_canonicalize_de(): assert canonicalize("BSI-DSZ-CC-0420-2007", "DE") == "BSI-DSZ-CC-0420-2007" + assert canonicalize("BSI-DSZ-CC-1004", "DE") == "BSI-DSZ-CC-1004" + assert canonicalize("BSI-DSZ-CC-0831-V4-2021", "DE") == "BSI-DSZ-CC-0831-V4-2021" + assert canonicalize("BSI-DSZ-CC-0837-V2-2014-MA-01", "DE") == "BSI-DSZ-CC-0837-V2-2014-MA-01" def test_canonicalize_es(): |
