PostgreSQL Source Code  git master
pgp.c File Reference
#include "postgres.h"
#include "pgp.h"
#include "px.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

◆ get_cipher_info()

static const struct cipher_info* get_cipher_info ( int  code)
static

Definition at line 93 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().

94 {
95  const struct cipher_info *i;
96 
97  for (i = cipher_list; i->name; i++)
98  if (i->code == code)
99  return i;
100  return NULL;
101 }
const char * name
Definition: pgp.c:61
int i
static const struct cipher_info cipher_list[]
Definition: pgp.c:79
int code
Definition: pgp.c:62

◆ pgp_disable_mdc()

int pgp_disable_mdc ( PGP_Context ctx,
int  disable 
)

Definition at line 234 of file pgp.c.

References PGP_Context::disable_mdc.

Referenced by set_arg().

235 {
236  ctx->disable_mdc = disable ? 1 : 0;
237  return 0;
238 }
int disable_mdc
Definition: pgp.h:147

◆ pgp_free()

int pgp_free ( PGP_Context ctx)

Definition at line 224 of file pgp.c.

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

Referenced by decrypt_internal(), and encrypt_internal().

225 {
226  if (ctx->pub_key)
227  pgp_key_free(ctx->pub_key);
228  px_memset(ctx, 0, sizeof *ctx);
229  px_free(ctx);
230  return 0;
231 }
#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:126

◆ pgp_get_cipher_block_size()

int pgp_get_cipher_block_size ( int  code)

Definition at line 157 of file pgp.c.

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

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

158 {
159  const struct cipher_info *i = get_cipher_info(code);
160 
161  if (i != NULL)
162  return i->block_len;
163  return 0;
164 }
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:93
int block_len
Definition: pgp.c:65
int i
int code
Definition: pgp.c:62

◆ pgp_get_cipher_code()

int pgp_get_cipher_code ( const char *  name)

Definition at line 115 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().

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

◆ pgp_get_cipher_key_size()

int pgp_get_cipher_key_size ( int  code)

Definition at line 147 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().

148 {
149  const struct cipher_info *i = get_cipher_info(code);
150 
151  if (i != NULL)
152  return i->key_len;
153  return 0;
154 }
int key_len
Definition: pgp.c:64
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:93
int i
int code
Definition: pgp.c:62

◆ pgp_get_cipher_name()

const char* pgp_get_cipher_name ( int  code)

Definition at line 137 of file pgp.c.

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

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

◆ pgp_get_digest_code()

int pgp_get_digest_code ( const char *  name)

Definition at line 104 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().

105 {
106  const struct digest_info *i;
107 
108  for (i = digest_list; i->name; i++)
109  if (pg_strcasecmp(i->name, name) == 0)
110  return i->code;
112 }
#define PXE_PGP_UNSUPPORTED_HASH
Definition: px.h:82
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:68

◆ pgp_get_digest_name()

const char* pgp_get_digest_name ( int  code)

Definition at line 126 of file pgp.c.

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

Referenced by pgp_load_digest().

127 {
128  const struct digest_info *i;
129 
130  for (i = digest_list; i->name; i++)
131  if (i->code == code)
132  return i->name;
133  return NULL;
134 }
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:68

◆ pgp_get_unicode_mode()

int pgp_get_unicode_mode ( PGP_Context ctx)

Definition at line 351 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by decrypt_internal(), and encrypt_internal().

352 {
353  return ctx->unicode_mode;
354 }
int unicode_mode
Definition: pgp.h:151

◆ pgp_init()

int pgp_init ( PGP_Context **  ctx_p)

Definition at line 199 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().

200 {
201  PGP_Context *ctx;
202 
203  ctx = px_alloc(sizeof *ctx);
204  memset(ctx, 0, sizeof *ctx);
205 
208  ctx->s2k_mode = def_s2k_mode;
209  ctx->s2k_count = def_s2k_count;
217  ctx->text_mode = def_text_mode;
218 
219  *ctx_p = ctx;
220  return 0;
221 }
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

