diff options
| author | Adam Janovsky | 2022-12-10 09:01:55 +0100 |
|---|---|---|
| committer | Adam Janovsky | 2022-12-10 09:01:55 +0100 |
| commit | 53a709e7e16e1ee07e0f4baeb270fcc1d41bb811 (patch) | |
| tree | 1abe1837e9198d165e1bc724190e3488403b3642 /src | |
| parent | 1c43b173667b2aee8ac64d423b1f46d774643eb3 (diff) | |
| download | sec-certs-53a709e7e16e1ee07e0f4baeb270fcc1d41bb811.tar.gz sec-certs-53a709e7e16e1ee07e0f4baeb270fcc1d41bb811.tar.zst sec-certs-53a709e7e16e1ee07e0f4baeb270fcc1d41bb811.zip | |
Rename CommonCriteriaCert -> CCCertificate
Diffstat (limited to 'src')
| -rw-r--r-- | src/sec_certs/__init__.py | 2 | ||||
| -rw-r--r-- | src/sec_certs/dataset/cc.py | 57 | ||||
| -rw-r--r-- | src/sec_certs/dataset/dataset.py | 2 | ||||
| -rw-r--r-- | src/sec_certs/model/evaluation.py | 8 | ||||
| -rw-r--r-- | src/sec_certs/model/sar_transformer.py | 24 | ||||
| -rw-r--r-- | src/sec_certs/sample/__init__.py | 4 | ||||
| -rw-r--r-- | src/sec_certs/sample/cc.py | 122 | ||||
| -rw-r--r-- | src/sec_certs/sample/cc_maintenance_update.py | 12 |
8 files changed, 114 insertions, 117 deletions
diff --git a/src/sec_certs/__init__.py b/src/sec_certs/__init__.py index 816ea2b8..323c53ad 100644 --- a/src/sec_certs/__init__.py +++ b/src/sec_certs/__init__.py @@ -2,6 +2,6 @@ Tool for analysis of security certificates and their security targets (Common Criteria, NIST FIPS140-2...). Contains three main sub-packages: - dataset - package that holds the respective datasets and performs all processing of them -- sample - package that holds a single sample (e.g., Common Criteria certificate - CommonCriteriaCert). Mostly data structure, but can provide basic functionality. +- sample - package that holds a single sample (e.g., Common Criteria certificate - CCCertificate). Mostly data structure, but can provide basic functionality. - model - package that provides data pipelines (transformers, classifiers, ...) for complex transformations of datasets. """ diff --git a/src/sec_certs/dataset/cc.py b/src/sec_certs/dataset/cc.py index 148adbb5..ec661ebe 100644 --- a/src/sec_certs/dataset/cc.py +++ b/src/sec_certs/dataset/cc.py @@ -26,7 +26,7 @@ from sec_certs.dataset.protection_profile import ProtectionProfileDataset from sec_certs.model.reference_finder import ReferenceFinder from sec_certs.model.sar_transformer import SARTransformer from sec_certs.model.transitive_vulnerability_finder import TransitiveVulnerabilityFinder -from sec_certs.sample.cc import CommonCriteriaCert +from sec_certs.sample.cc import CCCertificate from sec_certs.sample.cc_certificate_id import CertificateId from sec_certs.sample.cc_maintenance_update import CommonCriteriaMaintenanceUpdate from sec_certs.sample.protection_profile import ProtectionProfile @@ -44,15 +44,15 @@ class CCAuxillaryDatasets(AuxillaryDatasets): mu_dset: CCDatasetMaintenanceUpdates | None = None -class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSerializableType): +class CCDataset(Dataset[CCCertificate, CCAuxillaryDatasets], ComplexSerializableType): """ - Class that holds CommonCriteriaCert. Serializable into json, pandas, dictionary. Conveys basic certificate manipulations + Class that holds CCCertificate. Serializable into json, pandas, dictionary. Conveys basic certificate manipulations and dataset transformations. Many private methods that perform internal operations, feel free to exploit them. """ def __init__( self, - certs: dict[str, CommonCriteriaCert] = dict(), + certs: dict[str, CCCertificate] = dict(), root_dir: str | Path = constants.DUMMY_NONEXISTING_PATH, name: str | None = None, description: str = "", @@ -76,7 +76,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali """ Return self serialized into pandas DataFrame """ - df = pd.DataFrame([x.pandas_tuple for x in self.certs.values()], columns=CommonCriteriaCert.pandas_columns) + df = pd.DataFrame([x.pandas_tuple for x in self.certs.values()], columns=CCCertificate.pandas_columns) df = df.set_index("dgst") df.not_valid_before = pd.to_datetime(df.not_valid_before, infer_datetime_format=True) @@ -230,7 +230,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali cert.set_local_paths(self.reports_pdf_dir, self.targets_pdf_dir, self.reports_txt_dir, self.targets_txt_dir) # TODO: This forgets to set local paths for other auxillary datasets - def _merge_certs(self, certs: dict[str, CommonCriteriaCert], cert_source: str | None = None) -> None: + def _merge_certs(self, certs: dict[str, CCCertificate], cert_source: str | None = None) -> None: """ Merges dictionary of certificates into the dataset. Assuming they all are CommonCriteria certificates """ @@ -268,7 +268,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali ) -> None: """ Downloads CSV and HTML files that hold lists of certificates from common criteria website. Parses these files - and constructs CommonCriteriaCert objects, fills the dataset with those. + and constructs CCCertificate objects, fills the dataset with those. :param bool to_download: If CSV and HTML files shall be downloaded (or existing files utilized), defaults to True :param bool keep_metadata: If CSV and HTML files shall be kept on disk after download, defaults to True @@ -295,7 +295,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali self._set_local_paths() self.state.meta_sources_parsed = True - def _get_all_certs_from_csv(self, get_active: bool, get_archived: bool) -> dict[str, CommonCriteriaCert]: + def _get_all_certs_from_csv(self, get_active: bool, get_archived: bool) -> dict[str, CCCertificate]: """ Creates dictionary of new certificates from csv sources. """ @@ -311,7 +311,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali return new_certs @staticmethod - def _parse_single_csv(file: Path) -> dict[str, CommonCriteriaCert]: + def _parse_single_csv(file: Path) -> dict[str, CCCertificate]: """ Using pandas, this parses a single CSV file. """ @@ -389,13 +389,13 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali updates: dict[str, set] = {x.dgst: set() for x in df_base.itertuples()} for x in df_main.itertuples(): updates[x.dgst].add( - CommonCriteriaCert.MaintenanceReport( + CCCertificate.MaintenanceReport( x.maintenance_date.date(), x.maintenance_title, x.maintenance_report_link, x.maintenance_st_link ) ) certs = { - x.dgst: CommonCriteriaCert( + x.dgst: CCCertificate( cert_status, x.category, x.cert_name, @@ -418,7 +418,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali } return certs - def _get_all_certs_from_html(self, get_active: bool, get_archived: bool) -> dict[str, CommonCriteriaCert]: + def _get_all_certs_from_html(self, get_active: bool, get_archived: bool) -> dict[str, CCCertificate]: """ Prepares dictionary of certificates from all html files. """ @@ -436,7 +436,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali return new_certs @staticmethod - def _parse_single_html(file: Path) -> dict[str, CommonCriteriaCert]: + def _parse_single_html(file: Path) -> dict[str, CCCertificate]: """ Prepares a dictionary of certificates from a single html file. """ @@ -451,7 +451,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali def _parse_table( soup: BeautifulSoup, cert_status: str, table_id: str, category_string: str - ) -> dict[str, CommonCriteriaCert]: + ) -> dict[str, CCCertificate]: tables = soup.find_all("table", id=table_id) if not len(tables) <= 1: @@ -476,8 +476,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali try: table_certs = { - x.dgst: x - for x in [CommonCriteriaCert.from_html_row(row, cert_status, category_string) for row in body] + x.dgst: x for x in [CCCertificate.from_html_row(row, cert_status, category_string) for row in body] } except ValueError as e: raise ValueError(f"Bad html file: {file.name} ({str(e)})") from e @@ -534,7 +533,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali ) cert_processing.process_parallel( - CommonCriteriaCert.download_pdf_report, + CCCertificate.download_pdf_report, certs_to_process, config.n_threads, progress_bar_desc="Downloading PDFs of CC certification reports", @@ -552,7 +551,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali ) cert_processing.process_parallel( - CommonCriteriaCert.download_pdf_st, + CCCertificate.download_pdf_st, certs_to_process, config.n_threads, progress_bar_desc="Downloading PDFs of CC security targets", @@ -570,7 +569,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali ) cert_processing.process_parallel( - CommonCriteriaCert.convert_report_pdf, + CCCertificate.convert_report_pdf, certs_to_process, config.n_threads, progress_bar_desc="Converting PDFs of certification reports to txt", @@ -588,7 +587,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali ) cert_processing.process_parallel( - CommonCriteriaCert.convert_st_pdf, + CCCertificate.convert_st_pdf, certs_to_process, config.n_threads, progress_bar_desc="Converting PDFs of security targets to txt", @@ -602,7 +601,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting report metadata") certs_to_process = [x for x in self if x.state.report_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_report_pdf_metadata, + CCCertificate.extract_report_pdf_metadata, certs_to_process, config.n_threads, use_threading=False, @@ -614,7 +613,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting target metadata") certs_to_process = [x for x in self if x.state.st_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_st_pdf_metadata, + CCCertificate.extract_st_pdf_metadata, certs_to_process, config.n_threads, use_threading=False, @@ -630,7 +629,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting report frontpages") certs_to_process = [x for x in self if x.state.report_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_report_pdf_frontpage, + CCCertificate.extract_report_pdf_frontpage, certs_to_process, config.n_threads, use_threading=False, @@ -642,7 +641,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting target frontpages") certs_to_process = [x for x in self if x.state.st_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_st_pdf_frontpage, + CCCertificate.extract_st_pdf_frontpage, certs_to_process, config.n_threads, use_threading=False, @@ -658,7 +657,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting report keywords") certs_to_process = [x for x in self if x.state.report_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_report_pdf_keywords, + CCCertificate.extract_report_pdf_keywords, certs_to_process, config.n_threads, use_threading=False, @@ -670,7 +669,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali logger.info("Extracting target keywords") certs_to_process = [x for x in self if x.state.st_is_ok_to_analyze()] processed_certs = cert_processing.process_parallel( - CommonCriteriaCert.extract_st_pdf_keywords, + CCCertificate.extract_st_pdf_keywords, certs_to_process, config.n_threads, use_threading=False, @@ -801,7 +800,7 @@ class CCDataset(Dataset[CommonCriteriaCert, CCAuxillaryDatasets], ComplexSeriali self.mu_dataset_dir.mkdir(parents=True, exist_ok=True) if to_download or not self.mu_dataset_path.exists(): - maintained_certs: list[CommonCriteriaCert] = [x for x in self if x.maintenance_updates] + maintained_certs: list[CCCertificate] = [x for x in self if x.maintenance_updates] updates = list( itertools.chain.from_iterable( CommonCriteriaMaintenanceUpdate.get_updates_from_cc_cert(x) for x in maintained_certs @@ -894,7 +893,7 @@ class CCDatasetMaintenanceUpdates(CCDataset, ComplexSerializableType): def get_n_maintenances_df(self) -> pd.DataFrame: """ - Returns a DataFrame with CommonCriteriaCert digest as an index, and number of registered maintenances as a value + Returns a DataFrame with CCCertificate digest as an index, and number of registered maintenances as a value """ main_df = self.to_pandas() main_df.maintenance_date = main_df.maintenance_date.dt.date @@ -907,7 +906,7 @@ class CCDatasetMaintenanceUpdates(CCDataset, ComplexSerializableType): def get_maintenance_dates_df(self) -> pd.DataFrame: """ - Returns a DataFrame with CommonCriteriaCert digest as an index, and all the maintenance dates as a value. + Returns a DataFrame with CCCertificate digest as an index, and all the maintenance dates as a value. """ main_dates = self.to_pandas() main_dates.maintenance_date = main_dates.maintenance_date.map(lambda x: [x]) diff --git a/src/sec_certs/dataset/dataset.py b/src/sec_certs/dataset/dataset.py index 1c89e771..40ea5611 100644 --- a/src/sec_certs/dataset/dataset.py +++ b/src/sec_certs/dataset/dataset.py @@ -565,7 +565,7 @@ class Dataset(Generic[CertSubType, AuxillaryDatasetsSubType], ComplexSerializabl def update_with_certs(self, certs: list[CertSubType]) -> None: """ Enriches the dataset with `certs` - :param List[CommonCriteriaCert] certs: new certs to include into the dataset. + :param List[Certificate] certs: new certs to include into the dataset. """ if any([x not in self for x in certs]): logger.warning("Updating dataset with certificates outside of the dataset!") diff --git a/src/sec_certs/model/evaluation.py b/src/sec_certs/model/evaluation.py index d464c1b7..4d0243f6 100644 --- a/src/sec_certs/model/evaluation.py +++ b/src/sec_certs/model/evaluation.py @@ -8,7 +8,7 @@ import numpy as np import sec_certs.utils.helpers as helpers from sec_certs.dataset.cpe import CPEDataset -from sec_certs.sample.cc import CommonCriteriaCert +from sec_certs.sample.cc import CCCertificate from sec_certs.sample.fips import FIPSCertificate from sec_certs.serialization.json import CustomJSONEncoder @@ -33,7 +33,7 @@ def compute_precision(y: np.ndarray, y_pred: np.ndarray, **kwargs) -> float: def evaluate( - x_valid: list[CommonCriteriaCert | FIPSCertificate], + x_valid: list[CCCertificate | FIPSCertificate], y_valid: list[set[str] | None], outpath: Path | str | None, cpe_dset: CPEDataset, @@ -56,8 +56,8 @@ def evaluate( predicted_cpes = set() predicted_cpes_dict = {x: cpe_dset[x].title if cpe_dset[x].title else x for x in predicted_cpes} - cert_name = cert.name if isinstance(cert, CommonCriteriaCert) else cert.web_data.module_name - vendor = cert.manufacturer if isinstance(cert, CommonCriteriaCert) else cert.web_data.vendor + cert_name = cert.name if isinstance(cert, CCCertificate) else cert.web_data.module_name + vendor = cert.manufacturer if isinstance(cert, CCCertificate) else cert.web_data.vendor should_be_removed = {x: cpe_dset[x].title if cpe_dset[x].title else x for x in predicted_cpes - verified_cpes} should_be_added = {x: cpe_dset[x].title if cpe_dset[x].title else x for x in verified_cpes - predicted_cpes} diff --git a/src/sec_certs/model/sar_transformer.py b/src/sec_certs/model/sar_transformer.py index 1e05358f..45c4f7d5 100644 --- a/src/sec_certs/model/sar_transformer.py +++ b/src/sec_certs/model/sar_transformer.py @@ -5,7 +5,7 @@ from typing import Dict, Iterable, cast from sklearn.base import BaseEstimator, TransformerMixin -from sec_certs.sample.cc import CommonCriteriaCert +from sec_certs.sample.cc import CCCertificate from sec_certs.sample.sar import SAR, SAR_DICT_KEY logger = logging.getLogger(__name__) @@ -18,39 +18,39 @@ class SARTransformer(BaseEstimator, TransformerMixin): This class implements sklearn transformer interface, so fit_transform() can be called on it. """ - def fit(self, certificates: Iterable[CommonCriteriaCert]) -> SARTransformer: + def fit(self, certificates: Iterable[CCCertificate]) -> SARTransformer: """ Just returns self, no fitting needed - :param Iterable[CommonCriteriaCert] certificates: Unused parameter + :param Iterable[CCCertificate] certificates: Unused parameter :return SARTransformer: return self """ return self - def transform(self, certificates: Iterable[CommonCriteriaCert]) -> list[set[SAR] | None]: + def transform(self, certificates: Iterable[CCCertificate]) -> list[set[SAR] | None]: """ - Just a wrapper around transform_single_cert() called on an iterable of CommonCriteriaCert. + Just a wrapper around transform_single_cert() called on an iterable of CCCertificate. - :param Iterable[CommonCriteriaCert] certificates: Iterable of CommonCriteriaCert objects to perform the extraction on. + :param Iterable[CCCertificate] certificates: Iterable of CCCertificate objects to perform the extraction on. :return List[Optional[Set[SAR]]]: Returns List of results from transform_single_cert(). """ return [self.transform_single_cert(cert) for cert in certificates] - def transform_single_cert(self, cert: CommonCriteriaCert) -> set[SAR] | None: + def transform_single_cert(self, cert: CCCertificate) -> set[SAR] | None: """ - Given CommonCriteriaCert, will transform SAR keywords extracted from txt files + Given CCCertificate, will transform SAR keywords extracted from txt files into a set of SAR objects. Also handles extractin of correct SAR levels, duplicities and filtering. Uses three sources: CSV scan, security target, and certification report. The caller should assure that the certificates have the keywords extracted. - :param CommonCriteriaCert cert: Certificate to extract SARs from + :param CCCertificate cert: Certificate to extract SARs from :return Optional[Set[SAR]]: Set of SARs, None if none were identified. """ sec_level_candidates, st_candidates, report_candidates = self._collect_sar_candidates_from_all_sources(cert) return self._resolve_candidate_conflicts(sec_level_candidates, st_candidates, report_candidates, cert.dgst) @staticmethod - def _collect_sar_candidates_from_all_sources(cert: CommonCriteriaCert) -> tuple[set[SAR], set[SAR], set[SAR]]: + def _collect_sar_candidates_from_all_sources(cert: CCCertificate) -> tuple[set[SAR], set[SAR], set[SAR]]: """ Parses SARs from three distinct sources and returns the results as a three tuple: - Security level from CSV scan @@ -58,10 +58,10 @@ class SARTransformer(BaseEstimator, TransformerMixin): - Keywords from Certification report """ - def st_keywords_may_have_sars(sample: CommonCriteriaCert): + def st_keywords_may_have_sars(sample: CCCertificate): return sample.pdf_data.st_keywords and SAR_DICT_KEY in sample.pdf_data.st_keywords - def report_keywords_may_have_sars(sample: CommonCriteriaCert): + def report_keywords_may_have_sars(sample: CCCertificate): return sample.pdf_data.report_keywords and SAR_DICT_KEY in sample.pdf_data.report_keywords sec_level_sars = SARTransformer._parse_sars_from_security_level_list(cert.security_level) diff --git a/src/sec_certs/sample/__init__.py b/src/sec_certs/sample/__init__.py index e86c415e..f5f2130c 100644 --- a/src/sec_certs/sample/__init__.py +++ b/src/sec_certs/sample/__init__.py @@ -2,7 +2,7 @@ like CPE, CVE, etc. The objects mostly hold data and allow for serialization, but can also perform some basic transformations. """ -from sec_certs.sample.cc import CommonCriteriaCert +from sec_certs.sample.cc import CCCertificate from sec_certs.sample.cc_certificate_id import CertificateId from sec_certs.sample.cc_maintenance_update import CommonCriteriaMaintenanceUpdate from sec_certs.sample.cpe import CPE, cached_cpe @@ -17,7 +17,7 @@ from sec_certs.sample.sar import SAR __all__ = [ "CertificateId", "CommonCriteriaMaintenanceUpdate", - "CommonCriteriaCert", + "CCCertificate", "CPE", "cached_cpe", "CVE", diff --git a/src/sec_certs/sample/cc.py b/src/sec_certs/sample/cc.py index acd54178..603c29f2 100644 --- a/src/sec_certs/sample/cc.py +++ b/src/sec_certs/sample/cc.py @@ -45,8 +45,8 @@ class ReferenceType(Enum): INDIRECT = "indirect" -class CommonCriteriaCert( - Certificate["CommonCriteriaCert", "CommonCriteriaCert.Heuristics", "CommonCriteriaCert.PdfData"], +class CCCertificate( + Certificate["CCCertificate", "CCCertificate.Heuristics", "CCCertificate.PdfData"], PandasSerializableType, ComplexSerializableType, ): @@ -84,7 +84,7 @@ class CommonCriteriaCert( super().__setattr__("maintenance_date", sec_certs.utils.sanitization.sanitize_date(self.maintenance_date)) @classmethod - def from_dict(cls, dct: dict) -> CommonCriteriaCert.MaintenanceReport: + def from_dict(cls, dct: dict) -> CCCertificate.MaintenanceReport: new_dct = dct.copy() new_dct["maintenance_date"] = ( date.fromisoformat(dct["maintenance_date"]) @@ -384,7 +384,7 @@ class CommonCriteriaCert( @dataclass class Heuristics(BaseHeuristics, ComplexSerializableType): """ - Class for various heuristics related to CommonCriteriaCert + Class for various heuristics related to CCCertificate """ extracted_versions: set[str] | None = field(default=None) @@ -474,7 +474,7 @@ class CommonCriteriaCert( self.maintenance_updates = maintenance_updates self.state = self.InternalState() if not state else state self.pdf_data = self.PdfData() if not pdf_data else pdf_data - self.heuristics: CommonCriteriaCert.Heuristics = self.Heuristics() if not heuristics else heuristics + self.heuristics: CCCertificate.Heuristics = self.Heuristics() if not heuristics else heuristics @property def dgst(self) -> str: @@ -560,7 +560,7 @@ class CommonCriteriaCert( printed_manufacturer = self.manufacturer if self.manufacturer else "Unknown manufacturer" return str(printed_manufacturer) + " " + str(self.name) + " dgst: " + self.dgst - def merge(self, other: CommonCriteriaCert, other_source: str | None = None) -> None: + def merge(self, other: CCCertificate, other_source: str | None = None) -> None: """ Merges with other CC sample. Assuming they come from different sources, e.g., csv and html. Assuming that html source has better protection profiles, they overwrite CSV info @@ -587,9 +587,9 @@ class CommonCriteriaCert( ) @classmethod - def from_dict(cls, dct: dict) -> CommonCriteriaCert: + def from_dict(cls, dct: dict) -> CCCertificate: """ - Deserializes dictionary into `CommonCriteriaCert` + Deserializes dictionary into `CCCertificate` """ new_dct = dct.copy() new_dct["maintenance_updates"] = set(dct["maintenance_updates"]) @@ -604,7 +604,7 @@ class CommonCriteriaCert( if isinstance(dct["not_valid_after"], str) else dct["not_valid_after"] ) - return super(cls, CommonCriteriaCert).from_dict(new_dct) + return super(cls, CCCertificate).from_dict(new_dct) @staticmethod def _html_row_get_name(cell: Tag) -> str: @@ -639,7 +639,7 @@ class CommonCriteriaCert( if link.get("href") is not None and "/ppfiles/" in link.get("href"): protection_profiles.add( ProtectionProfile( - pp_name=str(link.contents[0]), pp_eal=None, pp_link=CommonCriteriaCert.cc_url + link.get("href") + pp_name=str(link.contents[0]), pp_eal=None, pp_link=CCCertificate.cc_url + link.get("href") ) ) return protection_profiles @@ -656,15 +656,15 @@ class CommonCriteriaCert( assert links[1].get("title").startswith("Certification Report") assert links[2].get("title").startswith("Security Target") - report_link = CommonCriteriaCert.cc_url + links[1].get("href") - security_target_link = CommonCriteriaCert.cc_url + links[2].get("href") + report_link = CCCertificate.cc_url + links[1].get("href") + security_target_link = CCCertificate.cc_url + links[2].get("href") return report_link, security_target_link @staticmethod def _html_row_get_cert_link(cell: Tag) -> str | None: links = cell.find_all("a") - return CommonCriteriaCert.cc_url + links[0].get("href") if links else None + return CCCertificate.cc_url + links[0].get("href") if links else None @staticmethod def _html_row_get_maintenance_div(cell: Tag) -> Tag | None: @@ -675,7 +675,7 @@ class CommonCriteriaCert( return None @staticmethod - def _html_row_get_maintenance_updates(main_div: Tag) -> set[CommonCriteriaCert.MaintenanceReport]: + def _html_row_get_maintenance_updates(main_div: Tag) -> set[CCCertificate.MaintenanceReport]: possible_updates = list(main_div.find_all("li")) maintenance_updates = set() for u in possible_updates: @@ -687,18 +687,18 @@ class CommonCriteriaCert( links = u.find_all("a") for link in links: if link.get("title").startswith("Maintenance Report:"): - main_report_link = CommonCriteriaCert.cc_url + link.get("href") + main_report_link = CCCertificate.cc_url + link.get("href") elif link.get("title").startswith("Maintenance ST"): - main_st_link = CommonCriteriaCert.cc_url + link.get("href") + main_st_link = CCCertificate.cc_url + link.get("href") else: logger.error("Unknown link in Maintenance part!") maintenance_updates.add( - CommonCriteriaCert.MaintenanceReport(main_date, main_title, main_report_link, main_st_link) + CCCertificate.MaintenanceReport(main_date, main_title, main_report_link, main_st_link) ) return maintenance_updates @classmethod - def from_html_row(cls, row: Tag, status: str, category: str) -> CommonCriteriaCert: + def from_html_row(cls, row: Tag, status: str, category: str) -> CCCertificate: """ Creates a CC sample from html row of commoncriteria.org webpage. """ @@ -707,20 +707,18 @@ class CommonCriteriaCert( if len(cells) != 7: raise ValueError(f"Unexpected number of <td> elements in CC html row. Expected: 7, actual: {len(cells)}") - name = CommonCriteriaCert._html_row_get_name(cells[0]) - manufacturer = CommonCriteriaCert._html_row_get_manufacturer(cells[1]) - manufacturer_web = CommonCriteriaCert._html_row_get_manufacturer_web(cells[1]) - scheme = CommonCriteriaCert._html_row_get_scheme(cells[6]) - security_level = CommonCriteriaCert._html_row_get_security_level(cells[5]) - protection_profiles = CommonCriteriaCert._html_row_get_protection_profiles(cells[0]) - not_valid_before = CommonCriteriaCert._html_row_get_date(cells[3]) - not_valid_after = CommonCriteriaCert._html_row_get_date(cells[4]) - report_link, st_link = CommonCriteriaCert._html_row_get_report_st_links(cells[0]) - cert_link = CommonCriteriaCert._html_row_get_cert_link(cells[2]) - maintenance_div = CommonCriteriaCert._html_row_get_maintenance_div(cells[0]) - maintenances = ( - CommonCriteriaCert._html_row_get_maintenance_updates(maintenance_div) if maintenance_div else set() - ) + name = CCCertificate._html_row_get_name(cells[0]) + manufacturer = CCCertificate._html_row_get_manufacturer(cells[1]) + manufacturer_web = CCCertificate._html_row_get_manufacturer_web(cells[1]) + scheme = CCCertificate._html_row_get_scheme(cells[6]) + security_level = CCCertificate._html_row_get_security_level(cells[5]) + protection_profiles = CCCertificate._html_row_get_protection_profiles(cells[0]) + not_valid_before = CCCertificate._html_row_get_date(cells[3]) + not_valid_after = CCCertificate._html_row_get_date(cells[4]) + report_link, st_link = CCCertificate._html_row_get_report_st_links(cells[0]) + cert_link = CCCertificate._html_row_get_cert_link(cells[2]) + maintenance_div = CCCertificate._html_row_get_maintenance_div(cells[0]) + maintenances = CCCertificate._html_row_get_maintenance_updates(maintenance_div) if maintenance_div else set() return cls( status, @@ -767,12 +765,12 @@ class CommonCriteriaCert( self.state.st_txt_path = Path(st_txt_dir) / (self.dgst + ".txt") @staticmethod - def download_pdf_report(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def download_pdf_report(cert: CCCertificate) -> CCCertificate: """ Downloads pdf of certification report given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to download the pdf report for - :return CommonCriteriaCert: returns the modified certificate with updated state + :param CCCertificate cert: cert to download the pdf report for + :return CCCertificate: returns the modified certificate with updated state """ exit_code: str | int if not cert.report_link: @@ -790,12 +788,12 @@ class CommonCriteriaCert( return cert @staticmethod - def download_pdf_st(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def download_pdf_st(cert: CCCertificate) -> CCCertificate: """ Downloads pdf of security target given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to download the pdf security target for - :return CommonCriteriaCert: returns the modified certificate with updated state + :param CCCertificate cert: cert to download the pdf security target for + :return CCCertificate: returns the modified certificate with updated state """ exit_code: str | int if not cert.st_link: @@ -813,12 +811,12 @@ class CommonCriteriaCert( return cert @staticmethod - def convert_report_pdf(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def convert_report_pdf(cert: CCCertificate) -> CCCertificate: """ Converts the pdf certification report to txt, given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to download the pdf report for - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to download the pdf report for + :return CCCertificate: the modified certificate with updated state """ ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file( cert.state.report_pdf_path, cert.state.report_txt_path @@ -835,12 +833,12 @@ class CommonCriteriaCert( return cert @staticmethod - def convert_st_pdf(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def convert_st_pdf(cert: CCCertificate) -> CCCertificate: """ Converts the pdf security target to txt, given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to download the pdf security target for - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to download the pdf security target for + :return CCCertificate: the modified certificate with updated state """ ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(cert.state.st_pdf_path, cert.state.st_txt_path) # If OCR was done the result was garbage @@ -855,12 +853,12 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_st_pdf_metadata(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_st_pdf_metadata(cert: CCCertificate) -> CCCertificate: """ Extracts metadata from security target pdf given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to extract the metadata for. - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to extract the metadata for. + :return CCCertificate: the modified certificate with updated state """ response, cert.pdf_data.st_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.st_pdf_path) if response != constants.RETURNCODE_OK: @@ -870,12 +868,12 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_report_pdf_metadata(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_report_pdf_metadata(cert: CCCertificate) -> CCCertificate: """ Extracts metadata from certification report pdf given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to extract the metadata for. - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to extract the metadata for. + :return CCCertificate: the modified certificate with updated state """ response, cert.pdf_data.report_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.report_pdf_path) if response != constants.RETURNCODE_OK: @@ -885,12 +883,12 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_st_pdf_frontpage(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_st_pdf_frontpage(cert: CCCertificate) -> CCCertificate: """ Extracts data from security target pdf frontpage given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to extract the frontpage data for. - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to extract the frontpage data for. + :return CCCertificate: the modified certificate with updated state """ cert.pdf_data.st_frontpage = {} @@ -902,12 +900,12 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_report_pdf_frontpage(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_report_pdf_frontpage(cert: CCCertificate) -> CCCertificate: """ Extracts data from certification report pdf frontpage given the certificate. Staticmethod to allow for parallelization. - :param CommonCriteriaCert cert: cert to extract the frontpage data for. - :return CommonCriteriaCert: the modified certificate with updated state + :param CCCertificate cert: cert to extract the frontpage data for. + :return CCCertificate: the modified certificate with updated state """ cert.pdf_data.report_frontpage = {} @@ -919,13 +917,13 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_report_pdf_keywords(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_report_pdf_keywords(cert: CCCertificate) -> CCCertificate: """ Matches regular expresions in txt obtained from certification report and extracts the matches into attribute. Static method to allow for parallelization - :param CommonCriteriaCert cert: certificate to extract the keywords for. - :return CommonCriteriaCert: the modified certificate with extracted keywords. + :param CCCertificate cert: certificate to extract the keywords for. + :return CCCertificate: the modified certificate with extracted keywords. """ report_keywords = sec_certs.utils.extract.extract_keywords(cert.state.report_txt_path, cc_rules) if report_keywords is None: @@ -935,13 +933,13 @@ class CommonCriteriaCert( return cert @staticmethod - def extract_st_pdf_keywords(cert: CommonCriteriaCert) -> CommonCriteriaCert: + def extract_st_pdf_keywords(cert: CCCertificate) -> CCCertificate: """ Matches regular expresions in txt obtained from security target and extracts the matches into attribute. Static method to allow for parallelization - :param CommonCriteriaCert cert: certificate to extract the keywords for. - :return CommonCriteriaCert: the modified certificate with extracted keywords. + :param CCCertificate cert: certificate to extract the keywords for. + :return CCCertificate: the modified certificate with extracted keywords. """ st_keywords = sec_certs.utils.extract.extract_keywords(cert.state.st_txt_path, cc_rules) if st_keywords is None: diff --git a/src/sec_certs/sample/cc_maintenance_update.py b/src/sec_certs/sample/cc_maintenance_update.py index 2cab2830..5399dde1 100644 --- a/src/sec_certs/sample/cc_maintenance_update.py +++ b/src/sec_certs/sample/cc_maintenance_update.py @@ -5,13 +5,13 @@ from datetime import date from typing import ClassVar import sec_certs.utils.helpers as helpers -from sec_certs.sample.cc import CommonCriteriaCert +from sec_certs.sample.cc import CCCertificate from sec_certs.serialization.json import ComplexSerializableType logger = logging.getLogger(__name__) -class CommonCriteriaMaintenanceUpdate(CommonCriteriaCert, ComplexSerializableType): +class CommonCriteriaMaintenanceUpdate(CCCertificate, ComplexSerializableType): pandas_columns: ClassVar[list[str]] = [ "dgst", "name", @@ -26,9 +26,9 @@ class CommonCriteriaMaintenanceUpdate(CommonCriteriaCert, ComplexSerializableTyp name: str, report_link: str, st_link: str, - state: CommonCriteriaCert.InternalState | None, - pdf_data: CommonCriteriaCert.PdfData | None, - heuristics: CommonCriteriaCert.Heuristics | None, + state: CCCertificate.InternalState | None, + pdf_data: CCCertificate.PdfData | None, + heuristics: CCCertificate.Heuristics | None, related_cert_digest: str, maintenance_date: date, ): @@ -74,7 +74,7 @@ class CommonCriteriaMaintenanceUpdate(CommonCriteriaCert, ComplexSerializableTyp return cls(*(tuple(dct.values()))) @classmethod - def get_updates_from_cc_cert(cls, cert: CommonCriteriaCert) -> list[CommonCriteriaMaintenanceUpdate]: + def get_updates_from_cc_cert(cls, cert: CCCertificate) -> list[CommonCriteriaMaintenanceUpdate]: if cert.maintenance_updates is None: raise RuntimeError("Dataset was probably not built correctly - this should not be happening.") |
