PostgreSQL Source Code  git master
pgp-pgsql.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "common/string.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "mbuf.h"
#include "pgp.h"
#include "px.h"
#include "utils/array.h"
#include "utils/builtins.h"
Include dependency graph for pgp-pgsql.c:

Go to the source code of this file.

Data Structures

struct  debug_expect
 
struct  pgp_armor_headers_state
 

Macros

#define EX_MSG(arg)
 
#define EX_CHECK(arg)
 

Functions

 PG_FUNCTION_INFO_V1 (pgp_sym_encrypt_bytea)
 
 PG_FUNCTION_INFO_V1 (pgp_sym_encrypt_text)
 
 PG_FUNCTION_INFO_V1 (pgp_sym_decrypt_bytea)
 
 PG_FUNCTION_INFO_V1 (pgp_sym_decrypt_text)
 
 PG_FUNCTION_INFO_V1 (pgp_pub_encrypt_bytea)
 
 PG_FUNCTION_INFO_V1 (pgp_pub_encrypt_text)
 
 PG_FUNCTION_INFO_V1 (pgp_pub_decrypt_bytea)
 
 PG_FUNCTION_INFO_V1 (pgp_pub_decrypt_text)
 
 PG_FUNCTION_INFO_V1 (pgp_key_id_w)
 
 PG_FUNCTION_INFO_V1 (pg_armor)
 
 PG_FUNCTION_INFO_V1 (pg_dearmor)
 
 PG_FUNCTION_INFO_V1 (pgp_armor_headers)
 
static textconvert_charset (text *src, int cset_from, int cset_to)
 
static textconvert_from_utf8 (text *src)
 
static textconvert_to_utf8 (text *src)
 
static void clear_and_pfree (text *p)
 
static void fill_expect (struct debug_expect *ex, int text_mode)
 
static void check_expect (PGP_Context *ctx, struct debug_expect *ex)
 
static void show_debug (const char *msg)
 
