diff --git a/src/Common/Armor.php b/src/Common/Armor.php index d3ba80a..f5e3b46 100644 --- a/src/Common/Armor.php +++ b/src/Common/Armor.php @@ -8,8 +8,8 @@ namespace OpenPGP\Common; -use phpseclib3\Common\Functions\Strings; use OpenPGP\Enum\ArmorType; +use phpseclib3\Common\Functions\Strings; /** * Armor class @@ -127,13 +127,10 @@ public function assert(ArmorType $type): self * Verify the checksum and return the encoded bytes * * @param string $armoredText - * @param bool $checksumRequired * @return self */ - public static function decode( - string $armoredText, - bool $checksumRequired = false - ): self { + public static function decode(string $armoredText): self + { $textDone = false; $checksum = ""; $type = null; @@ -182,7 +179,7 @@ public static function decode( $data = Strings::base64_decode(implode($dataLines)); if ( strcmp($checksum, self::crc24Checksum($data)) !== 0 && - (!empty($checksum) || $checksumRequired) + (!empty($checksum) || Config::checksumRequired()) ) { throw new \RuntimeException("Ascii armor integrity check failed!"); } @@ -233,7 +230,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", sprintf( self::MULTIPART_SECTION_MESSAGE_END, $partIndex, @@ -248,7 +247,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", sprintf(self::MULTIPART_LAST_MESSAGE_END, $partIndex), ], ArmorType::SignedMessage => [ @@ -272,7 +273,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", self::SIGNATURE_END, ], ArmorType::Message => [ @@ -283,7 +286,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", self::MESSAGE_END, ], ArmorType::PublicKey => [ @@ -294,7 +299,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", self::PUBLIC_KEY_BLOCK_END, ], ArmorType::PrivateKey => [ @@ -305,7 +312,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", self::PRIVATE_KEY_BLOCK_END, ], ArmorType::Signature => [ @@ -316,7 +325,9 @@ public static function encode( self::TRUNK_SIZE, Helper::EOL ), - "=" . self::crc24Checksum($data) . Helper::EOL, + Config::checksumRequired() + ? "=" . self::crc24Checksum($data) . Helper::EOL + : "", self::SIGNATURE_END, ], }; @@ -331,10 +342,13 @@ public static function encode( */ private static function addHeader(string $customComment = ""): string { - $headers = [ - "Version: " . Config::VERSION . Helper::EOL, - "Comment: " . Config::COMMENT . Helper::EOL, - ]; + $headers = []; + if (Config::showVersion()) { + $headers[] = "Version: " . Config::VERSION . Helper::EOL; + } + if (Config::showComment()) { + $headers[] = "Comment: " . Config::COMMENT . Helper::EOL; + } if (!empty($customComment)) { $headers[] = "Comment: " . $customComment . Helper::EOL; } diff --git a/src/Common/Config.php b/src/Common/Config.php index 43512b5..345083f 100644 --- a/src/Common/Config.php +++ b/src/Common/Config.php @@ -63,6 +63,12 @@ final class Config private static bool $allowUnauthenticated = false; + private static bool $showVersion = true; + + private static bool $showComment = false; + + private static bool $checksumRequired = false; + /** * Get preferred hash algorithm. * @@ -316,4 +322,64 @@ public static function setAllowUnauthenticated(bool $allow): void { self::$allowUnauthenticated = $allow; } + + /** + * Whether to include version header in armored messages. + * + * @return bool + */ + public static function showVersion(): bool + { + return self::$showVersion; + } + + /** + * Set show version. + * + * @param bool $showVersion + */ + public static function setShowVersion(bool $showVersion): void + { + self::$showVersion = $showVersion; + } + + /** + * Whether to include comment header in armored messages. + * + * @return bool + */ + public static function showComment(): bool + { + return self::$showComment; + } + + /** + * Set show comment. + * + * @param bool $showComment + */ + public static function setShowComment(bool $showComment): void + { + self::$showComment = $showComment; + } + + /** + * Whether checksum required in armored messages. + * + * @return bool + */ + public static function checksumRequired(): bool + { + return self::$checksumRequired; + } + + /** + * Set checksum required. + * + * @param bool $checksumRequired + */ + public static function setChecksumRequired(bool $checksumRequired): void + { + self::$checksumRequired = $checksumRequired; + } } diff --git a/src/Common/Helper.php b/src/Common/Helper.php index 4e7855e..00e1384 100644 --- a/src/Common/Helper.php +++ b/src/Common/Helper.php @@ -125,6 +125,11 @@ public static function bytesToShort( public static function stringToKey( S2kType $type = S2kType::Iterated ): S2KInterface { + if ($type === S2kType::Simple) { + throw new \RuntimeException( + "S2k type {$type->name} is unsupported." + ); + } return $type === S2kType::Argon2 ? new Argon2S2K( self::generatePassword(Argon2S2K::SALT_LENGTH),