diff --git a/cert_lib/src/errors.rs b/cert_lib/src/errors.rs deleted file mode 100644 index af73a78..0000000 --- a/cert_lib/src/errors.rs +++ /dev/null @@ -1,9 +0,0 @@ -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum CertificateGenerateError { - #[error("Error generating RSA key")] - X509Error(#[from] openssl::error::ErrorStack), - #[error("IO Error")] - IOError(#[from] std::io::Error), -} diff --git a/cert_lib/src/lib.rs b/cert_lib/src/lib.rs index 0b212ab..0e3039f 100644 --- a/cert_lib/src/lib.rs +++ b/cert_lib/src/lib.rs @@ -13,7 +13,6 @@ use openssl::{ x509::X509Builder, }; -pub mod errors; mod root_certificate; /// 生成证书,公钥保存为.pem文件,私钥保存为.key文件 @@ -101,3 +100,25 @@ pub fn calculate_power_euqal_result(cert: X509) -> anyhow::Result { result.to_dec_str()? )) } + +/// 从.pem文件中读取X509证书 +/// +/// - `file_path`:证书文件路径 +pub fn load_certificate>(file_path: P) -> anyhow::Result { + let file_path = file_path.as_ref(); + let cert_pem = fs::read(file_path)?; + let cert = X509::from_pem(&cert_pem)?; + Ok(cert) +} + +/// 从.key文件中读取RSA私钥 +/// +/// - `file_path`:私钥文件路径 +pub fn load_private_key>( + file_path: R, +) -> anyhow::Result> { + let file_path = file_path.as_ref(); + let key_pem = fs::read(file_path)?; + let key = PKey::private_key_from_pem(&key_pem)?; + Ok(key) +}