Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1141346pxb; Wed, 10 Feb 2021 00:42:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzNMKjBU/mfg+xHYoq+9dSxNuDl1er1pffXj5s/3CBxdqAUDj3sMiQLLrlYOzAyLisIO1Go X-Received: by 2002:a17:906:7d4d:: with SMTP id l13mr1885727ejp.107.1612946548524; Wed, 10 Feb 2021 00:42:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612946548; cv=none; d=google.com; s=arc-20160816; b=BqDeK1CkNuXhTnNQNExmVyQ5mZoS7y4jaOWK28J4dm1opx1e6Sj6xA8F6P4wzcH4z4 T4VABaToST/lenVgtx/1sp8gfKd9dg0kQi56CJiV/JRaCskuGciecMve4ZDe9C2CNHQO MrSrZLq+e7iOrmNqu5xY+OIr9cn9R9qZZ29uWJJeYSAToZ4Rnw4tt5UBYSqzXaLJ4fC4 d21YTRDlgs6n1AqCY8UMfm/rfSks60dJEV3f1VKIVd048KA0u1+Gs13a15ujLr3fIsAf nyYsTvvwSbfsOnhKTwMJYI51kMc9kKUmDHTT5kfrAUyF+vze0N/3CnnSdt2rVc8fCBXI Ttgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=qh9BwJJOq83vzzDt8ZTHae5dRPfpgQduAoqe0fHV+EA=; b=wm4QCVsMTyYiw5pgOU4iMhcBQtUHdnADXm2z6qyPq2SpOMnmwJosXeQdP2wtR1VDl6 p1Iz7duS4pQUjxuD38okbpwjDdbbin7hp535xYw098zEoXFh0ZeRr24cEdoMxr5IIFeq /eJ1OJWq8y3y/ryHoqUeRj1B8xQuG1Q5sjLGR80LTkrLGapX3pK8e0bpE8mpV8qKING8 3y4UAwpqaZk91oO/XMviWTBlRn9mQ93VI3rXaHercn1UG7pTS9j9jhODQDuY0KmMJDv8 tIM4vxjg9+cnUaawmO52n6z8mZNOQ4BJ5yJ3LVagjQsNOu8NewfSGoC4KZ9REUubuFyp rNDQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l23si884641ejx.701.2021.02.10.00.42.05; Wed, 10 Feb 2021 00:42:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233033AbhBJHrb (ORCPT + 99 others); Wed, 10 Feb 2021 02:47:31 -0500 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:60130 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232955AbhBJHqi (ORCPT ); Wed, 10 Feb 2021 02:46:38 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=songyang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0UOLx6x._1612943154; Received: from localhost(mailfrom:songyang@linux.alibaba.com fp:SMTPD_---0UOLx6x._1612943154) by smtp.aliyun-inc.com(127.0.0.1); Wed, 10 Feb 2021 15:45:54 +0800 From: Yang Song To: dhowells@redhat.com, dwmw2@infradead.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Cc: zhang.jia@linux.alibaba.com, tianjia.zhang@linux.alibaba.com, songyang@linux.alibaba.com Subject: [PATCH] sign-file: add openssl engine support Date: Wed, 10 Feb 2021 15:45:54 +0800 Message-Id: <20210210074554.81100-1-songyang@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.3.ge56e4f7 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use a customized signature service supported by openssl engine to sign the kernel module. Add command line parameters that support engine for sign-file to use the customized openssl engine service to sign kernel modules. Signed-off-by: Yang Song --- scripts/sign-file.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/scripts/sign-file.c b/scripts/sign-file.c index fbd34b8e8f57..276068e9b595 100644 --- a/scripts/sign-file.c +++ b/scripts/sign-file.c @@ -70,7 +70,7 @@ static __attribute__((noreturn)) void format(void) { fprintf(stderr, - "Usage: scripts/sign-file [-dp] []\n"); + "Usage: scripts/sign-file [-edp] [] []\n"); fprintf(stderr, " scripts/sign-file -s []\n"); exit(2); @@ -206,12 +206,54 @@ static X509 *read_x509(const char *x509_name) return x509; } +/* Try to load an engine in a shareable library */ +static ENGINE *try_load_engine(const char *engine) +{ + ENGINE *e = ENGINE_by_id("dynamic"); + if (e) { + if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0) + || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) { + ENGINE_free(e); + e = NULL; + } + } + return e; +} + +static ENGINE *setup_engine(const char *engine) +{ + ENGINE *e = NULL; + + if (engine) { + e = ENGINE_by_id(engine); + if (e == NULL) { + e = try_load_engine(engine); + if (e == NULL) { + ERR(1, "invalid engine \"%s\"\n", engine); + return NULL; + } + } + + if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { + ERR(1, "can't use that engine\n"); + ENGINE_free(e); + return NULL; + } + + fprintf(stdout, "engine \"%s\" set.\n", ENGINE_get_id(e)); + } + + return e; +} + int main(int argc, char **argv) { struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; + char *ossl_engine = NULL; char *hash_algo = NULL; char *private_key_name = NULL, *raw_sig_name = NULL; char *x509_name, *module_name, *dest_name; + bool use_engine = false; bool save_sig = false, replace_orig; bool sign_only = false; bool raw_sig = false; @@ -242,8 +284,9 @@ int main(int argc, char **argv) #endif do { - opt = getopt(argc, argv, "sdpk"); + opt = getopt(argc, argv, "sedpk"); switch (opt) { + case 'e': use_engine = true; break; case 's': raw_sig = true; break; case 'p': save_sig = true; break; case 'd': sign_only = true; save_sig = true; break; @@ -257,13 +300,18 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - if (argc < 4 || argc > 5) + if (argc < 4 || argc > 6) format(); if (raw_sig) { raw_sig_name = argv[0]; hash_algo = argv[1]; } else { + if (use_engine) { + ossl_engine = argv[0]; + argc--; + argv++; + } hash_algo = argv[0]; private_key_name = argv[1]; } @@ -291,6 +339,17 @@ int main(int argc, char **argv) ERR(!bm, "%s", module_name); if (!raw_sig) { + if (use_engine) { + if (ossl_engine == NULL) { + fprintf(stderr, "Input openssl engine is null\n"); + exit(1); + } + + // Engine setup + ENGINE_load_builtin_engines(); + setup_engine(ossl_engine); + } + /* Read the private key and the X.509 cert the PKCS#7 message * will point to. */ -- 2.19.1.3.ge56e4f7