]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/database.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / database.c
index 419794e6450f559240fc255b53540ca21f9c8e0a..617b61eb5a8fe5966136aa54c7cc690579fa2bba 100644 (file)
@@ -480,7 +480,7 @@ static void loc_database_free(struct loc_database* db) {
 
 LOC_EXPORT int loc_database_new(struct loc_ctx* ctx, struct loc_database** database, FILE* f) {
        struct loc_database* db = NULL;
-       int r;
+       int r = 1;
 
        // Fail on invalid file handle
        if (!f) {
@@ -641,8 +641,11 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
                }
        }
 
+       int sig1_valid = 0;
+       int sig2_valid = 0;
+
        // Check first signature
-       if (db->signature1.data) {
+       if (db->signature1.length) {
                hexdump(db->ctx, db->signature1.data, db->signature1.length);
 
                r = EVP_DigestVerifyFinal(mdctx,
@@ -650,19 +653,19 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
 
                if (r == 0) {
                        DEBUG(db->ctx, "The first signature is invalid\n");
-                       r = 1;
                } else if (r == 1) {
                        DEBUG(db->ctx, "The first signature is valid\n");
-                       r = 0;
+                       sig1_valid = 1;
                } else {
                        ERROR(db->ctx, "Error verifying the first signature: %s\n",
                                ERR_error_string(ERR_get_error(), NULL));
                        r = -1;
+                       goto CLEANUP;
                }
        }
 
        // Check second signature only when the first one was invalid
-       if (r && db->signature2.data) {
+       if (db->signature2.length) {
                hexdump(db->ctx, db->signature2.data, db->signature2.length);
 
                r = EVP_DigestVerifyFinal(mdctx,
@@ -670,14 +673,14 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
 
                if (r == 0) {
                        DEBUG(db->ctx, "The second signature is invalid\n");
-                       r = 1;
                } else if (r == 1) {
                        DEBUG(db->ctx, "The second signature is valid\n");
-                       r = 0;
+                       sig2_valid = 1;
                } else {
                        ERROR(db->ctx, "Error verifying the second signature: %s\n",
                                ERR_error_string(ERR_get_error(), NULL));
                        r = -1;
+                       goto CLEANUP;
                }
        }
 
@@ -685,6 +688,12 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
        INFO(db->ctx, "Signature checked in %.4fms\n",
                (double)(end - start) / CLOCKS_PER_SEC * 1000);
 
+       // Check if at least one signature as okay
+       if (sig1_valid || sig2_valid)
+               r = 0;
+       else
+               r = 1;
+
 CLEANUP:
        // Cleanup
        EVP_MD_CTX_free(mdctx);