crypt: Remove RC4 support.
[paraslash.git] / crypt.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file crypt.c Openssl-based encryption/decryption routines. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <openssl/rand.h>
13 #include <openssl/err.h>
14 #include <openssl/pem.h>
15 #include <openssl/sha.h>
16 #include <openssl/bn.h>
17 #include <openssl/aes.h>
18
19 #include "para.h"
20 #include "error.h"
21 #include "string.h"
22 #include "crypt.h"
23 #include "fd.h"
24 #include "crypt_backend.h"
25 #include "base64.h"
26
27 struct asymmetric_key {
28         RSA *rsa;
29 };
30
31 void get_random_bytes_or_die(unsigned char *buf, int num)
32 {
33         unsigned long err;
34
35         /* RAND_bytes() returns 1 on success, 0 otherwise. */
36         if (RAND_bytes(buf, num) == 1)
37                 return;
38         err = ERR_get_error();
39         PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
40         exit(EXIT_FAILURE);
41 }
42
43 /*
44  * Read 64 bytes from /dev/urandom and adds them to the SSL PRNG. Seed the PRNG
45  * used by random() with a random seed obtained from SSL. If /dev/random is not
46  * readable the function calls exit().
47  *
48  * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
49  * random(3), \ref para_random().
50  */
51 void init_random_seed_or_die(void)
52 {
53         int seed, ret = RAND_load_file("/dev/urandom", 64);
54
55         if (ret != 64) {
56                 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
57                 exit(EXIT_FAILURE);
58         }
59         get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
60         srandom(seed);
61 }
62
63 static EVP_PKEY *load_key(const char *file, int private)
64 {
65         BIO *key;
66         EVP_PKEY *pkey = NULL;
67         int ret = check_key_file(file, private);
68
69         if (ret < 0) {
70                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
71                 return NULL;
72         }
73         key = BIO_new(BIO_s_file());
74         if (!key)
75                 return NULL;
76         if (BIO_read_filename(key, file) > 0) {
77                 if (private == LOAD_PRIVATE_KEY)
78                         pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
79                 else
80                         pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
81         }
82         BIO_free(key);
83         return pkey;
84 }
85
86 static int get_openssl_key(const char *key_file, RSA **rsa, int private)
87 {
88         EVP_PKEY *key = load_key(key_file, private);
89
90         if (!key)
91                 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
92                         : -E_PUBLIC_KEY;
93         *rsa = EVP_PKEY_get1_RSA(key);
94         EVP_PKEY_free(key);
95         if (!*rsa)
96                 return -E_RSA;
97         return RSA_size(*rsa);
98 }
99
100 /*
101  * The public key loading functions below were inspired by corresponding code
102  * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
103  * Finland. However, not much of the original code remains.
104  */
105
106 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
107 {
108         const unsigned char *p = buf, *end = buf + len;
109         uint32_t bnsize;
110         BIGNUM *bn;
111
112         if (p + 4 < p)
113                 return -E_BIGNUM;
114         if (p + 4 > end)
115                 return -E_BIGNUM;
116         bnsize = read_ssh_u32(p);
117         PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
118         p += 4;
119         if (p + bnsize < p)
120                 return -E_BIGNUM;
121         if (p + bnsize > end)
122                 return -E_BIGNUM;
123         if (bnsize > 8192)
124                 return -E_BIGNUM;
125         bn = BN_bin2bn(p, bnsize, NULL);
126         if (!bn)
127                 return -E_BIGNUM;
128         *result = bn;
129         return bnsize + 4;
130 }
131
132 static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
133 {
134         int ret;
135         RSA *rsa;
136         BIGNUM *n, *e;
137         const unsigned char *p = blob, *end = blob + blen;
138
139         rsa = RSA_new();
140         if (!rsa)
141                 return -E_BIGNUM;
142         ret = read_bignum(p, end - p, &e);
143         if (ret < 0)
144                 goto fail;
145         p += ret;
146         ret = read_bignum(p, end - p, &n);
147         if (ret < 0)
148                 goto fail;
149 #ifdef HAVE_RSA_SET0_KEY
150         RSA_set0_key(rsa, n, e, NULL);
151 #else
152         rsa->n = n;
153         rsa->e = e;
154 #endif
155         *result = rsa;
156         return 1;
157 fail:
158         RSA_free(rsa);
159         return ret;
160 }
161
162 int get_asymmetric_key(const char *key_file, int private,
163                 struct asymmetric_key **result)
164 {
165         struct asymmetric_key *key = NULL;
166         void *map = NULL;
167         unsigned char *blob = NULL;
168         size_t map_size, encoded_size, decoded_size;
169         int ret, ret2;
170         char *cp;
171
172         key = para_malloc(sizeof(*key));
173         if (private) {
174                 ret = get_openssl_key(key_file, &key->rsa, LOAD_PRIVATE_KEY);
175                 goto out;
176         }
177         ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
178         if (ret < 0)
179                 goto out;
180         ret = is_ssh_rsa_key(map, map_size);
181         if (!ret) {
182                 ret = para_munmap(map, map_size);
183                 map = NULL;
184                 if (ret < 0)
185                         goto out;
186                 ret = get_openssl_key(key_file, &key->rsa, LOAD_PUBLIC_KEY);
187                 goto out;
188         }
189         cp = map + ret;
190         encoded_size = map_size - ret;
191         PARA_INFO_LOG("decoding public rsa-ssh key %s\n", key_file);
192         ret = uudecode(cp, encoded_size, (char **)&blob, &decoded_size);
193         if (ret < 0)
194                 goto out_unmap;
195         ret = check_ssh_key_header(blob, decoded_size);
196         if (ret < 0)
197                 goto out_unmap;
198         ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
199         if (ret < 0)
200                 goto out_unmap;
201         ret = RSA_size(key->rsa);
202 out_unmap:
203         ret2 = para_munmap(map, map_size);
204         if (ret >= 0 && ret2 < 0)
205                 ret = ret2;
206 out:
207         if (ret < 0) {
208                 free(key);
209                 *result = NULL;
210                 PARA_ERROR_LOG("key %s: %s\n", key_file, para_strerror(-ret));
211         } else
212                 *result = key;
213         free(blob);
214         return ret;
215 }
216
217 void free_asymmetric_key(struct asymmetric_key *key)
218 {
219         if (!key)
220                 return;
221         RSA_free(key->rsa);
222         free(key);
223 }
224
225 int priv_decrypt(const char *key_file, unsigned char *outbuf,
226                 unsigned char *inbuf, int inlen)
227 {
228         struct asymmetric_key *priv;
229         int ret;
230
231         if (inlen < 0)
232                 return -E_RSA;
233         ret = get_asymmetric_key(key_file, LOAD_PRIVATE_KEY, &priv);
234         if (ret < 0)
235                 return ret;
236         /*
237          * RSA is vulnerable to timing attacks. Generate a random blinding
238          * factor to protect against this kind of attack.
239          */
240         ret = -E_BLINDING;
241         if (RSA_blinding_on(priv->rsa, NULL) == 0)
242                 goto out;
243         ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
244                 RSA_PKCS1_OAEP_PADDING);
245         RSA_blinding_off(priv->rsa);
246         if (ret <= 0)
247                 ret = -E_DECRYPT;
248 out:
249         free_asymmetric_key(priv);
250         return ret;
251 }
252
253 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
254                 unsigned len, unsigned char *outbuf)
255 {
256         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
257
258         if (flen < 0)
259                 return -E_ENCRYPT;
260         ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
261                 RSA_PKCS1_OAEP_PADDING);
262         return ret < 0? -E_ENCRYPT : ret;
263 }
264
265 struct stream_cipher {
266         EVP_CIPHER_CTX *aes;
267 };
268
269 struct stream_cipher *sc_new(const unsigned char *data, int len)
270 {
271         struct stream_cipher *sc = para_malloc(sizeof(*sc));
272
273         assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
274         sc->aes = EVP_CIPHER_CTX_new();
275         EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
276                 data + AES_CRT128_BLOCK_SIZE);
277         return sc;
278 }
279
280 void sc_free(struct stream_cipher *sc)
281 {
282         if (!sc)
283                 return;
284         EVP_CIPHER_CTX_free(sc->aes);
285         free(sc);
286 }
287
288 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
289                 struct iovec *dst)
290 {
291         int ret, inlen = src->iov_len, outlen, tmplen;
292
293         *dst = (typeof(*dst)) {
294                 /* Add one for the terminating zero byte. */
295                 .iov_base = para_malloc(inlen + 1),
296                 .iov_len = inlen
297         };
298         ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
299         assert(ret != 0);
300         ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
301         assert(ret != 0);
302         outlen += tmplen;
303         ((char *)dst->iov_base)[outlen] = '\0';
304         dst->iov_len = outlen;
305 }
306
307 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
308 {
309         return aes_ctr128_crypt(sc->aes, src, dst);
310 }
311
312 void hash_function(const char *data, unsigned long len, unsigned char *hash)
313 {
314         SHA_CTX c;
315         SHA1_Init(&c);
316         SHA1_Update(&c, data, len);
317         SHA1_Final(hash, &c);
318 }