static int set_arg (PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
 
static char * getword (char *p, char **res_p, int *res_len)
 
static char * downcase_convert (const uint8 *s, int len)
 
static int parse_args (PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
 
static MBufcreate_mbuf_from_vardata (text *data)
 
static void init_work (PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
 
static byteaencrypt_internal (int is_pubenc, int is_text, text *data, text *key, text *args)
 
static byteadecrypt_internal (int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
 
Datum pgp_sym_encrypt_bytea (PG_FUNCTION_ARGS)
 
Datum pgp_sym_encrypt_text (PG_FUNCTION_ARGS)
 
Datum pgp_sym_decrypt_bytea (PG_FUNCTION_ARGS)
 
Datum pgp_sym_decrypt_text (PG_FUNCTION_ARGS)
 
Datum pgp_pub_encrypt_bytea (PG_FUNCTION_ARGS)
 
Datum pgp_pub_encrypt_text (PG_FUNCTION_ARGS)
 
Datum pgp_pub_decrypt_bytea (PG_FUNCTION_ARGS)
 
Datum pgp_pub_decrypt_text (PG_FUNCTION_ARGS)
 
static int parse_key_value_arrays (ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
 
Datum pg_armor (PG_FUNCTION_ARGS)
 
Datum pg_dearmor (PG_FUNCTION_ARGS)
 
Datum pgp_armor_headers (PG_FUNCTION_ARGS)
 
Datum pgp_key_id_w (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ EX_CHECK

#define EX_CHECK (   arg)
Value:
do { \
if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
} while (0)
#define EX_MSG(arg)
Definition: pgp-pgsql.c:137
void * arg

Definition at line 142 of file pgp-pgsql.c.

Referenced by check_expect().

◆ EX_MSG

#define EX_MSG (   arg)
Value:
"pgp_decrypt: unexpected %s: expected %d got %d", \
CppAsString(arg), ex->arg, ctx->arg)))
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define CppAsString(identifier)
Definition: c.h:288
void * arg

Definition at line 137 of file pgp-pgsql.c.

Function Documentation

◆ check_expect()

static void check_expect ( PGP_Context ctx,
struct debug_expect ex 
)
static

Definition at line 147 of file pgp-pgsql.c.

References debug_expect::cipher_algo, debug_expect::compress_algo, debug_expect::disable_mdc, EX_CHECK, debug_expect::s2k_cipher_algo, debug_expect::s2k_count, debug_expect::s2k_digest_algo, debug_expect::s2k_mode, debug_expect::unicode_mode, debug_expect::use_sess_key, and PGP_Context::use_sess_key.

Referenced by decrypt_internal().

148 {
149  EX_CHECK(cipher_algo);
150  EX_CHECK(s2k_mode);
151  EX_CHECK(s2k_count);
152  EX_CHECK(s2k_digest_algo);
153  EX_CHECK(use_sess_key);
154  if (ctx->use_sess_key)
155  EX_CHECK(s2k_cipher_algo);
156  EX_CHECK(disable_mdc);
157  EX_CHECK(compress_algo);
158  EX_CHECK(unicode_mode);
159 }
int use_sess_key
Definition: pgp.h:148
#define EX_CHECK(arg)
Definition: pgp-pgsql.c:142

◆ clear_and_pfree()

static void clear_and_pfree ( text p)
static

Definition at line 97 of file pgp-pgsql.c.

References pfree(), px_memset(), and VARSIZE_ANY.

Referenced by decrypt_internal(), and encrypt_internal().

98 {
99  px_memset(p, 0, VARSIZE_ANY(p));
100  pfree(p);
101 }
void pfree(void *pointer)
Definition: mcxt.c:1169
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ convert_charset()

static text* convert_charset ( text src,
int  cset_from,
int  cset_to 
)
static

Definition at line 68 of file pgp-pgsql.c.

References cstring_to_text(), pfree(), pg_do_encoding_conversion(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by convert_from_utf8(), and convert_to_utf8().

69 {
70  int src_len = VARSIZE_ANY_EXHDR(src);
71  unsigned char *dst;
72  unsigned char *csrc = (unsigned char *) VARDATA_ANY(src);
73  text *res;
74 
75  dst = pg_do_encoding_conversion(csrc, src_len, cset_from, cset_to);
76  if (dst == csrc)
77  return src;
78 
79  res = cstring_to_text((char *) dst);
80  pfree(dst);
81  return res;
82 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:356
void pfree(void *pointer)
Definition: mcxt.c:1169
text * cstring_to_text(const char *s)
Definition: varlena.c:190
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621

◆ convert_from_utf8()

static text* convert_from_utf8 ( text src)
static

Definition at line 85 of file pgp-pgsql.c.

References convert_charset(), GetDatabaseEncoding(), and PG_UTF8.

Referenced by decrypt_internal().

86 {
88 }
static text * convert_charset(text *src, int cset_from, int cset_to)
Definition: pgp-pgsql.c:68
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210

◆ convert_to_utf8()

static text* convert_to_utf8 ( text src)
static

Definition at line 91 of file pgp-pgsql.c.

References convert_charset(), GetDatabaseEncoding(), and PG_UTF8.

Referenced by encrypt_internal().

92 {
94 }
static text * convert_charset(text *src, int cset_from, int cset_to)
Definition: pgp-pgsql.c:68
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210

◆ create_mbuf_from_vardata()

static MBuf* create_mbuf_from_vardata ( text data)
static

Definition at line 344 of file pgp-pgsql.c.

References mbuf_create_from_data(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by decrypt_internal(), encrypt_internal(), and pgp_key_id_w().

345 {
346  return mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
347  VARSIZE_ANY_EXHDR(data));
348 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
unsigned char uint8
Definition: c.h:439
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:137

◆ decrypt_internal()

static bytea* decrypt_internal ( int  is_pubenc,
int  need_text,
text data,
text key,
text keypsw,
text args 
)
static

Definition at line 458 of file pgp-pgsql.c.

References check_expect(), clear_and_pfree(), convert_from_utf8(), create_mbuf_from_vardata(), debug_expect::expect, init_work(), mbuf_append(), mbuf_create(), mbuf_create_from_data(), mbuf_free(), mbuf_steal_data(), pgp_decrypt(), pgp_free(), pgp_get_unicode_mode(), pgp_set_pubkey(), pgp_set_symkey(), px_set_debug_handler(), px_THROW_ERROR(), SET_VARSIZE, VARDATA_ANY, VARHDRSZ, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by pgp_pub_decrypt_bytea(), pgp_pub_decrypt_text(), pgp_sym_decrypt_bytea(), and pgp_sym_decrypt_text().

460 {
461  int err;
462  MBuf *src = NULL,
463  *dst = NULL;
464  uint8 tmp[VARHDRSZ];
465  uint8 *restmp;
466  bytea *res;
467  int res_len;
468  PGP_Context *ctx = NULL;
469  struct debug_expect ex;
470  int got_unicode = 0;
471 
472 
473  init_work(&ctx, need_text, args, &ex);
474 
475  src = mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
476  VARSIZE_ANY_EXHDR(data));
477  dst = mbuf_create(VARSIZE_ANY(data) + 2048);
478 
479  /*
480  * reserve room for header
481  */
482  mbuf_append(dst, tmp, VARHDRSZ);
483 
484  /*
485  * set key
486  */
487  if (is_pubenc)
488  {
489  uint8 *psw = NULL;
490  int psw_len = 0;
491  MBuf *kbuf;
492 
493  if (keypsw)
494  {
495  psw = (uint8 *) VARDATA_ANY(keypsw);
496  psw_len = VARSIZE_ANY_EXHDR(keypsw);
497  }
498  kbuf = create_mbuf_from_vardata(key);
499  err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
500  mbuf_free(kbuf);
501  }
502  else
503  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
504  VARSIZE_ANY_EXHDR(key));
505 
506  /* decrypt */
507  if (err >= 0)
508  {
509  err = pgp_decrypt(ctx, src, dst);
510 
511  if (ex.expect)
512  check_expect(ctx, &ex);
513 
514  /* remember the setting */
515  got_unicode = pgp_get_unicode_mode(ctx);
516  }
517 
518  mbuf_free(src);
519  pgp_free(ctx);
520 
521  if (err)
522  {
523  px_set_debug_handler(NULL);
524  mbuf_free(dst);
525  px_THROW_ERROR(err);
526  }
527 
528  res_len = mbuf_steal_data(dst, &restmp);
529  mbuf_free(dst);
530 
531  /* res_len includes VARHDRSZ */
532  res = (bytea *) restmp;
533  SET_VARSIZE(res, res_len);
534 
535  if (need_text && got_unicode)
536  {
537  text *utf = convert_from_utf8(res);
538 
539  if (utf != res)
540  {
541  clear_and_pfree(res);
542  res = utf;
543  }
544  }
545  px_set_debug_handler(NULL);
546 
547  return res;
548 }
MBuf * mbuf_create(int len)
Definition: mbuf.c:117
void px_THROW_ERROR(int err)
Definition: px.c:97
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:350
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Definition: pgp-pubkey.c:565
static void clear_and_pfree(text *p)
Definition: pgp-pgsql.c:97
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
Definition: pgp-pgsql.c:147
static text * convert_from_utf8(text *src)
Definition: pgp-pgsql.c:85
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
Definition: pgp.c:363
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:147
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:100
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)
Definition: pgp-decrypt.c:1092
Definition: mbuf.c:39
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:137
Definition: c.h:621
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:344
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:223
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:175
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
Definition: pgp-pgsql.c:351

◆ downcase_convert()

static char* downcase_convert ( const uint8 s,
int  len 
)
static

Definition at line 288 of file pgp-pgsql.c.

References i, and palloc().

Referenced by parse_args().

289 {
290  int c,
291  i;
292  char *res = palloc(len + 1);
293 
294  for (i = 0; i < len; i++)
295  {
296  c = s[i];
297  if (c >= 'A' && c <= 'Z')
298  c += 'a' - 'A';
299  res[i] = c;
300  }
301  res[len] = 0;
302  return res;
303 }
char * c
void * palloc(Size size)
Definition: mcxt.c:1062
int i

◆ encrypt_internal()

static bytea* encrypt_internal ( int  is_pubenc,
int  is_text,
text data,
text key,
text args 
)
static

Definition at line 372 of file pgp-pgsql.c.

References clear_and_pfree(), convert_to_utf8(), create_mbuf_from_vardata(), debug_expect::debug, init_work(), mbuf_append(), mbuf_create(), mbuf_free(), mbuf_steal_data(), pgp_encrypt(), pgp_free(), pgp_get_unicode_mode(), pgp_set_pubkey(), pgp_set_symkey(), px_set_debug_handler(), px_THROW_ERROR(), SET_VARSIZE, VARDATA_ANY, VARHDRSZ, VARSIZE_ANY, and VARSIZE_ANY_EXHDR.

Referenced by pgp_pub_encrypt_bytea(), pgp_pub_encrypt_text(), pgp_sym_encrypt_bytea(), and pgp_sym_encrypt_text().

374 {
375  MBuf *src,
376  *dst;
377  uint8 tmp[VARHDRSZ];
378  uint8 *restmp;
379  bytea *res;
380  int res_len;
381  PGP_Context *ctx;
382  int err;
383  struct debug_expect ex;
384  text *tmp_data = NULL;
385 
386  init_work(&ctx, is_text, args, &ex);
387 
388  if (is_text && pgp_get_unicode_mode(ctx))
389  {
390  tmp_data = convert_to_utf8(data);
391  if (tmp_data == data)
392  tmp_data = NULL;
393  else
394  data = tmp_data;
395  }
396 
397  src = create_mbuf_from_vardata(data);
398  dst = mbuf_create(VARSIZE_ANY(data) + 128);
399 
400  /*
401  * reserve room for header
402  */
403  mbuf_append(dst, tmp, VARHDRSZ);
404 
405  /*
406  * set key
407  */
408  if (is_pubenc)
409  {
410  MBuf *kbuf = create_mbuf_from_vardata(key);
411 
412  err = pgp_set_pubkey(ctx, kbuf,
413  NULL, 0, 0);
414  mbuf_free(kbuf);
415  }
416  else
417  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
418  VARSIZE_ANY_EXHDR(key));
419 
420  /*
421  * encrypt
422  */
423  if (err >= 0)
424  err = pgp_encrypt(ctx, src, dst);
425 
426  /*
427  * check for error
428  */
429  if (err)
430  {
431  if (ex.debug)
432  px_set_debug_handler(NULL);
433  if (tmp_data)
434  clear_and_pfree(tmp_data);
435  pgp_free(ctx);
436  mbuf_free(src);
437  mbuf_free(dst);
438  px_THROW_ERROR(err);
439  }
440 
441  /* res_len includes VARHDRSZ */
442  res_len = mbuf_steal_data(dst, &restmp);
443  res = (bytea *) restmp;
444  SET_VARSIZE(res, res_len);
445 
446  if (tmp_data)
447  clear_and_pfree(tmp_data);
448  pgp_free(ctx);
449  mbuf_free(src);
450  mbuf_free(dst);
451 
452  px_set_debug_handler(NULL);
453 
454  return res;
455 }
MBuf * mbuf_create(int len)
Definition: mbuf.c:117
void px_THROW_ERROR(int err)
Definition: px.c:97
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:350
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Definition: pgp-pubkey.c:565
static void clear_and_pfree(text *p)
Definition: pgp-pgsql.c:97
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
Definition: pgp.c:363
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:147
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:100
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-encrypt.c:599
Definition: mbuf.c:39
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:621
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:344
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:223
static text * convert_to_utf8(text *src)
Definition: pgp-pgsql.c:91
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:175
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
Definition: pgp-pgsql.c:351

◆ fill_expect()

static void fill_expect ( struct debug_expect ex,
int  text_mode 
)
static

Definition at line 122 of file pgp-pgsql.c.

References debug_expect::cipher_algo, debug_expect::compress_algo, debug_expect::debug, debug_expect::disable_mdc, debug_expect::expect, debug_expect::s2k_cipher_algo, debug_expect::s2k_count, debug_expect::s2k_digest_algo, debug_expect::s2k_mode, debug_expect::unicode_mode, and debug_expect::use_sess_key.

Referenced by init_work().

123 {
124  ex->debug = 0;
125  ex->expect = 0;
126  ex->cipher_algo = -1;
127  ex->s2k_mode = -1;
128  ex->s2k_count = -1;
129  ex->s2k_cipher_algo = -1;
130  ex->s2k_digest_algo = -1;
131  ex->compress_algo = -1;
132  ex->use_sess_key = -1;
133  ex->disable_mdc = -1;
134  ex->unicode_mode = -1;
135 }
int disable_mdc
Definition: pgp-pgsql.c:117
int unicode_mode
Definition: pgp-pgsql.c:118
int s2k_digest_algo
Definition: pgp-pgsql.c:114
int s2k_cipher_algo
Definition: pgp-pgsql.c:113
int cipher_algo
Definition: pgp-pgsql.c:110
int compress_algo
Definition: pgp-pgsql.c:115
int use_sess_key
Definition: pgp-pgsql.c:116

◆ getword()

static char* getword ( char *  p,
char **  res_p,
int *  res_len 
)
static

Definition at line 259 of file pgp-pgsql.c.

Referenced by parse_args().

260 {
261  /* whitespace at start */
262  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
263  p++;
264 
265  /* word data */
266  *res_p = p;
267  if (*p == '=' || *p == ',')
268  p++;
269  else
270  while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
271  || *p == '=' || *p == ','))
272  p++;
273 
274  /* word end */
275  *res_len = p - *res_p;
276 
277  /* whitespace at end */
278  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
279  p++;
280 
281  return p;
282 }

