Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сигнеры для КриптоПро #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Esia/Signer/CliCryptoProSigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Esia\Signer;

use Esia\Signer\Exceptions\NoSuchTmpDirException;
use Esia\Signer\Exceptions\SignFailException;

final class CliCryptoProSigner implements SignerInterface
{
private $toolPath;
private $thumbprint;
private $pin;
private $tempDir;

public function __construct(
string $toolPath,
string $thumbprint,
?string $pin = null,
?string $tempDir = null
) {
$this->toolPath = $toolPath;
$this->thumbprint = $thumbprint;
$this->pin = $pin;
$this->tempDir = $tempDir ?? sys_get_temp_dir();

if (!file_exists($this->tempDir)) {
throw new NoSuchTmpDirException('Temporary folder is not found');
}
if (!is_writable($this->tempDir)) {
throw new NoSuchTmpDirException('Temporary folder is not writable');
}
}

public function sign(string $message): string
{
$tempPath = tempnam($this->tempDir, 'cryptcp');
file_put_contents($tempPath, $message);

try {
return $this->signFile($tempPath);
} catch (SignFailException $e) {
unlink($tempPath);

throw $e;
}
}

private function signFile(string $tempPath): string
{
$command = "$this->toolPath -signf -dir $this->tempDir -cert -thumbprint $this->thumbprint";
if ($this->pin) {
$command .= " -pin $this->pin";
}
$command .= " $tempPath";

$output = null;
$resultCode = null;
exec($command, $output, $resultCode);

if ($resultCode !== 0) {
throw new SignFailException('Failure signing: ' . implode("\n", $output));
}

$signatureFilePath = $tempPath . '.sgn';
$signature = file_get_contents($signatureFilePath);
unlink($signatureFilePath);

if (!$signature) {
throw new SignFailException("Failure reading $signatureFilePath");
}

return $signature;
}
}
47 changes: 47 additions & 0 deletions src/Esia/Signer/CryptoProSigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Esia\Signer;

use Esia\Signer\Exceptions\SignFailException;

final class CryptoProSigner implements SignerInterface
{
private $thumbprint;
private $pin;

public function __construct(
string $thumbprint,
?string $pin = null
) {
$this->thumbprint = $thumbprint;
$this->pin = $pin;
}

public function sign(string $message): string
{
$store = new \CPStore();
$store->Open(CURRENT_USER_STORE, 'My', STORE_OPEN_READ_ONLY);

$certificates = $store->get_Certificates();
$found = $certificates->Find(CERTIFICATE_FIND_SHA1_HASH, $this->thumbprint, 0);
$certificate = $found->Item(1);
if (!$certificate) {
throw new SignFailException('Cannot read the certificate');
}
if ($certificate->HasPrivateKey() === false) {
throw new SignFailException('Cannot read the private key');
}

$signer = new \CPSigner();
$signer->set_Certificate($certificate);
if ($this->pin) {
$signer->set_KeyPin($this->pin);
}

$sd = new \CPSignedData();
$sd->set_ContentEncoding(BASE64_TO_BINARY);
$sd->set_Content(base64_encode($message));

return $sd->SignCades($signer, CADES_BES, true, ENCODE_BASE64);
}
}
44 changes: 44 additions & 0 deletions tests/unit/Signer/CliCryptoProSignerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace tests\unit\Signer;

use Esia\Signer\CliCryptoProSigner;

class CliCryptoProSignerTest extends \Codeception\Test\Unit
{
public function testSign(): void
{
$signer = new CliCryptoProSigner(
'/opt/cprocsp/bin/amd64/cryptcp',
'66821344ce484aceb984d887b303544bfdda8ea4'
);

// we expect exception here, because we can't install cryptcp to ci 🤷
$this->expectException(\Esia\Signer\Exceptions\SignFailException::class);
$signer->sign('test');
}

public function testTempDirDoesNotExists(): void
{
$this->expectException(\Esia\Signer\Exceptions\NoSuchTmpDirException::class);

new CliCryptoProSigner(
'/opt/cprocsp/bin/amd64/cryptcp',
'66821344ce484aceb984d887b303544bfdda8ea4',
null,
'/'
);
}

public function testTempDirIsNotWritable(): void
{
$this->expectException(\Esia\Signer\Exceptions\NoSuchTmpDirException::class);

new CliCryptoProSigner(
'/opt/cprocsp/bin/amd64/cryptcp',
'66821344ce484aceb984d887b303544bfdda8ea4',
null,
codecept_log_dir('non_writable_directory')
);
}
}