cms: CHOICE types in `SET OF` are ordered as `SEQUENCE OF INTEGER`, not by their bytes

未关闭 适合新手
#2,538 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@brother-z 已经在做这个了。

开始于 2026年11月15日。

  • #2539 来自 @brother-z —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
cryptography

调研方向

issue 列出了 cert.rs、enveloped_data.rs、revocation.rs 和 signed_data.rs 中的四个 ValueOrd 实现。阅读每个实现如何比较 DER 编码,并根据 issue 中描述的字节串比较来检查排序。完成的标准是:受影响的 CMS 集合按其编码后的字节排序,解码后再重新编码仍能保留 DER 顺序。

由索引模型根据 Issue 内容生成。

描述

Found it while playing with EST server implementation and parsing example certs from RFC 7030 Appendix A.1 for /cacerts response. I encountered it in cert.rs, but same thing happens in enveloped_data.rs, revocation.rs and signed_data.rs.

Tl;dr: The comparison starts from correctly comparing headers, but if they're the same (same length certs) it tries to compare values of CHOICE and there it treats the whole blob (header included) as SEQUENCE OF INTEGER, makes a new, wrong header that can lie about length because bytes >=0x80 will get long encoding and then sorting falls apart.

The ValueOrd impls of CertificateChoices, RevocationInfoChoice, RecipientInfo and SignerIdentifier encode both values and compare the results with der_cmp:

self.to_der()?.der_cmp(&other.to_der()?)

to_der() returns Vec<u8>, and Vec<u8> gets DerOrd from the Vec<T> SEQUENCE OF impl, with each u8 as an INTEGER. So this compares the two encodings as if they were SEQUENCE OF INTEGER values. X.690 11.6 orders SET OF elements by comparing their encodings as octet strings, which for complete DER TLVs is Vec<u8>::cmp.

The two disagree:

use core::cmp::Ordering;
use der::DerOrd;

let a = vec![0x00u8, 0x80];
let b = vec![0x01u8, 0x00];
assert_eq!(a.cmp(&b), Ordering::Less);
assert_eq!(a.der_cmp(&b).unwrap(), Ordering::Less); // fails: Greater

As SEQUENCE OF INTEGER, a becomes 30 07 02 01 00 02 02 00 80 and b becomes 30 06 02 01 01 02 01 00. der_cmp compares these new headers first and gives wrong order.

SetOfVec sorts with this order when it is built and when it is decoded. So cms encodes CertificateSet, RevocationInfoChoices, RecipientInfos and SignerInfos (SignerInfo's derived ordering compares sid) in an order that is not DER, and decoding a correctly ordered set and encoding it again can change its bytes.

Example: the /cacerts response in RFC 7030 Appendix A.1 has four certificates. Decoded with cms, SignedData.certificates comes out as

NwN, OwO, OwN, NwO

The byte order is

OwO, NwN, NwO, OwN

(The RFC's own encoding uses neither order, so it isn't DER either.)

OwO and NwN are both 767 bytes, and NwO and OwN are both 775, yet each pair comes out swapped: their SEQUENCE OF INTEGER lengths differ (OwO 2599, NwN 2594, NwO 2640, OwN 2626).

Changing the four impls to Ok(self.to_der()?.cmp(&other.to_der()?)) fixes it. PR to follow.

主要语言
Rust
星标
347
派生
200
平均合并
20 小时 27 分钟
30 天内合并 PR
35

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RustCrypto/formats 的其他 Issue

查看 RustCrypto/formats 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。