| 1 |
//! This crate implements [COSE](https://tools.ietf.org/html/rfc8152) signature |
| 2 |
//! parsing. Verification has to be performed by the caller. |
| 3 |
//! |
| 4 |
//! Example usage: Let `payload` and `cose_signature` be variables holding the |
| 5 |
//! signed payload and the COSE signature bytes respectively. |
| 6 |
//! Let further `verify_callback` be a function callback that implements |
| 7 |
//! signature verification. |
| 8 |
//! |
| 9 |
//!```rust,ignore |
| 10 |
//! use cose::decoder::decode_signature; |
| 11 |
//! |
| 12 |
//! // Parse the incoming signature. |
| 13 |
//! let cose_signatures = decode_signature(cose_signature, &payload); |
| 14 |
//! let cose_signatures = match cose_signatures { |
| 15 |
//! Ok(signature) => signature, |
| 16 |
//! Err(_) => Vec::new(), |
| 17 |
//! }; |
| 18 |
//! if cose_signatures.len() < 1 { |
| 19 |
//! return false; |
| 20 |
//! } |
| 21 |
//! |
| 22 |
//! let mut result = true; |
| 23 |
//! for cose_signature in cose_signatures { |
| 24 |
//! // Call callback to verify the parsed signatures. |
| 25 |
//! result &= verify_callback(cose_signature); |
| 26 |
//! |
| 27 |
//! // We can stop early. The cose_signature is not valid. |
| 28 |
//! if !result { |
| 29 |
//! return result; |
| 30 |
//! } |
| 31 |
//! } |
| 32 |
//!``` |
| 33 |
extern crate moz_cbor as cbor; |
| 34 |
|
| 35 |
pub mod decoder; |
| 36 |
pub mod util; |
| 37 |
|
| 38 |
/// Errors that can be returned from COSE functions. |
| 39 |
#[derive(Debug, PartialEq)] |
| 40 |
pub enum CoseError { |
| 41 |
DecodingFailure, |
| 42 |
LibraryFailure, |
| 43 |
MalformedInput, |
| 44 |
MissingHeader, |
| 45 |
UnexpectedHeaderValue, |
| 46 |
UnexpectedTag, |
| 47 |
UnexpectedType, |
| 48 |
Unimplemented, |
| 49 |
VerificationFailed, |
| 50 |
UnknownSignatureScheme, |
| 51 |
SigningFailed, |
| 52 |
InvalidArgument, |
| 53 |
} |
| 54 |
|
| 55 |
/// An enum identifying supported signature algorithms. |
| 56 |
/// Currently ES256 (ECDSA with P256 and SHA256), ES384 (ECDSA with P384 and SHA384) |
| 57 |
/// ES512 (ECDSA with P521 and SHA512), and PS256 (RSASSA-PSS with SHA256) |
| 58 |
/// are supported. Note that with PS256, the salt length is defined |
| 59 |
/// to be 32 bytes. |
| 60 |
#[derive(Debug)] |
| 61 |
#[derive(PartialEq)] |
| 62 |
pub enum SignatureAlgorithm { |
| 63 |
ES256, |
| 64 |
ES384, |
| 65 |
ES512, |
| 66 |
PS256, |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(test)] |
| 70 |
mod test_setup; |
| 71 |
#[cfg(test)] |
| 72 |
mod test_cose; |