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

Go to the source code of this file.

Data Structures

struct  digest_info
 
struct  cipher_info
 

Functions

static const struct cipher_infoget_cipher_info (int code)
 
int pgp_get_digest_code (const char *name)
 
int pgp_get_cipher_code (const char *name)
 
const char * pgp_get_digest_name (int code)
 
const char * pgp_get_cipher_name (int code)
 
int pgp_get_cipher_key_size (int code)
 
int pgp_get_cipher_block_size (int code)
 
int pgp_load_cipher (int code, PX_Cipher **res)
 
int pgp_load_digest (int code, PX_MD **res)
 
int pgp_init (PGP_Context **ctx_p)
 
int pgp_free (PGP_Context *ctx)
 
int pgp_disable_mdc (PGP_Context *ctx, int disable)
 
int pgp_set_sess_key (PGP_Context *ctx, int use)
 
int pgp_set_convert_crlf (PGP_Context *ctx, int doit)
 
int pgp_set_s2k_mode (PGP_Context *ctx, int mode)
 
int pgp_set_s2k_count (PGP_Context *ctx, int count)
 
int pgp_set_compress_algo (PGP_Context *ctx, int algo)
 
int pgp_set_compress_level (PGP_Context *ctx, int level)
 
int pgp_set_text_mode (PGP_Context *ctx, int mode)
 
int pgp_set_cipher_algo (PGP_Context *ctx, const char *name)
 
int pgp_set_s2k_cipher_algo (PGP_Context *ctx, const char *name)
 
int pgp_set_s2k_digest_algo (PGP_Context *ctx, const char *name)
 
int pgp_get_unicode_mode (PGP_Context *ctx)
 
int pgp_set_unicode_mode (PGP_Context *ctx, int mode)
 
int pgp_set_symkey (PGP_Context *ctx, const uint8 *key, int len)
 

Variables

static int def_cipher_algo = PGP_SYM_AES_128
 
static int def_s2k_cipher_algo = -1
 
static int def_s2k_mode = PGP_S2K_ISALTED
 
static int def_s2k_count = -1
 
static int def_s2k_digest_algo = PGP_DIGEST_SHA1
 
static int def_compress_algo = PGP_COMPR_NONE
 
static int def_compress_level = 6
 
static int def_disable_mdc = 0
 
static int def_use_sess_key = 0
 
static int def_text_mode = 0
 
static int def_unicode_mode = 0
 
static int def_convert_crlf = 0
 
static const struct digest_info digest_list []
 
static const struct cipher_info cipher_list []
 

Function Documentation

static const struct cipher_info* get_cipher_info ( int  code)
static

Definition at line 94 of file pgp.c.

References cipher_info::code, i, and cipher_info::name.

Referenced by pgp_get_cipher_block_size(), pgp_get_cipher_key_size(), pgp_get_cipher_name(), and pgp_load_cipher().

95 {
96  const struct cipher_info *i;
97 
98  for (i = cipher_list; i->name; i++)
99  if (i->code == code)
100  return i;
101  return NULL;
102 }
const char * name
Definition: pgp.c:62
int i
static const struct cipher_info cipher_list[]
Definition: pgp.c:80
int code
Definition: pgp.c:63
int pgp_disable_mdc ( PGP_Context ctx,
int  disable 
)

Definition at line 235 of file pgp.c.

References PGP_Context::disable_mdc.

Referenced by set_arg().

236 {
237  ctx->disable_mdc = disable ? 1 : 0;
238  return 0;
239 }
int disable_mdc
Definition: pgp.h:147
int pgp_free ( PGP_Context ctx)

Definition at line 225 of file pgp.c.

References pgp_key_free(), PGP_Context::pub_key, px_free, and px_memset().

Referenced by decrypt_internal(), and encrypt_internal().

226 {
227  if (ctx->pub_key)
228  pgp_key_free(ctx->pub_key);
229  px_memset(ctx, 0, sizeof *ctx);
230  px_free(ctx);
231  return 0;
232 }
#define px_free(p)
Definition: px.h:46
void pgp_key_free(PGP_PubKey *pk)
Definition: pgp-pubkey.c:49
PGP_PubKey * pub_key
Definition: pgp.h:164
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:134
int pgp_get_cipher_block_size ( int  code)

