Skip to content
Merged
Changes from all commits
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
22 changes: 13 additions & 9 deletions der/src/asn1/set_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
//!
//! # Ordering Notes
//!
//! Some DER serializer implementations fail to properly sort elements of a
//! `SET OF`. This is technically non-canonical, but occurs frequently
//! enough that most DER decoders tolerate it. Unfortunately because
//! of that, we must also follow suit.
//! Some DER serializer implementations fail to properly sort elements of a `SET OF`. This is
//! technically non-canonical, but occurs frequently enough that most DER decoders tolerate it.
//!
//! However, all types in this module sort elements of a set at decode-time,
//! ensuring they'll be in the proper order if reserialized.
//! When decoding with `EncodingRules::Der`, this implementation sorts the elements of `SET OF` at
//! decode-time to ensure reserializations are canonical.

use crate::{
ArrayVec, Decode, DecodeValue, DerOrd, Encode, EncodeValue, Error, ErrorKind, FixedTag, Header,
Expand Down Expand Up @@ -116,8 +114,11 @@ where
result.inner.push(T::decode(reader)?)?;
}

// Ensure elements of the `SetOf` are sorted and will serialize as valid DER
der_sort(result.inner.as_mut())?;
if reader.encoding_rules().is_der() {
// Ensure elements of the `SetOf` are sorted and will serialize as valid DER
der_sort(result.inner.as_mut())?;
}

Ok(result)
}
}
Expand Down Expand Up @@ -339,7 +340,10 @@ where
inner.push(T::decode(reader)?);
}

der_sort(inner.as_mut())?;
if reader.encoding_rules().is_der() {
der_sort(inner.as_mut())?;
}

Ok(Self { inner })
}
}
Expand Down