paragonie / paseto

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

Invalid MAC for given ciphertext #160

Closed Jacobinoo closed 1 year ago

Jacobinoo commented 1 year ago

I have generated the secret local key using PASETO V3 on Node.js, and then converted it from KeyObject to hex. However the secret key is not compatible with PASETO PHP.

Generated secret key: feed3e081af0fdfdfbdaac45421cbdc1139e06cd52ec15bd4c7c4522cecfb314 Generated token: _v3.local.SwguoFUJpfByqaQiEhEhe9sM63XhvEkY_mXOkdDcIt7-Gi1D8CrnkaSEO-EXPgGTobUF5Gyvq51pLRDUBu-aAShnjRkaRlulID4JMvoatxieQ7k9qAIDX9nQ_dt-rCx_2Y1eV-cahtK6ovrAFlxMthQf4mwWFyJDlggLBd3wzMfDjV-ci78uIRcQdpKvL37bWeUon2t8OOC0sKImZGoSAr6uNpSUNFG

I can decrypt it in Node, but in PHP an error is returned: ParagonIE\Paseto\Exception\SecurityException: Invalid MAC for given ciphertext**

Am I doing something wrong or the secret keys are incompatible with other platforms?

PHP code:

$sharedKey = new SymmetricKey("feed3e081af0fdfdfbdaac45421cbdc1139e06cd52ec15bd4c7c4522cecfb314", new Version3);
$providedToken = "v3.local.SwguoFUJpfByqaQiEhEhe9sM63XhvEkY_mXOkdDcIt7-Gi1D8CrnkaSEO-EXPgGTobUF5Gyvq51pLRDUBu-aAShnjRkaRlulID4JMvoatxieQ7k9qAIDX9nQ_dt-rCx_2Y1eV-cahtK6ovrAFlxMthQf4mwWFyJDl_ggLBd3wzMfDjV-ci78uIRcQdpKvL37bWeUon2t8OOC0sKImZGoSAr6uNpSUNFG";
$parser = (new Parser())
    ->setKey($sharedKey)
    //->addRule(new ValidAt(new DateTime()))
    ->addRule(new IssuedBy('connect-ccas'))
    ->setPurpose(Purpose::local())
    ->setAllowedVersions(ProtocolCollection::v3());

try {
    $token = $parser->parse($providedToken);
    echo "Provided token is valid. Authorization succeded!";
} catch (PasetoException $ex) {
    echo "Provided token is invalid. Authorization failed!";
    echo "DEV_ERR: ".$ex;
}

Node:

const key = await V3.generateKey('local', {format: "keyobject"});
console.log((key.export()).toString('hex'));
//Outputs: feed3e081af0fdfdfbdaac45421cbdc1139e06cd52ec15bd4c7c4522cecfb314
aidantwoods commented 1 year ago

The new SymmetricKey constructor expects you to pass in the raw key material as bytes. If you have the key exported as hex, you'd need to replace your first line with something like:

$sharedKey = new SymmetricKey(hex2bin("feed3e081af0fdfdfbdaac45421cbdc1139e06cd52ec15bd4c7c4522cecfb314"), new Version3);
Jacobinoo commented 1 year ago

Makes sense! Thank you so much :))