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
75 #include <openssl/err.h>
78 * @brief intialize OpenSSL
88 //CRYPTO_malloc_init();
89 ERR_load_crypto_strings();
90 OpenSSL_add_all_algorithms();
95 * last error from OpenSSL as a string
98 get_error_string(void)
101 return (ERR_error_string(ERR_get_error(), NULL));
106 * @brief decode a signature packet
108 * We only support RSA
113 decode_sig(int tag, unsigned char **pptr, size_t len, OpenPGP_sig *sig)
116 unsigned char *pgpbytes;
124 n = tag; /* avoid unused */
127 * We need to keep a reference to the packet bytes
128 * as these form part of the signature data.
132 pgpbytes = ptr = *pptr;
136 sig->pgpbytes = malloc(5);
139 memcpy(sig->pgpbytes, ptr, 5);
140 sig->pgpbytes_len = 5;
141 sig->sig_type = *ptr++;
143 sig->key_id = octets2hex(ptr, 8);
145 sig->sig_alg = *ptr++;
146 sig->hash_alg = *ptr++;
147 } else if (version == 4) {
148 sig->sig_type = *ptr++;
149 sig->sig_alg = *ptr++;
150 sig->hash_alg = *ptr++;
151 hcount = octets2i(ptr, 2);
153 sig->pgpbytes_len = (size_t)hcount + 6;
154 sig->pgpbytes = malloc(sig->pgpbytes_len + 6);
157 memcpy(sig->pgpbytes, pgpbytes, sig->pgpbytes_len);
158 sp = &sig->pgpbytes[sig->pgpbytes_len];
161 memcpy(sp, i2octets(4, (int)sig->pgpbytes_len), 4);
162 sig->pgpbytes_len += 6;
165 sp = decode_subpacket(&ptr, &stag, &n);
167 /* can check stag to see if we care */
169 ucount = octets2i(ptr, 2);
172 sp = decode_subpacket(&ptr, &stag, &n);
174 /* can check stag to see if we care */
177 sig->key_id = octets2hex(sp, 8);
182 ptr += 2; /* skip hash16 */
183 if (sig->sig_alg == 1) { /* RSA */
184 sig->sig = decode_mpi(&ptr, &sig->sig_len);
187 return ((ssize_t)len);
191 * @brief map OpenPGP hash algorithm id's to name
195 static struct hash_alg_map {
209 get_hname(int hash_alg)
211 struct hash_alg_map *hmp;
213 for (hmp = hash_algs; hmp->halg > 0; hmp++) {
214 if (hmp->halg == hash_alg)
220 /* lifted from signer.c */
222 * @brief verify a digest
224 * The public key, digest name, file and signature data.
226 * @return 1 on success 0 on failure, -1 on error
230 verify_digest (EVP_PKEY *pkey,
232 unsigned char *mdata, size_t mlen,
233 unsigned char *sdata, size_t slen)
236 const EVP_MD *md = NULL;
237 EVP_PKEY_CTX *pctx = NULL;
242 md = EVP_get_digestbyname(digest);
243 EVP_DigestInit(&ctx, md);
245 pctx = EVP_PKEY_CTX_new(pkey, NULL);
248 if (EVP_PKEY_verify_init(pctx) <= 0)
250 if (EVP_PKEY_CTX_set_signature_md(pctx, ctx.digest) <= 0)
252 i = EVP_PKEY_verify(pctx, sdata, slen, mdata, mlen);
256 EVP_PKEY_CTX_free(pctx);
263 * @brief verify OpenPGP signed file
266 * @param[in] filename
267 * used to determine the signature name
270 * content of filename
276 * content of signature
283 * @return 0 on success
286 openpgp_verify(const char *filename,
287 unsigned char *fdata, size_t fbytes,
288 unsigned char *sdata, size_t sbytes,
294 const br_hash_class *md;
295 br_hash_compat_context mctx;
296 const unsigned char *hash_oid;
298 const EVP_MD *md = NULL;
301 unsigned char mdata[64];
303 unsigned char *ddata = NULL;
310 sig = NEW(OpenPGP_sig);
311 if (!sdata || !sig) {
312 warnx("cannot verify %s", filename);
315 if (!(sdata[0] & OPENPGP_TAG_ISTAG))
316 sdata = ddata = dearmor((char *)sdata, sbytes, &sbytes);
318 rc = decode_packet(2, &ptr, sbytes, (decoder_t)decode_sig, sig);
319 DEBUG_PRINTF(2, ("rc=%d keyID=%s\n", rc, sig->key_id ? sig->key_id : "?"));
320 if (rc == 0 && sig->key_id) {
321 key = load_key_id(sig->key_id);
323 warnx("cannot find key-id: %s", sig->key_id);
325 } else if (!(hname = get_hname(sig->hash_alg))) {
326 warnx("unsupported hash algorithm: %d", sig->hash_alg);
330 * Hash fdata according to the OpenPGP recipe
335 switch (sig->hash_alg) { /* see hash_algs above */
337 md = &br_sha1_vtable;
339 hash_oid = BR_HASH_OID_SHA1;
342 md = &br_sha256_vtable;
343 mlen = br_sha256_SIZE;
344 hash_oid = BR_HASH_OID_SHA256;
347 warnx("unsupported hash algorithm: %s", hname);
350 md->init(&mctx.vtable);
351 md->update(&mctx.vtable, fdata, fbytes);
352 md->update(&mctx.vtable, sig->pgpbytes,
354 md->out(&mctx.vtable, mdata);
356 rc = verify_rsa_digest(key->key, hash_oid,
357 mdata, mlen, sig->sig, sig->sig_len);
359 md = EVP_get_digestbyname(hname);
360 EVP_DigestInit(&mctx, md);
361 EVP_DigestUpdate(&mctx, fdata, fbytes);
362 EVP_DigestUpdate(&mctx, sig->pgpbytes,
364 mlen = sizeof(mdata);
365 EVP_DigestFinal(&mctx,mdata,(unsigned int *)&mlen);
367 rc = verify_digest(key->key, hname, mdata, mlen,
368 sig->sig, sig->sig_len);
373 printf("Verified %s signed by %s\n",
375 key->user ? key->user->name : "someone");
376 rc = 0; /* success */
377 } else if (rc == 0) {
378 printf("Unverified %s: %s\n",
379 filename, get_error_string());
382 printf("Unverified %s\n", filename);
386 warnx("cannot decode signature for %s", filename);
397 * @brief list of extensions we handle
399 * ".asc" is preferred as it works seamlessly with openpgp
401 static const char *sig_exts[] = {
409 * @brief verify OpenPGP signed file
412 * @param[in] filename
413 * used to determine the signature name
416 * content of filename
425 openpgp_verify_file(const char *filename, unsigned char *fdata, size_t nbytes)
427 char pbuf[MAXPATHLEN];
428 unsigned char *sdata;
429 const char *sname = NULL;
434 for (ep = sig_exts; *ep; ep++) {
435 n = snprintf(pbuf, sizeof(pbuf), "%s%s", filename, *ep);
436 if (n >= (int)sizeof(pbuf)) {
437 warnx("cannot form signature name for %s", filename);
440 if (access(pbuf, R_OK) == 0) {
446 warnx("cannot find signature for %s", filename);
449 sdata = read_file(sname, &sz);
450 return (openpgp_verify(filename, fdata, nbytes, sdata, sz, 1));
455 * @brief verify OpenPGP signature
457 * @return content of signed file
460 verify_asc(const char *sigfile, int flags)
462 char pbuf[MAXPATHLEN];
465 unsigned char *fdata, *sdata;
466 size_t fbytes, sbytes;
468 if ((sdata = read_file(sigfile, &sbytes))) {
469 n = strlcpy(pbuf, sigfile, sizeof(pbuf));
470 if ((cp = strrchr(pbuf, '.')))
472 if ((fdata = read_file(pbuf, &fbytes))) {
473 if (openpgp_verify(pbuf, fdata, fbytes, sdata,