Definition at line 158 of file pgp.c.

References cipher_info::block_len, get_cipher_info(), and i.

Referenced by prefix_init(), process_secret_key(), and write_prefix().

159 {
160  const struct cipher_info *i = get_cipher_info(code);
161 
162  if (i != NULL)
163  return i->block_len;
164  return 0;
165 }
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:94
int block_len
Definition: pgp.c:66
int i
int code
Definition: pgp.c:63
int pgp_get_cipher_code ( const char *  name)

Definition at line 116 of file pgp.c.

References cipher_info::code, i, cipher_info::name, pg_strcasecmp(), and PXE_PGP_UNSUPPORTED_CIPHER.

Referenced by pgp_set_cipher_algo(), pgp_set_s2k_cipher_algo(), and set_arg().

117 {
118  const struct cipher_info *i;
119 
120  for (i = cipher_list; i->name; i++)
121  if (pg_strcasecmp(i->name, name) == 0)
122  return i->code;
124 }
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition: px.h:84
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * name
Definition: pgp.c:62
const char * name
Definition: encode.c:521
int i
static const struct cipher_info cipher_list[]
Definition: pgp.c:80
int code
Definition: pgp.c:63
int pgp_get_cipher_key_size ( int  code)

Definition at line 148 of file pgp.c.

References get_cipher_info(), i, and cipher_info::key_len.

Referenced by decrypt_key(), init_sess_key(), and pgp_s2k_process().

149 {
150  const struct cipher_info *i = get_cipher_info(code);
151 
152  if (i != NULL)
153  return i->key_len;
154  return 0;
155 }
int key_len
Definition: pgp.c:65
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:94
int i
int code
Definition: pgp.c:63
const char* pgp_get_cipher_name ( int  code)

Definition at line 138 of file pgp.c.

References get_cipher_info(), i, and cipher_info::name.

139 {
140  const struct cipher_info *i = get_cipher_info(code);
141 
142  if (i != NULL)
143  return i->name;
144  return NULL;
145 }
const char * name
Definition: pgp.c:62
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:94
int i
int code
Definition: pgp.c:63
int pgp_get_digest_code ( const char *  name)

Definition at line 105 of file pgp.c.

References digest_info::code, i, digest_info::name, pg_strcasecmp(), and PXE_PGP_UNSUPPORTED_HASH.

Referenced by pgp_set_s2k_digest_algo(), and set_arg().

106 {
107  const struct digest_info *i;
108 
109  for (i = digest_list; i->name; i++)
110  if (pg_strcasecmp(i->name, name) == 0)
111  return i->code;
113 }
#define PXE_PGP_UNSUPPORTED_HASH
Definition: px.h:85
const char * name
Definition: pgp.c:55
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int code
Definition: pgp.c:56
const char * name
Definition: encode.c:521
int i
static const struct digest_info digest_list[]
Definition: pgp.c:69
const char* pgp_get_digest_name ( int  code)

Definition at line 127 of file pgp.c.

References digest_info::code, i, and digest_info::name.

Referenced by pgp_load_digest().

128 {
129  const struct digest_info *i;
130 
131  for (i = digest_list; i->name; i++)
132  if (i->code == code)
133  return i->name;
134  return NULL;
135 }
const char * name
Definition: pgp.c:55
int code
Definition: pgp.c:56
int i
static const struct digest_info digest_list[]
Definition: pgp.c:69
int pgp_get_unicode_mode ( PGP_Context ctx)

Definition at line 352 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by decrypt_internal(), and encrypt_internal().

353 {
354  return ctx->unicode_mode;
355 }
int unicode_mode
Definition: pgp.h:151
int pgp_init ( PGP_Context **  ctx_p)

Definition at line 200 of file pgp.c.

