2 * Copyright (c) 2018, Juniper Networks, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * 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
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * from: signer.c,v 1.10 2018/03/23 01:14:30 sjg
29 * @(#) Copyright (c) 2012 Simon J. Gerraty
31 * This file is provided in the hope that it will
32 * be of use. There is absolutely NO WARRANTY.
33 * Permission to copy, redistribute or otherwise
34 * use this file is hereby granted provided that
35 * the above copyright notice and this notice are
38 * Please send copies of changes and bug-fixes to:
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include "../libsecureboot-priv.h"
50 #include <sys/param.h>
65 #define get_error_string ve_error_get
77 #include <openssl/err.h>
80 * @brief intialize OpenSSL
90 //CRYPTO_malloc_init();
91 ERR_load_crypto_strings();
92 OpenSSL_add_all_algorithms();
97 * last error from OpenSSL as a string
100 get_error_string(void)
103 return (ERR_error_string(ERR_get_error(), NULL));
108 * @brief decode a signature packet
110 * We only support RSA
115 decode_sig(int tag, unsigned char **pptr, size_t len, OpenPGP_sig *sig)
118 unsigned char *pgpbytes;
126 n = tag; /* avoid unused */
129 * We need to keep a reference to the packet bytes
130 * as these form part of the signature data.
134 pgpbytes = ptr = *pptr;
138 sig->pgpbytes = malloc(5);
141 memcpy(sig->pgpbytes, ptr, 5);
142 sig->pgpbytes_len = 5;
143 sig->sig_type = *ptr++;
145 sig->key_id = octets2hex(ptr, 8);
147 sig->sig_alg = *ptr++;
148 sig->hash_alg = *ptr++;
149 } else if (version == 4) {
150 sig->sig_type = *ptr++;
151 sig->sig_alg = *ptr++;
152 sig->hash_alg = *ptr++;
153 hcount = octets2i(ptr, 2);
155 sig->pgpbytes_len = (size_t)hcount + 6;
156 sig->pgpbytes = malloc(sig->pgpbytes_len + 6);
159 memcpy(sig->pgpbytes, pgpbytes, sig->pgpbytes_len);
160 sp = &sig->pgpbytes[sig->pgpbytes_len];
163 memcpy(sp, i2octets(4, (int)sig->pgpbytes_len), 4);
164 sig->pgpbytes_len += 6;
167 sp = decode_subpacket(&ptr, &stag, &n);
169 /* can check stag to see if we care */
171 ucount = octets2i(ptr, 2);
174 sp = decode_subpacket(&ptr, &stag, &n);
176 /* can check stag to see if we care */
179 sig->key_id = octets2hex(sp, 8);
184 ptr += 2; /* skip hash16 */
185 if (sig->sig_alg == 1) { /* RSA */
186 sig->sig = decode_mpi(&ptr, &sig->sig_len);
189 return ((ssize_t)len);
193 * @brief map OpenPGP hash algorithm id's to name
197 static struct hash_alg_map {
211 get_hname(int hash_alg)
213 struct hash_alg_map *hmp;
215 for (hmp = hash_algs; hmp->halg > 0; hmp++) {
216 if (hmp->halg == hash_alg)
222 /* lifted from signer.c */
224 * @brief verify a digest
226 * The public key, digest name, file and signature data.
228 * @return 1 on success 0 on failure, -1 on error
232 verify_digest (EVP_PKEY *pkey,
234 unsigned char *mdata, size_t mlen,
235 unsigned char *sdata, size_t slen)
238 const EVP_MD *md = NULL;
239 EVP_PKEY_CTX *pctx = NULL;
244 md = EVP_get_digestbyname(digest);
245 EVP_DigestInit(&ctx, md);
247 pctx = EVP_PKEY_CTX_new(pkey, NULL);
250 if (EVP_PKEY_verify_init(pctx) <= 0)
252 if (EVP_PKEY_CTX_set_signature_md(pctx, ctx.digest) <= 0)
254 i = EVP_PKEY_verify(pctx, sdata, slen, mdata, mlen);
258 EVP_PKEY_CTX_free(pctx);
265 * @brief verify OpenPGP signed file
268 * @param[in] filename
269 * used to determine the signature name
272 * content of filename
278 * content of signature
285 * @return 0 on success
288 openpgp_verify(const char *filename,
289 unsigned char *fdata, size_t fbytes,
290 unsigned char *sdata, size_t sbytes,
296 const br_hash_class *md;
297 br_hash_compat_context mctx;
298 const unsigned char *hash_oid;
300 const EVP_MD *md = NULL;
303 unsigned char mdata[64];
305 unsigned char *ddata = NULL;
312 sig = NEW(OpenPGP_sig);
313 if (!sdata || !sig) {
314 warnx("cannot verify %s", filename);
317 if (!(sdata[0] & OPENPGP_TAG_ISTAG))
318 sdata = ddata = dearmor((char *)sdata, sbytes, &sbytes);
320 rc = decode_packet(2, &ptr, sbytes, (decoder_t)decode_sig, sig);
321 if (rc == 0 && sig->key_id) {
322 key = load_key_id(sig->key_id);
324 warnx("cannot find key-id: %s", sig->key_id);
326 } else if (!(hname = get_hname(sig->hash_alg))) {
327 warnx("unsupported hash algorithm: %d", sig->hash_alg);
331 * Hash fdata according to the OpenPGP recipe
336 switch (sig->hash_alg) { /* see hash_algs above */
338 md = &br_sha1_vtable;
340 hash_oid = BR_HASH_OID_SHA1;
343 md = &br_sha256_vtable;
344 mlen = br_sha256_SIZE;
345 hash_oid = BR_HASH_OID_SHA256;
348 warnx("unsupported hash algorithm: %s", hname);
351 md->init(&mctx.vtable);
352 md->update(&mctx.vtable, fdata, fbytes);
353 md->update(&mctx.vtable, sig->pgpbytes,
355 md->out(&mctx.vtable, mdata);
357 rc = verify_rsa_digest(key->key, hash_oid,
358 mdata, mlen, sig->sig, sig->sig_len);
360 md = EVP_get_digestbyname(hname);
361 EVP_DigestInit(&mctx, md);
362 EVP_DigestUpdate(&mctx, fdata, fbytes);
363 EVP_DigestUpdate(&mctx, sig->pgpbytes,
365 mlen = sizeof(mdata);
366 EVP_DigestFinal(&mctx,mdata,(unsigned int *)&mlen);
368 rc = verify_digest(key->key, hname, mdata, mlen,
369 sig->sig, sig->sig_len);
374 printf("Verified %s signed by %s\n",
376 key->user ? key->user->name : "someone");
377 rc = 0; /* success */
378 } else if (rc == 0) {
379 printf("Unverified %s: %s\n",
380 filename, get_error_string());
383 printf("Unverified %s\n", filename);
387 warnx("cannot decode signature for %s", filename);
398 * @brief list of extensions we handle
400 * ".asc" is preferred as it works seamlessly with openpgp
402 static const char *sig_exts[] = {
410 * @brief verify OpenPGP signed file
413 * @param[in] filename
414 * used to determine the signature name
417 * content of filename
426 openpgp_verify_file(const char *filename, unsigned char *fdata, size_t nbytes)
428 char pbuf[MAXPATHLEN];
429 unsigned char *sdata;
430 const char *sname = NULL;
435 for (ep = sig_exts; *ep; ep++) {
436 n = snprintf(pbuf, sizeof(pbuf), "%s%s", filename, *ep);
437 if (n >= (int)sizeof(pbuf)) {
438 warnx("cannot form signature name for %s", filename);
441 if (access(pbuf, R_OK) == 0) {
447 warnx("cannot find signature for %s", filename);
450 sdata = read_file(sname, &sz);
451 return (openpgp_verify(filename, fdata, nbytes, sdata, sz, 1));
456 * @brief verify OpenPGP signature
458 * @return content of signed file
461 verify_asc(const char *sigfile, int flags)
463 char pbuf[MAXPATHLEN];
466 unsigned char *fdata, *sdata;
467 size_t fbytes, sbytes;
469 if ((sdata = read_file(sigfile, &sbytes))) {
470 n = strlcpy(pbuf, sigfile, sizeof(pbuf));
471 if ((cp = strrchr(pbuf, '.')))
473 if ((fdata = read_file(pbuf, &fbytes))) {
474 if (openpgp_verify(pbuf, fdata, fbytes, sdata,