PostgreSQL Source Code  git master
pgp-pgsql.c File Reference
#include "postgres.h"
#include "lib/stringinfo.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "funcapi.h"
#include "mbuf.h"
#include "px.h"
#include "pgp.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 bool string_is_ascii (const char *str)
 
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:150
void * arg

Definition at line 155 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, rest)
Definition: elog.h:122
#define NOTICE
Definition: elog.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CppAsString(identifier)
Definition: c.h:205
void * arg

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

Function Documentation

◆ check_expect()

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

Definition at line 160 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().

161 {
162  EX_CHECK(cipher_algo);
163  EX_CHECK(s2k_mode);
164  EX_CHECK(s2k_count);
165  EX_CHECK(s2k_digest_algo);
166  EX_CHECK(use_sess_key);
167  if (ctx->use_sess_key)
168  EX_CHECK(s2k_cipher_algo);
169  EX_CHECK(disable_mdc);
170  EX_CHECK(compress_algo);
171  EX_CHECK(unicode_mode);
172 }
int use_sess_key
Definition: pgp.h:148
#define EX_CHECK(arg)
Definition: pgp-pgsql.c:155

◆ clear_and_pfree()

static void clear_and_pfree ( text p)
static

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

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

Referenced by decrypt_internal(), and encrypt_internal().

111 {
112  px_memset(p, 0, VARSIZE_ANY(p));
113  pfree(p);
114 }
void pfree(void *pointer)
Definition: mcxt.c:949
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134

◆ 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:347
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:326
void pfree(void *pointer)
Definition: mcxt.c:949
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:487

◆ 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:1004

◆ 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:1004

◆ create_mbuf_from_vardata()

static MBuf* create_mbuf_from_vardata ( text data)
static

Definition at line 357 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().

358 {
359  return mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
360  VARSIZE_ANY_EXHDR(data));
361 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
unsigned char uint8
Definition: c.h:294
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:139

◆ decrypt_internal()

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

Definition at line 471 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().

473 {
474  int err;
475  MBuf *src = NULL,
476  *dst = NULL;
477  uint8 tmp[VARHDRSZ];
478  uint8 *restmp;
479  bytea *res;
480  int res_len;
481  PGP_Context *ctx = NULL;
482  struct debug_expect ex;
483  int got_unicode = 0;
484 
485 
486  init_work(&ctx, need_text, args, &ex);
487 
488  src = mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
489  VARSIZE_ANY_EXHDR(data));
490  dst = mbuf_create(VARSIZE_ANY(data) + 2048);
491 
492  /*
493  * reserve room for header
494  */
495  mbuf_append(dst, tmp, VARHDRSZ);
496 
497  /*
498  * set key
499  */
500  if (is_pubenc)
501  {
502  uint8 *psw = NULL;
503  int psw_len = 0;
504  MBuf *kbuf;
505 
506  if (keypsw)
507  {
508  psw = (uint8 *) VARDATA_ANY(keypsw);
509  psw_len = VARSIZE_ANY_EXHDR(keypsw);
510  }
511  kbuf = create_mbuf_from_vardata(key);
512  err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
513  mbuf_free(kbuf);
514  }
515  else
516  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
517  VARSIZE_ANY_EXHDR(key));
518 
519  /* decrypt */
520  if (err >= 0)
521  {
522  err = pgp_decrypt(ctx, src, dst);
523 
524  if (ex.expect)
525  check_expect(ctx, &ex);
526 
527  /* remember the setting */
528  got_unicode = pgp_get_unicode_mode(ctx);
529  }
530 
531  mbuf_free(src);
532  pgp_free(ctx);
533 
534  if (err)
535  {
536  px_set_debug_handler(NULL);
537  mbuf_free(dst);
538  px_THROW_ERROR(err);
539  }
540 
541  res_len = mbuf_steal_data(dst, &restmp);
542  mbuf_free(dst);
543 
544  /* res_len includes VARHDRSZ */
545  res = (bytea *) restmp;
546  SET_VARSIZE(res, res_len);
547 
548  if (need_text && got_unicode)
549  {
550  text *utf = convert_from_utf8(res);
551 
552  if (utf != res)
553  {
554  clear_and_pfree(res);
555  res = utf;
556  }
557  }
558  px_set_debug_handler(NULL);
559 
560  return res;
561 }
MBuf * mbuf_create(int len)
Definition: mbuf.c:119
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:352
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Definition: pgp-pubkey.c:566
static void clear_and_pfree(text *p)
Definition: pgp-pgsql.c:110
#define VARHDRSZ
Definition: c.h:493
unsigned char uint8
Definition: c.h:294
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
Definition: pgp-pgsql.c:160
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:365
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:154
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:102
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)
Definition: pgp-decrypt.c:1090
Definition: mbuf.c:39
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:139
Definition: c.h:487
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:357
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:225
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:177
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
Definition: pgp-pgsql.c:364

