Struct minerva_voucher::Voucher
source · [−]pub struct Voucher { /* private fields */ }
Expand description
A structure implementing both “Voucher Request” and “Voucher” artifacts of Constrained BRSKI.
Implementations
sourceimpl Voucher
impl Voucher
sourcepub fn new_vrq() -> Self
pub fn new_vrq() -> Self
Creates a new, empty “Voucher Request” instance.
Examples
use minerva_voucher::Voucher;
let mut vrq = Voucher::new_vrq();
sourcepub fn is_vrq(&self) -> bool
pub fn is_vrq(&self) -> bool
Returns true
if the voucher is a “Voucher Request” instance.
Examples
use minerva_voucher::Voucher;
let mut v = Voucher::new_vrq();
assert!(v.is_vrq());
sourcepub fn get(&self, adisc: AttrDisc) -> Option<&Attr>
pub fn get(&self, adisc: AttrDisc) -> Option<&Attr>
Returns a reference to the attribute in the voucher, if any, that corresponds to the given attribute discriminant value.
Examples
use minerva_voucher::{Voucher, attr::*};
let mut vrq = Voucher::new_vrq();
vrq.set(Attr::CreatedOn(1475868702));
assert_eq!(vrq.get(ATTR_CREATED_ON), Some(&Attr::CreatedOn(1475868702)));
assert_eq!(vrq.get(ATTR_SERIAL_NUMBER), None);
sourcepub fn set(&mut self, attr: Attr) -> &mut Self
pub fn set(&mut self, attr: Attr) -> &mut Self
Adds an attribute to the voucher, replacing the existing attribute, if any, that corresponds to the given one. Returns a mut
reference to the voucher.
Panics
Panics if an invalid voucher attribute is being set.
Examples
use minerva_voucher::{Voucher, attr::*};
let mut vrq = Voucher::new_vrq();
assert_eq!(vrq.get(ATTR_CREATED_ON), None);
vrq.set(Attr::CreatedOn(1475868702));
assert_eq!(vrq.get(ATTR_CREATED_ON), Some(&Attr::CreatedOn(1475868702)));
vrq.set(Attr::CreatedOn(1599086034));
assert_eq!(vrq.get(ATTR_CREATED_ON), Some(&Attr::CreatedOn(1599086034)));
// Panics because `Attr::PinnedDomainPubk` is invalid for a "voucher request".
// vrq.set(Attr::PinnedDomainPubk(vec![]));
sourcepub fn remove(&mut self, adisc: AttrDisc) -> bool
pub fn remove(&mut self, adisc: AttrDisc) -> bool
Removes an attribute from the voucher. Returns whether the attribute was present in the voucher.
Examples
use minerva_voucher::{Voucher, attr::*};
let mut vrq = Voucher::new_vrq();
vrq.set(Attr::CreatedOn(1475868702));
assert_eq!(vrq.remove(ATTR_CREATED_ON), true);
assert_eq!(vrq.remove(ATTR_CREATED_ON), false);
sourcepub fn take(&mut self, adisc: AttrDisc) -> Option<Attr>
pub fn take(&mut self, adisc: AttrDisc) -> Option<Attr>
Removes and returns the attribute in the voucher, if any, that corresponds to the given attribute discriminant value.
Examples
use minerva_voucher::{Voucher, attr::*};
let mut vrq = Voucher::new_vrq();
vrq.set(Attr::CreatedOn(1475868702));
assert_eq!(vrq.take(ATTR_CREATED_ON), Some(Attr::CreatedOn(1475868702)));
assert_eq!(vrq.take(ATTR_CREATED_ON), None);
let sn = b"00-D0-E5-F2-00-02";
vrq.set(Attr::SerialNumber(sn.to_vec()));
assert_eq!(vrq.take(ATTR_SERIAL_NUMBER), Some(Attr::SerialNumber(sn.to_vec())));
assert_eq!(vrq.take(ATTR_SERIAL_NUMBER), None);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of attributes in the voucher.
Examples
use minerva_voucher::{Voucher, attr::Attr};
let mut vrq = Voucher::new_vrq();
assert_eq!(vrq.len(), 0);
vrq.set(Attr::CreatedOn(1475868702));
assert_eq!(vrq.len(), 1);
sourcepub fn iter(&self) -> impl Iterator<Item = &Attr> + '_
pub fn iter(&self) -> impl Iterator<Item = &Attr> + '_
Gets an iterator that visits the attributes in the voucher.
Examples
use minerva_voucher::{Voucher, attr::{Attr, Assertion}};
let mut vrq = Voucher::new_vrq();
vrq.set(Attr::Assertion(Assertion::Proximity))
.set(Attr::CreatedOn(1599086034))
.set(Attr::SerialNumber(b"00-D0-E5-F2-00-02".to_vec()));
let mut vrq_iter = vrq.iter();
assert!(vrq_iter.next().is_some());
assert!(vrq_iter.next().is_some());
assert!(vrq_iter.next().is_some());
assert!(vrq_iter.next().is_none());
sourcepub fn get_signature(&self) -> Option<(&[u8], &SignatureAlgorithm)>
pub fn get_signature(&self) -> Option<(&[u8], &SignatureAlgorithm)>
Returns a tuple of references to the signature and its corresponding algorithm in the voucher, if any.
Examples
use minerva_voucher::{Voucher, SignatureAlgorithm};
use core::convert::TryFrom;
static VCH_F2_00_02: &[u8] = core::include_bytes!(
concat!(env!("CARGO_MANIFEST_DIR"), "/data/00-D0-E5-F2-00-02/voucher_00-D0-E5-F2-00-02.vch"));
let vch = Voucher::new_vch();
assert_eq!(vch.get_signature(), None);
let vch = Voucher::try_from(VCH_F2_00_02).unwrap();
let (signature, alg) = vch.get_signature().unwrap();
assert_eq!(signature.len(), 64);
assert_eq!(*alg, SignatureAlgorithm::ES256);
sourcepub fn serialize(&self) -> Result<Vec<u8>, VoucherError>
pub fn serialize(&self) -> Result<Vec<u8>, VoucherError>
sourcepub fn get_signer_cert(&self) -> Option<&[u8]>
pub fn get_signer_cert(&self) -> Option<&[u8]>
Returns a reference to the signer certificate in the voucher, if any.
Examples
use minerva_voucher::Voucher;
let mut vrq = Voucher::new_vrq();
assert_eq!(vrq.get_signer_cert(), None);
vrq.set_signer_cert(&[4, 186, 197, 177, 28, 173, 143, 153, 249, 199, 43, 5, 207, 75, 158, 38, 210, 68, 220, 24, 159, 116, 82, 40, 37, 90, 33, 154, 134, 214, 160, 158, 255, 32, 19, 139, 248, 45, 193, 182, 213, 98, 190, 15, 165, 74, 183, 128, 74, 58, 100, 182, 215, 44, 207, 237, 107, 111, 182, 237, 40, 187, 252, 17, 126]);
assert_eq!(vrq.get_signer_cert().unwrap().len(), 65);
sourcepub fn set_signer_cert(&mut self, cert: &[u8]) -> &mut Self
pub fn set_signer_cert(&mut self, cert: &[u8]) -> &mut Self
Adds a singer certificate to the voucher. Returns a mut
reference to the voucher.
Examples
use minerva_voucher::Voucher;
let mut vrq = Voucher::new_vrq();
assert_eq!(vrq.get_signer_cert(), None);
vrq.set_signer_cert(&[4, 186, 197, 177, 28, 173, 143, 153, 249, 199, 43, 5, 207, 75, 158, 38, 210, 68, 220, 24, 159, 116, 82, 40, 37, 90, 33, 154, 134, 214, 160, 158, 255, 32, 19, 139, 248, 45, 193, 182, 213, 98, 190, 15, 165, 74, 183, 128, 74, 58, 100, 182, 215, 44, 207, 237, 107, 111, 182, 237, 40, 187, 252, 17, 126]);
assert_eq!(vrq.get_signer_cert().unwrap().len(), 65);
sourcepub fn to_sign(&mut self, alg: SignatureAlgorithm) -> (&mut Vec<u8>, &[u8])
pub fn to_sign(&mut self, alg: SignatureAlgorithm) -> (&mut Vec<u8>, &[u8])
Interfaces with meta data required for signing the voucher.
This method needs to be used when implementing the Sign
trait.
Returns a tuple of
- a
mut
reference to theVec<u8>
data where a new signature is being written, and - a reference to the CBOR-encoded
COSE_Sign1
structure for which signing is performed.
Examples
sourcepub fn to_validate(
&self
) -> (Option<&[u8]>, Option<(&[u8], &SignatureAlgorithm)>, &[u8])
pub fn to_validate(
&self
) -> (Option<&[u8]>, Option<(&[u8], &SignatureAlgorithm)>, &[u8])
Interfaces with meta data required for validating the voucher.
This method needs to be used when implementing the Validate
trait.
Returns a tuple of
- a reference to the signer certificate in the voucher, if any,
- a tuple of references to the signature and its corresponding algorithm in the voucher, if any, and
- a reference to the CBOR-encoded
COSE_Sign1
structure for which validation is performed.
Examples
sourcepub fn dump(&self)
pub fn dump(&self)
Prints internal representation of the voucher for debugging purposes.
Examples
use minerva_voucher::Voucher;
use core::convert::TryFrom;
static VCH_JADA: &[u8] = core::include_bytes!(
concat!(env!("CARGO_MANIFEST_DIR"), "/data/jada/voucher_jada123456789.vch"));
let vch = Voucher::try_from(VCH_JADA).unwrap();
vch.dump();
/* stdout:
======== Voucher::dump()
==== SidData::dump()
Voucher({VchTopLevel(VoucherVoucher), VchAssertion(Enumeration(Assertion(Proximity))), VchCreatedOn(DateAndTime(CreatedOn(1475868702))), VchExpiresOn(DateAndTime(ExpiresOn(1506816000))), VchNonce(Binary(Nonce([97, 98, 99, 100, 49, 50, 51, 52, 53]))), VchPinnedDomainPubk(Binary(PinnedDomainPubk([77, 70, 107, 119, 69, 119, 89, 72, 75, 111, 90, 73, 122, 106, 48, 67, 65, 81, 89, 73, 75, 111, 90, 73, 122, 106, 48, 68, 65, 81, 99, 68, 81, 103, 65, 69, 108, 109, 86, 81, 99, 106, 83, 54, 110, 43, 88, 100, 53, 108, 47, 50, 56, 73, 70, 118, 54, 85, 105, 101, 103, 81, 119, 83, 66, 122, 116, 71, 106, 53, 100, 107, 75, 50, 77, 65, 106, 81, 73, 80, 86, 56, 108, 56, 108, 72, 43, 69, 106, 76, 73, 79, 89, 100, 98, 74, 105, 73, 48, 86, 116, 69, 73, 102, 49, 47, 74, 113, 116, 43, 84, 79, 66, 102, 105, 110, 84, 78, 79, 76, 79, 103, 61, 61]))), VchSerialNumber(String(SerialNumber([74, 65, 68, 65, 49, 50, 51, 52, 53, 54, 55, 56, 57])))})
====
==== CoseSig::dump()
signature_type: ES256
signature: [len=64] [234, 232, 104, 236, 193, 118, 136, 55, 102, 197, 220, 91, 165, 184, 220, 162, 93, 171, 60, 46, 86, 165, 81, 206, 87, 5, 183, 147, 145, 67, 72, 225, 217, 85, 56, 95, 66, 111, 229, 137, 148, 12, 142, 214, 58, 86, 83, 68, 254, 186, 154, 162, 228, 175, 25, 168, 102, 60, 251, 36, 170, 105, 99, 194]
signer_cert: [len=65] [4, 186, 197, 177, 28, 173, 143, 153, 249, 199, 43, 5, 207, 75, 158, 38, 210, 68, 220, 24, 159, 116, 82, 40, 37, 90, 33, 154, 134, 214, 160, 158, 255, 32, 19, 139, 248, 45, 193, 182, 213, 98, 190, 15, 165, 74, 183, 128, 74, 58, 100, 182, 215, 44, 207, 237, 107, 111, 182, 237, 40, 187, 252, 17, 126]
to_verify: [len=202] [132, 106, 83, 105, 103, 110, 97, 116, 117, 114, 101, 49, 67, 161, 1, 38, 64, 88, 183, 161, 25, 9, 147, 166, 1, 105, 112, 114, 111, 120, 105, 109, 105, 116, 121, 2, 193, 26, 87, 247, 248, 30, 4, 193, 26, 89, 208, 48, 0, 11, 109, 74, 65, 68, 65, 49, 50, 51, 52, 53, 54, 55, 56, 57, 7, 105, 97, 98, 99, 100, 49, 50, 51, 52, 53, 9, 120, 124, 77, 70, 107, 119, 69, 119, 89, 72, 75, 111, 90, 73, 122, 106, 48, 67, 65, 81, 89, 73, 75, 111, 90, 73, 122, 106, 48, 68, 65, 81, 99, 68, 81, 103, 65, 69, 108, 109, 86, 81, 99, 106, 83, 54, 110, 43, 88, 100, 53, 108, 47, 50, 56, 73, 70, 118, 54, 85, 105, 101, 103, 81, 119, 83, 66, 122, 116, 71, 106, 53, 100, 107, 75, 50, 77, 65, 106, 81, 73, 80, 86, 56, 108, 56, 108, 72, 43, 69, 106, 76, 73, 79, 89, 100, 98, 74, 105, 73, 48, 86, 116, 69, 73, 102, 49, 47, 74, 113, 116, 43, 84, 79, 66, 102, 105, 110, 84, 78, 79, 76, 79, 103, 61, 61]
====
========
*/
Trait Implementations
sourceimpl Sign for Voucher
impl Sign for Voucher
sourcefn sign(
&mut self,
privkey_pem: &[u8],
alg: SignatureAlgorithm
) -> Result<&mut Self, VoucherError>
fn sign(
&mut self,
privkey_pem: &[u8],
alg: SignatureAlgorithm
) -> Result<&mut Self, VoucherError>
Signs the voucher using a PEM-encoded private key
based on the signature algorithm alg
.
Returns a &mut Self
reference if the voucher is signed.
Errors
If the voucher is not signed, or the internal signing function fails, a VoucherError::SigningFailed
is returned.
Examples
use minerva_voucher::{Voucher, attr::*, SignatureAlgorithm, Sign};
static KEY_PEM_F2_00_02: &[u8] = core::include_bytes!(
concat!(env!("CARGO_MANIFEST_DIR"), "/data/00-D0-E5-F2-00-02/key.pem"));
// This is required when the `Sign` trait is backed by mbedtls.
minerva_voucher::init_psa_crypto();
let mut vrq = Voucher::new_vrq();
vrq.set(Attr::Assertion(Assertion::Proximity))
.set(Attr::SerialNumber(b"00-D0-E5-F2-00-02".to_vec()));
assert!(vrq.get_signature().is_none());
vrq.sign(KEY_PEM_F2_00_02, SignatureAlgorithm::ES256).unwrap();
assert!(vrq.get_signature().is_some());
sourceimpl TryFrom<&[u8]> for Voucher
impl TryFrom<&[u8]> for Voucher
sourceimpl Validate for Voucher
impl Validate for Voucher
sourcefn validate(&self, pem: Option<&[u8]>) -> Result<&Self, VoucherError>
fn validate(&self, pem: Option<&[u8]>) -> Result<&Self, VoucherError>
Validates the voucher using a PEM-encoded certificate.
If the certificate pem
is None
, signer_cert
attached to the voucher (see Voucher::set_signer_cert
), if any, is used instead.
Returns a &Self
reference if the voucher is validated.
Errors
If the voucher is not validated, or the internal validation function fails, a VoucherError::ValidationFailed
is returned.
Examples
use minerva_voucher::{Voucher, Validate};
use core::convert::TryFrom;
static VCH_F2_00_02: &[u8] = core::include_bytes!(
concat!(env!("CARGO_MANIFEST_DIR"), "/data/00-D0-E5-F2-00-02/voucher_00-D0-E5-F2-00-02.vch"));
static MASA_CRT_F2_00_02: &[u8] = core::include_bytes!(
concat!(env!("CARGO_MANIFEST_DIR"), "/data/00-D0-E5-F2-00-02/masa.crt"));
// This is required when the `Validate` trait is backed by mbedtls.
minerva_voucher::init_psa_crypto();
let vch = Voucher::try_from(VCH_F2_00_02).unwrap();
assert!(vch.validate(Some(MASA_CRT_F2_00_02)).is_ok());
impl StructuralPartialEq for Voucher
Auto Trait Implementations
impl RefUnwindSafe for Voucher
impl Send for Voucher
impl Sync for Voucher
impl Unpin for Voucher
impl UnwindSafe for Voucher
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more