michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone
45afda
From d447376aa2bf66a5d5b6a928fb0c6e65189910ba Mon Sep 17 00:00:00 2001
45afda
From: Jes Sorensen <jsorensen@fb.com>
45afda
Date: Fri, 3 Apr 2020 16:38:08 -0400
45afda
Subject: [PATCH 19/33] Implement rpmSignVerity()
45afda
45afda
This generates the root Merkle tree hash and signs it using the
45afda
specified key and certificate.
45afda
45afda
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
45afda
---
45afda
 sign/rpmgensig.c     |  36 +++++++++++++
45afda
 sign/rpmsignverity.c | 121 +++++++++++++++++++++++++++++++++++++++++++
45afda
 sign/rpmsignverity.h |  29 +++++++++++
45afda
 3 files changed, 186 insertions(+)
45afda
 create mode 100644 sign/rpmsignverity.c
45afda
 create mode 100644 sign/rpmsignverity.h
45afda
45afda
diff --git a/sign/rpmgensig.c b/sign/rpmgensig.c
45afda
index a6e37e71b..8d5c5858f 100644
45afda
--- a/sign/rpmgensig.c
45afda
+++ b/sign/rpmgensig.c
45afda
@@ -22,6 +22,7 @@
45afda
 #include "lib/signature.h"
45afda
 #include "lib/rpmvs.h"
45afda
 #include "sign/rpmsignfiles.h"
45afda
+#include "sign/rpmsignverity.h"
45afda
 
45afda
 #include "debug.h"
45afda
 
45afda
@@ -446,6 +447,36 @@ static rpmRC includeFileSignatures(Header *sigp, Header *hdrp)
45afda
 #endif
45afda
 }
45afda
 
45afda
+static rpmRC includeVeritySignatures(FD_t fd, Header *sigp, Header *hdrp)
45afda
+{
45afda
+#ifdef WITH_FSVERITY
45afda
+    rpmRC rc;
45afda
+    char *key = rpmExpand("%{?_file_signing_key}", NULL);
45afda
+    char *keypass = rpmExpand("%{?_file_signing_key_password}", NULL);
45afda
+    char *cert = rpmExpand("%{?_file_signing_cert}", NULL);
45afda
+
45afda
+    if (rstreq(keypass, "")) {
45afda
+	free(keypass);
45afda
+	keypass = NULL;
45afda
+    }
45afda
+
45afda
+    if (key && cert) {
45afda
+	rc = rpmSignVerity(fd, *sigp, *hdrp, key, keypass, cert);
45afda
+    } else {
45afda
+	rpmlog(RPMLOG_ERR, _("fsverity signatures requires a key and a cert\n"));
45afda
+	rc = RPMRC_FAIL;
45afda
+    }
45afda
+
45afda
+    free(keypass);
45afda
+    free(key);
45afda
+    free(cert);
45afda
+    return rc;
45afda
+#else
45afda
+    rpmlog(RPMLOG_ERR, _("fsverity signing support not built in\n"));
45afda
+    return RPMRC_FAIL;
45afda
+#endif
45afda
+}
45afda
+
45afda
 static int msgCb(struct rpmsinfo_s *sinfo, void *cbdata)
45afda
 {
45afda
     char **msg = cbdata;
45afda
@@ -544,6 +575,11 @@ static int rpmSign(const char *rpm, int deleting, int flags)
45afda
 	    goto exit;
45afda
     }
45afda
 
