|
| 1 | +//! Cardano Improvement Proposal 509 (CIP-509) metadata module. |
| 2 | +//! Doc Reference: <https://github.com/input-output-hk/catalyst-CIPs/tree/x509-envelope-metadata/CIP-XXXX> |
| 3 | +//! CDDL Reference: <https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/x509-envelope.cddl> |
| 4 | +
|
| 5 | +// cspell: words pkix |
| 6 | + |
| 7 | +pub mod rbac; |
| 8 | +pub(crate) mod utils; |
| 9 | +pub(crate) mod validation; |
| 10 | +pub mod x509_chunks; |
| 11 | + |
| 12 | +use minicbor::{ |
| 13 | + decode::{self}, |
| 14 | + Decode, Decoder, |
| 15 | +}; |
| 16 | +use pallas::{crypto::hash::Hash, ledger::traverse::MultiEraTx}; |
| 17 | +use strum_macros::FromRepr; |
| 18 | +use validation::{ |
| 19 | + validate_aux, validate_payment_key, validate_role_singing_key, validate_stake_public_key, |
| 20 | + validate_txn_inputs_hash, |
| 21 | +}; |
| 22 | +use x509_chunks::X509Chunks; |
| 23 | + |
| 24 | +use super::transaction::witness::TxWitness; |
| 25 | +use crate::utils::{ |
| 26 | + decode_helper::{decode_bytes, decode_helper, decode_map_len}, |
| 27 | + general::{decode_utf8, decremented_index}, |
| 28 | + hashing::{blake2b_128, blake2b_256}, |
| 29 | +}; |
| 30 | + |
| 31 | +/// CIP509 label. |
| 32 | +pub const LABEL: u64 = 509; |
| 33 | + |
| 34 | +/// CIP509. |
| 35 | +#[derive(Debug, PartialEq, Clone, Default)] |
| 36 | +pub struct Cip509 { |
| 37 | + /// `UUIDv4` Purpose . |
| 38 | + pub purpose: UuidV4, // (bytes .size 16) |
| 39 | + /// Transaction inputs hash. |
| 40 | + pub txn_inputs_hash: TxInputHash, // bytes .size 16 |
| 41 | + /// Optional previous transaction ID. |
| 42 | + pub prv_tx_id: Option<Hash<32>>, // bytes .size 32 |
| 43 | + /// x509 chunks. |
| 44 | + pub x509_chunks: X509Chunks, // chunk_type => [ + x509_chunk ] |
| 45 | + /// Validation signature. |
| 46 | + pub validation_signature: Vec<u8>, // bytes size (1..64) |
| 47 | +} |
| 48 | + |
| 49 | +/// `UUIDv4` representing in 16 bytes. |
| 50 | +#[derive(Debug, PartialEq, Clone, Default)] |
| 51 | +pub struct UuidV4([u8; 16]); |
| 52 | + |
| 53 | +impl From<[u8; 16]> for UuidV4 { |
| 54 | + fn from(bytes: [u8; 16]) -> Self { |
| 55 | + UuidV4(bytes) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl TryFrom<Vec<u8>> for UuidV4 { |
| 60 | + type Error = &'static str; |
| 61 | + |
| 62 | + fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> { |
| 63 | + if vec.len() == 16 { |
| 64 | + let mut array = [0u8; 16]; |
| 65 | + array.copy_from_slice(&vec); |
| 66 | + Ok(UuidV4(array)) |
| 67 | + } else { |
| 68 | + Err("Input Vec must be exactly 16 bytes") |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Transaction input hash representing in 16 bytes. |
| 74 | +#[derive(Debug, PartialEq, Clone, Default)] |
| 75 | +pub struct TxInputHash([u8; 16]); |
| 76 | + |
| 77 | +impl From<[u8; 16]> for TxInputHash { |
| 78 | + fn from(bytes: [u8; 16]) -> Self { |
| 79 | + TxInputHash(bytes) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl TryFrom<Vec<u8>> for TxInputHash { |
| 84 | + type Error = &'static str; |
| 85 | + |
| 86 | + fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> { |
| 87 | + if vec.len() == 16 { |
| 88 | + let mut array = [0u8; 16]; |
| 89 | + array.copy_from_slice(&vec); |
| 90 | + Ok(TxInputHash(array)) |
| 91 | + } else { |
| 92 | + Err("Input Vec must be exactly 16 bytes") |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Enum of CIP509 metadatum with its associated unsigned integer value. |
| 98 | +#[allow(clippy::module_name_repetitions)] |
| 99 | +#[derive(FromRepr, Debug, PartialEq)] |
| 100 | +#[repr(u8)] |
| 101 | +pub(crate) enum Cip509IntIdentifier { |
| 102 | + /// Purpose. |
| 103 | + Purpose = 0, |
| 104 | + /// Transaction inputs hash. |
| 105 | + TxInputsHash = 1, |
| 106 | + /// Previous transaction ID. |
| 107 | + PreviousTxId = 2, |
| 108 | + /// Validation signature. |
| 109 | + ValidationSignature = 99, |
| 110 | +} |
| 111 | + |
| 112 | +impl Decode<'_, ()> for Cip509 { |
| 113 | + fn decode(d: &mut Decoder, ctx: &mut ()) -> Result<Self, decode::Error> { |
| 114 | + let map_len = decode_map_len(d, "CIP509")?; |
| 115 | + let mut cip509_metadatum = Cip509::default(); |
| 116 | + for _ in 0..map_len { |
| 117 | + // Use probe to peak |
| 118 | + let key = d.probe().u8()?; |
| 119 | + if let Some(key) = Cip509IntIdentifier::from_repr(key) { |
| 120 | + // Consuming the int |
| 121 | + let _: u8 = decode_helper(d, "CIP509", ctx)?; |
| 122 | + match key { |
| 123 | + Cip509IntIdentifier::Purpose => { |
| 124 | + cip509_metadatum.purpose = |
| 125 | + UuidV4::try_from(decode_bytes(d, "CIP509 purpose")?).map_err(|_| { |
| 126 | + decode::Error::message("Invalid data size of Purpose") |
| 127 | + })?; |
| 128 | + }, |
| 129 | + Cip509IntIdentifier::TxInputsHash => { |
| 130 | + cip509_metadatum.txn_inputs_hash = |
| 131 | + TxInputHash::try_from(decode_bytes(d, "CIP509 txn inputs hash")?) |
| 132 | + .map_err(|_| { |
| 133 | + decode::Error::message("Invalid data size of TxInputsHash") |
| 134 | + })?; |
| 135 | + }, |
| 136 | + Cip509IntIdentifier::PreviousTxId => { |
| 137 | + let prv_tx_hash: [u8; 32] = decode_bytes(d, "CIP509 previous tx ID")? |
| 138 | + .try_into() |
| 139 | + .map_err(|_| { |
| 140 | + decode::Error::message("Invalid data size of PreviousTxId") |
| 141 | + })?; |
| 142 | + cip509_metadatum.prv_tx_id = Some(Hash::from(prv_tx_hash)); |
| 143 | + }, |
| 144 | + Cip509IntIdentifier::ValidationSignature => { |
| 145 | + let validation_signature = decode_bytes(d, "CIP509 validation signature")?; |
| 146 | + if validation_signature.is_empty() || validation_signature.len() > 64 { |
| 147 | + return Err(decode::Error::message( |
| 148 | + "Invalid data size of ValidationSignature", |
| 149 | + )); |
| 150 | + } |
| 151 | + cip509_metadatum.validation_signature = validation_signature; |
| 152 | + }, |
| 153 | + } |
| 154 | + } else { |
| 155 | + // Handle the x509 chunks 10 11 12 |
| 156 | + let x509_chunks = X509Chunks::decode(d, ctx)?; |
| 157 | + cip509_metadatum.x509_chunks = x509_chunks; |
| 158 | + } |
| 159 | + } |
| 160 | + Ok(cip509_metadatum) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl Cip509 { |
| 165 | + /// Basic validation for CIP509 |
| 166 | + /// The validation include the following: |
| 167 | + /// * Hashing the transaction inputs within the transaction should match the |
| 168 | + /// txn-inputs-hash |
| 169 | + /// * Auxiliary data hash within the transaction should match the hash of the |
| 170 | + /// auxiliary data itself. |
| 171 | + /// * Public key validation for role 0 where public key extracted from x509 and c509 |
| 172 | + /// subject alternative name should match one of the witness in witness set within |
| 173 | + /// the transaction. |
| 174 | + /// * Payment key reference validation for role 0 where the reference should be either |
| 175 | + /// 1. Negative index reference - reference to transaction output in transaction: |
| 176 | + /// should match some of the key within witness set. |
| 177 | + /// 2. Positive index reference - reference to the transaction input in |
| 178 | + /// transaction: only check whether the index exist within the transaction |
| 179 | + /// inputs. |
| 180 | + /// * Role signing key validation for role 0 where the signing keys should only be the |
| 181 | + /// certificates |
| 182 | + /// |
| 183 | + /// See: |
| 184 | + /// * <https://github.com/input-output-hk/catalyst-CIPs/tree/x509-envelope-metadata/CIP-XXXX> |
| 185 | + /// * <https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/x509-envelope.cddl> |
| 186 | + /// |
| 187 | + /// Note: This CIP509 is still under development and is subject to change. |
| 188 | + /// |
| 189 | + /// # Parameters |
| 190 | + /// * `txn` - Transaction data was attached to and to be validated/decoded against. |
| 191 | + /// * `validation_report` - Validation report to store the validation result. |
| 192 | + pub fn validate(&self, txn: &MultiEraTx, validation_report: &mut Vec<String>) -> bool { |
| 193 | + let tx_input_validate = |
| 194 | + validate_txn_inputs_hash(self, txn, validation_report).unwrap_or(false); |
| 195 | + let aux_validate = validate_aux(txn, validation_report).unwrap_or(false); |
| 196 | + let mut stake_key_validate = true; |
| 197 | + let mut payment_key_validate = true; |
| 198 | + let mut signing_key = true; |
| 199 | + // Validate the role 0 |
| 200 | + if let Some(role_set) = &self.x509_chunks.0.role_set { |
| 201 | + // Validate only role 0 |
| 202 | + for role in role_set { |
| 203 | + if role.role_number == 0 { |
| 204 | + stake_key_validate = |
| 205 | + validate_stake_public_key(self, txn, validation_report).unwrap_or(false); |
| 206 | + payment_key_validate = |
| 207 | + validate_payment_key(txn, role, validation_report).unwrap_or(false); |
| 208 | + signing_key = validate_role_singing_key(role, validation_report); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + tx_input_validate |
| 213 | + && aux_validate |
| 214 | + && stake_key_validate |
| 215 | + && payment_key_validate |
| 216 | + && signing_key |
| 217 | + } |
| 218 | +} |
0 commit comments