PostgreSQL Source Code: contrib/pgcrypto/pgp.h File Reference (original) (raw)
#include "[lib/stringinfo.h](stringinfo%5F8h%5Fsource.html)"
#include "[mbuf.h](mbuf%5F8h%5Fsource.html)"
#include "[px.h](px%5F8h%5Fsource.html)"
Go to the source code of this file.
Data Structures | |
---|---|
struct | PGP_S2K |
struct | PGP_Context |
struct | PGP_MPI |
struct | PGP_PubKey |
Macros | |
---|---|
#define | PGP_MAX_KEY (256/8) |
#define | PGP_MAX_BLOCK (256/8) |
#define | PGP_MAX_DIGEST (512/8) |
#define | PGP_S2K_SALT 8 |
#define | s2k_decode_count(cval) (((unsigned) 16 + (cval & 15)) << ((cval >> 4) + 6)) |
Typedefs | |
---|---|
typedef struct PGP_MPI | PGP_MPI |
typedef struct PGP_PubKey | PGP_PubKey |
typedef struct PGP_Context | PGP_Context |
typedef struct PGP_S2K | PGP_S2K |
typedef struct PGP_CFB | PGP_CFB |
◆ PGP_MAX_BLOCK
#define PGP_MAX_BLOCK (256/8)
Definition at line 113 of file pgp.h.
◆ PGP_MAX_DIGEST
#define PGP_MAX_DIGEST (512/8)
Definition at line 114 of file pgp.h.
◆ PGP_MAX_KEY
#define PGP_MAX_KEY (256/8)
Definition at line 112 of file pgp.h.
◆ PGP_S2K_SALT
◆ s2k_decode_count
| #define s2k_decode_count | ( | | cval | ) | (((unsigned) 16 + (cval & 15)) << ((cval >> 4) + 6)) | | -------------------------- | - | | ---- | - | ---------------------------------------------------- |
Definition at line 176 of file pgp.h.
◆ PGP_CFB
◆ PGP_Context
◆ PGP_MPI
◆ PGP_PubKey
◆ PGP_S2K
◆ PGP_COMPR_TYPE
Enumerator |
---|
PGP_COMPR_NONE |
PGP_COMPR_ZIP |
PGP_COMPR_ZLIB |
PGP_COMPR_BZIP2 |
Definition at line 90 of file pgp.h.
◆ PGP_DIGEST_TYPE
Enumerator |
---|
PGP_DIGEST_MD5 |
PGP_DIGEST_SHA1 |
PGP_DIGEST_RIPEMD160 |
PGP_DIGEST_XSHA |
PGP_DIGEST_MD2 |
PGP_DIGEST_TIGER192 |
PGP_DIGEST_HAVAL5_160 |
PGP_DIGEST_SHA256 |
PGP_DIGEST_SHA384 |
PGP_DIGEST_SHA512 |
Definition at line 98 of file pgp.h.
◆ PGP_PKT_TYPE
Enumerator |
---|
PGP_PKT_RESERVED |
PGP_PKT_PUBENCRYPTED_SESSKEY |
PGP_PKT_SIGNATURE |
PGP_PKT_SYMENCRYPTED_SESSKEY |
PGP_PKT_SECRET_KEY |
PGP_PKT_PUBLIC_KEY |
PGP_PKT_SECRET_SUBKEY |
PGP_PKT_COMPRESSED_DATA |
PGP_PKT_SYMENCRYPTED_DATA |
PGP_PKT_MARKER |
PGP_PKT_LITERAL_DATA |
PGP_PKT_TRUST |
PGP_PKT_USER_ID |
PGP_PKT_PUBLIC_SUBKEY |
PGP_PKT_USER_ATTR |
PGP_PKT_SYMENCRYPTED_DATA_MDC |
PGP_PKT_MDC |
PGP_PKT_PRIV_61 |
Definition at line 44 of file pgp.h.
45{
64};
@ PGP_PKT_SYMENCRYPTED_DATA
@ PGP_PKT_COMPRESSED_DATA
@ PGP_PKT_SYMENCRYPTED_SESSKEY
@ PGP_PKT_SYMENCRYPTED_DATA_MDC
@ PGP_PKT_PUBENCRYPTED_SESSKEY
◆ PGP_PUB_ALGO_TYPE
Enumerator |
---|
PGP_PUB_RSA_ENCRYPT_SIGN |
PGP_PUB_RSA_ENCRYPT |
PGP_PUB_RSA_SIGN |
PGP_PUB_ELG_ENCRYPT |
PGP_PUB_DSA_SIGN |
Definition at line 66 of file pgp.h.
67{
73};
@ PGP_PUB_RSA_ENCRYPT_SIGN
◆ PGP_S2K_TYPE
Enumerator |
---|
PGP_S2K_SIMPLE |
PGP_S2K_SALTED |
PGP_S2K_ISALTED |
Definition at line 37 of file pgp.h.
◆ PGP_SYMENC_TYPE
Enumerator |
---|
PGP_SYM_PLAIN |
PGP_SYM_IDEA |
PGP_SYM_DES3 |
PGP_SYM_CAST5 |
PGP_SYM_BLOWFISH |
PGP_SYM_SAFER_SK128 |
PGP_SYM_DES_SK |
PGP_SYM_AES_128 |
PGP_SYM_AES_192 |
PGP_SYM_AES_256 |
PGP_SYM_TWOFISH |
Definition at line 75 of file pgp.h.
◆ _pgp_read_public_key()
Definition at line 158 of file pgp-pubkey.c.
159{
160 int res;
162
164 if (res < 0)
165 return res;
166
167
169 if (pk->ver != 4)
170 {
172 goto out;
173 }
174
175
177 if (res < 0)
178 goto out;
179
180
182
183 switch (pk->algo)
184 {
187 if (res < 0)
188 break;
190 if (res < 0)
191 break;
193 if (res < 0)
194 break;
196 if (res < 0)
197 break;
198
200 break;
201
206 if (res < 0)
207 break;
209 if (res < 0)
210 break;
211
213
216 break;
217
220 if (res < 0)
221 break;
223 if (res < 0)
224 break;
226 if (res < 0)
227 break;
228
230
232 break;
233
234 default:
235 px_debug("unknown public algo: %d", pk->algo);
237 }
238
239out:
240 if (res < 0)
242 else
243 *pk_p = pk;
244
245 return res;
246}
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
static int calc_key_id(PGP_PubKey *pk)
int pgp_key_alloc(PGP_PubKey **pk_p)
void pgp_key_free(PGP_PubKey *pk)
void px_debug(const char *fmt,...)
#define PXE_PGP_UNKNOWN_PUBALGO
#define PXE_PGP_NOT_V4_KEYPKT
struct PGP_PubKey::@0::@3 rsa
struct PGP_PubKey::@0::@2 elg
struct PGP_PubKey::@0::@4 dsa
References PGP_PubKey::algo, calc_key_id(), PGP_PubKey::can_encrypt, PGP_PubKey::dsa, PGP_PubKey::e, PGP_PubKey::elg, PGP_PubKey::g, GETBYTE, PGP_PubKey::n, PGP_PubKey::p, pgp_key_alloc(), pgp_key_free(), pgp_mpi_read(), PGP_PUB_DSA_SIGN, PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, PGP_PUB_RSA_SIGN, PGP_PubKey::pub, pullf_read_fixed(), px_debug(), PXE_PGP_NOT_V4_KEYPKT, PXE_PGP_UNKNOWN_PUBALGO, PGP_PubKey::q, PGP_PubKey::rsa, PGP_PubKey::time, PGP_PubKey::ver, and PGP_PubKey::y.
Referenced by internal_read_key(), process_secret_key(), and read_pubkey_keyid().
◆ pgp_armor_decode()
Definition at line 314 of file pgp-armor.c.
315{
316 const uint8 *p = src;
317 const uint8 *data_end = src + len;
318 long crc;
319 const uint8 *base64_start,
320 *armor_end;
321 const uint8 *base64_end = NULL;
323 int hlen;
324 int blen;
326
327
328 hlen = find_header(src, data_end, &p, 0);
329 if (hlen <= 0)
330 goto out;
331 p += hlen;
332
333
334 hlen = find_header(p, data_end, &armor_end, 1);
335 if (hlen <= 0)
336 goto out;
337
338
339 while (p < armor_end && *p != '\n' && *p != '\r')
340 {
341 p = memchr(p, '\n', armor_end - p);
342 if (!p)
343 goto out;
344
345
346 p++;
347 }
348 base64_start = p;
349
350
351 for (p = armor_end; p >= base64_start; p--)
352 if (*p == '=')
353 {
354 base64_end = p - 1;
355 break;
356 }
357 if (base64_end == NULL)
358 goto out;
359
360
362 goto out;
363 crc = (((long) buf[0]) << 16) + (((long) buf[1]) << 8) + (long) buf[2];
364
365
369 if (res > blen)
370 elog(FATAL, "overflow - decode estimate too small");
371 if (res >= 0)
372 {
374 dst->len += res;
375 else
377 }
378out:
379 return res;
380}
static long crc24(const uint8 *data, unsigned len)
static int find_header(const uint8 *data, const uint8 *datend, const uint8 **start_p, int is_end)
static int pg_base64_decode(const uint8 *src, unsigned len, uint8 *dst)
static unsigned pg_base64_dec_len(unsigned srclen)
#define PXE_PGP_CORRUPT_ARMOR
void enlargeStringInfo(StringInfo str, int needed)
References buf, crc, crc24(), StringInfoData::data, elog, enlargeStringInfo(), FATAL, find_header(), StringInfoData::len, len, pg_base64_dec_len(), pg_base64_decode(), and PXE_PGP_CORRUPT_ARMOR.
Referenced by pg_dearmor().
◆ pgp_armor_encode()
void pgp_armor_encode | ( | const uint8 * | src, |
---|---|---|---|
unsigned | len, | ||
StringInfo | dst, | ||
int | num_headers, | ||
char ** | keys, | ||
char ** | values | ||
) |
Definition at line 207 of file pgp-armor.c.
209{
210 int n;
211 int res;
212 unsigned b64len;
214
216
217 for (n = 0; n < num_headers; n++)
220
221
224
226 if (res > b64len)
227 elog(FATAL, "overflow - encode estimate too small");
228 dst->len += res;
229
230 if (*(dst->data + dst->len - 1) != '\n')
232
238
240}
static Datum values[MAXATTR]
static const char *const armor_footer
static unsigned pg_base64_enc_len(unsigned srclen)
static const char *const armor_header
static int pg_base64_encode(const uint8 *src, unsigned len, uint8 *dst)
static const unsigned char _base64[]
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
References _base64, appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), armor_footer, armor_header, crc, crc24(), StringInfoData::data, elog, enlargeStringInfo(), FATAL, StringInfoData::len, len, pg_base64_enc_len(), pg_base64_encode(), and values.
Referenced by pg_armor().
◆ pgp_cfb_create()
int pgp_cfb_create | ( | PGP_CFB ** | ctx_p, |
---|---|---|---|
int | algo, | ||
const uint8 * | key, | ||
int | key_len, | ||
int | resync, | ||
uint8 * | iv | ||
) |
Definition at line 52 of file pgp-cfb.c.
54{
55 int res;
58
60 if (res < 0)
61 return res;
62
64 if (res < 0)
65 {
67 return res;
68 }
69
70 ctx = palloc0(sizeof(*ctx));
71 ctx->ciph = ciph;
74
75 if (iv)
77
78 *ctx_p = ctx;
79 return 0;
80}
void * palloc0(Size size)
int pgp_load_cipher(int code, PX_Cipher **res)
#define px_cipher_free(c)
#define px_cipher_block_size(c)
#define px_cipher_init(c, k, klen, iv)
References PGP_CFB::block_size, PGP_CFB::ciph, PGP_CFB::fr, sort-test::key, palloc0(), pgp_load_cipher(), px_cipher_block_size, px_cipher_free, px_cipher_init, and PGP_CFB::resync.
Referenced by decrypt_key(), encrypt_init(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().
◆ pgp_cfb_decrypt()
Definition at line 260 of file pgp-cfb.c.
261{
263
265}
static int mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
int(* mix_data_t)(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
References cfb_process(), data, len, mix, mix_decrypt_normal(), mix_decrypt_resync(), and PGP_CFB::resync.
Referenced by decrypt_key(), and decrypt_read().
◆ pgp_cfb_encrypt()
Definition at line 252 of file pgp-cfb.c.
253{
255
257}
static int mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
References cfb_process(), data, len, mix, mix_encrypt_normal(), mix_encrypt_resync(), and PGP_CFB::resync.
Referenced by encrypt_process(), and symencrypt_sesskey().
◆ pgp_cfb_free()
Definition at line 83 of file pgp-cfb.c.
84{
88}
void pfree(void *pointer)
void px_memset(void *ptr, int c, size_t len)
References PGP_CFB::ciph, pfree(), px_cipher_free, and px_memset().
Referenced by decrypt_key(), encrypt_free(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().
◆ pgp_compress_filter()
◆ pgp_create_pkt_reader()
Definition at line 223 of file pgp-decrypt.c.
225{
226 int res;
228
229 pkt->type = pkttype;
232 if (res < 0)
234 return res;
235}
int pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)
static struct PullFilterOps pktreader_filter
References PktData::len, len, palloc(), pfree(), pktreader_filter, pullf_create(), and PktData::type.
Referenced by internal_read_key(), pgp_decrypt(), pgp_get_keyid(), and process_data_packets().
◆ pgp_create_pkt_writer()
◆ pgp_decompress_filter()
◆ pgp_decrypt()
Definition at line 1093 of file pgp-decrypt.c.
1094{
1095 int res;
1099 int len;
1100 int got_key = 0;
1101 int got_data = 0;
1102
1104
1105 while (res >= 0)
1106 {
1108 if (res <= 0)
1109 break;
1110
1112 if (res < 0)
1113 break;
1114
1116 switch (tag)
1117 {
1120 break;
1122
1124 got_key = 1;
1125 break;
1127 if (got_key)
1128
1129
1130
1131
1132
1133
1134 px_debug("pgp_decrypt: using first of several keys");
1135 else
1136 {
1137 got_key = 1;
1139 }
1140 break;
1142 if (!got_key)
1143 px_debug("pgp_decrypt: have data but no key");
1144 else if (got_data)
1145 px_debug("pgp_decrypt: got second data packet");
1146 else
1147 {
1148 got_data = 1;
1151 }
1152 break;
1154 if (!got_key)
1155 px_debug("pgp_decrypt: have data but no key");
1156 else if (got_data)
1157 px_debug("pgp_decrypt: several data pkts not supported");
1158 else
1159 {
1160 got_data = 1;
1163 }
1164 break;
1165 default:
1166 px_debug("pgp_decrypt: unknown tag: 0x%02x", tag);
1167 }
1169 pkt = NULL;
1170 }
1171
1172 if (pkt)
1174
1175 if (src)
1177
1178 if (res < 0)
1179 return res;
1180
1181
1182
1183
1184
1185
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1211
1212 return res;
1213}
int pullf_create_mbuf_reader(PullFilter **mp_p, MBuf *src)
void pullf_free(PullFilter *pf)
int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p, int allow_ctx)
static int parse_symenc_mdc_data(PGP_Context *ctx, PullFilter *pkt, MBuf *dst)
static int parse_symenc_sesskey(PGP_Context *ctx, PullFilter *src)
int pgp_skip_packet(PullFilter *pkt)
static int parse_symenc_data(PGP_Context *ctx, PullFilter *pkt, MBuf *dst)
int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len, int pkttype, PGP_Context *ctx)
int pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
#define PXE_PGP_CORRUPT_DATA
References PGP_Context::corrupt_prefix, PGP_Context::disable_mdc, len, NO_CTX_SIZE, parse_symenc_data(), parse_symenc_mdc_data(), parse_symenc_sesskey(), pgp_create_pkt_reader(), pgp_parse_pkt_hdr(), pgp_parse_pubenc_sesskey(), PGP_PKT_MARKER, PGP_PKT_PUBENCRYPTED_SESSKEY, PGP_PKT_SYMENCRYPTED_DATA, PGP_PKT_SYMENCRYPTED_DATA_MDC, PGP_PKT_SYMENCRYPTED_SESSKEY, pgp_skip_packet(), pullf_create_mbuf_reader(), pullf_free(), px_debug(), PXE_PGP_CORRUPT_DATA, PXE_PGP_NOT_TEXT, PXE_PGP_UNSUPPORTED_COMPR, PGP_Context::unexpected_binary, and PGP_Context::unsupported_compr.
Referenced by decrypt_internal().
◆ pgp_disable_mdc()
◆ pgp_elgamal_decrypt()
Definition at line 164 of file pgp-mpi-openssl.c.
166{
172 BIGNUM *c1x = BN_new();
173 BIGNUM *div = BN_new();
174 BIGNUM *m = BN_new();
175 BN_CTX *tmp = BN_CTX_new();
176
177 if (!c1 || !c2 || !p || || !c1x || !div || !m || !tmp)
178 goto err;
179
180
181
182
183 if (!BN_mod_exp(c1x, c1, x, p, tmp))
184 goto err;
185 if (!BN_mod_inverse(div, c1x, p, tmp))
186 goto err;
187 if (!BN_mod_mul(m, c2, div, p, tmp))
188 goto err;
189
190
192 if (*msg_p)
193 res = 0;
195 if (tmp)
196 BN_CTX_free(tmp);
197 if (m)
198 BN_clear_free(m);
199 if (div)
200 BN_clear_free(div);
201 if (c1x)
202 BN_clear_free(c1x);
203 if (x)
204 BN_clear_free(x);
205 if (p)
206 BN_clear_free(p);
207 if (c2)
208 BN_clear_free(c2);
209 if (c1)
210 BN_clear_free(c1);
211 return res;
212}
void err(int eval, const char *fmt,...)
static BIGNUM * mpi_to_bn(PGP_MPI *n)
static PGP_MPI * bn_to_mpi(BIGNUM *bn)
#define PXE_PGP_MATH_FAILED
References bn_to_mpi(), PGP_PubKey::elg, err(), mpi_to_bn(), PGP_PubKey::p, PGP_PubKey::pub, PXE_PGP_MATH_FAILED, PGP_PubKey::sec, x, and PGP_PubKey::x.
Referenced by decrypt_elgamal().
◆ pgp_elgamal_encrypt()
Definition at line 101 of file pgp-mpi-openssl.c.
103{
105 int k_bits;
110 BIGNUM *k = BN_new();
111 BIGNUM *yk = BN_new();
112 BIGNUM *c1 = BN_new();
113 BIGNUM *c2 = BN_new();
114 BN_CTX *tmp = BN_CTX_new();
115
116 if (!m || !p || !g || || !k || !yk || !c1 || !c2 || !tmp)
117 goto err;
118
119
120
121
123 if (!BN_rand(k, k_bits, 0, 0))
124 goto err;
125
126
127
128
129 if (!BN_mod_exp(c1, g, k, p, tmp))
130 goto err;
131 if (!BN_mod_exp(yk, y, k, p, tmp))
132 goto err;
133 if (!BN_mod_mul(c2, m, yk, p, tmp))
134 goto err;
135
136
139 if (*c1_p && *c2_p)
140 res = 0;
142 if (tmp)
143 BN_CTX_free(tmp);
144 if (c2)
145 BN_clear_free(c2);
146 if (c1)
147 BN_clear_free(c1);
148 if (yk)
149 BN_clear_free(yk);
150 if (k)
151 BN_clear_free(k);
152 if (y)
153 BN_clear_free(y);
154 if (g)
155 BN_clear_free(g);
156 if (p)
157 BN_clear_free(p);
158 if (m)
159 BN_clear_free(m);
160 return res;
161}
static int decide_k_bits(int p_bits)
References bn_to_mpi(), decide_k_bits(), PGP_PubKey::elg, err(), PGP_PubKey::g, mpi_to_bn(), PGP_PubKey::p, PGP_PubKey::pub, PXE_PGP_MATH_FAILED, y, and PGP_PubKey::y.
Referenced by encrypt_and_write_elgamal().
◆ pgp_encrypt()
Definition at line 599 of file pgp-encrypt.c.
600{
601 int res;
605 *pf_tmp;
606
607
608
609
612
613
615 if (res < 0)
616 goto out;
617
618
619
620
622 {
624 if (res < 0)
625 goto out;
626 }
627
629 if (res < 0)
630 goto out;
631
632
633
634
637 else
639 if (res < 0)
640 goto out;
641
642
644 if (res < 0)
645 goto out;
646 pf = pf_tmp;
647
648
650 if (res < 0)
651 goto out;
652 pf = pf_tmp;
653
654
656 {
658 if (res < 0)
659 goto out;
660 pf = pf_tmp;
661 }
662
663
665 if (res < 0)
666 goto out;
667
668
670 {
672 if (res < 0)
673 goto out;
674 pf = pf_tmp;
675 }
676
677
679 if (res < 0)
680 goto out;
681 pf = pf_tmp;
682
683
684
686 {
688 if (res < 0)
689 goto out;
690 pf = pf_tmp;
691 }
692
693
694
695
696
699 if (res >= 0)
701out:
703 return res;
704}
int pushf_write(PushFilter *mp, const uint8 *data, int len)
int mbuf_avail(MBuf *mbuf)
int pushf_create_mbuf_writer(PushFilter **res, MBuf *dst)
void pushf_free_all(PushFilter *mp)
int pushf_flush(PushFilter *mp)
int mbuf_grab(MBuf *mbuf, int len, uint8 **data_p)
static int write_symenc_sesskey(PGP_Context *ctx, PushFilter *dst)
static int init_litdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
static int write_prefix(PGP_Context *ctx, PushFilter *dst)
static const PushFilterOps crlf_filter
static int init_compress(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
static int init_encdata_packet(PushFilter **pf_res, PGP_Context *ctx, PushFilter *dst)
static const PushFilterOps encrypt_filter
static int init_s2k_key(PGP_Context *ctx)
static int init_sess_key(PGP_Context *ctx)
static const PushFilterOps mdc_filter
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
#define PXE_ARGUMENT_ERROR
References buf, PGP_Context::compress_algo, PGP_Context::compress_level, PGP_Context::convert_crlf, crlf_filter, PGP_Context::disable_mdc, encrypt_filter, init_compress(), init_encdata_packet(), init_litdata_packet(), init_s2k_key(), init_sess_key(), len, mbuf_avail(), mbuf_grab(), mdc_filter, pgp_write_pubenc_sesskey(), PGP_Context::pub_key, pushf_create(), pushf_create_mbuf_writer(), pushf_flush(), pushf_free_all(), pushf_write(), PXE_ARGUMENT_ERROR, PGP_Context::sym_key, PGP_Context::text_mode, write_prefix(), and write_symenc_sesskey().
Referenced by encrypt_internal().
◆ pgp_expect_packet_end()
◆ pgp_extract_armor_headers()
int pgp_extract_armor_headers | ( | const uint8 * | src, |
---|---|---|---|
unsigned | len, | ||
int * | nheaders, | ||
char *** | keys, | ||
char *** | values | ||
) |
Definition at line 390 of file pgp-armor.c.
392{
393 const uint8 *data_end = src + len;
395 const uint8 *base64_start;
396 const uint8 *armor_start;
397 const uint8 *armor_end;
398 Size armor_len;
399 char *line;
400 char *nextline;
401 char *eol,
403 int hlen;
404 char *buf;
405 int hdrlines;
406 int n;
407
408
409 hlen = find_header(src, data_end, &armor_start, 0);
410 if (hlen <= 0)
412 armor_start += hlen;
413
414
415 hlen = find_header(armor_start, data_end, &armor_end, 1);
416 if (hlen <= 0)
418
419
420 hdrlines = 0;
421 p = armor_start;
422 while (p < armor_end && *p != '\n' && *p != '\r')
423 {
424 p = memchr(p, '\n', armor_end - p);
425 if (!p)
427
428
429 p++;
430 hdrlines++;
431 }
432 base64_start = p;
433
434
435
436
437
438 armor_len = base64_start - armor_start;
440 memcpy(buf, armor_start, armor_len);
441 buf[armor_len] = '\0';
442
443
444 *keys = (char **) palloc(hdrlines * sizeof(char *));
445 *values = (char **) palloc(hdrlines * sizeof(char *));
446
447
448
449
450
451 n = 0;
452 line = buf;
453 for (;;)
454 {
455
456 eol = strchr(line, '\n');
457 if (!eol)
458 break;
459 nextline = eol + 1;
460
461 if (eol > line && *(eol - 1) == '\r')
462 eol--;
463 *eol = '\0';
464
465
466 colon = strstr(line, ": ");
470
471
472 if (n >= hdrlines)
473 elog(ERROR, "unexpected number of armor header lines");
474
475 (*keys)[n] = line;
476 (*values)[n] = colon + 2;
477 n++;
478
479
480 line = nextline;
481 }
482
483 if (n != hdrlines)
484 elog(ERROR, "unexpected number of armor header lines");
485
486 *nheaders = n;
487 return 0;
488}
References buf, colon, elog, ERROR, find_header(), len, palloc(), PXE_PGP_CORRUPT_ARMOR, and values.
Referenced by pgp_armor_headers().
◆ pgp_free()
◆ pgp_get_cipher_block_size()
int pgp_get_cipher_block_size | ( | int | code | ) |
---|
◆ pgp_get_cipher_code()
int pgp_get_cipher_code | ( | const char * | name | ) |
---|
◆ pgp_get_cipher_key_size()
int pgp_get_cipher_key_size | ( | int | code | ) |
---|
◆ pgp_get_digest_code()
int pgp_get_digest_code | ( | const char * | name | ) |
---|
◆ pgp_get_digest_name()
const char * pgp_get_digest_name | ( | int | code | ) |
---|
◆ pgp_get_keyid()
int pgp_get_keyid | ( | MBuf * | pgp_data, |
---|---|---|---|
char * | dst | ||
) |
Definition at line 112 of file pgp-info.c.
113{
114 int res;
119 int got_pub_key = 0,
120 got_symenc_key = 0,
121 got_pubenc_key = 0;
122 int got_data = 0;
123 uint8 keyid_buf[8];
124 int got_main_key = 0;
125
126
128 if (res < 0)
129 return res;
130
131 while (1)
132 {
134 if (res <= 0)
135 break;
137 if (res < 0)
138 break;
139
140 switch (tag)
141 {
144
145 if (!got_main_key)
146 {
147 got_main_key = 1;
149 }
150 else
152 break;
156 if (res < 0)
157 break;
158 if (res > 0)
159 got_pub_key++;
160 break;
162 got_pubenc_key++;
164 break;
167
168 got_data = 1;
169 break;
171 got_symenc_key++;
172
180 break;
181 default:
183 }
184
185 if (pkt)
187 pkt = NULL;
188
189 if (res < 0 || got_data)
190 break;
191 }
192
194 if (pkt)
196
197 if (res < 0)
198 return res;
199
200
201 if (got_pub_key && got_pubenc_key)
203
204 if (got_pub_key > 1)
206
207 if (got_pubenc_key > 1)
209
210
211
212
213 if (res >= 0)
214 {
215 if (got_pubenc_key || got_pub_key)
216 {
217 if (memcmp(keyid_buf, any_key, 8) == 0)
218 {
219 memcpy(dst, "ANYKEY", 7);
220 res = 6;
221 }
222 else
224 }
225 else if (got_symenc_key)
226 {
227 memcpy(dst, "SYMKEY", 7);
228 res = 6;
229 }
230 else
232 }
233
234 return res;
235}
static const uint8 any_key[]
static int read_pubkey_keyid(PullFilter *pkt, uint8 *keyid_buf)
static int print_key(uint8 *keyid, char *dst)
static int read_pubenc_keyid(PullFilter *pkt, uint8 *keyid_buf)
#define PXE_PGP_MULTIPLE_KEYS
#define PXE_PGP_NO_USABLE_KEY
References any_key, len, pgp_create_pkt_reader(), pgp_parse_pkt_hdr(), PGP_PKT_MARKER, PGP_PKT_PRIV_61, PGP_PKT_PUBENCRYPTED_SESSKEY, PGP_PKT_PUBLIC_KEY, PGP_PKT_PUBLIC_SUBKEY, PGP_PKT_SECRET_KEY, PGP_PKT_SECRET_SUBKEY, PGP_PKT_SIGNATURE, PGP_PKT_SYMENCRYPTED_DATA, PGP_PKT_SYMENCRYPTED_DATA_MDC, PGP_PKT_SYMENCRYPTED_SESSKEY, PGP_PKT_TRUST, PGP_PKT_USER_ATTR, PGP_PKT_USER_ID, pgp_skip_packet(), print_key(), pullf_create_mbuf_reader(), pullf_free(), PXE_PGP_CORRUPT_DATA, PXE_PGP_MULTIPLE_KEYS, PXE_PGP_NO_USABLE_KEY, read_pubenc_keyid(), and read_pubkey_keyid().
Referenced by pgp_key_id_w().
◆ pgp_get_unicode_mode()
◆ pgp_init()
Definition at line 189 of file pgp.c.
190{
192
193 ctx = palloc0(sizeof *ctx);
194
207
208 *ctx_p = ctx;
209 return 0;
210}
static int def_compress_algo
static int def_s2k_cipher_algo
static int def_convert_crlf
static int def_compress_level
static int def_s2k_digest_algo
static int def_unicode_mode
static int def_cipher_algo
static int def_disable_mdc
static int def_use_sess_key
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().
◆ pgp_key_alloc()
◆ pgp_key_free()
Definition at line 48 of file pgp-pubkey.c.
49{
50 if (pk == NULL)
51 return;
52
53 switch (pk->algo)
54 {
60 break;
70 break;
77 break;
78 }
81}
int pgp_mpi_free(PGP_MPI *mpi)
References PGP_PubKey::algo, PGP_PubKey::d, PGP_PubKey::dsa, PGP_PubKey::e, PGP_PubKey::elg, PGP_PubKey::g, PGP_PubKey::n, PGP_PubKey::p, pfree(), pgp_mpi_free(), PGP_PUB_DSA_SIGN, PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, PGP_PUB_RSA_SIGN, PGP_PubKey::pub, px_memset(), PGP_PubKey::q, PGP_PubKey::rsa, PGP_PubKey::sec, PGP_PubKey::u, PGP_PubKey::x, and PGP_PubKey::y.
Referenced by _pgp_read_public_key(), internal_read_key(), pgp_free(), process_secret_key(), and read_pubkey_keyid().
◆ pgp_load_cipher()
int pgp_load_cipher | ( | int | code, |
---|---|---|---|
PX_Cipher ** | res | ||
) |
◆ pgp_load_digest()
int pgp_load_digest | ( | int | code, |
---|---|---|---|
PX_MD ** | res | ||
) |
Definition at line 173 of file pgp.c.
174{
177
178 if (name == NULL)
180
182 if (err == 0)
183 return 0;
184
186}
int px_find_digest(const char *name, PX_MD **res)
const char * pgp_get_digest_name(int code)
References cipher_info::code, err(), 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().
◆ pgp_mpi_alloc()
int pgp_mpi_alloc | ( | int | bits, |
---|---|---|---|
PGP_MPI ** | mpi | ||
) |
◆ pgp_mpi_cksum()
unsigned pgp_mpi_cksum | ( | unsigned | cksum, |
---|---|---|---|
PGP_MPI * | n | ||
) |
◆ pgp_mpi_create()
int pgp_mpi_create | ( | uint8 * | data, |
---|---|---|---|
int | bits, | ||
PGP_MPI ** | mpi | ||
) |
◆ pgp_mpi_free()
◆ pgp_mpi_hash()
◆ pgp_mpi_read()
◆ pgp_mpi_write()
◆ pgp_parse_pkt_hdr()
int pgp_parse_pkt_hdr | ( | PullFilter * | src, |
---|---|---|---|
uint8 * | tag, | ||
int * | len_p, | ||
int | allow_ctx | ||
) |
Definition at line 129 of file pgp-decrypt.c.
130{
131 int lentype;
132 int res;
134
135
137 if (res < 0)
138 return res;
139 if (res == 0)
140 return 0;
141
142 if ((*p & 0x80) == 0)
143 {
144 px_debug("pgp_parse_pkt_hdr: not pkt hdr");
146 }
147
148 if (*p & 0x40)
149 {
150 *tag = *p & 0x3f;
152 }
153 else
154 {
155 lentype = *p & 3;
156 *tag = (*p >> 2) & 0x0F;
157 if (lentype == 3)
159 else
161 }
162 return res;
163}
static int parse_old_len(PullFilter *src, int *len_p, int lentype)
static int parse_new_len(PullFilter *src, int *len_p)
References parse_new_len(), parse_old_len(), PKT_CONTEXT, pullf_read(), px_debug(), and PXE_PGP_CORRUPT_DATA.
Referenced by internal_read_key(), pgp_decrypt(), pgp_get_keyid(), and process_data_packets().
◆ pgp_parse_pubenc_sesskey()
Definition at line 150 of file pgp-pubdec.c.
151{
152 int ver;
153 int algo;
154 int res;
158 int msglen;
160
162 if (pk == NULL)
163 {
166 }
167
169 if (ver != 3)
170 {
171 px_debug("unknown pubenc_sesskey pkt ver=%d", ver);
173 }
174
175
176
177
179 if (res < 0)
180 return res;
181 if (memcmp(key_id, any_key, 8) != 0
182 && memcmp(key_id, pk->key_id, 8) != 0)
183 {
184 px_debug("key_id's does not match");
186 }
187
188
189
190
192 switch (algo)
193 {
196 break;
200 break;
201 default:
203 }
204 if (res < 0)
205 return res;
206
207
208
209
211 if (msg == NULL)
212 {
213 px_debug("check_eme_pkcs1_v15 failed");
215 goto out;
216 }
217 msglen = m->bytes - (msg - m->data);
218
220 if (res < 0)
221 goto out;
222
223
224
225
229
230out:
232 if (res < 0)
233 return res;
235}
int pgp_expect_packet_end(PullFilter *pkt)
static int decrypt_rsa(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
static const uint8 any_key[]
static int decrypt_elgamal(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
static uint8 * check_eme_pkcs1_v15(uint8 *data, int len)
static int control_cksum(uint8 *msg, int msglen)
#define PXE_PGP_WRONG_KEY
uint8 sess_key[PGP_MAX_KEY]
References any_key, PGP_MPI::bytes, check_eme_pkcs1_v15(), PGP_Context::cipher_algo, control_cksum(), PGP_MPI::data, decrypt_elgamal(), decrypt_rsa(), GETBYTE, PGP_PubKey::key_id, pgp_expect_packet_end(), pgp_mpi_free(), PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, PGP_Context::pub_key, pullf_read_fixed(), px_debug(), PXE_BUG, PXE_PGP_CORRUPT_DATA, PXE_PGP_UNKNOWN_PUBALGO, PXE_PGP_WRONG_KEY, PGP_Context::sess_key, and PGP_Context::sess_key_len.
Referenced by pgp_decrypt().
◆ pgp_rsa_decrypt()
Definition at line 251 of file pgp-mpi-openssl.c.
252{
257 BIGNUM *m = BN_new();
258 BN_CTX *tmp = BN_CTX_new();
259
260 if (!m || !d || !n || || !tmp)
261 goto err;
262
263
264
265
266 if (!BN_mod_exp(m, c, d, n, tmp))
267 goto err;
268
270 if (*m_p)
271 res = 0;
273 if (tmp)
274 BN_CTX_free(tmp);
275 if (m)
276 BN_clear_free(m);
277 if (n)
278 BN_clear_free(n);
279 if (d)
280 BN_clear_free(d);
281 if (c)
282 BN_clear_free(c);
283 return res;
284}
References bn_to_mpi(), PGP_PubKey::d, err(), mpi_to_bn(), PGP_PubKey::n, PGP_PubKey::pub, PXE_PGP_MATH_FAILED, PGP_PubKey::rsa, and PGP_PubKey::sec.
Referenced by decrypt_rsa().
◆ pgp_rsa_encrypt()
Definition at line 215 of file pgp-mpi-openssl.c.
216{
221 BIGNUM *c = BN_new();
222 BN_CTX *tmp = BN_CTX_new();
223
224 if (!m || || !n ||
|| !tmp)
225 goto err;
226
227
228
229
230 if (!BN_mod_exp(c, m, e, n, tmp))
231 goto err;
232
234 if (*c_p)
235 res = 0;
237 if (tmp)
238 BN_CTX_free(tmp);
239 if (c)
240 BN_clear_free(c);
241 if (n)
242 BN_clear_free(n);
243 if (e)
244 BN_clear_free(e);
245 if (m)
246 BN_clear_free(m);
247 return res;
248}
References bn_to_mpi(), PGP_PubKey::e, err(), mpi_to_bn(), PGP_PubKey::n, PGP_PubKey::pub, PXE_PGP_MATH_FAILED, and PGP_PubKey::rsa.
Referenced by encrypt_and_write_rsa().
◆ pgp_s2k_fill()
int pgp_s2k_fill | ( | PGP_S2K * | s2k, |
---|---|---|---|
int | mode, | ||
int | digest_algo, | ||
int | count | ||
) |
Definition at line 223 of file pgp-s2k.c.
224{
225 int res = 0;
227
230
231 switch (s2k->mode)
232 {
234 break;
238 break;
245 break;
246 default:
248 }
249 return res;
250}
static PgChecksumMode mode
static uint8 decide_s2k_iter(unsigned rand_byte, int count)
bool pg_strong_random(void *buf, size_t len)
#define PXE_PGP_BAD_S2K_MODE
References decide_s2k_iter(), PGP_S2K::digest_algo, PGP_S2K::iter, PGP_S2K::mode, mode, pg_strong_random(), PGP_S2K_ISALTED, PGP_S2K_SALT, PGP_S2K_SALTED, PGP_S2K_SIMPLE, PXE_NO_RANDOM, PXE_PGP_BAD_S2K_MODE, and PGP_S2K::salt.
Referenced by init_s2k_key().
◆ pgp_s2k_process()
int pgp_s2k_process | ( | PGP_S2K * | s2k, |
---|---|---|---|
int | cipher, | ||
const uint8 * | key, | ||
int | key_len | ||
) |
Definition at line 279 of file pgp-s2k.c.
280{
281 int res;
283
287
289 if (res < 0)
290 return res;
291
292 switch (s2k->mode)
293 {
294 case 0:
296 break;
297 case 1:
299 break;
300 case 3:
302 break;
303 default:
305 }
307 return res;
308}
static int calc_s2k_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
static int calc_s2k_simple(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
static int calc_s2k_iter_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
int pgp_get_cipher_key_size(int code)
int pgp_load_digest(int code, PX_MD **res)
References calc_s2k_iter_salted(), calc_s2k_salted(), calc_s2k_simple(), PGP_S2K::digest_algo, sort-test::key, PGP_S2K::key_len, PGP_S2K::mode, pgp_get_cipher_key_size(), pgp_load_digest(), px_md_free, PXE_PGP_BAD_S2K_MODE, and PXE_PGP_UNSUPPORTED_CIPHER.
Referenced by init_s2k_key(), parse_symenc_sesskey(), and process_secret_key().
◆ pgp_s2k_read()
◆ pgp_set_cipher_algo()
int pgp_set_cipher_algo | ( | PGP_Context * | ctx, |
---|---|---|---|
const char * | name | ||
) |
◆ pgp_set_compress_algo()
int pgp_set_compress_algo | ( | PGP_Context * | ctx, |
---|---|---|---|
int | algo | ||
) |
◆ pgp_set_compress_level()
int pgp_set_compress_level | ( | PGP_Context * | ctx, |
---|---|---|---|
int | level | ||
) |
◆ pgp_set_convert_crlf()
int pgp_set_convert_crlf | ( | PGP_Context * | ctx, |
---|---|---|---|
int | doit | ||
) |
◆ pgp_set_pubkey()
int pgp_set_pubkey | ( | PGP_Context * | ctx, |
---|---|---|---|
MBuf * | keypkt, | ||
const uint8 * | key, | ||
int | key_len, | ||
int | pubtype | ||
) |
◆ pgp_set_s2k_cipher_algo()
int pgp_set_s2k_cipher_algo | ( | PGP_Context * | ctx, |
---|---|---|---|
const char * | name | ||
) |
◆ pgp_set_s2k_count()
◆ pgp_set_s2k_digest_algo()
int pgp_set_s2k_digest_algo | ( | PGP_Context * | ctx, |
---|---|---|---|
const char * | name | ||
) |
◆ pgp_set_s2k_mode()
◆ pgp_set_sess_key()
◆ pgp_set_symkey()
◆ pgp_set_text_mode()
◆ pgp_set_unicode_mode()
int pgp_set_unicode_mode | ( | PGP_Context * | ctx, |
---|---|---|---|
int | mode | ||
) |
◆ pgp_skip_packet()
◆ pgp_write_pubenc_sesskey()
Definition at line 190 of file pgp-pubenc.c.
191{
192 int res;
197
198 if (pk == NULL)
199 {
202 }
203
204 algo = pk->algo;
205
206
207
208
210 if (res < 0)
211 goto err;
213 if (res < 0)
214 goto err;
216 if (res < 0)
217 goto err;
219 if (res < 0)
220 goto err;
221
222 switch (algo)
223 {
226 break;
230 break;
231 }
232 if (res < 0)
233 goto err;
234
235
236
237
240 if (pkt)
242
243 return res;
244}
void pushf_free(PushFilter *mp)
int pgp_create_pkt_writer(PushFilter *dst, int tag, PushFilter **res_p)
static int encrypt_and_write_elgamal(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
static int encrypt_and_write_rsa(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
References PGP_PubKey::algo, encrypt_and_write_elgamal(), encrypt_and_write_rsa(), err(), PGP_PubKey::key_id, pgp_create_pkt_writer(), PGP_PKT_PUBENCRYPTED_SESSKEY, PGP_PUB_ELG_ENCRYPT, PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, PGP_Context::pub_key, pushf_flush(), pushf_free(), pushf_write(), px_debug(), and PXE_BUG.
Referenced by pgp_encrypt().