PostgreSQL Source Code  git master
pgp-pgsql.c
Go to the documentation of this file.
1 /*
2  * pgp-pgsql.c
3  * PostgreSQL wrappers for pgp.
4  *
5  * Copyright (c) 2005 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/pgp-pgsql.c
30  */
31 
32 #include "postgres.h"
33 
34 #include "catalog/pg_type.h"
35 #include "common/string.h"
36 #include "funcapi.h"
37 #include "lib/stringinfo.h"
38 #include "mb/pg_wchar.h"
39 #include "mbuf.h"
40 #include "pgp.h"
41 #include "px.h"
42 #include "utils/array.h"
43 #include "utils/builtins.h"
44 
45 /*
46  * public functions
47  */
52 
57 
59 
63 
64 /*
65  * returns src in case of no conversion or error
66  */
67 static text *
68 convert_charset(text *src, int cset_from, int cset_to)
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 }
83 
84 static text *
86 {
88 }
89 
90 static text *
92 {
94 }
95 
96 static void
98 {
99  px_memset(p, 0, VARSIZE_ANY(p));
100  pfree(p);
101 }
102 
103 /*
104  * expect-* arguments storage
105  */
107 {
108  int debug;
109  int expect;
111  int s2k_mode;
119 };
120 
121 static void
122 fill_expect(struct debug_expect *ex, int text_mode)
123 {
124  ex->debug = 0;
125  ex->expect = 0;
126  ex->cipher_algo = -1;
127  ex->s2k_mode = -1;
128  ex->s2k_count = -1;
129  ex->s2k_cipher_algo = -1;
130  ex->s2k_digest_algo = -1;
131  ex->compress_algo = -1;
132  ex->use_sess_key = -1;
133  ex->disable_mdc = -1;
134  ex->unicode_mode = -1;
135 }
136 
137 #define EX_MSG(arg) \
138  ereport(NOTICE, (errmsg( \
139  "pgp_decrypt: unexpected %s: expected %d got %d", \
140  CppAsString(arg), ex->arg, ctx->arg)))
141 
142 #define EX_CHECK(arg) do { \
143  if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
144  } while (0)
145 
146 static void
148 {
149  EX_CHECK(cipher_algo);
150  EX_CHECK(s2k_mode);
151  EX_CHECK(s2k_count);
152  EX_CHECK(s2k_digest_algo);
153  EX_CHECK(use_sess_key);
154  if (ctx->use_sess_key)
155  EX_CHECK(s2k_cipher_algo);
156  EX_CHECK(disable_mdc);
157  EX_CHECK(compress_algo);
158  EX_CHECK(unicode_mode);
159 }
160 
161 static void
162 show_debug(const char *msg)
163 {
164  ereport(NOTICE, (errmsg("dbg: %s", msg)));
165 }
166 
167 static int
168 set_arg(PGP_Context *ctx, char *key, char *val,
169  struct debug_expect *ex)
170 {
171  int res = 0;
172 
173  if (strcmp(key, "cipher-algo") == 0)
174  res = pgp_set_cipher_algo(ctx, val);
175  else if (strcmp(key, "disable-mdc") == 0)
176  res = pgp_disable_mdc(ctx, atoi(val));
177  else if (strcmp(key, "sess-key") == 0)
178  res = pgp_set_sess_key(ctx, atoi(val));
179  else if (strcmp(key, "s2k-mode") == 0)
180  res = pgp_set_s2k_mode(ctx, atoi(val));
181  else if (strcmp(key, "s2k-count") == 0)
182  res = pgp_set_s2k_count(ctx, atoi(val));
183  else if (strcmp(key, "s2k-digest-algo") == 0)
185  else if (strcmp(key, "s2k-cipher-algo") == 0)
187  else if (strcmp(key, "compress-algo") == 0)
188  res = pgp_set_compress_algo(ctx, atoi(val));
189  else if (strcmp(key, "compress-level") == 0)
190  res = pgp_set_compress_level(ctx, atoi(val));
191  else if (strcmp(key, "convert-crlf") == 0)
192  res = pgp_set_convert_crlf(ctx, atoi(val));
193  else if (strcmp(key, "unicode-mode") == 0)
194  res = pgp_set_unicode_mode(ctx, atoi(val));
195 
196  /*
197  * The remaining options are for debugging/testing and are therefore not
198  * documented in the user-facing docs.
199  */
200  else if (ex != NULL && strcmp(key, "debug") == 0)
201  ex->debug = atoi(val);
202  else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)
203  {
204  ex->expect = 1;
206  }
207  else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)
208  {
209  ex->expect = 1;
210  ex->disable_mdc = atoi(val);
211  }
212  else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)
213  {
214  ex->expect = 1;
215  ex->use_sess_key = atoi(val);
216  }
217  else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)
218  {
219  ex->expect = 1;
220  ex->s2k_mode = atoi(val);
221  }
222  else if (ex != NULL && strcmp(key, "expect-s2k-count") == 0)
223  {
224  ex->expect = 1;
225  ex->s2k_count = atoi(val);
226  }
227  else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)
228  {
229  ex->expect = 1;
231  }
232  else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)
233  {
234  ex->expect = 1;
236  }
237  else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)
238  {
239  ex->expect = 1;
240  ex->compress_algo = atoi(val);
241  }
242  else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)
243  {
244  ex->expect = 1;
245  ex->unicode_mode = atoi(val);
246  }
247  else
249 
250  return res;
251 }
252 
253 /*
254  * Find next word. Handle ',' and '=' as words. Skip whitespace.
255  * Put word info into res_p, res_len.
256  * Returns ptr to next word.
257  */
258 static char *
259 getword(char *p, char **res_p, int *res_len)
260 {
261  /* whitespace at start */
262  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
263  p++;
264 
265  /* word data */
266  *res_p = p;
267  if (*p == '=' || *p == ',')
268  p++;
269  else
270  while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
271  || *p == '=' || *p == ','))
272  p++;
273 
274  /* word end */
275  *res_len = p - *res_p;
276 
277  /* whitespace at end */
278  while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
279  p++;
280 
281  return p;
282 }
283 
284 /*
285  * Convert to lowercase asciiz string.
286  */
287 static char *
288 downcase_convert(const uint8 *s, int len)
289 {
290  int c,
291  i;
292  char *res = palloc(len + 1);
293 
294  for (i = 0; i < len; i++)
295  {
296  c = s[i];
297  if (c >= 'A' && c <= 'Z')
298  c += 'a' - 'A';
299  res[i] = c;
300  }
301  res[len] = 0;
302  return res;
303 }
304 
305 static int
306 parse_args(PGP_Context *ctx, uint8 *args, int arg_len,
307  struct debug_expect *ex)
308 {
309  char *str = downcase_convert(args, arg_len);
310  char *key,
311  *val;
312  int key_len,
313  val_len;
314  int res = 0;
315  char *p = str;
316 
317  while (*p)
318  {
320  p = getword(p, &key, &key_len);
321  if (*p++ != '=')
322  break;
323  p = getword(p, &val, &val_len);
324  if (*p == '\0')
325  ;
326  else if (*p++ != ',')
327  break;
328 
329  if (*key == 0 || *val == 0 || val_len == 0)
330  break;
331 
332  key[key_len] = 0;
333  val[val_len] = 0;
334 
335  res = set_arg(ctx, key, val, ex);
336  if (res < 0)
337  break;
338  }
339  pfree(str);
340  return res;
341 }
342 
343 static MBuf *
345 {
348 }
349 
350 static void
351 init_work(PGP_Context **ctx_p, int is_text,
352  text *args, struct debug_expect *ex)
353 {
354  int err = pgp_init(ctx_p);
355 
356  fill_expect(ex, is_text);
357 
358  if (err == 0 && args != NULL)
359  err = parse_args(*ctx_p, (uint8 *) VARDATA_ANY(args),
360  VARSIZE_ANY_EXHDR(args), ex);
361 
362  if (err)
364 
365  if (ex->debug)
367 
368  pgp_set_text_mode(*ctx_p, is_text);
369 }
370 
371 static bytea *
372 encrypt_internal(int is_pubenc, int is_text,
373  text *data, text *key, text *args)
374 {
375  MBuf *src,
376  *dst;
377  uint8 tmp[VARHDRSZ];
378  uint8 *restmp;
379  bytea *res;
380  int res_len;
381  PGP_Context *ctx;
382  int err;
383  struct debug_expect ex;
384  text *tmp_data = NULL;
385 
386  init_work(&ctx, is_text, args, &ex);
387 
388  if (is_text && pgp_get_unicode_mode(ctx))
389  {
390  tmp_data = convert_to_utf8(data);
391  if (tmp_data == data)
392  tmp_data = NULL;
393  else
394  data = tmp_data;
395  }
396 
398  dst = mbuf_create(VARSIZE_ANY(data) + 128);
399 
400  /*
401  * reserve room for header
402  */
403  mbuf_append(dst, tmp, VARHDRSZ);
404 
405  /*
406  * set key
407  */
408  if (is_pubenc)
409  {
411 
412  err = pgp_set_pubkey(ctx, kbuf,
413  NULL, 0, 0);
414  mbuf_free(kbuf);
415  }
416  else
417  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
419 
420  /*
421  * encrypt
422  */
423  if (err >= 0)
424  err = pgp_encrypt(ctx, src, dst);
425 
426  /*
427  * check for error
428  */
429  if (err)
430  {
431  if (ex.debug)
432  px_set_debug_handler(NULL);
433  if (tmp_data)
434  clear_and_pfree(tmp_data);
435  pgp_free(ctx);
436  mbuf_free(src);
437  mbuf_free(dst);
439  }
440 
441  /* res_len includes VARHDRSZ */
442  res_len = mbuf_steal_data(dst, &restmp);
443  res = (bytea *) restmp;
444  SET_VARSIZE(res, res_len);
445 
446  if (tmp_data)
447  clear_and_pfree(tmp_data);
448  pgp_free(ctx);
449  mbuf_free(src);
450  mbuf_free(dst);
451 
452  px_set_debug_handler(NULL);
453 
454  return res;
455 }
456 
457 static bytea *
458 decrypt_internal(int is_pubenc, int need_text, text *data,
459  text *key, text *keypsw, text *args)
460 {
461  int err;
462  MBuf *src = NULL,
463  *dst = NULL;
464  uint8 tmp[VARHDRSZ];
465  uint8 *restmp;
466  bytea *res;
467  int res_len;
468  PGP_Context *ctx = NULL;
469  struct debug_expect ex;
470  int got_unicode = 0;
471 
472 
473  init_work(&ctx, need_text, args, &ex);
474 
477  dst = mbuf_create(VARSIZE_ANY(data) + 2048);
478 
479  /*
480  * reserve room for header
481  */
482  mbuf_append(dst, tmp, VARHDRSZ);
483 
484  /*
485  * set key
486  */
487  if (is_pubenc)
488  {
489  uint8 *psw = NULL;
490  int psw_len = 0;
491  MBuf *kbuf;
492 
493  if (keypsw)
494  {
495  psw = (uint8 *) VARDATA_ANY(keypsw);
496  psw_len = VARSIZE_ANY_EXHDR(keypsw);
497  }
499  err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
500  mbuf_free(kbuf);
501  }
502  else
503  err = pgp_set_symkey(ctx, (uint8 *) VARDATA_ANY(key),
505 
506  /* decrypt */
507  if (err >= 0)
508  {
509  err = pgp_decrypt(ctx, src, dst);
510 
511  if (ex.expect)
512  check_expect(ctx, &ex);
513 
514  /* remember the setting */
515  got_unicode = pgp_get_unicode_mode(ctx);
516  }
517 
518  mbuf_free(src);
519  pgp_free(ctx);
520 
521  if (err)
522  {
523  px_set_debug_handler(NULL);
524  mbuf_free(dst);
526  }
527 
528  res_len = mbuf_steal_data(dst, &restmp);
529  mbuf_free(dst);
530 
531  /* res_len includes VARHDRSZ */
532  res = (bytea *) restmp;
533  SET_VARSIZE(res, res_len);
534 
535  if (need_text && got_unicode)
536  {
537  text *utf = convert_from_utf8(res);
538 
539  if (utf != res)
540  {
542  res = utf;
543  }
544  }
545  px_set_debug_handler(NULL);
546 
547  return res;
548 }
549 
550 /*
551  * Wrappers for symmetric-key functions
552  */
553 Datum
555 {
556  bytea *data;
557  text *arg = NULL;
558  text *res,
559  *key;
560 
562  key = PG_GETARG_TEXT_PP(1);
563  if (PG_NARGS() > 2)
564  arg = PG_GETARG_TEXT_PP(2);
565 
566  res = encrypt_internal(0, 0, data, key, arg);
567 
568  PG_FREE_IF_COPY(data, 0);
569  PG_FREE_IF_COPY(key, 1);
570  if (PG_NARGS() > 2)
571  PG_FREE_IF_COPY(arg, 2);
573 }
574 
575 Datum
577 {
578  text *data,
579  *key;
580  text *arg = NULL;
581  text *res;
582 
583  data = PG_GETARG_TEXT_PP(0);
584  key = PG_GETARG_TEXT_PP(1);
585  if (PG_NARGS() > 2)
586  arg = PG_GETARG_TEXT_PP(2);
587 
588  res = encrypt_internal(0, 1, data, key, arg);
589 
590  PG_FREE_IF_COPY(data, 0);
591  PG_FREE_IF_COPY(key, 1);
592  if (PG_NARGS() > 2)
593  PG_FREE_IF_COPY(arg, 2);
595 }
596 
597 
598 Datum
600 {
601  bytea *data;
602  text *arg = NULL;
603  text *res,
604  *key;
605 
607  key = PG_GETARG_TEXT_PP(1);
608  if (PG_NARGS() > 2)
609  arg = PG_GETARG_TEXT_PP(2);
610 
611  res = decrypt_internal(0, 0, data, key, NULL, arg);
612 
613  PG_FREE_IF_COPY(data, 0);
614  PG_FREE_IF_COPY(key, 1);
615  if (PG_NARGS() > 2)
616  PG_FREE_IF_COPY(arg, 2);
618 }
619 
620 Datum
622 {
623  bytea *data;
624  text *arg = NULL;
625  text *res,
626  *key;
627 
629  key = PG_GETARG_TEXT_PP(1);
630  if (PG_NARGS() > 2)
631  arg = PG_GETARG_TEXT_PP(2);
632 
633  res = decrypt_internal(0, 1, data, key, NULL, arg);
634 
635  PG_FREE_IF_COPY(data, 0);
636  PG_FREE_IF_COPY(key, 1);
637  if (PG_NARGS() > 2)
638  PG_FREE_IF_COPY(arg, 2);
640 }
641 
642 /*
643  * Wrappers for public-key functions
644  */
645 
646 Datum
648 {
649  bytea *data,
650  *key;
651  text *arg = NULL;
652  text *res;
653 
655  key = PG_GETARG_BYTEA_PP(1);
656  if (PG_NARGS() > 2)
657  arg = PG_GETARG_TEXT_PP(2);
658 
659  res = encrypt_internal(1, 0, data, key, arg);
660 
661  PG_FREE_IF_COPY(data, 0);
662  PG_FREE_IF_COPY(key, 1);
663  if (PG_NARGS() > 2)
664  PG_FREE_IF_COPY(arg, 2);
666 }
667 
668 Datum
670 {
671  bytea *key;
672  text *arg = NULL;
673  text *res,
674  *data;
675 
676  data = PG_GETARG_TEXT_PP(0);
677  key = PG_GETARG_BYTEA_PP(1);
678  if (PG_NARGS() > 2)
679  arg = PG_GETARG_TEXT_PP(2);
680 
681  res = encrypt_internal(1, 1, data, key, arg);
682 
683  PG_FREE_IF_COPY(data, 0);
684  PG_FREE_IF_COPY(key, 1);
685  if (PG_NARGS() > 2)
686  PG_FREE_IF_COPY(arg, 2);
688 }
689 
690 
691 Datum
693 {
694  bytea *data,
695  *key;
696  text *psw = NULL,
697  *arg = NULL;
698  text *res;
699 
701  key = PG_GETARG_BYTEA_PP(1);
702  if (PG_NARGS() > 2)
703  psw = PG_GETARG_TEXT_PP(2);
704  if (PG_NARGS() > 3)
705  arg = PG_GETARG_TEXT_PP(3);
706 
707  res = decrypt_internal(1, 0, data, key, psw, arg);
708 
709  PG_FREE_IF_COPY(data, 0);
710  PG_FREE_IF_COPY(key, 1);
711  if (PG_NARGS() > 2)
712  PG_FREE_IF_COPY(psw, 2);
713  if (PG_NARGS() > 3)
714  PG_FREE_IF_COPY(arg, 3);
716 }
717 
718 Datum
720 {
721  bytea *data,
722  *key;
723  text *psw = NULL,
724  *arg = NULL;
725  text *res;
726 
728  key = PG_GETARG_BYTEA_PP(1);
729  if (PG_NARGS() > 2)
730  psw = PG_GETARG_TEXT_PP(2);
731  if (PG_NARGS() > 3)
732  arg = PG_GETARG_TEXT_PP(3);
733 
734  res = decrypt_internal(1, 1, data, key, psw, arg);
735 
736  PG_FREE_IF_COPY(data, 0);
737  PG_FREE_IF_COPY(key, 1);
738  if (PG_NARGS() > 2)
739  PG_FREE_IF_COPY(psw, 2);
740  if (PG_NARGS() > 3)
741  PG_FREE_IF_COPY(arg, 3);
743 }
744 
745 
746 /*
747  * Wrappers for PGP ascii armor
748  */
749 
750 /*
751  * Helper function for pg_armor. Converts arrays of keys and values into
752  * plain C arrays, and checks that they don't contain invalid characters.
753  */
754 static int
756  char ***p_keys, char ***p_values)
757 {
758  int nkdims = ARR_NDIM(key_array);
759  int nvdims = ARR_NDIM(val_array);
760  char **keys,
761  **values;
762  Datum *key_datums,
763  *val_datums;
764  bool *key_nulls,
765  *val_nulls;
766  int key_count,
767  val_count;
768  int i;
769 
770  if (nkdims > 1 || nkdims != nvdims)
771  ereport(ERROR,
772  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
773  errmsg("wrong number of array subscripts")));
774  if (nkdims == 0)
775  return 0;
776 
777  deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
778  deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
779 
780  if (key_count != val_count)
781  ereport(ERROR,
782  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
783  errmsg("mismatched array dimensions")));
784 
785  keys = (char **) palloc(sizeof(char *) * key_count);
786  values = (char **) palloc(sizeof(char *) * val_count);
787 
788  for (i = 0; i < key_count; i++)
789  {
790  char *v;
791 
792  /* Check that the key doesn't contain anything funny */
793  if (key_nulls[i])
794  ereport(ERROR,
795  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
796  errmsg("null value not allowed for header key")));
797 
798  v = TextDatumGetCString(key_datums[i]);
799 
800  if (!pg_is_ascii(v))
801  ereport(ERROR,
802  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
803  errmsg("header key must not contain non-ASCII characters")));
804  if (strstr(v, ": "))
805  ereport(ERROR,
806  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
807  errmsg("header key must not contain \": \"")));
808  if (strchr(v, '\n'))
809  ereport(ERROR,
810  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
811  errmsg("header key must not contain newlines")));
812  keys[i] = v;
813 
814  /* And the same for the value */
815  if (val_nulls[i])
816  ereport(ERROR,
817  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
818  errmsg("null value not allowed for header value")));
819 
820  v = TextDatumGetCString(val_datums[i]);
821 
822  if (!pg_is_ascii(v))
823  ereport(ERROR,
824  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
825  errmsg("header value must not contain non-ASCII characters")));
826  if (strchr(v, '\n'))
827  ereport(ERROR,
828  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
829  errmsg("header value must not contain newlines")));
830 
831  values[i] = v;
832  }
833 
834  *p_keys = keys;
835  *p_values = values;
836  return key_count;
837 }
838 
839 Datum
841 {
842  bytea *data;
843  text *res;
844  int data_len;
846  int num_headers;
847  char **keys = NULL,
848  **values = NULL;
849 
851  data_len = VARSIZE_ANY_EXHDR(data);
852  if (PG_NARGS() == 3)
853  {
856  &keys, &values);
857  }
858  else if (PG_NARGS() == 1)
859  num_headers = 0;
860  else
861  elog(ERROR, "unexpected number of arguments %d", PG_NARGS());
862 
864 
865  pgp_armor_encode((uint8 *) VARDATA_ANY(data), data_len, &buf,
866  num_headers, keys, values);
867 
868  res = palloc(VARHDRSZ + buf.len);
869  SET_VARSIZE(res, VARHDRSZ + buf.len);
870  memcpy(VARDATA(res), buf.data, buf.len);
871  pfree(buf.data);
872 
873  PG_FREE_IF_COPY(data, 0);
875 }
876 
877 Datum
879 {
880  text *data;
881  bytea *res;
882  int data_len;
883  int ret;
885 
886  data = PG_GETARG_TEXT_PP(0);
887  data_len = VARSIZE_ANY_EXHDR(data);
888 
890 
891  ret = pgp_armor_decode((uint8 *) VARDATA_ANY(data), data_len, &buf);
892  if (ret < 0)
893  px_THROW_ERROR(ret);
894  res = palloc(VARHDRSZ + buf.len);
895  SET_VARSIZE(res, VARHDRSZ + buf.len);
896  memcpy(VARDATA(res), buf.data, buf.len);
897  pfree(buf.data);
898 
899  PG_FREE_IF_COPY(data, 0);
901 }
902 
903 /* cross-call state for pgp_armor_headers */
904 typedef struct
905 {
906  int nheaders;
907  char **keys;
908  char **values;
910 
911 Datum
913 {
914  FuncCallContext *funcctx;
916  char *utf8key;
917  char *utf8val;
918  HeapTuple tuple;
919  TupleDesc tupdesc;
920  AttInMetadata *attinmeta;
921 
922  if (SRF_IS_FIRSTCALL())
923  {
925  int res;
926  MemoryContext oldcontext;
927 
928  funcctx = SRF_FIRSTCALL_INIT();
929 
930  /* we need the state allocated in the multi call context */
931  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
932 
933  /* Build a tuple descriptor for our result type */
934  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
935  elog(ERROR, "return type must be a row type");
936 
937  attinmeta = TupleDescGetAttInMetadata(tupdesc);
938  funcctx->attinmeta = attinmeta;
939 
941 
944  &state->nheaders, &state->keys,
945  &state->values);
946  if (res < 0)
948 
949  MemoryContextSwitchTo(oldcontext);
950  funcctx->user_fctx = state;
951  }
952 
953  funcctx = SRF_PERCALL_SETUP();
954  state = (pgp_armor_headers_state *) funcctx->user_fctx;
955 
956  if (funcctx->call_cntr >= state->nheaders)
957  SRF_RETURN_DONE(funcctx);
958  else
959  {
960  char *values[2];
961 
962  /* we assume that the keys (and values) are in UTF-8. */
963  utf8key = state->keys[funcctx->call_cntr];
964  utf8val = state->values[funcctx->call_cntr];
965 
966  values[0] = pg_any_to_server(utf8key, strlen(utf8key), PG_UTF8);
967  values[1] = pg_any_to_server(utf8val, strlen(utf8val), PG_UTF8);
968 
969  /* build a tuple */
970  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
971  SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
972  }
973 }
974 
975 
976 
977 /*
978  * Wrappers for PGP key id
979  */
980 
981 Datum
983 {
984  bytea *data;
985  text *res;
986  int res_len;
987  MBuf *buf;
988 
991  res = palloc(VARHDRSZ + 17);
992 
993  res_len = pgp_get_keyid(buf, VARDATA(res));
994  mbuf_free(buf);
995  if (res_len < 0)
996  px_THROW_ERROR(res_len);
997  SET_VARSIZE(res, VARHDRSZ + res_len);
998 
999  PG_FREE_IF_COPY(data, 0);
1001 }
#define ARR_NDIM(a)
Definition: array.h:290
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3678
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define VARHDRSZ
Definition: c.h:679
unsigned char uint8
Definition: c.h:491
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define NOTICE
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:149
void err(int eval, const char *fmt,...)
Definition: err.c:43
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2134
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2085
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
long val
Definition: informix.c:664
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
MBuf * mbuf_create_from_data(uint8 *data, int len)
Definition: mbuf.c:131
int mbuf_free(MBuf *mbuf)
Definition: mbuf.c:62
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
Definition: mbuf.c:94
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
Definition: mbuf.c:162
MBuf * mbuf_create(int len)
Definition: mbuf.c:111
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:356
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc(Size size)
Definition: mcxt.c:1304
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
const void size_t len
const void * data
static char * buf
Definition: pg_test_fsync.c:73
@ PG_UTF8
Definition: pg_wchar.h:232
int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
Definition: pgp-armor.c:390
int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)
Definition: pgp-armor.c:314
void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)
Definition: pgp-armor.c:207
int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)
Definition: pgp-decrypt.c:1093
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-encrypt.c:599
int pgp_get_keyid(MBuf *pgp_data, char *dst)
Definition: pgp-info.c:112
static text * convert_charset(text *src, int cset_from, int cset_to)
Definition: pgp-pgsql.c:68
static text * convert_to_utf8(text *src)
Definition: pgp-pgsql.c:91
static void fill_expect(struct debug_expect *ex, int text_mode)
Definition: pgp-pgsql.c:122
Datum pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:692
Datum pgp_sym_decrypt_text(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:621
static char * getword(char *p, char **res_p, int *res_len)
Definition: pgp-pgsql.c:259
Datum pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:554
Datum pg_armor(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:840
Datum pg_dearmor(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:878
Datum pgp_pub_encrypt_text(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:669
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
Definition: pgp-pgsql.c:351
static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
Definition: pgp-pgsql.c:147
Datum pgp_key_id_w(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:982
static text * convert_from_utf8(text *src)
Definition: pgp-pgsql.c:85
Datum pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:599
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
Definition: pgp-pgsql.c:458
static void show_debug(const char *msg)
Definition: pgp-pgsql.c:162
static char * downcase_convert(const uint8 *s, int len)
Definition: pgp-pgsql.c:288
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
Definition: pgp-pgsql.c:306
Datum pgp_sym_encrypt_text(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:576
Datum pgp_armor_headers(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:912
Datum pgp_pub_decrypt_text(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:719
static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
Definition: pgp-pgsql.c:755
static MBuf * create_mbuf_from_vardata(text *data)
Definition: pgp-pgsql.c:344
PG_FUNCTION_INFO_V1(pgp_sym_encrypt_bytea)
#define EX_CHECK(arg)
Definition: pgp-pgsql.c:142
static int set_arg(PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
Definition: pgp-pgsql.c:168
static void clear_and_pfree(text *p)
Definition: pgp-pgsql.c:97
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
Definition: pgp-pgsql.c:372
Datum pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS)
Definition: pgp-pgsql.c:647
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Definition: pgp-pubkey.c:565
int pgp_disable_mdc(PGP_Context *ctx, int disable)
Definition: pgp.c:223
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:115
int pgp_init(PGP_Context **ctx_p)
Definition: pgp.c:189
int pgp_set_text_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:300
int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:329
int pgp_set_s2k_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:244
int pgp_get_digest_code(const char *name)
Definition: pgp.c:104
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
Definition: pgp.c:353
int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:318
int pgp_set_compress_algo(PGP_Context *ctx, int algo)
Definition: pgp.c:274
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:340
int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:307
int pgp_set_convert_crlf(PGP_Context *ctx, int doit)
Definition: pgp.c:237
int pgp_set_unicode_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:346
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:213
int pgp_set_s2k_count(PGP_Context *ctx, int count)
Definition: pgp.c:263
int pgp_set_sess_key(PGP_Context *ctx, int use)
Definition: pgp.c:230
int pgp_set_compress_level(PGP_Context *ctx, int level)
Definition: pgp.c:289
uintptr_t Datum
Definition: postgres.h:64
char * c
void px_THROW_ERROR(int err)
Definition: px.c:93
void px_set_debug_handler(void(*handler)(const char *))
Definition: px.c:143
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
#define PXE_ARGUMENT_ERROR
Definition: px.h:59
bool pg_is_ascii(const char *str)
Definition: string.c:133
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
void * user_fctx
Definition: funcapi.h:82
uint64 call_cntr
Definition: funcapi.h:65
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
Definition: mbuf.c:40
int use_sess_key
Definition: pgp.h:148
int s2k_digest_algo
Definition: pgp-pgsql.c:114
int cipher_algo
Definition: pgp-pgsql.c:110
int compress_algo
Definition: pgp-pgsql.c:115
int unicode_mode
Definition: pgp-pgsql.c:118
int disable_mdc
Definition: pgp-pgsql.c:117
int s2k_cipher_algo
Definition: pgp-pgsql.c:113
int use_sess_key
Definition: pgp-pgsql.c:116
Definition: regguts.h:323
Definition: c.h:674
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
text * cstring_to_text(const char *s)
Definition: varlena.c:184