aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2022-10-03 19:53:04 +0200
committerJ08nY2022-10-04 14:41:18 +0200
commit78d961a8ec2bfc3fa134918672d8100beaf18d05 (patch)
treec6d0f599b446867aed4c51f7f371299a535e5362
parente2829a09bb667b855997e8e8966fc8e4f17a22dd (diff)
downloadsec-certs-78d961a8ec2bfc3fa134918672d8100beaf18d05.tar.gz
sec-certs-78d961a8ec2bfc3fa134918672d8100beaf18d05.tar.zst
sec-certs-78d961a8ec2bfc3fa134918672d8100beaf18d05.zip
Store OCR results into CC cert state.
-rw-r--r--sec_certs/sample/common_criteria.py22
-rw-r--r--sec_certs/sample/fips.py4
-rw-r--r--sec_certs/utils/pdf.py19
-rw-r--r--tests/test_cc_oop.py5
4 files changed, 32 insertions, 18 deletions
diff --git a/sec_certs/sample/common_criteria.py b/sec_certs/sample/common_criteria.py
index a9298eab..b3ccb8db 100644
--- a/sec_certs/sample/common_criteria.py
+++ b/sec_certs/sample/common_criteria.py
@@ -843,14 +843,18 @@ class CommonCriteriaCert(
:param CommonCriteriaCert cert: cert to download the pdf report for
:return CommonCriteriaCert: the modified certificate with updated state
"""
- exit_code = sec_certs.utils.pdf.convert_pdf_file(cert.state.report_pdf_path, cert.state.report_txt_path)
- if exit_code != constants.RETURNCODE_OK:
+ ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(
+ cert.state.report_pdf_path, cert.state.report_txt_path
+ )
+ # If OCR was done the result was garbage
+ cert.state.report_convert_garbage = ocr_done
+ # And put the whole result into convert_ok
+ cert.state.report_convert_ok = ok_result
+ if not ok_result:
error_msg = "failed to convert report pdf->txt"
logger.error(f"Cert dgst: {cert.dgst} " + error_msg)
- cert.state.report_convert_ok = False
cert.state.errors.append(error_msg)
else:
- cert.state.report_convert_ok = True
cert.state.report_txt_hash = helpers.get_sha256_filepath(cert.state.report_txt_path)
return cert
@@ -862,14 +866,16 @@ class CommonCriteriaCert(
:param CommonCriteriaCert cert: cert to download the pdf security target for
:return CommonCriteriaCert: the modified certificate with updated state
"""
- exit_code = sec_certs.utils.pdf.convert_pdf_file(cert.state.st_pdf_path, cert.state.st_txt_path)
- if exit_code != constants.RETURNCODE_OK:
+ 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
+ cert.state.st_convert_garbage = ocr_done
+ # And put the whole result into convert_ok
+ cert.state.st_convert_ok = ok_result
+ if not ok_result:
error_msg = "failed to convert security target pdf->txt"
logger.error(f"Cert dgst: {cert.dgst} " + error_msg)
- cert.state.st_convert_ok = False
cert.state.errors.append(error_msg)
else:
- cert.state.st_convert_ok = True
cert.state.st_txt_hash = helpers.get_sha256_filepath(cert.state.st_txt_path)
return cert
diff --git a/sec_certs/sample/fips.py b/sec_certs/sample/fips.py
index cf45aafb..eb8308f5 100644
--- a/sec_certs/sample/fips.py
+++ b/sec_certs/sample/fips.py
@@ -559,8 +559,8 @@ class FIPSCertificate(Certificate["FIPSCertificate", "FIPSCertificate.Heuristics
"""
cert, pdf_path, txt_path = tup
if not cert.state.txt_state:
- exit_code = sec_certs.utils.pdf.convert_pdf_file(pdf_path, txt_path)
- if exit_code != constants.RETURNCODE_OK:
+ ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(pdf_path, txt_path)
+ if not ok_result:
logger.error(f"Cert dgst: {cert.cert_id} failed to convert security policy pdf->txt")
cert.state.txt_state = False
else:
diff --git a/sec_certs/utils/pdf.py b/sec_certs/utils/pdf.py
index 08de2cc7..a40fb18d 100644
--- a/sec_certs/utils/pdf.py
+++ b/sec_certs/utils/pdf.py
@@ -64,15 +64,18 @@ def ocr_pdf_file(pdf_path: Path) -> str:
return contents
-def convert_pdf_file(pdf_path: Path, txt_path: Path) -> str:
+def convert_pdf_file(pdf_path: Path, txt_path: Path) -> Tuple[bool, bool]:
"""
Convert a PDF tile to text and save it on the `txt_path`.
:param pdf_path: Path to the to-be-converted PDF file.
:param txt_path: Path to the resulting text file.
- :return: Whether the conversion was successful (see constants).
+ :return: A tuple of two results, whether OCR was done and what the complete result
+ was (OK/NOK).
"""
txt = None
+ ok = False
+ ocr = False
try:
with pdf_path.open("rb") as pdf_handle:
pdf = pdftotext.PDF(pdf_handle, "", True) # No password, Raw=True
@@ -82,19 +85,19 @@ def convert_pdf_file(pdf_path: Path, txt_path: Path) -> str:
if txt is None or text_is_garbage(txt):
logger.warning(f"Detected garbage during conversion of {pdf_path}")
+ ocr = True
try:
txt = ocr_pdf_file(pdf_path)
logger.info(f"OCR OK for {pdf_path}")
except Exception as e:
logger.error(f"Error during OCR of {pdf_path}, using garbage: {e}")
- if txt is None:
- return constants.RETURNCODE_NOK
+ if txt is not None:
+ ok = True
+ with txt_path.open("w", encoding="utf-8") as txt_handle:
+ txt_handle.write(txt)
- with txt_path.open("w", encoding="utf-8") as txt_handle:
- txt_handle.write(txt)
-
- return constants.RETURNCODE_OK
+ return ocr, ok
def parse_pdf_date(dateval: Optional[bytes]) -> Optional[datetime]:
diff --git a/tests/test_cc_oop.py b/tests/test_cc_oop.py
index 201b3db3..0777a1e1 100644
--- a/tests/test_cc_oop.py
+++ b/tests/test_cc_oop.py
@@ -142,6 +142,11 @@ class TestCommonCriteriaOOP(TestCase):
f"Hash of PDF security target for CommonCriteriaCert with digest {cert.dgst} deviates from template.",
)
+ self.assertFalse(dset["309ac2fd7f2dcf17"].state.report_convert_garbage)
+ self.assertFalse(dset["309ac2fd7f2dcf17"].state.st_convert_garbage)
+ self.assertTrue(dset["309ac2fd7f2dcf17"].state.report_convert_ok)
+ self.assertTrue(dset["309ac2fd7f2dcf17"].state.st_convert_ok)
+
self.assertTrue(dset["309ac2fd7f2dcf17"].state.report_txt_path.exists())
self.assertTrue(dset["309ac2fd7f2dcf17"].state.st_txt_path.exists())