diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-06 00:23:27 +0100 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-13 18:37:13 -0800 |
commit | 1d1cc940f47c41637adea5c5a1e5d3c80807f9d7 (patch) | |
tree | 8a44a82430e3df7ade7a61a6e4e30f100268af2d /nitrocli/src | |
parent | cb5b22d0886a0a8c19cf1afeef55a8245357cf20 (diff) | |
download | nitrocli-1d1cc940f47c41637adea5c5a1e5d3c80807f9d7.tar.gz nitrocli-1d1cc940f47c41637adea5c5a1e5d3c80807f9d7.tar.bz2 |
Add the base32 format for OTP secrets
Many applications display OTP secrets in the base32 format (according to
RFC 4648).
This patch adds base32 as a possible value for the --format option to
the otp set subcommand.
Diffstat (limited to 'nitrocli/src')
-rw-r--r-- | nitrocli/src/args.rs | 5 | ||||
-rw-r--r-- | nitrocli/src/commands.rs | 8 |
2 files changed, 12 insertions, 1 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index b109944..b5d4e81 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -327,6 +327,7 @@ impl From<OtpMode> for nitrokey::OtpMode { #[derive(Clone, Copy, Debug, PartialEq)] pub enum OtpSecretFormat { Ascii, + Base32, Hex, } @@ -337,6 +338,7 @@ impl fmt::Display for OtpSecretFormat { "{}", match *self { OtpSecretFormat::Ascii => "ascii", + OtpSecretFormat::Base32 => "base32", OtpSecretFormat::Hex => "hex", } ) @@ -349,6 +351,7 @@ impl str::FromStr for OtpSecretFormat { fn from_str(s: &str) -> result::Result<Self, Self::Err> { match s { "ascii" => Ok(OtpSecretFormat::Ascii), + "base32" => Ok(OtpSecretFormat::Base32), "hex" => Ok(OtpSecretFormat::Hex), _ => Err(()), } @@ -777,7 +780,7 @@ pub fn otp_set(ctx: &ExecCtx, args: Vec<String>) -> Result<()> { let _ = parser.refer(&mut secret_format).add_option( &["-f", "--format"], argparse::StoreOption, - "The format of the secret (ascii|hex)", + "The format of the secret (ascii|base32|hex)", ); parse(&parser, args)?; drop(parser); diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 71b2bdd..289c257 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -457,6 +457,13 @@ fn prepare_ascii_secret(secret: &str) -> Result<String> { } } +/// Prepare a base32 secret string for libnitrokey. +fn prepare_base32_secret(secret: &str) -> Result<String> { + base32::decode(base32::Alphabet::RFC4648 { padding: false }, secret) + .map(|vec| format_bytes(&vec)) + .ok_or_else(|| Error::Error("Could not parse base32 secret".to_string())) +} + /// Configure a one-time password slot on the Nitrokey device. pub fn otp_set( ctx: &args::ExecCtx, @@ -468,6 +475,7 @@ pub fn otp_set( ) -> Result<()> { let secret = match secret_format { args::OtpSecretFormat::Ascii => prepare_ascii_secret(&data.secret)?, + args::OtpSecretFormat::Base32 => prepare_base32_secret(&data.secret)?, args::OtpSecretFormat::Hex => data.secret, }; let data = nitrokey::OtpSlotData { secret, ..data }; |