◆ init_work()

static void init_work ( PGP_Context **  ctx_p,
int  is_text,
text args,
struct debug_expect ex 
)
static

Definition at line 351 of file pgp-pgsql.c.

References debug_expect::debug, fill_expect(), parse_args(), pgp_init(), pgp_set_text_mode(), px_set_debug_handler(), px_THROW_ERROR(), show_debug(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by decrypt_internal(), and encrypt_internal().

353 {
354  int err = pgp_init(ctx_p);
355 
356  fill_expect(ex, is_text);
357 
358  if (err == 0 && args != NULL)
359  err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
360  VARSIZE_ANY_EXHDR(args), ex);
361 
362  if (err)
363  px_THROW_ERROR(err);
364 
365  if (ex->debug)
367 
368  pgp_set_text_mode(*ctx_p, is_text);
369 }
void px_THROW_ERROR(int err)
Definition: px.c:97
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
unsigned char uint8
Definition: c.h:439
static void fill_expect(struct debug_expect *ex, int text_mode)
Definition: pgp-pgsql.c:122
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:147
int pgp_init(PGP_Context **ctx_p)
Definition: pgp.c:199
static void show_debug(const char *msg)
Definition: pgp-pgsql.c:162
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
Definition: pgp-pgsql.c:306
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int pgp_set_text_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:310

