Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / contrib / pgcrypto / pgp-pubdec.c
blobc5449f6ed3f6f53ddb198906361932d0eaa59a81
1 /*
2 * pgp-pubdec.c
3 * Decrypt public-key encrypted session key.
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
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.
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.
29 * $PostgreSQL$
31 #include "postgres.h"
33 #include "px.h"
34 #include "mbuf.h"
35 #include "pgp.h"
38 * padded msg = 02 || PS || 00 || M
39 * PS - pad bytes
40 * M - msg
42 static uint8 *
43 check_eme_pkcs1_v15(uint8 *data, int len)
45 uint8 *data_end = data + len;
46 uint8 *p = data;
47 int rnd = 0;
49 if (len < 1 + 8 + 1)
50 return NULL;
52 if (*p++ != 2)
53 return NULL;
55 while (p < data_end && *p)
57 p++;
58 rnd++;
61 if (p == data_end)
62 return NULL;
63 if (*p != 0)
64 return NULL;
65 if (rnd < 8)
66 return NULL;
67 return p + 1;
71 * secret message: 1 byte algo, sesskey, 2 byte cksum
72 * ignore algo in cksum
74 static int
75 control_cksum(uint8 *msg, int msglen)
77 int i;
78 unsigned my_cksum,
79 got_cksum;
81 if (msglen < 3)
82 return PXE_PGP_WRONG_KEY;
84 my_cksum = 0;
85 for (i = 1; i < msglen - 2; i++)
86 my_cksum += msg[i];
87 my_cksum &= 0xFFFF;
88 got_cksum = ((unsigned) (msg[msglen - 2]) << 8) + msg[msglen - 1];
89 if (my_cksum != got_cksum)
91 px_debug("pubenc cksum failed");
92 return PXE_PGP_WRONG_KEY;
94 return 0;
97 static int
98 decrypt_elgamal(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
100 int res;
101 PGP_MPI *c1 = NULL;
102 PGP_MPI *c2 = NULL;
104 if (pk->algo != PGP_PUB_ELG_ENCRYPT)
105 return PXE_PGP_WRONG_KEY;
107 /* read elgamal encrypted data */
108 res = pgp_mpi_read(pkt, &c1);
109 if (res < 0)
110 goto out;
111 res = pgp_mpi_read(pkt, &c2);
112 if (res < 0)
113 goto out;
115 /* decrypt */
116 res = pgp_elgamal_decrypt(pk, c1, c2, m_p);
118 out:
119 pgp_mpi_free(c1);
120 pgp_mpi_free(c2);
121 return res;
124 static int
125 decrypt_rsa(PGP_PubKey *pk, PullFilter *pkt, PGP_MPI **m_p)
127 int res;
128 PGP_MPI *c;
130 if (pk->algo != PGP_PUB_RSA_ENCRYPT
131 && pk->algo != PGP_PUB_RSA_ENCRYPT_SIGN)
132 return PXE_PGP_WRONG_KEY;
134 /* read rsa encrypted data */
135 res = pgp_mpi_read(pkt, &c);
136 if (res < 0)
137 return res;
139 /* decrypt */
140 res = pgp_rsa_decrypt(pk, c, m_p);
142 pgp_mpi_free(c);
143 return res;
146 /* key id is missing - user is expected to try all keys */
147 static const uint8
148 any_key[] = {0, 0, 0, 0, 0, 0, 0, 0};
151 pgp_parse_pubenc_sesskey(PGP_Context *ctx, PullFilter *pkt)
153 int ver;
154 int algo;
155 int res;
156 uint8 key_id[8];
157 PGP_PubKey *pk;
158 uint8 *msg;
159 int msglen;
160 PGP_MPI *m;
162 pk = ctx->pub_key;
163 if (pk == NULL)
165 px_debug("no pubkey?");
166 return PXE_BUG;
169 GETBYTE(pkt, ver);
170 if (ver != 3)
172 px_debug("unknown pubenc_sesskey pkt ver=%d", ver);
173 return PXE_PGP_CORRUPT_DATA;
177 * check if keyid's match - user-friendly msg
179 res = pullf_read_fixed(pkt, 8, key_id);
180 if (res < 0)
181 return res;
182 if (memcmp(key_id, any_key, 8) != 0
183 && memcmp(key_id, pk->key_id, 8) != 0)
185 px_debug("key_id's does not match");
186 return PXE_PGP_WRONG_KEY;
190 * Decrypt
192 GETBYTE(pkt, algo);
193 switch (algo)
195 case PGP_PUB_ELG_ENCRYPT:
196 res = decrypt_elgamal(pk, pkt, &m);
197 break;
198 case PGP_PUB_RSA_ENCRYPT:
199 case PGP_PUB_RSA_ENCRYPT_SIGN:
200 res = decrypt_rsa(pk, pkt, &m);
201 break;
202 default:
203 res = PXE_PGP_UNKNOWN_PUBALGO;
205 if (res < 0)
206 return res;
209 * extract message
211 msg = check_eme_pkcs1_v15(m->data, m->bytes);
212 if (msg == NULL)
214 px_debug("check_eme_pkcs1_v15 failed");
215 res = PXE_PGP_WRONG_KEY;
216 goto out;
218 msglen = m->bytes - (msg - m->data);
220 res = control_cksum(msg, msglen);
221 if (res < 0)
222 goto out;
225 * got sesskey
227 ctx->cipher_algo = *msg;
228 ctx->sess_key_len = msglen - 3;
229 memcpy(ctx->sess_key, msg + 1, ctx->sess_key_len);
231 out:
232 pgp_mpi_free(m);
233 if (res < 0)
234 return res;
235 return pgp_expect_packet_end(pkt);