PostgreSQL Source Code  git master
pgp-pgsql.c File Reference
#include "postgres.h"
#include "catalog/pg_type.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 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:149
void * arg

Definition at line 154 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:141
#define NOTICE
Definition: elog.h:37
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define CppAsString(identifier)
Definition: c.h:223
void * arg

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

Function Documentation

◆ check_expect()

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

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

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

◆ clear_and_pfree()

static void clear_and_pfree ( text p)
static

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

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

Referenced by decrypt_internal(), and encrypt_internal().

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

◆ convert_charset()

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

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

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

◆ convert_from_utf8()

static text* convert_from_utf8 ( text src)
static

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

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

Referenced by decrypt_internal().

85 {
87 }
static text * convert_charset(text *src, int cset_from, int cset_to)
Definition: pgp-pgsql.c:67
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046

◆ convert_to_utf8()

static text* convert_to_utf8 ( text src)
static

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

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

Referenced by encrypt_internal().

91 {
93 }
static text * convert_charset(text *src, int cset_from, int cset_to)
Definition: pgp-pgsql.c:67
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046

◆ create_mbuf_from_vardata()

static MBuf* create_mbuf_from_vardata ( text data)
static

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

357 {
358  return mbuf_create_from_data((uint8 *) VARDATA_ANY(data),
359  VARSIZE_ANY_EXHDR(data));
360 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned char uint8
Definition: c.h:357
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
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 470 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().

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

◆ downcase_convert()

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

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

References i, and palloc().

Referenced by parse_args().

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

◆ encrypt_internal()

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

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

386 {
387  MBuf *src,
388  *dst;
389  uint8 tmp[VARHDRSZ];
390  uint8 *restmp;
391  bytea *res;
392  int res_len;
393  PGP_Context *ctx;
394  int err;
395  struct debug_expect ex;
396  text *tmp_data = NULL;
397 
398  init_work(&ctx, is_text, args, &ex);
399 
400  if (is_text && pgp_get_unicode_mode(ctx))
401  {
402  tmp_data = convert_to_utf8(data);
403  if (tmp_data == data)
404  tmp_data = NULL;
405  else
406  data = tmp_data;
407  }
408 
409  src = create_mbuf_from_vardata(data);
410  dst = mbuf_create(VARSIZE_ANY(data) + 128);
411 
412  /*
413  * reserve room for header
414  */
415  mbuf_append(dst, tmp, VARHDRSZ);
416 
417  /*
418  * set key
419  */
420  if (is_pubenc)
421  {
422  MBuf *kbuf = create_mbuf_from_vardata(key);
423 
424  err = pgp_set_pubkey(ctx, kbuf,
425  NULL, 0, 0);
426  mbuf_free(kbuf);
427  }
428  else
429  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
430  VARSIZE_ANY_EXHDR(key));
431 
432  /*
433  * encrypt
434  */
435  if (err >= 0)
436  err = pgp_encrypt(ctx, src, dst);
437 
438  /*
439  * check for error
440  */
441  if (err)
442  {
443  if (ex.debug)
444  px_set_debug_handler(NULL);
445  if (tmp_data)
446  clear_and_pfree(tmp_data);
447  pgp_free(ctx);
448  mbuf_free(src);
449  mbuf_free(dst);
450  px_THROW_ERROR(err);
451  }
452 
453  /* res_len includes VARHDRSZ */
454  res_len = mbuf_steal_data(dst, &restmp);
455  res = (bytea *) restmp;
456  SET_VARSIZE(res, res_len);
457 
458  if (tmp_data)
459  clear_and_pfree(tmp_data);
460  pgp_free(ctx);
461  mbuf_free(src);
462  mbuf_free(dst);
463 
464  px_set_debug_handler(NULL);
465 
466  return res;
467 }
MBuf * mbuf_create(int len)
Definition: mbuf.c:117
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:351
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:109
#define VARHDRSZ
Definition: c.h:562
unsigned char uint8
Definition: c.h:357
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:68
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
Definition: pgp.c:364
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:146
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:100
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-encrypt.c:600
Definition: mbuf.c:39
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:356
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:224
static text * convert_to_utf8(text *src)
Definition: pgp-pgsql.c:90
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:363

