chantra / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
45afda
From 0a7318ab4467d3156723c7a265dbd3456b8d1e20 Mon Sep 17 00:00:00 2001
45afda
From: Panu Matilainen <pmatilai@redhat.com>
45afda
Date: Tue, 10 Oct 2017 14:44:18 +0300
45afda
Subject: [PATCH 07/33] Use rpm file info sets instead of header for retrieving
45afda
 file data
45afda
45afda
Simplifies the code a little, but more imporantly it avoids duplicating
45afda
code and special knowledge like the default digest algo and converting
45afda
hex to binary. As a side-effect, this fixes RPMTAG_FILESIGNATURELENGTH
45afda
inadvertly getting added into packages that have no files at all.
45afda
---
45afda
 sign/rpmsignfiles.c | 36 +++++++++++++++++-------------------
45afda
 1 file changed, 17 insertions(+), 19 deletions(-)
45afda
45afda
diff --git a/sign/rpmsignfiles.c b/sign/rpmsignfiles.c
45afda
index c1d227a07..de7a73cfd 100644
45afda
--- a/sign/rpmsignfiles.c
45afda
+++ b/sign/rpmsignfiles.c
45afda
@@ -8,7 +8,7 @@
45afda
 #include "imaevm.h"
45afda
 
45afda
 #include <rpm/rpmlog.h>		/* rpmlog */
45afda
-#include <rpm/rpmstring.h>	/* rnibble */
45afda
+#include <rpm/rpmfi.h>
45afda
 #include <rpm/rpmpgp.h>		/* rpmDigestLength */
45afda
 #include "lib/header.h"		/* HEADERGET_MINMEM */
45afda
 #include "lib/rpmtypes.h"	/* rpmRC */
45afda
@@ -32,7 +32,7 @@ static const char *hash_algo_name[] = {
45afda
 
45afda
 #define ARRAY_SIZE(a)  (sizeof(a) / sizeof(a[0]))
45afda
 
45afda
-static char *signFile(const char *algo, const char *fdigest, int diglen,
45afda
+static char *signFile(const char *algo, const uint8_t *fdigest, int diglen,
45afda
 const char *key, char *keypass)
45afda
 {
45afda
     char *fsignature;
45afda
@@ -40,15 +40,11 @@ const char *key, char *keypass)
45afda
     unsigned char signature[MAX_SIGNATURE_LENGTH];
45afda
     int siglen;
45afda
 
45afda
-    /* convert file digest hex to binary */
45afda
-    memset(digest, 0, diglen);
45afda
     /* some entries don't have a digest - we return an empty signature */
45afda
-    if (strlen(fdigest) != diglen * 2)
45afda
+    memset(digest, 0, diglen);
45afda
+    if (memcmp(digest, fdigest, diglen) == 0)
45afda
         return strdup("");
45afda
 
45afda
-    for (int i = 0; i < diglen; ++i, fdigest += 2)
45afda
-	digest[i] = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
45afda
-
45afda
     /* prepare file signature */
45afda
     memset(signature, 0, MAX_SIGNATURE_LENGTH);
45afda
     signature[0] = '\x03';
45afda
@@ -82,21 +78,23 @@ char *keypass)
45afda
 
45afda
 rpmRC rpmSignFiles(Header sigh, Header h, const char *key, char *keypass)
45afda
 {
45afda
-    struct rpmtd_s digests, td;
45afda
+    struct rpmtd_s td;
45afda
     int algo;
45afda
     int diglen;
45afda
     uint32_t siglen;
45afda
     const char *algoname;
45afda
-    const char *digest;
45afda
+    const uint8_t *digest;
45afda
     char *signature = NULL;
45afda
     rpmRC rc = RPMRC_FAIL;
45afda
+    rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, RPMFI_FLAGS_QUERY);
45afda
+
45afda
+    if (rpmfiFC(fi) == 0) {
45afda
+	rc = RPMRC_OK;
45afda
+	goto exit;
45afda
+    }
45afda
 
45afda
-    rpmtdReset(&digests);
45afda
-    algo = headerGetNumber(h, RPMTAG_FILEDIGESTALGO);
45afda
-    if (!algo) {
45afda
-        /* use default algorithm */
45afda
-        algo = PGPHASHALGO_MD5;
45afda
-    } else if (algo < 0 || algo >= ARRAY_SIZE(hash_algo_name)) {
45afda
+    algo = rpmfiDigestAlgo(fi);
45afda
+    if (algo >= ARRAY_SIZE(hash_algo_name)) {
45afda
 	rpmlog(RPMLOG_ERR, _("File digest algorithm id is invalid"));
45afda
 	goto exit;
45afda
     }
45afda
@@ -125,8 +123,8 @@ rpmRC rpmSignFiles(Header sigh, Header h, const char *key, char *keypass)
45afda
     td.data = NULL; /* set in the loop below */
45afda
     td.count = 1;
45afda
 
45afda
-    headerGet(h, RPMTAG_FILEDIGESTS, &digests, HEADERGET_MINMEM);
45afda
-    while ((digest = rpmtdNextString(&digests))) {
45afda
+    while (rpmfiNext(fi) >= 0) {
45afda
+	digest = rpmfiFDigest(fi, NULL, NULL);
45afda
 	signature = signFile(algoname, digest, diglen, key, keypass);
45afda
 	if (!signature) {
45afda
 	    rpmlog(RPMLOG_ERR, _("signFile failed\n"));
45afda
@@ -143,6 +141,6 @@ rpmRC rpmSignFiles(Header sigh, Header h, const char *key, char *keypass)
45afda
 
45afda
 exit:
45afda
     free(signature);
45afda
-    rpmtdFreeData(&digests);
45afda
+    rpmfiFree(fi);
45afda
     return rc;
45afda
 }
45afda
-- 
45afda
2.27.0
45afda