◆ pgp_load_cipher()

int pgp_load_cipher ( int  code,
PX_Cipher **  res 
)

Definition at line 167 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().

168 {
169  int err;
170  const struct cipher_info *i = get_cipher_info(code);
171 
172  if (i == NULL)
173  return PXE_PGP_CORRUPT_DATA;
174 
175  err = px_find_cipher(i->int_name, res);
176  if (err == 0)
177  return 0;
178 
180 }
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:78
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition: px.h:81
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:93
const char * int_name
Definition: pgp.c:63
int i
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: internal.c:578
int code
Definition: pgp.c:62

◆ pgp_load_digest()

int pgp_load_digest ( int  code,
PX_MD **  res 
)

Definition at line 183 of file pgp.c.

References digest_info::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().

184 {
185  int err;
186  const char *name = pgp_get_digest_name(code);
187 
188  if (name == NULL)
189  return PXE_PGP_CORRUPT_DATA;
190 
191  err = px_find_digest(name, res);
192  if (err == 0)
193  return 0;
194 
196 }
#define PXE_PGP_UNSUPPORTED_HASH
Definition: px.h:82
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:78
const char * pgp_get_digest_name(int code)
Definition: pgp.c:126
const char * name
Definition: encode.c:521
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:559
int code
Definition: pgp.c:62

◆ pgp_set_cipher_algo()

int pgp_set_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 318 of file pgp.c.

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

Referenced by set_arg().

319 {
321 
322  if (code < 0)
323  return code;
324  ctx->cipher_algo = code;
325  return 0;
326 }
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:115
int cipher_algo
Definition: pgp.h:144
const char * name
Definition: encode.c:521
int code
Definition: pgp.c:62

◆ pgp_set_compress_algo()

int pgp_set_compress_algo ( PGP_Context ctx,
int  algo 
)

Definition at line 285 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().

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

◆ pgp_set_compress_level()

int pgp_set_compress_level ( PGP_Context ctx,
int  level 
)

Definition at line 300 of file pgp.c.

References PGP_Context::compress_level, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

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

◆ pgp_set_convert_crlf()

int pgp_set_convert_crlf ( PGP_Context ctx,
int  doit 
)

Definition at line 248 of file pgp.c.

References PGP_Context::convert_crlf.

Referenced by set_arg().

249 {
250  ctx->convert_crlf = doit ? 1 : 0;
251  return 0;
252 }
int convert_crlf
Definition: pgp.h:150

◆ pgp_set_s2k_cipher_algo()

int pgp_set_s2k_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 329 of file pgp.c.

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

Referenced by set_arg().

330 {
332 
333  if (code < 0)
334  return code;
335  ctx->s2k_cipher_algo = code;
336  return 0;
337 }
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:115
int s2k_cipher_algo
Definition: pgp.h:143
const char * name
Definition: encode.c:521
int code
Definition: pgp.c:62

◆ pgp_set_s2k_count()

int pgp_set_s2k_count ( PGP_Context ctx,
int  count 
)

Definition at line 274 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().

275 {
276  if (ctx->s2k_mode == PGP_S2K_ISALTED && count >= 1024 && count <= 65011712)
277  {
278  ctx->s2k_count = count;
279  return PXE_OK;
280  }
281  return PXE_ARGUMENT_ERROR;
282 }
#define PXE_OK
Definition: px.h:59
#define PXE_ARGUMENT_ERROR
Definition: px.h:71
int s2k_mode
Definition: pgp.h:140
int s2k_count
Definition: pgp.h:141

◆ pgp_set_s2k_digest_algo()

int pgp_set_s2k_digest_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 340 of file pgp.c.

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

Referenced by set_arg().

