无法让 bouncycastle 在 der 编码中正确标记构造的八位字节字符串

发布于 2024-11-27 15:34:29 字数 976 浏览 1 评论 0原文

我有以下要求。我有一个 asn1 类型,需要封装在构造的八位字节字符串(标记为 0x24 的八位字节字符串)中。然后,该结构在 CMS 签名块中进行签名,并且整个 CMS 块需要进行 DER 编码。

我的问题是每次我对 CMS 块进行 DER 编码时,构造的八位字节字符串就会变成(非构造的?)八位字节字符串。我的意思是它的标签从 0x24(我需要的)切换到 0x04。

这是 Bouncycastle 的错误还是我做了不应该做的事情?忽略 CMS 块,似乎每当我尝试对“BerConstructedOctetString”进行编码时,它都不再是构造的八位字节字符串:

BERConstructedOctetString constructed = new BERConstructedOctetString(new DERInteger(3));
ASN1Object nonConstructed= new DEROctetString(new DERInteger(3));

System.out.println(Util.toHex(constructed.getDEREncoded()));
System.out.println(Util.toHex(nonConstructed.getDEREncoded()));
System.out.println(Util.toHex(constructed.getEncoded()));
System.out.println(Util.toHex(nonConstructed.getEncoded()));

具有以下输出:

04:03:02:01:03
04:03:02:01:03
24:80:04:03:02:01:03:00:00
04:03:02:01:03

编辑:这可能吗? bouncycastle 和 not-yet-commons-ssl 是否都包含“BerConstructedOctetString”,在编码为 DER 时将其编码和标记为 0x04?我的两个要求不兼容吗?

I have the following requirements. I have an asn1 type that needs to be encapsulated in a constructed octet string (octet string tagged as 0x24). This structure is then signed in a CMS signed block and the whole CMS block needs to be DER encoded.

My problem is every time I DER encode the CMS block the constructed octet string becomes a (nonconstructed?) octet string. I mean its tag switches from 0x24 (what I need) to 0x04.

Is this a Bouncycastle bug or am I doing something I shouldn't? Ignoring the CMS block, it appears that anytime I try to der encode a 'BerConstructedOctetString' it no longer is a constructed octetstring:

BERConstructedOctetString constructed = new BERConstructedOctetString(new DERInteger(3));
ASN1Object nonConstructed= new DEROctetString(new DERInteger(3));

System.out.println(Util.toHex(constructed.getDEREncoded()));
System.out.println(Util.toHex(nonConstructed.getDEREncoded()));
System.out.println(Util.toHex(constructed.getEncoded()));
System.out.println(Util.toHex(nonConstructed.getEncoded()));

With the following output:

04:03:02:01:03
04:03:02:01:03
24:80:04:03:02:01:03:00:00
04:03:02:01:03

Edit: Is this even possible? Is there a reason both bouncycastle and not-yet-commons-ssl contain 'BerConstructedOctetString' that encode and tag as 0x04 when encoded to DER? Are my two requirements incompatible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-12-04 15:34:29

我总是访问旧 RSA 实验室的此参考来回答以下问题BER/DER 编码。在这种情况下,请查看第 4 节中的规则#3(DER 编码规则):

3. For simple string types and implicitly tagged types derived from
simple string types, the primitive, definite-length method must be
employed.

如果我正确理解了这一点,则如果您要求 DER 编码,这会强制使用 0x04 进行原始编码。

I always go to this reference from the old RSA Laboratories to answer questions about BER/DER encoding. In this case, look at rule #3 in section 4 (DER encoding rules):

3. For simple string types and implicitly tagged types derived from
simple string types, the primitive, definite-length method must be
employed.

If I read this correctly, this forces the primitive encoding with 0x04 if you ask for DER encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文