1
//! Read-only C Tor key store support.
2

            
3
pub(crate) mod client;
4
pub(crate) mod err;
5
pub(crate) mod service;
6

            
7
use crate::keystore::fs_utils::{FilesystemAction, FilesystemError, RelKeyPath};
8
use crate::{KeystoreId, Result};
9

            
10
use fs_mistrust::{CheckedDir, Mistrust};
11

            
12
use std::path::{Path, PathBuf};
13

            
14
use err::CTorKeystoreError;
15

            
16
pub use client::CTorClientKeystore;
17
pub use service::CTorServiceKeystore;
18

            
19
/// Common fields for C Tor keystores.
20
struct CTorKeystore {
21
    /// The root of the key store.
22
    ///
23
    /// All the keys are read from this directory.
24
    keystore_dir: CheckedDir,
25
    /// The unique identifier of this instance.
26
    id: KeystoreId,
27
}
28

            
29
impl CTorKeystore {
30
    /// Create a new `CTorKeystore` rooted at the specified `keystore_dir` directory.
31
    ///
32
    /// This function returns an error if `keystore_dir` is not a directory,
33
    /// or if it does not conform to the requirements of the specified `Mistrust`.
34
696
    fn from_path_and_mistrust(
35
696
        keystore_dir: impl AsRef<Path>,
36
696
        mistrust: &Mistrust,
37
696
        id: KeystoreId,
38
696
    ) -> Result<Self> {
39
696
        let keystore_dir = mistrust
40
696
            .verifier()
41
696
            .check_content()
42
696
            .secure_dir(&keystore_dir)
43
696
            .map_err(|e| FilesystemError::FsMistrust {
44
                action: FilesystemAction::Init,
45
                path: keystore_dir.as_ref().into(),
46
                err: e.into(),
47
            })
48
696
            .map_err(CTorKeystoreError::Filesystem)?;
49

            
50
696
        Ok(Self { keystore_dir, id })
51
696
    }
52

            
53
    /// Return `rel_path` as a [`RelKeyPath`] relative to `keystore_dir`.
54
3044
    fn rel_path(&self, rel_path: PathBuf) -> RelKeyPath {
55
3044
        RelKeyPath::from_parts(&self.keystore_dir, rel_path)
56
3044
    }
57
}