如何在Flutter中固定公钥?

问题描述 投票:1回答:2

我想固定服务器的公共密钥,以便对服务器的任何请求都必须具有该公共密钥(这是为了防止像Charles这样的代理嗅探数据)。

我用Volley在Android中做过类似的事情。

如何使用Flutter进行相同操作?

dart flutter pinning
2个回答
5
投票

使用没有可信任根的SecurityContext创建您的客户端会强制执行错误的证书回调,即使是获得好的证书也是如此。

SecurityContext(withTrustedRoots: false);

在不良证书回调中,使用asn1lib package解析DER编码的证书。例如:

ASN1Parser p = ASN1Parser(der);
ASN1Sequence signedCert = p.nextObject() as ASN1Sequence;
ASN1Sequence cert = signedCert.elements[0] as ASN1Sequence;
ASN1Sequence pubKeyElement = cert.elements[6] as ASN1Sequence;

ASN1BitString pubKeyBits = pubKeyElement.elements[1] as ASN1BitString;

List<int> encodedPubKey = pubKeyBits.stringValue;
// could stop here and compare the encoded key parts, or...

// parse them into their modulus/exponent parts, and test those
// (assumes RSA public key)
ASN1Parser rsaParser = ASN1Parser(encodedPubKey);
ASN1Sequence keySeq = rsaParser.nextObject() as ASN1Sequence;
ASN1Integer modulus = keySeq.elements[0] as ASN1Integer;
ASN1Integer exponent = keySeq.elements[1] as ASN1Integer;

print(modulus.valueAsBigInteger);
print(exponent);

0
投票

旋转钥匙可降低风险。当攻击者获取旧服务器硬盘驱动器或备份文件并从中获取旧服务器私钥时,如果密钥已被旋转,则他们无法模拟当前服务器。因此,在更新证书时始终生成一个新密钥。配置客户端以信任旧密钥和新密钥。等待您的用户更新到客户端的新版本。然后将新密钥部署到您的服务器。然后,您可以从客户端删除旧密钥。

仅在不旋转键的情况下才需要服务器键固定。这是不安全的做法。

您应该旋转进行证书固定。我在How to do SSL pinning via self generated signed certificates in flutter?

中添加了示例代码
© www.soinside.com 2019 - 2024. All rights reserved.