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
|
/* lmcry_ossl.c
*
* An implementation of the cryprov interface for openssl.
*
* Copyright 2013-2017 Rainer Gerhards and Adiscon GmbH.
*
* This file is part of the rsyslog runtime library.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* -or-
* see COPYING.ASL20 in the source distribution
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "rsyslog.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "module-template.h"
#include "glbl.h"
#include "errmsg.h"
#include "cryprov.h"
#include "parserif.h"
#include "libossl.h"
#include "lmcry_ossl.h"
MODULE_TYPE_LIB
MODULE_TYPE_NOKEEP
/* static data */
DEFobjStaticHelpers
DEFobjCurrIf(glbl)
/* tables for interfacing with the v6 config system */
static struct cnfparamdescr cnfpdescrRegular[] = {
{ "cry.key", eCmdHdlrGetWord, 0 },
{ "cry.keyfile", eCmdHdlrGetWord, 0 },
{ "cry.mode", eCmdHdlrGetWord, 0 },
{ "cry.algo", eCmdHdlrGetWord, 0 }
};
static struct cnfparamblk pblkRegular =
{ CNFPARAMBLK_VERSION,
sizeof(cnfpdescrRegular)/sizeof(struct cnfparamdescr),
cnfpdescrRegular
};
static struct cnfparamdescr cnfpdescrQueue[] = {
{ "queue.cry.key", eCmdHdlrGetWord, 0 },
{ "queue.cry.keyfile", eCmdHdlrGetWord, 0 },
{ "queue.cry.mode", eCmdHdlrGetWord, 0 },
{ "queue.cry.algo", eCmdHdlrGetWord, 0 }
};
static struct cnfparamblk pblkQueue =
{ CNFPARAMBLK_VERSION,
sizeof(cnfpdescrQueue)/sizeof(struct cnfparamdescr),
cnfpdescrQueue
};
/* Standard-Constructor
*/
BEGINobjConstruct(lmcry_ossl)
CHKmalloc(pThis->ctx = osslCtxNew());
finalize_it:
ENDobjConstruct(lmcry_ossl)
/* destructor for the lmcry_ossl object */
BEGINobjDestruct(lmcry_ossl) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDestruct(lmcry_ossl)
rsosslCtxDel(pThis->ctx);
ENDobjDestruct(lmcry_ossl)
/* apply all params from param block to us. This must be called
* after construction, but before the OnFileOpen() entry point.
* Defaults are expected to have been set during construction.
*/
static rsRetVal
SetCnfParam(void *pT, struct nvlst *lst, int paramType)
{
lmcry_ossl_t *pThis = (lmcry_ossl_t*) pT;
int i, r;
unsigned keylen = 0;
uchar *key = NULL;
uchar *keyfile = NULL;
uchar *algomode = NULL;
int nKeys; /* number of keys (actually methods) specified */
struct cnfparamvals *pvals;
struct cnfparamblk *pblk;
DEFiRet;
pblk = (paramType == CRYPROV_PARAMTYPE_REGULAR ) ? &pblkRegular : &pblkQueue;
nKeys = 0;
pvals = nvlstGetParams(lst, pblk, NULL);
if(pvals == NULL) {
parser_errmsg("error crypto provider ossl config parameters");
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS);
}
if(Debug) {
dbgprintf("param blk in lmcry_ossl:\n");
cnfparamsPrint(pblk, pvals);
}
for(i = 0 ; i < pblk->nParams ; ++i) {
if(!pvals[i].bUsed)
continue;
if(!strcmp(pblk->descr[i].name, "cry.key") ||
!strcmp(pblk->descr[i].name, "queue.cry.key")) {
key = (uchar*) es_str2cstr(pvals[i].val.d.estr, NULL);
++nKeys;
} else if(!strcmp(pblk->descr[i].name, "cry.keyfile") ||
!strcmp(pblk->descr[i].name, "queue.cry.keyfile")) {
keyfile = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL);
++nKeys;
} else if(!strcmp(pblk->descr[i].name, "cry.algo") ||
!strcmp(pblk->descr[i].name, "queue.cry.algo")) {
algomode = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL);
} else {
DBGPRINTF("lmcry_ossl: program error, non-handled "
"param '%s'\n", pblk->descr[i].name);
}
}
if (algomode != NULL) {
iRet = rsosslSetAlgoMode(pThis->ctx, algomode);
if(iRet != RS_RET_OK) {
LogError(0, iRet, "cry.algo '%s' is not know/supported", algomode);
FINALIZE;
}
}
/* note: key must be set AFTER algo/mode is set (as it depends on them) */
if(nKeys != 1) {
LogError(0, RS_RET_INVALID_PARAMS, "excactly one of the following "
"parameters can be specified: cry.key, cry.keyfile\n");
ABORT_FINALIZE(RS_RET_INVALID_PARAMS);
}
if(key != NULL) {
LogError(0, RS_RET_ERR, "Note: specifying an actual key directly from the "
"config file is highly insecure - DO NOT USE FOR PRODUCTION");
keylen = strlen((char*)key);
}
if(keyfile != NULL) {
r = osslGetKeyFromFile((char*)keyfile, (char**)&key, &keylen);
if(r != 0) {
LogError(errno, RS_RET_ERR, "error reading keyfile %s",
keyfile);
ABORT_FINALIZE(RS_RET_INVALID_PARAMS);
}
}
/* if we reach this point, we have a valid key */
r = rsosslSetKey(pThis->ctx, key, keylen);
if(r > 0) {
LogError(0, RS_RET_INVALID_PARAMS, "Key length %d expected, but "
"key of length %d given", r, keylen);
ABORT_FINALIZE(RS_RET_INVALID_PARAMS);
}
finalize_it:
free(key);
free(keyfile);
free(algomode);
if(pvals != NULL)
cnfparamvalsDestruct(pvals, pblk);
RETiRet;
}
static void
SetDeleteOnClose(void *pF, int val)
{
osslfileSetDeleteOnClose(pF, val);
}
static rsRetVal
GetBytesLeftInBlock(void *pF, ssize_t *left)
{
return osslfileGetBytesLeftInBlock((osslfile) pF, left);
}
static rsRetVal
DeleteStateFiles(uchar *logfn)
{
return osslfileDeleteState(logfn);
}
static rsRetVal
OnFileOpen(void *pT, uchar *fn, void *pGF, char openMode)
{
lmcry_ossl_t* pThis = (lmcry_ossl_t*)pT;
osslfile* pgf = (osslfile*)pGF;
DEFiRet;
DBGPRINTF("lmcry_ossl: open file '%s', mode '%c'\n", fn, openMode);
iRet = rsosslInitCrypt(pThis->ctx, pgf, fn, openMode);
if (iRet != RS_RET_OK) {
LogError(0, iRet, "Encryption Provider"
"Error: cannot open .encinfo file - disabling log file");
}
RETiRet;
}
static rsRetVal
Decrypt(void *pF, uchar *rec, size_t *lenRec)
{
DEFiRet;
iRet = rsosslDecrypt(pF, rec, lenRec);
RETiRet;
}
static rsRetVal
Encrypt(void *pF, uchar *rec, size_t *lenRec)
{
DEFiRet;
iRet = rsosslEncrypt(pF, rec, lenRec);
RETiRet;
}
static rsRetVal
OnFileClose(void *pF, off64_t offsLogfile)
{
DEFiRet;
osslfileDestruct(pF, offsLogfile);
RETiRet;
}
BEGINobjQueryInterface(lmcry_ossl)
CODESTARTobjQueryInterface(lmcry_ossl)
if(pIf->ifVersion != cryprovCURR_IF_VERSION) {/* check for current version, increment on each change */
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
}
pIf->Construct = (rsRetVal(*)(void*)) lmcry_osslConstruct;
pIf->SetCnfParam = SetCnfParam;
pIf->SetDeleteOnClose = SetDeleteOnClose;
pIf->Destruct = (rsRetVal(*)(void*)) lmcry_osslDestruct;
pIf->OnFileOpen = OnFileOpen;
pIf->Encrypt = Encrypt;
pIf->Decrypt = Decrypt;
pIf->OnFileClose = OnFileClose;
pIf->DeleteStateFiles = DeleteStateFiles;
pIf->GetBytesLeftInBlock = GetBytesLeftInBlock;
finalize_it:
ENDobjQueryInterface(lmcry_ossl)
BEGINObjClassExit(lmcry_ossl, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
CODESTARTObjClassExit(lmcry_ossl)
/* release objects we no longer need */
objRelease(glbl, CORE_COMPONENT);
rsosslExit();
ENDObjClassExit(lmcry_ossl)
BEGINObjClassInit(lmcry_ossl, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */
/* request objects we use */
CHKiRet(objUse(glbl, CORE_COMPONENT));
if (rsosslInit() != 0) {
LogError(0, RS_RET_CRYPROV_ERR, "error initializing "
"ossl crypto provider - cannot encrypt");
ABORT_FINALIZE(RS_RET_CRYPROV_ERR);
}
ENDObjClassInit(lmcry_ossl)
/* --------------- here now comes the plumbing that makes as a library module --------------- */
BEGINmodExit
CODESTARTmodExit
lmcry_osslClassExit();
ENDmodExit
BEGINqueryEtryPt
CODESTARTqueryEtryPt
CODEqueryEtryPt_STD_LIB_QUERIES
ENDqueryEtryPt
BEGINmodInit()
CODESTARTmodInit
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
/* Initialize all classes that are in our module - this includes ourselfs */
CHKiRet(lmcry_osslClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */
ENDmodInit
|