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