PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 charpx_strerror (int err)
 
void px_memset (void *ptr, int c, size_t len)
 
const charpx_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
static int fb(int x)
#define px_cipher_decrypt(c, padding, data, dlen, res, rlen)
Definition px.h:224

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

Referenced by px_find_combo().

◆ combo_decrypt_len()

static unsigned combo_decrypt_len ( PX_Combo cx,
unsigned  dlen 
)
static

Definition at line 176 of file px.c.

177{
178 return dlen;
179}

References fb().

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:222

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

Referenced by px_find_combo().

◆ combo_encrypt_len()

static unsigned combo_encrypt_len ( PX_Combo cx,
unsigned  dlen 
)
static

Definition at line 170 of file px.c.

171{
172 return dlen + 512;
173}

References fb().

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:1616
void px_memset(void *ptr, int c, size_t len)
Definition px.c:124
#define px_cipher_free(c)
Definition px.h:226

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 182 of file px.c.

184{
185 int err;
186 unsigned ks,
187 ivs;
188 PX_Cipher *c = cx->cipher;
189 uint8 *ivbuf = NULL;
190 uint8 *keybuf;
191
193
195 if (ivs > 0)
196 {
197 ivbuf = palloc0(ivs);
198 if (ivlen > ivs)
199 memcpy(ivbuf, iv, ivs);
200 else if (ivlen > 0)
201 memcpy(ivbuf, iv, ivlen);
202 }
203
204 if (klen > ks)
205 klen = ks;
206 keybuf = palloc0(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}
uint8_t uint8
Definition c.h:544
void err(int eval, const char *fmt,...)
Definition err.c:43
void * palloc0(Size size)
Definition mcxt.c:1417
char * c
#define px_cipher_iv_size(c)
Definition px.h:220
#define px_cipher_init(c, k, klen, iv)
Definition px.h:221
#define px_cipher_key_size(c)
Definition px.h:218

References cx(), err(), fb(), 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 fb(), PXE_BAD_FORMAT, and PXE_BAD_OPTION.

Referenced by px_find_combo().

◆ px_debug()

◆ 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
296 buf = pstrdup(name);
297
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
335err1:
336 if (cx->cipher)
337 px_cipher_free(cx->cipher);
338 pfree(cx);
339 pfree(buf);
340 return PXE_NO_CIPHER;
341}
#define palloc0_object(type)
Definition fe_memutils.h:75
char * pstrdup(const char *in)
Definition mcxt.c:1781
int px_find_cipher(const char *name, PX_Cipher **res)
Definition openssl.c:776
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:176
static unsigned combo_encrypt_len(PX_Combo *cx, unsigned dlen)
Definition px.c:170
static int combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)
Definition px.c:182
#define PXE_NO_CIPHER
Definition px.h:49
Definition px.h:164
const char * name

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

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 130 of file px.c.

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

References 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 144 of file px.c.

145{
146 debug_handler = handler;
147}

References debug_handler.

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

◆ px_strerror()

const char * px_strerror ( int  err)

Definition at line 112 of file px.c.

113{
114 const struct error_desc *e;
115
116 for (e = px_err_list; e->desc; e++)
117 if (e->err == err)
118 return e->desc;
119 return "Bad error code";
120}
e
static const struct error_desc px_err_list[]
Definition px.c:42
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 94 of file px.c.

95{
96 if (err == PXE_NO_RANDOM)
97 {
100 errmsg("could not generate a random number")));
101 }
102 else
103 {
104 /* For other errors, use the message from the above list. */
107 errmsg("%s", px_strerror(err))));
108 }
109}
int errcode(int sqlerrcode)
Definition elog.c:864
int errmsg(const char *fmt,...)
Definition elog.c:1081
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
const char * px_strerror(int err)
Definition px.c:112
#define PXE_NO_RANDOM
Definition px.h:63

References ereport, err(), errcode(), errmsg(), ERROR, fb(), 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 141 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 42 of file px.c.

