journal-authenticate.c revision 56f64d95763a799ba4475daf44d8e9f72a1bd474
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2012 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <fcntl.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <sys/mman.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "journal-def.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "journal-file.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "journal-authenticate.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "fsprg.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poetteringstatic uint64_t journal_file_tag_seqnum(JournalFile *f) {
b4bbcaa9c44260e88402cb8f9a5fb8ac7f35e123Thomas Hindoe Paaboel Andersen uint64_t r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering assert(f);
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = le64toh(f->header->n_tags) + 1;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->header->n_tags = htole64(r);
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
430f0182b72373145c839dbfe99d2382855cb8f8Lennart Poettering return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poetteringint journal_file_append_tag(JournalFile *f) {
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen Object *o;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering uint64_t p;
33d52ab92f2f0bfd706e6f343d172618d1e03f3dLennart Poettering int r;
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poettering assert(f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering if (!f->seal)
afc5dbf37fd2399d37976388d9dd9ab470ecf446Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!f->hmac_running)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(f->hmac);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_append_object(f, OBJECT_TAG, sizeof(struct TagObject), &o, &p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering o->tag.seqnum = htole64(journal_file_tag_seqnum(f));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering o->tag.epoch = htole64(FSPRG_GetEpoch(f->fsprg_state));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering log_debug("Writing tag %"PRIu64" for epoch %"PRIu64"",
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering le64toh(o->tag.seqnum),
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering FSPRG_GetEpoch(f->fsprg_state));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering /* Add the tag object itself, so that we can protect its
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * header. This will exclude the actual hash value in it */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_TAG, o, p);
32917e33882778cf2ec6dd54b1e1082266fb072eZbigniew Jędrzejewski-Szmek if (r < 0)
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering return r;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering /* Get the HMAC tag and store it in the object */
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek memcpy(o->tag.tag, gcry_md_read(f->hmac, 0), TAG_LENGTH);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->hmac_running = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poetteringint journal_file_hmac_start(JournalFile *f) {
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz uint8_t key[256 / 8]; /* Let's pass 256 bit from FSPRG to HMAC */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering return 0;
e22aa3d3284709234f086ebebc13a905a295b7a7Lennart Poettering
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (f->hmac_running)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return 0;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Prepare HMAC for next cycle */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_md_reset(f->hmac);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_GetKey(f->fsprg_state, key, sizeof(key), 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_md_setkey(f->hmac, key, sizeof(key));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->hmac_running = true;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int journal_file_get_epoch(JournalFile *f, uint64_t realtime, uint64_t *epoch) {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer uint64_t t;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(epoch);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(f->seal);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (f->fss_start_usec == 0 ||
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_interval_usec == 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -ENOTSUP;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (realtime < f->fss_start_usec)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -ESTALE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering t = realtime - f->fss_start_usec;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = t / f->fss_interval_usec;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering *epoch = t;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int journal_file_fsprg_need_evolve(JournalFile *f, uint64_t realtime) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint64_t goal, epoch;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_get_epoch(f, realtime, &goal);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (epoch > goal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ESTALE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return epoch != goal;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_fsprg_evolve(JournalFile *f, uint64_t realtime) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint64_t goal, epoch;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_get_epoch(f, realtime, &goal);
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering epoch = FSPRG_GetEpoch(f->fsprg_state);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (epoch < goal)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Evolving FSPRG key from epoch %"PRIu64" to %"PRIu64".", epoch, goal);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering for (;;) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (epoch > goal)
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return -ESTALE;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (epoch == goal)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_Evolve(f->fsprg_state);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering epoch = FSPRG_GetEpoch(f->fsprg_state);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekint journal_file_fsprg_seek(JournalFile *f, uint64_t goal) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering void *msk;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek uint64_t epoch;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek assert(f);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (!f->seal)
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return 0;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(f->fsprg_seed);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (f->fsprg_state) {
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek /* Cheaper... */
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (goal == epoch)
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return 0;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek if (goal == epoch+1) {
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek FSPRG_Evolve(f->fsprg_state);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek return 0;
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek }
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek } else {
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek f->fsprg_state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek f->fsprg_state = malloc(f->fsprg_state_size);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek if (!f->fsprg_state)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -ENOMEM;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Seeking FSPRG key to %"PRIu64".", goal);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering msk = alloca(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_GenMK(msk, NULL, f->fsprg_seed, f->fsprg_seed_size, FSPRG_RECOMMENDED_SECPAR);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_Seek(f->fsprg_state, goal, msk, f->fsprg_seed, f->fsprg_seed_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_maybe_append_tag(JournalFile *f, uint64_t realtime) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(f);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!f->seal)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (realtime <= 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering realtime = now(CLOCK_REALTIME);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = journal_file_fsprg_need_evolve(f, realtime);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r <= 0)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return 0;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = journal_file_append_tag(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_fsprg_evolve(f, realtime);
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_hmac_put_object(JournalFile *f, int type, Object *o, uint64_t p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek assert(f);
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo r = journal_file_hmac_start(f);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (r < 0)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return r;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (!o) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo r = journal_file_move_to_object(f, type, p, &o);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo if (r < 0)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return r;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo } else {
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering if (type >= 0 && o->object.type != type)
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo return -EBADMSG;
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo }
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o, offsetof(ObjectHeader, payload));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo switch (o->object.type) {
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo case OBJECT_DATA:
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo /* All but hash and payload are mutable */
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->data.hash, sizeof(o->data.hash));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o->data.payload, le64toh(o->object.size) - offsetof(DataObject, payload));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo break;
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo case OBJECT_FIELD:
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering /* Same here */
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->field.hash, sizeof(o->field.hash));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o->field.payload, le64toh(o->object.size) - offsetof(FieldObject, payload));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo break;
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo case OBJECT_ENTRY:
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo /* All */
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->entry.seqnum, le64toh(o->object.size) - offsetof(EntryObject, seqnum));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case OBJECT_FIELD_HASH_TABLE:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case OBJECT_DATA_HASH_TABLE:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case OBJECT_ENTRY_ARRAY:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Nothing: everything is mutable */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case OBJECT_TAG:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* All but the tag itself */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &o->tag.seqnum, sizeof(o->tag.seqnum));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &o->tag.epoch, sizeof(o->tag.epoch));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek default:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_hmac_put_header(JournalFile *f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering assert(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek r = journal_file_hmac_start(f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* All but state+reserved, boot_id, arena_size,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt * tail_object_offset, n_objects, n_entries,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * tail_entry_seqnum, head_entry_seqnum, entry_array_offset,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * head_entry_realtime, tail_entry_realtime,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * tail_entry_monotonic, n_data, n_fields, n_tags,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * n_entry_arrays. */
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, f->header->signature, offsetof(Header, state) - offsetof(Header, signature));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->file_id, offsetof(Header, boot_id) - offsetof(Header, file_id));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->seqnum_id, offsetof(Header, arena_size) - offsetof(Header, seqnum_id));
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->data_hash_table_offset, offsetof(Header, tail_object_offset) - offsetof(Header, data_hash_table_offset));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_fss_load(JournalFile *f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r, fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char *p = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct stat st;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek FSSHeader *m = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_id128_t machine;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering assert(f);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (!f->seal)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = sd_id128_get_machine(&machine);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/fss",
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek SD_ID128_FORMAT_VAL(machine)) < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -ENOMEM;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (fd < 0) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (errno != ENOENT)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to open %s: %m", p);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = -errno;
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek goto finish;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (fstat(fd, &st) < 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (st.st_size < (off_t) sizeof(FSSHeader)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -ENODATA;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED, fd, 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m == MAP_FAILED) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m = NULL;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt r = -errno;
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering goto finish;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (memcmp(m->signature, FSS_HEADER_SIGNATURE, 8) != 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = -EBADMSG;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->incompatible_flags != 0) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = -EPROTONOSUPPORT;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto finish;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (le64toh(m->header_size) < sizeof(FSSHeader)) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = -EBADMSG;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto finish;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (le64toh(m->fsprg_state_size) != FSPRG_stateinbytes(le16toh(m->fsprg_secpar))) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = -EBADMSG;
65c1d46b0923771955519329160a1e4c7cd027b0Lennart Poettering goto finish;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering f->fss_file_size = le64toh(m->header_size) + le64toh(m->fsprg_state_size);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if ((uint64_t) st.st_size < f->fss_file_size) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = -ENODATA;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering goto finish;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (!sd_id128_equal(machine, m->machine_id)) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = -EHOSTDOWN;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk goto finish;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (le64toh(m->start_usec) <= 0 ||
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering le64toh(m->interval_usec) <= 0) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = -EBADMSG;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering goto finish;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (f->fss_file == MAP_FAILED) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_file = NULL;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = -errno;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering goto finish;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek f->fss_start_usec = le64toh(f->fss_file->start_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_interval_usec = le64toh(f->fss_file->interval_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fsprg_state = (uint8_t*) f->fss_file + le64toh(f->fss_file->header_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fsprg_state_size = le64toh(f->fss_file->fsprg_state_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = 0;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekfinish:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (m)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering munmap(m, PAGE_ALIGN(sizeof(FSSHeader)));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering safe_close(fd);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering free(p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return r;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekstatic void initialize_libgcrypt(void) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *p;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = gcry_check_version("1.4.5");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_hmac_setup(JournalFile *f) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_error_t e;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!f->seal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering initialize_libgcrypt();
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering e = gcry_md_open(&f->hmac, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (e != 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -ENOTSUP;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringint journal_file_append_first_tag(JournalFile *f) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering uint64_t p;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!f->seal)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_debug("Calculating first tag...");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_hmac_put_header(f);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p = le64toh(f->header->field_hash_table_offset);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (p < offsetof(Object, hash_table.items))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p -= offsetof(Object, hash_table.items);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_FIELD_HASH_TABLE, NULL, p);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p = le64toh(f->header->data_hash_table_offset);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (p < offsetof(Object, hash_table.items))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return -EINVAL;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p -= offsetof(Object, hash_table.items);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_DATA_HASH_TABLE, NULL, p);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_append_tag(f);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_parse_verification_key(JournalFile *f, const char *key) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint8_t *seed;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t seed_size, c;
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering const char *k;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long start, interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seed_size = FSPRG_RECOMMENDED_SEEDLEN;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seed = malloc(seed_size);
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering if (!seed)
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek k = key;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (c = 0; c < seed_size; c++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int x, y;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (*k == '-')
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek k++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek x = unhexchar(*k);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (x < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(seed);
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering return -EINVAL;
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering }
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering k++;
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering y = unhexchar(*k);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (y < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(seed);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek k++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering seed[c] = (uint8_t) (x * 16 + y);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*k != '/') {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(seed);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek k++;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sscanf(k, "%llx-%llx", &start, &interval);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r != 2) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(seed);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->fsprg_seed = seed;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fsprg_seed_size = seed_size;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->fss_start_usec = start * interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->fss_interval_usec = interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekbool journal_file_next_evolve_usec(JournalFile *f, usec_t *u) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk uint64_t epoch;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(f);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f->seal)
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *u = (usec_t) (f->fss_start_usec + f->fss_interval_usec * epoch + f->fss_interval_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek