2008-12-07 12:58:24 -05:00
|
|
|
/*
|
|
|
|
* signing.c
|
|
|
|
*
|
2013-01-01 23:19:59 -05:00
|
|
|
* Copyright (c) 2008-2013 Pacman Development Team <pacman-dev@archlinux.org>
|
2008-12-07 12:58:24 -05:00
|
|
|
*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-04-24 12:40:58 -04:00
|
|
|
|
|
|
|
#if HAVE_LIBGPGME
|
2008-12-07 12:58:24 -05:00
|
|
|
#include <locale.h> /* setlocale() */
|
|
|
|
#include <gpgme.h>
|
2011-04-24 12:40:58 -04:00
|
|
|
#include "base64.h"
|
|
|
|
#endif
|
2008-12-07 12:58:24 -05:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "signing.h"
|
|
|
|
#include "package.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "alpm.h"
|
2011-06-07 17:06:16 -04:00
|
|
|
#include "handle.h"
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-04-24 12:40:58 -04:00
|
|
|
#if HAVE_LIBGPGME
|
2008-12-07 12:58:24 -05:00
|
|
|
#define CHECK_ERR(void) do { \
|
2013-02-09 20:08:36 -05:00
|
|
|
if(gpg_err_code(err) != GPG_ERR_NO_ERROR) { goto gpg_error; } \
|
2008-12-07 12:58:24 -05:00
|
|
|
} while(0)
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Return a statically allocated validity string based on the GPGME validity
|
|
|
|
* code. This is mainly for debug purposes and is not translated.
|
|
|
|
* @param validity a validity code returned by GPGME
|
|
|
|
* @return a string such as "marginal"
|
|
|
|
*/
|
2011-06-14 11:15:43 -04:00
|
|
|
static const char *string_validity(gpgme_validity_t validity)
|
2011-04-22 16:55:54 -04:00
|
|
|
{
|
|
|
|
switch(validity) {
|
|
|
|
case GPGME_VALIDITY_UNKNOWN:
|
|
|
|
return "unknown";
|
|
|
|
case GPGME_VALIDITY_UNDEFINED:
|
|
|
|
return "undefined";
|
|
|
|
case GPGME_VALIDITY_NEVER:
|
|
|
|
return "never";
|
|
|
|
case GPGME_VALIDITY_MARGINAL:
|
|
|
|
return "marginal";
|
|
|
|
case GPGME_VALIDITY_FULL:
|
|
|
|
return "full";
|
|
|
|
case GPGME_VALIDITY_ULTIMATE:
|
|
|
|
return "ultimate";
|
|
|
|
}
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2011-06-14 11:15:43 -04:00
|
|
|
static void sigsum_test_bit(gpgme_sigsum_t sigsum, alpm_list_t **summary,
|
2011-05-04 16:42:50 -04:00
|
|
|
gpgme_sigsum_t bit, const char *value)
|
|
|
|
{
|
|
|
|
if(sigsum & bit) {
|
2011-06-14 11:15:43 -04:00
|
|
|
*summary = alpm_list_add(*summary, (void *)value);
|
2011-05-04 16:42:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Calculate a set of strings to represent the given GPGME signature summary
|
|
|
|
* value. This is a bitmask so you may get any number of strings back.
|
|
|
|
* @param sigsum a GPGME signature summary bitmask
|
|
|
|
* @return the list of signature summary strings
|
|
|
|
*/
|
2011-06-14 11:15:43 -04:00
|
|
|
static alpm_list_t *list_sigsum(gpgme_sigsum_t sigsum)
|
2011-04-22 16:55:54 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *summary = NULL;
|
|
|
|
/* The docs say this can be a bitmask...not sure I believe it, but we'll code
|
|
|
|
* for it anyway and show all possible flags in the returned string. */
|
|
|
|
|
|
|
|
/* The signature is fully valid. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_VALID, "valid");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* The signature is good. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_GREEN, "green");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* The signature is bad. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_RED, "red");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* One key has been revoked. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_REVOKED, "key revoked");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* One key has expired. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_EXPIRED, "key expired");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* The signature has expired. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_SIG_EXPIRED, "sig expired");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* Can't verify: key missing. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_KEY_MISSING, "key missing");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* CRL not available. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_CRL_MISSING, "crl missing");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* Available CRL is too old. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_CRL_TOO_OLD, "crl too old");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* A policy was not met. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_BAD_POLICY, "bad policy");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* A system error occured. */
|
2011-06-14 11:15:43 -04:00
|
|
|
sigsum_test_bit(sigsum, &summary, GPGME_SIGSUM_SYS_ERROR, "sys error");
|
2011-04-22 16:55:54 -04:00
|
|
|
/* Fallback case */
|
|
|
|
if(!sigsum) {
|
2011-05-04 16:42:50 -04:00
|
|
|
summary = alpm_list_add(summary, (void *)"(empty)");
|
2011-04-22 16:55:54 -04:00
|
|
|
}
|
|
|
|
return summary;
|
|
|
|
}
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Initialize the GPGME library.
|
|
|
|
* This can be safely called multiple times; however it is not thread-safe.
|
|
|
|
* @param handle the context handle
|
2011-09-22 15:40:51 -04:00
|
|
|
* @return 0 on success, -1 on error
|
2011-09-22 12:02:58 -04:00
|
|
|
*/
|
2011-06-28 00:04:00 -04:00
|
|
|
static int init_gpgme(alpm_handle_t *handle)
|
2008-12-07 12:58:24 -05:00
|
|
|
{
|
|
|
|
static int init = 0;
|
2011-06-14 11:15:43 -04:00
|
|
|
const char *version, *sigdir;
|
2008-12-07 12:58:24 -05:00
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_engine_info_t enginfo;
|
|
|
|
|
|
|
|
if(init) {
|
|
|
|
/* we already successfully initialized the library */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 20:12:21 -04:00
|
|
|
sigdir = handle->gpgdir;
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-09-22 16:47:57 -04:00
|
|
|
if(_alpm_access(handle, sigdir, "pubring.gpg", R_OK)
|
2011-07-06 11:24:19 -04:00
|
|
|
|| _alpm_access(handle, sigdir, "trustdb.gpg", R_OK)) {
|
|
|
|
handle->pm_errno = ALPM_ERR_NOT_A_FILE;
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "Signature verification will fail!\n");
|
2011-10-12 18:32:54 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_WARNING,
|
|
|
|
_("Public keyring not found; have you run '%s'?\n"),
|
|
|
|
"pacman-key --init");
|
2011-07-06 11:24:19 -04:00
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/* calling gpgme_check_version() returns the current version and runs
|
|
|
|
* some internal library setup code */
|
|
|
|
version = gpgme_check_version(NULL);
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "GPGME version: %s\n", version);
|
2008-12-07 12:58:24 -05:00
|
|
|
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 */
|
2011-06-14 11:15:43 -04:00
|
|
|
err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, sigdir);
|
2008-12-07 12:58:24 -05:00
|
|
|
CHECK_ERR();
|
|
|
|
err = gpgme_get_engine_info(&enginfo);
|
|
|
|
CHECK_ERR();
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "GPGME engine info: file=%s, home=%s\n",
|
2008-12-07 12:58:24 -05:00
|
|
|
enginfo->file_name, enginfo->home_dir);
|
|
|
|
|
|
|
|
init = 1;
|
|
|
|
return 0;
|
|
|
|
|
2013-02-09 20:08:36 -05:00
|
|
|
gpg_error:
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
|
2011-09-22 15:40:51 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_GPGME, -1);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-20 17:54:08 -04:00
|
|
|
/**
|
|
|
|
* Determine if we have a key is known in our local keyring.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param fpr the fingerprint key ID to look up
|
|
|
|
* @return 1 if key is known, 0 if key is unknown, -1 on error
|
|
|
|
*/
|
2012-10-29 06:51:04 -04:00
|
|
|
int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr)
|
2011-09-20 17:54:08 -04:00
|
|
|
{
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_key_t key;
|
|
|
|
int ret = -1;
|
|
|
|
|
2012-11-02 11:16:32 -04:00
|
|
|
if(init_gpgme(handle)) {
|
|
|
|
/* pm_errno was set in gpgme_init() */
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-09-20 17:54:08 -04:00
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
err = gpgme_new(&ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "looking up key %s locally\n", fpr);
|
|
|
|
|
|
|
|
err = gpgme_get_key(ctx, fpr, &key, 0);
|
|
|
|
if(gpg_err_code(err) == GPG_ERR_EOF) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n");
|
|
|
|
ret = 0;
|
|
|
|
} else if(gpg_err_code(err) == GPG_ERR_NO_ERROR) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup success, key exists\n");
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "gpg error: %s\n", gpgme_strerror(err));
|
|
|
|
}
|
2012-04-09 01:42:04 -04:00
|
|
|
gpgme_key_unref(key);
|
2011-09-20 17:54:08 -04:00
|
|
|
|
2013-02-09 20:08:36 -05:00
|
|
|
gpg_error:
|
2011-09-20 17:54:08 -04:00
|
|
|
gpgme_release(ctx);
|
2013-02-09 20:08:36 -05:00
|
|
|
|
|
|
|
error:
|
2011-09-20 17:54:08 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a GPG key in a remote location.
|
|
|
|
* This requires GPGME to call the gpg binary and have a keyserver previously
|
|
|
|
* defined in a gpg.conf configuration file.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param fpr the fingerprint key ID to look up
|
|
|
|
* @param pgpkey storage location for the given key if found
|
2011-09-22 15:40:51 -04:00
|
|
|
* @return 1 on success, 0 on key not found, -1 on error
|
2011-09-20 17:54:08 -04:00
|
|
|
*/
|
|
|
|
static int key_search(alpm_handle_t *handle, const char *fpr,
|
|
|
|
alpm_pgpkey_t *pgpkey)
|
|
|
|
{
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_keylist_mode_t mode;
|
|
|
|
gpgme_key_t key;
|
2011-09-22 15:40:51 -04:00
|
|
|
int ret = -1;
|
2012-03-27 23:10:34 -04:00
|
|
|
size_t fpr_len;
|
|
|
|
char *full_fpr;
|
|
|
|
|
|
|
|
/* gpg2 goes full retard here. For key searches ONLY, we need to prefix the
|
|
|
|
* key fingerprint with 0x, or the lookup will fail. */
|
|
|
|
fpr_len = strlen(fpr);
|
|
|
|
MALLOC(full_fpr, fpr_len + 3, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
|
|
|
|
sprintf(full_fpr, "0x%s", fpr);
|
2011-09-20 17:54:08 -04:00
|
|
|
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
err = gpgme_new(&ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
mode = gpgme_get_keylist_mode(ctx);
|
|
|
|
/* using LOCAL and EXTERN together doesn't work for GPG 1.X. Ugh. */
|
|
|
|
mode &= ~GPGME_KEYLIST_MODE_LOCAL;
|
|
|
|
mode |= GPGME_KEYLIST_MODE_EXTERN;
|
|
|
|
err = gpgme_set_keylist_mode(ctx, mode);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "looking up key %s remotely\n", fpr);
|
|
|
|
|
2012-03-27 23:10:34 -04:00
|
|
|
err = gpgme_get_key(ctx, full_fpr, &key, 0);
|
2011-09-20 17:54:08 -04:00
|
|
|
if(gpg_err_code(err) == GPG_ERR_EOF) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n");
|
2012-01-05 17:34:51 -05:00
|
|
|
/* Try an alternate lookup using the 8 character fingerprint value, since
|
|
|
|
* busted-ass keyservers can't support lookups using subkeys with the full
|
|
|
|
* value as of now. This is why 2012 is not the year of PGP encryption. */
|
2012-03-27 23:10:34 -04:00
|
|
|
if(fpr_len > 8) {
|
|
|
|
const char *short_fpr = memcpy(&full_fpr[fpr_len - 8], "0x", 2);
|
2012-01-05 17:34:51 -05:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG,
|
|
|
|
"looking up key %s remotely\n", short_fpr);
|
|
|
|
err = gpgme_get_key(ctx, short_fpr, &key, 0);
|
|
|
|
if(gpg_err_code(err) == GPG_ERR_EOF) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n");
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 20:08:36 -05:00
|
|
|
CHECK_ERR();
|
2011-09-20 17:54:08 -04:00
|
|
|
|
|
|
|
/* should only get here if key actually exists */
|
|
|
|
pgpkey->data = key;
|
|
|
|
if(key->subkeys->fpr) {
|
|
|
|
pgpkey->fingerprint = key->subkeys->fpr;
|
|
|
|
} else if(key->subkeys->keyid) {
|
|
|
|
pgpkey->fingerprint = key->subkeys->keyid;
|
|
|
|
}
|
|
|
|
pgpkey->uid = key->uids->uid;
|
|
|
|
pgpkey->name = key->uids->name;
|
|
|
|
pgpkey->email = key->uids->email;
|
|
|
|
pgpkey->created = key->subkeys->timestamp;
|
|
|
|
pgpkey->expires = key->subkeys->expires;
|
2011-10-18 16:46:50 -04:00
|
|
|
pgpkey->length = key->subkeys->length;
|
|
|
|
pgpkey->revoked = key->subkeys->revoked;
|
2011-10-20 14:39:28 -04:00
|
|
|
|
2013-01-03 16:48:53 -05:00
|
|
|
switch(key->subkeys->pubkey_algo) {
|
2011-10-20 14:39:28 -04:00
|
|
|
case GPGME_PK_RSA:
|
|
|
|
case GPGME_PK_RSA_E:
|
|
|
|
case GPGME_PK_RSA_S:
|
|
|
|
pgpkey->pubkey_algo = 'R';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPGME_PK_DSA:
|
|
|
|
pgpkey->pubkey_algo = 'D';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPGME_PK_ELG_E:
|
|
|
|
case GPGME_PK_ELG:
|
|
|
|
case GPGME_PK_ECDSA:
|
|
|
|
case GPGME_PK_ECDH:
|
|
|
|
pgpkey->pubkey_algo = 'E';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-22 15:40:51 -04:00
|
|
|
ret = 1;
|
2011-09-20 17:54:08 -04:00
|
|
|
|
2013-02-09 20:08:36 -05:00
|
|
|
gpg_error:
|
2012-01-08 13:14:04 -05:00
|
|
|
if(ret != 1) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "gpg error: %s\n", gpgme_strerror(err));
|
|
|
|
}
|
2012-03-27 23:10:34 -04:00
|
|
|
free(full_fpr);
|
2011-09-20 17:54:08 -04:00
|
|
|
gpgme_release(ctx);
|
2011-09-22 15:40:51 -04:00
|
|
|
return ret;
|
2011-09-20 17:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a key into the local keyring.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param key the key to import, likely retrieved from #key_search
|
2011-09-22 15:40:51 -04:00
|
|
|
* @return 0 on success, -1 on error
|
2011-09-20 17:54:08 -04:00
|
|
|
*/
|
|
|
|
static int key_import(alpm_handle_t *handle, alpm_pgpkey_t *key)
|
|
|
|
{
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_key_t keys[2];
|
2011-09-22 16:47:57 -04:00
|
|
|
gpgme_import_result_t result;
|
2011-09-22 15:40:51 -04:00
|
|
|
int ret = -1;
|
2011-09-20 17:54:08 -04:00
|
|
|
|
2011-09-22 16:47:57 -04:00
|
|
|
if(_alpm_access(handle, handle->gpgdir, "pubring.gpg", W_OK)) {
|
|
|
|
/* no chance of import succeeding if pubring isn't writable */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR, _("keyring is not writable\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-20 17:54:08 -04:00
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
err = gpgme_new(&ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "importing key\n");
|
|
|
|
|
|
|
|
keys[0] = key->data;
|
|
|
|
keys[1] = NULL;
|
|
|
|
err = gpgme_op_import_keys(ctx, keys);
|
|
|
|
CHECK_ERR();
|
2011-09-22 16:47:57 -04:00
|
|
|
result = gpgme_op_import_result(ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
/* we know we tried to import exactly one key, so check for this */
|
|
|
|
if(result->considered != 1 || !result->imports) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "could not import key, 0 results\n");
|
|
|
|
ret = -1;
|
|
|
|
} else if(result->imports->result != GPG_ERR_NO_ERROR) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "gpg error: %s\n", gpgme_strerror(err));
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2011-09-20 17:54:08 -04:00
|
|
|
|
2013-02-09 20:08:36 -05:00
|
|
|
gpg_error:
|
2011-09-20 17:54:08 -04:00
|
|
|
gpgme_release(ctx);
|
2011-09-22 15:40:51 -04:00
|
|
|
return ret;
|
2011-09-20 17:54:08 -04:00
|
|
|
}
|
|
|
|
|
2012-10-29 08:24:55 -04:00
|
|
|
/**
|
|
|
|
* Import a key defined by a fingerprint into the local keyring.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param fpr the fingerprint key ID to import
|
|
|
|
* @return 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int _alpm_key_import(alpm_handle_t *handle, const char *fpr) {
|
|
|
|
int answer = 0, ret = -1;
|
|
|
|
alpm_pgpkey_t fetch_key;
|
|
|
|
memset(&fetch_key, 0, sizeof(fetch_key));
|
|
|
|
|
|
|
|
if(key_search(handle, fpr, &fetch_key) == 1) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG,
|
|
|
|
"unknown key, found %s on keyserver\n", fetch_key.uid);
|
|
|
|
if(!_alpm_access(handle, handle->gpgdir, "pubring.gpg", W_OK)) {
|
|
|
|
QUESTION(handle, ALPM_QUESTION_IMPORT_KEY,
|
|
|
|
&fetch_key, NULL, NULL, &answer);
|
|
|
|
if(answer) {
|
|
|
|
if(key_import(handle, &fetch_key) == 0) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("key \"%s\" could not be imported\n"), fetch_key.uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* keyring directory was not writable, so we don't even try */
|
|
|
|
_alpm_log(handle, ALPM_LOG_WARNING,
|
|
|
|
_("key %s, \"%s\" found on keyserver, keyring is not writable\n"),
|
|
|
|
fetch_key.fingerprint, fetch_key.uid);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("key \"%s\" could not be looked up remotely\n"), fpr);
|
|
|
|
}
|
|
|
|
gpgme_key_unref(fetch_key.data);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:25:44 -04:00
|
|
|
/**
|
|
|
|
* Decode a loaded signature in base64 form.
|
|
|
|
* @param base64_data the signature to attempt to decode
|
|
|
|
* @param data the decoded data; must be freed by the caller
|
|
|
|
* @param data_len the length of the returned data
|
2011-09-22 15:40:51 -04:00
|
|
|
* @return 0 on success, -1 on failure to properly decode
|
2011-04-21 20:25:44 -04:00
|
|
|
*/
|
2012-11-02 09:44:11 -04:00
|
|
|
|
|
|
|
int _alpm_decode_signature(const char *base64_data,
|
2013-01-03 16:48:52 -05:00
|
|
|
unsigned char **data, size_t *data_len)
|
|
|
|
{
|
2011-08-14 21:47:08 -04:00
|
|
|
size_t len = strlen(base64_data);
|
|
|
|
unsigned char *usline = (unsigned char *)base64_data;
|
|
|
|
/* reasonable allocation of expected length is 3/4 of encoded length */
|
|
|
|
size_t destlen = len * 3 / 4;
|
2011-08-14 21:39:43 -04:00
|
|
|
MALLOC(*data, destlen, goto error);
|
2011-08-14 21:47:08 -04:00
|
|
|
if(base64_decode(*data, &destlen, usline, len)) {
|
2011-08-27 17:03:45 -04:00
|
|
|
free(*data);
|
2011-04-21 20:25:44 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
*data_len = destlen;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
*data = NULL;
|
|
|
|
*data_len = 0;
|
2011-09-22 15:40:51 -04:00
|
|
|
return -1;
|
2011-04-21 20:25:44 -04:00
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/**
|
2011-06-14 11:15:43 -04:00
|
|
|
* Check the PGP signature for the given file path.
|
|
|
|
* If base64_sig is provided, it will be used as the signature data after
|
|
|
|
* decoding. If base64_sig is NULL, expect a signature file next to path
|
2011-06-27 17:29:49 -04:00
|
|
|
* (e.g. "%s.sig").
|
|
|
|
*
|
|
|
|
* The return value will be 0 if nothing abnormal happened during the signature
|
|
|
|
* check, and -1 if an error occurred while checking signatures or if a
|
|
|
|
* signature could not be found; pm_errno will be set. Note that "abnormal"
|
2011-09-22 17:29:03 -04:00
|
|
|
* does not include a failed signature; the value in siglist should be checked
|
2011-06-27 17:29:49 -04:00
|
|
|
* to determine if the signature(s) are good.
|
2011-06-07 14:15:43 -04:00
|
|
|
* @param handle the context handle
|
2010-11-22 00:06:09 -05:00
|
|
|
* @param path the full path to a file
|
2011-06-14 11:15:43 -04:00
|
|
|
* @param base64_sig optional PGP signature data in base64 encoding
|
2011-08-24 14:24:42 -04:00
|
|
|
* @param siglist a pointer to storage for signature results
|
2011-06-27 17:29:49 -04:00
|
|
|
* @return 0 in normal cases, -1 if the something failed in the check process
|
2008-12-07 12:58:24 -05:00
|
|
|
*/
|
2011-06-28 00:04:00 -04:00
|
|
|
int _alpm_gpgme_checksig(alpm_handle_t *handle, const char *path,
|
2011-08-24 14:24:42 -04:00
|
|
|
const char *base64_sig, alpm_siglist_t *siglist)
|
2008-12-07 12:58:24 -05:00
|
|
|
{
|
2011-06-27 17:29:49 -04:00
|
|
|
int ret = -1, sigcount;
|
2011-10-03 11:52:16 -04:00
|
|
|
gpgme_error_t err = 0;
|
2008-12-07 12:58:24 -05:00
|
|
|
gpgme_ctx_t ctx;
|
2010-11-22 00:06:09 -05:00
|
|
|
gpgme_data_t filedata, sigdata;
|
2011-06-27 17:29:49 -04:00
|
|
|
gpgme_verify_result_t verify_result;
|
2008-12-07 12:58:24 -05:00
|
|
|
gpgme_signature_t gpgsig;
|
2011-04-21 20:01:06 -04:00
|
|
|
char *sigpath = NULL;
|
2011-04-21 20:25:44 -04:00
|
|
|
unsigned char *decoded_sigdata = NULL;
|
2010-11-22 00:06:09 -05:00
|
|
|
FILE *file = NULL, *sigfile = NULL;
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-07-06 12:26:56 -04:00
|
|
|
if(!path || _alpm_access(handle, NULL, path, R_OK) != 0) {
|
2011-07-01 12:01:39 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_NOT_A_FILE, -1);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
2011-04-21 20:01:06 -04:00
|
|
|
|
2011-08-24 14:24:42 -04:00
|
|
|
if(!siglist) {
|
2011-06-27 17:29:49 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1);
|
|
|
|
}
|
2011-08-24 14:24:42 -04:00
|
|
|
siglist->count = 0;
|
2011-06-27 17:29:49 -04:00
|
|
|
|
2011-04-21 20:25:44 -04:00
|
|
|
if(!base64_sig) {
|
2011-08-15 09:56:58 -04:00
|
|
|
sigpath = _alpm_sigpath(handle, path);
|
2012-01-11 13:04:34 -05:00
|
|
|
if(_alpm_access(handle, NULL, sigpath, R_OK) != 0
|
|
|
|
|| (sigfile = fopen(sigpath, "rb")) == NULL) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "sig path %s could not be opened\n",
|
|
|
|
sigpath);
|
|
|
|
handle->pm_errno = ALPM_ERR_SIG_MISSING;
|
|
|
|
goto error;
|
|
|
|
}
|
2011-04-21 20:01:06 -04:00
|
|
|
}
|
|
|
|
|
2011-10-03 11:52:16 -04:00
|
|
|
/* does the file we are verifying exist? */
|
|
|
|
file = fopen(path, "rb");
|
|
|
|
if(file == NULL) {
|
|
|
|
handle->pm_errno = ALPM_ERR_NOT_A_FILE;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-06-14 11:15:43 -04:00
|
|
|
if(init_gpgme(handle)) {
|
2008-12-07 12:58:24 -05:00
|
|
|
/* pm_errno was set in gpgme_init() */
|
2011-10-03 11:52:16 -04:00
|
|
|
goto error;
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_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
|
|
|
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-21 20:25:44 -04:00
|
|
|
if(base64_sig) {
|
2011-04-21 20:01:06 -04:00
|
|
|
/* memory-based, we loaded it from a sync DB */
|
2011-08-14 21:39:43 -04:00
|
|
|
size_t data_len;
|
2012-11-02 09:44:11 -04:00
|
|
|
int decode_ret = _alpm_decode_signature(base64_sig,
|
2011-04-21 20:25:44 -04:00
|
|
|
&decoded_sigdata, &data_len);
|
|
|
|
if(decode_ret) {
|
2011-06-27 17:29:49 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_SIG_INVALID;
|
2011-10-03 11:52:16 -04:00
|
|
|
goto gpg_error;
|
2011-04-21 20:25:44 -04:00
|
|
|
}
|
|
|
|
err = gpgme_data_new_from_mem(&sigdata,
|
|
|
|
(char *)decoded_sigdata, data_len, 0);
|
2011-04-21 20:01:06 -04:00
|
|
|
} else {
|
|
|
|
/* file-based, it is on disk */
|
|
|
|
err = gpgme_data_new_from_stream(&sigdata, sigfile);
|
|
|
|
}
|
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();
|
2011-06-27 17:29:49 -04:00
|
|
|
verify_result = gpgme_op_verify_result(ctx);
|
|
|
|
CHECK_ERR();
|
|
|
|
if(!verify_result || !verify_result->signatures) {
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "no signatures returned\n");
|
2011-06-27 17:29:49 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_SIG_MISSING;
|
2011-10-03 11:52:16 -04:00
|
|
|
goto gpg_error;
|
2011-06-27 17:29:49 -04:00
|
|
|
}
|
|
|
|
for(gpgsig = verify_result->signatures, sigcount = 0;
|
|
|
|
gpgsig; gpgsig = gpgsig->next, sigcount++);
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "%d signatures returned\n", sigcount);
|
|
|
|
|
2011-08-24 14:24:42 -04:00
|
|
|
CALLOC(siglist->results, sigcount, sizeof(alpm_sigresult_t),
|
2011-10-03 11:52:16 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_MEMORY; goto gpg_error);
|
2011-08-24 14:24:42 -04:00
|
|
|
siglist->count = sigcount;
|
2011-04-22 16:55:54 -04:00
|
|
|
|
2011-06-27 17:29:49 -04:00
|
|
|
for(gpgsig = verify_result->signatures, sigcount = 0; gpgsig;
|
|
|
|
gpgsig = gpgsig->next, sigcount++) {
|
2011-04-22 16:55:54 -04:00
|
|
|
alpm_list_t *summary_list, *summary;
|
2011-06-27 17:29:49 -04:00
|
|
|
alpm_sigstatus_t status;
|
2011-07-22 11:48:13 -04:00
|
|
|
alpm_sigvalidity_t validity;
|
2011-07-01 17:50:32 -04:00
|
|
|
gpgme_key_t key;
|
2011-08-24 14:24:42 -04:00
|
|
|
alpm_sigresult_t *result;
|
2011-04-22 16:55:54 -04:00
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "fingerprint: %s\n", gpgsig->fpr);
|
2011-06-14 11:15:43 -04:00
|
|
|
summary_list = list_sigsum(gpgsig->summary);
|
2011-04-22 16:55:54 -04:00
|
|
|
for(summary = summary_list; summary; summary = summary->next) {
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "summary: %s\n", (const char *)summary->data);
|
2011-04-22 16:55:54 -04:00
|
|
|
}
|
2011-05-04 16:42:50 -04:00
|
|
|
alpm_list_free(summary_list);
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "status: %s\n", gpgme_strerror(gpgsig->status));
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "timestamp: %lu\n", gpgsig->timestamp);
|
2012-11-13 01:49:01 -05:00
|
|
|
|
|
|
|
if((time_t)gpgsig->timestamp > time(NULL)) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG,
|
|
|
|
"signature timestamp is greater than system time.\n");
|
|
|
|
}
|
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "exp_timestamp: %lu\n", gpgsig->exp_timestamp);
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "validity: %s; reason: %s\n",
|
2011-04-29 19:25:44 -04:00
|
|
|
string_validity(gpgsig->validity),
|
2011-04-22 16:55:54 -04:00
|
|
|
gpgme_strerror(gpgsig->validity_reason));
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-08-24 14:24:42 -04:00
|
|
|
result = siglist->results + sigcount;
|
2011-06-27 17:29:49 -04:00
|
|
|
err = gpgme_get_key(ctx, gpgsig->fpr, &key, 0);
|
|
|
|
if(gpg_err_code(err) == GPG_ERR_EOF) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "key lookup failed, unknown key\n");
|
|
|
|
err = GPG_ERR_NO_ERROR;
|
2011-08-24 14:24:42 -04:00
|
|
|
/* we dupe the fpr in this case since we have no key to point at */
|
|
|
|
STRDUP(result->key.fingerprint, gpgsig->fpr,
|
2011-10-03 11:52:16 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_MEMORY; goto gpg_error);
|
2011-06-27 17:29:49 -04:00
|
|
|
} else {
|
|
|
|
CHECK_ERR();
|
|
|
|
if(key->uids) {
|
2011-08-24 14:24:42 -04:00
|
|
|
result->key.data = key;
|
|
|
|
result->key.fingerprint = key->subkeys->fpr;
|
|
|
|
result->key.uid = key->uids->uid;
|
|
|
|
result->key.name = key->uids->name;
|
|
|
|
result->key.email = key->uids->email;
|
|
|
|
result->key.created = key->subkeys->timestamp;
|
|
|
|
result->key.expires = key->subkeys->expires;
|
2011-09-21 16:30:12 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG,
|
|
|
|
"key: %s, %s, owner_trust %s, disabled %d\n",
|
|
|
|
key->subkeys->fpr, key->uids->uid,
|
|
|
|
string_validity(key->owner_trust), key->disabled);
|
2011-06-27 17:29:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 11:48:13 -04:00
|
|
|
switch(gpg_err_code(gpgsig->status)) {
|
|
|
|
/* good cases */
|
|
|
|
case GPG_ERR_NO_ERROR:
|
|
|
|
status = ALPM_SIGSTATUS_VALID;
|
|
|
|
break;
|
|
|
|
case GPG_ERR_KEY_EXPIRED:
|
|
|
|
status = ALPM_SIGSTATUS_KEY_EXPIRED;
|
|
|
|
break;
|
|
|
|
/* bad cases */
|
|
|
|
case GPG_ERR_SIG_EXPIRED:
|
|
|
|
status = ALPM_SIGSTATUS_SIG_EXPIRED;
|
|
|
|
break;
|
|
|
|
case GPG_ERR_NO_PUBKEY:
|
|
|
|
status = ALPM_SIGSTATUS_KEY_UNKNOWN;
|
|
|
|
break;
|
|
|
|
case GPG_ERR_BAD_SIGNATURE:
|
|
|
|
default:
|
|
|
|
status = ALPM_SIGSTATUS_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-21 16:30:12 -04:00
|
|
|
/* special case: key disabled is not returned in above status code */
|
|
|
|
if(result->key.data && key->disabled) {
|
|
|
|
status = ALPM_SIGSTATUS_KEY_DISABLED;
|
|
|
|
}
|
2011-07-22 11:48:13 -04:00
|
|
|
|
2011-08-25 18:41:47 -04:00
|
|
|
switch(gpgsig->validity) {
|
|
|
|
case GPGME_VALIDITY_ULTIMATE:
|
|
|
|
case GPGME_VALIDITY_FULL:
|
|
|
|
validity = ALPM_SIGVALIDITY_FULL;
|
|
|
|
break;
|
|
|
|
case GPGME_VALIDITY_MARGINAL:
|
|
|
|
validity = ALPM_SIGVALIDITY_MARGINAL;
|
|
|
|
break;
|
|
|
|
case GPGME_VALIDITY_NEVER:
|
|
|
|
validity = ALPM_SIGVALIDITY_NEVER;
|
|
|
|
break;
|
|
|
|
case GPGME_VALIDITY_UNKNOWN:
|
|
|
|
case GPGME_VALIDITY_UNDEFINED:
|
|
|
|
default:
|
|
|
|
validity = ALPM_SIGVALIDITY_UNKNOWN;
|
|
|
|
break;
|
2011-04-29 19:25:44 -04:00
|
|
|
}
|
|
|
|
|
2011-08-24 14:24:42 -04:00
|
|
|
result->status = status;
|
|
|
|
result->validity = validity;
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
|
2011-06-27 17:29:49 -04:00
|
|
|
ret = 0;
|
|
|
|
|
2011-10-03 11:52:16 -04:00
|
|
|
gpg_error:
|
2008-12-07 12:58:24 -05:00
|
|
|
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);
|
2011-10-03 11:52:16 -04:00
|
|
|
|
|
|
|
error:
|
2008-12-07 12:58:24 -05:00
|
|
|
if(sigfile) {
|
|
|
|
fclose(sigfile);
|
|
|
|
}
|
2010-11-22 00:06:09 -05:00
|
|
|
if(file) {
|
|
|
|
fclose(file);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
2011-04-21 20:01:06 -04:00
|
|
|
FREE(sigpath);
|
2011-04-21 20:25:44 -04:00
|
|
|
FREE(decoded_sigdata);
|
2011-07-01 16:51:21 -04:00
|
|
|
if(gpg_err_code(err) != GPG_ERR_NO_ERROR) {
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR, _("GPGME error: %s\n"), gpgme_strerror(err));
|
2011-07-01 12:01:39 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_GPGME, -1);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-09-27 10:15:36 -04:00
|
|
|
|
|
|
|
#else /* HAVE_LIBGPGME */
|
2012-10-29 06:51:04 -04:00
|
|
|
int _alpm_key_in_keychain(alpm_handle_t UNUSED *handle, const char UNUSED *fpr)
|
2011-09-27 10:15:36 -04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-29 06:51:04 -04:00
|
|
|
|
2012-10-29 08:24:55 -04:00
|
|
|
int _alpm_key_import(alpm_handle_t UNUSED *handle, const char UNUSED *fpr)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-16 11:48:47 -04:00
|
|
|
int _alpm_gpgme_checksig(alpm_handle_t UNUSED *handle, const char UNUSED *path,
|
2011-09-18 16:34:28 -04:00
|
|
|
const char UNUSED *base64_sig, alpm_siglist_t UNUSED *siglist)
|
2011-04-24 12:40:58 -04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-27 10:15:36 -04:00
|
|
|
#endif /* HAVE_LIBGPGME */
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-08-16 20:51:21 -04:00
|
|
|
/**
|
|
|
|
* Form a signature path given a file path.
|
|
|
|
* Caller must free the result.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param path the full path to a file
|
|
|
|
* @return the path with '.sig' appended, NULL on errors
|
|
|
|
*/
|
|
|
|
char *_alpm_sigpath(alpm_handle_t *handle, const char *path)
|
|
|
|
{
|
|
|
|
char *sigpath;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if(!path) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
len = strlen(path) + 5;
|
|
|
|
CALLOC(sigpath, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
|
|
|
|
sprintf(sigpath, "%s.sig", path);
|
|
|
|
return sigpath;
|
|
|
|
}
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Helper for checking the PGP signature for the given file path.
|
|
|
|
* This wraps #_alpm_gpgme_checksig in a slightly friendlier manner to simplify
|
|
|
|
* handling of optional signatures and marginal/unknown trust levels and
|
|
|
|
* handling the correct error code return values.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param path the full path to a file
|
|
|
|
* @param base64_sig optional PGP signature data in base64 encoding
|
|
|
|
* @param optional whether signatures are optional (e.g., missing OK)
|
|
|
|
* @param marginal whether signatures with marginal trust are acceptable
|
|
|
|
* @param unknown whether signatures with unknown trust are acceptable
|
|
|
|
* @param sigdata a pointer to storage for signature results
|
|
|
|
* @return 0 on success, -1 on error (consult pm_errno or sigdata)
|
|
|
|
*/
|
2011-06-27 17:29:49 -04:00
|
|
|
int _alpm_check_pgp_helper(alpm_handle_t *handle, const char *path,
|
2011-09-19 22:51:30 -04:00
|
|
|
const char *base64_sig, int optional, int marginal, int unknown,
|
|
|
|
alpm_siglist_t **sigdata)
|
2011-06-27 17:29:49 -04:00
|
|
|
{
|
2011-09-19 22:51:30 -04:00
|
|
|
alpm_siglist_t *siglist;
|
2011-06-27 17:29:49 -04:00
|
|
|
int ret;
|
|
|
|
|
2011-09-19 22:51:30 -04:00
|
|
|
CALLOC(siglist, 1, sizeof(alpm_siglist_t),
|
|
|
|
RET_ERR(handle, ALPM_ERR_MEMORY, -1));
|
2011-06-27 17:29:49 -04:00
|
|
|
|
2011-09-19 22:51:30 -04:00
|
|
|
ret = _alpm_gpgme_checksig(handle, path, base64_sig, siglist);
|
2011-06-27 17:29:49 -04:00
|
|
|
if(ret && handle->pm_errno == ALPM_ERR_SIG_MISSING) {
|
|
|
|
if(optional) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "missing optional signature\n");
|
|
|
|
handle->pm_errno = 0;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "missing required signature\n");
|
|
|
|
/* ret will already be -1 */
|
|
|
|
}
|
|
|
|
} else if(ret) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature check failed\n");
|
|
|
|
/* ret will already be -1 */
|
|
|
|
} else {
|
2011-08-24 14:24:42 -04:00
|
|
|
size_t num;
|
2011-09-19 22:51:30 -04:00
|
|
|
for(num = 0; !ret && num < siglist->count; num++) {
|
|
|
|
switch(siglist->results[num].status) {
|
2011-06-27 17:29:49 -04:00
|
|
|
case ALPM_SIGSTATUS_VALID:
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_EXPIRED:
|
2011-06-27 17:29:49 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is valid\n");
|
2011-09-19 22:51:30 -04:00
|
|
|
switch(siglist->results[num].validity) {
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGVALIDITY_FULL:
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is fully trusted\n");
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_MARGINAL:
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is marginal trust\n");
|
|
|
|
if(!marginal) {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_UNKNOWN:
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is unknown trust\n");
|
|
|
|
if(!unknown) {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_NEVER:
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature should never be trusted\n");
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2011-06-27 17:29:49 -04:00
|
|
|
}
|
2011-07-22 11:48:13 -04:00
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_SIG_EXPIRED:
|
|
|
|
case ALPM_SIGSTATUS_KEY_UNKNOWN:
|
2011-09-21 16:30:12 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_DISABLED:
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_INVALID:
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "signature is not valid\n");
|
2011-06-27 17:29:49 -04:00
|
|
|
ret = -1;
|
2011-07-22 11:48:13 -04:00
|
|
|
break;
|
2011-06-27 17:29:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-19 22:51:30 -04:00
|
|
|
if(sigdata) {
|
|
|
|
*sigdata = siglist;
|
|
|
|
} else {
|
|
|
|
alpm_siglist_cleanup(siglist);
|
|
|
|
free(siglist);
|
|
|
|
}
|
|
|
|
|
2011-06-27 17:29:49 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Examine a signature result list and take any appropriate or necessary
|
|
|
|
* actions. This may include asking the user to import a key or simply printing
|
|
|
|
* helpful failure messages so the user can take action out of band.
|
|
|
|
* @param handle the context handle
|
|
|
|
* @param identifier a friendly name for the signed resource; usually a
|
|
|
|
* database or package name
|
|
|
|
* @param siglist a pointer to storage for signature results
|
|
|
|
* @param optional whether signatures are optional (e.g., missing OK)
|
|
|
|
* @param marginal whether signatures with marginal trust are acceptable
|
|
|
|
* @param unknown whether signatures with unknown trust are acceptable
|
|
|
|
* @return 0 if all signatures are OK, -1 on errors, 1 if we should retry the
|
|
|
|
* validation process
|
|
|
|
*/
|
2011-09-19 23:53:15 -04:00
|
|
|
int _alpm_process_siglist(alpm_handle_t *handle, const char *identifier,
|
|
|
|
alpm_siglist_t *siglist, int optional, int marginal, int unknown)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int retry = 0;
|
|
|
|
|
|
|
|
if(!optional && siglist->count == 0) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: missing required signature\n"), identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < siglist->count; i++) {
|
|
|
|
alpm_sigresult_t *result = siglist->results + i;
|
|
|
|
const char *name = result->key.uid ? result->key.uid : result->key.fingerprint;
|
|
|
|
switch(result->status) {
|
|
|
|
case ALPM_SIGSTATUS_VALID:
|
|
|
|
case ALPM_SIGSTATUS_KEY_EXPIRED:
|
|
|
|
switch(result->validity) {
|
|
|
|
case ALPM_SIGVALIDITY_FULL:
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_MARGINAL:
|
|
|
|
if(!marginal) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature from \"%s\" is marginal trust\n"),
|
|
|
|
identifier, name);
|
2011-09-20 17:54:08 -04:00
|
|
|
/* QUESTION(handle, ALPM_QUESTION_EDIT_KEY_TRUST, &result->key, NULL, NULL, &answer); */
|
2011-09-19 23:53:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_UNKNOWN:
|
|
|
|
if(!unknown) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature from \"%s\" is unknown trust\n"),
|
|
|
|
identifier, name);
|
2011-09-20 17:54:08 -04:00
|
|
|
/* QUESTION(handle, ALPM_QUESTION_EDIT_KEY_TRUST, &result->key, NULL, NULL, &answer); */
|
2011-09-19 23:53:15 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_NEVER:
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature from \"%s\" should never be trusted\n"),
|
|
|
|
identifier, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_KEY_UNKNOWN:
|
2011-09-20 17:54:08 -04:00
|
|
|
/* ensure this key is still actually unknown; we may have imported it
|
|
|
|
* on an earlier call to this function. */
|
2012-10-29 06:51:04 -04:00
|
|
|
if(_alpm_key_in_keychain(handle, result->key.fingerprint) == 1) {
|
2011-09-20 17:54:08 -04:00
|
|
|
break;
|
|
|
|
}
|
2011-09-19 23:53:15 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
2011-09-20 17:54:08 -04:00
|
|
|
_("%s: key \"%s\" is unknown\n"), identifier, name);
|
2012-10-29 08:24:55 -04:00
|
|
|
|
|
|
|
if(_alpm_key_import(handle, result->key.fingerprint) == 0) {
|
|
|
|
retry = 1;
|
2011-09-20 17:54:08 -04:00
|
|
|
}
|
2012-10-29 08:24:55 -04:00
|
|
|
|
2011-09-19 23:53:15 -04:00
|
|
|
break;
|
2011-09-21 16:30:12 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_DISABLED:
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: key \"%s\" is disabled\n"), identifier, name);
|
|
|
|
break;
|
2011-09-19 23:53:15 -04:00
|
|
|
case ALPM_SIGSTATUS_SIG_EXPIRED:
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
2011-09-20 17:54:08 -04:00
|
|
|
_("%s: signature from \"%s\" is expired\n"), identifier, name);
|
2011-09-19 23:53:15 -04:00
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_INVALID:
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature from \"%s\" is invalid\n"),
|
|
|
|
identifier, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retry;
|
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/**
|
2011-04-21 20:01:06 -04:00
|
|
|
* Check the PGP signature for the given package file.
|
2008-12-07 12:58:24 -05:00
|
|
|
* @param pkg the package to check
|
2011-09-22 12:02:58 -04:00
|
|
|
* @param siglist a pointer to storage for signature results
|
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
|
|
|
*/
|
2011-06-27 17:29:49 -04:00
|
|
|
int SYMEXPORT alpm_pkg_check_pgp_signature(alpm_pkg_t *pkg,
|
2011-08-24 14:24:42 -04:00
|
|
|
alpm_siglist_t *siglist)
|
2008-12-07 12:58:24 -05:00
|
|
|
{
|
2011-06-14 11:15:43 -04:00
|
|
|
ASSERT(pkg != NULL, return -1);
|
2011-08-24 14:24:42 -04:00
|
|
|
ASSERT(siglist != NULL, RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
|
2011-06-14 11:01:08 -04:00
|
|
|
pkg->handle->pm_errno = 0;
|
2008-12-07 12:58:24 -05:00
|
|
|
|
2011-09-02 23:07:06 -04:00
|
|
|
return _alpm_gpgme_checksig(pkg->handle, pkg->filename,
|
2011-08-24 14:24:42 -04:00
|
|
|
pkg->base64_sig, siglist);
|
2008-12-07 12:58:24 -05:00
|
|
|
}
|
|
|
|
|
2010-11-22 00:06:09 -05:00
|
|
|
/**
|
2011-04-21 20:01:06 -04:00
|
|
|
* Check the PGP signature for the given database.
|
2010-11-22 00:06:09 -05:00
|
|
|
* @param db the database to check
|
2011-09-22 12:02:58 -04:00
|
|
|
* @param siglist a pointer to storage for signature results
|
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
|
|
|
*/
|
2011-06-27 17:29:49 -04:00
|
|
|
int SYMEXPORT alpm_db_check_pgp_signature(alpm_db_t *db,
|
2011-08-24 14:24:42 -04:00
|
|
|
alpm_siglist_t *siglist)
|
2010-11-22 00:06:09 -05:00
|
|
|
{
|
2011-06-14 11:15:43 -04:00
|
|
|
ASSERT(db != NULL, return -1);
|
2011-08-24 14:24:42 -04:00
|
|
|
ASSERT(siglist != NULL, RET_ERR(db->handle, ALPM_ERR_WRONG_ARGS, -1));
|
2011-06-14 11:01:08 -04:00
|
|
|
db->handle->pm_errno = 0;
|
2010-11-22 00:06:09 -05:00
|
|
|
|
2011-08-24 14:24:42 -04:00
|
|
|
return _alpm_gpgme_checksig(db->handle, _alpm_db_path(db), NULL, siglist);
|
2010-11-22 00:06:09 -05:00
|
|
|
}
|
|
|
|
|
2011-09-22 12:02:58 -04:00
|
|
|
/**
|
|
|
|
* Clean up and free a signature result list.
|
2011-09-22 17:29:03 -04:00
|
|
|
* Note that this does not free the siglist object itself in case that
|
2011-09-22 12:02:58 -04:00
|
|
|
* was allocated on the stack; this is the responsibility of the caller.
|
|
|
|
* @param siglist a pointer to storage for signature results
|
|
|
|
* @return 0 on success, -1 on error
|
|
|
|
*/
|
2011-08-24 14:24:42 -04:00
|
|
|
int SYMEXPORT alpm_siglist_cleanup(alpm_siglist_t *siglist)
|
2011-07-01 17:50:32 -04:00
|
|
|
{
|
2011-08-24 14:24:42 -04:00
|
|
|
ASSERT(siglist != NULL, return -1);
|
|
|
|
size_t num;
|
|
|
|
for(num = 0; num < siglist->count; num++) {
|
|
|
|
alpm_sigresult_t *result = siglist->results + num;
|
|
|
|
if(result->key.data) {
|
2011-09-18 16:34:28 -04:00
|
|
|
#if HAVE_LIBGPGME
|
2011-08-24 14:24:42 -04:00
|
|
|
gpgme_key_unref(result->key.data);
|
2011-09-18 16:34:28 -04:00
|
|
|
#endif
|
2011-08-24 14:24:42 -04:00
|
|
|
} else {
|
|
|
|
free(result->key.fingerprint);
|
2011-07-01 17:50:32 -04:00
|
|
|
}
|
|
|
|
}
|
2011-09-21 20:13:43 -04:00
|
|
|
if(siglist->count) {
|
|
|
|
free(siglist->results);
|
|
|
|
}
|
|
|
|
siglist->results = NULL;
|
2011-08-24 14:24:42 -04:00
|
|
|
siglist->count = 0;
|
2011-07-01 17:50:32 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-02 09:06:45 -04:00
|
|
|
/**
|
|
|
|
* Extract the Issuer Key ID from a signature
|
|
|
|
* @param sig PGP signature
|
|
|
|
* @param len length of signature
|
|
|
|
* @param keys a pointer to storage for key IDs
|
|
|
|
* @return 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int _alpm_extract_keyid(alpm_handle_t *handle, const char *identifier,
|
|
|
|
const unsigned char *sig, const size_t len, alpm_list_t **keys)
|
|
|
|
{
|
|
|
|
size_t pos, spos, blen, hlen, ulen, slen;
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
while(pos < len) {
|
|
|
|
if(!(sig[pos] & 0x80)) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature format error"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sig[pos] & 0x40) {
|
|
|
|
/* "new" packet format is not supported */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: unsupported signature format"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(((sig[pos] & 0x3f) >> 2) != 2) {
|
|
|
|
/* signature is not a "Signature Packet" */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature format error"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(sig[pos] & 0x03) {
|
|
|
|
case 0:
|
|
|
|
blen = sig[pos + 1];
|
|
|
|
pos = pos + 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
blen = (sig[pos + 1] << 8) | sig[pos + 2];
|
|
|
|
pos = pos + 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
blen = (sig[pos + 1] << 24) | (sig[pos + 2] << 16) | (sig[pos + 3] << 8) | sig[pos + 4];
|
|
|
|
pos = pos + 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
/* partial body length not supported */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: unsupported signature format"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sig[pos] != 4) {
|
|
|
|
/* only support version 4 signature packet format */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: unsupported signature format"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sig[pos + 1] != 0x00) {
|
|
|
|
/* not a signature of a binary document */
|
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
|
|
|
_("%s: signature format error"), identifier);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos + 4;
|
|
|
|
|
|
|
|
hlen = (sig[pos] << 8) | sig[pos + 1];
|
|
|
|
pos = pos + hlen + 2;
|
|
|
|
|
|
|
|
ulen = (sig[pos] << 8) | sig[pos + 1];
|
|
|
|
pos = pos + 2;
|
|
|
|
|
|
|
|
spos = pos;
|
|
|
|
|
|
|
|
while(spos < pos + ulen) {
|
|
|
|
if(sig[spos] < 192) {
|
|
|
|
slen = sig[spos];
|
|
|
|
spos = spos + 1;
|
|
|
|
} else if(sig[spos] < 255) {
|
|
|
|
slen = (sig[spos] << 8) | sig[spos + 1];
|
|
|
|
spos = spos + 2;
|
|
|
|
} else {
|
|
|
|
slen = (sig[spos + 1] << 24) | (sig[spos + 2] << 16) | (sig[spos + 3] << 8) | sig[spos + 4];
|
|
|
|
spos = spos + 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sig[spos] == 16) {
|
|
|
|
/* issuer key ID */
|
|
|
|
char key[17];
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
sprintf(&key[i * 2], "%02X", sig[spos + i + 1]);
|
|
|
|
}
|
|
|
|
*keys = alpm_list_add(*keys, strdup(key));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
spos = spos + slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = pos + (blen - hlen - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-07 12:58:24 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|