Debian Codegraph @

runtime/lmcry_ossl.c

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