aboutsummaryrefslogtreecommitdiff
path: root/src/key/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/key/mod.rs')
-rw-r--r--src/key/mod.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/key/mod.rs b/src/key/mod.rs
index 07ee7a7..0df7d31 100644
--- a/src/key/mod.rs
+++ b/src/key/mod.rs
@@ -18,7 +18,7 @@
use crate::{
cbor::value::Value,
- common::AsCborValue,
+ common::{AsCborValue, CborOrdering},
iana,
iana::EnumI64,
util::{to_cbor_array, ValueTryAs},
@@ -88,6 +88,26 @@ pub struct CoseKey {
pub params: Vec<(Label, Value)>,
}
+impl CoseKey {
+ /// Re-order the contents of the key so that the contents will be emitted in one of the standard
+ /// CBOR sorted orders.
+ pub fn canonicalize(&mut self, ordering: CborOrdering) {
+ // The keys that are represented as named fields CBOR-encode as single bytes 0x01 - 0x05,
+ // which sort before any other CBOR values (other than 0x00) in either sorting scheme:
+ // - In length-first sorting, a single byte sorts before anything multi-byte and 1-5 sorts
+ // before any other value.
+ // - In encoded-lexicographic sorting, there are no valid CBOR-encoded single values that
+ // start with a byte in the range 0x01 - 0x05 other than the values 1-5.
+ // So we only need to sort the `params`.
+ match ordering {
+ CborOrdering::Lexicographic => self.params.sort_by(|l, r| l.0.cmp(&r.0)),
+ CborOrdering::LengthFirstLexicographic => {
+ self.params.sort_by(|l, r| l.0.cmp_canonical(&r.0))
+ }
+ }
+ }
+}
+
impl crate::CborSerializable for CoseKey {}
const KTY: Label = Label::Int(iana::KeyParameter::Kty as i64);