1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file openssl.c Openssl-based encryption/decryption routines. */
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 #include <openssl/evp.h>
20 #include "crypt_backend.h"
21 #include "portable_io.h"
23 struct asymmetric_key {
27 void get_random_bytes_or_die(unsigned char *buf, int num)
31 /* RAND_bytes() returns 1 on success, 0 otherwise. */
32 if (RAND_bytes(buf, num) == 1)
34 err = ERR_get_error();
35 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err));
40 * Read 64 bytes from /dev/urandom and add them to the SSL PRNG. Then seed the
41 * PRNG used by random(3) with a random seed obtained from SSL.
45 int seed, ret = RAND_load_file("/dev/urandom", 64);
48 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret);
51 get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
55 void crypt_shutdown(void)
57 #ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
58 CRYPTO_cleanup_all_ex_data();
60 #ifdef HAVE_OPENSSL_THREAD_STOP /* openssl-1.1 or later */
61 OPENSSL_thread_stop();
62 #else /* openssl-1.0 */
63 ERR_remove_thread_state(NULL);
69 * The public key loading functions below were inspired by corresponding code
70 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
71 * Finland. However, not much of the original code remains.
74 static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
76 const unsigned char *p = buf, *end = buf + len;
84 bnsize = read_u32_be(p);
85 PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
93 bn = BN_bin2bn(p, bnsize, NULL);
100 static int read_public_key(const unsigned char *blob, int blen,
101 struct asymmetric_key *result)
106 const unsigned char *p = blob, *end = blob + blen;
111 ret = read_bignum(p, end - p, &e);
115 ret = read_bignum(p, end - p, &n);
118 #ifdef HAVE_RSA_SET0_KEY
119 RSA_set0_key(rsa, n, e, NULL);
133 static int read_pem_private_key(const char *path, struct asymmetric_key *priv)
136 BIO *bio = BIO_new(BIO_s_file());
140 return -E_PRIVATE_KEY;
141 if (BIO_read_filename(bio, path) <= 0)
143 pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
146 priv->rsa = EVP_PKEY_get1_RSA(pkey);
150 return priv->rsa? RSA_size(priv->rsa) : -E_PRIVATE_KEY;
153 static int read_openssh_private_key(const unsigned char *blob,
154 const unsigned char *end, struct asymmetric_key *priv)
158 BIGNUM *n, *e, *d, *iqmp, *p, *q; /* stored in the key file */
159 const unsigned char *cp = blob;
164 ret = read_bignum(cp, end - cp, &n);
168 ret = read_bignum(cp, end - cp, &e);
172 ret = read_bignum(cp, end - cp, &d);
176 ret = read_bignum(cp, end - cp, &iqmp);
180 ret = read_bignum(cp, end - cp, &p);
184 ret = read_bignum(cp, end - cp, &q);
187 #ifdef HAVE_RSA_SET0_KEY
188 RSA_set0_key(rsa, n, e, d);
189 RSA_set0_factors(rsa, p, q);
190 RSA_set0_crt_params(rsa, NULL, NULL, iqmp);
217 static int get_private_key(const char *path, struct asymmetric_key *priv)
220 unsigned char *blob, *end;
224 ret = decode_private_key(path, &blob, &blob_size);
227 end = blob + blob_size;
228 if (ret == PKT_OPENSSH) {
229 ret = find_openssh_bignum_offset(blob, blob_size);
232 PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
233 ret = read_openssh_private_key(blob + ret, end, priv);
235 ret = read_pem_private_key(path, priv);
241 int apc_get_pubkey(const char *key_file, struct asymmetric_key **result)
246 struct asymmetric_key *pub = alloc(sizeof(*pub));
248 ret = decode_public_key(key_file, &blob, &decoded_size);
251 ret = read_public_key(blob + ret, decoded_size - ret, pub);
254 ret = RSA_size(pub->rsa);
263 PARA_ERROR_LOG("can not load key %s\n", key_file);
268 void apc_free_pubkey(struct asymmetric_key *pub)
276 int apc_priv_decrypt(const char *key_file, unsigned char *outbuf,
277 unsigned char *inbuf, int inlen)
279 struct asymmetric_key *priv;
282 ret = check_private_key_file(key_file);
287 priv = alloc(sizeof(*priv));
288 ret = get_private_key(key_file, priv);
294 * RSA is vulnerable to timing attacks. Generate a random blinding
295 * factor to protect against this kind of attack.
298 if (RSA_blinding_on(priv->rsa, NULL) == 0)
300 ret = RSA_private_decrypt(inlen, inbuf, outbuf, priv->rsa,
301 RSA_PKCS1_OAEP_PADDING);
302 RSA_blinding_off(priv->rsa);
311 int apc_pub_encrypt(struct asymmetric_key *pub, unsigned char *inbuf,
312 unsigned len, unsigned char *outbuf)
314 int ret, flen = len; /* RSA_public_encrypt expects a signed int */
318 ret = RSA_public_encrypt(flen, inbuf, outbuf, pub->rsa,
319 RSA_PKCS1_OAEP_PADDING);
320 return ret < 0? -E_ENCRYPT : ret;
323 struct stream_cipher {
327 struct stream_cipher *sc_new(const unsigned char *data, int len)
329 struct stream_cipher *sc = alloc(sizeof(*sc));
331 assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
332 sc->aes = EVP_CIPHER_CTX_new();
333 EVP_EncryptInit_ex(sc->aes, EVP_aes_128_ctr(), NULL, data,
334 data + AES_CRT128_BLOCK_SIZE);
338 void sc_free(struct stream_cipher *sc)
342 EVP_CIPHER_CTX_free(sc->aes);
346 static void aes_ctr128_crypt(EVP_CIPHER_CTX *ctx, struct iovec *src,
349 int ret, inlen = src->iov_len, outlen, tmplen;
351 *dst = (typeof(*dst)) {
352 /* Add one for the terminating zero byte. */
353 .iov_base = alloc(inlen + 1),
356 ret = EVP_EncryptUpdate(ctx, dst->iov_base, &outlen, src->iov_base, inlen);
358 ret = EVP_EncryptFinal_ex(ctx, dst->iov_base + outlen, &tmplen);
361 ((char *)dst->iov_base)[outlen] = '\0';
362 dst->iov_len = outlen;
365 void sc_crypt(struct stream_cipher *sc, struct iovec *src, struct iovec *dst)
367 return aes_ctr128_crypt(sc->aes, src, dst);
370 void hash_function(const char *data, unsigned long len, unsigned char *hash)
372 EVP_MD_CTX *c = EVP_MD_CTX_new();
373 int ret = EVP_DigestInit_ex(c, EVP_sha1(), NULL);
375 ret = EVP_DigestUpdate(c, data, len);
377 ret = EVP_DigestFinal_ex(c, hash, NULL);
382 void hash2_function(const char *data, unsigned long len, unsigned char *hash)
384 EVP_MD_CTX *c = EVP_MD_CTX_new();
385 int ret = EVP_DigestInit_ex(c, EVP_sha256(), NULL);
387 ret = EVP_DigestUpdate(c, data, len);
389 ret = EVP_DigestFinal_ex(c, hash, NULL);