chantra / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
629b27
From 7da1e826ccb08fdd404524146736b3f12a473e31 Mon Sep 17 00:00:00 2001
629b27
From: chantra <chantr4@gmail.com>
629b27
Date: Mon, 31 Jan 2022 14:42:25 -0800
629b27
Subject: [PATCH 10/30] [rpm2extents] Make rpmkeys support reading metadata
629b27
 from transcoded footer
629b27
629b27
If the file is seekable and is a transcoded file, read signature validation from transcoded file tail metadata
629b27
---
629b27
 lib/rpmchecksig.c    | 112 ++++++++++++++++++++++++++++++++++++++++++-
629b27
 tests/rpm2extents.at |  34 ++++++++++++-
629b27
 2 files changed, 144 insertions(+), 2 deletions(-)
629b27
629b27
diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c
629b27
index fcdbb424f..6164d012c 100644
629b27
--- a/lib/rpmchecksig.c
629b27
+++ b/lib/rpmchecksig.c
629b27
@@ -24,6 +24,11 @@
629b27
 
629b27
 #include "debug.h"
629b27
 
629b27
+/* magic value at end of file (64 bits) that indicates this is a transcoded
629b27
+ * rpm.
629b27
+ */
629b27
+#define MAGIC 3472329499408095051
629b27
+
629b27
 int _print_pkts = 0;
629b27
 
629b27
 static int doImport(rpmts ts, const char *fn, char *buf, ssize_t blen)
629b27
@@ -220,6 +225,107 @@ exit:
629b27
     return rc;
629b27
 }
629b27
 
