【问题标题】:WebAuthn retrieve public key and credential idWebAuthn 检索公钥和凭证 ID
【发布时间】:2019-04-09 14:00:13
【问题描述】:

我已经按照这个教程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。解码似乎没用。

有什么想法吗?

【问题讨论】:

    标签: javascript buffer webauthn


    【解决方案1】:

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

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

    您从authData 解析出的credentialId 是由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
    }
    

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

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

    应验证从authData 响应中提取的所有其他信息,但无需保留它,只需保存credentialIdpublicKeyBytes 对。

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

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

    【讨论】:

      【解决方案2】:

      Credential IDCredential Public Key 值都不是 human palatable 字节串。

      关于如何处理它们。您将它们存储在数据库中的用户数据旁边或服务器正在使用的任何其他长期存储中,这样您就可以将它们用作未来身份验证仪式的输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-27
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2014-01-17
        • 2010-11-14
        相关资源
        最近更新 更多