paragonie / paseto

Platform-Agnostic Security Tokens
https://paseto.io
Other
3.23k stars 108 forks source link

[Version 3] Cannot create token from encoded private key #167

Closed frederikbosch closed 1 year ago

frederikbosch commented 1 year ago

A few months go I already reported #163. There are more problems with encoding a private key, and creating a token from the encoded private key. In order to show the problem, rather than reporting another issue, I created a test that shows the problem. I would not be surprised if #163 and this report are linked.

This code:

$privateKey = AsymmetricSecretKey::generate(new Version3());
$privateKeyEncoded = $privateKey->encode();

(new Builder())
    ->setKey(AsymmetricSecretKey::fromEncodedString($privateKeyEncoded, new Version3()))
    ->setVersion(new Version3())
    ->setPurpose(Purpose::public())
    ->setIssuer('Test')
    ->toString();

Results in this error

1) ParagonIE\Paseto\Tests\KeyTest::testBuildTokenWithReadingSecretKeyFromEncodedString
ParagonIE\Paseto\Exception\PasetoException: Signing failed.

/home/runner/work/paseto-1/paseto-1/src/Builder.php:668
/home/runner/work/paseto-1/paseto-1/tests/KeyTest.php:182

Caused by
FG\ASN1\Exception\ParserException: ASN.1 Parser Exception at offset 9: Can not parse content length (long form) from data: Offset index larger than input size

/home/runner/work/paseto-1/paseto-1/vendor/fgrosse/phpasn1/lib/ASN1/ASNObject.php:331
/home/runner/work/paseto-1/paseto-1/vendor/fgrosse/phpasn1/lib/ASN1/ASNObject.php:270
/home/runner/work/paseto-1/paseto-1/vendor/mdanter/ecc/src/Serializer/PrivateKey/DerPrivateKeySerializer.php:91
/home/runner/work/paseto-1/paseto-1/vendor/mdanter/ecc/src/Serializer/PrivateKey/PemPrivateKeySerializer.php:54
/home/runner/work/paseto-1/paseto-1/vendor/paragonie/easy-ecc/src/ECDSA/SecretKey.php:60
/home/runner/work/paseto-1/paseto-1/src/Protocol/Version3.php:287
/home/runner/work/paseto-1/paseto-1/src/Builder.php:661
/home/runner/work/paseto-1/paseto-1/tests/KeyTest.php:182

If I encode the raw key myself, using simple base64 encoding (base64_encode), and then call AsymmetricSecretKey::fromEncodedString(), I am able to read the private key.