Refactored crypto
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
|
pub mod crypto;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod signature;
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ impl UserConnections {
|
|||||||
return Err(anyhow::anyhow!("Client's hostname wasn't correct"));
|
return Err(anyhow::anyhow!("Client's hostname wasn't correct"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let Ok(public_key) = crate::signature::from_string(&public_key_string) else {
|
let Ok(public_key) = crate::crypto::from_string(&public_key_string) else {
|
||||||
send_socket(
|
send_socket(
|
||||||
&mut socket,
|
&mut socket,
|
||||||
&ClientMethod::Error {
|
&ClientMethod::Error {
|
||||||
@@ -81,7 +81,7 @@ impl UserConnections {
|
|||||||
if public_key
|
if public_key
|
||||||
.verify_strict(
|
.verify_strict(
|
||||||
format!("{timestamp}@{hostname}").as_bytes(),
|
format!("{timestamp}@{hostname}").as_bytes(),
|
||||||
&crate::signature::from_string_sig(&signature)?,
|
&crate::crypto::from_string_sig(&signature)?,
|
||||||
)
|
)
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
@@ -100,8 +100,8 @@ impl UserConnections {
|
|||||||
send_socket(
|
send_socket(
|
||||||
&mut socket,
|
&mut socket,
|
||||||
&ClientMethod::Initialized {
|
&ClientMethod::Initialized {
|
||||||
public_key: crate::signature::to_string(&server.key.verifying_key()),
|
public_key: crate::crypto::to_string(&server.key.verifying_key()),
|
||||||
signature: crate::signature::to_string_sig(&server.key.sign(
|
signature: crate::crypto::to_string_sig(&server.key.sign(
|
||||||
format!("{server_timestamp}@{hostname}@{public_key_string}").as_bytes(),
|
format!("{server_timestamp}@{hostname}@{public_key_string}").as_bytes(),
|
||||||
)),
|
)),
|
||||||
|
|
||||||
|
|||||||
@@ -24,13 +24,13 @@ pub async fn send_message(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let server_pubkey_string = crate::signature::to_string(&server.key.verifying_key());
|
let server_pubkey_string = crate::crypto::to_string(&server.key.verifying_key());
|
||||||
let signed_string = format!(
|
let signed_string = format!(
|
||||||
"{}@{}@{}",
|
"{}@{}@{}",
|
||||||
message.timestamp, server_pubkey_string, message.content
|
message.timestamp, server_pubkey_string, message.content
|
||||||
);
|
);
|
||||||
|
|
||||||
let signature = crate::signature::from_string_sig(&message.signature)
|
let signature = crate::crypto::from_string_sig(&message.signature)
|
||||||
.map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?;
|
.map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?;
|
||||||
|
|
||||||
verifying_key
|
verifying_key
|
||||||
@@ -39,7 +39,7 @@ pub async fn send_message(
|
|||||||
|
|
||||||
let stored = StoredMessage {
|
let stored = StoredMessage {
|
||||||
id: uuid::Uuid::new_v4().to_string(),
|
id: uuid::Uuid::new_v4().to_string(),
|
||||||
author: crate::signature::to_string(&verifying_key),
|
author: crate::crypto::to_string(&verifying_key),
|
||||||
is_edited: false,
|
is_edited: false,
|
||||||
data: message,
|
data: message,
|
||||||
};
|
};
|
||||||
@@ -92,18 +92,18 @@ pub async fn edit_message(
|
|||||||
.get_message(&channel_id, &message_id)?
|
.get_message(&channel_id, &message_id)?
|
||||||
.ok_or_else(|| anyhow::anyhow!("Message not found"))?;
|
.ok_or_else(|| anyhow::anyhow!("Message not found"))?;
|
||||||
|
|
||||||
let author_pubkey = crate::signature::to_string(&verifying_key);
|
let author_pubkey = crate::crypto::to_string(&verifying_key);
|
||||||
if existing.author != author_pubkey {
|
if existing.author != author_pubkey {
|
||||||
anyhow::bail!("Not authorized to edit this message");
|
anyhow::bail!("Not authorized to edit this message");
|
||||||
}
|
}
|
||||||
|
|
||||||
let server_pubkey_string = crate::signature::to_string(&server.key.verifying_key());
|
let server_pubkey_string = crate::crypto::to_string(&server.key.verifying_key());
|
||||||
let signed_string = format!(
|
let signed_string = format!(
|
||||||
"{}@{}@{}",
|
"{}@{}@{}",
|
||||||
existing.data.timestamp, server_pubkey_string, new_content
|
existing.data.timestamp, server_pubkey_string, new_content
|
||||||
);
|
);
|
||||||
|
|
||||||
let signature = crate::signature::from_string_sig(&new_signature)
|
let signature = crate::crypto::from_string_sig(&new_signature)
|
||||||
.map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?;
|
.map_err(|_| anyhow::anyhow!("Invalid signature encoding"))?;
|
||||||
|
|
||||||
verifying_key
|
verifying_key
|
||||||
@@ -146,7 +146,7 @@ pub async fn delete_message(
|
|||||||
.get_message(&channel_id, &message_id)?
|
.get_message(&channel_id, &message_id)?
|
||||||
.ok_or_else(|| anyhow::anyhow!("Message not found"))?;
|
.ok_or_else(|| anyhow::anyhow!("Message not found"))?;
|
||||||
|
|
||||||
let author_pubkey = crate::signature::to_string(&verifying_key);
|
let author_pubkey = crate::crypto::to_string(&verifying_key);
|
||||||
if existing.author != author_pubkey {
|
if existing.author != author_pubkey {
|
||||||
anyhow::bail!("Not authorized to delete this message");
|
anyhow::bail!("Not authorized to delete this message");
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -35,7 +35,7 @@ pub struct Server {
|
|||||||
impl Server {
|
impl Server {
|
||||||
pub async fn new() -> anyhow::Result<Arc<Self>> {
|
pub async fn new() -> anyhow::Result<Arc<Self>> {
|
||||||
Ok(Arc::new(Self {
|
Ok(Arc::new(Self {
|
||||||
key: crate::signature::get().await?,
|
key: crate::crypto::get().await?,
|
||||||
config: Config::get().await?,
|
config: Config::get().await?,
|
||||||
clients: Mutex::new(HashMap::new()),
|
clients: Mutex::new(HashMap::new()),
|
||||||
message_store: MessageStore::new(PathBuf::from("messages"))?,
|
message_store: MessageStore::new(PathBuf::from("messages"))?,
|
||||||
@@ -53,7 +53,7 @@ impl Server {
|
|||||||
Ok((client, public_key, meta)) => {
|
Ok((client, public_key, meta)) => {
|
||||||
if let Err(e) = s
|
if let Err(e) = s
|
||||||
.user_store
|
.user_store
|
||||||
.upsert_user(&crate::signature::to_string(&public_key), &meta)
|
.upsert_user(&crate::crypto::to_string(&public_key), &meta)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
eprintln!("Failed to upsert client: {e}");
|
eprintln!("Failed to upsert client: {e}");
|
||||||
|
|||||||
Reference in New Issue
Block a user