Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x509-cert: fixup RDN string representation #1126

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion x509-cert/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl FromStr for RdnSequence {
/// [RFC 4514]: https://datatracker.ietf.org/doc/html/rfc4514
impl fmt::Display for RdnSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, atv) in self.0.iter().enumerate() {
// As per RFC 4514 Section 2.1, the elements are reversed
for (i, atv) in self.0.iter().rev().enumerate() {
match i {
0 => write!(f, "{}", atv)?,
_ => write!(f, ",{}", atv)?,
Expand Down
21 changes: 21 additions & 0 deletions x509-cert/tests/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ fn decode_name() {
counter += 1;
}
}

#[cfg(feature = "std")]
{
// https://datatracker.ietf.org/doc/html/rfc4514.html#section-2.1
// If the RDNSequence is an empty sequence, the result is the empty or
// zero-length string.
// Otherwise, the output consists of the string encodings of each
// RelativeDistinguishedName in the RDNSequence (according to Section 2.2),
// starting with the last element of the sequence and moving backwards
// toward the first.
// The encodings of adjoining RelativeDistinguishedNames are separated by
// a comma (',' U+002C) character.
let name = rdn1a.to_string();
assert_eq!(name, "CN=Good CA,O=Test Certificates 2011,C=US");

// https://github.com/RustCrypto/formats/issues/1121
let rdn1 = Name::from_der(&hex!("3081c0310b30090603550406130255533113301106035504080c0a43616c69666f726e69613116301406035504070c0d4d6f756e7461696e205669657731133011060355040a0c0a476f6f676c65204c4c43311e301c06035504030c154f51464176444e4457732e676f6f676c652e636f6d31243022060355040b0c1b6d616e6167656d656e743a64732e67726f75702e3338393131313131293027060a0992268993f22c6401010c196964656e746974793a64732e67726f75702e33383931313131")[..]);
let rdn1a = rdn1.unwrap();
let name = rdn1a.to_string();
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");
}
}

#[test]
Expand Down