◆ parse_args()

static int parse_args ( PGP_Context ctx,
uint8 args,
int  arg_len,
struct debug_expect ex 
)
static

Definition at line 306 of file pgp-pgsql.c.

References downcase_convert(), getword(), sort-test::key, pfree(), PXE_ARGUMENT_ERROR, set_arg(), generate_unaccent_rules::str, and val.

Referenced by init_work().

308 {
309  char *str = downcase_convert(args, arg_len);
310  char *key,
311  *val;
312  int key_len,
313  val_len;
314  int res = 0;
315  char *p = str;
316 
317  while (*p)
318  {
319  res = PXE_ARGUMENT_ERROR;
320  p = getword(p, &key, &key_len);
321  if (*p++ != '=')
322  break;
323  p = getword(p, &val, &val_len);
324  if (*p == '\0')
325  ;
326  else if (*p++ != ',')
327  break;
328 
329  if (*key == 0 || *val == 0 || val_len == 0)
330  break;
331 
332  key[key_len] = 0;
333  val[val_len] = 0;
334 
335  res = set_arg(ctx, key, val, ex);
336  if (res < 0)
337  break;
338  }
339  pfree(str);
340  return res;
341 }
static char * downcase_convert(const uint8 *s, int len)
Definition: pgp-pgsql.c:288
#define PXE_ARGUMENT_ERROR
Definition: px.h:58
void pfree(void *pointer)
Definition: mcxt.c:1169
static char * getword(char *p, char **res_p, int *res_len)
Definition: pgp-pgsql.c:259
static int set_arg(PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
Definition: pgp-pgsql.c:168
long val
Definition: informix.c:664

◆ parse_key_value_arrays()

static int parse_key_value_arrays ( ArrayType key_array,
ArrayType val_array,
char ***  p_keys,
char ***  p_values 
)
static

Definition at line 755 of file pgp-pgsql.c.

References ARR_NDIM, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, i, palloc(), pg_is_ascii(), TextDatumGetCString, and values.

Referenced by pg_armor().

757 {
758  int nkdims = ARR_NDIM(key_array);
759  int nvdims = ARR_NDIM(val_array);
760  char **keys,
761  **values;
762  Datum *key_datums,
763  *val_datums;
764  bool *key_nulls,
765  *val_nulls;
766  int key_count,
767  val_count;
768  int i;
769 
770  if (nkdims > 1 || nkdims != nvdims)
771  ereport(ERROR,
772  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
773  errmsg("wrong number of array subscripts")));
774  if (nkdims == 0)
775  return 0;
776 
777  deconstruct_array(key_array,
778  TEXTOID, -1, false, TYPALIGN_INT,
779  &key_datums, &key_nulls, &key_count);
780 
781  deconstruct_array(val_array,
782  TEXTOID, -1, false, TYPALIGN_INT,
783  &val_datums, &val_nulls, &val_count);
784 
785  if (key_count != val_count)
786  ereport(ERROR,
787  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
788  errmsg("mismatched array dimensions")));
789 
790  keys = (char **) palloc(sizeof(char *) * key_count);
791  values = (char **) palloc(sizeof(char *) * val_count);
792 
793  for (i = 0; i < key_count; i++)
794  {
795  char *v;
796 
797  /* Check that the key doesn't contain anything funny */
798  if (key_nulls[i])
799  ereport(ERROR,
800  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
801  errmsg("null value not allowed for header key")));
802 
803  v = TextDatumGetCString(key_datums[i]);
804 
805  if (!pg_is_ascii(v))
806  ereport(ERROR,
807  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
808  errmsg("header key must not contain non-ASCII characters")));
809  if (strstr(v, ": "))
810  ereport(ERROR,
811  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
812  errmsg("header key must not contain \": \"")));
813  if (strchr(v, '\n'))
814  ereport(ERROR,
815  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
816  errmsg("header key must not contain newlines")));
817  keys[i] = v;
818 
819  /* And the same for the value */
820  if (val_nulls[i])
821  ereport(ERROR,
822  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
823  errmsg("null value not allowed for header value")));
824 
825  v = TextDatumGetCString(val_datums[i]);
826 
827  if (!pg_is_ascii(v))
828  ereport(ERROR,
829  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
830  errmsg("header value must not contain non-ASCII characters")));
831  if (strchr(v, '\n'))
832  ereport(ERROR,
833  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
834  errmsg("header value must not contain newlines")));
835 
836  values[i] = v;
837  }
838 
839  *p_keys = keys;
840  *p_values = values;
841  return key_count;
842 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool pg_is_ascii(const char *str)
Definition: string.c:99
#define ERROR
Definition: elog.h:46
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
static Datum values[MAXATTR]
Definition: bootstrap.c:166
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i

