Skip to content

Commit 948d0fc

Browse files
committed
Isolate hsm auth value from the cache
Signed-off-by: David Mulder <[email protected]> Reviewed-by: Alexander Bokovoy <[email protected]>
1 parent abcf764 commit 948d0fc

File tree

11 files changed

+92
-30
lines changed

11 files changed

+92
-30
lines changed

docs-xml/generate-pathconf-entities.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ echo "
1919
<!ENTITY pathconfig.SAMBA_DATADIR '\${prefix}/var/samba'>
2020
<!ENTITY pathconfig.CTDB_DATADIR '\${prefix}/share/ctdb'>
2121
<!ENTITY pathconfig.CONFIGFILE '\${prefix}/etc/smb.conf'>
22+
<!ENTITY pathconfig.HIMMELBLAUD_HSM_PIN_PATH '\${prefix}/var/lib/himmelblaud/hsm-pin'>
2223
"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<samba:parameter name="himmelblaud hsm pin path"
2+
context="G"
3+
type="string"
4+
xmlns:samba="http://www.samba.org/samba/DTD/samba-doc">
5+
<description>
6+
<para>Specifies the file path where the HSM PIN is stored. This PIN is used
7+
for unlocking TPM objects required for Azure Entra ID authentication. The HSM
8+
PIN is critical for ensuring secure communication and authentication within
9+
the Himmelblaud daemon.</para>
10+
</description>
11+
12+
<value type="default">&pathconfig.HIMMELBLAUD_HSM_PIN_PATH;</value>
13+
</samba:parameter>

dynconfig/dynconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@ DEFINE_DYN_CONFIG_PARAM(NTP_SIGND_SOCKET_DIR)
107107
DEFINE_DYN_CONFIG_PARAM(PYTHONDIR)
108108
DEFINE_DYN_CONFIG_PARAM(PYTHONARCHDIR)
109109
DEFINE_DYN_CONFIG_PARAM(SCRIPTSBINDIR)
110+
DEFINE_DYN_CONFIG_PARAM(HIMMELBLAUD_HSM_PIN_PATH)

dynconfig/dynconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ DEFINE_DYN_CONFIG_PROTO(NTP_SIGND_SOCKET_DIR)
5858
DEFINE_DYN_CONFIG_PROTO(PYTHONDIR)
5959
DEFINE_DYN_CONFIG_PROTO(PYTHONARCHDIR)
6060
DEFINE_DYN_CONFIG_PROTO(SCRIPTSBINDIR)
61+
DEFINE_DYN_CONFIG_PROTO(HIMMELBLAUD_HSM_PIN_PATH)

dynconfig/wscript

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ dynconfig = {
285285
'HELPTEXT': 'Where to put the smbpasswd file',
286286
'DELAY': True,
287287
},
288+
'HIMMELBLAUD_HSM_PIN_PATH': {
289+
'STD-PATH': '${LOCALSTATEDIR}/lib/himmelblaud/hsm-pin',
290+
'FHS-PATH': '${LOCALSTATEDIR}/lib/himmelblaud/hsm-pin',
291+
'OPTION': '--with-himmelblaud-hsm-pin-path',
292+
'HELPTEXT': 'Where to store the hsm pin',
293+
'DELAY': True,
294+
},
288295
}
289296

290297
def options(opt):

lib/param/loadparm.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,6 +3166,9 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
31663166
"AD DC only");
31673167

31683168
/* Set the default Himmelblaud globals */
3169+
lpcfg_do_global_parameter(lp_ctx,
3170+
"himmelblaud hsm pin path",
3171+
get_dyn_HIMMELBLAUD_HSM_PIN_PATH());
31693172
lpcfg_do_global_parameter(lp_ctx,
31703173
"himmelblaud hello enabled",
31713174
"false");

rust/himmelblaud/src/cache.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -404,32 +404,6 @@ impl PrivateCache {
404404
})
405405
}
406406

