PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
px.c File Reference
#include "postgres.h"
#include "px.h"
Include dependency graph for px.c:

Go to the source code of this file.

Data Structures

struct  error_desc
 

Functions

void px_THROW_ERROR (int err)
 
const char * px_strerror (int err)
 
void px_memset (void *ptr, int c, size_t len)
 
const char * px_resolve_alias (const PX_Alias *list, const char *name)
 
void px_set_debug_handler (void(*handler)(const char *))
 
void px_debug (const char *fmt,...)
 
static unsigned combo_encrypt_len (PX_Combo *cx, unsigned dlen)
 
static unsigned combo_decrypt_len (PX_Combo *cx, unsigned dlen)
 
static int combo_init (PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)
 
static int combo_encrypt (PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
 
static int combo_decrypt (PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
 
static void combo_free (PX_Combo *cx)
 
static int parse_cipher_name (char *full, char **cipher, char **pad)
 
int px_find_combo (const char *name, PX_Combo **res)
 

Variables

static const struct error_desc px_err_list []
 
static void(* debug_handler )(const char *) = NULL
 

Function Documentation

◆ combo_decrypt()

static int combo_decrypt ( PX_Combo cx,
const uint8 data,
unsigned  dlen,
uint8 res,
unsigned *  rlen 
)
static

Definition at line 225 of file px.c.

227 {
228  return px_cipher_decrypt(cx->cipher, cx->padding, data, dlen, res, rlen);
229 }
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
const void * data
#define px_cipher_decrypt(c, padding, data, dlen, res, rlen)
Definition: px.h:213

References cx(), data, px_cipher_decrypt, and res.

Referenced by px_find_combo().

◆ combo_decrypt_len()

static unsigned combo_decrypt_len ( PX_Combo cx,
unsigned  dlen 
)
static

Definition at line 175 of file px.c.

176 {
177  return dlen;
178 }

Referenced by px_find_combo().

◆ combo_encrypt()

static int combo_encrypt ( PX_Combo cx,
const uint8 data,
unsigned  dlen,
uint8 res,
unsigned *  rlen 
)
static

Definition at line 218 of file px.c.

220 {
221  return px_cipher_encrypt(cx->cipher, cx->padding, data, dlen, res, rlen);
222 }
#define px_cipher_encrypt(c, padding, data, dlen, res, rlen)
Definition: px.h:211

References cx(), data, px_cipher_encrypt, and res.

Referenced by px_find_combo().

◆ combo_encrypt_len()

static unsigned combo_encrypt_len ( PX_Combo cx,
unsigned  dlen 
)
static

Definition at line 169 of file px.c.

170 {
171  return dlen + 512;
172 }

Referenced by px_find_combo().

◆ combo_free()

static void combo_free ( PX_Combo cx)
static

Definition at line 232 of file px.c.

233 {
234  if (cx->cipher)
235  px_cipher_free(cx->cipher);
236  px_memset(cx, 0, sizeof(*cx));
237  pfree(cx);
238 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
#define px_cipher_free(c)
Definition: px.h:215

References cx(), pfree(), px_cipher_free, and px_memset().

Referenced by px_find_combo().

◆ combo_init()

static int combo_init ( PX_Combo cx,
const uint8 key,
unsigned  klen,
const uint8 iv,
unsigned  ivlen 
)
static

Definition at line 181 of file px.c.

183 {
184  int err;
185  unsigned ks,
186  ivs;
187  PX_Cipher *c = cx->cipher;
188  uint8 *ivbuf = NULL;
189  uint8 *keybuf;
190 
191  ks = px_cipher_key_size(c);
192 
193  ivs = px_cipher_iv_size(c);
194  if (ivs > 0)
195  {
196  ivbuf = palloc0(ivs);
197  if (ivlen > ivs)
198  memcpy(ivbuf, iv, ivs);
199  else if (ivlen > 0)
200  memcpy(ivbuf, iv, ivlen);
201  }
202 
203  if (klen > ks)
204  klen = ks;
205  keybuf = palloc0(ks);
206  memcpy(keybuf, key, klen);
207 
208  err = px_cipher_init(c, keybuf, klen, ivbuf);
209 
210  if (ivbuf)
211  pfree(ivbuf);
212  pfree(keybuf);
213 
214  return err;
215 }
unsigned char uint8
Definition: c.h:516
void err(int eval, const char *fmt,...)
Definition: err.c:43
void * palloc0(Size size)
Definition: mcxt.c:1347
char * c
#define px_cipher_iv_size(c)
Definition: px.h:209
#define px_cipher_init(c, k, klen, iv)
Definition: px.h:210
#define px_cipher_key_size(c)
Definition: px.h:207
Definition: px.h:141

References cx(), err(), sort-test::key, palloc0(), pfree(), px_cipher_init, px_cipher_iv_size, and px_cipher_key_size.

Referenced by px_find_combo().

◆ parse_cipher_name()

static int parse_cipher_name ( char *  full,
char **  cipher,
char **  pad 
)
static

Definition at line 243 of file px.c.

244 {
245  char *p,
246  *p2,
247  *q;
248 
249  *cipher = full;
250  *pad = NULL;
251 
252  p = strchr(full, '/');
253  if (p != NULL)
254  *p++ = 0;
255  while (p != NULL)
256  {
257  if ((q = strchr(p, '/')) != NULL)
258  *q++ = 0;
259 
260  if (!*p)
261  {
262  p = q;
263  continue;
264  }
265  p2 = strchr(p, ':');
266  if (p2 != NULL)
267  {
268  *p2++ = 0;
269  if (strcmp(p, "pad") == 0)
270  *pad = p2;
271  else
272  return PXE_BAD_OPTION;
273  }
274  else
275  return PXE_BAD_FORMAT;
276 
277  p = q;
278  }
279  return 0;
280 }
#define PXE_BAD_FORMAT
Definition: px.h:52
#define PXE_BAD_OPTION
Definition: px.h:51

References p2, PXE_BAD_FORMAT, and PXE_BAD_OPTION.

Referenced by px_find_combo().

◆ px_debug()

void px_debug ( const char *  fmt,
  ... 
)

Definition at line 149 of file px.c.

150 {
151  va_list ap;
152 
153  va_start(ap, fmt);
154  if (debug_handler)
155  {
156  char buf[512];
157 
158  vsnprintf(buf, sizeof(buf), fmt, ap);
160  }
161  va_end(ap);
162 }
static void const char * fmt
va_end(args)
va_start(args, fmt)
static char * buf
Definition: pg_test_fsync.c:72
#define vsnprintf
Definition: port.h:237
static void(* debug_handler)(const char *)
Definition: px.c:140

References buf, debug_handler, fmt, va_end(), va_start(), and vsnprintf.

Referenced by _pgp_read_public_key(), bn_to_mpi(), check_key_cksum(), check_key_sha1(), control_cksum(), decrypt_key(), internal_read_key(), mbuf_append(), mdc_finish(), mdcbuf_finish(), mpi_to_bn(), parse_compressed_data(), parse_literal_data(), parse_new_len(), parse_old_len(), parse_symenc_mdc_data(), parse_symenc_sesskey(), pgp_decrypt(), pgp_expect_packet_end(), pgp_mpi_alloc(), pgp_parse_pkt_hdr(), pgp_parse_pubenc_sesskey(), pgp_write_pubenc_sesskey(), process_data_packets(), process_secret_key(), and pullf_read_fixed().

◆ px_find_combo()

int px_find_combo ( const char *  name,
PX_Combo **  res 
)

Definition at line 285 of file px.c.

286 {
287  int err;
288  char *buf,
289  *s_cipher,
290  *s_pad;
291 
292  PX_Combo *cx;
293 
294  cx = palloc0(sizeof(*cx));
295  buf = pstrdup(name);
296 
297  err = parse_cipher_name(buf, &s_cipher, &s_pad);
298  if (err)
299  {
300  pfree(buf);
301  pfree(cx);
302  return err;
303  }
304 
305  err = px_find_cipher(s_cipher, &cx->cipher);
306  if (err)
307  goto err1;
308 
309  if (s_pad != NULL)
310  {
311  if (strcmp(s_pad, "pkcs") == 0)
312  cx->padding = 1;
313  else if (strcmp(s_pad, "none") == 0)
314  cx->padding = 0;
315  else
316  goto err1;
317  }
318  else
319  cx->padding = 1;
320 
321  cx->init = combo_init;
322  cx->encrypt = combo_encrypt;
323  cx->decrypt = combo_decrypt;
324  cx->encrypt_len = combo_encrypt_len;
325  cx->decrypt_len = combo_decrypt_len;
326  cx->free = combo_free;
327 
328  pfree(buf);
329 
330  *res = cx;
331 
332  return 0;
333 
334 err1:
335  if (cx->cipher)
336  px_cipher_free(cx->cipher);
337  pfree(cx);
338  pfree(buf);
339  return PXE_NO_CIPHER;
340 }
char * pstrdup(const char *in)
Definition: mcxt.c:1696
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: openssl.c:736
static void combo_free(PX_Combo *cx)
Definition: px.c:232
static int combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:218
static int parse_cipher_name(char *full, char **cipher, char **pad)
Definition: px.c:243
static int combo_decrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:225
static unsigned combo_decrypt_len(PX_Combo *cx, unsigned dlen)
Definition: px.c:175
static unsigned combo_encrypt_len(PX_Combo *cx, unsigned dlen)
Definition: px.c:169
static int combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)
Definition: px.c:181
#define PXE_NO_CIPHER
Definition: px.h:49
Definition: px.h:156
const char * name

References buf, combo_decrypt(), combo_decrypt_len(), combo_encrypt(), combo_encrypt_len(), combo_free(), combo_init(), cx(), err(), name, palloc0(), parse_cipher_name(), pfree(), pstrdup(), px_cipher_free, px_find_cipher(), PXE_NO_CIPHER, and res.

Referenced by pg_decrypt(), pg_decrypt_iv(), pg_encrypt(), and pg_encrypt_iv().

◆ px_memset()

◆ px_resolve_alias()

const char* px_resolve_alias ( const PX_Alias list,
const char *  name 
)

Definition at line 129 of file px.c.

130 {
131  while (list->name)
132  {
133  if (pg_strcasecmp(list->alias, name) == 0)
134  return list->name;
135  list++;
136  }
137  return name;
138 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

References sort-test::list, name, and pg_strcasecmp().

Referenced by px_find_cipher().

◆ px_set_debug_handler()

void px_set_debug_handler ( void(*)(const char *)  handler)

Definition at line 143 of file px.c.

144 {
145  debug_handler = handler;
146 }

References debug_handler.

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

◆ px_strerror()

const char* px_strerror ( int  err)

Definition at line 111 of file px.c.

112 {
113  const struct error_desc *e;
114 
115  for (e = px_err_list; e->desc; e++)
116  if (e->err == err)
117  return e->desc;
118  return "Bad error code";
119 }
e
Definition: preproc-init.c:82
static const struct error_desc px_err_list[]
Definition: px.c:42
Definition: px.c:37
const char * desc
Definition: px.c:39

References error_desc::desc, err(), and px_err_list.

Referenced by find_provider(), pg_decrypt(), pg_decrypt_iv(), pg_encrypt(), pg_encrypt_iv(), pg_gen_salt(), pg_gen_salt_rounds(), and px_THROW_ERROR().

◆ px_THROW_ERROR()

void px_THROW_ERROR ( int  err)

Definition at line 93 of file px.c.

94 {
95  if (err == PXE_NO_RANDOM)
96  {
97  ereport(ERROR,
98  (errcode(ERRCODE_INTERNAL_ERROR),
99  errmsg("could not generate a random number")));
100  }
101  else
102  {
103  /* For other errors, use the message from the above list. */
104  ereport(ERROR,
105  (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
106  errmsg("%s", px_strerror(err))));
107  }
108 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
const char * px_strerror(int err)
Definition: px.c:111
#define PXE_NO_RANDOM
Definition: px.h:63

References ereport, err(), errcode(), errmsg(), ERROR, px_strerror(), and PXE_NO_RANDOM.

Referenced by decrypt_internal(), encrypt_internal(), init_work(), pg_dearmor(), pg_random_bytes(), pgp_armor_headers(), and pgp_key_id_w().

Variable Documentation

◆ debug_handler

void(* debug_handler) (const char *) ( const char *  ) = NULL
static

Definition at line 140 of file px.c.

Referenced by px_debug(), and px_set_debug_handler().

◆ px_err_list

const struct error_desc px_err_list[]
static

Definition at line 1 of file px.c.

Referenced by px_strerror().