|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved.
|
|
|
|
*
|
|
|
|
* Author: Shawn Webb
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONF_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
Add CMake build tooling
This patch adds experimental-quality CMake build tooling.
The libmspack build required a modification to use "" instead of <> for
header #includes. This will hopefully be included in the libmspack
upstream project when adding CMake build tooling to libmspack.
Removed use of libltdl when using CMake.
Flex & Bison are now required to build.
If -DMAINTAINER_MODE, then GPERF is also required, though it currently
doesn't actually do anything. TODO!
I found that the autotools build system was generating the lexer output
but not actually compiling it, instead using previously generated (and
manually renamed) lexer c source. As a consequence, changes to the .l
and .y files weren't making it into the build. To resolve this, I
removed generated flex/bison files and fixed the tooling to use the
freshly generated files. Flex and bison are now required build tools.
On Windows, this adds a dependency on the winflexbison package,
which can be obtained using Chocolatey or may be manually installed.
CMake tooling only has partial support for building with external LLVM
library, and no support for the internal LLVM (to be removed in the
future). I.e. The CMake build currently only supports the bytecode
interpreter.
Many files used include paths relative to the top source directory or
relative to the current project, rather than relative to each build
target. Modern CMake support requires including internal dependency
headers the same way you would external dependency headers (albeit
with "" instead of <>). This meant correcting all header includes to
be relative to the build targets and not relative to the workspace.
For example, ...
```c
include "../libclamav/clamav.h"
include "clamd/clamd_others.h"
```
... becomes:
```c
// libclamav
include "clamav.h"
// clamd
include "clamd_others.h"
```
Fixes header name conflicts by renaming a few of the files.
Converted the "shared" code into a static library, which depends on
libclamav. The ironically named "shared" static library provides
features common to the ClamAV apps which are not required in
libclamav itself and are not intended for use by downstream projects.
This change was required for correct modern CMake practices but was
also required to use the automake "subdir-objects" option.
This eliminates warnings when running autoreconf which, in the next
version of autoconf & automake are likely to break the build.
libclamav used to build in multiple stages where an earlier stage is
a static library containing utils required by the "shared" code.
Linking clamdscan and clamdtop with this libclamav utils static lib
allowed these two apps to function without libclamav. While this is
nice in theory, the practical gains are minimal and it complicates
the build system. As such, the autotools and CMake tooling was
simplified for improved maintainability and this feature was thrown
out. clamdtop and clamdscan now require libclamav to function.
Removed the nopthreads version of the autotools
libclamav_internal_utils static library and added pthread linking to
a couple apps that may have issues building on some platforms without
it, with the intention of removing needless complexity from the
source. Kept the regular version of libclamav_internal_utils.la
though it is no longer used anywhere but in libclamav.
Added an experimental doxygen build option which attempts to build
clamav.h and libfreshclam doxygen html docs.
The CMake build tooling also may build the example program(s), which
isn't a feature in the Autotools build system.
Changed C standard to C90+ due to inline linking issues with socket.h
when linking libfreshclam.so on Linux.
Generate common.rc for win32.
Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef
from misc.c.
Add CMake files to the automake dist, so users can try the new
CMake tooling w/out having to build from a git clone.
clamonacc changes:
- Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other
similar macros.
- Added a new clamav-clamonacc.service systemd unit file, based on
the work of ChadDevOps & Aaron Brighton.
- Added missing clamonacc man page.
Updates to clamdscan man page, add missing options.
Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use
libclamav).
Rename Windows mspack.dll to libmspack.dll so all ClamAV-built
libraries have the lib-prefix with Visual Studio as with CMake.
5 years ago
|
|
|
// libclamav
|
|
|
|
#include "clamav.h"
|
|
|
|
#include "conv.h"
|
|
|
|
|
|
|
|
/** Get the expected decoded length of a base64-encoded string
|
|
|
|
* @param[in] data Base64-encoded string
|
|
|
|
* @param[in] len length of the string
|
|
|
|
* @return The expected decoded length of the base64-encoded string
|
|
|
|
*/
|
|
|
|
static size_t base64_len(const char *data, size_t len)
|
|
|
|
{
|
|
|
|
int padding = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = len - 1; i > 0 && data[i] == '='; i--)
|
|
|
|
padding++;
|
|
|
|
|
|
|
|
return (size_t)((3 * len) / 4 - padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Decode a base64-encoded string
|
|
|
|
* @param[in] data The base64-encoded string
|
|
|
|
* @param[in] len Length of the base64-encoded string
|
|
|
|
* @param[out] obuf If obuf is not set to NULL, store the decoded data in obuf. Otherwise, the decoded data is stored in a dynamically-allocated buffer.
|
|
|
|
* @param[out] olen The length of the decoded data
|
|
|
|
* @return The base64-decoded data
|
|
|
|
*/
|
|
|
|
void *cl_base64_decode(char *data, size_t len, void *obuf, size_t *olen, int oneline)
|
|
|
|
{
|
|
|
|
BIO *bio, *b64;
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
buf = (obuf) ? obuf : malloc(base64_len(data, len) + 1);
|
|
|
|
if (!(buf))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
b64 = BIO_new(BIO_f_base64());
|
|
|
|
if (!(b64)) {
|
|
|
|
if (!(obuf))
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio = BIO_new_mem_buf(data, len);
|
|
|
|
if (!(bio)) {
|
|
|
|
BIO_free(b64);
|
|
|
|
if (!(obuf))
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio = BIO_push(b64, bio);
|
|
|
|
if (oneline)
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
|
|
|
|
|
|
|
*olen = BIO_read(bio, buf, base64_len(data, len));
|
|
|
|
|
|
|
|
BIO_free_all(bio);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Base64-encode data
|
|
|
|
* @param[in] data The data to be encoded
|
|
|
|
* @param[in] len The length of the data
|
|
|
|
* @return A pointer to the base64-encoded data. The data is stored in a dynamically-allocated buffer.
|
|
|
|
*/
|
|
|
|
char *cl_base64_encode(void *data, size_t len)
|
|
|
|
{
|
|
|
|
BIO *bio, *b64;
|
|
|
|
char *buf, *p;
|
|
|
|
size_t elen;
|
|
|
|
|
|
|
|
b64 = BIO_new(BIO_f_base64());
|
|
|
|
if (!(b64))
|
|
|
|
return NULL;
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
|
|
|
if (!(bio)) {
|
|
|
|
BIO_free(b64);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio = BIO_push(b64, bio);
|
|
|
|
BIO_write(bio, data, len);
|
|
|
|
|
|
|
|
BIO_flush(bio);
|
|
|
|
elen = (size_t)BIO_get_mem_data(bio, &buf);
|
|
|
|
|
|
|
|
/* Ensure we're dealing with a NULL-terminated string */
|
|
|
|
p = (char *)malloc(elen + 1);
|
|
|
|
if (NULL == p) {
|
|
|
|
BIO_free(b64);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy((void *)p, (void *)buf, elen);
|
|
|
|
p[elen] = 0x00;
|
|
|
|
buf = p;
|
|
|
|
|
|
|
|
BIO_free_all(bio);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONV_SELF_TEST)
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *plaintext, *encoded, *decoded;
|
|
|
|
unsigned char *sha_plaintext, *sha_decoded;
|
|
|
|
size_t len;
|
|
|
|
int ret = 0;
|
|
|
|
unsigned int shalen;
|
|
|
|
|
|
|
|
initialize_crypto();
|
|
|
|
|
|
|
|
plaintext = (argv[1]) ? argv[1] : "Hello. This is dog";
|
|
|
|
sha_plaintext = sha256(plaintext, strlen(plaintext), NULL, NULL);
|
|
|
|
if (!(sha_plaintext)) {
|
|
|
|
fprintf(stderr, "Could not generate sha256 of plaintext\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded = base64_encode(plaintext, strlen(plaintext));
|
|
|
|
if (!(encoded)) {
|
|
|
|
fprintf(stderr, "Could not base64 encode plaintest\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Base64 encoded: %s\n", encoded);
|
|
|
|
|
|
|
|
decoded = base64_decode(encoded, strlen(encoded), NULL, &len);
|
|
|
|
if (!(decoded)) {
|
|
|
|
fprintf(stderr, "Could not base64 decoded string\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sha_decoded = sha256(decoded, len, NULL, &shalen);
|
|
|
|
if (!(sha_decoded)) {
|
|
|
|
fprintf(stderr, "Could not generate sha256 of decoded data\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(sha_plaintext, sha_decoded, shalen)) {
|
|
|
|
fprintf(stderr, "Decoded does not match plaintext: %s\n", decoded);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sha_decoded);
|
|
|
|
free(sha_plaintext);
|
|
|
|
free(encoded);
|
|
|
|
free(decoded);
|
|
|
|
|
|
|
|
cleanup_crypto();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|