首页 > 解决方案 > WebAuthn 检索公钥和凭证 ID

问题描述

我已按照本教程https://webauthn.guide/#registration

我正在使用 yubico nfc 密钥,我几乎设法注册了安全密钥。我从服务器发送一个随机字节质询来注册密钥和其他数据。

当我注册密钥时,我设法解码了 clientDataJson 和身份验证响应以检索大量信息。但是,我无法弄清楚如何处理 credentialId 和 authData 缓冲区,我尝试对它们进行解码、解密,但我总是得到一些奇怪的数据,而且没有任何东西看起来像 credentialId 或公钥。

这是我到目前为止得到的代码

 var createCredentialDefaultArgs = {
            publicKey: {
                // Relying Party (a.k.a. - Service):
                rp: {
                    name: 'Dummy'
                },

                // User:
                user: {
                    id: new Uint8Array(16),
                    name: 'John Doe',
                    displayName: 'Mr Doe'
                },

                pubKeyCredParams: [{
                    type: "public-key",
                    alg: -7
                }],

                attestation: "direct",

                timeout: 60000,

                challenge:  new Uint8Array(/* stuff*/).buffer
            }
        };


        $('[data-register-webauthn]')
            .on('click', function () {
                // register / create a new credential
                navigator.credentials.create(createCredentialDefaultArgs)
                    .then((cred) => {
                        console.log("NEW CREDENTIAL", cred);

                        const utf8Decoder = new TextDecoder('utf-8');

                        const decodedClientData = utf8Decoder.decode(cred.response.clientDataJSON);

                        // parse the string as an object
                        const clientDataObj = JSON.parse(decodedClientData);

                        const decodedAttestationObj = CBOR.decode(cred.response.attestationObject);

                        const {authData} = decodedAttestationObj;
                        // get the length of the credential ID
                        const dataView = new DataView(new ArrayBuffer(2));
                        const idLenBytes = authData.slice(53, 55);
                        idLenBytes.forEach(
                            (value, index) => dataView.setUint8(
                                index, value)
                        );
                        const credentialIdLength = dataView.getUint16();

                        // get the credential ID
                        const credentialId = authData.slice(
                            55, credentialIdLength);

                        // get the public key object
                        const publicKeyBytes = authData.slice(
                            55 + credentialIdLength);

                        // the publicKeyBytes are encoded again as CBOR
                        const publicKeyObject = CBOR.decode(
                            publicKeyBytes.buffer);
                        console.log(publicKeyObject)
                    })
                    .catch((err) => {
                        console.log("ERROR", err);
                    });
            })

最后,我不知道如何处理公钥对象和credentialId。解压似乎没用。

任何想法 ?

标签: javascriptbufferwebauthn

解决方案


我总是得到一些奇怪的数据,没有任何看起来像 credentialId 或公钥的数据。

那是因为两者都是字节序列。

credentialId您从中解析的是authData由 U2F 密钥生成的一串随机字节(通常为 96 字节长),因此不要指望那些有意义。

publicKey变量有点棘手,因为它是 CBOR 编码的(不是您的常规 PEM 字符串),在解码后publicKeyObject应该会给您这样的输出:

{
   1: 2,              // Ellipic Curve key type
   3: -7,             // ES256 signature algorithm
  -1: 1,              // P-256 curve
  -2: 0x7885DB484..., // X value
  -3: 0x814F3DD31...  // Y value
}

最后,我不知道如何处理公钥对象和credentialId。

您需要凭据 ID识别尝试对您的网站进行身份验证的用户,并需要公钥验证其身份。

从响应中提取的所有其他信息authData都应该经过验证,但不需要保留它,只需保存对credentialIdpublicKeyBytes.

该网站详细解释了身份验证过程:https ://webauthn.guide/#authentication

要了解如何验证签名,请查看此链接:https ://w3c.github.io/webauthn/#fig-signature


推荐阅读