◆ pg_armor()

Datum pg_armor ( PG_FUNCTION_ARGS  )

Definition at line 845 of file pgp-pgsql.c.

References buf, StringInfoData::data, elog, ERROR, initStringInfo(), StringInfoData::len, palloc(), parse_key_value_arrays(), pfree(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_BYTEA_PP, PG_NARGS, PG_RETURN_TEXT_P, pgp_armor_encode(), SET_VARSIZE, values, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

846 {
847  bytea *data;
848  text *res;
849  int data_len;
851  int num_headers;
852  char **keys = NULL,
853  **values = NULL;
854 
855  data = PG_GETARG_BYTEA_PP(0);
856  data_len = VARSIZE_ANY_EXHDR(data);
857  if (PG_NARGS() == 3)
858  {
861  &keys, &values);
862  }
863  else if (PG_NARGS() == 1)
864  num_headers = 0;
865  else
866  elog(ERROR, "unexpected number of arguments %d", PG_NARGS());
867 
868  initStringInfo(&buf);
869 
870  pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
871  num_headers, keys, values);
872 
873  res = palloc(VARHDRSZ + buf.len);
874  SET_VARSIZE(res, VARHDRSZ + buf.len);
875  memcpy(VARDATA(res), buf.data, buf.len);
876  pfree(buf.data);
877 
878  PG_FREE_IF_COPY(data, 0);
879  PG_RETURN_TEXT_P(res);
880 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)
Definition: pgp-armor.c:207
static char * buf
Definition: pg_test_fsync.c:68
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
Definition: pgp-pgsql.c:755
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ pg_dearmor()

Datum pg_dearmor ( PG_FUNCTION_ARGS  )

Definition at line 883 of file pgp-pgsql.c.

References buf, StringInfoData::data, initStringInfo(), StringInfoData::len, palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, pgp_armor_decode(), px_THROW_ERROR(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

884 {
885  text *data;
886  bytea *res;
887  int data_len;
888  int ret;
890 
891  data = PG_GETARG_TEXT_PP(0);
892  data_len = VARSIZE_ANY_EXHDR(data);
893 
894  initStringInfo(&buf);
895 
896  ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
897  if (ret < 0)
898  px_THROW_ERROR(ret);
899  res = palloc(VARHDRSZ + buf.len);
900  SET_VARSIZE(res, VARHDRSZ + buf.len);
901  memcpy(VARDATA(res), buf.data, buf.len);
902  pfree(buf.data);
903 
904  PG_FREE_IF_COPY(data, 0);
905  PG_RETURN_TEXT_P(res);
906 }
void px_THROW_ERROR(int err)
Definition: px.c:97
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
void pfree(void *pointer)
Definition: mcxt.c:1169
int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)
Definition: pgp-armor.c:314
static char * buf
Definition: pg_test_fsync.c:68
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ PG_FUNCTION_INFO_V1() [1/12]

PG_FUNCTION_INFO_V1 ( pgp_sym_encrypt_bytea  )

◆ PG_FUNCTION_INFO_V1() [2/12]

PG_FUNCTION_INFO_V1 ( pgp_sym_encrypt_text  )

◆ PG_FUNCTION_INFO_V1() [3/12]

PG_FUNCTION_INFO_V1 ( pgp_sym_decrypt_bytea  )

◆ PG_FUNCTION_INFO_V1() [4/12]

PG_FUNCTION_INFO_V1 ( pgp_sym_decrypt_text  )

◆ PG_FUNCTION_INFO_V1() [5/12]

PG_FUNCTION_INFO_V1 ( pgp_pub_encrypt_bytea  )

◆ PG_FUNCTION_INFO_V1() [6/12]

PG_FUNCTION_INFO_V1 ( pgp_pub_encrypt_text  )

◆ PG_FUNCTION_INFO_V1() [7/12]

PG_FUNCTION_INFO_V1 ( pgp_pub_decrypt_bytea  )

◆ PG_FUNCTION_INFO_V1() [8/12]

PG_FUNCTION_INFO_V1 ( pgp_pub_decrypt_text  )

◆ PG_FUNCTION_INFO_V1() [9/12]

PG_FUNCTION_INFO_V1 ( pgp_key_id_w  )

◆ PG_FUNCTION_INFO_V1() [10/12]

PG_FUNCTION_INFO_V1 ( pg_armor  )

◆ PG_FUNCTION_INFO_V1() [11/12]

PG_FUNCTION_INFO_V1 ( pg_dearmor  )

◆ PG_FUNCTION_INFO_V1() [12/12]

PG_FUNCTION_INFO_V1 ( pgp_armor_headers  )

◆ pgp_armor_headers()

Datum pgp_armor_headers ( PG_FUNCTION_ARGS  )

Definition at line 917 of file pgp-pgsql.c.