◆ fill_expect()

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

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

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

◆ getword()

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

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

Referenced by parse_args().

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

◆ init_work()

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

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

365 {
366  int err = pgp_init(ctx_p);
367 
368  fill_expect(ex, is_text);
369 
370  if (err == 0 && args != NULL)
371  err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
372  VARSIZE_ANY_EXHDR(args), ex);
373 
374  if (err)
375  px_THROW_ERROR(err);
376 
377  if (ex->debug)
379 
380  pgp_set_text_mode(*ctx_p, is_text);
381 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
unsigned char uint8
Definition: c.h:357
static void fill_expect(struct debug_expect *ex, int text_mode)
Definition: pgp-pgsql.c:134
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:146
int pgp_init(PGP_Context **ctx_p)
Definition: pgp.c:199
static void show_debug(const char *msg)
Definition: pgp-pgsql.c:174
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
Definition: pgp-pgsql.c:318
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int pgp_set_text_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:311

◆ parse_args()

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

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

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

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

Referenced by pg_armor().

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

◆ pg_armor()

Datum pg_armor ( PG_FUNCTION_ARGS  )

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

858 {
859  bytea *data;
860  text *res;
861  int data_len;
863  int num_headers;
864  char **keys = NULL,
865  **values = NULL;
866 
867  data = PG_GETARG_BYTEA_PP(0);
868  data_len = VARSIZE_ANY_EXHDR(data);
869  if (PG_NARGS() == 3)
870  {
873  &keys, &values);
874  }
875  else if (PG_NARGS() == 1)
876  num_headers = 0;
877  else
878  elog(ERROR, "unexpected number of arguments %d", PG_NARGS());
879 
880  initStringInfo(&buf);
881 
882  pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
883  num_headers, keys, values);
884 
885  res = palloc(VARHDRSZ + buf.len);
886  SET_VARSIZE(res, VARHDRSZ + buf.len);
887  memcpy(VARDATA(res), buf.data, buf.len);
888  pfree(buf.data);
889 
890  PG_FREE_IF_COPY(data, 0);
891  PG_RETURN_TEXT_P(res);
892 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:562
unsigned char uint8
Definition: c.h:357
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
void pfree(void *pointer)
Definition: mcxt.c:1056
#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:59
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define PG_NARGS()
Definition: fmgr.h:198
static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
Definition: pgp-pgsql.c:767
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ pg_dearmor()

Datum pg_dearmor ( PG_FUNCTION_ARGS  )

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

