2013-01-16 07:54:56 +01:00
|
|
|
/* Copyright (c) 2007-2013, The Tor Project, Inc. */
|
2007-05-01 00:43:01 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-06-04 01:38:20 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-06-04 17:30:40 +02:00
|
|
|
#include <fcntl.h>
|
2012-05-07 17:31:08 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2007-06-04 17:30:40 +02:00
|
|
|
#include <unistd.h>
|
2012-05-07 17:31:08 +02:00
|
|
|
#endif
|
2007-06-04 01:38:20 +02:00
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pem.h>
|
2009-09-29 06:49:43 +02:00
|
|
|
#include <openssl/rsa.h>
|
2007-05-01 00:43:01 +02:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/obj_mac.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#if 0
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CRYPTO_PRIVATE
|
|
|
|
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "compat.h"
|
2009-09-29 06:44:39 +02:00
|
|
|
#include "../common/util.h"
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "../common/torlog.h"
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "crypto.h"
|
2008-07-25 16:43:24 +02:00
|
|
|
#include "address.h"
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
#define IDENTITY_KEY_BITS 3072
|
|
|
|
#define SIGNING_KEY_BITS 1024
|
2007-05-01 03:10:50 +02:00
|
|
|
#define DEFAULT_LIFETIME 12
|
2007-05-01 00:43:01 +02:00
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/* These globals are set via command line options. */
|
2007-05-01 00:43:01 +02:00
|
|
|
char *identity_key_file = NULL;
|
|
|
|
char *signing_key_file = NULL;
|
|
|
|
char *certificate_file = NULL;
|
2007-10-19 18:28:10 +02:00
|
|
|
int reuse_signing_key = 0;
|
2007-05-01 00:43:01 +02:00
|
|
|
int verbose = 0;
|
|
|
|
int make_new_id = 0;
|
2007-05-01 03:10:50 +02:00
|
|
|
int months_lifetime = DEFAULT_LIFETIME;
|
2008-02-22 20:07:52 +01:00
|
|
|
int passphrase_fd = -1;
|
2007-10-19 18:28:10 +02:00
|
|
|
char *address = NULL;
|
2007-05-01 00:43:01 +02:00
|
|
|
|
2008-02-22 20:07:52 +01:00
|
|
|
char *passphrase = NULL;
|
|
|
|
size_t passphrase_len = 0;
|
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
EVP_PKEY *identity_key = NULL;
|
|
|
|
EVP_PKEY *signing_key = NULL;
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Write a usage message for tor-gencert to stderr. */
|
2007-05-01 00:43:01 +02:00
|
|
|
static void
|
|
|
|
show_help(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Syntax:\n"
|
2008-01-13 00:52:05 +01:00
|
|
|
"tor-gencert [-h|--help] [-v] [-r|--reuse] [--create-identity-key]\n"
|
|
|
|
" [-i identity_key_file] [-s signing_key_file] "
|
|
|
|
"[-c certificate_file]\n"
|
2008-02-22 20:07:52 +01:00
|
|
|
" [-m lifetime_in_months] [-a address:port] "
|
|
|
|
"[--passphrase-fd <fd>]\n");
|
2007-05-01 00:43:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXXX copied from crypto.c */
|
|
|
|
static void
|
|
|
|
crypto_log_errors(int severity, const char *doing)
|
|
|
|
{
|
2009-09-01 05:23:47 +02:00
|
|
|
unsigned long err;
|
2007-05-01 00:43:01 +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)";
|
|
|
|
if (!lib) lib = "(null)";
|
|
|
|
if (!func) func = "(null)";
|
|
|
|
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);
|
2007-05-01 00:43:01 +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);
|
2007-05-01 00:43:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-22 20:07:52 +01:00
|
|
|
/** Read the passphrase from the passphrase fd. */
|
|
|
|
static int
|
|
|
|
load_passphrase(void)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
char buf[1024]; /* "Ought to be enough for anybody." */
|
2009-09-01 05:23:47 +02:00
|
|
|
ssize_t n = read_all(passphrase_fd, buf, sizeof(buf), 0);
|
2008-02-22 20:07:52 +01:00
|
|
|
if (n < 0) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't read from passphrase fd: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp = memchr(buf, '\n', n);
|
|
|
|
passphrase_len = cp-buf;
|
|
|
|
passphrase = tor_strndup(buf, passphrase_len);
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(buf, 0, sizeof(buf));
|
2008-02-22 20:07:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_passphrase(void)
|
|
|
|
{
|
|
|
|
if (passphrase) {
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(passphrase, 0, passphrase_len);
|
2008-02-22 20:07:52 +01:00
|
|
|
tor_free(passphrase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Read the command line options from <b>argc</b> and <b>argv</b>,
|
|
|
|
* setting global option vars as needed.
|
|
|
|
*/
|
2007-05-01 00:43:01 +02:00
|
|
|
static int
|
|
|
|
parse_commandline(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
2008-09-05 21:55:04 +02:00
|
|
|
log_severity_list_t s;
|
2007-05-01 00:43:01 +02:00
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
|
|
|
|
show_help();
|
|
|
|
return 1;
|
|
|
|
} else if (!strcmp(argv[i], "-i")) {
|
|
|
|
if (i+1>=argc) {
|
|
|
|
fprintf(stderr, "No argument to -i\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
identity_key_file = tor_strdup(argv[++i]);
|
2008-01-10 18:08:05 +01:00
|
|
|
} else if (!strcmp(argv[i], "-s")) {
|
|
|
|
if (i+1>=argc) {
|
|
|
|
fprintf(stderr, "No argument to -s\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
signing_key_file = tor_strdup(argv[++i]);
|
2007-05-01 00:43:01 +02:00
|
|
|
} else if (!strcmp(argv[i], "-c")) {
|
|
|
|
if (i+1>=argc) {
|
|
|
|
fprintf(stderr, "No argument to -c\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
certificate_file = tor_strdup(argv[++i]);
|
2007-05-01 03:10:50 +02:00
|
|
|
} else if (!strcmp(argv[i], "-m")) {
|
|
|
|
if (i+1>=argc) {
|
|
|
|
fprintf(stderr, "No argument to -m\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
months_lifetime = atoi(argv[++i]);
|
|
|
|
if (months_lifetime > 24 || months_lifetime < 0) {
|
2011-11-25 14:16:33 +01:00
|
|
|
fprintf(stderr, "Lifetime (in months) was out of range.\n");
|
2007-05-01 03:10:50 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2007-10-19 18:28:10 +02:00
|
|
|
} else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--reuse")) {
|
|
|
|
reuse_signing_key = 1;
|
2007-05-01 00:43:01 +02:00
|
|
|
} else if (!strcmp(argv[i], "-v")) {
|
|
|
|
verbose = 1;
|
2007-10-19 18:28:10 +02:00
|
|
|
} else if (!strcmp(argv[i], "-a")) {
|
|
|
|
uint32_t addr;
|
|
|
|
uint16_t port;
|
|
|
|
char b[INET_NTOA_BUF_LEN];
|
|
|
|
struct in_addr in;
|
|
|
|
if (i+1>=argc) {
|
|
|
|
fprintf(stderr, "No argument to -a\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2011-10-11 17:21:31 +02:00
|
|
|
if (addr_port_lookup(LOG_ERR, argv[++i], NULL, &addr, &port)<0)
|
2007-10-19 18:28:10 +02:00
|
|
|
return 1;
|
|
|
|
in.s_addr = htonl(addr);
|
|
|
|
tor_inet_ntoa(&in, b, sizeof(b));
|
|
|
|
address = tor_malloc(INET_NTOA_BUF_LEN+32);
|
|
|
|
tor_snprintf(address, INET_NTOA_BUF_LEN+32, "%s:%d", b, (int)port);
|
2007-05-01 00:43:01 +02:00
|
|
|
} else if (!strcmp(argv[i], "--create-identity-key")) {
|
|
|
|
make_new_id = 1;
|
2008-02-22 20:07:52 +01:00
|
|
|
} else if (!strcmp(argv[i], "--passphrase-fd")) {
|
|
|
|
if (i+1>=argc) {
|
2008-03-05 23:39:49 +01:00
|
|
|
fprintf(stderr, "No argument to --passphrase-fd\n");
|
2008-02-22 20:07:52 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
passphrase_fd = atoi(argv[++i]);
|
2007-05-01 00:43:01 +02:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unrecognized option %s\n", argv[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 22:09:58 +01:00
|
|
|
memwipe(&s, 0, sizeof(s));
|
2008-03-13 23:18:38 +01:00
|
|
|
if (verbose)
|
2008-09-05 21:55:04 +02:00
|
|
|
set_log_severity_config(LOG_DEBUG, LOG_ERR, &s);
|
2008-03-13 23:18:38 +01:00
|
|
|
else
|
2008-09-05 21:55:04 +02:00
|
|
|
set_log_severity_config(LOG_WARN, LOG_ERR, &s);
|
2008-12-03 00:36:58 +01:00
|
|
|
add_stream_log(&s, "<stderr>", fileno(stderr));
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
if (!identity_key_file) {
|
|
|
|
identity_key_file = tor_strdup("./authority_identity_key");
|
|
|
|
log_info(LD_GENERAL, "No identity key file given; defaulting to %s",
|
|
|
|
identity_key_file);
|
|
|
|
}
|
|
|
|
if (!signing_key_file) {
|
|
|
|
signing_key_file = tor_strdup("./authority_signing_key");
|
|
|
|
log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
|
|
|
|
signing_key_file);
|
|
|
|
}
|
|
|
|
if (!certificate_file) {
|
|
|
|
certificate_file = tor_strdup("./authority_certificate");
|
|
|
|
log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
|
2008-01-10 18:08:05 +01:00
|
|
|
certificate_file);
|
2007-05-01 00:43:01 +02:00
|
|
|
}
|
2008-02-22 20:07:52 +01:00
|
|
|
if (passphrase_fd >= 0) {
|
|
|
|
if (load_passphrase()<0)
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-01 00:43:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-29 06:49:43 +02:00
|
|
|
static RSA *
|
|
|
|
generate_key(int bits)
|
|
|
|
{
|
|
|
|
RSA *rsa = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_t *env = crypto_pk_new();
|
2009-09-29 06:49:43 +02:00
|
|
|
if (crypto_pk_generate_key_with_bits(env,bits)<0)
|
|
|
|
goto done;
|
2012-10-12 18:22:13 +02:00
|
|
|
rsa = crypto_pk_get_rsa_(env);
|
2009-09-29 06:49:43 +02:00
|
|
|
rsa = RSAPrivateKey_dup(rsa);
|
|
|
|
done:
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(env);
|
2009-09-29 06:49:43 +02:00
|
|
|
return rsa;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Try to read the identity key from <b>identity_key_file</b>. If no such
|
|
|
|
* file exists and create_identity_key is set, make a new identity key and
|
|
|
|
* store it. Return 0 on success, nonzero on failure.
|
|
|
|
*/
|
2007-05-01 00:43:01 +02:00
|
|
|
static int
|
|
|
|
load_identity_key(void)
|
|
|
|
{
|
|
|
|
file_status_t status = file_status(identity_key_file);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (make_new_id) {
|
2007-08-29 21:02:43 +02:00
|
|
|
open_file_t *open_file = NULL;
|
2007-05-01 00:43:01 +02:00
|
|
|
RSA *key;
|
|
|
|
if (status != FN_NOENT) {
|
|
|
|
log_err(LD_GENERAL, "--create-identity-key was specified, but %s "
|
|
|
|
"already exists.", identity_key_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
log_notice(LD_GENERAL, "Generating %d-bit RSA identity key.",
|
|
|
|
IDENTITY_KEY_BITS);
|
2009-09-29 06:49:43 +02:00
|
|
|
if (!(key = generate_key(IDENTITY_KEY_BITS))) {
|
2007-05-01 00:43:01 +02:00
|
|
|
log_err(LD_GENERAL, "Couldn't generate identity key.");
|
|
|
|
crypto_log_errors(LOG_ERR, "Generating identity key");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
identity_key = EVP_PKEY_new();
|
|
|
|
if (!(EVP_PKEY_assign_RSA(identity_key, key))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't assign identity key.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-29 21:02:43 +02:00
|
|
|
if (!(f = start_writing_to_stdio_file(identity_key_file,
|
2011-07-19 03:12:31 +02:00
|
|
|
OPEN_FLAGS_REPLACE | O_TEXT, 0400,
|
2007-08-29 21:02:43 +02:00
|
|
|
&open_file)))
|
2007-05-01 00:43:01 +02:00
|
|
|
return 1;
|
|
|
|
|
2008-02-22 20:07:52 +01:00
|
|
|
/* Write the key to the file. If passphrase is not set, takes it from
|
|
|
|
* the terminal. */
|
2007-05-01 00:43:01 +02:00
|
|
|
if (!PEM_write_PKCS8PrivateKey_nid(f, identity_key,
|
|
|
|
NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
|
2008-02-22 20:07:52 +01:00
|
|
|
passphrase, (int)passphrase_len,
|
2007-05-01 00:43:01 +02:00
|
|
|
NULL, NULL)) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't write identity key to %s",
|
|
|
|
identity_key_file);
|
|
|
|
crypto_log_errors(LOG_ERR, "Writing identity key");
|
2007-08-29 21:02:43 +02:00
|
|
|
abort_writing_to_file(open_file);
|
2007-05-01 00:43:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2007-08-29 21:02:43 +02:00
|
|
|
finish_writing_to_file(open_file);
|
2007-05-01 00:43:01 +02:00
|
|
|
} else {
|
|
|
|
if (status != FN_FILE) {
|
|
|
|
log_err(LD_GENERAL,
|
|
|
|
"No identity key found in %s. To specify a location "
|
|
|
|
"for an identity key, use -i. To generate a new identity key, "
|
|
|
|
"use --create-identity-key.", identity_key_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(f = fopen(identity_key_file, "r"))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't open %s for reading: %s",
|
|
|
|
identity_key_file, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-22 20:07:52 +01:00
|
|
|
/* Read the key. If passphrase is not set, takes it from the terminal. */
|
|
|
|
identity_key = PEM_read_PrivateKey(f, NULL, NULL, passphrase);
|
2007-05-01 00:43:01 +02:00
|
|
|
if (!identity_key) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't read identity key from %s",
|
|
|
|
identity_key_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Load a saved signing key from disk. Return 0 on success, nonzero on
|
|
|
|
* failure. */
|
2007-10-19 18:28:10 +02:00
|
|
|
static int
|
|
|
|
load_signing_key(void)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if (!(f = fopen(signing_key_file, "r"))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't open %s for reading: %s",
|
|
|
|
signing_key_file, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!(signing_key = PEM_read_PrivateKey(f, NULL, NULL, NULL))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't read siging key from %s", signing_key_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Generate a new signing key and write it to disk. Return 0 on success,
|
|
|
|
* nonzero on failure. */
|
2007-05-01 00:43:01 +02:00
|
|
|
static int
|
|
|
|
generate_signing_key(void)
|
|
|
|
{
|
2007-08-29 21:02:43 +02:00
|
|
|
open_file_t *open_file;
|
2007-05-01 00:43:01 +02:00
|
|
|
FILE *f;
|
|
|
|
RSA *key;
|
|
|
|
log_notice(LD_GENERAL, "Generating %d-bit RSA signing key.",
|
|
|
|
SIGNING_KEY_BITS);
|
2009-09-29 06:49:43 +02:00
|
|
|
if (!(key = generate_key(SIGNING_KEY_BITS))) {
|
2007-05-01 00:43:01 +02:00
|
|
|
log_err(LD_GENERAL, "Couldn't generate signing key.");
|
|
|
|
crypto_log_errors(LOG_ERR, "Generating signing key");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
signing_key = EVP_PKEY_new();
|
|
|
|
if (!(EVP_PKEY_assign_RSA(signing_key, key))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't assign signing key.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-29 21:02:43 +02:00
|
|
|
if (!(f = start_writing_to_stdio_file(signing_key_file,
|
2011-07-19 03:12:31 +02:00
|
|
|
OPEN_FLAGS_REPLACE | O_TEXT, 0600,
|
2007-08-29 21:02:43 +02:00
|
|
|
&open_file)))
|
2007-06-04 17:30:40 +02:00
|
|
|
return 1;
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
/* Write signing key with no encryption. */
|
|
|
|
if (!PEM_write_RSAPrivateKey(f, key, NULL, NULL, 0, NULL, NULL)) {
|
|
|
|
crypto_log_errors(LOG_WARN, "writing signing key");
|
2007-08-29 21:02:43 +02:00
|
|
|
abort_writing_to_file(open_file);
|
2007-05-01 00:43:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-29 21:02:43 +02:00
|
|
|
finish_writing_to_file(open_file);
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Encode <b>key</b> in the format used in directory documents; return
|
|
|
|
* a newly allocated string holding the result or NULL on failure. */
|
2007-05-01 00:43:01 +02:00
|
|
|
static char *
|
|
|
|
key_to_string(EVP_PKEY *key)
|
|
|
|
{
|
|
|
|
BUF_MEM *buf;
|
|
|
|
BIO *b;
|
|
|
|
RSA *rsa = EVP_PKEY_get1_RSA(key);
|
|
|
|
char *result;
|
|
|
|
if (!rsa)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
b = BIO_new(BIO_s_mem());
|
|
|
|
if (!PEM_write_bio_RSAPublicKey(b, rsa)) {
|
|
|
|
crypto_log_errors(LOG_WARN, "writing public key to string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_get_mem_ptr(b, &buf);
|
|
|
|
(void) BIO_set_close(b, BIO_NOCLOSE);
|
|
|
|
BIO_free(b);
|
|
|
|
result = tor_malloc(buf->length + 1);
|
|
|
|
memcpy(result, buf->data, buf->length);
|
|
|
|
result[buf->length] = 0;
|
|
|
|
BUF_MEM_free(buf);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Set <b>out</b> to the hex-encoded fingerprint of <b>pkey</b>. */
|
2007-05-01 00:43:01 +02:00
|
|
|
static int
|
|
|
|
get_fingerprint(EVP_PKEY *pkey, char *out)
|
|
|
|
{
|
|
|
|
int r = 1;
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_pk_t *pk = crypto_new_pk_from_rsa_(EVP_PKEY_get1_RSA(pkey));
|
2007-05-01 00:43:01 +02:00
|
|
|
if (pk) {
|
|
|
|
r = crypto_pk_get_fingerprint(pk, out, 0);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pk);
|
2007-05-01 00:43:01 +02:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-12-12 19:31:39 +01:00
|
|
|
/** Set <b>out</b> to the hex-encoded fingerprint of <b>pkey</b>. */
|
|
|
|
static int
|
|
|
|
get_digest(EVP_PKEY *pkey, char *out)
|
|
|
|
{
|
|
|
|
int r = 1;
|
2012-10-12 18:22:13 +02:00
|
|
|
crypto_pk_t *pk = crypto_new_pk_from_rsa_(EVP_PKEY_get1_RSA(pkey));
|
2008-12-12 19:31:39 +01:00
|
|
|
if (pk) {
|
|
|
|
r = crypto_pk_get_digest(pk, out);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(pk);
|
2008-12-12 19:31:39 +01:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Generate a new certificate for our loaded or generated keys, and write it
|
|
|
|
* to disk. Return 0 on success, nonzero on failure. */
|
2007-05-01 00:43:01 +02:00
|
|
|
static int
|
|
|
|
generate_certificate(void)
|
|
|
|
{
|
|
|
|
char buf[8192];
|
|
|
|
time_t now = time(NULL);
|
2007-05-01 03:10:50 +02:00
|
|
|
struct tm tm;
|
2007-05-01 00:43:01 +02:00
|
|
|
char published[ISO_TIME_LEN+1];
|
2007-05-01 03:10:50 +02:00
|
|
|
char expires[ISO_TIME_LEN+1];
|
2008-12-12 19:31:39 +01:00
|
|
|
char id_digest[DIGEST_LEN];
|
2007-05-01 00:43:01 +02:00
|
|
|
char fingerprint[FINGERPRINT_LEN+1];
|
|
|
|
char *ident = key_to_string(identity_key);
|
|
|
|
char *signing = key_to_string(signing_key);
|
|
|
|
FILE *f;
|
|
|
|
size_t signed_len;
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
char signature[1024]; /* handles up to 8192-bit keys. */
|
|
|
|
int r;
|
|
|
|
|
|
|
|
get_fingerprint(identity_key, fingerprint);
|
2008-12-12 19:31:39 +01:00
|
|
|
get_digest(identity_key, id_digest);
|
2007-05-01 03:10:50 +02:00
|
|
|
|
|
|
|
tor_localtime_r(&now, &tm);
|
|
|
|
tm.tm_mon += months_lifetime;
|
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
format_iso_time(published, now);
|
2007-05-01 03:10:50 +02:00
|
|
|
format_iso_time(expires, mktime(&tm));
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
tor_snprintf(buf, sizeof(buf),
|
2007-10-19 18:28:10 +02:00
|
|
|
"dir-key-certificate-version 3"
|
|
|
|
"%s%s"
|
|
|
|
"\nfingerprint %s\n"
|
2007-05-01 00:43:01 +02:00
|
|
|
"dir-key-published %s\n"
|
2007-05-01 03:10:50 +02:00
|
|
|
"dir-key-expires %s\n"
|
2007-05-01 00:43:01 +02:00
|
|
|
"dir-identity-key\n%s"
|
|
|
|
"dir-signing-key\n%s"
|
2008-12-12 19:31:39 +01:00
|
|
|
"dir-key-crosscert\n"
|
|
|
|
"-----BEGIN ID SIGNATURE-----\n",
|
2007-10-19 18:28:10 +02:00
|
|
|
address?"\ndir-address ":"", address?address:"",
|
2008-12-12 19:31:39 +01:00
|
|
|
fingerprint, published, expires, ident, signing
|
|
|
|
);
|
2008-01-16 06:27:19 +01:00
|
|
|
tor_free(ident);
|
|
|
|
tor_free(signing);
|
2008-12-12 19:31:39 +01:00
|
|
|
|
|
|
|
/* Append a cross-certification */
|
|
|
|
r = RSA_private_encrypt(DIGEST_LEN, (unsigned char*)id_digest,
|
|
|
|
(unsigned char*)signature,
|
|
|
|
EVP_PKEY_get1_RSA(signing_key),
|
|
|
|
RSA_PKCS1_PADDING);
|
|
|
|
signed_len = strlen(buf);
|
|
|
|
base64_encode(buf+signed_len, sizeof(buf)-signed_len, signature, r);
|
|
|
|
|
|
|
|
strlcat(buf,
|
|
|
|
"-----END ID SIGNATURE-----\n"
|
|
|
|
"dir-key-certification\n", sizeof(buf));
|
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
signed_len = strlen(buf);
|
|
|
|
SHA1((const unsigned char*)buf,signed_len,(unsigned char*)digest);
|
|
|
|
|
|
|
|
r = RSA_private_encrypt(DIGEST_LEN, (unsigned char*)digest,
|
|
|
|
(unsigned char*)signature,
|
|
|
|
EVP_PKEY_get1_RSA(identity_key),
|
|
|
|
RSA_PKCS1_PADDING);
|
2007-06-13 20:15:59 +02:00
|
|
|
strlcat(buf, "-----BEGIN SIGNATURE-----\n", sizeof(buf));
|
2007-05-01 00:43:01 +02:00
|
|
|
signed_len = strlen(buf);
|
|
|
|
base64_encode(buf+signed_len, sizeof(buf)-signed_len, signature, r);
|
2007-06-13 20:15:59 +02:00
|
|
|
strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
if (!(f = fopen(certificate_file, "w"))) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
|
|
|
|
certificate_file, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-16 17:38:39 +02:00
|
|
|
if (fputs(buf, f) < 0) {
|
|
|
|
log_err(LD_GENERAL, "Couldn't write to %s: %s",
|
|
|
|
certificate_file, strerror(errno));
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-01 00:43:01 +02:00
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Entry point to tor-gencert */
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int r = 1;
|
2008-01-03 23:44:32 +01:00
|
|
|
init_logging();
|
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
/* Don't bother using acceleration. */
|
2009-05-24 01:42:44 +02:00
|
|
|
if (crypto_global_init(0, NULL, NULL)) {
|
2007-05-01 00:43:01 +02:00
|
|
|
fprintf(stderr, "Couldn't initialize crypto library.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-21 23:01:11 +01:00
|
|
|
if (crypto_seed_rng(1)) {
|
2007-05-01 00:43:01 +02:00
|
|
|
fprintf(stderr, "Couldn't seed RNG.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2007-06-04 01:38:20 +02:00
|
|
|
/* Make sure that files are made private. */
|
2007-06-04 17:30:40 +02:00
|
|
|
umask(0077);
|
2007-06-04 01:38:20 +02:00
|
|
|
|
2007-05-01 00:43:01 +02:00
|
|
|
if (parse_commandline(argc, argv))
|
|
|
|
goto done;
|
|
|
|
if (load_identity_key())
|
|
|
|
goto done;
|
2007-10-19 18:28:10 +02:00
|
|
|
if (reuse_signing_key) {
|
|
|
|
if (load_signing_key())
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
if (generate_signing_key())
|
|
|
|
goto done;
|
|
|
|
}
|
2007-05-01 00:43:01 +02:00
|
|
|
if (generate_certificate())
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
done:
|
2008-02-22 20:07:52 +01:00
|
|
|
clear_passphrase();
|
2007-05-01 00:43:01 +02:00
|
|
|
if (identity_key)
|
|
|
|
EVP_PKEY_free(identity_key);
|
|
|
|
if (signing_key)
|
|
|
|
EVP_PKEY_free(signing_key);
|
2007-10-19 18:28:10 +02:00
|
|
|
tor_free(address);
|
2007-05-01 00:43:01 +02:00
|
|
|
|
|
|
|
crypto_global_cleanup();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|