PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgp-info.c File Reference
#include "postgres.h"
#include "px.h"
#include "mbuf.h"
#include "pgp.h"
Include dependency graph for pgp-info.c:

Go to the source code of this file.

Functions

static int read_pubkey_keyid (PullFilter *pkt, uint8 *keyid_buf)
 
static int read_pubenc_keyid (PullFilter *pkt, uint8 *keyid_buf)
 
static int print_key (uint8 *keyid, char *dst)
 
int pgp_get_keyid (MBuf *pgp_data, char *dst)
 

Variables

static const char hextbl [] = "0123456789ABCDEF"
 
static const uint8 any_key []
 

Function Documentation

int pgp_get_keyid ( MBuf pgp_data,
char *  dst 
)

Definition at line 112 of file pgp-info.c.

References any_key, NULL, pgp_create_pkt_reader(), pgp_parse_pkt_hdr(), PGP_PKT_MARKER, PGP_PKT_PRIV_61, PGP_PKT_PUBENCRYPTED_SESSKEY, PGP_PKT_PUBLIC_KEY, PGP_PKT_PUBLIC_SUBKEY, PGP_PKT_SECRET_KEY, PGP_PKT_SECRET_SUBKEY, PGP_PKT_SIGNATURE, PGP_PKT_SYMENCRYPTED_DATA, PGP_PKT_SYMENCRYPTED_DATA_MDC, PGP_PKT_SYMENCRYPTED_SESSKEY, PGP_PKT_TRUST, PGP_PKT_USER_ATTR, PGP_PKT_USER_ID, pgp_skip_packet(), print_key(), pullf_create_mbuf_reader(), pullf_free(), PXE_PGP_CORRUPT_DATA, PXE_PGP_MULTIPLE_KEYS, PXE_PGP_NO_USABLE_KEY, read_pubenc_keyid(), and read_pubkey_keyid().

Referenced by pgp_key_id_w().

113 {
114  int res;
115  PullFilter *src;
116  PullFilter *pkt = NULL;
117  int len;
118  uint8 tag;
119  int got_pub_key = 0,
120  got_symenc_key = 0,
121  got_pubenc_key = 0;
122  int got_data = 0;
123  uint8 keyid_buf[8];
124  int got_main_key = 0;
125 
126 
127  res = pullf_create_mbuf_reader(&src, pgp_data);
128  if (res < 0)
129  return res;
130 
131  while (1)
132  {
133  res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
134  if (res <= 0)
135  break;
136  res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
137  if (res < 0)
138  break;
139 
140  switch (tag)
141  {
142  case PGP_PKT_SECRET_KEY:
143  case PGP_PKT_PUBLIC_KEY:
144  /* main key is for signing, so ignore it */
145  if (!got_main_key)
146  {
147  got_main_key = 1;
148  res = pgp_skip_packet(pkt);
149  }
150  else
151  res = PXE_PGP_MULTIPLE_KEYS;
152  break;
155  res = read_pubkey_keyid(pkt, keyid_buf);
156  if (res < 0)
157  break;
158  if (res > 0)
159  got_pub_key++;
160  break;
162  got_pubenc_key++;
163  res = read_pubenc_keyid(pkt, keyid_buf);
164  break;
167  /* don't skip it, just stop */
168  got_data = 1;
169  break;
171  got_symenc_key++;
172  /* fallthru */
173  case PGP_PKT_SIGNATURE:
174  case PGP_PKT_MARKER:
175  case PGP_PKT_TRUST:
176  case PGP_PKT_USER_ID:
177  case PGP_PKT_USER_ATTR:
178  case PGP_PKT_PRIV_61:
179  res = pgp_skip_packet(pkt);
180  break;
181  default:
182  res = PXE_PGP_CORRUPT_DATA;
183  }
184 
185  if (pkt)
186  pullf_free(pkt);
187  pkt = NULL;
188 
189  if (res < 0 || got_data)
190  break;
191  }
192 
193  pullf_free(src);
194  if (pkt)
195  pullf_free(pkt);
196 
197  if (res < 0)
198  return res;
199 
200  /* now check sanity */
201  if (got_pub_key && got_pubenc_key)
202  res = PXE_PGP_CORRUPT_DATA;
203 
204  if (got_pub_key > 1)
205  res = PXE_PGP_MULTIPLE_KEYS;
206 
207  if (got_pubenc_key > 1)
208  res = PXE_PGP_MULTIPLE_KEYS;
209 
210  /*
211  * if still ok, look what we got
212  */
213  if (res >= 0)
214  {
215  if (got_pubenc_key || got_pub_key)
216  {
217  if (memcmp(keyid_buf, any_key, 8) == 0)
218  {
219  memcpy(dst, "ANYKEY", 7);
220  res = 6;
221  }
222  else
223  res = print_key(keyid_buf, dst);
224  }
225  else if (got_symenc_key)
226  {
227  memcpy(dst, "SYMKEY", 7);
228  res = 6;
229  }
230  else
231  res = PXE_PGP_NO_USABLE_KEY;
232  }
233 
234  return res;
235 }
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:81
static int read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
Definition: pgp-info.c:71
void pullf_free(PullFilter *pf)
Definition: mbuf.c:245
int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p, int allow_ctx)
Definition: pgp-decrypt.c:129
unsigned char uint8
Definition: c.h:266
int pullf_create_mbuf_reader(PullFilter **mp_p, MBuf *src)
Definition: mbuf.c:352
static int print_key(uint8 *keyid, char *dst)
Definition: pgp-info.c:90
int pgp_skip_packet(PullFilter *pkt)
Definition: pgp-decrypt.c:1061
#define PXE_PGP_NO_USABLE_KEY
Definition: px.h:100
static int read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
Definition: pgp-info.c:38
int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len, int pkttype, PGP_Context *ctx)
Definition: pgp-decrypt.c:223
#define PXE_PGP_MULTIPLE_KEYS
Definition: px.h:95
#define NULL
Definition: c.h:229
static const uint8 any_key[]
Definition: pgp-info.c:105
static int print_key ( uint8 keyid,
char *  dst 
)
static

