Преглед изворни кода

Renamed WriteCap to Writecap and ReadCap to Readcap.

Matthew Carr пре 2 година
родитељ
комит
c92b2e5615
4 измењених фајлова са 71 додато и 71 уклоњено
  1. 41 41
      crates/node/src/crypto.rs
  2. 12 12
      crates/node/src/main.rs
  3. 1 1
      crates/node/src/serde_tests.rs
  4. 17 17
      crates/node/src/test_helpers.rs

+ 41 - 41
crates/node/src/crypto.rs

@@ -469,12 +469,12 @@ pub(crate) fn encrypt_block(
         Cryptotext::Cipher(_) => return Ok(VersionedBlock::V0(block)),
         Cryptotext::Plain(body) => body
     };
-    let (principal_owned, read_cap) = block.read_caps.remove_entry(principal)
+    let (principal_owned, read_cap) = block.readcaps.remove_entry(principal)
         .ok_or(Error::NoReadCap)?;
     let block_key = decrypt(read_cap, key)?;
     let new_body = encrypt_slice(&body, &block_key)?;
     block.body = Cryptotext::Cipher(new_body);
-    block.read_caps.insert(principal_owned, encrypt(&block_key, key)?);
+    block.readcaps.insert(principal_owned, encrypt(&block_key, key)?);
     Ok(VersionedBlock::V0(block))
 }
 
@@ -486,12 +486,12 @@ pub(crate) fn decrypt_block(
         Cryptotext::Plain(_) => return Ok(VersionedBlock::V0(block)),
         Cryptotext::Cipher(body) => body
     };
-    let (principal_owned, read_cap) = block.read_caps.remove_entry(principal)
+    let (principal_owned, read_cap) = block.readcaps.remove_entry(principal)
         .ok_or(Error::NoReadCap)?;
     let block_key = decrypt(read_cap, key)?;
     let new_body = decrypt_slice(&body, &block_key)?;
     block.body = Cryptotext::Plain(new_body);
-    block.read_caps.insert(principal_owned, Cryptotext::Plain(block_key));
+    block.readcaps.insert(principal_owned, Cryptotext::Plain(block_key));
     Ok(VersionedBlock::V0(block))
 }
 
@@ -523,16 +523,16 @@ fn decrypt_slice(ciphertext: &[u8], key: &Key) -> Result<Vec<u8>> {
 #[derive(Serialize)]
 struct SigHeader<'a> {
     path: &'a Path,
-    read_caps: &'a HashMap<Principal, Cryptotext<Key>>,
-    write_cap: &'a WriteCap,
+    readcaps: &'a HashMap<Principal, Cryptotext<Key>>,
+    writecap: &'a Writecap,
 }
 
 impl<'a> From<&'a Block> for SigHeader<'a> {
     fn from(block: &'a Block) -> SigHeader<'a> {
         SigHeader {
             path: &block.path,
-            read_caps: &block.read_caps,
-            write_cap: &block.write_cap,
+            readcaps: &block.readcaps,
+            writecap: &block.writecap,
         }
     }
 }
@@ -547,13 +547,13 @@ fn get_body(block: &Block) -> Result<&[u8]> {
     Ok(body)
 }
 
