openssl: Add support for RFC4716 keys
[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 void crypt_shutdown(void)
59 {
60 CRYPTO_cleanup_all_ex_data();
61 }
62
63 /*
64 * The public key loading functions below were inspired by corresponding code
65 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
66 * Finland. However, not much of the original code remains.
67 */
68
69 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
70 {
71 const unsigned char *p = buf, *end = buf + len;
72 uint32_t bnsize;
73 BIGNUM *bn;
74
75 if (p + 4 < p)
76 return -E_BIGNUM;
77 if (p + 4 > end)
78 return -E_BIGNUM;
79 bnsize = read_u32_be(p);
80 PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
81 p += 4;
82 if (p + bnsize < p)
83 return -E_BIGNUM;
84 if (p + bnsize > end)
85 return -E_BIGNUM;
86 if (bnsize > 8192)
87 return -E_BIGNUM;
88 bn = BN_bin2bn(p, bnsize, NULL);
89 if (!bn)
90 return -E_BIGNUM;
91 *result = bn;
92 return bnsize + 4;
93 }
94
95 static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
96 {
97 int ret;
98 RSA *rsa;
99 BIGNUM *n, *e;
100 const unsigned char *p = blob, *end = blob + blen;
101
102 rsa = RSA_new();
103 if (!rsa)
104 return -E_BIGNUM;
105 ret = read_bignum(p, end - p, &e);
106 if (ret < 0)
107 goto free_rsa;
108 p += ret;
109 ret = read_bignum(p, end - p, &n);
110 if (ret < 0)
111 goto free_e;
112 #ifdef HAVE_RSA_SET0_KEY
113 RSA_set0_key(rsa, n, e, NULL);
114 #else
115 rsa->n = n;
116 rsa->e = e;
117 #endif
118 *result = rsa;
119 return 1;
120 free_e:
121 BN_free(e);
122 free_rsa:
123 RSA_free(rsa);
124 return ret;
125 }
126
127 static int read_pem_private_key(const char *path, RSA **rsa)
128 {
129 EVP_PKEY *pkey;
130 BIO *bio = BIO_new(BIO_s_file());
131
132 *rsa = NULL;
133 if (!bio)
134 return -E_PRIVATE_KEY;
135 if (BIO_read_filename(bio, path) <= 0)
136 goto bio_free;
137 pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
138 if (!pkey)
139 goto bio_free;
140 *rsa = EVP_PKEY_get1_RSA(pkey);
141 EVP_PKEY_free(pkey);
142 bio_free:
143 BIO_free(bio);
144 return *rsa? RSA_size(*rsa) : -E_PRIVATE_KEY;
145 }
146
147 static int read_private_rsa_params(const unsigned char *blob,
148 const unsigned char *end, RSA **result)
149 {
150 int ret;
151 RSA *rsa;
152 BN_CTX *ctx;
153 BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
154 BIGNUM *dmp1, *dmq1; /* these will be computed */
155 BIGNUM *tmp;
156 const unsigned char *cp = blob;
157
158 rsa = RSA_new();
159 if (!rsa)
160 return -E_BIGNUM;
161 ret = -E_BIGNUM;
162 tmp = BN_new();
163 if (!tmp)
164 goto free_rsa;
165 ctx = BN_CTX_new();
166 if (!ctx)
167 goto free_tmp;
168 dmp1 = BN_new();
169 if (!dmp1)
170 goto free_ctx;
171 dmq1 = BN_new();
172 if (!dmq1)
173 goto free_dmp1;
174 ret = read_bignum(cp, end - cp, &n);
175 if (ret < 0)
176 goto free_dmq1;
177 cp += ret;
178 ret = read_bignum(cp, end - cp, &e);
179 if (ret < 0)
180 goto free_n;
181 cp += ret;
182 ret = read_bignum(cp, end - cp, &d);
183 if (ret < 0)
184 goto free_e;
185 cp += ret;
186 ret = read_bignum(cp, end - cp, &iqmp);
187 if (ret < 0)
188 goto free_d;
189 cp += ret;
190 ret = read_bignum(cp, end - cp, &p);
191 if (ret < 0)
192 goto free_iqmp;
193 cp += ret;
194 ret = read_bignum(cp, end - cp, &q);
195 if (ret < 0)
196 goto free_p;
197 ret = -E_BIGNUM;
198 if (!BN_sub(tmp, q, BN_value_one()))
199 goto free_q;
200 if (!BN_mod(dmp1, d, tmp, ctx))
201 goto free_q;
202 if (!BN_sub(tmp, q, BN_value_one()))
203 goto free_q;
204 if (!BN_mod(dmq1, d, tmp, ctx))
205 goto free_q;
206 #ifdef HAVE_RSA_SET0_KEY
207 RSA_set0_key(rsa, n, e, d);
208 RSA_set0_factors(rsa, p, q);
209 RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp);
210 #else
211 rsa->n = n;
212 rsa->e = e;
213 rsa->d = d;
214 rsa->p = p;
215 rsa->q = q;
216 rsa->dmp1 = dmp1;
217 rsa->dmq1 = dmq1;
218 rsa->iqmp = iqmp;
219 #endif
220 *result = rsa;
221 ret = 1;
222 goto free_ctx;
223 free_q:
224 BN_clear_free(q);
225 free_p:
226 BN_clear_free(p);
227 free_iqmp:
228 BN_clear_free(iqmp);
229 free_d:
230 BN_clear_free(d);
231 free_e:
232 BN_free(e);
233 free_n:
234 BN_free(n);
235 free_dmq1:
236 BN_clear_free(dmq1);
237 free_dmp1:
238 BN_clear_free(dmp1);
239 free_ctx:
240 BN_CTX_free(ctx);
241 free_tmp:
242 BN_clear_free(tmp);
243 free_rsa:
244 if (ret < 0)
245 RSA_free(rsa);
246 return ret;
247 }
248
249 static int get_private_key(const char *path, RSA **rsa)
250 {
251 int ret;
252 unsigned char *blob, *end;
253 size_t blob_size;
254
255 *rsa = NULL;
256 ret = decode_private_key(path, &blob, &blob_size);
257 if (ret < 0)
258 return ret;
259 end = blob + blob_size;
260 if (ret == PKT_OPENSSH) {
261 ret = find_openssh_bignum_offset(blob, blob_size);
262 if (ret < 0)
263 goto free_blob;
264 PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
265 ret = read_private_rsa_params(blob + ret, end, rsa);
266 } else
267 ret = read_pem_private_key(path, rsa);
268 free_blob:
269 free(blob);
270 return ret;
271 }
272
273 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
274 {
275 unsigned char *blob;
276 size_t decoded_size;
277 int ret;
278 struct asymmetric_key *key = para_malloc(sizeof(*key));
279
280 ret = decode_public_key(key_file, &blob, &decoded_size);
281 if (ret < 0)
282 goto out;
283 ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
284 if (ret < 0)
285 goto free_blob;
286 ret = RSA_size(key->rsa);
287 assert(ret > 0);
288 *result = key;
289 free_blob:
290 free(blob);
291 out:
292 if (ret < 0) {
293 free(key);
294 *result = NULL;
295 PARA_ERROR_LOG("can not load key %s\n", key_file);
296 }
297 return ret;
298 }
299
300 void apc_free_pubkey(struct asymmetric_key *key)
301 {
302 if (!key)
303 return;
304 RSA_free(key->rsa);
305 free(key);
306 }
307
308 int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
309 unsigned char *inbuf, int inlen)
310 {
311 struct asymmetric_key *priv;
312 int ret;
313
314 ret = check_private_key_file(key_file);
315 if (ret < 0)
316 return ret;
317 if (inlen < 0)
318 return -E_RSA;
319 priv = para_malloc(sizeof(*priv));
320 ret = get_private_key(key_file, &priv->rsa);
321 if (ret < 0) {
322 free(priv);
323 return ret;
324 }
325 /*
326 * RSA is vulnerable to timing attacks. Generate a random blinding
327 * factor to protect against this kind of attack.
328 */
329 ret = -E_BLINDING;
330 if (RSA_blinding_on(priv->rsa, NULL) == 0)
331 goto out;
332 ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
333 RSA_PKCS1_OAEP_PADDING);
334 RSA_blinding_off(priv->rsa);
335 if (ret <= 0)
336 ret = -E_DECRYPT;
337 out:
338 RSA_free(priv->rsa);
339 free(priv);
340 return ret;
341 }
342
343 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
344 unsigned len, unsigned char *outbuf)
345 {
346 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
347
348 if (flen < 0)
349 return -E_ENCRYPT;
350 ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
351 RSA_PKCS1_OAEP_PADDING);
352 return ret < 0? -E_ENCRYPT : ret;
353 }
354
355 struct stream_cipher {
356 EVP_CIPHER_CTX *aes;
357 };
358
359 struct stream_cipher *sc_new(const unsigned char *data, int len)
360 {
361 struct stream_cipher *sc = para_malloc(sizeof(*sc));
362
363 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
364 sc->aes = EVP_CIPHER_CTX_new();
365 EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
366 data + AES_CRT128_BLOCK_SIZE);
367 return sc;
368 }
369
370 void sc_free(struct stream_cipher *sc)
371 {
372 if (!sc)
373 return;
374 EVP_CIPHER_CTX_free(sc->aes);
375 free(sc);
376 }
377
378 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
379 struct iovec *dst)
380 {
381 int ret, inlen = src->iov_len, outlen, tmplen;
382
383 *dst = (typeof(*dst)) {
384 /* Add one for the terminating zero byte. */
385 .iov_base = para_malloc(inlen + 1),
386 .iov_len = inlen
387 };
388 ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
389 assert(ret != 0);
390 ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
391 assert(ret != 0);
392 outlen += tmplen;
393 ((char *)dst->iov_base)[outlen] = '\0';
394 dst->iov_len = outlen;
395 }
396
397 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
398 {
399 return aes_ctr128_crypt(sc->aes, src, dst);
400 }
401
402 void hash_function(const char *data, unsigned long len, unsigned char *hash)
403 {
404 SHA_CTX c;
405 SHA1_Init(&c);
406 SHA1_Update(&c, data, len);
407 SHA1_Final(hash, &c);
408 }