| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2002, 2005-2006, 2010, 2019-2025 Rami Lehti, |
| 5 | * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h"cxx . . $ `<file>/include/config.h`/ |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include "attributes.h"cxx . . $ `<file>/include/attributes.h`/ |
| 27 | #include "db_line.h"cxx . . $ `<file>/include/db_line.h`/ |
| 28 | #include "db_config.h"cxx . . $ `<file>/include/db_config.h`/ |
| 29 | #include "base64.h"cxx . . $ `<file>/include/base64.h`/ |
| 30 | #include "hashsum.h"cxx . . $ `<file>/include/hashsum.h`/ |
| 31 | #include "log.h"cxx . . $ `<file>/include/log.h`/ |
| 32 | #include "md.h"cxx . . $ `<file>/include/md.h`/ |
| 33 | #include "util.h"cxx . . $ `<file>/include/util.h`/ |
| 34 | #include "errorcodes.h"cxx . . $ `<file>/include/errorcodes.h`/ |
| 35 | |
| 36 | #ifdef WITH_GCRYPT |
| 37 | #include <gcrypt.h> |
| 38 | #endif |
| 39 | |
| 40 | #ifdef WITH_NETTLEcxx . . $ `./include/config.h:254:9`! |
| 41 | #include <nettle/nettle-types.h> |
| 42 | #include <nettle/md5.h> |
| 43 | #include <nettle/sha1.h> |
| 44 | #include <nettle/sha2.h> |
| 45 | #include <nettle/sha3.h> |
| 46 | #include <nettle/ripemd160.h> |
| 47 | #include <nettle/gosthash94.h> |
| 48 | #include <nettle/streebog.h> |
| 49 | |
| 50 | #include <nettle/version.h> |
| 51 | |
| 52 | typedef structcxx . . $ $anonymous_type_2b7faab42144e14_0# { |
| 53 | nettle_hash_init_funccxx . . $ nettle_hash_init_func# *initcxx . . $ $anonymous_type_2b7faab42144e14_0#init.; |
| 54 | nettle_hash_update_funccxx . . $ nettle_hash_update_func# *updatecxx . . $ $anonymous_type_2b7faab42144e14_0#update.; |
| 55 | nettle_hash_digest_funccxx . . $ nettle_hash_digest_func# *digestcxx . . $ $anonymous_type_2b7faab42144e14_0#digest.; |
| 56 | } nettle_fucntions_tcxx . . $ nettle_fucntions_t#; |
| 57 | |
| 58 | nettle_fucntions_tcxx . . $ nettle_fucntions_t# nettle_functionscxx . . $ nettle_functions.[] = { /* order must match hashsums array */ |
| 59 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) md5_initcxx . . $ `<external>/11272356983953097851/md5.h:44:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) md5_updatecxx . . $ `<external>/11272356983953097851/md5.h:45:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) md5_digestcxx . . $ `<external>/11272356983953097851/md5.h:46:9`! }, |
| 60 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha1_initcxx . . $ `<external>/2608364918782970970/sha1.h:44:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha1_updatecxx . . $ `<external>/2608364918782970970/sha1.h:45:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha1_digestcxx . . $ `<external>/2608364918782970970/sha1.h:46:9`! }, |
| 61 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha256_initcxx . . $ `<external>/16457224127188146744/sha2.h:46:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha256_updatecxx . . $ `<external>/16457224127188146744/sha2.h:47:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha256_digestcxx . . $ `<external>/16457224127188146744/sha2.h:48:9`! }, |
| 62 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha512_initcxx . . $ `<external>/16457224127188146744/sha2.h:52:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha512_updatecxx . . $ `<external>/16457224127188146744/sha2.h:53:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha512_digestcxx . . $ `<external>/16457224127188146744/sha2.h:54:9`! }, |
| 63 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) ripemd160_initcxx . . $ `<external>/3941896302922996870/ripemd160.h:44:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) ripemd160_updatecxx . . $ `<external>/3941896302922996870/ripemd160.h:45:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) ripemd160_digestcxx . . $ `<external>/3941896302922996870/ripemd160.h:46:9`! }, |
| 64 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 65 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 66 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 67 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 68 | { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 69 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) gosthash94_initcxx . . $ `<external>/7298742136097606593/gosthash94.h:71:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) gosthash94_updatecxx . . $ `<external>/7298742136097606593/gosthash94.h:72:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) gosthash94_digestcxx . . $ `<external>/7298742136097606593/gosthash94.h:73:9`! }, |
| 70 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) streebog256_initcxx . . $ `<external>/11383459528520575051/streebog.h:44:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) streebog256_updatecxx . . $ `<external>/11383459528520575051/streebog.h:88:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) streebog256_digestcxx . . $ `<external>/11383459528520575051/streebog.h:45:9`! }, |
| 71 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) streebog512_initcxx . . $ `<external>/11383459528520575051/streebog.h:46:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) streebog512_updatecxx . . $ `<external>/11383459528520575051/streebog.h:47:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) streebog512_digestcxx . . $ `<external>/11383459528520575051/streebog.h:48:9`! }, |
| 72 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha512_256_initcxx . . $ `<external>/16457224127188146744/sha2.h:58:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha512_256_updatecxx . . $ `<external>/16457224127188146744/sha2.h:189:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha512_256_digestcxx . . $ `<external>/16457224127188146744/sha2.h:59:9`! }, |
| 73 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha3_256_initcxx . . $ `<external>/13543988700779005385/sha3.h:52:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha3_256_updatecxx . . $ `<external>/13543988700779005385/sha3.h:53:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha3_256_digestcxx . . $ `<external>/13543988700779005385/sha3.h:54:9`! }, |
| 74 | { (nettle_hash_init_funccxx . . $ nettle_hash_init_func#*) sha3_512_initcxx . . $ `<external>/13543988700779005385/sha3.h:60:9`!, (nettle_hash_update_funccxx . . $ nettle_hash_update_func#*) sha3_512_updatecxx . . $ `<external>/13543988700779005385/sha3.h:61:9`!, (nettle_hash_digest_funccxx . . $ nettle_hash_digest_func#*) sha3_512_digestcxx . . $ `<external>/13543988700779005385/sha3.h:62:9`! }, |
| 75 | }; |
| 76 | #endif |
| 77 | |
| 78 | /* |
| 79 | Initialise md_container according its todo_attr field |
| 80 | */ |
| 81 | |
| 82 | int init_mdcxx . . $ init_md(74d27db1f5e1726).(struct md_containercxx . . $ md_container#* md, const char *filename, const char *whoami) { |
| 83 | |
| 84 | /* First we check the parameter.. */ |
| 85 | #ifdef _PARAMETER_CHECK_ |
| 86 | if (md==NULL) { |
| 87 | return RETFAIL; |
| 88 | } |
| 89 | #endif |
| 90 | /* |
| 91 | We don't have calculator for this yet :) |
| 92 | */ |
| 93 | md->calc_attrcxx . . $ md_container#calc_attr.=0; |
| 94 | #ifdef WITH_NETTLEcxx . . $ `./include/config.h:254:9`! |
| 95 | for (HASHSUMcxx . . $ HASHSUM# i = 0 ; i < num_hashescxx . . $ num_hashes. ; ++i) { |
| 96 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! h = ATTRcxx . . $ `include/attributes.h:28:9`!(hashsums[i].attribute); |
| 97 | if (h&md->todo_attrcxx . . $ md_container#todo_attr.) { |
| 98 | nettle_functionscxx . . $ nettle_functions.[i].initcxx . . $ $anonymous_type_2b7faab42144e14_0#init.(&md->ctxcxx . . $ md_container#ctx.[i]); |
| 99 | md->calc_attrcxx . . $ md_container#calc_attr.|=h; |
| 100 | } |
| 101 | } |
| 102 | #endif |
| 103 | #ifdef WITH_GCRYPT |
| 104 | if(gcry_md_open(&md->mdh,0,0)!=GPG_ERR_NO_ERROR){ |
| 105 | log_msg(LOG_LEVEL_ERROR,"gcrypt_md_open failed"); |
| 106 | exit(IO_ERROR); |
| 107 | } |
| 108 | |
| 109 | for (HASHSUM i = 0 ; i < num_hashes ; ++i) { |
| 110 | DB_ATTR_TYPE h = ATTR(hashsums[i].attribute); |
| 111 | if (h&md->todo_attr) { |
| 112 | if(gcry_md_enable(md->mdh,algorithms[i])==GPG_ERR_NO_ERROR){ |
| 113 | md->calc_attr|=h; |
| 114 | } else { |
| 115 | log_msg(LOG_LEVEL_WARNING,"%s: gcry_md_enable (%s) failed for '%s'", filename, attributes[hashsums[i].attribute].db_name, filename); |
| 116 | md->todo_attr&=~h; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | char *str; |
| 122 | LOG_WHOAMIcxx . . $ `include/log.h:73:9`!(LOG_LEVEL_DEBUG, "%s> initialized md_container: %s (%p)", filename, str = diff_attributes(0, md->calc_attr), (void*) md); |
| 123 | freecxx . . $ free(5d22bdacc48458e8).(str); |
| 124 | return RETOKcxx . . $ `include/db_config.h:45:9`!; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | update :) |
| 129 | Just call this when you have more data. |
| 130 | */ |
| 131 | |
| 132 | int update_mdcxx . . $ update_md(29e99b095fc90fd4).(struct md_containercxx . . $ md_container#* md,void* data,ssize_tcxx . . $ ssize_t# size) { |
| 133 | |
| 134 | #ifdef _PARAMETER_CHECK_ |
| 135 | if (md==NULL||data==NULL) { |
| 136 | return RETFAIL; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #ifdef WITH_NETTLEcxx . . $ `./include/config.h:254:9`! |
| 141 | for (HASHSUMcxx . . $ HASHSUM# i = 0 ; i < num_hashescxx . . $ num_hashes. ; ++i) { |
| 142 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! h = ATTRcxx . . $ `include/attributes.h:28:9`!(hashsums[i].attribute); |
| 143 | if (h&md->calc_attrcxx . . $ md_container#calc_attr.) { |
| 144 | nettle_functionscxx . . $ nettle_functions.[i].updatecxx . . $ $anonymous_type_2b7faab42144e14_0#update.(&md->ctxcxx . . $ md_container#ctx.[i], size, data); |
| 145 | } |
| 146 | } |
| 147 | #endif |
| 148 | #ifdef WITH_GCRYPT |
| 149 | gcry_md_write(md->mdh, data, size); |
| 150 | #endif |
| 151 | return RETOKcxx . . $ `include/db_config.h:45:9`!; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | close.. Does some magic. |
| 156 | After this calling update_db is not a good idea. |
| 157 | */ |
| 158 | |
| 159 | int close_mdcxx . . $ close_md(e2d4d3ac7f8219a9).(struct md_containercxx . . $ md_container#* md, md_hashsumscxx . . $ md_hashsums# * hs, const char *filename, const char *whoami) { |
| 160 | #ifdef _PARAMETER_CHECK_ |
| 161 | if (md==NULL) { |
| 162 | return RETFAIL; |
| 163 | } |
| 164 | #endif |
| 165 | if (hs) { |
| 166 | LOG_WHOAMIcxx . . $ `include/log.h:73:9`!(LOG_LEVEL_DEBUG, "%s> copy hashsums from md_container (%p)", filename, (void*) md); |
| 167 | #ifdef WITH_NETTLEcxx . . $ `./include/config.h:254:9`! |
| 168 | for (HASHSUMcxx . . $ HASHSUM# i = 0 ; i < num_hashescxx . . $ num_hashes. ; ++i) { |
| 169 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! h = ATTRcxx . . $ `include/attributes.h:28:9`!(hashsums[i].attribute); |
| 170 | if (h&md->calc_attrcxx . . $ md_container#calc_attr.) { |
| 171 | #if NETTLE_VERSION_MAJORcxx . . $ `<external>/13275623926108022261/version.h:43:9`! < 4 |
| 172 | nettle_functionscxx . . $ nettle_functions.[i].digestcxx . . $ $anonymous_type_2b7faab42144e14_0#digest.(&md->ctxcxx . . $ md_container#ctx.[i].md5cxx . . $ md_container#$anonymous_type_0#md5., hashsumscxx . . $ hashsums.[i].lengthcxx . . $ $anonymous_type_76c4ad3622101c33_0#length., hs->hashsumscxx . . $ md_hashsums#hashsums.[i]); |
| 173 | #else |
| 174 | nettle_functions[i].digest(&md->ctx[i].md5, hs->hashsums[i]); |
| 175 | #endif |
| 176 | } |
| 177 | } |
| 178 | #endif |
| 179 | #ifdef WITH_GCRYPT |
| 180 | for (HASHSUM i = 0 ; i < num_hashes ; ++i) { |
| 181 | if (md->calc_attr&ATTR(hashsums[i].attribute)) { |
| 182 | memcpy(hs->hashsums[i],gcry_md_read(md->mdh, algorithms[i]), hashsums[i].length); |
| 183 | } |
| 184 | } |
| 185 | #endif |
| 186 | hs->attrscxx . . $ md_hashsums#attrs. = md->calc_attrcxx . . $ md_container#calc_attr.; |
| 187 | } |
| 188 | LOG_WHOAMIcxx . . $ `include/log.h:73:9`!(LOG_LEVEL_DEBUG, "%s> free md_container (%p)", filename, (void*) md); |
| 189 | /* Nettle doesn’t do memory allocation */ |
| 190 | #ifdef WITH_GCRYPT |
| 191 | gcry_md_close(md->mdh); |
| 192 | #endif |
| 193 | return RETOKcxx . . $ `include/db_config.h:45:9`!; |
| 194 | } |
| 195 | |
| 196 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! copy_hashsumscxx . . $ copy_hashsums(f1f39ddc5401fb9).(char *context, md_hashsumscxx . . $ md_hashsums# *hs, bytecxx . . $ byte#* (*target)[num_hashescxx . . $ num_hashes.], const char *whoami) { |
| 197 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! disabled_hashsums = 0LL; |
| 198 | for (int i = 0 ; i < num_hashescxx . . $ num_hashes. ; ++i) { |
| 199 | DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! attr = ATTRcxx . . $ `include/attributes.h:28:9`!(hashsums[i].attribute); |
| 200 | if (hs->attrscxx . . $ md_hashsums#attrs.&attr) { |
| 201 | (*target)[i] = checked_malloccxx . . $ checked_malloc(bfcfa4d6b7f7ef64).(hashsumscxx . . $ hashsums.[i].lengthcxx . . $ $anonymous_type_76c4ad3622101c33_0#length.); |
| 202 | memcpycxx . . $ memcpy(cc817cb175f282d).((*target)[i],hs->hashsumscxx . . $ md_hashsums#hashsums.[i],hashsumscxx . . $ hashsums.[i].lengthcxx . . $ $anonymous_type_76c4ad3622101c33_0#length.); |
| 203 | char* hashsum_str = encode_base64cxx . . $ encode_base64(ea9ec9b3a5ad8ddb).((*target)[i], hashsumscxx . . $ hashsums.[i].lengthcxx . . $ $anonymous_type_76c4ad3622101c33_0#length.); |
| 204 | LOG_WHOAMIcxx . . $ `include/log.h:73:9`!(LOG_LEVEL_TRACE, "%s: copy %s hashsum (%s) to %p", context, attributes[hashsums[i].attribute].db_name, hashsum_str, (void*) (*target)[i]); |
| 205 | freecxx . . $ free(5d22bdacc48458e8). (hashsum_str); |
| 206 | } else { |
| 207 | disabled_hashsums |= attr; |
| 208 | (*target)[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 209 | } |
| 210 | } |
| 211 | return disabled_hashsums; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | Writes md_container to db_line. |
| 216 | */ |
| 217 | |
| 218 | void hashsums2linecxx . . $ hashsums2line(f5f0f94a1303d470).(md_hashsumscxx . . $ md_hashsums# *hs, struct db_linecxx . . $ db_line#* line ,const char *whoami) { |
| 219 | |
| 220 | #ifdef _PARAMETER_CHECK_ |
| 221 | if (md==NULL||line==NULL) { |
| 222 | return RETFAIL; |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | line->attrcxx . . $ db_line#attr. &= ~(copy_hashsumscxx . . $ copy_hashsums(f1f39ddc5401fb9).(line->filenamecxx . . $ db_line#filename., hs, &line->hashsumscxx . . $ db_line#hashsums., whoami)); |
| 227 | |
| 228 | } |