]> git.tuebingen.mpg.de Git - paraslash.git/blob - openssl.c
openssl: Don't pass pointers to RSA structures around.
[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,
101                 struct asymmetric_key *result)
102 {
103         int ret;
104         RSA *rsa;
105         BIGNUM *n, *e;
106         const unsigned char *p = blob, *end = blob + blen;
107
108         rsa = RSA_new();
109         if (!rsa)
110                 return -E_BIGNUM;
111         ret = read_bignum(p, end - p, &e);
112         if (ret < 0)
113                 goto free_rsa;
114         p += ret;
115         ret = read_bignum(p, end - p, &n);
116         if (ret < 0)
117                 goto free_e;
118 #ifdef HAVE_RSA_SET0_KEY
119         RSA_set0_key(rsa, n, e, NULL);
120 #else
121         rsa->n = n;
122         rsa->e = e;
123 #endif
124         result->rsa = rsa;
125         return 1;
126 free_e:
127         BN_free(e);
128 free_rsa:
129         RSA_free(rsa);
130         return ret;
131 }
132
133 static int read_pem_private_key(const char *path, struct asymmetric_key *priv)
134 {
135         EVP_PKEY *pkey;
136         BIO *bio = BIO_new(BIO_s_file());
137
138         priv->rsa = NULL;
139         if (!bio)
140                 return -E_PRIVATE_KEY;
141         if (BIO_read_filename(bio, path) <= 0)
142                 goto bio_free;
143         pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
144         if (!pkey)
145                 goto bio_free;
146         priv->rsa = EVP_PKEY_get1_RSA(pkey);
147         EVP_PKEY_free(pkey);
148 bio_free:
149         BIO_free(bio);
150         return priv->rsa? RSA_size(priv->rsa) : -E_PRIVATE_KEY;
151 }
152
153 static int read_openssh_private_key(const unsigned char *blob,
154                 const unsigned char *end, struct asymmetric_key *priv)
155 {
156         int ret;
157         RSA *rsa;
158         BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
159         const unsigned char *cp = blob;
160
161         rsa = RSA_new();
162         if (!rsa)
163                 return -E_BIGNUM;
164         ret = read_bignum(cp, end - cp, &n);
165         if (ret < 0)
166                 goto free_rsa;
167         cp += ret;
168         ret = read_bignum(cp, end - cp, &e);
169         if (ret < 0)
170                 goto free_n;
171         cp += ret;
172         ret = read_bignum(cp, end - cp, &d);
173         if (ret < 0)
174                 goto free_e;
175         cp += ret;
176         ret = read_bignum(cp, end - cp, &iqmp);
177         if (ret < 0)
178                 goto free_d;
179         cp += ret;
180         ret = read_bignum(cp, end - cp, &p);
181         if (ret < 0)
182                 goto free_iqmp;
183         cp += ret;
184         ret = read_bignum(cp, end - cp, &q);
185         if (ret < 0)
186                 goto free_p;
187 #ifdef HAVE_RSA_SET0_KEY
188         RSA_set0_key(rsa, n, e, d);
189         RSA_set0_factors(rsa, p, q);
190         RSA_set0_crt_params(rsa, NULL, NULL, iqmp);
191
192 #else
193         rsa->n = n;
194         rsa->e = e;
195         rsa->d = d;
196         rsa->iqmp = iqmp;
197         rsa->p = p;
198         rsa->q = q;
199 #endif
200         priv->rsa = rsa;
201         return 1;
202 free_p:
203         BN_clear_free(p);
204 free_iqmp:
205         BN_clear_free(iqmp);
206 free_d:
207         BN_clear_free(d);
208 free_e:
209         BN_free(e);
210 free_n:
211         BN_free(n);
212 free_rsa:
213         RSA_free(rsa);
214         return ret;
215 }
216
217 static int get_private_key(const char *path, struct asymmetric_key *priv)
218 {
219         int ret;
220         unsigned char *blob, *end;
221         size_t blob_size;
222
223         priv->rsa = NULL;
224         ret = decode_private_key(path, &blob, &blob_size);
225         if (ret < 0)
226                 return ret;
227         end = blob + blob_size;
228         if (ret == PKT_OPENSSH) {
229                 ret = find_openssh_bignum_offset(blob, blob_size);
230                 if (ret < 0)
231                         goto free_blob;
232                 PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
233                 ret = read_openssh_private_key(blob + ret, end, priv);
234         } else
235                 ret = read_pem_private_key(path, priv);
236 free_blob:
237         free(blob);
238         return ret;
239 }
240
241 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
242 {
243         unsigned char *blob;
244         size_t decoded_size;
245         int ret;
246         struct asymmetric_key *pub = alloc(sizeof(*pub));
247
248         ret = decode_public_key(key_file, &blob, &decoded_size);
249         if (ret < 0)
250                 goto out;
251         ret = read_public_key(blob + ret, decoded_size - ret, pub);
252         if (ret < 0)
253                 goto free_blob;
254         ret = RSA_size(pub->rsa);
255         assert(ret > 0);
256         *result = pub;
257 free_blob:
258         free(blob);
259 out:
260         if (ret < 0) {
261                 free(pub);
262                 *result = NULL;
263                 PARA_ERROR_LOG("can not load key %s\n", key_file);
264         }
265         return ret;
266 }
267
268 void apc_free_pubkey(struct asymmetric_key *pub)
269 {
270         if (!pub)
271                 return;
272         RSA_free(pub->rsa);
273         free(pub);
274 }
275
276 int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
277                 unsigned char *inbuf, int inlen)
278 {
279         struct asymmetric_key *priv;
280         int ret;
281
282         ret = check_private_key_file(key_file);
283         if (ret < 0)
284                 return ret;
285         if (inlen < 0)
286                 return -E_RSA;
287         priv = alloc(sizeof(*priv));
288         ret = get_private_key(key_file, priv);
289         if (ret < 0) {
290                 free(priv);
291                 return ret;
292         }
293         /*
294          * RSA is vulnerable to timing attacks. Generate a random blinding
295          * factor to protect against this kind of attack.
296          */
297         ret = -E_BLINDING;
298         if (RSA_blinding_on(priv->rsa, NULL) == 0)
299                 goto out;
300         ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
301                 RSA_PKCS1_OAEP_PADDING);
302         RSA_blinding_off(priv->rsa);
303         if (ret <= 0)
304                 ret = -E_DECRYPT;
305 out:
306         RSA_free(priv->rsa);
307         free(priv);
308         return ret;
309 }
310
311 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
312                 unsigned len, unsigned char *outbuf)
313 {
314         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
315
316         if (flen < 0)
317                 return -E_ENCRYPT;
318         ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
319                 RSA_PKCS1_OAEP_PADDING);
320         return ret < 0? -E_ENCRYPT : ret;
321 }
322
323 struct stream_cipher {
324         EVP_CIPHER_CTX *aes;
325 };
326
327 struct stream_cipher *sc_new(const unsigned char *data, int len)
328 {
329         struct stream_cipher *sc = alloc(sizeof(*sc));
330
331         assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
332         sc->aes = EVP_CIPHER_CTX_new();
333         EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
334                 data + AES_CRT128_BLOCK_SIZE);
335         return sc;
336 }
337
338 void sc_free(struct stream_cipher *sc)
339 {
340         if (!sc)
341                 return;
342         EVP_CIPHER_CTX_free(sc->aes);
343         free(sc);
344 }
345
346 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
347                 struct iovec *dst)
348 {
349         int ret, inlen = src->iov_len, outlen, tmplen;
350
351         *dst = (typeof(*dst)) {
352                 /* Add one for the terminating zero byte. */
353                 .iov_base = alloc(inlen + 1),
354                 .iov_len = inlen
355         };
356         ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
357         assert(ret != 0);
358         ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
359         assert(ret != 0);
360         outlen += tmplen;
361         ((char *)dst->iov_base)[outlen] = '\0';
362         dst->iov_len = outlen;
363 }
364
365 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
366 {
367         return aes_ctr128_crypt(sc->aes, src, dst);
368 }
369
370 void hash_function(const char *data, unsigned long len, unsigned char *hash)
371 {
372         EVP_MD_CTX *c = EVP_MD_CTX_new();
373         int ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL);
374         assert(ret != 0);
375         ret = EVP_DigestUpdate(c, data, len);
376         assert(ret != 0);
377         ret = EVP_DigestFinal_ex(c, hash, NULL);
378         assert(ret != 0);
379         EVP_MD_CTX_free(c);
380 }
381
382 void hash2_function(const char *data, unsigned long len, unsigned char *hash)
383 {
384         EVP_MD_CTX *c = EVP_MD_CTX_new();
385         int ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL);
386         assert(ret != 0);
387         ret = EVP_DigestUpdate(c, data, len);
388         assert(ret != 0);
389         ret = EVP_DigestFinal_ex(c, hash, NULL);
390         assert(ret != 0);
391         EVP_MD_CTX_free(c);
392 }