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

Go to the source code of this file.

Functions

static uint8check_eme_pkcs1_v15 (uint8 *data, int len)
 
static int control_cksum (uint8 *msg, int msglen)
 
static int decrypt_elgamal (PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
 
static int decrypt_rsa (PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
 
int pgp_parse_pubenc_sesskey (PGP_Context *ctx, PullFilter *pkt)
 

Variables

static const uint8 any_key [] = {0, 0, 0, 0, 0, 0, 0, 0}
 

Function Documentation

◆ check_eme_pkcs1_v15()

static uint8 * check_eme_pkcs1_v15 ( uint8 data,
int  len 
)
static

Definition at line 42 of file pgp-pubdec.c.

43{
44 uint8 *data_end = data + len;
45 uint8 *p = data;
46 int rnd = 0;
47
48 if (len < 1 + 8 + 1)
49 return NULL;
50
51 if (*p++ != 2)
52 return NULL;
53
54 while (p < data_end && *p)
55 {
56 p++;
57 rnd++;
58 }
59
60 if (p == data_end)
61 return NULL;
62 if (*p != 0)
63 return NULL;
64 if (rnd < 8)
65 return NULL;
66 return p + 1;
67}
uint8_t uint8
Definition: c.h:500
const void size_t len
const void * data

References data, and len.

Referenced by pgp_parse_pubenc_sesskey().

◆ control_cksum()

static int control_cksum ( uint8 msg,
int  msglen 
)
static

Definition at line 74 of file pgp-pubdec.c.

75{
76 int i;
77 unsigned my_cksum,
78 got_cksum;
79
80 if (msglen < 3)
81 return PXE_PGP_WRONG_KEY;
82
83 my_cksum = 0;
84 for (i = 1; i < msglen - 2; i++)
85 my_cksum += msg[i];
86 my_cksum &= 0xFFFF;
87 got_cksum = ((unsigned) (msg[msglen - 2]) << 8) + msg[msglen - 1];
88 if (my_cksum != got_cksum)
89 {
90 px_debug("pubenc cksum failed");
91 return PXE_PGP_WRONG_KEY;
92 }
93 return 0;
94}
int i
Definition: isn.c:72
void px_debug(const char *fmt,...)
Definition: px.c:149
#define PXE_PGP_WRONG_KEY
Definition: px.h:80

References i, px_debug(), and PXE_PGP_WRONG_KEY.

Referenced by pgp_parse_pubenc_sesskey().

◆ decrypt_elgamal()

static int decrypt_elgamal ( PGP_PubKey pk,
PullFilter pkt,
PGP_MPI **  m_p 
)
static

Definition at line 97 of file pgp-pubdec.c.

98{
99 int res;
100 PGP_MPI *c1 = NULL;
101 PGP_MPI *c2 = NULL;
102
103 if (pk->algo != PGP_PUB_ELG_ENCRYPT)
104 return PXE_PGP_WRONG_KEY;
105
106 /* read elgamal encrypted data */
107 res = pgp_mpi_read(pkt, &c1);
108 if (res < 0)
109 goto out;
110 res = pgp_mpi_read(pkt, &c2);
111 if (res < 0)
112 goto out;
113
114 /* decrypt */
115 res = pgp_elgamal_decrypt(pk, c1, c2, m_p);
116
117out:
118 pgp_mpi_free(c1);
119 pgp_mpi_free(c2);
120 return res;
121}
int pgp_elgamal_decrypt(PGP_PubKey *pk, PGP_MPI *_c1, PGP_MPI *_c2, PGP_MPI **msg_p)
int pgp_mpi_free(PGP_MPI *mpi)
Definition: pgp-mpi.c:70
int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
Definition: pgp-mpi.c:80
@ PGP_PUB_ELG_ENCRYPT
Definition: pgp.h:71
Definition: pgp.h:180
uint8 algo
Definition: pgp.h:190

References PGP_PubKey::algo, pgp_elgamal_decrypt(), pgp_mpi_free(), pgp_mpi_read(), PGP_PUB_ELG_ENCRYPT, and PXE_PGP_WRONG_KEY.

Referenced by pgp_parse_pubenc_sesskey().

◆ decrypt_rsa()

static int decrypt_rsa ( PGP_PubKey pk,
PullFilter pkt,
PGP_MPI **  m_p 
)
static

Definition at line 124 of file pgp-pubdec.c.

125{
126 int res;
127 PGP_MPI *c;
128
129 if (pk->algo != PGP_PUB_RSA_ENCRYPT
131 return PXE_PGP_WRONG_KEY;
132
133 /* read rsa encrypted data */
134 res = pgp_mpi_read(pkt, &c);
135 if (res < 0)
136 return res;
137
138 /* decrypt */
139 res = pgp_rsa_decrypt(pk, c, m_p);
140
142 return res;
143}
int pgp_rsa_decrypt(PGP_PubKey *pk, PGP_MPI *_c, PGP_MPI **m_p)
@ PGP_PUB_RSA_ENCRYPT_SIGN
Definition: pgp.h:68
@ PGP_PUB_RSA_ENCRYPT
Definition: pgp.h:69
char * c

References PGP_PubKey::algo, pgp_mpi_free(), pgp_mpi_read(), PGP_PUB_RSA_ENCRYPT, PGP_PUB_RSA_ENCRYPT_SIGN, pgp_rsa_decrypt(), and PXE_PGP_WRONG_KEY.

Referenced by pgp_parse_pubenc_sesskey().

◆ pgp_parse_pubenc_sesskey()

int pgp_parse_pubenc_sesskey ( PGP_Context ctx,
PullFilter pkt 
)

Definition at line 150 of file pgp-pubdec.c.

151{
152 int ver;
153 int algo;
154 int res;
155 uint8 key_id[8];
156 PGP_PubKey *pk;
157 uint8 *msg;
158 int msglen;
159 PGP_MPI *m;
160
161 pk = ctx->pub_key;
162 if (pk == NULL)
163 {
164 px_debug("no pubkey?");
165 return PXE_BUG;
166 }
167
168 GETBYTE(pkt, ver);
169 if (ver != 3)
170 {
171 px_debug("unknown pubenc_sesskey pkt ver=%d", ver);
173 }
174
175 /*
176 * check if keyid's match - user-friendly msg
177 */
178 res = pullf_read_fixed(pkt, 8, key_id);
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");
185 return PXE_PGP_WRONG_KEY;
186 }
187
188 /*
189 * Decrypt
190 */
191 GETBYTE(pkt, algo);
192 switch (algo)
193 {
195 res = decrypt_elgamal(pk, pkt, &m);
196 break;
199 res = decrypt_rsa(pk, pkt, &m);
200 break;
201 default:
203 }
204 if (res < 0)
205 return res;
206
207 /*
208 * extract message
209 */
210 msg = check_eme_pkcs1_v15(m->data, m->bytes);
211 if (msg == NULL)
212 {
213 px_debug("check_eme_pkcs1_v15 failed");
214 res = PXE_PGP_WRONG_KEY;
215 goto out;
216 }
217 msglen = m->bytes - (msg - m->data);
218
219 res = control_cksum(msg, msglen);
220 if (res < 0)
221 goto out;
222
223 /*
224 * got sesskey
225 */
226 ctx->cipher_algo = *msg;
227 ctx->sess_key_len = msglen - 3;
228 memcpy(ctx->sess_key, msg + 1, ctx->sess_key_len);
229
230out:
231 pgp_mpi_free(m);
232 if (res < 0)
233 return res;
234 return pgp_expect_packet_end(pkt);
235}
#define GETBYTE(x, i)
Definition: hstore_gist.c:40
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
Definition: mbuf.c:301
int pgp_expect_packet_end(PullFilter *pkt)
Definition: pgp-decrypt.c:1078
static int decrypt_rsa(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
Definition: pgp-pubdec.c:124
static const uint8 any_key[]
Definition: pgp-pubdec.c:147
static int decrypt_elgamal(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
Definition: pgp-pubdec.c:97
static uint8 * check_eme_pkcs1_v15(uint8 *data, int len)
Definition: pgp-pubdec.c:42
static int control_cksum(uint8 *msg, int msglen)
Definition: pgp-pubdec.c:74
#define PXE_BUG
Definition: px.h:58
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:67
#define PXE_PGP_UNKNOWN_PUBALGO
Definition: px.h:79
unsigned sess_key_len
Definition: pgp.h:172
PGP_PubKey * pub_key
Definition: pgp.h:164
int cipher_algo
Definition: pgp.h:144
uint8 sess_key[PGP_MAX_KEY]
Definition: pgp.h:171
int bytes
Definition: pgp.h:183
uint8 * data
Definition: pgp.h:181
uint8 key_id[8]
Definition: pgp.h:235

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().

Variable Documentation

◆ any_key

const uint8 any_key[] = {0, 0, 0, 0, 0, 0, 0, 0}
static

Definition at line 147 of file pgp-pubdec.c.

Referenced by pgp_parse_pubenc_sesskey().