◆ downcase_convert()

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

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

References i, and palloc().

Referenced by parse_args().

302 {
303  int c,
304  i;
305  char *res = palloc(len + 1);
306 
307  for (i = 0; i < len; i++)
308  {
309  c = s[i];
310  if (c >= 'A' && c <= 'Z')
311  c += 'a' - 'A';
312  res[i] = c;
313  }
314  res[len] = 0;
315  return res;
316 }
char * c
void * palloc(Size size)
Definition: mcxt.c:848
int i

◆ encrypt_internal()

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

Definition at line 385 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().

387 {
388  MBuf *src,
389  *dst;
390  uint8 tmp[VARHDRSZ];
391  uint8 *restmp;
392  bytea *res;
393  int res_len;
394  PGP_Context *ctx;
395  int err;
396  struct debug_expect ex;
397  text *tmp_data = NULL;
398 
399  init_work(&ctx, is_text, args, &ex);
400 
401  if (is_text && pgp_get_unicode_mode(ctx))
402  {
403  tmp_data = convert_to_utf8(data);
404  if (tmp_data == data)
405  tmp_data = NULL;
406  else
407  data = tmp_data;
408  }
409 
410  src = create_mbuf_from_vardata(data);
411  dst = mbuf_create(VARSIZE_ANY(data) + 128);
412 
413  /*
414  * reserve room for header
415  */
416  mbuf_append(dst, tmp, VARHDRSZ);
417 
418  /*
419  * set key
420  */
421  if (is_pubenc)
422  {
423  MBuf *kbuf = create_mbuf_from_vardata(key);
424 
425  err = pgp_set_pubkey(ctx, kbuf,
426  NULL, 0, 0);
427  mbuf_free(kbuf);
428  }
429  else
430  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
431  VARSIZE_ANY_EXHDR(key));
432 
433  /*
434  * encrypt
435  */
436  if (err >= 0)
437  err = pgp_encrypt(ctx, src, dst);
438 
439  /*
440  * check for error
441  */
442  if (err)
443  {
444  if (ex.debug)
445  px_set_debug_handler(NULL);
446  if (tmp_data)
447  clear_and_pfree(tmp_data);
448  pgp_free(ctx);
449  mbuf_free(src);
450  mbuf_free(dst);
451  px_THROW_ERROR(err);
452  }
453 
454  /* res_len includes VARHDRSZ */
455  res_len = mbuf_steal_data(dst, &restmp);
456  res = (bytea *) restmp;
457  SET_VARSIZE(res, res_len);
458 
459  if (tmp_data)
460  clear_and_pfree(tmp_data);
461  pgp_free(ctx);
462  mbuf_free(src);
463  mbuf_free(dst);
464 
465  px_set_debug_handler(NULL);
466 
467  return res;
468 }
MBuf * mbuf_create(int len)
Definition: mbuf.c:119
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:352
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Definition: pgp-pubkey.c:566
static void clear_and_pfree(text *p)
Definition: pgp-pgsql.c:110
#define VARHDRSZ
Definition: c.h:493
unsigned char uint8
Definition: c.h:294
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
Definition: pgp.c:365
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:154
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:102
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-encrypt.c:611
Definition: mbuf.c:39
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:487
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:357
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:225
static text * convert_to_utf8(text *src)
Definition: pgp-pgsql.c:91
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:177
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
Definition: pgp-pgsql.c:364