45afda
+    if (flags & RPMSIGN_FLAG_FSVERITY) {
45afda
+	if (includeVeritySignatures(fd, &sigh, &h))
45afda
+	    goto exit;
45afda
+    }
45afda
+
45afda
     if (deleting) {	/* Nuke all the signature tags. */
45afda
 	deleteSigs(sigh);
45afda
     } else {
45afda
diff --git a/sign/rpmsignverity.c b/sign/rpmsignverity.c
45afda
new file mode 100644
45afda
index 000000000..5346c3bc8
45afda
--- /dev/null
45afda
+++ b/sign/rpmsignverity.c
45afda
@@ -0,0 +1,121 @@
45afda
+/**
45afda
+ * Copyright (C) 2020 Facebook
45afda
+ *
45afda
+ * Author: Jes Sorensen <jsorensen@fb.com>
45afda
+ */
45afda
+
45afda
+#include "system.h"
45afda
+
45afda
+#include <rpm/rpmlib.h>		/* RPMSIGTAG & related */
45afda
+#include <rpm/rpmlog.h>		/* rpmlog */
45afda
+#include <rpm/rpmfi.h>
45afda
+#include <rpm/rpmpgp.h>		/* rpmDigestLength */
45afda
+#include "lib/header.h"		/* HEADERGET_MINMEM */
45afda
+#include "lib/header_internal.h"
45afda
+#include "lib/rpmtypes.h"	/* rpmRC */
45afda
+#include <libfsverity.h>
45afda
+#include "rpmio/rpmio_internal.h"
45afda
+#include "lib/rpmvs.h"
45afda
+
45afda
+#include "sign/rpmsignverity.h"
45afda
+
45afda
+#define MAX_SIGNATURE_LENGTH 1024
45afda
+
45afda
+static int rpmVerityRead(void *opaque, void *buf, size_t size)
45afda
+{
45afda
+	int retval;
45afda
+	rpmfi fi = (rpmfi)opaque;
45afda
+
45afda
+	retval = rpmfiArchiveRead(fi, buf, size);
45afda
+
45afda
+	if (retval > 0)
45afda
+		retval = 0;
45afda
+	return retval;
45afda
+}
45afda
+
45afda
+rpmRC rpmSignVerity(FD_t fd, Header sigh, Header h, char *key,
45afda
+		    char *keypass, char *cert)
45afda
+{
45afda
+    int rc, status;
45afda
+    FD_t gzdi;
45afda
+    rpmfiles files = NULL;
45afda
+    rpmfi fi = NULL;
45afda
+    rpmts ts = rpmtsCreate();
45afda
+    struct libfsverity_digest *digest = NULL;
45afda
+    struct libfsverity_merkle_tree_params params;
45afda
+    struct libfsverity_signature_params sig_params;
45afda
+    rpm_loff_t file_size;
45afda
+    off_t offset = Ftell(fd);
45afda
+    const char *compr;
45afda
+    char *rpmio_flags = NULL;
45afda
+    char *digest_hex;
45afda
+    uint8_t *sig;
45afda
+    size_t sig_size;
45afda
+
45afda
+    Fseek(fd, 0, SEEK_SET);
45afda
+    rpmtsSetVSFlags(ts, RPMVSF_MASK_NODIGESTS | RPMVSF_MASK_NOSIGNATURES |
45afda
+		    RPMVSF_NOHDRCHK);
45afda
+    rc = rpmReadPackageFile(ts, fd, "fsverity", &h);
45afda
+    if (rc != RPMRC_OK) {
45afda
+	rpmlog(RPMLOG_DEBUG, _("%s: rpmReadPackageFile returned %i\n"),
45afda
+	       __func__, rc);
45afda
+	goto out;
45afda
+    }
45afda
+
45afda
+    rpmlog(RPMLOG_DEBUG, _("key: %s\n"), key);
45afda
+    rpmlog(RPMLOG_DEBUG, _("cert: %s\n"), cert);
45afda
+
45afda
+    compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
45afda
+    rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
45afda
+
45afda
+    gzdi = Fdopen(fdDup(Fileno(fd)), rpmio_flags);
45afda
+    free(rpmio_flags);
45afda
+
45afda
+    files = rpmfilesNew(NULL, h, RPMTAG_BASENAMES, RPMFI_FLAGS_QUERY);
45afda
+    fi = rpmfiNewArchiveReader(gzdi, files,
45afda
+			       RPMFI_ITER_READ_ARCHIVE_OMIT_HARDLINKS);
45afda
+
45afda
+    while (rpmfiNext(fi) >= 0) {
45afda
+	if (!S_ISREG(rpmfiFMode(fi)))
45afda
+	    continue;
45afda
+	file_size = rpmfiFSize(fi);
45afda
+	rpmlog(RPMLOG_DEBUG, _("file: %s, (size %li)\n"),
45afda
+	       rpmfiFN(fi), file_size);
45afda
+
45afda
+	memset(&params, 0, sizeof(struct libfsverity_merkle_tree_params));
45afda
+	params.version = 1;
45afda
+	params.hash_algorithm = FS_VERITY_HASH_ALG_SHA256;
45afda
+	params.block_size = sysconf(_SC_PAGESIZE);
45afda
+	params.salt_size = 0 /* salt_size */;
45afda
+	params.salt = NULL /* salt */;
45afda
+	params.file_size = file_size;
45afda
+	status = libfsverity_compute_digest(fi, rpmVerityRead,
45afda
+					    &params, &digest);
45afda
+	if (!status) {
45afda
+	    digest_hex = pgpHexStr(digest->digest, digest->digest_size);
45afda
+	    rpmlog(RPMLOG_DEBUG, _("digest(%i): %s\n"),
45afda
+		   digest->digest_size, digest_hex);
45afda
+	    free(digest_hex);
45afda
+	}
45afda
+	memset(&sig_params, 0, sizeof(struct libfsverity_signature_params));
45afda
+	sig_params.keyfile = key;
45afda
+	sig_params.certfile = cert;
45afda
+	if (libfsverity_sign_digest(digest, &sig_params, &sig, &sig_size)) {
45afda
+	    rpmlog(RPMLOG_DEBUG, _("failed to sign digest\n"));
45afda
+	    rc = RPMRC_FAIL;
45afda
+	    goto out;
45afda
+	}
45afda
+	rpmlog(RPMLOG_DEBUG, _("digest signing success\n"));
45afda
+
45afda
+	free(digest);
45afda
+	free(sig);
45afda
+    }
45afda
+
45afda
+out:
45afda
+    Fseek(fd, offset, SEEK_SET);
45afda
+
45afda
+    rpmfilesFree(files);
45afda
+    rpmfiFree(fi);
45afda
+    rpmtsFree(ts);
45afda
+    return rc;
45afda
+}
45afda
diff --git a/sign/rpmsignverity.h b/sign/rpmsignverity.h
45afda
new file mode 100644
45afda
index 000000000..f3ad3bb18
45afda
--- /dev/null
45afda
+++ b/sign/rpmsignverity.h
45afda
@@ -0,0 +1,29 @@
45afda
+#ifndef H_RPMSIGNVERITY
45afda
+#define H_RPMSIGNVERITY
45afda
+
45afda
+#include <rpm/rpmtypes.h>
45afda
+#include <rpm/rpmutil.h>
45afda
+
45afda
+#ifdef __cplusplus
45afda
+extern "C" {
45afda
+#endif
45afda
+
45afda
+/**
45afda
+ * Sign file digests in header into signature header
45afda
+ * @param fd		file descriptor of RPM
45afda
+ * @param sigh		package signature header
45afda
+ * @param h		package header
45afda
+ * @param key		signing key
45afda
+ * @param keypass	signing key password
45afda
+ * @param cert		signing cert
45afda
+ * @return		RPMRC_OK on success
45afda
+ */
45afda
+RPM_GNUC_INTERNAL
45afda
+rpmRC rpmSignVerity(FD_t fd, Header sigh, Header h, char *key,
45afda
+		    char *keypass, char *cert);
45afda
+
45afda
+#ifdef _cplusplus
45afda
+}
45afda
+#endif
45afda
+
45afda
+#endif /* H_RPMSIGNVERITY */
45afda
-- 
45afda
2.27.0
45afda