| 1 |
/* libossl.c - rsyslog's openssl based crypto provider |
| 2 |
* |
| 3 |
* |
| 4 |
* We need to store some additional information in support of encryption. |
| 5 |
* For this, we create a side-file, which is named like the actual log |
| 6 |
* file, but with the suffix ".encinfo" appended. It contains the following |
| 7 |
* records: |
| 8 |
* IV:<hex> The initial vector used at block start. Also indicates start |
| 9 |
* start of block. |
| 10 |
* END:<int> The end offset of the block, as uint64_t in decimal notation. |
| 11 |
* This is used during encryption to know when the current |
| 12 |
* encryption block ends. |
| 13 |
* For the current implementation, there must always be an IV record |
| 14 |
* followed by an END record. Each records is LF-terminated. Record |
| 15 |
* types can simply be extended in the future by specifying new |
| 16 |
* types (like "IV") before the colon. |
| 17 |
* To identify a file as rsyslog encryption info file, it must start with |
| 18 |
* the line "FILETYPE:rsyslog-enrcyption-info" |
| 19 |
* There are some size constraints: the recordtype must be 31 bytes at |
| 20 |
* most and the actual value (between : and LF) must be 1023 bytes at most. |
| 21 |
* |
| 22 |
* This file is part of rsyslog. |
| 23 |
* |
| 24 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 |
* you may not use this file except in compliance with the License. |
| 26 |
* You may obtain a copy of the License at |
| 27 |
* |
| 28 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 29 |
* -or- |
| 30 |
* see COPYING.ASL20 in the source distribution |
| 31 |
* |
| 32 |
* Unless required by applicable law or agreed to in writing, software |
| 33 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 34 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 35 |
* See the License for the specific language governing permissions and |
| 36 |
* limitations under the License. |
| 37 |
*/ |
| 38 |
#if HAVE_CONFIG_H |
| 39 |
#include "config.h" |
| 40 |
#endif |
| 41 |
#include <stdio.h> |
| 42 |
#include <sys/stat.h> |
| 43 |
#include <sys/uio.h> |
| 44 |
#include <sys/types.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <fcntl.h> |
| 47 |
#include <errno.h> |
| 48 |
#include <openssl/evp.h> |
| 49 |
#include <string.h> |
| 50 |
|
| 51 |
#include "rsyslog.h" |
| 52 |
#include "srUtils.h" |
| 53 |
#include "debug.h" |
| 54 |
#include "libossl.h" |
| 55 |
#include "libcry_common.h" |
| 56 |
|
| 57 |
#define READBUF_SIZE 4096 /* size of the read buffer */ |
| 58 |
static rsRetVal rsosslBlkBegin(osslfile gf); |
| 59 |
|
| 60 |
/* read a key from a key file |
| 61 |
* @param[out] key - key buffer, must be freed by caller |
| 62 |
* @param[out] keylen - length of buffer |
| 63 |
* @returns 0 if OK, something else otherwise (we do not use |
| 64 |
* iRet as this is also called from non-rsyslog w/o runtime) |
| 65 |
* on error, errno is set and can be queried |
| 66 |
* The key length is limited to 64KiB to prevent DoS. |
| 67 |
* Note well: key is a blob, not a C string (NUL may be present!) |
| 68 |
*/ |
| 69 |
int osslGetKeyFromFile(const char* const fn, char** const key, unsigned* const keylen) { |
| 70 |
struct stat sb; |
| 71 |
int r = -1; |
| 72 |
|
| 73 |
const int fd = open(fn, O_RDONLY); |
| 74 |
if (fd < 0) goto done; |
| 75 |
if (fstat(fd, &sb) == -1) goto done; |
| 76 |
if (sb.st_size > 64 * 1024) { |
| 77 |
errno = EMSGSIZE; |
| 78 |
goto done; |
| 79 |
} |
| 80 |
if (sb.st_size == 0) { |
| 81 |
errno = EINVAL; |
| 82 |
goto done; |
| 83 |
} |
| 84 |
if ((*key = malloc(sb.st_size)) == NULL) goto done; |
| 85 |
if (read(fd, *key, sb.st_size) != sb.st_size) goto done; |
| 86 |
*keylen = sb.st_size; |
| 87 |
r = 0; |
| 88 |
done: |
| 89 |
if (fd >= 0) { |
| 90 |
close(fd); |
| 91 |
} |
| 92 |
return r; |
| 93 |
} |
| 94 |
|
| 95 |
static void addPadding(osslfile pF, uchar* buf, size_t* plen) { |
| 96 |
unsigned i; |
| 97 |
size_t nPad; |
| 98 |
nPad = (pF->blkLength - *plen % pF->blkLength) % pF->blkLength; |
| 99 |
DBGPRINTF("libgcry: addPadding %zd chars, blkLength %zd, mod %zd, pad %zd\n", *plen, pF->blkLength, |
| 100 |
*plen % pF->blkLength, nPad); |
| 101 |
for (i = 0; i < nPad; ++i) buf[(*plen) + i] = 0x00; |
| 102 |
(*plen) += nPad; |
| 103 |
} |
| 104 |
|
| 105 |
static void ATTR_NONNULL() removePadding(uchar* const buf, size_t* const plen) { |
| 106 |
const size_t len = *plen; |
| 107 |
size_t iSrc, iDst; |
| 108 |
|
| 109 |
iSrc = 0; |
| 110 |
/* skip to first NUL */ |
| 111 |
while (iSrc < len && buf[iSrc] == '\0') { |
| 112 |
++iSrc; |
| 113 |
} |
| 114 |
iDst = iSrc; |
| 115 |
|
| 116 |
while (iSrc < len) { |
| 117 |
if (buf[iSrc] != 0x00) buf[iDst++] = buf[iSrc]; |
| 118 |
++iSrc; |
| 119 |
} |
| 120 |
|
| 121 |
*plen = iDst; |
| 122 |
} |
| 123 |
|
| 124 |
static rsRetVal eiWriteRec(osslfile gf, const char* recHdr, size_t lenRecHdr, const char* buf, size_t lenBuf) { |
| 125 |
struct iovec iov[3]; |
| 126 |
ssize_t nwritten, towrite; |
| 127 |
DEFiRet; |
| 128 |
|
| 129 |
iov[0].iov_base = (void*)recHdr; |
| 130 |
iov[0].iov_len = lenRecHdr; |
| 131 |
iov[1].iov_base = (void*)buf; |
| 132 |
iov[1].iov_len = lenBuf; |
| 133 |
iov[2].iov_base = (void*)"\n"; |
| 134 |
iov[2].iov_len = 1; |
| 135 |
towrite = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; |
| 136 |
nwritten = writev(gf->fd, iov, sizeof(iov) / sizeof(struct iovec)); |
| 137 |
if (nwritten != towrite) { |
| 138 |
DBGPRINTF( |
| 139 |
"eiWrite%s: error writing file, towrite %d, " |
| 140 |
"nwritten %d\n", |
| 141 |
recHdr, (int)towrite, (int)nwritten); |
| 142 |
ABORT_FINALIZE(RS_RET_EI_WR_ERR); |
| 143 |
} |
| 144 |
DBGPRINTF("encryption info file %s: written %s, len %d\n", recHdr, gf->eiName, (int)nwritten); |
| 145 |
finalize_it: |
| 146 |
RETiRet; |
| 147 |
} |
| 148 |
|
| 149 |
static rsRetVal eiOpenRead(osslfile gf) { |
| 150 |
DEFiRet; |
| 151 |
gf->fd = open((char*)gf->eiName, O_RDONLY | O_NOCTTY | O_CLOEXEC); |
| 152 |
if (gf->fd == -1) { |
| 153 |
ABORT_FINALIZE(errno == ENOENT ? RS_RET_EI_NO_EXISTS : RS_RET_EI_OPN_ERR); |
| 154 |
} |
| 155 |
finalize_it: |
| 156 |
RETiRet; |
| 157 |
} |
| 158 |
|
| 159 |
static rsRetVal eiRead(osslfile gf) { |
| 160 |
ssize_t nRead; |
| 161 |
DEFiRet; |
| 162 |
|
| 163 |
if (gf->readBuf == NULL) { |
| 164 |
CHKmalloc(gf->readBuf = malloc(READBUF_SIZE)); |
| 165 |
} |
| 166 |
|
| 167 |
nRead = read(gf->fd, gf->readBuf, READBUF_SIZE); |
| 168 |
if (nRead <= 0) { |
| 169 |
ABORT_FINALIZE(RS_RET_ERR); |
| 170 |
} |
| 171 |
gf->readBufMaxIdx = (int16_t)nRead; |
| 172 |
gf->readBufIdx = 0; |
| 173 |
|
| 174 |
finalize_it: |
| 175 |
RETiRet; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/* returns EOF on any kind of error */ |
| 180 |
static int eiReadChar(osslfile gf) { |
| 181 |
int c; |
| 182 |
|
| 183 |
if (gf->readBufIdx >= gf->readBufMaxIdx) { |
| 184 |
if (eiRead(gf) != RS_RET_OK) { |
| 185 |
c = EOF; |
| 186 |
goto finalize_it; |
| 187 |
} |
| 188 |
} |
| 189 |
c = gf->readBuf[gf->readBufIdx++]; |
| 190 |
finalize_it: |
| 191 |
return c; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static rsRetVal eiCheckFiletype(osslfile gf) { |
| 196 |
char hdrBuf[128]; |
| 197 |
size_t toRead, didRead; |
| 198 |
sbool bNeedClose = 0; |
| 199 |
DEFiRet; |
| 200 |
|
| 201 |
if (gf->fd == -1) { |
| 202 |
CHKiRet(eiOpenRead(gf)); |
| 203 |
assert(gf->fd != -1); /* cannot happen after successful return */ |
| 204 |
bNeedClose = 1; |
| 205 |
} |
| 206 |
|
| 207 |
if (Debug) memset(hdrBuf, 0, sizeof(hdrBuf)); /* for dbgprintf below! */ |
| 208 |
toRead = sizeof("FILETYPE:") - 1 + sizeof(RSGCRY_FILETYPE_NAME) - 1 + 1; |
| 209 |
didRead = read(gf->fd, hdrBuf, toRead); |
| 210 |
if (bNeedClose) { |
| 211 |
close(gf->fd); |
| 212 |
gf->fd = -1; |
| 213 |
} |
| 214 |
DBGPRINTF("eiCheckFiletype read %zd bytes: '%s'\n", didRead, hdrBuf); |
| 215 |
if (didRead != toRead || strncmp(hdrBuf, "FILETYPE:" RSGCRY_FILETYPE_NAME "\n", toRead)) |
| 216 |
iRet = RS_RET_EI_INVLD_FILE; |
| 217 |
finalize_it: |
| 218 |
RETiRet; |
| 219 |
} |
| 220 |
|
| 221 |
/* rectype/value must be EIF_MAX_*_LEN+1 long! |
| 222 |
* returns 0 on success or something else on error/EOF |
| 223 |
*/ |
| 224 |
static rsRetVal eiGetRecord(osslfile gf, char* rectype, char* value) { |
| 225 |
unsigned short i, j; |
| 226 |
int c; |
| 227 |
DEFiRet; |
| 228 |
|
| 229 |
c = eiReadChar(gf); |
| 230 |
if (c == EOF) { |
| 231 |
ABORT_FINALIZE(RS_RET_NO_DATA); |
| 232 |
} |
| 233 |
for (i = 0; i < EIF_MAX_RECTYPE_LEN; ++i) { |
| 234 |
if (c == ':' || c == EOF) break; |
| 235 |
rectype[i] = c; |
| 236 |
c = eiReadChar(gf); |
| 237 |
} |
| 238 |
if (c != ':') { |
| 239 |
ABORT_FINALIZE(RS_RET_ERR); |
| 240 |
} |
| 241 |
rectype[i] = '\0'; |
| 242 |
j = 0; |
| 243 |
for (++i; i < EIF_MAX_VALUE_LEN; ++i, ++j) { |
| 244 |
c = eiReadChar(gf); |
| 245 |
if (c == '\n' || c == EOF) break; |
| 246 |
value[j] = c; |
| 247 |
} |
| 248 |
if (c != '\n') { |
| 249 |
ABORT_FINALIZE(RS_RET_ERR); |
| 250 |
} |
| 251 |
value[j] = '\0'; |
| 252 |
finalize_it: |
| 253 |
RETiRet; |
| 254 |
} |
| 255 |
|
| 256 |
static rsRetVal eiGetIV(osslfile gf, uchar* iv, size_t leniv) { |
| 257 |
char rectype[EIF_MAX_RECTYPE_LEN + 1]; |
| 258 |
char value[EIF_MAX_VALUE_LEN + 1]; |
| 259 |
size_t valueLen; |
| 260 |
unsigned short i, j; |
| 261 |
unsigned char nibble; |
| 262 |
DEFiRet; |
| 263 |
|
| 264 |
CHKiRet(eiGetRecord(gf, rectype, value)); |
| 265 |
const char* const cmp_IV = "IV"; // work-around for static analyzer |
| 266 |
if (strcmp(rectype, cmp_IV)) { |
| 267 |
DBGPRINTF( |
| 268 |
"no IV record found when expected, record type " |
| 269 |
"seen is '%s'\n", |
| 270 |
rectype); |
| 271 |
ABORT_FINALIZE(RS_RET_ERR); |
| 272 |
} |
| 273 |
valueLen = strlen(value); |
| 274 |
if (valueLen / 2 != leniv) { |
| 275 |
DBGPRINTF("length of IV is %zd, expected %zd\n", valueLen / 2, leniv); |
| 276 |
ABORT_FINALIZE(RS_RET_ERR); |
| 277 |
} |
| 278 |
|
| 279 |
for (i = j = 0; i < valueLen; ++i) { |
| 280 |
if (value[i] >= '0' && value[i] <= '9') |
| 281 |
nibble = value[i] - '0'; |
| 282 |
else if (value[i] >= 'a' && value[i] <= 'f') |
| 283 |
nibble = value[i] - 'a' + 10; |
| 284 |
else { |
| 285 |
DBGPRINTF("invalid IV '%s'\n", value); |
| 286 |
ABORT_FINALIZE(RS_RET_ERR); |
| 287 |
} |
| 288 |
if (i % 2 == 0) |
| 289 |
iv[j] = nibble << 4; |
| 290 |
else |
| 291 |
iv[j++] |= nibble; |
| 292 |
} |
| 293 |
finalize_it: |
| 294 |
RETiRet; |
| 295 |
} |
| 296 |
|
| 297 |
static rsRetVal eiGetEND(osslfile gf, off64_t* offs) { |
| 298 |
char rectype[EIF_MAX_RECTYPE_LEN + 1]; |
| 299 |
char value[EIF_MAX_VALUE_LEN + 1]; |
| 300 |
DEFiRet; |
| 301 |
|
| 302 |
CHKiRet(eiGetRecord(gf, rectype, value)); |
| 303 |
const char* const const_END = "END"; // clang static analyzer work-around |
| 304 |
if (strcmp(rectype, const_END)) { |
| 305 |
DBGPRINTF( |
| 306 |
"no END record found when expected, record type " |
| 307 |
"seen is '%s'\n", |
| 308 |
rectype); |
| 309 |
ABORT_FINALIZE(RS_RET_ERR); |
| 310 |
} |
| 311 |
*offs = atoll(value); |
| 312 |
finalize_it: |
| 313 |
RETiRet; |
| 314 |
} |
| 315 |
|
| 316 |
static rsRetVal eiOpenAppend(osslfile gf) { |
| 317 |
rsRetVal localRet; |
| 318 |
DEFiRet; |
| 319 |
localRet = eiCheckFiletype(gf); |
| 320 |
if (localRet == RS_RET_OK) { |
| 321 |
gf->fd = open((char*)gf->eiName, O_WRONLY | O_APPEND | O_NOCTTY | O_CLOEXEC, 0600); |
| 322 |
if (gf->fd == -1) { |
| 323 |
ABORT_FINALIZE(RS_RET_EI_OPN_ERR); |
| 324 |
} |
| 325 |
} else if (localRet == RS_RET_EI_NO_EXISTS) { |
| 326 |
/* looks like we need to create a new file */ |
| 327 |
gf->fd = open((char*)gf->eiName, O_WRONLY | O_CREAT | O_NOCTTY | O_CLOEXEC, 0600); |
| 328 |
if (gf->fd == -1) { |
| 329 |
ABORT_FINALIZE(RS_RET_EI_OPN_ERR); |
| 330 |
} |
| 331 |
CHKiRet(eiWriteRec(gf, "FILETYPE:", 9, RSGCRY_FILETYPE_NAME, sizeof(RSGCRY_FILETYPE_NAME) - 1)); |
| 332 |
} else { |
| 333 |
gf->fd = -1; |
| 334 |
ABORT_FINALIZE(localRet); |
| 335 |
} |
| 336 |
DBGPRINTF("encryption info file %s: opened as #%d\n", gf->eiName, gf->fd); |
| 337 |
finalize_it: |
| 338 |
RETiRet; |
| 339 |
} |
| 340 |
|
| 341 |
static rsRetVal __attribute__((nonnull(2))) eiWriteIV(osslfile gf, const uchar* const iv) { |
| 342 |
static const char hexchars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 343 |
unsigned iSrc, iDst; |
| 344 |
char hex[4096]; |
| 345 |
DEFiRet; |
| 346 |
|
| 347 |
if (gf->blkLength > sizeof(hex) / 2) { |
| 348 |
DBGPRINTF( |
| 349 |
"eiWriteIV: crypto block len way too large, aborting " |
| 350 |
"write"); |
| 351 |
ABORT_FINALIZE(RS_RET_ERR); |
| 352 |
} |
| 353 |
|
| 354 |
for (iSrc = iDst = 0; iSrc < gf->blkLength; ++iSrc) { |
| 355 |
hex[iDst++] = hexchars[iv[iSrc] >> 4]; |
| 356 |
hex[iDst++] = hexchars[iv[iSrc] & 0x0f]; |
| 357 |
} |
| 358 |
|
| 359 |
iRet = eiWriteRec(gf, "IV:", 3, hex, gf->blkLength * 2); |
| 360 |
finalize_it: |
| 361 |
RETiRet; |
| 362 |
} |
| 363 |
|
| 364 |
/* we do not return an error state, as we MUST close the file, |
| 365 |
* no matter what happens. |
| 366 |
*/ |
| 367 |
static void eiClose(osslfile gf, off64_t offsLogfile) { |
| 368 |
char offs[21]; |
| 369 |
size_t len; |
| 370 |
if (gf->fd == -1) return; |
| 371 |
if (gf->openMode == 'w') { |
| 372 |
/* 2^64 is 20 digits, so the snprintf buffer is large enough */ |
| 373 |
len = snprintf(offs, sizeof(offs), "%lld", (long long)offsLogfile); |
| 374 |
eiWriteRec(gf, "END:", 4, offs, len); |
| 375 |
} |
| 376 |
EVP_CIPHER_CTX_free(gf->chd); |
| 377 |
free(gf->readBuf); |
| 378 |
close(gf->fd); |
| 379 |
gf->fd = -1; |
| 380 |
DBGPRINTF("encryption info file %s: closed\n", gf->eiName); |
| 381 |
} |
| 382 |
|
| 383 |
/* this returns the number of bytes left inside the block or -1, if the block |
| 384 |
* size is unbounded. The function automatically handles end-of-block and begins |
| 385 |
* to read the next block in this case. |
| 386 |
*/ |
| 387 |
rsRetVal osslfileGetBytesLeftInBlock(osslfile gf, ssize_t* left) { |
| 388 |
DEFiRet; |
| 389 |
if (gf->bytesToBlkEnd == 0) { |
| 390 |
DBGPRINTF("libossl: end of current crypto block\n"); |
| 391 |
EVP_CIPHER_CTX_free(gf->chd); |
| 392 |
CHKiRet(rsosslBlkBegin(gf)); |
| 393 |
} |
| 394 |
*left = gf->bytesToBlkEnd; |
| 395 |
finalize_it: |
| 396 |
RETiRet; |
| 397 |
} |
| 398 |
|
| 399 |
/* this is a special functon for use by the rsyslog disk queue subsystem. It |
| 400 |
* needs to have the capability to delete state when a queue file is rolled |
| 401 |
* over. This simply generates the file name and deletes it. It must take care |
| 402 |
* of "all" state files, which currently happens to be a single one. |
| 403 |
*/ |
| 404 |
rsRetVal osslfileDeleteState(uchar* logfn) { |
| 405 |
char fn[MAXFNAME + 1]; |
| 406 |
DEFiRet; |
| 407 |
snprintf(fn, sizeof(fn), "%s%s", logfn, ENCINFO_SUFFIX); |
| 408 |
fn[MAXFNAME] = '\0'; /* be on save side */ |
| 409 |
DBGPRINTF("ossl crypto provider deletes state file '%s' on request\n", fn); |
| 410 |
unlink(fn); |
| 411 |
RETiRet; |
| 412 |
} |
| 413 |
|
| 414 |
static rsRetVal osslfileConstruct(osslctx ctx, osslfile* pgf, uchar* logfn) { |
| 415 |
char fn[MAXFNAME + 1]; |
| 416 |
osslfile gf; |
| 417 |
DEFiRet; |
| 418 |
|
| 419 |
CHKmalloc(gf = calloc(1, sizeof(struct osslfile_s))); |
| 420 |
CHKmalloc(gf->chd = EVP_CIPHER_CTX_new()); |
| 421 |
gf->ctx = ctx; |
| 422 |
gf->fd = -1; |
| 423 |
snprintf(fn, sizeof(fn), "%s%s", logfn, ENCINFO_SUFFIX); |
| 424 |
fn[MAXFNAME] = '\0'; /* be on save side */ |
| 425 |
gf->eiName = (uchar*)strdup(fn); |
| 426 |
*pgf = gf; |
| 427 |
finalize_it: |
| 428 |
RETiRet; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
osslctx osslCtxNew(void) { |
| 433 |
osslctx ctx; |
| 434 |
ctx = calloc(1, sizeof(struct osslctx_s)); |
| 435 |
if (ctx != NULL) { |
| 436 |
ctx->cipher = EVP_aes_128_cbc(); |
| 437 |
ctx->key = NULL; |
| 438 |
ctx->keyLen = -1; |
| 439 |
} |
| 440 |
return ctx; |
| 441 |
} |
| 442 |
|
| 443 |
int osslfileDestruct(osslfile gf, off64_t offsLogfile) { |
| 444 |
int r = 0; |
| 445 |
if (gf == NULL) goto done; |
| 446 |
|
| 447 |
DBGPRINTF("libossl: close file %s\n", gf->eiName); |
| 448 |
eiClose(gf, offsLogfile); |
| 449 |
if (gf->bDeleteOnClose) { |
| 450 |
DBGPRINTF("unlink file '%s' due to bDeleteOnClose set\n", gf->eiName); |
| 451 |
unlink((char*)gf->eiName); |
| 452 |
} |
| 453 |
free(gf->eiName); |
| 454 |
free(gf); |
| 455 |
done: |
| 456 |
return r; |
| 457 |
} |
| 458 |
|
| 459 |
void rsosslCtxDel(osslctx ctx) { |
| 460 |
if (ctx != NULL) { |
| 461 |
free(ctx->key); |
| 462 |
free(ctx); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
/* returns 0 on succes, positive if key length does not match and key |
| 468 |
* of return value size is required. |
| 469 |
*/ |
| 470 |
int rsosslSetKey(osslctx ctx, unsigned char* key, uint16_t keyLen) { |
| 471 |
uint16_t reqKeyLen; |
| 472 |
int r; |
| 473 |
|
| 474 |
reqKeyLen = EVP_CIPHER_get_key_length(ctx->cipher); |
| 475 |
if (keyLen != reqKeyLen) { |
| 476 |
r = reqKeyLen; |
| 477 |
goto done; |
| 478 |
} |
| 479 |
ctx->keyLen = keyLen; |
| 480 |
ctx->key = malloc(keyLen); |
| 481 |
memcpy(ctx->key, key, keyLen); |
| 482 |
r = 0; |
| 483 |
done: |
| 484 |
return r; |
| 485 |
} |
| 486 |
|
| 487 |
rsRetVal rsosslSetAlgoMode(osslctx ctx, uchar* algorithm) { |
| 488 |
EVP_CIPHER* cipher; |
| 489 |
DEFiRet; |
| 490 |
|
| 491 |
cipher = EVP_CIPHER_fetch(NULL, (char*)algorithm, NULL); |
| 492 |
if (cipher == NULL) { |
| 493 |
ABORT_FINALIZE(RS_RET_CRY_INVLD_ALGO); |
| 494 |
} |
| 495 |
ctx->cipher = cipher; |
| 496 |
|
| 497 |
finalize_it: |
| 498 |
RETiRet; |
| 499 |
} |
| 500 |
|
| 501 |
/* We use random numbers to initiate the IV. Rsyslog runtime will ensure |
| 502 |
* we get a sufficiently large number. |
| 503 |
*/ |
| 504 |
#if defined(__clang__) |
| 505 |
#pragma GCC diagnostic ignored "-Wunknown-attributes" |
| 506 |
#endif |
| 507 |
static rsRetVal |
| 508 |
#if defined(__clang__) |
| 509 |
__attribute__((no_sanitize("shift"))) /* IV shift causes known overflow */ |
| 510 |
#endif |
| 511 |
seedIV(osslfile gf, uchar** iv) { |
| 512 |
long rndnum = 0; /* keep compiler happy -- this value is always overriden */ |
| 513 |
DEFiRet; |
| 514 |
|
| 515 |
CHKmalloc(*iv = calloc(1, gf->blkLength)); |
| 516 |
for (size_t i = 0; i < gf->blkLength; ++i) { |
| 517 |
const int shift = (i % 4) * 8; |
| 518 |
if (shift == 0) { /* need new random data? */ |
| 519 |
rndnum = randomNumber(); |
| 520 |
} |
| 521 |
(*iv)[i] = 0xff & ((rndnum & (255u << shift)) >> shift); |
| 522 |
} |
| 523 |
finalize_it: |
| 524 |
RETiRet; |
| 525 |
} |
| 526 |
|
| 527 |
static rsRetVal readIV(osslfile gf, uchar** iv) { |
| 528 |
rsRetVal localRet; |
| 529 |
DEFiRet; |
| 530 |
|
| 531 |
if (gf->fd == -1) { |
| 532 |
while (gf->fd == -1) { |
| 533 |
localRet = eiOpenRead(gf); |
| 534 |
if (localRet == RS_RET_EI_NO_EXISTS) { |
| 535 |
/* wait until it is created */ |
| 536 |
srSleep(0, 10000); |
| 537 |
} else { |
| 538 |
CHKiRet(localRet); |
| 539 |
} |
| 540 |
} |
| 541 |
CHKiRet(eiCheckFiletype(gf)); |
| 542 |
} |
| 543 |
*iv = malloc(gf->blkLength); /* do NOT zero-out! */ |
| 544 |
iRet = eiGetIV(gf, *iv, (size_t)gf->blkLength); |
| 545 |
finalize_it: |
| 546 |
RETiRet; |
| 547 |
} |
| 548 |
|
| 549 |
/* this tries to read the END record. HOWEVER, no such record may be |
| 550 |
* present, which is the case if we handle a currently-written to queue |
| 551 |
* file. On the other hand, the queue file may contain multiple blocks. So |
| 552 |
* what we do is try to see if there is a block end or not - and set the |
| 553 |
* status accordingly. Note that once we found no end-of-block, we will never |
| 554 |
* retry. This is because that case can never happen under current queue |
| 555 |
* implementations. -- gerhards, 2013-05-16 |
| 556 |
*/ |
| 557 |
static rsRetVal readBlkEnd(osslfile gf) { |
| 558 |
off64_t blkEnd; |
| 559 |
DEFiRet; |
| 560 |
|
| 561 |
iRet = eiGetEND(gf, &blkEnd); |
| 562 |
if (iRet == RS_RET_OK) { |
| 563 |
gf->bytesToBlkEnd = (ssize_t)blkEnd; |
| 564 |
} else if (iRet == RS_RET_NO_DATA) { |
| 565 |
gf->bytesToBlkEnd = -1; |
| 566 |
} else { |
| 567 |
FINALIZE; |
| 568 |
} |
| 569 |
|
| 570 |
finalize_it: |
| 571 |
RETiRet; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
/* module-init dummy for potential later use */ |
| 576 |
int rsosslInit(void) { |
| 577 |
return 0; |
| 578 |
} |
| 579 |
|
| 580 |
/* module-deinit dummy for potential later use */ |
| 581 |
void rsosslExit(void) { |
| 582 |
return; |
| 583 |
} |
| 584 |
|
| 585 |
/* Read the block begin metadata and set our state variables accordingly. |
| 586 |
* Can also be used to init the first block in write case. |
| 587 |
*/ |
| 588 |
static rsRetVal rsosslBlkBegin(osslfile gf) { |
| 589 |
uchar* iv = NULL; |
| 590 |
DEFiRet; |
| 591 |
const char openMode = gf->openMode; |
| 592 |
// FIXME error handling |
| 593 |
|
| 594 |
if (openMode == 'r') { |
| 595 |
readIV(gf, &iv); |
| 596 |
readBlkEnd(gf); |
| 597 |
} else { |
| 598 |
CHKiRet(seedIV(gf, &iv)); |
| 599 |
} |
| 600 |
|
| 601 |
if (openMode == 'r') { |
| 602 |
if ((iRet = EVP_DecryptInit_ex(gf->chd, gf->ctx->cipher, NULL, gf->ctx->key, iv)) != 1) { |
| 603 |
DBGPRINTF("EVP_DecryptInit_ex failed: %d\n", iRet); |
| 604 |
ABORT_FINALIZE(RS_RET_ERR); |
| 605 |
} |
| 606 |
} else { |
| 607 |
if ((iRet = EVP_EncryptInit_ex(gf->chd, gf->ctx->cipher, NULL, gf->ctx->key, iv)) != 1) { |
| 608 |
DBGPRINTF("EVP_EncryptInit_ex failed: %d\n", iRet); |
| 609 |
ABORT_FINALIZE(RS_RET_ERR); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
if ((iRet = EVP_CIPHER_CTX_set_padding(gf->chd, 0)) != 1) { |
| 614 |
fprintf(stderr, "EVP_CIPHER_set_padding failed: %d\n", iRet); |
| 615 |
ABORT_FINALIZE(RS_RET_ERR); |
| 616 |
} |
| 617 |
|
| 618 |
if (openMode == 'w') { |
| 619 |
CHKiRet(eiOpenAppend(gf)); |
| 620 |
CHKiRet(eiWriteIV(gf, iv)); |
| 621 |
} |
| 622 |
finalize_it: |
| 623 |
free(iv); |
| 624 |
RETiRet; |
| 625 |
} |
| 626 |
|
| 627 |
rsRetVal rsosslInitCrypt(osslctx ctx, osslfile* pgf, uchar* fname, char openMode) { |
| 628 |
osslfile gf = NULL; |
| 629 |
DEFiRet; |
| 630 |
|
| 631 |
CHKiRet(osslfileConstruct(ctx, &gf, fname)); |
| 632 |
gf->openMode = openMode; |
| 633 |
gf->blkLength = EVP_CIPHER_get_block_size(ctx->cipher); |
| 634 |
CHKiRet(rsosslBlkBegin(gf)); |
| 635 |
*pgf = gf; |
| 636 |
finalize_it: |
| 637 |
if (iRet != RS_RET_OK && gf != NULL) osslfileDestruct(gf, -1); |
| 638 |
RETiRet; |
| 639 |
} |
| 640 |
|
| 641 |
rsRetVal rsosslDecrypt(osslfile pF, uchar* buf, size_t* len) { |
| 642 |
DEFiRet; |
| 643 |
int rc; |
| 644 |
|
| 645 |
if (pF->bytesToBlkEnd != -1) pF->bytesToBlkEnd -= *len; |
| 646 |
rc = EVP_DecryptUpdate(pF->chd, buf, (int*)len, buf, (int)*len); |
| 647 |
if (rc != 1) { |
| 648 |
DBGPRINTF("EVP_DecryptUpdate failed\n"); |
| 649 |
ABORT_FINALIZE(RS_RET_ERR); |
| 650 |
} |
| 651 |
removePadding(buf, len); |
| 652 |
|
| 653 |
dbgprintf("libossl: decrypted, bytesToBlkEnd %lld, buffer is now '%50.50s'\n", (long long)pF->bytesToBlkEnd, buf); |
| 654 |
|
| 655 |
finalize_it: |
| 656 |
RETiRet; |
| 657 |
} |
| 658 |
|
| 659 |
rsRetVal rsosslEncrypt(osslfile pF, uchar* buf, size_t* len) { |
| 660 |
DEFiRet; |
| 661 |
int tmplen; |
| 662 |
|
| 663 |
if (*len == 0) FINALIZE; |
| 664 |
|
| 665 |
addPadding(pF, buf, len); |
| 666 |
if (EVP_EncryptUpdate(pF->chd, buf, (int*)len, buf, (int)*len) != 1) { |
| 667 |
dbgprintf("EVP_EncryptUpdate failed\n"); |
| 668 |
ABORT_FINALIZE(RS_RET_CRYPROV_ERR); |
| 669 |
} |
| 670 |
|
| 671 |
if (EVP_EncryptFinal_ex(pF->chd, buf + *len, &tmplen) != 1) { |
| 672 |
dbgprintf("EVP_EncryptFinal_ex failed\n"); |
| 673 |
ABORT_FINALIZE(RS_RET_CRYPROV_ERR); |
| 674 |
} |
| 675 |
*len += tmplen; |
| 676 |
|
| 677 |
finalize_it: |
| 678 |
RETiRet; |
| 679 |
} |