91 if (!data1 && !data2 && !data3)
108 if (data1 && rnd[0] >= 32)
112 if (data2 && rnd[1] >= 160)
116 if (data3 && rnd[2] >= 160)
131 unsigned char *csrc = (
unsigned char *)
VARDATA(src);
160 for (p = str; *p; p++)
209 #define EX_MSG(arg) \
210 ereport(NOTICE, (errmsg( \
211 "pgp_decrypt: unexpected %s: expected %d got %d", \
212 CppAsString(arg), ex->arg, ctx->arg)))
214 #define EX_CHECK(arg) do { \
215 if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
245 if (strcmp(key,
"cipher-algo") == 0)
247 else if (strcmp(key,
"disable-mdc") == 0)
249 else if (strcmp(key,
"sess-key") == 0)
251 else if (strcmp(key,
"s2k-mode") == 0)
253 else if (strcmp(key,
"s2k-count") == 0)
255 else if (strcmp(key,
"s2k-digest-algo") == 0)
257 else if (strcmp(key,
"s2k-cipher-algo") == 0)
259 else if (strcmp(key,
"compress-algo") == 0)
261 else if (strcmp(key,
"compress-level") == 0)
263 else if (strcmp(key,
"convert-crlf") == 0)
265 else if (strcmp(key,
"unicode-mode") == 0)
272 else if (ex !=
NULL && strcmp(key,
"debug") == 0)
273 ex->
debug = atoi(val);
274 else if (ex !=
NULL && strcmp(key,
"expect-cipher-algo") == 0)
279 else if (ex !=
NULL && strcmp(key,
"expect-disable-mdc") == 0)
284 else if (ex !=
NULL && strcmp(key,
"expect-sess-key") == 0)
289 else if (ex !=
NULL && strcmp(key,
"expect-s2k-mode") == 0)
294 else if (ex !=
NULL && strcmp(key,
"expect-s2k-count") == 0)
299 else if (ex !=
NULL && strcmp(key,
"expect-s2k-digest-algo") == 0)
304 else if (ex !=
NULL && strcmp(key,
"expect-s2k-cipher-algo") == 0)
309 else if (ex !=
NULL && strcmp(key,
"expect-compress-algo") == 0)
314 else if (ex !=
NULL && strcmp(key,
"expect-unicode-mode") == 0)
334 while (*p && (*p ==
' ' || *p ==
'\t' || *p ==
'\n'))
339 if (*p ==
'=' || *p ==
',')
342 while (*p && !(*p ==
' ' || *p ==
'\t' || *p ==
'\n'
343 || *p ==
'=' || *p ==
','))
347 *res_len = p - *res_p;
350 while (*p && (*p ==
' ' || *p ==
'\t' || *p ==
'\n'))
364 char *res =
palloc(len + 1);
366 for (i = 0; i < len; i++)
369 if (c >=
'A' && c <=
'Z')
392 p =
getword(p, &key, &key_len);
395 p =
getword(p, &val, &val_len);
398 else if (*p++ !=
',')
401 if (*key == 0 || *val == 0 || val_len == 0)
407 res =
set_arg(ctx, key, val, ex);
430 if (err == 0 && args !=
NULL)
437 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
472 if (tmp_data == data)
520 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
526 res = (
bytea *) restmp;
609 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
617 res = (
bytea *) restmp;
620 if (need_text && got_unicode)
846 char ***p_keys,
char ***p_values)
860 if (nkdims > 1 || nkdims != nvdims)
862 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
863 errmsg(
"wrong number of array subscripts")));
869 &key_datums, &key_nulls, &key_count);
873 &val_datums, &val_nulls, &val_count);
875 if (key_count != val_count)
877 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
878 errmsg(
"mismatched array dimensions")));
880 keys = (
char **)
palloc(
sizeof(
char *) * key_count);
881 values = (
char **)
palloc(
sizeof(
char *) * val_count);
883 for (i = 0; i < key_count; i++)
890 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
891 errmsg(
"null value not allowed for header key")));
897 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
898 errmsg(
"header key must not contain non-ASCII characters")));
901 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
902 errmsg(
"header key must not contain \": \"")));
905 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
906 errmsg(
"header key must not contain newlines")));
912 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
913 errmsg(
"null value not allowed for header value")));
919 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
920 errmsg(
"header value must not contain non-ASCII characters")));
923 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
924 errmsg(
"header value must not contain newlines")));
961 num_headers, keys, values);
989 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
1032 elog(
ERROR,
"return type must be a row type");
1045 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
1096 (
errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
Datum pgp_key_id_w(PG_FUNCTION_ARGS)
Datum pg_armor(PG_FUNCTION_ARGS)
static void add_block_entropy(PX_MD *md, text *data)
PG_FUNCTION_INFO_V1(pgp_sym_encrypt_bytea)
MBuf * mbuf_create(int len)
static char * downcase_convert(const uint8 *s, int len)
#define PXE_ARGUMENT_ERROR
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
int pgp_get_cipher_code(const char *name)
int pgp_get_unicode_mode(PGP_Context *ctx)
int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)
Datum pgp_pub_encrypt_text(PG_FUNCTION_ARGS)
Datum pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS)
int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
int pgp_set_s2k_mode(PGP_Context *ctx, int mode)
#define SRF_IS_FIRSTCALL()
static void clear_and_pfree(text *p)
Datum pgp_pub_decrypt_text(PG_FUNCTION_ARGS)
#define px_md_update(md, data, dlen)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static text * convert_charset(text *src, int cset_from, int cset_to)
int errcode(int sqlerrcode)
int mbuf_free(MBuf *mbuf)
Datum pg_dearmor(PG_FUNCTION_ARGS)
int pgp_get_keyid(MBuf *pgp_data, char *dst)
int pgp_get_digest_code(const char *name)
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
#define px_md_finish(md, buf)
#define SRF_PERCALL_SETUP()
static void fill_expect(struct debug_expect *ex, int text_mode)
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
#define PG_GETARG_TEXT_PP(n)
Datum pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS)
#define SRF_RETURN_NEXT(_funcctx, _result)
int pgp_set_sess_key(PGP_Context *ctx, int use)
#define PG_GETARG_BYTEA_P(n)
static void check_expect(PGP_Context *ctx, struct debug_expect *ex)
#define PG_GETARG_ARRAYTYPE_P(n)
void pfree(void *pointer)
#define IS_HIGHBIT_SET(ch)
static text * convert_from_utf8(text *src)
Datum pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS)
int pgp_set_compress_level(PGP_Context *ctx, int level)
int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)
Datum pgp_sym_encrypt_text(PG_FUNCTION_ARGS)
void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)
int px_get_random_bytes(uint8 *dst, unsigned count)
Datum pgp_sym_decrypt_text(PG_FUNCTION_ARGS)
AttInMetadata * attinmeta
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
void px_set_debug_handler(void(*handler)(const char *))
#define ereport(elevel, rest)
static bool string_is_ascii(const char *str)
int pgp_set_convert_crlf(PGP_Context *ctx, int doit)
int px_add_entropy(const uint8 *data, unsigned count)
int pgp_init(PGP_Context **ctx_p)
const char * px_strerror(int err)
void initStringInfo(StringInfo str)
int mbuf_append(MBuf *dst, const uint8 *buf, int len)
#define TextDatumGetCString(d)
int GetDatabaseEncoding(void)
int pgp_set_unicode_mode(PGP_Context *ctx, int mode)
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Datum pgp_armor_headers(PG_FUNCTION_ARGS)
int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)
int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
int pgp_disable_mdc(PGP_Context *ctx, int disable)
#define PG_RETURN_TEXT_P(x)
text * cstring_to_text(const char *s)
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
static char * getword(char *p, char **res_p, int *res_len)
static void show_debug(const char *msg)
static void add_entropy(text *data1, text *data2, text *data3)
MemoryContext multi_call_memory_ctx
Datum pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS)
static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
#define PG_FREE_IF_COPY(ptr, n)
#define HeapTupleGetDatum(tuple)
static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
int pgp_set_s2k_count(PGP_Context *ctx, int count)
static Datum values[MAXATTR]
#define VARSIZE_ANY_EXHDR(PTR)
int errmsg(const char *fmt,...)
#define PG_GETARG_TEXT_P(n)
MBuf * mbuf_create_from_data(uint8 *data, int len)
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)
int pgp_set_text_mode(PGP_Context *ctx, int mode)
static MBuf * create_mbuf_from_vardata(text *data)
#define SET_VARSIZE(PTR, len)
static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)
static int set_arg(PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
int pgp_free(PGP_Context *ctx)
static text * convert_to_utf8(text *src)
char * pg_any_to_server(const char *s, int len, int encoding)
int px_find_digest(const char *name, PX_MD **res)
void px_memset(void *ptr, int c, size_t len)
int pgp_set_compress_algo(PGP_Context *ctx, int algo)
int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)
#define SRF_RETURN_DONE(_funcctx)
static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
#define SRF_FIRSTCALL_INIT()