]> granicus.if.org Git - postgresql/blob - contrib/pgcrypto/pgp-info.c
More pgcrypto fixes: avoid bogus alignment assumptions in sha2,
[postgresql] / contrib / pgcrypto / pgp-info.c
1 /*
2  * pgp-info.c
3  *        Provide info about PGP data.
4  *
5  * Copyright (c) 2005 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $PostgreSQL: pgsql/contrib/pgcrypto/pgp-info.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
30  */
31 #include "postgres.h"
32
33 #include "px.h"
34 #include "mbuf.h"
35 #include "pgp.h"
36
37 static int read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
38 {
39         int res = 0;
40         PGP_PubKey *pk;
41
42         res = pgp_key_alloc(&pk);
43         if (res < 0)
44                 return res;
45
46         res = _pgp_read_public_key(pkt, pk);
47         if (res < 0)
48                 goto err;
49         res = pgp_skip_packet(pkt);
50         if (res < 0)
51                 goto err;
52
53         res = 0;
54         if (pk->algo == PGP_PUB_ELG_ENCRYPT)
55         {
56                 memcpy(keyid_buf, pk->key_id, 8);
57                 res = 1;
58         }
59 err:
60         pgp_key_free(pk);
61         return res;
62 }
63
64 static int read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
65 {
66         uint8 ver;
67         int res;
68
69         GETBYTE(pkt, ver);
70         if (ver != 3)
71                 return -1;
72
73         res = pullf_read_fixed(pkt, 8, keyid_buf);
74         if (res < 0)
75                 return res;
76
77         return pgp_skip_packet(pkt);
78 }
79
80 static const char hextbl[] = "0123456789ABCDEF";
81
82 static int
83 print_key(uint8 *keyid, char *dst)
84 {
85         int i;
86         unsigned c;
87         for (i = 0; i < 8; i++) {
88                 c = keyid[i];
89                 *dst++ = hextbl[(c >> 4) & 0x0F];
90                 *dst++ = hextbl[c & 0x0F];
91         }
92         *dst = 0;
93         return 8*2;
94 }
95
96 static const uint8 any_key[] = 
97 { 0, 0, 0, 0,  0, 0, 0, 0 };
98
99 /*
100  * dst should have room for 17 bytes
101  */
102 int
103 pgp_get_keyid(MBuf *pgp_data, char *dst)
104 {
105         int res;
106         PullFilter *src;
107         PullFilter *pkt = NULL;
108         int len;
109         uint8 tag;
110         int got_pub_key=0, got_symenc_key=0, got_pubenc_key=0;
111         int got_data=0;
112         uint8 keyid_buf[8];
113
114
115         res = pullf_create_mbuf_reader(&src, pgp_data);
116         if (res < 0)
117                 return res;
118
119         while (1) {
120                 res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
121                 if (res <= 0)
122                         break;                          
123                 res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
124                 if (res < 0)
125                         break;
126
127                 switch (tag)
128                 {
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);
134                                 if (res < 0)
135                                         break;
136                                 if (res > 0)
137                                         got_pub_key++;
138                                 break;
139                         case PGP_PKT_PUBENCRYPTED_SESSKEY:
140                                 got_pubenc_key++;
141                                 res = read_pubenc_keyid(pkt, keyid_buf);
142                                 break;
143                         case PGP_PKT_SYMENCRYPTED_DATA:
144                         case PGP_PKT_SYMENCRYPTED_DATA_MDC:
145                                 got_data = 1;
146                                 break;
147                         case PGP_PKT_SYMENCRYPTED_SESSKEY:
148                                 got_symenc_key++;
149                                 /* fallthru */
150                         case PGP_PKT_SIGNATURE:
151                         case PGP_PKT_MARKER:
152                         case PGP_PKT_TRUST:
153                         case PGP_PKT_USER_ID:
154                         case PGP_PKT_USER_ATTR:
155                         case PGP_PKT_PRIV_61:
156                                 res = pgp_skip_packet(pkt);
157                                 break;
158                         default:
159                                 res = PXE_PGP_CORRUPT_DATA;
160                 }
161
162                 if (pkt)
163                         pullf_free(pkt);
164                 pkt = NULL;
165
166                 if (res < 0 || got_data)
167                         break;
168         }
169
170         pullf_free(src);
171         if (pkt)
172                 pullf_free(pkt);
173
174         if (res < 0)
175                 return res;
176
177         /* now check sanity */
178         if (got_pub_key && got_pubenc_key)
179                 res = PXE_PGP_CORRUPT_DATA;
180
181         if (got_pub_key > 1)
182                 res = -1;
183
184         if (got_pubenc_key > 1)
185                 res = -1;
186
187         /*
188          * if still ok, look what we got
189          */
190         if (res >= 0)
191         {
192                 if (got_pubenc_key || got_pub_key)
193                 {
194                         if (memcmp(keyid_buf, any_key, 8) == 0)
195                         {
196                                 memcpy(dst, "ANYKEY", 7);
197                                 res = 6;
198                         }
199                         else
200                                 res = print_key(keyid_buf, dst);
201                 }
202                 else if (got_symenc_key)
203                 {
204                         memcpy(dst, "SYMKEY", 7);
205                         res = 6;
206                 }
207                 else
208                         res = PXE_PGP_NO_USABLE_KEY;
209         }
210
211         return res;
212 }
213