References PGP_Context::cipher_algo, PGP_Context::compress_algo, PGP_Context::compress_level, PGP_Context::convert_crlf, def_cipher_algo, def_compress_algo, def_compress_level, def_convert_crlf, def_disable_mdc, def_s2k_cipher_algo, def_s2k_count, def_s2k_digest_algo, def_s2k_mode, def_text_mode, def_unicode_mode, def_use_sess_key, PGP_Context::disable_mdc, px_alloc, PGP_Context::s2k_cipher_algo, PGP_Context::s2k_count, PGP_Context::s2k_digest_algo, PGP_Context::s2k_mode, PGP_Context::text_mode, PGP_Context::unicode_mode, and PGP_Context::use_sess_key.

Referenced by init_work().

201 {
202  PGP_Context *ctx;
203 
204  ctx = px_alloc(sizeof *ctx);
205  memset(ctx, 0, sizeof *ctx);
206 
209  ctx->s2k_mode = def_s2k_mode;
210  ctx->s2k_count = def_s2k_count;
218  ctx->text_mode = def_text_mode;
219 
220  *ctx_p = ctx;
221  return 0;
222 }
static int def_convert_crlf
Definition: pgp.c:51
int cipher_algo
Definition: pgp.h:144
static int def_use_sess_key
Definition: pgp.c:48
static int def_s2k_mode
Definition: pgp.c:42
int unicode_mode
Definition: pgp.h:151
static int def_s2k_count
Definition: pgp.c:43
int compress_level
Definition: pgp.h:146
static int def_compress_algo
Definition: pgp.c:45
static int def_compress_level
Definition: pgp.c:46
int disable_mdc
Definition: pgp.h:147
int use_sess_key
Definition: pgp.h:148
static int def_text_mode
Definition: pgp.c:49
static int def_s2k_cipher_algo
Definition: pgp.c:41
static int def_unicode_mode
Definition: pgp.c:50
int compress_algo
Definition: pgp.h:145
static int def_cipher_algo
Definition: pgp.c:40
int s2k_cipher_algo
Definition: pgp.h:143
int convert_crlf
Definition: pgp.h:150
#define px_alloc(s)
Definition: px.h:44
int s2k_mode
Definition: pgp.h:140
int s2k_digest_algo
Definition: pgp.h:142
static int def_disable_mdc
Definition: pgp.c:47
static int def_s2k_digest_algo
Definition: pgp.c:44
int text_mode
Definition: pgp.h:149
int s2k_count
Definition: pgp.h:141
int pgp_load_cipher ( int  code,
PX_Cipher **  res 
)

Definition at line 168 of file pgp.c.

References get_cipher_info(), i, cipher_info::int_name, px_find_cipher(), PXE_PGP_CORRUPT_DATA, and PXE_PGP_UNSUPPORTED_CIPHER.

Referenced by pgp_cfb_create().

169 {
170  int err;
171  const struct cipher_info *i = get_cipher_info(code);
172 
173  if (i == NULL)
174  return PXE_PGP_CORRUPT_DATA;
175 
176  err = px_find_cipher(i->int_name, res);
177  if (err == 0)
178  return 0;
179 
181 }
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:81
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition: px.h:84
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:94
const char * int_name
Definition: pgp.c:64
int i
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: internal.c:597
int code
Definition: pgp.c:63
int pgp_load_digest ( int  code,
PX_MD **  res 
)

Definition at line 184 of file pgp.c.

References name, pgp_get_digest_name(), px_find_digest(), PXE_PGP_CORRUPT_DATA, and PXE_PGP_UNSUPPORTED_HASH.

Referenced by calc_key_id(), check_key_sha1(), mdc_init(), and pgp_s2k_process().

185 {
186  int err;
187  const char *name = pgp_get_digest_name(code);
188 
189  if (name == NULL)
190  return PXE_PGP_CORRUPT_DATA;
191 
192  err = px_find_digest(name, res);
193  if (err == 0)
194  return 0;
195 
197 }
#define PXE_PGP_UNSUPPORTED_HASH
Definition: px.h:85
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:81
const char * pgp_get_digest_name(int code)
Definition: pgp.c:127
const char * name
Definition: encode.c:521
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:578
int code
Definition: pgp.c:63
int pgp_set_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 319 of file pgp.c.

