PostgreSQL Source Code  git master
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 226 of file px.c.

228 {
229  return px_cipher_decrypt(cx->cipher, cx->padding, data, dlen, res, rlen);
230 }
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 219 of file px.c.

221 {
222  return px_cipher_encrypt(cx->cipher, cx->padding, data, dlen, res, rlen);
223 }
#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 233 of file px.c.

234 {
235  if (cx->cipher)
236  px_cipher_free(cx->cipher);
237  px_memset(cx, 0, sizeof(*cx));
238  pfree(cx);
239 }
void pfree(void *pointer)
Definition: mcxt.c:1175
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  memset(keybuf, 0, ks);
207  memcpy(keybuf, key, klen);
208 
209  err = px_cipher_init(c, keybuf, klen, ivbuf);
210 
211  if (ivbuf)
212  pfree(ivbuf);
213  pfree(keybuf);
214 
215  return err;
216 }
unsigned char uint8
Definition: c.h:439
void * palloc0(Size size)
Definition: mcxt.c:1099
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(), error_desc::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 244 of file px.c.

245 {
246  char *p,
247  *p2,
248  *q;
249 
250  *cipher = full;
251  *pad = NULL;
252 
253  p = strchr(full, '/');
254  if (p != NULL)
255  *p++ = 0;
256  while (p != NULL)
257  {
258  if ((q = strchr(p, '/')) != NULL)
259  *q++ = 0;
260 
261  if (!*p)
262  {
263  p = q;
264  continue;
265  }
266  p2 = strchr(p, ':');
267  if (p2 != NULL)
268  {
269  *p2++ = 0;
270  if (strcmp(p, "pad") == 0)
271  *pad = p2;
272  else
273  return PXE_BAD_OPTION;
274  }
275  else
276  return PXE_BAD_FORMAT;
277 
278  p = q;
279  }
280  return 0;
281 }
#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 }
va_end(args)
static void const char * fmt
va_start(args, fmt)
static char * buf
Definition: pg_test_fsync.c:67
#define vsnprintf
Definition: port.h:224
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 286 of file px.c.

287 {
288  int err;
289  char *buf,
290  *s_cipher,
291  *s_pad;
292 
293  PX_Combo *cx;
294 
295  cx = palloc0(sizeof(*cx));
296  buf = pstrdup(name);
297 
298  err = parse_cipher_name(buf, &s_cipher, &s_pad);
299  if (err)
300  {
301  pfree(buf);
302  pfree(cx);
303  return err;
304  }
305 
306  err = px_find_cipher(s_cipher, &cx->cipher);
307  if (err)
308  goto err1;
309 
310  if (s_pad != NULL)
311  {
312  if (strcmp(s_pad, "pkcs") == 0)
313  cx->padding = 1;
314  else if (strcmp(s_pad, "none") == 0)
315  cx->padding = 0;
316  else
317  goto err1;
318  }
319  else
320  cx->padding = 1;
321 
322  cx->init = combo_init;
323  cx->encrypt = combo_encrypt;
324  cx->decrypt = combo_decrypt;
325  cx->encrypt_len = combo_encrypt_len;
326  cx->decrypt_len = combo_decrypt_len;
327  cx->free = combo_free;
328 
329  pfree(buf);
330 
331  *res = cx;
332 
333  return 0;
334 
335 err1:
336  if (cx->cipher)
337  px_cipher_free(cx->cipher);
338  pfree(cx);
339  pfree(buf);
340  return PXE_NO_CIPHER;
341 }
const char * name
Definition: encode.c:561
char * pstrdup(const char *in)
Definition: mcxt.c:1305
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: openssl.c:771
static void combo_free(PX_Combo *cx)
Definition: px.c:233
static int combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:219
static int parse_cipher_name(char *full, char **cipher, char **pad)
Definition: px.c:244
static int combo_decrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:226
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

References buf, combo_decrypt(), combo_decrypt_len(), combo_encrypt(), combo_encrypt_len(), combo_free(), combo_init(), cx(), error_desc::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
int err
Definition: px.c:38

References error_desc::desc, error_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:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
const char * px_strerror(int err)
Definition: px.c:111
#define PXE_NO_RANDOM
Definition: px.h:63

References ereport, error_desc::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().