◆ fill_expect()

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

Definition at line 135 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().

136 {
137  ex->debug = 0;
138  ex->expect = 0;
139  ex->cipher_algo = -1;
140  ex->s2k_mode = -1;
141  ex->s2k_count = -1;
142  ex->s2k_cipher_algo = -1;
143  ex->s2k_digest_algo = -1;
144  ex->compress_algo = -1;
145  ex->use_sess_key = -1;
146  ex->disable_mdc = -1;
147  ex->unicode_mode = -1;
148 }
int disable_mdc
Definition: pgp-pgsql.c:130
int unicode_mode
Definition: pgp-pgsql.c:131
int s2k_digest_algo
Definition: pgp-pgsql.c:127
int s2k_cipher_algo
Definition: pgp-pgsql.c:126
int cipher_algo
Definition: pgp-pgsql.c:123
int compress_algo
Definition: pgp-pgsql.c:128
int use_sess_key
Definition: pgp-pgsql.c:129

◆ getword()

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

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

Referenced by parse_args().

273 {
274  /* whitespace at start */
275  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
276  p++;
277 
278  /* word data */
279  *res_p = p;
280  if (*p == '=' || *p == ',')
281  p++;
282  else
283  while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
284  || *p == '=' || *p == ','))
285  p++;
286 
287  /* word end */
288  *res_len = p - *res_p;
289 
290  /* whitespace at end */
291  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
292  p++;
293 
294  return p;
295 }

◆ init_work()

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

Definition at line 364 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().

366 {
367  int err = pgp_init(ctx_p);
368 
369  fill_expect(ex, is_text);
370 
371  if (err == 0 && args != NULL)
372  err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
373  VARSIZE_ANY_EXHDR(args), ex);
374 
375  if (err)
376  px_THROW_ERROR(err);
377 
378  if (ex->debug)
380 
381  pgp_set_text_mode(*ctx_p, is_text);
382 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
unsigned char uint8
Definition: c.h:294
static void fill_expect(struct debug_expect *ex, int text_mode)
Definition: pgp-pgsql.c:135
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:154
int pgp_init(PGP_Context **ctx_p)
Definition: pgp.c:200
static void show_debug(const char *msg)
Definition: pgp-pgsql.c:175
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
Definition: pgp-pgsql.c:319
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int pgp_set_text_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:312

◆ parse_args()

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

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

References downcase_convert(), getword(), pfree(), PXE_ARGUMENT_ERROR, set_arg(), generate_unaccent_rules::str, and val.

Referenced by init_work().

