Skip to content

Commit

Permalink
Merge pull request #2 from iZucken/master
Browse files Browse the repository at this point in the history
Добавлены сообщения к некоторым кодам ошибок;
  • Loading branch information
nikserg authored Aug 21, 2020
2 parents 5b0b924 + 037e6c5 commit 3f4026d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
29 changes: 22 additions & 7 deletions CryptoProCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace nikserg\cryptoprocli;

use nikserg\cryptoprocli\Exception\Cli;
use nikserg\cryptoprocli\Exception\SignatureError;

/**
* Class CryptoProCli
Expand Down Expand Up @@ -33,7 +34,7 @@ private static function getCryptcpExec()
* @param string $file
* @param string $thumbprint
* @param null $toFile
* @throws \Exception
* @throws Cli
*/
public static function signFile($file, $thumbprint, $toFile = null)
{
Expand All @@ -43,7 +44,7 @@ public static function signFile($file, $thumbprint, $toFile = null)

if (strpos($result, "Signed message is created.") <= 0 && strpos($result,
"Подписанное сообщение успешно создано") <= 0) {
throw new \Exception('В ответе Cryptcp не найдена строка "Signed message is created" или "Подписанное сообщение успешно создано": ' . $result . ' команда ' . $shellCommand);
throw new Cli('В ответе Cryptcp не найдена строка "Signed message is created" или "Подписанное сообщение успешно создано": ' . $result . ' команда ' . $shellCommand);
}
}

Expand All @@ -54,6 +55,7 @@ public static function signFile($file, $thumbprint, $toFile = null)
* @param $data
* @param $thumbprint
* @return bool|string
* @throws Cli
*/
public static function signData($data, $thumbprint)
{
Expand All @@ -73,16 +75,15 @@ public static function signData($data, $thumbprint)
*
* @param string $file Путь к файлу
* @param string $thumbprint SHA1 отпечаток, например, bb959544444d8d9e13ca3b8801d5f7a52f91fe97
* @throws \Exception
* @throws Cli
*/
public static function addSignToFile($file, $thumbprint)
{
$shellCommand = self::getCryptcpExec() .
' -addsign -thumbprint ' . $thumbprint . ' ' . $file;
$result = shell_exec($shellCommand);

if (strpos($result, "Signed message is created.") <= 0) {
throw new \Exception('В ответе Cryptcp не найдена строка Signed message is created: ' . $result . ' команда ' . $shellCommand);
throw new Cli('В ответе Cryptcp не найдена строка Signed message is created: ' . $result . ' команда ' . $shellCommand);
}
}

Expand All @@ -91,6 +92,8 @@ public static function addSignToFile($file, $thumbprint)
*
*
* @param $fileContent
* @throws Cli
* @throws SignatureError
*/
public static function verifyFileContent($fileContent)
{
Expand All @@ -111,20 +114,32 @@ private static function getDevNull()
return '/dev/null';
}

const ERROR_CODE_MESSAGE = [
'0x20000133' => 'Цепочка сертификатов не проверена',
'0x200001f9' => 'Подпись не верна',
'0x2000012d' => 'Сетификаты не найдены',
'0x2000012e' => 'Более одного сертификата',
];

/**
* Проверить, что файл подписан правильной подписью
*
*
* @param $file
* @throws Cli
* @throws SignatureError
*/
public static function verifyFile($file)
{
$shellCommand = 'yes "n" 2> '.self::getDevNull().' | ' . escapeshellarg(self::$cryptcpExec) . ' -verify -verall ' . escapeshellarg($file);
$result = shell_exec($shellCommand);
if (strpos($result, "[ErrorCode: 0x00000000]") === false && strpos($result, "[ReturnCode: 0]") === false) {
//Проверка неуспешна
throw new Cli('В ответе Cryptcp не найдена строка [ErrorCode: 0x00000000] и [ReturnCode: 0]: ' . $result . ' команда ' . $shellCommand);
preg_match('#\[ErrorCode: (.+)\]#', $result, $matches);
$code = strtolower($matches[1]);
if (isset(self::ERROR_CODE_MESSAGE[$code])) {
throw new SignatureError(self::ERROR_CODE_MESSAGE[$code]);
}
throw new Cli("Неожиданный результат $shellCommand: \n$result");
}
}
}
12 changes: 12 additions & 0 deletions Exception/SignatureError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace nikserg\cryptoprocli\Exception;

/**
* Ошибка в подписи
*
* @package nikserg\cryptoprocli\Exception
*/
class SignatureError extends \Exception
{
}

0 comments on commit 3f4026d

Please sign in to comment.