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