EC/PKCS8: OpenSSL didn't like phpseclib formed Ed25519 public keys

This commit is contained in:
terrafrost 2022-07-15 08:53:06 -05:00
parent 1692298efd
commit feced404bb
2 changed files with 8 additions and 12 deletions

View File

@ -625,12 +625,15 @@ abstract class PKCS8 extends PKCS
$key = [
'publicKeyAlgorithm' => [
'algorithm' => is_string(static::OID_NAME) ? static::OID_NAME : $oid,
'parameters' => $params
'algorithm' => is_string(static::OID_NAME) ? static::OID_NAME : $oid
],
'publicKey' => "\0" . $key
];
if ($oid != 'id-Ed25519' && $oid != 'id-Ed448') {
$key['publicKeyAlgorithm']['parameters'] = $params;
}
$key = ASN1::encodeDER($key, Maps\PublicKeyInfo::MAP);
return "-----BEGIN PUBLIC KEY-----\r\n" .

View File

@ -256,18 +256,11 @@ BjoJZJZQztmlj7Qep/sf1l8=
// from https://tools.ietf.org/html/draft-ietf-curdle-pkix-07#section-10.1
public function testEd25519PublicKey()
{
$key = PublicKeyLoader::load('-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=
-----END PUBLIC KEY-----');
$this->assertSameNL('Ed25519', $key->getCurve());
// in the above key AlgorithmIdentifier has a single "child". in the
// following key it has two. The second one is ("optional") NULL.
// https://security.stackexchange.com/q/110330/15922 elaborates on
// why phpseclib is encoding the NULL as opposed to omitting it.
$expected = '-----BEGIN PUBLIC KEY-----
MCwwBwYDK2VwBQADIQAZv0QJaYTN/oVBusFn3DuWyFCGqjC2tssMXDitcDFm4Q==
MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=
-----END PUBLIC KEY-----';
$key = PublicKeyLoader::load($expected);
$this->assertSameNL('Ed25519', $key->getCurve());
$this->assertSameNL($expected, $key->toString('PKCS8'));
}