896 {
897  text *data;
898  bytea *res;
899  int data_len;
900  int ret;
902 
903  data = PG_GETARG_TEXT_PP(0);
904  data_len = VARSIZE_ANY_EXHDR(data);
905 
906  initStringInfo(&buf);
907 
908  ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
909  if (ret < 0)
910  px_THROW_ERROR(ret);
911  res = palloc(VARHDRSZ + buf.len);
912  SET_VARSIZE(res, VARHDRSZ + buf.len);
913  memcpy(VARDATA(res), buf.data, buf.len);
914  pfree(buf.data);
915 
916  PG_FREE_IF_COPY(data, 0);
917  PG_RETURN_TEXT_P(res);
918 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:562
unsigned char uint8
Definition: c.h:357
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pfree(void *pointer)
Definition: mcxt.c:1056
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:59
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

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

930 {
931  FuncCallContext *funcctx;
933  char *utf8key;
934  char *utf8val;
935  HeapTuple tuple;
936  TupleDesc tupdesc;
937  AttInMetadata *attinmeta;
938 
939  if (SRF_IS_FIRSTCALL())
940  {
941  text *data = PG_GETARG_TEXT_PP(0);
942  int res;
943  MemoryContext oldcontext;
944 
945  funcctx = SRF_FIRSTCALL_INIT();
946 
947  /* we need the state allocated in the multi call context */
948  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
949 
950  /* Build a tuple descriptor for our result type */
951  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
952  elog(ERROR, "return type must be a row type");
953 
954  attinmeta = TupleDescGetAttInMetadata(tupdesc);
955  funcctx->attinmeta = attinmeta;
956 
958 
960  VARSIZE_ANY_EXHDR(data),
961  &state->nheaders, &state->keys,
962  &state->values);
963  if (res < 0)
964  px_THROW_ERROR(res);
965 
966  MemoryContextSwitchTo(oldcontext);
967  funcctx->user_fctx = state;
968  }
969 
970  funcctx = SRF_PERCALL_SETUP();
971  state = (pgp_armor_headers_state *) funcctx->user_fctx;
972 
973  if (funcctx->call_cntr >= state->nheaders)
974  SRF_RETURN_DONE(funcctx);
975  else
976  {
977  char *values[2];
978 
979  /* we assume that the keys (and values) are in UTF-8. */
980  utf8key = state->keys[funcctx->call_cntr];
981  utf8val = state->values[funcctx->call_cntr];
982 
983  values[0] = pg_any_to_server(utf8key, strlen(utf8key), PG_UTF8);
984  values[1] = pg_any_to_server(utf8val, strlen(utf8val), PG_UTF8);
985 
986  /* build a tuple */
987  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
988  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
989  }
990 }
uint64 call_cntr
Definition: funcapi.h:65
void px_THROW_ERROR(int err)
Definition: px.c:96
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:282
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned char uint8
Definition: c.h:357
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:286
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:288
#define ERROR
Definition: elog.h:43
AttInMetadata * attinmeta
Definition: funcapi.h:91
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
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:101
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:556
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:306
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:284

◆ pgp_key_id_w()

Datum pgp_key_id_w ( PG_FUNCTION_ARGS  )

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

1000 {
1001  bytea *data;
1002  text *res;
1003  int res_len;
1004  MBuf *buf;
1005 
1006  data = PG_GETARG_BYTEA_PP(0);
1007  buf = create_mbuf_from_vardata(data);
1008  res = palloc(VARHDRSZ + 17);
1009 
1010  res_len = pgp_get_keyid(buf, VARDATA(res));
1011  mbuf_free(buf);
1012  if (res_len < 0)
1013  px_THROW_ERROR(res_len);
1014  SET_VARSIZE(res, VARHDRSZ + res_len);
1015 
1016  PG_FREE_IF_COPY(data, 0);
1017  PG_RETURN_TEXT_P(res);
1018 }
void px_THROW_ERROR(int err)
Definition: px.c:96
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:562
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:361
Definition: mbuf.c:39
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:556
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:356
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ pgp_pub_decrypt_bytea()

Datum pgp_pub_decrypt_bytea ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_pub_decrypt_text()

Datum pgp_pub_decrypt_text ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_pub_encrypt_bytea()

Datum pgp_pub_encrypt_bytea ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_pub_encrypt_text()

Datum pgp_pub_encrypt_text ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_sym_decrypt_bytea()

Datum pgp_sym_decrypt_bytea ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_sym_decrypt_text()

Datum pgp_sym_decrypt_text ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_sym_encrypt_bytea()

Datum pgp_sym_encrypt_bytea ( PG_FUNCTION_ARGS  )

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

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

◆ pgp_sym_encrypt_text()

Datum pgp_sym_encrypt_text ( PG_FUNCTION_ARGS  )

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

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

◆ set_arg()

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

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

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

◆ show_debug()

static void show_debug ( const char *  msg)
static

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

References ereport, errmsg(), and NOTICE.

Referenced by init_work().

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

◆ string_is_ascii()

static bool string_is_ascii ( const char *  str)
static

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

References IS_HIGHBIT_SET.

Referenced by parse_key_value_arrays().

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