Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions

pgp-pgsql.c File Reference

#include "postgres.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.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

Defines

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

Functions

Datum pgp_sym_encrypt_text (PG_FUNCTION_ARGS)
Datum pgp_sym_encrypt_bytea (PG_FUNCTION_ARGS)
Datum pgp_sym_decrypt_text (PG_FUNCTION_ARGS)
Datum pgp_sym_decrypt_bytea (PG_FUNCTION_ARGS)
Datum pgp_pub_encrypt_text (PG_FUNCTION_ARGS)
Datum pgp_pub_encrypt_bytea (PG_FUNCTION_ARGS)
Datum pgp_pub_decrypt_text (PG_FUNCTION_ARGS)
Datum pgp_pub_decrypt_bytea (PG_FUNCTION_ARGS)
Datum pgp_key_id_w (PG_FUNCTION_ARGS)
Datum pg_armor (PG_FUNCTION_ARGS)
Datum pg_dearmor (PG_FUNCTION_ARGS)
 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)
static void add_block_entropy (PX_MD *md, text *data)
static void add_entropy (text *data1, text *data2, text *data3)
static textconvert_charset (text *src, int cset_from, int cset_to)
static textconvert_from_utf8 (text *src)
static textconvert_to_utf8 (text *src)
static void clear_and_pfree (text *p)
static void fill_expect (struct debug_expect *ex, int text_mode)
static void check_expect (PGP_Context *ctx, struct debug_expect *ex)
static void show_debug (const char *msg)
static int set_arg (PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)
static char * getword (char *p, char **res_p, int *res_len)
static char * downcase_convert (const uint8 *s, int len)
static int parse_args (PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)
static MBufcreate_mbuf_from_vardata (text *data)
static void init_work (PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)
static byteaencrypt_internal (int is_pubenc, int is_text, text *data, text *key, text *args)
static byteadecrypt_internal (int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)

Define Documentation

#define EX_CHECK (   arg  ) 
Value:
do { \
        if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \
    } while (0)

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

Referenced by check_expect().

#define EX_MSG (   arg  ) 
Value:
ereport(NOTICE, (errmsg( \
        "pgp_decrypt: unexpected %s: expected %d got %d", \
        CppAsString(arg), ex->arg, ctx->arg)))

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


Function Documentation

static void add_block_entropy ( PX_MD md,
text data 
) [static]

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

References px_add_entropy(), px_md_finish, px_md_reset, px_md_update, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by add_entropy().

{
    uint8       sha1[20];

    px_md_reset(md);
    px_md_update(md, (uint8 *) VARDATA(data), VARSIZE(data) - VARHDRSZ);
    px_md_finish(md, sha1);

    px_add_entropy(sha1, 20);

    memset(sha1, 0, 20);
}

static void add_entropy ( text data1,
text data2,
text data3 
) [static]

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

References add_block_entropy(), px_find_digest(), px_get_random_bytes(), and px_md_free.

Referenced by decrypt_internal(), and encrypt_internal().

{
    PX_MD      *md;
    uint8       rnd[3];

    if (!data1 && !data2 && !data3)
        return;

    if (px_get_random_bytes(rnd, 3) < 0)
        return;

    if (px_find_digest("sha1", &md) < 0)
        return;

    /*
     * Try to make the feeding unpredictable.
     *
     * Prefer data over keys, as it's rather likely that key is same in
     * several calls.
     */

    /* chance: 7/8 */
    if (data1 && rnd[0] >= 32)
        add_block_entropy(md, data1);

    /* chance: 5/8 */
    if (data2 && rnd[1] >= 160)
        add_block_entropy(md, data2);

    /* chance: 5/8 */
    if (data3 && rnd[2] >= 160)
        add_block_entropy(md, data3);

    px_md_free(md);
    memset(rnd, 0, sizeof(rnd));
}

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

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

References EX_CHECK, and PGP_Context::use_sess_key.

Referenced by decrypt_internal().

{
    EX_CHECK(cipher_algo);
    EX_CHECK(s2k_mode);
    EX_CHECK(s2k_digest_algo);
    EX_CHECK(use_sess_key);
    if (ctx->use_sess_key)
        EX_CHECK(s2k_cipher_algo);
    EX_CHECK(disable_mdc);
    EX_CHECK(compress_algo);
    EX_CHECK(unicode_mode);
}

static void clear_and_pfree ( text p  )  [static]

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

References pfree(), and VARSIZE.

Referenced by decrypt_internal(), and encrypt_internal().

{
    memset(p, 0, VARSIZE(p));
    pfree(p);
}

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

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

