Skip to content

Commit 848a2eb

Browse files
committed
x509-cert: use the shortest name when looking attr OID
Before this commit, the string serialization will format some attributes with the longer name when the same OID was provided by two RFC. For example this would use `STATEORPROVINCENAME` instead of `ST` for oid 2.5.4.8
1 parent 04c847e commit 848a2eb

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

x509-cert/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ edition = "2021"
1515
rust-version = "1.65"
1616

1717
[dependencies]
18-
const-oid = { version = "0.9.2", features = ["db"] } # TODO: path = "../const-oid"
18+
const-oid = { version = "0.9.3", features = ["db"] }
1919
der = { version = "0.7.6", features = ["alloc", "derive", "flagset", "oid"] }
2020
spki = { version = "0.7.2", features = ["alloc"] }
2121

x509-cert/src/attr.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use alloc::vec::Vec;
44
use const_oid::db::{
55
rfc4519::{COUNTRY_NAME, DOMAIN_COMPONENT, SERIAL_NUMBER},
6-
DB,
6+
Database, DB,
77
};
88
use core::{
99
fmt::{self, Write},
@@ -260,7 +260,7 @@ impl fmt::Display for AttributeTypeAndValue {
260260
_ => None,
261261
};
262262

263-
if let (Some(key), Some(val)) = (DB.by_oid(&self.oid), val) {
263+
if let (Some(key), Some(val)) = (DB.shortest_name_by_oid(&self.oid), val) {
264264
write!(f, "{}=", key.to_ascii_uppercase())?;
265265

266266
let mut iter = val.char_indices().peekable();
@@ -285,3 +285,26 @@ impl fmt::Display for AttributeTypeAndValue {
285285
Ok(())
286286
}
287287
}
288+
289+
/// Helper trait to bring shortest name by oid lookups to Database
290+
trait ShortestName {
291+
fn shortest_name_by_oid(&self, oid: &ObjectIdentifier) -> Option<&str>;
292+
}
293+
294+
impl<'a> ShortestName for Database<'a> {
295+
fn shortest_name_by_oid(&self, oid: &ObjectIdentifier) -> Option<&'a str> {
296+
let mut best_match: Option<&'a str> = None;
297+
298+
for m in self.find_names_for_oid(*oid) {
299+
if let Some(previous) = best_match {
300+
if m.len() < previous.len() {
301+
best_match = Some(m);
302+
}
303+
} else {
304+
best_match = Some(m);
305+
}
306+
}
307+
308+
best_match
309+
}
310+
}

x509-cert/tests/name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn decode_name() {
8585
let rdn1 = Name::from_der(&hex!("3081c0310b30090603550406130255533113301106035504080c0a43616c69666f726e69613116301406035504070c0d4d6f756e7461696e205669657731133011060355040a0c0a476f6f676c65204c4c43311e301c06035504030c154f51464176444e4457732e676f6f676c652e636f6d31243022060355040b0c1b6d616e6167656d656e743a64732e67726f75702e3338393131313131293027060a0992268993f22c6401010c196964656e746974793a64732e67726f75702e33383931313131")[..]);
8686
let rdn1a = rdn1.unwrap();
8787
let name = rdn1a.to_string();
88-
assert_eq!(name, "UID=identity:ds.group.3891111,OU=management:ds.group.3891111,CN=OQFAvDNDWs.google.com,O=Google LLC,L=Mountain View,STATEORPROVINCENAME=California,C=US");
88+
assert_eq!(name, "UID=identity:ds.group.3891111,OU=management:ds.group.3891111,CN=OQFAvDNDWs.google.com,O=Google LLC,L=Mountain View,ST=California,C=US");
8989
}
9090
}
9191

0 commit comments

Comments
 (0)