author | Olivier Brunel
<jjk@jjacky.com> 2023-05-09 11:11:08 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-07-05 07:37:02 UTC |
parent | 473be70d533a3de7aa8f58c4cf11d584f86ace38 |
src/doc/shldata-rw.h/shldata_write.3.md | +8 | -1 |
src/liblimb/shldata-rw.h/shldata_read.c | +14 | -1 |
diff --git a/src/doc/shldata-rw.h/shldata_write.3.md b/src/doc/shldata-rw.h/shldata_write.3.md index c7ce352..b125016 100644 --- a/src/doc/shldata-rw.h/shldata_write.3.md +++ b/src/doc/shldata-rw.h/shldata_write.3.md @@ -56,6 +56,13 @@ If the magic is valid, parameters will read and the encrypted data will then be decrypted using the password pointed by `pwd` of length `plen` and placed into the *stralloc* pointed by `sa`. +! INFO: +! It is possible to set the value pointed to be `magic` to the expected magic +! from the file. `shldata_read`() will check the value before reading the file, +! and if it was set to a valid magic (as per `shldata_chkmagic`()) instead of +! just checking the magic from the file is valid, it will check that it matches +! the expected value. + # RETURN VALUE The `shldata_chkmagic`() function returns 1 is `magic` is a valid shielded data @@ -70,7 +77,7 @@ The `shldata_write`() and `shldta_read`() functions may fail if : : *EINVAL* :: The magic number (given in `magic` or read from `file`, respectively) isn't -:: a valid magic for a shielded data file. +:: a valid magic for a shielded data file (or doesn't match the expected magic). : *ENOMEM* :: Out of memory. diff --git a/src/liblimb/shldata-rw.h/shldata_read.c b/src/liblimb/shldata-rw.h/shldata_read.c index 043d177..ab86ed6 100644 --- a/src/liblimb/shldata-rw.h/shldata_read.c +++ b/src/liblimb/shldata-rw.h/shldata_read.c @@ -1,12 +1,22 @@ /* This file is part of limb https://lila.oss/limb * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ /* SPDX-License-Identifier: GPL-2.0-only */ +#include <errno.h> #include <limb/buffer.h> #include <limb/buffer-shldata.h> #include <limb/djbunix.h> #include <limb/shldata-rw.h> #include <limb/unix-transactional.h> +static int +chkmagic(u32 magic, u32 wmagic) +{ + if (!wmagic) return shldata_chkmagic(magic); + if (magic == wmagic) return 1; + + return (errno = EINVAL, 0); +} + int shldata_read(u32 *magic, u64 *ver, stralloc *sa, int bfd, const char *file, const char *pwd, size_t plen) @@ -20,9 +30,12 @@ shldata_read(u32 *magic, u64 *ver, stralloc *sa, int bfd, const char *file, buffer_shldata_ctx bsd = BUFFER_SHLDATA_ZERO; + u32 wmagic; + wmagic = (shldata_chkmagic(*magic)) ? *magic : 0; + size_t salen = sa->len; if (!buffer_gethdr(&b, magic, ver) - || !shldata_chkmagic(*magic) + || !chkmagic(*magic, wmagic) || !buffer_shldata_getinit_sa(&b, pwd, plen, sa, &bsd) || !stralloc_readyplus(sa, buffer_shldata_datasize(&bsd)) || !buffer_shldata_get(&b, sa->s + sa->len, buffer_shldata_datasize(&bsd), &bsd)