Expand description
Set of types for supporting CBOR Object Signing and Encryption (COSE).
Builds on the ciborium
crate for underlying CBOR support.
§Usage
use coset::{iana, CborSerializable};
// Inputs.
let pt = b"This is the content";
let aad = b"this is additional data";
// Build a `CoseSign1` object.
let protected = coset::HeaderBuilder::new()
.algorithm(iana::Algorithm::ES256)
.key_id(b"11".to_vec())
.build();
let sign1 = coset::CoseSign1Builder::new()
.protected(protected)
.payload(pt.to_vec())
.create_signature(aad, |pt| signer.sign(pt)) // closure to do sign operation
.build();
// Serialize to bytes.
let sign1_data = sign1.to_vec().unwrap();
println!(
"'{}' + '{}' => {}",
String::from_utf8_lossy(pt),
String::from_utf8_lossy(aad),
hex::encode(&sign1_data)
);
// At the receiving end, deserialize the bytes back to a `CoseSign1` object.
let mut sign1 = coset::CoseSign1::from_slice(&sign1_data).unwrap();
// At this point, real code would validate the protected headers.
// Check the signature, which needs to have the same `aad` provided, by
// providing a closure that can do the verify operation.
let result = sign1.verify_signature(aad, |sig, data| verifier.verify(sig, data));
println!("Signature verified: {:?}.", result);
assert!(result.is_ok());
// Changing an unprotected header leaves the signature valid.
sign1.unprotected.content_type = Some(coset::ContentType::Text("text/plain".to_owned()));
assert!(sign1
.verify_signature(aad, |sig, data| verifier.verify(sig, data))
.is_ok());
// Providing a different `aad` means the signature won't validate.
assert!(sign1
.verify_signature(b"not aad", |sig, data| verifier.verify(sig, data))
.is_err());
// Changing a protected header invalidates the signature.
sign1.protected.original_data = None;
sign1.protected.header.content_type = Some(coset::ContentType::Text("text/plain".to_owned()));
assert!(sign1
.verify_signature(aad, |sig, data| verifier.verify(sig, data))
.is_err());
Re-exports§
pub use ciborium as cbor;
Modules§
Structs§
- Cose
Encrypt - Structure representing an encrypted object.
- Cose
Encrypt0 - Structure representing an encrypted object.
- Cose
Encrypt0 Builder - Builder for
CoseEncrypt0
objects. - Cose
Encrypt Builder - Builder for
CoseEncrypt
objects. - Cose
KdfContext - Structure representing a a key derivation context.
- Cose
KdfContext Builder - Builder for
CoseKdfContext
objects. - CoseKey
- Structure representing a cryptographic key.
- Cose
KeyBuilder - Builder for
CoseKey
objects. - Cose
KeySet - A collection of
CoseKey
objects. - CoseMac
- Structure representing a message with authentication code (MAC).
- Cose
Mac0 - Structure representing a message with authentication code (MAC) where the relevant key is implicit.
- Cose
Mac0 Builder - Builder for
CoseMac0
objects. - Cose
MacBuilder - Builder for
CoseMac
objects. - Cose
Recipient - Structure representing the recipient of encrypted data.
- Cose
Recipient Builder - Builder for
CoseRecipient
objects. - Cose
Sign - Signed payload with signatures.
- Cose
Sign1 - Signed payload with a single signature.
- Cose
Sign1 Builder - Builder for
CoseSign1
objects. - Cose
Sign Builder - Builder for
CoseSign
objects. - Cose
Signature - Structure representing a cryptographic signature.
- Cose
Signature Builder - Builder for
CoseSignature
objects. - EndOf
File - Marker structure indicating that the EOF was encountered when reading CBOR data.
- Header
- Structure representing a common COSE header map.
- Header
Builder - Builder for
Header
objects. - Party
Info - Structure representing a party involved in key derivation.
- Party
Info Builder - Builder for
PartyInfo
objects. - Protected
Header - Structure representing a protected COSE header map.
- Supp
PubInfo - Structure representing supplemental public information.
- Supp
PubInfo Builder - Builder for
SuppPubInfo
objects.
Enums§
- Cbor
Ordering - Indicate which ordering should be applied to CBOR values.
- Cose
Error - Error type for failures in encoding or decoding COSE types.
- Encryption
Context - Possible encryption contexts.
- Label
- A COSE label may be either a signed integer value or a string.
- MacContext
- Possible MAC contexts.
- Nonce
- A nonce value.
- Registered
Label - A COSE label which can be either a signed integer value or a string, but where the allowed integer values are governed by IANA.
- Registered
Label With Private - A COSE label which can be either a signed integer value or a string, and where the allowed integer values are governed by IANA but include a private use range.
- Signature
Context - Possible signature contexts.
Traits§
- AsCbor
Value - Trait for types that can be converted to/from a
Value
. - Cbor
Serializable - Extension trait that adds serialization/deserialization methods.
- Tagged
Cbor Serializable - Extension trait that adds tagged serialization/deserialization methods.
Functions§
- enc_
structure_ data - Create a binary blob that will be signed.
- mac_
structure_ data - Create a binary blob that will be signed.
- sig_
structure_ data - Create a binary blob that will be signed.
Type Aliases§
- Algorithm
- Algorithm identifier.
- Content
Type - Content type.
- KeyOperation
- Key operation.
- KeyType
- Key type.
- Result
- Crate-specific Result type