crypto: Remove support for ASN public keys.
[paraslash.git] / crypt.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file crypt.c Openssl-based encryption/decryption routines. */
8
9 #include <regex.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 #include <openssl/pem.h>
16 #include <openssl/sha.h>
17 #include <openssl/bn.h>
18 #include <openssl/aes.h>
19
20 #include "para.h"
21 #include "error.h"
22 #include "string.h"
23 #include "crypt.h"
24 #include "fd.h"
25 #include "crypt_backend.h"
26 #include "base64.h"
27
28 struct asymmetric_key {
29 RSA *rsa;
30 };
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 * Read 64 bytes from /dev/urandom and adds them to the SSL PRNG. Seed the PRNG
46 * used by random() with a random seed obtained from SSL. If /dev/random is not
47 * readable the function calls exit().
48 *
49 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
50 * random(3), \ref para_random().
51 */
52 void init_random_seed_or_die(void)
53 {
54 int seed, ret = RAND_load_file("/dev/urandom", 64);
55
56 if (ret != 64) {
57 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
58 exit(EXIT_FAILURE);
59 }
60 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
61 srandom(seed);
62 }
63
64 static EVP_PKEY *load_key(const char *file)
65 {
66 BIO *key;
67 EVP_PKEY *pkey = NULL;
68
69 key = BIO_new(BIO_s_file());
70 if (!key)
71 return NULL;
72 if (BIO_read_filename(key, file) > 0)
73 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
74 BIO_free(key);
75 return pkey;
76 }
77
78 static int get_private_key(const char *key_file, RSA **rsa)
79 {
80 EVP_PKEY *key = load_key(key_file);
81
82 if (!key)
83 return -E_PRIVATE_KEY;
84 *rsa = EVP_PKEY_get1_RSA(key);
85 EVP_PKEY_free(key);
86 if (!*rsa)
87 return -E_RSA;
88 return RSA_size(*rsa);
89 }
90
91 /*
92 * The public key loading functions below were inspired by corresponding code
93 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
94 * Finland. However, not much of the original code remains.
95 */
96
97 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
98 {
99 const unsigned char *p = buf, *end = buf + len;
100 uint32_t bnsize;
101 BIGNUM *bn;
102
103 if (p + 4 < p)
104 return -E_BIGNUM;
105 if (p + 4 > end)
106 return -E_BIGNUM;
107 bnsize = read_ssh_u32(p);
108 PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
109 p += 4;
110 if (p + bnsize < p)
111 return -E_BIGNUM;
112 if (p + bnsize > end)
113 return -E_BIGNUM;
114 if (bnsize > 8192)
115 return -E_BIGNUM;
116 bn = BN_bin2bn(p, bnsize, NULL);
117 if (!bn)
118 return -E_BIGNUM;
119 *result = bn;
120 return bnsize + 4;
121 }
122
123 static int read_rsa_bignums(const unsigned char *blob, int blen, RSA **result)
124 {
125 int ret;
126 RSA *rsa;
127 BIGNUM *n, *e;
128 const unsigned char *p = blob, *end = blob + blen;
129
130 rsa = RSA_new();
131 if (!rsa)
132 return -E_BIGNUM;
133 ret = read_bignum(p, end - p, &e);
134 if (ret < 0)
135 goto fail;
136 p += ret;
137 ret = read_bignum(p, end - p, &n);
138 if (ret < 0)
139 goto fail;
140 #ifdef HAVE_RSA_SET0_KEY
141 RSA_set0_key(rsa, n, e, NULL);
142 #else
143 rsa->n = n;
144 rsa->e = e;
145 #endif
146 *result = rsa;
147 return 1;
148 fail:
149 RSA_free(rsa);
150 return ret;
151 }
152
153 int get_public_key(const char *key_file, struct asymmetric_key **result)
154 {
155 struct asymmetric_key *key = NULL;
156 void *map = NULL;
157 unsigned char *blob = NULL;
158 size_t map_size, encoded_size, decoded_size;
159 int ret, ret2;
160 char *cp;
161
162 key = para_malloc(sizeof(*key));
163 ret = mmap_full_file(key_file, O_RDONLY, &map, &map_size, NULL);
164 if (ret < 0)
165 goto out;
166 ret = is_ssh_rsa_key(map, map_size);
167 if (!ret) {
168 para_munmap(map, map_size);
169 return -E_SSH_PARSE;
170 }
171 cp = map + ret;
172 encoded_size = map_size - ret;
173 PARA_INFO_LOG("decoding public rsa-ssh key %s\n", key_file);
174 ret = uudecode(cp, encoded_size, (char **)&blob, &decoded_size);
175 if (ret < 0)
176 goto out_unmap;
177 ret = check_ssh_key_header(blob, decoded_size);
178 if (ret < 0)
179 goto out_unmap;
180 ret = read_rsa_bignums(blob + ret, decoded_size - ret, &key->rsa);
181 if (ret < 0)
182 goto out_unmap;
183 ret = RSA_size(key->rsa);
184 out_unmap:
185 ret2 = para_munmap(map, map_size);
186 if (ret >= 0 && ret2 < 0)
187 ret = ret2;
188 out:
189 if (ret < 0) {
190 free(key);
191 *result = NULL;
192 PARA_ERROR_LOG("key %s: %s\n", key_file, para_strerror(-ret));
193 } else
194 *result = key;
195 free(blob);
196 return ret;
197 }
198
199 void free_public_key(struct asymmetric_key *key)
200 {
201 if (!key)
202 return;
203 RSA_free(key->rsa);
204 free(key);
205 }
206
207 int priv_decrypt(const char *key_file, unsigned char *outbuf,
208 unsigned char *inbuf, int inlen)
209 {
210 struct asymmetric_key *priv;
211 int ret;
212
213 ret = check_private_key_file(key_file);
214 if (ret < 0)
215 return ret;
216 if (inlen < 0)
217 return -E_RSA;
218 priv = para_malloc(sizeof(*priv));
219 ret = get_private_key(key_file, &priv->rsa);
220 if (ret < 0) {
221 free(priv);
222 return ret;
223 }
224 /*
225 * RSA is vulnerable to timing attacks. Generate a random blinding
226 * factor to protect against this kind of attack.
227 */
228 ret = -E_BLINDING;
229 if (RSA_blinding_on(priv->rsa, NULL) == 0)
230 goto out;
231 ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
232 RSA_PKCS1_OAEP_PADDING);
233 RSA_blinding_off(priv->rsa);
234 if (ret <= 0)
235 ret = -E_DECRYPT;
236 out:
237 RSA_free(priv->rsa);
238 free(priv);
239 return ret;
240 }
241
242 int pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
243 unsigned len, unsigned char *outbuf)
244 {
245 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
246
247 if (flen < 0)
248 return -E_ENCRYPT;
249 ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
250 RSA_PKCS1_OAEP_PADDING);
251 return ret < 0? -E_ENCRYPT : ret;
252 }
253
254 struct stream_cipher {
255 bool use_aes;
256 union {
257 RC4_KEY rc4_key;
258 EVP_CIPHER_CTX *aes;
259 } context;
260 };
261
262 struct stream_cipher *sc_new(const unsigned char *data, int len,
263 bool use_aes)
264 {
265 struct stream_cipher *sc = para_malloc(sizeof(*sc));
266
267 sc->use_aes = use_aes;
268 if (!use_aes) {
269 RC4_set_key(&sc->context.rc4_key, len, data);
270 return sc;
271 }
272 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
273 sc->context.aes = EVP_CIPHER_CTX_new();
274 EVP_EncryptInit_ex(sc->context.aes, EVP_aes_128_ctr(), NULL, data,
275 data + AES_CRT128_BLOCK_SIZE);
276 return sc;
277 }
278
279 void sc_free(struct stream_cipher *sc)
280 {
281 if (!sc)
282 return;
283 EVP_CIPHER_CTX_free(sc->context.aes);
284 free(sc);
285 }
286
287 /**
288 * The RC4() implementation of openssl apparently reads and writes data in
289 * blocks of 8 bytes. So we have to make sure our buffer sizes are a multiple
290 * of this.
291 */
292 #define RC4_ALIGN 8
293
294 static void rc4_crypt(RC4_KEY *key, struct iovec *src, struct iovec *dst)
295 {
296 size_t len = src->iov_len, l1, l2;
297
298 assert(len > 0);
299 assert(len < ((typeof(src->iov_len))-1) / 2);
300 l1 = ROUND_DOWN(len, RC4_ALIGN);
301 l2 = ROUND_UP(len, RC4_ALIGN);
302
303 *dst = (typeof(*dst)) {
304 /* Add one for the terminating zero byte. */
305 .iov_base = para_malloc(l2 + 1),
306 .iov_len = len
307 };
308 RC4(key, l1, src->iov_base, dst->iov_base);
309 if (len > l1) {
310 unsigned char remainder[RC4_ALIGN] = "";
311 memcpy(remainder, src->iov_base + l1, len - l1);
312 RC4(key, len - l1, remainder, dst->iov_base + l1);
313 }
314 ((char *)dst->iov_base)[len] = '\0';
315 }
316
317 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
318 struct iovec *dst)
319 {
320 int ret, inlen = src->iov_len, outlen, tmplen;
321
322 *dst = (typeof(*dst)) {
323 /* Add one for the terminating zero byte. */
324 .iov_base = para_malloc(inlen + 1),
325 .iov_len = inlen
326 };
327 ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
328 assert(ret != 0);
329 ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
330 assert(ret != 0);
331 outlen += tmplen;
332 ((char *)dst->iov_base)[outlen] = '\0';
333 dst->iov_len = outlen;
334 }
335
336 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
337 {
338 if (sc->use_aes)
339 return aes_ctr128_crypt(sc->context.aes, src, dst);
340 return rc4_crypt(&sc->context.rc4_key, src, dst);
341 }
342
343 void hash_function(const char *data, unsigned long len, unsigned char *hash)
344 {
345 SHA_CTX c;
346 SHA1_Init(&c);
347 SHA1_Update(&c, data, len);
348 SHA1_Final(hash, &c);
349 }