readpassphrase_3/lib.rs
1// Copyright 2025
2// Steven Dee
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7//
8// Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//
11// THIS SOFTWARE IS PROVIDED BY STEVEN DEE “AS IS” AND ANY EXPRESS
12// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14// ARE DISCLAIMED. IN NO EVENT SHALL STEVEN DEE BE LIABLE FOR ANY
15// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
17// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
19// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
20// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
21// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23//! This library endeavors to expose a thin wrapper around the C [`readpassphrase(3)`][0] function.
24//!
25//! Three different interfaces are exposed; for most purposes, you will want to use either
26//! [`getpass`] (for simple password entry) or [`readpassphrase`] (when you need flags from
27//! `readpassphrase(3)` or need more control over the memory.)
28//!
29//! The [`readpassphrase_owned`] function is a bit more niche; it may be used when you need a
30//! [`String`] output but need to pass flags or control the buffer size (vs [`getpass`].)
31//!
32//! Sensitive data should be zeroed as soon as possible to avoid leaving it visible in the
33//! process’s address space.
34//!
35//! # Usage
36//! To read a passphrase from the console:
37//! ```no_run
38//! # use readpassphrase_3::{getpass, zeroize::Zeroize};
39//! let mut pass = getpass(c"password: ").unwrap();
40//! // do_something_with(&pass);
41//! pass.zeroize();
42//! ```
43//!
44//! To control the buffer size or (on non-Windows) flags:
45//! ```no_run
46//! # use readpassphrase_3::{RppFlags, readpassphrase};
47//! # let mut buf = vec![0u8; 1];
48//! let pass = readpassphrase(c"pass: ", &mut buf, RppFlags::ECHO_ON).unwrap();
49//! ```
50//!
51//! To do so while transferring ownership:
52//! ```no_run
53//! # use readpassphrase_3::{Error, RppFlags, readpassphrase_owned};
54//! # fn main() -> Result<(), Error> {
55//! # let buf = vec![0u8; 1];
56//! let pass = readpassphrase_owned(c"pass: ", buf, RppFlags::empty())?;
57//! # Ok(())
58//! # }
59//! ```
60//!
61//! This crate works well with the [`zeroize`][1] crate; for example, [`zeroize::Zeroizing`][2] may
62//! be used to zero buffer contents regardless of a function’s control flow:
63//!
64//! ```no_run
65//! # use readpassphrase_3::{Error, PASSWORD_LEN, RppFlags, readpassphrase};
66//! use zeroize::Zeroizing;
67//! # fn main() -> Result<(), Error> {
68//! let mut buf = Zeroizing::new(vec![0u8; PASSWORD_LEN]);
69//! let pass = readpassphrase(c"pass: ", &mut buf, RppFlags::REQUIRE_TTY)?;
70//! // do_something_that_can_fail_with(pass)?;
71//! # Ok(())
72//! # }
73//! ```
74//!
75//! # “Mismatched types” errors
76//! The prompt strings in this API are references to [CStr], not [str]. This is because the
77//! underlying C function assumes that the prompt is a null-terminated string; were we to take
78//! `&str` instead of `&CStr`, we would need to make a copy of the prompt on every call.
79//!
80//! Most of the time, your prompts will be string literals; you can ask Rust to give you a `&CStr`
81//! literal by simply prepending `c` to the string:
82//! ```no_run
83//! # use readpassphrase_3::{Error, getpass};
84//! # fn main() -> Result<(), Error> {
85//! let _ = getpass(c"pass: ")?;
86//! // ^
87//! // |
88//! // like this
89//! # Ok(())
90//! # }
91//! ```
92//!
93//! # Windows Limitations
94//! The Windows implementation of `readpassphrase(3)` that we are using does not yet support UTF-8
95//! in prompts; they must be ASCII. It also does not yet support flags, and always behaves as
96//! though called with [`RppFlags::empty()`].
97//!
98//! [0]: https://man.openbsd.org/readpassphrase
99//! [1]: https://docs.rs/zeroize/latest/zeroize/
100//! [2]: https://docs.rs/zeroize/latest/zeroize/struct.Zeroizing.html
101
102use std::{ffi::CStr, fmt::Display, io, mem, str::Utf8Error};
103
104use bitflags::bitflags;
105use zeroize::Zeroize;
106
107#[cfg(all(not(docsrs), feature = "zeroize"))]
108pub use zeroize;
109
110/// Size of buffer used in [`getpass`].
111///
112/// Because `readpassphrase(3)` null-terminates its string, the actual maximum password length for
113/// [`getpass`] is 255.
114pub const PASSWORD_LEN: usize = 256;
115
116bitflags! {
117 /// Flags for controlling readpassphrase.
118 ///
119 /// The default flag `ECHO_OFF` is not represented here because `bitflags` [recommends against
120 /// zero-bit flags][0]; it may be specified as either [`RppFlags::empty()`] or
121 /// [`RppFlags::default()`].
122 ///
123 /// Note that the Windows `readpassphrase(3)` implementation always acts like it has been
124 /// passed `ECHO_OFF`, i.e., the flags are ignored.
125 ///
126 /// [0]: https://docs.rs/bitflags/latest/bitflags/#zero-bit-flags
127 #[derive(Default)]
128 pub struct RppFlags: i32 {
129 /// Leave echo on.
130 const ECHO_ON = 0x01;
131 /// Fail if there is no tty.
132 const REQUIRE_TTY = 0x02;
133 /// Force input to lower case.
134 const FORCELOWER = 0x04;
135 /// Force input to upper case.
136 const FORCEUPPER = 0x08;
137 /// Strip the high bit from input.
138 const SEVENBIT = 0x10;
139 /// Read from stdin, not `/dev/tty`.
140 const STDIN = 0x20;
141 }
142}
143
144/// Errors that can occur in readpassphrase.
145#[derive(Debug)]
146pub enum Error {
147 /// `readpassphrase(3)` itself encountered an error.
148 Io(io::Error),
149 /// The entered password was not UTF-8.
150 Utf8(Utf8Error),
151}
152
153/// Reads a passphrase using `readpassphrase(3)`.
154///
155/// This function tries to faithfully wrap `readpassphrase(3)` without overhead; the only
156/// additional work it does is:
157/// 1. It converts from a Rust byte slice to a C pointer/length pair going in.
158/// 2. It converts from a C `char *` to a Rust UTF-8 `&str` coming out.
159/// 3. It translates errors from `errno` (or string conversion) into [`Result`].
160///
161/// This function reads a passphrase of up to `buf.len() - 1` bytes. If the entered passphrase is
162/// longer, it will be truncated.
163///
164/// # Security
165/// The passed buffer might contain sensitive data even if this function returns an error (for
166/// example, if the contents are not valid UTF-8.) Therefore it should be zeroed as soon as
167/// possible. This can be achieved, for example, with [`zeroize::Zeroizing`][0]:
168/// ```no_run
169/// # use readpassphrase_3::{PASSWORD_LEN, Error, RppFlags, readpassphrase};
170/// use zeroize::Zeroizing;
171/// # fn main() -> Result<(), Error> {
172/// let mut buf = Zeroizing::new(vec![0u8; PASSWORD_LEN]);
173/// let pass = readpassphrase(c"Pass: ", &mut buf, RppFlags::default())?;
174/// # Ok(())
175/// # }
176/// ```
177///
178/// [0]: https://docs.rs/zeroize/latest/zeroize/struct.Zeroizing.html
179pub fn readpassphrase<'a>(
180 prompt: &CStr,
181 buf: &'a mut [u8],
182 flags: RppFlags,
183) -> Result<&'a str, Error> {
184 unsafe {
185 let res = ffi::readpassphrase(
186 prompt.as_ptr(),
187 buf.as_mut_ptr().cast(),
188 buf.len(),
189 flags.bits(),
190 );
191 if res.is_null() {
192 return Err(io::Error::last_os_error().into());
193 }
194 }
195 Ok(CStr::from_bytes_until_nul(buf).unwrap().to_str()?)
196}
197
198/// Reads a passphrase using `readpassphrase(3)`, returning it as a [`String`].
199///
200/// Internally, this function uses a buffer of [`PASSWORD_LEN`] bytes, allowing for passwords up to
201/// `PASSWORD_LEN - 1` characters (accounting for the C null terminator.) If the entered passphrase
202/// is longer, it will be truncated.
203///
204/// The passed flags are always [`RppFlags::default()`], i.e. `ECHO_OFF`.
205///
206/// # Security
207/// The returned `String` is owned by the caller, and therefore it is the caller’s responsibility
208/// to clear it when you are done with it:
209/// ```no_run
210/// # use readpassphrase_3::{Error, getpass, zeroize::Zeroize};
211/// # fn main() -> Result<(), Error> {
212/// let mut pass = getpass(c"Pass: ")?;
213/// _ = pass;
214/// pass.zeroize();
215/// # Ok(())
216/// # }
217/// ```
218pub fn getpass(prompt: &CStr) -> Result<String, Error> {
219 Ok(readpassphrase_owned(
220 prompt,
221 vec![0u8; PASSWORD_LEN],
222 RppFlags::empty(),
223 )?)
224}
225
226/// An error from [`readpassphrase_owned`].
227///
228/// This wraps [`Error`] but also contains the passed buffer, accessible via [`OwnedError::take`].
229#[derive(Debug)]
230pub struct OwnedError(Error, Option<Vec<u8>>);
231
232/// Reads a passphrase using `readpassphrase(3)` by reusing the passed buffer’s memory.
233///
234/// This function reads a passphrase of up to `buf.capacity() - 1` bytes. If the entered passphrase
235/// is longer, it will be truncated.
236///
237/// The returned [`String`] uses `buf`’s memory; on failure, this memory is returned to the caller
238/// in the second argument of the `Err` tuple with its length set to 0.
239///
240/// # Security
241/// The returned `String` is owned by the caller, and it is the caller’s responsibility to clear
242/// it. This can be done via [`zeroize`], e.g.:
243/// ```no_run
244/// # use readpassphrase_3::{
245/// # PASSWORD_LEN,
246/// # Error,
247/// # RppFlags,
248/// # readpassphrase_owned,
249/// # zeroize::Zeroize,
250/// # };
251/// # fn main() -> Result<(), Error> {
252/// let buf = vec![0u8; PASSWORD_LEN];
253/// let mut pass = readpassphrase_owned(c"Pass: ", buf, RppFlags::default())?;
254/// _ = pass;
255/// pass.zeroize();
256/// # Ok(())
257/// # }
258/// ```
259pub fn readpassphrase_owned(
260 prompt: &CStr,
261 mut buf: Vec<u8>,
262 flags: RppFlags,
263) -> Result<String, OwnedError> {
264 readpassphrase_mut(prompt, &mut buf, flags).map_err(|e| {
265 buf.clear();
266 OwnedError(e, Some(buf))
267 })
268}
269
270// Reads a passphrase into `buf`’s maybe-uninitialized capacity and returns it as a `String`
271// reusing `buf`’s memory on success. This function serves to make it possible to write
272// `readpassphrase_owned` without either pre-initializing the buffer or invoking undefined
273// behavior by constructing a maybe-uninitialized slice.
274fn readpassphrase_mut(prompt: &CStr, buf: &mut Vec<u8>, flags: RppFlags) -> Result<String, Error> {
275 unsafe {
276 let res = ffi::readpassphrase(
277 prompt.as_ptr(),
278 buf.as_mut_ptr().cast(),
279 buf.capacity(),
280 flags.bits(),
281 );
282 if res.is_null() {
283 return Err(io::Error::last_os_error().into());
284 }
285 let res = CStr::from_ptr(res).to_str()?;
286 buf.set_len(res.len());
287 Ok(String::from_utf8_unchecked(mem::take(buf)))
288 }
289}
290
291/// Securely zero the memory in `buf`.
292///
293/// This function zeroes the full capacity of `buf`, erasing any sensitive data in it. It is
294/// a simple shim for [`zeroize`] and the latter should be used instead.
295///
296/// # Usage
297/// The following are equivalent:
298/// ```no_run
299/// # use readpassphrase_3::{explicit_bzero, zeroize::Zeroize};
300/// let mut buf = vec![1u8; 1];
301/// // 1.
302/// explicit_bzero(&mut buf);
303/// // 2.
304/// buf.zeroize();
305/// ```
306#[deprecated(since = "0.6.0", note = "use zeroize::Zeroize instead")]
307pub fn explicit_bzero(buf: &mut Vec<u8>) {
308 buf.zeroize();
309}
310
311impl OwnedError {
312 /// Take `buf` out of the error.
313 ///
314 /// Returns empty [`Vec`] after the first call.
315 pub fn take(&mut self) -> Vec<u8> {
316 self.1.take().unwrap_or_default()
317 }
318}
319
320impl Drop for OwnedError {
321 fn drop(&mut self) {
322 self.1.take().as_mut().map(zeroize::Zeroize::zeroize);
323 }
324}
325
326impl From<OwnedError> for Error {
327 fn from(mut value: OwnedError) -> Self {
328 mem::replace(&mut value.0, Error::Io(io::ErrorKind::Other.into()))
329 }
330}
331
332impl From<io::Error> for Error {
333 fn from(value: io::Error) -> Self {
334 Error::Io(value)
335 }
336}
337
338impl From<Utf8Error> for Error {
339 fn from(value: Utf8Error) -> Self {
340 Error::Utf8(value)
341 }
342}
343
344impl core::error::Error for OwnedError {
345 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
346 Some(&self.0)
347 }
348}
349
350impl Display for OwnedError {
351 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
352 self.0.fmt(f)
353 }
354}
355
356impl core::error::Error for Error {
357 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
358 match self {
359 Error::Io(e) => Some(e),
360 Error::Utf8(e) => Some(e),
361 }
362 }
363}
364
365impl Display for Error {
366 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
367 match self {
368 Error::Io(e) => e.fmt(f),
369 Error::Utf8(e) => e.fmt(f),
370 }
371 }
372}
373
374/// A minimal in-crate implementation of [`zeroize::Zeroize`][0].
375///
376/// This provides compile-fenced memory zeroing for [`String`]s and [`Vec`]s without needing to
377/// depend on the `zeroize` crate.
378///
379/// If the optional `zeroize` feature is enabled, then this module is replaced with `zeroize`
380/// itself.
381///
382/// [0]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
383#[cfg(any(docsrs, not(feature = "zeroize")))]
384pub mod zeroize {
385 use std::{arch::asm, mem::MaybeUninit};
386
387 /// Trait for securely erasing values from memory.
388 pub trait Zeroize {
389 fn zeroize(&mut self);
390 }
391
392 impl Zeroize for Vec<u8> {
393 fn zeroize(&mut self) {
394 self.clear();
395 let buf = self.spare_capacity_mut();
396 buf.fill(MaybeUninit::zeroed());
397 compile_fence(buf);
398 }
399 }
400
401 impl Zeroize for String {
402 fn zeroize(&mut self) {
403 unsafe { self.as_mut_vec() }.zeroize();
404 }
405 }
406
407 impl Zeroize for [u8] {
408 fn zeroize(&mut self) {
409 self.fill(0);
410 compile_fence(self);
411 }
412 }
413
414 fn compile_fence<T>(buf: &[T]) {
415 unsafe {
416 asm!(
417 "/* {ptr} */",
418 ptr = in(reg) buf.as_ptr(),
419 options(nostack, preserves_flags, readonly)
420 );
421 }
422 }
423}
424
425mod ffi {
426 use std::ffi::{c_char, c_int};
427
428 unsafe extern "C" {
429 pub(crate) unsafe fn readpassphrase(
430 prompt: *const c_char,
431 buf: *mut c_char,
432 bufsiz: usize,
433 flags: c_int,
434 ) -> *mut c_char;
435 }
436}