Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This release focuses on analysisd scalability and broader stability. Key enhance
- @atomicturtle - [PR 2238](https://github.com/ossec/ossec-hids/pull/2238) - Fix false dpkg install alert when removing packages
- @atomicturtle - [PR 2239](https://github.com/ossec/ossec-hids/pull/2239) - Fix msauth Enterprise Admins false positives and negatives
- @atomicturtle - [PR 2240](https://github.com/ossec/ossec-hids/pull/2240) - Fix Postfix rule 3331 false positive on postscreen client ports
- @atomicturtle - [PR 2243](https://github.com/ossec/ossec-hids/pull/2243) - Fix AIX agent entropy failure after chroot by keeping OS RNG usable


**OSSEC changelog (4.1.0) <support@atomicorp.com>**
Expand Down
3 changes: 1 addition & 2 deletions src/addagent/manage_agents.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "manage_agents.h"
#include "os_crypto/md5/md5_op.h"
#include "external/cJSON/cJSON.h"
#include <openssl/rand.h>
#include <stdlib.h>

/* Global variables */
Expand Down Expand Up @@ -366,7 +365,7 @@ int add_agent(int json_output)
char rand_hex[129];

/* Generate cryptographically secure random bytes */
if (!RAND_bytes(random_data, sizeof(random_data))) {
if (!randombytes_try(random_data, sizeof(random_data))) {
if (json_output) {
cJSON *json_root = cJSON_CreateObject();
cJSON_AddNumberToObject(json_root, "error", 75);
Expand Down
47 changes: 26 additions & 21 deletions src/addagent/manage_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

#include "manage_agents.h"
#include <openssl/rand.h>
#include "os_crypto/md5/md5_op.h"
#include "external/cJSON/cJSON.h"
#include <stdlib.h>
Expand Down Expand Up @@ -324,6 +323,7 @@ int k_extract(const char *cmdextract, int json_output)
int k_bulkload(const char *cmdbulk)
{
int i = 1;
int status = 0;
FILE *fp, *infp;
char str1[STR_SIZE + 1];
char str2[STR_SIZE + 1];
Expand Down Expand Up @@ -443,38 +443,43 @@ int k_bulkload(const char *cmdbulk)
}
#endif

/* Cryptographically secure random number generation */
unsigned char random_data[64];
char rand_hex[129];

if (!RAND_bytes(random_data, sizeof(random_data))) {
merror("Failed to generate secure random data for agent key");
return 0;
}
/* Cryptographically secure random number generation */
unsigned char random_data[64];
char rand_hex[129];

/* Hex encode the random data */
for(i=0; i<64; i++) {
sprintf(&rand_hex[i*2], "%02x", random_data[i]);
}
if (!randombytes_try(random_data, sizeof(random_data))) {
merror("Failed to generate secure random data for agent key");
fclose(fp);
status = 1;
goto cleanup;
}

/* First key component: name + ID + random data */
snprintf(str1, STR_SIZE, "%s%s%s", name, id, rand_hex);
OS_MD5_Str(str1, md1);
/* Hex encode the random data */
for (i = 0; i < 64; i++) {
sprintf(&rand_hex[i * 2], "%02x", random_data[i]);
}

/* First key component: name + ID + random data */
snprintf(str1, STR_SIZE, "%s%s%s", name, id, rand_hex);
OS_MD5_Str(str1, md1);

/* Second key component: IP + name + random data (offset) */
snprintf(str2, STR_SIZE, "%s%s%s", ip, name, rand_hex + 64);
OS_MD5_Str(str2, md2);
/* Second key component: IP + name + random data (offset) */
snprintf(str2, STR_SIZE, "%s%s%s", ip, name, rand_hex + 64);
OS_MD5_Str(str2, md2);

fprintf(fp, "%s %s %s %s%s\n", id, name, ip, md1, md2);
fprintf(fp, "%s %s %s %s%s\n", id, name, ip, md1, md2);
fclose(fp);

printf(AGENT_ADD, id);
restart_necessary = 1;

cleanup:
free(c_ip.ip);
if (status != 0) {
break;
}
};

fclose(infp);
return (0);
return (status);
}
5 changes: 4 additions & 1 deletion src/headers/randombytes.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef __RANDOMBYTES_H
#define __RANDOMBYTES_H

#include <stddef.h>

void randombytes(void *ptr, size_t length);
/** Fill buffer with OS entropy. Returns 1 on success, 0 on failure (no exit). */
int randombytes_try(void *ptr, size_t length);
void srandom_init(void);

#endif

18 changes: 12 additions & 6 deletions src/os_crypto/aes/aes_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,35 @@
#include <openssl/evp.h>
#include <openssl/err.h>
#include "aes_op.h"
#include "randombytes.h"

typedef unsigned char uchar;


#include <openssl/rand.h>

int OS_AES_Str(const char *input, char *output, const char *charkey,
long size, short int action)
{
unsigned char iv[16];

if(action == OS_ENCRYPT)
{
/* Generate Random IV */
if (!RAND_bytes(iv, sizeof(iv))) {
return 0; // Error
/* Per-message random IV via OS entropy (FD kept across chroot). */
if (!randombytes_try(iv, sizeof(iv))) {
return 0;
}

/* Prepend IV to output */
memcpy(output, iv, 16);

/* Encrypt content after the IV */
return 16 + encrypt_AES((const uchar *)input, (int)size, (uchar *)charkey, iv, (uchar *)output + 16);
{
int aes_len = encrypt_AES((const uchar *)input, (int)size, (uchar *)charkey, iv,
(uchar *)output + 16);
if (aes_len == 0) {
return 0;
}
return 16 + aes_len;
}
}
else
{
Expand Down
22 changes: 17 additions & 5 deletions src/os_crypto/shared/msgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "os_crypto/md5/md5_op.h"
#include "os_crypto/blowfish/bf_op.h"
#include "os_crypto/aes/aes_op.h"
#include <openssl/rand.h>
#include <sys/stat.h>

/* Helper for File_Inode */
Expand Down Expand Up @@ -544,10 +543,15 @@ size_t CreateSecMSG(const keystore *keys, const char *msg, size_t msg_length, ch
return 0; // OS_INVALID
}

/* Random number, take only 5 chars ~= 2^16=65536*/
if (!RAND_bytes((unsigned char *)&rand1, sizeof(rand1))) {
merror("RAND_bytes failed");
return(0);
/* Random number, take only 5 chars ~= 2^16=65536.
* Entropy via randombytes_try: pre-chroot /dev/urandom FD on Unix,
* arc4random on OpenBSD, CryptoAPI (CRYPT_VERIFYCONTEXT) on Windows.
* Avoids OpenSSL RAND_bytes, which re-opens /dev/urandom by path after
* chroot and fails on AIX and similar platforms without getrandom().
*/
if (!randombytes_try(&rand1, sizeof(rand1))) {
merror("randombytes failed");
return (0);
}

_tmpmsg[OS_MAXSTR + 1] = '\0';
Expand Down Expand Up @@ -643,6 +647,14 @@ size_t CreateSecMSG(const keystore *keys, const char *msg, size_t msg_length, ch
(long) cmp_size,
OS_ENCRYPT,crypto_method);

/* Blowfish returns 1; AES returns ciphertext length. Zero means failure
* (e.g. AES IV entropy unavailable).
*/
if (!crypto_length) {
merror("encryption failed");
return (0);
}

if(cmp_size < crypto_length)
cmp_size = crypto_length;

Expand Down
Loading
Loading