pub struct IDCard { /* private fields */ }Expand description
An IDCard allows entities to declare sub-keys with specific purposes.
IDCards provide a way to manage multiple keys for an entity, each with specific purposes (e.g., “sign”, “decrypt”). Keys can have expiration dates, and the IDCard can be signed to establish trust. IDCards also support metadata and group memberships.
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
use std::time::Duration;
let rng = &mut OsRng;
let primary_key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&primary_key.public_key_bytes());
idcard.set_metadata("name", "Alice");
idcard.set_key_purposes(&primary_key.public_key_bytes(), &["sign", "decrypt"]);
idcard.set_key_duration(&primary_key.public_key_bytes(), Duration::from_secs(365 * 24 * 3600));Implementations§
Source§impl IDCard
impl IDCard
Sourcepub fn new(public_key: &[u8]) -> Self
pub fn new(public_key: &[u8]) -> Self
Create a new IDCard for a public key.
The primary key is automatically added with default purposes “sign” and “decrypt” and no expiration.
§Arguments
public_key- The primary public key for this entity
§Returns
A new IDCard instance with the primary key registered
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let idcard = IDCard::new(&key.public_key_bytes());Sourcepub fn set_metadata(&mut self, key: &str, value: &str)
pub fn set_metadata(&mut self, key: &str, value: &str)
Set metadata key-value pair.
Metadata is application-specific data stored with the IDCard. It is not encrypted or signed, so it should not contain sensitive information.
§Arguments
key- Metadata keyvalue- Metadata value
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key.public_key_bytes());
idcard.set_metadata("name", "Alice");
idcard.set_metadata("email", "alice@example.com");Sourcepub fn set_key_purposes(&mut self, public_key: &[u8], purposes: &[&str])
pub fn set_key_purposes(&mut self, public_key: &[u8], purposes: &[&str])
Set the purposes for a key in the IDCard.
Purposes define what operations a key is authorized for. Common purposes include “sign” and “decrypt”. If the key is not already in the IDCard, it will be added.
§Arguments
public_key- The public key to set purposes forpurposes- Array of purpose strings (e.g., [“sign”, “decrypt”])
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key.public_key_bytes());
idcard.set_key_purposes(&key.public_key_bytes(), &["sign"]);Sourcepub fn set_key_duration(&mut self, public_key: &[u8], duration: Duration)
pub fn set_key_duration(&mut self, public_key: &[u8], duration: Duration)
Set the expiration duration for a key.
This sets when the key will expire from now. If the key is not already in the IDCard, it will be added with no purposes.
§Arguments
public_key- The public key to set expiration forduration- Duration from now until expiration
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
use std::time::Duration;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key.public_key_bytes());
idcard.set_key_duration(&key.public_key_bytes(), Duration::from_secs(365 * 24 * 3600));Sourcepub fn test_key_purpose(&self, public_key: &[u8], purpose: &str) -> Result<()>
pub fn test_key_purpose(&self, public_key: &[u8], purpose: &str) -> Result<()>
Test if a key has a specific purpose and is not expired.
This method checks both that the key has the specified purpose and that it hasn’t expired (if it has an expiration date).
§Arguments
public_key- The public key to testpurpose- The purpose to check for (e.g., “sign”)
§Returns
Ok(())- Key has the purpose and is not expiredErr(BottleError::KeyNotFound)- Key is not in the IDCardErr(BottleError::KeyUnfit)- Key doesn’t have the purpose or is expired
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key.public_key_bytes());
idcard.set_key_purposes(&key.public_key_bytes(), &["sign"]);
assert!(idcard.test_key_purpose(&key.public_key_bytes(), "sign").is_ok());
assert!(idcard.test_key_purpose(&key.public_key_bytes(), "decrypt").is_err());Sourcepub fn get_keys(&self, purpose: &str) -> Vec<Vec<u8>>
pub fn get_keys(&self, purpose: &str) -> Vec<Vec<u8>>
Get all key fingerprints that have a specific purpose and are not expired.
§Arguments
purpose- The purpose to filter by (e.g., “sign”)
§Returns
A vector of key fingerprints (SHA-256 hashes) that have the specified purpose and are not expired
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key1 = Ed25519Key::generate(rng);
let key2 = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key1.public_key_bytes());
idcard.set_key_purposes(&key1.public_key_bytes(), &["sign"]);
idcard.set_key_purposes(&key2.public_key_bytes(), &["decrypt"]);
let sign_keys = idcard.get_keys("sign");
assert_eq!(sign_keys.len(), 1);Sourcepub fn update_groups(&mut self, groups: Vec<Vec<u8>>)
pub fn update_groups(&mut self, groups: Vec<Vec<u8>>)
Update the list of group memberships.
Groups are stored as serialized membership data. This replaces the entire list of groups.
§Arguments
groups- Vector of serialized membership data
Sourcepub fn sign<R: RngCore>(
&mut self,
rng: &mut R,
signer: &dyn Sign,
) -> Result<Vec<u8>>
pub fn sign<R: RngCore>( &mut self, rng: &mut R, signer: &dyn Sign, ) -> Result<Vec<u8>>
Sign the IDCard with a private key.
This creates a cryptographic signature of the IDCard (excluding the signature field itself) and stores it. The signed IDCard is then serialized and returned.
§Arguments
rng- A random number generatorsigner- A signer implementing theSigntrait
§Returns
Ok(Vec<u8>)- Serialized signed IDCardErr(BottleError::Serialization)- If serialization failsErr(BottleError::VerifyFailed)- If signing fails
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let mut idcard = IDCard::new(&key.public_key_bytes());
let signed_bytes = idcard.sign(rng, &key).unwrap();Sourcepub fn to_bytes(&self) -> Result<Vec<u8>>
pub fn to_bytes(&self) -> Result<Vec<u8>>
Serialize the IDCard to bytes using bincode.
§Returns
Ok(Vec<u8>)- Serialized IDCard bytesErr(BottleError::Serialization)- If serialization fails
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let idcard = IDCard::new(&key.public_key_bytes());
let bytes = idcard.to_bytes().unwrap();
let restored = IDCard::from_bytes(&bytes).unwrap();Sourcepub fn from_bytes(data: &[u8]) -> Result<Self>
pub fn from_bytes(data: &[u8]) -> Result<Self>
Deserialize an IDCard from bytes.
§Arguments
data- Serialized IDCard bytes (fromto_bytes)
§Returns
Ok(IDCard)- Deserialized IDCardErr(BottleError::Deserialization)- If deserialization fails
§Example
use rust_bottle::*;
use rand::rngs::OsRng;
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let idcard = IDCard::new(&key.public_key_bytes());
let bytes = idcard.to_bytes().unwrap();
let restored = IDCard::from_bytes(&bytes).unwrap();