• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use ciborium::de::Error as CbError;
2 use coset::cbor;
3 use coset::CoseError;
4 use coset::CoseKey;
5 use kmr_wire::read_to_value;
6 
7 use crate::cdi::ConfigurationDescriptor;
8 use crate::get_cbor_bytes_from_map;
9 use crate::get_cbor_string_from_map;
10 
11 #[allow(dead_code)]
12 #[derive(Default, Clone)]
13 pub struct CwtClaims {
14     pub iss: String,
15     pub sub: String,
16     pub code_hash: [u8; 32],
17     pub cfg_hash: [u8; 32],
18     pub cfg_descr: ConfigurationDescriptor,
19     pub auth_hash: [u8; 32],
20     pub mode: [u8; 1],
21     pub subject_pk: CoseKey,
22     pub key_usage: [u8; 1],
23     pub profile_name: String,
24 }
25 
26 const ISS_LABEL: i32 = 1;
27 const SUB_LABEL: i32 = 2;
28 const CODE_HASH_LABEL: i32 = -4670545;
29 const CFG_HASH_LABEL: i32 = -4670547;
30 const CFG_DESCR_LABEL: i32 = -4670548;
31 const AUTH_HASH_LABEL: i32 = -4670549;
32 const MODE_LABEL: i32 = -4670551;
33 const SUBJECT_PK_LABEL: i32 = -4670552;
34 const KEY_USAGE_LABEL: i32 = -4670553;
35 const PROFILE_NAME_LABEL: i32 = -4670554;
36 
37 impl coset::AsCborValue for CwtClaims {
from_cbor_value(value: cbor::value::Value) -> coset::Result<Self>38     fn from_cbor_value(value: cbor::value::Value) -> coset::Result<Self> {
39         if let Some(vals) = value.as_map() {
40             let cdi = get_cbor_bytes_from_map(vals, CFG_DESCR_LABEL)?;
41             let cdi =
42                 read_to_value(cdi).map_err(|_| CoseError::DecodeFailed(CbError::Syntax(0)))?;
43             let cdi = ConfigurationDescriptor::from_cbor_value(cdi.clone())?;
44 
45             let subject_pk = get_cbor_bytes_from_map(vals, SUBJECT_PK_LABEL)?;
46             let subject_pk = read_to_value(subject_pk)
47                 .map_err(|_| CoseError::DecodeFailed(CbError::Syntax(0)))?;
48             let subject_pk = CoseKey::from_cbor_value(subject_pk.clone())?;
49 
50             let cwt = CwtClaims {
51                 iss: get_cbor_string_from_map(vals, ISS_LABEL)?.to_owned(),
52                 sub: get_cbor_string_from_map(vals, SUB_LABEL)?.to_owned(),
53                 cfg_hash: *get_cbor_bytes_from_map(vals, CFG_HASH_LABEL)?
54                     .first_chunk()
55                     .ok_or(CoseError::DecodeFailed(CbError::Syntax(0)))?,
56                 code_hash: *get_cbor_bytes_from_map(vals, CODE_HASH_LABEL)?
57                     .first_chunk()
58                     .ok_or(CoseError::DecodeFailed(CbError::Syntax(0)))?,
59                 cfg_descr: cdi,
60                 auth_hash: *get_cbor_bytes_from_map(vals, AUTH_HASH_LABEL)?
61                     .first_chunk()
62                     .ok_or(CoseError::DecodeFailed(CbError::Syntax(0)))?,
63                 mode: *get_cbor_bytes_from_map(vals, MODE_LABEL)?
64                     .first_chunk()
65                     .ok_or(CoseError::DecodeFailed(CbError::Syntax(0)))?,
66                 subject_pk,
67                 key_usage: *get_cbor_bytes_from_map(vals, KEY_USAGE_LABEL)?
68                     .first_chunk()
69                     .ok_or(CoseError::DecodeFailed(CbError::Syntax(0)))?,
70 
71                 profile_name: get_cbor_string_from_map(vals, PROFILE_NAME_LABEL)?.to_owned(),
72             };
73 
74             Ok(cwt)
75         } else {
76             Err(CoseError::DecodeFailed(CbError::Syntax(0)))
77         }
78     }
to_cbor_value(self) -> coset::Result<cbor::value::Value>79     fn to_cbor_value(self) -> coset::Result<cbor::value::Value> {
80         Err(CoseError::EncodeFailed)
81     }
82 }
83