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