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