通过最新的Windows周年更新,Edge现在支持使用Windows Hello进行生物识别身份验证(参见https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/dev-guide/device/web-authentication/,https://blogs.windows.com/msedgedev/2016/04/12/a-world-without-passwords-windows-hello-in-microsoft-edge/)
我在C#,PHP和Node.js中有一些示例,我试图让它在Go中运行。
JS中的以下工作(我已经在挑战和密钥中进行了硬编码):
function parseBase64(s) {
s = s.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, '');
return new Uint8Array(Array.prototype.map.call(atob(s), function (c) { return c.charCodeAt(0) }));
}
function concatUint8Array(a1,a2) {
var d = new Uint8Array(a1.length + a2.length);
d.set(a1);
d.set(a2,a1.length);
return d;
}
var credAlgorithm = "RSASSA-PKCS1-v1_5";
var id,authenticatorData,signature,hash;
webauthn.getAssertion("chalenge").then(function(assertion) {
id = assertion.credential.id;
authenticatorData = assertion.authenticatorData;
signature = assertion.signature;
return crypto.subtle.digest("SHA-256",parseBase64(assertion.clientData));
}).then(function(h) {
hash = new Uint8Array(h);
var publicKey = "{\"kty\":\"RSA\",\"alg\":\"RS256\",\"ext\":false,\"n\":\"mEqGJwp0GL1oVwjRikkNfzd-Rkpb7vIbGodwQkTDsZT4_UE02WDaRa-PjxzL4lPZ4rUpV5SqVxM25aEIeGkEOR_8Xoqx7lpNKNOQs3E_o8hGBzQKpGcA7de678LeAUZdJZcnnQxXYjNf8St3aOIay7QrPoK8wQHEvv8Jqg7O1-pKEKCIwSKikCFHTxLhDDRo31KFG4XLWtLllCfEO6vmQTseT-_8OZPBSHOxR9VhIbY7VBhPq-PeAWURn3G52tQX-802waGmKBZ4B87YtEEPxCNbyyvlk8jRKP1KIrI49bgJhAe5Mow3yycQEnGuPDwLzmJ1lU6I4zgkyL1jI3Ghsw\",\"e\":\"AQAB\"}";
return crypto.subtle.importKey("jwk",JSON.parse(publicKey),credAlgorithm,false,["verify"]);
}).then(function(key) {
return crypto.subtle.verify({name:credAlgorithm, hash: { name: "SHA-256" }},key,parseBase64(signature),concatUint8Array(parseBase64(authenticatorData),hash));
}).then(function(result) {
console.log("ID=" + id + "\r\n" + result);
}).catch(function(err) {
console.log('got err: ', err);
});
在go中,我有以下代码,用于匹配上面的JS代码(req是一个带有来自JSON请求体的字符串的结构):
func webauthnSigninConversion(g string) ([]byte, error) {
g = strings.Replace(g, "-", "+", -1)
g = strings.Replace(g, "_", "/", -1)
switch(len(g) % 4) { // Pad with trailing '='s
case 0:
// No pad chars in this case
case 2:
// Two pad chars
g = g + "=="
case 3:
// One pad char
g = g + "=";
default:
return nil, fmt.Errorf("invalid string in public key")
}
b, err := base64.StdEncoding.DecodeString(g)
if err != nil {
return nil, err
}
return b, nil
}
clientData, err := webauthnSigninConversion(req.ClientData)
if err != nil {
return err
}
authenticatorData, err := webauthnSigninConversion(req.AuthenticatorData)
if err != nil {
return err
}
signature, err := webauthnSigninConversion(req.Signature)
if err != nil {
return err
}
publicKey := "{\"kty\":\"RSA\",\"alg\":\"RS256\",\"ext\":false,\"n\":\"mEqGJwp0GL1oVwjRikkNfzd-Rkpb7vIbGodwQkTDsZT4_UE02WDaRa-PjxzL4lPZ4rUpV5SqVxM25aEIeGkEOR_8Xoqx7lpNKNOQs3E_o8hGBzQKpGcA7de678LeAUZdJZcnnQxXYjNf8St3aOIay7QrPoK8wQHEvv8Jqg7O1-pKEKCIwSKikCFHTxLhDDRo31KFG4XLWtLllCfEO6vmQTseT-_8OZPBSHOxR9VhIbY7VBhPq-PeAWURn3G52tQX-802waGmKBZ4B87YtEEPxCNbyyvlk8jRKP1KIrI49bgJhAe5Mow3yycQEnGuPDwLzmJ1lU6I4zgkyL1jI3Ghsw\",\"e\":\"AQAB\"}" // this is really from a db, not hardcoded
// load json from public key, extract modulus and public exponent
obj := strings.Replace(publicKey, "\\", "", -1) // remove escapes
var k struct {
N string `json:"n"`
E string `json:"e"`
}
if err = json.Unmarshal([]byte(obj), &k); err != nil {
return err
}
n, err := webauthnSigninConversion(k.N)
if err != nil {
return err
}
e, err := webauthnSigninConversion(k.E)
if err != nil {
return err
}
pk := &rsa.PublicKey{
N: new(big.Int).SetBytes(n), // modulus
E: int(new(big.Int).SetBytes(e).Uint64()), // public exponent
}
hash := sha256.Sum256(clientData)
// Create data buffer to verify signature over
b := append(authenticatorData, hash[:]...)
if err = rsa.VerifyPKCS1v15(pk, crypto.SHA256, b, signature); err != nil {
return err
}
// if no error, signature matches
此代码以crypto/rsa: input must be hashed message
失败。如果我在hash[:]
中更改为使用b
而不是rsa.VerifyPKCS1v15
,则会因crypto/rsa: verification error
而失败。我认为我需要将authenticatorData
和hash
结合起来的原因是因为这是C#和PHP示例代码中发生的情况(cf,https://github.com/adrianba/fido-snippets/blob/master/csharp/app.cs,https://github.com/adrianba/fido-snippets/blob/master/php/fido-authenticator.php)。
也许Go以不同的方式做到了吗?
我在JS和Go中打印了字节数组,并验证了clientData
,signatureData
,authenticatorData
和hash
(以及后两者的组合数组)具有完全相同的值。在创建公钥后,我无法从JS中提取n和e字段,因此可能存在创建公钥的问题。
答案 0 :(得分:2)
我不是加密专家,但我在Go方面有一些经验,包括验证用PHP签名的签名。所以,假设比较的字节值是相同的,我会说你的问题可能是公钥创建。我建议尝试使用此函数从模数和指数创建公钥的解决方案:
func CreatePublicKey(nStr, eStr string)(pubKey *rsa.PublicKey, err error){
decN, err := base64.StdEncoding.DecodeString(nStr)
n := big.NewInt(0)
n.SetBytes(decN)
decE, err := base64.StdEncoding.DecodeString(eStr)
if err != nil {
fmt.Println(err)
return nil, err
}
var eBytes []byte
if len(decE) < 8 {
eBytes = make([]byte, 8-len(decE), 8)
eBytes = append(eBytes, decE...)
} else {
eBytes = decE
}
eReader := bytes.NewReader(eBytes)
var e uint64
err = binary.Read(eReader, binary.BigEndian, &e)
if err != nil {
fmt.Println(err)
return nil, err
}
pKey := rsa.PublicKey{N: n, E: int(e)}
return &pKey, nil
}
我比较了我的公钥和你的公钥(Playground),它们有不同的值。如果它有效,请您给我反馈一下我建议的解决方案吗?
修改1 :URLEncoding示例Playground 2
编辑2 :这是我验证签名的方式:
hasher := sha256.New()
hasher.Write([]byte(data))
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hasher.Sum(nil), signature)
因此,Edit 2代码段中的'data'变量与用于在PHP端签名的数据(消息)相同。