PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgp.h
Go to the documentation of this file.
1 /*
2  * pgp.h
3  * OpenPGP implementation.
4  *
5  * Copyright (c) 2005 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/pgp.h
30  */
31 
32 #include "lib/stringinfo.h"
33 
34 #include "mbuf.h"
35 #include "px.h"
36 
38 {
42 };
43 
45 {
63  PGP_PKT_PRIV_61 = 61 /* occurs in gpg secring */
64 };
65 
67 {
73 };
74 
76 {
77  PGP_SYM_PLAIN = 0, /* ?? */
78  PGP_SYM_IDEA = 1, /* obsolete, PGP 2.6 compat */
79  PGP_SYM_DES3 = 2, /* must */
80  PGP_SYM_CAST5 = 3, /* should */
82  PGP_SYM_SAFER_SK128 = 5, /* obsolete */
83  PGP_SYM_DES_SK = 6, /* obsolete */
84  PGP_SYM_AES_128 = 7, /* should */
88 };
89 
91 {
92  PGP_COMPR_NONE = 0, /* must */
93  PGP_COMPR_ZIP = 1, /* should */
96 };
97 
99 {
100  PGP_DIGEST_MD5 = 1, /* should, deprecated */
101  PGP_DIGEST_SHA1 = 2, /* must */
103  PGP_DIGEST_XSHA = 4, /* obsolete */
104  PGP_DIGEST_MD2 = 5, /* obsolete */
105  PGP_DIGEST_TIGER192 = 6, /* obsolete */
106  PGP_DIGEST_HAVAL5_160 = 7, /* obsolete */
110 };
111 
112 #define PGP_MAX_KEY (256/8)
113 #define PGP_MAX_BLOCK (256/8)
114 #define PGP_MAX_DIGEST (512/8)
115 #define PGP_S2K_SALT 8
116 
117 typedef struct PGP_MPI PGP_MPI;
118 typedef struct PGP_PubKey PGP_PubKey;
119 typedef struct PGP_Context PGP_Context;
120 typedef struct PGP_S2K PGP_S2K;
121 
122 struct PGP_S2K
123 {
127  uint8 iter; /* encoded (one-octet) count */
128  /* calculated: */
131 };
132 
133 
135 {
136  /*
137  * parameters
138  */
140  int s2k_mode;
141  int s2k_count; /* 4-byte decoded count */
152 
153  /*
154  * internal variables
155  */
157  int corrupt_prefix; /* prefix failed RFC 4880 "quick check" */
158  int unsupported_compr; /* has bzip2 compression */
159  int unexpected_binary; /* binary data seen in text_mode */
163 
164  PGP_PubKey *pub_key; /* ctx owns it */
165  const uint8 *sym_key; /* ctx does not own it */
167 
168  /*
169  * read or generated data
170  */
172  unsigned sess_key_len;
173 };
174 
175 /* from RFC 4880 3.7.1.3 */
176 #define s2k_decode_count(cval) \
177  (((unsigned) 16 + (cval & 15)) << ((cval >> 4) + 6))
178 
179 struct PGP_MPI
180 {
182  int bits;
183  int bytes;
184 };
185 
187 {
191 
192  /* public part */
193  union
194  {
195  struct
196  {
200  } elg;
201  struct
202  {
205  } rsa;
206  struct
207  {
208  PGP_MPI *p;
210  PGP_MPI *g;
211  PGP_MPI *y;
212  } dsa;
213  } pub;
214 
215  /* secret part */
216  union
217  {
218  struct
219  {
221  } elg;
222  struct
223  {
225  PGP_MPI *p;
226  PGP_MPI *q;
228  } rsa;
229  struct
230  {
231  PGP_MPI *x;
232  } dsa;
233  } sec;
234 
237 };
238 
239 int pgp_init(PGP_Context **ctx);
240 int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst);
241 int pgp_decrypt(PGP_Context *ctx, MBuf *src, MBuf *dst);
242 int pgp_free(PGP_Context *ctx);
243 
244 int pgp_get_digest_code(const char *name);
245 int pgp_get_cipher_code(const char *name);
246 const char *pgp_get_digest_name(int code);
247 const char *pgp_get_cipher_name(int code);
248 
249 int pgp_set_cipher_algo(PGP_Context *ctx, const char *name);
250 int pgp_set_s2k_mode(PGP_Context *ctx, int type);
251 int pgp_set_s2k_count(PGP_Context *ctx, int count);
252 int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name);
253 int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name);
254 int pgp_set_convert_crlf(PGP_Context *ctx, int doit);
255 int pgp_disable_mdc(PGP_Context *ctx, int disable);
256 int pgp_set_sess_key(PGP_Context *ctx, int use);
257 int pgp_set_compress_algo(PGP_Context *ctx, int algo);
258 int pgp_set_compress_level(PGP_Context *ctx, int level);
259 int pgp_set_text_mode(PGP_Context *ctx, int mode);
260 int pgp_set_unicode_mode(PGP_Context *ctx, int mode);
262 
263 int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int klen);
264 int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
265  const uint8 *key, int klen, int pubtype);
266 
267 int pgp_get_keyid(MBuf *pgp_data, char *dst);
268 
269 /* internal functions */
270 
271 int pgp_load_digest(int c, PX_MD **res);
272 int pgp_load_cipher(int c, PX_Cipher **res);
273 int pgp_get_cipher_key_size(int c);
275 
276 int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count);
277 int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k);
278 int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int klen);
279 
280 typedef struct PGP_CFB PGP_CFB;
281 int pgp_cfb_create(PGP_CFB **ctx_p, int algo,
282  const uint8 *key, int key_len, int recync, uint8 *iv);
283 void pgp_cfb_free(PGP_CFB *ctx);
284 int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
285 int pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
286 
287 void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst,
288  int num_headers, char **keys, char **values);
289 int pgp_armor_decode(const uint8 *src, int len, StringInfo dst);
290 int pgp_extract_armor_headers(const uint8 *src, unsigned len,
291  int *nheaders, char ***keys, char ***values);
292 
295 
296 int pgp_key_alloc(PGP_PubKey **pk_p);
297 void pgp_key_free(PGP_PubKey *pk);
298 int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p);
299 
301 int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len,
302  int pkttype, PGP_Context *ctx);
303 int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p,
304  int allow_ctx);
305 
306 int pgp_skip_packet(PullFilter *pkt);
308 
310 int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p);
311 
312 int pgp_mpi_alloc(int bits, PGP_MPI **mpi);
313 int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi);
314 int pgp_mpi_free(PGP_MPI *mpi);
315 int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi);
316 int pgp_mpi_write(PushFilter *dst, PGP_MPI *n);
317 int pgp_mpi_hash(PX_MD *md, PGP_MPI *n);
318 unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n);
319 
321  PGP_MPI **c1, PGP_MPI **c2);
323  PGP_MPI **m);
324 int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c);
325 int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *c, PGP_MPI **m);
326 
327 extern struct PullFilterOps pgp_decrypt_filter;
uint8 mode
Definition: pgp.h:124
int use_mdcbuf_filter
Definition: pgp.h:161
int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)
Definition: pgp-s2k.c:255
int pgp_set_sess_key(PGP_Context *ctx, int use)
Definition: pgp.c:242
int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
Definition: pgp-pubkey.c:159
int pgp_load_cipher(int c, PX_Cipher **res)
Definition: pgp.c:168
struct PGP_PubKey::@5::@7 elg
int pgp_get_cipher_key_size(int c)
Definition: pgp.c:148
int sym_key_len
Definition: pgp.h:166
int pgp_set_s2k_mode(PGP_Context *ctx, int type)
Definition: pgp.c:256
int pgp_disable_mdc(PGP_Context *ctx, int disable)
Definition: pgp.c:235
int pgp_key_alloc(PGP_PubKey **pk_p)
Definition: pgp-pubkey.c:38
PGP_MPI * p
Definition: pgp.h:197
int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int klen, int pubtype)
Definition: pgp-pubkey.c:566
PGP_SYMENC_TYPE
Definition: pgp.h:75
int in_mdc_pkt
Definition: pgp.h:160
uint8 ver
Definition: pgp.h:188
int cipher_algo
Definition: pgp.h:144
unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
Definition: pgp-mpi.c:132
int pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:251
int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *c1, PGP_MPI *c2, PGP_MPI **m)
uint8 * data
Definition: pgp.h:181
int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:330
struct PGP_PubKey::@5::@9 dsa
uint8 time[4]
Definition: pgp.h:189
int unexpected_binary
Definition: pgp.h:159
int pgp_get_digest_code(const char *name)
Definition: pgp.c:105
int unicode_mode
Definition: pgp.h:151
int pgp_set_text_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:312
PGP_PUB_ALGO_TYPE
Definition: pgp.h:66
int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)
Definition: pgp-armor.c:390
const uint8 * sym_key
Definition: pgp.h:165
unsigned char uint8
Definition: c.h:266
struct PullFilterOps pgp_decrypt_filter
Definition: pgp-decrypt.c:313
PGP_MPI * d
Definition: pgp.h:224
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
int compress_level
Definition: pgp.h:146
int unsupported_compr
Definition: pgp.h:158
int pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
Definition: pgp-pubdec.c:150
int pgp_get_keyid(MBuf *pgp_data, char *dst)
Definition: pgp-info.c:112
PGP_PKT_TYPE
Definition: pgp.h:44
int disable_mdc
Definition: pgp.h:147
void pgp_key_free(PGP_PubKey *pk)
Definition: pgp-pubkey.c:49
int pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
Definition: pgp-mpi.c:119
uint8 digest_algo
Definition: pgp.h:125
int use_sess_key
Definition: pgp.h:148
int pgp_load_digest(int c, PX_MD **res)
Definition: pgp.c:184
int bytes
Definition: pgp.h:183
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int klen)
Definition: pgp-s2k.c:281
int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)
Definition: pgp-armor.c:314
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
Definition: pgp-mpi.c:80
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
Definition: px.h:113
struct PGP_PubKey::@5::@8 rsa
int pgp_set_unicode_mode(PGP_Context *ctx, int mode)
Definition: pgp.c:358
#define PGP_MAX_KEY
Definition: pgp.h:112
int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
Definition: pgp-encrypt.c:315
char * c
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *c, PGP_MPI **m)
const char * pgp_get_cipher_name(int code)
Definition: pgp.c:138
PGP_MPI * y
Definition: pgp.h:199
PGP_COMPR_TYPE
Definition: pgp.h:90
uint8 salt[8]
Definition: pgp.h:126
uint8 key[PGP_MAX_KEY]
Definition: pgp.h:129
int compress_algo
Definition: pgp.h:145
int pgp_expect_packet_end(PullFilter *pkt)
Definition: pgp-decrypt.c:1075
PX_MD * mdc_ctx
Definition: pgp.h:162
int pgp_rsa_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c)
Definition: pgp.h:122
PGP_DIGEST_TYPE
Definition: pgp.h:98
void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)
Definition: pgp-armor.c:207
Definition: pgp.h:179
int pgp_init(PGP_Context **ctx)
Definition: pgp.c:200
PGP_MPI * q
Definition: pgp.h:209
int pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int klen)
Definition: pgp.c:365
PGP_MPI * g
Definition: pgp.h:198
int pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
Definition: pgp-compress.c:323
int pgp_set_compress_level(PGP_Context *ctx, int level)
Definition: pgp.c:301
int corrupt_prefix
Definition: pgp.h:157
void pgp_cfb_free(PGP_CFB *ctx)
Definition: pgp-cfb.c:84
int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int recync, uint8 *iv)
Definition: pgp-cfb.c:52
PGP_S2K_TYPE
Definition: pgp.h:37
int pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
Definition: pgp-compress.c:317
int pgp_decrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-decrypt.c:1090
int pgp_set_s2k_count(PGP_Context *ctx, int count)
Definition: pgp.c:275
int pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:56
PGP_MPI * u
Definition: pgp.h:227
int mdc_checked
Definition: pgp.h:156
union PGP_PubKey::@6 sec
PGP_MPI * e
Definition: pgp.h:204
unsigned sess_key_len
Definition: pgp.h:172
int pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
Definition: pgp-mpi.c:105
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
Definition: pgp-pubenc.c:195
int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len, int pkttype, PGP_Context *ctx)
Definition: pgp-decrypt.c:223
uint8 iter
Definition: pgp.h:127
int pgp_elgamal_encrypt(PGP_PubKey *pk, PGP_MPI *m, PGP_MPI **c1, PGP_MPI **c2)
int pgp_mpi_alloc(int bits, PGP_MPI **mpi)
Definition: pgp-mpi.c:37
Definition: mbuf.c:39
int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)
Definition: pgp-s2k.c:225
uint8 key_id[8]
Definition: pgp.h:235
int s2k_cipher_algo
Definition: pgp.h:143
PGP_MPI * x
Definition: pgp.h:220
Definition: px.h:154
union PGP_PubKey::@5 pub
uint8 key_len
Definition: pgp.h:130
const char * name
Definition: encode.c:521
int convert_crlf
Definition: pgp.h:150
uint8 algo
Definition: pgp.h:190
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int s2k_mode
Definition: pgp.h:140
int pgp_set_compress_algo(PGP_Context *ctx, int algo)
Definition: pgp.c:286
int s2k_digest_algo
Definition: pgp.h:142
int pgp_free(PGP_Context *ctx)
Definition: pgp.c:225
int pgp_set_convert_crlf(PGP_Context *ctx, int doit)
Definition: pgp.c:249
PGP_PubKey * pub_key
Definition: pgp.h:164
int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)
Definition: pgp-encrypt.c:611
PGP_MPI * n
Definition: pgp.h:203
int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p, int allow_ctx)
Definition: pgp-decrypt.c:129
int pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
Definition: pgp-cfb.c:259
int text_mode
Definition: pgp.h:149
int can_encrypt
Definition: pgp.h:236
int pgp_get_unicode_mode(PGP_Context *ctx)
Definition: pgp.c:352
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:116
int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:319
PGP_S2K s2k
Definition: pgp.h:139
int pgp_get_cipher_block_size(int c)
Definition: pgp.c:158
int s2k_count
Definition: pgp.h:141
int pgp_skip_packet(PullFilter *pkt)
Definition: pgp-decrypt.c:1061
int bits
Definition: pgp.h:182
int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
Definition: pgp.c:341
const char * pgp_get_digest_name(int code)
Definition: pgp.c:127