From ee20b0c48b1acd406ff2c5a42b014f9ff36aad65 Mon Sep 17 00:00:00 2001 From: Nguyen Van Nguyen Date: Wed, 25 Sep 2024 11:16:19 +0700 Subject: [PATCH] WIP Signed-off-by: Nguyen Van Nguyen --- examples/cleartext-signing.php | 221 +++++++++++++++++++++++++++++++++ src/Common/Armor.php | 2 +- src/Message/Signature.php | 43 ++++--- 3 files changed, 248 insertions(+), 18 deletions(-) create mode 100755 examples/cleartext-signing.php diff --git a/examples/cleartext-signing.php b/examples/cleartext-signing.php new file mode 100755 index 0000000..b5b8d0f --- /dev/null +++ b/examples/cleartext-signing.php @@ -0,0 +1,221 @@ +p1Vp%F}u=AO7'; + +$keyData = <<armor() . PHP_EOL; + +echo 'Verify signed message:' . PHP_EOL . PHP_EOL; +$verifications = OpenPGP::verify($armored, [ + $rsaPrivateKey->toPublic(), + $eccPrivateKey->toPublic(), + $curve25519PrivateKey->toPublic(), + $curve4489PrivateKey->toPublic(), +]); +foreach ($verifications as $verification) { + echo "Key ID: {$verification->getKeyID(true)}" . PHP_EOL; + echo "Signature is verified: {$verification->isVerified()}" . PHP_EOL; + echo "Verification error: {$verification->getVerificationError()}" . PHP_EOL . PHP_EOL; +} diff --git a/src/Common/Armor.php b/src/Common/Armor.php index 76b81f5..332b299 100644 --- a/src/Common/Armor.php +++ b/src/Common/Armor.php @@ -244,7 +244,7 @@ public static function encode( Helper::EOL, array_map(static fn ($hashAlgo) => "Hash: $hashAlgo", $hashAlgos) ) . Helper::EOL . Helper::EOL : Helper::EOL, - preg_replace(self::DASH_PATTERN, '- -', $text) . Helper::EOL, // Dash-escape text + preg_replace(self::DASH_PATTERN, '- - ', $text) . Helper::EOL, // Dash-escape text self::SIGNATURE_BEGIN, self::addHeader($customComment) . Helper::EOL, chunk_split(Strings::base64_encode($data), self::TRUNK_SIZE, Helper::EOL), diff --git a/src/Message/Signature.php b/src/Message/Signature.php index a908638..189967c 100644 --- a/src/Message/Signature.php +++ b/src/Message/Signature.php @@ -21,6 +21,7 @@ use OpenPGP\Type\{ CleartextMessageInterface, KeyInterface, + KeyPacketInterface, LiteralDataInterface, PacketListInterface, SignatureInterface, @@ -113,29 +114,37 @@ public function verify( $verifications = []; foreach ($this->packetList as $packet) { foreach ($verificationKeys as $key) { - $isVerified = false; - $verificationError = ''; - $keyPacket = $key->toPublic()->getSigningKeyPacket( - $packet->getIssuerKeyID() - ); + $keyPacket = null; try { - $isVerified = $packet->verify( - $keyPacket, - $literalData->getSignBytes(), - $time, + $keyPacket = $key->toPublic()->getSigningKeyPacket( + $packet->getIssuerKeyID() ); } catch (\Throwable $e) { - $verificationError = $e->getMessage(); - Config::getLogger()->error($verificationError); + Config::getLogger()->error($e->getMessage()); } + if ($keyPacket instanceof KeyPacketInterface) { + $isVerified = false; + $verificationError = ''; + try { + $isVerified = $packet->verify( + $keyPacket, + $literalData->getSignBytes(), + $time, + ); + } + catch (\Throwable $e) { + $verificationError = $e->getMessage(); + Config::getLogger()->error($verificationError); + } - $verifications[] = new Verification( - $keyPacket->getKeyID(), - $packet, - $isVerified, - $verificationError, - ); + $verifications[] = new Verification( + $keyPacket->getKeyID(), + $packet, + $isVerified, + $verificationError, + ); + } } } return $verifications;