References FuncCallContext::attinmeta, BuildTupleFromCStrings(), FuncCallContext::call_cntr, elog, ERROR, get_call_result_type(), HeapTupleGetDatum, pgp_armor_headers_state::keys, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, pgp_armor_headers_state::nheaders, palloc(), pg_any_to_server(), PG_GETARG_TEXT_PP, PG_UTF8, pgp_extract_armor_headers(), px_THROW_ERROR(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, values, pgp_armor_headers_state::values, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

918 {
919  FuncCallContext *funcctx;
921  char *utf8key;
922  char *utf8val;
923  HeapTuple tuple;
924  TupleDesc tupdesc;
925  AttInMetadata *attinmeta;
926 
927  if (SRF_IS_FIRSTCALL())
928  {
929  text *data = PG_GETARG_TEXT_PP(0);
930  int res;
931  MemoryContext oldcontext;
932 
933  funcctx = SRF_FIRSTCALL_INIT();
934 
935  /* we need the state allocated in the multi call context */
936  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
937 
938  /* Build a tuple descriptor for our result type */
939  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
940  elog(ERROR, "return type must be a row type");
941 
942  attinmeta = TupleDescGetAttInMetadata(tupdesc);
943  funcctx->attinmeta = attinmeta;
944 
946 
948  VARSIZE_ANY_EXHDR(data),
949  &state->nheaders, &state->keys,
950  &state->values);
951  if (res < 0)
952  px_THROW_ERROR(res);
953 
954  MemoryContextSwitchTo(oldcontext);
955  funcctx->user_fctx = state;
956  }
957 
958  funcctx = SRF_PERCALL_SETUP();
959  state = (pgp_armor_headers_state *) funcctx->user_fctx;
960 
961  if (funcctx->call_cntr >= state->nheaders)
962  SRF_RETURN_DONE(funcctx);
963  else
964  {
965  char *values[2];
966 
967  /* we assume that the keys (and values) are in UTF-8. */
968  utf8key = state->keys[funcctx->call_cntr];
969  utf8val = state->values[funcctx->call_cntr];
970 
971  values[0] = pg_any_to_server(utf8key, strlen(utf8key), PG_UTF8);
972  values[1] = pg_any_to_server(utf8val, strlen(utf8val), PG_UTF8);
973 
974  /* build a tuple */
975  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
976  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
977  }
978 }
uint64 call_cntr
Definition: funcapi.h:65
void px_THROW_ERROR(int err)
Definition: px.c:97
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:294
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned char uint8
Definition: c.h:439
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:298
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:300
#define ERROR
Definition: elog.h:46
AttInMetadata * attinmeta
Definition: funcapi.h:91
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
Definition: pgp-armor.c:390
Definition: regguts.h:317
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static Datum values[MAXATTR]
Definition: bootstrap.c:166
void * user_fctx
Definition: funcapi.h:82
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:318
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:296

◆ pgp_key_id_w()

Datum pgp_key_id_w ( PG_FUNCTION_ARGS  )

Definition at line 987 of file pgp-pgsql.c.

References buf, create_mbuf_from_vardata(), mbuf_free(), palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_RETURN_TEXT_P, pgp_get_keyid(), px_THROW_ERROR(), SET_VARSIZE, VARDATA, and VARHDRSZ.

988 {
989  bytea *data;
990  text *res;
991  int res_len;
992  MBuf *buf;
993 
994  data = PG_GETARG_BYTEA_PP(0);
995  buf = create_mbuf_from_vardata(data);
996  res = palloc(VARHDRSZ + 17);
997 
998  res_len = pgp_get_keyid(buf, VARDATA(res));
999  mbuf_free(buf);
1000  if (res_len < 0)
1001  px_THROW_ERROR(res_len);
1002  SET_VARSIZE(res, VARHDRSZ + res_len);
1003 
1004  PG_FREE_IF_COPY(data, 0);
1005  PG_RETURN_TEXT_P(res);
1006 }
void px_THROW_ERROR(int err)
Definition: px.c:97
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
int pgp_get_keyid(MBuf *pgp_data, char *dst)
Definition: pgp-info.c:112
static char * buf
Definition: pg_test_fsync.c:68
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
Definition: mbuf.c:39
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: c.h:621
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:344
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ pgp_pub_decrypt_bytea()

Datum pgp_pub_decrypt_bytea ( PG_FUNCTION_ARGS  )

Definition at line 692 of file pgp-pgsql.c.

References arg, decrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

