]> git.tuebingen.mpg.de Git - paraslash.git/blob - crypt.c
1172ddc333fdb12f2010c11785c39f773561e777
[paraslash.git] / crypt.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file crypt.c openssl-based RSA encryption/decryption routines */
8
9 #include <regex.h>
10 #include <dirent.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
15 #include <openssl/rc4.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "string.h"
20 #include "crypt.h"
21 #include "fd.h"
22 /**
23  * Fill a buffer with random content.
24  *
25  * \param buf The buffer to fill.
26  * \param num The size of \a buf in bytes.
27  *
28  * This function puts \a num cryptographically strong pseudo-random bytes into
29  * buf. If libssl can not guarantee an unpredictable byte sequence (for example
30  * because the PRNG has not been seeded with enough randomness) the function
31  * logs an error message and calls exit().
32  */
33 void get_random_bytes_or_die(unsigned char *buf, int num)
34 {
35         unsigned long err;
36
37         /* RAND_bytes() returns 1 on success, 0 otherwise. */
38         if (RAND_bytes(buf, num) == 1)
39                 return;
40         err = ERR_get_error();
41         PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
42         exit(EXIT_FAILURE);
43 }
44
45 /**
46  * Seed pseudo random number generators.
47  *
48  * This function reads 64 bytes from /dev/urandom and adds them to the SSL
49  * PRNG. It also seeds the PRNG used by random() with a random seed obtained
50  * from SSL. If /dev/random could not be read, an error message is logged and
51  * the function calls exit().
52  *
53  * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
54  * random(3), \ref para_random().
55  */
56 void init_random_seed_or_die(void)
57 {
58         int seed, ret = RAND_load_file("/dev/urandom", 64);
59
60         if (ret != 64) {
61                 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
62                 exit(EXIT_FAILURE);
63         }
64         get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
65         srandom(seed);
66 }
67
68 static EVP_PKEY *load_key(const char *file, int private)
69 {
70         BIO *key;
71         EVP_PKEY *pkey = 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 /**
87  * read an RSA key from a file
88  *
89  * \param key_file the file containing the key
90  * \param rsa RSA structure is returned here
91  * \param private if non-zero, read the private key, otherwise the public key
92  *
93  * \return The size of the RSA key on success, negative on errors.
94  *
95  * \sa openssl(1), rsa(1).
96  */
97 int get_rsa_key(char *key_file, RSA **rsa, int private)
98 {
99         EVP_PKEY *key = load_key(key_file, private);
100
101         if (!key)
102                 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
103                         : -E_PUBLIC_KEY;
104         *rsa = EVP_PKEY_get1_RSA(key);
105         EVP_PKEY_free(key);
106         if (!*rsa)
107                 return -E_RSA;
108         return RSA_size(*rsa);
109 }
110
111 /**
112  * free an RSA structure
113  *
114  * \param rsa pointer to the RSA struct to free
115  *
116  * This must be called for any key obtained by get_rsa_key().
117  */
118 void rsa_free(RSA *rsa)
119 {
120         if (rsa)
121                 RSA_free(rsa);
122 }
123
124 /**
125  * decrypt a buffer using an RSA key
126  *
127  * \param key_file full path of the rsa key
128  * \param outbuf the output buffer
129  * \param inbuf the encrypted input buffer
130  * \param rsa_inlen the length of \a inbuf
131  *
132  * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
133  *
134  * \return The size of the recovered plaintext on success, negative on errors.
135  *
136  * \sa RSA_private_decrypt(3)
137  **/
138 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
139                         unsigned rsa_inlen)
140 {
141         RSA *rsa;
142         int ret, inlen = rsa_inlen;
143
144         if (inlen < 0)
145                 return -E_RSA;
146         ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
147         if (ret < 0)
148                 return ret;
149         /*
150          * RSA is vulnerable to timing attacks. Generate a random blinding
151          * factor to protect against this kind of attack.
152          */
153         ret = -E_BLINDING;
154         if (RSA_blinding_on(rsa, NULL) == 0)
155                 goto out;
156         ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_OAEP_PADDING);
157         RSA_blinding_off(rsa);
158         if (ret <= 0)
159                 ret = -E_DECRYPT;
160 out:
161         rsa_free(rsa);
162         return ret;
163 }
164
165 /**
166  * encrypt a buffer using an RSA key
167  *
168  * \param rsa: public rsa key
169  * \param inbuf the input buffer
170  * \param len the length of \a inbuf
171  * \param outbuf the output buffer
172  *
173  * \return The size of the encrypted data on success, negative on errors
174  *
175  * \sa RSA_public_encrypt(3)
176  */
177 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
178                 unsigned len, unsigned char *outbuf)
179 {
180         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
181
182         if (flen < 0)
183                 return -E_ENCRYPT;
184         ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_OAEP_PADDING);
185         return ret < 0? -E_ENCRYPT : ret;
186 }
187
188 /**
189  * Encrypt and send a buffer.
190  *
191  * \param rc4c The rc4 crypt context.
192  * \param buf The buffer to send.
193  * \param len The size of \a buf in bytes.
194  *
195  * \return The return value of the underyling call to write_all().
196  *
197  * \sa \ref write_all(), RC4(3).
198  */
199 int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t len)
200 {
201         int ret;
202         unsigned char *tmp;
203
204         assert(len);
205         tmp = para_malloc(len);
206         RC4(&rc4c->send_key, len, (const unsigned char *)buf, tmp);
207         ret = write_all(rc4c->fd, (char *)tmp, &len);
208         free(tmp);
209         return ret;
210 }
211
212 /**
213  * Encrypt and send a \p NULL-terminated buffer.
214  *
215  * \param rc4c The rc4 crypt context.
216  * \param buf The buffer to send.
217  *
218  * \return The return value of the underyling call to rc4_send_bin_buffer().
219  */
220 int rc4_send_buffer(struct rc4_context *rc4c, const char *buf)
221 {
222         return rc4_send_bin_buffer(rc4c, buf, strlen(buf));
223 }
224
225 /**
226  * Format, encrypt and send a buffer.
227  *
228  * \param rc4c The rc4 crypt context.
229  * \param fmt A format string.
230  *
231  * \return The return value of the underyling call to rc4_send_buffer().
232  */
233 __printf_2_3 int rc4_send_va_buffer(struct rc4_context *rc4c, const char *fmt, ...)
234 {
235         char *msg;
236         int ret;
237
238         PARA_VSPRINTF(fmt, msg);
239         ret = rc4_send_buffer(rc4c, msg);
240         free(msg);
241         return ret;
242 }
243
244 /**
245  * Receive a buffer and decrypt it.
246  *
247  * \param rc4c The rc4 crypt context.
248  * \param buf The buffer to write the decrypted data to.
249  * \param size The size of \a buf.
250  *
251  * \return The number of bytes received on success, negative on errors, zero if
252  * the peer has performed an orderly shutdown.
253  *
254  * \sa recv(2), RC4(3).
255  */
256 int rc4_recv_bin_buffer(struct rc4_context *rc4c, char *buf, size_t size)
257 {
258         unsigned char *tmp = para_malloc(size);
259         ssize_t ret = recv(rc4c->fd, tmp, size, 0);
260
261         if (ret > 0)
262                 RC4(&rc4c->recv_key, ret, tmp, (unsigned char *)buf);
263         else if (ret < 0)
264                 ret = -ERRNO_TO_PARA_ERROR(errno);
265         free(tmp);
266         return ret;
267 }
268
269 /**
270  * Receive a buffer, decrypt it and write terminating NULL byte.
271  *
272  * \param rc4c The rc4 crypt context.
273  * \param buf The buffer to write the decrypted data to.
274  * \param size The size of \a buf.
275  *
276  * Read at most \a size - 1 bytes from file descriptor given by \a rc4c,
277  * decrypt the received data and write a NULL byte at the end of the decrypted
278  * data.
279  *
280  * \return The return value of the underlying call to \ref
281  * rc4_recv_bin_buffer().
282  */
283 int rc4_recv_buffer(struct rc4_context *rc4c, char *buf, size_t size)
284 {
285         int n;
286
287         assert(size);
288         n = rc4_recv_bin_buffer(rc4c, buf, size - 1);
289         if (n >= 0)
290                 buf[n] = '\0';
291         else
292                 *buf = '\0';
293         return n;
294 }