Skip to content

Commit

Permalink
Update Module.php
Browse files Browse the repository at this point in the history
Refactored class in view to PHP 8 features.
Refactored class to use the SetaPDF_Signer_Signature_Module_PadesProxyTrait trait instead of implementing the proxy methods manually.
  • Loading branch information
JanSlabon committed Nov 17, 2022
1 parent 532a600 commit c9884ed
Showing 1 changed file with 11 additions and 106 deletions.
117 changes: 11 additions & 106 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,11 @@ class Module implements
\SetaPDF_Signer_Signature_DictionaryInterface,
\SetaPDF_Signer_Signature_DocumentInterface
{
/**
* @var Client
*/
protected $client;

/**
* @var string
*/
protected $requestId;

/**
* @var SetaPDF_Signer_Signature_Module_Pades
*/
protected $padesModule;
use \SetaPDF_Signer_Signature_Module_PadesProxyTrait;

/**
* @var null|string
*/
protected $certificateSerialNumber;
protected Client $client;
protected string $requestId;
protected string $certificateSerialNumber;

/**
* @param Client $client
Expand All @@ -53,10 +39,9 @@ public function __construct(Client $client, string $requestId, string $certifica
$this->client = $client;
$this->requestId = $requestId;
$this->certificateSerialNumber = $certificateSerialNumber;
$this->padesModule = new SetaPDF_Signer_Signature_Module_Pades();
}

public function setDigest(string $digest)
public function setDigest(string $digest): void
{
if (!\in_array(
$digest,
Expand All @@ -69,61 +54,7 @@ public function setDigest(string $digest)
)) {
throw new InvalidArgumentException('Invalid digest.');
}
$this->padesModule->setDigest($digest);
}

/**
* Set the signing certificate.
*
* @param string|SetaPDF_Signer_X509_Certificate $certificate PEM encoded certificate, path to the PEM encoded
* certificate or a certificate instance.
* @throws InvalidArgumentException
* @throws SetaPDF_Signer_Asn1_Exception
*/
public function setCertificate($certificate)
{
$this->padesModule->setCertificate($certificate);
}

/**
* @return SetaPDF_Signer_X509_Certificate|string
*/
public function getCertificate()
{
return $this->padesModule->getCertificate();
}

/**
* Add additional certificates which are placed into the CMS structure.
*
* @param array|SetaPDF_Signer_X509_Collection $extraCertificates PEM encoded certificates or pathes to PEM encoded
* certificates.
* @throws SetaPDF_Signer_Asn1_Exception
*/
public function setExtraCertificates($extraCertificates): void
{
$this->padesModule->setExtraCertificates($extraCertificates);
}

/**
* Adds an OCSP response which will be embedded in the CMS structure.
*
* @param string|\SetaPDF_Signer_Ocsp_Response $ocspResponse DER encoded OCSP response or OCSP response instance.
* @throws SetaPDF_Signer_Exception
*/
public function addOcspResponse($ocspResponse)
{
$this->padesModule->addOcspResponse($ocspResponse);
}

/**
* Adds an CRL which will be embedded in the CMS structure.
*
* @param string|\SetaPDF_Signer_X509_Crl $crl
*/
public function addCrl($crl)
{
$this->padesModule->addCrl($crl);
$this->_getPadesModule()->setDigest($digest);
}

/**
Expand All @@ -142,40 +73,14 @@ public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath): string
$this->setExtraCertificates($certificate['chain']);
}

$digest = $this->padesModule->getDigest();
$padesModule = $this->_getPadesModule();
$digest = $padesModule->getDigest();
// get the hash data from the module
$hash = \base64_encode(\hash($digest, $this->padesModule->getDataToSign($tmpPath), true));
$this->padesModule->setSignatureValue(
$hash = \base64_encode(\hash($digest, $padesModule->getDataToSign($tmpPath), true));
$padesModule->setSignatureValue(
$this->client->sign($this->certificateSerialNumber, $this->requestId, $digest, $hash)
);

return (string) $this->padesModule->getCms();
}

/**
* @inheritDoc
*/
public function updateSignatureDictionary(SetaPDF_Core_Type_Dictionary $dictionary): void
{
$this->padesModule->updateSignatureDictionary($dictionary);
}

/**
* @inheritDoc
*/
public function updateDocument(SetaPDF_Core_Document $document): void
{
$this->padesModule->updateDocument($document);
}

/**
* Get the complete Cryptographic Message Syntax structure.
*
* @return SetaPDF_Signer_Asn1_Element
* @throws SetaPDF_Signer_Exception
*/
public function getCms()
{
return $this->padesModule->getCms();
return (string) $padesModule->getCms();
}
}

0 comments on commit c9884ed

Please sign in to comment.