3 * Provide info about PGP data.
5 * Copyright (c) 2005 Marko Kreen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * contrib/pgcrypto/pgp-info.c
38 read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
41 PGP_PubKey *pk = NULL;
43 res = _pgp_read_public_key(pkt, &pk);
47 /* skip secret key part, if it exists */
48 res = pgp_skip_packet(pkt);
52 /* is it encryption key */
55 case PGP_PUB_ELG_ENCRYPT:
56 case PGP_PUB_RSA_ENCRYPT:
57 case PGP_PUB_RSA_ENCRYPT_SIGN:
58 memcpy(keyid_buf, pk->key_id, 8);
71 read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
80 res = pullf_read_fixed(pkt, 8, keyid_buf);
84 return pgp_skip_packet(pkt);
87 static const char hextbl[] = "0123456789ABCDEF";
90 print_key(uint8 *keyid, char *dst)
95 for (i = 0; i < 8; i++)
98 *dst++ = hextbl[(c >> 4) & 0x0F];
99 *dst++ = hextbl[c & 0x0F];
105 static const uint8 any_key[] =
106 {0, 0, 0, 0, 0, 0, 0, 0};
109 * dst should have room for 17 bytes
112 pgp_get_keyid(MBuf *pgp_data, char *dst)
116 PullFilter *pkt = NULL;
124 int got_main_key = 0;
127 res = pullf_create_mbuf_reader(&src, pgp_data);
133 res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
136 res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
142 case PGP_PKT_SECRET_KEY:
143 case PGP_PKT_PUBLIC_KEY:
144 /* main key is for signing, so ignore it */
148 res = pgp_skip_packet(pkt);
151 res = PXE_PGP_MULTIPLE_KEYS;
153 case PGP_PKT_SECRET_SUBKEY:
154 case PGP_PKT_PUBLIC_SUBKEY:
155 res = read_pubkey_keyid(pkt, keyid_buf);
161 case PGP_PKT_PUBENCRYPTED_SESSKEY:
163 res = read_pubenc_keyid(pkt, keyid_buf);
165 case PGP_PKT_SYMENCRYPTED_DATA:
166 case PGP_PKT_SYMENCRYPTED_DATA_MDC:
167 /* don't skip it, just stop */
170 case PGP_PKT_SYMENCRYPTED_SESSKEY:
173 case PGP_PKT_SIGNATURE:
176 case PGP_PKT_USER_ID:
177 case PGP_PKT_USER_ATTR:
178 case PGP_PKT_PRIV_61:
179 res = pgp_skip_packet(pkt);
182 res = PXE_PGP_CORRUPT_DATA;
189 if (res < 0 || got_data)
200 /* now check sanity */
201 if (got_pub_key && got_pubenc_key)
202 res = PXE_PGP_CORRUPT_DATA;
205 res = PXE_PGP_MULTIPLE_KEYS;
207 if (got_pubenc_key > 1)
208 res = PXE_PGP_MULTIPLE_KEYS;
211 * if still ok, look what we got
215 if (got_pubenc_key || got_pub_key)
217 if (memcmp(keyid_buf, any_key, 8) == 0)
219 memcpy(dst, "ANYKEY", 7);
223 res = print_key(keyid_buf, dst);
225 else if (got_symenc_key)
227 memcpy(dst, "SYMKEY", 7);
231 res = PXE_PGP_NO_USABLE_KEY;