Expand description
CBOR Object Signing and Encryption (COSE) implementation based on
coset.
§Usage
use std::borrow::Cow;
use serde::{Serialize, Deserialize};
use ssi_claims_core::{VerifiableClaims, ValidateClaims, VerificationParameters};
use ssi_cose::{CosePayload, ValidateCoseHeader, CoseSignatureBytes, DecodedCoseSign1, CoseKey, key::CoseKeyGenerate};
// Our custom payload type.
#[derive(Serialize, Deserialize)]
struct CustomPayload {
data: String
}
// Define how the payload is encoded in COSE.
impl CosePayload for CustomPayload {
// Serialize the payload as JSON.
fn payload_bytes(&self) -> Cow<[u8]> {
Cow::Owned(serde_json::to_vec(self).unwrap())
}
}
// Define how to validate the COSE header (always valid by default).
impl<P> ValidateCoseHeader<P> for CustomPayload {}
// Define how to validate the payload (always valid by default).
impl<P> ValidateClaims<P, CoseSignatureBytes> for CustomPayload {}
// Create a payload.
let payload = CustomPayload {
data: "Some Data".to_owned()
};
// Create a signature key.
let key = CoseKey::generate_p256(); // requires the `secp256r1` feature.
// Sign the payload!
let bytes = payload.sign(
&key,
true // should the `COSE_Sign1` object be tagged or not.
).await.unwrap();
// Decode the signed COSE object.
let decoded: DecodedCoseSign1<CustomPayload> = bytes
.decode(true)
.unwrap()
.try_map(|_, bytes| serde_json::from_slice(bytes))
.unwrap();
assert_eq!(decoded.signing_bytes.payload.data, "Some Data");
// Verify the signature.
let params = VerificationParameters::from_resolver(&key);
decoded.verify(¶ms).await.unwrap();Re-exports§
Modules§
Structs§
- CoseKey
- Structure representing a cryptographic key.
- Cose
Sign1 - Signed payload with a single signature.
- Cose
Sign1 Bytes - CBOR-encoded
COSE_Sign1object. - Cose
Sign1 Bytes Buf - CBOR-encoded
COSE_Sign1object buffer. - Cose
Signature Bytes - COSE signature bytes.
- Cose
Signer Info - COSE signer information.
- Decoded
Cose Sign1 - Decoded
COSE_Sign1object. - Header
- Structure representing a common COSE header map.
- Payload
Bytes - Payload and bytes.
- Protected
Header - Structure representing a protected COSE header map.
- Unsigned
Cose Sign1 COSE_Sign1object without the signature.
Enums§
- Cbor
Value - A representation of a dynamic CBOR value that can handled dynamically
- Cose
Error - Error type for failures in encoding or decoding COSE types.
- Cose
Payload Type - COSE payload type.
- Cose
Verification Error - Label
- A COSE label may be either a signed integer value or a string.
Constants§
Traits§
- Cose
Payload - COSE payload.
- Cose
Signer - COSE signer.
- Validate
Cose Header
Functions§
- verify_
bytes - Verify a signature using a COSE key and algorithm.
Type Aliases§
- Content
Type - Content type.