341 {
343 
344  if (code < 0)
345  return code;
346  ctx->s2k_digest_algo = code;
347  return 0;
348 }
int pgp_get_digest_code(const char *name)
Definition: pgp.c:104
const char * name
Definition: encode.c:521
int s2k_digest_algo
Definition: pgp.h:142
int code
Definition: pgp.c:62

◆ pgp_set_s2k_mode()

int pgp_set_s2k_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 255 of file pgp.c.

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

Referenced by set_arg().

256 {
257  int err = PXE_OK;
258 
259  switch (mode)
260  {
261  case PGP_S2K_SIMPLE:
262  case PGP_S2K_SALTED:
263  case PGP_S2K_ISALTED:
264  ctx->s2k_mode = mode;
265  break;
266  default:
267  err = PXE_ARGUMENT_ERROR;
268  break;
269  }
270  return err;
271 }
#define PXE_OK
Definition: px.h:59
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define PXE_ARGUMENT_ERROR
Definition: px.h:71
int s2k_mode
Definition: pgp.h:140

◆ pgp_set_sess_key()

int pgp_set_sess_key ( PGP_Context ctx,
int  use 
)

Definition at line 241 of file pgp.c.

References PGP_Context::use_sess_key.

Referenced by set_arg().

242 {
243  ctx->use_sess_key = use ? 1 : 0;
244  return 0;
245 }
int use_sess_key
Definition: pgp.h:148

◆ pgp_set_symkey()

int pgp_set_symkey ( PGP_Context ctx,
const uint8 key,
int  len 
)

Definition at line 364 of file pgp.c.

References sort-test::key, PXE_ARGUMENT_ERROR, PGP_Context::sym_key, and PGP_Context::sym_key_len.

Referenced by decrypt_internal(), and encrypt_internal().

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

◆ pgp_set_text_mode()

int pgp_set_text_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 311 of file pgp.c.

References mode, and PGP_Context::text_mode.

Referenced by init_work().

312 {
313  ctx->text_mode = mode;
314  return 0;
315 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
int text_mode
Definition: pgp.h:149

◆ pgp_set_unicode_mode()

int pgp_set_unicode_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 357 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by set_arg().

358 {
359  ctx->unicode_mode = mode ? 1 : 0;
360  return 0;
361 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
int unicode_mode
Definition: pgp.h:151

Variable Documentation

◆ cipher_list

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 79 of file pgp.c.

◆ def_cipher_algo

int def_cipher_algo = PGP_SYM_AES_128
static

Definition at line 40 of file pgp.c.

Referenced by pgp_init().

◆ def_compress_algo

int def_compress_algo = PGP_COMPR_NONE
static

Definition at line 45 of file pgp.c.

Referenced by pgp_init().

◆ def_compress_level

int def_compress_level = 6
static

Definition at line 46 of file pgp.c.

Referenced by pgp_init().

◆ def_convert_crlf

int def_convert_crlf = 0
static

Definition at line 51 of file pgp.c.

Referenced by pgp_init().

◆ def_disable_mdc

int def_disable_mdc = 0
static

Definition at line 47 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_cipher_algo

int def_s2k_cipher_algo = -1
static

Definition at line 41 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_count

int def_s2k_count = -1
static

Definition at line 43 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_digest_algo

int def_s2k_digest_algo = PGP_DIGEST_SHA1
static

Definition at line 44 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_mode

int def_s2k_mode = PGP_S2K_ISALTED
static

Definition at line 42 of file pgp.c.

Referenced by pgp_init().

◆ def_text_mode

int def_text_mode = 0
static

Definition at line 49 of file pgp.c.

Referenced by pgp_init().

◆ def_unicode_mode

int def_unicode_mode = 0
static

Definition at line 50 of file pgp.c.

Referenced by pgp_init().

◆ def_use_sess_key

int def_use_sess_key = 0
static

Definition at line 48 of file pgp.c.

Referenced by pgp_init().

◆ digest_list

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 68 of file pgp.c.