Merge branch '3.0'

This commit is contained in:
terrafrost 2024-05-25 14:53:05 -05:00
commit 0db8742630
2 changed files with 35 additions and 6 deletions

View File

@ -130,11 +130,13 @@ abstract class PKCS8 extends Progenitor
if (isset($key['privateKey'])) { if (isset($key['privateKey'])) {
$components['curve'] = $key['privateKeyAlgorithm']['algorithm'] == 'id-Ed25519' ? new Ed25519() : new Ed448(); $components['curve'] = $key['privateKeyAlgorithm']['algorithm'] == 'id-Ed25519' ? new Ed25519() : new Ed448();
$expected = chr(ASN1::TYPE_OCTET_STRING) . ASN1::encodeLength($components['curve']::SIZE);
// 0x04 == octet string if (substr($key['privateKey'], 0, 2) != $expected) {
// 0x20 == length (32 bytes) throw new RuntimeException(
if (substr($key['privateKey'], 0, 2) != "\x04\x20") { 'The first two bytes of the ' .
throw new RuntimeException('The first two bytes of the private key field should be 0x0420'); $key['privateKeyAlgorithm']['algorithm'] .
' private key field should be 0x' . bin2hex($expected)
);
} }
$arr = $components['curve']->extractSecret(substr($key['privateKey'], 2)); $arr = $components['curve']->extractSecret(substr($key['privateKey'], 2));
$components['dA'] = $arr['dA']; $components['dA'] = $arr['dA'];
@ -200,7 +202,7 @@ abstract class PKCS8 extends Progenitor
if ($curve instanceof TwistedEdwardsCurve) { if ($curve instanceof TwistedEdwardsCurve) {
return self::wrapPrivateKey( return self::wrapPrivateKey(
"\x04\x20" . $secret, chr(ASN1::TYPE_OCTET_STRING) . ASN1::encodeLength($curve::SIZE) . $secret,
[], [],
null, null,
$password, $password,

View File

@ -299,6 +299,33 @@ WEKBIQAZv0QJaYTN/oVBusFn3DuWyFCGqjC2tssMXDitcDFm4Q==
$this->assertSameNL('Ed25519', $key->getPublicKey()->getCurve()); $this->assertSameNL('Ed25519', $key->getPublicKey()->getCurve());
} }
// Generate with:
// openssl genpkey -algorithm ed448 | openssl ec -pubout
public function testEd448PublicKey(): void
{
$expected = '-----BEGIN PUBLIC KEY-----
MEMwBQYDK2VxAzoAsA7zbld48IfDhm7Qd6FYrvnljtjhPRRqZi04NWyj8VXrWe1x
BMLQFJEE0JDmKayUWpUWsRXwmb6A
-----END PUBLIC KEY-----';
$key = PublicKeyLoader::load($expected);
$this->assertSameNL('Ed448', $key->getCurve());
$this->assertSameNL($expected, $key->toString('PKCS8'));
}
// Generate with:
// openssl genpkey -algorithm ed448
public function testEd448PrivateKey(): void
{
$expected = '-----BEGIN PRIVATE KEY-----
MEcCAQAwBQYDK2VxBDsEOettXaJYob4hJNKJNOD+FfMvdesLKNp0KwochI6AKmAb
tWhtkn99WOjd1PsGMh9zz2Vhdg3MwasOMQ==
-----END PRIVATE KEY-----';
$key = PublicKeyLoader::load($expected);
$this->assertSameNL($expected, $key->toString('PKCS8'));
$this->assertSameNL('Ed448', $key->getCurve());
$this->assertSameNL('Ed448', $key->getPublicKey()->getCurve());
}
public function testPuTTYnistp256(): void public function testPuTTYnistp256(): void
{ {
$key = PublicKeyLoader::load($expected = 'PuTTY-User-Key-File-2: ecdsa-sha2-nistp256 $key = PublicKeyLoader::load($expected = 'PuTTY-User-Key-File-2: ecdsa-sha2-nistp256