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

References PGP_Context::disable_mdc.

Referenced by set_arg().

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

◆ pgp_free()

int pgp_free ( PGP_Context ctx)

Definition at line 223 of file pgp.c.

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

Referenced by decrypt_internal(), and encrypt_internal().

224 {
225  if (ctx->pub_key)
226  pgp_key_free(ctx->pub_key);
227  px_memset(ctx, 0, sizeof *ctx);
228  pfree(ctx);
229  return 0;
230 }
void pfree(void *pointer)
Definition: mcxt.c:1057
void pgp_key_free(PGP_PubKey *pk)
Definition: pgp-pubkey.c:48
PGP_PubKey * pub_key
Definition: pgp.h:164
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:127

◆ 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:69
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:561
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:70
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:561
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 350 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by decrypt_internal(), and encrypt_internal().

351 {
352  return ctx->unicode_mode;
353 }
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, palloc0(), 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 = palloc0(sizeof *ctx);
204 
207  ctx->s2k_mode = def_s2k_mode;
208  ctx->s2k_count = def_s2k_count;
216  ctx->text_mode = def_text_mode;
217 
218  *ctx_p = ctx;
219  return 0;
220 }
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
void * palloc0(Size size)
Definition: mcxt.c:981
int s2k_cipher_algo
Definition: pgp.h:143
int convert_crlf
Definition: pgp.h:150
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:66
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition: px.h:69
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:572
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:70
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:66
const char * pgp_get_digest_name(int code)
Definition: pgp.c:126
const char * name
Definition: encode.c:561
int px_find_digest(const char *name, PX_MD **res)
Definition: internal.c:553
int code
Definition: pgp.c:62

◆ pgp_set_cipher_algo()

int pgp_set_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 317 of file pgp.c.

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

Referenced by set_arg().

318 {
320 
321  if (code < 0)
322  return code;
323  ctx->cipher_algo = code;
324  return 0;
325 }
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:561
int code
Definition: pgp.c:62

◆ pgp_set_compress_algo()

int pgp_set_compress_algo ( PGP_Context ctx,
int  algo 
)

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

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

◆ pgp_set_compress_level()

int pgp_set_compress_level ( PGP_Context ctx,
int  level 
)

Definition at line 299 of file pgp.c.

References PGP_Context::compress_level, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

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

◆ pgp_set_convert_crlf()

int pgp_set_convert_crlf ( PGP_Context ctx,
int  doit 
)

Definition at line 247 of file pgp.c.

References PGP_Context::convert_crlf.

Referenced by set_arg().

248 {
249  ctx->convert_crlf = doit ? 1 : 0;
250  return 0;
251 }
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 328 of file pgp.c.

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

Referenced by set_arg().

329 {
331 
332  if (code < 0)
333  return code;
334  ctx->s2k_cipher_algo = code;
335  return 0;
336 }
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:561
int code
Definition: pgp.c:62

◆ pgp_set_s2k_count()

int pgp_set_s2k_count ( PGP_Context ctx,
int  count 
)

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

274 {
275  if (ctx->s2k_mode == PGP_S2K_ISALTED && count >= 1024 && count <= 65011712)
276  {
277  ctx->s2k_count = count;
278  return PXE_OK;
279  }
280  return PXE_ARGUMENT_ERROR;
281 }
#define PXE_OK
Definition: px.h:46
#define PXE_ARGUMENT_ERROR
Definition: px.h:58
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 339 of file pgp.c.

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

Referenced by set_arg().

340 {
342 
343  if (code < 0)
344  return code;
345  ctx->s2k_digest_algo = code;
346  return 0;
347 }
int pgp_get_digest_code(const char *name)
Definition: pgp.c:104
const char * name
Definition: encode.c:561
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 254 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().

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

◆ pgp_set_sess_key()

int pgp_set_sess_key ( PGP_Context ctx,
int  use 
)

Definition at line 240 of file pgp.c.

References PGP_Context::use_sess_key.

Referenced by set_arg().

241 {
242  ctx->use_sess_key = use ? 1 : 0;
243  return 0;
244 }
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 363 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().

364 {
365  if (key == NULL || len < 1)
366  return PXE_ARGUMENT_ERROR;
367  ctx->sym_key = key;
368  ctx->sym_key_len = len;
369  return 0;
370 }
#define PXE_ARGUMENT_ERROR
Definition: px.h:58
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 310 of file pgp.c.

References mode, and PGP_Context::text_mode.

Referenced by init_work().

311 {
312  ctx->text_mode = mode;
313  return 0;
314 }
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 356 of file pgp.c.

References PGP_Context::unicode_mode.

Referenced by set_arg().

357 {
358  ctx->unicode_mode = mode ? 1 : 0;
359  return 0;
360 }
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.