-pub(crate) fn sign_block(versioned_block: &mut VersionedBlock, write_cap: WriteCap) -> Result<()> {
+pub(crate) fn sign_block(versioned_block: &mut VersionedBlock, writecap: Writecap) -> Result<()> {
     let VersionedBlock::V0(block) = versioned_block;
-    block.write_cap = write_cap;
+    block.writecap = writecap;
     let body = get_body(block)?;
     let sig_header = SigHeader::from(&*block);
     let header = to_vec(&sig_header)?;
-    let mut sign_algo = SignAlgo::try_from(&block.write_cap.signing_key)?;
+    let mut sign_algo = SignAlgo::try_from(&block.writecap.signing_key)?;
     sign_algo.sign([header.as_slice(), body].into_iter())?;
     block.signature = sign_algo.signature;
     Ok(())
@@ -564,7 +564,7 @@ pub(crate) fn verify_block(versioned_block: &VersionedBlock) -> Result<bool> {
     let body = get_body(block)?;
     let sig_header = SigHeader::from(&*block);
     let header = to_vec(&sig_header)?;
-    let verify_algo = VerifyAlgo::try_from(&block.write_cap.signing_key)?;
+    let verify_algo = VerifyAlgo::try_from(&block.writecap.signing_key)?;
     let parts = [header.as_slice(), body].into_iter();
     if !verify_algo.verify(parts, block.signature.as_slice())? {
         return Ok(false);
@@ -573,29 +573,29 @@ pub(crate) fn verify_block(versioned_block: &VersionedBlock) -> Result<bool> {
 }
 
 #[derive(Serialize)]
-struct WriteCapSig<'a> {
+struct WritecapSig<'a> {
     issued_to: &'a Principal,
     path: &'a Path,
     expires: &'a Epoch,
     signing_key: &'a Key,
 }
 
-impl<'a> From<&'a WriteCap> for WriteCapSig<'a> {
-    fn from(write_cap: &'a WriteCap) -> WriteCapSig<'a> {
-        WriteCapSig {
-            issued_to: &write_cap.issued_to,
-            path: &write_cap.path,
-            expires: &write_cap.expires,
-            signing_key: &write_cap.signing_key,
+impl<'a> From<&'a Writecap> for WritecapSig<'a> {
+    fn from(writecap: &'a Writecap) -> WritecapSig<'a> {
+        WritecapSig {
+            issued_to: &writecap.issued_to,
+            path: &writecap.path,
+            expires: &writecap.expires,
+            signing_key: &writecap.signing_key,
         }
     }
 }
 
-pub(crate) fn sign_writecap(write_cap: &mut WriteCap, key: &Key) -> Result<()> {
+pub(crate) fn sign_writecap(writecap: &mut Writecap, key: &Key) -> Result<()> {
     let mut sign_algo = SignAlgo::try_from(key)?;
-    let sig_input = to_vec(&WriteCapSig::from(&*write_cap))?;
+    let sig_input = to_vec(&WritecapSig::from(&*writecap))?;
     sign_algo.sign([sig_input.as_slice()].into_iter())?;
-    write_cap.signature = sign_algo.signature;
+    writecap.signature = sign_algo.signature;
     Ok(())
 }
 
@@ -619,45 +619,45 @@ pub(crate) enum WritecapAuthzErr {
     Crypto(String),
 }
 
-/// Verifies that the given `WriteCap` actually grants permission to write to the given `Path`.
+/// Verifies that the given `Writecap` actually grants permission to write to the given `Path`.
 pub(crate) fn verify_writecap(
-    mut write_cap: &WriteCap, path: &Path
+    mut writecap: &Writecap, path: &Path
 ) -> std::result::Result<(), WritecapAuthzErr> {
-    let mut prev: Option<&WriteCap> = None;
+    let mut prev: Option<&Writecap> = None;
     let mut sig_input = Vec::new();
     let now = Epoch::now();
     loop {
-        if !write_cap.path.contains(path) {
+        if !writecap.path.contains(path) {
             return Err(WritecapAuthzErr::UnauthorizedPath);
         }
-        if write_cap.expires <= now {
+        if writecap.expires <= now {
             return Err(WritecapAuthzErr::Expired);
         }
         if let Some(prev) = &prev {
-            if prev.signing_key.to_principal() != write_cap.issued_to {
+            if prev.signing_key.to_principal() != writecap.issued_to {
                 return Err(WritecapAuthzErr::NotChained);
             }
         }
-        let sig = WriteCapSig::from(write_cap);
+        let sig = WritecapSig::from(writecap);
         sig_input.clear();
         write_to(&sig, &mut sig_input).map_err(|e| WritecapAuthzErr::Serde(e.to_string()))?;
-        let verify_algo = VerifyAlgo::try_from(&write_cap.signing_key)
+        let verify_algo = VerifyAlgo::try_from(&writecap.signing_key)
             .map_err(|e| WritecapAuthzErr::Crypto(e.to_string()))?;
         let valid = verify_algo
-            .verify([sig_input.as_slice()].into_iter(), write_cap.signature.as_slice())
+            .verify([sig_input.as_slice()].into_iter(), writecap.signature.as_slice())
             .map_err(|e| WritecapAuthzErr::Crypto(e.to_string()))?;
         if !valid {
             return Err(WritecapAuthzErr::InvalidSignature);
         }
-        match &write_cap.next {
+        match &writecap.next {
             Some(next) => {
-                prev = Some(write_cap);
-                write_cap = next;
+                prev = Some(writecap);
+                writecap = next;
             },
             None => {
                 // We're at the root key. As long as the signer of this writecap is the owner of
                 // the path, then the writecap is valid.
-                if write_cap.signing_key.to_principal() == path.owner {
+                if writecap.signing_key.to_principal() == path.owner {
                     return Ok(());
                 }
                 else {
@@ -700,8 +700,8 @@ mod tests {
 
     fn make_versioned_block(principal: Principal, block_key: Key) -> Result<VersionedBlock> {
         let mut block = make_block()?;
-        block.read_caps.clear();
-        block.read_caps.insert(principal, Cryptotext::Plain(block_key));
+        block.readcaps.clear();
+        block.readcaps.insert(principal, Cryptotext::Plain(block_key));
         Ok(VersionedBlock::V0(block))
     }
 
@@ -751,7 +751,7 @@ mod tests {
         let block_key = Key::Aes256Ctr { key: BLOCK_KEY, iv: BLOCK_IV };
         let mut block = make_versioned_block(principal.clone(), block_key)?;
         let key = Key::generate(KeyId::Rsa)?;
-        let write_cap = WriteCap {
+        let writecap = Writecap {
             issued_to: Principal(Hash::Sha2_256(PRINCIPAL)),
             path: make_path(vec!["contacts", "emergency"]),
             expires: Epoch(1649904316),
@@ -759,8 +759,8 @@ mod tests {
             signature: Signature::Rsa(SIGNATURE),
             next: None,
         };
-        block = encrypt_block(block, &principal, &write_cap.signing_key)?;
-        sign_block(&mut block, write_cap)?;
+        block = encrypt_block(block, &principal, &writecap.signing_key)?;
+        sign_block(&mut block, writecap)?;
         assert_eq!(true, verify_block(&block)?);
         Ok(())
     }

+ 12 - 12
crates/node/src/main.rs

@@ -34,13 +34,13 @@ enum VersionedBlock {
 struct Block {
     /// Identifies this block and defines its location in the tree.
     path: Path,
-    /// This field contains a collection of `ReadCap`s indexed by the principal who holds them.
-    /// `ReadCap`s are envelopments of the key used to encrypt this block.
-    read_caps: HashMap<Principal, Cryptotext<Key>>,
+    /// This field contains a collection of `Readcap`s indexed by the principal who holds them.
+    /// `Readcap`s are envelopments of the key used to encrypt this block.
+    readcaps: HashMap<Principal, Cryptotext<Key>>,
     /// This field is used to verify that the signer of this block had permission to write it.
     /// It contains a certificate chain that must lead back to the root key for the tree this block
     /// is part of.
-    write_cap: WriteCap,
+    writecap: Writecap,
     /// The encrypted data contained in this block.
     body: Cryptotext<Vec<u8>>,
     /// The contents of the block are covered by a digital signature contained in this field.  
@@ -49,23 +49,23 @@ struct Block {
 
 /// An envelopment of a key, which is tagged with the principal who the key is meant for.
 #[derive(Debug, PartialEq, Serialize, Deserialize)]
-struct ReadCap {
-    /// The principal this `ReadCap` was issued to.
+struct Readcap {
+    /// The principal this `Readcap` was issued to.
     issued_to: Principal,
     /// An encipherment of a block key using the public key of the principal.
     key: Cryptotext<Key>,
 }
 
-impl ReadCap {
-    fn new(issued_to: Hash, key: Cryptotext<Key>) -> ReadCap {
-        ReadCap { issued_to: Principal(issued_to), key }
+impl Readcap {
+    fn new(issued_to: Hash, key: Cryptotext<Key>) -> Readcap {
+        Readcap { issued_to: Principal(issued_to), key }
     }
 }
 
 /// Verifies that a principal is authorized to write blocks in a tree.
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
-struct WriteCap {
-    /// The principal this `WriteCap` was issued to.
+struct Writecap {
+    /// The principal this `Writecap` was issued to.
     issued_to: Principal,
     /// The path where this write caps's validity begins.
     path: Path,
@@ -76,7 +76,7 @@ struct WriteCap {
     /// A digital signature which covers all of the fields in the write cap except for next.
     signature: Signature,
     /// The next write cap in the chain leading back to the root.
-    next: Option<Box<WriteCap>>,
+    next: Option<Box<Writecap>>,
 }
 
 /// Fragments are created from blocks using Erasure Encoding and stored with other nodes in the

+ 1 - 1
crates/node/src/serde_tests.rs

@@ -54,7 +54,7 @@ fn roundtrip_write_cap() -> Result<()> {
 
 #[test]
 fn roundtrip_read_cap() -> Result<()> {
-    let expected = make_read_cap();
+    let expected = make_readcap();
     let ser_result = to_vec(&expected);
     let de_result = from_vec(&ser_result?);
     assert_eq!(expected, de_result?);

+ 17 - 17
crates/node/src/test_helpers.rs

@@ -479,7 +479,7 @@ pub(crate) fn make_path(rel_components: Vec<&str>) -> Path {
     make_path_with_owner(make_principal(), rel_components)
 }
 
-pub(crate) fn make_writecap() -> Result<WriteCap> {
+pub(crate) fn make_writecap() -> Result<Writecap> {
     let (root_writecap, root_key) = make_self_signed_writecap()?;
     let public_key = Key::Rsa {
         public: Vec::from(NODE_PUBLIC_KEY),
@@ -491,15 +491,15 @@ pub(crate) fn make_writecap() -> Result<WriteCap> {
 }
 
 pub(crate) fn make_writecap_trusted_by(
-    next: WriteCap, signing_key: &Key, public_key: &Key, path_components: Vec<&str>
-) -> Result<WriteCap> {
+    next: Writecap, signing_key: &Key, public_key: &Key, path_components: Vec<&str>
+) -> Result<Writecap> {
     let hour_hence = Epoch::now() + Duration::from_secs(3600);
     let mut signing_public_key = signing_key.clone();
     match &mut signing_public_key {
         Key::Rsa { private, .. } => private.take(),
         _ => return Err(Error::Message("unexpected key type".to_string())),
     };
-    let mut writecap = WriteCap {
+    let mut writecap = Writecap {
         issued_to: public_key.to_principal(),
         path: make_path_with_owner(next.path.owner.clone(), path_components),
         expires: hour_hence,
@@ -511,7 +511,7 @@ pub(crate) fn make_writecap_trusted_by(
     Ok(writecap)
 }
 
-pub(crate) fn make_self_signed_writecap() -> Result<(WriteCap, Key)> {
+pub(crate) fn make_self_signed_writecap() -> Result<(Writecap, Key)> {
     let private_key = Key::Rsa {
         public: Vec::from(ROOT_PUBLIC_KEY),
         private: Some(Vec::from(ROOT_PRIVATE_KEY)),
@@ -520,7 +520,7 @@ pub(crate) fn make_self_signed_writecap() -> Result<(WriteCap, Key)> {
     Ok((make_self_signed_writecap_with(&private_key)?, private_key))
 }
 
-pub(crate) fn make_self_signed_writecap_with(private_key: &Key) -> Result<WriteCap> {
+pub(crate) fn make_self_signed_writecap_with(private_key: &Key) -> Result<Writecap> {
     let mut public_key = private_key.clone();
     match &mut public_key {
         Key::Rsa { private, .. } => private.take(),
@@ -528,7 +528,7 @@ pub(crate) fn make_self_signed_writecap_with(private_key: &Key) -> Result<WriteC
     };
     let root_principal = public_key.to_principal();
     let hour_hence = Epoch::now() + Duration::from_secs(3600);
-    let mut write_cap = WriteCap {
+    let mut writecap = Writecap {
         issued_to: root_principal.clone(),
         path: make_path_with_owner(root_principal, vec![]),
         expires: hour_hence,
@@ -536,28 +536,28 @@ pub(crate) fn make_self_signed_writecap_with(private_key: &Key) -> Result<WriteC
         signature: Signature::default(),
         next: None,
     };
-    crypto::sign_writecap(&mut write_cap, &private_key)
+    crypto::sign_writecap(&mut writecap, &private_key)
         .map_err(|e| Error::Message(e.to_string()))?;
-    Ok(write_cap)
+    Ok(writecap)
 }
 
-pub(crate) fn make_read_cap() -> ReadCap {
-    ReadCap::new(
+pub(crate) fn make_readcap() -> Readcap {
+    Readcap::new(
         Hash::Sha2_256(PRINCIPAL), Cryptotext::Plain(Key::Aes256Ctr { key: KEY, iv: IV })
     )
 }
 
 pub(crate) fn make_block() -> Result<Block> {
-    let mut read_caps = HashMap::new();
+    let mut readcaps = HashMap::new();
     {
-        let read_cap = make_read_cap();
-        read_caps.insert(read_cap.issued_to, read_cap.key);
+        let readcap = make_readcap();
+        readcaps.insert(readcap.issued_to, readcap.key);
     }
-    let write_cap = make_writecap()?;
+    let writecap = make_writecap()?;
     Ok(Block {
         path: make_path(vec!["apps", "verse"]),
-        read_caps,
-        write_cap,
+        readcaps,
+        writecap,
         body: Cryptotext::Plain(Vec::from(PAYLOAD)),
         signature: Signature::Rsa(SIGNATURE)
     })