Mercurial > hg > quicktun
annotate src/proto.nacltai.c @ 19:3400045a57b8 V2.1.6
Version 2.1.6, changed sourcecode archive generation, use NaCl version 20110221, do not duplicate NaCl sourcecode, support iproute2 for persistent tunnel creation on Debian, support running as root on Debian, removed autogenerated header files
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 27 Feb 2011 22:42:27 +0100 |
parents | 5be1ecb80cc9 |
children | 38d495566d1c |
rev | line source |
---|---|
0 | 1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
2 Redistribution and use in source and binary forms, with or without modification, are | |
3 permitted provided that the following conditions are met: | |
4 | |
5 1. Redistributions of source code must retain the above copyright notice, this list of | |
6 conditions and the following disclaimer. | |
7 | |
8 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
9 of conditions and the following disclaimer in the documentation and/or other materials | |
10 provided with the distribution. | |
11 | |
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR | |
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
21 | |
22 The views and conclusions contained in the software and documentation are those of the | |
23 authors and should not be interpreted as representing official policies, either expressed | |
24 or implied, of Ivo Smits.*/ | |
25 | |
26 #include "common.c" | |
27 #include "crypto_box.h" | |
19
3400045a57b8
Version 2.1.6, changed sourcecode archive generation, use NaCl version 20110221, do not duplicate NaCl sourcecode, support iproute2 for persistent tunnel creation on Debian, support running as root on Debian, removed autogenerated header files
Ivo Smits <Ivo@UCIS.nl>
parents:
11
diff
changeset
|
28 #include "crypto_scalarmult_curve25519.h" |
0 | 29 #include <sys/types.h> |
30 #include <sys/time.h> | |
31 | |
32 #define uint64 unsigned long long //typedef unsigned long long uint64; | |
33 | |
34 struct tai { | |
35 uint64 x; | |
36 }; | |
37 struct taia { | |
38 struct tai sec; | |
39 unsigned long nano; /* 0...999999999 */ | |
40 unsigned long atto; /* 0...999999999 */ | |
41 }; | |
42 | |
43 struct qt_proto_data_nacltai { | |
44 unsigned char cenonce[crypto_box_NONCEBYTES], cdnonce[crypto_box_NONCEBYTES]; | |
45 unsigned char cbefore[crypto_box_BEFORENMBYTES]; | |
46 struct taia cdtaip, cdtaic; | |
47 }; | |
48 | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
49 #define noncelength 16 |
0 | 50 #define nonceoffset (crypto_box_NONCEBYTES - noncelength) |
51 /*static unsigned char cbefore[crypto_box_BEFORENMBYTES]; | |
52 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES]; | |
53 static const unsigned char* buffer1offset = buffer1 + crypto_box_ZEROBYTES; | |
54 static const unsigned char* buffer2offset = buffer2 + crypto_box_BOXZEROBYTES - noncelength;*/ | |
55 static const int overhead = crypto_box_BOXZEROBYTES + noncelength; | |
56 | |
57 void tai_pack(char *s, struct tai *t) { | |
58 uint64 x; | |
59 x = t->x; | |
60 s[7] = x & 255; x >>= 8; | |
61 s[6] = x & 255; x >>= 8; | |
62 s[5] = x & 255; x >>= 8; | |
63 s[4] = x & 255; x >>= 8; | |
64 s[3] = x & 255; x >>= 8; | |
65 s[2] = x & 255; x >>= 8; | |
66 s[1] = x & 255; x >>= 8; | |
67 s[0] = x; | |
68 } | |
69 void tai_unpack(char *s, struct tai *t) { | |
70 uint64 x; | |
71 x = (unsigned char) s[0]; | |
72 x <<= 8; x += (unsigned char) s[1]; | |
73 x <<= 8; x += (unsigned char) s[2]; | |
74 x <<= 8; x += (unsigned char) s[3]; | |
75 x <<= 8; x += (unsigned char) s[4]; | |
76 x <<= 8; x += (unsigned char) s[5]; | |
77 x <<= 8; x += (unsigned char) s[6]; | |
78 x <<= 8; x += (unsigned char) s[7]; | |
79 t->x = x; | |
80 } | |
81 void taia_pack(char *s, struct taia *t) { | |
82 unsigned long x; | |
83 tai_pack(s,&t->sec); | |
84 s += 8; | |
85 x = t->atto; | |
86 s[7] = x & 255; x >>= 8; | |
87 s[6] = x & 255; x >>= 8; | |
88 s[5] = x & 255; x >>= 8; | |
89 s[4] = x; | |
90 x = t->nano; | |
91 s[3] = x & 255; x >>= 8; | |
92 s[2] = x & 255; x >>= 8; | |
93 s[1] = x & 255; x >>= 8; | |
94 s[0] = x; | |
95 } | |
96 void taia_unpack(char *s, struct taia *t) { | |
97 unsigned long x; | |
98 tai_unpack(s,&t->sec); | |
99 s += 8; | |
100 x = (unsigned char) s[4]; | |
101 x <<= 8; x += (unsigned char) s[5]; | |
102 x <<= 8; x += (unsigned char) s[6]; | |
103 x <<= 8; x += (unsigned char) s[7]; | |
104 t->atto = x; | |
105 x = (unsigned char) s[0]; | |
106 x <<= 8; x += (unsigned char) s[1]; | |
107 x <<= 8; x += (unsigned char) s[2]; | |
108 x <<= 8; x += (unsigned char) s[3]; | |
109 t->nano = x; | |
110 } | |
111 | |
112 void taia_now(struct taia *t) { | |
113 struct timeval now; | |
114 gettimeofday(&now,(struct timezone *) 0); | |
115 t->sec.x = 4611686018427387914ULL + (uint64) now.tv_sec; | |
116 t->nano = 1000 * now.tv_usec + 500; | |
117 t->atto = 0; | |
118 } | |
119 | |
120 static int encode(struct qtsession* sess, char* raw, char* enc, int len) { | |
11
5be1ecb80cc9
Fixed build script for debian systems without packaging tools, removed packet logging from nacltai
root <root@Really.UFO-Net.nl>
parents:
10
diff
changeset
|
121 // fprintf(stderr, "Encoding packet of %d bytes from %d to %d\n", len, raw, enc); |
0 | 122 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; |
123 memset(raw, 0, crypto_box_ZEROBYTES); | |
124 taia_now(&d->cdtaic); | |
125 taia_pack(d->cenonce + nonceoffset, &(d->cdtaic)); | |
126 if (crypto_box_afternm(enc, raw, len + crypto_box_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Crypto failed"); | |
127 memcpy((void*)(enc + crypto_box_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength); | |
128 len += overhead; | |
11
5be1ecb80cc9
Fixed build script for debian systems without packaging tools, removed packet logging from nacltai
root <root@Really.UFO-Net.nl>
parents:
10
diff
changeset
|
129 // fprintf(stderr, "Encoded packet of %d bytes from %d to %d\n", len, raw, enc); |
0 | 130 return len; |
131 } | |
132 | |
133 static int decode(struct qtsession* sess, char* enc, char* raw, int len) { | |
11
5be1ecb80cc9
Fixed build script for debian systems without packaging tools, removed packet logging from nacltai
root <root@Really.UFO-Net.nl>
parents:
10
diff
changeset
|
134 // fprintf(stderr, "Decoding packet of %d bytes from %d to %d\n", len, enc, raw); |
0 | 135 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; |
136 int i; | |
137 if (len < overhead) { | |
138 fprintf(stderr, "Short packet received: %d\n", len); | |
139 return 0; | |
140 } | |
141 len -= overhead; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
142 taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &d->cdtaic); |
10
1e4ba8d8ffc2
Use strncat, fixed nacltai timestamp check
Ivo Smits <Ivo@UCIS.nl>
parents:
6
diff
changeset
|
143 if (d->cdtaic.sec.x <= d->cdtaip.sec.x && d->cdtaic.nano <= d->cdtaip.nano && d->cdtaic.atto <= d->cdtaip.atto) { |
0 | 144 fprintf(stderr, "Timestamp going back, ignoring packet\n"); |
145 return 0; | |
146 } | |
147 memcpy(d->cdnonce + nonceoffset, enc + crypto_box_BOXZEROBYTES - noncelength, noncelength); | |
148 memset(enc, 0, crypto_box_BOXZEROBYTES); | |
149 if (i = crypto_box_open_afternm(raw, enc, len + crypto_box_ZEROBYTES, d->cdnonce, d->cbefore)) { | |
150 fprintf(stderr, "Decryption failed len=%d result=%d\n", len, i); | |
151 return 0; | |
152 } | |
153 d->cdtaip = d->cdtaic; | |
11
5be1ecb80cc9
Fixed build script for debian systems without packaging tools, removed packet logging from nacltai
root <root@Really.UFO-Net.nl>
parents:
10
diff
changeset
|
154 // fprintf(stderr, "Decoded packet of %d bytes from %d to %d\n", len, enc, raw); |
0 | 155 return len; |
156 } | |
157 | |
158 static int init(struct qtsession* sess) { | |
159 struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data; | |
160 char* envval; | |
161 printf("Initializing cryptography...\n"); | |
162 unsigned char cownpublickey[crypto_box_PUBLICKEYBYTES], cpublickey[crypto_box_PUBLICKEYBYTES], csecretkey[crypto_box_SECRETKEYBYTES]; | |
163 if (!(envval = getconf("PUBLIC_KEY"))) return errorexit("Missing PUBLIC_KEY"); | |
164 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PUBLIC_KEY length"); | |
165 hex2bin(cpublickey, envval, crypto_box_PUBLICKEYBYTES); | |
166 if (!(envval = getconf("PRIVATE_KEY"))) return errorexit("Missing PRIVATE_KEY"); | |
167 if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length"); | |
168 hex2bin(csecretkey, envval, crypto_box_SECRETKEYBYTES); | |
169 crypto_box_beforenm(d->cbefore, cpublickey, csecretkey); | |
170 | |
171 memset(d->cenonce, 0, crypto_box_NONCEBYTES); | |
172 memset(d->cdnonce, 0, crypto_box_NONCEBYTES); | |
173 | |
19
3400045a57b8
Version 2.1.6, changed sourcecode archive generation, use NaCl version 20110221, do not duplicate NaCl sourcecode, support iproute2 for persistent tunnel creation on Debian, support running as root on Debian, removed autogenerated header files
Ivo Smits <Ivo@UCIS.nl>
parents:
11
diff
changeset
|
174 crypto_scalarmult_curve25519_base(cownpublickey, csecretkey); |
0 | 175 |
176 if (envval = getenv("TIME_WINDOW")) { | |
177 taia_now(&d->cdtaip); | |
178 d->cdtaip.sec.x -= atol(envval); | |
179 } else { | |
4 | 180 fprintf(stderr, "Warning: TIME_WINDOW not set, risking an initial replay attack\n"); |
0 | 181 } |
182 if (envval = getenv("ROLE")) { | |
183 d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0; | |
184 } else { | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
185 d->cenonce[nonceoffset-1] = memcmp(cownpublickey, cpublickey, crypto_box_PUBLICKEYBYTES) > 0 ? 1 : 0; |
0 | 186 } |
187 d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
188 return 0; |
0 | 189 } |
190 | |
191 #ifdef COMBINED_BINARY | |
192 int tunmain_nacltai() { | |
193 #else | |
194 int tunmain() { | |
195 #endif | |
196 struct qtproto p = { | |
197 1, | |
198 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | |
199 MAX_PACKET_LEN + crypto_box_ZEROBYTES, | |
200 crypto_box_ZEROBYTES, | |
201 crypto_box_BOXZEROBYTES - noncelength, | |
202 encode, | |
203 decode, | |
204 init, | |
205 sizeof(struct qt_proto_data_nacltai), | |
206 }; | |
207 return qtrun(&p); | |
208 } | |
209 | |
210 #ifndef COMBINED_BINARY | |
211 int main() { | |
212 print_header(); | |
213 return tunmain(); | |
214 } | |
215 #endif |