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 * $PostgreSQL: pgsql/contrib/pgcrypto/pgp-info.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
37 static int read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
42 res = pgp_key_alloc(&pk);
46 res = _pgp_read_public_key(pkt, pk);
49 res = pgp_skip_packet(pkt);
54 if (pk->algo == PGP_PUB_ELG_ENCRYPT)
56 memcpy(keyid_buf, pk->key_id, 8);
64 static int read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
73 res = pullf_read_fixed(pkt, 8, keyid_buf);
77 return pgp_skip_packet(pkt);
80 static const char hextbl[] = "0123456789ABCDEF";
83 print_key(uint8 *keyid, char *dst)
87 for (i = 0; i < 8; i++) {
89 *dst++ = hextbl[(c >> 4) & 0x0F];
90 *dst++ = hextbl[c & 0x0F];
96 static const uint8 any_key[] =
97 { 0, 0, 0, 0, 0, 0, 0, 0 };
100 * dst should have room for 17 bytes
103 pgp_get_keyid(MBuf *pgp_data, char *dst)
107 PullFilter *pkt = NULL;
110 int got_pub_key=0, got_symenc_key=0, got_pubenc_key=0;
115 res = pullf_create_mbuf_reader(&src, pgp_data);
120 res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
123 res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
129 case PGP_PKT_SECRET_KEY:
130 case PGP_PKT_PUBLIC_KEY:
131 case PGP_PKT_SECRET_SUBKEY:
132 case PGP_PKT_PUBLIC_SUBKEY:
133 res = read_pubkey_keyid(pkt, keyid_buf);
139 case PGP_PKT_PUBENCRYPTED_SESSKEY:
141 res = read_pubenc_keyid(pkt, keyid_buf);
143 case PGP_PKT_SYMENCRYPTED_DATA:
144 case PGP_PKT_SYMENCRYPTED_DATA_MDC:
147 case PGP_PKT_SYMENCRYPTED_SESSKEY:
150 case PGP_PKT_SIGNATURE:
153 case PGP_PKT_USER_ID:
154 case PGP_PKT_USER_ATTR:
155 case PGP_PKT_PRIV_61:
156 res = pgp_skip_packet(pkt);
159 res = PXE_PGP_CORRUPT_DATA;
166 if (res < 0 || got_data)
177 /* now check sanity */
178 if (got_pub_key && got_pubenc_key)
179 res = PXE_PGP_CORRUPT_DATA;
184 if (got_pubenc_key > 1)
188 * if still ok, look what we got
192 if (got_pubenc_key || got_pub_key)
194 if (memcmp(keyid_buf, any_key, 8) == 0)
196 memcpy(dst, "ANYKEY", 7);
200 res = print_key(keyid_buf, dst);
202 else if (got_symenc_key)
204 memcpy(dst, "SYMKEY", 7);
208 res = PXE_PGP_NO_USABLE_KEY;