Skip to content
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
3 changes: 3 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ https://libresign.coop
<command>OCA\Libresign\Command\Crl\Stats</command>
<command>OCA\Libresign\Command\Crl\Cleanup</command>
<command>OCA\Libresign\Command\Crl\Revoke</command>
<command>OCA\Libresign\Command\OrganizationSeal\Add</command>
<command>OCA\Libresign\Command\OrganizationSeal\Delete</command>
<command>OCA\Libresign\Command\OrganizationSeal\ListCommand</command>
<command>OCA\Libresign\Command\SignatureElements\Cleanup</command>
<command>OCA\Libresign\Command\Developer\Reset</command>
<command>OCA\Libresign\Command\Developer\SignSetup</command>
Expand Down
82 changes: 82 additions & 0 deletions lib/Command/OrganizationSeal/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Command\OrganizationSeal;

use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\OrganizationSeal\OrganizationSealStore;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class Add extends Command {
public const PASSWORD_ENV = 'LIBRESIGN_SEAL_PASSWORD';

public function __construct(
private OrganizationSealStore $sealStore,
) {
parent::__construct();
}

#[\Override]
protected function configure(): void {
$this
->setName('libresign:organization-seal:add')
->setDescription('Add an organization seal certificate (PKCS#12) applied after the last signature when a request asks for it')
->addArgument('label', InputArgument::REQUIRED, 'Label shown to requesters')
->addArgument('pfx', InputArgument::REQUIRED, 'Path to the PKCS#12 (.p12/.pfx) file')
->addOption('password-from-env', null, InputOption::VALUE_NONE, 'Read the certificate password from the ' . self::PASSWORD_ENV . ' environment variable instead of asking for it');
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$path = (string)$input->getArgument('pfx');
$pfx = is_file($path) && is_readable($path) ? file_get_contents($path) : false;
if ($pfx === false) {
$output->writeln('<error>Cannot read the certificate file.</error>');
return self::FAILURE;
}

$password = $this->getPassword($input, $output);
if ($password === null) {
$output->writeln('<error>No certificate password given.</error>');
return self::FAILURE;
}

try {
$seal = $this->sealStore->add((string)$input->getArgument('label'), $pfx, $password);
} catch (LibresignException $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return self::FAILURE;
}

$output->writeln(sprintf('Organization seal added with id %s (%s, valid until %s).', $seal['id'], $seal['subject'], $seal['validTo']));
return self::SUCCESS;
}

private function getPassword(InputInterface $input, OutputInterface $output): ?string {
if ($input->getOption('password-from-env')) {
$password = getenv(self::PASSWORD_ENV);
return is_string($password) ? $password : null;
}

if (!$input->isInteractive()) {
return null;
}

$question = (new Question('Certificate password: '))
->setHidden(true)
->setHiddenFallback(false);
$password = (new QuestionHelper())->ask($input, $output, $question);
return is_string($password) ? $password : '';
}
}
42 changes: 42 additions & 0 deletions lib/Command/OrganizationSeal/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Command\OrganizationSeal;

use OCA\Libresign\Service\OrganizationSeal\OrganizationSealStore;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Delete extends Command {
public function __construct(
private OrganizationSealStore $sealStore,
) {
parent::__construct();
}

#[\Override]
protected function configure(): void {
$this
->setName('libresign:organization-seal:delete')
->setDescription('Delete an organization seal certificate')
->addArgument('id', InputArgument::REQUIRED, 'Id of the seal, see libresign:organization-seal:list');
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
if (!$this->sealStore->delete((string)$input->getArgument('id'))) {
$output->writeln('<error>Unknown organization seal.</error>');
return self::FAILURE;
}

$output->writeln('Organization seal deleted.');
return self::SUCCESS;
}
}
36 changes: 36 additions & 0 deletions lib/Command/OrganizationSeal/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Command\OrganizationSeal;