References PGP_Context::cipher_algo, cipher_info::code, and pgp_get_cipher_code().

Referenced by set_arg().

320 {
322 
323  if (code < 0)
324  return code;
325  ctx->cipher_algo = code;
326  return 0;
327 }
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:116
int cipher_algo
Definition: pgp.h:144
const char * name
Definition: encode.c:521
int code
Definition: pgp.c:63
int pgp_set_compress_algo ( PGP_Context ctx,
int  algo 
)

Definition at line 286 of file pgp.c.

References PGP_Context::compress_algo, PGP_COMPR_BZIP2, PGP_COMPR_NONE, PGP_COMPR_ZIP, PGP_COMPR_ZLIB, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

287 {
288  switch (algo)
289  {
290  case PGP_COMPR_NONE:
291  case PGP_COMPR_ZIP:
292  case PGP_COMPR_ZLIB:
293  case PGP_COMPR_BZIP2:
294  ctx->compress_algo = algo;
295  return 0;
296  }
297  return PXE_ARGUMENT_ERROR;
298 }
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int compress_algo
Definition: pgp.h:145
int pgp_set_compress_level ( PGP_Context ctx,
int  level 
)

Definition at line 301 of file pgp.c.

References PGP_Context::compress_level, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

302 {
303  if (level >= 0 && level <= 9)
304  {
305  ctx->compress_level = level;
306  return 0;
307  }
308  return PXE_ARGUMENT_ERROR;
309 }
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int compress_level
Definition: pgp.h:146
int pgp_set_convert_crlf ( PGP_Context ctx,
int  doit 
)

Definition at line 249 of file pgp.c.

References PGP_Context::convert_crlf.

Referenced by set_arg().

250 {
251  ctx->convert_crlf = doit ? 1 : 0;
252  return 0;
253 }
int convert_crlf
Definition: pgp.h:150
int pgp_set_s2k_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 330 of file pgp.c.

References cipher_info::code, pgp_get_cipher_code(), and PGP_Context::s2k_cipher_algo.

Referenced by set_arg().

331 {
333 
334  if (code < 0)
335  return code;
336  ctx->s2k_cipher_algo = code;
337  return 0;
338 }
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:116
int s2k_cipher_algo
Definition: pgp.h:143
const char * name
Definition: encode.c:521
int code
Definition: pgp.c:63
int pgp_set_s2k_count ( PGP_Context ctx,
int  count 
)

Definition at line 275 of file pgp.c.

References PGP_S2K_ISALTED, PXE_ARGUMENT_ERROR, PXE_OK, PGP_Context::s2k_count, and PGP_Context::s2k_mode.

Referenced by set_arg().

276 {
277  if (ctx->s2k_mode == PGP_S2K_ISALTED && count >= 1024 && count <= 65011712)
278  {
279  ctx->s2k_count = count;
280  return PXE_OK;
281  }
282  return PXE_ARGUMENT_ERROR;
283 }
#define PXE_OK
Definition: px.h:62
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int s2k_mode
Definition: pgp.h:140
int s2k_count
Definition: pgp.h:141
int pgp_set_s2k_digest_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 341 of file pgp.c.

References cipher_info::code, pgp_get_digest_code(), and PGP_Context::s2k_digest_algo.

Referenced by set_arg().

342 {
344 
345  if (code < 0)
346  return code;
347  ctx->s2k_digest_algo = code;
348  return 0;
349 }
int pgp_get_digest_code(const char *name)
Definition: pgp.c:105
const char * name
Definition: encode.c:521
int s2k_digest_algo
Definition: pgp.h:142
int code
Definition: pgp.c:63
int pgp_set_s2k_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 256 of file pgp.c.

References PGP_S2K_ISALTED, PGP_S2K_SALTED, PGP_S2K_SIMPLE, PXE_ARGUMENT_ERROR, PXE_OK, and PGP_Context::s2k_mode.

Referenced by set_arg().

