Skip to content

Commit 46084f2

Browse files
committed
ssl: preserve OpenSSL OID lookup rules
Keep object-name lookup case-sensitive and canonicalize numeric OID arcs before consulting the generated OpenSSL object table. Assisted-by: OpenAI Codex: GPT-5
1 parent 9a0aaf7 commit 46084f2

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

‎crates/stdlib/src/ssl/oid.rs‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ pub(super) struct OidTable {
4545
nid_to_idx: HashMap<i32, usize>,
4646
/// Short name -> index mapping
4747
short_name_to_idx: HashMap<&'static str, usize>,
48-
/// Long name -> index mapping (case-insensitive)
49-
long_name_to_idx: HashMap<String, usize>,
48+
/// Long name -> index mapping
49+
long_name_to_idx: HashMap<&'static str, usize>,
5050
/// OID string -> index mapping
5151
oid_str_to_idx: HashMap<String, usize>,
5252
}
@@ -64,9 +64,7 @@ impl OidTable {
6464
for (idx, entry) in entries.iter().enumerate() {
6565
nid_to_idx.entry(entry.nid).or_insert(idx);
6666
short_name_to_idx.entry(entry.short_name).or_insert(idx);
67-
long_name_to_idx
68-
.entry(entry.long_name.to_lowercase())
69-
.or_insert(idx);
67+
long_name_to_idx.entry(entry.long_name).or_insert(idx);
7068
if let Some(oid) = entry.oid_string() {
7169
oid_str_to_idx.entry(oid.to_owned()).or_insert(idx);
7270
}
@@ -87,22 +85,45 @@ impl OidTable {
8785

8886
pub(super) fn find_by_oid_string(&self, oid_str: &str) -> Option<&OidEntry> {
8987
self.oid_str_to_idx
90-
.get(oid_str)
88+
.get(canonical_oid(oid_str)?.as_str())
9189
.map(|&idx| &self.entries[idx])
9290
}
9391

9492
pub(super) fn find_by_name(&self, name: &str) -> Option<&OidEntry> {
95-
// Try short name first (exact match)
93+
// OpenSSL object names are case-sensitive. Try the short name first,
94+
// as OBJ_txt2obj does when a short and long name collide.
9695
self.short_name_to_idx
9796
.get(name)
98-
.or_else(|| {
99-
// Try long name (case-insensitive)
100-
self.long_name_to_idx.get(&name.to_lowercase())
101-
})
97+
.or_else(|| self.long_name_to_idx.get(name))
10298
.map(|&idx| &self.entries[idx])
10399
}
104100
}
105101

102+
/// Parse the numeric form accepted by OpenSSL and render it canonically for
103+
/// lookup. The first arc may not have leading zeroes, while later arcs may.
104+
fn canonical_oid(oid: &str) -> Option<String> {
105+
let oid = oid.trim_end_matches(' ');
106+
let oid = oid.strip_suffix('.').unwrap_or(oid);
107+
let mut parts = oid.split('.');
108+
let first = parts.next()?;
109+
if !matches!(first, "0" | "1" | "2") {
110+
return None;
111+
}
112+
113+
let mut canonical = first.to_owned();
114+
let mut count = 1;
115+
for part in parts {
116+
if part.is_empty() || !part.bytes().all(|byte| byte.is_ascii_digit()) {
117+
return None;
118+
}
119+
let part = part.trim_start_matches('0');
120+
canonical.push('.');
121+
canonical.push_str(if part.is_empty() { "0" } else { part });
122+
count += 1;
123+
}
124+
(count >= 2).then_some(canonical)
125+
}
126+
106127
/// Global OID table
107128
static OID_TABLE: rustpython_common::lock::LazyLock<OidTable> =
108129
rustpython_common::lock::LazyLock::new(OidTable::build);
@@ -296,9 +317,16 @@ mod tests {
296317
}
297318

298319
#[test]
299-
fn find_by_name_case_insensitive() {
300-
let entry = find_by_name("COMMONNAME").unwrap();
320+
fn find_by_name_is_case_sensitive() {
321+
assert!(find_by_name("commonName").is_some());
322+
assert!(find_by_name("COMMONNAME").is_none());
323+
}
324+
325+
#[test]
326+
fn find_by_oid_string_normalizes_arcs() {
327+
let entry = find_by_oid_string("2.005.004.003.").unwrap();
301328
assert_eq!(entry.nid, 13);
329+
assert_eq!(entry.oid_string(), Some("2.5.4.3"));
302330
}
303331

304332
#[test]

0 commit comments

Comments
 (0)