From 10fcd3a946a270fc6d111252b2de08dcd625a2b8 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 14 Feb 2018 11:12:45 +0100 Subject: Initial commit with support for OTP generation --- .gitignore | 4 + LICENSE | 21 +++ Makefile | 32 +++++ README.md | 49 +++++++ TODO.md | 12 ++ config.mk | 18 +++ nkotp.1.pod | 141 ++++++++++++++++++++ nkotp.c | 331 +++++++++++++++++++++++++++++++++++++++++++++ options.c | 436 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ options.h | 44 ++++++ 10 files changed, 1088 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 TODO.md create mode 100644 config.mk create mode 100644 nkotp.1.pod create mode 100644 nkotp.c create mode 100644 options.c create mode 100644 options.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5cdb1cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/nkotp.1 +/nkotp.1.html +/pod2htmd.tmp +*.o diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1a3601d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Robin Krahl + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2d5b6c8 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +include config.mk + +VERSION := $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH) + +CPPFLAGS += -DNKOTP_VERSION_MAJOR=$(VERSION_MAJOR) \ + -DNKOTP_VERSION_MINOR=$(VERSION_MINOR) \ + -DNKOTP_VERSION_PATCH=$(VERSION_PATCH) \ + -DNKOTP_VERSION=\"$(VERSION)\" +CPPFLAGS += $(CPPFLAGS_CONFUSE) +CPPFLAGS += $(CPPFLAGS_NITROKEY) +LDFLAGS += $(LDFLAGS_CONFUSE) +LDFLAGS += $(LDFLAGS_NITROKEY) + +P2MFLAGS += --release=$(VERSION) + +OBJECTS := nkotp.o options.o +TARGETS := nkotp nkotp.1 nkotp.1.html + +.PHONY: all clean + +all: $(TARGETS) + +clean: + rm -f $(OBJECTS) $(TARGETS) pod2htmd.tmp + +nkotp: $(OBJECTS) + +nkotp.1: nkotp.1.pod + pod2man $(P2MFLAGS) $^ > $@ + +nkotp.1.html: nkotp.1.pod + pod2html $(P2HFLAGS) $^ > $@ diff --git a/README.md b/README.md new file mode 100644 index 0000000..83792d7 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# nkotp -- one-time password generator for Nitrokey devices on the command line + +`nkotp` provides access to the one-time password (OTP) generator on +[Nitrokey][nk] devices on the command line. Both the Nitrokey Pro and the +Nitrokey Storage support the generation of one-time passwords based on the +[HOTP][hotp] and [TOTP][totp] algorithms. `nkotp` uses [`libnitrokey`][libnk] +to configure the OTP slots and generate OTPs on a Nitrokey device. + +## Dependencies + +`nkotp` requires a POSIX-compliant operating system such as Linux or macOS. + +### Runtime dependencies + +- `libc` with `getopt_long` support +- [`libconfuse`][libconfuse] v3.0.0 or later +- [`libnitrokey`][libnk] v3.0 or later + +### Additional build dependencies + +- `gcc` or any other C99 compiler +- GNU `make` +- `pod2html`, `pod2man` (usually distributed with `perl`) + +## Compilation + +Build `nkotp` with `make`. You can configure the build in `config.mk` if you +have non-standard library paths or compiler flags. + +## Usage + +For usage information, consult the man page [`nkotp(1)`][man]. + +## Bugs and hacking + +If you encouter a bug or if you want to contribute to nkotp, please send an +email to [nkotp-dev@ireas.org][nkotp-dev]. + +## License + +This program is published under the terms of the MIT/X11 license (see LICENSE). + +[nk]: https://www.nitrokey.com/ +[hotp]: https://en.wikipedia.org/wiki/HMAC-based_One-time_Password_Algorithm +[totp]: https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm +[libconfuse]: https://github.com/martinh/libconfuse +[libnk]: https://github.com/Nitrokey/libnitrokey +[man]: https://code.ireas.org/nkotp/doc/ +[nkotp-dev]: mailto:nkotp-dev@ireas.org diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..19d1eb7 --- /dev/null +++ b/TODO.md @@ -0,0 +1,12 @@ +- Decide what we want to do if passwords are too long: truncate, fail + silently, fail with error message (current solution). Affects + `read_user_password` and `read_password` in `nkotp.c`. +- Consider moving some constants to `libnitrokey`, especially the maximum + password lengths and the number of HOTP and TOTP slots. +- Find out the appropriate values to pass to `NK_get_totp_code` and + `NK_get_totp_code_PIN`. Affects `otp_generate` and `otp_generate_password` + in `nkotp.c`. +- Use a better seed than the current time stamp for the generation of the + temporary password (`generate_tmp_password` in `nkotp.c`). Consider + switching to a better random number generator in the first place + (`getrandom` for Linux). diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..549bac5 --- /dev/null +++ b/config.mk @@ -0,0 +1,18 @@ +# Version information +VERSION_MAJOR := 0 +VERSION_MINOR := 0 +VERSION_PATCH := 1 + +# Dependencies +CPPFLAGS_CONFUSE := +CPPFLAGS_NITROKEY := +LDFLAGS_CONFUSE := -lconfuse +LDFLAGS_NITROKEY := -lnitrokey + +# Compiler flags +CFLAGS += -std=c99 -pedantic -Wall +CPPFLAGS += -D_XOPEN_SOURCE=700 -D_GNU_SOURCE + +# Man page generation +P2MFLAGS += --section=1 --center=nkotp --name=NKOTP +P2HFLAGS += --noindex --title="nkotp(1)" diff --git a/nkotp.1.pod b/nkotp.1.pod new file mode 100644 index 0000000..875042e --- /dev/null +++ b/nkotp.1.pod @@ -0,0 +1,141 @@ +=head1 NAME + +nkotp - one-time password generator for Nitrokey devices + +=head1 SYNOPSIS + +B +S<[B<-a> I]> +S<[B<-c> I]> +S<[B<-m> I]> +S<[B<-s> I]> +S | B<-h> | B<-v>> + +=head1 DESCRIPTION + +B provides access to the one-time password (OTP) generator on Nitrokey +devices. Currently, B only supports the generation of OTPs. + +If an action requires the user password, it is prompted from the standard +input or read from the environment variable B (if set). + +=head1 OPTIONS + +=head2 General options + +=over + +=item B<-a> I, B<--algorithm> I + +Set the algorithm to use for one-time password operations. I can be +B for HOTP and B for TOTP (default). + +=item B<-c> I, B<--config> I + +Read the configuration from I. See the B section for the default +configuration files. + +=item B<-m> I, B<--model> I + +Set the Nitrokey model to connect to. I can be B

for a Nitrokey Pro, +B for a Nitrokey Storage and B for automatic selection (default). + +=item B<-s> I, B<--slot> I + +Set the slot to use for one-time password operations. The available slots +depend on the OTP algorithm (see B<--algorithm>). Currently, Nitrokey devices +provide three HOTP and 15 TOTP slots. The slot numbering starts at one. The +default value for this option is one. + +=back + +=head2 Modes of operation + +=over + +=item B<-g>, B<--generate> + +Generate a one-time password on the Nitrokey device and output it. The OTP +algorithm is set with the B<--algorithm> option. The OTP slot on the Nitrokey +device is set with the B<--slot> option. + +=item B<-h>, B<--help> + +Print a help message and exit. + +=item B<-v>, B<--version> + +Print version information and exit. + +=back + +=head1 CONFIGURATION + +B can read default values for the command-line options from a +configuration file. See the B section for more information on the +possible locations for the configuration file. + +The configuration file may assign values to the following options: + +=over + +=item B + +=item B + +=item B + +=back + +Each option corresponds to the command-line option with the same name. Values +set in the configuration file take precedence over environment variables. + +The configuration file should contain one assignment per line. Assignments +have the form C