257 {
258  int err = PXE_OK;
259 
260  switch (mode)
261  {
262  case PGP_S2K_SIMPLE:
263  case PGP_S2K_SALTED:
264  case PGP_S2K_ISALTED:
265  ctx->s2k_mode = mode;
266  break;
267  default:
268  err = PXE_ARGUMENT_ERROR;
269  break;
270  }
271  return err;
272 }
#define PXE_OK
Definition: px.h:62
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int s2k_mode
Definition: pgp.h:140
int pgp_set_sess_key ( PGP_Context ctx,
int  use 
)

Definition at line 242 of file pgp.c.

References PGP_Context::use_sess_key.

Referenced by set_arg().

243 {
244  ctx->use_sess_key = use ? 1 : 0;
245  return 0;
246 }
int use_sess_key
Definition: pgp.h:148
int pgp_set_symkey ( PGP_Context ctx,
const uint8 key,
int  len 
)

Definition at line 365 of file pgp.c.

References PXE_ARGUMENT_ERROR, PGP_Context::sym_key, and PGP_Context::sym_key_len.

Referenced by decrypt_internal(), and encrypt_internal().

366 {
367  if (key == NULL || len < 1)
368  return PXE_ARGUMENT_ERROR;
369  ctx->sym_key = key;
370  ctx->sym_key_len = len;
371  return 0;
372 }
#define PXE_ARGUMENT_ERROR
Definition: px.h:74
int sym_key_len
Definition: pgp.h:166
const uint8 * sym_key
Definition: pgp.h:165
int pgp_set_text_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 312 of file pgp.c.

References PGP_Context::text_mode.

Referenced by init_work().

313 {
314  ctx->text_mode = mode;
315  return 0;
316 }
int text_mode
Definition: pgp.h:149
int pgp_set_unicode_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 358 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by set_arg().

359 {
360  ctx->unicode_mode = mode ? 1 : 0;
361  return 0;
362 }
int unicode_mode
Definition: pgp.h:151

Variable Documentation

const struct cipher_info cipher_list[]
static
Initial value:
= {
{"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
{"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
{"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
{"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
{"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
{NULL, 0, NULL}
}

Definition at line 80 of file pgp.c.

int def_cipher_algo = PGP_SYM_AES_128
static

Definition at line 40 of file pgp.c.

Referenced by pgp_init().

int def_compress_algo = PGP_COMPR_NONE
static

Definition at line 45 of file pgp.c.

Referenced by pgp_init().

int def_compress_level = 6
static

Definition at line 46 of file pgp.c.

Referenced by pgp_init().

int def_convert_crlf = 0
static

Definition at line 51 of file pgp.c.

Referenced by pgp_init().

int def_disable_mdc = 0
static

Definition at line 47 of file pgp.c.

Referenced by pgp_init().

int def_s2k_cipher_algo = -1
static

Definition at line 41 of file pgp.c.

Referenced by pgp_init().

int def_s2k_count = -1
static

Definition at line 43 of file pgp.c.

Referenced by pgp_init().

int def_s2k_digest_algo = PGP_DIGEST_SHA1
static

Definition at line 44 of file pgp.c.

Referenced by pgp_init().

int def_s2k_mode = PGP_S2K_ISALTED
static

Definition at line 42 of file pgp.c.

Referenced by pgp_init().

int def_text_mode = 0
static

Definition at line 49 of file pgp.c.

Referenced by pgp_init().

int def_unicode_mode = 0
static

Definition at line 50 of file pgp.c.

Referenced by pgp_init().

int def_use_sess_key = 0
static

Definition at line 48 of file pgp.c.

Referenced by pgp_init().

const struct digest_info digest_list[]
static
Initial value:
= {
{"md5", PGP_DIGEST_MD5},
{"sha1", PGP_DIGEST_SHA1},
{"sha-1", PGP_DIGEST_SHA1},
{"ripemd160", PGP_DIGEST_RIPEMD160},
{"sha256", PGP_DIGEST_SHA256},
{"sha384", PGP_DIGEST_SHA384},
{"sha512", PGP_DIGEST_SHA512},
{NULL, 0}
}

Definition at line 69 of file pgp.c.