PostgreSQL Source Code  git master
pgp-armor.c File Reference
#include "postgres.h"
#include "pgp.h"
#include "px.h"
Include dependency graph for pgp-armor.c:

Go to the source code of this file.

Macros

#define CRC24_INIT   0x00b704ceL
 
#define CRC24_POLY   0x01864cfbL
 

Functions

static int pg_base64_encode (const uint8 *src, unsigned len, uint8 *dst)
 
static int pg_base64_decode (const uint8 *src, unsigned len, uint8 *dst)
 
static unsigned pg_base64_enc_len (unsigned srclen)
 
static unsigned pg_base64_dec_len (unsigned srclen)
 
static long crc24 (const uint8 *data, unsigned len)
 
void pgp_armor_encode (const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)
 
static const uint8find_str (const uint8 *data, const uint8 *data_end, const char *str, int strlen)
 
static int find_header (const uint8 *data, const uint8 *datend, const uint8 **start_p, int is_end)
 
int pgp_armor_decode (const uint8 *src, int len, StringInfo dst)
 
int pgp_extract_armor_headers (const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
 

Variables

static const unsigned char _base64 []
 
static const char *const armor_header = "-----BEGIN PGP MESSAGE-----\n"
 
static const char *const armor_footer = "\n-----END PGP MESSAGE-----\n"
 

Macro Definition Documentation

◆ CRC24_INIT

#define CRC24_INIT   0x00b704ceL

Definition at line 185 of file pgp-armor.c.

◆ CRC24_POLY

#define CRC24_POLY   0x01864cfbL

Definition at line 186 of file pgp-armor.c.

Function Documentation

◆ crc24()

static long crc24 ( const uint8 data,
unsigned  len 
)
static

Definition at line 188 of file pgp-armor.c.

189 {
190  unsigned crc = CRC24_INIT;
191  int i;
192 
193  while (len--)
194  {
195  crc ^= (*data++) << 16;
196  for (i = 0; i < 8; i++)
197  {
198  crc <<= 1;
199  if (crc & 0x1000000)
200  crc ^= CRC24_POLY;
201  }
202  }
203  return crc & 0xffffffL;
204 }
int i
Definition: isn.c:73
const void size_t len
const void * data
return crc
#define CRC24_POLY
Definition: pgp-armor.c:186
#define CRC24_INIT
Definition: pgp-armor.c:185

References crc, CRC24_INIT, CRC24_POLY, data, i, and len.

Referenced by pgp_armor_decode(), and pgp_armor_encode().

◆ find_header()

static int find_header ( const uint8 data,
const uint8 datend,
const uint8 **  start_p,
int  is_end 
)
static

Definition at line 266 of file pgp-armor.c.

268 {
269  const uint8 *p = data;
270  static const char *start_sep = "-----BEGIN";
271  static const char *end_sep = "-----END";
272  const char *sep = is_end ? end_sep : start_sep;
273 
274  /* find header line */
275  while (1)
276  {
277  p = find_str(p, datend, sep, strlen(sep));
278  if (p == NULL)
279  return PXE_PGP_CORRUPT_ARMOR;
280  /* it must start at beginning of line */
281  if (p == data || *(p - 1) == '\n')
282  break;
283  p += strlen(sep);
284  }
285  *start_p = p;
286  p += strlen(sep);
287 
288  /* check if header text ok */
289  for (; p < datend && *p != '-'; p++)
290  {
291  /* various junk can be there, but definitely not line-feed */
292  if (*p >= ' ')
293  continue;
294  return PXE_PGP_CORRUPT_ARMOR;
295  }
296  if (datend - p < 5 || memcmp(p, sep, 5) != 0)
297  return PXE_PGP_CORRUPT_ARMOR;
298  p += 5;
299 
300  /* check if at end of line */
301  if (p < datend)
302  {
303  if (*p != '\n' && *p != '\r')
304  return PXE_PGP_CORRUPT_ARMOR;
305  if (*p == '\r')
306  p++;
307  if (p < datend && *p == '\n')
308  p++;
309  }
310  return p - *start_p;
311 }
unsigned char uint8
Definition: c.h:504
static const uint8 * find_str(const uint8 *data, const uint8 *data_end, const char *str, int strlen)
Definition: pgp-armor.c:243
#define PXE_PGP_CORRUPT_ARMOR
Definition: px.h:68

References data, find_str(), and PXE_PGP_CORRUPT_ARMOR.

Referenced by pgp_armor_decode(), and pgp_extract_armor_headers().

◆ find_str()

static const uint8* find_str ( const uint8 data,
const uint8 data_end,
const char *  str,
int  strlen 
)
static

Definition at line 243 of file pgp-armor.c.

244 {
245  const uint8 *p = data;
246 
247  if (!strlen)
248  return NULL;
249  if (data_end - data < strlen)
250  return NULL;
251  while (p < data_end)
252  {
253  p = memchr(p, str[0], data_end - p);
254  if (p == NULL)
255  return NULL;
256  if (p + strlen > data_end)
257  return NULL;
258  if (memcmp(p, str, strlen) == 0)
259  return p;
260  p++;
261  }
262  return NULL;
263 }
const char * str

References data, and str.

Referenced by find_header().

◆ pg_base64_dec_len()

static unsigned pg_base64_dec_len ( unsigned  srclen)
static

Definition at line 172 of file pgp-armor.c.

173 {
174  return (srclen * 3) >> 2;
175 }

Referenced by pgp_armor_decode().

◆ pg_base64_decode()

static int pg_base64_decode ( const uint8 src,
unsigned  len,
uint8 dst 
)
static

Definition at line 95 of file pgp-armor.c.

96 {
97  const uint8 *srcend = src + len,
98  *s = src;
99  uint8 *p = dst;
100  char c;
101  unsigned b = 0;
102  unsigned long buf = 0;
103  int pos = 0,
104  end = 0;
105 
106  while (s < srcend)
107  {
108  c = *s++;
109  if (c >= 'A' && c <= 'Z')
110  b = c - 'A';
111  else if (c >= 'a' && c <= 'z')
112  b = c - 'a' + 26;
113  else if (c >= '0' && c <= '9')
114  b = c - '0' + 52;
115  else if (c == '+')
116  b = 62;
117  else if (c == '/')
118  b = 63;
119  else if (c == '=')
120  {
121  /*
122  * end sequence
123  */
124  if (!end)
125  {
126  if (pos == 2)
127  end = 1;
128  else if (pos == 3)
129  end = 2;
130  else
131  return PXE_PGP_CORRUPT_ARMOR;
132  }
133  b = 0;
134  }
135  else if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
136  continue;
137  else
138  return PXE_PGP_CORRUPT_ARMOR;
139 
140  /*
141  * add it to buffer
142  */
143  buf = (buf << 6) + b;
144  pos++;
145  if (pos == 4)
146  {
147  *p++ = (buf >> 16) & 255;
148  if (end == 0 || end > 1)
149  *p++ = (buf >> 8) & 255;
150  if (end == 0 || end > 2)
151  *p++ = buf & 255;
152  buf = 0;
153  pos = 0;
154  }
155  }
156 
157  if (pos != 0)
158  return PXE_PGP_CORRUPT_ARMOR;
159  return p - dst;
160 }
int b
Definition: isn.c:70
static char * buf
Definition: pg_test_fsync.c:73
char * c

References b, buf, len, and PXE_PGP_CORRUPT_ARMOR.

Referenced by pgp_armor_decode().

◆ pg_base64_enc_len()

static unsigned pg_base64_enc_len ( unsigned  srclen)
static

Definition at line 163 of file pgp-armor.c.

164 {
165  /*
166  * 3 bytes will be converted to 4, linefeed after 76 chars
167  */
168  return (srclen + 2) / 3 * 4 + srclen / (76 * 3 / 4);
169 }

Referenced by pgp_armor_encode().

◆ pg_base64_encode()

static int pg_base64_encode ( const uint8 src,
unsigned  len,
uint8 dst 
)
static

Definition at line 45 of file pgp-armor.c.

46 {
47  uint8 *p,
48  *lend = dst + 76;
49  const uint8 *s,
50  *end = src + len;
51  int pos = 2;
52  unsigned long buf = 0;
53 
54  s = src;
55  p = dst;
56 
57  while (s < end)
58  {
59  buf |= *s << (pos << 3);
60  pos--;
61  s++;
62 
63  /*
64  * write it out
65  */
66  if (pos < 0)
67  {
68  *p++ = _base64[(buf >> 18) & 0x3f];
69  *p++ = _base64[(buf >> 12) & 0x3f];
70  *p++ = _base64[(buf >> 6) & 0x3f];
71  *p++ = _base64[buf & 0x3f];
72 
73  pos = 2;
74  buf = 0;
75  }
76  if (p >= lend)
77  {
78  *p++ = '\n';
79  lend = p + 76;
80  }
81  }
82  if (pos != 2)
83  {
84  *p++ = _base64[(buf >> 18) & 0x3f];
85  *p++ = _base64[(buf >> 12) & 0x3f];
86  *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
87  *p++ = '=';
88  }
89 
90  return p - dst;
91 }
static const unsigned char _base64[]
Definition: pgp-armor.c:41

References _base64, buf, and len.

Referenced by pgp_armor_encode().

◆ pgp_armor_decode()

int pgp_armor_decode ( const uint8 src,
int  len,
StringInfo  dst 
)

Definition at line 314 of file pgp-armor.c.

315 {
316  const uint8 *p = src;
317  const uint8 *data_end = src + len;
318  long crc;
319  const uint8 *base64_start,
320  *armor_end;
321  const uint8 *base64_end = NULL;
322  uint8 buf[4];
323  int hlen;
324  int blen;
326 
327  /* armor start */
328  hlen = find_header(src, data_end, &p, 0);
329  if (hlen <= 0)
330  goto out;
331  p += hlen;
332 
333  /* armor end */
334  hlen = find_header(p, data_end, &armor_end, 1);
335  if (hlen <= 0)
336  goto out;
337 
338  /* skip comments - find empty line */
339  while (p < armor_end && *p != '\n' && *p != '\r')
340  {
341  p = memchr(p, '\n', armor_end - p);
342  if (!p)
343  goto out;
344 
345  /* step to start of next line */
346  p++;
347  }
348  base64_start = p;
349 
350  /* find crc pos */
351  for (p = armor_end; p >= base64_start; p--)
352  if (*p == '=')
353  {
354  base64_end = p - 1;
355  break;
356  }
357  if (base64_end == NULL)
358  goto out;
359 
360  /* decode crc */
361  if (pg_base64_decode(p + 1, 4, buf) != 3)
362  goto out;
363  crc = (((long) buf[0]) << 16) + (((long) buf[1]) << 8) + (long) buf[2];
364 
365  /* decode data */
366  blen = (int) pg_base64_dec_len(len);
367  enlargeStringInfo(dst, blen);
368  res = pg_base64_decode(base64_start, base64_end - base64_start, (uint8 *) dst->data);
369  if (res > blen)
370  elog(FATAL, "overflow - decode estimate too small");
371  if (res >= 0)
372  {
373  if (crc24((uint8 *) dst->data, res) == crc)
374  dst->len += res;
375  else
377  }
378 out:
379  return res;
380 }
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:224
static long crc24(const uint8 *data, unsigned len)
Definition: pgp-armor.c:188
static int find_header(const uint8 *data, const uint8 *datend, const uint8 **start_p, int is_end)
Definition: pgp-armor.c:266
static int pg_base64_decode(const uint8 *src, unsigned len, uint8 *dst)
Definition: pgp-armor.c:95
static unsigned pg_base64_dec_len(unsigned srclen)
Definition: pgp-armor.c:172
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:289

References buf, crc, crc24(), StringInfoData::data, elog, enlargeStringInfo(), FATAL, find_header(), StringInfoData::len, len, pg_base64_dec_len(), pg_base64_decode(), PXE_PGP_CORRUPT_ARMOR, and res.

Referenced by pg_dearmor().

◆ pgp_armor_encode()

void pgp_armor_encode ( const uint8 src,
unsigned  len,
StringInfo  dst,
int  num_headers,
char **  keys,
char **  values 
)

Definition at line 207 of file pgp-armor.c.

209 {
210  int n;
211  int res;
212  unsigned b64len;
213  unsigned crc = crc24(src, len);
214 
216 
217  for (n = 0; n < num_headers; n++)
218  appendStringInfo(dst, "%s: %s\n", keys[n], values[n]);
219  appendStringInfoChar(dst, '\n');
220 
221  /* make sure we have enough room to pg_base64_encode() */
222  b64len = pg_base64_enc_len(len);
223  enlargeStringInfo(dst, (int) b64len);
224 
225  res = pg_base64_encode(src, len, (uint8 *) dst->data + dst->len);
226  if (res > b64len)
227  elog(FATAL, "overflow - encode estimate too small");
228  dst->len += res;
229 
230  if (*(dst->data + dst->len - 1) != '\n')
231  appendStringInfoChar(dst, '\n');
232 
233  appendStringInfoChar(dst, '=');
234  appendStringInfoChar(dst, _base64[(crc >> 18) & 0x3f]);
235  appendStringInfoChar(dst, _base64[(crc >> 12) & 0x3f]);
236  appendStringInfoChar(dst, _base64[(crc >> 6) & 0x3f]);
237  appendStringInfoChar(dst, _base64[crc & 0x3f]);
238 
240 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
static const char *const armor_footer
Definition: pgp-armor.c:182
static unsigned pg_base64_enc_len(unsigned srclen)
Definition: pgp-armor.c:163
static const char *const armor_header
Definition: pgp-armor.c:181
static int pg_base64_encode(const uint8 *src, unsigned len, uint8 *dst)
Definition: pgp-armor.c:45
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194

References _base64, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), armor_footer, armor_header, crc, crc24(), StringInfoData::data, elog, enlargeStringInfo(), FATAL, StringInfoData::len, len, pg_base64_enc_len(), pg_base64_encode(), res, and values.

Referenced by pg_armor().

◆ pgp_extract_armor_headers()

int pgp_extract_armor_headers ( const uint8 src,
unsigned  len,
int *  nheaders,
char ***  keys,
char ***  values 
)

Definition at line 390 of file pgp-armor.c.

392 {
393  const uint8 *data_end = src + len;
394  const uint8 *p;
395  const uint8 *base64_start;
396  const uint8 *armor_start;
397  const uint8 *armor_end;
398  Size armor_len;
399  char *line;
400  char *nextline;
401  char *eol,
402  *colon;
403  int hlen;
404  char *buf;
405  int hdrlines;
406  int n;
407 
408  /* armor start */
409  hlen = find_header(src, data_end, &armor_start, 0);
410  if (hlen <= 0)
411  return PXE_PGP_CORRUPT_ARMOR;
412  armor_start += hlen;
413 
414  /* armor end */
415  hlen = find_header(armor_start, data_end, &armor_end, 1);
416  if (hlen <= 0)
417  return PXE_PGP_CORRUPT_ARMOR;
418 
419  /* Count the number of armor header lines. */
420  hdrlines = 0;
421  p = armor_start;
422  while (p < armor_end && *p != '\n' && *p != '\r')
423  {
424  p = memchr(p, '\n', armor_end - p);
425  if (!p)
426  return PXE_PGP_CORRUPT_ARMOR;
427 
428  /* step to start of next line */
429  p++;
430  hdrlines++;
431  }
432  base64_start = p;
433 
434  /*
435  * Make a modifiable copy of the part of the input that contains the
436  * headers. The returned key/value pointers will point inside the buffer.
437  */
438  armor_len = base64_start - armor_start;
439  buf = palloc(armor_len + 1);
440  memcpy(buf, armor_start, armor_len);
441  buf[armor_len] = '\0';
442 
443  /* Allocate return arrays */
444  *keys = (char **) palloc(hdrlines * sizeof(char *));
445  *values = (char **) palloc(hdrlines * sizeof(char *));
446 
447  /*
448  * Split the header lines at newlines and ": " separators, and collect
449  * pointers to the keys and values in the return arrays.
450  */
451  n = 0;
452  line = buf;
453  for (;;)
454  {
455  /* find end of line */
456  eol = strchr(line, '\n');
457  if (!eol)
458  break;
459  nextline = eol + 1;
460  /* if the line ends in CR + LF, strip the CR */
461  if (eol > line && *(eol - 1) == '\r')
462  eol--;
463  *eol = '\0';
464 
465  /* find colon+space separating the key and value */
466  colon = strstr(line, ": ");
467  if (!colon)
468  return PXE_PGP_CORRUPT_ARMOR;
469  *colon = '\0';
470 
471  /* shouldn't happen, we counted the number of lines beforehand */
472  if (n >= hdrlines)
473  elog(ERROR, "unexpected number of armor header lines");
474 
475  (*keys)[n] = line;
476  (*values)[n] = colon + 2;
477  n++;
478 
479  /* step to start of next line */
480  line = nextline;
481  }
482 
483  if (n != hdrlines)
484  elog(ERROR, "unexpected number of armor header lines");
485 
486  *nheaders = n;
487  return 0;
488 }
size_t Size
Definition: c.h:605
#define ERROR
Definition: elog.h:39
#define colon
Definition: indent_codes.h:43
void * palloc(Size size)
Definition: mcxt.c:1316

References buf, colon, elog, ERROR, find_header(), len, palloc(), PXE_PGP_CORRUPT_ARMOR, and values.

Referenced by pgp_armor_headers().

Variable Documentation

◆ _base64

const unsigned char _base64[]
static
Initial value:
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 41 of file pgp-armor.c.

Referenced by pg_base64_encode(), and pgp_armor_encode().

◆ armor_footer

const char* const armor_footer = "\n-----END PGP MESSAGE-----\n"
static

Definition at line 182 of file pgp-armor.c.

Referenced by pgp_armor_encode().

◆ armor_header

const char* const armor_header = "-----BEGIN PGP MESSAGE-----\n"
static

Definition at line 181 of file pgp-armor.c.

Referenced by pgp_armor_encode().