Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow piped PIN input #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_derive = "1.0.116"
serde = "1.0.116"
anyhow = "1.0.56"
ctap-hid-fido2 = "3.4.1"
atty = "0.2"

[build-dependencies]
hex = "0.3.2"
Expand All @@ -34,6 +35,7 @@ rpassword = "4.0.1"
libcryptsetup-rs = "0.9.1"
structopt = "0.3.2"
anyhow = "1.0.56"
atty = "0.2"

[profile.release]
lto = true
Expand Down
22 changes: 14 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ring::digest;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::io::{self, BufRead};

pub fn sha256(messages: &[&[u8]]) -> [u8; 32] {
let mut digest = digest::Context::new(&digest::SHA256);
Expand All @@ -17,18 +18,23 @@ pub fn read_password_tty(q: &str, verify: bool) -> Fido2LuksResult<String> {
read_password(q, verify, true)
}
pub fn read_password(q: &str, verify: bool, tty: bool) -> Fido2LuksResult<String> {
let res = if tty {
// Check if the standard input is a TTY (interactive) or piped
let is_tty = atty::is(atty::Stream::Stdin);

// Read password depending on TTY status
let res = if tty && is_tty {
rpassword::read_password_from_tty(Some(&[q, ": "].join("")))
} else {
print!("{}: ", q);
rpassword::read_password()
// Use stdin directly if it's not a TTY (e.g., piped input)
println!("{}: ", q);
let mut input = String::new();
io::stdin().lock().read_line(&mut input)?;
Ok(input.trim().to_string())
}?;

// Handle verification if needed
match res {
ref pass
if verify
&& &rpassword::read_password_from_tty(Some(&[q, "(again): "].join(" ")))?
!= pass =>
{
ref pass if verify && is_tty && &rpassword::read_password_from_tty(Some(&[q, "(again): "].join(" ")))? != pass => {
Err(Fido2LuksError::AskPassError {
cause: AskPassError::Mismatch,
})
Expand Down