2013-12-24 15:13:55 +05:30
|
|
|
/*-
|
2020-04-24 15:14:19 +05:30
|
|
|
* Licensed under the SPDX BSD-2-Clause identifier.
|
|
|
|
* Use is subject to license terms, as specified in the LICENSE file.
|
2013-12-24 15:13:55 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2014-09-27 16:30:34 +05:30
|
|
|
#include <sys/mman.h>
|
2013-12-24 15:13:55 +05:30
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
|
|
|
#include "xbps_api_impl.h"
|
|
|
|
|
|
|
|
static bool
|
2016-06-16 02:52:44 +05:30
|
|
|
rsa_verify_hash(struct xbps_repo *repo, xbps_data_t pubkey,
|
2013-12-24 15:13:55 +05:30
|
|
|
unsigned char *sig, unsigned int siglen,
|
2016-06-16 02:52:44 +05:30
|
|
|
unsigned char *sha256)
|
2013-12-24 15:13:55 +05:30
|
|
|
{
|
|
|
|
BIO *bio;
|
|
|
|
RSA *rsa;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
|
|
|
|
bio = BIO_new_mem_buf(__UNCONST(xbps_data_data_nocopy(pubkey)),
|
|
|
|
xbps_data_size(pubkey));
|
|
|
|
assert(bio);
|
|
|
|
|
|
|
|
rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
|
|
|
|
if (rsa == NULL) {
|
|
|
|
xbps_dbg_printf(repo->xhp, "`%s' error reading public key: %s\n",
|
|
|
|
repo->uri, ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:52:44 +05:30
|
|
|
rv = RSA_verify(NID_sha1, sha256, SHA256_DIGEST_LENGTH, sig, siglen, rsa);
|
2013-12-24 15:13:55 +05:30
|
|
|
RSA_free(rsa);
|
|
|
|
BIO_free(bio);
|
|
|
|
ERR_free_strings();
|
|
|
|
|
|
|
|
return rv ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-01-06 19:03:49 +05:30
|
|
|
xbps_verify_signature(struct xbps_repo *repo, const char *sigfile,
|
|
|
|
unsigned char *digest)
|
2013-12-24 15:13:55 +05:30
|
|
|
{
|
|
|
|
xbps_dictionary_t repokeyd = NULL;
|
|
|
|
xbps_data_t pubkey;
|
2020-01-06 19:03:49 +05:30
|
|
|
char *hexfp = NULL;
|
|
|
|
unsigned char *sig_buf = NULL;
|
|
|
|
size_t sigbuflen, sigfilelen;
|
|
|
|
char *rkeyfile = NULL;
|
2013-12-24 15:13:55 +05:30
|
|
|
bool val = false;
|
|
|
|
|
2020-01-06 19:03:49 +05:30
|
|
|
if (!xbps_dictionary_count(repo->idxmeta)) {
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_dbg_printf(repo->xhp, "%s: unsigned repository\n", repo->uri);
|
2013-12-24 15:13:55 +05:30
|
|
|
return false;
|
2014-01-30 17:37:34 +05:30
|
|
|
}
|
2014-01-31 00:04:26 +05:30
|
|
|
hexfp = xbps_pubkey2fp(repo->xhp,
|
2020-01-06 19:03:49 +05:30
|
|
|
xbps_dictionary_get(repo->idxmeta, "public-key"));
|
2014-01-30 17:37:34 +05:30
|
|
|
if (hexfp == NULL) {
|
|
|
|
xbps_dbg_printf(repo->xhp, "%s: incomplete signed repo, missing hexfp obj\n", repo->uri);
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-06 19:03:49 +05:30
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Prepare repository RSA public key to verify fname signature.
|
|
|
|
*/
|
2020-01-06 19:52:36 +05:30
|
|
|
rkeyfile = xbps_xasprintf("%s/keys/%s.plist", repo->xhp->metadir, hexfp);
|
2015-05-28 13:45:05 +05:30
|
|
|
repokeyd = xbps_plist_dictionary_from_file(repo->xhp, rkeyfile);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (xbps_object_type(repokeyd) != XBPS_TYPE_DICTIONARY) {
|
|
|
|
xbps_dbg_printf(repo->xhp, "cannot read rkey data at %s: %s\n",
|
|
|
|
rkeyfile, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey = xbps_dictionary_get(repokeyd, "public-key");
|
|
|
|
if (xbps_object_type(pubkey) != XBPS_TYPE_DATA)
|
|
|
|
goto out;
|
2020-01-06 19:03:49 +05:30
|
|
|
|
|
|
|
if (!xbps_mmap_file(sigfile, (void *)&sig_buf, &sigbuflen, &sigfilelen)) {
|
|
|
|
xbps_dbg_printf(repo->xhp, "can't open signature file %s: %s\n",
|
|
|
|
sigfile, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
/*
|
|
|
|
* Verify fname RSA signature.
|
|
|
|
*/
|
2016-06-16 02:52:44 +05:30
|
|
|
if (rsa_verify_hash(repo, pubkey, sig_buf, sigfilelen, digest))
|
2013-12-24 15:13:55 +05:30
|
|
|
val = true;
|
|
|
|
|
|
|
|
out:
|
2014-10-05 10:57:13 +05:30
|
|
|
if (hexfp)
|
|
|
|
free(hexfp);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (rkeyfile)
|
|
|
|
free(rkeyfile);
|
2020-01-06 19:03:49 +05:30
|
|
|
if (sig_buf)
|
|
|
|
(void)munmap(sig_buf, sigbuflen);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (repokeyd)
|
|
|
|
xbps_object_release(repokeyd);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2019-07-07 17:39:49 +05:30
|
|
|
|
|
|
|
bool
|
|
|
|
xbps_verify_file_signature(struct xbps_repo *repo, const char *fname)
|
|
|
|
{
|
|
|
|
char sig[PATH_MAX];
|
2020-02-10 06:24:52 +05:30
|
|
|
unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
|
2019-07-07 17:39:49 +05:30
|
|
|
bool val = false;
|
|
|
|
|
2020-02-10 06:24:52 +05:30
|
|
|
if (!xbps_file_sha256_raw(digest, sizeof digest, fname)) {
|
2019-07-07 17:39:49 +05:30
|
|
|
xbps_dbg_printf(repo->xhp, "can't open file %s: %s\n", fname, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(sig, sizeof sig, "%s.sig", fname);
|
|
|
|
val = xbps_verify_signature(repo, sig, digest);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|