use OC\Core\Command\Base;
use OCA\Libresign\Service\OrganizationSeal\OrganizationSealStore;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Base {
public function __construct(
private OrganizationSealStore $sealStore,
) {
parent::__construct();
}

#[\Override]
protected function configure(): void {
parent::configure();
$this
->setName('libresign:organization-seal:list')
->setDescription('List the organization seal certificates');
}

#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->writeTableInOutputFormat($input, $output, $this->sealStore->list());
return self::SUCCESS;
}
}
9 changes: 7 additions & 2 deletions lib/Handler/SignEngine/JSignPdf/JSignPdfHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Imagick;
use ImagickPixel;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Enum\DocMdpLevel;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
Expand Down Expand Up @@ -221,8 +222,12 @@ public function getSignedContent(): string {

$parameters = [];
$certificationLevel = $this->getCertificationLevel();
if ($certificationLevel !== null && !$this->getVisibleElements() && !$this->hasExistingSignatures($normalizedPdf)) {
$parameters['-cl'] = $certificationLevel;
if ($certificationLevel !== null) {
// The parameters are kept on this handler between signatures, so a
// signature that does not certify clears the level of an earlier one.
$parameters['-cl'] = !$this->getVisibleElements() && !$this->hasExistingSignatures($normalizedPdf)
? $certificationLevel
: DocMdpLevel::NOT_CERTIFIED->name;
}
$parameters += $this->getTsaParameters();
$param->addJSignParameters($parameters);
Expand Down
16 changes: 8 additions & 8 deletions lib/Handler/SignEngine/PhpNativeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getSignedContent(): string {
$visibleElements = $this->getVisibleElements();
$metadata = $this->buildMetadata();
$timestamp = $this->buildTimestampOptions();
$certificationLevel = $this->resolveCertificationLevel(empty($visibleElements));
$certificationLevel = $this->resolveCertificationLevel();

if (empty($visibleElements)) {
return $service->sign(SignPdfRequestDto::fromRequired(
Expand Down Expand Up @@ -248,19 +248,19 @@ private function getTsaSettings(): array {
return $settings;
}

private function resolveCertificationLevel(bool $noVisibleElements): ?CertificationLevel {
private function resolveCertificationLevel(): ?CertificationLevel {
if (!$this->docMdpConfigService->isEnabled()) {
return null;
}

// DocMDP values mirror CertificationLevel: 1=NoChanges, 2=FormFilling, 3=FormFillAndAnnotations
$level = $this->docMdpConfigService->getLevel()->value;
// Only certify on invisible signatures or on the first visible element
if ($noVisibleElements || !$this->hasExistingSignatures($this->getInputFile()->getContent())) {
return CertificationLevel::fromInt($level);
// A certification signature must be the first signature of the
// document; with visible elements only the first element certifies.
if ($this->hasExistingSignatures($this->getInputFile()->getContent())) {
return null;
}

return null;
// DocMDP values mirror CertificationLevel: 1=NoChanges, 2=FormFilling, 3=FormFillAndAnnotations
return CertificationLevel::fromInt($this->docMdpConfigService->getLevel()->value);
}

private function hasExistingSignatures(string $pdfContent): bool {
Expand Down
7 changes: 7 additions & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@
* effectiveValue: string,
* sourceScope: string,
* }
* @psalm-type LibresignOrganizationSealResult = array{
* status: 'sealed'|'failed',
* error?: string,
* at: string,
* }
* @psalm-type LibresignPolicySnapshotLegalInformationEntry = array{
* effectiveValue: string,
* sourceScope: string,
Expand Down Expand Up @@ -628,6 +633,7 @@
* rejection_comment_mode?: LibresignPolicySnapshotSignatureRejectionCommentModeEntry,
* rejection_visibility?: LibresignPolicySnapshotSignatureRejectionVisibilityEntry,
* rejection_comment_visibility?: LibresignPolicySnapshotSignatureRejectionVisibilityEntry,
* organization_seals?: LibresignPolicySnapshotEntry,
* }
* @psalm-type LibresignValidateMetadata = array{
* extension: string,
Expand All @@ -637,6 +643,7 @@
* policy_snapshot?: LibresignValidatePolicySnapshot,
* pdfVersion?: string,
* status_changed_at?: string,
* organization_seals?: array<string, LibresignOrganizationSealResult>,
* }
* @psalm-type LibresignFileRuntimeMetadata = LibresignValidateMetadata|array<string, LibresignDynamicMetadataValue>
* @psalm-type LibresignValidationPageResolution = array{
Expand Down
Loading