61 Commits

Author SHA1 Message Date
Christian Marangi
57270b247c CMakeLists: update cmake minimum required version to 3.10
New cmake version 4.0 requires at least 3.5 version as the minimum
required version with it increased to 3.10 in to-be-released cmake
versions.

Set the minimum required version to 3.10 to future-proof for future
cmake version.

Suggested-by: Hannu Nyman <hannu.nyman@iki.fi>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
2025-10-03 23:31:54 +02:00
Matthias Schiffer
00b921d80a Do not print line number in debug messages
The line number does not add any significant information, and it makes
the unit tests which check for these debug messages very fragile.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 23:04:05 +02:00
Matthias Schiffer
96c42c5ed3 Fix length checks in cert_load()
cert_load() iterates over multiple blobs, so the length argument to
blob_parse_untrusted() needs to be updated to prevent out-of-bounds
accesses.

Some other checks have become redundant and are removed, as
blob_parse_untrusted() already ensures that all attrs are contained in
the passed buffer.

Note that this issue currently does not pose a security threat, as an
over-restrictive check in blob_parse_untrusted() broke parsing of
buffers with multiple blobs completely.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 22:43:55 +02:00
Matthias Schiffer
fe06b4b836 usign-exec: improve usign -F output handling
While not likely to happen in pratice, nothing guarantees that read()
will retrieve more than 1 byte at a time. The easiest way to make this
code compliant is to wrap the file descriptor using fdopen().

While we're at it, also
- remove useless memset()
- check fingerprint for validity

The check is particularly relevant, as a usign bug [1] causing short
fingerprint outputs only went unnoticed for so long because the trailing
newline was considered one of the 16 characters ucert was expecting.

[1] 8ead1fd6a6.1589642591.git.mschiffer@universe-factory.net/

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:53:40 +02:00
Matthias Schiffer
19f9e1917e usign-exec: return code fixes
- WEXITSTATUS() should only be called when WIFEXITED() returns true
- Fix double WEXITSTATUS() in usign_f()

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:45:23 +02:00
Matthias Schiffer
077feb5b58 usign-exec: close writing end of pipe early in parent process
When the child process exited without producing output (for example
because usign was not found), the parent process would hang forever in
read(). By closing the writing end early in the parent process, read
will return as soon as no writing FDs are left - that is, when the child
process has exited.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:27:51 +02:00
Matthias Schiffer
7ec4bb764e usign-exec: remove redundant return statements
All switch() cases were already returning value or exiting. Instead,
move the default case out of the switch to reduce indentation (only
relevant for usign_f()).

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:26:52 +02:00
Matthias Schiffer
5a738e549d usign-exec: change usign_f_* fingerprint argument to char[17]
This makes it more obvious that a buffer with space for 17 characters is
expected to be passed. The code still works the same (a char[17] is
equivalent to char* as an argument).

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:26:40 +02:00
Matthias Schiffer
112488bbbc usign-exec: do not close stdin and stderr before exec
FDs 0, 1 and 2 should always be available. This also allows the exec error
message in the forked process to be displayed.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:26:34 +02:00
Matthias Schiffer
38dcb1a6f1 usign-exec: fix exec error handling
When execvp fails in the forked process, we must exit. Also add an error
message.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:26:23 +02:00
Matthias Schiffer
a9be4fb17d usign-exec: simplify usign execv calls
When the executable to exec is passed as an absolute path, execv() and
execvp() are equivalent, so there it no need to make the code hard to
read with #ifdefs.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 18:26:19 +02:00
Matthias Schiffer
854d93e232 Introduce read_file() helper, improve error reporting
This helper simplifies handling, ensures that there are no resource
leaks, and checks for EOF more robustly.

Also introduce error reporting at all call sites to give the user some
feedback when something went wrong.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 14:47:24 +02:00
Matthias Schiffer
afc86f352b Fix return code of write_file()
write_file() returns 1/true on success; it should return 0/false when
opening the file fails.

To make it more obvious that is function returns true and not 0 on
success, also change its return type to bool.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 14:14:02 +02:00
Matthias Schiffer
fdff108523 stdout/stderr improvements
- Print error messages to stderr
- fprintf(stdout, ...) is just printf(...)

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2020-05-16 13:30:26 +02:00
Petr Štetiar
dddb2aa812 ci: fix unit test failures by enabling full ucert build
Fixing following unit test failures:

 $ ucert -D -c $TEST_INPUTS/key-build.ucert
 ucert: invalid option -- 'D'

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2020-01-21 18:23:13 +01:00
Petr Štetiar
5f206bcfe5 ci: enable unit testing
In commit 4462ff9ded ("add cram based unit tests") some unit tests
were added so enable them on CI as well.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-26 09:48:31 +01:00
Petr Štetiar
14a279411c fix certificate blob parsing vulnerability by using blob_parse_untrusted
blob_parse expects blobs from trusted inputs, but in this case it can be
supplied with possibly malicious certificates from untrusted inputs as
well, so in order to prevent such conditions, switch to
blob_parse_untrusted which should hopefully handle such inputs
appropriately.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 20:52:58 +01:00
Petr Štetiar
19a7225ac0 fix leaking memory in cert_dump_blob
Fixes following valgrind reported memory leak:

 189 bytes in 1 blocks are definitely lost in loss record 3 of 4
    at realloc
    by blobmsg_format_json_with_cb
    by blobmsg_format_json_indent
    by cert_dump_blob (ucert.c:386)
    by cert_dump (ucert.c:405)
    by main (ucert.c:728)

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 20:52:58 +01:00
Petr Štetiar
9dba44ddd4 fix possibly garbage value returned in cert_process_revoker
Fixes following warning reported by clang-9 scan-build analyzer:

 ucert.c:585:2: warning: Undefined or garbage value returned to caller
        return ret;
        ^~~~~~~~~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 20:52:58 +01:00