693 {
694  bytea *data,
695  *key;
696  text *psw = NULL,
697  *arg = NULL;
698  text *res;
699 
700  data = PG_GETARG_BYTEA_PP(0);
701  key = PG_GETARG_BYTEA_PP(1);
702  if (PG_NARGS() > 2)
703  psw = PG_GETARG_BYTEA_PP(2);
704  if (PG_NARGS() > 3)
705  arg = PG_GETARG_BYTEA_PP(3);
706 
707  res = decrypt_internal(1, 0, data, key, psw, arg);
708 
709  PG_FREE_IF_COPY(data, 0);
710  PG_FREE_IF_COPY(key, 1);
711  if (PG_NARGS() > 2)
712  PG_FREE_IF_COPY(psw, 2);
713  if (PG_NARGS() > 3)
714  PG_FREE_IF_COPY(arg, 3);
715  PG_RETURN_TEXT_P(res);
716 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:458
void * arg
Definition: c.h:621

◆ pgp_pub_decrypt_text()

Datum pgp_pub_decrypt_text ( PG_FUNCTION_ARGS  )

Definition at line 719 of file pgp-pgsql.c.

References arg, decrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

720 {
721  bytea *data,
722  *key;
723  text *psw = NULL,
724  *arg = NULL;
725  text *res;
726 
727  data = PG_GETARG_BYTEA_PP(0);
728  key = PG_GETARG_BYTEA_PP(1);
729  if (PG_NARGS() > 2)
730  psw = PG_GETARG_BYTEA_PP(2);
731  if (PG_NARGS() > 3)
732  arg = PG_GETARG_BYTEA_PP(3);
733 
734  res = decrypt_internal(1, 1, data, key, psw, arg);
735 
736  PG_FREE_IF_COPY(data, 0);
737  PG_FREE_IF_COPY(key, 1);
738  if (PG_NARGS() > 2)
739  PG_FREE_IF_COPY(psw, 2);
740  if (PG_NARGS() > 3)
741  PG_FREE_IF_COPY(arg, 3);
742  PG_RETURN_TEXT_P(res);
743 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:458
void * arg
Definition: c.h:621

◆ pgp_pub_encrypt_bytea()

Datum pgp_pub_encrypt_bytea ( PG_FUNCTION_ARGS  )

Definition at line 647 of file pgp-pgsql.c.

References arg, encrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

648 {
649  bytea *data,
650  *key;
651  text *arg = NULL;
652  text *res;
653 
654  data = PG_GETARG_BYTEA_PP(0);
655  key = PG_GETARG_BYTEA_PP(1);
656  if (PG_NARGS() > 2)
657  arg = PG_GETARG_BYTEA_PP(2);
658 
659  res = encrypt_internal(1, 0, data, key, arg);
660 
661  PG_FREE_IF_COPY(data, 0);
662  PG_FREE_IF_COPY(key, 1);
663  if (PG_NARGS() > 2)
664  PG_FREE_IF_COPY(arg, 2);
665  PG_RETURN_TEXT_P(res);
666 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
void * arg
Definition: c.h:621
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:372

◆ pgp_pub_encrypt_text()

Datum pgp_pub_encrypt_text ( PG_FUNCTION_ARGS  )

Definition at line 669 of file pgp-pgsql.c.

References arg, encrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

670 {
671  bytea *data,
672  *key;
673  text *arg = NULL;
674  text *res;
675 
676  data = PG_GETARG_BYTEA_PP(0);
677  key = PG_GETARG_BYTEA_PP(1);
678  if (PG_NARGS() > 2)
679  arg = PG_GETARG_BYTEA_PP(2);
680 
681  res = encrypt_internal(1, 1, data, key, arg);
682 
683  PG_FREE_IF_COPY(data, 0);
684  PG_FREE_IF_COPY(key, 1);
685  if (PG_NARGS() > 2)
686  PG_FREE_IF_COPY(arg, 2);
687  PG_RETURN_TEXT_P(res);
688 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
void * arg
Definition: c.h:621
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:372

◆ pgp_sym_decrypt_bytea()

Datum pgp_sym_decrypt_bytea ( PG_FUNCTION_ARGS  )

Definition at line 599 of file pgp-pgsql.c.

References arg, decrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

600 {
601  bytea *data,
602  *key;
603  text *arg = NULL;
604  text *res;
605 
606  data = PG_GETARG_BYTEA_PP(0);
607  key = PG_GETARG_BYTEA_PP(1);
608  if (PG_NARGS() > 2)
609  arg = PG_GETARG_BYTEA_PP(2);
610 
611  res = decrypt_internal(0, 0, data, key, NULL, arg);
612 
613  PG_FREE_IF_COPY(data, 0);
614  PG_FREE_IF_COPY(key, 1);
615  if (PG_NARGS() > 2)
616  PG_FREE_IF_COPY(arg, 2);
617  PG_RETURN_TEXT_P(res);
618 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:458
void * arg
Definition: c.h:621

◆ pgp_sym_decrypt_text()

Datum pgp_sym_decrypt_text ( PG_FUNCTION_ARGS  )

Definition at line 621 of file pgp-pgsql.c.

References arg, decrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

622 {
623  bytea *data,
624  *key;
625  text *arg = NULL;
626  text *res;
627 
628  data = PG_GETARG_BYTEA_PP(0);
629  key = PG_GETARG_BYTEA_PP(1);
630  if (PG_NARGS() > 2)
631  arg = PG_GETARG_BYTEA_PP(2);
632 
633  res = decrypt_internal(0, 1, data, key, NULL, arg);
634 
635  PG_FREE_IF_COPY(data, 0);
636  PG_FREE_IF_COPY(key, 1);
637  if (PG_NARGS() > 2)
638  PG_FREE_IF_COPY(arg, 2);
639  PG_RETURN_TEXT_P(res);
640 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:458
void * arg
Definition: c.h:621

◆ pgp_sym_encrypt_bytea()

Datum pgp_sym_encrypt_bytea ( PG_FUNCTION_ARGS  )

Definition at line 554 of file pgp-pgsql.c.

References arg, encrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

555 {
556  bytea *data,
557  *key;
558  text *arg = NULL;
559  text *res;
560 
561  data = PG_GETARG_BYTEA_PP(0);
562  key = PG_GETARG_BYTEA_PP(1);
563  if (PG_NARGS() > 2)
564  arg = PG_GETARG_BYTEA_PP(2);
565 
566  res = encrypt_internal(0, 0, data, key, arg);
567 
568  PG_FREE_IF_COPY(data, 0);
569  PG_FREE_IF_COPY(key, 1);
570  if (PG_NARGS() > 2)
571  PG_FREE_IF_COPY(arg, 2);
572  PG_RETURN_TEXT_P(res);
573 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
void * arg
Definition: c.h:621
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:372

◆ pgp_sym_encrypt_text()

Datum pgp_sym_encrypt_text ( PG_FUNCTION_ARGS  )

Definition at line 576 of file pgp-pgsql.c.

References arg, encrypt_internal(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

577 {
578  bytea *data,
579  *key;
580  text *arg = NULL;
581  text *res;
582 
583  data = PG_GETARG_BYTEA_PP(0);
584  key = PG_GETARG_BYTEA_PP(1);
585  if (PG_NARGS() > 2)
586  arg = PG_GETARG_BYTEA_PP(2);
587 
588  res = encrypt_internal(0, 1, data, key, arg);
589 
590  PG_FREE_IF_COPY(data, 0);
591  PG_FREE_IF_COPY(key, 1);
592  if (PG_NARGS() > 2)
593  PG_FREE_IF_COPY(arg, 2);
594  PG_RETURN_TEXT_P(res);
595 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_NARGS()
Definition: fmgr.h:203
void * arg
Definition: c.h:621
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:372

◆ set_arg()

static int set_arg ( PGP_Context ctx,
char *  key,
char *  val,
struct debug_expect ex 
)
static

Definition at line 168 of file pgp-pgsql.c.

References debug_expect::cipher_algo, debug_expect::compress_algo, debug_expect::debug, debug_expect::disable_mdc, debug_expect::expect, pgp_disable_mdc(), pgp_get_cipher_code(), pgp_get_digest_code(), pgp_set_cipher_algo(), pgp_set_compress_algo(), pgp_set_compress_level(), pgp_set_convert_crlf(), pgp_set_s2k_cipher_algo(), pgp_set_s2k_count(), pgp_set_s2k_digest_algo(), pgp_set_s2k_mode(), pgp_set_sess_key(), pgp_set_unicode_mode(), PXE_ARGUMENT_ERROR, debug_expect::s2k_cipher_algo, debug_expect::s2k_count, debug_expect::s2k_digest_algo, debug_expect::s2k_mode, debug_expect::unicode_mode, and debug_expect::use_sess_key.

Referenced by parse_args().

170 {
171  int res = 0;
172 
173  if (strcmp(key, "cipher-algo") == 0)
174  res = pgp_set_cipher_algo(ctx, val);
175  else if (strcmp(key, "disable-mdc") == 0)
176  res = pgp_disable_mdc(ctx, atoi(val));
177  else if (strcmp(key, "sess-key") == 0)
178  res = pgp_set_sess_key(ctx, atoi(val));
179  else if (strcmp(key, "s2k-mode") == 0)
180  res = pgp_set_s2k_mode(ctx, atoi(val));
181  else if (strcmp(key, "s2k-count") == 0)
182  res = pgp_set_s2k_count(ctx, atoi(val));
183  else if (strcmp(key, "s2k-digest-algo") == 0)
184  res = pgp_set_s2k_digest_algo(ctx, val);
185  else if (strcmp(key, "s2k-cipher-algo") == 0)
186  res = pgp_set_s2k_cipher_algo(ctx, val);
187  else if (strcmp(key, "compress-algo") == 0)
188  res = pgp_set_compress_algo(ctx, atoi(val));
189  else if (strcmp(key, "compress-level") == 0)
190  res = pgp_set_compress_level(ctx, atoi(val));
191  else if (strcmp(key, "convert-crlf") == 0)
192  res = pgp_set_convert_crlf(ctx, atoi(val));
193  else if (strcmp(key, "unicode-mode") == 0)
194  res = pgp_set_unicode_mode(ctx, atoi(val));
195 
196  /*
197  * The remaining options are for debugging/testing and are therefore not
198  * documented in the user-facing docs.
199  */
200  else if (ex != NULL && strcmp(key, "debug") == 0)
201  ex->debug = atoi(val);
202  else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)
203  {
204  ex->expect = 1;
206  }
207  else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)
208  {
209  ex->expect = 1;
210  ex->disable_mdc = atoi(val);
211  }
212  else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)
213  {
214  ex->expect = 1;
215  ex->use_sess_key = atoi(val);
216  }
217  else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)
218  {
219  ex->expect = 1;
220  ex->s2k_mode = atoi(val);
221  }
222  else if (ex != NULL && strcmp(key, "expect-s2k-count") == 0)
223  {
224  ex->expect = 1;
225  ex->s2k_count = atoi(val);
226  }
227  else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)
228  {
229  ex->expect = 1;
231  }
232  else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)
233  {
234  ex->expect = 1;
236  }
237  else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)
238  {
239  ex->expect = 1;
240  ex->compress_algo = atoi(val);
241  }
242  else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)
243  {
244  ex->expect = 1;
245  ex->unicode_mode = atoi(val);
246  }
247  else
248  res = PXE_ARGUMENT_ERROR;
249 
250  return res;
251 }
int disable_mdc
Definition: pgp-pgsql.c:117
#define PXE_ARGUMENT_ERROR
Definition: px.h:58
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:115
int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:328
int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:339
int unicode_mode
Definition: pgp-pgsql.c:118
int pgp_set_s2k_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:254
int pgp_get_digest_code(const char *name)
Definition: pgp.c:104
int s2k_digest_algo
Definition: pgp-pgsql.c:114
int pgp_set_sess_key(PGP_Context *ctx, int use)
Definition: pgp.c:240
int s2k_cipher_algo
Definition: pgp-pgsql.c:113
int cipher_algo
Definition: pgp-pgsql.c:110
int pgp_set_compress_level(PGP_Context *ctx, int level)
Definition: pgp.c:299
int compress_algo
Definition: pgp-pgsql.c:115
int pgp_set_convert_crlf(PGP_Context *ctx, int doit)
Definition: pgp.c:247
int pgp_set_unicode_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:356
int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:317
int pgp_disable_mdc(PGP_Context *ctx, int disable)
Definition: pgp.c:233
int pgp_set_s2k_count(PGP_Context *ctx, int count)
Definition: pgp.c:273
long val
Definition: informix.c:664
int pgp_set_compress_algo(PGP_Context *ctx, int algo)
Definition: pgp.c:284
int use_sess_key
Definition: pgp-pgsql.c:116

◆ show_debug()

static void show_debug ( const char *  msg)
static

Definition at line 162 of file pgp-pgsql.c.

References ereport, errmsg(), and NOTICE.

Referenced by init_work().

163 {
164  ereport(NOTICE, (errmsg("dbg: %s", msg)));
165 }
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:909