Skip to content

Commit

Permalink
Make project build on stable Rust 1.28
Browse files Browse the repository at this point in the history
* Remove reliance on unstable feature int_to_from_bytes
	* Add a fallback method until feature becomes stable in Rust 1.29
	* See rust-lang/rust#51835
* Remove reliance on experimental feature attr_literals
	* Only used for `structopt`, but it provides `raw()`
	* See rust-lang/rust#34981
* Make tests run again
  • Loading branch information
ALCC01 committed Jul 17, 2018
1 parent 960090e commit 3589681
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
14 changes: 8 additions & 6 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ pub enum Command {
#[structopt(name = "touch")]
/// Initialize an empty vault file
Touch {
#[structopt(short = "f", long = "force", takes_value = false)]
#[structopt(short = "f", long = "force", raw(takes_value = "false"))]
/// Overwrite an existing file
force: bool,
},
#[structopt(name = "ls")]
/// List all records in a vault
List {
#[structopt(long = "disclose", takes_value = false)]
#[structopt(long = "disclose", raw(takes_value = "false"))]
/// Disclose secrets
disclose: bool,
},
Expand All @@ -58,13 +58,15 @@ pub enum OtpCommand {
#[structopt(
requires = "secret",
long = "totp",
takes_value = false,
raw(takes_value = "false"),
group = "algo",
conflicts_with = "hotp"
)]
/// Use TOTP as the generation algorithm
totp: bool,
#[structopt(requires = "secret", long = "hotp", takes_value = false, group = "algo")]
#[structopt(
requires = "secret", long = "hotp", raw(takes_value = "false"), group = "algo"
)]
/// Use HOTP as the generation algorithm
hotp: bool,
/// A label for this secret
Expand Down Expand Up @@ -195,7 +197,7 @@ pub fn match_args(sigil: Sigil) -> Result<(), Error> {
cli::password::remove_record(&vault?, &key?, ctx?, record)
}
PasswordCommand::GetPassword { record } => {
cli::password::get_password(&vault?, ctx?, record)
cli::password::get_password(&vault?, ctx?, &record)
}
PasswordCommand::Generate { chars } => cli::password::generate_password(chars),
},
Expand Down Expand Up @@ -237,7 +239,7 @@ pub fn match_args(sigil: Sigil) -> Result<(), Error> {
}
OtpCommand::ImportUrl { url } => cli::otp::import_url(&vault?, &key?, ctx?, &url),
OtpCommand::GetToken { record, counter } => {
cli::otp::get_token(&vault?, ctx?, record, counter)
cli::otp::get_token(&vault?, ctx?, &record, counter)
}
OtpCommand::Remove { record } => cli::otp::remove_record(&vault?, &key?, ctx?, record),
},
Expand Down
4 changes: 2 additions & 2 deletions src/cli/otp/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use std::path::PathBuf;
pub fn get_token(
vault_path: &PathBuf,
mut ctx: Context,
record_id: String,
record_id: &str,
counter: Option<u64>,
) -> Result<(), Error> {
tracepoint!();

// (1)
let vault = utils::read_vault(&vault_path, &mut ctx).unwrap();
let record = vault.get_otp_record(record_id)?;
let record = vault.get_otp_record(&record_id)?;

// (2)
tracepoint!();
Expand Down
8 changes: 2 additions & 6 deletions src/cli/password/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ use std::path::PathBuf;
* 1. `read_vault`, `vault.get_record`, bail on error
* 2. Return the `password` field
*/
pub fn get_password(
vault_path: &PathBuf,
mut ctx: Context,
record_id: String,
) -> Result<(), Error> {
pub fn get_password(vault_path: &PathBuf, mut ctx: Context, record_id: &str) -> Result<(), Error> {
tracepoint!();

// (1)
let vault = utils::read_vault(&vault_path, &mut ctx).unwrap();
let record = vault.get_record(record_id)?;
let record = vault.get_record(&record_id)?;

// (2)
println!("{}", record.password);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pub enum VaultError {
#[fail(display = "Record should be updated, not added")]
ShouldUpdate,
#[fail(display = "Failed to find record {}", 0)]
UnknownRecord(String),
#[fail(display = "Failed to find a matching record")]
UnknownRecord,
#[fail(display = "Vault path already exists")]
Overwriting,
#[fail(display = "Vault path is a directory")]
Expand Down
33 changes: 25 additions & 8 deletions src/lib/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
// (2)
tracepoint!();
let K = hmac::SigningKey::new(algorithm.to_algorithm(), K.as_ref());
// Swap bytes because of endianess
debug!("Counter ({}) is {:?}", C, C.swap_bytes().to_bytes());
let H = hmac::sign(&K, &C.swap_bytes().to_bytes());
// TODO Use int_to_from_bytes (see Rust PR #51835) when stabilized
// in Rust 1.29. When using it, .swap_bytes() because of endianess
println!("Counter ({}) is {:?}", C, u64_into_bytes(C));
let H = hmac::sign(&K, &u64_into_bytes(C));
debug!(
"Signed digest is {}",
H.as_ref()
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
#[cfg(test)]
mod tests {
use lib::otp;
use ring::digest;
use lib::types::HmacAlgorithm;

// Test values provided in RFC 4226
// Base32 for "12345678901234567890";
Expand All @@ -124,7 +125,7 @@ mod tests {
fn hotp_rfc_values() {
for value in 0..RFC_HOTP_VALUES.len() {
assert_eq!(
otp::hotp(&RFC_HOTP_SECRET, value as u64, 6, &digest::SHA1),
otp::hotp(&RFC_HOTP_SECRET, value as u64, 6, &HmacAlgorithm::SHA1),
RFC_HOTP_VALUES[value as usize]
);
}
Expand Down Expand Up @@ -155,7 +156,7 @@ mod tests {
&RFC_TOTP_SECRET_SHA1,
8,
RFC_TOTP_TIMES[value],
&digest::SHA1
&HmacAlgorithm::SHA1
),
RFC_TOTP_VALUES_SHA1[value as usize]
);
Expand All @@ -178,7 +179,7 @@ mod tests {
&RFC_TOTP_SECRET_SHA256,
8,
RFC_TOTP_TIMES[value],
&digest::SHA256
&HmacAlgorithm::SHA256
),
RFC_TOTP_VALUES_SHA256[value as usize]
);
Expand All @@ -201,10 +202,26 @@ mod tests {
&RFC_TOTP_SECRET_SHA512,
8,
RFC_TOTP_TIMES[value],
&digest::SHA512
&HmacAlgorithm::SHA512
),
RFC_TOTP_VALUES_SHA512[value as usize]
);
}
}
}

/// Converts u64 to a u8 array
/// Fallback method waiting for stabilization of int_to_from_bytes
/// (see Rust PR #51835) in Rust 1.29
fn u64_into_bytes(x: u64) -> [u8; 8] {
[
((x >> 56) & 0xff) as u8,
((x >> 48) & 0xff) as u8,
((x >> 40) & 0xff) as u8,
((x >> 32) & 0xff) as u8,
((x >> 24) & 0xff) as u8,
((x >> 16) & 0xff) as u8,
((x >> 8) & 0xff) as u8,
(x & 0xff) as u8,
]
}
18 changes: 8 additions & 10 deletions src/lib/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,41 @@ impl Vault {

pub fn remove_record(&mut self, record_id: String) -> Result<(), VaultError> {
tracepoint!();
let r = record_id.clone(); // We need ownership if we need to build an error
match self.passwords.entry(record_id) {
Entry::Occupied(entry) => {
entry.remove();
Ok(())
}
_ => Err(VaultError::UnknownRecord(r)),
_ => Err(VaultError::UnknownRecord),
}
}

pub fn remove_otp_record(&mut self, record_id: String) -> Result<(), VaultError> {
tracepoint!();
let r = record_id.clone(); // We need ownership if we need to build an error
match self.otps.entry(record_id) {
Entry::Occupied(entry) => {
entry.remove();
Ok(())
}
_ => Err(VaultError::UnknownRecord(r)),
_ => Err(VaultError::UnknownRecord),
}
}

pub fn get_record(&self, record_id: String) -> Result<&Record, VaultError> {
pub fn get_record(&self, record_id: &str) -> Result<&Record, VaultError> {
tracepoint!();
if let Some(record) = self.passwords.get(&record_id) {
if let Some(record) = self.passwords.get(record_id) {
Ok(record)
} else {
Err(VaultError::UnknownRecord(record_id))
Err(VaultError::UnknownRecord)
}
}

pub fn get_otp_record(&self, record_id: String) -> Result<&OtpRecord, VaultError> {
pub fn get_otp_record(&self, record_id: &str) -> Result<&OtpRecord, VaultError> {
tracepoint!();
if let Some(record) = self.otps.get(&record_id) {
if let Some(record) = self.otps.get(record_id) {
Ok(record)
} else {
Err(VaultError::UnknownRecord(record_id))
Err(VaultError::UnknownRecord)
}
}

Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(attr_literals)]
extern crate env_logger; // TODO env_logger may not be a good fit
#[macro_use]
extern crate structopt;
Expand Down

0 comments on commit 3589681

Please sign in to comment.