]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
archive: Add buckets for digests
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 12 Jul 2021 17:47:25 +0000 (17:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 12 Jul 2021 17:47:25 +0000 (17:47 +0000)
To be able to read multiple digests in one go, we simply add multiple
entries to the same checksum object.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c
src/libpakfire/include/pakfire/archive.h

index 50b93a9fba251a8e73da769391bc050630d687e1..be73cb8dfa9ae5f13b49dcba0a3f281127f96d90 100644 (file)
@@ -56,8 +56,9 @@ struct pakfire_archive_chksum {
        STAILQ_ENTRY(pakfire_archive_chksum) nodes;
 
        char path[PATH_MAX];
-       archive_checksum_algo_t algo;
-       unsigned char digest[EVP_MAX_MD_SIZE];
+
+       unsigned char digest_sha512[64];
+       unsigned char digest_sha256[32];
 };
 
 struct pakfire_archive {
@@ -316,18 +317,6 @@ ERROR:
 
 // Checksum Stuff
 
-static const char* checksum_algo_string(archive_checksum_algo_t algo) {
-       switch (algo) {
-               case PAKFIRE_CHECKSUM_SHA512:
-                       return "SHA512";
-
-               case PAKFIRE_CHECKSUM_UNKNOWN:
-                       return "UNKNOWN";
-       }
-
-       return NULL;
-}
-
 static int read_hexdigest(unsigned char* dst, size_t l, const char* src) {
        const char* p = src;
 
@@ -343,9 +332,21 @@ static int read_hexdigest(unsigned char* dst, size_t l, const char* src) {
 }
 
 static int pakfire_archive_add_chksum(struct pakfire_archive* archive, const char* path,
-               archive_checksum_algo_t algo, const char* hexdigest) {
+               const char* digest_sha512, const char* digest_sha256) {
        int r = 1;
 
+       // Path must be set
+       if (!path) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       // At least one of the digests must be set
+       if (!digest_sha512 && !digest_sha256) {
+               errno = EINVAL;
+               return 1;
+       }
+
        // Allocate a new chksum object
        struct pakfire_archive_chksum* chksum = calloc(1, sizeof(*chksum));
        if (!chksum)
@@ -356,19 +357,18 @@ static int pakfire_archive_add_chksum(struct pakfire_archive* archive, const cha
        if (r < 0)
                goto ERROR;
 
-       // Store algo
-       chksum->algo = algo;
+       // SHA512
+       if (digest_sha512)
+               memcpy(chksum->digest_sha512, digest_sha512, sizeof(chksum->digest_sha512));
 
-       // Store hexdigest as digest
-       r = read_hexdigest(chksum->digest, sizeof(chksum->digest), hexdigest);
-       if (r)
-               goto ERROR;
+       // SHA256
+       if (digest_sha256)
+               memcpy(chksum->digest_sha256, digest_sha256, sizeof(chksum->digest_sha256));
 
        // Append it
        STAILQ_INSERT_TAIL(&archive->chksums, chksum, nodes);
 
-       DEBUG(archive->pakfire, "Read checksum for %s - %s:%s\n",
-               path, checksum_algo_string(algo), hexdigest);
+       DEBUG(archive->pakfire, "Added checksum for %s\n", path);
 
        return 0;
 
@@ -538,7 +538,6 @@ static int pakfire_archive_parse_entry_checksums(struct pakfire_archive* archive
 
        const char* filename = NULL;
        const char* checksum = NULL;
-       archive_checksum_algo_t algo = PAKFIRE_CHECKSUM_SHA512;
 
        char* p = data;
        while (*p) {
@@ -568,7 +567,7 @@ static int pakfire_archive_parse_entry_checksums(struct pakfire_archive* archive
 
                // Add new checksum object
                if (filename && checksum) {
-                       r = pakfire_archive_add_chksum(archive, filename, algo, checksum);
+                       r = pakfire_archive_add_chksum(archive, filename, checksum, NULL);
                        if (r)
                                goto ERROR;
                }
@@ -1136,15 +1135,7 @@ static pakfire_archive_verify_status_t pakfire_archive_verify_file(struct pakfir
        EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
 
        // Select algorithm
-       switch (chksum->algo) {
-               case PAKFIRE_CHECKSUM_SHA512:
-                       md = EVP_sha512();
-                       break;
-
-               default:
-                       ERROR(pakfire, "Unknown algorithm chosen\n");
-                       goto ERROR;
-       }
+       md = EVP_sha512();
 
        // Initialise the hash algorithm
        r = EVP_DigestInit_ex(mdctx, md, NULL);
@@ -1186,7 +1177,7 @@ static pakfire_archive_verify_status_t pakfire_archive_verify_file(struct pakfir
        }
 
        // Compare digests
-       if (CRYPTO_memcmp(digest, chksum->digest, EVP_MD_CTX_size(mdctx)) == 0) {
+       if (CRYPTO_memcmp(digest, chksum->digest_sha512, EVP_MD_CTX_size(mdctx)) == 0) {
                DEBUG(pakfire, "Checksum of %s is OK\n", chksum->path);
                status = PAKFIRE_ARCHIVE_VERIFY_OK;
        } else {
index 17641846cf4ca5af9f5c3e2333c46ffe0f9f6f45..63fd3b0a197f1f9478b768fba17764b0d72189c2 100644 (file)
@@ -79,11 +79,6 @@ struct pakfire_package* pakfire_archive_make_package(
 struct pakfire_scriptlet* pakfire_archive_get_scriptlet(
        struct pakfire_archive* archive, const char* type);
 
-typedef enum archive_checksum_algo {
-       PAKFIRE_CHECKSUM_UNKNOWN = 0,
-       PAKFIRE_CHECKSUM_SHA512,
-} archive_checksum_algo_t;
-
 #endif
 
 #endif /* PAKFIRE_ARCHIVE_H */