42 {
43 {PXE_OK, "Everything ok"},
44 {PXE_NO_HASH, "No such hash algorithm"},
45 {PXE_NO_CIPHER, "No such cipher algorithm"},
46 {PXE_BAD_OPTION, "Unknown option"},
47 {PXE_BAD_FORMAT, "Badly formatted type"},
48 {PXE_KEY_TOO_BIG, "Key was too big"},
49 {PXE_CIPHER_INIT, "Cipher cannot be initialized"},
50 {PXE_HASH_UNUSABLE_FOR_HMAC, "This hash algorithm is unusable for HMAC"},
51 {PXE_BUG, "pgcrypto bug"},
52 {PXE_ARGUMENT_ERROR, "Illegal argument to function"},
53 {PXE_UNKNOWN_SALT_ALGO, "Unknown salt algorithm"},
54 {PXE_BAD_SALT_ROUNDS, "Incorrect number of rounds"},
55 {PXE_NO_RANDOM, "Failed to generate strong random bits"},
56 {PXE_DECRYPT_FAILED, "Decryption failed"},
57 {PXE_ENCRYPT_FAILED, "Encryption failed"},
58 {PXE_PGP_CORRUPT_DATA, "Wrong key or corrupt data"},
59 {PXE_PGP_CORRUPT_ARMOR, "Corrupt ascii-armor"},
60 {PXE_PGP_UNSUPPORTED_COMPR, "Unsupported compression algorithm"},
61 {PXE_PGP_UNSUPPORTED_CIPHER, "Unsupported cipher algorithm"},
62 {PXE_PGP_UNSUPPORTED_HASH, "Unsupported digest algorithm"},
63 {PXE_PGP_COMPRESSION_ERROR, "Compression error"},
64 {PXE_PGP_NOT_TEXT, "Not text data"},
65 {PXE_PGP_UNEXPECTED_PKT, "Unexpected packet in key data"},
66 {PXE_PGP_MATH_FAILED, "Math operation failed"},
67 {PXE_PGP_SHORT_ELGAMAL_KEY, "Elgamal keys must be at least 1024 bits long"},
68 {PXE_PGP_KEY_TOO_BIG, "Session key too big"},
69 {PXE_PGP_UNKNOWN_PUBALGO, "Unknown public-key encryption algorithm"},
70 {PXE_PGP_WRONG_KEY, "Wrong key"},
72 "Several keys given - pgcrypto does not handle keyring"},
73 {PXE_PGP_EXPECT_PUBLIC_KEY, "Refusing to encrypt with secret key"},
74 {PXE_PGP_EXPECT_SECRET_KEY, "Cannot decrypt with public key"},
75 {PXE_PGP_NOT_V4_KEYPKT, "Only V4 key packets are supported"},
76 {PXE_PGP_KEYPKT_CORRUPT, "Corrupt key packet"},
77 {PXE_PGP_NO_USABLE_KEY, "No encryption key found"},
78 {PXE_PGP_NEED_SECRET_PSW, "Need password for secret key"},
79 {PXE_PGP_BAD_S2K_MODE, "Bad S2K mode"},
80 {PXE_PGP_UNSUPPORTED_PUBALGO, "Unsupported public key algorithm"},
81 {PXE_PGP_MULTIPLE_SUBKEYS, "Several subkeys not supported"},
82
83 {0, NULL},
84};
#define PXE_PGP_EXPECT_PUBLIC_KEY
Definition px.h:82
#define PXE_PGP_KEY_TOO_BIG
Definition px.h:78
#define PXE_PGP_UNSUPPORTED_COMPR
Definition px.h:69
#define PXE_PGP_BAD_S2K_MODE
Definition px.h:88
#define PXE_OK
Definition px.h:46
#define PXE_DECRYPT_FAILED
Definition px.h:64
#define PXE_ARGUMENT_ERROR
Definition px.h:59
#define PXE_BAD_SALT_ROUNDS
Definition px.h:61
#define PXE_CIPHER_INIT
Definition px.h:54
#define PXE_PGP_UNEXPECTED_PKT
Definition px.h:74
#define PXE_PGP_MULTIPLE_KEYS
Definition px.h:81
#define PXE_ENCRYPT_FAILED
Definition px.h:65
#define PXE_PGP_UNSUPPORTED_PUBALGO
Definition px.h:89
#define PXE_PGP_NO_USABLE_KEY
Definition px.h:86
#define PXE_NO_HASH
Definition px.h:48
#define PXE_PGP_EXPECT_SECRET_KEY
Definition px.h:83
#define PXE_HASH_UNUSABLE_FOR_HMAC
Definition px.h:55
#define PXE_BUG
Definition px.h:58
#define PXE_PGP_NEED_SECRET_PSW
Definition px.h:87
#define PXE_PGP_MULTIPLE_SUBKEYS
Definition px.h:90
#define PXE_PGP_COMPRESSION_ERROR
Definition px.h:72
#define PXE_PGP_WRONG_KEY
Definition px.h:80
#define PXE_PGP_CORRUPT_DATA
Definition px.h:67
#define PXE_UNKNOWN_SALT_ALGO
Definition px.h:60
#define PXE_PGP_MATH_FAILED
Definition px.h:76
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition px.h:70
#define PXE_PGP_NOT_TEXT
Definition px.h:73
#define PXE_PGP_UNKNOWN_PUBALGO
Definition px.h:79
#define PXE_PGP_SHORT_ELGAMAL_KEY
Definition px.h:77
#define PXE_PGP_CORRUPT_ARMOR
Definition px.h:68
#define PXE_PGP_KEYPKT_CORRUPT
Definition px.h:85
#define PXE_PGP_UNSUPPORTED_HASH
Definition px.h:71
#define PXE_KEY_TOO_BIG
Definition px.h:53
#define PXE_PGP_NOT_V4_KEYPKT
Definition px.h:84

Referenced by px_strerror().