]> git.tuebingen.mpg.de Git - paraslash.git/blob - openssl.c
openssl: Kill rsa coefficient computations.
[paraslash.git] / openssl.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file openssl.c Openssl-based encryption/decryption routines. */
4
5 #include <regex.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <openssl/rand.h>
9 #include <openssl/err.h>
10 #include <openssl/pem.h>
11 #include <openssl/sha.h>
12 #include <openssl/bn.h>
13 #include <openssl/aes.h>
14 #include <openssl/evp.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "string.h"
19 #include "crypt.h"
20 #include "crypt_backend.h"
21 #include "portable_io.h"
22
23 struct asymmetric_key {
24         RSA *rsa;
25 };
26
27 void get_random_bytes_or_die(unsigned char *buf, int num)
28 {
29         unsigned long err;
30
31         /* RAND_bytes() returns 1 on success, 0 otherwise. */
32         if (RAND_bytes(buf, num) == 1)
33                 return;
34         err = ERR_get_error();
35         PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
36         exit(EXIT_FAILURE);
37 }
38
39 /*
40  * Read 64 bytes from /dev/urandom and add them to the SSL PRNG. Then seed the
41  * PRNG used by random(3) with a random seed obtained from SSL.
42  */
43 void crypt_init(void)
44 {
45         int seed, ret = RAND_load_file("/dev/urandom", 64);
46
47         if (ret != 64) {
48                 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
49                 exit(EXIT_FAILURE);
50         }
51         get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
52         srandom(seed);
53 }
54
55 void crypt_shutdown(void)
56 {
57 #ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
58         CRYPTO_cleanup_all_ex_data();
59 #endif
60 #ifdef HAVE_OPENSSL_THREAD_STOP /* openssl-1.1 or later */
61         OPENSSL_thread_stop();
62 #else /* openssl-1.0 */
63         ERR_remove_thread_state(NULL);
64 #endif
65         EVP_cleanup();
66 }
67
68 /*
69  * The public key loading functions below were inspired by corresponding code
70  * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
71  * Finland. However, not much of the original code remains.
72  */
73
74 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
75 {
76         const unsigned char *p = buf, *end = buf + len;
77         uint32_t bnsize;
78         BIGNUM *bn;
79
80         if (p + 4 < p)
81                 return -E_BIGNUM;
82         if (p + 4 > end)
83                 return -E_BIGNUM;
84         bnsize = read_u32_be(p);
85         PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
86         p += 4;
87         if (p + bnsize < p)
88                 return -E_BIGNUM;
89         if (p + bnsize > end)
90                 return -E_BIGNUM;
91         if (bnsize > 8192)
92                 return -E_BIGNUM;
93         bn = BN_bin2bn(p, bnsize, NULL);
94         if (!bn)
95                 return -E_BIGNUM;
96         *result = bn;
97         return bnsize + 4;
98 }
99
100 static int read_public_key(const unsigned char *blob, int blen, RSA **result)
101 {
102         int ret;
103         RSA *rsa;
104         BIGNUM *n, *e;
105         const unsigned char *p = blob, *end = blob + blen;
106
107         rsa = RSA_new();
108         if (!rsa)
109                 return -E_BIGNUM;
110         ret = read_bignum(p, end - p, &e);
111         if (ret < 0)
112                 goto free_rsa;
113         p += ret;
114         ret = read_bignum(p, end - p, &n);
115         if (ret < 0)
116                 goto free_e;
117 #ifdef HAVE_RSA_SET0_KEY
118         RSA_set0_key(rsa, n, e, NULL);
119 #else
120         rsa->n = n;
121         rsa->e = e;
122 #endif
123         *result = rsa;
124         return 1;
125 free_e:
126         BN_free(e);
127 free_rsa:
128         RSA_free(rsa);
129         return ret;
130 }
131
132 static int read_pem_private_key(const char *path, RSA **rsa)
133 {
134         EVP_PKEY *pkey;
135         BIO *bio = BIO_new(BIO_s_file());
136
137         *rsa = NULL;
138         if (!bio)
139                 return -E_PRIVATE_KEY;
140         if (BIO_read_filename(bio, path) <= 0)
141                 goto bio_free;
142         pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
143         if (!pkey)
144                 goto bio_free;
145         *rsa = EVP_PKEY_get1_RSA(pkey);
146         EVP_PKEY_free(pkey);
147 bio_free:
148         BIO_free(bio);
149         return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
150 }
151
152 static int read_openssh_private_key(const unsigned char *blob,
153                 const unsigned char *end, RSA **result)
154 {
155         int ret;
156         RSA *rsa;
157         BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
158         const unsigned char *cp = blob;
159
160         rsa = RSA_new();
161         if (!rsa)
162                 return -E_BIGNUM;
163         ret = read_bignum(cp, end - cp, &n);
164         if (ret < 0)
165                 goto free_rsa;
166         cp += ret;
167         ret = read_bignum(cp, end - cp, &e);
168         if (ret < 0)
169                 goto free_n;
170         cp += ret;
171         ret = read_bignum(cp, end - cp, &d);
172         if (ret < 0)
173                 goto free_e;
174         cp += ret;
175         ret = read_bignum(cp, end - cp, &iqmp);
176         if (ret < 0)
177                 goto free_d;
178         cp += ret;
179         ret = read_bignum(cp, end - cp, &p);
180         if (ret < 0)
181                 goto free_iqmp;
182         cp += ret;
183         ret = read_bignum(cp, end - cp, &q);
184         if (ret < 0)
185                 goto free_p;
186 #ifdef HAVE_RSA_SET0_KEY
187         RSA_set0_key(rsa, n, e, d);
188         RSA_set0_factors(rsa, p, q);
189         RSA_set0_crt_params(rsa, NULL, NULL, iqmp);
190
191 #else
192         rsa->n = n;
193         rsa->e = e;
194         rsa->d = d;
195         rsa->iqmp = iqmp;
196         rsa->p = p;
197         rsa->q = q;
198 #endif
199         *result = rsa;
200         return 1;
201 free_p:
202         BN_clear_free(p);
203 free_iqmp:
204         BN_clear_free(iqmp);
205 free_d:
206         BN_clear_free(d);
207 free_e:
208         BN_free(e);
209 free_n:
210         BN_free(n);
211 free_rsa:
212         RSA_free(rsa);
213         return ret;
214 }
215
216 static int get_private_key(const char *path, RSA **rsa)
217 {
218         int ret;
219         unsigned char *blob, *end;
220         size_t blob_size;
221
222         *rsa = NULL;
223         ret = decode_private_key(path, &blob, &blob_size);
224         if (ret < 0)
225                 return ret;
226         end = blob + blob_size;
227         if (ret == PKT_OPENSSH) {
228                 ret = find_openssh_bignum_offset(blob, blob_size);
229                 if (ret < 0)
230                         goto free_blob;
231                 PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
232                 ret = read_openssh_private_key(blob + ret, end, rsa);
233         } else
234                 ret = read_pem_private_key(path, rsa);
235 free_blob:
236         free(blob);
237         return ret;
238 }
239
240 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
241 {
242         unsigned char *blob;
243         size_t decoded_size;
244         int ret;
245         struct asymmetric_key *pub = alloc(sizeof(*pub));
246
247         ret = decode_public_key(key_file, &blob, &decoded_size);
248         if (ret < 0)
249                 goto out;
250         ret = read_public_key(blob + ret, decoded_size - ret, &pub->rsa);
251         if (ret < 0)
252                 goto free_blob;
253         ret = RSA_size(pub->rsa);
254         assert(ret > 0);
255         *result = pub;
256 free_blob:
257         free(blob);
258 out:
259         if (ret < 0) {
260                 free(pub);
261                 *result = NULL;
262                 PARA_ERROR_LOG("can not load key %s\n", key_file);
263         }
264         return ret;
265 }
266
267 void apc_free_pubkey(struct asymmetric_key *pub)
268 {
269         if (!pub)
270                 return;
271         RSA_free(pub->rsa);
272         free(pub);
273 }
274
275 int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
276                 unsigned char *inbuf, int inlen)
277 {
278         struct asymmetric_key *priv;
279         int ret;
280
281         ret = check_private_key_file(key_file);
282         if (ret < 0)
283                 return ret;
284         if (inlen < 0)
285                 return -E_RSA;
286         priv = alloc(sizeof(*priv));
287         ret = get_private_key(key_file, &priv->rsa);
288         if (ret < 0) {
289                 free(priv);
290                 return ret;
291         }
292         /*
293          * RSA is vulnerable to timing attacks. Generate a random blinding
294          * factor to protect against this kind of attack.
295          */
296         ret = -E_BLINDING;
297         if (RSA_blinding_on(priv->rsa, NULL) == 0)
298                 goto out;
299         ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
300                 RSA_PKCS1_OAEP_PADDING);
301         RSA_blinding_off(priv->rsa);
302         if (ret <= 0)
303                 ret = -E_DECRYPT;
304 out:
305         RSA_free(priv->rsa);
306         free(priv);
307         return ret;
308 }
309
310 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
311                 unsigned len, unsigned char *outbuf)
312 {
313         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
314
315         if (flen < 0)
316                 return -E_ENCRYPT;
317         ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
318                 RSA_PKCS1_OAEP_PADDING);
319         return ret < 0? -E_ENCRYPT : ret;
320 }
321
322 struct stream_cipher {
323         EVP_CIPHER_CTX *aes;
324 };
325
326 struct stream_cipher *sc_new(const unsigned char *data, int len)
327 {
328         struct stream_cipher *sc = alloc(sizeof(*sc));
329
330         assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
331         sc->aes = EVP_CIPHER_CTX_new();
332         EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
333                 data + AES_CRT128_BLOCK_SIZE);
334         return sc;
335 }
336
337 void sc_free(struct stream_cipher *sc)
338 {
339         if (!sc)
340                 return;
341         EVP_CIPHER_CTX_free(sc->aes);
342         free(sc);
343 }
344
345 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
346                 struct iovec *dst)
347 {
348         int ret, inlen = src->iov_len, outlen, tmplen;
349
350         *dst = (typeof(*dst)) {
351                 /* Add one for the terminating zero byte. */
352                 .iov_base = alloc(inlen + 1),
353                 .iov_len = inlen
354         };
355         ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
356         assert(ret != 0);
357         ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
358         assert(ret != 0);
359         outlen += tmplen;
360         ((char *)dst->iov_base)[outlen] = '\0';
361         dst->iov_len = outlen;
362 }
363
364 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
365 {
366         return aes_ctr128_crypt(sc->aes, src, dst);
367 }
368
369 void hash_function(const char *data, unsigned long len, unsigned char *hash)
370 {
371         EVP_MD_CTX *c = EVP_MD_CTX_new();
372         int ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL);
373         assert(ret != 0);
374         ret = EVP_DigestUpdate(c, data, len);
375         assert(ret != 0);
376         ret = EVP_DigestFinal_ex(c, hash, NULL);
377         assert(ret != 0);
378         EVP_MD_CTX_free(c);
379 }
380
381 void hash2_function(const char *data, unsigned long len, unsigned char *hash)
382 {
383         EVP_MD_CTX *c = EVP_MD_CTX_new();
384         int ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL);
385         assert(ret != 0);
386         ret = EVP_DigestUpdate(c, data, len);
387         assert(ret != 0);
388         ret = EVP_DigestFinal_ex(c, hash, NULL);
389         assert(ret != 0);
390         EVP_MD_CTX_free(c);
391 }