Definition at line 90 of file pgp-info.c.

References hextbl, and i.

Referenced by pgp_get_keyid().

91 {
92  int i;
93  unsigned c;
94 
95  for (i = 0; i < 8; i++)
96  {
97  c = keyid[i];
98  *dst++ = hextbl[(c >> 4) & 0x0F];
99  *dst++ = hextbl[c & 0x0F];
100  }
101  *dst = 0;
102  return 8 * 2;
103 }
char * c
static const char hextbl[]
Definition: pgp-info.c:87
int i
static int read_pubenc_keyid ( PullFilter pkt,
uint8 keyid_buf 
)
static

Definition at line 71 of file pgp-info.c.

References GETBYTE, pgp_skip_packet(), and pullf_read_fixed().

Referenced by pgp_get_keyid().

72 {
73  uint8 ver;
74  int res;
75 
76  GETBYTE(pkt, ver);
77  if (ver != 3)
78  return -1;
79 
80  res = pullf_read_fixed(pkt, 8, keyid_buf);
81  if (res < 0)
82  return res;
83 
84  return pgp_skip_packet(pkt);
85 }
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:317
unsigned char uint8
Definition: c.h:266
int pgp_skip_packet(PullFilter *pkt)
Definition: pgp-decrypt.c:1061
#define GETBYTE(x, i)
Definition: hstore_gist.c:32
static int read_pubkey_keyid ( PullFilter pkt,
uint8 keyid_buf 
)
static

Definition at line 38 of file pgp-info.c.

References _pgp_read_public_key(), PGP_PubKey::algo, PGP_PubKey::key_id, NULL, pgp_key_free(), PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, and pgp_skip_packet().

Referenced by pgp_get_keyid().

39 {
40  int res;
41  PGP_PubKey *pk = NULL;
42 
43  res = _pgp_read_public_key(pkt, &pk);
44  if (res < 0)
45  goto err;
46 
47  /* skip secret key part, if it exists */
48  res = pgp_skip_packet(pkt);
49  if (res < 0)
50  goto err;
51 
52  /* is it encryption key */
53  switch (pk->algo)
54  {
58  memcpy(keyid_buf, pk->key_id, 8);
59  res = 1;
60  break;
61  default:
62  res = 0;
63  }
64 
65 err:
66  pgp_key_free(pk);
67  return res;
68 }
int pgp_skip_packet(PullFilter *pkt)
Definition: pgp-decrypt.c:1061
void pgp_key_free(PGP_PubKey *pk)
Definition: pgp-pubkey.c:49
#define NULL
Definition: c.h:229
uint8 key_id[8]
Definition: pgp.h:235
uint8 algo
Definition: pgp.h:190
int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
Definition: pgp-pubkey.c:159

Variable Documentation

const uint8 any_key[]
static
Initial value:
=
{0, 0, 0, 0, 0, 0, 0, 0}

Definition at line 105 of file pgp-info.c.

Referenced by pgp_get_keyid().

const char hextbl[] = "0123456789ABCDEF"
static

Definition at line 87 of file pgp-info.c.

Referenced by appendByteaLiteral(), and print_key().