2008-12-07 12:58:24 -05:00
|
|
|
/*
|
|
|
|
* signing.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008-2011 Pacman Development Team <pacman-dev@archlinux.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <locale.h> /* setlocale() */
|
|
|
|
#include <gpgme.h>
|
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "signing.h"
|
|
|
|
#include "package.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "alpm.h"
|
|
|
|
|
|
|
|
#define CHECK_ERR(void) do { \
|
|
|
|
if(err != GPG_ERR_NO_ERROR) { goto error; } \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
static int gpgme_init(void)
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
const char *version;
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_engine_info_t enginfo;
|
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(init) {
|
|
|
|
/* we already successfully initialized the library */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!alpm_option_get_signaturedir()) {
|
|
|
|
RET_ERR(PM_ERR_SIG_MISSINGDIR, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calling gpgme_check_version() returns the current version and runs
|
|
|
|
* some internal library setup code */
|
|
|
|
version = gpgme_check_version(NULL);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "GPGME version: %s\n", version);
|
|
|
|
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
|
|
|
|
#ifdef LC_MESSAGES
|
|
|
|
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
|
|
|
|
#endif
|
|
|
|
/* NOTE:
|
|
|
|
* The GPGME library installs a SIGPIPE signal handler automatically if
|
|
|
|
* the default signal hander is in use. The only time we set a handler
|
|
|
|
* for SIGPIPE is in dload.c, and we reset it when we are done. Given that
|
|
|
|
* we do this, we can let GPGME do its automagic. However, if we install
|
|
|
|
* a library-wide SIGPIPE handler, we will have to be careful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* check for OpenPGP support (should be a no-brainer, but be safe) */
|
|
|
|
err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
/* set and check engine information */
|
|
|
|
err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL,
|
|
|
|
alpm_option_get_signaturedir());
|
|
|
|
CHECK_ERR();
|
|
|
|
err = gpgme_get_engine_info(&enginfo);
|
|
|
|
CHECK_ERR();
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "GPGME engine info: file=%s, home=%s\n",
|
|
|
|
enginfo->file_name, enginfo->home_dir);
|
|
|
|
|
|
|
|
init = 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
_alpm_log(PM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
|
|
|
|
RET_ERR(PM_ERR_GPGME, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-22 00:06:09 -05:00
|
|
|
* Check the PGP package signature for the given file.
|
|
|
|
* @param path the full path to a file
|
2008-12-07 12:58:24 -05:00
|
|
|
* @param sig PGP signature data in raw form (already decoded)
|
|
|
|
* @return a int value : 0 (valid), 1 (invalid), -1 (an error occured)
|
|
|
|
*/
|
2010-11-22 00:06:09 -05:00
|
|
|
int _alpm_gpgme_checksig(const char *path, const pmpgpsig_t *sig)
|
2008-12-07 12:58:24 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_ctx_t ctx;
|
2010-11-22 00:06:09 -05:00
|
|
|
gpgme_data_t filedata, sigdata;
|
2008-12-07 12:58:24 -05:00
|
|
|
gpgme_verify_result_t result;
|
|
|
|
gpgme_signature_t gpgsig;
|
2010-11-22 00:06:09 -05:00
|
|
|
FILE *file = NULL, *sigfile = NULL;
|
2008-12-07 12:58:24 -05:00
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
2011-04-15 20:30:11 -04:00
|
|
|
if(!sig || !sig->data) {
|
2008-12-07 12:58:24 -05:00
|
|
|
RET_ERR(PM_ERR_SIG_UNKNOWN, -1);
|
|
|
|
}
|
2010-11-22 00:06:09 -05:00
|
|
|
if(!path || access(path, R_OK) != 0) {
|
|
|
|
RET_ERR(PM_ERR_NOT_A_FILE, -1);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
if(gpgme_init()) {
|
|
|
|
/* pm_errno was set in gpgme_init() */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-11-22 00:06:09 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "checking signature for %s\n", path);
|
2008-12-07 12:58:24 -05:00
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
memset(&sigdata, 0, sizeof(sigdata));
|
2010-11-22 00:06:09 -05:00
|
|
|
memset(&filedata, 0, sizeof(filedata));
|
2008-12-07 12:58:24 -05:00
|
|
|
|
|
|
|
err = gpgme_new(&ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
/* create our necessary data objects to verify the signature */
|
2010-11-22 00:06:09 -05:00
|
|
|
file = fopen(path, "rb");
|
|
|
|
if(file == NULL) {
|
|
|
|
pm_errno = PM_ERR_NOT_A_FILE;
|
2008-12-07 12:58:24 -05:00
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-11-22 00:06:09 -05:00
|
|
|
err = gpgme_data_new_from_stream(&filedata, file);
|
2008-12-07 12:58:24 -05:00
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
/* next create data object for the signature */
|
2011-04-15 20:30:11 -04:00
|
|
|
err = gpgme_data_new_from_mem(&sigdata, (char *)sig->data, sig->len, 0);
|
2008-12-07 12:58:24 -05:00
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
/* here's where the magic happens */
|
2010-11-22 00:06:09 -05:00
|
|
|
err = gpgme_op_verify(ctx, sigdata, filedata, NULL);
|
2008-12-07 12:58:24 -05:00
|
|
|
CHECK_ERR();
|
|
|
|
result = gpgme_op_verify_result(ctx);
|
2010-11-24 02:22:32 -05:00
|
|
|
gpgsig = result->signatures;
|
2011-04-20 20:45:16 -04:00
|
|
|
if(!gpgsig || gpgsig->next) {
|
2008-12-07 12:58:24 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, _("Unexpected number of signatures\n"));
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-11-22 23:51:44 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "summary=%x\n", gpgsig->summary);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "fpr=%s\n", gpgsig->fpr);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "status=%d\n", gpgsig->status);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "timestamp=%lu\n", gpgsig->timestamp);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "wrong_key_usage=%u\n", gpgsig->wrong_key_usage);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "pka_trust=%u\n", gpgsig->pka_trust);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "chain_model=%u\n", gpgsig->chain_model);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "validity=%d\n", gpgsig->validity);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "validity_reason=%d\n", gpgsig->validity_reason);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "key=%d\n", gpgsig->pubkey_algo);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "hash=%d\n", gpgsig->hash_algo);
|
2008-12-07 12:58:24 -05:00
|
|
|
|
|
|
|
if(gpgsig->summary & GPGME_SIGSUM_VALID) {
|
|
|
|
/* good signature, continue */
|
2010-11-22 00:06:09 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("File %s has a valid signature.\n"),
|
|
|
|
path);
|
2008-12-07 12:58:24 -05:00
|
|
|
} else if(gpgsig->summary & GPGME_SIGSUM_GREEN) {
|
|
|
|
/* 'green' signature, not sure what to do here */
|
2010-11-22 00:06:09 -05:00
|
|
|
_alpm_log(PM_LOG_WARNING, _("File %s has a green signature.\n"),
|
|
|
|
path);
|
2008-12-07 12:58:24 -05:00
|
|
|
} else if(gpgsig->summary & GPGME_SIGSUM_KEY_MISSING) {
|
|
|
|
pm_errno = PM_ERR_SIG_UNKNOWN;
|
2010-11-22 00:06:09 -05:00
|
|
|
_alpm_log(PM_LOG_WARNING, _("File %s has a signature from an unknown key.\n"),
|
|
|
|
path);
|
2008-12-07 12:58:24 -05:00
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
/* we'll capture everything else here */
|
|
|
|
pm_errno = PM_ERR_SIG_INVALID;
|
2010-11-22 00:06:09 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, _("File %s has an invalid signature.\n"),
|
|
|
|
path);
|
2008-12-07 12:58:24 -05:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
gpgme_data_release(sigdata);
|
2010-11-22 00:06:09 -05:00
|
|
|
gpgme_data_release(filedata);
|
2008-12-07 12:58:24 -05:00
|
|
|
gpgme_release(ctx);
|
|
|
|
if(sigfile) {
|
|
|
|
fclose(sigfile);
|
|
|
|
}
|
2010-11-22 00:06:09 -05:00
|
|
|
if(file) {
|
|
|
|
fclose(file);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
if(err != GPG_ERR_NO_ERROR) {
|
|
|
|
_alpm_log(PM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
|
|
|
|
RET_ERR(PM_ERR_GPGME, -1);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-23 23:56:54 -04:00
|
|
|
/**
|
|
|
|
* Load the signature from the given path into the provided struct.
|
|
|
|
* @param sigfile the signature to attempt to load
|
|
|
|
* @param pgpsig the struct to place the data in
|
|
|
|
*
|
|
|
|
* @return 0 on success, 1 on file not found, -1 on error
|
|
|
|
*/
|
2011-03-28 13:48:37 -04:00
|
|
|
int _alpm_load_signature(const char *file, pmpgpsig_t *pgpsig) {
|
2011-03-23 23:56:54 -04:00
|
|
|
struct stat st;
|
2011-03-28 13:48:37 -04:00
|
|
|
char *sigfile;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* look around for a PGP signature file; load if available */
|
|
|
|
MALLOC(sigfile, strlen(file) + 5, RET_ERR(PM_ERR_MEMORY, -1));
|
|
|
|
sprintf(sigfile, "%s.sig", file);
|
2011-03-23 23:56:54 -04:00
|
|
|
|
|
|
|
if(access(sigfile, R_OK) == 0 && stat(sigfile, &st) == 0) {
|
|
|
|
FILE *f;
|
|
|
|
size_t bytes_read;
|
|
|
|
|
2011-03-28 13:48:37 -04:00
|
|
|
if(st.st_size > 4096 || (f = fopen(sigfile, "rb")) == NULL) {
|
|
|
|
free(sigfile);
|
|
|
|
return ret;
|
2011-03-23 23:56:54 -04:00
|
|
|
}
|
2011-04-15 20:30:11 -04:00
|
|
|
CALLOC(pgpsig->data, st.st_size, sizeof(unsigned char),
|
2011-03-23 23:56:54 -04:00
|
|
|
RET_ERR(PM_ERR_MEMORY, -1));
|
2011-04-15 20:30:11 -04:00
|
|
|
bytes_read = fread(pgpsig->data, sizeof(char), st.st_size, f);
|
2011-03-23 23:56:54 -04:00
|
|
|
if(bytes_read == (size_t)st.st_size) {
|
2011-04-15 20:30:11 -04:00
|
|
|
pgpsig->len = bytes_read;
|
2011-03-23 23:56:54 -04:00
|
|
|
_alpm_log(PM_LOG_DEBUG, "loaded gpg signature file, location %s\n",
|
|
|
|
sigfile);
|
2011-03-28 13:48:37 -04:00
|
|
|
ret = 0;
|
2011-03-23 23:56:54 -04:00
|
|
|
} else {
|
|
|
|
_alpm_log(PM_LOG_WARNING, _("Failed reading PGP signature file %s"),
|
|
|
|
sigfile);
|
2011-04-15 20:30:11 -04:00
|
|
|
FREE(pgpsig->data);
|
2011-03-23 23:56:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "signature file %s not found\n", sigfile);
|
|
|
|
/* not fatal...we return a different error code here */
|
2011-03-28 13:48:37 -04:00
|
|
|
ret = 1;
|
2011-03-23 23:56:54 -04:00
|
|
|
}
|
|
|
|
|
2011-03-28 13:48:37 -04:00
|
|
|
free(sigfile);
|
|
|
|
return ret;
|
2011-03-23 23:56:54 -04:00
|
|
|
}
|
|
|
|
|
2011-03-25 21:40:16 -04:00
|
|
|
/**
|
|
|
|
* Determines the necessity of checking for a valid PGP signature
|
|
|
|
* @param db the sync database to query
|
|
|
|
*
|
|
|
|
* @return signature verification level
|
|
|
|
*/
|
|
|
|
pgp_verify_t _alpm_db_get_sigverify_level(pmdb_t *db)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, PM_PGP_VERIFY_UNKNOWN));
|
|
|
|
|
|
|
|
if(db->pgp_verify != PM_PGP_VERIFY_UNKNOWN) {
|
|
|
|
return db->pgp_verify;
|
|
|
|
} else {
|
|
|
|
return alpm_option_get_default_sigverify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/**
|
|
|
|
* Check the PGP package signature for the given package file.
|
|
|
|
* @param pkg the package to check
|
2011-03-25 21:40:16 -04:00
|
|
|
* @return a int value : 0 (valid), 1 (invalid), -1 (an error occurred)
|
2008-12-07 12:58:24 -05:00
|
|
|
*/
|
|
|
|
int SYMEXPORT alpm_pkg_check_pgp_signature(pmpkg_t *pkg)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
ASSERT(pkg != NULL, return 0);
|
|
|
|
|
|
|
|
return _alpm_gpgme_checksig(alpm_pkg_get_filename(pkg),
|
|
|
|
alpm_pkg_get_pgpsig(pkg));
|
|
|
|
}
|
|
|
|
|
2010-11-22 00:06:09 -05:00
|
|
|
/**
|
|
|
|
* Check the PGP package signature for the given database.
|
|
|
|
* @param db the database to check
|
2011-03-25 21:40:16 -04:00
|
|
|
* @return a int value : 0 (valid), 1 (invalid), -1 (an error occurred)
|
2010-11-22 00:06:09 -05:00
|
|
|
*/
|
|
|
|
int SYMEXPORT alpm_db_check_pgp_signature(pmdb_t *db)
|
|
|
|
{
|
|
|
|
ALPM_LOG_FUNC;
|
2011-03-25 21:40:16 -04:00
|
|
|
ASSERT(db != NULL, return 0);
|
2010-11-22 00:06:09 -05:00
|
|
|
|
2011-03-23 23:56:54 -04:00
|
|
|
return _alpm_gpgme_checksig(_alpm_db_path(db),
|
|
|
|
_alpm_db_pgpsig(db));
|
2010-11-22 00:06:09 -05:00
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|