ucgi/: Return useful status codes when things go wrong.
[userv-utils] / ipif / mech-pkcs5.c
1 /*
2 * PKCS#5 padding mechanism for udp tunnel
3 *
4 * mechanism: pkcs5
5 * arguments: block size to pad to, must be power of 2 and <=128
6 *
7 * restrictions: none
8 * encoding: append between 1 and n bytes, all of the same value being
9 * the number of bytes appended
10 */
11 /*
12 * Copyright (C) 2000,2003 Ian Jackson
13 * This file is part of ipif, part of userv-utils
14 *
15 * This is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with userv-utils; if not, write to the Free Software
27 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 #include "forwarder.h"
31
32 struct mechdata {
33 unsigned mask;
34 };
35
36 static unsigned long setup(struct mechdata **md_r) {
37 struct mechdata *md;
38 unsigned long blocksize;
39
40 XMALLOC(md);
41
42 blocksize= getarg_ulong();
43 md->mask= blocksize - 1;
44 arg_assert(!(md->mask & blocksize));
45 arg_assert(blocksize <= 255);
46
47 *md_r= md;
48 return blocksize;
49 }
50
51 static void mes_pkcs5(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) {
52 unsigned long blocksize;
53
54 blocksize= setup(md_r);
55 *maxsuffix_io += blocksize + 1;
56 }
57
58 static void mds_pkcs5(struct mechdata **md_r) {
59 setup(md_r);
60 }
61
62 static void menc_pkcs5(struct mechdata *md, struct buffer *buf) {
63 unsigned char *pad;
64 int padlen;
65
66 /* eg with blocksize=4 mask=3 mask+2=5 */
67 /* msgsize 20 21 22 23 24 */
68 padlen= md->mask - buf->size; /* -17 -18 -19 -16 -17 */
69 padlen &= md->mask; /* 3 2 1 0 3 */
70 padlen++; /* 4 3 2 1 4 */
71
72 pad= buf_append(buf,padlen);
73 memset(pad,padlen,padlen);
74 }
75
76 static const char *mdec_pkcs5(struct mechdata *md, struct buffer *buf) {
77 unsigned char *padp;
78 unsigned padlen;
79 int i;
80
81 BUF_UNAPPEND(padp,buf,1);
82 padlen= *padp;
83 if (!padlen || (padlen > md->mask+1)) return "invalid length";
84
85 BUF_UNAPPEND(padp,buf,padlen-1);
86 for (i=0; i<padlen-1; i++)
87 if (*++padp != padlen) return "corrupted padding";
88
89 return 0;
90 }
91
92 STANDARD_MECHANISMLIST("pkcs5",pkcs5)