407-
pub(crate) fn hsm_pin_fetch_or_create(
408-
&mut self,
409-
) -> Result<AuthValue, Box<NTSTATUS>> {
410-
let hsm_pin = match self.cache.fetch_str("auth_value") {
411-
Some(hsm_pin) => hsm_pin,
412-
None => {
413-
let auth_str = match AuthValue::generate() {
414-
Ok(auth_str) => auth_str,
415-
Err(e) => {
416-
DBG_ERR!("Failed to create hsm pin: {:?}", e);
417-
return Err(Box::new(NT_STATUS_UNSUCCESSFUL));
418-
}
419-
};
420-
self.cache.store_bytes("auth_value", auth_str.as_bytes())?;
421-
auth_str
422-
}
423-
};
424-
match AuthValue::try_from(hsm_pin.as_bytes()) {
425-
Ok(auth_value) => Ok(auth_value),
426-
Err(e) => {
427-
DBG_ERR!("Invalid hsm pin: {:?}", e);
428-
return Err(Box::new(NT_STATUS_UNSUCCESSFUL));
429-
}
430-
}
431-
}
432-
433407
pub(crate) fn loadable_machine_key_fetch_or_create(
434408
&mut self,
435409
hsm: &mut BoxedDynTpm,

rust/himmelblaud/src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,21 @@ async fn main() -> ExitCode {
200200
};
201201

202202
// Check for and create the hsm pin if required.
203-
let auth_value = match pcache.hsm_pin_fetch_or_create() {
204-
Ok(auth_value) => auth_value,
205-
Err(e) => {
206-
DBG_ERR!("{:?}", e);
203+
let hsm_pin_path = match lp.himmelblaud_hsm_pin_path() {
204+
Ok(Some(hsm_pin_path)) => hsm_pin_path,
205+
_ => {
206+
DBG_ERR!("Failed loading hsm pin path.");
207207
return ExitCode::FAILURE;
208208
}
209209
};
210+
let auth_value =
211+
match utils::hsm_pin_fetch_or_create(&hsm_pin_path).await {
212+
Ok(auth_value) => auth_value,
213+
Err(e) => {
214+
DBG_ERR!("{:?}", e);
215+
return ExitCode::FAILURE;
216+
}
217+
};
210218

211219
// Setup the HSM and its machine key
212220
let mut hsm: BoxedDynTpm = BoxedDynTpm::new(SoftTpm::new());

rust/himmelblaud/src/utils.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
You should have received a copy of the GNU General Public License
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
21+
use dbg::{DBG_ERR, DBG_INFO};
22+
use kanidm_hsm_crypto::AuthValue;
2123
use ntstatus_gen::*;
24+
use std::path::PathBuf;
25+
use std::str::FromStr;
26+
use tokio::fs::File;
27+
use tokio::io::AsyncReadExt;
2228

2329
pub fn split_username(
2430
username: &str,
@@ -29,3 +35,47 @@ pub fn split_username(
2935
}
3036
Err(Box::new(NT_STATUS_INVALID_USER_PRINCIPAL_NAME))
3137
}
38+
39+
pub(crate) async fn hsm_pin_fetch_or_create(
40+
hsm_pin_path: &str,
41+
) -> Result<AuthValue, Box<NTSTATUS>> {
42+
let auth_value = if !PathBuf::from_str(hsm_pin_path)
43+
.map_err(|e| {
44+
DBG_ERR!("Failed to create hsm pin: {:?}", e);
45+
Box::new(NT_STATUS_UNSUCCESSFUL)
46+
})?
47+
.exists()
48+
{
49+
let auth_value = AuthValue::generate().map_err(|e| {
50+
DBG_ERR!("Failed to create hsm pin: {:?}", e);
51+
Box::new(NT_STATUS_UNSUCCESSFUL)
52+
})?;
53+
std::fs::write(hsm_pin_path, auth_value.clone()).map_err(|e| {
54+
DBG_ERR!("Failed to write hsm pin: {:?}", e);
55+
Box::new(NT_STATUS_UNSUCCESSFUL)
56+
})?;
57+
58+
DBG_INFO!("Generated new HSM pin");
59+
auth_value
60+
} else {
61+
let mut file = File::open(hsm_pin_path).await.map_err(|e| {
62+
DBG_ERR!("Failed to read hsm pin: {:?}", e);
63+
Box::new(NT_STATUS_UNSUCCESSFUL)
64+
})?;
65+
let mut auth_value = vec![];
66+
file.read_to_end(&mut auth_value).await.map_err(|e| {
67+
DBG_ERR!("Failed to read hsm pin: {:?}", e);
68+
Box::new(NT_STATUS_UNSUCCESSFUL)
69+
})?;
70+
std::str::from_utf8(&auth_value)
71+
.map_err(|e| {
72+
DBG_ERR!("Failed to read hsm pin: {:?}", e);
73+
Box::new(NT_STATUS_UNSUCCESSFUL)
74+
})?
75+
.to_string()
76+
};
77+
AuthValue::try_from(auth_value.as_bytes()).map_err(|e| {
78+
DBG_ERR!("Invalid hsm pin: {:?}", e);
79+
Box::new(NT_STATUS_UNSUCCESSFUL)
80+
})
81+
}

rust/param/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl LoadParm {
202202
lpcfg_str!(cache_directory);
203203
lpcfg_str!(template_homedir);
204204
lpcfg_str!(template_shell);
205+
lpcfg_str!(himmelblaud_hsm_pin_path);
205206
}
206207

207208
unsafe impl Send for LoadParm {}

0 commit comments

Comments
 (0)