trust-graph/src/revoke.rs

132 lines
3.9 KiB
Rust
Raw Normal View History

2021-01-15 19:37:06 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-02-09 13:26:44 +03:00
use crate::revoke::RevokeError::IncorrectSignature;
use fluence_keypair::key_pair::KeyPair;
use fluence_keypair::public_key::PublicKey;
use fluence_keypair::signature::Signature;
2021-01-20 16:14:20 +03:00
use serde::{Deserialize, Serialize};
use sha2::Digest;
2021-01-15 19:37:06 +03:00
use std::time::Duration;
use thiserror::Error as ThisError;
2021-02-08 17:35:56 +03:00
#[derive(ThisError, Debug)]
2021-02-08 17:35:56 +03:00
pub enum RevokeError {
#[error("Signature is incorrect: {0}")]
2021-04-15 14:00:27 +03:00
IncorrectSignature(
#[from]
#[source]
fluence_keypair::error::VerificationError,
2021-04-15 14:00:27 +03:00
),
2021-02-08 17:35:56 +03:00
}
2021-01-15 19:37:06 +03:00
/// "A document" that cancels trust created before.
/// TODO delete pk from Revoke (it is already in a trust node)
2021-04-15 14:00:27 +03:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2021-12-13 19:40:16 +03:00
pub struct Revocation {
2021-01-15 19:37:06 +03:00
/// who is revoked
pub pk: PublicKey,
/// date when revocation was created
pub revoked_at: Duration,
/// the issuer of this revocation
pub revoked_by: PublicKey,
/// proof of this revocation
pub signature: Signature,
2021-01-15 19:37:06 +03:00
}
2021-12-13 19:40:16 +03:00
impl Revocation {
2021-01-15 19:37:06 +03:00
#[allow(dead_code)]
pub fn new(
2021-01-15 19:37:06 +03:00
pk: PublicKey,
revoked_by: PublicKey,
revoked_at: Duration,
signature: Signature,
) -> Self {
Self {
pk,
revoked_at,
revoked_by,
signature,
}
}
/// Creates new revocation signed by a revoker.
#[allow(dead_code)]
pub fn create(revoker: &KeyPair, to_revoke: PublicKey, revoked_at: Duration) -> Self {
2021-12-13 19:40:16 +03:00
let msg = Revocation::signature_bytes(&to_revoke, revoked_at);
2021-04-15 14:00:27 +03:00
let signature = revoker.sign(&msg).unwrap();
2021-01-15 19:37:06 +03:00
2021-12-13 19:40:16 +03:00
Revocation::new(to_revoke, revoker.public(), revoked_at, signature)
2021-01-15 19:37:06 +03:00
}
pub fn signature_bytes(pk: &PublicKey, revoked_at: Duration) -> Vec<u8> {
let mut metadata = Vec::new();
2021-04-15 14:00:27 +03:00
let pk_bytes = &pk.encode();
metadata.push(pk_bytes.len() as u8);
metadata.extend(pk_bytes);
metadata.extend_from_slice(&(revoked_at.as_secs() as u64).to_le_bytes());
2021-01-15 19:37:06 +03:00
sha2::Sha256::digest(&metadata).to_vec()
2021-01-15 19:37:06 +03:00
}
/// Verifies that revocation is cryptographically correct.
2021-12-13 19:40:16 +03:00
pub fn verify(revoke: &Revocation) -> Result<(), RevokeError> {
let msg = Revocation::signature_bytes(&revoke.pk, revoke.revoked_at);
2021-01-15 19:37:06 +03:00
2021-01-18 20:08:05 +03:00
revoke
2021-01-15 19:37:06 +03:00
.revoked_by
.verify(msg.as_slice(), &revoke.signature)
.map_err(IncorrectSignature)
2021-01-15 19:37:06 +03:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2021-04-15 14:00:27 +03:00
fn test_gen_revoke_and_validate_ed25519() {
let revoker = KeyPair::generate_ed25519();
let to_revoke = KeyPair::generate_ed25519();
2021-01-15 19:37:06 +03:00
let duration = Duration::new(100, 0);
2021-12-13 19:40:16 +03:00
let revoke = Revocation::create(&revoker, to_revoke.public(), duration);
2021-01-15 19:37:06 +03:00
2021-12-13 19:40:16 +03:00
assert_eq!(Revocation::verify(&revoke).is_ok(), true);
2021-01-15 19:37:06 +03:00
}
#[test]
2021-04-15 14:00:27 +03:00
fn test_validate_corrupted_revoke_ed25519() {
let revoker = KeyPair::generate_ed25519();
let to_revoke = KeyPair::generate_ed25519();
2021-01-15 19:37:06 +03:00
let duration = Duration::new(100, 0);
2021-12-13 19:40:16 +03:00
let revoke = Revocation::create(&revoker, to_revoke.public(), duration);
2021-01-15 19:37:06 +03:00
let duration2 = Duration::new(95, 0);
2021-12-13 19:40:16 +03:00
let corrupted_revoke = Revocation::new(
2021-04-15 14:00:27 +03:00
to_revoke.public(),
revoker.public(),
2021-01-15 19:37:06 +03:00
duration2,
revoke.signature,
);
2021-12-13 19:40:16 +03:00
assert_eq!(Revocation::verify(&corrupted_revoke).is_ok(), false);
2021-01-15 19:37:06 +03:00
}
}