35#include <openssl/err.h>
36#include <openssl/sha.h>
37#include <openssl/rsa.h>
38#include <openssl/ssl.h>
39#include <openssl/pem.h>
41#include "xbps_api_impl.h"
44rsa_verify_hash(
struct xbps_repo *repo, xbps_data_t pubkey,
45 unsigned char *sig,
unsigned int siglen,
46 unsigned char *sha256)
52 ERR_load_crypto_strings();
53 SSL_load_error_strings();
55 bio = BIO_new_mem_buf(xbps_data_data_nocopy(pubkey),
56 xbps_data_size(pubkey));
59 rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
61 xbps_dbg_printf(
"`%s' error reading public key: %s\n",
62 repo->
uri, ERR_error_string(ERR_get_error(), NULL));
66 rv = RSA_verify(NID_sha256, sha256, SHA256_DIGEST_LENGTH, sig, siglen, rsa);
71 return rv ? true :
false;
76 unsigned char *digest)
78 xbps_dictionary_t repokeyd = NULL;
81 unsigned char *sig_buf = NULL;
82 size_t sigbuflen, sigfilelen;
83 char *rkeyfile = NULL;
86 if (!xbps_dictionary_count(repo->
idxmeta)) {
87 xbps_dbg_printf(
"%s: unsigned repository\n", repo->
uri);
92 xbps_dbg_printf(
"%s: incomplete signed repo, missing hexfp obj\n", repo->
uri);
101 if (xbps_object_type(repokeyd) != XBPS_TYPE_DICTIONARY) {
102 xbps_dbg_printf(
"cannot read rkey data at %s: %s\n",
103 rkeyfile, strerror(errno));
107 pubkey = xbps_dictionary_get(repokeyd,
"public-key");
108 if (xbps_object_type(pubkey) != XBPS_TYPE_DATA)
111 if (!
xbps_mmap_file(sigfile, (
void *)&sig_buf, &sigbuflen, &sigfilelen)) {
112 xbps_dbg_printf(
"can't open signature file %s: %s\n",
113 sigfile, strerror(errno));
119 if (rsa_verify_hash(repo, pubkey, sig_buf, sigfilelen, digest))
128 (void)munmap(sig_buf, sigbuflen);
130 xbps_object_release(repokeyd);
139 unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
143 xbps_dbg_printf(
"can't open file %s: %s\n", fname, strerror(errno));
147 snprintf(sig,
sizeof sig,
"%s.sig2", fname);
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
xbps_dictionary_t idxmeta
xbps_dictionary_t xbps_plist_dictionary_from_file(const char *path)
char * xbps_pubkey2fp(xbps_data_t pubkey)
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
bool xbps_verify_file_signature(struct xbps_repo *repo, const char *fname)
char bool xbps_mmap_file(const char *file, void **mmf, size_t *mmflen, size_t *filelen)
bool xbps_verify_signature(struct xbps_repo *repo, const char *sigfile, unsigned char *digest)
bool xbps_file_sha256_raw(unsigned char *dst, size_t len, const char *file)