Skip to content

Commit b160d6b

Browse files
author
Neil Conway
committed
pgcrypto update:
* Use error codes instead of -1 * px_strerror for new error codes * calling convention change for px_gen_salt - return error code * use px_strerror in pgcrypto.c Marko Kreen
1 parent fa332a0 commit b160d6b

File tree

9 files changed

+122
-66
lines changed

9 files changed

+122
-66
lines changed

contrib/pgcrypto/internal.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/internal.c,v 1.16 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232

@@ -275,7 +275,7 @@ rj_init(PX_Cipher * c, const uint8 *key, unsigned klen, const uint8 *iv)
275275
else if (klen <= 256 / 8)
276276
cx->keylen = 256 / 8;
277277
else
278-
return -1;
278+
return PXE_KEY_TOO_BIG;
279279

280280
memcpy(&cx->keybuf, key, klen);
281281

@@ -300,14 +300,14 @@ rj_encrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
300300
if (!cx->is_init)
301301
{
302302
if (rj_real_init(cx, 1))
303-
return -1;
303+
return PXE_CIPHER_INIT;
304304
}
305305

306306
if (dlen == 0)
307307
return 0;
308308

309309
if (dlen & 15)
310-
return -1;
310+
return PXE_NOTBLOCKSIZE;
311311

312312
memcpy(res, data, dlen);
313313

@@ -329,13 +329,13 @@ rj_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
329329

330330
if (!cx->is_init)
331331
if (rj_real_init(cx, 0))
332-
return -1;
332+
return PXE_CIPHER_INIT;
333333

334334
if (dlen == 0)
335335
return 0;
336336

337337
if (dlen & 15)
338-
return -1;
338+
return PXE_NOTBLOCKSIZE;
339339

340340
memcpy(res, data, dlen);
341341

@@ -422,7 +422,7 @@ bf_encrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
422422
return 0;
423423

424424
if (dlen & 7)
425-
return -1;
425+
return PXE_NOTBLOCKSIZE;
426426

427427
memcpy(res, data, dlen);
428428
switch (cx->mode)
@@ -446,7 +446,7 @@ bf_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *res)
446446
return 0;
447447

448448
if (dlen & 7)
449-
return -1;
449+
return PXE_NOTBLOCKSIZE;
450450

451451
memcpy(res, data, dlen);
452452
switch (cx->mode)
@@ -556,7 +556,7 @@ px_find_digest(const char *name, PX_MD ** res)
556556

557557
return 0;
558558
}
559-
return -1;
559+
return PXE_NO_HASH;
560560
}
561561

562562
int
@@ -575,7 +575,7 @@ px_find_cipher(const char *name, PX_Cipher ** res)
575575
}
576576

577577
if (c == NULL)
578-
return -1;
578+
return PXE_NO_CIPHER;
579579

580580
*res = c;
581581
return 0;

contrib/pgcrypto/openssl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.15 2005/03/21 05:18:45 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.16 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232
#include <postgres.h>
@@ -112,7 +112,7 @@ px_find_digest(const char *name, PX_MD ** res)
112112

113113
md = EVP_get_digestbyname(name);
114114
if (md == NULL)
115-
return -1;
115+
return PXE_NO_HASH;
116116

117117
ctx = px_alloc(sizeof(*ctx));
118118
EVP_DigestInit(ctx, md);
@@ -504,7 +504,7 @@ px_find_cipher(const char *name, PX_Cipher ** res)
504504
if (!strcmp(i->name, name))
505505
break;
506506
if (i->name == NULL)
507-
return -1;
507+
return PXE_NO_CIPHER;
508508

509509
od = px_alloc(sizeof(*od));
510510
memset(od, 0, sizeof(*od));

contrib/pgcrypto/pgcrypto.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.17 2005/03/21 05:18:45 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.18 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232
#include "postgres.h"
@@ -190,7 +190,7 @@ Datum
190190
pg_gen_salt(PG_FUNCTION_ARGS)
191191
{
192192
text *arg0;
193-
unsigned len;
193+
int len;
194194
text *res;
195195
char buf[PX_MAX_SALT_LEN + 1];
196196

@@ -204,10 +204,10 @@ pg_gen_salt(PG_FUNCTION_ARGS)
204204
memcpy(buf, VARDATA(arg0), len);
205205
buf[len] = 0;
206206
len = px_gen_salt(buf, buf, 0);
207-
if (len == 0)
207+
if (len < 0)
208208
ereport(ERROR,
209209
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
210-
errmsg("no such crypt algorithm")));
210+
errmsg("gen_salt: %s", px_strerror(len))));
211211

212212
res = (text *) palloc(len + VARHDRSZ);
213213
VARATT_SIZEP(res) = len + VARHDRSZ;
@@ -226,7 +226,7 @@ pg_gen_salt_rounds(PG_FUNCTION_ARGS)
226226
{
227227
text *arg0;
228228
int rounds;
229-
unsigned len;
229+
int len;
230230
text *res;
231231
char buf[PX_MAX_SALT_LEN + 1];
232232

@@ -241,10 +241,10 @@ pg_gen_salt_rounds(PG_FUNCTION_ARGS)
241241
memcpy(buf, VARDATA(arg0), len);
242242
buf[len] = 0;
243243
len = px_gen_salt(buf, buf, rounds);
244-
if (len == 0)
244+
if (len < 0)
245245
ereport(ERROR,
246246
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
247-
errmsg("no such crypt algorithm or bad number of rounds")));
247+
errmsg("gen_salt: %s", px_strerror(len))));
248248

