1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
//! COSE_Key functionality.
use crate::{
cbor::value::Value,
common::{AsCborValue, CborOrdering},
iana,
iana::EnumI64,
util::{to_cbor_array, ValueTryAs},
Algorithm, CoseError, Label, Result,
};
use alloc::{collections::BTreeSet, vec, vec::Vec};
#[cfg(test)]
mod tests;
/// Key type.
pub type KeyType = crate::RegisteredLabel<iana::KeyType>;
impl Default for KeyType {
fn default() -> Self {
KeyType::Assigned(iana::KeyType::Reserved)
}
}
/// Key operation.
pub type KeyOperation = crate::RegisteredLabel<iana::KeyOperation>;
/// A collection of [`CoseKey`] objects.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CoseKeySet(pub Vec<CoseKey>);
impl crate::CborSerializable for CoseKeySet {}
impl AsCborValue for CoseKeySet {
fn from_cbor_value(value: Value) -> Result<Self> {
Ok(Self(
value.try_as_array_then_convert(CoseKey::from_cbor_value)?,
))
}
fn to_cbor_value(self) -> Result<Value> {
to_cbor_array(self.0)
}
}
/// Structure representing a cryptographic key.
///
/// ```cddl
/// COSE_Key = {
/// 1 => tstr / int, ; kty
/// ? 2 => bstr, ; kid
/// ? 3 => tstr / int, ; alg
/// ? 4 => [+ (tstr / int) ], ; key_ops
/// ? 5 => bstr, ; Base IV
/// * label => values
/// }
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CoseKey {
/// Key type identification.
pub kty: KeyType,
/// Key identification.
pub key_id: Vec<u8>,
/// Key use restriction to this algorithm.
pub alg: Option<Algorithm>,
/// Restrict set of possible operations.
pub key_ops: BTreeSet<KeyOperation>,
/// Base IV to be xor-ed with partial IVs.
pub base_iv: Vec<u8>,
/// Any additional parameter (label,value) pairs. If duplicate labels are present,
/// CBOR-encoding will fail.
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);
const KID: Label = Label::Int(iana::KeyParameter::Kid as i64);
const ALG: Label = Label::Int(iana::KeyParameter::Alg as i64);
const KEY_OPS: Label = Label::Int(iana::KeyParameter::KeyOps as i64);
const BASE_IV: Label = Label::Int(iana::KeyParameter::BaseIv as i64);
impl AsCborValue for CoseKey {
fn from_cbor_value(value: Value) -> Result<Self> {
let m = value.try_as_map()?;
let mut key = Self::default();
let mut seen = BTreeSet::new();
for (l, value) in m.into_iter() {
// The `ciborium` CBOR library does not police duplicate map keys.
// RFC 8152 section 14 requires that COSE does police duplicates, so do it here.
let label = Label::from_cbor_value(l)?;
if seen.contains(&label) {
return Err(CoseError::DuplicateMapKey);
}
seen.insert(label.clone());
match label {
KTY => key.kty = KeyType::from_cbor_value(value)?,
KID => {
key.key_id = value.try_as_nonempty_bytes()?;
}
ALG => key.alg = Some(Algorithm::from_cbor_value(value)?),
KEY_OPS => {
let key_ops = value.try_as_array()?;
for key_op in key_ops.into_iter() {
if !key.key_ops.insert(KeyOperation::from_cbor_value(key_op)?) {
return Err(CoseError::UnexpectedItem(
"repeated array entry",
"unique array label",
));
}
}
if key.key_ops.is_empty() {
return Err(CoseError::UnexpectedItem("empty array", "non-empty array"));
}
}
BASE_IV => {
key.base_iv = value.try_as_nonempty_bytes()?;
}
label => key.params.push((label, value)),
}
}
// Check that key type has been set.
if key.kty == KeyType::Assigned(iana::KeyType::Reserved) {
return Err(CoseError::UnexpectedItem(
"no kty label",
"mandatory kty label",
));
}
Ok(key)
}
fn to_cbor_value(self) -> Result<Value> {
let mut map: Vec<(Value, Value)> = vec![(KTY.to_cbor_value()?, self.kty.to_cbor_value()?)];
if !self.key_id.is_empty() {
map.push((KID.to_cbor_value()?, Value::Bytes(self.key_id)));
}
if let Some(alg) = self.alg {
map.push((ALG.to_cbor_value()?, alg.to_cbor_value()?));
}
if !self.key_ops.is_empty() {
map.push((KEY_OPS.to_cbor_value()?, to_cbor_array(self.key_ops)?));
}
if !self.base_iv.is_empty() {
map.push((BASE_IV.to_cbor_value()?, Value::Bytes(self.base_iv)));
}
let mut seen = BTreeSet::new();
for (label, value) in self.params {
if seen.contains(&label) {
return Err(CoseError::DuplicateMapKey);
}
seen.insert(label.clone());
map.push((label.to_cbor_value()?, value));
}
Ok(Value::Map(map))
}
}
/// Builder for [`CoseKey`] objects.
#[derive(Debug, Default)]
pub struct CoseKeyBuilder(CoseKey);
impl CoseKeyBuilder {
builder! {CoseKey}
builder_set! {kty: KeyType}
builder_set! {key_id: Vec<u8>}
builder_set! {base_iv: Vec<u8>}
/// Constructor for an elliptic curve public key specified by `x` and `y` coordinates.
pub fn new_ec2_pub_key(curve: iana::EllipticCurve, x: Vec<u8>, y: Vec<u8>) -> Self {
Self(CoseKey {
kty: KeyType::Assigned(iana::KeyType::EC2),
params: vec![
(
Label::Int(iana::Ec2KeyParameter::Crv as i64),
Value::from(curve as u64),
),
(Label::Int(iana::Ec2KeyParameter::X as i64), Value::Bytes(x)),
(Label::Int(iana::Ec2KeyParameter::Y as i64), Value::Bytes(y)),
],
..Default::default()
})
}
/// Constructor for an elliptic curve public key specified by `x` coordinate plus sign of `y`
/// coordinate.
pub fn new_ec2_pub_key_y_sign(curve: iana::EllipticCurve, x: Vec<u8>, y_sign: bool) -> Self {
Self(CoseKey {
kty: KeyType::Assigned(iana::KeyType::EC2),
params: vec![
(
Label::Int(iana::Ec2KeyParameter::Crv as i64),
Value::from(curve as u64),
),
(Label::Int(iana::Ec2KeyParameter::X as i64), Value::Bytes(x)),
(
Label::Int(iana::Ec2KeyParameter::Y as i64),
Value::Bool(y_sign),
),
],
..Default::default()
})
}
/// Constructor for an elliptic curve private key specified by `d`, together with public `x` and
/// `y` coordinates.
pub fn new_ec2_priv_key(
curve: iana::EllipticCurve,
x: Vec<u8>,
y: Vec<u8>,
d: Vec<u8>,
) -> Self {
let mut builder = Self::new_ec2_pub_key(curve, x, y);
builder
.0
.params
.push((Label::Int(iana::Ec2KeyParameter::D as i64), Value::Bytes(d)));
builder
}
/// Constructor for a symmetric key specified by `k`.
pub fn new_symmetric_key(k: Vec<u8>) -> Self {
Self(CoseKey {
kty: KeyType::Assigned(iana::KeyType::Symmetric),
params: vec![(
Label::Int(iana::SymmetricKeyParameter::K as i64),
Value::Bytes(k),
)],
..Default::default()
})
}
/// Constructor for a octet keypair key.
pub fn new_okp_key() -> Self {
Self(CoseKey {
kty: KeyType::Assigned(iana::KeyType::OKP),
..Default::default()
})
}
/// Set the key type.
#[must_use]
pub fn key_type(mut self, key_type: iana::KeyType) -> Self {
self.0.kty = KeyType::Assigned(key_type);
self
}
/// Set the algorithm.
#[must_use]
pub fn algorithm(mut self, alg: iana::Algorithm) -> Self {
self.0.alg = Some(Algorithm::Assigned(alg));
self
}
/// Add a key operation.
#[must_use]
pub fn add_key_op(mut self, op: iana::KeyOperation) -> Self {
self.0.key_ops.insert(KeyOperation::Assigned(op));
self
}
/// Set a parameter value.
///
/// # Panics
///
/// This function will panic if it used to set a parameter label from the [`iana::KeyParameter`]
/// range.
#[must_use]
pub fn param(mut self, label: i64, value: Value) -> Self {
if iana::KeyParameter::from_i64(label).is_some() {
panic!("param() method used to set KeyParameter"); // safe: invalid input
}
self.0.params.push((Label::Int(label), value));
self
}
}