References cstring_to_text(), pfree(), pg_do_encoding_conversion(), VARDATA, and VARSIZE.

Referenced by convert_from_utf8(), and convert_to_utf8().

{
    int         src_len = VARSIZE(src) - VARHDRSZ;
    unsigned char *dst;
    unsigned char *csrc = (unsigned char *) VARDATA(src);
    text       *res;

    dst = pg_do_encoding_conversion(csrc, src_len, cset_from, cset_to);
    if (dst == csrc)
        return src;

    res = cstring_to_text((char *) dst);
    pfree(dst);
    return res;
}

static text* convert_from_utf8 ( text src  )  [static]

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

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

Referenced by decrypt_internal().

static text* convert_to_utf8 ( text src  )  [static]

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

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

Referenced by encrypt_internal().

static MBuf* create_mbuf_from_vardata ( text data  )  [static]

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

References mbuf_create_from_data(), VARDATA, VARHDRSZ, and VARSIZE.

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

{
    return mbuf_create_from_data((uint8 *) VARDATA(data),
                                 VARSIZE(data) - VARHDRSZ);
}

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

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

References add_entropy(), check_expect(), clear_and_pfree(), convert_from_utf8(), create_mbuf_from_vardata(), ereport, errcode(), errmsg(), ERROR, debug_expect::expect, init_work(), mbuf_append(), mbuf_create(), mbuf_create_from_data(), mbuf_free(), mbuf_steal_data(), NULL, pgp_decrypt(), pgp_free(), pgp_get_unicode_mode(), pgp_set_pubkey(), pgp_set_symkey(), px_set_debug_handler(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

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

{
    int         err;
    MBuf       *src = NULL,
               *dst = NULL;
    uint8       tmp[VARHDRSZ];
    uint8      *restmp;
    bytea      *res;
    int         res_len;
    PGP_Context *ctx = NULL;
    struct debug_expect ex;
    int         got_unicode = 0;


    init_work(&ctx, need_text, args, &ex);

    src = mbuf_create_from_data((uint8 *) VARDATA(data),
                                VARSIZE(data) - VARHDRSZ);
    dst = mbuf_create(VARSIZE(data) + 2048);

    /*
     * reserve room for header
     */
    mbuf_append(dst, tmp, VARHDRSZ);

    /*
     * set key
     */
    if (is_pubenc)
    {
        uint8      *psw = NULL;
        int         psw_len = 0;
        MBuf       *kbuf;

        if (keypsw)
        {
            psw = (uint8 *) VARDATA(keypsw);
            psw_len = VARSIZE(keypsw) - VARHDRSZ;
        }
        kbuf = create_mbuf_from_vardata(key);
        err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1);
        mbuf_free(kbuf);
    }
    else
        err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
                             VARSIZE(key) - VARHDRSZ);

    /*
     * decrypt
     */
    if (err >= 0)
        err = pgp_decrypt(ctx, src, dst);

    /*
     * failed?
     */
    if (err < 0)
        goto out;

    if (ex.expect)
        check_expect(ctx, &ex);

    /* remember the setting */
    got_unicode = pgp_get_unicode_mode(ctx);

out:
    if (src)
        mbuf_free(src);
    if (ctx)
        pgp_free(ctx);

    if (err)
    {
        px_set_debug_handler(NULL);
        if (dst)
            mbuf_free(dst);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("%s", px_strerror(err))));
    }

    res_len = mbuf_steal_data(dst, &restmp);
    mbuf_free(dst);

    /* res_len includes VARHDRSZ */
    res = (bytea *) restmp;
    SET_VARSIZE(res, res_len);

    if (need_text && got_unicode)
    {
        text       *utf = convert_from_utf8(res);

        if (utf != res)
        {
            clear_and_pfree(res);
            res = utf;
        }
    }
    px_set_debug_handler(NULL);

    /*
     * add successful decryptions also into RNG
     */
    add_entropy(res, key, keypsw);

    return res;
}

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

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

References i, and palloc().

Referenced by parse_args().

{
    int         c,
                i;
    char       *res = palloc(len + 1);

    for (i = 0; i < len; i++)
    {
        c = s[i];
        if (c >= 'A' && c <= 'Z')
            c += 'a' - 'A';
        res[i] = c;
    }
    res[len] = 0;
    return res;
}

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

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

