Skip to main content

sequoia_cryptoki/
builder.rs

1use std::path::PathBuf;
2use std::sync::OnceLock;
3
4use sequoia_directories::Home;
5
6use crate::Context;
7use crate::Result;
8
9enum ConfigFile {
10    /// Use the default configuration file based on the Sequoia home
11    /// directory.
12    Default,
13
14    /// Don't load a configuration file.
15    None,
16
17    /// Path to a configuration file to load.
18    Path(PathBuf),
19}
20
21/// A builder to configure a Context.
22///
23/// # Examples
24///
25/// Create a [`Context`] using the defaults.
26///
27/// ```rust
28/// use sequoia_cryptoki::ContextBuilder;
29///
30/// # fn main() -> anyhow::Result<()> {
31/// #
32/// let context = ContextBuilder::new().build()?;
33/// # Ok(()) }
34/// ```
35pub struct ContextBuilder {
36    /// An alternate sequoia home directory.
37    home: Option<Home>,
38
39    config_file: ConfigFile,
40
41    /// Paths to the PKCS#11 modules.
42    modules: Vec<PathBuf>,
43}
44
45impl ContextBuilder {
46    /// Returns a new, default context builder.
47    pub fn new() -> Self {
48        ContextBuilder {
49            home: None,
50            config_file: ConfigFile::Default,
51            modules: Vec::new(),
52        }
53    }
54
55    /// Use an alternate sequoia home directory.
56    ///
57    /// If `None`, uses the default.
58    pub fn home<H>(&mut self, home: H) -> &mut Self
59    where
60        H: Into<Option<Home>>
61    {
62        self.home = home.into();
63        self
64    }
65
66    /// Load an alternate configuration file.
67    ///
68    /// By default, the configuration file in the Sequoia home
69    /// directory is read.  When set, the specified file is read
70    /// instead.
71    pub fn config_file<P>(&mut self, path: P) -> &mut Self
72    where
73        P: Into<PathBuf>
74    {
75        self.config_file = ConfigFile::Path(path.into());
76        self
77    }
78
79    /// Don't load a configuration file.
80    ///
81    /// By default, the configuration file in the Sequoia home
82    /// directory is read.  When reading the configuration file is
83    /// disabled, the default configuration is used.
84    pub fn disable_config_file(&mut self) -> &mut Self {
85        self.config_file = ConfigFile::None;
86        self
87    }
88
89    /// Adds the specified module to the set of PKCS#11 modules to
90    /// load.
91    pub fn add_module<P>(&mut self, path: P) -> &mut Self
92        where P: Into<PathBuf>
93    {
94        self.modules.push(path.into());
95        self
96    }
97
98    /// Adds the specified modules to the set of PKCS#11 modules to
99    /// load.
100    pub fn extend_modules<P>(&mut self, paths: impl Iterator<Item=P>)
101        -> &mut Self
102    where P: Into<PathBuf>
103    {
104        self.modules.extend(paths.map(|p| p.into()));
105        self
106    }
107
108    /// Returns a new `Context` based on the builder.
109    pub fn build(&self) -> Result<Context> {
110        let mut config = match &self.config_file {
111            ConfigFile::Default => {
112                // Use the default configuration file.
113                let config_file = if let Some(ref home) = self.home {
114                    crate::ConfigFile::parse_home(home)?
115                } else {
116                    crate::ConfigFile::parse_default_home()?
117                };
118                config_file.to_config()?
119            }
120            ConfigFile::None => {
121                // Use the default configuration.
122                crate::Config::default()
123            }
124            ConfigFile::Path(path) => {
125                // Use the specified configuration file.
126                crate::ConfigFile::parse_file(path)?
127                    .to_config()?
128            }
129        };
130
131        config.extend_modules(self.modules.iter());
132
133        Ok(Context {
134            config,
135            backends: OnceLock::new(),
136        })
137    }
138}