2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file crypt.c openssl-based RSA encryption/decryption routines */
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>
22 * Fill a buffer with random content.
24 * \param buf The buffer to fill.
25 * \param num The size of \a buf in bytes.
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().
32 void get_random_bytes_or_die(unsigned char *buf, int num)
36 /* RAND_bytes() returns 1 on success, 0 otherwise. */
37 if (RAND_bytes(buf, num) == 1)
39 err = ERR_get_error();
40 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
45 * Seed pseudo random number generators.
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().
52 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
53 * random(3), \ref para_random().
55 void init_random_seed_or_die(void)
57 int seed, ret = RAND_load_file("/dev/urandom", 64);
60 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
63 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
67 static EVP_PKEY *load_key(const char *file, int private)
70 EVP_PKEY *pkey = NULL;
72 key = BIO_new(BIO_s_file());
75 if (BIO_read_filename(key, file) > 0) {
76 if (private == LOAD_PRIVATE_KEY)
77 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
79 pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
86 * read an RSA key from a file
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
92 * \return The size of the RSA key on success, negative on errors.
94 * \sa openssl(1), rsa(1).
96 int get_rsa_key(char *key_file, RSA **rsa, int private)
98 EVP_PKEY *key = load_key(key_file, private);
101 return (private == LOAD_PRIVATE_KEY)? -E_PRIVATE_KEY
103 *rsa = EVP_PKEY_get1_RSA(key);
107 return RSA_size(*rsa);
111 * free an RSA structure
113 * \param rsa pointer to the RSA struct to free
115 * This must be called for any key obtained by get_rsa_key().
117 void rsa_free(RSA *rsa)
124 * decrypt a buffer using an RSA key
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
131 * The \a outbuf must be large enough to hold at least \a rsa_inlen bytes.
133 * \return The size of the recovered plaintext on success, negative on errors.
135 * \sa RSA_private_decrypt(3)
137 int para_decrypt_buffer(char *key_file, unsigned char *outbuf, unsigned char *inbuf,
141 int ret, inlen = rsa_inlen;
145 ret = get_rsa_key(key_file, &rsa, LOAD_PRIVATE_KEY);
148 ret = RSA_private_decrypt(inlen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
150 return (ret > 0)? ret : -E_DECRYPT;
154 * decrypt the challenge number sent by para_server
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
161 * \return positive on success, negative on errors
163 * \sa para_decrypt_buffer()
165 int para_decrypt_challenge(char *key_file, long unsigned *challenge_nr,
166 unsigned char *inbuf, unsigned rsa_inlen)
168 unsigned char *rsa_out = OPENSSL_malloc(rsa_inlen + 1);
169 int ret = para_decrypt_buffer(key_file, rsa_out, inbuf, rsa_inlen);
173 ret = sscanf((char *)rsa_out, "%lu", challenge_nr) == 1?
176 OPENSSL_free(rsa_out);
181 * encrypt a buffer using an RSA key
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
188 * \return The size of the encrypted data on success, negative on errors
190 * \sa RSA_public_encrypt(3)
192 int para_encrypt_buffer(RSA *rsa, unsigned char *inbuf,
193 unsigned len, unsigned char *outbuf)
195 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
199 ret = RSA_public_encrypt(flen, inbuf, outbuf, rsa, RSA_PKCS1_PADDING);
200 return ret < 0? -E_ENCRYPT : ret;
204 * encrypt the given challenge number
206 * \param rsa: public rsa key
207 * \param challenge_nr the number to be encrypted
208 * \param outbuf the output buffer
210 * \a outbuf must be at least 64 bytes long
212 * \return The size of the encrypted data on success, negative on errors
214 * \sa para_encrypt_buffer()
217 int para_encrypt_challenge(RSA* rsa, long unsigned challenge_nr,
218 unsigned char *outbuf)
220 unsigned char *inbuf = (unsigned char*) make_message("%lu", challenge_nr);
221 int ret = para_encrypt_buffer(rsa, inbuf, strlen((char *)inbuf), outbuf);
227 * Encrypt and send a buffer.
229 * \param rc4c The rc4 crypt context.
230 * \param buf The buffer to send.
231 * \param len The size of \a buf in bytes.
233 * \return The return value of the underyling call to write_all().
235 * \sa \ref write_all(), RC4(3).
237 int rc4_send_bin_buffer(struct rc4_context *rc4c, const char *buf, size_t 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);
251 * Encrypt and send a \p NULL-terminated buffer.
253 * \param rc4c The rc4 crypt context.
254 * \param buf The buffer to send.
256 * \return The return value of the underyling call to rc4_send_bin_buffer().
258 int rc4_send_buffer(struct rc4_context *rc4c, const char *buf)
260 return rc4_send_bin_buffer(rc4c, buf, strlen(buf));
264 * Format, encrypt and send a buffer.
266 * \param rc4c The rc4 crypt context.
267 * \param fmt A format string.
269 * \return The return value of the underyling call to rc4_send_buffer().
271 __printf_2_3 int rc4_send_va_buffer(struct rc4_context *rc4c, const char *fmt, ...)
276 PARA_VSPRINTF(fmt, msg);
277 ret = rc4_send_buffer(rc4c, msg);
283 * Receive a buffer and decrypt it.
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.
289 * \return The number of bytes received on success, negative on errors, zero if
290 * the peer has performed an orderly shutdown.
292 * \sa recv(2), RC4(3).
294 int rc4_recv_bin_buffer(struct rc4_context *rc4c, char *buf, size_t size)
296 unsigned char *tmp = para_malloc(size);
297 ssize_t ret = recv(rc4c->fd, tmp, size, 0);
300 RC4(&rc4c->recv_key, ret, tmp, (unsigned char *)buf);
302 ret = -ERRNO_TO_PARA_ERROR(errno);
308 * Receive a buffer, decrypt it and write terminating NULL byte.
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.
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
318 * \return The return value of the underlying call to \ref
319 * rc4_recv_bin_buffer().
321 int rc4_recv_buffer(struct rc4_context *rc4c, char *buf, size_t size)
326 n = rc4_recv_bin_buffer(rc4c, buf, size - 1);