javascript - 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。解压似乎没用。
任何想法 ?
解决方案
我总是得到一些奇怪的数据,没有任何看起来像 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
都应该经过验证,但不需要保留它,只需保存对credentialId
和publicKeyBytes
.
该网站详细解释了身份验证过程:https ://webauthn.guide/#authentication
要了解如何验证签名,请查看此链接:https ://w3c.github.io/webauthn/#fig-signature
推荐阅读
- java - 创建弹出窗口
- python - 从 ipyparallel 进程中不断更新和清除标准输出
- java - Minecraft 1.16.5 织物改装 - 矿石
- model-view-controller - 在 3 层网络架构 MVC 应用程序中管理 web.config db 连接
- windows - 从批处理脚本中的外部文件中的标记读取值
- laravel - Nginx 配置在我的 docker 容器上不起作用
- python - 通配符 Snakemake 规则的预处理
- regex - 包含至少一位数字的字母数字字符串的正则表达式
- reactjs - 为什么我不能将我的组件导入 React JS 中的另一个组件?
- react-native - Expo CLI 和 Expo SDK 有什么区别