switch to libcryptsetup-rs
This commit is contained in:
112
src/cli.rs
112
src/cli.rs
@@ -1,11 +1,7 @@
|
||||
use crate::error::*;
|
||||
use crate::luks;
|
||||
use crate::*;
|
||||
|
||||
use cryptsetup_rs as luks;
|
||||
use cryptsetup_rs::api::{CryptDeviceHandle, CryptDeviceOpenBuilder, Luks1Params};
|
||||
use cryptsetup_rs::{CryptDevice, Luks1CryptDevice};
|
||||
|
||||
use libcryptsetup_sys::crypt_keyslot_info;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use failure::_core::fmt::{Error, Formatter};
|
||||
@@ -17,65 +13,6 @@ use std::process::exit;
|
||||
use std::thread;
|
||||
|
||||
use std::time::SystemTime;
|
||||
pub fn add_key_to_luks(
|
||||
device: PathBuf,
|
||||
secret: &[u8; 32],
|
||||
old_secret: Box<dyn Fn() -> Fido2LuksResult<Vec<u8>>>,
|
||||
exclusive: bool,
|
||||
) -> Fido2LuksResult<u8> {
|
||||
fn offer_format(
|
||||
_dev: CryptDeviceOpenBuilder,
|
||||
) -> Fido2LuksResult<CryptDeviceHandle<Luks1Params>> {
|
||||
unimplemented!()
|
||||
}
|
||||
let dev =
|
||||
|| -> luks::device::Result<CryptDeviceOpenBuilder> { luks::open(&device.canonicalize()?) };
|
||||
|
||||
let prev_key = old_secret()?;
|
||||
|
||||
let mut handle = match dev()?.luks1() {
|
||||
Ok(handle) => handle,
|
||||
Err(luks::device::Error::BlkidError(_)) => offer_format(dev()?)?,
|
||||
Err(luks::device::Error::CryptsetupError(errno)) => {
|
||||
//if i32::from(errno) == 555
|
||||
dbg!(errno);
|
||||
offer_format(dev()?)?
|
||||
} //TODO: find correct errorno and offer to format as luks
|
||||
err => err?,
|
||||
};
|
||||
handle.set_iteration_time(50);
|
||||
let slot = handle.add_keyslot(secret, Some(prev_key.as_slice()), None)?;
|
||||
if exclusive {
|
||||
for old_slot in 0..8u8 {
|
||||
if old_slot != slot
|
||||
&& (handle.keyslot_status(old_slot.into()) == crypt_keyslot_info::CRYPT_SLOT_ACTIVE
|
||||
|| handle.keyslot_status(old_slot.into())
|
||||
== crypt_keyslot_info::CRYPT_SLOT_ACTIVE_LAST)
|
||||
{
|
||||
handle.destroy_keyslot(old_slot)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
pub fn add_password_to_luks(
|
||||
device: PathBuf,
|
||||
secret: &[u8; 32],
|
||||
new_secret: Box<dyn Fn() -> Fido2LuksResult<Vec<u8>>>,
|
||||
add_password: bool,
|
||||
) -> Fido2LuksResult<u8> {
|
||||
let dev = luks::open(&device.canonicalize()?)?;
|
||||
let mut handle = dev.luks1()?;
|
||||
let prev_slot = if add_password {
|
||||
Some(handle.add_keyslot(&secret[..], Some(&secret[..]), None)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let slot = handle.update_keyslot(&new_secret()?[..], &secret[..], prev_slot)?;
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct HexEncoded(Vec<u8>);
|
||||
|
||||
@@ -265,22 +202,19 @@ pub fn run_cli() -> Fido2LuksResult<()> {
|
||||
ref secret_gen,
|
||||
} => {
|
||||
let secret = secret_gen.patch(&args).obtain_secret()?;
|
||||
let slot = add_key_to_luks(
|
||||
device.clone(),
|
||||
&secret,
|
||||
if let Some(keyfile) = keyfile.clone() {
|
||||
Box::new(move || util::read_keyfile(keyfile.clone()))
|
||||
} else {
|
||||
Box::new(|| {
|
||||
util::read_password("Old password", true).map(|p| p.as_bytes().to_vec())
|
||||
})
|
||||
},
|
||||
*exclusive,
|
||||
)?;
|
||||
let old_secret = if let Some(keyfile) = keyfile.clone() {
|
||||
util::read_keyfile(keyfile.clone())
|
||||
} else {
|
||||
util::read_password("Old password", false).map(|p| p.as_bytes().to_vec())
|
||||
}?;
|
||||
let added_slot = luks::add_key(device.clone(), &secret, &old_secret[..], Some(10))?;
|
||||
if *exclusive {
|
||||
luks::remove_keyslots(&device, &[added_slot])?;
|
||||
}
|
||||
println!(
|
||||
"Added to key to device {}, slot: {}",
|
||||
device.display(),
|
||||
slot
|
||||
added_slot
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -291,18 +225,16 @@ pub fn run_cli() -> Fido2LuksResult<()> {
|
||||
ref secret_gen,
|
||||
} => {
|
||||
let secret = secret_gen.patch(&args).obtain_secret()?;
|
||||
let slot = add_password_to_luks(
|
||||
device.clone(),
|
||||
&secret,
|
||||
if let Some(keyfile) = keyfile.clone() {
|
||||
Box::new(move || util::read_keyfile(keyfile.clone()))
|
||||
} else {
|
||||
Box::new(|| {
|
||||
util::read_password("Password to add", true).map(|p| p.as_bytes().to_vec())
|
||||
})
|
||||
},
|
||||
*add_password,
|
||||
)?;
|
||||
let new_secret = if let Some(keyfile) = keyfile.clone() {
|
||||
util::read_keyfile(keyfile.clone())
|
||||
} else {
|
||||
util::read_password("Password to add", *add_password).map(|p| p.as_bytes().to_vec())
|
||||
}?;
|
||||
let slot = if *add_password {
|
||||
luks::add_key(device, &new_secret[..], &secret, None)
|
||||
} else {
|
||||
luks::replace_key(device, &new_secret[..], &secret, Some(5))
|
||||
}?;
|
||||
println!(
|
||||
"Added to password to device {}, slot: {}",
|
||||
device.display(),
|
||||
@@ -319,7 +251,7 @@ pub fn run_cli() -> Fido2LuksResult<()> {
|
||||
let mut retries = *retries;
|
||||
loop {
|
||||
let secret = secret_gen.patch(&args).obtain_secret()?;
|
||||
match open_container(&device, &name, &secret) {
|
||||
match luks::open_container(&device, &name, &secret) {
|
||||
Err(e) => match e {
|
||||
Fido2LuksError::WrongSecret if retries > 0 => {
|
||||
retries -= 1;
|
||||
|
17
src/error.rs
17
src/error.rs
@@ -14,7 +14,9 @@ pub enum Fido2LuksError {
|
||||
#[fail(display = "no authenticator found, please ensure your device is plugged in")]
|
||||
NoAuthenticatorError,
|
||||
#[fail(display = "luks err")]
|
||||
LuksError { cause: cryptsetup_rs::device::Error },
|
||||
LuksError {
|
||||
cause: libcryptsetup_rs::LibcryptErr,
|
||||
},
|
||||
#[fail(display = "no authenticator found, please ensure your device is plugged in")]
|
||||
IoError { cause: io::Error },
|
||||
#[fail(display = "supplied secret isn't valid for this device")]
|
||||
@@ -44,6 +46,7 @@ pub enum AskPassError {
|
||||
Mismatch,
|
||||
}
|
||||
|
||||
use libcryptsetup_rs::LibcryptErr;
|
||||
use std::string::FromUtf8Error;
|
||||
use Fido2LuksError::*;
|
||||
|
||||
@@ -53,17 +56,11 @@ impl From<FidoError> for Fido2LuksError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<cryptsetup_rs::device::Error> for Fido2LuksError {
|
||||
fn from(e: cryptsetup_rs::device::Error) -> Self {
|
||||
match e {
|
||||
cryptsetup_rs::device::Error::CryptsetupError(error_no) if error_no.0 == 1i32 => {
|
||||
WrongSecret
|
||||
}
|
||||
e => LuksError { cause: e },
|
||||
}
|
||||
impl From<LibcryptErr> for Fido2LuksError {
|
||||
fn from(e: LibcryptErr) -> Self {
|
||||
LuksError { cause: e }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Fido2LuksError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
IoError { cause: e }
|
||||
|
86
src/luks.rs
Normal file
86
src/luks.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use crate::error::*;
|
||||
|
||||
use libcryptsetup_rs::{
|
||||
CryptActivateFlags, CryptDevice, CryptInit, CryptKeyslot, CryptVolumeKeyFlags,
|
||||
EncryptionFormat, KeyslotInfo, LibcryptErr,
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
fn load_device_handle<P: AsRef<Path>>(path: P) -> Fido2LuksResult<CryptDevice> {
|
||||
let mut device = CryptInit::init(path.as_ref())?;
|
||||
//TODO: determine luks version some way other way than just trying
|
||||
let mut load = |format| device.context_handle().load::<()>(format, None).map(|_| ());
|
||||
vec![EncryptionFormat::Luks2, EncryptionFormat::Luks1]
|
||||
.into_iter()
|
||||
.fold(None, |res, format| match res {
|
||||
Some(Ok(())) => res,
|
||||
Some(e) => Some(e.and(load(format))),
|
||||
None => Some(load(format)),
|
||||
})
|
||||
.unwrap()?;
|
||||
Ok(device)
|
||||
}
|
||||
|
||||
pub fn open_container<P: AsRef<Path>>(path: P, name: &str, secret: &[u8]) -> Fido2LuksResult<()> {
|
||||
let mut device = load_device_handle(path)?;
|
||||
device
|
||||
.activate_handle()
|
||||
.activate_by_passphrase(Some(name), None, secret, CryptActivateFlags::empty())
|
||||
.map(|_slot| ())
|
||||
.map_err(|_e| Fido2LuksError::WrongSecret)
|
||||
}
|
||||
|
||||
pub fn add_key<P: AsRef<Path>>(
|
||||
path: P,
|
||||
secret: &[u8],
|
||||
old_secret: &[u8],
|
||||
iteration_time: Option<u64>,
|
||||
) -> Fido2LuksResult<u32> {
|
||||
let mut device = load_device_handle(path)?;
|
||||
// Set iteration time not sure wether this applies to luks2 as well
|
||||
if let Some(millis) = iteration_time {
|
||||
device.settings_handle().set_iteration_time(millis)
|
||||
}
|
||||
let slot = device
|
||||
.keyslot_handle(None)
|
||||
.add_by_passphrase(old_secret, secret)?;
|
||||
Ok(slot)
|
||||
}
|
||||
|
||||
pub fn remove_keyslots<P: AsRef<Path>>(path: P, exclude: &[u32]) -> Fido2LuksResult<()> {
|
||||
let mut device = load_device_handle(path)?;
|
||||
let mut slot = 0;
|
||||
let mut handle;
|
||||
loop {
|
||||
handle = device.keyslot_handle(Some(slot));
|
||||
match handle.status()? {
|
||||
KeyslotInfo::Inactive => continue,
|
||||
KeyslotInfo::Active if !exclude.contains(&slot) => {
|
||||
dbg!((slot, handle.destroy()?));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
match handle.status()? {
|
||||
KeyslotInfo::ActiveLast => break,
|
||||
_ => (),
|
||||
}
|
||||
slot += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn replace_key<P: AsRef<Path>>(
|
||||
path: P,
|
||||
secret: &[u8],
|
||||
old_secret: &[u8],
|
||||
iteration_time: Option<u64>,
|
||||
) -> Fido2LuksResult<u32> {
|
||||
let mut device = load_device_handle(path)?;
|
||||
// Set iteration time not sure wether this applies to luks2 as well
|
||||
if let Some(millis) = iteration_time {
|
||||
device.settings_handle().set_iteration_time(millis)
|
||||
}
|
||||
Ok(device
|
||||
.keyslot_handle(None)
|
||||
.change_by_passphrase(None, None, old_secret, secret)? as u32)
|
||||
}
|
10
src/main.rs
10
src/main.rs
@@ -5,9 +5,6 @@ use crate::cli::*;
|
||||
use crate::config::*;
|
||||
use crate::device::*;
|
||||
use crate::error::*;
|
||||
use cryptsetup_rs as luks;
|
||||
use cryptsetup_rs::Luks1CryptDevice;
|
||||
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
@@ -16,14 +13,9 @@ mod cli;
|
||||
mod config;
|
||||
mod device;
|
||||
mod error;
|
||||
mod luks;
|
||||
mod util;
|
||||
|
||||
fn open_container(device: &PathBuf, name: &str, secret: &[u8; 32]) -> Fido2LuksResult<()> {
|
||||
let mut handle = luks::open(device.canonicalize()?)?.luks1()?;
|
||||
let _slot = handle.activate(name, &secret[..])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn assemble_secret(hmac_result: &[u8], salt: &[u8]) -> [u8; 32] {
|
||||
util::sha256(&[salt, hmac_result])
|
||||
}
|
||||
|
Reference in New Issue
Block a user