Skip to content

Commit

Permalink
Add key version enum
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <nguyennv1981@gmail.com>
  • Loading branch information
nguyennv committed Nov 5, 2024
1 parent 89d3324 commit a4306f2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 53 deletions.
35 changes: 35 additions & 0 deletions src/Enum/KeyVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP Privacy project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpenPGP\Enum;

/**
* Key version enum
*
* @package OpenPGP
* @category Enum
* @author Nguyen Van Nguyen - nguyennv1981@gmail.com
*/
enum KeyVersion: int
{
case V4 = 4;
case V6 = 6;

/**
* Get hash algo name
*
* @return string
*/
public function hashAlgo(): string
{
return match ($this) {
self::V4 => 'sha1',
self::V6 => 'sha256',
};
}
}
19 changes: 8 additions & 11 deletions src/Packet/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
EdDSACurve,
HashAlgorithm,
KeyAlgorithm,
KeyVersion,
MontgomeryCurve,
PacketTag
};
Expand All @@ -37,10 +38,6 @@
*/
class PublicKey extends AbstractPacket implements PublicKeyPacketInterface
{
const VERSION_4 = 4;
const VERSION_6 = 6;
const V4_HASH = "sha1";
const V6_HASH = "sha256";
const KEY_ID_SIZE = 8;

/**
Expand Down Expand Up @@ -73,12 +70,12 @@ public function __construct(
? PacketTag::PublicSubkey
: PacketTag::PublicKey
);
if ($version !== self::VERSION_4 && $version !== self::VERSION_6) {
if ($version !== KeyVersion::V4->value && $version !== KeyVersion::V6->value) {
throw new \InvalidArgumentException(
"Version {$version} of the key packet is unsupported."
);
}
$isV6 = $version === self::VERSION_6;
$isV6 = $version === KeyVersion::V6->value;

if ($isV6) {
if ($keyMaterial instanceof Key\ECPublicKeyMaterial) {
Expand All @@ -100,8 +97,8 @@ public function __construct(
}

$this->fingerprint = $isV6
? hash(self::V6_HASH, $this->getSignBytes(), true)
: hash(self::V4_HASH, $this->getSignBytes(), true);
? hash(KeyVersion::V6->hashAlgo(), $this->getSignBytes(), true)
: hash(KeyVersion::V4->hashAlgo(), $this->getSignBytes(), true);
$this->keyID = $isV6
? substr($this->fingerprint, 0, self::KEY_ID_SIZE)
: substr($this->fingerprint, 12, self::KEY_ID_SIZE);
Expand All @@ -128,7 +125,7 @@ public function toBytes(): string
chr($this->version),
pack("N", $this->creationTime->getTimestamp()),
chr($this->keyAlgorithm->value),
$this->version === self::VERSION_6
$this->version === KeyVersion::V6->value
? pack("N", strlen($kmBytes))
: "",
$kmBytes,
Expand Down Expand Up @@ -250,7 +247,7 @@ public function getPreferredHash(
public function getSignBytes(): string
{
$bytes = $this->toBytes();
$format = $this->version === self::VERSION_6 ? "N" : "n";
$format = $this->version === KeyVersion::V6->value ? "N" : "n";
return implode([
chr(0x95 + $this->version),
pack($format, strlen($bytes)),
Expand Down Expand Up @@ -280,7 +277,7 @@ protected static function decode(string $bytes): array
// A one-octet number denoting the public-key algorithm of this key.
$keyAlgorithm = KeyAlgorithm::from(ord($bytes[$offset++]));

if ($version === self::VERSION_6) {
if ($version === KeyVersion::V6->value) {
// - A four-octet scalar octet count for the following key material.
$offset += 4;
}
Expand Down
13 changes: 6 additions & 7 deletions src/Packet/PublicKeyEncryptedSessionKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OpenPGP\Packet;

use OpenPGP\Enum\{KeyAlgorithm, MontgomeryCurve, PacketTag};
use OpenPGP\Enum\{KeyAlgorithm, KeyVersion, MontgomeryCurve, PacketTag};
use OpenPGP\Type\{
KeyPacketInterface,
SecretKeyPacketInterface,
Expand All @@ -30,7 +30,6 @@ class PublicKeyEncryptedSessionKey extends AbstractPacket
{
const VERSION_3 = 3;
const VERSION_6 = 6;
const KEY_ID_SIZE = 8;

/**
* Constructor
Expand Down Expand Up @@ -81,12 +80,12 @@ public static function fromBytes(string $bytes): self
$keyVersion = ord($bytes[$offset++]);
$keyFingerprint = substr($bytes, $offset, $length - 1);
$offset += $length - 1;
$keyID = $keyVersion === PublicKey::VERSION_6
? substr($keyFingerprint, 0, self::KEY_ID_SIZE)
: substr($keyFingerprint, 12, self::KEY_ID_SIZE);
$keyID = $keyVersion === KeyVersion::V6->value
? substr($keyFingerprint, 0, PublicKey::KEY_ID_SIZE)
: substr($keyFingerprint, 12, PublicKey::KEY_ID_SIZE);
} else {
$keyID = substr($bytes, $offset, self::KEY_ID_SIZE);
$offset += self::KEY_ID_SIZE;
$keyID = substr($bytes, $offset, PublicKey::KEY_ID_SIZE);
$offset += PublicKey::KEY_ID_SIZE;
$keyVersion = 0;
$keyFingerprint = "";
}
Expand Down
35 changes: 17 additions & 18 deletions src/Packet/SecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
EdDSACurve,
HashAlgorithm,
KeyAlgorithm,
KeyVersion,
MontgomeryCurve,
PacketTag,
RSAKeySize,
Expand Down Expand Up @@ -76,7 +77,7 @@ public function __construct(
);

if (
$publicKey->getVersion() === PublicKey::VERSION_6 &&
$publicKey->getVersion() === KeyVersion::V6->value &&
$s2kUsage === S2kUsage::MalleableCfb
) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -137,10 +138,10 @@ public static function generate(
KeyAlgorithm::X448,
KeyAlgorithm::Ed25519,
KeyAlgorithm::Ed448
=> PublicKey::VERSION_6,
=> KeyVersion::V6->value,
default => Config::useV6Key()
? PublicKey::VERSION_6
: PublicKey::VERSION_4,
? KeyVersion::V6->value
: KeyVersion::V4->value,
};
return new self(
new PublicKey(
Expand All @@ -159,7 +160,7 @@ public static function generate(
*/
public function toBytes(): string
{
$isV6 = $this->getVersion() === PublicKey::VERSION_6;
$isV6 = $this->getVersion() === KeyVersion::V6->value;
if ($this->isEncrypted()) {
$optBytes = implode([
chr($this->symmetric->value),
Expand Down Expand Up @@ -438,7 +439,7 @@ public function getKeyData(): string
}

/**
* Decode public key packet
* Decode secret key packet
*
* @param string $bytes
* @param PublicKeyPacketInterface $publicKey
Expand All @@ -449,7 +450,7 @@ protected static function decode(
PublicKeyPacketInterface $publicKey
): array {
$offset = strlen($publicKey->toBytes());
$isV6 = $publicKey->getVersion() === PublicKey::VERSION_6;
$isV6 = $publicKey->getVersion() === KeyVersion::V6->value;
$s2kUsage = S2kUsage::from(ord($bytes[$offset++]));

// Only for a version 6 packet where the secret key material encrypted
Expand All @@ -473,19 +474,17 @@ protected static function decode(

// Only for a version 6 packet, and if string-to-key usage
// octet was 253 or 254, an one-octet count of the following field.
if (
$isV6 &&
($s2kUsage === S2kUsage::AeadProtect ||
$s2kUsage === S2kUsage::Cfb)
) {
if ($isV6 && (
$s2kUsage === S2kUsage::AeadProtect ||
$s2kUsage === S2kUsage::Cfb
)) {
$offset++;
}

$s2kType = S2kType::from(ord($bytes[$offset]));
$s2k =
$s2kType === S2kType::Argon2
? Argon2S2K::fromBytes(substr($bytes, $offset))
: GenericS2K::fromBytes(substr($bytes, $offset));
$s2k = $s2kType === S2kType::Argon2
? Argon2S2K::fromBytes(substr($bytes, $offset))
: GenericS2K::fromBytes(substr($bytes, $offset));
$offset += $s2kType->dataLength();
break;
default:
Expand Down Expand Up @@ -582,7 +581,7 @@ protected function encryptKeyMaterial(
Helper::assertSymmetric($symmetric);

$aeadProtect = $aead instanceof AeadAlgorithm;
if ($aeadProtect && $this->getVersion() !== PublicKey::VERSION_6) {
if ($aeadProtect && $this->getVersion() !== KeyVersion::V6->value) {
throw new \InvalidArgumentException(
"Using AEAD with version {$this->getVersion()} of the key packet is not allowed."
);
Expand Down Expand Up @@ -701,7 +700,7 @@ private static function produceEncryptionKey(
$symmetric->keySizeInByte(),
implode([
$packetTag,
chr(PublicKey::VERSION_6),
chr(KeyVersion::V6->value),
chr($symmetric->value),
chr($aead->value),
])
Expand Down
7 changes: 4 additions & 3 deletions src/Packet/SecretSubkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AeadAlgorithm,
Ecc,
KeyAlgorithm,
KeyVersion,
RSAKeySize,
S2kUsage,
SymmetricAlgorithm
Expand Down Expand Up @@ -116,10 +117,10 @@ public static function generate(
KeyAlgorithm::X448,
KeyAlgorithm::Ed25519,
KeyAlgorithm::Ed448
=> PublicKey::VERSION_6,
=> KeyVersion::V6->value,
default => Config::useV6Key()
? PublicKey::VERSION_6
: PublicKey::VERSION_4,
? KeyVersion::V6->value
: KeyVersion::V4->value,
};
return new self(
new PublicSubkey(
Expand Down
3 changes: 2 additions & 1 deletion src/Packet/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
HashAlgorithm,
KeyAlgorithm,
KeyFlag,
KeyVersion,
LiteralFormat,
PacketTag,
RevocationReasonTag,
Expand Down Expand Up @@ -528,7 +529,7 @@ public static function createLiteralData(
default => SignatureType::Binary,
};
$subpackets = [];
if ($signKey->getVersion() === PublicKey::VERSION_6) {
if ($signKey->getVersion() === KeyVersion::V6->value) {
foreach ($recipients as $recipient) {
if ($recipient instanceof KeyPacketInterface) {
$subpackets[] = Signature\IntendedRecipientFingerprint::fromKeyPacket(
Expand Down
Loading

0 comments on commit a4306f2

Please sign in to comment.