2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001, Matej Pfajfar.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2013-01-16 07:54:56 +01:00
|
|
|
* Copyright (c) 2007-2013, The Tor Project, Inc. */
|
2002-07-24 16:02:39 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/**
|
|
|
|
* \file crypto.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Wrapper functions to present a consistent interface to
|
|
|
|
* public-key and symmetric cryptography operations from OpenSSL.
|
2004-05-10 05:53:24 +02:00
|
|
|
**/
|
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#include "orconfig.h"
|
2004-03-11 07:19:08 +01:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2011-07-15 16:03:59 +02:00
|
|
|
#ifndef _WIN32_WINNT
|
2012-05-14 19:46:37 +02:00
|
|
|
#define _WIN32_WINNT 0x0501
|
2011-07-15 16:03:59 +02:00
|
|
|
#endif
|
2004-04-28 22:13:21 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <wincrypt.h>
|
2009-05-27 23:55:51 +02:00
|
|
|
/* Windows defines this; so does OpenSSL 0.9.8h and later. We don't actually
|
2008-06-28 06:16:17 +02:00
|
|
|
* use either definition. */
|
|
|
|
#undef OCSP_RESPONSE
|
2004-04-28 22:13:21 +02:00
|
|
|
#endif
|
|
|
|
|
2002-08-22 09:30:03 +02:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/evp.h>
|
2009-05-24 01:42:44 +02:00
|
|
|
#include <openssl/engine.h>
|
2002-08-22 09:30:03 +02:00
|
|
|
#include <openssl/rand.h>
|
2002-09-03 21:16:02 +02:00
|
|
|
#include <openssl/opensslv.h>
|
2003-05-01 02:53:46 +02:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dh.h>
|
2005-10-25 21:01:48 +02:00
|
|
|
#include <openssl/conf.h>
|
2007-11-01 04:56:17 +01:00
|
|
|
#include <openssl/hmac.h>
|
2002-07-24 16:02:39 +02:00
|
|
|
|
2004-04-03 06:05:12 +02:00
|
|
|
#ifdef HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
2004-04-26 20:09:50 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FCNTL_H
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
2004-04-03 06:05:12 +02:00
|
|
|
|
2007-05-01 00:42:50 +02:00
|
|
|
#define CRYPTO_PRIVATE
|
2002-08-22 09:30:03 +02:00
|
|
|
#include "crypto.h"
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "../common/torlog.h"
|
2003-06-30 21:18:32 +02:00
|
|
|
#include "aes.h"
|
2009-09-29 06:44:39 +02:00
|
|
|
#include "../common/util.h"
|
2004-11-01 21:41:47 +01:00
|
|
|
#include "container.h"
|
2005-02-13 23:32:25 +01:00
|
|
|
#include "compat.h"
|
2013-08-09 18:07:20 +02:00
|
|
|
#include "sandbox.h"
|
2002-08-22 09:30:03 +02:00
|
|
|
|
2012-09-13 01:25:58 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER < OPENSSL_V_SERIES(0,9,8)
|
|
|
|
#error "We require OpenSSL >= 0.9.8"
|
2002-09-03 21:16:02 +02:00
|
|
|
#endif
|
|
|
|
|
2009-09-29 06:46:53 +02:00
|
|
|
#ifdef ANDROID
|
|
|
|
/* Android's OpenSSL seems to have removed all of its Engine support. */
|
|
|
|
#define DISABLE_ENGINES
|
|
|
|
#endif
|
|
|
|
|
2012-01-10 06:14:35 +01:00
|
|
|
/** Longest recognized */
|
|
|
|
#define MAX_DNS_LABEL_SIZE 63
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Macro: is k a valid RSA public or private key? */
|
2004-04-25 21:21:44 +02:00
|
|
|
#define PUBLIC_KEY_OK(k) ((k) && (k)->key && (k)->key->n)
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Macro: is k a valid RSA private key? */
|
2004-04-25 21:21:44 +02:00
|
|
|
#define PRIVATE_KEY_OK(k) ((k) && (k)->key && (k)->key->p)
|
|
|
|
|
2005-03-23 07:20:50 +01:00
|
|
|
#ifdef TOR_IS_MULTITHREADED
|
2009-05-27 23:55:51 +02:00
|
|
|
/** A number of preallocated mutexes for use by OpenSSL. */
|
2012-10-12 18:22:13 +02:00
|
|
|
static tor_mutex_t **openssl_mutexes_ = NULL;
|
2009-05-27 23:55:51 +02:00
|
|
|
/** How many mutexes have we allocated for use by OpenSSL? */
|
2012-10-12 18:22:13 +02:00
|
|
|
static int n_openssl_mutexes_ = 0;
|
2005-03-23 07:20:50 +01:00
|
|
|
#endif
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** A public key, or a public/private key-pair. */
|
2012-01-18 21:53:30 +01:00
|
|
|
struct crypto_pk_t
|
2003-09-10 02:47:24 +02:00
|
|
|
{
|
2011-03-16 22:05:37 +01:00
|
|
|
int refs; /**< reference count, so we don't have to copy keys */
|
|
|
|
RSA *key; /**< The key itself */
|
2003-09-10 02:47:24 +02:00
|
|
|
};
|
|
|
|
|
2005-10-06 06:33:40 +02:00
|
|
|
/** Key and stream information for a stream cipher. */
|
2012-01-18 21:53:30 +01:00
|
|
|
struct crypto_cipher_t
|
2003-09-10 02:47:24 +02:00
|
|
|
{
|
2011-03-16 22:05:37 +01:00
|
|
|
char key[CIPHER_KEY_LEN]; /**< The raw key. */
|
2012-03-20 20:35:43 +01:00
|
|
|
char iv[CIPHER_IV_LEN]; /**< The initial IV. */
|
2011-03-16 22:05:37 +01:00
|
|
|
aes_cnt_cipher_t *cipher; /**< The key in format usable for counter-mode AES
|
|
|
|
* encryption */
|
2003-09-10 02:47:24 +02:00
|
|
|
};
|
|
|
|
|
2005-10-06 06:33:40 +02:00
|
|
|
/** A structure to hold the first half (x, g^x) of a Diffie-Hellman handshake
|
|
|
|
* while we're waiting for the second.*/
|
2012-01-18 21:53:30 +01:00
|
|
|
struct crypto_dh_t {
|
2011-03-16 22:05:37 +01:00
|
|
|
DH *dh; /**< The openssl DH object */
|
2004-04-03 04:40:30 +02:00
|
|
|
};
|
2003-06-13 23:13:37 +02:00
|
|
|
|
2005-02-13 23:32:25 +01:00
|
|
|
static int setup_openssl_threading(void);
|
2009-10-26 07:47:05 +01:00
|
|
|
static int tor_check_dh_key(int severity, BIGNUM *bn);
|
2005-02-13 23:32:25 +01:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Return the number of bytes added by padding method <b>padding</b>.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
static INLINE int
|
2005-09-30 03:09:52 +02:00
|
|
|
crypto_get_rsa_padding_overhead(int padding)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (padding)
|
2003-03-19 21:41:15 +01:00
|
|
|
{
|
2013-02-24 05:03:24 +01:00
|
|
|
case RSA_PKCS1_OAEP_PADDING: return PKCS1_OAEP_PADDING_OVERHEAD;
|
2004-04-25 21:59:38 +02:00
|
|
|
default: tor_assert(0); return -1;
|
2003-03-19 21:41:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Given a padding method <b>padding</b>, return the correct OpenSSL constant.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2003-08-12 05:08:41 +02:00
|
|
|
static INLINE int
|
2005-09-30 03:09:52 +02:00
|
|
|
crypto_get_rsa_padding(int padding)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (padding)
|
2004-04-01 05:08:35 +02:00
|
|
|
{
|
2004-04-03 04:40:30 +02:00
|
|
|
case PK_PKCS1_OAEP_PADDING: return RSA_PKCS1_OAEP_PADDING;
|
2004-04-25 21:59:38 +02:00
|
|
|
default: tor_assert(0); return -1;
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:56:29 +01:00
|
|
|
/** Boolean: has OpenSSL's crypto been initialized? */
|
|
|
|
static int crypto_early_initialized_ = 0;
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Boolean: has OpenSSL's crypto been initialized? */
|
2012-10-12 18:22:13 +02:00
|
|
|
static int crypto_global_initialized_ = 0;
|
2003-09-15 21:38:52 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Log all pending crypto errors at level <b>severity</b>. Use
|
|
|
|
* <b>doing</b> to describe our current activities.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2004-04-26 20:09:50 +02:00
|
|
|
static void
|
|
|
|
crypto_log_errors(int severity, const char *doing)
|
|
|
|
{
|
2008-02-21 22:57:42 +01:00
|
|
|
unsigned long err;
|
2004-04-26 20:09:50 +02:00
|
|
|
const char *msg, *lib, *func;
|
|
|
|
while ((err = ERR_get_error()) != 0) {
|
|
|
|
msg = (const char*)ERR_reason_error_string(err);
|
|
|
|
lib = (const char*)ERR_lib_error_string(err);
|
|
|
|
func = (const char*)ERR_func_error_string(err);
|
|
|
|
if (!msg) msg = "(null)";
|
2006-10-20 02:12:02 +02:00
|
|
|
if (!lib) lib = "(null)";
|
|
|
|
if (!func) func = "(null)";
|
2004-04-26 20:09:50 +02:00
|
|
|
if (doing) {
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_CRYPTO, "crypto error while %s: %s (in %s:%s)",
|
2013-02-01 22:19:02 +01:00
|
|
|
doing, msg, lib, func);
|
2004-04-26 20:09:50 +02:00
|
|
|
} else {
|
2013-02-01 22:19:02 +01:00
|
|
|
tor_log(severity, LD_CRYPTO, "crypto error: %s (in %s:%s)",
|
|
|
|
msg, lib, func);
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-10 05:53:24 +02:00
|
|
|
|
2009-09-29 06:46:53 +02:00
|
|
|
#ifndef DISABLE_ENGINES
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Log any OpenSSL engines we're using at NOTICE. */
|
2005-06-20 20:56:35 +02:00
|
|
|
static void
|
|
|
|
log_engine(const char *fn, ENGINE *e)
|
|
|
|
{
|
|
|
|
if (e) {
|
|
|
|
const char *name, *id;
|
|
|
|
name = ENGINE_get_name(e);
|
|
|
|
id = ENGINE_get_id(e);
|
2013-12-18 17:49:44 +01:00
|
|
|
log_notice(LD_CRYPTO, "Default OpenSSL engine for %s is %s [%s]",
|
|
|
|
fn, name?name:"?", id?id:"?");
|
2005-06-20 20:56:35 +02:00
|
|
|
} else {
|
2013-02-01 21:43:37 +01:00
|
|
|
log_info(LD_CRYPTO, "Using default implementation for %s", fn);
|
2005-06-20 20:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
2009-09-29 06:46:53 +02:00
|
|
|
#endif
|
2005-06-20 20:56:35 +02:00
|
|
|
|
2009-09-29 06:46:53 +02:00
|
|
|
#ifndef DISABLE_ENGINES
|
2009-05-24 01:42:44 +02:00
|
|
|
/** Try to load an engine in a shared library via fully qualified path.
|
|
|
|
*/
|
|
|
|
static ENGINE *
|
|
|
|
try_load_engine(const char *path, const char *engine)
|
|
|
|
{
|
|
|
|
ENGINE *e = ENGINE_by_id("dynamic");
|
|
|
|
if (e) {
|
|
|
|
if (!ENGINE_ctrl_cmd_string(e, "ID", engine, 0) ||
|
|
|
|
!ENGINE_ctrl_cmd_string(e, "DIR_LOAD", "2", 0) ||
|
|
|
|
!ENGINE_ctrl_cmd_string(e, "DIR_ADD", path, 0) ||
|
|
|
|
!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) {
|
|
|
|
ENGINE_free(e);
|
|
|
|
e = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
2009-09-29 06:46:53 +02:00
|
|
|
#endif
|
2009-05-24 01:42:44 +02:00
|
|
|
|
2013-09-04 03:56:06 +02:00
|
|
|
/* Returns a trimmed and human-readable version of an openssl version string
|
|
|
|
* <b>raw_version</b>. They are usually in the form of 'OpenSSL 1.0.0b 10
|
|
|
|
* May 2012' and this will parse them into a form similar to '1.0.0b' */
|
2013-09-01 18:38:01 +02:00
|
|
|
static char *
|
|
|
|
parse_openssl_version_str(const char *raw_version)
|
|
|
|
{
|
|
|
|
const char *end_of_version = NULL;
|
|
|
|
/* The output should be something like "OpenSSL 1.0.0b 10 May 2012. Let's
|
|
|
|
trim that down. */
|
|
|
|
if (!strcmpstart(raw_version, "OpenSSL ")) {
|
|
|
|
raw_version += strlen("OpenSSL ");
|
|
|
|
end_of_version = strchr(raw_version, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_of_version)
|
|
|
|
return tor_strndup(raw_version,
|
|
|
|
end_of_version-raw_version);
|
|
|
|
else
|
|
|
|
return tor_strdup(raw_version);
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:32:38 +02:00
|
|
|
static char *crypto_openssl_version_str = NULL;
|
|
|
|
/* Return a human-readable version of the run-time openssl version number. */
|
|
|
|
const char *
|
|
|
|
crypto_openssl_get_version_str(void)
|
|
|
|
{
|
|
|
|
if (crypto_openssl_version_str == NULL) {
|
|
|
|
const char *raw_version = SSLeay_version(SSLEAY_VERSION);
|
2013-09-01 18:38:01 +02:00
|
|
|
crypto_openssl_version_str = parse_openssl_version_str(raw_version);
|
2012-09-04 18:32:38 +02:00
|
|
|
}
|
|
|
|
return crypto_openssl_version_str;
|
|
|
|
}
|
|
|
|
|
2013-09-04 03:56:06 +02:00
|
|
|
static char *crypto_openssl_header_version_str = NULL;
|
2013-09-01 18:38:01 +02:00
|
|
|
/* Return a human-readable version of the compile-time openssl version
|
|
|
|
* number. */
|
|
|
|
const char *
|
|
|
|
crypto_openssl_get_header_version_str(void)
|
|
|
|
{
|
2013-09-04 03:56:06 +02:00
|
|
|
if (crypto_openssl_header_version_str == NULL) {
|
|
|
|
crypto_openssl_header_version_str =
|
|
|
|
parse_openssl_version_str(OPENSSL_VERSION_TEXT);
|
|
|
|
}
|
|
|
|
return crypto_openssl_header_version_str;
|
2013-09-01 18:38:01 +02:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:56:29 +01:00
|
|
|
/** Make sure that openssl is using its default PRNG. Return 1 if we had to
|
|
|
|
* adjust it; 0 otherwise. */
|
|
|
|
static int
|
|
|
|
crypto_force_rand_ssleay(void)
|
|
|
|
{
|
|
|
|
if (RAND_get_rand_method() != RAND_SSLeay()) {
|
|
|
|
log_notice(LD_CRYPTO, "It appears that one of our engines has provided "
|
|
|
|
"a replacement the OpenSSL RNG. Resetting it to the default "
|
|
|
|
"implementation.");
|
|
|
|
RAND_set_rand_method(RAND_SSLeay());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:46:58 +01:00
|
|
|
/** Set up the siphash key if we haven't already done so. */
|
|
|
|
int
|
|
|
|
crypto_init_siphash_key(void)
|
|
|
|
{
|
|
|
|
static int have_seeded_siphash = 0;
|
|
|
|
struct sipkey key;
|
|
|
|
if (have_seeded_siphash)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (crypto_rand((char*) &key, sizeof(key)) < 0)
|
|
|
|
return -1;
|
|
|
|
siphash_set_global_key(&key);
|
|
|
|
have_seeded_siphash = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize the crypto library. Return 0 on success, -1 on failure.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2005-06-20 20:56:35 +02:00
|
|
|
int
|
2014-02-12 17:56:29 +01:00
|
|
|
crypto_early_init(void)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2014-02-12 17:56:29 +01:00
|
|
|
if (!crypto_early_initialized_) {
|
2014-03-23 05:38:17 +01:00
|
|
|
|
|
|
|
crypto_early_initialized_ = 1;
|
|
|
|
|
2005-06-20 20:56:35 +02:00
|
|
|
ERR_load_crypto_strings();
|
|
|
|
OpenSSL_add_all_algorithms();
|
2014-02-12 17:56:29 +01:00
|
|
|
|
2005-06-20 20:56:35 +02:00
|
|
|
setup_openssl_threading();
|
2012-09-04 18:41:37 +02:00
|
|
|
|
|
|
|
if (SSLeay() == OPENSSL_VERSION_NUMBER &&
|
|
|
|
!strcmp(SSLeay_version(SSLEAY_VERSION), OPENSSL_VERSION_TEXT)) {
|
|
|
|
log_info(LD_CRYPTO, "OpenSSL version matches version from headers "
|
|
|
|
"(%lx: %s).", SSLeay(), SSLeay_version(SSLEAY_VERSION));
|
|
|
|
} else {
|
|
|
|
log_warn(LD_CRYPTO, "OpenSSL version from headers does not match the "
|
|
|
|
"version we're running with. If you get weird crashes, that "
|
|
|
|
"might be why. (Compiled with %lx: %s; running with %lx: %s).",
|
|
|
|
(unsigned long)OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_TEXT,
|
|
|
|
SSLeay(), SSLeay_version(SSLEAY_VERSION));
|
|
|
|
}
|
|
|
|
|
2012-09-13 01:31:24 +02:00
|
|
|
if (SSLeay() < OPENSSL_V_SERIES(1,0,0)) {
|
2012-10-10 09:48:36 +02:00
|
|
|
log_notice(LD_CRYPTO,
|
|
|
|
"Your OpenSSL version seems to be %s. We recommend 1.0.0 "
|
|
|
|
"or later.",
|
|
|
|
crypto_openssl_get_version_str());
|
2012-09-13 01:31:24 +02:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:56:29 +01:00
|
|
|
crypto_force_rand_ssleay();
|
|
|
|
|
|
|
|
if (crypto_seed_rng(1) < 0)
|
|
|
|
return -1;
|
2014-02-12 17:46:58 +01:00
|
|
|
if (crypto_init_siphash_key() < 0)
|
|
|
|
return -1;
|
2014-02-12 17:56:29 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize the crypto library. Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_global_init(int useAccel, const char *accelName, const char *accelDir)
|
|
|
|
{
|
|
|
|
if (!crypto_global_initialized_) {
|
|
|
|
crypto_early_init();
|
|
|
|
|
|
|
|
crypto_global_initialized_ = 1;
|
|
|
|
|
2006-05-24 02:37:38 +02:00
|
|
|
if (useAccel > 0) {
|
2009-09-29 06:46:53 +02:00
|
|
|
#ifdef DISABLE_ENGINES
|
|
|
|
(void)accelName;
|
|
|
|
(void)accelDir;
|
|
|
|
log_warn(LD_CRYPTO, "No OpenSSL hardware acceleration support enabled.");
|
|
|
|
#else
|
2009-05-24 01:42:44 +02:00
|
|
|
ENGINE *e = NULL;
|
2009-09-29 06:46:53 +02:00
|
|
|
|
2006-02-13 09:01:59 +01:00
|
|
|
log_info(LD_CRYPTO, "Initializing OpenSSL engine support.");
|
2005-06-20 20:56:35 +02:00
|
|
|
ENGINE_load_builtin_engines();
|
2009-05-24 01:42:44 +02:00
|
|
|
ENGINE_register_all_complete();
|
2009-09-29 06:46:53 +02:00
|
|
|
|
2009-05-24 01:42:44 +02:00
|
|
|
if (accelName) {
|
|
|
|
if (accelDir) {
|
|
|
|
log_info(LD_CRYPTO, "Trying to load dynamic OpenSSL engine \"%s\""
|
|
|
|
" via path \"%s\".", accelName, accelDir);
|
|
|
|
e = try_load_engine(accelName, accelDir);
|
|
|
|
} else {
|
|
|
|
log_info(LD_CRYPTO, "Initializing dynamic OpenSSL engine \"%s\""
|
|
|
|
" acceleration support.", accelName);
|
|
|
|
e = ENGINE_by_id(accelName);
|
|
|
|
}
|
|
|
|
if (!e) {
|
|
|
|
log_warn(LD_CRYPTO, "Unable to load dynamic OpenSSL engine \"%s\".",
|
|
|
|
accelName);
|
|
|
|
} else {
|
|
|
|
log_info(LD_CRYPTO, "Loaded dynamic OpenSSL engine \"%s\".",
|
|
|
|
accelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e) {
|
|
|
|
log_info(LD_CRYPTO, "Loaded OpenSSL hardware acceleration engine,"
|
|
|
|
" setting default ciphers.");
|
|
|
|
ENGINE_set_default(e, ENGINE_METHOD_ALL);
|
|
|
|
}
|
2013-11-18 17:12:24 +01:00
|
|
|
/* Log, if available, the intersection of the set of algorithms
|
|
|
|
used by Tor and the set of algorithms available in the engine */
|
2005-06-20 20:56:35 +02:00
|
|
|
log_engine("RSA", ENGINE_get_default_RSA());
|
|
|
|
log_engine("DH", ENGINE_get_default_DH());
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("ECDH", ENGINE_get_default_ECDH());
|
|
|
|
log_engine("ECDSA", ENGINE_get_default_ECDSA());
|
2005-06-20 20:56:35 +02:00
|
|
|
log_engine("RAND", ENGINE_get_default_RAND());
|
2013-12-18 17:49:44 +01:00
|
|
|
log_engine("RAND (which we will not use)", ENGINE_get_default_RAND());
|
2005-06-20 20:56:35 +02:00
|
|
|
log_engine("SHA1", ENGINE_get_digest_engine(NID_sha1));
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("3DES-CBC", ENGINE_get_cipher_engine(NID_des_ede3_cbc));
|
|
|
|
log_engine("AES-128-ECB", ENGINE_get_cipher_engine(NID_aes_128_ecb));
|
|
|
|
log_engine("AES-128-CBC", ENGINE_get_cipher_engine(NID_aes_128_cbc));
|
2013-11-18 17:23:54 +01:00
|
|
|
#ifdef NID_aes_128_ctr
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("AES-128-CTR", ENGINE_get_cipher_engine(NID_aes_128_ctr));
|
2013-11-18 17:23:54 +01:00
|
|
|
#endif
|
|
|
|
#ifdef NID_aes_128_gcm
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("AES-128-GCM", ENGINE_get_cipher_engine(NID_aes_128_gcm));
|
2013-11-18 17:23:54 +01:00
|
|
|
#endif
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("AES-256-CBC", ENGINE_get_cipher_engine(NID_aes_256_cbc));
|
2013-11-18 17:23:54 +01:00
|
|
|
#ifdef NID_aes_256_gcm
|
2013-11-18 17:12:24 +01:00
|
|
|
log_engine("AES-256-GCM", ENGINE_get_cipher_engine(NID_aes_256_gcm));
|
2013-11-18 17:23:54 +01:00
|
|
|
#endif
|
2013-11-18 17:12:24 +01:00
|
|
|
|
2009-09-29 06:46:53 +02:00
|
|
|
#endif
|
2009-05-24 01:42:44 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_CRYPTO, "NOT using OpenSSL engine support.");
|
2005-06-20 20:56:35 +02:00
|
|
|
}
|
2011-11-21 03:20:31 +01:00
|
|
|
|
2014-02-12 17:56:29 +01:00
|
|
|
if (crypto_force_rand_ssleay()) {
|
|
|
|
if (crypto_seed_rng(1) < 0)
|
|
|
|
return -1;
|
2013-12-18 17:49:44 +01:00
|
|
|
}
|
|
|
|
|
2011-11-21 03:20:31 +01:00
|
|
|
evaluate_evp_for_aes(-1);
|
2012-01-09 23:40:11 +01:00
|
|
|
evaluate_ctr_for_aes();
|
2003-09-15 21:38:52 +02:00
|
|
|
}
|
2002-07-24 16:02:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-25 21:01:48 +02:00
|
|
|
/** Free crypto resources held by this thread. */
|
|
|
|
void
|
|
|
|
crypto_thread_cleanup(void)
|
|
|
|
{
|
|
|
|
ERR_remove_state(0);
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
/** used by tortls.c: wrap an RSA* in a crypto_pk_t. */
|
|
|
|
crypto_pk_t *
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_new_pk_from_rsa_(RSA *rsa)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *env;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(rsa);
|
2012-01-18 21:53:30 +01:00
|
|
|
env = tor_malloc(sizeof(crypto_pk_t));
|
2002-09-24 12:43:57 +02:00
|
|
|
env->refs = 1;
|
2004-04-03 04:40:30 +02:00
|
|
|
env->key = rsa;
|
2003-09-10 02:47:24 +02:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2009-09-29 06:48:45 +02:00
|
|
|
/** Helper, used by tor-checkkey.c and tor-gencert.c. Return the RSA from a
|
2012-01-18 21:53:30 +01:00
|
|
|
* crypto_pk_t. */
|
2008-05-09 10:35:38 +02:00
|
|
|
RSA *
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_pk_get_rsa_(crypto_pk_t *env)
|
2008-05-09 10:35:38 +02:00
|
|
|
{
|
|
|
|
return env->key;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
/** used by tortls.c: get an equivalent EVP_PKEY* for a crypto_pk_t. Iff
|
2004-05-01 22:46:28 +02:00
|
|
|
* private is set, include the private-key portion of the key. */
|
2005-09-30 03:39:24 +02:00
|
|
|
EVP_PKEY *
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_pk_get_evp_pkey_(crypto_pk_t *env, int private)
|
2003-09-11 23:12:39 +02:00
|
|
|
{
|
|
|
|
RSA *key = NULL;
|
|
|
|
EVP_PKEY *pkey = NULL;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(env->key);
|
2004-04-25 00:17:50 +02:00
|
|
|
if (private) {
|
|
|
|
if (!(key = RSAPrivateKey_dup(env->key)))
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
if (!(key = RSAPublicKey_dup(env->key)))
|
|
|
|
goto error;
|
|
|
|
}
|
2003-09-11 23:12:39 +02:00
|
|
|
if (!(pkey = EVP_PKEY_new()))
|
|
|
|
goto error;
|
|
|
|
if (!(EVP_PKEY_assign_RSA(pkey, key)))
|
|
|
|
goto error;
|
|
|
|
return pkey;
|
|
|
|
error:
|
|
|
|
if (pkey)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
if (key)
|
|
|
|
RSA_free(key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
/** Used by tortls.c: Get the DH* from a crypto_dh_t.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2005-09-30 03:39:24 +02:00
|
|
|
DH *
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_dh_get_dh_(crypto_dh_t *dh)
|
2004-04-03 04:40:30 +02:00
|
|
|
{
|
|
|
|
return dh->dh;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Allocate and return storage for a public key. The key itself will not yet
|
2004-05-01 22:46:28 +02:00
|
|
|
* be set.
|
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *
|
|
|
|
crypto_pk_new(void)
|
2003-09-10 02:47:24 +02:00
|
|
|
{
|
|
|
|
RSA *rsa;
|
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
rsa = RSA_new();
|
2011-01-26 00:19:09 +01:00
|
|
|
tor_assert(rsa);
|
2012-10-12 18:22:13 +02:00
|
|
|
return crypto_new_pk_from_rsa_(rsa);
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Release a reference to an asymmetric key; when all the references
|
2004-05-01 23:41:23 +02:00
|
|
|
* are released, free the key.
|
2004-05-01 22:46:28 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(crypto_pk_t *env)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!env)
|
|
|
|
return;
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (--env->refs > 0)
|
2002-09-24 12:43:57 +02:00
|
|
|
return;
|
2010-10-26 18:22:04 +02:00
|
|
|
tor_assert(env->refs == 0);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
if (env->key)
|
|
|
|
RSA_free(env->key);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(env);
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
/** Allocate and return a new symmetric cipher using the provided key and iv.
|
|
|
|
* The key is CIPHER_KEY_LEN bytes; the IV is CIPHER_IV_LEN bytes. If you
|
|
|
|
* provide NULL in place of either one, it is generated at random.
|
2002-10-02 22:39:51 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_t *
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_new_with_iv(const char *key, const char *iv)
|
2002-10-02 22:39:51 +02:00
|
|
|
{
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_t *env;
|
2002-10-02 22:39:51 +02:00
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
env = tor_malloc_zero(sizeof(crypto_cipher_t));
|
2002-10-02 22:39:51 +02:00
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
if (key == NULL)
|
|
|
|
crypto_rand(env->key, CIPHER_KEY_LEN);
|
|
|
|
else
|
|
|
|
memcpy(env->key, key, CIPHER_KEY_LEN);
|
|
|
|
if (iv == NULL)
|
|
|
|
crypto_rand(env->iv, CIPHER_IV_LEN);
|
2002-10-02 22:39:51 +02:00
|
|
|
else
|
2012-03-20 20:35:43 +01:00
|
|
|
memcpy(env->iv, iv, CIPHER_IV_LEN);
|
2002-10-02 22:39:51 +02:00
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
env->cipher = aes_new_cipher(env->key, env->iv);
|
2002-10-02 22:39:51 +02:00
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
return env;
|
2002-10-02 22:39:51 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/** Return a new crypto_cipher_t with the provided <b>key</b> and an IV of all
|
|
|
|
* zero bytes. */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_t *
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_new(const char *key)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2012-03-20 20:35:43 +01:00
|
|
|
char zeroiv[CIPHER_IV_LEN];
|
|
|
|
memset(zeroiv, 0, sizeof(zeroiv));
|
|
|
|
return crypto_cipher_new_with_iv(key, zeroiv);
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Free a symmetric cipher.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_free(crypto_cipher_t *env)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!env)
|
|
|
|
return;
|
2003-03-19 21:41:15 +01:00
|
|
|
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(env->cipher);
|
2012-01-18 21:53:30 +01:00
|
|
|
aes_cipher_free(env->cipher);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(env, 0, sizeof(crypto_cipher_t));
|
2004-04-03 04:40:30 +02:00
|
|
|
tor_free(env);
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* public key crypto */
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2009-09-29 06:48:45 +02:00
|
|
|
/** Generate a <b>bits</b>-bit new public/private keypair in <b>env</b>.
|
|
|
|
* Return 0 on success, -1 on failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(env);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
Fix a dangling pointer issue in our RSA keygen code
If OpenSSL fails to generate an RSA key, do not retain a dangling
pointer to the previous (uninitialized) key value. The impact here
should be limited to a difficult-to-trigger crash, if OpenSSL is
running an engine that makes key generation failures possible, or if
OpenSSL runs out of memory. Fixes bug 19152; bugfix on
0.2.1.10-alpha. Found by Yuan Jochen Kang, Suman Jana, and Baishakhi
Ray.
This is potentially scary stuff, so let me walk through my analysis.
I think this is a bug, and a backport candidate, but not remotely
triggerable in any useful way.
Observation 1a:
Looking over the OpenSSL code here, the only way we can really fail in
the non-engine case is if malloc() fails. But if malloc() is failing,
then tor_malloc() calls should be tor_asserting -- the only way that an
attacker could do an exploit here would be to figure out some way to
make malloc() fail when openssl does it, but work whenever Tor does it.
(Also ordinary malloc() doesn't fail on platforms like Linux that
overcommit.)
Observation 1b:
Although engines are _allowed_ to fail in extra ways, I can't find much
evidence online that they actually _do_ fail in practice. More evidence
would be nice, though.
Observation 2:
We don't call crypto_pk_generate*() all that often, and we don't do it
in response to external inputs. The only way to get it to happen
remotely would be by causing a hidden service to build new introduction
points.
Observation 3a:
So, let's assume that both of the above observations are wrong, and the
attacker can make us generate a crypto_pk_env_t with a dangling pointer
in its 'key' field, and not immediately crash.
This dangling pointer will point to what used to be an RSA structure,
with the fields all set to NULL. Actually using this RSA structure,
before the memory is reused for anything else, will cause a crash.
In nearly every function where we call crypto_pk_generate*(), we quickly
use the RSA key pointer -- either to sign something, or to encode the
key, or to free the key. The only exception is when we generate an
intro key in rend_consider_services_intro_points(). In that case, we
don't actually use the key until the intro circuit is opened -- at which
point we encode it, and use it to sign an introduction request.
So in order to exploit this bug to do anything besides crash Tor, the
attacker needs to make sure that by the time the introduction circuit
completes, either:
* the e, d, and n BNs look valid, and at least one of the other BNs is
still NULL.
OR
* all 8 of the BNs must look valid.
To look like a valid BN, *they* all need to have their 'top' index plus
their 'd' pointer indicate an addressable region in memory.
So actually getting useful data of of this, rather than a crash, is
going to be pretty damn hard. You'd have to force an introduction point
to be created (or wait for one to be created), and force that particular
crypto_pk_generate*() to fail, and then arrange for the memory that the
RSA points to to in turn point to 3...8 valid BNs, all by the time the
introduction circuit completes.
Naturally, the signature won't check as valid [*], so the intro point
will reject the ESTABLISH_INTRO cell. So you need to _be_ the
introduction point, or you don't actually see this information.
[*] Okay, so if you could somehow make the 'rsa' pointer point to a
different valid RSA key, then you'd get a valid signature of an
ESTABLISH_INTRO cell using a key that was supposed to be used for
something else ... but nothing else looks like that, so you can't use
that signature elsewhere.
Observation 3b:
Your best bet as an attacker would be to make the dangling RSA pointer
actually contain a fake method, with a fake RSA_private_encrypt
function that actually pointed to code you wanted to execute. You'd
still need to transit 3 or 4 pointers deep though in order to make that
work.
Conclusion:
By 1, you probably can't trigger this without Tor crashing from OOM.
By 2, you probably can't trigger this reliably.
By 3, even if I'm wrong about 1 and 2, you have to jump through a pretty
big array of hoops in order to get any kind of data leak or code
execution.
So I'm calling it a bug, but not a security hole. Still worth
patching.
2016-05-20 19:58:52 +02:00
|
|
|
if (env->key) {
|
2004-04-03 04:40:30 +02:00
|
|
|
RSA_free(env->key);
|
Fix a dangling pointer issue in our RSA keygen code
If OpenSSL fails to generate an RSA key, do not retain a dangling
pointer to the previous (uninitialized) key value. The impact here
should be limited to a difficult-to-trigger crash, if OpenSSL is
running an engine that makes key generation failures possible, or if
OpenSSL runs out of memory. Fixes bug 19152; bugfix on
0.2.1.10-alpha. Found by Yuan Jochen Kang, Suman Jana, and Baishakhi
Ray.
This is potentially scary stuff, so let me walk through my analysis.
I think this is a bug, and a backport candidate, but not remotely
triggerable in any useful way.
Observation 1a:
Looking over the OpenSSL code here, the only way we can really fail in
the non-engine case is if malloc() fails. But if malloc() is failing,
then tor_malloc() calls should be tor_asserting -- the only way that an
attacker could do an exploit here would be to figure out some way to
make malloc() fail when openssl does it, but work whenever Tor does it.
(Also ordinary malloc() doesn't fail on platforms like Linux that
overcommit.)
Observation 1b:
Although engines are _allowed_ to fail in extra ways, I can't find much
evidence online that they actually _do_ fail in practice. More evidence
would be nice, though.
Observation 2:
We don't call crypto_pk_generate*() all that often, and we don't do it
in response to external inputs. The only way to get it to happen
remotely would be by causing a hidden service to build new introduction
points.
Observation 3a:
So, let's assume that both of the above observations are wrong, and the
attacker can make us generate a crypto_pk_env_t with a dangling pointer
in its 'key' field, and not immediately crash.
This dangling pointer will point to what used to be an RSA structure,
with the fields all set to NULL. Actually using this RSA structure,
before the memory is reused for anything else, will cause a crash.
In nearly every function where we call crypto_pk_generate*(), we quickly
use the RSA key pointer -- either to sign something, or to encode the
key, or to free the key. The only exception is when we generate an
intro key in rend_consider_services_intro_points(). In that case, we
don't actually use the key until the intro circuit is opened -- at which
point we encode it, and use it to sign an introduction request.
So in order to exploit this bug to do anything besides crash Tor, the
attacker needs to make sure that by the time the introduction circuit
completes, either:
* the e, d, and n BNs look valid, and at least one of the other BNs is
still NULL.
OR
* all 8 of the BNs must look valid.
To look like a valid BN, *they* all need to have their 'top' index plus
their 'd' pointer indicate an addressable region in memory.
So actually getting useful data of of this, rather than a crash, is
going to be pretty damn hard. You'd have to force an introduction point
to be created (or wait for one to be created), and force that particular
crypto_pk_generate*() to fail, and then arrange for the memory that the
RSA points to to in turn point to 3...8 valid BNs, all by the time the
introduction circuit completes.
Naturally, the signature won't check as valid [*], so the intro point
will reject the ESTABLISH_INTRO cell. So you need to _be_ the
introduction point, or you don't actually see this information.
[*] Okay, so if you could somehow make the 'rsa' pointer point to a
different valid RSA key, then you'd get a valid signature of an
ESTABLISH_INTRO cell using a key that was supposed to be used for
something else ... but nothing else looks like that, so you can't use
that signature elsewhere.
Observation 3b:
Your best bet as an attacker would be to make the dangling RSA pointer
actually contain a fake method, with a fake RSA_private_encrypt
function that actually pointed to code you wanted to execute. You'd
still need to transit 3 or 4 pointers deep though in order to make that
work.
Conclusion:
By 1, you probably can't trigger this without Tor crashing from OOM.
By 2, you probably can't trigger this reliably.
By 3, even if I'm wrong about 1 and 2, you have to jump through a pretty
big array of hoops in order to get any kind of data leak or code
execution.
So I'm calling it a bug, but not a security hole. Still worth
patching.
2016-05-20 19:58:52 +02:00
|
|
|
env->key = NULL;
|
|
|
|
}
|
2012-09-13 01:25:58 +02:00
|
|
|
|
2008-12-29 03:20:57 +01:00
|
|
|
{
|
|
|
|
BIGNUM *e = BN_new();
|
|
|
|
RSA *r = NULL;
|
|
|
|
if (!e)
|
|
|
|
goto done;
|
|
|
|
if (! BN_set_word(e, 65537))
|
|
|
|
goto done;
|
|
|
|
r = RSA_new();
|
|
|
|
if (!r)
|
|
|
|
goto done;
|
2009-09-29 06:48:45 +02:00
|
|
|
if (RSA_generate_key_ex(r, bits, e, NULL) == -1)
|
2008-12-29 03:20:57 +01:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
env->key = r;
|
|
|
|
r = NULL;
|
|
|
|
done:
|
|
|
|
if (e)
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(e);
|
2008-12-29 03:20:57 +01:00
|
|
|
if (r)
|
|
|
|
RSA_free(r);
|
2012-09-13 01:25:58 +02:00
|
|
|
}
|
|
|
|
|
2004-04-26 20:09:50 +02:00
|
|
|
if (!env->key) {
|
|
|
|
crypto_log_errors(LOG_WARN, "generating RSA key");
|
2002-07-25 10:17:22 +02:00
|
|
|
return -1;
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2002-07-24 16:02:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-10 18:07:34 +01:00
|
|
|
/** Read a PEM-encoded private key from the <b>len</b>-byte string <b>s</b>
|
|
|
|
* into <b>env</b>. Return 0 on success, -1 on failure. If len is -1,
|
|
|
|
* the string is nul-terminated.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2007-06-13 20:15:53 +02:00
|
|
|
/* Used here, and used for testing. */
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_read_private_key_from_string(crypto_pk_t *env,
|
2011-01-10 18:07:34 +01:00
|
|
|
const char *s, ssize_t len)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-09-21 06:55:43 +02:00
|
|
|
BIO *b;
|
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(s);
|
2011-04-26 19:00:46 +02:00
|
|
|
tor_assert(len < INT_MAX && len < SSIZE_T_CEILING);
|
2004-09-21 06:55:43 +02:00
|
|
|
|
2011-01-10 18:07:34 +01:00
|
|
|
/* Create a read-only memory BIO, backed by the string 's' */
|
|
|
|
b = BIO_new_mem_buf((char*)s, (int)len);
|
2011-01-26 00:26:49 +01:00
|
|
|
if (!b)
|
|
|
|
return -1;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
if (env->key)
|
|
|
|
RSA_free(env->key);
|
2004-09-21 06:55:43 +02:00
|
|
|
|
|
|
|
env->key = PEM_read_bio_RSAPrivateKey(b,NULL,NULL,NULL);
|
|
|
|
|
|
|
|
BIO_free(b);
|
|
|
|
|
2004-04-26 20:09:50 +02:00
|
|
|
if (!env->key) {
|
2004-09-21 06:55:43 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "Error parsing private key");
|
2002-07-25 10:17:22 +02:00
|
|
|
return -1;
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
2002-07-24 16:02:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-22 09:30:03 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Read a PEM-encoded private key from the file named by
|
|
|
|
* <b>keyfile</b> into <b>env</b>. Return 0 on success, -1 on failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_read_private_key_from_filename(crypto_pk_t *env,
|
2005-12-14 21:40:40 +01:00
|
|
|
const char *keyfile)
|
2002-08-22 09:30:03 +02:00
|
|
|
{
|
2004-09-21 06:55:43 +02:00
|
|
|
char *contents;
|
|
|
|
int r;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-09-21 06:55:43 +02:00
|
|
|
/* Read the file into a string. */
|
2006-10-20 01:05:02 +02:00
|
|
|
contents = read_file_to_str(keyfile, 0, NULL);
|
2004-09-21 06:55:43 +02:00
|
|
|
if (!contents) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Error reading private key from \"%s\"", keyfile);
|
2003-09-27 00:27:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-09-21 06:55:43 +02:00
|
|
|
/* Try to parse it. */
|
2011-01-10 18:07:34 +01:00
|
|
|
r = crypto_pk_read_private_key_from_string(env, contents, -1);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(contents, 0, strlen(contents));
|
2004-09-21 06:55:43 +02:00
|
|
|
tor_free(contents);
|
|
|
|
if (r)
|
|
|
|
return -1; /* read_private_key_from_string already warned, so we don't.*/
|
|
|
|
|
|
|
|
/* Make sure it's valid. */
|
2004-04-26 20:09:50 +02:00
|
|
|
if (crypto_pk_check_key(env) <= 0)
|
2002-07-25 10:17:22 +02:00
|
|
|
return -1;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2002-07-24 16:02:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-25 10:17:22 +02:00
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
/** Helper function to implement crypto_pk_write_*_key_to_string. */
|
|
|
|
static int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_write_key_to_string_impl(crypto_pk_t *env, char **dest,
|
2008-08-08 16:36:11 +02:00
|
|
|
size_t *len, int is_public)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2002-09-24 12:43:57 +02:00
|
|
|
BUF_MEM *buf;
|
2003-12-16 06:29:04 +01:00
|
|
|
BIO *b;
|
2008-08-08 16:36:11 +02:00
|
|
|
int r;
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
|
|
|
tor_assert(dest);
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
|
2011-01-26 00:26:49 +01:00
|
|
|
if (!b)
|
|
|
|
return -1;
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
/* Now you can treat b as if it were a file. Just use the
|
|
|
|
* PEM_*_bio_* functions instead of the non-bio variants.
|
|
|
|
*/
|
2008-08-08 16:36:11 +02:00
|
|
|
if (is_public)
|
|
|
|
r = PEM_write_bio_RSAPublicKey(b, env->key);
|
|
|
|
else
|
|
|
|
r = PEM_write_bio_RSAPrivateKey(b, env->key, NULL,NULL,0,NULL,NULL);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
crypto_log_errors(LOG_WARN, "writing RSA key to string");
|
2008-02-05 20:40:26 +01:00
|
|
|
BIO_free(b);
|
2004-04-03 04:40:30 +02:00
|
|
|
return -1;
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
BIO_get_mem_ptr(b, &buf);
|
2006-08-31 20:47:54 +02:00
|
|
|
(void)BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */
|
2004-04-03 04:40:30 +02:00
|
|
|
BIO_free(b);
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
*dest = tor_malloc(buf->length+1);
|
|
|
|
memcpy(*dest, buf->data, buf->length);
|
2006-07-15 21:21:30 +02:00
|
|
|
(*dest)[buf->length] = 0; /* nul terminate it */
|
2004-04-03 04:40:30 +02:00
|
|
|
*len = buf->length;
|
|
|
|
BUF_MEM_free(buf);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2002-09-24 12:43:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
/** PEM-encode the public key portion of <b>env</b> and write it to a
|
|
|
|
* newly allocated string. On success, set *<b>dest</b> to the new
|
|
|
|
* string, *<b>len</b> to the string's length, and return 0. On
|
|
|
|
* failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_write_public_key_to_string(crypto_pk_t *env, char **dest,
|
2008-08-08 16:36:11 +02:00
|
|
|
size_t *len)
|
|
|
|
{
|
|
|
|
return crypto_pk_write_key_to_string_impl(env, dest, len, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** PEM-encode the private key portion of <b>env</b> and write it to a
|
|
|
|
* newly allocated string. On success, set *<b>dest</b> to the new
|
|
|
|
* string, *<b>len</b> to the string's length, and return 0. On
|
|
|
|
* failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_write_private_key_to_string(crypto_pk_t *env, char **dest,
|
2008-08-08 16:36:11 +02:00
|
|
|
size_t *len)
|
|
|
|
{
|
|
|
|
return crypto_pk_write_key_to_string_impl(env, dest, len, 0);
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Read a PEM-encoded public key from the first <b>len</b> characters of
|
|
|
|
* <b>src</b>, and store the result in <b>env</b>. Return 0 on success, -1 on
|
2004-05-01 23:41:23 +02:00
|
|
|
* failure.
|
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_read_public_key_from_string(crypto_pk_t *env, const char *src,
|
2005-12-14 21:40:40 +01:00
|
|
|
size_t len)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2003-12-16 06:29:04 +01:00
|
|
|
BIO *b;
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(src);
|
2008-02-21 22:57:42 +01:00
|
|
|
tor_assert(len<INT_MAX);
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
|
2011-01-26 00:26:49 +01:00
|
|
|
if (!b)
|
|
|
|
return -1;
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2008-02-21 22:57:42 +01:00
|
|
|
BIO_write(b, src, (int)len);
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
if (env->key)
|
|
|
|
RSA_free(env->key);
|
|
|
|
env->key = PEM_read_bio_RSAPublicKey(b, NULL, NULL, NULL);
|
2004-04-12 07:27:38 +02:00
|
|
|
BIO_free(b);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!env->key) {
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "reading public key from string");
|
2004-04-03 04:40:30 +02:00
|
|
|
return -1;
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2002-09-24 12:43:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-06 03:49:07 +01:00
|
|
|
/** Write the private key from <b>env</b> into the file named by <b>fname</b>,
|
2004-05-01 23:41:23 +02:00
|
|
|
* PEM-encoded. Return 0 on success, -1 on failure.
|
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_write_private_key_to_filename(crypto_pk_t *env,
|
2003-09-26 20:27:35 +02:00
|
|
|
const char *fname)
|
|
|
|
{
|
|
|
|
BIO *bio;
|
|
|
|
char *cp;
|
|
|
|
long len;
|
2003-09-26 20:44:20 +02:00
|
|
|
char *s;
|
2003-09-26 20:27:35 +02:00
|
|
|
int r;
|
2004-04-03 04:40:30 +02:00
|
|
|
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(PRIVATE_KEY_OK(env));
|
2004-04-25 21:21:44 +02:00
|
|
|
|
2003-09-26 20:27:35 +02:00
|
|
|
if (!(bio = BIO_new(BIO_s_mem())))
|
|
|
|
return -1;
|
2004-04-03 04:40:30 +02:00
|
|
|
if (PEM_write_bio_RSAPrivateKey(bio, env->key, NULL,NULL,0,NULL,NULL)
|
2003-09-26 20:44:20 +02:00
|
|
|
== 0) {
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "writing private key");
|
2003-09-26 20:27:35 +02:00
|
|
|
BIO_free(bio);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len = BIO_get_mem_data(bio, &cp);
|
2004-10-13 07:54:58 +02:00
|
|
|
tor_assert(len >= 0);
|
2003-09-26 20:44:20 +02:00
|
|
|
s = tor_malloc(len+1);
|
2005-01-03 23:35:40 +01:00
|
|
|
memcpy(s, cp, len);
|
|
|
|
s[len]='\0';
|
2004-09-08 09:16:34 +02:00
|
|
|
r = write_str_to_file(fname, s, 0);
|
2003-09-26 20:27:35 +02:00
|
|
|
BIO_free(bio);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(s, 0, strlen(s));
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(s);
|
2003-09-26 20:27:35 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Return true iff <b>env</b> has a valid key.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_check_key(crypto_pk_t *env)
|
2002-07-25 10:17:22 +02:00
|
|
|
{
|
2004-04-26 20:09:50 +02:00
|
|
|
int r;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(env);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-26 20:09:50 +02:00
|
|
|
r = RSA_check_key(env->key);
|
|
|
|
if (r <= 0)
|
|
|
|
crypto_log_errors(LOG_WARN,"checking RSA key");
|
|
|
|
return r;
|
2002-07-25 10:17:22 +02:00
|
|
|
}
|
|
|
|
|
2008-08-08 16:36:11 +02:00
|
|
|
/** Return true iff <b>key</b> contains the private-key portion of the RSA
|
|
|
|
* key. */
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_key_is_private(const crypto_pk_t *key)
|
2008-08-08 16:36:11 +02:00
|
|
|
{
|
|
|
|
tor_assert(key);
|
|
|
|
return PRIVATE_KEY_OK(key);
|
|
|
|
}
|
|
|
|
|
2011-04-28 21:13:03 +02:00
|
|
|
/** Return true iff <b>env</b> contains a public key whose public exponent
|
|
|
|
* equals 65537.
|
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_public_exponent_ok(crypto_pk_t *env)
|
2011-04-28 21:13:03 +02:00
|
|
|
{
|
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
|
|
|
|
|
|
|
return BN_is_word(env->key->e, 65537);
|
|
|
|
}
|
|
|
|
|
2012-09-17 16:23:23 +02:00
|
|
|
/** Compare the public-key components of a and b. Return less than 0
|
|
|
|
* if a\<b, 0 if a==b, and greater than 0 if a\>b. A NULL key is
|
|
|
|
* considered to be less than all non-NULL keys, and equal to itself.
|
2012-09-15 11:47:14 +02:00
|
|
|
*
|
|
|
|
* Note that this may leak information about the keys through timing.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_cmp_keys(crypto_pk_t *a, crypto_pk_t *b)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2003-12-16 06:29:04 +01:00
|
|
|
int result;
|
2012-09-15 11:47:14 +02:00
|
|
|
char a_is_non_null = (a != NULL) && (a->key != NULL);
|
|
|
|
char b_is_non_null = (b != NULL) && (b->key != NULL);
|
|
|
|
char an_argument_is_null = !a_is_non_null | !b_is_non_null;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2012-09-15 11:47:14 +02:00
|
|
|
result = tor_memcmp(&a_is_non_null, &b_is_non_null, sizeof(a_is_non_null));
|
|
|
|
if (an_argument_is_null)
|
|
|
|
return result;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(PUBLIC_KEY_OK(a));
|
|
|
|
tor_assert(PUBLIC_KEY_OK(b));
|
2004-04-03 04:40:30 +02:00
|
|
|
result = BN_cmp((a->key)->n, (b->key)->n);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
return BN_cmp((a->key)->e, (b->key)->e);
|
2002-08-22 09:30:03 +02:00
|
|
|
}
|
|
|
|
|
2012-09-15 12:52:13 +02:00
|
|
|
/** Compare the public-key components of a and b. Return non-zero iff
|
|
|
|
* a==b. A NULL key is considered to be distinct from all non-NULL
|
|
|
|
* keys, and equal to itself.
|
|
|
|
*
|
|
|
|
* Note that this may leak information about the keys through timing.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_pk_eq_keys(crypto_pk_t *a, crypto_pk_t *b)
|
|
|
|
{
|
|
|
|
return (crypto_pk_cmp_keys(a, b) == 0);
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Return the size of the public key modulus in <b>env</b>, in bytes. */
|
2005-09-30 03:09:52 +02:00
|
|
|
size_t
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_keysize(crypto_pk_t *env)
|
2002-08-22 09:30:03 +02:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2005-05-07 07:55:06 +02:00
|
|
|
return (size_t) RSA_size(env->key);
|
2002-08-22 09:30:03 +02:00
|
|
|
}
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2011-06-02 18:32:59 +02:00
|
|
|
/** Return the size of the public key modulus of <b>env</b>, in bits. */
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_num_bits(crypto_pk_t *env)
|
2011-06-02 18:32:59 +02:00
|
|
|
{
|
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
|
|
|
tor_assert(env->key->n);
|
|
|
|
|
|
|
|
return BN_num_bits(env->key->n);
|
|
|
|
}
|
|
|
|
|
2004-10-07 22:58:53 +02:00
|
|
|
/** Increase the reference count of <b>env</b>, and return it.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *
|
|
|
|
crypto_pk_dup_key(crypto_pk_t *env)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
2002-09-24 12:43:57 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
env->refs++;
|
2002-09-24 12:43:57 +02:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2008-12-18 06:28:27 +01:00
|
|
|
/** Make a real honest-to-goodness copy of <b>env</b>, and return it. */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *
|
|
|
|
crypto_pk_copy_full(crypto_pk_t *env)
|
2008-12-18 06:28:27 +01:00
|
|
|
{
|
|
|
|
RSA *new_key;
|
2010-06-23 04:20:52 +02:00
|
|
|
int privatekey = 0;
|
2008-12-18 06:28:27 +01:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->key);
|
|
|
|
|
|
|
|
if (PRIVATE_KEY_OK(env)) {
|
|
|
|
new_key = RSAPrivateKey_dup(env->key);
|
2010-06-23 04:20:52 +02:00
|
|
|
privatekey = 1;
|
2008-12-18 06:28:27 +01:00
|
|
|
} else {
|
|
|
|
new_key = RSAPublicKey_dup(env->key);
|
|
|
|
}
|
2010-06-23 04:20:52 +02:00
|
|
|
if (!new_key) {
|
|
|
|
log_err(LD_CRYPTO, "Unable to duplicate a %s key: openssl failed.",
|
|
|
|
privatekey?"private":"public");
|
|
|
|
crypto_log_errors(LOG_ERR,
|
|
|
|
privatekey ? "Duplicating a private key" :
|
|
|
|
"Duplicating a public key");
|
|
|
|
tor_fragile_assert();
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-18 06:28:27 +01:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
return crypto_new_pk_from_rsa_(new_key);
|
2008-12-18 06:28:27 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Encrypt <b>fromlen</b> bytes from <b>from</b> with the public key
|
|
|
|
* in <b>env</b>, using the padding method <b>padding</b>. On success,
|
|
|
|
* write the result to <b>to</b>, and return the number of bytes
|
|
|
|
* written. On failure, return -1.
|
2011-01-13 20:36:41 +01:00
|
|
|
*
|
|
|
|
* <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
|
|
|
|
* at least the length of the modulus of <b>env</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_public_encrypt(crypto_pk_t *env, char *to, size_t tolen,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen, int padding)
|
2002-07-25 10:17:22 +02:00
|
|
|
{
|
2004-04-26 20:09:50 +02:00
|
|
|
int r;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2008-02-21 22:57:42 +01:00
|
|
|
tor_assert(fromlen<INT_MAX);
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen >= crypto_pk_keysize(env));
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2008-02-24 23:11:18 +01:00
|
|
|
r = RSA_public_encrypt((int)fromlen,
|
|
|
|
(unsigned char*)from, (unsigned char*)to,
|
2005-05-07 07:55:06 +02:00
|
|
|
env->key, crypto_get_rsa_padding(padding));
|
2004-05-01 23:41:23 +02:00
|
|
|
if (r<0) {
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "performing RSA encryption");
|
2004-05-01 23:41:23 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-26 20:09:50 +02:00
|
|
|
return r;
|
2002-07-25 10:17:22 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Decrypt <b>fromlen</b> bytes from <b>from</b> with the private key
|
|
|
|
* in <b>env</b>, using the padding method <b>padding</b>. On success,
|
|
|
|
* write the result to <b>to</b>, and return the number of bytes
|
|
|
|
* written. On failure, return -1.
|
2011-01-13 20:36:41 +01:00
|
|
|
*
|
|
|
|
* <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
|
|
|
|
* at least the length of the modulus of <b>env</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_private_decrypt(crypto_pk_t *env, char *to,
|
2011-01-13 20:36:41 +01:00
|
|
|
size_t tolen,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen,
|
2004-11-02 03:28:51 +01:00
|
|
|
int padding, int warnOnFailure)
|
2002-07-25 10:17:22 +02:00
|
|
|
{
|
2004-04-26 20:09:50 +02:00
|
|
|
int r;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
|
|
|
tor_assert(env->key);
|
2008-02-21 22:57:42 +01:00
|
|
|
tor_assert(fromlen<INT_MAX);
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen >= crypto_pk_keysize(env));
|
2004-04-03 04:40:30 +02:00
|
|
|
if (!env->key->p)
|
|
|
|
/* Not a private key */
|
|
|
|
return -1;
|
2003-04-16 17:24:09 +02:00
|
|
|
|
2008-02-21 22:57:42 +01:00
|
|
|
r = RSA_private_decrypt((int)fromlen,
|
|
|
|
(unsigned char*)from, (unsigned char*)to,
|
2005-05-07 07:55:06 +02:00
|
|
|
env->key, crypto_get_rsa_padding(padding));
|
|
|
|
|
2004-05-01 23:41:23 +02:00
|
|
|
if (r<0) {
|
2005-10-17 18:21:42 +02:00
|
|
|
crypto_log_errors(warnOnFailure?LOG_WARN:LOG_DEBUG,
|
2004-05-12 21:30:28 +02:00
|
|
|
"performing RSA decryption");
|
2004-05-01 23:41:23 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-26 20:09:50 +02:00
|
|
|
return r;
|
2002-07-25 10:17:22 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Check the signature in <b>from</b> (<b>fromlen</b> bytes long) with the
|
|
|
|
* public key in <b>env</b>, using PKCS1 padding. On success, write the
|
|
|
|
* signed data to <b>to</b>, and return the number of bytes written.
|
2004-05-02 01:29:20 +02:00
|
|
|
* On failure, return -1.
|
2011-01-13 20:36:41 +01:00
|
|
|
*
|
|
|
|
* <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
|
|
|
|
* at least the length of the modulus of <b>env</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_public_checksig(crypto_pk_t *env, char *to,
|
2011-01-13 20:36:41 +01:00
|
|
|
size_t tolen,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen)
|
2003-05-07 04:13:23 +02:00
|
|
|
{
|
2004-04-26 20:09:50 +02:00
|
|
|
int r;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2008-02-21 22:57:42 +01:00
|
|
|
tor_assert(fromlen < INT_MAX);
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen >= crypto_pk_keysize(env));
|
2008-02-24 23:11:18 +01:00
|
|
|
r = RSA_public_decrypt((int)fromlen,
|
|
|
|
(unsigned char*)from, (unsigned char*)to,
|
2005-12-14 21:40:40 +01:00
|
|
|
env->key, RSA_PKCS1_PADDING);
|
2004-04-26 20:09:50 +02:00
|
|
|
|
2004-05-02 01:29:20 +02:00
|
|
|
if (r<0) {
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "checking RSA signature");
|
2004-05-02 01:29:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-26 20:09:50 +02:00
|
|
|
return r;
|
2003-05-07 04:13:23 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Check a siglen-byte long signature at <b>sig</b> against
|
|
|
|
* <b>datalen</b> bytes of data at <b>data</b>, using the public key
|
|
|
|
* in <b>env</b>. Return 0 if <b>sig</b> is a correct signature for
|
|
|
|
* SHA1(data). Else return -1.
|
2004-04-02 00:10:33 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_public_checksig_digest(crypto_pk_t *env, const char *data,
|
2008-02-24 23:11:18 +01:00
|
|
|
size_t datalen, const char *sig, size_t siglen)
|
2004-04-02 00:10:33 +02:00
|
|
|
{
|
2004-04-03 04:40:30 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2007-05-02 23:37:53 +02:00
|
|
|
char *buf;
|
2011-01-13 20:36:41 +01:00
|
|
|
size_t buflen;
|
2004-04-02 00:10:33 +02:00
|
|
|
int r;
|
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(data);
|
|
|
|
tor_assert(sig);
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(datalen < SIZE_T_CEILING);
|
|
|
|
tor_assert(siglen < SIZE_T_CEILING);
|
2004-04-02 00:10:33 +02:00
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
if (crypto_digest(digest,data,datalen)<0) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_warn(LD_BUG, "couldn't compute digest");
|
2004-04-02 00:10:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-14 06:09:40 +01:00
|
|
|
buflen = crypto_pk_keysize(env);
|
2011-01-13 20:36:41 +01:00
|
|
|
buf = tor_malloc(buflen);
|
|
|
|
r = crypto_pk_public_checksig(env,buf,buflen,sig,siglen);
|
2004-04-03 04:40:30 +02:00
|
|
|
if (r != DIGEST_LEN) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Invalid signature");
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2004-04-02 00:10:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(buf, digest, DIGEST_LEN)) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Signature mismatched with digest.");
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2004-04-02 00:10:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2004-04-02 00:10:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
/** Sign <b>fromlen</b> bytes of data from <b>from</b> with the private key in
|
|
|
|
* <b>env</b>, using PKCS1 padding. On success, write the signature to
|
|
|
|
* <b>to</b>, and return the number of bytes written. On failure, return
|
|
|
|
* -1.
|
2011-01-13 20:36:41 +01:00
|
|
|
*
|
|
|
|
* <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
|
|
|
|
* at least the length of the modulus of <b>env</b>.
|
2004-11-02 03:28:51 +01:00
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_private_sign(crypto_pk_t *env, char *to, size_t tolen,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen)
|
2004-11-02 03:28:51 +01:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2008-02-21 22:57:42 +01:00
|
|
|
tor_assert(fromlen < INT_MAX);
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen >= crypto_pk_keysize(env));
|
2004-11-02 03:28:51 +01:00
|
|
|
if (!env->key->p)
|
|
|
|
/* Not a private key */
|
|
|
|
return -1;
|
|
|
|
|
2008-02-21 22:57:42 +01:00
|
|
|
r = RSA_private_encrypt((int)fromlen,
|
|
|
|
(unsigned char*)from, (unsigned char*)to,
|
2005-12-14 21:40:40 +01:00
|
|
|
env->key, RSA_PKCS1_PADDING);
|
2004-11-02 03:28:51 +01:00
|
|
|
if (r<0) {
|
|
|
|
crypto_log_errors(LOG_WARN, "generating RSA signature");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Compute a SHA1 digest of <b>fromlen</b> bytes of data stored at
|
|
|
|
* <b>from</b>; sign the data with the private key in <b>env</b>, and
|
|
|
|
* store it in <b>to</b>. Return the number of bytes written on
|
|
|
|
* success, and -1 on failure.
|
2011-01-13 20:36:41 +01:00
|
|
|
*
|
|
|
|
* <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
|
|
|
|
* at least the length of the modulus of <b>env</b>.
|
2004-04-02 00:10:33 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen)
|
2004-04-02 00:10:33 +02:00
|
|
|
{
|
2008-02-07 17:10:33 +01:00
|
|
|
int r;
|
2004-04-03 04:40:30 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2004-11-02 03:28:51 +01:00
|
|
|
if (crypto_digest(digest,from,fromlen)<0)
|
2004-05-01 23:41:23 +02:00
|
|
|
return -1;
|
2011-01-13 20:36:41 +01:00
|
|
|
r = crypto_pk_private_sign(env,to,tolen,digest,DIGEST_LEN);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(digest, 0, sizeof(digest));
|
2008-02-07 17:10:33 +01:00
|
|
|
return r;
|
2004-04-02 00:10:33 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Perform a hybrid (public/secret) encryption on <b>fromlen</b>
|
|
|
|
* bytes of data from <b>from</b>, with padding type 'padding',
|
|
|
|
* storing the results on <b>to</b>.
|
2004-04-01 05:08:35 +02:00
|
|
|
*
|
|
|
|
* Returns the number of bytes written on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* The encrypted data consists of:
|
2004-05-10 05:53:24 +02:00
|
|
|
* - The source data, padded and encrypted with the public key, if the
|
|
|
|
* padded source data is no longer than the public key, and <b>force</b>
|
|
|
|
* is false, OR
|
|
|
|
* - The beginning of the source data prefixed with a 16-byte symmetric key,
|
|
|
|
* padded and encrypted with the public key; followed by the rest of
|
|
|
|
* the source data encrypted in AES-CTR mode with the symmetric key.
|
2004-04-01 05:08:35 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_public_hybrid_encrypt(crypto_pk_t *env,
|
2011-01-13 20:36:41 +01:00
|
|
|
char *to, size_t tolen,
|
2005-09-30 03:09:52 +02:00
|
|
|
const char *from,
|
|
|
|
size_t fromlen,
|
|
|
|
int padding, int force)
|
2004-04-01 05:08:35 +02:00
|
|
|
{
|
2008-02-21 22:57:47 +01:00
|
|
|
int overhead, outlen, r;
|
|
|
|
size_t pkeylen, symlen;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_t *cipher = NULL;
|
2007-05-02 23:37:53 +02:00
|
|
|
char *buf = NULL;
|
2004-04-01 05:08:35 +02:00
|
|
|
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(fromlen < SIZE_T_CEILING);
|
2004-04-01 05:08:35 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
overhead = crypto_get_rsa_padding_overhead(crypto_get_rsa_padding(padding));
|
2004-04-01 05:08:35 +02:00
|
|
|
pkeylen = crypto_pk_keysize(env);
|
|
|
|
|
2004-04-06 22:55:46 +02:00
|
|
|
if (!force && fromlen+overhead <= pkeylen) {
|
2004-04-01 05:08:35 +02:00
|
|
|
/* It all fits in a single encrypt. */
|
2011-01-13 20:36:41 +01:00
|
|
|
return crypto_pk_public_encrypt(env,to,
|
|
|
|
tolen,
|
|
|
|
from,fromlen,padding);
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen >= fromlen + overhead + CIPHER_KEY_LEN);
|
|
|
|
tor_assert(tolen >= pkeylen);
|
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
cipher = crypto_cipher_new(NULL); /* generate a new key. */
|
|
|
|
|
2007-05-02 23:37:53 +02:00
|
|
|
buf = tor_malloc(pkeylen+1);
|
2004-04-03 04:40:30 +02:00
|
|
|
memcpy(buf, cipher->key, CIPHER_KEY_LEN);
|
|
|
|
memcpy(buf+CIPHER_KEY_LEN, from, pkeylen-overhead-CIPHER_KEY_LEN);
|
2004-04-01 05:08:35 +02:00
|
|
|
|
|
|
|
/* Length of symmetrically encrypted data. */
|
2004-04-03 04:40:30 +02:00
|
|
|
symlen = fromlen-(pkeylen-overhead-CIPHER_KEY_LEN);
|
2004-04-01 05:08:35 +02:00
|
|
|
|
2011-01-13 20:36:41 +01:00
|
|
|
outlen = crypto_pk_public_encrypt(env,to,tolen,buf,pkeylen-overhead,padding);
|
2005-05-07 07:55:06 +02:00
|
|
|
if (outlen!=(int)pkeylen) {
|
2004-04-01 05:08:35 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2004-11-02 03:28:51 +01:00
|
|
|
r = crypto_cipher_encrypt(cipher, to+outlen,
|
|
|
|
from+pkeylen-overhead-CIPHER_KEY_LEN, symlen);
|
2004-04-01 05:08:35 +02:00
|
|
|
|
|
|
|
if (r<0) goto err;
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(buf, 0, pkeylen);
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_free(cipher);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(outlen+symlen < INT_MAX);
|
|
|
|
return (int)(outlen + symlen);
|
2004-04-01 05:08:35 +02:00
|
|
|
err:
|
2012-04-18 16:38:39 +02:00
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(buf, 0, pkeylen);
|
2012-04-18 16:38:39 +02:00
|
|
|
tor_free(buf);
|
2012-03-30 16:16:58 +02:00
|
|
|
crypto_cipher_free(cipher);
|
2004-04-01 05:08:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Invert crypto_pk_public_hybrid_encrypt. */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_private_hybrid_decrypt(crypto_pk_t *env,
|
2005-09-30 03:09:52 +02:00
|
|
|
char *to,
|
2011-01-13 20:36:41 +01:00
|
|
|
size_t tolen,
|
2005-09-30 03:09:52 +02:00
|
|
|
const char *from,
|
|
|
|
size_t fromlen,
|
|
|
|
int padding, int warnOnFailure)
|
2004-04-01 05:08:35 +02:00
|
|
|
{
|
2006-10-09 04:35:51 +02:00
|
|
|
int outlen, r;
|
2005-05-07 07:55:06 +02:00
|
|
|
size_t pkeylen;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_t *cipher = NULL;
|
2007-05-02 23:37:53 +02:00
|
|
|
char *buf = NULL;
|
2004-04-01 05:08:35 +02:00
|
|
|
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(fromlen < SIZE_T_CEILING);
|
2004-04-01 05:08:35 +02:00
|
|
|
pkeylen = crypto_pk_keysize(env);
|
|
|
|
|
|
|
|
if (fromlen <= pkeylen) {
|
2011-01-13 20:36:41 +01:00
|
|
|
return crypto_pk_private_decrypt(env,to,tolen,from,fromlen,padding,
|
2005-12-14 21:40:40 +01:00
|
|
|
warnOnFailure);
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
2011-01-13 20:36:41 +01:00
|
|
|
|
2011-01-14 06:09:40 +01:00
|
|
|
buf = tor_malloc(pkeylen);
|
|
|
|
outlen = crypto_pk_private_decrypt(env,buf,pkeylen,from,pkeylen,padding,
|
2005-12-14 21:40:40 +01:00
|
|
|
warnOnFailure);
|
2004-04-01 05:08:35 +02:00
|
|
|
if (outlen<0) {
|
2005-10-18 23:58:19 +02:00
|
|
|
log_fn(warnOnFailure?LOG_WARN:LOG_DEBUG, LD_CRYPTO,
|
|
|
|
"Error decrypting public-key data");
|
2007-05-02 23:37:53 +02:00
|
|
|
goto err;
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
2004-04-03 04:40:30 +02:00
|
|
|
if (outlen < CIPHER_KEY_LEN) {
|
2005-10-18 23:58:19 +02:00
|
|
|
log_fn(warnOnFailure?LOG_WARN:LOG_INFO, LD_CRYPTO,
|
|
|
|
"No room for a symmetric key");
|
2007-05-02 23:37:53 +02:00
|
|
|
goto err;
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
2012-03-20 20:35:43 +01:00
|
|
|
cipher = crypto_cipher_new(buf);
|
2004-04-01 05:08:35 +02:00
|
|
|
if (!cipher) {
|
2007-05-02 23:37:53 +02:00
|
|
|
goto err;
|
2004-04-01 05:08:35 +02:00
|
|
|
}
|
2004-04-03 04:40:30 +02:00
|
|
|
memcpy(to,buf+CIPHER_KEY_LEN,outlen-CIPHER_KEY_LEN);
|
|
|
|
outlen -= CIPHER_KEY_LEN;
|
2011-01-13 20:36:41 +01:00
|
|
|
tor_assert(tolen - outlen >= fromlen - pkeylen);
|
2004-11-02 03:28:51 +01:00
|
|
|
r = crypto_cipher_decrypt(cipher, to+outlen, from+pkeylen, fromlen-pkeylen);
|
2004-04-01 05:08:35 +02:00
|
|
|
if (r<0)
|
|
|
|
goto err;
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(buf,0,pkeylen);
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_free(cipher);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(outlen + fromlen < INT_MAX);
|
|
|
|
return (int)(outlen + (fromlen-pkeylen));
|
2004-04-01 05:08:35 +02:00
|
|
|
err:
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(buf,0,pkeylen);
|
2007-05-02 23:37:53 +02:00
|
|
|
tor_free(buf);
|
2012-03-30 16:16:58 +02:00
|
|
|
crypto_cipher_free(cipher);
|
2004-04-01 05:08:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** ASN.1-encode the public portion of <b>pk</b> into <b>dest</b>.
|
|
|
|
* Return -1 on error, or the number of characters used on success.
|
2004-03-08 01:11:37 +01:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_asn1_encode(crypto_pk_t *pk, char *dest, size_t dest_len)
|
2004-03-30 21:47:32 +02:00
|
|
|
{
|
|
|
|
int len;
|
2013-06-06 12:45:35 +02:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
|
|
|
|
len = i2d_RSAPublicKey(pk->key, &buf);
|
|
|
|
if (len < 0 || buf == NULL)
|
2004-03-30 21:47:32 +02:00
|
|
|
return -1;
|
2013-06-06 12:45:35 +02:00
|
|
|
|
|
|
|
if ((size_t)len > dest_len || dest_len > SIZE_T_CEILING) {
|
|
|
|
OPENSSL_free(buf);
|
2004-03-30 21:47:32 +02:00
|
|
|
return -1;
|
2004-03-31 00:50:49 +02:00
|
|
|
}
|
|
|
|
/* We don't encode directly into 'dest', because that would be illegal
|
|
|
|
* type-punning. (C99 is smarter than me, C99 is smarter than me...)
|
|
|
|
*/
|
|
|
|
memcpy(dest,buf,len);
|
2013-06-06 12:45:35 +02:00
|
|
|
OPENSSL_free(buf);
|
2004-03-30 21:47:32 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Decode an ASN.1-encoded public key from <b>str</b>; return the result on
|
2004-05-02 01:29:20 +02:00
|
|
|
* success and NULL on failure.
|
2004-03-30 21:47:32 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *
|
2005-09-30 03:09:52 +02:00
|
|
|
crypto_pk_asn1_decode(const char *str, size_t len)
|
2004-03-30 21:47:32 +02:00
|
|
|
{
|
|
|
|
RSA *rsa;
|
2004-04-01 00:41:25 +02:00
|
|
|
unsigned char *buf;
|
2004-04-05 19:10:48 +02:00
|
|
|
const unsigned char *cp;
|
|
|
|
cp = buf = tor_malloc(len);
|
|
|
|
memcpy(buf,str,len);
|
|
|
|
rsa = d2i_RSAPublicKey(NULL, &cp, len);
|
2004-03-31 00:50:49 +02:00
|
|
|
tor_free(buf);
|
2004-04-26 20:09:50 +02:00
|
|
|
if (!rsa) {
|
|
|
|
crypto_log_errors(LOG_WARN,"decoding public key");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-12 18:22:13 +02:00
|
|
|
return crypto_new_pk_from_rsa_(rsa);
|
2004-03-30 21:47:32 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Given a private or public key <b>pk</b>, put a SHA1 hash of the
|
|
|
|
* public key into <b>digest_out</b> (must have DIGEST_LEN bytes of space).
|
2004-10-07 22:58:53 +02:00
|
|
|
* Return 0 on success, -1 on failure.
|
2004-03-30 21:47:32 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_get_digest(crypto_pk_t *pk, char *digest_out)
|
2003-09-25 07:17:11 +02:00
|
|
|
{
|
2013-06-06 12:45:35 +02:00
|
|
|
unsigned char *buf = NULL;
|
2003-09-25 07:17:11 +02:00
|
|
|
int len;
|
2004-04-03 04:40:30 +02:00
|
|
|
|
2013-06-06 12:45:35 +02:00
|
|
|
len = i2d_RSAPublicKey(pk->key, &buf);
|
|
|
|
if (len < 0 || buf == NULL)
|
2003-09-25 07:17:11 +02:00
|
|
|
return -1;
|
2005-05-07 07:55:06 +02:00
|
|
|
if (crypto_digest(digest_out, (char*)buf, len) < 0) {
|
2013-06-06 12:45:35 +02:00
|
|
|
OPENSSL_free(buf);
|
2003-09-25 07:17:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-06 12:45:35 +02:00
|
|
|
OPENSSL_free(buf);
|
2004-03-30 21:47:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-13 20:32:51 +02:00
|
|
|
/** Compute all digests of the DER encoding of <b>pk</b>, and store them
|
|
|
|
* in <b>digests_out</b>. Return 0 on success, -1 on failure. */
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_get_all_digests(crypto_pk_t *pk, digests_t *digests_out)
|
2011-09-13 20:32:51 +02:00
|
|
|
{
|
2013-06-06 12:45:35 +02:00
|
|
|
unsigned char *buf = NULL;
|
2011-09-13 20:32:51 +02:00
|
|
|
int len;
|
|
|
|
|
2013-06-06 12:45:35 +02:00
|
|
|
len = i2d_RSAPublicKey(pk->key, &buf);
|
|
|
|
if (len < 0 || buf == NULL)
|
2011-09-13 20:32:51 +02:00
|
|
|
return -1;
|
|
|
|
if (crypto_digest_all(digests_out, (char*)buf, len) < 0) {
|
2013-06-06 12:45:35 +02:00
|
|
|
OPENSSL_free(buf);
|
2011-09-13 20:32:51 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-06 12:45:35 +02:00
|
|
|
OPENSSL_free(buf);
|
2011-09-13 20:32:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-16 00:39:14 +01:00
|
|
|
/** Copy <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, adding spaces
|
|
|
|
* every four spaces. */
|
2013-06-06 23:58:28 +02:00
|
|
|
void
|
|
|
|
crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
|
2008-02-16 00:39:14 +01:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
char *end = out+outlen;
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(outlen < SIZE_T_CEILING);
|
|
|
|
|
2008-02-16 00:39:14 +01:00
|
|
|
while (*in && out<end) {
|
|
|
|
*out++ = *in++;
|
|
|
|
if (++n == 4 && *in && out<end) {
|
|
|
|
n = 0;
|
|
|
|
*out++ = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(out<end);
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Given a private or public key <b>pk</b>, put a fingerprint of the
|
|
|
|
* public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1 bytes of
|
2004-10-07 22:58:53 +02:00
|
|
|
* space). Return 0 on success, -1 on failure.
|
2004-05-10 05:53:24 +02:00
|
|
|
*
|
|
|
|
* Fingerprints are computed as the SHA1 digest of the ASN.1 encoding
|
2004-07-01 03:16:59 +02:00
|
|
|
* of the public key, converted to hexadecimal, in upper case, with a
|
|
|
|
* space after every four digits.
|
2004-10-06 15:26:10 +02:00
|
|
|
*
|
|
|
|
* If <b>add_space</b> is false, omit the spaces.
|
2004-03-30 21:47:32 +02:00
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
|
2004-03-30 21:47:32 +02:00
|
|
|
{
|
2005-05-07 07:55:06 +02:00
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
char hexdigest[HEX_DIGEST_LEN+1];
|
2004-03-30 21:47:32 +02:00
|
|
|
if (crypto_pk_get_digest(pk, digest)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-10-07 22:58:53 +02:00
|
|
|
base16_encode(hexdigest,sizeof(hexdigest),digest,DIGEST_LEN);
|
2004-10-06 15:26:10 +02:00
|
|
|
if (add_space) {
|
2013-06-06 23:58:28 +02:00
|
|
|
crypto_add_spaces_to_fp(fp_out, FINGERPRINT_LEN+1, hexdigest);
|
2004-10-07 22:58:53 +02:00
|
|
|
} else {
|
2007-01-20 04:35:03 +01:00
|
|
|
strncpy(fp_out, hexdigest, HEX_DIGEST_LEN+1);
|
2004-10-06 15:26:10 +02:00
|
|
|
}
|
2003-09-25 07:17:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-26 10:44:55 +01:00
|
|
|
/** Given a private or public key <b>pk</b>, put a hashed fingerprint of
|
|
|
|
* the public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1
|
|
|
|
* bytes of space). Return 0 on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* Hashed fingerprints are computed as the SHA1 digest of the SHA1 digest
|
|
|
|
* of the ASN.1 encoding of the public key, converted to hexadecimal, in
|
|
|
|
* upper case.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out)
|
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN], hashed_digest[DIGEST_LEN];
|
|
|
|
if (crypto_pk_get_digest(pk, digest)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (crypto_digest(hashed_digest, digest, DIGEST_LEN)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
base16_encode(fp_out, FINGERPRINT_LEN + 1, hashed_digest, DIGEST_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-24 16:02:39 +02:00
|
|
|
/* symmetric crypto */
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Return a pointer to the key set for the cipher in <b>env</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
const char *
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_get_key(crypto_cipher_t *env)
|
2003-09-10 02:47:24 +02:00
|
|
|
{
|
|
|
|
return env->key;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Encrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
|
|
|
|
* <b>env</b>; on success, store the result to <b>to</b> and return 0.
|
|
|
|
* On failure, return -1.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(env->cipher);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(fromlen);
|
|
|
|
tor_assert(to);
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(fromlen < SIZE_T_CEILING);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
aes_crypt(env->cipher, from, fromlen, to);
|
|
|
|
return 0;
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Decrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
|
|
|
|
* <b>env</b>; on success, store the result to <b>to</b> and return 0.
|
|
|
|
* On failure, return -1.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2004-11-02 03:28:51 +01:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
|
2005-05-07 07:55:06 +02:00
|
|
|
const char *from, size_t fromlen)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(env);
|
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(fromlen < SIZE_T_CEILING);
|
2003-06-30 21:18:32 +02:00
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
aes_crypt(env->cipher, from, fromlen, to);
|
|
|
|
return 0;
|
2003-06-30 21:18:32 +02:00
|
|
|
}
|
|
|
|
|
2008-02-07 17:10:33 +01:00
|
|
|
/** Encrypt <b>len</b> bytes on <b>from</b> using the cipher in <b>env</b>;
|
|
|
|
* on success, return 0. On failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *buf, size_t len)
|
2008-02-07 17:10:33 +01:00
|
|
|
{
|
2010-12-14 00:40:21 +01:00
|
|
|
tor_assert(len < SIZE_T_CEILING);
|
2008-02-07 17:10:33 +01:00
|
|
|
aes_crypt_inplace(env->cipher, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-19 17:53:41 +02:00
|
|
|
/** Encrypt <b>fromlen</b> bytes (at least 1) from <b>from</b> with the key in
|
2012-03-20 20:35:43 +01:00
|
|
|
* <b>key</b> to the buffer in <b>to</b> of length
|
2007-09-19 17:53:41 +02:00
|
|
|
* <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> plus
|
|
|
|
* CIPHER_IV_LEN bytes for the initialization vector. On success, return the
|
|
|
|
* number of bytes written, on failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_encrypt_with_iv(const char *key,
|
2007-09-19 17:53:41 +02:00
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen)
|
|
|
|
{
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_t *cipher;
|
2007-09-19 17:53:41 +02:00
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(fromlen < INT_MAX);
|
2007-09-19 17:53:41 +02:00
|
|
|
|
2007-09-20 22:08:47 +02:00
|
|
|
if (fromlen < 1)
|
|
|
|
return -1;
|
2007-09-19 17:53:41 +02:00
|
|
|
if (tolen < fromlen + CIPHER_IV_LEN)
|
|
|
|
return -1;
|
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
cipher = crypto_cipher_new_with_iv(key, NULL);
|
|
|
|
|
|
|
|
memcpy(to, cipher->iv, CIPHER_IV_LEN);
|
2007-09-19 17:53:41 +02:00
|
|
|
crypto_cipher_encrypt(cipher, to+CIPHER_IV_LEN, from, fromlen);
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_free(cipher);
|
2008-02-21 22:57:47 +01:00
|
|
|
return (int)(fromlen + CIPHER_IV_LEN);
|
2007-09-19 17:53:41 +02:00
|
|
|
}
|
|
|
|
|
2007-09-20 22:08:47 +02:00
|
|
|
/** Decrypt <b>fromlen</b> bytes (at least 1+CIPHER_IV_LEN) from <b>from</b>
|
2012-03-20 20:35:43 +01:00
|
|
|
* with the key in <b>key</b> to the buffer in <b>to</b> of length
|
2007-09-19 17:53:41 +02:00
|
|
|
* <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> minus
|
|
|
|
* CIPHER_IV_LEN bytes for the initialization vector. On success, return the
|
|
|
|
* number of bytes written, on failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_decrypt_with_iv(const char *key,
|
2007-09-19 17:53:41 +02:00
|
|
|
char *to, size_t tolen,
|
|
|
|
const char *from, size_t fromlen)
|
|
|
|
{
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_t *cipher;
|
|
|
|
tor_assert(key);
|
2007-09-19 17:53:41 +02:00
|
|
|
tor_assert(from);
|
|
|
|
tor_assert(to);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(fromlen < INT_MAX);
|
2007-09-19 17:53:41 +02:00
|
|
|
|
2007-09-20 22:08:47 +02:00
|
|
|
if (fromlen <= CIPHER_IV_LEN)
|
2007-09-19 17:53:41 +02:00
|
|
|
return -1;
|
|
|
|
if (tolen < fromlen - CIPHER_IV_LEN)
|
|
|
|
return -1;
|
|
|
|
|
2012-03-20 20:35:43 +01:00
|
|
|
cipher = crypto_cipher_new_with_iv(key, from);
|
|
|
|
|
2007-09-19 17:53:41 +02:00
|
|
|
crypto_cipher_encrypt(cipher, to, from+CIPHER_IV_LEN, fromlen-CIPHER_IV_LEN);
|
2012-03-20 20:35:43 +01:00
|
|
|
crypto_cipher_free(cipher);
|
2008-02-21 22:57:47 +01:00
|
|
|
return (int)(fromlen - CIPHER_IV_LEN);
|
2007-09-19 17:53:41 +02:00
|
|
|
}
|
|
|
|
|
2002-07-24 16:02:39 +02:00
|
|
|
/* SHA-1 */
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Compute the SHA1 digest of the <b>len</b> bytes on data stored in
|
2004-05-10 05:53:24 +02:00
|
|
|
* <b>m</b>. Write the DIGEST_LEN byte result into <b>digest</b>.
|
2004-12-01 04:48:14 +01:00
|
|
|
* Return 0 on success, -1 on failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
crypto_digest(char *digest, const char *m, size_t len)
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(m);
|
|
|
|
tor_assert(digest);
|
2005-05-07 07:55:06 +02:00
|
|
|
return (SHA1((const unsigned char*)m,len,(unsigned char*)digest) == NULL);
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Compute a 256-bit digest of <b>len</b> bytes in data stored in <b>m</b>,
|
|
|
|
* using the algorithm <b>algorithm</b>. Write the DIGEST_LEN256-byte result
|
|
|
|
* into <b>digest</b>. Return 0 on success, -1 on failure. */
|
2009-08-20 01:21:29 +02:00
|
|
|
int
|
|
|
|
crypto_digest256(char *digest, const char *m, size_t len,
|
|
|
|
digest_algorithm_t algorithm)
|
|
|
|
{
|
|
|
|
tor_assert(m);
|
|
|
|
tor_assert(digest);
|
|
|
|
tor_assert(algorithm == DIGEST_SHA256);
|
|
|
|
return (SHA256((const unsigned char*)m,len,(unsigned char*)digest) == NULL);
|
|
|
|
}
|
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
/** Set the digests_t in <b>ds_out</b> to contain every digest on the
|
|
|
|
* <b>len</b> bytes in <b>m</b> that we know how to compute. Return 0 on
|
|
|
|
* success, -1 on failure. */
|
|
|
|
int
|
|
|
|
crypto_digest_all(digests_t *ds_out, const char *m, size_t len)
|
|
|
|
{
|
2012-12-31 18:23:28 +01:00
|
|
|
int i;
|
2009-09-16 23:01:01 +02:00
|
|
|
tor_assert(ds_out);
|
|
|
|
memset(ds_out, 0, sizeof(*ds_out));
|
|
|
|
if (crypto_digest(ds_out->d[DIGEST_SHA1], m, len) < 0)
|
|
|
|
return -1;
|
|
|
|
for (i = DIGEST_SHA256; i < N_DIGEST_ALGORITHMS; ++i) {
|
|
|
|
if (crypto_digest256(ds_out->d[i], m, len, i) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the name of an algorithm, as used in directory documents. */
|
|
|
|
const char *
|
|
|
|
crypto_digest_algorithm_get_name(digest_algorithm_t alg)
|
|
|
|
{
|
|
|
|
switch (alg) {
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
return "sha1";
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
return "sha256";
|
|
|
|
default:
|
|
|
|
tor_fragile_assert();
|
|
|
|
return "??unknown_digest??";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:06:00 +02:00
|
|
|
/** Given the name of a digest algorithm, return its integer value, or -1 if
|
|
|
|
* the name is not recognized. */
|
2009-09-16 18:34:44 +02:00
|
|
|
int
|
|
|
|
crypto_digest_algorithm_parse_name(const char *name)
|
|
|
|
{
|
|
|
|
if (!strcmp(name, "sha1"))
|
|
|
|
return DIGEST_SHA1;
|
|
|
|
else if (!strcmp(name, "sha256"))
|
|
|
|
return DIGEST_SHA256;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-06 06:33:40 +02:00
|
|
|
/** Intermediate information about the digest of a stream of data. */
|
2012-01-18 21:53:30 +01:00
|
|
|
struct crypto_digest_t {
|
2009-08-20 01:21:29 +02:00
|
|
|
union {
|
2011-03-16 22:05:37 +01:00
|
|
|
SHA_CTX sha1; /**< state for SHA1 */
|
|
|
|
SHA256_CTX sha2; /**< state for SHA256 */
|
|
|
|
} d; /**< State for the digest we're using. Only one member of the
|
|
|
|
* union is usable, depending on the value of <b>algorithm</b>. */
|
2014-03-25 16:16:18 +01:00
|
|
|
digest_algorithm_bitfield_t algorithm : 8; /**< Which algorithm is in use? */
|
2003-12-16 06:29:04 +01:00
|
|
|
};
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Allocate and return a new digest object to compute SHA1 digests.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *
|
|
|
|
crypto_digest_new(void)
|
2003-12-16 06:29:04 +01:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *r;
|
|
|
|
r = tor_malloc(sizeof(crypto_digest_t));
|
2009-08-20 01:21:29 +02:00
|
|
|
SHA1_Init(&r->d.sha1);
|
|
|
|
r->algorithm = DIGEST_SHA1;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Allocate and return a new digest object to compute 256-bit digests
|
|
|
|
* using <b>algorithm</b>. */
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *
|
|
|
|
crypto_digest256_new(digest_algorithm_t algorithm)
|
2009-08-20 01:21:29 +02:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *r;
|
2009-08-20 01:21:29 +02:00
|
|
|
tor_assert(algorithm == DIGEST_SHA256);
|
2012-01-18 21:53:30 +01:00
|
|
|
r = tor_malloc(sizeof(crypto_digest_t));
|
2009-08-20 01:21:29 +02:00
|
|
|
SHA256_Init(&r->d.sha2);
|
|
|
|
r->algorithm = algorithm;
|
2003-12-16 06:29:04 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Deallocate a digest object.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_free(crypto_digest_t *digest)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!digest)
|
|
|
|
return;
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(digest, 0, sizeof(crypto_digest_t));
|
2004-04-03 04:40:30 +02:00
|
|
|
tor_free(digest);
|
2003-12-16 06:29:04 +01:00
|
|
|
}
|
2003-12-16 06:47:21 +01:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Add <b>len</b> bytes from <b>data</b> to the digest object.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
|
2003-12-16 06:47:21 +01:00
|
|
|
size_t len)
|
2003-12-16 06:29:04 +01:00
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(digest);
|
|
|
|
tor_assert(data);
|
2009-08-20 01:21:29 +02:00
|
|
|
/* Using the SHA*_*() calls directly means we don't support doing
|
|
|
|
* SHA in hardware. But so far the delay of getting the question
|
2004-06-01 18:36:56 +02:00
|
|
|
* to the hardware, and hearing the answer, is likely higher than
|
|
|
|
* just doing it ourselves. Hashes are fast.
|
|
|
|
*/
|
2009-08-20 01:21:29 +02:00
|
|
|
switch (digest->algorithm) {
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
SHA1_Update(&digest->d.sha1, (void*)data, len);
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
SHA256_Update(&digest->d.sha2, (void*)data, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tor_fragile_assert();
|
|
|
|
break;
|
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
}
|
2003-12-16 06:47:21 +01:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Compute the hash of the data that has been passed to the digest
|
|
|
|
* object; write the first out_len bytes of the result to <b>out</b>.
|
2009-08-20 01:21:29 +02:00
|
|
|
* <b>out_len</b> must be \<= DIGEST256_LEN.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_get_digest(crypto_digest_t *digest,
|
2005-09-30 03:09:52 +02:00
|
|
|
char *out, size_t out_len)
|
2003-12-16 06:29:04 +01:00
|
|
|
{
|
2009-08-20 01:21:29 +02:00
|
|
|
unsigned char r[DIGEST256_LEN];
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t tmpenv;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(digest);
|
|
|
|
tor_assert(out);
|
2009-08-20 01:21:29 +02:00
|
|
|
/* memcpy into a temporary ctx, since SHA*_Final clears the context */
|
2012-01-18 21:53:30 +01:00
|
|
|
memcpy(&tmpenv, digest, sizeof(crypto_digest_t));
|
2009-08-20 01:21:29 +02:00
|
|
|
switch (digest->algorithm) {
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
tor_assert(out_len <= DIGEST_LEN);
|
2009-08-20 18:03:32 +02:00
|
|
|
SHA1_Final(r, &tmpenv.d.sha1);
|
2009-08-20 01:21:29 +02:00
|
|
|
break;
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
tor_assert(out_len <= DIGEST256_LEN);
|
2009-08-20 18:03:32 +02:00
|
|
|
SHA256_Final(r, &tmpenv.d.sha2);
|
2009-08-20 01:21:29 +02:00
|
|
|
break;
|
|
|
|
default:
|
2011-10-06 19:02:50 +02:00
|
|
|
log_warn(LD_BUG, "Called with unknown algorithm %d", digest->algorithm);
|
|
|
|
/* If fragile_assert is not enabled, then we should at least not
|
|
|
|
* leak anything. */
|
|
|
|
memset(r, 0xff, sizeof(r));
|
2009-08-20 01:21:29 +02:00
|
|
|
tor_fragile_assert();
|
|
|
|
break;
|
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
memcpy(out, r, out_len);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(r, 0, sizeof(r));
|
2003-12-16 06:29:04 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Allocate and return a new digest object with the same state as
|
|
|
|
* <b>digest</b>
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *
|
|
|
|
crypto_digest_dup(const crypto_digest_t *digest)
|
2003-12-16 06:29:04 +01:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *r;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(digest);
|
2012-01-18 21:53:30 +01:00
|
|
|
r = tor_malloc(sizeof(crypto_digest_t));
|
|
|
|
memcpy(r,digest,sizeof(crypto_digest_t));
|
2003-12-16 06:47:21 +01:00
|
|
|
return r;
|
2003-12-16 06:29:04 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Replace the state of the digest object <b>into</b> with the state
|
|
|
|
* of the digest object <b>from</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_assign(crypto_digest_t *into,
|
|
|
|
const crypto_digest_t *from)
|
2003-12-16 06:29:04 +01:00
|
|
|
{
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(into);
|
|
|
|
tor_assert(from);
|
2012-01-18 21:53:30 +01:00
|
|
|
memcpy(into,from,sizeof(crypto_digest_t));
|
2003-12-16 06:29:04 +01:00
|
|
|
}
|
|
|
|
|
2013-02-22 18:53:45 +01:00
|
|
|
/** Given a list of strings in <b>lst</b>, set the <b>len_out</b>-byte digest
|
|
|
|
* at <b>digest_out</b> to the hash of the concatenation of those strings,
|
|
|
|
* plus the optional string <b>append</b>, computed with the algorithm
|
2013-03-18 20:00:52 +01:00
|
|
|
* <b>alg</b>.
|
|
|
|
* <b>out_len</b> must be \<= DIGEST256_LEN. */
|
2013-02-22 18:53:45 +01:00
|
|
|
void
|
|
|
|
crypto_digest_smartlist(char *digest_out, size_t len_out,
|
|
|
|
const smartlist_t *lst, const char *append,
|
|
|
|
digest_algorithm_t alg)
|
|
|
|
{
|
|
|
|
crypto_digest_t *d;
|
|
|
|
if (alg == DIGEST_SHA1)
|
|
|
|
d = crypto_digest_new();
|
|
|
|
else
|
|
|
|
d = crypto_digest256_new(alg);
|
|
|
|
SMARTLIST_FOREACH(lst, const char *, cp,
|
|
|
|
crypto_digest_add_bytes(d, cp, strlen(cp)));
|
|
|
|
if (append)
|
|
|
|
crypto_digest_add_bytes(d, append, strlen(append));
|
|
|
|
crypto_digest_get_digest(d, digest_out, len_out);
|
|
|
|
crypto_digest_free(d);
|
|
|
|
}
|
|
|
|
|
2011-09-13 17:38:13 +02:00
|
|
|
/** Compute the HMAC-SHA-256 of the <b>msg_len</b> bytes in <b>msg</b>, using
|
2012-02-06 11:29:48 +01:00
|
|
|
* the <b>key</b> of length <b>key_len</b>. Store the DIGEST256_LEN-byte
|
|
|
|
* result in <b>hmac_out</b>.
|
2011-09-13 17:38:13 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
crypto_hmac_sha256(char *hmac_out,
|
|
|
|
const char *key, size_t key_len,
|
|
|
|
const char *msg, size_t msg_len)
|
|
|
|
{
|
|
|
|
/* If we've got OpenSSL >=0.9.8 we can use its hmac implementation. */
|
|
|
|
tor_assert(key_len < INT_MAX);
|
|
|
|
tor_assert(msg_len < INT_MAX);
|
|
|
|
HMAC(EVP_sha256(), key, (int)key_len, (unsigned char*)msg, (int)msg_len,
|
|
|
|
(unsigned char*)hmac_out, NULL);
|
|
|
|
}
|
|
|
|
|
2003-12-16 06:29:04 +01:00
|
|
|
/* DH */
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2011-11-25 17:44:11 +01:00
|
|
|
/** Our DH 'g' parameter */
|
|
|
|
#define DH_GENERATOR 2
|
|
|
|
|
2011-01-24 22:03:14 +01:00
|
|
|
/** Shared P parameter for our circuit-crypto DH key exchanges. */
|
2003-05-01 02:53:46 +02:00
|
|
|
static BIGNUM *dh_param_p = NULL;
|
2011-01-24 22:03:14 +01:00
|
|
|
/** Shared P parameter for our TLS DH key exchanges. */
|
|
|
|
static BIGNUM *dh_param_p_tls = NULL;
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Shared G parameter for our DH key exchanges. */
|
2003-05-01 02:53:46 +02:00
|
|
|
static BIGNUM *dh_param_g = NULL;
|
|
|
|
|
2011-11-24 22:06:50 +01:00
|
|
|
/** Generate and return a reasonable and safe DH parameter p. */
|
2011-11-22 05:08:26 +01:00
|
|
|
static BIGNUM *
|
2011-11-25 01:00:58 +01:00
|
|
|
crypto_generate_dynamic_dh_modulus(void)
|
2011-11-24 22:06:50 +01:00
|
|
|
{
|
2011-11-25 01:00:58 +01:00
|
|
|
BIGNUM *dynamic_dh_modulus;
|
2011-11-24 22:06:50 +01:00
|
|
|
DH *dh_parameters;
|
2011-11-22 15:08:27 +01:00
|
|
|
int r, dh_codes;
|
2011-11-22 13:55:07 +01:00
|
|
|
char *s;
|
2011-11-24 22:06:50 +01:00
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
dynamic_dh_modulus = BN_new();
|
|
|
|
tor_assert(dynamic_dh_modulus);
|
2011-11-22 15:08:27 +01:00
|
|
|
|
|
|
|
dh_parameters = DH_generate_parameters(DH_BYTES*8, DH_GENERATOR, NULL, NULL);
|
2011-11-24 22:06:50 +01:00
|
|
|
tor_assert(dh_parameters);
|
2011-11-22 15:08:27 +01:00
|
|
|
|
2011-11-24 22:06:50 +01:00
|
|
|
r = DH_check(dh_parameters, &dh_codes);
|
2011-11-25 00:33:40 +01:00
|
|
|
tor_assert(r && !dh_codes);
|
2011-11-22 15:08:27 +01:00
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
BN_copy(dynamic_dh_modulus, dh_parameters->p);
|
|
|
|
tor_assert(dynamic_dh_modulus);
|
2011-11-22 15:08:27 +01:00
|
|
|
|
2011-11-24 22:06:50 +01:00
|
|
|
DH_free(dh_parameters);
|
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
{ /* log the dynamic DH modulus: */
|
|
|
|
s = BN_bn2hex(dynamic_dh_modulus);
|
2011-11-22 15:08:27 +01:00
|
|
|
tor_assert(s);
|
2011-11-25 01:00:58 +01:00
|
|
|
log_info(LD_OR, "Dynamic DH modulus generated: [%s]", s);
|
2011-11-22 13:55:07 +01:00
|
|
|
OPENSSL_free(s);
|
|
|
|
}
|
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
return dynamic_dh_modulus;
|
2011-11-24 22:06:50 +01:00
|
|
|
}
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
/** Store our dynamic DH modulus (and its group parameters) to
|
|
|
|
<b>fname</b> for future use. */
|
2011-11-25 17:39:28 +01:00
|
|
|
static int
|
2011-11-25 01:00:58 +01:00
|
|
|
crypto_store_dynamic_dh_modulus(const char *fname)
|
2011-11-24 00:22:31 +01:00
|
|
|
{
|
2011-11-26 19:29:57 +01:00
|
|
|
int len, new_len;
|
|
|
|
DH *dh = NULL;
|
2013-06-06 13:13:24 +02:00
|
|
|
unsigned char *dh_string_repr = NULL;
|
2011-11-26 19:29:57 +01:00
|
|
|
char *base64_encoded_dh = NULL;
|
2011-12-19 16:06:22 +01:00
|
|
|
char *file_string = NULL;
|
2011-11-24 22:32:10 +01:00
|
|
|
int retval = -1;
|
2012-01-10 22:53:37 +01:00
|
|
|
static const char file_header[] = "# This file contains stored Diffie-"
|
|
|
|
"Hellman parameters for future use.\n# You *do not* need to edit this "
|
|
|
|
"file.\n\n";
|
2011-11-24 22:32:10 +01:00
|
|
|
|
|
|
|
tor_assert(fname);
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
if (!dh_param_p_tls) {
|
|
|
|
log_info(LD_CRYPTO, "Tried to store a DH modulus that does not exist.");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-11-25 00:33:40 +01:00
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
if (!(dh = DH_new()))
|
|
|
|
goto done;
|
|
|
|
if (!(dh->p = BN_dup(dh_param_p_tls)))
|
|
|
|
goto done;
|
|
|
|
if (!(dh->g = BN_new()))
|
2011-11-24 22:32:10 +01:00
|
|
|
goto done;
|
2011-11-26 19:29:57 +01:00
|
|
|
if (!BN_set_word(dh->g, DH_GENERATOR))
|
|
|
|
goto done;
|
|
|
|
|
2013-06-06 13:13:24 +02:00
|
|
|
len = i2d_DHparams(dh, &dh_string_repr);
|
|
|
|
if ((len < 0) || (dh_string_repr == NULL)) {
|
2011-11-26 19:29:57 +01:00
|
|
|
log_warn(LD_CRYPTO, "Error occured while DER encoding DH modulus (2).");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-11-24 22:32:10 +01:00
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
base64_encoded_dh = tor_malloc_zero(len * 2); /* should be enough */
|
|
|
|
new_len = base64_encode(base64_encoded_dh, len * 2,
|
|
|
|
(char *)dh_string_repr, len);
|
|
|
|
if (new_len < 0) {
|
|
|
|
log_warn(LD_CRYPTO, "Error occured while base64-encoding DH modulus.");
|
2011-11-24 22:32:10 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-12-19 16:06:22 +01:00
|
|
|
/* concatenate file header and the dh parameters blob */
|
2012-01-10 22:53:37 +01:00
|
|
|
new_len = tor_asprintf(&file_string, "%s%s", file_header, base64_encoded_dh);
|
2011-12-19 16:06:22 +01:00
|
|
|
|
|
|
|
/* write to file */
|
2012-01-10 22:53:37 +01:00
|
|
|
if (write_bytes_to_new_file(fname, file_string, new_len, 0) < 0) {
|
2011-11-26 19:29:57 +01:00
|
|
|
log_info(LD_CRYPTO, "'%s' was already occupied.", fname);
|
2011-11-24 22:32:10 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
done:
|
2011-11-26 19:29:57 +01:00
|
|
|
if (dh)
|
|
|
|
DH_free(dh);
|
2013-06-10 19:49:13 +02:00
|
|
|
if (dh_string_repr)
|
|
|
|
OPENSSL_free(dh_string_repr);
|
2011-11-26 19:29:57 +01:00
|
|
|
tor_free(base64_encoded_dh);
|
2011-12-19 16:06:22 +01:00
|
|
|
tor_free(file_string);
|
2011-11-24 22:32:10 +01:00
|
|
|
|
|
|
|
return retval;
|
2011-11-24 00:22:31 +01:00
|
|
|
}
|
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
/** Return the dynamic DH modulus stored in <b>fname</b>. If there is no
|
|
|
|
dynamic DH modulus stored in <b>fname</b>, return NULL. */
|
2011-11-24 22:59:01 +01:00
|
|
|
static BIGNUM *
|
2011-11-25 01:00:58 +01:00
|
|
|
crypto_get_stored_dynamic_dh_modulus(const char *fname)
|
2011-11-24 22:59:01 +01:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
char *contents = NULL;
|
2011-12-19 16:06:22 +01:00
|
|
|
const char *contents_tmp = NULL;
|
2011-11-25 00:33:40 +01:00
|
|
|
int dh_codes;
|
2011-11-26 19:29:57 +01:00
|
|
|
DH *stored_dh = NULL;
|
|
|
|
BIGNUM *dynamic_dh_modulus = NULL;
|
|
|
|
int length = 0;
|
|
|
|
unsigned char *base64_decoded_dh = NULL;
|
|
|
|
const unsigned char *cp = NULL;
|
2011-11-24 22:59:01 +01:00
|
|
|
|
|
|
|
tor_assert(fname);
|
|
|
|
|
|
|
|
contents = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
|
2011-11-26 19:29:57 +01:00
|
|
|
if (!contents) {
|
|
|
|
log_info(LD_CRYPTO, "Could not open file '%s'", fname);
|
|
|
|
goto done; /*usually means that ENOENT. don't try to move file to broken.*/
|
|
|
|
}
|
|
|
|
|
2011-12-19 16:06:22 +01:00
|
|
|
/* skip the file header */
|
|
|
|
contents_tmp = eat_whitespace(contents);
|
|
|
|
if (!*contents_tmp) {
|
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH modulus file "
|
|
|
|
"seems corrupted (eat_whitespace).");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
/* 'fname' contains the DH parameters stored in base64-ed DER
|
2011-12-02 06:15:31 +01:00
|
|
|
* format. We are only interested in the DH modulus.
|
|
|
|
* NOTE: We allocate more storage here than we need. Since we're already
|
|
|
|
* doing that, we can also add 1 byte extra to appease Coverity's
|
|
|
|
* scanner. */
|
2011-11-24 22:59:01 +01:00
|
|
|
|
2011-12-19 16:06:22 +01:00
|
|
|
cp = base64_decoded_dh = tor_malloc_zero(strlen(contents_tmp) + 1);
|
|
|
|
length = base64_decode((char *)base64_decoded_dh, strlen(contents_tmp),
|
|
|
|
contents_tmp, strlen(contents_tmp));
|
2011-11-26 19:29:57 +01:00
|
|
|
if (length < 0) {
|
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH modulus seems corrupted (base64).");
|
2011-11-24 22:59:01 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
stored_dh = d2i_DHparams(NULL, &cp, length);
|
|
|
|
if ((!stored_dh) || (cp - base64_decoded_dh != length)) {
|
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH modulus seems corrupted (d2i).");
|
|
|
|
goto err;
|
|
|
|
}
|
2011-11-25 00:33:40 +01:00
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
{ /* check the cryptographic qualities of the stored dynamic DH modulus: */
|
|
|
|
retval = DH_check(stored_dh, &dh_codes);
|
2011-11-25 00:33:40 +01:00
|
|
|
if (!retval || dh_codes) {
|
2011-11-26 19:29:57 +01:00
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH modulus is not a safe prime.");
|
2011-11-25 00:33:40 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
retval = DH_size(stored_dh);
|
2011-11-25 00:33:40 +01:00
|
|
|
if (retval < DH_BYTES) {
|
2011-11-26 19:29:57 +01:00
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH modulus is smaller "
|
2011-11-25 00:33:40 +01:00
|
|
|
"than '%d' bits.", DH_BYTES*8);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-11-26 19:29:57 +01:00
|
|
|
|
|
|
|
if (!BN_is_word(stored_dh->g, 2)) {
|
|
|
|
log_warn(LD_CRYPTO, "Stored dynamic DH parameters do not use '2' "
|
|
|
|
"as the group generator.");
|
|
|
|
goto err;
|
|
|
|
}
|
2011-11-25 00:33:40 +01:00
|
|
|
}
|
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
{ /* log the dynamic DH modulus: */
|
2011-11-26 19:29:57 +01:00
|
|
|
char *s = BN_bn2hex(stored_dh->p);
|
2011-11-24 22:59:01 +01:00
|
|
|
tor_assert(s);
|
2011-11-25 01:00:58 +01:00
|
|
|
log_info(LD_OR, "Found stored dynamic DH modulus: [%s]", s);
|
2011-11-24 22:59:01 +01:00
|
|
|
OPENSSL_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
2011-11-25 17:39:45 +01:00
|
|
|
|
2012-05-27 18:28:15 +02:00
|
|
|
{
|
|
|
|
/* move broken prime to $filename.broken */
|
|
|
|
char *fname_new=NULL;
|
|
|
|
tor_asprintf(&fname_new, "%s.broken", fname);
|
2011-11-25 17:39:45 +01:00
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
log_warn(LD_CRYPTO, "Moving broken dynamic DH prime to '%s'.", fname_new);
|
2011-11-25 17:39:45 +01:00
|
|
|
|
|
|
|
if (replace_file(fname, fname_new))
|
2011-11-26 19:29:57 +01:00
|
|
|
log_notice(LD_CRYPTO, "Error while moving '%s' to '%s'.",
|
|
|
|
fname, fname_new);
|
2011-11-25 17:39:45 +01:00
|
|
|
|
|
|
|
tor_free(fname_new);
|
|
|
|
}
|
|
|
|
|
2011-11-26 19:29:57 +01:00
|
|
|
if (stored_dh) {
|
|
|
|
DH_free(stored_dh);
|
|
|
|
stored_dh = NULL;
|
2011-11-24 22:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(contents);
|
2011-11-26 19:29:57 +01:00
|
|
|
tor_free(base64_decoded_dh);
|
|
|
|
|
|
|
|
if (stored_dh) {
|
|
|
|
dynamic_dh_modulus = BN_dup(stored_dh->p);
|
|
|
|
DH_free(stored_dh);
|
|
|
|
}
|
2011-11-24 22:59:01 +01:00
|
|
|
|
2011-11-25 01:00:58 +01:00
|
|
|
return dynamic_dh_modulus;
|
2011-11-24 22:59:01 +01:00
|
|
|
}
|
|
|
|
|
2011-11-23 23:39:46 +01:00
|
|
|
/** Set the global TLS Diffie-Hellman modulus.
|
2011-11-25 01:00:58 +01:00
|
|
|
* If <b>dynamic_dh_modulus_fname</b> is set, try to read a dynamic DH modulus
|
2011-11-24 22:59:01 +01:00
|
|
|
* off it and use it as the DH modulus. If that's not possible,
|
2011-11-25 01:00:58 +01:00
|
|
|
* generate a new dynamic DH modulus.
|
|
|
|
* If <b>dynamic_dh_modulus_fname</b> is NULL, use the Apache mod_ssl DH
|
2011-11-24 22:59:01 +01:00
|
|
|
* modulus. */
|
2011-11-23 23:39:46 +01:00
|
|
|
void
|
2011-11-25 01:00:58 +01:00
|
|
|
crypto_set_tls_dh_prime(const char *dynamic_dh_modulus_fname)
|
2011-11-23 23:39:46 +01:00
|
|
|
{
|
|
|
|
BIGNUM *tls_prime = NULL;
|
2011-11-25 17:39:28 +01:00
|
|
|
int store_dh_prime_afterwards = 0;
|
2011-11-24 00:22:31 +01:00
|
|
|
int r;
|
2011-11-23 23:39:46 +01:00
|
|
|
|
|
|
|
/* If the space is occupied, free the previous TLS DH prime */
|
|
|
|
if (dh_param_p_tls) {
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(dh_param_p_tls);
|
2011-11-23 23:39:46 +01:00
|
|
|
dh_param_p_tls = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 17:39:28 +01:00
|
|
|
if (dynamic_dh_modulus_fname) { /* use dynamic DH modulus: */
|
2011-11-25 01:00:58 +01:00
|
|
|
log_info(LD_OR, "Using stored dynamic DH modulus.");
|
|
|
|
tls_prime = crypto_get_stored_dynamic_dh_modulus(dynamic_dh_modulus_fname);
|
2011-11-24 22:59:01 +01:00
|
|
|
|
|
|
|
if (!tls_prime) {
|
2011-11-25 01:00:58 +01:00
|
|
|
log_notice(LD_OR, "Generating fresh dynamic DH modulus. "
|
2011-11-25 00:59:47 +01:00
|
|
|
"This might take a while...");
|
2011-11-25 01:00:58 +01:00
|
|
|
tls_prime = crypto_generate_dynamic_dh_modulus();
|
2011-11-25 17:39:28 +01:00
|
|
|
|
|
|
|
store_dh_prime_afterwards++;
|
2011-11-23 23:39:46 +01:00
|
|
|
}
|
|
|
|
} else { /* use the static DH prime modulus used by Apache in mod_ssl: */
|
|
|
|
tls_prime = BN_new();
|
|
|
|
tor_assert(tls_prime);
|
|
|
|
|
|
|
|
/* This is the 1024-bit safe prime that Apache uses for its DH stuff; see
|
|
|
|
* modules/ssl/ssl_engine_dh.c; Apache also uses a generator of 2 with this
|
|
|
|
* prime.
|
|
|
|
*/
|
2011-11-25 01:08:31 +01:00
|
|
|
r =BN_hex2bn(&tls_prime,
|
|
|
|
"D67DE440CBBBDC1936D693D34AFD0AD50C84D239A45F520BB88174CB98"
|
|
|
|
"BCE951849F912E639C72FB13B4B4D7177E16D55AC179BA420B2A29FE324A"
|
|
|
|
"467A635E81FF5901377BEDDCFD33168A461AAD3B72DAE8860078045B07A7"
|
|
|
|
"DBCA7874087D1510EA9FCC9DDD330507DD62DB88AEAA747DE0F4D6E2BD68"
|
|
|
|
"B0E7393E0F24218EB3");
|
2011-11-23 23:39:46 +01:00
|
|
|
tor_assert(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(tls_prime);
|
|
|
|
|
|
|
|
dh_param_p_tls = tls_prime;
|
2011-11-25 17:39:28 +01:00
|
|
|
|
|
|
|
if (store_dh_prime_afterwards)
|
|
|
|
/* save the new dynamic DH modulus to disk. */
|
|
|
|
if (crypto_store_dynamic_dh_modulus(dynamic_dh_modulus_fname)) {
|
2011-11-26 19:29:57 +01:00
|
|
|
log_notice(LD_CRYPTO, "Failed while storing dynamic DH modulus. "
|
2011-11-25 17:39:28 +01:00
|
|
|
"Make sure your data directory is sane.");
|
|
|
|
}
|
2011-11-23 23:39:46 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Initialize dh_param_p and dh_param_g if they are not already
|
2004-05-01 23:41:23 +02:00
|
|
|
* set. */
|
2005-09-30 03:09:52 +02:00
|
|
|
static void
|
|
|
|
init_dh_param(void)
|
|
|
|
{
|
2011-11-23 23:39:46 +01:00
|
|
|
BIGNUM *circuit_dh_prime, *generator;
|
2005-08-07 22:36:14 +02:00
|
|
|
int r;
|
2011-11-23 23:39:46 +01:00
|
|
|
if (dh_param_p && dh_param_g)
|
2003-05-01 02:53:46 +02:00
|
|
|
return;
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2011-11-22 15:08:27 +01:00
|
|
|
circuit_dh_prime = BN_new();
|
|
|
|
generator = BN_new();
|
|
|
|
tor_assert(circuit_dh_prime && generator);
|
2003-05-01 02:53:46 +02:00
|
|
|
|
2011-11-24 22:06:50 +01:00
|
|
|
/* Set our generator for all DH parameters */
|
2011-11-22 15:08:27 +01:00
|
|
|
r = BN_set_word(generator, DH_GENERATOR);
|
2011-11-24 22:06:50 +01:00
|
|
|
tor_assert(r);
|
2003-05-01 02:53:46 +02:00
|
|
|
|
2003-05-07 04:28:42 +02:00
|
|
|
/* This is from rfc2409, section 6.2. It's a safe prime, and
|
|
|
|
supposedly it equals:
|
|
|
|
2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }.
|
|
|
|
*/
|
2011-11-22 15:08:27 +01:00
|
|
|
r = BN_hex2bn(&circuit_dh_prime,
|
2003-12-16 09:13:26 +01:00
|
|
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
|
|
|
|
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
|
|
|
|
"302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
|
|
|
|
"A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
|
|
|
|
"49286651ECE65381FFFFFFFFFFFFFFFF");
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(r);
|
2003-05-01 02:53:46 +02:00
|
|
|
|
2011-11-22 15:08:27 +01:00
|
|
|
/* Set the new values as the global DH parameters. */
|
|
|
|
dh_param_p = circuit_dh_prime;
|
|
|
|
dh_param_g = generator;
|
2011-11-23 23:39:46 +01:00
|
|
|
|
2011-12-12 20:25:55 +01:00
|
|
|
/* Ensure that we have TLS DH parameters set up, too, even if we're
|
|
|
|
going to change them soon. */
|
|
|
|
if (!dh_param_p_tls) {
|
|
|
|
crypto_set_tls_dh_prime(NULL);
|
|
|
|
}
|
2003-05-01 02:53:46 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Number of bits to use when choosing the x or y value in a Diffie-Hellman
|
|
|
|
* handshake. Since we exponentiate by this value, choosing a smaller one
|
|
|
|
* lets our handhake go faster.
|
|
|
|
*/
|
2005-11-14 20:18:31 +01:00
|
|
|
#define DH_PRIVATE_KEY_BITS 320
|
|
|
|
|
2004-10-12 21:09:40 +02:00
|
|
|
/** Allocate and return a new DH object for a key exchange.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_t *
|
2011-01-24 22:03:14 +01:00
|
|
|
crypto_dh_new(int dh_type)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_t *res = tor_malloc_zero(sizeof(crypto_dh_t));
|
2003-05-01 02:53:46 +02:00
|
|
|
|
2011-01-24 22:03:14 +01:00
|
|
|
tor_assert(dh_type == DH_TYPE_CIRCUIT || dh_type == DH_TYPE_TLS ||
|
|
|
|
dh_type == DH_TYPE_REND);
|
|
|
|
|
2003-05-01 02:53:46 +02:00
|
|
|
if (!dh_param_p)
|
|
|
|
init_dh_param();
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2003-05-01 02:53:46 +02:00
|
|
|
if (!(res->dh = DH_new()))
|
|
|
|
goto err;
|
|
|
|
|
2011-01-24 22:03:14 +01:00
|
|
|
if (dh_type == DH_TYPE_TLS) {
|
|
|
|
if (!(res->dh->p = BN_dup(dh_param_p_tls)))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
if (!(res->dh->p = BN_dup(dh_param_p)))
|
|
|
|
goto err;
|
|
|
|
}
|
2003-05-01 02:53:46 +02:00
|
|
|
|
|
|
|
if (!(res->dh->g = BN_dup(dh_param_g)))
|
|
|
|
goto err;
|
|
|
|
|
2005-11-14 20:18:31 +01:00
|
|
|
res->dh->length = DH_PRIVATE_KEY_BITS;
|
|
|
|
|
2003-05-01 02:53:46 +02:00
|
|
|
return res;
|
|
|
|
err:
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "creating DH object");
|
2008-09-05 22:18:22 +02:00
|
|
|
if (res->dh) DH_free(res->dh); /* frees p and g too */
|
|
|
|
tor_free(res);
|
2003-05-01 02:53:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2012-11-28 21:38:37 +01:00
|
|
|
/** Return a copy of <b>dh</b>, sharing its internal state. */
|
|
|
|
crypto_dh_t *
|
|
|
|
crypto_dh_dup(const crypto_dh_t *dh)
|
|
|
|
{
|
|
|
|
crypto_dh_t *dh_new = tor_malloc_zero(sizeof(crypto_dh_t));
|
|
|
|
dh_new->dh = dh->dh;
|
|
|
|
DH_up_ref(dh->dh);
|
|
|
|
return dh_new;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Return the length of the DH key in <b>dh</b>, in bytes.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_get_bytes(crypto_dh_t *dh)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(dh);
|
2003-05-01 02:53:46 +02:00
|
|
|
return DH_size(dh->dh);
|
|
|
|
}
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Generate \<x,g^x\> for our part of the key exchange. Return 0 on
|
2004-05-01 23:41:23 +02:00
|
|
|
* success, -1 on failure.
|
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_generate_public(crypto_dh_t *dh)
|
2004-04-01 22:04:54 +02:00
|
|
|
{
|
2005-08-07 22:36:14 +02:00
|
|
|
again:
|
2004-04-26 20:09:50 +02:00
|
|
|
if (!DH_generate_key(dh->dh)) {
|
|
|
|
crypto_log_errors(LOG_WARN, "generating DH key");
|
2004-04-01 22:04:54 +02:00
|
|
|
return -1;
|
2004-04-26 20:09:50 +02:00
|
|
|
}
|
2009-10-26 07:47:05 +01:00
|
|
|
if (tor_check_dh_key(LOG_WARN, dh->dh->pub_key)<0) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Weird! Our own DH key was invalid. I guess once-in-"
|
|
|
|
"the-universe chances really do happen. Trying again.");
|
2009-05-27 23:55:51 +02:00
|
|
|
/* Free and clear the keys, so OpenSSL will actually try again. */
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(dh->dh->pub_key);
|
|
|
|
BN_clear_free(dh->dh->priv_key);
|
2005-08-07 22:36:14 +02:00
|
|
|
dh->dh->pub_key = dh->dh->priv_key = NULL;
|
|
|
|
goto again;
|
|
|
|
}
|
2004-04-01 22:04:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Generate g^x as necessary, and write the g^x for the key exchange
|
|
|
|
* as a <b>pubkey_len</b>-byte value into <b>pubkey</b>. Return 0 on
|
|
|
|
* success, -1 on failure. <b>pubkey_len</b> must be \>= DH_BYTES.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_get_public(crypto_dh_t *dh, char *pubkey, size_t pubkey_len)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
|
|
|
int bytes;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(dh);
|
2004-04-01 22:04:54 +02:00
|
|
|
if (!dh->dh->pub_key) {
|
2004-04-26 20:09:50 +02:00
|
|
|
if (crypto_dh_generate_public(dh)<0)
|
2004-04-01 22:04:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(dh->dh->pub_key);
|
2003-05-01 02:53:46 +02:00
|
|
|
bytes = BN_num_bytes(dh->dh->pub_key);
|
2004-10-12 22:20:19 +02:00
|
|
|
tor_assert(bytes >= 0);
|
2005-10-17 18:21:42 +02:00
|
|
|
if (pubkey_len < (size_t)bytes) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO,
|
|
|
|
"Weird! pubkey_len (%d) was smaller than DH_BYTES (%d)",
|
|
|
|
(int) pubkey_len, bytes);
|
2003-05-01 02:53:46 +02:00
|
|
|
return -1;
|
2005-10-17 18:21:42 +02:00
|
|
|
}
|
2003-12-16 06:29:04 +01:00
|
|
|
|
2003-05-01 02:53:46 +02:00
|
|
|
memset(pubkey, 0, pubkey_len);
|
2005-05-07 07:55:06 +02:00
|
|
|
BN_bn2bin(dh->dh->pub_key, (unsigned char*)(pubkey+(pubkey_len-bytes)));
|
2003-05-01 02:53:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-07-30 21:10:20 +02:00
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** Check for bad Diffie-Hellman public keys (g^x). Return 0 if the key is
|
2005-12-07 00:09:44 +01:00
|
|
|
* okay (in the subgroup [2,p-2]), or -1 if it's bad.
|
2005-08-15 03:03:50 +02:00
|
|
|
* See http://www.cl.cam.ac.uk/ftp/users/rja14/psandqs.ps.gz for some tips.
|
2005-08-07 22:36:14 +02:00
|
|
|
*/
|
2005-08-05 01:14:42 +02:00
|
|
|
static int
|
2009-10-26 07:47:05 +01:00
|
|
|
tor_check_dh_key(int severity, BIGNUM *bn)
|
2005-08-05 01:14:42 +02:00
|
|
|
{
|
2005-12-07 00:09:44 +01:00
|
|
|
BIGNUM *x;
|
2005-08-07 22:36:14 +02:00
|
|
|
char *s;
|
2005-08-05 01:14:42 +02:00
|
|
|
tor_assert(bn);
|
2005-08-07 22:36:14 +02:00
|
|
|
x = BN_new();
|
2005-12-07 00:09:44 +01:00
|
|
|
tor_assert(x);
|
2005-08-05 01:14:42 +02:00
|
|
|
if (!dh_param_p)
|
|
|
|
init_dh_param();
|
2005-12-07 00:09:44 +01:00
|
|
|
BN_set_word(x, 1);
|
2005-08-07 22:36:14 +02:00
|
|
|
if (BN_cmp(bn,x)<=0) {
|
2009-10-26 07:47:05 +01:00
|
|
|
log_fn(severity, LD_CRYPTO, "DH key must be at least 2.");
|
2005-08-07 22:36:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BN_copy(x,dh_param_p);
|
2005-12-07 00:09:44 +01:00
|
|
|
BN_sub_word(x, 1);
|
2005-08-07 22:36:14 +02:00
|
|
|
if (BN_cmp(bn,x)>=0) {
|
2009-10-26 07:47:05 +01:00
|
|
|
log_fn(severity, LD_CRYPTO, "DH key must be at most p-2.");
|
2005-08-07 22:36:14 +02:00
|
|
|
goto err;
|
|
|
|
}
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(x);
|
2005-08-05 01:14:42 +02:00
|
|
|
return 0;
|
2005-08-07 22:36:14 +02:00
|
|
|
err:
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(x);
|
2005-08-07 22:36:14 +02:00
|
|
|
s = BN_bn2hex(bn);
|
2009-10-26 07:47:05 +01:00
|
|
|
log_fn(severity, LD_CRYPTO, "Rejecting insecure DH key [%s]", s);
|
2005-08-07 22:36:14 +02:00
|
|
|
OPENSSL_free(s);
|
|
|
|
return -1;
|
2005-08-05 01:14:42 +02:00
|
|
|
}
|
|
|
|
|
2003-07-30 21:10:20 +02:00
|
|
|
#undef MIN
|
|
|
|
#define MIN(a,b) ((a)<(b)?(a):(b))
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Given a DH key exchange object, and our peer's value of g^y (as a
|
2004-10-07 22:58:53 +02:00
|
|
|
* <b>pubkey_len</b>-byte value in <b>pubkey</b>) generate
|
2004-05-10 05:53:24 +02:00
|
|
|
* <b>secret_bytes_out</b> bytes of shared key material and write them
|
2004-11-02 03:28:51 +01:00
|
|
|
* to <b>secret_out</b>. Return the number of bytes generated on success,
|
2004-10-07 22:58:53 +02:00
|
|
|
* or -1 on failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*
|
|
|
|
* (We generate key material by computing
|
2004-05-02 01:29:20 +02:00
|
|
|
* SHA1( g^xy || "\x00" ) || SHA1( g^xy || "\x01" ) || ...
|
2004-05-01 23:41:23 +02:00
|
|
|
* where || is concatenation.)
|
|
|
|
*/
|
2008-02-21 22:57:47 +01:00
|
|
|
ssize_t
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
|
2005-09-30 03:09:52 +02:00
|
|
|
const char *pubkey, size_t pubkey_len,
|
|
|
|
char *secret_out, size_t secret_bytes_out)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
2005-05-07 07:55:06 +02:00
|
|
|
char *secret_tmp = NULL;
|
2003-07-30 21:10:20 +02:00
|
|
|
BIGNUM *pubkey_bn = NULL;
|
2011-01-15 17:22:25 +01:00
|
|
|
size_t secret_len=0, secret_tmp_len=0;
|
2004-10-12 22:20:19 +02:00
|
|
|
int result=0;
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(dh);
|
|
|
|
tor_assert(secret_bytes_out/DIGEST_LEN <= 255);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(pubkey_len < INT_MAX);
|
2003-07-30 21:10:20 +02:00
|
|
|
|
2008-02-21 22:57:47 +01:00
|
|
|
if (!(pubkey_bn = BN_bin2bn((const unsigned char*)pubkey,
|
|
|
|
(int)pubkey_len, NULL)))
|
2003-07-30 21:10:20 +02:00
|
|
|
goto error;
|
2009-10-26 07:47:05 +01:00
|
|
|
if (tor_check_dh_key(severity, pubkey_bn)<0) {
|
2005-08-05 01:14:42 +02:00
|
|
|
/* Check for invalid public keys. */
|
2009-10-26 07:47:05 +01:00
|
|
|
log_fn(severity, LD_CRYPTO,"Rejected invalid g^x");
|
2005-08-05 01:14:42 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2011-01-15 17:22:25 +01:00
|
|
|
secret_tmp_len = crypto_dh_get_bytes(dh);
|
|
|
|
secret_tmp = tor_malloc(secret_tmp_len);
|
2005-05-07 07:55:06 +02:00
|
|
|
result = DH_compute_key((unsigned char*)secret_tmp, pubkey_bn, dh->dh);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (result < 0) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO,"DH_compute_key() failed.");
|
2004-10-12 22:20:19 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
secret_len = result;
|
2012-12-03 18:20:05 +01:00
|
|
|
if (crypto_expand_key_material_TAP((uint8_t*)secret_tmp, secret_len,
|
|
|
|
(uint8_t*)secret_out, secret_bytes_out)<0)
|
2005-12-08 18:38:32 +01:00
|
|
|
goto error;
|
2003-07-30 21:10:20 +02:00
|
|
|
secret_len = secret_bytes_out;
|
2003-05-01 02:53:46 +02:00
|
|
|
|
2003-07-30 21:10:20 +02:00
|
|
|
goto done;
|
|
|
|
error:
|
2004-10-12 22:20:19 +02:00
|
|
|
result = -1;
|
2003-07-30 21:10:20 +02:00
|
|
|
done:
|
2004-04-26 20:09:50 +02:00
|
|
|
crypto_log_errors(LOG_WARN, "completing DH handshake");
|
2003-07-30 21:10:20 +02:00
|
|
|
if (pubkey_bn)
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(pubkey_bn);
|
2011-01-15 17:22:25 +01:00
|
|
|
if (secret_tmp) {
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(secret_tmp, 0, secret_tmp_len);
|
2011-01-15 17:22:25 +01:00
|
|
|
tor_free(secret_tmp);
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (result < 0)
|
2004-10-12 22:20:19 +02:00
|
|
|
return result;
|
|
|
|
else
|
|
|
|
return secret_len;
|
2003-05-01 02:53:46 +02:00
|
|
|
}
|
2004-10-12 22:20:19 +02:00
|
|
|
|
2005-12-08 18:38:32 +01:00
|
|
|
/** Given <b>key_in_len</b> bytes of negotiated randomness in <b>key_in</b>
|
|
|
|
* ("K"), expand it into <b>key_out_len</b> bytes of negotiated key material in
|
2008-02-09 04:11:10 +01:00
|
|
|
* <b>key_out</b> by taking the first <b>key_out_len</b> bytes of
|
2005-12-08 18:38:32 +01:00
|
|
|
* H(K | [00]) | H(K | [01]) | ....
|
|
|
|
*
|
2012-12-03 18:20:05 +01:00
|
|
|
* This is the key expansion algorithm used in the "TAP" circuit extension
|
|
|
|
* mechanism; it shouldn't be used for new protocols.
|
|
|
|
*
|
2005-12-08 18:38:32 +01:00
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
2012-12-03 18:20:05 +01:00
|
|
|
crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len,
|
|
|
|
uint8_t *key_out, size_t key_out_len)
|
2005-12-08 18:38:32 +01:00
|
|
|
{
|
|
|
|
int i;
|
2012-12-03 18:20:05 +01:00
|
|
|
uint8_t *cp, *tmp = tor_malloc(key_in_len+1);
|
|
|
|
uint8_t digest[DIGEST_LEN];
|
2005-12-08 18:38:32 +01:00
|
|
|
|
|
|
|
/* If we try to get more than this amount of key data, we'll repeat blocks.*/
|
|
|
|
tor_assert(key_out_len <= DIGEST_LEN*256);
|
|
|
|
|
|
|
|
memcpy(tmp, key_in, key_in_len);
|
2007-06-06 15:02:22 +02:00
|
|
|
for (cp = key_out, i=0; cp < key_out+key_out_len;
|
|
|
|
++i, cp += DIGEST_LEN) {
|
2005-12-08 18:38:32 +01:00
|
|
|
tmp[key_in_len] = i;
|
2012-12-03 18:20:05 +01:00
|
|
|
if (crypto_digest((char*)digest, (const char *)tmp, key_in_len+1))
|
2005-12-08 18:38:32 +01:00
|
|
|
goto err;
|
2007-06-06 15:02:22 +02:00
|
|
|
memcpy(cp, digest, MIN(DIGEST_LEN, key_out_len-(cp-key_out)));
|
2005-12-08 18:38:32 +01:00
|
|
|
}
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(tmp, 0, key_in_len+1);
|
2005-12-08 18:38:32 +01:00
|
|
|
tor_free(tmp);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(digest, 0, sizeof(digest));
|
2005-12-08 18:38:32 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(tmp, 0, key_in_len+1);
|
2005-12-08 18:38:32 +01:00
|
|
|
tor_free(tmp);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(digest, 0, sizeof(digest));
|
2005-12-08 18:38:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-03 18:20:05 +01:00
|
|
|
/** Expand some secret key material according to RFC5869, using SHA256 as the
|
|
|
|
* underlying hash. The <b>key_in_len</b> bytes at <b>key_in</b> are the
|
|
|
|
* secret key material; the <b>salt_in_len</b> bytes at <b>salt_in</b> and the
|
|
|
|
* <b>info_in_len</b> bytes in <b>info_in_len</b> are the algorithm's "salt"
|
|
|
|
* and "info" parameters respectively. On success, write <b>key_out_len</b>
|
|
|
|
* bytes to <b>key_out</b> and return 0. On failure, return -1.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_expand_key_material_rfc5869_sha256(
|
|
|
|
const uint8_t *key_in, size_t key_in_len,
|
|
|
|
const uint8_t *salt_in, size_t salt_in_len,
|
|
|
|
const uint8_t *info_in, size_t info_in_len,
|
|
|
|
uint8_t *key_out, size_t key_out_len)
|
|
|
|
{
|
|
|
|
uint8_t prk[DIGEST256_LEN];
|
|
|
|
uint8_t tmp[DIGEST256_LEN + 128 + 1];
|
|
|
|
uint8_t mac[DIGEST256_LEN];
|
|
|
|
int i;
|
|
|
|
uint8_t *outp;
|
|
|
|
size_t tmp_len;
|
|
|
|
|
|
|
|
crypto_hmac_sha256((char*)prk,
|
|
|
|
(const char*)salt_in, salt_in_len,
|
|
|
|
(const char*)key_in, key_in_len);
|
|
|
|
|
|
|
|
/* If we try to get more than this amount of key data, we'll repeat blocks.*/
|
|
|
|
tor_assert(key_out_len <= DIGEST256_LEN * 256);
|
|
|
|
tor_assert(info_in_len <= 128);
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
outp = key_out;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
while (key_out_len) {
|
|
|
|
size_t n;
|
|
|
|
if (i > 1) {
|
|
|
|
memcpy(tmp, mac, DIGEST256_LEN);
|
|
|
|
memcpy(tmp+DIGEST256_LEN, info_in, info_in_len);
|
|
|
|
tmp[DIGEST256_LEN+info_in_len] = i;
|
|
|
|
tmp_len = DIGEST256_LEN + info_in_len + 1;
|
|
|
|
} else {
|
|
|
|
memcpy(tmp, info_in, info_in_len);
|
|
|
|
tmp[info_in_len] = i;
|
|
|
|
tmp_len = info_in_len + 1;
|
|
|
|
}
|
|
|
|
crypto_hmac_sha256((char*)mac,
|
|
|
|
(const char*)prk, DIGEST256_LEN,
|
|
|
|
(const char*)tmp, tmp_len);
|
|
|
|
n = key_out_len < DIGEST256_LEN ? key_out_len : DIGEST256_LEN;
|
|
|
|
memcpy(outp, mac, n);
|
|
|
|
key_out_len -= n;
|
|
|
|
outp += n;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
memwipe(tmp, 0, sizeof(tmp));
|
|
|
|
memwipe(mac, 0, sizeof(mac));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Free a DH key exchange object.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_dh_free(crypto_dh_t *dh)
|
2003-05-01 02:53:46 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!dh)
|
|
|
|
return;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(dh->dh);
|
2003-05-01 02:53:46 +02:00
|
|
|
DH_free(dh->dh);
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(dh);
|
2003-05-01 02:53:46 +02:00
|
|
|
}
|
|
|
|
|
2002-07-24 16:02:39 +02:00
|
|
|
/* random numbers */
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** How many bytes of entropy we add at once.
|
|
|
|
*
|
|
|
|
* This is how much entropy OpenSSL likes to add right now, so maybe it will
|
2005-10-07 21:03:09 +02:00
|
|
|
* work for us too. */
|
|
|
|
#define ADD_ENTROPY 32
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** True iff it's safe to use RAND_poll after setup.
|
|
|
|
*
|
|
|
|
* Versions of OpenSSL prior to 0.9.7k and 0.9.8c had a bug where RAND_poll
|
2008-03-21 20:18:57 +01:00
|
|
|
* would allocate an fd_set on the stack, open a new file, and try to FD_SET
|
|
|
|
* that fd without checking whether it fit in the fd_set. Thus, if the
|
|
|
|
* system has not just been started up, it is unsafe to call */
|
|
|
|
#define RAND_POLL_IS_SAFE \
|
2012-09-13 01:25:58 +02:00
|
|
|
(OPENSSL_VERSION_NUMBER >= OPENSSL_V(0,9,8,'c'))
|
2008-03-21 20:18:57 +01:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Set the seed of the weak RNG to a random value. */
|
2013-02-08 22:28:05 +01:00
|
|
|
void
|
|
|
|
crypto_seed_weak_rng(tor_weak_rng_t *rng)
|
2010-11-29 21:53:33 +01:00
|
|
|
{
|
|
|
|
unsigned seed;
|
|
|
|
crypto_rand((void*)&seed, sizeof(seed));
|
2013-02-08 22:28:05 +01:00
|
|
|
tor_init_weak_random(rng, seed);
|
2010-11-29 21:53:33 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:31:07 +01:00
|
|
|
/** Try to get <b>out_len</b> bytes of the strongest entropy we can generate,
|
|
|
|
* storing it into <b>out</b>.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2012-12-04 05:31:07 +01:00
|
|
|
crypto_strongest_rand(uint8_t *out, size_t out_len)
|
2004-03-11 06:14:06 +01:00
|
|
|
{
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2004-03-11 06:14:06 +01:00
|
|
|
static int provider_set = 0;
|
2004-03-11 07:19:08 +01:00
|
|
|
static HCRYPTPROV provider;
|
2005-10-07 21:03:09 +02:00
|
|
|
#else
|
|
|
|
static const char *filenames[] = {
|
|
|
|
"/dev/srandom", "/dev/urandom", "/dev/random", NULL
|
|
|
|
};
|
2008-02-21 22:57:42 +01:00
|
|
|
int fd, i;
|
|
|
|
size_t n;
|
2005-10-07 21:03:09 +02:00
|
|
|
#endif
|
2004-03-11 06:14:06 +01:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2004-03-11 06:14:06 +01:00
|
|
|
if (!provider_set) {
|
2005-12-14 21:40:40 +01:00
|
|
|
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT)) {
|
2007-02-28 21:24:27 +01:00
|
|
|
if ((unsigned long)GetLastError() != (unsigned long)NTE_BAD_KEYSET) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Can't get CryptoAPI provider [1]");
|
2012-12-04 05:31:07 +01:00
|
|
|
return -1;
|
2004-03-11 06:14:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
provider_set = 1;
|
|
|
|
}
|
2012-12-04 05:31:07 +01:00
|
|
|
if (!CryptGenRandom(provider, out_len, out)) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO, "Can't get entropy from CryptoAPI.");
|
2012-12-04 05:31:07 +01:00
|
|
|
return -1;
|
2004-03-11 06:14:06 +01:00
|
|
|
}
|
2012-12-04 05:31:07 +01:00
|
|
|
|
2004-03-11 06:14:06 +01:00
|
|
|
return 0;
|
|
|
|
#else
|
2003-06-13 23:13:37 +02:00
|
|
|
for (i = 0; filenames[i]; ++i) {
|
2014-04-16 19:17:09 +02:00
|
|
|
log_debug(LD_FS, "Opening %s for entropy", filenames[i]);
|
2013-08-09 18:07:20 +02:00
|
|
|
fd = open(sandbox_intern_string(filenames[i]), O_RDONLY, 0);
|
2004-04-26 20:09:50 +02:00
|
|
|
if (fd<0) continue;
|
2012-12-04 05:31:07 +01:00
|
|
|
log_info(LD_CRYPTO, "Reading entropy from \"%s\"", filenames[i]);
|
|
|
|
n = read_all(fd, (char*)out, out_len, 0);
|
2004-04-26 20:09:50 +02:00
|
|
|
close(fd);
|
2012-12-04 05:31:07 +01:00
|
|
|
if (n != out_len) {
|
2006-02-13 09:01:59 +01:00
|
|
|
log_warn(LD_CRYPTO,
|
2008-02-21 22:57:47 +01:00
|
|
|
"Error reading from entropy source (read only %lu bytes).",
|
|
|
|
(unsigned long)n);
|
2003-06-13 23:13:37 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-12-04 05:31:07 +01:00
|
|
|
|
2003-06-13 23:13:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-04 05:31:07 +01:00
|
|
|
log_warn(LD_CRYPTO, "Cannot get strong entropy: no entropy source found.");
|
|
|
|
return -1;
|
2004-03-11 06:14:06 +01:00
|
|
|
#endif
|
2004-05-01 23:41:23 +02:00
|
|
|
}
|
2003-06-13 23:13:37 +02:00
|
|
|
|
2012-12-04 05:31:07 +01:00
|
|
|
/** Seed OpenSSL's random number generator with bytes from the operating
|
|
|
|
* system. <b>startup</b> should be true iff we have just started Tor and
|
|
|
|
* have not yet allocated a bunch of fds. Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_seed_rng(int startup)
|
|
|
|
{
|
|
|
|
int rand_poll_ok = 0, load_entropy_ok = 0;
|
|
|
|
uint8_t buf[ADD_ENTROPY];
|
|
|
|
|
|
|
|
/* OpenSSL has a RAND_poll function that knows about more kinds of
|
|
|
|
* entropy than we do. We'll try calling that, *and* calling our own entropy
|
|
|
|
* functions. If one succeeds, we'll accept the RNG as seeded. */
|
|
|
|
if (startup || RAND_POLL_IS_SAFE) {
|
|
|
|
rand_poll_ok = RAND_poll();
|
|
|
|
if (rand_poll_ok == 0)
|
|
|
|
log_warn(LD_CRYPTO, "RAND_poll() failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
load_entropy_ok = !crypto_strongest_rand(buf, sizeof(buf));
|
|
|
|
if (load_entropy_ok) {
|
|
|
|
RAND_seed(buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
memwipe(buf, 0, sizeof(buf));
|
2013-02-08 22:28:05 +01:00
|
|
|
|
2012-12-04 05:31:07 +01:00
|
|
|
if (rand_poll_ok || load_entropy_ok)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Write <b>n</b> bytes of strong random data to <b>to</b>. Return 0 on
|
2004-05-10 05:53:24 +02:00
|
|
|
* success, -1 on failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2013-08-01 18:13:09 +02:00
|
|
|
MOCK_IMPL(int,
|
|
|
|
crypto_rand, (char *to, size_t n))
|
2002-07-24 16:02:39 +02:00
|
|
|
{
|
2004-04-26 20:09:50 +02:00
|
|
|
int r;
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(n < INT_MAX);
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(to);
|
2008-02-21 22:57:47 +01:00
|
|
|
r = RAND_bytes((unsigned char*)to, (int)n);
|
2004-04-26 20:09:50 +02:00
|
|
|
if (r == 0)
|
|
|
|
crypto_log_errors(LOG_WARN, "generating random data");
|
2004-05-01 23:41:23 +02:00
|
|
|
return (r == 1) ? 0 : -1;
|
2002-07-24 16:02:39 +02:00
|
|
|
}
|
2002-07-25 10:17:22 +02:00
|
|
|
|
2004-12-01 04:48:14 +01:00
|
|
|
/** Return a pseudorandom integer, chosen uniformly from the values
|
2011-06-01 17:48:39 +02:00
|
|
|
* between 0 and <b>max</b>-1 inclusive. <b>max</b> must be between 1 and
|
|
|
|
* INT_MAX+1, inclusive. */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2005-10-07 00:18:01 +02:00
|
|
|
crypto_rand_int(unsigned int max)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2003-11-12 05:12:35 +01:00
|
|
|
unsigned int val;
|
2003-11-12 05:28:30 +01:00
|
|
|
unsigned int cutoff;
|
2011-06-01 17:48:39 +02:00
|
|
|
tor_assert(max <= ((unsigned int)INT_MAX)+1);
|
2004-04-25 21:59:38 +02:00
|
|
|
tor_assert(max > 0); /* don't div by 0 */
|
2003-11-12 05:28:30 +01:00
|
|
|
|
|
|
|
/* We ignore any values that are >= 'cutoff,' to avoid biasing the
|
|
|
|
* distribution with clipping at the upper end of unsigned int's
|
|
|
|
* range.
|
|
|
|
*/
|
|
|
|
cutoff = UINT_MAX - (UINT_MAX%max);
|
2004-11-28 10:05:49 +01:00
|
|
|
while (1) {
|
2005-10-07 00:18:01 +02:00
|
|
|
crypto_rand((char*)&val, sizeof(val));
|
2006-10-01 23:59:05 +02:00
|
|
|
if (val < cutoff)
|
|
|
|
return val % max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Return a pseudorandom 64-bit integer, chosen uniformly from the values
|
|
|
|
* between 0 and <b>max</b>-1. */
|
2006-10-01 23:59:05 +02:00
|
|
|
uint64_t
|
|
|
|
crypto_rand_uint64(uint64_t max)
|
|
|
|
{
|
|
|
|
uint64_t val;
|
|
|
|
uint64_t cutoff;
|
|
|
|
tor_assert(max < UINT64_MAX);
|
|
|
|
tor_assert(max > 0); /* don't div by 0 */
|
|
|
|
|
|
|
|
/* We ignore any values that are >= 'cutoff,' to avoid biasing the
|
|
|
|
* distribution with clipping at the upper end of unsigned int's
|
|
|
|
* range.
|
|
|
|
*/
|
|
|
|
cutoff = UINT64_MAX - (UINT64_MAX%max);
|
|
|
|
while (1) {
|
|
|
|
crypto_rand((char*)&val, sizeof(val));
|
2003-11-12 05:28:30 +01:00
|
|
|
if (val < cutoff)
|
|
|
|
return val % max;
|
|
|
|
}
|
2002-07-25 10:17:22 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 03:30:26 +02:00
|
|
|
/** Return a pseudorandom double d, chosen uniformly from the range
|
|
|
|
* 0.0 <= d < 1.0.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
crypto_rand_double(void)
|
|
|
|
{
|
|
|
|
/* We just use an unsigned int here; we don't really care about getting
|
|
|
|
* more than 32 bits of resolution */
|
|
|
|
unsigned int uint;
|
2010-06-23 03:31:31 +02:00
|
|
|
crypto_rand((char*)&uint, sizeof(uint));
|
|
|
|
#if SIZEOF_INT == 4
|
|
|
|
#define UINT_MAX_AS_DOUBLE 4294967296.0
|
|
|
|
#elif SIZEOF_INT == 8
|
|
|
|
#define UINT_MAX_AS_DOUBLE 1.8446744073709552e+19
|
|
|
|
#else
|
|
|
|
#error SIZEOF_INT is neither 4 nor 8
|
|
|
|
#endif
|
|
|
|
return ((double)uint) / UINT_MAX_AS_DOUBLE;
|
2010-06-23 03:30:26 +02:00
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Generate and return a new random hostname starting with <b>prefix</b>,
|
2012-01-10 01:14:51 +01:00
|
|
|
* ending with <b>suffix</b>, and containing no fewer than
|
2008-02-09 04:11:10 +01:00
|
|
|
* <b>min_rand_len</b> and no more than <b>max_rand_len</b> random base32
|
2012-01-10 01:14:51 +01:00
|
|
|
* characters between.
|
|
|
|
*
|
|
|
|
* Clip <b>max_rand_len</b> to MAX_DNS_LABEL_SIZE.
|
|
|
|
**/
|
2008-02-08 22:13:12 +01:00
|
|
|
char *
|
|
|
|
crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix,
|
|
|
|
const char *suffix)
|
|
|
|
{
|
|
|
|
char *result, *rand_bytes;
|
2008-02-21 22:57:47 +01:00
|
|
|
int randlen, rand_bytes_len;
|
|
|
|
size_t resultlen, prefixlen;
|
2008-02-08 22:13:12 +01:00
|
|
|
|
2012-01-10 01:14:51 +01:00
|
|
|
if (max_rand_len > MAX_DNS_LABEL_SIZE)
|
|
|
|
max_rand_len = MAX_DNS_LABEL_SIZE;
|
|
|
|
if (min_rand_len > max_rand_len)
|
|
|
|
min_rand_len = max_rand_len;
|
2011-12-21 18:48:38 +01:00
|
|
|
|
2008-02-08 22:13:12 +01:00
|
|
|
randlen = min_rand_len + crypto_rand_int(max_rand_len - min_rand_len + 1);
|
2011-12-21 18:48:38 +01:00
|
|
|
|
2008-02-08 22:13:12 +01:00
|
|
|
prefixlen = strlen(prefix);
|
|
|
|
resultlen = prefixlen + strlen(suffix) + randlen + 16;
|
|
|
|
|
|
|
|
rand_bytes_len = ((randlen*5)+7)/8;
|
|
|
|
if (rand_bytes_len % 5)
|
|
|
|
rand_bytes_len += 5 - (rand_bytes_len%5);
|
|
|
|
rand_bytes = tor_malloc(rand_bytes_len);
|
|
|
|
crypto_rand(rand_bytes, rand_bytes_len);
|
|
|
|
|
|
|
|
result = tor_malloc(resultlen);
|
|
|
|
memcpy(result, prefix, prefixlen);
|
|
|
|
base32_encode(result+prefixlen, resultlen-prefixlen,
|
|
|
|
rand_bytes, rand_bytes_len);
|
|
|
|
tor_free(rand_bytes);
|
|
|
|
strlcpy(result+prefixlen+randlen, suffix, resultlen-(prefixlen+randlen));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Return a randomly chosen element of <b>sl</b>; or NULL if <b>sl</b>
|
|
|
|
* is empty. */
|
2005-09-30 03:09:52 +02:00
|
|
|
void *
|
|
|
|
smartlist_choose(const smartlist_t *sl)
|
|
|
|
{
|
2008-02-21 22:57:47 +01:00
|
|
|
int len = smartlist_len(sl);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (len)
|
2005-10-07 00:18:01 +02:00
|
|
|
return smartlist_get(sl,crypto_rand_int(len));
|
2004-10-30 21:26:31 +02:00
|
|
|
return NULL; /* no elements to choose from */
|
|
|
|
}
|
|
|
|
|
2008-02-09 04:11:10 +01:00
|
|
|
/** Scramble the elements of <b>sl</b> into a random order. */
|
2007-05-18 23:19:14 +02:00
|
|
|
void
|
|
|
|
smartlist_shuffle(smartlist_t *sl)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* From the end of the list to the front, choose at random from the
|
|
|
|
positions we haven't looked at yet, and swap that position into the
|
|
|
|
current position. Remember to give "no swap" the same probability as
|
|
|
|
any other swap. */
|
|
|
|
for (i = smartlist_len(sl)-1; i > 0; --i) {
|
|
|
|
int j = crypto_rand_int(i+1);
|
|
|
|
smartlist_swap(sl, i, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Base64 encode <b>srclen</b> bytes of data from <b>src</b>. Write
|
2004-05-10 05:53:24 +02:00
|
|
|
* the result into <b>dest</b>, if it will fit within <b>destlen</b>
|
|
|
|
* bytes. Return the number of bytes written on success; -1 if
|
|
|
|
* destlen is too short, or other failure.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
int
|
2004-10-12 22:20:19 +02:00
|
|
|
base64_encode(char *dest, size_t destlen, const char *src, size_t srclen)
|
2003-05-07 04:13:23 +02:00
|
|
|
{
|
2008-02-09 04:11:10 +01:00
|
|
|
/* FFFF we might want to rewrite this along the lines of base64_decode, if
|
2007-10-17 21:23:56 +02:00
|
|
|
* it ever shows up in the profile. */
|
2003-05-07 04:13:23 +02:00
|
|
|
EVP_ENCODE_CTX ctx;
|
|
|
|
int len, ret;
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert(srclen < INT_MAX);
|
2003-12-16 06:29:04 +01:00
|
|
|
|
|
|
|
/* 48 bytes of input -> 64 bytes of output plus newline.
|
2003-05-07 04:13:23 +02:00
|
|
|
Plus one more byte, in case I'm wrong.
|
|
|
|
*/
|
|
|
|
if (destlen < ((srclen/48)+1)*66)
|
|
|
|
return -1;
|
2004-12-02 05:33:01 +01:00
|
|
|
if (destlen > SIZE_T_CEILING)
|
|
|
|
return -1;
|
2003-05-07 04:13:23 +02:00
|
|
|
|
|
|
|
EVP_EncodeInit(&ctx);
|
2005-12-14 21:40:40 +01:00
|
|
|
EVP_EncodeUpdate(&ctx, (unsigned char*)dest, &len,
|
2008-02-21 22:57:47 +01:00
|
|
|
(unsigned char*)src, (int)srclen);
|
2005-05-07 07:55:06 +02:00
|
|
|
EVP_EncodeFinal(&ctx, (unsigned char*)(dest+len), &ret);
|
2003-05-07 04:13:23 +02:00
|
|
|
ret += len;
|
|
|
|
return ret;
|
|
|
|
}
|
2004-05-01 23:41:23 +02:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
|
|
|
/** Special values used for the base64_decode_table */
|
2007-10-17 21:23:56 +02:00
|
|
|
#define X 255
|
|
|
|
#define SP 64
|
|
|
|
#define PAD 65
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2007-10-17 21:23:56 +02:00
|
|
|
/** Internal table mapping byte values to what they represent in base64.
|
|
|
|
* Numbers 0..63 are 6-bit integers. SPs are spaces, and should be
|
|
|
|
* skipped. Xs are invalid and must not appear in base64. PAD indicates
|
|
|
|
* end-of-string. */
|
|
|
|
static const uint8_t base64_decode_table[256] = {
|
|
|
|
X, X, X, X, X, X, X, X, X, SP, SP, SP, X, SP, X, X, /* */
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
SP, X, X, X, X, X, X, X, X, X, X, 62, X, X, X, 63,
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, X, X, X, PAD, X, X,
|
|
|
|
X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, X, X, X, X, X,
|
|
|
|
X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,
|
|
|
|
};
|
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Base64 decode <b>srclen</b> bytes of data from <b>src</b>. Write
|
2004-05-10 05:53:24 +02:00
|
|
|
* the result into <b>dest</b>, if it will fit within <b>destlen</b>
|
|
|
|
* bytes. Return the number of bytes written on success; -1 if
|
|
|
|
* destlen is too short, or other failure.
|
2004-12-13 19:38:19 +01:00
|
|
|
*
|
2007-10-17 21:23:56 +02:00
|
|
|
* NOTE 1: destlen is checked conservatively, as though srclen contained no
|
|
|
|
* spaces or padding.
|
|
|
|
*
|
|
|
|
* NOTE 2: This implementation does not check for the correct number of
|
|
|
|
* padding "=" characters at the end of the string, and does not check
|
|
|
|
* for internal padding characters.
|
2004-05-01 23:41:23 +02:00
|
|
|
*/
|
2003-12-16 06:29:04 +01:00
|
|
|
int
|
2004-10-12 22:20:19 +02:00
|
|
|
base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
|
2003-05-07 04:13:23 +02:00
|
|
|
{
|
2007-10-17 21:23:56 +02:00
|
|
|
#ifdef USE_OPENSSL_BASE64
|
2003-05-07 04:13:23 +02:00
|
|
|
EVP_ENCODE_CTX ctx;
|
|
|
|
int len, ret;
|
|
|
|
/* 64 bytes of input -> *up to* 48 bytes of output.
|
2004-10-13 07:54:58 +02:00
|
|
|
Plus one more byte, in case I'm wrong.
|
2003-05-07 04:13:23 +02:00
|
|
|
*/
|
|
|
|
if (destlen < ((srclen/64)+1)*49)
|
|
|
|
return -1;
|
2004-12-02 05:33:01 +01:00
|
|
|
if (destlen > SIZE_T_CEILING)
|
|
|
|
return -1;
|
2003-05-07 04:13:23 +02:00
|
|
|
|
2014-12-22 18:56:35 +01:00
|
|
|
memset(dest, 0, destlen);
|
|
|
|
|
2003-05-07 04:13:23 +02:00
|
|
|
EVP_DecodeInit(&ctx);
|
2005-12-14 21:40:40 +01:00
|
|
|
EVP_DecodeUpdate(&ctx, (unsigned char*)dest, &len,
|
|
|
|
(unsigned char*)src, srclen);
|
2005-05-07 07:55:06 +02:00
|
|
|
EVP_DecodeFinal(&ctx, (unsigned char*)dest, &ret);
|
2003-05-07 04:13:23 +02:00
|
|
|
ret += len;
|
|
|
|
return ret;
|
2007-10-17 21:23:56 +02:00
|
|
|
#else
|
|
|
|
const char *eos = src+srclen;
|
|
|
|
uint32_t n=0;
|
|
|
|
int n_idx=0;
|
|
|
|
char *dest_orig = dest;
|
|
|
|
|
|
|
|
/* Max number of bits == srclen*6.
|
|
|
|
* Number of bytes required to hold all bits == (srclen*6)/8.
|
|
|
|
* Yes, we want to round down: anything that hangs over the end of a
|
|
|
|
* byte is padding. */
|
|
|
|
if (destlen < (srclen*3)/4)
|
|
|
|
return -1;
|
|
|
|
if (destlen > SIZE_T_CEILING)
|
|
|
|
return -1;
|
|
|
|
|
2014-12-22 18:56:35 +01:00
|
|
|
memset(dest, 0, destlen);
|
|
|
|
|
2007-10-17 21:23:56 +02:00
|
|
|
/* Iterate over all the bytes in src. Each one will add 0 or 6 bits to the
|
|
|
|
* value we're decoding. Accumulate bits in <b>n</b>, and whenever we have
|
|
|
|
* 24 bits, batch them into 3 bytes and flush those bytes to dest.
|
|
|
|
*/
|
|
|
|
for ( ; src < eos; ++src) {
|
|
|
|
unsigned char c = (unsigned char) *src;
|
|
|
|
uint8_t v = base64_decode_table[c];
|
|
|
|
switch (v) {
|
|
|
|
case X:
|
|
|
|
/* This character isn't allowed in base64. */
|
|
|
|
return -1;
|
|
|
|
case SP:
|
|
|
|
/* This character is whitespace, and has no effect. */
|
|
|
|
continue;
|
|
|
|
case PAD:
|
|
|
|
/* We've hit an = character: the data is over. */
|
|
|
|
goto end_of_loop;
|
|
|
|
default:
|
|
|
|
/* We have an actual 6-bit value. Append it to the bits in n. */
|
|
|
|
n = (n<<6) | v;
|
|
|
|
if ((++n_idx) == 4) {
|
|
|
|
/* We've accumulated 24 bits in n. Flush them. */
|
|
|
|
*dest++ = (n>>16);
|
|
|
|
*dest++ = (n>>8) & 0xff;
|
|
|
|
*dest++ = (n) & 0xff;
|
|
|
|
n_idx = 0;
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end_of_loop:
|
|
|
|
/* If we have leftover bits, we need to cope. */
|
|
|
|
switch (n_idx) {
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
/* No leftover bits. We win. */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* 6 leftover bits. That's invalid; we can't form a byte out of that. */
|
|
|
|
return -1;
|
|
|
|
case 2:
|
|
|
|
/* 12 leftover bits: The last 4 are padding and the first 8 are data. */
|
|
|
|
*dest++ = n >> 4;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
/* 18 leftover bits: The last 2 are padding and the first 16 are data. */
|
|
|
|
*dest++ = n >> 10;
|
|
|
|
*dest++ = n >> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert((dest-dest_orig) <= (ssize_t)destlen);
|
2008-02-21 22:57:47 +01:00
|
|
|
tor_assert((dest-dest_orig) <= INT_MAX);
|
2007-10-17 21:23:56 +02:00
|
|
|
|
2008-02-21 22:57:47 +01:00
|
|
|
return (int)(dest-dest_orig);
|
2007-10-17 21:23:56 +02:00
|
|
|
#endif
|
2003-05-07 04:13:23 +02:00
|
|
|
}
|
2007-10-17 21:23:56 +02:00
|
|
|
#undef X
|
|
|
|
#undef SP
|
2008-12-17 18:20:42 +01:00
|
|
|
#undef PAD
|
2004-03-30 21:47:32 +02:00
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Base64 encode DIGEST_LINE bytes from <b>digest</b>, remove the trailing =
|
2006-09-29 20:13:37 +02:00
|
|
|
* and newline characters, and store the nul-terminated result in the first
|
|
|
|
* BASE64_DIGEST_LEN+1 bytes of <b>d64</b>. */
|
2005-09-18 04:18:59 +02:00
|
|
|
int
|
|
|
|
digest_to_base64(char *d64, const char *digest)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
base64_encode(buf, sizeof(buf), digest, DIGEST_LEN);
|
|
|
|
buf[BASE64_DIGEST_LEN] = '\0';
|
|
|
|
memcpy(d64, buf, BASE64_DIGEST_LEN+1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
|
2006-09-29 20:13:37 +02:00
|
|
|
* trailing newline or = characters), decode it and store the result in the
|
|
|
|
* first DIGEST_LEN bytes at <b>digest</b>. */
|
2005-09-18 04:18:59 +02:00
|
|
|
int
|
|
|
|
digest_from_base64(char *digest, const char *d64)
|
|
|
|
{
|
2007-10-17 21:23:56 +02:00
|
|
|
#ifdef USE_OPENSSL_BASE64
|
2005-09-18 04:18:59 +02:00
|
|
|
char buf_in[BASE64_DIGEST_LEN+3];
|
|
|
|
char buf[256];
|
|
|
|
if (strlen(d64) != BASE64_DIGEST_LEN)
|
|
|
|
return -1;
|
|
|
|
memcpy(buf_in, d64, BASE64_DIGEST_LEN);
|
|
|
|
memcpy(buf_in+BASE64_DIGEST_LEN, "=\n\0", 3);
|
|
|
|
if (base64_decode(buf, sizeof(buf), buf_in, strlen(buf_in)) != DIGEST_LEN)
|
|
|
|
return -1;
|
|
|
|
memcpy(digest, buf, DIGEST_LEN);
|
|
|
|
return 0;
|
2007-10-17 21:23:56 +02:00
|
|
|
#else
|
|
|
|
if (base64_decode(digest, DIGEST_LEN, d64, strlen(d64)) == DIGEST_LEN)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2005-09-18 04:18:59 +02:00
|
|
|
}
|
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Base64 encode DIGEST256_LINE bytes from <b>digest</b>, remove the
|
2009-08-20 02:03:40 +02:00
|
|
|
* trailing = and newline characters, and store the nul-terminated result in
|
|
|
|
* the first BASE64_DIGEST256_LEN+1 bytes of <b>d64</b>. */
|
|
|
|
int
|
|
|
|
digest256_to_base64(char *d64, const char *digest)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
base64_encode(buf, sizeof(buf), digest, DIGEST256_LEN);
|
|
|
|
buf[BASE64_DIGEST256_LEN] = '\0';
|
|
|
|
memcpy(d64, buf, BASE64_DIGEST256_LEN+1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-17 22:08:28 +01:00
|
|
|
/** Given a base64 encoded, nul-terminated digest in <b>d64</b> (without
|
2009-08-20 02:03:40 +02:00
|
|
|
* trailing newline or = characters), decode it and store the result in the
|
|
|
|
* first DIGEST256_LEN bytes at <b>digest</b>. */
|
|
|
|
int
|
|
|
|
digest256_from_base64(char *digest, const char *d64)
|
|
|
|
{
|
|
|
|
#ifdef USE_OPENSSL_BASE64
|
|
|
|
char buf_in[BASE64_DIGEST256_LEN+3];
|
|
|
|
char buf[256];
|
|
|
|
if (strlen(d64) != BASE64_DIGEST256_LEN)
|
|
|
|
return -1;
|
|
|
|
memcpy(buf_in, d64, BASE64_DIGEST256_LEN);
|
|
|
|
memcpy(buf_in+BASE64_DIGEST256_LEN, "=\n\0", 3);
|
|
|
|
if (base64_decode(buf, sizeof(buf), buf_in, strlen(buf_in)) != DIGEST256_LEN)
|
|
|
|
return -1;
|
|
|
|
memcpy(digest, buf, DIGEST256_LEN);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
if (base64_decode(digest, DIGEST256_LEN, d64, strlen(d64)) == DIGEST256_LEN)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-23 15:51:31 +01:00
|
|
|
/** Implements base32 encoding as in RFC 4648. Limitation: Requires
|
2004-07-22 10:30:06 +02:00
|
|
|
* that srclen*8 is a multiple of 5.
|
2004-04-08 22:56:33 +02:00
|
|
|
*/
|
2004-07-22 10:30:06 +02:00
|
|
|
void
|
2004-10-12 22:20:19 +02:00
|
|
|
base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
|
2004-03-30 21:47:32 +02:00
|
|
|
{
|
2011-01-03 22:16:53 +01:00
|
|
|
unsigned int i, v, u;
|
|
|
|
size_t nbits = srclen * 8, bit;
|
2004-03-30 21:47:32 +02:00
|
|
|
|
2011-01-03 22:16:53 +01:00
|
|
|
tor_assert(srclen < SIZE_T_CEILING/8);
|
2004-07-22 10:30:06 +02:00
|
|
|
tor_assert((nbits%5) == 0); /* We need an even multiple of 5 bits. */
|
|
|
|
tor_assert((nbits/5)+1 <= destlen); /* We need enough space. */
|
2004-12-02 05:33:01 +01:00
|
|
|
tor_assert(destlen < SIZE_T_CEILING);
|
2004-03-30 21:47:32 +02:00
|
|
|
|
|
|
|
for (i=0,bit=0; bit < nbits; ++i, bit+=5) {
|
|
|
|
/* set v to the 16-bit value starting at src[bits/8], 0-padded. */
|
2004-04-08 22:56:33 +02:00
|
|
|
v = ((uint8_t)src[bit/8]) << 8;
|
|
|
|
if (bit+5<nbits) v += (uint8_t)src[(bit/8)+1];
|
2004-03-30 21:47:32 +02:00
|
|
|
/* set u to the 5-bit value at the bit'th bit of src. */
|
|
|
|
u = (v >> (11-(bit%8))) & 0x1F;
|
|
|
|
dest[i] = BASE32_CHARS[u];
|
|
|
|
}
|
|
|
|
dest[i] = '\0';
|
|
|
|
}
|
2004-04-03 04:40:30 +02:00
|
|
|
|
2012-11-23 15:51:31 +01:00
|
|
|
/** Implements base32 decoding as in RFC 4648. Limitation: Requires
|
2007-09-18 19:07:56 +02:00
|
|
|
* that srclen*5 is a multiple of 8. Returns 0 if successful, -1 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
|
|
|
|
{
|
2007-10-17 21:23:56 +02:00
|
|
|
/* XXXX we might want to rewrite this along the lines of base64_decode, if
|
|
|
|
* it ever shows up in the profile. */
|
2011-01-05 18:49:02 +01:00
|
|
|
unsigned int i;
|
|
|
|
size_t nbits, j, bit;
|
2007-09-18 19:07:56 +02:00
|
|
|
char *tmp;
|
|
|
|
nbits = srclen * 5;
|
|
|
|
|
2011-01-03 22:16:53 +01:00
|
|
|
tor_assert(srclen < SIZE_T_CEILING / 5);
|
2007-09-18 19:07:56 +02:00
|
|
|
tor_assert((nbits%8) == 0); /* We need an even multiple of 8 bits. */
|
|
|
|
tor_assert((nbits/8) <= destlen); /* We need enough space. */
|
|
|
|
tor_assert(destlen < SIZE_T_CEILING);
|
|
|
|
|
2014-12-22 18:56:35 +01:00
|
|
|
memset(dest, 0, destlen);
|
|
|
|
|
2007-09-18 19:07:56 +02:00
|
|
|
/* Convert base32 encoded chars to the 5-bit values that they represent. */
|
|
|
|
tmp = tor_malloc_zero(srclen);
|
|
|
|
for (j = 0; j < srclen; ++j) {
|
|
|
|
if (src[j] > 0x60 && src[j] < 0x7B) tmp[j] = src[j] - 0x61;
|
|
|
|
else if (src[j] > 0x31 && src[j] < 0x38) tmp[j] = src[j] - 0x18;
|
2007-10-15 17:38:44 +02:00
|
|
|
else if (src[j] > 0x40 && src[j] < 0x5B) tmp[j] = src[j] - 0x41;
|
2007-09-18 19:07:56 +02:00
|
|
|
else {
|
|
|
|
log_warn(LD_BUG, "illegal character in base32 encoded string");
|
2007-10-15 17:38:44 +02:00
|
|
|
tor_free(tmp);
|
2007-09-18 19:07:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble result byte-wise by applying five possible cases. */
|
|
|
|
for (i = 0, bit = 0; bit < nbits; ++i, bit += 8) {
|
|
|
|
switch (bit % 40) {
|
|
|
|
case 0:
|
|
|
|
dest[i] = (((uint8_t)tmp[(bit/5)]) << 3) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+1]) >> 2);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
dest[i] = (((uint8_t)tmp[(bit/5)]) << 6) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+1]) << 1) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+2]) >> 4);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
dest[i] = (((uint8_t)tmp[(bit/5)]) << 4) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+1]) >> 1);
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
dest[i] = (((uint8_t)tmp[(bit/5)]) << 7) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+1]) << 2) +
|
|
|
|
(((uint8_t)tmp[(bit/5)+2]) >> 3);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
dest[i] = (((uint8_t)tmp[(bit/5)]) << 5) +
|
|
|
|
((uint8_t)tmp[(bit/5)+1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(tmp, 0, srclen);
|
2007-09-18 19:07:56 +02:00
|
|
|
tor_free(tmp);
|
|
|
|
tmp = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-03 20:49:03 +01:00
|
|
|
/** Implement RFC2440-style iterated-salted S2K conversion: convert the
|
|
|
|
* <b>secret_len</b>-byte <b>secret</b> into a <b>key_out_len</b> byte
|
|
|
|
* <b>key_out</b>. As in RFC2440, the first 8 bytes of s2k_specifier
|
|
|
|
* are a salt; the 9th byte describes how much iteration to do.
|
|
|
|
* Does not support <b>key_out_len</b> > DIGEST_LEN.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
secret_to_key(char *key_out, size_t key_out_len, const char *secret,
|
|
|
|
size_t secret_len, const char *s2k_specifier)
|
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_t *d;
|
2004-11-03 20:49:03 +01:00
|
|
|
uint8_t c;
|
2008-02-07 17:10:36 +01:00
|
|
|
size_t count, tmplen;
|
2004-11-03 20:49:03 +01:00
|
|
|
char *tmp;
|
2004-12-02 05:33:01 +01:00
|
|
|
tor_assert(key_out_len < SIZE_T_CEILING);
|
2004-11-03 20:49:03 +01:00
|
|
|
|
|
|
|
#define EXPBIAS 6
|
|
|
|
c = s2k_specifier[8];
|
|
|
|
count = ((uint32_t)16 + (c & 15)) << ((c >> 4) + EXPBIAS);
|
|
|
|
#undef EXPBIAS
|
|
|
|
|
|
|
|
tor_assert(key_out_len <= DIGEST_LEN);
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
d = crypto_digest_new();
|
2008-02-07 17:10:36 +01:00
|
|
|
tmplen = 8+secret_len;
|
|
|
|
tmp = tor_malloc(tmplen);
|
2004-11-03 20:49:03 +01:00
|
|
|
memcpy(tmp,s2k_specifier,8);
|
|
|
|
memcpy(tmp+8,secret,secret_len);
|
|
|
|
secret_len += 8;
|
|
|
|
while (count) {
|
|
|
|
if (count >= secret_len) {
|
|
|
|
crypto_digest_add_bytes(d, tmp, secret_len);
|
|
|
|
count -= secret_len;
|
|
|
|
} else {
|
|
|
|
crypto_digest_add_bytes(d, tmp, count);
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
crypto_digest_get_digest(d, key_out, key_out_len);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(tmp, 0, tmplen);
|
2004-11-03 20:49:03 +01:00
|
|
|
tor_free(tmp);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_digest_free(d);
|
2004-11-03 20:49:03 +01:00
|
|
|
}
|
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
/**
|
|
|
|
* Destroy the <b>sz</b> bytes of data stored at <b>mem</b>, setting them to
|
|
|
|
* the value <b>byte</b>.
|
2016-01-19 01:22:58 +01:00
|
|
|
* If <b>mem</b> is NULL or <b>sz</b> is zero, nothing happens.
|
2012-11-07 22:09:58 +01:00
|
|
|
*
|
|
|
|
* This function is preferable to memset, since many compilers will happily
|
|
|
|
* optimize out memset() when they can convince themselves that the data being
|
|
|
|
* cleared will never be read.
|
|
|
|
*
|
|
|
|
* Right now, our convention is to use this function when we are wiping data
|
|
|
|
* that's about to become inaccessible, such as stack buffers that are about
|
|
|
|
* to go out of scope or structures that are about to get freed. (In
|
|
|
|
* practice, it appears that the compilers we're currently using will optimize
|
|
|
|
* out the memset()s for stack-allocated buffers, but not those for
|
|
|
|
* about-to-be-freed structures. That could change, though, so we're being
|
|
|
|
* wary.) If there are live reads for the data, then you can just use
|
|
|
|
* memset().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
memwipe(void *mem, uint8_t byte, size_t sz)
|
|
|
|
{
|
2016-01-19 14:28:58 +01:00
|
|
|
if (sz == 0) {
|
2016-01-19 01:22:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-19 14:28:58 +01:00
|
|
|
/* If sz is nonzero, then mem must not be NULL. */
|
|
|
|
tor_assert(mem != NULL);
|
2016-01-19 01:22:58 +01:00
|
|
|
|
|
|
|
/* Data this large is likely to be an underflow. */
|
|
|
|
tor_assert(sz < SIZE_T_CEILING);
|
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
/* Because whole-program-optimization exists, we may not be able to just
|
|
|
|
* have this function call "memset". A smart compiler could inline it, then
|
|
|
|
* eliminate dead memsets, and declare itself to be clever. */
|
|
|
|
|
|
|
|
/* This is a slow and ugly function from OpenSSL that fills 'mem' with junk
|
|
|
|
* based on the pointer value, then uses that junk to update a global
|
|
|
|
* variable. It's an elaborate ruse to trick the compiler into not
|
|
|
|
* optimizing out the "wipe this memory" code. Read it if you like zany
|
|
|
|
* programming tricks! In later versions of Tor, we should look for better
|
|
|
|
* not-optimized-out memory wiping stuff. */
|
|
|
|
OPENSSL_cleanse(mem, sz);
|
|
|
|
/* Just in case some caller of memwipe() is relying on getting a buffer
|
|
|
|
* filled with a particular value, fill the buffer.
|
|
|
|
*
|
|
|
|
* If this function gets inlined, this memset might get eliminated, but
|
|
|
|
* that's okay: We only care about this particular memset in the case where
|
|
|
|
* the caller should have been using memset(), and the memset() wouldn't get
|
|
|
|
* eliminated. In other words, this is here so that we won't break anything
|
|
|
|
* if somebody accidentally calls memwipe() instead of memset().
|
|
|
|
**/
|
|
|
|
memset(mem, byte, sz);
|
|
|
|
}
|
|
|
|
|
2005-02-13 23:32:25 +01:00
|
|
|
#ifdef TOR_IS_MULTITHREADED
|
2013-03-11 18:23:10 +01:00
|
|
|
|
|
|
|
#ifndef OPENSSL_THREADS
|
|
|
|
#error OpenSSL has been built without thread support. Tor requires an \
|
|
|
|
OpenSSL library with thread support enabled.
|
|
|
|
#endif
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** Helper: OpenSSL uses this callback to manipulate mutexes. */
|
2005-02-13 23:32:25 +01:00
|
|
|
static void
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_locking_cb_(int mode, int n, const char *file, int line)
|
2005-02-13 23:32:25 +01:00
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void)file;
|
|
|
|
(void)line;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (!openssl_mutexes_)
|
2014-03-23 05:12:40 +01:00
|
|
|
/* This is not a really good fix for the
|
2005-04-01 04:37:10 +02:00
|
|
|
* "release-freed-lock-from-separate-thread-on-shutdown" problem, but
|
|
|
|
* it can't hurt. */
|
|
|
|
return;
|
2005-02-13 23:32:25 +01:00
|
|
|
if (mode & CRYPTO_LOCK)
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_mutex_acquire(openssl_mutexes_[n]);
|
2005-02-13 23:32:25 +01:00
|
|
|
else
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_mutex_release(openssl_mutexes_[n]);
|
2005-02-13 23:32:25 +01:00
|
|
|
}
|
2005-09-30 03:09:52 +02:00
|
|
|
|
2009-05-28 17:54:56 +02:00
|
|
|
/** OpenSSL helper type: wraps a Tor mutex so that OpenSSL can use it
|
|
|
|
* as a lock. */
|
2008-06-13 18:35:12 +02:00
|
|
|
struct CRYPTO_dynlock_value {
|
|
|
|
tor_mutex_t *lock;
|
|
|
|
};
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** OpenSSL callback function to allocate a lock: see CRYPTO_set_dynlock_*
|
2008-12-22 18:53:04 +01:00
|
|
|
* documentation in OpenSSL's docs for more info. */
|
2008-06-13 18:35:12 +02:00
|
|
|
static struct CRYPTO_dynlock_value *
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_dynlock_create_cb_(const char *file, int line)
|
2008-06-13 18:35:12 +02:00
|
|
|
{
|
|
|
|
struct CRYPTO_dynlock_value *v;
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
v = tor_malloc(sizeof(struct CRYPTO_dynlock_value));
|
|
|
|
v->lock = tor_mutex_new();
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** OpenSSL callback function to acquire or release a lock: see
|
2008-12-22 18:53:04 +01:00
|
|
|
* CRYPTO_set_dynlock_* documentation in OpenSSL's docs for more info. */
|
2008-06-13 18:35:12 +02:00
|
|
|
static void
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_dynlock_lock_cb_(int mode, struct CRYPTO_dynlock_value *v,
|
2008-06-13 18:35:12 +02:00
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
if (mode & CRYPTO_LOCK)
|
|
|
|
tor_mutex_acquire(v->lock);
|
|
|
|
else
|
|
|
|
tor_mutex_release(v->lock);
|
|
|
|
}
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** OpenSSL callback function to free a lock: see CRYPTO_set_dynlock_*
|
2008-12-22 18:53:04 +01:00
|
|
|
* documentation in OpenSSL's docs for more info. */
|
2008-06-13 18:35:12 +02:00
|
|
|
static void
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_dynlock_destroy_cb_(struct CRYPTO_dynlock_value *v,
|
2008-06-13 18:35:12 +02:00
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
(void)file;
|
|
|
|
(void)line;
|
|
|
|
tor_mutex_free(v->lock);
|
|
|
|
tor_free(v);
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Helper: Construct mutexes, and set callbacks to help OpenSSL handle being
|
|
|
|
* multithreaded. */
|
2005-02-13 23:32:25 +01:00
|
|
|
static int
|
2005-09-30 03:09:52 +02:00
|
|
|
setup_openssl_threading(void)
|
|
|
|
{
|
2005-02-13 23:32:25 +01:00
|
|
|
int i;
|
|
|
|
int n = CRYPTO_num_locks();
|
2012-10-12 18:22:13 +02:00
|
|
|
n_openssl_mutexes_ = n;
|
|
|
|
openssl_mutexes_ = tor_malloc(n*sizeof(tor_mutex_t *));
|
2005-03-23 07:20:50 +01:00
|
|
|
for (i=0; i < n; ++i)
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_mutexes_[i] = tor_mutex_new();
|
|
|
|
CRYPTO_set_locking_callback(openssl_locking_cb_);
|
2005-02-13 23:32:25 +01:00
|
|
|
CRYPTO_set_id_callback(tor_get_thread_id);
|
2012-10-12 18:22:13 +02:00
|
|
|
CRYPTO_set_dynlock_create_callback(openssl_dynlock_create_cb_);
|
|
|
|
CRYPTO_set_dynlock_lock_callback(openssl_dynlock_lock_cb_);
|
|
|
|
CRYPTO_set_dynlock_destroy_callback(openssl_dynlock_destroy_cb_);
|
2005-02-13 23:32:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2005-09-30 03:09:52 +02:00
|
|
|
static int
|
|
|
|
setup_openssl_threading(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-13 23:32:25 +01:00
|
|
|
#endif
|
2011-11-22 15:11:40 +01:00
|
|
|
|
|
|
|
/** Uninitialize the crypto library. Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
crypto_global_cleanup(void)
|
|
|
|
{
|
|
|
|
EVP_cleanup();
|
|
|
|
ERR_remove_state(0);
|
|
|
|
ERR_free_strings();
|
|
|
|
|
2011-11-22 15:14:59 +01:00
|
|
|
if (dh_param_p)
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(dh_param_p);
|
2011-11-22 15:14:59 +01:00
|
|
|
if (dh_param_p_tls)
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(dh_param_p_tls);
|
2011-11-22 15:14:59 +01:00
|
|
|
if (dh_param_g)
|
2014-02-02 19:40:30 +01:00
|
|
|
BN_clear_free(dh_param_g);
|
2011-11-22 15:14:59 +01:00
|
|
|
|
2011-11-22 15:11:40 +01:00
|
|
|
#ifndef DISABLE_ENGINES
|
|
|
|
ENGINE_cleanup();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CONF_modules_unload(1);
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
#ifdef TOR_IS_MULTITHREADED
|
2012-10-12 18:22:13 +02:00
|
|
|
if (n_openssl_mutexes_) {
|
|
|
|
int n = n_openssl_mutexes_;
|
|
|
|
tor_mutex_t **ms = openssl_mutexes_;
|
2011-11-22 15:11:40 +01:00
|
|
|
int i;
|
2012-10-12 18:22:13 +02:00
|
|
|
openssl_mutexes_ = NULL;
|
|
|
|
n_openssl_mutexes_ = 0;
|
2011-11-22 15:11:40 +01:00
|
|
|
for (i=0;i<n;++i) {
|
|
|
|
tor_mutex_free(ms[i]);
|
|
|
|
}
|
|
|
|
tor_free(ms);
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-04 18:32:38 +02:00
|
|
|
tor_free(crypto_openssl_version_str);
|
2013-09-04 03:56:06 +02:00
|
|
|
tor_free(crypto_openssl_header_version_str);
|
2011-11-22 15:11:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2011-03-16 22:09:32 +01:00
|
|
|
|