2000-08-28 03:27:03 +08:00
|
|
|
/** \ingroup signature
|
|
|
|
* \file lib/signature.c
|
|
|
|
*/
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1997-01-18 00:22:08 +08:00
|
|
|
|
2010-01-05 21:25:31 +08:00
|
|
|
#include <errno.h>
|
2008-06-27 18:49:48 +08:00
|
|
|
#include <inttypes.h>
|
2010-01-05 18:35:54 +08:00
|
|
|
#include <sys/wait.h>
|
2008-01-30 19:53:51 +08:00
|
|
|
#include <popt.h>
|
|
|
|
|
2008-05-21 20:59:39 +08:00
|
|
|
#include <rpm/rpmtypes.h>
|
2009-05-20 22:53:50 +08:00
|
|
|
#include <rpm/rpmmacro.h> /* XXX for rpmExpand() */
|
2007-12-08 20:02:32 +08:00
|
|
|
#include <rpm/rpmstring.h>
|
2008-01-30 23:05:29 +08:00
|
|
|
#include <rpm/rpmfileutil.h>
|
|
|
|
#include <rpm/rpmlog.h>
|
2009-05-20 22:53:50 +08:00
|
|
|
#include <rpm/rpmkeyring.h>
|
2002-04-12 00:55:19 +08:00
|
|
|
|
2008-01-30 23:05:29 +08:00
|
|
|
#include "rpmio/digest.h"
|
2007-11-23 18:39:29 +08:00
|
|
|
#include "lib/rpmlead.h"
|
|
|
|
#include "lib/signature.h"
|
2008-05-12 17:28:12 +08:00
|
|
|
#include "lib/header_internal.h"
|
2008-01-30 23:05:29 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1996-02-19 10:32:11 +08:00
|
|
|
|
2005-01-05 03:31:31 +08:00
|
|
|
#if !defined(__GLIBC__) && !defined(__APPLE__)
|
2002-04-09 06:52:45 +08:00
|
|
|
char ** environ = NULL;
|
|
|
|
#endif
|
|
|
|
|
2008-06-18 18:15:46 +08:00
|
|
|
static int sighdrPut(Header h, rpmSigTag tag, rpmTagType type,
|
|
|
|
rpm_data_t p, rpm_count_t c)
|
|
|
|
{
|
|
|
|
struct rpmtd_s sigtd;
|
|
|
|
rpmtdReset(&sigtd);
|
|
|
|
sigtd.tag = tag;
|
|
|
|
sigtd.type = type;
|
|
|
|
sigtd.data = p;
|
|
|
|
sigtd.count = c;
|
|
|
|
return headerPut(h, &sigtd, HEADERPUT_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2001-02-13 03:02:15 +08:00
|
|
|
/**
|
2002-09-01 06:39:34 +08:00
|
|
|
* Print package size.
|
2001-02-13 03:02:15 +08:00
|
|
|
* @todo rpmio: use fdSize rather than fstat(2) to get file size.
|
|
|
|
* @param fd package file handle
|
|
|
|
* @param siglen signature header size
|
|
|
|
* @param pad signature padding
|
|
|
|
* @param datalen length of header+payload
|
|
|
|
* @return rpmRC return code
|
|
|
|
*/
|
2008-06-09 17:58:43 +08:00
|
|
|
static inline rpmRC printSize(FD_t fd, size_t siglen, size_t pad, rpm_loff_t datalen)
|
1999-07-14 07:33:02 +08:00
|
|
|
{
|
2000-12-13 04:03:45 +08:00
|
|
|
struct stat st;
|
2004-11-06 04:43:10 +08:00
|
|
|
int fdno = Fileno(fd);
|
1999-07-14 07:33:02 +08:00
|
|
|
|
2007-12-02 00:41:14 +08:00
|
|
|
if (fstat(fdno, &st) < 0)
|
2001-02-13 03:02:15 +08:00
|
|
|
return RPMRC_FAIL;
|
1999-07-14 07:33:02 +08:00
|
|
|
|
2007-10-09 19:48:04 +08:00
|
|
|
rpmlog(RPMLOG_DEBUG,
|
2008-06-27 18:49:48 +08:00
|
|
|
"Expected size: %12" PRIu64 \
|
|
|
|
" = lead(%d)+sigs(%zd)+pad(%zd)+data(%" PRIu64 ")\n",
|
2007-12-02 00:31:09 +08:00
|
|
|
RPMLEAD_SIZE+siglen+pad+datalen,
|
2008-06-27 18:49:48 +08:00
|
|
|
RPMLEAD_SIZE, siglen, pad, datalen);
|
2007-10-09 19:48:04 +08:00
|
|
|
rpmlog(RPMLOG_DEBUG,
|
2008-06-27 18:49:48 +08:00
|
|
|
" Actual size: %12" PRIu64 "\n", (rpm_loff_t) st.st_size);
|
2000-12-13 04:03:45 +08:00
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
return RPMRC_OK;
|
1999-07-14 07:33:02 +08:00
|
|
|
}
|
|
|
|
|
2007-12-16 19:02:27 +08:00
|
|
|
rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type, char ** msg)
|
1996-02-19 10:32:11 +08:00
|
|
|
{
|
2008-03-31 18:12:29 +08:00
|
|
|
char *buf = NULL;
|
2007-10-26 19:24:14 +08:00
|
|
|
int32_t block[4];
|
|
|
|
int32_t il;
|
|
|
|
int32_t dl;
|
|
|
|
int32_t * ei = NULL;
|
2002-09-01 06:39:34 +08:00
|
|
|
entryInfo pe;
|
2002-09-17 04:10:20 +08:00
|
|
|
size_t nb;
|
2007-10-26 19:24:14 +08:00
|
|
|
int32_t ril = 0;
|
2008-04-26 19:27:30 +08:00
|
|
|
struct indexEntry_s entry;
|
|
|
|
struct entryInfo_s info;
|
2002-09-17 04:10:20 +08:00
|
|
|
unsigned char * dataStart;
|
|
|
|
unsigned char * dataEnd = NULL;
|
2002-09-01 06:39:34 +08:00
|
|
|
Header sigh = NULL;
|
2001-02-13 03:02:15 +08:00
|
|
|
rpmRC rc = RPMRC_FAIL; /* assume failure */
|
2002-09-01 06:39:34 +08:00
|
|
|
int xx;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sighp)
|
|
|
|
*sighp = NULL;
|
|
|
|
|
|
|
|
if (sig_type != RPMSIGTYPE_HEADERSIG)
|
|
|
|
goto exit;
|
|
|
|
|
2002-09-17 23:21:03 +08:00
|
|
|
memset(block, 0, sizeof(block));
|
2007-07-11 20:29:30 +08:00
|
|
|
if ((xx = timedRead(fd, (void *)block, sizeof(block))) != sizeof(block)) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf, _("sigh size(%d): BAD, read returned %d\n"),
|
|
|
|
(int)sizeof(block), xx);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2009-01-05 17:18:46 +08:00
|
|
|
if (memcmp(block, rpm_header_magic, sizeof(rpm_header_magic))) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf, _("sigh magic: BAD\n"));
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
il = ntohl(block[2]);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (il < 0 || il > 32) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
|
|
|
_("sigh tags: BAD, no. of tags(%d) out of range\n"), il);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
dl = ntohl(block[3]);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (dl < 0 || dl > 8192) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
|
|
|
_("sigh data: BAD, no. of bytes(%d) out of range\n"), dl);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
|
2008-04-26 19:27:30 +08:00
|
|
|
memset(&entry, 0, sizeof(entry));
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
nb = (il * sizeof(struct entryInfo_s)) + dl;
|
|
|
|
ei = xmalloc(sizeof(il) + sizeof(dl) + nb);
|
2002-09-17 04:10:20 +08:00
|
|
|
ei[0] = block[2];
|
|
|
|
ei[1] = block[3];
|
2002-09-01 06:39:34 +08:00
|
|
|
pe = (entryInfo) &ei[2];
|
2002-09-17 04:10:20 +08:00
|
|
|
dataStart = (unsigned char *) (pe + il);
|
2007-07-11 20:29:30 +08:00
|
|
|
if ((xx = timedRead(fd, (void *)pe, nb)) != nb) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
|
|
|
_("sigh blob(%d): BAD, read returned %d\n"), (int)nb, xx);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
|
2002-09-17 04:10:20 +08:00
|
|
|
/* Check (and convert) the 1st tag element. */
|
2008-04-26 19:27:30 +08:00
|
|
|
xx = headerVerifyInfo(1, dl, pe, &entry.info, 0);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (xx != -1) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf, _("tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
|
2008-04-26 19:27:30 +08:00
|
|
|
0, entry.info.tag, entry.info.type,
|
|
|
|
entry.info.offset, entry.info.count);
|
2002-09-17 04:10:20 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there an immutable header region tag? */
|
2008-04-26 19:27:30 +08:00
|
|
|
if (entry.info.tag == RPMTAG_HEADERSIGNATURES
|
|
|
|
&& entry.info.type == RPM_BIN_TYPE
|
|
|
|
&& entry.info.count == REGION_TAG_COUNT)
|
2002-09-17 04:10:20 +08:00
|
|
|
{
|
|
|
|
|
2008-04-26 19:27:30 +08:00
|
|
|
if (entry.info.offset >= dl) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
2002-09-17 04:10:20 +08:00
|
|
|
_("region offset: BAD, tag %d type %d offset %d count %d\n"),
|
2008-04-26 19:27:30 +08:00
|
|
|
entry.info.tag, entry.info.type,
|
|
|
|
entry.info.offset, entry.info.count);
|
2002-09-17 04:10:20 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there an immutable header region tag trailer? */
|
2008-04-26 19:27:30 +08:00
|
|
|
dataEnd = dataStart + entry.info.offset;
|
|
|
|
(void) memcpy(&info, dataEnd, REGION_TAG_COUNT);
|
2005-06-07 07:35:24 +08:00
|
|
|
/* XXX Really old packages have HEADER_IMAGE, not HEADER_SIGNATURES. */
|
2008-04-26 19:27:30 +08:00
|
|
|
if (info.tag == htonl(RPMTAG_HEADERIMAGE)) {
|
2008-02-05 22:35:44 +08:00
|
|
|
rpmSigTag stag = htonl(RPMTAG_HEADERSIGNATURES);
|
2008-04-26 19:27:30 +08:00
|
|
|
info.tag = stag;
|
2005-06-07 07:35:24 +08:00
|
|
|
memcpy(dataEnd, &stag, sizeof(stag));
|
|
|
|
}
|
2002-09-17 04:10:20 +08:00
|
|
|
dataEnd += REGION_TAG_COUNT;
|
|
|
|
|
2008-04-26 19:27:30 +08:00
|
|
|
xx = headerVerifyInfo(1, dl, &info, &entry.info, 1);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (xx != -1 ||
|
2008-04-26 19:27:30 +08:00
|
|
|
!((entry.info.tag == RPMTAG_HEADERSIGNATURES || entry.info.tag == RPMTAG_HEADERIMAGE)
|
|
|
|
&& entry.info.type == RPM_BIN_TYPE
|
|
|
|
&& entry.info.count == REGION_TAG_COUNT))
|
2002-09-17 04:10:20 +08:00
|
|
|
{
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
2002-09-17 04:10:20 +08:00
|
|
|
_("region trailer: BAD, tag %d type %d offset %d count %d\n"),
|
2008-04-26 19:27:30 +08:00
|
|
|
entry.info.tag, entry.info.type,
|
|
|
|
entry.info.offset, entry.info.count);
|
2002-09-17 04:10:20 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
2008-04-26 19:27:30 +08:00
|
|
|
memset(&info, 0, sizeof(info));
|
2002-09-17 04:10:20 +08:00
|
|
|
|
|
|
|
/* Is the no. of tags in the region less than the total no. of tags? */
|
2008-04-26 19:27:30 +08:00
|
|
|
ril = entry.info.offset/sizeof(*pe);
|
|
|
|
if ((entry.info.offset % sizeof(*pe)) || ril > il) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf, _("region size: BAD, ril(%d) > il(%d)\n"), ril, il);
|
2002-09-17 04:10:20 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
/* Sanity check signature tags */
|
2008-04-26 19:27:30 +08:00
|
|
|
memset(&info, 0, sizeof(info));
|
2002-09-17 04:10:20 +08:00
|
|
|
for (i = 1; i < il; i++) {
|
2008-04-26 19:27:30 +08:00
|
|
|
xx = headerVerifyInfo(1, dl, pe+i, &entry.info, 0);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (xx != -1) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
2002-09-17 04:10:20 +08:00
|
|
|
_("sigh tag[%d]: BAD, tag %d type %d offset %d count %d\n"),
|
2008-04-26 19:27:30 +08:00
|
|
|
i, entry.info.tag, entry.info.type,
|
|
|
|
entry.info.offset, entry.info.count);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
}
|
1996-07-08 06:19:32 +08:00
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
/* OK, blob looks sane, load the header. */
|
|
|
|
sigh = headerLoad(ei);
|
2002-09-17 04:10:20 +08:00
|
|
|
if (sigh == NULL) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf, _("sigh load: BAD\n"));
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2000-12-30 05:44:37 +08:00
|
|
|
|
2007-12-14 15:35:06 +08:00
|
|
|
{ size_t sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
|
|
|
|
size_t pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
|
2007-12-14 17:38:20 +08:00
|
|
|
ssize_t trc;
|
2008-05-23 17:19:16 +08:00
|
|
|
struct rpmtd_s sizetag;
|
2008-06-26 21:28:17 +08:00
|
|
|
rpm_loff_t archSize = 0;
|
1996-02-19 10:32:11 +08:00
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
/* Position at beginning of header. */
|
2007-12-14 17:38:20 +08:00
|
|
|
if (pad && (trc = timedRead(fd, (void *)block, pad)) != pad) {
|
2008-03-31 18:12:29 +08:00
|
|
|
rasprintf(&buf,
|
|
|
|
_("sigh pad(%zd): BAD, read %zd bytes\n"), pad, trc);
|
2002-09-01 06:39:34 +08:00
|
|
|
goto exit;
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
2002-03-11 03:00:31 +08:00
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
/* Print package component sizes. */
|
2008-06-26 21:28:17 +08:00
|
|
|
if (headerGet(sigh, RPMSIGTAG_LONGSIZE, &sizetag, HEADERGET_DEFAULT)) {
|
|
|
|
rpm_loff_t *tsize = rpmtdGetUint64(&sizetag);
|
|
|
|
archSize = (tsize) ? *tsize : 0;
|
|
|
|
} else if (headerGet(sigh, RPMSIGTAG_SIZE, &sizetag, HEADERGET_DEFAULT)) {
|
|
|
|
rpm_off_t *tsize = rpmtdGetUint32(&sizetag);
|
|
|
|
archSize = (tsize) ? *tsize : 0;
|
|
|
|
}
|
|
|
|
rpmtdFreeData(&sizetag);
|
|
|
|
rc = printSize(fd, sigSize, pad, archSize);
|
|
|
|
if (rc != RPMRC_OK) {
|
|
|
|
rasprintf(&buf,
|
|
|
|
_("sigh sigSize(%zd): BAD, fstat(2) failed\n"), sigSize);
|
|
|
|
goto exit;
|
2004-11-06 04:43:10 +08:00
|
|
|
}
|
2002-09-01 06:39:34 +08:00
|
|
|
}
|
2009-06-23 22:50:08 +08:00
|
|
|
ei = NULL; /* XXX will be freed with header */
|
2000-11-07 21:16:43 +08:00
|
|
|
|
2002-09-01 06:39:34 +08:00
|
|
|
exit:
|
2002-09-17 23:21:03 +08:00
|
|
|
if (sighp && sigh && rc == RPMRC_OK)
|
2002-09-01 06:39:34 +08:00
|
|
|
*sighp = headerLink(sigh);
|
|
|
|
sigh = headerFree(sigh);
|
2009-06-23 22:50:08 +08:00
|
|
|
free(ei);
|
2002-09-17 04:10:20 +08:00
|
|
|
|
|
|
|
if (msg != NULL) {
|
2008-03-31 18:12:29 +08:00
|
|
|
*msg = buf;
|
|
|
|
} else {
|
|
|
|
free(buf);
|
2002-09-17 04:10:20 +08:00
|
|
|
}
|
|
|
|
|
2000-11-07 21:16:43 +08:00
|
|
|
return rc;
|
1996-02-19 10:32:11 +08:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
int rpmWriteSignature(FD_t fd, Header sigh)
|
1996-02-20 14:02:32 +08:00
|
|
|
{
|
2007-11-26 17:42:39 +08:00
|
|
|
static uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
2001-02-12 06:02:29 +08:00
|
|
|
int sigSize, pad;
|
|
|
|
int rc;
|
2001-10-19 21:51:20 +08:00
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
rc = headerWrite(fd, sigh, HEADER_MAGIC_YES);
|
1999-08-24 23:18:43 +08:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
|
1996-07-08 06:19:32 +08:00
|
|
|
pad = (8 - (sigSize % 8)) % 8;
|
|
|
|
if (pad) {
|
1999-11-11 06:09:49 +08:00
|
|
|
if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
|
1999-08-24 23:18:43 +08:00
|
|
|
rc = 1;
|
1996-06-29 02:48:34 +08:00
|
|
|
}
|
2008-03-07 15:47:51 +08:00
|
|
|
rpmlog(RPMLOG_DEBUG, "Signature: size(%d)+pad(%d)\n", sigSize, pad);
|
1999-08-24 23:18:43 +08:00
|
|
|
return rc;
|
1996-07-08 06:19:32 +08:00
|
|
|
}
|
1996-06-29 02:48:34 +08:00
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
Header rpmNewSignature(void)
|
1996-07-08 06:19:32 +08:00
|
|
|
{
|
2005-03-09 21:22:48 +08:00
|
|
|
Header sigh = headerNew();
|
|
|
|
return sigh;
|
1996-07-08 06:19:32 +08:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
Header rpmFreeSignature(Header sigh)
|
1996-07-08 06:19:32 +08:00
|
|
|
{
|
2005-03-09 21:22:48 +08:00
|
|
|
return headerFree(sigh);
|
1996-07-08 06:19:32 +08:00
|
|
|
}
|
|
|
|
|
2010-06-29 15:47:18 +08:00
|
|
|
/*
|
|
|
|
* NSS doesn't support everything GPG does. Basic tests to see if the
|
|
|
|
* generated signature is something we can use.
|
|
|
|
*/
|
|
|
|
static int validatePGPSig(pgpDigParams sigp)
|
|
|
|
{
|
2010-09-29 03:13:25 +08:00
|
|
|
pgpPubkeyAlgo pa = sigp->pubkey_algo;
|
2010-06-29 15:47:18 +08:00
|
|
|
/* TODO: query from the implementation instead of hardwiring here */
|
|
|
|
if (pa != PGPPUBKEYALGO_DSA && pa != PGPPUBKEYALGO_RSA) {
|
|
|
|
rpmlog(RPMLOG_ERR, _("Unsupported PGP pubkey algorithm %d\n"),
|
|
|
|
sigp->pubkey_algo);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rpmDigestLength(sigp->hash_algo) == 0) {
|
|
|
|
rpmlog(RPMLOG_ERR, _("Unsupported PGP hash algorithm %d\n"),
|
|
|
|
sigp->hash_algo);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-07 07:17:31 +08:00
|
|
|
/**
|
2005-03-09 21:22:48 +08:00
|
|
|
* Generate GPG signature(s) for a header+payload file.
|
2002-03-07 07:17:31 +08:00
|
|
|
* @param file header+payload file name
|
2005-03-09 21:22:48 +08:00
|
|
|
* @retval *sigTagp signature tag
|
|
|
|
* @retval *pktp signature packet(s)
|
|
|
|
* @retval *pktlenp signature packet(s) length
|
2002-03-07 07:17:31 +08:00
|
|
|
* @param passPhrase private key pass phrase
|
|
|
|
* @return 0 on success, 1 on failure
|
1999-01-09 08:24:02 +08:00
|
|
|
*/
|
2008-02-05 22:35:44 +08:00
|
|
|
static int makeGPGSignature(const char * file, rpmSigTag * sigTagp,
|
2007-12-14 00:16:39 +08:00
|
|
|
uint8_t ** pktp, size_t * pktlenp,
|
2007-09-12 01:07:39 +08:00
|
|
|
const char * passPhrase)
|
1999-01-09 08:24:02 +08:00
|
|
|
{
|
2008-04-29 16:24:25 +08:00
|
|
|
char * sigfile = NULL;
|
1999-01-09 08:24:02 +08:00
|
|
|
int pid, status;
|
|
|
|
int inpipe[2];
|
2001-02-12 06:02:29 +08:00
|
|
|
FILE * fpipe;
|
2000-12-13 04:03:45 +08:00
|
|
|
struct stat st;
|
2002-01-11 03:16:54 +08:00
|
|
|
const char * cmd;
|
|
|
|
char *const *av;
|
2005-03-09 21:22:48 +08:00
|
|
|
pgpDig dig = NULL;
|
|
|
|
pgpDigParams sigp = NULL;
|
2008-04-29 16:24:25 +08:00
|
|
|
int rc = 1; /* assume failure */
|
1999-01-09 08:24:02 +08:00
|
|
|
|
2008-04-29 16:24:25 +08:00
|
|
|
rasprintf(&sigfile, "%s.sig", file);
|
1999-01-09 08:24:02 +08:00
|
|
|
|
2002-01-11 03:16:54 +08:00
|
|
|
addMacro(NULL, "__plaintext_filename", NULL, file, -1);
|
|
|
|
addMacro(NULL, "__signature_filename", NULL, sigfile, -1);
|
|
|
|
|
1999-09-18 05:08:32 +08:00
|
|
|
inpipe[0] = inpipe[1] = 0;
|
2008-05-05 13:27:07 +08:00
|
|
|
if (pipe(inpipe) < 0) {
|
|
|
|
rpmlog(RPMLOG_ERR, _("Couldn't create pipe for signing: %m"));
|
|
|
|
goto exit;
|
|
|
|
}
|
2001-10-19 21:51:20 +08:00
|
|
|
|
1999-01-09 08:24:02 +08:00
|
|
|
if (!(pid = fork())) {
|
2002-01-11 03:16:54 +08:00
|
|
|
const char *gpg_path = rpmExpand("%{?_gpg_path}", NULL);
|
1999-03-21 05:09:47 +08:00
|
|
|
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) dup2(inpipe[0], 3);
|
|
|
|
(void) close(inpipe[1]);
|
1999-03-21 05:09:47 +08:00
|
|
|
|
2002-01-11 03:16:54 +08:00
|
|
|
if (gpg_path && *gpg_path != '\0')
|
2008-06-03 18:39:07 +08:00
|
|
|
(void) setenv("GNUPGHOME", gpg_path, 1);
|
|
|
|
(void) setenv("LC_ALL", "C", 1);
|
2002-01-11 03:16:54 +08:00
|
|
|
|
2002-08-20 06:27:44 +08:00
|
|
|
unsetenv("MALLOC_CHECK_");
|
2002-01-11 03:16:54 +08:00
|
|
|
cmd = rpmExpand("%{?__gpg_sign_cmd}", NULL);
|
|
|
|
rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
|
|
|
|
if (!rc)
|
|
|
|
rc = execve(av[0], av+1, environ);
|
|
|
|
|
2007-11-19 22:25:24 +08:00
|
|
|
rpmlog(RPMLOG_ERR, _("Could not exec %s: %s\n"), "gpg",
|
2002-01-11 03:16:54 +08:00
|
|
|
strerror(errno));
|
2007-12-07 16:57:39 +08:00
|
|
|
_exit(EXIT_FAILURE);
|
1999-01-09 08:24:02 +08:00
|
|
|
}
|
|
|
|
|
2002-01-11 03:16:54 +08:00
|
|
|
delMacro(NULL, "__plaintext_filename");
|
|
|
|
delMacro(NULL, "__signature_filename");
|
|
|
|
|
1999-01-09 08:24:02 +08:00
|
|
|
fpipe = fdopen(inpipe[1], "w");
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) close(inpipe[0]);
|
2001-05-04 05:00:18 +08:00
|
|
|
if (fpipe) {
|
|
|
|
fprintf(fpipe, "%s\n", (passPhrase ? passPhrase : ""));
|
|
|
|
(void) fclose(fpipe);
|
|
|
|
}
|
1999-01-09 08:24:02 +08:00
|
|
|
|
2002-01-11 03:16:54 +08:00
|
|
|
(void) waitpid(pid, &status, 0);
|
1999-01-09 08:24:02 +08:00
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status)) {
|
2007-11-19 22:25:24 +08:00
|
|
|
rpmlog(RPMLOG_ERR, _("gpg exec failed (%d)\n"), WEXITSTATUS(status));
|
2008-04-29 16:24:25 +08:00
|
|
|
goto exit;
|
1999-01-09 08:24:02 +08:00
|
|
|
}
|
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
if (stat(sigfile, &st)) {
|
1999-01-09 08:24:02 +08:00
|
|
|
/* GPG failed to write signature */
|
2007-11-19 22:25:24 +08:00
|
|
|
rpmlog(RPMLOG_ERR, _("gpg failed to write signature\n"));
|
2008-04-29 16:24:25 +08:00
|
|
|
goto exit;
|
1999-01-09 08:24:02 +08:00
|
|
|
}
|
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
*pktlenp = st.st_size;
|
2008-03-07 15:47:51 +08:00
|
|
|
rpmlog(RPMLOG_DEBUG, "GPG sig size: %zd\n", *pktlenp);
|
2005-03-09 21:22:48 +08:00
|
|
|
*pktp = xmalloc(*pktlenp);
|
2001-10-19 21:51:20 +08:00
|
|
|
|
1999-01-09 08:24:02 +08:00
|
|
|
{ FD_t fd;
|
2002-01-11 03:16:54 +08:00
|
|
|
|
|
|
|
rc = 0;
|
2008-07-07 18:28:04 +08:00
|
|
|
fd = Fopen(sigfile, "r.ufdio");
|
2001-05-04 05:00:18 +08:00
|
|
|
if (fd != NULL && !Ferror(fd)) {
|
2008-07-07 18:28:04 +08:00
|
|
|
rc = Fread(*pktp, sizeof(**pktp), *pktlenp, fd);
|
2001-05-04 05:00:18 +08:00
|
|
|
(void) Fclose(fd);
|
|
|
|
}
|
2005-03-09 21:22:48 +08:00
|
|
|
if (rc != *pktlenp) {
|
|
|
|
*pktp = _free(*pktp);
|
2007-11-19 22:25:24 +08:00
|
|
|
rpmlog(RPMLOG_ERR, _("unable to read the signature\n"));
|
2008-04-29 16:24:25 +08:00
|
|
|
goto exit;
|
1999-01-09 08:24:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-07 15:47:51 +08:00
|
|
|
rpmlog(RPMLOG_DEBUG, "Got %zd bytes of GPG sig\n", *pktlenp);
|
2001-10-19 21:51:20 +08:00
|
|
|
|
2005-03-09 21:22:48 +08:00
|
|
|
/* Parse the signature, change signature tag as appropriate. */
|
|
|
|
dig = pgpNewDig();
|
|
|
|
|
2005-03-13 00:36:28 +08:00
|
|
|
(void) pgpPrtPkts(*pktp, *pktlenp, dig, 0);
|
2005-03-09 21:22:48 +08:00
|
|
|
sigp = &dig->signature;
|
|
|
|
|
|
|
|
switch (*sigTagp) {
|
|
|
|
case RPMSIGTAG_GPG:
|
|
|
|
/* XXX check MD5 hash too? */
|
|
|
|
if (sigp->pubkey_algo == PGPPUBKEYALGO_RSA)
|
|
|
|
*sigTagp = RPMSIGTAG_PGP;
|
|
|
|
break;
|
|
|
|
case RPMSIGTAG_PGP5: /* XXX legacy */
|
|
|
|
case RPMSIGTAG_PGP:
|
|
|
|
if (sigp->pubkey_algo == PGPPUBKEYALGO_DSA)
|
|
|
|
*sigTagp = RPMSIGTAG_GPG;
|
|
|
|
break;
|
|
|
|
case RPMSIGTAG_DSA:
|
|
|
|
/* XXX check MD5 hash too? */
|
|
|
|
if (sigp->pubkey_algo == PGPPUBKEYALGO_RSA)
|
|
|
|
*sigTagp = RPMSIGTAG_RSA;
|
|
|
|
break;
|
|
|
|
case RPMSIGTAG_RSA:
|
|
|
|
if (sigp->pubkey_algo == PGPPUBKEYALGO_DSA)
|
|
|
|
*sigTagp = RPMSIGTAG_DSA;
|
|
|
|
break;
|
2009-03-04 21:12:02 +08:00
|
|
|
default:
|
2008-02-05 22:35:44 +08:00
|
|
|
break;
|
2005-03-09 21:22:48 +08:00
|
|
|
}
|
|
|
|
|
2010-06-29 15:47:18 +08:00
|
|
|
rc = validatePGPSig(sigp);
|
2005-03-09 21:22:48 +08:00
|
|
|
dig = pgpFreeDig(dig);
|
|
|
|
|
2008-04-29 16:24:25 +08:00
|
|
|
exit:
|
|
|
|
(void) unlink(sigfile);
|
|
|
|
free(sigfile);
|
|
|
|
|
|
|
|
return rc;
|
1999-01-09 08:24:02 +08:00
|
|
|
}
|
|
|
|
|
2002-03-07 07:17:31 +08:00
|
|
|
/**
|
|
|
|
* Generate header only signature(s) from a header+payload file.
|
2005-03-09 21:22:48 +08:00
|
|
|
* @param sigh signature header
|
2002-03-07 07:17:31 +08:00
|
|
|
* @param file header+payload file name
|
|
|
|
* @param sigTag type of signature(s) to add
|
|
|
|
* @param passPhrase private key pass phrase
|
|
|
|
* @return 0 on success, -1 on failure
|
|
|
|
*/
|
2008-02-05 22:35:44 +08:00
|
|
|
static int makeHDRSignature(Header sigh, const char * file, rpmSigTag sigTag,
|
2007-09-12 01:07:39 +08:00
|
|
|
const char * passPhrase)
|
2002-03-07 07:17:31 +08:00
|
|
|
{
|
|
|
|
Header h = NULL;
|
|
|
|
FD_t fd = NULL;
|
2008-04-29 16:53:13 +08:00
|
|
|
uint8_t * pkt = NULL;
|
2007-12-14 18:54:35 +08:00
|
|
|
size_t pktlen;
|
2007-12-16 22:25:09 +08:00
|
|
|
char * fn = NULL;
|
2007-12-17 03:24:44 +08:00
|
|
|
char * SHA1 = NULL;
|
2002-03-07 07:17:31 +08:00
|
|
|
int ret = -1; /* assume failure. */
|
|
|
|
|
|
|
|
switch (sigTag) {
|
|
|
|
case RPMSIGTAG_SHA1:
|
|
|
|
fd = Fopen(file, "r.fdio");
|
|
|
|
if (fd == NULL || Ferror(fd))
|
|
|
|
goto exit;
|
|
|
|
h = headerRead(fd, HEADER_MAGIC_YES);
|
|
|
|
if (h == NULL)
|
|
|
|
goto exit;
|
|
|
|
(void) Fclose(fd); fd = NULL;
|
|
|
|
|
|
|
|
if (headerIsEntry(h, RPMTAG_HEADERIMMUTABLE)) {
|
|
|
|
DIGEST_CTX ctx;
|
2008-05-23 17:10:14 +08:00
|
|
|
struct rpmtd_s utd;
|
2002-03-07 07:17:31 +08:00
|
|
|
|
2008-05-23 17:10:14 +08:00
|
|
|
if (!headerGet(h, RPMTAG_HEADERIMMUTABLE, &utd, HEADERGET_DEFAULT)
|
|
|
|
|| utd.data == NULL)
|
2002-03-07 07:17:31 +08:00
|
|
|
{
|
2008-05-08 12:48:51 +08:00
|
|
|
rpmlog(RPMLOG_ERR,
|
|
|
|
_("Immutable header region could not be read. "
|
|
|
|
"Corrupted package?\n"));
|
2002-07-14 03:08:51 +08:00
|
|
|
h = headerFree(h);
|
2002-03-07 07:17:31 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
|
2009-01-05 17:18:46 +08:00
|
|
|
(void) rpmDigestUpdate(ctx, rpm_header_magic, sizeof(rpm_header_magic));
|
2008-05-23 17:10:14 +08:00
|
|
|
(void) rpmDigestUpdate(ctx, utd.data, utd.count);
|
2002-07-22 06:06:19 +08:00
|
|
|
(void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
|
2008-05-23 17:10:14 +08:00
|
|
|
rpmtdFreeData(&utd);
|
2002-03-07 07:17:31 +08:00
|
|
|
}
|
2002-07-14 03:08:51 +08:00
|
|
|
h = headerFree(h);
|
2002-03-07 07:17:31 +08:00
|
|
|
|
2002-07-22 06:06:19 +08:00
|
|
|
if (SHA1 == NULL)
|
2002-03-07 07:17:31 +08:00
|
|
|
goto exit;
|
2008-06-18 18:15:46 +08:00
|
|
|
if (!sighdrPut(sigh, RPMSIGTAG_SHA1, RPM_STRING_TYPE, SHA1, 1))
|
2002-03-07 07:17:31 +08:00
|
|
|
goto exit;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case RPMSIGTAG_DSA:
|
2002-03-07 10:17:59 +08:00
|
|
|
case RPMSIGTAG_RSA:
|
|
|
|
fd = Fopen(file, "r.fdio");
|
|
|
|
if (fd == NULL || Ferror(fd))
|
|
|
|
goto exit;
|
|
|
|
h = headerRead(fd, HEADER_MAGIC_YES);
|
|
|
|
if (h == NULL)
|
|
|
|
goto exit;
|
2008-04-11 14:05:05 +08:00
|
|
|
(void) Fclose(fd);
|
2008-05-03 17:34:19 +08:00
|
|
|
fd = rpmMkTempFile(NULL, &fn);
|
2008-04-11 14:05:05 +08:00
|
|
|
if (fd == NULL || Ferror(fd))
|
2002-03-07 10:17:59 +08:00
|
|
|
goto exit;
|
|
|
|
if (headerWrite(fd, h, HEADER_MAGIC_YES))
|
|
|
|
goto exit;
|
|
|
|
(void) Fclose(fd); fd = NULL;
|
2009-03-25 20:17:34 +08:00
|
|
|
if (makeGPGSignature(fn, &sigTag, &pkt, &pktlen, passPhrase)
|
2008-06-18 18:15:46 +08:00
|
|
|
|| !sighdrPut(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
|
2002-03-07 10:17:59 +08:00
|
|
|
goto exit;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2009-03-04 21:12:02 +08:00
|
|
|
default:
|
|
|
|
goto exit;
|
2008-02-05 22:35:44 +08:00
|
|
|
break;
|
2002-03-07 07:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (fn) {
|
|
|
|
(void) unlink(fn);
|
2008-04-29 16:53:13 +08:00
|
|
|
free(fn);
|
2002-03-07 07:17:31 +08:00
|
|
|
}
|
2008-04-29 16:53:13 +08:00
|
|
|
free(pkt);
|
|
|
|
free(SHA1);
|
2002-07-14 03:08:51 +08:00
|
|
|
h = headerFree(h);
|
2003-01-24 04:23:24 +08:00
|
|
|
if (fd != NULL) (void) Fclose(fd);
|
2002-03-07 07:17:31 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-05 22:35:44 +08:00
|
|
|
int rpmAddSignature(Header sigh, const char * file, rpmSigTag sigTag,
|
2001-10-24 00:48:20 +08:00
|
|
|
const char * passPhrase)
|
1996-06-29 02:48:34 +08:00
|
|
|
{
|
2000-12-13 04:03:45 +08:00
|
|
|
struct stat st;
|
2008-04-29 16:53:13 +08:00
|
|
|
uint8_t * pkt = NULL;
|
2007-12-14 18:54:35 +08:00
|
|
|
size_t pktlen;
|
2002-03-07 07:17:31 +08:00
|
|
|
int ret = -1; /* assume failure. */
|
2001-10-19 21:51:20 +08:00
|
|
|
|
1996-07-08 11:27:05 +08:00
|
|
|
switch (sigTag) {
|
2008-06-26 21:28:17 +08:00
|
|
|
case RPMSIGTAG_SIZE: {
|
|
|
|
rpm_off_t size;
|
2002-03-07 07:17:31 +08:00
|
|
|
if (stat(file, &st) != 0)
|
|
|
|
break;
|
2008-06-26 21:28:17 +08:00
|
|
|
size = st.st_size;
|
|
|
|
if (!sighdrPut(sigh, sigTag, RPM_INT32_TYPE, &size, 1))
|
2002-03-07 07:17:31 +08:00
|
|
|
break;
|
1999-07-14 07:33:02 +08:00
|
|
|
ret = 0;
|
2008-06-26 21:28:17 +08:00
|
|
|
} break;
|
|
|
|
case RPMSIGTAG_LONGSIZE: {
|
|
|
|
rpm_loff_t size;
|
|
|
|
if (stat(file, &st) != 0)
|
|
|
|
break;
|
|
|
|
size = st.st_size;
|
|
|
|
if (!sighdrPut(sigh, sigTag, RPM_INT64_TYPE, &size, 1))
|
|
|
|
break;
|
|
|
|
ret = 0;
|
|
|
|
} break;
|
1999-09-11 07:48:56 +08:00
|
|
|
case RPMSIGTAG_MD5:
|
2002-03-07 07:17:31 +08:00
|
|
|
pktlen = 16;
|
2008-04-29 16:54:44 +08:00
|
|
|
pkt = xcalloc(pktlen, sizeof(*pkt));
|
2007-11-22 22:06:11 +08:00
|
|
|
if (rpmDoDigest(PGPHASHALGO_MD5, file, 0, pkt, NULL)
|
2008-06-18 18:15:46 +08:00
|
|
|
|| !sighdrPut(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
|
2002-03-07 07:17:31 +08:00
|
|
|
break;
|
|
|
|
ret = 0;
|
1996-07-08 06:19:32 +08:00
|
|
|
break;
|
1999-09-11 07:48:56 +08:00
|
|
|
case RPMSIGTAG_PGP5: /* XXX legacy */
|
|
|
|
case RPMSIGTAG_PGP:
|
2009-03-26 15:59:01 +08:00
|
|
|
case RPMSIGTAG_GPG: {
|
2010-09-28 19:55:29 +08:00
|
|
|
rpmSigTag hdrtag;
|
2005-03-09 21:22:48 +08:00
|
|
|
if (makeGPGSignature(file, &sigTag, &pkt, &pktlen, passPhrase)
|
2008-06-18 18:15:46 +08:00
|
|
|
|| !sighdrPut(sigh, sigTag, RPM_BIN_TYPE, pkt, pktlen))
|
2002-03-07 07:17:31 +08:00
|
|
|
break;
|
2009-03-26 15:59:01 +08:00
|
|
|
/* XXX Piggyback a header-only DSA/RSA signature as well. */
|
2010-09-28 19:55:29 +08:00
|
|
|
hdrtag = (sigTag == RPMSIGTAG_GPG) ? RPMSIGTAG_DSA : RPMSIGTAG_RSA;
|
2009-03-26 15:59:01 +08:00
|
|
|
ret = makeHDRSignature(sigh, file, hdrtag, passPhrase);
|
|
|
|
} break;
|
2002-03-07 10:17:59 +08:00
|
|
|
case RPMSIGTAG_RSA:
|
2002-03-07 07:17:31 +08:00
|
|
|
case RPMSIGTAG_DSA:
|
|
|
|
case RPMSIGTAG_SHA1:
|
2005-03-09 21:22:48 +08:00
|
|
|
ret = makeHDRSignature(sigh, file, sigTag, passPhrase);
|
2002-03-04 07:09:49 +08:00
|
|
|
break;
|
2009-03-04 21:12:02 +08:00
|
|
|
default:
|
2008-02-05 22:35:44 +08:00
|
|
|
break;
|
1996-07-08 11:27:05 +08:00
|
|
|
}
|
2008-04-29 16:53:13 +08:00
|
|
|
free(pkt);
|
1996-07-08 11:27:05 +08:00
|
|
|
|
1999-07-14 07:33:02 +08:00
|
|
|
return ret;
|
1996-07-08 11:27:05 +08:00
|
|
|
}
|
|
|
|
|
2007-09-12 01:07:39 +08:00
|
|
|
static const char * rpmSigString(rpmRC res)
|
2001-10-24 04:52:51 +08:00
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
switch (res) {
|
2002-08-24 05:01:59 +08:00
|
|
|
case RPMRC_OK: str = "OK"; break;
|
|
|
|
case RPMRC_FAIL: str = "BAD"; break;
|
|
|
|
case RPMRC_NOKEY: str = "NOKEY"; break;
|
|
|
|
case RPMRC_NOTTRUSTED: str = "NOTRUSTED"; break;
|
2002-03-11 03:00:31 +08:00
|
|
|
default:
|
2002-08-24 05:01:59 +08:00
|
|
|
case RPMRC_NOTFOUND: str = "UNKNOWN"; break;
|
2001-10-24 04:52:51 +08:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-08-24 05:01:59 +08:00
|
|
|
static rpmRC
|
2009-03-25 22:35:41 +08:00
|
|
|
verifyMD5Digest(rpmtd sigtd, DIGEST_CTX md5ctx, char **msg)
|
2001-10-20 03:51:18 +08:00
|
|
|
{
|
2009-03-11 19:58:51 +08:00
|
|
|
rpmRC res = RPMRC_FAIL; /* assume failure */
|
2007-11-26 17:42:39 +08:00
|
|
|
uint8_t * md5sum = NULL;
|
2001-10-20 03:51:18 +08:00
|
|
|
size_t md5len = 0;
|
2008-04-07 21:13:29 +08:00
|
|
|
char *md5;
|
|
|
|
const char *title = _("MD5 digest:");
|
|
|
|
*msg = NULL;
|
2009-03-16 20:19:30 +08:00
|
|
|
DIGEST_CTX ctx = rpmDigestDup(md5ctx);
|
2001-10-20 03:51:18 +08:00
|
|
|
|
2009-03-16 20:19:30 +08:00
|
|
|
if (ctx == NULL || sigtd->data == NULL) {
|
2008-04-07 20:01:49 +08:00
|
|
|
rasprintf(msg, "%s %s\n", title, rpmSigString(res));
|
2002-03-04 07:09:49 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:19:30 +08:00
|
|
|
(void) rpmDigestFinal(ctx, (void **)&md5sum, &md5len, 0);
|
2001-10-20 03:51:18 +08:00
|
|
|
|
2008-04-07 20:01:49 +08:00
|
|
|
md5 = pgpHexStr(md5sum, md5len);
|
2008-06-14 19:03:11 +08:00
|
|
|
if (md5len != sigtd->count || memcmp(md5sum, sigtd->data, md5len)) {
|
|
|
|
char *hex = rpmtdFormat(sigtd, RPMTD_FORMAT_STRING, NULL);
|
2008-04-07 20:01:49 +08:00
|
|
|
rasprintf(msg, "%s %s Expected(%s) != (%s)\n", title,
|
|
|
|
rpmSigString(res), hex, md5);
|
|
|
|
free(hex);
|
2001-10-20 03:51:18 +08:00
|
|
|
} else {
|
2002-08-24 05:01:59 +08:00
|
|
|
res = RPMRC_OK;
|
2008-04-07 21:13:29 +08:00
|
|
|
rasprintf(msg, "%s %s (%s)\n", title, rpmSigString(res), md5);
|
2001-10-20 03:51:18 +08:00
|
|
|
}
|
2008-04-07 18:26:46 +08:00
|
|
|
free(md5);
|
2001-10-20 03:51:18 +08:00
|
|
|
|
2002-03-04 07:09:49 +08:00
|
|
|
exit:
|
2001-10-20 03:51:18 +08:00
|
|
|
md5sum = _free(md5sum);
|
2002-03-04 07:09:49 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-07 07:17:31 +08:00
|
|
|
/**
|
|
|
|
* Verify header immutable region SHA1 digest.
|
2008-04-07 19:55:36 +08:00
|
|
|
* @retval msg verbose success/failure text
|
2002-04-09 02:56:01 +08:00
|
|
|
* @param sha1ctx
|
2002-08-24 05:01:59 +08:00
|
|
|
* @return RPMRC_OK on success
|
2002-03-07 07:17:31 +08:00
|
|
|
*/
|
2002-08-24 05:01:59 +08:00
|
|
|
static rpmRC
|
2009-03-25 22:35:41 +08:00
|
|
|
verifySHA1Digest(rpmtd sigtd, DIGEST_CTX sha1ctx, char **msg)
|
2002-03-04 07:09:49 +08:00
|
|
|
{
|
2009-03-11 19:58:51 +08:00
|
|
|
rpmRC res = RPMRC_FAIL; /* assume failure */
|
2007-12-17 03:24:44 +08:00
|
|
|
char * SHA1 = NULL;
|
2008-04-07 19:55:36 +08:00
|
|
|
const char *title = _("Header SHA1 digest:");
|
2008-06-14 19:03:11 +08:00
|
|
|
const char *sig = sigtd->data;
|
2008-04-07 19:55:36 +08:00
|
|
|
*msg = NULL;
|
2009-03-16 20:19:30 +08:00
|
|
|
DIGEST_CTX ctx = rpmDigestDup(sha1ctx);
|
2002-03-04 07:09:49 +08:00
|
|
|
|
2009-03-16 20:19:30 +08:00
|
|
|
if (ctx == NULL || sigtd->data == NULL) {
|
2008-04-07 19:55:36 +08:00
|
|
|
rasprintf(msg, "%s %s\n", title, rpmSigString(res));
|
2002-03-04 07:09:49 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:19:30 +08:00
|
|
|
(void) rpmDigestFinal(ctx, (void **)&SHA1, NULL, 1);
|
2002-03-04 07:09:49 +08:00
|
|
|
|
2009-08-31 17:43:02 +08:00
|
|
|
if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || !rstreq(SHA1, sig)) {
|
2008-04-07 19:55:36 +08:00
|
|
|
rasprintf(msg, "%s %s Expected(%s) != (%s)\n", title,
|
|
|
|
rpmSigString(res), sig, SHA1 ? SHA1 : "(nil)");
|
2002-03-04 07:09:49 +08:00
|
|
|
} else {
|
2002-08-24 05:01:59 +08:00
|
|
|
res = RPMRC_OK;
|
2008-04-07 19:55:36 +08:00
|
|
|
rasprintf(msg, "%s %s (%s)\n", title, rpmSigString(res), SHA1);
|
2002-03-04 07:09:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2002-07-22 06:06:19 +08:00
|
|
|
SHA1 = _free(SHA1);
|
2002-03-04 07:09:49 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-07 07:17:31 +08:00
|
|
|
/**
|
2009-03-25 22:48:24 +08:00
|
|
|
* Verify DSA/RSA signature.
|
2008-07-01 22:00:04 +08:00
|
|
|
* @param keyring pubkey keyring
|
2009-03-25 22:48:24 +08:00
|
|
|
* @param dig OpenPGP container
|
|
|
|
* @param hashctx digest context
|
|
|
|
* @param isHdr header-only signature?
|
|
|
|
* @retval msg verbose success/failure text
|
2002-08-24 05:01:59 +08:00
|
|
|
* @return RPMRC_OK on success
|
2002-03-07 07:17:31 +08:00
|
|
|
*/
|
2002-08-24 05:01:59 +08:00
|
|
|
static rpmRC
|
2009-03-25 22:48:24 +08:00
|
|
|
verifySignature(rpmKeyring keyring, pgpDig dig, DIGEST_CTX hashctx, int isHdr,
|
|
|
|
char **msg)
|
2001-10-20 03:51:18 +08:00
|
|
|
{
|
2008-06-14 19:48:53 +08:00
|
|
|
pgpDigParams sigp = dig ? &dig->signature : NULL;
|
2009-03-11 20:30:16 +08:00
|
|
|
rpmRC res = RPMRC_FAIL; /* assume failure */
|
2009-03-25 22:30:20 +08:00
|
|
|
char *sigid = NULL;
|
2008-04-08 19:06:07 +08:00
|
|
|
*msg = NULL;
|
2001-10-20 03:51:18 +08:00
|
|
|
|
2009-03-25 22:48:24 +08:00
|
|
|
if (hashctx == NULL) {
|
2002-03-04 07:09:49 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-03-04 20:40:21 +08:00
|
|
|
/* Retrieve the matching public key and verify. */
|
|
|
|
res = rpmKeyringLookup(keyring, dig);
|
|
|
|
if (res == RPMRC_OK) {
|
2009-03-25 21:16:59 +08:00
|
|
|
res = pgpVerifySig(dig, hashctx);
|
2009-03-04 17:49:29 +08:00
|
|
|
}
|
2001-10-20 03:51:18 +08:00
|
|
|
|
2001-10-24 04:52:51 +08:00
|
|
|
exit:
|
2009-03-25 22:30:20 +08:00
|
|
|
sigid = pgpIdentItem(sigp);
|
2009-03-25 22:48:24 +08:00
|
|
|
rasprintf(msg, "%s%s: %s\n", isHdr ? _("Header ") : "", sigid,
|
|
|
|
rpmSigString(res));
|
2009-03-25 22:30:20 +08:00
|
|
|
free(sigid);
|
2001-10-20 03:51:18 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-24 05:01:59 +08:00
|
|
|
rpmRC
|
2009-03-25 19:23:19 +08:00
|
|
|
rpmVerifySignature(rpmKeyring keyring, rpmtd sigtd, pgpDig dig, DIGEST_CTX ctx, char ** result)
|
1999-07-14 07:33:02 +08:00
|
|
|
{
|
2009-03-11 21:07:06 +08:00
|
|
|
rpmRC res = RPMRC_NOTFOUND;
|
|
|
|
char *msg = NULL;
|
2008-04-03 13:07:00 +08:00
|
|
|
|
2008-06-14 22:09:50 +08:00
|
|
|
if (sigtd->data == NULL || sigtd->count <= 0 || dig == NULL) {
|
2009-03-11 21:07:06 +08:00
|
|
|
rasprintf(&msg, _("Verify signature: BAD PARAMETERS\n"));
|
|
|
|
goto exit;
|
2001-10-24 04:52:51 +08:00
|
|
|
}
|
|
|
|
|
2008-06-14 22:09:50 +08:00
|
|
|
switch (sigtd->tag) {
|
1999-09-11 07:48:56 +08:00
|
|
|
case RPMSIGTAG_MD5:
|
2009-03-25 22:35:41 +08:00
|
|
|
res = verifyMD5Digest(sigtd, ctx, &msg);
|
2001-10-20 03:51:18 +08:00
|
|
|
break;
|
2002-03-04 07:09:49 +08:00
|
|
|
case RPMSIGTAG_SHA1:
|
2009-03-25 22:35:41 +08:00
|
|
|
res = verifySHA1Digest(sigtd, ctx, &msg);
|
2002-03-04 07:09:49 +08:00
|
|
|
break;
|
2002-03-07 10:17:59 +08:00
|
|
|
case RPMSIGTAG_RSA:
|
2009-03-25 22:48:24 +08:00
|
|
|
case RPMSIGTAG_DSA:
|
|
|
|
res = verifySignature(keyring, dig, ctx, 1, &msg);
|
|
|
|
break;
|
1999-09-11 07:48:56 +08:00
|
|
|
case RPMSIGTAG_PGP5: /* XXX legacy */
|
|
|
|
case RPMSIGTAG_PGP:
|
|
|
|
case RPMSIGTAG_GPG:
|
2009-03-25 22:48:24 +08:00
|
|
|
res = verifySignature(keyring, dig, ctx, 0, &msg);
|
2001-10-20 03:51:18 +08:00
|
|
|
break;
|
1999-09-11 07:48:56 +08:00
|
|
|
default:
|
2009-03-11 21:07:06 +08:00
|
|
|
rasprintf(&msg, _("Signature: UNKNOWN (%d)\n"), sigtd->tag);
|
2001-10-20 03:51:18 +08:00
|
|
|
break;
|
1999-07-14 07:33:02 +08:00
|
|
|
}
|
2009-03-11 21:07:06 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if (result) {
|
|
|
|
*result = msg;
|
|
|
|
} else {
|
|
|
|
free(msg);
|
|
|
|
}
|
2001-10-20 03:51:18 +08:00
|
|
|
return res;
|
1999-07-14 07:33:02 +08:00
|
|
|
}
|