|
@@ -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(())
|
|
|
}
|