Petr Štetiar
4462ff9ded add cram based unit tests
For improved QA etc. for the start with initial test case for dump
command.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 20:52:58 +01:00
Petr Štetiar
5fe64b5606 cmake: split usign bits into static library
So it could be reused easily in unit tests for example.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 14:29:57 +01:00
Petr Štetiar
5d7626a2b6 cmake: reindent the file
In order to make the indentation consistent within the file.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 14:23:26 +01:00
Petr Štetiar
e284ed9419 cmake: enable hardening compiler flags and fix the reported issues
Lets enable some useful flags in order to spot possible issues during
QA on CI (GCC version 6 and higher). Fix warnings uncovered by this new
flags as reported by clang-9 on x86/64:

 ucert.c:158:33: error: comparison of integers of different signs: 'unsigned long' and 'int' [-Werror,-Wsign-compare]
 ucert.c:176:14: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ucert.c:314:18: error: comparison of integers of different signs: '__time_t' (aka 'long') and 'uint64_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
 ucert.c:315:18: error: comparison of integers of different signs: '__time_t' (aka 'long') and 'uint64_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
 ucert.c:557:17: error: comparison of integers of different signs: '__time_t' (aka 'long') and 'uint64_t' (aka 'unsigned long') [-Werror,-Wsign-compare]

Ref: https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/
Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 14:12:25 +01:00
Petr Štetiar
7e53906663 add initial GitLab CI support
Uses currently proof-of-concept openwrt-ci[1] in order to:

 * improve the quality of the codebase in various areas
 * decrease code review time and help merging contributions faster
 * get automagic feedback loop on various platforms and tools
   - out of tree build with OpenWrt SDK on following targets:
     * ath79-generic
     * imx6-generic
     * malta-be
     * mvebu-cortexa53
   - out of tree native build on x86/64 with GCC (versions 7, 8, 9) and Clang 10
   - out of tree native x86/64 static code analysis with cppcheck and
     scan-build from Clang 10

1. https://gitlab.com/ynezz/openwrt-ci/

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 13:55:38 +01:00
Petr Štetiar
fa0bf4ef45 cmake: add proper include and library dependencies
Otherwise it's not possible to compile it properly if the dependencies
are not installed in the standard include/libraries paths.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-16 09:09:18 +01:00
Rosen Penev
e4bd927cc7 cast ucert_argv to proper type when passing to execv
Fixes warnings:

warning: passing argument 2 of 'execv' from incompatible pointer type
[-Wincompatible-pointer-types]
  254 |       execv(usign_argv[0], usign_argv)

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-28 21:20:13 +01:00
Daniel Golle
e1c722b1b6 be more tolerant when reading key fingerprint
usign occasionally writes 16 characters then exits without writing a LF,
leaving ucert hanging waiting for more input.  Accept 16 characters
or more rather than 17 to work around the short read.

Signed-off-by: Mike McCormack <mike@atratus.org>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2018-09-18 13:34:26 +02:00
Damien Mascord
1056e730f5 Change the sigb buffer to be the same size as the fread
Signed-off-by: Damien Mascord <tusker@tusker.org>
2018-08-08 19:48:11 +02:00
Daniel Golle
63ad591e59 blob_buf needs to be zero'd
Fixes weird segfaults when compiling libubox with GCC 8.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2018-08-07 22:27:28 +02:00
Daniel Golle
ad816fc5eb set rpath to make bundle-libraries.sh happy
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2018-08-06 17:43:46 +02:00
Daniel Golle
46eec66b19 don't ever set pointer outside of buffer
even if it's not going to be used.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
54be4d6543 fix host build
use execvp in host builds instead of hardcoding /usr/bin/usign path

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
d1cc3a0b80 harden reading fingerprint from usign process
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
57c42c1a71 add light build variant without -C, -A and -D
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
0ac1d402db remove unused stat variable and gettimeofday only once while verifying
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
8e91520771 README.md...
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
c5369001f1 allow issue to append existing cert and be strictly quiet
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
5b2c68cf69 don't be crazily strickt on position of '-q' parameter
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
58269c35ef fix memory corruption caused by use-after-free
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
4a23b1ac9f README.md: add a line about context and dependencies
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
44e76778e3 output error message in case of revoked key
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
4ec94878f6 add README.md
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
321c9f9d42 add comments in usign-exec
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
bdc1ec4513 harden cmdline options
make all options single-set, only accept options after command and only
those needed for the specific command.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
493cc9a837 add comments and license headers
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
b0a87a068a take care of revokers in verify path
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
541693200a improve usage message and start working on revoker logic
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
b62d1266c1 allow append also on non-existing certfile
Just in case someone just wants a single plain signature without any
chain.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
8d598fe3c8 enumerate chain elements in dump output
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00
Daniel Golle
f75cdc5453 always include complete signature file including trailing newline
just to harmonize things

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This work was sponsored by WIO (wiowireless.com)
2018-06-14 18:51:22 +02:00