References add_entropy(), clear_and_pfree(), convert_to_utf8(), create_mbuf_from_vardata(), debug_expect::debug, ereport, errcode(), errmsg(), ERROR, init_work(), mbuf_append(), mbuf_create(), mbuf_free(), mbuf_steal_data(), NULL, pgp_encrypt(), pgp_free(), pgp_get_unicode_mode(), pgp_set_pubkey(), pgp_set_symkey(), px_set_debug_handler(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

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

{
    MBuf       *src,
               *dst;
    uint8       tmp[VARHDRSZ];
    uint8      *restmp;
    bytea      *res;
    int         res_len;
    PGP_Context *ctx;
    int         err;
    struct debug_expect ex;
    text       *tmp_data = NULL;

    /*
     * Add data and key info RNG.
     */
    add_entropy(data, key, NULL);

    init_work(&ctx, is_text, args, &ex);

    if (is_text && pgp_get_unicode_mode(ctx))
    {
        tmp_data = convert_to_utf8(data);
        if (tmp_data == data)
            tmp_data = NULL;
        else
            data = tmp_data;
    }

    src = create_mbuf_from_vardata(data);
    dst = mbuf_create(VARSIZE(data) + 128);

    /*
     * reserve room for header
     */
    mbuf_append(dst, tmp, VARHDRSZ);

    /*
     * set key
     */
    if (is_pubenc)
    {
        MBuf       *kbuf = create_mbuf_from_vardata(key);

        err = pgp_set_pubkey(ctx, kbuf,
                             NULL, 0, 0);
        mbuf_free(kbuf);
    }
    else
        err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
                             VARSIZE(key) - VARHDRSZ);

    /*
     * encrypt
     */
    if (err >= 0)
        err = pgp_encrypt(ctx, src, dst);

    /*
     * check for error
     */
    if (err)
    {
        if (ex.debug)
            px_set_debug_handler(NULL);
        if (tmp_data)
            clear_and_pfree(tmp_data);
        pgp_free(ctx);
        mbuf_free(src);
        mbuf_free(dst);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("%s", px_strerror(err))));
    }

    /* res_len includes VARHDRSZ */
    res_len = mbuf_steal_data(dst, &restmp);
    res = (bytea *) restmp;
    SET_VARSIZE(res, res_len);

    if (tmp_data)
        clear_and_pfree(tmp_data);
    pgp_free(ctx);
    mbuf_free(src);
    mbuf_free(dst);

    px_set_debug_handler(NULL);

    return res;
}

static void fill_expect ( struct debug_expect ex,
int  text_mode 
) [static]
static char* getword ( char *  p,
char **  res_p,
int *  res_len 
) [static]

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

Referenced by parse_args().

{
    /* whitespace at start */
    while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
        p++;

    /* word data */
    *res_p = p;
    if (*p == '=' || *p == ',')
        p++;
    else
        while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'
                       || *p == '=' || *p == ','))
            p++;

    /* word end */
    *res_len = p - *res_p;

    /* whitespace at end */
    while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
        p++;

    return p;
}

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

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

References debug_expect::debug, ereport, errcode(), errmsg(), ERROR, fill_expect(), NULL, parse_args(), pgp_init(), pgp_set_text_mode(), px_set_debug_handler(), px_strerror(), show_debug(), VARDATA, VARHDRSZ, and VARSIZE.

Referenced by decrypt_internal(), and encrypt_internal().

{
    int         err = pgp_init(ctx_p);

    fill_expect(ex, is_text);

    if (err == 0 && args != NULL)
        err = parse_args(*ctx_p, (uint8 *) VARDATA(args),
                         VARSIZE(args) - VARHDRSZ, ex);

    if (err)
    {
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("%s", px_strerror(err))));
    }

    if (ex->debug)
        px_set_debug_handler(show_debug);

    pgp_set_text_mode(*ctx_p, is_text);
}

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

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

References downcase_convert(), getword(), pfree(), set_arg(), and val.

Referenced by init_work().

{
    char       *str = downcase_convert(args, arg_len);
    char       *key,
               *val;
    int         key_len,
                val_len;
    int         res = 0;
    char       *p = str;

    while (*p)
    {
        res = PXE_ARGUMENT_ERROR;
        p = getword(p, &key, &key_len);
        if (*p++ != '=')
            break;
        p = getword(p, &val, &val_len);
        if (*p == '\0')
            ;
        else if (*p++ != ',')
            break;

        if (*key == 0 || *val == 0 || val_len == 0)
            break;

        key[key_len] = 0;
        val[val_len] = 0;

        res = set_arg(ctx, key, val, ex);
        if (res < 0)
            break;
    }
    pfree(str);
    return res;
}

