2010-09-03 19:40:18 +08:00
|
|
|
#include "system.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
2017-06-08 21:55:16 +08:00
|
|
|
#include <termios.h>
|
2010-09-03 19:40:18 +08:00
|
|
|
|
|
|
|
#include <popt.h>
|
|
|
|
#include <rpm/rpmcli.h>
|
2010-09-29 18:09:31 +08:00
|
|
|
#include <rpm/rpmsign.h>
|
2010-09-03 19:40:18 +08:00
|
|
|
#include "cliutils.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
enum modes {
|
2017-06-08 17:39:53 +08:00
|
|
|
MODE_NONE = 0,
|
2010-09-03 19:40:18 +08:00
|
|
|
MODE_ADDSIGN = (1 << 0),
|
|
|
|
MODE_RESIGN = (1 << 1),
|
|
|
|
MODE_DELSIGN = (1 << 2),
|
2020-04-14 06:24:31 +08:00
|
|
|
MODE_DELFILESIGN = (1 << 3),
|
2010-09-03 19:40:18 +08:00
|
|
|
};
|
|
|
|
|
2017-06-08 17:39:53 +08:00
|
|
|
static int mode = MODE_NONE;
|
2010-09-03 19:40:18 +08:00
|
|
|
|
2020-03-28 06:31:36 +08:00
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
2020-03-02 19:56:33 +08:00
|
|
|
static int fskpass = 0;
|
2015-07-22 01:00:42 +08:00
|
|
|
static char * fileSigningKey = NULL;
|
2017-06-08 22:36:28 +08:00
|
|
|
#endif
|
2020-04-04 04:26:06 +08:00
|
|
|
#ifdef WITH_FSVERITY
|
|
|
|
static char * fileSigningCert = NULL;
|
2020-06-11 00:30:54 +08:00
|
|
|
static char * verityAlgorithm = NULL;
|
2020-04-04 04:26:06 +08:00
|
|
|
#endif
|
2015-07-22 01:00:42 +08:00
|
|
|
|
2017-05-29 21:11:55 +08:00
|
|
|
static struct rpmSignArgs sargs = {NULL, 0, 0};
|
|
|
|
|
2010-11-15 15:07:10 +08:00
|
|
|
static struct poptOption signOptsTable[] = {
|
2010-09-03 19:40:18 +08:00
|
|
|
{ "addsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_ADDSIGN,
|
|
|
|
N_("sign package(s)"), NULL },
|
|
|
|
{ "resign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_RESIGN,
|
|
|
|
N_("sign package(s) (identical to --addsign)"), NULL },
|
|
|
|
{ "delsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_DELSIGN,
|
|
|
|
N_("delete package signatures"), NULL },
|
2020-04-14 06:24:31 +08:00
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
|
|
|
{ "delfilesign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode,
|
|
|
|
MODE_DELFILESIGN, N_("delete IMA and fsverity file signatures"), NULL },
|
|
|
|
#endif
|
2020-03-02 20:47:26 +08:00
|
|
|
{ "rpmv3", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR),
|
|
|
|
&sargs.signflags, RPMSIGN_FLAG_RPMV3,
|
|
|
|
N_("create rpm v3 header+payload signatures") },
|
2017-06-08 22:36:28 +08:00
|
|
|
#ifdef WITH_IMAEVM
|
2020-03-02 19:56:33 +08:00
|
|
|
{ "signfiles", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR),
|
|
|
|
&sargs.signflags, RPMSIGN_FLAG_IMA,
|
2015-07-22 01:00:42 +08:00
|
|
|
N_("sign package(s) files"), NULL},
|
2020-03-28 06:31:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_FSVERITY
|
|
|
|
{ "signverity", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR),
|
|
|
|
&sargs.signflags, RPMSIGN_FLAG_FSVERITY,
|
|
|
|
N_("generate fsverity signatures for package(s) files"), NULL},
|
2020-06-11 00:30:54 +08:00
|
|
|
{ "verityalgo", '\0', POPT_ARG_STRING, &verityAlgorithm, 0,
|
|
|
|
N_("algorithm to use for verity signatures, default sha256"),
|
|
|
|
N_("<algorithm>") },
|
2020-04-04 04:26:06 +08:00
|
|
|
{ "certpath", '\0', POPT_ARG_STRING, &fileSigningCert, 0,
|
|
|
|
N_("use file signing cert <cert>"),
|
|
|
|
N_("<cert>") },
|
2020-03-28 06:31:36 +08:00
|
|
|
#endif
|
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
2015-07-22 01:00:42 +08:00
|
|
|
{ "fskpath", '\0', POPT_ARG_STRING, &fileSigningKey, 0,
|
|
|
|
N_("use file signing key <key>"),
|
|
|
|
N_("<key>") },
|
2015-07-22 01:00:46 +08:00
|
|
|
{ "fskpass", '\0', POPT_ARG_NONE, &fskpass, 0,
|
|
|
|
N_("prompt for file signing key password"), NULL},
|
2017-06-08 22:36:28 +08:00
|
|
|
#endif
|
2010-11-15 15:07:10 +08:00
|
|
|
POPT_TABLEEND
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct poptOption optionsTable[] = {
|
|
|
|
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, signOptsTable, 0,
|
|
|
|
N_("Signature options:"), NULL },
|
|
|
|
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
|
|
|
|
N_("Common options for all rpm modes and executables:"), NULL },
|
2010-09-03 19:40:18 +08:00
|
|
|
|
|
|
|
POPT_AUTOALIAS
|
|
|
|
POPT_AUTOHELP
|
|
|
|
POPT_TABLEEND
|
|
|
|
};
|
|
|
|
|
2020-03-28 06:31:36 +08:00
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
2020-05-28 04:49:03 +08:00
|
|
|
static int flags_sign_files(int flags)
|
|
|
|
{
|
|
|
|
return (flags & (RPMSIGN_FLAG_IMA | RPMSIGN_FLAG_FSVERITY) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:55:16 +08:00
|
|
|
static char *get_fskpass(void)
|
|
|
|
{
|
|
|
|
struct termios flags, tmp_flags;
|
|
|
|
int passlen = 64;
|
2017-06-09 17:45:21 +08:00
|
|
|
char *password = xmalloc(passlen);
|
2017-06-09 17:52:08 +08:00
|
|
|
char *pwd = NULL;
|
2017-06-08 21:55:16 +08:00
|
|
|
|
|
|
|
tcgetattr(fileno(stdin), &flags);
|
|
|
|
tmp_flags = flags;
|
|
|
|
tmp_flags.c_lflag &= ~ECHO;
|
|
|
|
tmp_flags.c_lflag |= ECHONL;
|
|
|
|
|
|
|
|
if (tcsetattr(fileno(stdin), TCSANOW, &tmp_flags) != 0) {
|
|
|
|
perror("tcsetattr");
|
2017-06-09 17:52:08 +08:00
|
|
|
goto exit;
|
2017-06-08 21:55:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("PEM password: ");
|
|
|
|
pwd = fgets(password, passlen, stdin);
|
|
|
|
|
|
|
|
if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) {
|
|
|
|
perror("tcsetattr");
|
2017-06-09 17:52:08 +08:00
|
|
|
pwd = NULL;
|
|
|
|
goto exit;
|
2017-06-08 21:55:16 +08:00
|
|
|
}
|
2017-06-09 17:52:08 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if (pwd)
|
|
|
|
pwd[strlen(pwd) - 1] = '\0'; /* remove newline */
|
|
|
|
else
|
|
|
|
free(password);
|
2017-06-08 21:55:16 +08:00
|
|
|
return pwd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-09-03 19:40:18 +08:00
|
|
|
/* TODO: permit overriding macro setup on the command line */
|
2017-05-29 21:11:55 +08:00
|
|
|
static int doSign(poptContext optCon, struct rpmSignArgs *sargs)
|
2010-09-03 19:40:18 +08:00
|
|
|
{
|
|
|
|
int rc = EXIT_FAILURE;
|
|
|
|
char * name = rpmExpand("%{?_gpg_name}", NULL);
|
|
|
|
|
|
|
|
if (rstreq(name, "")) {
|
|
|
|
fprintf(stderr, _("You must set \"%%_gpg_name\" in your macro file\n"));
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-03-28 06:31:36 +08:00
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
2015-07-22 01:00:42 +08:00
|
|
|
if (fileSigningKey) {
|
2016-10-24 18:09:38 +08:00
|
|
|
rpmPushMacro(NULL, "_file_signing_key", NULL, fileSigningKey, RMIL_GLOBAL);
|
2015-07-22 01:00:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-04 04:26:06 +08:00
|
|
|
#ifdef WITH_FSVERITY
|
|
|
|
if (fileSigningCert) {
|
|
|
|
rpmPushMacro(NULL, "_file_signing_cert", NULL, fileSigningCert, RMIL_GLOBAL);
|
|
|
|
}
|
2020-06-11 00:30:54 +08:00
|
|
|
if (verityAlgorithm) {
|
|
|
|
rpmPushMacro(NULL, "_verity_algorithm", NULL, verityAlgorithm, RMIL_GLOBAL);
|
|
|
|
}
|
2020-04-04 04:26:06 +08:00
|
|
|
#endif
|
|
|
|
|
2020-05-28 04:49:03 +08:00
|
|
|
if (flags_sign_files(sargs->signflags)) {
|
2017-06-09 17:35:43 +08:00
|
|
|
char *fileSigningKeyPassword = NULL;
|
|
|
|
char *key = rpmExpand("%{?_file_signing_key}", NULL);
|
2015-07-22 01:00:42 +08:00
|
|
|
if (rstreq(key, "")) {
|
2017-06-08 17:42:00 +08:00
|
|
|
fprintf(stderr, _("You must set \"%%_file_signing_key\" in your macro file or on the command line with --fskpath\n"));
|
2015-07-22 01:00:42 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
2015-07-22 01:00:46 +08:00
|
|
|
|
|
|
|
if (fskpass) {
|
|
|
|
fileSigningKeyPassword = get_fskpass();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileSigningKeyPassword) {
|
2017-06-09 17:33:23 +08:00
|
|
|
rpmPushMacro(NULL, "_file_signing_key_password", NULL,
|
|
|
|
fileSigningKeyPassword, RMIL_CMDLINE);
|
2015-07-22 01:00:46 +08:00
|
|
|
memset(fileSigningKeyPassword, 0, strlen(fileSigningKeyPassword));
|
|
|
|
free(fileSigningKeyPassword);
|
|
|
|
}
|
|
|
|
|
2017-06-09 17:35:43 +08:00
|
|
|
free(key);
|
2015-07-22 01:00:42 +08:00
|
|
|
}
|
2017-06-08 22:36:28 +08:00
|
|
|
#endif
|
2015-07-22 01:00:42 +08:00
|
|
|
|
2015-06-10 00:06:29 +08:00
|
|
|
const char *arg;
|
|
|
|
rc = 0;
|
|
|
|
while ((arg = poptGetArg(optCon)) != NULL) {
|
2019-03-18 21:24:54 +08:00
|
|
|
if (rpmPkgSign(arg, sargs) < 0)
|
|
|
|
rc++;
|
2010-09-03 19:40:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
free(name);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ec = EXIT_FAILURE;
|
2013-08-18 03:50:41 +08:00
|
|
|
poptContext optCon = NULL;
|
2010-10-13 18:19:35 +08:00
|
|
|
const char *arg;
|
2010-09-03 19:40:18 +08:00
|
|
|
|
2013-08-18 03:50:41 +08:00
|
|
|
xsetprogname(argv[0]); /* Portability call -- see system.h */
|
|
|
|
|
|
|
|
optCon = rpmcliInit(argc, argv, optionsTable);
|
|
|
|
|
2010-09-03 19:40:18 +08:00
|
|
|
if (argc <= 1) {
|
|
|
|
printUsage(optCon, stderr, 0);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2010-10-13 18:19:35 +08:00
|
|
|
if (poptPeekArg(optCon) == NULL) {
|
2010-09-03 19:40:18 +08:00
|
|
|
argerror(_("no arguments given"));
|
|
|
|
}
|
|
|
|
|
2020-03-28 06:31:36 +08:00
|
|
|
#if defined(WITH_IMAEVM) || defined(WITH_FSVERITY)
|
2020-05-28 04:49:03 +08:00
|
|
|
if (fileSigningKey && !(flags_sign_files(sargs.signflags))) {
|
2015-07-22 01:00:42 +08:00
|
|
|
argerror(_("--fskpath may only be specified when signing files"));
|
|
|
|
}
|
2017-06-08 22:36:28 +08:00
|
|
|
#endif
|
2015-07-22 01:00:42 +08:00
|
|
|
|
2010-09-03 19:40:18 +08:00
|
|
|
switch (mode) {
|
|
|
|
case MODE_ADDSIGN:
|
|
|
|
case MODE_RESIGN:
|
2017-05-29 21:11:55 +08:00
|
|
|
ec = doSign(optCon, &sargs);
|
2010-09-03 19:40:18 +08:00
|
|
|
break;
|
|
|
|
case MODE_DELSIGN:
|
2010-10-13 19:20:30 +08:00
|
|
|
ec = 0;
|
2010-10-13 18:19:35 +08:00
|
|
|
while ((arg = poptGetArg(optCon)) != NULL) {
|
2019-03-18 21:24:54 +08:00
|
|
|
if (rpmPkgDelSign(arg, &sargs) < 0)
|
|
|
|
ec++;
|
2010-10-13 18:19:35 +08:00
|
|
|
}
|
2010-09-03 19:40:18 +08:00
|
|
|
break;
|
2020-04-14 06:24:31 +08:00
|
|
|
case MODE_DELFILESIGN:
|
|
|
|
ec = 0;
|
|
|
|
while ((arg = poptGetArg(optCon)) != NULL) {
|
|
|
|
if (rpmPkgDelFileSign(arg, &sargs) < 0)
|
|
|
|
ec++;
|
|
|
|
}
|
|
|
|
break;
|
2017-06-08 17:39:53 +08:00
|
|
|
case MODE_NONE:
|
|
|
|
printUsage(optCon, stderr, 0);
|
|
|
|
break;
|
2010-09-03 19:40:18 +08:00
|
|
|
default:
|
|
|
|
argerror(_("only one major mode may be specified"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
rpmcliFini(optCon);
|
2019-03-18 21:24:54 +08:00
|
|
|
return RETVAL(ec);
|
2010-09-03 19:40:18 +08:00
|
|
|
}
|