| 1 |
/* lib_ksils12.h - rsyslog's KSI-LS12 support library |
| 2 |
* |
| 3 |
* Copyright 2013-2017 Adiscon GmbH and Guardtime, Inc. |
| 4 |
* |
| 5 |
* This file is part of rsyslog. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* -or- |
| 13 |
* see COPYING.ASL20 in the source distribution |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
*/ |
| 21 |
#ifndef INCLUDED_KSILS12_H |
| 22 |
#define INCLUDED_KSILS12_H |
| 23 |
#include <ksi/ksi.h> |
| 24 |
|
| 25 |
#include "lib_ksi_queue.h" |
| 26 |
|
| 27 |
#define MAX_ROOTS 64 |
| 28 |
|
| 29 |
/* Flags and record types for TLV handling */ |
| 30 |
#define RSGT_FLAG_NONCRIT 0x20 |
| 31 |
#define RSGT_FLAG_FORWARD 0x40 |
| 32 |
#define RSGT_TYPE_MASK 0x1f |
| 33 |
#define RSGT_FLAG_TLV16 0x80 |
| 34 |
|
| 35 |
/* check return state of operation and abort, if non-OK */ |
| 36 |
#define CHKr(code) \ |
| 37 |
if ((r = code) != 0) goto done |
| 38 |
|
| 39 |
/* check the return value of a ksi api call and log a message in case of error */ |
| 40 |
#define CHECK_KSI_API(code, context, msg) \ |
| 41 |
if ((res = code) != 0) do { \ |
| 42 |
reportKSIAPIErr(context, NULL, msg, res); \ |
| 43 |
goto cleanup; \ |
| 44 |
} while (0) |
| 45 |
|
| 46 |
|
| 47 |
typedef enum LOGSIG_SyncMode_en { |
| 48 |
/** The block hashes and ksi signatures in one file */ |
| 49 |
LOGSIG_ASYNCHRONOUS = 0x00, |
| 50 |
/** The block hashes and ksi signatures split into separate files */ |
| 51 |
LOGSIG_SYNCHRONOUS = 0x01 |
| 52 |
} LOGSIG_SyncMode; |
| 53 |
|
| 54 |
enum { |
| 55 |
/* Signer state assigned before the signer thread is initialized. State remains |
| 56 |
* until thread initialization begins. In case of system failure to create new |
| 57 |
* thread state remains the same. |
| 58 |
*/ |
| 59 |
SIGNER_IDLE = 0x01, |
| 60 |
|
| 61 |
/* Signer state assigned while signer thread initialization is in progress. |
| 62 |
*/ |
| 63 |
SIGNER_INIT = 0x02, |
| 64 |
|
| 65 |
/* Signer state assigned when signer thread is initialized and ready to work. |
| 66 |
*/ |
| 67 |
SIGNER_STARTED = 0x04, |
| 68 |
|
| 69 |
/* Thread state assigned when signer thread is being closed (signer thread returns). |
| 70 |
*/ |
| 71 |
SIGNER_STOPPED = 0x08 |
| 72 |
}; |
| 73 |
|
| 74 |
/* Max number of roots inside the forest. This permits blocks of up to |
| 75 |
* 2^MAX_ROOTS records. We assume that 64 is sufficient for all use |
| 76 |
* cases ;) [and 64 is not really a waste of memory, so we do not even |
| 77 |
* try to work with reallocs and such...] |
| 78 |
*/ |
| 79 |
|
| 80 |
typedef struct rsksictx_s *rsksictx; |
| 81 |
typedef struct ksifile_s *ksifile; |
| 82 |
typedef struct ksierrctx_s ksierrctx_t; |
| 83 |
|
| 84 |
|
| 85 |
/* context for gt calls. This primarily serves as a container for the |
| 86 |
* config settings. The actual file-specific data is kept in ksifile. |
| 87 |
*/ |
| 88 |
struct rsksictx_s { |
| 89 |
KSI_CTX *ksi_ctx; /* libksi's context object */ |
| 90 |
KSI_DataHasher *hasher; |
| 91 |
KSI_HashAlgorithm hashAlg; |
| 92 |
KSI_HashAlgorithm hmacAlg; |
| 93 |
uint8_t bKeepRecordHashes; |
| 94 |
uint8_t bKeepTreeHashes; |
| 95 |
uint64_t confInterval; |
| 96 |
time_t tConfRequested; |
| 97 |
uint64_t blockLevelLimit; |
| 98 |
uint32_t blockTimeLimit; |
| 99 |
uint32_t blockSigTimeout; |
| 100 |
uint32_t effectiveBlockLevelLimit; /* level limit adjusted by gateway settings */ |
| 101 |
uint32_t threadSleepms; |
| 102 |
uint8_t syncMode; |
| 103 |
uid_t fileUID; /* IDs for creation */ |
| 104 |
uid_t dirUID; |
| 105 |
gid_t fileGID; |
| 106 |
gid_t dirGID; |
| 107 |
int fCreateMode; /* mode to use when creating files */ |
| 108 |
int fDirCreateMode; /* mode to use when creating files */ |
| 109 |
char *aggregatorUri; |
| 110 |
char *aggregatorId; |
| 111 |
char *aggregatorKey; |
| 112 |
char *aggregatorEndpoints[KSI_CTX_HA_MAX_SUBSERVICES]; |
| 113 |
int aggregatorEndpointCount; |
| 114 |
char *random_source; |
| 115 |
pthread_mutex_t module_lock; |
| 116 |
pthread_t signer_thread; |
| 117 |
ProtectedQueue *signer_queue; |
| 118 |
#if KSI_SDK_VER_MAJOR == 3 && KSI_SDK_VER_MINOR < 22 |
| 119 |
size_t roundCount; /* Count of signing requests in round. */ |
| 120 |
uint8_t bRoundLock; /* A lock for async. signer. */ |
| 121 |
#endif |
| 122 |
int signer_state; |
| 123 |
uint8_t disabled; /* permits to disable the plugin --> set to 1 */ |
| 124 |
|
| 125 |
ksifile *ksi; /* List of signature files for keeping track of block timeouts. */ |
| 126 |
size_t ksiCapacity; |
| 127 |
size_t ksiCount; |
| 128 |
|
| 129 |
char *debugFileName; |
| 130 |
int debugLevel; |
| 131 |
FILE *debugFile; |
| 132 |
uint64_t max_requests; |
| 133 |
void (*errFunc)(void *, unsigned char *); |
| 134 |
void (*logFunc)(void *, unsigned char *); |
| 135 |
void *usrptr; /* for error function */ |
| 136 |
}; |
| 137 |
|
| 138 |
/* this describes a file, as far as librsksi is concerned */ |
| 139 |
struct ksifile_s { |
| 140 |
/* the following data items are mirrored from rsksictx to |
| 141 |
* increase cache hit ratio (they are frequently accesed). |
| 142 |
*/ |
| 143 |
KSI_HashAlgorithm hashAlg; |
| 144 |
uint8_t bKeepRecordHashes; |
| 145 |
uint8_t bKeepTreeHashes; |
| 146 |
uint64_t blockSizeLimit; |
| 147 |
uint32_t blockTimeLimit; |
| 148 |
/* end mirrored properties */ |
| 149 |
uint8_t disabled; /* permits to disable this file --> set to 1 */ |
| 150 |
uint8_t *IV; /* initial value for blinding masks */ |
| 151 |
unsigned char lastLeaf[KSI_MAX_IMPRINT_LEN]; /* last leaf hash (maybe of previous block) |
| 152 |
--> preserve on term */ |
| 153 |
unsigned char *blockfilename; |
| 154 |
unsigned char *ksifilename; |
| 155 |
unsigned char *statefilename; |
| 156 |
uint64_t nRecords; /* current number of records in current block */ |
| 157 |
uint64_t bInBlk; /* are we currently inside a blk --> need to finish on close */ |
| 158 |
time_t blockStarted; |
| 159 |
int8_t nRoots; |
| 160 |
/* algo engineering: roots structure is split into two arrays |
| 161 |
* in order to improve cache hits. |
| 162 |
*/ |
| 163 |
KSI_DataHash *roots[MAX_ROOTS]; |
| 164 |
/* data members for the associated TLV file */ |
| 165 |
FILE *blockFile; |
| 166 |
FILE *sigFile; /* Note that this may only be closed by signer thread or when signer thread has terminated. */ |
| 167 |
rsksictx ctx; |
| 168 |
}; |
| 169 |
|
| 170 |
/* the following defines the ksistate file record. Currently, this record |
| 171 |
* is fixed, we may change that over time. |
| 172 |
*/ |
| 173 |
struct rsksistatefile { |
| 174 |
char hdr[9]; /* must be "KSISTAT10" */ |
| 175 |
uint8_t hashID; |
| 176 |
uint8_t lenHash; |
| 177 |
/* after that, the hash value is contained within the file */ |
| 178 |
}; |
| 179 |
|
| 180 |
/* error states */ |
| 181 |
#define RSGTE_SUCCESS 0 /* Success state */ |
| 182 |
#define RSGTE_IO 1 /* any kind of io error */ |
| 183 |
#define RSGTE_FMT 2 /* data fromat error */ |
| 184 |
#define RSGTE_INVLTYP 3 /* invalid TLV type record (unexcpected at this point) */ |
| 185 |
#define RSGTE_OOM 4 /* ran out of memory */ |
| 186 |
#define RSGTE_LEN 5 /* error related to length records */ |
| 187 |
#define RSGTE_SIG_EXTEND 6 /* error extending signature */ |
| 188 |
#define RSGTE_INVLD_RECCNT \ |
| 189 |
7 /* mismatch between actual records and records \ |
| 190 |
given in block-sig record */ |
| 191 |
#define RSGTE_INVLHDR 8 /* invalid file header */ |
| 192 |
#define RSGTE_EOF 9 /* specific EOF */ |
| 193 |
#define RSGTE_MISS_REC_HASH 10 /* record hash missing when expected */ |
| 194 |
#define RSGTE_MISS_TREE_HASH 11 /* tree hash missing when expected */ |
| 195 |
#define RSGTE_INVLD_REC_HASH 12 /* invalid record hash (failed verification) */ |
| 196 |
#define RSGTE_INVLD_TREE_HASH 13 /* invalid tree hash (failed verification) */ |
| 197 |
#define RSGTE_INVLD_REC_HASHID 14 /* invalid record hash ID (failed verification) */ |
| 198 |
#define RSGTE_INVLD_TREE_HASHID 15 /* invalid tree hash ID (failed verification) */ |
| 199 |
#define RSGTE_MISS_BLOCKSIG 16 /* block signature record missing when expected */ |
| 200 |
#define RSGTE_INVLD_SIGNATURE 17 /* Signature is invalid (KSI_Signature_verifyDataHash)*/ |
| 201 |
#define RSGTE_TS_CREATEHASH 18 /* error creating HASH (KSI_DataHash_create) */ |
| 202 |
#define RSGTE_TS_DERENCODE 19 /* error DER-Encoding a timestamp */ |
| 203 |
#define RSGTE_HASH_CREATE 20 /* error creating a hash */ |
| 204 |
#define RSGTE_END_OF_SIG 21 /* unexpected end of signature - more log line exist */ |
| 205 |
#define RSGTE_END_OF_LOG 22 /* unexpected end of log file - more signatures exist */ |
| 206 |
#define RSGTE_EXTRACT_HASH 23 /* error extracting hashes for record */ |
| 207 |
#define RSGTE_CONFIG_ERROR 24 /* Configuration error */ |
| 208 |
#define RSGTE_NETWORK_ERROR 25 /* Network error */ |
| 209 |
#define RSGTE_MISS_KSISIG 26 /* KSI signature missing */ |
| 210 |
#define RSGTE_INTERNAL 27 /* Internal error */ |
| 211 |
|
| 212 |
#define getIVLenKSI(bh) (hashOutputLengthOctetsKSI((bh)->hashID)) |
| 213 |
#define rsksiSetBlockLevelLimit(ctx, limit) ((ctx)->blockLevelLimit = (ctx)->effectiveBlockLevelLimit = limit) |
| 214 |
#define rsksiSetBlockTimeLimit(ctx, limit) ((ctx)->blockTimeLimit = limit) |
| 215 |
#define rsksiSetBlockSigTimeout(ctx, val) ((ctx)->blockSigTimeout = val) |
| 216 |
#define rsksiSetConfInterval(ctx, val) ((ctx)->confInterval = val) |
| 217 |
#define rsksiSetKeepRecordHashes(ctx, val) ((ctx)->bKeepRecordHashes = val) |
| 218 |
#define rsksiSetKeepTreeHashes(ctx, val) ((ctx)->bKeepTreeHashes = val) |
| 219 |
#define rsksiSetFileFormat(ctx, val) ((ctx)->fileFormat = val) |
| 220 |
#define rsksiSetSyncMode(ctx, val) ((ctx)->syncMode = val) |
| 221 |
#define rsksiSetRandomSource(ctx, val) ((ctx)->random_source = strdup(val)) |
| 222 |
#define rsksiSetFileUID(ctx, val) ((ctx)->fileUID = val) /* IDs for creation */ |
| 223 |
#define rsksiSetDirUID(ctx, val) ((ctx)->dirUID = val) |
| 224 |
#define rsksiSetFileGID(ctx, val) ((ctx)->fileGID = val) |
| 225 |
#define rsksiSetDirGID(ctx, val) ((ctx)->dirGID = val) |
| 226 |
#define rsksiSetCreateMode(ctx, val) ((ctx)->fCreateMode = val) |
| 227 |
#define rsksiSetDirCreateMode(ctx, val) ((ctx)->fDirCreateMode = val) |
| 228 |
#define rsksiSetDebugLevel(ctx, val) ((ctx)->debugLevel = val) |
| 229 |
|
| 230 |
|
| 231 |
int rsksiSetDebugFile(rsksictx ctx, char *val); |
| 232 |
int rsksiSetAggregator(rsksictx ctx, char *uri, char *loginid, char *key); |
| 233 |
int rsksiSetHashFunction(rsksictx ctx, char *algName); |
| 234 |
int rsksiSetHmacFunction(rsksictx ctx, char *algName); |
| 235 |
int rsksiInitModule(rsksictx ctx); |
| 236 |
rsksictx rsksiCtxNew(void); |
| 237 |
void rsksisetErrFunc(rsksictx ctx, void (*func)(void *, unsigned char *), void *usrptr); |
| 238 |
void rsksisetLogFunc(rsksictx ctx, void (*func)(void *, unsigned char *), void *usrptr); |
| 239 |
void reportKSIAPIErr(rsksictx ctx, ksifile ksi, const char *apiname, int ecode); |
| 240 |
ksifile rsksiCtxOpenFile(rsksictx ctx, unsigned char *logfn); |
| 241 |
int rsksifileDestruct(ksifile ksi); |
| 242 |
void rsksiCtxDel(rsksictx ctx); |
| 243 |
void sigblkInitKSI(ksifile ksi); |
| 244 |
int sigblkAddRecordKSI(ksifile ksi, const unsigned char *rec, const size_t len); |
| 245 |
int sigblkAddLeaf(ksifile ksi, const unsigned char *rec, const size_t len, bool metadata); |
| 246 |
unsigned sigblkCalcLevel(unsigned leaves); |
| 247 |
int sigblkFinishKSI(ksifile ksi); |
| 248 |
int sigblkAddMetadata(ksifile ksi, const char *key, const char *value); |
| 249 |
int sigblkCreateMask(ksifile ksi, KSI_DataHash **m); |
| 250 |
int sigblkCreateHash(ksifile ksi, KSI_DataHash **r, const unsigned char *rec, const size_t len); |
| 251 |
int sigblkHashTwoNodes(ksifile ksi, KSI_DataHash **node, KSI_DataHash *m, KSI_DataHash *r, uint8_t level); |
| 252 |
|
| 253 |
#endif /* #ifndef INCLUDED_KSILS12_H */ |