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
extern crate readpassphrase_sys;

use std::ops::BitOr;

use readpassphrase_sys as ffi;

/// Flags argument able to bitwise OR zero or more flags
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(i32)]
pub enum Flags {
    /// Turn off echo (default behavior)
    EchoOff = ffi::RPP_ECHO_OFF,
    /// Leave echo on
    EchoOn = ffi::RPP_ECHO_ON,
    /// Fail if there is no TTY
    RequireTty = ffi::RPP_REQUIRE_TTY,
    /// Force input to lower case
    ForceLower = ffi::RPP_FORCELOWER,
    /// Force input to upper case
    ForceUpper = ffi::RPP_FORCEUPPER,
    /// Strip the high bit from input
    SevenBit = ffi::RPP_SEVENBIT,
    /// Read passphrase from stdin; ignore prompt
    StdIn = ffi::RPP_STDIN,
}

/// Wrapper type for bitwise OR-ed flags
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct FlagsOr(i32);

impl From<Flags> for FlagsOr {
    fn from(f: Flags) -> Self {
        Self(f as i32)
    }
}

impl BitOr for Flags {
    type Output = FlagsOr;

    fn bitor(self, rhs: Self) -> Self::Output {
        FlagsOr((self as i32) | (rhs as i32))
    }
}

impl BitOr<Flags> for FlagsOr {
    type Output = FlagsOr;

    fn bitor(self, rhs: Flags) -> Self::Output {
        Self(self.0 | rhs as i32)
    }
}

impl BitOr for FlagsOr {
    type Output = FlagsOr;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

impl From<FlagsOr> for i32 {
    fn from(f: FlagsOr) -> Self {
        f.0
    }
}

/// Error type indicating a failed call to readpassphrase
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
    General,
    Utf8(std::str::Utf8Error),
    Nul(std::ffi::NulError),
}

impl From<std::str::Utf8Error> for Error {
    fn from(e: std::str::Utf8Error) -> Self {
        Self::Utf8(e)
    }
}

impl From<std::ffi::NulError> for Error {
    fn from(e: std::ffi::NulError) -> Self {
        Self::Nul(e)
    }
}

/// Displays a prompt to, and reads in a passphrase from, /dev/tty
/// 
/// If this file is inaccessible and the RPP_REQUIRE_TTY flag is not set,
/// readpassphrase displays the prompt on the standard error output and
/// reads from the standard input. In this case it is generally not possible
/// to turn off echo.
///
/// Example:
///
/// ```rust,no_run
/// # use readpassphrase::{readpassphrase, Flags};
/// let _pass = readpassphrase("Password:", 1024, Flags::RequireTty.into()).unwrap();
/// /* or */
/// let _pass = readpassphrase("Password:", 1024, Flags::RequireTty | Flags::ForceLower).unwrap();
/// ```
pub fn readpassphrase(prompt: &str, buf_len: usize, flags: FlagsOr) -> Result<String, Error> {
    let prompt_ptr = std::ffi::CString::new(prompt)?.into_raw();
    let buf = vec![1u8; buf_len];
    let buf_ptr = std::ffi::CString::new(buf)?.into_raw();
    // safety: all the pointers are non-null, and flags are valid
    // On failure a null pointer is returned
    let pass_ptr = unsafe { ffi::readpassphrase(prompt_ptr, buf_ptr, buf_len, flags.into()) };

    if pass_ptr == std::ptr::null_mut() {
        // safety: buf is non-null, and points to valid memory
        unsafe { libc::explicit_bzero(buf_ptr as *mut _, buf_len) };
        Err(Error::General)
    } else {
        // safety: pass_ptr is a pointer to valid memory, even if it not
        // a valid UTF8 C-string
        let pass_cstr = unsafe { std::ffi::CString::from_raw(pass_ptr) };
        let pass_len = pass_cstr.as_bytes().len();

        let pass = match pass_cstr.to_str() {
            Ok(p) => Ok(p.to_string()),
            Err(_) => Err(Error::General),
        };

        // clear the returned passphrase buffer
        // safety: pass and buf pointers are non-null, and point to valid memory
        unsafe { 
            libc::explicit_bzero(pass_ptr as *mut _, pass_len);
            libc::explicit_bzero(buf_ptr as *mut _, buf_len);
        }

        pass
    }
}

/// Convenience function to securely clear a passphrase
///
/// Example:
///
/// ```rust
/// # use readpassphrase::clear_passphrase;
/// let mut passphrase = "super secret password".to_string();
/// clear_passphrase(&mut passphrase);
/// assert_eq!(passphrase.as_bytes(), [0; 21].as_ref());
/// ```
pub fn clear_passphrase(pass: &mut str) {
    let ptr = pass.as_mut_ptr();
    // safety: pass pointer is non-null, and points to valid memory
    unsafe { libc::explicit_bzero(ptr as *mut _, pass.len()) };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_flags() {
        let orflags = Flags::RequireTty | Flags::SevenBit;
        assert_eq!(orflags.0, (Flags::RequireTty as i32) | (Flags::SevenBit as i32));
    }
}