Skip to content

Commit

Permalink
Resolve CWT tagged and COSE-untagged messages
Browse files Browse the repository at this point in the history
This makes a few more tests pass.

Co-authored-by: Tommaso Allevi <tomallevi@gmail.com>
  • Loading branch information
dodomorandi and allevo committed Nov 27, 2021
1 parent c6ca6f6 commit ff6ae22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/cwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::convert::{TryFrom, TryInto};
use thiserror::Error;

const COSE_SIGN1_CBOR_TAG: u64 = 18;
const CBOR_WEB_TOKEN_TAG: u64 = 61;
const COSE_HEADER_KEY_KID: i128 = 4;
const COSE_HEADER_KEY_ALG: i128 = 1;
const COSE_ECDSA256: i128 = -7;
Expand Down Expand Up @@ -157,12 +158,19 @@ impl TryFrom<&[u8]> for Cwt {
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
use CwtParseError::*;

let cwt: Value = ciborium::de::from_reader(data)?;
let (tag_id, cwt_content) = cwt.as_tag().ok_or(InvalidRootValue)?;
if tag_id != COSE_SIGN1_CBOR_TAG {
return Err(InvalidTag(tag_id));
}
let parts = cwt_content.as_array().ok_or(InvalidParts)?;
let cwt_content = match ciborium::de::from_reader(data)? {
Value::Tag(tag_id, content) if tag_id == CBOR_WEB_TOKEN_TAG => *content,
cwt => cwt,
};
let cwt_content = match cwt_content {
Value::Tag(COSE_SIGN1_CBOR_TAG, content) => *content,
Value::Tag(tag_id, _) => return Err(InvalidTag(tag_id)),
cwt => cwt,
};
let parts = match cwt_content {
Value::Array(parts) => parts,
_ => return Err(InvalidParts),
};
if parts.len() != 4 {
return Err(InvalidPartsCount(parts.len()));
}
Expand Down
4 changes: 0 additions & 4 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use std::path::PathBuf;
#[case::common_2dcode_raw_co21_json("common/2DCode/raw/CO21.json")]
#[case::common_2dcode_raw_co22_json("common/2DCode/raw/CO22.json")]
#[case::common_2dcode_raw_co23_json("common/2DCode/raw/CO23.json")]
#[ignore = "SKIPPED: invalid/missing cose tag. See #22"]
#[case::common_2dcode_raw_co28_json("common/2DCode/raw/CO28.json")]
#[case::common_2dcode_raw_co3_json("common/2DCode/raw/CO3.json")]
#[case::common_2dcode_raw_co5_json("common/2DCode/raw/CO5.json")]
Expand Down Expand Up @@ -129,11 +128,8 @@ use std::path::PathBuf;
#[case::es_2dcode_raw_1101_json("ES/2DCode/raw/1101.json")]
#[case::es_2dcode_raw_1102_json("ES/2DCode/raw/1102.json")]
#[case::es_2dcode_raw_1103_json("ES/2DCode/raw/1103.json")]
#[ignore = "invalid/missing cose tag. See #22"]
#[case::es_2dcode_raw_1501_json("ES/2DCode/raw/1501.json")]
#[ignore = "invalid/missing cose tag. See #22"]
#[case::es_2dcode_raw_1502_json("ES/2DCode/raw/1502.json")]
#[ignore = "invalid/missing cose tag. See #22"]
#[case::es_2dcode_raw_1503_json("ES/2DCode/raw/1503.json")]
#[case::es_2dcode_raw_201_json("ES/2DCode/raw/201.json")]
#[case::es_2dcode_raw_202_json("ES/2DCode/raw/202.json")]
Expand Down

0 comments on commit ff6ae22

Please sign in to comment.