Datum pg_armor ( PG_FUNCTION_ARGS   ) 

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

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_RETURN_TEXT_P, pgp_armor_enc_len(), pgp_armor_encode(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

{
    bytea      *data;
    text       *res;
    int         data_len,
                res_len,
                guess_len;

    data = PG_GETARG_BYTEA_P(0);
    data_len = VARSIZE(data) - VARHDRSZ;

    guess_len = pgp_armor_enc_len(data_len);
    res = palloc(VARHDRSZ + guess_len);

    res_len = pgp_armor_encode((uint8 *) VARDATA(data), data_len,
                               (uint8 *) VARDATA(res));
    if (res_len > guess_len)
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("Overflow - encode estimate too small")));
    SET_VARSIZE(res, VARHDRSZ + res_len);

    PG_FREE_IF_COPY(data, 0);
    PG_RETURN_TEXT_P(res);
}

Datum pg_dearmor ( PG_FUNCTION_ARGS   ) 

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

References ereport, errcode(), errmsg(), ERROR, palloc(), PG_FREE_IF_COPY, PG_GETARG_TEXT_P, PG_RETURN_TEXT_P, pgp_armor_dec_len(), pgp_armor_decode(), px_strerror(), SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

{
    text       *data;
    bytea      *res;
    int         data_len,
                res_len,
                guess_len;

    data = PG_GETARG_TEXT_P(0);
    data_len = VARSIZE(data) - VARHDRSZ;

    guess_len = pgp_armor_dec_len(data_len);
    res = palloc(VARHDRSZ + guess_len);

    res_len = pgp_armor_decode((uint8 *) VARDATA(data), data_len,
                               (uint8 *) VARDATA(res));
    if (res_len < 0)
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("%s", px_strerror(res_len))));
    if (res_len > guess_len)
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("Overflow - decode estimate too small")));
    SET_VARSIZE(res, VARHDRSZ + res_len);

    PG_FREE_IF_COPY(data, 0);
    PG_RETURN_TEXT_P(res);
}

PG_FUNCTION_INFO_V1 ( pg_dearmor   ) 
PG_FUNCTION_INFO_V1 ( pgp_pub_encrypt_text   ) 
PG_FUNCTION_INFO_V1 ( pg_armor   ) 
PG_FUNCTION_INFO_V1 ( pgp_key_id_w   ) 
PG_FUNCTION_INFO_V1 ( pgp_pub_encrypt_bytea   ) 
PG_FUNCTION_INFO_V1 ( pgp_sym_encrypt_bytea   ) 
PG_FUNCTION_INFO_V1 ( pgp_sym_encrypt_text   ) 
PG_FUNCTION_INFO_V1 ( pgp_pub_decrypt_bytea   ) 
PG_FUNCTION_INFO_V1 ( pgp_sym_decrypt_text   ) 
PG_FUNCTION_INFO_V1 ( pgp_sym_decrypt_bytea   ) 
PG_FUNCTION_INFO_V1 ( pgp_pub_decrypt_text   ) 
Datum pgp_key_id_w ( PG_FUNCTION_ARGS   ) 

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

