XBPS Library API 20240111
The X Binary Package System
verifysig.c
1/*-
2 * Copyright (c) 2013-2014 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <libgen.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33#include <sys/mman.h>
34
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>
40
41#include "xbps_api_impl.h"
42
43static bool
44rsa_verify_hash(struct xbps_repo *repo, xbps_data_t pubkey,
45 unsigned char *sig, unsigned int siglen,
46 unsigned char *sha256)
47{
48 BIO *bio;
49 RSA *rsa;
50 int rv;
51
52 ERR_load_crypto_strings();
53 SSL_load_error_strings();
54
55 bio = BIO_new_mem_buf(xbps_data_data_nocopy(pubkey),
56 xbps_data_size(pubkey));
57 assert(bio);
58
59 rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
60 if (rsa == NULL) {
61 xbps_dbg_printf("`%s' error reading public key: %s\n",
62 repo->uri, ERR_error_string(ERR_get_error(), NULL));
63 return false;
64 }
65
66 rv = RSA_verify(NID_sha256, sha256, SHA256_DIGEST_LENGTH, sig, siglen, rsa);
67 RSA_free(rsa);
68 BIO_free(bio);
69 ERR_free_strings();
70
71 return rv ? true : false;
72}
73
74bool
75xbps_verify_signature(struct xbps_repo *repo, const char *sigfile,
76 unsigned char *digest)
77{
78 xbps_dictionary_t repokeyd = NULL;
79 xbps_data_t pubkey;
80 char *hexfp = NULL;
81 unsigned char *sig_buf = NULL;
82 size_t sigbuflen, sigfilelen;
83 char *rkeyfile = NULL;
84 bool val = false;
85
86 if (!xbps_dictionary_count(repo->idxmeta)) {
87 xbps_dbg_printf("%s: unsigned repository\n", repo->uri);
88 return false;
89 }
90 hexfp = xbps_pubkey2fp(xbps_dictionary_get(repo->idxmeta, "public-key"));
91 if (hexfp == NULL) {
92 xbps_dbg_printf("%s: incomplete signed repo, missing hexfp obj\n", repo->uri);
93 return false;
94 }
95
96 /*
97 * Prepare repository RSA public key to verify fname signature.
98 */
99 rkeyfile = xbps_xasprintf("%s/keys/%s.plist", repo->xhp->metadir, hexfp);
100 repokeyd = xbps_plist_dictionary_from_file(rkeyfile);
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));
104 goto out;
105 }
106
107 pubkey = xbps_dictionary_get(repokeyd, "public-key");
108 if (xbps_object_type(pubkey) != XBPS_TYPE_DATA)
109 goto out;
110
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));
114 goto out;
115 }
116 /*
117 * Verify fname RSA signature.
118 */
119 if (rsa_verify_hash(repo, pubkey, sig_buf, sigfilelen, digest))
120 val = true;
121
122out:
123 if (hexfp)
124 free(hexfp);
125 if (rkeyfile)
126 free(rkeyfile);
127 if (sig_buf)
128 (void)munmap(sig_buf, sigbuflen);
129 if (repokeyd)
130 xbps_object_release(repokeyd);
131
132 return val;
133}
134
135bool
136xbps_verify_file_signature(struct xbps_repo *repo, const char *fname)
137{
138 char sig[PATH_MAX];
139 unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
140 bool val = false;
141
142 if (!xbps_file_sha256_raw(digest, sizeof digest, fname)) {
143 xbps_dbg_printf("can't open file %s: %s\n", fname, strerror(errno));
144 return false;
145 }
146
147 snprintf(sig, sizeof sig, "%s.sig2", fname);
148 val = xbps_verify_signature(repo, sig, digest);
149
150 return val;
151}
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
Definition xbps.h:664
xbps_dictionary_t idxmeta
Definition xbps.h:1434
struct xbps_handle * xhp
Definition xbps.h:1422
const char * uri
Definition xbps.h:1440
Repository structure.
Definition xbps.h:1409
xbps_dictionary_t xbps_plist_dictionary_from_file(const char *path)
char * xbps_pubkey2fp(xbps_data_t pubkey)
Definition pubkey2fp.c:66
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
bool xbps_verify_file_signature(struct xbps_repo *repo, const char *fname)
Definition verifysig.c:136
char bool xbps_mmap_file(const char *file, void **mmf, size_t *mmflen, size_t *filelen)
Definition util_hash.c:65
bool xbps_verify_signature(struct xbps_repo *repo, const char *sigfile, unsigned char *digest)
Definition verifysig.c:75
bool xbps_file_sha256_raw(unsigned char *dst, size_t len, const char *file)
Definition util_hash.c:116