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.h>
34 octets2hex(unsigned char *ptr, size_t n)
40 hex = malloc(2 * n + 1);
42 for (i = 0, cp = hex; i < n; i++) {
43 snprintf(&cp[i*2], 3, "%02X", ptr[i]);
50 i2octets(int n, size_t i)
52 static unsigned char o[16];
57 for (j = 0, x = n - 1; x >= 0; x--, j++) {
58 o[j] = (unsigned char)((i & (0xff << x * 8)) >> x * 8);
64 octets2i(unsigned char *ptr, size_t n)
69 for (val = i = 0; i < n; i++) {
70 val |= (*ptr++ << ((n - i - 1) * 8));
76 * @brief decode packet tag
78 * Also indicate if new/old and in the later case
84 decode_tag(unsigned char *ptr, int *isnew, int *ltype)
88 if (!ptr || !isnew || !ltype)
92 if (!(tag & OPENPGP_TAG_ISTAG))
93 return (-1); /* we are lost! */
94 *isnew = tag & OPENPGP_TAG_ISNEW;
96 *ltype = -1; /* irrelevant */
97 tag &= OPENPGP_TAG_NEW_MASK;
99 *ltype = tag & OPENPGP_TAG_OLD_TYPE;
100 tag = (tag & OPENPGP_TAG_OLD_MASK) >> 2;
106 * @brief return packet length
111 decode_new_len(unsigned char **pptr)
120 if (!(*ptr < 224 || *ptr == 255))
121 return (-1); /* not supported */
125 else if (*ptr < 224) {
126 len = ((*ptr - 192) << 8) + *(ptr+1) + 192;
128 } else if (*ptr == 255) {
129 len = (*ptr++ << 24);
130 len |= (*ptr++ << 16);
140 * @brief return packet length
145 decode_len(unsigned char **pptr, int ltype)
151 return (decode_new_len(pptr));
183 * @brief return pointer and length of an mpi
188 decode_mpi(unsigned char **pptr, size_t *sz)
194 if (pptr == NULL || sz == NULL)
199 mlen = (size_t)(*ptr++ << 8);
200 mlen |= (size_t)*ptr++; /* number of bits */
201 mlen = (mlen + 7) / 8; /* number of bytes */
210 * @brief return an OpenSSL BIGNUM from mpi
216 mpi2bn(unsigned char **pptr, size_t *sz)
218 return (decode_mpi(pptr, sz));
222 mpi2bn(unsigned char **pptr)
233 mlen = (*ptr++ << 8);
234 mlen |= *ptr++; /* number of bits */
235 mlen = (mlen + 7) / 8; /* number of bytes */
236 bn = BN_bin2bn(ptr, mlen, NULL);
245 * @brief decode a packet
247 * If want is set, check that the packet tag matches
248 * if all good, call the provided decoder with its arg
250 * @return count of unconsumed data
255 decode_packet(int want, unsigned char **pptr, size_t nbytes,
256 decoder_t decoder, void *decoder_arg)
268 tag = decode_tag(ptr, &isnew, <ype);
270 if (want > 0 && tag != want)
274 len = rc = decode_len(&ptr, ltype);
275 hlen = (int)(ptr - nptr);
276 nptr = ptr + len; /* consume it */
279 rc = decoder(tag, &ptr, len, decoder_arg);
281 nbytes -= (size_t)(hlen + len);
283 return (rc); /* error */
284 return ((int)nbytes); /* unconsumed data */
288 * @brief decode a sub packet
290 * @sa rfc4880:5.2.3.1
293 decode_subpacket(unsigned char **pptr, int *stag, int *sz)
299 len = decode_len(&ptr, -1);
300 *sz = (int)(len + ptr - *pptr);