Skip to content

Commit

Permalink
der: don't validate SetOf ordering after sorting (#1481)
Browse files Browse the repository at this point in the history
Closes #1236
  • Loading branch information
tarcieri authored Aug 18, 2024
1 parent b1cc2be commit c9773d6
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions der/src/asn1/set_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ 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())?;
validate(result.inner.as_ref())?;
Ok(result)
})
}
Expand Down Expand Up @@ -339,7 +339,6 @@ where
}

der_sort(inner.as_mut())?;
validate(inner.as_ref())?;
Ok(Self { inner })
})
}
Expand Down Expand Up @@ -473,27 +472,6 @@ fn der_sort<T: DerOrd>(slice: &mut [T]) -> Result<(), Error> {
Ok(())
}

/// Validate the elements of a `SET OF`, ensuring that they are all in order
/// and that there are no duplicates.
fn validate<T: DerOrd>(slice: &[T]) -> Result<(), Error> {
if let Some(len) = slice.len().checked_sub(1) {
for i in 0..len {
let j = i.checked_add(1).ok_or(ErrorKind::Overflow)?;

match slice.get(i..=j) {
Some([a, b]) => {
if a.der_cmp(b)? != Ordering::Less {
return Err(ErrorKind::SetOrdering.into());
}
}
_ => return Err(Tag::Set.value_error()),
}
}
}

Ok(())
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
Expand Down

0 comments on commit c9773d6

Please sign in to comment.