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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::{
config::provider::yubihsm::YubihsmConfig,
error::{Error, ErrorKind},
prelude::*,
};
use once_cell::sync::Lazy;
#[cfg(all(feature = "yubihsm-server", not(feature = "yubihsm-mock")))]
use std::thread;
use std::{
process,
sync::{
atomic::{self, AtomicBool},
Mutex, MutexGuard,
},
};
use yubihsm::{Client, Connector};
#[cfg(feature = "yubihsm-server")]
use zeroize::Zeroizing;
#[cfg(not(feature = "yubihsm-mock"))]
use {
crate::config::provider::yubihsm::AdapterConfig,
tendermint::net,
yubihsm::{device::SerialNumber, HttpConfig, UsbConfig},
};
static HSM_CONNECTOR: Lazy<Connector> = Lazy::new(init_connector);
static HSM_CLIENT: Lazy<Mutex<Client>> = Lazy::new(|| Mutex::new(init_client()));
static CLI_COMMAND: AtomicBool = AtomicBool::new(false);
pub(crate) fn mark_cli_command() {
CLI_COMMAND.store(true, atomic::Ordering::SeqCst);
}
#[cfg(feature = "yubihsm-server")]
fn is_cli_command() -> bool {
CLI_COMMAND.load(atomic::Ordering::SeqCst)
}
pub fn connector() -> &'static Connector {
&HSM_CONNECTOR
}
pub fn client() -> MutexGuard<'static, Client> {
HSM_CLIENT.lock().unwrap()
}
#[cfg(not(feature = "yubihsm-mock"))]
fn init_connector() -> Connector {
let cfg = config();
let serial_number = cfg
.serial_number
.as_ref()
.map(|serial| serial.parse::<SerialNumber>().unwrap());
#[cfg(feature = "yubihsm-server")]
{
if let Some(ref connector_server) = cfg.connector_server {
if connector_server.cli.is_some() && is_cli_command() {
let adapter = AdapterConfig::Http {
addr: connector_server.laddr.clone(),
};
return init_connector_adapter(&adapter, serial_number);
}
}
}
let connector = init_connector_adapter(&cfg.adapter, serial_number);
#[cfg(feature = "yubihsm-server")]
{
if let Some(ref connector_server) = cfg.connector_server {
run_connnector_server(
http_config_for_address(&connector_server.laddr),
connector.clone(),
)
}
}
connector
}
#[cfg(not(feature = "yubihsm-mock"))]
fn init_connector_adapter(adapter: &AdapterConfig, serial: Option<SerialNumber>) -> Connector {
match *adapter {
AdapterConfig::Http { ref addr } => connect_http(addr),
AdapterConfig::Usb { timeout_ms } => {
let usb_config = UsbConfig { serial, timeout_ms };
Connector::usb(&usb_config)
}
}
}
#[cfg(not(feature = "yubihsm-mock"))]
fn http_config_for_address(addr: &net::Address) -> HttpConfig {
match addr {
net::Address::Tcp {
peer_id: _,
ref host,
port,
} => {
let mut config = HttpConfig::default();
config.addr = host.clone();
config.port = *port;
config
}
net::Address::Unix { .. } => panic!("yubihsm does not support Unix sockets"),
}
}
#[cfg(not(feature = "yubihsm-mock"))]
fn connect_http(addr: &net::Address) -> Connector {
Connector::http(&http_config_for_address(addr))
}
#[cfg(feature = "yubihsm-mock")]
fn init_connector() -> Connector {
Connector::mockhsm()
}
fn init_client() -> Client {
let (credentials, reconnect) = client_config();
Client::open(connector().clone(), credentials, reconnect).unwrap_or_else(|e| {
status_err!("error connecting to YubiHSM2: {}", e);
process::exit(1);
})
}
#[cfg(not(feature = "yubihsm-server"))]
fn client_config() -> (yubihsm::Credentials, bool) {
(config().auth.credentials(), true)
}
#[cfg(feature = "yubihsm-server")]
fn client_config() -> (yubihsm::Credentials, bool) {
let cfg = config();
cfg.connector_server
.as_ref()
.and_then(|connector_server| {
connector_server.cli.as_ref().and_then(|cli| {
cli.auth_key.and_then(|auth_key_id| {
if is_cli_command() {
Some((prompt_for_auth_key_password(auth_key_id), false))
} else {
None
}
})
})
})
.unwrap_or_else(|| (cfg.auth.credentials(), true))
}
#[cfg(feature = "yubihsm-server")]
fn prompt_for_auth_key_password(auth_key_id: u16) -> yubihsm::Credentials {
let prompt = format!(
"Enter password for YubiHSM2 auth key 0x{:04x}: ",
auth_key_id
);
let password = Zeroizing::new(
rpassword::read_password_from_tty(Some(&prompt)).expect("error reading password"),
);
yubihsm::Credentials::from_password(auth_key_id, password.as_bytes())
}
pub fn config() -> YubihsmConfig {
let kms_config = app_config();
let yubihsm_configs = &kms_config.providers.yubihsm;
if yubihsm_configs.len() != 1 {
status_err!(
"expected one [yubihsm.provider] in config, found: {}",
yubihsm_configs.len()
);
process::exit(1);
}
yubihsm_configs[0].clone()
}
#[cfg(all(feature = "yubihsm-server", not(feature = "yubihsm-mock")))]
fn run_connnector_server(config: HttpConfig, connector: Connector) {
thread::spawn(move || loop {
let server = yubihsm::connector::http::Server::new(&config, connector.clone())
.expect("couldn't start yubihsm connector HTTP server");
if let Err(e) = server.run() {
error!("yubihsm HTTP service crashed! {}", e);
}
});
}
impl From<yubihsm::client::Error> for Error {
fn from(other: yubihsm::client::Error) -> Error {
ErrorKind::YubihsmError.context(other).into()
}
}