#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PayloadType {
Last = 0,
KeyData = 1,
Timestamp = 2,
Rand = 3,
SecurityPolicy = 4,
KeyValidationData = 5,
GeneralExtension = 6,
Certificate = 7,
Encrypted = 8,
Mac = 9,
Signature = 10,
PublicKey = 11,
Unknown = 255,
}
#[derive(Debug, Clone)]
pub struct CommonHeader {
pub version: u8,
pub data_type: u8,
pub next_payload: u8,
pub v_flag: bool,
pub prf_func: u8,
pub csp_id: u16,
pub cs_count: u8,
pub cs_id_map_type: u8,
}
#[derive(Debug, Clone)]
pub struct KeyDataPayload {
pub key_type: u8,
pub key_data: Vec<u8>,
pub salt_data: Option<Vec<u8>>,
pub kv_data: Option<Vec<u8>>,
}
#[derive(Debug, Clone)]
pub struct SecurityPolicyPayload {
pub policy_no: u8,
pub policy_type: u8,
pub policy_param: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct KeyValidationData {
pub validation_data: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct GeneralExtensionPayload {
pub ext_type: u8,
pub ext_data: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct CertificatePayload {
pub cert_type: CertificateType,
pub cert_data: Vec<u8>,
pub cert_chain: Vec<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CertificateType {
X509 = 0,
X509Chain = 1,
Pgp = 2,
Reserved = 255,
}
#[derive(Debug, Clone)]
pub struct SignaturePayload {
pub sig_algorithm: SignatureAlgorithm,
pub signature: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum SignatureAlgorithm {
RsaSha256 = 0,
RsaSha512 = 1,
EcdsaSha256 = 2,
EcdsaSha512 = 3,
RsaPssSha256 = 4,
RsaPssSha512 = 5,
}
#[derive(Debug, Clone)]
pub struct EncryptedPayload {
pub enc_algorithm: EncryptionAlgorithm,
pub encrypted_data: Vec<u8>,
pub iv: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum EncryptionAlgorithm {
RsaPkcs1 = 0,
RsaOaepSha256 = 1,
Ecies = 2,
}
#[derive(Debug, Clone)]
pub struct PublicKeyPayload {
pub key_algorithm: PublicKeyAlgorithm,
pub key_data: Vec<u8>,
pub key_params: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PublicKeyAlgorithm {
Rsa = 0,
EcdsaP256 = 1,
EcdsaP384 = 2,
EcdsaP521 = 3,
Ed25519 = 4,
}
impl CertificateType {
pub fn supports_chain(&self) -> bool {
matches!(self, CertificateType::X509Chain)
}
}
impl SignatureAlgorithm {
pub fn hash_algorithm(&self) -> &'static str {
match self {
SignatureAlgorithm::RsaSha256
| SignatureAlgorithm::EcdsaSha256
| SignatureAlgorithm::RsaPssSha256 => "SHA-256",
SignatureAlgorithm::RsaSha512
| SignatureAlgorithm::EcdsaSha512
| SignatureAlgorithm::RsaPssSha512 => "SHA-512",
}
}
pub fn is_rsa(&self) -> bool {
matches!(
self,
SignatureAlgorithm::RsaSha256
| SignatureAlgorithm::RsaSha512
| SignatureAlgorithm::RsaPssSha256
| SignatureAlgorithm::RsaPssSha512
)
}
pub fn is_ecdsa(&self) -> bool {
matches!(
self,
SignatureAlgorithm::EcdsaSha256 | SignatureAlgorithm::EcdsaSha512
)
}
}
impl EncryptionAlgorithm {
pub fn requires_iv(&self) -> bool {
matches!(self, EncryptionAlgorithm::Ecies)
}
pub fn key_size(&self) -> Option<usize> {
match self {
EncryptionAlgorithm::RsaPkcs1 => None, EncryptionAlgorithm::RsaOaepSha256 => None, EncryptionAlgorithm::Ecies => Some(32), }
}
}
impl PublicKeyAlgorithm {
pub fn key_size(&self) -> usize {
match self {
PublicKeyAlgorithm::Rsa => 256, PublicKeyAlgorithm::EcdsaP256 => 64, PublicKeyAlgorithm::EcdsaP384 => 96, PublicKeyAlgorithm::EcdsaP521 => 132, PublicKeyAlgorithm::Ed25519 => 32, }
}
pub fn curve_name(&self) -> Option<&'static str> {
match self {
PublicKeyAlgorithm::EcdsaP256 => Some("P-256"),
PublicKeyAlgorithm::EcdsaP384 => Some("P-384"),
PublicKeyAlgorithm::EcdsaP521 => Some("P-521"),
PublicKeyAlgorithm::Ed25519 => Some("Ed25519"),
PublicKeyAlgorithm::Rsa => None,
}
}
}