321 {
322  char *str = downcase_convert(args, arg_len);
323  char *key,
324  *val;
325  int key_len,
326  val_len;
327  int res = 0;
328  char *p = str;
329 
330  while (*p)
331  {
332  res = PXE_ARGUMENT_ERROR;
333  p = getword(p, &key, &key_len);
334  if (*p++ != '=')
335  break;
336  p = getword(p, &val, &val_len);
337  if (*p == '\0')
338  ;
339  else if (*p++ != ',')
340  break;
341 
342  if (*key == 0 || *val == 0 || val_len == 0)
343  break;
344 
345  key[key_len] = 0;
346  val[val_len] = 0;
347 
348  res = set_arg(ctx, key, val, ex);
349  if (res < 0)
350  break;
351  }
352  pfree(str);
353  return res;
354 }
static char * downcase_convert(const uint8 *s, int len)
Definition: pgp-pgsql.c:301
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
void pfree(void *pointer)
Definition: mcxt.c:949
static char * getword(char *p, char **res_p, int *res_len)
Definition: pgp-pgsql.c:272
static int set_arg(PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
Definition: pgp-pgsql.c:181
long val
Definition: informix.c:689

◆ 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 768 of file pgp-pgsql.c.

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

Referenced by pg_armor().

770 {
771  int nkdims = ARR_NDIM(key_array);
772  int nvdims = ARR_NDIM(val_array);
773  char **keys,
774  **values;
775  Datum *key_datums,
776  *val_datums;
777  bool *key_nulls,
778  *val_nulls;
779  int key_count,
780  val_count;
781  int i;
782 
783  if (nkdims > 1 || nkdims != nvdims)
784  ereport(ERROR,
785  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
786  errmsg("wrong number of array subscripts")));
787  if (nkdims == 0)
788  return 0;
789 
790  deconstruct_array(key_array,
791  TEXTOID, -1, false, 'i',
792  &key_datums, &key_nulls, &key_count);
793 
794  deconstruct_array(val_array,
795  TEXTOID, -1, false, 'i',
796  &val_datums, &val_nulls, &val_count);
797 
798  if (key_count != val_count)
799  ereport(ERROR,
800  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
801  errmsg("mismatched array dimensions")));
802 
803  keys = (char **) palloc(sizeof(char *) * key_count);
804  values = (char **) palloc(sizeof(char *) * val_count);
805 
806  for (i = 0; i < key_count; i++)
807  {
808  char *v;
809 
810  /* Check that the key doesn't contain anything funny */
811  if (key_nulls[i])
812  ereport(ERROR,
813  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
814  errmsg("null value not allowed for header key")));
815 
816  v = TextDatumGetCString(key_datums[i]);
817 
818  if (!string_is_ascii(v))
819  ereport(ERROR,
820  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
821  errmsg("header key must not contain non-ASCII characters")));
822  if (strstr(v, ": "))
823  ereport(ERROR,
824  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
825  errmsg("header key must not contain \": \"")));
826  if (strchr(v, '\n'))
827  ereport(ERROR,
828  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
829  errmsg("header key must not contain newlines")));
830  keys[i] = v;
831 
832  /* And the same for the value */
833  if (val_nulls[i])
834  ereport(ERROR,
835  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
836  errmsg("null value not allowed for header value")));
837 
838  v = TextDatumGetCString(val_datums[i]);
839 
840  if (!string_is_ascii(v))
841  ereport(ERROR,
842  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
843  errmsg("header value must not contain non-ASCII characters")));
844  if (strchr(v, '\n'))
845  ereport(ERROR,
846  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
847  errmsg("header value must not contain newlines")));
848 
849  values[i] = v;
850  }
851 
852  *p_keys = keys;
853  *p_values = values;
854  return key_count;
855 }
#define TEXTOID
Definition: pg_type.h:324
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static bool string_is_ascii(const char *str)
Definition: pgp-pgsql.c:97
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i

◆ pg_armor()

Datum pg_armor ( PG_FUNCTION_ARGS  )

Definition at line 858 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.

859 {
860  bytea *data;
861  text *res;
862  int data_len;
864  int num_headers;
865  char **keys = NULL,
866  **values = NULL;
867 
868  data = PG_GETARG_BYTEA_PP(0);
869  data_len = VARSIZE_ANY_EXHDR(data);
870  if (PG_NARGS() == 3)
871  {
874  &keys, &values);
875  }
876  else if (PG_NARGS() == 1)
877  num_headers = 0;
878  else
879  elog(ERROR, "unexpected number of arguments %d", PG_NARGS());
880 
881  initStringInfo(&buf);
882 
883  pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
884  num_headers, keys, values);
885 
886  res = palloc(VARHDRSZ + buf.len);
887  SET_VARSIZE(res, VARHDRSZ + buf.len);
888  memcpy(VARDATA(res), buf.data, buf.len);
889  pfree(buf.data);
890 
891  PG_FREE_IF_COPY(data, 0);
892  PG_RETURN_TEXT_P(res);
893 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:493
unsigned char uint8
Definition: c.h:294
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
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:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
Definition: pgp-pgsql.c:768
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:487
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define elog
Definition: elog.h:219

◆ pg_dearmor()

Datum pg_dearmor ( PG_FUNCTION_ARGS  )

Definition at line 896 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.

