Fixed meta protocol.
[tinc] / src / encr.c
1 /*
2     encr.c -- everything that deals with encryption
3     Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "config.h"
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <syslog.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29
30 #ifdef HAVE_GMP_H
31 # include <gmp.h>
32 #else
33 # ifdef HAVE_GMP2_GMP_H
34 #  include <gmp2/gmp.h>
35 # endif
36 #endif
37
38 #include <utils.h>
39 #include <xalloc.h>
40
41 #include <cipher.h>
42
43 #include "conf.h"
44 #include "encr.h"
45 #include "net.h"
46 #include "protocol.h"
47
48 #define ENCR_GENERATOR "0xd"
49 #define ENCR_PRIME "0x7fffffffffffffffffffffffffffffff" /* Mersenne :) */
50
51 char text_key[1000];
52 char *my_public_key_base36;
53 int key_inited = 0, encryption_keylen;
54 mpz_t my_private_key, my_public_key, generator, shared_prime;
55 int my_key_expiry = (time_t)(-1);
56
57 static char* mypassphrase;
58 static int mypassphraselen;
59
60 int char_hex_to_bin(int c)
61 {
62   if(isdigit(c))
63     return c - '0';
64   else
65     return tolower(c) - 'a' + 10;
66 }
67
68 int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
69 {
70   int i = 0, j = 0, l = strlen(hex);
71 cp
72   if(l&1)
73     {
74       i = j = 1;
75       bin[0] = char_hex_to_bin(hex[0]);
76     }
77   for(; i < l; i+=2, j++)
78     bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
79 cp
80   return j&1?j+1:j;
81 }
82
83 int read_passphrase(char *which, char **out)
84 {
85   FILE *f;
86   config_t const *cfg;
87   char *filename;
88   int size;
89   extern char *confbase;
90   char *pp;
91 cp
92   if((cfg = get_config_val(passphrasesdir)) == NULL)
93     {
94       filename = xmalloc(strlen(confbase)+13+strlen(which));
95       sprintf(filename, "%spassphrases/%s", confbase, which);
96     }
97   else
98     {
99       filename = xmalloc(strlen(cfg->data.ptr)+2+strlen(which));
100       sprintf(filename, "%s/%s", (char*)cfg->data.ptr, which);
101     }
102
103   if((f = fopen(filename, "rb")) == NULL)
104     {
105       syslog(LOG_ERR, "Could not open %s: %m", filename);
106       return -1;
107     }
108
109   fscanf(f, "%d ", &size);
110   if(size < 1 || size > (1<<15))
111     {
112       syslog(LOG_ERR, "Illegal passphrase in %s; size would be %d", filename, size);
113       return -1;
114     }
115   size >>= 2; /* bits->nibbles */
116   pp = xmalloc(size+2);
117   fgets(pp, size+1, f);
118   fclose(f);
119
120   *out = xmalloc(size);
121 cp
122   return str_hex_to_bin(*out, pp);
123 }
124
125 int read_my_passphrase(void)
126 {
127 cp
128   if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
129     return -1;
130 cp
131   return 0;
132 }
133
134 int generate_private_key(void)
135 {
136   FILE *f;
137   int i;
138   char *s;
139   config_t const *cfg;
140 cp
141   if((cfg = get_config_val(keyexpire)) == NULL)
142     my_key_expiry = (time_t)(time(NULL) + 3600);
143   else
144     my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
145
146   syslog(LOG_NOTICE, "Generating %d bits keys.", PRIVATE_KEY_BITS);
147
148   if((f = fopen("/dev/urandom", "r")) == NULL)
149     {
150       syslog(LOG_ERR, "Opening /dev/urandom failed: %m");
151       return -1;
152     }
153
154   s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
155
156   for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
157     sprintf(&s[i << 1], "%02x", fgetc(f));
158
159   s[2 * PRIVATE_KEY_LENGTH] = '\0';
160
161   mpz_set_str(my_private_key, s, 16);
162 cp
163   return 0;
164 }
165
166 void calculate_public_key(void)
167 {
168 cp
169   mpz_powm(my_public_key, generator, my_private_key, shared_prime);
170   my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
171 cp
172 }
173
174 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
175
176 int security_init(void)
177 {
178 cp
179   mpz_init(my_private_key);
180   mpz_init(my_public_key);
181   mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
182   mpz_init_set_str(generator, ENCR_GENERATOR, 0);
183
184   if(read_my_passphrase() < 0)
185     return -1;
186   if(generate_private_key() < 0)
187     return -1;
188
189   if(cipher_init(CIPHER_BLOWFISH) < 0)
190     return -1;
191
192   calculate_public_key();
193 cp
194   return 0;
195 }
196
197 void set_shared_key(char *almost_key)
198 {
199   char *tmp;
200   int len;
201   mpz_t ak, our_shared_key;
202 cp
203   mpz_init_set_str(ak, almost_key, 36);
204   mpz_init(our_shared_key);
205   mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
206
207   tmp = mpz_get_str(NULL, 16, our_shared_key);
208   len = str_hex_to_bin(text_key, tmp);
209
210   cipher_set_key(&encryption_key, len, text_key);
211   key_inited = 1;
212   encryption_keylen = len;
213
214   if(debug_lvl > 2)
215     syslog(LOG_INFO, "Encryption key set to %s", tmp);
216
217   free(tmp);
218   mpz_clear(ak);
219   mpz_clear(our_shared_key);
220 cp
221 }
222
223
224 void encrypt_passphrase(passphrase_t *pp)
225 {
226   char key[1000];
227   char tmp[1000];
228   unsigned char phrase[1000];
229   int keylen;
230   int i;
231   BF_KEY bf_key;
232   
233 cp  
234   mpz_get_str(tmp, 16, my_public_key);
235   keylen = str_hex_to_bin(key, tmp);
236
237   cipher_set_key(&bf_key, keylen, key);
238
239   low_crypt_key(mypassphrase, phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
240   pp->len = ((mypassphraselen - 1) | 7) + 1;
241   pp->phrase = xmalloc((pp->len << 1) + 1);
242   
243   for(i = 0; i < pp->len; i++)
244     snprintf(&(pp->phrase)[i << 1], 3, "%02x", (int)phrase[i]);
245
246   pp->phrase[(pp->len << 1) + 1] = '\0';
247
248   if(key_inited)
249     cipher_set_key(&encryption_key, encryption_keylen, text_key);
250 cp
251 }
252
253 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
254 {
255   char key[1000];
256   char *tmp;
257   unsigned char phrase[1000];
258   int keylen, pplen;
259   mpz_t pk;
260   unsigned char *out;
261   BF_KEY bf_key;
262   char which[sizeof("123.123.123.123")+1];
263   char *meuk;
264 cp
265   mpz_init_set_str(pk, his_pubkey, 36);
266   tmp = mpz_get_str(NULL, 16, pk);
267   keylen = str_hex_to_bin(key, tmp);
268   out = xmalloc((cl->pp->len >> 1) + 3);
269   pplen = str_hex_to_bin(phrase, cl->pp->phrase);
270
271   cipher_set_key(&bf_key, keylen, key);
272   low_crypt_key(phrase, out, &bf_key, pplen, BF_DECRYPT);
273   if(key_inited)
274     cipher_set_key(&encryption_key, encryption_keylen, text_key);
275
276   sprintf(which, IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
277   if((pplen = read_passphrase(which, &meuk)) < 0)
278     return -1;
279
280   if(memcmp(meuk, out, pplen))
281     return -1;
282 cp
283   return 0;
284 }
285
286 char *make_shared_key(char *pk)
287 {
288   mpz_t tmp, res;
289   char *r;
290 cp
291   mpz_init_set_str(tmp, pk, 36);
292   mpz_init(res);
293   mpz_powm(res, tmp, my_private_key, shared_prime);
294
295   r = mpz_get_str(NULL, 36, res);
296
297   mpz_clear(res);
298   mpz_clear(tmp);
299 cp
300   return r;
301 }
302
303 /*
304   free a key after overwriting it
305 */
306 void free_key(enc_key_t *k)
307 {
308 cp
309   if(!k)
310     return;
311   if(k->key)
312     {
313       memset(k->key, (char)(-1), k->length);
314       free(k->key);
315     }
316   free(k);
317 cp
318 }
319
320 void recalculate_encryption_keys(void)
321 {
322   conn_list_t *p;
323   char *ek;
324 cp
325   for(p = conn_list; p != NULL; p = p->next)
326     {
327       if(!p->public_key || !p->public_key->key)
328         /* We haven't received a key from this host (yet). */
329         continue;
330       ek = make_shared_key(p->public_key->key);
331       free_key(p->key);
332       p->key = xmalloc(sizeof(*p->key));
333       p->key->length = strlen(ek);
334       p->key->expiry = p->public_key->expiry;
335       p->key->key = xmalloc(strlen(ek) + 1);
336       strcpy(p->key->key, ek);
337     }
338 cp
339 }
340
341 void regenerate_keys(void)
342 {
343 cp
344   generate_private_key();
345   calculate_public_key();
346   send_key_changed2();
347   recalculate_encryption_keys();
348 cp
349 }