| 1 |
/* lmcry_gcry.c |
| 2 |
* |
| 3 |
* An implementation of the cryprov interface for libgcrypt. |
| 4 |
* |
| 5 |
* Copyright 2013-2017 Rainer Gerhards and Adiscon GmbH. |
| 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 |
#include <errno.h> |
| 30 |
|
| 31 |
#include "module-template.h" |
| 32 |
#include "glbl.h" |
| 33 |
#include "errmsg.h" |
| 34 |
#include "cryprov.h" |
| 35 |
#include "parserif.h" |
| 36 |
#include "libgcry.h" |
| 37 |
#include "lmcry_gcry.h" |
| 38 |
#include "libcry_common.h" |
| 39 |
|
| 40 |
MODULE_TYPE_LIB |
| 41 |
MODULE_TYPE_NOKEEP; |
| 42 |
|
| 43 |
/* static data */ |
| 44 |
DEFobjStaticHelpers; |
| 45 |
DEFobjCurrIf(glbl) |
| 46 |
|
| 47 |
/* tables for interfacing with the v6 config system */ |
| 48 |
static struct cnfparamdescr cnfpdescrRegular[] = {{"cry.key", eCmdHdlrGetWord, 0}, |
| 49 |
{"cry.keyfile", eCmdHdlrGetWord, 0}, |
| 50 |
{"cry.keyprogram", eCmdHdlrGetWord, 0}, |
| 51 |
{"cry.mode", eCmdHdlrGetWord, 0}, /* CBC, ECB, etc */ |
| 52 |
{"cry.algo", eCmdHdlrGetWord, 0}}; |
| 53 |
static struct cnfparamblk pblkRegular = {CNFPARAMBLK_VERSION, sizeof(cnfpdescrRegular) / sizeof(struct cnfparamdescr), |
| 54 |
cnfpdescrRegular}; |
| 55 |
|
| 56 |
static struct cnfparamdescr cnfpdescrQueue[] = {{"queue.cry.key", eCmdHdlrGetWord, 0}, |
| 57 |
{"queue.cry.keyfile", eCmdHdlrGetWord, 0}, |
| 58 |
{"queue.cry.keyprogram", eCmdHdlrGetWord, 0}, |
| 59 |
{"queue.cry.mode", eCmdHdlrGetWord, 0}, /* CBC, ECB, etc */ |
| 60 |
{"queue.cry.algo", eCmdHdlrGetWord, 0}}; |
| 61 |
static struct cnfparamblk pblkQueue = {CNFPARAMBLK_VERSION, sizeof(cnfpdescrQueue) / sizeof(struct cnfparamdescr), |
| 62 |
cnfpdescrQueue}; |
| 63 |
|
| 64 |
|
| 65 |
#if 0 |
| 66 |
static void |
| 67 |
errfunc(__attribute__((unused)) void *usrptr, uchar *emsg) |
| 68 |
{ |
| 69 |
LogError(0, RS_RET_CRYPROV_ERR, "Crypto Provider" |
| 70 |
"Error: %s - disabling encryption", emsg); |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
/* Standard-Constructor |
| 75 |
*/ |
| 76 |
BEGINobjConstruct(lmcry_gcry) |
| 77 |
CHKmalloc(pThis->ctx = gcryCtxNew()); |
| 78 |
finalize_it: |
| 79 |
ENDobjConstruct(lmcry_gcry) |
| 80 |
|
| 81 |
|
| 82 |
/* destructor for the lmcry_gcry object */ |
| 83 |
BEGINobjDestruct(lmcry_gcry) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 84 |
CODESTARTobjDestruct(lmcry_gcry); |
| 85 |
rsgcryCtxDel(pThis->ctx); |
| 86 |
ENDobjDestruct(lmcry_gcry) |
| 87 |
|
| 88 |
|
| 89 |
/* apply all params from param block to us. This must be called |
| 90 |
* after construction, but before the OnFileOpen() entry point. |
| 91 |
* Defaults are expected to have been set during construction. |
| 92 |
*/ |
| 93 |
static rsRetVal SetCnfParam(void *pT, struct nvlst *lst, int paramType) { |
| 94 |
lmcry_gcry_t *pThis = (lmcry_gcry_t *)pT; |
| 95 |
int i, r; |
| 96 |
unsigned keylen = 0; |
| 97 |
uchar *key = NULL; |
| 98 |
uchar *keyfile = NULL; |
| 99 |
uchar *keyprogram = NULL; |
| 100 |
uchar *algo = NULL; |
| 101 |
uchar *mode = NULL; |
| 102 |
int nKeys; /* number of keys (actually methods) specified */ |
| 103 |
struct cnfparamvals *pvals; |
| 104 |
struct cnfparamblk *pblk; |
| 105 |
DEFiRet; |
| 106 |
|
| 107 |
pblk = (paramType == CRYPROV_PARAMTYPE_REGULAR) ? &pblkRegular : &pblkQueue; |
| 108 |
nKeys = 0; |
| 109 |
pvals = nvlstGetParams(lst, pblk, NULL); |
| 110 |
if (pvals == NULL) { |
| 111 |
parser_errmsg("error crypto provider gcryconfig parameters]"); |
| 112 |
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); |
| 113 |
} |
| 114 |
if (Debug) { |
| 115 |
dbgprintf("param blk in lmcry_gcry:\n"); |
| 116 |
cnfparamsPrint(pblk, pvals); |
| 117 |
} |
| 118 |
|
| 119 |
for (i = 0; i < pblk->nParams; ++i) { |
| 120 |
if (!pvals[i].bUsed) continue; |
| 121 |
if (!strcmp(pblk->descr[i].name, "cry.key") || !strcmp(pblk->descr[i].name, "queue.cry.key")) { |
| 122 |
key = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 123 |
++nKeys; |
| 124 |
} else if (!strcmp(pblk->descr[i].name, "cry.keyfile") || !strcmp(pblk->descr[i].name, "queue.cry.keyfile")) { |
| 125 |
keyfile = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 126 |
++nKeys; |
| 127 |
} else if (!strcmp(pblk->descr[i].name, "cry.keyprogram") || |
| 128 |
!strcmp(pblk->descr[i].name, "queue.cry.keyprogram")) { |
| 129 |
keyprogram = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 130 |
++nKeys; |
| 131 |
} else if (!strcmp(pblk->descr[i].name, "cry.mode") || !strcmp(pblk->descr[i].name, "queue.cry.mode")) { |
| 132 |
mode = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 133 |
} else if (!strcmp(pblk->descr[i].name, "cry.algo") || !strcmp(pblk->descr[i].name, "queue.cry.algo")) { |
| 134 |
algo = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 135 |
} else { |
| 136 |
DBGPRINTF( |
| 137 |
"lmcry_gcry: program error, non-handled " |
| 138 |
"param '%s'\n", |
| 139 |
pblk->descr[i].name); |
| 140 |
} |
| 141 |
} |
| 142 |
if (algo != NULL) { |
| 143 |
iRet = rsgcrySetAlgo(pThis->ctx, algo); |
| 144 |
if (iRet != RS_RET_OK) { |
| 145 |
LogError(0, iRet, "cry.algo '%s' is not know/supported", algo); |
| 146 |
FINALIZE; |
| 147 |
} |
| 148 |
} |
| 149 |
if (mode != NULL) { |
| 150 |
iRet = rsgcrySetMode(pThis->ctx, mode); |
| 151 |
if (iRet != RS_RET_OK) { |
| 152 |
LogError(0, iRet, "cry.mode '%s' is not know/supported", mode); |
| 153 |
FINALIZE; |
| 154 |
} |
| 155 |
} |
| 156 |
/* note: key must be set AFTER algo/mode is set (as it depends on them) */ |
| 157 |
if (nKeys != 1) { |
| 158 |
LogError(0, RS_RET_INVALID_PARAMS, |
| 159 |
"excactly one of the following " |
| 160 |
"parameters can be specified: cry.key, cry.keyfile, cry.keyprogram\n"); |
| 161 |
ABORT_FINALIZE(RS_RET_INVALID_PARAMS); |
| 162 |
} |
| 163 |
if (key != NULL) { |
| 164 |
LogError(0, RS_RET_ERR, |
| 165 |
"Note: specifying an actual key directly from the " |
| 166 |
"config file is highly insecure - DO NOT USE FOR PRODUCTION"); |
| 167 |
keylen = strlen((char *)key); |
| 168 |
} |
| 169 |
if (keyfile != NULL) { |
| 170 |
r = cryGetKeyFromFile((char *)keyfile, (char **)&key, &keylen); |
| 171 |
if (r != 0) { |
| 172 |
LogError(errno, RS_RET_ERR, "error reading keyfile %s", keyfile); |
| 173 |
ABORT_FINALIZE(RS_RET_INVALID_PARAMS); |
| 174 |
} |
| 175 |
} |
| 176 |
if (keyprogram != NULL) { |
| 177 |
r = cryGetKeyFromProg((char *)keyprogram, (char **)&key, &keylen); |
| 178 |
if (r != 0) { |
| 179 |
LogError(0, RS_RET_ERR, "error %d obtaining key from program %s\n", r, keyprogram); |
| 180 |
ABORT_FINALIZE(RS_RET_INVALID_PARAMS); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* if we reach this point, we have a valid key */ |
| 185 |
r = rsgcrySetKey(pThis->ctx, key, keylen); |
| 186 |
if (r > 0) { |
| 187 |
LogError(0, RS_RET_INVALID_PARAMS, |
| 188 |
"Key length %d expected, but " |
| 189 |
"key of length %d given", |
| 190 |
r, keylen); |
| 191 |
ABORT_FINALIZE(RS_RET_INVALID_PARAMS); |
| 192 |
} |
| 193 |
|
| 194 |
finalize_it: |
| 195 |
free(key); |
| 196 |
free(keyfile); |
| 197 |
free(algo); |
| 198 |
free(keyprogram); |
| 199 |
free(mode); |
| 200 |
if (pvals != NULL) cnfparamvalsDestruct(pvals, pblk); |
| 201 |
RETiRet; |
| 202 |
} |
| 203 |
|
| 204 |
static void SetDeleteOnClose(void *pF, int val) { |
| 205 |
gcryfileSetDeleteOnClose(pF, val); |
| 206 |
} |
| 207 |
|
| 208 |
static rsRetVal GetBytesLeftInBlock(void *pF, ssize_t *left) { |
| 209 |
return gcryfileGetBytesLeftInBlock((gcryfile)pF, left); |
| 210 |
} |
| 211 |
|
| 212 |
static rsRetVal DeleteStateFiles(uchar *logfn) { |
| 213 |
return gcryfileDeleteState(logfn); |
| 214 |
} |
| 215 |
|
| 216 |
static rsRetVal OnFileOpen(void *pT, uchar *fn, void *pGF, char openMode) { |
| 217 |
lmcry_gcry_t *pThis = (lmcry_gcry_t *)pT; |
| 218 |
gcryfile *pgf = (gcryfile *)pGF; |
| 219 |
DEFiRet; |
| 220 |
DBGPRINTF("lmcry_gcry: open file '%s', mode '%c'\n", fn, openMode); |
| 221 |
|
| 222 |
iRet = rsgcryInitCrypt(pThis->ctx, pgf, fn, openMode); |
| 223 |
if (iRet != RS_RET_OK) { |
| 224 |
LogError(0, iRet, |
| 225 |
"Encryption Provider" |
| 226 |
"Error: cannot open .encinfo file - disabling log file"); |
| 227 |
} |
| 228 |
RETiRet; |
| 229 |
} |
| 230 |
|
| 231 |
static rsRetVal Decrypt(void *pF, uchar *rec, size_t *lenRec) { |
| 232 |
DEFiRet; |
| 233 |
iRet = rsgcryDecrypt(pF, rec, lenRec); |
| 234 |
|
| 235 |
RETiRet; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
static rsRetVal Encrypt(void *pF, uchar *rec, size_t *lenRec) { |
| 240 |
DEFiRet; |
| 241 |
iRet = rsgcryEncrypt(pF, rec, lenRec); |
| 242 |
|
| 243 |
RETiRet; |
| 244 |
} |
| 245 |
|
| 246 |
static rsRetVal OnFileClose(void *pF, off64_t offsLogfile) { |
| 247 |
DEFiRet; |
| 248 |
gcryfileDestruct(pF, offsLogfile); |
| 249 |
|
| 250 |
RETiRet; |
| 251 |
} |
| 252 |
|
| 253 |
BEGINobjQueryInterface(lmcry_gcry) |
| 254 |
CODESTARTobjQueryInterface(lmcry_gcry); |
| 255 |
if (pIf->ifVersion != cryprovCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 256 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 257 |
} |
| 258 |
pIf->Construct = (rsRetVal(*)(void *))lmcry_gcryConstruct; |
| 259 |
pIf->SetCnfParam = SetCnfParam; |
| 260 |
pIf->SetDeleteOnClose = SetDeleteOnClose; |
| 261 |
pIf->Destruct = (rsRetVal(*)(void *))lmcry_gcryDestruct; |
| 262 |
pIf->OnFileOpen = OnFileOpen; |
| 263 |
pIf->Encrypt = Encrypt; |
| 264 |
pIf->Decrypt = Decrypt; |
| 265 |
pIf->OnFileClose = OnFileClose; |
| 266 |
pIf->DeleteStateFiles = DeleteStateFiles; |
| 267 |
pIf->GetBytesLeftInBlock = GetBytesLeftInBlock; |
| 268 |
finalize_it: |
| 269 |
ENDobjQueryInterface(lmcry_gcry) |
| 270 |
|
| 271 |
|
| 272 |
BEGINObjClassExit(lmcry_gcry, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */ |
| 273 |
CODESTARTObjClassExit(lmcry_gcry); |
| 274 |
/* release objects we no longer need */ |
| 275 |
objRelease(glbl, CORE_COMPONENT); |
| 276 |
|
| 277 |
rsgcryExit(); |
| 278 |
ENDObjClassExit(lmcry_gcry) |
| 279 |
|
| 280 |
|
| 281 |
BEGINObjClassInit(lmcry_gcry, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 282 |
/* request objects we use */ |
| 283 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 284 |
|
| 285 |
if (rsgcryInit() != 0) { |
| 286 |
LogError(0, RS_RET_CRYPROV_ERR, |
| 287 |
"error initializing " |
| 288 |
"crypto provider - cannot encrypt"); |
| 289 |
ABORT_FINALIZE(RS_RET_CRYPROV_ERR); |
| 290 |
} |
| 291 |
ENDObjClassInit(lmcry_gcry) |
| 292 |
|
| 293 |
|
| 294 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 295 |
|
| 296 |
|
| 297 |
BEGINmodExit |
| 298 |
CODESTARTmodExit; |
| 299 |
lmcry_gcryClassExit(); |
| 300 |
ENDmodExit |
| 301 |
|
| 302 |
|
| 303 |
BEGINqueryEtryPt |
| 304 |
CODESTARTqueryEtryPt; |
| 305 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 306 |
ENDqueryEtryPt |
| 307 |
|
| 308 |
|
| 309 |
BEGINmodInit() |
| 310 |
CODESTARTmodInit; |
| 311 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 312 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 313 |
CHKiRet(lmcry_gcryClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 314 |
ENDmodInit |