forked from mdornseif/didentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
didentd-genanswer-crypt.c
81 lines (66 loc) · 1.98 KB
/
didentd-genanswer-crypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* $Id: didentd-genanswer-crypt.c,v 1.12 2001/10/14 05:59:58 drt Exp $
* --drt@un.bewaff.net
*
* - generate a RfC 1413 reply containing
* uid of connection localip, localport,
* remoteip, remoteport and a timestamp
* in encrypted form
*
* You might find more info at http://c0re.jp/c0de/didentd/
*
* I do not belive there is something like copyright.
*
*/
#include "time.h"
#include "env.h"
#include "stralloc.h"
#include "strerr.h"
#include "uint16.h"
#include "uint32.h"
#include "base64.h"
#include "pad.h"
#include "rijndael.h"
#include "txtparse.h"
static char rcsid[] = "$Id: didentd-genanswer-crypt.c,v 1.12 2001/10/14 05:59:58 drt Exp $";
/* returns a pointer to a string describing a problem or NULL if
sucessfull, adds to answer the part after the ports of an RfC 1413
reply */
char *generate_answer(stralloc *answer, uint32 uid,
char *lip, uint16 lport, char *rip, uint16 rport)
{
char *problem = "ok";
char *x;
char buf[5];
stralloc out = {0};
stralloc tmp = {0};
stralloc key = {0};
/* get key from enviroment */
x = env_get("KEY");
if (!x)
{
problem = "$KEY not set";
strerr_warn1("didentd warning: $KEY not set using 'snakeoilkey'", NULL);
x = "snakeoilkey";
}
/* initialize rijndael with $KEY */
stralloc_copys(&key, x);
txtparse(&key);
pad(&key, 32);
rijndaelKeySched(6, 8, key.s);
/* build answer */
stralloc_cats(answer, " : USERID : OTHER : ");
uint32_pack(buf, uid); stralloc_catb(&tmp, buf, 4);
uint16_pack(buf, lport); stralloc_catb(&tmp, buf, 2);
uint16_pack(buf, rport); stralloc_catb(&tmp, buf, 2);
uint32_pack(buf, time(NULL)); stralloc_catb(&tmp, buf, 4);
stralloc_catb(&tmp, lip, 4);
stralloc_catb(&tmp, rip, 8);
/* encrypt last part of answer with rijndael */
rijndaelEncrypt(tmp.s);
stralloc_readyplus(&out, 32);
base64encode(out.s, tmp.s, 24);
stralloc_catb(answer, out.s, 32);
stralloc_cats(answer, "\r\n");
stralloc_0(answer);
return problem;
}