use crate::error::{NetError, Result};
use crate::profile::Profile;
use libp2p::identity::{Keypair, PublicKey};
use serde::{Deserialize, Serialize};
use sha2::Digest;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NodeCert {
pub user_id: String,
pub peer_id: String,
pub label: String,
pub cert: Vec<u8>,
pub issued_at: i64,
pub revoked_at: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub revocation: Option<Vec<u8>>,
}
impl NodeCert {
pub fn active(&self) -> bool {
self.revoked_at.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Card {
pub id: String,
pub label: String,
pub key: Vec<u8>,
pub nodes: Vec<NodeCert>,
}
impl Card {
pub fn digest(&self) -> String {
let mut h = sha2::Sha256::new();
h.update(self.id.as_bytes());
h.update(self.label.as_bytes());
for n in &self.nodes {
h.update(n.peer_id.as_bytes());
h.update(n.issued_at.to_le_bytes());
h.update(n.revoked_at.unwrap_or(0).to_le_bytes());
}
hex::encode(&h.finalize()[..8])
}
}
pub fn user_id(key: &PublicKey) -> String {
hex::encode(&sha2::Sha256::digest(key.encode_protobuf())[..16])
}
pub fn decode_keypair(bytes: &[u8]) -> Result<Keypair> {
Keypair::from_protobuf_encoding(bytes).map_err(|e| NetError::Crypto(format!("user identity: {e}")))
}
pub fn decode_public(bytes: &[u8]) -> Result<PublicKey> {
PublicKey::try_decode_protobuf(bytes).map_err(|e| NetError::Crypto(format!("user key: {e}")))
}
impl Profile {
pub fn cert_bytes(&self, user: &str, peer: &str, at: i64) -> Vec<u8> {
format!("{}\n{user}\n{peer}\n{at}", self.cert).into_bytes()
}
pub fn revoke_bytes(&self, user: &str, peer: &str, at: i64) -> Vec<u8> {
format!("{}\n{user}\n{peer}\n{at}", self.revoke).into_bytes()
}
pub fn certify(&self, kp: &Keypair, user: &str, peer: &str, label: &str, at: i64) -> Result<NodeCert> {
let cert = kp.sign(&self.cert_bytes(user, peer, at)).map_err(|e| NetError::Crypto(format!("certify: {e}")))?;
Ok(NodeCert { user_id: user.into(), peer_id: peer.into(), label: label.into(), cert, issued_at: at, revoked_at: None, revocation: None })
}
pub fn revoke(&self, kp: &Keypair, user: &str, peer: &str, at: i64) -> Result<Vec<u8>> {
kp.sign(&self.revoke_bytes(user, peer, at)).map_err(|e| NetError::Crypto(format!("revoke: {e}")))
}
pub fn verify_cert(&self, key: &PublicKey, n: &NodeCert) -> bool {
if user_id(key) != n.user_id || !key.verify(&self.cert_bytes(&n.user_id, &n.peer_id, n.issued_at), &n.cert) {
return false;
}
match (n.revoked_at, &n.revocation) {
(None, None) => true,
(Some(at), Some(sig)) => key.verify(&self.revoke_bytes(&n.user_id, &n.peer_id, at), sig),
_ => false,
}
}
pub fn check_card(&self, from_peer: &str, card: &Card) -> Result<PublicKey> {
let key = decode_public(&card.key)?;
if user_id(&key) != card.id {
return Err(NetError::Crypto("user card: the id does not match the key".into()));
}
if !card.nodes.iter().any(|n| n.peer_id == from_peer && n.user_id == card.id && n.active() && self.verify_cert(&key, n)) {
return Err(NetError::Crypto("user card: no valid certificate for the sending node".into()));
}
Ok(key)
}
pub fn check_grant(&self, me: &str, from_peer: &str, card: &Card, keypair: &[u8]) -> Result<Keypair> {
let kp = decode_keypair(keypair)?;
if user_id(&kp.public()) != card.id || kp.public().encode_protobuf() != card.key {
return Err(NetError::Crypto("link: the key does not match the user".into()));
}
let key = kp.public();
if !card.nodes.iter().any(|n| n.peer_id == me && n.active() && self.verify_cert(&key, n)) {
return Err(NetError::Crypto("link: no certificate for this node".into()));
}
if !card.nodes.iter().any(|n| n.peer_id == from_peer && n.active() && self.verify_cert(&key, n)) {
return Err(NetError::Crypto("link: the approving node is not the user's".into()));
}
Ok(kp)
}
}