897 {
898  text *data;
899  bytea *res;
900  int data_len;
901  int ret;
903 
904  data = PG_GETARG_TEXT_PP(0);
905  data_len = VARSIZE_ANY_EXHDR(data);
906 
907  initStringInfo(&buf);
908 
909  ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
910  if (ret < 0)
911  px_THROW_ERROR(ret);
912  res = palloc(VARHDRSZ + buf.len);
913  SET_VARSIZE(res, VARHDRSZ + buf.len);
914  memcpy(VARDATA(res), buf.data, buf.len);
915  pfree(buf.data);
916 
917  PG_FREE_IF_COPY(data, 0);
918  PG_RETURN_TEXT_P(res);
919 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:493
unsigned char uint8
Definition: c.h:294
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:949
int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)
Definition: pgp-armor.c:314
static char * buf
Definition: pg_test_fsync.c:67
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:487
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

◆ 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 930 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.

931 {
932  FuncCallContext *funcctx;
934  char *utf8key;
935  char *utf8val;
936  HeapTuple tuple;
937  TupleDesc tupdesc;
938  AttInMetadata *attinmeta;
939 
940  if (SRF_IS_FIRSTCALL())
941  {
942  text *data = PG_GETARG_TEXT_PP(0);
943  int res;
944  MemoryContext oldcontext;
945 
946  funcctx = SRF_FIRSTCALL_INIT();
947 
948  /* we need the state allocated in the multi call context */
949  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
950 
951  /* Build a tuple descriptor for our result type */
952  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
953  elog(ERROR, "return type must be a row type");
954 
955  attinmeta = TupleDescGetAttInMetadata(tupdesc);
956  funcctx->attinmeta = attinmeta;
957 
959 
961  VARSIZE_ANY_EXHDR(data),
962  &state->nheaders, &state->keys,
963  &state->values);
964  if (res < 0)
965  px_THROW_ERROR(res);
966 
967  MemoryContextSwitchTo(oldcontext);
968  funcctx->user_fctx = state;
969  }
970 
971  funcctx = SRF_PERCALL_SETUP();
972  state = (pgp_armor_headers_state *) funcctx->user_fctx;
973 
974  if (funcctx->call_cntr >= state->nheaders)
975  SRF_RETURN_DONE(funcctx);
976  else
977  {
978  char *values[2];
979 
980  /* we assume that the keys (and values) are in UTF-8. */
981  utf8key = state->keys[funcctx->call_cntr];
982  utf8val = state->values[funcctx->call_cntr];
983 
984  values[0] = pg_any_to_server(utf8key, strlen(utf8key), PG_UTF8);
985  values[1] = pg_any_to_server(utf8val, strlen(utf8val), PG_UTF8);
986 
987  /* build a tuple */
988  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
989  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
990  }
991 }
uint64 call_cntr
Definition: funcapi.h:66
void px_THROW_ERROR(int err)
Definition: px.c:96
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned char uint8
Definition: c.h:294
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define ERROR
Definition: elog.h:43
AttInMetadata * attinmeta
Definition: funcapi.h:100
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
Definition: pgp-armor.c:390
Definition: regguts.h:298
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:230
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * user_fctx
Definition: funcapi.h:91
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:487
#define elog
Definition: elog.h:219
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:561
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ pgp_key_id_w()

Datum pgp_key_id_w ( PG_FUNCTION_ARGS  )

Definition at line 1000 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.

