99b3f7a6c3258bc5ef9981f062388a5a7d2854ba
[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
15 #include "para.h"
16 #include "error.h"
17 #include "string.h"
18 #include "crypt.h"
19 #include "crypt_backend.h"
20 #include "portable_io.h"
21
22 struct asymmetric_key {
23 RSA *rsa;
24 };
25
26 void get_random_bytes_or_die(unsigned char *buf, int num)
27 {
28 unsigned long err;
29
30 /* RAND_bytes() returns 1 on success, 0 otherwise. */
31 if (RAND_bytes(buf, num) == 1)
32 return;
33 err = ERR_get_error();
34 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
35 exit(EXIT_FAILURE);
36 }
37
38 /*
39 * Read 64 bytes from /dev/urandom and add them to the SSL PRNG. Seed the PRNG
40 * used by random(3) with a random seed obtained from SSL. If /dev/urandom is
41 * not readable, the function calls exit().
42 *
43 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
44 * random(3), \ref para_random().
45 */
46 void crypt_init(void)
47 {
48 int seed, ret = RAND_load_file("/dev/urandom", 64);
49
50 if (ret != 64) {
51 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
52 exit(EXIT_FAILURE);
53 }
54 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
55 srandom(seed);
56 }
57
58 static int get_private_key(const char *path, RSA **rsa)
59 {
60 EVP_PKEY *pkey;
61 BIO *bio = BIO_new(BIO_s_file());
62
63 *rsa = NULL;
64 if (!bio)
65 return -E_PRIVATE_KEY;
66 if (BIO_read_filename(bio, path) <= 0)
67 goto bio_free;
68 pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
69 if (!pkey)
70 goto bio_free;
71 *rsa = EVP_PKEY_get1_RSA(pkey);
72 EVP_PKEY_free(pkey);
73 bio_free:
74 BIO_free(bio);
75 return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
76 }
77
78 /*
79 * The public key loading functions below were inspired by corresponding code
80 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
81 * Finland. However, not much of the original code remains.
82 */
83
84 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
85 {
86 const unsigned char *p = buf, *end = buf + len;
87 uint32_t bnsize;
88 BIGNUM *bn;
89
90 if (p + 4 < p)
91 return -E_BIGNUM;
92 if (p + 4 > end)
93 return -E_BIGNUM;
94 bnsize = read_u32_be(p);
95 PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
96 p += 4;
97 if (p + bnsize < p)
98 return -E_BIGNUM;
99 if (p + bnsize > end)
100 return -E_BIGNUM;
101 if (bnsize > 8192)
102 return -E_BIGNUM;
103 bn = BN_bin2bn(p, bnsize, NULL);
104 if (!bn)
105 return -E_BIGNUM;
106 *result = bn;
107 return bnsize + 4;
108 }
109
110 static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
111 {
112 int ret;
113 RSA *rsa;
114 BIGNUM *n, *e;
115 const unsigned char *p = blob, *end = blob + blen;
116
117 rsa = RSA_new();
118 if (!rsa)
119 return -E_BIGNUM;
120 ret = read_bignum(p, end - p, &e);
121 if (ret < 0)
122 goto fail;
123 p += ret;
124 ret = read_bignum(p, end - p, &n);
125 if (ret < 0)
126 goto fail;
127 #ifdef HAVE_RSA_SET0_KEY
128 RSA_set0_key(rsa, n, e, NULL);
129 #else
130 rsa->n = n;
131 rsa->e = e;
132 #endif
133 *result = rsa;
134 return 1;
135 fail:
136 RSA_free(rsa);
137 return ret;
138 }
139
140 int get_public_key(const char *key_file, struct asymmetric_key **result)
141 {
142 unsigned char *blob;
143 size_t decoded_size;
144 int ret;
145 struct asymmetric_key *key = para_malloc(sizeof(*key));
146
147 ret = decode_ssh_key(key_file, &blob, &decoded_size);
148 if (ret < 0)
149 goto out;
150 ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
151 if (ret < 0)
152 goto free_blob;
153 ret = RSA_size(key->rsa);
154 assert(ret > 0);
155 *result = key;
156 free_blob:
157 free(blob);
158 out:
159 if (ret < 0) {
160 free(key);
161 *result = NULL;
162 PARA_ERROR_LOG("can not load key %s\n", key_file);
163 }
164 return ret;
165 }
166
167 void free_public_key(struct asymmetric_key *key)
168 {
169 if (!key)
170 return;
171 RSA_free(key->rsa);
172 free(key);
173 }
174
175 int priv_decrypt(const char *key_file, unsigned char *outbuf,
176 unsigned char *inbuf, int inlen)
177 {
178 struct asymmetric_key *priv;
179 int ret;
180
181 ret = check_private_key_file(key_file);
182 if (ret < 0)
183 return ret;
184 if (inlen < 0)
185 return -E_RSA;
186 priv = para_malloc(sizeof(*priv));
187 ret = get_private_key(key_file, &priv->rsa);
188 if (ret < 0) {
189 free(priv);
190 return ret;
191 }
192 /*
193 * RSA is vulnerable to timing attacks. Generate a random blinding
194 * factor to protect against this kind of attack.
195 */
196 ret = -E_BLINDING;
197 if (RSA_blinding_on(priv->rsa, NULL) == 0)
198 goto out;
199 ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
200 RSA_PKCS1_OAEP_PADDING);
201 RSA_blinding_off(priv->rsa);
202 if (ret <= 0)
203 ret = -E_DECRYPT;
204 out:
205 RSA_free(priv->rsa);
206 free(priv);
207 return ret;
208 }
209
210 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
211 unsigned len, unsigned char *outbuf)
212 {
213 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
214
215 if (flen < 0)
216 return -E_ENCRYPT;
217 ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
218 RSA_PKCS1_OAEP_PADDING);
219 return ret < 0? -E_ENCRYPT : ret;
220 }
221
222 struct stream_cipher {
223 EVP_CIPHER_CTX *aes;
224 };
225
226 struct stream_cipher *sc_new(const unsigned char *data, int len)
227 {
228 struct stream_cipher *sc = para_malloc(sizeof(*sc));
229
230 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
231 sc->aes = EVP_CIPHER_CTX_new();
232 EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
233 data + AES_CRT128_BLOCK_SIZE);
234 return sc;
235 }
236
237 void sc_free(struct stream_cipher *sc)
238 {
239 if (!sc)
240 return;
241 EVP_CIPHER_CTX_free(sc->aes);
242 free(sc);
243 }
244
245 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
246 struct iovec *dst)
247 {
248 int ret, inlen = src->iov_len, outlen, tmplen;
249
250 *dst = (typeof(*dst)) {
251 /* Add one for the terminating zero byte. */
252 .iov_base = para_malloc(inlen + 1),
253 .iov_len = inlen
254 };
255 ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
256 assert(ret != 0);
257 ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
258 assert(ret != 0);
259 outlen += tmplen;
260 ((char *)dst->iov_base)[outlen] = '\0';
261 dst->iov_len = outlen;
262 }
263
264 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
265 {
266 return aes_ctr128_crypt(sc->aes, src, dst);
267 }
268
269 void hash_function(const char *data, unsigned long len, unsigned char *hash)
270 {
271 SHA_CTX c;
272 SHA1_Init(&c);
273 SHA1_Update(&c, data, len);
274 SHA1_Final(hash, &c);
275 }