Rename utils::crypto module to crypto_rsa

This commit is contained in:
silverpill 2022-11-13 18:43:57 +00:00
parent 4d85638d8c
commit c11daa21a0
13 changed files with 49 additions and 46 deletions

View file

@ -18,7 +18,7 @@ use crate::models::profiles::types::{
PaymentOption,
};
use crate::models::users::types::User;
use crate::utils::crypto::{deserialize_private_key, get_public_key_pem};
use crate::utils::crypto_rsa::{deserialize_private_key, get_public_key_pem};
use crate::utils::files::get_file_url;
use crate::utils::urls::get_hostname;
use super::attachments::{
@ -346,8 +346,8 @@ pub fn get_instance_actor(
mod tests {
use url::Url;
use crate::models::profiles::types::DbActorProfile;
use crate::utils::crypto::{
generate_weak_private_key,
use crate::utils::crypto_rsa::{
generate_weak_rsa_key,
serialize_private_key,
};
use super::*;
@ -378,7 +378,7 @@ mod tests {
#[test]
fn test_local_actor() {
let private_key = generate_weak_private_key().unwrap();
let private_key = generate_weak_rsa_key().unwrap();
let private_key_pem = serialize_private_key(&private_key).unwrap();
let profile = DbActorProfile {
username: "testuser".to_string(),
@ -418,7 +418,7 @@ mod tests {
#[test]
fn test_instance_actor() {
let instance_url = Url::parse("https://example.com/").unwrap();
let instance_rsa_key = generate_weak_private_key().unwrap();
let instance_rsa_key = generate_weak_rsa_key().unwrap();
let instance = Instance::new(instance_url, instance_rsa_key);
let actor = get_instance_actor(&instance).unwrap();
assert_eq!(actor.id, "https://example.com/actor");

View file

@ -23,7 +23,7 @@ use crate::models::profiles::queries::{
search_profiles_by_did_only,
};
use crate::models::profiles::types::DbActorProfile;
use crate::utils::crypto::deserialize_public_key;
use crate::utils::crypto_rsa::deserialize_public_key;
use super::fetcher::helpers::get_or_import_profile_by_actor_id;
use super::receiver::HandlerError;

View file

@ -103,8 +103,8 @@ pub async fn prepare_signed_update_person(
#[cfg(test)]
mod tests {
use crate::models::profiles::types::DbActorProfile;
use crate::utils::crypto::{
generate_weak_private_key,
use crate::utils::crypto_rsa::{
generate_weak_rsa_key,
serialize_private_key,
};
use super::*;
@ -113,7 +113,7 @@ mod tests {
#[test]
fn test_build_update_person() {
let private_key = generate_weak_private_key().unwrap();
let private_key = generate_weak_rsa_key().unwrap();
let private_key_pem = serialize_private_key(&private_key).unwrap();
let user = User {
private_key: private_key_pem,

View file

@ -18,7 +18,7 @@ use crate::json_signatures::create::{
JsonSignatureError,
};
use crate::models::users::types::User;
use crate::utils::crypto::deserialize_private_key;
use crate::utils::crypto_rsa::deserialize_private_key;
use crate::utils::urls::get_hostname;
use super::actors::types::Actor;
use super::constants::{AP_MEDIA_TYPE, ACTOR_KEY_SUFFIX};

View file

@ -29,8 +29,8 @@ use crate::models::users::queries::{
};
use crate::monero::wallet::create_monero_wallet;
use crate::utils::{
crypto::{
generate_private_key,
crypto_rsa::{
generate_rsa_key,
serialize_private_key,
},
files::remove_files,
@ -69,7 +69,7 @@ pub struct GenerateRsaKey;
impl GenerateRsaKey {
pub fn execute(&self) -> () {
let private_key = generate_private_key().unwrap();
let private_key = generate_rsa_key().unwrap();
let private_key_str = serialize_private_key(&private_key).unwrap();
println!("{}", private_key_str);
}

View file

@ -10,9 +10,9 @@ use url::Url;
use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
use crate::activitypub::identifiers::local_instance_actor_id;
use crate::errors::ConversionError;
use crate::utils::crypto::{
use crate::utils::crypto_rsa::{
deserialize_private_key,
generate_private_key,
generate_rsa_key,
serialize_private_key,
};
use crate::utils::files::{set_file_permissions, write_file};
@ -235,7 +235,7 @@ fn read_instance_rsa_key(storage_dir: &Path) -> RsaPrivateKey {
.expect("failed to read instance RSA key");
private_key
} else {
let private_key = generate_private_key()
let private_key = generate_rsa_key()
.expect("failed to generate RSA key");
let private_key_str = serialize_private_key(&private_key)
.expect("failed to serialize RSA key");
@ -284,13 +284,13 @@ pub fn parse_config() -> Config {
#[cfg(test)]
mod tests {
use crate::utils::crypto::generate_weak_private_key;
use crate::utils::crypto_rsa::generate_weak_rsa_key;
use super::*;
#[test]
fn test_instance_url_https_dns() {
let instance_url = Url::parse("https://example.com/").unwrap();
let instance_rsa_key = generate_weak_private_key().unwrap();
let instance_rsa_key = generate_weak_rsa_key().unwrap();
let instance = Instance {
_url: instance_url,
_version: "1.0.0".to_string(),
@ -307,7 +307,7 @@ mod tests {
#[test]
fn test_instance_url_http_ipv4() {
let instance_url = Url::parse("http://1.2.3.4:3777/").unwrap();
let instance_rsa_key = generate_weak_private_key().unwrap();
let instance_rsa_key = generate_weak_rsa_key().unwrap();
let instance = Instance {
_url: instance_url,
_version: "1.0.0".to_string(),

View file

@ -2,7 +2,7 @@ use actix_web::http::Method;
use chrono::Utc;
use rsa::RsaPrivateKey;
use crate::utils::crypto::{sign_message, get_message_digest};
use crate::utils::crypto_rsa::{get_message_digest, sign_message};
const HTTP_SIGNATURE_ALGORITHM: &str = "rsa-sha256";
const HTTP_SIGNATURE_DATE_FORMAT: &str = "%a, %d %b %Y %T GMT";
@ -88,13 +88,13 @@ pub fn create_http_signature(
#[cfg(test)]
mod tests {
use crate::utils::crypto::generate_weak_private_key;
use crate::utils::crypto_rsa::generate_weak_rsa_key;
use super::*;
#[test]
fn test_create_signature_get() {
let request_url = "https://example.org/inbox";
let signer_key = generate_weak_private_key().unwrap();
let signer_key = generate_weak_rsa_key().unwrap();
let signer_key_id = "https://myserver.org/actor#main-key";
let headers = create_http_signature(
@ -123,7 +123,7 @@ mod tests {
fn test_create_signature_post() {
let request_url = "https://example.org/inbox";
let request_body = "{}";
let signer_key = generate_weak_private_key().unwrap();
let signer_key = generate_weak_rsa_key().unwrap();
let signer_key_id = "https://myserver.org/actor#main-key";
let result = create_http_signature(

View file

@ -5,7 +5,7 @@ use chrono::{DateTime, Duration, TimeZone, Utc};
use regex::Regex;
use rsa::RsaPublicKey;
use crate::utils::crypto::verify_signature;
use crate::utils::crypto_rsa::verify_rsa_signature;
#[derive(thiserror::Error, Debug)]
pub enum HttpSignatureVerificationError {
@ -120,7 +120,7 @@ pub fn verify_http_signature(
if expires_at < Utc::now() {
log::warn!("signature has expired");
};
let is_valid_signature = verify_signature(
let is_valid_signature = verify_rsa_signature(
signer_key,
&signature_data.message,
&signature_data.signature,
@ -139,7 +139,7 @@ mod tests {
Uri,
};
use crate::http_signatures::create::create_http_signature;
use crate::utils::crypto::generate_weak_private_key;
use crate::utils::crypto_rsa::generate_weak_rsa_key;
use super::*;
#[test]
@ -185,7 +185,7 @@ mod tests {
let request_method = Method::POST;
let request_url = "https://example.org/inbox";
let request_body = "{}";
let signer_key = generate_weak_private_key().unwrap();
let signer_key = generate_weak_rsa_key().unwrap();
let signer_key_id = "https://myserver.org/actor#main-key";
let signed_headers = create_http_signature(
request_method.clone(),

View file

@ -16,7 +16,7 @@ use crate::utils::canonicalization::{
canonicalize_object,
CanonicalizationError,
};
use crate::utils::crypto::sign_message;
use crate::utils::crypto_rsa::sign_message;
pub(super) const PROOF_KEY: &str = "proof";
pub(super) const PROOF_PURPOSE: &str = "assertionMethod";
@ -130,12 +130,12 @@ pub fn is_object_signed(object: &Value) -> bool {
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::utils::crypto::generate_weak_private_key;
use crate::utils::crypto_rsa::generate_weak_rsa_key;
use super::*;
#[test]
fn test_sign_object() {
let signer_key = generate_weak_private_key().unwrap();
let signer_key = generate_weak_rsa_key().unwrap();
let signer_key_id = "https://example.org/users/test#main-key";
let object = json!({
"type": "Create",

View file

@ -17,7 +17,7 @@ use crate::utils::canonicalization::{
canonicalize_object,
CanonicalizationError,
};
use crate::utils::crypto::verify_signature;
use crate::utils::crypto_rsa::verify_rsa_signature;
use super::create::{
IntegrityProof,
PROOF_KEY,
@ -103,7 +103,7 @@ pub fn verify_rsa_json_signature(
signature_data: &SignatureData,
signer_key: &RsaPublicKey,
) -> Result<(), VerificationError> {
let is_valid_signature = verify_signature(
let is_valid_signature = verify_rsa_signature(
signer_key,
&signature_data.message,
&signature_data.signature,
@ -136,7 +136,7 @@ pub fn verify_minisign_json_signature(
mod tests {
use serde_json::json;
use crate::json_signatures::create::sign_object;
use crate::utils::crypto::generate_weak_private_key;
use crate::utils::crypto_rsa::generate_weak_rsa_key;
use crate::utils::currencies::Currency;
use super::*;
@ -164,7 +164,7 @@ mod tests {
#[test]
fn test_create_and_verify_signature() {
let signer_key = generate_weak_private_key().unwrap();
let signer_key = generate_weak_rsa_key().unwrap();
let signer_key_id = "https://example.org/users/test#main-key";
let object = json!({
"type": "Create",

View file

@ -74,8 +74,8 @@ use crate::models::users::types::UserCreateData;
use crate::utils::{
caip2::ChainId,
canonicalization::canonicalize_object,
crypto::{
generate_private_key,
crypto_rsa::{
generate_rsa_key,
serialize_private_key,
},
currencies::Currency,
@ -156,7 +156,7 @@ pub async fn create_account(
};
// Generate RSA private key for actor
let private_key = match web::block(generate_private_key).await {
let private_key = match web::block(generate_rsa_key).await {
Ok(Ok(private_key)) => private_key,
_ => return Err(HttpError::InternalError),
};

View file

@ -2,14 +2,14 @@ use rsa::{Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};
use rsa::pkcs8::{FromPrivateKey, FromPublicKey, ToPrivateKey, ToPublicKey};
use sha2::{Digest, Sha256};
pub fn generate_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
pub fn generate_rsa_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
let mut rng = rand::rngs::OsRng;
let bits = 2048;
RsaPrivateKey::new(&mut rng, bits)
}
#[cfg(test)]
pub fn generate_weak_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
pub fn generate_weak_rsa_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
use rand::SeedableRng;
let mut rng = rand::rngs::SmallRng::seed_from_u64(0);
let bits = 512;
@ -64,7 +64,7 @@ pub fn get_message_digest(message: &str) -> String {
digest_b64
}
pub fn verify_signature(
pub fn verify_rsa_signature(
public_key: &RsaPublicKey,
message: &str,
signature_b64: &str,
@ -82,7 +82,6 @@ pub fn verify_signature(
#[cfg(test)]
mod tests {
use rand::rngs::OsRng;
use super::*;
#[test]
@ -95,20 +94,24 @@ YsFtrgWDQ/s8k86sNBU+Ce2GOL7seh46kyAWgJeohh4Rcrr23rftHbvxOcRM8VzYuCeb1DgVhPGtA0xU
#[test]
fn test_public_key_serialization_deserialization() {
let private_key = RsaPrivateKey::new(&mut OsRng, 512).unwrap();
let private_key = generate_weak_rsa_key().unwrap();
let public_key_pem = get_public_key_pem(&private_key).unwrap();
let public_key = deserialize_public_key(&public_key_pem).unwrap();
assert_eq!(public_key, RsaPublicKey::from(&private_key));
}
#[test]
fn test_verify_signature() {
let private_key = RsaPrivateKey::new(&mut OsRng, 512).unwrap();
fn test_verify_rsa_signature() {
let private_key = generate_weak_rsa_key().unwrap();
let message = "test".to_string();
let signature = sign_message(&private_key, &message).unwrap();
let public_key = RsaPublicKey::from(&private_key);
let is_valid = verify_signature(&public_key, &message, &signature).unwrap();
let is_valid = verify_rsa_signature(
&public_key,
&message,
&signature,
).unwrap();
assert_eq!(is_valid, true);
}
}

View file

@ -1,6 +1,6 @@
pub mod caip2;
pub mod canonicalization;
pub mod crypto;
pub mod crypto_rsa;
pub mod currencies;
pub mod files;
pub mod html;