| 1 |
/* lmsig_ksi-ls12.c |
| 2 |
* |
| 3 |
* An implementation of the sigprov interface for KSI-LS12. |
| 4 |
* |
| 5 |
* Copyright 2013-2017 Adiscon GmbH and Guardtime, Inc. |
| 6 |
* |
| 7 |
* This file is part of the rsyslog runtime library. |
| 8 |
* |
| 9 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
* you may not use this file except in compliance with the License. |
| 11 |
* You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* -or- |
| 15 |
* see COPYING.ASL20 in the source distribution |
| 16 |
* |
| 17 |
* Unless required by applicable law or agreed to in writing, software |
| 18 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 19 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 |
* See the License for the specific language governing permissions and |
| 21 |
* limitations under the License. |
| 22 |
*/ |
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "rsyslog.h" |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "module-template.h" |
| 31 |
#include "glbl.h" |
| 32 |
#include "errmsg.h" |
| 33 |
#include "sigprov.h" |
| 34 |
#include "lmsig_ksi-ls12.h" |
| 35 |
|
| 36 |
MODULE_TYPE_LIB |
| 37 |
MODULE_TYPE_NOKEEP; |
| 38 |
|
| 39 |
/* static data */ |
| 40 |
DEFobjStaticHelpers; |
| 41 |
DEFobjCurrIf(glbl) |
| 42 |
|
| 43 |
/* tables for interfacing with the v6 config system */ |
| 44 |
static struct cnfparamdescr cnfpdescr[] = { |
| 45 |
{"sig.hashfunction", eCmdHdlrGetWord, 0}, |
| 46 |
{"sig.aggregator.url", eCmdHdlrGetWord, CNFPARAM_REQUIRED}, |
| 47 |
{"sig.aggregator.user", eCmdHdlrGetWord, 0}, |
| 48 |
{"sig.aggregator.key", eCmdHdlrGetWord, 0}, |
| 49 |
{"sig.aggregator.hmacAlg", eCmdHdlrGetWord, 0}, |
| 50 |
{"sig.block.levelLimit", eCmdHdlrSize, CNFPARAM_REQUIRED}, |
| 51 |
{"sig.block.timeLimit", eCmdHdlrInt, 0}, |
| 52 |
{"sig.block.signtimeout", eCmdHdlrInt, 0}, |
| 53 |
{"sig.confinterval", eCmdHdlrInt, 0}, |
| 54 |
{"sig.keeprecordhashes", eCmdHdlrBinary, 0}, |
| 55 |
{"sig.keeptreehashes", eCmdHdlrBinary, 0}, |
| 56 |
{"sig.fileformat", eCmdHdlrString, 0}, |
| 57 |
{"sig.syncmode", eCmdHdlrString, 0}, |
| 58 |
{"sig.randomsource", eCmdHdlrString, 0}, |
| 59 |
{"sig.debugfile", eCmdHdlrString, 0}, |
| 60 |
{"sig.debuglevel", eCmdHdlrInt, 0}, |
| 61 |
{"dirowner", eCmdHdlrUID, 0}, /* legacy: dirowner */ |
| 62 |
{"dirownernum", eCmdHdlrInt, 0}, /* legacy: dirownernum */ |
| 63 |
{"dirgroup", eCmdHdlrGID, 0}, /* legacy: dirgroup */ |
| 64 |
{"dirgroupnum", eCmdHdlrInt, 0}, /* legacy: dirgroupnum */ |
| 65 |
{"fileowner", eCmdHdlrUID, 0}, /* legacy: fileowner */ |
| 66 |
{"fileownernum", eCmdHdlrInt, 0}, /* legacy: fileownernum */ |
| 67 |
{"filegroup", eCmdHdlrGID, 0}, /* legacy: filegroup */ |
| 68 |
{"filegroupnum", eCmdHdlrInt, 0}, /* legacy: filegroupnum */ |
| 69 |
{"dircreatemode", eCmdHdlrFileCreateMode, 0}, /* legacy: dircreatemode */ |
| 70 |
{"filecreatemode", eCmdHdlrFileCreateMode, 0} /* legacy: filecreatemode */ |
| 71 |
}; |
| 72 |
static struct cnfparamblk pblk = {CNFPARAMBLK_VERSION, sizeof(cnfpdescr) / sizeof(struct cnfparamdescr), cnfpdescr}; |
| 73 |
|
| 74 |
|
| 75 |
static void errfunc(__attribute__((unused)) void *usrptr, uchar *emsg) { |
| 76 |
LogError(0, RS_RET_SIGPROV_ERR, |
| 77 |
"KSI Signature Provider" |
| 78 |
"Error: %s", |
| 79 |
emsg); |
| 80 |
} |
| 81 |
|
| 82 |
static void logfunc(__attribute__((unused)) void *usrptr, uchar *emsg) { |
| 83 |
LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO, "KSI/LS12 Signature Provider: %s", emsg); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* Standard-Constructor |
| 88 |
*/ |
| 89 |
BEGINobjConstruct(lmsig_ksi_ls12) |
| 90 |
pThis->ctx = rsksiCtxNew(); |
| 91 |
rsksisetErrFunc(pThis->ctx, errfunc, NULL); |
| 92 |
rsksisetLogFunc(pThis->ctx, logfunc, NULL); |
| 93 |
ENDobjConstruct(lmsig_ksi_ls12) |
| 94 |
|
| 95 |
|
| 96 |
/* destructor for the lmsig_ksi object */ |
| 97 |
BEGINobjDestruct(lmsig_ksi_ls12) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 98 |
CODESTARTobjDestruct(lmsig_ksi_ls12); |
| 99 |
rsksiCtxDel(pThis->ctx); |
| 100 |
ENDobjDestruct(lmsig_ksi_ls12) |
| 101 |
|
| 102 |
/* apply all params from param block to us. This must be called |
| 103 |
* after construction, but before the OnFileOpen() entry point. |
| 104 |
* Defaults are expected to have been set during construction. |
| 105 |
*/ |
| 106 |
static rsRetVal SetCnfParam(void *pT, struct nvlst *lst) { |
| 107 |
char *ag_uri = NULL, *ag_loginid = NULL, *ag_key = NULL; |
| 108 |
char *hash = NULL, *hmac = NULL; |
| 109 |
lmsig_ksi_ls12_t *pThis = (lmsig_ksi_ls12_t *)pT; |
| 110 |
int i; |
| 111 |
uchar *cstr; |
| 112 |
struct cnfparamvals *pvals; |
| 113 |
DEFiRet; |
| 114 |
pvals = nvlstGetParams(lst, &pblk, NULL); |
| 115 |
if (pvals == NULL) { |
| 116 |
LogError(0, RS_RET_ERR, "Failed to load configuration - signing disabled"); |
| 117 |
pThis->ctx->disabled = true; |
| 118 |
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); |
| 119 |
} |
| 120 |
if (Debug) { |
| 121 |
dbgprintf("sig param blk in lmsig_ksi:\n"); |
| 122 |
cnfparamsPrint(&pblk, pvals); |
| 123 |
} |
| 124 |
|
| 125 |
for (i = 0; i < pblk.nParams; ++i) { |
| 126 |
if (!pvals[i].bUsed) continue; |
| 127 |
if (!strcmp(pblk.descr[i].name, "sig.hashfunction")) { |
| 128 |
hash = (char *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 129 |
} else if (!strcmp(pblk.descr[i].name, "sig.aggregator.url")) { |
| 130 |
ag_uri = es_str2cstr(pvals[i].val.d.estr, NULL); |
| 131 |
} else if (!strcmp(pblk.descr[i].name, "sig.aggregator.user")) { |
| 132 |
ag_loginid = es_str2cstr(pvals[i].val.d.estr, NULL); |
| 133 |
} else if (!strcmp(pblk.descr[i].name, "sig.aggregator.key")) { |
| 134 |
ag_key = es_str2cstr(pvals[i].val.d.estr, NULL); |
| 135 |
} else if (!strcmp(pblk.descr[i].name, "sig.aggregator.hmacAlg")) { |
| 136 |
hmac = (char *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 137 |
} else if (!strcmp(pblk.descr[i].name, "sig.block.levelLimit")) { |
| 138 |
if (pvals[i].val.d.n < 2) { |
| 139 |
LogError(0, RS_RET_ERR, |
| 140 |
"sig.block.levelLimit " |
| 141 |
"%llu invalid - signing disabled", |
| 142 |
pvals[i].val.d.n); |
| 143 |
pThis->ctx->disabled = true; |
| 144 |
} else { |
| 145 |
rsksiSetBlockLevelLimit(pThis->ctx, pvals[i].val.d.n); |
| 146 |
} |
| 147 |
} else if (!strcmp(pblk.descr[i].name, "sig.block.timeLimit")) { |
| 148 |
if (pvals[i].val.d.n < 0) { |
| 149 |
LogError(0, RS_RET_ERR, |
| 150 |
"sig.block.timeLimit " |
| 151 |
"%llu invalid - signing disabled", |
| 152 |
pvals[i].val.d.n); |
| 153 |
pThis->ctx->disabled = true; |
| 154 |
} else { |
| 155 |
rsksiSetBlockTimeLimit(pThis->ctx, pvals[i].val.d.n); |
| 156 |
} |
| 157 |
} else if (!strcmp(pblk.descr[i].name, "sig.confinterval")) { |
| 158 |
if (pvals[i].val.d.n < 0) { |
| 159 |
LogError(0, RS_RET_ERR, |
| 160 |
"sig.confinterval " |
| 161 |
"%llu invalid - signing disabled", |
| 162 |
pvals[i].val.d.n); |
| 163 |
pThis->ctx->disabled = true; |
| 164 |
} else { |
| 165 |
rsksiSetConfInterval(pThis->ctx, pvals[i].val.d.n); |
| 166 |
} |
| 167 |
} else if (!strcmp(pblk.descr[i].name, "sig.keeprecordhashes")) { |
| 168 |
rsksiSetKeepRecordHashes(pThis->ctx, pvals[i].val.d.n); |
| 169 |
} else if (!strcmp(pblk.descr[i].name, "sig.block.signtimeout")) { |
| 170 |
if (pvals[i].val.d.n < 0) { |
| 171 |
LogError(0, RS_RET_ERR, |
| 172 |
"sig.block.signtimeout " |
| 173 |
"%llu invalid - signing disabled", |
| 174 |
pvals[i].val.d.n); |
| 175 |
pThis->ctx->disabled = true; |
| 176 |
} else { |
| 177 |
rsksiSetBlockSigTimeout(pThis->ctx, pvals[i].val.d.n); |
| 178 |
} |
| 179 |
} else if (!strcmp(pblk.descr[i].name, "sig.keeptreehashes")) { |
| 180 |
rsksiSetKeepTreeHashes(pThis->ctx, pvals[i].val.d.n); |
| 181 |
} else if (!strcmp(pblk.descr[i].name, "sig.syncmode")) { |
| 182 |
cstr = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 183 |
if (!strcasecmp((char *)cstr, "sync")) |
| 184 |
rsksiSetSyncMode(pThis->ctx, LOGSIG_SYNCHRONOUS); |
| 185 |
else if (!strcasecmp((char *)cstr, "async")) |
| 186 |
rsksiSetSyncMode(pThis->ctx, LOGSIG_ASYNCHRONOUS); |
| 187 |
else |
| 188 |
LogError(0, RS_RET_ERR, "sig.syncmode '%s' unknown - using default", cstr); |
| 189 |
free(cstr); |
| 190 |
} else if (!strcmp(pblk.descr[i].name, "sig.randomsource")) { |
| 191 |
cstr = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 192 |
rsksiSetRandomSource(pThis->ctx, (char *)cstr); |
| 193 |
free(cstr); |
| 194 |
} else if (!strcmp(pblk.descr[i].name, "sig.debugfile")) { |
| 195 |
cstr = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 196 |
rsksiSetDebugFile(pThis->ctx, (char *)cstr); |
| 197 |
free(cstr); |
| 198 |
} else if (!strcmp(pblk.descr[i].name, "sig.debuglevel")) { |
| 199 |
rsksiSetDebugLevel(pThis->ctx, pvals[i].val.d.n); |
| 200 |
} else if (!strcmp(pblk.descr[i].name, "dirowner")) { |
| 201 |
rsksiSetDirUID(pThis->ctx, pvals[i].val.d.n); |
| 202 |
} else if (!strcmp(pblk.descr[i].name, "dirownernum")) { |
| 203 |
rsksiSetDirUID(pThis->ctx, pvals[i].val.d.n); |
| 204 |
} else if (!strcmp(pblk.descr[i].name, "dirgroup")) { |
| 205 |
rsksiSetDirGID(pThis->ctx, pvals[i].val.d.n); |
| 206 |
} else if (!strcmp(pblk.descr[i].name, "dirgroupnum")) { |
| 207 |
rsksiSetDirGID(pThis->ctx, pvals[i].val.d.n); |
| 208 |
} else if (!strcmp(pblk.descr[i].name, "fileowner")) { |
| 209 |
rsksiSetFileUID(pThis->ctx, pvals[i].val.d.n); |
| 210 |
} else if (!strcmp(pblk.descr[i].name, "fileownernum")) { |
| 211 |
rsksiSetFileUID(pThis->ctx, pvals[i].val.d.n); |
| 212 |
} else if (!strcmp(pblk.descr[i].name, "filegroup")) { |
| 213 |
rsksiSetFileGID(pThis->ctx, pvals[i].val.d.n); |
| 214 |
} else if (!strcmp(pblk.descr[i].name, "filegroupnum")) { |
| 215 |
rsksiSetFileGID(pThis->ctx, pvals[i].val.d.n); |
| 216 |
} else if (!strcmp(pblk.descr[i].name, "dircreatemode")) { |
| 217 |
rsksiSetDirCreateMode(pThis->ctx, pvals[i].val.d.n); |
| 218 |
} else if (!strcmp(pblk.descr[i].name, "filecreatemode")) { |
| 219 |
rsksiSetCreateMode(pThis->ctx, pvals[i].val.d.n); |
| 220 |
} else { |
| 221 |
DBGPRINTF( |
| 222 |
"lmsig_ksi: program error, non-handled " |
| 223 |
"param '%s'\n", |
| 224 |
pblk.descr[i].name); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (rsksiSetHashFunction(pThis->ctx, hash ? hash : (char *)"default") != KSI_OK) { |
| 229 |
ABORT_FINALIZE(RS_RET_KSI_ERR); |
| 230 |
} |
| 231 |
|
| 232 |
if (rsksiSetHmacFunction(pThis->ctx, hmac ? hmac : (char *)"default") != KSI_OK) { |
| 233 |
ABORT_FINALIZE(RS_RET_KSI_ERR); |
| 234 |
} |
| 235 |
|
| 236 |
if (rsksiSetAggregator(pThis->ctx, ag_uri, ag_loginid, ag_key) != KSI_OK) { |
| 237 |
ABORT_FINALIZE(RS_RET_KSI_ERR); |
| 238 |
} |
| 239 |
|
| 240 |
finalize_it: |
| 241 |
free(ag_uri); |
| 242 |
free(ag_loginid); |
| 243 |
free(ag_key); |
| 244 |
free(hash); |
| 245 |
free(hmac); |
| 246 |
|
| 247 |
if (pvals != NULL) cnfparamvalsDestruct(pvals, &pblk); |
| 248 |
RETiRet; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
static rsRetVal OnFileOpen(void *pT, uchar *fn, void *pGF) { |
| 253 |
lmsig_ksi_ls12_t *pThis = (lmsig_ksi_ls12_t *)pT; |
| 254 |
ksifile *pgf = (ksifile *)pGF; |
| 255 |
DEFiRet; |
| 256 |
/* note: if *pgf is set to NULL, this auto-disables GT functions */ |
| 257 |
*pgf = rsksiCtxOpenFile(pThis->ctx, fn); |
| 258 |
sigblkInitKSI(*pgf); |
| 259 |
RETiRet; |
| 260 |
} |
| 261 |
|
| 262 |
/* Note: we assume that the record is terminated by a \n. |
| 263 |
* As of the GuardTime paper, \n is not part of the signed |
| 264 |
* message, so we subtract one from the record size. This |
| 265 |
* may cause issues with non-standard formats, but let's |
| 266 |
* see how things evolve (the verifier will not work in |
| 267 |
* any case when the records are not \n delimited...). |
| 268 |
* rgerhards, 2013-03-17 |
| 269 |
*/ |
| 270 |
static rsRetVal OnRecordWrite(void *pF, uchar *rec, rs_size_t lenRec) { |
| 271 |
DEFiRet; |
| 272 |
DBGPRINTF("lmsig_ksi-ls12: onRecordWrite (%d): %s\n", lenRec - 1, rec); |
| 273 |
sigblkAddRecordKSI(pF, rec, lenRec - 1); |
| 274 |
|
| 275 |
RETiRet; |
| 276 |
} |
| 277 |
|
| 278 |
static rsRetVal OnFileClose(void *pF) { |
| 279 |
DEFiRet; |
| 280 |
DBGPRINTF("lmsig_ksi_ls12: onFileClose\n"); |
| 281 |
rsksifileDestruct(pF); |
| 282 |
|
| 283 |
RETiRet; |
| 284 |
} |
| 285 |
|
| 286 |
BEGINobjQueryInterface(lmsig_ksi_ls12) |
| 287 |
CODESTARTobjQueryInterface(lmsig_ksi_ls12); |
| 288 |
if (pIf->ifVersion != sigprovCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 289 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 290 |
} |
| 291 |
pIf->Construct = (rsRetVal(*)(void *))lmsig_ksi_ls12Construct; |
| 292 |
pIf->SetCnfParam = SetCnfParam; |
| 293 |
pIf->Destruct = (rsRetVal(*)(void *))lmsig_ksi_ls12Destruct; |
| 294 |
pIf->OnFileOpen = OnFileOpen; |
| 295 |
pIf->OnRecordWrite = OnRecordWrite; |
| 296 |
pIf->OnFileClose = OnFileClose; |
| 297 |
finalize_it: |
| 298 |
ENDobjQueryInterface(lmsig_ksi_ls12) |
| 299 |
|
| 300 |
|
| 301 |
BEGINObjClassExit(lmsig_ksi_ls12, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */ |
| 302 |
CODESTARTObjClassExit(lmsig_ksi_ls12); |
| 303 |
/* release objects we no longer need */ |
| 304 |
objRelease(glbl, CORE_COMPONENT); |
| 305 |
ENDObjClassExit(lmsig_ksi_ls12) |
| 306 |
|
| 307 |
|
| 308 |
BEGINObjClassInit(lmsig_ksi_ls12, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 309 |
/* request objects we use */ |
| 310 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 311 |
ENDObjClassInit(lmsig_ksi_ls12) |
| 312 |
|
| 313 |
|
| 314 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 315 |
|
| 316 |
|
| 317 |
BEGINmodExit |
| 318 |
CODESTARTmodExit; |
| 319 |
lmsig_ksi_ls12ClassExit(); |
| 320 |
ENDmodExit |
| 321 |
|
| 322 |
|
| 323 |
BEGINqueryEtryPt |
| 324 |
CODESTARTqueryEtryPt; |
| 325 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 326 |
ENDqueryEtryPt |
| 327 |
|
| 328 |
|
| 329 |
BEGINmodInit() |
| 330 |
CODESTARTmodInit; |
| 331 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; |
| 332 |
CHKiRet(lmsig_ksi_ls12ClassInit(pModInfo)); |
| 333 |
ENDmodInit |