1001 {
1002  bytea *data;
1003  text *res;
1004  int res_len;
1005  MBuf *buf;
1006 
1007  data = PG_GETARG_BYTEA_PP(0);
1008  buf = create_mbuf_from_vardata(data);
1009  res = palloc(VARHDRSZ + 17);
1010 
1011  res_len = pgp_get_keyid(buf, VARDATA(res));
1012  mbuf_free(buf);
1013  if (res_len < 0)
1014  px_THROW_ERROR(res_len);
1015  SET_VARSIZE(res, VARHDRSZ + res_len);
1016 
1017  PG_FREE_IF_COPY(data, 0);
1018  PG_RETURN_TEXT_P(res);
1019 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:493
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:67
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
Definition: mbuf.c:39
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:487
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:357
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

◆ pgp_pub_decrypt_bytea()

Datum pgp_pub_decrypt_bytea ( PG_FUNCTION_ARGS  )

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

References arg, decrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

706 {
707  bytea *data,
708  *key;
709  text *psw = NULL,
710  *arg = NULL;
711  text *res;
712 
713  data = PG_GETARG_BYTEA_PP(0);
714  key = PG_GETARG_BYTEA_PP(1);
715  if (PG_NARGS() > 2)
716  psw = PG_GETARG_BYTEA_PP(2);
717  if (PG_NARGS() > 3)
718  arg = PG_GETARG_BYTEA_PP(3);
719 
720  res = decrypt_internal(1, 0, data, key, psw, arg);
721 
722  PG_FREE_IF_COPY(data, 0);
723  PG_FREE_IF_COPY(key, 1);
724  if (PG_NARGS() > 2)
725  PG_FREE_IF_COPY(psw, 2);
726  if (PG_NARGS() > 3)
727  PG_FREE_IF_COPY(arg, 3);
728  PG_RETURN_TEXT_P(res);
729 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:471
void * arg
Definition: c.h:487

◆ pgp_pub_decrypt_text()

Datum pgp_pub_decrypt_text ( PG_FUNCTION_ARGS  )

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

References arg, decrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

733 {
734  bytea *data,
735  *key;
736  text *psw = NULL,
737  *arg = NULL;
738  text *res;
739 
740  data = PG_GETARG_BYTEA_PP(0);
741  key = PG_GETARG_BYTEA_PP(1);
742  if (PG_NARGS() > 2)
743  psw = PG_GETARG_BYTEA_PP(2);
744  if (PG_NARGS() > 3)
745  arg = PG_GETARG_BYTEA_PP(3);
746 
747  res = decrypt_internal(1, 1, data, key, psw, arg);
748 
749  PG_FREE_IF_COPY(data, 0);
750  PG_FREE_IF_COPY(key, 1);
751  if (PG_NARGS() > 2)
752  PG_FREE_IF_COPY(psw, 2);
753  if (PG_NARGS() > 3)
754  PG_FREE_IF_COPY(arg, 3);
755  PG_RETURN_TEXT_P(res);
756 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:471
void * arg
Definition: c.h:487

◆ pgp_pub_encrypt_bytea()

Datum pgp_pub_encrypt_bytea ( PG_FUNCTION_ARGS  )

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

References arg, encrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

661 {
662  bytea *data,
663  *key;
664  text *arg = NULL;
665  text *res;
666 
667  data = PG_GETARG_BYTEA_PP(0);
668  key = PG_GETARG_BYTEA_PP(1);
669  if (PG_NARGS() > 2)
670  arg = PG_GETARG_BYTEA_PP(2);
671 
672  res = encrypt_internal(1, 0, data, key, arg);
673 
674  PG_FREE_IF_COPY(data, 0);
675  PG_FREE_IF_COPY(key, 1);
676  if (PG_NARGS() > 2)
677  PG_FREE_IF_COPY(arg, 2);
678  PG_RETURN_TEXT_P(res);
679 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
void * arg
Definition: c.h:487
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:385

◆ pgp_pub_encrypt_text()

Datum pgp_pub_encrypt_text ( PG_FUNCTION_ARGS  )

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

References arg, encrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

683 {
684  bytea *data,
685  *key;
686  text *arg = NULL;
687  text *res;
688 
689  data = PG_GETARG_BYTEA_PP(0);
690  key = PG_GETARG_BYTEA_PP(1);
691  if (PG_NARGS() > 2)
692  arg = PG_GETARG_BYTEA_PP(2);
693 
694  res = encrypt_internal(1, 1, data, key, arg);
695 
696  PG_FREE_IF_COPY(data, 0);
697  PG_FREE_IF_COPY(key, 1);
698  if (PG_NARGS() > 2)
699  PG_FREE_IF_COPY(arg, 2);
700  PG_RETURN_TEXT_P(res);
701 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
void * arg
Definition: c.h:487
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:385

◆ pgp_sym_decrypt_bytea()

Datum pgp_sym_decrypt_bytea ( PG_FUNCTION_ARGS  )

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

References arg, decrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

613 {
614  bytea *data,
615  *key;
616  text *arg = NULL;
617  text *res;
618 
619  data = PG_GETARG_BYTEA_PP(0);
620  key = PG_GETARG_BYTEA_PP(1);
621  if (PG_NARGS() > 2)
622  arg = PG_GETARG_BYTEA_PP(2);
623 
624  res = decrypt_internal(0, 0, data, key, NULL, arg);
625 
626  PG_FREE_IF_COPY(data, 0);
627  PG_FREE_IF_COPY(key, 1);
628  if (PG_NARGS() > 2)
629  PG_FREE_IF_COPY(arg, 2);
630  PG_RETURN_TEXT_P(res);
631 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:471
void * arg
Definition: c.h:487

◆ pgp_sym_decrypt_text()

Datum pgp_sym_decrypt_text ( PG_FUNCTION_ARGS  )

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

References arg, decrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

635 {
636  bytea *data,
637  *key;
638  text *arg = NULL;
639  text *res;
640 
641  data = PG_GETARG_BYTEA_PP(0);
642  key = PG_GETARG_BYTEA_PP(1);
643  if (PG_NARGS() > 2)
644  arg = PG_GETARG_BYTEA_PP(2);
645 
646  res = decrypt_internal(0, 1, data, key, NULL, arg);
647 
648  PG_FREE_IF_COPY(data, 0);
649  PG_FREE_IF_COPY(key, 1);
650  if (PG_NARGS() > 2)
651  PG_FREE_IF_COPY(arg, 2);
652  PG_RETURN_TEXT_P(res);
653 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:471
void * arg
Definition: c.h:487

◆ pgp_sym_encrypt_bytea()

Datum pgp_sym_encrypt_bytea ( PG_FUNCTION_ARGS  )

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

References arg, encrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

568 {
569  bytea *data,
570  *key;
571  text *arg = NULL;
572  text *res;
573 
574  data = PG_GETARG_BYTEA_PP(0);
575  key = PG_GETARG_BYTEA_PP(1);
576  if (PG_NARGS() > 2)
577  arg = PG_GETARG_BYTEA_PP(2);
578 
579  res = encrypt_internal(0, 0, data, key, arg);
580 
581  PG_FREE_IF_COPY(data, 0);
582  PG_FREE_IF_COPY(key, 1);
583  if (PG_NARGS() > 2)
584  PG_FREE_IF_COPY(arg, 2);
585  PG_RETURN_TEXT_P(res);
586 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
void * arg
Definition: c.h:487
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:385

◆ pgp_sym_encrypt_text()

Datum pgp_sym_encrypt_text ( PG_FUNCTION_ARGS  )

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

References arg, encrypt_internal(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_NARGS, and PG_RETURN_TEXT_P.

590 {
591  bytea *data,
592  *key;
593  text *arg = NULL;
594  text *res;
595 
596  data = PG_GETARG_BYTEA_PP(0);
597  key = PG_GETARG_BYTEA_PP(1);
598  if (PG_NARGS() > 2)
599  arg = PG_GETARG_BYTEA_PP(2);
600 
601  res = encrypt_internal(0, 1, data, key, arg);
602 
603  PG_FREE_IF_COPY(data, 0);
604  PG_FREE_IF_COPY(key, 1);
605  if (PG_NARGS() > 2)
606  PG_FREE_IF_COPY(arg, 2);
607  PG_RETURN_TEXT_P(res);
608 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
void * arg
Definition: c.h:487
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:385

◆ set_arg()

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

Definition at line 181 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().

183 {
184  int res = 0;
185 
186  if (strcmp(key, "cipher-algo") == 0)
187  res = pgp_set_cipher_algo(ctx, val);
188  else if (strcmp(key, "disable-mdc") == 0)
189  res = pgp_disable_mdc(ctx, atoi(val));
190  else if (strcmp(key, "sess-key") == 0)
191  res = pgp_set_sess_key(ctx, atoi(val));
192  else if (strcmp(key, "s2k-mode") == 0)
193  res = pgp_set_s2k_mode(ctx, atoi(val));
194  else if (strcmp(key, "s2k-count") == 0)
195  res = pgp_set_s2k_count(ctx, atoi(val));
196  else if (strcmp(key, "s2k-digest-algo") == 0)
197  res = pgp_set_s2k_digest_algo(ctx, val);
198  else if (strcmp(key, "s2k-cipher-algo") == 0)
199  res = pgp_set_s2k_cipher_algo(ctx, val);
200  else if (strcmp(key, "compress-algo") == 0)
201  res = pgp_set_compress_algo(ctx, atoi(val));
202  else if (strcmp(key, "compress-level") == 0)
203  res = pgp_set_compress_level(ctx, atoi(val));
204  else if (strcmp(key, "convert-crlf") == 0)
205  res = pgp_set_convert_crlf(ctx, atoi(val));
206  else if (strcmp(key, "unicode-mode") == 0)
207  res = pgp_set_unicode_mode(ctx, atoi(val));
208 
209  /*
210  * The remaining options are for debugging/testing and are therefore not
211  * documented in the user-facing docs.
212  */
213  else if (ex != NULL && strcmp(key, "debug") == 0)
214  ex->debug = atoi(val);
215  else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)
216  {
217  ex->expect = 1;
219  }
220  else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)
221  {
222  ex->expect = 1;
223  ex->disable_mdc = atoi(val);
224  }
225  else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)
226  {
227  ex->expect = 1;
228  ex->use_sess_key = atoi(val);
229  }
230  else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)
231  {
232  ex->expect = 1;
233  ex->s2k_mode = atoi(val);
234  }
235  else if (ex != NULL && strcmp(key, "expect-s2k-count") == 0)
236  {
237  ex->expect = 1;
238  ex->s2k_count = atoi(val);
239  }
240  else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)
241  {
242  ex->expect = 1;
244  }
245  else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)
246  {
247  ex->expect = 1;
249  }
250  else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)
251  {
252  ex->expect = 1;
253  ex->compress_algo = atoi(val);
254  }
255  else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)
256  {
257  ex->expect = 1;
258  ex->unicode_mode = atoi(val);
259  }
260  else
261  res = PXE_ARGUMENT_ERROR;
262 
263  return res;
264 }
int disable_mdc
Definition: pgp-pgsql.c:130
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:116
int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:330
int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:341
int unicode_mode
Definition: pgp-pgsql.c:131
int pgp_set_s2k_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:256
int pgp_get_digest_code(const char *name)
Definition: pgp.c:105
int s2k_digest_algo
Definition: pgp-pgsql.c:127
int pgp_set_sess_key(PGP_Context *ctx, int use)
Definition: pgp.c:242
int s2k_cipher_algo
Definition: pgp-pgsql.c:126
int cipher_algo
Definition: pgp-pgsql.c:123
int pgp_set_compress_level(PGP_Context *ctx, int level)
Definition: pgp.c:301
int compress_algo
Definition: pgp-pgsql.c:128
int pgp_set_convert_crlf(PGP_Context *ctx, int doit)
Definition: pgp.c:249
int pgp_set_unicode_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:358
int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:319
int pgp_disable_mdc(PGP_Context *ctx, int disable)
Definition: pgp.c:235
int pgp_set_s2k_count(PGP_Context *ctx, int count)
Definition: pgp.c:275
long val
Definition: informix.c:689
int pgp_set_compress_algo(PGP_Context *ctx, int algo)
Definition: pgp.c:286
int use_sess_key
Definition: pgp-pgsql.c:129

◆ show_debug()

static void show_debug ( const char *  msg)
static

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

References ereport, errmsg(), and NOTICE.

Referenced by init_work().

176 {
177  ereport(NOTICE, (errmsg("dbg: %s", msg)));
178 }
#define ereport(elevel, rest)
Definition: elog.h:122
#define NOTICE
Definition: elog.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ string_is_ascii()

static bool string_is_ascii ( const char *  str)
static

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

References IS_HIGHBIT_SET.

Referenced by parse_key_value_arrays().

98 {
99  const char *p;
100 
101  for (p = str; *p; p++)
102  {
103  if (IS_HIGHBIT_SET(*p))
104  return false;
105  }
106  return true;
107 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:939