2013-10-05 15:08:04 +05:30
|
|
|
/*-
|
2015-01-11 13:41:38 +05:30
|
|
|
* Copyright (c) 2013-2015 Juan Romero Pardines.
|
2013-10-05 15:08:04 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
static RSA *
|
|
|
|
load_rsa_privkey(const char *path)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
RSA *rsa = NULL;
|
2013-10-12 13:25:03 +05:30
|
|
|
const char *p;
|
|
|
|
char *passphrase = NULL;
|
2013-10-05 15:08:04 +05:30
|
|
|
|
|
|
|
if ((fp = fopen(path, "r")) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((rsa = RSA_new()) == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-12 13:25:03 +05:30
|
|
|
p = getenv("XBPS_PASSPHRASE");
|
|
|
|
if (p) {
|
|
|
|
passphrase = strdup(p);
|
|
|
|
}
|
|
|
|
rsa = PEM_read_RSAPrivateKey(fp, 0, NULL, passphrase);
|
|
|
|
if (passphrase) {
|
|
|
|
free(passphrase);
|
|
|
|
passphrase = NULL;
|
|
|
|
}
|
2013-10-05 15:08:04 +05:30
|
|
|
fclose(fp);
|
|
|
|
return rsa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
pubkey_from_privkey(RSA *rsa)
|
|
|
|
{
|
|
|
|
BIO *bp;
|
2014-03-31 15:30:08 +05:30
|
|
|
char *buf = NULL;
|
|
|
|
int len;
|
2013-10-05 15:08:04 +05:30
|
|
|
|
|
|
|
bp = BIO_new(BIO_s_mem());
|
|
|
|
assert(bp);
|
|
|
|
|
|
|
|
if (!PEM_write_bio_RSA_PUBKEY(bp, rsa)) {
|
|
|
|
fprintf(stderr, "error writing public key: %s\n",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
BIO_free(bp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* XXX (xtraeme) 8192 should be always enough? */
|
|
|
|
buf = malloc(8192);
|
|
|
|
assert(buf);
|
2014-03-31 15:30:08 +05:30
|
|
|
len = BIO_read(bp, buf, 8191);
|
2013-10-05 15:08:04 +05:30
|
|
|
BIO_free(bp);
|
|
|
|
ERR_free_strings();
|
2014-03-31 15:30:08 +05:30
|
|
|
buf[len] = '\0';
|
2013-10-05 15:08:04 +05:30
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
static bool
|
|
|
|
rsa_sign_buf(RSA *rsa, const char *buf, unsigned int buflen,
|
2013-10-05 15:08:04 +05:30
|
|
|
unsigned char **sigret, unsigned int *siglen)
|
|
|
|
{
|
|
|
|
SHA256_CTX context;
|
|
|
|
unsigned char sha256[SHA256_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
SHA256_Init(&context);
|
2013-12-24 15:13:55 +05:30
|
|
|
SHA256_Update(&context, buf, buflen);
|
2013-10-05 15:08:04 +05:30
|
|
|
SHA256_Final(sha256, &context);
|
|
|
|
|
|
|
|
*sigret = calloc(1, RSA_size(rsa) + 1);
|
2013-12-24 15:13:55 +05:30
|
|
|
if (!RSA_sign(NID_sha1, sha256, sizeof(sha256),
|
|
|
|
*sigret, siglen, rsa)) {
|
|
|
|
free(*sigret);
|
|
|
|
return false;
|
2013-10-05 15:08:04 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
return true;
|
2013-10-05 15:08:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sign_repo(struct xbps_handle *xhp, const char *repodir,
|
|
|
|
const char *privkey, const char *signedby)
|
|
|
|
{
|
2013-12-24 15:13:55 +05:30
|
|
|
struct stat st;
|
2015-03-25 16:30:59 +05:30
|
|
|
struct xbps_repo *repo = NULL;
|
2014-01-21 15:36:02 +05:30
|
|
|
xbps_dictionary_t pkgd, meta = NULL;
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_data_t data = NULL, rpubkey = NULL;
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_object_iterator_t iter = NULL;
|
|
|
|
xbps_object_t obj;
|
|
|
|
RSA *rsa = NULL;
|
2013-10-05 15:08:04 +05:30
|
|
|
unsigned char *sig;
|
2013-12-24 15:13:55 +05:30
|
|
|
unsigned int siglen;
|
2014-01-30 17:37:34 +05:30
|
|
|
uint16_t rpubkeysize, pubkeysize;
|
2014-01-31 00:03:14 +05:30
|
|
|
const char *arch, *pkgver, *rsignedby = NULL;
|
2015-03-25 16:30:59 +05:30
|
|
|
char *binpkg = NULL, *binpkg_sig = NULL, *buf = NULL;
|
|
|
|
char *rlockfname = NULL, *defprivkey = NULL;
|
|
|
|
int binpkg_fd, binpkg_sig_fd, rlockfd = -1, rv = 0;
|
2013-12-24 15:13:55 +05:30
|
|
|
bool flush = false;
|
2013-10-05 15:08:04 +05:30
|
|
|
|
|
|
|
if (signedby == NULL) {
|
|
|
|
fprintf(stderr, "--signedby unset! cannot sign repository\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-31 16:05:31 +05:30
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
/*
|
|
|
|
* Check that repository index exists and not empty, otherwise bail out.
|
|
|
|
*/
|
2015-03-25 16:30:59 +05:30
|
|
|
if (!xbps_repo_lock(xhp, repodir, &rlockfd, &rlockfname)) {
|
|
|
|
rv = errno;
|
|
|
|
fprintf(stderr, "%s: cannot lock repository: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
repo = xbps_repo_open(xhp, repodir);
|
2013-10-05 15:08:04 +05:30
|
|
|
if (repo == NULL) {
|
2014-01-31 16:05:31 +05:30
|
|
|
rv = errno;
|
|
|
|
fprintf(stderr, "%s: cannot read repository data: %s\n",
|
|
|
|
_XBPS_RINDEX, strerror(errno));
|
|
|
|
goto out;
|
2013-10-05 15:08:04 +05:30
|
|
|
}
|
2013-10-07 13:53:14 +05:30
|
|
|
if (xbps_dictionary_count(repo->idx) == 0) {
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "%s: invalid repository, existing!\n", _XBPS_RINDEX);
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-10-05 15:08:04 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If privkey not set, default to ~/.ssh/id_rsa.
|
|
|
|
*/
|
|
|
|
if (privkey == NULL)
|
|
|
|
defprivkey = xbps_xasprintf("%s/.ssh/id_rsa", getenv("HOME"));
|
|
|
|
else
|
|
|
|
defprivkey = strdup(privkey);
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
ERR_load_crypto_strings();
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
OpenSSL_add_all_ciphers();
|
|
|
|
OpenSSL_add_all_digests();
|
|
|
|
|
|
|
|
if ((rsa = load_rsa_privkey(defprivkey)) == NULL) {
|
2014-01-31 16:05:31 +05:30
|
|
|
fprintf(stderr, "%s: failed to read the RSA privkey\n", _XBPS_RINDEX);
|
2014-01-21 15:36:02 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2013-11-30 12:34:37 +05:30
|
|
|
}
|
2013-11-30 13:00:25 +05:30
|
|
|
/*
|
2013-12-24 15:13:55 +05:30
|
|
|
* Iterate over the idx dictionary and then sign all binary
|
|
|
|
* packages in this repository.
|
2013-11-30 13:00:25 +05:30
|
|
|
*/
|
2014-01-21 15:36:02 +05:30
|
|
|
iter = xbps_dictionary_iterator(repo->idx);
|
2014-10-05 16:50:18 +05:30
|
|
|
assert(iter);
|
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
2014-01-21 15:36:02 +05:30
|
|
|
pkgd = xbps_dictionary_get_keysym(repo->idx, obj);
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
2013-10-05 15:08:04 +05:30
|
|
|
|
2013-12-24 15:13:55 +05:30
|
|
|
binpkg = xbps_xasprintf("%s/%s.%s.xbps", repodir, pkgver, arch);
|
|
|
|
binpkg_sig = xbps_xasprintf("%s.sig", binpkg);
|
|
|
|
/*
|
|
|
|
* Skip pkg if file signature exists
|
|
|
|
*/
|
2013-12-26 12:29:11 +05:30
|
|
|
if ((binpkg_sig_fd = access(binpkg_sig, R_OK)) == 0) {
|
2014-01-21 15:36:02 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_VERBOSE)
|
|
|
|
fprintf(stderr, "skipping %s, file signature found.\n", pkgver);
|
2013-12-24 15:13:55 +05:30
|
|
|
free(binpkg);
|
|
|
|
free(binpkg_sig);
|
|
|
|
close(binpkg_sig_fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Generate pkg file signature.
|
|
|
|
*/
|
|
|
|
if ((binpkg_fd = open(binpkg, O_RDONLY)) == -1) {
|
|
|
|
fprintf(stderr, "cannot read %s: %s\n", binpkg, strerror(errno));
|
|
|
|
free(binpkg);
|
|
|
|
free(binpkg_sig);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fstat(binpkg_fd, &st);
|
|
|
|
buf = malloc(st.st_size);
|
|
|
|
assert(buf);
|
|
|
|
if (read(binpkg_fd, buf, st.st_size) != st.st_size) {
|
|
|
|
fprintf(stderr, "failed to read %s: %s\n", binpkg, strerror(errno));
|
|
|
|
close(binpkg_fd);
|
|
|
|
free(buf);
|
|
|
|
free(binpkg);
|
|
|
|
free(binpkg_sig);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
close(binpkg_fd);
|
|
|
|
if (!rsa_sign_buf(rsa, buf, st.st_size, &sig, &siglen)) {
|
|
|
|
fprintf(stderr, "failed to sign %s: %s\n", binpkg, strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
free(binpkg);
|
|
|
|
free(binpkg_sig);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
free(binpkg);
|
|
|
|
/*
|
|
|
|
* Write pkg file signature.
|
|
|
|
*/
|
|
|
|
binpkg_sig_fd = creat(binpkg_sig, 0644);
|
|
|
|
if (binpkg_sig_fd == -1) {
|
|
|
|
fprintf(stderr, "failed to create %s: %s\n", binpkg_sig, strerror(errno));
|
2014-04-20 13:32:54 +05:30
|
|
|
free(sig);
|
2013-12-24 15:13:55 +05:30
|
|
|
free(binpkg_sig);
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-24 15:28:55 +05:30
|
|
|
if (write(binpkg_sig_fd, sig, siglen) != (ssize_t)siglen) {
|
2013-12-24 15:13:55 +05:30
|
|
|
fprintf(stderr, "failed to write %s: %s\n", binpkg_sig, strerror(errno));
|
|
|
|
free(sig);
|
|
|
|
free(binpkg_sig);
|
|
|
|
close(binpkg_sig_fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(sig);
|
|
|
|
free(binpkg_sig);
|
|
|
|
close(binpkg_sig_fd);
|
2014-01-21 15:36:02 +05:30
|
|
|
printf("signed successfully %s\n", pkgver);
|
2013-10-07 13:53:14 +05:30
|
|
|
}
|
2013-12-24 15:13:55 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2014-01-21 15:36:02 +05:30
|
|
|
/*
|
2014-01-30 17:37:34 +05:30
|
|
|
* Check if repository index-meta contains changes compared to its
|
2014-01-21 15:36:02 +05:30
|
|
|
* current state.
|
|
|
|
*/
|
|
|
|
if ((buf = pubkey_from_privkey(rsa)) == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
meta = xbps_dictionary_create();
|
2013-10-07 13:53:14 +05:30
|
|
|
|
2014-01-21 15:36:02 +05:30
|
|
|
data = xbps_data_create_data(buf, strlen(buf));
|
2014-01-30 17:37:34 +05:30
|
|
|
rpubkey = xbps_dictionary_get(repo->idxmeta, "public-key");
|
|
|
|
if (!xbps_data_equals(rpubkey, data))
|
2014-01-21 15:36:02 +05:30
|
|
|
flush = true;
|
|
|
|
|
2014-02-06 21:04:45 +05:30
|
|
|
free(buf);
|
2014-01-30 17:37:34 +05:30
|
|
|
|
2014-01-21 15:36:02 +05:30
|
|
|
pubkeysize = RSA_size(rsa) * 8;
|
2014-01-30 17:37:34 +05:30
|
|
|
xbps_dictionary_get_uint16(repo->idxmeta, "public-key-size", &rpubkeysize);
|
|
|
|
if (rpubkeysize != pubkeysize)
|
2014-01-21 15:36:02 +05:30
|
|
|
flush = true;
|
|
|
|
|
2014-01-31 00:16:46 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(repo->idxmeta, "signature-by", &rsignedby);
|
2014-01-30 17:37:34 +05:30
|
|
|
if (rsignedby == NULL || strcmp(rsignedby, signedby))
|
2014-01-21 15:36:02 +05:30
|
|
|
flush = true;
|
|
|
|
|
|
|
|
if (!flush)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
xbps_dictionary_set(meta, "public-key", data);
|
|
|
|
xbps_dictionary_set_uint16(meta, "public-key-size", pubkeysize);
|
|
|
|
xbps_dictionary_set_cstring_nocopy(meta, "signature-by", signedby);
|
|
|
|
xbps_dictionary_set_cstring_nocopy(meta, "signature-type", "rsa");
|
2014-02-06 16:22:36 +05:30
|
|
|
xbps_object_release(data);
|
|
|
|
data = NULL;
|
|
|
|
|
2014-11-13 21:39:43 +05:30
|
|
|
if (!repodata_flush(xhp, repodir, repo->idx, meta)) {
|
2014-02-06 21:04:59 +05:30
|
|
|
fprintf(stderr, "failed to write repodata: %s\n", strerror(errno));
|
2014-04-20 13:32:54 +05:30
|
|
|
goto out;
|
2014-02-06 21:04:59 +05:30
|
|
|
}
|
2014-01-21 15:36:02 +05:30
|
|
|
printf("Signed repository (%u package%s)\n",
|
|
|
|
xbps_dictionary_count(repo->idx),
|
|
|
|
xbps_dictionary_count(repo->idx) == 1 ? "" : "s");
|
|
|
|
|
|
|
|
out:
|
2014-04-20 13:32:54 +05:30
|
|
|
if (defprivkey) {
|
|
|
|
free(defprivkey);
|
|
|
|
}
|
2014-01-21 15:36:02 +05:30
|
|
|
if (rsa) {
|
|
|
|
RSA_free(rsa);
|
|
|
|
rsa = NULL;
|
|
|
|
}
|
2014-04-20 13:32:54 +05:30
|
|
|
if (repo) {
|
2015-01-11 13:41:38 +05:30
|
|
|
xbps_repo_close(repo);
|
2014-04-20 13:32:54 +05:30
|
|
|
}
|
2015-03-25 16:30:59 +05:30
|
|
|
xbps_repo_unlock(rlockfd, rlockfname);
|
|
|
|
|
2014-01-21 15:36:02 +05:30
|
|
|
return rv ? -1 : 0;
|
2013-10-05 15:08:04 +05:30
|
|
|
}
|