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>
19 #include "crypt_backend.h"
20 #include "portable_io.h"
22 struct asymmetric_key
{
26 void get_random_bytes_or_die(unsigned char *buf
, int num
)
30 /* RAND_bytes() returns 1 on success, 0 otherwise. */
31 if (RAND_bytes(buf
, num
) == 1)
33 err
= ERR_get_error();
34 PARA_EMERG_LOG("%s\n", ERR_reason_error_string(err
));
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().
43 * \sa RAND_load_file(3), \ref get_random_bytes_or_die(), srandom(3),
44 * random(3), \ref para_random().
48 int seed
, ret
= RAND_load_file("/dev/urandom", 64);
51 PARA_EMERG_LOG("could not seed PRNG (ret = %d)\n", ret
);
54 get_random_bytes_or_die((unsigned char *)&seed
, sizeof(seed
));
58 void crypt_shutdown(void)
60 #ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
61 CRYPTO_cleanup_all_ex_data();
63 #ifdef HAVE_OPENSSL_THREAD_STOP /* openssl-1.1 or later */
64 OPENSSL_thread_stop();
65 #else /* openssl-1.0 */
66 ERR_remove_thread_state(NULL
);
72 * The public key loading functions below were inspired by corresponding code
73 * of openssh-5.2p1, Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo,
74 * Finland. However, not much of the original code remains.
77 static int read_bignum(const unsigned char *buf
, size_t len
, BIGNUM
**result
)
79 const unsigned char *p
= buf
, *end
= buf
+ len
;
87 bnsize
= read_u32_be(p
);
88 PARA_DEBUG_LOG("bnsize: %u\n", bnsize
);
96 bn
= BN_bin2bn(p
, bnsize
, NULL
);
103 static int read_rsa_bignums(const unsigned char *blob
, int blen
, RSA
**result
)
108 const unsigned char *p
= blob
, *end
= blob
+ blen
;
113 ret
= read_bignum(p
, end
- p
, &e
);
117 ret
= read_bignum(p
, end
- p
, &n
);
120 #ifdef HAVE_RSA_SET0_KEY
121 RSA_set0_key(rsa
, n
, e
, NULL
);
135 static int read_pem_private_key(const char *path
, RSA
**rsa
)
138 BIO
*bio
= BIO_new(BIO_s_file());
142 return -E_PRIVATE_KEY
;
143 if (BIO_read_filename(bio
, path
) <= 0)
145 pkey
= PEM_read_bio_PrivateKey(bio
, NULL
, NULL
, NULL
);
148 *rsa
= EVP_PKEY_get1_RSA(pkey
);
152 return *rsa
? RSA_size(*rsa
) : -E_PRIVATE_KEY
;
155 static int read_private_rsa_params(const unsigned char *blob
,
156 const unsigned char *end
, RSA
**result
)
161 BIGNUM
*n
, *e
, *d
, *iqmp
, *p
, *q
; /* stored in the key file */
162 BIGNUM
*dmp1
, *dmq1
; /* these will be computed */
164 const unsigned char *cp
= blob
;
182 ret
= read_bignum(cp
, end
- cp
, &n
);
186 ret
= read_bignum(cp
, end
- cp
, &e
);
190 ret
= read_bignum(cp
, end
- cp
, &d
);
194 ret
= read_bignum(cp
, end
- cp
, &iqmp
);
198 ret
= read_bignum(cp
, end
- cp
, &p
);
202 ret
= read_bignum(cp
, end
- cp
, &q
);
206 if (!BN_sub(tmp
, q
, BN_value_one()))
208 if (!BN_mod(dmp1
, d
, tmp
, ctx
))
210 if (!BN_sub(tmp
, q
, BN_value_one()))
212 if (!BN_mod(dmq1
, d
, tmp
, ctx
))
214 #ifdef HAVE_RSA_SET0_KEY
215 RSA_set0_key(rsa
, n
, e
, d
);
216 RSA_set0_factors(rsa
, p
, q
);
217 RSA_set0_crt_params(rsa
, dmp1
, dmq1
, iqmp
);
257 static int get_private_key(const char *path
, RSA
**rsa
)
260 unsigned char *blob
, *end
;
264 ret
= decode_private_key(path
, &blob
, &blob_size
);
267 end
= blob
+ blob_size
;
268 if (ret
== PKT_OPENSSH
) {
269 ret
= find_openssh_bignum_offset(blob
, blob_size
);
272 PARA_INFO_LOG("reading RSA params at offset %d\n", ret
);
273 ret
= read_private_rsa_params(blob
+ ret
, end
, rsa
);
275 ret
= read_pem_private_key(path
, rsa
);
281 int apc_get_pubkey(const char *key_file
, struct asymmetric_key
**result
)
286 struct asymmetric_key
*key
= para_malloc(sizeof(*key
));
288 ret
= decode_public_key(key_file
, &blob
, &decoded_size
);
291 ret
= read_rsa_bignums(blob
+ ret
, decoded_size
- ret
, &key
->rsa
);
294 ret
= RSA_size(key
->rsa
);
303 PARA_ERROR_LOG("can not load key %s\n", key_file
);
308 void apc_free_pubkey(struct asymmetric_key
*key
)
316 int apc_priv_decrypt(const char *key_file
, unsigned char *outbuf
,
317 unsigned char *inbuf
, int inlen
)
319 struct asymmetric_key
*priv
;
322 ret
= check_private_key_file(key_file
);
327 priv
= para_malloc(sizeof(*priv
));
328 ret
= get_private_key(key_file
, &priv
->rsa
);
334 * RSA is vulnerable to timing attacks. Generate a random blinding
335 * factor to protect against this kind of attack.
338 if (RSA_blinding_on(priv
->rsa
, NULL
) == 0)
340 ret
= RSA_private_decrypt(inlen
, inbuf
, outbuf
, priv
->rsa
,
341 RSA_PKCS1_OAEP_PADDING
);
342 RSA_blinding_off(priv
->rsa
);
351 int apc_pub_encrypt(struct asymmetric_key
*pub
, unsigned char *inbuf
,
352 unsigned len
, unsigned char *outbuf
)
354 int ret
, flen
= len
; /* RSA_public_encrypt expects a signed int */
358 ret
= RSA_public_encrypt(flen
, inbuf
, outbuf
, pub
->rsa
,
359 RSA_PKCS1_OAEP_PADDING
);
360 return ret
< 0? -E_ENCRYPT
: ret
;
363 struct stream_cipher
{
367 struct stream_cipher
*sc_new(const unsigned char *data
, int len
)
369 struct stream_cipher
*sc
= para_malloc(sizeof(*sc
));
371 assert(len
>= 2 * AES_CRT128_BLOCK_SIZE
);
372 sc
->aes
= EVP_CIPHER_CTX_new();
373 EVP_EncryptInit_ex(sc
->aes
, EVP_aes_128_ctr(), NULL
, data
,
374 data
+ AES_CRT128_BLOCK_SIZE
);
378 void sc_free(struct stream_cipher
*sc
)
382 EVP_CIPHER_CTX_free(sc
->aes
);
386 static void aes_ctr128_crypt(EVP_CIPHER_CTX
*ctx
, struct iovec
*src
,
389 int ret
, inlen
= src
->iov_len
, outlen
, tmplen
;
391 *dst
= (typeof(*dst
)) {
392 /* Add one for the terminating zero byte. */
393 .iov_base
= para_malloc(inlen
+ 1),
396 ret
= EVP_EncryptUpdate(ctx
, dst
->iov_base
, &outlen
, src
->iov_base
, inlen
);
398 ret
= EVP_EncryptFinal_ex(ctx
, dst
->iov_base
+ outlen
, &tmplen
);
401 ((char *)dst
->iov_base
)[outlen
] = '\0';
402 dst
->iov_len
= outlen
;
405 void sc_crypt(struct stream_cipher
*sc
, struct iovec
*src
, struct iovec
*dst
)
407 return aes_ctr128_crypt(sc
->aes
, src
, dst
);
410 void hash_function(const char *data
, unsigned long len
, unsigned char *hash
)
414 SHA1_Update(&c
, data
, len
);
415 SHA1_Final(hash
, &c
);