summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-28 20:31:56 +0100
committerRobin Krahl <robin.krahl@ireas.org>2020-01-28 21:05:33 +0100
commitebd754d88330478981f65e4724cc561ceff4f9e7 (patch)
treeba291f2a4303850fdab7233d0e2e6b0a98760690 /src
parent777cbd0fee8187325b0272d3264b535828d4b4ea (diff)
downloadnitrokey-rs-ebd754d88330478981f65e4724cc561ceff4f9e7.tar.gz
nitrokey-rs-ebd754d88330478981f65e4724cc561ceff4f9e7.tar.bz2
Regenerate temporary passwords with null bytes
Previously, we silently cut off temporary passwords that contained a null byte. With the change to CString, we returned a LibraryError instead. With this patch, we change to generate_password function to continue generating passwords until we have a password without a null byte. The chance of generating a password with a null byte is ca. 10 % for our temporary password with 25 characters. Therefore the chance of having to re-generate the password multiple times is low enough that we don’t bother with re-generating only the null bytes of the password for the time being. This should be improved in the future.
Diffstat (limited to 'src')
-rw-r--r--src/util.rs12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/util.rs b/src/util.rs
index b9b1a68..a0d0d1b 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -76,9 +76,15 @@ pub fn get_last_error() -> Error {
}
pub fn generate_password(length: usize) -> Result<CString, Error> {
- let mut data = vec![0u8; length];
- OsRng.fill_bytes(&mut data[..]);
- get_cstring(data)
+ loop {
+ // Randomly generate a password until we get a string *without* null bytes. Otherwise
+ // the string would be cut off prematurely due to null-termination in C.
+ let mut data = vec![0u8; length];
+ OsRng.fill_bytes(&mut data[..]);
+ if let Ok(s) = CString::new(data) {
+ return Ok(s);
+ }
+ }
}
pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> {