From 4c1521779cb3493f85016ef3f7a1c8e316469a1c Mon Sep 17 00:00:00 2001 From: Sergey S <5818959@gmail.com> Date: Tue, 17 Apr 2018 13:37:11 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B5=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pycryptopro/utils.py | 50 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/pycryptopro/utils.py b/pycryptopro/utils.py index 4bb0ba6..9548c40 100644 --- a/pycryptopro/utils.py +++ b/pycryptopro/utils.py @@ -5,7 +5,10 @@ from datetime import datetime from pycryptopro.exceptions import ( - ShellCommandError, CertificateChainNotChecked, InvalidSignature, CertificatesNotFound + ShellCommandError, + CertificateChainNotChecked, + InvalidSignature, + CertificatesNotFound ) from subprocess import Popen, PIPE @@ -195,6 +198,16 @@ def _parse_response(self, stdout, stderr): raise ShellCommandError(stdout) + def _get_result_code(self, stdout): + if '[ReturnCode: 0]' in stdout: + return 0 + + match = re.search(r'ErrorCode: (.+)]', stdout) + if match: + return match.group(1).lower() + + raise ShellCommandError(stdout) + def _get_exception_class(self, error_code): exception_classes = { '0x20000133': CertificateChainNotChecked, @@ -260,6 +273,41 @@ def verify(self, sgn_dir, cert_filename, filename, errchain=True, norev=False, d signer_data = self._get_signer_data(stdout) return signer_data + def verifyMessage(self, cert_path, file_path, data_path, errchain=True, norev=False, dn=None, returnCode=False): + """ + Проверяет электронную подпись. + + :param cert_path: путь до файла с сертификатом + :param file_path: путь до подписываемого файла + :param data_path: путь до файла в который будут записаны данные + :param errchain: кидать ошибку если не удалось проверить цепочку сертификатов + :param norev: не проверять сертификаты в цепочке на предмет отозванности + :param dn: строки для поиска в RDN + :param returnCode: возвращать код вместо данных о подписанте + """ + + args = [file_path, data_path] + + if errchain: + args.append('-errchain') + else: + args.append('-nochain') + + if norev: + args.append('-norev') + + if dn is not None: + args.append('-dn \'{}\''.format(dn)) + + kwargs = { + 'f': os.path.join(cert_path) + } + + stdout = self.run_command('-verify', *args, **kwargs) + if returnCode: + return self._get_result_code(stdout) + return self._get_signer_data(stdout) + def _get_signer_data(self, stdout): pattern = r'Signer: (.*)' m = re.search(pattern, stdout) From c99a19478b62c17f84da3671e983279fd7233323 Mon Sep 17 00:00:00 2001 From: Sergey S <5818959@gmail.com> Date: Tue, 22 May 2018 11:40:24 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D1=82=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D1=83=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC=D0=BE=D0=B3=D0=BE=20=D1=83=D1=82=D0=B8=D0=BB=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D0=B9=20cryptcp=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pycryptopro/utils.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pycryptopro/utils.py b/pycryptopro/utils.py index 9548c40..04ae5b4 100644 --- a/pycryptopro/utils.py +++ b/pycryptopro/utils.py @@ -185,28 +185,22 @@ class Cryptcp(ShellCommand): def __init__(self, binary='/opt/cprocsp/bin/amd64/cryptcp'): self.binary = binary - def _parse_response(self, stdout, stderr): - if '[ReturnCode: 0]' in stdout: - return stdout - - match = re.search(r'ErrorCode: (.+)]', stdout) + def _get_result_code(self, stdout): + match = re.search(r'\[(ErrorCode|ResultCode): (.+)\]', stdout) if match: - error_code = match.group(1).lower() - exception_class = self._get_exception_class(error_code) - if exception_class: - raise exception_class(stdout) + return match.group(2).lower() raise ShellCommandError(stdout) - def _get_result_code(self, stdout): - if '[ReturnCode: 0]' in stdout: - return 0 + def _parse_response(self, stdout, stderr): + error_code = self._get_result_code(stdout) - match = re.search(r'ErrorCode: (.+)]', stdout) - if match: - return match.group(1).lower() + if '0' == error_code or '0x00000000' == error_code: + return stdout - raise ShellCommandError(stdout) + exception_class = self._get_exception_class(error_code) + if exception_class: + raise exception_class(stdout) def _get_exception_class(self, error_code): exception_classes = {