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.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "../libsecureboot-priv.h"
35 * @brief decode user-id packet
42 decode_user(int tag, unsigned char **pptr, size_t len, OpenPGP_user *user)
47 user->id = malloc(len + 1);
48 strncpy(user->id, (char *)*pptr, len);
50 user->name = user->id;
51 cp = strchr(user->id, '<');
53 user->id = strdup(user->id);
58 return ((ssize_t)len);
62 * @brief decode a key packet
64 * We only really support v4 and RSA
69 decode_key(int tag, unsigned char **pptr, size_t len, OpenPGP_key *key)
75 unsigned char mdata[br_sha512_SIZE];
79 const EVP_MD *md = NULL;
81 unsigned char mdata[EVP_MAX_MD_SIZE];
91 if (version == 4) { /* all we support really */
92 /* comput key fingerprint and id @sa rfc4880:12.2 */
93 mdata[0] = 0x99; /* rfc4880: 12.2.a.1 */
94 mdata[1] = (len >> 8) & 0xff;
95 mdata[2] = len & 0xff;
99 br_sha1_update(&mctx, mdata, 3);
100 br_sha1_update(&mctx, ptr, len);
101 br_sha1_out(&mctx, mdata);
104 md = EVP_get_digestbyname("sha1");
105 EVP_DigestInit(&mctx, md);
106 EVP_DigestUpdate(&mctx, mdata, 3);
107 EVP_DigestUpdate(&mctx, ptr, len);
108 mlen = (unsigned int)sizeof(mdata);
109 EVP_DigestFinal(&mctx, mdata, &mlen);
111 key->id = octets2hex(&mdata[mlen - 8], 8);
113 ptr += 1; /* done with version */
114 ptr += 4; /* skip ctime */
116 ptr += 2; /* valid days */
117 key->sig_alg = *ptr++;
118 if (key->sig_alg == 1) { /* RSA */
120 key->key = NEW(br_rsa_public_key);
123 key->key->n = mpi2bn(&ptr, &key->key->nlen);
124 key->key->e = mpi2bn(&ptr, &key->key->elen);
129 rsa->n = mpi2bn(&ptr);
130 rsa->e = mpi2bn(&ptr);
131 key->key = EVP_PKEY_new();
132 if (!key->key || !rsa->n || !rsa->e) {
135 if (!EVP_PKEY_set1_RSA(key->key, rsa))
140 return ((ssize_t)len);
149 EVP_PKEY_free(key->key);
157 load_key_buf(unsigned char *buf, size_t nbytes)
159 unsigned char *data = NULL;
170 if (!(buf[0] & OPENPGP_TAG_ISTAG)) {
171 data = dearmor((char *)buf, nbytes, &nbytes);
175 key = NEW(OpenPGP_key);
177 rc = decode_packet(0, &ptr, nbytes, (decoder_t)decode_key,
185 tag = decode_tag(ptr, &isnew, <ype);
187 key->user = NEW(OpenPGP_user);
188 rc = decode_packet(0, &ptr, (size_t)rc,
189 (decoder_t)decode_user, key->user);
197 static LIST_HEAD(, OpenPGP_key_) trust_list;
200 * @brief add a key to our list
203 openpgp_trust_add(OpenPGP_key *key)
210 LIST_INIT(&trust_list);
213 LIST_INSERT_HEAD(&trust_list, key, entries);
217 * @brief if keyID is in our list return the key
219 * @return key or NULL
222 openpgp_trust_get(const char *keyID)
226 openpgp_trust_add(NULL); /* initialize if needed */
228 LIST_FOREACH(key, &trust_list, entries) {
229 if (strcmp(key->id, keyID) == 0)
236 * @brief load a key from file
239 load_key_file(const char *kfile)
241 unsigned char *data = NULL;
245 data = read_file(kfile, &n);
246 key = load_key_buf(data, n);
248 openpgp_trust_add(key);
255 /* we can lookup keyID in filesystem */
257 static const char *trust_store[] = {
264 * @brief lookup key id in trust store
268 load_trusted_key_id(const char *keyID)
270 char kfile[MAXPATHLEN];
274 for (tp = trust_store; *tp; tp++) {
275 n = (size_t)snprintf(kfile, sizeof(kfile), "%s/%s", *tp, keyID);
276 if (n >= sizeof(kfile))
278 if (access(kfile, R_OK) == 0) {
279 return (load_key_file(kfile));
287 * @brief return key if trusted
290 load_key_id(const char *keyID)
294 key = openpgp_trust_get(keyID);
297 key = load_trusted_key_id(keyID);
303 * @brief initialize our internal trust store if any
306 openpgp_trust_init(void)
308 static int once = -1;
319 for (tp = ta_ASC; *tp; tp++) {
320 if ((cp = strdup(*tp))) {
322 key = load_key_buf((unsigned char *)cp, n);
325 openpgp_trust_add(key);
336 * @brief test that we can verify a signature
338 * Unlike X.509 certificates, we only support RSA keys
339 * so we stop after first successful signature verification
340 * (which should also be the first attempt ;-)
343 openpgp_self_tests(void)
345 static int rc = -1; /* remember result */
347 const char **vp, **tp;
348 char *fdata, *sdata = NULL;
349 size_t fbytes, sbytes;
351 if (openpgp_trust_init() > 0) {
352 for (tp = ta_ASC, vp = vc_ASC; *tp && *vp && rc; tp++, vp++) {
353 if ((fdata = strdup(*tp)) &&
354 (sdata = strdup(*vp))) {
355 fbytes = strlen(fdata);
356 sbytes = strlen(sdata);
357 rc = openpgp_verify("ta_ASC",
358 (unsigned char *)fdata, fbytes,
359 (unsigned char *)sdata, sbytes, 0);
360 printf("Testing verify OpenPGP signature:\t\t%s\n",
361 rc ? "Failed" : "Passed");