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