简体   繁体   English

Bouncy Castle DSA带有SHA1签名验证

[英]Bouncy Castle DSA with SHA1 signature verification

My project is using signature verification of some datasets which come from certain third-party software. 我的项目使用来自某些第三方软件的一些数据集的签名验证。 Signature algorithm used is SHA1withDSA . 使用的签名算法是SHA1withDSA When I was using standard SUN crypto provider, that comes with SDK, all went fine. 当我使用SDK附带的标准SUN加密提供程序时,一切都很顺利。 Recently I switched to Bouncy Castle 1.50, and after that some of datasets which previously (that is, with SUN provider) stood verification, began to fail it, while the rest is still verified OK. 最近我切换到Bouncy Castle 1.50,之后一些以前(也就是SUN提供商)的数据集经过验证,开始失败,其余的仍然可以验证。

I explored source codes of both providers, and it turned out that SDK's default provider has some sort of protection from incorrectly formed signatures (while capable of being recovered), and Bouncy Castle provider does not have it. 我探索了两个提供商的源代码,结果发现SDK的默认提供商对错误形成的签名(虽然能够被恢复)有某种保护,而Bouncy Castle提供商没有它。 Check out OpenJDK for Java 7 (lines 336-344) or OpenJDK for Java 8 (lines 265-273): there they have made some signature fix in certain case. 查看OpenJDK for Java 7 (第336-344行)或OpenJDK for Java 8 (第265-273行):在某些情况下,他们已经做了一些签名修复。 Whereas there is no such thing done for org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner#engineVerify , moreover, in org.bouncycastle.crypto.signers.DSASigner#verifySignature it is explicitly stated that numbers must be positive, otherwise verification fails straight away. 而没有用于做这样的事情org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner#engineVerify ,而且,在org.bouncycastle.crypto.signers.DSASigner#verifySignature则明确指出,数字必须为正,否则验证直接失败了。

Is it a bug in BC, or is there something that I missed? 这是BC的一个错误,还是我错过了什么? To overcome this, I have subclassed org.bouncycastle.crypto.signers.DSASigner and added there the same aforementioned signature fix, then plugged this in as yet another signature algorithm (through subclassing org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner ). 为了解决这个问题,我已经将org.bouncycastle.crypto.signers.DSASigner子类org.bouncycastle.crypto.signers.DSASigner并在那里添加了相同的上述签名修复,然后将其作为另一个签名算法插入(通过子类化org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner )。 But maybe there is another way that I overlooked, and this "issue" is well-known? 但也许我忽略了另一种方式,这个“问题”是众所周知的? Please advise. 请指教。

If the incorrect BER/DER encoding of ASN.1 integers - which are stored as signed big endian, right aligned octets - is indeed the culprit then Bouncy does not have a bug. 如果ASN.1整数的不正确的BER / DER编码-这是存储为签署大端,右对齐八位字节-的确是罪魁祸首那么快活没有一个bug。 Positive values should be left padded with a 00 valued byte if the first bit of the encoding is set, otherwise it would represent a negative value. 如果设置了编码的第一位,则应将正值填充为00值的字节,否则它将表示负值。

The Sun provider is wrong to allow those kind of signatures to verify, and the other party is of course generating invalid signatures. Sun提供商错误地允许这些签名进行验证,而另一方当然会生成无效签名。 Note that it is possible to let the signatures verify without this "fix" within the Sun code: simply adjust the encoding before feeding it to the verification function. 请注意,可以让签名在Sun代码中没有“修复”的情况下进行验证:只需在将编码提供给验证功能之前调整编码即可。

The only time when this is not possible is when the DSA verification is called as a generic signature verification method from another library instead of from an application that can adjust the data before the call. 唯一不可能的情况是,DSA验证被称为来自另一个库的通用签名验证方法,而不是来自可以在调用之前调整数据的应用程序。

On the other hand, I think you've created an elegant fix. 另一方面,我认为你已经创造了一个优雅的解决方案。 The only issue with it is that it may not run if the provider's signature is verified from a JCA compliant framework. 唯一的问题是,如果从符合JCA的框架验证提供者的签名,它可能无法运行。 The other possible fix is to re-encode before feeding it into the Signature class for verification. 另一种可能的解决方法是将其提供给Signature类进行验证之前重新编码。

Note that I don't see how this could be a security issue; 请注意,我不知道这可能是一个安全问题; the signature consists of the values of R and S, and it does not matter how they are encoded, as long as you receive the correct values in the end. 签名由R和S的值组成,只要您在最后收到正确的值,它们的编码方式无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM