From patchwork Sat Jul 18 19:08:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Spooren X-Patchwork-Id: 1331592 X-Patchwork-Delegate: mail@aparcar.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=aparcar.org Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B8HdM5bbjz9sR4 for ; Sun, 19 Jul 2020 05:10:46 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=EcKrdIITh87ZVRXRy8jqlPf8cUXJwRlfLZUpiSmkivo=; b=Q6UvQlOWDbYZEDsj6vVHHIzHBi IxpO7hddySRlkoFXn1ThSbG65whqxwaNDRawLMs/Ul1/LQhaC5CQHfO++cyXJAHiKdE2uAEafCR5o JvdRd1NPiwHYUciujyWhNC3Rkc5Znaq/m0xRjNtHPxCnCVjKajOTlvk3PTQCAyINWMSKdkad3puM/ xww74v0ZpAo8TCGZe9cLMSFTtYjP+Xyh/LBwi0MvPPHUYy9/iqnnu9OC2J01jA0MET1m6KKG9rMru MBRQJdFxyaqX+RqqhetgN0fHAhFdWvMl7aUPnQYF64grrUJxNUI599Dn7Tjr6p7N/6gNi6I6Ek1H7 gb4tRE8A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jwsC8-00037o-RA; Sat, 18 Jul 2020 19:08:24 +0000 Received: from relay12.mail.gandi.net ([217.70.178.232]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jwsC6-00037P-Jd for openwrt-devel@lists.openwrt.org; Sat, 18 Jul 2020 19:08:24 +0000 Received: from dawn.lan (udp224251uds.hawaiiantel.net [72.234.141.215]) (Authenticated sender: mail@aparcar.org) by relay12.mail.gandi.net (Postfix) with ESMTPA id A24BD200002; Sat, 18 Jul 2020 19:08:15 +0000 (UTC) From: Paul Spooren To: openwrt-devel@lists.openwrt.org Subject: [PATCH usign] usign: add VERIFYONLY option Date: Sat, 18 Jul 2020 09:08:08 -1000 Message-Id: <20200718190808.318798-1-mail@aparcar.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200718_150822_869369_6A01CF53 X-CRM114-Status: GOOD ( 11.34 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [217.70.178.232 listed in wl.mailspike.net] -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [217.70.178.232 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paul Spooren Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org The reimplementation of signify called usign is used for both opkg package list and sysupgrade verification. Currently usign support all features per default, including key generation and signing of of mesages. Inspired by Petrs work on the signify port[0] I tried to add similar ifndef conditions to reduce the code size. If VERIFYONLY is set, the usign binary size is reduced by 4kB. Only the functions `-V` for verification and `-F` for fingerprint printing are enabled. [0]: https://github.com/openwrt/openwrt/pull/2911 Signed-off-by: Paul Spooren --- main.c | 118 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 52 deletions(-) diff --git a/main.c b/main.c index ebfdfb0..ccf4455 100644 --- a/main.c +++ b/main.c @@ -57,7 +57,9 @@ static const char *pubkeyfile; static const char *pubkeydir; static const char *sigfile; static const char *seckeyfile; +#ifndef VERIFYONLY static const char *comment; +#endif static bool quiet; static enum { CMD_NONE, @@ -139,21 +141,6 @@ get_base64_file(const char *file, void *dest, int size, void *buf, int buflen) return b64_decode(buf, dest, size) == size; } -static void write_file(const char *name, const uint8_t *fingerprint, - const char *prefix, char *buf) -{ - FILE *f; - - f = open_file(name, false); - fputs("untrusted comment: ", f); - if (comment) - fputs(comment, f); - else - fprintf(f, "%s %016"PRIx64, prefix, - fingerprint_u64(fingerprint)); - fprintf(f, "\n%s\n", buf); - fclose(f); -} static int verify(const char *msgfile) { @@ -208,6 +195,47 @@ static int verify(const char *msgfile) return 0; } +static int fingerprint(void) +{ + struct seckey skey; + struct pubkey pkey; + struct sig sig; + char buf[512]; + uint8_t *fp; + + if (seckeyfile && + get_base64_file(seckeyfile, &skey, sizeof(skey), buf, sizeof(buf))) + fp = skey.fingerprint; + else if (pubkeyfile && + get_base64_file(pubkeyfile, &pkey, sizeof(pkey), buf, sizeof(buf))) + fp = pkey.fingerprint; + else if (sigfile && + get_base64_file(sigfile, &sig, sizeof(sig), buf, sizeof(buf))) + fp = sig.fingerprint; + else + return 1; + + fprintf(stdout, "%016"PRIx64"\n", fingerprint_u64(fp)); + return 0; +} + +#ifndef VERIFYONLY +static void write_file(const char *name, const uint8_t *fingerprint, + const char *prefix, char *buf) +{ + FILE *f; + + f = open_file(name, false); + fputs("untrusted comment: ", f); + if (comment) + fputs(comment, f); + else + fprintf(f, "%s %016"PRIx64, prefix, + fingerprint_u64(fingerprint)); + fprintf(f, "\n%s\n", buf); + fclose(f); +} + static int sign(const char *msgfile) { struct seckey skey; @@ -256,29 +284,6 @@ static int sign(const char *msgfile) return 0; } -static int fingerprint(void) -{ - struct seckey skey; - struct pubkey pkey; - struct sig sig; - char buf[512]; - uint8_t *fp; - - if (seckeyfile && - get_base64_file(seckeyfile, &skey, sizeof(skey), buf, sizeof(buf))) - fp = skey.fingerprint; - else if (pubkeyfile && - get_base64_file(pubkeyfile, &pkey, sizeof(pkey), buf, sizeof(buf))) - fp = pkey.fingerprint; - else if (sigfile && - get_base64_file(sigfile, &sig, sizeof(sig), buf, sizeof(buf))) - fp = sig.fingerprint; - else - return 1; - - fprintf(stdout, "%016"PRIx64"\n", fingerprint_u64(fp)); - return 0; -} static int generate(void) { @@ -332,6 +337,7 @@ static int generate(void) return 0; } +#endif static int usage(const char *cmd) { @@ -339,17 +345,21 @@ static int usage(const char *cmd) "Usage: %s \n" "Commands:\n" " -V: verify (needs at least -m and -p|-P)\n" - " -S: sign (needs at least -m and -s)\n" " -F: print key fingerprint of public/secret key or signature\n" +#ifndef VERIFYONLY + " -S: sign (needs at least -m and -s)\n" " -G: generate a new keypair (needs at least -p and -s)\n" +#endif "Options:\n" +#ifndef VERIFYONLY " -c : add comment to keys\n" - " -m : message file\n" + " -s : secret key file (sign/fingerprint only)\n" +#endif + " -x : signature file (defaults to .sig)\n" " -p : public key file (verify/fingerprint only)\n" " -P : public key directory (verify only)\n" + " -m : message file\n" " -q: quiet (do not print verification result, use return code only)\n" - " -s : secret key file (sign/fingerprint only)\n" - " -x : signature file (defaults to .sig)\n" "\n", cmd); return 1; @@ -373,18 +383,23 @@ int main(int argc, char **argv) case 'V': set_cmd(argv[0], CMD_VERIFY); break; - case 'S': - set_cmd(argv[0], CMD_SIGN); - break; case 'F': set_cmd(argv[0], CMD_FINGERPRINT); break; +#ifndef VERIFYONLY + case 'S': + set_cmd(argv[0], CMD_SIGN); + break; case 'G': set_cmd(argv[0], CMD_GENERATE); break; case 'c': comment = optarg; break; +#endif + case 's': + seckeyfile = optarg; + break; case 'm': msgfile = optarg; break; @@ -394,9 +409,6 @@ int main(int argc, char **argv) case 'p': pubkeyfile = optarg; break; - case 's': - seckeyfile = optarg; - break; case 'x': sigfile = optarg; break; @@ -425,20 +437,22 @@ int main(int argc, char **argv) if ((!pubkeyfile && !pubkeydir) || !msgfile) return usage(argv[0]); return verify(msgfile); - case CMD_SIGN: - if (!seckeyfile || !msgfile || !sigfile) - return usage(argv[0]); - return sign(msgfile); case CMD_FINGERPRINT: if (!!seckeyfile + !!pubkeyfile + !!sigfile != 1) { fprintf(stderr, "Need one secret/public key or signature\n"); return usage(argv[0]); } return fingerprint(); +#ifndef VERIFYONLY + case CMD_SIGN: + if (!seckeyfile || !msgfile || !sigfile) + return usage(argv[0]); + return sign(msgfile); case CMD_GENERATE: if (!seckeyfile || !pubkeyfile) return usage(argv[0]); return generate(); +#endif default: return usage(argv[0]); }