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