1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright (c) 2019 Airbus Commercial Aircraft
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include "messages.h"
#include "slog.h"
// Arguments and options
static gboolean iterative = FALSE;
static char *hostKey = NULL;
static char *prevHostKey = NULL;
static char *curMacFile = NULL;
static char *prevMacFile = NULL;
static char *inputLog = NULL;
static char *outputLog = NULL;
static int bufSize = DEF_BUF_SIZE;
// Return 1 on success, 0 on error
int normalMode(char *hostkey, char *MACfile, char *inputlog, char *outputlog, int bufsize)
{
char key[KEY_LENGTH];
guint64 counter;
msg_info("[SLOG] INFO: Reading key file", evt_tag_str("name", hostkey));
int ret = readKey(key, &counter, hostkey);
if (ret!=1)
{
msg_error("[SLOG] ERROR: Unable to read host key", evt_tag_str("file", hostkey));
return 0;
}
if (counter!=0UL)
{
msg_error("[SLOG] ERROR: Initial key k0 is required for verification and decryption but the supplied key read has a counter > 0.",
evt_tag_long("Counter", counter));
return 0;
}
msg_info("[SLOG] INFO: Reading MAC file", evt_tag_str("name", MACfile));
FILE *bigMAC = fopen(MACfile, "r");
if(bigMAC == NULL)
{
msg_error("[SLOG] ERROR: Unable to read MAC", evt_tag_str("file", MACfile));
return 0;
}
unsigned char MAC[CMAC_LENGTH];
if (readBigMAC(MACfile, (char *)MAC)==0)
{
msg_warning("[SLOG] WARNING: Unable to read MAC", evt_tag_str("file", MACfile));
}
FILE *input = fopen(inputlog, "r");
if(input == NULL)
{
msg_error("[SLOG] ERROR: Unable to open input log", evt_tag_str("file", inputlog));
return 0;
}
// Scan through file ones
guint64 entries = 0;
while(!feof(input))
{
char c = fgetc(input);
if(c == '\n')
{
entries++;
}
}
fclose(input);
msg_info("[SLOG] INFO: Number of lines in file", evt_tag_long("number", entries));
msg_info("[SLOG] INFO: Restoring and verifying log entries", evt_tag_int("buffer size", bufsize));
ret = fileVerify((unsigned char *)key, inputlog, outputlog, MAC, entries, bufsize);
if (ret == 0)
{
msg_error("[SLOG] ERROR: There is a problem with log verification. Please check log manually");
}
return ret;
}
// Return 1 on success, 0 on error
int iterativeMode(char *prevKey, char *prevMAC, char *curMAC, char *inputlog, char *outputlog, int bufsize)
{
char previousKey[KEY_LENGTH];
guint64 previousKeyCounter = 0;
msg_info("[SLOG] INFO: Reading previous key file", evt_tag_str("name", prevKey));
int ret = readKey(previousKey, &previousKeyCounter, prevKey);
if (ret!=1)
{
msg_error("[SLOG] ERROR: Previous key could not be loaded.", evt_tag_str("file", prevKey));
return 0;
}
msg_info("[SLOG] INFO: Reading previous MAC file", evt_tag_str("name", prevMAC));
FILE *previousBigMAC = fopen(prevMAC, "r");
if(previousBigMAC == NULL)
{
msg_error("[SLOG] ERROR: Unable to read previous MAC", evt_tag_str("file", prevMAC));
return 0;
}
unsigned char previousMAC[CMAC_LENGTH];
if (readBigMAC(prevMAC, (char *)previousMAC)==0)
{
msg_warning("[SLOG] WARNING: Unable to read previous MAC", evt_tag_str("file", prevMAC));
}
msg_info("[SLOG] INFO: Reading current MAC file", evt_tag_str("name", curMAC));
FILE *currentBigMAC = fopen(curMAC, "r");
if(currentBigMAC == NULL)
{
msg_error("[SLOG] ERROR: Unable to read current MAC", evt_tag_str("file", curMAC));
return 0;
}
unsigned char currentMAC[CMAC_LENGTH];
if (readBigMAC(curMAC, (char *)currentMAC)==0)
{
msg_warning("[SLOG] WARNING: Unable to read current MAC", evt_tag_str("file", curMAC));
}
FILE *input = fopen(inputlog, "r");
if(input == NULL)
{
msg_error("[SLOG] ERROR: Unable to open input log", evt_tag_str("file", inputlog));
return 0;
}
// Scan through file ones
guint64 entries = 0;
while(!feof(input))
{
char c = fgetc(input);
if(c == '\n')
{
entries++;
}
}
fclose(input);
msg_info("[SLOG] INFO: Number of lines in file", evt_tag_long("number", entries));
msg_info("[SLOG] INFO: Restoring and verifying log entries", evt_tag_int("buffer size", bufSize));
ret = iterativeFileVerify(previousMAC, (unsigned char *)previousKey, inputlog, currentMAC, outputlog, entries,
bufsize, previousKeyCounter);
if (ret == 0)
{
msg_error("[SLOG] ERROR: There is a problem with log verification. Please check log manually");
}
return ret;
}
// Return 0 for success and 1 on error
int main(int argc, char *argv[])
{
int index = 0;
SLogOptions options[] =
{
{ "iterative", 'i', "Iterative verification", NULL, NULL },
{ "key-file", 'k', "Initial host key file", "FILE", NULL },
{ "mac-file", 'm', "Current MAC file", "FILE", NULL },
{ "prev-key-file", 'p', "Previous host key file in iterative mode", "FILE", NULL },
{ "prev-mac-file", 'r', "Previous MAC file in iterative mode", "FILE", NULL },
{ NULL }
};
GOptionEntry entries[] =
{
{ options[0].longname, options[0].shortname, 0, G_OPTION_ARG_NONE, &iterative, options[0].description, NULL },
{ options[1].longname, options[1].shortname, 0, G_OPTION_ARG_CALLBACK, &validFileNameArg, options[1].description, options[1].type },
{ options[2].longname, options[2].shortname, 0, G_OPTION_ARG_CALLBACK, &validFileNameArg, options[2].description, options[2].type },
{ options[3].longname, options[3].shortname, 0, G_OPTION_ARG_CALLBACK, &validFileNameArg, options[3].description, options[3].type },
{ options[4].longname, options[4].shortname, 0, G_OPTION_ARG_CALLBACK, &validFileNameArg, options[4].description, options[4].type },
{ NULL }
};
GError *error = NULL;
GOptionContext *context = g_option_context_new("INPUTLOG OUTPUTLOG [COUNTER] - Log archive verification");
GOptionGroup *group = g_option_group_new("Basic options", "Basic log archive verification options", "basic", &options,
NULL);
g_option_group_add_entries(group, entries);
g_option_context_set_main_group(context, group);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
GString *errorMsg = g_string_new(error->message);
return slog_usage(context, group, errorMsg);
}
if(argc < 2 || argc > 4)
{
return slog_usage(context, group, NULL);
}
// Initialize internal messaging
msg_init(TRUE);
// Assign option arguments
index = 1;
hostKey = options[index].arg;
index++;
curMacFile = options[index].arg;
index++;
prevHostKey = options[index].arg;
index++;
prevMacFile = options[index].arg;
index++;
// Input and output file arguments
index = 1;
inputLog = argv[index];
index++;
if(!g_file_test(inputLog, G_FILE_TEST_IS_REGULAR))
{
GString *errorMsg = g_string_new(FILE_ERROR);
g_string_append(errorMsg, inputLog);
return slog_usage(context, group, errorMsg);
}
outputLog = argv[index];
index++;
if(outputLog == NULL)
{
return slog_usage(context, group, NULL);
}
// Buffer size arguments if applicable
if (argc == 4)
{
bufSize = atoi(argv[index]);
if(bufSize <= MIN_BUF_SIZE || bufSize > MAX_BUF_SIZE)
{
msg_error("[SLOG] ERROR: Invalid buffer size.", evt_tag_int("Size", bufSize),
evt_tag_int("Minimum buffer size", MIN_BUF_SIZE), evt_tag_int("Maximum buffer size", MAX_BUF_SIZE));
g_option_context_free(context);
return 1;
}
}
int ret = 0;
if (iterative)
{
if (prevHostKey == NULL || prevMacFile == NULL || curMacFile == NULL)
{
printf("%s", g_option_context_get_help(context, TRUE, NULL));
g_option_context_free(context);
return 1;
}
ret = 1 - iterativeMode(prevHostKey, prevMacFile, curMacFile, inputLog, outputLog, bufSize);
}
else
{
if (hostKey == NULL || curMacFile == NULL)
{
printf("%s", g_option_context_get_help(context, TRUE, NULL));
g_option_context_free(context);
return 1;
}
ret = 1 - normalMode(hostKey, curMacFile, inputLog, outputLog, bufSize);
}
// Release messaging resources
msg_deinit();
return ret;
}
|