Skip to content

Instantly share code, notes, and snippets.

@MasterKale
Created September 10, 2020 05:42
Show Gist options
  • Save MasterKale/175cb210b097632d7cd03fd409e2dfb3 to your computer and use it in GitHub Desktop.
Save MasterKale/175cb210b097632d7cd03fd409e2dfb3 to your computer and use it in GitHub Desktop.
SimpleWebAuthn "old format" public key upgrader
import base64url from 'base64url';
import cbor from 'cbor';
/**
* @simplewebauthn/server
* September 2020
*
* Add additional data to a barebones "old format" public key. Additional information includes:
*
* - "ECC" key type { kty: 2 }
* - "P-256" curve { alg: -7, crv: 1 }
*
* "Old format" is the following bas64url-encoded byte array:
*
* [0x04, x: 32 bytes, y: 32 bytes]
*/
const oldPubKey = 'BGmaxR4mBbukc2QhtW2ldhAAd555r-ljlGQN8MbcTnPP9CyUlE-0AB2fbzZbNgBvJuRa7r6o2jPphOmtyNPR_kY';
let oldBuffer = base64url.toBuffer(oldPubKey);
// Skip the "uncompressed" byte
oldBuffer = oldBuffer.slice(1);
const x = oldBuffer.slice(0, 32);
oldBuffer = oldBuffer.slice(32);
const y = oldBuffer.slice(0, 32);
oldBuffer = oldBuffer.slice(32);
const newMap = new cbor.Map([
[1, 2], // kty
[3, -7], // alg
[-1, 1], // crv
[-2, x], // x
[-3, y], // y
]);
const newBuffer = Buffer.from(cbor.encode(newMap));
const newPubKey = base64url.encode(newBuffer);
// This string is the "new format" value for the old "authenticator.publicKey" value
console.log(newPubKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment