Add signature basics
Add a way to sign and verify keys. Still need to figure out the keyserver situation and how to make that shit work
This commit is contained in:
+36
-3
@@ -1,9 +1,13 @@
|
||||
use uuid::Uuid;
|
||||
use crate::message::{MessageId,TaggedMessage,VerificationError,Relevance};
|
||||
use crate::message::{MessageId,TaggedMessage,Relevance};
|
||||
use crate::state::{self,StateValue,StatePath,StatePermission,StatePermissionKey};
|
||||
use crate::{RoomId,Group,Role,User,GroupPower,ServerAddr};
|
||||
use ed25519::signature::{Signer,Verifier};
|
||||
use ed25519::Signature;
|
||||
use rmp_serde::encode::Serializer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::OffsetDateTime;
|
||||
use thiserror::Error;
|
||||
|
||||
//StatePath [String]
|
||||
//octal is fine for now
|
||||
@@ -61,9 +65,38 @@ impl SignedClientMessage {
|
||||
target: self.target.unwrap_or(servername)
|
||||
}
|
||||
}
|
||||
pub fn verify(&self) -> Result<bool,VerificationError> {
|
||||
unimplemented!()
|
||||
// Canonical way to sign messages. Glue the message, target, and timestamp together. Serialize
|
||||
// them, then sign those bytes. It is slightly ambiguous how target=None messages should
|
||||
// by signed vs target=Some(local_server) especially if rewriting of the target happens.
|
||||
// This makes signature verification harder as you would have to check both
|
||||
// cases to see if either correctly verifies.
|
||||
pub fn sign<S: Signer<ed25519::Signature>>(&mut self, signer: S) -> Result<(),SignatureError> {
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
(&self.message,&self.target,&self.timestamp).serialize(&mut Serializer::new(&mut bytes).with_struct_map())?;
|
||||
let result = signer.try_sign(&bytes)?;
|
||||
|
||||
|
||||
self.signature = Box::new(result.to_bytes());
|
||||
Ok(())
|
||||
}
|
||||
pub fn verify<V: Verifier<ed25519::Signature>>(&self, verifier: V) -> Result<(),SignatureError> {
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
(&self.message,&self.target,&self.timestamp).serialize(&mut Serializer::new(&mut bytes).with_struct_map())?;
|
||||
|
||||
|
||||
let sig = Signature::from_slice(&self.signature)?;
|
||||
Ok(verifier.verify(&bytes,&sig)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,Error)]
|
||||
pub enum SignatureError {
|
||||
#[error("Problem while making eliptic curve {0}")]
|
||||
Ed25519(#[from] ed25519::Error),
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(#[from] rmp_serde::encode::Error),
|
||||
}
|
||||
#[derive(Serialize,Deserialize,Clone,Debug)]
|
||||
pub enum AuthMethod {
|
||||
|
||||
Reference in New Issue
Block a user