249249
res = (text *) palloc(len + VARHDRSZ);
250250
VARATT_SIZEP(res) = len + VARHDRSZ;
@@ -360,7 +360,7 @@ pg_encrypt(PG_FUNCTION_ARGS)
360360
pfree(res);
361361
ereport(ERROR,
362362
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
363-
errmsg("encrypt error: %d", err)));
363+
errmsg("encrypt error: %s", px_strerror(err))));
364364
}
365365

366366
VARATT_SIZEP(res) = VARHDRSZ + rlen;
@@ -406,7 +406,7 @@ pg_decrypt(PG_FUNCTION_ARGS)
406406
if (err)
407407
ereport(ERROR,
408408
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
409-
errmsg("decrypt error: %d", err)));
409+
errmsg("decrypt error: %s", px_strerror(err))));
410410

411411
VARATT_SIZEP(res) = VARHDRSZ + rlen;
412412

@@ -461,7 +461,7 @@ pg_encrypt_iv(PG_FUNCTION_ARGS)
461461
if (err)
462462
ereport(ERROR,
463463
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
464-
errmsg("encrypt_iv error: %d", err)));
464+
errmsg("encrypt_iv error: %s", px_strerror(err))));
465465

466466
VARATT_SIZEP(res) = VARHDRSZ + rlen;
467467

@@ -517,7 +517,7 @@ pg_decrypt_iv(PG_FUNCTION_ARGS)
517517
if (err)
518518
ereport(ERROR,
519519
(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
520-
errmsg("decrypt_iv error: %d", err)));
520+
errmsg("decrypt_iv error: %s", px_strerror(err))));
521521

522522
VARATT_SIZEP(res) = VARHDRSZ + rlen;
523523

@@ -568,7 +568,7 @@ find_provider(text *name,
568568
if (err && !silent)
569569
ereport(ERROR,
570570
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
571-
errmsg("%s type does not exist: \"%s\"", desc, buf)));
571+
errmsg("Cannot use \"%s\": %s", buf, px_strerror(err))));
572572

573573
pfree(buf);
574574

contrib/pgcrypto/px-crypt.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.9 2005/03/21 05:18:45 neilc Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.c,v 1.10 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232
#include <postgres.h>
@@ -147,39 +147,40 @@ static struct generator gen_list[] = {
147147
{NULL, NULL, 0, 0, 0, 0}
148148
};
149149

150-
unsigned
150+
int
151151
px_gen_salt(const char *salt_type, char *buf, int rounds)
152152
{
153-
int i,
154-
res;
153+
int res;
155154
struct generator *g;
156155
char *p;
157156
char rbuf[16];
158157

159-
for (i = 0; gen_list[i].name; i++)
160-
{
161-
g = &gen_list[i];
162-
if (pg_strcasecmp(g->name, salt_type) != 0)
163-
continue;
158+
for (g = gen_list; g->name; g++)
159+
if (pg_strcasecmp(g->name, salt_type) == 0)
160+
break;
164161

165-
if (g->def_rounds)
166-
{
167-
if (rounds == 0)
168-
rounds = g->def_rounds;
162+
if (g->name == NULL)
163+
return PXE_UNKNOWN_SALT_ALGO;
169164

170-
if (rounds < g->min_rounds || rounds > g->max_rounds)
171-
return 0;
172-
}
165+
if (g->def_rounds)
166+
{
167+
if (rounds == 0)
168+
rounds = g->def_rounds;
173169

174-
res = px_get_random_bytes(rbuf, g->input_len);
175-
if (res != g->input_len)
176-
return 0;
170+
if (rounds < g->min_rounds || rounds > g->max_rounds)
171+
return PXE_BAD_SALT_ROUNDS;
172+
}
177173

178-
p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
179-
memset(rbuf, 0, sizeof(rbuf));
174+
res = px_get_random_bytes(rbuf, g->input_len);
175+
if (res < 0)
176+
return res;
180177

181-
return p != NULL ? strlen(p) : 0;
182-
}
178+
p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
179+
memset(rbuf, 0, sizeof(rbuf));
180+
181+
if (p == NULL)
182+
return PXE_BAD_SALT_ROUNDS;
183183

184-
return 0;
184+
return strlen(p);
185185
}
186+

contrib/pgcrypto/px-crypt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.h,v 1.6 2003/11/29 22:39:28 pgsql Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px-crypt.h,v 1.7 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232
#ifndef _PX_CRYPT_H
@@ -49,7 +49,7 @@
4949
* main interface
5050
*/
5151
char *px_crypt(const char *psw, const char *salt, char *buf, unsigned buflen);
52-
unsigned px_gen_salt(const char *salt_type, char *dst, int rounds);
52+
int px_gen_salt(const char *salt_type, char *dst, int rounds);
5353

5454
/*
5555
* internal functions

contrib/pgcrypto/px-hmac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/px-hmac.c,v 1.5 2003/11/29 22:39:28 pgsql Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/px-hmac.c,v 1.6 2005/03/21 05:19:55 neilc Exp $
3030
*/
3131

3232

@@ -158,7 +158,7 @@ px_find_hmac(const char *name, PX_HMAC ** res)
158158
if (bs < 2)
159159
{
160160
px_md_free(md);
161-
return -1;
161+
return PXE_HASH_UNUSABLE_FOR_HMAC;
162162
}
163163

164164
h = px_alloc(sizeof(*h));

0 commit comments

Comments
 (0)