| 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" |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include "attributes.h" |
| 27 | #include "db_line.h" |
| 28 | #include "db_config.h" |
| 29 | #include "base64.h" |
| 30 | #include "hashsum.h" |
| 31 | #include "log.h" |
| 32 | #include "md.h" |
| 33 | #include "util.h" |
| 34 | #include "errorcodes.h" |
| 35 | |
| 36 | #ifdef WITH_GCRYPT |
| 37 | #include <gcrypt.h> |
| 38 | #endif |
| 39 | |
| 40 | #ifdef WITH_NETTLE |
| 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 struct { |
| 53 | nettle_hash_init_func *init; |
| 54 | nettle_hash_update_func *update; |
| 55 | nettle_hash_digest_func *digest; |
| 56 | } nettle_fucntions_t; |
| 57 | |
| 58 | nettle_fucntions_t nettle_functions[] = { /* order must match hashsums array */ |
| 59 | { (nettle_hash_init_func*) md5_init, (nettle_hash_update_func*) md5_update, (nettle_hash_digest_func*) md5_digest }, |
| 60 | { (nettle_hash_init_func*) sha1_init, (nettle_hash_update_func*) sha1_update, (nettle_hash_digest_func*) sha1_digest }, |
| 61 | { (nettle_hash_init_func*) sha256_init, (nettle_hash_update_func*) sha256_update, (nettle_hash_digest_func*) sha256_digest }, |
| 62 | { (nettle_hash_init_func*) sha512_init, (nettle_hash_update_func*) sha512_update, (nettle_hash_digest_func*) sha512_digest }, |
| 63 | { (nettle_hash_init_func*) ripemd160_init, (nettle_hash_update_func*) ripemd160_update, (nettle_hash_digest_func*) ripemd160_digest }, |
| 64 | { NULL, NULL, NULL }, |
| 65 | { NULL, NULL, NULL }, |
| 66 | { NULL, NULL, NULL }, |
| 67 | { NULL, NULL, NULL }, |
| 68 | { NULL, NULL, NULL }, |
| 69 | { (nettle_hash_init_func*) gosthash94_init, (nettle_hash_update_func*) gosthash94_update, (nettle_hash_digest_func*) gosthash94_digest }, |
| 70 | { (nettle_hash_init_func*) streebog256_init, (nettle_hash_update_func*) streebog256_update, (nettle_hash_digest_func*) streebog256_digest }, |
| 71 | { (nettle_hash_init_func*) streebog512_init, (nettle_hash_update_func*) streebog512_update, (nettle_hash_digest_func*) streebog512_digest }, |
| 72 | { (nettle_hash_init_func*) sha512_256_init, (nettle_hash_update_func*) sha512_256_update, (nettle_hash_digest_func*) sha512_256_digest }, |
| 73 | { (nettle_hash_init_func*) sha3_256_init, (nettle_hash_update_func*) sha3_256_update, (nettle_hash_digest_func*) sha3_256_digest }, |
| 74 | { (nettle_hash_init_func*) sha3_512_init, (nettle_hash_update_func*) sha3_512_update, (nettle_hash_digest_func*) sha3_512_digest }, |
| 75 | }; |
| 76 | #endif |
| 77 | |
| 78 | /* |
| 79 | Initialise md_container according its todo_attr field |
| 80 | */ |
| 81 | |
| 82 | int init_md(struct 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_attr=0; |
| 94 | #ifdef WITH_NETTLE |
| 95 | for (HASHSUM i = 0 ; i < num_hashes ; ++i) { |
| 96 | DB_ATTR_TYPE h = ATTR(hashsums[i].attribute); |
| 97 | if (h&md->todo_attr) { |
| 98 | nettle_functions[i].init(&md->ctx[i]); |
| 99 | md->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_WHOAMI(LOG_LEVEL_DEBUG, "%s> initialized md_container: %s (%p)", filename, str = diff_attributes(0, md->calc_attr), (void*) md); |
| 123 | free(str); |
| 124 | return RETOK; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | update :) |
| 129 | Just call this when you have more data. |
| 130 | */ |
| 131 | |
| 132 | int update_md(struct md_container* md,void* data,ssize_t size) { |
| 133 | |
| 134 | #ifdef _PARAMETER_CHECK_ |
| 135 | if (md==NULL||data==NULL) { |
| 136 | return RETFAIL; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #ifdef WITH_NETTLE |
| 141 | for (HASHSUM i = 0 ; i < num_hashes ; ++i) { |
| 142 | DB_ATTR_TYPE h = ATTR(hashsums[i].attribute); |
| 143 | if (h&md->calc_attr) { |
| 144 | nettle_functions[i].update(&md->ctx[i], size, data); |
| 145 | } |
| 146 | } |
| 147 | #endif |
| 148 | #ifdef WITH_GCRYPT |
| 149 | gcry_md_write(md->mdh, data, size); |
| 150 | #endif |
| 151 | return RETOK; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | close.. Does some magic. |
| 156 | After this calling update_db is not a good idea. |
| 157 | */ |
| 158 | |
| 159 | int close_md(struct md_container* md, 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_WHOAMI(LOG_LEVEL_DEBUG, "%s> copy hashsums from md_container (%p)", filename, (void*) md); |
| 167 | #ifdef WITH_NETTLE |
| 168 | for (HASHSUM i = 0 ; i < num_hashes ; ++i) { |
| 169 | DB_ATTR_TYPE h = ATTR(hashsums[i].attribute); |
| 170 | if (h&md->calc_attr) { |
| 171 | #if NETTLE_VERSION_MAJOR < 4 |
| 172 | nettle_functions[i].digest(&md->ctx[i].md5, hashsums[i].length, hs->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->attrs = md->calc_attr; |
| 187 | } |
| 188 | LOG_WHOAMI(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 RETOK; |
| 194 | } |
| 195 | |
| 196 | DB_ATTR_TYPE copy_hashsums(char *context, md_hashsums *hs, byte* (*target)[num_hashes], const char *whoami) { |
| 197 | DB_ATTR_TYPE disabled_hashsums = 0LL; |
| 198 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 199 | DB_ATTR_TYPE attr = ATTR(hashsums[i].attribute); |
| 200 | if (hs->attrs&attr) { |
| 201 | (*target)[i] = checked_malloc(hashsums[i].length); |
| 202 | memcpy((*target)[i],hs->hashsums[i],hashsums[i].length); |
| 203 | char* hashsum_str = encode_base64((*target)[i], hashsums[i].length); |
| 204 | LOG_WHOAMI(LOG_LEVEL_TRACE, "%s: copy %s hashsum (%s) to %p", context, attributes[hashsums[i].attribute].db_name, hashsum_str, (void*) (*target)[i]); |
| 205 | free (hashsum_str); |
| 206 | } else { |
| 207 | disabled_hashsums |= attr; |
| 208 | (*target)[i] = NULL; |
| 209 | } |
| 210 | } |
| 211 | return disabled_hashsums; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | Writes md_container to db_line. |
| 216 | */ |
| 217 | |
| 218 | void hashsums2line(md_hashsums *hs, struct 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->attr &= ~(copy_hashsums(line->filename, hs, &line->hashsums, whoami)); |
| 227 | |
| 228 | } |