| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 2020,2022,2024-2025 Hannes von Haugwitz |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <stdbool.h> |
| 22 | #include <stdlib.h> |
| 23 | #include "config.h" |
| 24 | #include "attributes.h" |
| 25 | #include "hashsum.h" |
| 26 | #include "log.h" |
| 27 | #include "errorcodes.h" |
| 28 | |
| 29 | #ifdef WITH_GCRYPT |
| 30 | #include <gcrypt.h> |
| 31 | #define NEED_LIBGCRYPT_VERSION "1.8.0" |
| 32 | #endif |
| 33 | |
| 34 | hashsum_t hashsums[] = { |
| 35 | { attr_md5, 16 }, |
| 36 | { attr_sha1, 20 }, |
| 37 | { attr_sha256, 32 }, |
| 38 | { attr_sha512, 64 }, |
| 39 | { attr_rmd160, 20 }, |
| 40 | { attr_tiger, 24 }, |
| 41 | { attr_crc32, 4 }, |
| 42 | { attr_crc32b, 4 }, |
| 43 | { attr_haval, 32 }, |
| 44 | { attr_whirlpool, 64 }, |
| 45 | { attr_gostr3411_94, 32 }, |
| 46 | { attr_stribog256, 32 }, |
| 47 | { attr_stribog512, 64 }, |
| 48 | { attr_sha512_256, 32 }, |
| 49 | { attr_sha3_256, 32 }, |
| 50 | { attr_sha3_512, 64 }, |
| 51 | }; |
| 52 | |
| 53 | DB_ATTR_TYPE DEPRECATED_HASHES = ATTR(attr_md5)|ATTR(attr_sha1)|ATTR(attr_rmd160)|ATTR(attr_gostr3411_94); |
| 54 | DB_ATTR_TYPE REMOVED_HASHES = ATTR(attr_crc32)|ATTR(attr_crc32b)|ATTR(attr_haval)|ATTR(attr_tiger)|ATTR(attr_whirlpool); |
| 55 | |
| 56 | #ifdef WITH_NETTLE |
| 57 | int algorithms[] = { /* order must match hashsums array */ |
| 58 | 1, /* md5 */ |
| 59 | 1, /* sha1 */ |
| 60 | 1, /* sha256 */ |
| 61 | 1, /* sha512 */ |
| 62 | 1, /* rmd160 */ |
| 63 | -1, /* tiger NOT available */ |
| 64 | -1, /* crc32 NOT available */ |
| 65 | -1, /* crc32b NOT available */ |
| 66 | -1, /* haval NOT available */ |
| 67 | -1, /* whirlpool NOT available */ |
| 68 | 1, /* gost */ |
| 69 | 1, /* stribog256 */ |
| 70 | 1, /* stribog512 */ |
| 71 | 1, /* sha512_256 */ |
| 72 | 1, /* sha3-256 */ |
| 73 | 1, /* sha3-512 */ |
| 74 | }; |
| 75 | #endif |
| 76 | |
| 77 | #ifdef WITH_GCRYPT |
| 78 | int algorithms[] = { /* order must match hashsums array */ |
| 79 | GCRY_MD_MD5, |
| 80 | GCRY_MD_SHA1, |
| 81 | GCRY_MD_SHA256, |
| 82 | GCRY_MD_SHA512, |
| 83 | GCRY_MD_RMD160, |
| 84 | -1, /* tiger is no longer supported by aide */ |
| 85 | -1, /* crc32 is no longer supported by aide */ |
| 86 | -1, /* CRC32B is not available */ |
| 87 | -1, /* GCRY_MD_HAVAL is not (yet) implemented */ |
| 88 | -1, /* whirlpool is no longer supported by aide */ |
| 89 | GCRY_MD_GOSTR3411_94, |
| 90 | GCRY_MD_STRIBOG256, |
| 91 | GCRY_MD_STRIBOG512, |
| 92 | GCRY_MD_SHA512_256, |
| 93 | GCRY_MD_SHA3_256, |
| 94 | GCRY_MD_SHA3_512, |
| 95 | }; |
| 96 | #endif |
| 97 | |
| 98 | void init_hashsum_lib(void) { |
| 99 | #ifdef WITH_GCRYPT |
| 100 | if(!gcry_check_version(NEED_LIBGCRYPT_VERSION)) { |
| 101 | log_msg(LOG_LEVEL_ERROR, "libgcrypt is too old (need %s, have %s)", NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL)); |
| 102 | exit(VERSION_MISMATCH_ERROR); |
| 103 | } |
| 104 | gcry_control(GCRYCTL_DISABLE_SECMEM, 0); |
| 105 | gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); |
| 106 | if (gcry_fips_mode_active()) { |
| 107 | char* str; |
| 108 | log_msg(LOG_LEVEL_NOTICE, "libgcrypt is running in FIPS mode, the following hash(es) are not available: %s", str = diff_attributes(0, ATTR(attr_md5))); |
| 109 | free(str); |
| 110 | } |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | DB_ATTR_TYPE get_hashes(bool include_unsupported) { |
| 115 | DB_ATTR_TYPE attr = 0LLU; |
| 116 | for (int i = 0; i < num_hashes; ++i) { |
| 117 | if (include_unsupported || (algorithms[i] >= 0 |
| 118 | #ifdef WITH_GCRYPT |
| 119 | && (algorithms[i] != GCRY_MD_MD5 || ! gcry_fips_mode_active()) |
| 120 | #endif |
| 121 | )) { |
| 122 | attr |= ATTR(hashsums[i].attribute); |
| 123 | } |
| 124 | } |
| 125 | return attr; |
| 126 | } |
| 127 | |
| 128 | DB_ATTR_TYPE validate_hashes(DB_ATTR_TYPE attrs, int linenumber, char *filename, char* linebuf) { |
| 129 | char *attr_str; |
| 130 | |
| 131 | DB_ATTR_TYPE requested_hashes = attrs&get_hashes(true); |
| 132 | |
| 133 | DB_ATTR_TYPE requested_deprecated = requested_hashes&DEPRECATED_HASHES; |
| 134 | if (requested_deprecated) { |
| 135 | attr_str = diff_attributes(0, requested_deprecated); |
| 136 | LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_NOTICE, "hashsum(s) '%s' are DEPRECATED and will be removed in the release after next, please update your config", attr_str) |
| 137 | free(attr_str); |
| 138 | } |
| 139 | DB_ATTR_TYPE requested_unsupported = requested_hashes&REMOVED_HASHES; |
| 140 | if (requested_unsupported) { |
| 141 | DB_ATTR_TYPE remaining_hashes = requested_hashes & ~requested_unsupported; |
| 142 | if (remaining_hashes) { |
| 143 | attr_str = diff_attributes(0, requested_unsupported); |
| 144 | char *remaining_str = diff_attributes(0, remaining_hashes); |
| 145 | LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_WARNING, "ignore unsupported hashsum(s): %s (remaining hashsum(s): %s), please update your config", attr_str, remaining_str); |
| 146 | attrs &= ~requested_unsupported; |
| 147 | free(remaining_str); |
| 148 | free(attr_str); |
| 149 | } else { |
| 150 | attr_str = diff_attributes(0, requested_unsupported); |
| 151 | LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "no hashsum(s) left after ignoring unsupported hashsum(s): %s", attr_str); |
| 152 | free(attr_str); |
| 153 | exit(INVALID_CONFIGURELINE_ERROR); |
| 154 | } |
| 155 | } |
| 156 | return attrs; |
| 157 | } |
| 158 | |
| 159 | DB_ATTR_TYPE get_transition_hashsums(char *old_filename, DB_ATTR_TYPE old_attr, char *new_filename, DB_ATTR_TYPE new_attr) { |
| 160 | DB_ATTR_TYPE transition_hashsums = 0LL; |
| 161 | |
| 162 | DB_ATTR_TYPE all_hashsums = get_hashes(true); |
| 163 | DB_ATTR_TYPE available_hashsums = get_hashes(false); |
| 164 | |
| 165 | DB_ATTR_TYPE added_hashsums = (~old_attr)&new_attr&all_hashsums; |
| 166 | DB_ATTR_TYPE removed_hashsums = old_attr&~(new_attr)&all_hashsums; |
| 167 | if (added_hashsums && removed_hashsums) { |
| 168 | char *diff_str = diff_attributes(old_attr&all_hashsums, new_attr&all_hashsums); |
| 169 | DB_ATTR_TYPE common_hashsums = old_attr&new_attr&available_hashsums; |
| 170 | if (common_hashsums == 0) { |
| 171 | transition_hashsums = removed_hashsums&available_hashsums; |
| 172 | if (transition_hashsums) { |
| 173 | char *trans_str = diff_attributes(0, transition_hashsums); |
| 174 | log_msg(LOG_LEVEL_COMPARE, "hashsum transition (%s) for old:'%s' and new:'%s': transition hashsum(s): %s", diff_str, old_filename, new_filename, trans_str); |
| 175 | free(trans_str); |
| 176 | } else { |
| 177 | log_msg(LOG_LEVEL_WARNING, "hashsum transition (%s) for old:'%s' and new:'%s': no common or transition hashsum(s) available", diff_str, old_filename, new_filename); |
| 178 | } |
| 179 | } else { |
| 180 | transition_hashsums = common_hashsums; |
| 181 | char *common_str = diff_attributes(0, common_hashsums); |
| 182 | log_msg(LOG_LEVEL_COMPARE, "hashsum transition (%s) for old:'%s' and new:'%s': common hashsum(s): %s", diff_str, old_filename, new_filename, common_str); |
| 183 | free(common_str); |
| 184 | } |
| 185 | free(diff_str); |
| 186 | } |
| 187 | return transition_hashsums; |
| 188 | } |
| 189 | |
| 190 | DB_ATTR_TYPE get_hashsums_to_ignore(char *old_filename, DB_ATTR_TYPE old_attr, char *new_filename, DB_ATTR_TYPE new_attr) { |
| 191 | DB_ATTR_TYPE hashsums_to_ignore = 0LL; |
| 192 | |
| 193 | DB_ATTR_TYPE all_hashsums = get_hashes(true); |
| 194 | DB_ATTR_TYPE available_hashsums = get_hashes(false); |
| 195 | |
| 196 | DB_ATTR_TYPE added_hashsums = (~old_attr)&new_attr&all_hashsums; |
| 197 | DB_ATTR_TYPE removed_hashsums = old_attr&~(new_attr)&all_hashsums; |
| 198 | if (added_hashsums && removed_hashsums) { |
| 199 | char *diff_str = diff_attributes(old_attr&all_hashsums, new_attr&all_hashsums); |
| 200 | DB_ATTR_TYPE common_hashsums = old_attr&new_attr&available_hashsums; |
| 201 | if (common_hashsums) { |
| 202 | hashsums_to_ignore = added_hashsums^removed_hashsums; |
| 203 | char *common_str = diff_attributes(0, common_hashsums); |
| 204 | log_msg(LOG_LEVEL_COMPARE, "│ hashsum transition (%s) for old:'%s' and new:'%s', common hashsum(s): %s", diff_str, old_filename, new_filename, common_str); |
| 205 | free(common_str); |
| 206 | } else { |
| 207 | log_msg(LOG_LEVEL_DEBUG, "│ hashsum transition (%s) for old:'%s' and new:'%s', no common hashsum available", diff_str, old_filename, new_filename); |
| 208 | } |
| 209 | free(diff_str); |
| 210 | } |
| 211 | return hashsums_to_ignore; |
| 212 | } |