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