899a46b0718bb8dad51bc94a04123aa0e000204c
[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
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
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
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   size >>= 2; /* nibbles->bits */
111   pp = xmalloc(size+2);
112   fgets(pp, size+1, f);
113   fclose(f);
114
115   *out = xmalloc(size);
116   return str_hex_to_bin(*out, pp);
117 }
118
119 int read_my_passphrase(void)
120 {
121   if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
122     return -1;
123
124   return 0;
125 }
126
127 int generate_private_key(void)
128 {
129   FILE *f;
130   int i;
131   char *s;
132   config_t const *cfg;
133
134   if((cfg = get_config_val(keyexpire)) == NULL)
135     my_key_expiry = (time_t)(time(NULL) + 3600);
136   else
137     my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
138
139   syslog(LOG_NOTICE, "Generating %d bits keys.", PRIVATE_KEY_BITS);
140
141   if((f = fopen("/dev/urandom", "r")) == NULL)
142     {
143       syslog(LOG_ERR, "Opening /dev/urandom failed: %m");
144       return -1;
145     }
146
147   s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
148
149   for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
150     sprintf(&s[i << 1], "%02x", fgetc(f));
151
152   s[2 * PRIVATE_KEY_LENGTH] = '\0';
153
154   mpz_set_str(my_private_key, s, 16);
155
156   return 0;
157 }
158
159 void calculate_public_key(void)
160 {
161   mpz_powm(my_public_key, generator, my_private_key, shared_prime);
162   my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
163 }
164
165 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
166
167 int security_init(void)
168 {
169   mpz_init(my_private_key);
170   mpz_init(my_public_key);
171   mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
172   mpz_init_set_str(generator, ENCR_GENERATOR, 0);
173
174   if(read_my_passphrase() < 0)
175     return -1;
176   if(generate_private_key() < 0)
177     return -1;
178
179   if(cipher_init(CIPHER_BLOWFISH) < 0)
180     return -1;
181
182   calculate_public_key();
183
184   return 0;
185 }
186
187 void set_shared_key(char *almost_key)
188 {
189   char *tmp;
190   int len;
191   mpz_t ak, our_shared_key;
192
193   mpz_init_set_str(ak, almost_key, 36);
194   mpz_init(our_shared_key);
195   mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
196
197   tmp = mpz_get_str(NULL, 16, our_shared_key);
198   len = str_hex_to_bin(text_key, tmp);
199
200   cipher_set_key(&encryption_key, len, &text_key[0]);
201   key_inited = 1;
202   encryption_keylen = len;
203
204   if(debug_lvl > 2)
205     syslog(LOG_INFO, "Encryption key set to %s", tmp);
206
207   free(tmp);
208   mpz_clear(ak);
209   mpz_clear(our_shared_key);
210 }
211
212
213 void encrypt_passphrase(passphrase_t *pp)
214 {
215   char key[1000];
216   char tmp[1000];
217   int len;
218   BF_KEY bf_key;
219   
220   mpz_get_str(&tmp[0], 16, my_public_key);
221   len = str_hex_to_bin(key, tmp);
222
223   cipher_set_key(&bf_key, len, &key[0]);
224
225   low_crypt_key(mypassphrase, pp->phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
226   pp->len = ((mypassphraselen - 1) | 7) + 5;
227
228   if(key_inited)
229     cipher_set_key(&encryption_key, encryption_keylen, &text_key[0]);
230 }
231
232 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
233 {
234   char key[1000];
235   char tmp[1000];
236   int len;
237   mpz_t pk;
238   unsigned char *out;
239   BF_KEY bf_key;
240   char which[sizeof("123.123.123.123")+1];
241   char *meuk;
242
243   mpz_init_set_str(pk, his_pubkey, 36);
244   mpz_get_str(&tmp[0], 16, pk);
245   len = str_hex_to_bin(key, tmp);
246   out = xmalloc(cl->pp->len+3);
247
248   cipher_set_key(&bf_key, len, &key[0]);
249   low_crypt_key(cl->pp->phrase, out, &bf_key, cl->pp->len, BF_DECRYPT);
250   if(key_inited)
251     cipher_set_key(&encryption_key, encryption_keylen, &text_key[0]);
252
253   sprintf(&which[0], IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
254   if((len = read_passphrase(which, &meuk)) < 0)
255     return -1;
256
257   if(memcmp(meuk, out, len))
258     return -1;
259
260   return 0;
261 }
262
263 char *make_shared_key(char *pk)
264 {
265   mpz_t tmp, res;
266   char *r;
267
268   mpz_init_set_str(tmp, pk, 36);
269   mpz_init(res);
270   mpz_powm(res, tmp, my_private_key, shared_prime);
271
272   r = mpz_get_str(NULL, 36, res);
273
274   mpz_clear(res);
275   mpz_clear(tmp);
276
277   return r;
278 }
279
280 /*
281   free a key after overwriting it
282 */
283 void free_key(enc_key_t *k)
284 {
285   if(!k)
286     return;
287   if(k->key)
288     {
289       memset(k->key, (char)(-1), k->length);
290       free(k->key);
291     }
292   free(k);
293 }
294
295 void recalculate_encryption_keys(void)
296 {
297   conn_list_t *p;
298   char *ek;
299
300   for(p = conn_list; p != NULL; p = p->next)
301     {
302       if(!p->public_key || !p->public_key->key)
303         continue;
304       ek = make_shared_key(p->public_key->key);
305       if(!p->key)
306         {
307           p->key = xmalloc(sizeof(enc_key_t));
308           p->key->key = NULL;
309         }
310       if(p->key->key)
311         free(p->key->key);
312       p->key->length = strlen(ek);
313       p->key->expiry = p->public_key->expiry;
314       p->key->key = xmalloc(strlen(ek) + 1);
315       strcpy(p->key->key, ek);
316     }
317 }
318
319 void regenerate_keys(void)
320 {
321   generate_private_key();
322   calculate_public_key();
323   send_key_changed2();
324   recalculate_encryption_keys();
325 }