References buf, create_mbuf_from_vardata(), ereport, errcode(), errmsg(), ERROR, mbuf_free(), palloc(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_RETURN_TEXT_P, pgp_get_keyid(), px_strerror(), SET_VARSIZE, VARDATA, and VARHDRSZ.

{
    bytea      *data;
    text       *res;
    int         res_len;
    MBuf       *buf;

    data = PG_GETARG_BYTEA_P(0);
    buf = create_mbuf_from_vardata(data);
    res = palloc(VARHDRSZ + 17);

    res_len = pgp_get_keyid(buf, VARDATA(res));
    mbuf_free(buf);
    if (res_len < 0)
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
                 errmsg("%s", px_strerror(res_len))));
    SET_VARSIZE(res, VARHDRSZ + res_len);

    PG_FREE_IF_COPY(data, 0);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_pub_decrypt_bytea ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *psw = NULL,
               *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        psw = PG_GETARG_BYTEA_P(2);
    if (PG_NARGS() > 3)
        arg = PG_GETARG_BYTEA_P(3);

    res = decrypt_internal(1, 0, data, key, psw, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(psw, 2);
    if (PG_NARGS() > 3)
        PG_FREE_IF_COPY(arg, 3);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_pub_decrypt_text ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *psw = NULL,
               *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        psw = PG_GETARG_BYTEA_P(2);
    if (PG_NARGS() > 3)
        arg = PG_GETARG_BYTEA_P(3);

    res = decrypt_internal(1, 1, data, key, psw, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(psw, 2);
    if (PG_NARGS() > 3)
        PG_FREE_IF_COPY(arg, 3);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_pub_encrypt_bytea ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = encrypt_internal(1, 0, data, key, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_pub_encrypt_text ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = encrypt_internal(1, 1, data, key, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_sym_decrypt_bytea ( PG_FUNCTION_ARGS   ) 

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

References arg, decrypt_internal(), NULL, PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_NARGS, and PG_RETURN_TEXT_P.

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = decrypt_internal(0, 0, data, key, NULL, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_sym_decrypt_text ( PG_FUNCTION_ARGS   ) 

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

References arg, decrypt_internal(), NULL, PG_FREE_IF_COPY, PG_GETARG_BYTEA_P, PG_NARGS, and PG_RETURN_TEXT_P.

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = decrypt_internal(0, 1, data, key, NULL, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_sym_encrypt_bytea ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = encrypt_internal(0, 0, data, key, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

Datum pgp_sym_encrypt_text ( PG_FUNCTION_ARGS   ) 

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

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

{
    bytea      *data,
               *key;
    text       *arg = NULL;
    text       *res;

    data = PG_GETARG_BYTEA_P(0);
    key = PG_GETARG_BYTEA_P(1);
    if (PG_NARGS() > 2)
        arg = PG_GETARG_BYTEA_P(2);

    res = encrypt_internal(0, 1, data, key, arg);

    PG_FREE_IF_COPY(data, 0);
    PG_FREE_IF_COPY(key, 1);
    if (PG_NARGS() > 2)
        PG_FREE_IF_COPY(arg, 2);
    PG_RETURN_TEXT_P(res);
}

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

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

References debug_expect::cipher_algo, debug_expect::compress_algo, debug_expect::debug, debug_expect::disable_mdc, debug_expect::expect, NULL, 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_digest_algo(), pgp_set_s2k_mode(), pgp_set_sess_key(), pgp_set_unicode_mode(), debug_expect::s2k_cipher_algo, debug_expect::s2k_digest_algo, debug_expect::s2k_mode, debug_expect::unicode_mode, and debug_expect::use_sess_key.

Referenced by parse_args().

{
    int         res = 0;

    if (strcmp(key, "cipher-algo") == 0)
        res = pgp_set_cipher_algo(ctx, val);
    else if (strcmp(key, "disable-mdc") == 0)
        res = pgp_disable_mdc(ctx, atoi(val));
    else if (strcmp(key, "sess-key") == 0)
        res = pgp_set_sess_key(ctx, atoi(val));
    else if (strcmp(key, "s2k-mode") == 0)
        res = pgp_set_s2k_mode(ctx, atoi(val));
    else if (strcmp(key, "s2k-digest-algo") == 0)
        res = pgp_set_s2k_digest_algo(ctx, val);
    else if (strcmp(key, "s2k-cipher-algo") == 0)
        res = pgp_set_s2k_cipher_algo(ctx, val);
    else if (strcmp(key, "compress-algo") == 0)
        res = pgp_set_compress_algo(ctx, atoi(val));
    else if (strcmp(key, "compress-level") == 0)
        res = pgp_set_compress_level(ctx, atoi(val));
    else if (strcmp(key, "convert-crlf") == 0)
        res = pgp_set_convert_crlf(ctx, atoi(val));
    else if (strcmp(key, "unicode-mode") == 0)
        res = pgp_set_unicode_mode(ctx, atoi(val));
    /* decrypt debug */
    else if (ex != NULL && strcmp(key, "debug") == 0)
        ex->debug = atoi(val);
    else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)
    {
        ex->expect = 1;
        ex->cipher_algo = pgp_get_cipher_code(val);
    }
    else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)
    {
        ex->expect = 1;
        ex->disable_mdc = atoi(val);
    }
    else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)
    {
        ex->expect = 1;
        ex->use_sess_key = atoi(val);
    }
    else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)
    {
        ex->expect = 1;
        ex->s2k_mode = atoi(val);
    }
    else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)
    {
        ex->expect = 1;
        ex->s2k_digest_algo = pgp_get_digest_code(val);
    }
    else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)
    {
        ex->expect = 1;
        ex->s2k_cipher_algo = pgp_get_cipher_code(val);
    }
    else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)
    {
        ex->expect = 1;
        ex->compress_algo = atoi(val);
    }
    else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)
    {
        ex->expect = 1;
        ex->unicode_mode = atoi(val);
    }
    else
        res = PXE_ARGUMENT_ERROR;

    return res;
}

static void show_debug ( const char *  msg  )  [static]

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

References ereport, errmsg(), and NOTICE.

Referenced by init_work().

{
    ereport(NOTICE, (errmsg("dbg: %s", msg)));
}