|
1 | | -use anyhow::Result; |
2 | | -use openssl::pkey::{Id, PKey}; |
| 1 | +use pithos_lib::error::PithosError; |
3 | 2 | use std::fs::File; |
4 | 3 | use std::io::Read; |
5 | 4 | use std::path::PathBuf; |
| 5 | +use x25519_dalek::{PublicKey, StaticSecret}; |
6 | 6 |
|
7 | | -//TODO: Try to load openSSL pem and if fails try to load Crypt4GH pem |
8 | | -pub fn load_key_from_pem(filepath: &PathBuf, private: bool) -> Result<[u8; 32]> { |
| 7 | +pub fn load_private_key_from_pem(filepath: &PathBuf) -> Result<StaticSecret, PithosError> { |
9 | 8 | // Open file handle and read file bytes |
10 | 9 | let mut file = File::open(filepath)?; |
11 | 10 | let mut file_content = vec![0u8; file.metadata()?.len() as usize]; |
12 | 11 | file.read_exact(&mut file_content)?; |
13 | 12 |
|
14 | | - // Parse into key |
15 | | - let mut key_bytes: [u8; 32] = [0; 32]; |
16 | | - if private { |
17 | | - key_bytes.copy_from_slice( |
18 | | - PKey::private_key_from_pem(&file_content)? |
19 | | - .raw_private_key()? |
20 | | - .as_slice(), |
21 | | - ); |
22 | | - } else { |
23 | | - key_bytes.copy_from_slice( |
24 | | - PKey::public_key_from_pem(&file_content)? |
25 | | - .raw_public_key()? |
26 | | - .as_slice(), |
27 | | - ); |
28 | | - } |
29 | | - |
30 | | - Ok(key_bytes) |
| 13 | + Ok(pithos_lib::helpers::x25519_keys::private_key_from_pem_bytes(&file_content)?) |
31 | 14 | } |
32 | 15 |
|
33 | | -pub fn _load_key_from_string(key_bytes: &[u8], private: bool) -> Result<[u8; 32]> { |
34 | | - // Parse into key |
35 | | - let mut x25519_key_bytes: [u8; 32] = [0; 32]; |
36 | | - if private { |
37 | | - x25519_key_bytes.copy_from_slice( |
38 | | - PKey::private_key_from_raw_bytes(key_bytes, Id::X25519)? |
39 | | - .raw_private_key()? |
40 | | - .as_slice(), |
41 | | - ); |
42 | | - } else { |
43 | | - x25519_key_bytes.copy_from_slice( |
44 | | - PKey::public_key_from_raw_bytes(key_bytes, Id::X25519)? |
45 | | - .raw_public_key()? |
46 | | - .as_slice(), |
47 | | - ); |
48 | | - } |
| 16 | +pub fn load_public_key_from_pem(filepath: &PathBuf) -> Result<PublicKey, PithosError> { |
| 17 | + // Open file handle and read file bytes |
| 18 | + let mut file = File::open(filepath)?; |
| 19 | + let mut file_content = vec![0u8; file.metadata()?.len() as usize]; |
| 20 | + file.read_exact(&mut file_content)?; |
49 | 21 |
|
50 | | - Ok(x25519_key_bytes) |
| 22 | + Ok(pithos_lib::helpers::x25519_keys::public_key_from_pem_bytes( |
| 23 | + &file_content, |
| 24 | + )?) |
51 | 25 | } |
0 commit comments