]> git.tuebingen.mpg.de Git - paraslash.git/blob - crypt.c
679ba35dd07d59ec184942524f8a1bb0be786939
[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 <dirent.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <openssl/rand.h>
13 #include <openssl/err.h>
14 #include <openssl/rc4.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "string.h"
19 #include "crypt.h"
20 #include "fd.h"
21 /**
22  * Fill a buffer with random content.
23  *
24  * \param buf The buffer to fill.
25  * \param num The size of \a buf in bytes.
26  *
27  * This function puts \a num cryptographically strong pseudo-random bytes into
28  * buf. If libssl can not guarantee an unpredictable byte sequence (for example
29  * because the PRNG has not been seeded with enough randomness) the function
30  * logs an error message and calls exit().
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  * Seed pseudo random number generators.
46  *
47  * This function reads 64 bytes from /dev/urandom and adds them to the SSL
48  * PRNG. It also seeds the PRNG used by random() with a random seed obtained
49  * from SSL. If /dev/random could not be read, an error message is logged and
50  * the function calls exit().
51  *
52  * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
53  * random(3), \ref para_random().
54  */
55 void init_random_seed_or_die(void)
56 {
57         int seed, ret = RAND_load_file("/dev/urandom", 64);
58
59         if (ret != 64) {
60                 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
61                 exit(EXIT_FAILURE);
62         }
63         get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
64         srandom(seed);
65 }
66
67 static EVP_PKEY *load_key(const char *file, int private)
68 {
69         BIO *key;
70         EVP_PKEY *pkey = NULL;
71
72         key = BIO_new(BIO_s_file());
73         if (!key)
74                 return NULL;
75         if (BIO_read_filename(key, file) > 0) {
76                 if (private == LOAD_PRIVATE_KEY)
77                         pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
78                 else
79                         pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
80         }
81         BIO_free(key);
82         return pkey;
83 }
84
85 /**
86  * read an RSA key from a file
87  *
88  * \param key_file the file containing the key
89  * \param rsa RSA structure is returned here
90  * \param private if non-zero, read the private key, otherwise the public key
91  *
92  * \return The size of the RSA key on success, negative on errors.
93  *
94  * \sa openssl(1), rsa(1).
95  */
96 int get_rsa_key(char *key_file, RSA **rsa, int private)
97 {
98         EVP_PKEY *key = load_key(key_file, private);
99
100         if (!key)
101                 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
102                         : -E_PUBLIC_KEY;
103         *rsa = EVP_PKEY_get1_RSA(key);
104         EVP_PKEY_free(key);
105         if (!*rsa)
106                 return -E_RSA;
107         return RSA_size(*rsa);
108 }
109
110 /**
111  * free an RSA structure
112  *
113  * \param rsa pointer to the RSA struct to free
114  *
115  * This must be called for any key obtained by get_rsa_key().
116  */
117 void rsa_free(RSA *rsa)
118 {
119         if (rsa)
120                 RSA_free(rsa);
121 }
122
123 /**
124  * decrypt a buffer using an RSA key
125  *
126  * \param key_file full path of the rsa key
127  * \param outbuf the output buffer
128  * \param inbuf the encrypted input buffer
129  * \param rsa_inlen the length of \a inbuf
130  *
131  * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
132  *
133  * \return The size of the recovered plaintext on success, negative on errors.
134  *
135  * \sa RSA_private_decrypt(3)
136  **/
137 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
138                         unsigned rsa_inlen)
139 {
140         RSA *rsa;
141         int ret, inlen = rsa_inlen;
142
143         if (inlen < 0)
144                 return -E_RSA;
145         ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
146         if (ret < 0)
147                 return ret;
148         ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
149         rsa_free(rsa);
150         return (ret > 0)? ret : -E_DECRYPT;
151 }
152
153 /**
154  * decrypt the challenge number sent by para_server
155  *
156  * \param key_file full path of the rsa key
157  * \param challenge_nr result is stored here
158  * \param inbuf the input buffer
159  * \param rsa_inlen the length of \a inbuf
160  *
161  * \return positive on success, negative on errors
162  *
163  * \sa para_decrypt_buffer()
164  */
165 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
166                 unsigned char *inbuf, unsigned rsa_inlen)
167 {
168         unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
169         int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
170
171         if (ret >= 0) {
172                 rsa_out[ret] = '\0';
173                 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
174                         1 : -E_CHALLENGE;
175         }
176         OPENSSL_free(rsa_out);
177         return ret;
178 }
179
180 /**
181  * encrypt a buffer using an RSA key
182  *
183  * \param rsa: public rsa key
184  * \param inbuf the input buffer
185  * \param len the length of \a inbuf
186  * \param outbuf the output buffer
187  *
188  * \return The size of the encrypted data on success, negative on errors
189  *
190  * \sa RSA_public_encrypt(3)
191  */
192 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
193                 unsigned len, unsigned char *outbuf)
194 {
195         int ret, flen = len; /* RSA_public_encrypt expects a signed int */
196
197         if (flen < 0)
198                 return -E_ENCRYPT;
199         ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
200         return ret < 0?  -E_ENCRYPT : ret;
201 }
202
203 /**
204  * encrypt the given challenge number
205  *
206  * \param rsa: public rsa key
207  * \param challenge_nr the number to be encrypted
208  * \param outbuf the output buffer
209  *
210  * \a outbuf must be at least 64 bytes long
211  *
212  * \return The size of the encrypted data on success, negative on errors
213  *
214  * \sa para_encrypt_buffer()
215  *
216  */
217 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
218         unsigned char *outbuf)
219 {
220         unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
221         int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);
222         free(inbuf);
223         return ret;
224 }
225
226 /**
227  * Encrypt and send a buffer.
228  *
229  * \param rc4c The rc4 crypt context.
230  * \param buf The buffer to send.
231  * \param len The size of \a buf in bytes.
232  *
233  * \return The return value of the underyling call to write_all().
234  *
235  * \sa \ref write_all(), RC4(3).
236  */
237 int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t len)
238 {
239         int ret;
240         unsigned char *tmp;
241
242         assert(len);
243         tmp = para_malloc(len);
244         RC4(&rc4c->send_key, len, (const unsigned char *)buf, tmp);
245         ret = write_all(rc4c->fd, (char *)tmp, &len);
246         free(tmp);
247         return ret;
248 }
249
250 /**
251  * Encrypt and send a \p NULL-terminated buffer.
252  *
253  * \param rc4c The rc4 crypt context.
254  * \param buf The buffer to send.
255  *
256  * \return The return value of the underyling call to rc4_send_bin_buffer().
257  */
258 int rc4_send_buffer(struct rc4_context *rc4c, const char *buf)
259 {
260         return rc4_send_bin_buffer(rc4c, buf, strlen(buf));
261 }
262
263 /**
264  * Format, encrypt and send a buffer.
265  *
266  * \param rc4c The rc4 crypt context.
267  * \param fmt A format string.
268  *
269  * \return The return value of the underyling call to rc4_send_buffer().
270  */
271 __printf_2_3 int rc4_send_va_buffer(struct rc4_context *rc4c, const char *fmt, ...)
272 {
273         char *msg;
274         int ret;
275
276         PARA_VSPRINTF(fmt, msg);
277         ret = rc4_send_buffer(rc4c, msg);
278         free(msg);
279         return ret;
280 }
281
282 /**
283  * Receive a buffer and decrypt it.
284  *
285  * \param rc4c The rc4 crypt context.
286  * \param buf The buffer to write the decrypted data to.
287  * \param size The size of \a buf.
288  *
289  * \return The number of bytes received on success, negative on errors, zero if
290  * the peer has performed an orderly shutdown.
291  *
292  * \sa recv(2), RC4(3).
293  */
294 int rc4_recv_bin_buffer(struct rc4_context *rc4c, char *buf, size_t size)
295 {
296         unsigned char *tmp = para_malloc(size);
297         ssize_t ret = recv(rc4c->fd, tmp, size, 0);
298
299         if (ret > 0)
300                 RC4(&rc4c->recv_key, ret, tmp, (unsigned char *)buf);
301         else if (ret < 0)
302                 ret = -ERRNO_TO_PARA_ERROR(errno);
303         free(tmp);
304         return ret;
305 }
306
307 /**
308  * Receive a buffer, decrypt it and write terminating NULL byte.
309  *
310  * \param rc4c The rc4 crypt context.
311  * \param buf The buffer to write the decrypted data to.
312  * \param size The size of \a buf.
313  *
314  * Read at most \a size - 1 bytes from file descriptor given by \a rc4c,
315  * decrypt the received data and write a NULL byte at the end of the decrypted
316  * data.
317  *
318  * \return The return value of the underlying call to \ref
319  * rc4_recv_bin_buffer().
320  */
321 int rc4_recv_buffer(struct rc4_context *rc4c, char *buf, size_t size)
322 {
323         int n;
324
325         assert(size);
326         n = rc4_recv_bin_buffer(rc4c, buf, size - 1);
327         if (n >= 0)
328                 buf[n] = '\0';
329         else
330                 *buf = '\0';
331         return n;
332 }