629b27
+static rpmRC isTranscodedRpm(FD_t fd) {
629b27
+    rpmRC rc = RPMRC_NOTFOUND;
629b27
+    rpm_loff_t current;
629b27
+    uint64_t magic;
629b27
+    size_t len;
629b27
+
629b27
+    // If the file is not seekable, we cannot detect whether or not it is transcoded.
629b27
+    if(Fseek(fd, 0, SEEK_CUR) < 0) {
629b27
+        return RPMRC_FAIL;
629b27
+    }
629b27
+    current = Ftell(fd);
629b27
+
629b27
+    if(Fseek(fd, -(sizeof(magic)), SEEK_END) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: failed to seek for magic\n"));
629b27
+	rc = RPMRC_FAIL;
629b27
+	goto exit;
629b27
+    }
629b27
+    len = sizeof(magic);
629b27
+    if (Fread(&magic, len, 1, fd) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to read magic\n"));
629b27
+	rc = RPMRC_FAIL;
629b27
+	goto exit;
629b27
+    }
629b27
+    if (magic != MAGIC) {
629b27
+	rpmlog(RPMLOG_DEBUG, _("isTranscodedRpm: not transcoded\n"));
629b27
+	rc = RPMRC_NOTFOUND;
629b27
+	goto exit;
629b27
+    }
629b27
+    rc = RPMRC_OK;
629b27
+exit:
629b27
+    if (Fseek(fd, current, SEEK_SET) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to seek back to original location\n"));
629b27
+    }
629b27
+    return rc;
629b27
+}
629b27
+
629b27
+static int rpmpkgVerifySigsTranscoded(FD_t fd){
629b27
+    rpm_loff_t current;
629b27
+    uint64_t magic;
629b27
+    rpm_loff_t offset;
629b27
+    int32_t rc;
629b27
+    size_t len;
629b27
+    uint64_t content_len;
629b27
+    char *content = NULL;
629b27
+
629b27
+    current = Ftell(fd);
629b27
+
629b27
+    if(Fseek(fd, -(sizeof(magic) + 3 * sizeof(offset) ), SEEK_END) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: failed to seek for offset\n"));
629b27
+	rc = -1;
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    len = sizeof(offset);
629b27
+    if (Fread(&offset, len, 1, fd) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to read Signature Verification offset\n"));
629b27
+	rc = -1;
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    if(Fseek(fd,  offset, SEEK_SET) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to seek signature verification offset\n"));
629b27
+	rc = -1;
629b27
+	goto exit;
629b27
+    }
629b27
+    len = sizeof(rc);
629b27
+    if (Fread(&rc, len, 1, fd) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to read Signature Verification RC\n"));
629b27
+	rc = -1;
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    len = sizeof(content_len);
629b27
+    if (Fread(&content_len, len, 1, fd) != len) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to read signature content length\n"));
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    content = malloc(content_len + 1);
629b27
+    if(content == NULL) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to allocate memory to read signature content\n"));
629b27
+	goto exit;
629b27
+    }
629b27
+    content[content_len] = 0;
629b27
+    if (Fread(content, content_len, 1, fd) != content_len) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: Failed to read signature content\n"));
629b27
+	goto exit;
629b27
+    }
629b27
+
629b27
+    rpmlog(RPMLOG_NOTICE, "%s", content);
629b27
+exit:
629b27
+    if(content){
629b27
+	free(content);
629b27
+    }
629b27
+    if (Fseek(fd, current, SEEK_SET) < 0) {
629b27
+	rpmlog(RPMLOG_ERR, _("rpmpkgVerifySigsTranscoded: unable to seek back to original location\n"));
629b27
+    }
629b27
+    return rc;
629b27
+
629b27
+}
629b27
+
629b27
 static int rpmpkgVerifySigs(rpmKeyring keyring, int vfylevel, rpmVSFlags flags,
629b27
 			   FD_t fd, const char *fn)
629b27
 {
629b27
@@ -229,10 +335,14 @@ static int rpmpkgVerifySigs(rpmKeyring keyring, int vfylevel, rpmVSFlags flags,
629b27
 			    .verbose = rpmIsVerbose(),
629b27
     };
629b27
     int rc;
629b27
-    struct rpmvs_s *vs = rpmvsCreate(vfylevel, flags, keyring);
629b27
 
629b27
     rpmlog(RPMLOG_NOTICE, "%s:%s", fn, vd.verbose ? "\n" : "");
629b27
 
629b27
+    if(isTranscodedRpm(fd) == RPMRC_OK){
629b27
+	return rpmpkgVerifySigsTranscoded(fd);
629b27
+    }
629b27
+    struct rpmvs_s *vs = rpmvsCreate(vfylevel, flags, keyring);
629b27
+
629b27
     rc = rpmpkgRead(vs, fd, NULL, NULL, &msg;;
629b27
 
629b27
     if (rc)
629b27
diff --git a/tests/rpm2extents.at b/tests/rpm2extents.at
629b27
index baea987e4..18accfc75 100644
629b27
--- a/tests/rpm2extents.at
629b27
+++ b/tests/rpm2extents.at
629b27
@@ -29,7 +29,7 @@ AT_CHECK([runroot_other cat /data/RPMS/hello-2.0-1.x86_64.rpm | runroot_other rp
629b27
 [ignore])
629b27
 AT_CLEANUP
629b27
 
629b27
-# Check that tailer writes checksig return code and content.
629b27
+# Check that transcoder writes checksig return code and content.
629b27
 #
629b27
 AT_SETUP([rpm2extents signature])
629b27
 AT_KEYWORDS([rpm2extents digest signature])
629b27
@@ -62,3 +62,35 @@ RPMSignOutput Content     Header V4 RSA/SHA256 Signature, key ID 1964c5fc: OK
629b27
 ],
629b27
 [])
629b27
 AT_CLEANUP
629b27
+
629b27
+AT_SETUP([rpm2extents signature verification])
629b27
+AT_KEYWORDS([rpm2extents digest signature])
629b27
+AT_CHECK([
629b27
+RPMDB_INIT
629b27
+
629b27
+runroot_other cat /data/RPMS/hello-2.0-1.x86_64-signed.rpm | runroot_other rpm2extents SHA256 > ${RPMTEST}/tmp/hello-2.0-1.x86_64-signed.rpm 2> /dev/null
629b27
+runroot rpmkeys -Kv /tmp/hello-2.0-1.x86_64-signed.rpm; echo $?
629b27
+runroot rpmkeys --import /data/keys/rpm.org-rsa-2048-test.pub
629b27
+runroot_other cat /data/RPMS/hello-2.0-1.x86_64-signed.rpm | runroot_other rpm2extents SHA256 > ${RPMTEST}/tmp/hello-2.0-1.x86_64-signed.rpm
629b27
+runroot rpmkeys -Kv /tmp/hello-2.0-1.x86_64-signed.rpm; echo $?
629b27
+],
629b27
+[0],
629b27
+[/tmp/hello-2.0-1.x86_64-signed.rpm:
629b27
+    Header V4 RSA/SHA256 Signature, key ID 1964c5fc: NOKEY
629b27
+    Header SHA256 digest: OK
629b27
+    Header SHA1 digest: OK
629b27
+    Payload SHA256 digest: OK
629b27
+    V4 RSA/SHA256 Signature, key ID 1964c5fc: NOKEY
629b27
+    MD5 digest: OK
629b27
+1
629b27
+/tmp/hello-2.0-1.x86_64-signed.rpm:
629b27
+    Header V4 RSA/SHA256 Signature, key ID 1964c5fc: OK
629b27
+    Header SHA256 digest: OK
629b27
+    Header SHA1 digest: OK
629b27
+    Payload SHA256 digest: OK
629b27
+    V4 RSA/SHA256 Signature, key ID 1964c5fc: OK
629b27
+    MD5 digest: OK
629b27
+0
629b27
+],
629b27
+[])
629b27
+AT_CLEANUP
629b27
-- 
629b27
2.35.1
629b27